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 <sound/core.h>
37 #include <sound/minors.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include "pcm_plugin.h"
41 #include <sound/info.h>
42 #include <linux/soundcard.h>
43 #include <sound/initval.h>
44 #include <sound/mixer_oss.h>
46 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
48 static int dsp_map[SNDRV_CARDS];
49 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50 static bool nonblock_open = 1;
52 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54 MODULE_LICENSE("GPL");
55 module_param_array(dsp_map, int, NULL, 0444);
56 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map, int, NULL, 0444);
58 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59 module_param(nonblock_open, bool, 0644);
60 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
64 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
65 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
66 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
68 static inline mm_segment_t snd_enter_user(void)
70 mm_segment_t fs = get_fs();
75 static inline void snd_leave_user(mm_segment_t fs)
81 * helper functions to process hw_params
83 static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
90 } else if (i->min == min && !i->openmin && openmin) {
100 if (snd_interval_checkempty(i)) {
101 snd_interval_none(i);
107 static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
112 i->openmax = openmax;
114 } else if (i->max == max && !i->openmax && openmax) {
124 if (snd_interval_checkempty(i)) {
125 snd_interval_none(i);
131 static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
133 struct snd_interval t;
136 t.openmin = t.openmax = 0;
138 return snd_interval_refine(i, &t);
142 * snd_pcm_hw_param_value_min
143 * @params: the hw_params instance
144 * @var: parameter to retrieve
145 * @dir: pointer to the direction (-1,0,1) or NULL
147 * Return the minimum value for field PAR.
150 snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
151 snd_pcm_hw_param_t var, int *dir)
153 if (hw_is_mask(var)) {
156 return snd_mask_min(hw_param_mask_c(params, var));
158 if (hw_is_interval(var)) {
159 const struct snd_interval *i = hw_param_interval_c(params, var);
162 return snd_interval_min(i);
168 * snd_pcm_hw_param_value_max
169 * @params: the hw_params instance
170 * @var: parameter to retrieve
171 * @dir: pointer to the direction (-1,0,1) or NULL
173 * Return the maximum value for field PAR.
176 snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
177 snd_pcm_hw_param_t var, int *dir)
179 if (hw_is_mask(var)) {
182 return snd_mask_max(hw_param_mask_c(params, var));
184 if (hw_is_interval(var)) {
185 const struct snd_interval *i = hw_param_interval_c(params, var);
187 *dir = - (int) i->openmax;
188 return snd_interval_max(i);
193 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
194 snd_pcm_hw_param_t var,
195 const struct snd_mask *val)
198 changed = snd_mask_refine(hw_param_mask(params, var), val);
200 params->cmask |= 1 << var;
201 params->rmask |= 1 << var;
206 static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
207 struct snd_pcm_hw_params *params,
208 snd_pcm_hw_param_t var,
209 const struct snd_mask *val)
211 int changed = _snd_pcm_hw_param_mask(params, var, val);
215 int err = snd_pcm_hw_refine(pcm, params);
222 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
223 snd_pcm_hw_param_t var, unsigned int val,
231 } else if (dir < 0) {
239 changed = snd_mask_refine_min(hw_param_mask(params, var),
241 else if (hw_is_interval(var))
242 changed = snd_interval_refine_min(hw_param_interval(params, var),
247 params->cmask |= 1 << var;
248 params->rmask |= 1 << var;
254 * snd_pcm_hw_param_min
256 * @params: the hw_params instance
257 * @var: parameter to retrieve
258 * @val: minimal value
259 * @dir: pointer to the direction (-1,0,1) or NULL
261 * Inside configuration space defined by PARAMS remove from PAR all
262 * values < VAL. Reduce configuration space accordingly.
263 * Return new minimum or -EINVAL if the configuration space is empty
265 static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
266 struct snd_pcm_hw_params *params,
267 snd_pcm_hw_param_t var, unsigned int val,
270 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
274 int err = snd_pcm_hw_refine(pcm, params);
278 return snd_pcm_hw_param_value_min(params, var, dir);
281 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
282 snd_pcm_hw_param_t var, unsigned int val,
290 } else if (dir > 0) {
295 if (hw_is_mask(var)) {
296 if (val == 0 && open) {
297 snd_mask_none(hw_param_mask(params, var));
300 changed = snd_mask_refine_max(hw_param_mask(params, var),
302 } else if (hw_is_interval(var))
303 changed = snd_interval_refine_max(hw_param_interval(params, var),
308 params->cmask |= 1 << var;
309 params->rmask |= 1 << var;
315 * snd_pcm_hw_param_max
317 * @params: the hw_params instance
318 * @var: parameter to retrieve
319 * @val: maximal value
320 * @dir: pointer to the direction (-1,0,1) or NULL
322 * Inside configuration space defined by PARAMS remove from PAR all
323 * values >= VAL + 1. Reduce configuration space accordingly.
324 * Return new maximum or -EINVAL if the configuration space is empty
326 static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
327 struct snd_pcm_hw_params *params,
328 snd_pcm_hw_param_t var, unsigned int val,
331 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
335 int err = snd_pcm_hw_refine(pcm, params);
339 return snd_pcm_hw_param_value_max(params, var, dir);
342 static int boundary_sub(int a, int adir,
346 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
347 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
352 } else if (*cdir == 2) {
358 static int boundary_lt(unsigned int a, int adir,
359 unsigned int b, int bdir)
371 return a < b || (a == b && adir < bdir);
374 /* Return 1 if min is nearer to best than max */
375 static int boundary_nearer(int min, int mindir,
376 int best, int bestdir,
381 boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
382 boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
383 return boundary_lt(dmin, dmindir, dmax, dmaxdir);
387 * snd_pcm_hw_param_near
389 * @params: the hw_params instance
390 * @var: parameter to retrieve
391 * @best: value to set
392 * @dir: pointer to the direction (-1,0,1) or NULL
394 * Inside configuration space defined by PARAMS set PAR to the available value
395 * nearest to VAL. Reduce configuration space accordingly.
396 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
397 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
398 * Return the value found.
400 static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
401 struct snd_pcm_hw_params *params,
402 snd_pcm_hw_param_t var, unsigned int best,
405 struct snd_pcm_hw_params *save = NULL;
407 unsigned int saved_min;
411 int valdir = dir ? *dir : 0;
416 mindir = maxdir = valdir;
419 else if (maxdir == 0)
425 save = kmalloc(sizeof(*save), GFP_KERNEL);
430 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
432 struct snd_pcm_hw_params *params1;
435 if ((unsigned int)min == saved_min && mindir == valdir)
437 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
438 if (params1 == NULL) {
443 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
448 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
455 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
465 v = snd_pcm_hw_param_last(pcm, params, var, dir);
467 v = snd_pcm_hw_param_first(pcm, params, var, dir);
471 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
472 snd_pcm_hw_param_t var, unsigned int val,
476 if (hw_is_mask(var)) {
477 struct snd_mask *m = hw_param_mask(params, var);
478 if (val == 0 && dir < 0) {
486 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
488 } else if (hw_is_interval(var)) {
489 struct snd_interval *i = hw_param_interval(params, var);
490 if (val == 0 && dir < 0) {
492 snd_interval_none(i);
494 changed = snd_interval_refine_set(i, val);
496 struct snd_interval t;
508 changed = snd_interval_refine(i, &t);
513 params->cmask |= 1 << var;
514 params->rmask |= 1 << var;
520 * snd_pcm_hw_param_set
522 * @params: the hw_params instance
523 * @var: parameter to retrieve
525 * @dir: pointer to the direction (-1,0,1) or NULL
527 * Inside configuration space defined by PARAMS remove from PAR all
528 * values != VAL. Reduce configuration space accordingly.
529 * Return VAL or -EINVAL if the configuration space is empty
531 static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
532 struct snd_pcm_hw_params *params,
533 snd_pcm_hw_param_t var, unsigned int val,
536 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
540 int err = snd_pcm_hw_refine(pcm, params);
544 return snd_pcm_hw_param_value(params, var, NULL);
547 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
548 snd_pcm_hw_param_t var)
551 changed = snd_interval_setinteger(hw_param_interval(params, var));
553 params->cmask |= 1 << var;
554 params->rmask |= 1 << var;
563 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
564 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
566 struct snd_pcm_runtime *runtime = substream->runtime;
567 struct snd_pcm_plugin *plugin, *next;
569 plugin = runtime->oss.plugin_first;
572 snd_pcm_plugin_free(plugin);
575 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
579 static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
581 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
582 plugin->next = runtime->oss.plugin_first;
584 if (runtime->oss.plugin_first) {
585 runtime->oss.plugin_first->prev = plugin;
586 runtime->oss.plugin_first = plugin;
588 runtime->oss.plugin_last =
589 runtime->oss.plugin_first = plugin;
594 int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
596 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
598 plugin->prev = runtime->oss.plugin_last;
599 if (runtime->oss.plugin_last) {
600 runtime->oss.plugin_last->next = plugin;
601 runtime->oss.plugin_last = plugin;
603 runtime->oss.plugin_last =
604 runtime->oss.plugin_first = plugin;
608 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
610 static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
612 struct snd_pcm_runtime *runtime = substream->runtime;
613 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
614 long bytes = frames_to_bytes(runtime, frames);
615 if (buffer_size == runtime->oss.buffer_bytes)
617 #if BITS_PER_LONG >= 64
618 return runtime->oss.buffer_bytes * bytes / buffer_size;
621 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
622 return div_u64(bsize, buffer_size);
627 static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
629 struct snd_pcm_runtime *runtime = substream->runtime;
630 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
631 if (buffer_size == runtime->oss.buffer_bytes)
632 return bytes_to_frames(runtime, bytes);
633 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
637 snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
639 return runtime->hw_ptr_interrupt;
642 /* define extended formats in the recent OSS versions (if any) */
644 #define AFMT_S32_LE 0x00001000
645 #define AFMT_S32_BE 0x00002000
646 #define AFMT_S24_LE 0x00008000
647 #define AFMT_S24_BE 0x00010000
648 #define AFMT_S24_PACKED 0x00040000
650 /* other supported formats */
651 #define AFMT_FLOAT 0x00004000
652 #define AFMT_SPDIF_RAW 0x00020000
654 /* unsupported formats */
655 #define AFMT_AC3 0x00000400
656 #define AFMT_VORBIS 0x00000800
658 static snd_pcm_format_t snd_pcm_oss_format_from(int format)
661 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
662 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
663 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
664 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
665 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
666 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
667 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
668 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
669 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
670 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
671 case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
672 case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
673 case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
674 case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
675 case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
676 case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
677 case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
678 default: return SNDRV_PCM_FORMAT_U8;
682 static int snd_pcm_oss_format_to(snd_pcm_format_t format)
685 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
686 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
687 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
688 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
689 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
690 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
691 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
692 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
693 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
694 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
695 case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
696 case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
697 case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
698 case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
699 case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
700 case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
701 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
702 default: return -EINVAL;
706 static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
707 struct snd_pcm_hw_params *oss_params,
708 struct snd_pcm_hw_params *slave_params)
711 ssize_t oss_buffer_size;
712 ssize_t oss_period_size, oss_periods;
713 ssize_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_hw_param_value_max(slave_params,
721 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
723 if (oss_buffer_size <= 0)
725 oss_buffer_size = snd_pcm_plug_client_size(substream,
726 oss_buffer_size * oss_frame_size);
727 if (oss_buffer_size <= 0)
729 oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
730 if (atomic_read(&substream->mmap_count)) {
731 if (oss_buffer_size > runtime->oss.mmap_bytes)
732 oss_buffer_size = runtime->oss.mmap_bytes;
735 if (substream->oss.setup.period_size > 16)
736 oss_period_size = substream->oss.setup.period_size;
737 else if (runtime->oss.fragshift) {
738 oss_period_size = 1 << runtime->oss.fragshift;
739 if (oss_period_size > oss_buffer_size / 2)
740 oss_period_size = oss_buffer_size / 2;
743 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
745 oss_period_size = oss_buffer_size;
747 oss_period_size /= 2;
748 } while (oss_period_size > bytes_per_sec);
749 if (runtime->oss.subdivision == 0) {
751 if (oss_period_size / sd > 4096)
753 if (oss_period_size / sd < 4096)
756 sd = runtime->oss.subdivision;
757 oss_period_size /= sd;
758 if (oss_period_size < 16)
759 oss_period_size = 16;
762 min_period_size = snd_pcm_plug_client_size(substream,
763 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
764 if (min_period_size > 0) {
765 min_period_size *= oss_frame_size;
766 min_period_size = roundup_pow_of_two(min_period_size);
767 if (oss_period_size < min_period_size)
768 oss_period_size = min_period_size;
771 max_period_size = snd_pcm_plug_client_size(substream,
772 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
773 if (max_period_size > 0) {
774 max_period_size *= oss_frame_size;
775 max_period_size = rounddown_pow_of_two(max_period_size);
776 if (oss_period_size > max_period_size)
777 oss_period_size = max_period_size;
780 oss_periods = oss_buffer_size / oss_period_size;
782 if (substream->oss.setup.periods > 1)
783 oss_periods = substream->oss.setup.periods;
785 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
786 if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
787 s = runtime->oss.maxfrags;
791 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
797 while (oss_period_size * oss_periods > oss_buffer_size)
798 oss_period_size /= 2;
800 if (oss_period_size < 16)
802 runtime->oss.period_bytes = oss_period_size;
803 runtime->oss.period_frames = 1;
804 runtime->oss.periods = oss_periods;
808 static int choose_rate(struct snd_pcm_substream *substream,
809 struct snd_pcm_hw_params *params, unsigned int best_rate)
811 struct snd_interval *it;
812 struct snd_pcm_hw_params *save;
813 unsigned int rate, prev;
815 save = kmalloc(sizeof(*save), GFP_KERNEL);
819 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
821 /* try multiples of the best rate */
824 if (it->max < rate || (it->max == rate && it->openmax))
826 if (it->min < rate || (it->min == rate && !it->openmin)) {
828 ret = snd_pcm_hw_param_set(substream, params,
829 SNDRV_PCM_HW_PARAM_RATE,
831 if (ret == (int)rate) {
843 /* not found, use the nearest rate */
845 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
848 /* parameter locking: returns immediately if tried during streaming */
849 static int lock_params(struct snd_pcm_runtime *runtime)
851 if (mutex_lock_interruptible(&runtime->oss.params_lock))
853 if (atomic_read(&runtime->oss.rw_ref)) {
854 mutex_unlock(&runtime->oss.params_lock);
860 static void unlock_params(struct snd_pcm_runtime *runtime)
862 mutex_unlock(&runtime->oss.params_lock);
865 /* call with params_lock held */
866 static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
868 struct snd_pcm_runtime *runtime = substream->runtime;
869 struct snd_pcm_hw_params *params, *sparams;
870 struct snd_pcm_sw_params *sw_params;
871 ssize_t oss_buffer_size, oss_period_size;
872 size_t oss_frame_size;
875 snd_pcm_format_t format, sformat;
877 struct snd_mask sformat_mask;
878 struct snd_mask mask;
880 if (!runtime->oss.params)
882 sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
883 params = kmalloc(sizeof(*params), GFP_KERNEL);
884 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
885 if (!sw_params || !params || !sparams) {
890 if (atomic_read(&substream->mmap_count))
893 direct = substream->oss.setup.direct;
895 _snd_pcm_hw_params_any(sparams);
896 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
897 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
898 snd_mask_none(&mask);
899 if (atomic_read(&substream->mmap_count))
900 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
902 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
904 snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
906 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
908 pcm_dbg(substream->pcm, "No usable accesses\n");
913 err = choose_rate(substream, sparams, runtime->oss.rate);
916 err = snd_pcm_hw_param_near(substream, sparams,
917 SNDRV_PCM_HW_PARAM_CHANNELS,
918 runtime->oss.channels, NULL);
922 format = snd_pcm_oss_format_from(runtime->oss.format);
924 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
928 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
930 if ((__force int)sformat < 0 ||
931 !snd_mask_test(&sformat_mask, (__force int)sformat)) {
932 for (sformat = (__force snd_pcm_format_t)0;
933 (__force int)sformat <= (__force int)SNDRV_PCM_FORMAT_LAST;
934 sformat = (__force snd_pcm_format_t)((__force int)sformat + 1)) {
935 if (snd_mask_test(&sformat_mask, (__force int)sformat) &&
936 snd_pcm_oss_format_to(sformat) >= 0)
939 if ((__force int)sformat > (__force int)SNDRV_PCM_FORMAT_LAST) {
940 pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
945 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
950 memcpy(params, sparams, sizeof(*params));
952 _snd_pcm_hw_params_any(params);
953 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
954 (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
955 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
956 (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
957 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
958 runtime->oss.channels, 0);
959 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
960 runtime->oss.rate, 0);
961 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
962 params_access(params), params_format(params),
963 params_channels(params), params_rate(params));
965 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
966 params_access(sparams), params_format(sparams),
967 params_channels(sparams), params_rate(sparams));
969 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
970 params_channels(params) / 8;
972 err = snd_pcm_oss_period_size(substream, params, sparams);
976 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
977 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
981 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
982 runtime->oss.periods, NULL);
986 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
988 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
990 pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
994 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
995 snd_pcm_oss_plugin_clear(substream);
997 /* add necessary plugins */
998 snd_pcm_oss_plugin_clear(substream);
999 if ((err = snd_pcm_plug_format_plugins(substream,
1002 pcm_dbg(substream->pcm,
1003 "snd_pcm_plug_format_plugins failed: %i\n", err);
1004 snd_pcm_oss_plugin_clear(substream);
1007 if (runtime->oss.plugin_first) {
1008 struct snd_pcm_plugin *plugin;
1009 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
1010 pcm_dbg(substream->pcm,
1011 "snd_pcm_plugin_build_io failed: %i\n", err);
1012 snd_pcm_oss_plugin_clear(substream);
1015 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1016 err = snd_pcm_plugin_append(plugin);
1018 err = snd_pcm_plugin_insert(plugin);
1021 snd_pcm_oss_plugin_clear(substream);
1028 if (runtime->oss.trigger) {
1029 sw_params->start_threshold = 1;
1031 sw_params->start_threshold = runtime->boundary;
1033 if (atomic_read(&substream->mmap_count) ||
1034 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1035 sw_params->stop_threshold = runtime->boundary;
1037 sw_params->stop_threshold = runtime->buffer_size;
1038 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
1039 sw_params->period_step = 1;
1040 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1041 1 : runtime->period_size;
1042 if (atomic_read(&substream->mmap_count) ||
1043 substream->oss.setup.nosilence) {
1044 sw_params->silence_threshold = 0;
1045 sw_params->silence_size = 0;
1047 snd_pcm_uframes_t frames;
1048 frames = runtime->period_size + 16;
1049 if (frames > runtime->buffer_size)
1050 frames = runtime->buffer_size;
1051 sw_params->silence_threshold = frames;
1052 sw_params->silence_size = frames;
1055 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1056 pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
1060 runtime->oss.periods = params_periods(sparams);
1061 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1062 if (oss_period_size < 0) {
1066 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1067 if (runtime->oss.plugin_first) {
1068 err = snd_pcm_plug_alloc(substream, oss_period_size);
1073 oss_period_size *= oss_frame_size;
1075 oss_buffer_size = oss_period_size * runtime->oss.periods;
1076 if (oss_buffer_size < 0) {
1081 runtime->oss.period_bytes = oss_period_size;
1082 runtime->oss.buffer_bytes = oss_buffer_size;
1084 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1085 runtime->oss.period_bytes,
1086 runtime->oss.buffer_bytes);
1087 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1088 params_period_size(sparams),
1089 params_buffer_size(sparams));
1091 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1092 runtime->oss.channels = params_channels(params);
1093 runtime->oss.rate = params_rate(params);
1095 vfree(runtime->oss.buffer);
1096 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1097 if (!runtime->oss.buffer) {
1102 runtime->oss.params = 0;
1103 runtime->oss.prepare = 1;
1104 runtime->oss.buffer_used = 0;
1105 if (runtime->dma_area)
1106 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1108 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1118 /* this one takes the lock by itself */
1119 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
1122 struct snd_pcm_runtime *runtime = substream->runtime;
1126 if (!(mutex_trylock(&runtime->oss.params_lock)))
1128 } else if (mutex_lock_interruptible(&runtime->oss.params_lock))
1129 return -ERESTARTSYS;
1131 err = snd_pcm_oss_change_params_locked(substream);
1132 mutex_unlock(&runtime->oss.params_lock);
1136 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1139 struct snd_pcm_substream *asubstream = NULL, *substream;
1141 for (idx = 0; idx < 2; idx++) {
1142 substream = pcm_oss_file->streams[idx];
1143 if (substream == NULL)
1145 if (asubstream == NULL)
1146 asubstream = substream;
1147 if (substream->runtime->oss.params) {
1148 err = snd_pcm_oss_change_params(substream, false);
1156 *r_substream = asubstream;
1160 /* call with params_lock held */
1161 /* NOTE: this always call PREPARE unconditionally no matter whether
1162 * runtime->oss.prepare is set or not
1164 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1167 struct snd_pcm_runtime *runtime = substream->runtime;
1169 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1171 pcm_dbg(substream->pcm,
1172 "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1175 runtime->oss.prepare = 0;
1176 runtime->oss.prev_hw_ptr_period = 0;
1177 runtime->oss.period_ptr = 0;
1178 runtime->oss.buffer_used = 0;
1183 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1185 struct snd_pcm_runtime *runtime;
1188 runtime = substream->runtime;
1189 if (runtime->oss.params) {
1190 err = snd_pcm_oss_change_params(substream, false);
1194 if (runtime->oss.prepare) {
1195 if (mutex_lock_interruptible(&runtime->oss.params_lock))
1196 return -ERESTARTSYS;
1197 err = snd_pcm_oss_prepare(substream);
1198 mutex_unlock(&runtime->oss.params_lock);
1205 /* call with params_lock held */
1206 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1208 struct snd_pcm_runtime *runtime;
1211 runtime = substream->runtime;
1212 if (runtime->oss.params) {
1213 err = snd_pcm_oss_change_params_locked(substream);
1217 if (runtime->oss.prepare) {
1218 err = snd_pcm_oss_prepare(substream);
1225 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1227 struct snd_pcm_runtime *runtime;
1228 snd_pcm_uframes_t frames;
1232 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1235 runtime = substream->runtime;
1236 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1238 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1239 /* until avail(delay) <= buffer_size */
1240 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1241 frames /= runtime->period_size;
1242 frames *= runtime->period_size;
1243 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1250 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1252 struct snd_pcm_runtime *runtime = substream->runtime;
1255 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1256 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1258 pcm_dbg(substream->pcm,
1259 "pcm_oss: write: recovering from %s\n",
1260 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1261 "XRUN" : "SUSPEND");
1263 ret = snd_pcm_oss_prepare(substream);
1269 fs = snd_enter_user();
1270 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1273 ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
1275 if (ret != -EPIPE && ret != -ESTRPIPE)
1277 /* test, if we can't store new data, because the stream */
1278 /* has not been started */
1279 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1285 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1287 struct snd_pcm_runtime *runtime = substream->runtime;
1288 snd_pcm_sframes_t delay;
1291 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1292 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1294 pcm_dbg(substream->pcm,
1295 "pcm_oss: read: recovering from %s\n",
1296 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1297 "XRUN" : "SUSPEND");
1299 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1302 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1303 ret = snd_pcm_oss_prepare(substream);
1307 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1312 fs = snd_enter_user();
1313 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1316 ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
1318 if (ret == -EPIPE) {
1319 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1320 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1326 if (ret != -ESTRPIPE)
1332 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1334 struct snd_pcm_runtime *runtime = substream->runtime;
1337 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1338 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1340 pcm_dbg(substream->pcm,
1341 "pcm_oss: writev: recovering from %s\n",
1342 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1343 "XRUN" : "SUSPEND");
1345 ret = snd_pcm_oss_prepare(substream);
1351 fs = snd_enter_user();
1352 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1355 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1357 if (ret != -EPIPE && ret != -ESTRPIPE)
1360 /* test, if we can't store new data, because the stream */
1361 /* has not been started */
1362 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1368 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1370 struct snd_pcm_runtime *runtime = substream->runtime;
1373 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1374 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1376 pcm_dbg(substream->pcm,
1377 "pcm_oss: readv: recovering from %s\n",
1378 runtime->status->state == SNDRV_PCM_STATE_XRUN ?
1379 "XRUN" : "SUSPEND");
1381 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1384 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1385 ret = snd_pcm_oss_prepare(substream);
1391 fs = snd_enter_user();
1392 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1395 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1397 if (ret != -EPIPE && ret != -ESTRPIPE)
1403 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1405 struct snd_pcm_runtime *runtime = substream->runtime;
1406 snd_pcm_sframes_t frames, frames1;
1407 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1408 if (runtime->oss.plugin_first) {
1409 struct snd_pcm_plugin_channel *channels;
1410 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1412 if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1414 buf = runtime->oss.buffer;
1416 frames = bytes / oss_frame_bytes;
1417 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1420 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1423 bytes = frames1 * oss_frame_bytes;
1427 frames = bytes_to_frames(runtime, bytes);
1428 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1431 bytes = frames_to_bytes(runtime, frames1);
1436 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1440 struct snd_pcm_runtime *runtime = substream->runtime;
1442 if (atomic_read(&substream->mmap_count))
1445 atomic_inc(&runtime->oss.rw_ref);
1447 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1451 tmp = snd_pcm_oss_make_ready_locked(substream);
1454 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1456 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1457 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1459 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1464 runtime->oss.buffer_used += tmp;
1468 if (substream->oss.setup.partialfrag ||
1469 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1470 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1471 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1474 runtime->oss.bytes += tmp;
1475 runtime->oss.period_ptr += tmp;
1476 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1477 if (runtime->oss.period_ptr == 0 ||
1478 runtime->oss.period_ptr == runtime->oss.buffer_used)
1479 runtime->oss.buffer_used = 0;
1480 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1486 tmp = snd_pcm_oss_write2(substream,
1487 (const char __force *)buf,
1488 runtime->oss.period_bytes, 0);
1491 runtime->oss.bytes += tmp;
1495 if ((substream->f_flags & O_NONBLOCK) != 0 &&
1496 tmp != runtime->oss.period_bytes)
1500 mutex_unlock(&runtime->oss.params_lock);
1503 if (signal_pending(current)) {
1509 atomic_dec(&runtime->oss.rw_ref);
1510 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1513 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1515 struct snd_pcm_runtime *runtime = substream->runtime;
1516 snd_pcm_sframes_t frames, frames1;
1517 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1518 char __user *final_dst = (char __force __user *)buf;
1519 if (runtime->oss.plugin_first) {
1520 struct snd_pcm_plugin_channel *channels;
1521 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1523 buf = runtime->oss.buffer;
1524 frames = bytes / oss_frame_bytes;
1525 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1528 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1531 bytes = frames1 * oss_frame_bytes;
1532 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1537 frames = bytes_to_frames(runtime, bytes);
1538 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1541 bytes = frames_to_bytes(runtime, frames1);
1546 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1550 struct snd_pcm_runtime *runtime = substream->runtime;
1552 if (atomic_read(&substream->mmap_count))
1555 atomic_inc(&runtime->oss.rw_ref);
1557 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1561 tmp = snd_pcm_oss_make_ready_locked(substream);
1564 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1565 if (runtime->oss.buffer_used == 0) {
1566 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1569 runtime->oss.bytes += tmp;
1570 runtime->oss.period_ptr = tmp;
1571 runtime->oss.buffer_used = tmp;
1574 if ((size_t) tmp > runtime->oss.buffer_used)
1575 tmp = runtime->oss.buffer_used;
1576 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1583 runtime->oss.buffer_used -= tmp;
1585 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1586 runtime->oss.period_bytes, 0);
1589 runtime->oss.bytes += tmp;
1595 mutex_unlock(&runtime->oss.params_lock);
1598 if (signal_pending(current)) {
1604 atomic_dec(&runtime->oss.rw_ref);
1605 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1608 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1610 struct snd_pcm_substream *substream;
1611 struct snd_pcm_runtime *runtime;
1614 for (i = 0; i < 2; i++) {
1615 substream = pcm_oss_file->streams[i];
1618 runtime = substream->runtime;
1619 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1620 mutex_lock(&runtime->oss.params_lock);
1621 runtime->oss.prepare = 1;
1622 runtime->oss.buffer_used = 0;
1623 runtime->oss.prev_hw_ptr_period = 0;
1624 runtime->oss.period_ptr = 0;
1625 mutex_unlock(&runtime->oss.params_lock);
1630 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1632 struct snd_pcm_substream *substream;
1635 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1636 if (substream != NULL) {
1637 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1639 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1641 /* note: all errors from the start action are ignored */
1642 /* OSS apps do not know, how to handle them */
1646 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1648 struct snd_pcm_runtime *runtime;
1650 snd_pcm_state_t state;
1654 runtime = substream->runtime;
1655 init_waitqueue_entry(&wait, current);
1656 add_wait_queue(&runtime->sleep, &wait);
1658 pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1661 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1663 runtime->oss.buffer_used = 0;
1667 if (result != 0 && result != -EAGAIN)
1670 set_current_state(TASK_INTERRUPTIBLE);
1671 snd_pcm_stream_lock_irq(substream);
1672 state = runtime->status->state;
1673 snd_pcm_stream_unlock_irq(substream);
1674 if (state != SNDRV_PCM_STATE_RUNNING) {
1675 set_current_state(TASK_RUNNING);
1678 res = schedule_timeout(10 * HZ);
1679 if (signal_pending(current)) {
1680 result = -ERESTARTSYS;
1684 pcm_err(substream->pcm,
1685 "OSS sync error - DMA timeout\n");
1690 remove_wait_queue(&runtime->sleep, &wait);
1694 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1697 unsigned int saved_f_flags;
1698 struct snd_pcm_substream *substream;
1699 struct snd_pcm_runtime *runtime;
1700 snd_pcm_format_t format;
1701 unsigned long width;
1704 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1705 if (substream != NULL) {
1706 runtime = substream->runtime;
1707 if (atomic_read(&substream->mmap_count))
1709 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1711 atomic_inc(&runtime->oss.rw_ref);
1712 if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1713 atomic_dec(&runtime->oss.rw_ref);
1714 return -ERESTARTSYS;
1716 format = snd_pcm_oss_format_from(runtime->oss.format);
1717 width = snd_pcm_format_physical_width(format);
1718 if (runtime->oss.buffer_used > 0) {
1720 pcm_dbg(substream->pcm, "sync: buffer_used\n");
1722 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1723 snd_pcm_format_set_silence(format,
1724 runtime->oss.buffer + runtime->oss.buffer_used,
1726 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1729 } else if (runtime->oss.period_ptr > 0) {
1731 pcm_dbg(substream->pcm, "sync: period_ptr\n");
1733 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1734 snd_pcm_format_set_silence(format,
1735 runtime->oss.buffer,
1737 err = snd_pcm_oss_sync1(substream, size);
1742 * The ALSA's period might be a bit large than OSS one.
1743 * Fill the remain portion of ALSA period with zeros.
1745 size = runtime->control->appl_ptr % runtime->period_size;
1747 size = runtime->period_size - size;
1748 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1749 size = (runtime->frame_bits * size) / 8;
1752 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1755 size1 /= runtime->sample_bits;
1756 snd_pcm_format_set_silence(runtime->format,
1757 runtime->oss.buffer,
1759 size1 /= runtime->channels; /* frames */
1760 fs = snd_enter_user();
1761 snd_pcm_lib_write(substream, (void __force __user *)runtime->oss.buffer, size1);
1764 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1765 void __user *buffers[runtime->channels];
1766 memset(buffers, 0, runtime->channels * sizeof(void *));
1767 snd_pcm_lib_writev(substream, buffers, size);
1771 mutex_unlock(&runtime->oss.params_lock);
1772 atomic_dec(&runtime->oss.rw_ref);
1776 * finish sync: drain the buffer
1779 saved_f_flags = substream->f_flags;
1780 substream->f_flags &= ~O_NONBLOCK;
1781 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1782 substream->f_flags = saved_f_flags;
1785 mutex_lock(&runtime->oss.params_lock);
1786 runtime->oss.prepare = 1;
1787 mutex_unlock(&runtime->oss.params_lock);
1790 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1791 if (substream != NULL) {
1792 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1794 runtime = substream->runtime;
1795 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1798 mutex_lock(&runtime->oss.params_lock);
1799 runtime->oss.buffer_used = 0;
1800 runtime->oss.prepare = 1;
1801 mutex_unlock(&runtime->oss.params_lock);
1806 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1810 for (idx = 1; idx >= 0; --idx) {
1811 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1812 struct snd_pcm_runtime *runtime;
1815 if (substream == NULL)
1817 runtime = substream->runtime;
1820 else if (rate > 192000)
1822 err = lock_params(runtime);
1825 if (runtime->oss.rate != rate) {
1826 runtime->oss.params = 1;
1827 runtime->oss.rate = rate;
1829 unlock_params(runtime);
1831 return snd_pcm_oss_get_rate(pcm_oss_file);
1834 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1836 struct snd_pcm_substream *substream;
1839 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1841 return substream->runtime->oss.rate;
1844 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1851 for (idx = 1; idx >= 0; --idx) {
1852 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1853 struct snd_pcm_runtime *runtime;
1856 if (substream == NULL)
1858 runtime = substream->runtime;
1859 err = lock_params(runtime);
1862 if (runtime->oss.channels != channels) {
1863 runtime->oss.params = 1;
1864 runtime->oss.channels = channels;
1866 unlock_params(runtime);
1868 return snd_pcm_oss_get_channels(pcm_oss_file);
1871 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1873 struct snd_pcm_substream *substream;
1876 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1878 return substream->runtime->oss.channels;
1881 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1883 struct snd_pcm_substream *substream;
1886 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1888 return substream->runtime->oss.period_bytes;
1891 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1893 struct snd_pcm_substream *substream;
1896 struct snd_pcm_hw_params *params;
1897 unsigned int formats = 0;
1898 struct snd_mask format_mask;
1901 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1903 if (atomic_read(&substream->mmap_count))
1906 direct = substream->oss.setup.direct;
1908 return AFMT_MU_LAW | AFMT_U8 |
1909 AFMT_S16_LE | AFMT_S16_BE |
1910 AFMT_S8 | AFMT_U16_LE |
1912 AFMT_S32_LE | AFMT_S32_BE |
1913 AFMT_S24_LE | AFMT_S24_BE |
1915 params = kmalloc(sizeof(*params), GFP_KERNEL);
1918 _snd_pcm_hw_params_any(params);
1919 err = snd_pcm_hw_refine(substream, params);
1922 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1923 for (fmt = 0; fmt < 32; ++fmt) {
1924 if (snd_mask_test(&format_mask, fmt)) {
1925 int f = snd_pcm_oss_format_to(fmt);
1933 return err < 0 ? err : formats;
1936 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1941 if (format != AFMT_QUERY) {
1942 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1945 if (!(formats & format))
1947 for (idx = 1; idx >= 0; --idx) {
1948 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1949 struct snd_pcm_runtime *runtime;
1950 if (substream == NULL)
1952 runtime = substream->runtime;
1953 err = lock_params(runtime);
1956 if (runtime->oss.format != format) {
1957 runtime->oss.params = 1;
1958 runtime->oss.format = format;
1960 unlock_params(runtime);
1963 return snd_pcm_oss_get_format(pcm_oss_file);
1966 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1968 struct snd_pcm_substream *substream;
1971 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1973 return substream->runtime->oss.format;
1976 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1978 struct snd_pcm_runtime *runtime;
1980 runtime = substream->runtime;
1981 if (subdivide == 0) {
1982 subdivide = runtime->oss.subdivision;
1987 if (runtime->oss.subdivision || runtime->oss.fragshift)
1989 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1990 subdivide != 8 && subdivide != 16)
1992 runtime->oss.subdivision = subdivide;
1993 runtime->oss.params = 1;
1997 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1999 int err = -EINVAL, idx;
2001 for (idx = 1; idx >= 0; --idx) {
2002 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2003 struct snd_pcm_runtime *runtime;
2005 if (substream == NULL)
2007 runtime = substream->runtime;
2008 err = lock_params(runtime);
2011 err = snd_pcm_oss_set_subdivide1(substream, subdivide);
2012 unlock_params(runtime);
2019 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
2021 struct snd_pcm_runtime *runtime;
2024 runtime = substream->runtime;
2025 if (runtime->oss.subdivision || runtime->oss.fragshift)
2027 fragshift = val & 0xffff;
2028 if (fragshift >= 25) /* should be large enough */
2030 runtime->oss.fragshift = fragshift;
2031 runtime->oss.maxfrags = (val >> 16) & 0xffff;
2032 if (runtime->oss.fragshift < 4) /* < 16 */
2033 runtime->oss.fragshift = 4;
2034 if (runtime->oss.maxfrags < 2)
2035 runtime->oss.maxfrags = 2;
2036 runtime->oss.params = 1;
2040 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
2042 int err = -EINVAL, idx;
2044 for (idx = 1; idx >= 0; --idx) {
2045 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2046 struct snd_pcm_runtime *runtime;
2048 if (substream == NULL)
2050 runtime = substream->runtime;
2051 err = lock_params(runtime);
2054 err = snd_pcm_oss_set_fragment1(substream, val);
2055 unlock_params(runtime);
2062 static int snd_pcm_oss_nonblock(struct file * file)
2064 spin_lock(&file->f_lock);
2065 file->f_flags |= O_NONBLOCK;
2066 spin_unlock(&file->f_lock);
2070 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2073 if (substream == NULL) {
2074 res &= ~DSP_CAP_DUPLEX;
2077 #ifdef DSP_CAP_MULTI
2078 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2079 if (substream->pstr->substream_count > 1)
2080 res |= DSP_CAP_MULTI;
2082 /* DSP_CAP_REALTIME is set all times: */
2083 /* all ALSA drivers can return actual pointer in ring buffer */
2084 #if defined(DSP_CAP_REALTIME) && 0
2086 struct snd_pcm_runtime *runtime = substream->runtime;
2087 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2088 res &= ~DSP_CAP_REALTIME;
2094 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2098 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2099 for (idx = 0; idx < 2; idx++) {
2100 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2101 result = snd_pcm_oss_get_caps1(substream, result);
2103 result |= 0x0001; /* revision - same as SB AWE 64 */
2107 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2108 snd_pcm_uframes_t hw_ptr)
2110 struct snd_pcm_runtime *runtime = substream->runtime;
2111 snd_pcm_uframes_t appl_ptr;
2112 appl_ptr = hw_ptr + runtime->buffer_size;
2113 appl_ptr %= runtime->boundary;
2114 runtime->control->appl_ptr = appl_ptr;
2117 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2119 struct snd_pcm_runtime *runtime;
2120 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2124 pcm_dbg(substream->pcm, "pcm_oss: trigger = 0x%x\n", trigger);
2127 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2128 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2131 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
2135 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
2139 runtime = psubstream->runtime;
2141 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2142 return -ERESTARTSYS;
2143 if (trigger & PCM_ENABLE_OUTPUT) {
2144 if (runtime->oss.trigger)
2146 if (atomic_read(&psubstream->mmap_count))
2147 snd_pcm_oss_simulate_fill(psubstream,
2148 get_hw_ptr_period(runtime));
2149 runtime->oss.trigger = 1;
2150 runtime->start_threshold = 1;
2151 cmd = SNDRV_PCM_IOCTL_START;
2153 if (!runtime->oss.trigger)
2155 runtime->oss.trigger = 0;
2156 runtime->start_threshold = runtime->boundary;
2157 cmd = SNDRV_PCM_IOCTL_DROP;
2158 runtime->oss.prepare = 1;
2161 mutex_unlock(&runtime->oss.params_lock);
2163 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2169 runtime = csubstream->runtime;
2171 if (mutex_lock_interruptible(&runtime->oss.params_lock))
2172 return -ERESTARTSYS;
2173 if (trigger & PCM_ENABLE_INPUT) {
2174 if (runtime->oss.trigger)
2176 runtime->oss.trigger = 1;
2177 runtime->start_threshold = 1;
2178 cmd = SNDRV_PCM_IOCTL_START;
2180 if (!runtime->oss.trigger)
2182 runtime->oss.trigger = 0;
2183 runtime->start_threshold = runtime->boundary;
2184 cmd = SNDRV_PCM_IOCTL_DROP;
2185 runtime->oss.prepare = 1;
2188 mutex_unlock(&runtime->oss.params_lock);
2190 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2198 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2200 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2203 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2204 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2205 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2206 result |= PCM_ENABLE_OUTPUT;
2207 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2208 result |= PCM_ENABLE_INPUT;
2212 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2214 struct snd_pcm_substream *substream;
2215 struct snd_pcm_runtime *runtime;
2216 snd_pcm_sframes_t delay;
2219 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2220 if (substream == NULL)
2222 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2224 runtime = substream->runtime;
2225 if (runtime->oss.params || runtime->oss.prepare)
2227 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2229 delay = 0; /* hack for broken OSS applications */
2232 return snd_pcm_oss_bytes(substream, delay);
2235 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2237 struct snd_pcm_substream *substream;
2238 struct snd_pcm_runtime *runtime;
2239 snd_pcm_sframes_t delay;
2241 struct count_info info;
2246 substream = pcm_oss_file->streams[stream];
2247 if (substream == NULL)
2249 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2251 runtime = substream->runtime;
2252 if (runtime->oss.params || runtime->oss.prepare) {
2253 memset(&info, 0, sizeof(info));
2254 if (copy_to_user(_info, &info, sizeof(info)))
2258 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2259 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2260 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2265 fixup = runtime->oss.buffer_used;
2268 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2269 fixup = -runtime->oss.buffer_used;
2273 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2274 if (atomic_read(&substream->mmap_count)) {
2275 snd_pcm_sframes_t n;
2276 delay = get_hw_ptr_period(runtime);
2277 n = delay - runtime->oss.prev_hw_ptr_period;
2279 n += runtime->boundary;
2280 info.blocks = n / runtime->period_size;
2281 runtime->oss.prev_hw_ptr_period = delay;
2282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2283 snd_pcm_oss_simulate_fill(substream, delay);
2284 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2286 delay = snd_pcm_oss_bytes(substream, delay);
2287 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2288 if (substream->oss.setup.buggyptr)
2289 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2291 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2292 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2295 info.blocks = delay / runtime->oss.period_bytes;
2296 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2299 if (copy_to_user(_info, &info, sizeof(info)))
2304 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2306 struct snd_pcm_substream *substream;
2307 struct snd_pcm_runtime *runtime;
2308 snd_pcm_sframes_t avail;
2310 struct audio_buf_info info;
2315 substream = pcm_oss_file->streams[stream];
2316 if (substream == NULL)
2318 runtime = substream->runtime;
2320 if (runtime->oss.params &&
2321 (err = snd_pcm_oss_change_params(substream, false)) < 0)
2324 info.fragsize = runtime->oss.period_bytes;
2325 info.fragstotal = runtime->periods;
2326 if (runtime->oss.prepare) {
2327 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2328 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2329 info.fragments = runtime->oss.periods;
2335 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2336 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2337 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2338 avail = runtime->buffer_size;
2342 avail = runtime->buffer_size - avail;
2343 fixup = -runtime->oss.buffer_used;
2346 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2347 fixup = runtime->oss.buffer_used;
2351 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2352 info.fragments = info.bytes / runtime->oss.period_bytes;
2356 pcm_dbg(substream->pcm,
2357 "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2358 info.bytes, info.fragments, info.fragstotal, info.fragsize);
2360 if (copy_to_user(_info, &info, sizeof(info)))
2365 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2367 // it won't be probably implemented
2368 // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2372 static const char *strip_task_path(const char *path)
2374 const char *ptr, *ptrl = NULL;
2375 for (ptr = path; *ptr; ptr++) {
2382 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2383 const char *task_name,
2384 struct snd_pcm_oss_setup *rsetup)
2386 struct snd_pcm_oss_setup *setup;
2388 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2390 for (setup = pcm->streams[stream].oss.setup_list; setup;
2391 setup = setup->next) {
2392 if (!strcmp(setup->task_name, task_name))
2395 } while ((task_name = strip_task_path(task_name)) != NULL);
2399 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2402 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2404 struct snd_pcm_runtime *runtime;
2405 runtime = substream->runtime;
2406 vfree(runtime->oss.buffer);
2407 runtime->oss.buffer = NULL;
2408 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2409 snd_pcm_oss_plugin_clear(substream);
2411 substream->oss.oss = 0;
2414 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2415 struct snd_pcm_oss_setup *setup,
2418 struct snd_pcm_runtime *runtime;
2420 substream->oss.oss = 1;
2421 substream->oss.setup = *setup;
2422 if (setup->nonblock)
2423 substream->f_flags |= O_NONBLOCK;
2424 else if (setup->block)
2425 substream->f_flags &= ~O_NONBLOCK;
2426 runtime = substream->runtime;
2427 runtime->oss.params = 1;
2428 runtime->oss.trigger = 1;
2429 runtime->oss.rate = 8000;
2430 mutex_init(&runtime->oss.params_lock);
2431 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2432 case SNDRV_MINOR_OSS_PCM_8:
2433 runtime->oss.format = AFMT_U8;
2435 case SNDRV_MINOR_OSS_PCM_16:
2436 runtime->oss.format = AFMT_S16_LE;
2439 runtime->oss.format = AFMT_MU_LAW;
2441 runtime->oss.channels = 1;
2442 runtime->oss.fragshift = 0;
2443 runtime->oss.maxfrags = 0;
2444 runtime->oss.subdivision = 0;
2445 substream->pcm_release = snd_pcm_oss_release_substream;
2446 atomic_set(&runtime->oss.rw_ref, 0);
2449 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2454 for (cidx = 0; cidx < 2; ++cidx) {
2455 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2457 snd_pcm_release_substream(substream);
2459 kfree(pcm_oss_file);
2463 static int snd_pcm_oss_open_file(struct file *file,
2464 struct snd_pcm *pcm,
2465 struct snd_pcm_oss_file **rpcm_oss_file,
2467 struct snd_pcm_oss_setup *setup)
2470 struct snd_pcm_oss_file *pcm_oss_file;
2471 struct snd_pcm_substream *substream;
2472 fmode_t f_mode = file->f_mode;
2475 *rpcm_oss_file = NULL;
2477 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2478 if (pcm_oss_file == NULL)
2481 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2482 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2483 f_mode = FMODE_WRITE;
2485 file->f_flags &= ~O_APPEND;
2486 for (idx = 0; idx < 2; idx++) {
2487 if (setup[idx].disable)
2489 if (! pcm->streams[idx].substream_count)
2490 continue; /* no matching substream */
2491 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2492 if (! (f_mode & FMODE_WRITE))
2495 if (! (f_mode & FMODE_READ))
2498 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2500 snd_pcm_oss_release_file(pcm_oss_file);
2504 pcm_oss_file->streams[idx] = substream;
2505 substream->file = pcm_oss_file;
2506 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2509 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2510 snd_pcm_oss_release_file(pcm_oss_file);
2514 file->private_data = pcm_oss_file;
2516 *rpcm_oss_file = pcm_oss_file;
2521 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2525 if (snd_BUG_ON(!task || !name || size < 2))
2527 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2528 name[idx] = task->comm[idx];
2533 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2537 struct snd_pcm *pcm;
2538 struct snd_pcm_oss_file *pcm_oss_file;
2539 struct snd_pcm_oss_setup setup[2];
2543 err = nonseekable_open(inode, file);
2547 pcm = snd_lookup_oss_minor_data(iminor(inode),
2548 SNDRV_OSS_DEVICE_TYPE_PCM);
2553 err = snd_card_file_add(pcm->card, file);
2556 if (!try_module_get(pcm->card->module)) {
2560 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2564 memset(setup, 0, sizeof(setup));
2565 if (file->f_mode & FMODE_WRITE)
2566 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2567 task_name, &setup[0]);
2568 if (file->f_mode & FMODE_READ)
2569 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2570 task_name, &setup[1]);
2572 nonblock = !!(file->f_flags & O_NONBLOCK);
2574 nonblock = nonblock_open;
2576 init_waitqueue_entry(&wait, current);
2577 add_wait_queue(&pcm->open_wait, &wait);
2578 mutex_lock(&pcm->open_mutex);
2580 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2581 iminor(inode), setup);
2584 if (err == -EAGAIN) {
2591 set_current_state(TASK_INTERRUPTIBLE);
2592 mutex_unlock(&pcm->open_mutex);
2594 mutex_lock(&pcm->open_mutex);
2595 if (pcm->card->shutdown) {
2599 if (signal_pending(current)) {
2604 remove_wait_queue(&pcm->open_wait, &wait);
2605 mutex_unlock(&pcm->open_mutex);
2608 snd_card_unref(pcm->card);
2612 module_put(pcm->card->module);
2614 snd_card_file_remove(pcm->card, file);
2617 snd_card_unref(pcm->card);
2621 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2623 struct snd_pcm *pcm;
2624 struct snd_pcm_substream *substream;
2625 struct snd_pcm_oss_file *pcm_oss_file;
2627 pcm_oss_file = file->private_data;
2628 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2629 if (substream == NULL)
2630 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2631 if (snd_BUG_ON(!substream))
2633 pcm = substream->pcm;
2634 if (!pcm->card->shutdown)
2635 snd_pcm_oss_sync(pcm_oss_file);
2636 mutex_lock(&pcm->open_mutex);
2637 snd_pcm_oss_release_file(pcm_oss_file);
2638 mutex_unlock(&pcm->open_mutex);
2639 wake_up(&pcm->open_wait);
2640 module_put(pcm->card->module);
2641 snd_card_file_remove(pcm->card, file);
2645 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2647 struct snd_pcm_oss_file *pcm_oss_file;
2648 int __user *p = (int __user *)arg;
2651 pcm_oss_file = file->private_data;
2652 if (cmd == OSS_GETVERSION)
2653 return put_user(SNDRV_OSS_VERSION, p);
2654 if (cmd == OSS_ALSAEMULVER)
2655 return put_user(1, p);
2656 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2657 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2658 struct snd_pcm_substream *substream;
2660 for (idx = 0; idx < 2; ++idx) {
2661 substream = pcm_oss_file->streams[idx];
2662 if (substream != NULL)
2665 if (snd_BUG_ON(idx >= 2))
2667 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2670 if (((cmd >> 8) & 0xff) != 'P')
2673 pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2676 case SNDCTL_DSP_RESET:
2677 return snd_pcm_oss_reset(pcm_oss_file);
2678 case SNDCTL_DSP_SYNC:
2679 return snd_pcm_oss_sync(pcm_oss_file);
2680 case SNDCTL_DSP_SPEED:
2681 if (get_user(res, p))
2683 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2685 return put_user(res, p);
2686 case SOUND_PCM_READ_RATE:
2687 res = snd_pcm_oss_get_rate(pcm_oss_file);
2690 return put_user(res, p);
2691 case SNDCTL_DSP_STEREO:
2692 if (get_user(res, p))
2694 res = res > 0 ? 2 : 1;
2695 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2697 return put_user(--res, p);
2698 case SNDCTL_DSP_GETBLKSIZE:
2699 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2702 return put_user(res, p);
2703 case SNDCTL_DSP_SETFMT:
2704 if (get_user(res, p))
2706 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2709 return put_user(res, p);
2710 case SOUND_PCM_READ_BITS:
2711 res = snd_pcm_oss_get_format(pcm_oss_file);
2714 return put_user(res, p);
2715 case SNDCTL_DSP_CHANNELS:
2716 if (get_user(res, p))
2718 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2721 return put_user(res, p);
2722 case SOUND_PCM_READ_CHANNELS:
2723 res = snd_pcm_oss_get_channels(pcm_oss_file);
2726 return put_user(res, p);
2727 case SOUND_PCM_WRITE_FILTER:
2728 case SOUND_PCM_READ_FILTER:
2730 case SNDCTL_DSP_POST:
2731 return snd_pcm_oss_post(pcm_oss_file);
2732 case SNDCTL_DSP_SUBDIVIDE:
2733 if (get_user(res, p))
2735 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2738 return put_user(res, p);
2739 case SNDCTL_DSP_SETFRAGMENT:
2740 if (get_user(res, p))
2742 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2743 case SNDCTL_DSP_GETFMTS:
2744 res = snd_pcm_oss_get_formats(pcm_oss_file);
2747 return put_user(res, p);
2748 case SNDCTL_DSP_GETOSPACE:
2749 case SNDCTL_DSP_GETISPACE:
2750 return snd_pcm_oss_get_space(pcm_oss_file,
2751 cmd == SNDCTL_DSP_GETISPACE ?
2752 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2753 (struct audio_buf_info __user *) arg);
2754 case SNDCTL_DSP_NONBLOCK:
2755 return snd_pcm_oss_nonblock(file);
2756 case SNDCTL_DSP_GETCAPS:
2757 res = snd_pcm_oss_get_caps(pcm_oss_file);
2760 return put_user(res, p);
2761 case SNDCTL_DSP_GETTRIGGER:
2762 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2765 return put_user(res, p);
2766 case SNDCTL_DSP_SETTRIGGER:
2767 if (get_user(res, p))
2769 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2770 case SNDCTL_DSP_GETIPTR:
2771 case SNDCTL_DSP_GETOPTR:
2772 return snd_pcm_oss_get_ptr(pcm_oss_file,
2773 cmd == SNDCTL_DSP_GETIPTR ?
2774 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2775 (struct count_info __user *) arg);
2776 case SNDCTL_DSP_MAPINBUF:
2777 case SNDCTL_DSP_MAPOUTBUF:
2778 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2779 cmd == SNDCTL_DSP_MAPINBUF ?
2780 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2781 (struct buffmem_desc __user *) arg);
2782 case SNDCTL_DSP_SETSYNCRO:
2783 /* stop DMA now.. */
2785 case SNDCTL_DSP_SETDUPLEX:
2786 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2789 case SNDCTL_DSP_GETODELAY:
2790 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2792 /* it's for sure, some broken apps don't check for error codes */
2796 return put_user(res, p);
2797 case SNDCTL_DSP_PROFILE:
2798 return 0; /* silently ignore */
2800 pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2805 #ifdef CONFIG_COMPAT
2806 /* all compatible */
2807 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2809 #define snd_pcm_oss_ioctl_compat NULL
2812 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2814 struct snd_pcm_oss_file *pcm_oss_file;
2815 struct snd_pcm_substream *substream;
2817 pcm_oss_file = file->private_data;
2818 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2819 if (substream == NULL)
2821 substream->f_flags = file->f_flags & O_NONBLOCK;
2823 return snd_pcm_oss_read1(substream, buf, count);
2826 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2827 pcm_dbg(substream->pcm,
2828 "pcm_oss: read %li bytes (returned %li bytes)\n",
2829 (long)count, (long)res);
2835 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2837 struct snd_pcm_oss_file *pcm_oss_file;
2838 struct snd_pcm_substream *substream;
2841 pcm_oss_file = file->private_data;
2842 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2843 if (substream == NULL)
2845 substream->f_flags = file->f_flags & O_NONBLOCK;
2846 result = snd_pcm_oss_write1(substream, buf, count);
2848 pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2849 (long)count, (long)result);
2854 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2856 struct snd_pcm_runtime *runtime = substream->runtime;
2857 if (atomic_read(&substream->mmap_count))
2858 return runtime->oss.prev_hw_ptr_period !=
2859 get_hw_ptr_period(runtime);
2861 return snd_pcm_playback_avail(runtime) >=
2862 runtime->oss.period_frames;
2865 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2867 struct snd_pcm_runtime *runtime = substream->runtime;
2868 if (atomic_read(&substream->mmap_count))
2869 return runtime->oss.prev_hw_ptr_period !=
2870 get_hw_ptr_period(runtime);
2872 return snd_pcm_capture_avail(runtime) >=
2873 runtime->oss.period_frames;
2876 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2878 struct snd_pcm_oss_file *pcm_oss_file;
2880 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2882 pcm_oss_file = file->private_data;
2884 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2885 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2888 if (psubstream != NULL) {
2889 struct snd_pcm_runtime *runtime = psubstream->runtime;
2890 poll_wait(file, &runtime->sleep, wait);
2891 snd_pcm_stream_lock_irq(psubstream);
2892 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2893 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2894 snd_pcm_oss_playback_ready(psubstream)))
2895 mask |= POLLOUT | POLLWRNORM;
2896 snd_pcm_stream_unlock_irq(psubstream);
2898 if (csubstream != NULL) {
2899 struct snd_pcm_runtime *runtime = csubstream->runtime;
2900 snd_pcm_state_t ostate;
2901 poll_wait(file, &runtime->sleep, wait);
2902 snd_pcm_stream_lock_irq(csubstream);
2903 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2904 snd_pcm_oss_capture_ready(csubstream))
2905 mask |= POLLIN | POLLRDNORM;
2906 snd_pcm_stream_unlock_irq(csubstream);
2907 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2908 struct snd_pcm_oss_file ofile;
2909 memset(&ofile, 0, sizeof(ofile));
2910 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2911 runtime->oss.trigger = 0;
2912 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2919 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2921 struct snd_pcm_oss_file *pcm_oss_file;
2922 struct snd_pcm_substream *substream = NULL;
2923 struct snd_pcm_runtime *runtime;
2927 pr_debug("pcm_oss: mmap begin\n");
2929 pcm_oss_file = file->private_data;
2930 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2931 case VM_READ | VM_WRITE:
2932 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2937 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2940 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2945 /* set VM_READ access as well to fix memset() routines that do
2946 reads before writes (to improve performance) */
2947 area->vm_flags |= VM_READ;
2948 if (substream == NULL)
2950 runtime = substream->runtime;
2951 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2953 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2954 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2958 if (runtime->oss.params) {
2959 /* use mutex_trylock() for params_lock for avoiding a deadlock
2960 * between mmap_sem and params_lock taken by
2961 * copy_from/to_user() in snd_pcm_oss_write/read()
2963 err = snd_pcm_oss_change_params(substream, true);
2967 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2968 if (runtime->oss.plugin_first != NULL)
2972 if (area->vm_pgoff != 0)
2975 err = snd_pcm_mmap_data(substream, file, area);
2978 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2979 runtime->silence_threshold = 0;
2980 runtime->silence_size = 0;
2982 pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2983 runtime->oss.mmap_bytes);
2985 /* In mmap mode we never stop */
2986 runtime->stop_threshold = runtime->boundary;
2991 #ifdef CONFIG_SND_VERBOSE_PROCFS
2996 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2997 struct snd_info_buffer *buffer)
2999 struct snd_pcm_str *pstr = entry->private_data;
3000 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
3001 mutex_lock(&pstr->oss.setup_mutex);
3003 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
3007 setup->disable ? " disable" : "",
3008 setup->direct ? " direct" : "",
3009 setup->block ? " block" : "",
3010 setup->nonblock ? " non-block" : "",
3011 setup->partialfrag ? " partial-frag" : "",
3012 setup->nosilence ? " no-silence" : "");
3013 setup = setup->next;
3015 mutex_unlock(&pstr->oss.setup_mutex);
3018 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
3020 struct snd_pcm_oss_setup *setup, *setupn;
3022 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
3023 setup; setup = setupn) {
3024 setupn = setup->next;
3025 kfree(setup->task_name);
3028 pstr->oss.setup_list = NULL;
3031 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
3032 struct snd_info_buffer *buffer)
3034 struct snd_pcm_str *pstr = entry->private_data;
3035 char line[128], str[32], task_name[32];
3038 struct snd_pcm_oss_setup *setup, *setup1, template;
3040 while (!snd_info_get_line(buffer, line, sizeof(line))) {
3041 mutex_lock(&pstr->oss.setup_mutex);
3042 memset(&template, 0, sizeof(template));
3043 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
3044 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
3045 snd_pcm_oss_proc_free_setup_list(pstr);
3046 mutex_unlock(&pstr->oss.setup_mutex);
3049 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
3050 if (!strcmp(setup->task_name, task_name)) {
3055 ptr = snd_info_get_str(str, ptr, sizeof(str));
3056 template.periods = simple_strtoul(str, NULL, 10);
3057 ptr = snd_info_get_str(str, ptr, sizeof(str));
3058 template.period_size = simple_strtoul(str, NULL, 10);
3059 for (idx1 = 31; idx1 >= 0; idx1--)
3060 if (template.period_size & (1 << idx1))
3062 for (idx1--; idx1 >= 0; idx1--)
3063 template.period_size &= ~(1 << idx1);
3065 ptr = snd_info_get_str(str, ptr, sizeof(str));
3066 if (!strcmp(str, "disable")) {
3067 template.disable = 1;
3068 } else if (!strcmp(str, "direct")) {
3069 template.direct = 1;
3070 } else if (!strcmp(str, "block")) {
3072 } else if (!strcmp(str, "non-block")) {
3073 template.nonblock = 1;
3074 } else if (!strcmp(str, "partial-frag")) {
3075 template.partialfrag = 1;
3076 } else if (!strcmp(str, "no-silence")) {
3077 template.nosilence = 1;
3078 } else if (!strcmp(str, "buggy-ptr")) {
3079 template.buggyptr = 1;
3082 if (setup == NULL) {
3083 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
3085 buffer->error = -ENOMEM;
3086 mutex_unlock(&pstr->oss.setup_mutex);
3089 if (pstr->oss.setup_list == NULL)
3090 pstr->oss.setup_list = setup;
3092 for (setup1 = pstr->oss.setup_list;
3093 setup1->next; setup1 = setup1->next);
3094 setup1->next = setup;
3096 template.task_name = kstrdup(task_name, GFP_KERNEL);
3097 if (! template.task_name) {
3099 buffer->error = -ENOMEM;
3100 mutex_unlock(&pstr->oss.setup_mutex);
3105 mutex_unlock(&pstr->oss.setup_mutex);
3109 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3112 for (stream = 0; stream < 2; ++stream) {
3113 struct snd_info_entry *entry;
3114 struct snd_pcm_str *pstr = &pcm->streams[stream];
3115 if (pstr->substream_count == 0)
3117 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
3118 entry->content = SNDRV_INFO_CONTENT_TEXT;
3119 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
3120 entry->c.text.read = snd_pcm_oss_proc_read;
3121 entry->c.text.write = snd_pcm_oss_proc_write;
3122 entry->private_data = pstr;
3123 if (snd_info_register(entry) < 0) {
3124 snd_info_free_entry(entry);
3128 pstr->oss.proc_entry = entry;
3132 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3135 for (stream = 0; stream < 2; ++stream) {
3136 struct snd_pcm_str *pstr = &pcm->streams[stream];
3137 snd_info_free_entry(pstr->oss.proc_entry);
3138 pstr->oss.proc_entry = NULL;
3139 snd_pcm_oss_proc_free_setup_list(pstr);
3142 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3143 #define snd_pcm_oss_proc_init(pcm)
3144 #define snd_pcm_oss_proc_done(pcm)
3145 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3151 static const struct file_operations snd_pcm_oss_f_reg =
3153 .owner = THIS_MODULE,
3154 .read = snd_pcm_oss_read,
3155 .write = snd_pcm_oss_write,
3156 .open = snd_pcm_oss_open,
3157 .release = snd_pcm_oss_release,
3158 .llseek = no_llseek,
3159 .poll = snd_pcm_oss_poll,
3160 .unlocked_ioctl = snd_pcm_oss_ioctl,
3161 .compat_ioctl = snd_pcm_oss_ioctl_compat,
3162 .mmap = snd_pcm_oss_mmap,
3165 static void register_oss_dsp(struct snd_pcm *pcm, int index)
3167 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3168 pcm->card, index, &snd_pcm_oss_f_reg,
3170 pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3171 pcm->card->number, pcm->device);
3175 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3178 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3181 register_oss_dsp(pcm, 0);
3182 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3183 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3184 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3185 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3186 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3187 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3192 pcm->oss.reg_mask |= 1;
3194 if (adsp_map[pcm->card->number] == (int)pcm->device) {
3195 register_oss_dsp(pcm, 1);
3197 pcm->oss.reg_mask |= 2;
3201 snd_pcm_oss_proc_init(pcm);
3206 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3209 if (pcm->oss.reg_mask & 1) {
3210 pcm->oss.reg_mask &= ~1;
3211 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3214 if (pcm->oss.reg_mask & 2) {
3215 pcm->oss.reg_mask &= ~2;
3216 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3219 if (dsp_map[pcm->card->number] == (int)pcm->device) {
3220 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3221 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3229 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3231 snd_pcm_oss_disconnect_minor(pcm);
3232 snd_pcm_oss_proc_done(pcm);
3236 static struct snd_pcm_notify snd_pcm_oss_notify =
3238 .n_register = snd_pcm_oss_register_minor,
3239 .n_disconnect = snd_pcm_oss_disconnect_minor,
3240 .n_unregister = snd_pcm_oss_unregister_minor,
3243 static int __init alsa_pcm_oss_init(void)
3248 /* check device map table */
3249 for (i = 0; i < SNDRV_CARDS; i++) {
3250 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3251 pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3255 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3256 pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3261 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3266 static void __exit alsa_pcm_oss_exit(void)
3268 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3271 module_init(alsa_pcm_oss_init)
3272 module_exit(alsa_pcm_oss_exit)