linux/drivers/usb/gadget/legacy/zero.c
<<
>>
Prefs
   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/master side "usbtest" driver to get a basic
  15 * functional 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/*-------------------------------------------------------------------------*/
  47USB_GADGET_COMPOSITE_OPTIONS();
  48
  49#define DRIVER_VERSION          "Cinco de Mayo 2008"
  50
  51static 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 */
  59static bool loopdefault = 0;
  60module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
  61
  62static 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 */
  92static unsigned autoresume = DEFAULT_AUTORESUME;
  93module_param(autoresume, uint, S_IRUGO);
  94MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
  95
  96/* Maximum Autoresume time */
  97static unsigned max_autoresume;
  98module_param(max_autoresume, uint, S_IRUGO);
  99MODULE_PARM_DESC(max_autoresume, "maximum seconds before remote wakeup");
 100
 101/* Interval between two remote wakeups */
 102static unsigned autoresume_interval_ms;
 103module_param(autoresume_interval_ms, uint, S_IRUGO);
 104MODULE_PARM_DESC(autoresume_interval_ms,
 105                "milliseconds to increase successive wakeup delays");
 106
 107static unsigned autoresume_step_ms;
 108/*-------------------------------------------------------------------------*/
 109
 110static 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
 122static const struct usb_descriptor_header *otg_desc[2];
 123
 124/* string IDs are assigned dynamically */
 125/* default serial number takes at least two packets */
 126static 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
 131static 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
 140static struct usb_gadget_strings stringtab_dev = {
 141        .language       = 0x0409,       /* en-us */
 142        .strings        = strings_dev,
 143};
 144
 145static struct usb_gadget_strings *dev_strings[] = {
 146        &stringtab_dev,
 147        NULL,
 148};
 149
 150/*-------------------------------------------------------------------------*/
 151
 152static struct timer_list        autoresume_timer;
 153static struct usb_composite_dev *autoresume_cdev;
 154
 155static void zero_autoresume(struct timer_list *unused)
 156{
 157        struct usb_composite_dev        *cdev = autoresume_cdev;
 158        struct usb_gadget               *g = cdev->gadget;
 159
 160        /* unconfigured devices can't issue wakeups */
 161        if (!cdev->config)
 162                return;
 163
 164        /* Normally the host would be woken up for something
 165         * more significant than just a timer firing; likely
 166         * because of some direct user request.
 167         */
 168        if (g->speed != USB_SPEED_UNKNOWN) {
 169                int status = usb_gadget_wakeup(g);
 170                INFO(cdev, "%s --> %d\n", __func__, status);
 171        }
 172}
 173
 174static void zero_suspend(struct usb_composite_dev *cdev)
 175{
 176        if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
 177                return;
 178
 179        if (autoresume) {
 180                if (max_autoresume &&
 181                        (autoresume_step_ms > max_autoresume * 1000))
 182                                autoresume_step_ms = autoresume * 1000;
 183
 184                mod_timer(&autoresume_timer, jiffies +
 185                        msecs_to_jiffies(autoresume_step_ms));
 186                DBG(cdev, "suspend, wakeup in %d milliseconds\n",
 187                        autoresume_step_ms);
 188
 189                autoresume_step_ms += autoresume_interval_ms;
 190        } else
 191                DBG(cdev, "%s\n", __func__);
 192}
 193
 194static void zero_resume(struct usb_composite_dev *cdev)
 195{
 196        DBG(cdev, "%s\n", __func__);
 197        del_timer(&autoresume_timer);
 198}
 199
 200/*-------------------------------------------------------------------------*/
 201
 202static struct usb_configuration loopback_driver = {
 203        .label          = "loopback",
 204        .bConfigurationValue = 2,
 205        .bmAttributes   = USB_CONFIG_ATT_SELFPOWER,
 206        /* .iConfiguration = DYNAMIC */
 207};
 208
 209static struct usb_function *func_ss;
 210static struct usb_function_instance *func_inst_ss;
 211
 212static int ss_config_setup(struct usb_configuration *c,
 213                const struct usb_ctrlrequest *ctrl)
 214{
 215        switch (ctrl->bRequest) {
 216        case 0x5b:
 217        case 0x5c:
 218                return func_ss->setup(func_ss, ctrl);
 219        default:
 220                return -EOPNOTSUPP;
 221        }
 222}
 223
 224static struct usb_configuration sourcesink_driver = {
 225        .label                  = "source/sink",
 226        .setup                  = ss_config_setup,
 227        .bConfigurationValue    = 3,
 228        .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
 229        /* .iConfiguration      = DYNAMIC */
 230};
 231
 232module_param_named(buflen, gzero_options.bulk_buflen, uint, 0);
 233module_param_named(pattern, gzero_options.pattern, uint, S_IRUGO|S_IWUSR);
 234MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
 235
 236module_param_named(isoc_interval, gzero_options.isoc_interval, uint,
 237                S_IRUGO|S_IWUSR);
 238MODULE_PARM_DESC(isoc_interval, "1 - 16");
 239
 240module_param_named(isoc_maxpacket, gzero_options.isoc_maxpacket, uint,
 241                S_IRUGO|S_IWUSR);
 242MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
 243
 244module_param_named(isoc_mult, gzero_options.isoc_mult, uint, S_IRUGO|S_IWUSR);
 245MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
 246
 247module_param_named(isoc_maxburst, gzero_options.isoc_maxburst, uint,
 248                S_IRUGO|S_IWUSR);
 249MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
 250
 251static struct usb_function *func_lb;
 252static struct usb_function_instance *func_inst_lb;
 253
 254module_param_named(qlen, gzero_options.qlen, uint, S_IRUGO|S_IWUSR);
 255MODULE_PARM_DESC(qlen, "depth of loopback queue");
 256
 257module_param_named(ss_bulk_qlen, gzero_options.ss_bulk_qlen, uint,
 258                S_IRUGO|S_IWUSR);
 259MODULE_PARM_DESC(bulk_qlen, "depth of sourcesink queue for bulk transfer");
 260
 261module_param_named(ss_iso_qlen, gzero_options.ss_iso_qlen, uint,
 262                S_IRUGO|S_IWUSR);
 263MODULE_PARM_DESC(iso_qlen, "depth of sourcesink queue for iso transfer");
 264
 265static int zero_bind(struct usb_composite_dev *cdev)
 266{
 267        struct f_ss_opts        *ss_opts;
 268        struct f_lb_opts        *lb_opts;
 269        int                     status;
 270
 271        /* Allocate string descriptor numbers ... note that string
 272         * contents can be overridden by the composite_dev glue.
 273         */
 274        status = usb_string_ids_tab(cdev, strings_dev);
 275        if (status < 0)
 276                return status;
 277
 278        device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
 279        device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
 280        device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
 281
 282        autoresume_cdev = cdev;
 283        timer_setup(&autoresume_timer, zero_autoresume, 0);
 284
 285        func_inst_ss = usb_get_function_instance("SourceSink");
 286        if (IS_ERR(func_inst_ss))
 287                return PTR_ERR(func_inst_ss);
 288
 289        ss_opts =  container_of(func_inst_ss, struct f_ss_opts, func_inst);
 290        ss_opts->pattern = gzero_options.pattern;
 291        ss_opts->isoc_interval = gzero_options.isoc_interval;
 292        ss_opts->isoc_maxpacket = gzero_options.isoc_maxpacket;
 293        ss_opts->isoc_mult = gzero_options.isoc_mult;
 294        ss_opts->isoc_maxburst = gzero_options.isoc_maxburst;
 295        ss_opts->bulk_buflen = gzero_options.bulk_buflen;
 296        ss_opts->bulk_qlen = gzero_options.ss_bulk_qlen;
 297        ss_opts->iso_qlen = gzero_options.ss_iso_qlen;
 298
 299        func_ss = usb_get_function(func_inst_ss);
 300        if (IS_ERR(func_ss)) {
 301                status = PTR_ERR(func_ss);
 302                goto err_put_func_inst_ss;
 303        }
 304
 305        func_inst_lb = usb_get_function_instance("Loopback");
 306        if (IS_ERR(func_inst_lb)) {
 307                status = PTR_ERR(func_inst_lb);
 308                goto err_put_func_ss;
 309        }
 310
 311        lb_opts = container_of(func_inst_lb, struct f_lb_opts, func_inst);
 312        lb_opts->bulk_buflen = gzero_options.bulk_buflen;
 313        lb_opts->qlen = gzero_options.qlen;
 314
 315        func_lb = usb_get_function(func_inst_lb);
 316        if (IS_ERR(func_lb)) {
 317                status = PTR_ERR(func_lb);
 318                goto err_put_func_inst_lb;
 319        }
 320
 321        sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
 322        loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
 323
 324        /* support autoresume for remote wakeup testing */
 325        sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
 326        loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
 327        sourcesink_driver.descriptors = NULL;
 328        loopback_driver.descriptors = NULL;
 329        if (autoresume) {
 330                sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 331                loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 332                autoresume_step_ms = autoresume * 1000;
 333        }
 334
 335        /* support OTG systems */
 336        if (gadget_is_otg(cdev->gadget)) {
 337                if (!otg_desc[0]) {
 338                        struct usb_descriptor_header *usb_desc;
 339
 340                        usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
 341                        if (!usb_desc) {
 342                                status = -ENOMEM;
 343                                goto err_conf_flb;
 344                        }
 345                        usb_otg_descriptor_init(cdev->gadget, usb_desc);
 346                        otg_desc[0] = usb_desc;
 347                        otg_desc[1] = NULL;
 348                }
 349                sourcesink_driver.descriptors = otg_desc;
 350                sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 351                loopback_driver.descriptors = otg_desc;
 352                loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 353        }
 354
 355        /* Register primary, then secondary configuration.  Note that
 356         * SH3 only allows one config...
 357         */
 358        if (loopdefault) {
 359                usb_add_config_only(cdev, &loopback_driver);
 360                usb_add_config_only(cdev, &sourcesink_driver);
 361        } else {
 362                usb_add_config_only(cdev, &sourcesink_driver);
 363                usb_add_config_only(cdev, &loopback_driver);
 364        }
 365        status = usb_add_function(&sourcesink_driver, func_ss);
 366        if (status)
 367                goto err_free_otg_desc;
 368
 369        usb_ep_autoconfig_reset(cdev->gadget);
 370        status = usb_add_function(&loopback_driver, func_lb);
 371        if (status)
 372                goto err_free_otg_desc;
 373
 374        usb_ep_autoconfig_reset(cdev->gadget);
 375        usb_composite_overwrite_options(cdev, &coverwrite);
 376
 377        INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
 378
 379        return 0;
 380
 381err_free_otg_desc:
 382        kfree(otg_desc[0]);
 383        otg_desc[0] = NULL;
 384err_conf_flb:
 385        usb_put_function(func_lb);
 386        func_lb = NULL;
 387err_put_func_inst_lb:
 388        usb_put_function_instance(func_inst_lb);
 389        func_inst_lb = NULL;
 390err_put_func_ss:
 391        usb_put_function(func_ss);
 392        func_ss = NULL;
 393err_put_func_inst_ss:
 394        usb_put_function_instance(func_inst_ss);
 395        func_inst_ss = NULL;
 396        return status;
 397}
 398
 399static int zero_unbind(struct usb_composite_dev *cdev)
 400{
 401        del_timer_sync(&autoresume_timer);
 402        if (!IS_ERR_OR_NULL(func_ss))
 403                usb_put_function(func_ss);
 404        usb_put_function_instance(func_inst_ss);
 405        if (!IS_ERR_OR_NULL(func_lb))
 406                usb_put_function(func_lb);
 407        usb_put_function_instance(func_inst_lb);
 408        kfree(otg_desc[0]);
 409        otg_desc[0] = NULL;
 410
 411        return 0;
 412}
 413
 414static struct usb_composite_driver zero_driver = {
 415        .name           = "zero",
 416        .dev            = &device_desc,
 417        .strings        = dev_strings,
 418        .max_speed      = USB_SPEED_SUPER,
 419        .bind           = zero_bind,
 420        .unbind         = zero_unbind,
 421        .suspend        = zero_suspend,
 422        .resume         = zero_resume,
 423};
 424
 425module_usb_composite_driver(zero_driver);
 426
 427MODULE_AUTHOR("David Brownell");
 428MODULE_LICENSE("GPL");
 429