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