• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Digital Audio (PCM) abstract layer / OSS compatible
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6 
7 #if 0
8 #define PLUGIN_DEBUG
9 #endif
10 #if 0
11 #define OSS_DEBUG
12 #endif
13 
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/time.h>
18 #include <linux/vmalloc.h>
19 #include <linux/module.h>
20 #include <linux/math64.h>
21 #include <linux/string.h>
22 #include <linux/compat.h>
23 #include <sound/core.h>
24 #include <sound/minors.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include "pcm_plugin.h"
28 #include <sound/info.h>
29 #include <linux/soundcard.h>
30 #include <sound/initval.h>
31 #include <sound/mixer_oss.h>
32 
33 #define OSS_ALSAEMULVER		_SIOR ('M', 249, int)
34 
35 static int dsp_map[SNDRV_CARDS];
36 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
37 static bool nonblock_open = 1;
38 
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
40 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
41 MODULE_LICENSE("GPL");
42 module_param_array(dsp_map, int, NULL, 0444);
43 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
44 module_param_array(adsp_map, int, NULL, 0444);
45 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
46 module_param(nonblock_open, bool, 0644);
47 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
48 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
49 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
50 
51 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
52 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
53 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
54 
55 /*
56  * helper functions to process hw_params
57  */
snd_interval_refine_min(struct snd_interval * i,unsigned int min,int openmin)58 static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
59 {
60 	int changed = 0;
61 	if (i->min < min) {
62 		i->min = min;
63 		i->openmin = openmin;
64 		changed = 1;
65 	} else if (i->min == min && !i->openmin && openmin) {
66 		i->openmin = 1;
67 		changed = 1;
68 	}
69 	if (i->integer) {
70 		if (i->openmin) {
71 			i->min++;
72 			i->openmin = 0;
73 		}
74 	}
75 	if (snd_interval_checkempty(i)) {
76 		snd_interval_none(i);
77 		return -EINVAL;
78 	}
79 	return changed;
80 }
81 
snd_interval_refine_max(struct snd_interval * i,unsigned int max,int openmax)82 static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
83 {
84 	int changed = 0;
85 	if (i->max > max) {
86 		i->max = max;
87 		i->openmax = openmax;
88 		changed = 1;
89 	} else if (i->max == max && !i->openmax && openmax) {
90 		i->openmax = 1;
91 		changed = 1;
92 	}
93 	if (i->integer) {
94 		if (i->openmax) {
95 			i->max--;
96 			i->openmax = 0;
97 		}
98 	}
99 	if (snd_interval_checkempty(i)) {
100 		snd_interval_none(i);
101 		return -EINVAL;
102 	}
103 	return changed;
104 }
105 
snd_interval_refine_set(struct snd_interval * i,unsigned int val)106 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
107 {
108 	struct snd_interval t;
109 	t.empty = 0;
110 	t.min = t.max = val;
111 	t.openmin = t.openmax = 0;
112 	t.integer = 1;
113 	return snd_interval_refine(i, &t);
114 }
115 
116 /**
117  * snd_pcm_hw_param_value_min
118  * @params: the hw_params instance
119  * @var: parameter to retrieve
120  * @dir: pointer to the direction (-1,0,1) or NULL
121  *
122  * Return the minimum value for field PAR.
123  */
124 static unsigned int
snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,int * dir)125 snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
126 			   snd_pcm_hw_param_t var, int *dir)
127 {
128 	if (hw_is_mask(var)) {
129 		if (dir)
130 			*dir = 0;
131 		return snd_mask_min(hw_param_mask_c(params, var));
132 	}
133 	if (hw_is_interval(var)) {
134 		const struct snd_interval *i = hw_param_interval_c(params, var);
135 		if (dir)
136 			*dir = i->openmin;
137 		return snd_interval_min(i);
138 	}
139 	return -EINVAL;
140 }
141 
142 /**
143  * snd_pcm_hw_param_value_max
144  * @params: the hw_params instance
145  * @var: parameter to retrieve
146  * @dir: pointer to the direction (-1,0,1) or NULL
147  *
148  * Return the maximum value for field PAR.
149  */
150 static int
snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,int * dir)151 snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
152 			   snd_pcm_hw_param_t var, int *dir)
153 {
154 	if (hw_is_mask(var)) {
155 		if (dir)
156 			*dir = 0;
157 		return snd_mask_max(hw_param_mask_c(params, var));
158 	}
159 	if (hw_is_interval(var)) {
160 		const struct snd_interval *i = hw_param_interval_c(params, var);
161 		if (dir)
162 			*dir = - (int) i->openmax;
163 		return snd_interval_max(i);
164 	}
165 	return -EINVAL;
166 }
167 
_snd_pcm_hw_param_mask(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,const struct snd_mask * val)168 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
169 				  snd_pcm_hw_param_t var,
170 				  const struct snd_mask *val)
171 {
172 	int changed;
173 	changed = snd_mask_refine(hw_param_mask(params, var), val);
174 	if (changed > 0) {
175 		params->cmask |= 1 << var;
176 		params->rmask |= 1 << var;
177 	}
178 	return changed;
179 }
180 
snd_pcm_hw_param_mask(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,const struct snd_mask * val)181 static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
182 				 struct snd_pcm_hw_params *params,
183 				 snd_pcm_hw_param_t var,
184 				 const struct snd_mask *val)
185 {
186 	int changed = _snd_pcm_hw_param_mask(params, var, val);
187 	if (changed < 0)
188 		return changed;
189 	if (params->rmask) {
190 		int err = snd_pcm_hw_refine(pcm, params);
191 		if (err < 0)
192 			return err;
193 	}
194 	return 0;
195 }
196 
_snd_pcm_hw_param_min(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int dir)197 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
198 				 snd_pcm_hw_param_t var, unsigned int val,
199 				 int dir)
200 {
201 	int changed;
202 	int open = 0;
203 	if (dir) {
204 		if (dir > 0) {
205 			open = 1;
206 		} else if (dir < 0) {
207 			if (val > 0) {
208 				open = 1;
209 				val--;
210 			}
211 		}
212 	}
213 	if (hw_is_mask(var))
214 		changed = snd_mask_refine_min(hw_param_mask(params, var),
215 					      val + !!open);
216 	else if (hw_is_interval(var))
217 		changed = snd_interval_refine_min(hw_param_interval(params, var),
218 						  val, open);
219 	else
220 		return -EINVAL;
221 	if (changed > 0) {
222 		params->cmask |= 1 << var;
223 		params->rmask |= 1 << var;
224 	}
225 	return changed;
226 }
227 
228 /**
229  * snd_pcm_hw_param_min
230  * @pcm: PCM instance
231  * @params: the hw_params instance
232  * @var: parameter to retrieve
233  * @val: minimal value
234  * @dir: pointer to the direction (-1,0,1) or NULL
235  *
236  * Inside configuration space defined by PARAMS remove from PAR all
237  * values < VAL. Reduce configuration space accordingly.
238  * Return new minimum or -EINVAL if the configuration space is empty
239  */
snd_pcm_hw_param_min(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int * dir)240 static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
241 				struct snd_pcm_hw_params *params,
242 				snd_pcm_hw_param_t var, unsigned int val,
243 				int *dir)
244 {
245 	int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
246 	if (changed < 0)
247 		return changed;
248 	if (params->rmask) {
249 		int err = snd_pcm_hw_refine(pcm, params);
250 		if (err < 0)
251 			return err;
252 	}
253 	return snd_pcm_hw_param_value_min(params, var, dir);
254 }
255 
_snd_pcm_hw_param_max(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int dir)256 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
257 				 snd_pcm_hw_param_t var, unsigned int val,
258 				 int dir)
259 {
260 	int changed;
261 	int open = 0;
262 	if (dir) {
263 		if (dir < 0) {
264 			open = 1;
265 		} else if (dir > 0) {
266 			open = 1;
267 			val++;
268 		}
269 	}
270 	if (hw_is_mask(var)) {
271 		if (val == 0 && open) {
272 			snd_mask_none(hw_param_mask(params, var));
273 			changed = -EINVAL;
274 		} else
275 			changed = snd_mask_refine_max(hw_param_mask(params, var),
276 						      val - !!open);
277 	} else if (hw_is_interval(var))
278 		changed = snd_interval_refine_max(hw_param_interval(params, var),
279 						  val, open);
280 	else
281 		return -EINVAL;
282 	if (changed > 0) {
283 		params->cmask |= 1 << var;
284 		params->rmask |= 1 << var;
285 	}
286 	return changed;
287 }
288 
289 /**
290  * snd_pcm_hw_param_max
291  * @pcm: PCM instance
292  * @params: the hw_params instance
293  * @var: parameter to retrieve
294  * @val: maximal value
295  * @dir: pointer to the direction (-1,0,1) or NULL
296  *
297  * Inside configuration space defined by PARAMS remove from PAR all
298  *  values >= VAL + 1. Reduce configuration space accordingly.
299  *  Return new maximum or -EINVAL if the configuration space is empty
300  */
snd_pcm_hw_param_max(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int * dir)301 static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
302 				struct snd_pcm_hw_params *params,
303 				snd_pcm_hw_param_t var, unsigned int val,
304 				int *dir)
305 {
306 	int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
307 	if (changed < 0)
308 		return changed;
309 	if (params->rmask) {
310 		int err = snd_pcm_hw_refine(pcm, params);
311 		if (err < 0)
312 			return err;
313 	}
314 	return snd_pcm_hw_param_value_max(params, var, dir);
315 }
316 
boundary_sub(int a,int adir,int b,int bdir,int * c,int * cdir)317 static int boundary_sub(int a, int adir,
318 			int b, int bdir,
319 			int *c, int *cdir)
320 {
321 	adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
322 	bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
323 	*c = a - b;
324 	*cdir = adir - bdir;
325 	if (*cdir == -2) {
326 		(*c)--;
327 	} else if (*cdir == 2) {
328 		(*c)++;
329 	}
330 	return 0;
331 }
332 
boundary_lt(unsigned int a,int adir,unsigned int b,int bdir)333 static int boundary_lt(unsigned int a, int adir,
334 		       unsigned int b, int bdir)
335 {
336 	if (adir < 0) {
337 		a--;
338 		adir = 1;
339 	} else if (adir > 0)
340 		adir = 1;
341 	if (bdir < 0) {
342 		b--;
343 		bdir = 1;
344 	} else if (bdir > 0)
345 		bdir = 1;
346 	return a < b || (a == b && adir < bdir);
347 }
348 
349 /* Return 1 if min is nearer to best than max */
boundary_nearer(int min,int mindir,int best,int bestdir,int max,int maxdir)350 static int boundary_nearer(int min, int mindir,
351 			   int best, int bestdir,
352 			   int max, int maxdir)
353 {
354 	int dmin, dmindir;
355 	int dmax, dmaxdir;
356 	boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
357 	boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
358 	return boundary_lt(dmin, dmindir, dmax, dmaxdir);
359 }
360 
361 /**
362  * snd_pcm_hw_param_near
363  * @pcm: PCM instance
364  * @params: the hw_params instance
365  * @var: parameter to retrieve
366  * @best: value to set
367  * @dir: pointer to the direction (-1,0,1) or NULL
368  *
369  * Inside configuration space defined by PARAMS set PAR to the available value
370  * nearest to VAL. Reduce configuration space accordingly.
371  * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
372  * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
373  * Return the value found.
374   */
snd_pcm_hw_param_near(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int best,int * dir)375 static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
376 				 struct snd_pcm_hw_params *params,
377 				 snd_pcm_hw_param_t var, unsigned int best,
378 				 int *dir)
379 {
380 	struct snd_pcm_hw_params *save = NULL;
381 	int v;
382 	unsigned int saved_min;
383 	int last = 0;
384 	int min, max;
385 	int mindir, maxdir;
386 	int valdir = dir ? *dir : 0;
387 	/* FIXME */
388 	if (best > INT_MAX)
389 		best = INT_MAX;
390 	min = max = best;
391 	mindir = maxdir = valdir;
392 	if (maxdir > 0)
393 		maxdir = 0;
394 	else if (maxdir == 0)
395 		maxdir = -1;
396 	else {
397 		maxdir = 1;
398 		max--;
399 	}
400 	save = kmalloc(sizeof(*save), GFP_KERNEL);
401 	if (save == NULL)
402 		return -ENOMEM;
403 	*save = *params;
404 	saved_min = min;
405 	min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
406 	if (min >= 0) {
407 		struct snd_pcm_hw_params *params1;
408 		if (max < 0)
409 			goto _end;
410 		if ((unsigned int)min == saved_min && mindir == valdir)
411 			goto _end;
412 		params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
413 		if (params1 == NULL) {
414 			kfree(save);
415 			return -ENOMEM;
416 		}
417 		*params1 = *save;
418 		max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
419 		if (max < 0) {
420 			kfree(params1);
421 			goto _end;
422 		}
423 		if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
424 			*params = *params1;
425 			last = 1;
426 		}
427 		kfree(params1);
428 	} else {
429 		*params = *save;
430 		max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
431 		if (max < 0) {
432 			kfree(save);
433 			return max;
434 		}
435 		last = 1;
436 	}
437  _end:
438  	kfree(save);
439 	if (last)
440 		v = snd_pcm_hw_param_last(pcm, params, var, dir);
441 	else
442 		v = snd_pcm_hw_param_first(pcm, params, var, dir);
443 	return v;
444 }
445 
_snd_pcm_hw_param_set(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int dir)446 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
447 				 snd_pcm_hw_param_t var, unsigned int val,
448 				 int dir)
449 {
450 	int changed;
451 	if (hw_is_mask(var)) {
452 		struct snd_mask *m = hw_param_mask(params, var);
453 		if (val == 0 && dir < 0) {
454 			changed = -EINVAL;
455 			snd_mask_none(m);
456 		} else {
457 			if (dir > 0)
458 				val++;
459 			else if (dir < 0)
460 				val--;
461 			changed = snd_mask_refine_set(hw_param_mask(params, var), val);
462 		}
463 	} else if (hw_is_interval(var)) {
464 		struct snd_interval *i = hw_param_interval(params, var);
465 		if (val == 0 && dir < 0) {
466 			changed = -EINVAL;
467 			snd_interval_none(i);
468 		} else if (dir == 0)
469 			changed = snd_interval_refine_set(i, val);
470 		else {
471 			struct snd_interval t;
472 			t.openmin = 1;
473 			t.openmax = 1;
474 			t.empty = 0;
475 			t.integer = 0;
476 			if (dir < 0) {
477 				t.min = val - 1;
478 				t.max = val;
479 			} else {
480 				t.min = val;
481 				t.max = val+1;
482 			}
483 			changed = snd_interval_refine(i, &t);
484 		}
485 	} else
486 		return -EINVAL;
487 	if (changed > 0) {
488 		params->cmask |= 1 << var;
489 		params->rmask |= 1 << var;
490 	}
491 	return changed;
492 }
493 
494 /**
495  * snd_pcm_hw_param_set
496  * @pcm: PCM instance
497  * @params: the hw_params instance
498  * @var: parameter to retrieve
499  * @val: value to set
500  * @dir: pointer to the direction (-1,0,1) or NULL
501  *
502  * Inside configuration space defined by PARAMS remove from PAR all
503  * values != VAL. Reduce configuration space accordingly.
504  *  Return VAL or -EINVAL if the configuration space is empty
505  */
snd_pcm_hw_param_set(struct snd_pcm_substream * pcm,struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var,unsigned int val,int dir)506 static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
507 				struct snd_pcm_hw_params *params,
508 				snd_pcm_hw_param_t var, unsigned int val,
509 				int dir)
510 {
511 	int changed = _snd_pcm_hw_param_set(params, var, val, dir);
512 	if (changed < 0)
513 		return changed;
514 	if (params->rmask) {
515 		int err = snd_pcm_hw_refine(pcm, params);
516 		if (err < 0)
517 			return err;
518 	}
519 	return snd_pcm_hw_param_value(params, var, NULL);
520 }
521 
_snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)522 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
523 					snd_pcm_hw_param_t var)
524 {
525 	int changed;
526 	changed = snd_interval_setinteger(hw_param_interval(params, var));
527 	if (changed > 0) {
528 		params->cmask |= 1 << var;
529 		params->rmask |= 1 << var;
530 	}
531 	return changed;
532 }
533 
534 /*
535  * plugin
536  */
537 
538 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
snd_pcm_oss_plugin_clear(struct snd_pcm_substream * substream)539 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
540 {
541 	struct snd_pcm_runtime *runtime = substream->runtime;
542 	struct snd_pcm_plugin *plugin, *next;
543 
544 	plugin = runtime->oss.plugin_first;
545 	while (plugin) {
546 		next = plugin->next;
547 		snd_pcm_plugin_free(plugin);
548 		plugin = next;
549 	}
550 	runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
551 	return 0;
552 }
553 
snd_pcm_plugin_insert(struct snd_pcm_plugin * plugin)554 static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
555 {
556 	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
557 	plugin->next = runtime->oss.plugin_first;
558 	plugin->prev = NULL;
559 	if (runtime->oss.plugin_first) {
560 		runtime->oss.plugin_first->prev = plugin;
561 		runtime->oss.plugin_first = plugin;
562 	} else {
563 		runtime->oss.plugin_last =
564 		runtime->oss.plugin_first = plugin;
565 	}
566 	return 0;
567 }
568 
snd_pcm_plugin_append(struct snd_pcm_plugin * plugin)569 int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
570 {
571 	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
572 	plugin->next = NULL;
573 	plugin->prev = runtime->oss.plugin_last;
574 	if (runtime->oss.plugin_last) {
575 		runtime->oss.plugin_last->next = plugin;
576 		runtime->oss.plugin_last = plugin;
577 	} else {
578 		runtime->oss.plugin_last =
579 		runtime->oss.plugin_first = plugin;
580 	}
581 	return 0;
582 }
583 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
584 
snd_pcm_oss_bytes(struct snd_pcm_substream * substream,long frames)585 static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
586 {
587 	struct snd_pcm_runtime *runtime = substream->runtime;
588 	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
589 	long bytes = frames_to_bytes(runtime, frames);
590 	if (buffer_size == runtime->oss.buffer_bytes)
591 		return bytes;
592 #if BITS_PER_LONG >= 64
593 	return runtime->oss.buffer_bytes * bytes / buffer_size;
594 #else
595 	{
596 		u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
597 		return div_u64(bsize, buffer_size);
598 	}
599 #endif
600 }
601 
snd_pcm_alsa_frames(struct snd_pcm_substream * substream,long bytes)602 static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
603 {
604 	struct snd_pcm_runtime *runtime = substream->runtime;
605 	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
606 	if (buffer_size == runtime->oss.buffer_bytes)
607 		return bytes_to_frames(runtime, bytes);
608 	return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
609 }
610 
611 static inline
get_hw_ptr_period(struct snd_pcm_runtime * runtime)612 snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
613 {
614 	return runtime->hw_ptr_interrupt;
615 }
616 
617 /* define extended formats in the recent OSS versions (if any) */
618 /* linear formats */
619 #define AFMT_S32_LE      0x00001000
620 #define AFMT_S32_BE      0x00002000
621 #define AFMT_S24_LE      0x00008000
622 #define AFMT_S24_BE      0x00010000
623 #define AFMT_S24_PACKED  0x00040000
624 
625 /* other supported formats */
626 #define AFMT_FLOAT       0x00004000
627 #define AFMT_SPDIF_RAW   0x00020000
628 
629 /* unsupported formats */
630 #define AFMT_AC3         0x00000400
631 #define AFMT_VORBIS      0x00000800
632 
snd_pcm_oss_format_from(int format)633 static snd_pcm_format_t snd_pcm_oss_format_from(int format)
634 {
635 	switch (format) {
636 	case AFMT_MU_LAW:	return SNDRV_PCM_FORMAT_MU_LAW;
637 	case AFMT_A_LAW:	return SNDRV_PCM_FORMAT_A_LAW;
638 	case AFMT_IMA_ADPCM:	return SNDRV_PCM_FORMAT_IMA_ADPCM;
639 	case AFMT_U8:		return SNDRV_PCM_FORMAT_U8;
640 	case AFMT_S16_LE:	return SNDRV_PCM_FORMAT_S16_LE;
641 	case AFMT_S16_BE:	return SNDRV_PCM_FORMAT_S16_BE;
642 	case AFMT_S8:		return SNDRV_PCM_FORMAT_S8;
643 	case AFMT_U16_LE:	return SNDRV_PCM_FORMAT_U16_LE;
644 	case AFMT_U16_BE:	return SNDRV_PCM_FORMAT_U16_BE;
645 	case AFMT_MPEG:		return SNDRV_PCM_FORMAT_MPEG;
646 	case AFMT_S32_LE:	return SNDRV_PCM_FORMAT_S32_LE;
647 	case AFMT_S32_BE:	return SNDRV_PCM_FORMAT_S32_BE;
648 	case AFMT_S24_LE:	return SNDRV_PCM_FORMAT_S24_LE;
649 	case AFMT_S24_BE:	return SNDRV_PCM_FORMAT_S24_BE;
650 	case AFMT_S24_PACKED:	return SNDRV_PCM_FORMAT_S24_3LE;
651 	case AFMT_FLOAT:	return SNDRV_PCM_FORMAT_FLOAT;
652 	case AFMT_SPDIF_RAW:	return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
653 	default:		return SNDRV_PCM_FORMAT_U8;
654 	}
655 }
656 
snd_pcm_oss_format_to(snd_pcm_format_t format)657 static int snd_pcm_oss_format_to(snd_pcm_format_t format)
658 {
659 	switch (format) {
660 	case SNDRV_PCM_FORMAT_MU_LAW:	return AFMT_MU_LAW;
661 	case SNDRV_PCM_FORMAT_A_LAW:	return AFMT_A_LAW;
662 	case SNDRV_PCM_FORMAT_IMA_ADPCM:	return AFMT_IMA_ADPCM;
663 	case SNDRV_PCM_FORMAT_U8:		return AFMT_U8;
664 	case SNDRV_PCM_FORMAT_S16_LE:	return AFMT_S16_LE;
665 	case SNDRV_PCM_FORMAT_S16_BE:	return AFMT_S16_BE;
666 	case SNDRV_PCM_FORMAT_S8:		return AFMT_S8;
667 	case SNDRV_PCM_FORMAT_U16_LE:	return AFMT_U16_LE;
668 	case SNDRV_PCM_FORMAT_U16_BE:	return AFMT_U16_BE;
669 	case SNDRV_PCM_FORMAT_MPEG:		return AFMT_MPEG;
670 	case SNDRV_PCM_FORMAT_S32_LE:	return AFMT_S32_LE;
671 	case SNDRV_PCM_FORMAT_S32_BE:	return AFMT_S32_BE;
672 	case SNDRV_PCM_FORMAT_S24_LE:	return AFMT_S24_LE;
673 	case SNDRV_PCM_FORMAT_S24_BE:	return AFMT_S24_BE;
674 	case SNDRV_PCM_FORMAT_S24_3LE:	return AFMT_S24_PACKED;
675 	case SNDRV_PCM_FORMAT_FLOAT:	return AFMT_FLOAT;
676 	case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
677 	default:			return -EINVAL;
678 	}
679 }
680 
snd_pcm_oss_period_size(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * oss_params,struct snd_pcm_hw_params * slave_params)681 static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
682 				   struct snd_pcm_hw_params *oss_params,
683 				   struct snd_pcm_hw_params *slave_params)
684 {
685 	ssize_t s;
686 	ssize_t oss_buffer_size;
687 	ssize_t oss_period_size, oss_periods;
688 	ssize_t min_period_size, max_period_size;
689 	struct snd_pcm_runtime *runtime = substream->runtime;
690 	size_t oss_frame_size;
691 
692 	oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
693 			 params_channels(oss_params) / 8;
694 
695 	oss_buffer_size = snd_pcm_hw_param_value_max(slave_params,
696 						     SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
697 						     NULL);
698 	if (oss_buffer_size <= 0)
699 		return -EINVAL;
700 	oss_buffer_size = snd_pcm_plug_client_size(substream,
701 						   oss_buffer_size * oss_frame_size);
702 	if (oss_buffer_size <= 0)
703 		return -EINVAL;
704 	oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
705 	if (atomic_read(&substream->mmap_count)) {
706 		if (oss_buffer_size > runtime->oss.mmap_bytes)
707 			oss_buffer_size = runtime->oss.mmap_bytes;
708 	}
709 
710 	if (substream->oss.setup.period_size > 16)
711 		oss_period_size = substream->oss.setup.period_size;
712 	else if (runtime->oss.fragshift) {
713 		oss_period_size = 1 << runtime->oss.fragshift;
714 		if (oss_period_size > oss_buffer_size / 2)
715 			oss_period_size = oss_buffer_size / 2;
716 	} else {
717 		int sd;
718 		size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
719 
720 		oss_period_size = oss_buffer_size;
721 		do {
722 			oss_period_size /= 2;
723 		} while (oss_period_size > bytes_per_sec);
724 		if (runtime->oss.subdivision == 0) {
725 			sd = 4;
726 			if (oss_period_size / sd > 4096)
727 				sd *= 2;
728 			if (oss_period_size / sd < 4096)
729 				sd = 1;
730 		} else
731 			sd = runtime->oss.subdivision;
732 		oss_period_size /= sd;
733 		if (oss_period_size < 16)
734 			oss_period_size = 16;
735 	}
736 
737 	min_period_size = snd_pcm_plug_client_size(substream,
738 						   snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
739 	if (min_period_size > 0) {
740 		min_period_size *= oss_frame_size;
741 		min_period_size = roundup_pow_of_two(min_period_size);
742 		if (oss_period_size < min_period_size)
743 			oss_period_size = min_period_size;
744 	}
745 
746 	max_period_size = snd_pcm_plug_client_size(substream,
747 						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
748 	if (max_period_size > 0) {
749 		max_period_size *= oss_frame_size;
750 		max_period_size = rounddown_pow_of_two(max_period_size);
751 		if (oss_period_size > max_period_size)
752 			oss_period_size = max_period_size;
753 	}
754 
755 	oss_periods = oss_buffer_size / oss_period_size;
756 
757 	if (substream->oss.setup.periods > 1)
758 		oss_periods = substream->oss.setup.periods;
759 
760 	s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
761 	if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
762 		s = runtime->oss.maxfrags;
763 	if (oss_periods > s)
764 		oss_periods = s;
765 
766 	s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
767 	if (s < 2)
768 		s = 2;
769 	if (oss_periods < s)
770 		oss_periods = s;
771 
772 	while (oss_period_size * oss_periods > oss_buffer_size)
773 		oss_period_size /= 2;
774 
775 	if (oss_period_size < 16)
776 		return -EINVAL;
777 	runtime->oss.period_bytes = oss_period_size;
778 	runtime->oss.period_frames = 1;
779 	runtime->oss.periods = oss_periods;
780 	return 0;
781 }
782 
choose_rate(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,unsigned int best_rate)783 static int choose_rate(struct snd_pcm_substream *substream,
784 		       struct snd_pcm_hw_params *params, unsigned int best_rate)
785 {
786 	const struct snd_interval *it;
787 	struct snd_pcm_hw_params *save;
788 	unsigned int rate, prev;
789 
790 	save = kmalloc(sizeof(*save), GFP_KERNEL);
791 	if (save == NULL)
792 		return -ENOMEM;
793 	*save = *params;
794 	it = hw_param_interval_c(save, SNDRV_PCM_HW_PARAM_RATE);
795 
796 	/* try multiples of the best rate */
797 	rate = best_rate;
798 	for (;;) {
799 		if (it->max < rate || (it->max == rate && it->openmax))
800 			break;
801 		if (it->min < rate || (it->min == rate && !it->openmin)) {
802 			int ret;
803 			ret = snd_pcm_hw_param_set(substream, params,
804 						   SNDRV_PCM_HW_PARAM_RATE,
805 						   rate, 0);
806 			if (ret == (int)rate) {
807 				kfree(save);
808 				return rate;
809 			}
810 			*params = *save;
811 		}
812 		prev = rate;
813 		rate += best_rate;
814 		if (rate <= prev)
815 			break;
816 	}
817 
818 	/* not found, use the nearest rate */
819 	kfree(save);
820 	return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
821 }
822 
823 /* parameter locking: returns immediately if tried during streaming */
lock_params(struct snd_pcm_runtime * runtime)824 static int lock_params(struct snd_pcm_runtime *runtime)
825 {
826 	if (mutex_lock_interruptible(&runtime->oss.params_lock))
827 		return -ERESTARTSYS;
828 	if (atomic_read(&runtime->oss.rw_ref)) {
829 		mutex_unlock(&runtime->oss.params_lock);
830 		return -EBUSY;
831 	}
832 	return 0;
833 }
834 
unlock_params(struct snd_pcm_runtime * runtime)835 static void unlock_params(struct snd_pcm_runtime *runtime)
836 {
837 	mutex_unlock(&runtime->oss.params_lock);
838 }
839 
840 /* call with params_lock held */
snd_pcm_oss_change_params_locked(struct snd_pcm_substream * substream)841 static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
842 {
843 	struct snd_pcm_runtime *runtime = substream->runtime;
844 	struct snd_pcm_hw_params *params, *sparams;
845 	struct snd_pcm_sw_params *sw_params;
846 	ssize_t oss_buffer_size, oss_period_size;
847 	size_t oss_frame_size;
848 	int err;
849 	int direct;
850 	snd_pcm_format_t format, sformat;
851 	int n;
852 	const struct snd_mask *sformat_mask;
853 	struct snd_mask mask;
854 
855 	if (!runtime->oss.params)
856 		return 0;
857 	sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
858 	params = kmalloc(sizeof(*params), GFP_KERNEL);
859 	sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
860 	if (!sw_params || !params || !sparams) {
861 		err = -ENOMEM;
862 		goto failure;
863 	}
864 
865 	if (atomic_read(&substream->mmap_count))
866 		direct = 1;
867 	else
868 		direct = substream->oss.setup.direct;
869 
870 	_snd_pcm_hw_params_any(sparams);
871 	_snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
872 	_snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
873 	snd_mask_none(&mask);
874 	if (atomic_read(&substream->mmap_count))
875 		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
876 	else {
877 		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
878 		if (!direct)
879 			snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
880 	}
881 	err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
882 	if (err < 0) {
883 		pcm_dbg(substream->pcm, "No usable accesses\n");
884 		err = -EINVAL;
885 		goto failure;
886 	}
887 
888 	err = choose_rate(substream, sparams, runtime->oss.rate);
889 	if (err < 0)
890 		goto failure;
891 	err = snd_pcm_hw_param_near(substream, sparams,
892 				    SNDRV_PCM_HW_PARAM_CHANNELS,
893 				    runtime->oss.channels, NULL);
894 	if (err < 0)
895 		goto failure;
896 
897 	format = snd_pcm_oss_format_from(runtime->oss.format);
898 
899 	sformat_mask = hw_param_mask_c(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
900 	if (direct)
901 		sformat = format;
902 	else
903 		sformat = snd_pcm_plug_slave_format(format, sformat_mask);
904 
905 	if ((__force int)sformat < 0 ||
906 	    !snd_mask_test_format(sformat_mask, sformat)) {
907 		pcm_for_each_format(sformat) {
908 			if (snd_mask_test_format(sformat_mask, sformat) &&
909 			    snd_pcm_oss_format_to(sformat) >= 0)
910 				goto format_found;
911 		}
912 		pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
913 		err = -EINVAL;
914 		goto failure;
915 	}
916  format_found:
917 	err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
918 	if (err < 0)
919 		goto failure;
920 
921 	if (direct) {
922 		memcpy(params, sparams, sizeof(*params));
923 	} else {
924 		_snd_pcm_hw_params_any(params);
925 		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
926 				      (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
927 		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
928 				      (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
929 		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
930 				      runtime->oss.channels, 0);
931 		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
932 				      runtime->oss.rate, 0);
933 		pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
934 			 params_access(params), params_format(params),
935 			 params_channels(params), params_rate(params));
936 	}
937 	pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
938 		 params_access(sparams), params_format(sparams),
939 		 params_channels(sparams), params_rate(sparams));
940 
941 	oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
942 			 params_channels(params) / 8;
943 
944 	err = snd_pcm_oss_period_size(substream, params, sparams);
945 	if (err < 0)
946 		goto failure;
947 
948 	n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
949 	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
950 	if (err < 0)
951 		goto failure;
952 
953 	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
954 				     runtime->oss.periods, NULL);
955 	if (err < 0)
956 		goto failure;
957 
958 	snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
959 
960 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
961 	if (err < 0) {
962 		pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
963 		goto failure;
964 	}
965 
966 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
967 	snd_pcm_oss_plugin_clear(substream);
968 	if (!direct) {
969 		/* add necessary plugins */
970 		snd_pcm_oss_plugin_clear(substream);
971 		if ((err = snd_pcm_plug_format_plugins(substream,
972 						       params,
973 						       sparams)) < 0) {
974 			pcm_dbg(substream->pcm,
975 				"snd_pcm_plug_format_plugins failed: %i\n", err);
976 			snd_pcm_oss_plugin_clear(substream);
977 			goto failure;
978 		}
979 		if (runtime->oss.plugin_first) {
980 			struct snd_pcm_plugin *plugin;
981 			if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
982 				pcm_dbg(substream->pcm,
983 					"snd_pcm_plugin_build_io failed: %i\n", err);
984 				snd_pcm_oss_plugin_clear(substream);
985 				goto failure;
986 			}
987 			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
988 				err = snd_pcm_plugin_append(plugin);
989 			} else {
990 				err = snd_pcm_plugin_insert(plugin);
991 			}
992 			if (err < 0) {
993 				snd_pcm_oss_plugin_clear(substream);
994 				goto failure;
995 			}
996 		}
997 	}
998 #endif
999 
1000 	if (runtime->oss.trigger) {
1001 		sw_params->start_threshold = 1;
1002 	} else {
1003 		sw_params->start_threshold = runtime->boundary;
1004 	}
1005 	if (atomic_read(&substream->mmap_count) ||
1006 	    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1007 		sw_params->stop_threshold = runtime->boundary;
1008 	else
1009 		sw_params->stop_threshold = runtime->buffer_size;
1010 	sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
1011 	sw_params->period_step = 1;
1012 	sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1013 		1 : runtime->period_size;
1014 	if (atomic_read(&substream->mmap_count) ||
1015 	    substream->oss.setup.nosilence) {
1016 		sw_params->silence_threshold = 0;
1017 		sw_params->silence_size = 0;
1018 	} else {
1019 		snd_pcm_uframes_t frames;
1020 		frames = runtime->period_size + 16;
1021 		if (frames > runtime->buffer_size)
1022 			frames = runtime->buffer_size;
1023 		sw_params->silence_threshold = frames;
1024 		sw_params->silence_size = frames;
1025 	}
1026 
1027 	if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1028 		pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
1029 		goto failure;
1030 	}
1031 
1032 	runtime->oss.periods = params_periods(sparams);
1033 	oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1034 	if (oss_period_size < 0) {
1035 		err = -EINVAL;
1036 		goto failure;
1037 	}
1038 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1039 	if (runtime->oss.plugin_first) {
1040 		err = snd_pcm_plug_alloc(substream, oss_period_size);
1041 		if (err < 0)
1042 			goto failure;
1043 	}
1044 #endif
1045 	oss_period_size *= oss_frame_size;
1046 
1047 	oss_buffer_size = oss_period_size * runtime->oss.periods;
1048 	if (oss_buffer_size < 0) {
1049 		err = -EINVAL;
1050 		goto failure;
1051 	}
1052 
1053 	runtime->oss.period_bytes = oss_period_size;
1054 	runtime->oss.buffer_bytes = oss_buffer_size;
1055 
1056 	pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1057 		 runtime->oss.period_bytes,
1058 		 runtime->oss.buffer_bytes);
1059 	pdprintf("slave: period_size = %i, buffer_size = %i\n",
1060 		 params_period_size(sparams),
1061 		 params_buffer_size(sparams));
1062 
1063 	runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1064 	runtime->oss.channels = params_channels(params);
1065 	runtime->oss.rate = params_rate(params);
1066 
1067 	kvfree(runtime->oss.buffer);
1068 	runtime->oss.buffer = kvzalloc(runtime->oss.period_bytes, GFP_KERNEL);
1069 	if (!runtime->oss.buffer) {
1070 		err = -ENOMEM;
1071 		goto failure;
1072 	}
1073 
1074 	runtime->oss.params = 0;
1075 	runtime->oss.prepare = 1;
1076 	runtime->oss.buffer_used = 0;
1077 	if (runtime->dma_area)
1078 		snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1079 
1080 	runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1081 
1082 	err = 0;
1083 failure:
1084 	kfree(sw_params);
1085 	kfree(params);
1086 	kfree(sparams);
1087 	return err;
1088 }
1089 
1090 /* this one takes the lock by itself */
snd_pcm_oss_change_params(struct snd_pcm_substream * substream,bool trylock)1091 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
1092 				     bool trylock)
1093 {
1094 	struct snd_pcm_runtime *runtime = substream->runtime;
1095 	int err;
1096 
1097 	if (trylock) {
1098 		if (!(mutex_trylock(&runtime->oss.params_lock)))
1099 			return -EAGAIN;
1100 	} else if (mutex_lock_interruptible(&runtime->oss.params_lock))
1101 		return -ERESTARTSYS;
1102 
1103 	err = snd_pcm_oss_change_params_locked(substream);
1104 	mutex_unlock(&runtime->oss.params_lock);
1105 	return err;
1106 }
1107 
snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file * pcm_oss_file,struct snd_pcm_substream ** r_substream)1108 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1109 {
1110 	int idx, err;
1111 	struct snd_pcm_substream *asubstream = NULL, *substream;
1112 
1113 	for (idx = 0; idx < 2; idx++) {
1114 		substream = pcm_oss_file->streams[idx];
1115 		if (substream == NULL)
1116 			continue;
1117 		if (asubstream == NULL)
1118 			asubstream = substream;
1119 		if (substream->runtime->oss.params) {
1120 			err = snd_pcm_oss_change_params(substream, false);
1121 			if (err < 0)
1122 				return err;
1123 		}
1124 	}
1125 	if (!asubstream)
1126 		return -EIO;
1127 	if (r_substream)
1128 		*r_substream = asubstream;
1129 	return 0;
1130 }
1131 
1132 /* call with params_lock held */
1133 /* NOTE: this always call PREPARE unconditionally no matter whether
1134  * runtime->oss.prepare is set or not
1135  */
snd_pcm_oss_prepare(struct snd_pcm_substream * substream)1136 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1137 {
1138 	int err;
1139 	struct snd_pcm_runtime *runtime = substream->runtime;
1140 
1141 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1142 	if (err < 0) {
1143 		pcm_dbg(substream->pcm,
1144 			"snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1145 		return err;
1146 	}
1147 	runtime->oss.prepare = 0;
1148 	runtime->oss.prev_hw_ptr_period = 0;
1149 	runtime->oss.period_ptr = 0;
1150 	runtime->oss.buffer_used = 0;
1151 
1152 	return 0;
1153 }
1154 
snd_pcm_oss_make_ready(struct snd_pcm_substream * substream)1155 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1156 {
1157 	struct snd_pcm_runtime *runtime;
1158 	int err;
1159 
1160 	runtime = substream->runtime;
1161 	if (runtime->oss.params) {
1162 		err = snd_pcm_oss_change_params(substream, false);
1163 		if (err < 0)
1164 			return err;
1165 	}
1166 	if (runtime->oss.prepare) {
1167 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
1168 			return -ERESTARTSYS;
1169 		err = snd_pcm_oss_prepare(substream);
1170 		mutex_unlock(&runtime->oss.params_lock);
1171 		if (err < 0)
1172 			return err;
1173 	}
1174 	return 0;
1175 }
1176 
1177 /* call with params_lock held */
snd_pcm_oss_make_ready_locked(struct snd_pcm_substream * substream)1178 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1179 {
1180 	struct snd_pcm_runtime *runtime;
1181 	int err;
1182 
1183 	runtime = substream->runtime;
1184 	if (runtime->oss.params) {
1185 		err = snd_pcm_oss_change_params_locked(substream);
1186 		if (err < 0)
1187 			return err;
1188 	}
1189 	if (runtime->oss.prepare) {
1190 		err = snd_pcm_oss_prepare(substream);
1191 		if (err < 0)
1192 			return err;
1193 	}
1194 	return 0;
1195 }
1196 
snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream * substream,snd_pcm_sframes_t * delay)1197 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1198 {
1199 	struct snd_pcm_runtime *runtime;
1200 	snd_pcm_uframes_t frames;
1201 	int err = 0;
1202 
1203 	while (1) {
1204 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1205 		if (err < 0)
1206 			break;
1207 		runtime = substream->runtime;
1208 		if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1209 			break;
1210 		/* in case of overrun, skip whole periods like OSS/Linux driver does */
1211 		/* until avail(delay) <= buffer_size */
1212 		frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1213 		frames /= runtime->period_size;
1214 		frames *= runtime->period_size;
1215 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1216 		if (err < 0)
1217 			break;
1218 	}
1219 	return err;
1220 }
1221 
snd_pcm_oss_write3(struct snd_pcm_substream * substream,const char * ptr,snd_pcm_uframes_t frames,int in_kernel)1222 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1223 {
1224 	struct snd_pcm_runtime *runtime = substream->runtime;
1225 	int ret;
1226 	while (1) {
1227 		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1228 		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1229 #ifdef OSS_DEBUG
1230 			pcm_dbg(substream->pcm,
1231 				"pcm_oss: write: recovering from %s\n",
1232 				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1233 				"XRUN" : "SUSPEND");
1234 #endif
1235 			ret = snd_pcm_oss_prepare(substream);
1236 			if (ret < 0)
1237 				break;
1238 		}
1239 		mutex_unlock(&runtime->oss.params_lock);
1240 		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1241 					 frames, in_kernel);
1242 		mutex_lock(&runtime->oss.params_lock);
1243 		if (ret != -EPIPE && ret != -ESTRPIPE)
1244 			break;
1245 		/* test, if we can't store new data, because the stream */
1246 		/* has not been started */
1247 		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1248 			return -EAGAIN;
1249 	}
1250 	return ret;
1251 }
1252 
snd_pcm_oss_read3(struct snd_pcm_substream * substream,char * ptr,snd_pcm_uframes_t frames,int in_kernel)1253 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1254 {
1255 	struct snd_pcm_runtime *runtime = substream->runtime;
1256 	snd_pcm_sframes_t delay;
1257 	int ret;
1258 	while (1) {
1259 		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1260 		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1261 #ifdef OSS_DEBUG
1262 			pcm_dbg(substream->pcm,
1263 				"pcm_oss: read: recovering from %s\n",
1264 				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1265 				"XRUN" : "SUSPEND");
1266 #endif
1267 			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1268 			if (ret < 0)
1269 				break;
1270 		} else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1271 			ret = snd_pcm_oss_prepare(substream);
1272 			if (ret < 0)
1273 				break;
1274 		}
1275 		ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1276 		if (ret < 0)
1277 			break;
1278 		mutex_unlock(&runtime->oss.params_lock);
1279 		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1280 					 frames, in_kernel);
1281 		mutex_lock(&runtime->oss.params_lock);
1282 		if (ret == -EPIPE) {
1283 			if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1284 				ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1285 				if (ret < 0)
1286 					break;
1287 			}
1288 			continue;
1289 		}
1290 		if (ret != -ESTRPIPE)
1291 			break;
1292 	}
1293 	return ret;
1294 }
1295 
1296 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
snd_pcm_oss_writev3(struct snd_pcm_substream * substream,void ** bufs,snd_pcm_uframes_t frames)1297 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1298 {
1299 	struct snd_pcm_runtime *runtime = substream->runtime;
1300 	int ret;
1301 	while (1) {
1302 		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1303 		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1304 #ifdef OSS_DEBUG
1305 			pcm_dbg(substream->pcm,
1306 				"pcm_oss: writev: recovering from %s\n",
1307 				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1308 				"XRUN" : "SUSPEND");
1309 #endif
1310 			ret = snd_pcm_oss_prepare(substream);
1311 			if (ret < 0)
1312 				break;
1313 		}
1314 		ret = snd_pcm_kernel_writev(substream, bufs, frames);
1315 		if (ret != -EPIPE && ret != -ESTRPIPE)
1316 			break;
1317 
1318 		/* test, if we can't store new data, because the stream */
1319 		/* has not been started */
1320 		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1321 			return -EAGAIN;
1322 	}
1323 	return ret;
1324 }
1325 
snd_pcm_oss_readv3(struct snd_pcm_substream * substream,void ** bufs,snd_pcm_uframes_t frames)1326 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1327 {
1328 	struct snd_pcm_runtime *runtime = substream->runtime;
1329 	int ret;
1330 	while (1) {
1331 		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1332 		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1333 #ifdef OSS_DEBUG
1334 			pcm_dbg(substream->pcm,
1335 				"pcm_oss: readv: recovering from %s\n",
1336 				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1337 				"XRUN" : "SUSPEND");
1338 #endif
1339 			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1340 			if (ret < 0)
1341 				break;
1342 		} else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1343 			ret = snd_pcm_oss_prepare(substream);
1344 			if (ret < 0)
1345 				break;
1346 		}
1347 		ret = snd_pcm_kernel_readv(substream, bufs, frames);
1348 		if (ret != -EPIPE && ret != -ESTRPIPE)
1349 			break;
1350 	}
1351 	return ret;
1352 }
1353 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
1354 
snd_pcm_oss_write2(struct snd_pcm_substream * substream,const char * buf,size_t bytes,int in_kernel)1355 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1356 {
1357 	struct snd_pcm_runtime *runtime = substream->runtime;
1358 	snd_pcm_sframes_t frames, frames1;
1359 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1360 	if (runtime->oss.plugin_first) {
1361 		struct snd_pcm_plugin_channel *channels;
1362 		size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1363 		if (!in_kernel) {
1364 			if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1365 				return -EFAULT;
1366 			buf = runtime->oss.buffer;
1367 		}
1368 		frames = bytes / oss_frame_bytes;
1369 		frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1370 		if (frames1 < 0)
1371 			return frames1;
1372 		frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1373 		if (frames1 <= 0)
1374 			return frames1;
1375 		bytes = frames1 * oss_frame_bytes;
1376 	} else
1377 #endif
1378 	{
1379 		frames = bytes_to_frames(runtime, bytes);
1380 		frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1381 		if (frames1 <= 0)
1382 			return frames1;
1383 		bytes = frames_to_bytes(runtime, frames1);
1384 	}
1385 	return bytes;
1386 }
1387 
snd_pcm_oss_write1(struct snd_pcm_substream * substream,const char __user * buf,size_t bytes)1388 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1389 {
1390 	size_t xfer = 0;
1391 	ssize_t tmp = 0;
1392 	struct snd_pcm_runtime *runtime = substream->runtime;
1393 
1394 	if (atomic_read(&substream->mmap_count))
1395 		return -ENXIO;
1396 
1397 	atomic_inc(&runtime->oss.rw_ref);
1398 	while (bytes > 0) {
1399 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1400 			tmp = -ERESTARTSYS;
1401 			break;
1402 		}
1403 		tmp = snd_pcm_oss_make_ready_locked(substream);
1404 		if (tmp < 0)
1405 			goto err;
1406 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1407 			tmp = bytes;
1408 			if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1409 				tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1410 			if (tmp > 0) {
1411 				if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1412 					tmp = -EFAULT;
1413 					goto err;
1414 				}
1415 			}
1416 			runtime->oss.buffer_used += tmp;
1417 			buf += tmp;
1418 			bytes -= tmp;
1419 			xfer += tmp;
1420 			if (substream->oss.setup.partialfrag ||
1421 			    runtime->oss.buffer_used == runtime->oss.period_bytes) {
1422 				tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1423 							 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1424 				if (tmp <= 0)
1425 					goto err;
1426 				runtime->oss.bytes += tmp;
1427 				runtime->oss.period_ptr += tmp;
1428 				runtime->oss.period_ptr %= runtime->oss.period_bytes;
1429 				if (runtime->oss.period_ptr == 0 ||
1430 				    runtime->oss.period_ptr == runtime->oss.buffer_used)
1431 					runtime->oss.buffer_used = 0;
1432 				else if ((substream->f_flags & O_NONBLOCK) != 0) {
1433 					tmp = -EAGAIN;
1434 					goto err;
1435 				}
1436 			}
1437 		} else {
1438 			tmp = snd_pcm_oss_write2(substream,
1439 						 (const char __force *)buf,
1440 						 runtime->oss.period_bytes, 0);
1441 			if (tmp <= 0)
1442 				goto err;
1443 			runtime->oss.bytes += tmp;
1444 			buf += tmp;
1445 			bytes -= tmp;
1446 			xfer += tmp;
1447 			if ((substream->f_flags & O_NONBLOCK) != 0 &&
1448 			    tmp != runtime->oss.period_bytes)
1449 				tmp = -EAGAIN;
1450 		}
1451  err:
1452 		mutex_unlock(&runtime->oss.params_lock);
1453 		if (tmp < 0)
1454 			break;
1455 		if (signal_pending(current)) {
1456 			tmp = -ERESTARTSYS;
1457 			break;
1458 		}
1459 		tmp = 0;
1460 	}
1461 	atomic_dec(&runtime->oss.rw_ref);
1462 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1463 }
1464 
snd_pcm_oss_read2(struct snd_pcm_substream * substream,char * buf,size_t bytes,int in_kernel)1465 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1466 {
1467 	struct snd_pcm_runtime *runtime = substream->runtime;
1468 	snd_pcm_sframes_t frames, frames1;
1469 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1470 	char __user *final_dst = (char __force __user *)buf;
1471 	if (runtime->oss.plugin_first) {
1472 		struct snd_pcm_plugin_channel *channels;
1473 		size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1474 		if (!in_kernel)
1475 			buf = runtime->oss.buffer;
1476 		frames = bytes / oss_frame_bytes;
1477 		frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1478 		if (frames1 < 0)
1479 			return frames1;
1480 		frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1481 		if (frames1 <= 0)
1482 			return frames1;
1483 		bytes = frames1 * oss_frame_bytes;
1484 		if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1485 			return -EFAULT;
1486 	} else
1487 #endif
1488 	{
1489 		frames = bytes_to_frames(runtime, bytes);
1490 		frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1491 		if (frames1 <= 0)
1492 			return frames1;
1493 		bytes = frames_to_bytes(runtime, frames1);
1494 	}
1495 	return bytes;
1496 }
1497 
snd_pcm_oss_read1(struct snd_pcm_substream * substream,char __user * buf,size_t bytes)1498 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1499 {
1500 	size_t xfer = 0;
1501 	ssize_t tmp = 0;
1502 	struct snd_pcm_runtime *runtime = substream->runtime;
1503 
1504 	if (atomic_read(&substream->mmap_count))
1505 		return -ENXIO;
1506 
1507 	atomic_inc(&runtime->oss.rw_ref);
1508 	while (bytes > 0) {
1509 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1510 			tmp = -ERESTARTSYS;
1511 			break;
1512 		}
1513 		tmp = snd_pcm_oss_make_ready_locked(substream);
1514 		if (tmp < 0)
1515 			goto err;
1516 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1517 			if (runtime->oss.buffer_used == 0) {
1518 				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1519 				if (tmp <= 0)
1520 					goto err;
1521 				runtime->oss.bytes += tmp;
1522 				runtime->oss.period_ptr = tmp;
1523 				runtime->oss.buffer_used = tmp;
1524 			}
1525 			tmp = bytes;
1526 			if ((size_t) tmp > runtime->oss.buffer_used)
1527 				tmp = runtime->oss.buffer_used;
1528 			if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1529 				tmp = -EFAULT;
1530 				goto err;
1531 			}
1532 			buf += tmp;
1533 			bytes -= tmp;
1534 			xfer += tmp;
1535 			runtime->oss.buffer_used -= tmp;
1536 		} else {
1537 			tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1538 						runtime->oss.period_bytes, 0);
1539 			if (tmp <= 0)
1540 				goto err;
1541 			runtime->oss.bytes += tmp;
1542 			buf += tmp;
1543 			bytes -= tmp;
1544 			xfer += tmp;
1545 		}
1546  err:
1547 		mutex_unlock(&runtime->oss.params_lock);
1548 		if (tmp < 0)
1549 			break;
1550 		if (signal_pending(current)) {
1551 			tmp = -ERESTARTSYS;
1552 			break;
1553 		}
1554 		tmp = 0;
1555 	}
1556 	atomic_dec(&runtime->oss.rw_ref);
1557 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1558 }
1559 
snd_pcm_oss_reset(struct snd_pcm_oss_file * pcm_oss_file)1560 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1561 {
1562 	struct snd_pcm_substream *substream;
1563 	struct snd_pcm_runtime *runtime;
1564 	int i;
1565 
1566 	for (i = 0; i < 2; i++) {
1567 		substream = pcm_oss_file->streams[i];
1568 		if (!substream)
1569 			continue;
1570 		runtime = substream->runtime;
1571 		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1572 		mutex_lock(&runtime->oss.params_lock);
1573 		runtime->oss.prepare = 1;
1574 		runtime->oss.buffer_used = 0;
1575 		runtime->oss.prev_hw_ptr_period = 0;
1576 		runtime->oss.period_ptr = 0;
1577 		mutex_unlock(&runtime->oss.params_lock);
1578 	}
1579 	return 0;
1580 }
1581 
snd_pcm_oss_post(struct snd_pcm_oss_file * pcm_oss_file)1582 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1583 {
1584 	struct snd_pcm_substream *substream;
1585 	int err;
1586 
1587 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1588 	if (substream != NULL) {
1589 		if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1590 			return err;
1591 		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1592 	}
1593 	/* note: all errors from the start action are ignored */
1594 	/* OSS apps do not know, how to handle them */
1595 	return 0;
1596 }
1597 
snd_pcm_oss_sync1(struct snd_pcm_substream * substream,size_t size)1598 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1599 {
1600 	struct snd_pcm_runtime *runtime;
1601 	ssize_t result = 0;
1602 	snd_pcm_state_t state;
1603 	long res;
1604 	wait_queue_entry_t wait;
1605 
1606 	runtime = substream->runtime;
1607 	init_waitqueue_entry(&wait, current);
1608 	add_wait_queue(&runtime->sleep, &wait);
1609 #ifdef OSS_DEBUG
1610 	pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1611 #endif
1612 	while (1) {
1613 		result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1614 		if (result > 0) {
1615 			runtime->oss.buffer_used = 0;
1616 			result = 0;
1617 			break;
1618 		}
1619 		if (result != 0 && result != -EAGAIN)
1620 			break;
1621 		result = 0;
1622 		set_current_state(TASK_INTERRUPTIBLE);
1623 		snd_pcm_stream_lock_irq(substream);
1624 		state = runtime->status->state;
1625 		snd_pcm_stream_unlock_irq(substream);
1626 		if (state != SNDRV_PCM_STATE_RUNNING) {
1627 			set_current_state(TASK_RUNNING);
1628 			break;
1629 		}
1630 		res = schedule_timeout(10 * HZ);
1631 		if (signal_pending(current)) {
1632 			result = -ERESTARTSYS;
1633 			break;
1634 		}
1635 		if (res == 0) {
1636 			pcm_err(substream->pcm,
1637 				"OSS sync error - DMA timeout\n");
1638 			result = -EIO;
1639 			break;
1640 		}
1641 	}
1642 	remove_wait_queue(&runtime->sleep, &wait);
1643 	return result;
1644 }
1645 
snd_pcm_oss_sync(struct snd_pcm_oss_file * pcm_oss_file)1646 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1647 {
1648 	int err = 0;
1649 	unsigned int saved_f_flags;
1650 	struct snd_pcm_substream *substream;
1651 	struct snd_pcm_runtime *runtime;
1652 	snd_pcm_format_t format;
1653 	unsigned long width;
1654 	size_t size;
1655 
1656 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1657 	if (substream != NULL) {
1658 		runtime = substream->runtime;
1659 		if (atomic_read(&substream->mmap_count))
1660 			goto __direct;
1661 		atomic_inc(&runtime->oss.rw_ref);
1662 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1663 			atomic_dec(&runtime->oss.rw_ref);
1664 			return -ERESTARTSYS;
1665 		}
1666 		err = snd_pcm_oss_make_ready_locked(substream);
1667 		if (err < 0)
1668 			goto unlock;
1669 		format = snd_pcm_oss_format_from(runtime->oss.format);
1670 		width = snd_pcm_format_physical_width(format);
1671 		if (runtime->oss.buffer_used > 0) {
1672 #ifdef OSS_DEBUG
1673 			pcm_dbg(substream->pcm, "sync: buffer_used\n");
1674 #endif
1675 			size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1676 			snd_pcm_format_set_silence(format,
1677 						   runtime->oss.buffer + runtime->oss.buffer_used,
1678 						   size);
1679 			err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1680 			if (err < 0)
1681 				goto unlock;
1682 		} else if (runtime->oss.period_ptr > 0) {
1683 #ifdef OSS_DEBUG
1684 			pcm_dbg(substream->pcm, "sync: period_ptr\n");
1685 #endif
1686 			size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1687 			snd_pcm_format_set_silence(format,
1688 						   runtime->oss.buffer,
1689 						   size * 8 / width);
1690 			err = snd_pcm_oss_sync1(substream, size);
1691 			if (err < 0)
1692 				goto unlock;
1693 		}
1694 		/*
1695 		 * The ALSA's period might be a bit large than OSS one.
1696 		 * Fill the remain portion of ALSA period with zeros.
1697 		 */
1698 		size = runtime->control->appl_ptr % runtime->period_size;
1699 		if (size > 0) {
1700 			size = runtime->period_size - size;
1701 			if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1702 				snd_pcm_lib_write(substream, NULL, size);
1703 			else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1704 				snd_pcm_lib_writev(substream, NULL, size);
1705 		}
1706 unlock:
1707 		mutex_unlock(&runtime->oss.params_lock);
1708 		atomic_dec(&runtime->oss.rw_ref);
1709 		if (err < 0)
1710 			return err;
1711 		/*
1712 		 * finish sync: drain the buffer
1713 		 */
1714 	      __direct:
1715 		saved_f_flags = substream->f_flags;
1716 		substream->f_flags &= ~O_NONBLOCK;
1717 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1718 		substream->f_flags = saved_f_flags;
1719 		if (err < 0)
1720 			return err;
1721 		mutex_lock(&runtime->oss.params_lock);
1722 		runtime->oss.prepare = 1;
1723 		mutex_unlock(&runtime->oss.params_lock);
1724 	}
1725 
1726 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1727 	if (substream != NULL) {
1728 		if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1729 			return err;
1730 		runtime = substream->runtime;
1731 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1732 		if (err < 0)
1733 			return err;
1734 		mutex_lock(&runtime->oss.params_lock);
1735 		runtime->oss.buffer_used = 0;
1736 		runtime->oss.prepare = 1;
1737 		mutex_unlock(&runtime->oss.params_lock);
1738 	}
1739 	return 0;
1740 }
1741 
snd_pcm_oss_set_rate(struct snd_pcm_oss_file * pcm_oss_file,int rate)1742 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1743 {
1744 	int idx;
1745 
1746 	for (idx = 1; idx >= 0; --idx) {
1747 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1748 		struct snd_pcm_runtime *runtime;
1749 		int err;
1750 
1751 		if (substream == NULL)
1752 			continue;
1753 		runtime = substream->runtime;
1754 		if (rate < 1000)
1755 			rate = 1000;
1756 		else if (rate > 192000)
1757 			rate = 192000;
1758 		err = lock_params(runtime);
1759 		if (err < 0)
1760 			return err;
1761 		if (runtime->oss.rate != rate) {
1762 			runtime->oss.params = 1;
1763 			runtime->oss.rate = rate;
1764 		}
1765 		unlock_params(runtime);
1766 	}
1767 	return snd_pcm_oss_get_rate(pcm_oss_file);
1768 }
1769 
snd_pcm_oss_get_rate(struct snd_pcm_oss_file * pcm_oss_file)1770 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1771 {
1772 	struct snd_pcm_substream *substream;
1773 	int err;
1774 
1775 	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1776 		return err;
1777 	return substream->runtime->oss.rate;
1778 }
1779 
snd_pcm_oss_set_channels(struct snd_pcm_oss_file * pcm_oss_file,unsigned int channels)1780 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1781 {
1782 	int idx;
1783 	if (channels < 1)
1784 		channels = 1;
1785 	if (channels > 128)
1786 		return -EINVAL;
1787 	for (idx = 1; idx >= 0; --idx) {
1788 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1789 		struct snd_pcm_runtime *runtime;
1790 		int err;
1791 
1792 		if (substream == NULL)
1793 			continue;
1794 		runtime = substream->runtime;
1795 		err = lock_params(runtime);
1796 		if (err < 0)
1797 			return err;
1798 		if (runtime->oss.channels != channels) {
1799 			runtime->oss.params = 1;
1800 			runtime->oss.channels = channels;
1801 		}
1802 		unlock_params(runtime);
1803 	}
1804 	return snd_pcm_oss_get_channels(pcm_oss_file);
1805 }
1806 
snd_pcm_oss_get_channels(struct snd_pcm_oss_file * pcm_oss_file)1807 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1808 {
1809 	struct snd_pcm_substream *substream;
1810 	int err;
1811 
1812 	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1813 		return err;
1814 	return substream->runtime->oss.channels;
1815 }
1816 
snd_pcm_oss_get_block_size(struct snd_pcm_oss_file * pcm_oss_file)1817 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1818 {
1819 	struct snd_pcm_substream *substream;
1820 	int err;
1821 
1822 	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1823 		return err;
1824 	return substream->runtime->oss.period_bytes;
1825 }
1826 
snd_pcm_oss_get_formats(struct snd_pcm_oss_file * pcm_oss_file)1827 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1828 {
1829 	struct snd_pcm_substream *substream;
1830 	int err;
1831 	int direct;
1832 	struct snd_pcm_hw_params *params;
1833 	unsigned int formats = 0;
1834 	const struct snd_mask *format_mask;
1835 	int fmt;
1836 
1837 	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1838 		return err;
1839 	if (atomic_read(&substream->mmap_count))
1840 		direct = 1;
1841 	else
1842 		direct = substream->oss.setup.direct;
1843 	if (!direct)
1844 		return AFMT_MU_LAW | AFMT_U8 |
1845 		       AFMT_S16_LE | AFMT_S16_BE |
1846 		       AFMT_S8 | AFMT_U16_LE |
1847 		       AFMT_U16_BE |
1848 			AFMT_S32_LE | AFMT_S32_BE |
1849 			AFMT_S24_LE | AFMT_S24_BE |
1850 			AFMT_S24_PACKED;
1851 	params = kmalloc(sizeof(*params), GFP_KERNEL);
1852 	if (!params)
1853 		return -ENOMEM;
1854 	_snd_pcm_hw_params_any(params);
1855 	err = snd_pcm_hw_refine(substream, params);
1856 	if (err < 0)
1857 		goto error;
1858 	format_mask = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
1859 	for (fmt = 0; fmt < 32; ++fmt) {
1860 		if (snd_mask_test(format_mask, fmt)) {
1861 			int f = snd_pcm_oss_format_to((__force snd_pcm_format_t)fmt);
1862 			if (f >= 0)
1863 				formats |= f;
1864 		}
1865 	}
1866 
1867  error:
1868 	kfree(params);
1869 	return err < 0 ? err : formats;
1870 }
1871 
snd_pcm_oss_set_format(struct snd_pcm_oss_file * pcm_oss_file,int format)1872 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1873 {
1874 	int formats, idx;
1875 	int err;
1876 
1877 	if (format != AFMT_QUERY) {
1878 		formats = snd_pcm_oss_get_formats(pcm_oss_file);
1879 		if (formats < 0)
1880 			return formats;
1881 		if (!(formats & format))
1882 			format = AFMT_U8;
1883 		for (idx = 1; idx >= 0; --idx) {
1884 			struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1885 			struct snd_pcm_runtime *runtime;
1886 			if (substream == NULL)
1887 				continue;
1888 			runtime = substream->runtime;
1889 			err = lock_params(runtime);
1890 			if (err < 0)
1891 				return err;
1892 			if (runtime->oss.format != format) {
1893 				runtime->oss.params = 1;
1894 				runtime->oss.format = format;
1895 			}
1896 			unlock_params(runtime);
1897 		}
1898 	}
1899 	return snd_pcm_oss_get_format(pcm_oss_file);
1900 }
1901 
snd_pcm_oss_get_format(struct snd_pcm_oss_file * pcm_oss_file)1902 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1903 {
1904 	struct snd_pcm_substream *substream;
1905 	int err;
1906 
1907 	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1908 		return err;
1909 	return substream->runtime->oss.format;
1910 }
1911 
snd_pcm_oss_set_subdivide1(struct snd_pcm_substream * substream,int subdivide)1912 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1913 {
1914 	struct snd_pcm_runtime *runtime;
1915 
1916 	runtime = substream->runtime;
1917 	if (subdivide == 0) {
1918 		subdivide = runtime->oss.subdivision;
1919 		if (subdivide == 0)
1920 			subdivide = 1;
1921 		return subdivide;
1922 	}
1923 	if (runtime->oss.subdivision || runtime->oss.fragshift)
1924 		return -EINVAL;
1925 	if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1926 	    subdivide != 8 && subdivide != 16)
1927 		return -EINVAL;
1928 	runtime->oss.subdivision = subdivide;
1929 	runtime->oss.params = 1;
1930 	return subdivide;
1931 }
1932 
snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file * pcm_oss_file,int subdivide)1933 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1934 {
1935 	int err = -EINVAL, idx;
1936 
1937 	for (idx = 1; idx >= 0; --idx) {
1938 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1939 		struct snd_pcm_runtime *runtime;
1940 
1941 		if (substream == NULL)
1942 			continue;
1943 		runtime = substream->runtime;
1944 		err = lock_params(runtime);
1945 		if (err < 0)
1946 			return err;
1947 		err = snd_pcm_oss_set_subdivide1(substream, subdivide);
1948 		unlock_params(runtime);
1949 		if (err < 0)
1950 			return err;
1951 	}
1952 	return err;
1953 }
1954 
snd_pcm_oss_set_fragment1(struct snd_pcm_substream * substream,unsigned int val)1955 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1956 {
1957 	struct snd_pcm_runtime *runtime;
1958 	int fragshift;
1959 
1960 	runtime = substream->runtime;
1961 	if (runtime->oss.subdivision || runtime->oss.fragshift)
1962 		return -EINVAL;
1963 	fragshift = val & 0xffff;
1964 	if (fragshift >= 25) /* should be large enough */
1965 		return -EINVAL;
1966 	runtime->oss.fragshift = fragshift;
1967 	runtime->oss.maxfrags = (val >> 16) & 0xffff;
1968 	if (runtime->oss.fragshift < 4)		/* < 16 */
1969 		runtime->oss.fragshift = 4;
1970 	if (runtime->oss.maxfrags < 2)
1971 		runtime->oss.maxfrags = 2;
1972 	runtime->oss.params = 1;
1973 	return 0;
1974 }
1975 
snd_pcm_oss_set_fragment(struct snd_pcm_oss_file * pcm_oss_file,unsigned int val)1976 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1977 {
1978 	int err = -EINVAL, idx;
1979 
1980 	for (idx = 1; idx >= 0; --idx) {
1981 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1982 		struct snd_pcm_runtime *runtime;
1983 
1984 		if (substream == NULL)
1985 			continue;
1986 		runtime = substream->runtime;
1987 		err = lock_params(runtime);
1988 		if (err < 0)
1989 			return err;
1990 		err = snd_pcm_oss_set_fragment1(substream, val);
1991 		unlock_params(runtime);
1992 		if (err < 0)
1993 			return err;
1994 	}
1995 	return err;
1996 }
1997 
snd_pcm_oss_nonblock(struct file * file)1998 static int snd_pcm_oss_nonblock(struct file * file)
1999 {
2000 	spin_lock(&file->f_lock);
2001 	file->f_flags |= O_NONBLOCK;
2002 	spin_unlock(&file->f_lock);
2003 	return 0;
2004 }
2005 
snd_pcm_oss_get_caps1(struct snd_pcm_substream * substream,int res)2006 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2007 {
2008 
2009 	if (substream == NULL) {
2010 		res &= ~DSP_CAP_DUPLEX;
2011 		return res;
2012 	}
2013 #ifdef DSP_CAP_MULTI
2014 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2015 		if (substream->pstr->substream_count > 1)
2016 			res |= DSP_CAP_MULTI;
2017 #endif
2018 	/* DSP_CAP_REALTIME is set all times: */
2019 	/* all ALSA drivers can return actual pointer in ring buffer */
2020 #if defined(DSP_CAP_REALTIME) && 0
2021 	{
2022 		struct snd_pcm_runtime *runtime = substream->runtime;
2023 		if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2024 			res &= ~DSP_CAP_REALTIME;
2025 	}
2026 #endif
2027 	return res;
2028 }
2029 
snd_pcm_oss_get_caps(struct snd_pcm_oss_file * pcm_oss_file)2030 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2031 {
2032 	int result, idx;
2033 
2034 	result = DSP_CAP_TRIGGER | DSP_CAP_MMAP	| DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2035 	for (idx = 0; idx < 2; idx++) {
2036 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2037 		result = snd_pcm_oss_get_caps1(substream, result);
2038 	}
2039 	result |= 0x0001;	/* revision - same as SB AWE 64 */
2040 	return result;
2041 }
2042 
snd_pcm_oss_simulate_fill(struct snd_pcm_substream * substream,snd_pcm_uframes_t hw_ptr)2043 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2044 				      snd_pcm_uframes_t hw_ptr)
2045 {
2046 	struct snd_pcm_runtime *runtime = substream->runtime;
2047 	snd_pcm_uframes_t appl_ptr;
2048 	appl_ptr = hw_ptr + runtime->buffer_size;
2049 	appl_ptr %= runtime->boundary;
2050 	runtime->control->appl_ptr = appl_ptr;
2051 }
2052 
snd_pcm_oss_set_trigger(struct snd_pcm_oss_file * pcm_oss_file,int trigger)2053 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2054 {
2055 	struct snd_pcm_runtime *runtime;
2056 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2057 	int err, cmd;
2058 
2059 #ifdef OSS_DEBUG
2060 	pr_debug("pcm_oss: trigger = 0x%x\n", trigger);
2061 #endif
2062 
2063 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2064 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2065 
2066 	if (psubstream) {
2067 		if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
2068 			return err;
2069 	}
2070 	if (csubstream) {
2071 		if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
2072 			return err;
2073 	}
2074       	if (psubstream) {
2075       		runtime = psubstream->runtime;
2076 		cmd = 0;
2077 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2078 			return -ERESTARTSYS;
2079 		if (trigger & PCM_ENABLE_OUTPUT) {
2080 			if (runtime->oss.trigger)
2081 				goto _skip1;
2082 			if (atomic_read(&psubstream->mmap_count))
2083 				snd_pcm_oss_simulate_fill(psubstream,
2084 						get_hw_ptr_period(runtime));
2085 			runtime->oss.trigger = 1;
2086 			runtime->start_threshold = 1;
2087 			cmd = SNDRV_PCM_IOCTL_START;
2088 		} else {
2089 			if (!runtime->oss.trigger)
2090 				goto _skip1;
2091 			runtime->oss.trigger = 0;
2092 			runtime->start_threshold = runtime->boundary;
2093 			cmd = SNDRV_PCM_IOCTL_DROP;
2094 			runtime->oss.prepare = 1;
2095 		}
2096  _skip1:
2097 		mutex_unlock(&runtime->oss.params_lock);
2098 		if (cmd) {
2099 			err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2100 			if (err < 0)
2101 				return err;
2102 		}
2103 	}
2104 	if (csubstream) {
2105       		runtime = csubstream->runtime;
2106 		cmd = 0;
2107 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2108 			return -ERESTARTSYS;
2109 		if (trigger & PCM_ENABLE_INPUT) {
2110 			if (runtime->oss.trigger)
2111 				goto _skip2;
2112 			runtime->oss.trigger = 1;
2113 			runtime->start_threshold = 1;
2114 			cmd = SNDRV_PCM_IOCTL_START;
2115 		} else {
2116 			if (!runtime->oss.trigger)
2117 				goto _skip2;
2118 			runtime->oss.trigger = 0;
2119 			runtime->start_threshold = runtime->boundary;
2120 			cmd = SNDRV_PCM_IOCTL_DROP;
2121 			runtime->oss.prepare = 1;
2122 		}
2123  _skip2:
2124 		mutex_unlock(&runtime->oss.params_lock);
2125 		if (cmd) {
2126 			err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2127 			if (err < 0)
2128 				return err;
2129 		}
2130 	}
2131 	return 0;
2132 }
2133 
snd_pcm_oss_get_trigger(struct snd_pcm_oss_file * pcm_oss_file)2134 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2135 {
2136 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2137 	int result = 0;
2138 
2139 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2140 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2141 	if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2142 		result |= PCM_ENABLE_OUTPUT;
2143 	if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2144 		result |= PCM_ENABLE_INPUT;
2145 	return result;
2146 }
2147 
snd_pcm_oss_get_odelay(struct snd_pcm_oss_file * pcm_oss_file)2148 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2149 {
2150 	struct snd_pcm_substream *substream;
2151 	struct snd_pcm_runtime *runtime;
2152 	snd_pcm_sframes_t delay;
2153 	int err;
2154 
2155 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2156 	if (substream == NULL)
2157 		return -EINVAL;
2158 	if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2159 		return err;
2160 	runtime = substream->runtime;
2161 	if (runtime->oss.params || runtime->oss.prepare)
2162 		return 0;
2163 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2164 	if (err == -EPIPE)
2165 		delay = 0;	/* hack for broken OSS applications */
2166 	else if (err < 0)
2167 		return err;
2168 	return snd_pcm_oss_bytes(substream, delay);
2169 }
2170 
snd_pcm_oss_get_ptr(struct snd_pcm_oss_file * pcm_oss_file,int stream,struct count_info __user * _info)2171 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2172 {
2173 	struct snd_pcm_substream *substream;
2174 	struct snd_pcm_runtime *runtime;
2175 	snd_pcm_sframes_t delay;
2176 	int fixup;
2177 	struct count_info info;
2178 	int err;
2179 
2180 	if (_info == NULL)
2181 		return -EFAULT;
2182 	substream = pcm_oss_file->streams[stream];
2183 	if (substream == NULL)
2184 		return -EINVAL;
2185 	if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2186 		return err;
2187 	runtime = substream->runtime;
2188 	if (runtime->oss.params || runtime->oss.prepare) {
2189 		memset(&info, 0, sizeof(info));
2190 		if (copy_to_user(_info, &info, sizeof(info)))
2191 			return -EFAULT;
2192 		return 0;
2193 	}
2194 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2195 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2196 		if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2197 			err = 0;
2198 			delay = 0;
2199 			fixup = 0;
2200 		} else {
2201 			fixup = runtime->oss.buffer_used;
2202 		}
2203 	} else {
2204 		err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2205 		fixup = -runtime->oss.buffer_used;
2206 	}
2207 	if (err < 0)
2208 		return err;
2209 	info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2210 	if (atomic_read(&substream->mmap_count)) {
2211 		snd_pcm_sframes_t n;
2212 		delay = get_hw_ptr_period(runtime);
2213 		n = delay - runtime->oss.prev_hw_ptr_period;
2214 		if (n < 0)
2215 			n += runtime->boundary;
2216 		info.blocks = n / runtime->period_size;
2217 		runtime->oss.prev_hw_ptr_period = delay;
2218 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2219 			snd_pcm_oss_simulate_fill(substream, delay);
2220 		info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2221 	} else {
2222 		delay = snd_pcm_oss_bytes(substream, delay);
2223 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2224 			if (substream->oss.setup.buggyptr)
2225 				info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2226 			else
2227 				info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2228 			info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2229 		} else {
2230 			delay += fixup;
2231 			info.blocks = delay / runtime->oss.period_bytes;
2232 			info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2233 		}
2234 	}
2235 	if (copy_to_user(_info, &info, sizeof(info)))
2236 		return -EFAULT;
2237 	return 0;
2238 }
2239 
snd_pcm_oss_get_space(struct snd_pcm_oss_file * pcm_oss_file,int stream,struct audio_buf_info __user * _info)2240 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2241 {
2242 	struct snd_pcm_substream *substream;
2243 	struct snd_pcm_runtime *runtime;
2244 	snd_pcm_sframes_t avail;
2245 	int fixup;
2246 	struct audio_buf_info info;
2247 	int err;
2248 
2249 	if (_info == NULL)
2250 		return -EFAULT;
2251 	substream = pcm_oss_file->streams[stream];
2252 	if (substream == NULL)
2253 		return -EINVAL;
2254 	runtime = substream->runtime;
2255 
2256 	if (runtime->oss.params &&
2257 	    (err = snd_pcm_oss_change_params(substream, false)) < 0)
2258 		return err;
2259 
2260 	info.fragsize = runtime->oss.period_bytes;
2261 	info.fragstotal = runtime->periods;
2262 	if (runtime->oss.prepare) {
2263 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2264 			info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2265 			info.fragments = runtime->oss.periods;
2266 		} else {
2267 			info.bytes = 0;
2268 			info.fragments = 0;
2269 		}
2270 	} else {
2271 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2272 			err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2273 			if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2274 				avail = runtime->buffer_size;
2275 				err = 0;
2276 				fixup = 0;
2277 			} else {
2278 				avail = runtime->buffer_size - avail;
2279 				fixup = -runtime->oss.buffer_used;
2280 			}
2281 		} else {
2282 			err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2283 			fixup = runtime->oss.buffer_used;
2284 		}
2285 		if (err < 0)
2286 			return err;
2287 		info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2288 		info.fragments = info.bytes / runtime->oss.period_bytes;
2289 	}
2290 
2291 #ifdef OSS_DEBUG
2292 	pcm_dbg(substream->pcm,
2293 		"pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2294 		info.bytes, info.fragments, info.fragstotal, info.fragsize);
2295 #endif
2296 	if (copy_to_user(_info, &info, sizeof(info)))
2297 		return -EFAULT;
2298 	return 0;
2299 }
2300 
snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file * pcm_oss_file,int stream,struct buffmem_desc __user * _info)2301 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2302 {
2303 	// it won't be probably implemented
2304 	// pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2305 	return -EINVAL;
2306 }
2307 
strip_task_path(const char * path)2308 static const char *strip_task_path(const char *path)
2309 {
2310 	const char *ptr, *ptrl = NULL;
2311 	for (ptr = path; *ptr; ptr++) {
2312 		if (*ptr == '/')
2313 			ptrl = ptr + 1;
2314 	}
2315 	return ptrl;
2316 }
2317 
snd_pcm_oss_look_for_setup(struct snd_pcm * pcm,int stream,const char * task_name,struct snd_pcm_oss_setup * rsetup)2318 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2319 				      const char *task_name,
2320 				      struct snd_pcm_oss_setup *rsetup)
2321 {
2322 	struct snd_pcm_oss_setup *setup;
2323 
2324 	mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2325 	do {
2326 		for (setup = pcm->streams[stream].oss.setup_list; setup;
2327 		     setup = setup->next) {
2328 			if (!strcmp(setup->task_name, task_name))
2329 				goto out;
2330 		}
2331 	} while ((task_name = strip_task_path(task_name)) != NULL);
2332  out:
2333 	if (setup)
2334 		*rsetup = *setup;
2335 	mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2336 }
2337 
snd_pcm_oss_release_substream(struct snd_pcm_substream * substream)2338 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2339 {
2340 	struct snd_pcm_runtime *runtime;
2341 	runtime = substream->runtime;
2342 	kvfree(runtime->oss.buffer);
2343 	runtime->oss.buffer = NULL;
2344 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2345 	snd_pcm_oss_plugin_clear(substream);
2346 #endif
2347 	substream->oss.oss = 0;
2348 }
2349 
snd_pcm_oss_init_substream(struct snd_pcm_substream * substream,struct snd_pcm_oss_setup * setup,int minor)2350 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2351 				       struct snd_pcm_oss_setup *setup,
2352 				       int minor)
2353 {
2354 	struct snd_pcm_runtime *runtime;
2355 
2356 	substream->oss.oss = 1;
2357 	substream->oss.setup = *setup;
2358 	if (setup->nonblock)
2359 		substream->f_flags |= O_NONBLOCK;
2360 	else if (setup->block)
2361 		substream->f_flags &= ~O_NONBLOCK;
2362 	runtime = substream->runtime;
2363 	runtime->oss.params = 1;
2364 	runtime->oss.trigger = 1;
2365 	runtime->oss.rate = 8000;
2366 	mutex_init(&runtime->oss.params_lock);
2367 	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2368 	case SNDRV_MINOR_OSS_PCM_8:
2369 		runtime->oss.format = AFMT_U8;
2370 		break;
2371 	case SNDRV_MINOR_OSS_PCM_16:
2372 		runtime->oss.format = AFMT_S16_LE;
2373 		break;
2374 	default:
2375 		runtime->oss.format = AFMT_MU_LAW;
2376 	}
2377 	runtime->oss.channels = 1;
2378 	runtime->oss.fragshift = 0;
2379 	runtime->oss.maxfrags = 0;
2380 	runtime->oss.subdivision = 0;
2381 	substream->pcm_release = snd_pcm_oss_release_substream;
2382 	atomic_set(&runtime->oss.rw_ref, 0);
2383 }
2384 
snd_pcm_oss_release_file(struct snd_pcm_oss_file * pcm_oss_file)2385 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2386 {
2387 	int cidx;
2388 	if (!pcm_oss_file)
2389 		return 0;
2390 	for (cidx = 0; cidx < 2; ++cidx) {
2391 		struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2392 		if (substream)
2393 			snd_pcm_release_substream(substream);
2394 	}
2395 	kfree(pcm_oss_file);
2396 	return 0;
2397 }
2398 
snd_pcm_oss_open_file(struct file * file,struct snd_pcm * pcm,struct snd_pcm_oss_file ** rpcm_oss_file,int minor,struct snd_pcm_oss_setup * setup)2399 static int snd_pcm_oss_open_file(struct file *file,
2400 				 struct snd_pcm *pcm,
2401 				 struct snd_pcm_oss_file **rpcm_oss_file,
2402 				 int minor,
2403 				 struct snd_pcm_oss_setup *setup)
2404 {
2405 	int idx, err;
2406 	struct snd_pcm_oss_file *pcm_oss_file;
2407 	struct snd_pcm_substream *substream;
2408 	fmode_t f_mode = file->f_mode;
2409 
2410 	if (rpcm_oss_file)
2411 		*rpcm_oss_file = NULL;
2412 
2413 	pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2414 	if (pcm_oss_file == NULL)
2415 		return -ENOMEM;
2416 
2417 	if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2418 	    (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2419 		f_mode = FMODE_WRITE;
2420 
2421 	file->f_flags &= ~O_APPEND;
2422 	for (idx = 0; idx < 2; idx++) {
2423 		if (setup[idx].disable)
2424 			continue;
2425 		if (! pcm->streams[idx].substream_count)
2426 			continue; /* no matching substream */
2427 		if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2428 			if (! (f_mode & FMODE_WRITE))
2429 				continue;
2430 		} else {
2431 			if (! (f_mode & FMODE_READ))
2432 				continue;
2433 		}
2434 		err = snd_pcm_open_substream(pcm, idx, file, &substream);
2435 		if (err < 0) {
2436 			snd_pcm_oss_release_file(pcm_oss_file);
2437 			return err;
2438 		}
2439 
2440 		pcm_oss_file->streams[idx] = substream;
2441 		snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2442 	}
2443 
2444 	if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2445 		snd_pcm_oss_release_file(pcm_oss_file);
2446 		return -EINVAL;
2447 	}
2448 
2449 	file->private_data = pcm_oss_file;
2450 	if (rpcm_oss_file)
2451 		*rpcm_oss_file = pcm_oss_file;
2452 	return 0;
2453 }
2454 
2455 
snd_task_name(struct task_struct * task,char * name,size_t size)2456 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2457 {
2458 	unsigned int idx;
2459 
2460 	if (snd_BUG_ON(!task || !name || size < 2))
2461 		return -EINVAL;
2462 	for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2463 		name[idx] = task->comm[idx];
2464 	name[idx] = '\0';
2465 	return 0;
2466 }
2467 
snd_pcm_oss_open(struct inode * inode,struct file * file)2468 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2469 {
2470 	int err;
2471 	char task_name[32];
2472 	struct snd_pcm *pcm;
2473 	struct snd_pcm_oss_file *pcm_oss_file;
2474 	struct snd_pcm_oss_setup setup[2];
2475 	int nonblock;
2476 	wait_queue_entry_t wait;
2477 
2478 	err = nonseekable_open(inode, file);
2479 	if (err < 0)
2480 		return err;
2481 
2482 	pcm = snd_lookup_oss_minor_data(iminor(inode),
2483 					SNDRV_OSS_DEVICE_TYPE_PCM);
2484 	if (pcm == NULL) {
2485 		err = -ENODEV;
2486 		goto __error1;
2487 	}
2488 	err = snd_card_file_add(pcm->card, file);
2489 	if (err < 0)
2490 		goto __error1;
2491 	if (!try_module_get(pcm->card->module)) {
2492 		err = -EFAULT;
2493 		goto __error2;
2494 	}
2495 	if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2496 		err = -EFAULT;
2497 		goto __error;
2498 	}
2499 	memset(setup, 0, sizeof(setup));
2500 	if (file->f_mode & FMODE_WRITE)
2501 		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2502 					   task_name, &setup[0]);
2503 	if (file->f_mode & FMODE_READ)
2504 		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2505 					   task_name, &setup[1]);
2506 
2507 	nonblock = !!(file->f_flags & O_NONBLOCK);
2508 	if (!nonblock)
2509 		nonblock = nonblock_open;
2510 
2511 	init_waitqueue_entry(&wait, current);
2512 	add_wait_queue(&pcm->open_wait, &wait);
2513 	mutex_lock(&pcm->open_mutex);
2514 	while (1) {
2515 		err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2516 					    iminor(inode), setup);
2517 		if (err >= 0)
2518 			break;
2519 		if (err == -EAGAIN) {
2520 			if (nonblock) {
2521 				err = -EBUSY;
2522 				break;
2523 			}
2524 		} else
2525 			break;
2526 		set_current_state(TASK_INTERRUPTIBLE);
2527 		mutex_unlock(&pcm->open_mutex);
2528 		schedule();
2529 		mutex_lock(&pcm->open_mutex);
2530 		if (pcm->card->shutdown) {
2531 			err = -ENODEV;
2532 			break;
2533 		}
2534 		if (signal_pending(current)) {
2535 			err = -ERESTARTSYS;
2536 			break;
2537 		}
2538 	}
2539 	remove_wait_queue(&pcm->open_wait, &wait);
2540 	mutex_unlock(&pcm->open_mutex);
2541 	if (err < 0)
2542 		goto __error;
2543 	snd_card_unref(pcm->card);
2544 	return err;
2545 
2546       __error:
2547      	module_put(pcm->card->module);
2548       __error2:
2549       	snd_card_file_remove(pcm->card, file);
2550       __error1:
2551 	if (pcm)
2552 		snd_card_unref(pcm->card);
2553 	return err;
2554 }
2555 
snd_pcm_oss_release(struct inode * inode,struct file * file)2556 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2557 {
2558 	struct snd_pcm *pcm;
2559 	struct snd_pcm_substream *substream;
2560 	struct snd_pcm_oss_file *pcm_oss_file;
2561 
2562 	pcm_oss_file = file->private_data;
2563 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2564 	if (substream == NULL)
2565 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2566 	if (snd_BUG_ON(!substream))
2567 		return -ENXIO;
2568 	pcm = substream->pcm;
2569 	if (!pcm->card->shutdown)
2570 		snd_pcm_oss_sync(pcm_oss_file);
2571 	mutex_lock(&pcm->open_mutex);
2572 	snd_pcm_oss_release_file(pcm_oss_file);
2573 	mutex_unlock(&pcm->open_mutex);
2574 	wake_up(&pcm->open_wait);
2575 	module_put(pcm->card->module);
2576 	snd_card_file_remove(pcm->card, file);
2577 	return 0;
2578 }
2579 
snd_pcm_oss_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2580 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2581 {
2582 	struct snd_pcm_oss_file *pcm_oss_file;
2583 	int __user *p = (int __user *)arg;
2584 	int res;
2585 
2586 	pcm_oss_file = file->private_data;
2587 	if (cmd == OSS_GETVERSION)
2588 		return put_user(SNDRV_OSS_VERSION, p);
2589 	if (cmd == OSS_ALSAEMULVER)
2590 		return put_user(1, p);
2591 #if IS_REACHABLE(CONFIG_SND_MIXER_OSS)
2592 	if (((cmd >> 8) & 0xff) == 'M')	{	/* mixer ioctl - for OSS compatibility */
2593 		struct snd_pcm_substream *substream;
2594 		int idx;
2595 		for (idx = 0; idx < 2; ++idx) {
2596 			substream = pcm_oss_file->streams[idx];
2597 			if (substream != NULL)
2598 				break;
2599 		}
2600 		if (snd_BUG_ON(idx >= 2))
2601 			return -ENXIO;
2602 		return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2603 	}
2604 #endif
2605 	if (((cmd >> 8) & 0xff) != 'P')
2606 		return -EINVAL;
2607 #ifdef OSS_DEBUG
2608 	pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2609 #endif
2610 	switch (cmd) {
2611 	case SNDCTL_DSP_RESET:
2612 		return snd_pcm_oss_reset(pcm_oss_file);
2613 	case SNDCTL_DSP_SYNC:
2614 		return snd_pcm_oss_sync(pcm_oss_file);
2615 	case SNDCTL_DSP_SPEED:
2616 		if (get_user(res, p))
2617 			return -EFAULT;
2618 		if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2619 			return res;
2620 		return put_user(res, p);
2621 	case SOUND_PCM_READ_RATE:
2622 		res = snd_pcm_oss_get_rate(pcm_oss_file);
2623 		if (res < 0)
2624 			return res;
2625 		return put_user(res, p);
2626 	case SNDCTL_DSP_STEREO:
2627 		if (get_user(res, p))
2628 			return -EFAULT;
2629 		res = res > 0 ? 2 : 1;
2630 		if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2631 			return res;
2632 		return put_user(--res, p);
2633 	case SNDCTL_DSP_GETBLKSIZE:
2634 		res = snd_pcm_oss_get_block_size(pcm_oss_file);
2635 		if (res < 0)
2636 			return res;
2637 		return put_user(res, p);
2638 	case SNDCTL_DSP_SETFMT:
2639 		if (get_user(res, p))
2640 			return -EFAULT;
2641 		res = snd_pcm_oss_set_format(pcm_oss_file, res);
2642 		if (res < 0)
2643 			return res;
2644 		return put_user(res, p);
2645 	case SOUND_PCM_READ_BITS:
2646 		res = snd_pcm_oss_get_format(pcm_oss_file);
2647 		if (res < 0)
2648 			return res;
2649 		return put_user(res, p);
2650 	case SNDCTL_DSP_CHANNELS:
2651 		if (get_user(res, p))
2652 			return -EFAULT;
2653 		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2654 		if (res < 0)
2655 			return res;
2656 		return put_user(res, p);
2657 	case SOUND_PCM_READ_CHANNELS:
2658 		res = snd_pcm_oss_get_channels(pcm_oss_file);
2659 		if (res < 0)
2660 			return res;
2661 		return put_user(res, p);
2662 	case SOUND_PCM_WRITE_FILTER:
2663 	case SOUND_PCM_READ_FILTER:
2664 		return -EIO;
2665 	case SNDCTL_DSP_POST:
2666 		return snd_pcm_oss_post(pcm_oss_file);
2667 	case SNDCTL_DSP_SUBDIVIDE:
2668 		if (get_user(res, p))
2669 			return -EFAULT;
2670 		res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2671 		if (res < 0)
2672 			return res;
2673 		return put_user(res, p);
2674 	case SNDCTL_DSP_SETFRAGMENT:
2675 		if (get_user(res, p))
2676 			return -EFAULT;
2677 		return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2678 	case SNDCTL_DSP_GETFMTS:
2679 		res = snd_pcm_oss_get_formats(pcm_oss_file);
2680 		if (res < 0)
2681 			return res;
2682 		return put_user(res, p);
2683 	case SNDCTL_DSP_GETOSPACE:
2684 	case SNDCTL_DSP_GETISPACE:
2685 		return snd_pcm_oss_get_space(pcm_oss_file,
2686 			cmd == SNDCTL_DSP_GETISPACE ?
2687 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2688 			(struct audio_buf_info __user *) arg);
2689 	case SNDCTL_DSP_NONBLOCK:
2690 		return snd_pcm_oss_nonblock(file);
2691 	case SNDCTL_DSP_GETCAPS:
2692 		res = snd_pcm_oss_get_caps(pcm_oss_file);
2693 		if (res < 0)
2694 			return res;
2695 		return put_user(res, p);
2696 	case SNDCTL_DSP_GETTRIGGER:
2697 		res = snd_pcm_oss_get_trigger(pcm_oss_file);
2698 		if (res < 0)
2699 			return res;
2700 		return put_user(res, p);
2701 	case SNDCTL_DSP_SETTRIGGER:
2702 		if (get_user(res, p))
2703 			return -EFAULT;
2704 		return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2705 	case SNDCTL_DSP_GETIPTR:
2706 	case SNDCTL_DSP_GETOPTR:
2707 		return snd_pcm_oss_get_ptr(pcm_oss_file,
2708 			cmd == SNDCTL_DSP_GETIPTR ?
2709 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2710 			(struct count_info __user *) arg);
2711 	case SNDCTL_DSP_MAPINBUF:
2712 	case SNDCTL_DSP_MAPOUTBUF:
2713 		return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2714 			cmd == SNDCTL_DSP_MAPINBUF ?
2715 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2716 			(struct buffmem_desc __user *) arg);
2717 	case SNDCTL_DSP_SETSYNCRO:
2718 		/* stop DMA now.. */
2719 		return 0;
2720 	case SNDCTL_DSP_SETDUPLEX:
2721 		if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2722 			return 0;
2723 		return -EIO;
2724 	case SNDCTL_DSP_GETODELAY:
2725 		res = snd_pcm_oss_get_odelay(pcm_oss_file);
2726 		if (res < 0) {
2727 			/* it's for sure, some broken apps don't check for error codes */
2728 			put_user(0, p);
2729 			return res;
2730 		}
2731 		return put_user(res, p);
2732 	case SNDCTL_DSP_PROFILE:
2733 		return 0;	/* silently ignore */
2734 	default:
2735 		pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2736 	}
2737 	return -EINVAL;
2738 }
2739 
2740 #ifdef CONFIG_COMPAT
2741 /* all compatible */
snd_pcm_oss_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)2742 static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
2743 				     unsigned long arg)
2744 {
2745 	/*
2746 	 * Everything is compatbile except SNDCTL_DSP_MAPINBUF/SNDCTL_DSP_MAPOUTBUF,
2747 	 * which are not implemented for the native case either
2748 	 */
2749 	return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2750 }
2751 #else
2752 #define snd_pcm_oss_ioctl_compat	NULL
2753 #endif
2754 
snd_pcm_oss_read(struct file * file,char __user * buf,size_t count,loff_t * offset)2755 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2756 {
2757 	struct snd_pcm_oss_file *pcm_oss_file;
2758 	struct snd_pcm_substream *substream;
2759 
2760 	pcm_oss_file = file->private_data;
2761 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2762 	if (substream == NULL)
2763 		return -ENXIO;
2764 	substream->f_flags = file->f_flags & O_NONBLOCK;
2765 #ifndef OSS_DEBUG
2766 	return snd_pcm_oss_read1(substream, buf, count);
2767 #else
2768 	{
2769 		ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2770 		pcm_dbg(substream->pcm,
2771 			"pcm_oss: read %li bytes (returned %li bytes)\n",
2772 			(long)count, (long)res);
2773 		return res;
2774 	}
2775 #endif
2776 }
2777 
snd_pcm_oss_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)2778 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2779 {
2780 	struct snd_pcm_oss_file *pcm_oss_file;
2781 	struct snd_pcm_substream *substream;
2782 	long result;
2783 
2784 	pcm_oss_file = file->private_data;
2785 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2786 	if (substream == NULL)
2787 		return -ENXIO;
2788 	substream->f_flags = file->f_flags & O_NONBLOCK;
2789 	result = snd_pcm_oss_write1(substream, buf, count);
2790 #ifdef OSS_DEBUG
2791 	pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2792 	       (long)count, (long)result);
2793 #endif
2794 	return result;
2795 }
2796 
snd_pcm_oss_playback_ready(struct snd_pcm_substream * substream)2797 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2798 {
2799 	struct snd_pcm_runtime *runtime = substream->runtime;
2800 	if (atomic_read(&substream->mmap_count))
2801 		return runtime->oss.prev_hw_ptr_period !=
2802 						get_hw_ptr_period(runtime);
2803 	else
2804 		return snd_pcm_playback_avail(runtime) >=
2805 						runtime->oss.period_frames;
2806 }
2807 
snd_pcm_oss_capture_ready(struct snd_pcm_substream * substream)2808 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2809 {
2810 	struct snd_pcm_runtime *runtime = substream->runtime;
2811 	if (atomic_read(&substream->mmap_count))
2812 		return runtime->oss.prev_hw_ptr_period !=
2813 						get_hw_ptr_period(runtime);
2814 	else
2815 		return snd_pcm_capture_avail(runtime) >=
2816 						runtime->oss.period_frames;
2817 }
2818 
snd_pcm_oss_poll(struct file * file,poll_table * wait)2819 static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait)
2820 {
2821 	struct snd_pcm_oss_file *pcm_oss_file;
2822 	__poll_t mask;
2823 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2824 
2825 	pcm_oss_file = file->private_data;
2826 
2827 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2828 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2829 
2830 	mask = 0;
2831 	if (psubstream != NULL) {
2832 		struct snd_pcm_runtime *runtime = psubstream->runtime;
2833 		poll_wait(file, &runtime->sleep, wait);
2834 		snd_pcm_stream_lock_irq(psubstream);
2835 		if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2836 		    (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2837 		     snd_pcm_oss_playback_ready(psubstream)))
2838 			mask |= EPOLLOUT | EPOLLWRNORM;
2839 		snd_pcm_stream_unlock_irq(psubstream);
2840 	}
2841 	if (csubstream != NULL) {
2842 		struct snd_pcm_runtime *runtime = csubstream->runtime;
2843 		snd_pcm_state_t ostate;
2844 		poll_wait(file, &runtime->sleep, wait);
2845 		snd_pcm_stream_lock_irq(csubstream);
2846 		if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2847 		    snd_pcm_oss_capture_ready(csubstream))
2848 			mask |= EPOLLIN | EPOLLRDNORM;
2849 		snd_pcm_stream_unlock_irq(csubstream);
2850 		if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2851 			struct snd_pcm_oss_file ofile;
2852 			memset(&ofile, 0, sizeof(ofile));
2853 			ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2854 			runtime->oss.trigger = 0;
2855 			snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2856 		}
2857 	}
2858 
2859 	return mask;
2860 }
2861 
snd_pcm_oss_mmap(struct file * file,struct vm_area_struct * area)2862 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2863 {
2864 	struct snd_pcm_oss_file *pcm_oss_file;
2865 	struct snd_pcm_substream *substream = NULL;
2866 	struct snd_pcm_runtime *runtime;
2867 	int err;
2868 
2869 #ifdef OSS_DEBUG
2870 	pr_debug("pcm_oss: mmap begin\n");
2871 #endif
2872 	pcm_oss_file = file->private_data;
2873 	switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2874 	case VM_READ | VM_WRITE:
2875 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2876 		if (substream)
2877 			break;
2878 		fallthrough;
2879 	case VM_READ:
2880 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2881 		break;
2882 	case VM_WRITE:
2883 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2884 		break;
2885 	default:
2886 		return -EINVAL;
2887 	}
2888 	/* set VM_READ access as well to fix memset() routines that do
2889 	   reads before writes (to improve performance) */
2890 	area->vm_flags |= VM_READ;
2891 	if (substream == NULL)
2892 		return -ENXIO;
2893 	runtime = substream->runtime;
2894 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2895 		return -EIO;
2896 	if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2897 		runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2898 	else
2899 		return -EIO;
2900 
2901 	if (runtime->oss.params) {
2902 		/* use mutex_trylock() for params_lock for avoiding a deadlock
2903 		 * between mmap_lock and params_lock taken by
2904 		 * copy_from/to_user() in snd_pcm_oss_write/read()
2905 		 */
2906 		err = snd_pcm_oss_change_params(substream, true);
2907 		if (err < 0)
2908 			return err;
2909 	}
2910 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2911 	if (runtime->oss.plugin_first != NULL)
2912 		return -EIO;
2913 #endif
2914 
2915 	if (area->vm_pgoff != 0)
2916 		return -EINVAL;
2917 
2918 	err = snd_pcm_mmap_data(substream, file, area);
2919 	if (err < 0)
2920 		return err;
2921 	runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2922 	runtime->silence_threshold = 0;
2923 	runtime->silence_size = 0;
2924 #ifdef OSS_DEBUG
2925 	pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2926 	       runtime->oss.mmap_bytes);
2927 #endif
2928 	/* In mmap mode we never stop */
2929 	runtime->stop_threshold = runtime->boundary;
2930 
2931 	return 0;
2932 }
2933 
2934 #ifdef CONFIG_SND_VERBOSE_PROCFS
2935 /*
2936  *  /proc interface
2937  */
2938 
snd_pcm_oss_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)2939 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2940 				  struct snd_info_buffer *buffer)
2941 {
2942 	struct snd_pcm_str *pstr = entry->private_data;
2943 	struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2944 	mutex_lock(&pstr->oss.setup_mutex);
2945 	while (setup) {
2946 		snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2947 			    setup->task_name,
2948 			    setup->periods,
2949 			    setup->period_size,
2950 			    setup->disable ? " disable" : "",
2951 			    setup->direct ? " direct" : "",
2952 			    setup->block ? " block" : "",
2953 			    setup->nonblock ? " non-block" : "",
2954 			    setup->partialfrag ? " partial-frag" : "",
2955 			    setup->nosilence ? " no-silence" : "");
2956 		setup = setup->next;
2957 	}
2958 	mutex_unlock(&pstr->oss.setup_mutex);
2959 }
2960 
snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)2961 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2962 {
2963 	struct snd_pcm_oss_setup *setup, *setupn;
2964 
2965 	for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2966 	     setup; setup = setupn) {
2967 		setupn = setup->next;
2968 		kfree(setup->task_name);
2969 		kfree(setup);
2970 	}
2971 	pstr->oss.setup_list = NULL;
2972 }
2973 
snd_pcm_oss_proc_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)2974 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2975 				   struct snd_info_buffer *buffer)
2976 {
2977 	struct snd_pcm_str *pstr = entry->private_data;
2978 	char line[128], str[32], task_name[32];
2979 	const char *ptr;
2980 	int idx1;
2981 	struct snd_pcm_oss_setup *setup, *setup1, template;
2982 
2983 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
2984 		mutex_lock(&pstr->oss.setup_mutex);
2985 		memset(&template, 0, sizeof(template));
2986 		ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2987 		if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2988 			snd_pcm_oss_proc_free_setup_list(pstr);
2989 			mutex_unlock(&pstr->oss.setup_mutex);
2990 			continue;
2991 		}
2992 		for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2993 			if (!strcmp(setup->task_name, task_name)) {
2994 				template = *setup;
2995 				break;
2996 			}
2997 		}
2998 		ptr = snd_info_get_str(str, ptr, sizeof(str));
2999 		template.periods = simple_strtoul(str, NULL, 10);
3000 		ptr = snd_info_get_str(str, ptr, sizeof(str));
3001 		template.period_size = simple_strtoul(str, NULL, 10);
3002 		for (idx1 = 31; idx1 >= 0; idx1--)
3003 			if (template.period_size & (1 << idx1))
3004 				break;
3005 		for (idx1--; idx1 >= 0; idx1--)
3006 			template.period_size &= ~(1 << idx1);
3007 		do {
3008 			ptr = snd_info_get_str(str, ptr, sizeof(str));
3009 			if (!strcmp(str, "disable")) {
3010 				template.disable = 1;
3011 			} else if (!strcmp(str, "direct")) {
3012 				template.direct = 1;
3013 			} else if (!strcmp(str, "block")) {
3014 				template.block = 1;
3015 			} else if (!strcmp(str, "non-block")) {
3016 				template.nonblock = 1;
3017 			} else if (!strcmp(str, "partial-frag")) {
3018 				template.partialfrag = 1;
3019 			} else if (!strcmp(str, "no-silence")) {
3020 				template.nosilence = 1;
3021 			} else if (!strcmp(str, "buggy-ptr")) {
3022 				template.buggyptr = 1;
3023 			}
3024 		} while (*str);
3025 		if (setup == NULL) {
3026 			setup = kmalloc(sizeof(*setup), GFP_KERNEL);
3027 			if (! setup) {
3028 				buffer->error = -ENOMEM;
3029 				mutex_unlock(&pstr->oss.setup_mutex);
3030 				return;
3031 			}
3032 			if (pstr->oss.setup_list == NULL)
3033 				pstr->oss.setup_list = setup;
3034 			else {
3035 				for (setup1 = pstr->oss.setup_list;
3036 				     setup1->next; setup1 = setup1->next);
3037 				setup1->next = setup;
3038 			}
3039 			template.task_name = kstrdup(task_name, GFP_KERNEL);
3040 			if (! template.task_name) {
3041 				kfree(setup);
3042 				buffer->error = -ENOMEM;
3043 				mutex_unlock(&pstr->oss.setup_mutex);
3044 				return;
3045 			}
3046 		}
3047 		*setup = template;
3048 		mutex_unlock(&pstr->oss.setup_mutex);
3049 	}
3050 }
3051 
snd_pcm_oss_proc_init(struct snd_pcm * pcm)3052 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3053 {
3054 	int stream;
3055 	for (stream = 0; stream < 2; ++stream) {
3056 		struct snd_info_entry *entry;
3057 		struct snd_pcm_str *pstr = &pcm->streams[stream];
3058 		if (pstr->substream_count == 0)
3059 			continue;
3060 		if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
3061 			entry->content = SNDRV_INFO_CONTENT_TEXT;
3062 			entry->mode = S_IFREG | 0644;
3063 			entry->c.text.read = snd_pcm_oss_proc_read;
3064 			entry->c.text.write = snd_pcm_oss_proc_write;
3065 			entry->private_data = pstr;
3066 			if (snd_info_register(entry) < 0) {
3067 				snd_info_free_entry(entry);
3068 				entry = NULL;
3069 			}
3070 		}
3071 		pstr->oss.proc_entry = entry;
3072 	}
3073 }
3074 
snd_pcm_oss_proc_done(struct snd_pcm * pcm)3075 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3076 {
3077 	int stream;
3078 	for (stream = 0; stream < 2; ++stream) {
3079 		struct snd_pcm_str *pstr = &pcm->streams[stream];
3080 		snd_info_free_entry(pstr->oss.proc_entry);
3081 		pstr->oss.proc_entry = NULL;
3082 		snd_pcm_oss_proc_free_setup_list(pstr);
3083 	}
3084 }
3085 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3086 #define snd_pcm_oss_proc_init(pcm)
3087 #define snd_pcm_oss_proc_done(pcm)
3088 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3089 
3090 /*
3091  *  ENTRY functions
3092  */
3093 
3094 static const struct file_operations snd_pcm_oss_f_reg =
3095 {
3096 	.owner =	THIS_MODULE,
3097 	.read =		snd_pcm_oss_read,
3098 	.write =	snd_pcm_oss_write,
3099 	.open =		snd_pcm_oss_open,
3100 	.release =	snd_pcm_oss_release,
3101 	.llseek =	no_llseek,
3102 	.poll =		snd_pcm_oss_poll,
3103 	.unlocked_ioctl =	snd_pcm_oss_ioctl,
3104 	.compat_ioctl =	snd_pcm_oss_ioctl_compat,
3105 	.mmap =		snd_pcm_oss_mmap,
3106 };
3107 
register_oss_dsp(struct snd_pcm * pcm,int index)3108 static void register_oss_dsp(struct snd_pcm *pcm, int index)
3109 {
3110 	if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3111 				    pcm->card, index, &snd_pcm_oss_f_reg,
3112 				    pcm) < 0) {
3113 		pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3114 			   pcm->card->number, pcm->device);
3115 	}
3116 }
3117 
snd_pcm_oss_register_minor(struct snd_pcm * pcm)3118 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3119 {
3120 	pcm->oss.reg = 0;
3121 	if (dsp_map[pcm->card->number] == (int)pcm->device) {
3122 		char name[128];
3123 		int duplex;
3124 		register_oss_dsp(pcm, 0);
3125 		duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3126 			      pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3127 			      !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3128 		sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3129 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3130 		snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3131 				      pcm->card->number,
3132 				      name);
3133 #endif
3134 		pcm->oss.reg++;
3135 		pcm->oss.reg_mask |= 1;
3136 	}
3137 	if (adsp_map[pcm->card->number] == (int)pcm->device) {
3138 		register_oss_dsp(pcm, 1);
3139 		pcm->oss.reg++;
3140 		pcm->oss.reg_mask |= 2;
3141 	}
3142 
3143 	if (pcm->oss.reg)
3144 		snd_pcm_oss_proc_init(pcm);
3145 
3146 	return 0;
3147 }
3148 
snd_pcm_oss_disconnect_minor(struct snd_pcm * pcm)3149 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3150 {
3151 	if (pcm->oss.reg) {
3152 		if (pcm->oss.reg_mask & 1) {
3153 			pcm->oss.reg_mask &= ~1;
3154 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3155 						  pcm->card, 0);
3156 		}
3157 		if (pcm->oss.reg_mask & 2) {
3158 			pcm->oss.reg_mask &= ~2;
3159 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3160 						  pcm->card, 1);
3161 		}
3162 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
3163 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3164 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3165 #endif
3166 		}
3167 		pcm->oss.reg = 0;
3168 	}
3169 	return 0;
3170 }
3171 
snd_pcm_oss_unregister_minor(struct snd_pcm * pcm)3172 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3173 {
3174 	snd_pcm_oss_disconnect_minor(pcm);
3175 	snd_pcm_oss_proc_done(pcm);
3176 	return 0;
3177 }
3178 
3179 static struct snd_pcm_notify snd_pcm_oss_notify =
3180 {
3181 	.n_register =	snd_pcm_oss_register_minor,
3182 	.n_disconnect = snd_pcm_oss_disconnect_minor,
3183 	.n_unregister =	snd_pcm_oss_unregister_minor,
3184 };
3185 
alsa_pcm_oss_init(void)3186 static int __init alsa_pcm_oss_init(void)
3187 {
3188 	int i;
3189 	int err;
3190 
3191 	/* check device map table */
3192 	for (i = 0; i < SNDRV_CARDS; i++) {
3193 		if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3194 			pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3195 				   i, dsp_map[i]);
3196 			dsp_map[i] = 0;
3197 		}
3198 		if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3199 			pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3200 				   i, adsp_map[i]);
3201 			adsp_map[i] = 1;
3202 		}
3203 	}
3204 	if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3205 		return err;
3206 	return 0;
3207 }
3208 
alsa_pcm_oss_exit(void)3209 static void __exit alsa_pcm_oss_exit(void)
3210 {
3211 	snd_pcm_notify(&snd_pcm_oss_notify, 1);
3212 }
3213 
3214 module_init(alsa_pcm_oss_init)
3215 module_exit(alsa_pcm_oss_exit)
3216