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 static 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;
96 /* timekeeping for the playback endpoint */
97 unsigned int p_interval;
98 unsigned int p_residue;
100 /* pre-calculated values for playback iso completion */
101 unsigned int p_pktsize;
102 unsigned int p_pktsize_residue;
103 unsigned int p_framesize;
106 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
107 #define PRD_SIZE_MAX PAGE_SIZE
108 #define MIN_PERIODS 4
110 static struct snd_pcm_hardware uac2_pcm_hardware = {
111 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
112 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
113 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
114 .rates = SNDRV_PCM_RATE_CONTINUOUS,
115 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
116 .buffer_bytes_max = BUFF_SIZE_MAX,
117 .period_bytes_max = PRD_SIZE_MAX,
118 .periods_min = MIN_PERIODS,
123 u8 as_out_intf, as_out_alt;
124 u8 as_in_intf, as_in_alt;
126 struct usb_ep *in_ep, *out_ep;
127 struct usb_function func;
129 /* The ALSA Sound Card it represents on the USB-Client side */
130 struct snd_uac2_chip uac2;
134 struct audio_dev *func_to_agdev(struct usb_function *f)
136 return container_of(f, struct audio_dev, func);
140 struct audio_dev *uac2_to_agdev(struct snd_uac2_chip *u)
142 return container_of(u, struct audio_dev, uac2);
146 struct snd_uac2_chip *pdev_to_uac2(struct platform_device *p)
148 return container_of(p, struct snd_uac2_chip, pdev);
152 struct f_uac2_opts *agdev_to_uac2_opts(struct audio_dev *agdev)
154 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
158 uint num_channels(uint chanmask)
163 num += (chanmask & 1);
171 agdev_iso_complete(struct usb_ep *ep, struct usb_request *req)
176 bool update_alsa = false;
177 int status = req->status;
178 struct uac2_req *ur = req->context;
179 struct snd_pcm_substream *substream;
180 struct uac2_rtd_params *prm = ur->pp;
181 struct snd_uac2_chip *uac2 = prm->uac2;
183 /* i/f shutting down */
184 if (!prm->ep_enabled || req->status == -ESHUTDOWN)
188 * We can't really do much about bad xfers.
189 * Afterall, the ISOCH xfers could fail legitimately.
192 pr_debug("%s: iso_complete status(%d) %d/%d\n",
193 __func__, status, req->actual, req->length);
197 /* Do nothing if ALSA isn't active */
201 spin_lock_irqsave(&prm->lock, flags);
203 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
205 * For each IN packet, take the quotient of the current data
206 * rate and the endpoint's interval as the base packet size.
207 * If there is a residue from this division, add it to the
208 * residue accumulator.
210 req->length = uac2->p_pktsize;
211 uac2->p_residue += uac2->p_pktsize_residue;
214 * Whenever there are more bytes in the accumulator than we
215 * need to add one more sample frame, increase this packet's
216 * size and decrease the accumulator.
218 if (uac2->p_residue / uac2->p_interval >= uac2->p_framesize) {
219 req->length += uac2->p_framesize;
220 uac2->p_residue -= uac2->p_framesize *
224 req->actual = req->length;
227 pending = prm->hw_ptr % prm->period_size;
228 pending += req->actual;
229 if (pending >= prm->period_size)
232 hw_ptr = prm->hw_ptr;
233 prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
235 spin_unlock_irqrestore(&prm->lock, flags);
237 /* Pack USB load in ALSA ring buffer */
238 pending = prm->dma_bytes - hw_ptr;
240 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
241 if (unlikely(pending < req->actual)) {
242 memcpy(req->buf, prm->dma_area + hw_ptr, pending);
243 memcpy(req->buf + pending, prm->dma_area,
244 req->actual - pending);
246 memcpy(req->buf, prm->dma_area + hw_ptr, req->actual);
249 if (unlikely(pending < req->actual)) {
250 memcpy(prm->dma_area + hw_ptr, req->buf, pending);
251 memcpy(prm->dma_area, req->buf + pending,
252 req->actual - pending);
254 memcpy(prm->dma_area + hw_ptr, req->buf, req->actual);
259 if (usb_ep_queue(ep, req, GFP_ATOMIC))
260 dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);
263 snd_pcm_period_elapsed(substream);
269 uac2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
271 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
272 struct uac2_rtd_params *prm;
276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
281 spin_lock_irqsave(&prm->lock, flags);
287 case SNDRV_PCM_TRIGGER_START:
288 case SNDRV_PCM_TRIGGER_RESUME:
291 case SNDRV_PCM_TRIGGER_STOP:
292 case SNDRV_PCM_TRIGGER_SUSPEND:
299 spin_unlock_irqrestore(&prm->lock, flags);
301 /* Clear buffer after Play stops */
302 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
303 memset(prm->rbuf, 0, prm->max_psize * USB_XFERS);
308 static snd_pcm_uframes_t uac2_pcm_pointer(struct snd_pcm_substream *substream)
310 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
311 struct uac2_rtd_params *prm;
313 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
318 return bytes_to_frames(substream->runtime, prm->hw_ptr);
321 static int uac2_pcm_hw_params(struct snd_pcm_substream *substream,
322 struct snd_pcm_hw_params *hw_params)
324 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
325 struct uac2_rtd_params *prm;
328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
333 err = snd_pcm_lib_malloc_pages(substream,
334 params_buffer_bytes(hw_params));
336 prm->dma_bytes = substream->runtime->dma_bytes;
337 prm->dma_area = substream->runtime->dma_area;
338 prm->period_size = params_period_bytes(hw_params);
344 static int uac2_pcm_hw_free(struct snd_pcm_substream *substream)
346 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
347 struct uac2_rtd_params *prm;
349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
354 prm->dma_area = NULL;
356 prm->period_size = 0;
358 return snd_pcm_lib_free_pages(substream);
361 static int uac2_pcm_open(struct snd_pcm_substream *substream)
363 struct snd_uac2_chip *uac2 = snd_pcm_substream_chip(substream);
364 struct snd_pcm_runtime *runtime = substream->runtime;
365 struct audio_dev *audio_dev;
366 struct f_uac2_opts *opts;
367 int p_ssize, c_ssize;
368 int p_srate, c_srate;
369 int p_chmask, c_chmask;
371 audio_dev = uac2_to_agdev(uac2);
372 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
373 p_ssize = opts->p_ssize;
374 c_ssize = opts->c_ssize;
375 p_srate = opts->p_srate;
376 c_srate = opts->c_srate;
377 p_chmask = opts->p_chmask;
378 c_chmask = opts->c_chmask;
381 runtime->hw = uac2_pcm_hardware;
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
384 spin_lock_init(&uac2->p_prm.lock);
385 runtime->hw.rate_min = p_srate;
388 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
391 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
394 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
397 runtime->hw.channels_min = num_channels(p_chmask);
398 runtime->hw.period_bytes_min = 2 * uac2->p_prm.max_psize
399 / runtime->hw.periods_min;
401 spin_lock_init(&uac2->c_prm.lock);
402 runtime->hw.rate_min = c_srate;
405 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
408 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
411 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
414 runtime->hw.channels_min = num_channels(c_chmask);
415 runtime->hw.period_bytes_min = 2 * uac2->c_prm.max_psize
416 / runtime->hw.periods_min;
419 runtime->hw.rate_max = runtime->hw.rate_min;
420 runtime->hw.channels_max = runtime->hw.channels_min;
422 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
427 /* ALSA cries without these function pointers */
428 static int uac2_pcm_null(struct snd_pcm_substream *substream)
433 static struct snd_pcm_ops uac2_pcm_ops = {
434 .open = uac2_pcm_open,
435 .close = uac2_pcm_null,
436 .ioctl = snd_pcm_lib_ioctl,
437 .hw_params = uac2_pcm_hw_params,
438 .hw_free = uac2_pcm_hw_free,
439 .trigger = uac2_pcm_trigger,
440 .pointer = uac2_pcm_pointer,
441 .prepare = uac2_pcm_null,
444 static int snd_uac2_probe(struct platform_device *pdev)
446 struct snd_uac2_chip *uac2 = pdev_to_uac2(pdev);
447 struct snd_card *card;
449 struct audio_dev *audio_dev;
450 struct f_uac2_opts *opts;
452 int p_chmask, c_chmask;
454 audio_dev = uac2_to_agdev(uac2);
455 opts = container_of(audio_dev->func.fi, struct f_uac2_opts, func_inst);
456 p_chmask = opts->p_chmask;
457 c_chmask = opts->c_chmask;
459 /* Choose any slot, with no id */
460 err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
467 * Create first PCM device
468 * Create a substream only for non-zero channel streams
470 err = snd_pcm_new(uac2->card, "UAC2 PCM", 0,
471 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
475 strcpy(pcm->name, "UAC2 PCM");
476 pcm->private_data = uac2;
480 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac2_pcm_ops);
481 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac2_pcm_ops);
483 strcpy(card->driver, "UAC2_Gadget");
484 strcpy(card->shortname, "UAC2_Gadget");
485 sprintf(card->longname, "UAC2_Gadget %i", pdev->id);
487 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
488 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
490 err = snd_card_register(card);
492 platform_set_drvdata(pdev, card);
505 static int snd_uac2_remove(struct platform_device *pdev)
507 struct snd_card *card = platform_get_drvdata(pdev);
510 return snd_card_free(card);
515 static void snd_uac2_release(struct device *dev)
517 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
520 static int alsa_uac2_init(struct audio_dev *agdev)
522 struct snd_uac2_chip *uac2 = &agdev->uac2;
525 uac2->pdrv.probe = snd_uac2_probe;
526 uac2->pdrv.remove = snd_uac2_remove;
527 uac2->pdrv.driver.name = uac2_name;
530 uac2->pdev.name = uac2_name;
531 uac2->pdev.dev.release = snd_uac2_release;
533 /* Register snd_uac2 driver */
534 err = platform_driver_register(&uac2->pdrv);
538 /* Register snd_uac2 device */
539 err = platform_device_register(&uac2->pdev);
541 platform_driver_unregister(&uac2->pdrv);
546 static void alsa_uac2_exit(struct audio_dev *agdev)
548 struct snd_uac2_chip *uac2 = &agdev->uac2;
550 platform_driver_unregister(&uac2->pdrv);
551 platform_device_unregister(&uac2->pdev);
555 /* --------- USB Function Interface ------------- */
572 static char clksrc_in[8];
573 static char clksrc_out[8];
575 static struct usb_string strings_fn[] = {
576 [STR_ASSOC].s = "Source/Sink",
577 [STR_IF_CTRL].s = "Topology Control",
578 [STR_CLKSRC_IN].s = clksrc_in,
579 [STR_CLKSRC_OUT].s = clksrc_out,
580 [STR_USB_IT].s = "USBH Out",
581 [STR_IO_IT].s = "USBD Out",
582 [STR_USB_OT].s = "USBH In",
583 [STR_IO_OT].s = "USBD In",
584 [STR_AS_OUT_ALT0].s = "Playback Inactive",
585 [STR_AS_OUT_ALT1].s = "Playback Active",
586 [STR_AS_IN_ALT0].s = "Capture Inactive",
587 [STR_AS_IN_ALT1].s = "Capture Active",
591 static struct usb_gadget_strings str_fn = {
592 .language = 0x0409, /* en-us */
593 .strings = strings_fn,
596 static struct usb_gadget_strings *fn_strings[] = {
601 static struct usb_interface_assoc_descriptor iad_desc = {
602 .bLength = sizeof iad_desc,
603 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
605 .bFirstInterface = 0,
606 .bInterfaceCount = 3,
607 .bFunctionClass = USB_CLASS_AUDIO,
608 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
609 .bFunctionProtocol = UAC_VERSION_2,
612 /* Audio Control Interface */
613 static struct usb_interface_descriptor std_ac_if_desc = {
614 .bLength = sizeof std_ac_if_desc,
615 .bDescriptorType = USB_DT_INTERFACE,
617 .bAlternateSetting = 0,
619 .bInterfaceClass = USB_CLASS_AUDIO,
620 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
621 .bInterfaceProtocol = UAC_VERSION_2,
624 /* Clock source for IN traffic */
625 static struct uac_clock_source_descriptor in_clk_src_desc = {
626 .bLength = sizeof in_clk_src_desc,
627 .bDescriptorType = USB_DT_CS_INTERFACE,
629 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
630 .bClockID = USB_IN_CLK_ID,
631 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
632 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
636 /* Clock source for OUT traffic */
637 static struct uac_clock_source_descriptor out_clk_src_desc = {
638 .bLength = sizeof out_clk_src_desc,
639 .bDescriptorType = USB_DT_CS_INTERFACE,
641 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
642 .bClockID = USB_OUT_CLK_ID,
643 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
644 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
648 /* Input Terminal for USB_OUT */
649 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
650 .bLength = sizeof usb_out_it_desc,
651 .bDescriptorType = USB_DT_CS_INTERFACE,
653 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
654 .bTerminalID = USB_OUT_IT_ID,
655 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
657 .bCSourceID = USB_OUT_CLK_ID,
659 .bmControls = (CONTROL_RDWR << COPY_CTRL),
662 /* Input Terminal for I/O-In */
663 static struct uac2_input_terminal_descriptor io_in_it_desc = {
664 .bLength = sizeof io_in_it_desc,
665 .bDescriptorType = USB_DT_CS_INTERFACE,
667 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
668 .bTerminalID = IO_IN_IT_ID,
669 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
671 .bCSourceID = USB_IN_CLK_ID,
673 .bmControls = (CONTROL_RDWR << COPY_CTRL),
676 /* Ouput Terminal for USB_IN */
677 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
678 .bLength = sizeof usb_in_ot_desc,
679 .bDescriptorType = USB_DT_CS_INTERFACE,
681 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
682 .bTerminalID = USB_IN_OT_ID,
683 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
685 .bSourceID = IO_IN_IT_ID,
686 .bCSourceID = USB_IN_CLK_ID,
687 .bmControls = (CONTROL_RDWR << COPY_CTRL),
690 /* Ouput Terminal for I/O-Out */
691 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
692 .bLength = sizeof io_out_ot_desc,
693 .bDescriptorType = USB_DT_CS_INTERFACE,
695 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
696 .bTerminalID = IO_OUT_OT_ID,
697 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
699 .bSourceID = USB_OUT_IT_ID,
700 .bCSourceID = USB_OUT_CLK_ID,
701 .bmControls = (CONTROL_RDWR << COPY_CTRL),
704 static struct uac2_ac_header_descriptor ac_hdr_desc = {
705 .bLength = sizeof ac_hdr_desc,
706 .bDescriptorType = USB_DT_CS_INTERFACE,
708 .bDescriptorSubtype = UAC_MS_HEADER,
709 .bcdADC = cpu_to_le16(0x200),
710 .bCategory = UAC2_FUNCTION_IO_BOX,
711 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
712 + sizeof usb_out_it_desc + sizeof io_in_it_desc
713 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
717 /* Audio Streaming OUT Interface - Alt0 */
718 static struct usb_interface_descriptor std_as_out_if0_desc = {
719 .bLength = sizeof std_as_out_if0_desc,
720 .bDescriptorType = USB_DT_INTERFACE,
722 .bAlternateSetting = 0,
724 .bInterfaceClass = USB_CLASS_AUDIO,
725 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
726 .bInterfaceProtocol = UAC_VERSION_2,
729 /* Audio Streaming OUT Interface - Alt1 */
730 static struct usb_interface_descriptor std_as_out_if1_desc = {
731 .bLength = sizeof std_as_out_if1_desc,
732 .bDescriptorType = USB_DT_INTERFACE,
734 .bAlternateSetting = 1,
736 .bInterfaceClass = USB_CLASS_AUDIO,
737 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
738 .bInterfaceProtocol = UAC_VERSION_2,
741 /* Audio Stream OUT Intface Desc */
742 static struct uac2_as_header_descriptor as_out_hdr_desc = {
743 .bLength = sizeof as_out_hdr_desc,
744 .bDescriptorType = USB_DT_CS_INTERFACE,
746 .bDescriptorSubtype = UAC_AS_GENERAL,
747 .bTerminalLink = USB_OUT_IT_ID,
749 .bFormatType = UAC_FORMAT_TYPE_I,
750 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
754 /* Audio USB_OUT Format */
755 static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
756 .bLength = sizeof as_out_fmt1_desc,
757 .bDescriptorType = USB_DT_CS_INTERFACE,
758 .bDescriptorSubtype = UAC_FORMAT_TYPE,
759 .bFormatType = UAC_FORMAT_TYPE_I,
762 /* STD AS ISO OUT Endpoint */
763 static struct usb_endpoint_descriptor fs_epout_desc = {
764 .bLength = USB_DT_ENDPOINT_SIZE,
765 .bDescriptorType = USB_DT_ENDPOINT,
767 .bEndpointAddress = USB_DIR_OUT,
768 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
769 /* .wMaxPacketSize = DYNAMIC */
773 static struct usb_endpoint_descriptor hs_epout_desc = {
774 .bLength = USB_DT_ENDPOINT_SIZE,
775 .bDescriptorType = USB_DT_ENDPOINT,
777 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
778 /* .wMaxPacketSize = DYNAMIC */
782 /* CS AS ISO OUT Endpoint */
783 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
784 .bLength = sizeof as_iso_out_desc,
785 .bDescriptorType = USB_DT_CS_ENDPOINT,
787 .bDescriptorSubtype = UAC_EP_GENERAL,
790 .bLockDelayUnits = 0,
794 /* Audio Streaming IN Interface - Alt0 */
795 static struct usb_interface_descriptor std_as_in_if0_desc = {
796 .bLength = sizeof std_as_in_if0_desc,
797 .bDescriptorType = USB_DT_INTERFACE,
799 .bAlternateSetting = 0,
801 .bInterfaceClass = USB_CLASS_AUDIO,
802 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
803 .bInterfaceProtocol = UAC_VERSION_2,
806 /* Audio Streaming IN Interface - Alt1 */
807 static struct usb_interface_descriptor std_as_in_if1_desc = {
808 .bLength = sizeof std_as_in_if1_desc,
809 .bDescriptorType = USB_DT_INTERFACE,
811 .bAlternateSetting = 1,
813 .bInterfaceClass = USB_CLASS_AUDIO,
814 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
815 .bInterfaceProtocol = UAC_VERSION_2,
818 /* Audio Stream IN Intface Desc */
819 static struct uac2_as_header_descriptor as_in_hdr_desc = {
820 .bLength = sizeof as_in_hdr_desc,
821 .bDescriptorType = USB_DT_CS_INTERFACE,
823 .bDescriptorSubtype = UAC_AS_GENERAL,
824 .bTerminalLink = USB_IN_OT_ID,
826 .bFormatType = UAC_FORMAT_TYPE_I,
827 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
831 /* Audio USB_IN Format */
832 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
833 .bLength = sizeof as_in_fmt1_desc,
834 .bDescriptorType = USB_DT_CS_INTERFACE,
835 .bDescriptorSubtype = UAC_FORMAT_TYPE,
836 .bFormatType = UAC_FORMAT_TYPE_I,
839 /* STD AS ISO IN Endpoint */
840 static struct usb_endpoint_descriptor fs_epin_desc = {
841 .bLength = USB_DT_ENDPOINT_SIZE,
842 .bDescriptorType = USB_DT_ENDPOINT,
844 .bEndpointAddress = USB_DIR_IN,
845 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
846 /* .wMaxPacketSize = DYNAMIC */
850 static struct usb_endpoint_descriptor hs_epin_desc = {
851 .bLength = USB_DT_ENDPOINT_SIZE,
852 .bDescriptorType = USB_DT_ENDPOINT,
854 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
855 /* .wMaxPacketSize = DYNAMIC */
859 /* CS AS ISO IN Endpoint */
860 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
861 .bLength = sizeof as_iso_in_desc,
862 .bDescriptorType = USB_DT_CS_ENDPOINT,
864 .bDescriptorSubtype = UAC_EP_GENERAL,
867 .bLockDelayUnits = 0,
871 static struct usb_descriptor_header *fs_audio_desc[] = {
872 (struct usb_descriptor_header *)&iad_desc,
873 (struct usb_descriptor_header *)&std_ac_if_desc,
875 (struct usb_descriptor_header *)&ac_hdr_desc,
876 (struct usb_descriptor_header *)&in_clk_src_desc,
877 (struct usb_descriptor_header *)&out_clk_src_desc,
878 (struct usb_descriptor_header *)&usb_out_it_desc,
879 (struct usb_descriptor_header *)&io_in_it_desc,
880 (struct usb_descriptor_header *)&usb_in_ot_desc,
881 (struct usb_descriptor_header *)&io_out_ot_desc,
883 (struct usb_descriptor_header *)&std_as_out_if0_desc,
884 (struct usb_descriptor_header *)&std_as_out_if1_desc,
886 (struct usb_descriptor_header *)&as_out_hdr_desc,
887 (struct usb_descriptor_header *)&as_out_fmt1_desc,
888 (struct usb_descriptor_header *)&fs_epout_desc,
889 (struct usb_descriptor_header *)&as_iso_out_desc,
891 (struct usb_descriptor_header *)&std_as_in_if0_desc,
892 (struct usb_descriptor_header *)&std_as_in_if1_desc,
894 (struct usb_descriptor_header *)&as_in_hdr_desc,
895 (struct usb_descriptor_header *)&as_in_fmt1_desc,
896 (struct usb_descriptor_header *)&fs_epin_desc,
897 (struct usb_descriptor_header *)&as_iso_in_desc,
901 static struct usb_descriptor_header *hs_audio_desc[] = {
902 (struct usb_descriptor_header *)&iad_desc,
903 (struct usb_descriptor_header *)&std_ac_if_desc,
905 (struct usb_descriptor_header *)&ac_hdr_desc,
906 (struct usb_descriptor_header *)&in_clk_src_desc,
907 (struct usb_descriptor_header *)&out_clk_src_desc,
908 (struct usb_descriptor_header *)&usb_out_it_desc,
909 (struct usb_descriptor_header *)&io_in_it_desc,
910 (struct usb_descriptor_header *)&usb_in_ot_desc,
911 (struct usb_descriptor_header *)&io_out_ot_desc,
913 (struct usb_descriptor_header *)&std_as_out_if0_desc,
914 (struct usb_descriptor_header *)&std_as_out_if1_desc,
916 (struct usb_descriptor_header *)&as_out_hdr_desc,
917 (struct usb_descriptor_header *)&as_out_fmt1_desc,
918 (struct usb_descriptor_header *)&hs_epout_desc,
919 (struct usb_descriptor_header *)&as_iso_out_desc,
921 (struct usb_descriptor_header *)&std_as_in_if0_desc,
922 (struct usb_descriptor_header *)&std_as_in_if1_desc,
924 (struct usb_descriptor_header *)&as_in_hdr_desc,
925 (struct usb_descriptor_header *)&as_in_fmt1_desc,
926 (struct usb_descriptor_header *)&hs_epin_desc,
927 (struct usb_descriptor_header *)&as_iso_in_desc,
931 struct cntrl_cur_lay3 {
935 struct cntrl_range_lay3 {
936 __le16 wNumSubRanges;
943 free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
945 struct snd_uac2_chip *uac2 = prm->uac2;
948 if (!prm->ep_enabled)
951 prm->ep_enabled = false;
953 for (i = 0; i < USB_XFERS; i++) {
954 if (prm->ureq[i].req) {
955 usb_ep_dequeue(ep, prm->ureq[i].req);
956 usb_ep_free_request(ep, prm->ureq[i].req);
957 prm->ureq[i].req = NULL;
961 if (usb_ep_disable(ep))
962 dev_err(&uac2->pdev.dev,
963 "%s:%d Error!\n", __func__, __LINE__);
966 static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
967 struct usb_endpoint_descriptor *ep_desc,
968 enum usb_device_speed speed, bool is_playback)
970 int chmask, srate, ssize;
971 u16 max_size_bw, max_size_ep;
990 chmask = uac2_opts->p_chmask;
991 srate = uac2_opts->p_srate;
992 ssize = uac2_opts->p_ssize;
994 chmask = uac2_opts->c_chmask;
995 srate = uac2_opts->c_srate;
996 ssize = uac2_opts->c_ssize;
999 max_size_bw = num_channels(chmask) * ssize *
1000 ((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1);
1001 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw,
1008 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
1010 struct audio_dev *agdev = func_to_agdev(fn);
1011 struct snd_uac2_chip *uac2 = &agdev->uac2;
1012 struct usb_composite_dev *cdev = cfg->cdev;
1013 struct usb_gadget *gadget = cdev->gadget;
1014 struct device *dev = &uac2->pdev.dev;
1015 struct uac2_rtd_params *prm;
1016 struct f_uac2_opts *uac2_opts;
1017 struct usb_string *us;
1020 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
1022 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
1025 iad_desc.iFunction = us[STR_ASSOC].id;
1026 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
1027 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
1028 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
1029 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
1030 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
1031 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
1032 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
1033 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
1034 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
1035 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
1036 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
1039 /* Initialize the configurable parameters */
1040 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1041 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1042 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1043 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1044 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1045 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1046 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1047 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1048 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
1049 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
1050 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
1051 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
1053 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
1054 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
1056 ret = usb_interface_id(cfg, fn);
1058 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1061 iad_desc.bFirstInterface = ret;
1063 std_ac_if_desc.bInterfaceNumber = ret;
1064 agdev->ac_intf = ret;
1067 ret = usb_interface_id(cfg, fn);
1069 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1072 std_as_out_if0_desc.bInterfaceNumber = ret;
1073 std_as_out_if1_desc.bInterfaceNumber = ret;
1074 agdev->as_out_intf = ret;
1075 agdev->as_out_alt = 0;
1077 ret = usb_interface_id(cfg, fn);
1079 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1082 std_as_in_if0_desc.bInterfaceNumber = ret;
1083 std_as_in_if1_desc.bInterfaceNumber = ret;
1084 agdev->as_in_intf = ret;
1085 agdev->as_in_alt = 0;
1087 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1088 if (!agdev->out_ep) {
1089 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1093 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1094 if (!agdev->in_ep) {
1095 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1099 uac2->p_prm.uac2 = uac2;
1100 uac2->c_prm.uac2 = uac2;
1102 /* Calculate wMaxPacketSize according to audio bandwidth */
1103 ret = set_ep_max_packet_size(uac2_opts, &fs_epin_desc, USB_SPEED_FULL,
1106 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1110 ret = set_ep_max_packet_size(uac2_opts, &fs_epout_desc, USB_SPEED_FULL,
1113 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1117 ret = set_ep_max_packet_size(uac2_opts, &hs_epin_desc, USB_SPEED_HIGH,
1120 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1124 ret = set_ep_max_packet_size(uac2_opts, &hs_epout_desc, USB_SPEED_HIGH,
1127 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1131 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1132 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1134 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
1139 prm = &agdev->uac2.c_prm;
1140 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1141 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1144 goto err_free_descs;
1147 prm = &agdev->uac2.p_prm;
1148 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1149 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1155 ret = alsa_uac2_init(agdev);
1161 kfree(agdev->uac2.p_prm.rbuf);
1162 kfree(agdev->uac2.c_prm.rbuf);
1164 usb_free_all_descriptors(fn);
1169 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1171 struct usb_composite_dev *cdev = fn->config->cdev;
1172 struct audio_dev *agdev = func_to_agdev(fn);
1173 struct snd_uac2_chip *uac2 = &agdev->uac2;
1174 struct usb_gadget *gadget = cdev->gadget;
1175 struct device *dev = &uac2->pdev.dev;
1176 struct usb_request *req;
1178 struct uac2_rtd_params *prm;
1181 /* No i/f has more than 2 alt settings */
1183 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1187 if (intf == agdev->ac_intf) {
1188 /* Control I/f has only 1 AltSetting - 0 */
1190 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1196 if (intf == agdev->as_out_intf) {
1199 config_ep_by_speed(gadget, fn, ep);
1200 agdev->as_out_alt = alt;
1201 req_len = prm->max_psize;
1202 } else if (intf == agdev->as_in_intf) {
1203 struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev);
1204 unsigned int factor, rate;
1205 struct usb_endpoint_descriptor *ep_desc;
1209 config_ep_by_speed(gadget, fn, ep);
1210 agdev->as_in_alt = alt;
1212 /* pre-calculate the playback endpoint's interval */
1213 if (gadget->speed == USB_SPEED_FULL) {
1214 ep_desc = &fs_epin_desc;
1217 ep_desc = &hs_epin_desc;
1221 /* pre-compute some values for iso_complete() */
1222 uac2->p_framesize = opts->p_ssize *
1223 num_channels(opts->p_chmask);
1224 rate = opts->p_srate * uac2->p_framesize;
1225 uac2->p_interval = factor / (1 << (ep_desc->bInterval - 1));
1226 uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval,
1229 if (uac2->p_pktsize < prm->max_psize)
1230 uac2->p_pktsize_residue = rate % uac2->p_interval;
1232 uac2->p_pktsize_residue = 0;
1234 req_len = uac2->p_pktsize;
1235 uac2->p_residue = 0;
1237 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1246 prm->ep_enabled = true;
1249 for (i = 0; i < USB_XFERS; i++) {
1250 if (!prm->ureq[i].req) {
1251 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1255 prm->ureq[i].req = req;
1256 prm->ureq[i].pp = prm;
1259 req->context = &prm->ureq[i];
1260 req->length = req_len;
1261 req->complete = agdev_iso_complete;
1262 req->buf = prm->rbuf + i * prm->max_psize;
1265 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1266 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1273 afunc_get_alt(struct usb_function *fn, unsigned intf)
1275 struct audio_dev *agdev = func_to_agdev(fn);
1276 struct snd_uac2_chip *uac2 = &agdev->uac2;
1278 if (intf == agdev->ac_intf)
1279 return agdev->ac_alt;
1280 else if (intf == agdev->as_out_intf)
1281 return agdev->as_out_alt;
1282 else if (intf == agdev->as_in_intf)
1283 return agdev->as_in_alt;
1285 dev_err(&uac2->pdev.dev,
1286 "%s:%d Invalid Interface %d!\n",
1287 __func__, __LINE__, intf);
1293 afunc_disable(struct usb_function *fn)
1295 struct audio_dev *agdev = func_to_agdev(fn);
1296 struct snd_uac2_chip *uac2 = &agdev->uac2;
1298 free_ep(&uac2->p_prm, agdev->in_ep);
1299 agdev->as_in_alt = 0;
1301 free_ep(&uac2->c_prm, agdev->out_ep);
1302 agdev->as_out_alt = 0;
1306 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1308 struct usb_request *req = fn->config->cdev->req;
1309 struct audio_dev *agdev = func_to_agdev(fn);
1310 struct snd_uac2_chip *uac2 = &agdev->uac2;
1311 struct f_uac2_opts *opts;
1312 u16 w_length = le16_to_cpu(cr->wLength);
1313 u16 w_index = le16_to_cpu(cr->wIndex);
1314 u16 w_value = le16_to_cpu(cr->wValue);
1315 u8 entity_id = (w_index >> 8) & 0xff;
1316 u8 control_selector = w_value >> 8;
1317 int value = -EOPNOTSUPP;
1318 int p_srate, c_srate;
1320 opts = agdev_to_uac2_opts(agdev);
1321 p_srate = opts->p_srate;
1322 c_srate = opts->c_srate;
1324 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1325 struct cntrl_cur_lay3 c;
1326 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
1328 if (entity_id == USB_IN_CLK_ID)
1329 c.dCUR = cpu_to_le32(p_srate);
1330 else if (entity_id == USB_OUT_CLK_ID)
1331 c.dCUR = cpu_to_le32(c_srate);
1333 value = min_t(unsigned, w_length, sizeof c);
1334 memcpy(req->buf, &c, value);
1335 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1336 *(u8 *)req->buf = 1;
1337 value = min_t(unsigned, w_length, 1);
1339 dev_err(&uac2->pdev.dev,
1340 "%s:%d control_selector=%d TODO!\n",
1341 __func__, __LINE__, control_selector);
1348 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1350 struct usb_request *req = fn->config->cdev->req;
1351 struct audio_dev *agdev = func_to_agdev(fn);
1352 struct snd_uac2_chip *uac2 = &agdev->uac2;
1353 struct f_uac2_opts *opts;
1354 u16 w_length = le16_to_cpu(cr->wLength);
1355 u16 w_index = le16_to_cpu(cr->wIndex);
1356 u16 w_value = le16_to_cpu(cr->wValue);
1357 u8 entity_id = (w_index >> 8) & 0xff;
1358 u8 control_selector = w_value >> 8;
1359 struct cntrl_range_lay3 r;
1360 int value = -EOPNOTSUPP;
1361 int p_srate, c_srate;
1363 opts = agdev_to_uac2_opts(agdev);
1364 p_srate = opts->p_srate;
1365 c_srate = opts->c_srate;
1367 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1368 if (entity_id == USB_IN_CLK_ID)
1369 r.dMIN = cpu_to_le32(p_srate);
1370 else if (entity_id == USB_OUT_CLK_ID)
1371 r.dMIN = cpu_to_le32(c_srate);
1377 r.wNumSubRanges = cpu_to_le16(1);
1379 value = min_t(unsigned, w_length, sizeof r);
1380 memcpy(req->buf, &r, value);
1382 dev_err(&uac2->pdev.dev,
1383 "%s:%d control_selector=%d TODO!\n",
1384 __func__, __LINE__, control_selector);
1391 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1393 if (cr->bRequest == UAC2_CS_CUR)
1394 return in_rq_cur(fn, cr);
1395 else if (cr->bRequest == UAC2_CS_RANGE)
1396 return in_rq_range(fn, cr);
1402 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1404 u16 w_length = le16_to_cpu(cr->wLength);
1405 u16 w_value = le16_to_cpu(cr->wValue);
1406 u8 control_selector = w_value >> 8;
1408 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1415 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1417 struct audio_dev *agdev = func_to_agdev(fn);
1418 struct snd_uac2_chip *uac2 = &agdev->uac2;
1419 u16 w_index = le16_to_cpu(cr->wIndex);
1420 u8 intf = w_index & 0xff;
1422 if (intf != agdev->ac_intf) {
1423 dev_err(&uac2->pdev.dev,
1424 "%s:%d Error!\n", __func__, __LINE__);
1428 if (cr->bRequestType & USB_DIR_IN)
1429 return ac_rq_in(fn, cr);
1430 else if (cr->bRequest == UAC2_CS_CUR)
1431 return out_rq_cur(fn, cr);
1437 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1439 struct usb_composite_dev *cdev = fn->config->cdev;
1440 struct audio_dev *agdev = func_to_agdev(fn);
1441 struct snd_uac2_chip *uac2 = &agdev->uac2;
1442 struct usb_request *req = cdev->req;
1443 u16 w_length = le16_to_cpu(cr->wLength);
1444 int value = -EOPNOTSUPP;
1446 /* Only Class specific requests are supposed to reach here */
1447 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1450 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1451 value = setup_rq_inf(fn, cr);
1453 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1456 req->length = value;
1457 req->zero = value < w_length;
1458 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1460 dev_err(&uac2->pdev.dev,
1461 "%s:%d Error!\n", __func__, __LINE__);
1469 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1471 return container_of(to_config_group(item), struct f_uac2_opts,
1475 static void f_uac2_attr_release(struct config_item *item)
1477 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1479 usb_put_function_instance(&opts->func_inst);
1482 static struct configfs_item_operations f_uac2_item_ops = {
1483 .release = f_uac2_attr_release,
1486 #define UAC2_ATTRIBUTE(name) \
1487 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
1490 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1493 mutex_lock(&opts->lock); \
1494 result = sprintf(page, "%u\n", opts->name); \
1495 mutex_unlock(&opts->lock); \
1500 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
1501 const char *page, size_t len) \
1503 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
1507 mutex_lock(&opts->lock); \
1508 if (opts->refcnt) { \
1513 ret = kstrtou32(page, 0, &num); \
1521 mutex_unlock(&opts->lock); \
1525 CONFIGFS_ATTR(f_uac2_opts_, name)
1527 UAC2_ATTRIBUTE(p_chmask);
1528 UAC2_ATTRIBUTE(p_srate);
1529 UAC2_ATTRIBUTE(p_ssize);
1530 UAC2_ATTRIBUTE(c_chmask);
1531 UAC2_ATTRIBUTE(c_srate);
1532 UAC2_ATTRIBUTE(c_ssize);
1534 static struct configfs_attribute *f_uac2_attrs[] = {
1535 &f_uac2_opts_attr_p_chmask,
1536 &f_uac2_opts_attr_p_srate,
1537 &f_uac2_opts_attr_p_ssize,
1538 &f_uac2_opts_attr_c_chmask,
1539 &f_uac2_opts_attr_c_srate,
1540 &f_uac2_opts_attr_c_ssize,
1544 static struct config_item_type f_uac2_func_type = {
1545 .ct_item_ops = &f_uac2_item_ops,
1546 .ct_attrs = f_uac2_attrs,
1547 .ct_owner = THIS_MODULE,
1550 static void afunc_free_inst(struct usb_function_instance *f)
1552 struct f_uac2_opts *opts;
1554 opts = container_of(f, struct f_uac2_opts, func_inst);
1558 static struct usb_function_instance *afunc_alloc_inst(void)
1560 struct f_uac2_opts *opts;
1562 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1564 return ERR_PTR(-ENOMEM);
1566 mutex_init(&opts->lock);
1567 opts->func_inst.free_func_inst = afunc_free_inst;
1569 config_group_init_type_name(&opts->func_inst.group, "",
1572 opts->p_chmask = UAC2_DEF_PCHMASK;
1573 opts->p_srate = UAC2_DEF_PSRATE;
1574 opts->p_ssize = UAC2_DEF_PSSIZE;
1575 opts->c_chmask = UAC2_DEF_CCHMASK;
1576 opts->c_srate = UAC2_DEF_CSRATE;
1577 opts->c_ssize = UAC2_DEF_CSSIZE;
1578 return &opts->func_inst;
1581 static void afunc_free(struct usb_function *f)
1583 struct audio_dev *agdev;
1584 struct f_uac2_opts *opts;
1586 agdev = func_to_agdev(f);
1587 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1589 mutex_lock(&opts->lock);
1591 mutex_unlock(&opts->lock);
1594 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1596 struct audio_dev *agdev = func_to_agdev(f);
1597 struct uac2_rtd_params *prm;
1599 alsa_uac2_exit(agdev);
1601 prm = &agdev->uac2.p_prm;
1604 prm = &agdev->uac2.c_prm;
1606 usb_free_all_descriptors(f);
1609 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1611 struct audio_dev *agdev;
1612 struct f_uac2_opts *opts;
1614 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1616 return ERR_PTR(-ENOMEM);
1618 opts = container_of(fi, struct f_uac2_opts, func_inst);
1619 mutex_lock(&opts->lock);
1621 mutex_unlock(&opts->lock);
1623 agdev->func.name = "uac2_func";
1624 agdev->func.bind = afunc_bind;
1625 agdev->func.unbind = afunc_unbind;
1626 agdev->func.set_alt = afunc_set_alt;
1627 agdev->func.get_alt = afunc_get_alt;
1628 agdev->func.disable = afunc_disable;
1629 agdev->func.setup = afunc_setup;
1630 agdev->func.free_func = afunc_free;
1632 return &agdev->func;
1635 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1636 MODULE_LICENSE("GPL");
1637 MODULE_AUTHOR("Yadwinder Singh");
1638 MODULE_AUTHOR("Jaswinder Singh");