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;
1494 runtime = substream->runtime;
1495 init_waitqueue_entry(&wait, current);
1496 add_wait_queue(&runtime->sleep, &wait);
1498 printk("sync1: size = %li\n", size);
1501 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1503 runtime->oss.buffer_used = 0;
1507 if (result != 0 && result != -EAGAIN)
1510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_lock_irq(substream);
1512 res = runtime->status->state;
1513 snd_pcm_stream_unlock_irq(substream);
1514 if (res != SNDRV_PCM_STATE_RUNNING) {
1515 set_current_state(TASK_RUNNING);
1518 res = schedule_timeout(10 * HZ);
1519 if (signal_pending(current)) {
1520 result = -ERESTARTSYS;
1524 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1529 remove_wait_queue(&runtime->sleep, &wait);
1533 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1536 unsigned int saved_f_flags;
1537 struct snd_pcm_substream *substream;
1538 struct snd_pcm_runtime *runtime;
1539 snd_pcm_format_t format;
1540 unsigned long width;
1543 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1544 if (substream != NULL) {
1545 runtime = substream->runtime;
1546 if (atomic_read(&substream->mmap_count))
1548 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1550 format = snd_pcm_oss_format_from(runtime->oss.format);
1551 width = snd_pcm_format_physical_width(format);
1552 mutex_lock(&runtime->oss.params_lock);
1553 if (runtime->oss.buffer_used > 0) {
1555 printk("sync: buffer_used\n");
1557 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1558 snd_pcm_format_set_silence(format,
1559 runtime->oss.buffer + runtime->oss.buffer_used,
1561 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1563 mutex_unlock(&runtime->oss.params_lock);
1566 } else if (runtime->oss.period_ptr > 0) {
1568 printk("sync: period_ptr\n");
1570 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1571 snd_pcm_format_set_silence(format,
1572 runtime->oss.buffer,
1574 err = snd_pcm_oss_sync1(substream, size);
1576 mutex_unlock(&runtime->oss.params_lock);
1581 * The ALSA's period might be a bit large than OSS one.
1582 * Fill the remain portion of ALSA period with zeros.
1584 size = runtime->control->appl_ptr % runtime->period_size;
1586 size = runtime->period_size - size;
1587 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1588 size = (runtime->frame_bits * size) / 8;
1591 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1594 size1 /= runtime->sample_bits;
1595 snd_pcm_format_set_silence(runtime->format,
1596 runtime->oss.buffer,
1598 fs = snd_enter_user();
1599 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1602 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1603 void __user *buffers[runtime->channels];
1604 memset(buffers, 0, runtime->channels * sizeof(void *));
1605 snd_pcm_lib_writev(substream, buffers, size);
1608 mutex_unlock(&runtime->oss.params_lock);
1610 * finish sync: drain the buffer
1613 saved_f_flags = substream->f_flags;
1614 substream->f_flags &= ~O_NONBLOCK;
1615 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1616 substream->f_flags = saved_f_flags;
1619 runtime->oss.prepare = 1;
1622 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1623 if (substream != NULL) {
1624 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1626 runtime = substream->runtime;
1627 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1630 runtime->oss.buffer_used = 0;
1631 runtime->oss.prepare = 1;
1636 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1640 for (idx = 1; idx >= 0; --idx) {
1641 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1642 struct snd_pcm_runtime *runtime;
1643 if (substream == NULL)
1645 runtime = substream->runtime;
1648 else if (rate > 192000)
1650 if (runtime->oss.rate != rate) {
1651 runtime->oss.params = 1;
1652 runtime->oss.rate = rate;
1655 return snd_pcm_oss_get_rate(pcm_oss_file);
1658 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1660 struct snd_pcm_substream *substream;
1663 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1665 return substream->runtime->oss.rate;
1668 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1675 for (idx = 1; idx >= 0; --idx) {
1676 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1677 struct snd_pcm_runtime *runtime;
1678 if (substream == NULL)
1680 runtime = substream->runtime;
1681 if (runtime->oss.channels != channels) {
1682 runtime->oss.params = 1;
1683 runtime->oss.channels = channels;
1686 return snd_pcm_oss_get_channels(pcm_oss_file);
1689 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1691 struct snd_pcm_substream *substream;
1694 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1696 return substream->runtime->oss.channels;
1699 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1701 struct snd_pcm_substream *substream;
1704 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1706 return substream->runtime->oss.period_bytes;
1709 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1711 struct snd_pcm_substream *substream;
1714 struct snd_pcm_hw_params *params;
1715 unsigned int formats = 0;
1716 struct snd_mask format_mask;
1719 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1721 if (atomic_read(&substream->mmap_count))
1724 direct = substream->oss.setup.direct;
1726 return AFMT_MU_LAW | AFMT_U8 |
1727 AFMT_S16_LE | AFMT_S16_BE |
1728 AFMT_S8 | AFMT_U16_LE |
1730 params = kmalloc(sizeof(*params), GFP_KERNEL);
1733 _snd_pcm_hw_params_any(params);
1734 err = snd_pcm_hw_refine(substream, params);
1735 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1737 snd_assert(err >= 0, return err);
1738 for (fmt = 0; fmt < 32; ++fmt) {
1739 if (snd_mask_test(&format_mask, fmt)) {
1740 int f = snd_pcm_oss_format_to(fmt);
1748 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1752 if (format != AFMT_QUERY) {
1753 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1756 if (!(formats & format))
1758 for (idx = 1; idx >= 0; --idx) {
1759 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1760 struct snd_pcm_runtime *runtime;
1761 if (substream == NULL)
1763 runtime = substream->runtime;
1764 if (runtime->oss.format != format) {
1765 runtime->oss.params = 1;
1766 runtime->oss.format = format;
1770 return snd_pcm_oss_get_format(pcm_oss_file);
1773 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1775 struct snd_pcm_substream *substream;
1778 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1780 return substream->runtime->oss.format;
1783 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1785 struct snd_pcm_runtime *runtime;
1787 if (substream == NULL)
1789 runtime = substream->runtime;
1790 if (subdivide == 0) {
1791 subdivide = runtime->oss.subdivision;
1796 if (runtime->oss.subdivision || runtime->oss.fragshift)
1798 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1799 subdivide != 8 && subdivide != 16)
1801 runtime->oss.subdivision = subdivide;
1802 runtime->oss.params = 1;
1806 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1808 int err = -EINVAL, idx;
1810 for (idx = 1; idx >= 0; --idx) {
1811 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1812 if (substream == NULL)
1814 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1820 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1822 struct snd_pcm_runtime *runtime;
1824 if (substream == NULL)
1826 runtime = substream->runtime;
1827 if (runtime->oss.subdivision || runtime->oss.fragshift)
1829 runtime->oss.fragshift = val & 0xffff;
1830 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1831 if (runtime->oss.fragshift < 4) /* < 16 */
1832 runtime->oss.fragshift = 4;
1833 if (runtime->oss.maxfrags < 2)
1834 runtime->oss.maxfrags = 2;
1835 runtime->oss.params = 1;
1839 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1841 int err = -EINVAL, idx;
1843 for (idx = 1; idx >= 0; --idx) {
1844 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1845 if (substream == NULL)
1847 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1853 static int snd_pcm_oss_nonblock(struct file * file)
1855 file->f_flags |= O_NONBLOCK;
1859 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1862 if (substream == NULL) {
1863 res &= ~DSP_CAP_DUPLEX;
1866 #ifdef DSP_CAP_MULTI
1867 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1868 if (substream->pstr->substream_count > 1)
1869 res |= DSP_CAP_MULTI;
1871 /* DSP_CAP_REALTIME is set all times: */
1872 /* all ALSA drivers can return actual pointer in ring buffer */
1873 #if defined(DSP_CAP_REALTIME) && 0
1875 struct snd_pcm_runtime *runtime = substream->runtime;
1876 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1877 res &= ~DSP_CAP_REALTIME;
1883 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1887 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1888 for (idx = 0; idx < 2; idx++) {
1889 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1890 result = snd_pcm_oss_get_caps1(substream, result);
1892 result |= 0x0001; /* revision - same as SB AWE 64 */
1896 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1898 struct snd_pcm_runtime *runtime = substream->runtime;
1899 snd_pcm_uframes_t appl_ptr;
1900 appl_ptr = hw_ptr + runtime->buffer_size;
1901 appl_ptr %= runtime->boundary;
1902 runtime->control->appl_ptr = appl_ptr;
1905 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1907 struct snd_pcm_runtime *runtime;
1908 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1912 printk("pcm_oss: trigger = 0x%x\n", trigger);
1915 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1916 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1919 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1923 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1927 runtime = psubstream->runtime;
1928 if (trigger & PCM_ENABLE_OUTPUT) {
1929 if (runtime->oss.trigger)
1931 if (atomic_read(&psubstream->mmap_count))
1932 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1933 runtime->oss.trigger = 1;
1934 runtime->start_threshold = 1;
1935 cmd = SNDRV_PCM_IOCTL_START;
1937 if (!runtime->oss.trigger)
1939 runtime->oss.trigger = 0;
1940 runtime->start_threshold = runtime->boundary;
1941 cmd = SNDRV_PCM_IOCTL_DROP;
1942 runtime->oss.prepare = 1;
1944 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
1950 runtime = csubstream->runtime;
1951 if (trigger & PCM_ENABLE_INPUT) {
1952 if (runtime->oss.trigger)
1954 runtime->oss.trigger = 1;
1955 runtime->start_threshold = 1;
1956 cmd = SNDRV_PCM_IOCTL_START;
1958 if (!runtime->oss.trigger)
1960 runtime->oss.trigger = 0;
1961 runtime->start_threshold = runtime->boundary;
1962 cmd = SNDRV_PCM_IOCTL_DROP;
1963 runtime->oss.prepare = 1;
1965 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
1973 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1975 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1978 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1979 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1980 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1981 result |= PCM_ENABLE_OUTPUT;
1982 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1983 result |= PCM_ENABLE_INPUT;
1987 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1989 struct snd_pcm_substream *substream;
1990 struct snd_pcm_runtime *runtime;
1991 snd_pcm_sframes_t delay;
1994 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1995 if (substream == NULL)
1997 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1999 runtime = substream->runtime;
2000 if (runtime->oss.params || runtime->oss.prepare)
2002 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2004 delay = 0; /* hack for broken OSS applications */
2007 return snd_pcm_oss_bytes(substream, delay);
2010 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2012 struct snd_pcm_substream *substream;
2013 struct snd_pcm_runtime *runtime;
2014 snd_pcm_sframes_t delay;
2016 struct count_info info;
2021 substream = pcm_oss_file->streams[stream];
2022 if (substream == NULL)
2024 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2026 runtime = substream->runtime;
2027 if (runtime->oss.params || runtime->oss.prepare) {
2028 memset(&info, 0, sizeof(info));
2029 if (copy_to_user(_info, &info, sizeof(info)))
2033 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2034 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2035 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2040 fixup = runtime->oss.buffer_used;
2043 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2044 fixup = -runtime->oss.buffer_used;
2048 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2049 if (atomic_read(&substream->mmap_count)) {
2050 snd_pcm_sframes_t n;
2051 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2053 n += runtime->boundary;
2054 info.blocks = n / runtime->period_size;
2055 runtime->oss.prev_hw_ptr_interrupt = delay;
2056 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2057 snd_pcm_oss_simulate_fill(substream, delay);
2058 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2060 delay = snd_pcm_oss_bytes(substream, delay);
2061 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2062 if (substream->oss.setup.buggyptr)
2063 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2065 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2066 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2069 info.blocks = delay / runtime->oss.period_bytes;
2070 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2073 if (copy_to_user(_info, &info, sizeof(info)))
2078 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2080 struct snd_pcm_substream *substream;
2081 struct snd_pcm_runtime *runtime;
2082 snd_pcm_sframes_t avail;
2084 struct audio_buf_info info;
2089 substream = pcm_oss_file->streams[stream];
2090 if (substream == NULL)
2092 runtime = substream->runtime;
2094 if (runtime->oss.params &&
2095 (err = snd_pcm_oss_change_params(substream)) < 0)
2098 info.fragsize = runtime->oss.period_bytes;
2099 info.fragstotal = runtime->periods;
2100 if (runtime->oss.prepare) {
2101 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2102 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2103 info.fragments = runtime->oss.periods;
2109 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2110 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2111 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2112 avail = runtime->buffer_size;
2116 avail = runtime->buffer_size - avail;
2117 fixup = -runtime->oss.buffer_used;
2120 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2121 fixup = runtime->oss.buffer_used;
2125 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2126 info.fragments = info.bytes / runtime->oss.period_bytes;
2130 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2132 if (copy_to_user(_info, &info, sizeof(info)))
2137 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2139 // it won't be probably implemented
2140 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2144 static const char *strip_task_path(const char *path)
2146 const char *ptr, *ptrl = NULL;
2147 for (ptr = path; *ptr; ptr++) {
2154 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2155 const char *task_name,
2156 struct snd_pcm_oss_setup *rsetup)
2158 struct snd_pcm_oss_setup *setup;
2160 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2162 for (setup = pcm->streams[stream].oss.setup_list; setup;
2163 setup = setup->next) {
2164 if (!strcmp(setup->task_name, task_name))
2167 } while ((task_name = strip_task_path(task_name)) != NULL);
2171 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2174 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2176 struct snd_pcm_runtime *runtime;
2177 runtime = substream->runtime;
2178 vfree(runtime->oss.buffer);
2179 runtime->oss.buffer = NULL;
2180 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2181 snd_pcm_oss_plugin_clear(substream);
2183 substream->oss.oss = 0;
2186 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2187 struct snd_pcm_oss_setup *setup,
2190 struct snd_pcm_runtime *runtime;
2192 substream->oss.oss = 1;
2193 substream->oss.setup = *setup;
2194 if (setup->nonblock)
2195 substream->f_flags |= O_NONBLOCK;
2196 else if (setup->block)
2197 substream->f_flags &= ~O_NONBLOCK;
2198 runtime = substream->runtime;
2199 runtime->oss.params = 1;
2200 runtime->oss.trigger = 1;
2201 runtime->oss.rate = 8000;
2202 mutex_init(&runtime->oss.params_lock);
2203 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2204 case SNDRV_MINOR_OSS_PCM_8:
2205 runtime->oss.format = AFMT_U8;
2207 case SNDRV_MINOR_OSS_PCM_16:
2208 runtime->oss.format = AFMT_S16_LE;
2211 runtime->oss.format = AFMT_MU_LAW;
2213 runtime->oss.channels = 1;
2214 runtime->oss.fragshift = 0;
2215 runtime->oss.maxfrags = 0;
2216 runtime->oss.subdivision = 0;
2217 substream->pcm_release = snd_pcm_oss_release_substream;
2220 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2223 snd_assert(pcm_oss_file != NULL, return -ENXIO);
2224 for (cidx = 0; cidx < 2; ++cidx) {
2225 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2227 snd_pcm_release_substream(substream);
2229 kfree(pcm_oss_file);
2233 static int snd_pcm_oss_open_file(struct file *file,
2234 struct snd_pcm *pcm,
2235 struct snd_pcm_oss_file **rpcm_oss_file,
2237 struct snd_pcm_oss_setup *setup)
2240 struct snd_pcm_oss_file *pcm_oss_file;
2241 struct snd_pcm_substream *substream;
2242 unsigned int f_mode = file->f_mode;
2244 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2245 *rpcm_oss_file = NULL;
2247 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2248 if (pcm_oss_file == NULL)
2251 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2252 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2253 f_mode = FMODE_WRITE;
2255 file->f_flags &= ~O_APPEND;
2256 for (idx = 0; idx < 2; idx++) {
2257 if (setup[idx].disable)
2259 if (! pcm->streams[idx].substream_count)
2260 continue; /* no matching substream */
2261 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2262 if (! (f_mode & FMODE_WRITE))
2265 if (! (f_mode & FMODE_READ))
2268 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2270 snd_pcm_oss_release_file(pcm_oss_file);
2274 pcm_oss_file->streams[idx] = substream;
2275 substream->file = pcm_oss_file;
2276 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2279 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2280 snd_pcm_oss_release_file(pcm_oss_file);
2284 file->private_data = pcm_oss_file;
2285 *rpcm_oss_file = pcm_oss_file;
2290 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2294 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2295 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2296 name[idx] = task->comm[idx];
2301 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2305 struct snd_pcm *pcm;
2306 struct snd_pcm_oss_file *pcm_oss_file;
2307 struct snd_pcm_oss_setup setup[2];
2311 pcm = snd_lookup_oss_minor_data(iminor(inode),
2312 SNDRV_OSS_DEVICE_TYPE_PCM);
2317 err = snd_card_file_add(pcm->card, file);
2320 if (!try_module_get(pcm->card->module)) {
2324 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2328 memset(setup, 0, sizeof(setup));
2329 if (file->f_mode & FMODE_WRITE)
2330 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2331 task_name, &setup[0]);
2332 if (file->f_mode & FMODE_READ)
2333 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2334 task_name, &setup[1]);
2336 nonblock = !!(file->f_flags & O_NONBLOCK);
2338 nonblock = nonblock_open;
2340 init_waitqueue_entry(&wait, current);
2341 add_wait_queue(&pcm->open_wait, &wait);
2342 mutex_lock(&pcm->open_mutex);
2344 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2345 iminor(inode), setup);
2348 if (err == -EAGAIN) {
2355 set_current_state(TASK_INTERRUPTIBLE);
2356 mutex_unlock(&pcm->open_mutex);
2358 mutex_lock(&pcm->open_mutex);
2359 if (signal_pending(current)) {
2364 remove_wait_queue(&pcm->open_wait, &wait);
2365 mutex_unlock(&pcm->open_mutex);
2371 module_put(pcm->card->module);
2373 snd_card_file_remove(pcm->card, file);
2378 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2380 struct snd_pcm *pcm;
2381 struct snd_pcm_substream *substream;
2382 struct snd_pcm_oss_file *pcm_oss_file;
2384 pcm_oss_file = file->private_data;
2385 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2386 if (substream == NULL)
2387 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2388 snd_assert(substream != NULL, return -ENXIO);
2389 pcm = substream->pcm;
2390 if (!pcm->card->shutdown)
2391 snd_pcm_oss_sync(pcm_oss_file);
2392 mutex_lock(&pcm->open_mutex);
2393 snd_pcm_oss_release_file(pcm_oss_file);
2394 mutex_unlock(&pcm->open_mutex);
2395 wake_up(&pcm->open_wait);
2396 module_put(pcm->card->module);
2397 snd_card_file_remove(pcm->card, file);
2401 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2403 struct snd_pcm_oss_file *pcm_oss_file;
2404 int __user *p = (int __user *)arg;
2407 pcm_oss_file = file->private_data;
2408 if (cmd == OSS_GETVERSION)
2409 return put_user(SNDRV_OSS_VERSION, p);
2410 if (cmd == OSS_ALSAEMULVER)
2411 return put_user(1, p);
2412 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2413 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2414 struct snd_pcm_substream *substream;
2416 for (idx = 0; idx < 2; ++idx) {
2417 substream = pcm_oss_file->streams[idx];
2418 if (substream != NULL)
2421 snd_assert(substream != NULL, return -ENXIO);
2422 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2425 if (((cmd >> 8) & 0xff) != 'P')
2428 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2431 case SNDCTL_DSP_RESET:
2432 return snd_pcm_oss_reset(pcm_oss_file);
2433 case SNDCTL_DSP_SYNC:
2434 return snd_pcm_oss_sync(pcm_oss_file);
2435 case SNDCTL_DSP_SPEED:
2436 if (get_user(res, p))
2438 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2440 return put_user(res, p);
2441 case SOUND_PCM_READ_RATE:
2442 res = snd_pcm_oss_get_rate(pcm_oss_file);
2445 return put_user(res, p);
2446 case SNDCTL_DSP_STEREO:
2447 if (get_user(res, p))
2449 res = res > 0 ? 2 : 1;
2450 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2452 return put_user(--res, p);
2453 case SNDCTL_DSP_GETBLKSIZE:
2454 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2457 return put_user(res, p);
2458 case SNDCTL_DSP_SETFMT:
2459 if (get_user(res, p))
2461 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2464 return put_user(res, p);
2465 case SOUND_PCM_READ_BITS:
2466 res = snd_pcm_oss_get_format(pcm_oss_file);
2469 return put_user(res, p);
2470 case SNDCTL_DSP_CHANNELS:
2471 if (get_user(res, p))
2473 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2476 return put_user(res, p);
2477 case SOUND_PCM_READ_CHANNELS:
2478 res = snd_pcm_oss_get_channels(pcm_oss_file);
2481 return put_user(res, p);
2482 case SOUND_PCM_WRITE_FILTER:
2483 case SOUND_PCM_READ_FILTER:
2485 case SNDCTL_DSP_POST:
2486 return snd_pcm_oss_post(pcm_oss_file);
2487 case SNDCTL_DSP_SUBDIVIDE:
2488 if (get_user(res, p))
2490 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2493 return put_user(res, p);
2494 case SNDCTL_DSP_SETFRAGMENT:
2495 if (get_user(res, p))
2497 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2498 case SNDCTL_DSP_GETFMTS:
2499 res = snd_pcm_oss_get_formats(pcm_oss_file);
2502 return put_user(res, p);
2503 case SNDCTL_DSP_GETOSPACE:
2504 case SNDCTL_DSP_GETISPACE:
2505 return snd_pcm_oss_get_space(pcm_oss_file,
2506 cmd == SNDCTL_DSP_GETISPACE ?
2507 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2508 (struct audio_buf_info __user *) arg);
2509 case SNDCTL_DSP_NONBLOCK:
2510 return snd_pcm_oss_nonblock(file);
2511 case SNDCTL_DSP_GETCAPS:
2512 res = snd_pcm_oss_get_caps(pcm_oss_file);
2515 return put_user(res, p);
2516 case SNDCTL_DSP_GETTRIGGER:
2517 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2520 return put_user(res, p);
2521 case SNDCTL_DSP_SETTRIGGER:
2522 if (get_user(res, p))
2524 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2525 case SNDCTL_DSP_GETIPTR:
2526 case SNDCTL_DSP_GETOPTR:
2527 return snd_pcm_oss_get_ptr(pcm_oss_file,
2528 cmd == SNDCTL_DSP_GETIPTR ?
2529 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2530 (struct count_info __user *) arg);
2531 case SNDCTL_DSP_MAPINBUF:
2532 case SNDCTL_DSP_MAPOUTBUF:
2533 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2534 cmd == SNDCTL_DSP_MAPINBUF ?
2535 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2536 (struct buffmem_desc __user *) arg);
2537 case SNDCTL_DSP_SETSYNCRO:
2538 /* stop DMA now.. */
2540 case SNDCTL_DSP_SETDUPLEX:
2541 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2544 case SNDCTL_DSP_GETODELAY:
2545 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2547 /* it's for sure, some broken apps don't check for error codes */
2551 return put_user(res, p);
2552 case SNDCTL_DSP_PROFILE:
2553 return 0; /* silently ignore */
2555 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2560 #ifdef CONFIG_COMPAT
2561 /* all compatible */
2562 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2564 #define snd_pcm_oss_ioctl_compat NULL
2567 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2569 struct snd_pcm_oss_file *pcm_oss_file;
2570 struct snd_pcm_substream *substream;
2572 pcm_oss_file = file->private_data;
2573 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2574 if (substream == NULL)
2576 substream->f_flags = file->f_flags & O_NONBLOCK;
2578 return snd_pcm_oss_read1(substream, buf, count);
2581 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2582 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2588 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2590 struct snd_pcm_oss_file *pcm_oss_file;
2591 struct snd_pcm_substream *substream;
2594 pcm_oss_file = file->private_data;
2595 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2596 if (substream == NULL)
2598 substream->f_flags = file->f_flags & O_NONBLOCK;
2599 result = snd_pcm_oss_write1(substream, buf, count);
2601 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2606 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2608 struct snd_pcm_runtime *runtime = substream->runtime;
2609 if (atomic_read(&substream->mmap_count))
2610 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2612 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2615 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2617 struct snd_pcm_runtime *runtime = substream->runtime;
2618 if (atomic_read(&substream->mmap_count))
2619 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2621 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2624 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2626 struct snd_pcm_oss_file *pcm_oss_file;
2628 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2630 pcm_oss_file = file->private_data;
2632 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2633 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2636 if (psubstream != NULL) {
2637 struct snd_pcm_runtime *runtime = psubstream->runtime;
2638 poll_wait(file, &runtime->sleep, wait);
2639 snd_pcm_stream_lock_irq(psubstream);
2640 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2641 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2642 snd_pcm_oss_playback_ready(psubstream)))
2643 mask |= POLLOUT | POLLWRNORM;
2644 snd_pcm_stream_unlock_irq(psubstream);
2646 if (csubstream != NULL) {
2647 struct snd_pcm_runtime *runtime = csubstream->runtime;
2648 snd_pcm_state_t ostate;
2649 poll_wait(file, &runtime->sleep, wait);
2650 snd_pcm_stream_lock_irq(csubstream);
2651 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2652 snd_pcm_oss_capture_ready(csubstream))
2653 mask |= POLLIN | POLLRDNORM;
2654 snd_pcm_stream_unlock_irq(csubstream);
2655 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2656 struct snd_pcm_oss_file ofile;
2657 memset(&ofile, 0, sizeof(ofile));
2658 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2659 runtime->oss.trigger = 0;
2660 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2667 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2669 struct snd_pcm_oss_file *pcm_oss_file;
2670 struct snd_pcm_substream *substream = NULL;
2671 struct snd_pcm_runtime *runtime;
2675 printk("pcm_oss: mmap begin\n");
2677 pcm_oss_file = file->private_data;
2678 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2679 case VM_READ | VM_WRITE:
2680 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2685 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2688 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2693 /* set VM_READ access as well to fix memset() routines that do
2694 reads before writes (to improve performance) */
2695 area->vm_flags |= VM_READ;
2696 if (substream == NULL)
2698 runtime = substream->runtime;
2699 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2701 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2702 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2706 if (runtime->oss.params) {
2707 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2710 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2711 if (runtime->oss.plugin_first != NULL)
2715 if (area->vm_pgoff != 0)
2718 err = snd_pcm_mmap_data(substream, file, area);
2721 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2722 runtime->silence_threshold = 0;
2723 runtime->silence_size = 0;
2725 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2727 /* In mmap mode we never stop */
2728 runtime->stop_threshold = runtime->boundary;
2733 #ifdef CONFIG_SND_VERBOSE_PROCFS
2738 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2739 struct snd_info_buffer *buffer)
2741 struct snd_pcm_str *pstr = entry->private_data;
2742 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2743 mutex_lock(&pstr->oss.setup_mutex);
2745 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2749 setup->disable ? " disable" : "",
2750 setup->direct ? " direct" : "",
2751 setup->block ? " block" : "",
2752 setup->nonblock ? " non-block" : "",
2753 setup->partialfrag ? " partial-frag" : "",
2754 setup->nosilence ? " no-silence" : "");
2755 setup = setup->next;
2757 mutex_unlock(&pstr->oss.setup_mutex);
2760 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2762 struct snd_pcm_oss_setup *setup, *setupn;
2764 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2765 setup; setup = setupn) {
2766 setupn = setup->next;
2767 kfree(setup->task_name);
2770 pstr->oss.setup_list = NULL;
2773 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2774 struct snd_info_buffer *buffer)
2776 struct snd_pcm_str *pstr = entry->private_data;
2777 char line[128], str[32], task_name[32], *ptr;
2779 struct snd_pcm_oss_setup *setup, *setup1, template;
2781 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2782 mutex_lock(&pstr->oss.setup_mutex);
2783 memset(&template, 0, sizeof(template));
2784 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2785 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2786 snd_pcm_oss_proc_free_setup_list(pstr);
2787 mutex_unlock(&pstr->oss.setup_mutex);
2790 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2791 if (!strcmp(setup->task_name, task_name)) {
2796 ptr = snd_info_get_str(str, ptr, sizeof(str));
2797 template.periods = simple_strtoul(str, NULL, 10);
2798 ptr = snd_info_get_str(str, ptr, sizeof(str));
2799 template.period_size = simple_strtoul(str, NULL, 10);
2800 for (idx1 = 31; idx1 >= 0; idx1--)
2801 if (template.period_size & (1 << idx1))
2803 for (idx1--; idx1 >= 0; idx1--)
2804 template.period_size &= ~(1 << idx1);
2806 ptr = snd_info_get_str(str, ptr, sizeof(str));
2807 if (!strcmp(str, "disable")) {
2808 template.disable = 1;
2809 } else if (!strcmp(str, "direct")) {
2810 template.direct = 1;
2811 } else if (!strcmp(str, "block")) {
2813 } else if (!strcmp(str, "non-block")) {
2814 template.nonblock = 1;
2815 } else if (!strcmp(str, "partial-frag")) {
2816 template.partialfrag = 1;
2817 } else if (!strcmp(str, "no-silence")) {
2818 template.nosilence = 1;
2819 } else if (!strcmp(str, "buggy-ptr")) {
2820 template.buggyptr = 1;
2823 if (setup == NULL) {
2824 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2826 buffer->error = -ENOMEM;
2827 mutex_lock(&pstr->oss.setup_mutex);
2830 if (pstr->oss.setup_list == NULL)
2831 pstr->oss.setup_list = setup;
2833 for (setup1 = pstr->oss.setup_list;
2834 setup1->next; setup1 = setup1->next);
2835 setup1->next = setup;
2837 template.task_name = kstrdup(task_name, GFP_KERNEL);
2838 if (! template.task_name) {
2840 buffer->error = -ENOMEM;
2841 mutex_lock(&pstr->oss.setup_mutex);
2846 mutex_unlock(&pstr->oss.setup_mutex);
2850 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2853 for (stream = 0; stream < 2; ++stream) {
2854 struct snd_info_entry *entry;
2855 struct snd_pcm_str *pstr = &pcm->streams[stream];
2856 if (pstr->substream_count == 0)
2858 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2859 entry->content = SNDRV_INFO_CONTENT_TEXT;
2860 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2861 entry->c.text.read = snd_pcm_oss_proc_read;
2862 entry->c.text.write = snd_pcm_oss_proc_write;
2863 entry->private_data = pstr;
2864 if (snd_info_register(entry) < 0) {
2865 snd_info_free_entry(entry);
2869 pstr->oss.proc_entry = entry;
2873 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2876 for (stream = 0; stream < 2; ++stream) {
2877 struct snd_pcm_str *pstr = &pcm->streams[stream];
2878 snd_info_free_entry(pstr->oss.proc_entry);
2879 pstr->oss.proc_entry = NULL;
2880 snd_pcm_oss_proc_free_setup_list(pstr);
2883 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2884 #define snd_pcm_oss_proc_init(pcm)
2885 #define snd_pcm_oss_proc_done(pcm)
2886 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2892 static const struct file_operations snd_pcm_oss_f_reg =
2894 .owner = THIS_MODULE,
2895 .read = snd_pcm_oss_read,
2896 .write = snd_pcm_oss_write,
2897 .open = snd_pcm_oss_open,
2898 .release = snd_pcm_oss_release,
2899 .poll = snd_pcm_oss_poll,
2900 .unlocked_ioctl = snd_pcm_oss_ioctl,
2901 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2902 .mmap = snd_pcm_oss_mmap,
2905 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2908 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2909 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2910 pcm->card, index, &snd_pcm_oss_f_reg,
2912 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2913 pcm->card->number, pcm->device);
2917 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2920 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2923 register_oss_dsp(pcm, 0);
2924 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2925 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2926 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2927 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2928 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2929 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2934 pcm->oss.reg_mask |= 1;
2936 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2937 register_oss_dsp(pcm, 1);
2939 pcm->oss.reg_mask |= 2;
2943 snd_pcm_oss_proc_init(pcm);
2948 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2951 if (pcm->oss.reg_mask & 1) {
2952 pcm->oss.reg_mask &= ~1;
2953 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2956 if (pcm->oss.reg_mask & 2) {
2957 pcm->oss.reg_mask &= ~2;
2958 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2961 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2962 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2963 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2971 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2973 snd_pcm_oss_disconnect_minor(pcm);
2974 snd_pcm_oss_proc_done(pcm);
2978 static struct snd_pcm_notify snd_pcm_oss_notify =
2980 .n_register = snd_pcm_oss_register_minor,
2981 .n_disconnect = snd_pcm_oss_disconnect_minor,
2982 .n_unregister = snd_pcm_oss_unregister_minor,
2985 static int __init alsa_pcm_oss_init(void)
2990 /* check device map table */
2991 for (i = 0; i < SNDRV_CARDS; i++) {
2992 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2993 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2997 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2998 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3003 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3008 static void __exit alsa_pcm_oss_exit(void)
3010 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3013 module_init(alsa_pcm_oss_init)
3014 module_exit(alsa_pcm_oss_exit)