xref: /linux/drivers/usb/gadget/function/f_uac1.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 /* Standard ISO OUT Endpoint Descriptor */
296 static struct usb_endpoint_descriptor ss_as_out_ep_desc  = {
297 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
298 	.bDescriptorType =	USB_DT_ENDPOINT,
299 	.bEndpointAddress =	USB_DIR_OUT,
300 	.bmAttributes =		USB_ENDPOINT_SYNC_ADAPTIVE
301 				| USB_ENDPOINT_XFER_ISOC,
302 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
303 	.bInterval =		4,
304 };
305 
306 static struct usb_ss_ep_comp_descriptor ss_as_out_ep_desc_comp = {
307 	.bLength		= sizeof(ss_as_out_ep_desc_comp),
308 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
309 	.bMaxBurst		= 0,
310 	.bmAttributes		= 0,
311 	/* wBytesPerInterval = DYNAMIC */
312 };
313 
314 /* Standard ISO OUT Endpoint Descriptor */
315 static struct usb_endpoint_descriptor ss_as_in_ep_desc  = {
316 	.bLength =		USB_DT_ENDPOINT_AUDIO_SIZE,
317 	.bDescriptorType =	USB_DT_ENDPOINT,
318 	.bEndpointAddress =	USB_DIR_IN,
319 	.bmAttributes =		USB_ENDPOINT_SYNC_ASYNC
320 				| USB_ENDPOINT_XFER_ISOC,
321 	.wMaxPacketSize	=	cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
322 	.bInterval =		4,
323 };
324 
325 static struct usb_ss_ep_comp_descriptor ss_as_in_ep_desc_comp = {
326 	.bLength		= sizeof(ss_as_in_ep_desc_comp),
327 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
328 	.bMaxBurst		= 0,
329 	.bmAttributes		= 0,
330 	/* wBytesPerInterval = DYNAMIC */
331 };
332 
333 static struct usb_descriptor_header *f_audio_ss_desc[] = {
334 	(struct usb_descriptor_header *)&ac_interface_desc,
335 	(struct usb_descriptor_header *)&ac_header_desc,
336 
337 	(struct usb_descriptor_header *)&usb_out_it_desc,
338 	(struct usb_descriptor_header *)&io_out_ot_desc,
339 	(struct usb_descriptor_header *)&io_in_it_desc,
340 	(struct usb_descriptor_header *)&usb_in_ot_desc,
341 
342 	(struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
343 	(struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
344 	(struct usb_descriptor_header *)&as_out_header_desc,
345 
346 	(struct usb_descriptor_header *)&as_out_type_i_desc,
347 
348 	//(struct usb_descriptor_header *)&as_out_ep_desc,
349 	(struct usb_descriptor_header *)&ss_as_out_ep_desc,
350 	(struct usb_descriptor_header *)&ss_as_out_ep_desc_comp,
351 	(struct usb_descriptor_header *)&as_iso_out_desc,
352 
353 	(struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
354 	(struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
355 	(struct usb_descriptor_header *)&as_in_header_desc,
356 
357 	(struct usb_descriptor_header *)&as_in_type_i_desc,
358 
359 	//(struct usb_descriptor_header *)&as_in_ep_desc,
360 	(struct usb_descriptor_header *)&ss_as_in_ep_desc,
361 	(struct usb_descriptor_header *)&ss_as_in_ep_desc_comp,
362 	(struct usb_descriptor_header *)&as_iso_in_desc,
363 	NULL,
364 };
365 
366 enum {
367 	STR_AC_IF,
368 	STR_USB_OUT_IT,
369 	STR_USB_OUT_IT_CH_NAMES,
370 	STR_IO_OUT_OT,
371 	STR_IO_IN_IT,
372 	STR_IO_IN_IT_CH_NAMES,
373 	STR_USB_IN_OT,
374 	STR_FU_IN,
375 	STR_FU_OUT,
376 	STR_AS_OUT_IF_ALT0,
377 	STR_AS_OUT_IF_ALT1,
378 	STR_AS_IN_IF_ALT0,
379 	STR_AS_IN_IF_ALT1,
380 	NUM_STR_DESCRIPTORS,
381 };
382 
383 static struct usb_string strings_uac1[NUM_STR_DESCRIPTORS + 1] = {};
384 
385 static struct usb_gadget_strings str_uac1 = {
386 	.language = 0x0409,	/* en-us */
387 	.strings = strings_uac1,
388 };
389 
390 static struct usb_gadget_strings *uac1_strings[] = {
391 	&str_uac1,
392 	NULL,
393 };
394 
395 /*
396  * This function is an ALSA sound card following USB Audio Class Spec 1.0.
397  */
398 
399 static void uac_cs_attr_sample_rate(struct usb_ep *ep, struct usb_request *req)
400 {
401 	struct usb_function *fn = ep->driver_data;
402 	struct usb_composite_dev *cdev = fn->config->cdev;
403 	struct g_audio *agdev = func_to_g_audio(fn);
404 	struct f_uac1 *uac1 = func_to_uac1(fn);
405 	u8 *buf = (u8 *)req->buf;
406 	u32 val = 0;
407 
408 	if (req->actual != 3) {
409 		WARN(cdev, "Invalid data size for UAC_EP_CS_ATTR_SAMPLE_RATE.\n");
410 		return;
411 	}
412 
413 	val = buf[0] | (buf[1] << 8) | (buf[2] << 16);
414 	if (uac1->ctl_id == (USB_DIR_IN | 2)) {
415 		uac1->p_srate = val;
416 		u_audio_set_playback_srate(agdev, uac1->p_srate);
417 	} else if (uac1->ctl_id == (USB_DIR_OUT | 1)) {
418 		uac1->c_srate = val;
419 		u_audio_set_capture_srate(agdev, uac1->c_srate);
420 	}
421 }
422 
423 static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req)
424 {
425 	struct g_audio *audio = req->context;
426 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
427 
428 	atomic_dec(&uac1->int_count);
429 	kfree(req->buf);
430 	usb_ep_free_request(_ep, req);
431 }
432 
433 static int audio_notify(struct g_audio *audio, int unit_id, int cs)
434 {
435 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
436 	struct usb_request *req;
437 	struct uac1_status_word *msg;
438 	int ret;
439 
440 	if (!uac1->int_ep->enabled)
441 		return 0;
442 
443 	if (atomic_inc_return(&uac1->int_count) > UAC1_DEF_INT_REQ_NUM) {
444 		atomic_dec(&uac1->int_count);
445 		return 0;
446 	}
447 
448 	req = usb_ep_alloc_request(uac1->int_ep, GFP_ATOMIC);
449 	if (req == NULL) {
450 		ret = -ENOMEM;
451 		goto err_dec_int_count;
452 	}
453 
454 	msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
455 	if (msg == NULL) {
456 		ret = -ENOMEM;
457 		goto err_free_request;
458 	}
459 
460 	msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING
461 				| UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF;
462 	msg->bOriginator = unit_id;
463 
464 	req->length = sizeof(*msg);
465 	req->buf = msg;
466 	req->context = audio;
467 	req->complete = audio_notify_complete;
468 
469 	ret = usb_ep_queue(uac1->int_ep, req, GFP_ATOMIC);
470 
471 	if (ret)
472 		goto err_free_msg;
473 
474 	return 0;
475 
476 err_free_msg:
477 	kfree(msg);
478 err_free_request:
479 	usb_ep_free_request(uac1->int_ep, req);
480 err_dec_int_count:
481 	atomic_dec(&uac1->int_count);
482 
483 	return ret;
484 }
485 
486 static int
487 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
488 {
489 	struct usb_request *req = fn->config->cdev->req;
490 	struct g_audio *audio = func_to_g_audio(fn);
491 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
492 	u16 w_length = le16_to_cpu(cr->wLength);
493 	u16 w_index = le16_to_cpu(cr->wIndex);
494 	u16 w_value = le16_to_cpu(cr->wValue);
495 	u8 entity_id = (w_index >> 8) & 0xff;
496 	u8 control_selector = w_value >> 8;
497 	int value = -EOPNOTSUPP;
498 
499 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
500 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
501 		unsigned int is_playback = 0;
502 
503 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
504 			is_playback = 1;
505 
506 		if (control_selector == UAC_FU_MUTE) {
507 			unsigned int mute;
508 
509 			u_audio_get_mute(audio, is_playback, &mute);
510 
511 			*(u8 *)req->buf = mute;
512 			value = min_t(unsigned int, w_length, 1);
513 		} else if (control_selector == UAC_FU_VOLUME) {
514 			__le16 c;
515 			s16 volume;
516 
517 			u_audio_get_volume(audio, is_playback, &volume);
518 
519 			c = cpu_to_le16(volume);
520 
521 			value = min_t(unsigned int, w_length, sizeof(c));
522 			memcpy(req->buf, &c, value);
523 		} else {
524 			dev_err(&audio->gadget->dev,
525 				"%s:%d control_selector=%d TODO!\n",
526 				__func__, __LINE__, control_selector);
527 		}
528 	} else {
529 		dev_err(&audio->gadget->dev,
530 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
531 			__func__, __LINE__, entity_id, control_selector);
532 	}
533 
534 	return value;
535 }
536 
537 static int
538 in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr)
539 {
540 	struct usb_request *req = fn->config->cdev->req;
541 	struct g_audio *audio = func_to_g_audio(fn);
542 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
543 	u16 w_length = le16_to_cpu(cr->wLength);
544 	u16 w_index = le16_to_cpu(cr->wIndex);
545 	u16 w_value = le16_to_cpu(cr->wValue);
546 	u8 entity_id = (w_index >> 8) & 0xff;
547 	u8 control_selector = w_value >> 8;
548 	int value = -EOPNOTSUPP;
549 
550 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
551 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
552 		unsigned int is_playback = 0;
553 
554 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
555 			is_playback = 1;
556 
557 		if (control_selector == UAC_FU_VOLUME) {
558 			__le16 r;
559 			s16 min_db;
560 
561 			if (is_playback)
562 				min_db = opts->p_volume_min;
563 			else
564 				min_db = opts->c_volume_min;
565 
566 			r = cpu_to_le16(min_db);
567 
568 			value = min_t(unsigned int, w_length, sizeof(r));
569 			memcpy(req->buf, &r, value);
570 		} else {
571 			dev_err(&audio->gadget->dev,
572 				"%s:%d control_selector=%d TODO!\n",
573 				__func__, __LINE__, control_selector);
574 		}
575 	} else {
576 		dev_err(&audio->gadget->dev,
577 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
578 			__func__, __LINE__, entity_id, control_selector);
579 	}
580 
581 	return value;
582 }
583 
584 static int
585 in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr)
586 {
587 	struct usb_request *req = fn->config->cdev->req;
588 	struct g_audio *audio = func_to_g_audio(fn);
589 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
590 	u16 w_length = le16_to_cpu(cr->wLength);
591 	u16 w_index = le16_to_cpu(cr->wIndex);
592 	u16 w_value = le16_to_cpu(cr->wValue);
593 	u8 entity_id = (w_index >> 8) & 0xff;
594 	u8 control_selector = w_value >> 8;
595 	int value = -EOPNOTSUPP;
596 
597 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
598 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
599 		unsigned int is_playback = 0;
600 
601 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
602 			is_playback = 1;
603 
604 		if (control_selector == UAC_FU_VOLUME) {
605 			__le16 r;
606 			s16 max_db;
607 
608 			if (is_playback)
609 				max_db = opts->p_volume_max;
610 			else
611 				max_db = opts->c_volume_max;
612 
613 			r = cpu_to_le16(max_db);
614 
615 			value = min_t(unsigned int, w_length, sizeof(r));
616 			memcpy(req->buf, &r, value);
617 		} else {
618 			dev_err(&audio->gadget->dev,
619 				"%s:%d control_selector=%d TODO!\n",
620 				__func__, __LINE__, control_selector);
621 		}
622 	} else {
623 		dev_err(&audio->gadget->dev,
624 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
625 			__func__, __LINE__, entity_id, control_selector);
626 	}
627 
628 	return value;
629 }
630 
631 static int
632 in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr)
633 {
634 	struct usb_request *req = fn->config->cdev->req;
635 	struct g_audio *audio = func_to_g_audio(fn);
636 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
637 	u16 w_length = le16_to_cpu(cr->wLength);
638 	u16 w_index = le16_to_cpu(cr->wIndex);
639 	u16 w_value = le16_to_cpu(cr->wValue);
640 	u8 entity_id = (w_index >> 8) & 0xff;
641 	u8 control_selector = w_value >> 8;
642 	int value = -EOPNOTSUPP;
643 
644 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
645 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
646 		unsigned int is_playback = 0;
647 
648 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
649 			is_playback = 1;
650 
651 		if (control_selector == UAC_FU_VOLUME) {
652 			__le16 r;
653 			s16 res_db;
654 
655 			if (is_playback)
656 				res_db = opts->p_volume_res;
657 			else
658 				res_db = opts->c_volume_res;
659 
660 			r = cpu_to_le16(res_db);
661 
662 			value = min_t(unsigned int, w_length, sizeof(r));
663 			memcpy(req->buf, &r, value);
664 		} else {
665 			dev_err(&audio->gadget->dev,
666 				"%s:%d control_selector=%d TODO!\n",
667 				__func__, __LINE__, control_selector);
668 		}
669 	} else {
670 		dev_err(&audio->gadget->dev,
671 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
672 			__func__, __LINE__, entity_id, control_selector);
673 	}
674 
675 	return value;
676 }
677 
678 static void
679 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
680 {
681 	struct g_audio *audio = req->context;
682 	struct usb_composite_dev *cdev = audio->func.config->cdev;
683 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
684 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
685 	struct usb_ctrlrequest *cr = &uac1->setup_cr;
686 	u16 w_index = le16_to_cpu(cr->wIndex);
687 	u16 w_value = le16_to_cpu(cr->wValue);
688 	u8 entity_id = (w_index >> 8) & 0xff;
689 	u8 control_selector = w_value >> 8;
690 
691 	if (req->status != 0) {
692 		dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
693 		return;
694 	}
695 
696 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
697 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
698 		unsigned int is_playback = 0;
699 
700 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
701 			is_playback = 1;
702 
703 		if (control_selector == UAC_FU_MUTE) {
704 			u8 mute = *(u8 *)req->buf;
705 
706 			u_audio_set_mute(audio, is_playback, mute);
707 
708 			return;
709 		} else if (control_selector == UAC_FU_VOLUME) {
710 			__le16 *c = req->buf;
711 			s16 volume;
712 
713 			volume = le16_to_cpu(*c);
714 			u_audio_set_volume(audio, is_playback, volume);
715 
716 			return;
717 		} else {
718 			dev_err(&audio->gadget->dev,
719 				"%s:%d control_selector=%d TODO!\n",
720 				__func__, __LINE__, control_selector);
721 			usb_ep_set_halt(ep);
722 		}
723 	} else {
724 		dev_err(&audio->gadget->dev,
725 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
726 			__func__, __LINE__, entity_id, control_selector);
727 		usb_ep_set_halt(ep);
728 
729 	}
730 }
731 
732 static int
733 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
734 {
735 	struct usb_request *req = fn->config->cdev->req;
736 	struct g_audio *audio = func_to_g_audio(fn);
737 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
738 	struct f_uac1 *uac1 = func_to_uac1(&audio->func);
739 	u16 w_length = le16_to_cpu(cr->wLength);
740 	u16 w_index = le16_to_cpu(cr->wIndex);
741 	u16 w_value = le16_to_cpu(cr->wValue);
742 	u8 entity_id = (w_index >> 8) & 0xff;
743 	u8 control_selector = w_value >> 8;
744 
745 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
746 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
747 		memcpy(&uac1->setup_cr, cr, sizeof(*cr));
748 		req->context = audio;
749 		req->complete = out_rq_cur_complete;
750 
751 		return w_length;
752 	} else {
753 		dev_err(&audio->gadget->dev,
754 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
755 			__func__, __LINE__, entity_id, control_selector);
756 	}
757 	return -EOPNOTSUPP;
758 }
759 
760 static int ac_rq_in(struct usb_function *f,
761 		const struct usb_ctrlrequest *ctrl)
762 {
763 	struct usb_composite_dev *cdev = f->config->cdev;
764 	int value = -EOPNOTSUPP;
765 	u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
766 	u16 len = le16_to_cpu(ctrl->wLength);
767 	u16 w_value = le16_to_cpu(ctrl->wValue);
768 
769 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
770 			ctrl->bRequest, w_value, len, ep);
771 
772 	switch (ctrl->bRequest) {
773 	case UAC_GET_CUR:
774 		return in_rq_cur(f, ctrl);
775 	case UAC_GET_MIN:
776 		return in_rq_min(f, ctrl);
777 	case UAC_GET_MAX:
778 		return in_rq_max(f, ctrl);
779 	case UAC_GET_RES:
780 		return in_rq_res(f, ctrl);
781 	case UAC_GET_MEM:
782 		break;
783 	case UAC_GET_STAT:
784 		value = len;
785 		break;
786 	default:
787 		break;
788 	}
789 
790 	return value;
791 }
792 
793 static int audio_set_endpoint_req(struct usb_function *f,
794 		const struct usb_ctrlrequest *ctrl)
795 {
796 	struct usb_composite_dev *cdev = f->config->cdev;
797 	struct usb_request	*req = f->config->cdev->req;
798 	struct f_uac1		*uac1 = func_to_uac1(f);
799 	int			value = -EOPNOTSUPP;
800 	u16			ep = le16_to_cpu(ctrl->wIndex);
801 	u16			len = le16_to_cpu(ctrl->wLength);
802 	u16			w_value = le16_to_cpu(ctrl->wValue);
803 	u8			cs = w_value >> 8;
804 
805 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
806 			ctrl->bRequest, w_value, len, ep);
807 
808 	switch (ctrl->bRequest) {
809 	case UAC_SET_CUR: {
810 		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
811 			cdev->gadget->ep0->driver_data = f;
812 			uac1->ctl_id = ep;
813 			req->complete = uac_cs_attr_sample_rate;
814 		}
815 		value = len;
816 		break;
817 	}
818 
819 	case UAC_SET_MIN:
820 		break;
821 
822 	case UAC_SET_MAX:
823 		break;
824 
825 	case UAC_SET_RES:
826 		break;
827 
828 	case UAC_SET_MEM:
829 		break;
830 
831 	default:
832 		break;
833 	}
834 
835 	return value;
836 }
837 
838 static int audio_get_endpoint_req(struct usb_function *f,
839 		const struct usb_ctrlrequest *ctrl)
840 {
841 	struct usb_composite_dev *cdev = f->config->cdev;
842 	struct usb_request *req = f->config->cdev->req;
843 	struct f_uac1 *uac1 = func_to_uac1(f);
844 	u8 *buf = (u8 *)req->buf;
845 	int value = -EOPNOTSUPP;
846 	u8 ep = le16_to_cpu(ctrl->wIndex);
847 	u16 len = le16_to_cpu(ctrl->wLength);
848 	u16 w_value = le16_to_cpu(ctrl->wValue);
849 	u8 cs = w_value >> 8;
850 	u32 val = 0;
851 
852 	DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
853 			ctrl->bRequest, w_value, len, ep);
854 
855 	switch (ctrl->bRequest) {
856 	case UAC_GET_CUR: {
857 		if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
858 			if (ep == (USB_DIR_IN | 2))
859 				val = uac1->p_srate;
860 			else if (ep == (USB_DIR_OUT | 1))
861 				val = uac1->c_srate;
862 			buf[2] = (val >> 16) & 0xff;
863 			buf[1] = (val >> 8) & 0xff;
864 			buf[0] = val & 0xff;
865 		}
866 		value = len;
867 		break;
868 	}
869 	case UAC_GET_MIN:
870 	case UAC_GET_MAX:
871 	case UAC_GET_RES:
872 		value = len;
873 		break;
874 	case UAC_GET_MEM:
875 		break;
876 	default:
877 		break;
878 	}
879 
880 	return value;
881 }
882 
883 static int
884 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
885 {
886 	struct usb_composite_dev *cdev = f->config->cdev;
887 	struct usb_request	*req = cdev->req;
888 	int			value = -EOPNOTSUPP;
889 	u16			w_index = le16_to_cpu(ctrl->wIndex);
890 	u16			w_value = le16_to_cpu(ctrl->wValue);
891 	u16			w_length = le16_to_cpu(ctrl->wLength);
892 
893 	/* composite driver infrastructure handles everything; interface
894 	 * activation uses set_alt().
895 	 */
896 	switch (ctrl->bRequestType) {
897 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
898 		value = audio_set_endpoint_req(f, ctrl);
899 		break;
900 
901 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
902 		value = audio_get_endpoint_req(f, ctrl);
903 		break;
904 	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
905 		if (ctrl->bRequest == UAC_SET_CUR)
906 			value = out_rq_cur(f, ctrl);
907 		break;
908 	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
909 		value = ac_rq_in(f, ctrl);
910 		break;
911 	default:
912 		ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
913 			ctrl->bRequestType, ctrl->bRequest,
914 			w_value, w_index, w_length);
915 	}
916 
917 	/* respond with data transfer or status phase? */
918 	if (value >= 0) {
919 		DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
920 			ctrl->bRequestType, ctrl->bRequest,
921 			w_value, w_index, w_length);
922 		req->zero = 0;
923 		req->length = value;
924 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
925 		if (value < 0)
926 			ERROR(cdev, "audio response on err %d\n", value);
927 	}
928 
929 	/* device either stalls (value < 0) or reports success */
930 	return value;
931 }
932 
933 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
934 {
935 	struct usb_composite_dev *cdev = f->config->cdev;
936 	struct usb_gadget *gadget = cdev->gadget;
937 	struct device *dev = &gadget->dev;
938 	struct g_audio *audio = func_to_g_audio(f);
939 	struct f_uac1 *uac1 = func_to_uac1(f);
940 	int ret = 0;
941 
942 	/* No i/f has more than 2 alt settings */
943 	if (alt > 1) {
944 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
945 		return -EINVAL;
946 	}
947 
948 	if (intf == uac1->ac_intf) {
949 		/* Control I/f has only 1 AltSetting - 0 */
950 		if (alt) {
951 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
952 			return -EINVAL;
953 		}
954 
955 		/* restart interrupt endpoint */
956 		if (uac1->int_ep) {
957 			usb_ep_disable(uac1->int_ep);
958 			config_ep_by_speed(gadget, &audio->func, uac1->int_ep);
959 			usb_ep_enable(uac1->int_ep);
960 		}
961 
962 		return 0;
963 	}
964 
965 	if (intf == uac1->as_out_intf) {
966 		uac1->as_out_alt = alt;
967 
968 		if (alt)
969 			ret = u_audio_start_capture(&uac1->g_audio);
970 		else
971 			u_audio_stop_capture(&uac1->g_audio);
972 	} else if (intf == uac1->as_in_intf) {
973 		uac1->as_in_alt = alt;
974 
975 		if (alt)
976 			ret = u_audio_start_playback(&uac1->g_audio);
977 		else
978 			u_audio_stop_playback(&uac1->g_audio);
979 	} else {
980 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
981 		return -EINVAL;
982 	}
983 
984 	return ret;
985 }
986 
987 static int f_audio_get_alt(struct usb_function *f, unsigned intf)
988 {
989 	struct usb_composite_dev *cdev = f->config->cdev;
990 	struct usb_gadget *gadget = cdev->gadget;
991 	struct device *dev = &gadget->dev;
992 	struct f_uac1 *uac1 = func_to_uac1(f);
993 
994 	if (intf == uac1->ac_intf)
995 		return uac1->ac_alt;
996 	else if (intf == uac1->as_out_intf)
997 		return uac1->as_out_alt;
998 	else if (intf == uac1->as_in_intf)
999 		return uac1->as_in_alt;
1000 	else
1001 		dev_err(dev, "%s:%d Invalid Interface %d!\n",
1002 			__func__, __LINE__, intf);
1003 
1004 	return -EINVAL;
1005 }
1006 
1007 
1008 static void f_audio_disable(struct usb_function *f)
1009 {
1010 	struct f_uac1 *uac1 = func_to_uac1(f);
1011 
1012 	uac1->as_out_alt = 0;
1013 	uac1->as_in_alt = 0;
1014 
1015 	u_audio_stop_playback(&uac1->g_audio);
1016 	u_audio_stop_capture(&uac1->g_audio);
1017 	if (uac1->int_ep)
1018 		usb_ep_disable(uac1->int_ep);
1019 }
1020 
1021 static void
1022 f_audio_suspend(struct usb_function *f)
1023 {
1024 	struct f_uac1 *uac1 = func_to_uac1(f);
1025 
1026 	u_audio_suspend(&uac1->g_audio);
1027 }
1028 
1029 /*-------------------------------------------------------------------------*/
1030 static struct uac_feature_unit_descriptor *build_fu_desc(int chmask)
1031 {
1032 	struct uac_feature_unit_descriptor *fu_desc;
1033 	int channels = num_channels(chmask);
1034 	int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels);
1035 
1036 	fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
1037 	if (!fu_desc)
1038 		return NULL;
1039 
1040 	fu_desc->bLength = fu_desc_size;
1041 	fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1042 
1043 	fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
1044 	fu_desc->bControlSize  = 2;
1045 
1046 	/* bUnitID, bSourceID and bmaControls will be defined later */
1047 
1048 	return fu_desc;
1049 }
1050 
1051 /* B.3.2  Class-Specific AC Interface Descriptor */
1052 static struct
1053 uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts)
1054 {
1055 	struct uac1_ac_header_descriptor *ac_desc;
1056 	int ac_header_desc_size;
1057 	int num_ifaces = 0;
1058 
1059 	if (EPOUT_EN(opts))
1060 		num_ifaces++;
1061 	if (EPIN_EN(opts))
1062 		num_ifaces++;
1063 
1064 	ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces);
1065 
1066 	ac_desc = kzalloc(ac_header_desc_size, GFP_KERNEL);
1067 	if (!ac_desc)
1068 		return NULL;
1069 
1070 	ac_desc->bLength = ac_header_desc_size;
1071 	ac_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1072 	ac_desc->bDescriptorSubtype = UAC_HEADER;
1073 	ac_desc->bcdADC = cpu_to_le16(0x0100);
1074 	ac_desc->bInCollection = num_ifaces;
1075 
1076 	/* wTotalLength and baInterfaceNr will be defined later */
1077 
1078 	return ac_desc;
1079 }
1080 
1081 /* Use macro to overcome line length limitation */
1082 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
1083 
1084 static void setup_descriptor(struct f_uac1_opts *opts)
1085 {
1086 	/* patch descriptors */
1087 	int i = 1; /* ID's start with 1 */
1088 
1089 	if (EPOUT_EN(opts))
1090 		usb_out_it_desc.bTerminalID = i++;
1091 	if (EPIN_EN(opts))
1092 		io_in_it_desc.bTerminalID = i++;
1093 	if (EPOUT_EN(opts))
1094 		io_out_ot_desc.bTerminalID = i++;
1095 	if (EPIN_EN(opts))
1096 		usb_in_ot_desc.bTerminalID = i++;
1097 	if (FUOUT_EN(opts))
1098 		out_feature_unit_desc->bUnitID = i++;
1099 	if (FUIN_EN(opts))
1100 		in_feature_unit_desc->bUnitID = i++;
1101 
1102 	if (FUIN_EN(opts)) {
1103 		usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
1104 		in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
1105 	} else {
1106 		usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
1107 	}
1108 	if (FUOUT_EN(opts)) {
1109 		io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
1110 		out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
1111 	} else {
1112 		io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
1113 	}
1114 
1115 	as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
1116 	as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
1117 
1118 	ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength);
1119 
1120 	if (EPIN_EN(opts)) {
1121 		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1122 
1123 		len += sizeof(usb_in_ot_desc);
1124 		len += sizeof(io_in_it_desc);
1125 		if (FUIN_EN(opts))
1126 			len += in_feature_unit_desc->bLength;
1127 		ac_header_desc->wTotalLength = cpu_to_le16(len);
1128 	}
1129 	if (EPOUT_EN(opts)) {
1130 		u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1131 
1132 		len += sizeof(usb_out_it_desc);
1133 		len += sizeof(io_out_ot_desc);
1134 		if (FUOUT_EN(opts))
1135 			len += out_feature_unit_desc->bLength;
1136 		ac_header_desc->wTotalLength = cpu_to_le16(len);
1137 	}
1138 
1139 	i = 0;
1140 	f_audio_desc[i++] = USBDHDR(&ac_interface_desc);
1141 	f_audio_desc[i++] = USBDHDR(ac_header_desc);
1142 
1143 	if (EPOUT_EN(opts)) {
1144 		f_audio_desc[i++] = USBDHDR(&usb_out_it_desc);
1145 		f_audio_desc[i++] = USBDHDR(&io_out_ot_desc);
1146 		if (FUOUT_EN(opts))
1147 			f_audio_desc[i++] = USBDHDR(out_feature_unit_desc);
1148 	}
1149 
1150 	if (EPIN_EN(opts)) {
1151 		f_audio_desc[i++] = USBDHDR(&io_in_it_desc);
1152 		f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc);
1153 		if (FUIN_EN(opts))
1154 			f_audio_desc[i++] = USBDHDR(in_feature_unit_desc);
1155 	}
1156 
1157 	if (FUOUT_EN(opts) || FUIN_EN(opts))
1158 		f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc);
1159 
1160 	if (EPOUT_EN(opts)) {
1161 		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc);
1162 		f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc);
1163 		f_audio_desc[i++] = USBDHDR(&as_out_header_desc);
1164 		f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc);
1165 		f_audio_desc[i++] = USBDHDR(&as_out_ep_desc);
1166 		f_audio_desc[i++] = USBDHDR(&as_iso_out_desc);
1167 	}
1168 	if (EPIN_EN(opts)) {
1169 		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc);
1170 		f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc);
1171 		f_audio_desc[i++] = USBDHDR(&as_in_header_desc);
1172 		f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc);
1173 		f_audio_desc[i++] = USBDHDR(&as_in_ep_desc);
1174 		f_audio_desc[i++] = USBDHDR(&as_iso_in_desc);
1175 	}
1176 	f_audio_desc[i] = NULL;
1177 }
1178 
1179 static int f_audio_validate_opts(struct g_audio *audio, struct device *dev)
1180 {
1181 	struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
1182 
1183 	if (!opts->p_chmask && !opts->c_chmask) {
1184 		dev_err(dev, "Error: no playback and capture channels\n");
1185 		return -EINVAL;
1186 	} else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) {
1187 		dev_err(dev, "Error: unsupported playback channels mask\n");
1188 		return -EINVAL;
1189 	} else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) {
1190 		dev_err(dev, "Error: unsupported capture channels mask\n");
1191 		return -EINVAL;
1192 	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
1193 		dev_err(dev, "Error: incorrect playback sample size\n");
1194 		return -EINVAL;
1195 	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
1196 		dev_err(dev, "Error: incorrect capture sample size\n");
1197 		return -EINVAL;
1198 	} else if (!opts->p_srates[0]) {
1199 		dev_err(dev, "Error: incorrect playback sampling rate\n");
1200 		return -EINVAL;
1201 	} else if (!opts->c_srates[0]) {
1202 		dev_err(dev, "Error: incorrect capture sampling rate\n");
1203 		return -EINVAL;
1204 	}
1205 
1206 	if (opts->p_volume_max <= opts->p_volume_min) {
1207 		dev_err(dev, "Error: incorrect playback volume max/min\n");
1208 		return -EINVAL;
1209 	} else if (opts->c_volume_max <= opts->c_volume_min) {
1210 		dev_err(dev, "Error: incorrect capture volume max/min\n");
1211 		return -EINVAL;
1212 	} else if (opts->p_volume_res <= 0) {
1213 		dev_err(dev, "Error: negative/zero playback volume resolution\n");
1214 		return -EINVAL;
1215 	} else if (opts->c_volume_res <= 0) {
1216 		dev_err(dev, "Error: negative/zero capture volume resolution\n");
1217 		return -EINVAL;
1218 	}
1219 
1220 	if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
1221 		dev_err(dev, "Error: incorrect playback volume resolution\n");
1222 		return -EINVAL;
1223 	} else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
1224 		dev_err(dev, "Error: incorrect capture volume resolution\n");
1225 		return -EINVAL;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
1231 /* audio function driver setup/binding */
1232 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1233 {
1234 	struct usb_composite_dev	*cdev = c->cdev;
1235 	struct usb_gadget		*gadget = cdev->gadget;
1236 	struct device			*dev = &gadget->dev;
1237 	struct f_uac1			*uac1 = func_to_uac1(f);
1238 	struct g_audio			*audio = func_to_g_audio(f);
1239 	struct f_uac1_opts		*audio_opts;
1240 	struct usb_ep			*ep = NULL;
1241 	struct usb_string		*us;
1242 	int				ba_iface_id;
1243 	int				status;
1244 	int				idx, i;
1245 
1246 	status = f_audio_validate_opts(audio, dev);
1247 	if (status)
1248 		return status;
1249 
1250 	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1251 
1252 	strings_uac1[STR_AC_IF].s = audio_opts->function_name;
1253 
1254 	strings_uac1[STR_USB_OUT_IT].s = audio_opts->c_it_name;
1255 	strings_uac1[STR_USB_OUT_IT_CH_NAMES].s = audio_opts->c_it_ch_name;
1256 	strings_uac1[STR_IO_OUT_OT].s = audio_opts->c_ot_name;
1257 	strings_uac1[STR_FU_OUT].s = audio_opts->c_fu_vol_name;
1258 	strings_uac1[STR_AS_OUT_IF_ALT0].s = "Playback Inactive";
1259 	strings_uac1[STR_AS_OUT_IF_ALT1].s = "Playback Active";
1260 
1261 	strings_uac1[STR_IO_IN_IT].s = audio_opts->p_it_name;
1262 	strings_uac1[STR_IO_IN_IT_CH_NAMES].s = audio_opts->p_it_ch_name;
1263 	strings_uac1[STR_USB_IN_OT].s = audio_opts->p_ot_name;
1264 	strings_uac1[STR_FU_IN].s = audio_opts->p_fu_vol_name;
1265 	strings_uac1[STR_AS_IN_IF_ALT0].s = "Capture Inactive";
1266 	strings_uac1[STR_AS_IN_IF_ALT1].s = "Capture Active";
1267 
1268 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
1269 	if (IS_ERR(us))
1270 		return PTR_ERR(us);
1271 
1272 	ac_header_desc = build_ac_header_desc(audio_opts);
1273 	if (!ac_header_desc)
1274 		return -ENOMEM;
1275 
1276 	if (FUOUT_EN(audio_opts)) {
1277 		out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask);
1278 		if (!out_feature_unit_desc) {
1279 			status = -ENOMEM;
1280 			goto fail;
1281 		}
1282 	}
1283 	if (FUIN_EN(audio_opts)) {
1284 		in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask);
1285 		if (!in_feature_unit_desc) {
1286 			status = -ENOMEM;
1287 			goto err_free_fu;
1288 		}
1289 	}
1290 
1291 	ac_interface_desc.iInterface = us[STR_AC_IF].id;
1292 	usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
1293 	usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
1294 	io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
1295 	as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
1296 	as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
1297 	io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
1298 	io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
1299 	usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
1300 	as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
1301 	as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
1302 
1303 	if (FUOUT_EN(audio_opts)) {
1304 		u8 *i_feature;
1305 
1306 		i_feature = (u8 *)out_feature_unit_desc +
1307 					out_feature_unit_desc->bLength - 1;
1308 		*i_feature = us[STR_FU_OUT].id;
1309 	}
1310 	if (FUIN_EN(audio_opts)) {
1311 		u8 *i_feature;
1312 
1313 		i_feature = (u8 *)in_feature_unit_desc +
1314 					in_feature_unit_desc->bLength - 1;
1315 		*i_feature = us[STR_FU_IN].id;
1316 	}
1317 
1318 	/* Set channel numbers */
1319 	usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1320 	usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
1321 	as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
1322 	as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
1323 	as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
1324 	io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1325 	io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
1326 	as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
1327 	as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
1328 	as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
1329 
1330 	if (FUOUT_EN(audio_opts)) {
1331 		__le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0];
1332 		u32 control = 0;
1333 
1334 		if (audio_opts->c_mute_present)
1335 			control |= UAC_FU_MUTE;
1336 		if (audio_opts->c_volume_present)
1337 			control |= UAC_FU_VOLUME;
1338 		*bma = cpu_to_le16(control);
1339 	}
1340 	if (FUIN_EN(audio_opts)) {
1341 		__le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0];
1342 		u32 control = 0;
1343 
1344 		if (audio_opts->p_mute_present)
1345 			control |= UAC_FU_MUTE;
1346 		if (audio_opts->p_volume_present)
1347 			control |= UAC_FU_VOLUME;
1348 		*bma = cpu_to_le16(control);
1349 	}
1350 
1351 	/* Set sample rates */
1352 	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1353 		if (audio_opts->c_srates[i] == 0)
1354 			break;
1355 		memcpy(as_out_type_i_desc.tSamFreq[idx++],
1356 				&audio_opts->c_srates[i], 3);
1357 	}
1358 	as_out_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1359 	as_out_type_i_desc.bSamFreqType = idx;
1360 
1361 	for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1362 		if (audio_opts->p_srates[i] == 0)
1363 			break;
1364 		memcpy(as_in_type_i_desc.tSamFreq[idx++],
1365 				&audio_opts->p_srates[i], 3);
1366 	}
1367 	as_in_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1368 	as_in_type_i_desc.bSamFreqType = idx;
1369 	uac1->p_srate = audio_opts->p_srates[0];
1370 	uac1->c_srate = audio_opts->c_srates[0];
1371 
1372 	/* allocate instance-specific interface IDs, and patch descriptors */
1373 	status = usb_interface_id(c, f);
1374 	if (status < 0)
1375 		goto err_free_fu;
1376 	ac_interface_desc.bInterfaceNumber = status;
1377 	uac1->ac_intf = status;
1378 	uac1->ac_alt = 0;
1379 
1380 	ba_iface_id = 0;
1381 
1382 	if (EPOUT_EN(audio_opts)) {
1383 		status = usb_interface_id(c, f);
1384 		if (status < 0)
1385 			goto err_free_fu;
1386 		as_out_interface_alt_0_desc.bInterfaceNumber = status;
1387 		as_out_interface_alt_1_desc.bInterfaceNumber = status;
1388 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1389 		uac1->as_out_intf = status;
1390 		uac1->as_out_alt = 0;
1391 	}
1392 
1393 	if (EPIN_EN(audio_opts)) {
1394 		status = usb_interface_id(c, f);
1395 		if (status < 0)
1396 			goto err_free_fu;
1397 		as_in_interface_alt_0_desc.bInterfaceNumber = status;
1398 		as_in_interface_alt_1_desc.bInterfaceNumber = status;
1399 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
1400 		uac1->as_in_intf = status;
1401 		uac1->as_in_alt = 0;
1402 	}
1403 
1404 	audio->gadget = gadget;
1405 
1406 	status = -ENODEV;
1407 
1408 	ac_interface_desc.bNumEndpoints = 0;
1409 
1410 	/* allocate AC interrupt endpoint */
1411 	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) {
1412 		ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc);
1413 		if (!ep)
1414 			goto err_free_fu;
1415 		uac1->int_ep = ep;
1416 		uac1->int_ep->desc = &ac_int_ep_desc;
1417 
1418 		ac_interface_desc.bNumEndpoints = 1;
1419 	}
1420 
1421 	/* allocate instance-specific endpoints */
1422 	if (EPOUT_EN(audio_opts)) {
1423 		ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
1424 		if (!ep)
1425 			goto err_free_fu;
1426 		ss_as_out_ep_desc.bEndpointAddress = as_out_ep_desc.bEndpointAddress;
1427 		audio->out_ep = ep;
1428 		audio->out_ep->desc = &as_out_ep_desc;
1429 	}
1430 
1431 	if (EPIN_EN(audio_opts)) {
1432 		ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
1433 		if (!ep)
1434 			goto err_free_fu;
1435 		ss_as_in_ep_desc.bEndpointAddress = as_in_ep_desc.bEndpointAddress;
1436 		audio->in_ep = ep;
1437 		audio->in_ep->desc = &as_in_ep_desc;
1438 	}
1439 
1440 	setup_descriptor(audio_opts);
1441 
1442 	/* copy descriptors, and track endpoint copies */
1443 	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, f_audio_ss_desc,
1444 					f_audio_ss_desc);
1445 	if (status)
1446 		goto err_free_fu;
1447 
1448 	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
1449 	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
1450 	audio->params.c_chmask = audio_opts->c_chmask;
1451 	memcpy(audio->params.c_srates, audio_opts->c_srates,
1452 			sizeof(audio->params.c_srates));
1453 	audio->params.c_ssize = audio_opts->c_ssize;
1454 	if (FUIN_EN(audio_opts)) {
1455 		audio->params.p_fu.id = USB_IN_FU_ID;
1456 		audio->params.p_fu.mute_present = audio_opts->p_mute_present;
1457 		audio->params.p_fu.volume_present =
1458 				audio_opts->p_volume_present;
1459 		audio->params.p_fu.volume_min = audio_opts->p_volume_min;
1460 		audio->params.p_fu.volume_max = audio_opts->p_volume_max;
1461 		audio->params.p_fu.volume_res = audio_opts->p_volume_res;
1462 	}
1463 	audio->params.p_chmask = audio_opts->p_chmask;
1464 	memcpy(audio->params.p_srates, audio_opts->p_srates,
1465 			sizeof(audio->params.p_srates));
1466 	audio->params.p_ssize = audio_opts->p_ssize;
1467 	if (FUOUT_EN(audio_opts)) {
1468 		audio->params.c_fu.id = USB_OUT_FU_ID;
1469 		audio->params.c_fu.mute_present = audio_opts->c_mute_present;
1470 		audio->params.c_fu.volume_present =
1471 				audio_opts->c_volume_present;
1472 		audio->params.c_fu.volume_min = audio_opts->c_volume_min;
1473 		audio->params.c_fu.volume_max = audio_opts->c_volume_max;
1474 		audio->params.c_fu.volume_res = audio_opts->c_volume_res;
1475 	}
1476 	audio->params.req_number = audio_opts->req_number;
1477 	audio->params.fb_max = FBACK_FAST_MAX;
1478 	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts))
1479 		audio->notify = audio_notify;
1480 
1481 	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
1482 	if (status)
1483 		goto err_card_register;
1484 
1485 	return 0;
1486 
1487 err_card_register:
1488 	usb_free_all_descriptors(f);
1489 err_free_fu:
1490 	kfree(out_feature_unit_desc);
1491 	out_feature_unit_desc = NULL;
1492 	kfree(in_feature_unit_desc);
1493 	in_feature_unit_desc = NULL;
1494 fail:
1495 	kfree(ac_header_desc);
1496 	ac_header_desc = NULL;
1497 	return status;
1498 }
1499 
1500 /*-------------------------------------------------------------------------*/
1501 
1502 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
1503 {
1504 	return container_of(to_config_group(item), struct f_uac1_opts,
1505 			    func_inst.group);
1506 }
1507 
1508 static void f_uac1_attr_release(struct config_item *item)
1509 {
1510 	struct f_uac1_opts *opts = to_f_uac1_opts(item);
1511 
1512 	usb_put_function_instance(&opts->func_inst);
1513 }
1514 
1515 static struct configfs_item_operations f_uac1_item_ops = {
1516 	.release	= f_uac1_attr_release,
1517 };
1518 
1519 #define uac1_kstrtou32			kstrtou32
1520 #define uac1_kstrtos16			kstrtos16
1521 #define uac1_kstrtobool(s, base, res)	kstrtobool((s), (res))
1522 
1523 static const char *u32_fmt = "%u\n";
1524 static const char *s16_fmt = "%hd\n";
1525 static const char *bool_fmt = "%u\n";
1526 
1527 #define UAC1_ATTRIBUTE(type, name)					\
1528 static ssize_t f_uac1_opts_##name##_show(				\
1529 					  struct config_item *item,	\
1530 					  char *page)			\
1531 {									\
1532 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1533 	int result;							\
1534 									\
1535 	mutex_lock(&opts->lock);					\
1536 	result = sprintf(page, type##_fmt, opts->name);			\
1537 	mutex_unlock(&opts->lock);					\
1538 									\
1539 	return result;							\
1540 }									\
1541 									\
1542 static ssize_t f_uac1_opts_##name##_store(				\
1543 					  struct config_item *item,	\
1544 					  const char *page, size_t len)	\
1545 {									\
1546 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1547 	int ret;							\
1548 	type num;							\
1549 									\
1550 	mutex_lock(&opts->lock);					\
1551 	if (opts->refcnt) {						\
1552 		ret = -EBUSY;						\
1553 		goto end;						\
1554 	}								\
1555 									\
1556 	ret = uac1_kstrto##type(page, 0, &num);				\
1557 	if (ret)							\
1558 		goto end;						\
1559 									\
1560 	opts->name = num;						\
1561 	ret = len;							\
1562 									\
1563 end:									\
1564 	mutex_unlock(&opts->lock);					\
1565 	return ret;							\
1566 }									\
1567 									\
1568 CONFIGFS_ATTR(f_uac1_opts_, name)
1569 
1570 #define UAC1_RATE_ATTRIBUTE(name)					\
1571 static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1572 					 char *page)			\
1573 {									\
1574 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1575 	int result = 0;							\
1576 	int i;								\
1577 									\
1578 	mutex_lock(&opts->lock);					\
1579 	page[0] = '\0';							\
1580 	for (i = 0; i < UAC_MAX_RATES; i++) {				\
1581 		if (opts->name##s[i] == 0)				\
1582 			break;						\
1583 		result += sprintf(page + strlen(page), "%u,",		\
1584 				opts->name##s[i]);			\
1585 	}								\
1586 	if (strlen(page) > 0)						\
1587 		page[strlen(page) - 1] = '\n';				\
1588 	mutex_unlock(&opts->lock);					\
1589 									\
1590 	return result;							\
1591 }									\
1592 									\
1593 static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1594 					  const char *page, size_t len)	\
1595 {									\
1596 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1597 	char *split_page = NULL;					\
1598 	int ret = -EINVAL;						\
1599 	char *token;							\
1600 	u32 num;							\
1601 	int i;								\
1602 									\
1603 	mutex_lock(&opts->lock);					\
1604 	if (opts->refcnt) {						\
1605 		ret = -EBUSY;						\
1606 		goto end;						\
1607 	}								\
1608 									\
1609 	i = 0;								\
1610 	memset(opts->name##s, 0x00, sizeof(opts->name##s));		\
1611 	split_page = kstrdup(page, GFP_KERNEL);				\
1612 	while ((token = strsep(&split_page, ",")) != NULL) {		\
1613 		ret = kstrtou32(token, 0, &num);			\
1614 		if (ret)						\
1615 			goto end;					\
1616 									\
1617 		opts->name##s[i++] = num;				\
1618 		ret = len;						\
1619 	};								\
1620 									\
1621 end:									\
1622 	kfree(split_page);						\
1623 	mutex_unlock(&opts->lock);					\
1624 	return ret;							\
1625 }									\
1626 									\
1627 CONFIGFS_ATTR(f_uac1_opts_, name)
1628 
1629 #define UAC1_ATTRIBUTE_STRING(name)					\
1630 static ssize_t f_uac1_opts_##name##_show(struct config_item *item,	\
1631 					 char *page)			\
1632 {									\
1633 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1634 	int result;							\
1635 									\
1636 	mutex_lock(&opts->lock);					\
1637 	result = scnprintf(page, sizeof(opts->name), "%s", opts->name);	\
1638 	mutex_unlock(&opts->lock);					\
1639 									\
1640 	return result;							\
1641 }									\
1642 									\
1643 static ssize_t f_uac1_opts_##name##_store(struct config_item *item,	\
1644 					  const char *page, size_t len)	\
1645 {									\
1646 	struct f_uac1_opts *opts = to_f_uac1_opts(item);		\
1647 	int ret = 0;							\
1648 									\
1649 	mutex_lock(&opts->lock);					\
1650 	if (opts->refcnt) {						\
1651 		ret = -EBUSY;						\
1652 		goto end;						\
1653 	}								\
1654 									\
1655 	ret = scnprintf(opts->name, min(sizeof(opts->name), len),	\
1656 			"%s", page);					\
1657 									\
1658 end:									\
1659 	mutex_unlock(&opts->lock);					\
1660 	return ret;							\
1661 }									\
1662 									\
1663 CONFIGFS_ATTR(f_uac1_opts_, name)
1664 
1665 UAC1_ATTRIBUTE(u32, c_chmask);
1666 UAC1_RATE_ATTRIBUTE(c_srate);
1667 UAC1_ATTRIBUTE(u32, c_ssize);
1668 UAC1_ATTRIBUTE(u32, p_chmask);
1669 UAC1_RATE_ATTRIBUTE(p_srate);
1670 UAC1_ATTRIBUTE(u32, p_ssize);
1671 UAC1_ATTRIBUTE(u32, req_number);
1672 
1673 UAC1_ATTRIBUTE(bool, p_mute_present);
1674 UAC1_ATTRIBUTE(bool, p_volume_present);
1675 UAC1_ATTRIBUTE(s16, p_volume_min);
1676 UAC1_ATTRIBUTE(s16, p_volume_max);
1677 UAC1_ATTRIBUTE(s16, p_volume_res);
1678 
1679 UAC1_ATTRIBUTE(bool, c_mute_present);
1680 UAC1_ATTRIBUTE(bool, c_volume_present);
1681 UAC1_ATTRIBUTE(s16, c_volume_min);
1682 UAC1_ATTRIBUTE(s16, c_volume_max);
1683 UAC1_ATTRIBUTE(s16, c_volume_res);
1684 
1685 UAC1_ATTRIBUTE_STRING(function_name);
1686 
1687 UAC1_ATTRIBUTE_STRING(p_it_name);
1688 UAC1_ATTRIBUTE_STRING(p_it_ch_name);
1689 UAC1_ATTRIBUTE_STRING(p_ot_name);
1690 UAC1_ATTRIBUTE_STRING(p_fu_vol_name);
1691 
1692 UAC1_ATTRIBUTE_STRING(c_it_name);
1693 UAC1_ATTRIBUTE_STRING(c_it_ch_name);
1694 UAC1_ATTRIBUTE_STRING(c_ot_name);
1695 UAC1_ATTRIBUTE_STRING(c_fu_vol_name);
1696 
1697 static struct configfs_attribute *f_uac1_attrs[] = {
1698 	&f_uac1_opts_attr_c_chmask,
1699 	&f_uac1_opts_attr_c_srate,
1700 	&f_uac1_opts_attr_c_ssize,
1701 	&f_uac1_opts_attr_p_chmask,
1702 	&f_uac1_opts_attr_p_srate,
1703 	&f_uac1_opts_attr_p_ssize,
1704 	&f_uac1_opts_attr_req_number,
1705 
1706 	&f_uac1_opts_attr_p_mute_present,
1707 	&f_uac1_opts_attr_p_volume_present,
1708 	&f_uac1_opts_attr_p_volume_min,
1709 	&f_uac1_opts_attr_p_volume_max,
1710 	&f_uac1_opts_attr_p_volume_res,
1711 
1712 	&f_uac1_opts_attr_c_mute_present,
1713 	&f_uac1_opts_attr_c_volume_present,
1714 	&f_uac1_opts_attr_c_volume_min,
1715 	&f_uac1_opts_attr_c_volume_max,
1716 	&f_uac1_opts_attr_c_volume_res,
1717 
1718 	&f_uac1_opts_attr_function_name,
1719 
1720 	&f_uac1_opts_attr_p_it_name,
1721 	&f_uac1_opts_attr_p_it_ch_name,
1722 	&f_uac1_opts_attr_p_ot_name,
1723 	&f_uac1_opts_attr_p_fu_vol_name,
1724 
1725 	&f_uac1_opts_attr_c_it_name,
1726 	&f_uac1_opts_attr_c_it_ch_name,
1727 	&f_uac1_opts_attr_c_ot_name,
1728 	&f_uac1_opts_attr_c_fu_vol_name,
1729 
1730 	NULL,
1731 };
1732 
1733 static const struct config_item_type f_uac1_func_type = {
1734 	.ct_item_ops	= &f_uac1_item_ops,
1735 	.ct_attrs	= f_uac1_attrs,
1736 	.ct_owner	= THIS_MODULE,
1737 };
1738 
1739 static void f_audio_free_inst(struct usb_function_instance *f)
1740 {
1741 	struct f_uac1_opts *opts;
1742 
1743 	opts = container_of(f, struct f_uac1_opts, func_inst);
1744 	kfree(opts);
1745 }
1746 
1747 static struct usb_function_instance *f_audio_alloc_inst(void)
1748 {
1749 	struct f_uac1_opts *opts;
1750 
1751 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1752 	if (!opts)
1753 		return ERR_PTR(-ENOMEM);
1754 
1755 	mutex_init(&opts->lock);
1756 	opts->func_inst.free_func_inst = f_audio_free_inst;
1757 
1758 	config_group_init_type_name(&opts->func_inst.group, "",
1759 				    &f_uac1_func_type);
1760 
1761 	opts->c_chmask = UAC1_DEF_CCHMASK;
1762 	opts->c_srates[0] = UAC1_DEF_CSRATE;
1763 	opts->c_ssize = UAC1_DEF_CSSIZE;
1764 	opts->p_chmask = UAC1_DEF_PCHMASK;
1765 	opts->p_srates[0] = UAC1_DEF_PSRATE;
1766 	opts->p_ssize = UAC1_DEF_PSSIZE;
1767 
1768 	opts->p_mute_present = UAC1_DEF_MUTE_PRESENT;
1769 	opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT;
1770 	opts->p_volume_min = UAC1_DEF_MIN_DB;
1771 	opts->p_volume_max = UAC1_DEF_MAX_DB;
1772 	opts->p_volume_res = UAC1_DEF_RES_DB;
1773 
1774 	opts->c_mute_present = UAC1_DEF_MUTE_PRESENT;
1775 	opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT;
1776 	opts->c_volume_min = UAC1_DEF_MIN_DB;
1777 	opts->c_volume_max = UAC1_DEF_MAX_DB;
1778 	opts->c_volume_res = UAC1_DEF_RES_DB;
1779 
1780 	opts->req_number = UAC1_DEF_REQ_NUM;
1781 
1782 	scnprintf(opts->function_name, sizeof(opts->function_name), "AC Interface");
1783 
1784 	scnprintf(opts->p_it_name, sizeof(opts->p_it_name), "Capture Input terminal");
1785 	scnprintf(opts->p_it_ch_name, sizeof(opts->p_it_ch_name), "Capture Channels");
1786 	scnprintf(opts->p_ot_name, sizeof(opts->p_ot_name), "Capture Output terminal");
1787 	scnprintf(opts->p_fu_vol_name, sizeof(opts->p_fu_vol_name), "Capture Volume");
1788 
1789 	scnprintf(opts->c_it_name, sizeof(opts->c_it_name), "Playback Input terminal");
1790 	scnprintf(opts->c_it_ch_name, sizeof(opts->c_it_ch_name), "Playback Channels");
1791 	scnprintf(opts->c_ot_name, sizeof(opts->c_ot_name), "Playback Output terminal");
1792 	scnprintf(opts->c_fu_vol_name, sizeof(opts->c_fu_vol_name), "Playback Volume");
1793 
1794 	return &opts->func_inst;
1795 }
1796 
1797 static void f_audio_free(struct usb_function *f)
1798 {
1799 	struct g_audio *audio;
1800 	struct f_uac1_opts *opts;
1801 
1802 	audio = func_to_g_audio(f);
1803 	opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1804 	kfree(audio);
1805 	mutex_lock(&opts->lock);
1806 	--opts->refcnt;
1807 	mutex_unlock(&opts->lock);
1808 }
1809 
1810 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1811 {
1812 	struct g_audio *audio = func_to_g_audio(f);
1813 
1814 	g_audio_cleanup(audio);
1815 	usb_free_all_descriptors(f);
1816 
1817 	kfree(out_feature_unit_desc);
1818 	out_feature_unit_desc = NULL;
1819 	kfree(in_feature_unit_desc);
1820 	in_feature_unit_desc = NULL;
1821 
1822 	kfree(ac_header_desc);
1823 	ac_header_desc = NULL;
1824 
1825 	audio->gadget = NULL;
1826 }
1827 
1828 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
1829 {
1830 	struct f_uac1 *uac1;
1831 	struct f_uac1_opts *opts;
1832 
1833 	/* allocate and initialize one new instance */
1834 	uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
1835 	if (!uac1)
1836 		return ERR_PTR(-ENOMEM);
1837 
1838 	opts = container_of(fi, struct f_uac1_opts, func_inst);
1839 	mutex_lock(&opts->lock);
1840 	++opts->refcnt;
1841 	mutex_unlock(&opts->lock);
1842 
1843 	uac1->g_audio.func.name = "uac1_func";
1844 	uac1->g_audio.func.bind = f_audio_bind;
1845 	uac1->g_audio.func.unbind = f_audio_unbind;
1846 	uac1->g_audio.func.set_alt = f_audio_set_alt;
1847 	uac1->g_audio.func.get_alt = f_audio_get_alt;
1848 	uac1->g_audio.func.setup = f_audio_setup;
1849 	uac1->g_audio.func.disable = f_audio_disable;
1850 	uac1->g_audio.func.suspend = f_audio_suspend;
1851 	uac1->g_audio.func.free_func = f_audio_free;
1852 
1853 	return &uac1->g_audio.func;
1854 }
1855 
1856 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
1857 MODULE_DESCRIPTION("USB Audio Class 1.0 Function (using u_audio API)");
1858 MODULE_LICENSE("GPL");
1859 MODULE_AUTHOR("Ruslan Bilovol");
1860