2 * u_uac1.c -- ALSA audio utilities for Gadget stack
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/module.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/ctype.h>
18 #include <linux/random.h>
19 #include <linux/syscalls.h>
24 * This component encapsulates the ALSA devices for USB audio gadget
27 /*-------------------------------------------------------------------------*/
30 * Some ALSA internal helper functions
32 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
34 struct snd_interval t;
37 t.openmin = t.openmax = 0;
39 return snd_interval_refine(i, &t);
42 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
43 snd_pcm_hw_param_t var, unsigned int val,
47 if (hw_is_mask(var)) {
48 struct snd_mask *m = hw_param_mask(params, var);
49 if (val == 0 && dir < 0) {
57 changed = snd_mask_refine_set(
58 hw_param_mask(params, var), val);
60 } else if (hw_is_interval(var)) {
61 struct snd_interval *i = hw_param_interval(params, var);
62 if (val == 0 && dir < 0) {
66 changed = snd_interval_refine_set(i, val);
68 struct snd_interval t;
80 changed = snd_interval_refine(i, &t);
85 params->cmask |= 1 << var;
86 params->rmask |= 1 << var;
90 /*-------------------------------------------------------------------------*/
93 * Set default hardware params
95 static int playback_default_hw_params(struct gaudio_snd_dev *snd)
97 struct snd_pcm_substream *substream = snd->substream;
98 struct snd_pcm_hw_params *params;
99 snd_pcm_sframes_t result;
102 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
103 * SNDRV_PCM_FORMAT_S16_LE
107 snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
108 snd->format = SNDRV_PCM_FORMAT_S16_LE;
112 params = kzalloc(sizeof(*params), GFP_KERNEL);
116 _snd_pcm_hw_params_any(params);
117 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
119 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
121 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
123 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
126 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
127 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params);
129 result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
132 "Preparing sound card failed: %d\n", (int)result);
137 /* Store the hardware parameters */
138 snd->access = params_access(params);
139 snd->format = params_format(params);
140 snd->channels = params_channels(params);
141 snd->rate = params_rate(params);
146 "Hardware params: access %x, format %x, channels %d, rate %d\n",
147 snd->access, snd->format, snd->channels, snd->rate);
153 * Playback audio buffer data by ALSA PCM device
155 size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
157 struct gaudio_snd_dev *snd = &card->playback;
158 struct snd_pcm_substream *substream = snd->substream;
159 struct snd_pcm_runtime *runtime = substream->runtime;
162 snd_pcm_sframes_t frames;
165 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
166 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
167 result = snd_pcm_kernel_ioctl(substream,
168 SNDRV_PCM_IOCTL_PREPARE, NULL);
170 ERROR(card, "Preparing sound card failed: %d\n",
176 frames = bytes_to_frames(runtime, count);
179 result = snd_pcm_lib_write(snd->substream, (void __user *)buf, frames);
180 if (result != frames) {
181 ERROR(card, "Playback error: %d\n", (int)result);
190 int u_audio_get_playback_channels(struct gaudio *card)
192 return card->playback.channels;
195 int u_audio_get_playback_rate(struct gaudio *card)
197 return card->playback.rate;
201 * Open ALSA PCM and control device files
202 * Initial the PCM or control device
204 static int gaudio_open_snd_dev(struct gaudio *card)
206 struct snd_pcm_file *pcm_file;
207 struct gaudio_snd_dev *snd;
208 struct f_uac1_opts *opts;
209 char *fn_play, *fn_cap, *fn_cntl;
211 opts = container_of(card->func.fi, struct f_uac1_opts, func_inst);
212 fn_play = opts->fn_play;
213 fn_cap = opts->fn_cap;
214 fn_cntl = opts->fn_cntl;
216 /* Open control device */
217 snd = &card->control;
218 snd->filp = filp_open(fn_cntl, O_RDWR, 0);
219 if (IS_ERR(snd->filp)) {
220 int ret = PTR_ERR(snd->filp);
221 ERROR(card, "unable to open sound control device file: %s\n",
228 /* Open PCM playback device and setup substream */
229 snd = &card->playback;
230 snd->filp = filp_open(fn_play, O_WRONLY, 0);
231 if (IS_ERR(snd->filp)) {
232 int ret = PTR_ERR(snd->filp);
234 ERROR(card, "No such PCM playback device: %s\n", fn_play);
238 pcm_file = snd->filp->private_data;
239 snd->substream = pcm_file->substream;
241 playback_default_hw_params(snd);
243 /* Open PCM capture device and setup substream */
244 snd = &card->capture;
245 snd->filp = filp_open(fn_cap, O_RDONLY, 0);
246 if (IS_ERR(snd->filp)) {
247 ERROR(card, "No such PCM capture device: %s\n", fn_cap);
248 snd->substream = NULL;
252 pcm_file = snd->filp->private_data;
253 snd->substream = pcm_file->substream;
261 * Close ALSA PCM and control device files
263 static int gaudio_close_snd_dev(struct gaudio *gau)
265 struct gaudio_snd_dev *snd;
267 /* Close control device */
270 filp_close(snd->filp, NULL);
272 /* Close PCM playback device and setup substream */
273 snd = &gau->playback;
275 filp_close(snd->filp, NULL);
277 /* Close PCM capture device and setup substream */
280 filp_close(snd->filp, NULL);
286 * gaudio_setup - setup ALSA interface and preparing for USB transfer
288 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
290 * Returns negative errno, or zero on success
292 int gaudio_setup(struct gaudio *card)
296 ret = gaudio_open_snd_dev(card);
298 ERROR(card, "we need at least one control device\n");
305 * gaudio_cleanup - remove ALSA device interface
307 * This is called to free all resources allocated by @gaudio_setup().
309 void gaudio_cleanup(struct gaudio *the_card)
312 gaudio_close_snd_dev(the_card);