2 * u_audio.c -- interface to USB gadget "ALSA sound card" utilities
5 * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com>
7 * Sound card implementation was cut-and-pasted with changes
8 * from f_uac2.c and has:
10 * Yadwinder Singh (yadi.brar01@gmail.com)
11 * Jaswinder Singh (jaswinder.singh@linaro.org)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
31 #define BUFF_SIZE_MAX (PAGE_SIZE * 16)
32 #define PRD_SIZE_MAX PAGE_SIZE
36 struct uac_rtd_params
*pp
; /* parent param */
37 struct usb_request
*req
;
40 /* Runtime data params for one stream */
41 struct uac_rtd_params
{
42 struct snd_uac_chip
*uac
; /* parent chip */
43 bool ep_enabled
; /* if the ep is enabled */
44 /* Size of the ring buffer */
46 unsigned char *dma_area
;
48 struct snd_pcm_substream
*ss
;
57 unsigned max_psize
; /* MaxPacketSize of endpoint */
64 struct g_audio
*audio_dev
;
66 struct uac_rtd_params p_prm
;
67 struct uac_rtd_params c_prm
;
69 struct snd_card
*card
;
72 /* timekeeping for the playback endpoint */
73 unsigned int p_interval
;
74 unsigned int p_residue
;
76 /* pre-calculated values for playback iso completion */
77 unsigned int p_pktsize
;
78 unsigned int p_pktsize_residue
;
79 unsigned int p_framesize
;
82 static const struct snd_pcm_hardware uac_pcm_hardware
= {
83 .info
= SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
84 | SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
85 | SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_RESUME
,
86 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
87 .periods_max
= BUFF_SIZE_MAX
/ PRD_SIZE_MAX
,
88 .buffer_bytes_max
= BUFF_SIZE_MAX
,
89 .period_bytes_max
= PRD_SIZE_MAX
,
90 .periods_min
= MIN_PERIODS
,
93 static void u_audio_iso_complete(struct usb_ep
*ep
, struct usb_request
*req
)
98 bool update_alsa
= false;
99 int status
= req
->status
;
100 struct uac_req
*ur
= req
->context
;
101 struct snd_pcm_substream
*substream
;
102 struct uac_rtd_params
*prm
= ur
->pp
;
103 struct snd_uac_chip
*uac
= prm
->uac
;
105 /* i/f shutting down */
106 if (!prm
->ep_enabled
|| req
->status
== -ESHUTDOWN
)
110 * We can't really do much about bad xfers.
111 * Afterall, the ISOCH xfers could fail legitimately.
114 pr_debug("%s: iso_complete status(%d) %d/%d\n",
115 __func__
, status
, req
->actual
, req
->length
);
119 /* Do nothing if ALSA isn't active */
123 spin_lock_irqsave(&prm
->lock
, flags
);
125 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
127 * For each IN packet, take the quotient of the current data
128 * rate and the endpoint's interval as the base packet size.
129 * If there is a residue from this division, add it to the
130 * residue accumulator.
132 req
->length
= uac
->p_pktsize
;
133 uac
->p_residue
+= uac
->p_pktsize_residue
;
136 * Whenever there are more bytes in the accumulator than we
137 * need to add one more sample frame, increase this packet's
138 * size and decrease the accumulator.
140 if (uac
->p_residue
/ uac
->p_interval
>= uac
->p_framesize
) {
141 req
->length
+= uac
->p_framesize
;
142 uac
->p_residue
-= uac
->p_framesize
*
146 req
->actual
= req
->length
;
149 pending
= prm
->hw_ptr
% prm
->period_size
;
150 pending
+= req
->actual
;
151 if (pending
>= prm
->period_size
)
154 hw_ptr
= prm
->hw_ptr
;
155 prm
->hw_ptr
= (prm
->hw_ptr
+ req
->actual
) % prm
->dma_bytes
;
157 spin_unlock_irqrestore(&prm
->lock
, flags
);
159 /* Pack USB load in ALSA ring buffer */
160 pending
= prm
->dma_bytes
- hw_ptr
;
162 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
163 if (unlikely(pending
< req
->actual
)) {
164 memcpy(req
->buf
, prm
->dma_area
+ hw_ptr
, pending
);
165 memcpy(req
->buf
+ pending
, prm
->dma_area
,
166 req
->actual
- pending
);
168 memcpy(req
->buf
, prm
->dma_area
+ hw_ptr
, req
->actual
);
171 if (unlikely(pending
< req
->actual
)) {
172 memcpy(prm
->dma_area
+ hw_ptr
, req
->buf
, pending
);
173 memcpy(prm
->dma_area
, req
->buf
+ pending
,
174 req
->actual
- pending
);
176 memcpy(prm
->dma_area
+ hw_ptr
, req
->buf
, req
->actual
);
181 if (usb_ep_queue(ep
, req
, GFP_ATOMIC
))
182 dev_err(uac
->card
->dev
, "%d Error!\n", __LINE__
);
185 snd_pcm_period_elapsed(substream
);
188 static int uac_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
190 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
191 struct uac_rtd_params
*prm
;
192 struct g_audio
*audio_dev
;
193 struct uac_params
*params
;
197 audio_dev
= uac
->audio_dev
;
198 params
= &audio_dev
->params
;
200 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
205 spin_lock_irqsave(&prm
->lock
, flags
);
211 case SNDRV_PCM_TRIGGER_START
:
212 case SNDRV_PCM_TRIGGER_RESUME
:
215 case SNDRV_PCM_TRIGGER_STOP
:
216 case SNDRV_PCM_TRIGGER_SUSPEND
:
223 spin_unlock_irqrestore(&prm
->lock
, flags
);
225 /* Clear buffer after Play stops */
226 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&& !prm
->ss
)
227 memset(prm
->rbuf
, 0, prm
->max_psize
* params
->req_number
);
232 static snd_pcm_uframes_t
uac_pcm_pointer(struct snd_pcm_substream
*substream
)
234 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
235 struct uac_rtd_params
*prm
;
237 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
242 return bytes_to_frames(substream
->runtime
, prm
->hw_ptr
);
245 static int uac_pcm_hw_params(struct snd_pcm_substream
*substream
,
246 struct snd_pcm_hw_params
*hw_params
)
248 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
249 struct uac_rtd_params
*prm
;
252 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
257 err
= snd_pcm_lib_malloc_pages(substream
,
258 params_buffer_bytes(hw_params
));
260 prm
->dma_bytes
= substream
->runtime
->dma_bytes
;
261 prm
->dma_area
= substream
->runtime
->dma_area
;
262 prm
->period_size
= params_period_bytes(hw_params
);
268 static int uac_pcm_hw_free(struct snd_pcm_substream
*substream
)
270 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
271 struct uac_rtd_params
*prm
;
273 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
278 prm
->dma_area
= NULL
;
280 prm
->period_size
= 0;
282 return snd_pcm_lib_free_pages(substream
);
285 static int uac_pcm_open(struct snd_pcm_substream
*substream
)
287 struct snd_uac_chip
*uac
= snd_pcm_substream_chip(substream
);
288 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
289 struct g_audio
*audio_dev
;
290 struct uac_params
*params
;
291 int p_ssize
, c_ssize
;
292 int p_srate
, c_srate
;
293 int p_chmask
, c_chmask
;
295 audio_dev
= uac
->audio_dev
;
296 params
= &audio_dev
->params
;
297 p_ssize
= params
->p_ssize
;
298 c_ssize
= params
->c_ssize
;
299 p_srate
= params
->p_srate
;
300 c_srate
= params
->c_srate
;
301 p_chmask
= params
->p_chmask
;
302 c_chmask
= params
->c_chmask
;
305 runtime
->hw
= uac_pcm_hardware
;
307 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
308 spin_lock_init(&uac
->p_prm
.lock
);
309 runtime
->hw
.rate_min
= p_srate
;
312 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S24_3LE
;
315 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S32_LE
;
318 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S16_LE
;
321 runtime
->hw
.channels_min
= num_channels(p_chmask
);
322 runtime
->hw
.period_bytes_min
= 2 * uac
->p_prm
.max_psize
323 / runtime
->hw
.periods_min
;
325 spin_lock_init(&uac
->c_prm
.lock
);
326 runtime
->hw
.rate_min
= c_srate
;
329 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S24_3LE
;
332 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S32_LE
;
335 runtime
->hw
.formats
= SNDRV_PCM_FMTBIT_S16_LE
;
338 runtime
->hw
.channels_min
= num_channels(c_chmask
);
339 runtime
->hw
.period_bytes_min
= 2 * uac
->c_prm
.max_psize
340 / runtime
->hw
.periods_min
;
343 runtime
->hw
.rate_max
= runtime
->hw
.rate_min
;
344 runtime
->hw
.channels_max
= runtime
->hw
.channels_min
;
346 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
351 /* ALSA cries without these function pointers */
352 static int uac_pcm_null(struct snd_pcm_substream
*substream
)
357 static const struct snd_pcm_ops uac_pcm_ops
= {
358 .open
= uac_pcm_open
,
359 .close
= uac_pcm_null
,
360 .ioctl
= snd_pcm_lib_ioctl
,
361 .hw_params
= uac_pcm_hw_params
,
362 .hw_free
= uac_pcm_hw_free
,
363 .trigger
= uac_pcm_trigger
,
364 .pointer
= uac_pcm_pointer
,
365 .prepare
= uac_pcm_null
,
368 static inline void free_ep(struct uac_rtd_params
*prm
, struct usb_ep
*ep
)
370 struct snd_uac_chip
*uac
= prm
->uac
;
371 struct g_audio
*audio_dev
;
372 struct uac_params
*params
;
375 if (!prm
->ep_enabled
)
378 prm
->ep_enabled
= false;
380 audio_dev
= uac
->audio_dev
;
381 params
= &audio_dev
->params
;
383 for (i
= 0; i
< params
->req_number
; i
++) {
384 if (prm
->ureq
[i
].req
) {
385 usb_ep_dequeue(ep
, prm
->ureq
[i
].req
);
386 usb_ep_free_request(ep
, prm
->ureq
[i
].req
);
387 prm
->ureq
[i
].req
= NULL
;
391 if (usb_ep_disable(ep
))
392 dev_err(uac
->card
->dev
, "%s:%d Error!\n", __func__
, __LINE__
);
396 int u_audio_start_capture(struct g_audio
*audio_dev
)
398 struct snd_uac_chip
*uac
= audio_dev
->uac
;
399 struct usb_gadget
*gadget
= audio_dev
->gadget
;
400 struct device
*dev
= &gadget
->dev
;
401 struct usb_request
*req
;
403 struct uac_rtd_params
*prm
;
404 struct uac_params
*params
= &audio_dev
->params
;
407 ep
= audio_dev
->out_ep
;
409 config_ep_by_speed(gadget
, &audio_dev
->func
, ep
);
410 req_len
= prm
->max_psize
;
412 prm
->ep_enabled
= true;
415 for (i
= 0; i
< params
->req_number
; i
++) {
416 if (!prm
->ureq
[i
].req
) {
417 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
421 prm
->ureq
[i
].req
= req
;
422 prm
->ureq
[i
].pp
= prm
;
425 req
->context
= &prm
->ureq
[i
];
426 req
->length
= req_len
;
427 req
->complete
= u_audio_iso_complete
;
428 req
->buf
= prm
->rbuf
+ i
* prm
->max_psize
;
431 if (usb_ep_queue(ep
, prm
->ureq
[i
].req
, GFP_ATOMIC
))
432 dev_err(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
437 EXPORT_SYMBOL_GPL(u_audio_start_capture
);
439 void u_audio_stop_capture(struct g_audio
*audio_dev
)
441 struct snd_uac_chip
*uac
= audio_dev
->uac
;
443 free_ep(&uac
->c_prm
, audio_dev
->out_ep
);
445 EXPORT_SYMBOL_GPL(u_audio_stop_capture
);
447 int u_audio_start_playback(struct g_audio
*audio_dev
)
449 struct snd_uac_chip
*uac
= audio_dev
->uac
;
450 struct usb_gadget
*gadget
= audio_dev
->gadget
;
451 struct device
*dev
= &gadget
->dev
;
452 struct usb_request
*req
;
454 struct uac_rtd_params
*prm
;
455 struct uac_params
*params
= &audio_dev
->params
;
456 unsigned int factor
, rate
;
457 const struct usb_endpoint_descriptor
*ep_desc
;
460 ep
= audio_dev
->in_ep
;
462 config_ep_by_speed(gadget
, &audio_dev
->func
, ep
);
466 /* pre-calculate the playback endpoint's interval */
467 if (gadget
->speed
== USB_SPEED_FULL
)
472 /* pre-compute some values for iso_complete() */
473 uac
->p_framesize
= params
->p_ssize
*
474 num_channels(params
->p_chmask
);
475 rate
= params
->p_srate
* uac
->p_framesize
;
476 uac
->p_interval
= factor
/ (1 << (ep_desc
->bInterval
- 1));
477 uac
->p_pktsize
= min_t(unsigned int, rate
/ uac
->p_interval
,
480 if (uac
->p_pktsize
< prm
->max_psize
)
481 uac
->p_pktsize_residue
= rate
% uac
->p_interval
;
483 uac
->p_pktsize_residue
= 0;
485 req_len
= uac
->p_pktsize
;
488 prm
->ep_enabled
= true;
491 for (i
= 0; i
< params
->req_number
; i
++) {
492 if (!prm
->ureq
[i
].req
) {
493 req
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
497 prm
->ureq
[i
].req
= req
;
498 prm
->ureq
[i
].pp
= prm
;
501 req
->context
= &prm
->ureq
[i
];
502 req
->length
= req_len
;
503 req
->complete
= u_audio_iso_complete
;
504 req
->buf
= prm
->rbuf
+ i
* prm
->max_psize
;
507 if (usb_ep_queue(ep
, prm
->ureq
[i
].req
, GFP_ATOMIC
))
508 dev_err(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
513 EXPORT_SYMBOL_GPL(u_audio_start_playback
);
515 void u_audio_stop_playback(struct g_audio
*audio_dev
)
517 struct snd_uac_chip
*uac
= audio_dev
->uac
;
519 free_ep(&uac
->p_prm
, audio_dev
->in_ep
);
521 EXPORT_SYMBOL_GPL(u_audio_stop_playback
);
523 int g_audio_setup(struct g_audio
*g_audio
, const char *pcm_name
,
524 const char *card_name
)
526 struct snd_uac_chip
*uac
;
527 struct snd_card
*card
;
529 struct uac_params
*params
;
530 int p_chmask
, c_chmask
;
536 uac
= kzalloc(sizeof(*uac
), GFP_KERNEL
);
540 uac
->audio_dev
= g_audio
;
542 params
= &g_audio
->params
;
543 p_chmask
= params
->p_chmask
;
544 c_chmask
= params
->c_chmask
;
547 struct uac_rtd_params
*prm
= &uac
->c_prm
;
549 uac
->c_prm
.uac
= uac
;
550 prm
->max_psize
= g_audio
->out_ep_maxpsize
;
552 prm
->ureq
= kcalloc(params
->req_number
, sizeof(struct uac_req
),
559 prm
->rbuf
= kcalloc(params
->req_number
, prm
->max_psize
,
569 struct uac_rtd_params
*prm
= &uac
->p_prm
;
571 uac
->p_prm
.uac
= uac
;
572 prm
->max_psize
= g_audio
->in_ep_maxpsize
;
574 prm
->ureq
= kcalloc(params
->req_number
, sizeof(struct uac_req
),
581 prm
->rbuf
= kcalloc(params
->req_number
, prm
->max_psize
,
590 /* Choose any slot, with no id */
591 err
= snd_card_new(&g_audio
->gadget
->dev
,
592 -1, NULL
, THIS_MODULE
, 0, &card
);
599 * Create first PCM device
600 * Create a substream only for non-zero channel streams
602 err
= snd_pcm_new(uac
->card
, pcm_name
, 0,
603 p_chmask
? 1 : 0, c_chmask
? 1 : 0, &pcm
);
607 strcpy(pcm
->name
, pcm_name
);
608 pcm
->private_data
= uac
;
611 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &uac_pcm_ops
);
612 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &uac_pcm_ops
);
614 strcpy(card
->driver
, card_name
);
615 strcpy(card
->shortname
, card_name
);
616 sprintf(card
->longname
, "%s %i", card_name
, card
->dev
->id
);
618 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
619 snd_dma_continuous_data(GFP_KERNEL
), 0, BUFF_SIZE_MAX
);
621 err
= snd_card_register(card
);
629 kfree(uac
->p_prm
.ureq
);
630 kfree(uac
->c_prm
.ureq
);
631 kfree(uac
->p_prm
.rbuf
);
632 kfree(uac
->c_prm
.rbuf
);
637 EXPORT_SYMBOL_GPL(g_audio_setup
);
639 void g_audio_cleanup(struct g_audio
*g_audio
)
641 struct snd_uac_chip
*uac
;
642 struct snd_card
*card
;
644 if (!g_audio
|| !g_audio
->uac
)
652 kfree(uac
->p_prm
.ureq
);
653 kfree(uac
->c_prm
.ureq
);
654 kfree(uac
->p_prm
.rbuf
);
655 kfree(uac
->c_prm
.rbuf
);
658 EXPORT_SYMBOL_GPL(g_audio_cleanup
);
660 MODULE_LICENSE("GPL");
661 MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities");
662 MODULE_AUTHOR("Ruslan Bilovol");