message.c 57 KB

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