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 * Ruslan Bilovol (ruslan.bilovol@gmail.com)
13 #include <linux/usb/audio.h>
14 #include <linux/usb/audio-v2.h>
15 #include <linux/module.h>
21 /* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */
22 #define UAC2_CHANNEL_MASK 0x07FFFFFF
25 * The driver implements a simple UAC_2 topology.
26 * USB-OUT -> IT_1 -> FU -> OT_3 -> ALSA_Capture
27 * ALSA_Playback -> IT_2 -> FU -> OT_4 -> USB-IN
28 * Capture and Playback sampling rates are independently
29 * controlled by two clock sources :
30 * CLK_5 := c_srate, and CLK_6 := p_srate
32 #define USB_OUT_CLK_ID (out_clk_src_desc.bClockID)
33 #define USB_IN_CLK_ID (in_clk_src_desc.bClockID)
34 #define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID)
35 #define USB_IN_FU_ID (in_feature_unit_desc->bUnitID)
37 #define CONTROL_ABSENT 0
38 #define CONTROL_RDONLY 1
39 #define CONTROL_RDWR 3
41 #define CLK_FREQ_CTRL 0
42 #define CLK_VLD_CTRL 2
43 #define FU_MUTE_CTRL 0
53 #define EPIN_EN(_opts) ((_opts)->p_chmask != 0)
54 #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0)
55 #define FUIN_EN(_opts) (EPIN_EN(_opts) \
56 && ((_opts)->p_mute_present \
57 || (_opts)->p_volume_present))
58 #define FUOUT_EN(_opts) (EPOUT_EN(_opts) \
59 && ((_opts)->c_mute_present \
60 || (_opts)->c_volume_present))
61 #define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC)
64 struct g_audio g_audio;
65 u8 ac_intf, as_in_intf, as_out_intf;
66 u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
68 struct usb_ctrlrequest setup_cr; /* will be used in data stage */
70 /* Interrupt IN endpoint of AC interface */
71 struct usb_ep *int_ep;
73 /* transient state, only valid during handling of a single control request */
77 static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
79 return container_of(f, struct f_uac2, g_audio.func);
83 struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
85 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
88 static int afunc_notify(struct g_audio *agdev, int unit_id, int cs);
90 /* --------- USB Function Interface ------------- */
109 static struct usb_string strings_fn[] = {
110 /* [STR_ASSOC].s = DYNAMIC, */
111 [STR_IF_CTRL].s = "Topology Control",
112 [STR_CLKSRC_IN].s = "Input Clock",
113 [STR_CLKSRC_OUT].s = "Output Clock",
114 [STR_USB_IT].s = "USBH Out",
115 [STR_IO_IT].s = "USBD Out",
116 [STR_USB_OT].s = "USBH In",
117 [STR_IO_OT].s = "USBD In",
118 [STR_FU_IN].s = "Capture Volume",
119 [STR_FU_OUT].s = "Playback Volume",
120 [STR_AS_OUT_ALT0].s = "Playback Inactive",
121 [STR_AS_OUT_ALT1].s = "Playback Active",
122 [STR_AS_IN_ALT0].s = "Capture Inactive",
123 [STR_AS_IN_ALT1].s = "Capture Active",
127 static const char *const speed_names[] = {
128 [USB_SPEED_UNKNOWN] = "UNKNOWN",
129 [USB_SPEED_LOW] = "LS",
130 [USB_SPEED_FULL] = "FS",
131 [USB_SPEED_HIGH] = "HS",
132 [USB_SPEED_WIRELESS] = "W",
133 [USB_SPEED_SUPER] = "SS",
134 [USB_SPEED_SUPER_PLUS] = "SS+",
137 static struct usb_gadget_strings str_fn = {
138 .language = 0x0409, /* en-us */
139 .strings = strings_fn,
142 static struct usb_gadget_strings *fn_strings[] = {
147 static struct usb_interface_assoc_descriptor iad_desc = {
148 .bLength = sizeof iad_desc,
149 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
151 .bFirstInterface = 0,
152 .bInterfaceCount = 3,
153 .bFunctionClass = USB_CLASS_AUDIO,
154 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
155 .bFunctionProtocol = UAC_VERSION_2,
158 /* Audio Control Interface */
159 static struct usb_interface_descriptor std_ac_if_desc = {
160 .bLength = sizeof std_ac_if_desc,
161 .bDescriptorType = USB_DT_INTERFACE,
163 .bAlternateSetting = 0,
164 /* .bNumEndpoints = DYNAMIC */
165 .bInterfaceClass = USB_CLASS_AUDIO,
166 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
167 .bInterfaceProtocol = UAC_VERSION_2,
170 /* Clock source for IN traffic */
171 static struct uac_clock_source_descriptor in_clk_src_desc = {
172 .bLength = sizeof in_clk_src_desc,
173 .bDescriptorType = USB_DT_CS_INTERFACE,
175 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
176 /* .bClockID = DYNAMIC */
177 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
178 .bmControls = (CONTROL_RDWR << CLK_FREQ_CTRL),
182 /* Clock source for OUT traffic */
183 static struct uac_clock_source_descriptor out_clk_src_desc = {
184 .bLength = sizeof out_clk_src_desc,
185 .bDescriptorType = USB_DT_CS_INTERFACE,
187 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
188 /* .bClockID = DYNAMIC */
189 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
190 .bmControls = (CONTROL_RDWR << CLK_FREQ_CTRL),
194 /* Input Terminal for USB_OUT */
195 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
196 .bLength = sizeof usb_out_it_desc,
197 .bDescriptorType = USB_DT_CS_INTERFACE,
199 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
200 /* .bTerminalID = DYNAMIC */
201 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
203 /* .bCSourceID = DYNAMIC */
205 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
208 /* Input Terminal for I/O-In */
209 static struct uac2_input_terminal_descriptor io_in_it_desc = {
210 .bLength = sizeof io_in_it_desc,
211 .bDescriptorType = USB_DT_CS_INTERFACE,
213 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
214 /* .bTerminalID = DYNAMIC */
215 /* .wTerminalType = DYNAMIC */
217 /* .bCSourceID = DYNAMIC */
219 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
222 /* Ouput Terminal for USB_IN */
223 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
224 .bLength = sizeof usb_in_ot_desc,
225 .bDescriptorType = USB_DT_CS_INTERFACE,
227 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
228 /* .bTerminalID = DYNAMIC */
229 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
231 /* .bSourceID = DYNAMIC */
232 /* .bCSourceID = DYNAMIC */
233 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
236 /* Ouput Terminal for I/O-Out */
237 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
238 .bLength = sizeof io_out_ot_desc,
239 .bDescriptorType = USB_DT_CS_INTERFACE,
241 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
242 /* .bTerminalID = DYNAMIC */
243 /* .wTerminalType = DYNAMIC */
245 /* .bSourceID = DYNAMIC */
246 /* .bCSourceID = DYNAMIC */
247 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
250 static struct uac2_feature_unit_descriptor *in_feature_unit_desc;
251 static struct uac2_feature_unit_descriptor *out_feature_unit_desc;
253 static struct uac2_ac_header_descriptor ac_hdr_desc = {
254 .bLength = sizeof ac_hdr_desc,
255 .bDescriptorType = USB_DT_CS_INTERFACE,
257 .bDescriptorSubtype = UAC_MS_HEADER,
258 .bcdADC = cpu_to_le16(0x200),
259 .bCategory = UAC2_FUNCTION_IO_BOX,
260 /* .wTotalLength = DYNAMIC */
264 /* AC IN Interrupt Endpoint */
265 static struct usb_endpoint_descriptor fs_ep_int_desc = {
266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
269 .bEndpointAddress = USB_DIR_IN,
270 .bmAttributes = USB_ENDPOINT_XFER_INT,
271 .wMaxPacketSize = cpu_to_le16(6),
275 static struct usb_endpoint_descriptor hs_ep_int_desc = {
276 .bLength = USB_DT_ENDPOINT_SIZE,
277 .bDescriptorType = USB_DT_ENDPOINT,
279 .bmAttributes = USB_ENDPOINT_XFER_INT,
280 .wMaxPacketSize = cpu_to_le16(6),
284 static struct usb_endpoint_descriptor ss_ep_int_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
288 .bEndpointAddress = USB_DIR_IN,
289 .bmAttributes = USB_ENDPOINT_XFER_INT,
290 .wMaxPacketSize = cpu_to_le16(6),
294 static struct usb_ss_ep_comp_descriptor ss_ep_int_desc_comp = {
295 .bLength = sizeof(ss_ep_int_desc_comp),
296 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
297 .wBytesPerInterval = cpu_to_le16(6),
300 /* Audio Streaming OUT Interface - Alt0 */
301 static struct usb_interface_descriptor std_as_out_if0_desc = {
302 .bLength = sizeof std_as_out_if0_desc,
303 .bDescriptorType = USB_DT_INTERFACE,
305 .bAlternateSetting = 0,
307 .bInterfaceClass = USB_CLASS_AUDIO,
308 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
309 .bInterfaceProtocol = UAC_VERSION_2,
312 /* Audio Streaming OUT Interface - Alt1 */
313 static struct usb_interface_descriptor std_as_out_if1_desc = {
314 .bLength = sizeof std_as_out_if1_desc,
315 .bDescriptorType = USB_DT_INTERFACE,
317 .bAlternateSetting = 1,
319 .bInterfaceClass = USB_CLASS_AUDIO,
320 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
321 .bInterfaceProtocol = UAC_VERSION_2,
324 /* Audio Stream OUT Intface Desc */
325 static struct uac2_as_header_descriptor as_out_hdr_desc = {
326 .bLength = sizeof as_out_hdr_desc,
327 .bDescriptorType = USB_DT_CS_INTERFACE,
329 .bDescriptorSubtype = UAC_AS_GENERAL,
330 /* .bTerminalLink = DYNAMIC */
332 .bFormatType = UAC_FORMAT_TYPE_I,
333 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
337 /* Audio USB_OUT Format */
338 static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
339 .bLength = sizeof as_out_fmt1_desc,
340 .bDescriptorType = USB_DT_CS_INTERFACE,
341 .bDescriptorSubtype = UAC_FORMAT_TYPE,
342 .bFormatType = UAC_FORMAT_TYPE_I,
345 /* STD AS ISO OUT Endpoint */
346 static struct usb_endpoint_descriptor fs_epout_desc = {
347 .bLength = USB_DT_ENDPOINT_SIZE,
348 .bDescriptorType = USB_DT_ENDPOINT,
350 .bEndpointAddress = USB_DIR_OUT,
351 /* .bmAttributes = DYNAMIC */
352 /* .wMaxPacketSize = DYNAMIC */
356 static struct usb_endpoint_descriptor hs_epout_desc = {
357 .bLength = USB_DT_ENDPOINT_SIZE,
358 .bDescriptorType = USB_DT_ENDPOINT,
360 /* .bmAttributes = DYNAMIC */
361 /* .wMaxPacketSize = DYNAMIC */
362 /* .bInterval = DYNAMIC */
365 static struct usb_endpoint_descriptor ss_epout_desc = {
366 .bLength = USB_DT_ENDPOINT_SIZE,
367 .bDescriptorType = USB_DT_ENDPOINT,
369 .bEndpointAddress = USB_DIR_OUT,
370 /* .bmAttributes = DYNAMIC */
371 /* .wMaxPacketSize = DYNAMIC */
372 /* .bInterval = DYNAMIC */
375 static struct usb_ss_ep_comp_descriptor ss_epout_desc_comp = {
376 .bLength = sizeof(ss_epout_desc_comp),
377 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
380 /* wBytesPerInterval = DYNAMIC */
383 /* CS AS ISO OUT Endpoint */
384 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
385 .bLength = sizeof as_iso_out_desc,
386 .bDescriptorType = USB_DT_CS_ENDPOINT,
388 .bDescriptorSubtype = UAC_EP_GENERAL,
391 .bLockDelayUnits = 0,
395 /* STD AS ISO IN Feedback Endpoint */
396 static struct usb_endpoint_descriptor fs_epin_fback_desc = {
397 .bLength = USB_DT_ENDPOINT_SIZE,
398 .bDescriptorType = USB_DT_ENDPOINT,
400 .bEndpointAddress = USB_DIR_IN,
401 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
402 .wMaxPacketSize = cpu_to_le16(3),
406 static struct usb_endpoint_descriptor hs_epin_fback_desc = {
407 .bLength = USB_DT_ENDPOINT_SIZE,
408 .bDescriptorType = USB_DT_ENDPOINT,
410 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
411 .wMaxPacketSize = cpu_to_le16(4),
415 static struct usb_endpoint_descriptor ss_epin_fback_desc = {
416 .bLength = USB_DT_ENDPOINT_SIZE,
417 .bDescriptorType = USB_DT_ENDPOINT,
419 .bEndpointAddress = USB_DIR_IN,
420 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK,
421 .wMaxPacketSize = cpu_to_le16(4),
425 static struct usb_ss_ep_comp_descriptor ss_epin_fback_desc_comp = {
426 .bLength = sizeof(ss_epin_fback_desc_comp),
427 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
430 .wBytesPerInterval = cpu_to_le16(4),
434 /* Audio Streaming IN Interface - Alt0 */
435 static struct usb_interface_descriptor std_as_in_if0_desc = {
436 .bLength = sizeof std_as_in_if0_desc,
437 .bDescriptorType = USB_DT_INTERFACE,
439 .bAlternateSetting = 0,
441 .bInterfaceClass = USB_CLASS_AUDIO,
442 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
443 .bInterfaceProtocol = UAC_VERSION_2,
446 /* Audio Streaming IN Interface - Alt1 */
447 static struct usb_interface_descriptor std_as_in_if1_desc = {
448 .bLength = sizeof std_as_in_if1_desc,
449 .bDescriptorType = USB_DT_INTERFACE,
451 .bAlternateSetting = 1,
453 .bInterfaceClass = USB_CLASS_AUDIO,
454 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
455 .bInterfaceProtocol = UAC_VERSION_2,
458 /* Audio Stream IN Intface Desc */
459 static struct uac2_as_header_descriptor as_in_hdr_desc = {
460 .bLength = sizeof as_in_hdr_desc,
461 .bDescriptorType = USB_DT_CS_INTERFACE,
463 .bDescriptorSubtype = UAC_AS_GENERAL,
464 /* .bTerminalLink = DYNAMIC */
466 .bFormatType = UAC_FORMAT_TYPE_I,
467 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
471 /* Audio USB_IN Format */
472 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
473 .bLength = sizeof as_in_fmt1_desc,
474 .bDescriptorType = USB_DT_CS_INTERFACE,
475 .bDescriptorSubtype = UAC_FORMAT_TYPE,
476 .bFormatType = UAC_FORMAT_TYPE_I,
479 /* STD AS ISO IN Endpoint */
480 static struct usb_endpoint_descriptor fs_epin_desc = {
481 .bLength = USB_DT_ENDPOINT_SIZE,
482 .bDescriptorType = USB_DT_ENDPOINT,
484 .bEndpointAddress = USB_DIR_IN,
485 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
486 /* .wMaxPacketSize = DYNAMIC */
490 static struct usb_endpoint_descriptor hs_epin_desc = {
491 .bLength = USB_DT_ENDPOINT_SIZE,
492 .bDescriptorType = USB_DT_ENDPOINT,
494 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
495 /* .wMaxPacketSize = DYNAMIC */
496 /* .bInterval = DYNAMIC */
499 static struct usb_endpoint_descriptor ss_epin_desc = {
500 .bLength = USB_DT_ENDPOINT_SIZE,
501 .bDescriptorType = USB_DT_ENDPOINT,
503 .bEndpointAddress = USB_DIR_IN,
504 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
505 /* .wMaxPacketSize = DYNAMIC */
506 /* .bInterval = DYNAMIC */
509 static struct usb_ss_ep_comp_descriptor ss_epin_desc_comp = {
510 .bLength = sizeof(ss_epin_desc_comp),
511 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
514 /* wBytesPerInterval = DYNAMIC */
517 /* CS AS ISO IN Endpoint */
518 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
519 .bLength = sizeof as_iso_in_desc,
520 .bDescriptorType = USB_DT_CS_ENDPOINT,
522 .bDescriptorSubtype = UAC_EP_GENERAL,
525 .bLockDelayUnits = 0,
529 static struct usb_descriptor_header *fs_audio_desc[] = {
530 (struct usb_descriptor_header *)&iad_desc,
531 (struct usb_descriptor_header *)&std_ac_if_desc,
533 (struct usb_descriptor_header *)&ac_hdr_desc,
534 (struct usb_descriptor_header *)&in_clk_src_desc,
535 (struct usb_descriptor_header *)&out_clk_src_desc,
536 (struct usb_descriptor_header *)&usb_out_it_desc,
537 (struct usb_descriptor_header *)&out_feature_unit_desc,
538 (struct usb_descriptor_header *)&io_in_it_desc,
539 (struct usb_descriptor_header *)&usb_in_ot_desc,
540 (struct usb_descriptor_header *)&in_feature_unit_desc,
541 (struct usb_descriptor_header *)&io_out_ot_desc,
543 (struct usb_descriptor_header *)&fs_ep_int_desc,
545 (struct usb_descriptor_header *)&std_as_out_if0_desc,
546 (struct usb_descriptor_header *)&std_as_out_if1_desc,
548 (struct usb_descriptor_header *)&as_out_hdr_desc,
549 (struct usb_descriptor_header *)&as_out_fmt1_desc,
550 (struct usb_descriptor_header *)&fs_epout_desc,
551 (struct usb_descriptor_header *)&as_iso_out_desc,
552 (struct usb_descriptor_header *)&fs_epin_fback_desc,
554 (struct usb_descriptor_header *)&std_as_in_if0_desc,
555 (struct usb_descriptor_header *)&std_as_in_if1_desc,
557 (struct usb_descriptor_header *)&as_in_hdr_desc,
558 (struct usb_descriptor_header *)&as_in_fmt1_desc,
559 (struct usb_descriptor_header *)&fs_epin_desc,
560 (struct usb_descriptor_header *)&as_iso_in_desc,
564 static struct usb_descriptor_header *hs_audio_desc[] = {
565 (struct usb_descriptor_header *)&iad_desc,
566 (struct usb_descriptor_header *)&std_ac_if_desc,
568 (struct usb_descriptor_header *)&ac_hdr_desc,
569 (struct usb_descriptor_header *)&in_clk_src_desc,
570 (struct usb_descriptor_header *)&out_clk_src_desc,
571 (struct usb_descriptor_header *)&usb_out_it_desc,
572 (struct usb_descriptor_header *)&out_feature_unit_desc,
573 (struct usb_descriptor_header *)&io_in_it_desc,
574 (struct usb_descriptor_header *)&usb_in_ot_desc,
575 (struct usb_descriptor_header *)&in_feature_unit_desc,
576 (struct usb_descriptor_header *)&io_out_ot_desc,
578 (struct usb_descriptor_header *)&hs_ep_int_desc,
580 (struct usb_descriptor_header *)&std_as_out_if0_desc,
581 (struct usb_descriptor_header *)&std_as_out_if1_desc,
583 (struct usb_descriptor_header *)&as_out_hdr_desc,
584 (struct usb_descriptor_header *)&as_out_fmt1_desc,
585 (struct usb_descriptor_header *)&hs_epout_desc,
586 (struct usb_descriptor_header *)&as_iso_out_desc,
587 (struct usb_descriptor_header *)&hs_epin_fback_desc,
589 (struct usb_descriptor_header *)&std_as_in_if0_desc,
590 (struct usb_descriptor_header *)&std_as_in_if1_desc,
592 (struct usb_descriptor_header *)&as_in_hdr_desc,
593 (struct usb_descriptor_header *)&as_in_fmt1_desc,
594 (struct usb_descriptor_header *)&hs_epin_desc,
595 (struct usb_descriptor_header *)&as_iso_in_desc,
599 static struct usb_descriptor_header *ss_audio_desc[] = {
600 (struct usb_descriptor_header *)&iad_desc,
601 (struct usb_descriptor_header *)&std_ac_if_desc,
603 (struct usb_descriptor_header *)&ac_hdr_desc,
604 (struct usb_descriptor_header *)&in_clk_src_desc,
605 (struct usb_descriptor_header *)&out_clk_src_desc,
606 (struct usb_descriptor_header *)&usb_out_it_desc,
607 (struct usb_descriptor_header *)&out_feature_unit_desc,
608 (struct usb_descriptor_header *)&io_in_it_desc,
609 (struct usb_descriptor_header *)&usb_in_ot_desc,
610 (struct usb_descriptor_header *)&in_feature_unit_desc,
611 (struct usb_descriptor_header *)&io_out_ot_desc,
613 (struct usb_descriptor_header *)&ss_ep_int_desc,
614 (struct usb_descriptor_header *)&ss_ep_int_desc_comp,
616 (struct usb_descriptor_header *)&std_as_out_if0_desc,
617 (struct usb_descriptor_header *)&std_as_out_if1_desc,
619 (struct usb_descriptor_header *)&as_out_hdr_desc,
620 (struct usb_descriptor_header *)&as_out_fmt1_desc,
621 (struct usb_descriptor_header *)&ss_epout_desc,
622 (struct usb_descriptor_header *)&ss_epout_desc_comp,
623 (struct usb_descriptor_header *)&as_iso_out_desc,
624 (struct usb_descriptor_header *)&ss_epin_fback_desc,
625 (struct usb_descriptor_header *)&ss_epin_fback_desc_comp,
627 (struct usb_descriptor_header *)&std_as_in_if0_desc,
628 (struct usb_descriptor_header *)&std_as_in_if1_desc,
630 (struct usb_descriptor_header *)&as_in_hdr_desc,
631 (struct usb_descriptor_header *)&as_in_fmt1_desc,
632 (struct usb_descriptor_header *)&ss_epin_desc,
633 (struct usb_descriptor_header *)&ss_epin_desc_comp,
634 (struct usb_descriptor_header *)&as_iso_in_desc,
638 struct cntrl_cur_lay2 {
642 struct cntrl_range_lay2 {
643 __le16 wNumSubRanges;
649 struct cntrl_cur_lay3 {
653 struct cntrl_subrange_lay3 {
659 #define ranges_lay3_size(c) (sizeof(c.wNumSubRanges) \
660 + le16_to_cpu(c.wNumSubRanges) \
661 * sizeof(struct cntrl_subrange_lay3))
663 #define DECLARE_UAC2_CNTRL_RANGES_LAY3(k, n) \
664 struct cntrl_ranges_lay3_##k { \
665 __le16 wNumSubRanges; \
666 struct cntrl_subrange_lay3 r[n]; \
669 DECLARE_UAC2_CNTRL_RANGES_LAY3(srates, UAC_MAX_RATES);
671 static int get_max_srate(const int *srates)
673 int i, max_srate = 0;
675 for (i = 0; i < UAC_MAX_RATES; i++) {
678 if (srates[i] > max_srate)
679 max_srate = srates[i];
684 static int get_max_bw_for_bint(const struct f_uac2_opts *uac2_opts,
685 u8 bint, unsigned int factor, bool is_playback)
687 int chmask, srate, ssize;
691 chmask = uac2_opts->p_chmask;
692 srate = get_max_srate(uac2_opts->p_srates);
693 ssize = uac2_opts->p_ssize;
695 chmask = uac2_opts->c_chmask;
696 srate = get_max_srate(uac2_opts->c_srates);
697 ssize = uac2_opts->c_ssize;
700 if (is_playback || (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC)) {
701 // playback is always async, capture only when configured
702 // Win10 requires max packet size + 1 frame
703 srate = srate * (1000 + uac2_opts->fb_max) / 1000;
704 // updated srate is always bigger, therefore DIV_ROUND_UP always yields +1
705 max_size_bw = num_channels(chmask) * ssize *
706 (DIV_ROUND_UP(srate, factor / (1 << (bint - 1))));
708 // adding 1 frame provision for Win10
709 max_size_bw = num_channels(chmask) * ssize *
710 (DIV_ROUND_UP(srate, factor / (1 << (bint - 1))) + 1);
715 static int set_ep_max_packet_size_bint(struct device *dev, const struct f_uac2_opts *uac2_opts,
716 struct usb_endpoint_descriptor *ep_desc,
717 enum usb_device_speed speed, bool is_playback)
719 u16 max_size_bw, max_size_ep;
727 bint = ep_desc->bInterval;
728 max_size_bw = get_max_bw_for_bint(uac2_opts, bint, 1000, is_playback);
732 case USB_SPEED_SUPER:
735 opts_bint = uac2_opts->p_hs_bint;
737 opts_bint = uac2_opts->c_hs_bint;
742 max_size_bw = get_max_bw_for_bint(uac2_opts, bint, 8000, is_playback);
744 /* checking bInterval from 4 to 1 whether the required bandwidth fits */
745 for (bint = 4; bint > 0; --bint) {
746 max_size_bw = get_max_bw_for_bint(
747 uac2_opts, bint, 8000, is_playback);
748 if (max_size_bw <= max_size_ep)
763 if (max_size_bw <= max_size_ep)
765 "%s %s: Would use wMaxPacketSize %d and bInterval %d\n",
766 speed_names[speed], dir, max_size_bw, bint);
769 "%s %s: Req. wMaxPacketSize %d at bInterval %d > max ISOC %d, may drop data!\n",
770 speed_names[speed], dir, max_size_bw, bint, max_size_ep);
771 max_size_bw = max_size_ep;
774 ep_desc->wMaxPacketSize = cpu_to_le16(max_size_bw);
775 ep_desc->bInterval = bint;
780 static struct uac2_feature_unit_descriptor *build_fu_desc(int chmask)
782 struct uac2_feature_unit_descriptor *fu_desc;
783 int channels = num_channels(chmask);
784 int fu_desc_size = UAC2_DT_FEATURE_UNIT_SIZE(channels);
786 fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
790 fu_desc->bLength = fu_desc_size;
791 fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
793 fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
795 /* bUnitID, bSourceID and bmaControls will be defined later */
800 /* Use macro to overcome line length limitation */
801 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
803 static void setup_headers(struct f_uac2_opts *opts,
804 struct usb_descriptor_header **headers,
805 enum usb_device_speed speed)
807 struct usb_ss_ep_comp_descriptor *epout_desc_comp = NULL;
808 struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL;
809 struct usb_ss_ep_comp_descriptor *epin_fback_desc_comp = NULL;
810 struct usb_ss_ep_comp_descriptor *ep_int_desc_comp = NULL;
811 struct usb_endpoint_descriptor *epout_desc;
812 struct usb_endpoint_descriptor *epin_desc;
813 struct usb_endpoint_descriptor *epin_fback_desc;
814 struct usb_endpoint_descriptor *ep_int_desc;
819 epout_desc = &fs_epout_desc;
820 epin_desc = &fs_epin_desc;
821 epin_fback_desc = &fs_epin_fback_desc;
822 ep_int_desc = &fs_ep_int_desc;
825 epout_desc = &hs_epout_desc;
826 epin_desc = &hs_epin_desc;
827 epin_fback_desc = &hs_epin_fback_desc;
828 ep_int_desc = &hs_ep_int_desc;
831 epout_desc = &ss_epout_desc;
832 epin_desc = &ss_epin_desc;
833 epout_desc_comp = &ss_epout_desc_comp;
834 epin_desc_comp = &ss_epin_desc_comp;
835 epin_fback_desc = &ss_epin_fback_desc;
836 epin_fback_desc_comp = &ss_epin_fback_desc_comp;
837 ep_int_desc = &ss_ep_int_desc;
838 ep_int_desc_comp = &ss_ep_int_desc_comp;
842 headers[i++] = USBDHDR(&iad_desc);
843 headers[i++] = USBDHDR(&std_ac_if_desc);
844 headers[i++] = USBDHDR(&ac_hdr_desc);
846 headers[i++] = USBDHDR(&in_clk_src_desc);
847 if (EPOUT_EN(opts)) {
848 headers[i++] = USBDHDR(&out_clk_src_desc);
849 headers[i++] = USBDHDR(&usb_out_it_desc);
852 headers[i++] = USBDHDR(out_feature_unit_desc);
856 headers[i++] = USBDHDR(&io_in_it_desc);
859 headers[i++] = USBDHDR(in_feature_unit_desc);
861 headers[i++] = USBDHDR(&usb_in_ot_desc);
865 headers[i++] = USBDHDR(&io_out_ot_desc);
867 if (FUOUT_EN(opts) || FUIN_EN(opts)) {
868 headers[i++] = USBDHDR(ep_int_desc);
869 if (ep_int_desc_comp)
870 headers[i++] = USBDHDR(ep_int_desc_comp);
873 if (EPOUT_EN(opts)) {
874 headers[i++] = USBDHDR(&std_as_out_if0_desc);
875 headers[i++] = USBDHDR(&std_as_out_if1_desc);
876 headers[i++] = USBDHDR(&as_out_hdr_desc);
877 headers[i++] = USBDHDR(&as_out_fmt1_desc);
878 headers[i++] = USBDHDR(epout_desc);
880 headers[i++] = USBDHDR(epout_desc_comp);
882 headers[i++] = USBDHDR(&as_iso_out_desc);
884 if (EPOUT_FBACK_IN_EN(opts)) {
885 headers[i++] = USBDHDR(epin_fback_desc);
886 if (epin_fback_desc_comp)
887 headers[i++] = USBDHDR(epin_fback_desc_comp);
892 headers[i++] = USBDHDR(&std_as_in_if0_desc);
893 headers[i++] = USBDHDR(&std_as_in_if1_desc);
894 headers[i++] = USBDHDR(&as_in_hdr_desc);
895 headers[i++] = USBDHDR(&as_in_fmt1_desc);
896 headers[i++] = USBDHDR(epin_desc);
898 headers[i++] = USBDHDR(epin_desc_comp);
900 headers[i++] = USBDHDR(&as_iso_in_desc);
905 static void setup_descriptor(struct f_uac2_opts *opts)
907 /* patch descriptors */
908 int i = 1; /* ID's start with 1 */
911 usb_out_it_desc.bTerminalID = i++;
913 io_in_it_desc.bTerminalID = i++;
915 io_out_ot_desc.bTerminalID = i++;
917 usb_in_ot_desc.bTerminalID = i++;
919 out_feature_unit_desc->bUnitID = i++;
921 in_feature_unit_desc->bUnitID = i++;
923 out_clk_src_desc.bClockID = i++;
925 in_clk_src_desc.bClockID = i++;
927 usb_out_it_desc.bCSourceID = out_clk_src_desc.bClockID;
930 usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
931 in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
933 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
936 usb_in_ot_desc.bCSourceID = in_clk_src_desc.bClockID;
937 io_in_it_desc.bCSourceID = in_clk_src_desc.bClockID;
938 io_out_ot_desc.bCSourceID = out_clk_src_desc.bClockID;
940 if (FUOUT_EN(opts)) {
941 io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
942 out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
944 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
947 as_out_hdr_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
948 as_in_hdr_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
950 iad_desc.bInterfaceCount = 1;
951 ac_hdr_desc.wTotalLength = cpu_to_le16(sizeof(ac_hdr_desc));
954 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
956 len += sizeof(in_clk_src_desc);
957 len += sizeof(usb_in_ot_desc);
960 len += in_feature_unit_desc->bLength;
962 len += sizeof(io_in_it_desc);
963 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
964 iad_desc.bInterfaceCount++;
966 if (EPOUT_EN(opts)) {
967 u16 len = le16_to_cpu(ac_hdr_desc.wTotalLength);
969 len += sizeof(out_clk_src_desc);
970 len += sizeof(usb_out_it_desc);
973 len += out_feature_unit_desc->bLength;
975 len += sizeof(io_out_ot_desc);
976 ac_hdr_desc.wTotalLength = cpu_to_le16(len);
977 iad_desc.bInterfaceCount++;
980 io_in_it_desc.wTerminalType = cpu_to_le16(opts->c_terminal_type);
981 io_out_ot_desc.wTerminalType = cpu_to_le16(opts->p_terminal_type);
983 setup_headers(opts, fs_audio_desc, USB_SPEED_FULL);
984 setup_headers(opts, hs_audio_desc, USB_SPEED_HIGH);
985 setup_headers(opts, ss_audio_desc, USB_SPEED_SUPER);
988 static int afunc_validate_opts(struct g_audio *agdev, struct device *dev)
990 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
991 const char *msg = NULL;
993 if (!opts->p_chmask && !opts->c_chmask)
994 msg = "no playback and capture channels";
995 else if (opts->p_chmask & ~UAC2_CHANNEL_MASK)
996 msg = "unsupported playback channels mask";
997 else if (opts->c_chmask & ~UAC2_CHANNEL_MASK)
998 msg = "unsupported capture channels mask";
999 else if ((opts->p_ssize < 1) || (opts->p_ssize > 4))
1000 msg = "incorrect playback sample size";
1001 else if ((opts->c_ssize < 1) || (opts->c_ssize > 4))
1002 msg = "incorrect capture sample size";
1003 else if (!opts->p_srates[0])
1004 msg = "incorrect playback sampling rate";
1005 else if (!opts->c_srates[0])
1006 msg = "incorrect capture sampling rate";
1008 else if (opts->p_volume_max <= opts->p_volume_min)
1009 msg = "incorrect playback volume max/min";
1010 else if (opts->c_volume_max <= opts->c_volume_min)
1011 msg = "incorrect capture volume max/min";
1012 else if (opts->p_volume_res <= 0)
1013 msg = "negative/zero playback volume resolution";
1014 else if (opts->c_volume_res <= 0)
1015 msg = "negative/zero capture volume resolution";
1017 else if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res)
1018 msg = "incorrect playback volume resolution";
1019 else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res)
1020 msg = "incorrect capture volume resolution";
1022 else if ((opts->p_hs_bint < 0) || (opts->p_hs_bint > 4))
1023 msg = "incorrect playback HS/SS bInterval (1-4: fixed, 0: auto)";
1024 else if ((opts->c_hs_bint < 0) || (opts->c_hs_bint > 4))
1025 msg = "incorrect capture HS/SS bInterval (1-4: fixed, 0: auto)";
1028 dev_err(dev, "Error: %s\n", msg);
1036 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
1038 struct f_uac2 *uac2 = func_to_uac2(fn);
1039 struct g_audio *agdev = func_to_g_audio(fn);
1040 struct usb_composite_dev *cdev = cfg->cdev;
1041 struct usb_gadget *gadget = cdev->gadget;
1042 struct device *dev = &gadget->dev;
1043 struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev);
1044 struct usb_string *us;
1047 ret = afunc_validate_opts(agdev, dev);
1051 strings_fn[STR_ASSOC].s = uac2_opts->function_name;
1053 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
1057 if (FUOUT_EN(uac2_opts)) {
1058 out_feature_unit_desc = build_fu_desc(uac2_opts->c_chmask);
1059 if (!out_feature_unit_desc)
1062 if (FUIN_EN(uac2_opts)) {
1063 in_feature_unit_desc = build_fu_desc(uac2_opts->p_chmask);
1064 if (!in_feature_unit_desc) {
1070 iad_desc.iFunction = us[STR_ASSOC].id;
1071 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
1072 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
1073 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
1074 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
1075 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
1076 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
1077 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
1078 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
1079 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
1080 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
1081 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
1083 if (FUOUT_EN(uac2_opts)) {
1084 u8 *i_feature = (u8 *)out_feature_unit_desc +
1085 out_feature_unit_desc->bLength - 1;
1086 *i_feature = us[STR_FU_OUT].id;
1088 if (FUIN_EN(uac2_opts)) {
1089 u8 *i_feature = (u8 *)in_feature_unit_desc +
1090 in_feature_unit_desc->bLength - 1;
1091 *i_feature = us[STR_FU_IN].id;
1095 /* Initialize the configurable parameters */
1096 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1097 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1098 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1099 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1100 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1101 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1102 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1103 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1104 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
1105 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
1106 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
1107 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
1108 if (FUOUT_EN(uac2_opts)) {
1109 __le32 *bma = (__le32 *)&out_feature_unit_desc->bmaControls[0];
1112 if (uac2_opts->c_mute_present)
1113 control |= CONTROL_RDWR << FU_MUTE_CTRL;
1114 if (uac2_opts->c_volume_present)
1115 control |= CONTROL_RDWR << FU_VOL_CTRL;
1116 *bma = cpu_to_le32(control);
1118 if (FUIN_EN(uac2_opts)) {
1119 __le32 *bma = (__le32 *)&in_feature_unit_desc->bmaControls[0];
1122 if (uac2_opts->p_mute_present)
1123 control |= CONTROL_RDWR << FU_MUTE_CTRL;
1124 if (uac2_opts->p_volume_present)
1125 control |= CONTROL_RDWR << FU_VOL_CTRL;
1126 *bma = cpu_to_le32(control);
1129 ret = usb_interface_id(cfg, fn);
1131 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1134 iad_desc.bFirstInterface = ret;
1136 std_ac_if_desc.bInterfaceNumber = ret;
1137 uac2->ac_intf = ret;
1140 if (EPOUT_EN(uac2_opts)) {
1141 ret = usb_interface_id(cfg, fn);
1143 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1146 std_as_out_if0_desc.bInterfaceNumber = ret;
1147 std_as_out_if1_desc.bInterfaceNumber = ret;
1148 std_as_out_if1_desc.bNumEndpoints = 1;
1149 uac2->as_out_intf = ret;
1150 uac2->as_out_alt = 0;
1152 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1153 fs_epout_desc.bmAttributes =
1154 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1155 hs_epout_desc.bmAttributes =
1156 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1157 ss_epout_desc.bmAttributes =
1158 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC;
1159 std_as_out_if1_desc.bNumEndpoints++;
1161 fs_epout_desc.bmAttributes =
1162 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1163 hs_epout_desc.bmAttributes =
1164 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1165 ss_epout_desc.bmAttributes =
1166 USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE;
1170 if (EPIN_EN(uac2_opts)) {
1171 ret = usb_interface_id(cfg, fn);
1173 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1176 std_as_in_if0_desc.bInterfaceNumber = ret;
1177 std_as_in_if1_desc.bInterfaceNumber = ret;
1178 uac2->as_in_intf = ret;
1179 uac2->as_in_alt = 0;
1182 if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts)) {
1183 uac2->int_ep = usb_ep_autoconfig(gadget, &fs_ep_int_desc);
1184 if (!uac2->int_ep) {
1185 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1190 std_ac_if_desc.bNumEndpoints = 1;
1193 hs_epin_desc.bInterval = uac2_opts->p_hs_bint;
1194 ss_epin_desc.bInterval = uac2_opts->p_hs_bint;
1195 hs_epout_desc.bInterval = uac2_opts->c_hs_bint;
1196 ss_epout_desc.bInterval = uac2_opts->c_hs_bint;
1198 /* Calculate wMaxPacketSize according to audio bandwidth */
1199 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &fs_epin_desc,
1200 USB_SPEED_FULL, true);
1202 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1206 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &fs_epout_desc,
1207 USB_SPEED_FULL, false);
1209 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1213 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epin_desc,
1214 USB_SPEED_HIGH, true);
1216 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1220 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epout_desc,
1221 USB_SPEED_HIGH, false);
1223 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1227 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epin_desc,
1228 USB_SPEED_SUPER, true);
1230 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1234 ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epout_desc,
1235 USB_SPEED_SUPER, false);
1237 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1241 if (EPOUT_EN(uac2_opts)) {
1242 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1243 if (!agdev->out_ep) {
1244 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1248 if (EPOUT_FBACK_IN_EN(uac2_opts)) {
1249 agdev->in_ep_fback = usb_ep_autoconfig(gadget,
1250 &fs_epin_fback_desc);
1251 if (!agdev->in_ep_fback) {
1252 dev_err(dev, "%s:%d Error!\n",
1253 __func__, __LINE__);
1260 if (EPIN_EN(uac2_opts)) {
1261 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1262 if (!agdev->in_ep) {
1263 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1269 agdev->in_ep_maxpsize = max_t(u16,
1270 le16_to_cpu(fs_epin_desc.wMaxPacketSize),
1271 le16_to_cpu(hs_epin_desc.wMaxPacketSize));
1272 agdev->out_ep_maxpsize = max_t(u16,
1273 le16_to_cpu(fs_epout_desc.wMaxPacketSize),
1274 le16_to_cpu(hs_epout_desc.wMaxPacketSize));
1276 agdev->in_ep_maxpsize = max_t(u16, agdev->in_ep_maxpsize,
1277 le16_to_cpu(ss_epin_desc.wMaxPacketSize));
1278 agdev->out_ep_maxpsize = max_t(u16, agdev->out_ep_maxpsize,
1279 le16_to_cpu(ss_epout_desc.wMaxPacketSize));
1281 ss_epin_desc_comp.wBytesPerInterval = ss_epin_desc.wMaxPacketSize;
1282 ss_epout_desc_comp.wBytesPerInterval = ss_epout_desc.wMaxPacketSize;
1284 // HS and SS endpoint addresses are copied from autoconfigured FS descriptors
1285 hs_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1286 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1287 hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1288 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1289 ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1290 ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress;
1291 ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1292 ss_ep_int_desc.bEndpointAddress = fs_ep_int_desc.bEndpointAddress;
1294 setup_descriptor(uac2_opts);
1296 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
1301 agdev->gadget = gadget;
1303 agdev->params.p_chmask = uac2_opts->p_chmask;
1304 memcpy(agdev->params.p_srates, uac2_opts->p_srates,
1305 sizeof(agdev->params.p_srates));
1306 agdev->params.p_ssize = uac2_opts->p_ssize;
1307 if (FUIN_EN(uac2_opts)) {
1308 agdev->params.p_fu.id = USB_IN_FU_ID;
1309 agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
1310 agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
1311 agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
1312 agdev->params.p_fu.volume_max = uac2_opts->p_volume_max;
1313 agdev->params.p_fu.volume_res = uac2_opts->p_volume_res;
1315 agdev->params.c_chmask = uac2_opts->c_chmask;
1316 memcpy(agdev->params.c_srates, uac2_opts->c_srates,
1317 sizeof(agdev->params.c_srates));
1318 agdev->params.c_ssize = uac2_opts->c_ssize;
1319 if (FUOUT_EN(uac2_opts)) {
1320 agdev->params.c_fu.id = USB_OUT_FU_ID;
1321 agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
1322 agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
1323 agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
1324 agdev->params.c_fu.volume_max = uac2_opts->c_volume_max;
1325 agdev->params.c_fu.volume_res = uac2_opts->c_volume_res;
1327 agdev->params.req_number = uac2_opts->req_number;
1328 agdev->params.fb_max = uac2_opts->fb_max;
1330 if (FUOUT_EN(uac2_opts) || FUIN_EN(uac2_opts))
1331 agdev->notify = afunc_notify;
1333 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
1335 goto err_free_descs;
1340 usb_free_all_descriptors(fn);
1341 agdev->gadget = NULL;
1343 kfree(out_feature_unit_desc);
1344 out_feature_unit_desc = NULL;
1345 kfree(in_feature_unit_desc);
1346 in_feature_unit_desc = NULL;
1351 afunc_notify_complete(struct usb_ep *_ep, struct usb_request *req)
1353 struct g_audio *agdev = req->context;
1354 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1356 atomic_dec(&uac2->int_count);
1358 usb_ep_free_request(_ep, req);
1362 afunc_notify(struct g_audio *agdev, int unit_id, int cs)
1364 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1365 struct usb_request *req;
1366 struct uac2_interrupt_data_msg *msg;
1367 u16 w_index, w_value;
1370 if (!uac2->int_ep->enabled)
1373 if (atomic_inc_return(&uac2->int_count) > UAC2_DEF_INT_REQ_NUM) {
1374 atomic_dec(&uac2->int_count);
1378 req = usb_ep_alloc_request(uac2->int_ep, GFP_ATOMIC);
1381 goto err_dec_int_count;
1384 msg = kzalloc(sizeof(*msg), GFP_ATOMIC);
1387 goto err_free_request;
1390 w_index = unit_id << 8 | uac2->ac_intf;
1393 msg->bInfo = 0; /* Non-vendor, interface interrupt */
1394 msg->bAttribute = UAC2_CS_CUR;
1395 msg->wIndex = cpu_to_le16(w_index);
1396 msg->wValue = cpu_to_le16(w_value);
1398 req->length = sizeof(*msg);
1400 req->context = agdev;
1401 req->complete = afunc_notify_complete;
1403 ret = usb_ep_queue(uac2->int_ep, req, GFP_ATOMIC);
1413 usb_ep_free_request(uac2->int_ep, req);
1415 atomic_dec(&uac2->int_count);
1421 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1423 struct usb_composite_dev *cdev = fn->config->cdev;
1424 struct f_uac2 *uac2 = func_to_uac2(fn);
1425 struct g_audio *agdev = func_to_g_audio(fn);
1426 struct usb_gadget *gadget = cdev->gadget;
1427 struct device *dev = &gadget->dev;
1430 /* No i/f has more than 2 alt settings */
1432 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1436 if (intf == uac2->ac_intf) {
1437 /* Control I/f has only 1 AltSetting - 0 */
1439 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1443 /* restart interrupt endpoint */
1445 usb_ep_disable(uac2->int_ep);
1446 config_ep_by_speed(gadget, &agdev->func, uac2->int_ep);
1447 usb_ep_enable(uac2->int_ep);
1453 if (intf == uac2->as_out_intf) {
1454 uac2->as_out_alt = alt;
1457 ret = u_audio_start_capture(&uac2->g_audio);
1459 u_audio_stop_capture(&uac2->g_audio);
1460 } else if (intf == uac2->as_in_intf) {
1461 uac2->as_in_alt = alt;
1464 ret = u_audio_start_playback(&uac2->g_audio);
1466 u_audio_stop_playback(&uac2->g_audio);
1468 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1476 afunc_get_alt(struct usb_function *fn, unsigned intf)
1478 struct f_uac2 *uac2 = func_to_uac2(fn);
1479 struct g_audio *agdev = func_to_g_audio(fn);
1481 if (intf == uac2->ac_intf)
1482 return uac2->ac_alt;
1483 else if (intf == uac2->as_out_intf)
1484 return uac2->as_out_alt;
1485 else if (intf == uac2->as_in_intf)
1486 return uac2->as_in_alt;
1488 dev_err(&agdev->gadget->dev,
1489 "%s:%d Invalid Interface %d!\n",
1490 __func__, __LINE__, intf);
1496 afunc_disable(struct usb_function *fn)
1498 struct f_uac2 *uac2 = func_to_uac2(fn);
1500 uac2->as_in_alt = 0;
1501 uac2->as_out_alt = 0;
1502 u_audio_stop_capture(&uac2->g_audio);
1503 u_audio_stop_playback(&uac2->g_audio);
1505 usb_ep_disable(uac2->int_ep);
1509 afunc_suspend(struct usb_function *fn)
1511 struct f_uac2 *uac2 = func_to_uac2(fn);
1513 u_audio_suspend(&uac2->g_audio);
1517 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1519 struct usb_request *req = fn->config->cdev->req;
1520 struct g_audio *agdev = func_to_g_audio(fn);
1521 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1522 u16 w_length = le16_to_cpu(cr->wLength);
1523 u16 w_index = le16_to_cpu(cr->wIndex);
1524 u16 w_value = le16_to_cpu(cr->wValue);
1525 u8 entity_id = (w_index >> 8) & 0xff;
1526 u8 control_selector = w_value >> 8;
1527 int value = -EOPNOTSUPP;
1528 u32 p_srate, c_srate;
1530 u_audio_get_playback_srate(agdev, &p_srate);
1531 u_audio_get_capture_srate(agdev, &c_srate);
1533 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1534 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1535 struct cntrl_cur_lay3 c;
1537 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1539 if (entity_id == USB_IN_CLK_ID)
1540 c.dCUR = cpu_to_le32(p_srate);
1541 else if (entity_id == USB_OUT_CLK_ID)
1542 c.dCUR = cpu_to_le32(c_srate);
1544 value = min_t(unsigned int, w_length, sizeof(c));
1545 memcpy(req->buf, &c, value);
1546 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1547 *(u8 *)req->buf = 1;
1548 value = min_t(unsigned int, w_length, 1);
1550 dev_err(&agdev->gadget->dev,
1551 "%s:%d control_selector=%d TODO!\n",
1552 __func__, __LINE__, control_selector);
1554 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1555 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1556 unsigned int is_playback = 0;
1558 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1561 if (control_selector == UAC_FU_MUTE) {
1564 u_audio_get_mute(agdev, is_playback, &mute);
1566 *(u8 *)req->buf = mute;
1567 value = min_t(unsigned int, w_length, 1);
1568 } else if (control_selector == UAC_FU_VOLUME) {
1569 struct cntrl_cur_lay2 c;
1572 memset(&c, 0, sizeof(struct cntrl_cur_lay2));
1574 u_audio_get_volume(agdev, is_playback, &volume);
1575 c.wCUR = cpu_to_le16(volume);
1577 value = min_t(unsigned int, w_length, sizeof(c));
1578 memcpy(req->buf, &c, value);
1580 dev_err(&agdev->gadget->dev,
1581 "%s:%d control_selector=%d TODO!\n",
1582 __func__, __LINE__, control_selector);
1585 dev_err(&agdev->gadget->dev,
1586 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1587 __func__, __LINE__, entity_id, control_selector);
1594 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1596 struct usb_request *req = fn->config->cdev->req;
1597 struct g_audio *agdev = func_to_g_audio(fn);
1598 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1599 u16 w_length = le16_to_cpu(cr->wLength);
1600 u16 w_index = le16_to_cpu(cr->wIndex);
1601 u16 w_value = le16_to_cpu(cr->wValue);
1602 u8 entity_id = (w_index >> 8) & 0xff;
1603 u8 control_selector = w_value >> 8;
1604 int value = -EOPNOTSUPP;
1606 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1607 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1608 struct cntrl_ranges_lay3_srates rs;
1610 int wNumSubRanges = 0;
1614 if (entity_id == USB_IN_CLK_ID)
1615 srates = opts->p_srates;
1616 else if (entity_id == USB_OUT_CLK_ID)
1617 srates = opts->c_srates;
1620 for (i = 0; i < UAC_MAX_RATES; i++) {
1625 rs.r[wNumSubRanges].dMIN = cpu_to_le32(srate);
1626 rs.r[wNumSubRanges].dMAX = cpu_to_le32(srate);
1627 rs.r[wNumSubRanges].dRES = 0;
1629 dev_dbg(&agdev->gadget->dev,
1630 "%s(): clk %d: rate ID %d: %d\n",
1631 __func__, entity_id, wNumSubRanges, srate);
1633 rs.wNumSubRanges = cpu_to_le16(wNumSubRanges);
1634 value = min_t(unsigned int, w_length, ranges_lay3_size(rs));
1635 dev_dbg(&agdev->gadget->dev, "%s(): sending %d rates, size %d\n",
1636 __func__, rs.wNumSubRanges, value);
1637 memcpy(req->buf, &rs, value);
1639 dev_err(&agdev->gadget->dev,
1640 "%s:%d control_selector=%d TODO!\n",
1641 __func__, __LINE__, control_selector);
1643 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1644 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1645 unsigned int is_playback = 0;
1647 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1650 if (control_selector == UAC_FU_VOLUME) {
1651 struct cntrl_range_lay2 r;
1652 s16 max_db, min_db, res_db;
1655 max_db = opts->p_volume_max;
1656 min_db = opts->p_volume_min;
1657 res_db = opts->p_volume_res;
1659 max_db = opts->c_volume_max;
1660 min_db = opts->c_volume_min;
1661 res_db = opts->c_volume_res;
1664 r.wMAX = cpu_to_le16(max_db);
1665 r.wMIN = cpu_to_le16(min_db);
1666 r.wRES = cpu_to_le16(res_db);
1667 r.wNumSubRanges = cpu_to_le16(1);
1669 value = min_t(unsigned int, w_length, sizeof(r));
1670 memcpy(req->buf, &r, value);
1672 dev_err(&agdev->gadget->dev,
1673 "%s:%d control_selector=%d TODO!\n",
1674 __func__, __LINE__, control_selector);
1677 dev_err(&agdev->gadget->dev,
1678 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1679 __func__, __LINE__, entity_id, control_selector);
1686 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1688 if (cr->bRequest == UAC2_CS_CUR)
1689 return in_rq_cur(fn, cr);
1690 else if (cr->bRequest == UAC2_CS_RANGE)
1691 return in_rq_range(fn, cr);
1696 static void uac2_cs_control_sam_freq(struct usb_ep *ep, struct usb_request *req)
1698 struct usb_function *fn = ep->driver_data;
1699 struct g_audio *agdev = func_to_g_audio(fn);
1700 struct f_uac2 *uac2 = func_to_uac2(fn);
1703 if (req->actual != 4)
1706 val = le32_to_cpu(*((__le32 *)req->buf));
1707 dev_dbg(&agdev->gadget->dev, "%s val: %d.\n", __func__, val);
1708 if (uac2->clock_id == USB_IN_CLK_ID) {
1709 u_audio_set_playback_srate(agdev, val);
1710 } else if (uac2->clock_id == USB_OUT_CLK_ID) {
1711 u_audio_set_capture_srate(agdev, val);
1716 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
1718 struct g_audio *agdev = req->context;
1719 struct usb_composite_dev *cdev = agdev->func.config->cdev;
1720 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1721 struct f_uac2 *uac2 = func_to_uac2(&agdev->func);
1722 struct usb_ctrlrequest *cr = &uac2->setup_cr;
1723 u16 w_index = le16_to_cpu(cr->wIndex);
1724 u16 w_value = le16_to_cpu(cr->wValue);
1725 u8 entity_id = (w_index >> 8) & 0xff;
1726 u8 control_selector = w_value >> 8;
1728 if (req->status != 0) {
1729 dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
1733 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1734 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1735 unsigned int is_playback = 0;
1737 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
1740 if (control_selector == UAC_FU_MUTE) {
1741 u8 mute = *(u8 *)req->buf;
1743 u_audio_set_mute(agdev, is_playback, mute);
1746 } else if (control_selector == UAC_FU_VOLUME) {
1747 struct cntrl_cur_lay2 *c = req->buf;
1750 volume = le16_to_cpu(c->wCUR);
1751 u_audio_set_volume(agdev, is_playback, volume);
1755 dev_err(&agdev->gadget->dev,
1756 "%s:%d control_selector=%d TODO!\n",
1757 __func__, __LINE__, control_selector);
1758 usb_ep_set_halt(ep);
1764 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1766 struct usb_composite_dev *cdev = fn->config->cdev;
1767 struct usb_request *req = fn->config->cdev->req;
1768 struct g_audio *agdev = func_to_g_audio(fn);
1769 struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev);
1770 struct f_uac2 *uac2 = func_to_uac2(fn);
1771 u16 w_length = le16_to_cpu(cr->wLength);
1772 u16 w_index = le16_to_cpu(cr->wIndex);
1773 u16 w_value = le16_to_cpu(cr->wValue);
1774 u8 entity_id = (w_index >> 8) & 0xff;
1775 u8 control_selector = w_value >> 8;
1776 u8 clock_id = w_index >> 8;
1778 if ((entity_id == USB_IN_CLK_ID) || (entity_id == USB_OUT_CLK_ID)) {
1779 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1780 dev_dbg(&agdev->gadget->dev,
1781 "control_selector UAC2_CS_CONTROL_SAM_FREQ, clock: %d\n", clock_id);
1782 cdev->gadget->ep0->driver_data = fn;
1783 uac2->clock_id = clock_id;
1784 req->complete = uac2_cs_control_sam_freq;
1787 } else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
1788 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
1789 memcpy(&uac2->setup_cr, cr, sizeof(*cr));
1790 req->context = agdev;
1791 req->complete = out_rq_cur_complete;
1795 dev_err(&agdev->gadget->dev,
1796 "%s:%d entity_id=%d control_selector=%d TODO!\n",
1797 __func__, __LINE__, entity_id, control_selector);
1803 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1805 struct f_uac2 *uac2 = func_to_uac2(fn);
1806 struct g_audio *agdev = func_to_g_audio(fn);
1807 u16 w_index = le16_to_cpu(cr->wIndex);
1808 u8 intf = w_index & 0xff;
1810 if (intf != uac2->ac_intf) {
1811 dev_err(&agdev->gadget->dev,
1812 "%s:%d Error!\n", __func__, __LINE__);
1816 if (cr->bRequestType & USB_DIR_IN)
1817 return ac_rq_in(fn, cr);
1818 else if (cr->bRequest == UAC2_CS_CUR)
1819 return out_rq_cur(fn, cr);
1825 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1827 struct usb_composite_dev *cdev = fn->config->cdev;
1828 struct g_audio *agdev = func_to_g_audio(fn);
1829 struct usb_request *req = cdev->req;
1830 u16 w_length = le16_to_cpu(cr->wLength);
1831 int value = -EOPNOTSUPP;
1833 /* Only Class specific requests are supposed to reach here */
1834 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1837 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1838 value = setup_rq_inf(fn, cr);
1840 dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
1841 __func__, __LINE__);
1844 req->length = value;
1845 req->zero = value < w_length;
1846 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1848 dev_err(&agdev->gadget->dev,
1849 "%s:%d Error!\n", __func__, __LINE__);
1857 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1859 return container_of(to_config_group(item), struct f_uac2_opts,
1863 static void f_uac2_attr_release(struct config_item *item)
1865 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1867 usb_put_function_instance(&opts->func_inst);
1870 static struct configfs_item_operations f_uac2_item_ops = {
1871 .release = f_uac2_attr_release,
1874 #define uac2_kstrtou8 kstrtou8
1875 #define uac2_kstrtou32 kstrtou32
1876 #define uac2_kstrtos16 kstrtos16
1877 #define uac2_kstrtobool(s, base, res) kstrtobool((s), (res))
1879 static const char *u8_fmt = "%u\n";
1880 static const char *u32_fmt = "%u\n";
1881 static const char *s16_fmt = "%hd\n";
1882 static const char *bool_fmt = "%u\n";
1884 #define UAC2_ATTRIBUTE(type, name) \
1885 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1888 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1891 mutex_lock(&opts->lock); \
1892 result = sprintf(page, type##_fmt, opts->name); \
1893 mutex_unlock(&opts->lock); \
1898 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1899 const char *page, size_t len) \
1901 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1905 mutex_lock(&opts->lock); \
1906 if (opts->refcnt) { \
1911 ret = uac2_kstrto##type(page, 0, &num); \
1919 mutex_unlock(&opts->lock); \
1923 CONFIGFS_ATTR(f_uac2_opts_, name)
1925 #define UAC2_ATTRIBUTE_SYNC(name) \
1926 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1929 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1933 mutex_lock(&opts->lock); \
1934 switch (opts->name) { \
1935 case USB_ENDPOINT_SYNC_ASYNC: \
1938 case USB_ENDPOINT_SYNC_ADAPTIVE: \
1945 result = sprintf(page, "%s\n", str); \
1946 mutex_unlock(&opts->lock); \
1951 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1952 const char *page, size_t len) \
1954 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1957 mutex_lock(&opts->lock); \
1958 if (opts->refcnt) { \
1963 if (!strncmp(page, "async", 5)) \
1964 opts->name = USB_ENDPOINT_SYNC_ASYNC; \
1965 else if (!strncmp(page, "adaptive", 8)) \
1966 opts->name = USB_ENDPOINT_SYNC_ADAPTIVE; \
1975 mutex_unlock(&opts->lock); \
1979 CONFIGFS_ATTR(f_uac2_opts_, name)
1981 #define UAC2_RATE_ATTRIBUTE(name) \
1982 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1985 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1989 mutex_lock(&opts->lock); \
1991 for (i = 0; i < UAC_MAX_RATES; i++) { \
1992 if (opts->name##s[i] == 0) \
1994 result += sprintf(page + strlen(page), "%u,", \
1995 opts->name##s[i]); \
1997 if (strlen(page) > 0) \
1998 page[strlen(page) - 1] = '\n'; \
1999 mutex_unlock(&opts->lock); \
2004 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
2005 const char *page, size_t len) \
2007 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
2008 char *split_page = NULL; \
2009 int ret = -EINVAL; \
2014 mutex_lock(&opts->lock); \
2015 if (opts->refcnt) { \
2021 memset(opts->name##s, 0x00, sizeof(opts->name##s)); \
2022 split_page = kstrdup(page, GFP_KERNEL); \
2023 while ((token = strsep(&split_page, ",")) != NULL) { \
2024 ret = kstrtou32(token, 0, &num); \
2028 opts->name##s[i++] = num; \
2033 kfree(split_page); \
2034 mutex_unlock(&opts->lock); \
2038 CONFIGFS_ATTR(f_uac2_opts_, name)
2040 #define UAC2_ATTRIBUTE_STRING(name) \
2041 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
2044 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
2047 mutex_lock(&opts->lock); \
2048 result = snprintf(page, sizeof(opts->name), "%s", opts->name); \
2049 mutex_unlock(&opts->lock); \
2054 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
2055 const char *page, size_t len) \
2057 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
2060 mutex_lock(&opts->lock); \
2061 if (opts->refcnt) { \
2066 ret = snprintf(opts->name, min(sizeof(opts->name), len), \
2070 mutex_unlock(&opts->lock); \
2074 CONFIGFS_ATTR(f_uac2_opts_, name)
2076 UAC2_ATTRIBUTE(u32, p_chmask);
2077 UAC2_RATE_ATTRIBUTE(p_srate);
2078 UAC2_ATTRIBUTE(u32, p_ssize);
2079 UAC2_ATTRIBUTE(u8, p_hs_bint);
2080 UAC2_ATTRIBUTE(u32, c_chmask);
2081 UAC2_RATE_ATTRIBUTE(c_srate);
2082 UAC2_ATTRIBUTE_SYNC(c_sync);
2083 UAC2_ATTRIBUTE(u32, c_ssize);
2084 UAC2_ATTRIBUTE(u8, c_hs_bint);
2085 UAC2_ATTRIBUTE(u32, req_number);
2087 UAC2_ATTRIBUTE(bool, p_mute_present);
2088 UAC2_ATTRIBUTE(bool, p_volume_present);
2089 UAC2_ATTRIBUTE(s16, p_volume_min);
2090 UAC2_ATTRIBUTE(s16, p_volume_max);
2091 UAC2_ATTRIBUTE(s16, p_volume_res);
2093 UAC2_ATTRIBUTE(bool, c_mute_present);
2094 UAC2_ATTRIBUTE(bool, c_volume_present);
2095 UAC2_ATTRIBUTE(s16, c_volume_min);
2096 UAC2_ATTRIBUTE(s16, c_volume_max);
2097 UAC2_ATTRIBUTE(s16, c_volume_res);
2098 UAC2_ATTRIBUTE(u32, fb_max);
2099 UAC2_ATTRIBUTE_STRING(function_name);
2101 UAC2_ATTRIBUTE(s16, p_terminal_type);
2102 UAC2_ATTRIBUTE(s16, c_terminal_type);
2104 static struct configfs_attribute *f_uac2_attrs[] = {
2105 &f_uac2_opts_attr_p_chmask,
2106 &f_uac2_opts_attr_p_srate,
2107 &f_uac2_opts_attr_p_ssize,
2108 &f_uac2_opts_attr_p_hs_bint,
2109 &f_uac2_opts_attr_c_chmask,
2110 &f_uac2_opts_attr_c_srate,
2111 &f_uac2_opts_attr_c_ssize,
2112 &f_uac2_opts_attr_c_hs_bint,
2113 &f_uac2_opts_attr_c_sync,
2114 &f_uac2_opts_attr_req_number,
2115 &f_uac2_opts_attr_fb_max,
2117 &f_uac2_opts_attr_p_mute_present,
2118 &f_uac2_opts_attr_p_volume_present,
2119 &f_uac2_opts_attr_p_volume_min,
2120 &f_uac2_opts_attr_p_volume_max,
2121 &f_uac2_opts_attr_p_volume_res,
2123 &f_uac2_opts_attr_c_mute_present,
2124 &f_uac2_opts_attr_c_volume_present,
2125 &f_uac2_opts_attr_c_volume_min,
2126 &f_uac2_opts_attr_c_volume_max,
2127 &f_uac2_opts_attr_c_volume_res,
2129 &f_uac2_opts_attr_function_name,
2131 &f_uac2_opts_attr_p_terminal_type,
2132 &f_uac2_opts_attr_c_terminal_type,
2137 static const struct config_item_type f_uac2_func_type = {
2138 .ct_item_ops = &f_uac2_item_ops,
2139 .ct_attrs = f_uac2_attrs,
2140 .ct_owner = THIS_MODULE,
2143 static void afunc_free_inst(struct usb_function_instance *f)
2145 struct f_uac2_opts *opts;
2147 opts = container_of(f, struct f_uac2_opts, func_inst);
2151 static struct usb_function_instance *afunc_alloc_inst(void)
2153 struct f_uac2_opts *opts;
2155 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2157 return ERR_PTR(-ENOMEM);
2159 mutex_init(&opts->lock);
2160 opts->func_inst.free_func_inst = afunc_free_inst;
2162 config_group_init_type_name(&opts->func_inst.group, "",
2165 opts->p_chmask = UAC2_DEF_PCHMASK;
2166 opts->p_srates[0] = UAC2_DEF_PSRATE;
2167 opts->p_ssize = UAC2_DEF_PSSIZE;
2168 opts->p_hs_bint = UAC2_DEF_PHSBINT;
2169 opts->c_chmask = UAC2_DEF_CCHMASK;
2170 opts->c_srates[0] = UAC2_DEF_CSRATE;
2171 opts->c_ssize = UAC2_DEF_CSSIZE;
2172 opts->c_hs_bint = UAC2_DEF_CHSBINT;
2173 opts->c_sync = UAC2_DEF_CSYNC;
2175 opts->p_mute_present = UAC2_DEF_MUTE_PRESENT;
2176 opts->p_volume_present = UAC2_DEF_VOLUME_PRESENT;
2177 opts->p_volume_min = UAC2_DEF_MIN_DB;
2178 opts->p_volume_max = UAC2_DEF_MAX_DB;
2179 opts->p_volume_res = UAC2_DEF_RES_DB;
2181 opts->c_mute_present = UAC2_DEF_MUTE_PRESENT;
2182 opts->c_volume_present = UAC2_DEF_VOLUME_PRESENT;
2183 opts->c_volume_min = UAC2_DEF_MIN_DB;
2184 opts->c_volume_max = UAC2_DEF_MAX_DB;
2185 opts->c_volume_res = UAC2_DEF_RES_DB;
2187 opts->req_number = UAC2_DEF_REQ_NUM;
2188 opts->fb_max = FBACK_FAST_MAX;
2190 snprintf(opts->function_name, sizeof(opts->function_name), "Source/Sink");
2192 opts->p_terminal_type = UAC2_DEF_P_TERM_TYPE;
2193 opts->c_terminal_type = UAC2_DEF_C_TERM_TYPE;
2195 return &opts->func_inst;
2198 static void afunc_free(struct usb_function *f)
2200 struct g_audio *agdev;
2201 struct f_uac2_opts *opts;
2203 agdev = func_to_g_audio(f);
2204 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
2206 mutex_lock(&opts->lock);
2208 mutex_unlock(&opts->lock);
2211 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
2213 struct g_audio *agdev = func_to_g_audio(f);
2215 g_audio_cleanup(agdev);
2216 usb_free_all_descriptors(f);
2218 agdev->gadget = NULL;
2220 kfree(out_feature_unit_desc);
2221 out_feature_unit_desc = NULL;
2222 kfree(in_feature_unit_desc);
2223 in_feature_unit_desc = NULL;
2226 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
2228 struct f_uac2 *uac2;
2229 struct f_uac2_opts *opts;
2231 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
2233 return ERR_PTR(-ENOMEM);
2235 opts = container_of(fi, struct f_uac2_opts, func_inst);
2236 mutex_lock(&opts->lock);
2238 mutex_unlock(&opts->lock);
2240 uac2->g_audio.func.name = "uac2_func";
2241 uac2->g_audio.func.bind = afunc_bind;
2242 uac2->g_audio.func.unbind = afunc_unbind;
2243 uac2->g_audio.func.set_alt = afunc_set_alt;
2244 uac2->g_audio.func.get_alt = afunc_get_alt;
2245 uac2->g_audio.func.disable = afunc_disable;
2246 uac2->g_audio.func.suspend = afunc_suspend;
2247 uac2->g_audio.func.setup = afunc_setup;
2248 uac2->g_audio.func.free_func = afunc_free;
2250 return &uac2->g_audio.func;
2253 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
2254 MODULE_LICENSE("GPL");
2255 MODULE_AUTHOR("Yadwinder Singh");
2256 MODULE_AUTHOR("Jaswinder Singh");
2257 MODULE_AUTHOR("Ruslan Bilovol");