xref: /linux/sound/core/oss/pcm_oss.c (revision ddea524e437eae0d5581482a90565b4c1e6d4b24)
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 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1150 	if (err < 0) {
1151 		pcm_dbg(substream->pcm,
1152 			"snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1153 		return err;
1154 	}
1155 	runtime->oss.prepare = 0;
1156 	runtime->oss.prev_hw_ptr_period = 0;
1157 	runtime->oss.period_ptr = 0;
1158 	runtime->oss.buffer_used = 0;
1159 
1160 	return 0;
1161 }
1162 
1163 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1164 {
1165 	struct snd_pcm_runtime *runtime;
1166 	int err;
1167 
1168 	runtime = substream->runtime;
1169 	if (runtime->oss.params) {
1170 		err = snd_pcm_oss_change_params(substream, false);
1171 		if (err < 0)
1172 			return err;
1173 	}
1174 	if (runtime->oss.prepare) {
1175 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
1176 			return -ERESTARTSYS;
1177 		err = snd_pcm_oss_prepare(substream);
1178 		mutex_unlock(&runtime->oss.params_lock);
1179 		if (err < 0)
1180 			return err;
1181 	}
1182 	return 0;
1183 }
1184 
1185 /* call with params_lock held */
1186 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1187 {
1188 	struct snd_pcm_runtime *runtime;
1189 	int err;
1190 
1191 	runtime = substream->runtime;
1192 	if (runtime->oss.params) {
1193 		err = snd_pcm_oss_change_params_locked(substream);
1194 		if (err < 0)
1195 			return err;
1196 	}
1197 	if (runtime->oss.prepare) {
1198 		err = snd_pcm_oss_prepare(substream);
1199 		if (err < 0)
1200 			return err;
1201 	}
1202 	return 0;
1203 }
1204 
1205 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1206 {
1207 	struct snd_pcm_runtime *runtime;
1208 	snd_pcm_uframes_t frames;
1209 	int err = 0;
1210 
1211 	while (1) {
1212 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1213 		if (err < 0)
1214 			break;
1215 		runtime = substream->runtime;
1216 		if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1217 			break;
1218 		/* in case of overrun, skip whole periods like OSS/Linux driver does */
1219 		/* until avail(delay) <= buffer_size */
1220 		frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1221 		frames /= runtime->period_size;
1222 		frames *= runtime->period_size;
1223 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1224 		if (err < 0)
1225 			break;
1226 	}
1227 	return err;
1228 }
1229 
1230 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1231 {
1232 	struct snd_pcm_runtime *runtime = substream->runtime;
1233 	snd_pcm_state_t state;
1234 	int ret;
1235 	while (1) {
1236 		state = snd_pcm_get_state(substream);
1237 		if (state == SNDRV_PCM_STATE_XRUN ||
1238 		    state == SNDRV_PCM_STATE_SUSPENDED) {
1239 #ifdef OSS_DEBUG
1240 			pcm_dbg(substream->pcm,
1241 				"pcm_oss: write: recovering from %s\n",
1242 				state == SNDRV_PCM_STATE_XRUN ?
1243 				"XRUN" : "SUSPEND");
1244 #endif
1245 			ret = snd_pcm_oss_prepare(substream);
1246 			if (ret < 0)
1247 				break;
1248 		}
1249 		mutex_unlock(&runtime->oss.params_lock);
1250 		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1251 					 frames, in_kernel);
1252 		mutex_lock(&runtime->oss.params_lock);
1253 		if (ret != -EPIPE && ret != -ESTRPIPE)
1254 			break;
1255 		/* test, if we can't store new data, because the stream */
1256 		/* has not been started */
1257 		if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_PREPARED)
1258 			return -EAGAIN;
1259 	}
1260 	return ret;
1261 }
1262 
1263 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1264 {
1265 	struct snd_pcm_runtime *runtime = substream->runtime;
1266 	snd_pcm_sframes_t delay;
1267 	snd_pcm_state_t state;
1268 	int ret;
1269 	while (1) {
1270 		state = snd_pcm_get_state(substream);
1271 		if (state == SNDRV_PCM_STATE_XRUN ||
1272 		    state == SNDRV_PCM_STATE_SUSPENDED) {
1273 #ifdef OSS_DEBUG
1274 			pcm_dbg(substream->pcm,
1275 				"pcm_oss: read: recovering from %s\n",
1276 				state == SNDRV_PCM_STATE_XRUN ?
1277 				"XRUN" : "SUSPEND");
1278 #endif
1279 			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1280 			if (ret < 0)
1281 				break;
1282 		} else if (state == SNDRV_PCM_STATE_SETUP) {
1283 			ret = snd_pcm_oss_prepare(substream);
1284 			if (ret < 0)
1285 				break;
1286 		}
1287 		ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1288 		if (ret < 0)
1289 			break;
1290 		mutex_unlock(&runtime->oss.params_lock);
1291 		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1292 					 frames, in_kernel);
1293 		mutex_lock(&runtime->oss.params_lock);
1294 		if (ret == -EPIPE) {
1295 			if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_DRAINING) {
1296 				ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1297 				if (ret < 0)
1298 					break;
1299 			}
1300 			continue;
1301 		}
1302 		if (ret != -ESTRPIPE)
1303 			break;
1304 	}
1305 	return ret;
1306 }
1307 
1308 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1309 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1310 {
1311 	snd_pcm_state_t state;
1312 	int ret;
1313 	while (1) {
1314 		state = snd_pcm_get_state(substream);
1315 		if (state == SNDRV_PCM_STATE_XRUN ||
1316 		    state == SNDRV_PCM_STATE_SUSPENDED) {
1317 #ifdef OSS_DEBUG
1318 			pcm_dbg(substream->pcm,
1319 				"pcm_oss: writev: recovering from %s\n",
1320 				state == SNDRV_PCM_STATE_XRUN ?
1321 				"XRUN" : "SUSPEND");
1322 #endif
1323 			ret = snd_pcm_oss_prepare(substream);
1324 			if (ret < 0)
1325 				break;
1326 		}
1327 		ret = snd_pcm_kernel_writev(substream, bufs, frames);
1328 		if (ret != -EPIPE && ret != -ESTRPIPE)
1329 			break;
1330 
1331 		/* test, if we can't store new data, because the stream */
1332 		/* has not been started */
1333 		if (snd_pcm_get_state(substream) == SNDRV_PCM_STATE_PREPARED)
1334 			return -EAGAIN;
1335 	}
1336 	return ret;
1337 }
1338 
1339 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1340 {
1341 	snd_pcm_state_t state;
1342 	int ret;
1343 	while (1) {
1344 		state = snd_pcm_get_state(substream);
1345 		if (state == SNDRV_PCM_STATE_XRUN ||
1346 		    state == SNDRV_PCM_STATE_SUSPENDED) {
1347 #ifdef OSS_DEBUG
1348 			pcm_dbg(substream->pcm,
1349 				"pcm_oss: readv: recovering from %s\n",
1350 				state == SNDRV_PCM_STATE_XRUN ?
1351 				"XRUN" : "SUSPEND");
1352 #endif
1353 			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1354 			if (ret < 0)
1355 				break;
1356 		} else if (state == SNDRV_PCM_STATE_SETUP) {
1357 			ret = snd_pcm_oss_prepare(substream);
1358 			if (ret < 0)
1359 				break;
1360 		}
1361 		ret = snd_pcm_kernel_readv(substream, bufs, frames);
1362 		if (ret != -EPIPE && ret != -ESTRPIPE)
1363 			break;
1364 	}
1365 	return ret;
1366 }
1367 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
1368 
1369 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1370 {
1371 	struct snd_pcm_runtime *runtime = substream->runtime;
1372 	snd_pcm_sframes_t frames, frames1;
1373 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1374 	if (runtime->oss.plugin_first) {
1375 		struct snd_pcm_plugin_channel *channels;
1376 		size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1377 		if (!in_kernel) {
1378 			if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1379 				return -EFAULT;
1380 			buf = runtime->oss.buffer;
1381 		}
1382 		frames = bytes / oss_frame_bytes;
1383 		frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1384 		if (frames1 < 0)
1385 			return frames1;
1386 		frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1387 		if (frames1 <= 0)
1388 			return frames1;
1389 		bytes = frames1 * oss_frame_bytes;
1390 	} else
1391 #endif
1392 	{
1393 		frames = bytes_to_frames(runtime, bytes);
1394 		frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1395 		if (frames1 <= 0)
1396 			return frames1;
1397 		bytes = frames_to_bytes(runtime, frames1);
1398 	}
1399 	return bytes;
1400 }
1401 
1402 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1403 {
1404 	size_t xfer = 0;
1405 	ssize_t tmp = 0;
1406 	struct snd_pcm_runtime *runtime = substream->runtime;
1407 
1408 	if (atomic_read(&substream->mmap_count))
1409 		return -ENXIO;
1410 
1411 	atomic_inc(&runtime->oss.rw_ref);
1412 	while (bytes > 0) {
1413 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1414 			tmp = -ERESTARTSYS;
1415 			break;
1416 		}
1417 		tmp = snd_pcm_oss_make_ready_locked(substream);
1418 		if (tmp < 0)
1419 			goto err;
1420 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1421 			tmp = bytes;
1422 			if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1423 				tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1424 			if (tmp > 0) {
1425 				if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1426 					tmp = -EFAULT;
1427 					goto err;
1428 				}
1429 			}
1430 			runtime->oss.buffer_used += tmp;
1431 			buf += tmp;
1432 			bytes -= tmp;
1433 			xfer += tmp;
1434 			if (substream->oss.setup.partialfrag ||
1435 			    runtime->oss.buffer_used == runtime->oss.period_bytes) {
1436 				tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1437 							 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1438 				if (tmp <= 0)
1439 					goto err;
1440 				runtime->oss.bytes += tmp;
1441 				runtime->oss.period_ptr += tmp;
1442 				runtime->oss.period_ptr %= runtime->oss.period_bytes;
1443 				if (runtime->oss.period_ptr == 0 ||
1444 				    runtime->oss.period_ptr == runtime->oss.buffer_used)
1445 					runtime->oss.buffer_used = 0;
1446 				else if ((substream->f_flags & O_NONBLOCK) != 0) {
1447 					tmp = -EAGAIN;
1448 					goto err;
1449 				}
1450 			}
1451 		} else {
1452 			tmp = snd_pcm_oss_write2(substream,
1453 						 (const char __force *)buf,
1454 						 runtime->oss.period_bytes, 0);
1455 			if (tmp <= 0)
1456 				goto err;
1457 			runtime->oss.bytes += tmp;
1458 			buf += tmp;
1459 			bytes -= tmp;
1460 			xfer += tmp;
1461 			if ((substream->f_flags & O_NONBLOCK) != 0 &&
1462 			    tmp != runtime->oss.period_bytes)
1463 				tmp = -EAGAIN;
1464 		}
1465  err:
1466 		mutex_unlock(&runtime->oss.params_lock);
1467 		if (tmp < 0)
1468 			break;
1469 		if (signal_pending(current)) {
1470 			tmp = -ERESTARTSYS;
1471 			break;
1472 		}
1473 		tmp = 0;
1474 	}
1475 	atomic_dec(&runtime->oss.rw_ref);
1476 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1477 }
1478 
1479 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1480 {
1481 	struct snd_pcm_runtime *runtime = substream->runtime;
1482 	snd_pcm_sframes_t frames, frames1;
1483 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1484 	char __user *final_dst = (char __force __user *)buf;
1485 	if (runtime->oss.plugin_first) {
1486 		struct snd_pcm_plugin_channel *channels;
1487 		size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1488 		if (!in_kernel)
1489 			buf = runtime->oss.buffer;
1490 		frames = bytes / oss_frame_bytes;
1491 		frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1492 		if (frames1 < 0)
1493 			return frames1;
1494 		frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1495 		if (frames1 <= 0)
1496 			return frames1;
1497 		bytes = frames1 * oss_frame_bytes;
1498 		if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1499 			return -EFAULT;
1500 	} else
1501 #endif
1502 	{
1503 		frames = bytes_to_frames(runtime, bytes);
1504 		frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1505 		if (frames1 <= 0)
1506 			return frames1;
1507 		bytes = frames_to_bytes(runtime, frames1);
1508 	}
1509 	return bytes;
1510 }
1511 
1512 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1513 {
1514 	size_t xfer = 0;
1515 	ssize_t tmp = 0;
1516 	struct snd_pcm_runtime *runtime = substream->runtime;
1517 
1518 	if (atomic_read(&substream->mmap_count))
1519 		return -ENXIO;
1520 
1521 	atomic_inc(&runtime->oss.rw_ref);
1522 	while (bytes > 0) {
1523 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1524 			tmp = -ERESTARTSYS;
1525 			break;
1526 		}
1527 		tmp = snd_pcm_oss_make_ready_locked(substream);
1528 		if (tmp < 0)
1529 			goto err;
1530 		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1531 			if (runtime->oss.buffer_used == 0) {
1532 				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1533 				if (tmp <= 0)
1534 					goto err;
1535 				runtime->oss.bytes += tmp;
1536 				runtime->oss.period_ptr = tmp;
1537 				runtime->oss.buffer_used = tmp;
1538 			}
1539 			tmp = bytes;
1540 			if ((size_t) tmp > runtime->oss.buffer_used)
1541 				tmp = runtime->oss.buffer_used;
1542 			if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1543 				tmp = -EFAULT;
1544 				goto err;
1545 			}
1546 			buf += tmp;
1547 			bytes -= tmp;
1548 			xfer += tmp;
1549 			runtime->oss.buffer_used -= tmp;
1550 		} else {
1551 			tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1552 						runtime->oss.period_bytes, 0);
1553 			if (tmp <= 0)
1554 				goto err;
1555 			runtime->oss.bytes += tmp;
1556 			buf += tmp;
1557 			bytes -= tmp;
1558 			xfer += tmp;
1559 		}
1560  err:
1561 		mutex_unlock(&runtime->oss.params_lock);
1562 		if (tmp < 0)
1563 			break;
1564 		if (signal_pending(current)) {
1565 			tmp = -ERESTARTSYS;
1566 			break;
1567 		}
1568 		tmp = 0;
1569 	}
1570 	atomic_dec(&runtime->oss.rw_ref);
1571 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1572 }
1573 
1574 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1575 {
1576 	struct snd_pcm_substream *substream;
1577 	struct snd_pcm_runtime *runtime;
1578 	int i;
1579 
1580 	for (i = 0; i < 2; i++) {
1581 		substream = pcm_oss_file->streams[i];
1582 		if (!substream)
1583 			continue;
1584 		runtime = substream->runtime;
1585 		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1586 		mutex_lock(&runtime->oss.params_lock);
1587 		runtime->oss.prepare = 1;
1588 		runtime->oss.buffer_used = 0;
1589 		runtime->oss.prev_hw_ptr_period = 0;
1590 		runtime->oss.period_ptr = 0;
1591 		mutex_unlock(&runtime->oss.params_lock);
1592 	}
1593 	return 0;
1594 }
1595 
1596 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1597 {
1598 	struct snd_pcm_substream *substream;
1599 	int err;
1600 
1601 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1602 	if (substream != NULL) {
1603 		err = snd_pcm_oss_make_ready(substream);
1604 		if (err < 0)
1605 			return err;
1606 		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1607 	}
1608 	/* note: all errors from the start action are ignored */
1609 	/* OSS apps do not know, how to handle them */
1610 	return 0;
1611 }
1612 
1613 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1614 {
1615 	struct snd_pcm_runtime *runtime;
1616 	ssize_t result = 0;
1617 	snd_pcm_state_t state;
1618 	long res;
1619 	wait_queue_entry_t wait;
1620 
1621 	runtime = substream->runtime;
1622 	init_waitqueue_entry(&wait, current);
1623 	add_wait_queue(&runtime->sleep, &wait);
1624 #ifdef OSS_DEBUG
1625 	pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1626 #endif
1627 	while (1) {
1628 		result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1629 		if (result > 0) {
1630 			runtime->oss.buffer_used = 0;
1631 			result = 0;
1632 			break;
1633 		}
1634 		if (result != 0 && result != -EAGAIN)
1635 			break;
1636 		result = 0;
1637 		set_current_state(TASK_INTERRUPTIBLE);
1638 		scoped_guard(pcm_stream_lock_irq, substream)
1639 			state = runtime->state;
1640 		if (state != SNDRV_PCM_STATE_RUNNING) {
1641 			set_current_state(TASK_RUNNING);
1642 			break;
1643 		}
1644 		res = schedule_timeout(10 * HZ);
1645 		if (signal_pending(current)) {
1646 			result = -ERESTARTSYS;
1647 			break;
1648 		}
1649 		if (res == 0) {
1650 			pcm_err(substream->pcm,
1651 				"OSS sync error - DMA timeout\n");
1652 			result = -EIO;
1653 			break;
1654 		}
1655 	}
1656 	remove_wait_queue(&runtime->sleep, &wait);
1657 	return result;
1658 }
1659 
1660 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1661 {
1662 	int err = 0;
1663 	unsigned int saved_f_flags;
1664 	struct snd_pcm_substream *substream;
1665 	struct snd_pcm_runtime *runtime;
1666 	snd_pcm_format_t format;
1667 	unsigned long width;
1668 	size_t size;
1669 
1670 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1671 	if (substream != NULL) {
1672 		runtime = substream->runtime;
1673 		if (atomic_read(&substream->mmap_count))
1674 			goto __direct;
1675 		atomic_inc(&runtime->oss.rw_ref);
1676 		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1677 			atomic_dec(&runtime->oss.rw_ref);
1678 			return -ERESTARTSYS;
1679 		}
1680 		err = snd_pcm_oss_make_ready_locked(substream);
1681 		if (err < 0)
1682 			goto unlock;
1683 		format = snd_pcm_oss_format_from(runtime->oss.format);
1684 		width = snd_pcm_format_physical_width(format);
1685 		if (runtime->oss.buffer_used > 0) {
1686 #ifdef OSS_DEBUG
1687 			pcm_dbg(substream->pcm, "sync: buffer_used\n");
1688 #endif
1689 			size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1690 			snd_pcm_format_set_silence(format,
1691 						   runtime->oss.buffer + runtime->oss.buffer_used,
1692 						   size);
1693 			err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1694 			if (err < 0)
1695 				goto unlock;
1696 		} else if (runtime->oss.period_ptr > 0) {
1697 #ifdef OSS_DEBUG
1698 			pcm_dbg(substream->pcm, "sync: period_ptr\n");
1699 #endif
1700 			size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1701 			snd_pcm_format_set_silence(format,
1702 						   runtime->oss.buffer,
1703 						   size * 8 / width);
1704 			err = snd_pcm_oss_sync1(substream, size);
1705 			if (err < 0)
1706 				goto unlock;
1707 		}
1708 		/*
1709 		 * The ALSA's period might be a bit large than OSS one.
1710 		 * Fill the remain portion of ALSA period with zeros.
1711 		 */
1712 		size = runtime->control->appl_ptr % runtime->period_size;
1713 		if (size > 0) {
1714 			size = runtime->period_size - size;
1715 			if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1716 				snd_pcm_lib_write(substream, NULL, size);
1717 			else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1718 				snd_pcm_lib_writev(substream, NULL, size);
1719 		}
1720 unlock:
1721 		mutex_unlock(&runtime->oss.params_lock);
1722 		atomic_dec(&runtime->oss.rw_ref);
1723 		if (err < 0)
1724 			return err;
1725 		/*
1726 		 * finish sync: drain the buffer
1727 		 */
1728 	      __direct:
1729 		saved_f_flags = substream->f_flags;
1730 		substream->f_flags &= ~O_NONBLOCK;
1731 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1732 		substream->f_flags = saved_f_flags;
1733 		if (err < 0)
1734 			return err;
1735 		mutex_lock(&runtime->oss.params_lock);
1736 		runtime->oss.prepare = 1;
1737 		mutex_unlock(&runtime->oss.params_lock);
1738 	}
1739 
1740 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1741 	if (substream != NULL) {
1742 		err = snd_pcm_oss_make_ready(substream);
1743 		if (err < 0)
1744 			return err;
1745 		runtime = substream->runtime;
1746 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1747 		if (err < 0)
1748 			return err;
1749 		mutex_lock(&runtime->oss.params_lock);
1750 		runtime->oss.buffer_used = 0;
1751 		runtime->oss.prepare = 1;
1752 		mutex_unlock(&runtime->oss.params_lock);
1753 	}
1754 	return 0;
1755 }
1756 
1757 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1758 {
1759 	int idx;
1760 
1761 	for (idx = 1; idx >= 0; --idx) {
1762 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1763 		struct snd_pcm_runtime *runtime;
1764 		int err;
1765 
1766 		if (substream == NULL)
1767 			continue;
1768 		runtime = substream->runtime;
1769 		if (rate < 1000)
1770 			rate = 1000;
1771 		else if (rate > 192000)
1772 			rate = 192000;
1773 		err = lock_params(runtime);
1774 		if (err < 0)
1775 			return err;
1776 		if (runtime->oss.rate != rate) {
1777 			runtime->oss.params = 1;
1778 			runtime->oss.rate = rate;
1779 		}
1780 		unlock_params(runtime);
1781 	}
1782 	return snd_pcm_oss_get_rate(pcm_oss_file);
1783 }
1784 
1785 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1786 {
1787 	struct snd_pcm_substream *substream;
1788 	int err;
1789 
1790 	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1791 	if (err < 0)
1792 		return err;
1793 	return substream->runtime->oss.rate;
1794 }
1795 
1796 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1797 {
1798 	int idx;
1799 	if (channels < 1)
1800 		channels = 1;
1801 	if (channels > 128)
1802 		return -EINVAL;
1803 	for (idx = 1; idx >= 0; --idx) {
1804 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1805 		struct snd_pcm_runtime *runtime;
1806 		int err;
1807 
1808 		if (substream == NULL)
1809 			continue;
1810 		runtime = substream->runtime;
1811 		err = lock_params(runtime);
1812 		if (err < 0)
1813 			return err;
1814 		if (runtime->oss.channels != channels) {
1815 			runtime->oss.params = 1;
1816 			runtime->oss.channels = channels;
1817 		}
1818 		unlock_params(runtime);
1819 	}
1820 	return snd_pcm_oss_get_channels(pcm_oss_file);
1821 }
1822 
1823 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1824 {
1825 	struct snd_pcm_substream *substream;
1826 	int err;
1827 
1828 	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1829 	if (err < 0)
1830 		return err;
1831 	return substream->runtime->oss.channels;
1832 }
1833 
1834 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1835 {
1836 	struct snd_pcm_substream *substream;
1837 	int err;
1838 
1839 	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1840 	if (err < 0)
1841 		return err;
1842 	return substream->runtime->oss.period_bytes;
1843 }
1844 
1845 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1846 {
1847 	struct snd_pcm_substream *substream;
1848 	int err;
1849 	int direct;
1850 	unsigned int formats = 0;
1851 	const struct snd_mask *format_mask;
1852 	int fmt;
1853 
1854 	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1855 	if (err < 0)
1856 		return err;
1857 	if (atomic_read(&substream->mmap_count))
1858 		direct = 1;
1859 	else
1860 		direct = substream->oss.setup.direct;
1861 	if (!direct)
1862 		return AFMT_MU_LAW | AFMT_U8 |
1863 		       AFMT_S16_LE | AFMT_S16_BE |
1864 		       AFMT_S8 | AFMT_U16_LE |
1865 		       AFMT_U16_BE |
1866 			AFMT_S32_LE | AFMT_S32_BE |
1867 			AFMT_S24_LE | AFMT_S24_BE |
1868 			AFMT_S24_PACKED;
1869 
1870 	struct snd_pcm_hw_params *params __free(kfree) =
1871 		kmalloc_obj(*params);
1872 	if (!params)
1873 		return -ENOMEM;
1874 	_snd_pcm_hw_params_any(params);
1875 	err = snd_pcm_hw_refine(substream, params);
1876 	if (err < 0)
1877 		return err;
1878 	format_mask = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
1879 	for (fmt = 0; fmt < 32; ++fmt) {
1880 		if (snd_mask_test(format_mask, fmt)) {
1881 			int f = snd_pcm_oss_format_to(fmt);
1882 			if (f >= 0)
1883 				formats |= f;
1884 		}
1885 	}
1886 
1887 	return formats;
1888 }
1889 
1890 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1891 {
1892 	int formats, idx;
1893 	int err;
1894 
1895 	if (format != AFMT_QUERY) {
1896 		formats = snd_pcm_oss_get_formats(pcm_oss_file);
1897 		if (formats < 0)
1898 			return formats;
1899 		if (!(formats & format))
1900 			format = AFMT_U8;
1901 		for (idx = 1; idx >= 0; --idx) {
1902 			struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1903 			struct snd_pcm_runtime *runtime;
1904 			if (substream == NULL)
1905 				continue;
1906 			runtime = substream->runtime;
1907 			err = lock_params(runtime);
1908 			if (err < 0)
1909 				return err;
1910 			if (runtime->oss.format != format) {
1911 				runtime->oss.params = 1;
1912 				runtime->oss.format = format;
1913 			}
1914 			unlock_params(runtime);
1915 		}
1916 	}
1917 	return snd_pcm_oss_get_format(pcm_oss_file);
1918 }
1919 
1920 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1921 {
1922 	struct snd_pcm_substream *substream;
1923 	int err;
1924 
1925 	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1926 	if (err < 0)
1927 		return err;
1928 	return substream->runtime->oss.format;
1929 }
1930 
1931 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1932 {
1933 	struct snd_pcm_runtime *runtime;
1934 
1935 	runtime = substream->runtime;
1936 	if (subdivide == 0) {
1937 		subdivide = runtime->oss.subdivision;
1938 		if (subdivide == 0)
1939 			subdivide = 1;
1940 		return subdivide;
1941 	}
1942 	if (runtime->oss.subdivision || runtime->oss.fragshift)
1943 		return -EINVAL;
1944 	if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1945 	    subdivide != 8 && subdivide != 16)
1946 		return -EINVAL;
1947 	runtime->oss.subdivision = subdivide;
1948 	runtime->oss.params = 1;
1949 	return subdivide;
1950 }
1951 
1952 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1953 {
1954 	int err = -EINVAL, idx;
1955 
1956 	for (idx = 1; idx >= 0; --idx) {
1957 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1958 		struct snd_pcm_runtime *runtime;
1959 
1960 		if (substream == NULL)
1961 			continue;
1962 		runtime = substream->runtime;
1963 		err = lock_params(runtime);
1964 		if (err < 0)
1965 			return err;
1966 		err = snd_pcm_oss_set_subdivide1(substream, subdivide);
1967 		unlock_params(runtime);
1968 		if (err < 0)
1969 			return err;
1970 	}
1971 	return err;
1972 }
1973 
1974 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1975 {
1976 	struct snd_pcm_runtime *runtime;
1977 	int fragshift;
1978 
1979 	runtime = substream->runtime;
1980 	if (runtime->oss.subdivision || runtime->oss.fragshift)
1981 		return -EINVAL;
1982 	fragshift = val & 0xffff;
1983 	if (fragshift >= 25) /* should be large enough */
1984 		return -EINVAL;
1985 	runtime->oss.fragshift = fragshift;
1986 	runtime->oss.maxfrags = (val >> 16) & 0xffff;
1987 	if (runtime->oss.fragshift < 4)		/* < 16 */
1988 		runtime->oss.fragshift = 4;
1989 	if (runtime->oss.maxfrags < 2)
1990 		runtime->oss.maxfrags = 2;
1991 	runtime->oss.params = 1;
1992 	return 0;
1993 }
1994 
1995 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1996 {
1997 	int err = -EINVAL, idx;
1998 
1999 	for (idx = 1; idx >= 0; --idx) {
2000 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2001 		struct snd_pcm_runtime *runtime;
2002 
2003 		if (substream == NULL)
2004 			continue;
2005 		runtime = substream->runtime;
2006 		err = lock_params(runtime);
2007 		if (err < 0)
2008 			return err;
2009 		err = snd_pcm_oss_set_fragment1(substream, val);
2010 		unlock_params(runtime);
2011 		if (err < 0)
2012 			return err;
2013 	}
2014 	return err;
2015 }
2016 
2017 static int snd_pcm_oss_nonblock(struct file * file)
2018 {
2019 	guard(spinlock)(&file->f_lock);
2020 	file->f_flags |= O_NONBLOCK;
2021 	return 0;
2022 }
2023 
2024 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2025 {
2026 
2027 	if (substream == NULL) {
2028 		res &= ~DSP_CAP_DUPLEX;
2029 		return res;
2030 	}
2031 #ifdef DSP_CAP_MULTI
2032 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2033 		if (substream->pstr->substream_count > 1)
2034 			res |= DSP_CAP_MULTI;
2035 #endif
2036 	/* DSP_CAP_REALTIME is set all times: */
2037 	/* all ALSA drivers can return actual pointer in ring buffer */
2038 #if defined(DSP_CAP_REALTIME) && 0
2039 	{
2040 		struct snd_pcm_runtime *runtime = substream->runtime;
2041 		if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2042 			res &= ~DSP_CAP_REALTIME;
2043 	}
2044 #endif
2045 	return res;
2046 }
2047 
2048 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2049 {
2050 	int result, idx;
2051 
2052 	result = DSP_CAP_TRIGGER | DSP_CAP_MMAP	| DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2053 	for (idx = 0; idx < 2; idx++) {
2054 		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2055 		result = snd_pcm_oss_get_caps1(substream, result);
2056 	}
2057 	result |= 0x0001;	/* revision - same as SB AWE 64 */
2058 	return result;
2059 }
2060 
2061 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2062 				      snd_pcm_uframes_t hw_ptr)
2063 {
2064 	struct snd_pcm_runtime *runtime = substream->runtime;
2065 	snd_pcm_uframes_t appl_ptr;
2066 	appl_ptr = hw_ptr + runtime->buffer_size;
2067 	appl_ptr %= runtime->boundary;
2068 	runtime->control->appl_ptr = appl_ptr;
2069 }
2070 
2071 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2072 {
2073 	struct snd_pcm_runtime *runtime;
2074 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2075 	int err, cmd;
2076 
2077 #ifdef OSS_DEBUG
2078 	pr_debug("pcm_oss: trigger = 0x%x\n", trigger);
2079 #endif
2080 
2081 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2082 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2083 
2084 	if (psubstream) {
2085 		err = snd_pcm_oss_make_ready(psubstream);
2086 		if (err < 0)
2087 			return err;
2088 	}
2089 	if (csubstream) {
2090 		err = snd_pcm_oss_make_ready(csubstream);
2091 		if (err < 0)
2092 			return err;
2093 	}
2094       	if (psubstream) {
2095       		runtime = psubstream->runtime;
2096 		cmd = 0;
2097 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2098 			return -ERESTARTSYS;
2099 		if (trigger & PCM_ENABLE_OUTPUT) {
2100 			if (runtime->oss.trigger)
2101 				goto _skip1;
2102 			if (atomic_read(&psubstream->mmap_count))
2103 				snd_pcm_oss_simulate_fill(psubstream,
2104 						get_hw_ptr_period(runtime));
2105 			runtime->oss.trigger = 1;
2106 			runtime->start_threshold = 1;
2107 			cmd = SNDRV_PCM_IOCTL_START;
2108 		} else {
2109 			if (!runtime->oss.trigger)
2110 				goto _skip1;
2111 			runtime->oss.trigger = 0;
2112 			runtime->start_threshold = runtime->boundary;
2113 			cmd = SNDRV_PCM_IOCTL_DROP;
2114 			runtime->oss.prepare = 1;
2115 		}
2116  _skip1:
2117 		mutex_unlock(&runtime->oss.params_lock);
2118 		if (cmd) {
2119 			err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2120 			if (err < 0)
2121 				return err;
2122 		}
2123 	}
2124 	if (csubstream) {
2125       		runtime = csubstream->runtime;
2126 		cmd = 0;
2127 		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2128 			return -ERESTARTSYS;
2129 		if (trigger & PCM_ENABLE_INPUT) {
2130 			if (runtime->oss.trigger)
2131 				goto _skip2;
2132 			runtime->oss.trigger = 1;
2133 			runtime->start_threshold = 1;
2134 			cmd = SNDRV_PCM_IOCTL_START;
2135 		} else {
2136 			if (!runtime->oss.trigger)
2137 				goto _skip2;
2138 			runtime->oss.trigger = 0;
2139 			runtime->start_threshold = runtime->boundary;
2140 			cmd = SNDRV_PCM_IOCTL_DROP;
2141 			runtime->oss.prepare = 1;
2142 		}
2143  _skip2:
2144 		mutex_unlock(&runtime->oss.params_lock);
2145 		if (cmd) {
2146 			err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2147 			if (err < 0)
2148 				return err;
2149 		}
2150 	}
2151 	return 0;
2152 }
2153 
2154 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2155 {
2156 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2157 	int result = 0;
2158 
2159 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2160 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2161 	if (psubstream && psubstream->runtime) {
2162 		guard(mutex)(&psubstream->runtime->oss.params_lock);
2163 		if (psubstream->runtime->oss.trigger)
2164 			result |= PCM_ENABLE_OUTPUT;
2165 	}
2166 	if (csubstream && csubstream->runtime) {
2167 		guard(mutex)(&csubstream->runtime->oss.params_lock);
2168 		if (csubstream->runtime->oss.trigger)
2169 			result |= PCM_ENABLE_INPUT;
2170 	}
2171 	return result;
2172 }
2173 
2174 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2175 {
2176 	struct snd_pcm_substream *substream;
2177 	struct snd_pcm_runtime *runtime;
2178 	snd_pcm_sframes_t delay;
2179 	int err;
2180 
2181 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2182 	if (substream == NULL)
2183 		return -EINVAL;
2184 	err = snd_pcm_oss_make_ready(substream);
2185 	if (err < 0)
2186 		return err;
2187 	runtime = substream->runtime;
2188 	if (runtime->oss.params || runtime->oss.prepare)
2189 		return 0;
2190 	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2191 	if (err == -EPIPE)
2192 		delay = 0;	/* hack for broken OSS applications */
2193 	else if (err < 0)
2194 		return err;
2195 	return snd_pcm_oss_bytes(substream, delay);
2196 }
2197 
2198 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2199 {
2200 	struct snd_pcm_substream *substream;
2201 	struct snd_pcm_runtime *runtime;
2202 	snd_pcm_sframes_t delay;
2203 	int fixup;
2204 	struct count_info info;
2205 	int err;
2206 
2207 	if (_info == NULL)
2208 		return -EFAULT;
2209 	substream = pcm_oss_file->streams[stream];
2210 	if (substream == NULL)
2211 		return -EINVAL;
2212 	err = snd_pcm_oss_make_ready(substream);
2213 	if (err < 0)
2214 		return err;
2215 	runtime = substream->runtime;
2216 	if (runtime->oss.params || runtime->oss.prepare) {
2217 		memset(&info, 0, sizeof(info));
2218 		if (copy_to_user(_info, &info, sizeof(info)))
2219 			return -EFAULT;
2220 		return 0;
2221 	}
2222 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2223 		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2224 		if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2225 			err = 0;
2226 			delay = 0;
2227 			fixup = 0;
2228 		} else {
2229 			fixup = runtime->oss.buffer_used;
2230 		}
2231 	} else {
2232 		err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2233 		fixup = -runtime->oss.buffer_used;
2234 	}
2235 	if (err < 0)
2236 		return err;
2237 	info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2238 	if (atomic_read(&substream->mmap_count)) {
2239 		snd_pcm_sframes_t n;
2240 		delay = get_hw_ptr_period(runtime);
2241 		n = delay - runtime->oss.prev_hw_ptr_period;
2242 		if (n < 0)
2243 			n += runtime->boundary;
2244 		info.blocks = n / runtime->period_size;
2245 		runtime->oss.prev_hw_ptr_period = delay;
2246 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2247 			snd_pcm_oss_simulate_fill(substream, delay);
2248 		info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2249 	} else {
2250 		delay = snd_pcm_oss_bytes(substream, delay);
2251 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2252 			if (substream->oss.setup.buggyptr)
2253 				info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2254 			else
2255 				info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2256 			info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2257 		} else {
2258 			delay += fixup;
2259 			info.blocks = delay / runtime->oss.period_bytes;
2260 			info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2261 		}
2262 	}
2263 	if (copy_to_user(_info, &info, sizeof(info)))
2264 		return -EFAULT;
2265 	return 0;
2266 }
2267 
2268 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2269 {
2270 	struct snd_pcm_substream *substream;
2271 	struct snd_pcm_runtime *runtime;
2272 	snd_pcm_sframes_t avail;
2273 	int fixup;
2274 	struct audio_buf_info info;
2275 	int err;
2276 
2277 	if (_info == NULL)
2278 		return -EFAULT;
2279 	substream = pcm_oss_file->streams[stream];
2280 	if (substream == NULL)
2281 		return -EINVAL;
2282 	runtime = substream->runtime;
2283 
2284 	if (runtime->oss.params) {
2285 		err = snd_pcm_oss_change_params(substream, false);
2286 		if (err < 0)
2287 			return err;
2288 	}
2289 
2290 	info.fragsize = runtime->oss.period_bytes;
2291 	info.fragstotal = runtime->periods;
2292 	if (runtime->oss.prepare) {
2293 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2294 			info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2295 			info.fragments = runtime->oss.periods;
2296 		} else {
2297 			info.bytes = 0;
2298 			info.fragments = 0;
2299 		}
2300 	} else {
2301 		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2302 			err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2303 			if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2304 				avail = runtime->buffer_size;
2305 				err = 0;
2306 				fixup = 0;
2307 			} else {
2308 				avail = runtime->buffer_size - avail;
2309 				fixup = -runtime->oss.buffer_used;
2310 			}
2311 		} else {
2312 			err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2313 			fixup = runtime->oss.buffer_used;
2314 		}
2315 		if (err < 0)
2316 			return err;
2317 		info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2318 		info.fragments = info.bytes / runtime->oss.period_bytes;
2319 	}
2320 
2321 #ifdef OSS_DEBUG
2322 	pcm_dbg(substream->pcm,
2323 		"pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2324 		info.bytes, info.fragments, info.fragstotal, info.fragsize);
2325 #endif
2326 	if (copy_to_user(_info, &info, sizeof(info)))
2327 		return -EFAULT;
2328 	return 0;
2329 }
2330 
2331 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2332 {
2333 	// it won't be probably implemented
2334 	// pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2335 	return -EINVAL;
2336 }
2337 
2338 static const char *strip_task_path(const char *path)
2339 {
2340 	const char *ptr, *ptrl = NULL;
2341 	for (ptr = path; *ptr; ptr++) {
2342 		if (*ptr == '/')
2343 			ptrl = ptr + 1;
2344 	}
2345 	return ptrl;
2346 }
2347 
2348 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2349 				      const char *task_name,
2350 				      struct snd_pcm_oss_setup *rsetup)
2351 {
2352 	struct snd_pcm_oss_setup *setup;
2353 
2354 	guard(mutex)(&pcm->streams[stream].oss.setup_mutex);
2355 	do {
2356 		for (setup = pcm->streams[stream].oss.setup_list; setup;
2357 		     setup = setup->next) {
2358 			if (!strcmp(setup->task_name, task_name))
2359 				goto out;
2360 		}
2361 	} while ((task_name = strip_task_path(task_name)) != NULL);
2362  out:
2363 	if (setup)
2364 		*rsetup = *setup;
2365 }
2366 
2367 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2368 {
2369 	snd_pcm_oss_release_buffers(substream);
2370 	substream->oss.oss = 0;
2371 }
2372 
2373 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2374 				       struct snd_pcm_oss_setup *setup,
2375 				       int minor)
2376 {
2377 	struct snd_pcm_runtime *runtime;
2378 
2379 	substream->oss.oss = 1;
2380 	substream->oss.setup = *setup;
2381 	if (setup->nonblock)
2382 		substream->f_flags |= O_NONBLOCK;
2383 	else if (setup->block)
2384 		substream->f_flags &= ~O_NONBLOCK;
2385 	runtime = substream->runtime;
2386 	runtime->oss.params = 1;
2387 	runtime->oss.trigger = 1;
2388 	runtime->oss.rate = 8000;
2389 	mutex_init(&runtime->oss.params_lock);
2390 	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2391 	case SNDRV_MINOR_OSS_PCM_8:
2392 		runtime->oss.format = AFMT_U8;
2393 		break;
2394 	case SNDRV_MINOR_OSS_PCM_16:
2395 		runtime->oss.format = AFMT_S16_LE;
2396 		break;
2397 	default:
2398 		runtime->oss.format = AFMT_MU_LAW;
2399 	}
2400 	runtime->oss.channels = 1;
2401 	runtime->oss.fragshift = 0;
2402 	runtime->oss.maxfrags = 0;
2403 	runtime->oss.subdivision = 0;
2404 	substream->pcm_release = snd_pcm_oss_release_substream;
2405 	atomic_set(&runtime->oss.rw_ref, 0);
2406 }
2407 
2408 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2409 {
2410 	int cidx;
2411 	if (!pcm_oss_file)
2412 		return 0;
2413 	for (cidx = 0; cidx < 2; ++cidx) {
2414 		struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2415 		if (substream)
2416 			snd_pcm_release_substream(substream);
2417 	}
2418 	kfree(pcm_oss_file);
2419 	return 0;
2420 }
2421 
2422 static int snd_pcm_oss_open_file(struct file *file,
2423 				 struct snd_pcm *pcm,
2424 				 struct snd_pcm_oss_file **rpcm_oss_file,
2425 				 int minor,
2426 				 struct snd_pcm_oss_setup *setup)
2427 {
2428 	int idx, err;
2429 	struct snd_pcm_oss_file *pcm_oss_file;
2430 	struct snd_pcm_substream *substream;
2431 	fmode_t f_mode = file->f_mode;
2432 
2433 	if (rpcm_oss_file)
2434 		*rpcm_oss_file = NULL;
2435 
2436 	pcm_oss_file = kzalloc_obj(*pcm_oss_file);
2437 	if (pcm_oss_file == NULL)
2438 		return -ENOMEM;
2439 
2440 	if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2441 	    (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2442 		f_mode = FMODE_WRITE;
2443 
2444 	file->f_flags &= ~O_APPEND;
2445 	for (idx = 0; idx < 2; idx++) {
2446 		if (setup[idx].disable)
2447 			continue;
2448 		if (! pcm->streams[idx].substream_count)
2449 			continue; /* no matching substream */
2450 		if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2451 			if (! (f_mode & FMODE_WRITE))
2452 				continue;
2453 		} else {
2454 			if (! (f_mode & FMODE_READ))
2455 				continue;
2456 		}
2457 		err = snd_pcm_open_substream(pcm, idx, file, &substream);
2458 		if (err < 0) {
2459 			snd_pcm_oss_release_file(pcm_oss_file);
2460 			return err;
2461 		}
2462 
2463 		pcm_oss_file->streams[idx] = substream;
2464 		snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2465 	}
2466 
2467 	if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2468 		snd_pcm_oss_release_file(pcm_oss_file);
2469 		return -EINVAL;
2470 	}
2471 
2472 	file->private_data = pcm_oss_file;
2473 	if (rpcm_oss_file)
2474 		*rpcm_oss_file = pcm_oss_file;
2475 	return 0;
2476 }
2477 
2478 
2479 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2480 {
2481 	unsigned int idx;
2482 
2483 	if (snd_BUG_ON(!task || !name || size < 2))
2484 		return -EINVAL;
2485 	for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2486 		name[idx] = task->comm[idx];
2487 	name[idx] = '\0';
2488 	return 0;
2489 }
2490 
2491 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2492 {
2493 	int err;
2494 	char task_name[32];
2495 	struct snd_pcm *pcm;
2496 	struct snd_pcm_oss_file *pcm_oss_file;
2497 	struct snd_pcm_oss_setup setup[2];
2498 	int nonblock;
2499 	wait_queue_entry_t wait;
2500 
2501 	nonseekable_open(inode, file);
2502 
2503 	pcm = snd_lookup_oss_minor_data(iminor(inode),
2504 					SNDRV_OSS_DEVICE_TYPE_PCM);
2505 	if (pcm == NULL) {
2506 		err = -ENODEV;
2507 		goto __error1;
2508 	}
2509 	err = snd_card_file_add(pcm->card, file);
2510 	if (err < 0)
2511 		goto __error1;
2512 	if (!try_module_get(pcm->card->module)) {
2513 		err = -ENODEV;
2514 		goto __error2;
2515 	}
2516 	if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2517 		err = -EFAULT;
2518 		goto __error;
2519 	}
2520 	memset(setup, 0, sizeof(setup));
2521 	if (file->f_mode & FMODE_WRITE)
2522 		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2523 					   task_name, &setup[0]);
2524 	if (file->f_mode & FMODE_READ)
2525 		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2526 					   task_name, &setup[1]);
2527 
2528 	nonblock = !!(file->f_flags & O_NONBLOCK);
2529 	if (!nonblock)
2530 		nonblock = nonblock_open;
2531 
2532 	init_waitqueue_entry(&wait, current);
2533 	add_wait_queue(&pcm->open_wait, &wait);
2534 	mutex_lock(&pcm->open_mutex);
2535 	while (1) {
2536 		err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2537 					    iminor(inode), setup);
2538 		if (err >= 0)
2539 			break;
2540 		if (err == -EAGAIN) {
2541 			if (nonblock) {
2542 				err = -EBUSY;
2543 				break;
2544 			}
2545 		} else
2546 			break;
2547 		set_current_state(TASK_INTERRUPTIBLE);
2548 		mutex_unlock(&pcm->open_mutex);
2549 		schedule();
2550 		mutex_lock(&pcm->open_mutex);
2551 		if (pcm->card->shutdown) {
2552 			err = -ENODEV;
2553 			break;
2554 		}
2555 		if (signal_pending(current)) {
2556 			err = -ERESTARTSYS;
2557 			break;
2558 		}
2559 	}
2560 	remove_wait_queue(&pcm->open_wait, &wait);
2561 	mutex_unlock(&pcm->open_mutex);
2562 	if (err < 0)
2563 		goto __error;
2564 	snd_card_unref(pcm->card);
2565 	return err;
2566 
2567       __error:
2568      	module_put(pcm->card->module);
2569       __error2:
2570       	snd_card_file_remove(pcm->card, file);
2571       __error1:
2572 	if (pcm)
2573 		snd_card_unref(pcm->card);
2574 	return err;
2575 }
2576 
2577 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2578 {
2579 	struct snd_pcm *pcm;
2580 	struct snd_pcm_substream *substream;
2581 	struct snd_pcm_oss_file *pcm_oss_file;
2582 
2583 	pcm_oss_file = file->private_data;
2584 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2585 	if (substream == NULL)
2586 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2587 	if (snd_BUG_ON(!substream))
2588 		return -ENXIO;
2589 	pcm = substream->pcm;
2590 	if (!pcm->card->shutdown)
2591 		snd_pcm_oss_sync(pcm_oss_file);
2592 	mutex_lock(&pcm->open_mutex);
2593 	snd_pcm_oss_release_file(pcm_oss_file);
2594 	mutex_unlock(&pcm->open_mutex);
2595 	wake_up(&pcm->open_wait);
2596 	module_put(pcm->card->module);
2597 	snd_card_file_remove(pcm->card, file);
2598 	return 0;
2599 }
2600 
2601 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2602 {
2603 	struct snd_pcm_oss_file *pcm_oss_file;
2604 	int __user *p = (int __user *)arg;
2605 	int res;
2606 
2607 	pcm_oss_file = file->private_data;
2608 	if (cmd == OSS_GETVERSION)
2609 		return put_user(SNDRV_OSS_VERSION, p);
2610 	if (cmd == OSS_ALSAEMULVER)
2611 		return put_user(1, p);
2612 #if IS_REACHABLE(CONFIG_SND_MIXER_OSS)
2613 	if (((cmd >> 8) & 0xff) == 'M')	{	/* mixer ioctl - for OSS compatibility */
2614 		struct snd_pcm_substream *substream;
2615 		int idx;
2616 		for (idx = 0; idx < 2; ++idx) {
2617 			substream = pcm_oss_file->streams[idx];
2618 			if (substream != NULL)
2619 				break;
2620 		}
2621 		if (snd_BUG_ON(idx >= 2))
2622 			return -ENXIO;
2623 		return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2624 	}
2625 #endif
2626 	if (((cmd >> 8) & 0xff) != 'P')
2627 		return -EINVAL;
2628 #ifdef OSS_DEBUG
2629 	pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2630 #endif
2631 	switch (cmd) {
2632 	case SNDCTL_DSP_RESET:
2633 		return snd_pcm_oss_reset(pcm_oss_file);
2634 	case SNDCTL_DSP_SYNC:
2635 		return snd_pcm_oss_sync(pcm_oss_file);
2636 	case SNDCTL_DSP_SPEED:
2637 		if (get_user(res, p))
2638 			return -EFAULT;
2639 		res = snd_pcm_oss_set_rate(pcm_oss_file, res);
2640 		if (res < 0)
2641 			return res;
2642 		return put_user(res, p);
2643 	case SOUND_PCM_READ_RATE:
2644 		res = snd_pcm_oss_get_rate(pcm_oss_file);
2645 		if (res < 0)
2646 			return res;
2647 		return put_user(res, p);
2648 	case SNDCTL_DSP_STEREO:
2649 		if (get_user(res, p))
2650 			return -EFAULT;
2651 		res = res > 0 ? 2 : 1;
2652 		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2653 		if (res < 0)
2654 			return res;
2655 		return put_user(--res, p);
2656 	case SNDCTL_DSP_GETBLKSIZE:
2657 		res = snd_pcm_oss_get_block_size(pcm_oss_file);
2658 		if (res < 0)
2659 			return res;
2660 		return put_user(res, p);
2661 	case SNDCTL_DSP_SETFMT:
2662 		if (get_user(res, p))
2663 			return -EFAULT;
2664 		res = snd_pcm_oss_set_format(pcm_oss_file, res);
2665 		if (res < 0)
2666 			return res;
2667 		return put_user(res, p);
2668 	case SOUND_PCM_READ_BITS:
2669 		res = snd_pcm_oss_get_format(pcm_oss_file);
2670 		if (res < 0)
2671 			return res;
2672 		return put_user(res, p);
2673 	case SNDCTL_DSP_CHANNELS:
2674 		if (get_user(res, p))
2675 			return -EFAULT;
2676 		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2677 		if (res < 0)
2678 			return res;
2679 		return put_user(res, p);
2680 	case SOUND_PCM_READ_CHANNELS:
2681 		res = snd_pcm_oss_get_channels(pcm_oss_file);
2682 		if (res < 0)
2683 			return res;
2684 		return put_user(res, p);
2685 	case SOUND_PCM_WRITE_FILTER:
2686 	case SOUND_PCM_READ_FILTER:
2687 		return -EIO;
2688 	case SNDCTL_DSP_POST:
2689 		return snd_pcm_oss_post(pcm_oss_file);
2690 	case SNDCTL_DSP_SUBDIVIDE:
2691 		if (get_user(res, p))
2692 			return -EFAULT;
2693 		res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2694 		if (res < 0)
2695 			return res;
2696 		return put_user(res, p);
2697 	case SNDCTL_DSP_SETFRAGMENT:
2698 		if (get_user(res, p))
2699 			return -EFAULT;
2700 		return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2701 	case SNDCTL_DSP_GETFMTS:
2702 		res = snd_pcm_oss_get_formats(pcm_oss_file);
2703 		if (res < 0)
2704 			return res;
2705 		return put_user(res, p);
2706 	case SNDCTL_DSP_GETOSPACE:
2707 	case SNDCTL_DSP_GETISPACE:
2708 		return snd_pcm_oss_get_space(pcm_oss_file,
2709 			cmd == SNDCTL_DSP_GETISPACE ?
2710 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2711 			(struct audio_buf_info __user *) arg);
2712 	case SNDCTL_DSP_NONBLOCK:
2713 		return snd_pcm_oss_nonblock(file);
2714 	case SNDCTL_DSP_GETCAPS:
2715 		res = snd_pcm_oss_get_caps(pcm_oss_file);
2716 		if (res < 0)
2717 			return res;
2718 		return put_user(res, p);
2719 	case SNDCTL_DSP_GETTRIGGER:
2720 		res = snd_pcm_oss_get_trigger(pcm_oss_file);
2721 		if (res < 0)
2722 			return res;
2723 		return put_user(res, p);
2724 	case SNDCTL_DSP_SETTRIGGER:
2725 		if (get_user(res, p))
2726 			return -EFAULT;
2727 		return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2728 	case SNDCTL_DSP_GETIPTR:
2729 	case SNDCTL_DSP_GETOPTR:
2730 		return snd_pcm_oss_get_ptr(pcm_oss_file,
2731 			cmd == SNDCTL_DSP_GETIPTR ?
2732 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2733 			(struct count_info __user *) arg);
2734 	case SNDCTL_DSP_MAPINBUF:
2735 	case SNDCTL_DSP_MAPOUTBUF:
2736 		return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2737 			cmd == SNDCTL_DSP_MAPINBUF ?
2738 				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2739 			(struct buffmem_desc __user *) arg);
2740 	case SNDCTL_DSP_SETSYNCRO:
2741 		/* stop DMA now.. */
2742 		return 0;
2743 	case SNDCTL_DSP_SETDUPLEX:
2744 		if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2745 			return 0;
2746 		return -EIO;
2747 	case SNDCTL_DSP_GETODELAY:
2748 		res = snd_pcm_oss_get_odelay(pcm_oss_file);
2749 		if (res < 0) {
2750 			/* it's for sure, some broken apps don't check for error codes */
2751 			put_user(0, p);
2752 			return res;
2753 		}
2754 		return put_user(res, p);
2755 	case SNDCTL_DSP_PROFILE:
2756 		return 0;	/* silently ignore */
2757 	default:
2758 		pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2759 	}
2760 	return -EINVAL;
2761 }
2762 
2763 #ifdef CONFIG_COMPAT
2764 /* all compatible */
2765 static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
2766 				     unsigned long arg)
2767 {
2768 	/*
2769 	 * Everything is compatbile except SNDCTL_DSP_MAPINBUF/SNDCTL_DSP_MAPOUTBUF,
2770 	 * which are not implemented for the native case either
2771 	 */
2772 	return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2773 }
2774 #else
2775 #define snd_pcm_oss_ioctl_compat	NULL
2776 #endif
2777 
2778 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2779 {
2780 	struct snd_pcm_oss_file *pcm_oss_file;
2781 	struct snd_pcm_substream *substream;
2782 
2783 	pcm_oss_file = file->private_data;
2784 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2785 	if (substream == NULL)
2786 		return -ENXIO;
2787 	substream->f_flags = file->f_flags & O_NONBLOCK;
2788 #ifndef OSS_DEBUG
2789 	return snd_pcm_oss_read1(substream, buf, count);
2790 #else
2791 	{
2792 		ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2793 		pcm_dbg(substream->pcm,
2794 			"pcm_oss: read %li bytes (returned %li bytes)\n",
2795 			(long)count, (long)res);
2796 		return res;
2797 	}
2798 #endif
2799 }
2800 
2801 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2802 {
2803 	struct snd_pcm_oss_file *pcm_oss_file;
2804 	struct snd_pcm_substream *substream;
2805 	long result;
2806 
2807 	pcm_oss_file = file->private_data;
2808 	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2809 	if (substream == NULL)
2810 		return -ENXIO;
2811 	substream->f_flags = file->f_flags & O_NONBLOCK;
2812 	result = snd_pcm_oss_write1(substream, buf, count);
2813 #ifdef OSS_DEBUG
2814 	pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2815 	       (long)count, (long)result);
2816 #endif
2817 	return result;
2818 }
2819 
2820 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2821 {
2822 	struct snd_pcm_runtime *runtime = substream->runtime;
2823 	if (atomic_read(&substream->mmap_count))
2824 		return runtime->oss.prev_hw_ptr_period !=
2825 						get_hw_ptr_period(runtime);
2826 	else
2827 		return snd_pcm_playback_avail(runtime) >=
2828 						runtime->oss.period_frames;
2829 }
2830 
2831 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2832 {
2833 	struct snd_pcm_runtime *runtime = substream->runtime;
2834 	if (atomic_read(&substream->mmap_count))
2835 		return runtime->oss.prev_hw_ptr_period !=
2836 						get_hw_ptr_period(runtime);
2837 	else
2838 		return snd_pcm_capture_avail(runtime) >=
2839 						runtime->oss.period_frames;
2840 }
2841 
2842 static bool need_input_retrigger(struct snd_pcm_runtime *runtime)
2843 {
2844 	bool ret;
2845 
2846 	guard(mutex)(&runtime->oss.params_lock);
2847 	ret = runtime->oss.trigger;
2848 	if (ret)
2849 		runtime->oss.trigger = 0;
2850 	return ret;
2851 }
2852 
2853 static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait)
2854 {
2855 	struct snd_pcm_oss_file *pcm_oss_file;
2856 	__poll_t mask;
2857 	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2858 
2859 	pcm_oss_file = file->private_data;
2860 
2861 	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2862 	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2863 
2864 	mask = 0;
2865 	if (psubstream != NULL) {
2866 		struct snd_pcm_runtime *runtime = psubstream->runtime;
2867 		poll_wait(file, &runtime->sleep, wait);
2868 		scoped_guard(pcm_stream_lock_irq, psubstream) {
2869 			if (runtime->state != SNDRV_PCM_STATE_DRAINING &&
2870 			    (runtime->state != SNDRV_PCM_STATE_RUNNING ||
2871 			     snd_pcm_oss_playback_ready(psubstream)))
2872 				mask |= EPOLLOUT | EPOLLWRNORM;
2873 		}
2874 	}
2875 	if (csubstream != NULL) {
2876 		struct snd_pcm_runtime *runtime = csubstream->runtime;
2877 		snd_pcm_state_t ostate;
2878 		poll_wait(file, &runtime->sleep, wait);
2879 		scoped_guard(pcm_stream_lock_irq, csubstream) {
2880 			ostate = runtime->state;
2881 			if (ostate != SNDRV_PCM_STATE_RUNNING ||
2882 			    snd_pcm_oss_capture_ready(csubstream))
2883 				mask |= EPOLLIN | EPOLLRDNORM;
2884 		}
2885 		if (ostate != SNDRV_PCM_STATE_RUNNING &&
2886 		    need_input_retrigger(runtime)) {
2887 			struct snd_pcm_oss_file ofile;
2888 			memset(&ofile, 0, sizeof(ofile));
2889 			ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2890 			snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2891 		}
2892 	}
2893 
2894 	return mask;
2895 }
2896 
2897 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2898 {
2899 	struct snd_pcm_oss_file *pcm_oss_file;
2900 	struct snd_pcm_substream *substream = NULL;
2901 	struct snd_pcm_runtime *runtime;
2902 	int err;
2903 
2904 #ifdef OSS_DEBUG
2905 	pr_debug("pcm_oss: mmap begin\n");
2906 #endif
2907 	pcm_oss_file = file->private_data;
2908 	switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2909 	case VM_READ | VM_WRITE:
2910 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2911 		if (substream)
2912 			break;
2913 		fallthrough;
2914 	case VM_READ:
2915 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2916 		break;
2917 	case VM_WRITE:
2918 		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2919 		break;
2920 	default:
2921 		return -EINVAL;
2922 	}
2923 	/* set VM_READ access as well to fix memset() routines that do
2924 	   reads before writes (to improve performance) */
2925 	vm_flags_set(area, VM_READ);
2926 	if (substream == NULL)
2927 		return -ENXIO;
2928 	runtime = substream->runtime;
2929 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2930 		return -EIO;
2931 	if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2932 		runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2933 	else
2934 		return -EIO;
2935 
2936 	if (runtime->oss.params) {
2937 		/* use mutex_trylock() for params_lock for avoiding a deadlock
2938 		 * between mmap_lock and params_lock taken by
2939 		 * copy_from/to_user() in snd_pcm_oss_write/read()
2940 		 */
2941 		err = snd_pcm_oss_change_params(substream, true);
2942 		if (err < 0)
2943 			return err;
2944 	}
2945 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2946 	if (runtime->oss.plugin_first != NULL)
2947 		return -EIO;
2948 #endif
2949 
2950 	if (area->vm_pgoff != 0)
2951 		return -EINVAL;
2952 
2953 	err = snd_pcm_mmap_data(substream, file, area);
2954 	if (err < 0)
2955 		return err;
2956 	runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2957 	runtime->silence_threshold = 0;
2958 	runtime->silence_size = 0;
2959 #ifdef OSS_DEBUG
2960 	pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2961 	       runtime->oss.mmap_bytes);
2962 #endif
2963 	/* In mmap mode we never stop */
2964 	runtime->stop_threshold = runtime->boundary;
2965 
2966 	return 0;
2967 }
2968 
2969 #ifdef CONFIG_SND_VERBOSE_PROCFS
2970 /*
2971  *  /proc interface
2972  */
2973 
2974 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2975 				  struct snd_info_buffer *buffer)
2976 {
2977 	struct snd_pcm_str *pstr = entry->private_data;
2978 	struct snd_pcm_oss_setup *setup;
2979 
2980 	guard(mutex)(&pstr->oss.setup_mutex);
2981 	setup = pstr->oss.setup_list;
2982 	while (setup) {
2983 		snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2984 			    setup->task_name,
2985 			    setup->periods,
2986 			    setup->period_size,
2987 			    setup->disable ? " disable" : "",
2988 			    setup->direct ? " direct" : "",
2989 			    setup->block ? " block" : "",
2990 			    setup->nonblock ? " non-block" : "",
2991 			    setup->partialfrag ? " partial-frag" : "",
2992 			    setup->nosilence ? " no-silence" : "");
2993 		setup = setup->next;
2994 	}
2995 }
2996 
2997 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2998 {
2999 	struct snd_pcm_oss_setup *setup, *setupn;
3000 
3001 	for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
3002 	     setup; setup = setupn) {
3003 		setupn = setup->next;
3004 		kfree(setup->task_name);
3005 		kfree(setup);
3006 	}
3007 	pstr->oss.setup_list = NULL;
3008 }
3009 
3010 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
3011 				   struct snd_info_buffer *buffer)
3012 {
3013 	struct snd_pcm_str *pstr = entry->private_data;
3014 	char line[128], str[32], task_name[32];
3015 	const char *ptr;
3016 	int idx1;
3017 	struct snd_pcm_oss_setup *setup, *setup1, template;
3018 
3019 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
3020 		guard(mutex)(&pstr->oss.setup_mutex);
3021 		memset(&template, 0, sizeof(template));
3022 		ptr = snd_info_get_str(task_name, line, sizeof(task_name));
3023 		if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
3024 			snd_pcm_oss_proc_free_setup_list(pstr);
3025 			continue;
3026 		}
3027 		for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
3028 			if (!strcmp(setup->task_name, task_name)) {
3029 				template = *setup;
3030 				break;
3031 			}
3032 		}
3033 		ptr = snd_info_get_str(str, ptr, sizeof(str));
3034 		template.periods = simple_strtoul(str, NULL, 10);
3035 		ptr = snd_info_get_str(str, ptr, sizeof(str));
3036 		template.period_size = simple_strtoul(str, NULL, 10);
3037 		for (idx1 = 31; idx1 >= 0; idx1--)
3038 			if (template.period_size & (1 << idx1))
3039 				break;
3040 		for (idx1--; idx1 >= 0; idx1--)
3041 			template.period_size &= ~(1 << idx1);
3042 		do {
3043 			ptr = snd_info_get_str(str, ptr, sizeof(str));
3044 			if (!strcmp(str, "disable")) {
3045 				template.disable = 1;
3046 			} else if (!strcmp(str, "direct")) {
3047 				template.direct = 1;
3048 			} else if (!strcmp(str, "block")) {
3049 				template.block = 1;
3050 			} else if (!strcmp(str, "non-block")) {
3051 				template.nonblock = 1;
3052 			} else if (!strcmp(str, "partial-frag")) {
3053 				template.partialfrag = 1;
3054 			} else if (!strcmp(str, "no-silence")) {
3055 				template.nosilence = 1;
3056 			} else if (!strcmp(str, "buggy-ptr")) {
3057 				template.buggyptr = 1;
3058 			}
3059 		} while (*str);
3060 		if (setup == NULL) {
3061 			setup = kmalloc_obj(*setup);
3062 			if (! setup) {
3063 				buffer->error = -ENOMEM;
3064 				return;
3065 			}
3066 			template.task_name = kstrdup(task_name, GFP_KERNEL);
3067 			if (!template.task_name) {
3068 				kfree(setup);
3069 				buffer->error = -ENOMEM;
3070 				return;
3071 			}
3072 			*setup = template;
3073 			if (pstr->oss.setup_list == NULL)
3074 				pstr->oss.setup_list = setup;
3075 			else {
3076 				for (setup1 = pstr->oss.setup_list;
3077 				     setup1->next; setup1 = setup1->next);
3078 				setup1->next = setup;
3079 			}
3080 			continue;
3081 		}
3082 		*setup = template;
3083 	}
3084 }
3085 
3086 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3087 {
3088 	int stream;
3089 	for (stream = 0; stream < 2; ++stream) {
3090 		struct snd_info_entry *entry;
3091 		struct snd_pcm_str *pstr = &pcm->streams[stream];
3092 		if (pstr->substream_count == 0)
3093 			continue;
3094 		entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root);
3095 		if (entry) {
3096 			entry->content = SNDRV_INFO_CONTENT_TEXT;
3097 			entry->mode = S_IFREG | 0644;
3098 			entry->c.text.read = snd_pcm_oss_proc_read;
3099 			entry->c.text.write = snd_pcm_oss_proc_write;
3100 			entry->private_data = pstr;
3101 			if (snd_info_register(entry) < 0) {
3102 				snd_info_free_entry(entry);
3103 				entry = NULL;
3104 			}
3105 		}
3106 		pstr->oss.proc_entry = entry;
3107 	}
3108 }
3109 
3110 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3111 {
3112 	int stream;
3113 	for (stream = 0; stream < 2; ++stream) {
3114 		struct snd_pcm_str *pstr = &pcm->streams[stream];
3115 		snd_info_free_entry(pstr->oss.proc_entry);
3116 		pstr->oss.proc_entry = NULL;
3117 		snd_pcm_oss_proc_free_setup_list(pstr);
3118 	}
3119 }
3120 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3121 static inline void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3122 {
3123 }
3124 static inline void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3125 {
3126 }
3127 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3128 
3129 /*
3130  *  ENTRY functions
3131  */
3132 
3133 static const struct file_operations snd_pcm_oss_f_reg = {
3134 	.owner		=	THIS_MODULE,
3135 	.read		=	snd_pcm_oss_read,
3136 	.write		=	snd_pcm_oss_write,
3137 	.open		=	snd_pcm_oss_open,
3138 	.release	=	snd_pcm_oss_release,
3139 	.poll		=	snd_pcm_oss_poll,
3140 	.unlocked_ioctl	=	snd_pcm_oss_ioctl,
3141 	.compat_ioctl	=	snd_pcm_oss_ioctl_compat,
3142 	.mmap		=	snd_pcm_oss_mmap,
3143 };
3144 
3145 static void register_oss_dsp(struct snd_pcm *pcm, int index)
3146 {
3147 	if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3148 				    pcm->card, index, &snd_pcm_oss_f_reg,
3149 				    pcm) < 0) {
3150 		pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3151 			   pcm->card->number, pcm->device);
3152 	}
3153 }
3154 
3155 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3156 {
3157 	if (pcm->oss.registered)
3158 		return 0;
3159 	if (!try_module_get(THIS_MODULE))
3160 		return -ENODEV;
3161 	pcm->oss.registered = true;
3162 
3163 	pcm->oss.reg = 0;
3164 	if (dsp_map[pcm->card->number] == (int)pcm->device) {
3165 		char name[128];
3166 		int duplex;
3167 		register_oss_dsp(pcm, 0);
3168 		duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3169 			      pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3170 			      !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3171 		sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3172 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3173 		snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3174 				      pcm->card->number,
3175 				      name);
3176 #endif
3177 		pcm->oss.reg++;
3178 		pcm->oss.reg_mask |= 1;
3179 	}
3180 	if (adsp_map[pcm->card->number] == (int)pcm->device) {
3181 		register_oss_dsp(pcm, 1);
3182 		pcm->oss.reg++;
3183 		pcm->oss.reg_mask |= 2;
3184 	}
3185 
3186 	if (pcm->oss.reg)
3187 		snd_pcm_oss_proc_init(pcm);
3188 
3189 	return 0;
3190 }
3191 
3192 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3193 {
3194 	if (pcm->oss.reg) {
3195 		if (pcm->oss.reg_mask & 1) {
3196 			pcm->oss.reg_mask &= ~1;
3197 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3198 						  pcm->card, 0);
3199 		}
3200 		if (pcm->oss.reg_mask & 2) {
3201 			pcm->oss.reg_mask &= ~2;
3202 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3203 						  pcm->card, 1);
3204 		}
3205 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
3206 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3207 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3208 #endif
3209 		}
3210 		pcm->oss.reg = 0;
3211 	}
3212 	return 0;
3213 }
3214 
3215 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3216 {
3217 	/* BUILD-state and creation-error cleanup need not have registered us. */
3218 	if (!pcm->oss.registered)
3219 		return 0;
3220 
3221 	snd_pcm_oss_disconnect_minor(pcm);
3222 	snd_pcm_oss_proc_done(pcm);
3223 	pcm->oss.registered = false;
3224 	module_put(THIS_MODULE);
3225 	return 0;
3226 }
3227 
3228 static struct snd_pcm_notify snd_pcm_oss_notify =
3229 {
3230 	.n_register =	snd_pcm_oss_register_minor,
3231 	.n_disconnect = snd_pcm_oss_disconnect_minor,
3232 	.n_unregister =	snd_pcm_oss_unregister_minor,
3233 };
3234 
3235 static int __init alsa_pcm_oss_init(void)
3236 {
3237 	int i;
3238 	int err;
3239 
3240 	/* check device map table */
3241 	for (i = 0; i < SNDRV_CARDS; i++) {
3242 		if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3243 			pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3244 				   i, dsp_map[i]);
3245 			dsp_map[i] = 0;
3246 		}
3247 		if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3248 			pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3249 				   i, adsp_map[i]);
3250 			adsp_map[i] = 1;
3251 		}
3252 	}
3253 	err = snd_pcm_notify(&snd_pcm_oss_notify, 0);
3254 	if (err < 0)
3255 		return err;
3256 	return 0;
3257 }
3258 
3259 static void __exit alsa_pcm_oss_exit(void)
3260 {
3261 	snd_pcm_notify(&snd_pcm_oss_notify, 1);
3262 }
3263 
3264 module_init(alsa_pcm_oss_init)
3265 module_exit(alsa_pcm_oss_exit)
3266