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