message.c 55 KB

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