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
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/ctype.h>
15 #include <linux/random.h>
16 #include <linux/syscalls.h>
18 #include "u_uac1_legacy.h"
21 * This component encapsulates the ALSA devices for USB audio gadget
24 /*-------------------------------------------------------------------------*/
27 * Some ALSA internal helper functions
29 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
31 struct snd_interval t
;
34 t
.openmin
= t
.openmax
= 0;
36 return snd_interval_refine(i
, &t
);
39 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
40 snd_pcm_hw_param_t var
, unsigned int val
,
44 if (hw_is_mask(var
)) {
45 struct snd_mask
*m
= hw_param_mask(params
, var
);
46 if (val
== 0 && dir
< 0) {
54 changed
= snd_mask_refine_set(
55 hw_param_mask(params
, var
), val
);
57 } else if (hw_is_interval(var
)) {
58 struct snd_interval
*i
= hw_param_interval(params
, var
);
59 if (val
== 0 && dir
< 0) {
63 changed
= snd_interval_refine_set(i
, val
);
65 struct snd_interval t
;
77 changed
= snd_interval_refine(i
, &t
);
82 params
->cmask
|= 1 << var
;
83 params
->rmask
|= 1 << var
;
87 /*-------------------------------------------------------------------------*/
90 * Set default hardware params
92 static int playback_default_hw_params(struct gaudio_snd_dev
*snd
)
94 struct snd_pcm_substream
*substream
= snd
->substream
;
95 struct snd_pcm_hw_params
*params
;
96 snd_pcm_sframes_t result
;
99 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
100 * SNDRV_PCM_FORMAT_S16_LE
104 snd
->access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
105 snd
->format
= SNDRV_PCM_FORMAT_S16_LE
;
109 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
113 _snd_pcm_hw_params_any(params
);
114 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
116 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
118 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
120 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
123 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
124 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, params
);
126 result
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
129 "Preparing sound card failed: %d\n", (int)result
);
134 /* Store the hardware parameters */
135 snd
->access
= params_access(params
);
136 snd
->format
= params_format(params
);
137 snd
->channels
= params_channels(params
);
138 snd
->rate
= params_rate(params
);
143 "Hardware params: access %x, format %x, channels %d, rate %d\n",
144 snd
->access
, snd
->format
, snd
->channels
, snd
->rate
);
150 * Playback audio buffer data by ALSA PCM device
152 size_t u_audio_playback(struct gaudio
*card
, void *buf
, size_t count
)
154 struct gaudio_snd_dev
*snd
= &card
->playback
;
155 struct snd_pcm_substream
*substream
= snd
->substream
;
156 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
158 snd_pcm_sframes_t frames
;
161 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
162 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
163 result
= snd_pcm_kernel_ioctl(substream
,
164 SNDRV_PCM_IOCTL_PREPARE
, NULL
);
166 ERROR(card
, "Preparing sound card failed: %d\n",
172 frames
= bytes_to_frames(runtime
, count
);
173 result
= snd_pcm_kernel_write(snd
->substream
, buf
, frames
);
174 if (result
!= frames
) {
175 ERROR(card
, "Playback error: %d\n", (int)result
);
182 int u_audio_get_playback_channels(struct gaudio
*card
)
184 return card
->playback
.channels
;
187 int u_audio_get_playback_rate(struct gaudio
*card
)
189 return card
->playback
.rate
;
193 * Open ALSA PCM and control device files
194 * Initial the PCM or control device
196 static int gaudio_open_snd_dev(struct gaudio
*card
)
198 struct snd_pcm_file
*pcm_file
;
199 struct gaudio_snd_dev
*snd
;
200 struct f_uac1_legacy_opts
*opts
;
201 char *fn_play
, *fn_cap
, *fn_cntl
;
203 opts
= container_of(card
->func
.fi
, struct f_uac1_legacy_opts
,
205 fn_play
= opts
->fn_play
;
206 fn_cap
= opts
->fn_cap
;
207 fn_cntl
= opts
->fn_cntl
;
209 /* Open control device */
210 snd
= &card
->control
;
211 snd
->filp
= filp_open(fn_cntl
, O_RDWR
, 0);
212 if (IS_ERR(snd
->filp
)) {
213 int ret
= PTR_ERR(snd
->filp
);
214 ERROR(card
, "unable to open sound control device file: %s\n",
221 /* Open PCM playback device and setup substream */
222 snd
= &card
->playback
;
223 snd
->filp
= filp_open(fn_play
, O_WRONLY
, 0);
224 if (IS_ERR(snd
->filp
)) {
225 int ret
= PTR_ERR(snd
->filp
);
227 ERROR(card
, "No such PCM playback device: %s\n", fn_play
);
231 pcm_file
= snd
->filp
->private_data
;
232 snd
->substream
= pcm_file
->substream
;
234 playback_default_hw_params(snd
);
236 /* Open PCM capture device and setup substream */
237 snd
= &card
->capture
;
238 snd
->filp
= filp_open(fn_cap
, O_RDONLY
, 0);
239 if (IS_ERR(snd
->filp
)) {
240 ERROR(card
, "No such PCM capture device: %s\n", fn_cap
);
241 snd
->substream
= NULL
;
245 pcm_file
= snd
->filp
->private_data
;
246 snd
->substream
= pcm_file
->substream
;
254 * Close ALSA PCM and control device files
256 static int gaudio_close_snd_dev(struct gaudio
*gau
)
258 struct gaudio_snd_dev
*snd
;
260 /* Close control device */
263 filp_close(snd
->filp
, NULL
);
265 /* Close PCM playback device and setup substream */
266 snd
= &gau
->playback
;
268 filp_close(snd
->filp
, NULL
);
270 /* Close PCM capture device and setup substream */
273 filp_close(snd
->filp
, NULL
);
279 * gaudio_setup - setup ALSA interface and preparing for USB transfer
281 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
283 * Returns negative errno, or zero on success
285 int gaudio_setup(struct gaudio
*card
)
289 ret
= gaudio_open_snd_dev(card
);
291 ERROR(card
, "we need at least one control device\n");
298 * gaudio_cleanup - remove ALSA device interface
300 * This is called to free all resources allocated by @gaudio_setup().
302 void gaudio_cleanup(struct gaudio
*the_card
)
305 gaudio_close_snd_dev(the_card
);