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 #include "gadget_chips.h"
19 #define DRIVER_DESC "Linux USB Audio Gadget"
20 #define DRIVER_VERSION "Feb 2, 2012"
22 USB_GADGET_COMPOSITE_OPTIONS();
24 #ifndef CONFIG_GADGET_UAC1
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");
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");
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)");
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");
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");
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)");
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");
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");
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");
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");
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");
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");
84 /* string IDs are assigned dynamically */
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
= "",
93 static struct usb_gadget_strings stringtab_dev
= {
94 .language
= 0x0409, /* en-us */
95 .strings
= strings_dev
,
98 static struct usb_gadget_strings
*audio_strings
[] = {
103 #ifndef CONFIG_GADGET_UAC1
104 static struct usb_function_instance
*fi_uac2
;
105 static struct usb_function
*f_uac2
;
107 static struct usb_function_instance
*fi_uac1
;
108 static struct usb_function
*f_uac1
;
111 /*-------------------------------------------------------------------------*/
113 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
114 * Instead: allocate your own, using normal USB-IF procedures.
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 */
121 /*-------------------------------------------------------------------------*/
123 static struct usb_device_descriptor device_desc
= {
124 .bLength
= sizeof device_desc
,
125 .bDescriptorType
= USB_DT_DEVICE
,
127 .bcdUSB
= __constant_cpu_to_le16(0x200),
129 #ifdef CONFIG_GADGET_UAC1
130 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
131 .bDeviceSubClass
= 0,
132 .bDeviceProtocol
= 0,
134 .bDeviceClass
= USB_CLASS_MISC
,
135 .bDeviceSubClass
= 0x02,
136 .bDeviceProtocol
= 0x01,
138 /* .bMaxPacketSize0 = f(hardware) */
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.
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,
153 static struct usb_otg_descriptor otg_descriptor
= {
154 .bLength
= sizeof otg_descriptor
,
155 .bDescriptorType
= USB_DT_OTG
,
157 /* REVISIT SRP-only hardware is possible, although
158 * it would not be called "OTG" ...
160 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
163 static const struct usb_descriptor_header
*otg_desc
[] = {
164 (struct usb_descriptor_header
*) &otg_descriptor
,
168 /*-------------------------------------------------------------------------*/
170 static int audio_do_config(struct usb_configuration
*c
)
174 /* FIXME alloc iConfiguration string, set it in c->strings */
176 if (gadget_is_otg(c
->cdev
->gadget
)) {
177 c
->descriptors
= otg_desc
;
178 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
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
);
188 status
= usb_add_function(c
, f_uac1
);
190 usb_put_function(f_uac1
);
194 f_uac2
= usb_get_function(fi_uac2
);
195 if (IS_ERR(f_uac2
)) {
196 status
= PTR_ERR(f_uac2
);
200 status
= usb_add_function(c
, f_uac2
);
202 usb_put_function(f_uac2
);
210 static struct usb_configuration audio_config_driver
= {
211 .label
= DRIVER_DESC
,
212 .bConfigurationValue
= 1,
213 /* .iConfiguration = DYNAMIC */
214 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
217 /*-------------------------------------------------------------------------*/
219 static int audio_bind(struct usb_composite_dev
*cdev
)
221 #ifndef CONFIG_GADGET_UAC1
222 struct f_uac2_opts
*uac2_opts
;
224 struct f_uac1_opts
*uac1_opts
;
228 #ifndef CONFIG_GADGET_UAC1
229 fi_uac2
= usb_get_function_instance("uac2");
231 return PTR_ERR(fi_uac2
);
233 fi_uac1
= usb_get_function_instance("uac1");
235 return PTR_ERR(fi_uac1
);
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
;
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
;
256 status
= usb_string_ids_tab(cdev
, strings_dev
);
259 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
260 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
262 status
= usb_add_config(cdev
, &audio_config_driver
, audio_do_config
);
265 usb_composite_overwrite_options(cdev
, &coverwrite
);
267 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
);
295 static struct usb_composite_driver audio_driver
= {
298 .strings
= audio_strings
,
299 .max_speed
= USB_SPEED_HIGH
,
301 .unbind
= audio_unbind
,
304 module_usb_composite_driver(audio_driver
);
306 MODULE_DESCRIPTION(DRIVER_DESC
);
307 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
308 MODULE_LICENSE("GPL");