zero.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "f_sourcesink.c"
  54. #include "f_loopback.c"
  55. /*-------------------------------------------------------------------------*/
  56. USB_GADGET_COMPOSITE_OPTIONS();
  57. #define DRIVER_VERSION "Cinco de Mayo 2008"
  58. static const char longname[] = "Gadget Zero";
  59. unsigned buflen = 4096; /* only used for bulk endpoints */
  60. module_param(buflen, uint, 0);
  61. /*
  62. * Normally the "loopback" configuration is second (index 1) so
  63. * it's not the default. Here's where to change that order, to
  64. * work better with hosts where config changes are problematic or
  65. * controllers (like original superh) that only support one config.
  66. */
  67. static bool loopdefault = 0;
  68. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  69. /*-------------------------------------------------------------------------*/
  70. /* Thanks to NetChip Technologies for donating this product ID.
  71. *
  72. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  73. * Instead: allocate your own, using normal USB-IF procedures.
  74. */
  75. #ifndef CONFIG_USB_ZERO_HNPTEST
  76. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  77. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  78. #define DEFAULT_AUTORESUME 0
  79. #else
  80. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  81. #define DRIVER_PRODUCT_NUM 0xbadd
  82. #define DEFAULT_AUTORESUME 5
  83. #endif
  84. /* If the optional "autoresume" mode is enabled, it provides good
  85. * functional coverage for the "USBCV" test harness from USB-IF.
  86. * It's always set if OTG mode is enabled.
  87. */
  88. unsigned autoresume = DEFAULT_AUTORESUME;
  89. module_param(autoresume, uint, S_IRUGO);
  90. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  91. /*-------------------------------------------------------------------------*/
  92. static struct usb_device_descriptor device_desc = {
  93. .bLength = sizeof device_desc,
  94. .bDescriptorType = USB_DT_DEVICE,
  95. .bcdUSB = cpu_to_le16(0x0200),
  96. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  97. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  98. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  99. .bNumConfigurations = 2,
  100. };
  101. #ifdef CONFIG_USB_OTG
  102. static struct usb_otg_descriptor otg_descriptor = {
  103. .bLength = sizeof otg_descriptor,
  104. .bDescriptorType = USB_DT_OTG,
  105. /* REVISIT SRP-only hardware is possible, although
  106. * it would not be called "OTG" ...
  107. */
  108. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  109. };
  110. const struct usb_descriptor_header *otg_desc[] = {
  111. (struct usb_descriptor_header *) &otg_descriptor,
  112. NULL,
  113. };
  114. #endif
  115. /* string IDs are assigned dynamically */
  116. static char manufacturer[50];
  117. /* default serial number takes at least two packets */
  118. static char serial[] = "0123456789.0123456789.0123456789";
  119. static struct usb_string strings_dev[] = {
  120. [USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
  121. [USB_GADGET_PRODUCT_IDX].s = longname,
  122. [USB_GADGET_SERIAL_IDX].s = serial,
  123. { } /* end of list */
  124. };
  125. static struct usb_gadget_strings stringtab_dev = {
  126. .language = 0x0409, /* en-us */
  127. .strings = strings_dev,
  128. };
  129. static struct usb_gadget_strings *dev_strings[] = {
  130. &stringtab_dev,
  131. NULL,
  132. };
  133. /*-------------------------------------------------------------------------*/
  134. struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
  135. {
  136. struct usb_request *req;
  137. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  138. if (req) {
  139. if (len)
  140. req->length = len;
  141. else
  142. req->length = buflen;
  143. req->buf = kmalloc(req->length, GFP_ATOMIC);
  144. if (!req->buf) {
  145. usb_ep_free_request(ep, req);
  146. req = NULL;
  147. }
  148. }
  149. return req;
  150. }
  151. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  152. {
  153. kfree(req->buf);
  154. usb_ep_free_request(ep, req);
  155. }
  156. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  157. {
  158. int value;
  159. if (ep->driver_data) {
  160. value = usb_ep_disable(ep);
  161. if (value < 0)
  162. DBG(cdev, "disable %s --> %d\n",
  163. ep->name, value);
  164. ep->driver_data = NULL;
  165. }
  166. }
  167. void disable_endpoints(struct usb_composite_dev *cdev,
  168. struct usb_ep *in, struct usb_ep *out,
  169. struct usb_ep *iso_in, struct usb_ep *iso_out)
  170. {
  171. disable_ep(cdev, in);
  172. disable_ep(cdev, out);
  173. if (iso_in)
  174. disable_ep(cdev, iso_in);
  175. if (iso_out)
  176. disable_ep(cdev, iso_out);
  177. }
  178. /*-------------------------------------------------------------------------*/
  179. static struct timer_list autoresume_timer;
  180. static void zero_autoresume(unsigned long _c)
  181. {
  182. struct usb_composite_dev *cdev = (void *)_c;
  183. struct usb_gadget *g = cdev->gadget;
  184. /* unconfigured devices can't issue wakeups */
  185. if (!cdev->config)
  186. return;
  187. /* Normally the host would be woken up for something
  188. * more significant than just a timer firing; likely
  189. * because of some direct user request.
  190. */
  191. if (g->speed != USB_SPEED_UNKNOWN) {
  192. int status = usb_gadget_wakeup(g);
  193. INFO(cdev, "%s --> %d\n", __func__, status);
  194. }
  195. }
  196. static void zero_suspend(struct usb_composite_dev *cdev)
  197. {
  198. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  199. return;
  200. if (autoresume) {
  201. mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
  202. DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
  203. } else
  204. DBG(cdev, "%s\n", __func__);
  205. }
  206. static void zero_resume(struct usb_composite_dev *cdev)
  207. {
  208. DBG(cdev, "%s\n", __func__);
  209. del_timer(&autoresume_timer);
  210. }
  211. /*-------------------------------------------------------------------------*/
  212. static int __init zero_bind(struct usb_composite_dev *cdev)
  213. {
  214. int gcnum;
  215. struct usb_gadget *gadget = cdev->gadget;
  216. int status;
  217. /* Allocate string descriptor numbers ... note that string
  218. * contents can be overridden by the composite_dev glue.
  219. */
  220. status = usb_string_ids_tab(cdev, strings_dev);
  221. if (status < 0)
  222. return status;
  223. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  224. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  225. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  226. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  227. /* Register primary, then secondary configuration. Note that
  228. * SH3 only allows one config...
  229. */
  230. if (loopdefault) {
  231. loopback_add(cdev, autoresume != 0);
  232. sourcesink_add(cdev, autoresume != 0);
  233. } else {
  234. sourcesink_add(cdev, autoresume != 0);
  235. loopback_add(cdev, autoresume != 0);
  236. }
  237. gcnum = usb_gadget_controller_number(gadget);
  238. if (gcnum >= 0)
  239. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  240. else {
  241. /* gadget zero is so simple (for now, no altsettings) that
  242. * it SHOULD NOT have problems with bulk-capable hardware.
  243. * so just warn about unrcognized controllers -- don't panic.
  244. *
  245. * things like configuration and altsetting numbering
  246. * can need hardware-specific attention though.
  247. */
  248. pr_warning("%s: controller '%s' not recognized\n",
  249. longname, gadget->name);
  250. device_desc.bcdDevice = cpu_to_le16(0x9999);
  251. }
  252. usb_composite_overwrite_options(cdev, &coverwrite);
  253. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  254. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  255. init_utsname()->sysname, init_utsname()->release,
  256. gadget->name);
  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);