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 */
35 /* Size of the ring buffer */
37 unsigned char *dma_area
;
39 struct snd_pcm_substream
*ss
;
48 unsigned max_psize
; /* MaxPacketSize of endpoint */
55 struct g_audio
*audio_dev
;
57 struct uac_rtd_params p_prm
;
58 struct uac_rtd_params c_prm
;
60 struct snd_card
*card
;
63 /* timekeeping for the playback endpoint */
64 unsigned int p_interval
;
65 unsigned int p_residue
;
67 /* pre-calculated values for playback iso completion */
68 unsigned int p_pktsize
;
69 unsigned int p_pktsize_residue
;
70 unsigned int p_framesize
;
73 static const struct snd_pcm_hardware uac_pcm_hardware
= {
74 .info
= SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
75 | SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
76 | SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_RESUME
,
77 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
78 .periods_max
= BUFF_SIZE_MAX
/ PRD_SIZE_MAX
,
79 .buffer_bytes_max
= BUFF_SIZE_MAX
,
80 .period_bytes_max
= PRD_SIZE_MAX
,
81 .periods_min
= MIN_PERIODS
,
84 static void u_audio_iso_complete(struct usb_ep
*ep
, struct usb_request
*req
)
89 bool update_alsa
= false;
90 int status
= req
->status
;
91 struct uac_req
*ur
= req
->context
;
92 struct snd_pcm_substream
*substream
;
93 struct uac_rtd_params
*prm
= ur
->pp
;
94 struct snd_uac_chip
*uac
= prm
->uac
;
96 /* i/f shutting down */
97 if (!prm
->ep_enabled
|| 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 spin_lock_irqsave(&prm
->lock
, flags
);
116 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
118 * For each IN packet, take the quotient of the current data
119 * rate and the endpoint's interval as the base packet size.
120 * If there is a residue from this division, add it to the
121 * residue accumulator.
123 req
->length
= uac
->p_pktsize
;
124 uac
->p_residue
+= uac
->p_pktsize_residue
;
127 * Whenever there are more bytes in the accumulator than we
128 * need to add one more sample frame, increase this packet's
129 * size and decrease the accumulator.
131 if (uac
->p_residue
/ uac
->p_interval
>= uac
->p_framesize
) {
132 req
->length
+= uac
->p_framesize
;
133 uac
->p_residue
-= uac
->p_framesize
*
137 req
->actual
= req
->length
;
140 pending
= prm
->hw_ptr
% prm
->period_size
;
141 pending
+= req
->actual
;
142 if (pending
>= prm
->period_size
)
145 hw_ptr
= prm
->hw_ptr
;
146 prm
->hw_ptr
= (prm
->hw_ptr
+ req
->actual
) % prm
->dma_bytes
;
148 spin_unlock_irqrestore(&prm
->lock
, flags
);
150 /* Pack USB load in ALSA ring buffer */
151 pending
= prm
->dma_bytes
- hw_ptr
;
153 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
154 if (unlikely(pending
< req
->actual
)) {
155 memcpy(req
->buf
, prm
->dma_area
+ hw_ptr
, pending
);
156 memcpy(req
->buf
+ pending
, prm
->dma_area
,
157 req
->actual
- pending
);
159 memcpy(req
->buf
, prm
->dma_area
+ hw_ptr
, req
->actual
);
162 if (unlikely(pending
< req
->actual
)) {
163 memcpy(prm
->dma_area
+ hw_ptr
, req
->buf
, pending
);
164 memcpy(prm
->dma_area
, req
->buf
+ pending
,
165 req
->actual
- pending
);
167 memcpy(prm
->dma_area
+ hw_ptr
, req
->buf
, req
->actual
);
172 if (usb_ep_queue(ep
, req
, GFP_ATOMIC
))
173 dev_err(uac
->card
->dev
, "%d Error!\n", __LINE__
);
176 snd_pcm_period_elapsed(substream
);
179 static int uac_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
181 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
182 struct uac_rtd_params
*prm
;
183 struct g_audio
*audio_dev
;
184 struct uac_params
*params
;
188 audio_dev
= uac
->audio_dev
;
189 params
= &audio_dev
->params
;
191 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
196 spin_lock_irqsave(&prm
->lock
, flags
);
202 case SNDRV_PCM_TRIGGER_START
:
203 case SNDRV_PCM_TRIGGER_RESUME
:
206 case SNDRV_PCM_TRIGGER_STOP
:
207 case SNDRV_PCM_TRIGGER_SUSPEND
:
214 spin_unlock_irqrestore(&prm
->lock
, flags
);
216 /* Clear buffer after Play stops */
217 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&& !prm
->ss
)
218 memset(prm
->rbuf
, 0, prm
->max_psize
* params
->req_number
);
223 static snd_pcm_uframes_t
uac_pcm_pointer(struct snd_pcm_substream
*substream
)
225 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
226 struct uac_rtd_params
*prm
;
228 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
233 return bytes_to_frames(substream
->runtime
, prm
->hw_ptr
);
236 static int uac_pcm_hw_params(struct snd_pcm_substream
*substream
,
237 struct snd_pcm_hw_params
*hw_params
)
239 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
240 struct uac_rtd_params
*prm
;
243 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
248 err
= snd_pcm_lib_malloc_pages(substream
,
249 params_buffer_bytes(hw_params
));
251 prm
->dma_bytes
= substream
->runtime
->dma_bytes
;
252 prm
->dma_area
= substream
->runtime
->dma_area
;
253 prm
->period_size
= params_period_bytes(hw_params
);
259 static int uac_pcm_hw_free(struct snd_pcm_substream
*substream
)
261 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
262 struct uac_rtd_params
*prm
;
264 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
269 prm
->dma_area
= NULL
;
271 prm
->period_size
= 0;
273 return snd_pcm_lib_free_pages(substream
);
276 static int uac_pcm_open(struct snd_pcm_substream
*substream
)
278 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
279 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 struct g_audio
*audio_dev
;
281 struct uac_params
*params
;
282 int p_ssize
, c_ssize
;
283 int p_srate
, c_srate
;
284 int p_chmask
, c_chmask
;
286 audio_dev
= uac
->audio_dev
;
287 params
= &audio_dev
->params
;
288 p_ssize
= params
->p_ssize
;
289 c_ssize
= params
->c_ssize
;
290 p_srate
= params
->p_srate
;
291 c_srate
= params
->c_srate
;
292 p_chmask
= params
->p_chmask
;
293 c_chmask
= params
->c_chmask
;
296 runtime
->hw
= uac_pcm_hardware
;
298 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
299 spin_lock_init(&uac
->p_prm
.lock
);
300 runtime
->hw
.rate_min
= p_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(p_chmask
);
313 runtime
->hw
.period_bytes_min
= 2 * uac
->p_prm
.max_psize
314 / runtime
->hw
.periods_min
;
316 spin_lock_init(&uac
->c_prm
.lock
);
317 runtime
->hw
.rate_min
= c_srate
;
320 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S24_3LE
;
323 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S32_LE
;
326 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S16_LE
;
329 runtime
->hw
.channels_min
= num_channels(c_chmask
);
330 runtime
->hw
.period_bytes_min
= 2 * uac
->c_prm
.max_psize
331 / runtime
->hw
.periods_min
;
334 runtime
->hw
.rate_max
= runtime
->hw
.rate_min
;
335 runtime
->hw
.channels_max
= runtime
->hw
.channels_min
;
337 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
342 /* ALSA cries without these function pointers */
343 static int uac_pcm_null(struct snd_pcm_substream
*substream
)
348 static const struct snd_pcm_ops uac_pcm_ops
= {
349 .open
= uac_pcm_open
,
350 .close
= uac_pcm_null
,
351 .ioctl
= snd_pcm_lib_ioctl
,
352 .hw_params
= uac_pcm_hw_params
,
353 .hw_free
= uac_pcm_hw_free
,
354 .trigger
= uac_pcm_trigger
,
355 .pointer
= uac_pcm_pointer
,
356 .prepare
= uac_pcm_null
,
359 static inline void free_ep(struct uac_rtd_params
*prm
, struct usb_ep
*ep
)
361 struct snd_uac_chip
*uac
= prm
->uac
;
362 struct g_audio
*audio_dev
;
363 struct uac_params
*params
;
366 if (!prm
->ep_enabled
)
369 prm
->ep_enabled
= false;
371 audio_dev
= uac
->audio_dev
;
372 params
= &audio_dev
->params
;
374 for (i
= 0; i
< params
->req_number
; i
++) {
375 if (prm
->ureq
[i
].req
) {
376 usb_ep_dequeue(ep
, prm
->ureq
[i
].req
);
377 usb_ep_free_request(ep
, prm
->ureq
[i
].req
);
378 prm
->ureq
[i
].req
= NULL
;
382 if (usb_ep_disable(ep
))
383 dev_err(uac
->card
->dev
, "%s:%d Error!\n", __func__
, __LINE__
);
387 int u_audio_start_capture(struct g_audio
*audio_dev
)
389 struct snd_uac_chip
*uac
= audio_dev
->uac
;
390 struct usb_gadget
*gadget
= audio_dev
->gadget
;
391 struct device
*dev
= &gadget
->dev
;
392 struct usb_request
*req
;
394 struct uac_rtd_params
*prm
;
395 struct uac_params
*params
= &audio_dev
->params
;
398 ep
= audio_dev
->out_ep
;
400 config_ep_by_speed(gadget
, &audio_dev
->func
, ep
);
401 req_len
= prm
->max_psize
;
403 prm
->ep_enabled
= true;
406 for (i
= 0; i
< params
->req_number
; i
++) {
407 if (!prm
->ureq
[i
].req
) {
408 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
412 prm
->ureq
[i
].req
= req
;
413 prm
->ureq
[i
].pp
= prm
;
416 req
->context
= &prm
->ureq
[i
];
417 req
->length
= req_len
;
418 req
->complete
= u_audio_iso_complete
;
419 req
->buf
= prm
->rbuf
+ i
* prm
->max_psize
;
422 if (usb_ep_queue(ep
, prm
->ureq
[i
].req
, GFP_ATOMIC
))
423 dev_err(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
428 EXPORT_SYMBOL_GPL(u_audio_start_capture
);
430 void u_audio_stop_capture(struct g_audio
*audio_dev
)
432 struct snd_uac_chip
*uac
= audio_dev
->uac
;
434 free_ep(&uac
->c_prm
, audio_dev
->out_ep
);
436 EXPORT_SYMBOL_GPL(u_audio_stop_capture
);
438 int u_audio_start_playback(struct g_audio
*audio_dev
)
440 struct snd_uac_chip
*uac
= audio_dev
->uac
;
441 struct usb_gadget
*gadget
= audio_dev
->gadget
;
442 struct device
*dev
= &gadget
->dev
;
443 struct usb_request
*req
;
445 struct uac_rtd_params
*prm
;
446 struct uac_params
*params
= &audio_dev
->params
;
447 unsigned int factor
, rate
;
448 const struct usb_endpoint_descriptor
*ep_desc
;
451 ep
= audio_dev
->in_ep
;
453 config_ep_by_speed(gadget
, &audio_dev
->func
, ep
);
457 /* pre-calculate the playback endpoint's interval */
458 if (gadget
->speed
== USB_SPEED_FULL
)
463 /* pre-compute some values for iso_complete() */
464 uac
->p_framesize
= params
->p_ssize
*
465 num_channels(params
->p_chmask
);
466 rate
= params
->p_srate
* uac
->p_framesize
;
467 uac
->p_interval
= factor
/ (1 << (ep_desc
->bInterval
- 1));
468 uac
->p_pktsize
= min_t(unsigned int, rate
/ uac
->p_interval
,
471 if (uac
->p_pktsize
< prm
->max_psize
)
472 uac
->p_pktsize_residue
= rate
% uac
->p_interval
;
474 uac
->p_pktsize_residue
= 0;
476 req_len
= uac
->p_pktsize
;
479 prm
->ep_enabled
= true;
482 for (i
= 0; i
< params
->req_number
; i
++) {
483 if (!prm
->ureq
[i
].req
) {
484 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
488 prm
->ureq
[i
].req
= req
;
489 prm
->ureq
[i
].pp
= prm
;
492 req
->context
= &prm
->ureq
[i
];
493 req
->length
= req_len
;
494 req
->complete
= u_audio_iso_complete
;
495 req
->buf
= prm
->rbuf
+ i
* prm
->max_psize
;
498 if (usb_ep_queue(ep
, prm
->ureq
[i
].req
, GFP_ATOMIC
))
499 dev_err(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
504 EXPORT_SYMBOL_GPL(u_audio_start_playback
);
506 void u_audio_stop_playback(struct g_audio
*audio_dev
)
508 struct snd_uac_chip
*uac
= audio_dev
->uac
;
510 free_ep(&uac
->p_prm
, audio_dev
->in_ep
);
512 EXPORT_SYMBOL_GPL(u_audio_stop_playback
);
514 int g_audio_setup(struct g_audio
*g_audio
, const char *pcm_name
,
515 const char *card_name
)
517 struct snd_uac_chip
*uac
;
518 struct snd_card
*card
;
520 struct uac_params
*params
;
521 int p_chmask
, c_chmask
;
527 uac
= kzalloc(sizeof(*uac
), GFP_KERNEL
);
531 uac
->audio_dev
= g_audio
;
533 params
= &g_audio
->params
;
534 p_chmask
= params
->p_chmask
;
535 c_chmask
= params
->c_chmask
;
538 struct uac_rtd_params
*prm
= &uac
->c_prm
;
540 uac
->c_prm
.uac
= uac
;
541 prm
->max_psize
= g_audio
->out_ep_maxpsize
;
543 prm
->ureq
= kcalloc(params
->req_number
, sizeof(struct uac_req
),
550 prm
->rbuf
= kcalloc(params
->req_number
, prm
->max_psize
,
560 struct uac_rtd_params
*prm
= &uac
->p_prm
;
562 uac
->p_prm
.uac
= uac
;
563 prm
->max_psize
= g_audio
->in_ep_maxpsize
;
565 prm
->ureq
= kcalloc(params
->req_number
, sizeof(struct uac_req
),
572 prm
->rbuf
= kcalloc(params
->req_number
, prm
->max_psize
,
581 /* Choose any slot, with no id */
582 err
= snd_card_new(&g_audio
->gadget
->dev
,
583 -1, NULL
, THIS_MODULE
, 0, &card
);
590 * Create first PCM device
591 * Create a substream only for non-zero channel streams
593 err
= snd_pcm_new(uac
->card
, pcm_name
, 0,
594 p_chmask
? 1 : 0, c_chmask
? 1 : 0, &pcm
);
598 strcpy(pcm
->name
, pcm_name
);
599 pcm
->private_data
= uac
;
602 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &uac_pcm_ops
);
603 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &uac_pcm_ops
);
605 strcpy(card
->driver
, card_name
);
606 strcpy(card
->shortname
, card_name
);
607 sprintf(card
->longname
, "%s %i", card_name
, card
->dev
->id
);
609 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
610 snd_dma_continuous_data(GFP_KERNEL
), 0, BUFF_SIZE_MAX
);
612 err
= snd_card_register(card
);
620 kfree(uac
->p_prm
.ureq
);
621 kfree(uac
->c_prm
.ureq
);
622 kfree(uac
->p_prm
.rbuf
);
623 kfree(uac
->c_prm
.rbuf
);
628 EXPORT_SYMBOL_GPL(g_audio_setup
);
630 void g_audio_cleanup(struct g_audio
*g_audio
)
632 struct snd_uac_chip
*uac
;
633 struct snd_card
*card
;
635 if (!g_audio
|| !g_audio
->uac
)
643 kfree(uac
->p_prm
.ureq
);
644 kfree(uac
->c_prm
.ureq
);
645 kfree(uac
->p_prm
.rbuf
);
646 kfree(uac
->c_prm
.rbuf
);
649 EXPORT_SYMBOL_GPL(g_audio_cleanup
);
651 MODULE_LICENSE("GPL");
652 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
653 MODULE_AUTHOR("Ruslan Bilovol");