core.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * udc.c - Core UDC Framework
  4. *
  5. * Copyright (C) 2010 Texas Instruments
  6. * Author: Felipe Balbi <balbi@ti.com>
  7. */
  8. #define pr_fmt(fmt) "UDC core: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/list.h>
  13. #include <linux/idr.h>
  14. #include <linux/err.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/usb/ch9.h>
  19. #include <linux/usb/gadget.h>
  20. #include <linux/usb.h>
  21. #include "trace.h"
  22. static DEFINE_IDA(gadget_id_numbers);
  23. static const struct bus_type gadget_bus_type;
  24. /**
  25. * struct usb_udc - describes one usb device controller
  26. * @driver: the gadget driver pointer. For use by the class code
  27. * @dev: the child device to the actual controller
  28. * @gadget: the gadget. For use by the class code
  29. * @list: for use by the udc class driver
  30. * @vbus: for udcs who care about vbus status, this value is real vbus status;
  31. * for udcs who do not care about vbus status, this value is always true
  32. * @started: the UDC's started state. True if the UDC had started.
  33. * @allow_connect: Indicates whether UDC is allowed to be pulled up.
  34. * Set/cleared by gadget_(un)bind_driver() after gadget driver is bound or
  35. * unbound.
  36. * @vbus_work: work routine to handle VBUS status change notifications.
  37. * @connect_lock: protects udc->started, gadget->connect,
  38. * gadget->allow_connect and gadget->deactivate. The routines
  39. * usb_gadget_connect_locked(), usb_gadget_disconnect_locked(),
  40. * usb_udc_connect_control_locked(), usb_gadget_udc_start_locked() and
  41. * usb_gadget_udc_stop_locked() are called with this lock held.
  42. *
  43. * This represents the internal data structure which is used by the UDC-class
  44. * to hold information about udc driver and gadget together.
  45. */
  46. struct usb_udc {
  47. struct usb_gadget_driver *driver;
  48. struct usb_gadget *gadget;
  49. struct device dev;
  50. struct list_head list;
  51. bool vbus;
  52. bool started;
  53. bool allow_connect;
  54. struct work_struct vbus_work;
  55. struct mutex connect_lock;
  56. };
  57. static const struct class udc_class;
  58. static LIST_HEAD(udc_list);
  59. /* Protects udc_list, udc->driver, driver->is_bound, and related calls */
  60. static DEFINE_MUTEX(udc_lock);
  61. /* ------------------------------------------------------------------------- */
  62. /**
  63. * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
  64. * @ep:the endpoint being configured
  65. * @maxpacket_limit:value of maximum packet size limit
  66. *
  67. * This function should be used only in UDC drivers to initialize endpoint
  68. * (usually in probe function).
  69. */
  70. void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
  71. unsigned maxpacket_limit)
  72. {
  73. ep->maxpacket_limit = maxpacket_limit;
  74. ep->maxpacket = maxpacket_limit;
  75. trace_usb_ep_set_maxpacket_limit(ep, 0);
  76. }
  77. EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
  78. /**
  79. * usb_ep_enable - configure endpoint, making it usable
  80. * @ep:the endpoint being configured. may not be the endpoint named "ep0".
  81. * drivers discover endpoints through the ep_list of a usb_gadget.
  82. *
  83. * When configurations are set, or when interface settings change, the driver
  84. * will enable or disable the relevant endpoints. while it is enabled, an
  85. * endpoint may be used for i/o until the driver receives a disconnect() from
  86. * the host or until the endpoint is disabled.
  87. *
  88. * the ep0 implementation (which calls this routine) must ensure that the
  89. * hardware capabilities of each endpoint match the descriptor provided
  90. * for it. for example, an endpoint named "ep2in-bulk" would be usable
  91. * for interrupt transfers as well as bulk, but it likely couldn't be used
  92. * for iso transfers or for endpoint 14. some endpoints are fully
  93. * configurable, with more generic names like "ep-a". (remember that for
  94. * USB, "in" means "towards the USB host".)
  95. *
  96. * This routine may be called in an atomic (interrupt) context.
  97. *
  98. * returns zero, or a negative error code.
  99. */
  100. int usb_ep_enable(struct usb_ep *ep)
  101. {
  102. int ret = 0;
  103. if (ep->enabled)
  104. goto out;
  105. /* UDC drivers can't handle endpoints with maxpacket size 0 */
  106. if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
  107. WARN_ONCE(1, "%s: ep%d (%s) has %s\n", __func__, ep->address, ep->name,
  108. (!ep->desc) ? "NULL descriptor" : "maxpacket 0");
  109. ret = -EINVAL;
  110. goto out;
  111. }
  112. ret = ep->ops->enable(ep, ep->desc);
  113. if (ret)
  114. goto out;
  115. ep->enabled = true;
  116. out:
  117. trace_usb_ep_enable(ep, ret);
  118. return ret;
  119. }
  120. EXPORT_SYMBOL_GPL(usb_ep_enable);
  121. /**
  122. * usb_ep_disable - endpoint is no longer usable
  123. * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
  124. *
  125. * no other task may be using this endpoint when this is called.
  126. * any pending and uncompleted requests will complete with status
  127. * indicating disconnect (-ESHUTDOWN) before this call returns.
  128. * gadget drivers must call usb_ep_enable() again before queueing
  129. * requests to the endpoint.
  130. *
  131. * This routine may be called in an atomic (interrupt) context.
  132. *
  133. * returns zero, or a negative error code.
  134. */
  135. int usb_ep_disable(struct usb_ep *ep)
  136. {
  137. int ret = 0;
  138. if (!ep->enabled)
  139. goto out;
  140. ret = ep->ops->disable(ep);
  141. if (ret)
  142. goto out;
  143. ep->enabled = false;
  144. out:
  145. trace_usb_ep_disable(ep, ret);
  146. return ret;
  147. }
  148. EXPORT_SYMBOL_GPL(usb_ep_disable);
  149. /**
  150. * usb_ep_alloc_request - allocate a request object to use with this endpoint
  151. * @ep:the endpoint to be used with with the request
  152. * @gfp_flags:GFP_* flags to use
  153. *
  154. * Request objects must be allocated with this call, since they normally
  155. * need controller-specific setup and may even need endpoint-specific
  156. * resources such as allocation of DMA descriptors.
  157. * Requests may be submitted with usb_ep_queue(), and receive a single
  158. * completion callback. Free requests with usb_ep_free_request(), when
  159. * they are no longer needed.
  160. *
  161. * Returns the request, or null if one could not be allocated.
  162. */
  163. struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
  164. gfp_t gfp_flags)
  165. {
  166. struct usb_request *req = NULL;
  167. req = ep->ops->alloc_request(ep, gfp_flags);
  168. if (req)
  169. req->ep = ep;
  170. trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
  171. return req;
  172. }
  173. EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
  174. /**
  175. * usb_ep_free_request - frees a request object
  176. * @ep:the endpoint associated with the request
  177. * @req:the request being freed
  178. *
  179. * Reverses the effect of usb_ep_alloc_request().
  180. * Caller guarantees the request is not queued, and that it will
  181. * no longer be requeued (or otherwise used).
  182. */
  183. void usb_ep_free_request(struct usb_ep *ep,
  184. struct usb_request *req)
  185. {
  186. trace_usb_ep_free_request(ep, req, 0);
  187. ep->ops->free_request(ep, req);
  188. }
  189. EXPORT_SYMBOL_GPL(usb_ep_free_request);
  190. /**
  191. * usb_ep_queue - queues (submits) an I/O request to an endpoint.
  192. * @ep:the endpoint associated with the request
  193. * @req:the request being submitted
  194. * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
  195. * pre-allocate all necessary memory with the request.
  196. *
  197. * This tells the device controller to perform the specified request through
  198. * that endpoint (reading or writing a buffer). When the request completes,
  199. * including being canceled by usb_ep_dequeue(), the request's completion
  200. * routine is called to return the request to the driver. Any endpoint
  201. * (except control endpoints like ep0) may have more than one transfer
  202. * request queued; they complete in FIFO order. Once a gadget driver
  203. * submits a request, that request may not be examined or modified until it
  204. * is given back to that driver through the completion callback.
  205. *
  206. * Each request is turned into one or more packets. The controller driver
  207. * never merges adjacent requests into the same packet. OUT transfers
  208. * will sometimes use data that's already buffered in the hardware.
  209. * Drivers can rely on the fact that the first byte of the request's buffer
  210. * always corresponds to the first byte of some USB packet, for both
  211. * IN and OUT transfers.
  212. *
  213. * Bulk endpoints can queue any amount of data; the transfer is packetized
  214. * automatically. The last packet will be short if the request doesn't fill it
  215. * out completely. Zero length packets (ZLPs) should be avoided in portable
  216. * protocols since not all usb hardware can successfully handle zero length
  217. * packets. (ZLPs may be explicitly written, and may be implicitly written if
  218. * the request 'zero' flag is set.) Bulk endpoints may also be used
  219. * for interrupt transfers; but the reverse is not true, and some endpoints
  220. * won't support every interrupt transfer. (Such as 768 byte packets.)
  221. *
  222. * Interrupt-only endpoints are less functional than bulk endpoints, for
  223. * example by not supporting queueing or not handling buffers that are
  224. * larger than the endpoint's maxpacket size. They may also treat data
  225. * toggle differently.
  226. *
  227. * Control endpoints ... after getting a setup() callback, the driver queues
  228. * one response (even if it would be zero length). That enables the
  229. * status ack, after transferring data as specified in the response. Setup
  230. * functions may return negative error codes to generate protocol stalls.
  231. * (Note that some USB device controllers disallow protocol stall responses
  232. * in some cases.) When control responses are deferred (the response is
  233. * written after the setup callback returns), then usb_ep_set_halt() may be
  234. * used on ep0 to trigger protocol stalls. Depending on the controller,
  235. * it may not be possible to trigger a status-stage protocol stall when the
  236. * data stage is over, that is, from within the response's completion
  237. * routine.
  238. *
  239. * For periodic endpoints, like interrupt or isochronous ones, the usb host
  240. * arranges to poll once per interval, and the gadget driver usually will
  241. * have queued some data to transfer at that time.
  242. *
  243. * Note that @req's ->complete() callback must never be called from
  244. * within usb_ep_queue() as that can create deadlock situations.
  245. *
  246. * This routine may be called in interrupt context.
  247. *
  248. * Returns zero, or a negative error code. Endpoints that are not enabled
  249. * report errors; errors will also be
  250. * reported when the usb peripheral is disconnected.
  251. *
  252. * If and only if @req is successfully queued (the return value is zero),
  253. * @req->complete() will be called exactly once, when the Gadget core and
  254. * UDC are finished with the request. When the completion function is called,
  255. * control of the request is returned to the device driver which submitted it.
  256. * The completion handler may then immediately free or reuse @req.
  257. */
  258. int usb_ep_queue(struct usb_ep *ep,
  259. struct usb_request *req, gfp_t gfp_flags)
  260. {
  261. int ret = 0;
  262. if (!ep->enabled && ep->address) {
  263. pr_debug("USB gadget: queue request to disabled ep 0x%x (%s)\n",
  264. ep->address, ep->name);
  265. ret = -ESHUTDOWN;
  266. goto out;
  267. }
  268. ret = ep->ops->queue(ep, req, gfp_flags);
  269. out:
  270. trace_usb_ep_queue(ep, req, ret);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(usb_ep_queue);
  274. /**
  275. * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
  276. * @ep:the endpoint associated with the request
  277. * @req:the request being canceled
  278. *
  279. * If the request is still active on the endpoint, it is dequeued and
  280. * eventually its completion routine is called (with status -ECONNRESET);
  281. * else a negative error code is returned. This routine is asynchronous,
  282. * that is, it may return before the completion routine runs.
  283. *
  284. * Note that some hardware can't clear out write fifos (to unlink the request
  285. * at the head of the queue) except as part of disconnecting from usb. Such
  286. * restrictions prevent drivers from supporting configuration changes,
  287. * even to configuration zero (a "chapter 9" requirement).
  288. *
  289. * This routine may be called in interrupt context.
  290. */
  291. int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  292. {
  293. int ret;
  294. ret = ep->ops->dequeue(ep, req);
  295. trace_usb_ep_dequeue(ep, req, ret);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(usb_ep_dequeue);
  299. /**
  300. * usb_ep_set_halt - sets the endpoint halt feature.
  301. * @ep: the non-isochronous endpoint being stalled
  302. *
  303. * Use this to stall an endpoint, perhaps as an error report.
  304. * Except for control endpoints,
  305. * the endpoint stays halted (will not stream any data) until the host
  306. * clears this feature; drivers may need to empty the endpoint's request
  307. * queue first, to make sure no inappropriate transfers happen.
  308. *
  309. * Note that while an endpoint CLEAR_FEATURE will be invisible to the
  310. * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
  311. * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
  312. * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
  313. *
  314. * This routine may be called in interrupt context.
  315. *
  316. * Returns zero, or a negative error code. On success, this call sets
  317. * underlying hardware state that blocks data transfers.
  318. * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
  319. * transfer requests are still queued, or if the controller hardware
  320. * (usually a FIFO) still holds bytes that the host hasn't collected.
  321. */
  322. int usb_ep_set_halt(struct usb_ep *ep)
  323. {
  324. int ret;
  325. ret = ep->ops->set_halt(ep, 1);
  326. trace_usb_ep_set_halt(ep, ret);
  327. return ret;
  328. }
  329. EXPORT_SYMBOL_GPL(usb_ep_set_halt);
  330. /**
  331. * usb_ep_clear_halt - clears endpoint halt, and resets toggle
  332. * @ep:the bulk or interrupt endpoint being reset
  333. *
  334. * Use this when responding to the standard usb "set interface" request,
  335. * for endpoints that aren't reconfigured, after clearing any other state
  336. * in the endpoint's i/o queue.
  337. *
  338. * This routine may be called in interrupt context.
  339. *
  340. * Returns zero, or a negative error code. On success, this call clears
  341. * the underlying hardware state reflecting endpoint halt and data toggle.
  342. * Note that some hardware can't support this request (like pxa2xx_udc),
  343. * and accordingly can't correctly implement interface altsettings.
  344. */
  345. int usb_ep_clear_halt(struct usb_ep *ep)
  346. {
  347. int ret;
  348. ret = ep->ops->set_halt(ep, 0);
  349. trace_usb_ep_clear_halt(ep, ret);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
  353. /**
  354. * usb_ep_set_wedge - sets the halt feature and ignores clear requests
  355. * @ep: the endpoint being wedged
  356. *
  357. * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  358. * requests. If the gadget driver clears the halt status, it will
  359. * automatically unwedge the endpoint.
  360. *
  361. * This routine may be called in interrupt context.
  362. *
  363. * Returns zero on success, else negative errno.
  364. */
  365. int usb_ep_set_wedge(struct usb_ep *ep)
  366. {
  367. int ret;
  368. if (ep->ops->set_wedge)
  369. ret = ep->ops->set_wedge(ep);
  370. else
  371. ret = ep->ops->set_halt(ep, 1);
  372. trace_usb_ep_set_wedge(ep, ret);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
  376. /**
  377. * usb_ep_fifo_status - returns number of bytes in fifo, or error
  378. * @ep: the endpoint whose fifo status is being checked.
  379. *
  380. * FIFO endpoints may have "unclaimed data" in them in certain cases,
  381. * such as after aborted transfers. Hosts may not have collected all
  382. * the IN data written by the gadget driver (and reported by a request
  383. * completion). The gadget driver may not have collected all the data
  384. * written OUT to it by the host. Drivers that need precise handling for
  385. * fault reporting or recovery may need to use this call.
  386. *
  387. * This routine may be called in interrupt context.
  388. *
  389. * This returns the number of such bytes in the fifo, or a negative
  390. * errno if the endpoint doesn't use a FIFO or doesn't support such
  391. * precise handling.
  392. */
  393. int usb_ep_fifo_status(struct usb_ep *ep)
  394. {
  395. int ret;
  396. if (ep->ops->fifo_status)
  397. ret = ep->ops->fifo_status(ep);
  398. else
  399. ret = -EOPNOTSUPP;
  400. trace_usb_ep_fifo_status(ep, ret);
  401. return ret;
  402. }
  403. EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
  404. /**
  405. * usb_ep_fifo_flush - flushes contents of a fifo
  406. * @ep: the endpoint whose fifo is being flushed.
  407. *
  408. * This call may be used to flush the "unclaimed data" that may exist in
  409. * an endpoint fifo after abnormal transaction terminations. The call
  410. * must never be used except when endpoint is not being used for any
  411. * protocol translation.
  412. *
  413. * This routine may be called in interrupt context.
  414. */
  415. void usb_ep_fifo_flush(struct usb_ep *ep)
  416. {
  417. if (ep->ops->fifo_flush)
  418. ep->ops->fifo_flush(ep);
  419. trace_usb_ep_fifo_flush(ep, 0);
  420. }
  421. EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
  422. /* ------------------------------------------------------------------------- */
  423. /**
  424. * usb_gadget_frame_number - returns the current frame number
  425. * @gadget: controller that reports the frame number
  426. *
  427. * Returns the usb frame number, normally eleven bits from a SOF packet,
  428. * or negative errno if this device doesn't support this capability.
  429. */
  430. int usb_gadget_frame_number(struct usb_gadget *gadget)
  431. {
  432. int ret;
  433. ret = gadget->ops->get_frame(gadget);
  434. trace_usb_gadget_frame_number(gadget, ret);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
  438. /**
  439. * usb_gadget_wakeup - tries to wake up the host connected to this gadget
  440. * @gadget: controller used to wake up the host
  441. *
  442. * Returns zero on success, else negative error code if the hardware
  443. * doesn't support such attempts, or its support has not been enabled
  444. * by the usb host. Drivers must return device descriptors that report
  445. * their ability to support this, or hosts won't enable it.
  446. *
  447. * This may also try to use SRP to wake the host and start enumeration,
  448. * even if OTG isn't otherwise in use. OTG devices may also start
  449. * remote wakeup even when hosts don't explicitly enable it.
  450. */
  451. int usb_gadget_wakeup(struct usb_gadget *gadget)
  452. {
  453. int ret = 0;
  454. if (!gadget->ops->wakeup) {
  455. ret = -EOPNOTSUPP;
  456. goto out;
  457. }
  458. ret = gadget->ops->wakeup(gadget);
  459. out:
  460. trace_usb_gadget_wakeup(gadget, ret);
  461. return ret;
  462. }
  463. EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
  464. /**
  465. * usb_gadget_set_remote_wakeup - configures the device remote wakeup feature.
  466. * @gadget:the device being configured for remote wakeup
  467. * @set:value to be configured.
  468. *
  469. * set to one to enable remote wakeup feature and zero to disable it.
  470. *
  471. * returns zero on success, else negative errno.
  472. */
  473. int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set)
  474. {
  475. int ret = 0;
  476. if (!gadget->ops->set_remote_wakeup) {
  477. ret = -EOPNOTSUPP;
  478. goto out;
  479. }
  480. ret = gadget->ops->set_remote_wakeup(gadget, set);
  481. out:
  482. trace_usb_gadget_set_remote_wakeup(gadget, ret);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(usb_gadget_set_remote_wakeup);
  486. /**
  487. * usb_gadget_set_selfpowered - sets the device selfpowered feature.
  488. * @gadget:the device being declared as self-powered
  489. *
  490. * this affects the device status reported by the hardware driver
  491. * to reflect that it now has a local power supply.
  492. *
  493. * returns zero on success, else negative errno.
  494. */
  495. int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
  496. {
  497. int ret = 0;
  498. if (!gadget->ops->set_selfpowered) {
  499. ret = -EOPNOTSUPP;
  500. goto out;
  501. }
  502. ret = gadget->ops->set_selfpowered(gadget, 1);
  503. out:
  504. trace_usb_gadget_set_selfpowered(gadget, ret);
  505. return ret;
  506. }
  507. EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
  508. /**
  509. * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
  510. * @gadget:the device being declared as bus-powered
  511. *
  512. * this affects the device status reported by the hardware driver.
  513. * some hardware may not support bus-powered operation, in which
  514. * case this feature's value can never change.
  515. *
  516. * returns zero on success, else negative errno.
  517. */
  518. int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
  519. {
  520. int ret = 0;
  521. if (!gadget->ops->set_selfpowered) {
  522. ret = -EOPNOTSUPP;
  523. goto out;
  524. }
  525. ret = gadget->ops->set_selfpowered(gadget, 0);
  526. out:
  527. trace_usb_gadget_clear_selfpowered(gadget, ret);
  528. return ret;
  529. }
  530. EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
  531. /**
  532. * usb_gadget_vbus_connect - Notify controller that VBUS is powered
  533. * @gadget:The device which now has VBUS power.
  534. * Context: can sleep
  535. *
  536. * This call is used by a driver for an external transceiver (or GPIO)
  537. * that detects a VBUS power session starting. Common responses include
  538. * resuming the controller, activating the D+ (or D-) pullup to let the
  539. * host detect that a USB device is attached, and starting to draw power
  540. * (8mA or possibly more, especially after SET_CONFIGURATION).
  541. *
  542. * Returns zero on success, else negative errno.
  543. */
  544. int usb_gadget_vbus_connect(struct usb_gadget *gadget)
  545. {
  546. int ret = 0;
  547. if (!gadget->ops->vbus_session) {
  548. ret = -EOPNOTSUPP;
  549. goto out;
  550. }
  551. ret = gadget->ops->vbus_session(gadget, 1);
  552. out:
  553. trace_usb_gadget_vbus_connect(gadget, ret);
  554. return ret;
  555. }
  556. EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
  557. /**
  558. * usb_gadget_vbus_draw - constrain controller's VBUS power usage
  559. * @gadget:The device whose VBUS usage is being described
  560. * @mA:How much current to draw, in milliAmperes. This should be twice
  561. * the value listed in the configuration descriptor bMaxPower field.
  562. *
  563. * This call is used by gadget drivers during SET_CONFIGURATION calls,
  564. * reporting how much power the device may consume. For example, this
  565. * could affect how quickly batteries are recharged.
  566. *
  567. * Returns zero on success, else negative errno.
  568. */
  569. int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  570. {
  571. int ret = 0;
  572. if (!gadget->ops->vbus_draw) {
  573. ret = -EOPNOTSUPP;
  574. goto out;
  575. }
  576. ret = gadget->ops->vbus_draw(gadget, mA);
  577. if (!ret)
  578. gadget->mA = mA;
  579. out:
  580. trace_usb_gadget_vbus_draw(gadget, ret);
  581. return ret;
  582. }
  583. EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
  584. /**
  585. * usb_gadget_vbus_disconnect - notify controller about VBUS session end
  586. * @gadget:the device whose VBUS supply is being described
  587. * Context: can sleep
  588. *
  589. * This call is used by a driver for an external transceiver (or GPIO)
  590. * that detects a VBUS power session ending. Common responses include
  591. * reversing everything done in usb_gadget_vbus_connect().
  592. *
  593. * Returns zero on success, else negative errno.
  594. */
  595. int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
  596. {
  597. int ret = 0;
  598. if (!gadget->ops->vbus_session) {
  599. ret = -EOPNOTSUPP;
  600. goto out;
  601. }
  602. ret = gadget->ops->vbus_session(gadget, 0);
  603. out:
  604. trace_usb_gadget_vbus_disconnect(gadget, ret);
  605. return ret;
  606. }
  607. EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
  608. static int usb_gadget_connect_locked(struct usb_gadget *gadget)
  609. __must_hold(&gadget->udc->connect_lock)
  610. {
  611. int ret = 0;
  612. if (!gadget->ops->pullup) {
  613. ret = -EOPNOTSUPP;
  614. goto out;
  615. }
  616. if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) {
  617. /*
  618. * If the gadget isn't usable (because it is deactivated,
  619. * unbound, or not yet started), we only save the new state.
  620. * The gadget will be connected automatically when it is
  621. * activated/bound/started.
  622. */
  623. gadget->connected = true;
  624. goto out;
  625. }
  626. ret = gadget->ops->pullup(gadget, 1);
  627. if (!ret)
  628. gadget->connected = 1;
  629. out:
  630. trace_usb_gadget_connect(gadget, ret);
  631. return ret;
  632. }
  633. /**
  634. * usb_gadget_connect - software-controlled connect to USB host
  635. * @gadget:the peripheral being connected
  636. *
  637. * Enables the D+ (or potentially D-) pullup. The host will start
  638. * enumerating this gadget when the pullup is active and a VBUS session
  639. * is active (the link is powered).
  640. *
  641. * Returns zero on success, else negative errno.
  642. */
  643. int usb_gadget_connect(struct usb_gadget *gadget)
  644. {
  645. int ret;
  646. mutex_lock(&gadget->udc->connect_lock);
  647. ret = usb_gadget_connect_locked(gadget);
  648. mutex_unlock(&gadget->udc->connect_lock);
  649. return ret;
  650. }
  651. EXPORT_SYMBOL_GPL(usb_gadget_connect);
  652. static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
  653. __must_hold(&gadget->udc->connect_lock)
  654. {
  655. int ret = 0;
  656. if (!gadget->ops->pullup) {
  657. ret = -EOPNOTSUPP;
  658. goto out;
  659. }
  660. if (!gadget->connected)
  661. goto out;
  662. if (gadget->deactivated || !gadget->udc->started) {
  663. /*
  664. * If gadget is deactivated we only save new state.
  665. * Gadget will stay disconnected after activation.
  666. */
  667. gadget->connected = false;
  668. goto out;
  669. }
  670. ret = gadget->ops->pullup(gadget, 0);
  671. if (!ret)
  672. gadget->connected = 0;
  673. mutex_lock(&udc_lock);
  674. if (gadget->udc->driver)
  675. gadget->udc->driver->disconnect(gadget);
  676. mutex_unlock(&udc_lock);
  677. out:
  678. trace_usb_gadget_disconnect(gadget, ret);
  679. return ret;
  680. }
  681. /**
  682. * usb_gadget_disconnect - software-controlled disconnect from USB host
  683. * @gadget:the peripheral being disconnected
  684. *
  685. * Disables the D+ (or potentially D-) pullup, which the host may see
  686. * as a disconnect (when a VBUS session is active). Not all systems
  687. * support software pullup controls.
  688. *
  689. * Following a successful disconnect, invoke the ->disconnect() callback
  690. * for the current gadget driver so that UDC drivers don't need to.
  691. *
  692. * Returns zero on success, else negative errno.
  693. */
  694. int usb_gadget_disconnect(struct usb_gadget *gadget)
  695. {
  696. int ret;
  697. mutex_lock(&gadget->udc->connect_lock);
  698. ret = usb_gadget_disconnect_locked(gadget);
  699. mutex_unlock(&gadget->udc->connect_lock);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
  703. /**
  704. * usb_gadget_deactivate - deactivate function which is not ready to work
  705. * @gadget: the peripheral being deactivated
  706. *
  707. * This routine may be used during the gadget driver bind() call to prevent
  708. * the peripheral from ever being visible to the USB host, unless later
  709. * usb_gadget_activate() is called. For example, user mode components may
  710. * need to be activated before the system can talk to hosts.
  711. *
  712. * This routine may sleep; it must not be called in interrupt context
  713. * (such as from within a gadget driver's disconnect() callback).
  714. *
  715. * Returns zero on success, else negative errno.
  716. */
  717. int usb_gadget_deactivate(struct usb_gadget *gadget)
  718. {
  719. int ret = 0;
  720. mutex_lock(&gadget->udc->connect_lock);
  721. if (gadget->deactivated)
  722. goto unlock;
  723. if (gadget->connected) {
  724. ret = usb_gadget_disconnect_locked(gadget);
  725. if (ret)
  726. goto unlock;
  727. /*
  728. * If gadget was being connected before deactivation, we want
  729. * to reconnect it in usb_gadget_activate().
  730. */
  731. gadget->connected = true;
  732. }
  733. gadget->deactivated = true;
  734. unlock:
  735. mutex_unlock(&gadget->udc->connect_lock);
  736. trace_usb_gadget_deactivate(gadget, ret);
  737. return ret;
  738. }
  739. EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
  740. /**
  741. * usb_gadget_activate - activate function which is not ready to work
  742. * @gadget: the peripheral being activated
  743. *
  744. * This routine activates gadget which was previously deactivated with
  745. * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
  746. *
  747. * This routine may sleep; it must not be called in interrupt context.
  748. *
  749. * Returns zero on success, else negative errno.
  750. */
  751. int usb_gadget_activate(struct usb_gadget *gadget)
  752. {
  753. int ret = 0;
  754. mutex_lock(&gadget->udc->connect_lock);
  755. if (!gadget->deactivated)
  756. goto unlock;
  757. gadget->deactivated = false;
  758. /*
  759. * If gadget has been connected before deactivation, or became connected
  760. * while it was being deactivated, we call usb_gadget_connect().
  761. */
  762. if (gadget->connected)
  763. ret = usb_gadget_connect_locked(gadget);
  764. unlock:
  765. mutex_unlock(&gadget->udc->connect_lock);
  766. trace_usb_gadget_activate(gadget, ret);
  767. return ret;
  768. }
  769. EXPORT_SYMBOL_GPL(usb_gadget_activate);
  770. /* ------------------------------------------------------------------------- */
  771. #ifdef CONFIG_HAS_DMA
  772. int usb_gadget_map_request_by_dev(struct device *dev,
  773. struct usb_request *req, int is_in)
  774. {
  775. if (req->length == 0)
  776. return 0;
  777. if (req->sg_was_mapped) {
  778. req->num_mapped_sgs = req->num_sgs;
  779. return 0;
  780. }
  781. if (req->num_sgs) {
  782. int mapped;
  783. mapped = dma_map_sg(dev, req->sg, req->num_sgs,
  784. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  785. if (mapped == 0) {
  786. dev_err(dev, "failed to map SGs\n");
  787. return -EFAULT;
  788. }
  789. req->num_mapped_sgs = mapped;
  790. } else {
  791. if (is_vmalloc_addr(req->buf)) {
  792. dev_err(dev, "buffer is not dma capable\n");
  793. return -EFAULT;
  794. } else if (object_is_on_stack(req->buf)) {
  795. dev_err(dev, "buffer is on stack\n");
  796. return -EFAULT;
  797. }
  798. req->dma = dma_map_single(dev, req->buf, req->length,
  799. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  800. if (dma_mapping_error(dev, req->dma)) {
  801. dev_err(dev, "failed to map buffer\n");
  802. return -EFAULT;
  803. }
  804. req->dma_mapped = 1;
  805. }
  806. return 0;
  807. }
  808. EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
  809. int usb_gadget_map_request(struct usb_gadget *gadget,
  810. struct usb_request *req, int is_in)
  811. {
  812. return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
  813. }
  814. EXPORT_SYMBOL_GPL(usb_gadget_map_request);
  815. void usb_gadget_unmap_request_by_dev(struct device *dev,
  816. struct usb_request *req, int is_in)
  817. {
  818. if (req->length == 0 || req->sg_was_mapped)
  819. return;
  820. if (req->num_mapped_sgs) {
  821. dma_unmap_sg(dev, req->sg, req->num_sgs,
  822. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  823. req->num_mapped_sgs = 0;
  824. } else if (req->dma_mapped) {
  825. dma_unmap_single(dev, req->dma, req->length,
  826. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  827. req->dma_mapped = 0;
  828. }
  829. }
  830. EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
  831. void usb_gadget_unmap_request(struct usb_gadget *gadget,
  832. struct usb_request *req, int is_in)
  833. {
  834. usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
  835. }
  836. EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
  837. #endif /* CONFIG_HAS_DMA */
  838. /* ------------------------------------------------------------------------- */
  839. /**
  840. * usb_gadget_giveback_request - give the request back to the gadget layer
  841. * @ep: the endpoint to be used with with the request
  842. * @req: the request being given back
  843. *
  844. * This is called by device controller drivers in order to return the
  845. * completed request back to the gadget layer.
  846. */
  847. void usb_gadget_giveback_request(struct usb_ep *ep,
  848. struct usb_request *req)
  849. {
  850. if (likely(req->status == 0))
  851. usb_led_activity(USB_LED_EVENT_GADGET);
  852. trace_usb_gadget_giveback_request(ep, req, 0);
  853. req->complete(ep, req);
  854. }
  855. EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
  856. /* ------------------------------------------------------------------------- */
  857. /**
  858. * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
  859. * in second parameter or NULL if searched endpoint not found
  860. * @g: controller to check for quirk
  861. * @name: name of searched endpoint
  862. */
  863. struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
  864. {
  865. struct usb_ep *ep;
  866. gadget_for_each_ep(ep, g) {
  867. if (!strcmp(ep->name, name))
  868. return ep;
  869. }
  870. return NULL;
  871. }
  872. EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
  873. /* ------------------------------------------------------------------------- */
  874. int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
  875. struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
  876. struct usb_ss_ep_comp_descriptor *ep_comp)
  877. {
  878. u8 type;
  879. u16 max;
  880. int num_req_streams = 0;
  881. /* endpoint already claimed? */
  882. if (ep->claimed)
  883. return 0;
  884. type = usb_endpoint_type(desc);
  885. max = usb_endpoint_maxp(desc);
  886. if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
  887. return 0;
  888. if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
  889. return 0;
  890. if (max > ep->maxpacket_limit)
  891. return 0;
  892. /* "high bandwidth" works only at high speed */
  893. if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
  894. return 0;
  895. switch (type) {
  896. case USB_ENDPOINT_XFER_CONTROL:
  897. /* only support ep0 for portable CONTROL traffic */
  898. return 0;
  899. case USB_ENDPOINT_XFER_ISOC:
  900. if (!ep->caps.type_iso)
  901. return 0;
  902. /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
  903. if (!gadget_is_dualspeed(gadget) && max > 1023)
  904. return 0;
  905. break;
  906. case USB_ENDPOINT_XFER_BULK:
  907. if (!ep->caps.type_bulk)
  908. return 0;
  909. if (ep_comp && gadget_is_superspeed(gadget)) {
  910. /* Get the number of required streams from the
  911. * EP companion descriptor and see if the EP
  912. * matches it
  913. */
  914. num_req_streams = ep_comp->bmAttributes & 0x1f;
  915. if (num_req_streams > ep->max_streams)
  916. return 0;
  917. }
  918. break;
  919. case USB_ENDPOINT_XFER_INT:
  920. /* Bulk endpoints handle interrupt transfers,
  921. * except the toggle-quirky iso-synch kind
  922. */
  923. if (!ep->caps.type_int && !ep->caps.type_bulk)
  924. return 0;
  925. /* INT: limit 64 bytes full speed, 1024 high/super speed */
  926. if (!gadget_is_dualspeed(gadget) && max > 64)
  927. return 0;
  928. break;
  929. }
  930. return 1;
  931. }
  932. EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
  933. /**
  934. * usb_gadget_check_config - checks if the UDC can support the binded
  935. * configuration
  936. * @gadget: controller to check the USB configuration
  937. *
  938. * Ensure that a UDC is able to support the requested resources by a
  939. * configuration, and that there are no resource limitations, such as
  940. * internal memory allocated to all requested endpoints.
  941. *
  942. * Returns zero on success, else a negative errno.
  943. */
  944. int usb_gadget_check_config(struct usb_gadget *gadget)
  945. {
  946. if (gadget->ops->check_config)
  947. return gadget->ops->check_config(gadget);
  948. return 0;
  949. }
  950. EXPORT_SYMBOL_GPL(usb_gadget_check_config);
  951. /* ------------------------------------------------------------------------- */
  952. static void usb_gadget_state_work(struct work_struct *work)
  953. {
  954. struct usb_gadget *gadget = work_to_gadget(work);
  955. struct usb_udc *udc = gadget->udc;
  956. if (udc)
  957. sysfs_notify(&udc->dev.kobj, NULL, "state");
  958. }
  959. void usb_gadget_set_state(struct usb_gadget *gadget,
  960. enum usb_device_state state)
  961. {
  962. unsigned long flags;
  963. spin_lock_irqsave(&gadget->state_lock, flags);
  964. gadget->state = state;
  965. if (!gadget->teardown)
  966. schedule_work(&gadget->work);
  967. spin_unlock_irqrestore(&gadget->state_lock, flags);
  968. trace_usb_gadget_set_state(gadget, 0);
  969. }
  970. EXPORT_SYMBOL_GPL(usb_gadget_set_state);
  971. /* ------------------------------------------------------------------------- */
  972. /* Acquire connect_lock before calling this function. */
  973. static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock)
  974. {
  975. if (udc->vbus)
  976. return usb_gadget_connect_locked(udc->gadget);
  977. else
  978. return usb_gadget_disconnect_locked(udc->gadget);
  979. }
  980. static void vbus_event_work(struct work_struct *work)
  981. {
  982. struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work);
  983. mutex_lock(&udc->connect_lock);
  984. usb_udc_connect_control_locked(udc);
  985. mutex_unlock(&udc->connect_lock);
  986. }
  987. /**
  988. * usb_udc_vbus_handler - updates the udc core vbus status, and try to
  989. * connect or disconnect gadget
  990. * @gadget: The gadget which vbus change occurs
  991. * @status: The vbus status
  992. *
  993. * The udc driver calls it when it wants to connect or disconnect gadget
  994. * according to vbus status.
  995. *
  996. * This function can be invoked from interrupt context by irq handlers of
  997. * the gadget drivers, however, usb_udc_connect_control() has to run in
  998. * non-atomic context due to the following:
  999. * a. Some of the gadget driver implementations expect the ->pullup
  1000. * callback to be invoked in non-atomic context.
  1001. * b. usb_gadget_disconnect() acquires udc_lock which is a mutex.
  1002. * Hence offload invocation of usb_udc_connect_control() to workqueue.
  1003. */
  1004. void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
  1005. {
  1006. struct usb_udc *udc = gadget->udc;
  1007. if (udc) {
  1008. udc->vbus = status;
  1009. schedule_work(&udc->vbus_work);
  1010. }
  1011. }
  1012. EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
  1013. /**
  1014. * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
  1015. * @gadget: The gadget which bus reset occurs
  1016. * @driver: The gadget driver we want to notify
  1017. *
  1018. * If the udc driver has bus reset handler, it needs to call this when the bus
  1019. * reset occurs, it notifies the gadget driver that the bus reset occurs as
  1020. * well as updates gadget state.
  1021. */
  1022. void usb_gadget_udc_reset(struct usb_gadget *gadget,
  1023. struct usb_gadget_driver *driver)
  1024. {
  1025. driver->reset(gadget);
  1026. usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
  1027. }
  1028. EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
  1029. /**
  1030. * usb_gadget_udc_start_locked - tells usb device controller to start up
  1031. * @udc: The UDC to be started
  1032. *
  1033. * This call is issued by the UDC Class driver when it's about
  1034. * to register a gadget driver to the device controller, before
  1035. * calling gadget driver's bind() method.
  1036. *
  1037. * It allows the controller to be powered off until strictly
  1038. * necessary to have it powered on.
  1039. *
  1040. * Returns zero on success, else negative errno.
  1041. *
  1042. * Caller should acquire connect_lock before invoking this function.
  1043. */
  1044. static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
  1045. __must_hold(&udc->connect_lock)
  1046. {
  1047. int ret;
  1048. if (udc->started) {
  1049. dev_err(&udc->dev, "UDC had already started\n");
  1050. return -EBUSY;
  1051. }
  1052. ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver);
  1053. if (!ret)
  1054. udc->started = true;
  1055. return ret;
  1056. }
  1057. /**
  1058. * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore
  1059. * @udc: The UDC to be stopped
  1060. *
  1061. * This call is issued by the UDC Class driver after calling
  1062. * gadget driver's unbind() method.
  1063. *
  1064. * The details are implementation specific, but it can go as
  1065. * far as powering off UDC completely and disable its data
  1066. * line pullups.
  1067. *
  1068. * Caller should acquire connect lock before invoking this function.
  1069. */
  1070. static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
  1071. __must_hold(&udc->connect_lock)
  1072. {
  1073. if (!udc->started) {
  1074. dev_err(&udc->dev, "UDC had already stopped\n");
  1075. return;
  1076. }
  1077. udc->gadget->ops->udc_stop(udc->gadget);
  1078. udc->started = false;
  1079. }
  1080. /**
  1081. * usb_gadget_udc_set_speed - tells usb device controller speed supported by
  1082. * current driver
  1083. * @udc: The device we want to set maximum speed
  1084. * @speed: The maximum speed to allowed to run
  1085. *
  1086. * This call is issued by the UDC Class driver before calling
  1087. * usb_gadget_udc_start() in order to make sure that we don't try to
  1088. * connect on speeds the gadget driver doesn't support.
  1089. */
  1090. static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
  1091. enum usb_device_speed speed)
  1092. {
  1093. struct usb_gadget *gadget = udc->gadget;
  1094. enum usb_device_speed s;
  1095. if (speed == USB_SPEED_UNKNOWN)
  1096. s = gadget->max_speed;
  1097. else
  1098. s = min(speed, gadget->max_speed);
  1099. if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate)
  1100. gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate);
  1101. else if (gadget->ops->udc_set_speed)
  1102. gadget->ops->udc_set_speed(gadget, s);
  1103. }
  1104. /**
  1105. * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks
  1106. * @udc: The UDC which should enable async callbacks
  1107. *
  1108. * This routine is used when binding gadget drivers. It undoes the effect
  1109. * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs
  1110. * (if necessary) and resume issuing callbacks.
  1111. *
  1112. * This routine will always be called in process context.
  1113. */
  1114. static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc)
  1115. {
  1116. struct usb_gadget *gadget = udc->gadget;
  1117. if (gadget->ops->udc_async_callbacks)
  1118. gadget->ops->udc_async_callbacks(gadget, true);
  1119. }
  1120. /**
  1121. * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks
  1122. * @udc: The UDC which should disable async callbacks
  1123. *
  1124. * This routine is used when unbinding gadget drivers. It prevents a race:
  1125. * The UDC driver doesn't know when the gadget driver's ->unbind callback
  1126. * runs, so unless it is told to disable asynchronous callbacks, it might
  1127. * issue a callback (such as ->disconnect) after the unbind has completed.
  1128. *
  1129. * After this function runs, the UDC driver must suppress all ->suspend,
  1130. * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver
  1131. * until async callbacks are again enabled. A simple-minded but effective
  1132. * way to accomplish this is to tell the UDC hardware not to generate any
  1133. * more IRQs.
  1134. *
  1135. * Request completion callbacks must still be issued. However, it's okay
  1136. * to defer them until the request is cancelled, since the pull-up will be
  1137. * turned off during the time period when async callbacks are disabled.
  1138. *
  1139. * This routine will always be called in process context.
  1140. */
  1141. static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc)
  1142. {
  1143. struct usb_gadget *gadget = udc->gadget;
  1144. if (gadget->ops->udc_async_callbacks)
  1145. gadget->ops->udc_async_callbacks(gadget, false);
  1146. }
  1147. /**
  1148. * usb_udc_release - release the usb_udc struct
  1149. * @dev: the dev member within usb_udc
  1150. *
  1151. * This is called by driver's core in order to free memory once the last
  1152. * reference is released.
  1153. */
  1154. static void usb_udc_release(struct device *dev)
  1155. {
  1156. struct usb_udc *udc;
  1157. udc = container_of(dev, struct usb_udc, dev);
  1158. dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
  1159. kfree(udc);
  1160. }
  1161. static const struct attribute_group *usb_udc_attr_groups[];
  1162. static void usb_udc_nop_release(struct device *dev)
  1163. {
  1164. dev_vdbg(dev, "%s\n", __func__);
  1165. }
  1166. /**
  1167. * usb_initialize_gadget - initialize a gadget and its embedded struct device
  1168. * @parent: the parent device to this udc. Usually the controller driver's
  1169. * device.
  1170. * @gadget: the gadget to be initialized.
  1171. * @release: a gadget release function.
  1172. */
  1173. void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
  1174. void (*release)(struct device *dev))
  1175. {
  1176. spin_lock_init(&gadget->state_lock);
  1177. gadget->teardown = false;
  1178. INIT_WORK(&gadget->work, usb_gadget_state_work);
  1179. gadget->dev.parent = parent;
  1180. if (release)
  1181. gadget->dev.release = release;
  1182. else
  1183. gadget->dev.release = usb_udc_nop_release;
  1184. device_initialize(&gadget->dev);
  1185. gadget->dev.bus = &gadget_bus_type;
  1186. }
  1187. EXPORT_SYMBOL_GPL(usb_initialize_gadget);
  1188. /**
  1189. * usb_add_gadget - adds a new gadget to the udc class driver list
  1190. * @gadget: the gadget to be added to the list.
  1191. *
  1192. * Returns zero on success, negative errno otherwise.
  1193. * Does not do a final usb_put_gadget() if an error occurs.
  1194. */
  1195. int usb_add_gadget(struct usb_gadget *gadget)
  1196. {
  1197. struct usb_udc *udc;
  1198. int ret = -ENOMEM;
  1199. udc = kzalloc_obj(*udc);
  1200. if (!udc)
  1201. goto error;
  1202. device_initialize(&udc->dev);
  1203. udc->dev.release = usb_udc_release;
  1204. udc->dev.class = &udc_class;
  1205. udc->dev.groups = usb_udc_attr_groups;
  1206. udc->dev.parent = gadget->dev.parent;
  1207. ret = dev_set_name(&udc->dev, "%s",
  1208. kobject_name(&gadget->dev.parent->kobj));
  1209. if (ret)
  1210. goto err_put_udc;
  1211. udc->gadget = gadget;
  1212. gadget->udc = udc;
  1213. mutex_init(&udc->connect_lock);
  1214. udc->started = false;
  1215. mutex_lock(&udc_lock);
  1216. list_add_tail(&udc->list, &udc_list);
  1217. mutex_unlock(&udc_lock);
  1218. INIT_WORK(&udc->vbus_work, vbus_event_work);
  1219. ret = device_add(&udc->dev);
  1220. if (ret)
  1221. goto err_unlist_udc;
  1222. usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
  1223. udc->vbus = true;
  1224. ret = ida_alloc(&gadget_id_numbers, GFP_KERNEL);
  1225. if (ret < 0)
  1226. goto err_del_udc;
  1227. gadget->id_number = ret;
  1228. dev_set_name(&gadget->dev, "gadget.%d", ret);
  1229. ret = device_add(&gadget->dev);
  1230. if (ret)
  1231. goto err_free_id;
  1232. ret = sysfs_create_link(&udc->dev.kobj,
  1233. &gadget->dev.kobj, "gadget");
  1234. if (ret)
  1235. goto err_del_gadget;
  1236. return 0;
  1237. err_del_gadget:
  1238. device_del(&gadget->dev);
  1239. err_free_id:
  1240. ida_free(&gadget_id_numbers, gadget->id_number);
  1241. err_del_udc:
  1242. flush_work(&gadget->work);
  1243. device_del(&udc->dev);
  1244. err_unlist_udc:
  1245. mutex_lock(&udc_lock);
  1246. list_del(&udc->list);
  1247. mutex_unlock(&udc_lock);
  1248. err_put_udc:
  1249. put_device(&udc->dev);
  1250. error:
  1251. return ret;
  1252. }
  1253. EXPORT_SYMBOL_GPL(usb_add_gadget);
  1254. /**
  1255. * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
  1256. * @parent: the parent device to this udc. Usually the controller driver's
  1257. * device.
  1258. * @gadget: the gadget to be added to the list.
  1259. * @release: a gadget release function.
  1260. *
  1261. * Returns zero on success, negative errno otherwise.
  1262. * Calls the gadget release function in the latter case.
  1263. */
  1264. int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
  1265. void (*release)(struct device *dev))
  1266. {
  1267. int ret;
  1268. usb_initialize_gadget(parent, gadget, release);
  1269. ret = usb_add_gadget(gadget);
  1270. if (ret)
  1271. usb_put_gadget(gadget);
  1272. return ret;
  1273. }
  1274. EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
  1275. /**
  1276. * usb_get_gadget_udc_name - get the name of the first UDC controller
  1277. * This functions returns the name of the first UDC controller in the system.
  1278. * Please note that this interface is usefull only for legacy drivers which
  1279. * assume that there is only one UDC controller in the system and they need to
  1280. * get its name before initialization. There is no guarantee that the UDC
  1281. * of the returned name will be still available, when gadget driver registers
  1282. * itself.
  1283. *
  1284. * Returns pointer to string with UDC controller name on success, NULL
  1285. * otherwise. Caller should kfree() returned string.
  1286. */
  1287. char *usb_get_gadget_udc_name(void)
  1288. {
  1289. struct usb_udc *udc;
  1290. char *name = NULL;
  1291. /* For now we take the first available UDC */
  1292. mutex_lock(&udc_lock);
  1293. list_for_each_entry(udc, &udc_list, list) {
  1294. if (!udc->driver) {
  1295. name = kstrdup(udc->gadget->name, GFP_KERNEL);
  1296. break;
  1297. }
  1298. }
  1299. mutex_unlock(&udc_lock);
  1300. return name;
  1301. }
  1302. EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
  1303. /**
  1304. * usb_add_gadget_udc - adds a new gadget to the udc class driver list
  1305. * @parent: the parent device to this udc. Usually the controller
  1306. * driver's device.
  1307. * @gadget: the gadget to be added to the list
  1308. *
  1309. * Returns zero on success, negative errno otherwise.
  1310. */
  1311. int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
  1312. {
  1313. return usb_add_gadget_udc_release(parent, gadget, NULL);
  1314. }
  1315. EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
  1316. /**
  1317. * usb_del_gadget - deletes a gadget and unregisters its udc
  1318. * @gadget: the gadget to be deleted.
  1319. *
  1320. * This will unbind @gadget, if it is bound.
  1321. * It will not do a final usb_put_gadget().
  1322. */
  1323. void usb_del_gadget(struct usb_gadget *gadget)
  1324. {
  1325. struct usb_udc *udc = gadget->udc;
  1326. unsigned long flags;
  1327. if (!udc)
  1328. return;
  1329. dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
  1330. mutex_lock(&udc_lock);
  1331. list_del(&udc->list);
  1332. mutex_unlock(&udc_lock);
  1333. kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
  1334. sysfs_remove_link(&udc->dev.kobj, "gadget");
  1335. device_del(&gadget->dev);
  1336. /*
  1337. * Set the teardown flag before flushing the work to prevent new work
  1338. * from being scheduled while we are cleaning up.
  1339. */
  1340. spin_lock_irqsave(&gadget->state_lock, flags);
  1341. gadget->teardown = true;
  1342. spin_unlock_irqrestore(&gadget->state_lock, flags);
  1343. flush_work(&gadget->work);
  1344. ida_free(&gadget_id_numbers, gadget->id_number);
  1345. cancel_work_sync(&udc->vbus_work);
  1346. device_unregister(&udc->dev);
  1347. }
  1348. EXPORT_SYMBOL_GPL(usb_del_gadget);
  1349. /**
  1350. * usb_del_gadget_udc - unregisters a gadget
  1351. * @gadget: the gadget to be unregistered.
  1352. *
  1353. * Calls usb_del_gadget() and does a final usb_put_gadget().
  1354. */
  1355. void usb_del_gadget_udc(struct usb_gadget *gadget)
  1356. {
  1357. usb_del_gadget(gadget);
  1358. usb_put_gadget(gadget);
  1359. }
  1360. EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
  1361. /* ------------------------------------------------------------------------- */
  1362. static int gadget_match_driver(struct device *dev, const struct device_driver *drv)
  1363. {
  1364. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1365. struct usb_udc *udc = gadget->udc;
  1366. const struct usb_gadget_driver *driver = container_of(drv,
  1367. struct usb_gadget_driver, driver);
  1368. /* If the driver specifies a udc_name, it must match the UDC's name */
  1369. if (driver->udc_name &&
  1370. strcmp(driver->udc_name, dev_name(&udc->dev)) != 0)
  1371. return 0;
  1372. /* If the driver is already bound to a gadget, it doesn't match */
  1373. if (driver->is_bound)
  1374. return 0;
  1375. /* Otherwise any gadget driver matches any UDC */
  1376. return 1;
  1377. }
  1378. static int gadget_bind_driver(struct device *dev)
  1379. {
  1380. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1381. struct usb_udc *udc = gadget->udc;
  1382. struct usb_gadget_driver *driver = container_of(dev->driver,
  1383. struct usb_gadget_driver, driver);
  1384. int ret = 0;
  1385. mutex_lock(&udc_lock);
  1386. if (driver->is_bound) {
  1387. mutex_unlock(&udc_lock);
  1388. return -ENXIO; /* Driver binds to only one gadget */
  1389. }
  1390. driver->is_bound = true;
  1391. udc->driver = driver;
  1392. mutex_unlock(&udc_lock);
  1393. dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function);
  1394. usb_gadget_udc_set_speed(udc, driver->max_speed);
  1395. ret = driver->bind(udc->gadget, driver);
  1396. if (ret)
  1397. goto err_bind;
  1398. mutex_lock(&udc->connect_lock);
  1399. ret = usb_gadget_udc_start_locked(udc);
  1400. if (ret) {
  1401. mutex_unlock(&udc->connect_lock);
  1402. goto err_start;
  1403. }
  1404. usb_gadget_enable_async_callbacks(udc);
  1405. udc->allow_connect = true;
  1406. ret = usb_udc_connect_control_locked(udc);
  1407. if (ret)
  1408. goto err_connect_control;
  1409. mutex_unlock(&udc->connect_lock);
  1410. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  1411. return 0;
  1412. err_connect_control:
  1413. udc->allow_connect = false;
  1414. usb_gadget_disable_async_callbacks(udc);
  1415. if (gadget->irq)
  1416. synchronize_irq(gadget->irq);
  1417. usb_gadget_udc_stop_locked(udc);
  1418. mutex_unlock(&udc->connect_lock);
  1419. err_start:
  1420. driver->unbind(udc->gadget);
  1421. err_bind:
  1422. if (ret != -EISNAM)
  1423. dev_err(&udc->dev, "failed to start %s: %d\n",
  1424. driver->function, ret);
  1425. mutex_lock(&udc_lock);
  1426. udc->driver = NULL;
  1427. driver->is_bound = false;
  1428. mutex_unlock(&udc_lock);
  1429. return ret;
  1430. }
  1431. static void gadget_unbind_driver(struct device *dev)
  1432. {
  1433. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1434. struct usb_udc *udc = gadget->udc;
  1435. struct usb_gadget_driver *driver = udc->driver;
  1436. dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
  1437. udc->allow_connect = false;
  1438. cancel_work_sync(&udc->vbus_work);
  1439. mutex_lock(&udc->connect_lock);
  1440. usb_gadget_disconnect_locked(gadget);
  1441. usb_gadget_disable_async_callbacks(udc);
  1442. if (gadget->irq)
  1443. synchronize_irq(gadget->irq);
  1444. mutex_unlock(&udc->connect_lock);
  1445. udc->driver->unbind(gadget);
  1446. mutex_lock(&udc->connect_lock);
  1447. usb_gadget_udc_stop_locked(udc);
  1448. mutex_unlock(&udc->connect_lock);
  1449. mutex_lock(&udc_lock);
  1450. driver->is_bound = false;
  1451. udc->driver = NULL;
  1452. mutex_unlock(&udc_lock);
  1453. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  1454. }
  1455. /* ------------------------------------------------------------------------- */
  1456. int usb_gadget_register_driver_owner(struct usb_gadget_driver *driver,
  1457. struct module *owner, const char *mod_name)
  1458. {
  1459. int ret;
  1460. if (!driver || !driver->bind || !driver->setup)
  1461. return -EINVAL;
  1462. driver->driver.bus = &gadget_bus_type;
  1463. driver->driver.owner = owner;
  1464. driver->driver.mod_name = mod_name;
  1465. driver->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
  1466. ret = driver_register(&driver->driver);
  1467. if (ret) {
  1468. pr_warn("%s: driver registration failed: %d\n",
  1469. driver->function, ret);
  1470. return ret;
  1471. }
  1472. mutex_lock(&udc_lock);
  1473. if (!driver->is_bound) {
  1474. if (driver->match_existing_only) {
  1475. pr_warn("%s: couldn't find an available UDC or it's busy\n",
  1476. driver->function);
  1477. ret = -EBUSY;
  1478. } else {
  1479. pr_info("%s: couldn't find an available UDC\n",
  1480. driver->function);
  1481. ret = 0;
  1482. }
  1483. }
  1484. mutex_unlock(&udc_lock);
  1485. if (ret)
  1486. driver_unregister(&driver->driver);
  1487. return ret;
  1488. }
  1489. EXPORT_SYMBOL_GPL(usb_gadget_register_driver_owner);
  1490. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1491. {
  1492. if (!driver || !driver->unbind)
  1493. return -EINVAL;
  1494. driver_unregister(&driver->driver);
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
  1498. /* ------------------------------------------------------------------------- */
  1499. static ssize_t srp_store(struct device *dev,
  1500. struct device_attribute *attr, const char *buf, size_t n)
  1501. {
  1502. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1503. if (sysfs_streq(buf, "1"))
  1504. usb_gadget_wakeup(udc->gadget);
  1505. return n;
  1506. }
  1507. static DEVICE_ATTR_WO(srp);
  1508. static ssize_t soft_connect_store(struct device *dev,
  1509. struct device_attribute *attr, const char *buf, size_t n)
  1510. {
  1511. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1512. ssize_t ret;
  1513. device_lock(&udc->gadget->dev);
  1514. if (!udc->driver) {
  1515. dev_err(dev, "soft-connect without a gadget driver\n");
  1516. ret = -EOPNOTSUPP;
  1517. goto out;
  1518. }
  1519. if (sysfs_streq(buf, "connect")) {
  1520. mutex_lock(&udc->connect_lock);
  1521. usb_gadget_udc_start_locked(udc);
  1522. usb_gadget_connect_locked(udc->gadget);
  1523. mutex_unlock(&udc->connect_lock);
  1524. } else if (sysfs_streq(buf, "disconnect")) {
  1525. mutex_lock(&udc->connect_lock);
  1526. usb_gadget_disconnect_locked(udc->gadget);
  1527. usb_gadget_udc_stop_locked(udc);
  1528. mutex_unlock(&udc->connect_lock);
  1529. } else {
  1530. dev_err(dev, "unsupported command '%s'\n", buf);
  1531. ret = -EINVAL;
  1532. goto out;
  1533. }
  1534. ret = n;
  1535. out:
  1536. device_unlock(&udc->gadget->dev);
  1537. return ret;
  1538. }
  1539. static DEVICE_ATTR_WO(soft_connect);
  1540. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  1541. char *buf)
  1542. {
  1543. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1544. struct usb_gadget *gadget = udc->gadget;
  1545. return sprintf(buf, "%s\n", usb_state_string(gadget->state));
  1546. }
  1547. static DEVICE_ATTR_RO(state);
  1548. static ssize_t function_show(struct device *dev, struct device_attribute *attr,
  1549. char *buf)
  1550. {
  1551. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1552. struct usb_gadget_driver *drv;
  1553. int rc = 0;
  1554. mutex_lock(&udc_lock);
  1555. drv = udc->driver;
  1556. if (drv && drv->function)
  1557. rc = scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
  1558. mutex_unlock(&udc_lock);
  1559. return rc;
  1560. }
  1561. static DEVICE_ATTR_RO(function);
  1562. #define USB_UDC_SPEED_ATTR(name, param) \
  1563. ssize_t name##_show(struct device *dev, \
  1564. struct device_attribute *attr, char *buf) \
  1565. { \
  1566. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  1567. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  1568. usb_speed_string(udc->gadget->param)); \
  1569. } \
  1570. static DEVICE_ATTR_RO(name)
  1571. static USB_UDC_SPEED_ATTR(current_speed, speed);
  1572. static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
  1573. #define USB_UDC_ATTR(name) \
  1574. ssize_t name##_show(struct device *dev, \
  1575. struct device_attribute *attr, char *buf) \
  1576. { \
  1577. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  1578. struct usb_gadget *gadget = udc->gadget; \
  1579. \
  1580. return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
  1581. } \
  1582. static DEVICE_ATTR_RO(name)
  1583. static USB_UDC_ATTR(is_otg);
  1584. static USB_UDC_ATTR(is_a_peripheral);
  1585. static USB_UDC_ATTR(b_hnp_enable);
  1586. static USB_UDC_ATTR(a_hnp_support);
  1587. static USB_UDC_ATTR(a_alt_hnp_support);
  1588. static USB_UDC_ATTR(is_selfpowered);
  1589. static struct attribute *usb_udc_attrs[] = {
  1590. &dev_attr_srp.attr,
  1591. &dev_attr_soft_connect.attr,
  1592. &dev_attr_state.attr,
  1593. &dev_attr_function.attr,
  1594. &dev_attr_current_speed.attr,
  1595. &dev_attr_maximum_speed.attr,
  1596. &dev_attr_is_otg.attr,
  1597. &dev_attr_is_a_peripheral.attr,
  1598. &dev_attr_b_hnp_enable.attr,
  1599. &dev_attr_a_hnp_support.attr,
  1600. &dev_attr_a_alt_hnp_support.attr,
  1601. &dev_attr_is_selfpowered.attr,
  1602. NULL,
  1603. };
  1604. static const struct attribute_group usb_udc_attr_group = {
  1605. .attrs = usb_udc_attrs,
  1606. };
  1607. static const struct attribute_group *usb_udc_attr_groups[] = {
  1608. &usb_udc_attr_group,
  1609. NULL,
  1610. };
  1611. static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1612. {
  1613. const struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1614. int ret;
  1615. ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
  1616. if (ret) {
  1617. dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
  1618. return ret;
  1619. }
  1620. mutex_lock(&udc_lock);
  1621. if (udc->driver)
  1622. ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
  1623. udc->driver->function);
  1624. mutex_unlock(&udc_lock);
  1625. if (ret) {
  1626. dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
  1627. return ret;
  1628. }
  1629. return 0;
  1630. }
  1631. static const struct class udc_class = {
  1632. .name = "udc",
  1633. .dev_uevent = usb_udc_uevent,
  1634. };
  1635. static const struct bus_type gadget_bus_type = {
  1636. .name = "gadget",
  1637. .probe = gadget_bind_driver,
  1638. .remove = gadget_unbind_driver,
  1639. .match = gadget_match_driver,
  1640. };
  1641. static int __init usb_udc_init(void)
  1642. {
  1643. int rc;
  1644. rc = class_register(&udc_class);
  1645. if (rc)
  1646. return rc;
  1647. rc = bus_register(&gadget_bus_type);
  1648. if (rc)
  1649. class_unregister(&udc_class);
  1650. return rc;
  1651. }
  1652. subsys_initcall(usb_udc_init);
  1653. static void __exit usb_udc_exit(void)
  1654. {
  1655. bus_unregister(&gadget_bus_type);
  1656. class_unregister(&udc_class);
  1657. }
  1658. module_exit(usb_udc_exit);
  1659. MODULE_DESCRIPTION("UDC Framework");
  1660. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1661. MODULE_LICENSE("GPL v2");