2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/time.h>
32 #include <linux/vmalloc.h>
33 #include <linux/module.h>
34 #include <linux/math64.h>
35 #include <linux/string.h>
36 #include <linux/compat.h>
37 #include <sound/core.h>
38 #include <sound/minors.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include "pcm_plugin.h"
42 #include <sound/info.h>
43 #include <linux/soundcard.h>
44 #include <sound/initval.h>
45 #include <sound/mixer_oss.h>
47 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
49 static int dsp_map
[SNDRV_CARDS
];
50 static int adsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
51 static bool nonblock_open
= 1;
53 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
54 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
55 MODULE_LICENSE("GPL");
56 module_param_array(dsp_map
, int, NULL
, 0444);
57 MODULE_PARM_DESC(dsp_map
, "PCM device number assigned to 1st OSS device.");
58 module_param_array(adsp_map
, int, NULL
, 0444);
59 MODULE_PARM_DESC(adsp_map
, "PCM device number assigned to 2nd OSS device.");
60 module_param(nonblock_open
, bool, 0644);
61 MODULE_PARM_DESC(nonblock_open
, "Don't block opening busy PCM devices.");
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM
);
63 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1
);
65 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
);
66 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
);
67 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
);
69 static inline mm_segment_t
snd_enter_user(void)
71 mm_segment_t fs
= get_fs();
76 static inline void snd_leave_user(mm_segment_t fs
)
82 * helper functions to process hw_params
84 static int snd_interval_refine_min(struct snd_interval
*i
, unsigned int min
, int openmin
)
91 } else if (i
->min
== min
&& !i
->openmin
&& openmin
) {
101 if (snd_interval_checkempty(i
)) {
102 snd_interval_none(i
);
108 static int snd_interval_refine_max(struct snd_interval
*i
, unsigned int max
, int openmax
)
113 i
->openmax
= openmax
;
115 } else if (i
->max
== max
&& !i
->openmax
&& openmax
) {
125 if (snd_interval_checkempty(i
)) {
126 snd_interval_none(i
);
132 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
134 struct snd_interval t
;
137 t
.openmin
= t
.openmax
= 0;
139 return snd_interval_refine(i
, &t
);
143 * snd_pcm_hw_param_value_min
144 * @params: the hw_params instance
145 * @var: parameter to retrieve
146 * @dir: pointer to the direction (-1,0,1) or NULL
148 * Return the minimum value for field PAR.
151 snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params
*params
,
152 snd_pcm_hw_param_t var
, int *dir
)
154 if (hw_is_mask(var
)) {
157 return snd_mask_min(hw_param_mask_c(params
, var
));
159 if (hw_is_interval(var
)) {
160 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
163 return snd_interval_min(i
);
169 * snd_pcm_hw_param_value_max
170 * @params: the hw_params instance
171 * @var: parameter to retrieve
172 * @dir: pointer to the direction (-1,0,1) or NULL
174 * Return the maximum value for field PAR.
177 snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params
*params
,
178 snd_pcm_hw_param_t var
, int *dir
)
180 if (hw_is_mask(var
)) {
183 return snd_mask_max(hw_param_mask_c(params
, var
));
185 if (hw_is_interval(var
)) {
186 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
188 *dir
= - (int) i
->openmax
;
189 return snd_interval_max(i
);
194 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params
*params
,
195 snd_pcm_hw_param_t var
,
196 const struct snd_mask
*val
)
199 changed
= snd_mask_refine(hw_param_mask(params
, var
), val
);
201 params
->cmask
|= 1 << var
;
202 params
->rmask
|= 1 << var
;
207 static int snd_pcm_hw_param_mask(struct snd_pcm_substream
*pcm
,
208 struct snd_pcm_hw_params
*params
,
209 snd_pcm_hw_param_t var
,
210 const struct snd_mask
*val
)
212 int changed
= _snd_pcm_hw_param_mask(params
, var
, val
);
216 int err
= snd_pcm_hw_refine(pcm
, params
);
223 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params
*params
,
224 snd_pcm_hw_param_t var
, unsigned int val
,
232 } else if (dir
< 0) {
240 changed
= snd_mask_refine_min(hw_param_mask(params
, var
),
242 else if (hw_is_interval(var
))
243 changed
= snd_interval_refine_min(hw_param_interval(params
, var
),
248 params
->cmask
|= 1 << var
;
249 params
->rmask
|= 1 << var
;
255 * snd_pcm_hw_param_min
257 * @params: the hw_params instance
258 * @var: parameter to retrieve
259 * @val: minimal value
260 * @dir: pointer to the direction (-1,0,1) or NULL
262 * Inside configuration space defined by PARAMS remove from PAR all
263 * values < VAL. Reduce configuration space accordingly.
264 * Return new minimum or -EINVAL if the configuration space is empty
266 static int snd_pcm_hw_param_min(struct snd_pcm_substream
*pcm
,
267 struct snd_pcm_hw_params
*params
,
268 snd_pcm_hw_param_t var
, unsigned int val
,
271 int changed
= _snd_pcm_hw_param_min(params
, var
, val
, dir
? *dir
: 0);
275 int err
= snd_pcm_hw_refine(pcm
, params
);
279 return snd_pcm_hw_param_value_min(params
, var
, dir
);
282 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params
*params
,
283 snd_pcm_hw_param_t var
, unsigned int val
,
291 } else if (dir
> 0) {
296 if (hw_is_mask(var
)) {
297 if (val
== 0 && open
) {
298 snd_mask_none(hw_param_mask(params
, var
));
301 changed
= snd_mask_refine_max(hw_param_mask(params
, var
),
303 } else if (hw_is_interval(var
))
304 changed
= snd_interval_refine_max(hw_param_interval(params
, var
),
309 params
->cmask
|= 1 << var
;
310 params
->rmask
|= 1 << var
;
316 * snd_pcm_hw_param_max
318 * @params: the hw_params instance
319 * @var: parameter to retrieve
320 * @val: maximal value
321 * @dir: pointer to the direction (-1,0,1) or NULL
323 * Inside configuration space defined by PARAMS remove from PAR all
324 * values >= VAL + 1. Reduce configuration space accordingly.
325 * Return new maximum or -EINVAL if the configuration space is empty
327 static int snd_pcm_hw_param_max(struct snd_pcm_substream
*pcm
,
328 struct snd_pcm_hw_params
*params
,
329 snd_pcm_hw_param_t var
, unsigned int val
,
332 int changed
= _snd_pcm_hw_param_max(params
, var
, val
, dir
? *dir
: 0);
336 int err
= snd_pcm_hw_refine(pcm
, params
);
340 return snd_pcm_hw_param_value_max(params
, var
, dir
);
343 static int boundary_sub(int a
, int adir
,
347 adir
= adir
< 0 ? -1 : (adir
> 0 ? 1 : 0);
348 bdir
= bdir
< 0 ? -1 : (bdir
> 0 ? 1 : 0);
353 } else if (*cdir
== 2) {
359 static int boundary_lt(unsigned int a
, int adir
,
360 unsigned int b
, int bdir
)
372 return a
< b
|| (a
== b
&& adir
< bdir
);
375 /* Return 1 if min is nearer to best than max */
376 static int boundary_nearer(int min
, int mindir
,
377 int best
, int bestdir
,
382 boundary_sub(best
, bestdir
, min
, mindir
, &dmin
, &dmindir
);
383 boundary_sub(max
, maxdir
, best
, bestdir
, &dmax
, &dmaxdir
);
384 return boundary_lt(dmin
, dmindir
, dmax
, dmaxdir
);
388 * snd_pcm_hw_param_near
390 * @params: the hw_params instance
391 * @var: parameter to retrieve
392 * @best: value to set
393 * @dir: pointer to the direction (-1,0,1) or NULL
395 * Inside configuration space defined by PARAMS set PAR to the available value
396 * nearest to VAL. Reduce configuration space accordingly.
397 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
398 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
399 * Return the value found.
401 static int snd_pcm_hw_param_near(struct snd_pcm_substream
*pcm
,
402 struct snd_pcm_hw_params
*params
,
403 snd_pcm_hw_param_t var
, unsigned int best
,
406 struct snd_pcm_hw_params
*save
= NULL
;
408 unsigned int saved_min
;
412 int valdir
= dir
? *dir
: 0;
417 mindir
= maxdir
= valdir
;
420 else if (maxdir
== 0)
426 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
431 min
= snd_pcm_hw_param_min(pcm
, params
, var
, min
, &mindir
);
433 struct snd_pcm_hw_params
*params1
;
436 if ((unsigned int)min
== saved_min
&& mindir
== valdir
)
438 params1
= kmalloc(sizeof(*params1
), GFP_KERNEL
);
439 if (params1
== NULL
) {
444 max
= snd_pcm_hw_param_max(pcm
, params1
, var
, max
, &maxdir
);
449 if (boundary_nearer(max
, maxdir
, best
, valdir
, min
, mindir
)) {
456 max
= snd_pcm_hw_param_max(pcm
, params
, var
, max
, &maxdir
);
466 v
= snd_pcm_hw_param_last(pcm
, params
, var
, dir
);
468 v
= snd_pcm_hw_param_first(pcm
, params
, var
, dir
);
472 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
473 snd_pcm_hw_param_t var
, unsigned int val
,
477 if (hw_is_mask(var
)) {
478 struct snd_mask
*m
= hw_param_mask(params
, var
);
479 if (val
== 0 && dir
< 0) {
487 changed
= snd_mask_refine_set(hw_param_mask(params
, var
), val
);
489 } else if (hw_is_interval(var
)) {
490 struct snd_interval
*i
= hw_param_interval(params
, var
);
491 if (val
== 0 && dir
< 0) {
493 snd_interval_none(i
);
495 changed
= snd_interval_refine_set(i
, val
);
497 struct snd_interval t
;
509 changed
= snd_interval_refine(i
, &t
);
514 params
->cmask
|= 1 << var
;
515 params
->rmask
|= 1 << var
;
521 * snd_pcm_hw_param_set
523 * @params: the hw_params instance
524 * @var: parameter to retrieve
526 * @dir: pointer to the direction (-1,0,1) or NULL
528 * Inside configuration space defined by PARAMS remove from PAR all
529 * values != VAL. Reduce configuration space accordingly.
530 * Return VAL or -EINVAL if the configuration space is empty
532 static int snd_pcm_hw_param_set(struct snd_pcm_substream
*pcm
,
533 struct snd_pcm_hw_params
*params
,
534 snd_pcm_hw_param_t var
, unsigned int val
,
537 int changed
= _snd_pcm_hw_param_set(params
, var
, val
, dir
);
541 int err
= snd_pcm_hw_refine(pcm
, params
);
545 return snd_pcm_hw_param_value(params
, var
, NULL
);
548 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params
*params
,
549 snd_pcm_hw_param_t var
)
552 changed
= snd_interval_setinteger(hw_param_interval(params
, var
));
554 params
->cmask
|= 1 << var
;
555 params
->rmask
|= 1 << var
;
564 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
565 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream
*substream
)
567 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
568 struct snd_pcm_plugin
*plugin
, *next
;
570 plugin
= runtime
->oss
.plugin_first
;
573 snd_pcm_plugin_free(plugin
);
576 runtime
->oss
.plugin_first
= runtime
->oss
.plugin_last
= NULL
;
580 static int snd_pcm_plugin_insert(struct snd_pcm_plugin
*plugin
)
582 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
583 plugin
->next
= runtime
->oss
.plugin_first
;
585 if (runtime
->oss
.plugin_first
) {
586 runtime
->oss
.plugin_first
->prev
= plugin
;
587 runtime
->oss
.plugin_first
= plugin
;
589 runtime
->oss
.plugin_last
=
590 runtime
->oss
.plugin_first
= plugin
;
595 int snd_pcm_plugin_append(struct snd_pcm_plugin
*plugin
)
597 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
599 plugin
->prev
= runtime
->oss
.plugin_last
;
600 if (runtime
->oss
.plugin_last
) {
601 runtime
->oss
.plugin_last
->next
= plugin
;
602 runtime
->oss
.plugin_last
= plugin
;
604 runtime
->oss
.plugin_last
=
605 runtime
->oss
.plugin_first
= plugin
;
609 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
611 static long snd_pcm_oss_bytes(struct snd_pcm_substream
*substream
, long frames
)
613 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
614 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
615 long bytes
= frames_to_bytes(runtime
, frames
);
616 if (buffer_size
== runtime
->oss
.buffer_bytes
)
618 #if BITS_PER_LONG >= 64
619 return runtime
->oss
.buffer_bytes
* bytes
/ buffer_size
;
622 u64 bsize
= (u64
)runtime
->oss
.buffer_bytes
* (u64
)bytes
;
623 return div_u64(bsize
, buffer_size
);
628 static long snd_pcm_alsa_frames(struct snd_pcm_substream
*substream
, long bytes
)
630 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
631 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
632 if (buffer_size
== runtime
->oss
.buffer_bytes
)
633 return bytes_to_frames(runtime
, bytes
);
634 return bytes_to_frames(runtime
, (buffer_size
* bytes
) / runtime
->oss
.buffer_bytes
);
638 snd_pcm_uframes_t
get_hw_ptr_period(struct snd_pcm_runtime
*runtime
)
640 return runtime
->hw_ptr_interrupt
;
643 /* define extended formats in the recent OSS versions (if any) */
645 #define AFMT_S32_LE 0x00001000
646 #define AFMT_S32_BE 0x00002000
647 #define AFMT_S24_LE 0x00008000
648 #define AFMT_S24_BE 0x00010000
649 #define AFMT_S24_PACKED 0x00040000
651 /* other supported formats */
652 #define AFMT_FLOAT 0x00004000
653 #define AFMT_SPDIF_RAW 0x00020000
655 /* unsupported formats */
656 #define AFMT_AC3 0x00000400
657 #define AFMT_VORBIS 0x00000800
659 static snd_pcm_format_t
snd_pcm_oss_format_from(int format
)
662 case AFMT_MU_LAW
: return SNDRV_PCM_FORMAT_MU_LAW
;
663 case AFMT_A_LAW
: return SNDRV_PCM_FORMAT_A_LAW
;
664 case AFMT_IMA_ADPCM
: return SNDRV_PCM_FORMAT_IMA_ADPCM
;
665 case AFMT_U8
: return SNDRV_PCM_FORMAT_U8
;
666 case AFMT_S16_LE
: return SNDRV_PCM_FORMAT_S16_LE
;
667 case AFMT_S16_BE
: return SNDRV_PCM_FORMAT_S16_BE
;
668 case AFMT_S8
: return SNDRV_PCM_FORMAT_S8
;
669 case AFMT_U16_LE
: return SNDRV_PCM_FORMAT_U16_LE
;
670 case AFMT_U16_BE
: return SNDRV_PCM_FORMAT_U16_BE
;
671 case AFMT_MPEG
: return SNDRV_PCM_FORMAT_MPEG
;
672 case AFMT_S32_LE
: return SNDRV_PCM_FORMAT_S32_LE
;
673 case AFMT_S32_BE
: return SNDRV_PCM_FORMAT_S32_BE
;
674 case AFMT_S24_LE
: return SNDRV_PCM_FORMAT_S24_LE
;
675 case AFMT_S24_BE
: return SNDRV_PCM_FORMAT_S24_BE
;
676 case AFMT_S24_PACKED
: return SNDRV_PCM_FORMAT_S24_3LE
;
677 case AFMT_FLOAT
: return SNDRV_PCM_FORMAT_FLOAT
;
678 case AFMT_SPDIF_RAW
: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME
;
679 default: return SNDRV_PCM_FORMAT_U8
;
683 static int snd_pcm_oss_format_to(snd_pcm_format_t format
)
686 case SNDRV_PCM_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
687 case SNDRV_PCM_FORMAT_A_LAW
: return AFMT_A_LAW
;
688 case SNDRV_PCM_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
689 case SNDRV_PCM_FORMAT_U8
: return AFMT_U8
;
690 case SNDRV_PCM_FORMAT_S16_LE
: return AFMT_S16_LE
;
691 case SNDRV_PCM_FORMAT_S16_BE
: return AFMT_S16_BE
;
692 case SNDRV_PCM_FORMAT_S8
: return AFMT_S8
;
693 case SNDRV_PCM_FORMAT_U16_LE
: return AFMT_U16_LE
;
694 case SNDRV_PCM_FORMAT_U16_BE
: return AFMT_U16_BE
;
695 case SNDRV_PCM_FORMAT_MPEG
: return AFMT_MPEG
;
696 case SNDRV_PCM_FORMAT_S32_LE
: return AFMT_S32_LE
;
697 case SNDRV_PCM_FORMAT_S32_BE
: return AFMT_S32_BE
;
698 case SNDRV_PCM_FORMAT_S24_LE
: return AFMT_S24_LE
;
699 case SNDRV_PCM_FORMAT_S24_BE
: return AFMT_S24_BE
;
700 case SNDRV_PCM_FORMAT_S24_3LE
: return AFMT_S24_PACKED
;
701 case SNDRV_PCM_FORMAT_FLOAT
: return AFMT_FLOAT
;
702 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME
: return AFMT_SPDIF_RAW
;
703 default: return -EINVAL
;
707 static int snd_pcm_oss_period_size(struct snd_pcm_substream
*substream
,
708 struct snd_pcm_hw_params
*oss_params
,
709 struct snd_pcm_hw_params
*slave_params
)
712 size_t oss_buffer_size
, oss_period_size
, oss_periods
;
713 size_t min_period_size
, max_period_size
;
714 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
715 size_t oss_frame_size
;
717 oss_frame_size
= snd_pcm_format_physical_width(params_format(oss_params
)) *
718 params_channels(oss_params
) / 8;
720 oss_buffer_size
= snd_pcm_plug_client_size(substream
,
721 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
)) * oss_frame_size
;
722 oss_buffer_size
= rounddown_pow_of_two(oss_buffer_size
);
723 if (atomic_read(&substream
->mmap_count
)) {
724 if (oss_buffer_size
> runtime
->oss
.mmap_bytes
)
725 oss_buffer_size
= runtime
->oss
.mmap_bytes
;
728 if (substream
->oss
.setup
.period_size
> 16)
729 oss_period_size
= substream
->oss
.setup
.period_size
;
730 else if (runtime
->oss
.fragshift
) {
731 oss_period_size
= 1 << runtime
->oss
.fragshift
;
732 if (oss_period_size
> oss_buffer_size
/ 2)
733 oss_period_size
= oss_buffer_size
/ 2;
736 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
738 oss_period_size
= oss_buffer_size
;
740 oss_period_size
/= 2;
741 } while (oss_period_size
> bytes_per_sec
);
742 if (runtime
->oss
.subdivision
== 0) {
744 if (oss_period_size
/ sd
> 4096)
746 if (oss_period_size
/ sd
< 4096)
749 sd
= runtime
->oss
.subdivision
;
750 oss_period_size
/= sd
;
751 if (oss_period_size
< 16)
752 oss_period_size
= 16;
755 min_period_size
= snd_pcm_plug_client_size(substream
,
756 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
757 min_period_size
*= oss_frame_size
;
758 min_period_size
= roundup_pow_of_two(min_period_size
);
759 if (oss_period_size
< min_period_size
)
760 oss_period_size
= min_period_size
;
762 max_period_size
= snd_pcm_plug_client_size(substream
,
763 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
764 max_period_size
*= oss_frame_size
;
765 max_period_size
= rounddown_pow_of_two(max_period_size
);
766 if (oss_period_size
> max_period_size
)
767 oss_period_size
= max_period_size
;
769 oss_periods
= oss_buffer_size
/ oss_period_size
;
771 if (substream
->oss
.setup
.periods
> 1)
772 oss_periods
= substream
->oss
.setup
.periods
;
774 s
= snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
775 if (runtime
->oss
.maxfrags
&& s
> runtime
->oss
.maxfrags
)
776 s
= runtime
->oss
.maxfrags
;
780 s
= snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
786 while (oss_period_size
* oss_periods
> oss_buffer_size
)
787 oss_period_size
/= 2;
789 if (oss_period_size
< 16)
791 runtime
->oss
.period_bytes
= oss_period_size
;
792 runtime
->oss
.period_frames
= 1;
793 runtime
->oss
.periods
= oss_periods
;
797 static int choose_rate(struct snd_pcm_substream
*substream
,
798 struct snd_pcm_hw_params
*params
, unsigned int best_rate
)
800 struct snd_interval
*it
;
801 struct snd_pcm_hw_params
*save
;
802 unsigned int rate
, prev
;
804 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
808 it
= hw_param_interval(save
, SNDRV_PCM_HW_PARAM_RATE
);
810 /* try multiples of the best rate */
813 if (it
->max
< rate
|| (it
->max
== rate
&& it
->openmax
))
815 if (it
->min
< rate
|| (it
->min
== rate
&& !it
->openmin
)) {
817 ret
= snd_pcm_hw_param_set(substream
, params
,
818 SNDRV_PCM_HW_PARAM_RATE
,
820 if (ret
== (int)rate
) {
832 /* not found, use the nearest rate */
834 return snd_pcm_hw_param_near(substream
, params
, SNDRV_PCM_HW_PARAM_RATE
, best_rate
, NULL
);
837 /* parameter locking: returns immediately if tried during streaming */
838 static int lock_params(struct snd_pcm_runtime
*runtime
)
840 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
842 if (atomic_read(&runtime
->oss
.rw_ref
)) {
843 mutex_unlock(&runtime
->oss
.params_lock
);
849 static void unlock_params(struct snd_pcm_runtime
*runtime
)
851 mutex_unlock(&runtime
->oss
.params_lock
);
854 /* call with params_lock held */
855 static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream
*substream
)
857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
858 struct snd_pcm_hw_params
*params
, *sparams
;
859 struct snd_pcm_sw_params
*sw_params
;
860 ssize_t oss_buffer_size
, oss_period_size
;
861 size_t oss_frame_size
;
864 snd_pcm_format_t format
, sformat
;
866 struct snd_mask sformat_mask
;
867 struct snd_mask mask
;
869 if (!runtime
->oss
.params
)
871 sw_params
= kzalloc(sizeof(*sw_params
), GFP_KERNEL
);
872 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
873 sparams
= kmalloc(sizeof(*sparams
), GFP_KERNEL
);
874 if (!sw_params
|| !params
|| !sparams
) {
879 if (atomic_read(&substream
->mmap_count
))
882 direct
= substream
->oss
.setup
.direct
;
884 _snd_pcm_hw_params_any(sparams
);
885 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
886 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
887 snd_mask_none(&mask
);
888 if (atomic_read(&substream
->mmap_count
))
889 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
891 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
893 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
895 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
897 pcm_dbg(substream
->pcm
, "No usable accesses\n");
901 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
902 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
904 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
906 sformat_mask
= *hw_param_mask(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
910 sformat
= snd_pcm_plug_slave_format(format
, &sformat_mask
);
912 if ((__force
int)sformat
< 0 ||
913 !snd_mask_test(&sformat_mask
, (__force
int)sformat
)) {
914 for (sformat
= (__force snd_pcm_format_t
)0;
915 (__force
int)sformat
<= (__force
int)SNDRV_PCM_FORMAT_LAST
;
916 sformat
= (__force snd_pcm_format_t
)((__force
int)sformat
+ 1)) {
917 if (snd_mask_test(&sformat_mask
, (__force
int)sformat
) &&
918 snd_pcm_oss_format_to(sformat
) >= 0)
921 if ((__force
int)sformat
> (__force
int)SNDRV_PCM_FORMAT_LAST
) {
922 pcm_dbg(substream
->pcm
, "Cannot find a format!!!\n");
927 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, (__force
int)sformat
, 0);
932 memcpy(params
, sparams
, sizeof(*params
));
934 _snd_pcm_hw_params_any(params
);
935 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
936 (__force
int)SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
937 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
938 (__force
int)snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
939 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
940 runtime
->oss
.channels
, 0);
941 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
942 runtime
->oss
.rate
, 0);
943 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
944 params_access(params
), params_format(params
),
945 params_channels(params
), params_rate(params
));
947 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
948 params_access(sparams
), params_format(sparams
),
949 params_channels(sparams
), params_rate(sparams
));
951 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
952 params_channels(params
) / 8;
954 err
= snd_pcm_oss_period_size(substream
, params
, sparams
);
958 n
= snd_pcm_plug_slave_size(substream
, runtime
->oss
.period_bytes
/ oss_frame_size
);
959 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, n
, NULL
);
963 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIODS
,
964 runtime
->oss
.periods
, NULL
);
968 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
970 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, sparams
);
972 pcm_dbg(substream
->pcm
, "HW_PARAMS failed: %i\n", err
);
976 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
977 snd_pcm_oss_plugin_clear(substream
);
979 /* add necessary plugins */
980 snd_pcm_oss_plugin_clear(substream
);
981 if ((err
= snd_pcm_plug_format_plugins(substream
,
984 pcm_dbg(substream
->pcm
,
985 "snd_pcm_plug_format_plugins failed: %i\n", err
);
986 snd_pcm_oss_plugin_clear(substream
);
989 if (runtime
->oss
.plugin_first
) {
990 struct snd_pcm_plugin
*plugin
;
991 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
992 pcm_dbg(substream
->pcm
,
993 "snd_pcm_plugin_build_io failed: %i\n", err
);
994 snd_pcm_oss_plugin_clear(substream
);
997 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
998 err
= snd_pcm_plugin_append(plugin
);
1000 err
= snd_pcm_plugin_insert(plugin
);
1003 snd_pcm_oss_plugin_clear(substream
);
1010 if (runtime
->oss
.trigger
) {
1011 sw_params
->start_threshold
= 1;
1013 sw_params
->start_threshold
= runtime
->boundary
;
1015 if (atomic_read(&substream
->mmap_count
) ||
1016 substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
1017 sw_params
->stop_threshold
= runtime
->boundary
;
1019 sw_params
->stop_threshold
= runtime
->buffer_size
;
1020 sw_params
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
1021 sw_params
->period_step
= 1;
1022 sw_params
->avail_min
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
1023 1 : runtime
->period_size
;
1024 if (atomic_read(&substream
->mmap_count
) ||
1025 substream
->oss
.setup
.nosilence
) {
1026 sw_params
->silence_threshold
= 0;
1027 sw_params
->silence_size
= 0;
1029 snd_pcm_uframes_t frames
;
1030 frames
= runtime
->period_size
+ 16;
1031 if (frames
> runtime
->buffer_size
)
1032 frames
= runtime
->buffer_size
;
1033 sw_params
->silence_threshold
= frames
;
1034 sw_params
->silence_size
= frames
;
1037 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_SW_PARAMS
, sw_params
)) < 0) {
1038 pcm_dbg(substream
->pcm
, "SW_PARAMS failed: %i\n", err
);
1042 runtime
->oss
.periods
= params_periods(sparams
);
1043 oss_period_size
= snd_pcm_plug_client_size(substream
, params_period_size(sparams
));
1044 if (oss_period_size
< 0) {
1048 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1049 if (runtime
->oss
.plugin_first
) {
1050 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
1055 oss_period_size
*= oss_frame_size
;
1057 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
1058 if (oss_buffer_size
< 0) {
1063 runtime
->oss
.period_bytes
= oss_period_size
;
1064 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
1066 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1067 runtime
->oss
.period_bytes
,
1068 runtime
->oss
.buffer_bytes
);
1069 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1070 params_period_size(sparams
),
1071 params_buffer_size(sparams
));
1073 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
1074 runtime
->oss
.channels
= params_channels(params
);
1075 runtime
->oss
.rate
= params_rate(params
);
1077 vfree(runtime
->oss
.buffer
);
1078 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
1079 if (!runtime
->oss
.buffer
) {
1084 runtime
->oss
.params
= 0;
1085 runtime
->oss
.prepare
= 1;
1086 runtime
->oss
.buffer_used
= 0;
1087 if (runtime
->dma_area
)
1088 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
1090 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
1100 /* this one takes the lock by itself */
1101 static int snd_pcm_oss_change_params(struct snd_pcm_substream
*substream
,
1104 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1108 if (!(mutex_trylock(&runtime
->oss
.params_lock
)))
1110 } else if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
1111 return -ERESTARTSYS
;
1113 err
= snd_pcm_oss_change_params_locked(substream
);
1114 mutex_unlock(&runtime
->oss
.params_lock
);
1118 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file
*pcm_oss_file
, struct snd_pcm_substream
**r_substream
)
1121 struct snd_pcm_substream
*asubstream
= NULL
, *substream
;
1123 for (idx
= 0; idx
< 2; idx
++) {
1124 substream
= pcm_oss_file
->streams
[idx
];
1125 if (substream
== NULL
)
1127 if (asubstream
== NULL
)
1128 asubstream
= substream
;
1129 if (substream
->runtime
->oss
.params
) {
1130 err
= snd_pcm_oss_change_params(substream
, false);
1138 *r_substream
= asubstream
;
1142 /* call with params_lock held */
1143 /* NOTE: this always call PREPARE unconditionally no matter whether
1144 * runtime->oss.prepare is set or not
1146 static int snd_pcm_oss_prepare(struct snd_pcm_substream
*substream
)
1149 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1151 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
1153 pcm_dbg(substream
->pcm
,
1154 "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1157 runtime
->oss
.prepare
= 0;
1158 runtime
->oss
.prev_hw_ptr_period
= 0;
1159 runtime
->oss
.period_ptr
= 0;
1160 runtime
->oss
.buffer_used
= 0;
1165 static int snd_pcm_oss_make_ready(struct snd_pcm_substream
*substream
)
1167 struct snd_pcm_runtime
*runtime
;
1170 runtime
= substream
->runtime
;
1171 if (runtime
->oss
.params
) {
1172 err
= snd_pcm_oss_change_params(substream
, false);
1176 if (runtime
->oss
.prepare
) {
1177 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
1178 return -ERESTARTSYS
;
1179 err
= snd_pcm_oss_prepare(substream
);
1180 mutex_unlock(&runtime
->oss
.params_lock
);
1187 /* call with params_lock held */
1188 static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream
*substream
)
1190 struct snd_pcm_runtime
*runtime
;
1193 runtime
= substream
->runtime
;
1194 if (runtime
->oss
.params
) {
1195 err
= snd_pcm_oss_change_params_locked(substream
);
1199 if (runtime
->oss
.prepare
) {
1200 err
= snd_pcm_oss_prepare(substream
);
1207 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream
*substream
, snd_pcm_sframes_t
*delay
)
1209 struct snd_pcm_runtime
*runtime
;
1210 snd_pcm_uframes_t frames
;
1214 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
1217 runtime
= substream
->runtime
;
1218 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
1220 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1221 /* until avail(delay) <= buffer_size */
1222 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
1223 frames
/= runtime
->period_size
;
1224 frames
*= runtime
->period_size
;
1225 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
1232 snd_pcm_sframes_t
snd_pcm_oss_write3(struct snd_pcm_substream
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1234 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1237 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1238 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1240 pcm_dbg(substream
->pcm
,
1241 "pcm_oss: write: recovering from %s\n",
1242 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1243 "XRUN" : "SUSPEND");
1245 ret
= snd_pcm_oss_prepare(substream
);
1251 fs
= snd_enter_user();
1252 ret
= snd_pcm_lib_write(substream
, (void __force __user
*)ptr
, frames
);
1255 ret
= snd_pcm_lib_write(substream
, (void __force __user
*)ptr
, frames
);
1257 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1259 /* test, if we can't store new data, because the stream */
1260 /* has not been started */
1261 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1267 snd_pcm_sframes_t
snd_pcm_oss_read3(struct snd_pcm_substream
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1269 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1270 snd_pcm_sframes_t delay
;
1273 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1274 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1276 pcm_dbg(substream
->pcm
,
1277 "pcm_oss: read: recovering from %s\n",
1278 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1279 "XRUN" : "SUSPEND");
1281 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1284 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1285 ret
= snd_pcm_oss_prepare(substream
);
1289 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1294 fs
= snd_enter_user();
1295 ret
= snd_pcm_lib_read(substream
, (void __force __user
*)ptr
, frames
);
1298 ret
= snd_pcm_lib_read(substream
, (void __force __user
*)ptr
, frames
);
1300 if (ret
== -EPIPE
) {
1301 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1302 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1308 if (ret
!= -ESTRPIPE
)
1314 snd_pcm_sframes_t
snd_pcm_oss_writev3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
1316 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1319 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1320 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1322 pcm_dbg(substream
->pcm
,
1323 "pcm_oss: writev: recovering from %s\n",
1324 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1325 "XRUN" : "SUSPEND");
1327 ret
= snd_pcm_oss_prepare(substream
);
1333 fs
= snd_enter_user();
1334 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
1337 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
1339 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1342 /* test, if we can't store new data, because the stream */
1343 /* has not been started */
1344 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1350 snd_pcm_sframes_t
snd_pcm_oss_readv3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
1352 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1355 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1356 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1358 pcm_dbg(substream
->pcm
,
1359 "pcm_oss: readv: recovering from %s\n",
1360 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1361 "XRUN" : "SUSPEND");
1363 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1366 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1367 ret
= snd_pcm_oss_prepare(substream
);
1373 fs
= snd_enter_user();
1374 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
1377 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
1379 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1385 static ssize_t
snd_pcm_oss_write2(struct snd_pcm_substream
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
1387 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1388 snd_pcm_sframes_t frames
, frames1
;
1389 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1390 if (runtime
->oss
.plugin_first
) {
1391 struct snd_pcm_plugin_channel
*channels
;
1392 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
1394 if (copy_from_user(runtime
->oss
.buffer
, (const char __force __user
*)buf
, bytes
))
1396 buf
= runtime
->oss
.buffer
;
1398 frames
= bytes
/ oss_frame_bytes
;
1399 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
1402 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
1405 bytes
= frames1
* oss_frame_bytes
;
1409 frames
= bytes_to_frames(runtime
, bytes
);
1410 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
1413 bytes
= frames_to_bytes(runtime
, frames1
);
1418 static ssize_t
snd_pcm_oss_write1(struct snd_pcm_substream
*substream
, const char __user
*buf
, size_t bytes
)
1422 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1424 if (atomic_read(&substream
->mmap_count
))
1427 atomic_inc(&runtime
->oss
.rw_ref
);
1429 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
)) {
1433 tmp
= snd_pcm_oss_make_ready_locked(substream
);
1436 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1438 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
1439 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
1441 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
)) {
1446 runtime
->oss
.buffer_used
+= tmp
;
1450 if (substream
->oss
.setup
.partialfrag
||
1451 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
1452 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
1453 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
1456 runtime
->oss
.bytes
+= tmp
;
1457 runtime
->oss
.period_ptr
+= tmp
;
1458 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
1459 if (runtime
->oss
.period_ptr
== 0 ||
1460 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
1461 runtime
->oss
.buffer_used
= 0;
1462 else if ((substream
->f_flags
& O_NONBLOCK
) != 0) {
1468 tmp
= snd_pcm_oss_write2(substream
,
1469 (const char __force
*)buf
,
1470 runtime
->oss
.period_bytes
, 0);
1473 runtime
->oss
.bytes
+= tmp
;
1477 if ((substream
->f_flags
& O_NONBLOCK
) != 0 &&
1478 tmp
!= runtime
->oss
.period_bytes
)
1482 mutex_unlock(&runtime
->oss
.params_lock
);
1485 if (signal_pending(current
)) {
1491 atomic_dec(&runtime
->oss
.rw_ref
);
1492 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1495 static ssize_t
snd_pcm_oss_read2(struct snd_pcm_substream
*substream
, char *buf
, size_t bytes
, int in_kernel
)
1497 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1498 snd_pcm_sframes_t frames
, frames1
;
1499 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1500 char __user
*final_dst
= (char __force __user
*)buf
;
1501 if (runtime
->oss
.plugin_first
) {
1502 struct snd_pcm_plugin_channel
*channels
;
1503 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
1505 buf
= runtime
->oss
.buffer
;
1506 frames
= bytes
/ oss_frame_bytes
;
1507 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
1510 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
1513 bytes
= frames1
* oss_frame_bytes
;
1514 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
1519 frames
= bytes_to_frames(runtime
, bytes
);
1520 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
1523 bytes
= frames_to_bytes(runtime
, frames1
);
1528 static ssize_t
snd_pcm_oss_read1(struct snd_pcm_substream
*substream
, char __user
*buf
, size_t bytes
)
1532 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1534 if (atomic_read(&substream
->mmap_count
))
1537 atomic_inc(&runtime
->oss
.rw_ref
);
1539 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
)) {
1543 tmp
= snd_pcm_oss_make_ready_locked(substream
);
1546 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1547 if (runtime
->oss
.buffer_used
== 0) {
1548 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
1551 runtime
->oss
.bytes
+= tmp
;
1552 runtime
->oss
.period_ptr
= tmp
;
1553 runtime
->oss
.buffer_used
= tmp
;
1556 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
1557 tmp
= runtime
->oss
.buffer_used
;
1558 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
)) {
1565 runtime
->oss
.buffer_used
-= tmp
;
1567 tmp
= snd_pcm_oss_read2(substream
, (char __force
*)buf
,
1568 runtime
->oss
.period_bytes
, 0);
1571 runtime
->oss
.bytes
+= tmp
;
1577 mutex_unlock(&runtime
->oss
.params_lock
);
1580 if (signal_pending(current
)) {
1586 atomic_dec(&runtime
->oss
.rw_ref
);
1587 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1590 static int snd_pcm_oss_reset(struct snd_pcm_oss_file
*pcm_oss_file
)
1592 struct snd_pcm_substream
*substream
;
1593 struct snd_pcm_runtime
*runtime
;
1596 for (i
= 0; i
< 2; i
++) {
1597 substream
= pcm_oss_file
->streams
[i
];
1600 runtime
= substream
->runtime
;
1601 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1602 mutex_lock(&runtime
->oss
.params_lock
);
1603 runtime
->oss
.prepare
= 1;
1604 runtime
->oss
.buffer_used
= 0;
1605 runtime
->oss
.prev_hw_ptr_period
= 0;
1606 runtime
->oss
.period_ptr
= 0;
1607 mutex_unlock(&runtime
->oss
.params_lock
);
1612 static int snd_pcm_oss_post(struct snd_pcm_oss_file
*pcm_oss_file
)
1614 struct snd_pcm_substream
*substream
;
1617 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1618 if (substream
!= NULL
) {
1619 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1621 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
1623 /* note: all errors from the start action are ignored */
1624 /* OSS apps do not know, how to handle them */
1628 static int snd_pcm_oss_sync1(struct snd_pcm_substream
*substream
, size_t size
)
1630 struct snd_pcm_runtime
*runtime
;
1632 snd_pcm_state_t state
;
1636 runtime
= substream
->runtime
;
1637 init_waitqueue_entry(&wait
, current
);
1638 add_wait_queue(&runtime
->sleep
, &wait
);
1640 pcm_dbg(substream
->pcm
, "sync1: size = %li\n", size
);
1643 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
1645 runtime
->oss
.buffer_used
= 0;
1649 if (result
!= 0 && result
!= -EAGAIN
)
1652 set_current_state(TASK_INTERRUPTIBLE
);
1653 snd_pcm_stream_lock_irq(substream
);
1654 state
= runtime
->status
->state
;
1655 snd_pcm_stream_unlock_irq(substream
);
1656 if (state
!= SNDRV_PCM_STATE_RUNNING
) {
1657 set_current_state(TASK_RUNNING
);
1660 res
= schedule_timeout(10 * HZ
);
1661 if (signal_pending(current
)) {
1662 result
= -ERESTARTSYS
;
1666 pcm_err(substream
->pcm
,
1667 "OSS sync error - DMA timeout\n");
1672 remove_wait_queue(&runtime
->sleep
, &wait
);
1676 static int snd_pcm_oss_sync(struct snd_pcm_oss_file
*pcm_oss_file
)
1679 unsigned int saved_f_flags
;
1680 struct snd_pcm_substream
*substream
;
1681 struct snd_pcm_runtime
*runtime
;
1682 snd_pcm_format_t format
;
1683 unsigned long width
;
1686 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1687 if (substream
!= NULL
) {
1688 runtime
= substream
->runtime
;
1689 if (atomic_read(&substream
->mmap_count
))
1691 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1693 atomic_inc(&runtime
->oss
.rw_ref
);
1694 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
)) {
1695 atomic_dec(&runtime
->oss
.rw_ref
);
1696 return -ERESTARTSYS
;
1698 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1699 width
= snd_pcm_format_physical_width(format
);
1700 if (runtime
->oss
.buffer_used
> 0) {
1702 pcm_dbg(substream
->pcm
, "sync: buffer_used\n");
1704 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1705 snd_pcm_format_set_silence(format
,
1706 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1708 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1711 } else if (runtime
->oss
.period_ptr
> 0) {
1713 pcm_dbg(substream
->pcm
, "sync: period_ptr\n");
1715 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1716 snd_pcm_format_set_silence(format
,
1717 runtime
->oss
.buffer
,
1719 err
= snd_pcm_oss_sync1(substream
, size
);
1724 * The ALSA's period might be a bit large than OSS one.
1725 * Fill the remain portion of ALSA period with zeros.
1727 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1729 size
= runtime
->period_size
- size
;
1730 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
1731 size
= (runtime
->frame_bits
* size
) / 8;
1734 size_t size1
= size
< runtime
->oss
.period_bytes
? size
: runtime
->oss
.period_bytes
;
1737 size1
/= runtime
->sample_bits
;
1738 snd_pcm_format_set_silence(runtime
->format
,
1739 runtime
->oss
.buffer
,
1741 size1
/= runtime
->channels
; /* frames */
1742 fs
= snd_enter_user();
1743 snd_pcm_lib_write(substream
, (void __force __user
*)runtime
->oss
.buffer
, size1
);
1746 } else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
1747 void __user
*buffers
[runtime
->channels
];
1748 memset(buffers
, 0, runtime
->channels
* sizeof(void *));
1749 snd_pcm_lib_writev(substream
, buffers
, size
);
1753 mutex_unlock(&runtime
->oss
.params_lock
);
1754 atomic_dec(&runtime
->oss
.rw_ref
);
1758 * finish sync: drain the buffer
1761 saved_f_flags
= substream
->f_flags
;
1762 substream
->f_flags
&= ~O_NONBLOCK
;
1763 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1764 substream
->f_flags
= saved_f_flags
;
1767 mutex_lock(&runtime
->oss
.params_lock
);
1768 runtime
->oss
.prepare
= 1;
1769 mutex_unlock(&runtime
->oss
.params_lock
);
1772 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1773 if (substream
!= NULL
) {
1774 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1776 runtime
= substream
->runtime
;
1777 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1780 mutex_lock(&runtime
->oss
.params_lock
);
1781 runtime
->oss
.buffer_used
= 0;
1782 runtime
->oss
.prepare
= 1;
1783 mutex_unlock(&runtime
->oss
.params_lock
);
1788 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file
*pcm_oss_file
, int rate
)
1792 for (idx
= 1; idx
>= 0; --idx
) {
1793 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1794 struct snd_pcm_runtime
*runtime
;
1797 if (substream
== NULL
)
1799 runtime
= substream
->runtime
;
1802 else if (rate
> 192000)
1804 err
= lock_params(runtime
);
1807 if (runtime
->oss
.rate
!= rate
) {
1808 runtime
->oss
.params
= 1;
1809 runtime
->oss
.rate
= rate
;
1811 unlock_params(runtime
);
1813 return snd_pcm_oss_get_rate(pcm_oss_file
);
1816 static int snd_pcm_oss_get_rate(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
.rate
;
1826 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int channels
)
1833 for (idx
= 1; idx
>= 0; --idx
) {
1834 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1835 struct snd_pcm_runtime
*runtime
;
1838 if (substream
== NULL
)
1840 runtime
= substream
->runtime
;
1841 err
= lock_params(runtime
);
1844 if (runtime
->oss
.channels
!= channels
) {
1845 runtime
->oss
.params
= 1;
1846 runtime
->oss
.channels
= channels
;
1848 unlock_params(runtime
);
1850 return snd_pcm_oss_get_channels(pcm_oss_file
);
1853 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
)
1855 struct snd_pcm_substream
*substream
;
1858 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1860 return substream
->runtime
->oss
.channels
;
1863 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file
*pcm_oss_file
)
1865 struct snd_pcm_substream
*substream
;
1868 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1870 return substream
->runtime
->oss
.period_bytes
;
1873 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file
*pcm_oss_file
)
1875 struct snd_pcm_substream
*substream
;
1878 struct snd_pcm_hw_params
*params
;
1879 unsigned int formats
= 0;
1880 struct snd_mask format_mask
;
1883 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1885 if (atomic_read(&substream
->mmap_count
))
1888 direct
= substream
->oss
.setup
.direct
;
1890 return AFMT_MU_LAW
| AFMT_U8
|
1891 AFMT_S16_LE
| AFMT_S16_BE
|
1892 AFMT_S8
| AFMT_U16_LE
|
1894 AFMT_S32_LE
| AFMT_S32_BE
|
1895 AFMT_S24_LE
| AFMT_S24_BE
|
1897 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1900 _snd_pcm_hw_params_any(params
);
1901 err
= snd_pcm_hw_refine(substream
, params
);
1904 format_mask
= *hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1905 for (fmt
= 0; fmt
< 32; ++fmt
) {
1906 if (snd_mask_test(&format_mask
, fmt
)) {
1907 int f
= snd_pcm_oss_format_to(fmt
);
1915 return err
< 0 ? err
: formats
;
1918 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file
*pcm_oss_file
, int format
)
1923 if (format
!= AFMT_QUERY
) {
1924 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1927 if (!(formats
& format
))
1929 for (idx
= 1; idx
>= 0; --idx
) {
1930 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1931 struct snd_pcm_runtime
*runtime
;
1932 if (substream
== NULL
)
1934 runtime
= substream
->runtime
;
1935 err
= lock_params(runtime
);
1938 if (runtime
->oss
.format
!= format
) {
1939 runtime
->oss
.params
= 1;
1940 runtime
->oss
.format
= format
;
1942 unlock_params(runtime
);
1945 return snd_pcm_oss_get_format(pcm_oss_file
);
1948 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
)
1950 struct snd_pcm_substream
*substream
;
1953 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1955 return substream
->runtime
->oss
.format
;
1958 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream
*substream
, int subdivide
)
1960 struct snd_pcm_runtime
*runtime
;
1962 runtime
= substream
->runtime
;
1963 if (subdivide
== 0) {
1964 subdivide
= runtime
->oss
.subdivision
;
1969 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1971 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1972 subdivide
!= 8 && subdivide
!= 16)
1974 runtime
->oss
.subdivision
= subdivide
;
1975 runtime
->oss
.params
= 1;
1979 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file
*pcm_oss_file
, int subdivide
)
1981 int err
= -EINVAL
, idx
;
1983 for (idx
= 1; idx
>= 0; --idx
) {
1984 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1985 struct snd_pcm_runtime
*runtime
;
1987 if (substream
== NULL
)
1989 runtime
= substream
->runtime
;
1990 err
= lock_params(runtime
);
1993 err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
);
1994 unlock_params(runtime
);
2001 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream
*substream
, unsigned int val
)
2003 struct snd_pcm_runtime
*runtime
;
2005 runtime
= substream
->runtime
;
2006 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
2008 runtime
->oss
.fragshift
= val
& 0xffff;
2009 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
2010 if (runtime
->oss
.fragshift
< 4) /* < 16 */
2011 runtime
->oss
.fragshift
= 4;
2012 if (runtime
->oss
.maxfrags
< 2)
2013 runtime
->oss
.maxfrags
= 2;
2014 runtime
->oss
.params
= 1;
2018 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int val
)
2020 int err
= -EINVAL
, idx
;
2022 for (idx
= 1; idx
>= 0; --idx
) {
2023 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
2024 struct snd_pcm_runtime
*runtime
;
2026 if (substream
== NULL
)
2028 runtime
= substream
->runtime
;
2029 err
= lock_params(runtime
);
2032 err
= snd_pcm_oss_set_fragment1(substream
, val
);
2033 unlock_params(runtime
);
2040 static int snd_pcm_oss_nonblock(struct file
* file
)
2042 spin_lock(&file
->f_lock
);
2043 file
->f_flags
|= O_NONBLOCK
;
2044 spin_unlock(&file
->f_lock
);
2048 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream
*substream
, int res
)
2051 if (substream
== NULL
) {
2052 res
&= ~DSP_CAP_DUPLEX
;
2055 #ifdef DSP_CAP_MULTI
2056 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2057 if (substream
->pstr
->substream_count
> 1)
2058 res
|= DSP_CAP_MULTI
;
2060 /* DSP_CAP_REALTIME is set all times: */
2061 /* all ALSA drivers can return actual pointer in ring buffer */
2062 #if defined(DSP_CAP_REALTIME) && 0
2064 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2065 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
2066 res
&= ~DSP_CAP_REALTIME
;
2072 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file
*pcm_oss_file
)
2076 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
2077 for (idx
= 0; idx
< 2; idx
++) {
2078 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
2079 result
= snd_pcm_oss_get_caps1(substream
, result
);
2081 result
|= 0x0001; /* revision - same as SB AWE 64 */
2085 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream
*substream
,
2086 snd_pcm_uframes_t hw_ptr
)
2088 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2089 snd_pcm_uframes_t appl_ptr
;
2090 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
2091 appl_ptr
%= runtime
->boundary
;
2092 runtime
->control
->appl_ptr
= appl_ptr
;
2095 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file
*pcm_oss_file
, int trigger
)
2097 struct snd_pcm_runtime
*runtime
;
2098 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2102 pcm_dbg(substream
->pcm
, "pcm_oss: trigger = 0x%x\n", trigger
);
2105 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2106 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2109 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
2113 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
2117 runtime
= psubstream
->runtime
;
2119 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
2120 return -ERESTARTSYS
;
2121 if (trigger
& PCM_ENABLE_OUTPUT
) {
2122 if (runtime
->oss
.trigger
)
2124 if (atomic_read(&psubstream
->mmap_count
))
2125 snd_pcm_oss_simulate_fill(psubstream
,
2126 get_hw_ptr_period(runtime
));
2127 runtime
->oss
.trigger
= 1;
2128 runtime
->start_threshold
= 1;
2129 cmd
= SNDRV_PCM_IOCTL_START
;
2131 if (!runtime
->oss
.trigger
)
2133 runtime
->oss
.trigger
= 0;
2134 runtime
->start_threshold
= runtime
->boundary
;
2135 cmd
= SNDRV_PCM_IOCTL_DROP
;
2136 runtime
->oss
.prepare
= 1;
2139 mutex_unlock(&runtime
->oss
.params_lock
);
2141 err
= snd_pcm_kernel_ioctl(psubstream
, cmd
, NULL
);
2147 runtime
= csubstream
->runtime
;
2149 if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
2150 return -ERESTARTSYS
;
2151 if (trigger
& PCM_ENABLE_INPUT
) {
2152 if (runtime
->oss
.trigger
)
2154 runtime
->oss
.trigger
= 1;
2155 runtime
->start_threshold
= 1;
2156 cmd
= SNDRV_PCM_IOCTL_START
;
2158 if (!runtime
->oss
.trigger
)
2160 runtime
->oss
.trigger
= 0;
2161 runtime
->start_threshold
= runtime
->boundary
;
2162 cmd
= SNDRV_PCM_IOCTL_DROP
;
2163 runtime
->oss
.prepare
= 1;
2166 mutex_unlock(&runtime
->oss
.params_lock
);
2168 err
= snd_pcm_kernel_ioctl(csubstream
, cmd
, NULL
);
2176 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file
*pcm_oss_file
)
2178 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2181 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2182 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2183 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
2184 result
|= PCM_ENABLE_OUTPUT
;
2185 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
2186 result
|= PCM_ENABLE_INPUT
;
2190 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file
*pcm_oss_file
)
2192 struct snd_pcm_substream
*substream
;
2193 struct snd_pcm_runtime
*runtime
;
2194 snd_pcm_sframes_t delay
;
2197 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2198 if (substream
== NULL
)
2200 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2202 runtime
= substream
->runtime
;
2203 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
2205 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2207 delay
= 0; /* hack for broken OSS applications */
2210 return snd_pcm_oss_bytes(substream
, delay
);
2213 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
2215 struct snd_pcm_substream
*substream
;
2216 struct snd_pcm_runtime
*runtime
;
2217 snd_pcm_sframes_t delay
;
2219 struct count_info info
;
2224 substream
= pcm_oss_file
->streams
[stream
];
2225 if (substream
== NULL
)
2227 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2229 runtime
= substream
->runtime
;
2230 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
2231 memset(&info
, 0, sizeof(info
));
2232 if (copy_to_user(_info
, &info
, sizeof(info
)))
2236 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2237 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2238 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
2243 fixup
= runtime
->oss
.buffer_used
;
2246 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
2247 fixup
= -runtime
->oss
.buffer_used
;
2251 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
2252 if (atomic_read(&substream
->mmap_count
)) {
2253 snd_pcm_sframes_t n
;
2254 delay
= get_hw_ptr_period(runtime
);
2255 n
= delay
- runtime
->oss
.prev_hw_ptr_period
;
2257 n
+= runtime
->boundary
;
2258 info
.blocks
= n
/ runtime
->period_size
;
2259 runtime
->oss
.prev_hw_ptr_period
= delay
;
2260 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2261 snd_pcm_oss_simulate_fill(substream
, delay
);
2262 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
2264 delay
= snd_pcm_oss_bytes(substream
, delay
);
2265 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2266 if (substream
->oss
.setup
.buggyptr
)
2267 info
.blocks
= (runtime
->oss
.buffer_bytes
- delay
- fixup
) / runtime
->oss
.period_bytes
;
2269 info
.blocks
= (delay
+ fixup
) / runtime
->oss
.period_bytes
;
2270 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
2273 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
2274 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
2277 if (copy_to_user(_info
, &info
, sizeof(info
)))
2282 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
2284 struct snd_pcm_substream
*substream
;
2285 struct snd_pcm_runtime
*runtime
;
2286 snd_pcm_sframes_t avail
;
2288 struct audio_buf_info info
;
2293 substream
= pcm_oss_file
->streams
[stream
];
2294 if (substream
== NULL
)
2296 runtime
= substream
->runtime
;
2298 if (runtime
->oss
.params
&&
2299 (err
= snd_pcm_oss_change_params(substream
, false)) < 0)
2302 info
.fragsize
= runtime
->oss
.period_bytes
;
2303 info
.fragstotal
= runtime
->periods
;
2304 if (runtime
->oss
.prepare
) {
2305 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2306 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
2307 info
.fragments
= runtime
->oss
.periods
;
2313 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2314 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
2315 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
2316 avail
= runtime
->buffer_size
;
2320 avail
= runtime
->buffer_size
- avail
;
2321 fixup
= -runtime
->oss
.buffer_used
;
2324 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
2325 fixup
= runtime
->oss
.buffer_used
;
2329 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
2330 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
2334 pcm_dbg(substream
->pcm
,
2335 "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2336 info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
2338 if (copy_to_user(_info
, &info
, sizeof(info
)))
2343 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
2345 // it won't be probably implemented
2346 // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2350 static const char *strip_task_path(const char *path
)
2352 const char *ptr
, *ptrl
= NULL
;
2353 for (ptr
= path
; *ptr
; ptr
++) {
2360 static void snd_pcm_oss_look_for_setup(struct snd_pcm
*pcm
, int stream
,
2361 const char *task_name
,
2362 struct snd_pcm_oss_setup
*rsetup
)
2364 struct snd_pcm_oss_setup
*setup
;
2366 mutex_lock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2368 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
;
2369 setup
= setup
->next
) {
2370 if (!strcmp(setup
->task_name
, task_name
))
2373 } while ((task_name
= strip_task_path(task_name
)) != NULL
);
2377 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2380 static void snd_pcm_oss_release_substream(struct snd_pcm_substream
*substream
)
2382 struct snd_pcm_runtime
*runtime
;
2383 runtime
= substream
->runtime
;
2384 vfree(runtime
->oss
.buffer
);
2385 runtime
->oss
.buffer
= NULL
;
2386 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2387 snd_pcm_oss_plugin_clear(substream
);
2389 substream
->oss
.oss
= 0;
2392 static void snd_pcm_oss_init_substream(struct snd_pcm_substream
*substream
,
2393 struct snd_pcm_oss_setup
*setup
,
2396 struct snd_pcm_runtime
*runtime
;
2398 substream
->oss
.oss
= 1;
2399 substream
->oss
.setup
= *setup
;
2400 if (setup
->nonblock
)
2401 substream
->f_flags
|= O_NONBLOCK
;
2402 else if (setup
->block
)
2403 substream
->f_flags
&= ~O_NONBLOCK
;
2404 runtime
= substream
->runtime
;
2405 runtime
->oss
.params
= 1;
2406 runtime
->oss
.trigger
= 1;
2407 runtime
->oss
.rate
= 8000;
2408 mutex_init(&runtime
->oss
.params_lock
);
2409 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
2410 case SNDRV_MINOR_OSS_PCM_8
:
2411 runtime
->oss
.format
= AFMT_U8
;
2413 case SNDRV_MINOR_OSS_PCM_16
:
2414 runtime
->oss
.format
= AFMT_S16_LE
;
2417 runtime
->oss
.format
= AFMT_MU_LAW
;
2419 runtime
->oss
.channels
= 1;
2420 runtime
->oss
.fragshift
= 0;
2421 runtime
->oss
.maxfrags
= 0;
2422 runtime
->oss
.subdivision
= 0;
2423 substream
->pcm_release
= snd_pcm_oss_release_substream
;
2424 atomic_set(&runtime
->oss
.rw_ref
, 0);
2427 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file
*pcm_oss_file
)
2432 for (cidx
= 0; cidx
< 2; ++cidx
) {
2433 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[cidx
];
2435 snd_pcm_release_substream(substream
);
2437 kfree(pcm_oss_file
);
2441 static int snd_pcm_oss_open_file(struct file
*file
,
2442 struct snd_pcm
*pcm
,
2443 struct snd_pcm_oss_file
**rpcm_oss_file
,
2445 struct snd_pcm_oss_setup
*setup
)
2448 struct snd_pcm_oss_file
*pcm_oss_file
;
2449 struct snd_pcm_substream
*substream
;
2450 fmode_t f_mode
= file
->f_mode
;
2453 *rpcm_oss_file
= NULL
;
2455 pcm_oss_file
= kzalloc(sizeof(*pcm_oss_file
), GFP_KERNEL
);
2456 if (pcm_oss_file
== NULL
)
2459 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
2460 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
2461 f_mode
= FMODE_WRITE
;
2463 file
->f_flags
&= ~O_APPEND
;
2464 for (idx
= 0; idx
< 2; idx
++) {
2465 if (setup
[idx
].disable
)
2467 if (! pcm
->streams
[idx
].substream_count
)
2468 continue; /* no matching substream */
2469 if (idx
== SNDRV_PCM_STREAM_PLAYBACK
) {
2470 if (! (f_mode
& FMODE_WRITE
))
2473 if (! (f_mode
& FMODE_READ
))
2476 err
= snd_pcm_open_substream(pcm
, idx
, file
, &substream
);
2478 snd_pcm_oss_release_file(pcm_oss_file
);
2482 pcm_oss_file
->streams
[idx
] = substream
;
2483 substream
->file
= pcm_oss_file
;
2484 snd_pcm_oss_init_substream(substream
, &setup
[idx
], minor
);
2487 if (!pcm_oss_file
->streams
[0] && !pcm_oss_file
->streams
[1]) {
2488 snd_pcm_oss_release_file(pcm_oss_file
);
2492 file
->private_data
= pcm_oss_file
;
2494 *rpcm_oss_file
= pcm_oss_file
;
2499 static int snd_task_name(struct task_struct
*task
, char *name
, size_t size
)
2503 if (snd_BUG_ON(!task
|| !name
|| size
< 2))
2505 for (idx
= 0; idx
< sizeof(task
->comm
) && idx
+ 1 < size
; idx
++)
2506 name
[idx
] = task
->comm
[idx
];
2511 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
2515 struct snd_pcm
*pcm
;
2516 struct snd_pcm_oss_file
*pcm_oss_file
;
2517 struct snd_pcm_oss_setup setup
[2];
2521 err
= nonseekable_open(inode
, file
);
2525 pcm
= snd_lookup_oss_minor_data(iminor(inode
),
2526 SNDRV_OSS_DEVICE_TYPE_PCM
);
2531 err
= snd_card_file_add(pcm
->card
, file
);
2534 if (!try_module_get(pcm
->card
->module
)) {
2538 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
2542 memset(setup
, 0, sizeof(setup
));
2543 if (file
->f_mode
& FMODE_WRITE
)
2544 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
2545 task_name
, &setup
[0]);
2546 if (file
->f_mode
& FMODE_READ
)
2547 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
2548 task_name
, &setup
[1]);
2550 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
2552 nonblock
= nonblock_open
;
2554 init_waitqueue_entry(&wait
, current
);
2555 add_wait_queue(&pcm
->open_wait
, &wait
);
2556 mutex_lock(&pcm
->open_mutex
);
2558 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
2559 iminor(inode
), setup
);
2562 if (err
== -EAGAIN
) {
2569 set_current_state(TASK_INTERRUPTIBLE
);
2570 mutex_unlock(&pcm
->open_mutex
);
2572 mutex_lock(&pcm
->open_mutex
);
2573 if (pcm
->card
->shutdown
) {
2577 if (signal_pending(current
)) {
2582 remove_wait_queue(&pcm
->open_wait
, &wait
);
2583 mutex_unlock(&pcm
->open_mutex
);
2586 snd_card_unref(pcm
->card
);
2590 module_put(pcm
->card
->module
);
2592 snd_card_file_remove(pcm
->card
, file
);
2595 snd_card_unref(pcm
->card
);
2599 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
2601 struct snd_pcm
*pcm
;
2602 struct snd_pcm_substream
*substream
;
2603 struct snd_pcm_oss_file
*pcm_oss_file
;
2605 pcm_oss_file
= file
->private_data
;
2606 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2607 if (substream
== NULL
)
2608 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2609 if (snd_BUG_ON(!substream
))
2611 pcm
= substream
->pcm
;
2612 if (!pcm
->card
->shutdown
)
2613 snd_pcm_oss_sync(pcm_oss_file
);
2614 mutex_lock(&pcm
->open_mutex
);
2615 snd_pcm_oss_release_file(pcm_oss_file
);
2616 mutex_unlock(&pcm
->open_mutex
);
2617 wake_up(&pcm
->open_wait
);
2618 module_put(pcm
->card
->module
);
2619 snd_card_file_remove(pcm
->card
, file
);
2623 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2625 struct snd_pcm_oss_file
*pcm_oss_file
;
2626 int __user
*p
= (int __user
*)arg
;
2629 pcm_oss_file
= file
->private_data
;
2630 if (cmd
== OSS_GETVERSION
)
2631 return put_user(SNDRV_OSS_VERSION
, p
);
2632 if (cmd
== OSS_ALSAEMULVER
)
2633 return put_user(1, p
);
2634 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2635 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2636 struct snd_pcm_substream
*substream
;
2638 for (idx
= 0; idx
< 2; ++idx
) {
2639 substream
= pcm_oss_file
->streams
[idx
];
2640 if (substream
!= NULL
)
2643 if (snd_BUG_ON(idx
>= 2))
2645 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
2648 if (((cmd
>> 8) & 0xff) != 'P')
2651 pr_debug("pcm_oss: ioctl = 0x%x\n", cmd
);
2654 case SNDCTL_DSP_RESET
:
2655 return snd_pcm_oss_reset(pcm_oss_file
);
2656 case SNDCTL_DSP_SYNC
:
2657 return snd_pcm_oss_sync(pcm_oss_file
);
2658 case SNDCTL_DSP_SPEED
:
2659 if (get_user(res
, p
))
2661 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
2663 return put_user(res
, p
);
2664 case SOUND_PCM_READ_RATE
:
2665 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
2668 return put_user(res
, p
);
2669 case SNDCTL_DSP_STEREO
:
2670 if (get_user(res
, p
))
2672 res
= res
> 0 ? 2 : 1;
2673 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
2675 return put_user(--res
, p
);
2676 case SNDCTL_DSP_GETBLKSIZE
:
2677 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
2680 return put_user(res
, p
);
2681 case SNDCTL_DSP_SETFMT
:
2682 if (get_user(res
, p
))
2684 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
2687 return put_user(res
, p
);
2688 case SOUND_PCM_READ_BITS
:
2689 res
= snd_pcm_oss_get_format(pcm_oss_file
);
2692 return put_user(res
, p
);
2693 case SNDCTL_DSP_CHANNELS
:
2694 if (get_user(res
, p
))
2696 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
2699 return put_user(res
, p
);
2700 case SOUND_PCM_READ_CHANNELS
:
2701 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
2704 return put_user(res
, p
);
2705 case SOUND_PCM_WRITE_FILTER
:
2706 case SOUND_PCM_READ_FILTER
:
2708 case SNDCTL_DSP_POST
:
2709 return snd_pcm_oss_post(pcm_oss_file
);
2710 case SNDCTL_DSP_SUBDIVIDE
:
2711 if (get_user(res
, p
))
2713 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
2716 return put_user(res
, p
);
2717 case SNDCTL_DSP_SETFRAGMENT
:
2718 if (get_user(res
, p
))
2720 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
2721 case SNDCTL_DSP_GETFMTS
:
2722 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
2725 return put_user(res
, p
);
2726 case SNDCTL_DSP_GETOSPACE
:
2727 case SNDCTL_DSP_GETISPACE
:
2728 return snd_pcm_oss_get_space(pcm_oss_file
,
2729 cmd
== SNDCTL_DSP_GETISPACE
?
2730 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2731 (struct audio_buf_info __user
*) arg
);
2732 case SNDCTL_DSP_NONBLOCK
:
2733 return snd_pcm_oss_nonblock(file
);
2734 case SNDCTL_DSP_GETCAPS
:
2735 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
2738 return put_user(res
, p
);
2739 case SNDCTL_DSP_GETTRIGGER
:
2740 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2743 return put_user(res
, p
);
2744 case SNDCTL_DSP_SETTRIGGER
:
2745 if (get_user(res
, p
))
2747 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2748 case SNDCTL_DSP_GETIPTR
:
2749 case SNDCTL_DSP_GETOPTR
:
2750 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2751 cmd
== SNDCTL_DSP_GETIPTR
?
2752 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2753 (struct count_info __user
*) arg
);
2754 case SNDCTL_DSP_MAPINBUF
:
2755 case SNDCTL_DSP_MAPOUTBUF
:
2756 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2757 cmd
== SNDCTL_DSP_MAPINBUF
?
2758 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2759 (struct buffmem_desc __user
*) arg
);
2760 case SNDCTL_DSP_SETSYNCRO
:
2761 /* stop DMA now.. */
2763 case SNDCTL_DSP_SETDUPLEX
:
2764 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2767 case SNDCTL_DSP_GETODELAY
:
2768 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2770 /* it's for sure, some broken apps don't check for error codes */
2774 return put_user(res
, p
);
2775 case SNDCTL_DSP_PROFILE
:
2776 return 0; /* silently ignore */
2778 pr_debug("pcm_oss: unknown command = 0x%x\n", cmd
);
2783 #ifdef CONFIG_COMPAT
2784 /* all compatible */
2785 static long snd_pcm_oss_ioctl_compat(struct file
*file
, unsigned int cmd
,
2788 return snd_pcm_oss_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
2791 #define snd_pcm_oss_ioctl_compat NULL
2794 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2796 struct snd_pcm_oss_file
*pcm_oss_file
;
2797 struct snd_pcm_substream
*substream
;
2799 pcm_oss_file
= file
->private_data
;
2800 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2801 if (substream
== NULL
)
2803 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2805 return snd_pcm_oss_read1(substream
, buf
, count
);
2808 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2809 pcm_dbg(substream
->pcm
,
2810 "pcm_oss: read %li bytes (returned %li bytes)\n",
2811 (long)count
, (long)res
);
2817 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2819 struct snd_pcm_oss_file
*pcm_oss_file
;
2820 struct snd_pcm_substream
*substream
;
2823 pcm_oss_file
= file
->private_data
;
2824 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2825 if (substream
== NULL
)
2827 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2828 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2830 pcm_dbg(substream
->pcm
, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2831 (long)count
, (long)result
);
2836 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream
*substream
)
2838 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2839 if (atomic_read(&substream
->mmap_count
))
2840 return runtime
->oss
.prev_hw_ptr_period
!=
2841 get_hw_ptr_period(runtime
);
2843 return snd_pcm_playback_avail(runtime
) >=
2844 runtime
->oss
.period_frames
;
2847 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream
*substream
)
2849 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2850 if (atomic_read(&substream
->mmap_count
))
2851 return runtime
->oss
.prev_hw_ptr_period
!=
2852 get_hw_ptr_period(runtime
);
2854 return snd_pcm_capture_avail(runtime
) >=
2855 runtime
->oss
.period_frames
;
2858 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2860 struct snd_pcm_oss_file
*pcm_oss_file
;
2862 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2864 pcm_oss_file
= file
->private_data
;
2866 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2867 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2870 if (psubstream
!= NULL
) {
2871 struct snd_pcm_runtime
*runtime
= psubstream
->runtime
;
2872 poll_wait(file
, &runtime
->sleep
, wait
);
2873 snd_pcm_stream_lock_irq(psubstream
);
2874 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2875 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2876 snd_pcm_oss_playback_ready(psubstream
)))
2877 mask
|= POLLOUT
| POLLWRNORM
;
2878 snd_pcm_stream_unlock_irq(psubstream
);
2880 if (csubstream
!= NULL
) {
2881 struct snd_pcm_runtime
*runtime
= csubstream
->runtime
;
2882 snd_pcm_state_t ostate
;
2883 poll_wait(file
, &runtime
->sleep
, wait
);
2884 snd_pcm_stream_lock_irq(csubstream
);
2885 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2886 snd_pcm_oss_capture_ready(csubstream
))
2887 mask
|= POLLIN
| POLLRDNORM
;
2888 snd_pcm_stream_unlock_irq(csubstream
);
2889 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2890 struct snd_pcm_oss_file ofile
;
2891 memset(&ofile
, 0, sizeof(ofile
));
2892 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2893 runtime
->oss
.trigger
= 0;
2894 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2901 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2903 struct snd_pcm_oss_file
*pcm_oss_file
;
2904 struct snd_pcm_substream
*substream
= NULL
;
2905 struct snd_pcm_runtime
*runtime
;
2909 pr_debug("pcm_oss: mmap begin\n");
2911 pcm_oss_file
= file
->private_data
;
2912 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2913 case VM_READ
| VM_WRITE
:
2914 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2919 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2922 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2927 /* set VM_READ access as well to fix memset() routines that do
2928 reads before writes (to improve performance) */
2929 area
->vm_flags
|= VM_READ
;
2930 if (substream
== NULL
)
2932 runtime
= substream
->runtime
;
2933 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2935 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2936 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2940 if (runtime
->oss
.params
) {
2941 /* use mutex_trylock() for params_lock for avoiding a deadlock
2942 * between mmap_sem and params_lock taken by
2943 * copy_from/to_user() in snd_pcm_oss_write/read()
2945 err
= snd_pcm_oss_change_params(substream
, true);
2949 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2950 if (runtime
->oss
.plugin_first
!= NULL
)
2954 if (area
->vm_pgoff
!= 0)
2957 err
= snd_pcm_mmap_data(substream
, file
, area
);
2960 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2961 runtime
->silence_threshold
= 0;
2962 runtime
->silence_size
= 0;
2964 pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2965 runtime
->oss
.mmap_bytes
);
2967 /* In mmap mode we never stop */
2968 runtime
->stop_threshold
= runtime
->boundary
;
2973 #ifdef CONFIG_SND_VERBOSE_PROCFS
2978 static void snd_pcm_oss_proc_read(struct snd_info_entry
*entry
,
2979 struct snd_info_buffer
*buffer
)
2981 struct snd_pcm_str
*pstr
= entry
->private_data
;
2982 struct snd_pcm_oss_setup
*setup
= pstr
->oss
.setup_list
;
2983 mutex_lock(&pstr
->oss
.setup_mutex
);
2985 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2989 setup
->disable
? " disable" : "",
2990 setup
->direct
? " direct" : "",
2991 setup
->block
? " block" : "",
2992 setup
->nonblock
? " non-block" : "",
2993 setup
->partialfrag
? " partial-frag" : "",
2994 setup
->nosilence
? " no-silence" : "");
2995 setup
= setup
->next
;
2997 mutex_unlock(&pstr
->oss
.setup_mutex
);
3000 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str
* pstr
)
3002 struct snd_pcm_oss_setup
*setup
, *setupn
;
3004 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
3005 setup
; setup
= setupn
) {
3006 setupn
= setup
->next
;
3007 kfree(setup
->task_name
);
3010 pstr
->oss
.setup_list
= NULL
;
3013 static void snd_pcm_oss_proc_write(struct snd_info_entry
*entry
,
3014 struct snd_info_buffer
*buffer
)
3016 struct snd_pcm_str
*pstr
= entry
->private_data
;
3017 char line
[128], str
[32], task_name
[32];
3020 struct snd_pcm_oss_setup
*setup
, *setup1
, template;
3022 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
3023 mutex_lock(&pstr
->oss
.setup_mutex
);
3024 memset(&template, 0, sizeof(template));
3025 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
3026 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
3027 snd_pcm_oss_proc_free_setup_list(pstr
);
3028 mutex_unlock(&pstr
->oss
.setup_mutex
);
3031 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
3032 if (!strcmp(setup
->task_name
, task_name
)) {
3037 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
3038 template.periods
= simple_strtoul(str
, NULL
, 10);
3039 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
3040 template.period_size
= simple_strtoul(str
, NULL
, 10);
3041 for (idx1
= 31; idx1
>= 0; idx1
--)
3042 if (template.period_size
& (1 << idx1
))
3044 for (idx1
--; idx1
>= 0; idx1
--)
3045 template.period_size
&= ~(1 << idx1
);
3047 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
3048 if (!strcmp(str
, "disable")) {
3049 template.disable
= 1;
3050 } else if (!strcmp(str
, "direct")) {
3051 template.direct
= 1;
3052 } else if (!strcmp(str
, "block")) {
3054 } else if (!strcmp(str
, "non-block")) {
3055 template.nonblock
= 1;
3056 } else if (!strcmp(str
, "partial-frag")) {
3057 template.partialfrag
= 1;
3058 } else if (!strcmp(str
, "no-silence")) {
3059 template.nosilence
= 1;
3060 } else if (!strcmp(str
, "buggy-ptr")) {
3061 template.buggyptr
= 1;
3064 if (setup
== NULL
) {
3065 setup
= kmalloc(sizeof(*setup
), GFP_KERNEL
);
3067 buffer
->error
= -ENOMEM
;
3068 mutex_unlock(&pstr
->oss
.setup_mutex
);
3071 if (pstr
->oss
.setup_list
== NULL
)
3072 pstr
->oss
.setup_list
= setup
;
3074 for (setup1
= pstr
->oss
.setup_list
;
3075 setup1
->next
; setup1
= setup1
->next
);
3076 setup1
->next
= setup
;
3078 template.task_name
= kstrdup(task_name
, GFP_KERNEL
);
3079 if (! template.task_name
) {
3081 buffer
->error
= -ENOMEM
;
3082 mutex_unlock(&pstr
->oss
.setup_mutex
);
3087 mutex_unlock(&pstr
->oss
.setup_mutex
);
3091 static void snd_pcm_oss_proc_init(struct snd_pcm
*pcm
)
3094 for (stream
= 0; stream
< 2; ++stream
) {
3095 struct snd_info_entry
*entry
;
3096 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
3097 if (pstr
->substream_count
== 0)
3099 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
3100 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
3101 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
3102 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
3103 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
3104 entry
->private_data
= pstr
;
3105 if (snd_info_register(entry
) < 0) {
3106 snd_info_free_entry(entry
);
3110 pstr
->oss
.proc_entry
= entry
;
3114 static void snd_pcm_oss_proc_done(struct snd_pcm
*pcm
)
3117 for (stream
= 0; stream
< 2; ++stream
) {
3118 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
3119 snd_info_free_entry(pstr
->oss
.proc_entry
);
3120 pstr
->oss
.proc_entry
= NULL
;
3121 snd_pcm_oss_proc_free_setup_list(pstr
);
3124 #else /* !CONFIG_SND_VERBOSE_PROCFS */
3125 #define snd_pcm_oss_proc_init(pcm)
3126 #define snd_pcm_oss_proc_done(pcm)
3127 #endif /* CONFIG_SND_VERBOSE_PROCFS */
3133 static const struct file_operations snd_pcm_oss_f_reg
=
3135 .owner
= THIS_MODULE
,
3136 .read
= snd_pcm_oss_read
,
3137 .write
= snd_pcm_oss_write
,
3138 .open
= snd_pcm_oss_open
,
3139 .release
= snd_pcm_oss_release
,
3140 .llseek
= no_llseek
,
3141 .poll
= snd_pcm_oss_poll
,
3142 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
3143 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
3144 .mmap
= snd_pcm_oss_mmap
,
3147 static void register_oss_dsp(struct snd_pcm
*pcm
, int index
)
3149 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3150 pcm
->card
, index
, &snd_pcm_oss_f_reg
,
3152 pcm_err(pcm
, "unable to register OSS PCM device %i:%i\n",
3153 pcm
->card
->number
, pcm
->device
);
3157 static int snd_pcm_oss_register_minor(struct snd_pcm
*pcm
)
3160 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
3163 register_oss_dsp(pcm
, 0);
3164 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
3165 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
3166 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
3167 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
3168 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3169 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
3174 pcm
->oss
.reg_mask
|= 1;
3176 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
3177 register_oss_dsp(pcm
, 1);
3179 pcm
->oss
.reg_mask
|= 2;
3183 snd_pcm_oss_proc_init(pcm
);
3188 static int snd_pcm_oss_disconnect_minor(struct snd_pcm
*pcm
)
3191 if (pcm
->oss
.reg_mask
& 1) {
3192 pcm
->oss
.reg_mask
&= ~1;
3193 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3196 if (pcm
->oss
.reg_mask
& 2) {
3197 pcm
->oss
.reg_mask
&= ~2;
3198 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3201 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
3202 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3203 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
3211 static int snd_pcm_oss_unregister_minor(struct snd_pcm
*pcm
)
3213 snd_pcm_oss_disconnect_minor(pcm
);
3214 snd_pcm_oss_proc_done(pcm
);
3218 static struct snd_pcm_notify snd_pcm_oss_notify
=
3220 .n_register
= snd_pcm_oss_register_minor
,
3221 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
3222 .n_unregister
= snd_pcm_oss_unregister_minor
,
3225 static int __init
alsa_pcm_oss_init(void)
3230 /* check device map table */
3231 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
3232 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3233 pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3237 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3238 pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3243 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
3248 static void __exit
alsa_pcm_oss_exit(void)
3250 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
3253 module_init(alsa_pcm_oss_init
)
3254 module_exit(alsa_pcm_oss_exit
)