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