2 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
5 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
7 * Sound card implementation was cut-and-pasted with changes
8 * from f_uac2.c and has:
10 * Yadwinder Singh (yadi.brar01@gmail.com)
11 * Jaswinder Singh (jaswinder.singh@linaro.org)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
31 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
32 #define PRD_SIZE_MAX PAGE_SIZE
36 struct uac_rtd_params *pp; /* parent param */
37 struct usb_request *req;
40 /* Runtime data params for one stream */
41 struct uac_rtd_params {
42 struct snd_uac_chip *uac; /* parent chip */
43 bool ep_enabled; /* if the ep is enabled */
45 struct snd_pcm_substream *ss;
52 unsigned max_psize; /* MaxPacketSize of endpoint */
59 struct g_audio *audio_dev;
61 struct uac_rtd_params p_prm;
62 struct uac_rtd_params c_prm;
64 struct snd_card *card;
67 /* timekeeping for the playback endpoint */
68 unsigned int p_interval;
69 unsigned int p_residue;
71 /* pre-calculated values for playback iso completion */
72 unsigned int p_pktsize;
73 unsigned int p_pktsize_residue;
74 unsigned int p_framesize;
77 static const struct snd_pcm_hardware uac_pcm_hardware = {
78 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
79 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
80 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
81 .rates = SNDRV_PCM_RATE_CONTINUOUS,
82 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
83 .buffer_bytes_max = BUFF_SIZE_MAX,
84 .period_bytes_max = PRD_SIZE_MAX,
85 .periods_min = MIN_PERIODS,
88 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
91 unsigned long flags, flags2;
93 int status = req->status;
94 struct uac_req *ur = req->context;
95 struct snd_pcm_substream *substream;
96 struct snd_pcm_runtime *runtime;
97 struct uac_rtd_params *prm = ur->pp;
98 struct snd_uac_chip *uac = prm->uac;
100 /* i/f shutting down */
101 if (!prm->ep_enabled) {
102 usb_ep_free_request(ep, req);
106 if (req->status == -ESHUTDOWN)
110 * We can't really do much about bad xfers.
111 * Afterall, the ISOCH xfers could fail legitimately.
114 pr_debug("%s: iso_complete status(%d) %d/%d\n",
115 __func__, status, req->actual, req->length);
119 /* Do nothing if ALSA isn't active */
123 snd_pcm_stream_lock_irqsave(substream, flags2);
125 runtime = substream->runtime;
126 if (!runtime || !snd_pcm_running(substream)) {
127 snd_pcm_stream_unlock_irqrestore(substream, flags2);
131 spin_lock_irqsave(&prm->lock, flags);
133 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
135 * For each IN packet, take the quotient of the current data
136 * rate and the endpoint's interval as the base packet size.
137 * If there is a residue from this division, add it to the
138 * residue accumulator.
140 req->length = uac->p_pktsize;
141 uac->p_residue += uac->p_pktsize_residue;
144 * Whenever there are more bytes in the accumulator than we
145 * need to add one more sample frame, increase this packet's
146 * size and decrease the accumulator.
148 if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
149 req->length += uac->p_framesize;
150 uac->p_residue -= uac->p_framesize *
154 req->actual = req->length;
157 hw_ptr = prm->hw_ptr;
159 spin_unlock_irqrestore(&prm->lock, flags);
161 /* Pack USB load in ALSA ring buffer */
162 pending = runtime->dma_bytes - hw_ptr;
164 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
165 if (unlikely(pending < req->actual)) {
166 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
167 memcpy(req->buf + pending, runtime->dma_area,
168 req->actual - pending);
170 memcpy(req->buf, runtime->dma_area + hw_ptr,
174 if (unlikely(pending < req->actual)) {
175 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
176 memcpy(runtime->dma_area, req->buf + pending,
177 req->actual - pending);
179 memcpy(runtime->dma_area + hw_ptr, req->buf,
184 spin_lock_irqsave(&prm->lock, flags);
185 /* update hw_ptr after data is copied to memory */
186 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
187 hw_ptr = prm->hw_ptr;
188 spin_unlock_irqrestore(&prm->lock, flags);
189 snd_pcm_stream_unlock_irqrestore(substream, flags2);
191 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
192 snd_pcm_period_elapsed(substream);
195 if (usb_ep_queue(ep, req, GFP_ATOMIC))
196 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
199 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
201 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
202 struct uac_rtd_params *prm;
203 struct g_audio *audio_dev;
204 struct uac_params *params;
208 audio_dev = uac->audio_dev;
209 params = &audio_dev->params;
211 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
216 spin_lock_irqsave(&prm->lock, flags);
222 case SNDRV_PCM_TRIGGER_START:
223 case SNDRV_PCM_TRIGGER_RESUME:
226 case SNDRV_PCM_TRIGGER_STOP:
227 case SNDRV_PCM_TRIGGER_SUSPEND:
234 spin_unlock_irqrestore(&prm->lock, flags);
236 /* Clear buffer after Play stops */
237 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
238 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
243 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
245 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
246 struct uac_rtd_params *prm;
248 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
253 return bytes_to_frames(substream->runtime, prm->hw_ptr);
256 static int uac_pcm_hw_params(struct snd_pcm_substream *substream,
257 struct snd_pcm_hw_params *hw_params)
259 return snd_pcm_lib_malloc_pages(substream,
260 params_buffer_bytes(hw_params));
263 static int uac_pcm_hw_free(struct snd_pcm_substream *substream)
265 return snd_pcm_lib_free_pages(substream);
268 static int uac_pcm_open(struct snd_pcm_substream *substream)
270 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
271 struct snd_pcm_runtime *runtime = substream->runtime;
272 struct g_audio *audio_dev;
273 struct uac_params *params;
274 int p_ssize, c_ssize;
275 int p_srate, c_srate;
276 int p_chmask, c_chmask;
278 audio_dev = uac->audio_dev;
279 params = &audio_dev->params;
280 p_ssize = params->p_ssize;
281 c_ssize = params->c_ssize;
282 p_srate = params->p_srate;
283 c_srate = params->c_srate;
284 p_chmask = params->p_chmask;
285 c_chmask = params->c_chmask;
288 runtime->hw = uac_pcm_hardware;
290 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
291 spin_lock_init(&uac->p_prm.lock);
292 runtime->hw.rate_min = p_srate;
295 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
298 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
301 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
304 runtime->hw.channels_min = num_channels(p_chmask);
305 runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
306 / runtime->hw.periods_min;
308 spin_lock_init(&uac->c_prm.lock);
309 runtime->hw.rate_min = c_srate;
312 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
315 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
318 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
321 runtime->hw.channels_min = num_channels(c_chmask);
322 runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
323 / runtime->hw.periods_min;
326 runtime->hw.rate_max = runtime->hw.rate_min;
327 runtime->hw.channels_max = runtime->hw.channels_min;
329 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
334 /* ALSA cries without these function pointers */
335 static int uac_pcm_null(struct snd_pcm_substream *substream)
340 static const struct snd_pcm_ops uac_pcm_ops = {
341 .open = uac_pcm_open,
342 .close = uac_pcm_null,
343 .ioctl = snd_pcm_lib_ioctl,
344 .hw_params = uac_pcm_hw_params,
345 .hw_free = uac_pcm_hw_free,
346 .trigger = uac_pcm_trigger,
347 .pointer = uac_pcm_pointer,
348 .prepare = uac_pcm_null,
351 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
353 struct snd_uac_chip *uac = prm->uac;
354 struct g_audio *audio_dev;
355 struct uac_params *params;
358 if (!prm->ep_enabled)
361 audio_dev = uac->audio_dev;
362 params = &audio_dev->params;
364 for (i = 0; i < params->req_number; i++) {
365 if (prm->ureq[i].req) {
366 if (usb_ep_dequeue(ep, prm->ureq[i].req))
367 usb_ep_free_request(ep, prm->ureq[i].req);
369 * If usb_ep_dequeue() cannot successfully dequeue the
370 * request, the request will be freed by the completion
374 prm->ureq[i].req = NULL;
378 prm->ep_enabled = false;
380 if (usb_ep_disable(ep))
381 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
384 int u_audio_start_capture(struct g_audio *audio_dev)
386 struct snd_uac_chip *uac = audio_dev->uac;
387 struct usb_gadget *gadget = audio_dev->gadget;
388 struct device *dev = &gadget->dev;
389 struct usb_request *req;
391 struct uac_rtd_params *prm;
392 struct uac_params *params = &audio_dev->params;
395 ep = audio_dev->out_ep;
397 config_ep_by_speed(gadget, &audio_dev->func, ep);
398 req_len = prm->max_psize;
400 prm->ep_enabled = true;
403 for (i = 0; i < params->req_number; i++) {
404 if (!prm->ureq[i].req) {
405 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
409 prm->ureq[i].req = req;
410 prm->ureq[i].pp = prm;
413 req->context = &prm->ureq[i];
414 req->length = req_len;
415 req->complete = u_audio_iso_complete;
416 req->buf = prm->rbuf + i * prm->max_psize;
419 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
420 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
425 EXPORT_SYMBOL_GPL(u_audio_start_capture);
427 void u_audio_stop_capture(struct g_audio *audio_dev)
429 struct snd_uac_chip *uac = audio_dev->uac;
431 free_ep(&uac->c_prm, audio_dev->out_ep);
433 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
435 int u_audio_start_playback(struct g_audio *audio_dev)
437 struct snd_uac_chip *uac = audio_dev->uac;
438 struct usb_gadget *gadget = audio_dev->gadget;
439 struct device *dev = &gadget->dev;
440 struct usb_request *req;
442 struct uac_rtd_params *prm;
443 struct uac_params *params = &audio_dev->params;
444 unsigned int factor, rate;
445 const struct usb_endpoint_descriptor *ep_desc;
448 ep = audio_dev->in_ep;
450 config_ep_by_speed(gadget, &audio_dev->func, ep);
454 /* pre-calculate the playback endpoint's interval */
455 if (gadget->speed == USB_SPEED_FULL)
460 /* pre-compute some values for iso_complete() */
461 uac->p_framesize = params->p_ssize *
462 num_channels(params->p_chmask);
463 rate = params->p_srate * uac->p_framesize;
464 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
465 uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval,
468 if (uac->p_pktsize < prm->max_psize)
469 uac->p_pktsize_residue = rate % uac->p_interval;
471 uac->p_pktsize_residue = 0;
473 req_len = uac->p_pktsize;
476 prm->ep_enabled = true;
479 for (i = 0; i < params->req_number; i++) {
480 if (!prm->ureq[i].req) {
481 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
485 prm->ureq[i].req = req;
486 prm->ureq[i].pp = prm;
489 req->context = &prm->ureq[i];
490 req->length = req_len;
491 req->complete = u_audio_iso_complete;
492 req->buf = prm->rbuf + i * prm->max_psize;
495 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
496 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
501 EXPORT_SYMBOL_GPL(u_audio_start_playback);
503 void u_audio_stop_playback(struct g_audio *audio_dev)
505 struct snd_uac_chip *uac = audio_dev->uac;
507 free_ep(&uac->p_prm, audio_dev->in_ep);
509 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
511 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
512 const char *card_name)
514 struct snd_uac_chip *uac;
515 struct snd_card *card;
517 struct uac_params *params;
518 int p_chmask, c_chmask;
524 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
528 uac->audio_dev = g_audio;
530 params = &g_audio->params;
531 p_chmask = params->p_chmask;
532 c_chmask = params->c_chmask;
535 struct uac_rtd_params *prm = &uac->c_prm;
537 uac->c_prm.uac = uac;
538 prm->max_psize = g_audio->out_ep_maxpsize;
540 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
547 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
557 struct uac_rtd_params *prm = &uac->p_prm;
559 uac->p_prm.uac = uac;
560 prm->max_psize = g_audio->in_ep_maxpsize;
562 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
569 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
578 /* Choose any slot, with no id */
579 err = snd_card_new(&g_audio->gadget->dev,
580 -1, NULL, THIS_MODULE, 0, &card);
587 * Create first PCM device
588 * Create a substream only for non-zero channel streams
590 err = snd_pcm_new(uac->card, pcm_name, 0,
591 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
595 strlcpy(pcm->name, pcm_name, sizeof(pcm->name));
596 pcm->private_data = uac;
599 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
600 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
602 strlcpy(card->driver, card_name, sizeof(card->driver));
603 strlcpy(card->shortname, card_name, sizeof(card->shortname));
604 sprintf(card->longname, "%s %i", card_name, card->dev->id);
606 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
607 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
609 err = snd_card_register(card);
617 kfree(uac->p_prm.ureq);
618 kfree(uac->c_prm.ureq);
619 kfree(uac->p_prm.rbuf);
620 kfree(uac->c_prm.rbuf);
625 EXPORT_SYMBOL_GPL(g_audio_setup);
627 void g_audio_cleanup(struct g_audio *g_audio)
629 struct snd_uac_chip *uac;
630 struct snd_card *card;
632 if (!g_audio || !g_audio->uac)
640 kfree(uac->p_prm.ureq);
641 kfree(uac->c_prm.ureq);
642 kfree(uac->p_prm.rbuf);
643 kfree(uac->c_prm.rbuf);
646 EXPORT_SYMBOL_GPL(g_audio_cleanup);
648 MODULE_LICENSE("GPL");
649 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
650 MODULE_AUTHOR("Ruslan Bilovol");