message.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * message.c - synchronous message handling
  4. *
  5. * Released under the GPLv2 only.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/pci.h> /* for scatterlist macros */
  9. #include <linux/usb.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/slab.h>
  13. #include <linux/mm.h>
  14. #include <linux/timer.h>
  15. #include <linux/ctype.h>
  16. #include <linux/nls.h>
  17. #include <linux/device.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/usb/cdc.h>
  20. #include <linux/usb/quirks.h>
  21. #include <linux/usb/hcd.h> /* for usbcore internals */
  22. #include <linux/usb/of.h>
  23. #include <asm/byteorder.h>
  24. #include "usb.h"
  25. static void cancel_async_set_config(struct usb_device *udev);
  26. struct api_context {
  27. struct completion done;
  28. int status;
  29. };
  30. static void usb_api_blocking_completion(struct urb *urb)
  31. {
  32. struct api_context *ctx = urb->context;
  33. ctx->status = urb->status;
  34. complete(&ctx->done);
  35. }
  36. /*
  37. * Starts urb and waits for completion or timeout.
  38. * Whether or not the wait is killable depends on the flag passed in.
  39. * For example, compare usb_bulk_msg() and usb_bulk_msg_killable().
  40. *
  41. * For non-killable waits, we enforce a maximum limit on the timeout value.
  42. */
  43. static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length,
  44. bool killable)
  45. {
  46. struct api_context ctx;
  47. unsigned long expire;
  48. int retval;
  49. long rc;
  50. init_completion(&ctx.done);
  51. urb->context = &ctx;
  52. urb->actual_length = 0;
  53. retval = usb_submit_urb(urb, GFP_NOIO);
  54. if (unlikely(retval))
  55. goto out;
  56. if (!killable && (timeout <= 0 || timeout > USB_MAX_SYNCHRONOUS_TIMEOUT))
  57. timeout = USB_MAX_SYNCHRONOUS_TIMEOUT;
  58. expire = (timeout > 0) ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
  59. if (killable)
  60. rc = wait_for_completion_killable_timeout(&ctx.done, expire);
  61. else
  62. rc = wait_for_completion_timeout(&ctx.done, expire);
  63. if (rc <= 0) {
  64. usb_kill_urb(urb);
  65. if (ctx.status != -ENOENT)
  66. retval = ctx.status;
  67. else if (rc == 0)
  68. retval = -ETIMEDOUT;
  69. else
  70. retval = rc;
  71. dev_dbg(&urb->dev->dev,
  72. "%s timed out or killed on ep%d%s len=%u/%u\n",
  73. current->comm,
  74. usb_endpoint_num(&urb->ep->desc),
  75. usb_urb_dir_in(urb) ? "in" : "out",
  76. urb->actual_length,
  77. urb->transfer_buffer_length);
  78. } else
  79. retval = ctx.status;
  80. out:
  81. if (actual_length)
  82. *actual_length = urb->actual_length;
  83. usb_free_urb(urb);
  84. return retval;
  85. }
  86. /*-------------------------------------------------------------------*/
  87. /* returns status (negative) or length (positive) */
  88. static int usb_internal_control_msg(struct usb_device *usb_dev,
  89. unsigned int pipe,
  90. struct usb_ctrlrequest *cmd,
  91. void *data, int len, int timeout)
  92. {
  93. struct urb *urb;
  94. int retv;
  95. int length;
  96. urb = usb_alloc_urb(0, GFP_NOIO);
  97. if (!urb)
  98. return -ENOMEM;
  99. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
  100. len, usb_api_blocking_completion, NULL);
  101. retv = usb_start_wait_urb(urb, timeout, &length, false);
  102. if (retv < 0)
  103. return retv;
  104. else
  105. return length;
  106. }
  107. /**
  108. * usb_control_msg - Builds a control urb, sends it off and waits for completion
  109. * @dev: pointer to the usb device to send the message to
  110. * @pipe: endpoint "pipe" to send the message to
  111. * @request: USB message request value
  112. * @requesttype: USB message request type value
  113. * @value: USB message value
  114. * @index: USB message index value
  115. * @data: pointer to the data to send
  116. * @size: length in bytes of the data to send
  117. * @timeout: time in msecs to wait for the message to complete before timing out
  118. *
  119. * Context: task context, might sleep.
  120. *
  121. * This function sends a simple control message to a specified endpoint and
  122. * waits for the message to complete, or timeout.
  123. *
  124. * Don't use this function from within an interrupt context. If you need
  125. * an asynchronous message, or need to send a message from within interrupt
  126. * context, use usb_submit_urb(). If a thread in your driver uses this call,
  127. * make sure your disconnect() method can wait for it to complete. Since you
  128. * don't have a handle on the URB used, you can't cancel the request.
  129. *
  130. * Return: If successful, the number of bytes transferred. Otherwise, a negative
  131. * error number.
  132. */
  133. int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  134. __u8 requesttype, __u16 value, __u16 index, void *data,
  135. __u16 size, int timeout)
  136. {
  137. struct usb_ctrlrequest *dr;
  138. int ret;
  139. dr = kmalloc_obj(struct usb_ctrlrequest, GFP_NOIO);
  140. if (!dr)
  141. return -ENOMEM;
  142. dr->bRequestType = requesttype;
  143. dr->bRequest = request;
  144. dr->wValue = cpu_to_le16(value);
  145. dr->wIndex = cpu_to_le16(index);
  146. dr->wLength = cpu_to_le16(size);
  147. ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
  148. /* Linger a bit, prior to the next control message. */
  149. if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG)
  150. msleep(200);
  151. kfree(dr);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(usb_control_msg);
  155. /**
  156. * usb_control_msg_send - Builds a control "send" message, sends it off and waits for completion
  157. * @dev: pointer to the usb device to send the message to
  158. * @endpoint: endpoint to send the message to
  159. * @request: USB message request value
  160. * @requesttype: USB message request type value
  161. * @value: USB message value
  162. * @index: USB message index value
  163. * @driver_data: pointer to the data to send
  164. * @size: length in bytes of the data to send
  165. * @timeout: time in msecs to wait for the message to complete before timing out
  166. * @memflags: the flags for memory allocation for buffers
  167. *
  168. * Context: !in_interrupt ()
  169. *
  170. * This function sends a control message to a specified endpoint that is not
  171. * expected to fill in a response (i.e. a "send message") and waits for the
  172. * message to complete, or timeout.
  173. *
  174. * Do not use this function from within an interrupt context. If you need
  175. * an asynchronous message, or need to send a message from within interrupt
  176. * context, use usb_submit_urb(). If a thread in your driver uses this call,
  177. * make sure your disconnect() method can wait for it to complete. Since you
  178. * don't have a handle on the URB used, you can't cancel the request.
  179. *
  180. * The data pointer can be made to a reference on the stack, or anywhere else,
  181. * as it will not be modified at all. This does not have the restriction that
  182. * usb_control_msg() has where the data pointer must be to dynamically allocated
  183. * memory (i.e. memory that can be successfully DMAed to a device).
  184. *
  185. * Return: If successful, 0 is returned, Otherwise, a negative error number.
  186. */
  187. int usb_control_msg_send(struct usb_device *dev, __u8 endpoint, __u8 request,
  188. __u8 requesttype, __u16 value, __u16 index,
  189. const void *driver_data, __u16 size, int timeout,
  190. gfp_t memflags)
  191. {
  192. unsigned int pipe = usb_sndctrlpipe(dev, endpoint);
  193. int ret;
  194. u8 *data = NULL;
  195. if (size) {
  196. data = kmemdup(driver_data, size, memflags);
  197. if (!data)
  198. return -ENOMEM;
  199. }
  200. ret = usb_control_msg(dev, pipe, request, requesttype, value, index,
  201. data, size, timeout);
  202. kfree(data);
  203. if (ret < 0)
  204. return ret;
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(usb_control_msg_send);
  208. /**
  209. * usb_control_msg_recv - Builds a control "receive" message, sends it off and waits for completion
  210. * @dev: pointer to the usb device to send the message to
  211. * @endpoint: endpoint to send the message to
  212. * @request: USB message request value
  213. * @requesttype: USB message request type value
  214. * @value: USB message value
  215. * @index: USB message index value
  216. * @driver_data: pointer to the data to be filled in by the message
  217. * @size: length in bytes of the data to be received
  218. * @timeout: time in msecs to wait for the message to complete before timing out
  219. * @memflags: the flags for memory allocation for buffers
  220. *
  221. * Context: !in_interrupt ()
  222. *
  223. * This function sends a control message to a specified endpoint that is
  224. * expected to fill in a response (i.e. a "receive message") and waits for the
  225. * message to complete, or timeout.
  226. *
  227. * Do not use this function from within an interrupt context. If you need
  228. * an asynchronous message, or need to send a message from within interrupt
  229. * context, use usb_submit_urb(). If a thread in your driver uses this call,
  230. * make sure your disconnect() method can wait for it to complete. Since you
  231. * don't have a handle on the URB used, you can't cancel the request.
  232. *
  233. * The data pointer can be made to a reference on the stack, or anywhere else
  234. * that can be successfully written to. This function does not have the
  235. * restriction that usb_control_msg() has where the data pointer must be to
  236. * dynamically allocated memory (i.e. memory that can be successfully DMAed to a
  237. * device).
  238. *
  239. * The "whole" message must be properly received from the device in order for
  240. * this function to be successful. If a device returns less than the expected
  241. * amount of data, then the function will fail. Do not use this for messages
  242. * where a variable amount of data might be returned.
  243. *
  244. * Return: If successful, 0 is returned, Otherwise, a negative error number.
  245. */
  246. int usb_control_msg_recv(struct usb_device *dev, __u8 endpoint, __u8 request,
  247. __u8 requesttype, __u16 value, __u16 index,
  248. void *driver_data, __u16 size, int timeout,
  249. gfp_t memflags)
  250. {
  251. unsigned int pipe = usb_rcvctrlpipe(dev, endpoint);
  252. int ret;
  253. u8 *data;
  254. if (!size || !driver_data)
  255. return -EINVAL;
  256. data = kmalloc(size, memflags);
  257. if (!data)
  258. return -ENOMEM;
  259. ret = usb_control_msg(dev, pipe, request, requesttype, value, index,
  260. data, size, timeout);
  261. if (ret < 0)
  262. goto exit;
  263. if (ret == size) {
  264. memcpy(driver_data, data, size);
  265. ret = 0;
  266. } else {
  267. ret = -EREMOTEIO;
  268. }
  269. exit:
  270. kfree(data);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(usb_control_msg_recv);
  274. /**
  275. * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
  276. * @usb_dev: pointer to the usb device to send the message to
  277. * @pipe: endpoint "pipe" to send the message to
  278. * @data: pointer to the data to send
  279. * @len: length in bytes of the data to send
  280. * @actual_length: pointer to a location to put the actual length transferred
  281. * in bytes
  282. * @timeout: time in msecs to wait for the message to complete before timing out
  283. *
  284. * Context: task context, might sleep.
  285. *
  286. * This function sends a simple interrupt message to a specified endpoint and
  287. * waits for the message to complete, or timeout.
  288. *
  289. * Don't use this function from within an interrupt context. If you need
  290. * an asynchronous message, or need to send a message from within interrupt
  291. * context, use usb_submit_urb() If a thread in your driver uses this call,
  292. * make sure your disconnect() method can wait for it to complete. Since you
  293. * don't have a handle on the URB used, you can't cancel the request.
  294. *
  295. * Return:
  296. * If successful, 0. Otherwise a negative error number. The number of actual
  297. * bytes transferred will be stored in the @actual_length parameter.
  298. */
  299. int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
  300. void *data, int len, int *actual_length, int timeout)
  301. {
  302. return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
  303. }
  304. EXPORT_SYMBOL_GPL(usb_interrupt_msg);
  305. /**
  306. * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
  307. * @usb_dev: pointer to the usb device to send the message to
  308. * @pipe: endpoint "pipe" to send the message to
  309. * @data: pointer to the data to send
  310. * @len: length in bytes of the data to send
  311. * @actual_length: pointer to a location to put the actual length transferred
  312. * in bytes
  313. * @timeout: time in msecs to wait for the message to complete before timing out
  314. *
  315. * Context: task context, might sleep.
  316. *
  317. * This function sends a simple bulk message to a specified endpoint
  318. * and waits for the message to complete, or timeout.
  319. *
  320. * Don't use this function from within an interrupt context. If you need
  321. * an asynchronous message, or need to send a message from within interrupt
  322. * context, use usb_submit_urb() If a thread in your driver uses this call,
  323. * make sure your disconnect() method can wait for it to complete. Since you
  324. * don't have a handle on the URB used, you can't cancel the request.
  325. *
  326. * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
  327. * users are forced to abuse this routine by using it to submit URBs for
  328. * interrupt endpoints. We will take the liberty of creating an interrupt URB
  329. * (with the default interval) if the target is an interrupt endpoint.
  330. *
  331. * Return:
  332. * If successful, 0. Otherwise a negative error number. The number of actual
  333. * bytes transferred will be stored in the @actual_length parameter.
  334. *
  335. */
  336. int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
  337. void *data, int len, int *actual_length, int timeout)
  338. {
  339. struct urb *urb;
  340. struct usb_host_endpoint *ep;
  341. ep = usb_pipe_endpoint(usb_dev, pipe);
  342. if (!ep || len < 0)
  343. return -EINVAL;
  344. urb = usb_alloc_urb(0, GFP_KERNEL);
  345. if (!urb)
  346. return -ENOMEM;
  347. if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  348. USB_ENDPOINT_XFER_INT) {
  349. pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
  350. usb_fill_int_urb(urb, usb_dev, pipe, data, len,
  351. usb_api_blocking_completion, NULL,
  352. ep->desc.bInterval);
  353. } else
  354. usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
  355. usb_api_blocking_completion, NULL);
  356. return usb_start_wait_urb(urb, timeout, actual_length, false);
  357. }
  358. EXPORT_SYMBOL_GPL(usb_bulk_msg);
  359. /**
  360. * usb_bulk_msg_killable - Builds a bulk urb, sends it off and waits for completion in a killable state
  361. * @usb_dev: pointer to the usb device to send the message to
  362. * @pipe: endpoint "pipe" to send the message to
  363. * @data: pointer to the data to send
  364. * @len: length in bytes of the data to send
  365. * @actual_length: pointer to a location to put the actual length transferred
  366. * in bytes
  367. * @timeout: time in msecs to wait for the message to complete before
  368. * timing out (if <= 0, the wait is as long as possible)
  369. *
  370. * Context: task context, might sleep.
  371. *
  372. * This function is just like usb_blk_msg(), except that it waits in a
  373. * killable state and there is no limit on the timeout length.
  374. *
  375. * Return:
  376. * If successful, 0. Otherwise a negative error number. The number of actual
  377. * bytes transferred will be stored in the @actual_length parameter.
  378. *
  379. */
  380. int usb_bulk_msg_killable(struct usb_device *usb_dev, unsigned int pipe,
  381. void *data, int len, int *actual_length, int timeout)
  382. {
  383. struct urb *urb;
  384. struct usb_host_endpoint *ep;
  385. ep = usb_pipe_endpoint(usb_dev, pipe);
  386. if (!ep || len < 0)
  387. return -EINVAL;
  388. urb = usb_alloc_urb(0, GFP_KERNEL);
  389. if (!urb)
  390. return -ENOMEM;
  391. if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  392. USB_ENDPOINT_XFER_INT) {
  393. pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
  394. usb_fill_int_urb(urb, usb_dev, pipe, data, len,
  395. usb_api_blocking_completion, NULL,
  396. ep->desc.bInterval);
  397. } else
  398. usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
  399. usb_api_blocking_completion, NULL);
  400. return usb_start_wait_urb(urb, timeout, actual_length, true);
  401. }
  402. EXPORT_SYMBOL_GPL(usb_bulk_msg_killable);
  403. /*-------------------------------------------------------------------*/
  404. static void sg_clean(struct usb_sg_request *io)
  405. {
  406. if (io->urbs) {
  407. while (io->entries--)
  408. usb_free_urb(io->urbs[io->entries]);
  409. kfree(io->urbs);
  410. io->urbs = NULL;
  411. }
  412. io->dev = NULL;
  413. }
  414. static void sg_complete(struct urb *urb)
  415. {
  416. unsigned long flags;
  417. struct usb_sg_request *io = urb->context;
  418. int status = urb->status;
  419. spin_lock_irqsave(&io->lock, flags);
  420. /* In 2.5 we require hcds' endpoint queues not to progress after fault
  421. * reports, until the completion callback (this!) returns. That lets
  422. * device driver code (like this routine) unlink queued urbs first,
  423. * if it needs to, since the HC won't work on them at all. So it's
  424. * not possible for page N+1 to overwrite page N, and so on.
  425. *
  426. * That's only for "hard" faults; "soft" faults (unlinks) sometimes
  427. * complete before the HCD can get requests away from hardware,
  428. * though never during cleanup after a hard fault.
  429. */
  430. if (io->status
  431. && (io->status != -ECONNRESET
  432. || status != -ECONNRESET)
  433. && urb->actual_length) {
  434. dev_err(io->dev->bus->controller,
  435. "dev %s ep%d%s scatterlist error %d/%d\n",
  436. io->dev->devpath,
  437. usb_endpoint_num(&urb->ep->desc),
  438. usb_urb_dir_in(urb) ? "in" : "out",
  439. status, io->status);
  440. /* BUG (); */
  441. }
  442. if (io->status == 0 && status && status != -ECONNRESET) {
  443. int i, found, retval;
  444. io->status = status;
  445. /* the previous urbs, and this one, completed already.
  446. * unlink pending urbs so they won't rx/tx bad data.
  447. * careful: unlink can sometimes be synchronous...
  448. */
  449. spin_unlock_irqrestore(&io->lock, flags);
  450. for (i = 0, found = 0; i < io->entries; i++) {
  451. if (!io->urbs[i])
  452. continue;
  453. if (found) {
  454. usb_block_urb(io->urbs[i]);
  455. retval = usb_unlink_urb(io->urbs[i]);
  456. if (retval != -EINPROGRESS &&
  457. retval != -ENODEV &&
  458. retval != -EBUSY &&
  459. retval != -EIDRM)
  460. dev_err(&io->dev->dev,
  461. "%s, unlink --> %d\n",
  462. __func__, retval);
  463. } else if (urb == io->urbs[i])
  464. found = 1;
  465. }
  466. spin_lock_irqsave(&io->lock, flags);
  467. }
  468. /* on the last completion, signal usb_sg_wait() */
  469. io->bytes += urb->actual_length;
  470. io->count--;
  471. if (!io->count)
  472. complete(&io->complete);
  473. spin_unlock_irqrestore(&io->lock, flags);
  474. }
  475. /**
  476. * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
  477. * @io: request block being initialized. until usb_sg_wait() returns,
  478. * treat this as a pointer to an opaque block of memory,
  479. * @dev: the usb device that will send or receive the data
  480. * @pipe: endpoint "pipe" used to transfer the data
  481. * @period: polling rate for interrupt endpoints, in frames or
  482. * (for high speed endpoints) microframes; ignored for bulk
  483. * @sg: scatterlist entries
  484. * @nents: how many entries in the scatterlist
  485. * @length: how many bytes to send from the scatterlist, or zero to
  486. * send every byte identified in the list.
  487. * @mem_flags: SLAB_* flags affecting memory allocations in this call
  488. *
  489. * This initializes a scatter/gather request, allocating resources such as
  490. * I/O mappings and urb memory (except maybe memory used by USB controller
  491. * drivers).
  492. *
  493. * The request must be issued using usb_sg_wait(), which waits for the I/O to
  494. * complete (or to be canceled) and then cleans up all resources allocated by
  495. * usb_sg_init().
  496. *
  497. * The request may be canceled with usb_sg_cancel(), either before or after
  498. * usb_sg_wait() is called.
  499. *
  500. * Return: Zero for success, else a negative errno value.
  501. */
  502. int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
  503. unsigned pipe, unsigned period, struct scatterlist *sg,
  504. int nents, size_t length, gfp_t mem_flags)
  505. {
  506. int i;
  507. int urb_flags;
  508. int use_sg;
  509. if (!io || !dev || !sg
  510. || usb_pipecontrol(pipe)
  511. || usb_pipeisoc(pipe)
  512. || nents <= 0)
  513. return -EINVAL;
  514. spin_lock_init(&io->lock);
  515. io->dev = dev;
  516. io->pipe = pipe;
  517. if (dev->bus->sg_tablesize > 0) {
  518. use_sg = true;
  519. io->entries = 1;
  520. } else {
  521. use_sg = false;
  522. io->entries = nents;
  523. }
  524. /* initialize all the urbs we'll use */
  525. io->urbs = kmalloc_objs(*io->urbs, io->entries, mem_flags);
  526. if (!io->urbs)
  527. goto nomem;
  528. urb_flags = URB_NO_INTERRUPT;
  529. if (usb_pipein(pipe))
  530. urb_flags |= URB_SHORT_NOT_OK;
  531. for_each_sg(sg, sg, io->entries, i) {
  532. struct urb *urb;
  533. unsigned len;
  534. urb = usb_alloc_urb(0, mem_flags);
  535. if (!urb) {
  536. io->entries = i;
  537. goto nomem;
  538. }
  539. io->urbs[i] = urb;
  540. urb->dev = NULL;
  541. urb->pipe = pipe;
  542. urb->interval = period;
  543. urb->transfer_flags = urb_flags;
  544. urb->complete = sg_complete;
  545. urb->context = io;
  546. urb->sg = sg;
  547. if (use_sg) {
  548. /* There is no single transfer buffer */
  549. urb->transfer_buffer = NULL;
  550. urb->num_sgs = nents;
  551. /* A length of zero means transfer the whole sg list */
  552. len = length;
  553. if (len == 0) {
  554. struct scatterlist *sg2;
  555. int j;
  556. for_each_sg(sg, sg2, nents, j)
  557. len += sg2->length;
  558. }
  559. } else {
  560. /*
  561. * Some systems can't use DMA; they use PIO instead.
  562. * For their sakes, transfer_buffer is set whenever
  563. * possible.
  564. */
  565. if (!PageHighMem(sg_page(sg)))
  566. urb->transfer_buffer = sg_virt(sg);
  567. else
  568. urb->transfer_buffer = NULL;
  569. len = sg->length;
  570. if (length) {
  571. len = min_t(size_t, len, length);
  572. length -= len;
  573. if (length == 0)
  574. io->entries = i + 1;
  575. }
  576. }
  577. urb->transfer_buffer_length = len;
  578. }
  579. io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
  580. /* transaction state */
  581. io->count = io->entries;
  582. io->status = 0;
  583. io->bytes = 0;
  584. init_completion(&io->complete);
  585. return 0;
  586. nomem:
  587. sg_clean(io);
  588. return -ENOMEM;
  589. }
  590. EXPORT_SYMBOL_GPL(usb_sg_init);
  591. /**
  592. * usb_sg_wait - synchronously execute scatter/gather request
  593. * @io: request block handle, as initialized with usb_sg_init().
  594. * some fields become accessible when this call returns.
  595. *
  596. * Context: task context, might sleep.
  597. *
  598. * This function blocks until the specified I/O operation completes. It
  599. * leverages the grouping of the related I/O requests to get good transfer
  600. * rates, by queueing the requests. At higher speeds, such queuing can
  601. * significantly improve USB throughput.
  602. *
  603. * There are three kinds of completion for this function.
  604. *
  605. * (1) success, where io->status is zero. The number of io->bytes
  606. * transferred is as requested.
  607. * (2) error, where io->status is a negative errno value. The number
  608. * of io->bytes transferred before the error is usually less
  609. * than requested, and can be nonzero.
  610. * (3) cancellation, a type of error with status -ECONNRESET that
  611. * is initiated by usb_sg_cancel().
  612. *
  613. * When this function returns, all memory allocated through usb_sg_init() or
  614. * this call will have been freed. The request block parameter may still be
  615. * passed to usb_sg_cancel(), or it may be freed. It could also be
  616. * reinitialized and then reused.
  617. *
  618. * Data Transfer Rates:
  619. *
  620. * Bulk transfers are valid for full or high speed endpoints.
  621. * The best full speed data rate is 19 packets of 64 bytes each
  622. * per frame, or 1216 bytes per millisecond.
  623. * The best high speed data rate is 13 packets of 512 bytes each
  624. * per microframe, or 52 KBytes per millisecond.
  625. *
  626. * The reason to use interrupt transfers through this API would most likely
  627. * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
  628. * could be transferred. That capability is less useful for low or full
  629. * speed interrupt endpoints, which allow at most one packet per millisecond,
  630. * of at most 8 or 64 bytes (respectively).
  631. *
  632. * It is not necessary to call this function to reserve bandwidth for devices
  633. * under an xHCI host controller, as the bandwidth is reserved when the
  634. * configuration or interface alt setting is selected.
  635. */
  636. void usb_sg_wait(struct usb_sg_request *io)
  637. {
  638. int i;
  639. int entries = io->entries;
  640. /* queue the urbs. */
  641. spin_lock_irq(&io->lock);
  642. i = 0;
  643. while (i < entries && !io->status) {
  644. int retval;
  645. io->urbs[i]->dev = io->dev;
  646. spin_unlock_irq(&io->lock);
  647. retval = usb_submit_urb(io->urbs[i], GFP_NOIO);
  648. switch (retval) {
  649. /* maybe we retrying will recover */
  650. case -ENXIO: /* hc didn't queue this one */
  651. case -EAGAIN:
  652. case -ENOMEM:
  653. retval = 0;
  654. yield();
  655. break;
  656. /* no error? continue immediately.
  657. *
  658. * NOTE: to work better with UHCI (4K I/O buffer may
  659. * need 3K of TDs) it may be good to limit how many
  660. * URBs are queued at once; N milliseconds?
  661. */
  662. case 0:
  663. ++i;
  664. cpu_relax();
  665. break;
  666. /* fail any uncompleted urbs */
  667. default:
  668. io->urbs[i]->status = retval;
  669. dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
  670. __func__, retval);
  671. usb_sg_cancel(io);
  672. }
  673. spin_lock_irq(&io->lock);
  674. if (retval && (io->status == 0 || io->status == -ECONNRESET))
  675. io->status = retval;
  676. }
  677. io->count -= entries - i;
  678. if (io->count == 0)
  679. complete(&io->complete);
  680. spin_unlock_irq(&io->lock);
  681. /* OK, yes, this could be packaged as non-blocking.
  682. * So could the submit loop above ... but it's easier to
  683. * solve neither problem than to solve both!
  684. */
  685. wait_for_completion(&io->complete);
  686. sg_clean(io);
  687. }
  688. EXPORT_SYMBOL_GPL(usb_sg_wait);
  689. /**
  690. * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
  691. * @io: request block, initialized with usb_sg_init()
  692. *
  693. * This stops a request after it has been started by usb_sg_wait().
  694. * It can also prevents one initialized by usb_sg_init() from starting,
  695. * so that call just frees resources allocated to the request.
  696. */
  697. void usb_sg_cancel(struct usb_sg_request *io)
  698. {
  699. unsigned long flags;
  700. int i, retval;
  701. spin_lock_irqsave(&io->lock, flags);
  702. if (io->status || io->count == 0) {
  703. spin_unlock_irqrestore(&io->lock, flags);
  704. return;
  705. }
  706. /* shut everything down */
  707. io->status = -ECONNRESET;
  708. io->count++; /* Keep the request alive until we're done */
  709. spin_unlock_irqrestore(&io->lock, flags);
  710. for (i = io->entries - 1; i >= 0; --i) {
  711. usb_block_urb(io->urbs[i]);
  712. retval = usb_unlink_urb(io->urbs[i]);
  713. if (retval != -EINPROGRESS
  714. && retval != -ENODEV
  715. && retval != -EBUSY
  716. && retval != -EIDRM)
  717. dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
  718. __func__, retval);
  719. }
  720. spin_lock_irqsave(&io->lock, flags);
  721. io->count--;
  722. if (!io->count)
  723. complete(&io->complete);
  724. spin_unlock_irqrestore(&io->lock, flags);
  725. }
  726. EXPORT_SYMBOL_GPL(usb_sg_cancel);
  727. /*-------------------------------------------------------------------*/
  728. /**
  729. * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
  730. * @dev: the device whose descriptor is being retrieved
  731. * @type: the descriptor type (USB_DT_*)
  732. * @index: the number of the descriptor
  733. * @buf: where to put the descriptor
  734. * @size: how big is "buf"?
  735. *
  736. * Context: task context, might sleep.
  737. *
  738. * Gets a USB descriptor. Convenience functions exist to simplify
  739. * getting some types of descriptors. Use
  740. * usb_get_string() or usb_string() for USB_DT_STRING.
  741. * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
  742. * are part of the device structure.
  743. * In addition to a number of USB-standard descriptors, some
  744. * devices also use class-specific or vendor-specific descriptors.
  745. *
  746. * This call is synchronous, and may not be used in an interrupt context.
  747. *
  748. * Return: The number of bytes received on success, or else the status code
  749. * returned by the underlying usb_control_msg() call.
  750. */
  751. int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  752. unsigned char index, void *buf, int size)
  753. {
  754. int i;
  755. int result;
  756. if (size <= 0) /* No point in asking for no data */
  757. return -EINVAL;
  758. memset(buf, 0, size); /* Make sure we parse really received data */
  759. for (i = 0; i < 3; ++i) {
  760. /* retry on length 0 or error; some devices are flakey */
  761. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  762. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  763. (type << 8) + index, 0, buf, size,
  764. USB_CTRL_GET_TIMEOUT);
  765. if (result <= 0 && result != -ETIMEDOUT)
  766. continue;
  767. if (result > 1 && ((u8 *)buf)[1] != type) {
  768. result = -ENODATA;
  769. continue;
  770. }
  771. break;
  772. }
  773. return result;
  774. }
  775. EXPORT_SYMBOL_GPL(usb_get_descriptor);
  776. /**
  777. * usb_get_string - gets a string descriptor
  778. * @dev: the device whose string descriptor is being retrieved
  779. * @langid: code for language chosen (from string descriptor zero)
  780. * @index: the number of the descriptor
  781. * @buf: where to put the string
  782. * @size: how big is "buf"?
  783. *
  784. * Context: task context, might sleep.
  785. *
  786. * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
  787. * in little-endian byte order).
  788. * The usb_string() function will often be a convenient way to turn
  789. * these strings into kernel-printable form.
  790. *
  791. * Strings may be referenced in device, configuration, interface, or other
  792. * descriptors, and could also be used in vendor-specific ways.
  793. *
  794. * This call is synchronous, and may not be used in an interrupt context.
  795. *
  796. * Return: The number of bytes received on success, or else the status code
  797. * returned by the underlying usb_control_msg() call.
  798. */
  799. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  800. unsigned char index, void *buf, int size)
  801. {
  802. int i;
  803. int result;
  804. if (size <= 0) /* No point in asking for no data */
  805. return -EINVAL;
  806. for (i = 0; i < 3; ++i) {
  807. /* retry on length 0 or stall; some devices are flakey */
  808. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  809. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  810. (USB_DT_STRING << 8) + index, langid, buf, size,
  811. USB_CTRL_GET_TIMEOUT);
  812. if (result == 0 || result == -EPIPE)
  813. continue;
  814. if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
  815. result = -ENODATA;
  816. continue;
  817. }
  818. break;
  819. }
  820. return result;
  821. }
  822. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  823. {
  824. int newlength, oldlength = *length;
  825. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  826. if (!isprint(buf[newlength]) || buf[newlength + 1])
  827. break;
  828. if (newlength > 2) {
  829. buf[0] = newlength;
  830. *length = newlength;
  831. }
  832. }
  833. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  834. unsigned int index, unsigned char *buf)
  835. {
  836. int rc;
  837. /* Try to read the string descriptor by asking for the maximum
  838. * possible number of bytes */
  839. if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
  840. rc = -EIO;
  841. else
  842. rc = usb_get_string(dev, langid, index, buf, 255);
  843. /* If that failed try to read the descriptor length, then
  844. * ask for just that many bytes */
  845. if (rc < 2) {
  846. rc = usb_get_string(dev, langid, index, buf, 2);
  847. if (rc == 2)
  848. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  849. }
  850. if (rc >= 2) {
  851. if (!buf[0] && !buf[1])
  852. usb_try_string_workarounds(buf, &rc);
  853. /* There might be extra junk at the end of the descriptor */
  854. if (buf[0] < rc)
  855. rc = buf[0];
  856. rc = rc - (rc & 1); /* force a multiple of two */
  857. }
  858. if (rc < 2)
  859. rc = (rc < 0 ? rc : -EINVAL);
  860. return rc;
  861. }
  862. static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
  863. {
  864. int err;
  865. if (dev->have_langid)
  866. return 0;
  867. if (dev->string_langid < 0)
  868. return -EPIPE;
  869. err = usb_string_sub(dev, 0, 0, tbuf);
  870. /* If the string was reported but is malformed, default to english
  871. * (0x0409) */
  872. if (err == -ENODATA || (err > 0 && err < 4)) {
  873. dev->string_langid = 0x0409;
  874. dev->have_langid = 1;
  875. dev_err(&dev->dev,
  876. "language id specifier not provided by device, defaulting to English\n");
  877. return 0;
  878. }
  879. /* In case of all other errors, we assume the device is not able to
  880. * deal with strings at all. Set string_langid to -1 in order to
  881. * prevent any string to be retrieved from the device */
  882. if (err < 0) {
  883. dev_info(&dev->dev, "string descriptor 0 read error: %d\n",
  884. err);
  885. dev->string_langid = -1;
  886. return -EPIPE;
  887. }
  888. /* always use the first langid listed */
  889. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  890. dev->have_langid = 1;
  891. dev_dbg(&dev->dev, "default language 0x%04x\n",
  892. dev->string_langid);
  893. return 0;
  894. }
  895. /**
  896. * usb_string - returns UTF-8 version of a string descriptor
  897. * @dev: the device whose string descriptor is being retrieved
  898. * @index: the number of the descriptor
  899. * @buf: where to put the string
  900. * @size: how big is "buf"?
  901. *
  902. * Context: task context, might sleep.
  903. *
  904. * This converts the UTF-16LE encoded strings returned by devices, from
  905. * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
  906. * that are more usable in most kernel contexts. Note that this function
  907. * chooses strings in the first language supported by the device.
  908. *
  909. * This call is synchronous, and may not be used in an interrupt context.
  910. *
  911. * Return: length of the string (>= 0) or usb_control_msg status (< 0).
  912. */
  913. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  914. {
  915. unsigned char *tbuf;
  916. int err;
  917. if (dev->state == USB_STATE_SUSPENDED)
  918. return -EHOSTUNREACH;
  919. if (size <= 0 || !buf)
  920. return -EINVAL;
  921. buf[0] = 0;
  922. if (index <= 0 || index >= 256)
  923. return -EINVAL;
  924. tbuf = kmalloc(256, GFP_NOIO);
  925. if (!tbuf)
  926. return -ENOMEM;
  927. err = usb_get_langid(dev, tbuf);
  928. if (err < 0)
  929. goto errout;
  930. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  931. if (err < 0)
  932. goto errout;
  933. size--; /* leave room for trailing NULL char in output buffer */
  934. err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
  935. UTF16_LITTLE_ENDIAN, buf, size);
  936. buf[err] = 0;
  937. if (tbuf[1] != USB_DT_STRING)
  938. dev_dbg(&dev->dev,
  939. "wrong descriptor type %02x for string %d (\"%s\")\n",
  940. tbuf[1], index, buf);
  941. errout:
  942. kfree(tbuf);
  943. return err;
  944. }
  945. EXPORT_SYMBOL_GPL(usb_string);
  946. /* one UTF-8-encoded 16-bit character has at most three bytes */
  947. #define MAX_USB_STRING_SIZE (127 * 3 + 1)
  948. /**
  949. * usb_cache_string - read a string descriptor and cache it for later use
  950. * @udev: the device whose string descriptor is being read
  951. * @index: the descriptor index
  952. *
  953. * Return: A pointer to a kmalloc'ed buffer containing the descriptor string,
  954. * or %NULL if the index is 0 or the string could not be read.
  955. */
  956. char *usb_cache_string(struct usb_device *udev, int index)
  957. {
  958. char *buf;
  959. char *smallbuf = NULL;
  960. int len;
  961. if (index <= 0)
  962. return NULL;
  963. buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
  964. if (buf) {
  965. len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
  966. if (len > 0) {
  967. smallbuf = kmalloc(++len, GFP_NOIO);
  968. if (!smallbuf)
  969. return buf;
  970. memcpy(smallbuf, buf, len);
  971. }
  972. kfree(buf);
  973. }
  974. return smallbuf;
  975. }
  976. EXPORT_SYMBOL_GPL(usb_cache_string);
  977. /*
  978. * usb_get_device_descriptor - read the device descriptor
  979. * @udev: the device whose device descriptor should be read
  980. *
  981. * Context: task context, might sleep.
  982. *
  983. * Not exported, only for use by the core. If drivers really want to read
  984. * the device descriptor directly, they can call usb_get_descriptor() with
  985. * type = USB_DT_DEVICE and index = 0.
  986. *
  987. * Returns: a pointer to a dynamically allocated usb_device_descriptor
  988. * structure (which the caller must deallocate), or an ERR_PTR value.
  989. */
  990. struct usb_device_descriptor *usb_get_device_descriptor(struct usb_device *udev)
  991. {
  992. struct usb_device_descriptor *desc;
  993. int ret;
  994. desc = kmalloc_obj(*desc, GFP_NOIO);
  995. if (!desc)
  996. return ERR_PTR(-ENOMEM);
  997. ret = usb_get_descriptor(udev, USB_DT_DEVICE, 0, desc, sizeof(*desc));
  998. if (ret == sizeof(*desc))
  999. return desc;
  1000. if (ret >= 0)
  1001. ret = -EMSGSIZE;
  1002. kfree(desc);
  1003. return ERR_PTR(ret);
  1004. }
  1005. /*
  1006. * usb_set_isoch_delay - informs the device of the packet transmit delay
  1007. * @dev: the device whose delay is to be informed
  1008. * Context: task context, might sleep
  1009. *
  1010. * Since this is an optional request, we don't bother if it fails.
  1011. */
  1012. int usb_set_isoch_delay(struct usb_device *dev)
  1013. {
  1014. /* skip hub devices */
  1015. if (dev->descriptor.bDeviceClass == USB_CLASS_HUB)
  1016. return 0;
  1017. /* skip non-SS/non-SSP devices */
  1018. if (dev->speed < USB_SPEED_SUPER)
  1019. return 0;
  1020. return usb_control_msg_send(dev, 0,
  1021. USB_REQ_SET_ISOCH_DELAY,
  1022. USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
  1023. dev->hub_delay, 0, NULL, 0,
  1024. USB_CTRL_SET_TIMEOUT,
  1025. GFP_NOIO);
  1026. }
  1027. /**
  1028. * usb_get_status - issues a GET_STATUS call
  1029. * @dev: the device whose status is being checked
  1030. * @recip: USB_RECIP_*; for device, interface, or endpoint
  1031. * @type: USB_STATUS_TYPE_*; for standard or PTM status types
  1032. * @target: zero (for device), else interface or endpoint number
  1033. * @data: pointer to two bytes of bitmap data
  1034. *
  1035. * Context: task context, might sleep.
  1036. *
  1037. * Returns device, interface, or endpoint status. Normally only of
  1038. * interest to see if the device is self powered, or has enabled the
  1039. * remote wakeup facility; or whether a bulk or interrupt endpoint
  1040. * is halted ("stalled").
  1041. *
  1042. * Bits in these status bitmaps are set using the SET_FEATURE request,
  1043. * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
  1044. * function should be used to clear halt ("stall") status.
  1045. *
  1046. * This call is synchronous, and may not be used in an interrupt context.
  1047. *
  1048. * Returns 0 and the status value in *@data (in host byte order) on success,
  1049. * or else the status code from the underlying usb_control_msg() call.
  1050. */
  1051. int usb_get_status(struct usb_device *dev, int recip, int type, int target,
  1052. void *data)
  1053. {
  1054. int ret;
  1055. void *status;
  1056. int length;
  1057. switch (type) {
  1058. case USB_STATUS_TYPE_STANDARD:
  1059. length = 2;
  1060. break;
  1061. case USB_STATUS_TYPE_PTM:
  1062. if (recip != USB_RECIP_DEVICE)
  1063. return -EINVAL;
  1064. length = 4;
  1065. break;
  1066. default:
  1067. return -EINVAL;
  1068. }
  1069. status = kmalloc(length, GFP_KERNEL);
  1070. if (!status)
  1071. return -ENOMEM;
  1072. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  1073. USB_REQ_GET_STATUS, USB_DIR_IN | recip, USB_STATUS_TYPE_STANDARD,
  1074. target, status, length, USB_CTRL_GET_TIMEOUT);
  1075. switch (ret) {
  1076. case 4:
  1077. if (type != USB_STATUS_TYPE_PTM) {
  1078. ret = -EIO;
  1079. break;
  1080. }
  1081. *(u32 *) data = le32_to_cpu(*(__le32 *) status);
  1082. ret = 0;
  1083. break;
  1084. case 2:
  1085. if (type != USB_STATUS_TYPE_STANDARD) {
  1086. ret = -EIO;
  1087. break;
  1088. }
  1089. *(u16 *) data = le16_to_cpu(*(__le16 *) status);
  1090. ret = 0;
  1091. break;
  1092. default:
  1093. ret = -EIO;
  1094. }
  1095. kfree(status);
  1096. return ret;
  1097. }
  1098. EXPORT_SYMBOL_GPL(usb_get_status);
  1099. /**
  1100. * usb_clear_halt - tells device to clear endpoint halt/stall condition
  1101. * @dev: device whose endpoint is halted
  1102. * @pipe: endpoint "pipe" being cleared
  1103. *
  1104. * Context: task context, might sleep.
  1105. *
  1106. * This is used to clear halt conditions for bulk and interrupt endpoints,
  1107. * as reported by URB completion status. Endpoints that are halted are
  1108. * sometimes referred to as being "stalled". Such endpoints are unable
  1109. * to transmit or receive data until the halt status is cleared. Any URBs
  1110. * queued for such an endpoint should normally be unlinked by the driver
  1111. * before clearing the halt condition, as described in sections 5.7.5
  1112. * and 5.8.5 of the USB 2.0 spec.
  1113. *
  1114. * Note that control and isochronous endpoints don't halt, although control
  1115. * endpoints report "protocol stall" (for unsupported requests) using the
  1116. * same status code used to report a true stall.
  1117. *
  1118. * This call is synchronous, and may not be used in an interrupt context.
  1119. * If a thread in your driver uses this call, make sure your disconnect()
  1120. * method can wait for it to complete.
  1121. *
  1122. * Return: Zero on success, or else the status code returned by the
  1123. * underlying usb_control_msg() call.
  1124. */
  1125. int usb_clear_halt(struct usb_device *dev, int pipe)
  1126. {
  1127. int result;
  1128. int endp = usb_pipeendpoint(pipe);
  1129. if (usb_pipein(pipe))
  1130. endp |= USB_DIR_IN;
  1131. /* we don't care if it wasn't halted first. in fact some devices
  1132. * (like some ibmcam model 1 units) seem to expect hosts to make
  1133. * this request for iso endpoints, which can't halt!
  1134. */
  1135. result = usb_control_msg_send(dev, 0,
  1136. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
  1137. USB_ENDPOINT_HALT, endp, NULL, 0,
  1138. USB_CTRL_SET_TIMEOUT, GFP_NOIO);
  1139. /* don't un-halt or force to DATA0 except on success */
  1140. if (result)
  1141. return result;
  1142. /* NOTE: seems like Microsoft and Apple don't bother verifying
  1143. * the clear "took", so some devices could lock up if you check...
  1144. * such as the Hagiwara FlashGate DUAL. So we won't bother.
  1145. *
  1146. * NOTE: make sure the logic here doesn't diverge much from
  1147. * the copy in usb-storage, for as long as we need two copies.
  1148. */
  1149. usb_reset_endpoint(dev, endp);
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL_GPL(usb_clear_halt);
  1153. static int create_intf_ep_devs(struct usb_interface *intf)
  1154. {
  1155. struct usb_device *udev = interface_to_usbdev(intf);
  1156. struct usb_host_interface *alt = intf->cur_altsetting;
  1157. int i;
  1158. if (intf->ep_devs_created || intf->unregistering)
  1159. return 0;
  1160. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  1161. (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
  1162. intf->ep_devs_created = 1;
  1163. return 0;
  1164. }
  1165. static void remove_intf_ep_devs(struct usb_interface *intf)
  1166. {
  1167. struct usb_host_interface *alt = intf->cur_altsetting;
  1168. int i;
  1169. if (!intf->ep_devs_created)
  1170. return;
  1171. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  1172. usb_remove_ep_devs(&alt->endpoint[i]);
  1173. intf->ep_devs_created = 0;
  1174. }
  1175. /**
  1176. * usb_disable_endpoint -- Disable an endpoint by address
  1177. * @dev: the device whose endpoint is being disabled
  1178. * @epaddr: the endpoint's address. Endpoint number for output,
  1179. * endpoint number + USB_DIR_IN for input
  1180. * @reset_hardware: flag to erase any endpoint state stored in the
  1181. * controller hardware
  1182. *
  1183. * Disables the endpoint for URB submission and nukes all pending URBs.
  1184. * If @reset_hardware is set then also deallocates hcd/hardware state
  1185. * for the endpoint.
  1186. */
  1187. void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
  1188. bool reset_hardware)
  1189. {
  1190. unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
  1191. struct usb_host_endpoint *ep;
  1192. if (!dev)
  1193. return;
  1194. if (usb_endpoint_out(epaddr)) {
  1195. ep = dev->ep_out[epnum];
  1196. if (reset_hardware && epnum != 0)
  1197. dev->ep_out[epnum] = NULL;
  1198. } else {
  1199. ep = dev->ep_in[epnum];
  1200. if (reset_hardware && epnum != 0)
  1201. dev->ep_in[epnum] = NULL;
  1202. }
  1203. if (ep) {
  1204. ep->enabled = 0;
  1205. usb_hcd_flush_endpoint(dev, ep);
  1206. if (reset_hardware)
  1207. usb_hcd_disable_endpoint(dev, ep);
  1208. }
  1209. }
  1210. /**
  1211. * usb_reset_endpoint - Reset an endpoint's state.
  1212. * @dev: the device whose endpoint is to be reset
  1213. * @epaddr: the endpoint's address. Endpoint number for output,
  1214. * endpoint number + USB_DIR_IN for input
  1215. *
  1216. * Resets any host-side endpoint state such as the toggle bit,
  1217. * sequence number or current window.
  1218. */
  1219. void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
  1220. {
  1221. unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
  1222. struct usb_host_endpoint *ep;
  1223. if (usb_endpoint_out(epaddr))
  1224. ep = dev->ep_out[epnum];
  1225. else
  1226. ep = dev->ep_in[epnum];
  1227. if (ep)
  1228. usb_hcd_reset_endpoint(dev, ep);
  1229. }
  1230. EXPORT_SYMBOL_GPL(usb_reset_endpoint);
  1231. /**
  1232. * usb_disable_interface -- Disable all endpoints for an interface
  1233. * @dev: the device whose interface is being disabled
  1234. * @intf: pointer to the interface descriptor
  1235. * @reset_hardware: flag to erase any endpoint state stored in the
  1236. * controller hardware
  1237. *
  1238. * Disables all the endpoints for the interface's current altsetting.
  1239. */
  1240. void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
  1241. bool reset_hardware)
  1242. {
  1243. struct usb_host_interface *alt = intf->cur_altsetting;
  1244. int i;
  1245. for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
  1246. usb_disable_endpoint(dev,
  1247. alt->endpoint[i].desc.bEndpointAddress,
  1248. reset_hardware);
  1249. }
  1250. }
  1251. /*
  1252. * usb_disable_device_endpoints -- Disable all endpoints for a device
  1253. * @dev: the device whose endpoints are being disabled
  1254. * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
  1255. */
  1256. static void usb_disable_device_endpoints(struct usb_device *dev, int skip_ep0)
  1257. {
  1258. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1259. int i;
  1260. if (hcd->driver->check_bandwidth) {
  1261. /* First pass: Cancel URBs, leave endpoint pointers intact. */
  1262. for (i = skip_ep0; i < 16; ++i) {
  1263. usb_disable_endpoint(dev, i, false);
  1264. usb_disable_endpoint(dev, i + USB_DIR_IN, false);
  1265. }
  1266. /* Remove endpoints from the host controller internal state */
  1267. mutex_lock(hcd->bandwidth_mutex);
  1268. usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
  1269. mutex_unlock(hcd->bandwidth_mutex);
  1270. }
  1271. /* Second pass: remove endpoint pointers */
  1272. for (i = skip_ep0; i < 16; ++i) {
  1273. usb_disable_endpoint(dev, i, true);
  1274. usb_disable_endpoint(dev, i + USB_DIR_IN, true);
  1275. }
  1276. }
  1277. /**
  1278. * usb_disable_device - Disable all the endpoints for a USB device
  1279. * @dev: the device whose endpoints are being disabled
  1280. * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
  1281. *
  1282. * Disables all the device's endpoints, potentially including endpoint 0.
  1283. * Deallocates hcd/hardware state for the endpoints (nuking all or most
  1284. * pending urbs) and usbcore state for the interfaces, so that usbcore
  1285. * must usb_set_configuration() before any interfaces could be used.
  1286. */
  1287. void usb_disable_device(struct usb_device *dev, int skip_ep0)
  1288. {
  1289. int i;
  1290. /* getting rid of interfaces will disconnect
  1291. * any drivers bound to them (a key side effect)
  1292. */
  1293. if (dev->actconfig) {
  1294. /*
  1295. * FIXME: In order to avoid self-deadlock involving the
  1296. * bandwidth_mutex, we have to mark all the interfaces
  1297. * before unregistering any of them.
  1298. */
  1299. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
  1300. dev->actconfig->interface[i]->unregistering = 1;
  1301. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1302. struct usb_interface *interface;
  1303. /* remove this interface if it has been registered */
  1304. interface = dev->actconfig->interface[i];
  1305. if (!device_is_registered(&interface->dev))
  1306. continue;
  1307. dev_dbg(&dev->dev, "unregistering interface %s\n",
  1308. dev_name(&interface->dev));
  1309. remove_intf_ep_devs(interface);
  1310. device_del(&interface->dev);
  1311. }
  1312. /* Now that the interfaces are unbound, nobody should
  1313. * try to access them.
  1314. */
  1315. for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
  1316. put_device(&dev->actconfig->interface[i]->dev);
  1317. dev->actconfig->interface[i] = NULL;
  1318. }
  1319. usb_disable_usb2_hardware_lpm(dev);
  1320. usb_unlocked_disable_lpm(dev);
  1321. usb_disable_ltm(dev);
  1322. dev->actconfig = NULL;
  1323. if (dev->state == USB_STATE_CONFIGURED)
  1324. usb_set_device_state(dev, USB_STATE_ADDRESS);
  1325. }
  1326. dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
  1327. skip_ep0 ? "non-ep0" : "all");
  1328. usb_disable_device_endpoints(dev, skip_ep0);
  1329. }
  1330. /**
  1331. * usb_enable_endpoint - Enable an endpoint for USB communications
  1332. * @dev: the device whose interface is being enabled
  1333. * @ep: the endpoint
  1334. * @reset_ep: flag to reset the endpoint state
  1335. *
  1336. * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
  1337. * For control endpoints, both the input and output sides are handled.
  1338. */
  1339. void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
  1340. bool reset_ep)
  1341. {
  1342. int epnum = usb_endpoint_num(&ep->desc);
  1343. int is_out = usb_endpoint_dir_out(&ep->desc);
  1344. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1345. if (reset_ep)
  1346. usb_hcd_reset_endpoint(dev, ep);
  1347. if (is_out || is_control)
  1348. dev->ep_out[epnum] = ep;
  1349. if (!is_out || is_control)
  1350. dev->ep_in[epnum] = ep;
  1351. ep->enabled = 1;
  1352. }
  1353. /**
  1354. * usb_enable_interface - Enable all the endpoints for an interface
  1355. * @dev: the device whose interface is being enabled
  1356. * @intf: pointer to the interface descriptor
  1357. * @reset_eps: flag to reset the endpoints' state
  1358. *
  1359. * Enables all the endpoints for the interface's current altsetting.
  1360. */
  1361. void usb_enable_interface(struct usb_device *dev,
  1362. struct usb_interface *intf, bool reset_eps)
  1363. {
  1364. struct usb_host_interface *alt = intf->cur_altsetting;
  1365. int i;
  1366. for (i = 0; i < alt->desc.bNumEndpoints; ++i)
  1367. usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
  1368. }
  1369. /**
  1370. * usb_set_interface - Makes a particular alternate setting be current
  1371. * @dev: the device whose interface is being updated
  1372. * @interface: the interface being updated
  1373. * @alternate: the setting being chosen.
  1374. *
  1375. * Context: task context, might sleep.
  1376. *
  1377. * This is used to enable data transfers on interfaces that may not
  1378. * be enabled by default. Not all devices support such configurability.
  1379. * Only the driver bound to an interface may change its setting.
  1380. *
  1381. * Within any given configuration, each interface may have several
  1382. * alternative settings. These are often used to control levels of
  1383. * bandwidth consumption. For example, the default setting for a high
  1384. * speed interrupt endpoint may not send more than 64 bytes per microframe,
  1385. * while interrupt transfers of up to 3KBytes per microframe are legal.
  1386. * Also, isochronous endpoints may never be part of an
  1387. * interface's default setting. To access such bandwidth, alternate
  1388. * interface settings must be made current.
  1389. *
  1390. * Note that in the Linux USB subsystem, bandwidth associated with
  1391. * an endpoint in a given alternate setting is not reserved until an URB
  1392. * is submitted that needs that bandwidth. Some other operating systems
  1393. * allocate bandwidth early, when a configuration is chosen.
  1394. *
  1395. * xHCI reserves bandwidth and configures the alternate setting in
  1396. * usb_hcd_alloc_bandwidth(). If it fails the original interface altsetting
  1397. * may be disabled. Drivers cannot rely on any particular alternate
  1398. * setting being in effect after a failure.
  1399. *
  1400. * This call is synchronous, and may not be used in an interrupt context.
  1401. * Also, drivers must not change altsettings while urbs are scheduled for
  1402. * endpoints in that interface; all such urbs must first be completed
  1403. * (perhaps forced by unlinking). If a thread in your driver uses this call,
  1404. * make sure your disconnect() method can wait for it to complete.
  1405. *
  1406. * Return: Zero on success, or else the status code returned by the
  1407. * underlying usb_control_msg() call.
  1408. */
  1409. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  1410. {
  1411. struct usb_interface *iface;
  1412. struct usb_host_interface *alt;
  1413. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1414. int i, ret, manual = 0;
  1415. unsigned int epaddr;
  1416. unsigned int pipe;
  1417. if (dev->state == USB_STATE_SUSPENDED)
  1418. return -EHOSTUNREACH;
  1419. iface = usb_ifnum_to_if(dev, interface);
  1420. if (!iface) {
  1421. dev_dbg(&dev->dev, "selecting invalid interface %d\n",
  1422. interface);
  1423. return -EINVAL;
  1424. }
  1425. if (iface->unregistering)
  1426. return -ENODEV;
  1427. alt = usb_altnum_to_altsetting(iface, alternate);
  1428. if (!alt) {
  1429. dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
  1430. alternate);
  1431. return -EINVAL;
  1432. }
  1433. /*
  1434. * usb3 hosts configure the interface in usb_hcd_alloc_bandwidth,
  1435. * including freeing dropped endpoint ring buffers.
  1436. * Make sure the interface endpoints are flushed before that
  1437. */
  1438. usb_disable_interface(dev, iface, false);
  1439. /* Make sure we have enough bandwidth for this alternate interface.
  1440. * Remove the current alt setting and add the new alt setting.
  1441. */
  1442. mutex_lock(hcd->bandwidth_mutex);
  1443. /* Disable LPM, and re-enable it once the new alt setting is installed,
  1444. * so that the xHCI driver can recalculate the U1/U2 timeouts.
  1445. */
  1446. if (usb_disable_lpm(dev)) {
  1447. dev_err(&iface->dev, "%s Failed to disable LPM\n", __func__);
  1448. mutex_unlock(hcd->bandwidth_mutex);
  1449. return -ENOMEM;
  1450. }
  1451. /* Changing alt-setting also frees any allocated streams */
  1452. for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
  1453. iface->cur_altsetting->endpoint[i].streams = 0;
  1454. ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
  1455. if (ret < 0) {
  1456. dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
  1457. alternate);
  1458. usb_enable_lpm(dev);
  1459. mutex_unlock(hcd->bandwidth_mutex);
  1460. return ret;
  1461. }
  1462. if (dev->quirks & USB_QUIRK_NO_SET_INTF)
  1463. ret = -EPIPE;
  1464. else
  1465. ret = usb_control_msg_send(dev, 0,
  1466. USB_REQ_SET_INTERFACE,
  1467. USB_RECIP_INTERFACE, alternate,
  1468. interface, NULL, 0, 5000,
  1469. GFP_NOIO);
  1470. /* 9.4.10 says devices don't need this and are free to STALL the
  1471. * request if the interface only has one alternate setting.
  1472. */
  1473. if (ret == -EPIPE && iface->num_altsetting == 1) {
  1474. dev_dbg(&dev->dev,
  1475. "manual set_interface for iface %d, alt %d\n",
  1476. interface, alternate);
  1477. manual = 1;
  1478. } else if (ret) {
  1479. /* Re-instate the old alt setting */
  1480. usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
  1481. usb_enable_lpm(dev);
  1482. mutex_unlock(hcd->bandwidth_mutex);
  1483. return ret;
  1484. }
  1485. mutex_unlock(hcd->bandwidth_mutex);
  1486. /* FIXME drivers shouldn't need to replicate/bugfix the logic here
  1487. * when they implement async or easily-killable versions of this or
  1488. * other "should-be-internal" functions (like clear_halt).
  1489. * should hcd+usbcore postprocess control requests?
  1490. */
  1491. /* prevent submissions using previous endpoint settings */
  1492. if (iface->cur_altsetting != alt) {
  1493. remove_intf_ep_devs(iface);
  1494. usb_remove_sysfs_intf_files(iface);
  1495. }
  1496. usb_disable_interface(dev, iface, true);
  1497. iface->cur_altsetting = alt;
  1498. /* Now that the interface is installed, re-enable LPM. */
  1499. usb_unlocked_enable_lpm(dev);
  1500. /* If the interface only has one altsetting and the device didn't
  1501. * accept the request, we attempt to carry out the equivalent action
  1502. * by manually clearing the HALT feature for each endpoint in the
  1503. * new altsetting.
  1504. */
  1505. if (manual) {
  1506. for (i = 0; i < alt->desc.bNumEndpoints; i++) {
  1507. epaddr = alt->endpoint[i].desc.bEndpointAddress;
  1508. pipe = __create_pipe(dev,
  1509. USB_ENDPOINT_NUMBER_MASK & epaddr) |
  1510. (usb_endpoint_out(epaddr) ?
  1511. USB_DIR_OUT : USB_DIR_IN);
  1512. usb_clear_halt(dev, pipe);
  1513. }
  1514. }
  1515. /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
  1516. *
  1517. * Note:
  1518. * Despite EP0 is always present in all interfaces/AS, the list of
  1519. * endpoints from the descriptor does not contain EP0. Due to its
  1520. * omnipresence one might expect EP0 being considered "affected" by
  1521. * any SetInterface request and hence assume toggles need to be reset.
  1522. * However, EP0 toggles are re-synced for every individual transfer
  1523. * during the SETUP stage - hence EP0 toggles are "don't care" here.
  1524. * (Likewise, EP0 never "halts" on well designed devices.)
  1525. */
  1526. usb_enable_interface(dev, iface, true);
  1527. if (device_is_registered(&iface->dev)) {
  1528. usb_create_sysfs_intf_files(iface);
  1529. create_intf_ep_devs(iface);
  1530. }
  1531. return 0;
  1532. }
  1533. EXPORT_SYMBOL_GPL(usb_set_interface);
  1534. /**
  1535. * usb_reset_configuration - lightweight device reset
  1536. * @dev: the device whose configuration is being reset
  1537. *
  1538. * This issues a standard SET_CONFIGURATION request to the device using
  1539. * the current configuration. The effect is to reset most USB-related
  1540. * state in the device, including interface altsettings (reset to zero),
  1541. * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
  1542. * endpoints). Other usbcore state is unchanged, including bindings of
  1543. * usb device drivers to interfaces.
  1544. *
  1545. * Because this affects multiple interfaces, avoid using this with composite
  1546. * (multi-interface) devices. Instead, the driver for each interface may
  1547. * use usb_set_interface() on the interfaces it claims. Be careful though;
  1548. * some devices don't support the SET_INTERFACE request, and others won't
  1549. * reset all the interface state (notably endpoint state). Resetting the whole
  1550. * configuration would affect other drivers' interfaces.
  1551. *
  1552. * The caller must own the device lock.
  1553. *
  1554. * Return: Zero on success, else a negative error code.
  1555. *
  1556. * If this routine fails the device will probably be in an unusable state
  1557. * with endpoints disabled, and interfaces only partially enabled.
  1558. */
  1559. int usb_reset_configuration(struct usb_device *dev)
  1560. {
  1561. int i, retval;
  1562. struct usb_host_config *config;
  1563. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1564. if (dev->state == USB_STATE_SUSPENDED)
  1565. return -EHOSTUNREACH;
  1566. /* caller must have locked the device and must own
  1567. * the usb bus readlock (so driver bindings are stable);
  1568. * calls during probe() are fine
  1569. */
  1570. usb_disable_device_endpoints(dev, 1); /* skip ep0*/
  1571. config = dev->actconfig;
  1572. retval = 0;
  1573. mutex_lock(hcd->bandwidth_mutex);
  1574. /* Disable LPM, and re-enable it once the configuration is reset, so
  1575. * that the xHCI driver can recalculate the U1/U2 timeouts.
  1576. */
  1577. if (usb_disable_lpm(dev)) {
  1578. dev_err(&dev->dev, "%s Failed to disable LPM\n", __func__);
  1579. mutex_unlock(hcd->bandwidth_mutex);
  1580. return -ENOMEM;
  1581. }
  1582. /* xHCI adds all endpoints in usb_hcd_alloc_bandwidth */
  1583. retval = usb_hcd_alloc_bandwidth(dev, config, NULL, NULL);
  1584. if (retval < 0) {
  1585. usb_enable_lpm(dev);
  1586. mutex_unlock(hcd->bandwidth_mutex);
  1587. return retval;
  1588. }
  1589. retval = usb_control_msg_send(dev, 0, USB_REQ_SET_CONFIGURATION, 0,
  1590. config->desc.bConfigurationValue, 0,
  1591. NULL, 0, USB_CTRL_SET_TIMEOUT,
  1592. GFP_NOIO);
  1593. if (retval) {
  1594. usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
  1595. usb_enable_lpm(dev);
  1596. mutex_unlock(hcd->bandwidth_mutex);
  1597. return retval;
  1598. }
  1599. mutex_unlock(hcd->bandwidth_mutex);
  1600. /* re-init hc/hcd interface/endpoint state */
  1601. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  1602. struct usb_interface *intf = config->interface[i];
  1603. struct usb_host_interface *alt;
  1604. alt = usb_altnum_to_altsetting(intf, 0);
  1605. /* No altsetting 0? We'll assume the first altsetting.
  1606. * We could use a GetInterface call, but if a device is
  1607. * so non-compliant that it doesn't have altsetting 0
  1608. * then I wouldn't trust its reply anyway.
  1609. */
  1610. if (!alt)
  1611. alt = &intf->altsetting[0];
  1612. if (alt != intf->cur_altsetting) {
  1613. remove_intf_ep_devs(intf);
  1614. usb_remove_sysfs_intf_files(intf);
  1615. }
  1616. intf->cur_altsetting = alt;
  1617. usb_enable_interface(dev, intf, true);
  1618. if (device_is_registered(&intf->dev)) {
  1619. usb_create_sysfs_intf_files(intf);
  1620. create_intf_ep_devs(intf);
  1621. }
  1622. }
  1623. /* Now that the interfaces are installed, re-enable LPM. */
  1624. usb_unlocked_enable_lpm(dev);
  1625. return 0;
  1626. }
  1627. EXPORT_SYMBOL_GPL(usb_reset_configuration);
  1628. static void usb_release_interface(struct device *dev)
  1629. {
  1630. struct usb_interface *intf = to_usb_interface(dev);
  1631. struct usb_interface_cache *intfc =
  1632. altsetting_to_usb_interface_cache(intf->altsetting);
  1633. kref_put(&intfc->ref, usb_release_interface_cache);
  1634. usb_put_dev(interface_to_usbdev(intf));
  1635. of_node_put(dev->of_node);
  1636. kfree(intf);
  1637. }
  1638. /*
  1639. * usb_deauthorize_interface - deauthorize an USB interface
  1640. *
  1641. * @intf: USB interface structure
  1642. */
  1643. void usb_deauthorize_interface(struct usb_interface *intf)
  1644. {
  1645. struct device *dev = &intf->dev;
  1646. device_lock(dev->parent);
  1647. if (intf->authorized) {
  1648. device_lock(dev);
  1649. intf->authorized = 0;
  1650. device_unlock(dev);
  1651. usb_forced_unbind_intf(intf);
  1652. }
  1653. device_unlock(dev->parent);
  1654. }
  1655. /*
  1656. * usb_authorize_interface - authorize an USB interface
  1657. *
  1658. * @intf: USB interface structure
  1659. */
  1660. void usb_authorize_interface(struct usb_interface *intf)
  1661. {
  1662. struct device *dev = &intf->dev;
  1663. if (!intf->authorized) {
  1664. device_lock(dev);
  1665. intf->authorized = 1; /* authorize interface */
  1666. device_unlock(dev);
  1667. }
  1668. }
  1669. static int usb_if_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1670. {
  1671. const struct usb_device *usb_dev;
  1672. const struct usb_interface *intf;
  1673. const struct usb_host_interface *alt;
  1674. intf = to_usb_interface(dev);
  1675. usb_dev = interface_to_usbdev(intf);
  1676. alt = intf->cur_altsetting;
  1677. if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
  1678. alt->desc.bInterfaceClass,
  1679. alt->desc.bInterfaceSubClass,
  1680. alt->desc.bInterfaceProtocol))
  1681. return -ENOMEM;
  1682. if (add_uevent_var(env,
  1683. "MODALIAS=usb:"
  1684. "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
  1685. le16_to_cpu(usb_dev->descriptor.idVendor),
  1686. le16_to_cpu(usb_dev->descriptor.idProduct),
  1687. le16_to_cpu(usb_dev->descriptor.bcdDevice),
  1688. usb_dev->descriptor.bDeviceClass,
  1689. usb_dev->descriptor.bDeviceSubClass,
  1690. usb_dev->descriptor.bDeviceProtocol,
  1691. alt->desc.bInterfaceClass,
  1692. alt->desc.bInterfaceSubClass,
  1693. alt->desc.bInterfaceProtocol,
  1694. alt->desc.bInterfaceNumber))
  1695. return -ENOMEM;
  1696. return 0;
  1697. }
  1698. const struct device_type usb_if_device_type = {
  1699. .name = "usb_interface",
  1700. .release = usb_release_interface,
  1701. .uevent = usb_if_uevent,
  1702. };
  1703. static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
  1704. struct usb_host_config *config,
  1705. u8 inum)
  1706. {
  1707. struct usb_interface_assoc_descriptor *retval = NULL;
  1708. struct usb_interface_assoc_descriptor *intf_assoc;
  1709. int first_intf;
  1710. int last_intf;
  1711. int i;
  1712. for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
  1713. intf_assoc = config->intf_assoc[i];
  1714. if (intf_assoc->bInterfaceCount == 0)
  1715. continue;
  1716. first_intf = intf_assoc->bFirstInterface;
  1717. last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
  1718. if (inum >= first_intf && inum <= last_intf) {
  1719. if (!retval)
  1720. retval = intf_assoc;
  1721. else
  1722. dev_err(&dev->dev, "Interface #%d referenced"
  1723. " by multiple IADs\n", inum);
  1724. }
  1725. }
  1726. return retval;
  1727. }
  1728. /*
  1729. * Internal function to queue a device reset
  1730. * See usb_queue_reset_device() for more details
  1731. */
  1732. static void __usb_queue_reset_device(struct work_struct *ws)
  1733. {
  1734. int rc;
  1735. struct usb_interface *iface =
  1736. container_of(ws, struct usb_interface, reset_ws);
  1737. struct usb_device *udev = interface_to_usbdev(iface);
  1738. rc = usb_lock_device_for_reset(udev, iface);
  1739. if (rc >= 0) {
  1740. usb_reset_device(udev);
  1741. usb_unlock_device(udev);
  1742. }
  1743. usb_put_intf(iface); /* Undo _get_ in usb_queue_reset_device() */
  1744. }
  1745. /*
  1746. * Internal function to set the wireless_status sysfs attribute
  1747. * See usb_set_wireless_status() for more details
  1748. */
  1749. static void __usb_wireless_status_intf(struct work_struct *ws)
  1750. {
  1751. struct usb_interface *iface =
  1752. container_of(ws, struct usb_interface, wireless_status_work);
  1753. device_lock(iface->dev.parent);
  1754. if (iface->sysfs_files_created)
  1755. usb_update_wireless_status_attr(iface);
  1756. device_unlock(iface->dev.parent);
  1757. usb_put_intf(iface); /* Undo _get_ in usb_set_wireless_status() */
  1758. }
  1759. /**
  1760. * usb_set_wireless_status - sets the wireless_status struct member
  1761. * @iface: the interface to modify
  1762. * @status: the new wireless status
  1763. *
  1764. * Set the wireless_status struct member to the new value, and emit
  1765. * sysfs changes as necessary.
  1766. *
  1767. * Returns: 0 on success, -EALREADY if already set.
  1768. */
  1769. int usb_set_wireless_status(struct usb_interface *iface,
  1770. enum usb_wireless_status status)
  1771. {
  1772. if (iface->wireless_status == status)
  1773. return -EALREADY;
  1774. usb_get_intf(iface);
  1775. iface->wireless_status = status;
  1776. schedule_work(&iface->wireless_status_work);
  1777. return 0;
  1778. }
  1779. EXPORT_SYMBOL_GPL(usb_set_wireless_status);
  1780. /*
  1781. * usb_set_configuration - Makes a particular device setting be current
  1782. * @dev: the device whose configuration is being updated
  1783. * @configuration: the configuration being chosen.
  1784. *
  1785. * Context: task context, might sleep. Caller holds device lock.
  1786. *
  1787. * This is used to enable non-default device modes. Not all devices
  1788. * use this kind of configurability; many devices only have one
  1789. * configuration.
  1790. *
  1791. * @configuration is the value of the configuration to be installed.
  1792. * According to the USB spec (e.g. section 9.1.1.5), configuration values
  1793. * must be non-zero; a value of zero indicates that the device in
  1794. * unconfigured. However some devices erroneously use 0 as one of their
  1795. * configuration values. To help manage such devices, this routine will
  1796. * accept @configuration = -1 as indicating the device should be put in
  1797. * an unconfigured state.
  1798. *
  1799. * USB device configurations may affect Linux interoperability,
  1800. * power consumption and the functionality available. For example,
  1801. * the default configuration is limited to using 100mA of bus power,
  1802. * so that when certain device functionality requires more power,
  1803. * and the device is bus powered, that functionality should be in some
  1804. * non-default device configuration. Other device modes may also be
  1805. * reflected as configuration options, such as whether two ISDN
  1806. * channels are available independently; and choosing between open
  1807. * standard device protocols (like CDC) or proprietary ones.
  1808. *
  1809. * Note that a non-authorized device (dev->authorized == 0) will only
  1810. * be put in unconfigured mode.
  1811. *
  1812. * Note that USB has an additional level of device configurability,
  1813. * associated with interfaces. That configurability is accessed using
  1814. * usb_set_interface().
  1815. *
  1816. * This call is synchronous. The calling context must be able to sleep,
  1817. * must own the device lock, and must not hold the driver model's USB
  1818. * bus mutex; usb interface driver probe() methods cannot use this routine.
  1819. *
  1820. * Returns zero on success, or else the status code returned by the
  1821. * underlying call that failed. On successful completion, each interface
  1822. * in the original device configuration has been destroyed, and each one
  1823. * in the new configuration has been probed by all relevant usb device
  1824. * drivers currently known to the kernel.
  1825. */
  1826. int usb_set_configuration(struct usb_device *dev, int configuration)
  1827. {
  1828. int i, ret;
  1829. struct usb_host_config *cp = NULL;
  1830. struct usb_interface **new_interfaces = NULL;
  1831. struct usb_hcd *hcd = bus_to_hcd(dev->bus);
  1832. int n, nintf;
  1833. if (dev->authorized == 0 || configuration == -1)
  1834. configuration = 0;
  1835. else {
  1836. for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
  1837. if (dev->config[i].desc.bConfigurationValue ==
  1838. configuration) {
  1839. cp = &dev->config[i];
  1840. break;
  1841. }
  1842. }
  1843. }
  1844. if ((!cp && configuration != 0))
  1845. return -EINVAL;
  1846. /* The USB spec says configuration 0 means unconfigured.
  1847. * But if a device includes a configuration numbered 0,
  1848. * we will accept it as a correctly configured state.
  1849. * Use -1 if you really want to unconfigure the device.
  1850. */
  1851. if (cp && configuration == 0)
  1852. dev_warn(&dev->dev, "config 0 descriptor??\n");
  1853. /* Allocate memory for new interfaces before doing anything else,
  1854. * so that if we run out then nothing will have changed. */
  1855. n = nintf = 0;
  1856. if (cp) {
  1857. nintf = cp->desc.bNumInterfaces;
  1858. new_interfaces = kmalloc_objs(*new_interfaces, nintf, GFP_NOIO);
  1859. if (!new_interfaces)
  1860. return -ENOMEM;
  1861. for (; n < nintf; ++n) {
  1862. new_interfaces[n] = kzalloc_obj(struct usb_interface,
  1863. GFP_NOIO);
  1864. if (!new_interfaces[n]) {
  1865. ret = -ENOMEM;
  1866. free_interfaces:
  1867. while (--n >= 0)
  1868. kfree(new_interfaces[n]);
  1869. kfree(new_interfaces);
  1870. return ret;
  1871. }
  1872. }
  1873. i = dev->bus_mA - usb_get_max_power(dev, cp);
  1874. if (i < 0)
  1875. dev_warn(&dev->dev, "new config #%d exceeds power "
  1876. "limit by %dmA\n",
  1877. configuration, -i);
  1878. }
  1879. /* Wake up the device so we can send it the Set-Config request */
  1880. ret = usb_autoresume_device(dev);
  1881. if (ret)
  1882. goto free_interfaces;
  1883. /* if it's already configured, clear out old state first.
  1884. * getting rid of old interfaces means unbinding their drivers.
  1885. */
  1886. if (dev->state != USB_STATE_ADDRESS)
  1887. usb_disable_device(dev, 1); /* Skip ep0 */
  1888. /* Get rid of pending async Set-Config requests for this device */
  1889. cancel_async_set_config(dev);
  1890. /* Make sure we have bandwidth (and available HCD resources) for this
  1891. * configuration. Remove endpoints from the schedule if we're dropping
  1892. * this configuration to set configuration 0. After this point, the
  1893. * host controller will not allow submissions to dropped endpoints. If
  1894. * this call fails, the device state is unchanged.
  1895. */
  1896. mutex_lock(hcd->bandwidth_mutex);
  1897. /* Disable LPM, and re-enable it once the new configuration is
  1898. * installed, so that the xHCI driver can recalculate the U1/U2
  1899. * timeouts.
  1900. */
  1901. if (dev->actconfig && usb_disable_lpm(dev)) {
  1902. dev_err(&dev->dev, "%s Failed to disable LPM\n", __func__);
  1903. mutex_unlock(hcd->bandwidth_mutex);
  1904. ret = -ENOMEM;
  1905. goto free_interfaces;
  1906. }
  1907. ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
  1908. if (ret < 0) {
  1909. if (dev->actconfig)
  1910. usb_enable_lpm(dev);
  1911. mutex_unlock(hcd->bandwidth_mutex);
  1912. usb_autosuspend_device(dev);
  1913. goto free_interfaces;
  1914. }
  1915. /*
  1916. * Initialize the new interface structures and the
  1917. * hc/hcd/usbcore interface/endpoint state.
  1918. */
  1919. for (i = 0; i < nintf; ++i) {
  1920. struct usb_interface_cache *intfc;
  1921. struct usb_interface *intf;
  1922. struct usb_host_interface *alt;
  1923. u8 ifnum;
  1924. cp->interface[i] = intf = new_interfaces[i];
  1925. intfc = cp->intf_cache[i];
  1926. intf->altsetting = intfc->altsetting;
  1927. intf->num_altsetting = intfc->num_altsetting;
  1928. intf->authorized = !!HCD_INTF_AUTHORIZED(hcd);
  1929. kref_get(&intfc->ref);
  1930. alt = usb_altnum_to_altsetting(intf, 0);
  1931. /* No altsetting 0? We'll assume the first altsetting.
  1932. * We could use a GetInterface call, but if a device is
  1933. * so non-compliant that it doesn't have altsetting 0
  1934. * then I wouldn't trust its reply anyway.
  1935. */
  1936. if (!alt)
  1937. alt = &intf->altsetting[0];
  1938. ifnum = alt->desc.bInterfaceNumber;
  1939. intf->intf_assoc = find_iad(dev, cp, ifnum);
  1940. intf->cur_altsetting = alt;
  1941. usb_enable_interface(dev, intf, true);
  1942. intf->dev.parent = &dev->dev;
  1943. if (usb_of_has_combined_node(dev)) {
  1944. device_set_of_node_from_dev(&intf->dev, &dev->dev);
  1945. } else {
  1946. intf->dev.of_node = usb_of_get_interface_node(dev,
  1947. configuration, ifnum);
  1948. }
  1949. ACPI_COMPANION_SET(&intf->dev, ACPI_COMPANION(&dev->dev));
  1950. intf->dev.driver = NULL;
  1951. intf->dev.bus = &usb_bus_type;
  1952. intf->dev.type = &usb_if_device_type;
  1953. intf->dev.groups = usb_interface_groups;
  1954. INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
  1955. INIT_WORK(&intf->wireless_status_work, __usb_wireless_status_intf);
  1956. intf->minor = -1;
  1957. device_initialize(&intf->dev);
  1958. pm_runtime_no_callbacks(&intf->dev);
  1959. dev_set_name(&intf->dev, "%d-%s:%d.%d", dev->bus->busnum,
  1960. dev->devpath, configuration, ifnum);
  1961. usb_get_dev(dev);
  1962. }
  1963. kfree(new_interfaces);
  1964. ret = usb_control_msg_send(dev, 0, USB_REQ_SET_CONFIGURATION, 0,
  1965. configuration, 0, NULL, 0,
  1966. USB_CTRL_SET_TIMEOUT, GFP_NOIO);
  1967. if (ret && cp) {
  1968. /*
  1969. * All the old state is gone, so what else can we do?
  1970. * The device is probably useless now anyway.
  1971. */
  1972. usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
  1973. for (i = 0; i < nintf; ++i) {
  1974. usb_disable_interface(dev, cp->interface[i], true);
  1975. put_device(&cp->interface[i]->dev);
  1976. cp->interface[i] = NULL;
  1977. }
  1978. cp = NULL;
  1979. }
  1980. dev->actconfig = cp;
  1981. mutex_unlock(hcd->bandwidth_mutex);
  1982. if (!cp) {
  1983. usb_set_device_state(dev, USB_STATE_ADDRESS);
  1984. /* Leave LPM disabled while the device is unconfigured. */
  1985. usb_autosuspend_device(dev);
  1986. return ret;
  1987. }
  1988. usb_set_device_state(dev, USB_STATE_CONFIGURED);
  1989. if (cp->string == NULL &&
  1990. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  1991. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  1992. /* Now that the interfaces are installed, re-enable LPM. */
  1993. usb_unlocked_enable_lpm(dev);
  1994. /* Enable LTM if it was turned off by usb_disable_device. */
  1995. usb_enable_ltm(dev);
  1996. /* Now that all the interfaces are set up, register them
  1997. * to trigger binding of drivers to interfaces. probe()
  1998. * routines may install different altsettings and may
  1999. * claim() any interfaces not yet bound. Many class drivers
  2000. * need that: CDC, audio, video, etc.
  2001. */
  2002. for (i = 0; i < nintf; ++i) {
  2003. struct usb_interface *intf = cp->interface[i];
  2004. if (intf->dev.of_node &&
  2005. !of_device_is_available(intf->dev.of_node)) {
  2006. dev_info(&dev->dev, "skipping disabled interface %d\n",
  2007. intf->cur_altsetting->desc.bInterfaceNumber);
  2008. continue;
  2009. }
  2010. dev_dbg(&dev->dev,
  2011. "adding %s (config #%d, interface %d)\n",
  2012. dev_name(&intf->dev), configuration,
  2013. intf->cur_altsetting->desc.bInterfaceNumber);
  2014. device_enable_async_suspend(&intf->dev);
  2015. ret = device_add(&intf->dev);
  2016. if (ret != 0) {
  2017. dev_err(&dev->dev, "device_add(%s) --> %d\n",
  2018. dev_name(&intf->dev), ret);
  2019. continue;
  2020. }
  2021. create_intf_ep_devs(intf);
  2022. }
  2023. usb_autosuspend_device(dev);
  2024. return 0;
  2025. }
  2026. EXPORT_SYMBOL_GPL(usb_set_configuration);
  2027. static LIST_HEAD(set_config_list);
  2028. static DEFINE_SPINLOCK(set_config_lock);
  2029. struct set_config_request {
  2030. struct usb_device *udev;
  2031. int config;
  2032. struct work_struct work;
  2033. struct list_head node;
  2034. };
  2035. /* Worker routine for usb_driver_set_configuration() */
  2036. static void driver_set_config_work(struct work_struct *work)
  2037. {
  2038. struct set_config_request *req =
  2039. container_of(work, struct set_config_request, work);
  2040. struct usb_device *udev = req->udev;
  2041. usb_lock_device(udev);
  2042. spin_lock(&set_config_lock);
  2043. list_del(&req->node);
  2044. spin_unlock(&set_config_lock);
  2045. if (req->config >= -1) /* Is req still valid? */
  2046. usb_set_configuration(udev, req->config);
  2047. usb_unlock_device(udev);
  2048. usb_put_dev(udev);
  2049. kfree(req);
  2050. }
  2051. /* Cancel pending Set-Config requests for a device whose configuration
  2052. * was just changed
  2053. */
  2054. static void cancel_async_set_config(struct usb_device *udev)
  2055. {
  2056. struct set_config_request *req;
  2057. spin_lock(&set_config_lock);
  2058. list_for_each_entry(req, &set_config_list, node) {
  2059. if (req->udev == udev)
  2060. req->config = -999; /* Mark as cancelled */
  2061. }
  2062. spin_unlock(&set_config_lock);
  2063. }
  2064. /**
  2065. * usb_driver_set_configuration - Provide a way for drivers to change device configurations
  2066. * @udev: the device whose configuration is being updated
  2067. * @config: the configuration being chosen.
  2068. * Context: In process context, must be able to sleep
  2069. *
  2070. * Device interface drivers are not allowed to change device configurations.
  2071. * This is because changing configurations will destroy the interface the
  2072. * driver is bound to and create new ones; it would be like a floppy-disk
  2073. * driver telling the computer to replace the floppy-disk drive with a
  2074. * tape drive!
  2075. *
  2076. * Still, in certain specialized circumstances the need may arise. This
  2077. * routine gets around the normal restrictions by using a work thread to
  2078. * submit the change-config request.
  2079. *
  2080. * Return: 0 if the request was successfully queued, error code otherwise.
  2081. * The caller has no way to know whether the queued request will eventually
  2082. * succeed.
  2083. */
  2084. int usb_driver_set_configuration(struct usb_device *udev, int config)
  2085. {
  2086. struct set_config_request *req;
  2087. req = kmalloc_obj(*req);
  2088. if (!req)
  2089. return -ENOMEM;
  2090. req->udev = udev;
  2091. req->config = config;
  2092. INIT_WORK(&req->work, driver_set_config_work);
  2093. spin_lock(&set_config_lock);
  2094. list_add(&req->node, &set_config_list);
  2095. spin_unlock(&set_config_lock);
  2096. usb_get_dev(udev);
  2097. schedule_work(&req->work);
  2098. return 0;
  2099. }
  2100. EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
  2101. /**
  2102. * cdc_parse_cdc_header - parse the extra headers present in CDC devices
  2103. * @hdr: the place to put the results of the parsing
  2104. * @intf: the interface for which parsing is requested
  2105. * @buffer: pointer to the extra headers to be parsed
  2106. * @buflen: length of the extra headers
  2107. *
  2108. * This evaluates the extra headers present in CDC devices which
  2109. * bind the interfaces for data and control and provide details
  2110. * about the capabilities of the device.
  2111. *
  2112. * Return: number of descriptors parsed or -EINVAL
  2113. * if the header is contradictory beyond salvage
  2114. */
  2115. int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr,
  2116. struct usb_interface *intf,
  2117. u8 *buffer,
  2118. int buflen)
  2119. {
  2120. /* duplicates are ignored */
  2121. struct usb_cdc_union_desc *union_header = NULL;
  2122. /* duplicates are not tolerated */
  2123. struct usb_cdc_header_desc *header = NULL;
  2124. struct usb_cdc_ether_desc *ether = NULL;
  2125. struct usb_cdc_mdlm_detail_desc *detail = NULL;
  2126. struct usb_cdc_mdlm_desc *desc = NULL;
  2127. unsigned int elength;
  2128. int cnt = 0;
  2129. memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header));
  2130. hdr->phonet_magic_present = false;
  2131. while (buflen > 0) {
  2132. elength = buffer[0];
  2133. if (!elength) {
  2134. dev_err(&intf->dev, "skipping garbage byte\n");
  2135. elength = 1;
  2136. goto next_desc;
  2137. }
  2138. if ((buflen < elength) || (elength < 3)) {
  2139. dev_err(&intf->dev, "invalid descriptor buffer length\n");
  2140. break;
  2141. }
  2142. if (buffer[1] != USB_DT_CS_INTERFACE) {
  2143. dev_err(&intf->dev, "skipping garbage\n");
  2144. goto next_desc;
  2145. }
  2146. switch (buffer[2]) {
  2147. case USB_CDC_UNION_TYPE: /* we've found it */
  2148. if (elength < sizeof(struct usb_cdc_union_desc))
  2149. goto next_desc;
  2150. if (union_header) {
  2151. dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
  2152. goto next_desc;
  2153. }
  2154. union_header = (struct usb_cdc_union_desc *)buffer;
  2155. break;
  2156. case USB_CDC_COUNTRY_TYPE:
  2157. if (elength < sizeof(struct usb_cdc_country_functional_desc))
  2158. goto next_desc;
  2159. hdr->usb_cdc_country_functional_desc =
  2160. (struct usb_cdc_country_functional_desc *)buffer;
  2161. break;
  2162. case USB_CDC_HEADER_TYPE:
  2163. if (elength != sizeof(struct usb_cdc_header_desc))
  2164. goto next_desc;
  2165. if (header)
  2166. return -EINVAL;
  2167. header = (struct usb_cdc_header_desc *)buffer;
  2168. break;
  2169. case USB_CDC_ACM_TYPE:
  2170. if (elength < sizeof(struct usb_cdc_acm_descriptor))
  2171. goto next_desc;
  2172. hdr->usb_cdc_acm_descriptor =
  2173. (struct usb_cdc_acm_descriptor *)buffer;
  2174. break;
  2175. case USB_CDC_ETHERNET_TYPE:
  2176. if (elength != sizeof(struct usb_cdc_ether_desc))
  2177. goto next_desc;
  2178. if (ether)
  2179. return -EINVAL;
  2180. ether = (struct usb_cdc_ether_desc *)buffer;
  2181. break;
  2182. case USB_CDC_CALL_MANAGEMENT_TYPE:
  2183. if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor))
  2184. goto next_desc;
  2185. hdr->usb_cdc_call_mgmt_descriptor =
  2186. (struct usb_cdc_call_mgmt_descriptor *)buffer;
  2187. break;
  2188. case USB_CDC_DMM_TYPE:
  2189. if (elength < sizeof(struct usb_cdc_dmm_desc))
  2190. goto next_desc;
  2191. hdr->usb_cdc_dmm_desc =
  2192. (struct usb_cdc_dmm_desc *)buffer;
  2193. break;
  2194. case USB_CDC_MDLM_TYPE:
  2195. if (elength < sizeof(struct usb_cdc_mdlm_desc))
  2196. goto next_desc;
  2197. if (desc)
  2198. return -EINVAL;
  2199. desc = (struct usb_cdc_mdlm_desc *)buffer;
  2200. break;
  2201. case USB_CDC_MDLM_DETAIL_TYPE:
  2202. if (elength < sizeof(struct usb_cdc_mdlm_detail_desc))
  2203. goto next_desc;
  2204. if (detail)
  2205. return -EINVAL;
  2206. detail = (struct usb_cdc_mdlm_detail_desc *)buffer;
  2207. break;
  2208. case USB_CDC_NCM_TYPE:
  2209. if (elength < sizeof(struct usb_cdc_ncm_desc))
  2210. goto next_desc;
  2211. hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer;
  2212. break;
  2213. case USB_CDC_MBIM_TYPE:
  2214. if (elength < sizeof(struct usb_cdc_mbim_desc))
  2215. goto next_desc;
  2216. hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer;
  2217. break;
  2218. case USB_CDC_MBIM_EXTENDED_TYPE:
  2219. if (elength < sizeof(struct usb_cdc_mbim_extended_desc))
  2220. goto next_desc;
  2221. hdr->usb_cdc_mbim_extended_desc =
  2222. (struct usb_cdc_mbim_extended_desc *)buffer;
  2223. break;
  2224. case CDC_PHONET_MAGIC_NUMBER:
  2225. hdr->phonet_magic_present = true;
  2226. break;
  2227. default:
  2228. /*
  2229. * there are LOTS more CDC descriptors that
  2230. * could legitimately be found here.
  2231. */
  2232. dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n",
  2233. buffer[2], elength);
  2234. goto next_desc;
  2235. }
  2236. cnt++;
  2237. next_desc:
  2238. buflen -= elength;
  2239. buffer += elength;
  2240. }
  2241. hdr->usb_cdc_union_desc = union_header;
  2242. hdr->usb_cdc_header_desc = header;
  2243. hdr->usb_cdc_mdlm_detail_desc = detail;
  2244. hdr->usb_cdc_mdlm_desc = desc;
  2245. hdr->usb_cdc_ether_desc = ether;
  2246. return cnt;
  2247. }
  2248. EXPORT_SYMBOL(cdc_parse_cdc_header);