2 * f_uac2.c -- USB Audio Class 2.0 Function
5 * Yadwinder Singh (yadi.brar01@gmail.com)
6 * Jaswinder Singh (jaswinder.singh@linaro.org)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/usb/audio.h>
15 #include <linux/usb/audio-v2.h>
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
25 /* Keep everyone on toes */
29 * The driver implements a simple UAC_2 topology.
30 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
31 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
32 * Capture and Playback sampling rates are independently
33 * controlled by two clock sources :
34 * CLK_5 := c_srate, and CLK_6 := p_srate
36 #define USB_OUT_IT_ID 1
38 #define IO_OUT_OT_ID 3
39 #define USB_IN_OT_ID 4
40 #define USB_OUT_CLK_ID 5
41 #define USB_IN_CLK_ID 6
43 #define CONTROL_ABSENT 0
44 #define CONTROL_RDONLY 1
45 #define CONTROL_RDWR 3
47 #define CLK_FREQ_CTRL 0
48 #define CLK_VLD_CTRL 2
57 const char *uac2_name = "snd_uac2";
60 struct uac2_rtd_params *pp; /* parent param */
61 struct usb_request *req;
64 struct uac2_rtd_params {
65 struct snd_uac2_chip *uac2; /* parent chip */
66 bool ep_enabled; /* if the ep is enabled */
67 /* Size of the ring buffer */
69 unsigned char *dma_area;
71 struct snd_pcm_substream *ss;
81 struct uac2_req ureq[USB_XFERS];
86 struct snd_uac2_chip {
87 struct platform_device pdev;
88 struct platform_driver pdrv;
90 struct uac2_rtd_params p_prm;
91 struct uac2_rtd_params c_prm;
93 struct snd_card *card;
97 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
98 #define PRD_SIZE_MAX PAGE_SIZE
101 static struct snd_pcm_hardware uac2_pcm_hardware = {
102 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
103 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
104 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
105 .rates = SNDRV_PCM_RATE_CONTINUOUS,
106 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
107 .buffer_bytes_max = BUFF_SIZE_MAX,
108 .period_bytes_max = PRD_SIZE_MAX,
109 .periods_min = MIN_PERIODS,
114 u8 as_out_intf, as_out_alt;
115 u8 as_in_intf, as_in_alt;
117 struct usb_ep *in_ep, *out_ep;
118 struct usb_function func;
120 /* The ALSA Sound Card it represents on the USB-Client side */
121 struct snd_uac2_chip uac2;
125 struct audio_dev *func_to_agdev(struct usb_function *f)
127 return container_of(f, struct audio_dev, func);
131 struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
133 return container_of(u, struct audio_dev, uac2);
137 struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
139 return container_of(p, struct snd_uac2_chip, pdev);
143 uint num_channels(uint chanmask)
148 num += (chanmask & 1);
156 agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
160 bool update_alsa = false;
161 unsigned char *src, *dst;
162 int status = req->status;
163 struct uac2_req *ur = req->context;
164 struct snd_pcm_substream *substream;
165 struct uac2_rtd_params *prm = ur->pp;
166 struct snd_uac2_chip *uac2 = prm->uac2;
168 /* i/f shutting down */
169 if (!prm->ep_enabled || req->status == -ESHUTDOWN)
173 * We can't really do much about bad xfers.
174 * Afterall, the ISOCH xfers could fail legitimately.
177 pr_debug("%s: iso_complete status(%d) %d/%d\n",
178 __func__, status, req->actual, req->length);
182 /* Do nothing if ALSA isn't active */
186 spin_lock_irqsave(&prm->lock, flags);
188 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
189 src = prm->dma_area + prm->hw_ptr;
190 req->actual = req->length;
193 dst = prm->dma_area + prm->hw_ptr;
197 pending = prm->hw_ptr % prm->period_size;
198 pending += req->actual;
199 if (pending >= prm->period_size)
202 prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
204 spin_unlock_irqrestore(&prm->lock, flags);
206 /* Pack USB load in ALSA ring buffer */
207 memcpy(dst, src, req->actual);
209 if (usb_ep_queue(ep, req, GFP_ATOMIC))
210 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
213 snd_pcm_period_elapsed(substream);
219 uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
221 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
222 struct uac2_rtd_params *prm;
226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
231 spin_lock_irqsave(&prm->lock, flags);
237 case SNDRV_PCM_TRIGGER_START:
238 case SNDRV_PCM_TRIGGER_RESUME:
241 case SNDRV_PCM_TRIGGER_STOP:
242 case SNDRV_PCM_TRIGGER_SUSPEND:
249 spin_unlock_irqrestore(&prm->lock, flags);
251 /* Clear buffer after Play stops */
252 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
253 memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
258 static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
260 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
261 struct uac2_rtd_params *prm;
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
268 return bytes_to_frames(substream->runtime, prm->hw_ptr);
271 static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
272 struct snd_pcm_hw_params *hw_params)
274 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
275 struct uac2_rtd_params *prm;
278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
283 err = snd_pcm_lib_malloc_pages(substream,
284 params_buffer_bytes(hw_params));
286 prm->dma_bytes = substream->runtime->dma_bytes;
287 prm->dma_area = substream->runtime->dma_area;
288 prm->period_size = params_period_bytes(hw_params);
294 static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
296 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
297 struct uac2_rtd_params *prm;
299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
304 prm->dma_area = NULL;
306 prm->period_size = 0;
308 return snd_pcm_lib_free_pages(substream);
311 static int uac2_pcm_open(struct snd_pcm_substream *substream)
313 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
314 struct snd_pcm_runtime *runtime = substream->runtime;
315 struct audio_dev *audio_dev;
316 struct f_uac2_opts *opts;
317 int p_ssize, c_ssize;
318 int p_srate, c_srate;
319 int p_chmask, c_chmask;
321 audio_dev = uac2_to_agdev(uac2);
322 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
323 p_ssize = opts->p_ssize;
324 c_ssize = opts->c_ssize;
325 p_srate = opts->p_srate;
326 c_srate = opts->c_srate;
327 p_chmask = opts->p_chmask;
328 c_chmask = opts->c_chmask;
330 runtime->hw = uac2_pcm_hardware;
332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
333 spin_lock_init(&uac2->p_prm.lock);
334 runtime->hw.rate_min = p_srate;
337 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
340 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
343 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
346 runtime->hw.channels_min = num_channels(p_chmask);
347 runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
348 / runtime->hw.periods_min;
350 spin_lock_init(&uac2->c_prm.lock);
351 runtime->hw.rate_min = c_srate;
354 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
357 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
360 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
363 runtime->hw.channels_min = num_channels(c_chmask);
364 runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
365 / runtime->hw.periods_min;
368 runtime->hw.rate_max = runtime->hw.rate_min;
369 runtime->hw.channels_max = runtime->hw.channels_min;
371 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
376 /* ALSA cries without these function pointers */
377 static int uac2_pcm_null(struct snd_pcm_substream *substream)
382 static struct snd_pcm_ops uac2_pcm_ops = {
383 .open = uac2_pcm_open,
384 .close = uac2_pcm_null,
385 .ioctl = snd_pcm_lib_ioctl,
386 .hw_params = uac2_pcm_hw_params,
387 .hw_free = uac2_pcm_hw_free,
388 .trigger = uac2_pcm_trigger,
389 .pointer = uac2_pcm_pointer,
390 .prepare = uac2_pcm_null,
393 static int snd_uac2_probe(struct platform_device *pdev)
395 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
396 struct snd_card *card;
398 struct audio_dev *audio_dev;
399 struct f_uac2_opts *opts;
401 int p_chmask, c_chmask;
403 audio_dev = uac2_to_agdev(uac2);
404 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
405 p_chmask = opts->p_chmask;
406 c_chmask = opts->c_chmask;
408 /* Choose any slot, with no id */
409 err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
416 * Create first PCM device
417 * Create a substream only for non-zero channel streams
419 err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
420 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
424 strcpy(pcm->name, "UAC2 PCM");
425 pcm->private_data = uac2;
429 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
430 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
432 strcpy(card->driver, "UAC2_Gadget");
433 strcpy(card->shortname, "UAC2_Gadget");
434 sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
436 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
437 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
439 err = snd_card_register(card);
441 platform_set_drvdata(pdev, card);
454 static int snd_uac2_remove(struct platform_device *pdev)
456 struct snd_card *card = platform_get_drvdata(pdev);
459 return snd_card_free(card);
464 static int alsa_uac2_init(struct audio_dev *agdev)
466 struct snd_uac2_chip *uac2 = &agdev->uac2;
469 uac2->pdrv.probe = snd_uac2_probe;
470 uac2->pdrv.remove = snd_uac2_remove;
471 uac2->pdrv.driver.name = uac2_name;
474 uac2->pdev.name = uac2_name;
476 /* Register snd_uac2 driver */
477 err = platform_driver_register(&uac2->pdrv);
481 /* Register snd_uac2 device */
482 err = platform_device_register(&uac2->pdev);
484 platform_driver_unregister(&uac2->pdrv);
489 static void alsa_uac2_exit(struct audio_dev *agdev)
491 struct snd_uac2_chip *uac2 = &agdev->uac2;
493 platform_driver_unregister(&uac2->pdrv);
494 platform_device_unregister(&uac2->pdev);
498 /* --------- USB Function Interface ------------- */
515 static char clksrc_in[8];
516 static char clksrc_out[8];
518 static struct usb_string strings_fn[] = {
519 [STR_ASSOC].s = "Source/Sink",
520 [STR_IF_CTRL].s = "Topology Control",
521 [STR_CLKSRC_IN].s = clksrc_in,
522 [STR_CLKSRC_OUT].s = clksrc_out,
523 [STR_USB_IT].s = "USBH Out",
524 [STR_IO_IT].s = "USBD Out",
525 [STR_USB_OT].s = "USBH In",
526 [STR_IO_OT].s = "USBD In",
527 [STR_AS_OUT_ALT0].s = "Playback Inactive",
528 [STR_AS_OUT_ALT1].s = "Playback Active",
529 [STR_AS_IN_ALT0].s = "Capture Inactive",
530 [STR_AS_IN_ALT1].s = "Capture Active",
534 static struct usb_gadget_strings str_fn = {
535 .language = 0x0409, /* en-us */
536 .strings = strings_fn,
539 static struct usb_gadget_strings *fn_strings[] = {
544 static struct usb_qualifier_descriptor devqual_desc = {
545 .bLength = sizeof devqual_desc,
546 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
548 .bcdUSB = cpu_to_le16(0x200),
549 .bDeviceClass = USB_CLASS_MISC,
550 .bDeviceSubClass = 0x02,
551 .bDeviceProtocol = 0x01,
552 .bNumConfigurations = 1,
556 static struct usb_interface_assoc_descriptor iad_desc = {
557 .bLength = sizeof iad_desc,
558 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
560 .bFirstInterface = 0,
561 .bInterfaceCount = 3,
562 .bFunctionClass = USB_CLASS_AUDIO,
563 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
564 .bFunctionProtocol = UAC_VERSION_2,
567 /* Audio Control Interface */
568 static struct usb_interface_descriptor std_ac_if_desc = {
569 .bLength = sizeof std_ac_if_desc,
570 .bDescriptorType = USB_DT_INTERFACE,
572 .bAlternateSetting = 0,
574 .bInterfaceClass = USB_CLASS_AUDIO,
575 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
576 .bInterfaceProtocol = UAC_VERSION_2,
579 /* Clock source for IN traffic */
580 struct uac_clock_source_descriptor in_clk_src_desc = {
581 .bLength = sizeof in_clk_src_desc,
582 .bDescriptorType = USB_DT_CS_INTERFACE,
584 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
585 .bClockID = USB_IN_CLK_ID,
586 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
587 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
591 /* Clock source for OUT traffic */
592 struct uac_clock_source_descriptor out_clk_src_desc = {
593 .bLength = sizeof out_clk_src_desc,
594 .bDescriptorType = USB_DT_CS_INTERFACE,
596 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
597 .bClockID = USB_OUT_CLK_ID,
598 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
599 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
603 /* Input Terminal for USB_OUT */
604 struct uac2_input_terminal_descriptor usb_out_it_desc = {
605 .bLength = sizeof usb_out_it_desc,
606 .bDescriptorType = USB_DT_CS_INTERFACE,
608 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
609 .bTerminalID = USB_OUT_IT_ID,
610 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
612 .bCSourceID = USB_OUT_CLK_ID,
614 .bmControls = (CONTROL_RDWR << COPY_CTRL),
617 /* Input Terminal for I/O-In */
618 struct uac2_input_terminal_descriptor io_in_it_desc = {
619 .bLength = sizeof io_in_it_desc,
620 .bDescriptorType = USB_DT_CS_INTERFACE,
622 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
623 .bTerminalID = IO_IN_IT_ID,
624 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
626 .bCSourceID = USB_IN_CLK_ID,
628 .bmControls = (CONTROL_RDWR << COPY_CTRL),
631 /* Ouput Terminal for USB_IN */
632 struct uac2_output_terminal_descriptor usb_in_ot_desc = {
633 .bLength = sizeof usb_in_ot_desc,
634 .bDescriptorType = USB_DT_CS_INTERFACE,
636 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
637 .bTerminalID = USB_IN_OT_ID,
638 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
640 .bSourceID = IO_IN_IT_ID,
641 .bCSourceID = USB_IN_CLK_ID,
642 .bmControls = (CONTROL_RDWR << COPY_CTRL),
645 /* Ouput Terminal for I/O-Out */
646 struct uac2_output_terminal_descriptor io_out_ot_desc = {
647 .bLength = sizeof io_out_ot_desc,
648 .bDescriptorType = USB_DT_CS_INTERFACE,
650 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
651 .bTerminalID = IO_OUT_OT_ID,
652 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
654 .bSourceID = USB_OUT_IT_ID,
655 .bCSourceID = USB_OUT_CLK_ID,
656 .bmControls = (CONTROL_RDWR << COPY_CTRL),
659 struct uac2_ac_header_descriptor ac_hdr_desc = {
660 .bLength = sizeof ac_hdr_desc,
661 .bDescriptorType = USB_DT_CS_INTERFACE,
663 .bDescriptorSubtype = UAC_MS_HEADER,
664 .bcdADC = cpu_to_le16(0x200),
665 .bCategory = UAC2_FUNCTION_IO_BOX,
666 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
667 + sizeof usb_out_it_desc + sizeof io_in_it_desc
668 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
672 /* Audio Streaming OUT Interface - Alt0 */
673 static struct usb_interface_descriptor std_as_out_if0_desc = {
674 .bLength = sizeof std_as_out_if0_desc,
675 .bDescriptorType = USB_DT_INTERFACE,
677 .bAlternateSetting = 0,
679 .bInterfaceClass = USB_CLASS_AUDIO,
680 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
681 .bInterfaceProtocol = UAC_VERSION_2,
684 /* Audio Streaming OUT Interface - Alt1 */
685 static struct usb_interface_descriptor std_as_out_if1_desc = {
686 .bLength = sizeof std_as_out_if1_desc,
687 .bDescriptorType = USB_DT_INTERFACE,
689 .bAlternateSetting = 1,
691 .bInterfaceClass = USB_CLASS_AUDIO,
692 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
693 .bInterfaceProtocol = UAC_VERSION_2,
696 /* Audio Stream OUT Intface Desc */
697 struct uac2_as_header_descriptor as_out_hdr_desc = {
698 .bLength = sizeof as_out_hdr_desc,
699 .bDescriptorType = USB_DT_CS_INTERFACE,
701 .bDescriptorSubtype = UAC_AS_GENERAL,
702 .bTerminalLink = USB_OUT_IT_ID,
704 .bFormatType = UAC_FORMAT_TYPE_I,
705 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
709 /* Audio USB_OUT Format */
710 struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
711 .bLength = sizeof as_out_fmt1_desc,
712 .bDescriptorType = USB_DT_CS_INTERFACE,
713 .bDescriptorSubtype = UAC_FORMAT_TYPE,
714 .bFormatType = UAC_FORMAT_TYPE_I,
717 /* STD AS ISO OUT Endpoint */
718 struct usb_endpoint_descriptor fs_epout_desc = {
719 .bLength = USB_DT_ENDPOINT_SIZE,
720 .bDescriptorType = USB_DT_ENDPOINT,
722 .bEndpointAddress = USB_DIR_OUT,
723 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
727 struct usb_endpoint_descriptor hs_epout_desc = {
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
731 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
735 /* CS AS ISO OUT Endpoint */
736 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
737 .bLength = sizeof as_iso_out_desc,
738 .bDescriptorType = USB_DT_CS_ENDPOINT,
740 .bDescriptorSubtype = UAC_EP_GENERAL,
743 .bLockDelayUnits = 0,
747 /* Audio Streaming IN Interface - Alt0 */
748 static struct usb_interface_descriptor std_as_in_if0_desc = {
749 .bLength = sizeof std_as_in_if0_desc,
750 .bDescriptorType = USB_DT_INTERFACE,
752 .bAlternateSetting = 0,
754 .bInterfaceClass = USB_CLASS_AUDIO,
755 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
756 .bInterfaceProtocol = UAC_VERSION_2,
759 /* Audio Streaming IN Interface - Alt1 */
760 static struct usb_interface_descriptor std_as_in_if1_desc = {
761 .bLength = sizeof std_as_in_if1_desc,
762 .bDescriptorType = USB_DT_INTERFACE,
764 .bAlternateSetting = 1,
766 .bInterfaceClass = USB_CLASS_AUDIO,
767 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
768 .bInterfaceProtocol = UAC_VERSION_2,
771 /* Audio Stream IN Intface Desc */
772 struct uac2_as_header_descriptor as_in_hdr_desc = {
773 .bLength = sizeof as_in_hdr_desc,
774 .bDescriptorType = USB_DT_CS_INTERFACE,
776 .bDescriptorSubtype = UAC_AS_GENERAL,
777 .bTerminalLink = USB_IN_OT_ID,
779 .bFormatType = UAC_FORMAT_TYPE_I,
780 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
784 /* Audio USB_IN Format */
785 struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
786 .bLength = sizeof as_in_fmt1_desc,
787 .bDescriptorType = USB_DT_CS_INTERFACE,
788 .bDescriptorSubtype = UAC_FORMAT_TYPE,
789 .bFormatType = UAC_FORMAT_TYPE_I,
792 /* STD AS ISO IN Endpoint */
793 struct usb_endpoint_descriptor fs_epin_desc = {
794 .bLength = USB_DT_ENDPOINT_SIZE,
795 .bDescriptorType = USB_DT_ENDPOINT,
797 .bEndpointAddress = USB_DIR_IN,
798 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
802 struct usb_endpoint_descriptor hs_epin_desc = {
803 .bLength = USB_DT_ENDPOINT_SIZE,
804 .bDescriptorType = USB_DT_ENDPOINT,
806 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
810 /* CS AS ISO IN Endpoint */
811 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
812 .bLength = sizeof as_iso_in_desc,
813 .bDescriptorType = USB_DT_CS_ENDPOINT,
815 .bDescriptorSubtype = UAC_EP_GENERAL,
818 .bLockDelayUnits = 0,
822 static struct usb_descriptor_header *fs_audio_desc[] = {
823 (struct usb_descriptor_header *)&iad_desc,
824 (struct usb_descriptor_header *)&std_ac_if_desc,
826 (struct usb_descriptor_header *)&ac_hdr_desc,
827 (struct usb_descriptor_header *)&in_clk_src_desc,
828 (struct usb_descriptor_header *)&out_clk_src_desc,
829 (struct usb_descriptor_header *)&usb_out_it_desc,
830 (struct usb_descriptor_header *)&io_in_it_desc,
831 (struct usb_descriptor_header *)&usb_in_ot_desc,
832 (struct usb_descriptor_header *)&io_out_ot_desc,
834 (struct usb_descriptor_header *)&std_as_out_if0_desc,
835 (struct usb_descriptor_header *)&std_as_out_if1_desc,
837 (struct usb_descriptor_header *)&as_out_hdr_desc,
838 (struct usb_descriptor_header *)&as_out_fmt1_desc,
839 (struct usb_descriptor_header *)&fs_epout_desc,
840 (struct usb_descriptor_header *)&as_iso_out_desc,
842 (struct usb_descriptor_header *)&std_as_in_if0_desc,
843 (struct usb_descriptor_header *)&std_as_in_if1_desc,
845 (struct usb_descriptor_header *)&as_in_hdr_desc,
846 (struct usb_descriptor_header *)&as_in_fmt1_desc,
847 (struct usb_descriptor_header *)&fs_epin_desc,
848 (struct usb_descriptor_header *)&as_iso_in_desc,
852 static struct usb_descriptor_header *hs_audio_desc[] = {
853 (struct usb_descriptor_header *)&iad_desc,
854 (struct usb_descriptor_header *)&std_ac_if_desc,
856 (struct usb_descriptor_header *)&ac_hdr_desc,
857 (struct usb_descriptor_header *)&in_clk_src_desc,
858 (struct usb_descriptor_header *)&out_clk_src_desc,
859 (struct usb_descriptor_header *)&usb_out_it_desc,
860 (struct usb_descriptor_header *)&io_in_it_desc,
861 (struct usb_descriptor_header *)&usb_in_ot_desc,
862 (struct usb_descriptor_header *)&io_out_ot_desc,
864 (struct usb_descriptor_header *)&std_as_out_if0_desc,
865 (struct usb_descriptor_header *)&std_as_out_if1_desc,
867 (struct usb_descriptor_header *)&as_out_hdr_desc,
868 (struct usb_descriptor_header *)&as_out_fmt1_desc,
869 (struct usb_descriptor_header *)&hs_epout_desc,
870 (struct usb_descriptor_header *)&as_iso_out_desc,
872 (struct usb_descriptor_header *)&std_as_in_if0_desc,
873 (struct usb_descriptor_header *)&std_as_in_if1_desc,
875 (struct usb_descriptor_header *)&as_in_hdr_desc,
876 (struct usb_descriptor_header *)&as_in_fmt1_desc,
877 (struct usb_descriptor_header *)&hs_epin_desc,
878 (struct usb_descriptor_header *)&as_iso_in_desc,
882 struct cntrl_cur_lay3 {
886 struct cntrl_range_lay3 {
894 free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
896 struct snd_uac2_chip *uac2 = prm->uac2;
899 prm->ep_enabled = false;
901 for (i = 0; i < USB_XFERS; i++) {
902 if (prm->ureq[i].req) {
903 usb_ep_dequeue(ep, prm->ureq[i].req);
904 usb_ep_free_request(ep, prm->ureq[i].req);
905 prm->ureq[i].req = NULL;
909 if (usb_ep_disable(ep))
910 dev_err(&uac2->pdev.dev,
911 "%s:%d Error!\n", __func__, __LINE__);
915 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
917 struct audio_dev *agdev = func_to_agdev(fn);
918 struct snd_uac2_chip *uac2 = &agdev->uac2;
919 struct usb_composite_dev *cdev = cfg->cdev;
920 struct usb_gadget *gadget = cdev->gadget;
921 struct uac2_rtd_params *prm;
922 struct f_uac2_opts *uac2_opts;
923 struct usb_string *us;
926 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
928 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
931 iad_desc.iFunction = us[STR_ASSOC].id;
932 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
933 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
934 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
935 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
936 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
937 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
938 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
939 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
940 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
941 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
942 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
945 /* Initialize the configurable parameters */
946 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
947 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
948 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
949 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
950 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
951 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
952 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
953 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
954 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
955 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
956 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
957 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
959 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
960 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
962 ret = usb_interface_id(cfg, fn);
964 dev_err(&uac2->pdev.dev,
965 "%s:%d Error!\n", __func__, __LINE__);
968 std_ac_if_desc.bInterfaceNumber = ret;
969 agdev->ac_intf = ret;
972 ret = usb_interface_id(cfg, fn);
974 dev_err(&uac2->pdev.dev,
975 "%s:%d Error!\n", __func__, __LINE__);
978 std_as_out_if0_desc.bInterfaceNumber = ret;
979 std_as_out_if1_desc.bInterfaceNumber = ret;
980 agdev->as_out_intf = ret;
981 agdev->as_out_alt = 0;
983 ret = usb_interface_id(cfg, fn);
985 dev_err(&uac2->pdev.dev,
986 "%s:%d Error!\n", __func__, __LINE__);
989 std_as_in_if0_desc.bInterfaceNumber = ret;
990 std_as_in_if1_desc.bInterfaceNumber = ret;
991 agdev->as_in_intf = ret;
992 agdev->as_in_alt = 0;
994 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
995 if (!agdev->out_ep) {
996 dev_err(&uac2->pdev.dev,
997 "%s:%d Error!\n", __func__, __LINE__);
1000 agdev->out_ep->driver_data = agdev;
1002 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1003 if (!agdev->in_ep) {
1004 dev_err(&uac2->pdev.dev,
1005 "%s:%d Error!\n", __func__, __LINE__);
1008 agdev->in_ep->driver_data = agdev;
1010 uac2->p_prm.uac2 = uac2;
1011 uac2->c_prm.uac2 = uac2;
1013 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1014 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
1015 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1016 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
1018 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
1022 prm = &agdev->uac2.c_prm;
1023 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1024 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1030 prm = &agdev->uac2.p_prm;
1031 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1032 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1038 ret = alsa_uac2_init(agdev);
1043 kfree(agdev->uac2.p_prm.rbuf);
1044 kfree(agdev->uac2.c_prm.rbuf);
1045 usb_free_all_descriptors(fn);
1047 agdev->in_ep->driver_data = NULL;
1049 agdev->out_ep->driver_data = NULL;
1054 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1056 struct usb_composite_dev *cdev = fn->config->cdev;
1057 struct audio_dev *agdev = func_to_agdev(fn);
1058 struct snd_uac2_chip *uac2 = &agdev->uac2;
1059 struct usb_gadget *gadget = cdev->gadget;
1060 struct usb_request *req;
1062 struct uac2_rtd_params *prm;
1065 /* No i/f has more than 2 alt settings */
1067 dev_err(&uac2->pdev.dev,
1068 "%s:%d Error!\n", __func__, __LINE__);
1072 if (intf == agdev->ac_intf) {
1073 /* Control I/f has only 1 AltSetting - 0 */
1075 dev_err(&uac2->pdev.dev,
1076 "%s:%d Error!\n", __func__, __LINE__);
1082 if (intf == agdev->as_out_intf) {
1085 config_ep_by_speed(gadget, fn, ep);
1086 agdev->as_out_alt = alt;
1087 } else if (intf == agdev->as_in_intf) {
1090 config_ep_by_speed(gadget, fn, ep);
1091 agdev->as_in_alt = alt;
1093 dev_err(&uac2->pdev.dev,
1094 "%s:%d Error!\n", __func__, __LINE__);
1103 prm->ep_enabled = true;
1106 for (i = 0; i < USB_XFERS; i++) {
1107 if (prm->ureq[i].req) {
1108 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1109 dev_err(&uac2->pdev.dev, "%d Error!\n",
1114 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1116 dev_err(&uac2->pdev.dev,
1117 "%s:%d Error!\n", __func__, __LINE__);
1121 prm->ureq[i].req = req;
1122 prm->ureq[i].pp = prm;
1125 req->context = &prm->ureq[i];
1126 req->length = prm->max_psize;
1127 req->complete = agdev_iso_complete;
1128 req->buf = prm->rbuf + i * req->length;
1130 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1131 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1138 afunc_get_alt(struct usb_function *fn, unsigned intf)
1140 struct audio_dev *agdev = func_to_agdev(fn);
1141 struct snd_uac2_chip *uac2 = &agdev->uac2;
1143 if (intf == agdev->ac_intf)
1144 return agdev->ac_alt;
1145 else if (intf == agdev->as_out_intf)
1146 return agdev->as_out_alt;
1147 else if (intf == agdev->as_in_intf)
1148 return agdev->as_in_alt;
1150 dev_err(&uac2->pdev.dev,
1151 "%s:%d Invalid Interface %d!\n",
1152 __func__, __LINE__, intf);
1158 afunc_disable(struct usb_function *fn)
1160 struct audio_dev *agdev = func_to_agdev(fn);
1161 struct snd_uac2_chip *uac2 = &agdev->uac2;
1163 free_ep(&uac2->p_prm, agdev->in_ep);
1164 agdev->as_in_alt = 0;
1166 free_ep(&uac2->c_prm, agdev->out_ep);
1167 agdev->as_out_alt = 0;
1171 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1173 struct usb_request *req = fn->config->cdev->req;
1174 struct audio_dev *agdev = func_to_agdev(fn);
1175 struct snd_uac2_chip *uac2 = &agdev->uac2;
1176 struct f_uac2_opts *opts;
1177 u16 w_length = le16_to_cpu(cr->wLength);
1178 u16 w_index = le16_to_cpu(cr->wIndex);
1179 u16 w_value = le16_to_cpu(cr->wValue);
1180 u8 entity_id = (w_index >> 8) & 0xff;
1181 u8 control_selector = w_value >> 8;
1182 int value = -EOPNOTSUPP;
1183 int p_srate, c_srate;
1185 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1186 p_srate = opts->p_srate;
1187 c_srate = opts->c_srate;
1189 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1190 struct cntrl_cur_lay3 c;
1192 if (entity_id == USB_IN_CLK_ID)
1194 else if (entity_id == USB_OUT_CLK_ID)
1197 value = min_t(unsigned, w_length, sizeof c);
1198 memcpy(req->buf, &c, value);
1199 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1200 *(u8 *)req->buf = 1;
1201 value = min_t(unsigned, w_length, 1);
1203 dev_err(&uac2->pdev.dev,
1204 "%s:%d control_selector=%d TODO!\n",
1205 __func__, __LINE__, control_selector);
1212 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1214 struct usb_request *req = fn->config->cdev->req;
1215 struct audio_dev *agdev = func_to_agdev(fn);
1216 struct snd_uac2_chip *uac2 = &agdev->uac2;
1217 struct f_uac2_opts *opts;
1218 u16 w_length = le16_to_cpu(cr->wLength);
1219 u16 w_index = le16_to_cpu(cr->wIndex);
1220 u16 w_value = le16_to_cpu(cr->wValue);
1221 u8 entity_id = (w_index >> 8) & 0xff;
1222 u8 control_selector = w_value >> 8;
1223 struct cntrl_range_lay3 r;
1224 int value = -EOPNOTSUPP;
1225 int p_srate, c_srate;
1227 opts = container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
1228 p_srate = opts->p_srate;
1229 c_srate = opts->c_srate;
1231 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1232 if (entity_id == USB_IN_CLK_ID)
1234 else if (entity_id == USB_OUT_CLK_ID)
1241 r.wNumSubRanges = 1;
1243 value = min_t(unsigned, w_length, sizeof r);
1244 memcpy(req->buf, &r, value);
1246 dev_err(&uac2->pdev.dev,
1247 "%s:%d control_selector=%d TODO!\n",
1248 __func__, __LINE__, control_selector);
1255 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1257 if (cr->bRequest == UAC2_CS_CUR)
1258 return in_rq_cur(fn, cr);
1259 else if (cr->bRequest == UAC2_CS_RANGE)
1260 return in_rq_range(fn, cr);
1266 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1268 u16 w_length = le16_to_cpu(cr->wLength);
1269 u16 w_value = le16_to_cpu(cr->wValue);
1270 u8 control_selector = w_value >> 8;
1272 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1279 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1281 struct audio_dev *agdev = func_to_agdev(fn);
1282 struct snd_uac2_chip *uac2 = &agdev->uac2;
1283 u16 w_index = le16_to_cpu(cr->wIndex);
1284 u8 intf = w_index & 0xff;
1286 if (intf != agdev->ac_intf) {
1287 dev_err(&uac2->pdev.dev,
1288 "%s:%d Error!\n", __func__, __LINE__);
1292 if (cr->bRequestType & USB_DIR_IN)
1293 return ac_rq_in(fn, cr);
1294 else if (cr->bRequest == UAC2_CS_CUR)
1295 return out_rq_cur(fn, cr);
1301 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1303 struct usb_composite_dev *cdev = fn->config->cdev;
1304 struct audio_dev *agdev = func_to_agdev(fn);
1305 struct snd_uac2_chip *uac2 = &agdev->uac2;
1306 struct usb_request *req = cdev->req;
1307 u16 w_length = le16_to_cpu(cr->wLength);
1308 int value = -EOPNOTSUPP;
1310 /* Only Class specific requests are supposed to reach here */
1311 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1314 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1315 value = setup_rq_inf(fn, cr);
1317 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1320 req->length = value;
1321 req->zero = value < w_length;
1322 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1324 dev_err(&uac2->pdev.dev,
1325 "%s:%d Error!\n", __func__, __LINE__);
1333 static void afunc_free_inst(struct usb_function_instance *f)
1335 struct f_uac2_opts *opts;
1337 opts = container_of(f, struct f_uac2_opts, func_inst);
1341 static struct usb_function_instance *afunc_alloc_inst(void)
1343 struct f_uac2_opts *opts;
1345 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1347 return ERR_PTR(-ENOMEM);
1349 opts->func_inst.free_func_inst = afunc_free_inst;
1351 return &opts->func_inst;
1354 static void afunc_free(struct usb_function *f)
1356 struct audio_dev *agdev;
1358 agdev = func_to_agdev(f);
1362 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1364 struct audio_dev *agdev = func_to_agdev(f);
1365 struct uac2_rtd_params *prm;
1367 alsa_uac2_exit(agdev);
1369 prm = &agdev->uac2.p_prm;
1372 prm = &agdev->uac2.c_prm;
1374 usb_free_all_descriptors(f);
1377 agdev->in_ep->driver_data = NULL;
1379 agdev->out_ep->driver_data = NULL;
1382 struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1384 struct audio_dev *agdev;
1385 struct f_uac2_opts *opts;
1387 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1389 return ERR_PTR(-ENOMEM);
1391 opts = container_of(fi, struct f_uac2_opts, func_inst);
1393 agdev->func.name = "uac2_func";
1394 agdev->func.bind = afunc_bind;
1395 agdev->func.unbind = afunc_unbind;
1396 agdev->func.set_alt = afunc_set_alt;
1397 agdev->func.get_alt = afunc_get_alt;
1398 agdev->func.disable = afunc_disable;
1399 agdev->func.setup = afunc_setup;
1400 agdev->func.free_func = afunc_free;
1402 return &agdev->func;
1405 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1406 MODULE_LICENSE("GPL");
1407 MODULE_AUTHOR("Yadwinder Singh");
1408 MODULE_AUTHOR("Jaswinder Singh");