2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/time.h>
32 #include <linux/vmalloc.h>
33 #include <linux/module.h>
34 #include <linux/math64.h>
35 #include <linux/string.h>
36 #include <linux/compat.h>
37 #include <sound/core.h>
38 #include <sound/minors.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include "pcm_plugin.h"
42 #include <sound/info.h>
43 #include <linux/soundcard.h>
44 #include <sound/initval.h>
45 #include <sound/mixer_oss.h>
47 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
49 static int dsp_map[SNDRV_CARDS];
50 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
51 static bool nonblock_open = 1;
53 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
54 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
55 MODULE_LICENSE("GPL");
56 module_param_array(dsp_map, int, NULL, 0444);
57 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
58 module_param_array(adsp_map, int, NULL, 0444);
59 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
60 module_param(nonblock_open, bool, 0644);
61 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
63 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
65 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
66 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
67 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
69 static inline mm_segment_t snd_enter_user(void)
71 mm_segment_t fs = get_fs();
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);
466 v = snd_pcm_hw_param_last(pcm, params, var, dir);
468 v = snd_pcm_hw_param_first(pcm, params, var, dir);
472 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
473 snd_pcm_hw_param_t var, unsigned int val,
477 if (hw_is_mask(var)) {
478 struct snd_mask *m = hw_param_mask(params, var);
479 if (val == 0 && dir < 0) {
487 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
489 } else if (hw_is_interval(var)) {
490 struct snd_interval *i = hw_param_interval(params, var);
491 if (val == 0 && dir < 0) {
493 snd_interval_none(i);
495 changed = snd_interval_refine_set(i, val);
497 struct snd_interval t;
509 changed = snd_interval_refine(i, &t);
514 params->cmask |= 1 << var;
515 params->rmask |= 1 << var;
521 * snd_pcm_hw_param_set
523 * @params: the hw_params instance
524 * @var: parameter to retrieve
526 * @dir: pointer to the direction (-1,0,1) or NULL
528 * Inside configuration space defined by PARAMS remove from PAR all
529 * values != VAL. Reduce configuration space accordingly.
530 * Return VAL or -EINVAL if the configuration space is empty
532 static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
533 struct snd_pcm_hw_params *params,
534 snd_pcm_hw_param_t var, unsigned int val,
537 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
541 int err = snd_pcm_hw_refine(pcm, params);
545 return snd_pcm_hw_param_value(params, var, NULL);
548 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
549 snd_pcm_hw_param_t var)
552 changed = snd_interval_setinteger(hw_param_interval(params, var));
554 params->cmask |= 1 << var;
555 params->rmask |= 1 << var;
564 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
565 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
567 struct snd_pcm_runtime *runtime = substream->runtime;
568 struct snd_pcm_plugin *plugin, *next;
570 plugin = runtime->oss.plugin_first;
573 snd_pcm_plugin_free(plugin);
576 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
580 static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
582 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
583 plugin->next = runtime->oss.plugin_first;
585 if (runtime->oss.plugin_first) {
586 runtime->oss.plugin_first->prev = plugin;
587 runtime->oss.plugin_first = plugin;
589 runtime->oss.plugin_last =
590 runtime->oss.plugin_first = plugin;
595 int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
597 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
599 plugin->prev = runtime->oss.plugin_last;
600 if (runtime->oss.plugin_last) {
601 runtime->oss.plugin_last->next = plugin;
602 runtime->oss.plugin_last = plugin;
604 runtime->oss.plugin_last =
605 runtime->oss.plugin_first = plugin;
609 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
611 static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
613 struct snd_pcm_runtime *runtime = substream->runtime;
614 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
615 long bytes = frames_to_bytes(runtime, frames);
616 if (buffer_size == runtime->oss.buffer_bytes)
618 #if BITS_PER_LONG >= 64
619 return runtime->oss.buffer_bytes * bytes / buffer_size;
622 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
623 return div_u64(bsize, buffer_size);
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);
638 snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
640 return runtime->hw_ptr_interrupt;
643 /* define extended formats in the recent OSS versions (if any) */
645 #define AFMT_S32_LE 0x00001000
646 #define AFMT_S32_BE 0x00002000
647 #define AFMT_S24_LE 0x00008000
648 #define AFMT_S24_BE 0x00010000
649 #define AFMT_S24_PACKED 0x00040000
651 /* other supported formats */
652 #define AFMT_FLOAT 0x00004000
653 #define AFMT_SPDIF_RAW 0x00020000
655 /* unsupported formats */
656 #define AFMT_AC3 0x00000400
657 #define AFMT_VORBIS 0x00000800
659 static snd_pcm_format_t snd_pcm_oss_format_from(int format)
662 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
663 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
664 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
665 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
666 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
667 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
668 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
669 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
670 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
671 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
672 case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
673 case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
674 case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
675 case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
676 case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
677 case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
678 case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
679 default: return SNDRV_PCM_FORMAT_U8;
683 static int snd_pcm_oss_format_to(snd_pcm_format_t format)
686 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
687 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
688 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
689 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
690 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
691 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
692 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
693 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
694 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
695 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
696 case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
697 case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
698 case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
699 case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
700 case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
701 case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
702 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
703 default: return -EINVAL;
707 static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
708 struct snd_pcm_hw_params *oss_params,
709 struct snd_pcm_hw_params *slave_params)
712 size_t oss_buffer_size, oss_period_size, oss_periods;
713 size_t min_period_size, max_period_size;
714 struct snd_pcm_runtime *runtime = substream->runtime;
715 size_t oss_frame_size;
717 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
718 params_channels(oss_params) / 8;
720 oss_buffer_size = snd_pcm_plug_client_size(substream,
721 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
722 if (!oss_buffer_size)
724 oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
725 if (atomic_read(&substream->mmap_count)) {
726 if (oss_buffer_size > runtime->oss.mmap_bytes)
727 oss_buffer_size = runtime->oss.mmap_bytes;
730 if (substream->oss.setup.period_size > 16)
731 oss_period_size = substream->oss.setup.period_size;
732 else if (runtime->oss.fragshift) {
733 oss_period_size = 1 << runtime->oss.fragshift;
734 if (oss_period_size > oss_buffer_size / 2)
735 oss_period_size = oss_buffer_size / 2;
738 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
740 oss_period_size = oss_buffer_size;
742 oss_period_size /= 2;
743 } while (oss_period_size > bytes_per_sec);
744 if (runtime->oss.subdivision == 0) {
746 if (oss_period_size / sd > 4096)
748 if (oss_period_size / sd < 4096)
751 sd = runtime->oss.subdivision;
752 oss_period_size /= sd;
753 if (oss_period_size < 16)
754 oss_period_size = 16;
757 min_period_size = snd_pcm_plug_client_size(substream,
758 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
759 if (min_period_size) {
760 min_period_size *= oss_frame_size;
761 min_period_size = roundup_pow_of_two(min_period_size);
762 if (oss_period_size < min_period_size)
763 oss_period_size = min_period_size;
766 max_period_size = snd_pcm_plug_client_size(substream,
767 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
768 if (max_period_size) {
769 max_period_size *= oss_frame_size;
770 max_period_size = rounddown_pow_of_two(max_period_size);
771 if (oss_period_size > max_period_size)
772 oss_period_size = max_period_size;
775 oss_periods = oss_buffer_size / oss_period_size;
777 if (substream->oss.setup.periods > 1)
778 oss_periods = substream->oss.setup.periods;
780 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
781 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
782 s = runtime->oss.maxfrags;
786 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
792 while (oss_period_size * oss_periods > oss_buffer_size)
793 oss_period_size /= 2;
795 if (oss_period_size < 16)
797 runtime->oss.period_bytes = oss_period_size;
798 runtime->oss.period_frames = 1;
799 runtime->oss.periods = oss_periods;
803 static int choose_rate(struct snd_pcm_substream *substream,
804 struct snd_pcm_hw_params *params, unsigned int best_rate)
806 struct snd_interval *it;
807 struct snd_pcm_hw_params *save;
808 unsigned int rate, prev;
810 save = kmalloc(sizeof(*save), GFP_KERNEL);
814 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
816 /* try multiples of the best rate */
819 if (it->max < rate || (it->max == rate && it->openmax))
821 if (it->min < rate || (it->min == rate && !it->openmin)) {
823 ret = snd_pcm_hw_param_set(substream, params,
824 SNDRV_PCM_HW_PARAM_RATE,
826 if (ret == (int)rate) {
838 /* not found, use the nearest rate */
840 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
843 /* parameter locking: returns immediately if tried during streaming */
844 static int lock_params(struct snd_pcm_runtime *runtime)
846 if (mutex_lock_interruptible(&runtime->oss.params_lock))
848 if (atomic_read(&runtime->oss.rw_ref)) {
849 mutex_unlock(&runtime->oss.params_lock);
855 static void unlock_params(struct snd_pcm_runtime *runtime)
857 mutex_unlock(&runtime->oss.params_lock);
860 /* call with params_lock held */
861 static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
863 struct snd_pcm_runtime *runtime = substream->runtime;
864 struct snd_pcm_hw_params *params, *sparams;
865 struct snd_pcm_sw_params *sw_params;
866 ssize_t oss_buffer_size, oss_period_size;
867 size_t oss_frame_size;
870 snd_pcm_format_t format, sformat;
872 struct snd_mask sformat_mask;
873 struct snd_mask mask;
875 if (!runtime->oss.params)
877 sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
878 params = kmalloc(sizeof(*params), GFP_KERNEL);
879 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
880 if (!sw_params || !params || !sparams) {
885 if (atomic_read(&substream->mmap_count))
888 direct = substream->oss.setup.direct;
890 _snd_pcm_hw_params_any(sparams);
891 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
892 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
893 snd_mask_none(&mask);
894 if (atomic_read(&substream->mmap_count))
895 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
897 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
899 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
901 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
903 pcm_dbg(substream->pcm, "No usable accesses\n");
907 choose_rate(substream, sparams, runtime->oss.rate);
908 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
910 format = snd_pcm_oss_format_from(runtime->oss.format);
912 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
916 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
918 if ((__force int)sformat < 0 ||
919 !snd_mask_test(&sformat_mask, (__force int)sformat)) {
920 for (sformat = (__force snd_pcm_format_t)0;
921 (__force int)sformat <= (__force int)SNDRV_PCM_FORMAT_LAST;
922 sformat = (__force snd_pcm_format_t)((__force int)sformat + 1)) {
923 if (snd_mask_test(&sformat_mask, (__force int)sformat) &&
924 snd_pcm_oss_format_to(sformat) >= 0)
927 if ((__force int)sformat > (__force int)SNDRV_PCM_FORMAT_LAST) {
928 pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
933 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
938 memcpy(params, sparams, sizeof(*params));
940 _snd_pcm_hw_params_any(params);
941 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
942 (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
943 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
944 (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
945 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
946 runtime->oss.channels, 0);
947 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
948 runtime->oss.rate, 0);
949 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
950 params_access(params), params_format(params),
951 params_channels(params), params_rate(params));
953 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
954 params_access(sparams), params_format(sparams),
955 params_channels(sparams), params_rate(sparams));
957 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
958 params_channels(params) / 8;
960 err = snd_pcm_oss_period_size(substream, params, sparams);
964 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
965 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
969 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
970 runtime->oss.periods, NULL);
974 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
976 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
978 pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
982 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
983 snd_pcm_oss_plugin_clear(substream);
985 /* add necessary plugins */
986 snd_pcm_oss_plugin_clear(substream);
987 if ((err = snd_pcm_plug_format_plugins(substream,
990 pcm_dbg(substream->pcm,
991 "snd_pcm_plug_format_plugins failed: %i\n", err);
992 snd_pcm_oss_plugin_clear(substream);
995 if (runtime->oss.plugin_first) {
996 struct snd_pcm_plugin *plugin;
997 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
998 pcm_dbg(substream->pcm,
999 "snd_pcm_plugin_build_io failed: %i\n", err);
1000 snd_pcm_oss_plugin_clear(substream);
1003 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1004 err = snd_pcm_plugin_append(plugin);
1006 err = snd_pcm_plugin_insert(plugin);
1009 snd_pcm_oss_plugin_clear(substream);
1016 if (runtime->oss.trigger) {
1017 sw_params->start_threshold = 1;
1019 sw_params->start_threshold = runtime->boundary;
1021 if (atomic_read(&substream->mmap_count) ||
1022 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1023 sw_params->stop_threshold = runtime->boundary;
1025 sw_params->stop_threshold = runtime->buffer_size;
1026 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
1027 sw_params->period_step = 1;
1028 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1029 1 : runtime->period_size;
1030 if (atomic_read(&substream->mmap_count) ||
1031 substream->oss.setup.nosilence) {
1032 sw_params->silence_threshold = 0;
1033 sw_params->silence_size = 0;
1035 snd_pcm_uframes_t frames;
1036 frames = runtime->period_size + 16;
1037 if (frames > runtime->buffer_size)
1038 frames = runtime->buffer_size;
1039 sw_params->silence_threshold = frames;
1040 sw_params->silence_size = frames;
1043 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1044 pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
1048 runtime->oss.periods = params_periods(sparams);
1049 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1050 if (oss_period_size < 0) {
1054 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1055 if (runtime->oss.plugin_first) {
1056 err = snd_pcm_plug_alloc(substream, oss_period_size);
1061 oss_period_size *= oss_frame_size;
1063 oss_buffer_size = oss_period_size * runtime->oss.periods;
1064 if (oss_buffer_size < 0) {
1069 runtime->oss.period_bytes = oss_period_size;
1070 runtime->oss.buffer_bytes = oss_buffer_size;
1072 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1073 runtime->oss.period_bytes,
1074 runtime->oss.buffer_bytes);
1075 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1076 params_period_size(sparams),
1077 params_buffer_size(sparams));
1079 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1080 runtime->oss.channels = params_channels(params);
1081 runtime->oss.rate = params_rate(params);
1083 vfree(runtime->oss.buffer);
1084 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1085 if (!runtime->oss.buffer) {
1090 runtime->oss.params = 0;
1091 runtime->oss.prepare = 1;
1092 runtime->oss.buffer_used = 0;
1093 if (runtime->dma_area)
1094 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1096 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1106 /* this one takes the lock by itself */
1107 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1114 if (!(mutex_trylock(&runtime->oss.params_lock)))
1116 } else if (mutex_lock_interruptible(&runtime->oss.params_lock))
1117 return -ERESTARTSYS;
1119 err = snd_pcm_oss_change_params_locked(substream);
1120 mutex_unlock(&runtime->oss.params_lock);
1124 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1127 struct snd_pcm_substream *asubstream = NULL, *substream;
1129 for (idx = 0; idx < 2; idx++) {
1130 substream = pcm_oss_file->streams[idx];
1131 if (substream == NULL)
1133 if (asubstream == NULL)
1134 asubstream = substream;
1135 if (substream->runtime->oss.params) {
1136 err = snd_pcm_oss_change_params(substream, false);
1144 *r_substream = asubstream;
1148 /* call with params_lock held */
1149 /* NOTE: this always call PREPARE unconditionally no matter whether
1150 * runtime->oss.prepare is set or not
1152 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1155 struct snd_pcm_runtime *runtime = substream->runtime;
1157 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1159 pcm_dbg(substream->pcm,
1160 "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1163 runtime->oss.prepare = 0;
1164 runtime->oss.prev_hw_ptr_period = 0;
1165 runtime->oss.period_ptr = 0;
1166 runtime->oss.buffer_used = 0;
1171 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1173 struct snd_pcm_runtime *runtime;
1176 runtime = substream->runtime;
1177 if (runtime->oss.params) {
1178 err = snd_pcm_oss_change_params(substream, false);
1182 if (runtime->oss.prepare) {
1183 if (mutex_lock_interruptible(&runtime->oss.params_lock))
1184 return -ERESTARTSYS;
1185 err = snd_pcm_oss_prepare(substream);
1186 mutex_unlock(&runtime->oss.params_lock);
1193 /* call with params_lock held */
1194 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1196 struct snd_pcm_runtime *runtime;
1199 runtime = substream->runtime;
1200 if (runtime->oss.params) {
1201 err = snd_pcm_oss_change_params_locked(substream);
1205 if (runtime->oss.prepare) {
1206 err = snd_pcm_oss_prepare(substream);
1213 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1215 struct snd_pcm_runtime *runtime;
1216 snd_pcm_uframes_t frames;
1220 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1223 runtime = substream->runtime;
1224 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1226 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1227 /* until avail(delay) <= buffer_size */
1228 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1229 frames /= runtime->period_size;
1230 frames *= runtime->period_size;
1231 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1238 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1243 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1244 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1246 pcm_dbg(substream->pcm,
1247 "pcm_oss: write: recovering from %s\n",
1248 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1249 "XRUN" : "SUSPEND");
1251 ret = snd_pcm_oss_prepare(substream);
1257 fs = snd_enter_user();
1258 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1261 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1263 if (ret != -EPIPE && ret != -ESTRPIPE)
1265 /* test, if we can't store new data, because the stream */
1266 /* has not been started */
1267 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1273 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1275 struct snd_pcm_runtime *runtime = substream->runtime;
1276 snd_pcm_sframes_t delay;
1279 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1280 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1282 pcm_dbg(substream->pcm,
1283 "pcm_oss: read: recovering from %s\n",
1284 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1285 "XRUN" : "SUSPEND");
1287 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1290 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1291 ret = snd_pcm_oss_prepare(substream);
1295 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1300 fs = snd_enter_user();
1301 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1304 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1306 if (ret == -EPIPE) {
1307 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1308 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1314 if (ret != -ESTRPIPE)
1320 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1322 struct snd_pcm_runtime *runtime = substream->runtime;
1325 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1326 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1328 pcm_dbg(substream->pcm,
1329 "pcm_oss: writev: recovering from %s\n",
1330 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1331 "XRUN" : "SUSPEND");
1333 ret = snd_pcm_oss_prepare(substream);
1339 fs = snd_enter_user();
1340 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1343 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1345 if (ret != -EPIPE && ret != -ESTRPIPE)
1348 /* test, if we can't store new data, because the stream */
1349 /* has not been started */
1350 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1356 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1358 struct snd_pcm_runtime *runtime = substream->runtime;
1361 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1362 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1364 pcm_dbg(substream->pcm,
1365 "pcm_oss: readv: recovering from %s\n",
1366 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1367 "XRUN" : "SUSPEND");
1369 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1372 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1373 ret = snd_pcm_oss_prepare(substream);
1379 fs = snd_enter_user();
1380 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1383 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1385 if (ret != -EPIPE && ret != -ESTRPIPE)
1391 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1393 struct snd_pcm_runtime *runtime = substream->runtime;
1394 snd_pcm_sframes_t frames, frames1;
1395 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1396 if (runtime->oss.plugin_first) {
1397 struct snd_pcm_plugin_channel *channels;
1398 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1400 if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1402 buf = runtime->oss.buffer;
1404 frames = bytes / oss_frame_bytes;
1405 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1408 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1411 bytes = frames1 * oss_frame_bytes;
1415 frames = bytes_to_frames(runtime, bytes);
1416 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1419 bytes = frames_to_bytes(runtime, frames1);
1424 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1428 struct snd_pcm_runtime *runtime = substream->runtime;
1430 if (atomic_read(&substream->mmap_count))
1433 atomic_inc(&runtime->oss.rw_ref);
1435 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1439 tmp = snd_pcm_oss_make_ready_locked(substream);
1442 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1444 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1445 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1447 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1452 runtime->oss.buffer_used += tmp;
1456 if (substream->oss.setup.partialfrag ||
1457 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1458 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1459 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1462 runtime->oss.bytes += tmp;
1463 runtime->oss.period_ptr += tmp;
1464 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1465 if (runtime->oss.period_ptr == 0 ||
1466 runtime->oss.period_ptr == runtime->oss.buffer_used)
1467 runtime->oss.buffer_used = 0;
1468 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1474 tmp = snd_pcm_oss_write2(substream,
1475 (const char __force *)buf,
1476 runtime->oss.period_bytes, 0);
1479 runtime->oss.bytes += tmp;
1483 if ((substream->f_flags & O_NONBLOCK) != 0 &&
1484 tmp != runtime->oss.period_bytes)
1488 mutex_unlock(&runtime->oss.params_lock);
1491 if (signal_pending(current)) {
1497 atomic_dec(&runtime->oss.rw_ref);
1498 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1501 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1503 struct snd_pcm_runtime *runtime = substream->runtime;
1504 snd_pcm_sframes_t frames, frames1;
1505 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1506 char __user *final_dst = (char __force __user *)buf;
1507 if (runtime->oss.plugin_first) {
1508 struct snd_pcm_plugin_channel *channels;
1509 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1511 buf = runtime->oss.buffer;
1512 frames = bytes / oss_frame_bytes;
1513 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1516 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1519 bytes = frames1 * oss_frame_bytes;
1520 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1525 frames = bytes_to_frames(runtime, bytes);
1526 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1529 bytes = frames_to_bytes(runtime, frames1);
1534 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1538 struct snd_pcm_runtime *runtime = substream->runtime;
1540 if (atomic_read(&substream->mmap_count))
1543 atomic_inc(&runtime->oss.rw_ref);
1545 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1549 tmp = snd_pcm_oss_make_ready_locked(substream);
1552 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1553 if (runtime->oss.buffer_used == 0) {
1554 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1557 runtime->oss.bytes += tmp;
1558 runtime->oss.period_ptr = tmp;
1559 runtime->oss.buffer_used = tmp;
1562 if ((size_t) tmp > runtime->oss.buffer_used)
1563 tmp = runtime->oss.buffer_used;
1564 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1571 runtime->oss.buffer_used -= tmp;
1573 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1574 runtime->oss.period_bytes, 0);
1577 runtime->oss.bytes += tmp;
1583 mutex_unlock(&runtime->oss.params_lock);
1586 if (signal_pending(current)) {
1592 atomic_dec(&runtime->oss.rw_ref);
1593 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1596 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1598 struct snd_pcm_substream *substream;
1599 struct snd_pcm_runtime *runtime;
1602 for (i = 0; i < 2; i++) {
1603 substream = pcm_oss_file->streams[i];
1606 runtime = substream->runtime;
1607 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1608 mutex_lock(&runtime->oss.params_lock);
1609 runtime->oss.prepare = 1;
1610 runtime->oss.buffer_used = 0;
1611 runtime->oss.prev_hw_ptr_period = 0;
1612 runtime->oss.period_ptr = 0;
1613 mutex_unlock(&runtime->oss.params_lock);
1618 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1620 struct snd_pcm_substream *substream;
1623 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1624 if (substream != NULL) {
1625 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1627 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1629 /* note: all errors from the start action are ignored */
1630 /* OSS apps do not know, how to handle them */
1634 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1636 struct snd_pcm_runtime *runtime;
1638 snd_pcm_state_t state;
1642 runtime = substream->runtime;
1643 init_waitqueue_entry(&wait, current);
1644 add_wait_queue(&runtime->sleep, &wait);
1646 pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1649 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1651 runtime->oss.buffer_used = 0;
1655 if (result != 0 && result != -EAGAIN)
1658 set_current_state(TASK_INTERRUPTIBLE);
1659 snd_pcm_stream_lock_irq(substream);
1660 state = runtime->status->state;
1661 snd_pcm_stream_unlock_irq(substream);
1662 if (state != SNDRV_PCM_STATE_RUNNING) {
1663 set_current_state(TASK_RUNNING);
1666 res = schedule_timeout(10 * HZ);
1667 if (signal_pending(current)) {
1668 result = -ERESTARTSYS;
1672 pcm_err(substream->pcm,
1673 "OSS sync error - DMA timeout\n");
1678 remove_wait_queue(&runtime->sleep, &wait);
1682 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1685 unsigned int saved_f_flags;
1686 struct snd_pcm_substream *substream;
1687 struct snd_pcm_runtime *runtime;
1688 snd_pcm_format_t format;
1689 unsigned long width;
1692 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1693 if (substream != NULL) {
1694 runtime = substream->runtime;
1695 if (atomic_read(&substream->mmap_count))
1697 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1699 atomic_inc(&runtime->oss.rw_ref);
1700 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1701 atomic_dec(&runtime->oss.rw_ref);
1702 return -ERESTARTSYS;
1704 format = snd_pcm_oss_format_from(runtime->oss.format);
1705 width = snd_pcm_format_physical_width(format);
1706 if (runtime->oss.buffer_used > 0) {
1708 pcm_dbg(substream->pcm, "sync: buffer_used\n");
1710 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1711 snd_pcm_format_set_silence(format,
1712 runtime->oss.buffer + runtime->oss.buffer_used,
1714 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1717 } else if (runtime->oss.period_ptr > 0) {
1719 pcm_dbg(substream->pcm, "sync: period_ptr\n");
1721 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1722 snd_pcm_format_set_silence(format,
1723 runtime->oss.buffer,
1725 err = snd_pcm_oss_sync1(substream, size);
1730 * The ALSA's period might be a bit large than OSS one.
1731 * Fill the remain portion of ALSA period with zeros.
1733 size = runtime->control->appl_ptr % runtime->period_size;
1735 size = runtime->period_size - size;
1736 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1737 size = (runtime->frame_bits * size) / 8;
1740 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1743 size1 /= runtime->sample_bits;
1744 snd_pcm_format_set_silence(runtime->format,
1745 runtime->oss.buffer,
1747 size1 /= runtime->channels; /* frames */
1748 fs = snd_enter_user();
1749 snd_pcm_lib_write(substream, (void __force __user *)runtime->oss.buffer, size1);
1752 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1753 void __user *buffers[runtime->channels];
1754 memset(buffers, 0, runtime->channels * sizeof(void *));
1755 snd_pcm_lib_writev(substream, buffers, size);
1759 mutex_unlock(&runtime->oss.params_lock);
1760 atomic_dec(&runtime->oss.rw_ref);
1764 * finish sync: drain the buffer
1767 saved_f_flags = substream->f_flags;
1768 substream->f_flags &= ~O_NONBLOCK;
1769 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1770 substream->f_flags = saved_f_flags;
1773 mutex_lock(&runtime->oss.params_lock);
1774 runtime->oss.prepare = 1;
1775 mutex_unlock(&runtime->oss.params_lock);
1778 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1779 if (substream != NULL) {
1780 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1782 runtime = substream->runtime;
1783 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1786 mutex_lock(&runtime->oss.params_lock);
1787 runtime->oss.buffer_used = 0;
1788 runtime->oss.prepare = 1;
1789 mutex_unlock(&runtime->oss.params_lock);
1794 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1798 for (idx = 1; idx >= 0; --idx) {
1799 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1800 struct snd_pcm_runtime *runtime;
1803 if (substream == NULL)
1805 runtime = substream->runtime;
1808 else if (rate > 192000)
1810 err = lock_params(runtime);
1813 if (runtime->oss.rate != rate) {
1814 runtime->oss.params = 1;
1815 runtime->oss.rate = rate;
1817 unlock_params(runtime);
1819 return snd_pcm_oss_get_rate(pcm_oss_file);
1822 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1824 struct snd_pcm_substream *substream;
1827 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1829 return substream->runtime->oss.rate;
1832 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1839 for (idx = 1; idx >= 0; --idx) {
1840 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1841 struct snd_pcm_runtime *runtime;
1844 if (substream == NULL)
1846 runtime = substream->runtime;
1847 err = lock_params(runtime);
1850 if (runtime->oss.channels != channels) {
1851 runtime->oss.params = 1;
1852 runtime->oss.channels = channels;
1854 unlock_params(runtime);
1856 return snd_pcm_oss_get_channels(pcm_oss_file);
1859 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1861 struct snd_pcm_substream *substream;
1864 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1866 return substream->runtime->oss.channels;
1869 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1871 struct snd_pcm_substream *substream;
1874 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1876 return substream->runtime->oss.period_bytes;
1879 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1881 struct snd_pcm_substream *substream;
1884 struct snd_pcm_hw_params *params;
1885 unsigned int formats = 0;
1886 struct snd_mask format_mask;
1889 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1891 if (atomic_read(&substream->mmap_count))
1894 direct = substream->oss.setup.direct;
1896 return AFMT_MU_LAW | AFMT_U8 |
1897 AFMT_S16_LE | AFMT_S16_BE |
1898 AFMT_S8 | AFMT_U16_LE |
1900 AFMT_S32_LE | AFMT_S32_BE |
1901 AFMT_S24_LE | AFMT_S24_BE |
1903 params = kmalloc(sizeof(*params), GFP_KERNEL);
1906 _snd_pcm_hw_params_any(params);
1907 err = snd_pcm_hw_refine(substream, params);
1910 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1911 for (fmt = 0; fmt < 32; ++fmt) {
1912 if (snd_mask_test(&format_mask, fmt)) {
1913 int f = snd_pcm_oss_format_to(fmt);
1921 return err < 0 ? err : formats;
1924 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1929 if (format != AFMT_QUERY) {
1930 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1933 if (!(formats & format))
1935 for (idx = 1; idx >= 0; --idx) {
1936 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1937 struct snd_pcm_runtime *runtime;
1938 if (substream == NULL)
1940 runtime = substream->runtime;
1941 err = lock_params(runtime);
1944 if (runtime->oss.format != format) {
1945 runtime->oss.params = 1;
1946 runtime->oss.format = format;
1948 unlock_params(runtime);
1951 return snd_pcm_oss_get_format(pcm_oss_file);
1954 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1956 struct snd_pcm_substream *substream;
1959 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1961 return substream->runtime->oss.format;
1964 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1966 struct snd_pcm_runtime *runtime;
1968 runtime = substream->runtime;
1969 if (subdivide == 0) {
1970 subdivide = runtime->oss.subdivision;
1975 if (runtime->oss.subdivision || runtime->oss.fragshift)
1977 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1978 subdivide != 8 && subdivide != 16)
1980 runtime->oss.subdivision = subdivide;
1981 runtime->oss.params = 1;
1985 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1987 int err = -EINVAL, idx;
1989 for (idx = 1; idx >= 0; --idx) {
1990 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1991 struct snd_pcm_runtime *runtime;
1993 if (substream == NULL)
1995 runtime = substream->runtime;
1996 err = lock_params(runtime);
1999 err = snd_pcm_oss_set_subdivide1(substream, subdivide);
2000 unlock_params(runtime);
2007 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
2009 struct snd_pcm_runtime *runtime;
2012 runtime = substream->runtime;
2013 if (runtime->oss.subdivision || runtime->oss.fragshift)
2015 fragshift = val & 0xffff;
2016 if (fragshift >= 31)
2018 runtime->oss.fragshift = fragshift;
2019 runtime->oss.maxfrags = (val >> 16) & 0xffff;
2020 if (runtime->oss.fragshift < 4) /* < 16 */
2021 runtime->oss.fragshift = 4;
2022 if (runtime->oss.maxfrags < 2)
2023 runtime->oss.maxfrags = 2;
2024 runtime->oss.params = 1;
2028 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
2030 int err = -EINVAL, idx;
2032 for (idx = 1; idx >= 0; --idx) {
2033 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2034 struct snd_pcm_runtime *runtime;
2036 if (substream == NULL)
2038 runtime = substream->runtime;
2039 err = lock_params(runtime);
2042 err = snd_pcm_oss_set_fragment1(substream, val);
2043 unlock_params(runtime);
2050 static int snd_pcm_oss_nonblock(struct file * file)
2052 spin_lock(&file->f_lock);
2053 file->f_flags |= O_NONBLOCK;
2054 spin_unlock(&file->f_lock);
2058 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2061 if (substream == NULL) {
2062 res &= ~DSP_CAP_DUPLEX;
2065 #ifdef DSP_CAP_MULTI
2066 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2067 if (substream->pstr->substream_count > 1)
2068 res |= DSP_CAP_MULTI;
2070 /* DSP_CAP_REALTIME is set all times: */
2071 /* all ALSA drivers can return actual pointer in ring buffer */
2072 #if defined(DSP_CAP_REALTIME) && 0
2074 struct snd_pcm_runtime *runtime = substream->runtime;
2075 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2076 res &= ~DSP_CAP_REALTIME;
2082 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2086 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2087 for (idx = 0; idx < 2; idx++) {
2088 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2089 result = snd_pcm_oss_get_caps1(substream, result);
2091 result |= 0x0001; /* revision - same as SB AWE 64 */
2095 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2096 snd_pcm_uframes_t hw_ptr)
2098 struct snd_pcm_runtime *runtime = substream->runtime;
2099 snd_pcm_uframes_t appl_ptr;
2100 appl_ptr = hw_ptr + runtime->buffer_size;
2101 appl_ptr %= runtime->boundary;
2102 runtime->control->appl_ptr = appl_ptr;
2105 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2107 struct snd_pcm_runtime *runtime;
2108 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2112 pcm_dbg(substream->pcm, "pcm_oss: trigger = 0x%x\n", trigger);
2115 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2116 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2119 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
2123 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
2127 runtime = psubstream->runtime;
2129 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2130 return -ERESTARTSYS;
2131 if (trigger & PCM_ENABLE_OUTPUT) {
2132 if (runtime->oss.trigger)
2134 if (atomic_read(&psubstream->mmap_count))
2135 snd_pcm_oss_simulate_fill(psubstream,
2136 get_hw_ptr_period(runtime));
2137 runtime->oss.trigger = 1;
2138 runtime->start_threshold = 1;
2139 cmd = SNDRV_PCM_IOCTL_START;
2141 if (!runtime->oss.trigger)
2143 runtime->oss.trigger = 0;
2144 runtime->start_threshold = runtime->boundary;
2145 cmd = SNDRV_PCM_IOCTL_DROP;
2146 runtime->oss.prepare = 1;
2149 mutex_unlock(&runtime->oss.params_lock);
2151 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2157 runtime = csubstream->runtime;
2159 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2160 return -ERESTARTSYS;
2161 if (trigger & PCM_ENABLE_INPUT) {
2162 if (runtime->oss.trigger)
2164 runtime->oss.trigger = 1;
2165 runtime->start_threshold = 1;
2166 cmd = SNDRV_PCM_IOCTL_START;
2168 if (!runtime->oss.trigger)
2170 runtime->oss.trigger = 0;
2171 runtime->start_threshold = runtime->boundary;
2172 cmd = SNDRV_PCM_IOCTL_DROP;
2173 runtime->oss.prepare = 1;
2176 mutex_unlock(&runtime->oss.params_lock);
2178 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2186 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2188 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2191 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2192 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2193 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2194 result |= PCM_ENABLE_OUTPUT;
2195 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2196 result |= PCM_ENABLE_INPUT;
2200 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2202 struct snd_pcm_substream *substream;
2203 struct snd_pcm_runtime *runtime;
2204 snd_pcm_sframes_t delay;
2207 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2208 if (substream == NULL)
2210 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2212 runtime = substream->runtime;
2213 if (runtime->oss.params || runtime->oss.prepare)
2215 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2217 delay = 0; /* hack for broken OSS applications */
2220 return snd_pcm_oss_bytes(substream, delay);
2223 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2225 struct snd_pcm_substream *substream;
2226 struct snd_pcm_runtime *runtime;
2227 snd_pcm_sframes_t delay;
2229 struct count_info info;
2234 substream = pcm_oss_file->streams[stream];
2235 if (substream == NULL)
2237 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2239 runtime = substream->runtime;
2240 if (runtime->oss.params || runtime->oss.prepare) {
2241 memset(&info, 0, sizeof(info));
2242 if (copy_to_user(_info, &info, sizeof(info)))
2246 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2247 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2248 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2253 fixup = runtime->oss.buffer_used;
2256 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2257 fixup = -runtime->oss.buffer_used;
2261 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2262 if (atomic_read(&substream->mmap_count)) {
2263 snd_pcm_sframes_t n;
2264 delay = get_hw_ptr_period(runtime);
2265 n = delay - runtime->oss.prev_hw_ptr_period;
2267 n += runtime->boundary;
2268 info.blocks = n / runtime->period_size;
2269 runtime->oss.prev_hw_ptr_period = delay;
2270 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2271 snd_pcm_oss_simulate_fill(substream, delay);
2272 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2274 delay = snd_pcm_oss_bytes(substream, delay);
2275 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2276 if (substream->oss.setup.buggyptr)
2277 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2279 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2280 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2283 info.blocks = delay / runtime->oss.period_bytes;
2284 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2287 if (copy_to_user(_info, &info, sizeof(info)))
2292 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2294 struct snd_pcm_substream *substream;
2295 struct snd_pcm_runtime *runtime;
2296 snd_pcm_sframes_t avail;
2298 struct audio_buf_info info;
2303 substream = pcm_oss_file->streams[stream];
2304 if (substream == NULL)
2306 runtime = substream->runtime;
2308 if (runtime->oss.params &&
2309 (err = snd_pcm_oss_change_params(substream, false)) < 0)
2312 info.fragsize = runtime->oss.period_bytes;
2313 info.fragstotal = runtime->periods;
2314 if (runtime->oss.prepare) {
2315 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2316 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2317 info.fragments = runtime->oss.periods;
2323 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2324 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2325 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2326 avail = runtime->buffer_size;
2330 avail = runtime->buffer_size - avail;
2331 fixup = -runtime->oss.buffer_used;
2334 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2335 fixup = runtime->oss.buffer_used;
2339 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2340 info.fragments = info.bytes / runtime->oss.period_bytes;
2344 pcm_dbg(substream->pcm,
2345 "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2346 info.bytes, info.fragments, info.fragstotal, info.fragsize);
2348 if (copy_to_user(_info, &info, sizeof(info)))
2353 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2355 // it won't be probably implemented
2356 // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2360 static const char *strip_task_path(const char *path)
2362 const char *ptr, *ptrl = NULL;
2363 for (ptr = path; *ptr; ptr++) {
2370 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2371 const char *task_name,
2372 struct snd_pcm_oss_setup *rsetup)
2374 struct snd_pcm_oss_setup *setup;
2376 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2378 for (setup = pcm->streams[stream].oss.setup_list; setup;
2379 setup = setup->next) {
2380 if (!strcmp(setup->task_name, task_name))
2383 } while ((task_name = strip_task_path(task_name)) != NULL);
2387 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2390 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2392 struct snd_pcm_runtime *runtime;
2393 runtime = substream->runtime;
2394 vfree(runtime->oss.buffer);
2395 runtime->oss.buffer = NULL;
2396 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2397 snd_pcm_oss_plugin_clear(substream);
2399 substream->oss.oss = 0;
2402 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2403 struct snd_pcm_oss_setup *setup,
2406 struct snd_pcm_runtime *runtime;
2408 substream->oss.oss = 1;
2409 substream->oss.setup = *setup;
2410 if (setup->nonblock)
2411 substream->f_flags |= O_NONBLOCK;
2412 else if (setup->block)
2413 substream->f_flags &= ~O_NONBLOCK;
2414 runtime = substream->runtime;
2415 runtime->oss.params = 1;
2416 runtime->oss.trigger = 1;
2417 runtime->oss.rate = 8000;
2418 mutex_init(&runtime->oss.params_lock);
2419 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2420 case SNDRV_MINOR_OSS_PCM_8:
2421 runtime->oss.format = AFMT_U8;
2423 case SNDRV_MINOR_OSS_PCM_16:
2424 runtime->oss.format = AFMT_S16_LE;
2427 runtime->oss.format = AFMT_MU_LAW;
2429 runtime->oss.channels = 1;
2430 runtime->oss.fragshift = 0;
2431 runtime->oss.maxfrags = 0;
2432 runtime->oss.subdivision = 0;
2433 substream->pcm_release = snd_pcm_oss_release_substream;
2434 atomic_set(&runtime->oss.rw_ref, 0);
2437 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2442 for (cidx = 0; cidx < 2; ++cidx) {
2443 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2445 snd_pcm_release_substream(substream);
2447 kfree(pcm_oss_file);
2451 static int snd_pcm_oss_open_file(struct file *file,
2452 struct snd_pcm *pcm,
2453 struct snd_pcm_oss_file **rpcm_oss_file,
2455 struct snd_pcm_oss_setup *setup)
2458 struct snd_pcm_oss_file *pcm_oss_file;
2459 struct snd_pcm_substream *substream;
2460 fmode_t f_mode = file->f_mode;
2463 *rpcm_oss_file = NULL;
2465 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2466 if (pcm_oss_file == NULL)
2469 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2470 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2471 f_mode = FMODE_WRITE;
2473 file->f_flags &= ~O_APPEND;
2474 for (idx = 0; idx < 2; idx++) {
2475 if (setup[idx].disable)
2477 if (! pcm->streams[idx].substream_count)
2478 continue; /* no matching substream */
2479 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2480 if (! (f_mode & FMODE_WRITE))
2483 if (! (f_mode & FMODE_READ))
2486 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2488 snd_pcm_oss_release_file(pcm_oss_file);
2492 pcm_oss_file->streams[idx] = substream;
2493 substream->file = pcm_oss_file;
2494 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2497 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2498 snd_pcm_oss_release_file(pcm_oss_file);
2502 file->private_data = pcm_oss_file;
2504 *rpcm_oss_file = pcm_oss_file;
2509 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2513 if (snd_BUG_ON(!task || !name || size < 2))
2515 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2516 name[idx] = task->comm[idx];
2521 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2525 struct snd_pcm *pcm;
2526 struct snd_pcm_oss_file *pcm_oss_file;
2527 struct snd_pcm_oss_setup setup[2];
2531 err = nonseekable_open(inode, file);
2535 pcm = snd_lookup_oss_minor_data(iminor(inode),
2536 SNDRV_OSS_DEVICE_TYPE_PCM);
2541 err = snd_card_file_add(pcm->card, file);
2544 if (!try_module_get(pcm->card->module)) {
2548 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2552 memset(setup, 0, sizeof(setup));
2553 if (file->f_mode & FMODE_WRITE)
2554 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2555 task_name, &setup[0]);
2556 if (file->f_mode & FMODE_READ)
2557 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2558 task_name, &setup[1]);
2560 nonblock = !!(file->f_flags & O_NONBLOCK);
2562 nonblock = nonblock_open;
2564 init_waitqueue_entry(&wait, current);
2565 add_wait_queue(&pcm->open_wait, &wait);
2566 mutex_lock(&pcm->open_mutex);
2568 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2569 iminor(inode), setup);
2572 if (err == -EAGAIN) {
2579 set_current_state(TASK_INTERRUPTIBLE);
2580 mutex_unlock(&pcm->open_mutex);
2582 mutex_lock(&pcm->open_mutex);
2583 if (pcm->card->shutdown) {
2587 if (signal_pending(current)) {
2592 remove_wait_queue(&pcm->open_wait, &wait);
2593 mutex_unlock(&pcm->open_mutex);
2596 snd_card_unref(pcm->card);
2600 module_put(pcm->card->module);
2602 snd_card_file_remove(pcm->card, file);
2605 snd_card_unref(pcm->card);
2609 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2611 struct snd_pcm *pcm;
2612 struct snd_pcm_substream *substream;
2613 struct snd_pcm_oss_file *pcm_oss_file;
2615 pcm_oss_file = file->private_data;
2616 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2617 if (substream == NULL)
2618 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2619 if (snd_BUG_ON(!substream))
2621 pcm = substream->pcm;
2622 if (!pcm->card->shutdown)
2623 snd_pcm_oss_sync(pcm_oss_file);
2624 mutex_lock(&pcm->open_mutex);
2625 snd_pcm_oss_release_file(pcm_oss_file);
2626 mutex_unlock(&pcm->open_mutex);
2627 wake_up(&pcm->open_wait);
2628 module_put(pcm->card->module);
2629 snd_card_file_remove(pcm->card, file);
2633 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2635 struct snd_pcm_oss_file *pcm_oss_file;
2636 int __user *p = (int __user *)arg;
2639 pcm_oss_file = file->private_data;
2640 if (cmd == OSS_GETVERSION)
2641 return put_user(SNDRV_OSS_VERSION, p);
2642 if (cmd == OSS_ALSAEMULVER)
2643 return put_user(1, p);
2644 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2645 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2646 struct snd_pcm_substream *substream;
2648 for (idx = 0; idx < 2; ++idx) {
2649 substream = pcm_oss_file->streams[idx];
2650 if (substream != NULL)
2653 if (snd_BUG_ON(idx >= 2))
2655 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2658 if (((cmd >> 8) & 0xff) != 'P')
2661 pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2664 case SNDCTL_DSP_RESET:
2665 return snd_pcm_oss_reset(pcm_oss_file);
2666 case SNDCTL_DSP_SYNC:
2667 return snd_pcm_oss_sync(pcm_oss_file);
2668 case SNDCTL_DSP_SPEED:
2669 if (get_user(res, p))
2671 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2673 return put_user(res, p);
2674 case SOUND_PCM_READ_RATE:
2675 res = snd_pcm_oss_get_rate(pcm_oss_file);
2678 return put_user(res, p);
2679 case SNDCTL_DSP_STEREO:
2680 if (get_user(res, p))
2682 res = res > 0 ? 2 : 1;
2683 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2685 return put_user(--res, p);
2686 case SNDCTL_DSP_GETBLKSIZE:
2687 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2690 return put_user(res, p);
2691 case SNDCTL_DSP_SETFMT:
2692 if (get_user(res, p))
2694 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2697 return put_user(res, p);
2698 case SOUND_PCM_READ_BITS:
2699 res = snd_pcm_oss_get_format(pcm_oss_file);
2702 return put_user(res, p);
2703 case SNDCTL_DSP_CHANNELS:
2704 if (get_user(res, p))
2706 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2709 return put_user(res, p);
2710 case SOUND_PCM_READ_CHANNELS:
2711 res = snd_pcm_oss_get_channels(pcm_oss_file);
2714 return put_user(res, p);
2715 case SOUND_PCM_WRITE_FILTER:
2716 case SOUND_PCM_READ_FILTER:
2718 case SNDCTL_DSP_POST:
2719 return snd_pcm_oss_post(pcm_oss_file);
2720 case SNDCTL_DSP_SUBDIVIDE:
2721 if (get_user(res, p))
2723 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2726 return put_user(res, p);
2727 case SNDCTL_DSP_SETFRAGMENT:
2728 if (get_user(res, p))
2730 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2731 case SNDCTL_DSP_GETFMTS:
2732 res = snd_pcm_oss_get_formats(pcm_oss_file);
2735 return put_user(res, p);
2736 case SNDCTL_DSP_GETOSPACE:
2737 case SNDCTL_DSP_GETISPACE:
2738 return snd_pcm_oss_get_space(pcm_oss_file,
2739 cmd == SNDCTL_DSP_GETISPACE ?
2740 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2741 (struct audio_buf_info __user *) arg);
2742 case SNDCTL_DSP_NONBLOCK:
2743 return snd_pcm_oss_nonblock(file);
2744 case SNDCTL_DSP_GETCAPS:
2745 res = snd_pcm_oss_get_caps(pcm_oss_file);
2748 return put_user(res, p);
2749 case SNDCTL_DSP_GETTRIGGER:
2750 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2753 return put_user(res, p);
2754 case SNDCTL_DSP_SETTRIGGER:
2755 if (get_user(res, p))
2757 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2758 case SNDCTL_DSP_GETIPTR:
2759 case SNDCTL_DSP_GETOPTR:
2760 return snd_pcm_oss_get_ptr(pcm_oss_file,
2761 cmd == SNDCTL_DSP_GETIPTR ?
2762 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2763 (struct count_info __user *) arg);
2764 case SNDCTL_DSP_MAPINBUF:
2765 case SNDCTL_DSP_MAPOUTBUF:
2766 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2767 cmd == SNDCTL_DSP_MAPINBUF ?
2768 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2769 (struct buffmem_desc __user *) arg);
2770 case SNDCTL_DSP_SETSYNCRO:
2771 /* stop DMA now.. */
2773 case SNDCTL_DSP_SETDUPLEX:
2774 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2777 case SNDCTL_DSP_GETODELAY:
2778 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2780 /* it's for sure, some broken apps don't check for error codes */
2784 return put_user(res, p);
2785 case SNDCTL_DSP_PROFILE:
2786 return 0; /* silently ignore */
2788 pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2793 #ifdef CONFIG_COMPAT
2794 /* all compatible */
2795 static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
2798 return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2801 #define snd_pcm_oss_ioctl_compat NULL
2804 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2806 struct snd_pcm_oss_file *pcm_oss_file;
2807 struct snd_pcm_substream *substream;
2809 pcm_oss_file = file->private_data;
2810 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2811 if (substream == NULL)
2813 substream->f_flags = file->f_flags & O_NONBLOCK;
2815 return snd_pcm_oss_read1(substream, buf, count);
2818 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2819 pcm_dbg(substream->pcm,
2820 "pcm_oss: read %li bytes (returned %li bytes)\n",
2821 (long)count, (long)res);
2827 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2829 struct snd_pcm_oss_file *pcm_oss_file;
2830 struct snd_pcm_substream *substream;
2833 pcm_oss_file = file->private_data;
2834 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2835 if (substream == NULL)
2837 substream->f_flags = file->f_flags & O_NONBLOCK;
2838 result = snd_pcm_oss_write1(substream, buf, count);
2840 pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2841 (long)count, (long)result);
2846 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2848 struct snd_pcm_runtime *runtime = substream->runtime;
2849 if (atomic_read(&substream->mmap_count))
2850 return runtime->oss.prev_hw_ptr_period !=
2851 get_hw_ptr_period(runtime);
2853 return snd_pcm_playback_avail(runtime) >=
2854 runtime->oss.period_frames;
2857 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2859 struct snd_pcm_runtime *runtime = substream->runtime;
2860 if (atomic_read(&substream->mmap_count))
2861 return runtime->oss.prev_hw_ptr_period !=
2862 get_hw_ptr_period(runtime);
2864 return snd_pcm_capture_avail(runtime) >=
2865 runtime->oss.period_frames;
2868 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2870 struct snd_pcm_oss_file *pcm_oss_file;
2872 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2874 pcm_oss_file = file->private_data;
2876 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2877 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2880 if (psubstream != NULL) {
2881 struct snd_pcm_runtime *runtime = psubstream->runtime;
2882 poll_wait(file, &runtime->sleep, wait);
2883 snd_pcm_stream_lock_irq(psubstream);
2884 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2885 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2886 snd_pcm_oss_playback_ready(psubstream)))
2887 mask |= POLLOUT | POLLWRNORM;
2888 snd_pcm_stream_unlock_irq(psubstream);
2890 if (csubstream != NULL) {
2891 struct snd_pcm_runtime *runtime = csubstream->runtime;
2892 snd_pcm_state_t ostate;
2893 poll_wait(file, &runtime->sleep, wait);
2894 snd_pcm_stream_lock_irq(csubstream);
2895 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2896 snd_pcm_oss_capture_ready(csubstream))
2897 mask |= POLLIN | POLLRDNORM;
2898 snd_pcm_stream_unlock_irq(csubstream);
2899 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2900 struct snd_pcm_oss_file ofile;
2901 memset(&ofile, 0, sizeof(ofile));
2902 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2903 runtime->oss.trigger = 0;
2904 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2911 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2913 struct snd_pcm_oss_file *pcm_oss_file;
2914 struct snd_pcm_substream *substream = NULL;
2915 struct snd_pcm_runtime *runtime;
2919 pr_debug("pcm_oss: mmap begin\n");
2921 pcm_oss_file = file->private_data;
2922 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2923 case VM_READ | VM_WRITE:
2924 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2929 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2932 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2937 /* set VM_READ access as well to fix memset() routines that do
2938 reads before writes (to improve performance) */
2939 area->vm_flags |= VM_READ;
2940 if (substream == NULL)
2942 runtime = substream->runtime;
2943 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2945 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2946 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2950 if (runtime->oss.params) {
2951 /* use mutex_trylock() for params_lock for avoiding a deadlock
2952 * between mmap_sem and params_lock taken by
2953 * copy_from/to_user() in snd_pcm_oss_write/read()
2955 err = snd_pcm_oss_change_params(substream, true);
2959 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2960 if (runtime->oss.plugin_first != NULL)
2964 if (area->vm_pgoff != 0)
2967 err = snd_pcm_mmap_data(substream, file, area);
2970 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2971 runtime->silence_threshold = 0;
2972 runtime->silence_size = 0;
2974 pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2975 runtime->oss.mmap_bytes);
2977 /* In mmap mode we never stop */
2978 runtime->stop_threshold = runtime->boundary;
2983 #ifdef CONFIG_SND_VERBOSE_PROCFS
2988 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2989 struct snd_info_buffer *buffer)
2991 struct snd_pcm_str *pstr = entry->private_data;
2992 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2993 mutex_lock(&pstr->oss.setup_mutex);
2995 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2999 setup->disable ? " disable" : "",
3000 setup->direct ? " direct" : "",
3001 setup->block ? " block" : "",
3002 setup->nonblock ? " non-block" : "",
3003 setup->partialfrag ? " partial-frag" : "",
3004 setup->nosilence ? " no-silence" : "");
3005 setup = setup->next;
3007 mutex_unlock(&pstr->oss.setup_mutex);
3010 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
3012 struct snd_pcm_oss_setup *setup, *setupn;
3014 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
3015 setup; setup = setupn) {
3016 setupn = setup->next;
3017 kfree(setup->task_name);
3020 pstr->oss.setup_list = NULL;
3023 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
3024 struct snd_info_buffer *buffer)
3026 struct snd_pcm_str *pstr = entry->private_data;
3027 char line[128], str[32], task_name[32];
3030 struct snd_pcm_oss_setup *setup, *setup1, template;
3032 while (!snd_info_get_line(buffer, line, sizeof(line))) {
3033 mutex_lock(&pstr->oss.setup_mutex);
3034 memset(&template, 0, sizeof(template));
3035 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
3036 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
3037 snd_pcm_oss_proc_free_setup_list(pstr);
3038 mutex_unlock(&pstr->oss.setup_mutex);
3041 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
3042 if (!strcmp(setup->task_name, task_name)) {
3047 ptr = snd_info_get_str(str, ptr, sizeof(str));
3048 template.periods = simple_strtoul(str, NULL, 10);
3049 ptr = snd_info_get_str(str, ptr, sizeof(str));
3050 template.period_size = simple_strtoul(str, NULL, 10);
3051 for (idx1 = 31; idx1 >= 0; idx1--)
3052 if (template.period_size & (1 << idx1))
3054 for (idx1--; idx1 >= 0; idx1--)
3055 template.period_size &= ~(1 << idx1);
3057 ptr = snd_info_get_str(str, ptr, sizeof(str));
3058 if (!strcmp(str, "disable")) {
3059 template.disable = 1;
3060 } else if (!strcmp(str, "direct")) {
3061 template.direct = 1;
3062 } else if (!strcmp(str, "block")) {
3064 } else if (!strcmp(str, "non-block")) {
3065 template.nonblock = 1;
3066 } else if (!strcmp(str, "partial-frag")) {
3067 template.partialfrag = 1;
3068 } else if (!strcmp(str, "no-silence")) {
3069 template.nosilence = 1;
3070 } else if (!strcmp(str, "buggy-ptr")) {
3071 template.buggyptr = 1;
3074 if (setup == NULL) {
3075 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
3077 buffer->error = -ENOMEM;
3078 mutex_unlock(&pstr->oss.setup_mutex);
3081 if (pstr->oss.setup_list == NULL)
3082 pstr->oss.setup_list = setup;
3084 for (setup1 = pstr->oss.setup_list;
3085 setup1->next; setup1 = setup1->next);
3086 setup1->next = setup;
3088 template.task_name = kstrdup(task_name, GFP_KERNEL);
3089 if (! template.task_name) {
3091 buffer->error = -ENOMEM;
3092 mutex_unlock(&pstr->oss.setup_mutex);
3097 mutex_unlock(&pstr->oss.setup_mutex);
3101 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3104 for (stream = 0; stream < 2; ++stream) {
3105 struct snd_info_entry *entry;
3106 struct snd_pcm_str *pstr = &pcm->streams[stream];
3107 if (pstr->substream_count == 0)
3109 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
3110 entry->content = SNDRV_INFO_CONTENT_TEXT;
3111 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
3112 entry->c.text.read = snd_pcm_oss_proc_read;
3113 entry->c.text.write = snd_pcm_oss_proc_write;
3114 entry->private_data = pstr;
3115 if (snd_info_register(entry) < 0) {
3116 snd_info_free_entry(entry);
3120 pstr->oss.proc_entry = entry;
3124 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3127 for (stream = 0; stream < 2; ++stream) {
3128 struct snd_pcm_str *pstr = &pcm->streams[stream];
3129 snd_info_free_entry(pstr->oss.proc_entry);
3130 pstr->oss.proc_entry = NULL;
3131 snd_pcm_oss_proc_free_setup_list(pstr);
3134 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3135 #define snd_pcm_oss_proc_init(pcm)
3136 #define snd_pcm_oss_proc_done(pcm)
3137 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3143 static const struct file_operations snd_pcm_oss_f_reg =
3145 .owner = THIS_MODULE,
3146 .read = snd_pcm_oss_read,
3147 .write = snd_pcm_oss_write,
3148 .open = snd_pcm_oss_open,
3149 .release = snd_pcm_oss_release,
3150 .llseek = no_llseek,
3151 .poll = snd_pcm_oss_poll,
3152 .unlocked_ioctl = snd_pcm_oss_ioctl,
3153 .compat_ioctl = snd_pcm_oss_ioctl_compat,
3154 .mmap = snd_pcm_oss_mmap,
3157 static void register_oss_dsp(struct snd_pcm *pcm, int index)
3159 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3160 pcm->card, index, &snd_pcm_oss_f_reg,
3162 pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3163 pcm->card->number, pcm->device);
3167 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3170 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3173 register_oss_dsp(pcm, 0);
3174 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3175 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3176 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3177 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3178 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3179 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3184 pcm->oss.reg_mask |= 1;
3186 if (adsp_map[pcm->card->number] == (int)pcm->device) {
3187 register_oss_dsp(pcm, 1);
3189 pcm->oss.reg_mask |= 2;
3193 snd_pcm_oss_proc_init(pcm);
3198 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3201 if (pcm->oss.reg_mask & 1) {
3202 pcm->oss.reg_mask &= ~1;
3203 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3206 if (pcm->oss.reg_mask & 2) {
3207 pcm->oss.reg_mask &= ~2;
3208 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3211 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3212 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3213 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3221 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3223 snd_pcm_oss_disconnect_minor(pcm);
3224 snd_pcm_oss_proc_done(pcm);
3228 static struct snd_pcm_notify snd_pcm_oss_notify =
3230 .n_register = snd_pcm_oss_register_minor,
3231 .n_disconnect = snd_pcm_oss_disconnect_minor,
3232 .n_unregister = snd_pcm_oss_unregister_minor,
3235 static int __init alsa_pcm_oss_init(void)
3240 /* check device map table */
3241 for (i = 0; i < SNDRV_CARDS; i++) {
3242 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3243 pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3247 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3248 pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3253 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3258 static void __exit alsa_pcm_oss_exit(void)
3260 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3263 module_init(alsa_pcm_oss_init)
3264 module_exit(alsa_pcm_oss_exit)