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/sched/signal.h>
32 #include <linux/time.h>
33 #include <linux/vmalloc.h>
34 #include <linux/module.h>
35 #include <linux/math64.h>
36 #include <linux/string.h>
37 #include <linux/compat.h>
38 #include <sound/core.h>
39 #include <sound/minors.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include "pcm_plugin.h"
43 #include <sound/info.h>
44 #include <linux/soundcard.h>
45 #include <sound/initval.h>
46 #include <sound/mixer_oss.h>
48 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
50 static int dsp_map
[SNDRV_CARDS
];
51 static int adsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
52 static bool nonblock_open
= 1;
54 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
55 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
56 MODULE_LICENSE("GPL");
57 module_param_array(dsp_map
, int, NULL
, 0444);
58 MODULE_PARM_DESC(dsp_map
, "PCM device number assigned to 1st OSS device.");
59 module_param_array(adsp_map
, int, NULL
, 0444);
60 MODULE_PARM_DESC(adsp_map
, "PCM device number assigned to 2nd OSS device.");
61 module_param(nonblock_open
, bool, 0644);
62 MODULE_PARM_DESC(nonblock_open
, "Don't block opening busy PCM devices.");
63 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM
);
64 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1
);
66 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
);
67 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
);
68 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
);
71 * helper functions to process hw_params
73 static int snd_interval_refine_min(struct snd_interval
*i
, unsigned int min
, int openmin
)
80 } else if (i
->min
== min
&& !i
->openmin
&& openmin
) {
90 if (snd_interval_checkempty(i
)) {
97 static int snd_interval_refine_max(struct snd_interval
*i
, unsigned int max
, int openmax
)
102 i
->openmax
= openmax
;
104 } else if (i
->max
== max
&& !i
->openmax
&& openmax
) {
114 if (snd_interval_checkempty(i
)) {
115 snd_interval_none(i
);
121 static int snd_interval_refine_set(struct snd_interval
*i
, unsigned int val
)
123 struct snd_interval t
;
126 t
.openmin
= t
.openmax
= 0;
128 return snd_interval_refine(i
, &t
);
132 * snd_pcm_hw_param_value_min
133 * @params: the hw_params instance
134 * @var: parameter to retrieve
135 * @dir: pointer to the direction (-1,0,1) or NULL
137 * Return the minimum value for field PAR.
140 snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params
*params
,
141 snd_pcm_hw_param_t var
, int *dir
)
143 if (hw_is_mask(var
)) {
146 return snd_mask_min(hw_param_mask_c(params
, var
));
148 if (hw_is_interval(var
)) {
149 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
152 return snd_interval_min(i
);
158 * snd_pcm_hw_param_value_max
159 * @params: the hw_params instance
160 * @var: parameter to retrieve
161 * @dir: pointer to the direction (-1,0,1) or NULL
163 * Return the maximum value for field PAR.
166 snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params
*params
,
167 snd_pcm_hw_param_t var
, int *dir
)
169 if (hw_is_mask(var
)) {
172 return snd_mask_max(hw_param_mask_c(params
, var
));
174 if (hw_is_interval(var
)) {
175 const struct snd_interval
*i
= hw_param_interval_c(params
, var
);
177 *dir
= - (int) i
->openmax
;
178 return snd_interval_max(i
);
183 static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params
*params
,
184 snd_pcm_hw_param_t var
,
185 const struct snd_mask
*val
)
188 changed
= snd_mask_refine(hw_param_mask(params
, var
), val
);
190 params
->cmask
|= 1 << var
;
191 params
->rmask
|= 1 << var
;
196 static int snd_pcm_hw_param_mask(struct snd_pcm_substream
*pcm
,
197 struct snd_pcm_hw_params
*params
,
198 snd_pcm_hw_param_t var
,
199 const struct snd_mask
*val
)
201 int changed
= _snd_pcm_hw_param_mask(params
, var
, val
);
205 int err
= snd_pcm_hw_refine(pcm
, params
);
212 static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params
*params
,
213 snd_pcm_hw_param_t var
, unsigned int val
,
221 } else if (dir
< 0) {
229 changed
= snd_mask_refine_min(hw_param_mask(params
, var
),
231 else if (hw_is_interval(var
))
232 changed
= snd_interval_refine_min(hw_param_interval(params
, var
),
237 params
->cmask
|= 1 << var
;
238 params
->rmask
|= 1 << var
;
244 * snd_pcm_hw_param_min
246 * @params: the hw_params instance
247 * @var: parameter to retrieve
248 * @val: minimal value
249 * @dir: pointer to the direction (-1,0,1) or NULL
251 * Inside configuration space defined by PARAMS remove from PAR all
252 * values < VAL. Reduce configuration space accordingly.
253 * Return new minimum or -EINVAL if the configuration space is empty
255 static int snd_pcm_hw_param_min(struct snd_pcm_substream
*pcm
,
256 struct snd_pcm_hw_params
*params
,
257 snd_pcm_hw_param_t var
, unsigned int val
,
260 int changed
= _snd_pcm_hw_param_min(params
, var
, val
, dir
? *dir
: 0);
264 int err
= snd_pcm_hw_refine(pcm
, params
);
268 return snd_pcm_hw_param_value_min(params
, var
, dir
);
271 static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params
*params
,
272 snd_pcm_hw_param_t var
, unsigned int val
,
280 } else if (dir
> 0) {
285 if (hw_is_mask(var
)) {
286 if (val
== 0 && open
) {
287 snd_mask_none(hw_param_mask(params
, var
));
290 changed
= snd_mask_refine_max(hw_param_mask(params
, var
),
292 } else if (hw_is_interval(var
))
293 changed
= snd_interval_refine_max(hw_param_interval(params
, var
),
298 params
->cmask
|= 1 << var
;
299 params
->rmask
|= 1 << var
;
305 * snd_pcm_hw_param_max
307 * @params: the hw_params instance
308 * @var: parameter to retrieve
309 * @val: maximal value
310 * @dir: pointer to the direction (-1,0,1) or NULL
312 * Inside configuration space defined by PARAMS remove from PAR all
313 * values >= VAL + 1. Reduce configuration space accordingly.
314 * Return new maximum or -EINVAL if the configuration space is empty
316 static int snd_pcm_hw_param_max(struct snd_pcm_substream
*pcm
,
317 struct snd_pcm_hw_params
*params
,
318 snd_pcm_hw_param_t var
, unsigned int val
,
321 int changed
= _snd_pcm_hw_param_max(params
, var
, val
, dir
? *dir
: 0);
325 int err
= snd_pcm_hw_refine(pcm
, params
);
329 return snd_pcm_hw_param_value_max(params
, var
, dir
);
332 static int boundary_sub(int a
, int adir
,
336 adir
= adir
< 0 ? -1 : (adir
> 0 ? 1 : 0);
337 bdir
= bdir
< 0 ? -1 : (bdir
> 0 ? 1 : 0);
342 } else if (*cdir
== 2) {
348 static int boundary_lt(unsigned int a
, int adir
,
349 unsigned int b
, int bdir
)
361 return a
< b
|| (a
== b
&& adir
< bdir
);
364 /* Return 1 if min is nearer to best than max */
365 static int boundary_nearer(int min
, int mindir
,
366 int best
, int bestdir
,
371 boundary_sub(best
, bestdir
, min
, mindir
, &dmin
, &dmindir
);
372 boundary_sub(max
, maxdir
, best
, bestdir
, &dmax
, &dmaxdir
);
373 return boundary_lt(dmin
, dmindir
, dmax
, dmaxdir
);
377 * snd_pcm_hw_param_near
379 * @params: the hw_params instance
380 * @var: parameter to retrieve
381 * @best: value to set
382 * @dir: pointer to the direction (-1,0,1) or NULL
384 * Inside configuration space defined by PARAMS set PAR to the available value
385 * nearest to VAL. Reduce configuration space accordingly.
386 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
387 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
388 * Return the value found.
390 static int snd_pcm_hw_param_near(struct snd_pcm_substream
*pcm
,
391 struct snd_pcm_hw_params
*params
,
392 snd_pcm_hw_param_t var
, unsigned int best
,
395 struct snd_pcm_hw_params
*save
= NULL
;
397 unsigned int saved_min
;
401 int valdir
= dir
? *dir
: 0;
406 mindir
= maxdir
= valdir
;
409 else if (maxdir
== 0)
415 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
420 min
= snd_pcm_hw_param_min(pcm
, params
, var
, min
, &mindir
);
422 struct snd_pcm_hw_params
*params1
;
425 if ((unsigned int)min
== saved_min
&& mindir
== valdir
)
427 params1
= kmalloc(sizeof(*params1
), GFP_KERNEL
);
428 if (params1
== NULL
) {
433 max
= snd_pcm_hw_param_max(pcm
, params1
, var
, max
, &maxdir
);
438 if (boundary_nearer(max
, maxdir
, best
, valdir
, min
, mindir
)) {
445 max
= snd_pcm_hw_param_max(pcm
, params
, var
, max
, &maxdir
);
455 v
= snd_pcm_hw_param_last(pcm
, params
, var
, dir
);
457 v
= snd_pcm_hw_param_first(pcm
, params
, var
, dir
);
462 static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params
*params
,
463 snd_pcm_hw_param_t var
, unsigned int val
,
467 if (hw_is_mask(var
)) {
468 struct snd_mask
*m
= hw_param_mask(params
, var
);
469 if (val
== 0 && dir
< 0) {
477 changed
= snd_mask_refine_set(hw_param_mask(params
, var
), val
);
479 } else if (hw_is_interval(var
)) {
480 struct snd_interval
*i
= hw_param_interval(params
, var
);
481 if (val
== 0 && dir
< 0) {
483 snd_interval_none(i
);
485 changed
= snd_interval_refine_set(i
, val
);
487 struct snd_interval t
;
499 changed
= snd_interval_refine(i
, &t
);
504 params
->cmask
|= 1 << var
;
505 params
->rmask
|= 1 << var
;
511 * snd_pcm_hw_param_set
513 * @params: the hw_params instance
514 * @var: parameter to retrieve
516 * @dir: pointer to the direction (-1,0,1) or NULL
518 * Inside configuration space defined by PARAMS remove from PAR all
519 * values != VAL. Reduce configuration space accordingly.
520 * Return VAL or -EINVAL if the configuration space is empty
522 static int snd_pcm_hw_param_set(struct snd_pcm_substream
*pcm
,
523 struct snd_pcm_hw_params
*params
,
524 snd_pcm_hw_param_t var
, unsigned int val
,
527 int changed
= _snd_pcm_hw_param_set(params
, var
, val
, dir
);
531 int err
= snd_pcm_hw_refine(pcm
, params
);
535 return snd_pcm_hw_param_value(params
, var
, NULL
);
538 static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params
*params
,
539 snd_pcm_hw_param_t var
)
542 changed
= snd_interval_setinteger(hw_param_interval(params
, var
));
544 params
->cmask
|= 1 << var
;
545 params
->rmask
|= 1 << var
;
554 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
555 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream
*substream
)
557 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
558 struct snd_pcm_plugin
*plugin
, *next
;
560 plugin
= runtime
->oss
.plugin_first
;
563 snd_pcm_plugin_free(plugin
);
566 runtime
->oss
.plugin_first
= runtime
->oss
.plugin_last
= NULL
;
570 static int snd_pcm_plugin_insert(struct snd_pcm_plugin
*plugin
)
572 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
573 plugin
->next
= runtime
->oss
.plugin_first
;
575 if (runtime
->oss
.plugin_first
) {
576 runtime
->oss
.plugin_first
->prev
= plugin
;
577 runtime
->oss
.plugin_first
= plugin
;
579 runtime
->oss
.plugin_last
=
580 runtime
->oss
.plugin_first
= plugin
;
585 int snd_pcm_plugin_append(struct snd_pcm_plugin
*plugin
)
587 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
589 plugin
->prev
= runtime
->oss
.plugin_last
;
590 if (runtime
->oss
.plugin_last
) {
591 runtime
->oss
.plugin_last
->next
= plugin
;
592 runtime
->oss
.plugin_last
= plugin
;
594 runtime
->oss
.plugin_last
=
595 runtime
->oss
.plugin_first
= plugin
;
599 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
601 static long snd_pcm_oss_bytes(struct snd_pcm_substream
*substream
, long frames
)
603 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
604 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
605 long bytes
= frames_to_bytes(runtime
, frames
);
606 if (buffer_size
== runtime
->oss
.buffer_bytes
)
608 #if BITS_PER_LONG >= 64
609 return runtime
->oss
.buffer_bytes
* bytes
/ buffer_size
;
612 u64 bsize
= (u64
)runtime
->oss
.buffer_bytes
* (u64
)bytes
;
613 return div_u64(bsize
, buffer_size
);
618 static long snd_pcm_alsa_frames(struct snd_pcm_substream
*substream
, long bytes
)
620 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
621 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
622 if (buffer_size
== runtime
->oss
.buffer_bytes
)
623 return bytes_to_frames(runtime
, bytes
);
624 return bytes_to_frames(runtime
, (buffer_size
* bytes
) / runtime
->oss
.buffer_bytes
);
628 snd_pcm_uframes_t
get_hw_ptr_period(struct snd_pcm_runtime
*runtime
)
630 return runtime
->hw_ptr_interrupt
;
633 /* define extended formats in the recent OSS versions (if any) */
635 #define AFMT_S32_LE 0x00001000
636 #define AFMT_S32_BE 0x00002000
637 #define AFMT_S24_LE 0x00008000
638 #define AFMT_S24_BE 0x00010000
639 #define AFMT_S24_PACKED 0x00040000
641 /* other supported formats */
642 #define AFMT_FLOAT 0x00004000
643 #define AFMT_SPDIF_RAW 0x00020000
645 /* unsupported formats */
646 #define AFMT_AC3 0x00000400
647 #define AFMT_VORBIS 0x00000800
649 static snd_pcm_format_t
snd_pcm_oss_format_from(int format
)
652 case AFMT_MU_LAW
: return SNDRV_PCM_FORMAT_MU_LAW
;
653 case AFMT_A_LAW
: return SNDRV_PCM_FORMAT_A_LAW
;
654 case AFMT_IMA_ADPCM
: return SNDRV_PCM_FORMAT_IMA_ADPCM
;
655 case AFMT_U8
: return SNDRV_PCM_FORMAT_U8
;
656 case AFMT_S16_LE
: return SNDRV_PCM_FORMAT_S16_LE
;
657 case AFMT_S16_BE
: return SNDRV_PCM_FORMAT_S16_BE
;
658 case AFMT_S8
: return SNDRV_PCM_FORMAT_S8
;
659 case AFMT_U16_LE
: return SNDRV_PCM_FORMAT_U16_LE
;
660 case AFMT_U16_BE
: return SNDRV_PCM_FORMAT_U16_BE
;
661 case AFMT_MPEG
: return SNDRV_PCM_FORMAT_MPEG
;
662 case AFMT_S32_LE
: return SNDRV_PCM_FORMAT_S32_LE
;
663 case AFMT_S32_BE
: return SNDRV_PCM_FORMAT_S32_BE
;
664 case AFMT_S24_LE
: return SNDRV_PCM_FORMAT_S24_LE
;
665 case AFMT_S24_BE
: return SNDRV_PCM_FORMAT_S24_BE
;
666 case AFMT_S24_PACKED
: return SNDRV_PCM_FORMAT_S24_3LE
;
667 case AFMT_FLOAT
: return SNDRV_PCM_FORMAT_FLOAT
;
668 case AFMT_SPDIF_RAW
: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME
;
669 default: return SNDRV_PCM_FORMAT_U8
;
673 static int snd_pcm_oss_format_to(snd_pcm_format_t format
)
676 case SNDRV_PCM_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
677 case SNDRV_PCM_FORMAT_A_LAW
: return AFMT_A_LAW
;
678 case SNDRV_PCM_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
679 case SNDRV_PCM_FORMAT_U8
: return AFMT_U8
;
680 case SNDRV_PCM_FORMAT_S16_LE
: return AFMT_S16_LE
;
681 case SNDRV_PCM_FORMAT_S16_BE
: return AFMT_S16_BE
;
682 case SNDRV_PCM_FORMAT_S8
: return AFMT_S8
;
683 case SNDRV_PCM_FORMAT_U16_LE
: return AFMT_U16_LE
;
684 case SNDRV_PCM_FORMAT_U16_BE
: return AFMT_U16_BE
;
685 case SNDRV_PCM_FORMAT_MPEG
: return AFMT_MPEG
;
686 case SNDRV_PCM_FORMAT_S32_LE
: return AFMT_S32_LE
;
687 case SNDRV_PCM_FORMAT_S32_BE
: return AFMT_S32_BE
;
688 case SNDRV_PCM_FORMAT_S24_LE
: return AFMT_S24_LE
;
689 case SNDRV_PCM_FORMAT_S24_BE
: return AFMT_S24_BE
;
690 case SNDRV_PCM_FORMAT_S24_3LE
: return AFMT_S24_PACKED
;
691 case SNDRV_PCM_FORMAT_FLOAT
: return AFMT_FLOAT
;
692 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME
: return AFMT_SPDIF_RAW
;
693 default: return -EINVAL
;
697 static int snd_pcm_oss_period_size(struct snd_pcm_substream
*substream
,
698 struct snd_pcm_hw_params
*oss_params
,
699 struct snd_pcm_hw_params
*slave_params
)
702 size_t oss_buffer_size
, oss_period_size
, oss_periods
;
703 size_t min_period_size
, max_period_size
;
704 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
705 size_t oss_frame_size
;
707 oss_frame_size
= snd_pcm_format_physical_width(params_format(oss_params
)) *
708 params_channels(oss_params
) / 8;
710 oss_buffer_size
= snd_pcm_plug_client_size(substream
,
711 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
)) * oss_frame_size
;
712 oss_buffer_size
= rounddown_pow_of_two(oss_buffer_size
);
713 if (atomic_read(&substream
->mmap_count
)) {
714 if (oss_buffer_size
> runtime
->oss
.mmap_bytes
)
715 oss_buffer_size
= runtime
->oss
.mmap_bytes
;
718 if (substream
->oss
.setup
.period_size
> 16)
719 oss_period_size
= substream
->oss
.setup
.period_size
;
720 else if (runtime
->oss
.fragshift
) {
721 oss_period_size
= 1 << runtime
->oss
.fragshift
;
722 if (oss_period_size
> oss_buffer_size
/ 2)
723 oss_period_size
= oss_buffer_size
/ 2;
726 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
728 oss_period_size
= oss_buffer_size
;
730 oss_period_size
/= 2;
731 } while (oss_period_size
> bytes_per_sec
);
732 if (runtime
->oss
.subdivision
== 0) {
734 if (oss_period_size
/ sd
> 4096)
736 if (oss_period_size
/ sd
< 4096)
739 sd
= runtime
->oss
.subdivision
;
740 oss_period_size
/= sd
;
741 if (oss_period_size
< 16)
742 oss_period_size
= 16;
745 min_period_size
= snd_pcm_plug_client_size(substream
,
746 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
747 min_period_size
*= oss_frame_size
;
748 min_period_size
= roundup_pow_of_two(min_period_size
);
749 if (oss_period_size
< min_period_size
)
750 oss_period_size
= min_period_size
;
752 max_period_size
= snd_pcm_plug_client_size(substream
,
753 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
754 max_period_size
*= oss_frame_size
;
755 max_period_size
= rounddown_pow_of_two(max_period_size
);
756 if (oss_period_size
> max_period_size
)
757 oss_period_size
= max_period_size
;
759 oss_periods
= oss_buffer_size
/ oss_period_size
;
761 if (substream
->oss
.setup
.periods
> 1)
762 oss_periods
= substream
->oss
.setup
.periods
;
764 s
= snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
765 if (runtime
->oss
.maxfrags
&& s
> runtime
->oss
.maxfrags
)
766 s
= runtime
->oss
.maxfrags
;
770 s
= snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
776 while (oss_period_size
* oss_periods
> oss_buffer_size
)
777 oss_period_size
/= 2;
779 if (oss_period_size
< 16)
781 runtime
->oss
.period_bytes
= oss_period_size
;
782 runtime
->oss
.period_frames
= 1;
783 runtime
->oss
.periods
= oss_periods
;
787 static int choose_rate(struct snd_pcm_substream
*substream
,
788 struct snd_pcm_hw_params
*params
, unsigned int best_rate
)
790 const struct snd_interval
*it
;
791 struct snd_pcm_hw_params
*save
;
792 unsigned int rate
, prev
;
794 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
798 it
= hw_param_interval_c(save
, SNDRV_PCM_HW_PARAM_RATE
);
800 /* try multiples of the best rate */
803 if (it
->max
< rate
|| (it
->max
== rate
&& it
->openmax
))
805 if (it
->min
< rate
|| (it
->min
== rate
&& !it
->openmin
)) {
807 ret
= snd_pcm_hw_param_set(substream
, params
,
808 SNDRV_PCM_HW_PARAM_RATE
,
810 if (ret
== (int)rate
) {
822 /* not found, use the nearest rate */
824 return snd_pcm_hw_param_near(substream
, params
, SNDRV_PCM_HW_PARAM_RATE
, best_rate
, NULL
);
827 static int snd_pcm_oss_change_params(struct snd_pcm_substream
*substream
,
830 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
831 struct snd_pcm_hw_params
*params
, *sparams
;
832 struct snd_pcm_sw_params
*sw_params
;
833 ssize_t oss_buffer_size
, oss_period_size
;
834 size_t oss_frame_size
;
837 snd_pcm_format_t format
, sformat
;
839 const struct snd_mask
*sformat_mask
;
840 struct snd_mask mask
;
843 if (!(mutex_trylock(&runtime
->oss
.params_lock
)))
845 } else if (mutex_lock_interruptible(&runtime
->oss
.params_lock
))
847 sw_params
= kzalloc(sizeof(*sw_params
), GFP_KERNEL
);
848 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
849 sparams
= kmalloc(sizeof(*sparams
), GFP_KERNEL
);
850 if (!sw_params
|| !params
|| !sparams
) {
855 if (atomic_read(&substream
->mmap_count
))
858 direct
= substream
->oss
.setup
.direct
;
860 _snd_pcm_hw_params_any(sparams
);
861 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
862 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
863 snd_mask_none(&mask
);
864 if (atomic_read(&substream
->mmap_count
))
865 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
867 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
869 snd_mask_set(&mask
, (__force
int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
871 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
873 pcm_dbg(substream
->pcm
, "No usable accesses\n");
877 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
878 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
880 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
882 sformat_mask
= hw_param_mask_c(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
886 sformat
= snd_pcm_plug_slave_format(format
, sformat_mask
);
888 if ((__force
int)sformat
< 0 ||
889 !snd_mask_test(sformat_mask
, (__force
int)sformat
)) {
890 for (sformat
= (__force snd_pcm_format_t
)0;
891 (__force
int)sformat
<= (__force
int)SNDRV_PCM_FORMAT_LAST
;
892 sformat
= (__force snd_pcm_format_t
)((__force
int)sformat
+ 1)) {
893 if (snd_mask_test(sformat_mask
, (__force
int)sformat
) &&
894 snd_pcm_oss_format_to(sformat
) >= 0)
897 if ((__force
int)sformat
> (__force
int)SNDRV_PCM_FORMAT_LAST
) {
898 pcm_dbg(substream
->pcm
, "Cannot find a format!!!\n");
903 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, (__force
int)sformat
, 0);
908 memcpy(params
, sparams
, sizeof(*params
));
910 _snd_pcm_hw_params_any(params
);
911 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
912 (__force
int)SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
913 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
914 (__force
int)snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
915 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
916 runtime
->oss
.channels
, 0);
917 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
918 runtime
->oss
.rate
, 0);
919 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
920 params_access(params
), params_format(params
),
921 params_channels(params
), params_rate(params
));
923 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
924 params_access(sparams
), params_format(sparams
),
925 params_channels(sparams
), params_rate(sparams
));
927 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
928 params_channels(params
) / 8;
930 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
931 snd_pcm_oss_plugin_clear(substream
);
933 /* add necessary plugins */
934 snd_pcm_oss_plugin_clear(substream
);
935 if ((err
= snd_pcm_plug_format_plugins(substream
,
938 pcm_dbg(substream
->pcm
,
939 "snd_pcm_plug_format_plugins failed: %i\n", err
);
940 snd_pcm_oss_plugin_clear(substream
);
943 if (runtime
->oss
.plugin_first
) {
944 struct snd_pcm_plugin
*plugin
;
945 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
946 pcm_dbg(substream
->pcm
,
947 "snd_pcm_plugin_build_io failed: %i\n", err
);
948 snd_pcm_oss_plugin_clear(substream
);
951 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
952 err
= snd_pcm_plugin_append(plugin
);
954 err
= snd_pcm_plugin_insert(plugin
);
957 snd_pcm_oss_plugin_clear(substream
);
964 err
= snd_pcm_oss_period_size(substream
, params
, sparams
);
968 n
= snd_pcm_plug_slave_size(substream
, runtime
->oss
.period_bytes
/ oss_frame_size
);
969 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, n
, NULL
);
973 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIODS
,
974 runtime
->oss
.periods
, NULL
);
978 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
980 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, sparams
)) < 0) {
981 pcm_dbg(substream
->pcm
, "HW_PARAMS failed: %i\n", err
);
985 if (runtime
->oss
.trigger
) {
986 sw_params
->start_threshold
= 1;
988 sw_params
->start_threshold
= runtime
->boundary
;
990 if (atomic_read(&substream
->mmap_count
) ||
991 substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
992 sw_params
->stop_threshold
= runtime
->boundary
;
994 sw_params
->stop_threshold
= runtime
->buffer_size
;
995 sw_params
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
996 sw_params
->period_step
= 1;
997 sw_params
->avail_min
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
998 1 : runtime
->period_size
;
999 if (atomic_read(&substream
->mmap_count
) ||
1000 substream
->oss
.setup
.nosilence
) {
1001 sw_params
->silence_threshold
= 0;
1002 sw_params
->silence_size
= 0;
1004 snd_pcm_uframes_t frames
;
1005 frames
= runtime
->period_size
+ 16;
1006 if (frames
> runtime
->buffer_size
)
1007 frames
= runtime
->buffer_size
;
1008 sw_params
->silence_threshold
= frames
;
1009 sw_params
->silence_size
= frames
;
1012 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_SW_PARAMS
, sw_params
)) < 0) {
1013 pcm_dbg(substream
->pcm
, "SW_PARAMS failed: %i\n", err
);
1017 runtime
->oss
.periods
= params_periods(sparams
);
1018 oss_period_size
= snd_pcm_plug_client_size(substream
, params_period_size(sparams
));
1019 if (oss_period_size
< 0) {
1023 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1024 if (runtime
->oss
.plugin_first
) {
1025 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
1030 oss_period_size
*= oss_frame_size
;
1032 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
1033 if (oss_buffer_size
< 0) {
1038 runtime
->oss
.period_bytes
= oss_period_size
;
1039 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
1041 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1042 runtime
->oss
.period_bytes
,
1043 runtime
->oss
.buffer_bytes
);
1044 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1045 params_period_size(sparams
),
1046 params_buffer_size(sparams
));
1048 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
1049 runtime
->oss
.channels
= params_channels(params
);
1050 runtime
->oss
.rate
= params_rate(params
);
1052 vfree(runtime
->oss
.buffer
);
1053 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
1054 if (!runtime
->oss
.buffer
) {
1059 runtime
->oss
.params
= 0;
1060 runtime
->oss
.prepare
= 1;
1061 runtime
->oss
.buffer_used
= 0;
1062 if (runtime
->dma_area
)
1063 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
1065 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
1072 mutex_unlock(&runtime
->oss
.params_lock
);
1076 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file
*pcm_oss_file
, struct snd_pcm_substream
**r_substream
)
1079 struct snd_pcm_substream
*asubstream
= NULL
, *substream
;
1081 for (idx
= 0; idx
< 2; idx
++) {
1082 substream
= pcm_oss_file
->streams
[idx
];
1083 if (substream
== NULL
)
1085 if (asubstream
== NULL
)
1086 asubstream
= substream
;
1087 if (substream
->runtime
->oss
.params
) {
1088 err
= snd_pcm_oss_change_params(substream
, false);
1096 *r_substream
= asubstream
;
1100 static int snd_pcm_oss_prepare(struct snd_pcm_substream
*substream
)
1103 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1105 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
1107 pcm_dbg(substream
->pcm
,
1108 "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1111 runtime
->oss
.prepare
= 0;
1112 runtime
->oss
.prev_hw_ptr_period
= 0;
1113 runtime
->oss
.period_ptr
= 0;
1114 runtime
->oss
.buffer_used
= 0;
1119 static int snd_pcm_oss_make_ready(struct snd_pcm_substream
*substream
)
1121 struct snd_pcm_runtime
*runtime
;
1124 if (substream
== NULL
)
1126 runtime
= substream
->runtime
;
1127 if (runtime
->oss
.params
) {
1128 err
= snd_pcm_oss_change_params(substream
, false);
1132 if (runtime
->oss
.prepare
) {
1133 err
= snd_pcm_oss_prepare(substream
);
1140 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream
*substream
, snd_pcm_sframes_t
*delay
)
1142 struct snd_pcm_runtime
*runtime
;
1143 snd_pcm_uframes_t frames
;
1147 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
1150 runtime
= substream
->runtime
;
1151 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
1153 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1154 /* until avail(delay) <= buffer_size */
1155 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
1156 frames
/= runtime
->period_size
;
1157 frames
*= runtime
->period_size
;
1158 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
1165 snd_pcm_sframes_t
snd_pcm_oss_write3(struct snd_pcm_substream
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1167 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1170 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1171 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1173 pcm_dbg(substream
->pcm
,
1174 "pcm_oss: write: recovering from %s\n",
1175 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1176 "XRUN" : "SUSPEND");
1178 ret
= snd_pcm_oss_prepare(substream
);
1182 ret
= __snd_pcm_lib_xfer(substream
, (void *)ptr
, true,
1184 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1186 /* test, if we can't store new data, because the stream */
1187 /* has not been started */
1188 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1194 snd_pcm_sframes_t
snd_pcm_oss_read3(struct snd_pcm_substream
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
1196 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1197 snd_pcm_sframes_t delay
;
1200 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1201 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1203 pcm_dbg(substream
->pcm
,
1204 "pcm_oss: read: recovering from %s\n",
1205 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1206 "XRUN" : "SUSPEND");
1208 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1211 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1212 ret
= snd_pcm_oss_prepare(substream
);
1216 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1219 ret
= __snd_pcm_lib_xfer(substream
, (void *)ptr
, true,
1221 if (ret
== -EPIPE
) {
1222 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1223 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1229 if (ret
!= -ESTRPIPE
)
1235 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1236 snd_pcm_sframes_t
snd_pcm_oss_writev3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
)
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 pcm_dbg(substream
->pcm
,
1245 "pcm_oss: writev: recovering from %s\n",
1246 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1247 "XRUN" : "SUSPEND");
1249 ret
= snd_pcm_oss_prepare(substream
);
1253 ret
= snd_pcm_kernel_writev(substream
, bufs
, frames
);
1254 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1257 /* test, if we can't store new data, because the stream */
1258 /* has not been started */
1259 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
1265 snd_pcm_sframes_t
snd_pcm_oss_readv3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
)
1267 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1270 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
1271 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1273 pcm_dbg(substream
->pcm
,
1274 "pcm_oss: readv: recovering from %s\n",
1275 runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
?
1276 "XRUN" : "SUSPEND");
1278 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1281 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
1282 ret
= snd_pcm_oss_prepare(substream
);
1286 ret
= snd_pcm_kernel_readv(substream
, bufs
, frames
);
1287 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
1292 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
1294 static ssize_t
snd_pcm_oss_write2(struct snd_pcm_substream
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
1296 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1297 snd_pcm_sframes_t frames
, frames1
;
1298 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1299 if (runtime
->oss
.plugin_first
) {
1300 struct snd_pcm_plugin_channel
*channels
;
1301 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
1303 if (copy_from_user(runtime
->oss
.buffer
, (const char __force __user
*)buf
, bytes
))
1305 buf
= runtime
->oss
.buffer
;
1307 frames
= bytes
/ oss_frame_bytes
;
1308 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
1311 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
1314 bytes
= frames1
* oss_frame_bytes
;
1318 frames
= bytes_to_frames(runtime
, bytes
);
1319 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
1322 bytes
= frames_to_bytes(runtime
, frames1
);
1327 static ssize_t
snd_pcm_oss_write1(struct snd_pcm_substream
*substream
, const char __user
*buf
, size_t bytes
)
1331 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1333 if (atomic_read(&substream
->mmap_count
))
1336 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
1338 mutex_lock(&runtime
->oss
.params_lock
);
1340 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1342 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
1343 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
1345 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
)) {
1350 runtime
->oss
.buffer_used
+= tmp
;
1354 if (substream
->oss
.setup
.partialfrag
||
1355 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
1356 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
1357 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
1360 runtime
->oss
.bytes
+= tmp
;
1361 runtime
->oss
.period_ptr
+= tmp
;
1362 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
1363 if (runtime
->oss
.period_ptr
== 0 ||
1364 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
1365 runtime
->oss
.buffer_used
= 0;
1366 else if ((substream
->f_flags
& O_NONBLOCK
) != 0) {
1372 tmp
= snd_pcm_oss_write2(substream
,
1373 (const char __force
*)buf
,
1374 runtime
->oss
.period_bytes
, 0);
1377 runtime
->oss
.bytes
+= tmp
;
1381 if ((substream
->f_flags
& O_NONBLOCK
) != 0 &&
1382 tmp
!= runtime
->oss
.period_bytes
)
1386 mutex_unlock(&runtime
->oss
.params_lock
);
1390 mutex_unlock(&runtime
->oss
.params_lock
);
1391 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1394 static ssize_t
snd_pcm_oss_read2(struct snd_pcm_substream
*substream
, char *buf
, size_t bytes
, int in_kernel
)
1396 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1397 snd_pcm_sframes_t frames
, frames1
;
1398 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1399 char __user
*final_dst
= (char __force __user
*)buf
;
1400 if (runtime
->oss
.plugin_first
) {
1401 struct snd_pcm_plugin_channel
*channels
;
1402 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
1404 buf
= runtime
->oss
.buffer
;
1405 frames
= bytes
/ oss_frame_bytes
;
1406 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
1409 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
1412 bytes
= frames1
* oss_frame_bytes
;
1413 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
1418 frames
= bytes_to_frames(runtime
, bytes
);
1419 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
1422 bytes
= frames_to_bytes(runtime
, frames1
);
1427 static ssize_t
snd_pcm_oss_read1(struct snd_pcm_substream
*substream
, char __user
*buf
, size_t bytes
)
1431 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1433 if (atomic_read(&substream
->mmap_count
))
1436 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
1438 mutex_lock(&runtime
->oss
.params_lock
);
1440 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
1441 if (runtime
->oss
.buffer_used
== 0) {
1442 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
1445 runtime
->oss
.bytes
+= tmp
;
1446 runtime
->oss
.period_ptr
= tmp
;
1447 runtime
->oss
.buffer_used
= tmp
;
1450 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
1451 tmp
= runtime
->oss
.buffer_used
;
1452 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
)) {
1459 runtime
->oss
.buffer_used
-= tmp
;
1461 tmp
= snd_pcm_oss_read2(substream
, (char __force
*)buf
,
1462 runtime
->oss
.period_bytes
, 0);
1465 runtime
->oss
.bytes
+= tmp
;
1471 mutex_unlock(&runtime
->oss
.params_lock
);
1475 mutex_unlock(&runtime
->oss
.params_lock
);
1476 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
1479 static int snd_pcm_oss_reset(struct snd_pcm_oss_file
*pcm_oss_file
)
1481 struct snd_pcm_substream
*substream
;
1482 struct snd_pcm_runtime
*runtime
;
1485 for (i
= 0; i
< 2; i
++) {
1486 substream
= pcm_oss_file
->streams
[i
];
1489 runtime
= substream
->runtime
;
1490 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1491 runtime
->oss
.prepare
= 1;
1492 runtime
->oss
.buffer_used
= 0;
1493 runtime
->oss
.prev_hw_ptr_period
= 0;
1494 runtime
->oss
.period_ptr
= 0;
1499 static int snd_pcm_oss_post(struct snd_pcm_oss_file
*pcm_oss_file
)
1501 struct snd_pcm_substream
*substream
;
1504 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1505 if (substream
!= NULL
) {
1506 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1508 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
1510 /* note: all errors from the start action are ignored */
1511 /* OSS apps do not know, how to handle them */
1515 static int snd_pcm_oss_sync1(struct snd_pcm_substream
*substream
, size_t size
)
1517 struct snd_pcm_runtime
*runtime
;
1519 snd_pcm_state_t state
;
1521 wait_queue_entry_t wait
;
1523 runtime
= substream
->runtime
;
1524 init_waitqueue_entry(&wait
, current
);
1525 add_wait_queue(&runtime
->sleep
, &wait
);
1527 pcm_dbg(substream
->pcm
, "sync1: size = %li\n", size
);
1530 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
1532 runtime
->oss
.buffer_used
= 0;
1536 if (result
!= 0 && result
!= -EAGAIN
)
1539 set_current_state(TASK_INTERRUPTIBLE
);
1540 snd_pcm_stream_lock_irq(substream
);
1541 state
= runtime
->status
->state
;
1542 snd_pcm_stream_unlock_irq(substream
);
1543 if (state
!= SNDRV_PCM_STATE_RUNNING
) {
1544 set_current_state(TASK_RUNNING
);
1547 res
= schedule_timeout(10 * HZ
);
1548 if (signal_pending(current
)) {
1549 result
= -ERESTARTSYS
;
1553 pcm_err(substream
->pcm
,
1554 "OSS sync error - DMA timeout\n");
1559 remove_wait_queue(&runtime
->sleep
, &wait
);
1563 static int snd_pcm_oss_sync(struct snd_pcm_oss_file
*pcm_oss_file
)
1566 unsigned int saved_f_flags
;
1567 struct snd_pcm_substream
*substream
;
1568 struct snd_pcm_runtime
*runtime
;
1569 snd_pcm_format_t format
;
1570 unsigned long width
;
1573 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1574 if (substream
!= NULL
) {
1575 runtime
= substream
->runtime
;
1576 if (atomic_read(&substream
->mmap_count
))
1578 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1580 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1581 width
= snd_pcm_format_physical_width(format
);
1582 mutex_lock(&runtime
->oss
.params_lock
);
1583 if (runtime
->oss
.buffer_used
> 0) {
1585 pcm_dbg(substream
->pcm
, "sync: buffer_used\n");
1587 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1588 snd_pcm_format_set_silence(format
,
1589 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1591 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1593 mutex_unlock(&runtime
->oss
.params_lock
);
1596 } else if (runtime
->oss
.period_ptr
> 0) {
1598 pcm_dbg(substream
->pcm
, "sync: period_ptr\n");
1600 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1601 snd_pcm_format_set_silence(format
,
1602 runtime
->oss
.buffer
,
1604 err
= snd_pcm_oss_sync1(substream
, size
);
1606 mutex_unlock(&runtime
->oss
.params_lock
);
1611 * The ALSA's period might be a bit large than OSS one.
1612 * Fill the remain portion of ALSA period with zeros.
1614 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1616 size
= runtime
->period_size
- size
;
1617 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
)
1618 snd_pcm_lib_write(substream
, NULL
, size
);
1619 else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
1620 snd_pcm_lib_writev(substream
, NULL
, size
);
1622 mutex_unlock(&runtime
->oss
.params_lock
);
1624 * finish sync: drain the buffer
1627 saved_f_flags
= substream
->f_flags
;
1628 substream
->f_flags
&= ~O_NONBLOCK
;
1629 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1630 substream
->f_flags
= saved_f_flags
;
1633 runtime
->oss
.prepare
= 1;
1636 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1637 if (substream
!= NULL
) {
1638 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1640 runtime
= substream
->runtime
;
1641 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1644 runtime
->oss
.buffer_used
= 0;
1645 runtime
->oss
.prepare
= 1;
1650 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file
*pcm_oss_file
, int rate
)
1654 for (idx
= 1; idx
>= 0; --idx
) {
1655 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1656 struct snd_pcm_runtime
*runtime
;
1657 if (substream
== NULL
)
1659 runtime
= substream
->runtime
;
1662 else if (rate
> 192000)
1664 if (runtime
->oss
.rate
!= rate
) {
1665 runtime
->oss
.params
= 1;
1666 runtime
->oss
.rate
= rate
;
1669 return snd_pcm_oss_get_rate(pcm_oss_file
);
1672 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
)
1674 struct snd_pcm_substream
*substream
;
1677 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1679 return substream
->runtime
->oss
.rate
;
1682 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int channels
)
1689 for (idx
= 1; idx
>= 0; --idx
) {
1690 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1691 struct snd_pcm_runtime
*runtime
;
1692 if (substream
== NULL
)
1694 runtime
= substream
->runtime
;
1695 if (runtime
->oss
.channels
!= channels
) {
1696 runtime
->oss
.params
= 1;
1697 runtime
->oss
.channels
= channels
;
1700 return snd_pcm_oss_get_channels(pcm_oss_file
);
1703 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
)
1705 struct snd_pcm_substream
*substream
;
1708 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1710 return substream
->runtime
->oss
.channels
;
1713 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file
*pcm_oss_file
)
1715 struct snd_pcm_substream
*substream
;
1718 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1720 return substream
->runtime
->oss
.period_bytes
;
1723 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file
*pcm_oss_file
)
1725 struct snd_pcm_substream
*substream
;
1728 struct snd_pcm_hw_params
*params
;
1729 unsigned int formats
= 0;
1730 const struct snd_mask
*format_mask
;
1733 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1735 if (atomic_read(&substream
->mmap_count
))
1738 direct
= substream
->oss
.setup
.direct
;
1740 return AFMT_MU_LAW
| AFMT_U8
|
1741 AFMT_S16_LE
| AFMT_S16_BE
|
1742 AFMT_S8
| AFMT_U16_LE
|
1744 AFMT_S32_LE
| AFMT_S32_BE
|
1745 AFMT_S24_LE
| AFMT_S24_BE
|
1747 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1750 _snd_pcm_hw_params_any(params
);
1751 err
= snd_pcm_hw_refine(substream
, params
);
1752 format_mask
= hw_param_mask_c(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1756 for (fmt
= 0; fmt
< 32; ++fmt
) {
1757 if (snd_mask_test(format_mask
, fmt
)) {
1758 int f
= snd_pcm_oss_format_to(fmt
);
1766 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file
*pcm_oss_file
, int format
)
1770 if (format
!= AFMT_QUERY
) {
1771 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1774 if (!(formats
& format
))
1776 for (idx
= 1; idx
>= 0; --idx
) {
1777 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1778 struct snd_pcm_runtime
*runtime
;
1779 if (substream
== NULL
)
1781 runtime
= substream
->runtime
;
1782 if (runtime
->oss
.format
!= format
) {
1783 runtime
->oss
.params
= 1;
1784 runtime
->oss
.format
= format
;
1788 return snd_pcm_oss_get_format(pcm_oss_file
);
1791 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
)
1793 struct snd_pcm_substream
*substream
;
1796 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1798 return substream
->runtime
->oss
.format
;
1801 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream
*substream
, int subdivide
)
1803 struct snd_pcm_runtime
*runtime
;
1805 if (substream
== NULL
)
1807 runtime
= substream
->runtime
;
1808 if (subdivide
== 0) {
1809 subdivide
= runtime
->oss
.subdivision
;
1814 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1816 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1817 subdivide
!= 8 && subdivide
!= 16)
1819 runtime
->oss
.subdivision
= subdivide
;
1820 runtime
->oss
.params
= 1;
1824 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file
*pcm_oss_file
, int subdivide
)
1826 int err
= -EINVAL
, idx
;
1828 for (idx
= 1; idx
>= 0; --idx
) {
1829 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1830 if (substream
== NULL
)
1832 if ((err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
)) < 0)
1838 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream
*substream
, unsigned int val
)
1840 struct snd_pcm_runtime
*runtime
;
1842 if (substream
== NULL
)
1844 runtime
= substream
->runtime
;
1845 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1847 runtime
->oss
.fragshift
= val
& 0xffff;
1848 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
1849 if (runtime
->oss
.fragshift
< 4) /* < 16 */
1850 runtime
->oss
.fragshift
= 4;
1851 if (runtime
->oss
.maxfrags
< 2)
1852 runtime
->oss
.maxfrags
= 2;
1853 runtime
->oss
.params
= 1;
1857 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int val
)
1859 int err
= -EINVAL
, idx
;
1861 for (idx
= 1; idx
>= 0; --idx
) {
1862 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1863 if (substream
== NULL
)
1865 if ((err
= snd_pcm_oss_set_fragment1(substream
, val
)) < 0)
1871 static int snd_pcm_oss_nonblock(struct file
* file
)
1873 spin_lock(&file
->f_lock
);
1874 file
->f_flags
|= O_NONBLOCK
;
1875 spin_unlock(&file
->f_lock
);
1879 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream
*substream
, int res
)
1882 if (substream
== NULL
) {
1883 res
&= ~DSP_CAP_DUPLEX
;
1886 #ifdef DSP_CAP_MULTI
1887 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1888 if (substream
->pstr
->substream_count
> 1)
1889 res
|= DSP_CAP_MULTI
;
1891 /* DSP_CAP_REALTIME is set all times: */
1892 /* all ALSA drivers can return actual pointer in ring buffer */
1893 #if defined(DSP_CAP_REALTIME) && 0
1895 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1896 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
1897 res
&= ~DSP_CAP_REALTIME
;
1903 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file
*pcm_oss_file
)
1907 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
1908 for (idx
= 0; idx
< 2; idx
++) {
1909 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1910 result
= snd_pcm_oss_get_caps1(substream
, result
);
1912 result
|= 0x0001; /* revision - same as SB AWE 64 */
1916 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream
*substream
,
1917 snd_pcm_uframes_t hw_ptr
)
1919 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1920 snd_pcm_uframes_t appl_ptr
;
1921 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
1922 appl_ptr
%= runtime
->boundary
;
1923 runtime
->control
->appl_ptr
= appl_ptr
;
1926 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file
*pcm_oss_file
, int trigger
)
1928 struct snd_pcm_runtime
*runtime
;
1929 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1933 pcm_dbg(substream
->pcm
, "pcm_oss: trigger = 0x%x\n", trigger
);
1936 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1937 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1940 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
1944 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
1948 runtime
= psubstream
->runtime
;
1949 if (trigger
& PCM_ENABLE_OUTPUT
) {
1950 if (runtime
->oss
.trigger
)
1952 if (atomic_read(&psubstream
->mmap_count
))
1953 snd_pcm_oss_simulate_fill(psubstream
,
1954 get_hw_ptr_period(runtime
));
1955 runtime
->oss
.trigger
= 1;
1956 runtime
->start_threshold
= 1;
1957 cmd
= SNDRV_PCM_IOCTL_START
;
1959 if (!runtime
->oss
.trigger
)
1961 runtime
->oss
.trigger
= 0;
1962 runtime
->start_threshold
= runtime
->boundary
;
1963 cmd
= SNDRV_PCM_IOCTL_DROP
;
1964 runtime
->oss
.prepare
= 1;
1966 err
= snd_pcm_kernel_ioctl(psubstream
, cmd
, NULL
);
1972 runtime
= csubstream
->runtime
;
1973 if (trigger
& PCM_ENABLE_INPUT
) {
1974 if (runtime
->oss
.trigger
)
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(csubstream
, cmd
, NULL
);
1995 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file
*pcm_oss_file
)
1997 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2000 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2001 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2002 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
2003 result
|= PCM_ENABLE_OUTPUT
;
2004 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
2005 result
|= PCM_ENABLE_INPUT
;
2009 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file
*pcm_oss_file
)
2011 struct snd_pcm_substream
*substream
;
2012 struct snd_pcm_runtime
*runtime
;
2013 snd_pcm_sframes_t delay
;
2016 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2017 if (substream
== NULL
)
2019 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2021 runtime
= substream
->runtime
;
2022 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
2024 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2026 delay
= 0; /* hack for broken OSS applications */
2029 return snd_pcm_oss_bytes(substream
, delay
);
2032 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
2034 struct snd_pcm_substream
*substream
;
2035 struct snd_pcm_runtime
*runtime
;
2036 snd_pcm_sframes_t delay
;
2038 struct count_info info
;
2043 substream
= pcm_oss_file
->streams
[stream
];
2044 if (substream
== NULL
)
2046 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
2048 runtime
= substream
->runtime
;
2049 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
2050 memset(&info
, 0, sizeof(info
));
2051 if (copy_to_user(_info
, &info
, sizeof(info
)))
2055 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2056 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
2057 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
2062 fixup
= runtime
->oss
.buffer_used
;
2065 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
2066 fixup
= -runtime
->oss
.buffer_used
;
2070 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
2071 if (atomic_read(&substream
->mmap_count
)) {
2072 snd_pcm_sframes_t n
;
2073 delay
= get_hw_ptr_period(runtime
);
2074 n
= delay
- runtime
->oss
.prev_hw_ptr_period
;
2076 n
+= runtime
->boundary
;
2077 info
.blocks
= n
/ runtime
->period_size
;
2078 runtime
->oss
.prev_hw_ptr_period
= delay
;
2079 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2080 snd_pcm_oss_simulate_fill(substream
, delay
);
2081 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
2083 delay
= snd_pcm_oss_bytes(substream
, delay
);
2084 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2085 if (substream
->oss
.setup
.buggyptr
)
2086 info
.blocks
= (runtime
->oss
.buffer_bytes
- delay
- fixup
) / runtime
->oss
.period_bytes
;
2088 info
.blocks
= (delay
+ fixup
) / runtime
->oss
.period_bytes
;
2089 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
2092 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
2093 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
2096 if (copy_to_user(_info
, &info
, sizeof(info
)))
2101 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
2103 struct snd_pcm_substream
*substream
;
2104 struct snd_pcm_runtime
*runtime
;
2105 snd_pcm_sframes_t avail
;
2107 struct audio_buf_info info
;
2112 substream
= pcm_oss_file
->streams
[stream
];
2113 if (substream
== NULL
)
2115 runtime
= substream
->runtime
;
2117 if (runtime
->oss
.params
&&
2118 (err
= snd_pcm_oss_change_params(substream
, false)) < 0)
2121 info
.fragsize
= runtime
->oss
.period_bytes
;
2122 info
.fragstotal
= runtime
->periods
;
2123 if (runtime
->oss
.prepare
) {
2124 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2125 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
2126 info
.fragments
= runtime
->oss
.periods
;
2132 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2133 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
2134 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
2135 avail
= runtime
->buffer_size
;
2139 avail
= runtime
->buffer_size
- avail
;
2140 fixup
= -runtime
->oss
.buffer_used
;
2143 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
2144 fixup
= runtime
->oss
.buffer_used
;
2148 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
2149 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
2153 pcm_dbg(substream
->pcm
,
2154 "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2155 info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
2157 if (copy_to_user(_info
, &info
, sizeof(info
)))
2162 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
2164 // it won't be probably implemented
2165 // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2169 static const char *strip_task_path(const char *path
)
2171 const char *ptr
, *ptrl
= NULL
;
2172 for (ptr
= path
; *ptr
; ptr
++) {
2179 static void snd_pcm_oss_look_for_setup(struct snd_pcm
*pcm
, int stream
,
2180 const char *task_name
,
2181 struct snd_pcm_oss_setup
*rsetup
)
2183 struct snd_pcm_oss_setup
*setup
;
2185 mutex_lock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2187 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
;
2188 setup
= setup
->next
) {
2189 if (!strcmp(setup
->task_name
, task_name
))
2192 } while ((task_name
= strip_task_path(task_name
)) != NULL
);
2196 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
2199 static void snd_pcm_oss_release_substream(struct snd_pcm_substream
*substream
)
2201 struct snd_pcm_runtime
*runtime
;
2202 runtime
= substream
->runtime
;
2203 vfree(runtime
->oss
.buffer
);
2204 runtime
->oss
.buffer
= NULL
;
2205 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2206 snd_pcm_oss_plugin_clear(substream
);
2208 substream
->oss
.oss
= 0;
2211 static void snd_pcm_oss_init_substream(struct snd_pcm_substream
*substream
,
2212 struct snd_pcm_oss_setup
*setup
,
2215 struct snd_pcm_runtime
*runtime
;
2217 substream
->oss
.oss
= 1;
2218 substream
->oss
.setup
= *setup
;
2219 if (setup
->nonblock
)
2220 substream
->f_flags
|= O_NONBLOCK
;
2221 else if (setup
->block
)
2222 substream
->f_flags
&= ~O_NONBLOCK
;
2223 runtime
= substream
->runtime
;
2224 runtime
->oss
.params
= 1;
2225 runtime
->oss
.trigger
= 1;
2226 runtime
->oss
.rate
= 8000;
2227 mutex_init(&runtime
->oss
.params_lock
);
2228 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
2229 case SNDRV_MINOR_OSS_PCM_8
:
2230 runtime
->oss
.format
= AFMT_U8
;
2232 case SNDRV_MINOR_OSS_PCM_16
:
2233 runtime
->oss
.format
= AFMT_S16_LE
;
2236 runtime
->oss
.format
= AFMT_MU_LAW
;
2238 runtime
->oss
.channels
= 1;
2239 runtime
->oss
.fragshift
= 0;
2240 runtime
->oss
.maxfrags
= 0;
2241 runtime
->oss
.subdivision
= 0;
2242 substream
->pcm_release
= snd_pcm_oss_release_substream
;
2245 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file
*pcm_oss_file
)
2250 for (cidx
= 0; cidx
< 2; ++cidx
) {
2251 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[cidx
];
2253 snd_pcm_release_substream(substream
);
2255 kfree(pcm_oss_file
);
2259 static int snd_pcm_oss_open_file(struct file
*file
,
2260 struct snd_pcm
*pcm
,
2261 struct snd_pcm_oss_file
**rpcm_oss_file
,
2263 struct snd_pcm_oss_setup
*setup
)
2266 struct snd_pcm_oss_file
*pcm_oss_file
;
2267 struct snd_pcm_substream
*substream
;
2268 fmode_t f_mode
= file
->f_mode
;
2271 *rpcm_oss_file
= NULL
;
2273 pcm_oss_file
= kzalloc(sizeof(*pcm_oss_file
), GFP_KERNEL
);
2274 if (pcm_oss_file
== NULL
)
2277 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
2278 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
2279 f_mode
= FMODE_WRITE
;
2281 file
->f_flags
&= ~O_APPEND
;
2282 for (idx
= 0; idx
< 2; idx
++) {
2283 if (setup
[idx
].disable
)
2285 if (! pcm
->streams
[idx
].substream_count
)
2286 continue; /* no matching substream */
2287 if (idx
== SNDRV_PCM_STREAM_PLAYBACK
) {
2288 if (! (f_mode
& FMODE_WRITE
))
2291 if (! (f_mode
& FMODE_READ
))
2294 err
= snd_pcm_open_substream(pcm
, idx
, file
, &substream
);
2296 snd_pcm_oss_release_file(pcm_oss_file
);
2300 pcm_oss_file
->streams
[idx
] = substream
;
2301 substream
->file
= pcm_oss_file
;
2302 snd_pcm_oss_init_substream(substream
, &setup
[idx
], minor
);
2305 if (!pcm_oss_file
->streams
[0] && !pcm_oss_file
->streams
[1]) {
2306 snd_pcm_oss_release_file(pcm_oss_file
);
2310 file
->private_data
= pcm_oss_file
;
2312 *rpcm_oss_file
= pcm_oss_file
;
2317 static int snd_task_name(struct task_struct
*task
, char *name
, size_t size
)
2321 if (snd_BUG_ON(!task
|| !name
|| size
< 2))
2323 for (idx
= 0; idx
< sizeof(task
->comm
) && idx
+ 1 < size
; idx
++)
2324 name
[idx
] = task
->comm
[idx
];
2329 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
2333 struct snd_pcm
*pcm
;
2334 struct snd_pcm_oss_file
*pcm_oss_file
;
2335 struct snd_pcm_oss_setup setup
[2];
2337 wait_queue_entry_t wait
;
2339 err
= nonseekable_open(inode
, file
);
2343 pcm
= snd_lookup_oss_minor_data(iminor(inode
),
2344 SNDRV_OSS_DEVICE_TYPE_PCM
);
2349 err
= snd_card_file_add(pcm
->card
, file
);
2352 if (!try_module_get(pcm
->card
->module
)) {
2356 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
2360 memset(setup
, 0, sizeof(setup
));
2361 if (file
->f_mode
& FMODE_WRITE
)
2362 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
2363 task_name
, &setup
[0]);
2364 if (file
->f_mode
& FMODE_READ
)
2365 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
2366 task_name
, &setup
[1]);
2368 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
2370 nonblock
= nonblock_open
;
2372 init_waitqueue_entry(&wait
, current
);
2373 add_wait_queue(&pcm
->open_wait
, &wait
);
2374 mutex_lock(&pcm
->open_mutex
);
2376 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
2377 iminor(inode
), setup
);
2380 if (err
== -EAGAIN
) {
2387 set_current_state(TASK_INTERRUPTIBLE
);
2388 mutex_unlock(&pcm
->open_mutex
);
2390 mutex_lock(&pcm
->open_mutex
);
2391 if (pcm
->card
->shutdown
) {
2395 if (signal_pending(current
)) {
2400 remove_wait_queue(&pcm
->open_wait
, &wait
);
2401 mutex_unlock(&pcm
->open_mutex
);
2404 snd_card_unref(pcm
->card
);
2408 module_put(pcm
->card
->module
);
2410 snd_card_file_remove(pcm
->card
, file
);
2413 snd_card_unref(pcm
->card
);
2417 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
2419 struct snd_pcm
*pcm
;
2420 struct snd_pcm_substream
*substream
;
2421 struct snd_pcm_oss_file
*pcm_oss_file
;
2423 pcm_oss_file
= file
->private_data
;
2424 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2425 if (substream
== NULL
)
2426 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2427 if (snd_BUG_ON(!substream
))
2429 pcm
= substream
->pcm
;
2430 if (!pcm
->card
->shutdown
)
2431 snd_pcm_oss_sync(pcm_oss_file
);
2432 mutex_lock(&pcm
->open_mutex
);
2433 snd_pcm_oss_release_file(pcm_oss_file
);
2434 mutex_unlock(&pcm
->open_mutex
);
2435 wake_up(&pcm
->open_wait
);
2436 module_put(pcm
->card
->module
);
2437 snd_card_file_remove(pcm
->card
, file
);
2441 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2443 struct snd_pcm_oss_file
*pcm_oss_file
;
2444 int __user
*p
= (int __user
*)arg
;
2447 pcm_oss_file
= file
->private_data
;
2448 if (cmd
== OSS_GETVERSION
)
2449 return put_user(SNDRV_OSS_VERSION
, p
);
2450 if (cmd
== OSS_ALSAEMULVER
)
2451 return put_user(1, p
);
2452 #if IS_REACHABLE(CONFIG_SND_MIXER_OSS)
2453 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
2454 struct snd_pcm_substream
*substream
;
2456 for (idx
= 0; idx
< 2; ++idx
) {
2457 substream
= pcm_oss_file
->streams
[idx
];
2458 if (substream
!= NULL
)
2461 if (snd_BUG_ON(idx
>= 2))
2463 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
2466 if (((cmd
>> 8) & 0xff) != 'P')
2469 pr_debug("pcm_oss: ioctl = 0x%x\n", cmd
);
2472 case SNDCTL_DSP_RESET
:
2473 return snd_pcm_oss_reset(pcm_oss_file
);
2474 case SNDCTL_DSP_SYNC
:
2475 return snd_pcm_oss_sync(pcm_oss_file
);
2476 case SNDCTL_DSP_SPEED
:
2477 if (get_user(res
, p
))
2479 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
2481 return put_user(res
, p
);
2482 case SOUND_PCM_READ_RATE
:
2483 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
2486 return put_user(res
, p
);
2487 case SNDCTL_DSP_STEREO
:
2488 if (get_user(res
, p
))
2490 res
= res
> 0 ? 2 : 1;
2491 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
2493 return put_user(--res
, p
);
2494 case SNDCTL_DSP_GETBLKSIZE
:
2495 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
2498 return put_user(res
, p
);
2499 case SNDCTL_DSP_SETFMT
:
2500 if (get_user(res
, p
))
2502 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
2505 return put_user(res
, p
);
2506 case SOUND_PCM_READ_BITS
:
2507 res
= snd_pcm_oss_get_format(pcm_oss_file
);
2510 return put_user(res
, p
);
2511 case SNDCTL_DSP_CHANNELS
:
2512 if (get_user(res
, p
))
2514 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
2517 return put_user(res
, p
);
2518 case SOUND_PCM_READ_CHANNELS
:
2519 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
2522 return put_user(res
, p
);
2523 case SOUND_PCM_WRITE_FILTER
:
2524 case SOUND_PCM_READ_FILTER
:
2526 case SNDCTL_DSP_POST
:
2527 return snd_pcm_oss_post(pcm_oss_file
);
2528 case SNDCTL_DSP_SUBDIVIDE
:
2529 if (get_user(res
, p
))
2531 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
2534 return put_user(res
, p
);
2535 case SNDCTL_DSP_SETFRAGMENT
:
2536 if (get_user(res
, p
))
2538 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
2539 case SNDCTL_DSP_GETFMTS
:
2540 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
2543 return put_user(res
, p
);
2544 case SNDCTL_DSP_GETOSPACE
:
2545 case SNDCTL_DSP_GETISPACE
:
2546 return snd_pcm_oss_get_space(pcm_oss_file
,
2547 cmd
== SNDCTL_DSP_GETISPACE
?
2548 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2549 (struct audio_buf_info __user
*) arg
);
2550 case SNDCTL_DSP_NONBLOCK
:
2551 return snd_pcm_oss_nonblock(file
);
2552 case SNDCTL_DSP_GETCAPS
:
2553 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
2556 return put_user(res
, p
);
2557 case SNDCTL_DSP_GETTRIGGER
:
2558 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2561 return put_user(res
, p
);
2562 case SNDCTL_DSP_SETTRIGGER
:
2563 if (get_user(res
, p
))
2565 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2566 case SNDCTL_DSP_GETIPTR
:
2567 case SNDCTL_DSP_GETOPTR
:
2568 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2569 cmd
== SNDCTL_DSP_GETIPTR
?
2570 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2571 (struct count_info __user
*) arg
);
2572 case SNDCTL_DSP_MAPINBUF
:
2573 case SNDCTL_DSP_MAPOUTBUF
:
2574 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2575 cmd
== SNDCTL_DSP_MAPINBUF
?
2576 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2577 (struct buffmem_desc __user
*) arg
);
2578 case SNDCTL_DSP_SETSYNCRO
:
2579 /* stop DMA now.. */
2581 case SNDCTL_DSP_SETDUPLEX
:
2582 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2585 case SNDCTL_DSP_GETODELAY
:
2586 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2588 /* it's for sure, some broken apps don't check for error codes */
2592 return put_user(res
, p
);
2593 case SNDCTL_DSP_PROFILE
:
2594 return 0; /* silently ignore */
2596 pr_debug("pcm_oss: unknown command = 0x%x\n", cmd
);
2601 #ifdef CONFIG_COMPAT
2602 /* all compatible */
2603 static long snd_pcm_oss_ioctl_compat(struct file
*file
, unsigned int cmd
,
2606 return snd_pcm_oss_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
2609 #define snd_pcm_oss_ioctl_compat NULL
2612 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2614 struct snd_pcm_oss_file
*pcm_oss_file
;
2615 struct snd_pcm_substream
*substream
;
2617 pcm_oss_file
= file
->private_data
;
2618 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2619 if (substream
== NULL
)
2621 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2623 return snd_pcm_oss_read1(substream
, buf
, count
);
2626 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2627 pcm_dbg(substream
->pcm
,
2628 "pcm_oss: read %li bytes (returned %li bytes)\n",
2629 (long)count
, (long)res
);
2635 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2637 struct snd_pcm_oss_file
*pcm_oss_file
;
2638 struct snd_pcm_substream
*substream
;
2641 pcm_oss_file
= file
->private_data
;
2642 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2643 if (substream
== NULL
)
2645 substream
->f_flags
= file
->f_flags
& O_NONBLOCK
;
2646 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2648 pcm_dbg(substream
->pcm
, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2649 (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_period
!=
2659 get_hw_ptr_period(runtime
);
2661 return snd_pcm_playback_avail(runtime
) >=
2662 runtime
->oss
.period_frames
;
2665 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream
*substream
)
2667 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2668 if (atomic_read(&substream
->mmap_count
))
2669 return runtime
->oss
.prev_hw_ptr_period
!=
2670 get_hw_ptr_period(runtime
);
2672 return snd_pcm_capture_avail(runtime
) >=
2673 runtime
->oss
.period_frames
;
2676 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2678 struct snd_pcm_oss_file
*pcm_oss_file
;
2680 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2682 pcm_oss_file
= file
->private_data
;
2684 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2685 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2688 if (psubstream
!= NULL
) {
2689 struct snd_pcm_runtime
*runtime
= psubstream
->runtime
;
2690 poll_wait(file
, &runtime
->sleep
, wait
);
2691 snd_pcm_stream_lock_irq(psubstream
);
2692 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2693 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2694 snd_pcm_oss_playback_ready(psubstream
)))
2695 mask
|= POLLOUT
| POLLWRNORM
;
2696 snd_pcm_stream_unlock_irq(psubstream
);
2698 if (csubstream
!= NULL
) {
2699 struct snd_pcm_runtime
*runtime
= csubstream
->runtime
;
2700 snd_pcm_state_t ostate
;
2701 poll_wait(file
, &runtime
->sleep
, wait
);
2702 snd_pcm_stream_lock_irq(csubstream
);
2703 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2704 snd_pcm_oss_capture_ready(csubstream
))
2705 mask
|= POLLIN
| POLLRDNORM
;
2706 snd_pcm_stream_unlock_irq(csubstream
);
2707 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2708 struct snd_pcm_oss_file ofile
;
2709 memset(&ofile
, 0, sizeof(ofile
));
2710 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2711 runtime
->oss
.trigger
= 0;
2712 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2719 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2721 struct snd_pcm_oss_file
*pcm_oss_file
;
2722 struct snd_pcm_substream
*substream
= NULL
;
2723 struct snd_pcm_runtime
*runtime
;
2727 pr_debug("pcm_oss: mmap begin\n");
2729 pcm_oss_file
= file
->private_data
;
2730 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2731 case VM_READ
| VM_WRITE
:
2732 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2737 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2740 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2745 /* set VM_READ access as well to fix memset() routines that do
2746 reads before writes (to improve performance) */
2747 area
->vm_flags
|= VM_READ
;
2748 if (substream
== NULL
)
2750 runtime
= substream
->runtime
;
2751 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2753 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2754 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2758 if (runtime
->oss
.params
) {
2759 /* use mutex_trylock() for params_lock for avoiding a deadlock
2760 * between mmap_sem and params_lock taken by
2761 * copy_from/to_user() in snd_pcm_oss_write/read()
2763 err
= snd_pcm_oss_change_params(substream
, true);
2767 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2768 if (runtime
->oss
.plugin_first
!= NULL
)
2772 if (area
->vm_pgoff
!= 0)
2775 err
= snd_pcm_mmap_data(substream
, file
, area
);
2778 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2779 runtime
->silence_threshold
= 0;
2780 runtime
->silence_size
= 0;
2782 pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2783 runtime
->oss
.mmap_bytes
);
2785 /* In mmap mode we never stop */
2786 runtime
->stop_threshold
= runtime
->boundary
;
2791 #ifdef CONFIG_SND_VERBOSE_PROCFS
2796 static void snd_pcm_oss_proc_read(struct snd_info_entry
*entry
,
2797 struct snd_info_buffer
*buffer
)
2799 struct snd_pcm_str
*pstr
= entry
->private_data
;
2800 struct snd_pcm_oss_setup
*setup
= pstr
->oss
.setup_list
;
2801 mutex_lock(&pstr
->oss
.setup_mutex
);
2803 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2807 setup
->disable
? " disable" : "",
2808 setup
->direct
? " direct" : "",
2809 setup
->block
? " block" : "",
2810 setup
->nonblock
? " non-block" : "",
2811 setup
->partialfrag
? " partial-frag" : "",
2812 setup
->nosilence
? " no-silence" : "");
2813 setup
= setup
->next
;
2815 mutex_unlock(&pstr
->oss
.setup_mutex
);
2818 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str
* pstr
)
2820 struct snd_pcm_oss_setup
*setup
, *setupn
;
2822 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
2823 setup
; setup
= setupn
) {
2824 setupn
= setup
->next
;
2825 kfree(setup
->task_name
);
2828 pstr
->oss
.setup_list
= NULL
;
2831 static void snd_pcm_oss_proc_write(struct snd_info_entry
*entry
,
2832 struct snd_info_buffer
*buffer
)
2834 struct snd_pcm_str
*pstr
= entry
->private_data
;
2835 char line
[128], str
[32], task_name
[32];
2838 struct snd_pcm_oss_setup
*setup
, *setup1
, template;
2840 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
2841 mutex_lock(&pstr
->oss
.setup_mutex
);
2842 memset(&template, 0, sizeof(template));
2843 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
2844 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
2845 snd_pcm_oss_proc_free_setup_list(pstr
);
2846 mutex_unlock(&pstr
->oss
.setup_mutex
);
2849 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
2850 if (!strcmp(setup
->task_name
, task_name
)) {
2855 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2856 template.periods
= simple_strtoul(str
, NULL
, 10);
2857 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2858 template.period_size
= simple_strtoul(str
, NULL
, 10);
2859 for (idx1
= 31; idx1
>= 0; idx1
--)
2860 if (template.period_size
& (1 << idx1
))
2862 for (idx1
--; idx1
>= 0; idx1
--)
2863 template.period_size
&= ~(1 << idx1
);
2865 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2866 if (!strcmp(str
, "disable")) {
2867 template.disable
= 1;
2868 } else if (!strcmp(str
, "direct")) {
2869 template.direct
= 1;
2870 } else if (!strcmp(str
, "block")) {
2872 } else if (!strcmp(str
, "non-block")) {
2873 template.nonblock
= 1;
2874 } else if (!strcmp(str
, "partial-frag")) {
2875 template.partialfrag
= 1;
2876 } else if (!strcmp(str
, "no-silence")) {
2877 template.nosilence
= 1;
2878 } else if (!strcmp(str
, "buggy-ptr")) {
2879 template.buggyptr
= 1;
2882 if (setup
== NULL
) {
2883 setup
= kmalloc(sizeof(*setup
), GFP_KERNEL
);
2885 buffer
->error
= -ENOMEM
;
2886 mutex_unlock(&pstr
->oss
.setup_mutex
);
2889 if (pstr
->oss
.setup_list
== NULL
)
2890 pstr
->oss
.setup_list
= setup
;
2892 for (setup1
= pstr
->oss
.setup_list
;
2893 setup1
->next
; setup1
= setup1
->next
);
2894 setup1
->next
= setup
;
2896 template.task_name
= kstrdup(task_name
, GFP_KERNEL
);
2897 if (! template.task_name
) {
2899 buffer
->error
= -ENOMEM
;
2900 mutex_unlock(&pstr
->oss
.setup_mutex
);
2905 mutex_unlock(&pstr
->oss
.setup_mutex
);
2909 static void snd_pcm_oss_proc_init(struct snd_pcm
*pcm
)
2912 for (stream
= 0; stream
< 2; ++stream
) {
2913 struct snd_info_entry
*entry
;
2914 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2915 if (pstr
->substream_count
== 0)
2917 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
2918 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
2919 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2920 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
2921 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
2922 entry
->private_data
= pstr
;
2923 if (snd_info_register(entry
) < 0) {
2924 snd_info_free_entry(entry
);
2928 pstr
->oss
.proc_entry
= entry
;
2932 static void snd_pcm_oss_proc_done(struct snd_pcm
*pcm
)
2935 for (stream
= 0; stream
< 2; ++stream
) {
2936 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2937 snd_info_free_entry(pstr
->oss
.proc_entry
);
2938 pstr
->oss
.proc_entry
= NULL
;
2939 snd_pcm_oss_proc_free_setup_list(pstr
);
2942 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2943 #define snd_pcm_oss_proc_init(pcm)
2944 #define snd_pcm_oss_proc_done(pcm)
2945 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2951 static const struct file_operations snd_pcm_oss_f_reg
=
2953 .owner
= THIS_MODULE
,
2954 .read
= snd_pcm_oss_read
,
2955 .write
= snd_pcm_oss_write
,
2956 .open
= snd_pcm_oss_open
,
2957 .release
= snd_pcm_oss_release
,
2958 .llseek
= no_llseek
,
2959 .poll
= snd_pcm_oss_poll
,
2960 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
2961 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
2962 .mmap
= snd_pcm_oss_mmap
,
2965 static void register_oss_dsp(struct snd_pcm
*pcm
, int index
)
2967 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2968 pcm
->card
, index
, &snd_pcm_oss_f_reg
,
2970 pcm_err(pcm
, "unable to register OSS PCM device %i:%i\n",
2971 pcm
->card
->number
, pcm
->device
);
2975 static int snd_pcm_oss_register_minor(struct snd_pcm
*pcm
)
2978 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2981 register_oss_dsp(pcm
, 0);
2982 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
2983 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
2984 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
2985 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
2986 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2987 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
2992 pcm
->oss
.reg_mask
|= 1;
2994 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2995 register_oss_dsp(pcm
, 1);
2997 pcm
->oss
.reg_mask
|= 2;
3001 snd_pcm_oss_proc_init(pcm
);
3006 static int snd_pcm_oss_disconnect_minor(struct snd_pcm
*pcm
)
3009 if (pcm
->oss
.reg_mask
& 1) {
3010 pcm
->oss
.reg_mask
&= ~1;
3011 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3014 if (pcm
->oss
.reg_mask
& 2) {
3015 pcm
->oss
.reg_mask
&= ~2;
3016 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
3019 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
3020 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
3021 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
3029 static int snd_pcm_oss_unregister_minor(struct snd_pcm
*pcm
)
3031 snd_pcm_oss_disconnect_minor(pcm
);
3032 snd_pcm_oss_proc_done(pcm
);
3036 static struct snd_pcm_notify snd_pcm_oss_notify
=
3038 .n_register
= snd_pcm_oss_register_minor
,
3039 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
3040 .n_unregister
= snd_pcm_oss_unregister_minor
,
3043 static int __init
alsa_pcm_oss_init(void)
3048 /* check device map table */
3049 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
3050 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3051 pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3055 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
3056 pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3061 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
3066 static void __exit
alsa_pcm_oss_exit(void)
3068 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
3071 module_init(alsa_pcm_oss_init
)
3072 module_exit(alsa_pcm_oss_exit
)