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