zero.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * zero.c -- Gadget Zero, for USB development
  4. *
  5. * Copyright (C) 2003-2008 David Brownell
  6. * Copyright (C) 2008 by Nokia Corporation
  7. */
  8. /*
  9. * Gadget Zero only needs two bulk endpoints, and is an example of how you
  10. * can write a hardware-agnostic gadget driver running inside a USB device.
  11. * Some hardware details are visible, but don't affect most of the driver.
  12. *
  13. * Use it with the Linux host/master side "usbtest" driver to get a basic
  14. * functional test of your device-side usb stack, or with "usb-skeleton".
  15. *
  16. * It supports two similar configurations. One sinks whatever the usb host
  17. * writes, and in return sources zeroes. The other loops whatever the host
  18. * writes back, so the host can read it.
  19. *
  20. * Many drivers will only have one configuration, letting them be much
  21. * simpler if they also don't support high speed operation (like this
  22. * driver does).
  23. *
  24. * Why is *this* driver using two configurations, rather than setting up
  25. * two interfaces with different functions? To help verify that multiple
  26. * configuration infrastructure is working correctly; also, so that it can
  27. * work with low capability USB controllers without four bulk endpoints.
  28. */
  29. /*
  30. * driver assumes self-powered hardware, and
  31. * has no way for users to trigger remote wakeup.
  32. */
  33. /* #define VERBOSE_DEBUG */
  34. #include <linux/kernel.h>
  35. #include <linux/slab.h>
  36. #include <linux/device.h>
  37. #include <linux/module.h>
  38. #include <linux/err.h>
  39. #include <linux/usb/composite.h>
  40. #include "g_zero.h"
  41. /*-------------------------------------------------------------------------*/
  42. USB_GADGET_COMPOSITE_OPTIONS();
  43. #define DRIVER_VERSION "Cinco de Mayo 2008"
  44. static const char longname[] = "Gadget Zero";
  45. /*
  46. * Normally the "loopback" configuration is second (index 1) so
  47. * it's not the default. Here's where to change that order, to
  48. * work better with hosts where config changes are problematic or
  49. * controllers (like original superh) that only support one config.
  50. */
  51. static bool loopdefault = 0;
  52. module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  53. static struct usb_zero_options gzero_options = {
  54. .isoc_interval = GZERO_ISOC_INTERVAL,
  55. .isoc_maxpacket = GZERO_ISOC_MAXPACKET,
  56. .bulk_buflen = GZERO_BULK_BUFLEN,
  57. .qlen = GZERO_QLEN,
  58. .ss_bulk_qlen = GZERO_SS_BULK_QLEN,
  59. .ss_iso_qlen = GZERO_SS_ISO_QLEN,
  60. };
  61. /*-------------------------------------------------------------------------*/
  62. /* Thanks to NetChip Technologies for donating this product ID.
  63. *
  64. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  65. * Instead: allocate your own, using normal USB-IF procedures.
  66. */
  67. #ifndef CONFIG_USB_ZERO_HNPTEST
  68. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  69. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  70. #define DEFAULT_AUTORESUME 0
  71. #else
  72. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  73. #define DRIVER_PRODUCT_NUM 0xbadd
  74. #define DEFAULT_AUTORESUME 5
  75. #endif
  76. /* If the optional "autoresume" mode is enabled, it provides good
  77. * functional coverage for the "USBCV" test harness from USB-IF.
  78. * It's always set if OTG mode is enabled.
  79. */
  80. static unsigned autoresume = DEFAULT_AUTORESUME;
  81. module_param(autoresume, uint, S_IRUGO);
  82. MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  83. /* Maximum Autoresume time */
  84. static unsigned max_autoresume;
  85. module_param(max_autoresume, uint, S_IRUGO);
  86. MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
  87. /* Interval between two remote wakeups */
  88. static unsigned autoresume_interval_ms;
  89. module_param(autoresume_interval_ms, uint, S_IRUGO);
  90. MODULE_PARM_DESC(autoresume_interval_ms,
  91. "milliseconds to increase successive wakeup delays");
  92. static unsigned autoresume_step_ms;
  93. /*-------------------------------------------------------------------------*/
  94. static struct usb_device_descriptor device_desc = {
  95. .bLength = sizeof device_desc,
  96. .bDescriptorType = USB_DT_DEVICE,
  97. /* .bcdUSB = DYNAMIC */
  98. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  99. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  100. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  101. .bNumConfigurations = 2,
  102. };
  103. static const struct usb_descriptor_header *otg_desc[2];
  104. /* string IDs are assigned dynamically */
  105. /* default serial number takes at least two packets */
  106. static char serial[] = "0123456789.0123456789.0123456789";
  107. #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
  108. #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
  109. static struct usb_string strings_dev[] = {
  110. [USB_GADGET_MANUFACTURER_IDX].s = "",
  111. [USB_GADGET_PRODUCT_IDX].s = longname,
  112. [USB_GADGET_SERIAL_IDX].s = serial,
  113. [USB_GZERO_SS_DESC].s = "source and sink data",
  114. [USB_GZERO_LB_DESC].s = "loop input to output",
  115. { } /* end of list */
  116. };
  117. static struct usb_gadget_strings stringtab_dev = {
  118. .language = 0x0409, /* en-us */
  119. .strings = strings_dev,
  120. };
  121. static struct usb_gadget_strings *dev_strings[] = {
  122. &stringtab_dev,
  123. NULL,
  124. };
  125. /*-------------------------------------------------------------------------*/
  126. static struct timer_list autoresume_timer;
  127. static struct usb_composite_dev *autoresume_cdev;
  128. static void zero_autoresume(struct timer_list *unused)
  129. {
  130. struct usb_composite_dev *cdev = autoresume_cdev;
  131. struct usb_gadget *g = cdev->gadget;
  132. /* unconfigured devices can't issue wakeups */
  133. if (!cdev->config)
  134. return;
  135. /* Normally the host would be woken up for something
  136. * more significant than just a timer firing; likely
  137. * because of some direct user request.
  138. */
  139. if (g->speed != USB_SPEED_UNKNOWN) {
  140. int status = usb_gadget_wakeup(g);
  141. INFO(cdev, "%s --> %d\n", __func__, status);
  142. }
  143. }
  144. static void zero_suspend(struct usb_composite_dev *cdev)
  145. {
  146. if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
  147. return;
  148. if (autoresume) {
  149. if (max_autoresume &&
  150. (autoresume_step_ms > max_autoresume * 1000))
  151. autoresume_step_ms = autoresume * 1000;
  152. mod_timer(&autoresume_timer, jiffies +
  153. msecs_to_jiffies(autoresume_step_ms));
  154. DBG(cdev, "suspend, wakeup in %d milliseconds\n",
  155. autoresume_step_ms);
  156. autoresume_step_ms += autoresume_interval_ms;
  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. module_param_named(ss_bulk_qlen, gzero_options.ss_bulk_qlen, uint,
  211. S_IRUGO|S_IWUSR);
  212. MODULE_PARM_DESC(bulk_qlen, "depth of sourcesink queue for bulk transfer");
  213. module_param_named(ss_iso_qlen, gzero_options.ss_iso_qlen, uint,
  214. S_IRUGO|S_IWUSR);
  215. MODULE_PARM_DESC(iso_qlen, "depth of sourcesink queue for iso transfer");
  216. static int zero_bind(struct usb_composite_dev *cdev)
  217. {
  218. struct f_ss_opts *ss_opts;
  219. struct f_lb_opts *lb_opts;
  220. int status;
  221. /* Allocate string descriptor numbers ... note that string
  222. * contents can be overridden by the composite_dev glue.
  223. */
  224. status = usb_string_ids_tab(cdev, strings_dev);
  225. if (status < 0)
  226. return status;
  227. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  228. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  229. device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
  230. autoresume_cdev = cdev;
  231. timer_setup(&autoresume_timer, zero_autoresume, 0);
  232. func_inst_ss = usb_get_function_instance("SourceSink");
  233. if (IS_ERR(func_inst_ss))
  234. return PTR_ERR(func_inst_ss);
  235. ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
  236. ss_opts->pattern = gzero_options.pattern;
  237. ss_opts->isoc_interval = gzero_options.isoc_interval;
  238. ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
  239. ss_opts->isoc_mult = gzero_options.isoc_mult;
  240. ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
  241. ss_opts->bulk_buflen = gzero_options.bulk_buflen;
  242. ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen;
  243. ss_opts->iso_qlen = gzero_options.ss_iso_qlen;
  244. func_ss = usb_get_function(func_inst_ss);
  245. if (IS_ERR(func_ss)) {
  246. status = PTR_ERR(func_ss);
  247. goto err_put_func_inst_ss;
  248. }
  249. func_inst_lb = usb_get_function_instance("Loopback");
  250. if (IS_ERR(func_inst_lb)) {
  251. status = PTR_ERR(func_inst_lb);
  252. goto err_put_func_ss;
  253. }
  254. lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
  255. lb_opts->bulk_buflen = gzero_options.bulk_buflen;
  256. lb_opts->qlen = gzero_options.qlen;
  257. func_lb = usb_get_function(func_inst_lb);
  258. if (IS_ERR(func_lb)) {
  259. status = PTR_ERR(func_lb);
  260. goto err_put_func_inst_lb;
  261. }
  262. sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
  263. loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
  264. /* support autoresume for remote wakeup testing */
  265. sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  266. loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
  267. sourcesink_driver.descriptors = NULL;
  268. loopback_driver.descriptors = NULL;
  269. if (autoresume) {
  270. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  271. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  272. autoresume_step_ms = autoresume * 1000;
  273. }
  274. /* support OTG systems */
  275. if (gadget_is_otg(cdev->gadget)) {
  276. if (!otg_desc[0]) {
  277. struct usb_descriptor_header *usb_desc;
  278. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  279. if (!usb_desc) {
  280. status = -ENOMEM;
  281. goto err_conf_flb;
  282. }
  283. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  284. otg_desc[0] = usb_desc;
  285. otg_desc[1] = NULL;
  286. }
  287. sourcesink_driver.descriptors = otg_desc;
  288. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  289. loopback_driver.descriptors = otg_desc;
  290. loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  291. }
  292. /* Register primary, then secondary configuration. Note that
  293. * SH3 only allows one config...
  294. */
  295. if (loopdefault) {
  296. usb_add_config_only(cdev, &loopback_driver);
  297. usb_add_config_only(cdev, &sourcesink_driver);
  298. } else {
  299. usb_add_config_only(cdev, &sourcesink_driver);
  300. usb_add_config_only(cdev, &loopback_driver);
  301. }
  302. status = usb_add_function(&sourcesink_driver, func_ss);
  303. if (status)
  304. goto err_free_otg_desc;
  305. usb_ep_autoconfig_reset(cdev->gadget);
  306. status = usb_add_function(&loopback_driver, func_lb);
  307. if (status)
  308. goto err_free_otg_desc;
  309. usb_ep_autoconfig_reset(cdev->gadget);
  310. usb_composite_overwrite_options(cdev, &coverwrite);
  311. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
  312. return 0;
  313. err_free_otg_desc:
  314. kfree(otg_desc[0]);
  315. otg_desc[0] = NULL;
  316. err_conf_flb:
  317. usb_put_function(func_lb);
  318. func_lb = NULL;
  319. err_put_func_inst_lb:
  320. usb_put_function_instance(func_inst_lb);
  321. func_inst_lb = NULL;
  322. err_put_func_ss:
  323. usb_put_function(func_ss);
  324. func_ss = NULL;
  325. err_put_func_inst_ss:
  326. usb_put_function_instance(func_inst_ss);
  327. func_inst_ss = NULL;
  328. return status;
  329. }
  330. static int zero_unbind(struct usb_composite_dev *cdev)
  331. {
  332. del_timer_sync(&autoresume_timer);
  333. if (!IS_ERR_OR_NULL(func_ss))
  334. usb_put_function(func_ss);
  335. usb_put_function_instance(func_inst_ss);
  336. if (!IS_ERR_OR_NULL(func_lb))
  337. usb_put_function(func_lb);
  338. usb_put_function_instance(func_inst_lb);
  339. kfree(otg_desc[0]);
  340. otg_desc[0] = NULL;
  341. return 0;
  342. }
  343. static struct usb_composite_driver zero_driver = {
  344. .name = "zero",
  345. .dev = &device_desc,
  346. .strings = dev_strings,
  347. .max_speed = USB_SPEED_SUPER,
  348. .bind = zero_bind,
  349. .unbind = zero_unbind,
  350. .suspend = zero_suspend,
  351. .resume = zero_resume,
  352. };
  353. module_usb_composite_driver(zero_driver);
  354. MODULE_AUTHOR("David Brownell");
  355. MODULE_LICENSE("GPL");