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