udc-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. /**
  30. * struct usb_udc - describes one usb device controller
  31. * @driver - the gadget driver pointer. For use by the class code
  32. * @dev - the child device to the actual controller
  33. * @gadget - the gadget. For use by the class code
  34. * @list - for use by the udc class driver
  35. * @vbus - for udcs who care about vbus status, this value is real vbus status;
  36. * for udcs who do not care about vbus status, this value is always true
  37. *
  38. * This represents the internal data structure which is used by the UDC-class
  39. * to hold information about udc driver and gadget together.
  40. */
  41. struct usb_udc {
  42. struct usb_gadget_driver *driver;
  43. struct usb_gadget *gadget;
  44. struct device dev;
  45. struct list_head list;
  46. bool vbus;
  47. };
  48. static struct class *udc_class;
  49. static LIST_HEAD(udc_list);
  50. static LIST_HEAD(gadget_driver_pending_list);
  51. static DEFINE_MUTEX(udc_lock);
  52. static int udc_bind_to_driver(struct usb_udc *udc,
  53. struct usb_gadget_driver *driver);
  54. /* ------------------------------------------------------------------------- */
  55. #ifdef CONFIG_HAS_DMA
  56. int usb_gadget_map_request(struct usb_gadget *gadget,
  57. struct usb_request *req, int is_in)
  58. {
  59. struct device *dev = gadget->dev.parent;
  60. if (req->length == 0)
  61. return 0;
  62. if (req->num_sgs) {
  63. int mapped;
  64. mapped = dma_map_sg(dev, req->sg, req->num_sgs,
  65. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  66. if (mapped == 0) {
  67. dev_err(&gadget->dev, "failed to map SGs\n");
  68. return -EFAULT;
  69. }
  70. req->num_mapped_sgs = mapped;
  71. } else {
  72. req->dma = dma_map_single(dev, req->buf, req->length,
  73. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  74. if (dma_mapping_error(dev, req->dma)) {
  75. dev_err(dev, "failed to map buffer\n");
  76. return -EFAULT;
  77. }
  78. }
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(usb_gadget_map_request);
  82. void usb_gadget_unmap_request(struct usb_gadget *gadget,
  83. struct usb_request *req, int is_in)
  84. {
  85. if (req->length == 0)
  86. return;
  87. if (req->num_mapped_sgs) {
  88. dma_unmap_sg(gadget->dev.parent, req->sg, req->num_mapped_sgs,
  89. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  90. req->num_mapped_sgs = 0;
  91. } else {
  92. dma_unmap_single(gadget->dev.parent, req->dma, req->length,
  93. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  94. }
  95. }
  96. EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
  97. #endif /* CONFIG_HAS_DMA */
  98. /* ------------------------------------------------------------------------- */
  99. /**
  100. * usb_gadget_giveback_request - give the request back to the gadget layer
  101. * Context: in_interrupt()
  102. *
  103. * This is called by device controller drivers in order to return the
  104. * completed request back to the gadget layer.
  105. */
  106. void usb_gadget_giveback_request(struct usb_ep *ep,
  107. struct usb_request *req)
  108. {
  109. if (likely(req->status == 0))
  110. usb_led_activity(USB_LED_EVENT_GADGET);
  111. req->complete(ep, req);
  112. }
  113. EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
  114. /* ------------------------------------------------------------------------- */
  115. /**
  116. * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
  117. * in second parameter or NULL if searched endpoint not found
  118. * @g: controller to check for quirk
  119. * @name: name of searched endpoint
  120. */
  121. struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
  122. {
  123. struct usb_ep *ep;
  124. gadget_for_each_ep(ep, g) {
  125. if (!strcmp(ep->name, name))
  126. return ep;
  127. }
  128. return NULL;
  129. }
  130. EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
  131. /* ------------------------------------------------------------------------- */
  132. int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
  133. struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
  134. struct usb_ss_ep_comp_descriptor *ep_comp)
  135. {
  136. u8 type;
  137. u16 max;
  138. int num_req_streams = 0;
  139. /* endpoint already claimed? */
  140. if (ep->claimed)
  141. return 0;
  142. type = usb_endpoint_type(desc);
  143. max = 0x7ff & usb_endpoint_maxp(desc);
  144. if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
  145. return 0;
  146. if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
  147. return 0;
  148. if (max > ep->maxpacket_limit)
  149. return 0;
  150. /* "high bandwidth" works only at high speed */
  151. if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11))
  152. return 0;
  153. switch (type) {
  154. case USB_ENDPOINT_XFER_CONTROL:
  155. /* only support ep0 for portable CONTROL traffic */
  156. return 0;
  157. case USB_ENDPOINT_XFER_ISOC:
  158. if (!ep->caps.type_iso)
  159. return 0;
  160. /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
  161. if (!gadget_is_dualspeed(gadget) && max > 1023)
  162. return 0;
  163. break;
  164. case USB_ENDPOINT_XFER_BULK:
  165. if (!ep->caps.type_bulk)
  166. return 0;
  167. if (ep_comp && gadget_is_superspeed(gadget)) {
  168. /* Get the number of required streams from the
  169. * EP companion descriptor and see if the EP
  170. * matches it
  171. */
  172. num_req_streams = ep_comp->bmAttributes & 0x1f;
  173. if (num_req_streams > ep->max_streams)
  174. return 0;
  175. }
  176. break;
  177. case USB_ENDPOINT_XFER_INT:
  178. /* Bulk endpoints handle interrupt transfers,
  179. * except the toggle-quirky iso-synch kind
  180. */
  181. if (!ep->caps.type_int && !ep->caps.type_bulk)
  182. return 0;
  183. /* INT: limit 64 bytes full speed, 1024 high/super speed */
  184. if (!gadget_is_dualspeed(gadget) && max > 64)
  185. return 0;
  186. break;
  187. }
  188. return 1;
  189. }
  190. EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
  191. /* ------------------------------------------------------------------------- */
  192. static void usb_gadget_state_work(struct work_struct *work)
  193. {
  194. struct usb_gadget *gadget = work_to_gadget(work);
  195. struct usb_udc *udc = gadget->udc;
  196. if (udc)
  197. sysfs_notify(&udc->dev.kobj, NULL, "state");
  198. }
  199. void usb_gadget_set_state(struct usb_gadget *gadget,
  200. enum usb_device_state state)
  201. {
  202. gadget->state = state;
  203. schedule_work(&gadget->work);
  204. }
  205. EXPORT_SYMBOL_GPL(usb_gadget_set_state);
  206. /* ------------------------------------------------------------------------- */
  207. static void usb_udc_connect_control(struct usb_udc *udc)
  208. {
  209. if (udc->vbus)
  210. usb_gadget_connect(udc->gadget);
  211. else
  212. usb_gadget_disconnect(udc->gadget);
  213. }
  214. /**
  215. * usb_udc_vbus_handler - updates the udc core vbus status, and try to
  216. * connect or disconnect gadget
  217. * @gadget: The gadget which vbus change occurs
  218. * @status: The vbus status
  219. *
  220. * The udc driver calls it when it wants to connect or disconnect gadget
  221. * according to vbus status.
  222. */
  223. void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
  224. {
  225. struct usb_udc *udc = gadget->udc;
  226. if (udc) {
  227. udc->vbus = status;
  228. usb_udc_connect_control(udc);
  229. }
  230. }
  231. EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
  232. /**
  233. * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
  234. * @gadget: The gadget which bus reset occurs
  235. * @driver: The gadget driver we want to notify
  236. *
  237. * If the udc driver has bus reset handler, it needs to call this when the bus
  238. * reset occurs, it notifies the gadget driver that the bus reset occurs as
  239. * well as updates gadget state.
  240. */
  241. void usb_gadget_udc_reset(struct usb_gadget *gadget,
  242. struct usb_gadget_driver *driver)
  243. {
  244. driver->reset(gadget);
  245. usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
  246. }
  247. EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
  248. /**
  249. * usb_gadget_udc_start - tells usb device controller to start up
  250. * @udc: The UDC to be started
  251. *
  252. * This call is issued by the UDC Class driver when it's about
  253. * to register a gadget driver to the device controller, before
  254. * calling gadget driver's bind() method.
  255. *
  256. * It allows the controller to be powered off until strictly
  257. * necessary to have it powered on.
  258. *
  259. * Returns zero on success, else negative errno.
  260. */
  261. static inline int usb_gadget_udc_start(struct usb_udc *udc)
  262. {
  263. return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
  264. }
  265. /**
  266. * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
  267. * @gadget: The device we want to stop activity
  268. * @driver: The driver to unbind from @gadget
  269. *
  270. * This call is issued by the UDC Class driver after calling
  271. * gadget driver's unbind() method.
  272. *
  273. * The details are implementation specific, but it can go as
  274. * far as powering off UDC completely and disable its data
  275. * line pullups.
  276. */
  277. static inline void usb_gadget_udc_stop(struct usb_udc *udc)
  278. {
  279. udc->gadget->ops->udc_stop(udc->gadget);
  280. }
  281. /**
  282. * usb_udc_release - release the usb_udc struct
  283. * @dev: the dev member within usb_udc
  284. *
  285. * This is called by driver's core in order to free memory once the last
  286. * reference is released.
  287. */
  288. static void usb_udc_release(struct device *dev)
  289. {
  290. struct usb_udc *udc;
  291. udc = container_of(dev, struct usb_udc, dev);
  292. dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
  293. kfree(udc);
  294. }
  295. static const struct attribute_group *usb_udc_attr_groups[];
  296. static void usb_udc_nop_release(struct device *dev)
  297. {
  298. dev_vdbg(dev, "%s\n", __func__);
  299. }
  300. /**
  301. * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
  302. * @parent: the parent device to this udc. Usually the controller driver's
  303. * device.
  304. * @gadget: the gadget to be added to the list.
  305. * @release: a gadget release function.
  306. *
  307. * Returns zero on success, negative errno otherwise.
  308. */
  309. int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
  310. void (*release)(struct device *dev))
  311. {
  312. struct usb_udc *udc;
  313. struct usb_gadget_driver *driver;
  314. int ret = -ENOMEM;
  315. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  316. if (!udc)
  317. goto err1;
  318. dev_set_name(&gadget->dev, "gadget");
  319. INIT_WORK(&gadget->work, usb_gadget_state_work);
  320. gadget->dev.parent = parent;
  321. #ifdef CONFIG_HAS_DMA
  322. dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
  323. gadget->dev.dma_parms = parent->dma_parms;
  324. gadget->dev.dma_mask = parent->dma_mask;
  325. #endif
  326. if (release)
  327. gadget->dev.release = release;
  328. else
  329. gadget->dev.release = usb_udc_nop_release;
  330. ret = device_register(&gadget->dev);
  331. if (ret)
  332. goto err2;
  333. device_initialize(&udc->dev);
  334. udc->dev.release = usb_udc_release;
  335. udc->dev.class = udc_class;
  336. udc->dev.groups = usb_udc_attr_groups;
  337. udc->dev.parent = parent;
  338. ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
  339. if (ret)
  340. goto err3;
  341. udc->gadget = gadget;
  342. gadget->udc = udc;
  343. mutex_lock(&udc_lock);
  344. list_add_tail(&udc->list, &udc_list);
  345. ret = device_add(&udc->dev);
  346. if (ret)
  347. goto err4;
  348. usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
  349. udc->vbus = true;
  350. /* pick up one of pending gadget drivers */
  351. list_for_each_entry(driver, &gadget_driver_pending_list, pending) {
  352. if (!driver->udc_name || strcmp(driver->udc_name,
  353. dev_name(&udc->dev)) == 0) {
  354. ret = udc_bind_to_driver(udc, driver);
  355. if (ret != -EPROBE_DEFER)
  356. list_del(&driver->pending);
  357. if (ret)
  358. goto err4;
  359. break;
  360. }
  361. }
  362. mutex_unlock(&udc_lock);
  363. return 0;
  364. err4:
  365. list_del(&udc->list);
  366. mutex_unlock(&udc_lock);
  367. err3:
  368. put_device(&udc->dev);
  369. device_del(&gadget->dev);
  370. err2:
  371. put_device(&gadget->dev);
  372. kfree(udc);
  373. err1:
  374. return ret;
  375. }
  376. EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
  377. /**
  378. * usb_get_gadget_udc_name - get the name of the first UDC controller
  379. * This functions returns the name of the first UDC controller in the system.
  380. * Please note that this interface is usefull only for legacy drivers which
  381. * assume that there is only one UDC controller in the system and they need to
  382. * get its name before initialization. There is no guarantee that the UDC
  383. * of the returned name will be still available, when gadget driver registers
  384. * itself.
  385. *
  386. * Returns pointer to string with UDC controller name on success, NULL
  387. * otherwise. Caller should kfree() returned string.
  388. */
  389. char *usb_get_gadget_udc_name(void)
  390. {
  391. struct usb_udc *udc;
  392. char *name = NULL;
  393. /* For now we take the first available UDC */
  394. mutex_lock(&udc_lock);
  395. list_for_each_entry(udc, &udc_list, list) {
  396. if (!udc->driver) {
  397. name = kstrdup(udc->gadget->name, GFP_KERNEL);
  398. break;
  399. }
  400. }
  401. mutex_unlock(&udc_lock);
  402. return name;
  403. }
  404. EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
  405. /**
  406. * usb_add_gadget_udc - adds a new gadget to the udc class driver list
  407. * @parent: the parent device to this udc. Usually the controller
  408. * driver's device.
  409. * @gadget: the gadget to be added to the list
  410. *
  411. * Returns zero on success, negative errno otherwise.
  412. */
  413. int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
  414. {
  415. return usb_add_gadget_udc_release(parent, gadget, NULL);
  416. }
  417. EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
  418. static void usb_gadget_remove_driver(struct usb_udc *udc)
  419. {
  420. dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
  421. udc->driver->function);
  422. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  423. usb_gadget_disconnect(udc->gadget);
  424. udc->driver->disconnect(udc->gadget);
  425. udc->driver->unbind(udc->gadget);
  426. usb_gadget_udc_stop(udc);
  427. udc->driver = NULL;
  428. udc->dev.driver = NULL;
  429. udc->gadget->dev.driver = NULL;
  430. }
  431. /**
  432. * usb_del_gadget_udc - deletes @udc from udc_list
  433. * @gadget: the gadget to be removed.
  434. *
  435. * This, will call usb_gadget_unregister_driver() if
  436. * the @udc is still busy.
  437. */
  438. void usb_del_gadget_udc(struct usb_gadget *gadget)
  439. {
  440. struct usb_udc *udc = gadget->udc;
  441. if (!udc)
  442. return;
  443. dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
  444. mutex_lock(&udc_lock);
  445. list_del(&udc->list);
  446. if (udc->driver) {
  447. struct usb_gadget_driver *driver = udc->driver;
  448. usb_gadget_remove_driver(udc);
  449. list_add(&driver->pending, &gadget_driver_pending_list);
  450. }
  451. mutex_unlock(&udc_lock);
  452. kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
  453. flush_work(&gadget->work);
  454. device_unregister(&udc->dev);
  455. device_unregister(&gadget->dev);
  456. }
  457. EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
  458. /* ------------------------------------------------------------------------- */
  459. static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
  460. {
  461. int ret;
  462. dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
  463. driver->function);
  464. udc->driver = driver;
  465. udc->dev.driver = &driver->driver;
  466. udc->gadget->dev.driver = &driver->driver;
  467. ret = driver->bind(udc->gadget, driver);
  468. if (ret)
  469. goto err1;
  470. ret = usb_gadget_udc_start(udc);
  471. if (ret) {
  472. driver->unbind(udc->gadget);
  473. goto err1;
  474. }
  475. usb_udc_connect_control(udc);
  476. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  477. return 0;
  478. err1:
  479. if (ret != -EISNAM)
  480. dev_err(&udc->dev, "failed to start %s: %d\n",
  481. udc->driver->function, ret);
  482. udc->driver = NULL;
  483. udc->dev.driver = NULL;
  484. udc->gadget->dev.driver = NULL;
  485. return ret;
  486. }
  487. int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  488. {
  489. struct usb_udc *udc = NULL;
  490. int ret = -ENODEV;
  491. if (!driver || !driver->bind || !driver->setup)
  492. return -EINVAL;
  493. mutex_lock(&udc_lock);
  494. if (driver->udc_name) {
  495. list_for_each_entry(udc, &udc_list, list) {
  496. ret = strcmp(driver->udc_name, dev_name(&udc->dev));
  497. if (!ret)
  498. break;
  499. }
  500. if (!ret && !udc->driver)
  501. goto found;
  502. } else {
  503. list_for_each_entry(udc, &udc_list, list) {
  504. /* For now we take the first one */
  505. if (!udc->driver)
  506. goto found;
  507. }
  508. }
  509. list_add_tail(&driver->pending, &gadget_driver_pending_list);
  510. pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
  511. driver->function);
  512. mutex_unlock(&udc_lock);
  513. return 0;
  514. found:
  515. ret = udc_bind_to_driver(udc, driver);
  516. mutex_unlock(&udc_lock);
  517. return ret;
  518. }
  519. EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
  520. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  521. {
  522. struct usb_udc *udc = NULL;
  523. int ret = -ENODEV;
  524. if (!driver || !driver->unbind)
  525. return -EINVAL;
  526. mutex_lock(&udc_lock);
  527. list_for_each_entry(udc, &udc_list, list)
  528. if (udc->driver == driver) {
  529. usb_gadget_remove_driver(udc);
  530. usb_gadget_set_state(udc->gadget,
  531. USB_STATE_NOTATTACHED);
  532. ret = 0;
  533. break;
  534. }
  535. if (ret) {
  536. list_del(&driver->pending);
  537. ret = 0;
  538. }
  539. mutex_unlock(&udc_lock);
  540. return ret;
  541. }
  542. EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
  543. /* ------------------------------------------------------------------------- */
  544. static ssize_t usb_udc_srp_store(struct device *dev,
  545. struct device_attribute *attr, const char *buf, size_t n)
  546. {
  547. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  548. if (sysfs_streq(buf, "1"))
  549. usb_gadget_wakeup(udc->gadget);
  550. return n;
  551. }
  552. static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
  553. static ssize_t usb_udc_softconn_store(struct device *dev,
  554. struct device_attribute *attr, const char *buf, size_t n)
  555. {
  556. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  557. if (!udc->driver) {
  558. dev_err(dev, "soft-connect without a gadget driver\n");
  559. return -EOPNOTSUPP;
  560. }
  561. if (sysfs_streq(buf, "connect")) {
  562. usb_gadget_udc_start(udc);
  563. usb_gadget_connect(udc->gadget);
  564. } else if (sysfs_streq(buf, "disconnect")) {
  565. usb_gadget_disconnect(udc->gadget);
  566. udc->driver->disconnect(udc->gadget);
  567. usb_gadget_udc_stop(udc);
  568. } else {
  569. dev_err(dev, "unsupported command '%s'\n", buf);
  570. return -EINVAL;
  571. }
  572. return n;
  573. }
  574. static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
  575. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  576. char *buf)
  577. {
  578. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  579. struct usb_gadget *gadget = udc->gadget;
  580. return sprintf(buf, "%s\n", usb_state_string(gadget->state));
  581. }
  582. static DEVICE_ATTR_RO(state);
  583. #define USB_UDC_SPEED_ATTR(name, param) \
  584. ssize_t name##_show(struct device *dev, \
  585. struct device_attribute *attr, char *buf) \
  586. { \
  587. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  588. return snprintf(buf, PAGE_SIZE, "%s\n", \
  589. usb_speed_string(udc->gadget->param)); \
  590. } \
  591. static DEVICE_ATTR_RO(name)
  592. static USB_UDC_SPEED_ATTR(current_speed, speed);
  593. static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
  594. #define USB_UDC_ATTR(name) \
  595. ssize_t name##_show(struct device *dev, \
  596. struct device_attribute *attr, char *buf) \
  597. { \
  598. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  599. struct usb_gadget *gadget = udc->gadget; \
  600. \
  601. return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
  602. } \
  603. static DEVICE_ATTR_RO(name)
  604. static USB_UDC_ATTR(is_otg);
  605. static USB_UDC_ATTR(is_a_peripheral);
  606. static USB_UDC_ATTR(b_hnp_enable);
  607. static USB_UDC_ATTR(a_hnp_support);
  608. static USB_UDC_ATTR(a_alt_hnp_support);
  609. static USB_UDC_ATTR(is_selfpowered);
  610. static struct attribute *usb_udc_attrs[] = {
  611. &dev_attr_srp.attr,
  612. &dev_attr_soft_connect.attr,
  613. &dev_attr_state.attr,
  614. &dev_attr_current_speed.attr,
  615. &dev_attr_maximum_speed.attr,
  616. &dev_attr_is_otg.attr,
  617. &dev_attr_is_a_peripheral.attr,
  618. &dev_attr_b_hnp_enable.attr,
  619. &dev_attr_a_hnp_support.attr,
  620. &dev_attr_a_alt_hnp_support.attr,
  621. &dev_attr_is_selfpowered.attr,
  622. NULL,
  623. };
  624. static const struct attribute_group usb_udc_attr_group = {
  625. .attrs = usb_udc_attrs,
  626. };
  627. static const struct attribute_group *usb_udc_attr_groups[] = {
  628. &usb_udc_attr_group,
  629. NULL,
  630. };
  631. static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
  632. {
  633. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  634. int ret;
  635. ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
  636. if (ret) {
  637. dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
  638. return ret;
  639. }
  640. if (udc->driver) {
  641. ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
  642. udc->driver->function);
  643. if (ret) {
  644. dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
  645. return ret;
  646. }
  647. }
  648. return 0;
  649. }
  650. static int __init usb_udc_init(void)
  651. {
  652. udc_class = class_create(THIS_MODULE, "udc");
  653. if (IS_ERR(udc_class)) {
  654. pr_err("failed to create udc class --> %ld\n",
  655. PTR_ERR(udc_class));
  656. return PTR_ERR(udc_class);
  657. }
  658. udc_class->dev_uevent = usb_udc_uevent;
  659. return 0;
  660. }
  661. subsys_initcall(usb_udc_init);
  662. static void __exit usb_udc_exit(void)
  663. {
  664. class_destroy(udc_class);
  665. }
  666. module_exit(usb_udc_exit);
  667. MODULE_DESCRIPTION("UDC Framework");
  668. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  669. MODULE_LICENSE("GPL v2");