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 ssize_t oss_buffer_size;
713 ssize_t oss_period_size, oss_periods;
714 ssize_t min_period_size, max_period_size;
715 struct snd_pcm_runtime *runtime = substream->runtime;
716 size_t oss_frame_size;
718 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
719 params_channels(oss_params) / 8;
721 oss_buffer_size = snd_pcm_hw_param_value_max(slave_params,
722 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
724 if (oss_buffer_size <= 0)
726 oss_buffer_size = snd_pcm_plug_client_size(substream,
727 oss_buffer_size * oss_frame_size);
728 if (oss_buffer_size <= 0)
730 oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
731 if (atomic_read(&substream->mmap_count)) {
732 if (oss_buffer_size > runtime->oss.mmap_bytes)
733 oss_buffer_size = runtime->oss.mmap_bytes;
736 if (substream->oss.setup.period_size > 16)
737 oss_period_size = substream->oss.setup.period_size;
738 else if (runtime->oss.fragshift) {
739 oss_period_size = 1 << runtime->oss.fragshift;
740 if (oss_period_size > oss_buffer_size / 2)
741 oss_period_size = oss_buffer_size / 2;
744 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
746 oss_period_size = oss_buffer_size;
748 oss_period_size /= 2;
749 } while (oss_period_size > bytes_per_sec);
750 if (runtime->oss.subdivision == 0) {
752 if (oss_period_size / sd > 4096)
754 if (oss_period_size / sd < 4096)
757 sd = runtime->oss.subdivision;
758 oss_period_size /= sd;
759 if (oss_period_size < 16)
760 oss_period_size = 16;
763 min_period_size = snd_pcm_plug_client_size(substream,
764 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
765 if (min_period_size > 0) {
766 min_period_size *= oss_frame_size;
767 min_period_size = roundup_pow_of_two(min_period_size);
768 if (oss_period_size < min_period_size)
769 oss_period_size = min_period_size;
772 max_period_size = snd_pcm_plug_client_size(substream,
773 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
774 if (max_period_size > 0) {
775 max_period_size *= oss_frame_size;
776 max_period_size = rounddown_pow_of_two(max_period_size);
777 if (oss_period_size > max_period_size)
778 oss_period_size = max_period_size;
781 oss_periods = oss_buffer_size / oss_period_size;
783 if (substream->oss.setup.periods > 1)
784 oss_periods = substream->oss.setup.periods;
786 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
787 if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
788 s = runtime->oss.maxfrags;
792 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
798 while (oss_period_size * oss_periods > oss_buffer_size)
799 oss_period_size /= 2;
801 if (oss_period_size < 16)
803 runtime->oss.period_bytes = oss_period_size;
804 runtime->oss.period_frames = 1;
805 runtime->oss.periods = oss_periods;
809 static int choose_rate(struct snd_pcm_substream *substream,
810 struct snd_pcm_hw_params *params, unsigned int best_rate)
812 struct snd_interval *it;
813 struct snd_pcm_hw_params *save;
814 unsigned int rate, prev;
816 save = kmalloc(sizeof(*save), GFP_KERNEL);
820 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
822 /* try multiples of the best rate */
825 if (it->max < rate || (it->max == rate && it->openmax))
827 if (it->min < rate || (it->min == rate && !it->openmin)) {
829 ret = snd_pcm_hw_param_set(substream, params,
830 SNDRV_PCM_HW_PARAM_RATE,
832 if (ret == (int)rate) {
844 /* not found, use the nearest rate */
846 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
849 /* parameter locking: returns immediately if tried during streaming */
850 static int lock_params(struct snd_pcm_runtime *runtime)
852 if (mutex_lock_interruptible(&runtime->oss.params_lock))
854 if (atomic_read(&runtime->oss.rw_ref)) {
855 mutex_unlock(&runtime->oss.params_lock);
861 static void unlock_params(struct snd_pcm_runtime *runtime)
863 mutex_unlock(&runtime->oss.params_lock);
866 /* call with params_lock held */
867 static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
869 struct snd_pcm_runtime *runtime = substream->runtime;
870 struct snd_pcm_hw_params *params, *sparams;
871 struct snd_pcm_sw_params *sw_params;
872 ssize_t oss_buffer_size, oss_period_size;
873 size_t oss_frame_size;
876 snd_pcm_format_t format, sformat;
878 struct snd_mask sformat_mask;
879 struct snd_mask mask;
881 if (!runtime->oss.params)
883 sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
884 params = kmalloc(sizeof(*params), GFP_KERNEL);
885 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
886 if (!sw_params || !params || !sparams) {
891 if (atomic_read(&substream->mmap_count))
894 direct = substream->oss.setup.direct;
896 _snd_pcm_hw_params_any(sparams);
897 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
898 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
899 snd_mask_none(&mask);
900 if (atomic_read(&substream->mmap_count))
901 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
903 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
905 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
907 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
909 pcm_dbg(substream->pcm, "No usable accesses\n");
914 err = choose_rate(substream, sparams, runtime->oss.rate);
917 err = snd_pcm_hw_param_near(substream, sparams,
918 SNDRV_PCM_HW_PARAM_CHANNELS,
919 runtime->oss.channels, NULL);
923 format = snd_pcm_oss_format_from(runtime->oss.format);
925 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
929 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
931 if ((__force int)sformat < 0 ||
932 !snd_mask_test(&sformat_mask, (__force int)sformat)) {
933 for (sformat = (__force snd_pcm_format_t)0;
934 (__force int)sformat <= (__force int)SNDRV_PCM_FORMAT_LAST;
935 sformat = (__force snd_pcm_format_t)((__force int)sformat + 1)) {
936 if (snd_mask_test(&sformat_mask, (__force int)sformat) &&
937 snd_pcm_oss_format_to(sformat) >= 0)
940 if ((__force int)sformat > (__force int)SNDRV_PCM_FORMAT_LAST) {
941 pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
946 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
951 memcpy(params, sparams, sizeof(*params));
953 _snd_pcm_hw_params_any(params);
954 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
955 (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
956 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
957 (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
958 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
959 runtime->oss.channels, 0);
960 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
961 runtime->oss.rate, 0);
962 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
963 params_access(params), params_format(params),
964 params_channels(params), params_rate(params));
966 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
967 params_access(sparams), params_format(sparams),
968 params_channels(sparams), params_rate(sparams));
970 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
971 params_channels(params) / 8;
973 err = snd_pcm_oss_period_size(substream, params, sparams);
977 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
978 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
982 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
983 runtime->oss.periods, NULL);
987 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
989 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
991 pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
995 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
996 snd_pcm_oss_plugin_clear(substream);
998 /* add necessary plugins */
999 snd_pcm_oss_plugin_clear(substream);
1000 if ((err = snd_pcm_plug_format_plugins(substream,
1003 pcm_dbg(substream->pcm,
1004 "snd_pcm_plug_format_plugins failed: %i\n", err);
1005 snd_pcm_oss_plugin_clear(substream);
1008 if (runtime->oss.plugin_first) {
1009 struct snd_pcm_plugin *plugin;
1010 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
1011 pcm_dbg(substream->pcm,
1012 "snd_pcm_plugin_build_io failed: %i\n", err);
1013 snd_pcm_oss_plugin_clear(substream);
1016 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1017 err = snd_pcm_plugin_append(plugin);
1019 err = snd_pcm_plugin_insert(plugin);
1022 snd_pcm_oss_plugin_clear(substream);
1029 if (runtime->oss.trigger) {
1030 sw_params->start_threshold = 1;
1032 sw_params->start_threshold = runtime->boundary;
1034 if (atomic_read(&substream->mmap_count) ||
1035 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1036 sw_params->stop_threshold = runtime->boundary;
1038 sw_params->stop_threshold = runtime->buffer_size;
1039 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
1040 sw_params->period_step = 1;
1041 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1042 1 : runtime->period_size;
1043 if (atomic_read(&substream->mmap_count) ||
1044 substream->oss.setup.nosilence) {
1045 sw_params->silence_threshold = 0;
1046 sw_params->silence_size = 0;
1048 snd_pcm_uframes_t frames;
1049 frames = runtime->period_size + 16;
1050 if (frames > runtime->buffer_size)
1051 frames = runtime->buffer_size;
1052 sw_params->silence_threshold = frames;
1053 sw_params->silence_size = frames;
1056 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1057 pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
1061 runtime->oss.periods = params_periods(sparams);
1062 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1063 if (oss_period_size < 0) {
1067 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1068 if (runtime->oss.plugin_first) {
1069 err = snd_pcm_plug_alloc(substream, oss_period_size);
1074 oss_period_size *= oss_frame_size;
1076 oss_buffer_size = oss_period_size * runtime->oss.periods;
1077 if (oss_buffer_size < 0) {
1082 runtime->oss.period_bytes = oss_period_size;
1083 runtime->oss.buffer_bytes = oss_buffer_size;
1085 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1086 runtime->oss.period_bytes,
1087 runtime->oss.buffer_bytes);
1088 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1089 params_period_size(sparams),
1090 params_buffer_size(sparams));
1092 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1093 runtime->oss.channels = params_channels(params);
1094 runtime->oss.rate = params_rate(params);
1096 vfree(runtime->oss.buffer);
1097 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1098 if (!runtime->oss.buffer) {
1103 runtime->oss.params = 0;
1104 runtime->oss.prepare = 1;
1105 runtime->oss.buffer_used = 0;
1106 if (runtime->dma_area)
1107 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1109 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1119 /* this one takes the lock by itself */
1120 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
1123 struct snd_pcm_runtime *runtime = substream->runtime;
1127 if (!(mutex_trylock(&runtime->oss.params_lock)))
1129 } else if (mutex_lock_interruptible(&runtime->oss.params_lock))
1130 return -ERESTARTSYS;
1132 err = snd_pcm_oss_change_params_locked(substream);
1133 mutex_unlock(&runtime->oss.params_lock);
1137 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1140 struct snd_pcm_substream *asubstream = NULL, *substream;
1142 for (idx = 0; idx < 2; idx++) {
1143 substream = pcm_oss_file->streams[idx];
1144 if (substream == NULL)
1146 if (asubstream == NULL)
1147 asubstream = substream;
1148 if (substream->runtime->oss.params) {
1149 err = snd_pcm_oss_change_params(substream, false);
1157 *r_substream = asubstream;
1161 /* call with params_lock held */
1162 /* NOTE: this always call PREPARE unconditionally no matter whether
1163 * runtime->oss.prepare is set or not
1165 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1168 struct snd_pcm_runtime *runtime = substream->runtime;
1170 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1172 pcm_dbg(substream->pcm,
1173 "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1176 runtime->oss.prepare = 0;
1177 runtime->oss.prev_hw_ptr_period = 0;
1178 runtime->oss.period_ptr = 0;
1179 runtime->oss.buffer_used = 0;
1184 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1186 struct snd_pcm_runtime *runtime;
1189 runtime = substream->runtime;
1190 if (runtime->oss.params) {
1191 err = snd_pcm_oss_change_params(substream, false);
1195 if (runtime->oss.prepare) {
1196 if (mutex_lock_interruptible(&runtime->oss.params_lock))
1197 return -ERESTARTSYS;
1198 err = snd_pcm_oss_prepare(substream);
1199 mutex_unlock(&runtime->oss.params_lock);
1206 /* call with params_lock held */
1207 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1209 struct snd_pcm_runtime *runtime;
1212 runtime = substream->runtime;
1213 if (runtime->oss.params) {
1214 err = snd_pcm_oss_change_params_locked(substream);
1218 if (runtime->oss.prepare) {
1219 err = snd_pcm_oss_prepare(substream);
1226 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1228 struct snd_pcm_runtime *runtime;
1229 snd_pcm_uframes_t frames;
1233 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1236 runtime = substream->runtime;
1237 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1239 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1240 /* until avail(delay) <= buffer_size */
1241 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1242 frames /= runtime->period_size;
1243 frames *= runtime->period_size;
1244 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1251 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1253 struct snd_pcm_runtime *runtime = substream->runtime;
1256 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1257 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1259 pcm_dbg(substream->pcm,
1260 "pcm_oss: write: recovering from %s\n",
1261 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1262 "XRUN" : "SUSPEND");
1264 ret = snd_pcm_oss_prepare(substream);
1270 fs = snd_enter_user();
1271 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1274 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1276 if (ret != -EPIPE && ret != -ESTRPIPE)
1278 /* test, if we can't store new data, because the stream */
1279 /* has not been started */
1280 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1286 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1288 struct snd_pcm_runtime *runtime = substream->runtime;
1289 snd_pcm_sframes_t delay;
1292 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1293 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1295 pcm_dbg(substream->pcm,
1296 "pcm_oss: read: recovering from %s\n",
1297 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1298 "XRUN" : "SUSPEND");
1300 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1303 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1304 ret = snd_pcm_oss_prepare(substream);
1308 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1313 fs = snd_enter_user();
1314 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1317 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1319 if (ret == -EPIPE) {
1320 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1321 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1327 if (ret != -ESTRPIPE)
1333 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1335 struct snd_pcm_runtime *runtime = substream->runtime;
1338 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1339 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1341 pcm_dbg(substream->pcm,
1342 "pcm_oss: writev: recovering from %s\n",
1343 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1344 "XRUN" : "SUSPEND");
1346 ret = snd_pcm_oss_prepare(substream);
1352 fs = snd_enter_user();
1353 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1356 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1358 if (ret != -EPIPE && ret != -ESTRPIPE)
1361 /* test, if we can't store new data, because the stream */
1362 /* has not been started */
1363 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1369 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1371 struct snd_pcm_runtime *runtime = substream->runtime;
1374 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1375 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1377 pcm_dbg(substream->pcm,
1378 "pcm_oss: readv: recovering from %s\n",
1379 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1380 "XRUN" : "SUSPEND");
1382 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1385 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1386 ret = snd_pcm_oss_prepare(substream);
1392 fs = snd_enter_user();
1393 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1396 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1398 if (ret != -EPIPE && ret != -ESTRPIPE)
1404 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1406 struct snd_pcm_runtime *runtime = substream->runtime;
1407 snd_pcm_sframes_t frames, frames1;
1408 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1409 if (runtime->oss.plugin_first) {
1410 struct snd_pcm_plugin_channel *channels;
1411 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1413 if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1415 buf = runtime->oss.buffer;
1417 frames = bytes / oss_frame_bytes;
1418 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1421 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1424 bytes = frames1 * oss_frame_bytes;
1428 frames = bytes_to_frames(runtime, bytes);
1429 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1432 bytes = frames_to_bytes(runtime, frames1);
1437 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1443 if (atomic_read(&substream->mmap_count))
1446 atomic_inc(&runtime->oss.rw_ref);
1448 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1452 tmp = snd_pcm_oss_make_ready_locked(substream);
1455 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1457 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1458 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1460 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1465 runtime->oss.buffer_used += tmp;
1469 if (substream->oss.setup.partialfrag ||
1470 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1471 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1472 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1475 runtime->oss.bytes += tmp;
1476 runtime->oss.period_ptr += tmp;
1477 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1478 if (runtime->oss.period_ptr == 0 ||
1479 runtime->oss.period_ptr == runtime->oss.buffer_used)
1480 runtime->oss.buffer_used = 0;
1481 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1487 tmp = snd_pcm_oss_write2(substream,
1488 (const char __force *)buf,
1489 runtime->oss.period_bytes, 0);
1492 runtime->oss.bytes += tmp;
1496 if ((substream->f_flags & O_NONBLOCK) != 0 &&
1497 tmp != runtime->oss.period_bytes)
1501 mutex_unlock(&runtime->oss.params_lock);
1504 if (signal_pending(current)) {
1510 atomic_dec(&runtime->oss.rw_ref);
1511 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1514 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1516 struct snd_pcm_runtime *runtime = substream->runtime;
1517 snd_pcm_sframes_t frames, frames1;
1518 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1519 char __user *final_dst = (char __force __user *)buf;
1520 if (runtime->oss.plugin_first) {
1521 struct snd_pcm_plugin_channel *channels;
1522 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1524 buf = runtime->oss.buffer;
1525 frames = bytes / oss_frame_bytes;
1526 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1529 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1532 bytes = frames1 * oss_frame_bytes;
1533 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1538 frames = bytes_to_frames(runtime, bytes);
1539 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1542 bytes = frames_to_bytes(runtime, frames1);
1547 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1551 struct snd_pcm_runtime *runtime = substream->runtime;
1553 if (atomic_read(&substream->mmap_count))
1556 atomic_inc(&runtime->oss.rw_ref);
1558 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1562 tmp = snd_pcm_oss_make_ready_locked(substream);
1565 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1566 if (runtime->oss.buffer_used == 0) {
1567 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1570 runtime->oss.bytes += tmp;
1571 runtime->oss.period_ptr = tmp;
1572 runtime->oss.buffer_used = tmp;
1575 if ((size_t) tmp > runtime->oss.buffer_used)
1576 tmp = runtime->oss.buffer_used;
1577 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1584 runtime->oss.buffer_used -= tmp;
1586 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1587 runtime->oss.period_bytes, 0);
1590 runtime->oss.bytes += tmp;
1596 mutex_unlock(&runtime->oss.params_lock);
1599 if (signal_pending(current)) {
1605 atomic_dec(&runtime->oss.rw_ref);
1606 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1609 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1611 struct snd_pcm_substream *substream;
1612 struct snd_pcm_runtime *runtime;
1615 for (i = 0; i < 2; i++) {
1616 substream = pcm_oss_file->streams[i];
1619 runtime = substream->runtime;
1620 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1621 mutex_lock(&runtime->oss.params_lock);
1622 runtime->oss.prepare = 1;
1623 runtime->oss.buffer_used = 0;
1624 runtime->oss.prev_hw_ptr_period = 0;
1625 runtime->oss.period_ptr = 0;
1626 mutex_unlock(&runtime->oss.params_lock);
1631 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1633 struct snd_pcm_substream *substream;
1636 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1637 if (substream != NULL) {
1638 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1640 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1642 /* note: all errors from the start action are ignored */
1643 /* OSS apps do not know, how to handle them */
1647 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1649 struct snd_pcm_runtime *runtime;
1651 snd_pcm_state_t state;
1655 runtime = substream->runtime;
1656 init_waitqueue_entry(&wait, current);
1657 add_wait_queue(&runtime->sleep, &wait);
1659 pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1662 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1664 runtime->oss.buffer_used = 0;
1668 if (result != 0 && result != -EAGAIN)
1671 set_current_state(TASK_INTERRUPTIBLE);
1672 snd_pcm_stream_lock_irq(substream);
1673 state = runtime->status->state;
1674 snd_pcm_stream_unlock_irq(substream);
1675 if (state != SNDRV_PCM_STATE_RUNNING) {
1676 set_current_state(TASK_RUNNING);
1679 res = schedule_timeout(10 * HZ);
1680 if (signal_pending(current)) {
1681 result = -ERESTARTSYS;
1685 pcm_err(substream->pcm,
1686 "OSS sync error - DMA timeout\n");
1691 remove_wait_queue(&runtime->sleep, &wait);
1695 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1698 unsigned int saved_f_flags;
1699 struct snd_pcm_substream *substream;
1700 struct snd_pcm_runtime *runtime;
1701 snd_pcm_format_t format;
1702 unsigned long width;
1705 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1706 if (substream != NULL) {
1707 runtime = substream->runtime;
1708 if (atomic_read(&substream->mmap_count))
1710 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1712 atomic_inc(&runtime->oss.rw_ref);
1713 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1714 atomic_dec(&runtime->oss.rw_ref);
1715 return -ERESTARTSYS;
1717 format = snd_pcm_oss_format_from(runtime->oss.format);
1718 width = snd_pcm_format_physical_width(format);
1719 if (runtime->oss.buffer_used > 0) {
1721 pcm_dbg(substream->pcm, "sync: buffer_used\n");
1723 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1724 snd_pcm_format_set_silence(format,
1725 runtime->oss.buffer + runtime->oss.buffer_used,
1727 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1730 } else if (runtime->oss.period_ptr > 0) {
1732 pcm_dbg(substream->pcm, "sync: period_ptr\n");
1734 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1735 snd_pcm_format_set_silence(format,
1736 runtime->oss.buffer,
1738 err = snd_pcm_oss_sync1(substream, size);
1743 * The ALSA's period might be a bit large than OSS one.
1744 * Fill the remain portion of ALSA period with zeros.
1746 size = runtime->control->appl_ptr % runtime->period_size;
1748 size = runtime->period_size - size;
1749 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1750 size = (runtime->frame_bits * size) / 8;
1753 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1756 size1 /= runtime->sample_bits;
1757 snd_pcm_format_set_silence(runtime->format,
1758 runtime->oss.buffer,
1760 size1 /= runtime->channels; /* frames */
1761 fs = snd_enter_user();
1762 snd_pcm_lib_write(substream, (void __force __user *)runtime->oss.buffer, size1);
1765 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1766 void __user *buffers[runtime->channels];
1767 memset(buffers, 0, runtime->channels * sizeof(void *));
1768 snd_pcm_lib_writev(substream, buffers, size);
1772 mutex_unlock(&runtime->oss.params_lock);
1773 atomic_dec(&runtime->oss.rw_ref);
1777 * finish sync: drain the buffer
1780 saved_f_flags = substream->f_flags;
1781 substream->f_flags &= ~O_NONBLOCK;
1782 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1783 substream->f_flags = saved_f_flags;
1786 mutex_lock(&runtime->oss.params_lock);
1787 runtime->oss.prepare = 1;
1788 mutex_unlock(&runtime->oss.params_lock);
1791 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1792 if (substream != NULL) {
1793 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1795 runtime = substream->runtime;
1796 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1799 mutex_lock(&runtime->oss.params_lock);
1800 runtime->oss.buffer_used = 0;
1801 runtime->oss.prepare = 1;
1802 mutex_unlock(&runtime->oss.params_lock);
1807 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1811 for (idx = 1; idx >= 0; --idx) {
1812 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1813 struct snd_pcm_runtime *runtime;
1816 if (substream == NULL)
1818 runtime = substream->runtime;
1821 else if (rate > 192000)
1823 err = lock_params(runtime);
1826 if (runtime->oss.rate != rate) {
1827 runtime->oss.params = 1;
1828 runtime->oss.rate = rate;
1830 unlock_params(runtime);
1832 return snd_pcm_oss_get_rate(pcm_oss_file);
1835 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1837 struct snd_pcm_substream *substream;
1840 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1842 return substream->runtime->oss.rate;
1845 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1852 for (idx = 1; idx >= 0; --idx) {
1853 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1854 struct snd_pcm_runtime *runtime;
1857 if (substream == NULL)
1859 runtime = substream->runtime;
1860 err = lock_params(runtime);
1863 if (runtime->oss.channels != channels) {
1864 runtime->oss.params = 1;
1865 runtime->oss.channels = channels;
1867 unlock_params(runtime);
1869 return snd_pcm_oss_get_channels(pcm_oss_file);
1872 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1874 struct snd_pcm_substream *substream;
1877 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1879 return substream->runtime->oss.channels;
1882 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1884 struct snd_pcm_substream *substream;
1887 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1889 return substream->runtime->oss.period_bytes;
1892 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1894 struct snd_pcm_substream *substream;
1897 struct snd_pcm_hw_params *params;
1898 unsigned int formats = 0;
1899 struct snd_mask format_mask;
1902 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1904 if (atomic_read(&substream->mmap_count))
1907 direct = substream->oss.setup.direct;
1909 return AFMT_MU_LAW | AFMT_U8 |
1910 AFMT_S16_LE | AFMT_S16_BE |
1911 AFMT_S8 | AFMT_U16_LE |
1913 AFMT_S32_LE | AFMT_S32_BE |
1914 AFMT_S24_LE | AFMT_S24_BE |
1916 params = kmalloc(sizeof(*params), GFP_KERNEL);
1919 _snd_pcm_hw_params_any(params);
1920 err = snd_pcm_hw_refine(substream, params);
1923 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1924 for (fmt = 0; fmt < 32; ++fmt) {
1925 if (snd_mask_test(&format_mask, fmt)) {
1926 int f = snd_pcm_oss_format_to(fmt);
1934 return err < 0 ? err : formats;
1937 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1942 if (format != AFMT_QUERY) {
1943 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1946 if (!(formats & format))
1948 for (idx = 1; idx >= 0; --idx) {
1949 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1950 struct snd_pcm_runtime *runtime;
1951 if (substream == NULL)
1953 runtime = substream->runtime;
1954 err = lock_params(runtime);
1957 if (runtime->oss.format != format) {
1958 runtime->oss.params = 1;
1959 runtime->oss.format = format;
1961 unlock_params(runtime);
1964 return snd_pcm_oss_get_format(pcm_oss_file);
1967 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1969 struct snd_pcm_substream *substream;
1972 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1974 return substream->runtime->oss.format;
1977 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1979 struct snd_pcm_runtime *runtime;
1981 runtime = substream->runtime;
1982 if (subdivide == 0) {
1983 subdivide = runtime->oss.subdivision;
1988 if (runtime->oss.subdivision || runtime->oss.fragshift)
1990 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1991 subdivide != 8 && subdivide != 16)
1993 runtime->oss.subdivision = subdivide;
1994 runtime->oss.params = 1;
1998 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
2000 int err = -EINVAL, idx;
2002 for (idx = 1; idx >= 0; --idx) {
2003 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2004 struct snd_pcm_runtime *runtime;
2006 if (substream == NULL)
2008 runtime = substream->runtime;
2009 err = lock_params(runtime);
2012 err = snd_pcm_oss_set_subdivide1(substream, subdivide);
2013 unlock_params(runtime);
2020 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
2022 struct snd_pcm_runtime *runtime;
2025 runtime = substream->runtime;
2026 if (runtime->oss.subdivision || runtime->oss.fragshift)
2028 fragshift = val & 0xffff;
2029 if (fragshift >= 25) /* should be large enough */
2031 runtime->oss.fragshift = fragshift;
2032 runtime->oss.maxfrags = (val >> 16) & 0xffff;
2033 if (runtime->oss.fragshift < 4) /* < 16 */
2034 runtime->oss.fragshift = 4;
2035 if (runtime->oss.maxfrags < 2)
2036 runtime->oss.maxfrags = 2;
2037 runtime->oss.params = 1;
2041 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
2043 int err = -EINVAL, idx;
2045 for (idx = 1; idx >= 0; --idx) {
2046 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2047 struct snd_pcm_runtime *runtime;
2049 if (substream == NULL)
2051 runtime = substream->runtime;
2052 err = lock_params(runtime);
2055 err = snd_pcm_oss_set_fragment1(substream, val);
2056 unlock_params(runtime);
2063 static int snd_pcm_oss_nonblock(struct file * file)
2065 spin_lock(&file->f_lock);
2066 file->f_flags |= O_NONBLOCK;
2067 spin_unlock(&file->f_lock);
2071 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2074 if (substream == NULL) {
2075 res &= ~DSP_CAP_DUPLEX;
2078 #ifdef DSP_CAP_MULTI
2079 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2080 if (substream->pstr->substream_count > 1)
2081 res |= DSP_CAP_MULTI;
2083 /* DSP_CAP_REALTIME is set all times: */
2084 /* all ALSA drivers can return actual pointer in ring buffer */
2085 #if defined(DSP_CAP_REALTIME) && 0
2087 struct snd_pcm_runtime *runtime = substream->runtime;
2088 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2089 res &= ~DSP_CAP_REALTIME;
2095 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2099 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2100 for (idx = 0; idx < 2; idx++) {
2101 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2102 result = snd_pcm_oss_get_caps1(substream, result);
2104 result |= 0x0001; /* revision - same as SB AWE 64 */
2108 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2109 snd_pcm_uframes_t hw_ptr)
2111 struct snd_pcm_runtime *runtime = substream->runtime;
2112 snd_pcm_uframes_t appl_ptr;
2113 appl_ptr = hw_ptr + runtime->buffer_size;
2114 appl_ptr %= runtime->boundary;
2115 runtime->control->appl_ptr = appl_ptr;
2118 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2120 struct snd_pcm_runtime *runtime;
2121 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2125 pr_debug("pcm_oss: trigger = 0x%x\n", trigger);
2128 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2129 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2132 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
2136 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
2140 runtime = psubstream->runtime;
2142 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2143 return -ERESTARTSYS;
2144 if (trigger & PCM_ENABLE_OUTPUT) {
2145 if (runtime->oss.trigger)
2147 if (atomic_read(&psubstream->mmap_count))
2148 snd_pcm_oss_simulate_fill(psubstream,
2149 get_hw_ptr_period(runtime));
2150 runtime->oss.trigger = 1;
2151 runtime->start_threshold = 1;
2152 cmd = SNDRV_PCM_IOCTL_START;
2154 if (!runtime->oss.trigger)
2156 runtime->oss.trigger = 0;
2157 runtime->start_threshold = runtime->boundary;
2158 cmd = SNDRV_PCM_IOCTL_DROP;
2159 runtime->oss.prepare = 1;
2162 mutex_unlock(&runtime->oss.params_lock);
2164 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2170 runtime = csubstream->runtime;
2172 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2173 return -ERESTARTSYS;
2174 if (trigger & PCM_ENABLE_INPUT) {
2175 if (runtime->oss.trigger)
2177 runtime->oss.trigger = 1;
2178 runtime->start_threshold = 1;
2179 cmd = SNDRV_PCM_IOCTL_START;
2181 if (!runtime->oss.trigger)
2183 runtime->oss.trigger = 0;
2184 runtime->start_threshold = runtime->boundary;
2185 cmd = SNDRV_PCM_IOCTL_DROP;
2186 runtime->oss.prepare = 1;
2189 mutex_unlock(&runtime->oss.params_lock);
2191 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2199 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2201 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2204 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2205 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2206 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2207 result |= PCM_ENABLE_OUTPUT;
2208 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2209 result |= PCM_ENABLE_INPUT;
2213 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2215 struct snd_pcm_substream *substream;
2216 struct snd_pcm_runtime *runtime;
2217 snd_pcm_sframes_t delay;
2220 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2221 if (substream == NULL)
2223 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2225 runtime = substream->runtime;
2226 if (runtime->oss.params || runtime->oss.prepare)
2228 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2230 delay = 0; /* hack for broken OSS applications */
2233 return snd_pcm_oss_bytes(substream, delay);
2236 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2238 struct snd_pcm_substream *substream;
2239 struct snd_pcm_runtime *runtime;
2240 snd_pcm_sframes_t delay;
2242 struct count_info info;
2247 substream = pcm_oss_file->streams[stream];
2248 if (substream == NULL)
2250 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2252 runtime = substream->runtime;
2253 if (runtime->oss.params || runtime->oss.prepare) {
2254 memset(&info, 0, sizeof(info));
2255 if (copy_to_user(_info, &info, sizeof(info)))
2259 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2260 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2261 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2266 fixup = runtime->oss.buffer_used;
2269 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2270 fixup = -runtime->oss.buffer_used;
2274 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2275 if (atomic_read(&substream->mmap_count)) {
2276 snd_pcm_sframes_t n;
2277 delay = get_hw_ptr_period(runtime);
2278 n = delay - runtime->oss.prev_hw_ptr_period;
2280 n += runtime->boundary;
2281 info.blocks = n / runtime->period_size;
2282 runtime->oss.prev_hw_ptr_period = delay;
2283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2284 snd_pcm_oss_simulate_fill(substream, delay);
2285 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2287 delay = snd_pcm_oss_bytes(substream, delay);
2288 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2289 if (substream->oss.setup.buggyptr)
2290 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2292 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2293 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2296 info.blocks = delay / runtime->oss.period_bytes;
2297 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2300 if (copy_to_user(_info, &info, sizeof(info)))
2305 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2307 struct snd_pcm_substream *substream;
2308 struct snd_pcm_runtime *runtime;
2309 snd_pcm_sframes_t avail;
2311 struct audio_buf_info info;
2316 substream = pcm_oss_file->streams[stream];
2317 if (substream == NULL)
2319 runtime = substream->runtime;
2321 if (runtime->oss.params &&
2322 (err = snd_pcm_oss_change_params(substream, false)) < 0)
2325 info.fragsize = runtime->oss.period_bytes;
2326 info.fragstotal = runtime->periods;
2327 if (runtime->oss.prepare) {
2328 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2329 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2330 info.fragments = runtime->oss.periods;
2336 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2337 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2338 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2339 avail = runtime->buffer_size;
2343 avail = runtime->buffer_size - avail;
2344 fixup = -runtime->oss.buffer_used;
2347 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2348 fixup = runtime->oss.buffer_used;
2352 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2353 info.fragments = info.bytes / runtime->oss.period_bytes;
2357 pcm_dbg(substream->pcm,
2358 "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2359 info.bytes, info.fragments, info.fragstotal, info.fragsize);
2361 if (copy_to_user(_info, &info, sizeof(info)))
2366 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2368 // it won't be probably implemented
2369 // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2373 static const char *strip_task_path(const char *path)
2375 const char *ptr, *ptrl = NULL;
2376 for (ptr = path; *ptr; ptr++) {
2383 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2384 const char *task_name,
2385 struct snd_pcm_oss_setup *rsetup)
2387 struct snd_pcm_oss_setup *setup;
2389 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2391 for (setup = pcm->streams[stream].oss.setup_list; setup;
2392 setup = setup->next) {
2393 if (!strcmp(setup->task_name, task_name))
2396 } while ((task_name = strip_task_path(task_name)) != NULL);
2400 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2403 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2405 struct snd_pcm_runtime *runtime;
2406 runtime = substream->runtime;
2407 vfree(runtime->oss.buffer);
2408 runtime->oss.buffer = NULL;
2409 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2410 snd_pcm_oss_plugin_clear(substream);
2412 substream->oss.oss = 0;
2415 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2416 struct snd_pcm_oss_setup *setup,
2419 struct snd_pcm_runtime *runtime;
2421 substream->oss.oss = 1;
2422 substream->oss.setup = *setup;
2423 if (setup->nonblock)
2424 substream->f_flags |= O_NONBLOCK;
2425 else if (setup->block)
2426 substream->f_flags &= ~O_NONBLOCK;
2427 runtime = substream->runtime;
2428 runtime->oss.params = 1;
2429 runtime->oss.trigger = 1;
2430 runtime->oss.rate = 8000;
2431 mutex_init(&runtime->oss.params_lock);
2432 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2433 case SNDRV_MINOR_OSS_PCM_8:
2434 runtime->oss.format = AFMT_U8;
2436 case SNDRV_MINOR_OSS_PCM_16:
2437 runtime->oss.format = AFMT_S16_LE;
2440 runtime->oss.format = AFMT_MU_LAW;
2442 runtime->oss.channels = 1;
2443 runtime->oss.fragshift = 0;
2444 runtime->oss.maxfrags = 0;
2445 runtime->oss.subdivision = 0;
2446 substream->pcm_release = snd_pcm_oss_release_substream;
2447 atomic_set(&runtime->oss.rw_ref, 0);
2450 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2455 for (cidx = 0; cidx < 2; ++cidx) {
2456 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2458 snd_pcm_release_substream(substream);
2460 kfree(pcm_oss_file);
2464 static int snd_pcm_oss_open_file(struct file *file,
2465 struct snd_pcm *pcm,
2466 struct snd_pcm_oss_file **rpcm_oss_file,
2468 struct snd_pcm_oss_setup *setup)
2471 struct snd_pcm_oss_file *pcm_oss_file;
2472 struct snd_pcm_substream *substream;
2473 fmode_t f_mode = file->f_mode;
2476 *rpcm_oss_file = NULL;
2478 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2479 if (pcm_oss_file == NULL)
2482 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2483 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2484 f_mode = FMODE_WRITE;
2486 file->f_flags &= ~O_APPEND;
2487 for (idx = 0; idx < 2; idx++) {
2488 if (setup[idx].disable)
2490 if (! pcm->streams[idx].substream_count)
2491 continue; /* no matching substream */
2492 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2493 if (! (f_mode & FMODE_WRITE))
2496 if (! (f_mode & FMODE_READ))
2499 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2501 snd_pcm_oss_release_file(pcm_oss_file);
2505 pcm_oss_file->streams[idx] = substream;
2506 substream->file = pcm_oss_file;
2507 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2510 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2511 snd_pcm_oss_release_file(pcm_oss_file);
2515 file->private_data = pcm_oss_file;
2517 *rpcm_oss_file = pcm_oss_file;
2522 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2526 if (snd_BUG_ON(!task || !name || size < 2))
2528 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2529 name[idx] = task->comm[idx];
2534 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2538 struct snd_pcm *pcm;
2539 struct snd_pcm_oss_file *pcm_oss_file;
2540 struct snd_pcm_oss_setup setup[2];
2544 err = nonseekable_open(inode, file);
2548 pcm = snd_lookup_oss_minor_data(iminor(inode),
2549 SNDRV_OSS_DEVICE_TYPE_PCM);
2554 err = snd_card_file_add(pcm->card, file);
2557 if (!try_module_get(pcm->card->module)) {
2561 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2565 memset(setup, 0, sizeof(setup));
2566 if (file->f_mode & FMODE_WRITE)
2567 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2568 task_name, &setup[0]);
2569 if (file->f_mode & FMODE_READ)
2570 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2571 task_name, &setup[1]);
2573 nonblock = !!(file->f_flags & O_NONBLOCK);
2575 nonblock = nonblock_open;
2577 init_waitqueue_entry(&wait, current);
2578 add_wait_queue(&pcm->open_wait, &wait);
2579 mutex_lock(&pcm->open_mutex);
2581 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2582 iminor(inode), setup);
2585 if (err == -EAGAIN) {
2592 set_current_state(TASK_INTERRUPTIBLE);
2593 mutex_unlock(&pcm->open_mutex);
2595 mutex_lock(&pcm->open_mutex);
2596 if (pcm->card->shutdown) {
2600 if (signal_pending(current)) {
2605 remove_wait_queue(&pcm->open_wait, &wait);
2606 mutex_unlock(&pcm->open_mutex);
2609 snd_card_unref(pcm->card);
2613 module_put(pcm->card->module);
2615 snd_card_file_remove(pcm->card, file);
2618 snd_card_unref(pcm->card);
2622 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2624 struct snd_pcm *pcm;
2625 struct snd_pcm_substream *substream;
2626 struct snd_pcm_oss_file *pcm_oss_file;
2628 pcm_oss_file = file->private_data;
2629 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2630 if (substream == NULL)
2631 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2632 if (snd_BUG_ON(!substream))
2634 pcm = substream->pcm;
2635 if (!pcm->card->shutdown)
2636 snd_pcm_oss_sync(pcm_oss_file);
2637 mutex_lock(&pcm->open_mutex);
2638 snd_pcm_oss_release_file(pcm_oss_file);
2639 mutex_unlock(&pcm->open_mutex);
2640 wake_up(&pcm->open_wait);
2641 module_put(pcm->card->module);
2642 snd_card_file_remove(pcm->card, file);
2646 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2648 struct snd_pcm_oss_file *pcm_oss_file;
2649 int __user *p = (int __user *)arg;
2652 pcm_oss_file = file->private_data;
2653 if (cmd == OSS_GETVERSION)
2654 return put_user(SNDRV_OSS_VERSION, p);
2655 if (cmd == OSS_ALSAEMULVER)
2656 return put_user(1, p);
2657 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2658 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2659 struct snd_pcm_substream *substream;
2661 for (idx = 0; idx < 2; ++idx) {
2662 substream = pcm_oss_file->streams[idx];
2663 if (substream != NULL)
2666 if (snd_BUG_ON(idx >= 2))
2668 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2671 if (((cmd >> 8) & 0xff) != 'P')
2674 pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2677 case SNDCTL_DSP_RESET:
2678 return snd_pcm_oss_reset(pcm_oss_file);
2679 case SNDCTL_DSP_SYNC:
2680 return snd_pcm_oss_sync(pcm_oss_file);
2681 case SNDCTL_DSP_SPEED:
2682 if (get_user(res, p))
2684 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2686 return put_user(res, p);
2687 case SOUND_PCM_READ_RATE:
2688 res = snd_pcm_oss_get_rate(pcm_oss_file);
2691 return put_user(res, p);
2692 case SNDCTL_DSP_STEREO:
2693 if (get_user(res, p))
2695 res = res > 0 ? 2 : 1;
2696 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2698 return put_user(--res, p);
2699 case SNDCTL_DSP_GETBLKSIZE:
2700 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2703 return put_user(res, p);
2704 case SNDCTL_DSP_SETFMT:
2705 if (get_user(res, p))
2707 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2710 return put_user(res, p);
2711 case SOUND_PCM_READ_BITS:
2712 res = snd_pcm_oss_get_format(pcm_oss_file);
2715 return put_user(res, p);
2716 case SNDCTL_DSP_CHANNELS:
2717 if (get_user(res, p))
2719 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2722 return put_user(res, p);
2723 case SOUND_PCM_READ_CHANNELS:
2724 res = snd_pcm_oss_get_channels(pcm_oss_file);
2727 return put_user(res, p);
2728 case SOUND_PCM_WRITE_FILTER:
2729 case SOUND_PCM_READ_FILTER:
2731 case SNDCTL_DSP_POST:
2732 return snd_pcm_oss_post(pcm_oss_file);
2733 case SNDCTL_DSP_SUBDIVIDE:
2734 if (get_user(res, p))
2736 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2739 return put_user(res, p);
2740 case SNDCTL_DSP_SETFRAGMENT:
2741 if (get_user(res, p))
2743 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2744 case SNDCTL_DSP_GETFMTS:
2745 res = snd_pcm_oss_get_formats(pcm_oss_file);
2748 return put_user(res, p);
2749 case SNDCTL_DSP_GETOSPACE:
2750 case SNDCTL_DSP_GETISPACE:
2751 return snd_pcm_oss_get_space(pcm_oss_file,
2752 cmd == SNDCTL_DSP_GETISPACE ?
2753 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2754 (struct audio_buf_info __user *) arg);
2755 case SNDCTL_DSP_NONBLOCK:
2756 return snd_pcm_oss_nonblock(file);
2757 case SNDCTL_DSP_GETCAPS:
2758 res = snd_pcm_oss_get_caps(pcm_oss_file);
2761 return put_user(res, p);
2762 case SNDCTL_DSP_GETTRIGGER:
2763 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2766 return put_user(res, p);
2767 case SNDCTL_DSP_SETTRIGGER:
2768 if (get_user(res, p))
2770 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2771 case SNDCTL_DSP_GETIPTR:
2772 case SNDCTL_DSP_GETOPTR:
2773 return snd_pcm_oss_get_ptr(pcm_oss_file,
2774 cmd == SNDCTL_DSP_GETIPTR ?
2775 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2776 (struct count_info __user *) arg);
2777 case SNDCTL_DSP_MAPINBUF:
2778 case SNDCTL_DSP_MAPOUTBUF:
2779 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2780 cmd == SNDCTL_DSP_MAPINBUF ?
2781 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2782 (struct buffmem_desc __user *) arg);
2783 case SNDCTL_DSP_SETSYNCRO:
2784 /* stop DMA now.. */
2786 case SNDCTL_DSP_SETDUPLEX:
2787 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2790 case SNDCTL_DSP_GETODELAY:
2791 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2793 /* it's for sure, some broken apps don't check for error codes */
2797 return put_user(res, p);
2798 case SNDCTL_DSP_PROFILE:
2799 return 0; /* silently ignore */
2801 pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2806 #ifdef CONFIG_COMPAT
2807 /* all compatible */
2808 static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
2811 return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2814 #define snd_pcm_oss_ioctl_compat NULL
2817 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2819 struct snd_pcm_oss_file *pcm_oss_file;
2820 struct snd_pcm_substream *substream;
2822 pcm_oss_file = file->private_data;
2823 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2824 if (substream == NULL)
2826 substream->f_flags = file->f_flags & O_NONBLOCK;
2828 return snd_pcm_oss_read1(substream, buf, count);
2831 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2832 pcm_dbg(substream->pcm,
2833 "pcm_oss: read %li bytes (returned %li bytes)\n",
2834 (long)count, (long)res);
2840 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2842 struct snd_pcm_oss_file *pcm_oss_file;
2843 struct snd_pcm_substream *substream;
2846 pcm_oss_file = file->private_data;
2847 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2848 if (substream == NULL)
2850 substream->f_flags = file->f_flags & O_NONBLOCK;
2851 result = snd_pcm_oss_write1(substream, buf, count);
2853 pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2854 (long)count, (long)result);
2859 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2861 struct snd_pcm_runtime *runtime = substream->runtime;
2862 if (atomic_read(&substream->mmap_count))
2863 return runtime->oss.prev_hw_ptr_period !=
2864 get_hw_ptr_period(runtime);
2866 return snd_pcm_playback_avail(runtime) >=
2867 runtime->oss.period_frames;
2870 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2872 struct snd_pcm_runtime *runtime = substream->runtime;
2873 if (atomic_read(&substream->mmap_count))
2874 return runtime->oss.prev_hw_ptr_period !=
2875 get_hw_ptr_period(runtime);
2877 return snd_pcm_capture_avail(runtime) >=
2878 runtime->oss.period_frames;
2881 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2883 struct snd_pcm_oss_file *pcm_oss_file;
2885 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2887 pcm_oss_file = file->private_data;
2889 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2890 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2893 if (psubstream != NULL) {
2894 struct snd_pcm_runtime *runtime = psubstream->runtime;
2895 poll_wait(file, &runtime->sleep, wait);
2896 snd_pcm_stream_lock_irq(psubstream);
2897 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2898 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2899 snd_pcm_oss_playback_ready(psubstream)))
2900 mask |= POLLOUT | POLLWRNORM;
2901 snd_pcm_stream_unlock_irq(psubstream);
2903 if (csubstream != NULL) {
2904 struct snd_pcm_runtime *runtime = csubstream->runtime;
2905 snd_pcm_state_t ostate;
2906 poll_wait(file, &runtime->sleep, wait);
2907 snd_pcm_stream_lock_irq(csubstream);
2908 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2909 snd_pcm_oss_capture_ready(csubstream))
2910 mask |= POLLIN | POLLRDNORM;
2911 snd_pcm_stream_unlock_irq(csubstream);
2912 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2913 struct snd_pcm_oss_file ofile;
2914 memset(&ofile, 0, sizeof(ofile));
2915 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2916 runtime->oss.trigger = 0;
2917 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2924 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2926 struct snd_pcm_oss_file *pcm_oss_file;
2927 struct snd_pcm_substream *substream = NULL;
2928 struct snd_pcm_runtime *runtime;
2932 pr_debug("pcm_oss: mmap begin\n");
2934 pcm_oss_file = file->private_data;
2935 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2936 case VM_READ | VM_WRITE:
2937 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2942 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2945 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2950 /* set VM_READ access as well to fix memset() routines that do
2951 reads before writes (to improve performance) */
2952 area->vm_flags |= VM_READ;
2953 if (substream == NULL)
2955 runtime = substream->runtime;
2956 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2958 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2959 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2963 if (runtime->oss.params) {
2964 /* use mutex_trylock() for params_lock for avoiding a deadlock
2965 * between mmap_sem and params_lock taken by
2966 * copy_from/to_user() in snd_pcm_oss_write/read()
2968 err = snd_pcm_oss_change_params(substream, true);
2972 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2973 if (runtime->oss.plugin_first != NULL)
2977 if (area->vm_pgoff != 0)
2980 err = snd_pcm_mmap_data(substream, file, area);
2983 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2984 runtime->silence_threshold = 0;
2985 runtime->silence_size = 0;
2987 pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2988 runtime->oss.mmap_bytes);
2990 /* In mmap mode we never stop */
2991 runtime->stop_threshold = runtime->boundary;
2996 #ifdef CONFIG_SND_VERBOSE_PROCFS
3001 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
3002 struct snd_info_buffer *buffer)
3004 struct snd_pcm_str *pstr = entry->private_data;
3005 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
3006 mutex_lock(&pstr->oss.setup_mutex);
3008 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
3012 setup->disable ? " disable" : "",
3013 setup->direct ? " direct" : "",
3014 setup->block ? " block" : "",
3015 setup->nonblock ? " non-block" : "",
3016 setup->partialfrag ? " partial-frag" : "",
3017 setup->nosilence ? " no-silence" : "");
3018 setup = setup->next;
3020 mutex_unlock(&pstr->oss.setup_mutex);
3023 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
3025 struct snd_pcm_oss_setup *setup, *setupn;
3027 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
3028 setup; setup = setupn) {
3029 setupn = setup->next;
3030 kfree(setup->task_name);
3033 pstr->oss.setup_list = NULL;
3036 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
3037 struct snd_info_buffer *buffer)
3039 struct snd_pcm_str *pstr = entry->private_data;
3040 char line[128], str[32], task_name[32];
3043 struct snd_pcm_oss_setup *setup, *setup1, template;
3045 while (!snd_info_get_line(buffer, line, sizeof(line))) {
3046 mutex_lock(&pstr->oss.setup_mutex);
3047 memset(&template, 0, sizeof(template));
3048 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
3049 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
3050 snd_pcm_oss_proc_free_setup_list(pstr);
3051 mutex_unlock(&pstr->oss.setup_mutex);
3054 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
3055 if (!strcmp(setup->task_name, task_name)) {
3060 ptr = snd_info_get_str(str, ptr, sizeof(str));
3061 template.periods = simple_strtoul(str, NULL, 10);
3062 ptr = snd_info_get_str(str, ptr, sizeof(str));
3063 template.period_size = simple_strtoul(str, NULL, 10);
3064 for (idx1 = 31; idx1 >= 0; idx1--)
3065 if (template.period_size & (1 << idx1))
3067 for (idx1--; idx1 >= 0; idx1--)
3068 template.period_size &= ~(1 << idx1);
3070 ptr = snd_info_get_str(str, ptr, sizeof(str));
3071 if (!strcmp(str, "disable")) {
3072 template.disable = 1;
3073 } else if (!strcmp(str, "direct")) {
3074 template.direct = 1;
3075 } else if (!strcmp(str, "block")) {
3077 } else if (!strcmp(str, "non-block")) {
3078 template.nonblock = 1;
3079 } else if (!strcmp(str, "partial-frag")) {
3080 template.partialfrag = 1;
3081 } else if (!strcmp(str, "no-silence")) {
3082 template.nosilence = 1;
3083 } else if (!strcmp(str, "buggy-ptr")) {
3084 template.buggyptr = 1;
3087 if (setup == NULL) {
3088 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
3090 buffer->error = -ENOMEM;
3091 mutex_unlock(&pstr->oss.setup_mutex);
3094 if (pstr->oss.setup_list == NULL)
3095 pstr->oss.setup_list = setup;
3097 for (setup1 = pstr->oss.setup_list;
3098 setup1->next; setup1 = setup1->next);
3099 setup1->next = setup;
3101 template.task_name = kstrdup(task_name, GFP_KERNEL);
3102 if (! template.task_name) {
3104 buffer->error = -ENOMEM;
3105 mutex_unlock(&pstr->oss.setup_mutex);
3110 mutex_unlock(&pstr->oss.setup_mutex);
3114 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3117 for (stream = 0; stream < 2; ++stream) {
3118 struct snd_info_entry *entry;
3119 struct snd_pcm_str *pstr = &pcm->streams[stream];
3120 if (pstr->substream_count == 0)
3122 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
3123 entry->content = SNDRV_INFO_CONTENT_TEXT;
3124 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
3125 entry->c.text.read = snd_pcm_oss_proc_read;
3126 entry->c.text.write = snd_pcm_oss_proc_write;
3127 entry->private_data = pstr;
3128 if (snd_info_register(entry) < 0) {
3129 snd_info_free_entry(entry);
3133 pstr->oss.proc_entry = entry;
3137 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3140 for (stream = 0; stream < 2; ++stream) {
3141 struct snd_pcm_str *pstr = &pcm->streams[stream];
3142 snd_info_free_entry(pstr->oss.proc_entry);
3143 pstr->oss.proc_entry = NULL;
3144 snd_pcm_oss_proc_free_setup_list(pstr);
3147 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3148 #define snd_pcm_oss_proc_init(pcm)
3149 #define snd_pcm_oss_proc_done(pcm)
3150 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3156 static const struct file_operations snd_pcm_oss_f_reg =
3158 .owner = THIS_MODULE,
3159 .read = snd_pcm_oss_read,
3160 .write = snd_pcm_oss_write,
3161 .open = snd_pcm_oss_open,
3162 .release = snd_pcm_oss_release,
3163 .llseek = no_llseek,
3164 .poll = snd_pcm_oss_poll,
3165 .unlocked_ioctl = snd_pcm_oss_ioctl,
3166 .compat_ioctl = snd_pcm_oss_ioctl_compat,
3167 .mmap = snd_pcm_oss_mmap,
3170 static void register_oss_dsp(struct snd_pcm *pcm, int index)
3172 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3173 pcm->card, index, &snd_pcm_oss_f_reg,
3175 pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3176 pcm->card->number, pcm->device);
3180 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3183 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3186 register_oss_dsp(pcm, 0);
3187 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3188 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3189 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3190 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3191 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3192 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3197 pcm->oss.reg_mask |= 1;
3199 if (adsp_map[pcm->card->number] == (int)pcm->device) {
3200 register_oss_dsp(pcm, 1);
3202 pcm->oss.reg_mask |= 2;
3206 snd_pcm_oss_proc_init(pcm);
3211 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3214 if (pcm->oss.reg_mask & 1) {
3215 pcm->oss.reg_mask &= ~1;
3216 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3219 if (pcm->oss.reg_mask & 2) {
3220 pcm->oss.reg_mask &= ~2;
3221 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3224 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3225 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3226 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3234 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3236 snd_pcm_oss_disconnect_minor(pcm);
3237 snd_pcm_oss_proc_done(pcm);
3241 static struct snd_pcm_notify snd_pcm_oss_notify =
3243 .n_register = snd_pcm_oss_register_minor,
3244 .n_disconnect = snd_pcm_oss_disconnect_minor,
3245 .n_unregister = snd_pcm_oss_unregister_minor,
3248 static int __init alsa_pcm_oss_init(void)
3253 /* check device map table */
3254 for (i = 0; i < SNDRV_CARDS; i++) {
3255 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3256 pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3260 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3261 pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3266 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3271 static void __exit alsa_pcm_oss_exit(void)
3273 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3276 module_init(alsa_pcm_oss_init)
3277 module_exit(alsa_pcm_oss_exit)