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