PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/core/message.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 1950 lines | 1063 code | 216 blank | 671 comment | 241 complexity | cffa602a090f7b8835a310982c7ab39d MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * message.c - synchronous message handling
  3. */
  4. #include <linux/pci.h> /* for scatterlist macros */
  5. #include <linux/usb.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <linux/timer.h>
  11. #include <linux/ctype.h>
  12. #include <linux/nls.h>
  13. #include <linux/device.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/usb/quirks.h>
  16. #include <linux/usb/hcd.h> /* for usbcore internals */
  17. #include <asm/byteorder.h>
  18. #include "usb.h"
  19. static void cancel_async_set_config(struct usb_device *udev);
  20. struct api_context {
  21. struct completion done;
  22. int status;
  23. };
  24. static void usb_api_blocking_completion(struct urb *urb)
  25. {
  26. struct api_context *ctx = urb->context;
  27. ctx->status = urb->status;
  28. complete(&ctx->done);
  29. }
  30. /*
  31. * Starts urb and waits for completion or timeout. Note that this call
  32. * is NOT interruptible. Many device driver i/o requests should be
  33. * interruptible and therefore these drivers should implement their
  34. * own interruptible routines.
  35. */
  36. static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
  37. {
  38. struct api_context ctx;
  39. unsigned long expire;
  40. int retval;
  41. init_completion(&ctx.done);
  42. urb->context = &ctx;
  43. urb->actual_length = 0;
  44. retval = usb_submit_urb(urb, GFP_NOIO);
  45. if (unlikely(retval))
  46. goto out;
  47. expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
  48. if (!wait_for_completion_timeout(&ctx.done, expire)) {
  49. usb_kill_urb(urb);
  50. retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
  51. dev_dbg(&urb->dev->dev,
  52. "%s timed out on ep%d%s len=%u/%u\n",
  53. current->comm,
  54. usb_endpoint_num(&urb->ep->desc),
  55. usb_urb_dir_in(urb) ? "in" : "out",
  56. urb->actual_length,
  57. urb->transfer_buffer_length);
  58. } else
  59. retval = ctx.status;
  60. out:
  61. if (actual_length)
  62. *actual_length = urb->actual_length;
  63. usb_free_urb(urb);
  64. return retval;
  65. }
  66. /*-------------------------------------------------------------------*/
  67. /* returns status (negative) or length (positive) */
  68. static int usb_internal_control_msg(struct usb_device *usb_dev,
  69. unsigned int pipe,
  70. struct usb_ctrlrequest *cmd,
  71. void *data, int len, int timeout)
  72. {
  73. struct urb *urb;
  74. int retv;
  75. int length;
  76. urb = usb_alloc_urb(0, GFP_NOIO);
  77. if (!urb)
  78. return -ENOMEM;
  79. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
  80. len, usb_api_blocking_completion, NULL);
  81. retv = usb_start_wait_urb(urb, timeout, &length);
  82. if (retv < 0)
  83. return retv;
  84. else
  85. return length;
  86. }
  87. /**
  88. * usb_control_msg - Builds a control urb, sends it off and waits for completion
  89. * @dev: pointer to the usb device to send the message to
  90. * @pipe: endpoint "pipe" to send the message to
  91. * @request: USB message request value
  92. * @requesttype: USB message request type value
  93. * @value: USB message value
  94. * @index: USB message index value
  95. * @data: pointer to the data to send
  96. * @size: length in bytes of the data to send
  97. * @timeout: time in msecs to wait for the message to complete before timing
  98. * out (if 0 the wait is forever)
  99. *
  100. * Context: !in_interrupt ()
  101. *
  102. * This function sends a simple control message to a specified endpoint and
  103. * waits for the message to complete, or timeout.
  104. *
  105. * If successful, it returns the number of bytes transferred, otherwise a
  106. * negative error number.
  107. *
  108. * Don't use this function from within an interrupt context, like a bottom half
  109. * handler. If you need an asynchronous message, or need to send a message
  110. * from within interrupt context, use usb_submit_urb().
  111. * If a thread in your driver uses this call, make sure your disconnect()
  112. * method can wait for it to complete. Since you don't have a handle on the
  113. * URB used, you can't cancel the request.
  114. */
  115. int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  116. __u8 requesttype, __u16 value, __u16 index, void *data,
  117. __u16 size, int timeout)
  118. {
  119. struct usb_ctrlrequest *dr;
  120. int ret;
  121. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
  122. if (!dr)
  123. return -ENOMEM;
  124. dr->bRequestType = requesttype;
  125. dr->bRequest = request;
  126. dr->wValue = cpu_to_le16(value);
  127. dr->wIndex = cpu_to_le16(index);
  128. dr->wLength = cpu_to_le16(size);
  129. /* dbg("usb_control_msg"); */
  130. ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
  131. kfree(dr);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(usb_control_msg);
  135. /**
  136. * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
  137. * @usb_dev: pointer to the usb device to send the message to
  138. * @pipe: endpoint "pipe" to send the message to
  139. * @data: pointer to the data to send
  140. * @len: length in bytes of the data to send
  141. * @actual_length: pointer to a location to put the actual length transferred
  142. * in bytes
  143. * @timeout: time in msecs to wait for the message to complete before
  144. * timing out (if 0 the wait is forever)
  145. *
  146. * Context: !in_interrupt ()
  147. *
  148. * This function sends a simple interrupt message to a specified endpoint and
  149. * waits for the message to complete, or timeout.
  150. *
  151. * If successful, it returns 0, otherwise a negative error number. The number
  152. * of actual bytes transferred will be stored in the actual_length paramater.
  153. *
  154. * Don't use this function from within an interrupt context, like a bottom half
  155. * handler. If you need an asynchronous message, or need to send a message
  156. * from within interrupt context, use usb_submit_urb() If a thread in your
  157. * driver uses this call, make sure your disconnect() method can wait for it to
  158. * complete. Since you don't have a handle on the URB used, you can't cancel
  159. * the request.
  160. */
  161. int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
  162. void *data, int len, int *actual_length, int timeout)
  163. {
  164. return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
  165. }
  166. EXPORT_SYMBOL_GPL(usb_interrupt_msg);
  167. /**
  168. * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
  169. * @usb_dev: pointer to the usb device to send the message to
  170. * @pipe: endpoint "pipe" to send the message to
  171. * @data: pointer to the data to send
  172. * @len: length in bytes of the data to send
  173. * @actual_length: pointer to a location to put the actual length transferred
  174. * in bytes
  175. * @timeout: time in msecs to wait for the message to complete before
  176. * timing out (if 0 the wait is forever)
  177. *
  178. * Context: !in_interrupt ()
  179. *
  180. * This function sends a simple bulk message to a specified endpoint
  181. * and waits for the message to complete, or timeout.
  182. *
  183. * If successful, it returns 0, otherwise a negative error number. The number
  184. * of actual bytes transferred will be stored in the actual_length paramater.
  185. *
  186. * Don't use this function from within an interrupt context, like a bottom half
  187. * handler. If you need an asynchronous message, or need to send a message
  188. * from within interrupt context, use usb_submit_urb() If a thread in your
  189. * driver uses this call, make sure your disconnect() method can wait for it to
  190. * complete. Since you don't have a handle on the URB used, you can't cancel
  191. * the request.
  192. *
  193. * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
  194. * users are forced to abuse this routine by using it to submit URBs for
  195. * interrupt endpoints. We will take the liberty of creating an interrupt URB
  196. * (with the default interval) if the target is an interrupt endpoint.
  197. */
  198. int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
  199. void *data, int len, int *actual_length, int timeout)
  200. {
  201. struct urb *urb;
  202. struct usb_host_endpoint *ep;
  203. ep = usb_pipe_endpoint(usb_dev, pipe);
  204. if (!ep || len < 0)
  205. return -EINVAL;
  206. urb = usb_alloc_urb(0, GFP_KERNEL);
  207. if (!urb)
  208. return -ENOMEM;
  209. if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  210. USB_ENDPOINT_XFER_INT) {
  211. pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
  212. usb_fill_int_urb(urb, usb_dev, pipe, data, len,
  213. usb_api_blocking_completion, NULL,
  214. ep->desc.bInterval);
  215. } else
  216. usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
  217. usb_api_blocking_completion, NULL);
  218. return usb_start_wait_urb(urb, timeout, actual_length);
  219. }
  220. EXPORT_SYMBOL_GPL(usb_bulk_msg);
  221. /*-------------------------------------------------------------------*/
  222. static void sg_clean(struct usb_sg_request *io)
  223. {
  224. if (io->urbs) {
  225. while (io->entries--)
  226. usb_free_urb(io->urbs [io->entries]);
  227. kfree(io->urbs);
  228. io->urbs = NULL;
  229. }
  230. io->dev = NULL;
  231. }
  232. static void sg_complete(struct urb *urb)
  233. {
  234. struct usb_sg_request *io = urb->context;
  235. int status = urb->status;
  236. spin_lock(&io->lock);
  237. /* In 2.5 we require hcds' endpoint queues not to progress after fault
  238. * reports, until the completion callback (this!) returns. That lets
  239. * device driver code (like this routine) unlink queued urbs first,
  240. * if it needs to, since the HC won't work on them at all. So it's
  241. * not possible for page N+1 to overwrite page N, and so on.
  242. *
  243. * That's only for "hard" faults; "soft" faults (unlinks) sometimes
  244. * complete before the HCD can get requests away from hardware,
  245. * though never during cleanup after a hard fault.
  246. */
  247. if (io->status
  248. && (io->status != -ECONNRESET
  249. || status != -ECONNRESET)
  250. && urb->actual_length) {
  251. dev_err(io->dev->bus->controller,
  252. "dev %s ep%d%s scatterlist error %d/%d\n",
  253. io->dev->devpath,
  254. usb_endpoint_num(&urb->ep->desc),
  255. usb_urb_dir_in(urb) ? "in" : "out",
  256. status, io->status);
  257. /* BUG (); */
  258. }
  259. if (io->status == 0 && status && status != -ECONNRESET) {
  260. int i, found, retval;
  261. io->status = status;
  262. /* the previous urbs, and this one, completed already.
  263. * unlink pending urbs so they won't rx/tx bad data.
  264. * careful: unlink can sometimes be synchronous...
  265. */
  266. spin_unlock(&io->lock);
  267. for (i = 0, found = 0; i < io->entries; i++) {
  268. if (!io->urbs [i] || !io->urbs [i]->dev)
  269. continue;
  270. if (found) {
  271. retval = usb_unlink_urb(io->urbs [i]);
  272. if (retval != -EINPROGRESS &&
  273. retval != -ENODEV &&
  274. retval != -EBUSY)
  275. dev_err(&io->dev->dev,
  276. "%s, unlink --> %d\n",
  277. __func__, retval);
  278. } else if (urb == io->urbs [i])
  279. found = 1;
  280. }
  281. spin_lock(&io->lock);
  282. }
  283. urb->dev = NULL;
  284. /* on the last completion, signal usb_sg_wait() */
  285. io->bytes += urb->actual_length;
  286. io->count--;
  287. if (!io->count)
  288. complete(&io->complete);
  289. spin_unlock(&io->lock);
  290. }
  291. /**
  292. * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
  293. * @io: request block being initialized. until usb_sg_wait() returns,
  294. * treat this as a pointer to an opaque block of memory,
  295. * @dev: the usb device that will send or receive the data
  296. * @pipe: endpoint "pipe" used to transfer the data
  297. * @period: polling rate for interrupt endpoints, in frames or
  298. * (for high speed endpoints) microframes; ignored for bulk
  299. * @sg: scatterlist entries
  300. * @nents: how many entries in the scatterlist
  301. * @length: how many bytes to send from the scatterlist, or zero to
  302. * send every byte identified in the list.
  303. * @mem_flags: SLAB_* flags affecting memory allocations in this call
  304. *
  305. * Returns zero for success, else a negative errno value. This initializes a
  306. * scatter/gather request, allocating resources such as I/O mappings and urb
  307. * memory (except maybe memory used by USB controller drivers).
  308. *
  309. * The request must be issued using usb_sg_wait(), which waits for the I/O to
  310. * complete (or to be canceled) and then cleans up all resources allocated by
  311. * usb_sg_init().
  312. *
  313. * The request may be canceled with usb_sg_cancel(), either before or after
  314. * usb_sg_wait() is called.
  315. */
  316. int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
  317. unsigned pipe, unsigned period, struct scatterlist *sg,
  318. int nents, size_t length, gfp_t mem_flags)
  319. {
  320. int i;
  321. int urb_flags;
  322. int use_sg;
  323. if (!io || !dev || !sg
  324. || usb_pipecontrol(pipe)
  325. || usb_pipeisoc(pipe)
  326. || nents <= 0)
  327. return -EINVAL;
  328. spin_lock_init(&io->lock);
  329. io->dev = dev;
  330. io->pipe = pipe;
  331. if (dev->bus->sg_tablesize > 0) {
  332. use_sg = true;
  333. io->entries = 1;
  334. } else {
  335. use_sg = false;
  336. io->entries = nents;
  337. }
  338. /* initialize all the urbs we'll use */
  339. io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
  340. if (!io->urbs)
  341. goto nomem;
  342. urb_flags = URB_NO_INTERRUPT;
  343. if (usb_pipein(pipe))
  344. urb_flags |= URB_SHORT_NOT_OK;
  345. for_each_sg(sg, sg, io->entries, i) {
  346. struct urb *urb;
  347. unsigned len;
  348. urb = usb_alloc_urb(0, mem_flags);
  349. if (!urb) {
  350. io->entries = i;
  351. goto nomem;
  352. }
  353. io->urbs[i] = urb;
  354. urb->dev = NULL;
  355. urb->pipe = pipe;
  356. urb->interval = period;
  357. urb->transfer_flags = urb_flags;
  358. urb->complete = sg_complete;
  359. urb->context = io;
  360. urb->sg = sg;
  361. if (use_sg) {
  362. /* There is no single transfer buffer */
  363. urb->transfer_buffer = NULL;
  364. urb->num_sgs = nents;
  365. /* A length of zero means transfer the whole sg list */
  366. len = length;
  367. if (len == 0) {
  368. struct scatterlist *sg2;
  369. int j;
  370. for_each_sg(sg, sg2, nents, j)
  371. len += sg2->length;
  372. }
  373. } else {
  374. /*
  375. * Some systems can't use DMA; they use PIO instead.
  376. * For their sakes, transfer_buffer is set whenever
  377. * possible.
  378. */
  379. if (!PageHighMem(sg_page(sg)))
  380. urb->transfer_buffer = sg_virt(sg);
  381. else
  382. urb->transfer_buffer = NULL;
  383. len = sg->length;
  384. if (length) {
  385. len = min_t(unsigned, len, length);
  386. length -= len;
  387. if (length == 0)
  388. io->entries = i + 1;
  389. }
  390. }
  391. urb->transfer_buffer_length = len;
  392. }
  393. io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
  394. /* transaction state */
  395. io->count = io->entries;
  396. io->status = 0;
  397. io->bytes = 0;
  398. init_completion(&io->complete);
  399. return 0;
  400. nomem:
  401. sg_clean(io);
  402. return -ENOMEM;
  403. }
  404. EXPORT_SYMBOL_GPL(usb_sg_init);
  405. /**
  406. * usb_sg_wait - synchronously execute scatter/gather request
  407. * @io: request block handle, as initialized with usb_sg_init().
  408. * some fields become accessible when this call returns.
  409. * Context: !in_interrupt ()
  410. *
  411. * This function blocks until the specified I/O operation completes. It
  412. * leverages the grouping of the related I/O requests to get good transfer
  413. * rates, by queueing the requests. At higher speeds, such queuing can
  414. * significantly improve USB throughput.
  415. *
  416. * There are three kinds of completion for this function.
  417. * (1) success, where io->status is zero. The number of io->bytes
  418. * transferred is as requested.
  419. * (2) error, where io->status is a negative errno value. The number
  420. * of io->bytes transferred before the error is usually less
  421. * than requested, and can be nonzero.
  422. * (3) cancellation, a type of error with status -ECONNRESET that
  423. * is initiated by usb_sg_cancel().
  424. *
  425. * When this function returns, all memory allocated through usb_sg_init() or
  426. * this call will have been freed. The request block parameter may still be
  427. * passed to usb_sg_cancel(), or it may be freed. It could also be
  428. * reinitialized and then reused.
  429. *
  430. * Data Transfer Rates:
  431. *
  432. * Bulk transfers are valid for full or high speed endpoints.
  433. * The best full speed data rate is 19 packets of 64 bytes each
  434. * per frame, or 1216 bytes per millisecond.
  435. * The best high speed data rate is 13 packets of 512 bytes each
  436. * per microframe, or 52 KBytes per millisecond.
  437. *
  438. * The reason to use interrupt transfers through this API would most likely
  439. * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
  440. * could be transferred. That capability is less useful for low or full
  441. * speed interrupt endpoints, which allow at most one packet per millisecond,
  442. * of at most 8 or 64 bytes (respectively).
  443. *
  444. * It is not necessary to call this function to reserve bandwidth for devices
  445. * under an xHCI host controller, as the bandwidth is reserved when the
  446. * configuration or interface alt setting is selected.
  447. */
  448. void usb_sg_wait(struct usb_sg_request *io)
  449. {
  450. int i;
  451. int entries = io->entries;
  452. /* queue the urbs. */
  453. spin_lock_irq(&io->lock);
  454. i = 0;
  455. while (i < entries && !io->status) {
  456. int retval;
  457. io->urbs[i]->dev = io->dev;
  458. retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
  459. /* after we submit, let completions or cancelations fire;
  460. * we handshake using io->status.
  461. */
  462. spin_unlock_irq(&io->lock);
  463. switch (retval) {
  464. /* maybe we retrying will recover */
  465. case -ENXIO: /* hc didn't queue this one */
  466. case -EAGAIN:
  467. case -ENOMEM:
  468. io->urbs[i]->dev = NULL;
  469. retval = 0;
  470. yield();
  471. break;
  472. /* no error? continue immediately.
  473. *
  474. * NOTE: to work better with UHCI (4K I/O buffer may
  475. * need 3K of TDs) it may be good to limit how many
  476. * URBs are queued at once; N milliseconds?
  477. */
  478. case 0:
  479. ++i;
  480. cpu_relax();
  481. break;
  482. /* fail any uncompleted urbs */
  483. default:
  484. io->urbs[i]->dev = NULL;
  485. io->urbs[i]->status = retval;
  486. dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
  487. __func__, retval);
  488. usb_sg_cancel(io);
  489. }
  490. spin_lock_irq(&io->lock);
  491. if (retval && (io->status == 0 || io->status == -ECONNRESET))
  492. io->status = retval;
  493. }
  494. io->count -= entries - i;
  495. if (io->count == 0)
  496. complete(&io->complete);
  497. spin_unlock_irq(&io->lock);
  498. /* OK, yes, this could be packaged as non-blocking.
  499. * So could the submit loop above ... but it's easier to
  500. * solve neither problem than to solve both!
  501. */
  502. wait_for_completion(&io->complete);
  503. sg_clean(io);
  504. }
  505. EXPORT_SYMBOL_GPL(usb_sg_wait);
  506. /**
  507. * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
  508. * @io: request block, initialized with usb_sg_init()
  509. *
  510. * This stops a request after it has been started by usb_sg_wait().
  511. * It can also prevents one initialized by usb_sg_init() from starting,
  512. * so that call just frees resources allocated to the request.
  513. */
  514. void usb_sg_cancel(struct usb_sg_request *io)
  515. {
  516. unsigned long flags;
  517. spin_lock_irqsave(&io->lock, flags);
  518. /* shut everything down, if it didn't already */
  519. if (!io->status) {
  520. int i;
  521. io->status = -ECONNRESET;
  522. spin_unlock(&io->lock);
  523. for (i = 0; i < io->entries; i++) {
  524. int retval;
  525. if (!io->urbs [i]->dev)
  526. continue;
  527. retval = usb_unlink_urb(io->urbs [i]);
  528. if (retval != -EINPROGRESS && retval != -EBUSY)
  529. dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
  530. __func__, retval);
  531. }
  532. spin_lock(&io->lock);
  533. }
  534. spin_unlock_irqrestore(&io->lock, flags);
  535. }
  536. EXPORT_SYMBOL_GPL(usb_sg_cancel);
  537. /*-------------------------------------------------------------------*/
  538. /**
  539. * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
  540. * @dev: the device whose descriptor is being retrieved
  541. * @type: the descriptor type (USB_DT_*)
  542. * @index: the number of the descriptor
  543. * @buf: where to put the descriptor
  544. * @size: how big is "buf"?
  545. * Context: !in_interrupt ()
  546. *
  547. * Gets a USB descriptor. Convenience functions exist to simplify
  548. * getting some types of descriptors. Use
  549. * usb_get_string() or usb_string() for USB_DT_STRING.
  550. * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
  551. * are part of the device structure.
  552. * In addition to a number of USB-standard descriptors, some
  553. * devices also use class-specific or vendor-specific descriptors.
  554. *
  555. * This call is synchronous, and may not be used in an interrupt context.
  556. *
  557. * Returns the number of bytes received on success, or else the status code
  558. * returned by the underlying usb_control_msg() call.
  559. */
  560. int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  561. unsigned char index, void *buf, int size)
  562. {
  563. int i;
  564. int result;
  565. memset(buf, 0, size); /* Make sure we parse really received data */
  566. for (i = 0; i < 3; ++i) {
  567. /* retry on length 0 or error; some devices are flakey */
  568. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  569. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  570. (type << 8) + index, 0, buf, size,
  571. USB_CTRL_GET_TIMEOUT);
  572. if (result <= 0 && result != -ETIMEDOUT)
  573. continue;
  574. if (result > 1 && ((u8 *)buf)[1] != type) {
  575. result = -ENODATA;
  576. continue;
  577. }
  578. break;
  579. }
  580. return result;
  581. }
  582. EXPORT_SYMBOL_GPL(usb_get_descriptor);
  583. /**
  584. * usb_get_string - gets a string descriptor
  585. * @dev: the device whose string descriptor is being retrieved
  586. * @langid: code for language chosen (from string descriptor zero)
  587. * @index: the number of the descriptor
  588. * @buf: where to put the string
  589. * @size: how big is "buf"?
  590. * Context: !in_interrupt ()
  591. *
  592. * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
  593. * in little-endian byte order).
  594. * The usb_string() function will often be a convenient way to turn
  595. * these strings into kernel-printable form.
  596. *
  597. * Strings may be referenced in device, configuration, interface, or other
  598. * descriptors, and could also be used in vendor-specific ways.
  599. *
  600. * This call is synchronous, and may not be used in an interrupt context.
  601. *
  602. * Returns the number of bytes received on success, or else the status code
  603. * returned by the underlying usb_control_msg() call.
  604. */
  605. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  606. unsigned char index, void *buf, int size)
  607. {
  608. int i;
  609. int result;
  610. for (i = 0; i < 3; ++i) {
  611. /* retry on length 0 or stall; some devices are flakey */
  612. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  613. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  614. (USB_DT_STRING << 8) + index, langid, buf, size,
  615. USB_CTRL_GET_TIMEOUT);
  616. if (result == 0 || result == -EPIPE)
  617. continue;
  618. if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
  619. result = -ENODATA;
  620. continue;
  621. }
  622. break;
  623. }
  624. return result;
  625. }
  626. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  627. {
  628. int newlength, oldlength = *length;
  629. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  630. if (!isprint(buf[newlength]) || buf[newlength + 1])
  631. break;
  632. if (newlength > 2) {
  633. buf[0] = newlength;
  634. *length = newlength;
  635. }
  636. }
  637. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  638. unsigned int index, unsigned char *buf)
  639. {
  640. int rc;
  641. /* Try to read the string descriptor by asking for the maximum
  642. * possible number of bytes */
  643. if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
  644. rc = -EIO;
  645. else
  646. rc = usb_get_string(dev, langid, index, buf, 255);
  647. /* If that failed try to read the descriptor length, then
  648. * ask for just that many bytes */
  649. if (rc < 2) {
  650. rc = usb_get_string(dev, langid, index, buf, 2);
  651. if (rc == 2)
  652. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  653. }
  654. if (rc >= 2) {
  655. if (!buf[0] && !buf[1])
  656. usb_try_string_workarounds(buf, &rc);
  657. /* There might be extra junk at the end of the descriptor */
  658. if (buf[0] < rc)
  659. rc = buf[0];
  660. rc = rc - (rc & 1); /* force a multiple of two */
  661. }
  662. if (rc < 2)
  663. rc = (rc < 0 ? rc : -EINVAL);
  664. return rc;
  665. }
  666. static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
  667. {
  668. int err;
  669. if (dev->have_langid)
  670. return 0;
  671. if (dev->string_langid < 0)
  672. return -EPIPE;
  673. err = usb_string_sub(dev, 0, 0, tbuf);
  674. /* If the string was reported but is malformed, default to english
  675. * (0x0409) */
  676. if (err == -ENODATA || (err > 0 && err < 4)) {
  677. dev->string_langid = 0x0409;
  678. dev->have_langid = 1;
  679. dev_err(&dev->dev,
  680. "string descriptor 0 malformed (err = %d), "
  681. "defaulting to 0x%04x\n",
  682. err, dev->string_langid);
  683. return 0;
  684. }
  685. /* In case of all other errors, we assume the device is not able to
  686. * deal with strings at all. Set string_langid to -1 in order to
  687. * prevent any string to be retrieved from the device */
  688. if (err < 0) {
  689. dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
  690. err);
  691. dev->string_langid = -1;
  692. return -EPIPE;
  693. }
  694. /* always use the first langid listed */
  695. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  696. dev->have_langid = 1;
  697. dev_dbg(&dev->dev, "default language 0x%04x\n",
  698. dev->string_langid);
  699. return 0;
  700. }
  701. /**
  702. * usb_string - returns UTF-8 version of a string descriptor
  703. * @dev: the device whose string descriptor is being retrieved
  704. * @index: the number of the descriptor
  705. * @buf: where to put the string
  706. * @size: how big is "buf"?
  707. * Context: !in_interrupt ()
  708. *
  709. * This converts the UTF-16LE encoded strings returned by devices, from
  710. * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
  711. * that are more usable in most kernel contexts. Note that this function
  712. * chooses strings in the first language supported by the device.
  713. *
  714. * This call is synchronous, and may not be used in an interrupt context.
  715. *
  716. * Returns length of the string (>= 0) or usb_control_msg status (< 0).
  717. */
  718. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  719. {
  720. unsigned char *tbuf;
  721. int err;
  722. if (dev->state == USB_STATE_SUSPENDED)
  723. return -EHOSTUNREACH;
  724. if (size <= 0 || !buf || !index)
  725. return -EINVAL;
  726. buf[0] = 0;
  727. tbuf = kmalloc(256, GFP_NOIO);
  728. if (!tbuf)
  729. return -ENOMEM;
  730. err = usb_get_langid(dev, tbuf);
  731. if (err < 0)
  732. goto errout;
  733. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  734. if (err < 0)
  735. goto errout;
  736. size--; /* leave room for trailing NULL char in output buffer */
  737. err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
  738. UTF16_LITTLE_ENDIAN, buf, size);
  739. buf[err] = 0;
  740. if (tbuf[1] != USB_DT_STRING)
  741. dev_dbg(&dev->dev,
  742. "wrong descriptor type %02x for string %d (\"%s\")\n",
  743. tbuf[1], index, buf);
  744. errout:
  745. kfree(tbuf);
  746. return err;
  747. }
  748. EXPORT_SYMBOL_GPL(usb_string);
  749. /* one UTF-8-encoded 16-bit character has at most three bytes */
  750. #define MAX_USB_STRING_SIZE (127 * 3 + 1)
  751. /**
  752. * usb_cache_string - read a string descriptor and cache it for later use
  753. * @udev: the device whose string descriptor is being read
  754. * @index: the descriptor index
  755. *
  756. * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
  757. * or NULL if the index is 0 or the string could not be read.
  758. */
  759. char *usb_cache_string(struct usb_device *udev, int index)
  760. {
  761. char *buf;
  762. char *smallbuf = NULL;
  763. int len;
  764. if (index <= 0)
  765. return NULL;
  766. buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
  767. if (buf) {
  768. len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
  769. if (len > 0) {
  770. smallbuf = kmalloc(++len, GFP_NOIO);
  771. if (!smallbuf)
  772. return buf;
  773. memcpy(smallbuf, buf, len);
  774. }
  775. kfree(buf);
  776. }
  777. return smallbuf;
  778. }
  779. /*
  780. * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
  781. * @dev: the device whose device descriptor is being updated
  782. * @size: how much of the descriptor to read
  783. * Context: !in_interrupt ()
  784. *
  785. * Updates the copy of the device descriptor stored in the device structure,
  786. * which dedicates space for this purpose.
  787. *
  788. * Not exported, only for use by the core. If drivers really want to read
  789. * the device descriptor directly, they can call usb_get_descriptor() with
  790. * type = USB_DT_DEVICE and index = 0.
  791. *
  792. * This call is synchronous, and may not be used in an interrupt context.
  793. *
  794. * Returns the number of bytes received on success, or else the status code
  795. * returned by the underlying usb_control_msg() call.
  796. */
  797. int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
  798. {
  799. struct usb_device_descriptor *desc;
  800. int ret;
  801. if (size > sizeof(*desc))
  802. return -EINVAL;
  803. desc = kmalloc(sizeof(*desc), GFP_NOIO);
  804. if (!desc)
  805. return -ENOMEM;
  806. ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
  807. if (ret >= 0)
  808. memcpy(&dev->descriptor, desc, size);
  809. kfree(desc);
  810. return ret;
  811. }
  812. /**
  813. * usb_get_status - issues a GET_STATUS call
  814. * @dev: the device whose status is being checked
  815. * @type: USB_RECIP_*; for device, interface, or endpoint
  816. * @target: zero (for device), else interface or endpoint number
  817. * @data: pointer to two bytes of bitmap data
  818. * Context: !in_interrupt ()
  819. *
  820. * Returns device, interface, or endpoint status. Normally only of
  821. * interest to see if the device is self powered, or has enabled the
  822. * remote wakeup facility; or whether a bulk or interrupt endpoint
  823. * is halted ("stalled").
  824. *
  825. * Bits in these status bitmaps are set using the SET_FEATURE request,
  826. * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
  827. * function should be used to clear halt ("stall") status.
  828. *
  829. * This call is synchronous, and may not be used in an interrupt context.
  830. *
  831. * Returns the number of bytes received on success, or else the status code
  832. * returned by the underlying usb_control_msg() call.
  833. */
  834. int usb_get_status(struct usb_device *dev, int type, int target, void *data)
  835. {
  836. int ret;
  837. u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
  838. if (!status)
  839. return -ENOMEM;
  840. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  841. USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
  842. sizeof(*status), USB_CTRL_GET_TIMEOUT);
  843. *(u16 *)data = *status;
  844. kfree(status);
  845. return ret;
  846. }
  847. EXPORT_SYMBOL_GPL(usb_get_status);
  848. /**
  849. * usb_clear_halt - tells device to clear endpoint halt/stall condition
  850. * @dev: device whose endpoint is halted
  851. * @pipe: endpoint "pipe" being cleared
  852. * Context: !in_interrupt ()
  853. *
  854. * This is used to clear halt conditions for bulk and interrupt endpoints,
  855. * as reported by URB completion status. Endpoints that are halted are
  856. * sometimes referred to as being "stalled". Such endpoints are unable
  857. * to transmit or receive data until the halt status is cleared. Any URBs
  858. * queued for such an endpoint should normally be unlinked by the driver
  859. * before clearing the halt condition, as described in sections 5.7.5
  860. * and 5.8.5 of the USB 2.0 spec.
  861. *
  862. * Note that control and isochronous endpoints don't halt, although control
  863. * endpoints report "protocol stall" (for unsupported requests) using the
  864. * same status code used to report a true stall.
  865. *
  866. * This call is synchronous, and may not be used in an interrupt context.
  867. *
  868. * Returns zero on success, or else the status code returned by the
  869. * underlying usb_control_msg() call.
  870. */
  871. int usb_clear_halt(struct usb_device *dev, int pipe)
  872. {
  873. int result;
  874. int endp = usb_pipeendpoint(pipe);
  875. if (usb_pipein(pipe))
  876. endp |= USB_DIR_IN;
  877. /* we don't care if it wasn't halted first. in fact some devices
  878. * (like some ibmcam model 1 units) seem to expect hosts to make
  879. * this request for iso endpoints, which can't halt!
  880. */
  881. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  882. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
  883. USB_ENDPOINT_HALT, endp, NULL, 0,
  884. USB_CTRL_SET_TIMEOUT);
  885. /* don't un-halt or force to DATA0 except on success */
  886. if (result < 0)
  887. return result;
  888. /* NOTE: seems like Microsoft and Apple don't bother verifying
  889. * the clear "took", so some devices could lock up if you check...
  890. * such as the Hagiwara FlashGate DUAL. So we won't bother.
  891. *
  892. * NOTE: make sure the logic here doesn't diverge much from
  893. * the copy in usb-storage, for as long as we need two copies.
  894. */
  895. usb_reset_endpoint(dev, endp);
  896. return 0;
  897. }
  898. EXPORT_SYMBOL_GPL(usb_clear_halt);
  899. static int create_intf_ep_devs(struct usb_interface *intf)
  900. {
  901. struct usb_device *udev = interface_to_usbdev(intf);
  902. struct usb_host_interface *alt = intf->cur_altsetting;
  903. int i;
  904. if (intf->ep_devs_created || intf->unregistering)
  905. return 0;
  906. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  907. (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
  908. intf->ep_devs_created = 1;
  909. return 0;
  910. }
  911. static void remove_intf_ep_devs(struct usb_interface *intf)
  912. {
  913. struct usb_host_interface *alt = intf->cur_altsetting;
  914. int i;
  915. if (!intf->ep_devs_created)
  916. return;
  917. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  918. usb_remove_ep_devs(&alt->endpoint[i]);
  919. intf->ep_devs_created = 0;
  920. }
  921. /**
  922. * usb_disable_endpoint -- Disable an endpoint by address
  923. * @dev: the device whose endpoint is being disabled
  924. * @epaddr: the endpoint's address. Endpoint number for output,
  925. * endpoint number + USB_DIR_IN for input
  926. * @reset_hardware: flag to erase any endpoint state stored in the
  927. * controller hardware
  928. *
  929. * Disables the endpoint for URB submission and nukes all pending URBs.
  930. * If @reset_hardware is set then also deallocates hcd/hardware state
  931. * for the endpoint.
  932. */
  933. void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
  934. bool reset_hardware)
  935. {
  936. unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
  937. struct usb_host_endpoint *ep;
  938. if (!dev)
  939. return;
  940. if (usb_endpoint_out(epaddr)) {
  941. ep = dev->ep_out[epnum];
  942. if (reset_hardware)
  943. dev->ep_out[epnum] = NULL;
  944. } else {
  945. ep = dev->ep_in[epnum];
  946. if (reset_hardware)
  947. dev->ep_in[epnum] = NULL;
  948. }
  949. if (ep) {
  950. ep->enabled = 0;
  951. usb_hcd_flush_endpoint(dev, ep);
  952. if (reset_hardware)
  953. usb_hcd_disable_endpoint(dev, ep);
  954. }
  955. }
  956. /**
  957. * usb_reset_endpoint - Reset an endpoint's state.
  958. * @dev: the device whose endpoint is to be reset
  959. * @epaddr: the endpoint's address. Endpoint number for output,
  960. * endpoint number + USB_DIR_IN for input
  961. *
  962. * Resets any host-side endpoint state such as the toggle bit,
  963. * sequence number or current window.
  964. */
  965. void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
  966. {
  967. unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
  968. struct usb_host_endpoint *ep;
  969. if (usb_endpoint_out(epaddr))
  970. ep = dev->ep_out[epnum];
  971. else
  972. ep = dev->ep_in[epnum];
  973. if (ep)
  974. usb_hcd_reset_endpoint(dev, ep);
  975. }
  976. EXPORT_SYMBOL_GPL(usb_reset_endpoint);
  977. /**
  978. * usb_disable_interface -- Disable all endpoints for an interface
  979. * @dev: the device whose interface is being disabled
  980. * @intf: pointer to the interface descriptor
  981. * @reset_hardware: flag to erase any endpoint state stored in the
  982. * controller hardware
  983. *
  984. * Disables all the endpoints for the interface's current altsetting.
  985. */
  986. void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
  987. bool reset_hardware)
  988. {
  989. struct usb_host_interface *alt = intf->cur_altsetting;
  990. int i;
  991. for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
  992. usb_disable_endpoint(dev,
  993. alt->endpoint[i].desc.bEndpointAddress,
  994. reset_hardware);
  995. }
  996. }
  997. /**
  998. * usb_disable_device - Disable all the endpoints for a USB device
  999. * @dev: the device whose endpoints are being disabled
  1000. * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
  1001. *
  1002. * Disables all the device's endpoints, potentially including endpoint 0.
  1003. * Deallocates hcd/hardware state for the endpoints (nuking all or most
  1004. * pending urbs) and usbcore state for the interfaces, so that usbcore
  1005. * must usb_set_configuration() before any interfaces could be used.
  1006. *
  1007. * Must be called with hcd->bandwidth_mutex held.
  1008. */
  1009. void usb_disable_device(struct usb_device *dev, int skip_ep0)
  1010. {
  1011. int i;
  1012. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1013. /* getting rid of interfaces will disconnect
  1014. * any drivers bound to them (a key side effect)
  1015. */
  1016. if (dev->actconfig) {
  1017. /*
  1018. * FIXME: In order to avoid self-deadlock involving the
  1019. * bandwidth_mutex, we have to mark all the interfaces
  1020. * before unregistering any of them.
  1021. */
  1022. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
  1023. dev->actconfig->interface[i]->unregistering = 1;
  1024. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1025. struct usb_interface *interface;
  1026. /* remove this interface if it has been registered */
  1027. interface = dev->actconfig->interface[i];
  1028. if (!device_is_registered(&interface->dev))
  1029. continue;
  1030. dev_dbg(&dev->dev, "unregistering interface %s\n",
  1031. dev_name(&interface->dev));
  1032. remove_intf_ep_devs(interface);
  1033. device_del(&interface->dev);
  1034. }
  1035. /* Now that the interfaces are unbound, nobody should
  1036. * try to access them.
  1037. */
  1038. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1039. put_device(&dev->actconfig->interface[i]->dev);
  1040. dev->actconfig->interface[i] = NULL;
  1041. }
  1042. dev->actconfig = NULL;
  1043. if (dev->state == USB_STATE_CONFIGURED)
  1044. usb_set_device_state(dev, USB_STATE_ADDRESS);
  1045. }
  1046. dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
  1047. skip_ep0 ? "non-ep0" : "all");
  1048. if (hcd->driver->check_bandwidth) {
  1049. /* First pass: Cancel URBs, leave endpoint pointers intact. */
  1050. for (i = skip_ep0; i < 16; ++i) {
  1051. usb_disable_endpoint(dev, i, false);
  1052. usb_disable_endpoint(dev, i + USB_DIR_IN, false);
  1053. }
  1054. /* Remove endpoints from the host controller internal state */
  1055. usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
  1056. /* Second pass: remove endpoint pointers */
  1057. }
  1058. for (i = skip_ep0; i < 16; ++i) {
  1059. usb_disable_endpoint(dev, i, true);
  1060. usb_disable_endpoint(dev, i + USB_DIR_IN, true);
  1061. }
  1062. }
  1063. /**
  1064. * usb_enable_endpoint - Enable an endpoint for USB communications
  1065. * @dev: the device whose interface is being enabled
  1066. * @ep: the endpoint
  1067. * @reset_ep: flag to reset the endpoint state
  1068. *
  1069. * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
  1070. * For control endpoints, both the input and output sides are handled.
  1071. */
  1072. void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
  1073. bool reset_ep)
  1074. {
  1075. int epnum = usb_endpoint_num(&ep->desc);
  1076. int is_out = usb_endpoint_dir_out(&ep->desc);
  1077. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1078. if (reset_ep)
  1079. usb_hcd_reset_endpoint(dev, ep);
  1080. if (is_out || is_control)
  1081. dev->ep_out[epnum] = ep;
  1082. if (!is_out || is_control)
  1083. dev->ep_in[epnum] = ep;
  1084. ep->enabled = 1;
  1085. }
  1086. /**
  1087. * usb_enable_interface - Enable all the endpoints for an interface
  1088. * @dev: the device whose interface is being enabled
  1089. * @intf: pointer to the interface descriptor
  1090. * @reset_eps: flag to reset the endpoints' state
  1091. *
  1092. * Enables all the endpoints for the interface's current altsetting.
  1093. */
  1094. void usb_enable_interface(struct usb_device *dev,
  1095. struct usb_interface *intf, bool reset_eps)
  1096. {
  1097. struct usb_host_interface *alt = intf->cur_altsetting;
  1098. int i;
  1099. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  1100. usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
  1101. }
  1102. /**
  1103. * usb_set_interface - Makes a particular alternate setting be current
  1104. * @dev: the device whose interface is being updated
  1105. * @interface: the interface being updated
  1106. * @alternate: the setting being chosen.
  1107. * Context: !in_interrupt ()
  1108. *
  1109. * This is used to enable data transfers on interfaces that may not
  1110. * be enabled by default. Not all devices support such configurability.
  1111. * Only the driver bound to an interface may change its setting.
  1112. *
  1113. * Within any given configuration, each interface may have several
  1114. * alternative settings. These are often used to control levels of
  1115. * bandwidth consumption. For example, the default setting for a high
  1116. * speed interrupt endpoint may not send more than 64 bytes per microframe,
  1117. * while interrupt transfers of up to 3KBytes per microframe are legal.
  1118. * Also, isochronous endpoints may never be part of an
  1119. * interface's default setting. To access such bandwidth, alternate
  1120. * interface settings must be made current.
  1121. *
  1122. * Note that in the Linux USB subsystem, bandwidth associated with
  1123. * an endpoint in a given alternate setting is not reserved until an URB
  1124. * is submitted that needs that bandwidth. Some other operating systems
  1125. * allocate bandwidth early, when a configuration is chosen.
  1126. *
  1127. * This call is synchronous, and may not be used in an interrupt context.
  1128. * Also, drivers must not change altsettings while urbs are scheduled for
  1129. * endpoints in that interface; all such urbs must first be completed
  1130. * (perhaps forced by unlinking).
  1131. *
  1132. * Returns zero on success, or else the status code returned by the
  1133. * underlying usb_control_msg() call.
  1134. */
  1135. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  1136. {
  1137. struct usb_interface *iface;
  1138. struct usb_host_interface *alt;
  1139. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1140. int ret;
  1141. int manual = 0;
  1142. unsigned int epaddr;
  1143. unsigned int pipe;
  1144. if (dev->state == USB_STATE_SUSPENDED)
  1145. return -EHOSTUNREACH;
  1146. iface = usb_ifnum_to_if(dev, interface);
  1147. if (!iface) {
  1148. dev_dbg(&dev->dev, "selecting invalid interface %d\n",
  1149. interface);
  1150. return -EINVAL;
  1151. }
  1152. if (iface->unregistering)
  1153. return -ENODEV;
  1154. alt = usb_altnum_to_altsetting(iface, alternate);
  1155. if (!alt) {
  1156. dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
  1157. alternate);
  1158. return -EINVAL;
  1159. }
  1160. /* Make sure we have enough bandwidth for this alternate interface.
  1161. * Remove the current alt setting and add the new alt setting.
  1162. */
  1163. mutex_lock(hcd->bandwidth_mutex);
  1164. ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
  1165. if (ret < 0) {
  1166. dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
  1167. alternate);
  1168. mutex_unlock(hcd->bandwidth_mutex);
  1169. return ret;
  1170. }
  1171. if (dev->quirks & USB_QUIRK_NO_SET_INTF)
  1172. ret = -EPIPE;
  1173. else
  1174. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1175. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  1176. alternate, interface, NULL, 0, 5000);
  1177. /* 9.4.10 says devices don't need this and are free to STALL the
  1178. * request if the interface only has one alternate setting.
  1179. */
  1180. if (ret == -EPIPE && iface->num_altsetting == 1) {
  1181. dev_dbg(&dev->dev,
  1182. "manual set_interface for iface %d, alt %d\n",
  1183. interface, alternate);
  1184. manual = 1;
  1185. } else if (ret < 0) {
  1186. /* Re-instate the old alt setting */
  1187. usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
  1188. mutex_unlock(hcd->bandwidth_mutex);
  1189. return ret;
  1190. }
  1191. mutex_unlock(hcd->bandwidth_mutex);
  1192. /* FIXME drivers shouldn't need to replicate/bugfix the logic here
  1193. * when they implement async or easily-killable versions of this or
  1194. * other "should-be-internal" functions (like clear_halt).
  1195. * should hcd+usbcore postprocess control requests?
  1196. */
  1197. /* prevent submissions using previous endpoint settings */
  1198. if (iface->cur_altsetting != alt) {
  1199. remove_intf_ep_devs(iface);
  1200. usb_remove_sysfs_intf_files(iface);
  1201. }
  1202. usb_disable_interface(dev, iface, true);
  1203. iface->cur_altsetting = alt;
  1204. /* If the interface only has one altsetting and the device didn't
  1205. * accept the request, we attempt to carry out the equivalent action
  1206. * by manually clearing the HALT feature for each endpoint in the
  1207. * new altsetting.
  1208. */
  1209. if (manual) {
  1210. int i;
  1211. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  1212. epaddr = alt->endpoint[i].desc.bEndpointAddress;
  1213. pipe = __create_pipe(dev,
  1214. USB_ENDPOINT_NUMBER_MASK & epaddr) |
  1215. (usb_endpoint_out(epaddr) ?
  1216. USB_DIR_OUT : USB_DIR_IN);
  1217. usb_clear_halt(dev, pipe);
  1218. }
  1219. }
  1220. /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
  1221. *
  1222. * Note:
  1223. * Despite EP0 is always present in all interfaces/AS, the list of
  1224. * endpoints from the descriptor does not contain EP0. Due to its
  1225. * omnipresence one might expect EP0 being considered "affected" by
  1226. * any SetInterface request and hence assume toggles need to be reset.
  1227. * However, EP0 toggles are re-synced for every individual transfer
  1228. * during the SETUP stage - hence EP0 toggles are "don't care" here.
  1229. * (Likewise, EP0 never "halts" on well designed devices.)
  1230. */
  1231. usb_enable_interface(dev, iface, true);
  1232. if (device_is_registered(&iface->dev)) {
  1233. usb_create_sysfs_intf_files(iface);
  1234. create_intf_ep_devs(iface);
  1235. }
  1236. return 0;
  1237. }
  1238. EXPORT_SYMBOL_GPL(usb_set_interface);
  1239. /**
  1240. * usb_reset_configuration - lightweight device reset
  1241. * @dev: the device whose configuration is being reset
  1242. *
  1243. * This issues a standard SET_CONFIGURATION request to the device using
  1244. * the current configuration. The effect is to reset most USB-related
  1245. * state in the device, including interface altsettings (reset to zero),
  1246. * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
  1247. * endpoints). Other usbcore state is unchanged, including bindings of
  1248. * usb device drivers to interfaces.
  1249. *
  1250. * Because this affects multiple interfaces, avoid using this with composite
  1251. * (multi-interface) devices. Instead, the driver for each interface may
  1252. * use usb_set_interface() on the interfaces it claims. Be careful though;
  1253. * some devices don't support the SET_INTERFACE request, and others won't
  1254. * reset all the interface state (notably endpoint state). Resetting the whole
  1255. * configuration would affect other drivers' interfaces.
  1256. *
  1257. * The caller must own the device lock.
  1258. *
  1259. * Returns zero on success, else a negative error code.
  1260. */
  1261. int usb_reset_configuration(struct usb_device *dev)
  1262. {
  1263. int i, retval;
  1264. struct usb_host_config *config;
  1265. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1266. if (dev->state == USB_STATE_SUSPENDED)
  1267. return -EHOSTUNREACH;
  1268. /* caller must have locked the device and must own
  1269. * the usb bus readlock (so driver bindings are stable);
  1270. * calls during probe() are fine
  1271. */
  1272. for (i = 1; i < 16; ++i) {
  1273. usb_disable_endpoint(dev, i, true);
  1274. usb_disable_endpoint(dev, i + USB_DIR_IN, true);
  1275. }
  1276. config = dev->actconfig;
  1277. retval = 0;
  1278. mutex_lock(hcd->bandwidth_mutex);
  1279. /* Make sure we have enough bandwidth for each alternate setting 0 */
  1280. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  1281. struct usb_interface *intf = config->interface[i];
  1282. struct usb_host_interface *alt;
  1283. alt = usb_altnum_to_altsetting(intf, 0);
  1284. if (!alt)
  1285. alt = &intf->altsetting[0];
  1286. if (alt != intf->cur_altsetting)
  1287. retval = usb_hcd_alloc_bandwidth(dev, NULL,
  1288. intf->cur_altsetting, alt);
  1289. if (retval < 0)
  1290. break;
  1291. }
  1292. /* If not, reinstate the old alternate settings */
  1293. if (retval < 0) {
  1294. reset_old_alts:
  1295. for (i--; i >= 0; i--) {
  1296. struct usb_interface *intf = config->interface[i];
  1297. struct usb_host_interface *alt;
  1298. alt = usb_altnum_to_altsetting(intf, 0);
  1299. if (!alt)
  1300. alt = &intf->altsetting[0];
  1301. if (alt != intf->cur_altsetting)
  1302. usb_hcd_alloc_bandwidth(dev, NULL,
  1303. alt, intf->cur_altsetting);
  1304. }
  1305. mutex_unlock(hcd->bandwidth_mutex);
  1306. return retval;
  1307. }
  1308. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1309. USB_REQ_SET_CONFIGURATION, 0,
  1310. config->desc.bConfigurationValue, 0,
  1311. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1312. if (retval < 0)
  1313. goto reset_old_alts;
  1314. mutex_unlock(hcd->bandwidth_mutex);
  1315. /* re-init hc/hcd interface/endpoint state */
  1316. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  1317. struct usb_interface *intf = config->interface[i];
  1318. struct usb_host_interface *alt;
  1319. alt = usb_altnum_to_altsetting(intf, 0);
  1320. /* No altsetting 0? We'll assume the first altsetting.
  1321. * We could use a GetInterface call, but if a device is
  1322. * so non-compliant that it doesn't have altsetting 0
  1323. * then I wouldn't trust its reply anyway.
  1324. */
  1325. if (!alt)
  1326. alt = &intf->altsetting[0];
  1327. if (alt != intf->cur_altsetting) {
  1328. remove_intf_ep_devs(intf);
  1329. usb_remove_sysfs_intf_files(intf);
  1330. }
  1331. intf->cur_altsetting = alt;
  1332. usb_enable_interface(dev, intf, true);
  1333. if (device_is_registered(&intf->dev)) {
  1334. usb_create_sysfs_intf_files(intf);
  1335. create_intf_ep_devs(intf);
  1336. }
  1337. }
  1338. return 0;
  1339. }
  1340. EXPORT_SYMBOL_GPL(usb_reset_configuration);
  1341. static void usb_release_interface(struct device *dev)
  1342. {
  1343. struct usb_interface *intf = to_usb_interface(dev);
  1344. struct usb_interface_cache *intfc =
  1345. altsetting_to_usb_interface_cache(intf->altsetting);
  1346. kref_put(&intfc->ref, usb_release_interface_cache);
  1347. kfree(intf);
  1348. }
  1349. #ifdef CONFIG_HOTPLUG
  1350. static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
  1351. {
  1352. struct usb_device *usb_dev;
  1353. struct usb_interface *intf;
  1354. struct usb_host_interface *alt;
  1355. intf = to_usb_interface(dev);
  1356. usb_dev = interface_to_usbdev(intf);
  1357. alt = intf->cur_altsetting;
  1358. if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
  1359. alt->desc.bInterfaceClass,
  1360. alt->desc.bInterfaceSubClass,
  1361. alt->desc.bInterfaceProtocol))
  1362. return -ENOMEM;
  1363. if (add_uevent_var(env,
  1364. "MODALIAS=usb:"
  1365. "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
  1366. le16_to_cpu(usb_dev->descriptor.idVendor),
  1367. le16_to_cpu(usb_dev->descriptor.idProduct),
  1368. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  1369. usb_dev->descriptor.bDeviceClass,
  1370. usb_dev->descriptor.bDeviceSubClass,
  1371. usb_dev->descriptor.bDeviceProtocol,
  1372. alt->desc.bInterfaceClass,
  1373. alt->desc.bInterfaceSubClass,
  1374. alt->desc.bInterfaceProtocol))
  1375. return -ENOMEM;
  1376. return 0;
  1377. }
  1378. #else
  1379. static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
  1380. {
  1381. return -ENODEV;
  1382. }
  1383. #endif /* CONFIG_HOTPLUG */
  1384. struct device_type usb_if_device_type = {
  1385. .name = "usb_interface",
  1386. .release = usb_release_interface,
  1387. .uevent = usb_if_uevent,
  1388. };
  1389. static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
  1390. struct usb_host_config *config,
  1391. u8 inum)
  1392. {
  1393. struct usb_interface_assoc_descriptor *retval = NULL;
  1394. struct usb_interface_assoc_descriptor *intf_assoc;
  1395. int first_intf;
  1396. int last_intf;
  1397. int i;
  1398. for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
  1399. intf_assoc = config->intf_assoc[i];
  1400. if (intf_assoc->bInterfaceCount == 0)
  1401. continue;
  1402. first_intf = intf_assoc->bFirstInterface;
  1403. last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
  1404. if (inum >= first_intf && inum <= last_intf) {
  1405. if (!retval)
  1406. retval = intf_assoc;
  1407. else
  1408. dev_err(&dev->dev, "Interface #%d referenced"
  1409. " by multiple IADs\n", inum);
  1410. }
  1411. }
  1412. return retval;
  1413. }
  1414. /*
  1415. * Internal function to queue a device reset
  1416. *
  1417. * This is initialized into the workstruct in 'struct
  1418. * usb_device->reset_ws' that is launched by
  1419. * message.c:usb_set_configuration() when initializing each 'struct
  1420. * usb_interface'.
  1421. *
  1422. * It is safe to get the USB device without reference counts because
  1423. * the life cycle of @iface is bound to the life cycle of @udev. Then,
  1424. * this function will be ran only if @iface is alive (and before
  1425. * freeing it any scheduled instances of it will have been cancelled).
  1426. *
  1427. * We need to set a flag (usb_dev->reset_running) because when we call
  1428. * the reset, the interfaces might be unbound. The current interface
  1429. * cannot try to remove the queued work as it would cause a deadlock
  1430. * (you cannot remove your work from within your executing
  1431. * workqueue). This flag lets it know, so that
  1432. * usb_cancel_queued_reset() doesn't try to do it.
  1433. *
  1434. * See usb_queue_reset_device() for more details
  1435. */
  1436. static void __usb_queue_reset_device(struct work_struct *ws)
  1437. {
  1438. int rc;
  1439. struct usb_interface *iface =
  1440. container_of(ws, struct usb_interface, reset_ws);
  1441. struct usb_device *udev = interface_to_usbdev(iface);
  1442. rc = usb_lock_device_for_reset(udev, iface);
  1443. if (rc >= 0) {
  1444. iface->reset_running = 1;
  1445. usb_reset_device(udev);
  1446. iface->reset_running = 0;
  1447. usb_unlock_device(udev);
  1448. }
  1449. }
  1450. /*
  1451. * usb_set_configuration - Makes a particular device setting be current
  1452. * @dev: the device whose configuration is being updated
  1453. * @configuration: the configuration being chosen.
  1454. * Context: !in_interrupt(), caller owns the device lock
  1455. *
  1456. * This is used to enable non-default device modes. Not all devices
  1457. * use this kind of configurability; many devices only have one
  1458. * configuration.
  1459. *
  1460. * @configuration is the value of the configuration to be installed.
  1461. * According to the USB spec (e.g. section 9.1.1.5), configuration values
  1462. * must be non-zero; a value of zero indicates that the device in
  1463. * unconfigured. However some devices erroneously use 0 as one of their
  1464. * configuration values. To help manage such devices, this routine will
  1465. * accept @configuration = -1 as indicating the device should be put in
  1466. * an unconfigured state.
  1467. *
  1468. * USB device configurations may affect Linux interoperability,
  1469. * power consumption and the functionality available. For example,
  1470. * the default configuration is limited to using 100mA of bus power,
  1471. * so that when certain device functionality requires more power,
  1472. * and the device is bus powered, that functionality should be in some
  1473. * non-default device configuration. Other device modes may also be
  1474. * reflected as configuration options, such as whether two ISDN
  1475. * channels are available independently; and choosing between open
  1476. * standard device protocols (like CDC) or proprietary ones.
  1477. *
  1478. * Note that a non-authorized device (dev->authorized == 0) will only
  1479. * be put in unconfigured mode.
  1480. *
  1481. * Note that USB has an additional level of device configurability,
  1482. * associated with interfaces. That configurability is accessed using
  1483. * usb_set_interface().
  1484. *
  1485. * This call is synchronous. The calling context must be able to sleep,
  1486. * must own the device lock, and must not hold the driver model's USB
  1487. * bus mutex; usb interface driver probe() methods cannot use this routine.
  1488. *
  1489. * Returns zero on success, or else the status code returned by the
  1490. * underlying call that failed. On successful completion, each interface
  1491. * in the original device configuration has been destroyed, and each one
  1492. * in the new configuration has been probed by all relevant usb device
  1493. * drivers currently known to the kernel.
  1494. */
  1495. int usb_set_configuration(struct usb_device *dev, int configuration)
  1496. {
  1497. int i, ret;
  1498. struct usb_host_config *cp = NULL;
  1499. struct usb_interface **new_interfaces = NULL;
  1500. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1501. int n, nintf;
  1502. if (dev->authorized == 0 || configuration == -1)
  1503. configuration = 0;
  1504. else {
  1505. for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
  1506. if (dev->config[i].desc.bConfigurationValue ==
  1507. configuration) {
  1508. cp = &dev->config[i];
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. if ((!cp && configuration != 0))
  1514. return -EINVAL;
  1515. /* The USB spec says configuration 0 means unconfigured.
  1516. * But if a device includes a configuration numbered 0,
  1517. * we will accept it as a correctly configured state.
  1518. * Use -1 if you really want to unconfigure the device.
  1519. */
  1520. if (cp && configuration == 0)
  1521. dev_warn(&dev->dev, "config 0 descriptor??\n");
  1522. /* Allocate memory for new interfaces before doing anything else,
  1523. * so that if we run out then nothing will have changed. */
  1524. n = nintf = 0;
  1525. if (cp) {
  1526. nintf = cp->desc.bNumInterfaces;
  1527. new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
  1528. GFP_NOIO);
  1529. if (!new_interfaces) {
  1530. dev_err(&dev->dev, "Out of memory\n");
  1531. return -ENOMEM;
  1532. }
  1533. for (; n < nintf; ++n) {
  1534. new_interfaces[n] = kzalloc(
  1535. sizeof(struct usb_interface),
  1536. GFP_NOIO);
  1537. if (!new_interfaces[n]) {
  1538. dev_err(&dev->dev, "Out of memory\n");
  1539. ret = -ENOMEM;
  1540. free_interfaces:
  1541. while (--n >= 0)
  1542. kfree(new_interfaces[n]);
  1543. kfree(new_interfaces);
  1544. return ret;
  1545. }
  1546. }
  1547. i = dev->bus_mA - cp->desc.bMaxPower * 2;
  1548. if (i < 0)
  1549. dev_warn(&dev->dev, "new config #%d exceeds power "
  1550. "limit by %dmA\n",
  1551. configuration, -i);
  1552. }
  1553. /* Wake up the device so we can send it the Set-Config request */
  1554. ret = usb_autoresume_device(dev);
  1555. if (ret)
  1556. goto free_interfaces;
  1557. /* if it's already configured, clear out old state first.
  1558. * getting rid of old interfaces means unbinding their drivers.
  1559. */
  1560. mutex_lock(hcd->bandwidth_mutex);
  1561. if (dev->state != USB_STATE_ADDRESS)
  1562. usb_disable_device(dev, 1); /* Skip ep0 */
  1563. /* Get rid of pending async Set-Config requests for this device */
  1564. cancel_async_set_config(dev);
  1565. /* Make sure we have bandwidth (and available HCD resources) for this
  1566. * configuration. Remove endpoints from the schedule if we're dropping
  1567. * this configuration to set configuration 0. After this point, the
  1568. * host controller will not allow submissions to dropped endpoints. If
  1569. * this call fails, the device state is unchanged.
  1570. */
  1571. ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
  1572. if (ret < 0) {
  1573. mutex_unlock(hcd->bandwidth_mutex);
  1574. usb_autosuspend_device(dev);
  1575. goto free_interfaces;
  1576. }
  1577. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1578. USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
  1579. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1580. if (ret < 0) {
  1581. /* All the old state is gone, so what else can we do?
  1582. * The device is probably useless now anyway.
  1583. */
  1584. cp = NULL;
  1585. }
  1586. dev->actconfig = cp;
  1587. if (!cp) {
  1588. usb_set_device_state(dev, USB_STATE_ADDRESS);
  1589. usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
  1590. mutex_unlock(hcd->bandwidth_mutex);
  1591. usb_autosuspend_device(dev);
  1592. goto free_interfaces;
  1593. }
  1594. mutex_unlock(hcd->bandwidth_mutex);
  1595. usb_set_device_state(dev, USB_STATE_CONFIGURED);
  1596. /* Initialize the new interface structures and the
  1597. * hc/hcd/usbcore interface/endpoint state.
  1598. */
  1599. for (i = 0; i < nintf; ++i) {
  1600. struct usb_interface_cache *intfc;
  1601. struct usb_interface *intf;
  1602. struct usb_host_interface *alt;
  1603. cp->interface[i] = intf = new_interfaces[i];
  1604. intfc = cp->intf_cache[i];
  1605. intf->altsetting = intfc->altsetting;
  1606. intf->num_altsetting = intfc->num_altsetting;
  1607. intf->intf_assoc = find_iad(dev, cp, i);
  1608. kref_get(&intfc->ref);
  1609. alt = usb_altnum_to_altsetting(intf, 0);
  1610. /* No altsetting 0? We'll assume the first altsetting.
  1611. * We could use a GetInterface call, but if a device is
  1612. * so non-compliant that it doesn't have altsetting 0
  1613. * then I wouldn't trust its reply anyway.
  1614. */
  1615. if (!alt)
  1616. alt = &intf->altsetting[0];
  1617. intf->cur_altsetting = alt;
  1618. usb_enable_interface(dev, intf, true);
  1619. intf->dev.parent = &dev->dev;
  1620. intf->dev.driver = NULL;
  1621. intf->dev.bus = &usb_bus_type;
  1622. intf->dev.type = &usb_if_device_type;
  1623. intf->dev.groups = usb_interface_groups;
  1624. intf->dev.dma_mask = dev->dev.dma_mask;
  1625. INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
  1626. intf->minor = -1;
  1627. device_initialize(&intf->dev);
  1628. pm_runtime_no_callbacks(&intf->dev);
  1629. dev_set_name(&intf->dev, "%d-%s:%d.%d",
  1630. dev->bus->busnum, dev->devpath,
  1631. configuration, alt->desc.bInterfaceNumber);
  1632. }
  1633. kfree(new_interfaces);
  1634. if (cp->string == NULL &&
  1635. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  1636. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  1637. /* Now that all the interfaces are set up, register them
  1638. * to trigger binding of drivers to interfaces. probe()
  1639. * routines may install different altsettings and may
  1640. * claim() any interfaces not yet bound. Many class drivers
  1641. * need that: CDC, audio, video, etc.
  1642. */
  1643. for (i = 0; i < nintf; ++i) {
  1644. struct usb_interface *intf = cp->interface[i];
  1645. dev_dbg(&dev->dev,
  1646. "adding %s (config #%d, interface %d)\n",
  1647. dev_name(&intf->dev), configuration,
  1648. intf->cur_altsetting->desc.bInterfaceNumber);
  1649. device_enable_async_suspend(&intf->dev);
  1650. ret = device_add(&intf->dev);
  1651. if (ret != 0) {
  1652. dev_err(&dev->dev, "device_add(%s) --> %d\n",
  1653. dev_name(&intf->dev), ret);
  1654. continue;
  1655. }
  1656. create_intf_ep_devs(intf);
  1657. }
  1658. usb_autosuspend_device(dev);
  1659. return 0;
  1660. }
  1661. static LIST_HEAD(set_config_list);
  1662. static DEFINE_SPINLOCK(set_config_lock);
  1663. struct set_config_request {
  1664. struct usb_device *udev;
  1665. int config;
  1666. struct work_struct work;
  1667. struct list_head node;
  1668. };
  1669. /* Worker routine for usb_driver_set_configuration() */
  1670. static void driver_set_config_work(struct work_struct *work)
  1671. {
  1672. struct set_config_request *req =
  1673. container_of(work, struct set_config_request, work);
  1674. struct usb_device *udev = req->udev;
  1675. usb_lock_device(udev);
  1676. spin_lock(&set_config_lock);
  1677. list_del(&req->node);
  1678. spin_unlock(&set_config_lock);
  1679. if (req->config >= -1) /* Is req still valid? */
  1680. usb_set_configuration(udev, req->config);
  1681. usb_unlock_device(udev);
  1682. usb_put_dev(udev);
  1683. kfree(req);
  1684. }
  1685. /* Cancel pending Set-Config requests for a device whose configuration
  1686. * was just changed
  1687. */
  1688. static void cancel_async_set_config(struct usb_device *udev)
  1689. {
  1690. struct set_config_request *req;
  1691. spin_lock(&set_config_lock);
  1692. list_for_each_entry(req, &set_config_list, node) {
  1693. if (req->udev == udev)
  1694. req->config = -999; /* Mark as cancelled */
  1695. }
  1696. spin_unlock(&set_config_lock);
  1697. }
  1698. /**
  1699. * usb_driver_set_configuration - Provide a way for drivers to change device configurations
  1700. * @udev: the device whose configuration is being updated
  1701. * @config: the configuration being chosen.
  1702. * Context: In process context, must be able to sleep
  1703. *
  1704. * Device interface drivers are not allowed to change device configurations.
  1705. * This is because changing configurations will destroy the interface the
  1706. * driver is bound to and create new ones; it would be like a floppy-disk
  1707. * driver telling the computer to replace the floppy-disk drive with a
  1708. * tape drive!
  1709. *
  1710. * Still, in certain specialized circumstances the need may arise. This
  1711. * routine gets around the normal restrictions by using a work thread to
  1712. * submit the change-config request.
  1713. *
  1714. * Returns 0 if the request was successfully queued, error code otherwise.
  1715. * The caller has no way to know whether the queued request will eventually
  1716. * succeed.
  1717. */
  1718. int usb_driver_set_configuration(struct usb_device *udev, int config)
  1719. {
  1720. struct set_config_request *req;
  1721. req = kmalloc(sizeof(*req), GFP_KERNEL);
  1722. if (!req)
  1723. return -ENOMEM;
  1724. req->udev = udev;
  1725. req->config = config;
  1726. INIT_WORK(&req->work, driver_set_config_work);
  1727. spin_lock(&set_config_lock);
  1728. list_add(&req->node, &set_config_list);
  1729. spin_unlock(&set_config_lock);
  1730. usb_get_dev(udev);
  1731. schedule_work(&req->work);
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL_GPL(usb_driver_set_configuration);