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