zero.c 12 KB

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