2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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
29 #include <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/vmalloc.h>
35 #include <linux/moduleparam.h>
36 #include <linux/string.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>
46 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
48 static int dsp_map[SNDRV_CARDS];
49 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50 static int nonblock_open = 1;
52 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54 MODULE_LICENSE("GPL");
55 module_param_array(dsp_map, int, NULL, 0444);
56 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map, int, NULL, 0444);
58 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59 module_param(nonblock_open, bool, 0644);
60 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
64 extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
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();
76 static inline void snd_leave_user(mm_segment_t fs)
82 * helper functions to process hw_params
84 static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
91 } else if (i->min == min && !i->openmin && openmin) {
101 if (snd_interval_checkempty(i)) {
102 snd_interval_none(i);
108 static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
113 i->openmax = openmax;
115 } else if (i->max == max && !i->openmax && openmax) {
125 if (snd_interval_checkempty(i)) {
126 snd_interval_none(i);
132 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
134 struct snd_interval t;
137 t.openmin = t.openmax = 0;
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.
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)) {
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);
163 return snd_interval_min(i);
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.
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)) {
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);
188 *dir = - (int) i->openmax;
189 return snd_interval_max(i);
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)
199 changed = snd_mask_refine(hw_param_mask(params, var), val);
201 params->cmask |= 1 << var;
202 params->rmask |= 1 << var;
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);
216 int err = snd_pcm_hw_refine(pcm, params);
223 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
224 snd_pcm_hw_param_t var, unsigned int val,
232 } else if (dir < 0) {
240 changed = snd_mask_refine_min(hw_param_mask(params, var),
242 else if (hw_is_interval(var))
243 changed = snd_interval_refine_min(hw_param_interval(params, var),
248 params->cmask |= 1 << var;
249 params->rmask |= 1 << var;
255 * snd_pcm_hw_param_min
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,
271 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
275 int err = snd_pcm_hw_refine(pcm, params);
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,
291 } else if (dir > 0) {
296 if (hw_is_mask(var)) {
297 if (val == 0 && open) {
298 snd_mask_none(hw_param_mask(params, var));
301 changed = snd_mask_refine_max(hw_param_mask(params, var),
303 } else if (hw_is_interval(var))
304 changed = snd_interval_refine_max(hw_param_interval(params, var),
309 params->cmask |= 1 << var;
310 params->rmask |= 1 << var;
316 * snd_pcm_hw_param_max
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,
332 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
336 int err = snd_pcm_hw_refine(pcm, params);
340 return snd_pcm_hw_param_value_max(params, var, dir);
343 static int boundary_sub(int a, int adir,
347 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
348 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
353 } else if (*cdir == 2) {
359 static int boundary_lt(unsigned int a, int adir,
360 unsigned int b, int bdir)
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,
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
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,
406 struct snd_pcm_hw_params *save = NULL;
408 unsigned int saved_min;
412 int valdir = dir ? *dir : 0;
417 mindir = maxdir = valdir;
420 else if (maxdir == 0)
426 save = kmalloc(sizeof(*save), GFP_KERNEL);
431 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
433 struct snd_pcm_hw_params *params1;
436 if ((unsigned int)min == saved_min && mindir == valdir)
438 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
439 if (params1 == NULL) {
444 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
449 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
456 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
457 snd_assert(max >= 0, return -EINVAL);
463 v = snd_pcm_hw_param_last(pcm, params, var, dir);
465 v = snd_pcm_hw_param_first(pcm, params, var, dir);
466 snd_assert(v >= 0, return -EINVAL);
470 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
471 snd_pcm_hw_param_t var, unsigned int val,
475 if (hw_is_mask(var)) {
476 struct snd_mask *m = hw_param_mask(params, var);
477 if (val == 0 && dir < 0) {
485 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
487 } else if (hw_is_interval(var)) {
488 struct snd_interval *i = hw_param_interval(params, var);
489 if (val == 0 && dir < 0) {
491 snd_interval_none(i);
493 changed = snd_interval_refine_set(i, val);
495 struct snd_interval t;
507 changed = snd_interval_refine(i, &t);
512 params->cmask |= 1 << var;
513 params->rmask |= 1 << var;
519 * snd_pcm_hw_param_set
521 * @params: the hw_params instance
522 * @var: parameter to retrieve
524 * @dir: pointer to the direction (-1,0,1) or NULL
526 * Inside configuration space defined by PARAMS remove from PAR all
527 * values != VAL. Reduce configuration space accordingly.
528 * Return VAL or -EINVAL if the configuration space is empty
530 static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
531 struct snd_pcm_hw_params *params,
532 snd_pcm_hw_param_t var, unsigned int val,
535 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
539 int err = snd_pcm_hw_refine(pcm, params);
543 return snd_pcm_hw_param_value(params, var, NULL);
546 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
547 snd_pcm_hw_param_t var)
550 changed = snd_interval_setinteger(hw_param_interval(params, var));
552 params->cmask |= 1 << var;
553 params->rmask |= 1 << var;
562 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
563 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
565 struct snd_pcm_runtime *runtime = substream->runtime;
566 struct snd_pcm_plugin *plugin, *next;
568 plugin = runtime->oss.plugin_first;
571 snd_pcm_plugin_free(plugin);
574 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
578 static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
580 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
581 plugin->next = runtime->oss.plugin_first;
583 if (runtime->oss.plugin_first) {
584 runtime->oss.plugin_first->prev = plugin;
585 runtime->oss.plugin_first = plugin;
587 runtime->oss.plugin_last =
588 runtime->oss.plugin_first = plugin;
593 int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
595 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
597 plugin->prev = runtime->oss.plugin_last;
598 if (runtime->oss.plugin_last) {
599 runtime->oss.plugin_last->next = plugin;
600 runtime->oss.plugin_last = plugin;
602 runtime->oss.plugin_last =
603 runtime->oss.plugin_first = plugin;
607 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
609 static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
611 struct snd_pcm_runtime *runtime = substream->runtime;
612 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
613 long bytes = frames_to_bytes(runtime, frames);
614 if (buffer_size == runtime->oss.buffer_bytes)
616 #if BITS_PER_LONG >= 64
617 return runtime->oss.buffer_bytes * bytes / buffer_size;
620 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
622 div64_32(&bsize, buffer_size, &rem);
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 int snd_pcm_oss_format_from(int format)
640 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
641 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
642 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
643 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
644 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
645 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
646 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
647 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
648 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
649 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
650 default: return SNDRV_PCM_FORMAT_U8;
654 static int snd_pcm_oss_format_to(int format)
657 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
658 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
659 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
660 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
661 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
662 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
663 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
664 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
665 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
666 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
667 default: return -EINVAL;
671 static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
672 struct snd_pcm_hw_params *oss_params,
673 struct snd_pcm_hw_params *slave_params)
676 size_t oss_buffer_size, oss_period_size, oss_periods;
677 size_t min_period_size, max_period_size;
678 struct snd_pcm_runtime *runtime = substream->runtime;
679 size_t oss_frame_size;
681 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
682 params_channels(oss_params) / 8;
684 oss_buffer_size = snd_pcm_plug_client_size(substream,
685 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
686 oss_buffer_size = 1 << ld2(oss_buffer_size);
687 if (atomic_read(&substream->mmap_count)) {
688 if (oss_buffer_size > runtime->oss.mmap_bytes)
689 oss_buffer_size = runtime->oss.mmap_bytes;
692 if (substream->oss.setup.period_size > 16)
693 oss_period_size = substream->oss.setup.period_size;
694 else if (runtime->oss.fragshift) {
695 oss_period_size = 1 << runtime->oss.fragshift;
696 if (oss_period_size > oss_buffer_size / 2)
697 oss_period_size = oss_buffer_size / 2;
700 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
702 oss_period_size = oss_buffer_size;
704 oss_period_size /= 2;
705 } while (oss_period_size > bytes_per_sec);
706 if (runtime->oss.subdivision == 0) {
708 if (oss_period_size / sd > 4096)
710 if (oss_period_size / sd < 4096)
713 sd = runtime->oss.subdivision;
714 oss_period_size /= sd;
715 if (oss_period_size < 16)
716 oss_period_size = 16;
719 min_period_size = snd_pcm_plug_client_size(substream,
720 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
721 min_period_size *= oss_frame_size;
722 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
723 if (oss_period_size < min_period_size)
724 oss_period_size = min_period_size;
726 max_period_size = snd_pcm_plug_client_size(substream,
727 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
728 max_period_size *= oss_frame_size;
729 max_period_size = 1 << ld2(max_period_size);
730 if (oss_period_size > max_period_size)
731 oss_period_size = max_period_size;
733 oss_periods = oss_buffer_size / oss_period_size;
735 if (substream->oss.setup.periods > 1)
736 oss_periods = substream->oss.setup.periods;
738 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
739 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
740 s = runtime->oss.maxfrags;
744 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
750 while (oss_period_size * oss_periods > oss_buffer_size)
751 oss_period_size /= 2;
753 snd_assert(oss_period_size >= 16, return -EINVAL);
754 runtime->oss.period_bytes = oss_period_size;
755 runtime->oss.period_frames = 1;
756 runtime->oss.periods = oss_periods;
760 static int choose_rate(struct snd_pcm_substream *substream,
761 struct snd_pcm_hw_params *params, unsigned int best_rate)
763 struct snd_interval *it;
764 struct snd_pcm_hw_params *save;
765 unsigned int rate, prev;
767 save = kmalloc(sizeof(*save), GFP_KERNEL);
771 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
773 /* try multiples of the best rate */
776 if (it->max < rate || (it->max == rate && it->openmax))
778 if (it->min < rate || (it->min == rate && !it->openmin)) {
780 ret = snd_pcm_hw_param_set(substream, params,
781 SNDRV_PCM_HW_PARAM_RATE,
783 if (ret == (int)rate) {
795 /* not found, use the nearest rate */
797 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
800 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
802 struct snd_pcm_runtime *runtime = substream->runtime;
803 struct snd_pcm_hw_params *params, *sparams;
804 struct snd_pcm_sw_params *sw_params;
805 ssize_t oss_buffer_size, oss_period_size;
806 size_t oss_frame_size;
809 int format, sformat, n;
810 struct snd_mask sformat_mask;
811 struct snd_mask mask;
813 if (mutex_lock_interruptible(&runtime->oss.params_lock))
815 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
816 params = kmalloc(sizeof(*params), GFP_KERNEL);
817 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
818 if (!sw_params || !params || !sparams) {
819 snd_printd("No memory\n");
824 if (atomic_read(&substream->mmap_count))
827 direct = substream->oss.setup.direct;
829 _snd_pcm_hw_params_any(sparams);
830 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
831 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
832 snd_mask_none(&mask);
833 if (atomic_read(&substream->mmap_count))
834 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
836 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
838 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
840 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
842 snd_printd("No usable accesses\n");
846 choose_rate(substream, sparams, runtime->oss.rate);
847 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
849 format = snd_pcm_oss_format_from(runtime->oss.format);
851 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
855 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
857 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
858 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
859 if (snd_mask_test(&sformat_mask, sformat) &&
860 snd_pcm_oss_format_to(sformat) >= 0)
863 if (sformat > SNDRV_PCM_FORMAT_LAST) {
864 snd_printd("Cannot find a format!!!\n");
869 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
870 snd_assert(err >= 0, goto failure);
873 memcpy(params, sparams, sizeof(*params));
875 _snd_pcm_hw_params_any(params);
876 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
877 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
878 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
879 snd_pcm_oss_format_from(runtime->oss.format), 0);
880 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
881 runtime->oss.channels, 0);
882 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
883 runtime->oss.rate, 0);
884 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
885 params_access(params), params_format(params),
886 params_channels(params), params_rate(params));
888 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
889 params_access(sparams), params_format(sparams),
890 params_channels(sparams), params_rate(sparams));
892 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
893 params_channels(params) / 8;
895 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
896 snd_pcm_oss_plugin_clear(substream);
898 /* add necessary plugins */
899 snd_pcm_oss_plugin_clear(substream);
900 if ((err = snd_pcm_plug_format_plugins(substream,
903 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
904 snd_pcm_oss_plugin_clear(substream);
907 if (runtime->oss.plugin_first) {
908 struct snd_pcm_plugin *plugin;
909 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
910 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
911 snd_pcm_oss_plugin_clear(substream);
914 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
915 err = snd_pcm_plugin_append(plugin);
917 err = snd_pcm_plugin_insert(plugin);
920 snd_pcm_oss_plugin_clear(substream);
927 err = snd_pcm_oss_period_size(substream, params, sparams);
931 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
932 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
933 snd_assert(err >= 0, goto failure);
935 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
936 runtime->oss.periods, NULL);
937 snd_assert(err >= 0, goto failure);
939 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
941 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
942 snd_printd("HW_PARAMS failed: %i\n", err);
946 memset(sw_params, 0, sizeof(*sw_params));
947 if (runtime->oss.trigger) {
948 sw_params->start_threshold = 1;
950 sw_params->start_threshold = runtime->boundary;
952 if (atomic_read(&substream->mmap_count) ||
953 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
954 sw_params->stop_threshold = runtime->boundary;
956 sw_params->stop_threshold = runtime->buffer_size;
957 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
958 sw_params->period_step = 1;
959 sw_params->sleep_min = 0;
960 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
961 1 : runtime->period_size;
962 sw_params->xfer_align = 1;
963 if (atomic_read(&substream->mmap_count) ||
964 substream->oss.setup.nosilence) {
965 sw_params->silence_threshold = 0;
966 sw_params->silence_size = 0;
968 snd_pcm_uframes_t frames;
969 frames = runtime->period_size + 16;
970 if (frames > runtime->buffer_size)
971 frames = runtime->buffer_size;
972 sw_params->silence_threshold = frames;
973 sw_params->silence_size = frames;
976 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
977 snd_printd("SW_PARAMS failed: %i\n", err);
981 runtime->oss.periods = params_periods(sparams);
982 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
983 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
984 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
985 if (runtime->oss.plugin_first) {
986 err = snd_pcm_plug_alloc(substream, oss_period_size);
991 oss_period_size *= oss_frame_size;
993 oss_buffer_size = oss_period_size * runtime->oss.periods;
994 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
996 runtime->oss.period_bytes = oss_period_size;
997 runtime->oss.buffer_bytes = oss_buffer_size;
999 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1000 runtime->oss.period_bytes,
1001 runtime->oss.buffer_bytes);
1002 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1003 params_period_size(sparams),
1004 params_buffer_size(sparams));
1006 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1007 runtime->oss.channels = params_channels(params);
1008 runtime->oss.rate = params_rate(params);
1010 runtime->oss.params = 0;
1011 runtime->oss.prepare = 1;
1012 vfree(runtime->oss.buffer);
1013 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1014 runtime->oss.buffer_used = 0;
1015 if (runtime->dma_area)
1016 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1018 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1025 mutex_unlock(&runtime->oss.params_lock);
1029 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1032 struct snd_pcm_substream *asubstream = NULL, *substream;
1034 for (idx = 0; idx < 2; idx++) {
1035 substream = pcm_oss_file->streams[idx];
1036 if (substream == NULL)
1038 if (asubstream == NULL)
1039 asubstream = substream;
1040 if (substream->runtime->oss.params) {
1041 err = snd_pcm_oss_change_params(substream);
1046 snd_assert(asubstream != NULL, return -EIO);
1048 *r_substream = asubstream;
1052 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1055 struct snd_pcm_runtime *runtime = substream->runtime;
1057 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1059 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1062 runtime->oss.prepare = 0;
1063 runtime->oss.prev_hw_ptr_interrupt = 0;
1064 runtime->oss.period_ptr = 0;
1065 runtime->oss.buffer_used = 0;
1070 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1072 struct snd_pcm_runtime *runtime;
1075 if (substream == NULL)
1077 runtime = substream->runtime;
1078 if (runtime->oss.params) {
1079 err = snd_pcm_oss_change_params(substream);
1083 if (runtime->oss.prepare) {
1084 err = snd_pcm_oss_prepare(substream);
1091 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1093 struct snd_pcm_runtime *runtime;
1094 snd_pcm_uframes_t frames;
1098 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1101 runtime = substream->runtime;
1102 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1104 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1105 /* until avail(delay) <= buffer_size */
1106 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1107 frames /= runtime->period_size;
1108 frames *= runtime->period_size;
1109 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1116 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1121 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1122 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1124 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1125 printk("pcm_oss: write: recovering from XRUN\n");
1127 printk("pcm_oss: write: recovering from SUSPEND\n");
1129 ret = snd_pcm_oss_prepare(substream);
1135 fs = snd_enter_user();
1136 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1139 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1141 if (ret != -EPIPE && ret != -ESTRPIPE)
1143 /* test, if we can't store new data, because the stream */
1144 /* has not been started */
1145 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1151 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1153 struct snd_pcm_runtime *runtime = substream->runtime;
1154 snd_pcm_sframes_t delay;
1157 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1158 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1160 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1161 printk("pcm_oss: read: recovering from XRUN\n");
1163 printk("pcm_oss: read: recovering from SUSPEND\n");
1165 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1168 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1169 ret = snd_pcm_oss_prepare(substream);
1173 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1178 fs = snd_enter_user();
1179 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1182 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1184 if (ret == -EPIPE) {
1185 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1186 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1192 if (ret != -ESTRPIPE)
1198 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1200 struct snd_pcm_runtime *runtime = substream->runtime;
1203 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1204 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1206 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1207 printk("pcm_oss: writev: recovering from XRUN\n");
1209 printk("pcm_oss: writev: recovering from SUSPEND\n");
1211 ret = snd_pcm_oss_prepare(substream);
1217 fs = snd_enter_user();
1218 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1221 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1223 if (ret != -EPIPE && ret != -ESTRPIPE)
1226 /* test, if we can't store new data, because the stream */
1227 /* has not been started */
1228 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1234 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1236 struct snd_pcm_runtime *runtime = substream->runtime;
1239 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1240 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1242 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1243 printk("pcm_oss: readv: recovering from XRUN\n");
1245 printk("pcm_oss: readv: recovering from SUSPEND\n");
1247 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1250 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1251 ret = snd_pcm_oss_prepare(substream);
1257 fs = snd_enter_user();
1258 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1261 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1263 if (ret != -EPIPE && ret != -ESTRPIPE)
1269 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1271 struct snd_pcm_runtime *runtime = substream->runtime;
1272 snd_pcm_sframes_t frames, frames1;
1273 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1274 if (runtime->oss.plugin_first) {
1275 struct snd_pcm_plugin_channel *channels;
1276 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1278 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1280 buf = runtime->oss.buffer;
1282 frames = bytes / oss_frame_bytes;
1283 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1286 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1289 bytes = frames1 * oss_frame_bytes;
1293 frames = bytes_to_frames(runtime, bytes);
1294 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1297 bytes = frames_to_bytes(runtime, frames1);
1302 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1306 struct snd_pcm_runtime *runtime = substream->runtime;
1308 if (atomic_read(&substream->mmap_count))
1311 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1313 mutex_lock(&runtime->oss.params_lock);
1315 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1317 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1318 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1320 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1325 runtime->oss.buffer_used += tmp;
1329 if (substream->oss.setup.partialfrag ||
1330 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1331 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1332 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1335 runtime->oss.bytes += tmp;
1336 runtime->oss.period_ptr += tmp;
1337 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1338 if (runtime->oss.period_ptr == 0 ||
1339 runtime->oss.period_ptr == runtime->oss.buffer_used)
1340 runtime->oss.buffer_used = 0;
1341 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1347 tmp = snd_pcm_oss_write2(substream,
1348 (const char __force *)buf,
1349 runtime->oss.period_bytes, 0);
1352 runtime->oss.bytes += tmp;
1356 if ((substream->f_flags & O_NONBLOCK) != 0 &&
1357 tmp != runtime->oss.period_bytes)
1361 mutex_unlock(&runtime->oss.params_lock);
1365 mutex_unlock(&runtime->oss.params_lock);
1366 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1369 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1371 struct snd_pcm_runtime *runtime = substream->runtime;
1372 snd_pcm_sframes_t frames, frames1;
1373 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1374 char __user *final_dst = (char __user *)buf;
1375 if (runtime->oss.plugin_first) {
1376 struct snd_pcm_plugin_channel *channels;
1377 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1379 buf = runtime->oss.buffer;
1380 frames = bytes / oss_frame_bytes;
1381 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1384 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1387 bytes = frames1 * oss_frame_bytes;
1388 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1393 frames = bytes_to_frames(runtime, bytes);
1394 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1397 bytes = frames_to_bytes(runtime, frames1);
1402 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1406 struct snd_pcm_runtime *runtime = substream->runtime;
1408 if (atomic_read(&substream->mmap_count))
1411 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1413 mutex_lock(&runtime->oss.params_lock);
1415 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1416 if (runtime->oss.buffer_used == 0) {
1417 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1420 runtime->oss.bytes += tmp;
1421 runtime->oss.period_ptr = tmp;
1422 runtime->oss.buffer_used = tmp;
1425 if ((size_t) tmp > runtime->oss.buffer_used)
1426 tmp = runtime->oss.buffer_used;
1427 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1434 runtime->oss.buffer_used -= tmp;
1436 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1437 runtime->oss.period_bytes, 0);
1440 runtime->oss.bytes += tmp;
1446 mutex_unlock(&runtime->oss.params_lock);
1450 mutex_unlock(&runtime->oss.params_lock);
1451 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1454 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1456 struct snd_pcm_substream *substream;
1458 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459 if (substream != NULL) {
1460 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1461 substream->runtime->oss.prepare = 1;
1463 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1464 if (substream != NULL) {
1465 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1466 substream->runtime->oss.prepare = 1;
1471 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1473 struct snd_pcm_substream *substream;
1476 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1477 if (substream != NULL) {
1478 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1480 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1482 /* note: all errors from the start action are ignored */
1483 /* OSS apps do not know, how to handle them */
1487 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1489 struct snd_pcm_runtime *runtime;
1493 static char printed_comm[16];
1495 if (strncmp(printed_comm, current->comm, 16)) {
1496 printk(KERN_DEBUG "application %s uses obsolete OSS audio interface\n",
1498 memcpy(printed_comm, current->comm, 16);
1501 runtime = substream->runtime;
1502 init_waitqueue_entry(&wait, current);
1503 add_wait_queue(&runtime->sleep, &wait);
1505 printk("sync1: size = %li\n", size);
1508 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1510 runtime->oss.buffer_used = 0;
1514 if (result != 0 && result != -EAGAIN)
1517 set_current_state(TASK_INTERRUPTIBLE);
1518 snd_pcm_stream_lock_irq(substream);
1519 res = runtime->status->state;
1520 snd_pcm_stream_unlock_irq(substream);
1521 if (res != SNDRV_PCM_STATE_RUNNING) {
1522 set_current_state(TASK_RUNNING);
1525 res = schedule_timeout(10 * HZ);
1526 if (signal_pending(current)) {
1527 result = -ERESTARTSYS;
1531 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1536 remove_wait_queue(&runtime->sleep, &wait);
1540 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1543 unsigned int saved_f_flags;
1544 struct snd_pcm_substream *substream;
1545 struct snd_pcm_runtime *runtime;
1546 snd_pcm_format_t format;
1547 unsigned long width;
1550 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1551 if (substream != NULL) {
1552 runtime = substream->runtime;
1553 if (atomic_read(&substream->mmap_count))
1555 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1557 format = snd_pcm_oss_format_from(runtime->oss.format);
1558 width = snd_pcm_format_physical_width(format);
1559 mutex_lock(&runtime->oss.params_lock);
1560 if (runtime->oss.buffer_used > 0) {
1562 printk("sync: buffer_used\n");
1564 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1565 snd_pcm_format_set_silence(format,
1566 runtime->oss.buffer + runtime->oss.buffer_used,
1568 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1570 mutex_unlock(&runtime->oss.params_lock);
1573 } else if (runtime->oss.period_ptr > 0) {
1575 printk("sync: period_ptr\n");
1577 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1578 snd_pcm_format_set_silence(format,
1579 runtime->oss.buffer,
1581 err = snd_pcm_oss_sync1(substream, size);
1583 mutex_unlock(&runtime->oss.params_lock);
1588 * The ALSA's period might be a bit large than OSS one.
1589 * Fill the remain portion of ALSA period with zeros.
1591 size = runtime->control->appl_ptr % runtime->period_size;
1593 size = runtime->period_size - size;
1594 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1595 size = (runtime->frame_bits * size) / 8;
1598 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1601 size1 /= runtime->sample_bits;
1602 snd_pcm_format_set_silence(runtime->format,
1603 runtime->oss.buffer,
1605 fs = snd_enter_user();
1606 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1609 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1610 void __user *buffers[runtime->channels];
1611 memset(buffers, 0, runtime->channels * sizeof(void *));
1612 snd_pcm_lib_writev(substream, buffers, size);
1615 mutex_unlock(&runtime->oss.params_lock);
1617 * finish sync: drain the buffer
1620 saved_f_flags = substream->f_flags;
1621 substream->f_flags &= ~O_NONBLOCK;
1622 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1623 substream->f_flags = saved_f_flags;
1626 runtime->oss.prepare = 1;
1629 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1630 if (substream != NULL) {
1631 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1633 runtime = substream->runtime;
1634 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1637 runtime->oss.buffer_used = 0;
1638 runtime->oss.prepare = 1;
1643 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1647 for (idx = 1; idx >= 0; --idx) {
1648 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1649 struct snd_pcm_runtime *runtime;
1650 if (substream == NULL)
1652 runtime = substream->runtime;
1655 else if (rate > 192000)
1657 if (runtime->oss.rate != rate) {
1658 runtime->oss.params = 1;
1659 runtime->oss.rate = rate;
1662 return snd_pcm_oss_get_rate(pcm_oss_file);
1665 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1667 struct snd_pcm_substream *substream;
1670 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1672 return substream->runtime->oss.rate;
1675 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1682 for (idx = 1; idx >= 0; --idx) {
1683 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1684 struct snd_pcm_runtime *runtime;
1685 if (substream == NULL)
1687 runtime = substream->runtime;
1688 if (runtime->oss.channels != channels) {
1689 runtime->oss.params = 1;
1690 runtime->oss.channels = channels;
1693 return snd_pcm_oss_get_channels(pcm_oss_file);
1696 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1698 struct snd_pcm_substream *substream;
1701 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1703 return substream->runtime->oss.channels;
1706 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1708 struct snd_pcm_substream *substream;
1711 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1713 return substream->runtime->oss.period_bytes;
1716 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1718 struct snd_pcm_substream *substream;
1721 struct snd_pcm_hw_params *params;
1722 unsigned int formats = 0;
1723 struct snd_mask format_mask;
1726 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1728 if (atomic_read(&substream->mmap_count))
1731 direct = substream->oss.setup.direct;
1733 return AFMT_MU_LAW | AFMT_U8 |
1734 AFMT_S16_LE | AFMT_S16_BE |
1735 AFMT_S8 | AFMT_U16_LE |
1737 params = kmalloc(sizeof(*params), GFP_KERNEL);
1740 _snd_pcm_hw_params_any(params);
1741 err = snd_pcm_hw_refine(substream, params);
1742 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1744 snd_assert(err >= 0, return err);
1745 for (fmt = 0; fmt < 32; ++fmt) {
1746 if (snd_mask_test(&format_mask, fmt)) {
1747 int f = snd_pcm_oss_format_to(fmt);
1755 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1759 if (format != AFMT_QUERY) {
1760 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1763 if (!(formats & format))
1765 for (idx = 1; idx >= 0; --idx) {
1766 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1767 struct snd_pcm_runtime *runtime;
1768 if (substream == NULL)
1770 runtime = substream->runtime;
1771 if (runtime->oss.format != format) {
1772 runtime->oss.params = 1;
1773 runtime->oss.format = format;
1777 return snd_pcm_oss_get_format(pcm_oss_file);
1780 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1782 struct snd_pcm_substream *substream;
1785 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1787 return substream->runtime->oss.format;
1790 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1792 struct snd_pcm_runtime *runtime;
1794 if (substream == NULL)
1796 runtime = substream->runtime;
1797 if (subdivide == 0) {
1798 subdivide = runtime->oss.subdivision;
1803 if (runtime->oss.subdivision || runtime->oss.fragshift)
1805 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1806 subdivide != 8 && subdivide != 16)
1808 runtime->oss.subdivision = subdivide;
1809 runtime->oss.params = 1;
1813 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1815 int err = -EINVAL, idx;
1817 for (idx = 1; idx >= 0; --idx) {
1818 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1819 if (substream == NULL)
1821 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1827 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1829 struct snd_pcm_runtime *runtime;
1831 if (substream == NULL)
1833 runtime = substream->runtime;
1834 if (runtime->oss.subdivision || runtime->oss.fragshift)
1836 runtime->oss.fragshift = val & 0xffff;
1837 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1838 if (runtime->oss.fragshift < 4) /* < 16 */
1839 runtime->oss.fragshift = 4;
1840 if (runtime->oss.maxfrags < 2)
1841 runtime->oss.maxfrags = 2;
1842 runtime->oss.params = 1;
1846 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1848 int err = -EINVAL, idx;
1850 for (idx = 1; idx >= 0; --idx) {
1851 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1852 if (substream == NULL)
1854 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1860 static int snd_pcm_oss_nonblock(struct file * file)
1862 file->f_flags |= O_NONBLOCK;
1866 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1869 if (substream == NULL) {
1870 res &= ~DSP_CAP_DUPLEX;
1873 #ifdef DSP_CAP_MULTI
1874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1875 if (substream->pstr->substream_count > 1)
1876 res |= DSP_CAP_MULTI;
1878 /* DSP_CAP_REALTIME is set all times: */
1879 /* all ALSA drivers can return actual pointer in ring buffer */
1880 #if defined(DSP_CAP_REALTIME) && 0
1882 struct snd_pcm_runtime *runtime = substream->runtime;
1883 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1884 res &= ~DSP_CAP_REALTIME;
1890 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1894 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1895 for (idx = 0; idx < 2; idx++) {
1896 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1897 result = snd_pcm_oss_get_caps1(substream, result);
1899 result |= 0x0001; /* revision - same as SB AWE 64 */
1903 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1905 struct snd_pcm_runtime *runtime = substream->runtime;
1906 snd_pcm_uframes_t appl_ptr;
1907 appl_ptr = hw_ptr + runtime->buffer_size;
1908 appl_ptr %= runtime->boundary;
1909 runtime->control->appl_ptr = appl_ptr;
1912 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1914 struct snd_pcm_runtime *runtime;
1915 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1919 printk("pcm_oss: trigger = 0x%x\n", trigger);
1922 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1923 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1926 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1930 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1934 runtime = psubstream->runtime;
1935 if (trigger & PCM_ENABLE_OUTPUT) {
1936 if (runtime->oss.trigger)
1938 if (atomic_read(&psubstream->mmap_count))
1939 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1940 runtime->oss.trigger = 1;
1941 runtime->start_threshold = 1;
1942 cmd = SNDRV_PCM_IOCTL_START;
1944 if (!runtime->oss.trigger)
1946 runtime->oss.trigger = 0;
1947 runtime->start_threshold = runtime->boundary;
1948 cmd = SNDRV_PCM_IOCTL_DROP;
1949 runtime->oss.prepare = 1;
1951 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
1957 runtime = csubstream->runtime;
1958 if (trigger & PCM_ENABLE_INPUT) {
1959 if (runtime->oss.trigger)
1961 runtime->oss.trigger = 1;
1962 runtime->start_threshold = 1;
1963 cmd = SNDRV_PCM_IOCTL_START;
1965 if (!runtime->oss.trigger)
1967 runtime->oss.trigger = 0;
1968 runtime->start_threshold = runtime->boundary;
1969 cmd = SNDRV_PCM_IOCTL_DROP;
1970 runtime->oss.prepare = 1;
1972 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
1980 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1982 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1985 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1986 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1987 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1988 result |= PCM_ENABLE_OUTPUT;
1989 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1990 result |= PCM_ENABLE_INPUT;
1994 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1996 struct snd_pcm_substream *substream;
1997 struct snd_pcm_runtime *runtime;
1998 snd_pcm_sframes_t delay;
2001 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2002 if (substream == NULL)
2004 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2006 runtime = substream->runtime;
2007 if (runtime->oss.params || runtime->oss.prepare)
2009 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2011 delay = 0; /* hack for broken OSS applications */
2014 return snd_pcm_oss_bytes(substream, delay);
2017 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2019 struct snd_pcm_substream *substream;
2020 struct snd_pcm_runtime *runtime;
2021 snd_pcm_sframes_t delay;
2023 struct count_info info;
2028 substream = pcm_oss_file->streams[stream];
2029 if (substream == NULL)
2031 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2033 runtime = substream->runtime;
2034 if (runtime->oss.params || runtime->oss.prepare) {
2035 memset(&info, 0, sizeof(info));
2036 if (copy_to_user(_info, &info, sizeof(info)))
2040 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2041 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2042 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2047 fixup = runtime->oss.buffer_used;
2050 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2051 fixup = -runtime->oss.buffer_used;
2055 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2056 if (atomic_read(&substream->mmap_count)) {
2057 snd_pcm_sframes_t n;
2058 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2060 n += runtime->boundary;
2061 info.blocks = n / runtime->period_size;
2062 runtime->oss.prev_hw_ptr_interrupt = delay;
2063 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2064 snd_pcm_oss_simulate_fill(substream, delay);
2065 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2067 delay = snd_pcm_oss_bytes(substream, delay);
2068 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2069 if (substream->oss.setup.buggyptr)
2070 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2072 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2073 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2076 info.blocks = delay / runtime->oss.period_bytes;
2077 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2080 if (copy_to_user(_info, &info, sizeof(info)))
2085 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2087 struct snd_pcm_substream *substream;
2088 struct snd_pcm_runtime *runtime;
2089 snd_pcm_sframes_t avail;
2091 struct audio_buf_info info;
2096 substream = pcm_oss_file->streams[stream];
2097 if (substream == NULL)
2099 runtime = substream->runtime;
2101 if (runtime->oss.params &&
2102 (err = snd_pcm_oss_change_params(substream)) < 0)
2105 info.fragsize = runtime->oss.period_bytes;
2106 info.fragstotal = runtime->periods;
2107 if (runtime->oss.prepare) {
2108 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2109 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2110 info.fragments = runtime->oss.periods;
2116 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2117 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2118 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2119 avail = runtime->buffer_size;
2123 avail = runtime->buffer_size - avail;
2124 fixup = -runtime->oss.buffer_used;
2127 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2128 fixup = runtime->oss.buffer_used;
2132 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2133 info.fragments = info.bytes / runtime->oss.period_bytes;
2137 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2139 if (copy_to_user(_info, &info, sizeof(info)))
2144 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2146 // it won't be probably implemented
2147 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2151 static const char *strip_task_path(const char *path)
2153 const char *ptr, *ptrl = NULL;
2154 for (ptr = path; *ptr; ptr++) {
2161 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2162 const char *task_name,
2163 struct snd_pcm_oss_setup *rsetup)
2165 struct snd_pcm_oss_setup *setup;
2167 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2169 for (setup = pcm->streams[stream].oss.setup_list; setup;
2170 setup = setup->next) {
2171 if (!strcmp(setup->task_name, task_name))
2174 } while ((task_name = strip_task_path(task_name)) != NULL);
2178 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2181 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2183 struct snd_pcm_runtime *runtime;
2184 runtime = substream->runtime;
2185 vfree(runtime->oss.buffer);
2186 runtime->oss.buffer = NULL;
2187 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2188 snd_pcm_oss_plugin_clear(substream);
2190 substream->oss.oss = 0;
2193 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2194 struct snd_pcm_oss_setup *setup,
2197 struct snd_pcm_runtime *runtime;
2199 substream->oss.oss = 1;
2200 substream->oss.setup = *setup;
2201 if (setup->nonblock)
2202 substream->f_flags |= O_NONBLOCK;
2203 else if (setup->block)
2204 substream->f_flags &= ~O_NONBLOCK;
2205 runtime = substream->runtime;
2206 runtime->oss.params = 1;
2207 runtime->oss.trigger = 1;
2208 runtime->oss.rate = 8000;
2209 mutex_init(&runtime->oss.params_lock);
2210 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2211 case SNDRV_MINOR_OSS_PCM_8:
2212 runtime->oss.format = AFMT_U8;
2214 case SNDRV_MINOR_OSS_PCM_16:
2215 runtime->oss.format = AFMT_S16_LE;
2218 runtime->oss.format = AFMT_MU_LAW;
2220 runtime->oss.channels = 1;
2221 runtime->oss.fragshift = 0;
2222 runtime->oss.maxfrags = 0;
2223 runtime->oss.subdivision = 0;
2224 substream->pcm_release = snd_pcm_oss_release_substream;
2227 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2230 snd_assert(pcm_oss_file != NULL, return -ENXIO);
2231 for (cidx = 0; cidx < 2; ++cidx) {
2232 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2234 snd_pcm_release_substream(substream);
2236 kfree(pcm_oss_file);
2240 static int snd_pcm_oss_open_file(struct file *file,
2241 struct snd_pcm *pcm,
2242 struct snd_pcm_oss_file **rpcm_oss_file,
2244 struct snd_pcm_oss_setup *setup)
2247 struct snd_pcm_oss_file *pcm_oss_file;
2248 struct snd_pcm_substream *substream;
2249 unsigned int f_mode = file->f_mode;
2251 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2252 *rpcm_oss_file = NULL;
2254 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2255 if (pcm_oss_file == NULL)
2258 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2259 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2260 f_mode = FMODE_WRITE;
2262 file->f_flags &= ~O_APPEND;
2263 for (idx = 0; idx < 2; idx++) {
2264 if (setup[idx].disable)
2266 if (! pcm->streams[idx].substream_count)
2267 continue; /* no matching substream */
2268 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2269 if (! (f_mode & FMODE_WRITE))
2272 if (! (f_mode & FMODE_READ))
2275 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2277 snd_pcm_oss_release_file(pcm_oss_file);
2281 pcm_oss_file->streams[idx] = substream;
2282 substream->file = pcm_oss_file;
2283 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2286 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2287 snd_pcm_oss_release_file(pcm_oss_file);
2291 file->private_data = pcm_oss_file;
2292 *rpcm_oss_file = pcm_oss_file;
2297 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2301 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2302 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2303 name[idx] = task->comm[idx];
2308 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2312 struct snd_pcm *pcm;
2313 struct snd_pcm_oss_file *pcm_oss_file;
2314 struct snd_pcm_oss_setup setup[2];
2318 pcm = snd_lookup_oss_minor_data(iminor(inode),
2319 SNDRV_OSS_DEVICE_TYPE_PCM);
2324 err = snd_card_file_add(pcm->card, file);
2327 if (!try_module_get(pcm->card->module)) {
2331 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2335 memset(setup, 0, sizeof(setup));
2336 if (file->f_mode & FMODE_WRITE)
2337 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2338 task_name, &setup[0]);
2339 if (file->f_mode & FMODE_READ)
2340 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2341 task_name, &setup[1]);
2343 nonblock = !!(file->f_flags & O_NONBLOCK);
2345 nonblock = nonblock_open;
2347 init_waitqueue_entry(&wait, current);
2348 add_wait_queue(&pcm->open_wait, &wait);
2349 mutex_lock(&pcm->open_mutex);
2351 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2352 iminor(inode), setup);
2355 if (err == -EAGAIN) {
2362 set_current_state(TASK_INTERRUPTIBLE);
2363 mutex_unlock(&pcm->open_mutex);
2365 mutex_lock(&pcm->open_mutex);
2366 if (signal_pending(current)) {
2371 remove_wait_queue(&pcm->open_wait, &wait);
2372 mutex_unlock(&pcm->open_mutex);
2378 module_put(pcm->card->module);
2380 snd_card_file_remove(pcm->card, file);
2385 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2387 struct snd_pcm *pcm;
2388 struct snd_pcm_substream *substream;
2389 struct snd_pcm_oss_file *pcm_oss_file;
2391 pcm_oss_file = file->private_data;
2392 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2393 if (substream == NULL)
2394 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2395 snd_assert(substream != NULL, return -ENXIO);
2396 pcm = substream->pcm;
2397 if (!pcm->card->shutdown)
2398 snd_pcm_oss_sync(pcm_oss_file);
2399 mutex_lock(&pcm->open_mutex);
2400 snd_pcm_oss_release_file(pcm_oss_file);
2401 mutex_unlock(&pcm->open_mutex);
2402 wake_up(&pcm->open_wait);
2403 module_put(pcm->card->module);
2404 snd_card_file_remove(pcm->card, file);
2408 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2410 struct snd_pcm_oss_file *pcm_oss_file;
2411 int __user *p = (int __user *)arg;
2414 pcm_oss_file = file->private_data;
2415 if (cmd == OSS_GETVERSION)
2416 return put_user(SNDRV_OSS_VERSION, p);
2417 if (cmd == OSS_ALSAEMULVER)
2418 return put_user(1, p);
2419 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2420 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2421 struct snd_pcm_substream *substream;
2423 for (idx = 0; idx < 2; ++idx) {
2424 substream = pcm_oss_file->streams[idx];
2425 if (substream != NULL)
2428 snd_assert(substream != NULL, return -ENXIO);
2429 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2432 if (((cmd >> 8) & 0xff) != 'P')
2435 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2438 case SNDCTL_DSP_RESET:
2439 return snd_pcm_oss_reset(pcm_oss_file);
2440 case SNDCTL_DSP_SYNC:
2441 return snd_pcm_oss_sync(pcm_oss_file);
2442 case SNDCTL_DSP_SPEED:
2443 if (get_user(res, p))
2445 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2447 return put_user(res, p);
2448 case SOUND_PCM_READ_RATE:
2449 res = snd_pcm_oss_get_rate(pcm_oss_file);
2452 return put_user(res, p);
2453 case SNDCTL_DSP_STEREO:
2454 if (get_user(res, p))
2456 res = res > 0 ? 2 : 1;
2457 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2459 return put_user(--res, p);
2460 case SNDCTL_DSP_GETBLKSIZE:
2461 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2464 return put_user(res, p);
2465 case SNDCTL_DSP_SETFMT:
2466 if (get_user(res, p))
2468 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2471 return put_user(res, p);
2472 case SOUND_PCM_READ_BITS:
2473 res = snd_pcm_oss_get_format(pcm_oss_file);
2476 return put_user(res, p);
2477 case SNDCTL_DSP_CHANNELS:
2478 if (get_user(res, p))
2480 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2483 return put_user(res, p);
2484 case SOUND_PCM_READ_CHANNELS:
2485 res = snd_pcm_oss_get_channels(pcm_oss_file);
2488 return put_user(res, p);
2489 case SOUND_PCM_WRITE_FILTER:
2490 case SOUND_PCM_READ_FILTER:
2492 case SNDCTL_DSP_POST:
2493 return snd_pcm_oss_post(pcm_oss_file);
2494 case SNDCTL_DSP_SUBDIVIDE:
2495 if (get_user(res, p))
2497 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2500 return put_user(res, p);
2501 case SNDCTL_DSP_SETFRAGMENT:
2502 if (get_user(res, p))
2504 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2505 case SNDCTL_DSP_GETFMTS:
2506 res = snd_pcm_oss_get_formats(pcm_oss_file);
2509 return put_user(res, p);
2510 case SNDCTL_DSP_GETOSPACE:
2511 case SNDCTL_DSP_GETISPACE:
2512 return snd_pcm_oss_get_space(pcm_oss_file,
2513 cmd == SNDCTL_DSP_GETISPACE ?
2514 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2515 (struct audio_buf_info __user *) arg);
2516 case SNDCTL_DSP_NONBLOCK:
2517 return snd_pcm_oss_nonblock(file);
2518 case SNDCTL_DSP_GETCAPS:
2519 res = snd_pcm_oss_get_caps(pcm_oss_file);
2522 return put_user(res, p);
2523 case SNDCTL_DSP_GETTRIGGER:
2524 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2527 return put_user(res, p);
2528 case SNDCTL_DSP_SETTRIGGER:
2529 if (get_user(res, p))
2531 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2532 case SNDCTL_DSP_GETIPTR:
2533 case SNDCTL_DSP_GETOPTR:
2534 return snd_pcm_oss_get_ptr(pcm_oss_file,
2535 cmd == SNDCTL_DSP_GETIPTR ?
2536 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2537 (struct count_info __user *) arg);
2538 case SNDCTL_DSP_MAPINBUF:
2539 case SNDCTL_DSP_MAPOUTBUF:
2540 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2541 cmd == SNDCTL_DSP_MAPINBUF ?
2542 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2543 (struct buffmem_desc __user *) arg);
2544 case SNDCTL_DSP_SETSYNCRO:
2545 /* stop DMA now.. */
2547 case SNDCTL_DSP_SETDUPLEX:
2548 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2551 case SNDCTL_DSP_GETODELAY:
2552 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2554 /* it's for sure, some broken apps don't check for error codes */
2558 return put_user(res, p);
2559 case SNDCTL_DSP_PROFILE:
2560 return 0; /* silently ignore */
2562 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2567 #ifdef CONFIG_COMPAT
2568 /* all compatible */
2569 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2571 #define snd_pcm_oss_ioctl_compat NULL
2574 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2576 struct snd_pcm_oss_file *pcm_oss_file;
2577 struct snd_pcm_substream *substream;
2579 pcm_oss_file = file->private_data;
2580 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2581 if (substream == NULL)
2583 substream->f_flags = file->f_flags & O_NONBLOCK;
2585 return snd_pcm_oss_read1(substream, buf, count);
2588 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2589 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2595 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2597 struct snd_pcm_oss_file *pcm_oss_file;
2598 struct snd_pcm_substream *substream;
2601 pcm_oss_file = file->private_data;
2602 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2603 if (substream == NULL)
2605 substream->f_flags = file->f_flags & O_NONBLOCK;
2606 result = snd_pcm_oss_write1(substream, buf, count);
2608 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2613 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2615 struct snd_pcm_runtime *runtime = substream->runtime;
2616 if (atomic_read(&substream->mmap_count))
2617 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2619 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2622 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2624 struct snd_pcm_runtime *runtime = substream->runtime;
2625 if (atomic_read(&substream->mmap_count))
2626 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2628 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2631 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2633 struct snd_pcm_oss_file *pcm_oss_file;
2635 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2637 pcm_oss_file = file->private_data;
2639 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2640 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2643 if (psubstream != NULL) {
2644 struct snd_pcm_runtime *runtime = psubstream->runtime;
2645 poll_wait(file, &runtime->sleep, wait);
2646 snd_pcm_stream_lock_irq(psubstream);
2647 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2648 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2649 snd_pcm_oss_playback_ready(psubstream)))
2650 mask |= POLLOUT | POLLWRNORM;
2651 snd_pcm_stream_unlock_irq(psubstream);
2653 if (csubstream != NULL) {
2654 struct snd_pcm_runtime *runtime = csubstream->runtime;
2655 snd_pcm_state_t ostate;
2656 poll_wait(file, &runtime->sleep, wait);
2657 snd_pcm_stream_lock_irq(csubstream);
2658 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2659 snd_pcm_oss_capture_ready(csubstream))
2660 mask |= POLLIN | POLLRDNORM;
2661 snd_pcm_stream_unlock_irq(csubstream);
2662 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2663 struct snd_pcm_oss_file ofile;
2664 memset(&ofile, 0, sizeof(ofile));
2665 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2666 runtime->oss.trigger = 0;
2667 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2674 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2676 struct snd_pcm_oss_file *pcm_oss_file;
2677 struct snd_pcm_substream *substream = NULL;
2678 struct snd_pcm_runtime *runtime;
2682 printk("pcm_oss: mmap begin\n");
2684 pcm_oss_file = file->private_data;
2685 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2686 case VM_READ | VM_WRITE:
2687 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2692 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2695 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2700 /* set VM_READ access as well to fix memset() routines that do
2701 reads before writes (to improve performance) */
2702 area->vm_flags |= VM_READ;
2703 if (substream == NULL)
2705 runtime = substream->runtime;
2706 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2708 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2709 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2713 if (runtime->oss.params) {
2714 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2717 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2718 if (runtime->oss.plugin_first != NULL)
2722 if (area->vm_pgoff != 0)
2725 err = snd_pcm_mmap_data(substream, file, area);
2728 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2729 runtime->silence_threshold = 0;
2730 runtime->silence_size = 0;
2732 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2734 /* In mmap mode we never stop */
2735 runtime->stop_threshold = runtime->boundary;
2740 #ifdef CONFIG_SND_VERBOSE_PROCFS
2745 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2746 struct snd_info_buffer *buffer)
2748 struct snd_pcm_str *pstr = entry->private_data;
2749 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2750 mutex_lock(&pstr->oss.setup_mutex);
2752 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2756 setup->disable ? " disable" : "",
2757 setup->direct ? " direct" : "",
2758 setup->block ? " block" : "",
2759 setup->nonblock ? " non-block" : "",
2760 setup->partialfrag ? " partial-frag" : "",
2761 setup->nosilence ? " no-silence" : "");
2762 setup = setup->next;
2764 mutex_unlock(&pstr->oss.setup_mutex);
2767 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2769 struct snd_pcm_oss_setup *setup, *setupn;
2771 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2772 setup; setup = setupn) {
2773 setupn = setup->next;
2774 kfree(setup->task_name);
2777 pstr->oss.setup_list = NULL;
2780 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2781 struct snd_info_buffer *buffer)
2783 struct snd_pcm_str *pstr = entry->private_data;
2784 char line[128], str[32], task_name[32], *ptr;
2786 struct snd_pcm_oss_setup *setup, *setup1, template;
2788 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2789 mutex_lock(&pstr->oss.setup_mutex);
2790 memset(&template, 0, sizeof(template));
2791 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2792 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2793 snd_pcm_oss_proc_free_setup_list(pstr);
2794 mutex_unlock(&pstr->oss.setup_mutex);
2797 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2798 if (!strcmp(setup->task_name, task_name)) {
2803 ptr = snd_info_get_str(str, ptr, sizeof(str));
2804 template.periods = simple_strtoul(str, NULL, 10);
2805 ptr = snd_info_get_str(str, ptr, sizeof(str));
2806 template.period_size = simple_strtoul(str, NULL, 10);
2807 for (idx1 = 31; idx1 >= 0; idx1--)
2808 if (template.period_size & (1 << idx1))
2810 for (idx1--; idx1 >= 0; idx1--)
2811 template.period_size &= ~(1 << idx1);
2813 ptr = snd_info_get_str(str, ptr, sizeof(str));
2814 if (!strcmp(str, "disable")) {
2815 template.disable = 1;
2816 } else if (!strcmp(str, "direct")) {
2817 template.direct = 1;
2818 } else if (!strcmp(str, "block")) {
2820 } else if (!strcmp(str, "non-block")) {
2821 template.nonblock = 1;
2822 } else if (!strcmp(str, "partial-frag")) {
2823 template.partialfrag = 1;
2824 } else if (!strcmp(str, "no-silence")) {
2825 template.nosilence = 1;
2826 } else if (!strcmp(str, "buggy-ptr")) {
2827 template.buggyptr = 1;
2830 if (setup == NULL) {
2831 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2833 buffer->error = -ENOMEM;
2834 mutex_lock(&pstr->oss.setup_mutex);
2837 if (pstr->oss.setup_list == NULL)
2838 pstr->oss.setup_list = setup;
2840 for (setup1 = pstr->oss.setup_list;
2841 setup1->next; setup1 = setup1->next);
2842 setup1->next = setup;
2844 template.task_name = kstrdup(task_name, GFP_KERNEL);
2845 if (! template.task_name) {
2847 buffer->error = -ENOMEM;
2848 mutex_lock(&pstr->oss.setup_mutex);
2853 mutex_unlock(&pstr->oss.setup_mutex);
2857 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2860 for (stream = 0; stream < 2; ++stream) {
2861 struct snd_info_entry *entry;
2862 struct snd_pcm_str *pstr = &pcm->streams[stream];
2863 if (pstr->substream_count == 0)
2865 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2866 entry->content = SNDRV_INFO_CONTENT_TEXT;
2867 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2868 entry->c.text.read = snd_pcm_oss_proc_read;
2869 entry->c.text.write = snd_pcm_oss_proc_write;
2870 entry->private_data = pstr;
2871 if (snd_info_register(entry) < 0) {
2872 snd_info_free_entry(entry);
2876 pstr->oss.proc_entry = entry;
2880 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2883 for (stream = 0; stream < 2; ++stream) {
2884 struct snd_pcm_str *pstr = &pcm->streams[stream];
2885 snd_info_free_entry(pstr->oss.proc_entry);
2886 pstr->oss.proc_entry = NULL;
2887 snd_pcm_oss_proc_free_setup_list(pstr);
2890 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2891 #define snd_pcm_oss_proc_init(pcm)
2892 #define snd_pcm_oss_proc_done(pcm)
2893 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2899 static struct file_operations snd_pcm_oss_f_reg =
2901 .owner = THIS_MODULE,
2902 .read = snd_pcm_oss_read,
2903 .write = snd_pcm_oss_write,
2904 .open = snd_pcm_oss_open,
2905 .release = snd_pcm_oss_release,
2906 .poll = snd_pcm_oss_poll,
2907 .unlocked_ioctl = snd_pcm_oss_ioctl,
2908 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2909 .mmap = snd_pcm_oss_mmap,
2912 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2915 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2916 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2917 pcm->card, index, &snd_pcm_oss_f_reg,
2919 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2920 pcm->card->number, pcm->device);
2924 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2927 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2930 register_oss_dsp(pcm, 0);
2931 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2932 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2933 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2934 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2935 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2936 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2941 pcm->oss.reg_mask |= 1;
2943 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2944 register_oss_dsp(pcm, 1);
2946 pcm->oss.reg_mask |= 2;
2950 snd_pcm_oss_proc_init(pcm);
2955 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2958 if (pcm->oss.reg_mask & 1) {
2959 pcm->oss.reg_mask &= ~1;
2960 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2963 if (pcm->oss.reg_mask & 2) {
2964 pcm->oss.reg_mask &= ~2;
2965 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2968 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2969 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2970 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2978 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2980 snd_pcm_oss_disconnect_minor(pcm);
2981 snd_pcm_oss_proc_done(pcm);
2985 static struct snd_pcm_notify snd_pcm_oss_notify =
2987 .n_register = snd_pcm_oss_register_minor,
2988 .n_disconnect = snd_pcm_oss_disconnect_minor,
2989 .n_unregister = snd_pcm_oss_unregister_minor,
2992 static int __init alsa_pcm_oss_init(void)
2997 /* check device map table */
2998 for (i = 0; i < SNDRV_CARDS; i++) {
2999 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3000 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
3004 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3005 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3010 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3015 static void __exit alsa_pcm_oss_exit(void)
3017 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3020 module_init(alsa_pcm_oss_init)
3021 module_exit(alsa_pcm_oss_exit)