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