core.c 42 KB

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