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 
410 /* Audio Streaming IN Interface - Alt0 */
411 static struct usb_interface_descriptor std_as_in_if0_desc = {
412 	.bLength = sizeof std_as_in_if0_desc,
413 	.bDescriptorType = USB_DT_INTERFACE,
414 
415 	.bAlternateSetting = 0,
416 	.bNumEndpoints = 0,
417 	.bInterfaceClass = USB_CLASS_AUDIO,
418 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
419 	.bInterfaceProtocol = UAC_VERSION_2,
420 };
421 
422 /* Audio Streaming IN Interface - Alt1 */
423 static struct usb_interface_descriptor std_as_in_if1_desc = {
424 	.bLength = sizeof std_as_in_if1_desc,
425 	.bDescriptorType = USB_DT_INTERFACE,
426 
427 	.bAlternateSetting = 1,
428 	.bNumEndpoints = 1,
429 	.bInterfaceClass = USB_CLASS_AUDIO,
430 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
431 	.bInterfaceProtocol = UAC_VERSION_2,
432 };
433 
434 /* Audio Stream IN Intface Desc */
435 static struct uac2_as_header_descriptor as_in_hdr_desc = {
436 	.bLength = sizeof as_in_hdr_desc,
437 	.bDescriptorType = USB_DT_CS_INTERFACE,
438 
439 	.bDescriptorSubtype = UAC_AS_GENERAL,
440 	/* .bTerminalLink = DYNAMIC */
441 	.bmControls = 0,
442 	.bFormatType = UAC_FORMAT_TYPE_I,
443 	.bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
444 	.iChannelNames = 0,
445 };
446 
447 /* Audio USB_IN Format */
448 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
449 	.bLength = sizeof as_in_fmt1_desc,
450 	.bDescriptorType = USB_DT_CS_INTERFACE,
451 	.bDescriptorSubtype = UAC_FORMAT_TYPE,
452 	.bFormatType = UAC_FORMAT_TYPE_I,
453 };
454 
455 /* STD AS ISO IN Endpoint */
456 static struct usb_endpoint_descriptor fs_epin_desc = {
457 	.bLength = USB_DT_ENDPOINT_SIZE,
458 	.bDescriptorType = USB_DT_ENDPOINT,
459 
460 	.bEndpointAddress = USB_DIR_IN,
461 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
462 	/* .wMaxPacketSize = DYNAMIC */
463 	.bInterval = 1,
464 };
465 
466 static struct usb_endpoint_descriptor hs_epin_desc = {
467 	.bLength = USB_DT_ENDPOINT_SIZE,
468 	.bDescriptorType = USB_DT_ENDPOINT,
469 
470 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
471 	/* .wMaxPacketSize = DYNAMIC */
472 	.bInterval = 4,
473 };
474 
475 static struct usb_endpoint_descriptor ss_epin_desc = {
476 	.bLength = USB_DT_ENDPOINT_SIZE,
477 	.bDescriptorType = USB_DT_ENDPOINT,
478 
479 	.bEndpointAddress = USB_DIR_IN,
480 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
481 	/* .wMaxPacketSize = DYNAMIC */
482 	.bInterval = 4,
483 };
484 
485 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
486 	.bLength		= sizeof(ss_epin_desc_comp),
487 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
488 	.bMaxBurst		= 0,
489 	.bmAttributes		= 0,
490 	/* wBytesPerInterval = DYNAMIC */
491 };
492 
493 /* CS AS ISO IN Endpoint */
494 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
495 	.bLength = sizeof as_iso_in_desc,
496 	.bDescriptorType = USB_DT_CS_ENDPOINT,
497 
498 	.bDescriptorSubtype = UAC_EP_GENERAL,
499 	.bmAttributes = 0,
500 	.bmControls = 0,
501 	.bLockDelayUnits = 0,
502 	.wLockDelay = 0,
503 };
504 
505 static struct usb_descriptor_header *fs_audio_desc[] = {
506 	(struct usb_descriptor_header *)&iad_desc,
507 	(struct usb_descriptor_header *)&std_ac_if_desc,
508 
509 	(struct usb_descriptor_header *)&ac_hdr_desc,
510 	(struct usb_descriptor_header *)&in_clk_src_desc,
511 	(struct usb_descriptor_header *)&out_clk_src_desc,
512 	(struct usb_descriptor_header *)&usb_out_it_desc,
513 	(struct usb_descriptor_header *)&out_feature_unit_desc,
514 	(struct usb_descriptor_header *)&io_in_it_desc,
515 	(struct usb_descriptor_header *)&usb_in_ot_desc,
516 	(struct usb_descriptor_header *)&in_feature_unit_desc,
517 	(struct usb_descriptor_header *)&io_out_ot_desc,
518 
519 	(struct usb_descriptor_header *)&fs_ep_int_desc,
520 
521 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
522 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
523 
524 	(struct usb_descriptor_header *)&as_out_hdr_desc,
525 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
526 	(struct usb_descriptor_header *)&fs_epout_desc,
527 	(struct usb_descriptor_header *)&as_iso_out_desc,
528 	(struct usb_descriptor_header *)&fs_epin_fback_desc,
529 
530 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
531 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
532 
533 	(struct usb_descriptor_header *)&as_in_hdr_desc,
534 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
535 	(struct usb_descriptor_header *)&fs_epin_desc,
536 	(struct usb_descriptor_header *)&as_iso_in_desc,
537 	NULL,
538 };
539 
540 static struct usb_descriptor_header *hs_audio_desc[] = {
541 	(struct usb_descriptor_header *)&iad_desc,
542 	(struct usb_descriptor_header *)&std_ac_if_desc,
543 
544 	(struct usb_descriptor_header *)&ac_hdr_desc,
545 	(struct usb_descriptor_header *)&in_clk_src_desc,
546 	(struct usb_descriptor_header *)&out_clk_src_desc,
547 	(struct usb_descriptor_header *)&usb_out_it_desc,
548 	(struct usb_descriptor_header *)&out_feature_unit_desc,
549 	(struct usb_descriptor_header *)&io_in_it_desc,
550 	(struct usb_descriptor_header *)&usb_in_ot_desc,
551 	(struct usb_descriptor_header *)&in_feature_unit_desc,
552 	(struct usb_descriptor_header *)&io_out_ot_desc,
553 
554 	(struct usb_descriptor_header *)&hs_ep_int_desc,
555 
556 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
557 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
558 
559 	(struct usb_descriptor_header *)&as_out_hdr_desc,
560 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
561 	(struct usb_descriptor_header *)&hs_epout_desc,
562 	(struct usb_descriptor_header *)&as_iso_out_desc,
563 	(struct usb_descriptor_header *)&hs_epin_fback_desc,
564 
565 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
566 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
567 
568 	(struct usb_descriptor_header *)&as_in_hdr_desc,
569 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
570 	(struct usb_descriptor_header *)&hs_epin_desc,
571 	(struct usb_descriptor_header *)&as_iso_in_desc,
572 	NULL,
573 };
574 
575 static struct usb_descriptor_header *ss_audio_desc[] = {
576 	(struct usb_descriptor_header *)&iad_desc,
577 	(struct usb_descriptor_header *)&std_ac_if_desc,
578 
579 	(struct usb_descriptor_header *)&ac_hdr_desc,
580 	(struct usb_descriptor_header *)&in_clk_src_desc,
581 	(struct usb_descriptor_header *)&out_clk_src_desc,
582 	(struct usb_descriptor_header *)&usb_out_it_desc,
583   (struct usb_descriptor_header *)&out_feature_unit_desc,
584 	(struct usb_descriptor_header *)&io_in_it_desc,
585 	(struct usb_descriptor_header *)&usb_in_ot_desc,
586 	(struct usb_descriptor_header *)&in_feature_unit_desc,
587 	(struct usb_descriptor_header *)&io_out_ot_desc,
588 
589   (struct usb_descriptor_header *)&ss_ep_int_desc,
590 
591 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
592 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
593 
594 	(struct usb_descriptor_header *)&as_out_hdr_desc,
595 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
596 	(struct usb_descriptor_header *)&ss_epout_desc,
597 	(struct usb_descriptor_header *)&ss_epout_desc_comp,
598 	(struct usb_descriptor_header *)&as_iso_out_desc,
599 	(struct usb_descriptor_header *)&ss_epin_fback_desc,
600 
601 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
602 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
603 
604 	(struct usb_descriptor_header *)&as_in_hdr_desc,
605 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
606 	(struct usb_descriptor_header *)&ss_epin_desc,
607 	(struct usb_descriptor_header *)&ss_epin_desc_comp,
608 	(struct usb_descriptor_header *)&as_iso_in_desc,
609 	NULL,
610 };
611 
612 struct cntrl_cur_lay2 {
613 	__le16	wCUR;
614 };
615 
616 struct cntrl_range_lay2 {
617 	__le16	wNumSubRanges;
618 	__le16	wMIN;
619 	__le16	wMAX;
620 	__le16	wRES;
621 } __packed;
622 
623 struct cntrl_cur_lay3 {
624 	__le32	dCUR;
625 };
626 
627 struct cntrl_range_lay3 {
628 	__le16	wNumSubRanges;
629 	__le32	dMIN;
630 	__le32	dMAX;
631 	__le32	dRES;
632 } __packed;
633 
634 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
635 	struct usb_endpoint_descriptor *ep_desc,
636 	enum usb_device_speed speed, bool is_playback)
637 {
638 	int chmask, srate, ssize;
639 	u16 max_size_bw, max_size_ep;
640 	unsigned int factor;
641 
642 	switch (speed) {
643 	case USB_SPEED_FULL:
644 		max_size_ep = 1023;
645 		factor = 1000;
646 		break;
647 
648 	case USB_SPEED_HIGH:
649 	case USB_SPEED_SUPER:
650 		max_size_ep = 1024;
651 		factor = 8000;
652 		break;
653 
654 	default:
655 		return -EINVAL;
656 	}
657 
658 	if (is_playback) {
659 		chmask = uac2_opts->p_chmask;
660 		srate = uac2_opts->p_srate;
661 		ssize = uac2_opts->p_ssize;
662 	} else {
663 		chmask = uac2_opts->c_chmask;
664 		srate = uac2_opts->c_srate;
665 		ssize = uac2_opts->c_ssize;
666 	}
667 
668 	if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
669 		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
670 
671 	max_size_bw = num_channels(chmask) * ssize *
672 		DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
673 	ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
674 						    max_size_ep));
675 
676 	return 0;
677 }
678 
679 static struct uac2_feature_unit_descriptor *build_fu_desc(int chmask)
680 {
681 	struct uac2_feature_unit_descriptor *fu_desc;
682 	int channels = num_channels(chmask);
683 	int fu_desc_size = UAC2_DT_FEATURE_UNIT_SIZE(channels);
684 
685 	fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
686 	if (!fu_desc)
687 		return NULL;
688 
689 	fu_desc->bLength = fu_desc_size;
690 	fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
691 
692 	fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
693 
694 	/* bUnitID, bSourceID and bmaControls will be defined later */
695 
696 	return fu_desc;
697 }
698 
699 /* Use macro to overcome line length limitation */
700 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
701 
702 static void setup_headers(struct f_uac2_opts *opts,
703 			  struct usb_descriptor_header **headers,
704 			  enum usb_device_speed speed)
705 {
706 	struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
707 	struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
708 	struct usb_endpoint_descriptor *epout_desc;
709 	struct usb_endpoint_descriptor *epin_desc;
710 	struct usb_endpoint_descriptor *epin_fback_desc;
711 	struct usb_endpoint_descriptor *ep_int_desc;
712 	int i;
713 
714 	switch (speed) {
715 	case USB_SPEED_FULL:
716 		epout_desc = &fs_epout_desc;
717 		epin_desc = &fs_epin_desc;
718 		epin_fback_desc = &fs_epin_fback_desc;
719 		ep_int_desc = &fs_ep_int_desc;
720 		break;
721 	case USB_SPEED_HIGH:
722 		epout_desc = &hs_epout_desc;
723 		epin_desc = &hs_epin_desc;
724 		epin_fback_desc = &hs_epin_fback_desc;
725 		ep_int_desc = &hs_ep_int_desc;
726 		break;
727 	default:
728 		epout_desc = &ss_epout_desc;
729 		epin_desc = &ss_epin_desc;
730 		epout_desc_comp = &ss_epout_desc_comp;
731 		epin_desc_comp = &ss_epin_desc_comp;
732 		epin_fback_desc = &ss_epin_fback_desc;
733 		ep_int_desc = &ss_ep_int_desc;
734 	}
735 
736 	i = 0;
737 	headers[i++] = USBDHDR(&iad_desc);
738 	headers[i++] = USBDHDR(&std_ac_if_desc);
739 	headers[i++] = USBDHDR(&ac_hdr_desc);
740 	if (EPIN_EN(opts))
741 		headers[i++] = USBDHDR(&in_clk_src_desc);
742 	if (EPOUT_EN(opts)) {
743 		headers[i++] = USBDHDR(&out_clk_src_desc);
744 		headers[i++] = USBDHDR(&usb_out_it_desc);
745 
746     if (FUOUT_EN(opts))
747       headers[i++] = USBDHDR(out_feature_unit_desc);
748   }
749 
750 	if (EPIN_EN(opts)) {
751 		headers[i++] = USBDHDR(&io_in_it_desc);
752 
753     if (FUIN_EN(opts))
754       headers[i++] = USBDHDR(in_feature_unit_desc);
755 
756 		headers[i++] = USBDHDR(&usb_in_ot_desc);
757 	}
758 
759 	if (EPOUT_EN(opts))
760 		headers[i++] = USBDHDR(&io_out_ot_desc);
761 
762   if (FUOUT_EN(opts) || FUIN_EN(opts))
763       headers[i++] = USBDHDR(ep_int_desc);
764 
765   if (EPOUT_EN(opts)) {
766 		headers[i++] = USBDHDR(&std_as_out_if0_desc);
767 		headers[i++] = USBDHDR(&std_as_out_if1_desc);
768 		headers[i++] = USBDHDR(&as_out_hdr_desc);
769 		headers[i++] = USBDHDR(&as_out_fmt1_desc);
770 		headers[i++] = USBDHDR(epout_desc);
771 		if (epout_desc_comp)
772 			headers[i++] = USBDHDR(epout_desc_comp);
773 
774 		headers[i++] = USBDHDR(&as_iso_out_desc);
775 
776 		if (EPOUT_FBACK_IN_EN(opts))
777 			headers[i++] = USBDHDR(epin_fback_desc);
778 	}
779 
780 	if (EPIN_EN(opts)) {
781 		headers[i++] = USBDHDR(&std_as_in_if0_desc);
782 		headers[i++] = USBDHDR(&std_as_in_if1_desc);
783 		headers[i++] = USBDHDR(&as_in_hdr_desc);
784 		headers[i++] = USBDHDR(&as_in_fmt1_desc);
785 		headers[i++] = USBDHDR(epin_desc);
786 		if (epin_desc_comp)
787 			headers[i++] = USBDHDR(epin_desc_comp);
788 
789 		headers[i++] = USBDHDR(&as_iso_in_desc);
790 	}
791 	headers[i] = NULL;
792 }
793 
794 static void setup_descriptor(struct f_uac2_opts *opts)
795 {
796 	/* patch descriptors */
797 	int i = 1; /* ID's start with 1 */
798 
799 	if (EPOUT_EN(opts))
800 		usb_out_it_desc.bTerminalID = i++;
801 	if (EPIN_EN(opts))
802 		io_in_it_desc.bTerminalID = i++;
803 	if (EPOUT_EN(opts))
804 		io_out_ot_desc.bTerminalID = i++;
805 	if (EPIN_EN(opts))
806 		usb_in_ot_desc.bTerminalID = i++;
807 	if (FUOUT_EN(opts))
808 		out_feature_unit_desc->bUnitID = i++;
809 	if (FUIN_EN(opts))
810 		in_feature_unit_desc->bUnitID = i++;
811 	if (EPOUT_EN(opts))
812 		out_clk_src_desc.bClockID = i++;
813 	if (EPIN_EN(opts))
814 		in_clk_src_desc.bClockID = i++;
815 
816 	usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
817 
818 	if (FUIN_EN(opts)) {
819 		usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
820 		in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
821 	} else {
822 		usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
823 	}
824 
825 	usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
826 	io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
827 	io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
828 
829 	if (FUOUT_EN(opts)) {
830 		io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
831 		out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
832 	} else {
833 		io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
834 	}
835 
836 	as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
837 	as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
838 
839 	iad_desc.bInterfaceCount = 1;
840 	ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
841 
842 	if (EPIN_EN(opts)) {
843 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
844 
845 		len += sizeof(in_clk_src_desc);
846 		len += sizeof(usb_in_ot_desc);
847 
848 		if (FUIN_EN(opts))
849 			len += in_feature_unit_desc->bLength;
850 
851 		len += sizeof(io_in_it_desc);
852 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
853 		iad_desc.bInterfaceCount++;
854 	}
855 	if (EPOUT_EN(opts)) {
856 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
857 
858 		len += sizeof(out_clk_src_desc);
859 		len += sizeof(usb_out_it_desc);
860 
861 		if (FUOUT_EN(opts))
862 			len += out_feature_unit_desc->bLength;
863 
864 		len += sizeof(io_out_ot_desc);
865 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
866 		iad_desc.bInterfaceCount++;
867 	}
868 
869 	setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
870 	setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
871 	setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
872 }
873 
874 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
875 {
876 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
877 
878 	if (!opts->p_chmask && !opts->c_chmask) {
879 		dev_err(dev, "Error: no playback and capture channels\n");
880 		return -EINVAL;
881 	} else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) {
882 		dev_err(dev, "Error: unsupported playback channels mask\n");
883 		return -EINVAL;
884 	} else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) {
885 		dev_err(dev, "Error: unsupported capture channels mask\n");
886 		return -EINVAL;
887 	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
888 		dev_err(dev, "Error: incorrect playback sample size\n");
889 		return -EINVAL;
890 	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
891 		dev_err(dev, "Error: incorrect capture sample size\n");
892 		return -EINVAL;
893 	} else if (!opts->p_srate) {
894 		dev_err(dev, "Error: incorrect playback sampling rate\n");
895 		return -EINVAL;
896 	} else if (!opts->c_srate) {
897 		dev_err(dev, "Error: incorrect capture sampling rate\n");
898 		return -EINVAL;
899 	}
900 
901 	if (opts->p_volume_max <= opts->p_volume_min) {
902 		dev_err(dev, "Error: incorrect playback volume max/min\n");
903 			return -EINVAL;
904 	} else if (opts->c_volume_max <= opts->c_volume_min) {
905 		dev_err(dev, "Error: incorrect capture volume max/min\n");
906 			return -EINVAL;
907 	} else if (opts->p_volume_res <= 0) {
908 		dev_err(dev, "Error: negative/zero playback volume resolution\n");
909 			return -EINVAL;
910 	} else if (opts->c_volume_res <= 0) {
911 		dev_err(dev, "Error: negative/zero capture volume resolution\n");
912 			return -EINVAL;
913 	}
914 
915 	if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
916 		dev_err(dev, "Error: incorrect playback volume resolution\n");
917 			return -EINVAL;
918 	} else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
919 		dev_err(dev, "Error: incorrect capture volume resolution\n");
920 			return -EINVAL;
921 	}
922 
923 	return 0;
924 }
925 
926 static int
927 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
928 {
929 	struct f_uac2 *uac2 = func_to_uac2(fn);
930 	struct g_audio *agdev = func_to_g_audio(fn);
931 	struct usb_composite_dev *cdev = cfg->cdev;
932 	struct usb_gadget *gadget = cdev->gadget;
933 	struct device *dev = &gadget->dev;
934 	struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
935 	struct usb_string *us;
936 	int ret;
937 
938 	ret = afunc_validate_opts(agdev, dev);
939 	if (ret)
940 		return ret;
941 
942 	us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
943 	if (IS_ERR(us))
944 		return PTR_ERR(us);
945 
946 	if (FUOUT_EN(uac2_opts)) {
947 		out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
948 		if (!out_feature_unit_desc)
949 			return -ENOMEM;
950 	}
951 	if (FUIN_EN(uac2_opts)) {
952 		in_feature_unit_desc = build_fu_desc(uac2_opts->p_chmask);
953 		if (!in_feature_unit_desc) {
954 			ret = -ENOMEM;
955 			goto err_free_fu;
956 		}
957 	}
958 
959 	iad_desc.iFunction = us[STR_ASSOC].id;
960 	std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
961 	in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
962 	out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
963 	usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
964 	io_in_it_desc.iTerminal = us[STR_IO_IT].id;
965 	usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
966 	io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
967 	std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
968 	std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
969 	std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
970 	std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
971 
972 	if (FUOUT_EN(uac2_opts)) {
973 		u8 *i_feature = (u8 *)out_feature_unit_desc +
974 				out_feature_unit_desc->bLength - 1;
975 		*i_feature = us[STR_FU_OUT].id;
976 	}
977 	if (FUIN_EN(uac2_opts)) {
978 		u8 *i_feature = (u8 *)in_feature_unit_desc +
979 				in_feature_unit_desc->bLength - 1;
980 		*i_feature = us[STR_FU_IN].id;
981 	}
982 
983 
984 	/* Initialize the configurable parameters */
985 	usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
986 	usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
987 	io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
988 	io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
989 	as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
990 	as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
991 	as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
992 	as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
993 	as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
994 	as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
995 	as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
996 	as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
997 	if (FUOUT_EN(uac2_opts)) {
998 		__le32 *bma = (__le32 *)&out_feature_unit_desc->bmaControls[0];
999 		u32 control = 0;
1000 
1001 		if (uac2_opts->c_mute_present)
1002 			control |= CONTROL_RDWR << FU_MUTE_CTRL;
1003 		if (uac2_opts->c_volume_present)
1004 			control |= CONTROL_RDWR << FU_VOL_CTRL;
1005 		*bma = cpu_to_le32(control);
1006 	}
1007 	if (FUIN_EN(uac2_opts)) {
1008 		__le32 *bma = (__le32 *)&in_feature_unit_desc->bmaControls[0];
1009 		u32 control = 0;
1010 
1011 		if (uac2_opts->p_mute_present)
1012 			control |= CONTROL_RDWR << FU_MUTE_CTRL;
1013 		if (uac2_opts->p_volume_present)
1014 			control |= CONTROL_RDWR << FU_VOL_CTRL;
1015 		*bma = cpu_to_le32(control);
1016 	}
1017 
1018 	snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
1019 	snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
1020 
1021 	ret = usb_interface_id(cfg, fn);
1022 	if (ret < 0) {
1023 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1024 		goto err_free_fu;
1025 	}
1026 	iad_desc.bFirstInterface = ret;
1027 
1028 	std_ac_if_desc.bInterfaceNumber = ret;
1029 	uac2->ac_intf = ret;
1030 	uac2->ac_alt = 0;
1031 
1032 	if (EPOUT_EN(uac2_opts)) {
1033 		ret = usb_interface_id(cfg, fn);
1034 		if (ret < 0) {
1035 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1036 			goto err_free_fu;
1037 		}
1038 		std_as_out_if0_desc.bInterfaceNumber = ret;
1039 		std_as_out_if1_desc.bInterfaceNumber = ret;
1040 		uac2->as_out_intf = ret;
1041 		uac2->as_out_alt = 0;
1042 
1043 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1044 			fs_epout_desc.bmAttributes =
1045 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1046 			hs_epout_desc.bmAttributes =
1047 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1048 			ss_epout_desc.bmAttributes =
1049 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1050 			std_as_out_if1_desc.bNumEndpoints++;
1051 		} else {
1052 			fs_epout_desc.bmAttributes =
1053 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1054 			hs_epout_desc.bmAttributes =
1055 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1056 			ss_epout_desc.bmAttributes =
1057 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1058 		}
1059 	}
1060 
1061 	if (EPIN_EN(uac2_opts)) {
1062 		ret = usb_interface_id(cfg, fn);
1063 		if (ret < 0) {
1064 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1065 			goto err_free_fu;
1066 		}
1067 		std_as_in_if0_desc.bInterfaceNumber = ret;
1068 		std_as_in_if1_desc.bInterfaceNumber = ret;
1069 		uac2->as_in_intf = ret;
1070 		uac2->as_in_alt = 0;
1071 	}
1072 
1073 	if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts)) {
1074 		uac2->int_ep = usb_ep_autoconfig(gadget, &fs_ep_int_desc);
1075 		if (!uac2->int_ep) {
1076 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1077 			ret = -ENODEV;
1078 			goto err_free_fu;
1079 		}
1080 
1081 		std_ac_if_desc.bNumEndpoints = 1;
1082 	}
1083 
1084 	/* Calculate wMaxPacketSize according to audio bandwidth */
1085 	ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
1086 				     true);
1087 	if (ret < 0) {
1088 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1089 		return ret;
1090 	}
1091 
1092 	ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
1093 				     false);
1094 	if (ret < 0) {
1095 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1096 		return ret;
1097 	}
1098 
1099 	ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
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, &hs_epout_desc, USB_SPEED_HIGH,
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, &ss_epin_desc, USB_SPEED_SUPER,
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, &ss_epout_desc, USB_SPEED_SUPER,
1121 				     false);
1122 	if (ret < 0) {
1123 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1124 		return ret;
1125 	}
1126 
1127 	if (EPOUT_EN(uac2_opts)) {
1128 		agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1129 		if (!agdev->out_ep) {
1130 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1131 			ret = -ENODEV;
1132 			goto err_free_fu;
1133 		}
1134 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1135 			agdev->in_ep_fback = usb_ep_autoconfig(gadget,
1136 						       &fs_epin_fback_desc);
1137 			if (!agdev->in_ep_fback) {
1138 				dev_err(dev, "%s:%d Error!\n",
1139 					__func__, __LINE__);
1140 				ret = -ENODEV;
1141 				goto err_free_fu;
1142 			}
1143 		}
1144 	}
1145 
1146 	if (EPIN_EN(uac2_opts)) {
1147 		agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1148 		if (!agdev->in_ep) {
1149 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1150 			ret = -ENODEV;
1151 			goto err_free_fu;
1152 		}
1153 	}
1154 
1155 	agdev->in_ep_maxpsize = max_t(u16,
1156 				le16_to_cpu(fs_epin_desc.wMaxPacketSize),
1157 				le16_to_cpu(hs_epin_desc.wMaxPacketSize));
1158 	agdev->out_ep_maxpsize = max_t(u16,
1159 				le16_to_cpu(fs_epout_desc.wMaxPacketSize),
1160 				le16_to_cpu(hs_epout_desc.wMaxPacketSize));
1161 
1162 	agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
1163 				le16_to_cpu(ss_epin_desc.wMaxPacketSize));
1164 	agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
1165 				le16_to_cpu(ss_epout_desc.wMaxPacketSize));
1166 
1167 	// HS and SS endpoint addresses are copied from autoconfigured FS descriptors
1168 	hs_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1169 	hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1170 	hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1171 	hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1172 	ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1173 	ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1174 	ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1175 	ss_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1176 
1177 	setup_descriptor(uac2_opts);
1178 
1179 	ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
1180 				     ss_audio_desc);
1181 	if (ret)
1182 		goto err_free_fu;
1183 
1184 	agdev->gadget = gadget;
1185 
1186 	agdev->params.p_chmask = uac2_opts->p_chmask;
1187 	agdev->params.p_srate = uac2_opts->p_srate;
1188 	agdev->params.p_ssize = uac2_opts->p_ssize;
1189 	if (FUIN_EN(uac2_opts)) {
1190 		agdev->params.p_fu.id = USB_IN_FU_ID;
1191 		agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
1192 		agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
1193 		agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
1194 		agdev->params.p_fu.volume_max = uac2_opts->p_volume_max;
1195 		agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
1196 	}
1197 	agdev->params.c_chmask = uac2_opts->c_chmask;
1198 	agdev->params.c_srate = uac2_opts->c_srate;
1199 	agdev->params.c_ssize = uac2_opts->c_ssize;
1200 	if (FUOUT_EN(uac2_opts)) {
1201 		agdev->params.c_fu.id = USB_OUT_FU_ID;
1202 		agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
1203 		agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
1204 		agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
1205 		agdev->params.c_fu.volume_max = uac2_opts->c_volume_max;
1206 		agdev->params.c_fu.volume_res = uac2_opts->c_volume_res;
1207 	}
1208 	agdev->params.req_number = uac2_opts->req_number;
1209 	agdev->params.fb_max = uac2_opts->fb_max;
1210 
1211 	if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts))
1212     agdev->notify = afunc_notify;
1213 
1214 	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
1215 	if (ret)
1216 		goto err_free_descs;
1217 
1218 	return 0;
1219 
1220 err_free_descs:
1221 	usb_free_all_descriptors(fn);
1222 	agdev->gadget = NULL;
1223 err_free_fu:
1224 	kfree(out_feature_unit_desc);
1225 	out_feature_unit_desc = NULL;
1226 	kfree(in_feature_unit_desc);
1227 	in_feature_unit_desc = NULL;
1228 	return ret;
1229 }
1230 
1231 static void
1232 afunc_notify_complete(struct usb_ep *_ep, struct usb_request *req)
1233 {
1234 	struct g_audio *agdev = req->context;
1235 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1236 
1237 	atomic_dec(&uac2->int_count);
1238 	kfree(req->buf);
1239 	usb_ep_free_request(_ep, req);
1240 }
1241 
1242 static int
1243 afunc_notify(struct g_audio *agdev, int unit_id, int cs)
1244 {
1245 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1246 	struct usb_request *req;
1247 	struct uac2_interrupt_data_msg *msg;
1248 	u16 w_index, w_value;
1249 	int ret;
1250 
1251 	if (!uac2->int_ep->enabled)
1252 		return 0;
1253 
1254 	if (atomic_inc_return(&uac2->int_count) > UAC2_DEF_INT_REQ_NUM) {
1255 		atomic_dec(&uac2->int_count);
1256 		return 0;
1257 	}
1258 
1259 	req = usb_ep_alloc_request(uac2->int_ep, GFP_ATOMIC);
1260 	if (req == NULL) {
1261 		ret = -ENOMEM;
1262 		goto err_dec_int_count;
1263 	}
1264 
1265 	msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
1266 	if (msg == NULL) {
1267 		ret = -ENOMEM;
1268 		goto err_free_request;
1269 	}
1270 
1271 	w_index = unit_id << 8 | uac2->ac_intf;
1272 	w_value = cs << 8;
1273 
1274 	msg->bInfo = 0; /* Non-vendor, interface interrupt */
1275 	msg->bAttribute = UAC2_CS_CUR;
1276 	msg->wIndex = cpu_to_le16(w_index);
1277 	msg->wValue = cpu_to_le16(w_value);
1278 
1279 	req->length = sizeof(*msg);
1280 	req->buf = msg;
1281 	req->context = agdev;
1282 	req->complete = afunc_notify_complete;
1283 
1284 	ret = usb_ep_queue(uac2->int_ep, req, GFP_ATOMIC);
1285 
1286 	if (ret)
1287 		goto err_free_msg;
1288 
1289 	return 0;
1290 
1291 err_free_msg:
1292 	kfree(msg);
1293 err_free_request:
1294 	usb_ep_free_request(uac2->int_ep, req);
1295 err_dec_int_count:
1296 	atomic_dec(&uac2->int_count);
1297 
1298 	return ret;
1299 }
1300 
1301 static int
1302 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1303 {
1304 	struct usb_composite_dev *cdev = fn->config->cdev;
1305 	struct f_uac2 *uac2 = func_to_uac2(fn);
1306 	struct g_audio *agdev = func_to_g_audio(fn);
1307 	struct usb_gadget *gadget = cdev->gadget;
1308 	struct device *dev = &gadget->dev;
1309 	int ret = 0;
1310 
1311 	/* No i/f has more than 2 alt settings */
1312 	if (alt > 1) {
1313 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1314 		return -EINVAL;
1315 	}
1316 
1317 	if (intf == uac2->ac_intf) {
1318 		/* Control I/f has only 1 AltSetting - 0 */
1319 		if (alt) {
1320 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1321 			return -EINVAL;
1322 		}
1323 
1324 		/* restart interrupt endpoint */
1325 		if (uac2->int_ep) {
1326 			usb_ep_disable(uac2->int_ep);
1327 			config_ep_by_speed(gadget, &agdev->func, uac2->int_ep);
1328 			usb_ep_enable(uac2->int_ep);
1329 		}
1330 
1331 		return 0;
1332 	}
1333 
1334 	if (intf == uac2->as_out_intf) {
1335 		uac2->as_out_alt = alt;
1336 
1337 		if (alt)
1338 			ret = u_audio_start_capture(&uac2->g_audio);
1339 		else
1340 			u_audio_stop_capture(&uac2->g_audio);
1341 	} else if (intf == uac2->as_in_intf) {
1342 		uac2->as_in_alt = alt;
1343 
1344 		if (alt)
1345 			ret = u_audio_start_playback(&uac2->g_audio);
1346 		else
1347 			u_audio_stop_playback(&uac2->g_audio);
1348 	} else {
1349 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1350 		return -EINVAL;
1351 	}
1352 
1353 	return ret;
1354 }
1355 
1356 static int
1357 afunc_get_alt(struct usb_function *fn, unsigned intf)
1358 {
1359 	struct f_uac2 *uac2 = func_to_uac2(fn);
1360 	struct g_audio *agdev = func_to_g_audio(fn);
1361 
1362 	if (intf == uac2->ac_intf)
1363 		return uac2->ac_alt;
1364 	else if (intf == uac2->as_out_intf)
1365 		return uac2->as_out_alt;
1366 	else if (intf == uac2->as_in_intf)
1367 		return uac2->as_in_alt;
1368 	else
1369 		dev_err(&agdev->gadget->dev,
1370 			"%s:%d Invalid Interface %d!\n",
1371 			__func__, __LINE__, intf);
1372 
1373 	return -EINVAL;
1374 }
1375 
1376 static void
1377 afunc_disable(struct usb_function *fn)
1378 {
1379 	struct f_uac2 *uac2 = func_to_uac2(fn);
1380 
1381 	uac2->as_in_alt = 0;
1382 	uac2->as_out_alt = 0;
1383 	u_audio_stop_capture(&uac2->g_audio);
1384 	u_audio_stop_playback(&uac2->g_audio);
1385 	if (uac2->int_ep)
1386 		usb_ep_disable(uac2->int_ep);
1387 }
1388 
1389 static int
1390 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1391 {
1392 	struct usb_request *req = fn->config->cdev->req;
1393 	struct g_audio *agdev = func_to_g_audio(fn);
1394 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1395 	u16 w_length = le16_to_cpu(cr->wLength);
1396 	u16 w_index = le16_to_cpu(cr->wIndex);
1397 	u16 w_value = le16_to_cpu(cr->wValue);
1398 	u8 entity_id = (w_index >> 8) & 0xff;
1399 	u8 control_selector = w_value >> 8;
1400 	int value = -EOPNOTSUPP;
1401 	int p_srate, c_srate;
1402 
1403 	p_srate = opts->p_srate;
1404 	c_srate = opts->c_srate;
1405 
1406 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1407 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1408 			struct cntrl_cur_lay3 c;
1409 
1410 			memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1411 
1412 			if (entity_id == USB_IN_CLK_ID)
1413 				c.dCUR = cpu_to_le32(p_srate);
1414 			else if (entity_id == USB_OUT_CLK_ID)
1415 				c.dCUR = cpu_to_le32(c_srate);
1416 
1417 			value = min_t(unsigned int, w_length, sizeof(c));
1418 			memcpy(req->buf, &c, value);
1419 		} else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1420 			*(u8 *)req->buf = 1;
1421 			value = min_t(unsigned int, w_length, 1);
1422 		} else {
1423 			dev_err(&agdev->gadget->dev,
1424 				"%s:%d control_selector=%d TODO!\n",
1425 				__func__, __LINE__, control_selector);
1426 		}
1427 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1428 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1429 		unsigned int is_playback = 0;
1430 
1431 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1432 			is_playback = 1;
1433 
1434 		if (control_selector == UAC_FU_MUTE) {
1435 			unsigned int mute;
1436 
1437 			u_audio_get_mute(agdev, is_playback, &mute);
1438 
1439 			*(u8 *)req->buf = mute;
1440 			value = min_t(unsigned int, w_length, 1);
1441 		} else if (control_selector == UAC_FU_VOLUME) {
1442 			struct cntrl_cur_lay2 c;
1443 			s16 volume;
1444 
1445 			memset(&c, 0, sizeof(struct cntrl_cur_lay2));
1446 
1447 			u_audio_get_volume(agdev, is_playback, &volume);
1448 			c.wCUR = cpu_to_le16(volume);
1449 
1450 			value = min_t(unsigned int, w_length, sizeof(c));
1451 			memcpy(req->buf, &c, value);
1452 		} else {
1453 			dev_err(&agdev->gadget->dev,
1454 				"%s:%d control_selector=%d TODO!\n",
1455 				__func__, __LINE__, control_selector);
1456 		}
1457 	} else {
1458 		dev_err(&agdev->gadget->dev,
1459 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1460 			__func__, __LINE__, entity_id, control_selector);
1461 	}
1462 
1463 	return value;
1464 }
1465 
1466 static int
1467 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1468 {
1469 	struct usb_request *req = fn->config->cdev->req;
1470 	struct g_audio *agdev = func_to_g_audio(fn);
1471 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1472 	u16 w_length = le16_to_cpu(cr->wLength);
1473 	u16 w_index = le16_to_cpu(cr->wIndex);
1474 	u16 w_value = le16_to_cpu(cr->wValue);
1475 	u8 entity_id = (w_index >> 8) & 0xff;
1476 	u8 control_selector = w_value >> 8;
1477 	int value = -EOPNOTSUPP;
1478 	int p_srate, c_srate;
1479 
1480 	p_srate = opts->p_srate;
1481 	c_srate = opts->c_srate;
1482 
1483 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1484 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1485 			struct cntrl_range_lay3 r;
1486 
1487 			if (entity_id == USB_IN_CLK_ID)
1488 				r.dMIN = cpu_to_le32(p_srate);
1489 			else if (entity_id == USB_OUT_CLK_ID)
1490 				r.dMIN = cpu_to_le32(c_srate);
1491 			else
1492 				return -EOPNOTSUPP;
1493 
1494 			r.dMAX = r.dMIN;
1495 			r.dRES = 0;
1496 			r.wNumSubRanges = cpu_to_le16(1);
1497 
1498 			value = min_t(unsigned int, w_length, sizeof(r));
1499 			memcpy(req->buf, &r, value);
1500 		} else {
1501 			dev_err(&agdev->gadget->dev,
1502 				"%s:%d control_selector=%d TODO!\n",
1503 				__func__, __LINE__, control_selector);
1504 		}
1505 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1506 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1507 		unsigned int is_playback = 0;
1508 
1509 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1510 			is_playback = 1;
1511 
1512 		if (control_selector == UAC_FU_VOLUME) {
1513 			struct cntrl_range_lay2 r;
1514 			s16 max_db, min_db, res_db;
1515 
1516 			if (is_playback) {
1517 				max_db = opts->p_volume_max;
1518 				min_db = opts->p_volume_min;
1519 				res_db = opts->p_volume_res;
1520 			} else {
1521 				max_db = opts->c_volume_max;
1522 				min_db = opts->c_volume_min;
1523 				res_db = opts->c_volume_res;
1524 			}
1525 
1526 			r.wMAX = cpu_to_le16(max_db);
1527 			r.wMIN = cpu_to_le16(min_db);
1528 			r.wRES = cpu_to_le16(res_db);
1529 			r.wNumSubRanges = cpu_to_le16(1);
1530 
1531 			value = min_t(unsigned int, w_length, sizeof(r));
1532 			memcpy(req->buf, &r, value);
1533 		} else {
1534 			dev_err(&agdev->gadget->dev,
1535 				"%s:%d control_selector=%d TODO!\n",
1536 				__func__, __LINE__, control_selector);
1537 		}
1538 	} else {
1539 		dev_err(&agdev->gadget->dev,
1540 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1541 			__func__, __LINE__, entity_id, control_selector);
1542 	}
1543 
1544 	return value;
1545 }
1546 
1547 static int
1548 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1549 {
1550 	if (cr->bRequest == UAC2_CS_CUR)
1551 		return in_rq_cur(fn, cr);
1552 	else if (cr->bRequest == UAC2_CS_RANGE)
1553 		return in_rq_range(fn, cr);
1554 	else
1555 		return -EOPNOTSUPP;
1556 }
1557 
1558 static void
1559 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
1560 {
1561 	struct g_audio *agdev = req->context;
1562 	struct usb_composite_dev *cdev = agdev->func.config->cdev;
1563 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1564 	struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1565 	struct usb_ctrlrequest *cr = &uac2->setup_cr;
1566 	u16 w_index = le16_to_cpu(cr->wIndex);
1567 	u16 w_value = le16_to_cpu(cr->wValue);
1568 	u8 entity_id = (w_index >> 8) & 0xff;
1569 	u8 control_selector = w_value >> 8;
1570 
1571 	if (req->status != 0) {
1572 		dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
1573 		return;
1574 	}
1575 
1576 	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1577 		(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1578 		unsigned int is_playback = 0;
1579 
1580 		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1581 			is_playback = 1;
1582 
1583 		if (control_selector == UAC_FU_MUTE) {
1584 			u8 mute = *(u8 *)req->buf;
1585 
1586 			u_audio_set_mute(agdev, is_playback, mute);
1587 
1588 			return;
1589 		} else if (control_selector == UAC_FU_VOLUME) {
1590 			struct cntrl_cur_lay2 *c = req->buf;
1591 			s16 volume;
1592 
1593 			volume = le16_to_cpu(c->wCUR);
1594 			u_audio_set_volume(agdev, is_playback, volume);
1595 
1596 			return;
1597 		} else {
1598 			dev_err(&agdev->gadget->dev,
1599 				"%s:%d control_selector=%d TODO!\n",
1600 				__func__, __LINE__, control_selector);
1601 			usb_ep_set_halt(ep);
1602 		}
1603 	}
1604 }
1605 
1606 static int
1607 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1608 {
1609 	struct usb_request *req = fn->config->cdev->req;
1610 	struct g_audio *agdev = func_to_g_audio(fn);
1611 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1612 	struct f_uac2 *uac2 = func_to_uac2(fn);
1613 	u16 w_length = le16_to_cpu(cr->wLength);
1614 	u16 w_index = le16_to_cpu(cr->wIndex);
1615 	u16 w_value = le16_to_cpu(cr->wValue);
1616 	u8 entity_id = (w_index >> 8) & 0xff;
1617 	u8 control_selector = w_value >> 8;
1618 
1619 	if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1620 		if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1621 			return w_length;
1622 	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1623 			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1624 		memcpy(&uac2->setup_cr, cr, sizeof(*cr));
1625 		req->context = agdev;
1626 		req->complete = out_rq_cur_complete;
1627 
1628 		return w_length;
1629 	} else {
1630 		dev_err(&agdev->gadget->dev,
1631 			"%s:%d entity_id=%d control_selector=%d TODO!\n",
1632 			__func__, __LINE__, entity_id, control_selector);
1633 	}
1634 	return -EOPNOTSUPP;
1635 }
1636 
1637 static int
1638 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1639 {
1640 	struct f_uac2 *uac2 = func_to_uac2(fn);
1641 	struct g_audio *agdev = func_to_g_audio(fn);
1642 	u16 w_index = le16_to_cpu(cr->wIndex);
1643 	u8 intf = w_index & 0xff;
1644 
1645 	if (intf != uac2->ac_intf) {
1646 		dev_err(&agdev->gadget->dev,
1647 			"%s:%d Error!\n", __func__, __LINE__);
1648 		return -EOPNOTSUPP;
1649 	}
1650 
1651 	if (cr->bRequestType & USB_DIR_IN)
1652 		return ac_rq_in(fn, cr);
1653 	else if (cr->bRequest == UAC2_CS_CUR)
1654 		return out_rq_cur(fn, cr);
1655 
1656 	return -EOPNOTSUPP;
1657 }
1658 
1659 static int
1660 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1661 {
1662 	struct usb_composite_dev *cdev = fn->config->cdev;
1663 	struct g_audio *agdev = func_to_g_audio(fn);
1664 	struct usb_request *req = cdev->req;
1665 	u16 w_length = le16_to_cpu(cr->wLength);
1666 	int value = -EOPNOTSUPP;
1667 
1668 	/* Only Class specific requests are supposed to reach here */
1669 	if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1670 		return -EOPNOTSUPP;
1671 
1672 	if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1673 		value = setup_rq_inf(fn, cr);
1674 	else
1675 		dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1676 				__func__, __LINE__);
1677 
1678 	if (value >= 0) {
1679 		req->length = value;
1680 		req->zero = value < w_length;
1681 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1682 		if (value < 0) {
1683 			dev_err(&agdev->gadget->dev,
1684 				"%s:%d Error!\n", __func__, __LINE__);
1685 			req->status = 0;
1686 		}
1687 	}
1688 
1689 	return value;
1690 }
1691 
1692 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1693 {
1694 	return container_of(to_config_group(item), struct f_uac2_opts,
1695 			    func_inst.group);
1696 }
1697 
1698 static void f_uac2_attr_release(struct config_item *item)
1699 {
1700 	struct f_uac2_opts *opts = to_f_uac2_opts(item);
1701 
1702 	usb_put_function_instance(&opts->func_inst);
1703 }
1704 
1705 static struct configfs_item_operations f_uac2_item_ops = {
1706 	.release	= f_uac2_attr_release,
1707 };
1708 
1709 #define uac2_kstrtou32 kstrtou32
1710 #define uac2_kstrtos16 kstrtos16
1711 #define uac2_kstrtobool(s, base, res) kstrtobool((s), (res))
1712 
1713 static const char *u32_fmt = "%u\n";
1714 static const char *s16_fmt = "%hd\n";
1715 static const char *bool_fmt = "%u\n";
1716 
1717 #define UAC2_ATTRIBUTE(type, name)					\
1718 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1719 					 char *page)			\
1720 {									\
1721 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1722 	int result;							\
1723 									\
1724 	mutex_lock(&opts->lock);					\
1725 	result = sprintf(page, type##_fmt, opts->name);			\
1726 	mutex_unlock(&opts->lock);					\
1727 									\
1728 	return result;							\
1729 }									\
1730 									\
1731 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1732 					  const char *page, size_t len)	\
1733 {									\
1734 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1735 	int ret;							\
1736 	type num;							\
1737 									\
1738 	mutex_lock(&opts->lock);					\
1739 	if (opts->refcnt) {						\
1740 		ret = -EBUSY;						\
1741 		goto end;						\
1742 	}								\
1743 									\
1744 	ret = uac2_kstrto##type(page, 0, &num);				\
1745 	if (ret)							\
1746 		goto end;						\
1747 									\
1748 	opts->name = num;						\
1749 	ret = len;							\
1750 									\
1751 end:									\
1752 	mutex_unlock(&opts->lock);					\
1753 	return ret;							\
1754 }									\
1755 									\
1756 CONFIGFS_ATTR(f_uac2_opts_, name)
1757 
1758 #define UAC2_ATTRIBUTE_SYNC(name)					\
1759 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1760 					 char *page)			\
1761 {									\
1762 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1763 	int result;							\
1764 	char *str;							\
1765 									\
1766 	mutex_lock(&opts->lock);					\
1767 	switch (opts->name) {						\
1768 	case USB_ENDPOINT_SYNC_ASYNC:					\
1769 		str = "async";						\
1770 		break;							\
1771 	case USB_ENDPOINT_SYNC_ADAPTIVE:				\
1772 		str = "adaptive";					\
1773 		break;							\
1774 	default:							\
1775 		str = "unknown";					\
1776 		break;							\
1777 	}								\
1778 	result = sprintf(page, "%s\n", str);				\
1779 	mutex_unlock(&opts->lock);					\
1780 									\
1781 	return result;							\
1782 }									\
1783 									\
1784 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1785 					  const char *page, size_t len)	\
1786 {									\
1787 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1788 	int ret = 0;							\
1789 									\
1790 	mutex_lock(&opts->lock);					\
1791 	if (opts->refcnt) {						\
1792 		ret = -EBUSY;						\
1793 		goto end;						\
1794 	}								\
1795 									\
1796 	if (!strncmp(page, "async", 5))					\
1797 		opts->name = USB_ENDPOINT_SYNC_ASYNC;			\
1798 	else if (!strncmp(page, "adaptive", 8))				\
1799 		opts->name = USB_ENDPOINT_SYNC_ADAPTIVE;		\
1800 	else {								\
1801 		ret = -EINVAL;						\
1802 		goto end;						\
1803 	}								\
1804 									\
1805 	ret = len;							\
1806 									\
1807 end:									\
1808 	mutex_unlock(&opts->lock);					\
1809 	return ret;							\
1810 }									\
1811 									\
1812 CONFIGFS_ATTR(f_uac2_opts_, name)
1813 
1814 UAC2_ATTRIBUTE(u32, p_chmask);
1815 UAC2_ATTRIBUTE(u32, p_srate);
1816 UAC2_ATTRIBUTE(u32, p_ssize);
1817 UAC2_ATTRIBUTE(u32, c_chmask);
1818 UAC2_ATTRIBUTE(u32, c_srate);
1819 UAC2_ATTRIBUTE_SYNC(c_sync);
1820 UAC2_ATTRIBUTE(u32, c_ssize);
1821 UAC2_ATTRIBUTE(u32, req_number);
1822 
1823 UAC2_ATTRIBUTE(bool, p_mute_present);
1824 UAC2_ATTRIBUTE(bool, p_volume_present);
1825 UAC2_ATTRIBUTE(s16, p_volume_min);
1826 UAC2_ATTRIBUTE(s16, p_volume_max);
1827 UAC2_ATTRIBUTE(s16, p_volume_res);
1828 
1829 UAC2_ATTRIBUTE(bool, c_mute_present);
1830 UAC2_ATTRIBUTE(bool, c_volume_present);
1831 UAC2_ATTRIBUTE(s16, c_volume_min);
1832 UAC2_ATTRIBUTE(s16, c_volume_max);
1833 UAC2_ATTRIBUTE(s16, c_volume_res);
1834 UAC2_ATTRIBUTE(u32, fb_max);
1835 
1836 static struct configfs_attribute *f_uac2_attrs[] = {
1837 	&f_uac2_opts_attr_p_chmask,
1838 	&f_uac2_opts_attr_p_srate,
1839 	&f_uac2_opts_attr_p_ssize,
1840 	&f_uac2_opts_attr_c_chmask,
1841 	&f_uac2_opts_attr_c_srate,
1842 	&f_uac2_opts_attr_c_ssize,
1843 	&f_uac2_opts_attr_c_sync,
1844 	&f_uac2_opts_attr_req_number,
1845 	&f_uac2_opts_attr_fb_max,
1846 
1847 	&f_uac2_opts_attr_p_mute_present,
1848 	&f_uac2_opts_attr_p_volume_present,
1849 	&f_uac2_opts_attr_p_volume_min,
1850 	&f_uac2_opts_attr_p_volume_max,
1851 	&f_uac2_opts_attr_p_volume_res,
1852 
1853 	&f_uac2_opts_attr_c_mute_present,
1854 	&f_uac2_opts_attr_c_volume_present,
1855 	&f_uac2_opts_attr_c_volume_min,
1856 	&f_uac2_opts_attr_c_volume_max,
1857 	&f_uac2_opts_attr_c_volume_res,
1858 
1859 	NULL,
1860 };
1861 
1862 static const struct config_item_type f_uac2_func_type = {
1863 	.ct_item_ops	= &f_uac2_item_ops,
1864 	.ct_attrs	= f_uac2_attrs,
1865 	.ct_owner	= THIS_MODULE,
1866 };
1867 
1868 static void afunc_free_inst(struct usb_function_instance *f)
1869 {
1870 	struct f_uac2_opts *opts;
1871 
1872 	opts = container_of(f, struct f_uac2_opts, func_inst);
1873 	kfree(opts);
1874 }
1875 
1876 static struct usb_function_instance *afunc_alloc_inst(void)
1877 {
1878 	struct f_uac2_opts *opts;
1879 
1880 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1881 	if (!opts)
1882 		return ERR_PTR(-ENOMEM);
1883 
1884 	mutex_init(&opts->lock);
1885 	opts->func_inst.free_func_inst = afunc_free_inst;
1886 
1887 	config_group_init_type_name(&opts->func_inst.group, "",
1888 				    &f_uac2_func_type);
1889 
1890 	opts->p_chmask = UAC2_DEF_PCHMASK;
1891 	opts->p_srate = UAC2_DEF_PSRATE;
1892 	opts->p_ssize = UAC2_DEF_PSSIZE;
1893 	opts->c_chmask = UAC2_DEF_CCHMASK;
1894 	opts->c_srate = UAC2_DEF_CSRATE;
1895 	opts->c_ssize = UAC2_DEF_CSSIZE;
1896 	opts->c_sync = UAC2_DEF_CSYNC;
1897 
1898 	opts->p_mute_present = UAC2_DEF_MUTE_PRESENT;
1899 	opts->p_volume_present = UAC2_DEF_VOLUME_PRESENT;
1900 	opts->p_volume_min = UAC2_DEF_MIN_DB;
1901 	opts->p_volume_max = UAC2_DEF_MAX_DB;
1902 	opts->p_volume_res = UAC2_DEF_RES_DB;
1903 
1904 	opts->c_mute_present = UAC2_DEF_MUTE_PRESENT;
1905 	opts->c_volume_present = UAC2_DEF_VOLUME_PRESENT;
1906 	opts->c_volume_min = UAC2_DEF_MIN_DB;
1907 	opts->c_volume_max = UAC2_DEF_MAX_DB;
1908 	opts->c_volume_res = UAC2_DEF_RES_DB;
1909 
1910 	opts->req_number = UAC2_DEF_REQ_NUM;
1911 	opts->fb_max = UAC2_DEF_FB_MAX;
1912 	return &opts->func_inst;
1913 }
1914 
1915 static void afunc_free(struct usb_function *f)
1916 {
1917 	struct g_audio *agdev;
1918 	struct f_uac2_opts *opts;
1919 
1920 	agdev = func_to_g_audio(f);
1921 	opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1922 	kfree(agdev);
1923 	mutex_lock(&opts->lock);
1924 	--opts->refcnt;
1925 	mutex_unlock(&opts->lock);
1926 }
1927 
1928 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1929 {
1930 	struct g_audio *agdev = func_to_g_audio(f);
1931 
1932 	g_audio_cleanup(agdev);
1933 	usb_free_all_descriptors(f);
1934 
1935 	agdev->gadget = NULL;
1936 
1937 	kfree(out_feature_unit_desc);
1938 	out_feature_unit_desc = NULL;
1939 	kfree(in_feature_unit_desc);
1940 	in_feature_unit_desc = NULL;
1941 }
1942 
1943 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1944 {
1945 	struct f_uac2	*uac2;
1946 	struct f_uac2_opts *opts;
1947 
1948 	uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
1949 	if (uac2 == NULL)
1950 		return ERR_PTR(-ENOMEM);
1951 
1952 	opts = container_of(fi, struct f_uac2_opts, func_inst);
1953 	mutex_lock(&opts->lock);
1954 	++opts->refcnt;
1955 	mutex_unlock(&opts->lock);
1956 
1957 	uac2->g_audio.func.name = "uac2_func";
1958 	uac2->g_audio.func.bind = afunc_bind;
1959 	uac2->g_audio.func.unbind = afunc_unbind;
1960 	uac2->g_audio.func.set_alt = afunc_set_alt;
1961 	uac2->g_audio.func.get_alt = afunc_get_alt;
1962 	uac2->g_audio.func.disable = afunc_disable;
1963 	uac2->g_audio.func.setup = afunc_setup;
1964 	uac2->g_audio.func.free_func = afunc_free;
1965 
1966 	return &uac2->g_audio.func;
1967 }
1968 
1969 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1970 MODULE_LICENSE("GPL");
1971 MODULE_AUTHOR("Yadwinder Singh");
1972 MODULE_AUTHOR("Jaswinder Singh");
1973 MODULE_AUTHOR("Ruslan Bilovol");
1974