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 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(&substream->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(&substream->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(&substream->mmap_count) ||
951 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
952 sw_params->stop_threshold = runtime->boundary;
954 sw_params->stop_threshold = runtime->buffer_size;
955 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
956 sw_params->period_step = 1;
957 sw_params->sleep_min = 0;
958 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
959 1 : runtime->period_size;
960 sw_params->xfer_align = 1;
961 if (atomic_read(&substream->mmap_count) ||
962 substream->oss.setup.nosilence) {
963 sw_params->silence_threshold = 0;
964 sw_params->silence_size = 0;
966 snd_pcm_uframes_t frames;
967 frames = runtime->period_size + 16;
968 if (frames > runtime->buffer_size)
969 frames = runtime->buffer_size;
970 sw_params->silence_threshold = frames;
971 sw_params->silence_size = frames;
974 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
975 snd_printd("SW_PARAMS failed: %i\n", err);
979 runtime->oss.periods = params_periods(sparams);
980 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
981 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
982 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
983 if (runtime->oss.plugin_first) {
984 err = snd_pcm_plug_alloc(substream, oss_period_size);
989 oss_period_size *= oss_frame_size;
991 oss_buffer_size = oss_period_size * runtime->oss.periods;
992 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
994 runtime->oss.period_bytes = oss_period_size;
995 runtime->oss.buffer_bytes = oss_buffer_size;
997 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
998 runtime->oss.period_bytes,
999 runtime->oss.buffer_bytes);
1000 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1001 params_period_size(sparams),
1002 params_buffer_size(sparams));
1004 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1005 runtime->oss.channels = params_channels(params);
1006 runtime->oss.rate = params_rate(params);
1008 runtime->oss.params = 0;
1009 runtime->oss.prepare = 1;
1010 vfree(runtime->oss.buffer);
1011 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1012 runtime->oss.buffer_used = 0;
1013 if (runtime->dma_area)
1014 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1016 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1026 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1029 struct snd_pcm_substream *asubstream = NULL, *substream;
1031 for (idx = 0; idx < 2; idx++) {
1032 substream = pcm_oss_file->streams[idx];
1033 if (substream == NULL)
1035 if (asubstream == NULL)
1036 asubstream = substream;
1037 if (substream->runtime->oss.params) {
1038 err = snd_pcm_oss_change_params(substream);
1043 snd_assert(asubstream != NULL, return -EIO);
1045 *r_substream = asubstream;
1049 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1052 struct snd_pcm_runtime *runtime = substream->runtime;
1054 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1056 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1059 runtime->oss.prepare = 0;
1060 runtime->oss.prev_hw_ptr_interrupt = 0;
1061 runtime->oss.period_ptr = 0;
1062 runtime->oss.buffer_used = 0;
1067 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1069 struct snd_pcm_runtime *runtime;
1072 if (substream == NULL)
1074 runtime = substream->runtime;
1075 if (runtime->oss.params) {
1076 err = snd_pcm_oss_change_params(substream);
1080 if (runtime->oss.prepare) {
1081 err = snd_pcm_oss_prepare(substream);
1088 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1090 struct snd_pcm_runtime *runtime;
1091 snd_pcm_uframes_t frames;
1095 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1098 runtime = substream->runtime;
1099 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1101 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1102 /* until avail(delay) <= buffer_size */
1103 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1104 frames /= runtime->period_size;
1105 frames *= runtime->period_size;
1106 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1113 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1115 struct snd_pcm_runtime *runtime = substream->runtime;
1118 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1119 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1121 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1122 printk("pcm_oss: write: recovering from XRUN\n");
1124 printk("pcm_oss: write: recovering from SUSPEND\n");
1126 ret = snd_pcm_oss_prepare(substream);
1132 fs = snd_enter_user();
1133 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1136 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1138 if (ret != -EPIPE && ret != -ESTRPIPE)
1140 /* test, if we can't store new data, because the stream */
1141 /* has not been started */
1142 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1148 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1150 struct snd_pcm_runtime *runtime = substream->runtime;
1151 snd_pcm_sframes_t delay;
1154 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1155 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1157 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1158 printk("pcm_oss: read: recovering from XRUN\n");
1160 printk("pcm_oss: read: recovering from SUSPEND\n");
1162 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1165 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1166 ret = snd_pcm_oss_prepare(substream);
1170 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1175 fs = snd_enter_user();
1176 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1179 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1181 if (ret == -EPIPE) {
1182 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1183 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1189 if (ret != -ESTRPIPE)
1195 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1197 struct snd_pcm_runtime *runtime = substream->runtime;
1200 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1201 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1203 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1204 printk("pcm_oss: writev: recovering from XRUN\n");
1206 printk("pcm_oss: writev: recovering from SUSPEND\n");
1208 ret = snd_pcm_oss_prepare(substream);
1214 fs = snd_enter_user();
1215 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1218 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1220 if (ret != -EPIPE && ret != -ESTRPIPE)
1223 /* test, if we can't store new data, because the stream */
1224 /* has not been started */
1225 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1231 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1236 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1237 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1239 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1240 printk("pcm_oss: readv: recovering from XRUN\n");
1242 printk("pcm_oss: readv: recovering from SUSPEND\n");
1244 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1247 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1248 ret = snd_pcm_oss_prepare(substream);
1254 fs = snd_enter_user();
1255 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1258 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1260 if (ret != -EPIPE && ret != -ESTRPIPE)
1266 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1268 struct snd_pcm_runtime *runtime = substream->runtime;
1269 snd_pcm_sframes_t frames, frames1;
1270 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1271 if (runtime->oss.plugin_first) {
1272 struct snd_pcm_plugin_channel *channels;
1273 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1275 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1277 buf = runtime->oss.buffer;
1279 frames = bytes / oss_frame_bytes;
1280 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1283 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1286 bytes = frames1 * oss_frame_bytes;
1290 frames = bytes_to_frames(runtime, bytes);
1291 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1294 bytes = frames_to_bytes(runtime, frames1);
1299 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1303 struct snd_pcm_runtime *runtime = substream->runtime;
1305 if (atomic_read(&substream->mmap_count))
1308 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1311 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1313 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1314 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1316 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
1317 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1319 runtime->oss.buffer_used += tmp;
1323 if (substream->oss.setup.partialfrag ||
1324 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1325 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1326 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1328 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1329 runtime->oss.bytes += tmp;
1330 runtime->oss.period_ptr += tmp;
1331 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1332 if (runtime->oss.period_ptr == 0 ||
1333 runtime->oss.period_ptr == runtime->oss.buffer_used)
1334 runtime->oss.buffer_used = 0;
1335 else if ((substream->f_flags & O_NONBLOCK) != 0)
1336 return xfer > 0 ? xfer : -EAGAIN;
1339 tmp = snd_pcm_oss_write2(substream,
1340 (const char __force *)buf,
1341 runtime->oss.period_bytes, 0);
1343 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1344 runtime->oss.bytes += tmp;
1348 if ((substream->f_flags & O_NONBLOCK) != 0 &&
1349 tmp != runtime->oss.period_bytes)
1356 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1358 struct snd_pcm_runtime *runtime = substream->runtime;
1359 snd_pcm_sframes_t frames, frames1;
1360 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1361 char __user *final_dst = (char __user *)buf;
1362 if (runtime->oss.plugin_first) {
1363 struct snd_pcm_plugin_channel *channels;
1364 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1366 buf = runtime->oss.buffer;
1367 frames = bytes / oss_frame_bytes;
1368 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1371 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1374 bytes = frames1 * oss_frame_bytes;
1375 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1380 frames = bytes_to_frames(runtime, bytes);
1381 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1384 bytes = frames_to_bytes(runtime, frames1);
1389 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1393 struct snd_pcm_runtime *runtime = substream->runtime;
1395 if (atomic_read(&substream->mmap_count))
1398 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1401 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1402 if (runtime->oss.buffer_used == 0) {
1403 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1405 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1406 runtime->oss.bytes += tmp;
1407 runtime->oss.period_ptr = tmp;
1408 runtime->oss.buffer_used = tmp;
1411 if ((size_t) tmp > runtime->oss.buffer_used)
1412 tmp = runtime->oss.buffer_used;
1413 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
1414 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1418 runtime->oss.buffer_used -= tmp;
1420 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1421 runtime->oss.period_bytes, 0);
1423 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1424 runtime->oss.bytes += tmp;
1433 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1435 struct snd_pcm_substream *substream;
1437 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1438 if (substream != NULL) {
1439 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1440 substream->runtime->oss.prepare = 1;
1442 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1443 if (substream != NULL) {
1444 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1445 substream->runtime->oss.prepare = 1;
1450 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1452 struct snd_pcm_substream *substream;
1455 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1456 if (substream != NULL) {
1457 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1459 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1461 /* note: all errors from the start action are ignored */
1462 /* OSS apps do not know, how to handle them */
1466 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1468 struct snd_pcm_runtime *runtime;
1473 runtime = substream->runtime;
1474 init_waitqueue_entry(&wait, current);
1475 add_wait_queue(&runtime->sleep, &wait);
1477 printk("sync1: size = %li\n", size);
1480 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1482 runtime->oss.buffer_used = 0;
1486 if (result != 0 && result != -EAGAIN)
1489 set_current_state(TASK_INTERRUPTIBLE);
1490 snd_pcm_stream_lock_irq(substream);
1491 res = runtime->status->state;
1492 snd_pcm_stream_unlock_irq(substream);
1493 if (res != SNDRV_PCM_STATE_RUNNING) {
1494 set_current_state(TASK_RUNNING);
1497 res = schedule_timeout(10 * HZ);
1498 if (signal_pending(current)) {
1499 result = -ERESTARTSYS;
1503 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1508 remove_wait_queue(&runtime->sleep, &wait);
1512 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1515 unsigned int saved_f_flags;
1516 struct snd_pcm_substream *substream;
1517 struct snd_pcm_runtime *runtime;
1518 snd_pcm_format_t format;
1519 unsigned long width;
1522 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1523 if (substream != NULL) {
1524 runtime = substream->runtime;
1525 if (atomic_read(&substream->mmap_count))
1527 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1529 format = snd_pcm_oss_format_from(runtime->oss.format);
1530 width = snd_pcm_format_physical_width(format);
1531 if (runtime->oss.buffer_used > 0) {
1533 printk("sync: buffer_used\n");
1535 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1536 snd_pcm_format_set_silence(format,
1537 runtime->oss.buffer + runtime->oss.buffer_used,
1539 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1542 } else if (runtime->oss.period_ptr > 0) {
1544 printk("sync: period_ptr\n");
1546 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1547 snd_pcm_format_set_silence(format,
1548 runtime->oss.buffer,
1550 err = snd_pcm_oss_sync1(substream, size);
1555 * The ALSA's period might be a bit large than OSS one.
1556 * Fill the remain portion of ALSA period with zeros.
1558 size = runtime->control->appl_ptr % runtime->period_size;
1560 size = runtime->period_size - size;
1561 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1562 size = (runtime->frame_bits * size) / 8;
1565 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1568 size1 /= runtime->sample_bits;
1569 snd_pcm_format_set_silence(runtime->format,
1570 runtime->oss.buffer,
1572 fs = snd_enter_user();
1573 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1576 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1577 void __user *buffers[runtime->channels];
1578 memset(buffers, 0, runtime->channels * sizeof(void *));
1579 snd_pcm_lib_writev(substream, buffers, size);
1583 * finish sync: drain the buffer
1586 saved_f_flags = substream->f_flags;
1587 substream->f_flags &= ~O_NONBLOCK;
1588 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1589 substream->f_flags = saved_f_flags;
1592 runtime->oss.prepare = 1;
1595 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1596 if (substream != NULL) {
1597 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1599 runtime = substream->runtime;
1600 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1603 runtime->oss.buffer_used = 0;
1604 runtime->oss.prepare = 1;
1609 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1613 for (idx = 1; idx >= 0; --idx) {
1614 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1615 struct snd_pcm_runtime *runtime;
1616 if (substream == NULL)
1618 runtime = substream->runtime;
1621 else if (rate > 192000)
1623 if (runtime->oss.rate != rate) {
1624 runtime->oss.params = 1;
1625 runtime->oss.rate = rate;
1628 return snd_pcm_oss_get_rate(pcm_oss_file);
1631 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1633 struct snd_pcm_substream *substream;
1636 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1638 return substream->runtime->oss.rate;
1641 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1648 for (idx = 1; idx >= 0; --idx) {
1649 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1650 struct snd_pcm_runtime *runtime;
1651 if (substream == NULL)
1653 runtime = substream->runtime;
1654 if (runtime->oss.channels != channels) {
1655 runtime->oss.params = 1;
1656 runtime->oss.channels = channels;
1659 return snd_pcm_oss_get_channels(pcm_oss_file);
1662 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1664 struct snd_pcm_substream *substream;
1667 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1669 return substream->runtime->oss.channels;
1672 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1674 struct snd_pcm_substream *substream;
1677 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1679 return substream->runtime->oss.period_bytes;
1682 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1684 struct snd_pcm_substream *substream;
1687 struct snd_pcm_hw_params *params;
1688 unsigned int formats = 0;
1689 struct snd_mask format_mask;
1692 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1694 if (atomic_read(&substream->mmap_count))
1697 direct = substream->oss.setup.direct;
1699 return AFMT_MU_LAW | AFMT_U8 |
1700 AFMT_S16_LE | AFMT_S16_BE |
1701 AFMT_S8 | AFMT_U16_LE |
1703 params = kmalloc(sizeof(*params), GFP_KERNEL);
1706 _snd_pcm_hw_params_any(params);
1707 err = snd_pcm_hw_refine(substream, params);
1708 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1710 snd_assert(err >= 0, return err);
1711 for (fmt = 0; fmt < 32; ++fmt) {
1712 if (snd_mask_test(&format_mask, fmt)) {
1713 int f = snd_pcm_oss_format_to(fmt);
1721 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1725 if (format != AFMT_QUERY) {
1726 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1729 if (!(formats & format))
1731 for (idx = 1; idx >= 0; --idx) {
1732 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1733 struct snd_pcm_runtime *runtime;
1734 if (substream == NULL)
1736 runtime = substream->runtime;
1737 if (runtime->oss.format != format) {
1738 runtime->oss.params = 1;
1739 runtime->oss.format = format;
1743 return snd_pcm_oss_get_format(pcm_oss_file);
1746 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1748 struct snd_pcm_substream *substream;
1751 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1753 return substream->runtime->oss.format;
1756 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1758 struct snd_pcm_runtime *runtime;
1760 if (substream == NULL)
1762 runtime = substream->runtime;
1763 if (subdivide == 0) {
1764 subdivide = runtime->oss.subdivision;
1769 if (runtime->oss.subdivision || runtime->oss.fragshift)
1771 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1772 subdivide != 8 && subdivide != 16)
1774 runtime->oss.subdivision = subdivide;
1775 runtime->oss.params = 1;
1779 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1781 int err = -EINVAL, idx;
1783 for (idx = 1; idx >= 0; --idx) {
1784 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1785 if (substream == NULL)
1787 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1793 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1795 struct snd_pcm_runtime *runtime;
1797 if (substream == NULL)
1799 runtime = substream->runtime;
1800 if (runtime->oss.subdivision || runtime->oss.fragshift)
1802 runtime->oss.fragshift = val & 0xffff;
1803 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1804 if (runtime->oss.fragshift < 4) /* < 16 */
1805 runtime->oss.fragshift = 4;
1806 if (runtime->oss.maxfrags < 2)
1807 runtime->oss.maxfrags = 2;
1808 runtime->oss.params = 1;
1812 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1814 int err = -EINVAL, idx;
1816 for (idx = 1; idx >= 0; --idx) {
1817 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1818 if (substream == NULL)
1820 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1826 static int snd_pcm_oss_nonblock(struct file * file)
1828 file->f_flags |= O_NONBLOCK;
1832 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1835 if (substream == NULL) {
1836 res &= ~DSP_CAP_DUPLEX;
1839 #ifdef DSP_CAP_MULTI
1840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1841 if (substream->pstr->substream_count > 1)
1842 res |= DSP_CAP_MULTI;
1844 /* DSP_CAP_REALTIME is set all times: */
1845 /* all ALSA drivers can return actual pointer in ring buffer */
1846 #if defined(DSP_CAP_REALTIME) && 0
1848 struct snd_pcm_runtime *runtime = substream->runtime;
1849 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1850 res &= ~DSP_CAP_REALTIME;
1856 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1860 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1861 for (idx = 0; idx < 2; idx++) {
1862 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1863 result = snd_pcm_oss_get_caps1(substream, result);
1865 result |= 0x0001; /* revision - same as SB AWE 64 */
1869 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1871 struct snd_pcm_runtime *runtime = substream->runtime;
1872 snd_pcm_uframes_t appl_ptr;
1873 appl_ptr = hw_ptr + runtime->buffer_size;
1874 appl_ptr %= runtime->boundary;
1875 runtime->control->appl_ptr = appl_ptr;
1878 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1880 struct snd_pcm_runtime *runtime;
1881 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1885 printk("pcm_oss: trigger = 0x%x\n", trigger);
1888 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1889 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1892 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1896 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1900 runtime = psubstream->runtime;
1901 if (trigger & PCM_ENABLE_OUTPUT) {
1902 if (runtime->oss.trigger)
1904 if (atomic_read(&psubstream->mmap_count))
1905 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1906 runtime->oss.trigger = 1;
1907 runtime->start_threshold = 1;
1908 cmd = SNDRV_PCM_IOCTL_START;
1910 if (!runtime->oss.trigger)
1912 runtime->oss.trigger = 0;
1913 runtime->start_threshold = runtime->boundary;
1914 cmd = SNDRV_PCM_IOCTL_DROP;
1915 runtime->oss.prepare = 1;
1917 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
1923 runtime = csubstream->runtime;
1924 if (trigger & PCM_ENABLE_INPUT) {
1925 if (runtime->oss.trigger)
1927 runtime->oss.trigger = 1;
1928 runtime->start_threshold = 1;
1929 cmd = SNDRV_PCM_IOCTL_START;
1931 if (!runtime->oss.trigger)
1933 runtime->oss.trigger = 0;
1934 runtime->start_threshold = runtime->boundary;
1935 cmd = SNDRV_PCM_IOCTL_DROP;
1936 runtime->oss.prepare = 1;
1938 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
1946 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1948 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1951 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1952 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1953 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1954 result |= PCM_ENABLE_OUTPUT;
1955 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1956 result |= PCM_ENABLE_INPUT;
1960 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1962 struct snd_pcm_substream *substream;
1963 struct snd_pcm_runtime *runtime;
1964 snd_pcm_sframes_t delay;
1967 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1968 if (substream == NULL)
1970 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1972 runtime = substream->runtime;
1973 if (runtime->oss.params || runtime->oss.prepare)
1975 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1977 delay = 0; /* hack for broken OSS applications */
1980 return snd_pcm_oss_bytes(substream, delay);
1983 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
1985 struct snd_pcm_substream *substream;
1986 struct snd_pcm_runtime *runtime;
1987 snd_pcm_sframes_t delay;
1989 struct count_info info;
1994 substream = pcm_oss_file->streams[stream];
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) {
2001 memset(&info, 0, sizeof(info));
2002 if (copy_to_user(_info, &info, sizeof(info)))
2006 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2007 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2008 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2013 fixup = runtime->oss.buffer_used;
2016 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2017 fixup = -runtime->oss.buffer_used;
2021 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2022 if (atomic_read(&substream->mmap_count)) {
2023 snd_pcm_sframes_t n;
2024 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2026 n += runtime->boundary;
2027 info.blocks = n / runtime->period_size;
2028 runtime->oss.prev_hw_ptr_interrupt = delay;
2029 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2030 snd_pcm_oss_simulate_fill(substream, delay);
2031 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2033 delay = snd_pcm_oss_bytes(substream, delay);
2034 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2035 if (substream->oss.setup.buggyptr)
2036 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2038 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2039 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2042 info.blocks = delay / runtime->oss.period_bytes;
2043 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2046 if (copy_to_user(_info, &info, sizeof(info)))
2051 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2053 struct snd_pcm_substream *substream;
2054 struct snd_pcm_runtime *runtime;
2055 snd_pcm_sframes_t avail;
2057 struct audio_buf_info info;
2062 substream = pcm_oss_file->streams[stream];
2063 if (substream == NULL)
2065 runtime = substream->runtime;
2067 if (runtime->oss.params &&
2068 (err = snd_pcm_oss_change_params(substream)) < 0)
2071 info.fragsize = runtime->oss.period_bytes;
2072 info.fragstotal = runtime->periods;
2073 if (runtime->oss.prepare) {
2074 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2075 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2076 info.fragments = runtime->oss.periods;
2082 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2083 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2084 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2085 avail = runtime->buffer_size;
2089 avail = runtime->buffer_size - avail;
2090 fixup = -runtime->oss.buffer_used;
2093 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2094 fixup = runtime->oss.buffer_used;
2098 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2099 info.fragments = info.bytes / runtime->oss.period_bytes;
2103 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2105 if (copy_to_user(_info, &info, sizeof(info)))
2110 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2112 // it won't be probably implemented
2113 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2117 static const char *strip_task_path(const char *path)
2119 const char *ptr, *ptrl = NULL;
2120 for (ptr = path; *ptr; ptr++) {
2127 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2128 const char *task_name,
2129 struct snd_pcm_oss_setup *rsetup)
2131 struct snd_pcm_oss_setup *setup;
2133 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2135 for (setup = pcm->streams[stream].oss.setup_list; setup;
2136 setup = setup->next) {
2137 if (!strcmp(setup->task_name, task_name))
2140 } while ((task_name = strip_task_path(task_name)) != NULL);
2144 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2147 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2149 struct snd_pcm_runtime *runtime;
2150 runtime = substream->runtime;
2151 vfree(runtime->oss.buffer);
2152 runtime->oss.buffer = NULL;
2153 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2154 snd_pcm_oss_plugin_clear(substream);
2156 substream->oss.oss = 0;
2159 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2160 struct snd_pcm_oss_setup *setup,
2163 struct snd_pcm_runtime *runtime;
2165 substream->oss.oss = 1;
2166 substream->oss.setup = *setup;
2167 if (setup->nonblock)
2168 substream->f_flags |= O_NONBLOCK;
2169 else if (setup->block)
2170 substream->f_flags &= ~O_NONBLOCK;
2171 runtime = substream->runtime;
2172 runtime->oss.params = 1;
2173 runtime->oss.trigger = 1;
2174 runtime->oss.rate = 8000;
2175 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2176 case SNDRV_MINOR_OSS_PCM_8:
2177 runtime->oss.format = AFMT_U8;
2179 case SNDRV_MINOR_OSS_PCM_16:
2180 runtime->oss.format = AFMT_S16_LE;
2183 runtime->oss.format = AFMT_MU_LAW;
2185 runtime->oss.channels = 1;
2186 runtime->oss.fragshift = 0;
2187 runtime->oss.maxfrags = 0;
2188 runtime->oss.subdivision = 0;
2189 substream->pcm_release = snd_pcm_oss_release_substream;
2192 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2195 snd_assert(pcm_oss_file != NULL, return -ENXIO);
2196 for (cidx = 0; cidx < 2; ++cidx) {
2197 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2199 snd_pcm_release_substream(substream);
2201 kfree(pcm_oss_file);
2205 static int snd_pcm_oss_open_file(struct file *file,
2206 struct snd_pcm *pcm,
2207 struct snd_pcm_oss_file **rpcm_oss_file,
2209 struct snd_pcm_oss_setup *setup)
2212 struct snd_pcm_oss_file *pcm_oss_file;
2213 struct snd_pcm_substream *substream;
2214 unsigned int f_mode = file->f_mode;
2216 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2217 *rpcm_oss_file = NULL;
2219 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2220 if (pcm_oss_file == NULL)
2223 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2224 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2225 f_mode = FMODE_WRITE;
2227 file->f_flags &= ~O_APPEND;
2228 for (idx = 0; idx < 2; idx++) {
2229 if (setup[idx].disable)
2231 if (! pcm->streams[idx].substream_count)
2232 continue; /* no matching substream */
2233 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2234 if (! (f_mode & FMODE_WRITE))
2237 if (! (f_mode & FMODE_READ))
2240 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2242 snd_pcm_oss_release_file(pcm_oss_file);
2246 pcm_oss_file->streams[idx] = substream;
2247 substream->file = pcm_oss_file;
2248 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2251 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2252 snd_pcm_oss_release_file(pcm_oss_file);
2256 file->private_data = pcm_oss_file;
2257 *rpcm_oss_file = pcm_oss_file;
2262 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2266 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2267 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2268 name[idx] = task->comm[idx];
2273 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2277 struct snd_pcm *pcm;
2278 struct snd_pcm_oss_file *pcm_oss_file;
2279 struct snd_pcm_oss_setup setup[2];
2283 pcm = snd_lookup_oss_minor_data(iminor(inode),
2284 SNDRV_OSS_DEVICE_TYPE_PCM);
2289 err = snd_card_file_add(pcm->card, file);
2292 if (!try_module_get(pcm->card->module)) {
2296 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2300 memset(setup, 0, sizeof(setup));
2301 if (file->f_mode & FMODE_WRITE)
2302 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2303 task_name, &setup[0]);
2304 if (file->f_mode & FMODE_READ)
2305 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2306 task_name, &setup[1]);
2308 nonblock = !!(file->f_flags & O_NONBLOCK);
2310 nonblock = nonblock_open;
2312 init_waitqueue_entry(&wait, current);
2313 add_wait_queue(&pcm->open_wait, &wait);
2314 mutex_lock(&pcm->open_mutex);
2316 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2317 iminor(inode), setup);
2320 if (err == -EAGAIN) {
2327 set_current_state(TASK_INTERRUPTIBLE);
2328 mutex_unlock(&pcm->open_mutex);
2330 mutex_lock(&pcm->open_mutex);
2331 if (signal_pending(current)) {
2336 remove_wait_queue(&pcm->open_wait, &wait);
2337 mutex_unlock(&pcm->open_mutex);
2343 module_put(pcm->card->module);
2345 snd_card_file_remove(pcm->card, file);
2350 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2352 struct snd_pcm *pcm;
2353 struct snd_pcm_substream *substream;
2354 struct snd_pcm_oss_file *pcm_oss_file;
2356 pcm_oss_file = file->private_data;
2357 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2358 if (substream == NULL)
2359 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2360 snd_assert(substream != NULL, return -ENXIO);
2361 pcm = substream->pcm;
2362 snd_pcm_oss_sync(pcm_oss_file);
2363 mutex_lock(&pcm->open_mutex);
2364 snd_pcm_oss_release_file(pcm_oss_file);
2365 mutex_unlock(&pcm->open_mutex);
2366 wake_up(&pcm->open_wait);
2367 module_put(pcm->card->module);
2368 snd_card_file_remove(pcm->card, file);
2372 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2374 struct snd_pcm_oss_file *pcm_oss_file;
2375 int __user *p = (int __user *)arg;
2378 pcm_oss_file = file->private_data;
2379 if (cmd == OSS_GETVERSION)
2380 return put_user(SNDRV_OSS_VERSION, p);
2381 if (cmd == OSS_ALSAEMULVER)
2382 return put_user(1, p);
2383 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2384 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2385 struct snd_pcm_substream *substream;
2387 for (idx = 0; idx < 2; ++idx) {
2388 substream = pcm_oss_file->streams[idx];
2389 if (substream != NULL)
2392 snd_assert(substream != NULL, return -ENXIO);
2393 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2396 if (((cmd >> 8) & 0xff) != 'P')
2399 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2402 case SNDCTL_DSP_RESET:
2403 return snd_pcm_oss_reset(pcm_oss_file);
2404 case SNDCTL_DSP_SYNC:
2405 return snd_pcm_oss_sync(pcm_oss_file);
2406 case SNDCTL_DSP_SPEED:
2407 if (get_user(res, p))
2409 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2411 return put_user(res, p);
2412 case SOUND_PCM_READ_RATE:
2413 res = snd_pcm_oss_get_rate(pcm_oss_file);
2416 return put_user(res, p);
2417 case SNDCTL_DSP_STEREO:
2418 if (get_user(res, p))
2420 res = res > 0 ? 2 : 1;
2421 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2423 return put_user(--res, p);
2424 case SNDCTL_DSP_GETBLKSIZE:
2425 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2428 return put_user(res, p);
2429 case SNDCTL_DSP_SETFMT:
2430 if (get_user(res, p))
2432 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2435 return put_user(res, p);
2436 case SOUND_PCM_READ_BITS:
2437 res = snd_pcm_oss_get_format(pcm_oss_file);
2440 return put_user(res, p);
2441 case SNDCTL_DSP_CHANNELS:
2442 if (get_user(res, p))
2444 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2447 return put_user(res, p);
2448 case SOUND_PCM_READ_CHANNELS:
2449 res = snd_pcm_oss_get_channels(pcm_oss_file);
2452 return put_user(res, p);
2453 case SOUND_PCM_WRITE_FILTER:
2454 case SOUND_PCM_READ_FILTER:
2456 case SNDCTL_DSP_POST:
2457 return snd_pcm_oss_post(pcm_oss_file);
2458 case SNDCTL_DSP_SUBDIVIDE:
2459 if (get_user(res, p))
2461 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2464 return put_user(res, p);
2465 case SNDCTL_DSP_SETFRAGMENT:
2466 if (get_user(res, p))
2468 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2469 case SNDCTL_DSP_GETFMTS:
2470 res = snd_pcm_oss_get_formats(pcm_oss_file);
2473 return put_user(res, p);
2474 case SNDCTL_DSP_GETOSPACE:
2475 case SNDCTL_DSP_GETISPACE:
2476 return snd_pcm_oss_get_space(pcm_oss_file,
2477 cmd == SNDCTL_DSP_GETISPACE ?
2478 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2479 (struct audio_buf_info __user *) arg);
2480 case SNDCTL_DSP_NONBLOCK:
2481 return snd_pcm_oss_nonblock(file);
2482 case SNDCTL_DSP_GETCAPS:
2483 res = snd_pcm_oss_get_caps(pcm_oss_file);
2486 return put_user(res, p);
2487 case SNDCTL_DSP_GETTRIGGER:
2488 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2491 return put_user(res, p);
2492 case SNDCTL_DSP_SETTRIGGER:
2493 if (get_user(res, p))
2495 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2496 case SNDCTL_DSP_GETIPTR:
2497 case SNDCTL_DSP_GETOPTR:
2498 return snd_pcm_oss_get_ptr(pcm_oss_file,
2499 cmd == SNDCTL_DSP_GETIPTR ?
2500 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2501 (struct count_info __user *) arg);
2502 case SNDCTL_DSP_MAPINBUF:
2503 case SNDCTL_DSP_MAPOUTBUF:
2504 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2505 cmd == SNDCTL_DSP_MAPINBUF ?
2506 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2507 (struct buffmem_desc __user *) arg);
2508 case SNDCTL_DSP_SETSYNCRO:
2509 /* stop DMA now.. */
2511 case SNDCTL_DSP_SETDUPLEX:
2512 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2515 case SNDCTL_DSP_GETODELAY:
2516 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2518 /* it's for sure, some broken apps don't check for error codes */
2522 return put_user(res, p);
2523 case SNDCTL_DSP_PROFILE:
2524 return 0; /* silently ignore */
2526 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2531 #ifdef CONFIG_COMPAT
2532 /* all compatible */
2533 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2535 #define snd_pcm_oss_ioctl_compat NULL
2538 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2540 struct snd_pcm_oss_file *pcm_oss_file;
2541 struct snd_pcm_substream *substream;
2543 pcm_oss_file = file->private_data;
2544 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2545 if (substream == NULL)
2547 substream->f_flags = file->f_flags & O_NONBLOCK;
2549 return snd_pcm_oss_read1(substream, buf, count);
2552 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2553 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2559 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2561 struct snd_pcm_oss_file *pcm_oss_file;
2562 struct snd_pcm_substream *substream;
2565 pcm_oss_file = file->private_data;
2566 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2567 if (substream == NULL)
2569 substream->f_flags = file->f_flags & O_NONBLOCK;
2570 result = snd_pcm_oss_write1(substream, buf, count);
2572 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2577 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2579 struct snd_pcm_runtime *runtime = substream->runtime;
2580 if (atomic_read(&substream->mmap_count))
2581 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2583 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2586 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2588 struct snd_pcm_runtime *runtime = substream->runtime;
2589 if (atomic_read(&substream->mmap_count))
2590 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2592 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2595 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2597 struct snd_pcm_oss_file *pcm_oss_file;
2599 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2601 pcm_oss_file = file->private_data;
2603 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2604 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2607 if (psubstream != NULL) {
2608 struct snd_pcm_runtime *runtime = psubstream->runtime;
2609 poll_wait(file, &runtime->sleep, wait);
2610 snd_pcm_stream_lock_irq(psubstream);
2611 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2612 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2613 snd_pcm_oss_playback_ready(psubstream)))
2614 mask |= POLLOUT | POLLWRNORM;
2615 snd_pcm_stream_unlock_irq(psubstream);
2617 if (csubstream != NULL) {
2618 struct snd_pcm_runtime *runtime = csubstream->runtime;
2619 snd_pcm_state_t ostate;
2620 poll_wait(file, &runtime->sleep, wait);
2621 snd_pcm_stream_lock_irq(csubstream);
2622 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2623 snd_pcm_oss_capture_ready(csubstream))
2624 mask |= POLLIN | POLLRDNORM;
2625 snd_pcm_stream_unlock_irq(csubstream);
2626 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2627 struct snd_pcm_oss_file ofile;
2628 memset(&ofile, 0, sizeof(ofile));
2629 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2630 runtime->oss.trigger = 0;
2631 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2638 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2640 struct snd_pcm_oss_file *pcm_oss_file;
2641 struct snd_pcm_substream *substream = NULL;
2642 struct snd_pcm_runtime *runtime;
2646 printk("pcm_oss: mmap begin\n");
2648 pcm_oss_file = file->private_data;
2649 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2650 case VM_READ | VM_WRITE:
2651 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2656 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2659 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2664 /* set VM_READ access as well to fix memset() routines that do
2665 reads before writes (to improve performance) */
2666 area->vm_flags |= VM_READ;
2667 if (substream == NULL)
2669 runtime = substream->runtime;
2670 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2672 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2673 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2677 if (runtime->oss.params) {
2678 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2681 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2682 if (runtime->oss.plugin_first != NULL)
2686 if (area->vm_pgoff != 0)
2689 err = snd_pcm_mmap_data(substream, file, area);
2692 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2693 runtime->silence_threshold = 0;
2694 runtime->silence_size = 0;
2696 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2698 /* In mmap mode we never stop */
2699 runtime->stop_threshold = runtime->boundary;
2704 #ifdef CONFIG_SND_VERBOSE_PROCFS
2709 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2710 struct snd_info_buffer *buffer)
2712 struct snd_pcm_str *pstr = entry->private_data;
2713 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2714 mutex_lock(&pstr->oss.setup_mutex);
2716 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2720 setup->disable ? " disable" : "",
2721 setup->direct ? " direct" : "",
2722 setup->block ? " block" : "",
2723 setup->nonblock ? " non-block" : "",
2724 setup->partialfrag ? " partial-frag" : "",
2725 setup->nosilence ? " no-silence" : "");
2726 setup = setup->next;
2728 mutex_unlock(&pstr->oss.setup_mutex);
2731 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2733 struct snd_pcm_oss_setup *setup, *setupn;
2735 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2736 setup; setup = setupn) {
2737 setupn = setup->next;
2738 kfree(setup->task_name);
2741 pstr->oss.setup_list = NULL;
2744 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2745 struct snd_info_buffer *buffer)
2747 struct snd_pcm_str *pstr = entry->private_data;
2748 char line[128], str[32], task_name[32], *ptr;
2750 struct snd_pcm_oss_setup *setup, *setup1, template;
2752 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2753 mutex_lock(&pstr->oss.setup_mutex);
2754 memset(&template, 0, sizeof(template));
2755 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2756 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2757 snd_pcm_oss_proc_free_setup_list(pstr);
2758 mutex_unlock(&pstr->oss.setup_mutex);
2761 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2762 if (!strcmp(setup->task_name, task_name)) {
2767 ptr = snd_info_get_str(str, ptr, sizeof(str));
2768 template.periods = simple_strtoul(str, NULL, 10);
2769 ptr = snd_info_get_str(str, ptr, sizeof(str));
2770 template.period_size = simple_strtoul(str, NULL, 10);
2771 for (idx1 = 31; idx1 >= 0; idx1--)
2772 if (template.period_size & (1 << idx1))
2774 for (idx1--; idx1 >= 0; idx1--)
2775 template.period_size &= ~(1 << idx1);
2777 ptr = snd_info_get_str(str, ptr, sizeof(str));
2778 if (!strcmp(str, "disable")) {
2779 template.disable = 1;
2780 } else if (!strcmp(str, "direct")) {
2781 template.direct = 1;
2782 } else if (!strcmp(str, "block")) {
2784 } else if (!strcmp(str, "non-block")) {
2785 template.nonblock = 1;
2786 } else if (!strcmp(str, "partial-frag")) {
2787 template.partialfrag = 1;
2788 } else if (!strcmp(str, "no-silence")) {
2789 template.nosilence = 1;
2790 } else if (!strcmp(str, "buggy-ptr")) {
2791 template.buggyptr = 1;
2794 if (setup == NULL) {
2795 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2797 buffer->error = -ENOMEM;
2798 mutex_lock(&pstr->oss.setup_mutex);
2801 if (pstr->oss.setup_list == NULL)
2802 pstr->oss.setup_list = setup;
2804 for (setup1 = pstr->oss.setup_list;
2805 setup1->next; setup1 = setup1->next);
2806 setup1->next = setup;
2808 template.task_name = kstrdup(task_name, GFP_KERNEL);
2809 if (! template.task_name) {
2811 buffer->error = -ENOMEM;
2812 mutex_lock(&pstr->oss.setup_mutex);
2817 mutex_unlock(&pstr->oss.setup_mutex);
2821 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2824 for (stream = 0; stream < 2; ++stream) {
2825 struct snd_info_entry *entry;
2826 struct snd_pcm_str *pstr = &pcm->streams[stream];
2827 if (pstr->substream_count == 0)
2829 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2830 entry->content = SNDRV_INFO_CONTENT_TEXT;
2831 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2832 entry->c.text.read = snd_pcm_oss_proc_read;
2833 entry->c.text.write = snd_pcm_oss_proc_write;
2834 entry->private_data = pstr;
2835 if (snd_info_register(entry) < 0) {
2836 snd_info_free_entry(entry);
2840 pstr->oss.proc_entry = entry;
2844 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2847 for (stream = 0; stream < 2; ++stream) {
2848 struct snd_pcm_str *pstr = &pcm->streams[stream];
2849 snd_info_free_entry(pstr->oss.proc_entry);
2850 pstr->oss.proc_entry = NULL;
2851 snd_pcm_oss_proc_free_setup_list(pstr);
2854 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2855 #define snd_pcm_oss_proc_init(pcm)
2856 #define snd_pcm_oss_proc_done(pcm)
2857 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2863 static struct file_operations snd_pcm_oss_f_reg =
2865 .owner = THIS_MODULE,
2866 .read = snd_pcm_oss_read,
2867 .write = snd_pcm_oss_write,
2868 .open = snd_pcm_oss_open,
2869 .release = snd_pcm_oss_release,
2870 .poll = snd_pcm_oss_poll,
2871 .unlocked_ioctl = snd_pcm_oss_ioctl,
2872 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2873 .mmap = snd_pcm_oss_mmap,
2876 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2879 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2880 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2881 pcm->card, index, &snd_pcm_oss_f_reg,
2883 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2884 pcm->card->number, pcm->device);
2888 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2891 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2894 register_oss_dsp(pcm, 0);
2895 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2896 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2897 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2898 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2899 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2900 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2905 pcm->oss.reg_mask |= 1;
2907 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2908 register_oss_dsp(pcm, 1);
2910 pcm->oss.reg_mask |= 2;
2914 snd_pcm_oss_proc_init(pcm);
2919 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2922 if (pcm->oss.reg_mask & 1) {
2923 pcm->oss.reg_mask &= ~1;
2924 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2927 if (pcm->oss.reg_mask & 2) {
2928 pcm->oss.reg_mask &= ~2;
2929 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2932 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2933 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2934 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2942 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2944 snd_pcm_oss_disconnect_minor(pcm);
2945 snd_pcm_oss_proc_done(pcm);
2949 static struct snd_pcm_notify snd_pcm_oss_notify =
2951 .n_register = snd_pcm_oss_register_minor,
2952 .n_disconnect = snd_pcm_oss_disconnect_minor,
2953 .n_unregister = snd_pcm_oss_unregister_minor,
2956 static int __init alsa_pcm_oss_init(void)
2961 /* check device map table */
2962 for (i = 0; i < SNDRV_CARDS; i++) {
2963 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2964 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2968 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2969 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
2974 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2979 static void __exit alsa_pcm_oss_exit(void)
2981 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2984 module_init(alsa_pcm_oss_init)
2985 module_exit(alsa_pcm_oss_exit)