Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * zero.c -- Gadget Zero, for USB development |
| 3 | * |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 David Brownell |
| 5 | * Copyright (C) 2008 by Nokia Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | /* |
| 14 | * Gadget Zero only needs two bulk endpoints, and is an example of how you |
| 15 | * can write a hardware-agnostic gadget driver running inside a USB device. |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 16 | * Some hardware details are visible, but don't affect most of the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 18 | * Use it with the Linux host/master side "usbtest" driver to get a basic |
| 19 | * functional test of your device-side usb stack, or with "usb-skeleton". |
| 20 | * |
| 21 | * It supports two similar configurations. One sinks whatever the usb host |
| 22 | * writes, and in return sources zeroes. The other loops whatever the host |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 23 | * writes back, so the host can read it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * |
| 25 | * Many drivers will only have one configuration, letting them be much |
| 26 | * simpler if they also don't support high speed operation (like this |
| 27 | * driver does). |
David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 28 | * |
| 29 | * Why is *this* driver using two configurations, rather than setting up |
| 30 | * two interfaces with different functions? To help verify that multiple |
| 31 | * configuration infrastucture is working correctly; also, so that it can |
| 32 | * work with low capability USB controllers without four bulk endpoints. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 35 | /* |
| 36 | * driver assumes self-powered hardware, and |
| 37 | * has no way for users to trigger remote wakeup. |
| 38 | */ |
| 39 | |
David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 40 | /* #define VERBOSE_DEBUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/device.h> |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 45 | #include <linux/module.h> |
| 46 | #include <linux/err.h> |
| 47 | #include <linux/usb/composite.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 49 | #include "g_zero.h" |
David Brownell | 7e75bc0 | 2008-08-18 17:41:31 -0700 | [diff] [blame] | 50 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 51 | USB_GADGET_COMPOSITE_OPTIONS(); |
David Brownell | 7e75bc0 | 2008-08-18 17:41:31 -0700 | [diff] [blame] | 52 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 53 | #define DRIVER_VERSION "Cinco de Mayo 2008" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 55 | static const char longname[] = "Gadget Zero"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Normally the "loopback" configuration is second (index 1) so |
| 59 | * it's not the default. Here's where to change that order, to |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 60 | * work better with hosts where config changes are problematic or |
| 61 | * controllers (like original superh) that only support one config. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 63 | static bool loopdefault = 0; |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 64 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Felipe Balbi | 38b3ad5 | 2013-01-18 13:18:44 +0200 | [diff] [blame] | 66 | static struct usb_zero_options gzero_options = { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 67 | .isoc_interval = 4, |
| 68 | .isoc_maxpacket = 1024, |
| 69 | .bulk_buflen = 4096, |
| 70 | .qlen = 32, |
| 71 | }; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /*-------------------------------------------------------------------------*/ |
| 74 | |
| 75 | /* Thanks to NetChip Technologies for donating this product ID. |
| 76 | * |
| 77 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 78 | * Instead: allocate your own, using normal USB-IF procedures. |
| 79 | */ |
| 80 | #ifndef CONFIG_USB_ZERO_HNPTEST |
| 81 | #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */ |
| 82 | #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */ |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 83 | #define DEFAULT_AUTORESUME 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #else |
| 85 | #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */ |
| 86 | #define DRIVER_PRODUCT_NUM 0xbadd |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 87 | #define DEFAULT_AUTORESUME 5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | #endif |
| 89 | |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 90 | /* If the optional "autoresume" mode is enabled, it provides good |
| 91 | * functional coverage for the "USBCV" test harness from USB-IF. |
| 92 | * It's always set if OTG mode is enabled. |
| 93 | */ |
| 94 | unsigned autoresume = DEFAULT_AUTORESUME; |
| 95 | module_param(autoresume, uint, S_IRUGO); |
| 96 | MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup"); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /*-------------------------------------------------------------------------*/ |
| 99 | |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 100 | static struct usb_device_descriptor device_desc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .bLength = sizeof device_desc, |
| 102 | .bDescriptorType = USB_DT_DEVICE, |
| 103 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 104 | .bcdUSB = cpu_to_le16(0x0200), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .bDeviceClass = USB_CLASS_VENDOR_SPEC, |
| 106 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 107 | .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM), |
| 108 | .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .bNumConfigurations = 2, |
| 110 | }; |
| 111 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 112 | #ifdef CONFIG_USB_OTG |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 113 | static struct usb_otg_descriptor otg_descriptor = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .bLength = sizeof otg_descriptor, |
| 115 | .bDescriptorType = USB_DT_OTG, |
| 116 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 117 | /* REVISIT SRP-only hardware is possible, although |
| 118 | * it would not be called "OTG" ... |
| 119 | */ |
| 120 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 123 | static const struct usb_descriptor_header *otg_desc[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | (struct usb_descriptor_header *) &otg_descriptor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | NULL, |
| 126 | }; |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 127 | #else |
| 128 | #define otg_desc NULL |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 129 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 131 | /* string IDs are assigned dynamically */ |
David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 132 | /* default serial number takes at least two packets */ |
| 133 | static char serial[] = "0123456789.0123456789.0123456789"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 135 | #define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0) |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 136 | #define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1) |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 137 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 138 | static struct usb_string strings_dev[] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 139 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 140 | [USB_GADGET_PRODUCT_IDX].s = longname, |
| 141 | [USB_GADGET_SERIAL_IDX].s = serial, |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 142 | [USB_GZERO_SS_DESC].s = "source and sink data", |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 143 | [USB_GZERO_LB_DESC].s = "loop input to output", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { } /* end of list */ |
| 145 | }; |
| 146 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 147 | static struct usb_gadget_strings stringtab_dev = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .language = 0x0409, /* en-us */ |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 149 | .strings = strings_dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 152 | static struct usb_gadget_strings *dev_strings[] = { |
| 153 | &stringtab_dev, |
| 154 | NULL, |
| 155 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | /*-------------------------------------------------------------------------*/ |
| 158 | |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 159 | static struct timer_list autoresume_timer; |
| 160 | |
| 161 | static void zero_autoresume(unsigned long _c) |
| 162 | { |
| 163 | struct usb_composite_dev *cdev = (void *)_c; |
| 164 | struct usb_gadget *g = cdev->gadget; |
| 165 | |
| 166 | /* unconfigured devices can't issue wakeups */ |
| 167 | if (!cdev->config) |
| 168 | return; |
| 169 | |
| 170 | /* Normally the host would be woken up for something |
| 171 | * more significant than just a timer firing; likely |
| 172 | * because of some direct user request. |
| 173 | */ |
| 174 | if (g->speed != USB_SPEED_UNKNOWN) { |
| 175 | int status = usb_gadget_wakeup(g); |
| 176 | INFO(cdev, "%s --> %d\n", __func__, status); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static void zero_suspend(struct usb_composite_dev *cdev) |
| 181 | { |
| 182 | if (cdev->gadget->speed == USB_SPEED_UNKNOWN) |
| 183 | return; |
| 184 | |
| 185 | if (autoresume) { |
| 186 | mod_timer(&autoresume_timer, jiffies + (HZ * autoresume)); |
| 187 | DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume); |
| 188 | } else |
| 189 | DBG(cdev, "%s\n", __func__); |
| 190 | } |
| 191 | |
| 192 | static void zero_resume(struct usb_composite_dev *cdev) |
| 193 | { |
| 194 | DBG(cdev, "%s\n", __func__); |
| 195 | del_timer(&autoresume_timer); |
| 196 | } |
| 197 | |
| 198 | /*-------------------------------------------------------------------------*/ |
| 199 | |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 200 | static struct usb_configuration loopback_driver = { |
| 201 | .label = "loopback", |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 202 | .bConfigurationValue = 2, |
| 203 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 204 | /* .iConfiguration = DYNAMIC */ |
| 205 | }; |
| 206 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 207 | static struct usb_function *func_ss; |
| 208 | static struct usb_function_instance *func_inst_ss; |
| 209 | |
| 210 | static int ss_config_setup(struct usb_configuration *c, |
| 211 | const struct usb_ctrlrequest *ctrl) |
| 212 | { |
| 213 | switch (ctrl->bRequest) { |
| 214 | case 0x5b: |
| 215 | case 0x5c: |
| 216 | return func_ss->setup(func_ss, ctrl); |
| 217 | default: |
| 218 | return -EOPNOTSUPP; |
| 219 | } |
| 220 | } |
| 221 | |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 222 | static struct usb_configuration sourcesink_driver = { |
| 223 | .label = "source/sink", |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 224 | .setup = ss_config_setup, |
| 225 | .bConfigurationValue = 3, |
| 226 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 227 | /* .iConfiguration = DYNAMIC */ |
| 228 | }; |
| 229 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 230 | module_param_named(buflen, gzero_options.bulk_buflen, uint, 0); |
| 231 | module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR); |
| 232 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none"); |
| 233 | |
| 234 | module_param_named(isoc_interval, gzero_options.isoc_interval, uint, |
| 235 | S_IRUGO|S_IWUSR); |
| 236 | MODULE_PARM_DESC(isoc_interval, "1 - 16"); |
| 237 | |
| 238 | module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint, |
| 239 | S_IRUGO|S_IWUSR); |
| 240 | MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); |
| 241 | |
| 242 | module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR); |
| 243 | MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)"); |
| 244 | |
| 245 | module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint, |
| 246 | S_IRUGO|S_IWUSR); |
| 247 | MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)"); |
| 248 | |
| 249 | static struct usb_function *func_lb; |
| 250 | static struct usb_function_instance *func_inst_lb; |
| 251 | |
| 252 | module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR); |
| 253 | MODULE_PARM_DESC(qlen, "depth of loopback queue"); |
| 254 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 255 | static int __init zero_bind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 257 | struct f_ss_opts *ss_opts; |
| 258 | struct f_lb_opts *lb_opts; |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 259 | int status; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 260 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 261 | /* Allocate string descriptor numbers ... note that string |
| 262 | * contents can be overridden by the composite_dev glue. |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 263 | */ |
Sebastian Andrzej Siewior | e1f15cc | 2012-09-06 20:11:16 +0200 | [diff] [blame] | 264 | status = usb_string_ids_tab(cdev, strings_dev); |
| 265 | if (status < 0) |
| 266 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 268 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
| 269 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
| 270 | device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id; |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 271 | |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 272 | setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev); |
| 273 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 274 | func_inst_ss = usb_get_function_instance("SourceSink"); |
| 275 | if (IS_ERR(func_inst_ss)) |
| 276 | return PTR_ERR(func_inst_ss); |
| 277 | |
| 278 | ss_opts = container_of(func_inst_ss, struct f_ss_opts, func_inst); |
| 279 | ss_opts->pattern = gzero_options.pattern; |
| 280 | ss_opts->isoc_interval = gzero_options.isoc_interval; |
| 281 | ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket; |
| 282 | ss_opts->isoc_mult = gzero_options.isoc_mult; |
| 283 | ss_opts->isoc_maxburst = gzero_options.isoc_maxpacket; |
| 284 | ss_opts->bulk_buflen = gzero_options.bulk_buflen; |
| 285 | |
| 286 | func_ss = usb_get_function(func_inst_ss); |
| 287 | if (IS_ERR(func_ss)) |
| 288 | goto err_put_func_inst_ss; |
| 289 | |
| 290 | func_inst_lb = usb_get_function_instance("Loopback"); |
| 291 | if (IS_ERR(func_inst_lb)) |
| 292 | goto err_put_func_ss; |
| 293 | |
| 294 | lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst); |
| 295 | lb_opts->bulk_buflen = gzero_options.bulk_buflen; |
| 296 | lb_opts->qlen = gzero_options.qlen; |
| 297 | |
| 298 | func_lb = usb_get_function(func_inst_lb); |
| 299 | if (IS_ERR(func_lb)) { |
| 300 | status = PTR_ERR(func_lb); |
| 301 | goto err_put_func_inst_lb; |
| 302 | } |
| 303 | |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 304 | sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id; |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 305 | loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id; |
| 306 | |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 307 | /* support autoresume for remote wakeup testing */ |
| 308 | sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 309 | loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP; |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 310 | sourcesink_driver.descriptors = NULL; |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 311 | loopback_driver.descriptors = NULL; |
| 312 | if (autoresume) { |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 313 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 314 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 315 | } |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 316 | |
| 317 | /* support OTG systems */ |
| 318 | if (gadget_is_otg(cdev->gadget)) { |
| 319 | sourcesink_driver.descriptors = otg_desc; |
| 320 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Sebastian Andrzej Siewior | 78f46f0 | 2012-12-23 21:09:59 +0100 | [diff] [blame] | 321 | loopback_driver.descriptors = otg_desc; |
| 322 | loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
Sebastian Andrzej Siewior | eeae540 | 2012-12-23 21:09:58 +0100 | [diff] [blame] | 323 | } |
| 324 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 325 | /* Register primary, then secondary configuration. Note that |
| 326 | * SH3 only allows one config... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | */ |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 328 | if (loopdefault) { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 329 | usb_add_config_only(cdev, &loopback_driver); |
| 330 | usb_add_config_only(cdev, &sourcesink_driver); |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 331 | } else { |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 332 | usb_add_config_only(cdev, &sourcesink_driver); |
| 333 | usb_add_config_only(cdev, &loopback_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 335 | status = usb_add_function(&sourcesink_driver, func_ss); |
| 336 | if (status) |
| 337 | goto err_conf_flb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 339 | usb_ep_autoconfig_reset(cdev->gadget); |
| 340 | status = usb_add_function(&loopback_driver, func_lb); |
| 341 | if (status) |
| 342 | goto err_conf_flb; |
| 343 | |
| 344 | usb_ep_autoconfig_reset(cdev->gadget); |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 345 | usb_composite_overwrite_options(cdev, &coverwrite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 347 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 350 | |
| 351 | err_conf_flb: |
| 352 | usb_put_function(func_lb); |
| 353 | func_lb = NULL; |
| 354 | err_put_func_inst_lb: |
| 355 | usb_put_function_instance(func_inst_lb); |
| 356 | func_inst_lb = NULL; |
| 357 | err_put_func_ss: |
| 358 | usb_put_function(func_ss); |
| 359 | func_ss = NULL; |
| 360 | err_put_func_inst_ss: |
| 361 | usb_put_function_instance(func_inst_ss); |
| 362 | func_inst_ss = NULL; |
| 363 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 366 | static int zero_unbind(struct usb_composite_dev *cdev) |
| 367 | { |
| 368 | del_timer_sync(&autoresume_timer); |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 369 | if (!IS_ERR_OR_NULL(func_ss)) |
| 370 | usb_put_function(func_ss); |
Andrzej Pietrasiewicz | abe7a40 | 2013-04-18 14:43:26 +0200 | [diff] [blame] | 371 | usb_put_function_instance(func_inst_ss); |
Sebastian Andrzej Siewior | cf9a08a | 2012-12-23 21:10:01 +0100 | [diff] [blame] | 372 | if (!IS_ERR_OR_NULL(func_lb)) |
| 373 | usb_put_function(func_lb); |
Andrzej Pietrasiewicz | abe7a40 | 2013-04-18 14:43:26 +0200 | [diff] [blame] | 374 | usb_put_function_instance(func_inst_lb); |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 378 | static __refdata struct usb_composite_driver zero_driver = { |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 379 | .name = "zero", |
| 380 | .dev = &device_desc, |
| 381 | .strings = dev_strings, |
Amit Blay | 57c97c0 | 2011-07-03 17:29:31 +0300 | [diff] [blame] | 382 | .max_speed = USB_SPEED_SUPER, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 383 | .bind = zero_bind, |
David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 384 | .unbind = zero_unbind, |
| 385 | .suspend = zero_suspend, |
| 386 | .resume = zero_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 389 | MODULE_AUTHOR("David Brownell"); |
| 390 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 392 | static int __init init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 394 | return usb_composite_probe(&zero_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 396 | module_init(init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 398 | static void __exit cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 400 | usb_composite_unregister(&zero_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 402 | module_exit(cleanup); |