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] = {[0 ... (SNDRV_CARDS-1)] = 0};
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(&runtime->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 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
814 params = kmalloc(sizeof(*params), GFP_KERNEL);
815 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
816 if (!sw_params || !params || !sparams) {
817 snd_printd("No memory\n");
822 if (atomic_read(&runtime->mmap_count))
825 direct = substream->oss.setup.direct;
827 _snd_pcm_hw_params_any(sparams);
828 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
829 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
830 snd_mask_none(&mask);
831 if (atomic_read(&runtime->mmap_count))
832 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
834 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
836 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
838 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
840 snd_printd("No usable accesses\n");
844 choose_rate(substream, sparams, runtime->oss.rate);
845 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
847 format = snd_pcm_oss_format_from(runtime->oss.format);
849 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
853 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
855 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
856 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
857 if (snd_mask_test(&sformat_mask, sformat) &&
858 snd_pcm_oss_format_to(sformat) >= 0)
861 if (sformat > SNDRV_PCM_FORMAT_LAST) {
862 snd_printd("Cannot find a format!!!\n");
867 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
868 snd_assert(err >= 0, goto failure);
871 memcpy(params, sparams, sizeof(*params));
873 _snd_pcm_hw_params_any(params);
874 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
875 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
876 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
877 snd_pcm_oss_format_from(runtime->oss.format), 0);
878 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
879 runtime->oss.channels, 0);
880 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
881 runtime->oss.rate, 0);
882 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
883 params_access(params), params_format(params),
884 params_channels(params), params_rate(params));
886 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
887 params_access(sparams), params_format(sparams),
888 params_channels(sparams), params_rate(sparams));
890 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
891 params_channels(params) / 8;
893 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
894 snd_pcm_oss_plugin_clear(substream);
896 /* add necessary plugins */
897 snd_pcm_oss_plugin_clear(substream);
898 if ((err = snd_pcm_plug_format_plugins(substream,
901 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
902 snd_pcm_oss_plugin_clear(substream);
905 if (runtime->oss.plugin_first) {
906 struct snd_pcm_plugin *plugin;
907 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
908 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
909 snd_pcm_oss_plugin_clear(substream);
912 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
913 err = snd_pcm_plugin_append(plugin);
915 err = snd_pcm_plugin_insert(plugin);
918 snd_pcm_oss_plugin_clear(substream);
925 err = snd_pcm_oss_period_size(substream, params, sparams);
929 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
930 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
931 snd_assert(err >= 0, goto failure);
933 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
934 runtime->oss.periods, NULL);
935 snd_assert(err >= 0, goto failure);
937 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
939 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
940 snd_printd("HW_PARAMS failed: %i\n", err);
944 memset(sw_params, 0, sizeof(*sw_params));
945 if (runtime->oss.trigger) {
946 sw_params->start_threshold = 1;
948 sw_params->start_threshold = runtime->boundary;
950 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
951 sw_params->stop_threshold = runtime->boundary;
953 sw_params->stop_threshold = runtime->buffer_size;
954 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
955 sw_params->period_step = 1;
956 sw_params->sleep_min = 0;
957 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
958 1 : runtime->period_size;
959 sw_params->xfer_align = 1;
960 if (atomic_read(&runtime->mmap_count) ||
961 substream->oss.setup.nosilence) {
962 sw_params->silence_threshold = 0;
963 sw_params->silence_size = 0;
965 snd_pcm_uframes_t frames;
966 frames = runtime->period_size + 16;
967 if (frames > runtime->buffer_size)
968 frames = runtime->buffer_size;
969 sw_params->silence_threshold = frames;
970 sw_params->silence_size = frames;
973 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
974 snd_printd("SW_PARAMS failed: %i\n", err);
978 runtime->oss.periods = params_periods(sparams);
979 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
980 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
981 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
982 if (runtime->oss.plugin_first) {
983 err = snd_pcm_plug_alloc(substream, oss_period_size);
988 oss_period_size *= oss_frame_size;
990 oss_buffer_size = oss_period_size * runtime->oss.periods;
991 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
993 runtime->oss.period_bytes = oss_period_size;
994 runtime->oss.buffer_bytes = oss_buffer_size;
996 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
997 runtime->oss.period_bytes,
998 runtime->oss.buffer_bytes);
999 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1000 params_period_size(sparams),
1001 params_buffer_size(sparams));
1003 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1004 runtime->oss.channels = params_channels(params);
1005 runtime->oss.rate = params_rate(params);
1007 runtime->oss.params = 0;
1008 runtime->oss.prepare = 1;
1009 vfree(runtime->oss.buffer);
1010 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1011 runtime->oss.buffer_used = 0;
1012 if (runtime->dma_area)
1013 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1015 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1025 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1028 struct snd_pcm_substream *asubstream = NULL, *substream;
1030 for (idx = 0; idx < 2; idx++) {
1031 substream = pcm_oss_file->streams[idx];
1032 if (substream == NULL)
1034 if (asubstream == NULL)
1035 asubstream = substream;
1036 if (substream->runtime->oss.params) {
1037 err = snd_pcm_oss_change_params(substream);
1042 snd_assert(asubstream != NULL, return -EIO);
1044 *r_substream = asubstream;
1048 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1051 struct snd_pcm_runtime *runtime = substream->runtime;
1053 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1055 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1058 runtime->oss.prepare = 0;
1059 runtime->oss.prev_hw_ptr_interrupt = 0;
1060 runtime->oss.period_ptr = 0;
1061 runtime->oss.buffer_used = 0;
1066 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1068 struct snd_pcm_runtime *runtime;
1071 if (substream == NULL)
1073 runtime = substream->runtime;
1074 if (runtime->oss.params) {
1075 err = snd_pcm_oss_change_params(substream);
1079 if (runtime->oss.prepare) {
1080 err = snd_pcm_oss_prepare(substream);
1087 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1089 struct snd_pcm_runtime *runtime;
1090 snd_pcm_uframes_t frames;
1094 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1097 runtime = substream->runtime;
1098 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1100 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1101 /* until avail(delay) <= buffer_size */
1102 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1103 frames /= runtime->period_size;
1104 frames *= runtime->period_size;
1105 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1112 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1114 struct snd_pcm_runtime *runtime = substream->runtime;
1117 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1118 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1120 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1121 printk("pcm_oss: write: recovering from XRUN\n");
1123 printk("pcm_oss: write: recovering from SUSPEND\n");
1125 ret = snd_pcm_oss_prepare(substream);
1131 fs = snd_enter_user();
1132 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1135 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1137 if (ret != -EPIPE && ret != -ESTRPIPE)
1139 /* test, if we can't store new data, because the stream */
1140 /* has not been started */
1141 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1147 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1149 struct snd_pcm_runtime *runtime = substream->runtime;
1150 snd_pcm_sframes_t delay;
1153 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1154 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1156 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1157 printk("pcm_oss: read: recovering from XRUN\n");
1159 printk("pcm_oss: read: recovering from SUSPEND\n");
1161 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1164 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1165 ret = snd_pcm_oss_prepare(substream);
1169 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1174 fs = snd_enter_user();
1175 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1178 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1180 if (ret == -EPIPE) {
1181 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1182 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1188 if (ret != -ESTRPIPE)
1194 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1196 struct snd_pcm_runtime *runtime = substream->runtime;
1199 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1200 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1202 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1203 printk("pcm_oss: writev: recovering from XRUN\n");
1205 printk("pcm_oss: writev: recovering from SUSPEND\n");
1207 ret = snd_pcm_oss_prepare(substream);
1213 fs = snd_enter_user();
1214 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1217 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1219 if (ret != -EPIPE && ret != -ESTRPIPE)
1222 /* test, if we can't store new data, because the stream */
1223 /* has not been started */
1224 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1230 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1232 struct snd_pcm_runtime *runtime = substream->runtime;
1235 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1236 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1238 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1239 printk("pcm_oss: readv: recovering from XRUN\n");
1241 printk("pcm_oss: readv: recovering from SUSPEND\n");
1243 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1246 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1247 ret = snd_pcm_oss_prepare(substream);
1253 fs = snd_enter_user();
1254 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1257 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1259 if (ret != -EPIPE && ret != -ESTRPIPE)
1265 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1267 struct snd_pcm_runtime *runtime = substream->runtime;
1268 snd_pcm_sframes_t frames, frames1;
1269 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1270 if (runtime->oss.plugin_first) {
1271 struct snd_pcm_plugin_channel *channels;
1272 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1274 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1276 buf = runtime->oss.buffer;
1278 frames = bytes / oss_frame_bytes;
1279 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1282 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1285 bytes = frames1 * oss_frame_bytes;
1289 frames = bytes_to_frames(runtime, bytes);
1290 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1293 bytes = frames_to_bytes(runtime, frames1);
1298 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1302 struct snd_pcm_runtime *runtime = substream->runtime;
1304 if (atomic_read(&runtime->mmap_count))
1307 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1310 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1312 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1313 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1315 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
1316 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1318 runtime->oss.buffer_used += tmp;
1322 if (substream->oss.setup.partialfrag ||
1323 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1324 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1325 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1327 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1328 runtime->oss.bytes += tmp;
1329 runtime->oss.period_ptr += tmp;
1330 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1331 if (runtime->oss.period_ptr == 0 ||
1332 runtime->oss.period_ptr == runtime->oss.buffer_used)
1333 runtime->oss.buffer_used = 0;
1334 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
1335 return xfer > 0 ? xfer : -EAGAIN;
1338 tmp = snd_pcm_oss_write2(substream,
1339 (const char __force *)buf,
1340 runtime->oss.period_bytes, 0);
1342 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1343 runtime->oss.bytes += tmp;
1347 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
1348 tmp != runtime->oss.period_bytes)
1355 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1357 struct snd_pcm_runtime *runtime = substream->runtime;
1358 snd_pcm_sframes_t frames, frames1;
1359 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1360 char __user *final_dst = (char __user *)buf;
1361 if (runtime->oss.plugin_first) {
1362 struct snd_pcm_plugin_channel *channels;
1363 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1365 buf = runtime->oss.buffer;
1366 frames = bytes / oss_frame_bytes;
1367 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1370 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1373 bytes = frames1 * oss_frame_bytes;
1374 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1379 frames = bytes_to_frames(runtime, bytes);
1380 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1383 bytes = frames_to_bytes(runtime, frames1);
1388 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1392 struct snd_pcm_runtime *runtime = substream->runtime;
1394 if (atomic_read(&runtime->mmap_count))
1397 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1400 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1401 if (runtime->oss.buffer_used == 0) {
1402 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1404 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1405 runtime->oss.bytes += tmp;
1406 runtime->oss.period_ptr = tmp;
1407 runtime->oss.buffer_used = tmp;
1410 if ((size_t) tmp > runtime->oss.buffer_used)
1411 tmp = runtime->oss.buffer_used;
1412 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
1413 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1417 runtime->oss.buffer_used -= tmp;
1419 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1420 runtime->oss.period_bytes, 0);
1422 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1423 runtime->oss.bytes += tmp;
1432 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1434 struct snd_pcm_substream *substream;
1436 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1437 if (substream != NULL) {
1438 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1439 substream->runtime->oss.prepare = 1;
1441 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1442 if (substream != NULL) {
1443 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1444 substream->runtime->oss.prepare = 1;
1449 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1451 struct snd_pcm_substream *substream;
1454 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1455 if (substream != NULL) {
1456 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1458 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1460 /* note: all errors from the start action are ignored */
1461 /* OSS apps do not know, how to handle them */
1465 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1467 struct snd_pcm_runtime *runtime;
1472 runtime = substream->runtime;
1473 init_waitqueue_entry(&wait, current);
1474 add_wait_queue(&runtime->sleep, &wait);
1476 printk("sync1: size = %li\n", size);
1479 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1481 runtime->oss.buffer_used = 0;
1485 if (result != 0 && result != -EAGAIN)
1488 set_current_state(TASK_INTERRUPTIBLE);
1489 snd_pcm_stream_lock_irq(substream);
1490 res = runtime->status->state;
1491 snd_pcm_stream_unlock_irq(substream);
1492 if (res != SNDRV_PCM_STATE_RUNNING) {
1493 set_current_state(TASK_RUNNING);
1496 res = schedule_timeout(10 * HZ);
1497 if (signal_pending(current)) {
1498 result = -ERESTARTSYS;
1502 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1507 remove_wait_queue(&runtime->sleep, &wait);
1511 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1514 unsigned int saved_f_flags;
1515 struct snd_pcm_substream *substream;
1516 struct snd_pcm_runtime *runtime;
1517 snd_pcm_format_t format;
1518 unsigned long width;
1521 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1522 if (substream != NULL) {
1523 runtime = substream->runtime;
1524 if (atomic_read(&runtime->mmap_count))
1526 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1528 format = snd_pcm_oss_format_from(runtime->oss.format);
1529 width = snd_pcm_format_physical_width(format);
1530 if (runtime->oss.buffer_used > 0) {
1532 printk("sync: buffer_used\n");
1534 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1535 snd_pcm_format_set_silence(format,
1536 runtime->oss.buffer + runtime->oss.buffer_used,
1538 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1541 } else if (runtime->oss.period_ptr > 0) {
1543 printk("sync: period_ptr\n");
1545 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1546 snd_pcm_format_set_silence(format,
1547 runtime->oss.buffer,
1549 err = snd_pcm_oss_sync1(substream, size);
1554 * The ALSA's period might be a bit large than OSS one.
1555 * Fill the remain portion of ALSA period with zeros.
1557 size = runtime->control->appl_ptr % runtime->period_size;
1559 size = runtime->period_size - size;
1560 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1561 size = (runtime->frame_bits * size) / 8;
1564 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1567 size1 /= runtime->sample_bits;
1568 snd_pcm_format_set_silence(runtime->format,
1569 runtime->oss.buffer,
1571 fs = snd_enter_user();
1572 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1575 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1576 void __user *buffers[runtime->channels];
1577 memset(buffers, 0, runtime->channels * sizeof(void *));
1578 snd_pcm_lib_writev(substream, buffers, size);
1582 * finish sync: drain the buffer
1585 saved_f_flags = substream->ffile->f_flags;
1586 substream->ffile->f_flags &= ~O_NONBLOCK;
1587 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1588 substream->ffile->f_flags = saved_f_flags;
1591 runtime->oss.prepare = 1;
1594 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1595 if (substream != NULL) {
1596 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1598 runtime = substream->runtime;
1599 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1602 runtime->oss.buffer_used = 0;
1603 runtime->oss.prepare = 1;
1608 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1612 for (idx = 1; idx >= 0; --idx) {
1613 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1614 struct snd_pcm_runtime *runtime;
1615 if (substream == NULL)
1617 runtime = substream->runtime;
1620 else if (rate > 192000)
1622 if (runtime->oss.rate != rate) {
1623 runtime->oss.params = 1;
1624 runtime->oss.rate = rate;
1627 return snd_pcm_oss_get_rate(pcm_oss_file);
1630 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1632 struct snd_pcm_substream *substream;
1635 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1637 return substream->runtime->oss.rate;
1640 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
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;
1653 if (runtime->oss.channels != channels) {
1654 runtime->oss.params = 1;
1655 runtime->oss.channels = channels;
1658 return snd_pcm_oss_get_channels(pcm_oss_file);
1661 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1663 struct snd_pcm_substream *substream;
1666 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1668 return substream->runtime->oss.channels;
1671 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1673 struct snd_pcm_substream *substream;
1676 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1678 return substream->runtime->oss.period_bytes;
1681 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1683 struct snd_pcm_substream *substream;
1686 struct snd_pcm_hw_params *params;
1687 unsigned int formats = 0;
1688 struct snd_mask format_mask;
1691 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1693 if (atomic_read(&substream->runtime->mmap_count))
1696 direct = substream->oss.setup.direct;
1698 return AFMT_MU_LAW | AFMT_U8 |
1699 AFMT_S16_LE | AFMT_S16_BE |
1700 AFMT_S8 | AFMT_U16_LE |
1702 params = kmalloc(sizeof(*params), GFP_KERNEL);
1705 _snd_pcm_hw_params_any(params);
1706 err = snd_pcm_hw_refine(substream, params);
1707 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1709 snd_assert(err >= 0, return err);
1710 for (fmt = 0; fmt < 32; ++fmt) {
1711 if (snd_mask_test(&format_mask, fmt)) {
1712 int f = snd_pcm_oss_format_to(fmt);
1720 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1724 if (format != AFMT_QUERY) {
1725 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1728 if (!(formats & format))
1730 for (idx = 1; idx >= 0; --idx) {
1731 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1732 struct snd_pcm_runtime *runtime;
1733 if (substream == NULL)
1735 runtime = substream->runtime;
1736 if (runtime->oss.format != format) {
1737 runtime->oss.params = 1;
1738 runtime->oss.format = format;
1742 return snd_pcm_oss_get_format(pcm_oss_file);
1745 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1747 struct snd_pcm_substream *substream;
1750 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1752 return substream->runtime->oss.format;
1755 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1757 struct snd_pcm_runtime *runtime;
1759 if (substream == NULL)
1761 runtime = substream->runtime;
1762 if (subdivide == 0) {
1763 subdivide = runtime->oss.subdivision;
1768 if (runtime->oss.subdivision || runtime->oss.fragshift)
1770 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1771 subdivide != 8 && subdivide != 16)
1773 runtime->oss.subdivision = subdivide;
1774 runtime->oss.params = 1;
1778 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1780 int err = -EINVAL, idx;
1782 for (idx = 1; idx >= 0; --idx) {
1783 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1784 if (substream == NULL)
1786 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1792 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1794 struct snd_pcm_runtime *runtime;
1796 if (substream == NULL)
1798 runtime = substream->runtime;
1799 if (runtime->oss.subdivision || runtime->oss.fragshift)
1801 runtime->oss.fragshift = val & 0xffff;
1802 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1803 if (runtime->oss.fragshift < 4) /* < 16 */
1804 runtime->oss.fragshift = 4;
1805 if (runtime->oss.maxfrags < 2)
1806 runtime->oss.maxfrags = 2;
1807 runtime->oss.params = 1;
1811 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1813 int err = -EINVAL, idx;
1815 for (idx = 1; idx >= 0; --idx) {
1816 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1817 if (substream == NULL)
1819 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1825 static int snd_pcm_oss_nonblock(struct file * file)
1827 file->f_flags |= O_NONBLOCK;
1831 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1834 if (substream == NULL) {
1835 res &= ~DSP_CAP_DUPLEX;
1838 #ifdef DSP_CAP_MULTI
1839 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1840 if (substream->pstr->substream_count > 1)
1841 res |= DSP_CAP_MULTI;
1843 /* DSP_CAP_REALTIME is set all times: */
1844 /* all ALSA drivers can return actual pointer in ring buffer */
1845 #if defined(DSP_CAP_REALTIME) && 0
1847 struct snd_pcm_runtime *runtime = substream->runtime;
1848 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1849 res &= ~DSP_CAP_REALTIME;
1855 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1859 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1860 for (idx = 0; idx < 2; idx++) {
1861 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1862 result = snd_pcm_oss_get_caps1(substream, result);
1864 result |= 0x0001; /* revision - same as SB AWE 64 */
1868 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1870 struct snd_pcm_runtime *runtime = substream->runtime;
1871 snd_pcm_uframes_t appl_ptr;
1872 appl_ptr = hw_ptr + runtime->buffer_size;
1873 appl_ptr %= runtime->boundary;
1874 runtime->control->appl_ptr = appl_ptr;
1877 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1879 struct snd_pcm_runtime *runtime;
1880 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1884 printk("pcm_oss: trigger = 0x%x\n", trigger);
1887 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1888 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1891 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1895 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1899 runtime = psubstream->runtime;
1900 if (trigger & PCM_ENABLE_OUTPUT) {
1901 if (runtime->oss.trigger)
1903 if (atomic_read(&psubstream->runtime->mmap_count))
1904 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1905 runtime->oss.trigger = 1;
1906 runtime->start_threshold = 1;
1907 cmd = SNDRV_PCM_IOCTL_START;
1909 if (!runtime->oss.trigger)
1911 runtime->oss.trigger = 0;
1912 runtime->start_threshold = runtime->boundary;
1913 cmd = SNDRV_PCM_IOCTL_DROP;
1914 runtime->oss.prepare = 1;
1916 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
1922 runtime = csubstream->runtime;
1923 if (trigger & PCM_ENABLE_INPUT) {
1924 if (runtime->oss.trigger)
1926 runtime->oss.trigger = 1;
1927 runtime->start_threshold = 1;
1928 cmd = SNDRV_PCM_IOCTL_START;
1930 if (!runtime->oss.trigger)
1932 runtime->oss.trigger = 0;
1933 runtime->start_threshold = runtime->boundary;
1934 cmd = SNDRV_PCM_IOCTL_DROP;
1935 runtime->oss.prepare = 1;
1937 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
1945 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1947 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1950 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1951 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1952 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1953 result |= PCM_ENABLE_OUTPUT;
1954 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1955 result |= PCM_ENABLE_INPUT;
1959 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1961 struct snd_pcm_substream *substream;
1962 struct snd_pcm_runtime *runtime;
1963 snd_pcm_sframes_t delay;
1966 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1967 if (substream == NULL)
1969 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1971 runtime = substream->runtime;
1972 if (runtime->oss.params || runtime->oss.prepare)
1974 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1976 delay = 0; /* hack for broken OSS applications */
1979 return snd_pcm_oss_bytes(substream, delay);
1982 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
1984 struct snd_pcm_substream *substream;
1985 struct snd_pcm_runtime *runtime;
1986 snd_pcm_sframes_t delay;
1988 struct count_info info;
1993 substream = pcm_oss_file->streams[stream];
1994 if (substream == NULL)
1996 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1998 runtime = substream->runtime;
1999 if (runtime->oss.params || runtime->oss.prepare) {
2000 memset(&info, 0, sizeof(info));
2001 if (copy_to_user(_info, &info, sizeof(info)))
2005 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2006 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2007 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2012 fixup = runtime->oss.buffer_used;
2015 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2016 fixup = -runtime->oss.buffer_used;
2020 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2021 if (atomic_read(&runtime->mmap_count)) {
2022 snd_pcm_sframes_t n;
2023 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2025 n += runtime->boundary;
2026 info.blocks = n / runtime->period_size;
2027 runtime->oss.prev_hw_ptr_interrupt = delay;
2028 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2029 snd_pcm_oss_simulate_fill(substream, delay);
2030 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2032 delay = snd_pcm_oss_bytes(substream, delay);
2033 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2034 if (substream->oss.setup.buggyptr)
2035 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2037 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2038 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2041 info.blocks = delay / runtime->oss.period_bytes;
2042 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2045 if (copy_to_user(_info, &info, sizeof(info)))
2050 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2052 struct snd_pcm_substream *substream;
2053 struct snd_pcm_runtime *runtime;
2054 snd_pcm_sframes_t avail;
2056 struct audio_buf_info info;
2061 substream = pcm_oss_file->streams[stream];
2062 if (substream == NULL)
2064 runtime = substream->runtime;
2066 if (runtime->oss.params &&
2067 (err = snd_pcm_oss_change_params(substream)) < 0)
2070 info.fragsize = runtime->oss.period_bytes;
2071 info.fragstotal = runtime->periods;
2072 if (runtime->oss.prepare) {
2073 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2074 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2075 info.fragments = runtime->oss.periods;
2081 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2082 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2083 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2084 avail = runtime->buffer_size;
2088 avail = runtime->buffer_size - avail;
2089 fixup = -runtime->oss.buffer_used;
2092 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2093 fixup = runtime->oss.buffer_used;
2097 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2098 info.fragments = info.bytes / runtime->oss.period_bytes;
2102 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2104 if (copy_to_user(_info, &info, sizeof(info)))
2109 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2111 // it won't be probably implemented
2112 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2116 static const char *strip_task_path(const char *path)
2118 const char *ptr, *ptrl = NULL;
2119 for (ptr = path; *ptr; ptr++) {
2126 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2127 const char *task_name,
2128 struct snd_pcm_oss_setup *rsetup)
2130 struct snd_pcm_oss_setup *setup;
2132 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2134 for (setup = pcm->streams[stream].oss.setup_list; setup;
2135 setup = setup->next) {
2136 if (!strcmp(setup->task_name, task_name))
2139 } while ((task_name = strip_task_path(task_name)) != NULL);
2143 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2146 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2148 struct snd_pcm_runtime *runtime;
2149 runtime = substream->runtime;
2150 vfree(runtime->oss.buffer);
2151 runtime->oss.buffer = NULL;
2152 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2153 snd_pcm_oss_plugin_clear(substream);
2155 substream->oss.oss = 0;
2158 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2159 struct snd_pcm_oss_setup *setup,
2162 struct snd_pcm_runtime *runtime;
2164 substream->oss.oss = 1;
2165 substream->oss.setup = *setup;
2166 if (setup->nonblock)
2167 substream->ffile->f_flags |= O_NONBLOCK;
2168 else if (setup->block)
2169 substream->ffile->f_flags &= ~O_NONBLOCK;
2170 runtime = substream->runtime;
2171 runtime->oss.params = 1;
2172 runtime->oss.trigger = 1;
2173 runtime->oss.rate = 8000;
2174 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2175 case SNDRV_MINOR_OSS_PCM_8:
2176 runtime->oss.format = AFMT_U8;
2178 case SNDRV_MINOR_OSS_PCM_16:
2179 runtime->oss.format = AFMT_S16_LE;
2182 runtime->oss.format = AFMT_MU_LAW;
2184 runtime->oss.channels = 1;
2185 runtime->oss.fragshift = 0;
2186 runtime->oss.maxfrags = 0;
2187 runtime->oss.subdivision = 0;
2188 substream->pcm_release = snd_pcm_oss_release_substream;
2191 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2194 snd_assert(pcm_oss_file != NULL, return -ENXIO);
2195 for (cidx = 0; cidx < 2; ++cidx) {
2196 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2198 snd_pcm_release_substream(substream);
2200 kfree(pcm_oss_file);
2204 static int snd_pcm_oss_open_file(struct file *file,
2205 struct snd_pcm *pcm,
2206 struct snd_pcm_oss_file **rpcm_oss_file,
2208 struct snd_pcm_oss_setup *setup)
2211 struct snd_pcm_oss_file *pcm_oss_file;
2212 struct snd_pcm_substream *substream;
2213 unsigned int f_mode = file->f_mode;
2215 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2216 *rpcm_oss_file = NULL;
2218 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2219 if (pcm_oss_file == NULL)
2222 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2223 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2224 f_mode = FMODE_WRITE;
2226 for (idx = 0; idx < 2; idx++) {
2227 if (setup[idx].disable)
2229 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2230 if (! (f_mode & FMODE_WRITE))
2233 if (! (f_mode & FMODE_READ))
2236 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2238 snd_pcm_oss_release_file(pcm_oss_file);
2242 pcm_oss_file->streams[idx] = substream;
2243 substream->file = pcm_oss_file;
2244 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2247 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2248 snd_pcm_oss_release_file(pcm_oss_file);
2252 file->private_data = pcm_oss_file;
2253 *rpcm_oss_file = pcm_oss_file;
2258 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2262 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2263 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2264 name[idx] = task->comm[idx];
2269 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2273 struct snd_pcm *pcm;
2274 struct snd_pcm_oss_file *pcm_oss_file;
2275 struct snd_pcm_oss_setup setup[2];
2279 pcm = snd_lookup_oss_minor_data(iminor(inode),
2280 SNDRV_OSS_DEVICE_TYPE_PCM);
2285 err = snd_card_file_add(pcm->card, file);
2288 if (!try_module_get(pcm->card->module)) {
2292 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2296 memset(setup, 0, sizeof(setup));
2297 if (file->f_mode & FMODE_WRITE)
2298 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2299 task_name, &setup[0]);
2300 if (file->f_mode & FMODE_READ)
2301 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2302 task_name, &setup[1]);
2304 nonblock = !!(file->f_flags & O_NONBLOCK);
2306 nonblock = nonblock_open;
2308 init_waitqueue_entry(&wait, current);
2309 add_wait_queue(&pcm->open_wait, &wait);
2310 mutex_lock(&pcm->open_mutex);
2312 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2313 iminor(inode), setup);
2316 if (err == -EAGAIN) {
2323 set_current_state(TASK_INTERRUPTIBLE);
2324 mutex_unlock(&pcm->open_mutex);
2326 mutex_lock(&pcm->open_mutex);
2327 if (signal_pending(current)) {
2332 remove_wait_queue(&pcm->open_wait, &wait);
2333 mutex_unlock(&pcm->open_mutex);
2339 module_put(pcm->card->module);
2341 snd_card_file_remove(pcm->card, file);
2346 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2348 struct snd_pcm *pcm;
2349 struct snd_pcm_substream *substream;
2350 struct snd_pcm_oss_file *pcm_oss_file;
2352 pcm_oss_file = file->private_data;
2353 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2354 if (substream == NULL)
2355 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2356 snd_assert(substream != NULL, return -ENXIO);
2357 pcm = substream->pcm;
2358 snd_pcm_oss_sync(pcm_oss_file);
2359 mutex_lock(&pcm->open_mutex);
2360 snd_pcm_oss_release_file(pcm_oss_file);
2361 mutex_unlock(&pcm->open_mutex);
2362 wake_up(&pcm->open_wait);
2363 module_put(pcm->card->module);
2364 snd_card_file_remove(pcm->card, file);
2368 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2370 struct snd_pcm_oss_file *pcm_oss_file;
2371 int __user *p = (int __user *)arg;
2374 pcm_oss_file = file->private_data;
2375 if (cmd == OSS_GETVERSION)
2376 return put_user(SNDRV_OSS_VERSION, p);
2377 if (cmd == OSS_ALSAEMULVER)
2378 return put_user(1, p);
2379 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2380 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2381 struct snd_pcm_substream *substream;
2383 for (idx = 0; idx < 2; ++idx) {
2384 substream = pcm_oss_file->streams[idx];
2385 if (substream != NULL)
2388 snd_assert(substream != NULL, return -ENXIO);
2389 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2392 if (((cmd >> 8) & 0xff) != 'P')
2395 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2398 case SNDCTL_DSP_RESET:
2399 return snd_pcm_oss_reset(pcm_oss_file);
2400 case SNDCTL_DSP_SYNC:
2401 return snd_pcm_oss_sync(pcm_oss_file);
2402 case SNDCTL_DSP_SPEED:
2403 if (get_user(res, p))
2405 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2407 return put_user(res, p);
2408 case SOUND_PCM_READ_RATE:
2409 res = snd_pcm_oss_get_rate(pcm_oss_file);
2412 return put_user(res, p);
2413 case SNDCTL_DSP_STEREO:
2414 if (get_user(res, p))
2416 res = res > 0 ? 2 : 1;
2417 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2419 return put_user(--res, p);
2420 case SNDCTL_DSP_GETBLKSIZE:
2421 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2424 return put_user(res, p);
2425 case SNDCTL_DSP_SETFMT:
2426 if (get_user(res, p))
2428 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2431 return put_user(res, p);
2432 case SOUND_PCM_READ_BITS:
2433 res = snd_pcm_oss_get_format(pcm_oss_file);
2436 return put_user(res, p);
2437 case SNDCTL_DSP_CHANNELS:
2438 if (get_user(res, p))
2440 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2443 return put_user(res, p);
2444 case SOUND_PCM_READ_CHANNELS:
2445 res = snd_pcm_oss_get_channels(pcm_oss_file);
2448 return put_user(res, p);
2449 case SOUND_PCM_WRITE_FILTER:
2450 case SOUND_PCM_READ_FILTER:
2452 case SNDCTL_DSP_POST:
2453 return snd_pcm_oss_post(pcm_oss_file);
2454 case SNDCTL_DSP_SUBDIVIDE:
2455 if (get_user(res, p))
2457 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2460 return put_user(res, p);
2461 case SNDCTL_DSP_SETFRAGMENT:
2462 if (get_user(res, p))
2464 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2465 case SNDCTL_DSP_GETFMTS:
2466 res = snd_pcm_oss_get_formats(pcm_oss_file);
2469 return put_user(res, p);
2470 case SNDCTL_DSP_GETOSPACE:
2471 case SNDCTL_DSP_GETISPACE:
2472 return snd_pcm_oss_get_space(pcm_oss_file,
2473 cmd == SNDCTL_DSP_GETISPACE ?
2474 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2475 (struct audio_buf_info __user *) arg);
2476 case SNDCTL_DSP_NONBLOCK:
2477 return snd_pcm_oss_nonblock(file);
2478 case SNDCTL_DSP_GETCAPS:
2479 res = snd_pcm_oss_get_caps(pcm_oss_file);
2482 return put_user(res, p);
2483 case SNDCTL_DSP_GETTRIGGER:
2484 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2487 return put_user(res, p);
2488 case SNDCTL_DSP_SETTRIGGER:
2489 if (get_user(res, p))
2491 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2492 case SNDCTL_DSP_GETIPTR:
2493 case SNDCTL_DSP_GETOPTR:
2494 return snd_pcm_oss_get_ptr(pcm_oss_file,
2495 cmd == SNDCTL_DSP_GETIPTR ?
2496 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2497 (struct count_info __user *) arg);
2498 case SNDCTL_DSP_MAPINBUF:
2499 case SNDCTL_DSP_MAPOUTBUF:
2500 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2501 cmd == SNDCTL_DSP_MAPINBUF ?
2502 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2503 (struct buffmem_desc __user *) arg);
2504 case SNDCTL_DSP_SETSYNCRO:
2505 /* stop DMA now.. */
2507 case SNDCTL_DSP_SETDUPLEX:
2508 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2511 case SNDCTL_DSP_GETODELAY:
2512 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2514 /* it's for sure, some broken apps don't check for error codes */
2518 return put_user(res, p);
2519 case SNDCTL_DSP_PROFILE:
2520 return 0; /* silently ignore */
2522 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2527 #ifdef CONFIG_COMPAT
2528 /* all compatible */
2529 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2531 #define snd_pcm_oss_ioctl_compat NULL
2534 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2536 struct snd_pcm_oss_file *pcm_oss_file;
2537 struct snd_pcm_substream *substream;
2539 pcm_oss_file = file->private_data;
2540 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2541 if (substream == NULL)
2544 return snd_pcm_oss_read1(substream, buf, count);
2547 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2548 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2554 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2556 struct snd_pcm_oss_file *pcm_oss_file;
2557 struct snd_pcm_substream *substream;
2560 pcm_oss_file = file->private_data;
2561 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2562 if (substream == NULL)
2564 result = snd_pcm_oss_write1(substream, buf, count);
2566 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2571 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2573 struct snd_pcm_runtime *runtime = substream->runtime;
2574 if (atomic_read(&runtime->mmap_count))
2575 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2577 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2580 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2582 struct snd_pcm_runtime *runtime = substream->runtime;
2583 if (atomic_read(&runtime->mmap_count))
2584 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2586 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2589 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2591 struct snd_pcm_oss_file *pcm_oss_file;
2593 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2595 pcm_oss_file = file->private_data;
2597 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2598 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2601 if (psubstream != NULL) {
2602 struct snd_pcm_runtime *runtime = psubstream->runtime;
2603 poll_wait(file, &runtime->sleep, wait);
2604 snd_pcm_stream_lock_irq(psubstream);
2605 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2606 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2607 snd_pcm_oss_playback_ready(psubstream)))
2608 mask |= POLLOUT | POLLWRNORM;
2609 snd_pcm_stream_unlock_irq(psubstream);
2611 if (csubstream != NULL) {
2612 struct snd_pcm_runtime *runtime = csubstream->runtime;
2613 snd_pcm_state_t ostate;
2614 poll_wait(file, &runtime->sleep, wait);
2615 snd_pcm_stream_lock_irq(csubstream);
2616 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2617 snd_pcm_oss_capture_ready(csubstream))
2618 mask |= POLLIN | POLLRDNORM;
2619 snd_pcm_stream_unlock_irq(csubstream);
2620 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2621 struct snd_pcm_oss_file ofile;
2622 memset(&ofile, 0, sizeof(ofile));
2623 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2624 runtime->oss.trigger = 0;
2625 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2632 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2634 struct snd_pcm_oss_file *pcm_oss_file;
2635 struct snd_pcm_substream *substream = NULL;
2636 struct snd_pcm_runtime *runtime;
2640 printk("pcm_oss: mmap begin\n");
2642 pcm_oss_file = file->private_data;
2643 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2644 case VM_READ | VM_WRITE:
2645 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2650 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2653 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2658 /* set VM_READ access as well to fix memset() routines that do
2659 reads before writes (to improve performance) */
2660 area->vm_flags |= VM_READ;
2661 if (substream == NULL)
2663 runtime = substream->runtime;
2664 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2666 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2667 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2671 if (runtime->oss.params) {
2672 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2675 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2676 if (runtime->oss.plugin_first != NULL)
2680 if (area->vm_pgoff != 0)
2683 err = snd_pcm_mmap_data(substream, file, area);
2686 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2687 runtime->silence_threshold = 0;
2688 runtime->silence_size = 0;
2690 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2692 /* In mmap mode we never stop */
2693 runtime->stop_threshold = runtime->boundary;
2698 #ifdef CONFIG_SND_VERBOSE_PROCFS
2703 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2704 struct snd_info_buffer *buffer)
2706 struct snd_pcm_str *pstr = entry->private_data;
2707 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2708 mutex_lock(&pstr->oss.setup_mutex);
2710 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2714 setup->disable ? " disable" : "",
2715 setup->direct ? " direct" : "",
2716 setup->block ? " block" : "",
2717 setup->nonblock ? " non-block" : "",
2718 setup->partialfrag ? " partial-frag" : "",
2719 setup->nosilence ? " no-silence" : "");
2720 setup = setup->next;
2722 mutex_unlock(&pstr->oss.setup_mutex);
2725 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2727 struct snd_pcm_oss_setup *setup, *setupn;
2729 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2730 setup; setup = setupn) {
2731 setupn = setup->next;
2732 kfree(setup->task_name);
2735 pstr->oss.setup_list = NULL;
2738 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2739 struct snd_info_buffer *buffer)
2741 struct snd_pcm_str *pstr = entry->private_data;
2742 char line[128], str[32], task_name[32], *ptr;
2744 struct snd_pcm_oss_setup *setup, *setup1, template;
2746 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2747 mutex_lock(&pstr->oss.setup_mutex);
2748 memset(&template, 0, sizeof(template));
2749 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2750 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2751 snd_pcm_oss_proc_free_setup_list(pstr);
2752 mutex_unlock(&pstr->oss.setup_mutex);
2755 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2756 if (!strcmp(setup->task_name, task_name)) {
2761 ptr = snd_info_get_str(str, ptr, sizeof(str));
2762 template.periods = simple_strtoul(str, NULL, 10);
2763 ptr = snd_info_get_str(str, ptr, sizeof(str));
2764 template.period_size = simple_strtoul(str, NULL, 10);
2765 for (idx1 = 31; idx1 >= 0; idx1--)
2766 if (template.period_size & (1 << idx1))
2768 for (idx1--; idx1 >= 0; idx1--)
2769 template.period_size &= ~(1 << idx1);
2771 ptr = snd_info_get_str(str, ptr, sizeof(str));
2772 if (!strcmp(str, "disable")) {
2773 template.disable = 1;
2774 } else if (!strcmp(str, "direct")) {
2775 template.direct = 1;
2776 } else if (!strcmp(str, "block")) {
2778 } else if (!strcmp(str, "non-block")) {
2779 template.nonblock = 1;
2780 } else if (!strcmp(str, "partial-frag")) {
2781 template.partialfrag = 1;
2782 } else if (!strcmp(str, "no-silence")) {
2783 template.nosilence = 1;
2784 } else if (!strcmp(str, "buggy-ptr")) {
2785 template.buggyptr = 1;
2788 if (setup == NULL) {
2789 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2791 buffer->error = -ENOMEM;
2792 mutex_lock(&pstr->oss.setup_mutex);
2795 if (pstr->oss.setup_list == NULL)
2796 pstr->oss.setup_list = setup;
2798 for (setup1 = pstr->oss.setup_list;
2799 setup1->next; setup1 = setup1->next);
2800 setup1->next = setup;
2802 template.task_name = kstrdup(task_name, GFP_KERNEL);
2803 if (! template.task_name) {
2805 buffer->error = -ENOMEM;
2806 mutex_lock(&pstr->oss.setup_mutex);
2811 mutex_unlock(&pstr->oss.setup_mutex);
2815 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2818 for (stream = 0; stream < 2; ++stream) {
2819 struct snd_info_entry *entry;
2820 struct snd_pcm_str *pstr = &pcm->streams[stream];
2821 if (pstr->substream_count == 0)
2823 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2824 entry->content = SNDRV_INFO_CONTENT_TEXT;
2825 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2826 entry->c.text.read = snd_pcm_oss_proc_read;
2827 entry->c.text.write = snd_pcm_oss_proc_write;
2828 entry->private_data = pstr;
2829 if (snd_info_register(entry) < 0) {
2830 snd_info_free_entry(entry);
2834 pstr->oss.proc_entry = entry;
2838 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2841 for (stream = 0; stream < 2; ++stream) {
2842 struct snd_pcm_str *pstr = &pcm->streams[stream];
2843 if (pstr->oss.proc_entry) {
2844 snd_info_unregister(pstr->oss.proc_entry);
2845 pstr->oss.proc_entry = NULL;
2846 snd_pcm_oss_proc_free_setup_list(pstr);
2850 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2851 #define snd_pcm_oss_proc_init(pcm)
2852 #define snd_pcm_oss_proc_done(pcm)
2853 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2859 static struct file_operations snd_pcm_oss_f_reg =
2861 .owner = THIS_MODULE,
2862 .read = snd_pcm_oss_read,
2863 .write = snd_pcm_oss_write,
2864 .open = snd_pcm_oss_open,
2865 .release = snd_pcm_oss_release,
2866 .poll = snd_pcm_oss_poll,
2867 .unlocked_ioctl = snd_pcm_oss_ioctl,
2868 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2869 .mmap = snd_pcm_oss_mmap,
2872 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2875 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2876 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2877 pcm->card, index, &snd_pcm_oss_f_reg,
2879 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2880 pcm->card->number, pcm->device);
2884 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2887 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2890 register_oss_dsp(pcm, 0);
2891 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2892 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2893 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2894 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2895 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2896 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2901 pcm->oss.reg_mask |= 1;
2903 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2904 register_oss_dsp(pcm, 1);
2906 pcm->oss.reg_mask |= 2;
2910 snd_pcm_oss_proc_init(pcm);
2915 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2918 if (pcm->oss.reg_mask & 1) {
2919 pcm->oss.reg_mask &= ~1;
2920 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2923 if (pcm->oss.reg_mask & 2) {
2924 pcm->oss.reg_mask &= ~2;
2925 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2932 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2934 snd_pcm_oss_disconnect_minor(pcm);
2936 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2937 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2938 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2942 snd_pcm_oss_proc_done(pcm);
2947 static struct snd_pcm_notify snd_pcm_oss_notify =
2949 .n_register = snd_pcm_oss_register_minor,
2950 .n_disconnect = snd_pcm_oss_disconnect_minor,
2951 .n_unregister = snd_pcm_oss_unregister_minor,
2954 static int __init alsa_pcm_oss_init(void)
2959 /* check device map table */
2960 for (i = 0; i < SNDRV_CARDS; i++) {
2961 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2962 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2966 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2967 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
2972 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2977 static void __exit alsa_pcm_oss_exit(void)
2979 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2982 module_init(alsa_pcm_oss_init)
2983 module_exit(alsa_pcm_oss_exit)