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>
23 /* Playback(USB-IN) Default Stereo - Fl/Fr */
24 static int p_chmask = 0x3;
25 module_param(p_chmask, uint, S_IRUGO);
26 MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
28 /* Playback Default 48 KHz */
29 static int p_srate = 48000;
30 module_param(p_srate, uint, S_IRUGO);
31 MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
33 /* Playback Default 16bits/sample */
34 static int p_ssize = 2;
35 module_param(p_ssize, uint, S_IRUGO);
36 MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
38 /* Capture(USB-OUT) Default Stereo - Fl/Fr */
39 static int c_chmask = 0x3;
40 module_param(c_chmask, uint, S_IRUGO);
41 MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
43 /* Capture Default 64 KHz */
44 static int c_srate = 64000;
45 module_param(c_srate, uint, S_IRUGO);
46 MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
48 /* Capture Default 16bits/sample */
49 static int c_ssize = 2;
50 module_param(c_ssize, uint, S_IRUGO);
51 MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
53 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
55 #define ALT_SET(x, a) do {(x) &= ~0xff; (x) |= (a); } while (0)
56 #define ALT_GET(x) ((x) & 0xff)
57 #define INTF_SET(x, i) do {(x) &= 0xff; (x) |= ((i) << 8); } while (0)
58 #define INTF_GET(x) ((x >> 8) & 0xff)
60 /* Keep everyone on toes */
64 * The driver implements a simple UAC_2 topology.
65 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
66 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
67 * Capture and Playback sampling rates are independently
68 * controlled by two clock sources :
69 * CLK_5 := c_srate, and CLK_6 := p_srate
71 #define USB_OUT_IT_ID 1
73 #define IO_OUT_OT_ID 3
74 #define USB_IN_OT_ID 4
75 #define USB_OUT_CLK_ID 5
76 #define USB_IN_CLK_ID 6
78 #define CONTROL_ABSENT 0
79 #define CONTROL_RDONLY 1
80 #define CONTROL_RDWR 3
82 #define CLK_FREQ_CTRL 0
83 #define CLK_VLD_CTRL 2
92 const char *uac2_name = "snd_uac2";
95 struct uac2_rtd_params *pp; /* parent param */
96 struct usb_request *req;
99 struct uac2_rtd_params {
100 bool ep_enabled; /* if the ep is enabled */
101 /* Size of the ring buffer */
103 unsigned char *dma_area;
105 struct snd_pcm_substream *ss;
115 struct uac2_req ureq[USB_XFERS];
120 struct snd_uac2_chip {
121 struct platform_device pdev;
122 struct platform_driver pdrv;
124 struct uac2_rtd_params p_prm;
125 struct uac2_rtd_params c_prm;
127 struct snd_card *card;
131 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
132 #define PRD_SIZE_MAX PAGE_SIZE
133 #define MIN_PERIODS 4
135 static struct snd_pcm_hardware uac2_pcm_hardware = {
136 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
137 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
138 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
139 .rates = SNDRV_PCM_RATE_CONTINUOUS,
140 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
141 .buffer_bytes_max = BUFF_SIZE_MAX,
142 .period_bytes_max = PRD_SIZE_MAX,
143 .periods_min = MIN_PERIODS,
147 /* Currently active {Interface[15:8] | AltSettings[7:0]} */
148 __u16 ac_alt, as_out_alt, as_in_alt;
150 struct usb_ep *in_ep, *out_ep;
151 struct usb_function func;
153 /* The ALSA Sound Card it represents on the USB-Client side */
154 struct snd_uac2_chip uac2;
157 static struct audio_dev *agdev_g;
160 struct audio_dev *func_to_agdev(struct usb_function *f)
162 return container_of(f, struct audio_dev, func);
166 struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
168 return container_of(u, struct audio_dev, uac2);
172 struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
174 return container_of(p, struct snd_uac2_chip, pdev);
178 struct snd_uac2_chip *prm_to_uac2(struct uac2_rtd_params *r)
180 struct snd_uac2_chip *uac2 = container_of(r,
181 struct snd_uac2_chip, c_prm);
183 if (&uac2->c_prm != r)
184 uac2 = container_of(r, struct snd_uac2_chip, p_prm);
190 uint num_channels(uint chanmask)
195 num += (chanmask & 1);
203 agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
207 bool update_alsa = false;
208 unsigned char *src, *dst;
209 int status = req->status;
210 struct uac2_req *ur = req->context;
211 struct snd_pcm_substream *substream;
212 struct uac2_rtd_params *prm = ur->pp;
213 struct snd_uac2_chip *uac2 = prm_to_uac2(prm);
215 /* i/f shutting down */
216 if (!prm->ep_enabled)
220 * We can't really do much about bad xfers.
221 * Afterall, the ISOCH xfers could fail legitimately.
224 pr_debug("%s: iso_complete status(%d) %d/%d\n",
225 __func__, status, req->actual, req->length);
229 /* Do nothing if ALSA isn't active */
233 spin_lock_irqsave(&prm->lock, flags);
235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
236 src = prm->dma_area + prm->hw_ptr;
237 req->actual = req->length;
240 dst = prm->dma_area + prm->hw_ptr;
244 pending = prm->hw_ptr % prm->period_size;
245 pending += req->actual;
246 if (pending >= prm->period_size)
249 prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
251 spin_unlock_irqrestore(&prm->lock, flags);
253 /* Pack USB load in ALSA ring buffer */
254 memcpy(dst, src, req->actual);
256 if (usb_ep_queue(ep, req, GFP_ATOMIC))
257 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
260 snd_pcm_period_elapsed(substream);
266 uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
268 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
269 struct audio_dev *agdev = uac2_to_agdev(uac2);
270 struct uac2_rtd_params *prm;
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
283 spin_lock_irqsave(&prm->lock, flags);
289 case SNDRV_PCM_TRIGGER_START:
290 case SNDRV_PCM_TRIGGER_RESUME:
293 case SNDRV_PCM_TRIGGER_STOP:
294 case SNDRV_PCM_TRIGGER_SUSPEND:
301 spin_unlock_irqrestore(&prm->lock, flags);
303 /* Clear buffer after Play stops */
304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
305 memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
310 static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
312 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
313 struct uac2_rtd_params *prm;
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
320 return bytes_to_frames(substream->runtime, prm->hw_ptr);
323 static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
324 struct snd_pcm_hw_params *hw_params)
326 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
327 struct uac2_rtd_params *prm;
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
335 err = snd_pcm_lib_malloc_pages(substream,
336 params_buffer_bytes(hw_params));
338 prm->dma_bytes = substream->runtime->dma_bytes;
339 prm->dma_area = substream->runtime->dma_area;
340 prm->period_size = params_period_bytes(hw_params);
346 static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
348 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
349 struct uac2_rtd_params *prm;
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
356 prm->dma_area = NULL;
358 prm->period_size = 0;
360 return snd_pcm_lib_free_pages(substream);
363 static int uac2_pcm_open(struct snd_pcm_substream *substream)
365 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
366 struct snd_pcm_runtime *runtime = substream->runtime;
368 runtime->hw = uac2_pcm_hardware;
370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
371 spin_lock_init(&uac2->p_prm.lock);
372 runtime->hw.rate_min = p_srate;
373 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! p_ssize ! */
374 runtime->hw.channels_min = num_channels(p_chmask);
375 runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
376 / runtime->hw.periods_min;
378 spin_lock_init(&uac2->c_prm.lock);
379 runtime->hw.rate_min = c_srate;
380 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; /* ! c_ssize ! */
381 runtime->hw.channels_min = num_channels(c_chmask);
382 runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
383 / runtime->hw.periods_min;
386 runtime->hw.rate_max = runtime->hw.rate_min;
387 runtime->hw.channels_max = runtime->hw.channels_min;
389 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
394 /* ALSA cries without these function pointers */
395 static int uac2_pcm_null(struct snd_pcm_substream *substream)
400 static struct snd_pcm_ops uac2_pcm_ops = {
401 .open = uac2_pcm_open,
402 .close = uac2_pcm_null,
403 .ioctl = snd_pcm_lib_ioctl,
404 .hw_params = uac2_pcm_hw_params,
405 .hw_free = uac2_pcm_hw_free,
406 .trigger = uac2_pcm_trigger,
407 .pointer = uac2_pcm_pointer,
408 .prepare = uac2_pcm_null,
411 static int __devinit snd_uac2_probe(struct platform_device *pdev)
413 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
414 struct snd_card *card;
418 /* Choose any slot, with no id */
419 err = snd_card_create(-1, NULL, THIS_MODULE, 0, &card);
426 * Create first PCM device
427 * Create a substream only for non-zero channel streams
429 err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
430 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
434 strcpy(pcm->name, "UAC2 PCM");
435 pcm->private_data = uac2;
439 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
440 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
442 strcpy(card->driver, "UAC2_Gadget");
443 strcpy(card->shortname, "UAC2_Gadget");
444 sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
446 snd_card_set_dev(card, &pdev->dev);
448 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
449 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
451 err = snd_card_register(card);
453 platform_set_drvdata(pdev, card);
466 static int snd_uac2_remove(struct platform_device *pdev)
468 struct snd_card *card = platform_get_drvdata(pdev);
470 platform_set_drvdata(pdev, NULL);
473 return snd_card_free(card);
478 static int alsa_uac2_init(struct audio_dev *agdev)
480 struct snd_uac2_chip *uac2 = &agdev->uac2;
483 uac2->pdrv.probe = snd_uac2_probe;
484 uac2->pdrv.remove = snd_uac2_remove;
485 uac2->pdrv.driver.name = uac2_name;
488 uac2->pdev.name = uac2_name;
490 /* Register snd_uac2 driver */
491 err = platform_driver_register(&uac2->pdrv);
495 /* Register snd_uac2 device */
496 err = platform_device_register(&uac2->pdev);
498 platform_driver_unregister(&uac2->pdrv);
503 static void alsa_uac2_exit(struct audio_dev *agdev)
505 struct snd_uac2_chip *uac2 = &agdev->uac2;
507 platform_driver_unregister(&uac2->pdrv);
508 platform_device_unregister(&uac2->pdev);
512 /* --------- USB Function Interface ------------- */
529 static const char ifassoc[] = "Source/Sink";
530 static const char ifctrl[] = "Topology Control";
531 static char clksrc_in[8];
532 static char clksrc_out[8];
533 static const char usb_it[] = "USBH Out";
534 static const char io_it[] = "USBD Out";
535 static const char usb_ot[] = "USBH In";
536 static const char io_ot[] = "USBD In";
537 static const char out_alt0[] = "Playback Inactive";
538 static const char out_alt1[] = "Playback Active";
539 static const char in_alt0[] = "Capture Inactive";
540 static const char in_alt1[] = "Capture Active";
542 static struct usb_string strings_fn[] = {
543 [STR_ASSOC].s = ifassoc,
544 [STR_IF_CTRL].s = ifctrl,
545 [STR_CLKSRC_IN].s = clksrc_in,
546 [STR_CLKSRC_OUT].s = clksrc_out,
547 [STR_USB_IT].s = usb_it,
548 [STR_IO_IT].s = io_it,
549 [STR_USB_OT].s = usb_ot,
550 [STR_IO_OT].s = io_ot,
551 [STR_AS_OUT_ALT0].s = out_alt0,
552 [STR_AS_OUT_ALT1].s = out_alt1,
553 [STR_AS_IN_ALT0].s = in_alt0,
554 [STR_AS_IN_ALT1].s = in_alt1,
558 static struct usb_gadget_strings str_fn = {
559 .language = 0x0409, /* en-us */
560 .strings = strings_fn,
563 static struct usb_gadget_strings *fn_strings[] = {
568 static struct usb_qualifier_descriptor devqual_desc = {
569 .bLength = sizeof devqual_desc,
570 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
572 .bcdUSB = cpu_to_le16(0x200),
573 .bDeviceClass = USB_CLASS_MISC,
574 .bDeviceSubClass = 0x02,
575 .bDeviceProtocol = 0x01,
576 .bNumConfigurations = 1,
580 static struct usb_interface_assoc_descriptor iad_desc = {
581 .bLength = sizeof iad_desc,
582 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
584 .bFirstInterface = 0,
585 .bInterfaceCount = 3,
586 .bFunctionClass = USB_CLASS_AUDIO,
587 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
588 .bFunctionProtocol = UAC_VERSION_2,
591 /* Audio Control Interface */
592 static struct usb_interface_descriptor std_ac_if_desc = {
593 .bLength = sizeof std_ac_if_desc,
594 .bDescriptorType = USB_DT_INTERFACE,
596 .bAlternateSetting = 0,
598 .bInterfaceClass = USB_CLASS_AUDIO,
599 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
600 .bInterfaceProtocol = UAC_VERSION_2,
603 /* Clock source for IN traffic */
604 struct uac_clock_source_descriptor in_clk_src_desc = {
605 .bLength = sizeof in_clk_src_desc,
606 .bDescriptorType = USB_DT_CS_INTERFACE,
608 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
609 .bClockID = USB_IN_CLK_ID,
610 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
611 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
615 /* Clock source for OUT traffic */
616 struct uac_clock_source_descriptor out_clk_src_desc = {
617 .bLength = sizeof out_clk_src_desc,
618 .bDescriptorType = USB_DT_CS_INTERFACE,
620 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
621 .bClockID = USB_OUT_CLK_ID,
622 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
623 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
627 /* Input Terminal for USB_OUT */
628 struct uac2_input_terminal_descriptor usb_out_it_desc = {
629 .bLength = sizeof usb_out_it_desc,
630 .bDescriptorType = USB_DT_CS_INTERFACE,
632 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
633 .bTerminalID = USB_OUT_IT_ID,
634 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
636 .bCSourceID = USB_OUT_CLK_ID,
638 .bmControls = (CONTROL_RDWR << COPY_CTRL),
641 /* Input Terminal for I/O-In */
642 struct uac2_input_terminal_descriptor io_in_it_desc = {
643 .bLength = sizeof io_in_it_desc,
644 .bDescriptorType = USB_DT_CS_INTERFACE,
646 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
647 .bTerminalID = IO_IN_IT_ID,
648 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
650 .bCSourceID = USB_IN_CLK_ID,
652 .bmControls = (CONTROL_RDWR << COPY_CTRL),
655 /* Ouput Terminal for USB_IN */
656 struct uac2_output_terminal_descriptor usb_in_ot_desc = {
657 .bLength = sizeof usb_in_ot_desc,
658 .bDescriptorType = USB_DT_CS_INTERFACE,
660 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
661 .bTerminalID = USB_IN_OT_ID,
662 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
664 .bSourceID = IO_IN_IT_ID,
665 .bCSourceID = USB_IN_CLK_ID,
666 .bmControls = (CONTROL_RDWR << COPY_CTRL),
669 /* Ouput Terminal for I/O-Out */
670 struct uac2_output_terminal_descriptor io_out_ot_desc = {
671 .bLength = sizeof io_out_ot_desc,
672 .bDescriptorType = USB_DT_CS_INTERFACE,
674 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
675 .bTerminalID = IO_OUT_OT_ID,
676 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
678 .bSourceID = USB_OUT_IT_ID,
679 .bCSourceID = USB_OUT_CLK_ID,
680 .bmControls = (CONTROL_RDWR << COPY_CTRL),
683 struct uac2_ac_header_descriptor ac_hdr_desc = {
684 .bLength = sizeof ac_hdr_desc,
685 .bDescriptorType = USB_DT_CS_INTERFACE,
687 .bDescriptorSubtype = UAC_MS_HEADER,
688 .bcdADC = cpu_to_le16(0x200),
689 .bCategory = UAC2_FUNCTION_IO_BOX,
690 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
691 + sizeof usb_out_it_desc + sizeof io_in_it_desc
692 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
696 /* Audio Streaming OUT Interface - Alt0 */
697 static struct usb_interface_descriptor std_as_out_if0_desc = {
698 .bLength = sizeof std_as_out_if0_desc,
699 .bDescriptorType = USB_DT_INTERFACE,
701 .bAlternateSetting = 0,
703 .bInterfaceClass = USB_CLASS_AUDIO,
704 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
705 .bInterfaceProtocol = UAC_VERSION_2,
708 /* Audio Streaming OUT Interface - Alt1 */
709 static struct usb_interface_descriptor std_as_out_if1_desc = {
710 .bLength = sizeof std_as_out_if1_desc,
711 .bDescriptorType = USB_DT_INTERFACE,
713 .bAlternateSetting = 1,
715 .bInterfaceClass = USB_CLASS_AUDIO,
716 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
717 .bInterfaceProtocol = UAC_VERSION_2,
720 /* Audio Stream OUT Intface Desc */
721 struct uac2_as_header_descriptor as_out_hdr_desc = {
722 .bLength = sizeof as_out_hdr_desc,
723 .bDescriptorType = USB_DT_CS_INTERFACE,
725 .bDescriptorSubtype = UAC_AS_GENERAL,
726 .bTerminalLink = USB_OUT_IT_ID,
728 .bFormatType = UAC_FORMAT_TYPE_I,
729 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
733 /* Audio USB_OUT Format */
734 struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
735 .bLength = sizeof as_out_fmt1_desc,
736 .bDescriptorType = USB_DT_CS_INTERFACE,
737 .bDescriptorSubtype = UAC_FORMAT_TYPE,
738 .bFormatType = UAC_FORMAT_TYPE_I,
741 /* STD AS ISO OUT Endpoint */
742 struct usb_endpoint_descriptor fs_epout_desc = {
743 .bLength = USB_DT_ENDPOINT_SIZE,
744 .bDescriptorType = USB_DT_ENDPOINT,
746 .bEndpointAddress = USB_DIR_OUT,
747 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
751 struct usb_endpoint_descriptor hs_epout_desc = {
752 .bLength = USB_DT_ENDPOINT_SIZE,
753 .bDescriptorType = USB_DT_ENDPOINT,
755 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
759 /* CS AS ISO OUT Endpoint */
760 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
761 .bLength = sizeof as_iso_out_desc,
762 .bDescriptorType = USB_DT_CS_ENDPOINT,
764 .bDescriptorSubtype = UAC_EP_GENERAL,
767 .bLockDelayUnits = 0,
771 /* Audio Streaming IN Interface - Alt0 */
772 static struct usb_interface_descriptor std_as_in_if0_desc = {
773 .bLength = sizeof std_as_in_if0_desc,
774 .bDescriptorType = USB_DT_INTERFACE,
776 .bAlternateSetting = 0,
778 .bInterfaceClass = USB_CLASS_AUDIO,
779 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
780 .bInterfaceProtocol = UAC_VERSION_2,
783 /* Audio Streaming IN Interface - Alt1 */
784 static struct usb_interface_descriptor std_as_in_if1_desc = {
785 .bLength = sizeof std_as_in_if1_desc,
786 .bDescriptorType = USB_DT_INTERFACE,
788 .bAlternateSetting = 1,
790 .bInterfaceClass = USB_CLASS_AUDIO,
791 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
792 .bInterfaceProtocol = UAC_VERSION_2,
795 /* Audio Stream IN Intface Desc */
796 struct uac2_as_header_descriptor as_in_hdr_desc = {
797 .bLength = sizeof as_in_hdr_desc,
798 .bDescriptorType = USB_DT_CS_INTERFACE,
800 .bDescriptorSubtype = UAC_AS_GENERAL,
801 .bTerminalLink = USB_IN_OT_ID,
803 .bFormatType = UAC_FORMAT_TYPE_I,
804 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
808 /* Audio USB_IN Format */
809 struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
810 .bLength = sizeof as_in_fmt1_desc,
811 .bDescriptorType = USB_DT_CS_INTERFACE,
812 .bDescriptorSubtype = UAC_FORMAT_TYPE,
813 .bFormatType = UAC_FORMAT_TYPE_I,
816 /* STD AS ISO IN Endpoint */
817 struct usb_endpoint_descriptor fs_epin_desc = {
818 .bLength = USB_DT_ENDPOINT_SIZE,
819 .bDescriptorType = USB_DT_ENDPOINT,
821 .bEndpointAddress = USB_DIR_IN,
822 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
826 struct usb_endpoint_descriptor hs_epin_desc = {
827 .bLength = USB_DT_ENDPOINT_SIZE,
828 .bDescriptorType = USB_DT_ENDPOINT,
830 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
834 /* CS AS ISO IN Endpoint */
835 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
836 .bLength = sizeof as_iso_in_desc,
837 .bDescriptorType = USB_DT_CS_ENDPOINT,
839 .bDescriptorSubtype = UAC_EP_GENERAL,
842 .bLockDelayUnits = 0,
846 static struct usb_descriptor_header *fs_audio_desc[] = {
847 (struct usb_descriptor_header *)&iad_desc,
848 (struct usb_descriptor_header *)&std_ac_if_desc,
850 (struct usb_descriptor_header *)&ac_hdr_desc,
851 (struct usb_descriptor_header *)&in_clk_src_desc,
852 (struct usb_descriptor_header *)&out_clk_src_desc,
853 (struct usb_descriptor_header *)&usb_out_it_desc,
854 (struct usb_descriptor_header *)&io_in_it_desc,
855 (struct usb_descriptor_header *)&usb_in_ot_desc,
856 (struct usb_descriptor_header *)&io_out_ot_desc,
858 (struct usb_descriptor_header *)&std_as_out_if0_desc,
859 (struct usb_descriptor_header *)&std_as_out_if1_desc,
861 (struct usb_descriptor_header *)&as_out_hdr_desc,
862 (struct usb_descriptor_header *)&as_out_fmt1_desc,
863 (struct usb_descriptor_header *)&fs_epout_desc,
864 (struct usb_descriptor_header *)&as_iso_out_desc,
866 (struct usb_descriptor_header *)&std_as_in_if0_desc,
867 (struct usb_descriptor_header *)&std_as_in_if1_desc,
869 (struct usb_descriptor_header *)&as_in_hdr_desc,
870 (struct usb_descriptor_header *)&as_in_fmt1_desc,
871 (struct usb_descriptor_header *)&fs_epin_desc,
872 (struct usb_descriptor_header *)&as_iso_in_desc,
876 static struct usb_descriptor_header *hs_audio_desc[] = {
877 (struct usb_descriptor_header *)&iad_desc,
878 (struct usb_descriptor_header *)&std_ac_if_desc,
880 (struct usb_descriptor_header *)&ac_hdr_desc,
881 (struct usb_descriptor_header *)&in_clk_src_desc,
882 (struct usb_descriptor_header *)&out_clk_src_desc,
883 (struct usb_descriptor_header *)&usb_out_it_desc,
884 (struct usb_descriptor_header *)&io_in_it_desc,
885 (struct usb_descriptor_header *)&usb_in_ot_desc,
886 (struct usb_descriptor_header *)&io_out_ot_desc,
888 (struct usb_descriptor_header *)&std_as_out_if0_desc,
889 (struct usb_descriptor_header *)&std_as_out_if1_desc,
891 (struct usb_descriptor_header *)&as_out_hdr_desc,
892 (struct usb_descriptor_header *)&as_out_fmt1_desc,
893 (struct usb_descriptor_header *)&hs_epout_desc,
894 (struct usb_descriptor_header *)&as_iso_out_desc,
896 (struct usb_descriptor_header *)&std_as_in_if0_desc,
897 (struct usb_descriptor_header *)&std_as_in_if1_desc,
899 (struct usb_descriptor_header *)&as_in_hdr_desc,
900 (struct usb_descriptor_header *)&as_in_fmt1_desc,
901 (struct usb_descriptor_header *)&hs_epin_desc,
902 (struct usb_descriptor_header *)&as_iso_in_desc,
906 struct cntrl_cur_lay3 {
910 struct cntrl_range_lay3 {
918 free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
920 struct snd_uac2_chip *uac2 = prm_to_uac2(prm);
923 prm->ep_enabled = false;
925 for (i = 0; i < USB_XFERS; i++) {
926 if (prm->ureq[i].req) {
927 usb_ep_dequeue(ep, prm->ureq[i].req);
928 usb_ep_free_request(ep, prm->ureq[i].req);
929 prm->ureq[i].req = NULL;
933 if (usb_ep_disable(ep))
934 dev_err(&uac2->pdev.dev,
935 "%s:%d Error!\n", __func__, __LINE__);
939 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
941 struct audio_dev *agdev = func_to_agdev(fn);
942 struct snd_uac2_chip *uac2 = &agdev->uac2;
943 struct usb_composite_dev *cdev = cfg->cdev;
944 struct usb_gadget *gadget = cdev->gadget;
945 struct uac2_rtd_params *prm;
948 ret = usb_interface_id(cfg, fn);
950 dev_err(&uac2->pdev.dev,
951 "%s:%d Error!\n", __func__, __LINE__);
954 std_ac_if_desc.bInterfaceNumber = ret;
955 ALT_SET(agdev->ac_alt, 0);
956 INTF_SET(agdev->ac_alt, ret);
958 ret = usb_interface_id(cfg, fn);
960 dev_err(&uac2->pdev.dev,
961 "%s:%d Error!\n", __func__, __LINE__);
964 std_as_out_if0_desc.bInterfaceNumber = ret;
965 std_as_out_if1_desc.bInterfaceNumber = ret;
966 ALT_SET(agdev->as_out_alt, 0);
967 INTF_SET(agdev->as_out_alt, ret);
969 ret = usb_interface_id(cfg, fn);
971 dev_err(&uac2->pdev.dev,
972 "%s:%d Error!\n", __func__, __LINE__);
975 std_as_in_if0_desc.bInterfaceNumber = ret;
976 std_as_in_if1_desc.bInterfaceNumber = ret;
977 ALT_SET(agdev->as_in_alt, 0);
978 INTF_SET(agdev->as_in_alt, ret);
980 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
982 dev_err(&uac2->pdev.dev,
983 "%s:%d Error!\n", __func__, __LINE__);
984 agdev->out_ep->driver_data = agdev;
986 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
988 dev_err(&uac2->pdev.dev,
989 "%s:%d Error!\n", __func__, __LINE__);
990 agdev->in_ep->driver_data = agdev;
992 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
993 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
994 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
995 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
997 fn->descriptors = usb_copy_descriptors(fs_audio_desc);
998 if (gadget_is_dualspeed(gadget))
999 fn->hs_descriptors = usb_copy_descriptors(hs_audio_desc);
1001 prm = &agdev->uac2.c_prm;
1002 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1003 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1006 dev_err(&uac2->pdev.dev,
1007 "%s:%d Error!\n", __func__, __LINE__);
1010 prm = &agdev->uac2.p_prm;
1011 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1012 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1015 dev_err(&uac2->pdev.dev,
1016 "%s:%d Error!\n", __func__, __LINE__);
1019 return alsa_uac2_init(agdev);
1023 afunc_unbind(struct usb_configuration *cfg, struct usb_function *fn)
1025 struct audio_dev *agdev = func_to_agdev(fn);
1026 struct usb_composite_dev *cdev = cfg->cdev;
1027 struct usb_gadget *gadget = cdev->gadget;
1028 struct uac2_rtd_params *prm;
1030 alsa_uac2_exit(agdev);
1032 prm = &agdev->uac2.p_prm;
1035 prm = &agdev->uac2.c_prm;
1038 if (gadget_is_dualspeed(gadget))
1039 usb_free_descriptors(fn->hs_descriptors);
1040 usb_free_descriptors(fn->descriptors);
1043 agdev->in_ep->driver_data = NULL;
1045 agdev->out_ep->driver_data = NULL;
1049 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1051 struct usb_composite_dev *cdev = fn->config->cdev;
1052 struct audio_dev *agdev = func_to_agdev(fn);
1053 struct snd_uac2_chip *uac2 = &agdev->uac2;
1054 struct usb_gadget *gadget = cdev->gadget;
1055 struct usb_request *req;
1057 struct uac2_rtd_params *prm;
1060 /* No i/f has more than 2 alt settings */
1062 dev_err(&uac2->pdev.dev,
1063 "%s:%d Error!\n", __func__, __LINE__);
1067 if (intf == INTF_GET(agdev->ac_alt)) {
1068 /* Control I/f has only 1 AltSetting - 0 */
1070 dev_err(&uac2->pdev.dev,
1071 "%s:%d Error!\n", __func__, __LINE__);
1077 if (intf == INTF_GET(agdev->as_out_alt)) {
1080 config_ep_by_speed(gadget, fn, ep);
1081 ALT_SET(agdev->as_out_alt, alt);
1082 } else if (intf == INTF_GET(agdev->as_in_alt)) {
1085 config_ep_by_speed(gadget, fn, ep);
1086 ALT_SET(agdev->as_in_alt, alt);
1088 dev_err(&uac2->pdev.dev,
1089 "%s:%d Error!\n", __func__, __LINE__);
1098 prm->ep_enabled = true;
1101 for (i = 0; i < USB_XFERS; i++) {
1102 if (prm->ureq[i].req) {
1103 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1104 dev_err(&uac2->pdev.dev, "%d Error!\n",
1109 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1111 dev_err(&uac2->pdev.dev,
1112 "%s:%d Error!\n", __func__, __LINE__);
1116 prm->ureq[i].req = req;
1117 prm->ureq[i].pp = prm;
1120 req->dma = DMA_ADDR_INVALID;
1121 req->context = &prm->ureq[i];
1122 req->length = prm->max_psize;
1123 req->complete = agdev_iso_complete;
1124 req->buf = prm->rbuf + i * req->length;
1126 if (usb_ep_queue(ep, req, GFP_ATOMIC))
1127 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
1134 afunc_get_alt(struct usb_function *fn, unsigned intf)
1136 struct audio_dev *agdev = func_to_agdev(fn);
1137 struct snd_uac2_chip *uac2 = &agdev->uac2;
1139 if (intf == INTF_GET(agdev->ac_alt))
1140 return ALT_GET(agdev->ac_alt);
1141 else if (intf == INTF_GET(agdev->as_out_alt))
1142 return ALT_GET(agdev->as_out_alt);
1143 else if (intf == INTF_GET(agdev->as_in_alt))
1144 return ALT_GET(agdev->as_in_alt);
1146 dev_err(&uac2->pdev.dev,
1147 "%s:%d Invalid Interface %d!\n",
1148 __func__, __LINE__, intf);
1154 afunc_disable(struct usb_function *fn)
1156 struct audio_dev *agdev = func_to_agdev(fn);
1157 struct snd_uac2_chip *uac2 = &agdev->uac2;
1159 free_ep(&uac2->p_prm, agdev->in_ep);
1160 ALT_SET(agdev->as_in_alt, 0);
1162 free_ep(&uac2->c_prm, agdev->out_ep);
1163 ALT_SET(agdev->as_out_alt, 0);
1167 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1169 struct usb_request *req = fn->config->cdev->req;
1170 struct audio_dev *agdev = func_to_agdev(fn);
1171 struct snd_uac2_chip *uac2 = &agdev->uac2;
1172 u16 w_length = le16_to_cpu(cr->wLength);
1173 u16 w_index = le16_to_cpu(cr->wIndex);
1174 u16 w_value = le16_to_cpu(cr->wValue);
1175 u8 entity_id = (w_index >> 8) & 0xff;
1176 u8 control_selector = w_value >> 8;
1177 int value = -EOPNOTSUPP;
1179 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1180 struct cntrl_cur_lay3 c;
1182 if (entity_id == USB_IN_CLK_ID)
1184 else if (entity_id == USB_OUT_CLK_ID)
1187 value = min_t(unsigned, w_length, sizeof c);
1188 memcpy(req->buf, &c, value);
1189 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1190 *(u8 *)req->buf = 1;
1191 value = min_t(unsigned, w_length, 1);
1193 dev_err(&uac2->pdev.dev,
1194 "%s:%d control_selector=%d TODO!\n",
1195 __func__, __LINE__, control_selector);
1202 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1204 struct usb_request *req = fn->config->cdev->req;
1205 struct audio_dev *agdev = func_to_agdev(fn);
1206 struct snd_uac2_chip *uac2 = &agdev->uac2;
1207 u16 w_length = le16_to_cpu(cr->wLength);
1208 u16 w_index = le16_to_cpu(cr->wIndex);
1209 u16 w_value = le16_to_cpu(cr->wValue);
1210 u8 entity_id = (w_index >> 8) & 0xff;
1211 u8 control_selector = w_value >> 8;
1212 struct cntrl_range_lay3 r;
1213 int value = -EOPNOTSUPP;
1215 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1216 if (entity_id == USB_IN_CLK_ID)
1218 else if (entity_id == USB_OUT_CLK_ID)
1225 r.wNumSubRanges = 1;
1227 value = min_t(unsigned, w_length, sizeof r);
1228 memcpy(req->buf, &r, value);
1230 dev_err(&uac2->pdev.dev,
1231 "%s:%d control_selector=%d TODO!\n",
1232 __func__, __LINE__, control_selector);
1239 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1241 if (cr->bRequest == UAC2_CS_CUR)
1242 return in_rq_cur(fn, cr);
1243 else if (cr->bRequest == UAC2_CS_RANGE)
1244 return in_rq_range(fn, cr);
1250 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1252 u16 w_length = le16_to_cpu(cr->wLength);
1253 u16 w_value = le16_to_cpu(cr->wValue);
1254 u8 control_selector = w_value >> 8;
1256 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1263 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1265 struct audio_dev *agdev = func_to_agdev(fn);
1266 struct snd_uac2_chip *uac2 = &agdev->uac2;
1267 u16 w_index = le16_to_cpu(cr->wIndex);
1268 u8 intf = w_index & 0xff;
1270 if (intf != INTF_GET(agdev->ac_alt)) {
1271 dev_err(&uac2->pdev.dev,
1272 "%s:%d Error!\n", __func__, __LINE__);
1276 if (cr->bRequestType & USB_DIR_IN)
1277 return ac_rq_in(fn, cr);
1278 else if (cr->bRequest == UAC2_CS_CUR)
1279 return out_rq_cur(fn, cr);
1285 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1287 struct usb_composite_dev *cdev = fn->config->cdev;
1288 struct audio_dev *agdev = func_to_agdev(fn);
1289 struct snd_uac2_chip *uac2 = &agdev->uac2;
1290 struct usb_request *req = cdev->req;
1291 u16 w_length = le16_to_cpu(cr->wLength);
1292 int value = -EOPNOTSUPP;
1294 /* Only Class specific requests are supposed to reach here */
1295 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1298 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1299 value = setup_rq_inf(fn, cr);
1301 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1304 req->length = value;
1305 req->zero = value < w_length;
1306 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1308 dev_err(&uac2->pdev.dev,
1309 "%s:%d Error!\n", __func__, __LINE__);
1317 static int audio_bind_config(struct usb_configuration *cfg)
1321 agdev_g = kzalloc(sizeof *agdev_g, GFP_KERNEL);
1322 if (agdev_g == NULL) {
1323 printk(KERN_ERR "Unable to allocate audio gadget\n");
1327 id = usb_string_id(cfg->cdev);
1331 strings_fn[STR_ASSOC].id = id;
1332 iad_desc.iFunction = id,
1334 id = usb_string_id(cfg->cdev);
1338 strings_fn[STR_IF_CTRL].id = id;
1339 std_ac_if_desc.iInterface = id,
1341 id = usb_string_id(cfg->cdev);
1345 strings_fn[STR_CLKSRC_IN].id = id;
1346 in_clk_src_desc.iClockSource = id,
1348 id = usb_string_id(cfg->cdev);
1352 strings_fn[STR_CLKSRC_OUT].id = id;
1353 out_clk_src_desc.iClockSource = id,
1355 id = usb_string_id(cfg->cdev);
1359 strings_fn[STR_USB_IT].id = id;
1360 usb_out_it_desc.iTerminal = id,
1362 id = usb_string_id(cfg->cdev);
1366 strings_fn[STR_IO_IT].id = id;
1367 io_in_it_desc.iTerminal = id;
1369 id = usb_string_id(cfg->cdev);
1373 strings_fn[STR_USB_OT].id = id;
1374 usb_in_ot_desc.iTerminal = id;
1376 id = usb_string_id(cfg->cdev);
1380 strings_fn[STR_IO_OT].id = id;
1381 io_out_ot_desc.iTerminal = id;
1383 id = usb_string_id(cfg->cdev);
1387 strings_fn[STR_AS_OUT_ALT0].id = id;
1388 std_as_out_if0_desc.iInterface = id;
1390 id = usb_string_id(cfg->cdev);
1394 strings_fn[STR_AS_OUT_ALT1].id = id;
1395 std_as_out_if1_desc.iInterface = id;
1397 id = usb_string_id(cfg->cdev);
1401 strings_fn[STR_AS_IN_ALT0].id = id;
1402 std_as_in_if0_desc.iInterface = id;
1404 id = usb_string_id(cfg->cdev);
1408 strings_fn[STR_AS_IN_ALT1].id = id;
1409 std_as_in_if1_desc.iInterface = id;
1411 agdev_g->func.name = "uac2_func";
1412 agdev_g->func.strings = fn_strings;
1413 agdev_g->func.bind = afunc_bind;
1414 agdev_g->func.unbind = afunc_unbind;
1415 agdev_g->func.set_alt = afunc_set_alt;
1416 agdev_g->func.get_alt = afunc_get_alt;
1417 agdev_g->func.disable = afunc_disable;
1418 agdev_g->func.setup = afunc_setup;
1420 /* Initialize the configurable parameters */
1421 usb_out_it_desc.bNrChannels = num_channels(c_chmask);
1422 usb_out_it_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1423 io_in_it_desc.bNrChannels = num_channels(p_chmask);
1424 io_in_it_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1425 as_out_hdr_desc.bNrChannels = num_channels(c_chmask);
1426 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(c_chmask);
1427 as_in_hdr_desc.bNrChannels = num_channels(p_chmask);
1428 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(p_chmask);
1429 as_out_fmt1_desc.bSubslotSize = c_ssize;
1430 as_out_fmt1_desc.bBitResolution = c_ssize * 8;
1431 as_in_fmt1_desc.bSubslotSize = p_ssize;
1432 as_in_fmt1_desc.bBitResolution = p_ssize * 8;
1434 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", p_srate);
1435 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", c_srate);
1437 res = usb_add_function(cfg, &agdev_g->func);
1445 uac2_unbind_config(struct usb_configuration *cfg)