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