zero.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <linux/module.h>
  42. #include <linux/err.h>
  43. #include <linux/usb/composite.h>
  44. #include "g_zero.h"
  45. /*-------------------------------------------------------------------------*/
  46. USB_GADGET_COMPOSITE_OPTIONS();
  47. #define DRIVER_VERSION "Cinco de Mayo 2008"
  48. static const char longname[] = "Gadget Zero";
  49. /*
  50. * Normally the "loopback" configuration is second (index 1) so
  51. * it's not the default. Here's where to change that order, to
  52. * work better with hosts where config changes are problematic or
  53. * controllers (like original superh) that only support one config.
  54. */
  55. static bool loopdefault = 0;
  56. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  57. static struct usb_zero_options gzero_options = {
  58. .isoc_interval = 4,
  59. .isoc_maxpacket = 1024,
  60. .bulk_buflen = 4096,
  61. .qlen = 32,
  62. };
  63. /*-------------------------------------------------------------------------*/
  64. /* Thanks to NetChip Technologies for donating this product ID.
  65. *
  66. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  67. * Instead: allocate your own, using normal USB-IF procedures.
  68. */
  69. #ifndef CONFIG_USB_ZERO_HNPTEST
  70. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  71. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  72. #define DEFAULT_AUTORESUME 0
  73. #else
  74. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  75. #define DRIVER_PRODUCT_NUM 0xbadd
  76. #define DEFAULT_AUTORESUME 5
  77. #endif
  78. /* If the optional "autoresume" mode is enabled, it provides good
  79. * functional coverage for the "USBCV" test harness from USB-IF.
  80. * It's always set if OTG mode is enabled.
  81. */
  82. unsigned autoresume = DEFAULT_AUTORESUME;
  83. module_param(autoresume, uint, S_IRUGO);
  84. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  85. /*-------------------------------------------------------------------------*/
  86. static struct usb_device_descriptor device_desc = {
  87. .bLength = sizeof device_desc,
  88. .bDescriptorType = USB_DT_DEVICE,
  89. .bcdUSB = cpu_to_le16(0x0200),
  90. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  91. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  92. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  93. .bNumConfigurations = 2,
  94. };
  95. #ifdef CONFIG_USB_OTG
  96. static struct usb_otg_descriptor otg_descriptor = {
  97. .bLength = sizeof otg_descriptor,
  98. .bDescriptorType = USB_DT_OTG,
  99. /* REVISIT SRP-only hardware is possible, although
  100. * it would not be called "OTG" ...
  101. */
  102. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  103. };
  104. static const struct usb_descriptor_header *otg_desc[] = {
  105. (struct usb_descriptor_header *) &otg_descriptor,
  106. NULL,
  107. };
  108. #else
  109. #define otg_desc NULL
  110. #endif
  111. /* string IDs are assigned dynamically */
  112. /* default serial number takes at least two packets */
  113. static char serial[] = "0123456789.0123456789.0123456789";
  114. #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
  115. #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
  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. [USB_GZERO_SS_DESC].s = "source and sink data",
  121. [USB_GZERO_LB_DESC].s = "loop input to output",
  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. static struct timer_list autoresume_timer;
  134. static void zero_autoresume(unsigned long _c)
  135. {
  136. struct usb_composite_dev *cdev = (void *)_c;
  137. struct usb_gadget *g = cdev->gadget;
  138. /* unconfigured devices can't issue wakeups */
  139. if (!cdev->config)
  140. return;
  141. /* Normally the host would be woken up for something
  142. * more significant than just a timer firing; likely
  143. * because of some direct user request.
  144. */
  145. if (g->speed != USB_SPEED_UNKNOWN) {
  146. int status = usb_gadget_wakeup(g);
  147. INFO(cdev, "%s --> %d\n", __func__, status);
  148. }
  149. }
  150. static void zero_suspend(struct usb_composite_dev *cdev)
  151. {
  152. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  153. return;
  154. if (autoresume) {
  155. mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
  156. DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
  157. } else
  158. DBG(cdev, "%s\n", __func__);
  159. }
  160. static void zero_resume(struct usb_composite_dev *cdev)
  161. {
  162. DBG(cdev, "%s\n", __func__);
  163. del_timer(&autoresume_timer);
  164. }
  165. /*-------------------------------------------------------------------------*/
  166. static struct usb_configuration loopback_driver = {
  167. .label = "loopback",
  168. .bConfigurationValue = 2,
  169. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  170. /* .iConfiguration = DYNAMIC */
  171. };
  172. static struct usb_function *func_ss;
  173. static struct usb_function_instance *func_inst_ss;
  174. static int ss_config_setup(struct usb_configuration *c,
  175. const struct usb_ctrlrequest *ctrl)
  176. {
  177. switch (ctrl->bRequest) {
  178. case 0x5b:
  179. case 0x5c:
  180. return func_ss->setup(func_ss, ctrl);
  181. default:
  182. return -EOPNOTSUPP;
  183. }
  184. }
  185. static struct usb_configuration sourcesink_driver = {
  186. .label = "source/sink",
  187. .setup = ss_config_setup,
  188. .bConfigurationValue = 3,
  189. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  190. /* .iConfiguration = DYNAMIC */
  191. };
  192. module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
  193. module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
  194. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
  195. module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
  196. S_IRUGO|S_IWUSR);
  197. MODULE_PARM_DESC(isoc_interval, "1 - 16");
  198. module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
  199. S_IRUGO|S_IWUSR);
  200. MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
  201. module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
  202. MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
  203. module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
  204. S_IRUGO|S_IWUSR);
  205. MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
  206. static struct usb_function *func_lb;
  207. static struct usb_function_instance *func_inst_lb;
  208. module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
  209. MODULE_PARM_DESC(qlen, "depth of loopback queue");
  210. static int __init zero_bind(struct usb_composite_dev *cdev)
  211. {
  212. struct f_ss_opts *ss_opts;
  213. struct f_lb_opts *lb_opts;
  214. int status;
  215. /* Allocate string descriptor numbers ... note that string
  216. * contents can be overridden by the composite_dev glue.
  217. */
  218. status = usb_string_ids_tab(cdev, strings_dev);
  219. if (status < 0)
  220. return status;
  221. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  222. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  223. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  224. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  225. func_inst_ss = usb_get_function_instance("SourceSink");
  226. if (IS_ERR(func_inst_ss))
  227. return PTR_ERR(func_inst_ss);
  228. ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
  229. ss_opts->pattern = gzero_options.pattern;
  230. ss_opts->isoc_interval = gzero_options.isoc_interval;
  231. ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
  232. ss_opts->isoc_mult = gzero_options.isoc_mult;
  233. ss_opts->isoc_maxburst = gzero_options.isoc_maxpacket;
  234. ss_opts->bulk_buflen = gzero_options.bulk_buflen;
  235. func_ss = usb_get_function(func_inst_ss);
  236. if (IS_ERR(func_ss))
  237. goto err_put_func_inst_ss;
  238. func_inst_lb = usb_get_function_instance("Loopback");
  239. if (IS_ERR(func_inst_lb))
  240. goto err_put_func_ss;
  241. lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
  242. lb_opts->bulk_buflen = gzero_options.bulk_buflen;
  243. lb_opts->qlen = gzero_options.qlen;
  244. func_lb = usb_get_function(func_inst_lb);
  245. if (IS_ERR(func_lb)) {
  246. status = PTR_ERR(func_lb);
  247. goto err_put_func_inst_lb;
  248. }
  249. sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
  250. loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
  251. /* support autoresume for remote wakeup testing */
  252. sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  253. loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  254. sourcesink_driver.descriptors = NULL;
  255. loopback_driver.descriptors = NULL;
  256. if (autoresume) {
  257. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  258. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  259. }
  260. /* support OTG systems */
  261. if (gadget_is_otg(cdev->gadget)) {
  262. sourcesink_driver.descriptors = otg_desc;
  263. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  264. loopback_driver.descriptors = otg_desc;
  265. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  266. }
  267. /* Register primary, then secondary configuration. Note that
  268. * SH3 only allows one config...
  269. */
  270. if (loopdefault) {
  271. usb_add_config_only(cdev, &loopback_driver);
  272. usb_add_config_only(cdev, &sourcesink_driver);
  273. } else {
  274. usb_add_config_only(cdev, &sourcesink_driver);
  275. usb_add_config_only(cdev, &loopback_driver);
  276. }
  277. status = usb_add_function(&sourcesink_driver, func_ss);
  278. if (status)
  279. goto err_conf_flb;
  280. usb_ep_autoconfig_reset(cdev->gadget);
  281. status = usb_add_function(&loopback_driver, func_lb);
  282. if (status)
  283. goto err_conf_flb;
  284. usb_ep_autoconfig_reset(cdev->gadget);
  285. usb_composite_overwrite_options(cdev, &coverwrite);
  286. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  287. return 0;
  288. err_conf_flb:
  289. usb_put_function(func_lb);
  290. func_lb = NULL;
  291. err_put_func_inst_lb:
  292. usb_put_function_instance(func_inst_lb);
  293. func_inst_lb = NULL;
  294. err_put_func_ss:
  295. usb_put_function(func_ss);
  296. func_ss = NULL;
  297. err_put_func_inst_ss:
  298. usb_put_function_instance(func_inst_ss);
  299. func_inst_ss = NULL;
  300. return status;
  301. }
  302. static int zero_unbind(struct usb_composite_dev *cdev)
  303. {
  304. del_timer_sync(&autoresume_timer);
  305. if (!IS_ERR_OR_NULL(func_ss))
  306. usb_put_function(func_ss);
  307. if (!IS_ERR_OR_NULL(func_lb))
  308. usb_put_function(func_lb);
  309. return 0;
  310. }
  311. static __refdata struct usb_composite_driver zero_driver = {
  312. .name = "zero",
  313. .dev = &device_desc,
  314. .strings = dev_strings,
  315. .max_speed = USB_SPEED_SUPER,
  316. .bind = zero_bind,
  317. .unbind = zero_unbind,
  318. .suspend = zero_suspend,
  319. .resume = zero_resume,
  320. };
  321. MODULE_AUTHOR("David Brownell");
  322. MODULE_LICENSE("GPL");
  323. static int __init init(void)
  324. {
  325. return usb_composite_probe(&zero_driver);
  326. }
  327. module_init(init);
  328. static void __exit cleanup(void)
  329. {
  330. usb_composite_unregister(&zero_driver);
  331. }
  332. module_exit(cleanup);