zero.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * zero.c -- Gadget Zero, for USB development
  3. *
  4. * Copyright (C) 2003-2008 David Brownell
  5. * Copyright (C) 2008 by Nokia Corporation
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * Gadget Zero only needs two bulk endpoints, and is an example of how you
  23. * can write a hardware-agnostic gadget driver running inside a USB device.
  24. * Some hardware details are visible, but don't affect most of the driver.
  25. *
  26. * Use it with the Linux host/master side "usbtest" driver to get a basic
  27. * functional test of your device-side usb stack, or with "usb-skeleton".
  28. *
  29. * It supports two similar configurations. One sinks whatever the usb host
  30. * writes, and in return sources zeroes. The other loops whatever the host
  31. * writes back, so the host can read it.
  32. *
  33. * Many drivers will only have one configuration, letting them be much
  34. * simpler if they also don't support high speed operation (like this
  35. * driver does).
  36. *
  37. * Why is *this* driver using two configurations, rather than setting up
  38. * two interfaces with different functions? To help verify that multiple
  39. * configuration infrastucture is working correctly; also, so that it can
  40. * work with low capability USB controllers without four bulk endpoints.
  41. */
  42. /*
  43. * driver assumes self-powered hardware, and
  44. * has no way for users to trigger remote wakeup.
  45. */
  46. /* #define VERBOSE_DEBUG */
  47. #include <linux/kernel.h>
  48. #include <linux/utsname.h>
  49. #include <linux/device.h>
  50. #include "g_zero.h"
  51. #include "gadget_chips.h"
  52. /*-------------------------------------------------------------------------*/
  53. /*
  54. * Kbuild is not very cooperative with respect to linking separately
  55. * compiled library objects into one module. So for now we won't use
  56. * separate compilation ... ensuring init/exit sections work to shrink
  57. * the runtime footprint, and giving us at least some parts of what
  58. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  59. */
  60. #include "composite.c"
  61. #include "usbstring.c"
  62. #include "config.c"
  63. #include "epautoconf.c"
  64. #include "f_sourcesink.c"
  65. #include "f_loopback.c"
  66. /*-------------------------------------------------------------------------*/
  67. #define DRIVER_VERSION "Cinco de Mayo 2008"
  68. static const char longname[] = "Gadget Zero";
  69. unsigned buflen = 4096;
  70. module_param(buflen, uint, 0);
  71. /*
  72. * Normally the "loopback" configuration is second (index 1) so
  73. * it's not the default. Here's where to change that order, to
  74. * work better with hosts where config changes are problematic or
  75. * controllers (like original superh) that only support one config.
  76. */
  77. static int loopdefault = 0;
  78. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  79. /*-------------------------------------------------------------------------*/
  80. /* Thanks to NetChip Technologies for donating this product ID.
  81. *
  82. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  83. * Instead: allocate your own, using normal USB-IF procedures.
  84. */
  85. #ifndef CONFIG_USB_ZERO_HNPTEST
  86. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  87. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  88. #define DEFAULT_AUTORESUME 0
  89. #else
  90. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  91. #define DRIVER_PRODUCT_NUM 0xbadd
  92. #define DEFAULT_AUTORESUME 5
  93. #endif
  94. /* If the optional "autoresume" mode is enabled, it provides good
  95. * functional coverage for the "USBCV" test harness from USB-IF.
  96. * It's always set if OTG mode is enabled.
  97. */
  98. unsigned autoresume = DEFAULT_AUTORESUME;
  99. module_param(autoresume, uint, S_IRUGO);
  100. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  101. /*-------------------------------------------------------------------------*/
  102. static struct usb_device_descriptor device_desc = {
  103. .bLength = sizeof device_desc,
  104. .bDescriptorType = USB_DT_DEVICE,
  105. .bcdUSB = cpu_to_le16(0x0200),
  106. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  107. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  108. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  109. .bNumConfigurations = 2,
  110. };
  111. #ifdef CONFIG_USB_OTG
  112. static struct usb_otg_descriptor otg_descriptor = {
  113. .bLength = sizeof otg_descriptor,
  114. .bDescriptorType = USB_DT_OTG,
  115. /* REVISIT SRP-only hardware is possible, although
  116. * it would not be called "OTG" ...
  117. */
  118. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  119. };
  120. const struct usb_descriptor_header *otg_desc[] = {
  121. (struct usb_descriptor_header *) &otg_descriptor,
  122. NULL,
  123. };
  124. #endif
  125. /* string IDs are assigned dynamically */
  126. #define STRING_MANUFACTURER_IDX 0
  127. #define STRING_PRODUCT_IDX 1
  128. #define STRING_SERIAL_IDX 2
  129. static char manufacturer[50];
  130. /* default serial number takes at least two packets */
  131. static char serial[] = "0123456789.0123456789.0123456789";
  132. static struct usb_string strings_dev[] = {
  133. [STRING_MANUFACTURER_IDX].s = manufacturer,
  134. [STRING_PRODUCT_IDX].s = longname,
  135. [STRING_SERIAL_IDX].s = serial,
  136. { } /* end of list */
  137. };
  138. static struct usb_gadget_strings stringtab_dev = {
  139. .language = 0x0409, /* en-us */
  140. .strings = strings_dev,
  141. };
  142. static struct usb_gadget_strings *dev_strings[] = {
  143. &stringtab_dev,
  144. NULL,
  145. };
  146. /*-------------------------------------------------------------------------*/
  147. struct usb_request *alloc_ep_req(struct usb_ep *ep)
  148. {
  149. struct usb_request *req;
  150. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  151. if (req) {
  152. req->length = buflen;
  153. req->buf = kmalloc(buflen, GFP_ATOMIC);
  154. if (!req->buf) {
  155. usb_ep_free_request(ep, req);
  156. req = NULL;
  157. }
  158. }
  159. return req;
  160. }
  161. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  162. {
  163. kfree(req->buf);
  164. usb_ep_free_request(ep, req);
  165. }
  166. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  167. {
  168. int value;
  169. if (ep->driver_data) {
  170. value = usb_ep_disable(ep);
  171. if (value < 0)
  172. DBG(cdev, "disable %s --> %d\n",
  173. ep->name, value);
  174. ep->driver_data = NULL;
  175. }
  176. }
  177. void disable_endpoints(struct usb_composite_dev *cdev,
  178. struct usb_ep *in, struct usb_ep *out)
  179. {
  180. disable_ep(cdev, in);
  181. disable_ep(cdev, out);
  182. }
  183. /*-------------------------------------------------------------------------*/
  184. static struct timer_list autoresume_timer;
  185. static void zero_autoresume(unsigned long _c)
  186. {
  187. struct usb_composite_dev *cdev = (void *)_c;
  188. struct usb_gadget *g = cdev->gadget;
  189. /* unconfigured devices can't issue wakeups */
  190. if (!cdev->config)
  191. return;
  192. /* Normally the host would be woken up for something
  193. * more significant than just a timer firing; likely
  194. * because of some direct user request.
  195. */
  196. if (g->speed != USB_SPEED_UNKNOWN) {
  197. int status = usb_gadget_wakeup(g);
  198. INFO(cdev, "%s --> %d\n", __func__, status);
  199. }
  200. }
  201. static void zero_suspend(struct usb_composite_dev *cdev)
  202. {
  203. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  204. return;
  205. if (autoresume) {
  206. mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
  207. DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
  208. } else
  209. DBG(cdev, "%s\n", __func__);
  210. }
  211. static void zero_resume(struct usb_composite_dev *cdev)
  212. {
  213. DBG(cdev, "%s\n", __func__);
  214. del_timer(&autoresume_timer);
  215. }
  216. /*-------------------------------------------------------------------------*/
  217. static int __init zero_bind(struct usb_composite_dev *cdev)
  218. {
  219. int gcnum;
  220. struct usb_gadget *gadget = cdev->gadget;
  221. int id;
  222. /* Allocate string descriptor numbers ... note that string
  223. * contents can be overridden by the composite_dev glue.
  224. */
  225. id = usb_string_id(cdev);
  226. if (id < 0)
  227. return id;
  228. strings_dev[STRING_MANUFACTURER_IDX].id = id;
  229. device_desc.iManufacturer = id;
  230. id = usb_string_id(cdev);
  231. if (id < 0)
  232. return id;
  233. strings_dev[STRING_PRODUCT_IDX].id = id;
  234. device_desc.iProduct = id;
  235. id = usb_string_id(cdev);
  236. if (id < 0)
  237. return id;
  238. strings_dev[STRING_SERIAL_IDX].id = id;
  239. device_desc.iSerialNumber = id;
  240. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  241. /* Register primary, then secondary configuration. Note that
  242. * SH3 only allows one config...
  243. */
  244. if (loopdefault) {
  245. loopback_add(cdev, autoresume != 0);
  246. if (!gadget_is_sh(gadget))
  247. sourcesink_add(cdev, autoresume != 0);
  248. } else {
  249. sourcesink_add(cdev, autoresume != 0);
  250. if (!gadget_is_sh(gadget))
  251. loopback_add(cdev, autoresume != 0);
  252. }
  253. gcnum = usb_gadget_controller_number(gadget);
  254. if (gcnum >= 0)
  255. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  256. else {
  257. /* gadget zero is so simple (for now, no altsettings) that
  258. * it SHOULD NOT have problems with bulk-capable hardware.
  259. * so just warn about unrcognized controllers -- don't panic.
  260. *
  261. * things like configuration and altsetting numbering
  262. * can need hardware-specific attention though.
  263. */
  264. pr_warning("%s: controller '%s' not recognized\n",
  265. longname, gadget->name);
  266. device_desc.bcdDevice = cpu_to_le16(0x9999);
  267. }
  268. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  269. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  270. init_utsname()->sysname, init_utsname()->release,
  271. gadget->name);
  272. return 0;
  273. }
  274. static int zero_unbind(struct usb_composite_dev *cdev)
  275. {
  276. del_timer_sync(&autoresume_timer);
  277. return 0;
  278. }
  279. static struct usb_composite_driver zero_driver = {
  280. .name = "zero",
  281. .dev = &device_desc,
  282. .strings = dev_strings,
  283. .bind = zero_bind,
  284. .unbind = zero_unbind,
  285. .suspend = zero_suspend,
  286. .resume = zero_resume,
  287. };
  288. MODULE_AUTHOR("David Brownell");
  289. MODULE_LICENSE("GPL");
  290. static int __init init(void)
  291. {
  292. return usb_composite_register(&zero_driver);
  293. }
  294. module_init(init);
  295. static void __exit cleanup(void)
  296. {
  297. usb_composite_unregister(&zero_driver);
  298. }
  299. module_exit(cleanup);