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)");
58 static char *fn_play = FILE_PCM_PLAYBACK;
59 module_param(fn_play, charp, S_IRUGO);
60 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
62 static char *fn_cap = FILE_PCM_CAPTURE;
63 module_param(fn_cap, charp, S_IRUGO);
64 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
66 static char *fn_cntl = FILE_CONTROL;
67 module_param(fn_cntl, charp, S_IRUGO);
68 MODULE_PARM_DESC(fn_cntl, "Control device file name");
70 static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
71 module_param(req_buf_size, int, S_IRUGO);
72 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
74 static int req_count = UAC1_REQ_COUNT;
75 module_param(req_count, int, S_IRUGO);
76 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
78 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
79 module_param(audio_buf_size, int, S_IRUGO);
80 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
83 /* string IDs are assigned dynamically */
85 static 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 = "",
92 static struct usb_gadget_strings stringtab_dev = {
93 .language = 0x0409, /* en-us */
94 .strings = strings_dev,
97 static struct usb_gadget_strings *audio_strings[] = {
102 #ifndef CONFIG_GADGET_UAC1
103 static struct usb_function_instance *fi_uac2;
104 static struct usb_function *f_uac2;
106 static struct usb_function_instance *fi_uac1;
107 static struct usb_function *f_uac1;
110 /*-------------------------------------------------------------------------*/
112 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
113 * Instead: allocate your own, using normal USB-IF procedures.
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 */
120 /*-------------------------------------------------------------------------*/
122 static struct usb_device_descriptor device_desc = {
123 .bLength = sizeof device_desc,
124 .bDescriptorType = USB_DT_DEVICE,
126 .bcdUSB = cpu_to_le16(0x200),
128 #ifdef CONFIG_GADGET_UAC1
129 .bDeviceClass = USB_CLASS_PER_INTERFACE,
130 .bDeviceSubClass = 0,
131 .bDeviceProtocol = 0,
133 .bDeviceClass = USB_CLASS_MISC,
134 .bDeviceSubClass = 0x02,
135 .bDeviceProtocol = 0x01,
137 /* .bMaxPacketSize0 = f(hardware) */
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.
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,
152 static const struct usb_descriptor_header *otg_desc[2];
154 /*-------------------------------------------------------------------------*/
156 static int audio_do_config(struct usb_configuration *c)
160 /* FIXME alloc iConfiguration string, set it in c->strings */
162 if (gadget_is_otg(c->cdev->gadget)) {
163 c->descriptors = otg_desc;
164 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
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);
174 status = usb_add_function(c, f_uac1);
176 usb_put_function(f_uac1);
180 f_uac2 = usb_get_function(fi_uac2);
181 if (IS_ERR(f_uac2)) {
182 status = PTR_ERR(f_uac2);
186 status = usb_add_function(c, f_uac2);
188 usb_put_function(f_uac2);
196 static struct usb_configuration audio_config_driver = {
197 .label = DRIVER_DESC,
198 .bConfigurationValue = 1,
199 /* .iConfiguration = DYNAMIC */
200 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
203 /*-------------------------------------------------------------------------*/
205 static int audio_bind(struct usb_composite_dev *cdev)
207 #ifndef CONFIG_GADGET_UAC1
208 struct f_uac2_opts *uac2_opts;
210 struct f_uac1_opts *uac1_opts;
214 #ifndef CONFIG_GADGET_UAC1
215 fi_uac2 = usb_get_function_instance("uac2");
217 return PTR_ERR(fi_uac2);
219 fi_uac1 = usb_get_function_instance("uac1");
221 return PTR_ERR(fi_uac1);
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;
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;
242 status = usb_string_ids_tab(cdev, strings_dev);
245 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
246 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
248 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
249 struct usb_descriptor_header *usb_desc;
251 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
254 usb_otg_descriptor_init(cdev->gadget, usb_desc);
255 otg_desc[0] = usb_desc;
259 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
262 usb_composite_overwrite_options(cdev, &coverwrite);
264 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
271 #ifndef CONFIG_GADGET_UAC1
272 usb_put_function_instance(fi_uac2);
274 usb_put_function_instance(fi_uac1);
279 static int audio_unbind(struct usb_composite_dev *cdev)
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);
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);
298 static struct usb_composite_driver audio_driver = {
301 .strings = audio_strings,
302 .max_speed = USB_SPEED_HIGH,
304 .unbind = audio_unbind,
307 module_usb_composite_driver(audio_driver);
309 MODULE_DESCRIPTION(DRIVER_DESC);
310 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
311 MODULE_LICENSE("GPL");