2 * audio.c -- Audio gadget driver
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
7 * Enter bugs at http://blackfin.uclinux.org/
9 * Licensed under the GPL-2 or later.
12 /* #define VERBOSE_DEBUG */
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/usb/composite.h>
18 #define DRIVER_DESC "Linux USB Audio Gadget"
19 #define DRIVER_VERSION "Feb 2, 2012"
21 USB_GADGET_COMPOSITE_OPTIONS();
23 #ifndef CONFIG_GADGET_UAC1
26 /* Playback(USB-IN) Default Stereo - Fl/Fr */
27 static int p_chmask = UAC2_DEF_PCHMASK;
28 module_param(p_chmask, uint, S_IRUGO);
29 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
31 /* Playback Default 48 KHz */
32 static int p_srate = UAC2_DEF_PSRATE;
33 module_param(p_srate, uint, S_IRUGO);
34 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
36 /* Playback Default 16bits/sample */
37 static int p_ssize = UAC2_DEF_PSSIZE;
38 module_param(p_ssize, uint, S_IRUGO);
39 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
41 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
42 static int c_chmask = UAC2_DEF_CCHMASK;
43 module_param(c_chmask, uint, S_IRUGO);
44 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
46 /* Capture Default 64 KHz */
47 static int c_srate = UAC2_DEF_CSRATE;
48 module_param(c_srate, uint, S_IRUGO);
49 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
51 /* Capture Default 16bits/sample */
52 static int c_ssize = UAC2_DEF_CSSIZE;
53 module_param(c_ssize, uint, S_IRUGO);
54 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
56 #ifndef CONFIG_GADGET_UAC1_LEGACY
59 /* Playback(USB-IN) Default Stereo - Fl/Fr */
60 static int p_chmask = UAC1_DEF_PCHMASK;
61 module_param(p_chmask, uint, S_IRUGO);
62 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
64 /* Playback Default 48 KHz */
65 static int p_srate = UAC1_DEF_PSRATE;
66 module_param(p_srate, uint, S_IRUGO);
67 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
69 /* Playback Default 16bits/sample */
70 static int p_ssize = UAC1_DEF_PSSIZE;
71 module_param(p_ssize, uint, S_IRUGO);
72 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
74 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
75 static int c_chmask = UAC1_DEF_CCHMASK;
76 module_param(c_chmask, uint, S_IRUGO);
77 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
79 /* Capture Default 48 KHz */
80 static int c_srate = UAC1_DEF_CSRATE;
81 module_param(c_srate, uint, S_IRUGO);
82 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
84 /* Capture Default 16bits/sample */
85 static int c_ssize = UAC1_DEF_CSSIZE;
86 module_param(c_ssize, uint, S_IRUGO);
87 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
88 #else /* CONFIG_GADGET_UAC1_LEGACY */
89 #include "u_uac1_legacy.h"
91 static char *fn_play = FILE_PCM_PLAYBACK;
92 module_param(fn_play, charp, S_IRUGO);
93 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
95 static char *fn_cap = FILE_PCM_CAPTURE;
96 module_param(fn_cap, charp, S_IRUGO);
97 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
99 static char *fn_cntl = FILE_CONTROL;
100 module_param(fn_cntl, charp, S_IRUGO);
101 MODULE_PARM_DESC(fn_cntl, "Control device file name");
103 static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
104 module_param(req_buf_size, int, S_IRUGO);
105 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
107 static int req_count = UAC1_REQ_COUNT;
108 module_param(req_count, int, S_IRUGO);
109 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
111 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
112 module_param(audio_buf_size, int, S_IRUGO);
113 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
114 #endif /* CONFIG_GADGET_UAC1_LEGACY */
117 /* string IDs are assigned dynamically */
119 static struct usb_string strings_dev[] = {
120 [USB_GADGET_MANUFACTURER_IDX].s = "",
121 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
122 [USB_GADGET_SERIAL_IDX].s = "",
123 { } /* end of list */
126 static struct usb_gadget_strings stringtab_dev = {
127 .language = 0x0409, /* en-us */
128 .strings = strings_dev,
131 static struct usb_gadget_strings *audio_strings[] = {
136 #ifndef CONFIG_GADGET_UAC1
137 static struct usb_function_instance *fi_uac2;
138 static struct usb_function *f_uac2;
140 static struct usb_function_instance *fi_uac1;
141 static struct usb_function *f_uac1;
144 /*-------------------------------------------------------------------------*/
146 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
147 * Instead: allocate your own, using normal USB-IF procedures.
150 /* Thanks to Linux Foundation for donating this product ID. */
151 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
152 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
154 /*-------------------------------------------------------------------------*/
156 static struct usb_device_descriptor device_desc = {
157 .bLength = sizeof device_desc,
158 .bDescriptorType = USB_DT_DEVICE,
160 /* .bcdUSB = DYNAMIC */
162 #ifdef CONFIG_GADGET_UAC1_LEGACY
163 .bDeviceClass = USB_CLASS_PER_INTERFACE,
164 .bDeviceSubClass = 0,
165 .bDeviceProtocol = 0,
167 .bDeviceClass = USB_CLASS_MISC,
168 .bDeviceSubClass = 0x02,
169 .bDeviceProtocol = 0x01,
171 /* .bMaxPacketSize0 = f(hardware) */
173 /* Vendor and product id defaults change according to what configs
174 * we support. (As does bNumConfigurations.) These values can
175 * also be overridden by module parameters.
177 .idVendor = cpu_to_le16(AUDIO_VENDOR_NUM),
178 .idProduct = cpu_to_le16(AUDIO_PRODUCT_NUM),
179 /* .bcdDevice = f(hardware) */
180 /* .iManufacturer = DYNAMIC */
181 /* .iProduct = DYNAMIC */
182 /* NO SERIAL NUMBER */
183 .bNumConfigurations = 1,
186 static const struct usb_descriptor_header *otg_desc[2];
188 /*-------------------------------------------------------------------------*/
190 static int audio_do_config(struct usb_configuration *c)
194 /* FIXME alloc iConfiguration string, set it in c->strings */
196 if (gadget_is_otg(c->cdev->gadget)) {
197 c->descriptors = otg_desc;
198 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
201 #ifdef CONFIG_GADGET_UAC1
202 f_uac1 = usb_get_function(fi_uac1);
203 if (IS_ERR(f_uac1)) {
204 status = PTR_ERR(f_uac1);
208 status = usb_add_function(c, f_uac1);
210 usb_put_function(f_uac1);
214 f_uac2 = usb_get_function(fi_uac2);
215 if (IS_ERR(f_uac2)) {
216 status = PTR_ERR(f_uac2);
220 status = usb_add_function(c, f_uac2);
222 usb_put_function(f_uac2);
230 static struct usb_configuration audio_config_driver = {
231 .label = DRIVER_DESC,
232 .bConfigurationValue = 1,
233 /* .iConfiguration = DYNAMIC */
234 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
237 /*-------------------------------------------------------------------------*/
239 static int audio_bind(struct usb_composite_dev *cdev)
241 #ifndef CONFIG_GADGET_UAC1
242 struct f_uac2_opts *uac2_opts;
244 #ifndef CONFIG_GADGET_UAC1_LEGACY
245 struct f_uac1_opts *uac1_opts;
247 struct f_uac1_legacy_opts *uac1_opts;
252 #ifndef CONFIG_GADGET_UAC1
253 fi_uac2 = usb_get_function_instance("uac2");
255 return PTR_ERR(fi_uac2);
257 #ifndef CONFIG_GADGET_UAC1_LEGACY
258 fi_uac1 = usb_get_function_instance("uac1");
260 fi_uac1 = usb_get_function_instance("uac1_legacy");
263 return PTR_ERR(fi_uac1);
266 #ifndef CONFIG_GADGET_UAC1
267 uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
268 uac2_opts->p_chmask = p_chmask;
269 uac2_opts->p_srate = p_srate;
270 uac2_opts->p_ssize = p_ssize;
271 uac2_opts->c_chmask = c_chmask;
272 uac2_opts->c_srate = c_srate;
273 uac2_opts->c_ssize = c_ssize;
274 uac2_opts->req_number = UAC2_DEF_REQ_NUM;
276 #ifndef CONFIG_GADGET_UAC1_LEGACY
277 uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
278 uac1_opts->p_chmask = p_chmask;
279 uac1_opts->p_srate = p_srate;
280 uac1_opts->p_ssize = p_ssize;
281 uac1_opts->c_chmask = c_chmask;
282 uac1_opts->c_srate = c_srate;
283 uac1_opts->c_ssize = c_ssize;
284 uac1_opts->req_number = UAC1_DEF_REQ_NUM;
285 #else /* CONFIG_GADGET_UAC1_LEGACY */
286 uac1_opts = container_of(fi_uac1, struct f_uac1_legacy_opts, func_inst);
287 uac1_opts->fn_play = fn_play;
288 uac1_opts->fn_cap = fn_cap;
289 uac1_opts->fn_cntl = fn_cntl;
290 uac1_opts->req_buf_size = req_buf_size;
291 uac1_opts->req_count = req_count;
292 uac1_opts->audio_buf_size = audio_buf_size;
293 #endif /* CONFIG_GADGET_UAC1_LEGACY */
296 status = usb_string_ids_tab(cdev, strings_dev);
299 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
300 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
302 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
303 struct usb_descriptor_header *usb_desc;
305 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
308 usb_otg_descriptor_init(cdev->gadget, usb_desc);
309 otg_desc[0] = usb_desc;
313 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
316 usb_composite_overwrite_options(cdev, &coverwrite);
318 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
325 #ifndef CONFIG_GADGET_UAC1
326 usb_put_function_instance(fi_uac2);
328 usb_put_function_instance(fi_uac1);
333 static int audio_unbind(struct usb_composite_dev *cdev)
335 #ifdef CONFIG_GADGET_UAC1
336 if (!IS_ERR_OR_NULL(f_uac1))
337 usb_put_function(f_uac1);
338 if (!IS_ERR_OR_NULL(fi_uac1))
339 usb_put_function_instance(fi_uac1);
341 if (!IS_ERR_OR_NULL(f_uac2))
342 usb_put_function(f_uac2);
343 if (!IS_ERR_OR_NULL(fi_uac2))
344 usb_put_function_instance(fi_uac2);
352 static struct usb_composite_driver audio_driver = {
355 .strings = audio_strings,
356 .max_speed = USB_SPEED_HIGH,
358 .unbind = audio_unbind,
361 module_usb_composite_driver(audio_driver);
363 MODULE_DESCRIPTION(DRIVER_DESC);
364 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
365 MODULE_LICENSE("GPL");