zero.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. static struct usb_string strings_dev[] = {
  117. [USB_GADGET_MANUFACTURER_IDX].s = "",
  118. [USB_GADGET_PRODUCT_IDX].s = longname,
  119. [USB_GADGET_SERIAL_IDX].s = serial,
  120. { } /* end of list */
  121. };
  122. static struct usb_gadget_strings stringtab_dev = {
  123. .language = 0x0409, /* en-us */
  124. .strings = strings_dev,
  125. };
  126. static struct usb_gadget_strings *dev_strings[] = {
  127. &stringtab_dev,
  128. NULL,
  129. };
  130. /*-------------------------------------------------------------------------*/
  131. struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
  132. {
  133. struct usb_request *req;
  134. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  135. if (req) {
  136. if (len)
  137. req->length = len;
  138. else
  139. req->length = buflen;
  140. req->buf = kmalloc(req->length, GFP_ATOMIC);
  141. if (!req->buf) {
  142. usb_ep_free_request(ep, req);
  143. req = NULL;
  144. }
  145. }
  146. return req;
  147. }
  148. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  149. {
  150. kfree(req->buf);
  151. usb_ep_free_request(ep, req);
  152. }
  153. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  154. {
  155. int value;
  156. if (ep->driver_data) {
  157. value = usb_ep_disable(ep);
  158. if (value < 0)
  159. DBG(cdev, "disable %s --> %d\n",
  160. ep->name, value);
  161. ep->driver_data = NULL;
  162. }
  163. }
  164. void disable_endpoints(struct usb_composite_dev *cdev,
  165. struct usb_ep *in, struct usb_ep *out,
  166. struct usb_ep *iso_in, struct usb_ep *iso_out)
  167. {
  168. disable_ep(cdev, in);
  169. disable_ep(cdev, out);
  170. if (iso_in)
  171. disable_ep(cdev, iso_in);
  172. if (iso_out)
  173. disable_ep(cdev, iso_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 status;
  212. /* Allocate string descriptor numbers ... note that string
  213. * contents can be overridden by the composite_dev glue.
  214. */
  215. status = usb_string_ids_tab(cdev, strings_dev);
  216. if (status < 0)
  217. return status;
  218. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  219. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  220. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  221. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  222. /* Register primary, then secondary configuration. Note that
  223. * SH3 only allows one config...
  224. */
  225. if (loopdefault) {
  226. loopback_add(cdev, autoresume != 0);
  227. sourcesink_add(cdev, autoresume != 0);
  228. } else {
  229. sourcesink_add(cdev, autoresume != 0);
  230. loopback_add(cdev, autoresume != 0);
  231. }
  232. usb_composite_overwrite_options(cdev, &coverwrite);
  233. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  234. return 0;
  235. }
  236. static int zero_unbind(struct usb_composite_dev *cdev)
  237. {
  238. del_timer_sync(&autoresume_timer);
  239. return 0;
  240. }
  241. static __refdata struct usb_composite_driver zero_driver = {
  242. .name = "zero",
  243. .dev = &device_desc,
  244. .strings = dev_strings,
  245. .max_speed = USB_SPEED_SUPER,
  246. .bind = zero_bind,
  247. .unbind = zero_unbind,
  248. .suspend = zero_suspend,
  249. .resume = zero_resume,
  250. };
  251. MODULE_AUTHOR("David Brownell");
  252. MODULE_LICENSE("GPL");
  253. static int __init init(void)
  254. {
  255. return usb_composite_probe(&zero_driver);
  256. }
  257. module_init(init);
  258. static void __exit cleanup(void)
  259. {
  260. usb_composite_unregister(&zero_driver);
  261. }
  262. module_exit(cleanup);