zero.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/device.h>
  41. #include "g_zero.h"
  42. #include "gadget_chips.h"
  43. /*-------------------------------------------------------------------------*/
  44. /*
  45. * Kbuild is not very cooperative with respect to linking separately
  46. * compiled library objects into one module. So for now we won't use
  47. * separate compilation ... ensuring init/exit sections work to shrink
  48. * the runtime footprint, and giving us at least some parts of what
  49. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  50. */
  51. #include "f_sourcesink.c"
  52. #include "f_loopback.c"
  53. /*-------------------------------------------------------------------------*/
  54. USB_GADGET_COMPOSITE_OPTIONS();
  55. #define DRIVER_VERSION "Cinco de Mayo 2008"
  56. static const char longname[] = "Gadget Zero";
  57. unsigned buflen = 4096; /* only used for bulk endpoints */
  58. module_param(buflen, uint, 0);
  59. /*
  60. * Normally the "loopback" configuration is second (index 1) so
  61. * it's not the default. Here's where to change that order, to
  62. * work better with hosts where config changes are problematic or
  63. * controllers (like original superh) that only support one config.
  64. */
  65. static bool loopdefault = 0;
  66. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  67. /*-------------------------------------------------------------------------*/
  68. /* Thanks to NetChip Technologies for donating this product ID.
  69. *
  70. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  71. * Instead: allocate your own, using normal USB-IF procedures.
  72. */
  73. #ifndef CONFIG_USB_ZERO_HNPTEST
  74. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  75. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  76. #define DEFAULT_AUTORESUME 0
  77. #else
  78. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  79. #define DRIVER_PRODUCT_NUM 0xbadd
  80. #define DEFAULT_AUTORESUME 5
  81. #endif
  82. /* If the optional "autoresume" mode is enabled, it provides good
  83. * functional coverage for the "USBCV" test harness from USB-IF.
  84. * It's always set if OTG mode is enabled.
  85. */
  86. unsigned autoresume = DEFAULT_AUTORESUME;
  87. module_param(autoresume, uint, S_IRUGO);
  88. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  89. /*-------------------------------------------------------------------------*/
  90. static struct usb_device_descriptor device_desc = {
  91. .bLength = sizeof device_desc,
  92. .bDescriptorType = USB_DT_DEVICE,
  93. .bcdUSB = cpu_to_le16(0x0200),
  94. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  95. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  96. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  97. .bNumConfigurations = 2,
  98. };
  99. #ifdef CONFIG_USB_OTG
  100. static struct usb_otg_descriptor otg_descriptor = {
  101. .bLength = sizeof otg_descriptor,
  102. .bDescriptorType = USB_DT_OTG,
  103. /* REVISIT SRP-only hardware is possible, although
  104. * it would not be called "OTG" ...
  105. */
  106. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  107. };
  108. const struct usb_descriptor_header *otg_desc[] = {
  109. (struct usb_descriptor_header *) &otg_descriptor,
  110. NULL,
  111. };
  112. #endif
  113. /* string IDs are assigned dynamically */
  114. /* default serial number takes at least two packets */
  115. static char serial[] = "0123456789.0123456789.0123456789";
  116. #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
  117. static struct usb_string strings_dev[] = {
  118. [USB_GADGET_MANUFACTURER_IDX].s = "",
  119. [USB_GADGET_PRODUCT_IDX].s = longname,
  120. [USB_GADGET_SERIAL_IDX].s = serial,
  121. [USB_GZERO_SS_DESC].s = "source and sink data",
  122. { } /* end of list */
  123. };
  124. static struct usb_gadget_strings stringtab_dev = {
  125. .language = 0x0409, /* en-us */
  126. .strings = strings_dev,
  127. };
  128. static struct usb_gadget_strings *dev_strings[] = {
  129. &stringtab_dev,
  130. NULL,
  131. };
  132. /*-------------------------------------------------------------------------*/
  133. struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
  134. {
  135. struct usb_request *req;
  136. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  137. if (req) {
  138. if (len)
  139. req->length = len;
  140. else
  141. req->length = buflen;
  142. req->buf = kmalloc(req->length, GFP_ATOMIC);
  143. if (!req->buf) {
  144. usb_ep_free_request(ep, req);
  145. req = NULL;
  146. }
  147. }
  148. return req;
  149. }
  150. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  151. {
  152. kfree(req->buf);
  153. usb_ep_free_request(ep, req);
  154. }
  155. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  156. {
  157. int value;
  158. if (ep->driver_data) {
  159. value = usb_ep_disable(ep);
  160. if (value < 0)
  161. DBG(cdev, "disable %s --> %d\n",
  162. ep->name, value);
  163. ep->driver_data = NULL;
  164. }
  165. }
  166. void disable_endpoints(struct usb_composite_dev *cdev,
  167. struct usb_ep *in, struct usb_ep *out,
  168. struct usb_ep *iso_in, struct usb_ep *iso_out)
  169. {
  170. disable_ep(cdev, in);
  171. disable_ep(cdev, out);
  172. if (iso_in)
  173. disable_ep(cdev, iso_in);
  174. if (iso_out)
  175. disable_ep(cdev, iso_out);
  176. }
  177. /*-------------------------------------------------------------------------*/
  178. static struct timer_list autoresume_timer;
  179. static void zero_autoresume(unsigned long _c)
  180. {
  181. struct usb_composite_dev *cdev = (void *)_c;
  182. struct usb_gadget *g = cdev->gadget;
  183. /* unconfigured devices can't issue wakeups */
  184. if (!cdev->config)
  185. return;
  186. /* Normally the host would be woken up for something
  187. * more significant than just a timer firing; likely
  188. * because of some direct user request.
  189. */
  190. if (g->speed != USB_SPEED_UNKNOWN) {
  191. int status = usb_gadget_wakeup(g);
  192. INFO(cdev, "%s --> %d\n", __func__, status);
  193. }
  194. }
  195. static void zero_suspend(struct usb_composite_dev *cdev)
  196. {
  197. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  198. return;
  199. if (autoresume) {
  200. mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
  201. DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
  202. } else
  203. DBG(cdev, "%s\n", __func__);
  204. }
  205. static void zero_resume(struct usb_composite_dev *cdev)
  206. {
  207. DBG(cdev, "%s\n", __func__);
  208. del_timer(&autoresume_timer);
  209. }
  210. /*-------------------------------------------------------------------------*/
  211. static struct usb_configuration sourcesink_driver = {
  212. .label = "source/sink",
  213. .strings = sourcesink_strings,
  214. .setup = ss_config_setup,
  215. .bConfigurationValue = 3,
  216. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  217. /* .iConfiguration = DYNAMIC */
  218. };
  219. static int __init zero_bind(struct usb_composite_dev *cdev)
  220. {
  221. int status;
  222. /* Allocate string descriptor numbers ... note that string
  223. * contents can be overridden by the composite_dev glue.
  224. */
  225. status = usb_string_ids_tab(cdev, strings_dev);
  226. if (status < 0)
  227. return status;
  228. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  229. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  230. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  231. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  232. sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
  233. /* support autoresume for remote wakeup testing */
  234. sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  235. sourcesink_driver.descriptors = NULL;
  236. if (autoresume)
  237. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  238. /* support OTG systems */
  239. if (gadget_is_otg(cdev->gadget)) {
  240. sourcesink_driver.descriptors = otg_desc;
  241. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  242. }
  243. /* Register primary, then secondary configuration. Note that
  244. * SH3 only allows one config...
  245. */
  246. if (loopdefault) {
  247. loopback_add(cdev, autoresume != 0);
  248. usb_add_config(cdev, &sourcesink_driver,
  249. sourcesink_bind_config);
  250. } else {
  251. usb_add_config(cdev, &sourcesink_driver,
  252. sourcesink_bind_config);
  253. loopback_add(cdev, autoresume != 0);
  254. }
  255. usb_composite_overwrite_options(cdev, &coverwrite);
  256. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  257. return 0;
  258. }
  259. static int zero_unbind(struct usb_composite_dev *cdev)
  260. {
  261. del_timer_sync(&autoresume_timer);
  262. return 0;
  263. }
  264. static __refdata struct usb_composite_driver zero_driver = {
  265. .name = "zero",
  266. .dev = &device_desc,
  267. .strings = dev_strings,
  268. .max_speed = USB_SPEED_SUPER,
  269. .bind = zero_bind,
  270. .unbind = zero_unbind,
  271. .suspend = zero_suspend,
  272. .resume = zero_resume,
  273. };
  274. MODULE_AUTHOR("David Brownell");
  275. MODULE_LICENSE("GPL");
  276. static int __init init(void)
  277. {
  278. return usb_composite_probe(&zero_driver);
  279. }
  280. module_init(init);
  281. static void __exit cleanup(void)
  282. {
  283. usb_composite_unregister(&zero_driver);
  284. }
  285. module_exit(cleanup);