zero.c 9.4 KB

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