15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28443f2d2SAndrzej Pietrasiewicz /*
38443f2d2SAndrzej Pietrasiewicz * zero.c -- Gadget Zero, for USB development
48443f2d2SAndrzej Pietrasiewicz *
58443f2d2SAndrzej Pietrasiewicz * Copyright (C) 2003-2008 David Brownell
68443f2d2SAndrzej Pietrasiewicz * Copyright (C) 2008 by Nokia Corporation
78443f2d2SAndrzej Pietrasiewicz */
88443f2d2SAndrzej Pietrasiewicz
98443f2d2SAndrzej Pietrasiewicz /*
108443f2d2SAndrzej Pietrasiewicz * Gadget Zero only needs two bulk endpoints, and is an example of how you
118443f2d2SAndrzej Pietrasiewicz * can write a hardware-agnostic gadget driver running inside a USB device.
128443f2d2SAndrzej Pietrasiewicz * Some hardware details are visible, but don't affect most of the driver.
138443f2d2SAndrzej Pietrasiewicz *
14*b9b70170SGreg Kroah-Hartman * Use it with the Linux host side "usbtest" driver to get a basic functional
15*b9b70170SGreg Kroah-Hartman * test of your device-side usb stack, or with "usb-skeleton".
168443f2d2SAndrzej Pietrasiewicz *
178443f2d2SAndrzej Pietrasiewicz * It supports two similar configurations. One sinks whatever the usb host
188443f2d2SAndrzej Pietrasiewicz * writes, and in return sources zeroes. The other loops whatever the host
198443f2d2SAndrzej Pietrasiewicz * writes back, so the host can read it.
208443f2d2SAndrzej Pietrasiewicz *
218443f2d2SAndrzej Pietrasiewicz * Many drivers will only have one configuration, letting them be much
228443f2d2SAndrzej Pietrasiewicz * simpler if they also don't support high speed operation (like this
238443f2d2SAndrzej Pietrasiewicz * driver does).
248443f2d2SAndrzej Pietrasiewicz *
258443f2d2SAndrzej Pietrasiewicz * Why is *this* driver using two configurations, rather than setting up
268443f2d2SAndrzej Pietrasiewicz * two interfaces with different functions? To help verify that multiple
27b8bcf32aSGeert Uytterhoeven * configuration infrastructure is working correctly; also, so that it can
288443f2d2SAndrzej Pietrasiewicz * work with low capability USB controllers without four bulk endpoints.
298443f2d2SAndrzej Pietrasiewicz */
308443f2d2SAndrzej Pietrasiewicz
318443f2d2SAndrzej Pietrasiewicz /*
328443f2d2SAndrzej Pietrasiewicz * driver assumes self-powered hardware, and
338443f2d2SAndrzej Pietrasiewicz * has no way for users to trigger remote wakeup.
348443f2d2SAndrzej Pietrasiewicz */
358443f2d2SAndrzej Pietrasiewicz
368443f2d2SAndrzej Pietrasiewicz /* #define VERBOSE_DEBUG */
378443f2d2SAndrzej Pietrasiewicz
388443f2d2SAndrzej Pietrasiewicz #include <linux/kernel.h>
398443f2d2SAndrzej Pietrasiewicz #include <linux/slab.h>
408443f2d2SAndrzej Pietrasiewicz #include <linux/device.h>
418443f2d2SAndrzej Pietrasiewicz #include <linux/module.h>
428443f2d2SAndrzej Pietrasiewicz #include <linux/err.h>
438443f2d2SAndrzej Pietrasiewicz #include <linux/usb/composite.h>
448443f2d2SAndrzej Pietrasiewicz
458443f2d2SAndrzej Pietrasiewicz #include "g_zero.h"
468443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
478443f2d2SAndrzej Pietrasiewicz USB_GADGET_COMPOSITE_OPTIONS();
488443f2d2SAndrzej Pietrasiewicz
498443f2d2SAndrzej Pietrasiewicz #define DRIVER_VERSION "Cinco de Mayo 2008"
508443f2d2SAndrzej Pietrasiewicz
518443f2d2SAndrzej Pietrasiewicz static const char longname[] = "Gadget Zero";
528443f2d2SAndrzej Pietrasiewicz
538443f2d2SAndrzej Pietrasiewicz /*
548443f2d2SAndrzej Pietrasiewicz * Normally the "loopback" configuration is second (index 1) so
558443f2d2SAndrzej Pietrasiewicz * it's not the default. Here's where to change that order, to
568443f2d2SAndrzej Pietrasiewicz * work better with hosts where config changes are problematic or
578443f2d2SAndrzej Pietrasiewicz * controllers (like original superh) that only support one config.
588443f2d2SAndrzej Pietrasiewicz */
598443f2d2SAndrzej Pietrasiewicz static bool loopdefault = 0;
608443f2d2SAndrzej Pietrasiewicz module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
618443f2d2SAndrzej Pietrasiewicz
628443f2d2SAndrzej Pietrasiewicz static struct usb_zero_options gzero_options = {
638443f2d2SAndrzej Pietrasiewicz .isoc_interval = GZERO_ISOC_INTERVAL,
648443f2d2SAndrzej Pietrasiewicz .isoc_maxpacket = GZERO_ISOC_MAXPACKET,
658443f2d2SAndrzej Pietrasiewicz .bulk_buflen = GZERO_BULK_BUFLEN,
668443f2d2SAndrzej Pietrasiewicz .qlen = GZERO_QLEN,
670d6c3d96SPeter Chen .ss_bulk_qlen = GZERO_SS_BULK_QLEN,
680d6c3d96SPeter Chen .ss_iso_qlen = GZERO_SS_ISO_QLEN,
698443f2d2SAndrzej Pietrasiewicz };
708443f2d2SAndrzej Pietrasiewicz
718443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
728443f2d2SAndrzej Pietrasiewicz
738443f2d2SAndrzej Pietrasiewicz /* Thanks to NetChip Technologies for donating this product ID.
748443f2d2SAndrzej Pietrasiewicz *
758443f2d2SAndrzej Pietrasiewicz * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
768443f2d2SAndrzej Pietrasiewicz * Instead: allocate your own, using normal USB-IF procedures.
778443f2d2SAndrzej Pietrasiewicz */
788443f2d2SAndrzej Pietrasiewicz #ifndef CONFIG_USB_ZERO_HNPTEST
798443f2d2SAndrzej Pietrasiewicz #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
808443f2d2SAndrzej Pietrasiewicz #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
818443f2d2SAndrzej Pietrasiewicz #define DEFAULT_AUTORESUME 0
828443f2d2SAndrzej Pietrasiewicz #else
838443f2d2SAndrzej Pietrasiewicz #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
848443f2d2SAndrzej Pietrasiewicz #define DRIVER_PRODUCT_NUM 0xbadd
858443f2d2SAndrzej Pietrasiewicz #define DEFAULT_AUTORESUME 5
868443f2d2SAndrzej Pietrasiewicz #endif
878443f2d2SAndrzej Pietrasiewicz
888443f2d2SAndrzej Pietrasiewicz /* If the optional "autoresume" mode is enabled, it provides good
898443f2d2SAndrzej Pietrasiewicz * functional coverage for the "USBCV" test harness from USB-IF.
908443f2d2SAndrzej Pietrasiewicz * It's always set if OTG mode is enabled.
918443f2d2SAndrzej Pietrasiewicz */
928443f2d2SAndrzej Pietrasiewicz static unsigned autoresume = DEFAULT_AUTORESUME;
938443f2d2SAndrzej Pietrasiewicz module_param(autoresume, uint, S_IRUGO);
948443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
958443f2d2SAndrzej Pietrasiewicz
968443f2d2SAndrzej Pietrasiewicz /* Maximum Autoresume time */
978443f2d2SAndrzej Pietrasiewicz static unsigned max_autoresume;
988443f2d2SAndrzej Pietrasiewicz module_param(max_autoresume, uint, S_IRUGO);
998443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
1008443f2d2SAndrzej Pietrasiewicz
1018443f2d2SAndrzej Pietrasiewicz /* Interval between two remote wakeups */
1028443f2d2SAndrzej Pietrasiewicz static unsigned autoresume_interval_ms;
1038443f2d2SAndrzej Pietrasiewicz module_param(autoresume_interval_ms, uint, S_IRUGO);
1048443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(autoresume_interval_ms,
1058443f2d2SAndrzej Pietrasiewicz "milliseconds to increase successive wakeup delays");
1068443f2d2SAndrzej Pietrasiewicz
1078443f2d2SAndrzej Pietrasiewicz static unsigned autoresume_step_ms;
1088443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
1098443f2d2SAndrzej Pietrasiewicz
1108443f2d2SAndrzej Pietrasiewicz static struct usb_device_descriptor device_desc = {
1118443f2d2SAndrzej Pietrasiewicz .bLength = sizeof device_desc,
1128443f2d2SAndrzej Pietrasiewicz .bDescriptorType = USB_DT_DEVICE,
1138443f2d2SAndrzej Pietrasiewicz
1140aecfc1bSIgor Kotrasinski /* .bcdUSB = DYNAMIC */
1158443f2d2SAndrzej Pietrasiewicz .bDeviceClass = USB_CLASS_VENDOR_SPEC,
1168443f2d2SAndrzej Pietrasiewicz
1178443f2d2SAndrzej Pietrasiewicz .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
1188443f2d2SAndrzej Pietrasiewicz .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
1198443f2d2SAndrzej Pietrasiewicz .bNumConfigurations = 2,
1208443f2d2SAndrzej Pietrasiewicz };
1218443f2d2SAndrzej Pietrasiewicz
1229e832bf7SLi Jun static const struct usb_descriptor_header *otg_desc[2];
1238443f2d2SAndrzej Pietrasiewicz
1248443f2d2SAndrzej Pietrasiewicz /* string IDs are assigned dynamically */
1258443f2d2SAndrzej Pietrasiewicz /* default serial number takes at least two packets */
1268443f2d2SAndrzej Pietrasiewicz static char serial[] = "0123456789.0123456789.0123456789";
1278443f2d2SAndrzej Pietrasiewicz
1288443f2d2SAndrzej Pietrasiewicz #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
1298443f2d2SAndrzej Pietrasiewicz #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
1308443f2d2SAndrzej Pietrasiewicz
1318443f2d2SAndrzej Pietrasiewicz static struct usb_string strings_dev[] = {
1328443f2d2SAndrzej Pietrasiewicz [USB_GADGET_MANUFACTURER_IDX].s = "",
1338443f2d2SAndrzej Pietrasiewicz [USB_GADGET_PRODUCT_IDX].s = longname,
1348443f2d2SAndrzej Pietrasiewicz [USB_GADGET_SERIAL_IDX].s = serial,
1358443f2d2SAndrzej Pietrasiewicz [USB_GZERO_SS_DESC].s = "source and sink data",
1368443f2d2SAndrzej Pietrasiewicz [USB_GZERO_LB_DESC].s = "loop input to output",
1378443f2d2SAndrzej Pietrasiewicz { } /* end of list */
1388443f2d2SAndrzej Pietrasiewicz };
1398443f2d2SAndrzej Pietrasiewicz
1408443f2d2SAndrzej Pietrasiewicz static struct usb_gadget_strings stringtab_dev = {
1418443f2d2SAndrzej Pietrasiewicz .language = 0x0409, /* en-us */
1428443f2d2SAndrzej Pietrasiewicz .strings = strings_dev,
1438443f2d2SAndrzej Pietrasiewicz };
1448443f2d2SAndrzej Pietrasiewicz
1458443f2d2SAndrzej Pietrasiewicz static struct usb_gadget_strings *dev_strings[] = {
1468443f2d2SAndrzej Pietrasiewicz &stringtab_dev,
1478443f2d2SAndrzej Pietrasiewicz NULL,
1488443f2d2SAndrzej Pietrasiewicz };
1498443f2d2SAndrzej Pietrasiewicz
1508443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
1518443f2d2SAndrzej Pietrasiewicz
1528443f2d2SAndrzej Pietrasiewicz static struct timer_list autoresume_timer;
15374fef19fSKees Cook static struct usb_composite_dev *autoresume_cdev;
1548443f2d2SAndrzej Pietrasiewicz
zero_autoresume(struct timer_list * unused)15574fef19fSKees Cook static void zero_autoresume(struct timer_list *unused)
1568443f2d2SAndrzej Pietrasiewicz {
15774fef19fSKees Cook struct usb_composite_dev *cdev = autoresume_cdev;
1588443f2d2SAndrzej Pietrasiewicz struct usb_gadget *g = cdev->gadget;
1598443f2d2SAndrzej Pietrasiewicz
1608443f2d2SAndrzej Pietrasiewicz /* unconfigured devices can't issue wakeups */
1618443f2d2SAndrzej Pietrasiewicz if (!cdev->config)
1628443f2d2SAndrzej Pietrasiewicz return;
1638443f2d2SAndrzej Pietrasiewicz
1648443f2d2SAndrzej Pietrasiewicz /* Normally the host would be woken up for something
1658443f2d2SAndrzej Pietrasiewicz * more significant than just a timer firing; likely
1668443f2d2SAndrzej Pietrasiewicz * because of some direct user request.
1678443f2d2SAndrzej Pietrasiewicz */
1688443f2d2SAndrzej Pietrasiewicz if (g->speed != USB_SPEED_UNKNOWN) {
1698443f2d2SAndrzej Pietrasiewicz int status = usb_gadget_wakeup(g);
1708443f2d2SAndrzej Pietrasiewicz INFO(cdev, "%s --> %d\n", __func__, status);
1718443f2d2SAndrzej Pietrasiewicz }
1728443f2d2SAndrzej Pietrasiewicz }
1738443f2d2SAndrzej Pietrasiewicz
zero_suspend(struct usb_composite_dev * cdev)1748443f2d2SAndrzej Pietrasiewicz static void zero_suspend(struct usb_composite_dev *cdev)
1758443f2d2SAndrzej Pietrasiewicz {
1768443f2d2SAndrzej Pietrasiewicz if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
1778443f2d2SAndrzej Pietrasiewicz return;
1788443f2d2SAndrzej Pietrasiewicz
1798443f2d2SAndrzej Pietrasiewicz if (autoresume) {
1808443f2d2SAndrzej Pietrasiewicz if (max_autoresume &&
1818443f2d2SAndrzej Pietrasiewicz (autoresume_step_ms > max_autoresume * 1000))
1828443f2d2SAndrzej Pietrasiewicz autoresume_step_ms = autoresume * 1000;
1838443f2d2SAndrzej Pietrasiewicz
1848443f2d2SAndrzej Pietrasiewicz mod_timer(&autoresume_timer, jiffies +
1858443f2d2SAndrzej Pietrasiewicz msecs_to_jiffies(autoresume_step_ms));
1868443f2d2SAndrzej Pietrasiewicz DBG(cdev, "suspend, wakeup in %d milliseconds\n",
1878443f2d2SAndrzej Pietrasiewicz autoresume_step_ms);
1888443f2d2SAndrzej Pietrasiewicz
1898443f2d2SAndrzej Pietrasiewicz autoresume_step_ms += autoresume_interval_ms;
1908443f2d2SAndrzej Pietrasiewicz } else
1918443f2d2SAndrzej Pietrasiewicz DBG(cdev, "%s\n", __func__);
1928443f2d2SAndrzej Pietrasiewicz }
1938443f2d2SAndrzej Pietrasiewicz
zero_resume(struct usb_composite_dev * cdev)1948443f2d2SAndrzej Pietrasiewicz static void zero_resume(struct usb_composite_dev *cdev)
1958443f2d2SAndrzej Pietrasiewicz {
1968443f2d2SAndrzej Pietrasiewicz DBG(cdev, "%s\n", __func__);
1978443f2d2SAndrzej Pietrasiewicz del_timer(&autoresume_timer);
1988443f2d2SAndrzej Pietrasiewicz }
1998443f2d2SAndrzej Pietrasiewicz
2008443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
2018443f2d2SAndrzej Pietrasiewicz
2028443f2d2SAndrzej Pietrasiewicz static struct usb_configuration loopback_driver = {
2038443f2d2SAndrzej Pietrasiewicz .label = "loopback",
2048443f2d2SAndrzej Pietrasiewicz .bConfigurationValue = 2,
2058443f2d2SAndrzej Pietrasiewicz .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
2068443f2d2SAndrzej Pietrasiewicz /* .iConfiguration = DYNAMIC */
2078443f2d2SAndrzej Pietrasiewicz };
2088443f2d2SAndrzej Pietrasiewicz
2098443f2d2SAndrzej Pietrasiewicz static struct usb_function *func_ss;
2108443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance *func_inst_ss;
2118443f2d2SAndrzej Pietrasiewicz
ss_config_setup(struct usb_configuration * c,const struct usb_ctrlrequest * ctrl)2128443f2d2SAndrzej Pietrasiewicz static int ss_config_setup(struct usb_configuration *c,
2138443f2d2SAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl)
2148443f2d2SAndrzej Pietrasiewicz {
2158443f2d2SAndrzej Pietrasiewicz switch (ctrl->bRequest) {
2168443f2d2SAndrzej Pietrasiewicz case 0x5b:
2178443f2d2SAndrzej Pietrasiewicz case 0x5c:
2188443f2d2SAndrzej Pietrasiewicz return func_ss->setup(func_ss, ctrl);
2198443f2d2SAndrzej Pietrasiewicz default:
2208443f2d2SAndrzej Pietrasiewicz return -EOPNOTSUPP;
2218443f2d2SAndrzej Pietrasiewicz }
2228443f2d2SAndrzej Pietrasiewicz }
2238443f2d2SAndrzej Pietrasiewicz
2248443f2d2SAndrzej Pietrasiewicz static struct usb_configuration sourcesink_driver = {
2258443f2d2SAndrzej Pietrasiewicz .label = "source/sink",
2268443f2d2SAndrzej Pietrasiewicz .setup = ss_config_setup,
2278443f2d2SAndrzej Pietrasiewicz .bConfigurationValue = 3,
2288443f2d2SAndrzej Pietrasiewicz .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
2298443f2d2SAndrzej Pietrasiewicz /* .iConfiguration = DYNAMIC */
2308443f2d2SAndrzej Pietrasiewicz };
2318443f2d2SAndrzej Pietrasiewicz
2328443f2d2SAndrzej Pietrasiewicz module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
2338443f2d2SAndrzej Pietrasiewicz module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
2348443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
2358443f2d2SAndrzej Pietrasiewicz
2368443f2d2SAndrzej Pietrasiewicz module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
2378443f2d2SAndrzej Pietrasiewicz S_IRUGO|S_IWUSR);
2388443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(isoc_interval, "1 - 16");
2398443f2d2SAndrzej Pietrasiewicz
2408443f2d2SAndrzej Pietrasiewicz module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
2418443f2d2SAndrzej Pietrasiewicz S_IRUGO|S_IWUSR);
2428443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
2438443f2d2SAndrzej Pietrasiewicz
2448443f2d2SAndrzej Pietrasiewicz module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
2458443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
2468443f2d2SAndrzej Pietrasiewicz
2478443f2d2SAndrzej Pietrasiewicz module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
2488443f2d2SAndrzej Pietrasiewicz S_IRUGO|S_IWUSR);
2498443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
2508443f2d2SAndrzej Pietrasiewicz
2518443f2d2SAndrzej Pietrasiewicz static struct usb_function *func_lb;
2528443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance *func_inst_lb;
2538443f2d2SAndrzej Pietrasiewicz
2548443f2d2SAndrzej Pietrasiewicz module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
2558443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(qlen, "depth of loopback queue");
2568443f2d2SAndrzej Pietrasiewicz
2570d6c3d96SPeter Chen module_param_named(ss_bulk_qlen, gzero_options.ss_bulk_qlen, uint,
2580d6c3d96SPeter Chen S_IRUGO|S_IWUSR);
2590d6c3d96SPeter Chen MODULE_PARM_DESC(bulk_qlen, "depth of sourcesink queue for bulk transfer");
2600d6c3d96SPeter Chen
2610d6c3d96SPeter Chen module_param_named(ss_iso_qlen, gzero_options.ss_iso_qlen, uint,
2620d6c3d96SPeter Chen S_IRUGO|S_IWUSR);
2630d6c3d96SPeter Chen MODULE_PARM_DESC(iso_qlen, "depth of sourcesink queue for iso transfer");
2640d6c3d96SPeter Chen
zero_bind(struct usb_composite_dev * cdev)265c94e289fSArnd Bergmann static int zero_bind(struct usb_composite_dev *cdev)
2668443f2d2SAndrzej Pietrasiewicz {
2678443f2d2SAndrzej Pietrasiewicz struct f_ss_opts *ss_opts;
2688443f2d2SAndrzej Pietrasiewicz struct f_lb_opts *lb_opts;
2698443f2d2SAndrzej Pietrasiewicz int status;
2708443f2d2SAndrzej Pietrasiewicz
2718443f2d2SAndrzej Pietrasiewicz /* Allocate string descriptor numbers ... note that string
2728443f2d2SAndrzej Pietrasiewicz * contents can be overridden by the composite_dev glue.
2738443f2d2SAndrzej Pietrasiewicz */
2748443f2d2SAndrzej Pietrasiewicz status = usb_string_ids_tab(cdev, strings_dev);
2758443f2d2SAndrzej Pietrasiewicz if (status < 0)
2768443f2d2SAndrzej Pietrasiewicz return status;
2778443f2d2SAndrzej Pietrasiewicz
2788443f2d2SAndrzej Pietrasiewicz device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
2798443f2d2SAndrzej Pietrasiewicz device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
2808443f2d2SAndrzej Pietrasiewicz device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
2818443f2d2SAndrzej Pietrasiewicz
28274fef19fSKees Cook autoresume_cdev = cdev;
28374fef19fSKees Cook timer_setup(&autoresume_timer, zero_autoresume, 0);
2848443f2d2SAndrzej Pietrasiewicz
2858443f2d2SAndrzej Pietrasiewicz func_inst_ss = usb_get_function_instance("SourceSink");
2868443f2d2SAndrzej Pietrasiewicz if (IS_ERR(func_inst_ss))
2878443f2d2SAndrzej Pietrasiewicz return PTR_ERR(func_inst_ss);
2888443f2d2SAndrzej Pietrasiewicz
2898443f2d2SAndrzej Pietrasiewicz ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst);
2908443f2d2SAndrzej Pietrasiewicz ss_opts->pattern = gzero_options.pattern;
2918443f2d2SAndrzej Pietrasiewicz ss_opts->isoc_interval = gzero_options.isoc_interval;
2928443f2d2SAndrzej Pietrasiewicz ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
2938443f2d2SAndrzej Pietrasiewicz ss_opts->isoc_mult = gzero_options.isoc_mult;
2948443f2d2SAndrzej Pietrasiewicz ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
2958443f2d2SAndrzej Pietrasiewicz ss_opts->bulk_buflen = gzero_options.bulk_buflen;
2960d6c3d96SPeter Chen ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen;
2970d6c3d96SPeter Chen ss_opts->iso_qlen = gzero_options.ss_iso_qlen;
2988443f2d2SAndrzej Pietrasiewicz
2998443f2d2SAndrzej Pietrasiewicz func_ss = usb_get_function(func_inst_ss);
3008443f2d2SAndrzej Pietrasiewicz if (IS_ERR(func_ss)) {
3018443f2d2SAndrzej Pietrasiewicz status = PTR_ERR(func_ss);
3028443f2d2SAndrzej Pietrasiewicz goto err_put_func_inst_ss;
3038443f2d2SAndrzej Pietrasiewicz }
3048443f2d2SAndrzej Pietrasiewicz
3058443f2d2SAndrzej Pietrasiewicz func_inst_lb = usb_get_function_instance("Loopback");
3068443f2d2SAndrzej Pietrasiewicz if (IS_ERR(func_inst_lb)) {
3078443f2d2SAndrzej Pietrasiewicz status = PTR_ERR(func_inst_lb);
3088443f2d2SAndrzej Pietrasiewicz goto err_put_func_ss;
3098443f2d2SAndrzej Pietrasiewicz }
3108443f2d2SAndrzej Pietrasiewicz
3118443f2d2SAndrzej Pietrasiewicz lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
3128443f2d2SAndrzej Pietrasiewicz lb_opts->bulk_buflen = gzero_options.bulk_buflen;
3138443f2d2SAndrzej Pietrasiewicz lb_opts->qlen = gzero_options.qlen;
3148443f2d2SAndrzej Pietrasiewicz
3158443f2d2SAndrzej Pietrasiewicz func_lb = usb_get_function(func_inst_lb);
3168443f2d2SAndrzej Pietrasiewicz if (IS_ERR(func_lb)) {
3178443f2d2SAndrzej Pietrasiewicz status = PTR_ERR(func_lb);
3188443f2d2SAndrzej Pietrasiewicz goto err_put_func_inst_lb;
3198443f2d2SAndrzej Pietrasiewicz }
3208443f2d2SAndrzej Pietrasiewicz
3218443f2d2SAndrzej Pietrasiewicz sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
3228443f2d2SAndrzej Pietrasiewicz loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
3238443f2d2SAndrzej Pietrasiewicz
3248443f2d2SAndrzej Pietrasiewicz /* support autoresume for remote wakeup testing */
3258443f2d2SAndrzej Pietrasiewicz sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
3268443f2d2SAndrzej Pietrasiewicz loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
3278443f2d2SAndrzej Pietrasiewicz sourcesink_driver.descriptors = NULL;
3288443f2d2SAndrzej Pietrasiewicz loopback_driver.descriptors = NULL;
3298443f2d2SAndrzej Pietrasiewicz if (autoresume) {
3308443f2d2SAndrzej Pietrasiewicz sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3318443f2d2SAndrzej Pietrasiewicz loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3328443f2d2SAndrzej Pietrasiewicz autoresume_step_ms = autoresume * 1000;
3338443f2d2SAndrzej Pietrasiewicz }
3348443f2d2SAndrzej Pietrasiewicz
3358443f2d2SAndrzej Pietrasiewicz /* support OTG systems */
3368443f2d2SAndrzej Pietrasiewicz if (gadget_is_otg(cdev->gadget)) {
3379e832bf7SLi Jun if (!otg_desc[0]) {
3389e832bf7SLi Jun struct usb_descriptor_header *usb_desc;
3399e832bf7SLi Jun
3409e832bf7SLi Jun usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
3419e832bf7SLi Jun if (!usb_desc) {
3429e832bf7SLi Jun status = -ENOMEM;
3439e832bf7SLi Jun goto err_conf_flb;
3449e832bf7SLi Jun }
3459e832bf7SLi Jun usb_otg_descriptor_init(cdev->gadget, usb_desc);
3469e832bf7SLi Jun otg_desc[0] = usb_desc;
3479e832bf7SLi Jun otg_desc[1] = NULL;
3489e832bf7SLi Jun }
3498443f2d2SAndrzej Pietrasiewicz sourcesink_driver.descriptors = otg_desc;
3508443f2d2SAndrzej Pietrasiewicz sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3518443f2d2SAndrzej Pietrasiewicz loopback_driver.descriptors = otg_desc;
3528443f2d2SAndrzej Pietrasiewicz loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3538443f2d2SAndrzej Pietrasiewicz }
3548443f2d2SAndrzej Pietrasiewicz
3558443f2d2SAndrzej Pietrasiewicz /* Register primary, then secondary configuration. Note that
3568443f2d2SAndrzej Pietrasiewicz * SH3 only allows one config...
3578443f2d2SAndrzej Pietrasiewicz */
3588443f2d2SAndrzej Pietrasiewicz if (loopdefault) {
3598443f2d2SAndrzej Pietrasiewicz usb_add_config_only(cdev, &loopback_driver);
3608443f2d2SAndrzej Pietrasiewicz usb_add_config_only(cdev, &sourcesink_driver);
3618443f2d2SAndrzej Pietrasiewicz } else {
3628443f2d2SAndrzej Pietrasiewicz usb_add_config_only(cdev, &sourcesink_driver);
3638443f2d2SAndrzej Pietrasiewicz usb_add_config_only(cdev, &loopback_driver);
3648443f2d2SAndrzej Pietrasiewicz }
3658443f2d2SAndrzej Pietrasiewicz status = usb_add_function(&sourcesink_driver, func_ss);
3668443f2d2SAndrzej Pietrasiewicz if (status)
3679e832bf7SLi Jun goto err_free_otg_desc;
3688443f2d2SAndrzej Pietrasiewicz
3698443f2d2SAndrzej Pietrasiewicz usb_ep_autoconfig_reset(cdev->gadget);
3708443f2d2SAndrzej Pietrasiewicz status = usb_add_function(&loopback_driver, func_lb);
3718443f2d2SAndrzej Pietrasiewicz if (status)
3729e832bf7SLi Jun goto err_free_otg_desc;
3738443f2d2SAndrzej Pietrasiewicz
3748443f2d2SAndrzej Pietrasiewicz usb_ep_autoconfig_reset(cdev->gadget);
3758443f2d2SAndrzej Pietrasiewicz usb_composite_overwrite_options(cdev, &coverwrite);
3768443f2d2SAndrzej Pietrasiewicz
3778443f2d2SAndrzej Pietrasiewicz INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
3788443f2d2SAndrzej Pietrasiewicz
3798443f2d2SAndrzej Pietrasiewicz return 0;
3808443f2d2SAndrzej Pietrasiewicz
3819e832bf7SLi Jun err_free_otg_desc:
3829e832bf7SLi Jun kfree(otg_desc[0]);
3839e832bf7SLi Jun otg_desc[0] = NULL;
3848443f2d2SAndrzej Pietrasiewicz err_conf_flb:
3858443f2d2SAndrzej Pietrasiewicz usb_put_function(func_lb);
3868443f2d2SAndrzej Pietrasiewicz func_lb = NULL;
3878443f2d2SAndrzej Pietrasiewicz err_put_func_inst_lb:
3888443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(func_inst_lb);
3898443f2d2SAndrzej Pietrasiewicz func_inst_lb = NULL;
3908443f2d2SAndrzej Pietrasiewicz err_put_func_ss:
3918443f2d2SAndrzej Pietrasiewicz usb_put_function(func_ss);
3928443f2d2SAndrzej Pietrasiewicz func_ss = NULL;
3938443f2d2SAndrzej Pietrasiewicz err_put_func_inst_ss:
3948443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(func_inst_ss);
3958443f2d2SAndrzej Pietrasiewicz func_inst_ss = NULL;
3968443f2d2SAndrzej Pietrasiewicz return status;
3978443f2d2SAndrzej Pietrasiewicz }
3988443f2d2SAndrzej Pietrasiewicz
zero_unbind(struct usb_composite_dev * cdev)3998443f2d2SAndrzej Pietrasiewicz static int zero_unbind(struct usb_composite_dev *cdev)
4008443f2d2SAndrzej Pietrasiewicz {
4018443f2d2SAndrzej Pietrasiewicz del_timer_sync(&autoresume_timer);
4028443f2d2SAndrzej Pietrasiewicz if (!IS_ERR_OR_NULL(func_ss))
4038443f2d2SAndrzej Pietrasiewicz usb_put_function(func_ss);
4048443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(func_inst_ss);
4058443f2d2SAndrzej Pietrasiewicz if (!IS_ERR_OR_NULL(func_lb))
4068443f2d2SAndrzej Pietrasiewicz usb_put_function(func_lb);
4078443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(func_inst_lb);
4089e832bf7SLi Jun kfree(otg_desc[0]);
4099e832bf7SLi Jun otg_desc[0] = NULL;
4109e832bf7SLi Jun
4118443f2d2SAndrzej Pietrasiewicz return 0;
4128443f2d2SAndrzej Pietrasiewicz }
4138443f2d2SAndrzej Pietrasiewicz
414c94e289fSArnd Bergmann static struct usb_composite_driver zero_driver = {
4158443f2d2SAndrzej Pietrasiewicz .name = "zero",
4168443f2d2SAndrzej Pietrasiewicz .dev = &device_desc,
4178443f2d2SAndrzej Pietrasiewicz .strings = dev_strings,
4188443f2d2SAndrzej Pietrasiewicz .max_speed = USB_SPEED_SUPER,
4198443f2d2SAndrzej Pietrasiewicz .bind = zero_bind,
4208443f2d2SAndrzej Pietrasiewicz .unbind = zero_unbind,
4218443f2d2SAndrzej Pietrasiewicz .suspend = zero_suspend,
4228443f2d2SAndrzej Pietrasiewicz .resume = zero_resume,
4238443f2d2SAndrzej Pietrasiewicz };
4248443f2d2SAndrzej Pietrasiewicz
4258443f2d2SAndrzej Pietrasiewicz module_usb_composite_driver(zero_driver);
4268443f2d2SAndrzej Pietrasiewicz
4278443f2d2SAndrzej Pietrasiewicz MODULE_AUTHOR("David Brownell");
4288443f2d2SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
429