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