1 // SPDX-License-Identifier: GPL-2.0+
3 * audio.c -- Audio gadget driver
5 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6 * Copyright (C) 2008 Analog Devices, Inc
8 * Enter bugs at http://blackfin.uclinux.org/
11 /* #define VERBOSE_DEBUG */
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/usb/composite.h>
17 #define DRIVER_DESC "Linux USB Audio Gadget"
18 #define DRIVER_VERSION "Feb 2, 2012"
20 USB_GADGET_COMPOSITE_OPTIONS();
22 #ifndef CONFIG_GADGET_UAC1
25 /* Playback(USB-IN) Default Stereo - Fl/Fr */
26 static int p_chmask
= UAC2_DEF_PCHMASK
;
27 module_param(p_chmask
, uint
, S_IRUGO
);
28 MODULE_PARM_DESC(p_chmask
, "Playback Channel Mask");
30 /* Playback Default 48 KHz */
31 static int p_srate
= UAC2_DEF_PSRATE
;
32 module_param(p_srate
, uint
, S_IRUGO
);
33 MODULE_PARM_DESC(p_srate
, "Playback Sampling Rate");
35 /* Playback Default 16bits/sample */
36 static int p_ssize
= UAC2_DEF_PSSIZE
;
37 module_param(p_ssize
, uint
, S_IRUGO
);
38 MODULE_PARM_DESC(p_ssize
, "Playback Sample Size(bytes)");
40 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
41 static int c_chmask
= UAC2_DEF_CCHMASK
;
42 module_param(c_chmask
, uint
, S_IRUGO
);
43 MODULE_PARM_DESC(c_chmask
, "Capture Channel Mask");
45 /* Capture Default 64 KHz */
46 static int c_srate
= UAC2_DEF_CSRATE
;
47 module_param(c_srate
, uint
, S_IRUGO
);
48 MODULE_PARM_DESC(c_srate
, "Capture Sampling Rate");
50 /* Capture Default 16bits/sample */
51 static int c_ssize
= UAC2_DEF_CSSIZE
;
52 module_param(c_ssize
, uint
, S_IRUGO
);
53 MODULE_PARM_DESC(c_ssize
, "Capture Sample Size(bytes)");
55 #ifndef CONFIG_GADGET_UAC1_LEGACY
58 /* Playback(USB-IN) Default Stereo - Fl/Fr */
59 static int p_chmask
= UAC1_DEF_PCHMASK
;
60 module_param(p_chmask
, uint
, S_IRUGO
);
61 MODULE_PARM_DESC(p_chmask
, "Playback Channel Mask");
63 /* Playback Default 48 KHz */
64 static int p_srate
= UAC1_DEF_PSRATE
;
65 module_param(p_srate
, uint
, S_IRUGO
);
66 MODULE_PARM_DESC(p_srate
, "Playback Sampling Rate");
68 /* Playback Default 16bits/sample */
69 static int p_ssize
= UAC1_DEF_PSSIZE
;
70 module_param(p_ssize
, uint
, S_IRUGO
);
71 MODULE_PARM_DESC(p_ssize
, "Playback Sample Size(bytes)");
73 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
74 static int c_chmask
= UAC1_DEF_CCHMASK
;
75 module_param(c_chmask
, uint
, S_IRUGO
);
76 MODULE_PARM_DESC(c_chmask
, "Capture Channel Mask");
78 /* Capture Default 48 KHz */
79 static int c_srate
= UAC1_DEF_CSRATE
;
80 module_param(c_srate
, uint
, S_IRUGO
);
81 MODULE_PARM_DESC(c_srate
, "Capture Sampling Rate");
83 /* Capture Default 16bits/sample */
84 static int c_ssize
= UAC1_DEF_CSSIZE
;
85 module_param(c_ssize
, uint
, S_IRUGO
);
86 MODULE_PARM_DESC(c_ssize
, "Capture Sample Size(bytes)");
87 #else /* CONFIG_GADGET_UAC1_LEGACY */
88 #include "u_uac1_legacy.h"
90 static char *fn_play
= FILE_PCM_PLAYBACK
;
91 module_param(fn_play
, charp
, S_IRUGO
);
92 MODULE_PARM_DESC(fn_play
, "Playback PCM device file name");
94 static char *fn_cap
= FILE_PCM_CAPTURE
;
95 module_param(fn_cap
, charp
, S_IRUGO
);
96 MODULE_PARM_DESC(fn_cap
, "Capture PCM device file name");
98 static char *fn_cntl
= FILE_CONTROL
;
99 module_param(fn_cntl
, charp
, S_IRUGO
);
100 MODULE_PARM_DESC(fn_cntl
, "Control device file name");
102 static int req_buf_size
= UAC1_OUT_EP_MAX_PACKET_SIZE
;
103 module_param(req_buf_size
, int, S_IRUGO
);
104 MODULE_PARM_DESC(req_buf_size
, "ISO OUT endpoint request buffer size");
106 static int req_count
= UAC1_REQ_COUNT
;
107 module_param(req_count
, int, S_IRUGO
);
108 MODULE_PARM_DESC(req_count
, "ISO OUT endpoint request count");
110 static int audio_buf_size
= UAC1_AUDIO_BUF_SIZE
;
111 module_param(audio_buf_size
, int, S_IRUGO
);
112 MODULE_PARM_DESC(audio_buf_size
, "Audio buffer size");
113 #endif /* CONFIG_GADGET_UAC1_LEGACY */
116 /* string IDs are assigned dynamically */
118 static struct usb_string strings_dev
[] = {
119 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
120 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
121 [USB_GADGET_SERIAL_IDX
].s
= "",
122 { } /* end of list */
125 static struct usb_gadget_strings stringtab_dev
= {
126 .language
= 0x0409, /* en-us */
127 .strings
= strings_dev
,
130 static struct usb_gadget_strings
*audio_strings
[] = {
135 #ifndef CONFIG_GADGET_UAC1
136 static struct usb_function_instance
*fi_uac2
;
137 static struct usb_function
*f_uac2
;
139 static struct usb_function_instance
*fi_uac1
;
140 static struct usb_function
*f_uac1
;
143 /*-------------------------------------------------------------------------*/
145 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
146 * Instead: allocate your own, using normal USB-IF procedures.
149 /* Thanks to Linux Foundation for donating this product ID. */
150 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
151 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
153 /*-------------------------------------------------------------------------*/
155 static struct usb_device_descriptor device_desc
= {
156 .bLength
= sizeof device_desc
,
157 .bDescriptorType
= USB_DT_DEVICE
,
159 /* .bcdUSB = DYNAMIC */
161 #ifdef CONFIG_GADGET_UAC1_LEGACY
162 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
163 .bDeviceSubClass
= 0,
164 .bDeviceProtocol
= 0,
166 .bDeviceClass
= USB_CLASS_MISC
,
167 .bDeviceSubClass
= 0x02,
168 .bDeviceProtocol
= 0x01,
170 /* .bMaxPacketSize0 = f(hardware) */
172 /* Vendor and product id defaults change according to what configs
173 * we support. (As does bNumConfigurations.) These values can
174 * also be overridden by module parameters.
176 .idVendor
= cpu_to_le16(AUDIO_VENDOR_NUM
),
177 .idProduct
= cpu_to_le16(AUDIO_PRODUCT_NUM
),
178 /* .bcdDevice = f(hardware) */
179 /* .iManufacturer = DYNAMIC */
180 /* .iProduct = DYNAMIC */
181 /* NO SERIAL NUMBER */
182 .bNumConfigurations
= 1,
185 static const struct usb_descriptor_header
*otg_desc
[2];
187 /*-------------------------------------------------------------------------*/
189 static int audio_do_config(struct usb_configuration
*c
)
193 /* FIXME alloc iConfiguration string, set it in c->strings */
195 if (gadget_is_otg(c
->cdev
->gadget
)) {
196 c
->descriptors
= otg_desc
;
197 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
200 #ifdef CONFIG_GADGET_UAC1
201 f_uac1
= usb_get_function(fi_uac1
);
202 if (IS_ERR(f_uac1
)) {
203 status
= PTR_ERR(f_uac1
);
207 status
= usb_add_function(c
, f_uac1
);
209 usb_put_function(f_uac1
);
213 f_uac2
= usb_get_function(fi_uac2
);
214 if (IS_ERR(f_uac2
)) {
215 status
= PTR_ERR(f_uac2
);
219 status
= usb_add_function(c
, f_uac2
);
221 usb_put_function(f_uac2
);
229 static struct usb_configuration audio_config_driver
= {
230 .label
= DRIVER_DESC
,
231 .bConfigurationValue
= 1,
232 /* .iConfiguration = DYNAMIC */
233 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
236 /*-------------------------------------------------------------------------*/
238 static int audio_bind(struct usb_composite_dev
*cdev
)
240 #ifndef CONFIG_GADGET_UAC1
241 struct f_uac2_opts
*uac2_opts
;
243 #ifndef CONFIG_GADGET_UAC1_LEGACY
244 struct f_uac1_opts
*uac1_opts
;
246 struct f_uac1_legacy_opts
*uac1_opts
;
251 #ifndef CONFIG_GADGET_UAC1
252 fi_uac2
= usb_get_function_instance("uac2");
254 return PTR_ERR(fi_uac2
);
256 #ifndef CONFIG_GADGET_UAC1_LEGACY
257 fi_uac1
= usb_get_function_instance("uac1");
259 fi_uac1
= usb_get_function_instance("uac1_legacy");
262 return PTR_ERR(fi_uac1
);
265 #ifndef CONFIG_GADGET_UAC1
266 uac2_opts
= container_of(fi_uac2
, struct f_uac2_opts
, func_inst
);
267 uac2_opts
->p_chmask
= p_chmask
;
268 uac2_opts
->p_srate
= p_srate
;
269 uac2_opts
->p_ssize
= p_ssize
;
270 uac2_opts
->c_chmask
= c_chmask
;
271 uac2_opts
->c_srate
= c_srate
;
272 uac2_opts
->c_ssize
= c_ssize
;
273 uac2_opts
->req_number
= UAC2_DEF_REQ_NUM
;
275 #ifndef CONFIG_GADGET_UAC1_LEGACY
276 uac1_opts
= container_of(fi_uac1
, struct f_uac1_opts
, func_inst
);
277 uac1_opts
->p_chmask
= p_chmask
;
278 uac1_opts
->p_srate
= p_srate
;
279 uac1_opts
->p_ssize
= p_ssize
;
280 uac1_opts
->c_chmask
= c_chmask
;
281 uac1_opts
->c_srate
= c_srate
;
282 uac1_opts
->c_ssize
= c_ssize
;
283 uac1_opts
->req_number
= UAC1_DEF_REQ_NUM
;
284 #else /* CONFIG_GADGET_UAC1_LEGACY */
285 uac1_opts
= container_of(fi_uac1
, struct f_uac1_legacy_opts
, func_inst
);
286 uac1_opts
->fn_play
= fn_play
;
287 uac1_opts
->fn_cap
= fn_cap
;
288 uac1_opts
->fn_cntl
= fn_cntl
;
289 uac1_opts
->req_buf_size
= req_buf_size
;
290 uac1_opts
->req_count
= req_count
;
291 uac1_opts
->audio_buf_size
= audio_buf_size
;
292 #endif /* CONFIG_GADGET_UAC1_LEGACY */
295 status
= usb_string_ids_tab(cdev
, strings_dev
);
298 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
299 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
301 if (gadget_is_otg(cdev
->gadget
) && !otg_desc
[0]) {
302 struct usb_descriptor_header
*usb_desc
;
304 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
307 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
308 otg_desc
[0] = usb_desc
;
312 status
= usb_add_config(cdev
, &audio_config_driver
, audio_do_config
);
315 usb_composite_overwrite_options(cdev
, &coverwrite
);
317 INFO(cdev
, "%s, version: %s\n", DRIVER_DESC
, DRIVER_VERSION
);
324 #ifndef CONFIG_GADGET_UAC1
325 usb_put_function_instance(fi_uac2
);
327 usb_put_function_instance(fi_uac1
);
332 static int audio_unbind(struct usb_composite_dev
*cdev
)
334 #ifdef CONFIG_GADGET_UAC1
335 if (!IS_ERR_OR_NULL(f_uac1
))
336 usb_put_function(f_uac1
);
337 if (!IS_ERR_OR_NULL(fi_uac1
))
338 usb_put_function_instance(fi_uac1
);
340 if (!IS_ERR_OR_NULL(f_uac2
))
341 usb_put_function(f_uac2
);
342 if (!IS_ERR_OR_NULL(fi_uac2
))
343 usb_put_function_instance(fi_uac2
);
351 static struct usb_composite_driver audio_driver
= {
354 .strings
= audio_strings
,
355 .max_speed
= USB_SPEED_HIGH
,
357 .unbind
= audio_unbind
,
360 module_usb_composite_driver(audio_driver
);
362 MODULE_DESCRIPTION(DRIVER_DESC
);
363 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
364 MODULE_LICENSE("GPL");