• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API)
3  *
4  * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com>
5  *
6  * This driver doesn't expect any real Audio codec to be present
7  * on the device - the audio streams are simply sinked to and
8  * sourced from a virtual ALSA sound card created.
9  *
10  * This file is based on f_uac1.c which is
11  *   Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
12  *   Copyright (C) 2008 Analog Devices, Inc
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  */
19 
20 #include <linux/usb/audio.h>
21 #include <linux/module.h>
22 
23 #include "u_audio.h"
24 #include "u_uac1.h"
25 
26 struct f_uac1 {
27 	struct g_audio g_audio;
28 	u8 ac_intf, as_in_intf, as_out_intf;
29 	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
30 };
31 
func_to_uac1(struct usb_function * f)32 static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
33 {
34 	return container_of(f, struct f_uac1, g_audio.func);
35 }
36 
37 /*
38  * DESCRIPTORS ... most are static, but strings and full
39  * configuration descriptors are built on demand.
40  */
41 
42 /*
43  * We have three interfaces - one AudioControl and two AudioStreaming
44  *
45  * The driver implements a simple UAC_1 topology.
46  * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture
47  * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN
48  */
49 #define F_AUDIO_AC_INTERFACE		0
50 #define F_AUDIO_AS_OUT_INTERFACE	1
51 #define F_AUDIO_AS_IN_INTERFACE		2
52 /* Number of streaming interfaces */
53 #define F_AUDIO_NUM_INTERFACES		2
54 
55 /* B.3.1  Standard AC Interface Descriptor */
56 static struct usb_interface_descriptor ac_interface_desc = {
57 	.bLength =		USB_DT_INTERFACE_SIZE,
58 	.bDescriptorType =	USB_DT_INTERFACE,
59 	.bNumEndpoints =	0,
60 	.bInterfaceClass =	USB_CLASS_AUDIO,
61 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL,
62 };
63 
64 /*
65  * The number of AudioStreaming and MIDIStreaming interfaces
66  * in the Audio Interface Collection
67  */
68 DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
69 
70 #define UAC_DT_AC_HEADER_LENGTH	UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
71 /* 2 input terminals and 2 output terminals */
72 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
73 	+ 2*UAC_DT_INPUT_TERMINAL_SIZE + 2*UAC_DT_OUTPUT_TERMINAL_SIZE)
74 /* B.3.2  Class-Specific AC Interface Descriptor */
75 static struct uac1_ac_header_descriptor_2 ac_header_desc = {
76 	.bLength =		UAC_DT_AC_HEADER_LENGTH,
77 	.bDescriptorType =	USB_DT_CS_INTERFACE,
78 	.bDescriptorSubtype =	UAC_HEADER,
79 	.bcdADC =		cpu_to_le16(0x0100),
80 	.wTotalLength =		cpu_to_le16(UAC_DT_TOTAL_LENGTH),
81 	.bInCollection =	F_AUDIO_NUM_INTERFACES,
82 	.baInterfaceNr = {
83 	/* Interface number of the AudioStream interfaces */
84 		[0] =		1,
85 		[1] =		2,
86 	}
87 };
88 
89 #define USB_OUT_IT_ID	1
90 static struct uac_input_terminal_descriptor usb_out_it_desc = {
91 	.bLength =		UAC_DT_INPUT_TERMINAL_SIZE,
92 	.bDescriptorType =	USB_DT_CS_INTERFACE,
93 	.bDescriptorSubtype =	UAC_INPUT_TERMINAL,
94 	.bTerminalID =		USB_OUT_IT_ID,
95 	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
96 	.bAssocTerminal =	0,
97 	.wChannelConfig =	cpu_to_le16(0x3),
98 };
99 
100 #define IO_OUT_OT_ID	2
101 static struct uac1_output_terminal_descriptor io_out_ot_desc = {
102 	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE,
103 	.bDescriptorType	= USB_DT_CS_INTERFACE,
104 	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL,
105 	.bTerminalID		= IO_OUT_OT_ID,
106 	.wTerminalType		= cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
107 	.bAssocTerminal		= 0,
108 	.bSourceID		= USB_OUT_IT_ID,
109 };
110 
111 #define IO_IN_IT_ID	3
112 static struct uac_input_terminal_descriptor io_in_it_desc = {
113 	.bLength		= UAC_DT_INPUT_TERMINAL_SIZE,
114 	.bDescriptorType	= USB_DT_CS_INTERFACE,
115 	.bDescriptorSubtype	= UAC_INPUT_TERMINAL,
116 	.bTerminalID		= IO_IN_IT_ID,
117 	.wTerminalType		= cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
118 	.bAssocTerminal		= 0,
119 	.wChannelConfig		= cpu_to_le16(0x3),
120 };
121 
122 #define USB_IN_OT_ID	4
123 static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
124 	.bLength =		UAC_DT_OUTPUT_TERMINAL_SIZE,
125 	.bDescriptorType =	USB_DT_CS_INTERFACE,
126 	.bDescriptorSubtype =	UAC_OUTPUT_TERMINAL,
127 	.bTerminalID =		USB_IN_OT_ID,
128 	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
129 	.bAssocTerminal =	0,
130 	.bSourceID =		IO_IN_IT_ID,
131 };
132 
133 /* B.4.1  Standard AS Interface Descriptor */
134 static struct usb_interface_descriptor as_out_interface_alt_0_desc = {
135 	.bLength =		USB_DT_INTERFACE_SIZE,
136 	.bDescriptorType =	USB_DT_INTERFACE,
137 	.bAlternateSetting =	0,
138 	.bNumEndpoints =	0,
139 	.bInterfaceClass =	USB_CLASS_AUDIO,
140 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
141 };
142 
143 static struct usb_interface_descriptor as_out_interface_alt_1_desc = {
144 	.bLength =		USB_DT_INTERFACE_SIZE,
145 	.bDescriptorType =	USB_DT_INTERFACE,
146 	.bAlternateSetting =	1,
147 	.bNumEndpoints =	1,
148 	.bInterfaceClass =	USB_CLASS_AUDIO,
149 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
150 };
151 
152 static struct usb_interface_descriptor as_in_interface_alt_0_desc = {
153 	.bLength =		USB_DT_INTERFACE_SIZE,
154 	.bDescriptorType =	USB_DT_INTERFACE,
155 	.bAlternateSetting =	0,
156 	.bNumEndpoints =	0,
157 	.bInterfaceClass =	USB_CLASS_AUDIO,
158 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
159 };
160 
161 static struct usb_interface_descriptor as_in_interface_alt_1_desc = {
162 	.bLength =		USB_DT_INTERFACE_SIZE,
163 	.bDescriptorType =	USB_DT_INTERFACE,
164 	.bAlternateSetting =	1,
165 	.bNumEndpoints =	1,
166 	.bInterfaceClass =	USB_CLASS_AUDIO,
167 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
168 };
169 
170 /* B.4.2  Class-Specific AS Interface Descriptor */
171 static struct uac1_as_header_descriptor as_out_header_desc = {
172 	.bLength =		UAC_DT_AS_HEADER_SIZE,
173 	.bDescriptorType =	USB_DT_CS_INTERFACE,
174 	.bDescriptorSubtype =	UAC_AS_GENERAL,
175 	.bTerminalLink =	USB_OUT_IT_ID,
176 	.bDelay =		1,
177 	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
178 };
179 
180 static struct uac1_as_header_descriptor as_in_header_desc = {
181 	.bLength =		UAC_DT_AS_HEADER_SIZE,
182 	.bDescriptorType =	USB_DT_CS_INTERFACE,
183 	.bDescriptorSubtype =	UAC_AS_GENERAL,
184 	.bTerminalLink =	USB_IN_OT_ID,
185 	.bDelay =		1,
186 	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
187 };
188 
189 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
190 
191 static struct uac_format_type_i_discrete_descriptor_1 as_out_type_i_desc = {
192 	.bLength =		UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
193 	.bDescriptorType =	USB_DT_CS_INTERFACE,
194 	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
195 	.bFormatType =		UAC_FORMAT_TYPE_I,
196 	.bSubframeSize =	2,
197 	.bBitResolution =	16,
198 	.bSamFreqType =		1,
199 };
200 
201 /* Standard ISO OUT Endpoint Descriptor */
202 static struct usb_endpoint_descriptor as_out_ep_desc  = {
203 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
204 	.bDescriptorType =	USB_DT_ENDPOINT,
205 	.bEndpointAddress =	USB_DIR_OUT,
206 	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE
207 				| USB_ENDPOINT_XFER_ISOC,
208 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
209 	.bInterval =		4,
210 };
211 
212 /* Class-specific AS ISO OUT Endpoint Descriptor */
213 static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
214 	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
215 	.bDescriptorType =	USB_DT_CS_ENDPOINT,
216 	.bDescriptorSubtype =	UAC_EP_GENERAL,
217 	.bmAttributes =		1,
218 	.bLockDelayUnits =	1,
219 	.wLockDelay =		cpu_to_le16(1),
220 };
221 
222 static struct uac_format_type_i_discrete_descriptor_1 as_in_type_i_desc = {
223 	.bLength =		UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
224 	.bDescriptorType =	USB_DT_CS_INTERFACE,
225 	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
226 	.bFormatType =		UAC_FORMAT_TYPE_I,
227 	.bSubframeSize =	2,
228 	.bBitResolution =	16,
229 	.bSamFreqType =		1,
230 };
231 
232 /* Standard ISO OUT Endpoint Descriptor */
233 static struct usb_endpoint_descriptor as_in_ep_desc  = {
234 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
235 	.bDescriptorType =	USB_DT_ENDPOINT,
236 	.bEndpointAddress =	USB_DIR_IN,
237 	.bmAttributes =		USB_ENDPOINT_SYNC_ASYNC
238 				| USB_ENDPOINT_XFER_ISOC,
239 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
240 	.bInterval =		4,
241 };
242 
243 /* Class-specific AS ISO OUT Endpoint Descriptor */
244 static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
245 	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
246 	.bDescriptorType =	USB_DT_CS_ENDPOINT,
247 	.bDescriptorSubtype =	UAC_EP_GENERAL,
248 	.bmAttributes =		1,
249 	.bLockDelayUnits =	0,
250 	.wLockDelay =		0,
251 };
252 
253 static struct usb_descriptor_header *f_audio_desc[] = {
254 	(struct usb_descriptor_header *)&ac_interface_desc,
255 	(struct usb_descriptor_header *)&ac_header_desc,
256 
257 	(struct usb_descriptor_header *)&usb_out_it_desc,
258 	(struct usb_descriptor_header *)&io_out_ot_desc,
259 	(struct usb_descriptor_header *)&io_in_it_desc,
260 	(struct usb_descriptor_header *)&usb_in_ot_desc,
261 
262 	(struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
263 	(struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
264 	(struct usb_descriptor_header *)&as_out_header_desc,
265 
266 	(struct usb_descriptor_header *)&as_out_type_i_desc,
267 
268 	(struct usb_descriptor_header *)&as_out_ep_desc,
269 	(struct usb_descriptor_header *)&as_iso_out_desc,
270 
271 	(struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
272 	(struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
273 	(struct usb_descriptor_header *)&as_in_header_desc,
274 
275 	(struct usb_descriptor_header *)&as_in_type_i_desc,
276 
277 	(struct usb_descriptor_header *)&as_in_ep_desc,
278 	(struct usb_descriptor_header *)&as_iso_in_desc,
279 	NULL,
280 };
281 
282 enum {
283 	STR_AC_IF,
284 	STR_USB_OUT_IT,
285 	STR_USB_OUT_IT_CH_NAMES,
286 	STR_IO_OUT_OT,
287 	STR_IO_IN_IT,
288 	STR_IO_IN_IT_CH_NAMES,
289 	STR_USB_IN_OT,
290 	STR_AS_OUT_IF_ALT0,
291 	STR_AS_OUT_IF_ALT1,
292 	STR_AS_IN_IF_ALT0,
293 	STR_AS_IN_IF_ALT1,
294 };
295 
296 static struct usb_string strings_uac1[] = {
297 	[STR_AC_IF].s = "AC Interface",
298 	[STR_USB_OUT_IT].s = "Playback Input terminal",
299 	[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
300 	[STR_IO_OUT_OT].s = "Playback Output terminal",
301 	[STR_IO_IN_IT].s = "Capture Input terminal",
302 	[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
303 	[STR_USB_IN_OT].s = "Capture Output terminal",
304 	[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
305 	[STR_AS_OUT_IF_ALT1].s = "Playback Active",
306 	[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
307 	[STR_AS_IN_IF_ALT1].s = "Capture Active",
308 	{ },
309 };
310 
311 static struct usb_gadget_strings str_uac1 = {
312 	.language = 0x0409,	/* en-us */
313 	.strings = strings_uac1,
314 };
315 
316 static struct usb_gadget_strings *uac1_strings[] = {
317 	&str_uac1,
318 	NULL,
319 };
320 
321 /*
322  * This function is an ALSA sound card following USB Audio Class Spec 1.0.
323  */
324 
audio_set_endpoint_req(struct usb_function * f,const struct usb_ctrlrequest * ctrl)325 static int audio_set_endpoint_req(struct usb_function *f,
326 		const struct usb_ctrlrequest *ctrl)
327 {
328 	struct usb_composite_dev *cdev = f->config->cdev;
329 	int			value = -EOPNOTSUPP;
330 	u16			ep = le16_to_cpu(ctrl->wIndex);
331 	u16			len = le16_to_cpu(ctrl->wLength);
332 	u16			w_value = le16_to_cpu(ctrl->wValue);
333 
334 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
335 			ctrl->bRequest, w_value, len, ep);
336 
337 	switch (ctrl->bRequest) {
338 	case UAC_SET_CUR:
339 		value = len;
340 		break;
341 
342 	case UAC_SET_MIN:
343 		break;
344 
345 	case UAC_SET_MAX:
346 		break;
347 
348 	case UAC_SET_RES:
349 		break;
350 
351 	case UAC_SET_MEM:
352 		break;
353 
354 	default:
355 		break;
356 	}
357 
358 	return value;
359 }
360 
audio_get_endpoint_req(struct usb_function * f,const struct usb_ctrlrequest * ctrl)361 static int audio_get_endpoint_req(struct usb_function *f,
362 		const struct usb_ctrlrequest *ctrl)
363 {
364 	struct usb_composite_dev *cdev = f->config->cdev;
365 	int value = -EOPNOTSUPP;
366 	u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
367 	u16 len = le16_to_cpu(ctrl->wLength);
368 	u16 w_value = le16_to_cpu(ctrl->wValue);
369 
370 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
371 			ctrl->bRequest, w_value, len, ep);
372 
373 	switch (ctrl->bRequest) {
374 	case UAC_GET_CUR:
375 	case UAC_GET_MIN:
376 	case UAC_GET_MAX:
377 	case UAC_GET_RES:
378 		value = len;
379 		break;
380 	case UAC_GET_MEM:
381 		break;
382 	default:
383 		break;
384 	}
385 
386 	return value;
387 }
388 
389 static int
f_audio_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)390 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
391 {
392 	struct usb_composite_dev *cdev = f->config->cdev;
393 	struct usb_request	*req = cdev->req;
394 	int			value = -EOPNOTSUPP;
395 	u16			w_index = le16_to_cpu(ctrl->wIndex);
396 	u16			w_value = le16_to_cpu(ctrl->wValue);
397 	u16			w_length = le16_to_cpu(ctrl->wLength);
398 
399 	/* composite driver infrastructure handles everything; interface
400 	 * activation uses set_alt().
401 	 */
402 	switch (ctrl->bRequestType) {
403 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
404 		value = audio_set_endpoint_req(f, ctrl);
405 		break;
406 
407 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
408 		value = audio_get_endpoint_req(f, ctrl);
409 		break;
410 
411 	default:
412 		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
413 			ctrl->bRequestType, ctrl->bRequest,
414 			w_value, w_index, w_length);
415 	}
416 
417 	/* respond with data transfer or status phase? */
418 	if (value >= 0) {
419 		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
420 			ctrl->bRequestType, ctrl->bRequest,
421 			w_value, w_index, w_length);
422 		req->zero = 0;
423 		req->length = value;
424 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
425 		if (value < 0)
426 			ERROR(cdev, "audio response on err %d\n", value);
427 	}
428 
429 	/* device either stalls (value < 0) or reports success */
430 	return value;
431 }
432 
f_audio_set_alt(struct usb_function * f,unsigned intf,unsigned alt)433 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
434 {
435 	struct usb_composite_dev *cdev = f->config->cdev;
436 	struct usb_gadget *gadget = cdev->gadget;
437 	struct device *dev = &gadget->dev;
438 	struct f_uac1 *uac1 = func_to_uac1(f);
439 	int ret = 0;
440 
441 	/* No i/f has more than 2 alt settings */
442 	if (alt > 1) {
443 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
444 		return -EINVAL;
445 	}
446 
447 	if (intf == uac1->ac_intf) {
448 		/* Control I/f has only 1 AltSetting - 0 */
449 		if (alt) {
450 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
451 			return -EINVAL;
452 		}
453 		return 0;
454 	}
455 
456 	if (intf == uac1->as_out_intf) {
457 		uac1->as_out_alt = alt;
458 
459 		if (alt)
460 			ret = u_audio_start_capture(&uac1->g_audio);
461 		else
462 			u_audio_stop_capture(&uac1->g_audio);
463 	} else if (intf == uac1->as_in_intf) {
464 		uac1->as_in_alt = alt;
465 
466 			if (alt)
467 				ret = u_audio_start_playback(&uac1->g_audio);
468 			else
469 				u_audio_stop_playback(&uac1->g_audio);
470 	} else {
471 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
472 		return -EINVAL;
473 	}
474 
475 	return ret;
476 }
477 
f_audio_get_alt(struct usb_function * f,unsigned intf)478 static int f_audio_get_alt(struct usb_function *f, unsigned intf)
479 {
480 	struct usb_composite_dev *cdev = f->config->cdev;
481 	struct usb_gadget *gadget = cdev->gadget;
482 	struct device *dev = &gadget->dev;
483 	struct f_uac1 *uac1 = func_to_uac1(f);
484 
485 	if (intf == uac1->ac_intf)
486 		return uac1->ac_alt;
487 	else if (intf == uac1->as_out_intf)
488 		return uac1->as_out_alt;
489 	else if (intf == uac1->as_in_intf)
490 		return uac1->as_in_alt;
491 	else
492 		dev_err(dev, "%s:%d Invalid Interface %d!\n",
493 			__func__, __LINE__, intf);
494 
495 	return -EINVAL;
496 }
497 
498 
f_audio_disable(struct usb_function * f)499 static void f_audio_disable(struct usb_function *f)
500 {
501 	struct f_uac1 *uac1 = func_to_uac1(f);
502 
503 	uac1->as_out_alt = 0;
504 	uac1->as_in_alt = 0;
505 
506 	u_audio_stop_capture(&uac1->g_audio);
507 }
508 
509 /*-------------------------------------------------------------------------*/
510 
511 /* audio function driver setup/binding */
f_audio_bind(struct usb_configuration * c,struct usb_function * f)512 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
513 {
514 	struct usb_composite_dev	*cdev = c->cdev;
515 	struct usb_gadget		*gadget = cdev->gadget;
516 	struct f_uac1			*uac1 = func_to_uac1(f);
517 	struct g_audio			*audio = func_to_g_audio(f);
518 	struct f_uac1_opts		*audio_opts;
519 	struct usb_ep			*ep = NULL;
520 	struct usb_string		*us;
521 	u8				*sam_freq;
522 	int				rate;
523 	int				status;
524 
525 	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
526 
527 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
528 	if (IS_ERR(us))
529 		return PTR_ERR(us);
530 	ac_interface_desc.iInterface = us[STR_AC_IF].id;
531 	usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
532 	usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
533 	io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
534 	as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
535 	as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
536 	io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
537 	io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
538 	usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
539 	as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
540 	as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
541 
542 	/* Set channel numbers */
543 	usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
544 	usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
545 	as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
546 	as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
547 	as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
548 	io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
549 	io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
550 	as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
551 	as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
552 	as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
553 
554 	/* Set sample rates */
555 	rate = audio_opts->c_srate;
556 	sam_freq = as_out_type_i_desc.tSamFreq[0];
557 	memcpy(sam_freq, &rate, 3);
558 	rate = audio_opts->p_srate;
559 	sam_freq = as_in_type_i_desc.tSamFreq[0];
560 	memcpy(sam_freq, &rate, 3);
561 
562 	/* allocate instance-specific interface IDs, and patch descriptors */
563 	status = usb_interface_id(c, f);
564 	if (status < 0)
565 		goto fail;
566 	ac_interface_desc.bInterfaceNumber = status;
567 	uac1->ac_intf = status;
568 	uac1->ac_alt = 0;
569 
570 	status = usb_interface_id(c, f);
571 	if (status < 0)
572 		goto fail;
573 	as_out_interface_alt_0_desc.bInterfaceNumber = status;
574 	as_out_interface_alt_1_desc.bInterfaceNumber = status;
575 	uac1->as_out_intf = status;
576 	uac1->as_out_alt = 0;
577 
578 	status = usb_interface_id(c, f);
579 	if (status < 0)
580 		goto fail;
581 	as_in_interface_alt_0_desc.bInterfaceNumber = status;
582 	as_in_interface_alt_1_desc.bInterfaceNumber = status;
583 	uac1->as_in_intf = status;
584 	uac1->as_in_alt = 0;
585 
586 	audio->gadget = gadget;
587 
588 	status = -ENODEV;
589 
590 	/* allocate instance-specific endpoints */
591 	ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
592 	if (!ep)
593 		goto fail;
594 	audio->out_ep = ep;
595 	audio->out_ep->desc = &as_out_ep_desc;
596 
597 	ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
598 	if (!ep)
599 		goto fail;
600 	audio->in_ep = ep;
601 	audio->in_ep->desc = &as_in_ep_desc;
602 
603 	/* copy descriptors, and track endpoint copies */
604 	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
605 					NULL);
606 	if (status)
607 		goto fail;
608 
609 	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
610 	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
611 	audio->params.c_chmask = audio_opts->c_chmask;
612 	audio->params.c_srate = audio_opts->c_srate;
613 	audio->params.c_ssize = audio_opts->c_ssize;
614 	audio->params.p_chmask = audio_opts->p_chmask;
615 	audio->params.p_srate = audio_opts->p_srate;
616 	audio->params.p_ssize = audio_opts->p_ssize;
617 	audio->params.req_number = audio_opts->req_number;
618 
619 	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
620 	if (status)
621 		goto err_card_register;
622 
623 	return 0;
624 
625 err_card_register:
626 	usb_free_all_descriptors(f);
627 fail:
628 	return status;
629 }
630 
631 /*-------------------------------------------------------------------------*/
632 
to_f_uac1_opts(struct config_item * item)633 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
634 {
635 	return container_of(to_config_group(item), struct f_uac1_opts,
636 			    func_inst.group);
637 }
638 
f_uac1_attr_release(struct config_item * item)639 static void f_uac1_attr_release(struct config_item *item)
640 {
641 	struct f_uac1_opts *opts = to_f_uac1_opts(item);
642 
643 	usb_put_function_instance(&opts->func_inst);
644 }
645 
646 static struct configfs_item_operations f_uac1_item_ops = {
647 	.release	= f_uac1_attr_release,
648 };
649 
650 #define UAC1_ATTRIBUTE(name)						\
651 static ssize_t f_uac1_opts_##name##_show(				\
652 					  struct config_item *item,	\
653 					  char *page)			\
654 {									\
655 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
656 	int result;							\
657 									\
658 	mutex_lock(&opts->lock);					\
659 	result = sprintf(page, "%u\n", opts->name);			\
660 	mutex_unlock(&opts->lock);					\
661 									\
662 	return result;							\
663 }									\
664 									\
665 static ssize_t f_uac1_opts_##name##_store(				\
666 					  struct config_item *item,	\
667 					  const char *page, size_t len)	\
668 {									\
669 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
670 	int ret;							\
671 	u32 num;							\
672 									\
673 	mutex_lock(&opts->lock);					\
674 	if (opts->refcnt) {						\
675 		ret = -EBUSY;						\
676 		goto end;						\
677 	}								\
678 									\
679 	ret = kstrtou32(page, 0, &num);					\
680 	if (ret)							\
681 		goto end;						\
682 									\
683 	opts->name = num;						\
684 	ret = len;							\
685 									\
686 end:									\
687 	mutex_unlock(&opts->lock);					\
688 	return ret;							\
689 }									\
690 									\
691 CONFIGFS_ATTR(f_uac1_opts_, name)
692 
693 UAC1_ATTRIBUTE(c_chmask);
694 UAC1_ATTRIBUTE(c_srate);
695 UAC1_ATTRIBUTE(c_ssize);
696 UAC1_ATTRIBUTE(p_chmask);
697 UAC1_ATTRIBUTE(p_srate);
698 UAC1_ATTRIBUTE(p_ssize);
699 UAC1_ATTRIBUTE(req_number);
700 
701 static struct configfs_attribute *f_uac1_attrs[] = {
702 	&f_uac1_opts_attr_c_chmask,
703 	&f_uac1_opts_attr_c_srate,
704 	&f_uac1_opts_attr_c_ssize,
705 	&f_uac1_opts_attr_p_chmask,
706 	&f_uac1_opts_attr_p_srate,
707 	&f_uac1_opts_attr_p_ssize,
708 	&f_uac1_opts_attr_req_number,
709 	NULL,
710 };
711 
712 static struct config_item_type f_uac1_func_type = {
713 	.ct_item_ops	= &f_uac1_item_ops,
714 	.ct_attrs	= f_uac1_attrs,
715 	.ct_owner	= THIS_MODULE,
716 };
717 
f_audio_free_inst(struct usb_function_instance * f)718 static void f_audio_free_inst(struct usb_function_instance *f)
719 {
720 	struct f_uac1_opts *opts;
721 
722 	opts = container_of(f, struct f_uac1_opts, func_inst);
723 	kfree(opts);
724 }
725 
f_audio_alloc_inst(void)726 static struct usb_function_instance *f_audio_alloc_inst(void)
727 {
728 	struct f_uac1_opts *opts;
729 
730 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
731 	if (!opts)
732 		return ERR_PTR(-ENOMEM);
733 
734 	mutex_init(&opts->lock);
735 	opts->func_inst.free_func_inst = f_audio_free_inst;
736 
737 	config_group_init_type_name(&opts->func_inst.group, "",
738 				    &f_uac1_func_type);
739 
740 	opts->c_chmask = UAC1_DEF_CCHMASK;
741 	opts->c_srate = UAC1_DEF_CSRATE;
742 	opts->c_ssize = UAC1_DEF_CSSIZE;
743 	opts->p_chmask = UAC1_DEF_PCHMASK;
744 	opts->p_srate = UAC1_DEF_PSRATE;
745 	opts->p_ssize = UAC1_DEF_PSSIZE;
746 	opts->req_number = UAC1_DEF_REQ_NUM;
747 	return &opts->func_inst;
748 }
749 
f_audio_free(struct usb_function * f)750 static void f_audio_free(struct usb_function *f)
751 {
752 	struct g_audio *audio;
753 	struct f_uac1_opts *opts;
754 
755 	audio = func_to_g_audio(f);
756 	opts = container_of(f->fi, struct f_uac1_opts, func_inst);
757 	kfree(audio);
758 	mutex_lock(&opts->lock);
759 	--opts->refcnt;
760 	mutex_unlock(&opts->lock);
761 }
762 
f_audio_unbind(struct usb_configuration * c,struct usb_function * f)763 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
764 {
765 	struct g_audio *audio = func_to_g_audio(f);
766 
767 	g_audio_cleanup(audio);
768 	usb_free_all_descriptors(f);
769 
770 	audio->gadget = NULL;
771 }
772 
f_audio_alloc(struct usb_function_instance * fi)773 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
774 {
775 	struct f_uac1 *uac1;
776 	struct f_uac1_opts *opts;
777 
778 	/* allocate and initialize one new instance */
779 	uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
780 	if (!uac1)
781 		return ERR_PTR(-ENOMEM);
782 
783 	opts = container_of(fi, struct f_uac1_opts, func_inst);
784 	mutex_lock(&opts->lock);
785 	++opts->refcnt;
786 	mutex_unlock(&opts->lock);
787 
788 	uac1->g_audio.func.name = "uac1_func";
789 	uac1->g_audio.func.bind = f_audio_bind;
790 	uac1->g_audio.func.unbind = f_audio_unbind;
791 	uac1->g_audio.func.set_alt = f_audio_set_alt;
792 	uac1->g_audio.func.get_alt = f_audio_get_alt;
793 	uac1->g_audio.func.setup = f_audio_setup;
794 	uac1->g_audio.func.disable = f_audio_disable;
795 	uac1->g_audio.func.free_func = f_audio_free;
796 
797 	return &uac1->g_audio.func;
798 }
799 
800 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
801 MODULE_LICENSE("GPL");
802 MODULE_AUTHOR("Ruslan Bilovol");
803