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