1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * audio.c -- Audio gadget driver
4 *
5 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6 * Copyright (C) 2008 Analog Devices, Inc
7 */
8
9 /* #define VERBOSE_DEBUG */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/usb/composite.h>
14
15 #define DRIVER_DESC "Linux USB Audio Gadget"
16 #define DRIVER_VERSION "Feb 2, 2012"
17
18 USB_GADGET_COMPOSITE_OPTIONS();
19
20 #ifndef CONFIG_GADGET_UAC1
21 #include "u_uac2.h"
22
23 /* Playback(USB-IN) Default Stereo - Fl/Fr */
24 static int p_chmask = UAC2_DEF_PCHMASK;
25 module_param(p_chmask, uint, 0444);
26 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
27
28 /* Playback Default 48 KHz */
29 static int p_srates[UAC_MAX_RATES] = {UAC2_DEF_PSRATE};
30 static int p_srates_cnt = 1;
31 module_param_array_named(p_srate, p_srates, uint, &p_srates_cnt, 0444);
32 MODULE_PARM_DESC(p_srate, "Playback Sampling Rates (array)");
33
34 /* Playback Default 16bits/sample */
35 static int p_ssize = UAC2_DEF_PSSIZE;
36 module_param(p_ssize, uint, 0444);
37 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
38
39 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
40 static int c_chmask = UAC2_DEF_CCHMASK;
41 module_param(c_chmask, uint, 0444);
42 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
43
44 /* Capture Default 64 KHz */
45 static int c_srates[UAC_MAX_RATES] = {UAC2_DEF_CSRATE};
46 static int c_srates_cnt = 1;
47 module_param_array_named(c_srate, c_srates, uint, &c_srates_cnt, 0444);
48 MODULE_PARM_DESC(c_srate, "Capture Sampling Rates (array)");
49
50 /* Capture Default 16bits/sample */
51 static int c_ssize = UAC2_DEF_CSSIZE;
52 module_param(c_ssize, uint, 0444);
53 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
54 #else
55 #ifndef CONFIG_GADGET_UAC1_LEGACY
56 #include "u_uac1.h"
57
58 /* Playback(USB-IN) Default Stereo - Fl/Fr */
59 static int p_chmask = UAC1_DEF_PCHMASK;
60 module_param(p_chmask, uint, 0444);
61 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
62
63 /* Playback Default 48 KHz */
64 static int p_srates[UAC_MAX_RATES] = {UAC1_DEF_PSRATE};
65 static int p_srates_cnt = 1;
66 module_param_array_named(p_srate, p_srates, uint, &p_srates_cnt, 0444);
67 MODULE_PARM_DESC(p_srate, "Playback Sampling Rates (array)");
68
69 /* Playback Default 16bits/sample */
70 static int p_ssize = UAC1_DEF_PSSIZE;
71 module_param(p_ssize, uint, 0444);
72 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
73
74 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
75 static int c_chmask = UAC1_DEF_CCHMASK;
76 module_param(c_chmask, uint, 0444);
77 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
78
79 /* Capture Default 48 KHz */
80 static int c_srates[UAC_MAX_RATES] = {UAC1_DEF_CSRATE};
81 static int c_srates_cnt = 1;
82 module_param_array_named(c_srate, c_srates, uint, &c_srates_cnt, 0444);
83 MODULE_PARM_DESC(c_srate, "Capture Sampling Rates (array)");
84
85 /* Capture Default 16bits/sample */
86 static int c_ssize = UAC1_DEF_CSSIZE;
87 module_param(c_ssize, uint, 0444);
88 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
89 #else /* CONFIG_GADGET_UAC1_LEGACY */
90 #include "u_uac1_legacy.h"
91
92 static char *fn_play = FILE_PCM_PLAYBACK;
93 module_param(fn_play, charp, 0444);
94 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
95
96 static char *fn_cap = FILE_PCM_CAPTURE;
97 module_param(fn_cap, charp, 0444);
98 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
99
100 static char *fn_cntl = FILE_CONTROL;
101 module_param(fn_cntl, charp, 0444);
102 MODULE_PARM_DESC(fn_cntl, "Control device file name");
103
104 static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
105 module_param(req_buf_size, int, 0444);
106 MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
107
108 static int req_count = UAC1_REQ_COUNT;
109 module_param(req_count, int, 0444);
110 MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
111
112 static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
113 module_param(audio_buf_size, int, 0444);
114 MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
115 #endif /* CONFIG_GADGET_UAC1_LEGACY */
116 #endif
117
118 /* string IDs are assigned dynamically */
119
120 static struct usb_string strings_dev[] = {
121 [USB_GADGET_MANUFACTURER_IDX].s = "",
122 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
123 [USB_GADGET_SERIAL_IDX].s = "",
124 { } /* end of list */
125 };
126
127 static struct usb_gadget_strings stringtab_dev = {
128 .language = 0x0409, /* en-us */
129 .strings = strings_dev,
130 };
131
132 static struct usb_gadget_strings *audio_strings[] = {
133 &stringtab_dev,
134 NULL,
135 };
136
137 #ifndef CONFIG_GADGET_UAC1
138 static struct usb_function_instance *fi_uac2;
139 static struct usb_function *f_uac2;
140 #else
141 static struct usb_function_instance *fi_uac1;
142 static struct usb_function *f_uac1;
143 #endif
144
145 /*-------------------------------------------------------------------------*/
146
147 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
148 * Instead: allocate your own, using normal USB-IF procedures.
149 */
150
151 /* Thanks to Linux Foundation for donating this product ID. */
152 #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
153 #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
154
155 /*-------------------------------------------------------------------------*/
156
157 static struct usb_device_descriptor device_desc = {
158 .bLength = sizeof device_desc,
159 .bDescriptorType = USB_DT_DEVICE,
160
161 /* .bcdUSB = DYNAMIC */
162
163 #ifdef CONFIG_GADGET_UAC1_LEGACY
164 .bDeviceClass = USB_CLASS_PER_INTERFACE,
165 .bDeviceSubClass = 0,
166 .bDeviceProtocol = 0,
167 #else
168 .bDeviceClass = USB_CLASS_MISC,
169 .bDeviceSubClass = 0x02,
170 .bDeviceProtocol = 0x01,
171 #endif
172 /* .bMaxPacketSize0 = f(hardware) */
173
174 /* Vendor and product id defaults change according to what configs
175 * we support. (As does bNumConfigurations.) These values can
176 * also be overridden by module parameters.
177 */
178 .idVendor = cpu_to_le16(AUDIO_VENDOR_NUM),
179 .idProduct = cpu_to_le16(AUDIO_PRODUCT_NUM),
180 /* .bcdDevice = f(hardware) */
181 /* .iManufacturer = DYNAMIC */
182 /* .iProduct = DYNAMIC */
183 /* NO SERIAL NUMBER */
184 .bNumConfigurations = 1,
185 };
186
187 static const struct usb_descriptor_header *otg_desc[2];
188
189 /*-------------------------------------------------------------------------*/
190
audio_do_config(struct usb_configuration * c)191 static int audio_do_config(struct usb_configuration *c)
192 {
193 int status;
194
195 /* FIXME alloc iConfiguration string, set it in c->strings */
196
197 if (gadget_is_otg(c->cdev->gadget)) {
198 c->descriptors = otg_desc;
199 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
200 }
201
202 #ifdef CONFIG_GADGET_UAC1
203 f_uac1 = usb_get_function(fi_uac1);
204 if (IS_ERR(f_uac1)) {
205 status = PTR_ERR(f_uac1);
206 return status;
207 }
208
209 status = usb_add_function(c, f_uac1);
210 if (status < 0) {
211 usb_put_function(f_uac1);
212 return status;
213 }
214 #else
215 f_uac2 = usb_get_function(fi_uac2);
216 if (IS_ERR(f_uac2)) {
217 status = PTR_ERR(f_uac2);
218 return status;
219 }
220
221 status = usb_add_function(c, f_uac2);
222 if (status < 0) {
223 usb_put_function(f_uac2);
224 return status;
225 }
226 #endif
227
228 return 0;
229 }
230
231 static struct usb_configuration audio_config_driver = {
232 .label = DRIVER_DESC,
233 .bConfigurationValue = 1,
234 /* .iConfiguration = DYNAMIC */
235 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
236 };
237
238 /*-------------------------------------------------------------------------*/
239
audio_bind(struct usb_composite_dev * cdev)240 static int audio_bind(struct usb_composite_dev *cdev)
241 {
242 #ifndef CONFIG_GADGET_UAC1
243 struct f_uac2_opts *uac2_opts;
244 int i;
245 #else
246 #ifndef CONFIG_GADGET_UAC1_LEGACY
247 struct f_uac1_opts *uac1_opts;
248 int i;
249 #else
250 struct f_uac1_legacy_opts *uac1_opts;
251 #endif
252 #endif
253 int status;
254
255 #ifndef CONFIG_GADGET_UAC1
256 fi_uac2 = usb_get_function_instance("uac2");
257 if (IS_ERR(fi_uac2))
258 return PTR_ERR(fi_uac2);
259 #else
260 #ifndef CONFIG_GADGET_UAC1_LEGACY
261 fi_uac1 = usb_get_function_instance("uac1");
262 #else
263 fi_uac1 = usb_get_function_instance("uac1_legacy");
264 #endif
265 if (IS_ERR(fi_uac1))
266 return PTR_ERR(fi_uac1);
267 #endif
268
269 #ifndef CONFIG_GADGET_UAC1
270 uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
271 uac2_opts->p_chmask = p_chmask;
272
273 for (i = 0; i < p_srates_cnt; ++i)
274 uac2_opts->p_srates[i] = p_srates[i];
275
276 uac2_opts->p_ssize = p_ssize;
277 uac2_opts->c_chmask = c_chmask;
278
279 for (i = 0; i < c_srates_cnt; ++i)
280 uac2_opts->c_srates[i] = c_srates[i];
281
282 uac2_opts->c_ssize = c_ssize;
283 uac2_opts->req_number = UAC2_DEF_REQ_NUM;
284 #else
285 #ifndef CONFIG_GADGET_UAC1_LEGACY
286 uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
287 uac1_opts->p_chmask = p_chmask;
288
289 for (i = 0; i < p_srates_cnt; ++i)
290 uac1_opts->p_srates[i] = p_srates[i];
291
292 uac1_opts->p_ssize = p_ssize;
293 uac1_opts->c_chmask = c_chmask;
294
295 for (i = 0; i < c_srates_cnt; ++i)
296 uac1_opts->c_srates[i] = c_srates[i];
297
298 uac1_opts->c_ssize = c_ssize;
299 uac1_opts->req_number = UAC1_DEF_REQ_NUM;
300 #else /* CONFIG_GADGET_UAC1_LEGACY */
301 uac1_opts = container_of(fi_uac1, struct f_uac1_legacy_opts, func_inst);
302 uac1_opts->fn_play = fn_play;
303 uac1_opts->fn_cap = fn_cap;
304 uac1_opts->fn_cntl = fn_cntl;
305 uac1_opts->req_buf_size = req_buf_size;
306 uac1_opts->req_count = req_count;
307 uac1_opts->audio_buf_size = audio_buf_size;
308 #endif /* CONFIG_GADGET_UAC1_LEGACY */
309 #endif
310
311 status = usb_string_ids_tab(cdev, strings_dev);
312 if (status < 0)
313 goto fail;
314 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
315 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
316
317 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
318 struct usb_descriptor_header *usb_desc;
319
320 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
321 if (!usb_desc) {
322 status = -ENOMEM;
323 goto fail;
324 }
325 usb_otg_descriptor_init(cdev->gadget, usb_desc);
326 otg_desc[0] = usb_desc;
327 otg_desc[1] = NULL;
328 }
329
330 status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
331 if (status < 0)
332 goto fail_otg_desc;
333 usb_composite_overwrite_options(cdev, &coverwrite);
334
335 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
336 return 0;
337
338 fail_otg_desc:
339 kfree(otg_desc[0]);
340 otg_desc[0] = NULL;
341 fail:
342 #ifndef CONFIG_GADGET_UAC1
343 usb_put_function_instance(fi_uac2);
344 #else
345 usb_put_function_instance(fi_uac1);
346 #endif
347 return status;
348 }
349
audio_unbind(struct usb_composite_dev * cdev)350 static int audio_unbind(struct usb_composite_dev *cdev)
351 {
352 #ifdef CONFIG_GADGET_UAC1
353 if (!IS_ERR_OR_NULL(f_uac1))
354 usb_put_function(f_uac1);
355 if (!IS_ERR_OR_NULL(fi_uac1))
356 usb_put_function_instance(fi_uac1);
357 #else
358 if (!IS_ERR_OR_NULL(f_uac2))
359 usb_put_function(f_uac2);
360 if (!IS_ERR_OR_NULL(fi_uac2))
361 usb_put_function_instance(fi_uac2);
362 #endif
363 kfree(otg_desc[0]);
364 otg_desc[0] = NULL;
365
366 return 0;
367 }
368
369 static struct usb_composite_driver audio_driver = {
370 .name = "g_audio",
371 .dev = &device_desc,
372 .strings = audio_strings,
373 .max_speed = USB_SPEED_HIGH,
374 .bind = audio_bind,
375 .unbind = audio_unbind,
376 };
377
378 module_usb_composite_driver(audio_driver);
379
380 MODULE_DESCRIPTION(DRIVER_DESC);
381 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
382 MODULE_LICENSE("GPL");
383
384