xref: /openbmc/linux/drivers/usb/gadget/function/f_uac2.c (revision 67bb66d32905627e29400e2cb7f87a7c4c8cf667)
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 
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 #include <linux/module.h>
13 
14 #include "u_audio.h"
15 #include "u_uac2.h"
16 
17 /* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
18 #define UAC2_CHANNEL_MASK 0x07FFFFFF
19 
20 /*
21  * The driver implements a simple UAC_2 topology.
22  * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
23  * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
24  * Capture and Playback sampling rates are independently
25  *  controlled by two clock sources :
26  *    CLK_5 := c_srate, and CLK_6 := p_srate
27  */
28 #define USB_OUT_CLK_ID	(out_clk_src_desc.bClockID)
29 #define USB_IN_CLK_ID	(in_clk_src_desc.bClockID)
30 
31 #define CONTROL_ABSENT	0
32 #define CONTROL_RDONLY	1
33 #define CONTROL_RDWR	3
34 
35 #define CLK_FREQ_CTRL	0
36 #define CLK_VLD_CTRL	2
37 
38 #define COPY_CTRL	0
39 #define CONN_CTRL	2
40 #define OVRLD_CTRL	4
41 #define CLSTR_CTRL	6
42 #define UNFLW_CTRL	8
43 #define OVFLW_CTRL	10
44 
45 #define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
46 #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
47 #define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC)
48 
49 struct f_uac2 {
50 	struct g_audio g_audio;
51 	u8 ac_intf, as_in_intf, as_out_intf;
52 	u8 ac_alt, as_in_alt, as_out_alt;	/* needed for get_alt() */
53 };
54 
55 static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
56 {
57 	return container_of(f, struct f_uac2, g_audio.func);
58 }
59 
60 static inline
61 struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
62 {
63 	return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
64 }
65 
66 /* --------- USB Function Interface ------------- */
67 
68 enum {
69 	STR_ASSOC,
70 	STR_IF_CTRL,
71 	STR_CLKSRC_IN,
72 	STR_CLKSRC_OUT,
73 	STR_USB_IT,
74 	STR_IO_IT,
75 	STR_USB_OT,
76 	STR_IO_OT,
77 	STR_AS_OUT_ALT0,
78 	STR_AS_OUT_ALT1,
79 	STR_AS_IN_ALT0,
80 	STR_AS_IN_ALT1,
81 };
82 
83 static char clksrc_in[8];
84 static char clksrc_out[8];
85 
86 static struct usb_string strings_fn[] = {
87 	[STR_ASSOC].s = "Source/Sink",
88 	[STR_IF_CTRL].s = "Topology Control",
89 	[STR_CLKSRC_IN].s = clksrc_in,
90 	[STR_CLKSRC_OUT].s = clksrc_out,
91 	[STR_USB_IT].s = "USBH Out",
92 	[STR_IO_IT].s = "USBD Out",
93 	[STR_USB_OT].s = "USBH In",
94 	[STR_IO_OT].s = "USBD In",
95 	[STR_AS_OUT_ALT0].s = "Playback Inactive",
96 	[STR_AS_OUT_ALT1].s = "Playback Active",
97 	[STR_AS_IN_ALT0].s = "Capture Inactive",
98 	[STR_AS_IN_ALT1].s = "Capture Active",
99 	{ },
100 };
101 
102 static struct usb_gadget_strings str_fn = {
103 	.language = 0x0409,	/* en-us */
104 	.strings = strings_fn,
105 };
106 
107 static struct usb_gadget_strings *fn_strings[] = {
108 	&str_fn,
109 	NULL,
110 };
111 
112 static struct usb_interface_assoc_descriptor iad_desc = {
113 	.bLength = sizeof iad_desc,
114 	.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
115 
116 	.bFirstInterface = 0,
117 	.bInterfaceCount = 3,
118 	.bFunctionClass = USB_CLASS_AUDIO,
119 	.bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
120 	.bFunctionProtocol = UAC_VERSION_2,
121 };
122 
123 /* Audio Control Interface */
124 static struct usb_interface_descriptor std_ac_if_desc = {
125 	.bLength = sizeof std_ac_if_desc,
126 	.bDescriptorType = USB_DT_INTERFACE,
127 
128 	.bAlternateSetting = 0,
129 	.bNumEndpoints = 0,
130 	.bInterfaceClass = USB_CLASS_AUDIO,
131 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
132 	.bInterfaceProtocol = UAC_VERSION_2,
133 };
134 
135 /* Clock source for IN traffic */
136 static struct uac_clock_source_descriptor in_clk_src_desc = {
137 	.bLength = sizeof in_clk_src_desc,
138 	.bDescriptorType = USB_DT_CS_INTERFACE,
139 
140 	.bDescriptorSubtype = UAC2_CLOCK_SOURCE,
141 	/* .bClockID = DYNAMIC */
142 	.bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
143 	.bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
144 	.bAssocTerminal = 0,
145 };
146 
147 /* Clock source for OUT traffic */
148 static struct uac_clock_source_descriptor out_clk_src_desc = {
149 	.bLength = sizeof out_clk_src_desc,
150 	.bDescriptorType = USB_DT_CS_INTERFACE,
151 
152 	.bDescriptorSubtype = UAC2_CLOCK_SOURCE,
153 	/* .bClockID = DYNAMIC */
154 	.bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
155 	.bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
156 	.bAssocTerminal = 0,
157 };
158 
159 /* Input Terminal for USB_OUT */
160 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
161 	.bLength = sizeof usb_out_it_desc,
162 	.bDescriptorType = USB_DT_CS_INTERFACE,
163 
164 	.bDescriptorSubtype = UAC_INPUT_TERMINAL,
165 	/* .bTerminalID = DYNAMIC */
166 	.wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
167 	.bAssocTerminal = 0,
168 	/* .bCSourceID = DYNAMIC */
169 	.iChannelNames = 0,
170 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
171 };
172 
173 /* Input Terminal for I/O-In */
174 static struct uac2_input_terminal_descriptor io_in_it_desc = {
175 	.bLength = sizeof io_in_it_desc,
176 	.bDescriptorType = USB_DT_CS_INTERFACE,
177 
178 	.bDescriptorSubtype = UAC_INPUT_TERMINAL,
179 	/* .bTerminalID = DYNAMIC */
180 	.wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
181 	.bAssocTerminal = 0,
182 	/* .bCSourceID = DYNAMIC */
183 	.iChannelNames = 0,
184 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
185 };
186 
187 /* Ouput Terminal for USB_IN */
188 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
189 	.bLength = sizeof usb_in_ot_desc,
190 	.bDescriptorType = USB_DT_CS_INTERFACE,
191 
192 	.bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
193 	/* .bTerminalID = DYNAMIC */
194 	.wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
195 	.bAssocTerminal = 0,
196 	/* .bSourceID = DYNAMIC */
197 	/* .bCSourceID = DYNAMIC */
198 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
199 };
200 
201 /* Ouput Terminal for I/O-Out */
202 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
203 	.bLength = sizeof io_out_ot_desc,
204 	.bDescriptorType = USB_DT_CS_INTERFACE,
205 
206 	.bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
207 	/* .bTerminalID = DYNAMIC */
208 	.wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
209 	.bAssocTerminal = 0,
210 	/* .bSourceID = DYNAMIC */
211 	/* .bCSourceID = DYNAMIC */
212 	.bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
213 };
214 
215 static struct uac2_ac_header_descriptor ac_hdr_desc = {
216 	.bLength = sizeof ac_hdr_desc,
217 	.bDescriptorType = USB_DT_CS_INTERFACE,
218 
219 	.bDescriptorSubtype = UAC_MS_HEADER,
220 	.bcdADC = cpu_to_le16(0x200),
221 	.bCategory = UAC2_FUNCTION_IO_BOX,
222 	/* .wTotalLength = DYNAMIC */
223 	.bmControls = 0,
224 };
225 
226 /* Audio Streaming OUT Interface - Alt0 */
227 static struct usb_interface_descriptor std_as_out_if0_desc = {
228 	.bLength = sizeof std_as_out_if0_desc,
229 	.bDescriptorType = USB_DT_INTERFACE,
230 
231 	.bAlternateSetting = 0,
232 	.bNumEndpoints = 0,
233 	.bInterfaceClass = USB_CLASS_AUDIO,
234 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
235 	.bInterfaceProtocol = UAC_VERSION_2,
236 };
237 
238 /* Audio Streaming OUT Interface - Alt1 */
239 static struct usb_interface_descriptor std_as_out_if1_desc = {
240 	.bLength = sizeof std_as_out_if1_desc,
241 	.bDescriptorType = USB_DT_INTERFACE,
242 
243 	.bAlternateSetting = 1,
244 	.bNumEndpoints = 1,
245 	.bInterfaceClass = USB_CLASS_AUDIO,
246 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
247 	.bInterfaceProtocol = UAC_VERSION_2,
248 };
249 
250 /* Audio Stream OUT Intface Desc */
251 static struct uac2_as_header_descriptor as_out_hdr_desc = {
252 	.bLength = sizeof as_out_hdr_desc,
253 	.bDescriptorType = USB_DT_CS_INTERFACE,
254 
255 	.bDescriptorSubtype = UAC_AS_GENERAL,
256 	/* .bTerminalLink = DYNAMIC */
257 	.bmControls = 0,
258 	.bFormatType = UAC_FORMAT_TYPE_I,
259 	.bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
260 	.iChannelNames = 0,
261 };
262 
263 /* Audio USB_OUT Format */
264 static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
265 	.bLength = sizeof as_out_fmt1_desc,
266 	.bDescriptorType = USB_DT_CS_INTERFACE,
267 	.bDescriptorSubtype = UAC_FORMAT_TYPE,
268 	.bFormatType = UAC_FORMAT_TYPE_I,
269 };
270 
271 /* STD AS ISO OUT Endpoint */
272 static struct usb_endpoint_descriptor fs_epout_desc = {
273 	.bLength = USB_DT_ENDPOINT_SIZE,
274 	.bDescriptorType = USB_DT_ENDPOINT,
275 
276 	.bEndpointAddress = USB_DIR_OUT,
277 	/* .bmAttributes = DYNAMIC */
278 	/* .wMaxPacketSize = DYNAMIC */
279 	.bInterval = 1,
280 };
281 
282 static struct usb_endpoint_descriptor hs_epout_desc = {
283 	.bLength = USB_DT_ENDPOINT_SIZE,
284 	.bDescriptorType = USB_DT_ENDPOINT,
285 
286 	/* .bmAttributes = DYNAMIC */
287 	/* .wMaxPacketSize = DYNAMIC */
288 	.bInterval = 4,
289 };
290 
291 static struct usb_endpoint_descriptor ss_epout_desc = {
292 	.bLength = USB_DT_ENDPOINT_SIZE,
293 	.bDescriptorType = USB_DT_ENDPOINT,
294 
295 	.bEndpointAddress = USB_DIR_OUT,
296 	/* .bmAttributes = DYNAMIC */
297 	/* .wMaxPacketSize = DYNAMIC */
298 	.bInterval = 4,
299 };
300 
301 static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
302 	.bLength		= sizeof(ss_epout_desc_comp),
303 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
304 	.bMaxBurst		= 0,
305 	.bmAttributes		= 0,
306 	/* wBytesPerInterval = DYNAMIC */
307 };
308 
309 /* CS AS ISO OUT Endpoint */
310 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
311 	.bLength = sizeof as_iso_out_desc,
312 	.bDescriptorType = USB_DT_CS_ENDPOINT,
313 
314 	.bDescriptorSubtype = UAC_EP_GENERAL,
315 	.bmAttributes = 0,
316 	.bmControls = 0,
317 	.bLockDelayUnits = 0,
318 	.wLockDelay = 0,
319 };
320 
321 /* STD AS ISO IN Feedback Endpoint */
322 static struct usb_endpoint_descriptor fs_epin_fback_desc = {
323 	.bLength = USB_DT_ENDPOINT_SIZE,
324 	.bDescriptorType = USB_DT_ENDPOINT,
325 
326 	.bEndpointAddress = USB_DIR_IN,
327 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
328 	.wMaxPacketSize = cpu_to_le16(3),
329 	.bInterval = 1,
330 };
331 
332 static struct usb_endpoint_descriptor hs_epin_fback_desc = {
333 	.bLength = USB_DT_ENDPOINT_SIZE,
334 	.bDescriptorType = USB_DT_ENDPOINT,
335 
336 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
337 	.wMaxPacketSize = cpu_to_le16(4),
338 	.bInterval = 4,
339 };
340 
341 static struct usb_endpoint_descriptor ss_epin_fback_desc = {
342 	.bLength = USB_DT_ENDPOINT_SIZE,
343 	.bDescriptorType = USB_DT_ENDPOINT,
344 
345 	.bEndpointAddress = USB_DIR_IN,
346 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
347 	.wMaxPacketSize = cpu_to_le16(4),
348 	.bInterval = 4,
349 };
350 
351 
352 /* Audio Streaming IN Interface - Alt0 */
353 static struct usb_interface_descriptor std_as_in_if0_desc = {
354 	.bLength = sizeof std_as_in_if0_desc,
355 	.bDescriptorType = USB_DT_INTERFACE,
356 
357 	.bAlternateSetting = 0,
358 	.bNumEndpoints = 0,
359 	.bInterfaceClass = USB_CLASS_AUDIO,
360 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
361 	.bInterfaceProtocol = UAC_VERSION_2,
362 };
363 
364 /* Audio Streaming IN Interface - Alt1 */
365 static struct usb_interface_descriptor std_as_in_if1_desc = {
366 	.bLength = sizeof std_as_in_if1_desc,
367 	.bDescriptorType = USB_DT_INTERFACE,
368 
369 	.bAlternateSetting = 1,
370 	.bNumEndpoints = 1,
371 	.bInterfaceClass = USB_CLASS_AUDIO,
372 	.bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
373 	.bInterfaceProtocol = UAC_VERSION_2,
374 };
375 
376 /* Audio Stream IN Intface Desc */
377 static struct uac2_as_header_descriptor as_in_hdr_desc = {
378 	.bLength = sizeof as_in_hdr_desc,
379 	.bDescriptorType = USB_DT_CS_INTERFACE,
380 
381 	.bDescriptorSubtype = UAC_AS_GENERAL,
382 	/* .bTerminalLink = DYNAMIC */
383 	.bmControls = 0,
384 	.bFormatType = UAC_FORMAT_TYPE_I,
385 	.bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
386 	.iChannelNames = 0,
387 };
388 
389 /* Audio USB_IN Format */
390 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
391 	.bLength = sizeof as_in_fmt1_desc,
392 	.bDescriptorType = USB_DT_CS_INTERFACE,
393 	.bDescriptorSubtype = UAC_FORMAT_TYPE,
394 	.bFormatType = UAC_FORMAT_TYPE_I,
395 };
396 
397 /* STD AS ISO IN Endpoint */
398 static struct usb_endpoint_descriptor fs_epin_desc = {
399 	.bLength = USB_DT_ENDPOINT_SIZE,
400 	.bDescriptorType = USB_DT_ENDPOINT,
401 
402 	.bEndpointAddress = USB_DIR_IN,
403 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
404 	/* .wMaxPacketSize = DYNAMIC */
405 	.bInterval = 1,
406 };
407 
408 static struct usb_endpoint_descriptor hs_epin_desc = {
409 	.bLength = USB_DT_ENDPOINT_SIZE,
410 	.bDescriptorType = USB_DT_ENDPOINT,
411 
412 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
413 	/* .wMaxPacketSize = DYNAMIC */
414 	.bInterval = 4,
415 };
416 
417 static struct usb_endpoint_descriptor ss_epin_desc = {
418 	.bLength = USB_DT_ENDPOINT_SIZE,
419 	.bDescriptorType = USB_DT_ENDPOINT,
420 
421 	.bEndpointAddress = USB_DIR_IN,
422 	.bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
423 	/* .wMaxPacketSize = DYNAMIC */
424 	.bInterval = 4,
425 };
426 
427 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
428 	.bLength		= sizeof(ss_epin_desc_comp),
429 	.bDescriptorType	= USB_DT_SS_ENDPOINT_COMP,
430 	.bMaxBurst		= 0,
431 	.bmAttributes		= 0,
432 	/* wBytesPerInterval = DYNAMIC */
433 };
434 
435 /* CS AS ISO IN Endpoint */
436 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
437 	.bLength = sizeof as_iso_in_desc,
438 	.bDescriptorType = USB_DT_CS_ENDPOINT,
439 
440 	.bDescriptorSubtype = UAC_EP_GENERAL,
441 	.bmAttributes = 0,
442 	.bmControls = 0,
443 	.bLockDelayUnits = 0,
444 	.wLockDelay = 0,
445 };
446 
447 static struct usb_descriptor_header *fs_audio_desc[] = {
448 	(struct usb_descriptor_header *)&iad_desc,
449 	(struct usb_descriptor_header *)&std_ac_if_desc,
450 
451 	(struct usb_descriptor_header *)&ac_hdr_desc,
452 	(struct usb_descriptor_header *)&in_clk_src_desc,
453 	(struct usb_descriptor_header *)&out_clk_src_desc,
454 	(struct usb_descriptor_header *)&usb_out_it_desc,
455 	(struct usb_descriptor_header *)&io_in_it_desc,
456 	(struct usb_descriptor_header *)&usb_in_ot_desc,
457 	(struct usb_descriptor_header *)&io_out_ot_desc,
458 
459 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
460 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
461 
462 	(struct usb_descriptor_header *)&as_out_hdr_desc,
463 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
464 	(struct usb_descriptor_header *)&fs_epout_desc,
465 	(struct usb_descriptor_header *)&as_iso_out_desc,
466 	(struct usb_descriptor_header *)&fs_epin_fback_desc,
467 
468 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
469 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
470 
471 	(struct usb_descriptor_header *)&as_in_hdr_desc,
472 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
473 	(struct usb_descriptor_header *)&fs_epin_desc,
474 	(struct usb_descriptor_header *)&as_iso_in_desc,
475 	NULL,
476 };
477 
478 static struct usb_descriptor_header *hs_audio_desc[] = {
479 	(struct usb_descriptor_header *)&iad_desc,
480 	(struct usb_descriptor_header *)&std_ac_if_desc,
481 
482 	(struct usb_descriptor_header *)&ac_hdr_desc,
483 	(struct usb_descriptor_header *)&in_clk_src_desc,
484 	(struct usb_descriptor_header *)&out_clk_src_desc,
485 	(struct usb_descriptor_header *)&usb_out_it_desc,
486 	(struct usb_descriptor_header *)&io_in_it_desc,
487 	(struct usb_descriptor_header *)&usb_in_ot_desc,
488 	(struct usb_descriptor_header *)&io_out_ot_desc,
489 
490 	(struct usb_descriptor_header *)&std_as_out_if0_desc,
491 	(struct usb_descriptor_header *)&std_as_out_if1_desc,
492 
493 	(struct usb_descriptor_header *)&as_out_hdr_desc,
494 	(struct usb_descriptor_header *)&as_out_fmt1_desc,
495 	(struct usb_descriptor_header *)&hs_epout_desc,
496 	(struct usb_descriptor_header *)&as_iso_out_desc,
497 	(struct usb_descriptor_header *)&hs_epin_fback_desc,
498 
499 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
500 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
501 
502 	(struct usb_descriptor_header *)&as_in_hdr_desc,
503 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
504 	(struct usb_descriptor_header *)&hs_epin_desc,
505 	(struct usb_descriptor_header *)&as_iso_in_desc,
506 	NULL,
507 };
508 
509 static struct usb_descriptor_header *ss_audio_desc[] = {
510 	(struct usb_descriptor_header *)&iad_desc,
511 	(struct usb_descriptor_header *)&std_ac_if_desc,
512 
513 	(struct usb_descriptor_header *)&ac_hdr_desc,
514 	(struct usb_descriptor_header *)&in_clk_src_desc,
515 	(struct usb_descriptor_header *)&out_clk_src_desc,
516 	(struct usb_descriptor_header *)&usb_out_it_desc,
517 	(struct usb_descriptor_header *)&io_in_it_desc,
518 	(struct usb_descriptor_header *)&usb_in_ot_desc,
519 	(struct usb_descriptor_header *)&io_out_ot_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 *)&ss_epout_desc,
527 	(struct usb_descriptor_header *)&ss_epout_desc_comp,
528 	(struct usb_descriptor_header *)&as_iso_out_desc,
529 	(struct usb_descriptor_header *)&ss_epin_fback_desc,
530 
531 	(struct usb_descriptor_header *)&std_as_in_if0_desc,
532 	(struct usb_descriptor_header *)&std_as_in_if1_desc,
533 
534 	(struct usb_descriptor_header *)&as_in_hdr_desc,
535 	(struct usb_descriptor_header *)&as_in_fmt1_desc,
536 	(struct usb_descriptor_header *)&ss_epin_desc,
537 	(struct usb_descriptor_header *)&ss_epin_desc_comp,
538 	(struct usb_descriptor_header *)&as_iso_in_desc,
539 	NULL,
540 };
541 
542 struct cntrl_cur_lay3 {
543 	__le32	dCUR;
544 };
545 
546 struct cntrl_range_lay3 {
547 	__le16	wNumSubRanges;
548 	__le32	dMIN;
549 	__le32	dMAX;
550 	__le32	dRES;
551 } __packed;
552 
553 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
554 	struct usb_endpoint_descriptor *ep_desc,
555 	enum usb_device_speed speed, bool is_playback)
556 {
557 	int chmask, srate, ssize;
558 	u16 max_size_bw, max_size_ep;
559 	unsigned int factor;
560 
561 	switch (speed) {
562 	case USB_SPEED_FULL:
563 		max_size_ep = 1023;
564 		factor = 1000;
565 		break;
566 
567 	case USB_SPEED_HIGH:
568 	case USB_SPEED_SUPER:
569 		max_size_ep = 1024;
570 		factor = 8000;
571 		break;
572 
573 	default:
574 		return -EINVAL;
575 	}
576 
577 	if (is_playback) {
578 		chmask = uac2_opts->p_chmask;
579 		srate = uac2_opts->p_srate;
580 		ssize = uac2_opts->p_ssize;
581 	} else {
582 		chmask = uac2_opts->c_chmask;
583 		srate = uac2_opts->c_srate;
584 		ssize = uac2_opts->c_ssize;
585 	}
586 
587 	if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
588 		srate = srate * (1000 + uac2_opts->fb_max) / 1000;
589 
590 	max_size_bw = num_channels(chmask) * ssize *
591 		DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
592 	ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
593 						    max_size_ep));
594 
595 	return 0;
596 }
597 
598 /* Use macro to overcome line length limitation */
599 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
600 
601 static void setup_headers(struct f_uac2_opts *opts,
602 			  struct usb_descriptor_header **headers,
603 			  enum usb_device_speed speed)
604 {
605 	struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
606 	struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
607 	struct usb_endpoint_descriptor *epout_desc;
608 	struct usb_endpoint_descriptor *epin_desc;
609 	struct usb_endpoint_descriptor *epin_fback_desc;
610 	int i;
611 
612 	switch (speed) {
613 	case USB_SPEED_FULL:
614 		epout_desc = &fs_epout_desc;
615 		epin_desc = &fs_epin_desc;
616 		epin_fback_desc = &fs_epin_fback_desc;
617 		break;
618 	case USB_SPEED_HIGH:
619 		epout_desc = &hs_epout_desc;
620 		epin_desc = &hs_epin_desc;
621 		epin_fback_desc = &hs_epin_fback_desc;
622 		break;
623 	default:
624 		epout_desc = &ss_epout_desc;
625 		epin_desc = &ss_epin_desc;
626 		epout_desc_comp = &ss_epout_desc_comp;
627 		epin_desc_comp = &ss_epin_desc_comp;
628 		epin_fback_desc = &ss_epin_fback_desc;
629 	}
630 
631 	i = 0;
632 	headers[i++] = USBDHDR(&iad_desc);
633 	headers[i++] = USBDHDR(&std_ac_if_desc);
634 	headers[i++] = USBDHDR(&ac_hdr_desc);
635 	if (EPIN_EN(opts))
636 		headers[i++] = USBDHDR(&in_clk_src_desc);
637 	if (EPOUT_EN(opts)) {
638 		headers[i++] = USBDHDR(&out_clk_src_desc);
639 		headers[i++] = USBDHDR(&usb_out_it_desc);
640 	}
641 	if (EPIN_EN(opts)) {
642 		headers[i++] = USBDHDR(&io_in_it_desc);
643 		headers[i++] = USBDHDR(&usb_in_ot_desc);
644 	}
645 	if (EPOUT_EN(opts)) {
646 		headers[i++] = USBDHDR(&io_out_ot_desc);
647 		headers[i++] = USBDHDR(&std_as_out_if0_desc);
648 		headers[i++] = USBDHDR(&std_as_out_if1_desc);
649 		headers[i++] = USBDHDR(&as_out_hdr_desc);
650 		headers[i++] = USBDHDR(&as_out_fmt1_desc);
651 		headers[i++] = USBDHDR(epout_desc);
652 		if (epout_desc_comp)
653 			headers[i++] = USBDHDR(epout_desc_comp);
654 
655 		headers[i++] = USBDHDR(&as_iso_out_desc);
656 
657 		if (EPOUT_FBACK_IN_EN(opts))
658 			headers[i++] = USBDHDR(epin_fback_desc);
659 	}
660 	if (EPIN_EN(opts)) {
661 		headers[i++] = USBDHDR(&std_as_in_if0_desc);
662 		headers[i++] = USBDHDR(&std_as_in_if1_desc);
663 		headers[i++] = USBDHDR(&as_in_hdr_desc);
664 		headers[i++] = USBDHDR(&as_in_fmt1_desc);
665 		headers[i++] = USBDHDR(epin_desc);
666 		if (epin_desc_comp)
667 			headers[i++] = USBDHDR(epin_desc_comp);
668 
669 		headers[i++] = USBDHDR(&as_iso_in_desc);
670 	}
671 	headers[i] = NULL;
672 }
673 
674 static void setup_descriptor(struct f_uac2_opts *opts)
675 {
676 	/* patch descriptors */
677 	int i = 1; /* ID's start with 1 */
678 
679 	if (EPOUT_EN(opts))
680 		usb_out_it_desc.bTerminalID = i++;
681 	if (EPIN_EN(opts))
682 		io_in_it_desc.bTerminalID = i++;
683 	if (EPOUT_EN(opts))
684 		io_out_ot_desc.bTerminalID = i++;
685 	if (EPIN_EN(opts))
686 		usb_in_ot_desc.bTerminalID = i++;
687 	if (EPOUT_EN(opts))
688 		out_clk_src_desc.bClockID = i++;
689 	if (EPIN_EN(opts))
690 		in_clk_src_desc.bClockID = i++;
691 
692 	usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
693 	usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
694 	usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
695 	io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
696 	io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
697 	io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
698 	as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
699 	as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
700 
701 	iad_desc.bInterfaceCount = 1;
702 	ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
703 
704 	if (EPIN_EN(opts)) {
705 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
706 
707 		len += sizeof(in_clk_src_desc);
708 		len += sizeof(usb_in_ot_desc);
709 		len += sizeof(io_in_it_desc);
710 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
711 		iad_desc.bInterfaceCount++;
712 	}
713 	if (EPOUT_EN(opts)) {
714 		u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
715 
716 		len += sizeof(out_clk_src_desc);
717 		len += sizeof(usb_out_it_desc);
718 		len += sizeof(io_out_ot_desc);
719 		ac_hdr_desc.wTotalLength = cpu_to_le16(len);
720 		iad_desc.bInterfaceCount++;
721 	}
722 
723 	setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
724 	setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
725 	setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
726 }
727 
728 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
729 {
730 	struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
731 
732 	if (!opts->p_chmask && !opts->c_chmask) {
733 		dev_err(dev, "Error: no playback and capture channels\n");
734 		return -EINVAL;
735 	} else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) {
736 		dev_err(dev, "Error: unsupported playback channels mask\n");
737 		return -EINVAL;
738 	} else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) {
739 		dev_err(dev, "Error: unsupported capture channels mask\n");
740 		return -EINVAL;
741 	} else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
742 		dev_err(dev, "Error: incorrect playback sample size\n");
743 		return -EINVAL;
744 	} else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
745 		dev_err(dev, "Error: incorrect capture sample size\n");
746 		return -EINVAL;
747 	} else if (!opts->p_srate) {
748 		dev_err(dev, "Error: incorrect playback sampling rate\n");
749 		return -EINVAL;
750 	} else if (!opts->c_srate) {
751 		dev_err(dev, "Error: incorrect capture sampling rate\n");
752 		return -EINVAL;
753 	}
754 
755 	return 0;
756 }
757 
758 static int
759 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
760 {
761 	struct f_uac2 *uac2 = func_to_uac2(fn);
762 	struct g_audio *agdev = func_to_g_audio(fn);
763 	struct usb_composite_dev *cdev = cfg->cdev;
764 	struct usb_gadget *gadget = cdev->gadget;
765 	struct device *dev = &gadget->dev;
766 	struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
767 	struct usb_string *us;
768 	int ret;
769 
770 	ret = afunc_validate_opts(agdev, dev);
771 	if (ret)
772 		return ret;
773 
774 	us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
775 	if (IS_ERR(us))
776 		return PTR_ERR(us);
777 	iad_desc.iFunction = us[STR_ASSOC].id;
778 	std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
779 	in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
780 	out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
781 	usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
782 	io_in_it_desc.iTerminal = us[STR_IO_IT].id;
783 	usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
784 	io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
785 	std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
786 	std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
787 	std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
788 	std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
789 
790 
791 	/* Initialize the configurable parameters */
792 	usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
793 	usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
794 	io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
795 	io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
796 	as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
797 	as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
798 	as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
799 	as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
800 	as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
801 	as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
802 	as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
803 	as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
804 
805 	snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
806 	snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
807 
808 	ret = usb_interface_id(cfg, fn);
809 	if (ret < 0) {
810 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
811 		return ret;
812 	}
813 	iad_desc.bFirstInterface = ret;
814 
815 	std_ac_if_desc.bInterfaceNumber = ret;
816 	uac2->ac_intf = ret;
817 	uac2->ac_alt = 0;
818 
819 	if (EPOUT_EN(uac2_opts)) {
820 		ret = usb_interface_id(cfg, fn);
821 		if (ret < 0) {
822 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
823 			return ret;
824 		}
825 		std_as_out_if0_desc.bInterfaceNumber = ret;
826 		std_as_out_if1_desc.bInterfaceNumber = ret;
827 		uac2->as_out_intf = ret;
828 		uac2->as_out_alt = 0;
829 
830 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
831 			fs_epout_desc.bmAttributes =
832 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
833 			hs_epout_desc.bmAttributes =
834 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
835 			ss_epout_desc.bmAttributes =
836 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
837 			std_as_out_if1_desc.bNumEndpoints++;
838 		} else {
839 			fs_epout_desc.bmAttributes =
840 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
841 			hs_epout_desc.bmAttributes =
842 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
843 			ss_epout_desc.bmAttributes =
844 			  USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
845 		}
846 	}
847 
848 	if (EPIN_EN(uac2_opts)) {
849 		ret = usb_interface_id(cfg, fn);
850 		if (ret < 0) {
851 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
852 			return ret;
853 		}
854 		std_as_in_if0_desc.bInterfaceNumber = ret;
855 		std_as_in_if1_desc.bInterfaceNumber = ret;
856 		uac2->as_in_intf = ret;
857 		uac2->as_in_alt = 0;
858 	}
859 
860 	/* Calculate wMaxPacketSize according to audio bandwidth */
861 	ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
862 				     true);
863 	if (ret < 0) {
864 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
865 		return ret;
866 	}
867 
868 	ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
869 				     false);
870 	if (ret < 0) {
871 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
872 		return ret;
873 	}
874 
875 	ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
876 				     true);
877 	if (ret < 0) {
878 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
879 		return ret;
880 	}
881 
882 	ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
883 				     false);
884 	if (ret < 0) {
885 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
886 		return ret;
887 	}
888 
889 	ret = set_ep_max_packet_size(uac2_opts, &ss_epin_desc, USB_SPEED_SUPER,
890 				     true);
891 	if (ret < 0) {
892 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
893 		return ret;
894 	}
895 
896 	ret = set_ep_max_packet_size(uac2_opts, &ss_epout_desc, USB_SPEED_SUPER,
897 				     false);
898 	if (ret < 0) {
899 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
900 		return ret;
901 	}
902 
903 	if (EPOUT_EN(uac2_opts)) {
904 		agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
905 		if (!agdev->out_ep) {
906 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
907 			return -ENODEV;
908 		}
909 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
910 			agdev->in_ep_fback = usb_ep_autoconfig(gadget,
911 						       &fs_epin_fback_desc);
912 			if (!agdev->in_ep_fback) {
913 				dev_err(dev, "%s:%d Error!\n",
914 					__func__, __LINE__);
915 				return -ENODEV;
916 			}
917 		}
918 	}
919 
920 	if (EPIN_EN(uac2_opts)) {
921 		agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
922 		if (!agdev->in_ep) {
923 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
924 			return -ENODEV;
925 		}
926 	}
927 
928 	agdev->in_ep_maxpsize = max_t(u16,
929 				le16_to_cpu(fs_epin_desc.wMaxPacketSize),
930 				le16_to_cpu(hs_epin_desc.wMaxPacketSize));
931 	agdev->out_ep_maxpsize = max_t(u16,
932 				le16_to_cpu(fs_epout_desc.wMaxPacketSize),
933 				le16_to_cpu(hs_epout_desc.wMaxPacketSize));
934 
935 	agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
936 				le16_to_cpu(ss_epin_desc.wMaxPacketSize));
937 	agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
938 				le16_to_cpu(ss_epout_desc.wMaxPacketSize));
939 
940 	hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
941 	hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
942 	hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
943 	ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
944 	ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
945 	ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
946 
947 	setup_descriptor(uac2_opts);
948 
949 	ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
950 				     ss_audio_desc);
951 	if (ret)
952 		return ret;
953 
954 	agdev->gadget = gadget;
955 
956 	agdev->params.p_chmask = uac2_opts->p_chmask;
957 	agdev->params.p_srate = uac2_opts->p_srate;
958 	agdev->params.p_ssize = uac2_opts->p_ssize;
959 	agdev->params.c_chmask = uac2_opts->c_chmask;
960 	agdev->params.c_srate = uac2_opts->c_srate;
961 	agdev->params.c_ssize = uac2_opts->c_ssize;
962 	agdev->params.req_number = uac2_opts->req_number;
963 	agdev->params.fb_max = uac2_opts->fb_max;
964 	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
965 	if (ret)
966 		goto err_free_descs;
967 	return 0;
968 
969 err_free_descs:
970 	usb_free_all_descriptors(fn);
971 	agdev->gadget = NULL;
972 	return ret;
973 }
974 
975 static int
976 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
977 {
978 	struct usb_composite_dev *cdev = fn->config->cdev;
979 	struct f_uac2 *uac2 = func_to_uac2(fn);
980 	struct usb_gadget *gadget = cdev->gadget;
981 	struct device *dev = &gadget->dev;
982 	int ret = 0;
983 
984 	/* No i/f has more than 2 alt settings */
985 	if (alt > 1) {
986 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
987 		return -EINVAL;
988 	}
989 
990 	if (intf == uac2->ac_intf) {
991 		/* Control I/f has only 1 AltSetting - 0 */
992 		if (alt) {
993 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
994 			return -EINVAL;
995 		}
996 		return 0;
997 	}
998 
999 	if (intf == uac2->as_out_intf) {
1000 		uac2->as_out_alt = alt;
1001 
1002 		if (alt)
1003 			ret = u_audio_start_capture(&uac2->g_audio);
1004 		else
1005 			u_audio_stop_capture(&uac2->g_audio);
1006 	} else if (intf == uac2->as_in_intf) {
1007 		uac2->as_in_alt = alt;
1008 
1009 		if (alt)
1010 			ret = u_audio_start_playback(&uac2->g_audio);
1011 		else
1012 			u_audio_stop_playback(&uac2->g_audio);
1013 	} else {
1014 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1015 		return -EINVAL;
1016 	}
1017 
1018 	return ret;
1019 }
1020 
1021 static int
1022 afunc_get_alt(struct usb_function *fn, unsigned intf)
1023 {
1024 	struct f_uac2 *uac2 = func_to_uac2(fn);
1025 	struct g_audio *agdev = func_to_g_audio(fn);
1026 
1027 	if (intf == uac2->ac_intf)
1028 		return uac2->ac_alt;
1029 	else if (intf == uac2->as_out_intf)
1030 		return uac2->as_out_alt;
1031 	else if (intf == uac2->as_in_intf)
1032 		return uac2->as_in_alt;
1033 	else
1034 		dev_err(&agdev->gadget->dev,
1035 			"%s:%d Invalid Interface %d!\n",
1036 			__func__, __LINE__, intf);
1037 
1038 	return -EINVAL;
1039 }
1040 
1041 static void
1042 afunc_disable(struct usb_function *fn)
1043 {
1044 	struct f_uac2 *uac2 = func_to_uac2(fn);
1045 
1046 	uac2->as_in_alt = 0;
1047 	uac2->as_out_alt = 0;
1048 	u_audio_stop_capture(&uac2->g_audio);
1049 	u_audio_stop_playback(&uac2->g_audio);
1050 }
1051 
1052 static int
1053 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1054 {
1055 	struct usb_request *req = fn->config->cdev->req;
1056 	struct g_audio *agdev = func_to_g_audio(fn);
1057 	struct f_uac2_opts *opts;
1058 	u16 w_length = le16_to_cpu(cr->wLength);
1059 	u16 w_index = le16_to_cpu(cr->wIndex);
1060 	u16 w_value = le16_to_cpu(cr->wValue);
1061 	u8 entity_id = (w_index >> 8) & 0xff;
1062 	u8 control_selector = w_value >> 8;
1063 	int value = -EOPNOTSUPP;
1064 	int p_srate, c_srate;
1065 
1066 	opts = g_audio_to_uac2_opts(agdev);
1067 	p_srate = opts->p_srate;
1068 	c_srate = opts->c_srate;
1069 
1070 	if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1071 		struct cntrl_cur_lay3 c;
1072 		memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1073 
1074 		if (entity_id == USB_IN_CLK_ID)
1075 			c.dCUR = cpu_to_le32(p_srate);
1076 		else if (entity_id == USB_OUT_CLK_ID)
1077 			c.dCUR = cpu_to_le32(c_srate);
1078 
1079 		value = min_t(unsigned, w_length, sizeof c);
1080 		memcpy(req->buf, &c, value);
1081 	} else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1082 		*(u8 *)req->buf = 1;
1083 		value = min_t(unsigned, w_length, 1);
1084 	} else {
1085 		dev_err(&agdev->gadget->dev,
1086 			"%s:%d control_selector=%d TODO!\n",
1087 			__func__, __LINE__, control_selector);
1088 	}
1089 
1090 	return value;
1091 }
1092 
1093 static int
1094 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1095 {
1096 	struct usb_request *req = fn->config->cdev->req;
1097 	struct g_audio *agdev = func_to_g_audio(fn);
1098 	struct f_uac2_opts *opts;
1099 	u16 w_length = le16_to_cpu(cr->wLength);
1100 	u16 w_index = le16_to_cpu(cr->wIndex);
1101 	u16 w_value = le16_to_cpu(cr->wValue);
1102 	u8 entity_id = (w_index >> 8) & 0xff;
1103 	u8 control_selector = w_value >> 8;
1104 	struct cntrl_range_lay3 r;
1105 	int value = -EOPNOTSUPP;
1106 	int p_srate, c_srate;
1107 
1108 	opts = g_audio_to_uac2_opts(agdev);
1109 	p_srate = opts->p_srate;
1110 	c_srate = opts->c_srate;
1111 
1112 	if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1113 		if (entity_id == USB_IN_CLK_ID)
1114 			r.dMIN = cpu_to_le32(p_srate);
1115 		else if (entity_id == USB_OUT_CLK_ID)
1116 			r.dMIN = cpu_to_le32(c_srate);
1117 		else
1118 			return -EOPNOTSUPP;
1119 
1120 		r.dMAX = r.dMIN;
1121 		r.dRES = 0;
1122 		r.wNumSubRanges = cpu_to_le16(1);
1123 
1124 		value = min_t(unsigned, w_length, sizeof r);
1125 		memcpy(req->buf, &r, value);
1126 	} else {
1127 		dev_err(&agdev->gadget->dev,
1128 			"%s:%d control_selector=%d TODO!\n",
1129 			__func__, __LINE__, control_selector);
1130 	}
1131 
1132 	return value;
1133 }
1134 
1135 static int
1136 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1137 {
1138 	if (cr->bRequest == UAC2_CS_CUR)
1139 		return in_rq_cur(fn, cr);
1140 	else if (cr->bRequest == UAC2_CS_RANGE)
1141 		return in_rq_range(fn, cr);
1142 	else
1143 		return -EOPNOTSUPP;
1144 }
1145 
1146 static int
1147 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1148 {
1149 	u16 w_length = le16_to_cpu(cr->wLength);
1150 	u16 w_value = le16_to_cpu(cr->wValue);
1151 	u8 control_selector = w_value >> 8;
1152 
1153 	if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1154 		return w_length;
1155 
1156 	return -EOPNOTSUPP;
1157 }
1158 
1159 static int
1160 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1161 {
1162 	struct f_uac2 *uac2 = func_to_uac2(fn);
1163 	struct g_audio *agdev = func_to_g_audio(fn);
1164 	u16 w_index = le16_to_cpu(cr->wIndex);
1165 	u8 intf = w_index & 0xff;
1166 
1167 	if (intf != uac2->ac_intf) {
1168 		dev_err(&agdev->gadget->dev,
1169 			"%s:%d Error!\n", __func__, __LINE__);
1170 		return -EOPNOTSUPP;
1171 	}
1172 
1173 	if (cr->bRequestType & USB_DIR_IN)
1174 		return ac_rq_in(fn, cr);
1175 	else if (cr->bRequest == UAC2_CS_CUR)
1176 		return out_rq_cur(fn, cr);
1177 
1178 	return -EOPNOTSUPP;
1179 }
1180 
1181 static int
1182 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1183 {
1184 	struct usb_composite_dev *cdev = fn->config->cdev;
1185 	struct g_audio *agdev = func_to_g_audio(fn);
1186 	struct usb_request *req = cdev->req;
1187 	u16 w_length = le16_to_cpu(cr->wLength);
1188 	int value = -EOPNOTSUPP;
1189 
1190 	/* Only Class specific requests are supposed to reach here */
1191 	if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1192 		return -EOPNOTSUPP;
1193 
1194 	if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1195 		value = setup_rq_inf(fn, cr);
1196 	else
1197 		dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1198 				__func__, __LINE__);
1199 
1200 	if (value >= 0) {
1201 		req->length = value;
1202 		req->zero = value < w_length;
1203 		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1204 		if (value < 0) {
1205 			dev_err(&agdev->gadget->dev,
1206 				"%s:%d Error!\n", __func__, __LINE__);
1207 			req->status = 0;
1208 		}
1209 	}
1210 
1211 	return value;
1212 }
1213 
1214 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1215 {
1216 	return container_of(to_config_group(item), struct f_uac2_opts,
1217 			    func_inst.group);
1218 }
1219 
1220 static void f_uac2_attr_release(struct config_item *item)
1221 {
1222 	struct f_uac2_opts *opts = to_f_uac2_opts(item);
1223 
1224 	usb_put_function_instance(&opts->func_inst);
1225 }
1226 
1227 static struct configfs_item_operations f_uac2_item_ops = {
1228 	.release	= f_uac2_attr_release,
1229 };
1230 
1231 #define UAC2_ATTRIBUTE(name)						\
1232 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1233 					 char *page)			\
1234 {									\
1235 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1236 	int result;							\
1237 									\
1238 	mutex_lock(&opts->lock);					\
1239 	result = sprintf(page, "%u\n", opts->name);			\
1240 	mutex_unlock(&opts->lock);					\
1241 									\
1242 	return result;							\
1243 }									\
1244 									\
1245 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1246 					  const char *page, size_t len)	\
1247 {									\
1248 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1249 	int ret;							\
1250 	u32 num;							\
1251 									\
1252 	mutex_lock(&opts->lock);					\
1253 	if (opts->refcnt) {						\
1254 		ret = -EBUSY;						\
1255 		goto end;						\
1256 	}								\
1257 									\
1258 	ret = kstrtou32(page, 0, &num);					\
1259 	if (ret)							\
1260 		goto end;						\
1261 									\
1262 	opts->name = num;						\
1263 	ret = len;							\
1264 									\
1265 end:									\
1266 	mutex_unlock(&opts->lock);					\
1267 	return ret;							\
1268 }									\
1269 									\
1270 CONFIGFS_ATTR(f_uac2_opts_, name)
1271 
1272 #define UAC2_ATTRIBUTE_SYNC(name)					\
1273 static ssize_t f_uac2_opts_##name##_show(struct config_item *item,	\
1274 					 char *page)			\
1275 {									\
1276 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1277 	int result;							\
1278 	char *str;							\
1279 									\
1280 	mutex_lock(&opts->lock);					\
1281 	switch (opts->name) {						\
1282 	case USB_ENDPOINT_SYNC_ASYNC:					\
1283 		str = "async";						\
1284 		break;							\
1285 	case USB_ENDPOINT_SYNC_ADAPTIVE:				\
1286 		str = "adaptive";					\
1287 		break;							\
1288 	default:							\
1289 		str = "unknown";					\
1290 		break;							\
1291 	}								\
1292 	result = sprintf(page, "%s\n", str);				\
1293 	mutex_unlock(&opts->lock);					\
1294 									\
1295 	return result;							\
1296 }									\
1297 									\
1298 static ssize_t f_uac2_opts_##name##_store(struct config_item *item,	\
1299 					  const char *page, size_t len)	\
1300 {									\
1301 	struct f_uac2_opts *opts = to_f_uac2_opts(item);		\
1302 	int ret = 0;							\
1303 									\
1304 	mutex_lock(&opts->lock);					\
1305 	if (opts->refcnt) {						\
1306 		ret = -EBUSY;						\
1307 		goto end;						\
1308 	}								\
1309 									\
1310 	if (!strncmp(page, "async", 5))					\
1311 		opts->name = USB_ENDPOINT_SYNC_ASYNC;			\
1312 	else if (!strncmp(page, "adaptive", 8))				\
1313 		opts->name = USB_ENDPOINT_SYNC_ADAPTIVE;		\
1314 	else {								\
1315 		ret = -EINVAL;						\
1316 		goto end;						\
1317 	}								\
1318 									\
1319 	ret = len;							\
1320 									\
1321 end:									\
1322 	mutex_unlock(&opts->lock);					\
1323 	return ret;							\
1324 }									\
1325 									\
1326 CONFIGFS_ATTR(f_uac2_opts_, name)
1327 
1328 UAC2_ATTRIBUTE(p_chmask);
1329 UAC2_ATTRIBUTE(p_srate);
1330 UAC2_ATTRIBUTE(p_ssize);
1331 UAC2_ATTRIBUTE(c_chmask);
1332 UAC2_ATTRIBUTE(c_srate);
1333 UAC2_ATTRIBUTE_SYNC(c_sync);
1334 UAC2_ATTRIBUTE(c_ssize);
1335 UAC2_ATTRIBUTE(req_number);
1336 UAC2_ATTRIBUTE(fb_max);
1337 
1338 static struct configfs_attribute *f_uac2_attrs[] = {
1339 	&f_uac2_opts_attr_p_chmask,
1340 	&f_uac2_opts_attr_p_srate,
1341 	&f_uac2_opts_attr_p_ssize,
1342 	&f_uac2_opts_attr_c_chmask,
1343 	&f_uac2_opts_attr_c_srate,
1344 	&f_uac2_opts_attr_c_ssize,
1345 	&f_uac2_opts_attr_c_sync,
1346 	&f_uac2_opts_attr_req_number,
1347 	&f_uac2_opts_attr_fb_max,
1348 	NULL,
1349 };
1350 
1351 static const struct config_item_type f_uac2_func_type = {
1352 	.ct_item_ops	= &f_uac2_item_ops,
1353 	.ct_attrs	= f_uac2_attrs,
1354 	.ct_owner	= THIS_MODULE,
1355 };
1356 
1357 static void afunc_free_inst(struct usb_function_instance *f)
1358 {
1359 	struct f_uac2_opts *opts;
1360 
1361 	opts = container_of(f, struct f_uac2_opts, func_inst);
1362 	kfree(opts);
1363 }
1364 
1365 static struct usb_function_instance *afunc_alloc_inst(void)
1366 {
1367 	struct f_uac2_opts *opts;
1368 
1369 	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1370 	if (!opts)
1371 		return ERR_PTR(-ENOMEM);
1372 
1373 	mutex_init(&opts->lock);
1374 	opts->func_inst.free_func_inst = afunc_free_inst;
1375 
1376 	config_group_init_type_name(&opts->func_inst.group, "",
1377 				    &f_uac2_func_type);
1378 
1379 	opts->p_chmask = UAC2_DEF_PCHMASK;
1380 	opts->p_srate = UAC2_DEF_PSRATE;
1381 	opts->p_ssize = UAC2_DEF_PSSIZE;
1382 	opts->c_chmask = UAC2_DEF_CCHMASK;
1383 	opts->c_srate = UAC2_DEF_CSRATE;
1384 	opts->c_ssize = UAC2_DEF_CSSIZE;
1385 	opts->c_sync = UAC2_DEF_CSYNC;
1386 	opts->req_number = UAC2_DEF_REQ_NUM;
1387 	opts->fb_max = UAC2_DEF_FB_MAX;
1388 	return &opts->func_inst;
1389 }
1390 
1391 static void afunc_free(struct usb_function *f)
1392 {
1393 	struct g_audio *agdev;
1394 	struct f_uac2_opts *opts;
1395 
1396 	agdev = func_to_g_audio(f);
1397 	opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1398 	kfree(agdev);
1399 	mutex_lock(&opts->lock);
1400 	--opts->refcnt;
1401 	mutex_unlock(&opts->lock);
1402 }
1403 
1404 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1405 {
1406 	struct g_audio *agdev = func_to_g_audio(f);
1407 
1408 	g_audio_cleanup(agdev);
1409 	usb_free_all_descriptors(f);
1410 
1411 	agdev->gadget = NULL;
1412 }
1413 
1414 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1415 {
1416 	struct f_uac2	*uac2;
1417 	struct f_uac2_opts *opts;
1418 
1419 	uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
1420 	if (uac2 == NULL)
1421 		return ERR_PTR(-ENOMEM);
1422 
1423 	opts = container_of(fi, struct f_uac2_opts, func_inst);
1424 	mutex_lock(&opts->lock);
1425 	++opts->refcnt;
1426 	mutex_unlock(&opts->lock);
1427 
1428 	uac2->g_audio.func.name = "uac2_func";
1429 	uac2->g_audio.func.bind = afunc_bind;
1430 	uac2->g_audio.func.unbind = afunc_unbind;
1431 	uac2->g_audio.func.set_alt = afunc_set_alt;
1432 	uac2->g_audio.func.get_alt = afunc_get_alt;
1433 	uac2->g_audio.func.disable = afunc_disable;
1434 	uac2->g_audio.func.setup = afunc_setup;
1435 	uac2->g_audio.func.free_func = afunc_free;
1436 
1437 	return &uac2->g_audio.func;
1438 }
1439 
1440 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1441 MODULE_LICENSE("GPL");
1442 MODULE_AUTHOR("Yadwinder Singh");
1443 MODULE_AUTHOR("Jaswinder Singh");
1444