message.c 58 KB

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