udc-core.c 12 KB

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