xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/function/u_uac1_legacy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * u_uac1.c -- ALSA audio utilities for Gadget stack
4  *
5  * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
6  * Copyright (C) 2008 Analog Devices, Inc
7  */
8 
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>
17 
18 #include "u_uac1_legacy.h"
19 
20 /*
21  * This component encapsulates the ALSA devices for USB audio gadget
22  */
23 
24 /*-------------------------------------------------------------------------*/
25 
26 /*
27  * Some ALSA internal helper functions
28  */
snd_interval_refine_set(struct snd_interval * i,unsigned int val)29 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
30 {
31 	struct snd_interval t;
32 	t.empty = 0;
33 	t.min = t.max = val;
34 	t.openmin = t.openmax = 0;
35 	t.integer = 1;
36 	return snd_interval_refine(i, &t);
37 }
38 
_snd_pcm_hw_param_set(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int dir)39 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
40 				 snd_pcm_hw_param_t var, unsigned int val,
41 				 int dir)
42 {
43 	int changed;
44 	if (hw_is_mask(var)) {
45 		struct snd_mask *m = hw_param_mask(params, var);
46 		if (val == 0 && dir < 0) {
47 			changed = -EINVAL;
48 			snd_mask_none(m);
49 		} else {
50 			if (dir > 0)
51 				val++;
52 			else if (dir < 0)
53 				val--;
54 			changed = snd_mask_refine_set(
55 					hw_param_mask(params, var), val);
56 		}
57 	} else if (hw_is_interval(var)) {
58 		struct snd_interval *i = hw_param_interval(params, var);
59 		if (val == 0 && dir < 0) {
60 			changed = -EINVAL;
61 			snd_interval_none(i);
62 		} else if (dir == 0)
63 			changed = snd_interval_refine_set(i, val);
64 		else {
65 			struct snd_interval t;
66 			t.openmin = 1;
67 			t.openmax = 1;
68 			t.empty = 0;
69 			t.integer = 0;
70 			if (dir < 0) {
71 				t.min = val - 1;
72 				t.max = val;
73 			} else {
74 				t.min = val;
75 				t.max = val+1;
76 			}
77 			changed = snd_interval_refine(i, &t);
78 		}
79 	} else
80 		return -EINVAL;
81 	if (changed) {
82 		params->cmask |= 1 << var;
83 		params->rmask |= 1 << var;
84 	}
85 	return changed;
86 }
87 /*-------------------------------------------------------------------------*/
88 
89 /*
90  * Set default hardware params
91  */
playback_default_hw_params(struct gaudio_snd_dev * snd)92 static int playback_default_hw_params(struct gaudio_snd_dev *snd)
93 {
94 	struct snd_pcm_substream *substream = snd->substream;
95 	struct snd_pcm_hw_params *params;
96 	snd_pcm_sframes_t result;
97 
98        /*
99 	* SNDRV_PCM_ACCESS_RW_INTERLEAVED,
100 	* SNDRV_PCM_FORMAT_S16_LE
101 	* CHANNELS: 2
102 	* RATE: 48000
103 	*/
104 	snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
105 	snd->format = SNDRV_PCM_FORMAT_S16_LE;
106 	snd->channels = 2;
107 	snd->rate = 48000;
108 
109 	params = kzalloc(sizeof(*params), GFP_KERNEL);
110 	if (!params)
111 		return -ENOMEM;
112 
113 	_snd_pcm_hw_params_any(params);
114 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
115 			snd->access, 0);
116 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
117 			snd->format, 0);
118 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
119 			snd->channels, 0);
120 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
121 			snd->rate, 0);
122 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
123 			      snd->rate / 10, 0);
124 	_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
125 			      snd->rate, 0);
126 
127 	snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
128 	snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params);
129 
130 	result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
131 	if (result < 0) {
132 		ERROR(snd->card,
133 			"Preparing sound card failed: %d\n", (int)result);
134 		kfree(params);
135 		return result;
136 	}
137 
138 	/* Store the hardware parameters */
139 	snd->access = params_access(params);
140 	snd->format = params_format(params);
141 	snd->channels = params_channels(params);
142 	snd->rate = params_rate(params);
143 
144 	kfree(params);
145 
146 	INFO(snd->card,
147 		"Hardware params: access %x, format %x, channels %d, rate %d\n",
148 		snd->access, snd->format, snd->channels, snd->rate);
149 
150 	return 0;
151 }
152 
153 /*
154  * Playback audio buffer data by ALSA PCM device
155  */
u_audio_playback(struct gaudio * card,void * buf,size_t count)156 size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
157 {
158 	struct gaudio_snd_dev	*snd = &card->playback;
159 	struct snd_pcm_substream *substream = snd->substream;
160 	struct snd_pcm_runtime *runtime = substream->runtime;
161 	ssize_t result;
162 	snd_pcm_sframes_t frames;
163 
164 try_again:
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);
169 		if (result < 0) {
170 			ERROR(card, "Preparing sound card failed: %d\n",
171 					(int)result);
172 			return result;
173 		}
174 	}
175 
176 	frames = bytes_to_frames(runtime, count);
177 	result = snd_pcm_kernel_write(snd->substream, buf, frames);
178 	if (result != frames) {
179 		ERROR(card, "Playback error: %d\n", (int)result);
180 		goto try_again;
181 	}
182 
183 	return 0;
184 }
185 
u_audio_get_playback_channels(struct gaudio * card)186 int u_audio_get_playback_channels(struct gaudio *card)
187 {
188 	return card->playback.channels;
189 }
190 
u_audio_get_playback_rate(struct gaudio * card)191 int u_audio_get_playback_rate(struct gaudio *card)
192 {
193 	return card->playback.rate;
194 }
195 
196 /*
197  * Open ALSA PCM and control device files
198  * Initial the PCM or control device
199  */
gaudio_open_snd_dev(struct gaudio * card)200 static int gaudio_open_snd_dev(struct gaudio *card)
201 {
202 	struct snd_pcm_file *pcm_file;
203 	struct gaudio_snd_dev *snd;
204 	struct f_uac1_legacy_opts *opts;
205 	char *fn_play, *fn_cap, *fn_cntl;
206 
207 	opts = container_of(card->func.fi, struct f_uac1_legacy_opts,
208 			    func_inst);
209 	fn_play = opts->fn_play;
210 	fn_cap = opts->fn_cap;
211 	fn_cntl = opts->fn_cntl;
212 
213 	/* Open control device */
214 	snd = &card->control;
215 	snd->filp = filp_open(fn_cntl, O_RDWR, 0);
216 	if (IS_ERR(snd->filp)) {
217 		int ret = PTR_ERR(snd->filp);
218 		ERROR(card, "unable to open sound control device file: %s\n",
219 				fn_cntl);
220 		snd->filp = NULL;
221 		return ret;
222 	}
223 	snd->card = card;
224 
225 	/* Open PCM playback device and setup substream */
226 	snd = &card->playback;
227 	snd->filp = filp_open(fn_play, O_WRONLY, 0);
228 	if (IS_ERR(snd->filp)) {
229 		int ret = PTR_ERR(snd->filp);
230 
231 		ERROR(card, "No such PCM playback device: %s\n", fn_play);
232 		snd->filp = NULL;
233 		return ret;
234 	}
235 	pcm_file = snd->filp->private_data;
236 	snd->substream = pcm_file->substream;
237 	snd->card = card;
238 	playback_default_hw_params(snd);
239 
240 	/* Open PCM capture device and setup substream */
241 	snd = &card->capture;
242 	snd->filp = filp_open(fn_cap, O_RDONLY, 0);
243 	if (IS_ERR(snd->filp)) {
244 		ERROR(card, "No such PCM capture device: %s\n", fn_cap);
245 		snd->substream = NULL;
246 		snd->card = NULL;
247 		snd->filp = NULL;
248 	} else {
249 		pcm_file = snd->filp->private_data;
250 		snd->substream = pcm_file->substream;
251 		snd->card = card;
252 	}
253 
254 	return 0;
255 }
256 
257 /*
258  * Close ALSA PCM and control device files
259  */
gaudio_close_snd_dev(struct gaudio * gau)260 static int gaudio_close_snd_dev(struct gaudio *gau)
261 {
262 	struct gaudio_snd_dev	*snd;
263 
264 	/* Close control device */
265 	snd = &gau->control;
266 	if (snd->filp)
267 		filp_close(snd->filp, NULL);
268 
269 	/* Close PCM playback device and setup substream */
270 	snd = &gau->playback;
271 	if (snd->filp)
272 		filp_close(snd->filp, NULL);
273 
274 	/* Close PCM capture device and setup substream */
275 	snd = &gau->capture;
276 	if (snd->filp)
277 		filp_close(snd->filp, NULL);
278 
279 	return 0;
280 }
281 
282 /*
283  * gaudio_setup - setup ALSA interface and preparing for USB transfer
284  *
285  * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
286  *
287  * Returns negative errno, or zero on success
288  */
gaudio_setup(struct gaudio * card)289 int gaudio_setup(struct gaudio *card)
290 {
291 	int	ret;
292 
293 	ret = gaudio_open_snd_dev(card);
294 	if (ret)
295 		ERROR(card, "we need at least one control device\n");
296 
297 	return ret;
298 
299 }
300 
301 /*
302  * gaudio_cleanup - remove ALSA device interface
303  *
304  * This is called to free all resources allocated by @gaudio_setup().
305  */
gaudio_cleanup(struct gaudio * the_card)306 void gaudio_cleanup(struct gaudio *the_card)
307 {
308 	if (the_card)
309 		gaudio_close_snd_dev(the_card);
310 }
311 
312