1 /*
2 * Gadget Function Driver for USB audio source device
3 *
4 * Copyright (C) 2012 Google, Inc.
5 *
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.
9 *
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.
14 *
15 */
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,
40 .bNumEndpoints = 0,
41 .bInterfaceClass = USB_CLASS_AUDIO,
42 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
43 };
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,
60 .baInterfaceNr = {
61 [0] = AUDIO_AC_INTERFACE,
62 [1] = AUDIO_AS_INTERFACE,
63 }
64 };
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,
73 .bAssocTerminal = 0,
74 .wChannelConfig = 0x3,
75 };
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,
86 .bControlSize = 2,
87 };
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,
98 };
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,
105 .bNumEndpoints = 0,
106 .bInterfaceClass = USB_CLASS_AUDIO,
107 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
108 };
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,
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_AUDIO,
116 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
117 };
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,
125 .bDelay = 1,
126 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
127 };
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,
136 .bSubframeSize = 2,
137 .bBitResolution = 16,
138 .bSamFreqType = 1,
139 };
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 */
150 };
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 */
161 };
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,
168 .bmAttributes = 1,
169 .bLockDelayUnits = 1,
170 .wLockDelay = __constant_cpu_to_le16(1),
171 };
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,
189 NULL,
190 };
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,
208 NULL,
209 };
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,
219 .channels_min = 2,
220 .channels_max = 2,
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,
227 .periods_min = 2,
228 .periods_max = 1024,
229 };
231 /*-------------------------------------------------------------------------*/
233 struct audio_source_config {
234 int card;
235 int device;
236 };
238 struct audio_dev {
239 struct usb_function func;
240 struct snd_card *card;
241 struct snd_pcm *pcm;
242 struct snd_pcm_substream *substream;
244 struct list_head idle_reqs;
245 struct usb_ep *in_ep;
247 spinlock_t lock;
249 /* beginning, end and current position in our buffer */
250 void *buffer_start;
251 void *buffer_end;
252 void *buffer_pos;
254 /* byte size of a "period" */
255 unsigned int period;
256 /* bytes sent since last call to snd_pcm_period_elapsed */
257 unsigned int period_offset;
258 /* time we started playing */
259 ktime_t start_time;
260 /* number of frames sent since start_time */
261 s64 frames_sent;
262 };
264 static inline struct audio_dev *func_to_audio(struct usb_function *f)
265 {
266 return container_of(f, struct audio_dev, func);
267 }
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size)
272 {
273 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
274 if (!req)
275 return NULL;
277 req->buf = kmalloc(buffer_size, GFP_KERNEL);
278 if (!req->buf) {
279 usb_ep_free_request(ep, req);
280 return NULL;
281 }
282 req->length = buffer_size;
283 return req;
284 }
286 static void audio_request_free(struct usb_request *req, struct usb_ep *ep)
287 {
288 if (req) {
289 kfree(req->buf);
290 usb_ep_free_request(ep, req);
291 }
292 }
294 static void audio_req_put(struct audio_dev *audio, struct usb_request *req)
295 {
296 unsigned long flags;
298 spin_lock_irqsave(&audio->lock, flags);
299 list_add_tail(&req->list, &audio->idle_reqs);
300 spin_unlock_irqrestore(&audio->lock, flags);
301 }
303 static struct usb_request *audio_req_get(struct audio_dev *audio)
304 {
305 unsigned long flags;
306 struct usb_request *req;
308 spin_lock_irqsave(&audio->lock, flags);
309 if (list_empty(&audio->idle_reqs)) {
310 req = 0;
311 } else {
312 req = list_first_entry(&audio->idle_reqs, struct usb_request,
313 list);
314 list_del(&req->list);
315 }
316 spin_unlock_irqrestore(&audio->lock, flags);
317 return req;
318 }
320 /* send the appropriate number of packets to match our bitrate */
321 static void audio_send(struct audio_dev *audio)
322 {
323 struct snd_pcm_runtime *runtime;
324 struct usb_request *req;
325 int length, length1, length2, ret;
326 s64 msecs;
327 s64 frames;
328 ktime_t now;
330 /* audio->substream will be null if we have been closed */
331 if (!audio->substream)
332 return;
333 /* audio->buffer_pos will be null if we have been stopped */
334 if (!audio->buffer_pos)
335 return;
337 runtime = audio->substream->runtime;
339 /* compute number of frames to send */
340 now = ktime_get();
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.
349 */
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 */
356 if (frames <= 0)
357 frames = FRAMES_PER_MSEC;
359 while (frames > 0) {
360 req = audio_req_get(audio);
361 if (!req)
362 break;
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;
370 else
371 length1 = length;
372 memcpy(req->buf, audio->buffer_pos, length1);
373 if (length1 < length) {
374 /* Wrap around and copy remaining length
375 * at beginning of buffer.
376 */
377 length2 = length - length1;
378 memcpy(req->buf + length1, audio->buffer_start,
379 length2);
380 audio->buffer_pos = audio->buffer_start + length2;
381 } else {
382 audio->buffer_pos += length1;
383 if (audio->buffer_pos >= audio->buffer_end)
384 audio->buffer_pos = audio->buffer_start;
385 }
387 req->length = length;
388 ret = usb_ep_queue(audio->in_ep, req, GFP_ATOMIC);
389 if (ret < 0) {
390 pr_err("usb_ep_queue failed ret: %d\n", ret);
391 audio_req_put(audio, req);
392 break;
393 }
395 frames -= bytes_to_frames(runtime, length);
396 audio->frames_sent += bytes_to_frames(runtime, length);
397 }
398 }
400 static void audio_control_complete(struct usb_ep *ep, struct usb_request *req)
401 {
402 /* nothing to do here */
403 }
405 static void audio_data_complete(struct usb_ep *ep, struct usb_request *req)
406 {
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)
415 return;
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;
421 }
422 audio_send(audio);
423 }
425 static int audio_set_endpoint_req(struct usb_function *f,
426 const struct usb_ctrlrequest *ctrl)
427 {
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) {
437 case UAC_SET_CUR:
438 case UAC_SET_MIN:
439 case UAC_SET_MAX:
440 case UAC_SET_RES:
441 value = len;
442 break;
443 default:
444 break;
445 }
447 return value;
448 }
450 static int audio_get_endpoint_req(struct usb_function *f,
451 const struct usb_ctrlrequest *ctrl)
452 {
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) {
465 case UAC_GET_CUR:
466 case UAC_GET_MIN:
467 case UAC_GET_MAX:
468 case UAC_GET_RES:
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);
473 value = 3;
474 break;
475 default:
476 break;
477 }
478 }
480 return value;
481 }
483 static int
484 audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
485 {
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().
495 */
496 switch (ctrl->bRequestType) {
497 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
498 value = audio_set_endpoint_req(f, ctrl);
499 break;
501 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
502 value = audio_get_endpoint_req(f, ctrl);
503 break;
504 }
506 /* respond with data transfer or status phase? */
507 if (value >= 0) {
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);
511 req->zero = 0;
512 req->length = value;
513 req->complete = audio_control_complete;
514 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
515 if (value < 0)
516 pr_err("audio response on err %d\n", value);
517 }
519 /* device either stalls (value < 0) or reports success */
520 return value;
521 }
523 static int audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
524 {
525 struct audio_dev *audio = func_to_audio(f);
526 struct usb_composite_dev *cdev = f->config->cdev;
527 int ret;
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);
532 if (ret)
533 return ret;
535 usb_ep_enable(audio->in_ep);
536 return 0;
537 }
539 static void audio_disable(struct usb_function *f)
540 {
541 struct audio_dev *audio = func_to_audio(f);
543 pr_debug("audio_disable\n");
544 usb_ep_disable(audio->in_ep);
545 }
547 /*-------------------------------------------------------------------------*/
549 static void audio_build_desc(struct audio_dev *audio)
550 {
551 u8 *sam_freq;
552 int rate;
554 /* Set channel numbers */
555 input_terminal_desc.bNrChannels = 2;
556 as_type_i_desc.bNrChannels = 2;
558 /* Set sample rates */
559 rate = SAMPLE_RATE;
560 sam_freq = as_type_i_desc.tSamFreq[0];
561 memcpy(sam_freq, &rate, 3);
562 }
564 /* audio function driver setup/binding */
565 static int
566 audio_bind(struct usb_configuration *c, struct usb_function *f)
567 {
568 struct usb_composite_dev *cdev = c->cdev;
569 struct audio_dev *audio = func_to_audio(f);
570 int status;
571 struct usb_ep *ep;
572 struct usb_request *req;
573 int i;
575 audio_build_desc(audio);
577 /* allocate instance-specific interface IDs, and patch descriptors */
578 status = usb_interface_id(c, f);
579 if (status < 0)
580 goto fail;
581 ac_interface_desc.bInterfaceNumber = status;
583 status = usb_interface_id(c, f);
584 if (status < 0)
585 goto fail;
586 as_interface_alt_0_desc.bInterfaceNumber = status;
587 as_interface_alt_1_desc.bInterfaceNumber = status;
589 status = -ENODEV;
591 /* allocate our endpoint */
592 ep = usb_ep_autoconfig(cdev->gadget, &fs_as_in_ep_desc);
593 if (!ep)
594 goto fail;
595 audio->in_ep = ep;
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);
607 if (req) {
608 req->context = audio;
609 req->complete = audio_data_complete;
610 audio_req_put(audio, req);
611 } else
612 status = -ENOMEM;
613 }
615 fail:
616 return status;
617 }
619 static void
620 audio_unbind(struct usb_configuration *c, struct usb_function *f)
621 {
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);
629 audio->card = NULL;
630 audio->pcm = NULL;
631 audio->substream = NULL;
632 audio->in_ep = NULL;
633 }
635 static void audio_pcm_playback_start(struct audio_dev *audio)
636 {
637 audio->start_time = ktime_get();
638 audio->frames_sent = 0;
639 audio_send(audio);
640 }
642 static void audio_pcm_playback_stop(struct audio_dev *audio)
643 {
644 unsigned long flags;
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);
651 }
653 static int audio_pcm_open(struct snd_pcm_substream *substream)
654 {
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;
664 return 0;
665 }
667 static int audio_pcm_close(struct snd_pcm_substream *substream)
668 {
669 struct audio_dev *audio = substream->private_data;
670 unsigned long flags;
672 spin_lock_irqsave(&audio->lock, flags);
673 audio->substream = NULL;
674 spin_unlock_irqrestore(&audio->lock, flags);
676 return 0;
677 }
679 static int audio_pcm_hw_params(struct snd_pcm_substream *substream,
680 struct snd_pcm_hw_params *params)
681 {
682 unsigned int channels = params_channels(params);
683 unsigned int rate = params_rate(params);
685 if (rate != SAMPLE_RATE)
686 return -EINVAL;
687 if (channels != 2)
688 return -EINVAL;
690 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
691 params_buffer_bytes(params));
692 }
694 static int audio_pcm_hw_free(struct snd_pcm_substream *substream)
695 {
696 return snd_pcm_lib_free_vmalloc_buffer(substream);
697 }
699 static int audio_pcm_prepare(struct snd_pcm_substream *substream)
700 {
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;
711 return 0;
712 }
714 static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream)
715 {
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);
722 }
724 static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream,
725 int cmd)
726 {
727 struct audio_dev *audio = substream->runtime->private_data;
728 int ret = 0;
730 switch (cmd) {
731 case SNDRV_PCM_TRIGGER_START:
732 case SNDRV_PCM_TRIGGER_RESUME:
733 audio_pcm_playback_start(audio);
734 break;
736 case SNDRV_PCM_TRIGGER_STOP:
737 case SNDRV_PCM_TRIGGER_SUSPEND:
738 audio_pcm_playback_stop(audio);
739 break;
741 default:
742 ret = -EINVAL;
743 }
745 return ret;
746 }
748 static struct audio_dev _audio_dev = {
749 .func = {
750 .name = "audio_source",
751 .bind = audio_bind,
752 .unbind = audio_unbind,
753 .set_alt = audio_set_alt,
754 .setup = audio_setup,
755 .disable = audio_disable,
756 },
757 .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock),
758 .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs),
759 };
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,
770 };
772 int audio_source_bind_config(struct usb_configuration *c,
773 struct audio_source_config *config)
774 {
775 struct audio_dev *audio;
776 struct snd_card *card;
777 struct snd_pcm *pcm;
778 int err;
780 config->card = -1;
781 config->device = -1;
783 audio = &_audio_dev;
785 err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
786 THIS_MODULE, 0, &card);
787 if (err)
788 return err;
790 snd_card_set_dev(card, &c->cdev->gadget->dev);
792 err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm);
793 if (err)
794 goto pcm_fail;
795 pcm->private_data = audio;
796 pcm->info_flags = 0;
797 audio->pcm = pcm;
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,
803 NULL, 0, 64 * 1024);
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);
811 if (err)
812 goto register_fail;
814 err = usb_add_function(c, &audio->func);
815 if (err)
816 goto add_fail;
818 config->card = pcm->card->number;
819 config->device = pcm->device;
820 audio->card = card;
821 return 0;
823 add_fail:
824 register_fail:
825 pcm_fail:
826 snd_card_free(audio->card);
827 return err;
828 }