1 // SPDX-License-Identifier: GPL-2.0+
3 * f_uac2.c -- USB Audio Class 2.0 Function
6 * Yadwinder Singh (yadi.brar01@gmail.com)
7 * Jaswinder Singh (jaswinder.singh@linaro.org)
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 #include <linux/module.h>
17 /* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
18 #define UAC2_CHANNEL_MASK 0x07FFFFFF
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
28 #define USB_OUT_CLK_ID (out_clk_src_desc.bClockID)
29 #define USB_IN_CLK_ID (in_clk_src_desc.bClockID)
31 #define CONTROL_ABSENT 0
32 #define CONTROL_RDONLY 1
33 #define CONTROL_RDWR 3
35 #define CLK_FREQ_CTRL 0
36 #define CLK_VLD_CTRL 2
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)
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() */
55 static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
57 return container_of(f, struct f_uac2, g_audio.func);
61 struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
63 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
66 /* --------- USB Function Interface ------------- */
83 static char clksrc_in[8];
84 static char clksrc_out[8];
86 static 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",
102 static struct usb_gadget_strings str_fn = {
103 .language = 0x0409, /* en-us */
104 .strings = strings_fn,
107 static struct usb_gadget_strings *fn_strings[] = {
112 static struct usb_interface_assoc_descriptor iad_desc = {
113 .bLength = sizeof iad_desc,
114 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
116 .bFirstInterface = 0,
117 .bInterfaceCount = 3,
118 .bFunctionClass = USB_CLASS_AUDIO,
119 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
120 .bFunctionProtocol = UAC_VERSION_2,
123 /* Audio Control Interface */
124 static struct usb_interface_descriptor std_ac_if_desc = {
125 .bLength = sizeof std_ac_if_desc,
126 .bDescriptorType = USB_DT_INTERFACE,
128 .bAlternateSetting = 0,
130 .bInterfaceClass = USB_CLASS_AUDIO,
131 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
132 .bInterfaceProtocol = UAC_VERSION_2,
135 /* Clock source for IN traffic */
136 static struct uac_clock_source_descriptor in_clk_src_desc = {
137 .bLength = sizeof in_clk_src_desc,
138 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
141 /* .bClockID = DYNAMIC */
142 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
143 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
147 /* Clock source for OUT traffic */
148 static struct uac_clock_source_descriptor out_clk_src_desc = {
149 .bLength = sizeof out_clk_src_desc,
150 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
153 /* .bClockID = DYNAMIC */
154 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
155 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
159 /* Input Terminal for USB_OUT */
160 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
161 .bLength = sizeof usb_out_it_desc,
162 .bDescriptorType = USB_DT_CS_INTERFACE,
164 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
165 /* .bTerminalID = DYNAMIC */
166 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
168 /* .bCSourceID = DYNAMIC */
170 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
173 /* Input Terminal for I/O-In */
174 static struct uac2_input_terminal_descriptor io_in_it_desc = {
175 .bLength = sizeof io_in_it_desc,
176 .bDescriptorType = USB_DT_CS_INTERFACE,
178 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
179 /* .bTerminalID = DYNAMIC */
180 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
182 /* .bCSourceID = DYNAMIC */
184 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
187 /* Ouput Terminal for USB_IN */
188 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
189 .bLength = sizeof usb_in_ot_desc,
190 .bDescriptorType = USB_DT_CS_INTERFACE,
192 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
193 /* .bTerminalID = DYNAMIC */
194 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
196 /* .bSourceID = DYNAMIC */
197 /* .bCSourceID = DYNAMIC */
198 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
201 /* Ouput Terminal for I/O-Out */
202 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
203 .bLength = sizeof io_out_ot_desc,
204 .bDescriptorType = USB_DT_CS_INTERFACE,
206 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
207 /* .bTerminalID = DYNAMIC */
208 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
210 /* .bSourceID = DYNAMIC */
211 /* .bCSourceID = DYNAMIC */
212 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
215 static struct uac2_ac_header_descriptor ac_hdr_desc = {
216 .bLength = sizeof ac_hdr_desc,
217 .bDescriptorType = USB_DT_CS_INTERFACE,
219 .bDescriptorSubtype = UAC_MS_HEADER,
220 .bcdADC = cpu_to_le16(0x200),
221 .bCategory = UAC2_FUNCTION_IO_BOX,
222 /* .wTotalLength = DYNAMIC */
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,
231 .bAlternateSetting = 0,
233 .bInterfaceClass = USB_CLASS_AUDIO,
234 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
235 .bInterfaceProtocol = UAC_VERSION_2,
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,
243 .bAlternateSetting = 1,
245 .bInterfaceClass = USB_CLASS_AUDIO,
246 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
247 .bInterfaceProtocol = UAC_VERSION_2,
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,
255 .bDescriptorSubtype = UAC_AS_GENERAL,
256 /* .bTerminalLink = DYNAMIC */
258 .bFormatType = UAC_FORMAT_TYPE_I,
259 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
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,
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,
276 .bEndpointAddress = USB_DIR_OUT,
277 /* .bmAttributes = DYNAMIC */
278 /* .wMaxPacketSize = DYNAMIC */
282 static struct usb_endpoint_descriptor hs_epout_desc = {
283 .bLength = USB_DT_ENDPOINT_SIZE,
284 .bDescriptorType = USB_DT_ENDPOINT,
286 /* .bmAttributes = DYNAMIC */
287 /* .wMaxPacketSize = DYNAMIC */
291 static struct usb_endpoint_descriptor ss_epout_desc = {
292 .bLength = USB_DT_ENDPOINT_SIZE,
293 .bDescriptorType = USB_DT_ENDPOINT,
295 .bEndpointAddress = USB_DIR_OUT,
296 /* .bmAttributes = DYNAMIC */
297 /* .wMaxPacketSize = DYNAMIC */
301 static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
302 .bLength = sizeof(ss_epout_desc_comp),
303 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
306 /* wBytesPerInterval = DYNAMIC */
309 /* CS AS ISO OUT Endpoint */
310 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
311 .bLength = sizeof as_iso_out_desc,
312 .bDescriptorType = USB_DT_CS_ENDPOINT,
314 .bDescriptorSubtype = UAC_EP_GENERAL,
317 .bLockDelayUnits = 0,
321 /* STD AS ISO IN Feedback Endpoint */
322 static struct usb_endpoint_descriptor fs_epin_fback_desc = {
323 .bLength = USB_DT_ENDPOINT_SIZE,
324 .bDescriptorType = USB_DT_ENDPOINT,
326 .bEndpointAddress = USB_DIR_IN,
327 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
328 .wMaxPacketSize = cpu_to_le16(3),
332 static struct usb_endpoint_descriptor hs_epin_fback_desc = {
333 .bLength = USB_DT_ENDPOINT_SIZE,
334 .bDescriptorType = USB_DT_ENDPOINT,
336 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
337 .wMaxPacketSize = cpu_to_le16(4),
341 static struct usb_endpoint_descriptor ss_epin_fback_desc = {
342 .bLength = USB_DT_ENDPOINT_SIZE,
343 .bDescriptorType = USB_DT_ENDPOINT,
345 .bEndpointAddress = USB_DIR_IN,
346 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
347 .wMaxPacketSize = cpu_to_le16(4),
352 /* Audio Streaming IN Interface - Alt0 */
353 static struct usb_interface_descriptor std_as_in_if0_desc = {
354 .bLength = sizeof std_as_in_if0_desc,
355 .bDescriptorType = USB_DT_INTERFACE,
357 .bAlternateSetting = 0,
359 .bInterfaceClass = USB_CLASS_AUDIO,
360 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
361 .bInterfaceProtocol = UAC_VERSION_2,
364 /* Audio Streaming IN Interface - Alt1 */
365 static struct usb_interface_descriptor std_as_in_if1_desc = {
366 .bLength = sizeof std_as_in_if1_desc,
367 .bDescriptorType = USB_DT_INTERFACE,
369 .bAlternateSetting = 1,
371 .bInterfaceClass = USB_CLASS_AUDIO,
372 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
373 .bInterfaceProtocol = UAC_VERSION_2,
376 /* Audio Stream IN Intface Desc */
377 static struct uac2_as_header_descriptor as_in_hdr_desc = {
378 .bLength = sizeof as_in_hdr_desc,
379 .bDescriptorType = USB_DT_CS_INTERFACE,
381 .bDescriptorSubtype = UAC_AS_GENERAL,
382 /* .bTerminalLink = DYNAMIC */
384 .bFormatType = UAC_FORMAT_TYPE_I,
385 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
389 /* Audio USB_IN Format */
390 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
391 .bLength = sizeof as_in_fmt1_desc,
392 .bDescriptorType = USB_DT_CS_INTERFACE,
393 .bDescriptorSubtype = UAC_FORMAT_TYPE,
394 .bFormatType = UAC_FORMAT_TYPE_I,
397 /* STD AS ISO IN Endpoint */
398 static struct usb_endpoint_descriptor fs_epin_desc = {
399 .bLength = USB_DT_ENDPOINT_SIZE,
400 .bDescriptorType = USB_DT_ENDPOINT,
402 .bEndpointAddress = USB_DIR_IN,
403 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
404 /* .wMaxPacketSize = DYNAMIC */
408 static struct usb_endpoint_descriptor hs_epin_desc = {
409 .bLength = USB_DT_ENDPOINT_SIZE,
410 .bDescriptorType = USB_DT_ENDPOINT,
412 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
413 /* .wMaxPacketSize = DYNAMIC */
417 static struct usb_endpoint_descriptor ss_epin_desc = {
418 .bLength = USB_DT_ENDPOINT_SIZE,
419 .bDescriptorType = USB_DT_ENDPOINT,
421 .bEndpointAddress = USB_DIR_IN,
422 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
423 /* .wMaxPacketSize = DYNAMIC */
427 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
428 .bLength = sizeof(ss_epin_desc_comp),
429 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
432 /* wBytesPerInterval = DYNAMIC */
435 /* CS AS ISO IN Endpoint */
436 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
437 .bLength = sizeof as_iso_in_desc,
438 .bDescriptorType = USB_DT_CS_ENDPOINT,
440 .bDescriptorSubtype = UAC_EP_GENERAL,
443 .bLockDelayUnits = 0,
447 static struct usb_descriptor_header *fs_audio_desc[] = {
448 (struct usb_descriptor_header *)&iad_desc,
449 (struct usb_descriptor_header *)&std_ac_if_desc,
451 (struct usb_descriptor_header *)&ac_hdr_desc,
452 (struct usb_descriptor_header *)&in_clk_src_desc,
453 (struct usb_descriptor_header *)&out_clk_src_desc,
454 (struct usb_descriptor_header *)&usb_out_it_desc,
455 (struct usb_descriptor_header *)&io_in_it_desc,
456 (struct usb_descriptor_header *)&usb_in_ot_desc,
457 (struct usb_descriptor_header *)&io_out_ot_desc,
459 (struct usb_descriptor_header *)&std_as_out_if0_desc,
460 (struct usb_descriptor_header *)&std_as_out_if1_desc,
462 (struct usb_descriptor_header *)&as_out_hdr_desc,
463 (struct usb_descriptor_header *)&as_out_fmt1_desc,
464 (struct usb_descriptor_header *)&fs_epout_desc,
465 (struct usb_descriptor_header *)&as_iso_out_desc,
466 (struct usb_descriptor_header *)&fs_epin_fback_desc,
468 (struct usb_descriptor_header *)&std_as_in_if0_desc,
469 (struct usb_descriptor_header *)&std_as_in_if1_desc,
471 (struct usb_descriptor_header *)&as_in_hdr_desc,
472 (struct usb_descriptor_header *)&as_in_fmt1_desc,
473 (struct usb_descriptor_header *)&fs_epin_desc,
474 (struct usb_descriptor_header *)&as_iso_in_desc,
478 static struct usb_descriptor_header *hs_audio_desc[] = {
479 (struct usb_descriptor_header *)&iad_desc,
480 (struct usb_descriptor_header *)&std_ac_if_desc,
482 (struct usb_descriptor_header *)&ac_hdr_desc,
483 (struct usb_descriptor_header *)&in_clk_src_desc,
484 (struct usb_descriptor_header *)&out_clk_src_desc,
485 (struct usb_descriptor_header *)&usb_out_it_desc,
486 (struct usb_descriptor_header *)&io_in_it_desc,
487 (struct usb_descriptor_header *)&usb_in_ot_desc,
488 (struct usb_descriptor_header *)&io_out_ot_desc,
490 (struct usb_descriptor_header *)&std_as_out_if0_desc,
491 (struct usb_descriptor_header *)&std_as_out_if1_desc,
493 (struct usb_descriptor_header *)&as_out_hdr_desc,
494 (struct usb_descriptor_header *)&as_out_fmt1_desc,
495 (struct usb_descriptor_header *)&hs_epout_desc,
496 (struct usb_descriptor_header *)&as_iso_out_desc,
497 (struct usb_descriptor_header *)&hs_epin_fback_desc,
499 (struct usb_descriptor_header *)&std_as_in_if0_desc,
500 (struct usb_descriptor_header *)&std_as_in_if1_desc,
502 (struct usb_descriptor_header *)&as_in_hdr_desc,
503 (struct usb_descriptor_header *)&as_in_fmt1_desc,
504 (struct usb_descriptor_header *)&hs_epin_desc,
505 (struct usb_descriptor_header *)&as_iso_in_desc,
509 static struct usb_descriptor_header *ss_audio_desc[] = {
510 (struct usb_descriptor_header *)&iad_desc,
511 (struct usb_descriptor_header *)&std_ac_if_desc,
513 (struct usb_descriptor_header *)&ac_hdr_desc,
514 (struct usb_descriptor_header *)&in_clk_src_desc,
515 (struct usb_descriptor_header *)&out_clk_src_desc,
516 (struct usb_descriptor_header *)&usb_out_it_desc,
517 (struct usb_descriptor_header *)&io_in_it_desc,
518 (struct usb_descriptor_header *)&usb_in_ot_desc,
519 (struct usb_descriptor_header *)&io_out_ot_desc,
521 (struct usb_descriptor_header *)&std_as_out_if0_desc,
522 (struct usb_descriptor_header *)&std_as_out_if1_desc,
524 (struct usb_descriptor_header *)&as_out_hdr_desc,
525 (struct usb_descriptor_header *)&as_out_fmt1_desc,
526 (struct usb_descriptor_header *)&ss_epout_desc,
527 (struct usb_descriptor_header *)&ss_epout_desc_comp,
528 (struct usb_descriptor_header *)&as_iso_out_desc,
529 (struct usb_descriptor_header *)&ss_epin_fback_desc,
531 (struct usb_descriptor_header *)&std_as_in_if0_desc,
532 (struct usb_descriptor_header *)&std_as_in_if1_desc,
534 (struct usb_descriptor_header *)&as_in_hdr_desc,
535 (struct usb_descriptor_header *)&as_in_fmt1_desc,
536 (struct usb_descriptor_header *)&ss_epin_desc,
537 (struct usb_descriptor_header *)&ss_epin_desc_comp,
538 (struct usb_descriptor_header *)&as_iso_in_desc,
542 struct cntrl_cur_lay3 {
546 struct cntrl_range_lay3 {
547 __le16 wNumSubRanges;
553 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
554 struct usb_endpoint_descriptor *ep_desc,
555 enum usb_device_speed speed, bool is_playback)
557 int chmask, srate, ssize;
558 u16 max_size_bw, max_size_ep;
568 case USB_SPEED_SUPER:
578 chmask = uac2_opts->p_chmask;
579 srate = uac2_opts->p_srate;
580 ssize = uac2_opts->p_ssize;
582 chmask = uac2_opts->c_chmask;
583 srate = uac2_opts->c_srate;
584 ssize = uac2_opts->c_ssize;
587 if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC))
588 srate = srate * (1000 + uac2_opts->fb_max) / 1000;
590 max_size_bw = num_channels(chmask) * ssize *
591 DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
592 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
598 /* Use macro to overcome line length limitation */
599 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
601 static void setup_headers(struct f_uac2_opts *opts,
602 struct usb_descriptor_header **headers,
603 enum usb_device_speed speed)
605 struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
606 struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
607 struct usb_endpoint_descriptor *epout_desc;
608 struct usb_endpoint_descriptor *epin_desc;
609 struct usb_endpoint_descriptor *epin_fback_desc;
614 epout_desc = &fs_epout_desc;
615 epin_desc = &fs_epin_desc;
616 epin_fback_desc = &fs_epin_fback_desc;
619 epout_desc = &hs_epout_desc;
620 epin_desc = &hs_epin_desc;
621 epin_fback_desc = &hs_epin_fback_desc;
624 epout_desc = &ss_epout_desc;
625 epin_desc = &ss_epin_desc;
626 epout_desc_comp = &ss_epout_desc_comp;
627 epin_desc_comp = &ss_epin_desc_comp;
628 epin_fback_desc = &ss_epin_fback_desc;
632 headers[i++] = USBDHDR(&iad_desc);
633 headers[i++] = USBDHDR(&std_ac_if_desc);
634 headers[i++] = USBDHDR(&ac_hdr_desc);
636 headers[i++] = USBDHDR(&in_clk_src_desc);
637 if (EPOUT_EN(opts)) {
638 headers[i++] = USBDHDR(&out_clk_src_desc);
639 headers[i++] = USBDHDR(&usb_out_it_desc);
642 headers[i++] = USBDHDR(&io_in_it_desc);
643 headers[i++] = USBDHDR(&usb_in_ot_desc);
645 if (EPOUT_EN(opts)) {
646 headers[i++] = USBDHDR(&io_out_ot_desc);
647 headers[i++] = USBDHDR(&std_as_out_if0_desc);
648 headers[i++] = USBDHDR(&std_as_out_if1_desc);
649 headers[i++] = USBDHDR(&as_out_hdr_desc);
650 headers[i++] = USBDHDR(&as_out_fmt1_desc);
651 headers[i++] = USBDHDR(epout_desc);
653 headers[i++] = USBDHDR(epout_desc_comp);
655 headers[i++] = USBDHDR(&as_iso_out_desc);
657 if (EPOUT_FBACK_IN_EN(opts))
658 headers[i++] = USBDHDR(epin_fback_desc);
661 headers[i++] = USBDHDR(&std_as_in_if0_desc);
662 headers[i++] = USBDHDR(&std_as_in_if1_desc);
663 headers[i++] = USBDHDR(&as_in_hdr_desc);
664 headers[i++] = USBDHDR(&as_in_fmt1_desc);
665 headers[i++] = USBDHDR(epin_desc);
667 headers[i++] = USBDHDR(epin_desc_comp);
669 headers[i++] = USBDHDR(&as_iso_in_desc);
674 static void setup_descriptor(struct f_uac2_opts *opts)
676 /* patch descriptors */
677 int i = 1; /* ID's start with 1 */
680 usb_out_it_desc.bTerminalID = i++;
682 io_in_it_desc.bTerminalID = i++;
684 io_out_ot_desc.bTerminalID = i++;
686 usb_in_ot_desc.bTerminalID = i++;
688 out_clk_src_desc.bClockID = i++;
690 in_clk_src_desc.bClockID = i++;
692 usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
693 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
694 usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
695 io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
696 io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
697 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
698 as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
699 as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
701 iad_desc.bInterfaceCount = 1;
702 ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
705 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
707 len += sizeof(in_clk_src_desc);
708 len += sizeof(usb_in_ot_desc);
709 len += sizeof(io_in_it_desc);
710 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
711 iad_desc.bInterfaceCount++;
713 if (EPOUT_EN(opts)) {
714 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
716 len += sizeof(out_clk_src_desc);
717 len += sizeof(usb_out_it_desc);
718 len += sizeof(io_out_ot_desc);
719 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
720 iad_desc.bInterfaceCount++;
723 setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
724 setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
725 setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
728 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
730 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
732 if (!opts->p_chmask && !opts->c_chmask) {
733 dev_err(dev, "Error: no playback and capture channels\n");
735 } else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) {
736 dev_err(dev, "Error: unsupported playback channels mask\n");
738 } else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) {
739 dev_err(dev, "Error: unsupported capture channels mask\n");
741 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
742 dev_err(dev, "Error: incorrect playback sample size\n");
744 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
745 dev_err(dev, "Error: incorrect capture sample size\n");
747 } else if (!opts->p_srate) {
748 dev_err(dev, "Error: incorrect playback sampling rate\n");
750 } else if (!opts->c_srate) {
751 dev_err(dev, "Error: incorrect capture sampling rate\n");
759 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
761 struct f_uac2 *uac2 = func_to_uac2(fn);
762 struct g_audio *agdev = func_to_g_audio(fn);
763 struct usb_composite_dev *cdev = cfg->cdev;
764 struct usb_gadget *gadget = cdev->gadget;
765 struct device *dev = &gadget->dev;
766 struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
767 struct usb_string *us;
770 ret = afunc_validate_opts(agdev, dev);
774 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
777 iad_desc.iFunction = us[STR_ASSOC].id;
778 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
779 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
780 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
781 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
782 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
783 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
784 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
785 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
786 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
787 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
788 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
791 /* Initialize the configurable parameters */
792 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
793 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
794 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
795 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
796 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
797 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
798 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
799 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
800 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
801 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
802 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
803 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
805 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
806 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
808 ret = usb_interface_id(cfg, fn);
810 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
813 iad_desc.bFirstInterface = ret;
815 std_ac_if_desc.bInterfaceNumber = ret;
819 if (EPOUT_EN(uac2_opts)) {
820 ret = usb_interface_id(cfg, fn);
822 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
825 std_as_out_if0_desc.bInterfaceNumber = ret;
826 std_as_out_if1_desc.bInterfaceNumber = ret;
827 uac2->as_out_intf = ret;
828 uac2->as_out_alt = 0;
830 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
831 fs_epout_desc.bmAttributes =
832 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
833 hs_epout_desc.bmAttributes =
834 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
835 ss_epout_desc.bmAttributes =
836 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
837 std_as_out_if1_desc.bNumEndpoints++;
839 fs_epout_desc.bmAttributes =
840 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
841 hs_epout_desc.bmAttributes =
842 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
843 ss_epout_desc.bmAttributes =
844 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
848 if (EPIN_EN(uac2_opts)) {
849 ret = usb_interface_id(cfg, fn);
851 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
854 std_as_in_if0_desc.bInterfaceNumber = ret;
855 std_as_in_if1_desc.bInterfaceNumber = ret;
856 uac2->as_in_intf = ret;
860 /* Calculate wMaxPacketSize according to audio bandwidth */
861 ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
864 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
868 ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
871 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
875 ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
878 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
882 ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
885 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
889 ret = set_ep_max_packet_size(uac2_opts, &ss_epin_desc, USB_SPEED_SUPER,
892 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
896 ret = set_ep_max_packet_size(uac2_opts, &ss_epout_desc, USB_SPEED_SUPER,
899 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
903 if (EPOUT_EN(uac2_opts)) {
904 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
905 if (!agdev->out_ep) {
906 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
909 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
910 agdev->in_ep_fback = usb_ep_autoconfig(gadget,
911 &fs_epin_fback_desc);
912 if (!agdev->in_ep_fback) {
913 dev_err(dev, "%s:%d Error!\n",
920 if (EPIN_EN(uac2_opts)) {
921 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
923 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
928 agdev->in_ep_maxpsize = max_t(u16,
929 le16_to_cpu(fs_epin_desc.wMaxPacketSize),
930 le16_to_cpu(hs_epin_desc.wMaxPacketSize));
931 agdev->out_ep_maxpsize = max_t(u16,
932 le16_to_cpu(fs_epout_desc.wMaxPacketSize),
933 le16_to_cpu(hs_epout_desc.wMaxPacketSize));
935 agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
936 le16_to_cpu(ss_epin_desc.wMaxPacketSize));
937 agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
938 le16_to_cpu(ss_epout_desc.wMaxPacketSize));
940 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
941 hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
942 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
943 ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
944 ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
945 ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
947 setup_descriptor(uac2_opts);
949 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
954 agdev->gadget = gadget;
956 agdev->params.p_chmask = uac2_opts->p_chmask;
957 agdev->params.p_srate = uac2_opts->p_srate;
958 agdev->params.p_ssize = uac2_opts->p_ssize;
959 agdev->params.c_chmask = uac2_opts->c_chmask;
960 agdev->params.c_srate = uac2_opts->c_srate;
961 agdev->params.c_ssize = uac2_opts->c_ssize;
962 agdev->params.req_number = uac2_opts->req_number;
963 agdev->params.fb_max = uac2_opts->fb_max;
964 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
970 usb_free_all_descriptors(fn);
971 agdev->gadget = NULL;
976 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
978 struct usb_composite_dev *cdev = fn->config->cdev;
979 struct f_uac2 *uac2 = func_to_uac2(fn);
980 struct usb_gadget *gadget = cdev->gadget;
981 struct device *dev = &gadget->dev;
984 /* No i/f has more than 2 alt settings */
986 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
990 if (intf == uac2->ac_intf) {
991 /* Control I/f has only 1 AltSetting - 0 */
993 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
999 if (intf == uac2->as_out_intf) {
1000 uac2->as_out_alt = alt;
1003 ret = u_audio_start_capture(&uac2->g_audio);
1005 u_audio_stop_capture(&uac2->g_audio);
1006 } else if (intf == uac2->as_in_intf) {
1007 uac2->as_in_alt = alt;
1010 ret = u_audio_start_playback(&uac2->g_audio);
1012 u_audio_stop_playback(&uac2->g_audio);
1014 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1022 afunc_get_alt(struct usb_function *fn, unsigned intf)
1024 struct f_uac2 *uac2 = func_to_uac2(fn);
1025 struct g_audio *agdev = func_to_g_audio(fn);
1027 if (intf == uac2->ac_intf)
1028 return uac2->ac_alt;
1029 else if (intf == uac2->as_out_intf)
1030 return uac2->as_out_alt;
1031 else if (intf == uac2->as_in_intf)
1032 return uac2->as_in_alt;
1034 dev_err(&agdev->gadget->dev,
1035 "%s:%d Invalid Interface %d!\n",
1036 __func__, __LINE__, intf);
1042 afunc_disable(struct usb_function *fn)
1044 struct f_uac2 *uac2 = func_to_uac2(fn);
1046 uac2->as_in_alt = 0;
1047 uac2->as_out_alt = 0;
1048 u_audio_stop_capture(&uac2->g_audio);
1049 u_audio_stop_playback(&uac2->g_audio);
1053 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1055 struct usb_request *req = fn->config->cdev->req;
1056 struct g_audio *agdev = func_to_g_audio(fn);
1057 struct f_uac2_opts *opts;
1058 u16 w_length = le16_to_cpu(cr->wLength);
1059 u16 w_index = le16_to_cpu(cr->wIndex);
1060 u16 w_value = le16_to_cpu(cr->wValue);
1061 u8 entity_id = (w_index >> 8) & 0xff;
1062 u8 control_selector = w_value >> 8;
1063 int value = -EOPNOTSUPP;
1064 int p_srate, c_srate;
1066 opts = g_audio_to_uac2_opts(agdev);
1067 p_srate = opts->p_srate;
1068 c_srate = opts->c_srate;
1070 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1071 struct cntrl_cur_lay3 c;
1072 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1074 if (entity_id == USB_IN_CLK_ID)
1075 c.dCUR = cpu_to_le32(p_srate);
1076 else if (entity_id == USB_OUT_CLK_ID)
1077 c.dCUR = cpu_to_le32(c_srate);
1079 value = min_t(unsigned, w_length, sizeof c);
1080 memcpy(req->buf, &c, value);
1081 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1082 *(u8 *)req->buf = 1;
1083 value = min_t(unsigned, w_length, 1);
1085 dev_err(&agdev->gadget->dev,
1086 "%s:%d control_selector=%d TODO!\n",
1087 __func__, __LINE__, control_selector);
1094 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1096 struct usb_request *req = fn->config->cdev->req;
1097 struct g_audio *agdev = func_to_g_audio(fn);
1098 struct f_uac2_opts *opts;
1099 u16 w_length = le16_to_cpu(cr->wLength);
1100 u16 w_index = le16_to_cpu(cr->wIndex);
1101 u16 w_value = le16_to_cpu(cr->wValue);
1102 u8 entity_id = (w_index >> 8) & 0xff;
1103 u8 control_selector = w_value >> 8;
1104 struct cntrl_range_lay3 r;
1105 int value = -EOPNOTSUPP;
1106 int p_srate, c_srate;
1108 opts = g_audio_to_uac2_opts(agdev);
1109 p_srate = opts->p_srate;
1110 c_srate = opts->c_srate;
1112 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1113 if (entity_id == USB_IN_CLK_ID)
1114 r.dMIN = cpu_to_le32(p_srate);
1115 else if (entity_id == USB_OUT_CLK_ID)
1116 r.dMIN = cpu_to_le32(c_srate);
1122 r.wNumSubRanges = cpu_to_le16(1);
1124 value = min_t(unsigned, w_length, sizeof r);
1125 memcpy(req->buf, &r, value);
1127 dev_err(&agdev->gadget->dev,
1128 "%s:%d control_selector=%d TODO!\n",
1129 __func__, __LINE__, control_selector);
1136 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1138 if (cr->bRequest == UAC2_CS_CUR)
1139 return in_rq_cur(fn, cr);
1140 else if (cr->bRequest == UAC2_CS_RANGE)
1141 return in_rq_range(fn, cr);
1147 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1149 u16 w_length = le16_to_cpu(cr->wLength);
1150 u16 w_value = le16_to_cpu(cr->wValue);
1151 u8 control_selector = w_value >> 8;
1153 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1160 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1162 struct f_uac2 *uac2 = func_to_uac2(fn);
1163 struct g_audio *agdev = func_to_g_audio(fn);
1164 u16 w_index = le16_to_cpu(cr->wIndex);
1165 u8 intf = w_index & 0xff;
1167 if (intf != uac2->ac_intf) {
1168 dev_err(&agdev->gadget->dev,
1169 "%s:%d Error!\n", __func__, __LINE__);
1173 if (cr->bRequestType & USB_DIR_IN)
1174 return ac_rq_in(fn, cr);
1175 else if (cr->bRequest == UAC2_CS_CUR)
1176 return out_rq_cur(fn, cr);
1182 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1184 struct usb_composite_dev *cdev = fn->config->cdev;
1185 struct g_audio *agdev = func_to_g_audio(fn);
1186 struct usb_request *req = cdev->req;
1187 u16 w_length = le16_to_cpu(cr->wLength);
1188 int value = -EOPNOTSUPP;
1190 /* Only Class specific requests are supposed to reach here */
1191 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1194 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1195 value = setup_rq_inf(fn, cr);
1197 dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1198 __func__, __LINE__);
1201 req->length = value;
1202 req->zero = value < w_length;
1203 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1205 dev_err(&agdev->gadget->dev,
1206 "%s:%d Error!\n", __func__, __LINE__);
1214 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1216 return container_of(to_config_group(item), struct f_uac2_opts,
1220 static void f_uac2_attr_release(struct config_item *item)
1222 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1224 usb_put_function_instance(&opts->func_inst);
1227 static struct configfs_item_operations f_uac2_item_ops = {
1228 .release = f_uac2_attr_release,
1231 #define UAC2_ATTRIBUTE(name) \
1232 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1235 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1238 mutex_lock(&opts->lock); \
1239 result = sprintf(page, "%u\n", opts->name); \
1240 mutex_unlock(&opts->lock); \
1245 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1246 const char *page, size_t len) \
1248 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1252 mutex_lock(&opts->lock); \
1253 if (opts->refcnt) { \
1258 ret = kstrtou32(page, 0, &num); \
1266 mutex_unlock(&opts->lock); \
1270 CONFIGFS_ATTR(f_uac2_opts_, name)
1272 #define UAC2_ATTRIBUTE_SYNC(name) \
1273 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1276 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1280 mutex_lock(&opts->lock); \
1281 switch (opts->name) { \
1282 case USB_ENDPOINT_SYNC_ASYNC: \
1285 case USB_ENDPOINT_SYNC_ADAPTIVE: \
1292 result = sprintf(page, "%s\n", str); \
1293 mutex_unlock(&opts->lock); \
1298 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1299 const char *page, size_t len) \
1301 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1304 mutex_lock(&opts->lock); \
1305 if (opts->refcnt) { \
1310 if (!strncmp(page, "async", 5)) \
1311 opts->name = USB_ENDPOINT_SYNC_ASYNC; \
1312 else if (!strncmp(page, "adaptive", 8)) \
1313 opts->name = USB_ENDPOINT_SYNC_ADAPTIVE; \
1322 mutex_unlock(&opts->lock); \
1326 CONFIGFS_ATTR(f_uac2_opts_, name)
1328 UAC2_ATTRIBUTE(p_chmask);
1329 UAC2_ATTRIBUTE(p_srate);
1330 UAC2_ATTRIBUTE(p_ssize);
1331 UAC2_ATTRIBUTE(c_chmask);
1332 UAC2_ATTRIBUTE(c_srate);
1333 UAC2_ATTRIBUTE_SYNC(c_sync);
1334 UAC2_ATTRIBUTE(c_ssize);
1335 UAC2_ATTRIBUTE(req_number);
1336 UAC2_ATTRIBUTE(fb_max);
1338 static struct configfs_attribute *f_uac2_attrs[] = {
1339 &f_uac2_opts_attr_p_chmask,
1340 &f_uac2_opts_attr_p_srate,
1341 &f_uac2_opts_attr_p_ssize,
1342 &f_uac2_opts_attr_c_chmask,
1343 &f_uac2_opts_attr_c_srate,
1344 &f_uac2_opts_attr_c_ssize,
1345 &f_uac2_opts_attr_c_sync,
1346 &f_uac2_opts_attr_req_number,
1347 &f_uac2_opts_attr_fb_max,
1351 static const struct config_item_type f_uac2_func_type = {
1352 .ct_item_ops = &f_uac2_item_ops,
1353 .ct_attrs = f_uac2_attrs,
1354 .ct_owner = THIS_MODULE,
1357 static void afunc_free_inst(struct usb_function_instance *f)
1359 struct f_uac2_opts *opts;
1361 opts = container_of(f, struct f_uac2_opts, func_inst);
1365 static struct usb_function_instance *afunc_alloc_inst(void)
1367 struct f_uac2_opts *opts;
1369 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1371 return ERR_PTR(-ENOMEM);
1373 mutex_init(&opts->lock);
1374 opts->func_inst.free_func_inst = afunc_free_inst;
1376 config_group_init_type_name(&opts->func_inst.group, "",
1379 opts->p_chmask = UAC2_DEF_PCHMASK;
1380 opts->p_srate = UAC2_DEF_PSRATE;
1381 opts->p_ssize = UAC2_DEF_PSSIZE;
1382 opts->c_chmask = UAC2_DEF_CCHMASK;
1383 opts->c_srate = UAC2_DEF_CSRATE;
1384 opts->c_ssize = UAC2_DEF_CSSIZE;
1385 opts->c_sync = UAC2_DEF_CSYNC;
1386 opts->req_number = UAC2_DEF_REQ_NUM;
1387 opts->fb_max = UAC2_DEF_FB_MAX;
1388 return &opts->func_inst;
1391 static void afunc_free(struct usb_function *f)
1393 struct g_audio *agdev;
1394 struct f_uac2_opts *opts;
1396 agdev = func_to_g_audio(f);
1397 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1399 mutex_lock(&opts->lock);
1401 mutex_unlock(&opts->lock);
1404 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1406 struct g_audio *agdev = func_to_g_audio(f);
1408 g_audio_cleanup(agdev);
1409 usb_free_all_descriptors(f);
1411 agdev->gadget = NULL;
1414 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1416 struct f_uac2 *uac2;
1417 struct f_uac2_opts *opts;
1419 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
1421 return ERR_PTR(-ENOMEM);
1423 opts = container_of(fi, struct f_uac2_opts, func_inst);
1424 mutex_lock(&opts->lock);
1426 mutex_unlock(&opts->lock);
1428 uac2->g_audio.func.name = "uac2_func";
1429 uac2->g_audio.func.bind = afunc_bind;
1430 uac2->g_audio.func.unbind = afunc_unbind;
1431 uac2->g_audio.func.set_alt = afunc_set_alt;
1432 uac2->g_audio.func.get_alt = afunc_get_alt;
1433 uac2->g_audio.func.disable = afunc_disable;
1434 uac2->g_audio.func.setup = afunc_setup;
1435 uac2->g_audio.func.free_func = afunc_free;
1437 return &uac2->g_audio.func;
1440 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1441 MODULE_LICENSE("GPL");
1442 MODULE_AUTHOR("Yadwinder Singh");
1443 MODULE_AUTHOR("Jaswinder Singh");