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
9 /* #define VERBOSE_DEBUG */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/usb/composite.h>
15 #define DRIVER_DESC "Linux USB Audio Gadget"
16 #define DRIVER_VERSION "Feb 2, 2012"
18 USB_GADGET_COMPOSITE_OPTIONS();
20 #ifndef CONFIG_GADGET_UAC1
23 /* Playback(USB-IN) Default Stereo - Fl/Fr */
24 static int p_chmask
= UAC2_DEF_PCHMASK
;
25 module_param(p_chmask
, uint
, S_IRUGO
);
26 MODULE_PARM_DESC(p_chmask
, "Playback Channel Mask");
28 /* Playback Default 48 KHz */
29 static int p_srate
= UAC2_DEF_PSRATE
;
30 module_param(p_srate
, uint
, S_IRUGO
);
31 MODULE_PARM_DESC(p_srate
, "Playback Sampling Rate");
33 /* Playback Default 16bits/sample */
34 static int p_ssize
= UAC2_DEF_PSSIZE
;
35 module_param(p_ssize
, uint
, S_IRUGO
);
36 MODULE_PARM_DESC(p_ssize
, "Playback Sample Size(bytes)");
38 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
39 static int c_chmask
= UAC2_DEF_CCHMASK
;
40 module_param(c_chmask
, uint
, S_IRUGO
);
41 MODULE_PARM_DESC(c_chmask
, "Capture Channel Mask");
43 /* Capture Default 64 KHz */
44 static int c_srate
= UAC2_DEF_CSRATE
;
45 module_param(c_srate
, uint
, S_IRUGO
);
46 MODULE_PARM_DESC(c_srate
, "Capture Sampling Rate");
48 /* Capture Default 16bits/sample */
49 static int c_ssize
= UAC2_DEF_CSSIZE
;
50 module_param(c_ssize
, uint
, S_IRUGO
);
51 MODULE_PARM_DESC(c_ssize
, "Capture Sample Size(bytes)");
53 #ifndef CONFIG_GADGET_UAC1_LEGACY
56 /* Playback(USB-IN) Default Stereo - Fl/Fr */
57 static int p_chmask
= UAC1_DEF_PCHMASK
;
58 module_param(p_chmask
, uint
, S_IRUGO
);
59 MODULE_PARM_DESC(p_chmask
, "Playback Channel Mask");
61 /* Playback Default 48 KHz */
62 static int p_srate
= UAC1_DEF_PSRATE
;
63 module_param(p_srate
, uint
, S_IRUGO
);
64 MODULE_PARM_DESC(p_srate
, "Playback Sampling Rate");
66 /* Playback Default 16bits/sample */
67 static int p_ssize
= UAC1_DEF_PSSIZE
;
68 module_param(p_ssize
, uint
, S_IRUGO
);
69 MODULE_PARM_DESC(p_ssize
, "Playback Sample Size(bytes)");
71 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
72 static int c_chmask
= UAC1_DEF_CCHMASK
;
73 module_param(c_chmask
, uint
, S_IRUGO
);
74 MODULE_PARM_DESC(c_chmask
, "Capture Channel Mask");
76 /* Capture Default 48 KHz */
77 static int c_srate
= UAC1_DEF_CSRATE
;
78 module_param(c_srate
, uint
, S_IRUGO
);
79 MODULE_PARM_DESC(c_srate
, "Capture Sampling Rate");
81 /* Capture Default 16bits/sample */
82 static int c_ssize
= UAC1_DEF_CSSIZE
;
83 module_param(c_ssize
, uint
, S_IRUGO
);
84 MODULE_PARM_DESC(c_ssize
, "Capture Sample Size(bytes)");
85 #else /* CONFIG_GADGET_UAC1_LEGACY */
86 #include "u_uac1_legacy.h"
88 static char *fn_play
= FILE_PCM_PLAYBACK
;
89 module_param(fn_play
, charp
, S_IRUGO
);
90 MODULE_PARM_DESC(fn_play
, "Playback PCM device file name");
92 static char *fn_cap
= FILE_PCM_CAPTURE
;
93 module_param(fn_cap
, charp
, S_IRUGO
);
94 MODULE_PARM_DESC(fn_cap
, "Capture PCM device file name");
96 static char *fn_cntl
= FILE_CONTROL
;
97 module_param(fn_cntl
, charp
, S_IRUGO
);
98 MODULE_PARM_DESC(fn_cntl
, "Control device file name");
100 static int req_buf_size
= UAC1_OUT_EP_MAX_PACKET_SIZE
;
101 module_param(req_buf_size
, int, S_IRUGO
);
102 MODULE_PARM_DESC(req_buf_size
, "ISO OUT endpoint request buffer size");
104 static int req_count
= UAC1_REQ_COUNT
;
105 module_param(req_count
, int, S_IRUGO
);
106 MODULE_PARM_DESC(req_count
, "ISO OUT endpoint request count");
108 static int audio_buf_size
= UAC1_AUDIO_BUF_SIZE
;
109 module_param(audio_buf_size
, int, S_IRUGO
);
110 MODULE_PARM_DESC(audio_buf_size
, "Audio buffer size");
111 #endif /* CONFIG_GADGET_UAC1_LEGACY */
114 /* string IDs are assigned dynamically */
116 static struct usb_string strings_dev
[] = {
117 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
118 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
119 [USB_GADGET_SERIAL_IDX
].s
= "",
120 { } /* end of list */
123 static struct usb_gadget_strings stringtab_dev
= {
124 .language
= 0x0409, /* en-us */
125 .strings
= strings_dev
,
128 static struct usb_gadget_strings
*audio_strings
[] = {
133 #ifndef CONFIG_GADGET_UAC1
134 static struct usb_function_instance
*fi_uac2
;
135 static struct usb_function
*f_uac2
;
137 static struct usb_function_instance
*fi_uac1
;
138 static struct usb_function
*f_uac1
;
141 /*-------------------------------------------------------------------------*/
143 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
144 * Instead: allocate your own, using normal USB-IF procedures.
147 /* Thanks to Linux Foundation for donating this product ID. */
148 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
149 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
151 /*-------------------------------------------------------------------------*/
153 static struct usb_device_descriptor device_desc
= {
154 .bLength
= sizeof device_desc
,
155 .bDescriptorType
= USB_DT_DEVICE
,
157 /* .bcdUSB = DYNAMIC */
159 #ifdef CONFIG_GADGET_UAC1_LEGACY
160 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
161 .bDeviceSubClass
= 0,
162 .bDeviceProtocol
= 0,
164 .bDeviceClass
= USB_CLASS_MISC
,
165 .bDeviceSubClass
= 0x02,
166 .bDeviceProtocol
= 0x01,
168 /* .bMaxPacketSize0 = f(hardware) */
170 /* Vendor and product id defaults change according to what configs
171 * we support. (As does bNumConfigurations.) These values can
172 * also be overridden by module parameters.
174 .idVendor
= cpu_to_le16(AUDIO_VENDOR_NUM
),
175 .idProduct
= cpu_to_le16(AUDIO_PRODUCT_NUM
),
176 /* .bcdDevice = f(hardware) */
177 /* .iManufacturer = DYNAMIC */
178 /* .iProduct = DYNAMIC */
179 /* NO SERIAL NUMBER */
180 .bNumConfigurations
= 1,
183 static const struct usb_descriptor_header
*otg_desc
[2];
185 /*-------------------------------------------------------------------------*/
187 static int audio_do_config(struct usb_configuration
*c
)
191 /* FIXME alloc iConfiguration string, set it in c->strings */
193 if (gadget_is_otg(c
->cdev
->gadget
)) {
194 c
->descriptors
= otg_desc
;
195 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
198 #ifdef CONFIG_GADGET_UAC1
199 f_uac1
= usb_get_function(fi_uac1
);
200 if (IS_ERR(f_uac1
)) {
201 status
= PTR_ERR(f_uac1
);
205 status
= usb_add_function(c
, f_uac1
);
207 usb_put_function(f_uac1
);
211 f_uac2
= usb_get_function(fi_uac2
);
212 if (IS_ERR(f_uac2
)) {
213 status
= PTR_ERR(f_uac2
);
217 status
= usb_add_function(c
, f_uac2
);
219 usb_put_function(f_uac2
);
227 static struct usb_configuration audio_config_driver
= {
228 .label
= DRIVER_DESC
,
229 .bConfigurationValue
= 1,
230 /* .iConfiguration = DYNAMIC */
231 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
234 /*-------------------------------------------------------------------------*/
236 static int audio_bind(struct usb_composite_dev
*cdev
)
238 #ifndef CONFIG_GADGET_UAC1
239 struct f_uac2_opts
*uac2_opts
;
241 #ifndef CONFIG_GADGET_UAC1_LEGACY
242 struct f_uac1_opts
*uac1_opts
;
244 struct f_uac1_legacy_opts
*uac1_opts
;
249 #ifndef CONFIG_GADGET_UAC1
250 fi_uac2
= usb_get_function_instance("uac2");
252 return PTR_ERR(fi_uac2
);
254 #ifndef CONFIG_GADGET_UAC1_LEGACY
255 fi_uac1
= usb_get_function_instance("uac1");
257 fi_uac1
= usb_get_function_instance("uac1_legacy");
260 return PTR_ERR(fi_uac1
);
263 #ifndef CONFIG_GADGET_UAC1
264 uac2_opts
= container_of(fi_uac2
, struct f_uac2_opts
, func_inst
);
265 uac2_opts
->p_chmask
= p_chmask
;
266 uac2_opts
->p_srate
= p_srate
;
267 uac2_opts
->p_ssize
= p_ssize
;
268 uac2_opts
->c_chmask
= c_chmask
;
269 uac2_opts
->c_srate
= c_srate
;
270 uac2_opts
->c_ssize
= c_ssize
;
271 uac2_opts
->req_number
= UAC2_DEF_REQ_NUM
;
273 #ifndef CONFIG_GADGET_UAC1_LEGACY
274 uac1_opts
= container_of(fi_uac1
, struct f_uac1_opts
, func_inst
);
275 uac1_opts
->p_chmask
= p_chmask
;
276 uac1_opts
->p_srate
= p_srate
;
277 uac1_opts
->p_ssize
= p_ssize
;
278 uac1_opts
->c_chmask
= c_chmask
;
279 uac1_opts
->c_srate
= c_srate
;
280 uac1_opts
->c_ssize
= c_ssize
;
281 uac1_opts
->req_number
= UAC1_DEF_REQ_NUM
;
282 #else /* CONFIG_GADGET_UAC1_LEGACY */
283 uac1_opts
= container_of(fi_uac1
, struct f_uac1_legacy_opts
, func_inst
);
284 uac1_opts
->fn_play
= fn_play
;
285 uac1_opts
->fn_cap
= fn_cap
;
286 uac1_opts
->fn_cntl
= fn_cntl
;
287 uac1_opts
->req_buf_size
= req_buf_size
;
288 uac1_opts
->req_count
= req_count
;
289 uac1_opts
->audio_buf_size
= audio_buf_size
;
290 #endif /* CONFIG_GADGET_UAC1_LEGACY */
293 status
= usb_string_ids_tab(cdev
, strings_dev
);
296 device_desc
.iManufacturer
= strings_dev
[USB_GADGET_MANUFACTURER_IDX
].id
;
297 device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
299 if (gadget_is_otg(cdev
->gadget
) && !otg_desc
[0]) {
300 struct usb_descriptor_header
*usb_desc
;
302 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
305 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
306 otg_desc
[0] = usb_desc
;
310 status
= usb_add_config(cdev
, &audio_config_driver
, audio_do_config
);
313 usb_composite_overwrite_options(cdev
, &coverwrite
);
315 INFO(cdev
, "%s, version: %s\n", DRIVER_DESC
, DRIVER_VERSION
);
322 #ifndef CONFIG_GADGET_UAC1
323 usb_put_function_instance(fi_uac2
);
325 usb_put_function_instance(fi_uac1
);
330 static int audio_unbind(struct usb_composite_dev
*cdev
)
332 #ifdef CONFIG_GADGET_UAC1
333 if (!IS_ERR_OR_NULL(f_uac1
))
334 usb_put_function(f_uac1
);
335 if (!IS_ERR_OR_NULL(fi_uac1
))
336 usb_put_function_instance(fi_uac1
);
338 if (!IS_ERR_OR_NULL(f_uac2
))
339 usb_put_function(f_uac2
);
340 if (!IS_ERR_OR_NULL(fi_uac2
))
341 usb_put_function_instance(fi_uac2
);
349 static struct usb_composite_driver audio_driver
= {
352 .strings
= audio_strings
,
353 .max_speed
= USB_SPEED_HIGH
,
355 .unbind
= audio_unbind
,
358 module_usb_composite_driver(audio_driver
);
360 MODULE_DESCRIPTION(DRIVER_DESC
);
361 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
362 MODULE_LICENSE("GPL");