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  * Copyright (C) 2021 Julian Scheel <julian@jusst.de>
7  *
8  * This driver doesn't expect any real Audio codec to be present
9  * on the device - the audio streams are simply sinked to and
10  * sourced from a virtual ALSA sound card created.
11  *
12  * This file is based on f_uac1.c which is
13  *   Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
14  *   Copyright (C) 2008 Analog Devices, Inc
15  */
16 
17 #include <linux/usb/audio.h>
18 #include <linux/module.h>
19 
20 #include "u_audio.h"
21 #include "u_uac1.h"
22 
23 /* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */
24 #define UAC1_CHANNEL_MASK 0x0FFF
25 
26 #define USB_OUT_FU_ID	(out_feature_unit_desc->bUnitID)
27 #define USB_IN_FU_ID	(in_feature_unit_desc->bUnitID)
28 
29 #define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
30 #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
31 #define FUIN_EN(_opts) ((_opts)->p_mute_present \
32 			|| (_opts)->p_volume_present)
33 #define FUOUT_EN(_opts) ((_opts)->c_mute_present \
34 			|| (_opts)->c_volume_present)
35 
36 struct f_uac1 {
37 	struct g_audio g_audio;
38 	u8 ac_intf, as_in_intf, as_out_intf;
39 	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
40 
41 	struct usb_ctrlrequest setup_cr;	/* will be used in data stage */
42 
43 	/* Interrupt IN endpoint of AC interface */
44 	struct usb_ep	*int_ep;
45 	atomic_t	int_count;
46 	int ctl_id;		/* EP id */
47 	int c_srate;	/* current capture srate */
48 	int p_srate;	/* current playback prate */
49 };
50 
51 static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
52 {
53 	return container_of(f, struct f_uac1, g_audio.func);
54 }
55 
56 static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio)
57 {
58 	return container_of(audio->func.fi, struct f_uac1_opts, func_inst);
59 }
60 
61 /*
62  * DESCRIPTORS ... most are static, but strings and full
63  * configuration descriptors are built on demand.
64  */
65 
66 /*
67  * We have three interfaces - one AudioControl and two AudioStreaming
68  *
69  * The driver implements a simple UAC_1 topology.
70  * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture
71  * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN
72  */
73 
74 /* B.3.1  Standard AC Interface Descriptor */
75 static struct usb_interface_descriptor ac_interface_desc = {
76 	.bLength =		USB_DT_INTERFACE_SIZE,
77 	.bDescriptorType =	USB_DT_INTERFACE,
78 	/* .bNumEndpoints =	DYNAMIC */
79 	.bInterfaceClass =	USB_CLASS_AUDIO,
80 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL,
81 };
82 
83 /* B.3.2  Class-Specific AC Interface Descriptor */
84 static struct uac1_ac_header_descriptor *ac_header_desc;
85 
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 =	DYNAMIC */
91 	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
92 	.bAssocTerminal =	0,
93 	.wChannelConfig =	cpu_to_le16(0x3),
94 };
95 
96 static struct uac1_output_terminal_descriptor io_out_ot_desc = {
97 	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE,
98 	.bDescriptorType	= USB_DT_CS_INTERFACE,
99 	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL,
100 	/* .bTerminalID =	DYNAMIC */
101 	.wTerminalType		= cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
102 	.bAssocTerminal		= 0,
103 	/* .bSourceID =		DYNAMIC */
104 };
105 
106 static struct uac_input_terminal_descriptor io_in_it_desc = {
107 	.bLength		= UAC_DT_INPUT_TERMINAL_SIZE,
108 	.bDescriptorType	= USB_DT_CS_INTERFACE,
109 	.bDescriptorSubtype	= UAC_INPUT_TERMINAL,
110 	/* .bTerminalID		= DYNAMIC */
111 	.wTerminalType		= cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
112 	.bAssocTerminal		= 0,
113 	.wChannelConfig		= cpu_to_le16(0x3),
114 };
115 
116 static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
117 	.bLength =		UAC_DT_OUTPUT_TERMINAL_SIZE,
118 	.bDescriptorType =	USB_DT_CS_INTERFACE,
119 	.bDescriptorSubtype =	UAC_OUTPUT_TERMINAL,
120 	/* .bTerminalID =	DYNAMIC */
121 	.wTerminalType =	cpu_to_le16(UAC_TERMINAL_STREAMING),
122 	.bAssocTerminal =	0,
123 	/* .bSourceID =		DYNAMIC */
124 };
125 
126 static struct uac_feature_unit_descriptor *in_feature_unit_desc;
127 static struct uac_feature_unit_descriptor *out_feature_unit_desc;
128 
129 /* AC IN Interrupt Endpoint */
130 static struct usb_endpoint_descriptor ac_int_ep_desc = {
131 	.bLength = USB_DT_ENDPOINT_SIZE,
132 	.bDescriptorType = USB_DT_ENDPOINT,
133 	.bEndpointAddress = USB_DIR_IN,
134 	.bmAttributes = USB_ENDPOINT_XFER_INT,
135 	.wMaxPacketSize = cpu_to_le16(2),
136 	.bInterval = 4,
137 };
138 
139 /* B.4.1  Standard AS Interface Descriptor */
140 static struct usb_interface_descriptor as_out_interface_alt_0_desc = {
141 	.bLength =		USB_DT_INTERFACE_SIZE,
142 	.bDescriptorType =	USB_DT_INTERFACE,
143 	.bAlternateSetting =	0,
144 	.bNumEndpoints =	0,
145 	.bInterfaceClass =	USB_CLASS_AUDIO,
146 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
147 };
148 
149 static struct usb_interface_descriptor as_out_interface_alt_1_desc = {
150 	.bLength =		USB_DT_INTERFACE_SIZE,
151 	.bDescriptorType =	USB_DT_INTERFACE,
152 	.bAlternateSetting =	1,
153 	.bNumEndpoints =	1,
154 	.bInterfaceClass =	USB_CLASS_AUDIO,
155 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
156 };
157 
158 static struct usb_interface_descriptor as_in_interface_alt_0_desc = {
159 	.bLength =		USB_DT_INTERFACE_SIZE,
160 	.bDescriptorType =	USB_DT_INTERFACE,
161 	.bAlternateSetting =	0,
162 	.bNumEndpoints =	0,
163 	.bInterfaceClass =	USB_CLASS_AUDIO,
164 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
165 };
166 
167 static struct usb_interface_descriptor as_in_interface_alt_1_desc = {
168 	.bLength =		USB_DT_INTERFACE_SIZE,
169 	.bDescriptorType =	USB_DT_INTERFACE,
170 	.bAlternateSetting =	1,
171 	.bNumEndpoints =	1,
172 	.bInterfaceClass =	USB_CLASS_AUDIO,
173 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOSTREAMING,
174 };
175 
176 /* B.4.2  Class-Specific AS Interface Descriptor */
177 static struct uac1_as_header_descriptor as_out_header_desc = {
178 	.bLength =		UAC_DT_AS_HEADER_SIZE,
179 	.bDescriptorType =	USB_DT_CS_INTERFACE,
180 	.bDescriptorSubtype =	UAC_AS_GENERAL,
181 	/* .bTerminalLink =	DYNAMIC */
182 	.bDelay =		1,
183 	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
184 };
185 
186 static struct uac1_as_header_descriptor as_in_header_desc = {
187 	.bLength =		UAC_DT_AS_HEADER_SIZE,
188 	.bDescriptorType =	USB_DT_CS_INTERFACE,
189 	.bDescriptorSubtype =	UAC_AS_GENERAL,
190 	/* .bTerminalLink =	DYNAMIC */
191 	.bDelay =		1,
192 	.wFormatTag =		cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
193 };
194 
195 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(UAC_MAX_RATES);
196 #define uac_format_type_i_discrete_descriptor			\
197 	uac_format_type_i_discrete_descriptor_##UAC_MAX_RATES
198 
199 static struct uac_format_type_i_discrete_descriptor as_out_type_i_desc = {
200 	.bLength =		0, /* filled on rate setup */
201 	.bDescriptorType =	USB_DT_CS_INTERFACE,
202 	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
203 	.bFormatType =		UAC_FORMAT_TYPE_I,
204 	.bSubframeSize =	2,
205 	.bBitResolution =	16,
206 	.bSamFreqType =		0, /* filled on rate setup */
207 };
208 
209 /* Standard ISO OUT Endpoint Descriptor */
210 static struct usb_endpoint_descriptor as_out_ep_desc  = {
211 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
212 	.bDescriptorType =	USB_DT_ENDPOINT,
213 	.bEndpointAddress =	USB_DIR_OUT,
214 	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE
215 				| USB_ENDPOINT_XFER_ISOC,
216 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
217 	.bInterval =		4,
218 };
219 
220 /* Class-specific AS ISO OUT Endpoint Descriptor */
221 static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
222 	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
223 	.bDescriptorType =	USB_DT_CS_ENDPOINT,
224 	.bDescriptorSubtype =	UAC_EP_GENERAL,
225 	.bmAttributes =		1,
226 	.bLockDelayUnits =	1,
227 	.wLockDelay =		cpu_to_le16(1),
228 };
229 
230 static struct uac_format_type_i_discrete_descriptor as_in_type_i_desc = {
231 	.bLength =		0, /* filled on rate setup */
232 	.bDescriptorType =	USB_DT_CS_INTERFACE,
233 	.bDescriptorSubtype =	UAC_FORMAT_TYPE,
234 	.bFormatType =		UAC_FORMAT_TYPE_I,
235 	.bSubframeSize =	2,
236 	.bBitResolution =	16,
237 	.bSamFreqType =		0, /* filled on rate setup */
238 };
239 
240 /* Standard ISO OUT Endpoint Descriptor */
241 static struct usb_endpoint_descriptor as_in_ep_desc  = {
242 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
243 	.bDescriptorType =	USB_DT_ENDPOINT,
244 	.bEndpointAddress =	USB_DIR_IN,
245 	.bmAttributes =		USB_ENDPOINT_SYNC_ASYNC
246 				| USB_ENDPOINT_XFER_ISOC,
247 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
248 	.bInterval =		4,
249 };
250 
251 /* Class-specific AS ISO OUT Endpoint Descriptor */
252 static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
253 	.bLength =		UAC_ISO_ENDPOINT_DESC_SIZE,
254 	.bDescriptorType =	USB_DT_CS_ENDPOINT,
255 	.bDescriptorSubtype =	UAC_EP_GENERAL,
256 	.bmAttributes =		1,
257 	.bLockDelayUnits =	0,
258 	.wLockDelay =		0,
259 };
260 
261 static struct usb_descriptor_header *f_audio_desc[] = {
262 	(struct usb_descriptor_header *)&ac_interface_desc,
263 	(struct usb_descriptor_header *)&ac_header_desc,
264 
265 	(struct usb_descriptor_header *)&usb_out_it_desc,
266 	(struct usb_descriptor_header *)&io_out_ot_desc,
267 	(struct usb_descriptor_header *)&out_feature_unit_desc,
268 
269 	(struct usb_descriptor_header *)&io_in_it_desc,
270 	(struct usb_descriptor_header *)&usb_in_ot_desc,
271 	(struct usb_descriptor_header *)&in_feature_unit_desc,
272 
273 	(struct usb_descriptor_header *)&ac_int_ep_desc,
274 
275 	(struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
276 	(struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
277 	(struct usb_descriptor_header *)&as_out_header_desc,
278 
279 	(struct usb_descriptor_header *)&as_out_type_i_desc,
280 
281 	(struct usb_descriptor_header *)&as_out_ep_desc,
282 	(struct usb_descriptor_header *)&as_iso_out_desc,
283 
284 	(struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
285 	(struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
286 	(struct usb_descriptor_header *)&as_in_header_desc,
287 
288 	(struct usb_descriptor_header *)&as_in_type_i_desc,
289 
290 	(struct usb_descriptor_header *)&as_in_ep_desc,
291 	(struct usb_descriptor_header *)&as_iso_in_desc,
292 	NULL,
293 };
294 
295 enum {
296 	STR_AC_IF,
297 	STR_USB_OUT_IT,
298 	STR_USB_OUT_IT_CH_NAMES,
299 	STR_IO_OUT_OT,
300 	STR_IO_IN_IT,
301 	STR_IO_IN_IT_CH_NAMES,
302 	STR_USB_IN_OT,
303 	STR_FU_IN,
304 	STR_FU_OUT,
305 	STR_AS_OUT_IF_ALT0,
306 	STR_AS_OUT_IF_ALT1,
307 	STR_AS_IN_IF_ALT0,
308 	STR_AS_IN_IF_ALT1,
309 };
310 
311 static struct usb_string strings_uac1[] = {
312 	/* [STR_AC_IF].s = DYNAMIC, */
313 	[STR_USB_OUT_IT].s = "Playback Input terminal",
314 	[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
315 	[STR_IO_OUT_OT].s = "Playback Output terminal",
316 	[STR_IO_IN_IT].s = "Capture Input terminal",
317 	[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
318 	[STR_USB_IN_OT].s = "Capture Output terminal",
319 	[STR_FU_IN].s = "Capture Volume",
320 	[STR_FU_OUT].s = "Playback Volume",
321 	[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
322 	[STR_AS_OUT_IF_ALT1].s = "Playback Active",
323 	[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
324 	[STR_AS_IN_IF_ALT1].s = "Capture Active",
325 	{ },
326 };
327 
328 static struct usb_gadget_strings str_uac1 = {
329 	.language = 0x0409,	/* en-us */
330 	.strings = strings_uac1,
331 };
332 
333 static struct usb_gadget_strings *uac1_strings[] = {
334 	&str_uac1,
335 	NULL,
336 };
337 
338 /*
339  * This function is an ALSA sound card following USB Audio Class Spec 1.0.
340  */
341 
342 static void uac_cs_attr_sample_rate(struct usb_ep *ep, struct usb_request *req)
343 {
344 	struct usb_function *fn = ep->driver_data;
345 	struct usb_composite_dev *cdev = fn->config->cdev;
346 	struct g_audio *agdev = func_to_g_audio(fn);
347 	struct f_uac1 *uac1 = func_to_uac1(fn);
348 	u8 *buf = (u8 *)req->buf;
349 	u32 val = 0;
350 
351 	if (req->actual != 3) {
352 		WARN(cdev, "Invalid data size for UAC_EP_CS_ATTR_SAMPLE_RATE.\n");
353 		return;
354 	}
355 
356 	val = buf[0] | (buf[1] << 8) | (buf[2] << 16);
357 	if (uac1->ctl_id == (USB_DIR_IN | 2)) {
358 		uac1->p_srate = val;
359 		u_audio_set_playback_srate(agdev, uac1->p_srate);
360 	} else if (uac1->ctl_id == (USB_DIR_OUT | 1)) {
361 		uac1->c_srate = val;
362 		u_audio_set_capture_srate(agdev, uac1->c_srate);
363 	}
364 }
365 
366 static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req)
367 {
368 	struct g_audio *audio = req->context;
369 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
370 
371 	atomic_dec(&uac1->int_count);
372 	kfree(req->buf);
373 	usb_ep_free_request(_ep, req);
374 }
375 
376 static int audio_notify(struct g_audio *audio, int unit_id, int cs)
377 {
378 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
379 	struct usb_request *req;
380 	struct uac1_status_word *msg;
381 	int ret;
382 
383 	if (!uac1->int_ep->enabled)
384 		return 0;
385 
386 	if (atomic_inc_return(&uac1->int_count) > UAC1_DEF_INT_REQ_NUM) {
387 		atomic_dec(&uac1->int_count);
388 		return 0;
389 	}
390 
391 	req = usb_ep_alloc_request(uac1->int_ep, GFP_ATOMIC);
392 	if (req == NULL) {
393 		ret = -ENOMEM;
394 		goto err_dec_int_count;
395 	}
396 
397 	msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
398 	if (msg == NULL) {
399 		ret = -ENOMEM;
400 		goto err_free_request;
401 	}
402 
403 	msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING
404 				| UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF;
405 	msg->bOriginator = unit_id;
406 
407 	req->length = sizeof(*msg);
408 	req->buf = msg;
409 	req->context = audio;
410 	req->complete = audio_notify_complete;
411 
412 	ret = usb_ep_queue(uac1->int_ep, req, GFP_ATOMIC);
413 
414 	if (ret)
415 		goto err_free_msg;
416 
417 	return 0;
418 
419 err_free_msg:
420 	kfree(msg);
421 err_free_request:
422 	usb_ep_free_request(uac1->int_ep, req);
423 err_dec_int_count:
424 	atomic_dec(&uac1->int_count);
425 
426 	return ret;
427 }
428 
429 static int
430 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
431 {
432 	struct usb_request *req = fn->config->cdev->req;
433 	struct g_audio *audio = func_to_g_audio(fn);
434 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
435 	u16 w_length = le16_to_cpu(cr->wLength);
436 	u16 w_index = le16_to_cpu(cr->wIndex);
437 	u16 w_value = le16_to_cpu(cr->wValue);
438 	u8 entity_id = (w_index >> 8) & 0xff;
439 	u8 control_selector = w_value >> 8;
440 	int value = -EOPNOTSUPP;
441 
442 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
443 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
444 		unsigned int is_playback = 0;
445 
446 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
447 			is_playback = 1;
448 
449 		if (control_selector == UAC_FU_MUTE) {
450 			unsigned int mute;
451 
452 			u_audio_get_mute(audio, is_playback, &mute);
453 
454 			*(u8 *)req->buf = mute;
455 			value = min_t(unsigned int, w_length, 1);
456 		} else if (control_selector == UAC_FU_VOLUME) {
457 			__le16 c;
458 			s16 volume;
459 
460 			u_audio_get_volume(audio, is_playback, &volume);
461 
462 			c = cpu_to_le16(volume);
463 
464 			value = min_t(unsigned int, w_length, sizeof(c));
465 			memcpy(req->buf, &c, value);
466 		} else {
467 			dev_err(&audio->gadget->dev,
468 				"%s:%d control_selector=%d TODO!\n",
469 				__func__, __LINE__, control_selector);
470 		}
471 	} else {
472 		dev_err(&audio->gadget->dev,
473 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
474 			__func__, __LINE__, entity_id, control_selector);
475 	}
476 
477 	return value;
478 }
479 
480 static int
481 in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr)
482 {
483 	struct usb_request *req = fn->config->cdev->req;
484 	struct g_audio *audio = func_to_g_audio(fn);
485 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
486 	u16 w_length = le16_to_cpu(cr->wLength);
487 	u16 w_index = le16_to_cpu(cr->wIndex);
488 	u16 w_value = le16_to_cpu(cr->wValue);
489 	u8 entity_id = (w_index >> 8) & 0xff;
490 	u8 control_selector = w_value >> 8;
491 	int value = -EOPNOTSUPP;
492 
493 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
494 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
495 		unsigned int is_playback = 0;
496 
497 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
498 			is_playback = 1;
499 
500 		if (control_selector == UAC_FU_VOLUME) {
501 			__le16 r;
502 			s16 min_db;
503 
504 			if (is_playback)
505 				min_db = opts->p_volume_min;
506 			else
507 				min_db = opts->c_volume_min;
508 
509 			r = cpu_to_le16(min_db);
510 
511 			value = min_t(unsigned int, w_length, sizeof(r));
512 			memcpy(req->buf, &r, value);
513 		} else {
514 			dev_err(&audio->gadget->dev,
515 				"%s:%d control_selector=%d TODO!\n",
516 				__func__, __LINE__, control_selector);
517 		}
518 	} else {
519 		dev_err(&audio->gadget->dev,
520 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
521 			__func__, __LINE__, entity_id, control_selector);
522 	}
523 
524 	return value;
525 }
526 
527 static int
528 in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr)
529 {
530 	struct usb_request *req = fn->config->cdev->req;
531 	struct g_audio *audio = func_to_g_audio(fn);
532 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
533 	u16 w_length = le16_to_cpu(cr->wLength);
534 	u16 w_index = le16_to_cpu(cr->wIndex);
535 	u16 w_value = le16_to_cpu(cr->wValue);
536 	u8 entity_id = (w_index >> 8) & 0xff;
537 	u8 control_selector = w_value >> 8;
538 	int value = -EOPNOTSUPP;
539 
540 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
541 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
542 		unsigned int is_playback = 0;
543 
544 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
545 			is_playback = 1;
546 
547 		if (control_selector == UAC_FU_VOLUME) {
548 			__le16 r;
549 			s16 max_db;
550 
551 			if (is_playback)
552 				max_db = opts->p_volume_max;
553 			else
554 				max_db = opts->c_volume_max;
555 
556 			r = cpu_to_le16(max_db);
557 
558 			value = min_t(unsigned int, w_length, sizeof(r));
559 			memcpy(req->buf, &r, value);
560 		} else {
561 			dev_err(&audio->gadget->dev,
562 				"%s:%d control_selector=%d TODO!\n",
563 				__func__, __LINE__, control_selector);
564 		}
565 	} else {
566 		dev_err(&audio->gadget->dev,
567 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
568 			__func__, __LINE__, entity_id, control_selector);
569 	}
570 
571 	return value;
572 }
573 
574 static int
575 in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr)
576 {
577 	struct usb_request *req = fn->config->cdev->req;
578 	struct g_audio *audio = func_to_g_audio(fn);
579 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
580 	u16 w_length = le16_to_cpu(cr->wLength);
581 	u16 w_index = le16_to_cpu(cr->wIndex);
582 	u16 w_value = le16_to_cpu(cr->wValue);
583 	u8 entity_id = (w_index >> 8) & 0xff;
584 	u8 control_selector = w_value >> 8;
585 	int value = -EOPNOTSUPP;
586 
587 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
588 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
589 		unsigned int is_playback = 0;
590 
591 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
592 			is_playback = 1;
593 
594 		if (control_selector == UAC_FU_VOLUME) {
595 			__le16 r;
596 			s16 res_db;
597 
598 			if (is_playback)
599 				res_db = opts->p_volume_res;
600 			else
601 				res_db = opts->c_volume_res;
602 
603 			r = cpu_to_le16(res_db);
604 
605 			value = min_t(unsigned int, w_length, sizeof(r));
606 			memcpy(req->buf, &r, value);
607 		} else {
608 			dev_err(&audio->gadget->dev,
609 				"%s:%d control_selector=%d TODO!\n",
610 				__func__, __LINE__, control_selector);
611 		}
612 	} else {
613 		dev_err(&audio->gadget->dev,
614 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
615 			__func__, __LINE__, entity_id, control_selector);
616 	}
617 
618 	return value;
619 }
620 
621 static void
622 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
623 {
624 	struct g_audio *audio = req->context;
625 	struct usb_composite_dev *cdev = audio->func.config->cdev;
626 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
627 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
628 	struct usb_ctrlrequest *cr = &uac1->setup_cr;
629 	u16 w_index = le16_to_cpu(cr->wIndex);
630 	u16 w_value = le16_to_cpu(cr->wValue);
631 	u8 entity_id = (w_index >> 8) & 0xff;
632 	u8 control_selector = w_value >> 8;
633 
634 	if (req->status != 0) {
635 		dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
636 		return;
637 	}
638 
639 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
640 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
641 		unsigned int is_playback = 0;
642 
643 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
644 			is_playback = 1;
645 
646 		if (control_selector == UAC_FU_MUTE) {
647 			u8 mute = *(u8 *)req->buf;
648 
649 			u_audio_set_mute(audio, is_playback, mute);
650 
651 			return;
652 		} else if (control_selector == UAC_FU_VOLUME) {
653 			__le16 *c = req->buf;
654 			s16 volume;
655 
656 			volume = le16_to_cpu(*c);
657 			u_audio_set_volume(audio, is_playback, volume);
658 
659 			return;
660 		} else {
661 			dev_err(&audio->gadget->dev,
662 				"%s:%d control_selector=%d TODO!\n",
663 				__func__, __LINE__, control_selector);
664 			usb_ep_set_halt(ep);
665 		}
666 	} else {
667 		dev_err(&audio->gadget->dev,
668 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
669 			__func__, __LINE__, entity_id, control_selector);
670 		usb_ep_set_halt(ep);
671 
672 	}
673 }
674 
675 static int
676 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
677 {
678 	struct usb_request *req = fn->config->cdev->req;
679 	struct g_audio *audio = func_to_g_audio(fn);
680 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
681 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
682 	u16 w_length = le16_to_cpu(cr->wLength);
683 	u16 w_index = le16_to_cpu(cr->wIndex);
684 	u16 w_value = le16_to_cpu(cr->wValue);
685 	u8 entity_id = (w_index >> 8) & 0xff;
686 	u8 control_selector = w_value >> 8;
687 
688 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
689 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
690 		memcpy(&uac1->setup_cr, cr, sizeof(*cr));
691 		req->context = audio;
692 		req->complete = out_rq_cur_complete;
693 
694 		return w_length;
695 	} else {
696 		dev_err(&audio->gadget->dev,
697 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
698 			__func__, __LINE__, entity_id, control_selector);
699 	}
700 	return -EOPNOTSUPP;
701 }
702 
703 static int ac_rq_in(struct usb_function *f,
704 		const struct usb_ctrlrequest *ctrl)
705 {
706 	struct usb_composite_dev *cdev = f->config->cdev;
707 	int value = -EOPNOTSUPP;
708 	u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
709 	u16 len = le16_to_cpu(ctrl->wLength);
710 	u16 w_value = le16_to_cpu(ctrl->wValue);
711 
712 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
713 			ctrl->bRequest, w_value, len, ep);
714 
715 	switch (ctrl->bRequest) {
716 	case UAC_GET_CUR:
717 		return in_rq_cur(f, ctrl);
718 	case UAC_GET_MIN:
719 		return in_rq_min(f, ctrl);
720 	case UAC_GET_MAX:
721 		return in_rq_max(f, ctrl);
722 	case UAC_GET_RES:
723 		return in_rq_res(f, ctrl);
724 	case UAC_GET_MEM:
725 		break;
726 	case UAC_GET_STAT:
727 		value = len;
728 		break;
729 	default:
730 		break;
731 	}
732 
733 	return value;
734 }
735 
736 static int audio_set_endpoint_req(struct usb_function *f,
737 		const struct usb_ctrlrequest *ctrl)
738 {
739 	struct usb_composite_dev *cdev = f->config->cdev;
740 	struct usb_request	*req = f->config->cdev->req;
741 	struct f_uac1		*uac1 = func_to_uac1(f);
742 	int			value = -EOPNOTSUPP;
743 	u16			ep = le16_to_cpu(ctrl->wIndex);
744 	u16			len = le16_to_cpu(ctrl->wLength);
745 	u16			w_value = le16_to_cpu(ctrl->wValue);
746 	u8			cs = w_value >> 8;
747 
748 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
749 			ctrl->bRequest, w_value, len, ep);
750 
751 	switch (ctrl->bRequest) {
752 	case UAC_SET_CUR: {
753 		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
754 			cdev->gadget->ep0->driver_data = f;
755 			uac1->ctl_id = ep;
756 			req->complete = uac_cs_attr_sample_rate;
757 		}
758 		value = len;
759 		break;
760 	}
761 
762 	case UAC_SET_MIN:
763 		break;
764 
765 	case UAC_SET_MAX:
766 		break;
767 
768 	case UAC_SET_RES:
769 		break;
770 
771 	case UAC_SET_MEM:
772 		break;
773 
774 	default:
775 		break;
776 	}
777 
778 	return value;
779 }
780 
781 static int audio_get_endpoint_req(struct usb_function *f,
782 		const struct usb_ctrlrequest *ctrl)
783 {
784 	struct usb_composite_dev *cdev = f->config->cdev;
785 	struct usb_request *req = f->config->cdev->req;
786 	struct f_uac1 *uac1 = func_to_uac1(f);
787 	u8 *buf = (u8 *)req->buf;
788 	int value = -EOPNOTSUPP;
789 	u8 ep = le16_to_cpu(ctrl->wIndex);
790 	u16 len = le16_to_cpu(ctrl->wLength);
791 	u16 w_value = le16_to_cpu(ctrl->wValue);
792 	u8 cs = w_value >> 8;
793 	u32 val = 0;
794 
795 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
796 			ctrl->bRequest, w_value, len, ep);
797 
798 	switch (ctrl->bRequest) {
799 	case UAC_GET_CUR: {
800 		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
801 			if (ep == (USB_DIR_IN | 2))
802 				val = uac1->p_srate;
803 			else if (ep == (USB_DIR_OUT | 1))
804 				val = uac1->c_srate;
805 			buf[2] = (val >> 16) & 0xff;
806 			buf[1] = (val >> 8) & 0xff;
807 			buf[0] = val & 0xff;
808 		}
809 		value = len;
810 		break;
811 	}
812 	case UAC_GET_MIN:
813 	case UAC_GET_MAX:
814 	case UAC_GET_RES:
815 		value = len;
816 		break;
817 	case UAC_GET_MEM:
818 		break;
819 	default:
820 		break;
821 	}
822 
823 	return value;
824 }
825 
826 static int
827 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
828 {
829 	struct usb_composite_dev *cdev = f->config->cdev;
830 	struct usb_request	*req = cdev->req;
831 	int			value = -EOPNOTSUPP;
832 	u16			w_index = le16_to_cpu(ctrl->wIndex);
833 	u16			w_value = le16_to_cpu(ctrl->wValue);
834 	u16			w_length = le16_to_cpu(ctrl->wLength);
835 
836 	/* composite driver infrastructure handles everything; interface
837 	 * activation uses set_alt().
838 	 */
839 	switch (ctrl->bRequestType) {
840 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
841 		value = audio_set_endpoint_req(f, ctrl);
842 		break;
843 
844 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
845 		value = audio_get_endpoint_req(f, ctrl);
846 		break;
847 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
848 		if (ctrl->bRequest == UAC_SET_CUR)
849 			value = out_rq_cur(f, ctrl);
850 		break;
851 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
852 		value = ac_rq_in(f, ctrl);
853 		break;
854 	default:
855 		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
856 			ctrl->bRequestType, ctrl->bRequest,
857 			w_value, w_index, w_length);
858 	}
859 
860 	/* respond with data transfer or status phase? */
861 	if (value >= 0) {
862 		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
863 			ctrl->bRequestType, ctrl->bRequest,
864 			w_value, w_index, w_length);
865 		req->zero = 0;
866 		req->length = value;
867 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
868 		if (value < 0)
869 			ERROR(cdev, "audio response on err %d\n", value);
870 	}
871 
872 	/* device either stalls (value < 0) or reports success */
873 	return value;
874 }
875 
876 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
877 {
878 	struct usb_composite_dev *cdev = f->config->cdev;
879 	struct usb_gadget *gadget = cdev->gadget;
880 	struct device *dev = &gadget->dev;
881 	struct g_audio *audio = func_to_g_audio(f);
882 	struct f_uac1 *uac1 = func_to_uac1(f);
883 	int ret = 0;
884 
885 	/* No i/f has more than 2 alt settings */
886 	if (alt > 1) {
887 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
888 		return -EINVAL;
889 	}
890 
891 	if (intf == uac1->ac_intf) {
892 		/* Control I/f has only 1 AltSetting - 0 */
893 		if (alt) {
894 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
895 			return -EINVAL;
896 		}
897 
898 		/* restart interrupt endpoint */
899 		if (uac1->int_ep) {
900 			usb_ep_disable(uac1->int_ep);
901 			config_ep_by_speed(gadget, &audio->func, uac1->int_ep);
902 			usb_ep_enable(uac1->int_ep);
903 		}
904 
905 		return 0;
906 	}
907 
908 	if (intf == uac1->as_out_intf) {
909 		uac1->as_out_alt = alt;
910 
911 		if (alt)
912 			ret = u_audio_start_capture(&uac1->g_audio);
913 		else
914 			u_audio_stop_capture(&uac1->g_audio);
915 	} else if (intf == uac1->as_in_intf) {
916 		uac1->as_in_alt = alt;
917 
918 		if (alt)
919 			ret = u_audio_start_playback(&uac1->g_audio);
920 		else
921 			u_audio_stop_playback(&uac1->g_audio);
922 	} else {
923 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
924 		return -EINVAL;
925 	}
926 
927 	return ret;
928 }
929 
930 static int f_audio_get_alt(struct usb_function *f, unsigned intf)
931 {
932 	struct usb_composite_dev *cdev = f->config->cdev;
933 	struct usb_gadget *gadget = cdev->gadget;
934 	struct device *dev = &gadget->dev;
935 	struct f_uac1 *uac1 = func_to_uac1(f);
936 
937 	if (intf == uac1->ac_intf)
938 		return uac1->ac_alt;
939 	else if (intf == uac1->as_out_intf)
940 		return uac1->as_out_alt;
941 	else if (intf == uac1->as_in_intf)
942 		return uac1->as_in_alt;
943 	else
944 		dev_err(dev, "%s:%d Invalid Interface %d!\n",
945 			__func__, __LINE__, intf);
946 
947 	return -EINVAL;
948 }
949 
950 
951 static void f_audio_disable(struct usb_function *f)
952 {
953 	struct f_uac1 *uac1 = func_to_uac1(f);
954 
955 	uac1->as_out_alt = 0;
956 	uac1->as_in_alt = 0;
957 
958 	u_audio_stop_playback(&uac1->g_audio);
959 	u_audio_stop_capture(&uac1->g_audio);
960 	if (uac1->int_ep)
961 		usb_ep_disable(uac1->int_ep);
962 }
963 
964 static void
965 f_audio_suspend(struct usb_function *f)
966 {
967 	struct f_uac1 *uac1 = func_to_uac1(f);
968 
969 	u_audio_suspend(&uac1->g_audio);
970 }
971 
972 /*-------------------------------------------------------------------------*/
973 static struct uac_feature_unit_descriptor *build_fu_desc(int chmask)
974 {
975 	struct uac_feature_unit_descriptor *fu_desc;
976 	int channels = num_channels(chmask);
977 	int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels);
978 
979 	fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
980 	if (!fu_desc)
981 		return NULL;
982 
983 	fu_desc->bLength = fu_desc_size;
984 	fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
985 
986 	fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
987 	fu_desc->bControlSize  = 2;
988 
989 	/* bUnitID, bSourceID and bmaControls will be defined later */
990 
991 	return fu_desc;
992 }
993 
994 /* B.3.2  Class-Specific AC Interface Descriptor */
995 static struct
996 uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts)
997 {
998 	struct uac1_ac_header_descriptor *ac_desc;
999 	int ac_header_desc_size;
1000 	int num_ifaces = 0;
1001 
1002 	if (EPOUT_EN(opts))
1003 		num_ifaces++;
1004 	if (EPIN_EN(opts))
1005 		num_ifaces++;
1006 
1007 	ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces);
1008 
1009 	ac_desc = kzalloc(ac_header_desc_size, GFP_KERNEL);
1010 	if (!ac_desc)
1011 		return NULL;
1012 
1013 	ac_desc->bLength = ac_header_desc_size;
1014 	ac_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1015 	ac_desc->bDescriptorSubtype = UAC_HEADER;
1016 	ac_desc->bcdADC = cpu_to_le16(0x0100);
1017 	ac_desc->bInCollection = num_ifaces;
1018 
1019 	/* wTotalLength and baInterfaceNr will be defined later */
1020 
1021 	return ac_desc;
1022 }
1023 
1024 /* Use macro to overcome line length limitation */
1025 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
1026 
1027 static void setup_descriptor(struct f_uac1_opts *opts)
1028 {
1029 	/* patch descriptors */
1030 	int i = 1; /* ID's start with 1 */
1031 
1032 	if (EPOUT_EN(opts))
1033 		usb_out_it_desc.bTerminalID = i++;
1034 	if (EPIN_EN(opts))
1035 		io_in_it_desc.bTerminalID = i++;
1036 	if (EPOUT_EN(opts))
1037 		io_out_ot_desc.bTerminalID = i++;
1038 	if (EPIN_EN(opts))
1039 		usb_in_ot_desc.bTerminalID = i++;
1040 	if (FUOUT_EN(opts))
1041 		out_feature_unit_desc->bUnitID = i++;
1042 	if (FUIN_EN(opts))
1043 		in_feature_unit_desc->bUnitID = i++;
1044 
1045 	if (FUIN_EN(opts)) {
1046 		usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
1047 		in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
1048 	} else {
1049 		usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
1050 	}
1051 	if (FUOUT_EN(opts)) {
1052 		io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
1053 		out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
1054 	} else {
1055 		io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
1056 	}
1057 
1058 	as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
1059 	as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
1060 
1061 	ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength);
1062 
1063 	if (EPIN_EN(opts)) {
1064 		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1065 
1066 		len += sizeof(usb_in_ot_desc);
1067 		len += sizeof(io_in_it_desc);
1068 		if (FUIN_EN(opts))
1069 			len += in_feature_unit_desc->bLength;
1070 		ac_header_desc->wTotalLength = cpu_to_le16(len);
1071 	}
1072 	if (EPOUT_EN(opts)) {
1073 		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1074 
1075 		len += sizeof(usb_out_it_desc);
1076 		len += sizeof(io_out_ot_desc);
1077 		if (FUOUT_EN(opts))
1078 			len += out_feature_unit_desc->bLength;
1079 		ac_header_desc->wTotalLength = cpu_to_le16(len);
1080 	}
1081 
1082 	i = 0;
1083 	f_audio_desc[i++] = USBDHDR(&ac_interface_desc);
1084 	f_audio_desc[i++] = USBDHDR(ac_header_desc);
1085 
1086 	if (EPOUT_EN(opts)) {
1087 		f_audio_desc[i++] = USBDHDR(&usb_out_it_desc);
1088 		f_audio_desc[i++] = USBDHDR(&io_out_ot_desc);
1089 		if (FUOUT_EN(opts))
1090 			f_audio_desc[i++] = USBDHDR(out_feature_unit_desc);
1091 	}
1092 
1093 	if (EPIN_EN(opts)) {
1094 		f_audio_desc[i++] = USBDHDR(&io_in_it_desc);
1095 		f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc);
1096 		if (FUIN_EN(opts))
1097 			f_audio_desc[i++] = USBDHDR(in_feature_unit_desc);
1098 	}
1099 
1100 	if (FUOUT_EN(opts) || FUIN_EN(opts))
1101 		f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc);
1102 
1103 	if (EPOUT_EN(opts)) {
1104 		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc);
1105 		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc);
1106 		f_audio_desc[i++] = USBDHDR(&as_out_header_desc);
1107 		f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc);
1108 		f_audio_desc[i++] = USBDHDR(&as_out_ep_desc);
1109 		f_audio_desc[i++] = USBDHDR(&as_iso_out_desc);
1110 	}
1111 	if (EPIN_EN(opts)) {
1112 		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc);
1113 		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc);
1114 		f_audio_desc[i++] = USBDHDR(&as_in_header_desc);
1115 		f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc);
1116 		f_audio_desc[i++] = USBDHDR(&as_in_ep_desc);
1117 		f_audio_desc[i++] = USBDHDR(&as_iso_in_desc);
1118 	}
1119 	f_audio_desc[i] = NULL;
1120 }
1121 
1122 static int f_audio_validate_opts(struct g_audio *audio, struct device *dev)
1123 {
1124 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
1125 
1126 	if (!opts->p_chmask && !opts->c_chmask) {
1127 		dev_err(dev, "Error: no playback and capture channels\n");
1128 		return -EINVAL;
1129 	} else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) {
1130 		dev_err(dev, "Error: unsupported playback channels mask\n");
1131 		return -EINVAL;
1132 	} else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) {
1133 		dev_err(dev, "Error: unsupported capture channels mask\n");
1134 		return -EINVAL;
1135 	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
1136 		dev_err(dev, "Error: incorrect playback sample size\n");
1137 		return -EINVAL;
1138 	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
1139 		dev_err(dev, "Error: incorrect capture sample size\n");
1140 		return -EINVAL;
1141 	} else if (!opts->p_srates[0]) {
1142 		dev_err(dev, "Error: incorrect playback sampling rate\n");
1143 		return -EINVAL;
1144 	} else if (!opts->c_srates[0]) {
1145 		dev_err(dev, "Error: incorrect capture sampling rate\n");
1146 		return -EINVAL;
1147 	}
1148 
1149 	if (opts->p_volume_max <= opts->p_volume_min) {
1150 		dev_err(dev, "Error: incorrect playback volume max/min\n");
1151 		return -EINVAL;
1152 	} else if (opts->c_volume_max <= opts->c_volume_min) {
1153 		dev_err(dev, "Error: incorrect capture volume max/min\n");
1154 		return -EINVAL;
1155 	} else if (opts->p_volume_res <= 0) {
1156 		dev_err(dev, "Error: negative/zero playback volume resolution\n");
1157 		return -EINVAL;
1158 	} else if (opts->c_volume_res <= 0) {
1159 		dev_err(dev, "Error: negative/zero capture volume resolution\n");
1160 		return -EINVAL;
1161 	}
1162 
1163 	if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
1164 		dev_err(dev, "Error: incorrect playback volume resolution\n");
1165 		return -EINVAL;
1166 	} else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
1167 		dev_err(dev, "Error: incorrect capture volume resolution\n");
1168 		return -EINVAL;
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 /* audio function driver setup/binding */
1175 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1176 {
1177 	struct usb_composite_dev	*cdev = c->cdev;
1178 	struct usb_gadget		*gadget = cdev->gadget;
1179 	struct device			*dev = &gadget->dev;
1180 	struct f_uac1			*uac1 = func_to_uac1(f);
1181 	struct g_audio			*audio = func_to_g_audio(f);
1182 	struct f_uac1_opts		*audio_opts;
1183 	struct usb_ep			*ep = NULL;
1184 	struct usb_string		*us;
1185 	int				ba_iface_id;
1186 	int				status;
1187 	int				idx, i;
1188 
1189 	status = f_audio_validate_opts(audio, dev);
1190 	if (status)
1191 		return status;
1192 
1193 	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1194 
1195 	strings_uac1[STR_AC_IF].s = audio_opts->function_name;
1196 
1197 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
1198 	if (IS_ERR(us))
1199 		return PTR_ERR(us);
1200 
1201 	ac_header_desc = build_ac_header_desc(audio_opts);
1202 	if (!ac_header_desc)
1203 		return -ENOMEM;
1204 
1205 	if (FUOUT_EN(audio_opts)) {
1206 		out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask);
1207 		if (!out_feature_unit_desc) {
1208 			status = -ENOMEM;
1209 			goto fail;
1210 		}
1211 	}
1212 	if (FUIN_EN(audio_opts)) {
1213 		in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask);
1214 		if (!in_feature_unit_desc) {
1215 			status = -ENOMEM;
1216 			goto err_free_fu;
1217 		}
1218 	}
1219 
1220 	ac_interface_desc.iInterface = us[STR_AC_IF].id;
1221 	usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
1222 	usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
1223 	io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
1224 	as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
1225 	as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
1226 	io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
1227 	io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
1228 	usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
1229 	as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
1230 	as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
1231 
1232 	if (FUOUT_EN(audio_opts)) {
1233 		u8 *i_feature;
1234 
1235 		i_feature = (u8 *)out_feature_unit_desc +
1236 					out_feature_unit_desc->bLength - 1;
1237 		*i_feature = us[STR_FU_OUT].id;
1238 	}
1239 	if (FUIN_EN(audio_opts)) {
1240 		u8 *i_feature;
1241 
1242 		i_feature = (u8 *)in_feature_unit_desc +
1243 					in_feature_unit_desc->bLength - 1;
1244 		*i_feature = us[STR_FU_IN].id;
1245 	}
1246 
1247 	/* Set channel numbers */
1248 	usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1249 	usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
1250 	as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1251 	as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
1252 	as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
1253 	io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1254 	io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
1255 	as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1256 	as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
1257 	as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
1258 
1259 	if (FUOUT_EN(audio_opts)) {
1260 		__le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0];
1261 		u32 control = 0;
1262 
1263 		if (audio_opts->c_mute_present)
1264 			control |= UAC_FU_MUTE;
1265 		if (audio_opts->c_volume_present)
1266 			control |= UAC_FU_VOLUME;
1267 		*bma = cpu_to_le16(control);
1268 	}
1269 	if (FUIN_EN(audio_opts)) {
1270 		__le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0];
1271 		u32 control = 0;
1272 
1273 		if (audio_opts->p_mute_present)
1274 			control |= UAC_FU_MUTE;
1275 		if (audio_opts->p_volume_present)
1276 			control |= UAC_FU_VOLUME;
1277 		*bma = cpu_to_le16(control);
1278 	}
1279 
1280 	/* Set sample rates */
1281 	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1282 		if (audio_opts->c_srates[i] == 0)
1283 			break;
1284 		memcpy(as_out_type_i_desc.tSamFreq[idx++],
1285 				&audio_opts->c_srates[i], 3);
1286 	}
1287 	as_out_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1288 	as_out_type_i_desc.bSamFreqType = idx;
1289 
1290 	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1291 		if (audio_opts->p_srates[i] == 0)
1292 			break;
1293 		memcpy(as_in_type_i_desc.tSamFreq[idx++],
1294 				&audio_opts->p_srates[i], 3);
1295 	}
1296 	as_in_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1297 	as_in_type_i_desc.bSamFreqType = idx;
1298 	uac1->p_srate = audio_opts->p_srates[0];
1299 	uac1->c_srate = audio_opts->c_srates[0];
1300 
1301 	/* allocate instance-specific interface IDs, and patch descriptors */
1302 	status = usb_interface_id(c, f);
1303 	if (status < 0)
1304 		goto err_free_fu;
1305 	ac_interface_desc.bInterfaceNumber = status;
1306 	uac1->ac_intf = status;
1307 	uac1->ac_alt = 0;
1308 
1309 	ba_iface_id = 0;
1310 
1311 	if (EPOUT_EN(audio_opts)) {
1312 		status = usb_interface_id(c, f);
1313 		if (status < 0)
1314 			goto err_free_fu;
1315 		as_out_interface_alt_0_desc.bInterfaceNumber = status;
1316 		as_out_interface_alt_1_desc.bInterfaceNumber = status;
1317 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1318 		uac1->as_out_intf = status;
1319 		uac1->as_out_alt = 0;
1320 	}
1321 
1322 	if (EPIN_EN(audio_opts)) {
1323 		status = usb_interface_id(c, f);
1324 		if (status < 0)
1325 			goto err_free_fu;
1326 		as_in_interface_alt_0_desc.bInterfaceNumber = status;
1327 		as_in_interface_alt_1_desc.bInterfaceNumber = status;
1328 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1329 		uac1->as_in_intf = status;
1330 		uac1->as_in_alt = 0;
1331 	}
1332 
1333 	audio->gadget = gadget;
1334 
1335 	status = -ENODEV;
1336 
1337 	ac_interface_desc.bNumEndpoints = 0;
1338 
1339 	/* allocate AC interrupt endpoint */
1340 	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) {
1341 		ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc);
1342 		if (!ep)
1343 			goto err_free_fu;
1344 		uac1->int_ep = ep;
1345 		uac1->int_ep->desc = &ac_int_ep_desc;
1346 
1347 		ac_interface_desc.bNumEndpoints = 1;
1348 	}
1349 
1350 	/* allocate instance-specific endpoints */
1351 	if (EPOUT_EN(audio_opts)) {
1352 		ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
1353 		if (!ep)
1354 			goto err_free_fu;
1355 		audio->out_ep = ep;
1356 		audio->out_ep->desc = &as_out_ep_desc;
1357 	}
1358 
1359 	if (EPIN_EN(audio_opts)) {
1360 		ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
1361 		if (!ep)
1362 			goto err_free_fu;
1363 		audio->in_ep = ep;
1364 		audio->in_ep->desc = &as_in_ep_desc;
1365 	}
1366 
1367 	setup_descriptor(audio_opts);
1368 
1369 	/* copy descriptors, and track endpoint copies */
1370 	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
1371 					NULL);
1372 	if (status)
1373 		goto err_free_fu;
1374 
1375 	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
1376 	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
1377 	audio->params.c_chmask = audio_opts->c_chmask;
1378 	memcpy(audio->params.c_srates, audio_opts->c_srates,
1379 			sizeof(audio->params.c_srates));
1380 	audio->params.c_ssize = audio_opts->c_ssize;
1381 	if (FUIN_EN(audio_opts)) {
1382 		audio->params.p_fu.id = USB_IN_FU_ID;
1383 		audio->params.p_fu.mute_present = audio_opts->p_mute_present;
1384 		audio->params.p_fu.volume_present =
1385 				audio_opts->p_volume_present;
1386 		audio->params.p_fu.volume_min = audio_opts->p_volume_min;
1387 		audio->params.p_fu.volume_max = audio_opts->p_volume_max;
1388 		audio->params.p_fu.volume_res = audio_opts->p_volume_res;
1389 	}
1390 	audio->params.p_chmask = audio_opts->p_chmask;
1391 	memcpy(audio->params.p_srates, audio_opts->p_srates,
1392 			sizeof(audio->params.p_srates));
1393 	audio->params.p_ssize = audio_opts->p_ssize;
1394 	if (FUOUT_EN(audio_opts)) {
1395 		audio->params.c_fu.id = USB_OUT_FU_ID;
1396 		audio->params.c_fu.mute_present = audio_opts->c_mute_present;
1397 		audio->params.c_fu.volume_present =
1398 				audio_opts->c_volume_present;
1399 		audio->params.c_fu.volume_min = audio_opts->c_volume_min;
1400 		audio->params.c_fu.volume_max = audio_opts->c_volume_max;
1401 		audio->params.c_fu.volume_res = audio_opts->c_volume_res;
1402 	}
1403 	audio->params.req_number = audio_opts->req_number;
1404 	audio->params.fb_max = FBACK_FAST_MAX;
1405 	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts))
1406 		audio->notify = audio_notify;
1407 
1408 	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
1409 	if (status)
1410 		goto err_card_register;
1411 
1412 	return 0;
1413 
1414 err_card_register:
1415 	usb_free_all_descriptors(f);
1416 err_free_fu:
1417 	kfree(out_feature_unit_desc);
1418 	out_feature_unit_desc = NULL;
1419 	kfree(in_feature_unit_desc);
1420 	in_feature_unit_desc = NULL;
1421 fail:
1422 	kfree(ac_header_desc);
1423 	ac_header_desc = NULL;
1424 	return status;
1425 }
1426 
1427 /*-------------------------------------------------------------------------*/
1428 
1429 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
1430 {
1431 	return container_of(to_config_group(item), struct f_uac1_opts,
1432 			    func_inst.group);
1433 }
1434 
1435 static void f_uac1_attr_release(struct config_item *item)
1436 {
1437 	struct f_uac1_opts *opts = to_f_uac1_opts(item);
1438 
1439 	usb_put_function_instance(&opts->func_inst);
1440 }
1441 
1442 static struct configfs_item_operations f_uac1_item_ops = {
1443 	.release	= f_uac1_attr_release,
1444 };
1445 
1446 #define uac1_kstrtou32			kstrtou32
1447 #define uac1_kstrtos16			kstrtos16
1448 #define uac1_kstrtobool(s, base, res)	kstrtobool((s), (res))
1449 
1450 static const char *u32_fmt = "%u\n";
1451 static const char *s16_fmt = "%hd\n";
1452 static const char *bool_fmt = "%u\n";
1453 
1454 #define UAC1_ATTRIBUTE(type, name)					\
1455 static ssize_t f_uac1_opts_##name##_show(				\
1456 					  struct config_item *item,	\
1457 					  char *page)			\
1458 {									\
1459 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1460 	int result;							\
1461 									\
1462 	mutex_lock(&opts->lock);					\
1463 	result = sprintf(page, type##_fmt, opts->name);			\
1464 	mutex_unlock(&opts->lock);					\
1465 									\
1466 	return result;							\
1467 }									\
1468 									\
1469 static ssize_t f_uac1_opts_##name##_store(				\
1470 					  struct config_item *item,	\
1471 					  const char *page, size_t len)	\
1472 {									\
1473 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1474 	int ret;							\
1475 	type num;							\
1476 									\
1477 	mutex_lock(&opts->lock);					\
1478 	if (opts->refcnt) {						\
1479 		ret = -EBUSY;						\
1480 		goto end;						\
1481 	}								\
1482 									\
1483 	ret = uac1_kstrto##type(page, 0, &num);				\
1484 	if (ret)							\
1485 		goto end;						\
1486 									\
1487 	opts->name = num;						\
1488 	ret = len;							\
1489 									\
1490 end:									\
1491 	mutex_unlock(&opts->lock);					\
1492 	return ret;							\
1493 }									\
1494 									\
1495 CONFIGFS_ATTR(f_uac1_opts_, name)
1496 
1497 #define UAC1_RATE_ATTRIBUTE(name)					\
1498 static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1499 					 char *page)			\
1500 {									\
1501 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1502 	int result = 0;							\
1503 	int i;								\
1504 									\
1505 	mutex_lock(&opts->lock);					\
1506 	page[0] = '\0';							\
1507 	for (i = 0; i < UAC_MAX_RATES; i++) {				\
1508 		if (opts->name##s[i] == 0)				\
1509 			break;						\
1510 		result += sprintf(page + strlen(page), "%u,",		\
1511 				opts->name##s[i]);			\
1512 	}								\
1513 	if (strlen(page) > 0)						\
1514 		page[strlen(page) - 1] = '\n';				\
1515 	mutex_unlock(&opts->lock);					\
1516 									\
1517 	return result;							\
1518 }									\
1519 									\
1520 static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1521 					  const char *page, size_t len)	\
1522 {									\
1523 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1524 	char *split_page = NULL;					\
1525 	int ret = -EINVAL;						\
1526 	char *token;							\
1527 	u32 num;							\
1528 	int i;								\
1529 									\
1530 	mutex_lock(&opts->lock);					\
1531 	if (opts->refcnt) {						\
1532 		ret = -EBUSY;						\
1533 		goto end;						\
1534 	}								\
1535 									\
1536 	i = 0;								\
1537 	memset(opts->name##s, 0x00, sizeof(opts->name##s));		\
1538 	split_page = kstrdup(page, GFP_KERNEL);				\
1539 	while ((token = strsep(&split_page, ",")) != NULL) {		\
1540 		ret = kstrtou32(token, 0, &num);			\
1541 		if (ret)						\
1542 			goto end;					\
1543 									\
1544 		opts->name##s[i++] = num;				\
1545 		ret = len;						\
1546 	};								\
1547 									\
1548 end:									\
1549 	kfree(split_page);						\
1550 	mutex_unlock(&opts->lock);					\
1551 	return ret;							\
1552 }									\
1553 									\
1554 CONFIGFS_ATTR(f_uac1_opts_, name)
1555 
1556 #define UAC1_ATTRIBUTE_STRING(name)					\
1557 static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1558 					 char *page)			\
1559 {									\
1560 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1561 	int result;							\
1562 									\
1563 	mutex_lock(&opts->lock);					\
1564 	result = snprintf(page, sizeof(opts->name), "%s", opts->name);	\
1565 	mutex_unlock(&opts->lock);					\
1566 									\
1567 	return result;							\
1568 }									\
1569 									\
1570 static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1571 					  const char *page, size_t len)	\
1572 {									\
1573 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1574 	int ret = 0;							\
1575 									\
1576 	mutex_lock(&opts->lock);					\
1577 	if (opts->refcnt) {						\
1578 		ret = -EBUSY;						\
1579 		goto end;						\
1580 	}								\
1581 									\
1582 	ret = snprintf(opts->name, min(sizeof(opts->name), len),	\
1583 			"%s", page);					\
1584 									\
1585 end:									\
1586 	mutex_unlock(&opts->lock);					\
1587 	return ret;							\
1588 }									\
1589 									\
1590 CONFIGFS_ATTR(f_uac1_opts_, name)
1591 
1592 UAC1_ATTRIBUTE(u32, c_chmask);
1593 UAC1_RATE_ATTRIBUTE(c_srate);
1594 UAC1_ATTRIBUTE(u32, c_ssize);
1595 UAC1_ATTRIBUTE(u32, p_chmask);
1596 UAC1_RATE_ATTRIBUTE(p_srate);
1597 UAC1_ATTRIBUTE(u32, p_ssize);
1598 UAC1_ATTRIBUTE(u32, req_number);
1599 
1600 UAC1_ATTRIBUTE(bool, p_mute_present);
1601 UAC1_ATTRIBUTE(bool, p_volume_present);
1602 UAC1_ATTRIBUTE(s16, p_volume_min);
1603 UAC1_ATTRIBUTE(s16, p_volume_max);
1604 UAC1_ATTRIBUTE(s16, p_volume_res);
1605 
1606 UAC1_ATTRIBUTE(bool, c_mute_present);
1607 UAC1_ATTRIBUTE(bool, c_volume_present);
1608 UAC1_ATTRIBUTE(s16, c_volume_min);
1609 UAC1_ATTRIBUTE(s16, c_volume_max);
1610 UAC1_ATTRIBUTE(s16, c_volume_res);
1611 UAC1_ATTRIBUTE_STRING(function_name);
1612 
1613 static struct configfs_attribute *f_uac1_attrs[] = {
1614 	&f_uac1_opts_attr_c_chmask,
1615 	&f_uac1_opts_attr_c_srate,
1616 	&f_uac1_opts_attr_c_ssize,
1617 	&f_uac1_opts_attr_p_chmask,
1618 	&f_uac1_opts_attr_p_srate,
1619 	&f_uac1_opts_attr_p_ssize,
1620 	&f_uac1_opts_attr_req_number,
1621 
1622 	&f_uac1_opts_attr_p_mute_present,
1623 	&f_uac1_opts_attr_p_volume_present,
1624 	&f_uac1_opts_attr_p_volume_min,
1625 	&f_uac1_opts_attr_p_volume_max,
1626 	&f_uac1_opts_attr_p_volume_res,
1627 
1628 	&f_uac1_opts_attr_c_mute_present,
1629 	&f_uac1_opts_attr_c_volume_present,
1630 	&f_uac1_opts_attr_c_volume_min,
1631 	&f_uac1_opts_attr_c_volume_max,
1632 	&f_uac1_opts_attr_c_volume_res,
1633 
1634 	&f_uac1_opts_attr_function_name,
1635 
1636 	NULL,
1637 };
1638 
1639 static const struct config_item_type f_uac1_func_type = {
1640 	.ct_item_ops	= &f_uac1_item_ops,
1641 	.ct_attrs	= f_uac1_attrs,
1642 	.ct_owner	= THIS_MODULE,
1643 };
1644 
1645 static void f_audio_free_inst(struct usb_function_instance *f)
1646 {
1647 	struct f_uac1_opts *opts;
1648 
1649 	opts = container_of(f, struct f_uac1_opts, func_inst);
1650 	kfree(opts);
1651 }
1652 
1653 static struct usb_function_instance *f_audio_alloc_inst(void)
1654 {
1655 	struct f_uac1_opts *opts;
1656 
1657 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1658 	if (!opts)
1659 		return ERR_PTR(-ENOMEM);
1660 
1661 	mutex_init(&opts->lock);
1662 	opts->func_inst.free_func_inst = f_audio_free_inst;
1663 
1664 	config_group_init_type_name(&opts->func_inst.group, "",
1665 				    &f_uac1_func_type);
1666 
1667 	opts->c_chmask = UAC1_DEF_CCHMASK;
1668 	opts->c_srates[0] = UAC1_DEF_CSRATE;
1669 	opts->c_ssize = UAC1_DEF_CSSIZE;
1670 	opts->p_chmask = UAC1_DEF_PCHMASK;
1671 	opts->p_srates[0] = UAC1_DEF_PSRATE;
1672 	opts->p_ssize = UAC1_DEF_PSSIZE;
1673 
1674 	opts->p_mute_present = UAC1_DEF_MUTE_PRESENT;
1675 	opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT;
1676 	opts->p_volume_min = UAC1_DEF_MIN_DB;
1677 	opts->p_volume_max = UAC1_DEF_MAX_DB;
1678 	opts->p_volume_res = UAC1_DEF_RES_DB;
1679 
1680 	opts->c_mute_present = UAC1_DEF_MUTE_PRESENT;
1681 	opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT;
1682 	opts->c_volume_min = UAC1_DEF_MIN_DB;
1683 	opts->c_volume_max = UAC1_DEF_MAX_DB;
1684 	opts->c_volume_res = UAC1_DEF_RES_DB;
1685 
1686 	opts->req_number = UAC1_DEF_REQ_NUM;
1687 
1688 	snprintf(opts->function_name, sizeof(opts->function_name), "AC Interface");
1689 
1690 	return &opts->func_inst;
1691 }
1692 
1693 static void f_audio_free(struct usb_function *f)
1694 {
1695 	struct g_audio *audio;
1696 	struct f_uac1_opts *opts;
1697 
1698 	audio = func_to_g_audio(f);
1699 	opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1700 	kfree(audio);
1701 	mutex_lock(&opts->lock);
1702 	--opts->refcnt;
1703 	mutex_unlock(&opts->lock);
1704 }
1705 
1706 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1707 {
1708 	struct g_audio *audio = func_to_g_audio(f);
1709 
1710 	g_audio_cleanup(audio);
1711 	usb_free_all_descriptors(f);
1712 
1713 	kfree(out_feature_unit_desc);
1714 	out_feature_unit_desc = NULL;
1715 	kfree(in_feature_unit_desc);
1716 	in_feature_unit_desc = NULL;
1717 
1718 	kfree(ac_header_desc);
1719 	ac_header_desc = NULL;
1720 
1721 	audio->gadget = NULL;
1722 }
1723 
1724 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
1725 {
1726 	struct f_uac1 *uac1;
1727 	struct f_uac1_opts *opts;
1728 
1729 	/* allocate and initialize one new instance */
1730 	uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
1731 	if (!uac1)
1732 		return ERR_PTR(-ENOMEM);
1733 
1734 	opts = container_of(fi, struct f_uac1_opts, func_inst);
1735 	mutex_lock(&opts->lock);
1736 	++opts->refcnt;
1737 	mutex_unlock(&opts->lock);
1738 
1739 	uac1->g_audio.func.name = "uac1_func";
1740 	uac1->g_audio.func.bind = f_audio_bind;
1741 	uac1->g_audio.func.unbind = f_audio_unbind;
1742 	uac1->g_audio.func.set_alt = f_audio_set_alt;
1743 	uac1->g_audio.func.get_alt = f_audio_get_alt;
1744 	uac1->g_audio.func.setup = f_audio_setup;
1745 	uac1->g_audio.func.disable = f_audio_disable;
1746 	uac1->g_audio.func.suspend = f_audio_suspend;
1747 	uac1->g_audio.func.free_func = f_audio_free;
1748 
1749 	return &uac1->g_audio.func;
1750 }
1751 
1752 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
1753 MODULE_LICENSE("GPL");
1754 MODULE_AUTHOR("Ruslan Bilovol");
1755