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