message.c 56 KB

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