xref: /linux/drivers/usb/gadget/legacy/audio.c (revision 160b8e75932fd51a49607d32dbfa1d417977b79c)
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  * Enter bugs at http://blackfin.uclinux.org/
9  */
10 
11 /* #define VERBOSE_DEBUG */
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/usb/composite.h>
16 
17 #define DRIVER_DESC		"Linux USB Audio Gadget"
18 #define DRIVER_VERSION		"Feb 2, 2012"
19 
20 USB_GADGET_COMPOSITE_OPTIONS();
21 
22 #ifndef CONFIG_GADGET_UAC1
23 #include "u_uac2.h"
24 
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");
29 
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");
34 
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)");
39 
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");
44 
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");
49 
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)");
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, S_IRUGO);
61 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
62 
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");
67 
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)");
72 
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");
77 
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");
82 
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"
89 
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");
93 
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");
97 
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");
101 
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");
105 
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");
109 
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 */
114 #endif
115 
116 /* string IDs are assigned dynamically */
117 
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 */
123 };
124 
125 static struct usb_gadget_strings stringtab_dev = {
126 	.language = 0x0409,	/* en-us */
127 	.strings = strings_dev,
128 };
129 
130 static struct usb_gadget_strings *audio_strings[] = {
131 	&stringtab_dev,
132 	NULL,
133 };
134 
135 #ifndef CONFIG_GADGET_UAC1
136 static struct usb_function_instance *fi_uac2;
137 static struct usb_function *f_uac2;
138 #else
139 static struct usb_function_instance *fi_uac1;
140 static struct usb_function *f_uac1;
141 #endif
142 
143 /*-------------------------------------------------------------------------*/
144 
145 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
146  * Instead:  allocate your own, using normal USB-IF procedures.
147  */
148 
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 */
152 
153 /*-------------------------------------------------------------------------*/
154 
155 static struct usb_device_descriptor device_desc = {
156 	.bLength =		sizeof device_desc,
157 	.bDescriptorType =	USB_DT_DEVICE,
158 
159 	/* .bcdUSB = DYNAMIC */
160 
161 #ifdef CONFIG_GADGET_UAC1_LEGACY
162 	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
163 	.bDeviceSubClass =	0,
164 	.bDeviceProtocol =	0,
165 #else
166 	.bDeviceClass =		USB_CLASS_MISC,
167 	.bDeviceSubClass =	0x02,
168 	.bDeviceProtocol =	0x01,
169 #endif
170 	/* .bMaxPacketSize0 = f(hardware) */
171 
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.
175 	 */
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,
183 };
184 
185 static const struct usb_descriptor_header *otg_desc[2];
186 
187 /*-------------------------------------------------------------------------*/
188 
189 static int audio_do_config(struct usb_configuration *c)
190 {
191 	int status;
192 
193 	/* FIXME alloc iConfiguration string, set it in c->strings */
194 
195 	if (gadget_is_otg(c->cdev->gadget)) {
196 		c->descriptors = otg_desc;
197 		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
198 	}
199 
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);
204 		return status;
205 	}
206 
207 	status = usb_add_function(c, f_uac1);
208 	if (status < 0) {
209 		usb_put_function(f_uac1);
210 		return status;
211 	}
212 #else
213 	f_uac2 = usb_get_function(fi_uac2);
214 	if (IS_ERR(f_uac2)) {
215 		status = PTR_ERR(f_uac2);
216 		return status;
217 	}
218 
219 	status = usb_add_function(c, f_uac2);
220 	if (status < 0) {
221 		usb_put_function(f_uac2);
222 		return status;
223 	}
224 #endif
225 
226 	return 0;
227 }
228 
229 static struct usb_configuration audio_config_driver = {
230 	.label			= DRIVER_DESC,
231 	.bConfigurationValue	= 1,
232 	/* .iConfiguration = DYNAMIC */
233 	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
234 };
235 
236 /*-------------------------------------------------------------------------*/
237 
238 static int audio_bind(struct usb_composite_dev *cdev)
239 {
240 #ifndef CONFIG_GADGET_UAC1
241 	struct f_uac2_opts	*uac2_opts;
242 #else
243 #ifndef CONFIG_GADGET_UAC1_LEGACY
244 	struct f_uac1_opts	*uac1_opts;
245 #else
246 	struct f_uac1_legacy_opts	*uac1_opts;
247 #endif
248 #endif
249 	int			status;
250 
251 #ifndef CONFIG_GADGET_UAC1
252 	fi_uac2 = usb_get_function_instance("uac2");
253 	if (IS_ERR(fi_uac2))
254 		return PTR_ERR(fi_uac2);
255 #else
256 #ifndef CONFIG_GADGET_UAC1_LEGACY
257 	fi_uac1 = usb_get_function_instance("uac1");
258 #else
259 	fi_uac1 = usb_get_function_instance("uac1_legacy");
260 #endif
261 	if (IS_ERR(fi_uac1))
262 		return PTR_ERR(fi_uac1);
263 #endif
264 
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;
274 #else
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 */
293 #endif
294 
295 	status = usb_string_ids_tab(cdev, strings_dev);
296 	if (status < 0)
297 		goto fail;
298 	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
299 	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
300 
301 	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
302 		struct usb_descriptor_header *usb_desc;
303 
304 		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
305 		if (!usb_desc)
306 			goto fail;
307 		usb_otg_descriptor_init(cdev->gadget, usb_desc);
308 		otg_desc[0] = usb_desc;
309 		otg_desc[1] = NULL;
310 	}
311 
312 	status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
313 	if (status < 0)
314 		goto fail_otg_desc;
315 	usb_composite_overwrite_options(cdev, &coverwrite);
316 
317 	INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
318 	return 0;
319 
320 fail_otg_desc:
321 	kfree(otg_desc[0]);
322 	otg_desc[0] = NULL;
323 fail:
324 #ifndef CONFIG_GADGET_UAC1
325 	usb_put_function_instance(fi_uac2);
326 #else
327 	usb_put_function_instance(fi_uac1);
328 #endif
329 	return status;
330 }
331 
332 static int audio_unbind(struct usb_composite_dev *cdev)
333 {
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);
339 #else
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);
344 #endif
345 	kfree(otg_desc[0]);
346 	otg_desc[0] = NULL;
347 
348 	return 0;
349 }
350 
351 static struct usb_composite_driver audio_driver = {
352 	.name		= "g_audio",
353 	.dev		= &device_desc,
354 	.strings	= audio_strings,
355 	.max_speed	= USB_SPEED_HIGH,
356 	.bind		= audio_bind,
357 	.unbind		= audio_unbind,
358 };
359 
360 module_usb_composite_driver(audio_driver);
361 
362 MODULE_DESCRIPTION(DRIVER_DESC);
363 MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
364 MODULE_LICENSE("GPL");
365 
366