core.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  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. gadget->state = state;
  963. schedule_work(&gadget->work);
  964. }
  965. EXPORT_SYMBOL_GPL(usb_gadget_set_state);
  966. /* ------------------------------------------------------------------------- */
  967. /* Acquire connect_lock before calling this function. */
  968. static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock)
  969. {
  970. if (udc->vbus)
  971. return usb_gadget_connect_locked(udc->gadget);
  972. else
  973. return usb_gadget_disconnect_locked(udc->gadget);
  974. }
  975. static void vbus_event_work(struct work_struct *work)
  976. {
  977. struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work);
  978. mutex_lock(&udc->connect_lock);
  979. usb_udc_connect_control_locked(udc);
  980. mutex_unlock(&udc->connect_lock);
  981. }
  982. /**
  983. * usb_udc_vbus_handler - updates the udc core vbus status, and try to
  984. * connect or disconnect gadget
  985. * @gadget: The gadget which vbus change occurs
  986. * @status: The vbus status
  987. *
  988. * The udc driver calls it when it wants to connect or disconnect gadget
  989. * according to vbus status.
  990. *
  991. * This function can be invoked from interrupt context by irq handlers of
  992. * the gadget drivers, however, usb_udc_connect_control() has to run in
  993. * non-atomic context due to the following:
  994. * a. Some of the gadget driver implementations expect the ->pullup
  995. * callback to be invoked in non-atomic context.
  996. * b. usb_gadget_disconnect() acquires udc_lock which is a mutex.
  997. * Hence offload invocation of usb_udc_connect_control() to workqueue.
  998. */
  999. void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
  1000. {
  1001. struct usb_udc *udc = gadget->udc;
  1002. if (udc) {
  1003. udc->vbus = status;
  1004. schedule_work(&udc->vbus_work);
  1005. }
  1006. }
  1007. EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
  1008. /**
  1009. * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
  1010. * @gadget: The gadget which bus reset occurs
  1011. * @driver: The gadget driver we want to notify
  1012. *
  1013. * If the udc driver has bus reset handler, it needs to call this when the bus
  1014. * reset occurs, it notifies the gadget driver that the bus reset occurs as
  1015. * well as updates gadget state.
  1016. */
  1017. void usb_gadget_udc_reset(struct usb_gadget *gadget,
  1018. struct usb_gadget_driver *driver)
  1019. {
  1020. driver->reset(gadget);
  1021. usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
  1022. }
  1023. EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
  1024. /**
  1025. * usb_gadget_udc_start_locked - tells usb device controller to start up
  1026. * @udc: The UDC to be started
  1027. *
  1028. * This call is issued by the UDC Class driver when it's about
  1029. * to register a gadget driver to the device controller, before
  1030. * calling gadget driver's bind() method.
  1031. *
  1032. * It allows the controller to be powered off until strictly
  1033. * necessary to have it powered on.
  1034. *
  1035. * Returns zero on success, else negative errno.
  1036. *
  1037. * Caller should acquire connect_lock before invoking this function.
  1038. */
  1039. static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
  1040. __must_hold(&udc->connect_lock)
  1041. {
  1042. int ret;
  1043. if (udc->started) {
  1044. dev_err(&udc->dev, "UDC had already started\n");
  1045. return -EBUSY;
  1046. }
  1047. ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver);
  1048. if (!ret)
  1049. udc->started = true;
  1050. return ret;
  1051. }
  1052. /**
  1053. * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore
  1054. * @udc: The UDC to be stopped
  1055. *
  1056. * This call is issued by the UDC Class driver after calling
  1057. * gadget driver's unbind() method.
  1058. *
  1059. * The details are implementation specific, but it can go as
  1060. * far as powering off UDC completely and disable its data
  1061. * line pullups.
  1062. *
  1063. * Caller should acquire connect lock before invoking this function.
  1064. */
  1065. static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
  1066. __must_hold(&udc->connect_lock)
  1067. {
  1068. if (!udc->started) {
  1069. dev_err(&udc->dev, "UDC had already stopped\n");
  1070. return;
  1071. }
  1072. udc->gadget->ops->udc_stop(udc->gadget);
  1073. udc->started = false;
  1074. }
  1075. /**
  1076. * usb_gadget_udc_set_speed - tells usb device controller speed supported by
  1077. * current driver
  1078. * @udc: The device we want to set maximum speed
  1079. * @speed: The maximum speed to allowed to run
  1080. *
  1081. * This call is issued by the UDC Class driver before calling
  1082. * usb_gadget_udc_start() in order to make sure that we don't try to
  1083. * connect on speeds the gadget driver doesn't support.
  1084. */
  1085. static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
  1086. enum usb_device_speed speed)
  1087. {
  1088. struct usb_gadget *gadget = udc->gadget;
  1089. enum usb_device_speed s;
  1090. if (speed == USB_SPEED_UNKNOWN)
  1091. s = gadget->max_speed;
  1092. else
  1093. s = min(speed, gadget->max_speed);
  1094. if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate)
  1095. gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate);
  1096. else if (gadget->ops->udc_set_speed)
  1097. gadget->ops->udc_set_speed(gadget, s);
  1098. }
  1099. /**
  1100. * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks
  1101. * @udc: The UDC which should enable async callbacks
  1102. *
  1103. * This routine is used when binding gadget drivers. It undoes the effect
  1104. * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs
  1105. * (if necessary) and resume issuing callbacks.
  1106. *
  1107. * This routine will always be called in process context.
  1108. */
  1109. static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc)
  1110. {
  1111. struct usb_gadget *gadget = udc->gadget;
  1112. if (gadget->ops->udc_async_callbacks)
  1113. gadget->ops->udc_async_callbacks(gadget, true);
  1114. }
  1115. /**
  1116. * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks
  1117. * @udc: The UDC which should disable async callbacks
  1118. *
  1119. * This routine is used when unbinding gadget drivers. It prevents a race:
  1120. * The UDC driver doesn't know when the gadget driver's ->unbind callback
  1121. * runs, so unless it is told to disable asynchronous callbacks, it might
  1122. * issue a callback (such as ->disconnect) after the unbind has completed.
  1123. *
  1124. * After this function runs, the UDC driver must suppress all ->suspend,
  1125. * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver
  1126. * until async callbacks are again enabled. A simple-minded but effective
  1127. * way to accomplish this is to tell the UDC hardware not to generate any
  1128. * more IRQs.
  1129. *
  1130. * Request completion callbacks must still be issued. However, it's okay
  1131. * to defer them until the request is cancelled, since the pull-up will be
  1132. * turned off during the time period when async callbacks are disabled.
  1133. *
  1134. * This routine will always be called in process context.
  1135. */
  1136. static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc)
  1137. {
  1138. struct usb_gadget *gadget = udc->gadget;
  1139. if (gadget->ops->udc_async_callbacks)
  1140. gadget->ops->udc_async_callbacks(gadget, false);
  1141. }
  1142. /**
  1143. * usb_udc_release - release the usb_udc struct
  1144. * @dev: the dev member within usb_udc
  1145. *
  1146. * This is called by driver's core in order to free memory once the last
  1147. * reference is released.
  1148. */
  1149. static void usb_udc_release(struct device *dev)
  1150. {
  1151. struct usb_udc *udc;
  1152. udc = container_of(dev, struct usb_udc, dev);
  1153. dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
  1154. kfree(udc);
  1155. }
  1156. static const struct attribute_group *usb_udc_attr_groups[];
  1157. static void usb_udc_nop_release(struct device *dev)
  1158. {
  1159. dev_vdbg(dev, "%s\n", __func__);
  1160. }
  1161. /**
  1162. * usb_initialize_gadget - initialize a gadget and its embedded struct device
  1163. * @parent: the parent device to this udc. Usually the controller driver's
  1164. * device.
  1165. * @gadget: the gadget to be initialized.
  1166. * @release: a gadget release function.
  1167. */
  1168. void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
  1169. void (*release)(struct device *dev))
  1170. {
  1171. INIT_WORK(&gadget->work, usb_gadget_state_work);
  1172. gadget->dev.parent = parent;
  1173. if (release)
  1174. gadget->dev.release = release;
  1175. else
  1176. gadget->dev.release = usb_udc_nop_release;
  1177. device_initialize(&gadget->dev);
  1178. gadget->dev.bus = &gadget_bus_type;
  1179. }
  1180. EXPORT_SYMBOL_GPL(usb_initialize_gadget);
  1181. /**
  1182. * usb_add_gadget - adds a new gadget to the udc class driver list
  1183. * @gadget: the gadget to be added to the list.
  1184. *
  1185. * Returns zero on success, negative errno otherwise.
  1186. * Does not do a final usb_put_gadget() if an error occurs.
  1187. */
  1188. int usb_add_gadget(struct usb_gadget *gadget)
  1189. {
  1190. struct usb_udc *udc;
  1191. int ret = -ENOMEM;
  1192. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  1193. if (!udc)
  1194. goto error;
  1195. device_initialize(&udc->dev);
  1196. udc->dev.release = usb_udc_release;
  1197. udc->dev.class = &udc_class;
  1198. udc->dev.groups = usb_udc_attr_groups;
  1199. udc->dev.parent = gadget->dev.parent;
  1200. ret = dev_set_name(&udc->dev, "%s",
  1201. kobject_name(&gadget->dev.parent->kobj));
  1202. if (ret)
  1203. goto err_put_udc;
  1204. udc->gadget = gadget;
  1205. gadget->udc = udc;
  1206. mutex_init(&udc->connect_lock);
  1207. udc->started = false;
  1208. mutex_lock(&udc_lock);
  1209. list_add_tail(&udc->list, &udc_list);
  1210. mutex_unlock(&udc_lock);
  1211. INIT_WORK(&udc->vbus_work, vbus_event_work);
  1212. ret = device_add(&udc->dev);
  1213. if (ret)
  1214. goto err_unlist_udc;
  1215. usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
  1216. udc->vbus = true;
  1217. ret = ida_alloc(&gadget_id_numbers, GFP_KERNEL);
  1218. if (ret < 0)
  1219. goto err_del_udc;
  1220. gadget->id_number = ret;
  1221. dev_set_name(&gadget->dev, "gadget.%d", ret);
  1222. ret = device_add(&gadget->dev);
  1223. if (ret)
  1224. goto err_free_id;
  1225. ret = sysfs_create_link(&udc->dev.kobj,
  1226. &gadget->dev.kobj, "gadget");
  1227. if (ret)
  1228. goto err_del_gadget;
  1229. return 0;
  1230. err_del_gadget:
  1231. device_del(&gadget->dev);
  1232. err_free_id:
  1233. ida_free(&gadget_id_numbers, gadget->id_number);
  1234. err_del_udc:
  1235. flush_work(&gadget->work);
  1236. device_del(&udc->dev);
  1237. err_unlist_udc:
  1238. mutex_lock(&udc_lock);
  1239. list_del(&udc->list);
  1240. mutex_unlock(&udc_lock);
  1241. err_put_udc:
  1242. put_device(&udc->dev);
  1243. error:
  1244. return ret;
  1245. }
  1246. EXPORT_SYMBOL_GPL(usb_add_gadget);
  1247. /**
  1248. * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
  1249. * @parent: the parent device to this udc. Usually the controller driver's
  1250. * device.
  1251. * @gadget: the gadget to be added to the list.
  1252. * @release: a gadget release function.
  1253. *
  1254. * Returns zero on success, negative errno otherwise.
  1255. * Calls the gadget release function in the latter case.
  1256. */
  1257. int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
  1258. void (*release)(struct device *dev))
  1259. {
  1260. int ret;
  1261. usb_initialize_gadget(parent, gadget, release);
  1262. ret = usb_add_gadget(gadget);
  1263. if (ret)
  1264. usb_put_gadget(gadget);
  1265. return ret;
  1266. }
  1267. EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
  1268. /**
  1269. * usb_get_gadget_udc_name - get the name of the first UDC controller
  1270. * This functions returns the name of the first UDC controller in the system.
  1271. * Please note that this interface is usefull only for legacy drivers which
  1272. * assume that there is only one UDC controller in the system and they need to
  1273. * get its name before initialization. There is no guarantee that the UDC
  1274. * of the returned name will be still available, when gadget driver registers
  1275. * itself.
  1276. *
  1277. * Returns pointer to string with UDC controller name on success, NULL
  1278. * otherwise. Caller should kfree() returned string.
  1279. */
  1280. char *usb_get_gadget_udc_name(void)
  1281. {
  1282. struct usb_udc *udc;
  1283. char *name = NULL;
  1284. /* For now we take the first available UDC */
  1285. mutex_lock(&udc_lock);
  1286. list_for_each_entry(udc, &udc_list, list) {
  1287. if (!udc->driver) {
  1288. name = kstrdup(udc->gadget->name, GFP_KERNEL);
  1289. break;
  1290. }
  1291. }
  1292. mutex_unlock(&udc_lock);
  1293. return name;
  1294. }
  1295. EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
  1296. /**
  1297. * usb_add_gadget_udc - adds a new gadget to the udc class driver list
  1298. * @parent: the parent device to this udc. Usually the controller
  1299. * driver's device.
  1300. * @gadget: the gadget to be added to the list
  1301. *
  1302. * Returns zero on success, negative errno otherwise.
  1303. */
  1304. int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
  1305. {
  1306. return usb_add_gadget_udc_release(parent, gadget, NULL);
  1307. }
  1308. EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
  1309. /**
  1310. * usb_del_gadget - deletes a gadget and unregisters its udc
  1311. * @gadget: the gadget to be deleted.
  1312. *
  1313. * This will unbind @gadget, if it is bound.
  1314. * It will not do a final usb_put_gadget().
  1315. */
  1316. void usb_del_gadget(struct usb_gadget *gadget)
  1317. {
  1318. struct usb_udc *udc = gadget->udc;
  1319. if (!udc)
  1320. return;
  1321. dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
  1322. mutex_lock(&udc_lock);
  1323. list_del(&udc->list);
  1324. mutex_unlock(&udc_lock);
  1325. kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
  1326. sysfs_remove_link(&udc->dev.kobj, "gadget");
  1327. device_del(&gadget->dev);
  1328. flush_work(&gadget->work);
  1329. ida_free(&gadget_id_numbers, gadget->id_number);
  1330. cancel_work_sync(&udc->vbus_work);
  1331. device_unregister(&udc->dev);
  1332. }
  1333. EXPORT_SYMBOL_GPL(usb_del_gadget);
  1334. /**
  1335. * usb_del_gadget_udc - unregisters a gadget
  1336. * @gadget: the gadget to be unregistered.
  1337. *
  1338. * Calls usb_del_gadget() and does a final usb_put_gadget().
  1339. */
  1340. void usb_del_gadget_udc(struct usb_gadget *gadget)
  1341. {
  1342. usb_del_gadget(gadget);
  1343. usb_put_gadget(gadget);
  1344. }
  1345. EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
  1346. /* ------------------------------------------------------------------------- */
  1347. static int gadget_match_driver(struct device *dev, const struct device_driver *drv)
  1348. {
  1349. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1350. struct usb_udc *udc = gadget->udc;
  1351. const struct usb_gadget_driver *driver = container_of(drv,
  1352. struct usb_gadget_driver, driver);
  1353. /* If the driver specifies a udc_name, it must match the UDC's name */
  1354. if (driver->udc_name &&
  1355. strcmp(driver->udc_name, dev_name(&udc->dev)) != 0)
  1356. return 0;
  1357. /* If the driver is already bound to a gadget, it doesn't match */
  1358. if (driver->is_bound)
  1359. return 0;
  1360. /* Otherwise any gadget driver matches any UDC */
  1361. return 1;
  1362. }
  1363. static int gadget_bind_driver(struct device *dev)
  1364. {
  1365. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1366. struct usb_udc *udc = gadget->udc;
  1367. struct usb_gadget_driver *driver = container_of(dev->driver,
  1368. struct usb_gadget_driver, driver);
  1369. int ret = 0;
  1370. mutex_lock(&udc_lock);
  1371. if (driver->is_bound) {
  1372. mutex_unlock(&udc_lock);
  1373. return -ENXIO; /* Driver binds to only one gadget */
  1374. }
  1375. driver->is_bound = true;
  1376. udc->driver = driver;
  1377. mutex_unlock(&udc_lock);
  1378. dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function);
  1379. usb_gadget_udc_set_speed(udc, driver->max_speed);
  1380. ret = driver->bind(udc->gadget, driver);
  1381. if (ret)
  1382. goto err_bind;
  1383. mutex_lock(&udc->connect_lock);
  1384. ret = usb_gadget_udc_start_locked(udc);
  1385. if (ret) {
  1386. mutex_unlock(&udc->connect_lock);
  1387. goto err_start;
  1388. }
  1389. usb_gadget_enable_async_callbacks(udc);
  1390. udc->allow_connect = true;
  1391. ret = usb_udc_connect_control_locked(udc);
  1392. if (ret)
  1393. goto err_connect_control;
  1394. mutex_unlock(&udc->connect_lock);
  1395. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  1396. return 0;
  1397. err_connect_control:
  1398. udc->allow_connect = false;
  1399. usb_gadget_disable_async_callbacks(udc);
  1400. if (gadget->irq)
  1401. synchronize_irq(gadget->irq);
  1402. usb_gadget_udc_stop_locked(udc);
  1403. mutex_unlock(&udc->connect_lock);
  1404. err_start:
  1405. driver->unbind(udc->gadget);
  1406. err_bind:
  1407. if (ret != -EISNAM)
  1408. dev_err(&udc->dev, "failed to start %s: %d\n",
  1409. driver->function, ret);
  1410. mutex_lock(&udc_lock);
  1411. udc->driver = NULL;
  1412. driver->is_bound = false;
  1413. mutex_unlock(&udc_lock);
  1414. return ret;
  1415. }
  1416. static void gadget_unbind_driver(struct device *dev)
  1417. {
  1418. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1419. struct usb_udc *udc = gadget->udc;
  1420. struct usb_gadget_driver *driver = udc->driver;
  1421. dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
  1422. udc->allow_connect = false;
  1423. cancel_work_sync(&udc->vbus_work);
  1424. mutex_lock(&udc->connect_lock);
  1425. usb_gadget_disconnect_locked(gadget);
  1426. usb_gadget_disable_async_callbacks(udc);
  1427. if (gadget->irq)
  1428. synchronize_irq(gadget->irq);
  1429. mutex_unlock(&udc->connect_lock);
  1430. udc->driver->unbind(gadget);
  1431. mutex_lock(&udc->connect_lock);
  1432. usb_gadget_udc_stop_locked(udc);
  1433. mutex_unlock(&udc->connect_lock);
  1434. mutex_lock(&udc_lock);
  1435. driver->is_bound = false;
  1436. udc->driver = NULL;
  1437. mutex_unlock(&udc_lock);
  1438. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  1439. }
  1440. /* ------------------------------------------------------------------------- */
  1441. int usb_gadget_register_driver_owner(struct usb_gadget_driver *driver,
  1442. struct module *owner, const char *mod_name)
  1443. {
  1444. int ret;
  1445. if (!driver || !driver->bind || !driver->setup)
  1446. return -EINVAL;
  1447. driver->driver.bus = &gadget_bus_type;
  1448. driver->driver.owner = owner;
  1449. driver->driver.mod_name = mod_name;
  1450. driver->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
  1451. ret = driver_register(&driver->driver);
  1452. if (ret) {
  1453. pr_warn("%s: driver registration failed: %d\n",
  1454. driver->function, ret);
  1455. return ret;
  1456. }
  1457. mutex_lock(&udc_lock);
  1458. if (!driver->is_bound) {
  1459. if (driver->match_existing_only) {
  1460. pr_warn("%s: couldn't find an available UDC or it's busy\n",
  1461. driver->function);
  1462. ret = -EBUSY;
  1463. } else {
  1464. pr_info("%s: couldn't find an available UDC\n",
  1465. driver->function);
  1466. ret = 0;
  1467. }
  1468. }
  1469. mutex_unlock(&udc_lock);
  1470. if (ret)
  1471. driver_unregister(&driver->driver);
  1472. return ret;
  1473. }
  1474. EXPORT_SYMBOL_GPL(usb_gadget_register_driver_owner);
  1475. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1476. {
  1477. if (!driver || !driver->unbind)
  1478. return -EINVAL;
  1479. driver_unregister(&driver->driver);
  1480. return 0;
  1481. }
  1482. EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
  1483. /* ------------------------------------------------------------------------- */
  1484. static ssize_t srp_store(struct device *dev,
  1485. struct device_attribute *attr, const char *buf, size_t n)
  1486. {
  1487. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1488. if (sysfs_streq(buf, "1"))
  1489. usb_gadget_wakeup(udc->gadget);
  1490. return n;
  1491. }
  1492. static DEVICE_ATTR_WO(srp);
  1493. static ssize_t soft_connect_store(struct device *dev,
  1494. struct device_attribute *attr, const char *buf, size_t n)
  1495. {
  1496. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1497. ssize_t ret;
  1498. device_lock(&udc->gadget->dev);
  1499. if (!udc->driver) {
  1500. dev_err(dev, "soft-connect without a gadget driver\n");
  1501. ret = -EOPNOTSUPP;
  1502. goto out;
  1503. }
  1504. if (sysfs_streq(buf, "connect")) {
  1505. mutex_lock(&udc->connect_lock);
  1506. usb_gadget_udc_start_locked(udc);
  1507. usb_gadget_connect_locked(udc->gadget);
  1508. mutex_unlock(&udc->connect_lock);
  1509. } else if (sysfs_streq(buf, "disconnect")) {
  1510. mutex_lock(&udc->connect_lock);
  1511. usb_gadget_disconnect_locked(udc->gadget);
  1512. usb_gadget_udc_stop_locked(udc);
  1513. mutex_unlock(&udc->connect_lock);
  1514. } else {
  1515. dev_err(dev, "unsupported command '%s'\n", buf);
  1516. ret = -EINVAL;
  1517. goto out;
  1518. }
  1519. ret = n;
  1520. out:
  1521. device_unlock(&udc->gadget->dev);
  1522. return ret;
  1523. }
  1524. static DEVICE_ATTR_WO(soft_connect);
  1525. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  1526. char *buf)
  1527. {
  1528. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1529. struct usb_gadget *gadget = udc->gadget;
  1530. return sprintf(buf, "%s\n", usb_state_string(gadget->state));
  1531. }
  1532. static DEVICE_ATTR_RO(state);
  1533. static ssize_t function_show(struct device *dev, struct device_attribute *attr,
  1534. char *buf)
  1535. {
  1536. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1537. struct usb_gadget_driver *drv;
  1538. int rc = 0;
  1539. mutex_lock(&udc_lock);
  1540. drv = udc->driver;
  1541. if (drv && drv->function)
  1542. rc = scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
  1543. mutex_unlock(&udc_lock);
  1544. return rc;
  1545. }
  1546. static DEVICE_ATTR_RO(function);
  1547. #define USB_UDC_SPEED_ATTR(name, param) \
  1548. ssize_t name##_show(struct device *dev, \
  1549. struct device_attribute *attr, char *buf) \
  1550. { \
  1551. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  1552. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  1553. usb_speed_string(udc->gadget->param)); \
  1554. } \
  1555. static DEVICE_ATTR_RO(name)
  1556. static USB_UDC_SPEED_ATTR(current_speed, speed);
  1557. static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
  1558. #define USB_UDC_ATTR(name) \
  1559. ssize_t name##_show(struct device *dev, \
  1560. struct device_attribute *attr, char *buf) \
  1561. { \
  1562. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  1563. struct usb_gadget *gadget = udc->gadget; \
  1564. \
  1565. return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
  1566. } \
  1567. static DEVICE_ATTR_RO(name)
  1568. static USB_UDC_ATTR(is_otg);
  1569. static USB_UDC_ATTR(is_a_peripheral);
  1570. static USB_UDC_ATTR(b_hnp_enable);
  1571. static USB_UDC_ATTR(a_hnp_support);
  1572. static USB_UDC_ATTR(a_alt_hnp_support);
  1573. static USB_UDC_ATTR(is_selfpowered);
  1574. static struct attribute *usb_udc_attrs[] = {
  1575. &dev_attr_srp.attr,
  1576. &dev_attr_soft_connect.attr,
  1577. &dev_attr_state.attr,
  1578. &dev_attr_function.attr,
  1579. &dev_attr_current_speed.attr,
  1580. &dev_attr_maximum_speed.attr,
  1581. &dev_attr_is_otg.attr,
  1582. &dev_attr_is_a_peripheral.attr,
  1583. &dev_attr_b_hnp_enable.attr,
  1584. &dev_attr_a_hnp_support.attr,
  1585. &dev_attr_a_alt_hnp_support.attr,
  1586. &dev_attr_is_selfpowered.attr,
  1587. NULL,
  1588. };
  1589. static const struct attribute_group usb_udc_attr_group = {
  1590. .attrs = usb_udc_attrs,
  1591. };
  1592. static const struct attribute_group *usb_udc_attr_groups[] = {
  1593. &usb_udc_attr_group,
  1594. NULL,
  1595. };
  1596. static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1597. {
  1598. const struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  1599. int ret;
  1600. ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
  1601. if (ret) {
  1602. dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
  1603. return ret;
  1604. }
  1605. mutex_lock(&udc_lock);
  1606. if (udc->driver)
  1607. ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
  1608. udc->driver->function);
  1609. mutex_unlock(&udc_lock);
  1610. if (ret) {
  1611. dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
  1612. return ret;
  1613. }
  1614. return 0;
  1615. }
  1616. static const struct class udc_class = {
  1617. .name = "udc",
  1618. .dev_uevent = usb_udc_uevent,
  1619. };
  1620. static const struct bus_type gadget_bus_type = {
  1621. .name = "gadget",
  1622. .probe = gadget_bind_driver,
  1623. .remove = gadget_unbind_driver,
  1624. .match = gadget_match_driver,
  1625. };
  1626. static int __init usb_udc_init(void)
  1627. {
  1628. int rc;
  1629. rc = class_register(&udc_class);
  1630. if (rc)
  1631. return rc;
  1632. rc = bus_register(&gadget_bus_type);
  1633. if (rc)
  1634. class_unregister(&udc_class);
  1635. return rc;
  1636. }
  1637. subsys_initcall(usb_udc_init);
  1638. static void __exit usb_udc_exit(void)
  1639. {
  1640. bus_unregister(&gadget_bus_type);
  1641. class_unregister(&udc_class);
  1642. }
  1643. module_exit(usb_udc_exit);
  1644. MODULE_DESCRIPTION("UDC Framework");
  1645. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  1646. MODULE_LICENSE("GPL v2");