zero.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 infrastructure 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 = GZERO_ISOC_INTERVAL,
  59. .isoc_maxpacket = GZERO_ISOC_MAXPACKET,
  60. .bulk_buflen = GZERO_BULK_BUFLEN,
  61. .qlen = GZERO_QLEN,
  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. static unsigned autoresume = DEFAULT_AUTORESUME;
  83. module_param(autoresume, uint, S_IRUGO);
  84. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  85. /* Maximum Autoresume time */
  86. static unsigned max_autoresume;
  87. module_param(max_autoresume, uint, S_IRUGO);
  88. MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
  89. /* Interval between two remote wakeups */
  90. static unsigned autoresume_interval_ms;
  91. module_param(autoresume_interval_ms, uint, S_IRUGO);
  92. MODULE_PARM_DESC(autoresume_interval_ms,
  93. "milliseconds to increase successive wakeup delays");
  94. static unsigned autoresume_step_ms;
  95. /*-------------------------------------------------------------------------*/
  96. static struct usb_device_descriptor device_desc = {
  97. .bLength = sizeof device_desc,
  98. .bDescriptorType = USB_DT_DEVICE,
  99. .bcdUSB = cpu_to_le16(0x0200),
  100. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  101. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  102. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  103. .bNumConfigurations = 2,
  104. };
  105. #ifdef CONFIG_USB_OTG
  106. static struct usb_otg_descriptor otg_descriptor = {
  107. .bLength = sizeof otg_descriptor,
  108. .bDescriptorType = USB_DT_OTG,
  109. /* REVISIT SRP-only hardware is possible, although
  110. * it would not be called "OTG" ...
  111. */
  112. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  113. };
  114. static const struct usb_descriptor_header *otg_desc[] = {
  115. (struct usb_descriptor_header *) &otg_descriptor,
  116. NULL,
  117. };
  118. #else
  119. #define otg_desc NULL
  120. #endif
  121. /* string IDs are assigned dynamically */
  122. /* default serial number takes at least two packets */
  123. static char serial[] = "0123456789.0123456789.0123456789";
  124. #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
  125. #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
  126. static struct usb_string strings_dev[] = {
  127. [USB_GADGET_MANUFACTURER_IDX].s = "",
  128. [USB_GADGET_PRODUCT_IDX].s = longname,
  129. [USB_GADGET_SERIAL_IDX].s = serial,
  130. [USB_GZERO_SS_DESC].s = "source and sink data",
  131. [USB_GZERO_LB_DESC].s = "loop input to output",
  132. { } /* end of list */
  133. };
  134. static struct usb_gadget_strings stringtab_dev = {
  135. .language = 0x0409, /* en-us */
  136. .strings = strings_dev,
  137. };
  138. static struct usb_gadget_strings *dev_strings[] = {
  139. &stringtab_dev,
  140. NULL,
  141. };
  142. /*-------------------------------------------------------------------------*/
  143. static struct timer_list autoresume_timer;
  144. static void zero_autoresume(unsigned long _c)
  145. {
  146. struct usb_composite_dev *cdev = (void *)_c;
  147. struct usb_gadget *g = cdev->gadget;
  148. /* unconfigured devices can't issue wakeups */
  149. if (!cdev->config)
  150. return;
  151. /* Normally the host would be woken up for something
  152. * more significant than just a timer firing; likely
  153. * because of some direct user request.
  154. */
  155. if (g->speed != USB_SPEED_UNKNOWN) {
  156. int status = usb_gadget_wakeup(g);
  157. INFO(cdev, "%s --> %d\n", __func__, status);
  158. }
  159. }
  160. static void zero_suspend(struct usb_composite_dev *cdev)
  161. {
  162. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  163. return;
  164. if (autoresume) {
  165. if (max_autoresume &&
  166. (autoresume_step_ms > max_autoresume * 1000))
  167. autoresume_step_ms = autoresume * 1000;
  168. mod_timer(&autoresume_timer, jiffies +
  169. msecs_to_jiffies(autoresume_step_ms));
  170. DBG(cdev, "suspend, wakeup in %d milliseconds\n",
  171. autoresume_step_ms);
  172. autoresume_step_ms += autoresume_interval_ms;
  173. } else
  174. DBG(cdev, "%s\n", __func__);
  175. }
  176. static void zero_resume(struct usb_composite_dev *cdev)
  177. {
  178. DBG(cdev, "%s\n", __func__);
  179. del_timer(&autoresume_timer);
  180. }
  181. /*-------------------------------------------------------------------------*/
  182. static struct usb_configuration loopback_driver = {
  183. .label = "loopback",
  184. .bConfigurationValue = 2,
  185. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  186. /* .iConfiguration = DYNAMIC */
  187. };
  188. static struct usb_function *func_ss;
  189. static struct usb_function_instance *func_inst_ss;
  190. static int ss_config_setup(struct usb_configuration *c,
  191. const struct usb_ctrlrequest *ctrl)
  192. {
  193. switch (ctrl->bRequest) {
  194. case 0x5b:
  195. case 0x5c:
  196. return func_ss->setup(func_ss, ctrl);
  197. default:
  198. return -EOPNOTSUPP;
  199. }
  200. }
  201. static struct usb_configuration sourcesink_driver = {
  202. .label = "source/sink",
  203. .setup = ss_config_setup,
  204. .bConfigurationValue = 3,
  205. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  206. /* .iConfiguration = DYNAMIC */
  207. };
  208. module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
  209. module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
  210. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
  211. module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
  212. S_IRUGO|S_IWUSR);
  213. MODULE_PARM_DESC(isoc_interval, "1 - 16");
  214. module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
  215. S_IRUGO|S_IWUSR);
  216. MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
  217. module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
  218. MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
  219. module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
  220. S_IRUGO|S_IWUSR);
  221. MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
  222. static struct usb_function *func_lb;
  223. static struct usb_function_instance *func_inst_lb;
  224. module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
  225. MODULE_PARM_DESC(qlen, "depth of loopback queue");
  226. static int __init zero_bind(struct usb_composite_dev *cdev)
  227. {
  228. struct f_ss_opts *ss_opts;
  229. struct f_lb_opts *lb_opts;
  230. int status;
  231. /* Allocate string descriptor numbers ... note that string
  232. * contents can be overridden by the composite_dev glue.
  233. */
  234. status = usb_string_ids_tab(cdev, strings_dev);
  235. if (status < 0)
  236. return status;
  237. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  238. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  239. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  240. setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
  241. func_inst_ss = usb_get_function_instance("SourceSink");
  242. if (IS_ERR(func_inst_ss))
  243. return PTR_ERR(func_inst_ss);
  244. ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
  245. ss_opts->pattern = gzero_options.pattern;
  246. ss_opts->isoc_interval = gzero_options.isoc_interval;
  247. ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
  248. ss_opts->isoc_mult = gzero_options.isoc_mult;
  249. ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
  250. ss_opts->bulk_buflen = gzero_options.bulk_buflen;
  251. func_ss = usb_get_function(func_inst_ss);
  252. if (IS_ERR(func_ss)) {
  253. status = PTR_ERR(func_ss);
  254. goto err_put_func_inst_ss;
  255. }
  256. func_inst_lb = usb_get_function_instance("Loopback");
  257. if (IS_ERR(func_inst_lb)) {
  258. status = PTR_ERR(func_inst_lb);
  259. goto err_put_func_ss;
  260. }
  261. lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
  262. lb_opts->bulk_buflen = gzero_options.bulk_buflen;
  263. lb_opts->qlen = gzero_options.qlen;
  264. func_lb = usb_get_function(func_inst_lb);
  265. if (IS_ERR(func_lb)) {
  266. status = PTR_ERR(func_lb);
  267. goto err_put_func_inst_lb;
  268. }
  269. sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
  270. loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
  271. /* support autoresume for remote wakeup testing */
  272. sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  273. loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  274. sourcesink_driver.descriptors = NULL;
  275. loopback_driver.descriptors = NULL;
  276. if (autoresume) {
  277. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  278. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  279. autoresume_step_ms = autoresume * 1000;
  280. }
  281. /* support OTG systems */
  282. if (gadget_is_otg(cdev->gadget)) {
  283. sourcesink_driver.descriptors = otg_desc;
  284. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  285. loopback_driver.descriptors = otg_desc;
  286. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  287. }
  288. /* Register primary, then secondary configuration. Note that
  289. * SH3 only allows one config...
  290. */
  291. if (loopdefault) {
  292. usb_add_config_only(cdev, &loopback_driver);
  293. usb_add_config_only(cdev, &sourcesink_driver);
  294. } else {
  295. usb_add_config_only(cdev, &sourcesink_driver);
  296. usb_add_config_only(cdev, &loopback_driver);
  297. }
  298. status = usb_add_function(&sourcesink_driver, func_ss);
  299. if (status)
  300. goto err_conf_flb;
  301. usb_ep_autoconfig_reset(cdev->gadget);
  302. status = usb_add_function(&loopback_driver, func_lb);
  303. if (status)
  304. goto err_conf_flb;
  305. usb_ep_autoconfig_reset(cdev->gadget);
  306. usb_composite_overwrite_options(cdev, &coverwrite);
  307. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  308. return 0;
  309. err_conf_flb:
  310. usb_put_function(func_lb);
  311. func_lb = NULL;
  312. err_put_func_inst_lb:
  313. usb_put_function_instance(func_inst_lb);
  314. func_inst_lb = NULL;
  315. err_put_func_ss:
  316. usb_put_function(func_ss);
  317. func_ss = NULL;
  318. err_put_func_inst_ss:
  319. usb_put_function_instance(func_inst_ss);
  320. func_inst_ss = NULL;
  321. return status;
  322. }
  323. static int zero_unbind(struct usb_composite_dev *cdev)
  324. {
  325. del_timer_sync(&autoresume_timer);
  326. if (!IS_ERR_OR_NULL(func_ss))
  327. usb_put_function(func_ss);
  328. usb_put_function_instance(func_inst_ss);
  329. if (!IS_ERR_OR_NULL(func_lb))
  330. usb_put_function(func_lb);
  331. usb_put_function_instance(func_inst_lb);
  332. return 0;
  333. }
  334. static __refdata struct usb_composite_driver zero_driver = {
  335. .name = "zero",
  336. .dev = &device_desc,
  337. .strings = dev_strings,
  338. .max_speed = USB_SPEED_SUPER,
  339. .bind = zero_bind,
  340. .unbind = zero_unbind,
  341. .suspend = zero_suspend,
  342. .resume = zero_resume,
  343. };
  344. module_usb_composite_driver(zero_driver);
  345. MODULE_AUTHOR("David Brownell");
  346. MODULE_LICENSE("GPL");