2 * Gadget Function Driver for USB audio source device
4 * Copyright (C) 2012 Google, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/device.h>
18 #include <linux/usb/audio.h>
19 #include <linux/wait.h>
20 #include <sound/core.h>
21 #include <sound/initval.h>
22 #include <sound/pcm.h>
24 #define SAMPLE_RATE 44100
25 #define FRAMES_PER_MSEC (SAMPLE_RATE / 1000)
27 #define IN_EP_MAX_PACKET_SIZE 384
29 /* Number of requests to allocate */
30 #define IN_EP_REQ_COUNT 4
32 #define AUDIO_AC_INTERFACE 0
33 #define AUDIO_AS_INTERFACE 1
34 #define AUDIO_NUM_INTERFACES 2
36 /* B.3.1 Standard AC Interface Descriptor */
37 static struct usb_interface_descriptor ac_interface_desc = {
38 .bLength = USB_DT_INTERFACE_SIZE,
39 .bDescriptorType = USB_DT_INTERFACE,
41 .bInterfaceClass = USB_CLASS_AUDIO,
42 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
45 DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
47 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(AUDIO_NUM_INTERFACES)
48 /* 1 input terminal, 1 output terminal and 1 feature unit */
49 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
50 + UAC_DT_INPUT_TERMINAL_SIZE + UAC_DT_OUTPUT_TERMINAL_SIZE \
51 + UAC_DT_FEATURE_UNIT_SIZE(0))
52 /* B.3.2 Class-Specific AC Interface Descriptor */
53 static struct uac1_ac_header_descriptor_2 ac_header_desc = {
54 .bLength = UAC_DT_AC_HEADER_LENGTH,
55 .bDescriptorType = USB_DT_CS_INTERFACE,
56 .bDescriptorSubtype = UAC_HEADER,
57 .bcdADC = __constant_cpu_to_le16(0x0100),
58 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
59 .bInCollection = AUDIO_NUM_INTERFACES,
61 [0] = AUDIO_AC_INTERFACE,
62 [1] = AUDIO_AS_INTERFACE,
66 #define INPUT_TERMINAL_ID 1
67 static struct uac_input_terminal_descriptor input_terminal_desc = {
68 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
69 .bDescriptorType = USB_DT_CS_INTERFACE,
70 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
71 .bTerminalID = INPUT_TERMINAL_ID,
72 .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE,
74 .wChannelConfig = 0x3,
77 DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
79 #define FEATURE_UNIT_ID 2
80 static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
81 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
82 .bDescriptorType = USB_DT_CS_INTERFACE,
83 .bDescriptorSubtype = UAC_FEATURE_UNIT,
84 .bUnitID = FEATURE_UNIT_ID,
85 .bSourceID = INPUT_TERMINAL_ID,
89 #define OUTPUT_TERMINAL_ID 3
90 static struct uac1_output_terminal_descriptor output_terminal_desc = {
91 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
92 .bDescriptorType = USB_DT_CS_INTERFACE,
93 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
94 .bTerminalID = OUTPUT_TERMINAL_ID,
95 .wTerminalType = UAC_TERMINAL_STREAMING,
96 .bAssocTerminal = FEATURE_UNIT_ID,
97 .bSourceID = FEATURE_UNIT_ID,
100 /* B.4.1 Standard AS Interface Descriptor */
101 static struct usb_interface_descriptor as_interface_alt_0_desc = {
102 .bLength = USB_DT_INTERFACE_SIZE,
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bAlternateSetting = 0,
106 .bInterfaceClass = USB_CLASS_AUDIO,
107 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
110 static struct usb_interface_descriptor as_interface_alt_1_desc = {
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 .bAlternateSetting = 1,
115 .bInterfaceClass = USB_CLASS_AUDIO,
116 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
119 /* B.4.2 Class-Specific AS Interface Descriptor */
120 static struct uac1_as_header_descriptor as_header_desc = {
121 .bLength = UAC_DT_AS_HEADER_SIZE,
122 .bDescriptorType = USB_DT_CS_INTERFACE,
123 .bDescriptorSubtype = UAC_AS_GENERAL,
124 .bTerminalLink = INPUT_TERMINAL_ID,
126 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
129 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
131 static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
132 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubtype = UAC_FORMAT_TYPE,
135 .bFormatType = UAC_FORMAT_TYPE_I,
137 .bBitResolution = 16,
141 /* Standard ISO IN Endpoint Descriptor for highspeed */
142 static struct usb_endpoint_descriptor hs_as_in_ep_desc = {
143 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
144 .bDescriptorType = USB_DT_ENDPOINT,
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_SYNC_SYNC
147 | USB_ENDPOINT_XFER_ISOC,
148 .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
149 .bInterval = 4, /* poll 1 per millisecond */
152 /* Standard ISO IN Endpoint Descriptor for highspeed */
153 static struct usb_endpoint_descriptor fs_as_in_ep_desc = {
154 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
155 .bDescriptorType = USB_DT_ENDPOINT,
156 .bEndpointAddress = USB_DIR_IN,
157 .bmAttributes = USB_ENDPOINT_SYNC_SYNC
158 | USB_ENDPOINT_XFER_ISOC,
159 .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
160 .bInterval = 1, /* poll 1 per millisecond */
163 /* Class-specific AS ISO OUT Endpoint Descriptor */
164 static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
165 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
166 .bDescriptorType = USB_DT_CS_ENDPOINT,
167 .bDescriptorSubtype = UAC_EP_GENERAL,
169 .bLockDelayUnits = 1,
170 .wLockDelay = __constant_cpu_to_le16(1),
173 static struct usb_descriptor_header *hs_audio_desc[] = {
174 (struct usb_descriptor_header *)&ac_interface_desc,
175 (struct usb_descriptor_header *)&ac_header_desc,
177 (struct usb_descriptor_header *)&input_terminal_desc,
178 (struct usb_descriptor_header *)&output_terminal_desc,
179 (struct usb_descriptor_header *)&feature_unit_desc,
181 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
182 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
183 (struct usb_descriptor_header *)&as_header_desc,
185 (struct usb_descriptor_header *)&as_type_i_desc,
187 (struct usb_descriptor_header *)&hs_as_in_ep_desc,
188 (struct usb_descriptor_header *)&as_iso_in_desc,
192 static struct usb_descriptor_header *fs_audio_desc[] = {
193 (struct usb_descriptor_header *)&ac_interface_desc,
194 (struct usb_descriptor_header *)&ac_header_desc,
196 (struct usb_descriptor_header *)&input_terminal_desc,
197 (struct usb_descriptor_header *)&output_terminal_desc,
198 (struct usb_descriptor_header *)&feature_unit_desc,
200 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
201 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
202 (struct usb_descriptor_header *)&as_header_desc,
204 (struct usb_descriptor_header *)&as_type_i_desc,
206 (struct usb_descriptor_header *)&fs_as_in_ep_desc,
207 (struct usb_descriptor_header *)&as_iso_in_desc,
211 static struct snd_pcm_hardware audio_hw_info = {
212 .info = SNDRV_PCM_INFO_MMAP |
213 SNDRV_PCM_INFO_MMAP_VALID |
214 SNDRV_PCM_INFO_BATCH |
215 SNDRV_PCM_INFO_INTERLEAVED |
216 SNDRV_PCM_INFO_BLOCK_TRANSFER,
218 .formats = SNDRV_PCM_FMTBIT_S16_LE,
221 .rate_min = SAMPLE_RATE,
222 .rate_max = SAMPLE_RATE,
224 .buffer_bytes_max = 1024 * 1024,
225 .period_bytes_min = 64,
226 .period_bytes_max = 512 * 1024,
231 /*-------------------------------------------------------------------------*/
233 struct audio_source_config {
239 struct usb_function func;
240 struct snd_card *card;
242 struct snd_pcm_substream *substream;
244 struct list_head idle_reqs;
245 struct usb_ep *in_ep;
249 /* beginning, end and current position in our buffer */
254 /* byte size of a "period" */
256 /* bytes sent since last call to snd_pcm_period_elapsed */
257 unsigned int period_offset;
258 /* time we started playing */
260 /* number of frames sent since start_time */
264 static inline struct audio_dev *func_to_audio(struct usb_function *f)
266 return container_of(f, struct audio_dev, func);
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size)
273 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
277 req->buf = kmalloc(buffer_size, GFP_KERNEL);
279 usb_ep_free_request(ep, req);
282 req->length = buffer_size;
286 static void audio_request_free(struct usb_request *req, struct usb_ep *ep)
290 usb_ep_free_request(ep, req);
294 static void audio_req_put(struct audio_dev *audio, struct usb_request *req)
298 spin_lock_irqsave(&audio->lock, flags);
299 list_add_tail(&req->list, &audio->idle_reqs);
300 spin_unlock_irqrestore(&audio->lock, flags);
303 static struct usb_request *audio_req_get(struct audio_dev *audio)
306 struct usb_request *req;
308 spin_lock_irqsave(&audio->lock, flags);
309 if (list_empty(&audio->idle_reqs)) {
312 req = list_first_entry(&audio->idle_reqs, struct usb_request,
314 list_del(&req->list);
316 spin_unlock_irqrestore(&audio->lock, flags);
320 /* send the appropriate number of packets to match our bitrate */
321 static void audio_send(struct audio_dev *audio)
323 struct snd_pcm_runtime *runtime;
324 struct usb_request *req;
325 int length, length1, length2, ret;
330 /* audio->substream will be null if we have been closed */
331 if (!audio->substream)
333 /* audio->buffer_pos will be null if we have been stopped */
334 if (!audio->buffer_pos)
337 runtime = audio->substream->runtime;
339 /* compute number of frames to send */
341 msecs = ktime_to_ns(now) - ktime_to_ns(audio->start_time);
342 do_div(msecs, 1000000);
343 frames = msecs * SAMPLE_RATE;
344 do_div(frames, 1000);
346 /* Readjust our frames_sent if we fall too far behind.
347 * If we get too far behind it is better to drop some frames than
348 * to keep sending data too fast in an attempt to catch up.
350 if (frames - audio->frames_sent > 10 * FRAMES_PER_MSEC)
351 audio->frames_sent = frames - FRAMES_PER_MSEC;
353 frames -= audio->frames_sent;
355 /* We need to send something to keep the pipeline going */
357 frames = FRAMES_PER_MSEC;
360 req = audio_req_get(audio);
364 length = frames_to_bytes(runtime, frames);
365 if (length > IN_EP_MAX_PACKET_SIZE)
366 length = IN_EP_MAX_PACKET_SIZE;
368 if (audio->buffer_pos + length > audio->buffer_end)
369 length1 = audio->buffer_end - audio->buffer_pos;
372 memcpy(req->buf, audio->buffer_pos, length1);
373 if (length1 < length) {
374 /* Wrap around and copy remaining length
375 * at beginning of buffer.
377 length2 = length - length1;
378 memcpy(req->buf + length1, audio->buffer_start,
380 audio->buffer_pos = audio->buffer_start + length2;
382 audio->buffer_pos += length1;
383 if (audio->buffer_pos >= audio->buffer_end)
384 audio->buffer_pos = audio->buffer_start;
387 req->length = length;
388 ret = usb_ep_queue(audio->in_ep, req, GFP_ATOMIC);
390 pr_err("usb_ep_queue failed ret: %d\n", ret);
391 audio_req_put(audio, req);
395 frames -= bytes_to_frames(runtime, length);
396 audio->frames_sent += bytes_to_frames(runtime, length);
400 static void audio_control_complete(struct usb_ep *ep, struct usb_request *req)
402 /* nothing to do here */
405 static void audio_data_complete(struct usb_ep *ep, struct usb_request *req)
407 struct audio_dev *audio = req->context;
409 pr_debug("audio_data_complete req->status %d req->actual %d\n",
410 req->status, req->actual);
412 audio_req_put(audio, req);
414 if (!audio->buffer_start || req->status)
417 audio->period_offset += req->actual;
418 if (audio->period_offset >= audio->period) {
419 snd_pcm_period_elapsed(audio->substream);
420 audio->period_offset = 0;
425 static int audio_set_endpoint_req(struct usb_function *f,
426 const struct usb_ctrlrequest *ctrl)
428 int value = -EOPNOTSUPP;
429 u16 ep = le16_to_cpu(ctrl->wIndex);
430 u16 len = le16_to_cpu(ctrl->wLength);
431 u16 w_value = le16_to_cpu(ctrl->wValue);
433 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
434 ctrl->bRequest, w_value, len, ep);
436 switch (ctrl->bRequest) {
450 static int audio_get_endpoint_req(struct usb_function *f,
451 const struct usb_ctrlrequest *ctrl)
453 struct usb_composite_dev *cdev = f->config->cdev;
454 int value = -EOPNOTSUPP;
455 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
456 u16 len = le16_to_cpu(ctrl->wLength);
457 u16 w_value = le16_to_cpu(ctrl->wValue);
458 u8 *buf = cdev->req->buf;
460 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
461 ctrl->bRequest, w_value, len, ep);
463 if (w_value == UAC_EP_CS_ATTR_SAMPLE_RATE << 8) {
464 switch (ctrl->bRequest) {
469 /* return our sample rate */
470 buf[0] = (u8)SAMPLE_RATE;
471 buf[1] = (u8)(SAMPLE_RATE >> 8);
472 buf[2] = (u8)(SAMPLE_RATE >> 16);
484 audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
486 struct usb_composite_dev *cdev = f->config->cdev;
487 struct usb_request *req = cdev->req;
488 int value = -EOPNOTSUPP;
489 u16 w_index = le16_to_cpu(ctrl->wIndex);
490 u16 w_value = le16_to_cpu(ctrl->wValue);
491 u16 w_length = le16_to_cpu(ctrl->wLength);
493 /* composite driver infrastructure handles everything; interface
494 * activation uses set_alt().
496 switch (ctrl->bRequestType) {
497 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
498 value = audio_set_endpoint_req(f, ctrl);
501 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
502 value = audio_get_endpoint_req(f, ctrl);
506 /* respond with data transfer or status phase? */
508 pr_debug("audio req%02x.%02x v%04x i%04x l%d\n",
509 ctrl->bRequestType, ctrl->bRequest,
510 w_value, w_index, w_length);
513 req->complete = audio_control_complete;
514 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
516 pr_err("audio response on err %d\n", value);
519 /* device either stalls (value < 0) or reports success */
523 static int audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
525 struct audio_dev *audio = func_to_audio(f);
526 struct usb_composite_dev *cdev = f->config->cdev;
529 pr_debug("audio_set_alt intf %d, alt %d\n", intf, alt);
531 ret = config_ep_by_speed(cdev->gadget, f, audio->in_ep);
535 usb_ep_enable(audio->in_ep);
539 static void audio_disable(struct usb_function *f)
541 struct audio_dev *audio = func_to_audio(f);
543 pr_debug("audio_disable\n");
544 usb_ep_disable(audio->in_ep);
547 /*-------------------------------------------------------------------------*/
549 static void audio_build_desc(struct audio_dev *audio)
554 /* Set channel numbers */
555 input_terminal_desc.bNrChannels = 2;
556 as_type_i_desc.bNrChannels = 2;
558 /* Set sample rates */
560 sam_freq = as_type_i_desc.tSamFreq[0];
561 memcpy(sam_freq, &rate, 3);
564 /* audio function driver setup/binding */
566 audio_bind(struct usb_configuration *c, struct usb_function *f)
568 struct usb_composite_dev *cdev = c->cdev;
569 struct audio_dev *audio = func_to_audio(f);
572 struct usb_request *req;
575 audio_build_desc(audio);
577 /* allocate instance-specific interface IDs, and patch descriptors */
578 status = usb_interface_id(c, f);
581 ac_interface_desc.bInterfaceNumber = status;
583 status = usb_interface_id(c, f);
586 as_interface_alt_0_desc.bInterfaceNumber = status;
587 as_interface_alt_1_desc.bInterfaceNumber = status;
591 /* allocate our endpoint */
592 ep = usb_ep_autoconfig(cdev->gadget, &fs_as_in_ep_desc);
596 ep->driver_data = audio; /* claim */
598 if (gadget_is_dualspeed(c->cdev->gadget))
599 hs_as_in_ep_desc.bEndpointAddress =
600 fs_as_in_ep_desc.bEndpointAddress;
602 f->fs_descriptors = fs_audio_desc;
603 f->hs_descriptors = hs_audio_desc;
605 for (i = 0, status = 0; i < IN_EP_REQ_COUNT && status == 0; i++) {
606 req = audio_request_new(ep, IN_EP_MAX_PACKET_SIZE);
608 req->context = audio;
609 req->complete = audio_data_complete;
610 audio_req_put(audio, req);
620 audio_unbind(struct usb_configuration *c, struct usb_function *f)
622 struct audio_dev *audio = func_to_audio(f);
623 struct usb_request *req;
625 while ((req = audio_req_get(audio)))
626 audio_request_free(req, audio->in_ep);
628 snd_card_free_when_closed(audio->card);
631 audio->substream = NULL;
635 static void audio_pcm_playback_start(struct audio_dev *audio)
637 audio->start_time = ktime_get();
638 audio->frames_sent = 0;
642 static void audio_pcm_playback_stop(struct audio_dev *audio)
646 spin_lock_irqsave(&audio->lock, flags);
647 audio->buffer_start = 0;
648 audio->buffer_end = 0;
649 audio->buffer_pos = 0;
650 spin_unlock_irqrestore(&audio->lock, flags);
653 static int audio_pcm_open(struct snd_pcm_substream *substream)
655 struct snd_pcm_runtime *runtime = substream->runtime;
656 struct audio_dev *audio = substream->private_data;
658 runtime->private_data = audio;
659 runtime->hw = audio_hw_info;
660 snd_pcm_limit_hw_rates(runtime);
661 runtime->hw.channels_max = 2;
663 audio->substream = substream;
667 static int audio_pcm_close(struct snd_pcm_substream *substream)
669 struct audio_dev *audio = substream->private_data;
672 spin_lock_irqsave(&audio->lock, flags);
673 audio->substream = NULL;
674 spin_unlock_irqrestore(&audio->lock, flags);
679 static int audio_pcm_hw_params(struct snd_pcm_substream *substream,
680 struct snd_pcm_hw_params *params)
682 unsigned int channels = params_channels(params);
683 unsigned int rate = params_rate(params);
685 if (rate != SAMPLE_RATE)
690 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
691 params_buffer_bytes(params));
694 static int audio_pcm_hw_free(struct snd_pcm_substream *substream)
696 return snd_pcm_lib_free_vmalloc_buffer(substream);
699 static int audio_pcm_prepare(struct snd_pcm_substream *substream)
701 struct snd_pcm_runtime *runtime = substream->runtime;
702 struct audio_dev *audio = runtime->private_data;
704 audio->period = snd_pcm_lib_period_bytes(substream);
705 audio->period_offset = 0;
706 audio->buffer_start = runtime->dma_area;
707 audio->buffer_end = audio->buffer_start
708 + snd_pcm_lib_buffer_bytes(substream);
709 audio->buffer_pos = audio->buffer_start;
714 static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream)
716 struct snd_pcm_runtime *runtime = substream->runtime;
717 struct audio_dev *audio = runtime->private_data;
718 ssize_t bytes = audio->buffer_pos - audio->buffer_start;
720 /* return offset of next frame to fill in our buffer */
721 return bytes_to_frames(runtime, bytes);
724 static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream,
727 struct audio_dev *audio = substream->runtime->private_data;
731 case SNDRV_PCM_TRIGGER_START:
732 case SNDRV_PCM_TRIGGER_RESUME:
733 audio_pcm_playback_start(audio);
736 case SNDRV_PCM_TRIGGER_STOP:
737 case SNDRV_PCM_TRIGGER_SUSPEND:
738 audio_pcm_playback_stop(audio);
748 static struct audio_dev _audio_dev = {
750 .name = "audio_source",
752 .unbind = audio_unbind,
753 .set_alt = audio_set_alt,
754 .setup = audio_setup,
755 .disable = audio_disable,
757 .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock),
758 .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs),
761 static struct snd_pcm_ops audio_playback_ops = {
762 .open = audio_pcm_open,
763 .close = audio_pcm_close,
764 .ioctl = snd_pcm_lib_ioctl,
765 .hw_params = audio_pcm_hw_params,
766 .hw_free = audio_pcm_hw_free,
767 .prepare = audio_pcm_prepare,
768 .trigger = audio_pcm_playback_trigger,
769 .pointer = audio_pcm_pointer,
772 int audio_source_bind_config(struct usb_configuration *c,
773 struct audio_source_config *config)
775 struct audio_dev *audio;
776 struct snd_card *card;
785 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
786 THIS_MODULE, 0, &card);
790 snd_card_set_dev(card, &c->cdev->gadget->dev);
792 err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm);
795 pcm->private_data = audio;
799 strlcpy(pcm->name, "USB gadget audio", sizeof(pcm->name));
801 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &audio_playback_ops);
802 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
805 strlcpy(card->driver, "audio_source", sizeof(card->driver));
806 strlcpy(card->shortname, card->driver, sizeof(card->shortname));
807 strlcpy(card->longname, "USB accessory audio source",
808 sizeof(card->longname));
810 err = snd_card_register(card);
814 err = usb_add_function(c, &audio->func);
818 config->card = pcm->card->number;
819 config->device = pcm->device;
826 snd_card_free(audio->card);