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/moduleparam.h>
34 #include <linux/string.h>
35 #include <sound/core.h>
36 #include <sound/minors.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include "pcm_plugin.h"
40 #include <sound/info.h>
41 #include <linux/soundcard.h>
42 #include <sound/initval.h>
44 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
46 static int dsp_map
[SNDRV_CARDS
];
47 static int adsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
48 static int nonblock_open
= 1;
50 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
51 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
52 MODULE_LICENSE("GPL");
53 module_param_array(dsp_map
, int, NULL
, 0444);
54 MODULE_PARM_DESC(dsp_map
, "PCM device number assigned to 1st OSS device.");
55 module_param_array(adsp_map
, int, NULL
, 0444);
56 MODULE_PARM_DESC(adsp_map
, "PCM device number assigned to 2nd OSS device.");
57 module_param(nonblock_open
, bool, 0644);
58 MODULE_PARM_DESC(nonblock_open
, "Don't block opening busy PCM devices.");
59 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM
);
60 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1
);
62 extern int snd_mixer_oss_ioctl_card(struct snd_card
*card
, unsigned int cmd
, unsigned long arg
);
63 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
);
64 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
);
65 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
);
67 static inline mm_segment_t
snd_enter_user(void)
69 mm_segment_t fs
= get_fs();
74 static inline void snd_leave_user(mm_segment_t fs
)
80 * helper functions to process hw_params
82 static int snd_interval_refine_min(struct snd_interval
*i
, unsigned int min
, int openmin
)
89 } else if (i
->min
== min
&& !i
->openmin
&& openmin
) {
99 if (snd_interval_checkempty(i
)) {
100 snd_interval_none(i
);
106 static int snd_interval_refine_max(struct snd_interval
*i
, unsigned int max
, int openmax
)
111 i
->openmax
= openmax
;
113 } else if (i
->max
== max
&& !i
->openmax
&& openmax
) {
123 if (snd_interval_checkempty(i
)) {
124 snd_interval_none(i
);
130 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
132 struct snd_interval t
;
135 t
.openmin
= t
.openmax
= 0;
137 return snd_interval_refine(i
, &t
);
141 * snd_pcm_hw_param_value_min
142 * @params: the hw_params instance
143 * @var: parameter to retrieve
144 * @dir: pointer to the direction (-1,0,1) or NULL
146 * Return the minimum value for field PAR.
149 snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params
*params
,
150 snd_pcm_hw_param_t var
, int *dir
)
152 if (hw_is_mask(var
)) {
155 return snd_mask_min(hw_param_mask_c(params
, var
));
157 if (hw_is_interval(var
)) {
158 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
161 return snd_interval_min(i
);
167 * snd_pcm_hw_param_value_max
168 * @params: the hw_params instance
169 * @var: parameter to retrieve
170 * @dir: pointer to the direction (-1,0,1) or NULL
172 * Return the maximum value for field PAR.
175 snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params
*params
,
176 snd_pcm_hw_param_t var
, int *dir
)
178 if (hw_is_mask(var
)) {
181 return snd_mask_max(hw_param_mask_c(params
, var
));
183 if (hw_is_interval(var
)) {
184 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
186 *dir
= - (int) i
->openmax
;
187 return snd_interval_max(i
);
192 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params
*params
,
193 snd_pcm_hw_param_t var
,
194 const struct snd_mask
*val
)
197 changed
= snd_mask_refine(hw_param_mask(params
, var
), val
);
199 params
->cmask
|= 1 << var
;
200 params
->rmask
|= 1 << var
;
205 static int snd_pcm_hw_param_mask(struct snd_pcm_substream
*pcm
,
206 struct snd_pcm_hw_params
*params
,
207 snd_pcm_hw_param_t var
,
208 const struct snd_mask
*val
)
210 int changed
= _snd_pcm_hw_param_mask(params
, var
, val
);
214 int err
= snd_pcm_hw_refine(pcm
, params
);
221 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params
*params
,
222 snd_pcm_hw_param_t var
, unsigned int val
,
230 } else if (dir
< 0) {
238 changed
= snd_mask_refine_min(hw_param_mask(params
, var
),
240 else if (hw_is_interval(var
))
241 changed
= snd_interval_refine_min(hw_param_interval(params
, var
),
246 params
->cmask
|= 1 << var
;
247 params
->rmask
|= 1 << var
;
253 * snd_pcm_hw_param_min
255 * @params: the hw_params instance
256 * @var: parameter to retrieve
257 * @val: minimal value
258 * @dir: pointer to the direction (-1,0,1) or NULL
260 * Inside configuration space defined by PARAMS remove from PAR all
261 * values < VAL. Reduce configuration space accordingly.
262 * Return new minimum or -EINVAL if the configuration space is empty
264 static int snd_pcm_hw_param_min(struct snd_pcm_substream
*pcm
,
265 struct snd_pcm_hw_params
*params
,
266 snd_pcm_hw_param_t var
, unsigned int val
,
269 int changed
= _snd_pcm_hw_param_min(params
, var
, val
, dir
? *dir
: 0);
273 int err
= snd_pcm_hw_refine(pcm
, params
);
277 return snd_pcm_hw_param_value_min(params
, var
, dir
);
280 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params
*params
,
281 snd_pcm_hw_param_t var
, unsigned int val
,
289 } else if (dir
> 0) {
294 if (hw_is_mask(var
)) {
295 if (val
== 0 && open
) {
296 snd_mask_none(hw_param_mask(params
, var
));
299 changed
= snd_mask_refine_max(hw_param_mask(params
, var
),
301 } else if (hw_is_interval(var
))
302 changed
= snd_interval_refine_max(hw_param_interval(params
, var
),
307 params
->cmask
|= 1 << var
;
308 params
->rmask
|= 1 << var
;
314 * snd_pcm_hw_param_max
316 * @params: the hw_params instance
317 * @var: parameter to retrieve
318 * @val: maximal value
319 * @dir: pointer to the direction (-1,0,1) or NULL
321 * Inside configuration space defined by PARAMS remove from PAR all
322 * values >= VAL + 1. Reduce configuration space accordingly.
323 * Return new maximum or -EINVAL if the configuration space is empty
325 static int snd_pcm_hw_param_max(struct snd_pcm_substream
*pcm
,
326 struct snd_pcm_hw_params
*params
,
327 snd_pcm_hw_param_t var
, unsigned int val
,
330 int changed
= _snd_pcm_hw_param_max(params
, var
, val
, dir
? *dir
: 0);
334 int err
= snd_pcm_hw_refine(pcm
, params
);
338 return snd_pcm_hw_param_value_max(params
, var
, dir
);
341 static int boundary_sub(int a
, int adir
,
345 adir
= adir
< 0 ? -1 : (adir
> 0 ? 1 : 0);
346 bdir
= bdir
< 0 ? -1 : (bdir
> 0 ? 1 : 0);
351 } else if (*cdir
== 2) {
357 static int boundary_lt(unsigned int a
, int adir
,
358 unsigned int b
, int bdir
)
370 return a
< b
|| (a
== b
&& adir
< bdir
);
373 /* Return 1 if min is nearer to best than max */
374 static int boundary_nearer(int min
, int mindir
,
375 int best
, int bestdir
,
380 boundary_sub(best
, bestdir
, min
, mindir
, &dmin
, &dmindir
);
381 boundary_sub(max
, maxdir
, best
, bestdir
, &dmax
, &dmaxdir
);
382 return boundary_lt(dmin
, dmindir
, dmax
, dmaxdir
);
386 * snd_pcm_hw_param_near
388 * @params: the hw_params instance
389 * @var: parameter to retrieve
390 * @best: value to set
391 * @dir: pointer to the direction (-1,0,1) or NULL
393 * Inside configuration space defined by PARAMS set PAR to the available value
394 * nearest to VAL. Reduce configuration space accordingly.
395 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
396 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
397 * Return the value found.
399 static int snd_pcm_hw_param_near(struct snd_pcm_substream
*pcm
,
400 struct snd_pcm_hw_params
*params
,
401 snd_pcm_hw_param_t var
, unsigned int best
,
404 struct snd_pcm_hw_params
*save
= NULL
;
406 unsigned int saved_min
;
410 int valdir
= dir
? *dir
: 0;
415 mindir
= maxdir
= valdir
;
418 else if (maxdir
== 0)
424 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
429 min
= snd_pcm_hw_param_min(pcm
, params
, var
, min
, &mindir
);
431 struct snd_pcm_hw_params
*params1
;
434 if ((unsigned int)min
== saved_min
&& mindir
== valdir
)
436 params1
= kmalloc(sizeof(*params1
), GFP_KERNEL
);
437 if (params1
== NULL
) {
442 max
= snd_pcm_hw_param_max(pcm
, params1
, var
, max
, &maxdir
);
447 if (boundary_nearer(max
, maxdir
, best
, valdir
, min
, mindir
)) {
454 max
= snd_pcm_hw_param_max(pcm
, params
, var
, max
, &maxdir
);
462 v
= snd_pcm_hw_param_last(pcm
, params
, var
, dir
);
464 v
= snd_pcm_hw_param_first(pcm
, params
, var
, dir
);
469 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
470 snd_pcm_hw_param_t var
, unsigned int val
,
474 if (hw_is_mask(var
)) {
475 struct snd_mask
*m
= hw_param_mask(params
, var
);
476 if (val
== 0 && dir
< 0) {
484 changed
= snd_mask_refine_set(hw_param_mask(params
, var
), val
);
486 } else if (hw_is_interval(var
)) {
487 struct snd_interval
*i
= hw_param_interval(params
, var
);
488 if (val
== 0 && dir
< 0) {
490 snd_interval_none(i
);
492 changed
= snd_interval_refine_set(i
, val
);
494 struct snd_interval t
;
506 changed
= snd_interval_refine(i
, &t
);
511 params
->cmask
|= 1 << var
;
512 params
->rmask
|= 1 << var
;
518 * snd_pcm_hw_param_set
520 * @params: the hw_params instance
521 * @var: parameter to retrieve
523 * @dir: pointer to the direction (-1,0,1) or NULL
525 * Inside configuration space defined by PARAMS remove from PAR all
526 * values != VAL. Reduce configuration space accordingly.
527 * Return VAL or -EINVAL if the configuration space is empty
529 static int snd_pcm_hw_param_set(struct snd_pcm_substream
*pcm
,
530 struct snd_pcm_hw_params
*params
,
531 snd_pcm_hw_param_t var
, unsigned int val
,
534 int changed
= _snd_pcm_hw_param_set(params
, var
, val
, dir
);
538 int err
= snd_pcm_hw_refine(pcm
, params
);
542 return snd_pcm_hw_param_value(params
, var
, NULL
);
545 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params
*params
,
546 snd_pcm_hw_param_t var
)
549 changed
= snd_interval_setinteger(hw_param_interval(params
, var
));
551 params
->cmask
|= 1 << var
;
552 params
->rmask
|= 1 << var
;
561 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
562 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream
*substream
)
564 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
565 struct snd_pcm_plugin
*plugin
, *next
;
567 plugin
= runtime
->oss
.plugin_first
;
570 snd_pcm_plugin_free(plugin
);
573 runtime
->oss
.plugin_first
= runtime
->oss
.plugin_last
= NULL
;
577 static int snd_pcm_plugin_insert(struct snd_pcm_plugin
*plugin
)
579 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
580 plugin
->next
= runtime
->oss
.plugin_first
;
582 if (runtime
->oss
.plugin_first
) {
583 runtime
->oss
.plugin_first
->prev
= plugin
;
584 runtime
->oss
.plugin_first
= plugin
;
586 runtime
->oss
.plugin_last
=
587 runtime
->oss
.plugin_first
= plugin
;
592 int snd_pcm_plugin_append(struct snd_pcm_plugin
*plugin
)
594 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
596 plugin
->prev
= runtime
->oss
.plugin_last
;
597 if (runtime
->oss
.plugin_last
) {
598 runtime
->oss
.plugin_last
->next
= plugin
;
599 runtime
->oss
.plugin_last
= plugin
;
601 runtime
->oss
.plugin_last
=
602 runtime
->oss
.plugin_first
= plugin
;
606 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
608 static long snd_pcm_oss_bytes(struct snd_pcm_substream
*substream
, long frames
)
610 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
611 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
612 long bytes
= frames_to_bytes(runtime
, frames
);
613 if (buffer_size
== runtime
->oss
.buffer_bytes
)
615 #if BITS_PER_LONG >= 64
616 return runtime
->oss
.buffer_bytes
* bytes
/ buffer_size
;
619 u64 bsize
= (u64
)runtime
->oss
.buffer_bytes
* (u64
)bytes
;
621 div64_32(&bsize
, buffer_size
, &rem
);
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
);
636 /* define extended formats in the recent OSS versions (if any) */
638 #define AFMT_S32_LE 0x00001000
639 #define AFMT_S32_BE 0x00002000
640 #define AFMT_S24_LE 0x00008000
641 #define AFMT_S24_BE 0x00010000
642 #define AFMT_S24_PACKED 0x00040000
644 /* other supported formats */
645 #define AFMT_FLOAT 0x00004000
646 #define AFMT_SPDIF_RAW 0x00020000
648 /* unsupported formats */
649 #define AFMT_AC3 0x00000400
650 #define AFMT_VORBIS 0x00000800
652 static int snd_pcm_oss_format_from(int format
)
655 case AFMT_MU_LAW
: return SNDRV_PCM_FORMAT_MU_LAW
;
656 case AFMT_A_LAW
: return SNDRV_PCM_FORMAT_A_LAW
;
657 case AFMT_IMA_ADPCM
: return SNDRV_PCM_FORMAT_IMA_ADPCM
;
658 case AFMT_U8
: return SNDRV_PCM_FORMAT_U8
;
659 case AFMT_S16_LE
: return SNDRV_PCM_FORMAT_S16_LE
;
660 case AFMT_S16_BE
: return SNDRV_PCM_FORMAT_S16_BE
;
661 case AFMT_S8
: return SNDRV_PCM_FORMAT_S8
;
662 case AFMT_U16_LE
: return SNDRV_PCM_FORMAT_U16_LE
;
663 case AFMT_U16_BE
: return SNDRV_PCM_FORMAT_U16_BE
;
664 case AFMT_MPEG
: return SNDRV_PCM_FORMAT_MPEG
;
665 case AFMT_S32_LE
: return SNDRV_PCM_FORMAT_S32_LE
;
666 case AFMT_S32_BE
: return SNDRV_PCM_FORMAT_S32_BE
;
667 case AFMT_S24_LE
: return SNDRV_PCM_FORMAT_S24_LE
;
668 case AFMT_S24_BE
: return SNDRV_PCM_FORMAT_S24_BE
;
669 case AFMT_S24_PACKED
: return SNDRV_PCM_FORMAT_S24_3LE
;
670 case AFMT_FLOAT
: return SNDRV_PCM_FORMAT_FLOAT
;
671 case AFMT_SPDIF_RAW
: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME
;
672 default: return SNDRV_PCM_FORMAT_U8
;
676 static int snd_pcm_oss_format_to(int format
)
679 case SNDRV_PCM_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
680 case SNDRV_PCM_FORMAT_A_LAW
: return AFMT_A_LAW
;
681 case SNDRV_PCM_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
682 case SNDRV_PCM_FORMAT_U8
: return AFMT_U8
;
683 case SNDRV_PCM_FORMAT_S16_LE
: return AFMT_S16_LE
;
684 case SNDRV_PCM_FORMAT_S16_BE
: return AFMT_S16_BE
;
685 case SNDRV_PCM_FORMAT_S8
: return AFMT_S8
;
686 case SNDRV_PCM_FORMAT_U16_LE
: return AFMT_U16_LE
;
687 case SNDRV_PCM_FORMAT_U16_BE
: return AFMT_U16_BE
;
688 case SNDRV_PCM_FORMAT_MPEG
: return AFMT_MPEG
;
689 case SNDRV_PCM_FORMAT_S32_LE
: return AFMT_S32_LE
;
690 case SNDRV_PCM_FORMAT_S32_BE
: return AFMT_S32_BE
;
691 case SNDRV_PCM_FORMAT_S24_LE
: return AFMT_S24_LE
;
692 case SNDRV_PCM_FORMAT_S24_BE
: return AFMT_S24_BE
;
693 case SNDRV_PCM_FORMAT_S24_3LE
: return AFMT_S24_PACKED
;
694 case SNDRV_PCM_FORMAT_FLOAT
: return AFMT_FLOAT
;
695 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME
: return AFMT_SPDIF_RAW
;
696 default: return -EINVAL
;
700 static int snd_pcm_oss_period_size(struct snd_pcm_substream
*substream
,
701 struct snd_pcm_hw_params
*oss_params
,
702 struct snd_pcm_hw_params
*slave_params
)
705 size_t oss_buffer_size
, oss_period_size
, oss_periods
;
706 size_t min_period_size
, max_period_size
;
707 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
708 size_t oss_frame_size
;
710 oss_frame_size
= snd_pcm_format_physical_width(params_format(oss_params
)) *
711 params_channels(oss_params
) / 8;
713 oss_buffer_size
= snd_pcm_plug_client_size(substream
,
714 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
)) * oss_frame_size
;
715 oss_buffer_size
= 1 << ld2(oss_buffer_size
);
716 if (atomic_read(&substream
->mmap_count
)) {
717 if (oss_buffer_size
> runtime
->oss
.mmap_bytes
)
718 oss_buffer_size
= runtime
->oss
.mmap_bytes
;
721 if (substream
->oss
.setup
.period_size
> 16)
722 oss_period_size
= substream
->oss
.setup
.period_size
;
723 else if (runtime
->oss
.fragshift
) {
724 oss_period_size
= 1 << runtime
->oss
.fragshift
;
725 if (oss_period_size
> oss_buffer_size
/ 2)
726 oss_period_size
= oss_buffer_size
/ 2;
729 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
731 oss_period_size
= oss_buffer_size
;
733 oss_period_size
/= 2;
734 } while (oss_period_size
> bytes_per_sec
);
735 if (runtime
->oss
.subdivision
== 0) {
737 if (oss_period_size
/ sd
> 4096)
739 if (oss_period_size
/ sd
< 4096)
742 sd
= runtime
->oss
.subdivision
;
743 oss_period_size
/= sd
;
744 if (oss_period_size
< 16)
745 oss_period_size
= 16;
748 min_period_size
= snd_pcm_plug_client_size(substream
,
749 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
750 min_period_size
*= oss_frame_size
;
751 min_period_size
= 1 << (ld2(min_period_size
- 1) + 1);
752 if (oss_period_size
< min_period_size
)
753 oss_period_size
= min_period_size
;
755 max_period_size
= snd_pcm_plug_client_size(substream
,
756 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
757 max_period_size
*= oss_frame_size
;
758 max_period_size
= 1 << ld2(max_period_size
);
759 if (oss_period_size
> max_period_size
)
760 oss_period_size
= max_period_size
;
762 oss_periods
= oss_buffer_size
/ oss_period_size
;
764 if (substream
->oss
.setup
.periods
> 1)
765 oss_periods
= substream
->oss
.setup
.periods
;
767 s
= snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
768 if (runtime
->oss
.maxfrags
&& s
> runtime
->oss
.maxfrags
)
769 s
= runtime
->oss
.maxfrags
;
773 s
= snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
779 while (oss_period_size
* oss_periods
> oss_buffer_size
)
780 oss_period_size
/= 2;
782 if (oss_period_size
< 16)
784 runtime
->oss
.period_bytes
= oss_period_size
;
785 runtime
->oss
.period_frames
= 1;
786 runtime
->oss
.periods
= oss_periods
;
790 static int choose_rate(struct snd_pcm_substream
*substream
,
791 struct snd_pcm_hw_params
*params
, unsigned int best_rate
)
793 struct snd_interval
*it
;
794 struct snd_pcm_hw_params
*save
;
795 unsigned int rate
, prev
;
797 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
801 it
= hw_param_interval(save
, SNDRV_PCM_HW_PARAM_RATE
);
803 /* try multiples of the best rate */
806 if (it
->max
< rate
|| (it
->max
== rate
&& it
->openmax
))
808 if (it
->min
< rate
|| (it
->min
== rate
&& !it
->openmin
)) {
810 ret
= snd_pcm_hw_param_set(substream
, params
,
811 SNDRV_PCM_HW_PARAM_RATE
,
813 if (ret
== (int)rate
) {
825 /* not found, use the nearest rate */
827 return snd_pcm_hw_param_near(substream
, params
, SNDRV_PCM_HW_PARAM_RATE
, best_rate
, NULL
);
830 static int snd_pcm_oss_change_params(struct snd_pcm_substream
*substream
)
832 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
833 struct snd_pcm_hw_params
*params
, *sparams
;
834 struct snd_pcm_sw_params
*sw_params
;
835 ssize_t oss_buffer_size
, oss_period_size
;
836 size_t oss_frame_size
;
839 int format
, sformat
, n
;
840 struct snd_mask sformat_mask
;
841 struct snd_mask mask
;
843 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
845 sw_params
= kmalloc(sizeof(*sw_params
), GFP_KERNEL
);
846 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
847 sparams
= kmalloc(sizeof(*sparams
), GFP_KERNEL
);
848 if (!sw_params
|| !params
|| !sparams
) {
849 snd_printd("No memory\n");
854 if (atomic_read(&substream
->mmap_count
))
857 direct
= substream
->oss
.setup
.direct
;
859 _snd_pcm_hw_params_any(sparams
);
860 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
861 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
862 snd_mask_none(&mask
);
863 if (atomic_read(&substream
->mmap_count
))
864 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
866 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
868 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
870 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
872 snd_printd("No usable accesses\n");
876 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
877 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
879 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
881 sformat_mask
= *hw_param_mask(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
885 sformat
= snd_pcm_plug_slave_format(format
, &sformat_mask
);
887 if (sformat
< 0 || !snd_mask_test(&sformat_mask
, sformat
)) {
888 for (sformat
= 0; sformat
<= SNDRV_PCM_FORMAT_LAST
; sformat
++) {
889 if (snd_mask_test(&sformat_mask
, sformat
) &&
890 snd_pcm_oss_format_to(sformat
) >= 0)
893 if (sformat
> SNDRV_PCM_FORMAT_LAST
) {
894 snd_printd("Cannot find a format!!!\n");
899 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, sformat
, 0);
904 memcpy(params
, sparams
, sizeof(*params
));
906 _snd_pcm_hw_params_any(params
);
907 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
908 SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
909 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
910 snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
911 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
912 runtime
->oss
.channels
, 0);
913 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
914 runtime
->oss
.rate
, 0);
915 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
916 params_access(params
), params_format(params
),
917 params_channels(params
), params_rate(params
));
919 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
920 params_access(sparams
), params_format(sparams
),
921 params_channels(sparams
), params_rate(sparams
));
923 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
924 params_channels(params
) / 8;
926 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
927 snd_pcm_oss_plugin_clear(substream
);
929 /* add necessary plugins */
930 snd_pcm_oss_plugin_clear(substream
);
931 if ((err
= snd_pcm_plug_format_plugins(substream
,
934 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err
);
935 snd_pcm_oss_plugin_clear(substream
);
938 if (runtime
->oss
.plugin_first
) {
939 struct snd_pcm_plugin
*plugin
;
940 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
941 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err
);
942 snd_pcm_oss_plugin_clear(substream
);
945 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
946 err
= snd_pcm_plugin_append(plugin
);
948 err
= snd_pcm_plugin_insert(plugin
);
951 snd_pcm_oss_plugin_clear(substream
);
958 err
= snd_pcm_oss_period_size(substream
, params
, sparams
);
962 n
= snd_pcm_plug_slave_size(substream
, runtime
->oss
.period_bytes
/ oss_frame_size
);
963 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, n
, NULL
);
967 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIODS
,
968 runtime
->oss
.periods
, NULL
);
972 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
974 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, sparams
)) < 0) {
975 snd_printd("HW_PARAMS failed: %i\n", err
);
979 memset(sw_params
, 0, sizeof(*sw_params
));
980 if (runtime
->oss
.trigger
) {
981 sw_params
->start_threshold
= 1;
983 sw_params
->start_threshold
= runtime
->boundary
;
985 if (atomic_read(&substream
->mmap_count
) ||
986 substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
987 sw_params
->stop_threshold
= runtime
->boundary
;
989 sw_params
->stop_threshold
= runtime
->buffer_size
;
990 sw_params
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
991 sw_params
->period_step
= 1;
992 sw_params
->avail_min
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
993 1 : runtime
->period_size
;
994 if (atomic_read(&substream
->mmap_count
) ||
995 substream
->oss
.setup
.nosilence
) {
996 sw_params
->silence_threshold
= 0;
997 sw_params
->silence_size
= 0;
999 snd_pcm_uframes_t frames
;
1000 frames
= runtime
->period_size
+ 16;
1001 if (frames
> runtime
->buffer_size
)
1002 frames
= runtime
->buffer_size
;
1003 sw_params
->silence_threshold
= frames
;
1004 sw_params
->silence_size
= frames
;
1007 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_SW_PARAMS
, sw_params
)) < 0) {
1008 snd_printd("SW_PARAMS failed: %i\n", err
);
1012 runtime
->oss
.periods
= params_periods(sparams
);
1013 oss_period_size
= snd_pcm_plug_client_size(substream
, params_period_size(sparams
));
1014 if (oss_period_size
< 0) {
1018 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1019 if (runtime
->oss
.plugin_first
) {
1020 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
1025 oss_period_size
*= oss_frame_size
;
1027 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
1028 if (oss_buffer_size
< 0) {
1033 runtime
->oss
.period_bytes
= oss_period_size
;
1034 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
1036 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1037 runtime
->oss
.period_bytes
,
1038 runtime
->oss
.buffer_bytes
);
1039 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1040 params_period_size(sparams
),
1041 params_buffer_size(sparams
));
1043 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
1044 runtime
->oss
.channels
= params_channels(params
);
1045 runtime
->oss
.rate
= params_rate(params
);
1047 runtime
->oss
.params
= 0;
1048 runtime
->oss
.prepare
= 1;
1049 vfree(runtime
->oss
.buffer
);
1050 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
1051 runtime
->oss
.buffer_used
= 0;
1052 if (runtime
->dma_area
)
1053 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
1055 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
1062 mutex_unlock(&runtime
->oss
.params_lock
);
1066 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file
*pcm_oss_file
, struct snd_pcm_substream
**r_substream
)
1069 struct snd_pcm_substream
*asubstream
= NULL
, *substream
;
1071 for (idx
= 0; idx
< 2; idx
++) {
1072 substream
= pcm_oss_file
->streams
[idx
];
1073 if (substream
== NULL
)
1075 if (asubstream
== NULL
)
1076 asubstream
= substream
;
1077 if (substream
->runtime
->oss
.params
) {
1078 err
= snd_pcm_oss_change_params(substream
);
1086 *r_substream
= asubstream
;
1090 static int snd_pcm_oss_prepare(struct snd_pcm_substream
*substream
)
1093 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1095 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
1097 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1100 runtime
->oss
.prepare
= 0;
1101 runtime
->oss
.prev_hw_ptr_interrupt
= 0;
1102 runtime
->oss
.period_ptr
= 0;
1103 runtime
->oss
.buffer_used
= 0;
1108 static int snd_pcm_oss_make_ready(struct snd_pcm_substream
*substream
)
1110 struct snd_pcm_runtime
*runtime
;
1113 if (substream
== NULL
)
1115 runtime
= substream
->runtime
;
1116 if (runtime
->oss
.params
) {
1117 err
= snd_pcm_oss_change_params(substream
);
1121 if (runtime
->oss
.prepare
) {
1122 err
= snd_pcm_oss_prepare(substream
);
1129 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream
*substream
, snd_pcm_sframes_t
*delay
)
1131 struct snd_pcm_runtime
*runtime
;
1132 snd_pcm_uframes_t frames
;
1136 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
1139 runtime
= substream
->runtime
;
1140 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
1142 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1143 /* until avail(delay) <= buffer_size */
1144 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
1145 frames
/= runtime
->period_size
;
1146 frames
*= runtime
->period_size
;
1147 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
1154 snd_pcm_sframes_t
snd_pcm_oss_write3(struct snd_pcm_substream
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1156 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1159 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1160 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1162 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
1163 printk("pcm_oss: write: recovering from XRUN\n");
1165 printk("pcm_oss: write: recovering from SUSPEND\n");
1167 ret
= snd_pcm_oss_prepare(substream
);
1173 fs
= snd_enter_user();
1174 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
1177 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
1179 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1181 /* test, if we can't store new data, because the stream */
1182 /* has not been started */
1183 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1189 snd_pcm_sframes_t
snd_pcm_oss_read3(struct snd_pcm_substream
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1191 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1192 snd_pcm_sframes_t delay
;
1195 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1196 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1198 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
1199 printk("pcm_oss: read: recovering from XRUN\n");
1201 printk("pcm_oss: read: recovering from SUSPEND\n");
1203 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1206 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1207 ret
= snd_pcm_oss_prepare(substream
);
1211 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1216 fs
= snd_enter_user();
1217 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
1220 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
1222 if (ret
== -EPIPE
) {
1223 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1224 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1230 if (ret
!= -ESTRPIPE
)
1236 snd_pcm_sframes_t
snd_pcm_oss_writev3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
1238 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1241 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1242 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1244 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
1245 printk("pcm_oss: writev: recovering from XRUN\n");
1247 printk("pcm_oss: writev: recovering from SUSPEND\n");
1249 ret
= snd_pcm_oss_prepare(substream
);
1255 fs
= snd_enter_user();
1256 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
1259 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
1261 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1264 /* test, if we can't store new data, because the stream */
1265 /* has not been started */
1266 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1272 snd_pcm_sframes_t
snd_pcm_oss_readv3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
1274 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1277 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1278 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1280 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
1281 printk("pcm_oss: readv: recovering from XRUN\n");
1283 printk("pcm_oss: readv: recovering from SUSPEND\n");
1285 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1288 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1289 ret
= snd_pcm_oss_prepare(substream
);
1295 fs
= snd_enter_user();
1296 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
1299 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
1301 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1307 static ssize_t
snd_pcm_oss_write2(struct snd_pcm_substream
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
1309 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1310 snd_pcm_sframes_t frames
, frames1
;
1311 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1312 if (runtime
->oss
.plugin_first
) {
1313 struct snd_pcm_plugin_channel
*channels
;
1314 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
1316 if (copy_from_user(runtime
->oss
.buffer
, (const char __user
*)buf
, bytes
))
1318 buf
= runtime
->oss
.buffer
;
1320 frames
= bytes
/ oss_frame_bytes
;
1321 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
1324 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
1327 bytes
= frames1
* oss_frame_bytes
;
1331 frames
= bytes_to_frames(runtime
, bytes
);
1332 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
1335 bytes
= frames_to_bytes(runtime
, frames1
);
1340 static ssize_t
snd_pcm_oss_write1(struct snd_pcm_substream
*substream
, const char __user
*buf
, size_t bytes
)
1344 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1346 if (atomic_read(&substream
->mmap_count
))
1349 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
1351 mutex_lock(&runtime
->oss
.params_lock
);
1353 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1355 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
1356 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
1358 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
)) {
1363 runtime
->oss
.buffer_used
+= tmp
;
1367 if (substream
->oss
.setup
.partialfrag
||
1368 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
1369 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
1370 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
1373 runtime
->oss
.bytes
+= tmp
;
1374 runtime
->oss
.period_ptr
+= tmp
;
1375 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
1376 if (runtime
->oss
.period_ptr
== 0 ||
1377 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
1378 runtime
->oss
.buffer_used
= 0;
1379 else if ((substream
->f_flags
& O_NONBLOCK
) != 0) {
1385 tmp
= snd_pcm_oss_write2(substream
,
1386 (const char __force
*)buf
,
1387 runtime
->oss
.period_bytes
, 0);
1390 runtime
->oss
.bytes
+= tmp
;
1394 if ((substream
->f_flags
& O_NONBLOCK
) != 0 &&
1395 tmp
!= runtime
->oss
.period_bytes
)
1399 mutex_unlock(&runtime
->oss
.params_lock
);
1403 mutex_unlock(&runtime
->oss
.params_lock
);
1404 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1407 static ssize_t
snd_pcm_oss_read2(struct snd_pcm_substream
*substream
, char *buf
, size_t bytes
, int in_kernel
)
1409 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1410 snd_pcm_sframes_t frames
, frames1
;
1411 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1412 char __user
*final_dst
= (char __user
*)buf
;
1413 if (runtime
->oss
.plugin_first
) {
1414 struct snd_pcm_plugin_channel
*channels
;
1415 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
1417 buf
= runtime
->oss
.buffer
;
1418 frames
= bytes
/ oss_frame_bytes
;
1419 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
1422 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
1425 bytes
= frames1
* oss_frame_bytes
;
1426 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
1431 frames
= bytes_to_frames(runtime
, bytes
);
1432 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
1435 bytes
= frames_to_bytes(runtime
, frames1
);
1440 static ssize_t
snd_pcm_oss_read1(struct snd_pcm_substream
*substream
, char __user
*buf
, size_t bytes
)
1444 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1446 if (atomic_read(&substream
->mmap_count
))
1449 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
1451 mutex_lock(&runtime
->oss
.params_lock
);
1453 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1454 if (runtime
->oss
.buffer_used
== 0) {
1455 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
1458 runtime
->oss
.bytes
+= tmp
;
1459 runtime
->oss
.period_ptr
= tmp
;
1460 runtime
->oss
.buffer_used
= tmp
;
1463 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
1464 tmp
= runtime
->oss
.buffer_used
;
1465 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
)) {
1472 runtime
->oss
.buffer_used
-= tmp
;
1474 tmp
= snd_pcm_oss_read2(substream
, (char __force
*)buf
,
1475 runtime
->oss
.period_bytes
, 0);
1478 runtime
->oss
.bytes
+= tmp
;
1484 mutex_unlock(&runtime
->oss
.params_lock
);
1488 mutex_unlock(&runtime
->oss
.params_lock
);
1489 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1492 static int snd_pcm_oss_reset(struct snd_pcm_oss_file
*pcm_oss_file
)
1494 struct snd_pcm_substream
*substream
;
1496 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1497 if (substream
!= NULL
) {
1498 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1499 substream
->runtime
->oss
.prepare
= 1;
1501 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1502 if (substream
!= NULL
) {
1503 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1504 substream
->runtime
->oss
.prepare
= 1;
1509 static int snd_pcm_oss_post(struct snd_pcm_oss_file
*pcm_oss_file
)
1511 struct snd_pcm_substream
*substream
;
1514 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1515 if (substream
!= NULL
) {
1516 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1518 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
1520 /* note: all errors from the start action are ignored */
1521 /* OSS apps do not know, how to handle them */
1525 static int snd_pcm_oss_sync1(struct snd_pcm_substream
*substream
, size_t size
)
1527 struct snd_pcm_runtime
*runtime
;
1532 runtime
= substream
->runtime
;
1533 init_waitqueue_entry(&wait
, current
);
1534 add_wait_queue(&runtime
->sleep
, &wait
);
1536 printk("sync1: size = %li\n", size
);
1539 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
1541 runtime
->oss
.buffer_used
= 0;
1545 if (result
!= 0 && result
!= -EAGAIN
)
1548 set_current_state(TASK_INTERRUPTIBLE
);
1549 snd_pcm_stream_lock_irq(substream
);
1550 res
= runtime
->status
->state
;
1551 snd_pcm_stream_unlock_irq(substream
);
1552 if (res
!= SNDRV_PCM_STATE_RUNNING
) {
1553 set_current_state(TASK_RUNNING
);
1556 res
= schedule_timeout(10 * HZ
);
1557 if (signal_pending(current
)) {
1558 result
= -ERESTARTSYS
;
1562 snd_printk(KERN_ERR
"OSS sync error - DMA timeout\n");
1567 remove_wait_queue(&runtime
->sleep
, &wait
);
1571 static int snd_pcm_oss_sync(struct snd_pcm_oss_file
*pcm_oss_file
)
1574 unsigned int saved_f_flags
;
1575 struct snd_pcm_substream
*substream
;
1576 struct snd_pcm_runtime
*runtime
;
1577 snd_pcm_format_t format
;
1578 unsigned long width
;
1581 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1582 if (substream
!= NULL
) {
1583 runtime
= substream
->runtime
;
1584 if (atomic_read(&substream
->mmap_count
))
1586 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1588 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1589 width
= snd_pcm_format_physical_width(format
);
1590 mutex_lock(&runtime
->oss
.params_lock
);
1591 if (runtime
->oss
.buffer_used
> 0) {
1593 printk("sync: buffer_used\n");
1595 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1596 snd_pcm_format_set_silence(format
,
1597 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1599 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1601 mutex_unlock(&runtime
->oss
.params_lock
);
1604 } else if (runtime
->oss
.period_ptr
> 0) {
1606 printk("sync: period_ptr\n");
1608 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1609 snd_pcm_format_set_silence(format
,
1610 runtime
->oss
.buffer
,
1612 err
= snd_pcm_oss_sync1(substream
, size
);
1614 mutex_unlock(&runtime
->oss
.params_lock
);
1619 * The ALSA's period might be a bit large than OSS one.
1620 * Fill the remain portion of ALSA period with zeros.
1622 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1624 size
= runtime
->period_size
- size
;
1625 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
1626 size
= (runtime
->frame_bits
* size
) / 8;
1629 size_t size1
= size
< runtime
->oss
.period_bytes
? size
: runtime
->oss
.period_bytes
;
1632 size1
/= runtime
->sample_bits
;
1633 snd_pcm_format_set_silence(runtime
->format
,
1634 runtime
->oss
.buffer
,
1636 size1
/= runtime
->channels
; /* frames */
1637 fs
= snd_enter_user();
1638 snd_pcm_lib_write(substream
, (void __user
*)runtime
->oss
.buffer
, size1
);
1641 } else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
1642 void __user
*buffers
[runtime
->channels
];
1643 memset(buffers
, 0, runtime
->channels
* sizeof(void *));
1644 snd_pcm_lib_writev(substream
, buffers
, size
);
1647 mutex_unlock(&runtime
->oss
.params_lock
);
1649 * finish sync: drain the buffer
1652 saved_f_flags
= substream
->f_flags
;
1653 substream
->f_flags
&= ~O_NONBLOCK
;
1654 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1655 substream
->f_flags
= saved_f_flags
;
1658 runtime
->oss
.prepare
= 1;
1661 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1662 if (substream
!= NULL
) {
1663 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1665 runtime
= substream
->runtime
;
1666 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1669 runtime
->oss
.buffer_used
= 0;
1670 runtime
->oss
.prepare
= 1;
1675 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file
*pcm_oss_file
, int rate
)
1679 for (idx
= 1; idx
>= 0; --idx
) {
1680 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1681 struct snd_pcm_runtime
*runtime
;
1682 if (substream
== NULL
)
1684 runtime
= substream
->runtime
;
1687 else if (rate
> 192000)
1689 if (runtime
->oss
.rate
!= rate
) {
1690 runtime
->oss
.params
= 1;
1691 runtime
->oss
.rate
= rate
;
1694 return snd_pcm_oss_get_rate(pcm_oss_file
);
1697 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
)
1699 struct snd_pcm_substream
*substream
;
1702 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1704 return substream
->runtime
->oss
.rate
;
1707 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int channels
)
1714 for (idx
= 1; idx
>= 0; --idx
) {
1715 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1716 struct snd_pcm_runtime
*runtime
;
1717 if (substream
== NULL
)
1719 runtime
= substream
->runtime
;
1720 if (runtime
->oss
.channels
!= channels
) {
1721 runtime
->oss
.params
= 1;
1722 runtime
->oss
.channels
= channels
;
1725 return snd_pcm_oss_get_channels(pcm_oss_file
);
1728 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
)
1730 struct snd_pcm_substream
*substream
;
1733 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1735 return substream
->runtime
->oss
.channels
;
1738 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file
*pcm_oss_file
)
1740 struct snd_pcm_substream
*substream
;
1743 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1745 return substream
->runtime
->oss
.period_bytes
;
1748 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file
*pcm_oss_file
)
1750 struct snd_pcm_substream
*substream
;
1753 struct snd_pcm_hw_params
*params
;
1754 unsigned int formats
= 0;
1755 struct snd_mask format_mask
;
1758 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1760 if (atomic_read(&substream
->mmap_count
))
1763 direct
= substream
->oss
.setup
.direct
;
1765 return AFMT_MU_LAW
| AFMT_U8
|
1766 AFMT_S16_LE
| AFMT_S16_BE
|
1767 AFMT_S8
| AFMT_U16_LE
|
1769 AFMT_S32_LE
| AFMT_S32_BE
|
1770 AFMT_S24_LE
| AFMT_S24_LE
|
1772 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1775 _snd_pcm_hw_params_any(params
);
1776 err
= snd_pcm_hw_refine(substream
, params
);
1777 format_mask
= *hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1781 for (fmt
= 0; fmt
< 32; ++fmt
) {
1782 if (snd_mask_test(&format_mask
, fmt
)) {
1783 int f
= snd_pcm_oss_format_to(fmt
);
1791 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file
*pcm_oss_file
, int format
)
1795 if (format
!= AFMT_QUERY
) {
1796 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1799 if (!(formats
& format
))
1801 for (idx
= 1; idx
>= 0; --idx
) {
1802 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1803 struct snd_pcm_runtime
*runtime
;
1804 if (substream
== NULL
)
1806 runtime
= substream
->runtime
;
1807 if (runtime
->oss
.format
!= format
) {
1808 runtime
->oss
.params
= 1;
1809 runtime
->oss
.format
= format
;
1813 return snd_pcm_oss_get_format(pcm_oss_file
);
1816 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
)
1818 struct snd_pcm_substream
*substream
;
1821 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1823 return substream
->runtime
->oss
.format
;
1826 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream
*substream
, int subdivide
)
1828 struct snd_pcm_runtime
*runtime
;
1830 if (substream
== NULL
)
1832 runtime
= substream
->runtime
;
1833 if (subdivide
== 0) {
1834 subdivide
= runtime
->oss
.subdivision
;
1839 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1841 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1842 subdivide
!= 8 && subdivide
!= 16)
1844 runtime
->oss
.subdivision
= subdivide
;
1845 runtime
->oss
.params
= 1;
1849 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file
*pcm_oss_file
, int subdivide
)
1851 int err
= -EINVAL
, idx
;
1853 for (idx
= 1; idx
>= 0; --idx
) {
1854 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1855 if (substream
== NULL
)
1857 if ((err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
)) < 0)
1863 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream
*substream
, unsigned int val
)
1865 struct snd_pcm_runtime
*runtime
;
1867 if (substream
== NULL
)
1869 runtime
= substream
->runtime
;
1870 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1872 runtime
->oss
.fragshift
= val
& 0xffff;
1873 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
1874 if (runtime
->oss
.fragshift
< 4) /* < 16 */
1875 runtime
->oss
.fragshift
= 4;
1876 if (runtime
->oss
.maxfrags
< 2)
1877 runtime
->oss
.maxfrags
= 2;
1878 runtime
->oss
.params
= 1;
1882 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int val
)
1884 int err
= -EINVAL
, idx
;
1886 for (idx
= 1; idx
>= 0; --idx
) {
1887 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1888 if (substream
== NULL
)
1890 if ((err
= snd_pcm_oss_set_fragment1(substream
, val
)) < 0)
1896 static int snd_pcm_oss_nonblock(struct file
* file
)
1898 file
->f_flags
|= O_NONBLOCK
;
1902 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream
*substream
, int res
)
1905 if (substream
== NULL
) {
1906 res
&= ~DSP_CAP_DUPLEX
;
1909 #ifdef DSP_CAP_MULTI
1910 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1911 if (substream
->pstr
->substream_count
> 1)
1912 res
|= DSP_CAP_MULTI
;
1914 /* DSP_CAP_REALTIME is set all times: */
1915 /* all ALSA drivers can return actual pointer in ring buffer */
1916 #if defined(DSP_CAP_REALTIME) && 0
1918 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1919 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
1920 res
&= ~DSP_CAP_REALTIME
;
1926 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file
*pcm_oss_file
)
1930 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
1931 for (idx
= 0; idx
< 2; idx
++) {
1932 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1933 result
= snd_pcm_oss_get_caps1(substream
, result
);
1935 result
|= 0x0001; /* revision - same as SB AWE 64 */
1939 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream
*substream
, snd_pcm_uframes_t hw_ptr
)
1941 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1942 snd_pcm_uframes_t appl_ptr
;
1943 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
1944 appl_ptr
%= runtime
->boundary
;
1945 runtime
->control
->appl_ptr
= appl_ptr
;
1948 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file
*pcm_oss_file
, int trigger
)
1950 struct snd_pcm_runtime
*runtime
;
1951 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1955 printk("pcm_oss: trigger = 0x%x\n", trigger
);
1958 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1959 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1962 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
1966 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
1970 runtime
= psubstream
->runtime
;
1971 if (trigger
& PCM_ENABLE_OUTPUT
) {
1972 if (runtime
->oss
.trigger
)
1974 if (atomic_read(&psubstream
->mmap_count
))
1975 snd_pcm_oss_simulate_fill(psubstream
, runtime
->hw_ptr_interrupt
);
1976 runtime
->oss
.trigger
= 1;
1977 runtime
->start_threshold
= 1;
1978 cmd
= SNDRV_PCM_IOCTL_START
;
1980 if (!runtime
->oss
.trigger
)
1982 runtime
->oss
.trigger
= 0;
1983 runtime
->start_threshold
= runtime
->boundary
;
1984 cmd
= SNDRV_PCM_IOCTL_DROP
;
1985 runtime
->oss
.prepare
= 1;
1987 err
= snd_pcm_kernel_ioctl(psubstream
, cmd
, NULL
);
1993 runtime
= csubstream
->runtime
;
1994 if (trigger
& PCM_ENABLE_INPUT
) {
1995 if (runtime
->oss
.trigger
)
1997 runtime
->oss
.trigger
= 1;
1998 runtime
->start_threshold
= 1;
1999 cmd
= SNDRV_PCM_IOCTL_START
;
2001 if (!runtime
->oss
.trigger
)
2003 runtime
->oss
.trigger
= 0;
2004 runtime
->start_threshold
= runtime
->boundary
;
2005 cmd
= SNDRV_PCM_IOCTL_DROP
;
2006 runtime
->oss
.prepare
= 1;
2008 err
= snd_pcm_kernel_ioctl(csubstream
, cmd
, NULL
);
2016 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file
*pcm_oss_file
)
2018 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2021 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2022 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2023 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
2024 result
|= PCM_ENABLE_OUTPUT
;
2025 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
2026 result
|= PCM_ENABLE_INPUT
;
2030 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file
*pcm_oss_file
)
2032 struct snd_pcm_substream
*substream
;
2033 struct snd_pcm_runtime
*runtime
;
2034 snd_pcm_sframes_t delay
;
2037 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2038 if (substream
== NULL
)
2040 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2042 runtime
= substream
->runtime
;
2043 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
2045 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2047 delay
= 0; /* hack for broken OSS applications */
2050 return snd_pcm_oss_bytes(substream
, delay
);
2053 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
2055 struct snd_pcm_substream
*substream
;
2056 struct snd_pcm_runtime
*runtime
;
2057 snd_pcm_sframes_t delay
;
2059 struct count_info info
;
2064 substream
= pcm_oss_file
->streams
[stream
];
2065 if (substream
== NULL
)
2067 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2069 runtime
= substream
->runtime
;
2070 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
2071 memset(&info
, 0, sizeof(info
));
2072 if (copy_to_user(_info
, &info
, sizeof(info
)))
2076 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2077 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2078 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
2083 fixup
= runtime
->oss
.buffer_used
;
2086 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
2087 fixup
= -runtime
->oss
.buffer_used
;
2091 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
2092 if (atomic_read(&substream
->mmap_count
)) {
2093 snd_pcm_sframes_t n
;
2094 n
= (delay
= runtime
->hw_ptr_interrupt
) - runtime
->oss
.prev_hw_ptr_interrupt
;
2096 n
+= runtime
->boundary
;
2097 info
.blocks
= n
/ runtime
->period_size
;
2098 runtime
->oss
.prev_hw_ptr_interrupt
= delay
;
2099 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2100 snd_pcm_oss_simulate_fill(substream
, delay
);
2101 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
2103 delay
= snd_pcm_oss_bytes(substream
, delay
);
2104 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2105 if (substream
->oss
.setup
.buggyptr
)
2106 info
.blocks
= (runtime
->oss
.buffer_bytes
- delay
- fixup
) / runtime
->oss
.period_bytes
;
2108 info
.blocks
= (delay
+ fixup
) / runtime
->oss
.period_bytes
;
2109 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
2112 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
2113 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
2116 if (copy_to_user(_info
, &info
, sizeof(info
)))
2121 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
2123 struct snd_pcm_substream
*substream
;
2124 struct snd_pcm_runtime
*runtime
;
2125 snd_pcm_sframes_t avail
;
2127 struct audio_buf_info info
;
2132 substream
= pcm_oss_file
->streams
[stream
];
2133 if (substream
== NULL
)
2135 runtime
= substream
->runtime
;
2137 if (runtime
->oss
.params
&&
2138 (err
= snd_pcm_oss_change_params(substream
)) < 0)
2141 info
.fragsize
= runtime
->oss
.period_bytes
;
2142 info
.fragstotal
= runtime
->periods
;
2143 if (runtime
->oss
.prepare
) {
2144 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2145 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
2146 info
.fragments
= runtime
->oss
.periods
;
2152 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2153 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
2154 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
2155 avail
= runtime
->buffer_size
;
2159 avail
= runtime
->buffer_size
- avail
;
2160 fixup
= -runtime
->oss
.buffer_used
;
2163 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
2164 fixup
= runtime
->oss
.buffer_used
;
2168 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
2169 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
2173 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
2175 if (copy_to_user(_info
, &info
, sizeof(info
)))
2180 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
2182 // it won't be probably implemented
2183 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2187 static const char *strip_task_path(const char *path
)
2189 const char *ptr
, *ptrl
= NULL
;
2190 for (ptr
= path
; *ptr
; ptr
++) {
2197 static void snd_pcm_oss_look_for_setup(struct snd_pcm
*pcm
, int stream
,
2198 const char *task_name
,
2199 struct snd_pcm_oss_setup
*rsetup
)
2201 struct snd_pcm_oss_setup
*setup
;
2203 mutex_lock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2205 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
;
2206 setup
= setup
->next
) {
2207 if (!strcmp(setup
->task_name
, task_name
))
2210 } while ((task_name
= strip_task_path(task_name
)) != NULL
);
2214 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2217 static void snd_pcm_oss_release_substream(struct snd_pcm_substream
*substream
)
2219 struct snd_pcm_runtime
*runtime
;
2220 runtime
= substream
->runtime
;
2221 vfree(runtime
->oss
.buffer
);
2222 runtime
->oss
.buffer
= NULL
;
2223 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2224 snd_pcm_oss_plugin_clear(substream
);
2226 substream
->oss
.oss
= 0;
2229 static void snd_pcm_oss_init_substream(struct snd_pcm_substream
*substream
,
2230 struct snd_pcm_oss_setup
*setup
,
2233 struct snd_pcm_runtime
*runtime
;
2235 substream
->oss
.oss
= 1;
2236 substream
->oss
.setup
= *setup
;
2237 if (setup
->nonblock
)
2238 substream
->f_flags
|= O_NONBLOCK
;
2239 else if (setup
->block
)
2240 substream
->f_flags
&= ~O_NONBLOCK
;
2241 runtime
= substream
->runtime
;
2242 runtime
->oss
.params
= 1;
2243 runtime
->oss
.trigger
= 1;
2244 runtime
->oss
.rate
= 8000;
2245 mutex_init(&runtime
->oss
.params_lock
);
2246 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
2247 case SNDRV_MINOR_OSS_PCM_8
:
2248 runtime
->oss
.format
= AFMT_U8
;
2250 case SNDRV_MINOR_OSS_PCM_16
:
2251 runtime
->oss
.format
= AFMT_S16_LE
;
2254 runtime
->oss
.format
= AFMT_MU_LAW
;
2256 runtime
->oss
.channels
= 1;
2257 runtime
->oss
.fragshift
= 0;
2258 runtime
->oss
.maxfrags
= 0;
2259 runtime
->oss
.subdivision
= 0;
2260 substream
->pcm_release
= snd_pcm_oss_release_substream
;
2263 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file
*pcm_oss_file
)
2268 for (cidx
= 0; cidx
< 2; ++cidx
) {
2269 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[cidx
];
2271 snd_pcm_release_substream(substream
);
2273 kfree(pcm_oss_file
);
2277 static int snd_pcm_oss_open_file(struct file
*file
,
2278 struct snd_pcm
*pcm
,
2279 struct snd_pcm_oss_file
**rpcm_oss_file
,
2281 struct snd_pcm_oss_setup
*setup
)
2284 struct snd_pcm_oss_file
*pcm_oss_file
;
2285 struct snd_pcm_substream
*substream
;
2286 unsigned int f_mode
= file
->f_mode
;
2289 *rpcm_oss_file
= NULL
;
2291 pcm_oss_file
= kzalloc(sizeof(*pcm_oss_file
), GFP_KERNEL
);
2292 if (pcm_oss_file
== NULL
)
2295 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
2296 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
2297 f_mode
= FMODE_WRITE
;
2299 file
->f_flags
&= ~O_APPEND
;
2300 for (idx
= 0; idx
< 2; idx
++) {
2301 if (setup
[idx
].disable
)
2303 if (! pcm
->streams
[idx
].substream_count
)
2304 continue; /* no matching substream */
2305 if (idx
== SNDRV_PCM_STREAM_PLAYBACK
) {
2306 if (! (f_mode
& FMODE_WRITE
))
2309 if (! (f_mode
& FMODE_READ
))
2312 err
= snd_pcm_open_substream(pcm
, idx
, file
, &substream
);
2314 snd_pcm_oss_release_file(pcm_oss_file
);
2318 pcm_oss_file
->streams
[idx
] = substream
;
2319 substream
->file
= pcm_oss_file
;
2320 snd_pcm_oss_init_substream(substream
, &setup
[idx
], minor
);
2323 if (!pcm_oss_file
->streams
[0] && !pcm_oss_file
->streams
[1]) {
2324 snd_pcm_oss_release_file(pcm_oss_file
);
2328 file
->private_data
= pcm_oss_file
;
2330 *rpcm_oss_file
= pcm_oss_file
;
2335 static int snd_task_name(struct task_struct
*task
, char *name
, size_t size
)
2339 if (snd_BUG_ON(!task
|| !name
|| size
< 2))
2341 for (idx
= 0; idx
< sizeof(task
->comm
) && idx
+ 1 < size
; idx
++)
2342 name
[idx
] = task
->comm
[idx
];
2347 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
2351 struct snd_pcm
*pcm
;
2352 struct snd_pcm_oss_file
*pcm_oss_file
;
2353 struct snd_pcm_oss_setup setup
[2];
2357 pcm
= snd_lookup_oss_minor_data(iminor(inode
),
2358 SNDRV_OSS_DEVICE_TYPE_PCM
);
2363 err
= snd_card_file_add(pcm
->card
, file
);
2366 if (!try_module_get(pcm
->card
->module
)) {
2370 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
2374 memset(setup
, 0, sizeof(setup
));
2375 if (file
->f_mode
& FMODE_WRITE
)
2376 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
2377 task_name
, &setup
[0]);
2378 if (file
->f_mode
& FMODE_READ
)
2379 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
2380 task_name
, &setup
[1]);
2382 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
2384 nonblock
= nonblock_open
;
2386 init_waitqueue_entry(&wait
, current
);
2387 add_wait_queue(&pcm
->open_wait
, &wait
);
2388 mutex_lock(&pcm
->open_mutex
);
2390 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
2391 iminor(inode
), setup
);
2394 if (err
== -EAGAIN
) {
2401 set_current_state(TASK_INTERRUPTIBLE
);
2402 mutex_unlock(&pcm
->open_mutex
);
2404 mutex_lock(&pcm
->open_mutex
);
2405 if (signal_pending(current
)) {
2410 remove_wait_queue(&pcm
->open_wait
, &wait
);
2411 mutex_unlock(&pcm
->open_mutex
);
2417 module_put(pcm
->card
->module
);
2419 snd_card_file_remove(pcm
->card
, file
);
2424 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
2426 struct snd_pcm
*pcm
;
2427 struct snd_pcm_substream
*substream
;
2428 struct snd_pcm_oss_file
*pcm_oss_file
;
2430 pcm_oss_file
= file
->private_data
;
2431 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2432 if (substream
== NULL
)
2433 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2434 if (snd_BUG_ON(!substream
))
2436 pcm
= substream
->pcm
;
2437 if (!pcm
->card
->shutdown
)
2438 snd_pcm_oss_sync(pcm_oss_file
);
2439 mutex_lock(&pcm
->open_mutex
);
2440 snd_pcm_oss_release_file(pcm_oss_file
);
2441 mutex_unlock(&pcm
->open_mutex
);
2442 wake_up(&pcm
->open_wait
);
2443 module_put(pcm
->card
->module
);
2444 snd_card_file_remove(pcm
->card
, file
);
2448 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2450 struct snd_pcm_oss_file
*pcm_oss_file
;
2451 int __user
*p
= (int __user
*)arg
;
2454 pcm_oss_file
= file
->private_data
;
2455 if (cmd
== OSS_GETVERSION
)
2456 return put_user(SNDRV_OSS_VERSION
, p
);
2457 if (cmd
== OSS_ALSAEMULVER
)
2458 return put_user(1, p
);
2459 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2460 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2461 struct snd_pcm_substream
*substream
;
2463 for (idx
= 0; idx
< 2; ++idx
) {
2464 substream
= pcm_oss_file
->streams
[idx
];
2465 if (substream
!= NULL
)
2468 if (snd_BUG_ON(idx
>= 2))
2470 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
2473 if (((cmd
>> 8) & 0xff) != 'P')
2476 printk("pcm_oss: ioctl = 0x%x\n", cmd
);
2479 case SNDCTL_DSP_RESET
:
2480 return snd_pcm_oss_reset(pcm_oss_file
);
2481 case SNDCTL_DSP_SYNC
:
2482 return snd_pcm_oss_sync(pcm_oss_file
);
2483 case SNDCTL_DSP_SPEED
:
2484 if (get_user(res
, p
))
2486 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
2488 return put_user(res
, p
);
2489 case SOUND_PCM_READ_RATE
:
2490 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
2493 return put_user(res
, p
);
2494 case SNDCTL_DSP_STEREO
:
2495 if (get_user(res
, p
))
2497 res
= res
> 0 ? 2 : 1;
2498 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
2500 return put_user(--res
, p
);
2501 case SNDCTL_DSP_GETBLKSIZE
:
2502 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
2505 return put_user(res
, p
);
2506 case SNDCTL_DSP_SETFMT
:
2507 if (get_user(res
, p
))
2509 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
2512 return put_user(res
, p
);
2513 case SOUND_PCM_READ_BITS
:
2514 res
= snd_pcm_oss_get_format(pcm_oss_file
);
2517 return put_user(res
, p
);
2518 case SNDCTL_DSP_CHANNELS
:
2519 if (get_user(res
, p
))
2521 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
2524 return put_user(res
, p
);
2525 case SOUND_PCM_READ_CHANNELS
:
2526 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
2529 return put_user(res
, p
);
2530 case SOUND_PCM_WRITE_FILTER
:
2531 case SOUND_PCM_READ_FILTER
:
2533 case SNDCTL_DSP_POST
:
2534 return snd_pcm_oss_post(pcm_oss_file
);
2535 case SNDCTL_DSP_SUBDIVIDE
:
2536 if (get_user(res
, p
))
2538 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
2541 return put_user(res
, p
);
2542 case SNDCTL_DSP_SETFRAGMENT
:
2543 if (get_user(res
, p
))
2545 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
2546 case SNDCTL_DSP_GETFMTS
:
2547 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
2550 return put_user(res
, p
);
2551 case SNDCTL_DSP_GETOSPACE
:
2552 case SNDCTL_DSP_GETISPACE
:
2553 return snd_pcm_oss_get_space(pcm_oss_file
,
2554 cmd
== SNDCTL_DSP_GETISPACE
?
2555 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2556 (struct audio_buf_info __user
*) arg
);
2557 case SNDCTL_DSP_NONBLOCK
:
2558 return snd_pcm_oss_nonblock(file
);
2559 case SNDCTL_DSP_GETCAPS
:
2560 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
2563 return put_user(res
, p
);
2564 case SNDCTL_DSP_GETTRIGGER
:
2565 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2568 return put_user(res
, p
);
2569 case SNDCTL_DSP_SETTRIGGER
:
2570 if (get_user(res
, p
))
2572 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2573 case SNDCTL_DSP_GETIPTR
:
2574 case SNDCTL_DSP_GETOPTR
:
2575 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2576 cmd
== SNDCTL_DSP_GETIPTR
?
2577 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2578 (struct count_info __user
*) arg
);
2579 case SNDCTL_DSP_MAPINBUF
:
2580 case SNDCTL_DSP_MAPOUTBUF
:
2581 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2582 cmd
== SNDCTL_DSP_MAPINBUF
?
2583 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2584 (struct buffmem_desc __user
*) arg
);
2585 case SNDCTL_DSP_SETSYNCRO
:
2586 /* stop DMA now.. */
2588 case SNDCTL_DSP_SETDUPLEX
:
2589 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2592 case SNDCTL_DSP_GETODELAY
:
2593 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2595 /* it's for sure, some broken apps don't check for error codes */
2599 return put_user(res
, p
);
2600 case SNDCTL_DSP_PROFILE
:
2601 return 0; /* silently ignore */
2603 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd
);
2608 #ifdef CONFIG_COMPAT
2609 /* all compatible */
2610 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2612 #define snd_pcm_oss_ioctl_compat NULL
2615 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2617 struct snd_pcm_oss_file
*pcm_oss_file
;
2618 struct snd_pcm_substream
*substream
;
2620 pcm_oss_file
= file
->private_data
;
2621 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2622 if (substream
== NULL
)
2624 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2626 return snd_pcm_oss_read1(substream
, buf
, count
);
2629 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2630 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count
, (long)res
);
2636 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2638 struct snd_pcm_oss_file
*pcm_oss_file
;
2639 struct snd_pcm_substream
*substream
;
2642 pcm_oss_file
= file
->private_data
;
2643 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2644 if (substream
== NULL
)
2646 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2647 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2649 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count
, (long)result
);
2654 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream
*substream
)
2656 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2657 if (atomic_read(&substream
->mmap_count
))
2658 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2660 return snd_pcm_playback_avail(runtime
) >= runtime
->oss
.period_frames
;
2663 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream
*substream
)
2665 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2666 if (atomic_read(&substream
->mmap_count
))
2667 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2669 return snd_pcm_capture_avail(runtime
) >= runtime
->oss
.period_frames
;
2672 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2674 struct snd_pcm_oss_file
*pcm_oss_file
;
2676 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2678 pcm_oss_file
= file
->private_data
;
2680 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2681 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2684 if (psubstream
!= NULL
) {
2685 struct snd_pcm_runtime
*runtime
= psubstream
->runtime
;
2686 poll_wait(file
, &runtime
->sleep
, wait
);
2687 snd_pcm_stream_lock_irq(psubstream
);
2688 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2689 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2690 snd_pcm_oss_playback_ready(psubstream
)))
2691 mask
|= POLLOUT
| POLLWRNORM
;
2692 snd_pcm_stream_unlock_irq(psubstream
);
2694 if (csubstream
!= NULL
) {
2695 struct snd_pcm_runtime
*runtime
= csubstream
->runtime
;
2696 snd_pcm_state_t ostate
;
2697 poll_wait(file
, &runtime
->sleep
, wait
);
2698 snd_pcm_stream_lock_irq(csubstream
);
2699 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2700 snd_pcm_oss_capture_ready(csubstream
))
2701 mask
|= POLLIN
| POLLRDNORM
;
2702 snd_pcm_stream_unlock_irq(csubstream
);
2703 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2704 struct snd_pcm_oss_file ofile
;
2705 memset(&ofile
, 0, sizeof(ofile
));
2706 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2707 runtime
->oss
.trigger
= 0;
2708 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2715 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2717 struct snd_pcm_oss_file
*pcm_oss_file
;
2718 struct snd_pcm_substream
*substream
= NULL
;
2719 struct snd_pcm_runtime
*runtime
;
2723 printk("pcm_oss: mmap begin\n");
2725 pcm_oss_file
= file
->private_data
;
2726 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2727 case VM_READ
| VM_WRITE
:
2728 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2733 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2736 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2741 /* set VM_READ access as well to fix memset() routines that do
2742 reads before writes (to improve performance) */
2743 area
->vm_flags
|= VM_READ
;
2744 if (substream
== NULL
)
2746 runtime
= substream
->runtime
;
2747 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2749 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2750 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2754 if (runtime
->oss
.params
) {
2755 if ((err
= snd_pcm_oss_change_params(substream
)) < 0)
2758 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2759 if (runtime
->oss
.plugin_first
!= NULL
)
2763 if (area
->vm_pgoff
!= 0)
2766 err
= snd_pcm_mmap_data(substream
, file
, area
);
2769 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2770 runtime
->silence_threshold
= 0;
2771 runtime
->silence_size
= 0;
2773 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime
->oss
.mmap_bytes
);
2775 /* In mmap mode we never stop */
2776 runtime
->stop_threshold
= runtime
->boundary
;
2781 #ifdef CONFIG_SND_VERBOSE_PROCFS
2786 static void snd_pcm_oss_proc_read(struct snd_info_entry
*entry
,
2787 struct snd_info_buffer
*buffer
)
2789 struct snd_pcm_str
*pstr
= entry
->private_data
;
2790 struct snd_pcm_oss_setup
*setup
= pstr
->oss
.setup_list
;
2791 mutex_lock(&pstr
->oss
.setup_mutex
);
2793 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2797 setup
->disable
? " disable" : "",
2798 setup
->direct
? " direct" : "",
2799 setup
->block
? " block" : "",
2800 setup
->nonblock
? " non-block" : "",
2801 setup
->partialfrag
? " partial-frag" : "",
2802 setup
->nosilence
? " no-silence" : "");
2803 setup
= setup
->next
;
2805 mutex_unlock(&pstr
->oss
.setup_mutex
);
2808 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str
* pstr
)
2810 struct snd_pcm_oss_setup
*setup
, *setupn
;
2812 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
2813 setup
; setup
= setupn
) {
2814 setupn
= setup
->next
;
2815 kfree(setup
->task_name
);
2818 pstr
->oss
.setup_list
= NULL
;
2821 static void snd_pcm_oss_proc_write(struct snd_info_entry
*entry
,
2822 struct snd_info_buffer
*buffer
)
2824 struct snd_pcm_str
*pstr
= entry
->private_data
;
2825 char line
[128], str
[32], task_name
[32], *ptr
;
2827 struct snd_pcm_oss_setup
*setup
, *setup1
, template;
2829 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
2830 mutex_lock(&pstr
->oss
.setup_mutex
);
2831 memset(&template, 0, sizeof(template));
2832 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
2833 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
2834 snd_pcm_oss_proc_free_setup_list(pstr
);
2835 mutex_unlock(&pstr
->oss
.setup_mutex
);
2838 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
2839 if (!strcmp(setup
->task_name
, task_name
)) {
2844 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2845 template.periods
= simple_strtoul(str
, NULL
, 10);
2846 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2847 template.period_size
= simple_strtoul(str
, NULL
, 10);
2848 for (idx1
= 31; idx1
>= 0; idx1
--)
2849 if (template.period_size
& (1 << idx1
))
2851 for (idx1
--; idx1
>= 0; idx1
--)
2852 template.period_size
&= ~(1 << idx1
);
2854 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2855 if (!strcmp(str
, "disable")) {
2856 template.disable
= 1;
2857 } else if (!strcmp(str
, "direct")) {
2858 template.direct
= 1;
2859 } else if (!strcmp(str
, "block")) {
2861 } else if (!strcmp(str
, "non-block")) {
2862 template.nonblock
= 1;
2863 } else if (!strcmp(str
, "partial-frag")) {
2864 template.partialfrag
= 1;
2865 } else if (!strcmp(str
, "no-silence")) {
2866 template.nosilence
= 1;
2867 } else if (!strcmp(str
, "buggy-ptr")) {
2868 template.buggyptr
= 1;
2871 if (setup
== NULL
) {
2872 setup
= kmalloc(sizeof(*setup
), GFP_KERNEL
);
2874 buffer
->error
= -ENOMEM
;
2875 mutex_lock(&pstr
->oss
.setup_mutex
);
2878 if (pstr
->oss
.setup_list
== NULL
)
2879 pstr
->oss
.setup_list
= setup
;
2881 for (setup1
= pstr
->oss
.setup_list
;
2882 setup1
->next
; setup1
= setup1
->next
);
2883 setup1
->next
= setup
;
2885 template.task_name
= kstrdup(task_name
, GFP_KERNEL
);
2886 if (! template.task_name
) {
2888 buffer
->error
= -ENOMEM
;
2889 mutex_lock(&pstr
->oss
.setup_mutex
);
2894 mutex_unlock(&pstr
->oss
.setup_mutex
);
2898 static void snd_pcm_oss_proc_init(struct snd_pcm
*pcm
)
2901 for (stream
= 0; stream
< 2; ++stream
) {
2902 struct snd_info_entry
*entry
;
2903 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2904 if (pstr
->substream_count
== 0)
2906 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
2907 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
2908 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2909 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
2910 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
2911 entry
->private_data
= pstr
;
2912 if (snd_info_register(entry
) < 0) {
2913 snd_info_free_entry(entry
);
2917 pstr
->oss
.proc_entry
= entry
;
2921 static void snd_pcm_oss_proc_done(struct snd_pcm
*pcm
)
2924 for (stream
= 0; stream
< 2; ++stream
) {
2925 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2926 snd_info_free_entry(pstr
->oss
.proc_entry
);
2927 pstr
->oss
.proc_entry
= NULL
;
2928 snd_pcm_oss_proc_free_setup_list(pstr
);
2931 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2932 #define snd_pcm_oss_proc_init(pcm)
2933 #define snd_pcm_oss_proc_done(pcm)
2934 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2940 static const struct file_operations snd_pcm_oss_f_reg
=
2942 .owner
= THIS_MODULE
,
2943 .read
= snd_pcm_oss_read
,
2944 .write
= snd_pcm_oss_write
,
2945 .open
= snd_pcm_oss_open
,
2946 .release
= snd_pcm_oss_release
,
2947 .poll
= snd_pcm_oss_poll
,
2948 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
2949 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
2950 .mmap
= snd_pcm_oss_mmap
,
2953 static void register_oss_dsp(struct snd_pcm
*pcm
, int index
)
2956 sprintf(name
, "dsp%i%i", pcm
->card
->number
, pcm
->device
);
2957 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2958 pcm
->card
, index
, &snd_pcm_oss_f_reg
,
2960 snd_printk(KERN_ERR
"unable to register OSS PCM device %i:%i\n",
2961 pcm
->card
->number
, pcm
->device
);
2965 static int snd_pcm_oss_register_minor(struct snd_pcm
*pcm
)
2968 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2971 register_oss_dsp(pcm
, 0);
2972 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
2973 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
2974 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
2975 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
2976 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2977 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
2982 pcm
->oss
.reg_mask
|= 1;
2984 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2985 register_oss_dsp(pcm
, 1);
2987 pcm
->oss
.reg_mask
|= 2;
2991 snd_pcm_oss_proc_init(pcm
);
2996 static int snd_pcm_oss_disconnect_minor(struct snd_pcm
*pcm
)
2999 if (pcm
->oss
.reg_mask
& 1) {
3000 pcm
->oss
.reg_mask
&= ~1;
3001 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3004 if (pcm
->oss
.reg_mask
& 2) {
3005 pcm
->oss
.reg_mask
&= ~2;
3006 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3009 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
3010 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3011 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
3019 static int snd_pcm_oss_unregister_minor(struct snd_pcm
*pcm
)
3021 snd_pcm_oss_disconnect_minor(pcm
);
3022 snd_pcm_oss_proc_done(pcm
);
3026 static struct snd_pcm_notify snd_pcm_oss_notify
=
3028 .n_register
= snd_pcm_oss_register_minor
,
3029 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
3030 .n_unregister
= snd_pcm_oss_unregister_minor
,
3033 static int __init
alsa_pcm_oss_init(void)
3038 /* check device map table */
3039 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
3040 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3041 snd_printk(KERN_ERR
"invalid dsp_map[%d] = %d\n",
3045 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3046 snd_printk(KERN_ERR
"invalid adsp_map[%d] = %d\n",
3051 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
3056 static void __exit
alsa_pcm_oss_exit(void)
3058 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
3061 module_init(alsa_pcm_oss_init
)
3062 module_exit(alsa_pcm_oss_exit
)