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>
19 #include <sound/control.h>
23 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
24 #define PRD_SIZE_MAX PAGE_SIZE
27 /* Runtime data params for one stream */
28 struct uac_rtd_params {
29 struct snd_uac_chip *uac; /* parent chip */
30 bool ep_enabled; /* if the ep is enabled */
32 struct snd_pcm_substream *ss;
39 unsigned int pitch; /* Stream pitch ratio to 1000000 */
40 unsigned int max_psize; /* MaxPacketSize of endpoint */
42 struct usb_request **reqs;
44 struct usb_request *req_fback; /* Feedback endpoint request */
45 bool fb_ep_enabled; /* if the ep is enabled */
49 struct g_audio *audio_dev;
51 struct uac_rtd_params p_prm;
52 struct uac_rtd_params c_prm;
54 struct snd_card *card;
57 /* timekeeping for the playback endpoint */
58 unsigned int p_interval;
59 unsigned int p_residue;
61 /* pre-calculated values for playback iso completion */
62 unsigned int p_pktsize;
63 unsigned int p_pktsize_residue;
64 unsigned int p_framesize;
67 static const struct snd_pcm_hardware uac_pcm_hardware = {
68 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
69 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
70 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
71 .rates = SNDRV_PCM_RATE_CONTINUOUS,
72 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
73 .buffer_bytes_max = BUFF_SIZE_MAX,
74 .period_bytes_max = PRD_SIZE_MAX,
75 .periods_min = MIN_PERIODS,
78 static void u_audio_set_fback_frequency(enum usb_device_speed speed,
79 unsigned long long freq,
86 * Because the pitch base is 1000000, the final divider here
87 * will be 1000 * 1000000 = 1953125 << 9
89 * Instead of dealing with big numbers lets fold this 9 left shift
92 if (speed == USB_SPEED_FULL) {
94 * Full-speed feedback endpoints report frequency
96 * Format is encoded in Q10.10 left-justified in the 24 bits,
97 * so that it has a Q10.14 format.
99 * ff = (freq << 14) / 1000
104 * High-speed feedback endpoints report frequency
105 * in samples/microframe.
106 * Format is encoded in Q12.13 fitted into four bytes so that
107 * the binary point is located between the second and the third
108 * byte fromat (that is Q16.16)
110 * ff = (freq << 16) / 8000
115 ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
117 *(__le32 *)buf = cpu_to_le32(ff);
120 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
122 unsigned int pending;
124 int status = req->status;
125 struct snd_pcm_substream *substream;
126 struct snd_pcm_runtime *runtime;
127 struct uac_rtd_params *prm = req->context;
128 struct snd_uac_chip *uac = prm->uac;
130 /* i/f shutting down */
131 if (!prm->ep_enabled) {
132 usb_ep_free_request(ep, req);
136 if (req->status == -ESHUTDOWN)
140 * We can't really do much about bad xfers.
141 * Afterall, the ISOCH xfers could fail legitimately.
144 pr_debug("%s: iso_complete status(%d) %d/%d\n",
145 __func__, status, req->actual, req->length);
149 /* Do nothing if ALSA isn't active */
153 snd_pcm_stream_lock(substream);
155 runtime = substream->runtime;
156 if (!runtime || !snd_pcm_running(substream)) {
157 snd_pcm_stream_unlock(substream);
161 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
163 * For each IN packet, take the quotient of the current data
164 * rate and the endpoint's interval as the base packet size.
165 * If there is a residue from this division, add it to the
166 * residue accumulator.
168 req->length = uac->p_pktsize;
169 uac->p_residue += uac->p_pktsize_residue;
172 * Whenever there are more bytes in the accumulator than we
173 * need to add one more sample frame, increase this packet's
174 * size and decrease the accumulator.
176 if (uac->p_residue / uac->p_interval >= uac->p_framesize) {
177 req->length += uac->p_framesize;
178 uac->p_residue -= uac->p_framesize *
182 req->actual = req->length;
185 hw_ptr = prm->hw_ptr;
187 /* Pack USB load in ALSA ring buffer */
188 pending = runtime->dma_bytes - hw_ptr;
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
191 if (unlikely(pending < req->actual)) {
192 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
193 memcpy(req->buf + pending, runtime->dma_area,
194 req->actual - pending);
196 memcpy(req->buf, runtime->dma_area + hw_ptr,
200 if (unlikely(pending < req->actual)) {
201 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
202 memcpy(runtime->dma_area, req->buf + pending,
203 req->actual - pending);
205 memcpy(runtime->dma_area + hw_ptr, req->buf,
210 /* update hw_ptr after data is copied to memory */
211 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
212 hw_ptr = prm->hw_ptr;
213 snd_pcm_stream_unlock(substream);
215 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
216 snd_pcm_period_elapsed(substream);
219 if (usb_ep_queue(ep, req, GFP_ATOMIC))
220 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
223 static void u_audio_iso_fback_complete(struct usb_ep *ep,
224 struct usb_request *req)
226 struct uac_rtd_params *prm = req->context;
227 struct snd_uac_chip *uac = prm->uac;
228 struct g_audio *audio_dev = uac->audio_dev;
229 struct uac_params *params = &audio_dev->params;
230 int status = req->status;
232 /* i/f shutting down */
233 if (!prm->fb_ep_enabled) {
235 usb_ep_free_request(ep, req);
239 if (req->status == -ESHUTDOWN)
243 * We can't really do much about bad xfers.
244 * Afterall, the ISOCH xfers could fail legitimately.
247 pr_debug("%s: iso_complete status(%d) %d/%d\n",
248 __func__, status, req->actual, req->length);
250 u_audio_set_fback_frequency(audio_dev->gadget->speed,
251 params->c_srate, prm->pitch,
254 if (usb_ep_queue(ep, req, GFP_ATOMIC))
255 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
258 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
260 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
261 struct uac_rtd_params *prm;
262 struct g_audio *audio_dev;
263 struct uac_params *params;
266 audio_dev = uac->audio_dev;
267 params = &audio_dev->params;
269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
278 case SNDRV_PCM_TRIGGER_START:
279 case SNDRV_PCM_TRIGGER_RESUME:
282 case SNDRV_PCM_TRIGGER_STOP:
283 case SNDRV_PCM_TRIGGER_SUSPEND:
290 /* Clear buffer after Play stops */
291 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
292 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
297 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
299 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
300 struct uac_rtd_params *prm;
302 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
307 return bytes_to_frames(substream->runtime, prm->hw_ptr);
310 static u64 uac_ssize_to_fmt(int ssize)
316 ret = SNDRV_PCM_FMTBIT_S24_3LE;
319 ret = SNDRV_PCM_FMTBIT_S32_LE;
322 ret = SNDRV_PCM_FMTBIT_S16_LE;
329 static int uac_pcm_open(struct snd_pcm_substream *substream)
331 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
332 struct snd_pcm_runtime *runtime = substream->runtime;
333 struct g_audio *audio_dev;
334 struct uac_params *params;
335 int p_ssize, c_ssize;
336 int p_srate, c_srate;
337 int p_chmask, c_chmask;
339 audio_dev = uac->audio_dev;
340 params = &audio_dev->params;
341 p_ssize = params->p_ssize;
342 c_ssize = params->c_ssize;
343 p_srate = params->p_srate;
344 c_srate = params->c_srate;
345 p_chmask = params->p_chmask;
346 c_chmask = params->c_chmask;
349 runtime->hw = uac_pcm_hardware;
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
352 runtime->hw.rate_min = p_srate;
353 runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
354 runtime->hw.channels_min = num_channels(p_chmask);
355 runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
356 / runtime->hw.periods_min;
358 runtime->hw.rate_min = c_srate;
359 runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
360 runtime->hw.channels_min = num_channels(c_chmask);
361 runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
362 / runtime->hw.periods_min;
365 runtime->hw.rate_max = runtime->hw.rate_min;
366 runtime->hw.channels_max = runtime->hw.channels_min;
368 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
373 /* ALSA cries without these function pointers */
374 static int uac_pcm_null(struct snd_pcm_substream *substream)
379 static const struct snd_pcm_ops uac_pcm_ops = {
380 .open = uac_pcm_open,
381 .close = uac_pcm_null,
382 .trigger = uac_pcm_trigger,
383 .pointer = uac_pcm_pointer,
384 .prepare = uac_pcm_null,
387 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
389 struct snd_uac_chip *uac = prm->uac;
390 struct g_audio *audio_dev;
391 struct uac_params *params;
394 if (!prm->ep_enabled)
397 audio_dev = uac->audio_dev;
398 params = &audio_dev->params;
400 for (i = 0; i < params->req_number; i++) {
402 if (usb_ep_dequeue(ep, prm->reqs[i]))
403 usb_ep_free_request(ep, prm->reqs[i]);
405 * If usb_ep_dequeue() cannot successfully dequeue the
406 * request, the request will be freed by the completion
414 prm->ep_enabled = false;
416 if (usb_ep_disable(ep))
417 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
420 static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
422 struct snd_uac_chip *uac = prm->uac;
424 if (!prm->fb_ep_enabled)
427 if (prm->req_fback) {
428 if (usb_ep_dequeue(ep, prm->req_fback)) {
429 kfree(prm->req_fback->buf);
430 usb_ep_free_request(ep, prm->req_fback);
432 prm->req_fback = NULL;
435 prm->fb_ep_enabled = false;
437 if (usb_ep_disable(ep))
438 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
441 int u_audio_start_capture(struct g_audio *audio_dev)
443 struct snd_uac_chip *uac = audio_dev->uac;
444 struct usb_gadget *gadget = audio_dev->gadget;
445 struct device *dev = &gadget->dev;
446 struct usb_request *req, *req_fback;
447 struct usb_ep *ep, *ep_fback;
448 struct uac_rtd_params *prm;
449 struct uac_params *params = &audio_dev->params;
452 ep = audio_dev->out_ep;
454 config_ep_by_speed(gadget, &audio_dev->func, ep);
455 req_len = ep->maxpacket;
457 prm->ep_enabled = true;
460 for (i = 0; i < params->req_number; i++) {
462 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
470 req->length = req_len;
471 req->complete = u_audio_iso_complete;
472 req->buf = prm->rbuf + i * ep->maxpacket;
475 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
476 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
479 ep_fback = audio_dev->in_ep_fback;
483 /* Setup feedback endpoint */
484 config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
485 prm->fb_ep_enabled = true;
486 usb_ep_enable(ep_fback);
487 req_len = ep_fback->maxpacket;
489 req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
490 if (req_fback == NULL)
493 prm->req_fback = req_fback;
495 req_fback->context = prm;
496 req_fback->length = req_len;
497 req_fback->complete = u_audio_iso_fback_complete;
499 req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
504 * Configure the feedback endpoint's reported frequency.
505 * Always start with original frequency since its deviation can't
506 * be meauserd at start of playback
508 prm->pitch = 1000000;
509 u_audio_set_fback_frequency(audio_dev->gadget->speed,
510 params->c_srate, prm->pitch,
513 if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
514 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
518 EXPORT_SYMBOL_GPL(u_audio_start_capture);
520 void u_audio_stop_capture(struct g_audio *audio_dev)
522 struct snd_uac_chip *uac = audio_dev->uac;
524 if (audio_dev->in_ep_fback)
525 free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
526 free_ep(&uac->c_prm, audio_dev->out_ep);
528 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
530 int u_audio_start_playback(struct g_audio *audio_dev)
532 struct snd_uac_chip *uac = audio_dev->uac;
533 struct usb_gadget *gadget = audio_dev->gadget;
534 struct device *dev = &gadget->dev;
535 struct usb_request *req;
537 struct uac_rtd_params *prm;
538 struct uac_params *params = &audio_dev->params;
540 const struct usb_endpoint_descriptor *ep_desc;
543 ep = audio_dev->in_ep;
545 config_ep_by_speed(gadget, &audio_dev->func, ep);
549 /* pre-calculate the playback endpoint's interval */
550 if (gadget->speed == USB_SPEED_FULL)
555 /* pre-compute some values for iso_complete() */
556 uac->p_framesize = params->p_ssize *
557 num_channels(params->p_chmask);
558 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
559 uac->p_pktsize = min_t(unsigned int,
561 (params->p_srate / uac->p_interval),
564 if (uac->p_pktsize < ep->maxpacket)
565 uac->p_pktsize_residue = uac->p_framesize *
566 (params->p_srate % uac->p_interval);
568 uac->p_pktsize_residue = 0;
570 req_len = uac->p_pktsize;
573 prm->ep_enabled = true;
576 for (i = 0; i < params->req_number; i++) {
578 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
586 req->length = req_len;
587 req->complete = u_audio_iso_complete;
588 req->buf = prm->rbuf + i * ep->maxpacket;
591 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
592 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
597 EXPORT_SYMBOL_GPL(u_audio_start_playback);
599 void u_audio_stop_playback(struct g_audio *audio_dev)
601 struct snd_uac_chip *uac = audio_dev->uac;
603 free_ep(&uac->p_prm, audio_dev->in_ep);
605 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
607 static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
608 struct snd_ctl_elem_info *uinfo)
610 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
611 struct snd_uac_chip *uac = prm->uac;
612 struct g_audio *audio_dev = uac->audio_dev;
613 struct uac_params *params = &audio_dev->params;
614 unsigned int pitch_min, pitch_max;
616 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
617 pitch_max = (1000 + params->fb_max) * 1000;
619 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
621 uinfo->value.integer.min = pitch_min;
622 uinfo->value.integer.max = pitch_max;
623 uinfo->value.integer.step = 1;
627 static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
628 struct snd_ctl_elem_value *ucontrol)
630 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
632 ucontrol->value.integer.value[0] = prm->pitch;
637 static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
638 struct snd_ctl_elem_value *ucontrol)
640 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
641 struct snd_uac_chip *uac = prm->uac;
642 struct g_audio *audio_dev = uac->audio_dev;
643 struct uac_params *params = &audio_dev->params;
645 unsigned int pitch_min, pitch_max;
648 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
649 pitch_max = (1000 + params->fb_max) * 1000;
651 val = ucontrol->value.integer.value[0];
658 if (prm->pitch != val) {
666 static const struct snd_kcontrol_new u_audio_controls[] = {
668 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
669 .name = "Capture Pitch 1000000",
670 .info = u_audio_pitch_info,
671 .get = u_audio_pitch_get,
672 .put = u_audio_pitch_put,
676 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
677 const char *card_name)
679 struct snd_uac_chip *uac;
680 struct snd_card *card;
682 struct snd_kcontrol *kctl;
683 struct uac_params *params;
684 int p_chmask, c_chmask;
690 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
694 uac->audio_dev = g_audio;
696 params = &g_audio->params;
697 p_chmask = params->p_chmask;
698 c_chmask = params->c_chmask;
701 struct uac_rtd_params *prm = &uac->c_prm;
703 uac->c_prm.uac = uac;
704 prm->max_psize = g_audio->out_ep_maxpsize;
706 prm->reqs = kcalloc(params->req_number,
707 sizeof(struct usb_request *),
714 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
724 struct uac_rtd_params *prm = &uac->p_prm;
726 uac->p_prm.uac = uac;
727 prm->max_psize = g_audio->in_ep_maxpsize;
729 prm->reqs = kcalloc(params->req_number,
730 sizeof(struct usb_request *),
737 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
746 /* Choose any slot, with no id */
747 err = snd_card_new(&g_audio->gadget->dev,
748 -1, NULL, THIS_MODULE, 0, &card);
755 * Create first PCM device
756 * Create a substream only for non-zero channel streams
758 err = snd_pcm_new(uac->card, pcm_name, 0,
759 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
763 strscpy(pcm->name, pcm_name, sizeof(pcm->name));
764 pcm->private_data = uac;
767 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
768 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
770 if (c_chmask && g_audio->in_ep_fback) {
771 strscpy(card->mixername, card_name, sizeof(card->driver));
773 kctl = snd_ctl_new1(&u_audio_controls[0], &uac->c_prm);
779 kctl->id.device = pcm->device;
780 kctl->id.subdevice = 0;
782 err = snd_ctl_add(card, kctl);
787 strscpy(card->driver, card_name, sizeof(card->driver));
788 strscpy(card->shortname, card_name, sizeof(card->shortname));
789 sprintf(card->longname, "%s %i", card_name, card->dev->id);
791 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
792 NULL, 0, BUFF_SIZE_MAX);
794 err = snd_card_register(card);
802 kfree(uac->p_prm.reqs);
803 kfree(uac->c_prm.reqs);
804 kfree(uac->p_prm.rbuf);
805 kfree(uac->c_prm.rbuf);
810 EXPORT_SYMBOL_GPL(g_audio_setup);
812 void g_audio_cleanup(struct g_audio *g_audio)
814 struct snd_uac_chip *uac;
815 struct snd_card *card;
817 if (!g_audio || !g_audio->uac)
825 kfree(uac->p_prm.reqs);
826 kfree(uac->c_prm.reqs);
827 kfree(uac->p_prm.rbuf);
828 kfree(uac->c_prm.rbuf);
831 EXPORT_SYMBOL_GPL(g_audio_cleanup);
833 MODULE_LICENSE("GPL");
834 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
835 MODULE_AUTHOR("Ruslan Bilovol");