2 * u_audio.c -- ALSA audio utilities for Gadget stack
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
7 * Enter bugs at http://blackfin.uclinux.org/
9 * Licensed under the GPL-2 or later.
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/ctype.h>
16 #include <linux/random.h>
17 #include <linux/syscalls.h>
22 * This component encapsulates the ALSA devices for USB audio gadget
25 #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p"
26 #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c"
27 #define FILE_CONTROL "/dev/snd/controlC0"
29 static char *fn_play
= FILE_PCM_PLAYBACK
;
30 module_param(fn_play
, charp
, S_IRUGO
);
31 MODULE_PARM_DESC(fn_play
, "Playback PCM device file name");
33 static char *fn_cap
= FILE_PCM_CAPTURE
;
34 module_param(fn_cap
, charp
, S_IRUGO
);
35 MODULE_PARM_DESC(fn_cap
, "Capture PCM device file name");
37 static char *fn_cntl
= FILE_CONTROL
;
38 module_param(fn_cntl
, charp
, S_IRUGO
);
39 MODULE_PARM_DESC(fn_cntl
, "Control device file name");
41 /*-------------------------------------------------------------------------*/
44 * Some ALSA internal helper functions
46 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
48 struct snd_interval t
;
51 t
.openmin
= t
.openmax
= 0;
53 return snd_interval_refine(i
, &t
);
56 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
57 snd_pcm_hw_param_t var
, unsigned int val
,
61 if (hw_is_mask(var
)) {
62 struct snd_mask
*m
= hw_param_mask(params
, var
);
63 if (val
== 0 && dir
< 0) {
71 changed
= snd_mask_refine_set(
72 hw_param_mask(params
, var
), val
);
74 } else if (hw_is_interval(var
)) {
75 struct snd_interval
*i
= hw_param_interval(params
, var
);
76 if (val
== 0 && dir
< 0) {
80 changed
= snd_interval_refine_set(i
, val
);
82 struct snd_interval t
;
94 changed
= snd_interval_refine(i
, &t
);
99 params
->cmask
|= 1 << var
;
100 params
->rmask
|= 1 << var
;
104 /*-------------------------------------------------------------------------*/
107 * Set default hardware params
109 static int playback_default_hw_params(struct gaudio_snd_dev
*snd
)
111 struct snd_pcm_substream
*substream
= snd
->substream
;
112 struct snd_pcm_hw_params
*params
;
113 snd_pcm_sframes_t result
;
116 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
117 * SNDRV_PCM_FORMAT_S16_LE
121 snd
->access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
122 snd
->format
= SNDRV_PCM_FORMAT_S16_LE
;
126 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
130 _snd_pcm_hw_params_any(params
);
131 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
133 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
135 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
137 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
140 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
141 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, params
);
143 result
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
146 "Preparing sound card failed: %d\n", (int)result
);
151 /* Store the hardware parameters */
152 snd
->access
= params_access(params
);
153 snd
->format
= params_format(params
);
154 snd
->channels
= params_channels(params
);
155 snd
->rate
= params_rate(params
);
160 "Hardware params: access %x, format %x, channels %d, rate %d\n",
161 snd
->access
, snd
->format
, snd
->channels
, snd
->rate
);
167 * Playback audio buffer data by ALSA PCM device
169 static size_t u_audio_playback(struct gaudio
*card
, void *buf
, size_t count
)
171 struct gaudio_snd_dev
*snd
= &card
->playback
;
172 struct snd_pcm_substream
*substream
= snd
->substream
;
173 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
176 snd_pcm_sframes_t frames
;
179 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
180 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
181 result
= snd_pcm_kernel_ioctl(substream
,
182 SNDRV_PCM_IOCTL_PREPARE
, NULL
);
184 ERROR(card
, "Preparing sound card failed: %d\n",
190 frames
= bytes_to_frames(runtime
, count
);
193 result
= snd_pcm_lib_write(snd
->substream
, buf
, frames
);
194 if (result
!= frames
) {
195 ERROR(card
, "Playback error: %d\n", (int)result
);
204 static int u_audio_get_playback_channels(struct gaudio
*card
)
206 return card
->playback
.channels
;
209 static int u_audio_get_playback_rate(struct gaudio
*card
)
211 return card
->playback
.rate
;
215 * Open ALSA PCM and control device files
216 * Initial the PCM or control device
218 static int gaudio_open_snd_dev(struct gaudio
*card
)
220 struct snd_pcm_file
*pcm_file
;
221 struct gaudio_snd_dev
*snd
;
226 /* Open control device */
227 snd
= &card
->control
;
228 snd
->filp
= filp_open(fn_cntl
, O_RDWR
, 0);
229 if (IS_ERR(snd
->filp
)) {
230 int ret
= PTR_ERR(snd
->filp
);
231 ERROR(card
, "unable to open sound control device file: %s\n",
238 /* Open PCM playback device and setup substream */
239 snd
= &card
->playback
;
240 snd
->filp
= filp_open(fn_play
, O_WRONLY
, 0);
241 if (IS_ERR(snd
->filp
)) {
242 ERROR(card
, "No such PCM playback device: %s\n", fn_play
);
245 pcm_file
= snd
->filp
->private_data
;
246 snd
->substream
= pcm_file
->substream
;
248 playback_default_hw_params(snd
);
250 /* Open PCM capture device and setup substream */
251 snd
= &card
->capture
;
252 snd
->filp
= filp_open(fn_cap
, O_RDONLY
, 0);
253 if (IS_ERR(snd
->filp
)) {
254 ERROR(card
, "No such PCM capture device: %s\n", fn_cap
);
255 snd
->substream
= NULL
;
258 pcm_file
= snd
->filp
->private_data
;
259 snd
->substream
= pcm_file
->substream
;
267 * Close ALSA PCM and control device files
269 static int gaudio_close_snd_dev(struct gaudio
*gau
)
271 struct gaudio_snd_dev
*snd
;
273 /* Close control device */
275 if (!IS_ERR(snd
->filp
))
276 filp_close(snd
->filp
, current
->files
);
278 /* Close PCM playback device and setup substream */
279 snd
= &gau
->playback
;
280 if (!IS_ERR(snd
->filp
))
281 filp_close(snd
->filp
, current
->files
);
283 /* Close PCM capture device and setup substream */
285 if (!IS_ERR(snd
->filp
))
286 filp_close(snd
->filp
, current
->files
);
292 * gaudio_setup - setup ALSA interface and preparing for USB transfer
294 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
296 * Returns negative errno, or zero on success
298 int __init
gaudio_setup(struct gaudio
*card
)
302 ret
= gaudio_open_snd_dev(card
);
304 ERROR(card
, "we need at least one control device\n");
311 * gaudio_cleanup - remove ALSA device interface
313 * This is called to free all resources allocated by @gaudio_setup().
315 void gaudio_cleanup(struct gaudio
*card
)
318 gaudio_close_snd_dev(card
);