x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / gadget / function / u_uac1_legacy.c
blobcbc868d117af9087dcf466fa804737bed2882288
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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/
9 */
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 /*-------------------------------------------------------------------------*/
28 /**
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;
34 t.empty = 0;
35 t.min = t.max = val;
36 t.openmin = t.openmax = 0;
37 t.integer = 1;
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,
43 int dir)
45 int changed;
46 if (hw_is_mask(var)) {
47 struct snd_mask *m = hw_param_mask(params, var);
48 if (val == 0 && dir < 0) {
49 changed = -EINVAL;
50 snd_mask_none(m);
51 } else {
52 if (dir > 0)
53 val++;
54 else if (dir < 0)
55 val--;
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) {
62 changed = -EINVAL;
63 snd_interval_none(i);
64 } else if (dir == 0)
65 changed = snd_interval_refine_set(i, val);
66 else {
67 struct snd_interval t;
68 t.openmin = 1;
69 t.openmax = 1;
70 t.empty = 0;
71 t.integer = 0;
72 if (dir < 0) {
73 t.min = val - 1;
74 t.max = val;
75 } else {
76 t.min = val;
77 t.max = val+1;
79 changed = snd_interval_refine(i, &t);
81 } else
82 return -EINVAL;
83 if (changed) {
84 params->cmask |= 1 << var;
85 params->rmask |= 1 << var;
87 return changed;
89 /*-------------------------------------------------------------------------*/
91 /**
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
103 * CHANNELS: 2
104 * RATE: 48000
106 snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
107 snd->format = SNDRV_PCM_FORMAT_S16_LE;
108 snd->channels = 2;
109 snd->rate = 48000;
111 params = kzalloc(sizeof(*params), GFP_KERNEL);
112 if (!params)
113 return -ENOMEM;
115 _snd_pcm_hw_params_any(params);
116 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
117 snd->access, 0);
118 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
119 snd->format, 0);
120 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
121 snd->channels, 0);
122 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
123 snd->rate, 0);
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);
129 if (result < 0) {
130 ERROR(snd->card,
131 "Preparing sound card failed: %d\n", (int)result);
132 kfree(params);
133 return 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);
142 kfree(params);
144 INFO(snd->card,
145 "Hardware params: access %x, format %x, channels %d, rate %d\n",
146 snd->access, snd->format, snd->channels, snd->rate);
148 return 0;
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;
159 ssize_t result;
160 snd_pcm_sframes_t frames;
162 try_again:
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);
167 if (result < 0) {
168 ERROR(card, "Preparing sound card failed: %d\n",
169 (int)result);
170 return result;
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);
178 goto try_again;
181 return 0;
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,
206 func_inst);
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",
217 fn_cntl);
218 snd->filp = NULL;
219 return ret;
221 snd->card = card;
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);
230 snd->filp = NULL;
231 return ret;
233 pcm_file = snd->filp->private_data;
234 snd->substream = pcm_file->substream;
235 snd->card = card;
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;
244 snd->card = NULL;
245 snd->filp = NULL;
246 } else {
247 pcm_file = snd->filp->private_data;
248 snd->substream = pcm_file->substream;
249 snd->card = card;
252 return 0;
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 */
263 snd = &gau->control;
264 if (snd->filp)
265 filp_close(snd->filp, NULL);
267 /* Close PCM playback device and setup substream */
268 snd = &gau->playback;
269 if (snd->filp)
270 filp_close(snd->filp, NULL);
272 /* Close PCM capture device and setup substream */
273 snd = &gau->capture;
274 if (snd->filp)
275 filp_close(snd->filp, NULL);
277 return 0;
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)
289 int ret;
291 ret = gaudio_open_snd_dev(card);
292 if (ret)
293 ERROR(card, "we need at least one control device\n");
295 return ret;
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)
306 if (the_card)
307 gaudio_close_snd_dev(the_card);