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;
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 int alsa_uac2_init(struct audio_dev *agdev)
517 struct snd_uac2_chip *uac2 = &agdev->uac2;
520 uac2->pdrv.probe = snd_uac2_probe;
521 uac2->pdrv.remove = snd_uac2_remove;
522 uac2->pdrv.driver.name = uac2_name;
525 uac2->pdev.name = uac2_name;
527 /* Register snd_uac2 driver */
528 err = platform_driver_register(&uac2->pdrv);
532 /* Register snd_uac2 device */
533 err = platform_device_register(&uac2->pdev);
535 platform_driver_unregister(&uac2->pdrv);
540 static void alsa_uac2_exit(struct audio_dev *agdev)
542 struct snd_uac2_chip *uac2 = &agdev->uac2;
544 platform_driver_unregister(&uac2->pdrv);
545 platform_device_unregister(&uac2->pdev);
549 /* --------- USB Function Interface ------------- */
566 static char clksrc_in[8];
567 static char clksrc_out[8];
569 static struct usb_string strings_fn[] = {
570 [STR_ASSOC].s = "Source/Sink",
571 [STR_IF_CTRL].s = "Topology Control",
572 [STR_CLKSRC_IN].s = clksrc_in,
573 [STR_CLKSRC_OUT].s = clksrc_out,
574 [STR_USB_IT].s = "USBH Out",
575 [STR_IO_IT].s = "USBD Out",
576 [STR_USB_OT].s = "USBH In",
577 [STR_IO_OT].s = "USBD In",
578 [STR_AS_OUT_ALT0].s = "Playback Inactive",
579 [STR_AS_OUT_ALT1].s = "Playback Active",
580 [STR_AS_IN_ALT0].s = "Capture Inactive",
581 [STR_AS_IN_ALT1].s = "Capture Active",
585 static struct usb_gadget_strings str_fn = {
586 .language = 0x0409, /* en-us */
587 .strings = strings_fn,
590 static struct usb_gadget_strings *fn_strings[] = {
595 static struct usb_qualifier_descriptor devqual_desc = {
596 .bLength = sizeof devqual_desc,
597 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
599 .bcdUSB = cpu_to_le16(0x200),
600 .bDeviceClass = USB_CLASS_MISC,
601 .bDeviceSubClass = 0x02,
602 .bDeviceProtocol = 0x01,
603 .bNumConfigurations = 1,
607 static struct usb_interface_assoc_descriptor iad_desc = {
608 .bLength = sizeof iad_desc,
609 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
611 .bFirstInterface = 0,
612 .bInterfaceCount = 3,
613 .bFunctionClass = USB_CLASS_AUDIO,
614 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
615 .bFunctionProtocol = UAC_VERSION_2,
618 /* Audio Control Interface */
619 static struct usb_interface_descriptor std_ac_if_desc = {
620 .bLength = sizeof std_ac_if_desc,
621 .bDescriptorType = USB_DT_INTERFACE,
623 .bAlternateSetting = 0,
625 .bInterfaceClass = USB_CLASS_AUDIO,
626 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
627 .bInterfaceProtocol = UAC_VERSION_2,
630 /* Clock source for IN traffic */
631 struct uac_clock_source_descriptor in_clk_src_desc = {
632 .bLength = sizeof in_clk_src_desc,
633 .bDescriptorType = USB_DT_CS_INTERFACE,
635 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
636 .bClockID = USB_IN_CLK_ID,
637 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
638 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
642 /* Clock source for OUT traffic */
643 struct uac_clock_source_descriptor out_clk_src_desc = {
644 .bLength = sizeof out_clk_src_desc,
645 .bDescriptorType = USB_DT_CS_INTERFACE,
647 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
648 .bClockID = USB_OUT_CLK_ID,
649 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
650 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
654 /* Input Terminal for USB_OUT */
655 struct uac2_input_terminal_descriptor usb_out_it_desc = {
656 .bLength = sizeof usb_out_it_desc,
657 .bDescriptorType = USB_DT_CS_INTERFACE,
659 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
660 .bTerminalID = USB_OUT_IT_ID,
661 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
663 .bCSourceID = USB_OUT_CLK_ID,
665 .bmControls = (CONTROL_RDWR << COPY_CTRL),
668 /* Input Terminal for I/O-In */
669 struct uac2_input_terminal_descriptor io_in_it_desc = {
670 .bLength = sizeof io_in_it_desc,
671 .bDescriptorType = USB_DT_CS_INTERFACE,
673 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
674 .bTerminalID = IO_IN_IT_ID,
675 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
677 .bCSourceID = USB_IN_CLK_ID,
679 .bmControls = (CONTROL_RDWR << COPY_CTRL),
682 /* Ouput Terminal for USB_IN */
683 struct uac2_output_terminal_descriptor usb_in_ot_desc = {
684 .bLength = sizeof usb_in_ot_desc,
685 .bDescriptorType = USB_DT_CS_INTERFACE,
687 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
688 .bTerminalID = USB_IN_OT_ID,
689 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
691 .bSourceID = IO_IN_IT_ID,
692 .bCSourceID = USB_IN_CLK_ID,
693 .bmControls = (CONTROL_RDWR << COPY_CTRL),
696 /* Ouput Terminal for I/O-Out */
697 struct uac2_output_terminal_descriptor io_out_ot_desc = {
698 .bLength = sizeof io_out_ot_desc,
699 .bDescriptorType = USB_DT_CS_INTERFACE,
701 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
702 .bTerminalID = IO_OUT_OT_ID,
703 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
705 .bSourceID = USB_OUT_IT_ID,
706 .bCSourceID = USB_OUT_CLK_ID,
707 .bmControls = (CONTROL_RDWR << COPY_CTRL),
710 struct uac2_ac_header_descriptor ac_hdr_desc = {
711 .bLength = sizeof ac_hdr_desc,
712 .bDescriptorType = USB_DT_CS_INTERFACE,
714 .bDescriptorSubtype = UAC_MS_HEADER,
715 .bcdADC = cpu_to_le16(0x200),
716 .bCategory = UAC2_FUNCTION_IO_BOX,
717 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
718 + sizeof usb_out_it_desc + sizeof io_in_it_desc
719 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
723 /* Audio Streaming OUT Interface - Alt0 */
724 static struct usb_interface_descriptor std_as_out_if0_desc = {
725 .bLength = sizeof std_as_out_if0_desc,
726 .bDescriptorType = USB_DT_INTERFACE,
728 .bAlternateSetting = 0,
730 .bInterfaceClass = USB_CLASS_AUDIO,
731 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
732 .bInterfaceProtocol = UAC_VERSION_2,
735 /* Audio Streaming OUT Interface - Alt1 */
736 static struct usb_interface_descriptor std_as_out_if1_desc = {
737 .bLength = sizeof std_as_out_if1_desc,
738 .bDescriptorType = USB_DT_INTERFACE,
740 .bAlternateSetting = 1,
742 .bInterfaceClass = USB_CLASS_AUDIO,
743 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
744 .bInterfaceProtocol = UAC_VERSION_2,
747 /* Audio Stream OUT Intface Desc */
748 struct uac2_as_header_descriptor as_out_hdr_desc = {
749 .bLength = sizeof as_out_hdr_desc,
750 .bDescriptorType = USB_DT_CS_INTERFACE,
752 .bDescriptorSubtype = UAC_AS_GENERAL,
753 .bTerminalLink = USB_OUT_IT_ID,
755 .bFormatType = UAC_FORMAT_TYPE_I,
756 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
760 /* Audio USB_OUT Format */
761 struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
762 .bLength = sizeof as_out_fmt1_desc,
763 .bDescriptorType = USB_DT_CS_INTERFACE,
764 .bDescriptorSubtype = UAC_FORMAT_TYPE,
765 .bFormatType = UAC_FORMAT_TYPE_I,
768 /* STD AS ISO OUT Endpoint */
769 struct usb_endpoint_descriptor fs_epout_desc = {
770 .bLength = USB_DT_ENDPOINT_SIZE,
771 .bDescriptorType = USB_DT_ENDPOINT,
773 .bEndpointAddress = USB_DIR_OUT,
774 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
778 struct usb_endpoint_descriptor hs_epout_desc = {
779 .bLength = USB_DT_ENDPOINT_SIZE,
780 .bDescriptorType = USB_DT_ENDPOINT,
782 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
786 /* CS AS ISO OUT Endpoint */
787 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
788 .bLength = sizeof as_iso_out_desc,
789 .bDescriptorType = USB_DT_CS_ENDPOINT,
791 .bDescriptorSubtype = UAC_EP_GENERAL,
794 .bLockDelayUnits = 0,
798 /* Audio Streaming IN Interface - Alt0 */
799 static struct usb_interface_descriptor std_as_in_if0_desc = {
800 .bLength = sizeof std_as_in_if0_desc,
801 .bDescriptorType = USB_DT_INTERFACE,
803 .bAlternateSetting = 0,
805 .bInterfaceClass = USB_CLASS_AUDIO,
806 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
807 .bInterfaceProtocol = UAC_VERSION_2,
810 /* Audio Streaming IN Interface - Alt1 */
811 static struct usb_interface_descriptor std_as_in_if1_desc = {
812 .bLength = sizeof std_as_in_if1_desc,
813 .bDescriptorType = USB_DT_INTERFACE,
815 .bAlternateSetting = 1,
817 .bInterfaceClass = USB_CLASS_AUDIO,
818 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
819 .bInterfaceProtocol = UAC_VERSION_2,
822 /* Audio Stream IN Intface Desc */
823 struct uac2_as_header_descriptor as_in_hdr_desc = {
824 .bLength = sizeof as_in_hdr_desc,
825 .bDescriptorType = USB_DT_CS_INTERFACE,
827 .bDescriptorSubtype = UAC_AS_GENERAL,
828 .bTerminalLink = USB_IN_OT_ID,
830 .bFormatType = UAC_FORMAT_TYPE_I,
831 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
835 /* Audio USB_IN Format */
836 struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
837 .bLength = sizeof as_in_fmt1_desc,
838 .bDescriptorType = USB_DT_CS_INTERFACE,
839 .bDescriptorSubtype = UAC_FORMAT_TYPE,
840 .bFormatType = UAC_FORMAT_TYPE_I,
843 /* STD AS ISO IN Endpoint */
844 struct usb_endpoint_descriptor fs_epin_desc = {
845 .bLength = USB_DT_ENDPOINT_SIZE,
846 .bDescriptorType = USB_DT_ENDPOINT,
848 .bEndpointAddress = USB_DIR_IN,
849 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
853 struct usb_endpoint_descriptor hs_epin_desc = {
854 .bLength = USB_DT_ENDPOINT_SIZE,
855 .bDescriptorType = USB_DT_ENDPOINT,
857 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
861 /* CS AS ISO IN Endpoint */
862 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
863 .bLength = sizeof as_iso_in_desc,
864 .bDescriptorType = USB_DT_CS_ENDPOINT,
866 .bDescriptorSubtype = UAC_EP_GENERAL,
869 .bLockDelayUnits = 0,
873 static struct usb_descriptor_header *fs_audio_desc[] = {
874 (struct usb_descriptor_header *)&iad_desc,
875 (struct usb_descriptor_header *)&std_ac_if_desc,
877 (struct usb_descriptor_header *)&ac_hdr_desc,
878 (struct usb_descriptor_header *)&in_clk_src_desc,
879 (struct usb_descriptor_header *)&out_clk_src_desc,
880 (struct usb_descriptor_header *)&usb_out_it_desc,
881 (struct usb_descriptor_header *)&io_in_it_desc,
882 (struct usb_descriptor_header *)&usb_in_ot_desc,
883 (struct usb_descriptor_header *)&io_out_ot_desc,
885 (struct usb_descriptor_header *)&std_as_out_if0_desc,
886 (struct usb_descriptor_header *)&std_as_out_if1_desc,
888 (struct usb_descriptor_header *)&as_out_hdr_desc,
889 (struct usb_descriptor_header *)&as_out_fmt1_desc,
890 (struct usb_descriptor_header *)&fs_epout_desc,
891 (struct usb_descriptor_header *)&as_iso_out_desc,
893 (struct usb_descriptor_header *)&std_as_in_if0_desc,
894 (struct usb_descriptor_header *)&std_as_in_if1_desc,
896 (struct usb_descriptor_header *)&as_in_hdr_desc,
897 (struct usb_descriptor_header *)&as_in_fmt1_desc,
898 (struct usb_descriptor_header *)&fs_epin_desc,
899 (struct usb_descriptor_header *)&as_iso_in_desc,
903 static struct usb_descriptor_header *hs_audio_desc[] = {
904 (struct usb_descriptor_header *)&iad_desc,
905 (struct usb_descriptor_header *)&std_ac_if_desc,
907 (struct usb_descriptor_header *)&ac_hdr_desc,
908 (struct usb_descriptor_header *)&in_clk_src_desc,
909 (struct usb_descriptor_header *)&out_clk_src_desc,
910 (struct usb_descriptor_header *)&usb_out_it_desc,
911 (struct usb_descriptor_header *)&io_in_it_desc,
912 (struct usb_descriptor_header *)&usb_in_ot_desc,
913 (struct usb_descriptor_header *)&io_out_ot_desc,
915 (struct usb_descriptor_header *)&std_as_out_if0_desc,
916 (struct usb_descriptor_header *)&std_as_out_if1_desc,
918 (struct usb_descriptor_header *)&as_out_hdr_desc,
919 (struct usb_descriptor_header *)&as_out_fmt1_desc,
920 (struct usb_descriptor_header *)&hs_epout_desc,
921 (struct usb_descriptor_header *)&as_iso_out_desc,
923 (struct usb_descriptor_header *)&std_as_in_if0_desc,
924 (struct usb_descriptor_header *)&std_as_in_if1_desc,
926 (struct usb_descriptor_header *)&as_in_hdr_desc,
927 (struct usb_descriptor_header *)&as_in_fmt1_desc,
928 (struct usb_descriptor_header *)&hs_epin_desc,
929 (struct usb_descriptor_header *)&as_iso_in_desc,
933 struct cntrl_cur_lay3 {
937 struct cntrl_range_lay3 {
945 free_ep(struct uac2_rtd_params *prm, struct usb_ep *ep)
947 struct snd_uac2_chip *uac2 = prm->uac2;
950 prm->ep_enabled = false;
952 for (i = 0; i < USB_XFERS; i++) {
953 if (prm->ureq[i].req) {
954 usb_ep_dequeue(ep, prm->ureq[i].req);
955 usb_ep_free_request(ep, prm->ureq[i].req);
956 prm->ureq[i].req = NULL;
960 if (usb_ep_disable(ep))
961 dev_err(&uac2->pdev.dev,
962 "%s:%d Error!\n", __func__, __LINE__);
966 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
968 struct audio_dev *agdev = func_to_agdev(fn);
969 struct snd_uac2_chip *uac2 = &agdev->uac2;
970 struct usb_composite_dev *cdev = cfg->cdev;
971 struct usb_gadget *gadget = cdev->gadget;
972 struct device *dev = &uac2->pdev.dev;
973 struct uac2_rtd_params *prm;
974 struct f_uac2_opts *uac2_opts;
975 struct usb_string *us;
978 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
980 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
983 iad_desc.iFunction = us[STR_ASSOC].id;
984 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
985 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
986 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
987 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
988 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
989 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
990 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
991 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
992 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
993 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
994 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
997 /* Initialize the configurable parameters */
998 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
999 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1000 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1001 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1002 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
1003 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
1004 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
1005 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
1006 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
1007 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
1008 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
1009 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
1011 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
1012 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
1014 ret = usb_interface_id(cfg, fn);
1016 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1019 std_ac_if_desc.bInterfaceNumber = ret;
1020 agdev->ac_intf = ret;
1023 ret = usb_interface_id(cfg, fn);
1025 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1028 std_as_out_if0_desc.bInterfaceNumber = ret;
1029 std_as_out_if1_desc.bInterfaceNumber = ret;
1030 agdev->as_out_intf = ret;
1031 agdev->as_out_alt = 0;
1033 ret = usb_interface_id(cfg, fn);
1035 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1038 std_as_in_if0_desc.bInterfaceNumber = ret;
1039 std_as_in_if1_desc.bInterfaceNumber = ret;
1040 agdev->as_in_intf = ret;
1041 agdev->as_in_alt = 0;
1043 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
1044 if (!agdev->out_ep) {
1045 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1048 agdev->out_ep->driver_data = agdev;
1050 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
1051 if (!agdev->in_ep) {
1052 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1055 agdev->in_ep->driver_data = agdev;
1057 uac2->p_prm.uac2 = uac2;
1058 uac2->c_prm.uac2 = uac2;
1060 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
1061 hs_epout_desc.wMaxPacketSize = fs_epout_desc.wMaxPacketSize;
1062 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
1063 hs_epin_desc.wMaxPacketSize = fs_epin_desc.wMaxPacketSize;
1065 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL);
1069 prm = &agdev->uac2.c_prm;
1070 prm->max_psize = hs_epout_desc.wMaxPacketSize;
1071 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1077 prm = &agdev->uac2.p_prm;
1078 prm->max_psize = hs_epin_desc.wMaxPacketSize;
1079 prm->rbuf = kzalloc(prm->max_psize * USB_XFERS, GFP_KERNEL);
1085 ret = alsa_uac2_init(agdev);
1090 kfree(agdev->uac2.p_prm.rbuf);
1091 kfree(agdev->uac2.c_prm.rbuf);
1092 usb_free_all_descriptors(fn);
1094 agdev->in_ep->driver_data = NULL;
1096 agdev->out_ep->driver_data = NULL;
1101 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
1103 struct usb_composite_dev *cdev = fn->config->cdev;
1104 struct audio_dev *agdev = func_to_agdev(fn);
1105 struct snd_uac2_chip *uac2 = &agdev->uac2;
1106 struct usb_gadget *gadget = cdev->gadget;
1107 struct device *dev = &uac2->pdev.dev;
1108 struct usb_request *req;
1110 struct uac2_rtd_params *prm;
1113 /* No i/f has more than 2 alt settings */
1115 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1119 if (intf == agdev->ac_intf) {
1120 /* Control I/f has only 1 AltSetting - 0 */
1122 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1128 if (intf == agdev->as_out_intf) {
1131 config_ep_by_speed(gadget, fn, ep);
1132 agdev->as_out_alt = alt;
1133 req_len = prm->max_psize;
1134 } else if (intf == agdev->as_in_intf) {
1135 struct f_uac2_opts *opts = agdev_to_uac2_opts(agdev);
1136 unsigned int factor, rate;
1137 struct usb_endpoint_descriptor *ep_desc;
1141 config_ep_by_speed(gadget, fn, ep);
1142 agdev->as_in_alt = alt;
1144 /* pre-calculate the playback endpoint's interval */
1145 if (gadget->speed == USB_SPEED_FULL) {
1146 ep_desc = &fs_epin_desc;
1149 ep_desc = &hs_epin_desc;
1153 /* pre-compute some values for iso_complete() */
1154 uac2->p_framesize = opts->p_ssize *
1155 num_channels(opts->p_chmask);
1156 rate = opts->p_srate * uac2->p_framesize;
1157 uac2->p_interval = (1 << (ep_desc->bInterval - 1)) * factor;
1158 uac2->p_pktsize = min_t(unsigned int, rate / uac2->p_interval,
1161 if (uac2->p_pktsize < prm->max_psize)
1162 uac2->p_pktsize_residue = rate % uac2->p_interval;
1164 uac2->p_pktsize_residue = 0;
1166 req_len = uac2->p_pktsize;
1167 uac2->p_residue = 0;
1169 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1178 prm->ep_enabled = true;
1181 for (i = 0; i < USB_XFERS; i++) {
1182 if (!prm->ureq[i].req) {
1183 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
1187 prm->ureq[i].req = req;
1188 prm->ureq[i].pp = prm;
1191 req->context = &prm->ureq[i];
1192 req->length = req_len;
1193 req->complete = agdev_iso_complete;
1194 req->buf = prm->rbuf + i * prm->max_psize;
1197 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
1198 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
1205 afunc_get_alt(struct usb_function *fn, unsigned intf)
1207 struct audio_dev *agdev = func_to_agdev(fn);
1208 struct snd_uac2_chip *uac2 = &agdev->uac2;
1210 if (intf == agdev->ac_intf)
1211 return agdev->ac_alt;
1212 else if (intf == agdev->as_out_intf)
1213 return agdev->as_out_alt;
1214 else if (intf == agdev->as_in_intf)
1215 return agdev->as_in_alt;
1217 dev_err(&uac2->pdev.dev,
1218 "%s:%d Invalid Interface %d!\n",
1219 __func__, __LINE__, intf);
1225 afunc_disable(struct usb_function *fn)
1227 struct audio_dev *agdev = func_to_agdev(fn);
1228 struct snd_uac2_chip *uac2 = &agdev->uac2;
1230 free_ep(&uac2->p_prm, agdev->in_ep);
1231 agdev->as_in_alt = 0;
1233 free_ep(&uac2->c_prm, agdev->out_ep);
1234 agdev->as_out_alt = 0;
1238 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1240 struct usb_request *req = fn->config->cdev->req;
1241 struct audio_dev *agdev = func_to_agdev(fn);
1242 struct snd_uac2_chip *uac2 = &agdev->uac2;
1243 struct f_uac2_opts *opts;
1244 u16 w_length = le16_to_cpu(cr->wLength);
1245 u16 w_index = le16_to_cpu(cr->wIndex);
1246 u16 w_value = le16_to_cpu(cr->wValue);
1247 u8 entity_id = (w_index >> 8) & 0xff;
1248 u8 control_selector = w_value >> 8;
1249 int value = -EOPNOTSUPP;
1250 int p_srate, c_srate;
1252 opts = agdev_to_uac2_opts(agdev);
1253 p_srate = opts->p_srate;
1254 c_srate = opts->c_srate;
1256 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1257 struct cntrl_cur_lay3 c;
1259 if (entity_id == USB_IN_CLK_ID)
1261 else if (entity_id == USB_OUT_CLK_ID)
1264 value = min_t(unsigned, w_length, sizeof c);
1265 memcpy(req->buf, &c, value);
1266 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
1267 *(u8 *)req->buf = 1;
1268 value = min_t(unsigned, w_length, 1);
1270 dev_err(&uac2->pdev.dev,
1271 "%s:%d control_selector=%d TODO!\n",
1272 __func__, __LINE__, control_selector);
1279 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1281 struct usb_request *req = fn->config->cdev->req;
1282 struct audio_dev *agdev = func_to_agdev(fn);
1283 struct snd_uac2_chip *uac2 = &agdev->uac2;
1284 struct f_uac2_opts *opts;
1285 u16 w_length = le16_to_cpu(cr->wLength);
1286 u16 w_index = le16_to_cpu(cr->wIndex);
1287 u16 w_value = le16_to_cpu(cr->wValue);
1288 u8 entity_id = (w_index >> 8) & 0xff;
1289 u8 control_selector = w_value >> 8;
1290 struct cntrl_range_lay3 r;
1291 int value = -EOPNOTSUPP;
1292 int p_srate, c_srate;
1294 opts = agdev_to_uac2_opts(agdev);
1295 p_srate = opts->p_srate;
1296 c_srate = opts->c_srate;
1298 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
1299 if (entity_id == USB_IN_CLK_ID)
1301 else if (entity_id == USB_OUT_CLK_ID)
1308 r.wNumSubRanges = 1;
1310 value = min_t(unsigned, w_length, sizeof r);
1311 memcpy(req->buf, &r, value);
1313 dev_err(&uac2->pdev.dev,
1314 "%s:%d control_selector=%d TODO!\n",
1315 __func__, __LINE__, control_selector);
1322 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1324 if (cr->bRequest == UAC2_CS_CUR)
1325 return in_rq_cur(fn, cr);
1326 else if (cr->bRequest == UAC2_CS_RANGE)
1327 return in_rq_range(fn, cr);
1333 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1335 u16 w_length = le16_to_cpu(cr->wLength);
1336 u16 w_value = le16_to_cpu(cr->wValue);
1337 u8 control_selector = w_value >> 8;
1339 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
1346 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1348 struct audio_dev *agdev = func_to_agdev(fn);
1349 struct snd_uac2_chip *uac2 = &agdev->uac2;
1350 u16 w_index = le16_to_cpu(cr->wIndex);
1351 u8 intf = w_index & 0xff;
1353 if (intf != agdev->ac_intf) {
1354 dev_err(&uac2->pdev.dev,
1355 "%s:%d Error!\n", __func__, __LINE__);
1359 if (cr->bRequestType & USB_DIR_IN)
1360 return ac_rq_in(fn, cr);
1361 else if (cr->bRequest == UAC2_CS_CUR)
1362 return out_rq_cur(fn, cr);
1368 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
1370 struct usb_composite_dev *cdev = fn->config->cdev;
1371 struct audio_dev *agdev = func_to_agdev(fn);
1372 struct snd_uac2_chip *uac2 = &agdev->uac2;
1373 struct usb_request *req = cdev->req;
1374 u16 w_length = le16_to_cpu(cr->wLength);
1375 int value = -EOPNOTSUPP;
1377 /* Only Class specific requests are supposed to reach here */
1378 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
1381 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
1382 value = setup_rq_inf(fn, cr);
1384 dev_err(&uac2->pdev.dev, "%s:%d Error!\n", __func__, __LINE__);
1387 req->length = value;
1388 req->zero = value < w_length;
1389 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1391 dev_err(&uac2->pdev.dev,
1392 "%s:%d Error!\n", __func__, __LINE__);
1400 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
1402 return container_of(to_config_group(item), struct f_uac2_opts,
1406 CONFIGFS_ATTR_STRUCT(f_uac2_opts);
1407 CONFIGFS_ATTR_OPS(f_uac2_opts);
1409 static void f_uac2_attr_release(struct config_item *item)
1411 struct f_uac2_opts *opts = to_f_uac2_opts(item);
1413 usb_put_function_instance(&opts->func_inst);
1416 static struct configfs_item_operations f_uac2_item_ops = {
1417 .release = f_uac2_attr_release,
1418 .show_attribute = f_uac2_opts_attr_show,
1419 .store_attribute = f_uac2_opts_attr_store,
1422 #define UAC2_ATTRIBUTE(name) \
1423 static ssize_t f_uac2_opts_##name##_show(struct f_uac2_opts *opts, \
1428 mutex_lock(&opts->lock); \
1429 result = sprintf(page, "%u\n", opts->name); \
1430 mutex_unlock(&opts->lock); \
1435 static ssize_t f_uac2_opts_##name##_store(struct f_uac2_opts *opts, \
1436 const char *page, size_t len) \
1441 mutex_lock(&opts->lock); \
1442 if (opts->refcnt) { \
1447 ret = kstrtou32(page, 0, &num); \
1455 mutex_unlock(&opts->lock); \
1459 static struct f_uac2_opts_attribute f_uac2_opts_##name = \
1460 __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
1461 f_uac2_opts_##name##_show, \
1462 f_uac2_opts_##name##_store)
1464 UAC2_ATTRIBUTE(p_chmask);
1465 UAC2_ATTRIBUTE(p_srate);
1466 UAC2_ATTRIBUTE(p_ssize);
1467 UAC2_ATTRIBUTE(c_chmask);
1468 UAC2_ATTRIBUTE(c_srate);
1469 UAC2_ATTRIBUTE(c_ssize);
1471 static struct configfs_attribute *f_uac2_attrs[] = {
1472 &f_uac2_opts_p_chmask.attr,
1473 &f_uac2_opts_p_srate.attr,
1474 &f_uac2_opts_p_ssize.attr,
1475 &f_uac2_opts_c_chmask.attr,
1476 &f_uac2_opts_c_srate.attr,
1477 &f_uac2_opts_c_ssize.attr,
1481 static struct config_item_type f_uac2_func_type = {
1482 .ct_item_ops = &f_uac2_item_ops,
1483 .ct_attrs = f_uac2_attrs,
1484 .ct_owner = THIS_MODULE,
1487 static void afunc_free_inst(struct usb_function_instance *f)
1489 struct f_uac2_opts *opts;
1491 opts = container_of(f, struct f_uac2_opts, func_inst);
1495 static struct usb_function_instance *afunc_alloc_inst(void)
1497 struct f_uac2_opts *opts;
1499 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1501 return ERR_PTR(-ENOMEM);
1503 mutex_init(&opts->lock);
1504 opts->func_inst.free_func_inst = afunc_free_inst;
1506 config_group_init_type_name(&opts->func_inst.group, "",
1509 opts->p_chmask = UAC2_DEF_PCHMASK;
1510 opts->p_srate = UAC2_DEF_PSRATE;
1511 opts->p_ssize = UAC2_DEF_PSSIZE;
1512 opts->c_chmask = UAC2_DEF_CCHMASK;
1513 opts->c_srate = UAC2_DEF_CSRATE;
1514 opts->c_ssize = UAC2_DEF_CSSIZE;
1515 return &opts->func_inst;
1518 static void afunc_free(struct usb_function *f)
1520 struct audio_dev *agdev;
1521 struct f_uac2_opts *opts;
1523 agdev = func_to_agdev(f);
1524 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
1526 mutex_lock(&opts->lock);
1528 mutex_unlock(&opts->lock);
1531 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
1533 struct audio_dev *agdev = func_to_agdev(f);
1534 struct uac2_rtd_params *prm;
1536 alsa_uac2_exit(agdev);
1538 prm = &agdev->uac2.p_prm;
1541 prm = &agdev->uac2.c_prm;
1543 usb_free_all_descriptors(f);
1546 agdev->in_ep->driver_data = NULL;
1548 agdev->out_ep->driver_data = NULL;
1551 struct usb_function *afunc_alloc(struct usb_function_instance *fi)
1553 struct audio_dev *agdev;
1554 struct f_uac2_opts *opts;
1556 agdev = kzalloc(sizeof(*agdev), GFP_KERNEL);
1558 return ERR_PTR(-ENOMEM);
1560 opts = container_of(fi, struct f_uac2_opts, func_inst);
1561 mutex_lock(&opts->lock);
1563 mutex_unlock(&opts->lock);
1565 agdev->func.name = "uac2_func";
1566 agdev->func.bind = afunc_bind;
1567 agdev->func.unbind = afunc_unbind;
1568 agdev->func.set_alt = afunc_set_alt;
1569 agdev->func.get_alt = afunc_get_alt;
1570 agdev->func.disable = afunc_disable;
1571 agdev->func.setup = afunc_setup;
1572 agdev->func.free_func = afunc_free;
1574 return &agdev->func;
1577 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1578 MODULE_LICENSE("GPL");
1579 MODULE_AUTHOR("Yadwinder Singh");
1580 MODULE_AUTHOR("Jaswinder Singh");