message.c 58 KB

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