linux/drivers/usb/gadget/legacy/audio.c
<<
>>
Prefs
   1/*
   2 * audio.c -- Audio gadget driver
   3 *
   4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
   5 * Copyright (C) 2008 Analog Devices, Inc
   6 *
   7 * Enter bugs at http://blackfin.uclinux.org/
   8 *
   9 * Licensed under the GPL-2 or later.
  10 */
  11
  12/* #define VERBOSE_DEBUG */
  13
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/usb/composite.h>
  17
  18#define DRIVER_DESC             "Linux USB Audio Gadget"
  19#define DRIVER_VERSION          "Feb 2, 2012"
  20
  21USB_GADGET_COMPOSITE_OPTIONS();
  22
  23#ifndef CONFIG_GADGET_UAC1
  24#include "u_uac2.h"
  25
  26/* Playback(USB-IN) Default Stereo - Fl/Fr */
  27static int p_chmask = UAC2_DEF_PCHMASK;
  28module_param(p_chmask, uint, S_IRUGO);
  29MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
  30
  31/* Playback Default 48 KHz */
  32static int p_srate = UAC2_DEF_PSRATE;
  33module_param(p_srate, uint, S_IRUGO);
  34MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
  35
  36/* Playback Default 16bits/sample */
  37static int p_ssize = UAC2_DEF_PSSIZE;
  38module_param(p_ssize, uint, S_IRUGO);
  39MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
  40
  41/* Capture(USB-OUT) Default Stereo - Fl/Fr */
  42static int c_chmask = UAC2_DEF_CCHMASK;
  43module_param(c_chmask, uint, S_IRUGO);
  44MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
  45
  46/* Capture Default 64 KHz */
  47static int c_srate = UAC2_DEF_CSRATE;
  48module_param(c_srate, uint, S_IRUGO);
  49MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
  50
  51/* Capture Default 16bits/sample */
  52static int c_ssize = UAC2_DEF_CSSIZE;
  53module_param(c_ssize, uint, S_IRUGO);
  54MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
  55#else
  56#include "u_uac1.h"
  57
  58static char *fn_play = FILE_PCM_PLAYBACK;
  59module_param(fn_play, charp, S_IRUGO);
  60MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
  61
  62static char *fn_cap = FILE_PCM_CAPTURE;
  63module_param(fn_cap, charp, S_IRUGO);
  64MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
  65
  66static char *fn_cntl = FILE_CONTROL;
  67module_param(fn_cntl, charp, S_IRUGO);
  68MODULE_PARM_DESC(fn_cntl, "Control device file name");
  69
  70static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
  71module_param(req_buf_size, int, S_IRUGO);
  72MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
  73
  74static int req_count = UAC1_REQ_COUNT;
  75module_param(req_count, int, S_IRUGO);
  76MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
  77
  78static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
  79module_param(audio_buf_size, int, S_IRUGO);
  80MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
  81#endif
  82
  83/* string IDs are assigned dynamically */
  84
  85static struct usb_string strings_dev[] = {
  86        [USB_GADGET_MANUFACTURER_IDX].s = "",
  87        [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  88        [USB_GADGET_SERIAL_IDX].s = "",
  89        {  } /* end of list */
  90};
  91
  92static struct usb_gadget_strings stringtab_dev = {
  93        .language = 0x0409,     /* en-us */
  94        .strings = strings_dev,
  95};
  96
  97static struct usb_gadget_strings *audio_strings[] = {
  98        &stringtab_dev,
  99        NULL,
 100};
 101
 102#ifndef CONFIG_GADGET_UAC1
 103static struct usb_function_instance *fi_uac2;
 104static struct usb_function *f_uac2;
 105#else
 106static struct usb_function_instance *fi_uac1;
 107static struct usb_function *f_uac1;
 108#endif
 109
 110/*-------------------------------------------------------------------------*/
 111
 112/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 113 * Instead:  allocate your own, using normal USB-IF procedures.
 114 */
 115
 116/* Thanks to Linux Foundation for donating this product ID. */
 117#define AUDIO_VENDOR_NUM                0x1d6b  /* Linux Foundation */
 118#define AUDIO_PRODUCT_NUM               0x0101  /* Linux-USB Audio Gadget */
 119
 120/*-------------------------------------------------------------------------*/
 121
 122static struct usb_device_descriptor device_desc = {
 123        .bLength =              sizeof device_desc,
 124        .bDescriptorType =      USB_DT_DEVICE,
 125
 126        /* .bcdUSB = DYNAMIC */
 127
 128#ifdef CONFIG_GADGET_UAC1
 129        .bDeviceClass =         USB_CLASS_PER_INTERFACE,
 130        .bDeviceSubClass =      0,
 131        .bDeviceProtocol =      0,
 132#else
 133        .bDeviceClass =         USB_CLASS_MISC,
 134        .bDeviceSubClass =      0x02,
 135        .bDeviceProtocol =      0x01,
 136#endif
 137        /* .bMaxPacketSize0 = f(hardware) */
 138
 139        /* Vendor and product id defaults change according to what configs
 140         * we support.  (As does bNumConfigurations.)  These values can
 141         * also be overridden by module parameters.
 142         */
 143        .idVendor =             cpu_to_le16(AUDIO_VENDOR_NUM),
 144        .idProduct =            cpu_to_le16(AUDIO_PRODUCT_NUM),
 145        /* .bcdDevice = f(hardware) */
 146        /* .iManufacturer = DYNAMIC */
 147        /* .iProduct = DYNAMIC */
 148        /* NO SERIAL NUMBER */
 149        .bNumConfigurations =   1,
 150};
 151
 152static const struct usb_descriptor_header *otg_desc[2];
 153
 154/*-------------------------------------------------------------------------*/
 155
 156static int audio_do_config(struct usb_configuration *c)
 157{
 158        int status;
 159
 160        /* FIXME alloc iConfiguration string, set it in c->strings */
 161
 162        if (gadget_is_otg(c->cdev->gadget)) {
 163                c->descriptors = otg_desc;
 164                c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
 165        }
 166
 167#ifdef CONFIG_GADGET_UAC1
 168        f_uac1 = usb_get_function(fi_uac1);
 169        if (IS_ERR(f_uac1)) {
 170                status = PTR_ERR(f_uac1);
 171                return status;
 172        }
 173
 174        status = usb_add_function(c, f_uac1);
 175        if (status < 0) {
 176                usb_put_function(f_uac1);
 177                return status;
 178        }
 179#else
 180        f_uac2 = usb_get_function(fi_uac2);
 181        if (IS_ERR(f_uac2)) {
 182                status = PTR_ERR(f_uac2);
 183                return status;
 184        }
 185
 186        status = usb_add_function(c, f_uac2);
 187        if (status < 0) {
 188                usb_put_function(f_uac2);
 189                return status;
 190        }
 191#endif
 192
 193        return 0;
 194}
 195
 196static struct usb_configuration audio_config_driver = {
 197        .label                  = DRIVER_DESC,
 198        .bConfigurationValue    = 1,
 199        /* .iConfiguration = DYNAMIC */
 200        .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
 201};
 202
 203/*-------------------------------------------------------------------------*/
 204
 205static int audio_bind(struct usb_composite_dev *cdev)
 206{
 207#ifndef CONFIG_GADGET_UAC1
 208        struct f_uac2_opts      *uac2_opts;
 209#else
 210        struct f_uac1_opts      *uac1_opts;
 211#endif
 212        int                     status;
 213
 214#ifndef CONFIG_GADGET_UAC1
 215        fi_uac2 = usb_get_function_instance("uac2");
 216        if (IS_ERR(fi_uac2))
 217                return PTR_ERR(fi_uac2);
 218#else
 219        fi_uac1 = usb_get_function_instance("uac1");
 220        if (IS_ERR(fi_uac1))
 221                return PTR_ERR(fi_uac1);
 222#endif
 223
 224#ifndef CONFIG_GADGET_UAC1
 225        uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
 226        uac2_opts->p_chmask = p_chmask;
 227        uac2_opts->p_srate = p_srate;
 228        uac2_opts->p_ssize = p_ssize;
 229        uac2_opts->c_chmask = c_chmask;
 230        uac2_opts->c_srate = c_srate;
 231        uac2_opts->c_ssize = c_ssize;
 232#else
 233        uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
 234        uac1_opts->fn_play = fn_play;
 235        uac1_opts->fn_cap = fn_cap;
 236        uac1_opts->fn_cntl = fn_cntl;
 237        uac1_opts->req_buf_size = req_buf_size;
 238        uac1_opts->req_count = req_count;
 239        uac1_opts->audio_buf_size = audio_buf_size;
 240#endif
 241
 242        status = usb_string_ids_tab(cdev, strings_dev);
 243        if (status < 0)
 244                goto fail;
 245        device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
 246        device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
 247
 248        if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
 249                struct usb_descriptor_header *usb_desc;
 250
 251                usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
 252                if (!usb_desc)
 253                        goto fail;
 254                usb_otg_descriptor_init(cdev->gadget, usb_desc);
 255                otg_desc[0] = usb_desc;
 256                otg_desc[1] = NULL;
 257        }
 258
 259        status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
 260        if (status < 0)
 261                goto fail_otg_desc;
 262        usb_composite_overwrite_options(cdev, &coverwrite);
 263
 264        INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
 265        return 0;
 266
 267fail_otg_desc:
 268        kfree(otg_desc[0]);
 269        otg_desc[0] = NULL;
 270fail:
 271#ifndef CONFIG_GADGET_UAC1
 272        usb_put_function_instance(fi_uac2);
 273#else
 274        usb_put_function_instance(fi_uac1);
 275#endif
 276        return status;
 277}
 278
 279static int audio_unbind(struct usb_composite_dev *cdev)
 280{
 281#ifdef CONFIG_GADGET_UAC1
 282        if (!IS_ERR_OR_NULL(f_uac1))
 283                usb_put_function(f_uac1);
 284        if (!IS_ERR_OR_NULL(fi_uac1))
 285                usb_put_function_instance(fi_uac1);
 286#else
 287        if (!IS_ERR_OR_NULL(f_uac2))
 288                usb_put_function(f_uac2);
 289        if (!IS_ERR_OR_NULL(fi_uac2))
 290                usb_put_function_instance(fi_uac2);
 291#endif
 292        kfree(otg_desc[0]);
 293        otg_desc[0] = NULL;
 294
 295        return 0;
 296}
 297
 298static struct usb_composite_driver audio_driver = {
 299        .name           = "g_audio",
 300        .dev            = &device_desc,
 301        .strings        = audio_strings,
 302        .max_speed      = USB_SPEED_HIGH,
 303        .bind           = audio_bind,
 304        .unbind         = audio_unbind,
 305};
 306
 307module_usb_composite_driver(audio_driver);
 308
 309MODULE_DESCRIPTION(DRIVER_DESC);
 310MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
 311MODULE_LICENSE("GPL");
 312
 313