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