core.c 44 KB

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