Debian Codegraph @

sound/core/oss/pcm_oss.c

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