zero.c 9.8 KB

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