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