1 // SPDX-License-Identifier: GPL-2.0+
3 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
6 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
8 * Sound card implementation was cut-and-pasted with changes
9 * from f_uac2.c and has:
11 * Yadwinder Singh (yadi.brar01@gmail.com)
12 * Jaswinder Singh (jaswinder.singh@linaro.org)
15 #include <linux/module.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
22 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
23 #define PRD_SIZE_MAX PAGE_SIZE
27 struct uac_rtd_params *pp; /* parent param */
28 struct usb_request *req;
31 /* Runtime data params for one stream */
32 struct uac_rtd_params {
33 struct snd_uac_chip *uac; /* parent chip */
34 bool ep_enabled; /* if the ep is enabled */
36 struct snd_pcm_substream *ss;
43 unsigned max_psize; /* MaxPacketSize of endpoint */
50 struct g_audio *audio_dev;
52 struct uac_rtd_params p_prm;
53 struct uac_rtd_params c_prm;
55 struct snd_card *card;
58 /* timekeeping for the playback endpoint */
59 unsigned int p_interval;
60 unsigned int p_residue;
62 /* pre-calculated values for playback iso completion */
63 unsigned int p_pktsize;
64 unsigned int p_pktsize_residue;
65 unsigned int p_framesize;
68 static const struct snd_pcm_hardware uac_pcm_hardware = {
69 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
70 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
71 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
72 .rates = SNDRV_PCM_RATE_CONTINUOUS,
73 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
74 .buffer_bytes_max = BUFF_SIZE_MAX,
75 .period_bytes_max = PRD_SIZE_MAX,
76 .periods_min = MIN_PERIODS,
79 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
82 unsigned long flags, flags2;
84 int status = req->status;
85 struct uac_req *ur = req->context;
86 struct snd_pcm_substream *substream;
87 struct snd_pcm_runtime *runtime;
88 struct uac_rtd_params *prm = ur->pp;
89 struct snd_uac_chip *uac = prm->uac;
91 /* i/f shutting down */
92 if (!prm->ep_enabled) {
93 usb_ep_free_request(ep, req);
97 if (req->status == -ESHUTDOWN)
101 * We can't really do much about bad xfers.
102 * Afterall, the ISOCH xfers could fail legitimately.
105 pr_debug("%s: iso_complete status(%d) %d/%d\n",
106 __func__, status, req->actual, req->length);
110 /* Do nothing if ALSA isn't active */
114 snd_pcm_stream_lock_irqsave(substream, flags2);
116 runtime = substream->runtime;
117 if (!runtime || !snd_pcm_running(substream)) {
118 snd_pcm_stream_unlock_irqrestore(substream, flags2);
122 spin_lock_irqsave(&prm->lock, flags);
124 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
126 * For each IN packet, take the quotient of the current data
127 * rate and the endpoint's interval as the base packet size.
128 * If there is a residue from this division, add it to the
129 * residue accumulator.
131 req->length = uac->p_pktsize;
132 uac->p_residue += uac->p_pktsize_residue;
135 * Whenever there are more bytes in the accumulator than we
136 * need to add one more sample frame, increase this packet's
137 * size and decrease the accumulator.
139 if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
140 req->length += uac->p_framesize;
141 uac->p_residue -= uac->p_framesize *
145 req->actual = req->length;
148 hw_ptr = prm->hw_ptr;
150 spin_unlock_irqrestore(&prm->lock, flags);
152 /* Pack USB load in ALSA ring buffer */
153 pending = runtime->dma_bytes - hw_ptr;
155 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
156 if (unlikely(pending < req->actual)) {
157 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
158 memcpy(req->buf + pending, runtime->dma_area,
159 req->actual - pending);
161 memcpy(req->buf, runtime->dma_area + hw_ptr,
165 if (unlikely(pending < req->actual)) {
166 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
167 memcpy(runtime->dma_area, req->buf + pending,
168 req->actual - pending);
170 memcpy(runtime->dma_area + hw_ptr, req->buf,
175 spin_lock_irqsave(&prm->lock, flags);
176 /* update hw_ptr after data is copied to memory */
177 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
178 hw_ptr = prm->hw_ptr;
179 spin_unlock_irqrestore(&prm->lock, flags);
180 snd_pcm_stream_unlock_irqrestore(substream, flags2);
182 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
183 snd_pcm_period_elapsed(substream);
186 if (usb_ep_queue(ep, req, GFP_ATOMIC))
187 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
190 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
192 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
193 struct uac_rtd_params *prm;
194 struct g_audio *audio_dev;
195 struct uac_params *params;
199 audio_dev = uac->audio_dev;
200 params = &audio_dev->params;
202 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
207 spin_lock_irqsave(&prm->lock, flags);
213 case SNDRV_PCM_TRIGGER_START:
214 case SNDRV_PCM_TRIGGER_RESUME:
217 case SNDRV_PCM_TRIGGER_STOP:
218 case SNDRV_PCM_TRIGGER_SUSPEND:
225 spin_unlock_irqrestore(&prm->lock, flags);
227 /* Clear buffer after Play stops */
228 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
229 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
234 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
236 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
237 struct uac_rtd_params *prm;
239 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
244 return bytes_to_frames(substream->runtime, prm->hw_ptr);
247 static int uac_pcm_hw_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *hw_params)
250 return snd_pcm_lib_malloc_pages(substream,
251 params_buffer_bytes(hw_params));
254 static int uac_pcm_hw_free(struct snd_pcm_substream *substream)
256 return snd_pcm_lib_free_pages(substream);
259 static int uac_pcm_open(struct snd_pcm_substream *substream)
261 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
262 struct snd_pcm_runtime *runtime = substream->runtime;
263 struct g_audio *audio_dev;
264 struct uac_params *params;
265 int p_ssize, c_ssize;
266 int p_srate, c_srate;
267 int p_chmask, c_chmask;
269 audio_dev = uac->audio_dev;
270 params = &audio_dev->params;
271 p_ssize = params->p_ssize;
272 c_ssize = params->c_ssize;
273 p_srate = params->p_srate;
274 c_srate = params->c_srate;
275 p_chmask = params->p_chmask;
276 c_chmask = params->c_chmask;
279 runtime->hw = uac_pcm_hardware;
281 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
282 spin_lock_init(&uac->p_prm.lock);
283 runtime->hw.rate_min = p_srate;
286 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
289 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
292 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
295 runtime->hw.channels_min = num_channels(p_chmask);
296 runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
297 / runtime->hw.periods_min;
299 spin_lock_init(&uac->c_prm.lock);
300 runtime->hw.rate_min = c_srate;
303 runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE;
306 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
309 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
312 runtime->hw.channels_min = num_channels(c_chmask);
313 runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
314 / runtime->hw.periods_min;
317 runtime->hw.rate_max = runtime->hw.rate_min;
318 runtime->hw.channels_max = runtime->hw.channels_min;
320 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
325 /* ALSA cries without these function pointers */
326 static int uac_pcm_null(struct snd_pcm_substream *substream)
331 static const struct snd_pcm_ops uac_pcm_ops = {
332 .open = uac_pcm_open,
333 .close = uac_pcm_null,
334 .ioctl = snd_pcm_lib_ioctl,
335 .hw_params = uac_pcm_hw_params,
336 .hw_free = uac_pcm_hw_free,
337 .trigger = uac_pcm_trigger,
338 .pointer = uac_pcm_pointer,
339 .prepare = uac_pcm_null,
342 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
344 struct snd_uac_chip *uac = prm->uac;
345 struct g_audio *audio_dev;
346 struct uac_params *params;
349 if (!prm->ep_enabled)
352 audio_dev = uac->audio_dev;
353 params = &audio_dev->params;
355 for (i = 0; i < params->req_number; i++) {
356 if (prm->ureq[i].req) {
357 if (usb_ep_dequeue(ep, prm->ureq[i].req))
358 usb_ep_free_request(ep, prm->ureq[i].req);
360 * If usb_ep_dequeue() cannot successfully dequeue the
361 * request, the request will be freed by the completion
365 prm->ureq[i].req = NULL;
369 prm->ep_enabled = false;
371 if (usb_ep_disable(ep))
372 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
375 int u_audio_start_capture(struct g_audio *audio_dev)
377 struct snd_uac_chip *uac = audio_dev->uac;
378 struct usb_gadget *gadget = audio_dev->gadget;
379 struct device *dev = &gadget->dev;
380 struct usb_request *req;
382 struct uac_rtd_params *prm;
383 struct uac_params *params = &audio_dev->params;
386 ep = audio_dev->out_ep;
388 config_ep_by_speed(gadget, &audio_dev->func, ep);
389 req_len = prm->max_psize;
391 prm->ep_enabled = true;
394 for (i = 0; i < params->req_number; i++) {
395 if (!prm->ureq[i].req) {
396 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
400 prm->ureq[i].req = req;
401 prm->ureq[i].pp = prm;
404 req->context = &prm->ureq[i];
405 req->length = req_len;
406 req->complete = u_audio_iso_complete;
407 req->buf = prm->rbuf + i * prm->max_psize;
410 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
411 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
416 EXPORT_SYMBOL_GPL(u_audio_start_capture);
418 void u_audio_stop_capture(struct g_audio *audio_dev)
420 struct snd_uac_chip *uac = audio_dev->uac;
422 free_ep(&uac->c_prm, audio_dev->out_ep);
424 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
426 int u_audio_start_playback(struct g_audio *audio_dev)
428 struct snd_uac_chip *uac = audio_dev->uac;
429 struct usb_gadget *gadget = audio_dev->gadget;
430 struct device *dev = &gadget->dev;
431 struct usb_request *req;
433 struct uac_rtd_params *prm;
434 struct uac_params *params = &audio_dev->params;
435 unsigned int factor, rate;
436 const struct usb_endpoint_descriptor *ep_desc;
439 ep = audio_dev->in_ep;
441 config_ep_by_speed(gadget, &audio_dev->func, ep);
445 /* pre-calculate the playback endpoint's interval */
446 if (gadget->speed == USB_SPEED_FULL)
451 /* pre-compute some values for iso_complete() */
452 uac->p_framesize = params->p_ssize *
453 num_channels(params->p_chmask);
454 rate = params->p_srate * uac->p_framesize;
455 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
456 uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval,
459 if (uac->p_pktsize < prm->max_psize)
460 uac->p_pktsize_residue = rate % uac->p_interval;
462 uac->p_pktsize_residue = 0;
464 req_len = uac->p_pktsize;
467 prm->ep_enabled = true;
470 for (i = 0; i < params->req_number; i++) {
471 if (!prm->ureq[i].req) {
472 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
476 prm->ureq[i].req = req;
477 prm->ureq[i].pp = prm;
480 req->context = &prm->ureq[i];
481 req->length = req_len;
482 req->complete = u_audio_iso_complete;
483 req->buf = prm->rbuf + i * prm->max_psize;
486 if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC))
487 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
492 EXPORT_SYMBOL_GPL(u_audio_start_playback);
494 void u_audio_stop_playback(struct g_audio *audio_dev)
496 struct snd_uac_chip *uac = audio_dev->uac;
498 free_ep(&uac->p_prm, audio_dev->in_ep);
500 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
502 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
503 const char *card_name)
505 struct snd_uac_chip *uac;
506 struct snd_card *card;
508 struct uac_params *params;
509 int p_chmask, c_chmask;
515 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
519 uac->audio_dev = g_audio;
521 params = &g_audio->params;
522 p_chmask = params->p_chmask;
523 c_chmask = params->c_chmask;
526 struct uac_rtd_params *prm = &uac->c_prm;
528 uac->c_prm.uac = uac;
529 prm->max_psize = g_audio->out_ep_maxpsize;
531 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
538 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
548 struct uac_rtd_params *prm = &uac->p_prm;
550 uac->p_prm.uac = uac;
551 prm->max_psize = g_audio->in_ep_maxpsize;
553 prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req),
560 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
569 /* Choose any slot, with no id */
570 err = snd_card_new(&g_audio->gadget->dev,
571 -1, NULL, THIS_MODULE, 0, &card);
578 * Create first PCM device
579 * Create a substream only for non-zero channel streams
581 err = snd_pcm_new(uac->card, pcm_name, 0,
582 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
586 strlcpy(pcm->name, pcm_name, sizeof(pcm->name));
587 pcm->private_data = uac;
590 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
591 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
593 strlcpy(card->driver, card_name, sizeof(card->driver));
594 strlcpy(card->shortname, card_name, sizeof(card->shortname));
595 sprintf(card->longname, "%s %i", card_name, card->dev->id);
597 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
598 snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX);
600 err = snd_card_register(card);
608 kfree(uac->p_prm.ureq);
609 kfree(uac->c_prm.ureq);
610 kfree(uac->p_prm.rbuf);
611 kfree(uac->c_prm.rbuf);
616 EXPORT_SYMBOL_GPL(g_audio_setup);
618 void g_audio_cleanup(struct g_audio *g_audio)
620 struct snd_uac_chip *uac;
621 struct snd_card *card;
623 if (!g_audio || !g_audio->uac)
629 snd_card_free_when_closed(card);
631 kfree(uac->p_prm.ureq);
632 kfree(uac->c_prm.ureq);
633 kfree(uac->p_prm.rbuf);
634 kfree(uac->c_prm.rbuf);
637 EXPORT_SYMBOL_GPL(g_audio_cleanup);
639 MODULE_LICENSE("GPL");
640 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
641 MODULE_AUTHOR("Ruslan Bilovol");