zero.c 13 KB

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