1 // SPDX-License-Identifier: GPL-2.0+
3 * u_uac1.c -- ALSA audio utilities for Gadget stack
5 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6 * Copyright (C) 2008 Analog Devices, Inc
8 * Enter bugs at http://blackfin.uclinux.org/
11 #include <linux/kernel.h>
12 #include <linux/module.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>
20 #include "u_uac1_legacy.h"
23 * This component encapsulates the ALSA devices for USB audio gadget
26 /*-------------------------------------------------------------------------*/
29 * Some ALSA internal helper functions
31 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
33 struct snd_interval t
;
36 t
.openmin
= t
.openmax
= 0;
38 return snd_interval_refine(i
, &t
);
41 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
42 snd_pcm_hw_param_t var
, unsigned int val
,
46 if (hw_is_mask(var
)) {
47 struct snd_mask
*m
= hw_param_mask(params
, var
);
48 if (val
== 0 && dir
< 0) {
56 changed
= snd_mask_refine_set(
57 hw_param_mask(params
, var
), val
);
59 } else if (hw_is_interval(var
)) {
60 struct snd_interval
*i
= hw_param_interval(params
, var
);
61 if (val
== 0 && dir
< 0) {
65 changed
= snd_interval_refine_set(i
, val
);
67 struct snd_interval t
;
79 changed
= snd_interval_refine(i
, &t
);
84 params
->cmask
|= 1 << var
;
85 params
->rmask
|= 1 << var
;
89 /*-------------------------------------------------------------------------*/
92 * Set default hardware params
94 static int playback_default_hw_params(struct gaudio_snd_dev
*snd
)
96 struct snd_pcm_substream
*substream
= snd
->substream
;
97 struct snd_pcm_hw_params
*params
;
98 snd_pcm_sframes_t result
;
101 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
102 * SNDRV_PCM_FORMAT_S16_LE
106 snd
->access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
107 snd
->format
= SNDRV_PCM_FORMAT_S16_LE
;
111 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
115 _snd_pcm_hw_params_any(params
);
116 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
118 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
120 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
122 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
125 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
126 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, params
);
128 result
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
131 "Preparing sound card failed: %d\n", (int)result
);
136 /* Store the hardware parameters */
137 snd
->access
= params_access(params
);
138 snd
->format
= params_format(params
);
139 snd
->channels
= params_channels(params
);
140 snd
->rate
= params_rate(params
);
145 "Hardware params: access %x, format %x, channels %d, rate %d\n",
146 snd
->access
, snd
->format
, snd
->channels
, snd
->rate
);
152 * Playback audio buffer data by ALSA PCM device
154 size_t u_audio_playback(struct gaudio
*card
, void *buf
, size_t count
)
156 struct gaudio_snd_dev
*snd
= &card
->playback
;
157 struct snd_pcm_substream
*substream
= snd
->substream
;
158 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
160 snd_pcm_sframes_t frames
;
163 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
164 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
165 result
= snd_pcm_kernel_ioctl(substream
,
166 SNDRV_PCM_IOCTL_PREPARE
, NULL
);
168 ERROR(card
, "Preparing sound card failed: %d\n",
174 frames
= bytes_to_frames(runtime
, count
);
175 result
= snd_pcm_kernel_write(snd
->substream
, buf
, frames
);
176 if (result
!= frames
) {
177 ERROR(card
, "Playback error: %d\n", (int)result
);
184 int u_audio_get_playback_channels(struct gaudio
*card
)
186 return card
->playback
.channels
;
189 int u_audio_get_playback_rate(struct gaudio
*card
)
191 return card
->playback
.rate
;
195 * Open ALSA PCM and control device files
196 * Initial the PCM or control device
198 static int gaudio_open_snd_dev(struct gaudio
*card
)
200 struct snd_pcm_file
*pcm_file
;
201 struct gaudio_snd_dev
*snd
;
202 struct f_uac1_legacy_opts
*opts
;
203 char *fn_play
, *fn_cap
, *fn_cntl
;
205 opts
= container_of(card
->func
.fi
, struct f_uac1_legacy_opts
,
207 fn_play
= opts
->fn_play
;
208 fn_cap
= opts
->fn_cap
;
209 fn_cntl
= opts
->fn_cntl
;
211 /* Open control device */
212 snd
= &card
->control
;
213 snd
->filp
= filp_open(fn_cntl
, O_RDWR
, 0);
214 if (IS_ERR(snd
->filp
)) {
215 int ret
= PTR_ERR(snd
->filp
);
216 ERROR(card
, "unable to open sound control device file: %s\n",
223 /* Open PCM playback device and setup substream */
224 snd
= &card
->playback
;
225 snd
->filp
= filp_open(fn_play
, O_WRONLY
, 0);
226 if (IS_ERR(snd
->filp
)) {
227 int ret
= PTR_ERR(snd
->filp
);
229 ERROR(card
, "No such PCM playback device: %s\n", fn_play
);
233 pcm_file
= snd
->filp
->private_data
;
234 snd
->substream
= pcm_file
->substream
;
236 playback_default_hw_params(snd
);
238 /* Open PCM capture device and setup substream */
239 snd
= &card
->capture
;
240 snd
->filp
= filp_open(fn_cap
, O_RDONLY
, 0);
241 if (IS_ERR(snd
->filp
)) {
242 ERROR(card
, "No such PCM capture device: %s\n", fn_cap
);
243 snd
->substream
= NULL
;
247 pcm_file
= snd
->filp
->private_data
;
248 snd
->substream
= pcm_file
->substream
;
256 * Close ALSA PCM and control device files
258 static int gaudio_close_snd_dev(struct gaudio
*gau
)
260 struct gaudio_snd_dev
*snd
;
262 /* Close control device */
265 filp_close(snd
->filp
, NULL
);
267 /* Close PCM playback device and setup substream */
268 snd
= &gau
->playback
;
270 filp_close(snd
->filp
, NULL
);
272 /* Close PCM capture device and setup substream */
275 filp_close(snd
->filp
, NULL
);
281 * gaudio_setup - setup ALSA interface and preparing for USB transfer
283 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
285 * Returns negative errno, or zero on success
287 int gaudio_setup(struct gaudio
*card
)
291 ret
= gaudio_open_snd_dev(card
);
293 ERROR(card
, "we need at least one control device\n");
300 * gaudio_cleanup - remove ALSA device interface
302 * This is called to free all resources allocated by @gaudio_setup().
304 void gaudio_cleanup(struct gaudio
*the_card
)
307 gaudio_close_snd_dev(the_card
);