udc-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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/usb/ch9.h>
  25. #include <linux/usb/gadget.h>
  26. /**
  27. * struct usb_udc - describes one usb device controller
  28. * @driver - the gadget driver pointer. For use by the class code
  29. * @dev - the child device to the actual controller
  30. * @gadget - the gadget. For use by the class code
  31. * @list - for use by the udc class driver
  32. *
  33. * This represents the internal data structure which is used by the UDC-class
  34. * to hold information about udc driver and gadget together.
  35. */
  36. struct usb_udc {
  37. struct usb_gadget_driver *driver;
  38. struct usb_gadget *gadget;
  39. struct device dev;
  40. struct list_head list;
  41. };
  42. static struct class *udc_class;
  43. static LIST_HEAD(udc_list);
  44. static DEFINE_MUTEX(udc_lock);
  45. /* ------------------------------------------------------------------------- */
  46. /**
  47. * usb_gadget_start - tells usb device controller to start up
  48. * @gadget: The gadget we want to get started
  49. * @driver: The driver we want to bind to @gadget
  50. * @bind: The bind function for @driver
  51. *
  52. * This call is issued by the UDC Class driver when it's about
  53. * to register a gadget driver to the device controller, before
  54. * calling gadget driver's bind() method.
  55. *
  56. * It allows the controller to be powered off until strictly
  57. * necessary to have it powered on.
  58. *
  59. * Returns zero on success, else negative errno.
  60. */
  61. static inline int usb_gadget_start(struct usb_gadget *gadget,
  62. struct usb_gadget_driver *driver,
  63. int (*bind)(struct usb_gadget *))
  64. {
  65. return gadget->ops->start(driver, bind);
  66. }
  67. /**
  68. * usb_gadget_udc_start - tells usb device controller to start up
  69. * @gadget: The gadget we want to get started
  70. * @driver: The driver we want to bind to @gadget
  71. *
  72. * This call is issued by the UDC Class driver when it's about
  73. * to register a gadget driver to the device controller, before
  74. * calling gadget driver's bind() method.
  75. *
  76. * It allows the controller to be powered off until strictly
  77. * necessary to have it powered on.
  78. *
  79. * Returns zero on success, else negative errno.
  80. */
  81. static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
  82. struct usb_gadget_driver *driver)
  83. {
  84. return gadget->ops->udc_start(gadget, driver);
  85. }
  86. /**
  87. * usb_gadget_stop - tells usb device controller we don't need it anymore
  88. * @gadget: The device we want to stop activity
  89. * @driver: The driver to unbind from @gadget
  90. *
  91. * This call is issued by the UDC Class driver after calling
  92. * gadget driver's unbind() method.
  93. *
  94. * The details are implementation specific, but it can go as
  95. * far as powering off UDC completely and disable its data
  96. * line pullups.
  97. */
  98. static inline void usb_gadget_stop(struct usb_gadget *gadget,
  99. struct usb_gadget_driver *driver)
  100. {
  101. gadget->ops->stop(driver);
  102. }
  103. /**
  104. * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
  105. * @gadget: The device we want to stop activity
  106. * @driver: The driver to unbind from @gadget
  107. *
  108. * This call is issued by the UDC Class driver after calling
  109. * gadget driver's unbind() method.
  110. *
  111. * The details are implementation specific, but it can go as
  112. * far as powering off UDC completely and disable its data
  113. * line pullups.
  114. */
  115. static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
  116. struct usb_gadget_driver *driver)
  117. {
  118. gadget->ops->udc_stop(gadget, driver);
  119. }
  120. /**
  121. * usb_udc_release - release the usb_udc struct
  122. * @dev: the dev member within usb_udc
  123. *
  124. * This is called by driver's core in order to free memory once the last
  125. * reference is released.
  126. */
  127. static void usb_udc_release(struct device *dev)
  128. {
  129. struct usb_udc *udc;
  130. udc = container_of(dev, struct usb_udc, dev);
  131. dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
  132. kfree(udc);
  133. }
  134. static const struct attribute_group *usb_udc_attr_groups[];
  135. /**
  136. * usb_add_gadget_udc - adds a new gadget to the udc class driver list
  137. * @parent: the parent device to this udc. Usually the controller
  138. * driver's device.
  139. * @gadget: the gadget to be added to the list
  140. *
  141. * Returns zero on success, negative errno otherwise.
  142. */
  143. int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
  144. {
  145. struct usb_udc *udc;
  146. int ret = -ENOMEM;
  147. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  148. if (!udc)
  149. goto err1;
  150. device_initialize(&udc->dev);
  151. udc->dev.release = usb_udc_release;
  152. udc->dev.class = udc_class;
  153. udc->dev.groups = usb_udc_attr_groups;
  154. udc->dev.parent = parent;
  155. ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
  156. if (ret)
  157. goto err2;
  158. udc->gadget = gadget;
  159. mutex_lock(&udc_lock);
  160. list_add_tail(&udc->list, &udc_list);
  161. ret = device_add(&udc->dev);
  162. if (ret)
  163. goto err3;
  164. mutex_unlock(&udc_lock);
  165. return 0;
  166. err3:
  167. list_del(&udc->list);
  168. mutex_unlock(&udc_lock);
  169. err2:
  170. put_device(&udc->dev);
  171. err1:
  172. return ret;
  173. }
  174. EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
  175. static int udc_is_newstyle(struct usb_udc *udc)
  176. {
  177. if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop)
  178. return 1;
  179. return 0;
  180. }
  181. static void usb_gadget_remove_driver(struct usb_udc *udc)
  182. {
  183. dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
  184. udc->gadget->name);
  185. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  186. if (udc_is_newstyle(udc)) {
  187. usb_gadget_disconnect(udc->gadget);
  188. udc->driver->unbind(udc->gadget);
  189. usb_gadget_udc_stop(udc->gadget, udc->driver);
  190. } else {
  191. usb_gadget_stop(udc->gadget, udc->driver);
  192. }
  193. udc->driver = NULL;
  194. udc->dev.driver = NULL;
  195. }
  196. /**
  197. * usb_del_gadget_udc - deletes @udc from udc_list
  198. * @gadget: the gadget to be removed.
  199. *
  200. * This, will call usb_gadget_unregister_driver() if
  201. * the @udc is still busy.
  202. */
  203. void usb_del_gadget_udc(struct usb_gadget *gadget)
  204. {
  205. struct usb_udc *udc = NULL;
  206. mutex_lock(&udc_lock);
  207. list_for_each_entry(udc, &udc_list, list)
  208. if (udc->gadget == gadget)
  209. goto found;
  210. dev_err(gadget->dev.parent, "gadget not registered.\n");
  211. mutex_unlock(&udc_lock);
  212. return;
  213. found:
  214. dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
  215. list_del(&udc->list);
  216. mutex_unlock(&udc_lock);
  217. if (udc->driver)
  218. usb_gadget_remove_driver(udc);
  219. kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
  220. device_unregister(&udc->dev);
  221. }
  222. EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
  223. /* ------------------------------------------------------------------------- */
  224. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  225. int (*bind)(struct usb_gadget *))
  226. {
  227. struct usb_udc *udc = NULL;
  228. int ret;
  229. if (!driver || !bind || !driver->setup)
  230. return -EINVAL;
  231. mutex_lock(&udc_lock);
  232. list_for_each_entry(udc, &udc_list, list) {
  233. /* For now we take the first one */
  234. if (!udc->driver)
  235. goto found;
  236. }
  237. pr_debug("couldn't find an available UDC\n");
  238. mutex_unlock(&udc_lock);
  239. return -ENODEV;
  240. found:
  241. dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
  242. driver->function);
  243. udc->driver = driver;
  244. udc->dev.driver = &driver->driver;
  245. if (udc_is_newstyle(udc)) {
  246. ret = bind(udc->gadget);
  247. if (ret)
  248. goto err1;
  249. ret = usb_gadget_udc_start(udc->gadget, driver);
  250. if (ret) {
  251. driver->unbind(udc->gadget);
  252. goto err1;
  253. }
  254. usb_gadget_connect(udc->gadget);
  255. } else {
  256. ret = usb_gadget_start(udc->gadget, driver, bind);
  257. if (ret)
  258. goto err1;
  259. }
  260. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  261. mutex_unlock(&udc_lock);
  262. return 0;
  263. err1:
  264. dev_err(&udc->dev, "failed to start %s: %d\n",
  265. udc->driver->function, ret);
  266. udc->driver = NULL;
  267. udc->dev.driver = NULL;
  268. mutex_unlock(&udc_lock);
  269. return ret;
  270. }
  271. EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
  272. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  273. {
  274. struct usb_udc *udc = NULL;
  275. int ret = -ENODEV;
  276. if (!driver || !driver->unbind)
  277. return -EINVAL;
  278. mutex_lock(&udc_lock);
  279. list_for_each_entry(udc, &udc_list, list)
  280. if (udc->driver == driver) {
  281. usb_gadget_remove_driver(udc);
  282. ret = 0;
  283. break;
  284. }
  285. mutex_unlock(&udc_lock);
  286. return ret;
  287. }
  288. EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
  289. /* ------------------------------------------------------------------------- */
  290. static ssize_t usb_udc_srp_store(struct device *dev,
  291. struct device_attribute *attr, const char *buf, size_t n)
  292. {
  293. struct usb_udc *udc = dev_get_drvdata(dev);
  294. if (sysfs_streq(buf, "1"))
  295. usb_gadget_wakeup(udc->gadget);
  296. return n;
  297. }
  298. static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
  299. static ssize_t usb_udc_softconn_store(struct device *dev,
  300. struct device_attribute *attr, const char *buf, size_t n)
  301. {
  302. struct usb_udc *udc = dev_get_drvdata(dev);
  303. if (sysfs_streq(buf, "connect")) {
  304. usb_gadget_connect(udc->gadget);
  305. } else if (sysfs_streq(buf, "disconnect")) {
  306. usb_gadget_disconnect(udc->gadget);
  307. } else {
  308. dev_err(dev, "unsupported command '%s'\n", buf);
  309. return -EINVAL;
  310. }
  311. return n;
  312. }
  313. static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
  314. static ssize_t usb_udc_speed_show(struct device *dev,
  315. struct device_attribute *attr, char *buf)
  316. {
  317. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  318. struct usb_gadget *gadget = udc->gadget;
  319. switch (gadget->speed) {
  320. case USB_SPEED_LOW:
  321. return snprintf(buf, PAGE_SIZE, "low-speed\n");
  322. case USB_SPEED_FULL:
  323. return snprintf(buf, PAGE_SIZE, "full-speed\n");
  324. case USB_SPEED_HIGH:
  325. return snprintf(buf, PAGE_SIZE, "high-speed\n");
  326. case USB_SPEED_WIRELESS:
  327. return snprintf(buf, PAGE_SIZE, "wireless\n");
  328. case USB_SPEED_SUPER:
  329. return snprintf(buf, PAGE_SIZE, "super-speed\n");
  330. case USB_SPEED_UNKNOWN: /* FALLTHROUGH */
  331. default:
  332. return snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
  333. }
  334. }
  335. static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL);
  336. #define USB_UDC_ATTR(name) \
  337. ssize_t usb_udc_##name##_show(struct device *dev, \
  338. struct device_attribute *attr, char *buf) \
  339. { \
  340. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  341. struct usb_gadget *gadget = udc->gadget; \
  342. \
  343. return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
  344. } \
  345. static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL)
  346. static USB_UDC_ATTR(is_dualspeed);
  347. static USB_UDC_ATTR(is_otg);
  348. static USB_UDC_ATTR(is_a_peripheral);
  349. static USB_UDC_ATTR(b_hnp_enable);
  350. static USB_UDC_ATTR(a_hnp_support);
  351. static USB_UDC_ATTR(a_alt_hnp_support);
  352. static struct attribute *usb_udc_attrs[] = {
  353. &dev_attr_srp.attr,
  354. &dev_attr_soft_connect.attr,
  355. &dev_attr_speed.attr,
  356. &dev_attr_is_dualspeed.attr,
  357. &dev_attr_is_otg.attr,
  358. &dev_attr_is_a_peripheral.attr,
  359. &dev_attr_b_hnp_enable.attr,
  360. &dev_attr_a_hnp_support.attr,
  361. &dev_attr_a_alt_hnp_support.attr,
  362. NULL,
  363. };
  364. static const struct attribute_group usb_udc_attr_group = {
  365. .attrs = usb_udc_attrs,
  366. };
  367. static const struct attribute_group *usb_udc_attr_groups[] = {
  368. &usb_udc_attr_group,
  369. NULL,
  370. };
  371. static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
  372. {
  373. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  374. int ret;
  375. ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
  376. if (ret) {
  377. dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
  378. return ret;
  379. }
  380. if (udc->driver) {
  381. ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
  382. udc->driver->function);
  383. if (ret) {
  384. dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
  385. return ret;
  386. }
  387. }
  388. return 0;
  389. }
  390. static int __init usb_udc_init(void)
  391. {
  392. udc_class = class_create(THIS_MODULE, "udc");
  393. if (IS_ERR(udc_class)) {
  394. pr_err("failed to create udc class --> %ld\n",
  395. PTR_ERR(udc_class));
  396. return PTR_ERR(udc_class);
  397. }
  398. udc_class->dev_uevent = usb_udc_uevent;
  399. return 0;
  400. }
  401. subsys_initcall(usb_udc_init);
  402. static void __exit usb_udc_exit(void)
  403. {
  404. class_destroy(udc_class);
  405. }
  406. module_exit(usb_udc_exit);
  407. MODULE_DESCRIPTION("UDC Framework");
  408. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  409. MODULE_LICENSE("GPL v2");