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