message.c 57 KB

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