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