1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * f_uac2.c -- USB Audio Class 2.0 Function
4  *
5  * Copyright (C) 2011
6  *    Yadwinder Singh (yadi.brar01@gmail.com)
7  *    Jaswinder Singh (jaswinder.singh@linaro.org)
8  *
9  * Copyright (C) 2020
10  *    Ruslan Bilovol (ruslan.bilovol@gmail.com)
11  */
12 
13 #include <linux/usb/audio.h>
14 #include <linux/usb/audio-v2.h>
15 #include <linux/module.h>
16 
17 #include "u_audio.h"
18 #include "u_uac2.h"
19 
20 /* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
21 #define UAC2_CHANNEL_MASK 0x07FFFFFF
22 
23 /*
24  * The driver implements a simple UAC_2 topology.
25  * USB-OUT -> IT_1 -> FU -> OT_3 -> ALSA_Capture
26  * ALSA_Playback -> IT_2 -> FU -> OT_4 -> USB-IN
27  * Capture and Playback sampling rates are independently
28  *  controlled by two clock sources :
29  *    CLK_5 := c_srate, and CLK_6 := p_srate
30  */
31 #define USB_OUT_CLK_ID	(out_clk_src_desc.bClockID)
32 #define USB_IN_CLK_ID	(in_clk_src_desc.bClockID)
33 #define USB_OUT_FU_ID	(out_feature_unit_desc->bUnitID)
34 #define USB_IN_FU_ID	(in_feature_unit_desc->bUnitID)
35 
36 #define CONTROL_ABSENT	0
37 #define CONTROL_RDONLY	1
38 #define CONTROL_RDWR	3
39 
40 #define CLK_FREQ_CTRL	0
41 #define CLK_VLD_CTRL	2
42 #define FU_MUTE_CTRL	0
43 #define FU_VOL_CTRL	2
44 
45 #define COPY_CTRL	0
46 #define CONN_CTRL	2
47 #define OVRLD_CTRL	4
48 #define CLSTR_CTRL	6
49 #define UNFLW_CTRL	8
50 #define OVFLW_CTRL	10
51 
52 #define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
53 #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
54 #define FUIN_EN(_opts) (EPIN_EN(_opts) \
55 				&& ((_opts)->p_mute_present \
56 				|| (_opts)->p_volume_present))
57 #define FUOUT_EN(_opts) (EPOUT_EN(_opts) \
58 				&& ((_opts)->c_mute_present \
59 				|| (_opts)->c_volume_present))
60 #define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC)
61 
62 struct f_uac2 {
63 	struct g_audio g_audio;
64 	u8 ac_intf, as_in_intf, as_out_intf;
65 	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
66 
67 	struct usb_ctrlrequest setup_cr;	/* will be used in data stage */
68 
69 	/* Interrupt IN endpoint of AC interface */
70 	struct usb_ep	*int_ep;
71 	atomic_t	int_count;
72 };
73 
74 static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
75 {
76 	return container_of(f, struct f_uac2, g_audio.func);
77 }
78 
79 static inline
80 struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
81 {
82 	return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
83 }
84 
85 static int afunc_notify(struct g_audio *agdev, int unit_id, int cs);
86 
87 /* --------- USB Function Interface ------------- */
88 
89 enum {
90 	STR_ASSOC,
91 	STR_IF_CTRL,
92 	STR_CLKSRC_IN,
93 	STR_CLKSRC_OUT,
94 	STR_USB_IT,
95 	STR_IO_IT,
96 	STR_USB_OT,
97 	STR_IO_OT,
98 	STR_FU_IN,
99 	STR_FU_OUT,
100 	STR_AS_OUT_ALT0,
101 	STR_AS_OUT_ALT1,
102 	STR_AS_IN_ALT0,
103 	STR_AS_IN_ALT1,
104 };
105 
106 static char clksrc_in[8];
107 static char clksrc_out[8];
108 
109 static struct usb_string strings_fn[] = {
110 	[STR_ASSOC].s = "Source/Sink",
111 	[STR_IF_CTRL].s = "Topology Control",
112 	[STR_CLKSRC_IN].s = clksrc_in,
113 	[STR_CLKSRC_OUT].s = clksrc_out,
114 	[STR_USB_IT].s = "USBH Out",
115 	[STR_IO_IT].s = "USBD Out",
116 	[STR_USB_OT].s = "USBH In",
117 	[STR_IO_OT].s = "USBD In",
118 	[STR_FU_IN].s = "Capture Volume",
119 	[STR_FU_OUT].s = "Playback Volume",
120 	[STR_AS_OUT_ALT0].s = "Playback Inactive",
121 	[STR_AS_OUT_ALT1].s = "Playback Active",
122 	[STR_AS_IN_ALT0].s = "Capture Inactive",
123 	[STR_AS_IN_ALT1].s = "Capture Active",
124 	{ },
125 };
126 
127 static struct usb_gadget_strings str_fn = {
128 	.language = 0x0409,	/* en-us */
129 	.strings = strings_fn,
130 };
131 
132 static struct usb_gadget_strings *fn_strings[] = {
133 	&str_fn,
134 	NULL,
135 };
136 
137 static struct usb_interface_assoc_descriptor iad_desc = {
138 	.bLength = sizeof iad_desc,
139 	.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
140 
141 	.bFirstInterface = 0,
142 	.bInterfaceCount = 3,
143 	.bFunctionClass = USB_CLASS_AUDIO,
144 	.bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
145 	.bFunctionProtocol = UAC_VERSION_2,
146 };
147 
148 /* Audio Control Interface */
149 static struct usb_interface_descriptor std_ac_if_desc = {
150 	.bLength = sizeof std_ac_if_desc,
151 	.bDescriptorType = USB_DT_INTERFACE,
152 
153 	.bAlternateSetting = 0,
154 	/* .bNumEndpoints = DYNAMIC */
155 	.bInterfaceClass = USB_CLASS_AUDIO,
156 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
157 	.bInterfaceProtocol = UAC_VERSION_2,
158 };
159 
160 /* Clock source for IN traffic */
161 static struct uac_clock_source_descriptor in_clk_src_desc = {
162 	.bLength = sizeof in_clk_src_desc,
163 	.bDescriptorType = USB_DT_CS_INTERFACE,
164 
165 	.bDescriptorSubtype = UAC2_CLOCK_SOURCE,
166 	/* .bClockID = DYNAMIC */
167 	.bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
168 	.bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
169 	.bAssocTerminal = 0,
170 };
171 
172 /* Clock source for OUT traffic */
173 static struct uac_clock_source_descriptor out_clk_src_desc = {
174 	.bLength = sizeof out_clk_src_desc,
175 	.bDescriptorType = USB_DT_CS_INTERFACE,
176 
177 	.bDescriptorSubtype = UAC2_CLOCK_SOURCE,
178 	/* .bClockID = DYNAMIC */
179 	.bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
180 	.bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
181 	.bAssocTerminal = 0,
182 };
183 
184 /* Input Terminal for USB_OUT */
185 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
186 	.bLength = sizeof usb_out_it_desc,
187 	.bDescriptorType = USB_DT_CS_INTERFACE,
188 
189 	.bDescriptorSubtype = UAC_INPUT_TERMINAL,
190 	/* .bTerminalID = DYNAMIC */
191 	.wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
192 	.bAssocTerminal = 0,
193 	/* .bCSourceID = DYNAMIC */
194 	.iChannelNames = 0,
195 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
196 };
197 
198 /* Input Terminal for I/O-In */
199 static struct uac2_input_terminal_descriptor io_in_it_desc = {
200 	.bLength = sizeof io_in_it_desc,
201 	.bDescriptorType = USB_DT_CS_INTERFACE,
202 
203 	.bDescriptorSubtype = UAC_INPUT_TERMINAL,
204 	/* .bTerminalID = DYNAMIC */
205 	.wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
206 	.bAssocTerminal = 0,
207 	/* .bCSourceID = DYNAMIC */
208 	.iChannelNames = 0,
209 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
210 };
211 
212 /* Ouput Terminal for USB_IN */
213 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
214 	.bLength = sizeof usb_in_ot_desc,
215 	.bDescriptorType = USB_DT_CS_INTERFACE,
216 
217 	.bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
218 	/* .bTerminalID = DYNAMIC */
219 	.wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
220 	.bAssocTerminal = 0,
221 	/* .bSourceID = DYNAMIC */
222 	/* .bCSourceID = DYNAMIC */
223 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
224 };
225 
226 /* Ouput Terminal for I/O-Out */
227 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
228 	.bLength = sizeof io_out_ot_desc,
229 	.bDescriptorType = USB_DT_CS_INTERFACE,
230 
231 	.bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
232 	/* .bTerminalID = DYNAMIC */
233 	.wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
234 	.bAssocTerminal = 0,
235 	/* .bSourceID = DYNAMIC */
236 	/* .bCSourceID = DYNAMIC */
237 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
238 };
239 
240 static struct uac2_feature_unit_descriptor *in_feature_unit_desc;
241 static struct uac2_feature_unit_descriptor *out_feature_unit_desc;
242 
243 static struct uac2_ac_header_descriptor ac_hdr_desc = {
244 	.bLength = sizeof ac_hdr_desc,
245 	.bDescriptorType = USB_DT_CS_INTERFACE,
246 
247 	.bDescriptorSubtype = UAC_MS_HEADER,
248 	.bcdADC = cpu_to_le16(0x200),
249 	.bCategory = UAC2_FUNCTION_IO_BOX,
250 	/* .wTotalLength = DYNAMIC */
251 	.bmControls = 0,
252 };
253 
254 /* AC IN Interrupt Endpoint */
255 static struct usb_endpoint_descriptor fs_ep_int_desc = {
256 	.bLength = USB_DT_ENDPOINT_SIZE,
257 	.bDescriptorType = USB_DT_ENDPOINT,
258 
259 	.bEndpointAddress = USB_DIR_IN,
260 	.bmAttributes = USB_ENDPOINT_XFER_INT,
261 	.wMaxPacketSize = cpu_to_le16(6),
262 	.bInterval = 1,
263 };
264 
265 static struct usb_endpoint_descriptor hs_ep_int_desc = {
266 	.bLength = USB_DT_ENDPOINT_SIZE,
267 	.bDescriptorType = USB_DT_ENDPOINT,
268 
269 	.bmAttributes = USB_ENDPOINT_XFER_INT,
270 	.wMaxPacketSize = cpu_to_le16(6),
271 	.bInterval = 4,
272 };
273 
274 static struct usb_endpoint_descriptor ss_ep_int_desc = {
275 	.bLength = USB_DT_ENDPOINT_SIZE,
276 	.bDescriptorType = USB_DT_ENDPOINT,
277 
278 	.bEndpointAddress = USB_DIR_IN,
279 	.bmAttributes = USB_ENDPOINT_XFER_INT,
280 	.wMaxPacketSize = cpu_to_le16(6),
281 	.bInterval = 4,
282 };
283 
284 /* Audio Streaming OUT Interface - Alt0 */
285 static struct usb_interface_descriptor std_as_out_if0_desc = {
286 	.bLength = sizeof std_as_out_if0_desc,
287 	.bDescriptorType = USB_DT_INTERFACE,
288 
289 	.bAlternateSetting = 0,
290 	.bNumEndpoints = 0,
291 	.bInterfaceClass = USB_CLASS_AUDIO,
292 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
293 	.bInterfaceProtocol = UAC_VERSION_2,
294 };
295 
296 /* Audio Streaming OUT Interface - Alt1 */
297 static struct usb_interface_descriptor std_as_out_if1_desc = {
298 	.bLength = sizeof std_as_out_if1_desc,
299 	.bDescriptorType = USB_DT_INTERFACE,
300 
301 	.bAlternateSetting = 1,
302 	.bNumEndpoints = 1,
303 	.bInterfaceClass = USB_CLASS_AUDIO,
304 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
305 	.bInterfaceProtocol = UAC_VERSION_2,
306 };
307 
308 /* Audio Stream OUT Intface Desc */
309 static struct uac2_as_header_descriptor as_out_hdr_desc = {
310 	.bLength = sizeof as_out_hdr_desc,
311 	.bDescriptorType = USB_DT_CS_INTERFACE,
312 
313 	.bDescriptorSubtype = UAC_AS_GENERAL,
314 	/* .bTerminalLink = DYNAMIC */
315 	.bmControls = 0,
316 	.bFormatType = UAC_FORMAT_TYPE_I,
317 	.bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
318 	.iChannelNames = 0,
319 };
320 
321 /* Audio USB_OUT Format */
322 static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
323 	.bLength = sizeof as_out_fmt1_desc,
324 	.bDescriptorType = USB_DT_CS_INTERFACE,
325 	.bDescriptorSubtype = UAC_FORMAT_TYPE,
326 	.bFormatType = UAC_FORMAT_TYPE_I,
327 };
328 
329 /* STD AS ISO OUT Endpoint */
330 static struct usb_endpoint_descriptor fs_epout_desc = {
331 	.bLength = USB_DT_ENDPOINT_SIZE,
332 	.bDescriptorType = USB_DT_ENDPOINT,
333 
334 	.bEndpointAddress = USB_DIR_OUT,
335 	/* .bmAttributes = DYNAMIC */
336 	/* .wMaxPacketSize = DYNAMIC */
337 	.bInterval = 1,
338 };
339 
340 static struct usb_endpoint_descriptor hs_epout_desc = {
341 	.bLength = USB_DT_ENDPOINT_SIZE,
342 	.bDescriptorType = USB_DT_ENDPOINT,
343 
344 	/* .bmAttributes = DYNAMIC */
345 	/* .wMaxPacketSize = DYNAMIC */
346 	.bInterval = 4,
347 };
348 
349 static struct usb_endpoint_descriptor ss_epout_desc = {
350 	.bLength = USB_DT_ENDPOINT_SIZE,
351 	.bDescriptorType = USB_DT_ENDPOINT,
352 
353 	.bEndpointAddress = USB_DIR_OUT,
354 	/* .bmAttributes = DYNAMIC */
355 	/* .wMaxPacketSize = DYNAMIC */
356 	.bInterval = 4,
357 };
358 
359 static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
360 	.bLength		= sizeof(ss_epout_desc_comp),
361 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
362 	.bMaxBurst		= 0,
363 	.bmAttributes		= 0,
364 	/* wBytesPerInterval = DYNAMIC */
365 };
366 
367 /* CS AS ISO OUT Endpoint */
368 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
369 	.bLength = sizeof as_iso_out_desc,
370 	.bDescriptorType = USB_DT_CS_ENDPOINT,
371 
372 	.bDescriptorSubtype = UAC_EP_GENERAL,
373 	.bmAttributes = 0,
374 	.bmControls = 0,
375 	.bLockDelayUnits = 0,
376 	.wLockDelay = 0,
377 };
378 
379 /* STD AS ISO IN Feedback Endpoint */
380 static struct usb_endpoint_descriptor fs_epin_fback_desc = {
381 	.bLength = USB_DT_ENDPOINT_SIZE,
382 	.bDescriptorType = USB_DT_ENDPOINT,
383 
384 	.bEndpointAddress = USB_DIR_IN,
385 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
386 	.wMaxPacketSize = cpu_to_le16(3),
387 	.bInterval = 1,
388 };
389 
390 static struct usb_endpoint_descriptor hs_epin_fback_desc = {
391 	.bLength = USB_DT_ENDPOINT_SIZE,
392 	.bDescriptorType = USB_DT_ENDPOINT,
393 
394 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
395 	.wMaxPacketSize = cpu_to_le16(4),
396 	.bInterval = 4,
397 };
398 
399 static struct usb_endpoint_descriptor ss_epin_fback_desc = {
400 	.bLength = USB_DT_ENDPOINT_SIZE,
401 	.bDescriptorType = USB_DT_ENDPOINT,
402 
403 	.bEndpointAddress = USB_DIR_IN,
404 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
405 	.wMaxPacketSize = cpu_to_le16(4),
406 	.bInterval = 4,
407 };
408 
409 static struct usb_ss_ep_comp_descriptor ss_epin_fback_desc_comp = {
410 	.bLength		= sizeof(ss_epin_fback_desc_comp),
411 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
412 	.bMaxBurst		= 0,
413 	.bmAttributes		= 0,
414 	.wBytesPerInterval	= cpu_to_le16(4),
415 };
416 
417 
418 /* Audio Streaming IN Interface - Alt0 */
419 static struct usb_interface_descriptor std_as_in_if0_desc = {
420 	.bLength = sizeof std_as_in_if0_desc,
421 	.bDescriptorType = USB_DT_INTERFACE,
422 
423 	.bAlternateSetting = 0,
424 	.bNumEndpoints = 0,
425 	.bInterfaceClass = USB_CLASS_AUDIO,
426 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
427 	.bInterfaceProtocol = UAC_VERSION_2,
428 };
429 
430 /* Audio Streaming IN Interface - Alt1 */
431 static struct usb_interface_descriptor std_as_in_if1_desc = {
432 	.bLength = sizeof std_as_in_if1_desc,
433 	.bDescriptorType = USB_DT_INTERFACE,
434 
435 	.bAlternateSetting = 1,
436 	.bNumEndpoints = 1,
437 	.bInterfaceClass = USB_CLASS_AUDIO,
438 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
439 	.bInterfaceProtocol = UAC_VERSION_2,
440 };
441 
442 /* Audio Stream IN Intface Desc */
443 static struct uac2_as_header_descriptor as_in_hdr_desc = {
444 	.bLength = sizeof as_in_hdr_desc,
445 	.bDescriptorType = USB_DT_CS_INTERFACE,
446 
447 	.bDescriptorSubtype = UAC_AS_GENERAL,
448 	/* .bTerminalLink = DYNAMIC */
449 	.bmControls = 0,
450 	.bFormatType = UAC_FORMAT_TYPE_I,
451 	.bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
452 	.iChannelNames = 0,
453 };
454 
455 /* Audio USB_IN Format */
456 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
457 	.bLength = sizeof as_in_fmt1_desc,
458 	.bDescriptorType = USB_DT_CS_INTERFACE,
459 	.bDescriptorSubtype = UAC_FORMAT_TYPE,
460 	.bFormatType = UAC_FORMAT_TYPE_I,
461 };
462 
463 /* STD AS ISO IN Endpoint */
464 static struct usb_endpoint_descriptor fs_epin_desc = {
465 	.bLength = USB_DT_ENDPOINT_SIZE,
466 	.bDescriptorType = USB_DT_ENDPOINT,
467 
468 	.bEndpointAddress = USB_DIR_IN,
469 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
470 	/* .wMaxPacketSize = DYNAMIC */
471 	.bInterval = 1,
472 };
473 
474 static struct usb_endpoint_descriptor hs_epin_desc = {
475 	.bLength = USB_DT_ENDPOINT_SIZE,
476 	.bDescriptorType = USB_DT_ENDPOINT,
477 
478 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
479 	/* .wMaxPacketSize = DYNAMIC */
480 	.bInterval = 4,
481 };
482 
483 static struct usb_endpoint_descriptor ss_epin_desc = {
484 	.bLength = USB_DT_ENDPOINT_SIZE,
485 	.bDescriptorType = USB_DT_ENDPOINT,
486 
487 	.bEndpointAddress = USB_DIR_IN,
488 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
489 	/* .wMaxPacketSize = DYNAMIC */
490 	.bInterval = 4,
491 };
492 
493 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
494 	.bLength		= sizeof(ss_epin_desc_comp),
495 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
496 	.bMaxBurst		= 0,
497 	.bmAttributes		= 0,
498 	/* wBytesPerInterval = DYNAMIC */
499 };
500 
501 /* CS AS ISO IN Endpoint */
502 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
503 	.bLength = sizeof as_iso_in_desc,
504 	.bDescriptorType = USB_DT_CS_ENDPOINT,
505 
506 	.bDescriptorSubtype = UAC_EP_GENERAL,
507 	.bmAttributes = 0,
508 	.bmControls = 0,
509 	.bLockDelayUnits = 0,
510 	.wLockDelay = 0,
511 };
512 
513 static struct usb_descriptor_header *fs_audio_desc[] = {
514 	(struct usb_descriptor_header *)&iad_desc,
515 	(struct usb_descriptor_header *)&std_ac_if_desc,
516 
517 	(struct usb_descriptor_header *)&ac_hdr_desc,
518 	(struct usb_descriptor_header *)&in_clk_src_desc,
519 	(struct usb_descriptor_header *)&out_clk_src_desc,
520 	(struct usb_descriptor_header *)&usb_out_it_desc,
521 	(struct usb_descriptor_header *)&out_feature_unit_desc,
522 	(struct usb_descriptor_header *)&io_in_it_desc,
523 	(struct usb_descriptor_header *)&usb_in_ot_desc,
524 	(struct usb_descriptor_header *)&in_feature_unit_desc,
525 	(struct usb_descriptor_header *)&io_out_ot_desc,
526 
527 	(struct usb_descriptor_header *)&fs_ep_int_desc,
528 
529 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
530 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
531 
532 	(struct usb_descriptor_header *)&as_out_hdr_desc,
533 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
534 	(struct usb_descriptor_header *)&fs_epout_desc,
535 	(struct usb_descriptor_header *)&as_iso_out_desc,
536 	(struct usb_descriptor_header *)&fs_epin_fback_desc,
537 
538 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
539 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
540 
541 	(struct usb_descriptor_header *)&as_in_hdr_desc,
542 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
543 	(struct usb_descriptor_header *)&fs_epin_desc,
544 	(struct usb_descriptor_header *)&as_iso_in_desc,
545 	NULL,
546 };
547 
548 static struct usb_descriptor_header *hs_audio_desc[] = {
549 	(struct usb_descriptor_header *)&iad_desc,
550 	(struct usb_descriptor_header *)&std_ac_if_desc,
551 
552 	(struct usb_descriptor_header *)&ac_hdr_desc,
553 	(struct usb_descriptor_header *)&in_clk_src_desc,
554 	(struct usb_descriptor_header *)&out_clk_src_desc,
555 	(struct usb_descriptor_header *)&usb_out_it_desc,
556 	(struct usb_descriptor_header *)&out_feature_unit_desc,
557 	(struct usb_descriptor_header *)&io_in_it_desc,
558 	(struct usb_descriptor_header *)&usb_in_ot_desc,
559 	(struct usb_descriptor_header *)&in_feature_unit_desc,
560 	(struct usb_descriptor_header *)&io_out_ot_desc,
561 
562 	(struct usb_descriptor_header *)&hs_ep_int_desc,
563 
564 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
565 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
566 
567 	(struct usb_descriptor_header *)&as_out_hdr_desc,
568 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
569 	(struct usb_descriptor_header *)&hs_epout_desc,
570 	(struct usb_descriptor_header *)&as_iso_out_desc,
571 	(struct usb_descriptor_header *)&hs_epin_fback_desc,
572 
573 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
574 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
575 
576 	(struct usb_descriptor_header *)&as_in_hdr_desc,
577 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
578 	(struct usb_descriptor_header *)&hs_epin_desc,
579 	(struct usb_descriptor_header *)&as_iso_in_desc,
580 	NULL,
581 };
582 
583 static struct usb_descriptor_header *ss_audio_desc[] = {
584 	(struct usb_descriptor_header *)&iad_desc,
585 	(struct usb_descriptor_header *)&std_ac_if_desc,
586 
587 	(struct usb_descriptor_header *)&ac_hdr_desc,
588 	(struct usb_descriptor_header *)&in_clk_src_desc,
589 	(struct usb_descriptor_header *)&out_clk_src_desc,
590 	(struct usb_descriptor_header *)&usb_out_it_desc,
591   (struct usb_descriptor_header *)&out_feature_unit_desc,
592 	(struct usb_descriptor_header *)&io_in_it_desc,
593 	(struct usb_descriptor_header *)&usb_in_ot_desc,
594 	(struct usb_descriptor_header *)&in_feature_unit_desc,
595 	(struct usb_descriptor_header *)&io_out_ot_desc,
596 
597   (struct usb_descriptor_header *)&ss_ep_int_desc,
598 
599 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
600 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
601 
602 	(struct usb_descriptor_header *)&as_out_hdr_desc,
603 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
604 	(struct usb_descriptor_header *)&ss_epout_desc,
605 	(struct usb_descriptor_header *)&ss_epout_desc_comp,
606 	(struct usb_descriptor_header *)&as_iso_out_desc,
607 	(struct usb_descriptor_header *)&ss_epin_fback_desc,
608 	(struct usb_descriptor_header *)&ss_epin_fback_desc_comp,
609 
610 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
611 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
612 
613 	(struct usb_descriptor_header *)&as_in_hdr_desc,
614 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
615 	(struct usb_descriptor_header *)&ss_epin_desc,
616 	(struct usb_descriptor_header *)&ss_epin_desc_comp,
617 	(struct usb_descriptor_header *)&as_iso_in_desc,
618 	NULL,
619 };
620 
621 struct cntrl_cur_lay2 {
622 	__le16	wCUR;
623 };
624 
625 struct cntrl_range_lay2 {
626 	__le16	wNumSubRanges;
627 	__le16	wMIN;
628 	__le16	wMAX;
629 	__le16	wRES;
630 } __packed;
631 
632 struct cntrl_cur_lay3 {
633 	__le32	dCUR;
634 };
635 
636 struct cntrl_range_lay3 {
637 	__le16	wNumSubRanges;
638 	__le32	dMIN;
639 	__le32	dMAX;
640 	__le32	dRES;
641 } __packed;
642 
643 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
644 	struct usb_endpoint_descriptor *ep_desc,
645 	enum usb_device_speed speed, bool is_playback)
646 {
647 	int chmask, srate, ssize;
648 	u16 max_size_bw, max_size_ep;
649 	unsigned int factor;
650 
651 	switch (speed) {
652 	case USB_SPEED_FULL:
653 		max_size_ep = 1023;
654 		factor = 1000;
655 		break;
656 
657 	case USB_SPEED_HIGH:
658 	case USB_SPEED_SUPER:
659 		max_size_ep = 1024;
660 		factor = 8000;
661 		break;
662 
663 	default:
664 		return -EINVAL;
665 	}
666 
667 	if (is_playback) {
668 		chmask = uac2_opts->p_chmask;
669 		srate = uac2_opts->p_srate;
670 		ssize = uac2_opts->p_ssize;
671 	} else {
672 		chmask = uac2_opts->c_chmask;
673 		srate = uac2_opts->c_srate;
674 		ssize = uac2_opts->c_ssize;
675 	}
676 
677 	if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
678 		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
679 
680 	max_size_bw = num_channels(chmask) * ssize *
681 		DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
682 	ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
683 						    max_size_ep));
684 
685 	return 0;
686 }
687 
688 static struct uac2_feature_unit_descriptor *build_fu_desc(int chmask)
689 {
690 	struct uac2_feature_unit_descriptor *fu_desc;
691 	int channels = num_channels(chmask);
692 	int fu_desc_size = UAC2_DT_FEATURE_UNIT_SIZE(channels);
693 
694 	fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
695 	if (!fu_desc)
696 		return NULL;
697 
698 	fu_desc->bLength = fu_desc_size;
699 	fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
700 
701 	fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
702 
703 	/* bUnitID, bSourceID and bmaControls will be defined later */
704 
705 	return fu_desc;
706 }
707 
708 /* Use macro to overcome line length limitation */
709 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
710 
711 static void setup_headers(struct f_uac2_opts *opts,
712 			  struct usb_descriptor_header **headers,
713 			  enum usb_device_speed speed)
714 {
715 	struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
716 	struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
717 	struct usb_ss_ep_comp_descriptor *epin_fback_desc_comp = NULL;
718 	struct usb_endpoint_descriptor *epout_desc;
719 	struct usb_endpoint_descriptor *epin_desc;
720 	struct usb_endpoint_descriptor *epin_fback_desc;
721 	struct usb_endpoint_descriptor *ep_int_desc;
722 	int i;
723 
724 	switch (speed) {
725 	case USB_SPEED_FULL:
726 		epout_desc = &fs_epout_desc;
727 		epin_desc = &fs_epin_desc;
728 		epin_fback_desc = &fs_epin_fback_desc;
729 		ep_int_desc = &fs_ep_int_desc;
730 		break;
731 	case USB_SPEED_HIGH:
732 		epout_desc = &hs_epout_desc;
733 		epin_desc = &hs_epin_desc;
734 		epin_fback_desc = &hs_epin_fback_desc;
735 		ep_int_desc = &hs_ep_int_desc;
736 		break;
737 	default:
738 		epout_desc = &ss_epout_desc;
739 		epin_desc = &ss_epin_desc;
740 		epout_desc_comp = &ss_epout_desc_comp;
741 		epin_desc_comp = &ss_epin_desc_comp;
742 		epin_fback_desc = &ss_epin_fback_desc;
743 		epin_fback_desc_comp = &ss_epin_fback_desc_comp;
744 		ep_int_desc = &ss_ep_int_desc;
745 	}
746 
747 	i = 0;
748 	headers[i++] = USBDHDR(&iad_desc);
749 	headers[i++] = USBDHDR(&std_ac_if_desc);
750 	headers[i++] = USBDHDR(&ac_hdr_desc);
751 	if (EPIN_EN(opts))
752 		headers[i++] = USBDHDR(&in_clk_src_desc);
753 	if (EPOUT_EN(opts)) {
754 		headers[i++] = USBDHDR(&out_clk_src_desc);
755 		headers[i++] = USBDHDR(&usb_out_it_desc);
756 
757     if (FUOUT_EN(opts))
758       headers[i++] = USBDHDR(out_feature_unit_desc);
759   }
760 
761 	if (EPIN_EN(opts)) {
762 		headers[i++] = USBDHDR(&io_in_it_desc);
763 
764     if (FUIN_EN(opts))
765       headers[i++] = USBDHDR(in_feature_unit_desc);
766 
767 		headers[i++] = USBDHDR(&usb_in_ot_desc);
768 	}
769 
770 	if (EPOUT_EN(opts))
771 		headers[i++] = USBDHDR(&io_out_ot_desc);
772 
773   if (FUOUT_EN(opts) || FUIN_EN(opts))
774       headers[i++] = USBDHDR(ep_int_desc);
775 
776   if (EPOUT_EN(opts)) {
777 		headers[i++] = USBDHDR(&std_as_out_if0_desc);
778 		headers[i++] = USBDHDR(&std_as_out_if1_desc);
779 		headers[i++] = USBDHDR(&as_out_hdr_desc);
780 		headers[i++] = USBDHDR(&as_out_fmt1_desc);
781 		headers[i++] = USBDHDR(epout_desc);
782 		if (epout_desc_comp)
783 			headers[i++] = USBDHDR(epout_desc_comp);
784 
785 		headers[i++] = USBDHDR(&as_iso_out_desc);
786 
787 		if (EPOUT_FBACK_IN_EN(opts)) {
788 			headers[i++] = USBDHDR(epin_fback_desc);
789 			if (epin_fback_desc_comp)
790 				headers[i++] = USBDHDR(epin_fback_desc_comp);
791 		}
792 	}
793 
794 	if (EPIN_EN(opts)) {
795 		headers[i++] = USBDHDR(&std_as_in_if0_desc);
796 		headers[i++] = USBDHDR(&std_as_in_if1_desc);
797 		headers[i++] = USBDHDR(&as_in_hdr_desc);
798 		headers[i++] = USBDHDR(&as_in_fmt1_desc);
799 		headers[i++] = USBDHDR(epin_desc);
800 		if (epin_desc_comp)
801 			headers[i++] = USBDHDR(epin_desc_comp);
802 
803 		headers[i++] = USBDHDR(&as_iso_in_desc);
804 	}
805 	headers[i] = NULL;
806 }
807 
808 static void setup_descriptor(struct f_uac2_opts *opts)
809 {
810 	/* patch descriptors */
811 	int i = 1; /* ID's start with 1 */
812 
813 	if (EPOUT_EN(opts))
814 		usb_out_it_desc.bTerminalID = i++;
815 	if (EPIN_EN(opts))
816 		io_in_it_desc.bTerminalID = i++;
817 	if (EPOUT_EN(opts))
818 		io_out_ot_desc.bTerminalID = i++;
819 	if (EPIN_EN(opts))
820 		usb_in_ot_desc.bTerminalID = i++;
821 	if (FUOUT_EN(opts))
822 		out_feature_unit_desc->bUnitID = i++;
823 	if (FUIN_EN(opts))
824 		in_feature_unit_desc->bUnitID = i++;
825 	if (EPOUT_EN(opts))
826 		out_clk_src_desc.bClockID = i++;
827 	if (EPIN_EN(opts))
828 		in_clk_src_desc.bClockID = i++;
829 
830 	usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
831 
832 	if (FUIN_EN(opts)) {
833 		usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
834 		in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
835 	} else {
836 		usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
837 	}
838 
839 	usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
840 	io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
841 	io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
842 
843 	if (FUOUT_EN(opts)) {
844 		io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
845 		out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
846 	} else {
847 		io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
848 	}
849 
850 	as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
851 	as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
852 
853 	iad_desc.bInterfaceCount = 1;
854 	ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
855 
856 	if (EPIN_EN(opts)) {
857 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
858 
859 		len += sizeof(in_clk_src_desc);
860 		len += sizeof(usb_in_ot_desc);
861 
862 		if (FUIN_EN(opts))
863 			len += in_feature_unit_desc->bLength;
864 
865 		len += sizeof(io_in_it_desc);
866 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
867 		iad_desc.bInterfaceCount++;
868 	}
869 	if (EPOUT_EN(opts)) {
870 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
871 
872 		len += sizeof(out_clk_src_desc);
873 		len += sizeof(usb_out_it_desc);
874 
875 		if (FUOUT_EN(opts))
876 			len += out_feature_unit_desc->bLength;
877 
878 		len += sizeof(io_out_ot_desc);
879 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
880 		iad_desc.bInterfaceCount++;
881 	}
882 
883 	setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
884 	setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
885 	setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
886 }
887 
888 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
889 {
890 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
891 
892 	if (!opts->p_chmask && !opts->c_chmask) {
893 		dev_err(dev, "Error: no playback and capture channels\n");
894 		return -EINVAL;
895 	} else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) {
896 		dev_err(dev, "Error: unsupported playback channels mask\n");
897 		return -EINVAL;
898 	} else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) {
899 		dev_err(dev, "Error: unsupported capture channels mask\n");
900 		return -EINVAL;
901 	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
902 		dev_err(dev, "Error: incorrect playback sample size\n");
903 		return -EINVAL;
904 	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
905 		dev_err(dev, "Error: incorrect capture sample size\n");
906 		return -EINVAL;
907 	} else if (!opts->p_srate) {
908 		dev_err(dev, "Error: incorrect playback sampling rate\n");
909 		return -EINVAL;
910 	} else if (!opts->c_srate) {
911 		dev_err(dev, "Error: incorrect capture sampling rate\n");
912 		return -EINVAL;
913 	}
914 
915 	if (opts->p_volume_max <= opts->p_volume_min) {
916 		dev_err(dev, "Error: incorrect playback volume max/min\n");
917 			return -EINVAL;
918 	} else if (opts->c_volume_max <= opts->c_volume_min) {
919 		dev_err(dev, "Error: incorrect capture volume max/min\n");
920 			return -EINVAL;
921 	} else if (opts->p_volume_res <= 0) {
922 		dev_err(dev, "Error: negative/zero playback volume resolution\n");
923 			return -EINVAL;
924 	} else if (opts->c_volume_res <= 0) {
925 		dev_err(dev, "Error: negative/zero capture volume resolution\n");
926 			return -EINVAL;
927 	}
928 
929 	if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
930 		dev_err(dev, "Error: incorrect playback volume resolution\n");
931 			return -EINVAL;
932 	} else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
933 		dev_err(dev, "Error: incorrect capture volume resolution\n");
934 			return -EINVAL;
935 	}
936 
937 	return 0;
938 }
939 
940 static int
941 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
942 {
943 	struct f_uac2 *uac2 = func_to_uac2(fn);
944 	struct g_audio *agdev = func_to_g_audio(fn);
945 	struct usb_composite_dev *cdev = cfg->cdev;
946 	struct usb_gadget *gadget = cdev->gadget;
947 	struct device *dev = &gadget->dev;
948 	struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
949 	struct usb_string *us;
950 	int ret;
951 
952 	ret = afunc_validate_opts(agdev, dev);
953 	if (ret)
954 		return ret;
955 
956 	us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
957 	if (IS_ERR(us))
958 		return PTR_ERR(us);
959 
960 	if (FUOUT_EN(uac2_opts)) {
961 		out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
962 		if (!out_feature_unit_desc)
963 			return -ENOMEM;
964 	}
965 	if (FUIN_EN(uac2_opts)) {
966 		in_feature_unit_desc = build_fu_desc(uac2_opts->p_chmask);
967 		if (!in_feature_unit_desc) {
968 			ret = -ENOMEM;
969 			goto err_free_fu;
970 		}
971 	}
972 
973 	iad_desc.iFunction = us[STR_ASSOC].id;
974 	std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
975 	in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
976 	out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
977 	usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
978 	io_in_it_desc.iTerminal = us[STR_IO_IT].id;
979 	usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
980 	io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
981 	std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
982 	std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
983 	std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
984 	std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
985 
986 	if (FUOUT_EN(uac2_opts)) {
987 		u8 *i_feature = (u8 *)out_feature_unit_desc +
988 				out_feature_unit_desc->bLength - 1;
989 		*i_feature = us[STR_FU_OUT].id;
990 	}
991 	if (FUIN_EN(uac2_opts)) {
992 		u8 *i_feature = (u8 *)in_feature_unit_desc +
993 				in_feature_unit_desc->bLength - 1;
994 		*i_feature = us[STR_FU_IN].id;
995 	}
996 
997 
998 	/* Initialize the configurable parameters */
999 	usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1000 	usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1001 	io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1002 	io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1003 	as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1004 	as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1005 	as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1006 	as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1007 	as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
1008 	as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
1009 	as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
1010 	as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
1011 	if (FUOUT_EN(uac2_opts)) {
1012 		__le32 *bma = (__le32 *)&out_feature_unit_desc->bmaControls[0];
1013 		u32 control = 0;
1014 
1015 		if (uac2_opts->c_mute_present)
1016 			control |= CONTROL_RDWR << FU_MUTE_CTRL;
1017 		if (uac2_opts->c_volume_present)
1018 			control |= CONTROL_RDWR << FU_VOL_CTRL;
1019 		*bma = cpu_to_le32(control);
1020 	}
1021 	if (FUIN_EN(uac2_opts)) {
1022 		__le32 *bma = (__le32 *)&in_feature_unit_desc->bmaControls[0];
1023 		u32 control = 0;
1024 
1025 		if (uac2_opts->p_mute_present)
1026 			control |= CONTROL_RDWR << FU_MUTE_CTRL;
1027 		if (uac2_opts->p_volume_present)
1028 			control |= CONTROL_RDWR << FU_VOL_CTRL;
1029 		*bma = cpu_to_le32(control);
1030 	}
1031 
1032 	snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
1033 	snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
1034 
1035 	ret = usb_interface_id(cfg, fn);
1036 	if (ret < 0) {
1037 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1038 		goto err_free_fu;
1039 	}
1040 	iad_desc.bFirstInterface = ret;
1041 
1042 	std_ac_if_desc.bInterfaceNumber = ret;
1043 	uac2->ac_intf = ret;
1044 	uac2->ac_alt = 0;
1045 
1046 	if (EPOUT_EN(uac2_opts)) {
1047 		ret = usb_interface_id(cfg, fn);
1048 		if (ret < 0) {
1049 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1050 			goto err_free_fu;
1051 		}
1052 		std_as_out_if0_desc.bInterfaceNumber = ret;
1053 		std_as_out_if1_desc.bInterfaceNumber = ret;
1054 		uac2->as_out_intf = ret;
1055 		uac2->as_out_alt = 0;
1056 
1057 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1058 			fs_epout_desc.bmAttributes =
1059 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1060 			hs_epout_desc.bmAttributes =
1061 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1062 			ss_epout_desc.bmAttributes =
1063 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1064 			std_as_out_if1_desc.bNumEndpoints++;
1065 		} else {
1066 			fs_epout_desc.bmAttributes =
1067 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1068 			hs_epout_desc.bmAttributes =
1069 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1070 			ss_epout_desc.bmAttributes =
1071 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1072 		}
1073 	}
1074 
1075 	if (EPIN_EN(uac2_opts)) {
1076 		ret = usb_interface_id(cfg, fn);
1077 		if (ret < 0) {
1078 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1079 			goto err_free_fu;
1080 		}
1081 		std_as_in_if0_desc.bInterfaceNumber = ret;
1082 		std_as_in_if1_desc.bInterfaceNumber = ret;
1083 		uac2->as_in_intf = ret;
1084 		uac2->as_in_alt = 0;
1085 	}
1086 
1087 	if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts)) {
1088 		uac2->int_ep = usb_ep_autoconfig(gadget, &fs_ep_int_desc);
1089 		if (!uac2->int_ep) {
1090 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1091 			ret = -ENODEV;
1092 			goto err_free_fu;
1093 		}
1094 
1095 		std_ac_if_desc.bNumEndpoints = 1;
1096 	}
1097 
1098 	/* Calculate wMaxPacketSize according to audio bandwidth */
1099 	ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
1100 				     true);
1101 	if (ret < 0) {
1102 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1103 		return ret;
1104 	}
1105 
1106 	ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
1107 				     false);
1108 	if (ret < 0) {
1109 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1110 		return ret;
1111 	}
1112 
1113 	ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
1114 				     true);
1115 	if (ret < 0) {
1116 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1117 		return ret;
1118 	}
1119 
1120 	ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
1121 				     false);
1122 	if (ret < 0) {
1123 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1124 		return ret;
1125 	}
1126 
1127 	ret = set_ep_max_packet_size(uac2_opts, &ss_epin_desc, USB_SPEED_SUPER,
1128 				     true);
1129 	if (ret < 0) {
1130 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1131 		return ret;
1132 	}
1133 
1134 	ret = set_ep_max_packet_size(uac2_opts, &ss_epout_desc, USB_SPEED_SUPER,
1135 				     false);
1136 	if (ret < 0) {
1137 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1138 		return ret;
1139 	}
1140 
1141 	if (EPOUT_EN(uac2_opts)) {
1142 		agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1143 		if (!agdev->out_ep) {
1144 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1145 			ret = -ENODEV;
1146 			goto err_free_fu;
1147 		}
1148 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1149 			agdev->in_ep_fback = usb_ep_autoconfig(gadget,
1150 						       &fs_epin_fback_desc);
1151 			if (!agdev->in_ep_fback) {
1152 				dev_err(dev, "%s:%d Error!\n",
1153 					__func__, __LINE__);
1154 				ret = -ENODEV;
1155 				goto err_free_fu;
1156 			}
1157 		}
1158 	}
1159 
1160 	if (EPIN_EN(uac2_opts)) {
1161 		agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1162 		if (!agdev->in_ep) {
1163 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1164 			ret = -ENODEV;
1165 			goto err_free_fu;
1166 		}
1167 	}
1168 
1169 	agdev->in_ep_maxpsize = max_t(u16,
1170 				le16_to_cpu(fs_epin_desc.wMaxPacketSize),
1171 				le16_to_cpu(hs_epin_desc.wMaxPacketSize));
1172 	agdev->out_ep_maxpsize = max_t(u16,
1173 				le16_to_cpu(fs_epout_desc.wMaxPacketSize),
1174 				le16_to_cpu(hs_epout_desc.wMaxPacketSize));
1175 
1176 	agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
1177 				le16_to_cpu(ss_epin_desc.wMaxPacketSize));
1178 	agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
1179 				le16_to_cpu(ss_epout_desc.wMaxPacketSize));
1180 
1181 	ss_epin_desc_comp.wBytesPerInterval = ss_epin_desc.wMaxPacketSize;
1182 	ss_epout_desc_comp.wBytesPerInterval = ss_epout_desc.wMaxPacketSize;
1183 
1184 	// HS and SS endpoint addresses are copied from autoconfigured FS descriptors
1185 	hs_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1186 	hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1187 	hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1188 	hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1189 	ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1190 	ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1191 	ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1192 	ss_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1193 
1194 	setup_descriptor(uac2_opts);
1195 
1196 	ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
1197 				     ss_audio_desc);
1198 	if (ret)
1199 		goto err_free_fu;
1200 
1201 	agdev->gadget = gadget;
1202 
1203 	agdev->params.p_chmask = uac2_opts->p_chmask;
1204 	agdev->params.p_srate = uac2_opts->p_srate;
1205 	agdev->params.p_ssize = uac2_opts->p_ssize;
1206 	if (FUIN_EN(uac2_opts)) {
1207 		agdev->params.p_fu.id = USB_IN_FU_ID;
1208 		agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
1209 		agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
1210 		agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
1211 		agdev->params.p_fu.volume_max = uac2_opts->p_volume_max;
1212 		agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
1213 	}
1214 	agdev->params.c_chmask = uac2_opts->c_chmask;
1215 	agdev->params.c_srate = uac2_opts->c_srate;
1216 	agdev->params.c_ssize = uac2_opts->c_ssize;
1217 	if (FUOUT_EN(uac2_opts)) {
1218 		agdev->params.c_fu.id = USB_OUT_FU_ID;
1219 		agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
1220 		agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
1221 		agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
1222 		agdev->params.c_fu.volume_max = uac2_opts->c_volume_max;
1223 		agdev->params.c_fu.volume_res = uac2_opts->c_volume_res;
1224 	}
1225 	agdev->params.req_number = uac2_opts->req_number;
1226 	agdev->params.fb_max = uac2_opts->fb_max;
1227 
1228 	if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts))
1229     agdev->notify = afunc_notify;
1230 
1231 	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
1232 	if (ret)
1233 		goto err_free_descs;
1234 
1235 	return 0;
1236 
1237 err_free_descs:
1238 	usb_free_all_descriptors(fn);
1239 	agdev->gadget = NULL;
1240 err_free_fu:
1241 	kfree(out_feature_unit_desc);
1242 	out_feature_unit_desc = NULL;
1243 	kfree(in_feature_unit_desc);
1244 	in_feature_unit_desc = NULL;
1245 	return ret;
1246 }
1247 
1248 static void
1249 afunc_notify_complete(struct usb_ep *_ep, struct usb_request *req)
1250 {
1251 	struct g_audio *agdev = req->context;
1252 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1253 
1254 	atomic_dec(&uac2->int_count);
1255 	kfree(req->buf);
1256 	usb_ep_free_request(_ep, req);
1257 }
1258 
1259 static int
1260 afunc_notify(struct g_audio *agdev, int unit_id, int cs)
1261 {
1262 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1263 	struct usb_request *req;
1264 	struct uac2_interrupt_data_msg *msg;
1265 	u16 w_index, w_value;
1266 	int ret;
1267 
1268 	if (!uac2->int_ep->enabled)
1269 		return 0;
1270 
1271 	if (atomic_inc_return(&uac2->int_count) > UAC2_DEF_INT_REQ_NUM) {
1272 		atomic_dec(&uac2->int_count);
1273 		return 0;
1274 	}
1275 
1276 	req = usb_ep_alloc_request(uac2->int_ep, GFP_ATOMIC);
1277 	if (req == NULL) {
1278 		ret = -ENOMEM;
1279 		goto err_dec_int_count;
1280 	}
1281 
1282 	msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
1283 	if (msg == NULL) {
1284 		ret = -ENOMEM;
1285 		goto err_free_request;
1286 	}
1287 
1288 	w_index = unit_id << 8 | uac2->ac_intf;
1289 	w_value = cs << 8;
1290 
1291 	msg->bInfo = 0; /* Non-vendor, interface interrupt */
1292 	msg->bAttribute = UAC2_CS_CUR;
1293 	msg->wIndex = cpu_to_le16(w_index);
1294 	msg->wValue = cpu_to_le16(w_value);
1295 
1296 	req->length = sizeof(*msg);
1297 	req->buf = msg;
1298 	req->context = agdev;
1299 	req->complete = afunc_notify_complete;
1300 
1301 	ret = usb_ep_queue(uac2->int_ep, req, GFP_ATOMIC);
1302 
1303 	if (ret)
1304 		goto err_free_msg;
1305 
1306 	return 0;
1307 
1308 err_free_msg:
1309 	kfree(msg);
1310 err_free_request:
1311 	usb_ep_free_request(uac2->int_ep, req);
1312 err_dec_int_count:
1313 	atomic_dec(&uac2->int_count);
1314 
1315 	return ret;
1316 }
1317 
1318 static int
1319 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1320 {
1321 	struct usb_composite_dev *cdev = fn->config->cdev;
1322 	struct f_uac2 *uac2 = func_to_uac2(fn);
1323 	struct g_audio *agdev = func_to_g_audio(fn);
1324 	struct usb_gadget *gadget = cdev->gadget;
1325 	struct device *dev = &gadget->dev;
1326 	int ret = 0;
1327 
1328 	/* No i/f has more than 2 alt settings */
1329 	if (alt > 1) {
1330 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1331 		return -EINVAL;
1332 	}
1333 
1334 	if (intf == uac2->ac_intf) {
1335 		/* Control I/f has only 1 AltSetting - 0 */
1336 		if (alt) {
1337 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1338 			return -EINVAL;
1339 		}
1340 
1341 		/* restart interrupt endpoint */
1342 		if (uac2->int_ep) {
1343 			usb_ep_disable(uac2->int_ep);
1344 			config_ep_by_speed(gadget, &agdev->func, uac2->int_ep);
1345 			usb_ep_enable(uac2->int_ep);
1346 		}
1347 
1348 		return 0;
1349 	}
1350 
1351 	if (intf == uac2->as_out_intf) {
1352 		uac2->as_out_alt = alt;
1353 
1354 		if (alt)
1355 			ret = u_audio_start_capture(&uac2->g_audio);
1356 		else
1357 			u_audio_stop_capture(&uac2->g_audio);
1358 	} else if (intf == uac2->as_in_intf) {
1359 		uac2->as_in_alt = alt;
1360 
1361 		if (alt)
1362 			ret = u_audio_start_playback(&uac2->g_audio);
1363 		else
1364 			u_audio_stop_playback(&uac2->g_audio);
1365 	} else {
1366 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1367 		return -EINVAL;
1368 	}
1369 
1370 	return ret;
1371 }
1372 
1373 static int
1374 afunc_get_alt(struct usb_function *fn, unsigned intf)
1375 {
1376 	struct f_uac2 *uac2 = func_to_uac2(fn);
1377 	struct g_audio *agdev = func_to_g_audio(fn);
1378 
1379 	if (intf == uac2->ac_intf)
1380 		return uac2->ac_alt;
1381 	else if (intf == uac2->as_out_intf)
1382 		return uac2->as_out_alt;
1383 	else if (intf == uac2->as_in_intf)
1384 		return uac2->as_in_alt;
1385 	else
1386 		dev_err(&agdev->gadget->dev,
1387 			"%s:%d Invalid Interface %d!\n",
1388 			__func__, __LINE__, intf);
1389 
1390 	return -EINVAL;
1391 }
1392 
1393 static void
1394 afunc_disable(struct usb_function *fn)
1395 {
1396 	struct f_uac2 *uac2 = func_to_uac2(fn);
1397 
1398 	uac2->as_in_alt = 0;
1399 	uac2->as_out_alt = 0;
1400 	u_audio_stop_capture(&uac2->g_audio);
1401 	u_audio_stop_playback(&uac2->g_audio);
1402 	if (uac2->int_ep)
1403 		usb_ep_disable(uac2->int_ep);
1404 }
1405 
1406 static int
1407 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1408 {
1409 	struct usb_request *req = fn->config->cdev->req;
1410 	struct g_audio *agdev = func_to_g_audio(fn);
1411 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1412 	u16 w_length = le16_to_cpu(cr->wLength);
1413 	u16 w_index = le16_to_cpu(cr->wIndex);
1414 	u16 w_value = le16_to_cpu(cr->wValue);
1415 	u8 entity_id = (w_index >> 8) & 0xff;
1416 	u8 control_selector = w_value >> 8;
1417 	int value = -EOPNOTSUPP;
1418 	int p_srate, c_srate;
1419 
1420 	p_srate = opts->p_srate;
1421 	c_srate = opts->c_srate;
1422 
1423 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1424 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1425 			struct cntrl_cur_lay3 c;
1426 
1427 			memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1428 
1429 			if (entity_id == USB_IN_CLK_ID)
1430 				c.dCUR = cpu_to_le32(p_srate);
1431 			else if (entity_id == USB_OUT_CLK_ID)
1432 				c.dCUR = cpu_to_le32(c_srate);
1433 
1434 			value = min_t(unsigned int, w_length, sizeof(c));
1435 			memcpy(req->buf, &c, value);
1436 		} else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1437 			*(u8 *)req->buf = 1;
1438 			value = min_t(unsigned int, w_length, 1);
1439 		} else {
1440 			dev_err(&agdev->gadget->dev,
1441 				"%s:%d control_selector=%d TODO!\n",
1442 				__func__, __LINE__, control_selector);
1443 		}
1444 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1445 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1446 		unsigned int is_playback = 0;
1447 
1448 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1449 			is_playback = 1;
1450 
1451 		if (control_selector == UAC_FU_MUTE) {
1452 			unsigned int mute;
1453 
1454 			u_audio_get_mute(agdev, is_playback, &mute);
1455 
1456 			*(u8 *)req->buf = mute;
1457 			value = min_t(unsigned int, w_length, 1);
1458 		} else if (control_selector == UAC_FU_VOLUME) {
1459 			struct cntrl_cur_lay2 c;
1460 			s16 volume;
1461 
1462 			memset(&c, 0, sizeof(struct cntrl_cur_lay2));
1463 
1464 			u_audio_get_volume(agdev, is_playback, &volume);
1465 			c.wCUR = cpu_to_le16(volume);
1466 
1467 			value = min_t(unsigned int, w_length, sizeof(c));
1468 			memcpy(req->buf, &c, value);
1469 		} else {
1470 			dev_err(&agdev->gadget->dev,
1471 				"%s:%d control_selector=%d TODO!\n",
1472 				__func__, __LINE__, control_selector);
1473 		}
1474 	} else {
1475 		dev_err(&agdev->gadget->dev,
1476 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1477 			__func__, __LINE__, entity_id, control_selector);
1478 	}
1479 
1480 	return value;
1481 }
1482 
1483 static int
1484 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1485 {
1486 	struct usb_request *req = fn->config->cdev->req;
1487 	struct g_audio *agdev = func_to_g_audio(fn);
1488 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1489 	u16 w_length = le16_to_cpu(cr->wLength);
1490 	u16 w_index = le16_to_cpu(cr->wIndex);
1491 	u16 w_value = le16_to_cpu(cr->wValue);
1492 	u8 entity_id = (w_index >> 8) & 0xff;
1493 	u8 control_selector = w_value >> 8;
1494 	int value = -EOPNOTSUPP;
1495 	int p_srate, c_srate;
1496 
1497 	p_srate = opts->p_srate;
1498 	c_srate = opts->c_srate;
1499 
1500 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1501 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1502 			struct cntrl_range_lay3 r;
1503 
1504 			if (entity_id == USB_IN_CLK_ID)
1505 				r.dMIN = cpu_to_le32(p_srate);
1506 			else if (entity_id == USB_OUT_CLK_ID)
1507 				r.dMIN = cpu_to_le32(c_srate);
1508 			else
1509 				return -EOPNOTSUPP;
1510 
1511 			r.dMAX = r.dMIN;
1512 			r.dRES = 0;
1513 			r.wNumSubRanges = cpu_to_le16(1);
1514 
1515 			value = min_t(unsigned int, w_length, sizeof(r));
1516 			memcpy(req->buf, &r, value);
1517 		} else {
1518 			dev_err(&agdev->gadget->dev,
1519 				"%s:%d control_selector=%d TODO!\n",
1520 				__func__, __LINE__, control_selector);
1521 		}
1522 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1523 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1524 		unsigned int is_playback = 0;
1525 
1526 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1527 			is_playback = 1;
1528 
1529 		if (control_selector == UAC_FU_VOLUME) {
1530 			struct cntrl_range_lay2 r;
1531 			s16 max_db, min_db, res_db;
1532 
1533 			if (is_playback) {
1534 				max_db = opts->p_volume_max;
1535 				min_db = opts->p_volume_min;
1536 				res_db = opts->p_volume_res;
1537 			} else {
1538 				max_db = opts->c_volume_max;
1539 				min_db = opts->c_volume_min;
1540 				res_db = opts->c_volume_res;
1541 			}
1542 
1543 			r.wMAX = cpu_to_le16(max_db);
1544 			r.wMIN = cpu_to_le16(min_db);
1545 			r.wRES = cpu_to_le16(res_db);
1546 			r.wNumSubRanges = cpu_to_le16(1);
1547 
1548 			value = min_t(unsigned int, w_length, sizeof(r));
1549 			memcpy(req->buf, &r, value);
1550 		} else {
1551 			dev_err(&agdev->gadget->dev,
1552 				"%s:%d control_selector=%d TODO!\n",
1553 				__func__, __LINE__, control_selector);
1554 		}
1555 	} else {
1556 		dev_err(&agdev->gadget->dev,
1557 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1558 			__func__, __LINE__, entity_id, control_selector);
1559 	}
1560 
1561 	return value;
1562 }
1563 
1564 static int
1565 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1566 {
1567 	if (cr->bRequest == UAC2_CS_CUR)
1568 		return in_rq_cur(fn, cr);
1569 	else if (cr->bRequest == UAC2_CS_RANGE)
1570 		return in_rq_range(fn, cr);
1571 	else
1572 		return -EOPNOTSUPP;
1573 }
1574 
1575 static void
1576 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
1577 {
1578 	struct g_audio *agdev = req->context;
1579 	struct usb_composite_dev *cdev = agdev->func.config->cdev;
1580 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1581 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1582 	struct usb_ctrlrequest *cr = &uac2->setup_cr;
1583 	u16 w_index = le16_to_cpu(cr->wIndex);
1584 	u16 w_value = le16_to_cpu(cr->wValue);
1585 	u8 entity_id = (w_index >> 8) & 0xff;
1586 	u8 control_selector = w_value >> 8;
1587 
1588 	if (req->status != 0) {
1589 		dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
1590 		return;
1591 	}
1592 
1593 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1594 		(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1595 		unsigned int is_playback = 0;
1596 
1597 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1598 			is_playback = 1;
1599 
1600 		if (control_selector == UAC_FU_MUTE) {
1601 			u8 mute = *(u8 *)req->buf;
1602 
1603 			u_audio_set_mute(agdev, is_playback, mute);
1604 
1605 			return;
1606 		} else if (control_selector == UAC_FU_VOLUME) {
1607 			struct cntrl_cur_lay2 *c = req->buf;
1608 			s16 volume;
1609 
1610 			volume = le16_to_cpu(c->wCUR);
1611 			u_audio_set_volume(agdev, is_playback, volume);
1612 
1613 			return;
1614 		} else {
1615 			dev_err(&agdev->gadget->dev,
1616 				"%s:%d control_selector=%d TODO!\n",
1617 				__func__, __LINE__, control_selector);
1618 			usb_ep_set_halt(ep);
1619 		}
1620 	}
1621 }
1622 
1623 static int
1624 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1625 {
1626 	struct usb_request *req = fn->config->cdev->req;
1627 	struct g_audio *agdev = func_to_g_audio(fn);
1628 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1629 	struct f_uac2 *uac2 = func_to_uac2(fn);
1630 	u16 w_length = le16_to_cpu(cr->wLength);
1631 	u16 w_index = le16_to_cpu(cr->wIndex);
1632 	u16 w_value = le16_to_cpu(cr->wValue);
1633 	u8 entity_id = (w_index >> 8) & 0xff;
1634 	u8 control_selector = w_value >> 8;
1635 
1636 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1637 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1638 			return w_length;
1639 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1640 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1641 		memcpy(&uac2->setup_cr, cr, sizeof(*cr));
1642 		req->context = agdev;
1643 		req->complete = out_rq_cur_complete;
1644 
1645 		return w_length;
1646 	} else {
1647 		dev_err(&agdev->gadget->dev,
1648 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1649 			__func__, __LINE__, entity_id, control_selector);
1650 	}
1651 	return -EOPNOTSUPP;
1652 }
1653 
1654 static int
1655 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1656 {
1657 	struct f_uac2 *uac2 = func_to_uac2(fn);
1658 	struct g_audio *agdev = func_to_g_audio(fn);
1659 	u16 w_index = le16_to_cpu(cr->wIndex);
1660 	u8 intf = w_index & 0xff;
1661 
1662 	if (intf != uac2->ac_intf) {
1663 		dev_err(&agdev->gadget->dev,
1664 			"%s:%d Error!\n", __func__, __LINE__);
1665 		return -EOPNOTSUPP;
1666 	}
1667 
1668 	if (cr->bRequestType & USB_DIR_IN)
1669 		return ac_rq_in(fn, cr);
1670 	else if (cr->bRequest == UAC2_CS_CUR)
1671 		return out_rq_cur(fn, cr);
1672 
1673 	return -EOPNOTSUPP;
1674 }
1675 
1676 static int
1677 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1678 {
1679 	struct usb_composite_dev *cdev = fn->config->cdev;
1680 	struct g_audio *agdev = func_to_g_audio(fn);
1681 	struct usb_request *req = cdev->req;
1682 	u16 w_length = le16_to_cpu(cr->wLength);
1683 	int value = -EOPNOTSUPP;
1684 
1685 	/* Only Class specific requests are supposed to reach here */
1686 	if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1687 		return -EOPNOTSUPP;
1688 
1689 	if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1690 		value = setup_rq_inf(fn, cr);
1691 	else
1692 		dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1693 				__func__, __LINE__);
1694 
1695 	if (value >= 0) {
1696 		req->length = value;
1697 		req->zero = value < w_length;
1698 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1699 		if (value < 0) {
1700 			dev_err(&agdev->gadget->dev,
1701 				"%s:%d Error!\n", __func__, __LINE__);
1702 			req->status = 0;
1703 		}
1704 	}
1705 
1706 	return value;
1707 }
1708 
1709 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1710 {
1711 	return container_of(to_config_group(item), struct f_uac2_opts,
1712 			    func_inst.group);
1713 }
1714 
1715 static void f_uac2_attr_release(struct config_item *item)
1716 {
1717 	struct f_uac2_opts *opts = to_f_uac2_opts(item);
1718 
1719 	usb_put_function_instance(&opts->func_inst);
1720 }
1721 
1722 static struct configfs_item_operations f_uac2_item_ops = {
1723 	.release	= f_uac2_attr_release,
1724 };
1725 
1726 #define uac2_kstrtou32 kstrtou32
1727 #define uac2_kstrtos16 kstrtos16
1728 #define uac2_kstrtobool(s, base, res) kstrtobool((s), (res))
1729 
1730 static const char *u32_fmt = "%u\n";
1731 static const char *s16_fmt = "%hd\n";
1732 static const char *bool_fmt = "%u\n";
1733 
1734 #define UAC2_ATTRIBUTE(type, name)					\
1735 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1736 					 char *page)			\
1737 {									\
1738 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1739 	int result;							\
1740 									\
1741 	mutex_lock(&opts->lock);					\
1742 	result = sprintf(page, type##_fmt, opts->name);			\
1743 	mutex_unlock(&opts->lock);					\
1744 									\
1745 	return result;							\
1746 }									\
1747 									\
1748 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1749 					  const char *page, size_t len)	\
1750 {									\
1751 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1752 	int ret;							\
1753 	type num;							\
1754 									\
1755 	mutex_lock(&opts->lock);					\
1756 	if (opts->refcnt) {						\
1757 		ret = -EBUSY;						\
1758 		goto end;						\
1759 	}								\
1760 									\
1761 	ret = uac2_kstrto##type(page, 0, &num);				\
1762 	if (ret)							\
1763 		goto end;						\
1764 									\
1765 	opts->name = num;						\
1766 	ret = len;							\
1767 									\
1768 end:									\
1769 	mutex_unlock(&opts->lock);					\
1770 	return ret;							\
1771 }									\
1772 									\
1773 CONFIGFS_ATTR(f_uac2_opts_, name)
1774 
1775 #define UAC2_ATTRIBUTE_SYNC(name)					\
1776 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1777 					 char *page)			\
1778 {									\
1779 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1780 	int result;							\
1781 	char *str;							\
1782 									\
1783 	mutex_lock(&opts->lock);					\
1784 	switch (opts->name) {						\
1785 	case USB_ENDPOINT_SYNC_ASYNC:					\
1786 		str = "async";						\
1787 		break;							\
1788 	case USB_ENDPOINT_SYNC_ADAPTIVE:				\
1789 		str = "adaptive";					\
1790 		break;							\
1791 	default:							\
1792 		str = "unknown";					\
1793 		break;							\
1794 	}								\
1795 	result = sprintf(page, "%s\n", str);				\
1796 	mutex_unlock(&opts->lock);					\
1797 									\
1798 	return result;							\
1799 }									\
1800 									\
1801 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1802 					  const char *page, size_t len)	\
1803 {									\
1804 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1805 	int ret = 0;							\
1806 									\
1807 	mutex_lock(&opts->lock);					\
1808 	if (opts->refcnt) {						\
1809 		ret = -EBUSY;						\
1810 		goto end;						\
1811 	}								\
1812 									\
1813 	if (!strncmp(page, "async", 5))					\
1814 		opts->name = USB_ENDPOINT_SYNC_ASYNC;			\
1815 	else if (!strncmp(page, "adaptive", 8))				\
1816 		opts->name = USB_ENDPOINT_SYNC_ADAPTIVE;		\
1817 	else {								\
1818 		ret = -EINVAL;						\
1819 		goto end;						\
1820 	}								\
1821 									\
1822 	ret = len;							\
1823 									\
1824 end:									\
1825 	mutex_unlock(&opts->lock);					\
1826 	return ret;							\
1827 }									\
1828 									\
1829 CONFIGFS_ATTR(f_uac2_opts_, name)
1830 
1831 UAC2_ATTRIBUTE(u32, p_chmask);
1832 UAC2_ATTRIBUTE(u32, p_srate);
1833 UAC2_ATTRIBUTE(u32, p_ssize);
1834 UAC2_ATTRIBUTE(u32, c_chmask);
1835 UAC2_ATTRIBUTE(u32, c_srate);
1836 UAC2_ATTRIBUTE_SYNC(c_sync);
1837 UAC2_ATTRIBUTE(u32, c_ssize);
1838 UAC2_ATTRIBUTE(u32, req_number);
1839 
1840 UAC2_ATTRIBUTE(bool, p_mute_present);
1841 UAC2_ATTRIBUTE(bool, p_volume_present);
1842 UAC2_ATTRIBUTE(s16, p_volume_min);
1843 UAC2_ATTRIBUTE(s16, p_volume_max);
1844 UAC2_ATTRIBUTE(s16, p_volume_res);
1845 
1846 UAC2_ATTRIBUTE(bool, c_mute_present);
1847 UAC2_ATTRIBUTE(bool, c_volume_present);
1848 UAC2_ATTRIBUTE(s16, c_volume_min);
1849 UAC2_ATTRIBUTE(s16, c_volume_max);
1850 UAC2_ATTRIBUTE(s16, c_volume_res);
1851 UAC2_ATTRIBUTE(u32, fb_max);
1852 
1853 static struct configfs_attribute *f_uac2_attrs[] = {
1854 	&f_uac2_opts_attr_p_chmask,
1855 	&f_uac2_opts_attr_p_srate,
1856 	&f_uac2_opts_attr_p_ssize,
1857 	&f_uac2_opts_attr_c_chmask,
1858 	&f_uac2_opts_attr_c_srate,
1859 	&f_uac2_opts_attr_c_ssize,
1860 	&f_uac2_opts_attr_c_sync,
1861 	&f_uac2_opts_attr_req_number,
1862 	&f_uac2_opts_attr_fb_max,
1863 
1864 	&f_uac2_opts_attr_p_mute_present,
1865 	&f_uac2_opts_attr_p_volume_present,
1866 	&f_uac2_opts_attr_p_volume_min,
1867 	&f_uac2_opts_attr_p_volume_max,
1868 	&f_uac2_opts_attr_p_volume_res,
1869 
1870 	&f_uac2_opts_attr_c_mute_present,
1871 	&f_uac2_opts_attr_c_volume_present,
1872 	&f_uac2_opts_attr_c_volume_min,
1873 	&f_uac2_opts_attr_c_volume_max,
1874 	&f_uac2_opts_attr_c_volume_res,
1875 
1876 	NULL,
1877 };
1878 
1879 static const struct config_item_type f_uac2_func_type = {
1880 	.ct_item_ops	= &f_uac2_item_ops,
1881 	.ct_attrs	= f_uac2_attrs,
1882 	.ct_owner	= THIS_MODULE,
1883 };
1884 
1885 static void afunc_free_inst(struct usb_function_instance *f)
1886 {
1887 	struct f_uac2_opts *opts;
1888 
1889 	opts = container_of(f, struct f_uac2_opts, func_inst);
1890 	kfree(opts);
1891 }
1892 
1893 static struct usb_function_instance *afunc_alloc_inst(void)
1894 {
1895 	struct f_uac2_opts *opts;
1896 
1897 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1898 	if (!opts)
1899 		return ERR_PTR(-ENOMEM);
1900 
1901 	mutex_init(&opts->lock);
1902 	opts->func_inst.free_func_inst = afunc_free_inst;
1903 
1904 	config_group_init_type_name(&opts->func_inst.group, "",
1905 				    &f_uac2_func_type);
1906 
1907 	opts->p_chmask = UAC2_DEF_PCHMASK;
1908 	opts->p_srate = UAC2_DEF_PSRATE;
1909 	opts->p_ssize = UAC2_DEF_PSSIZE;
1910 	opts->c_chmask = UAC2_DEF_CCHMASK;
1911 	opts->c_srate = UAC2_DEF_CSRATE;
1912 	opts->c_ssize = UAC2_DEF_CSSIZE;
1913 	opts->c_sync = UAC2_DEF_CSYNC;
1914 
1915 	opts->p_mute_present = UAC2_DEF_MUTE_PRESENT;
1916 	opts->p_volume_present = UAC2_DEF_VOLUME_PRESENT;
1917 	opts->p_volume_min = UAC2_DEF_MIN_DB;
1918 	opts->p_volume_max = UAC2_DEF_MAX_DB;
1919 	opts->p_volume_res = UAC2_DEF_RES_DB;
1920 
1921 	opts->c_mute_present = UAC2_DEF_MUTE_PRESENT;
1922 	opts->c_volume_present = UAC2_DEF_VOLUME_PRESENT;
1923 	opts->c_volume_min = UAC2_DEF_MIN_DB;
1924 	opts->c_volume_max = UAC2_DEF_MAX_DB;
1925 	opts->c_volume_res = UAC2_DEF_RES_DB;
1926 
1927 	opts->req_number = UAC2_DEF_REQ_NUM;
1928 	opts->fb_max = UAC2_DEF_FB_MAX;
1929 	return &opts->func_inst;
1930 }
1931 
1932 static void afunc_free(struct usb_function *f)
1933 {
1934 	struct g_audio *agdev;
1935 	struct f_uac2_opts *opts;
1936 
1937 	agdev = func_to_g_audio(f);
1938 	opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1939 	kfree(agdev);
1940 	mutex_lock(&opts->lock);
1941 	--opts->refcnt;
1942 	mutex_unlock(&opts->lock);
1943 }
1944 
1945 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1946 {
1947 	struct g_audio *agdev = func_to_g_audio(f);
1948 
1949 	g_audio_cleanup(agdev);
1950 	usb_free_all_descriptors(f);
1951 
1952 	agdev->gadget = NULL;
1953 
1954 	kfree(out_feature_unit_desc);
1955 	out_feature_unit_desc = NULL;
1956 	kfree(in_feature_unit_desc);
1957 	in_feature_unit_desc = NULL;
1958 }
1959 
1960 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1961 {
1962 	struct f_uac2	*uac2;
1963 	struct f_uac2_opts *opts;
1964 
1965 	uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
1966 	if (uac2 == NULL)
1967 		return ERR_PTR(-ENOMEM);
1968 
1969 	opts = container_of(fi, struct f_uac2_opts, func_inst);
1970 	mutex_lock(&opts->lock);
1971 	++opts->refcnt;
1972 	mutex_unlock(&opts->lock);
1973 
1974 	uac2->g_audio.func.name = "uac2_func";
1975 	uac2->g_audio.func.bind = afunc_bind;
1976 	uac2->g_audio.func.unbind = afunc_unbind;
1977 	uac2->g_audio.func.set_alt = afunc_set_alt;
1978 	uac2->g_audio.func.get_alt = afunc_get_alt;
1979 	uac2->g_audio.func.disable = afunc_disable;
1980 	uac2->g_audio.func.setup = afunc_setup;
1981 	uac2->g_audio.func.free_func = afunc_free;
1982 
1983 	return &uac2->g_audio.func;
1984 }
1985 
1986 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1987 MODULE_LICENSE("GPL");
1988 MODULE_AUTHOR("Yadwinder Singh");
1989 MODULE_AUTHOR("Jaswinder Singh");
1990 MODULE_AUTHOR("Ruslan Bilovol");
1991