2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/vmalloc.h>
35 #include <linux/moduleparam.h>
36 #include <linux/string.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>
46 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
48 static int dsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 0};
49 static int adsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
50 static int nonblock_open
= 1;
52 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54 MODULE_LICENSE("GPL");
55 module_param_array(dsp_map
, int, NULL
, 0444);
56 MODULE_PARM_DESC(dsp_map
, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map
, int, NULL
, 0444);
58 MODULE_PARM_DESC(adsp_map
, "PCM device number assigned to 2nd OSS device.");
59 module_param(nonblock_open
, bool, 0644);
60 MODULE_PARM_DESC(nonblock_open
, "Don't block opening busy PCM devices.");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM
);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1
);
64 extern int snd_mixer_oss_ioctl_card(struct snd_card
*card
, unsigned int cmd
, unsigned long arg
);
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
)
81 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
82 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream
*substream
)
84 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
85 struct snd_pcm_plugin
*plugin
, *next
;
87 plugin
= runtime
->oss
.plugin_first
;
90 snd_pcm_plugin_free(plugin
);
93 runtime
->oss
.plugin_first
= runtime
->oss
.plugin_last
= NULL
;
97 static int snd_pcm_plugin_insert(struct snd_pcm_plugin
*plugin
)
99 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
100 plugin
->next
= runtime
->oss
.plugin_first
;
102 if (runtime
->oss
.plugin_first
) {
103 runtime
->oss
.plugin_first
->prev
= plugin
;
104 runtime
->oss
.plugin_first
= plugin
;
106 runtime
->oss
.plugin_last
=
107 runtime
->oss
.plugin_first
= plugin
;
112 int snd_pcm_plugin_append(struct snd_pcm_plugin
*plugin
)
114 struct snd_pcm_runtime
*runtime
= plugin
->plug
->runtime
;
116 plugin
->prev
= runtime
->oss
.plugin_last
;
117 if (runtime
->oss
.plugin_last
) {
118 runtime
->oss
.plugin_last
->next
= plugin
;
119 runtime
->oss
.plugin_last
= plugin
;
121 runtime
->oss
.plugin_last
=
122 runtime
->oss
.plugin_first
= plugin
;
126 #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
128 static long snd_pcm_oss_bytes(struct snd_pcm_substream
*substream
, long frames
)
130 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
131 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
132 long bytes
= frames_to_bytes(runtime
, frames
);
133 if (buffer_size
== runtime
->oss
.buffer_bytes
)
135 #if BITS_PER_LONG >= 64
136 return runtime
->oss
.buffer_bytes
* bytes
/ buffer_size
;
139 u64 bsize
= (u64
)runtime
->oss
.buffer_bytes
* (u64
)bytes
;
141 div64_32(&bsize
, buffer_size
, &rem
);
147 static long snd_pcm_alsa_frames(struct snd_pcm_substream
*substream
, long bytes
)
149 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
150 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
151 if (buffer_size
== runtime
->oss
.buffer_bytes
)
152 return bytes_to_frames(runtime
, bytes
);
153 return bytes_to_frames(runtime
, (buffer_size
* bytes
) / runtime
->oss
.buffer_bytes
);
156 static int snd_pcm_oss_format_from(int format
)
159 case AFMT_MU_LAW
: return SNDRV_PCM_FORMAT_MU_LAW
;
160 case AFMT_A_LAW
: return SNDRV_PCM_FORMAT_A_LAW
;
161 case AFMT_IMA_ADPCM
: return SNDRV_PCM_FORMAT_IMA_ADPCM
;
162 case AFMT_U8
: return SNDRV_PCM_FORMAT_U8
;
163 case AFMT_S16_LE
: return SNDRV_PCM_FORMAT_S16_LE
;
164 case AFMT_S16_BE
: return SNDRV_PCM_FORMAT_S16_BE
;
165 case AFMT_S8
: return SNDRV_PCM_FORMAT_S8
;
166 case AFMT_U16_LE
: return SNDRV_PCM_FORMAT_U16_LE
;
167 case AFMT_U16_BE
: return SNDRV_PCM_FORMAT_U16_BE
;
168 case AFMT_MPEG
: return SNDRV_PCM_FORMAT_MPEG
;
169 default: return SNDRV_PCM_FORMAT_U8
;
173 static int snd_pcm_oss_format_to(int format
)
176 case SNDRV_PCM_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
177 case SNDRV_PCM_FORMAT_A_LAW
: return AFMT_A_LAW
;
178 case SNDRV_PCM_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
179 case SNDRV_PCM_FORMAT_U8
: return AFMT_U8
;
180 case SNDRV_PCM_FORMAT_S16_LE
: return AFMT_S16_LE
;
181 case SNDRV_PCM_FORMAT_S16_BE
: return AFMT_S16_BE
;
182 case SNDRV_PCM_FORMAT_S8
: return AFMT_S8
;
183 case SNDRV_PCM_FORMAT_U16_LE
: return AFMT_U16_LE
;
184 case SNDRV_PCM_FORMAT_U16_BE
: return AFMT_U16_BE
;
185 case SNDRV_PCM_FORMAT_MPEG
: return AFMT_MPEG
;
186 default: return -EINVAL
;
190 static int snd_pcm_oss_period_size(struct snd_pcm_substream
*substream
,
191 struct snd_pcm_hw_params
*oss_params
,
192 struct snd_pcm_hw_params
*slave_params
)
195 size_t oss_buffer_size
, oss_period_size
, oss_periods
;
196 size_t min_period_size
, max_period_size
;
197 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
198 size_t oss_frame_size
;
200 oss_frame_size
= snd_pcm_format_physical_width(params_format(oss_params
)) *
201 params_channels(oss_params
) / 8;
203 oss_buffer_size
= snd_pcm_plug_client_size(substream
,
204 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
)) * oss_frame_size
;
205 oss_buffer_size
= 1 << ld2(oss_buffer_size
);
206 if (atomic_read(&runtime
->mmap_count
)) {
207 if (oss_buffer_size
> runtime
->oss
.mmap_bytes
)
208 oss_buffer_size
= runtime
->oss
.mmap_bytes
;
211 if (substream
->oss
.setup
&&
212 substream
->oss
.setup
->period_size
> 16)
213 oss_period_size
= substream
->oss
.setup
->period_size
;
214 else if (runtime
->oss
.fragshift
) {
215 oss_period_size
= 1 << runtime
->oss
.fragshift
;
216 if (oss_period_size
> oss_buffer_size
/ 2)
217 oss_period_size
= oss_buffer_size
/ 2;
220 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
222 oss_period_size
= oss_buffer_size
;
224 oss_period_size
/= 2;
225 } while (oss_period_size
> bytes_per_sec
);
226 if (runtime
->oss
.subdivision
== 0) {
228 if (oss_period_size
/ sd
> 4096)
230 if (oss_period_size
/ sd
< 4096)
233 sd
= runtime
->oss
.subdivision
;
234 oss_period_size
/= sd
;
235 if (oss_period_size
< 16)
236 oss_period_size
= 16;
239 min_period_size
= snd_pcm_plug_client_size(substream
,
240 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
241 min_period_size
*= oss_frame_size
;
242 min_period_size
= 1 << (ld2(min_period_size
- 1) + 1);
243 if (oss_period_size
< min_period_size
)
244 oss_period_size
= min_period_size
;
246 max_period_size
= snd_pcm_plug_client_size(substream
,
247 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
248 max_period_size
*= oss_frame_size
;
249 max_period_size
= 1 << ld2(max_period_size
);
250 if (oss_period_size
> max_period_size
)
251 oss_period_size
= max_period_size
;
253 oss_periods
= oss_buffer_size
/ oss_period_size
;
255 if (substream
->oss
.setup
) {
256 if (substream
->oss
.setup
->periods
> 1)
257 oss_periods
= substream
->oss
.setup
->periods
;
260 s
= snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
261 if (runtime
->oss
.maxfrags
&& s
> runtime
->oss
.maxfrags
)
262 s
= runtime
->oss
.maxfrags
;
266 s
= snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
272 while (oss_period_size
* oss_periods
> oss_buffer_size
)
273 oss_period_size
/= 2;
275 snd_assert(oss_period_size
>= 16, return -EINVAL
);
276 runtime
->oss
.period_bytes
= oss_period_size
;
277 runtime
->oss
.period_frames
= 1;
278 runtime
->oss
.periods
= oss_periods
;
282 static int choose_rate(struct snd_pcm_substream
*substream
,
283 struct snd_pcm_hw_params
*params
, unsigned int best_rate
)
285 struct snd_interval
*it
;
286 struct snd_pcm_hw_params
*save
;
287 unsigned int rate
, prev
;
289 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
293 it
= hw_param_interval(save
, SNDRV_PCM_HW_PARAM_RATE
);
295 /* try multiples of the best rate */
298 if (it
->max
< rate
|| (it
->max
== rate
&& it
->openmax
))
300 if (it
->min
< rate
|| (it
->min
== rate
&& !it
->openmin
)) {
302 ret
= snd_pcm_hw_param_set(substream
, params
,
303 SNDRV_PCM_HW_PARAM_RATE
,
305 if (ret
== (int)rate
) {
317 /* not found, use the nearest rate */
319 return snd_pcm_hw_param_near(substream
, params
, SNDRV_PCM_HW_PARAM_RATE
, best_rate
, NULL
);
322 static int snd_pcm_oss_change_params(struct snd_pcm_substream
*substream
)
324 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
325 struct snd_pcm_hw_params
*params
, *sparams
;
326 struct snd_pcm_sw_params
*sw_params
;
327 ssize_t oss_buffer_size
, oss_period_size
;
328 size_t oss_frame_size
;
331 int format
, sformat
, n
;
332 struct snd_mask sformat_mask
;
333 struct snd_mask mask
;
335 sw_params
= kmalloc(sizeof(*sw_params
), GFP_KERNEL
);
336 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
337 sparams
= kmalloc(sizeof(*sparams
), GFP_KERNEL
);
338 if (!sw_params
|| !params
|| !sparams
) {
339 snd_printd("No memory\n");
344 if (atomic_read(&runtime
->mmap_count
)) {
347 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
348 direct
= (setup
!= NULL
&& setup
->direct
);
351 _snd_pcm_hw_params_any(sparams
);
352 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
353 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
354 snd_mask_none(&mask
);
355 if (atomic_read(&runtime
->mmap_count
))
356 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
358 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
360 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
362 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
364 snd_printd("No usable accesses\n");
368 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
369 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
371 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
373 sformat_mask
= *hw_param_mask(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
377 sformat
= snd_pcm_plug_slave_format(format
, &sformat_mask
);
379 if (sformat
< 0 || !snd_mask_test(&sformat_mask
, sformat
)) {
380 for (sformat
= 0; sformat
<= SNDRV_PCM_FORMAT_LAST
; sformat
++) {
381 if (snd_mask_test(&sformat_mask
, sformat
) &&
382 snd_pcm_oss_format_to(sformat
) >= 0)
385 if (sformat
> SNDRV_PCM_FORMAT_LAST
) {
386 snd_printd("Cannot find a format!!!\n");
391 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, sformat
, 0);
392 snd_assert(err
>= 0, goto failure
);
395 memcpy(params
, sparams
, sizeof(*params
));
397 _snd_pcm_hw_params_any(params
);
398 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
399 SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
400 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
401 snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
402 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
403 runtime
->oss
.channels
, 0);
404 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
405 runtime
->oss
.rate
, 0);
406 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
407 params_access(params
), params_format(params
),
408 params_channels(params
), params_rate(params
));
410 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
411 params_access(sparams
), params_format(sparams
),
412 params_channels(sparams
), params_rate(sparams
));
414 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
415 params_channels(params
) / 8;
417 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
418 snd_pcm_oss_plugin_clear(substream
);
420 /* add necessary plugins */
421 snd_pcm_oss_plugin_clear(substream
);
422 if ((err
= snd_pcm_plug_format_plugins(substream
,
425 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err
);
426 snd_pcm_oss_plugin_clear(substream
);
429 if (runtime
->oss
.plugin_first
) {
430 struct snd_pcm_plugin
*plugin
;
431 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
432 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err
);
433 snd_pcm_oss_plugin_clear(substream
);
436 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
437 err
= snd_pcm_plugin_append(plugin
);
439 err
= snd_pcm_plugin_insert(plugin
);
442 snd_pcm_oss_plugin_clear(substream
);
449 err
= snd_pcm_oss_period_size(substream
, params
, sparams
);
453 n
= snd_pcm_plug_slave_size(substream
, runtime
->oss
.period_bytes
/ oss_frame_size
);
454 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, n
, NULL
);
455 snd_assert(err
>= 0, goto failure
);
457 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIODS
,
458 runtime
->oss
.periods
, NULL
);
459 snd_assert(err
>= 0, goto failure
);
461 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
463 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, sparams
)) < 0) {
464 snd_printd("HW_PARAMS failed: %i\n", err
);
468 memset(sw_params
, 0, sizeof(*sw_params
));
469 if (runtime
->oss
.trigger
) {
470 sw_params
->start_threshold
= 1;
472 sw_params
->start_threshold
= runtime
->boundary
;
474 if (atomic_read(&runtime
->mmap_count
) || substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
475 sw_params
->stop_threshold
= runtime
->boundary
;
477 sw_params
->stop_threshold
= runtime
->buffer_size
;
478 sw_params
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
479 sw_params
->period_step
= 1;
480 sw_params
->sleep_min
= 0;
481 sw_params
->avail_min
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
482 1 : runtime
->period_size
;
483 sw_params
->xfer_align
= 1;
484 if (atomic_read(&runtime
->mmap_count
) ||
485 (substream
->oss
.setup
&& substream
->oss
.setup
->nosilence
)) {
486 sw_params
->silence_threshold
= 0;
487 sw_params
->silence_size
= 0;
489 snd_pcm_uframes_t frames
;
490 frames
= runtime
->period_size
+ 16;
491 if (frames
> runtime
->buffer_size
)
492 frames
= runtime
->buffer_size
;
493 sw_params
->silence_threshold
= frames
;
494 sw_params
->silence_size
= frames
;
497 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_SW_PARAMS
, sw_params
)) < 0) {
498 snd_printd("SW_PARAMS failed: %i\n", err
);
502 runtime
->oss
.periods
= params_periods(sparams
);
503 oss_period_size
= snd_pcm_plug_client_size(substream
, params_period_size(sparams
));
504 snd_assert(oss_period_size
>= 0, err
= -EINVAL
; goto failure
);
505 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
506 if (runtime
->oss
.plugin_first
) {
507 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
512 oss_period_size
*= oss_frame_size
;
514 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
515 snd_assert(oss_buffer_size
>= 0, err
= -EINVAL
; goto failure
);
517 runtime
->oss
.period_bytes
= oss_period_size
;
518 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
520 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
521 runtime
->oss
.period_bytes
,
522 runtime
->oss
.buffer_bytes
);
523 pdprintf("slave: period_size = %i, buffer_size = %i\n",
524 params_period_size(sparams
),
525 params_buffer_size(sparams
));
527 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
528 runtime
->oss
.channels
= params_channels(params
);
529 runtime
->oss
.rate
= params_rate(params
);
531 runtime
->oss
.params
= 0;
532 runtime
->oss
.prepare
= 1;
533 vfree(runtime
->oss
.buffer
);
534 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
535 runtime
->oss
.buffer_used
= 0;
536 if (runtime
->dma_area
)
537 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
539 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
549 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file
*pcm_oss_file
, struct snd_pcm_substream
**r_substream
)
552 struct snd_pcm_substream
*asubstream
= NULL
, *substream
;
554 for (idx
= 0; idx
< 2; idx
++) {
555 substream
= pcm_oss_file
->streams
[idx
];
556 if (substream
== NULL
)
558 if (asubstream
== NULL
)
559 asubstream
= substream
;
560 if (substream
->runtime
->oss
.params
) {
561 err
= snd_pcm_oss_change_params(substream
);
566 snd_assert(asubstream
!= NULL
, return -EIO
);
568 *r_substream
= asubstream
;
572 static int snd_pcm_oss_prepare(struct snd_pcm_substream
*substream
)
575 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
577 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
579 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
582 runtime
->oss
.prepare
= 0;
583 runtime
->oss
.prev_hw_ptr_interrupt
= 0;
584 runtime
->oss
.period_ptr
= 0;
585 runtime
->oss
.buffer_used
= 0;
590 static int snd_pcm_oss_make_ready(struct snd_pcm_substream
*substream
)
592 struct snd_pcm_runtime
*runtime
;
595 if (substream
== NULL
)
597 runtime
= substream
->runtime
;
598 if (runtime
->oss
.params
) {
599 err
= snd_pcm_oss_change_params(substream
);
603 if (runtime
->oss
.prepare
) {
604 err
= snd_pcm_oss_prepare(substream
);
611 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream
*substream
, snd_pcm_sframes_t
*delay
)
613 struct snd_pcm_runtime
*runtime
;
614 snd_pcm_uframes_t frames
;
618 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
621 runtime
= substream
->runtime
;
622 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
624 /* in case of overrun, skip whole periods like OSS/Linux driver does */
625 /* until avail(delay) <= buffer_size */
626 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
627 frames
/= runtime
->period_size
;
628 frames
*= runtime
->period_size
;
629 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
636 snd_pcm_sframes_t
snd_pcm_oss_write3(struct snd_pcm_substream
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
638 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
641 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
642 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
644 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
645 printk("pcm_oss: write: recovering from XRUN\n");
647 printk("pcm_oss: write: recovering from SUSPEND\n");
649 ret
= snd_pcm_oss_prepare(substream
);
655 fs
= snd_enter_user();
656 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
659 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
661 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
663 /* test, if we can't store new data, because the stream */
664 /* has not been started */
665 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
671 snd_pcm_sframes_t
snd_pcm_oss_read3(struct snd_pcm_substream
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
673 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
674 snd_pcm_sframes_t delay
;
677 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
678 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
680 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
681 printk("pcm_oss: read: recovering from XRUN\n");
683 printk("pcm_oss: read: recovering from SUSPEND\n");
685 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
688 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
689 ret
= snd_pcm_oss_prepare(substream
);
693 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
698 fs
= snd_enter_user();
699 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
702 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
705 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
706 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
712 if (ret
!= -ESTRPIPE
)
718 snd_pcm_sframes_t
snd_pcm_oss_writev3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
720 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
723 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
724 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
726 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
727 printk("pcm_oss: writev: recovering from XRUN\n");
729 printk("pcm_oss: writev: recovering from SUSPEND\n");
731 ret
= snd_pcm_oss_prepare(substream
);
737 fs
= snd_enter_user();
738 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
741 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
743 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
746 /* test, if we can't store new data, because the stream */
747 /* has not been started */
748 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
754 snd_pcm_sframes_t
snd_pcm_oss_readv3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
756 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
759 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
760 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
762 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
763 printk("pcm_oss: readv: recovering from XRUN\n");
765 printk("pcm_oss: readv: recovering from SUSPEND\n");
767 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
770 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
771 ret
= snd_pcm_oss_prepare(substream
);
777 fs
= snd_enter_user();
778 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
781 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
783 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
789 static ssize_t
snd_pcm_oss_write2(struct snd_pcm_substream
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
791 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
792 snd_pcm_sframes_t frames
, frames1
;
793 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
794 if (runtime
->oss
.plugin_first
) {
795 struct snd_pcm_plugin_channel
*channels
;
796 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
798 if (copy_from_user(runtime
->oss
.buffer
, (const char __user
*)buf
, bytes
))
800 buf
= runtime
->oss
.buffer
;
802 frames
= bytes
/ oss_frame_bytes
;
803 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
806 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
809 bytes
= frames1
* oss_frame_bytes
;
813 frames
= bytes_to_frames(runtime
, bytes
);
814 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
817 bytes
= frames_to_bytes(runtime
, frames1
);
822 static ssize_t
snd_pcm_oss_write1(struct snd_pcm_substream
*substream
, const char __user
*buf
, size_t bytes
)
826 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
828 if (atomic_read(&runtime
->mmap_count
))
831 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
834 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
836 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
837 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
839 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
))
840 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
842 runtime
->oss
.buffer_used
+= tmp
;
846 if ((substream
->oss
.setup
!= NULL
&& substream
->oss
.setup
->partialfrag
) ||
847 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
848 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
849 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
851 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
852 runtime
->oss
.bytes
+= tmp
;
853 runtime
->oss
.period_ptr
+= tmp
;
854 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
855 if (runtime
->oss
.period_ptr
== 0 ||
856 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
857 runtime
->oss
.buffer_used
= 0;
858 else if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0)
859 return xfer
> 0 ? xfer
: -EAGAIN
;
862 tmp
= snd_pcm_oss_write2(substream
,
863 (const char __force
*)buf
,
864 runtime
->oss
.period_bytes
, 0);
866 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
867 runtime
->oss
.bytes
+= tmp
;
871 if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0 &&
872 tmp
!= runtime
->oss
.period_bytes
)
879 static ssize_t
snd_pcm_oss_read2(struct snd_pcm_substream
*substream
, char *buf
, size_t bytes
, int in_kernel
)
881 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
882 snd_pcm_sframes_t frames
, frames1
;
883 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
884 char __user
*final_dst
= (char __user
*)buf
;
885 if (runtime
->oss
.plugin_first
) {
886 struct snd_pcm_plugin_channel
*channels
;
887 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
889 buf
= runtime
->oss
.buffer
;
890 frames
= bytes
/ oss_frame_bytes
;
891 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
894 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
897 bytes
= frames1
* oss_frame_bytes
;
898 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
903 frames
= bytes_to_frames(runtime
, bytes
);
904 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
907 bytes
= frames_to_bytes(runtime
, frames1
);
912 static ssize_t
snd_pcm_oss_read1(struct snd_pcm_substream
*substream
, char __user
*buf
, size_t bytes
)
916 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
918 if (atomic_read(&runtime
->mmap_count
))
921 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
924 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
925 if (runtime
->oss
.buffer_used
== 0) {
926 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
928 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
929 runtime
->oss
.bytes
+= tmp
;
930 runtime
->oss
.period_ptr
= tmp
;
931 runtime
->oss
.buffer_used
= tmp
;
934 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
935 tmp
= runtime
->oss
.buffer_used
;
936 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
))
937 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
941 runtime
->oss
.buffer_used
-= tmp
;
943 tmp
= snd_pcm_oss_read2(substream
, (char __force
*)buf
,
944 runtime
->oss
.period_bytes
, 0);
946 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
947 runtime
->oss
.bytes
+= tmp
;
956 static int snd_pcm_oss_reset(struct snd_pcm_oss_file
*pcm_oss_file
)
958 struct snd_pcm_substream
*substream
;
960 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
961 if (substream
!= NULL
) {
962 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
963 substream
->runtime
->oss
.prepare
= 1;
965 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
966 if (substream
!= NULL
) {
967 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
968 substream
->runtime
->oss
.prepare
= 1;
973 static int snd_pcm_oss_post(struct snd_pcm_oss_file
*pcm_oss_file
)
975 struct snd_pcm_substream
*substream
;
978 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
979 if (substream
!= NULL
) {
980 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
982 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
984 /* note: all errors from the start action are ignored */
985 /* OSS apps do not know, how to handle them */
989 static int snd_pcm_oss_sync1(struct snd_pcm_substream
*substream
, size_t size
)
991 struct snd_pcm_runtime
*runtime
;
996 runtime
= substream
->runtime
;
997 init_waitqueue_entry(&wait
, current
);
998 add_wait_queue(&runtime
->sleep
, &wait
);
1000 printk("sync1: size = %li\n", size
);
1003 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
1005 runtime
->oss
.buffer_used
= 0;
1009 if (result
!= 0 && result
!= -EAGAIN
)
1012 set_current_state(TASK_INTERRUPTIBLE
);
1013 snd_pcm_stream_lock_irq(substream
);
1014 res
= runtime
->status
->state
;
1015 snd_pcm_stream_unlock_irq(substream
);
1016 if (res
!= SNDRV_PCM_STATE_RUNNING
) {
1017 set_current_state(TASK_RUNNING
);
1020 res
= schedule_timeout(10 * HZ
);
1021 if (signal_pending(current
)) {
1022 result
= -ERESTARTSYS
;
1026 snd_printk(KERN_ERR
"OSS sync error - DMA timeout\n");
1031 remove_wait_queue(&runtime
->sleep
, &wait
);
1035 static int snd_pcm_oss_sync(struct snd_pcm_oss_file
*pcm_oss_file
)
1038 unsigned int saved_f_flags
;
1039 struct snd_pcm_substream
*substream
;
1040 struct snd_pcm_runtime
*runtime
;
1041 snd_pcm_format_t format
;
1042 unsigned long width
;
1045 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1046 if (substream
!= NULL
) {
1047 runtime
= substream
->runtime
;
1048 if (atomic_read(&runtime
->mmap_count
))
1050 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1052 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1053 width
= snd_pcm_format_physical_width(format
);
1054 if (runtime
->oss
.buffer_used
> 0) {
1056 printk("sync: buffer_used\n");
1058 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1059 snd_pcm_format_set_silence(format
,
1060 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1062 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1065 } else if (runtime
->oss
.period_ptr
> 0) {
1067 printk("sync: period_ptr\n");
1069 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1070 snd_pcm_format_set_silence(format
,
1071 runtime
->oss
.buffer
,
1073 err
= snd_pcm_oss_sync1(substream
, size
);
1078 * The ALSA's period might be a bit large than OSS one.
1079 * Fill the remain portion of ALSA period with zeros.
1081 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1083 size
= runtime
->period_size
- size
;
1084 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
1085 size
= (runtime
->frame_bits
* size
) / 8;
1088 size_t size1
= size
< runtime
->oss
.period_bytes
? size
: runtime
->oss
.period_bytes
;
1091 size1
/= runtime
->sample_bits
;
1092 snd_pcm_format_set_silence(runtime
->format
,
1093 runtime
->oss
.buffer
,
1095 fs
= snd_enter_user();
1096 snd_pcm_lib_write(substream
, (void __user
*)runtime
->oss
.buffer
, size1
);
1099 } else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
1100 void __user
*buffers
[runtime
->channels
];
1101 memset(buffers
, 0, runtime
->channels
* sizeof(void *));
1102 snd_pcm_lib_writev(substream
, buffers
, size
);
1106 * finish sync: drain the buffer
1109 saved_f_flags
= substream
->ffile
->f_flags
;
1110 substream
->ffile
->f_flags
&= ~O_NONBLOCK
;
1111 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1112 substream
->ffile
->f_flags
= saved_f_flags
;
1115 runtime
->oss
.prepare
= 1;
1118 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1119 if (substream
!= NULL
) {
1120 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1122 runtime
= substream
->runtime
;
1123 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1126 runtime
->oss
.buffer_used
= 0;
1127 runtime
->oss
.prepare
= 1;
1132 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file
*pcm_oss_file
, int rate
)
1136 for (idx
= 1; idx
>= 0; --idx
) {
1137 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1138 struct snd_pcm_runtime
*runtime
;
1139 if (substream
== NULL
)
1141 runtime
= substream
->runtime
;
1144 else if (rate
> 192000)
1146 if (runtime
->oss
.rate
!= rate
) {
1147 runtime
->oss
.params
= 1;
1148 runtime
->oss
.rate
= rate
;
1151 return snd_pcm_oss_get_rate(pcm_oss_file
);
1154 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
)
1156 struct snd_pcm_substream
*substream
;
1159 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1161 return substream
->runtime
->oss
.rate
;
1164 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int channels
)
1171 for (idx
= 1; idx
>= 0; --idx
) {
1172 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1173 struct snd_pcm_runtime
*runtime
;
1174 if (substream
== NULL
)
1176 runtime
= substream
->runtime
;
1177 if (runtime
->oss
.channels
!= channels
) {
1178 runtime
->oss
.params
= 1;
1179 runtime
->oss
.channels
= channels
;
1182 return snd_pcm_oss_get_channels(pcm_oss_file
);
1185 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
)
1187 struct snd_pcm_substream
*substream
;
1190 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1192 return substream
->runtime
->oss
.channels
;
1195 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file
*pcm_oss_file
)
1197 struct snd_pcm_substream
*substream
;
1200 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1202 return substream
->runtime
->oss
.period_bytes
;
1205 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file
*pcm_oss_file
)
1207 struct snd_pcm_substream
*substream
;
1210 struct snd_pcm_hw_params
*params
;
1211 unsigned int formats
= 0;
1212 struct snd_mask format_mask
;
1215 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1217 if (atomic_read(&substream
->runtime
->mmap_count
)) {
1220 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
1221 direct
= (setup
!= NULL
&& setup
->direct
);
1224 return AFMT_MU_LAW
| AFMT_U8
|
1225 AFMT_S16_LE
| AFMT_S16_BE
|
1226 AFMT_S8
| AFMT_U16_LE
|
1228 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1231 _snd_pcm_hw_params_any(params
);
1232 err
= snd_pcm_hw_refine(substream
, params
);
1233 format_mask
= *hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1235 snd_assert(err
>= 0, return err
);
1236 for (fmt
= 0; fmt
< 32; ++fmt
) {
1237 if (snd_mask_test(&format_mask
, fmt
)) {
1238 int f
= snd_pcm_oss_format_to(fmt
);
1246 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file
*pcm_oss_file
, int format
)
1250 if (format
!= AFMT_QUERY
) {
1251 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1252 if (!(formats
& format
))
1254 for (idx
= 1; idx
>= 0; --idx
) {
1255 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1256 struct snd_pcm_runtime
*runtime
;
1257 if (substream
== NULL
)
1259 runtime
= substream
->runtime
;
1260 if (runtime
->oss
.format
!= format
) {
1261 runtime
->oss
.params
= 1;
1262 runtime
->oss
.format
= format
;
1266 return snd_pcm_oss_get_format(pcm_oss_file
);
1269 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
)
1271 struct snd_pcm_substream
*substream
;
1274 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1276 return substream
->runtime
->oss
.format
;
1279 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream
*substream
, int subdivide
)
1281 struct snd_pcm_runtime
*runtime
;
1283 if (substream
== NULL
)
1285 runtime
= substream
->runtime
;
1286 if (subdivide
== 0) {
1287 subdivide
= runtime
->oss
.subdivision
;
1292 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1294 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1295 subdivide
!= 8 && subdivide
!= 16)
1297 runtime
->oss
.subdivision
= subdivide
;
1298 runtime
->oss
.params
= 1;
1302 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file
*pcm_oss_file
, int subdivide
)
1304 int err
= -EINVAL
, idx
;
1306 for (idx
= 1; idx
>= 0; --idx
) {
1307 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1308 if (substream
== NULL
)
1310 if ((err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
)) < 0)
1316 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream
*substream
, unsigned int val
)
1318 struct snd_pcm_runtime
*runtime
;
1320 if (substream
== NULL
)
1322 runtime
= substream
->runtime
;
1323 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1325 runtime
->oss
.fragshift
= val
& 0xffff;
1326 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
1327 if (runtime
->oss
.fragshift
< 4) /* < 16 */
1328 runtime
->oss
.fragshift
= 4;
1329 if (runtime
->oss
.maxfrags
< 2)
1330 runtime
->oss
.maxfrags
= 2;
1331 runtime
->oss
.params
= 1;
1335 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int val
)
1337 int err
= -EINVAL
, idx
;
1339 for (idx
= 1; idx
>= 0; --idx
) {
1340 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1341 if (substream
== NULL
)
1343 if ((err
= snd_pcm_oss_set_fragment1(substream
, val
)) < 0)
1349 static int snd_pcm_oss_nonblock(struct file
* file
)
1351 file
->f_flags
|= O_NONBLOCK
;
1355 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream
*substream
, int res
)
1358 if (substream
== NULL
) {
1359 res
&= ~DSP_CAP_DUPLEX
;
1362 #ifdef DSP_CAP_MULTI
1363 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1364 if (substream
->pstr
->substream_count
> 1)
1365 res
|= DSP_CAP_MULTI
;
1367 /* DSP_CAP_REALTIME is set all times: */
1368 /* all ALSA drivers can return actual pointer in ring buffer */
1369 #if defined(DSP_CAP_REALTIME) && 0
1371 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1372 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
1373 res
&= ~DSP_CAP_REALTIME
;
1379 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file
*pcm_oss_file
)
1383 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
1384 for (idx
= 0; idx
< 2; idx
++) {
1385 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1386 result
= snd_pcm_oss_get_caps1(substream
, result
);
1388 result
|= 0x0001; /* revision - same as SB AWE 64 */
1392 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream
*substream
, snd_pcm_uframes_t hw_ptr
)
1394 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1395 snd_pcm_uframes_t appl_ptr
;
1396 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
1397 appl_ptr
%= runtime
->boundary
;
1398 runtime
->control
->appl_ptr
= appl_ptr
;
1401 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file
*pcm_oss_file
, int trigger
)
1403 struct snd_pcm_runtime
*runtime
;
1404 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1408 printk("pcm_oss: trigger = 0x%x\n", trigger
);
1411 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1412 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1415 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
1419 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
1423 runtime
= psubstream
->runtime
;
1424 if (trigger
& PCM_ENABLE_OUTPUT
) {
1425 if (runtime
->oss
.trigger
)
1427 if (atomic_read(&psubstream
->runtime
->mmap_count
))
1428 snd_pcm_oss_simulate_fill(psubstream
, runtime
->hw_ptr_interrupt
);
1429 runtime
->oss
.trigger
= 1;
1430 runtime
->start_threshold
= 1;
1431 cmd
= SNDRV_PCM_IOCTL_START
;
1433 if (!runtime
->oss
.trigger
)
1435 runtime
->oss
.trigger
= 0;
1436 runtime
->start_threshold
= runtime
->boundary
;
1437 cmd
= SNDRV_PCM_IOCTL_DROP
;
1438 runtime
->oss
.prepare
= 1;
1440 err
= snd_pcm_kernel_ioctl(psubstream
, cmd
, NULL
);
1446 runtime
= csubstream
->runtime
;
1447 if (trigger
& PCM_ENABLE_INPUT
) {
1448 if (runtime
->oss
.trigger
)
1450 runtime
->oss
.trigger
= 1;
1451 runtime
->start_threshold
= 1;
1452 cmd
= SNDRV_PCM_IOCTL_START
;
1454 if (!runtime
->oss
.trigger
)
1456 runtime
->oss
.trigger
= 0;
1457 runtime
->start_threshold
= runtime
->boundary
;
1458 cmd
= SNDRV_PCM_IOCTL_DROP
;
1459 runtime
->oss
.prepare
= 1;
1461 err
= snd_pcm_kernel_ioctl(csubstream
, cmd
, NULL
);
1469 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file
*pcm_oss_file
)
1471 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1474 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1475 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1476 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
1477 result
|= PCM_ENABLE_OUTPUT
;
1478 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
1479 result
|= PCM_ENABLE_INPUT
;
1483 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file
*pcm_oss_file
)
1485 struct snd_pcm_substream
*substream
;
1486 struct snd_pcm_runtime
*runtime
;
1487 snd_pcm_sframes_t delay
;
1490 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1491 if (substream
== NULL
)
1493 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1495 runtime
= substream
->runtime
;
1496 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
1498 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1500 delay
= 0; /* hack for broken OSS applications */
1503 return snd_pcm_oss_bytes(substream
, delay
);
1506 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
1508 struct snd_pcm_substream
*substream
;
1509 struct snd_pcm_runtime
*runtime
;
1510 snd_pcm_sframes_t delay
;
1512 struct count_info info
;
1517 substream
= pcm_oss_file
->streams
[stream
];
1518 if (substream
== NULL
)
1520 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1522 runtime
= substream
->runtime
;
1523 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
1524 memset(&info
, 0, sizeof(info
));
1525 if (copy_to_user(_info
, &info
, sizeof(info
)))
1529 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1530 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1531 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
1536 fixup
= runtime
->oss
.buffer_used
;
1539 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1540 fixup
= -runtime
->oss
.buffer_used
;
1544 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
1545 if (atomic_read(&runtime
->mmap_count
)) {
1546 snd_pcm_sframes_t n
;
1547 n
= (delay
= runtime
->hw_ptr_interrupt
) - runtime
->oss
.prev_hw_ptr_interrupt
;
1549 n
+= runtime
->boundary
;
1550 info
.blocks
= n
/ runtime
->period_size
;
1551 runtime
->oss
.prev_hw_ptr_interrupt
= delay
;
1552 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1553 snd_pcm_oss_simulate_fill(substream
, delay
);
1554 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
1556 delay
= snd_pcm_oss_bytes(substream
, delay
);
1557 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1558 struct snd_pcm_oss_setup
*setup
= substream
->oss
.setup
;
1559 if (setup
&& setup
->buggyptr
)
1560 info
.blocks
= (runtime
->oss
.buffer_bytes
- delay
- fixup
) / runtime
->oss
.period_bytes
;
1562 info
.blocks
= (delay
+ fixup
) / runtime
->oss
.period_bytes
;
1563 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
1566 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
1567 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
1570 if (copy_to_user(_info
, &info
, sizeof(info
)))
1575 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
1577 struct snd_pcm_substream
*substream
;
1578 struct snd_pcm_runtime
*runtime
;
1579 snd_pcm_sframes_t avail
;
1581 struct audio_buf_info info
;
1586 substream
= pcm_oss_file
->streams
[stream
];
1587 if (substream
== NULL
)
1589 runtime
= substream
->runtime
;
1591 if (runtime
->oss
.params
&&
1592 (err
= snd_pcm_oss_change_params(substream
)) < 0)
1595 info
.fragsize
= runtime
->oss
.period_bytes
;
1596 info
.fragstotal
= runtime
->periods
;
1597 if (runtime
->oss
.prepare
) {
1598 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1599 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
1600 info
.fragments
= runtime
->oss
.periods
;
1606 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1607 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
1608 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
1609 avail
= runtime
->buffer_size
;
1613 avail
= runtime
->buffer_size
- avail
;
1614 fixup
= -runtime
->oss
.buffer_used
;
1617 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
1618 fixup
= runtime
->oss
.buffer_used
;
1622 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
1623 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
1627 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
1629 if (copy_to_user(_info
, &info
, sizeof(info
)))
1634 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
1636 // it won't be probably implemented
1637 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1641 static struct snd_pcm_oss_setup
*snd_pcm_oss_look_for_setup(struct snd_pcm
*pcm
, int stream
, const char *task_name
)
1643 const char *ptr
, *ptrl
;
1644 struct snd_pcm_oss_setup
*setup
;
1646 mutex_lock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1647 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
; setup
= setup
->next
) {
1648 if (!strcmp(setup
->task_name
, task_name
)) {
1649 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1653 ptr
= ptrl
= task_name
;
1659 if (ptrl
== task_name
) {
1663 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
; setup
= setup
->next
) {
1664 if (!strcmp(setup
->task_name
, ptrl
)) {
1665 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1670 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1674 static void snd_pcm_oss_release_substream(struct snd_pcm_substream
*substream
)
1676 struct snd_pcm_runtime
*runtime
;
1677 runtime
= substream
->runtime
;
1678 vfree(runtime
->oss
.buffer
);
1679 runtime
->oss
.buffer
= NULL
;
1680 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1681 snd_pcm_oss_plugin_clear(substream
);
1683 substream
->oss
.oss
= 0;
1686 static void snd_pcm_oss_init_substream(struct snd_pcm_substream
*substream
,
1687 struct snd_pcm_oss_setup
*setup
,
1690 struct snd_pcm_runtime
*runtime
;
1692 substream
->oss
.oss
= 1;
1693 substream
->oss
.setup
= setup
;
1694 if (setup
->nonblock
)
1695 substream
->ffile
->f_flags
|= O_NONBLOCK
;
1697 substream
->ffile
->f_flags
&= ~O_NONBLOCK
;
1698 runtime
= substream
->runtime
;
1699 runtime
->oss
.params
= 1;
1700 runtime
->oss
.trigger
= 1;
1701 runtime
->oss
.rate
= 8000;
1702 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
1703 case SNDRV_MINOR_OSS_PCM_8
:
1704 runtime
->oss
.format
= AFMT_U8
;
1706 case SNDRV_MINOR_OSS_PCM_16
:
1707 runtime
->oss
.format
= AFMT_S16_LE
;
1710 runtime
->oss
.format
= AFMT_MU_LAW
;
1712 runtime
->oss
.channels
= 1;
1713 runtime
->oss
.fragshift
= 0;
1714 runtime
->oss
.maxfrags
= 0;
1715 runtime
->oss
.subdivision
= 0;
1716 substream
->pcm_release
= snd_pcm_oss_release_substream
;
1719 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file
*pcm_oss_file
)
1722 snd_assert(pcm_oss_file
!= NULL
, return -ENXIO
);
1723 for (cidx
= 0; cidx
< 2; ++cidx
) {
1724 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[cidx
];
1726 snd_pcm_release_substream(substream
);
1728 kfree(pcm_oss_file
);
1732 static int snd_pcm_oss_open_file(struct file
*file
,
1733 struct snd_pcm
*pcm
,
1734 struct snd_pcm_oss_file
**rpcm_oss_file
,
1736 struct snd_pcm_oss_setup
**setup
)
1739 struct snd_pcm_oss_file
*pcm_oss_file
;
1740 struct snd_pcm_substream
*substream
;
1741 unsigned int f_mode
= file
->f_mode
;
1743 snd_assert(rpcm_oss_file
!= NULL
, return -EINVAL
);
1744 *rpcm_oss_file
= NULL
;
1746 pcm_oss_file
= kzalloc(sizeof(*pcm_oss_file
), GFP_KERNEL
);
1747 if (pcm_oss_file
== NULL
)
1750 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
1751 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
1752 f_mode
= FMODE_WRITE
;
1754 for (idx
= 0; idx
< 2; idx
++) {
1755 if (! setup
[idx
] || setup
[idx
]->disable
)
1757 if (idx
== SNDRV_PCM_STREAM_PLAYBACK
) {
1758 if (! (f_mode
& FMODE_WRITE
))
1761 if (! (f_mode
& FMODE_READ
))
1764 err
= snd_pcm_open_substream(pcm
, idx
, file
, &substream
);
1766 snd_pcm_oss_release_file(pcm_oss_file
);
1770 pcm_oss_file
->streams
[idx
] = substream
;
1771 snd_pcm_oss_init_substream(substream
, setup
[idx
], minor
);
1774 if (! pcm_oss_file
->streams
[0] && pcm_oss_file
->streams
[1]) {
1775 snd_pcm_oss_release_file(pcm_oss_file
);
1779 file
->private_data
= pcm_oss_file
;
1780 *rpcm_oss_file
= pcm_oss_file
;
1785 static int snd_task_name(struct task_struct
*task
, char *name
, size_t size
)
1789 snd_assert(task
!= NULL
&& name
!= NULL
&& size
>= 2, return -EINVAL
);
1790 for (idx
= 0; idx
< sizeof(task
->comm
) && idx
+ 1 < size
; idx
++)
1791 name
[idx
] = task
->comm
[idx
];
1796 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
1800 struct snd_pcm
*pcm
;
1801 struct snd_pcm_oss_file
*pcm_oss_file
;
1802 struct snd_pcm_oss_setup
*setup
[2];
1806 pcm
= snd_lookup_oss_minor_data(iminor(inode
),
1807 SNDRV_OSS_DEVICE_TYPE_PCM
);
1812 err
= snd_card_file_add(pcm
->card
, file
);
1815 if (!try_module_get(pcm
->card
->module
)) {
1819 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
1823 memset(setup
, 0, sizeof(*setup
));
1824 if (file
->f_mode
& FMODE_WRITE
)
1825 setup
[0] = snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, task_name
);
1826 if (file
->f_mode
& FMODE_READ
)
1827 setup
[1] = snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
, task_name
);
1829 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
1831 nonblock
= nonblock_open
;
1833 init_waitqueue_entry(&wait
, current
);
1834 add_wait_queue(&pcm
->open_wait
, &wait
);
1835 mutex_lock(&pcm
->open_mutex
);
1837 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
1838 iminor(inode
), setup
);
1841 if (err
== -EAGAIN
) {
1848 set_current_state(TASK_INTERRUPTIBLE
);
1849 mutex_unlock(&pcm
->open_mutex
);
1851 mutex_lock(&pcm
->open_mutex
);
1852 if (signal_pending(current
)) {
1857 remove_wait_queue(&pcm
->open_wait
, &wait
);
1858 mutex_unlock(&pcm
->open_mutex
);
1864 module_put(pcm
->card
->module
);
1866 snd_card_file_remove(pcm
->card
, file
);
1871 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
1873 struct snd_pcm
*pcm
;
1874 struct snd_pcm_substream
*substream
;
1875 struct snd_pcm_oss_file
*pcm_oss_file
;
1877 pcm_oss_file
= file
->private_data
;
1878 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1879 if (substream
== NULL
)
1880 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1881 snd_assert(substream
!= NULL
, return -ENXIO
);
1882 pcm
= substream
->pcm
;
1883 snd_pcm_oss_sync(pcm_oss_file
);
1884 mutex_lock(&pcm
->open_mutex
);
1885 snd_pcm_oss_release_file(pcm_oss_file
);
1886 mutex_unlock(&pcm
->open_mutex
);
1887 wake_up(&pcm
->open_wait
);
1888 module_put(pcm
->card
->module
);
1889 snd_card_file_remove(pcm
->card
, file
);
1893 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1895 struct snd_pcm_oss_file
*pcm_oss_file
;
1896 int __user
*p
= (int __user
*)arg
;
1899 pcm_oss_file
= file
->private_data
;
1900 if (cmd
== OSS_GETVERSION
)
1901 return put_user(SNDRV_OSS_VERSION
, p
);
1902 if (cmd
== OSS_ALSAEMULVER
)
1903 return put_user(1, p
);
1904 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1905 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1906 struct snd_pcm_substream
*substream
;
1908 for (idx
= 0; idx
< 2; ++idx
) {
1909 substream
= pcm_oss_file
->streams
[idx
];
1910 if (substream
!= NULL
)
1913 snd_assert(substream
!= NULL
, return -ENXIO
);
1914 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
1917 if (((cmd
>> 8) & 0xff) != 'P')
1920 printk("pcm_oss: ioctl = 0x%x\n", cmd
);
1923 case SNDCTL_DSP_RESET
:
1924 return snd_pcm_oss_reset(pcm_oss_file
);
1925 case SNDCTL_DSP_SYNC
:
1926 return snd_pcm_oss_sync(pcm_oss_file
);
1927 case SNDCTL_DSP_SPEED
:
1928 if (get_user(res
, p
))
1930 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
1932 return put_user(res
, p
);
1933 case SOUND_PCM_READ_RATE
:
1934 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
1937 return put_user(res
, p
);
1938 case SNDCTL_DSP_STEREO
:
1939 if (get_user(res
, p
))
1941 res
= res
> 0 ? 2 : 1;
1942 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
1944 return put_user(--res
, p
);
1945 case SNDCTL_DSP_GETBLKSIZE
:
1946 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
1949 return put_user(res
, p
);
1950 case SNDCTL_DSP_SETFMT
:
1951 if (get_user(res
, p
))
1953 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
1956 return put_user(res
, p
);
1957 case SOUND_PCM_READ_BITS
:
1958 res
= snd_pcm_oss_get_format(pcm_oss_file
);
1961 return put_user(res
, p
);
1962 case SNDCTL_DSP_CHANNELS
:
1963 if (get_user(res
, p
))
1965 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
1968 return put_user(res
, p
);
1969 case SOUND_PCM_READ_CHANNELS
:
1970 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
1973 return put_user(res
, p
);
1974 case SOUND_PCM_WRITE_FILTER
:
1975 case SOUND_PCM_READ_FILTER
:
1977 case SNDCTL_DSP_POST
:
1978 return snd_pcm_oss_post(pcm_oss_file
);
1979 case SNDCTL_DSP_SUBDIVIDE
:
1980 if (get_user(res
, p
))
1982 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
1985 return put_user(res
, p
);
1986 case SNDCTL_DSP_SETFRAGMENT
:
1987 if (get_user(res
, p
))
1989 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
1990 case SNDCTL_DSP_GETFMTS
:
1991 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
1994 return put_user(res
, p
);
1995 case SNDCTL_DSP_GETOSPACE
:
1996 case SNDCTL_DSP_GETISPACE
:
1997 return snd_pcm_oss_get_space(pcm_oss_file
,
1998 cmd
== SNDCTL_DSP_GETISPACE
?
1999 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2000 (struct audio_buf_info __user
*) arg
);
2001 case SNDCTL_DSP_NONBLOCK
:
2002 return snd_pcm_oss_nonblock(file
);
2003 case SNDCTL_DSP_GETCAPS
:
2004 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
2007 return put_user(res
, p
);
2008 case SNDCTL_DSP_GETTRIGGER
:
2009 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2012 return put_user(res
, p
);
2013 case SNDCTL_DSP_SETTRIGGER
:
2014 if (get_user(res
, p
))
2016 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2017 case SNDCTL_DSP_GETIPTR
:
2018 case SNDCTL_DSP_GETOPTR
:
2019 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2020 cmd
== SNDCTL_DSP_GETIPTR
?
2021 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2022 (struct count_info __user
*) arg
);
2023 case SNDCTL_DSP_MAPINBUF
:
2024 case SNDCTL_DSP_MAPOUTBUF
:
2025 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2026 cmd
== SNDCTL_DSP_MAPINBUF
?
2027 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2028 (struct buffmem_desc __user
*) arg
);
2029 case SNDCTL_DSP_SETSYNCRO
:
2030 /* stop DMA now.. */
2032 case SNDCTL_DSP_SETDUPLEX
:
2033 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2036 case SNDCTL_DSP_GETODELAY
:
2037 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2039 /* it's for sure, some broken apps don't check for error codes */
2043 return put_user(res
, p
);
2044 case SNDCTL_DSP_PROFILE
:
2045 return 0; /* silently ignore */
2047 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd
);
2052 #ifdef CONFIG_COMPAT
2053 /* all compatible */
2054 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2056 #define snd_pcm_oss_ioctl_compat NULL
2059 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2061 struct snd_pcm_oss_file
*pcm_oss_file
;
2062 struct snd_pcm_substream
*substream
;
2064 pcm_oss_file
= file
->private_data
;
2065 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2066 if (substream
== NULL
)
2069 return snd_pcm_oss_read1(substream
, buf
, count
);
2072 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2073 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count
, (long)res
);
2079 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2081 struct snd_pcm_oss_file
*pcm_oss_file
;
2082 struct snd_pcm_substream
*substream
;
2085 pcm_oss_file
= file
->private_data
;
2086 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2087 if (substream
== NULL
)
2089 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2091 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count
, (long)result
);
2096 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream
*substream
)
2098 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2099 if (atomic_read(&runtime
->mmap_count
))
2100 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2102 return snd_pcm_playback_avail(runtime
) >= runtime
->oss
.period_frames
;
2105 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream
*substream
)
2107 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2108 if (atomic_read(&runtime
->mmap_count
))
2109 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2111 return snd_pcm_capture_avail(runtime
) >= runtime
->oss
.period_frames
;
2114 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2116 struct snd_pcm_oss_file
*pcm_oss_file
;
2118 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2120 pcm_oss_file
= file
->private_data
;
2122 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2123 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2126 if (psubstream
!= NULL
) {
2127 struct snd_pcm_runtime
*runtime
= psubstream
->runtime
;
2128 poll_wait(file
, &runtime
->sleep
, wait
);
2129 snd_pcm_stream_lock_irq(psubstream
);
2130 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2131 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2132 snd_pcm_oss_playback_ready(psubstream
)))
2133 mask
|= POLLOUT
| POLLWRNORM
;
2134 snd_pcm_stream_unlock_irq(psubstream
);
2136 if (csubstream
!= NULL
) {
2137 struct snd_pcm_runtime
*runtime
= csubstream
->runtime
;
2138 snd_pcm_state_t ostate
;
2139 poll_wait(file
, &runtime
->sleep
, wait
);
2140 snd_pcm_stream_lock_irq(csubstream
);
2141 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2142 snd_pcm_oss_capture_ready(csubstream
))
2143 mask
|= POLLIN
| POLLRDNORM
;
2144 snd_pcm_stream_unlock_irq(csubstream
);
2145 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2146 struct snd_pcm_oss_file ofile
;
2147 memset(&ofile
, 0, sizeof(ofile
));
2148 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2149 runtime
->oss
.trigger
= 0;
2150 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2157 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2159 struct snd_pcm_oss_file
*pcm_oss_file
;
2160 struct snd_pcm_substream
*substream
= NULL
;
2161 struct snd_pcm_runtime
*runtime
;
2165 printk("pcm_oss: mmap begin\n");
2167 pcm_oss_file
= file
->private_data
;
2168 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2169 case VM_READ
| VM_WRITE
:
2170 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2175 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2178 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2183 /* set VM_READ access as well to fix memset() routines that do
2184 reads before writes (to improve performance) */
2185 area
->vm_flags
|= VM_READ
;
2186 if (substream
== NULL
)
2188 runtime
= substream
->runtime
;
2189 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2191 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2192 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2196 if (runtime
->oss
.params
) {
2197 if ((err
= snd_pcm_oss_change_params(substream
)) < 0)
2200 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2201 if (runtime
->oss
.plugin_first
!= NULL
)
2205 if (area
->vm_pgoff
!= 0)
2208 err
= snd_pcm_mmap_data(substream
, file
, area
);
2211 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2212 runtime
->silence_threshold
= 0;
2213 runtime
->silence_size
= 0;
2215 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime
->oss
.mmap_bytes
);
2217 /* In mmap mode we never stop */
2218 runtime
->stop_threshold
= runtime
->boundary
;
2223 #ifdef CONFIG_PROC_FS
2228 static void snd_pcm_oss_proc_read(struct snd_info_entry
*entry
,
2229 struct snd_info_buffer
*buffer
)
2231 struct snd_pcm_str
*pstr
= entry
->private_data
;
2232 struct snd_pcm_oss_setup
*setup
= pstr
->oss
.setup_list
;
2233 mutex_lock(&pstr
->oss
.setup_mutex
);
2235 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2239 setup
->disable
? " disable" : "",
2240 setup
->direct
? " direct" : "",
2241 setup
->block
? " block" : "",
2242 setup
->nonblock
? " non-block" : "",
2243 setup
->partialfrag
? " partial-frag" : "",
2244 setup
->nosilence
? " no-silence" : "");
2245 setup
= setup
->next
;
2247 mutex_unlock(&pstr
->oss
.setup_mutex
);
2250 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str
* pstr
)
2253 struct snd_pcm_substream
*substream
;
2254 struct snd_pcm_oss_setup
*setup
, *setupn
;
2256 for (idx
= 0, substream
= pstr
->substream
;
2257 idx
< pstr
->substream_count
; idx
++, substream
= substream
->next
)
2258 substream
->oss
.setup
= NULL
;
2259 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
2260 setup
; setup
= setupn
) {
2261 setupn
= setup
->next
;
2262 kfree(setup
->task_name
);
2265 pstr
->oss
.setup_list
= NULL
;
2268 static void snd_pcm_oss_proc_write(struct snd_info_entry
*entry
,
2269 struct snd_info_buffer
*buffer
)
2271 struct snd_pcm_str
*pstr
= entry
->private_data
;
2272 char line
[128], str
[32], task_name
[32], *ptr
;
2274 struct snd_pcm_oss_setup
*setup
, *setup1
, template;
2276 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
2277 mutex_lock(&pstr
->oss
.setup_mutex
);
2278 memset(&template, 0, sizeof(template));
2279 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
2280 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
2281 snd_pcm_oss_proc_free_setup_list(pstr
);
2282 mutex_unlock(&pstr
->oss
.setup_mutex
);
2285 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
2286 if (!strcmp(setup
->task_name
, task_name
)) {
2291 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2292 template.periods
= simple_strtoul(str
, NULL
, 10);
2293 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2294 template.period_size
= simple_strtoul(str
, NULL
, 10);
2295 for (idx1
= 31; idx1
>= 0; idx1
--)
2296 if (template.period_size
& (1 << idx1
))
2298 for (idx1
--; idx1
>= 0; idx1
--)
2299 template.period_size
&= ~(1 << idx1
);
2301 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2302 if (!strcmp(str
, "disable")) {
2303 template.disable
= 1;
2304 } else if (!strcmp(str
, "direct")) {
2305 template.direct
= 1;
2306 } else if (!strcmp(str
, "block")) {
2308 } else if (!strcmp(str
, "non-block")) {
2309 template.nonblock
= 1;
2310 } else if (!strcmp(str
, "partial-frag")) {
2311 template.partialfrag
= 1;
2312 } else if (!strcmp(str
, "no-silence")) {
2313 template.nosilence
= 1;
2314 } else if (!strcmp(str
, "buggy-ptr")) {
2315 template.buggyptr
= 1;
2318 if (setup
== NULL
) {
2319 setup
= kmalloc(sizeof(struct snd_pcm_oss_setup
), GFP_KERNEL
);
2321 if (pstr
->oss
.setup_list
== NULL
) {
2322 pstr
->oss
.setup_list
= setup
;
2324 for (setup1
= pstr
->oss
.setup_list
; setup1
->next
; setup1
= setup1
->next
);
2325 setup1
->next
= setup
;
2327 template.task_name
= kstrdup(task_name
, GFP_KERNEL
);
2329 buffer
->error
= -ENOMEM
;
2334 mutex_unlock(&pstr
->oss
.setup_mutex
);
2338 static void snd_pcm_oss_proc_init(struct snd_pcm
*pcm
)
2341 for (stream
= 0; stream
< 2; ++stream
) {
2342 struct snd_info_entry
*entry
;
2343 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2344 if (pstr
->substream_count
== 0)
2346 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
2347 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
2348 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2349 entry
->c
.text
.read_size
= 8192;
2350 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
2351 entry
->c
.text
.write_size
= 8192;
2352 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
2353 entry
->private_data
= pstr
;
2354 if (snd_info_register(entry
) < 0) {
2355 snd_info_free_entry(entry
);
2359 pstr
->oss
.proc_entry
= entry
;
2363 static void snd_pcm_oss_proc_done(struct snd_pcm
*pcm
)
2366 for (stream
= 0; stream
< 2; ++stream
) {
2367 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2368 if (pstr
->oss
.proc_entry
) {
2369 snd_info_unregister(pstr
->oss
.proc_entry
);
2370 pstr
->oss
.proc_entry
= NULL
;
2371 snd_pcm_oss_proc_free_setup_list(pstr
);
2375 #else /* !CONFIG_PROC_FS */
2376 #define snd_pcm_oss_proc_init(pcm)
2377 #define snd_pcm_oss_proc_done(pcm)
2378 #endif /* CONFIG_PROC_FS */
2384 static struct file_operations snd_pcm_oss_f_reg
=
2386 .owner
= THIS_MODULE
,
2387 .read
= snd_pcm_oss_read
,
2388 .write
= snd_pcm_oss_write
,
2389 .open
= snd_pcm_oss_open
,
2390 .release
= snd_pcm_oss_release
,
2391 .poll
= snd_pcm_oss_poll
,
2392 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
2393 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
2394 .mmap
= snd_pcm_oss_mmap
,
2397 static void register_oss_dsp(struct snd_pcm
*pcm
, int index
)
2400 sprintf(name
, "dsp%i%i", pcm
->card
->number
, pcm
->device
);
2401 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2402 pcm
->card
, index
, &snd_pcm_oss_f_reg
,
2404 snd_printk(KERN_ERR
"unable to register OSS PCM device %i:%i\n",
2405 pcm
->card
->number
, pcm
->device
);
2409 static int snd_pcm_oss_register_minor(struct snd_pcm
*pcm
)
2412 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2415 register_oss_dsp(pcm
, 0);
2416 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
2417 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
2418 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
2419 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
2420 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2421 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
2426 pcm
->oss
.reg_mask
|= 1;
2428 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2429 register_oss_dsp(pcm
, 1);
2431 pcm
->oss
.reg_mask
|= 2;
2435 snd_pcm_oss_proc_init(pcm
);
2440 static int snd_pcm_oss_disconnect_minor(struct snd_pcm
*pcm
)
2443 if (pcm
->oss
.reg_mask
& 1) {
2444 pcm
->oss
.reg_mask
&= ~1;
2445 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2448 if (pcm
->oss
.reg_mask
& 2) {
2449 pcm
->oss
.reg_mask
&= ~2;
2450 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2457 static int snd_pcm_oss_unregister_minor(struct snd_pcm
*pcm
)
2459 snd_pcm_oss_disconnect_minor(pcm
);
2461 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2462 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2463 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
2467 snd_pcm_oss_proc_done(pcm
);
2472 static struct snd_pcm_notify snd_pcm_oss_notify
=
2474 .n_register
= snd_pcm_oss_register_minor
,
2475 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
2476 .n_unregister
= snd_pcm_oss_unregister_minor
,
2479 static int __init
alsa_pcm_oss_init(void)
2484 /* check device map table */
2485 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
2486 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2487 snd_printk(KERN_ERR
"invalid dsp_map[%d] = %d\n",
2491 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2492 snd_printk(KERN_ERR
"invalid adsp_map[%d] = %d\n",
2497 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
2502 static void __exit
alsa_pcm_oss_exit(void)
2504 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
2507 module_init(alsa_pcm_oss_init
)
2508 module_exit(alsa_pcm_oss_exit
)