···39 * own interruptible routines.40 */41static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)42-{ 43 struct api_context ctx;44 unsigned long expire;45 int retval;···74}7576/*-------------------------------------------------------------------*/77-// returns status (negative) or length (positive)78static int usb_internal_control_msg(struct usb_device *usb_dev,79- unsigned int pipe, 80 struct usb_ctrlrequest *cmd,81 void *data, int len, int timeout)82{···87 urb = usb_alloc_urb(0, GFP_NOIO);88 if (!urb)89 return -ENOMEM;90-91 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,92 len, usb_api_blocking_completion, NULL);93···99}100101/**102- * usb_control_msg - Builds a control urb, sends it off and waits for completion103- * @dev: pointer to the usb device to send the message to104- * @pipe: endpoint "pipe" to send the message to105- * @request: USB message request value106- * @requesttype: USB message request type value107- * @value: USB message value108- * @index: USB message index value109- * @data: pointer to the data to send110- * @size: length in bytes of the data to send111- * @timeout: time in msecs to wait for the message to complete before112- * timing out (if 0 the wait is forever)113- * Context: !in_interrupt ()114 *115- * This function sends a simple control message to a specified endpoint116- * and waits for the message to complete, or timeout.117- * 118- * If successful, it returns the number of bytes transferred, otherwise a negative error number.119 *120- * Don't use this function from within an interrupt context, like a121- * bottom half handler. If you need an asynchronous message, or need to send122- * a message from within interrupt context, use usb_submit_urb()123- * If a thread in your driver uses this call, make sure your disconnect()124- * method can wait for it to complete. Since you don't have a handle on125- * the URB used, you can't cancel the request.000000126 */127-int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,128- __u16 value, __u16 index, void *data, __u16 size, int timeout)0129{130- struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);131 int ret;132-0133 if (!dr)134 return -ENOMEM;135136- dr->bRequestType= requesttype;137 dr->bRequest = request;138 dr->wValue = cpu_to_le16p(&value);139 dr->wIndex = cpu_to_le16p(&index);140 dr->wLength = cpu_to_le16p(&size);141142- //dbg("usb_control_msg"); 143144 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);145···159 * @pipe: endpoint "pipe" to send the message to160 * @data: pointer to the data to send161 * @len: length in bytes of the data to send162- * @actual_length: pointer to a location to put the actual length transferred in bytes0163 * @timeout: time in msecs to wait for the message to complete before164 * timing out (if 0 the wait is forever)0165 * Context: !in_interrupt ()166 *167 * This function sends a simple interrupt message to a specified endpoint and···187EXPORT_SYMBOL_GPL(usb_interrupt_msg);188189/**190- * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion191- * @usb_dev: pointer to the usb device to send the message to192- * @pipe: endpoint "pipe" to send the message to193- * @data: pointer to the data to send194- * @len: length in bytes of the data to send195- * @actual_length: pointer to a location to put the actual length transferred in bytes196- * @timeout: time in msecs to wait for the message to complete before197- * timing out (if 0 the wait is forever)198- * Context: !in_interrupt ()199 *200- * This function sends a simple bulk message to a specified endpoint201- * and waits for the message to complete, or timeout.202- * 203- * If successful, it returns 0, otherwise a negative error number.204- * The number of actual bytes transferred will be stored in the 205- * actual_length paramater.206 *207- * Don't use this function from within an interrupt context, like a208- * bottom half handler. If you need an asynchronous message, or need to209- * send a message from within interrupt context, use usb_submit_urb()210- * If a thread in your driver uses this call, make sure your disconnect()211- * method can wait for it to complete. Since you don't have a handle on212- * the URB used, you can't cancel the request.213 *214- * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT215- * ioctl, users are forced to abuse this routine by using it to submit216- * URBs for interrupt endpoints. We will take the liberty of creating217- * an interrupt URB (with the default interval) if the target is an218- * interrupt endpoint.000000000219 */220-int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 221- void *data, int len, int *actual_length, int timeout)222{223 struct urb *urb;224 struct usb_host_endpoint *ep;···248249/*-------------------------------------------------------------------*/250251-static void sg_clean (struct usb_sg_request *io)252{253 if (io->urbs) {254 while (io->entries--)255- usb_free_urb (io->urbs [io->entries]);256- kfree (io->urbs);257 io->urbs = NULL;258 }259 if (io->dev->dev.dma_mask != NULL)260- usb_buffer_unmap_sg (io->dev, usb_pipein(io->pipe),261- io->sg, io->nents);262 io->dev = NULL;263}264265-static void sg_complete (struct urb *urb)266{267- struct usb_sg_request *io = urb->context;268 int status = urb->status;269270- spin_lock (&io->lock);271272 /* In 2.5 we require hcds' endpoint queues not to progress after fault273 * reports, until the completion callback (this!) returns. That lets···283 && (io->status != -ECONNRESET284 || status != -ECONNRESET)285 && urb->actual_length) {286- dev_err (io->dev->bus->controller,287 "dev %s ep%d%s scatterlist error %d/%d\n",288 io->dev->devpath,289 usb_endpoint_num(&urb->ep->desc),290 usb_urb_dir_in(urb) ? "in" : "out",291 status, io->status);292- // BUG ();293 }294295 if (io->status == 0 && status && status != -ECONNRESET) {···301 * unlink pending urbs so they won't rx/tx bad data.302 * careful: unlink can sometimes be synchronous...303 */304- spin_unlock (&io->lock);305 for (i = 0, found = 0; i < io->entries; i++) {306 if (!io->urbs [i] || !io->urbs [i]->dev)307 continue;308 if (found) {309- retval = usb_unlink_urb (io->urbs [i]);310 if (retval != -EINPROGRESS &&311 retval != -ENODEV &&312 retval != -EBUSY)313- dev_err (&io->dev->dev,314 "%s, unlink --> %d\n",315 __FUNCTION__, retval);316 } else if (urb == io->urbs [i])317 found = 1;318 }319- spin_lock (&io->lock);320 }321 urb->dev = NULL;322···324 io->bytes += urb->actual_length;325 io->count--;326 if (!io->count)327- complete (&io->complete);328329- spin_unlock (&io->lock);330}331332···355 * The request may be canceled with usb_sg_cancel(), either before or after356 * usb_sg_wait() is called.357 */358-int usb_sg_init (359- struct usb_sg_request *io,360- struct usb_device *dev,361- unsigned pipe, 362- unsigned period,363- struct scatterlist *sg,364- int nents,365- size_t length,366- gfp_t mem_flags367-)368{369- int i;370- int urb_flags;371- int dma;372373 if (!io || !dev || !sg374- || usb_pipecontrol (pipe)375- || usb_pipeisoc (pipe)376 || nents <= 0)377 return -EINVAL;378379- spin_lock_init (&io->lock);380 io->dev = dev;381 io->pipe = pipe;382 io->sg = sg;···381 dma = (dev->dev.dma_mask != NULL);382 if (dma)383 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),384- sg, nents);385 else386 io->entries = nents;387···390 return io->entries;391392 io->count = io->entries;393- io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);394 if (!io->urbs)395 goto nomem;396397 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;398- if (usb_pipein (pipe))399 urb_flags |= URB_SHORT_NOT_OK;400401 for (i = 0; i < io->entries; i++) {402- unsigned len;403404- io->urbs [i] = usb_alloc_urb (0, mem_flags);405- if (!io->urbs [i]) {406 io->entries = i;407 goto nomem;408 }409410- io->urbs [i]->dev = NULL;411- io->urbs [i]->pipe = pipe;412- io->urbs [i]->interval = period;413- io->urbs [i]->transfer_flags = urb_flags;414415- io->urbs [i]->complete = sg_complete;416- io->urbs [i]->context = io;417418 /*419 * Some systems need to revert to PIO when DMA is temporarily···432 * to prevent stale pointers and to help spot bugs.433 */434 if (dma) {435- io->urbs [i]->transfer_dma = sg_dma_address (sg + i);436- len = sg_dma_len (sg + i);437#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)438 io->urbs[i]->transfer_buffer = NULL;439#else···441#endif442 } else {443 /* hc may use _only_ transfer_buffer */444- io->urbs [i]->transfer_buffer = sg_virt(&sg[i]);445- len = sg [i].length;446 }447448 if (length) {449- len = min_t (unsigned, len, length);450 length -= len;451 if (length == 0)452 io->entries = i + 1;453 }454- io->urbs [i]->transfer_buffer_length = len;455 }456- io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;457458 /* transaction state */459 io->status = 0;460 io->bytes = 0;461- init_completion (&io->complete);462 return 0;463464nomem:465- sg_clean (io);466 return -ENOMEM;467}468EXPORT_SYMBOL_GPL(usb_sg_init);···506 * speed interrupt endpoints, which allow at most one packet per millisecond,507 * of at most 8 or 64 bytes (respectively).508 */509-void usb_sg_wait (struct usb_sg_request *io)510{511- int i, entries = io->entries;0512513 /* queue the urbs. */514- spin_lock_irq (&io->lock);515 i = 0;516 while (i < entries && !io->status) {517- int retval;518519- io->urbs [i]->dev = io->dev;520- retval = usb_submit_urb (io->urbs [i], GFP_ATOMIC);521522 /* after we submit, let completions or cancelations fire;523 * we handshake using io->status.524 */525- spin_unlock_irq (&io->lock);526 switch (retval) {527 /* maybe we retrying will recover */528- case -ENXIO: // hc didn't queue this one529 case -EAGAIN:530 case -ENOMEM:531 io->urbs[i]->dev = NULL;532 retval = 0;533- yield ();534 break;535536 /* no error? continue immediately.···542 */543 case 0:544 ++i;545- cpu_relax ();546 break;547548 /* fail any uncompleted urbs */549 default:550- io->urbs [i]->dev = NULL;551- io->urbs [i]->status = retval;552- dev_dbg (&io->dev->dev, "%s, submit --> %d\n",553 __FUNCTION__, retval);554- usb_sg_cancel (io);555 }556- spin_lock_irq (&io->lock);557 if (retval && (io->status == 0 || io->status == -ECONNRESET))558 io->status = retval;559 }560 io->count -= entries - i;561 if (io->count == 0)562- complete (&io->complete);563- spin_unlock_irq (&io->lock);564565 /* OK, yes, this could be packaged as non-blocking.566 * So could the submit loop above ... but it's easier to567 * solve neither problem than to solve both!568 */569- wait_for_completion (&io->complete);570571- sg_clean (io);572}573EXPORT_SYMBOL_GPL(usb_sg_wait);574···580 * It can also prevents one initialized by usb_sg_init() from starting,581 * so that call just frees resources allocated to the request.582 */583-void usb_sg_cancel (struct usb_sg_request *io)584{585- unsigned long flags;586587- spin_lock_irqsave (&io->lock, flags);588589 /* shut everything down, if it didn't already */590 if (!io->status) {591- int i;592593 io->status = -ECONNRESET;594- spin_unlock (&io->lock);595 for (i = 0; i < io->entries; i++) {596- int retval;597598 if (!io->urbs [i]->dev)599 continue;600- retval = usb_unlink_urb (io->urbs [i]);601 if (retval != -EINPROGRESS && retval != -EBUSY)602- dev_warn (&io->dev->dev, "%s, unlink --> %d\n",603 __FUNCTION__, retval);604 }605- spin_lock (&io->lock);606 }607- spin_unlock_irqrestore (&io->lock, flags);608}609EXPORT_SYMBOL_GPL(usb_sg_cancel);610···632 * Returns the number of bytes received on success, or else the status code633 * returned by the underlying usb_control_msg() call.634 */635-int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)0636{637 int i;638 int result;639-640- memset(buf,0,size); // Make sure we parse really received data641642 for (i = 0; i < 3; ++i) {643 /* retry on length 0 or error; some devices are flakey */···713}714715static int usb_string_sub(struct usb_device *dev, unsigned int langid,716- unsigned int index, unsigned char *buf)717{718 int rc;719···756 * @buf: where to put the string757 * @size: how big is "buf"?758 * Context: !in_interrupt ()759- * 760 * This converts the UTF-16LE encoded strings returned by devices, from761 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones762 * that are more usable in most kernel contexts. Note that all characters···792 if (!dev->have_langid) {793 err = usb_string_sub(dev, 0, 0, tbuf);794 if (err < 0) {795- dev_err (&dev->dev,796 "string descriptor 0 read error: %d\n",797 err);798 goto errout;799 } else if (err < 4) {800- dev_err (&dev->dev, "string descriptor 0 too short\n");801 err = -EINVAL;802 goto errout;803 } else {804 dev->have_langid = 1;805- dev->string_langid = tbuf[2] | (tbuf[3]<< 8);806- /* always use the first langid listed */807- dev_dbg (&dev->dev, "default language 0x%04x\n",808 dev->string_langid);809 }810 }811-812 err = usb_string_sub(dev, dev->string_langid, index, tbuf);813 if (err < 0)814 goto errout;···826 err = idx;827828 if (tbuf[1] != USB_DT_STRING)829- dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);00830831 errout:832 kfree(tbuf);···850 char *smallbuf = NULL;851 int len;852853- if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {854- if ((len = usb_string(udev, index, buf, 256)) > 0) {855- if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)000000856 return buf;857 memcpy(smallbuf, buf, len);858 }···897 return -ENOMEM;898899 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);900- if (ret >= 0) 901 memcpy(&dev->descriptor, desc, size);902 kfree(desc);903 return ret;···970{971 int result;972 int endp = usb_pipeendpoint(pipe);973-974- if (usb_pipein (pipe))975 endp |= USB_DIR_IN;976977 /* we don't care if it wasn't halted first. in fact some devices···1054 }1055}10561057-/*1058 * usb_disable_device - Disable all the endpoints for a USB device1059 * @dev: the device whose endpoints are being disabled1060 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.···1069 int i;10701071 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,1072- skip_ep0 ? "non-ep0" : "all");1073 for (i = skip_ep0; i < 16; ++i) {1074 usb_disable_endpoint(dev, i);1075 usb_disable_endpoint(dev, i + USB_DIR_IN);···1087 interface = dev->actconfig->interface[i];1088 if (!device_is_registered(&interface->dev))1089 continue;1090- dev_dbg (&dev->dev, "unregistering interface %s\n",1091 interface->dev.bus_id);1092 usb_remove_sysfs_intf_files(interface);1093- device_del (&interface->dev);1094 }10951096 /* Now that the interfaces are unbound, nobody should1097 * try to access them.1098 */1099 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {1100- put_device (&dev->actconfig->interface[i]->dev);1101 dev->actconfig->interface[i] = NULL;1102 }1103 dev->actconfig = NULL;···1106 }1107}11081109-1110-/*1111 * usb_enable_endpoint - Enable an endpoint for USB communications1112 * @dev: the device whose interface is being enabled1113 * @ep: the endpoint···1131 ep->enabled = 1;1132}11331134-/*1135 * usb_enable_interface - Enable all the endpoints for an interface1136 * @dev: the device whose interface is being enabled1137 * @intf: pointer to the interface descriptor···1187 struct usb_host_interface *alt;1188 int ret;1189 int manual = 0;0011901191 if (dev->state == USB_STATE_SUSPENDED)1192 return -EHOSTUNREACH;···1243 int i;12441245 for (i = 0; i < alt->desc.bNumEndpoints; i++) {1246- unsigned int epaddr =1247- alt->endpoint[i].desc.bEndpointAddress;1248- unsigned int pipe =1249- __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)1250- | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);12511252 usb_clear_halt(dev, pipe);1253 }···1376 return -ENOMEM;13771378 if (add_uevent_var(env,1379- "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",01380 le16_to_cpu(usb_dev->descriptor.idVendor),1381 le16_to_cpu(usb_dev->descriptor.idProduct),1382 le16_to_cpu(usb_dev->descriptor.bcdDevice),···1407};14081409static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,1410- struct usb_host_config *config,1411- u8 inum)1412{1413 struct usb_interface_assoc_descriptor *retval = NULL;1414 struct usb_interface_assoc_descriptor *intf_assoc;···14341435 return retval;1436}1437-14381439/*1440 * usb_set_configuration - Makes a particular device setting be current···1552 * getting rid of old interfaces means unbinding their drivers.1553 */1554 if (dev->state != USB_STATE_ADDRESS)1555- usb_disable_device (dev, 1); // Skip ep015561557- if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),1558- USB_REQ_SET_CONFIGURATION, 0, configuration, 0,1559- NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {1560-1561 /* All the old state is gone, so what else can we do?1562 * The device is probably useless now anyway.1563 */···1604 intf->dev.bus = &usb_bus_type;1605 intf->dev.type = &usb_if_device_type;1606 intf->dev.dma_mask = dev->dev.dma_mask;1607- device_initialize (&intf->dev);1608 mark_quiesced(intf);1609- sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",1610- dev->bus->busnum, dev->devpath,1611- configuration, alt->desc.bInterfaceNumber);1612 }1613 kfree(new_interfaces);1614···1624 for (i = 0; i < nintf; ++i) {1625 struct usb_interface *intf = cp->interface[i];16261627- dev_dbg (&dev->dev,1628 "adding %s (config #%d, interface %d)\n",1629 intf->dev.bus_id, configuration,1630 intf->cur_altsetting->desc.bInterfaceNumber);1631- ret = device_add (&intf->dev);1632 if (ret != 0) {1633 dev_err(&dev->dev, "device_add(%s) --> %d\n",1634 intf->dev.bus_id, ret);
···39 * own interruptible routines.40 */41static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)42+{43 struct api_context ctx;44 unsigned long expire;45 int retval;···74}7576/*-------------------------------------------------------------------*/77+/* returns status (negative) or length (positive) */78static int usb_internal_control_msg(struct usb_device *usb_dev,79+ unsigned int pipe,80 struct usb_ctrlrequest *cmd,81 void *data, int len, int timeout)82{···87 urb = usb_alloc_urb(0, GFP_NOIO);88 if (!urb)89 return -ENOMEM;90+91 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,92 len, usb_api_blocking_completion, NULL);93···99}100101/**102+ * usb_control_msg - Builds a control urb, sends it off and waits for completion103+ * @dev: pointer to the usb device to send the message to104+ * @pipe: endpoint "pipe" to send the message to105+ * @request: USB message request value106+ * @requesttype: USB message request type value107+ * @value: USB message value108+ * @index: USB message index value109+ * @data: pointer to the data to send110+ * @size: length in bytes of the data to send111+ * @timeout: time in msecs to wait for the message to complete before timing112+ * out (if 0 the wait is forever)0113 *114+ * Context: !in_interrupt ()000115 *116+ * This function sends a simple control message to a specified endpoint and117+ * waits for the message to complete, or timeout.118+ *119+ * If successful, it returns the number of bytes transferred, otherwise a120+ * negative error number.121+ *122+ * Don't use this function from within an interrupt context, like a bottom half123+ * handler. If you need an asynchronous message, or need to send a message124+ * from within interrupt context, use usb_submit_urb().125+ * If a thread in your driver uses this call, make sure your disconnect()126+ * method can wait for it to complete. Since you don't have a handle on the127+ * URB used, you can't cancel the request.128 */129+int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,130+ __u8 requesttype, __u16 value, __u16 index, void *data,131+ __u16 size, int timeout)132{133+ struct usb_ctrlrequest *dr;134 int ret;135+136+ dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);137 if (!dr)138 return -ENOMEM;139140+ dr->bRequestType = requesttype;141 dr->bRequest = request;142 dr->wValue = cpu_to_le16p(&value);143 dr->wIndex = cpu_to_le16p(&index);144 dr->wLength = cpu_to_le16p(&size);145146+ /* dbg("usb_control_msg"); */147148 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);149···155 * @pipe: endpoint "pipe" to send the message to156 * @data: pointer to the data to send157 * @len: length in bytes of the data to send158+ * @actual_length: pointer to a location to put the actual length transferred159+ * in bytes160 * @timeout: time in msecs to wait for the message to complete before161 * timing out (if 0 the wait is forever)162+ *163 * Context: !in_interrupt ()164 *165 * This function sends a simple interrupt message to a specified endpoint and···181EXPORT_SYMBOL_GPL(usb_interrupt_msg);182183/**184+ * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion185+ * @usb_dev: pointer to the usb device to send the message to186+ * @pipe: endpoint "pipe" to send the message to187+ * @data: pointer to the data to send188+ * @len: length in bytes of the data to send189+ * @actual_length: pointer to a location to put the actual length transferred190+ * in bytes191+ * @timeout: time in msecs to wait for the message to complete before192+ * timing out (if 0 the wait is forever)193 *194+ * Context: !in_interrupt ()00000195 *196+ * This function sends a simple bulk message to a specified endpoint197+ * and waits for the message to complete, or timeout.0000198 *199+ * If successful, it returns 0, otherwise a negative error number. The number200+ * of actual bytes transferred will be stored in the actual_length paramater.201+ *202+ * Don't use this function from within an interrupt context, like a bottom half203+ * handler. If you need an asynchronous message, or need to send a message204+ * from within interrupt context, use usb_submit_urb() If a thread in your205+ * driver uses this call, make sure your disconnect() method can wait for it to206+ * complete. Since you don't have a handle on the URB used, you can't cancel207+ * the request.208+ *209+ * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,210+ * users are forced to abuse this routine by using it to submit URBs for211+ * interrupt endpoints. We will take the liberty of creating an interrupt URB212+ * (with the default interval) if the target is an interrupt endpoint.213 */214+int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,215+ void *data, int len, int *actual_length, int timeout)216{217 struct urb *urb;218 struct usb_host_endpoint *ep;···242243/*-------------------------------------------------------------------*/244245+static void sg_clean(struct usb_sg_request *io)246{247 if (io->urbs) {248 while (io->entries--)249+ usb_free_urb(io->urbs [io->entries]);250+ kfree(io->urbs);251 io->urbs = NULL;252 }253 if (io->dev->dev.dma_mask != NULL)254+ usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe),255+ io->sg, io->nents);256 io->dev = NULL;257}258259+static void sg_complete(struct urb *urb)260{261+ struct usb_sg_request *io = urb->context;262 int status = urb->status;263264+ spin_lock(&io->lock);265266 /* In 2.5 we require hcds' endpoint queues not to progress after fault267 * reports, until the completion callback (this!) returns. That lets···277 && (io->status != -ECONNRESET278 || status != -ECONNRESET)279 && urb->actual_length) {280+ dev_err(io->dev->bus->controller,281 "dev %s ep%d%s scatterlist error %d/%d\n",282 io->dev->devpath,283 usb_endpoint_num(&urb->ep->desc),284 usb_urb_dir_in(urb) ? "in" : "out",285 status, io->status);286+ /* BUG (); */287 }288289 if (io->status == 0 && status && status != -ECONNRESET) {···295 * unlink pending urbs so they won't rx/tx bad data.296 * careful: unlink can sometimes be synchronous...297 */298+ spin_unlock(&io->lock);299 for (i = 0, found = 0; i < io->entries; i++) {300 if (!io->urbs [i] || !io->urbs [i]->dev)301 continue;302 if (found) {303+ retval = usb_unlink_urb(io->urbs [i]);304 if (retval != -EINPROGRESS &&305 retval != -ENODEV &&306 retval != -EBUSY)307+ dev_err(&io->dev->dev,308 "%s, unlink --> %d\n",309 __FUNCTION__, retval);310 } else if (urb == io->urbs [i])311 found = 1;312 }313+ spin_lock(&io->lock);314 }315 urb->dev = NULL;316···318 io->bytes += urb->actual_length;319 io->count--;320 if (!io->count)321+ complete(&io->complete);322323+ spin_unlock(&io->lock);324}325326···349 * The request may be canceled with usb_sg_cancel(), either before or after350 * usb_sg_wait() is called.351 */352+int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,353+ unsigned pipe, unsigned period, struct scatterlist *sg,354+ int nents, size_t length, gfp_t mem_flags)0000000355{356+ int i;357+ int urb_flags;358+ int dma;359360 if (!io || !dev || !sg361+ || usb_pipecontrol(pipe)362+ || usb_pipeisoc(pipe)363 || nents <= 0)364 return -EINVAL;365366+ spin_lock_init(&io->lock);367 io->dev = dev;368 io->pipe = pipe;369 io->sg = sg;···382 dma = (dev->dev.dma_mask != NULL);383 if (dma)384 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),385+ sg, nents);386 else387 io->entries = nents;388···391 return io->entries;392393 io->count = io->entries;394+ io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);395 if (!io->urbs)396 goto nomem;397398 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;399+ if (usb_pipein(pipe))400 urb_flags |= URB_SHORT_NOT_OK;401402 for (i = 0; i < io->entries; i++) {403+ unsigned len;404405+ io->urbs[i] = usb_alloc_urb(0, mem_flags);406+ if (!io->urbs[i]) {407 io->entries = i;408 goto nomem;409 }410411+ io->urbs[i]->dev = NULL;412+ io->urbs[i]->pipe = pipe;413+ io->urbs[i]->interval = period;414+ io->urbs[i]->transfer_flags = urb_flags;415416+ io->urbs[i]->complete = sg_complete;417+ io->urbs[i]->context = io;418419 /*420 * Some systems need to revert to PIO when DMA is temporarily···433 * to prevent stale pointers and to help spot bugs.434 */435 if (dma) {436+ io->urbs[i]->transfer_dma = sg_dma_address(sg + i);437+ len = sg_dma_len(sg + i);438#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)439 io->urbs[i]->transfer_buffer = NULL;440#else···442#endif443 } else {444 /* hc may use _only_ transfer_buffer */445+ io->urbs[i]->transfer_buffer = sg_virt(&sg[i]);446+ len = sg[i].length;447 }448449 if (length) {450+ len = min_t(unsigned, len, length);451 length -= len;452 if (length == 0)453 io->entries = i + 1;454 }455+ io->urbs[i]->transfer_buffer_length = len;456 }457+ io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;458459 /* transaction state */460 io->status = 0;461 io->bytes = 0;462+ init_completion(&io->complete);463 return 0;464465nomem:466+ sg_clean(io);467 return -ENOMEM;468}469EXPORT_SYMBOL_GPL(usb_sg_init);···507 * speed interrupt endpoints, which allow at most one packet per millisecond,508 * of at most 8 or 64 bytes (respectively).509 */510+void usb_sg_wait(struct usb_sg_request *io)511{512+ int i;513+ int entries = io->entries;514515 /* queue the urbs. */516+ spin_lock_irq(&io->lock);517 i = 0;518 while (i < entries && !io->status) {519+ int retval;520521+ io->urbs[i]->dev = io->dev;522+ retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);523524 /* after we submit, let completions or cancelations fire;525 * we handshake using io->status.526 */527+ spin_unlock_irq(&io->lock);528 switch (retval) {529 /* maybe we retrying will recover */530+ case -ENXIO: /* hc didn't queue this one */531 case -EAGAIN:532 case -ENOMEM:533 io->urbs[i]->dev = NULL;534 retval = 0;535+ yield();536 break;537538 /* no error? continue immediately.···542 */543 case 0:544 ++i;545+ cpu_relax();546 break;547548 /* fail any uncompleted urbs */549 default:550+ io->urbs[i]->dev = NULL;551+ io->urbs[i]->status = retval;552+ dev_dbg(&io->dev->dev, "%s, submit --> %d\n",553 __FUNCTION__, retval);554+ usb_sg_cancel(io);555 }556+ spin_lock_irq(&io->lock);557 if (retval && (io->status == 0 || io->status == -ECONNRESET))558 io->status = retval;559 }560 io->count -= entries - i;561 if (io->count == 0)562+ complete(&io->complete);563+ spin_unlock_irq(&io->lock);564565 /* OK, yes, this could be packaged as non-blocking.566 * So could the submit loop above ... but it's easier to567 * solve neither problem than to solve both!568 */569+ wait_for_completion(&io->complete);570571+ sg_clean(io);572}573EXPORT_SYMBOL_GPL(usb_sg_wait);574···580 * It can also prevents one initialized by usb_sg_init() from starting,581 * so that call just frees resources allocated to the request.582 */583+void usb_sg_cancel(struct usb_sg_request *io)584{585+ unsigned long flags;586587+ spin_lock_irqsave(&io->lock, flags);588589 /* shut everything down, if it didn't already */590 if (!io->status) {591+ int i;592593 io->status = -ECONNRESET;594+ spin_unlock(&io->lock);595 for (i = 0; i < io->entries; i++) {596+ int retval;597598 if (!io->urbs [i]->dev)599 continue;600+ retval = usb_unlink_urb(io->urbs [i]);601 if (retval != -EINPROGRESS && retval != -EBUSY)602+ dev_warn(&io->dev->dev, "%s, unlink --> %d\n",603 __FUNCTION__, retval);604 }605+ spin_lock(&io->lock);606 }607+ spin_unlock_irqrestore(&io->lock, flags);608}609EXPORT_SYMBOL_GPL(usb_sg_cancel);610···632 * Returns the number of bytes received on success, or else the status code633 * returned by the underlying usb_control_msg() call.634 */635+int usb_get_descriptor(struct usb_device *dev, unsigned char type,636+ unsigned char index, void *buf, int size)637{638 int i;639 int result;640+641+ memset(buf, 0, size); /* Make sure we parse really received data */642643 for (i = 0; i < 3; ++i) {644 /* retry on length 0 or error; some devices are flakey */···712}713714static int usb_string_sub(struct usb_device *dev, unsigned int langid,715+ unsigned int index, unsigned char *buf)716{717 int rc;718···755 * @buf: where to put the string756 * @size: how big is "buf"?757 * Context: !in_interrupt ()758+ *759 * This converts the UTF-16LE encoded strings returned by devices, from760 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones761 * that are more usable in most kernel contexts. Note that all characters···791 if (!dev->have_langid) {792 err = usb_string_sub(dev, 0, 0, tbuf);793 if (err < 0) {794+ dev_err(&dev->dev,795 "string descriptor 0 read error: %d\n",796 err);797 goto errout;798 } else if (err < 4) {799+ dev_err(&dev->dev, "string descriptor 0 too short\n");800 err = -EINVAL;801 goto errout;802 } else {803 dev->have_langid = 1;804+ dev->string_langid = tbuf[2] | (tbuf[3] << 8);805+ /* always use the first langid listed */806+ dev_dbg(&dev->dev, "default language 0x%04x\n",807 dev->string_langid);808 }809 }810+811 err = usb_string_sub(dev, dev->string_langid, index, tbuf);812 if (err < 0)813 goto errout;···825 err = idx;826827 if (tbuf[1] != USB_DT_STRING)828+ dev_dbg(&dev->dev,829+ "wrong descriptor type %02x for string %d (\"%s\")\n",830+ tbuf[1], index, buf);831832 errout:833 kfree(tbuf);···847 char *smallbuf = NULL;848 int len;849850+ if (index <= 0)851+ return NULL;852+853+ buf = kmalloc(256, GFP_KERNEL);854+ if (buf) {855+ len = usb_string(udev, index, buf, 256);856+ if (len > 0) {857+ smallbuf = kmalloc(++len, GFP_KERNEL);858+ if (!smallbuf)859 return buf;860 memcpy(smallbuf, buf, len);861 }···888 return -ENOMEM;889890 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);891+ if (ret >= 0)892 memcpy(&dev->descriptor, desc, size);893 kfree(desc);894 return ret;···961{962 int result;963 int endp = usb_pipeendpoint(pipe);964+965+ if (usb_pipein(pipe))966 endp |= USB_DIR_IN;967968 /* we don't care if it wasn't halted first. in fact some devices···1045 }1046}10471048+/**1049 * usb_disable_device - Disable all the endpoints for a USB device1050 * @dev: the device whose endpoints are being disabled1051 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.···1060 int i;10611062 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,1063+ skip_ep0 ? "non-ep0" : "all");1064 for (i = skip_ep0; i < 16; ++i) {1065 usb_disable_endpoint(dev, i);1066 usb_disable_endpoint(dev, i + USB_DIR_IN);···1078 interface = dev->actconfig->interface[i];1079 if (!device_is_registered(&interface->dev))1080 continue;1081+ dev_dbg(&dev->dev, "unregistering interface %s\n",1082 interface->dev.bus_id);1083 usb_remove_sysfs_intf_files(interface);1084+ device_del(&interface->dev);1085 }10861087 /* Now that the interfaces are unbound, nobody should1088 * try to access them.1089 */1090 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {1091+ put_device(&dev->actconfig->interface[i]->dev);1092 dev->actconfig->interface[i] = NULL;1093 }1094 dev->actconfig = NULL;···1097 }1098}10991100+/**01101 * usb_enable_endpoint - Enable an endpoint for USB communications1102 * @dev: the device whose interface is being enabled1103 * @ep: the endpoint···1123 ep->enabled = 1;1124}11251126+/**1127 * usb_enable_interface - Enable all the endpoints for an interface1128 * @dev: the device whose interface is being enabled1129 * @intf: pointer to the interface descriptor···1179 struct usb_host_interface *alt;1180 int ret;1181 int manual = 0;1182+ unsigned int epaddr;1183+ unsigned int pipe;11841185 if (dev->state == USB_STATE_SUSPENDED)1186 return -EHOSTUNREACH;···1233 int i;12341235 for (i = 0; i < alt->desc.bNumEndpoints; i++) {1236+ epaddr = alt->endpoint[i].desc.bEndpointAddress;1237+ pipe = __create_pipe(dev,1238+ USB_ENDPOINT_NUMBER_MASK & epaddr) |1239+ (usb_endpoint_out(epaddr) ?1240+ USB_DIR_OUT : USB_DIR_IN);12411242 usb_clear_halt(dev, pipe);1243 }···1366 return -ENOMEM;13671368 if (add_uevent_var(env,1369+ "MODALIAS=usb:"1370+ "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",1371 le16_to_cpu(usb_dev->descriptor.idVendor),1372 le16_to_cpu(usb_dev->descriptor.idProduct),1373 le16_to_cpu(usb_dev->descriptor.bcdDevice),···1396};13971398static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,1399+ struct usb_host_config *config,1400+ u8 inum)1401{1402 struct usb_interface_assoc_descriptor *retval = NULL;1403 struct usb_interface_assoc_descriptor *intf_assoc;···14231424 return retval;1425}014261427/*1428 * usb_set_configuration - Makes a particular device setting be current···1542 * getting rid of old interfaces means unbinding their drivers.1543 */1544 if (dev->state != USB_STATE_ADDRESS)1545+ usb_disable_device(dev, 1); /* Skip ep0 */15461547+ ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),1548+ USB_REQ_SET_CONFIGURATION, 0, configuration, 0,1549+ NULL, 0, USB_CTRL_SET_TIMEOUT);1550+ if (ret < 0) {1551 /* All the old state is gone, so what else can we do?1552 * The device is probably useless now anyway.1553 */···1594 intf->dev.bus = &usb_bus_type;1595 intf->dev.type = &usb_if_device_type;1596 intf->dev.dma_mask = dev->dev.dma_mask;1597+ device_initialize(&intf->dev);1598 mark_quiesced(intf);1599+ sprintf(&intf->dev.bus_id[0], "%d-%s:%d.%d",1600+ dev->bus->busnum, dev->devpath,1601+ configuration, alt->desc.bInterfaceNumber);1602 }1603 kfree(new_interfaces);1604···1614 for (i = 0; i < nintf; ++i) {1615 struct usb_interface *intf = cp->interface[i];16161617+ dev_dbg(&dev->dev,1618 "adding %s (config #%d, interface %d)\n",1619 intf->dev.bus_id, configuration,1620 intf->cur_altsetting->desc.bInterfaceNumber);1621+ ret = device_add(&intf->dev);1622 if (ret != 0) {1623 dev_err(&dev->dev, "device_add(%s) --> %d\n",1624 intf->dev.bus_id, ret);