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/kernel.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/control.h>
21 #include <sound/tlv.h>
22 #include <linux/usb/audio.h>
26 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
27 #define PRD_SIZE_MAX PAGE_SIZE
38 /* Runtime data params for one stream */
39 struct uac_rtd_params {
40 struct snd_uac_chip *uac; /* parent chip */
41 bool ep_enabled; /* if the ep is enabled */
43 struct snd_pcm_substream *ss;
50 unsigned int pitch; /* Stream pitch ratio to 1000000 */
51 unsigned int max_psize; /* MaxPacketSize of endpoint */
53 struct usb_request **reqs;
55 struct usb_request *req_fback; /* Feedback endpoint request */
56 bool fb_ep_enabled; /* if the ep is enabled */
58 /* Volume/Mute controls and their state */
59 int fu_id; /* Feature Unit ID */
60 struct snd_kcontrol *snd_kctl_volume;
61 struct snd_kcontrol *snd_kctl_mute;
62 s16 volume_min, volume_max, volume_res;
66 struct snd_kcontrol *snd_kctl_rate; /* read-only current rate */
67 int srate; /* selected samplerate */
68 int active; /* playback/capture running */
70 spinlock_t lock; /* lock for control transfers */
75 struct g_audio *audio_dev;
77 struct uac_rtd_params p_prm;
78 struct uac_rtd_params c_prm;
80 struct snd_card *card;
83 /* pre-calculated values for playback iso completion */
84 unsigned long long p_residue_mil;
85 unsigned int p_interval;
86 unsigned int p_framesize;
89 static const struct snd_pcm_hardware uac_pcm_hardware = {
90 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
91 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
92 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
93 .rates = SNDRV_PCM_RATE_CONTINUOUS,
94 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
95 .buffer_bytes_max = BUFF_SIZE_MAX,
96 .period_bytes_max = PRD_SIZE_MAX,
97 .periods_min = MIN_PERIODS,
100 static void u_audio_set_fback_frequency(enum usb_device_speed speed,
101 struct usb_ep *out_ep,
102 unsigned long long freq,
107 const struct usb_endpoint_descriptor *ep_desc;
110 * Because the pitch base is 1000000, the final divider here
111 * will be 1000 * 1000000 = 1953125 << 9
113 * Instead of dealing with big numbers lets fold this 9 left shift
116 if (speed == USB_SPEED_FULL) {
118 * Full-speed feedback endpoints report frequency
120 * Format is encoded in Q10.10 left-justified in the 24 bits,
121 * so that it has a Q10.14 format.
123 * ff = (freq << 14) / 1000
128 * High-speed feedback endpoints report frequency
129 * in samples/microframe.
130 * Format is encoded in Q12.13 fitted into four bytes so that
131 * the binary point is located between the second and the third
132 * byte fromat (that is Q16.16)
134 * ff = (freq << 16) / 8000
136 * Win10 and OSX UAC2 drivers require number of samples per packet
137 * in order to honor the feedback value.
138 * Linux snd-usb-audio detects the applied bit-shift automatically.
140 ep_desc = out_ep->desc;
141 freq <<= 4 + (ep_desc->bInterval - 1);
144 ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
146 *(__le32 *)buf = cpu_to_le32(ff);
149 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
151 unsigned int pending;
153 int status = req->status;
154 struct snd_pcm_substream *substream;
155 struct snd_pcm_runtime *runtime;
156 struct uac_rtd_params *prm = req->context;
157 struct snd_uac_chip *uac = prm->uac;
158 unsigned int frames, p_pktsize;
159 unsigned long long pitched_rate_mil, p_pktsize_residue_mil,
160 residue_frames_mil, div_result;
162 /* i/f shutting down */
163 if (!prm->ep_enabled) {
164 usb_ep_free_request(ep, req);
168 if (req->status == -ESHUTDOWN)
172 * We can't really do much about bad xfers.
173 * Afterall, the ISOCH xfers could fail legitimately.
176 pr_debug("%s: iso_complete status(%d) %d/%d\n",
177 __func__, status, req->actual, req->length);
181 /* Do nothing if ALSA isn't active */
185 snd_pcm_stream_lock(substream);
187 runtime = substream->runtime;
188 if (!runtime || !snd_pcm_running(substream)) {
189 snd_pcm_stream_unlock(substream);
193 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
195 * For each IN packet, take the quotient of the current data
196 * rate and the endpoint's interval as the base packet size.
197 * If there is a residue from this division, add it to the
198 * residue accumulator.
200 unsigned long long p_interval_mil = uac->p_interval * 1000000ULL;
202 pitched_rate_mil = (unsigned long long) prm->srate * prm->pitch;
203 div_result = pitched_rate_mil;
204 do_div(div_result, uac->p_interval);
205 do_div(div_result, 1000000);
206 frames = (unsigned int) div_result;
208 pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n",
209 prm->srate, prm->pitch, p_interval_mil, frames);
211 p_pktsize = min_t(unsigned int,
212 uac->p_framesize * frames,
215 if (p_pktsize < ep->maxpacket) {
216 residue_frames_mil = pitched_rate_mil - frames * p_interval_mil;
217 p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil;
219 p_pktsize_residue_mil = 0;
221 req->length = p_pktsize;
222 uac->p_residue_mil += p_pktsize_residue_mil;
225 * Whenever there are more bytes in the accumulator p_residue_mil than we
226 * need to add one more sample frame, increase this packet's
227 * size and decrease the accumulator.
229 div_result = uac->p_residue_mil;
230 do_div(div_result, uac->p_interval);
231 do_div(div_result, 1000000);
232 if ((unsigned int) div_result >= uac->p_framesize) {
233 req->length += uac->p_framesize;
234 uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
235 pr_debug("increased req length to %d\n", req->length);
237 pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
239 req->actual = req->length;
242 hw_ptr = prm->hw_ptr;
244 /* Pack USB load in ALSA ring buffer */
245 pending = runtime->dma_bytes - hw_ptr;
247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
248 if (unlikely(pending < req->actual)) {
249 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
250 memcpy(req->buf + pending, runtime->dma_area,
251 req->actual - pending);
253 memcpy(req->buf, runtime->dma_area + hw_ptr,
257 if (unlikely(pending < req->actual)) {
258 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
259 memcpy(runtime->dma_area, req->buf + pending,
260 req->actual - pending);
262 memcpy(runtime->dma_area + hw_ptr, req->buf,
267 /* update hw_ptr after data is copied to memory */
268 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
269 hw_ptr = prm->hw_ptr;
270 snd_pcm_stream_unlock(substream);
272 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
273 snd_pcm_period_elapsed(substream);
276 if (usb_ep_queue(ep, req, GFP_ATOMIC))
277 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
280 static void u_audio_iso_fback_complete(struct usb_ep *ep,
281 struct usb_request *req)
283 struct uac_rtd_params *prm = req->context;
284 struct snd_uac_chip *uac = prm->uac;
285 struct g_audio *audio_dev = uac->audio_dev;
286 int status = req->status;
288 /* i/f shutting down */
289 if (!prm->fb_ep_enabled) {
291 usb_ep_free_request(ep, req);
295 if (req->status == -ESHUTDOWN)
299 * We can't really do much about bad xfers.
300 * Afterall, the ISOCH xfers could fail legitimately.
303 pr_debug("%s: iso_complete status(%d) %d/%d\n",
304 __func__, status, req->actual, req->length);
306 u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep,
307 prm->srate, prm->pitch,
310 if (usb_ep_queue(ep, req, GFP_ATOMIC))
311 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
314 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
316 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
317 struct uac_rtd_params *prm;
318 struct g_audio *audio_dev;
319 struct uac_params *params;
322 audio_dev = uac->audio_dev;
323 params = &audio_dev->params;
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
334 case SNDRV_PCM_TRIGGER_START:
335 case SNDRV_PCM_TRIGGER_RESUME:
338 case SNDRV_PCM_TRIGGER_STOP:
339 case SNDRV_PCM_TRIGGER_SUSPEND:
346 /* Clear buffer after Play stops */
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
348 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
353 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
355 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
356 struct uac_rtd_params *prm;
358 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
363 return bytes_to_frames(substream->runtime, prm->hw_ptr);
366 static u64 uac_ssize_to_fmt(int ssize)
372 ret = SNDRV_PCM_FMTBIT_S24_3LE;
375 ret = SNDRV_PCM_FMTBIT_S32_LE;
378 ret = SNDRV_PCM_FMTBIT_S16_LE;
385 static int uac_pcm_open(struct snd_pcm_substream *substream)
387 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
388 struct snd_pcm_runtime *runtime = substream->runtime;
389 struct g_audio *audio_dev;
390 struct uac_params *params;
391 struct uac_rtd_params *prm;
392 int p_ssize, c_ssize;
393 int p_chmask, c_chmask;
395 audio_dev = uac->audio_dev;
396 params = &audio_dev->params;
397 p_ssize = params->p_ssize;
398 c_ssize = params->c_ssize;
399 p_chmask = params->p_chmask;
400 c_chmask = params->c_chmask;
401 uac->p_residue_mil = 0;
403 runtime->hw = uac_pcm_hardware;
405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
406 runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
407 runtime->hw.channels_min = num_channels(p_chmask);
410 runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
411 runtime->hw.channels_min = num_channels(c_chmask);
415 runtime->hw.period_bytes_min = 2 * prm->max_psize
416 / runtime->hw.periods_min;
417 runtime->hw.rate_min = prm->srate;
418 runtime->hw.rate_max = runtime->hw.rate_min;
419 runtime->hw.channels_max = runtime->hw.channels_min;
421 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
426 /* ALSA cries without these function pointers */
427 static int uac_pcm_null(struct snd_pcm_substream *substream)
432 static const struct snd_pcm_ops uac_pcm_ops = {
433 .open = uac_pcm_open,
434 .close = uac_pcm_null,
435 .trigger = uac_pcm_trigger,
436 .pointer = uac_pcm_pointer,
437 .prepare = uac_pcm_null,
440 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
442 struct snd_uac_chip *uac = prm->uac;
443 struct g_audio *audio_dev;
444 struct uac_params *params;
447 if (!prm->ep_enabled)
450 audio_dev = uac->audio_dev;
451 params = &audio_dev->params;
453 for (i = 0; i < params->req_number; i++) {
455 if (usb_ep_dequeue(ep, prm->reqs[i]))
456 usb_ep_free_request(ep, prm->reqs[i]);
458 * If usb_ep_dequeue() cannot successfully dequeue the
459 * request, the request will be freed by the completion
467 prm->ep_enabled = false;
469 if (usb_ep_disable(ep))
470 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
473 static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
475 struct snd_uac_chip *uac = prm->uac;
477 if (!prm->fb_ep_enabled)
480 if (prm->req_fback) {
481 if (usb_ep_dequeue(ep, prm->req_fback)) {
482 kfree(prm->req_fback->buf);
483 usb_ep_free_request(ep, prm->req_fback);
485 prm->req_fback = NULL;
488 prm->fb_ep_enabled = false;
490 if (usb_ep_disable(ep))
491 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
494 static void set_active(struct uac_rtd_params *prm, bool active)
496 // notifying through the Rate ctrl
497 struct snd_kcontrol *kctl = prm->snd_kctl_rate;
500 spin_lock_irqsave(&prm->lock, flags);
501 if (prm->active != active) {
502 prm->active = active;
503 snd_ctl_notify(prm->uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
506 spin_unlock_irqrestore(&prm->lock, flags);
509 int u_audio_set_capture_srate(struct g_audio *audio_dev, int srate)
511 struct uac_params *params = &audio_dev->params;
512 struct snd_uac_chip *uac = audio_dev->uac;
513 struct uac_rtd_params *prm;
517 dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate);
519 for (i = 0; i < UAC_MAX_RATES; i++) {
520 if (params->c_srates[i] == srate) {
521 spin_lock_irqsave(&prm->lock, flags);
523 spin_unlock_irqrestore(&prm->lock, flags);
526 if (params->c_srates[i] == 0)
532 EXPORT_SYMBOL_GPL(u_audio_set_capture_srate);
534 int u_audio_get_capture_srate(struct g_audio *audio_dev, u32 *val)
536 struct snd_uac_chip *uac = audio_dev->uac;
537 struct uac_rtd_params *prm;
541 spin_lock_irqsave(&prm->lock, flags);
543 spin_unlock_irqrestore(&prm->lock, flags);
546 EXPORT_SYMBOL_GPL(u_audio_get_capture_srate);
548 int u_audio_set_playback_srate(struct g_audio *audio_dev, int srate)
550 struct uac_params *params = &audio_dev->params;
551 struct snd_uac_chip *uac = audio_dev->uac;
552 struct uac_rtd_params *prm;
556 dev_dbg(&audio_dev->gadget->dev, "%s: srate %d\n", __func__, srate);
558 for (i = 0; i < UAC_MAX_RATES; i++) {
559 if (params->p_srates[i] == srate) {
560 spin_lock_irqsave(&prm->lock, flags);
562 spin_unlock_irqrestore(&prm->lock, flags);
565 if (params->p_srates[i] == 0)
571 EXPORT_SYMBOL_GPL(u_audio_set_playback_srate);
573 int u_audio_get_playback_srate(struct g_audio *audio_dev, u32 *val)
575 struct snd_uac_chip *uac = audio_dev->uac;
576 struct uac_rtd_params *prm;
580 spin_lock_irqsave(&prm->lock, flags);
582 spin_unlock_irqrestore(&prm->lock, flags);
585 EXPORT_SYMBOL_GPL(u_audio_get_playback_srate);
587 int u_audio_start_capture(struct g_audio *audio_dev)
589 struct snd_uac_chip *uac = audio_dev->uac;
590 struct usb_gadget *gadget = audio_dev->gadget;
591 struct device *dev = &gadget->dev;
592 struct usb_request *req, *req_fback;
593 struct usb_ep *ep, *ep_fback;
594 struct uac_rtd_params *prm;
595 struct uac_params *params = &audio_dev->params;
599 dev_dbg(dev, "start capture with rate %d\n", prm->srate);
600 ep = audio_dev->out_ep;
601 config_ep_by_speed(gadget, &audio_dev->func, ep);
602 req_len = ep->maxpacket;
604 prm->ep_enabled = true;
607 for (i = 0; i < params->req_number; i++) {
609 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
617 req->length = req_len;
618 req->complete = u_audio_iso_complete;
619 req->buf = prm->rbuf + i * ep->maxpacket;
622 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
623 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
626 set_active(&uac->c_prm, true);
628 ep_fback = audio_dev->in_ep_fback;
632 /* Setup feedback endpoint */
633 config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
634 prm->fb_ep_enabled = true;
635 usb_ep_enable(ep_fback);
636 req_len = ep_fback->maxpacket;
638 req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
639 if (req_fback == NULL)
642 prm->req_fback = req_fback;
644 req_fback->context = prm;
645 req_fback->length = req_len;
646 req_fback->complete = u_audio_iso_fback_complete;
648 req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
653 * Configure the feedback endpoint's reported frequency.
654 * Always start with original frequency since its deviation can't
655 * be meauserd at start of playback
657 prm->pitch = 1000000;
658 u_audio_set_fback_frequency(audio_dev->gadget->speed, ep,
659 prm->srate, prm->pitch,
662 if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
663 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
667 EXPORT_SYMBOL_GPL(u_audio_start_capture);
669 void u_audio_stop_capture(struct g_audio *audio_dev)
671 struct snd_uac_chip *uac = audio_dev->uac;
673 set_active(&uac->c_prm, false);
674 if (audio_dev->in_ep_fback)
675 free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
676 free_ep(&uac->c_prm, audio_dev->out_ep);
678 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
680 int u_audio_start_playback(struct g_audio *audio_dev)
682 struct snd_uac_chip *uac = audio_dev->uac;
683 struct usb_gadget *gadget = audio_dev->gadget;
684 struct device *dev = &gadget->dev;
685 struct usb_request *req;
687 struct uac_rtd_params *prm;
688 struct uac_params *params = &audio_dev->params;
690 const struct usb_endpoint_descriptor *ep_desc;
692 unsigned int p_pktsize;
695 dev_dbg(dev, "start playback with rate %d\n", prm->srate);
696 ep = audio_dev->in_ep;
697 config_ep_by_speed(gadget, &audio_dev->func, ep);
701 * Always start with original frequency
703 prm->pitch = 1000000;
705 /* pre-calculate the playback endpoint's interval */
706 if (gadget->speed == USB_SPEED_FULL)
711 /* pre-compute some values for iso_complete() */
712 uac->p_framesize = params->p_ssize *
713 num_channels(params->p_chmask);
714 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
715 p_pktsize = min_t(unsigned int,
717 (prm->srate / uac->p_interval),
721 uac->p_residue_mil = 0;
723 prm->ep_enabled = true;
726 for (i = 0; i < params->req_number; i++) {
728 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
736 req->length = req_len;
737 req->complete = u_audio_iso_complete;
738 req->buf = prm->rbuf + i * ep->maxpacket;
741 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
742 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
745 set_active(&uac->p_prm, true);
749 EXPORT_SYMBOL_GPL(u_audio_start_playback);
751 void u_audio_stop_playback(struct g_audio *audio_dev)
753 struct snd_uac_chip *uac = audio_dev->uac;
755 set_active(&uac->p_prm, false);
756 free_ep(&uac->p_prm, audio_dev->in_ep);
758 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
760 void u_audio_suspend(struct g_audio *audio_dev)
762 struct snd_uac_chip *uac = audio_dev->uac;
764 set_active(&uac->p_prm, false);
765 set_active(&uac->c_prm, false);
767 EXPORT_SYMBOL_GPL(u_audio_suspend);
769 int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val)
771 struct snd_uac_chip *uac = audio_dev->uac;
772 struct uac_rtd_params *prm;
780 spin_lock_irqsave(&prm->lock, flags);
782 spin_unlock_irqrestore(&prm->lock, flags);
786 EXPORT_SYMBOL_GPL(u_audio_get_volume);
788 int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val)
790 struct snd_uac_chip *uac = audio_dev->uac;
791 struct uac_rtd_params *prm;
800 spin_lock_irqsave(&prm->lock, flags);
801 val = clamp(val, prm->volume_min, prm->volume_max);
802 if (prm->volume != val) {
806 spin_unlock_irqrestore(&prm->lock, flags);
809 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
810 &prm->snd_kctl_volume->id);
814 EXPORT_SYMBOL_GPL(u_audio_set_volume);
816 int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val)
818 struct snd_uac_chip *uac = audio_dev->uac;
819 struct uac_rtd_params *prm;
827 spin_lock_irqsave(&prm->lock, flags);
829 spin_unlock_irqrestore(&prm->lock, flags);
833 EXPORT_SYMBOL_GPL(u_audio_get_mute);
835 int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val)
837 struct snd_uac_chip *uac = audio_dev->uac;
838 struct uac_rtd_params *prm;
850 spin_lock_irqsave(&prm->lock, flags);
851 if (prm->mute != mute) {
855 spin_unlock_irqrestore(&prm->lock, flags);
858 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
859 &prm->snd_kctl_mute->id);
863 EXPORT_SYMBOL_GPL(u_audio_set_mute);
866 static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
867 struct snd_ctl_elem_info *uinfo)
869 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
870 struct snd_uac_chip *uac = prm->uac;
871 struct g_audio *audio_dev = uac->audio_dev;
872 struct uac_params *params = &audio_dev->params;
873 unsigned int pitch_min, pitch_max;
875 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
876 pitch_max = (1000 + params->fb_max) * 1000;
878 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
880 uinfo->value.integer.min = pitch_min;
881 uinfo->value.integer.max = pitch_max;
882 uinfo->value.integer.step = 1;
886 static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
887 struct snd_ctl_elem_value *ucontrol)
889 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
891 ucontrol->value.integer.value[0] = prm->pitch;
896 static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
897 struct snd_ctl_elem_value *ucontrol)
899 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
900 struct snd_uac_chip *uac = prm->uac;
901 struct g_audio *audio_dev = uac->audio_dev;
902 struct uac_params *params = &audio_dev->params;
904 unsigned int pitch_min, pitch_max;
907 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
908 pitch_max = (1000 + params->fb_max) * 1000;
910 val = ucontrol->value.integer.value[0];
917 if (prm->pitch != val) {
925 static int u_audio_mute_info(struct snd_kcontrol *kcontrol,
926 struct snd_ctl_elem_info *uinfo)
928 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
930 uinfo->value.integer.min = 0;
931 uinfo->value.integer.max = 1;
932 uinfo->value.integer.step = 1;
937 static int u_audio_mute_get(struct snd_kcontrol *kcontrol,
938 struct snd_ctl_elem_value *ucontrol)
940 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
943 spin_lock_irqsave(&prm->lock, flags);
944 ucontrol->value.integer.value[0] = !prm->mute;
945 spin_unlock_irqrestore(&prm->lock, flags);
950 static int u_audio_mute_put(struct snd_kcontrol *kcontrol,
951 struct snd_ctl_elem_value *ucontrol)
953 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
954 struct snd_uac_chip *uac = prm->uac;
955 struct g_audio *audio_dev = uac->audio_dev;
960 val = !ucontrol->value.integer.value[0];
962 spin_lock_irqsave(&prm->lock, flags);
963 if (val != prm->mute) {
967 spin_unlock_irqrestore(&prm->lock, flags);
969 if (change && audio_dev->notify)
970 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE);
976 * TLV callback for mixer volume controls
978 static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
979 unsigned int size, unsigned int __user *_tlv)
981 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
982 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
984 if (size < sizeof(scale))
987 /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */
988 scale[2] = (prm->volume_min * 100) / 256;
989 scale[3] = (prm->volume_max * 100) / 256;
990 if (copy_to_user(_tlv, scale, sizeof(scale)))
996 static int u_audio_volume_info(struct snd_kcontrol *kcontrol,
997 struct snd_ctl_elem_info *uinfo)
999 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
1001 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1003 uinfo->value.integer.min = 0;
1004 uinfo->value.integer.max =
1005 (prm->volume_max - prm->volume_min + prm->volume_res - 1)
1007 uinfo->value.integer.step = 1;
1012 static int u_audio_volume_get(struct snd_kcontrol *kcontrol,
1013 struct snd_ctl_elem_value *ucontrol)
1015 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
1016 unsigned long flags;
1018 spin_lock_irqsave(&prm->lock, flags);
1019 ucontrol->value.integer.value[0] =
1020 (prm->volume - prm->volume_min) / prm->volume_res;
1021 spin_unlock_irqrestore(&prm->lock, flags);
1026 static int u_audio_volume_put(struct snd_kcontrol *kcontrol,
1027 struct snd_ctl_elem_value *ucontrol)
1029 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
1030 struct snd_uac_chip *uac = prm->uac;
1031 struct g_audio *audio_dev = uac->audio_dev;
1034 unsigned long flags;
1037 val = ucontrol->value.integer.value[0];
1039 spin_lock_irqsave(&prm->lock, flags);
1040 volume = (val * prm->volume_res) + prm->volume_min;
1041 volume = clamp(volume, prm->volume_min, prm->volume_max);
1042 if (volume != prm->volume) {
1043 prm->volume = volume;
1046 spin_unlock_irqrestore(&prm->lock, flags);
1048 if (change && audio_dev->notify)
1049 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME);
1054 static int get_max_srate(const int *srates)
1056 int i, max_srate = 0;
1058 for (i = 0; i < UAC_MAX_RATES; i++) {
1061 if (srates[i] > max_srate)
1062 max_srate = srates[i];
1067 static int get_min_srate(const int *srates)
1069 int i, min_srate = INT_MAX;
1071 for (i = 0; i < UAC_MAX_RATES; i++) {
1074 if (srates[i] < min_srate)
1075 min_srate = srates[i];
1080 static int u_audio_rate_info(struct snd_kcontrol *kcontrol,
1081 struct snd_ctl_elem_info *uinfo)
1084 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
1085 struct snd_uac_chip *uac = prm->uac;
1086 struct g_audio *audio_dev = uac->audio_dev;
1087 struct uac_params *params = &audio_dev->params;
1089 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1092 if (prm == &uac->c_prm)
1093 srates = params->c_srates;
1095 srates = params->p_srates;
1096 uinfo->value.integer.min = get_min_srate(srates);
1097 uinfo->value.integer.max = get_max_srate(srates);
1101 static int u_audio_rate_get(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_value *ucontrol)
1104 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
1105 unsigned long flags;
1107 spin_lock_irqsave(&prm->lock, flags);
1109 ucontrol->value.integer.value[0] = prm->srate;
1111 /* not active: reporting zero rate */
1112 ucontrol->value.integer.value[0] = 0;
1113 spin_unlock_irqrestore(&prm->lock, flags);
1117 static struct snd_kcontrol_new u_audio_controls[] = {
1119 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1120 .name = "Capture Pitch 1000000",
1121 .info = u_audio_pitch_info,
1122 .get = u_audio_pitch_get,
1123 .put = u_audio_pitch_put,
1125 [UAC_P_PITCH_CTRL] {
1126 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1127 .name = "Playback Pitch 1000000",
1128 .info = u_audio_pitch_info,
1129 .get = u_audio_pitch_get,
1130 .put = u_audio_pitch_put,
1133 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134 .name = "", /* will be filled later */
1135 .info = u_audio_mute_info,
1136 .get = u_audio_mute_get,
1137 .put = u_audio_mute_put,
1140 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1141 .name = "", /* will be filled later */
1142 .info = u_audio_volume_info,
1143 .get = u_audio_volume_get,
1144 .put = u_audio_volume_put,
1147 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1148 .name = "", /* will be filled later */
1149 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1150 .info = u_audio_rate_info,
1151 .get = u_audio_rate_get,
1155 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
1156 const char *card_name)
1158 struct snd_uac_chip *uac;
1159 struct snd_card *card;
1160 struct snd_pcm *pcm;
1161 struct snd_kcontrol *kctl;
1162 struct uac_params *params;
1163 int p_chmask, c_chmask;
1169 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
1173 uac->audio_dev = g_audio;
1175 params = &g_audio->params;
1176 p_chmask = params->p_chmask;
1177 c_chmask = params->c_chmask;
1180 struct uac_rtd_params *prm = &uac->c_prm;
1182 spin_lock_init(&prm->lock);
1183 uac->c_prm.uac = uac;
1184 prm->max_psize = g_audio->out_ep_maxpsize;
1185 prm->srate = params->c_srates[0];
1187 prm->reqs = kcalloc(params->req_number,
1188 sizeof(struct usb_request *),
1195 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
1205 struct uac_rtd_params *prm = &uac->p_prm;
1207 spin_lock_init(&prm->lock);
1208 uac->p_prm.uac = uac;
1209 prm->max_psize = g_audio->in_ep_maxpsize;
1210 prm->srate = params->p_srates[0];
1212 prm->reqs = kcalloc(params->req_number,
1213 sizeof(struct usb_request *),
1220 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
1229 /* Choose any slot, with no id */
1230 err = snd_card_new(&g_audio->gadget->dev,
1231 -1, NULL, THIS_MODULE, 0, &card);
1238 * Create first PCM device
1239 * Create a substream only for non-zero channel streams
1241 err = snd_pcm_new(uac->card, pcm_name, 0,
1242 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
1246 strscpy(pcm->name, pcm_name, sizeof(pcm->name));
1247 pcm->private_data = uac;
1250 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
1251 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
1254 * Create mixer and controls
1255 * Create only if it's required on USB side
1257 if ((c_chmask && g_audio->in_ep_fback)
1258 || (p_chmask && params->p_fu.id)
1259 || (c_chmask && params->c_fu.id))
1260 strscpy(card->mixername, card_name, sizeof(card->driver));
1262 if (c_chmask && g_audio->in_ep_fback) {
1263 kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL],
1270 kctl->id.device = pcm->device;
1271 kctl->id.subdevice = 0;
1273 err = snd_ctl_add(card, kctl);
1279 kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL],
1286 kctl->id.device = pcm->device;
1287 kctl->id.subdevice = 0;
1289 err = snd_ctl_add(card, kctl);
1294 for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) {
1295 struct uac_rtd_params *prm;
1296 struct uac_fu_params *fu;
1300 if (!pcm->streams[i].substream_count)
1303 if (i == SNDRV_PCM_STREAM_PLAYBACK) {
1306 direction = "Playback";
1310 direction = "Capture";
1313 prm->fu_id = fu->id;
1315 if (fu->mute_present) {
1316 snprintf(ctrl_name, sizeof(ctrl_name),
1317 "PCM %s Switch", direction);
1319 u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name;
1321 kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL],
1328 kctl->id.device = pcm->device;
1329 kctl->id.subdevice = 0;
1331 err = snd_ctl_add(card, kctl);
1334 prm->snd_kctl_mute = kctl;
1338 if (fu->volume_present) {
1339 snprintf(ctrl_name, sizeof(ctrl_name),
1340 "PCM %s Volume", direction);
1342 u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name;
1344 kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL],
1351 kctl->id.device = pcm->device;
1352 kctl->id.subdevice = 0;
1355 kctl->tlv.c = u_audio_volume_tlv;
1356 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1357 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1359 err = snd_ctl_add(card, kctl);
1362 prm->snd_kctl_volume = kctl;
1363 prm->volume = fu->volume_max;
1364 prm->volume_max = fu->volume_max;
1365 prm->volume_min = fu->volume_min;
1366 prm->volume_res = fu->volume_res;
1369 /* Add rate control */
1370 snprintf(ctrl_name, sizeof(ctrl_name),
1371 "%s Rate", direction);
1372 u_audio_controls[UAC_RATE_CTRL].name = ctrl_name;
1374 kctl = snd_ctl_new1(&u_audio_controls[UAC_RATE_CTRL], prm);
1380 kctl->id.device = pcm->device;
1381 kctl->id.subdevice = 0;
1383 err = snd_ctl_add(card, kctl);
1386 prm->snd_kctl_rate = kctl;
1389 strscpy(card->driver, card_name, sizeof(card->driver));
1390 strscpy(card->shortname, card_name, sizeof(card->shortname));
1391 sprintf(card->longname, "%s %i", card_name, card->dev->id);
1393 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1394 NULL, 0, BUFF_SIZE_MAX);
1396 err = snd_card_register(card);
1402 snd_card_free(card);
1404 kfree(uac->p_prm.reqs);
1405 kfree(uac->c_prm.reqs);
1406 kfree(uac->p_prm.rbuf);
1407 kfree(uac->c_prm.rbuf);
1412 EXPORT_SYMBOL_GPL(g_audio_setup);
1414 void g_audio_cleanup(struct g_audio *g_audio)
1416 struct snd_uac_chip *uac;
1417 struct snd_card *card;
1419 if (!g_audio || !g_audio->uac)
1425 snd_card_free(card);
1427 kfree(uac->p_prm.reqs);
1428 kfree(uac->c_prm.reqs);
1429 kfree(uac->p_prm.rbuf);
1430 kfree(uac->c_prm.rbuf);
1433 EXPORT_SYMBOL_GPL(g_audio_cleanup);
1435 MODULE_LICENSE("GPL");
1436 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
1437 MODULE_AUTHOR("Ruslan Bilovol");