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