udc-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. udc->driver->disconnect(udc->gadget);
  188. udc->driver->unbind(udc->gadget);
  189. usb_gadget_udc_stop(udc->gadget, udc->driver);
  190. usb_gadget_disconnect(udc->gadget);
  191. } else {
  192. usb_gadget_stop(udc->gadget, udc->driver);
  193. }
  194. udc->driver = NULL;
  195. udc->dev.driver = NULL;
  196. }
  197. /**
  198. * usb_del_gadget_udc - deletes @udc from udc_list
  199. * @gadget: the gadget to be removed.
  200. *
  201. * This, will call usb_gadget_unregister_driver() if
  202. * the @udc is still busy.
  203. */
  204. void usb_del_gadget_udc(struct usb_gadget *gadget)
  205. {
  206. struct usb_udc *udc = NULL;
  207. mutex_lock(&udc_lock);
  208. list_for_each_entry(udc, &udc_list, list)
  209. if (udc->gadget == gadget)
  210. goto found;
  211. dev_err(gadget->dev.parent, "gadget not registered.\n");
  212. mutex_unlock(&udc_lock);
  213. return;
  214. found:
  215. dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
  216. list_del(&udc->list);
  217. mutex_unlock(&udc_lock);
  218. if (udc->driver)
  219. usb_gadget_remove_driver(udc);
  220. kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
  221. device_unregister(&udc->dev);
  222. }
  223. EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
  224. /* ------------------------------------------------------------------------- */
  225. int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
  226. int (*bind)(struct usb_gadget *))
  227. {
  228. struct usb_udc *udc = NULL;
  229. int ret;
  230. if (!driver || !bind || !driver->setup)
  231. return -EINVAL;
  232. mutex_lock(&udc_lock);
  233. list_for_each_entry(udc, &udc_list, list) {
  234. /* For now we take the first one */
  235. if (!udc->driver)
  236. goto found;
  237. }
  238. pr_debug("couldn't find an available UDC\n");
  239. mutex_unlock(&udc_lock);
  240. return -ENODEV;
  241. found:
  242. dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
  243. driver->function);
  244. udc->driver = driver;
  245. udc->dev.driver = &driver->driver;
  246. if (udc_is_newstyle(udc)) {
  247. ret = bind(udc->gadget);
  248. if (ret)
  249. goto err1;
  250. ret = usb_gadget_udc_start(udc->gadget, driver);
  251. if (ret) {
  252. driver->unbind(udc->gadget);
  253. goto err1;
  254. }
  255. usb_gadget_connect(udc->gadget);
  256. } else {
  257. ret = usb_gadget_start(udc->gadget, driver, bind);
  258. if (ret)
  259. goto err1;
  260. }
  261. kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
  262. mutex_unlock(&udc_lock);
  263. return 0;
  264. err1:
  265. dev_err(&udc->dev, "failed to start %s: %d\n",
  266. udc->driver->function, ret);
  267. udc->driver = NULL;
  268. udc->dev.driver = NULL;
  269. mutex_unlock(&udc_lock);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
  273. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  274. {
  275. struct usb_udc *udc = NULL;
  276. int ret = -ENODEV;
  277. if (!driver || !driver->unbind)
  278. return -EINVAL;
  279. mutex_lock(&udc_lock);
  280. list_for_each_entry(udc, &udc_list, list)
  281. if (udc->driver == driver) {
  282. usb_gadget_remove_driver(udc);
  283. ret = 0;
  284. break;
  285. }
  286. mutex_unlock(&udc_lock);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
  290. /* ------------------------------------------------------------------------- */
  291. static ssize_t usb_udc_srp_store(struct device *dev,
  292. struct device_attribute *attr, const char *buf, size_t n)
  293. {
  294. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  295. if (sysfs_streq(buf, "1"))
  296. usb_gadget_wakeup(udc->gadget);
  297. return n;
  298. }
  299. static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
  300. static ssize_t usb_udc_softconn_store(struct device *dev,
  301. struct device_attribute *attr, const char *buf, size_t n)
  302. {
  303. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  304. if (sysfs_streq(buf, "connect")) {
  305. usb_gadget_connect(udc->gadget);
  306. } else if (sysfs_streq(buf, "disconnect")) {
  307. usb_gadget_disconnect(udc->gadget);
  308. } else {
  309. dev_err(dev, "unsupported command '%s'\n", buf);
  310. return -EINVAL;
  311. }
  312. return n;
  313. }
  314. static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
  315. #define USB_UDC_SPEED_ATTR(name, param) \
  316. ssize_t usb_udc_##param##_show(struct device *dev, \
  317. struct device_attribute *attr, char *buf) \
  318. { \
  319. struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
  320. return snprintf(buf, PAGE_SIZE, "%s\n", \
  321. usb_speed_string(udc->gadget->param)); \
  322. } \
  323. static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
  324. static USB_UDC_SPEED_ATTR(current_speed, speed);
  325. static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
  326. /* TODO: Scheduled for removal in 3.8. */
  327. static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
  328. struct device_attribute *attr, char *buf)
  329. {
  330. struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
  331. return snprintf(buf, PAGE_SIZE, "%d\n",
  332. gadget_is_dualspeed(udc->gadget));
  333. }
  334. static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_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 = container_of(dev, struct usb_udc, 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_IRUGO, usb_udc_##name##_show, NULL)
  345. static USB_UDC_ATTR(is_otg);
  346. static USB_UDC_ATTR(is_a_peripheral);
  347. static USB_UDC_ATTR(b_hnp_enable);
  348. static USB_UDC_ATTR(a_hnp_support);
  349. static USB_UDC_ATTR(a_alt_hnp_support);
  350. static struct attribute *usb_udc_attrs[] = {
  351. &dev_attr_srp.attr,
  352. &dev_attr_soft_connect.attr,
  353. &dev_attr_current_speed.attr,
  354. &dev_attr_maximum_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. return 0;
  399. }
  400. subsys_initcall(usb_udc_init);
  401. static void __exit usb_udc_exit(void)
  402. {
  403. class_destroy(udc_class);
  404. }
  405. module_exit(usb_udc_exit);
  406. MODULE_DESCRIPTION("UDC Framework");
  407. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  408. MODULE_LICENSE("GPL v2");