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/slab.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <linux/ctype.h>
17 #include <linux/random.h>
18 #include <linux/syscalls.h>
23 * This component encapsulates the ALSA devices for USB audio gadget
26 #define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p"
27 #define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c"
28 #define FILE_CONTROL "/dev/snd/controlC0"
30 static char *fn_play = FILE_PCM_PLAYBACK;
31 module_param(fn_play, charp, S_IRUGO);
32 MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
34 static char *fn_cap = FILE_PCM_CAPTURE;
35 module_param(fn_cap, charp, S_IRUGO);
36 MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
38 static char *fn_cntl = FILE_CONTROL;
39 module_param(fn_cntl, charp, S_IRUGO);
40 MODULE_PARM_DESC(fn_cntl, "Control device file name");
42 /*-------------------------------------------------------------------------*/
45 * Some ALSA internal helper functions
47 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
49 struct snd_interval t;
52 t.openmin = t.openmax = 0;
54 return snd_interval_refine(i, &t);
57 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
58 snd_pcm_hw_param_t var, unsigned int val,
62 if (hw_is_mask(var)) {
63 struct snd_mask *m = hw_param_mask(params, var);
64 if (val == 0 && dir < 0) {
72 changed = snd_mask_refine_set(
73 hw_param_mask(params, var), val);
75 } else if (hw_is_interval(var)) {
76 struct snd_interval *i = hw_param_interval(params, var);
77 if (val == 0 && dir < 0) {
81 changed = snd_interval_refine_set(i, val);
83 struct snd_interval t;
95 changed = snd_interval_refine(i, &t);
100 params->cmask |= 1 << var;
101 params->rmask |= 1 << var;
105 /*-------------------------------------------------------------------------*/
108 * Set default hardware params
110 static int playback_default_hw_params(struct gaudio_snd_dev *snd)
112 struct snd_pcm_substream *substream = snd->substream;
113 struct snd_pcm_hw_params *params;
114 snd_pcm_sframes_t result;
117 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
118 * SNDRV_PCM_FORMAT_S16_LE
122 snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
123 snd->format = SNDRV_PCM_FORMAT_S16_LE;
127 params = kzalloc(sizeof(*params), GFP_KERNEL);
131 _snd_pcm_hw_params_any(params);
132 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
134 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
136 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
138 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
141 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
142 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params);
144 result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
147 "Preparing sound card failed: %d\n", (int)result);
152 /* Store the hardware parameters */
153 snd->access = params_access(params);
154 snd->format = params_format(params);
155 snd->channels = params_channels(params);
156 snd->rate = params_rate(params);
161 "Hardware params: access %x, format %x, channels %d, rate %d\n",
162 snd->access, snd->format, snd->channels, snd->rate);
168 * Playback audio buffer data by ALSA PCM device
170 static size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
172 struct gaudio_snd_dev *snd = &card->playback;
173 struct snd_pcm_substream *substream = snd->substream;
174 struct snd_pcm_runtime *runtime = substream->runtime;
177 snd_pcm_sframes_t frames;
180 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
181 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
182 result = snd_pcm_kernel_ioctl(substream,
183 SNDRV_PCM_IOCTL_PREPARE, NULL);
185 ERROR(card, "Preparing sound card failed: %d\n",
191 frames = bytes_to_frames(runtime, count);
194 result = snd_pcm_lib_write(snd->substream, buf, frames);
195 if (result != frames) {
196 ERROR(card, "Playback error: %d\n", (int)result);
205 static int u_audio_get_playback_channels(struct gaudio *card)
207 return card->playback.channels;
210 static int u_audio_get_playback_rate(struct gaudio *card)
212 return card->playback.rate;
216 * Open ALSA PCM and control device files
217 * Initial the PCM or control device
219 static int gaudio_open_snd_dev(struct gaudio *card)
221 struct snd_pcm_file *pcm_file;
222 struct gaudio_snd_dev *snd;
227 /* Open control device */
228 snd = &card->control;
229 snd->filp = filp_open(fn_cntl, O_RDWR, 0);
230 if (IS_ERR(snd->filp)) {
231 int ret = PTR_ERR(snd->filp);
232 ERROR(card, "unable to open sound control device file: %s\n",
239 /* Open PCM playback device and setup substream */
240 snd = &card->playback;
241 snd->filp = filp_open(fn_play, O_WRONLY, 0);
242 if (IS_ERR(snd->filp)) {
243 ERROR(card, "No such PCM playback device: %s\n", fn_play);
246 pcm_file = snd->filp->private_data;
247 snd->substream = pcm_file->substream;
249 playback_default_hw_params(snd);
251 /* Open PCM capture device and setup substream */
252 snd = &card->capture;
253 snd->filp = filp_open(fn_cap, O_RDONLY, 0);
254 if (IS_ERR(snd->filp)) {
255 ERROR(card, "No such PCM capture device: %s\n", fn_cap);
256 snd->substream = NULL;
259 pcm_file = snd->filp->private_data;
260 snd->substream = pcm_file->substream;
268 * Close ALSA PCM and control device files
270 static int gaudio_close_snd_dev(struct gaudio *gau)
272 struct gaudio_snd_dev *snd;
274 /* Close control device */
276 if (!IS_ERR(snd->filp))
277 filp_close(snd->filp, current->files);
279 /* Close PCM playback device and setup substream */
280 snd = &gau->playback;
281 if (!IS_ERR(snd->filp))
282 filp_close(snd->filp, current->files);
284 /* Close PCM capture device and setup substream */
286 if (!IS_ERR(snd->filp))
287 filp_close(snd->filp, current->files);
292 static struct gaudio *the_card;
294 * gaudio_setup - setup ALSA interface and preparing for USB transfer
296 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
298 * Returns negative errno, or zero on success
300 int __init gaudio_setup(struct gaudio *card)
304 ret = gaudio_open_snd_dev(card);
306 ERROR(card, "we need at least one control device\n");
316 * gaudio_cleanup - remove ALSA device interface
318 * This is called to free all resources allocated by @gaudio_setup().
320 void gaudio_cleanup(void)
323 gaudio_close_snd_dev(the_card);