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
37 /* Runtime data params for one stream */
38 struct uac_rtd_params {
39 struct snd_uac_chip *uac; /* parent chip */
40 bool ep_enabled; /* if the ep is enabled */
42 struct snd_pcm_substream *ss;
49 unsigned int pitch; /* Stream pitch ratio to 1000000 */
50 unsigned int max_psize; /* MaxPacketSize of endpoint */
52 struct usb_request **reqs;
54 struct usb_request *req_fback; /* Feedback endpoint request */
55 bool fb_ep_enabled; /* if the ep is enabled */
57 /* Volume/Mute controls and their state */
58 int fu_id; /* Feature Unit ID */
59 struct snd_kcontrol *snd_kctl_volume;
60 struct snd_kcontrol *snd_kctl_mute;
61 s16 volume_min, volume_max, volume_res;
65 spinlock_t lock; /* lock for control transfers */
70 struct g_audio *audio_dev;
72 struct uac_rtd_params p_prm;
73 struct uac_rtd_params c_prm;
75 struct snd_card *card;
78 /* pre-calculated values for playback iso completion */
79 unsigned long long p_residue_mil;
80 unsigned int p_interval;
81 unsigned int p_framesize;
84 static const struct snd_pcm_hardware uac_pcm_hardware = {
85 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER
86 | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID
87 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
88 .rates = SNDRV_PCM_RATE_CONTINUOUS,
89 .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX,
90 .buffer_bytes_max = BUFF_SIZE_MAX,
91 .period_bytes_max = PRD_SIZE_MAX,
92 .periods_min = MIN_PERIODS,
95 static void u_audio_set_fback_frequency(enum usb_device_speed speed,
96 struct usb_ep *out_ep,
97 unsigned long long freq,
102 const struct usb_endpoint_descriptor *ep_desc;
105 * Because the pitch base is 1000000, the final divider here
106 * will be 1000 * 1000000 = 1953125 << 9
108 * Instead of dealing with big numbers lets fold this 9 left shift
111 if (speed == USB_SPEED_FULL) {
113 * Full-speed feedback endpoints report frequency
115 * Format is encoded in Q10.10 left-justified in the 24 bits,
116 * so that it has a Q10.14 format.
118 * ff = (freq << 14) / 1000
123 * High-speed feedback endpoints report frequency
124 * in samples/microframe.
125 * Format is encoded in Q12.13 fitted into four bytes so that
126 * the binary point is located between the second and the third
127 * byte fromat (that is Q16.16)
129 * ff = (freq << 16) / 8000
131 * Win10 and OSX UAC2 drivers require number of samples per packet
132 * in order to honor the feedback value.
133 * Linux snd-usb-audio detects the applied bit-shift automatically.
135 ep_desc = out_ep->desc;
136 freq <<= 4 + (ep_desc->bInterval - 1);
139 ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), 1953125);
141 *(__le32 *)buf = cpu_to_le32(ff);
144 static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
146 unsigned int pending;
148 int status = req->status;
149 struct snd_pcm_substream *substream;
150 struct snd_pcm_runtime *runtime;
151 struct uac_rtd_params *prm = req->context;
152 struct snd_uac_chip *uac = prm->uac;
153 struct g_audio *audio_dev = uac->audio_dev;
154 struct uac_params *params = &audio_dev->params;
155 unsigned int frames, p_pktsize;
156 unsigned long long pitched_rate_mil, p_pktsize_residue_mil,
157 residue_frames_mil, div_result;
159 /* i/f shutting down */
160 if (!prm->ep_enabled) {
161 usb_ep_free_request(ep, req);
165 if (req->status == -ESHUTDOWN)
169 * We can't really do much about bad xfers.
170 * Afterall, the ISOCH xfers could fail legitimately.
173 pr_debug("%s: iso_complete status(%d) %d/%d\n",
174 __func__, status, req->actual, req->length);
178 /* Do nothing if ALSA isn't active */
182 snd_pcm_stream_lock(substream);
184 runtime = substream->runtime;
185 if (!runtime || !snd_pcm_running(substream)) {
186 snd_pcm_stream_unlock(substream);
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
192 * For each IN packet, take the quotient of the current data
193 * rate and the endpoint's interval as the base packet size.
194 * If there is a residue from this division, add it to the
195 * residue accumulator.
197 unsigned long long p_interval_mil = uac->p_interval * 1000000ULL;
199 pitched_rate_mil = (unsigned long long)
200 params->p_srate * prm->pitch;
201 div_result = pitched_rate_mil;
202 do_div(div_result, uac->p_interval);
203 do_div(div_result, 1000000);
204 frames = (unsigned int) div_result;
206 pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n",
207 params->p_srate, prm->pitch, p_interval_mil, frames);
209 p_pktsize = min_t(unsigned int,
210 uac->p_framesize * frames,
213 if (p_pktsize < ep->maxpacket) {
214 residue_frames_mil = pitched_rate_mil - frames * p_interval_mil;
215 p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil;
217 p_pktsize_residue_mil = 0;
219 req->length = p_pktsize;
220 uac->p_residue_mil += p_pktsize_residue_mil;
223 * Whenever there are more bytes in the accumulator p_residue_mil than we
224 * need to add one more sample frame, increase this packet's
225 * size and decrease the accumulator.
227 div_result = uac->p_residue_mil;
228 do_div(div_result, uac->p_interval);
229 do_div(div_result, 1000000);
230 if ((unsigned int) div_result >= uac->p_framesize) {
231 req->length += uac->p_framesize;
232 uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
233 pr_debug("increased req length to %d\n", req->length);
235 pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
237 req->actual = req->length;
240 hw_ptr = prm->hw_ptr;
242 /* Pack USB load in ALSA ring buffer */
243 pending = runtime->dma_bytes - hw_ptr;
245 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
246 if (unlikely(pending < req->actual)) {
247 memcpy(req->buf, runtime->dma_area + hw_ptr, pending);
248 memcpy(req->buf + pending, runtime->dma_area,
249 req->actual - pending);
251 memcpy(req->buf, runtime->dma_area + hw_ptr,
255 if (unlikely(pending < req->actual)) {
256 memcpy(runtime->dma_area + hw_ptr, req->buf, pending);
257 memcpy(runtime->dma_area, req->buf + pending,
258 req->actual - pending);
260 memcpy(runtime->dma_area + hw_ptr, req->buf,
265 /* update hw_ptr after data is copied to memory */
266 prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes;
267 hw_ptr = prm->hw_ptr;
268 snd_pcm_stream_unlock(substream);
270 if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual)
271 snd_pcm_period_elapsed(substream);
274 if (usb_ep_queue(ep, req, GFP_ATOMIC))
275 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
278 static void u_audio_iso_fback_complete(struct usb_ep *ep,
279 struct usb_request *req)
281 struct uac_rtd_params *prm = req->context;
282 struct snd_uac_chip *uac = prm->uac;
283 struct g_audio *audio_dev = uac->audio_dev;
284 struct uac_params *params = &audio_dev->params;
285 int status = req->status;
287 /* i/f shutting down */
288 if (!prm->fb_ep_enabled) {
290 usb_ep_free_request(ep, req);
294 if (req->status == -ESHUTDOWN)
298 * We can't really do much about bad xfers.
299 * Afterall, the ISOCH xfers could fail legitimately.
302 pr_debug("%s: iso_complete status(%d) %d/%d\n",
303 __func__, status, req->actual, req->length);
305 u_audio_set_fback_frequency(audio_dev->gadget->speed, audio_dev->out_ep,
306 params->c_srate, prm->pitch,
309 if (usb_ep_queue(ep, req, GFP_ATOMIC))
310 dev_err(uac->card->dev, "%d Error!\n", __LINE__);
313 static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
315 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
316 struct uac_rtd_params *prm;
317 struct g_audio *audio_dev;
318 struct uac_params *params;
321 audio_dev = uac->audio_dev;
322 params = &audio_dev->params;
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
333 case SNDRV_PCM_TRIGGER_START:
334 case SNDRV_PCM_TRIGGER_RESUME:
337 case SNDRV_PCM_TRIGGER_STOP:
338 case SNDRV_PCM_TRIGGER_SUSPEND:
345 /* Clear buffer after Play stops */
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss)
347 memset(prm->rbuf, 0, prm->max_psize * params->req_number);
352 static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream)
354 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
355 struct uac_rtd_params *prm;
357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
362 return bytes_to_frames(substream->runtime, prm->hw_ptr);
365 static u64 uac_ssize_to_fmt(int ssize)
371 ret = SNDRV_PCM_FMTBIT_S24_3LE;
374 ret = SNDRV_PCM_FMTBIT_S32_LE;
377 ret = SNDRV_PCM_FMTBIT_S16_LE;
384 static int uac_pcm_open(struct snd_pcm_substream *substream)
386 struct snd_uac_chip *uac = snd_pcm_substream_chip(substream);
387 struct snd_pcm_runtime *runtime = substream->runtime;
388 struct g_audio *audio_dev;
389 struct uac_params *params;
390 int p_ssize, c_ssize;
391 int p_srate, c_srate;
392 int p_chmask, c_chmask;
394 audio_dev = uac->audio_dev;
395 params = &audio_dev->params;
396 p_ssize = params->p_ssize;
397 c_ssize = params->c_ssize;
398 p_srate = params->p_srate;
399 c_srate = params->c_srate;
400 p_chmask = params->p_chmask;
401 c_chmask = params->c_chmask;
402 uac->p_residue_mil = 0;
404 runtime->hw = uac_pcm_hardware;
406 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
407 runtime->hw.rate_min = p_srate;
408 runtime->hw.formats = uac_ssize_to_fmt(p_ssize);
409 runtime->hw.channels_min = num_channels(p_chmask);
410 runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize
411 / runtime->hw.periods_min;
413 runtime->hw.rate_min = c_srate;
414 runtime->hw.formats = uac_ssize_to_fmt(c_ssize);
415 runtime->hw.channels_min = num_channels(c_chmask);
416 runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize
417 / runtime->hw.periods_min;
420 runtime->hw.rate_max = runtime->hw.rate_min;
421 runtime->hw.channels_max = runtime->hw.channels_min;
423 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
428 /* ALSA cries without these function pointers */
429 static int uac_pcm_null(struct snd_pcm_substream *substream)
434 static const struct snd_pcm_ops uac_pcm_ops = {
435 .open = uac_pcm_open,
436 .close = uac_pcm_null,
437 .trigger = uac_pcm_trigger,
438 .pointer = uac_pcm_pointer,
439 .prepare = uac_pcm_null,
442 static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep)
444 struct snd_uac_chip *uac = prm->uac;
445 struct g_audio *audio_dev;
446 struct uac_params *params;
449 if (!prm->ep_enabled)
452 audio_dev = uac->audio_dev;
453 params = &audio_dev->params;
455 for (i = 0; i < params->req_number; i++) {
457 if (usb_ep_dequeue(ep, prm->reqs[i]))
458 usb_ep_free_request(ep, prm->reqs[i]);
460 * If usb_ep_dequeue() cannot successfully dequeue the
461 * request, the request will be freed by the completion
469 prm->ep_enabled = false;
471 if (usb_ep_disable(ep))
472 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
475 static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep)
477 struct snd_uac_chip *uac = prm->uac;
479 if (!prm->fb_ep_enabled)
482 if (prm->req_fback) {
483 if (usb_ep_dequeue(ep, prm->req_fback)) {
484 kfree(prm->req_fback->buf);
485 usb_ep_free_request(ep, prm->req_fback);
487 prm->req_fback = NULL;
490 prm->fb_ep_enabled = false;
492 if (usb_ep_disable(ep))
493 dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__);
496 int u_audio_start_capture(struct g_audio *audio_dev)
498 struct snd_uac_chip *uac = audio_dev->uac;
499 struct usb_gadget *gadget = audio_dev->gadget;
500 struct device *dev = &gadget->dev;
501 struct usb_request *req, *req_fback;
502 struct usb_ep *ep, *ep_fback;
503 struct uac_rtd_params *prm;
504 struct uac_params *params = &audio_dev->params;
507 ep = audio_dev->out_ep;
509 config_ep_by_speed(gadget, &audio_dev->func, ep);
510 req_len = ep->maxpacket;
512 prm->ep_enabled = true;
515 for (i = 0; i < params->req_number; i++) {
517 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
525 req->length = req_len;
526 req->complete = u_audio_iso_complete;
527 req->buf = prm->rbuf + i * ep->maxpacket;
530 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
531 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
534 ep_fback = audio_dev->in_ep_fback;
538 /* Setup feedback endpoint */
539 config_ep_by_speed(gadget, &audio_dev->func, ep_fback);
540 prm->fb_ep_enabled = true;
541 usb_ep_enable(ep_fback);
542 req_len = ep_fback->maxpacket;
544 req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC);
545 if (req_fback == NULL)
548 prm->req_fback = req_fback;
550 req_fback->context = prm;
551 req_fback->length = req_len;
552 req_fback->complete = u_audio_iso_fback_complete;
554 req_fback->buf = kzalloc(req_len, GFP_ATOMIC);
559 * Configure the feedback endpoint's reported frequency.
560 * Always start with original frequency since its deviation can't
561 * be meauserd at start of playback
563 prm->pitch = 1000000;
564 u_audio_set_fback_frequency(audio_dev->gadget->speed, ep,
565 params->c_srate, prm->pitch,
568 if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC))
569 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
573 EXPORT_SYMBOL_GPL(u_audio_start_capture);
575 void u_audio_stop_capture(struct g_audio *audio_dev)
577 struct snd_uac_chip *uac = audio_dev->uac;
579 if (audio_dev->in_ep_fback)
580 free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback);
581 free_ep(&uac->c_prm, audio_dev->out_ep);
583 EXPORT_SYMBOL_GPL(u_audio_stop_capture);
585 int u_audio_start_playback(struct g_audio *audio_dev)
587 struct snd_uac_chip *uac = audio_dev->uac;
588 struct usb_gadget *gadget = audio_dev->gadget;
589 struct device *dev = &gadget->dev;
590 struct usb_request *req;
592 struct uac_rtd_params *prm;
593 struct uac_params *params = &audio_dev->params;
595 const struct usb_endpoint_descriptor *ep_desc;
597 unsigned int p_pktsize;
599 ep = audio_dev->in_ep;
601 config_ep_by_speed(gadget, &audio_dev->func, ep);
605 * Always start with original frequency
607 prm->pitch = 1000000;
609 /* pre-calculate the playback endpoint's interval */
610 if (gadget->speed == USB_SPEED_FULL)
615 /* pre-compute some values for iso_complete() */
616 uac->p_framesize = params->p_ssize *
617 num_channels(params->p_chmask);
618 uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
619 p_pktsize = min_t(unsigned int,
621 (params->p_srate / uac->p_interval),
625 uac->p_residue_mil = 0;
627 prm->ep_enabled = true;
630 for (i = 0; i < params->req_number; i++) {
632 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
640 req->length = req_len;
641 req->complete = u_audio_iso_complete;
642 req->buf = prm->rbuf + i * ep->maxpacket;
645 if (usb_ep_queue(ep, prm->reqs[i], GFP_ATOMIC))
646 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
651 EXPORT_SYMBOL_GPL(u_audio_start_playback);
653 void u_audio_stop_playback(struct g_audio *audio_dev)
655 struct snd_uac_chip *uac = audio_dev->uac;
657 free_ep(&uac->p_prm, audio_dev->in_ep);
659 EXPORT_SYMBOL_GPL(u_audio_stop_playback);
661 int u_audio_get_volume(struct g_audio *audio_dev, int playback, s16 *val)
663 struct snd_uac_chip *uac = audio_dev->uac;
664 struct uac_rtd_params *prm;
672 spin_lock_irqsave(&prm->lock, flags);
674 spin_unlock_irqrestore(&prm->lock, flags);
678 EXPORT_SYMBOL_GPL(u_audio_get_volume);
680 int u_audio_set_volume(struct g_audio *audio_dev, int playback, s16 val)
682 struct snd_uac_chip *uac = audio_dev->uac;
683 struct uac_rtd_params *prm;
692 spin_lock_irqsave(&prm->lock, flags);
693 val = clamp(val, prm->volume_min, prm->volume_max);
694 if (prm->volume != val) {
698 spin_unlock_irqrestore(&prm->lock, flags);
701 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
702 &prm->snd_kctl_volume->id);
706 EXPORT_SYMBOL_GPL(u_audio_set_volume);
708 int u_audio_get_mute(struct g_audio *audio_dev, int playback, int *val)
710 struct snd_uac_chip *uac = audio_dev->uac;
711 struct uac_rtd_params *prm;
719 spin_lock_irqsave(&prm->lock, flags);
721 spin_unlock_irqrestore(&prm->lock, flags);
725 EXPORT_SYMBOL_GPL(u_audio_get_mute);
727 int u_audio_set_mute(struct g_audio *audio_dev, int playback, int val)
729 struct snd_uac_chip *uac = audio_dev->uac;
730 struct uac_rtd_params *prm;
742 spin_lock_irqsave(&prm->lock, flags);
743 if (prm->mute != mute) {
747 spin_unlock_irqrestore(&prm->lock, flags);
750 snd_ctl_notify(uac->card, SNDRV_CTL_EVENT_MASK_VALUE,
751 &prm->snd_kctl_mute->id);
755 EXPORT_SYMBOL_GPL(u_audio_set_mute);
758 static int u_audio_pitch_info(struct snd_kcontrol *kcontrol,
759 struct snd_ctl_elem_info *uinfo)
761 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
762 struct snd_uac_chip *uac = prm->uac;
763 struct g_audio *audio_dev = uac->audio_dev;
764 struct uac_params *params = &audio_dev->params;
765 unsigned int pitch_min, pitch_max;
767 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
768 pitch_max = (1000 + params->fb_max) * 1000;
770 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
772 uinfo->value.integer.min = pitch_min;
773 uinfo->value.integer.max = pitch_max;
774 uinfo->value.integer.step = 1;
778 static int u_audio_pitch_get(struct snd_kcontrol *kcontrol,
779 struct snd_ctl_elem_value *ucontrol)
781 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
783 ucontrol->value.integer.value[0] = prm->pitch;
788 static int u_audio_pitch_put(struct snd_kcontrol *kcontrol,
789 struct snd_ctl_elem_value *ucontrol)
791 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
792 struct snd_uac_chip *uac = prm->uac;
793 struct g_audio *audio_dev = uac->audio_dev;
794 struct uac_params *params = &audio_dev->params;
796 unsigned int pitch_min, pitch_max;
799 pitch_min = (1000 - FBACK_SLOW_MAX) * 1000;
800 pitch_max = (1000 + params->fb_max) * 1000;
802 val = ucontrol->value.integer.value[0];
809 if (prm->pitch != val) {
817 static int u_audio_mute_info(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_info *uinfo)
820 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
822 uinfo->value.integer.min = 0;
823 uinfo->value.integer.max = 1;
824 uinfo->value.integer.step = 1;
829 static int u_audio_mute_get(struct snd_kcontrol *kcontrol,
830 struct snd_ctl_elem_value *ucontrol)
832 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
835 spin_lock_irqsave(&prm->lock, flags);
836 ucontrol->value.integer.value[0] = !prm->mute;
837 spin_unlock_irqrestore(&prm->lock, flags);
842 static int u_audio_mute_put(struct snd_kcontrol *kcontrol,
843 struct snd_ctl_elem_value *ucontrol)
845 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
846 struct snd_uac_chip *uac = prm->uac;
847 struct g_audio *audio_dev = uac->audio_dev;
852 val = !ucontrol->value.integer.value[0];
854 spin_lock_irqsave(&prm->lock, flags);
855 if (val != prm->mute) {
859 spin_unlock_irqrestore(&prm->lock, flags);
861 if (change && audio_dev->notify)
862 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_MUTE);
868 * TLV callback for mixer volume controls
870 static int u_audio_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
871 unsigned int size, unsigned int __user *_tlv)
873 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
874 DECLARE_TLV_DB_MINMAX(scale, 0, 0);
876 if (size < sizeof(scale))
879 /* UAC volume resolution is 1/256 dB, TLV is 1/100 dB */
880 scale[2] = (prm->volume_min * 100) / 256;
881 scale[3] = (prm->volume_max * 100) / 256;
882 if (copy_to_user(_tlv, scale, sizeof(scale)))
888 static int u_audio_volume_info(struct snd_kcontrol *kcontrol,
889 struct snd_ctl_elem_info *uinfo)
891 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
893 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
895 uinfo->value.integer.min = 0;
896 uinfo->value.integer.max =
897 (prm->volume_max - prm->volume_min + prm->volume_res - 1)
899 uinfo->value.integer.step = 1;
904 static int u_audio_volume_get(struct snd_kcontrol *kcontrol,
905 struct snd_ctl_elem_value *ucontrol)
907 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
910 spin_lock_irqsave(&prm->lock, flags);
911 ucontrol->value.integer.value[0] =
912 (prm->volume - prm->volume_min) / prm->volume_res;
913 spin_unlock_irqrestore(&prm->lock, flags);
918 static int u_audio_volume_put(struct snd_kcontrol *kcontrol,
919 struct snd_ctl_elem_value *ucontrol)
921 struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol);
922 struct snd_uac_chip *uac = prm->uac;
923 struct g_audio *audio_dev = uac->audio_dev;
929 val = ucontrol->value.integer.value[0];
931 spin_lock_irqsave(&prm->lock, flags);
932 volume = (val * prm->volume_res) + prm->volume_min;
933 volume = clamp(volume, prm->volume_min, prm->volume_max);
934 if (volume != prm->volume) {
935 prm->volume = volume;
938 spin_unlock_irqrestore(&prm->lock, flags);
940 if (change && audio_dev->notify)
941 audio_dev->notify(audio_dev, prm->fu_id, UAC_FU_VOLUME);
947 static struct snd_kcontrol_new u_audio_controls[] = {
949 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
950 .name = "Capture Pitch 1000000",
951 .info = u_audio_pitch_info,
952 .get = u_audio_pitch_get,
953 .put = u_audio_pitch_put,
956 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
957 .name = "Playback Pitch 1000000",
958 .info = u_audio_pitch_info,
959 .get = u_audio_pitch_get,
960 .put = u_audio_pitch_put,
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .name = "", /* will be filled later */
965 .info = u_audio_mute_info,
966 .get = u_audio_mute_get,
967 .put = u_audio_mute_put,
970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
971 .name = "", /* will be filled later */
972 .info = u_audio_volume_info,
973 .get = u_audio_volume_get,
974 .put = u_audio_volume_put,
978 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
979 const char *card_name)
981 struct snd_uac_chip *uac;
982 struct snd_card *card;
984 struct snd_kcontrol *kctl;
985 struct uac_params *params;
986 int p_chmask, c_chmask;
992 uac = kzalloc(sizeof(*uac), GFP_KERNEL);
996 uac->audio_dev = g_audio;
998 params = &g_audio->params;
999 p_chmask = params->p_chmask;
1000 c_chmask = params->c_chmask;
1003 struct uac_rtd_params *prm = &uac->c_prm;
1005 spin_lock_init(&prm->lock);
1006 uac->c_prm.uac = uac;
1007 prm->max_psize = g_audio->out_ep_maxpsize;
1009 prm->reqs = kcalloc(params->req_number,
1010 sizeof(struct usb_request *),
1017 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
1027 struct uac_rtd_params *prm = &uac->p_prm;
1029 spin_lock_init(&prm->lock);
1030 uac->p_prm.uac = uac;
1031 prm->max_psize = g_audio->in_ep_maxpsize;
1033 prm->reqs = kcalloc(params->req_number,
1034 sizeof(struct usb_request *),
1041 prm->rbuf = kcalloc(params->req_number, prm->max_psize,
1050 /* Choose any slot, with no id */
1051 err = snd_card_new(&g_audio->gadget->dev,
1052 -1, NULL, THIS_MODULE, 0, &card);
1059 * Create first PCM device
1060 * Create a substream only for non-zero channel streams
1062 err = snd_pcm_new(uac->card, pcm_name, 0,
1063 p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm);
1067 strscpy(pcm->name, pcm_name, sizeof(pcm->name));
1068 pcm->private_data = uac;
1071 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops);
1072 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops);
1075 * Create mixer and controls
1076 * Create only if it's required on USB side
1078 if ((c_chmask && g_audio->in_ep_fback)
1079 || (p_chmask && params->p_fu.id)
1080 || (c_chmask && params->c_fu.id))
1081 strscpy(card->mixername, card_name, sizeof(card->driver));
1083 if (c_chmask && g_audio->in_ep_fback) {
1084 kctl = snd_ctl_new1(&u_audio_controls[UAC_FBACK_CTRL],
1091 kctl->id.device = pcm->device;
1092 kctl->id.subdevice = 0;
1094 err = snd_ctl_add(card, kctl);
1100 kctl = snd_ctl_new1(&u_audio_controls[UAC_P_PITCH_CTRL],
1107 kctl->id.device = pcm->device;
1108 kctl->id.subdevice = 0;
1110 err = snd_ctl_add(card, kctl);
1115 for (i = 0; i <= SNDRV_PCM_STREAM_LAST; i++) {
1116 struct uac_rtd_params *prm;
1117 struct uac_fu_params *fu;
1121 if (!pcm->streams[i].substream_count)
1124 if (i == SNDRV_PCM_STREAM_PLAYBACK) {
1127 direction = "Playback";
1131 direction = "Capture";
1134 prm->fu_id = fu->id;
1136 if (fu->mute_present) {
1137 snprintf(ctrl_name, sizeof(ctrl_name),
1138 "PCM %s Switch", direction);
1140 u_audio_controls[UAC_MUTE_CTRL].name = ctrl_name;
1142 kctl = snd_ctl_new1(&u_audio_controls[UAC_MUTE_CTRL],
1149 kctl->id.device = pcm->device;
1150 kctl->id.subdevice = 0;
1152 err = snd_ctl_add(card, kctl);
1155 prm->snd_kctl_mute = kctl;
1159 if (fu->volume_present) {
1160 snprintf(ctrl_name, sizeof(ctrl_name),
1161 "PCM %s Volume", direction);
1163 u_audio_controls[UAC_VOLUME_CTRL].name = ctrl_name;
1165 kctl = snd_ctl_new1(&u_audio_controls[UAC_VOLUME_CTRL],
1172 kctl->id.device = pcm->device;
1173 kctl->id.subdevice = 0;
1176 kctl->tlv.c = u_audio_volume_tlv;
1177 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1178 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1180 err = snd_ctl_add(card, kctl);
1183 prm->snd_kctl_volume = kctl;
1184 prm->volume = fu->volume_max;
1185 prm->volume_max = fu->volume_max;
1186 prm->volume_min = fu->volume_min;
1187 prm->volume_res = fu->volume_res;
1191 strscpy(card->driver, card_name, sizeof(card->driver));
1192 strscpy(card->shortname, card_name, sizeof(card->shortname));
1193 sprintf(card->longname, "%s %i", card_name, card->dev->id);
1195 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1196 NULL, 0, BUFF_SIZE_MAX);
1198 err = snd_card_register(card);
1204 snd_card_free(card);
1206 kfree(uac->p_prm.reqs);
1207 kfree(uac->c_prm.reqs);
1208 kfree(uac->p_prm.rbuf);
1209 kfree(uac->c_prm.rbuf);
1214 EXPORT_SYMBOL_GPL(g_audio_setup);
1216 void g_audio_cleanup(struct g_audio *g_audio)
1218 struct snd_uac_chip *uac;
1219 struct snd_card *card;
1221 if (!g_audio || !g_audio->uac)
1227 snd_card_free(card);
1229 kfree(uac->p_prm.reqs);
1230 kfree(uac->c_prm.reqs);
1231 kfree(uac->p_prm.rbuf);
1232 kfree(uac->c_prm.rbuf);
1235 EXPORT_SYMBOL_GPL(g_audio_cleanup);
1237 MODULE_LICENSE("GPL");
1238 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
1239 MODULE_AUTHOR("Ruslan Bilovol");