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