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