message.c 56 KB

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