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 <sound/core.h>
37 #include <sound/minors.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include "pcm_plugin.h"
41 #include <sound/info.h>
42 #include <linux/soundcard.h>
43 #include <sound/initval.h>
45 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
47 static int dsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 0};
48 static int adsp_map
[SNDRV_CARDS
] = {[0 ... (SNDRV_CARDS
-1)] = 1};
49 static int nonblock_open
= 1;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
52 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
53 MODULE_LICENSE("GPL");
54 module_param_array(dsp_map
, int, NULL
, 0444);
55 MODULE_PARM_DESC(dsp_map
, "PCM device number assigned to 1st OSS device.");
56 module_param_array(adsp_map
, int, NULL
, 0444);
57 MODULE_PARM_DESC(adsp_map
, "PCM device number assigned to 2nd OSS device.");
58 module_param(nonblock_open
, bool, 0644);
59 MODULE_PARM_DESC(nonblock_open
, "Don't block opening busy PCM devices.");
60 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM
);
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1
);
63 extern int snd_mixer_oss_ioctl_card(snd_card_t
*card
, unsigned int cmd
, unsigned long arg
);
64 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t
*pcm_oss_file
);
65 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t
*pcm_oss_file
);
66 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t
*pcm_oss_file
);
68 static inline mm_segment_t
snd_enter_user(void)
70 mm_segment_t fs
= get_fs();
75 static inline void snd_leave_user(mm_segment_t fs
)
80 static int snd_pcm_oss_plugin_clear(snd_pcm_substream_t
*substream
)
82 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
83 snd_pcm_plugin_t
*plugin
, *next
;
85 plugin
= runtime
->oss
.plugin_first
;
88 snd_pcm_plugin_free(plugin
);
91 runtime
->oss
.plugin_first
= runtime
->oss
.plugin_last
= NULL
;
95 static int snd_pcm_plugin_insert(snd_pcm_plugin_t
*plugin
)
97 snd_pcm_runtime_t
*runtime
= plugin
->plug
->runtime
;
98 plugin
->next
= runtime
->oss
.plugin_first
;
100 if (runtime
->oss
.plugin_first
) {
101 runtime
->oss
.plugin_first
->prev
= plugin
;
102 runtime
->oss
.plugin_first
= plugin
;
104 runtime
->oss
.plugin_last
=
105 runtime
->oss
.plugin_first
= plugin
;
110 int snd_pcm_plugin_append(snd_pcm_plugin_t
*plugin
)
112 snd_pcm_runtime_t
*runtime
= plugin
->plug
->runtime
;
114 plugin
->prev
= runtime
->oss
.plugin_last
;
115 if (runtime
->oss
.plugin_last
) {
116 runtime
->oss
.plugin_last
->next
= plugin
;
117 runtime
->oss
.plugin_last
= plugin
;
119 runtime
->oss
.plugin_last
=
120 runtime
->oss
.plugin_first
= plugin
;
125 static long snd_pcm_oss_bytes(snd_pcm_substream_t
*substream
, long frames
)
127 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
128 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
129 long bytes
= frames_to_bytes(runtime
, frames
);
130 if (buffer_size
== runtime
->oss
.buffer_bytes
)
132 #if BITS_PER_LONG >= 64
133 return runtime
->oss
.buffer_bytes
* bytes
/ buffer_size
;
136 u64 bsize
= (u64
)runtime
->oss
.buffer_bytes
* (u64
)bytes
;
138 div64_32(&bsize
, buffer_size
, &rem
);
144 static long snd_pcm_alsa_frames(snd_pcm_substream_t
*substream
, long bytes
)
146 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
147 long buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
148 if (buffer_size
== runtime
->oss
.buffer_bytes
)
149 return bytes_to_frames(runtime
, bytes
);
150 return bytes_to_frames(runtime
, (buffer_size
* bytes
) / runtime
->oss
.buffer_bytes
);
153 static int snd_pcm_oss_format_from(int format
)
156 case AFMT_MU_LAW
: return SNDRV_PCM_FORMAT_MU_LAW
;
157 case AFMT_A_LAW
: return SNDRV_PCM_FORMAT_A_LAW
;
158 case AFMT_IMA_ADPCM
: return SNDRV_PCM_FORMAT_IMA_ADPCM
;
159 case AFMT_U8
: return SNDRV_PCM_FORMAT_U8
;
160 case AFMT_S16_LE
: return SNDRV_PCM_FORMAT_S16_LE
;
161 case AFMT_S16_BE
: return SNDRV_PCM_FORMAT_S16_BE
;
162 case AFMT_S8
: return SNDRV_PCM_FORMAT_S8
;
163 case AFMT_U16_LE
: return SNDRV_PCM_FORMAT_U16_LE
;
164 case AFMT_U16_BE
: return SNDRV_PCM_FORMAT_U16_BE
;
165 case AFMT_MPEG
: return SNDRV_PCM_FORMAT_MPEG
;
166 default: return SNDRV_PCM_FORMAT_U8
;
170 static int snd_pcm_oss_format_to(int format
)
173 case SNDRV_PCM_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
174 case SNDRV_PCM_FORMAT_A_LAW
: return AFMT_A_LAW
;
175 case SNDRV_PCM_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
176 case SNDRV_PCM_FORMAT_U8
: return AFMT_U8
;
177 case SNDRV_PCM_FORMAT_S16_LE
: return AFMT_S16_LE
;
178 case SNDRV_PCM_FORMAT_S16_BE
: return AFMT_S16_BE
;
179 case SNDRV_PCM_FORMAT_S8
: return AFMT_S8
;
180 case SNDRV_PCM_FORMAT_U16_LE
: return AFMT_U16_LE
;
181 case SNDRV_PCM_FORMAT_U16_BE
: return AFMT_U16_BE
;
182 case SNDRV_PCM_FORMAT_MPEG
: return AFMT_MPEG
;
183 default: return -EINVAL
;
187 static int snd_pcm_oss_period_size(snd_pcm_substream_t
*substream
,
188 snd_pcm_hw_params_t
*oss_params
,
189 snd_pcm_hw_params_t
*slave_params
)
192 size_t oss_buffer_size
, oss_period_size
, oss_periods
;
193 size_t min_period_size
, max_period_size
;
194 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
195 size_t oss_frame_size
;
197 oss_frame_size
= snd_pcm_format_physical_width(params_format(oss_params
)) *
198 params_channels(oss_params
) / 8;
200 oss_buffer_size
= snd_pcm_plug_client_size(substream
,
201 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, NULL
)) * oss_frame_size
;
202 oss_buffer_size
= 1 << ld2(oss_buffer_size
);
203 if (atomic_read(&runtime
->mmap_count
)) {
204 if (oss_buffer_size
> runtime
->oss
.mmap_bytes
)
205 oss_buffer_size
= runtime
->oss
.mmap_bytes
;
208 if (substream
->oss
.setup
&&
209 substream
->oss
.setup
->period_size
> 16)
210 oss_period_size
= substream
->oss
.setup
->period_size
;
211 else if (runtime
->oss
.fragshift
) {
212 oss_period_size
= 1 << runtime
->oss
.fragshift
;
213 if (oss_period_size
> oss_buffer_size
/ 2)
214 oss_period_size
= oss_buffer_size
/ 2;
217 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
219 oss_period_size
= oss_buffer_size
;
221 oss_period_size
/= 2;
222 } while (oss_period_size
> bytes_per_sec
);
223 if (runtime
->oss
.subdivision
== 0) {
225 if (oss_period_size
/ sd
> 4096)
227 if (oss_period_size
/ sd
< 4096)
230 sd
= runtime
->oss
.subdivision
;
231 oss_period_size
/= sd
;
232 if (oss_period_size
< 16)
233 oss_period_size
= 16;
236 min_period_size
= snd_pcm_plug_client_size(substream
,
237 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
238 min_period_size
*= oss_frame_size
;
239 min_period_size
= 1 << (ld2(min_period_size
- 1) + 1);
240 if (oss_period_size
< min_period_size
)
241 oss_period_size
= min_period_size
;
243 max_period_size
= snd_pcm_plug_client_size(substream
,
244 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
245 max_period_size
*= oss_frame_size
;
246 max_period_size
= 1 << ld2(max_period_size
);
247 if (oss_period_size
> max_period_size
)
248 oss_period_size
= max_period_size
;
250 oss_periods
= oss_buffer_size
/ oss_period_size
;
252 if (substream
->oss
.setup
) {
253 if (substream
->oss
.setup
->periods
> 1)
254 oss_periods
= substream
->oss
.setup
->periods
;
257 s
= snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
258 if (runtime
->oss
.maxfrags
&& s
> runtime
->oss
.maxfrags
)
259 s
= runtime
->oss
.maxfrags
;
263 s
= snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIODS
, NULL
);
269 while (oss_period_size
* oss_periods
> oss_buffer_size
)
270 oss_period_size
/= 2;
272 snd_assert(oss_period_size
>= 16, return -EINVAL
);
273 runtime
->oss
.period_bytes
= oss_period_size
;
274 runtime
->oss
.period_frames
= 1;
275 runtime
->oss
.periods
= oss_periods
;
279 static int choose_rate(snd_pcm_substream_t
*substream
,
280 snd_pcm_hw_params_t
*params
, unsigned int best_rate
)
283 snd_pcm_hw_params_t
*save
;
284 unsigned int rate
, prev
;
286 save
= kmalloc(sizeof(*save
), GFP_KERNEL
);
290 it
= hw_param_interval(save
, SNDRV_PCM_HW_PARAM_RATE
);
292 /* try multiples of the best rate */
295 if (it
->max
< rate
|| (it
->max
== rate
&& it
->openmax
))
297 if (it
->min
< rate
|| (it
->min
== rate
&& !it
->openmin
)) {
299 ret
= snd_pcm_hw_param_set(substream
, params
,
300 SNDRV_PCM_HW_PARAM_RATE
,
302 if (ret
== (int)rate
) {
314 /* not found, use the nearest rate */
316 return snd_pcm_hw_param_near(substream
, params
, SNDRV_PCM_HW_PARAM_RATE
, best_rate
, NULL
);
319 static int snd_pcm_oss_change_params(snd_pcm_substream_t
*substream
)
321 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
322 snd_pcm_hw_params_t
*params
, *sparams
;
323 snd_pcm_sw_params_t
*sw_params
;
324 ssize_t oss_buffer_size
, oss_period_size
;
325 size_t oss_frame_size
;
328 int format
, sformat
, n
;
329 snd_mask_t sformat_mask
;
332 sw_params
= kmalloc(sizeof(*sw_params
), GFP_KERNEL
);
333 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
334 sparams
= kmalloc(sizeof(*sparams
), GFP_KERNEL
);
335 if (!sw_params
|| !params
|| !sparams
) {
336 snd_printd("No memory\n");
341 if (atomic_read(&runtime
->mmap_count
)) {
344 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
345 direct
= (setup
!= NULL
&& setup
->direct
);
348 _snd_pcm_hw_params_any(sparams
);
349 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
350 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
351 snd_mask_none(&mask
);
352 if (atomic_read(&runtime
->mmap_count
))
353 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
355 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
357 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
359 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
361 snd_printd("No usable accesses\n");
365 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
366 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
368 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
370 sformat_mask
= *hw_param_mask(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
374 sformat
= snd_pcm_plug_slave_format(format
, &sformat_mask
);
376 if (sformat
< 0 || !snd_mask_test(&sformat_mask
, sformat
)) {
377 for (sformat
= 0; sformat
<= SNDRV_PCM_FORMAT_LAST
; sformat
++) {
378 if (snd_mask_test(&sformat_mask
, sformat
) &&
379 snd_pcm_oss_format_to(sformat
) >= 0)
382 if (sformat
> SNDRV_PCM_FORMAT_LAST
) {
383 snd_printd("Cannot find a format!!!\n");
388 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, sformat
, 0);
389 snd_assert(err
>= 0, goto failure
);
392 memcpy(params
, sparams
, sizeof(*params
));
394 _snd_pcm_hw_params_any(params
);
395 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
396 SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
397 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
398 snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
399 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
400 runtime
->oss
.channels
, 0);
401 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
402 runtime
->oss
.rate
, 0);
403 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
404 params_access(params
), params_format(params
),
405 params_channels(params
), params_rate(params
));
407 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
408 params_access(sparams
), params_format(sparams
),
409 params_channels(sparams
), params_rate(sparams
));
411 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
412 params_channels(params
) / 8;
414 snd_pcm_oss_plugin_clear(substream
);
416 /* add necessary plugins */
417 snd_pcm_oss_plugin_clear(substream
);
418 if ((err
= snd_pcm_plug_format_plugins(substream
,
421 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err
);
422 snd_pcm_oss_plugin_clear(substream
);
425 if (runtime
->oss
.plugin_first
) {
426 snd_pcm_plugin_t
*plugin
;
427 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
428 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err
);
429 snd_pcm_oss_plugin_clear(substream
);
432 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
433 err
= snd_pcm_plugin_append(plugin
);
435 err
= snd_pcm_plugin_insert(plugin
);
438 snd_pcm_oss_plugin_clear(substream
);
444 err
= snd_pcm_oss_period_size(substream
, params
, sparams
);
448 n
= snd_pcm_plug_slave_size(substream
, runtime
->oss
.period_bytes
/ oss_frame_size
);
449 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, n
, NULL
);
450 snd_assert(err
>= 0, goto failure
);
452 err
= snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_PERIODS
,
453 runtime
->oss
.periods
, NULL
);
454 snd_assert(err
>= 0, goto failure
);
456 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
458 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_HW_PARAMS
, sparams
)) < 0) {
459 snd_printd("HW_PARAMS failed: %i\n", err
);
463 memset(sw_params
, 0, sizeof(*sw_params
));
464 if (runtime
->oss
.trigger
) {
465 sw_params
->start_threshold
= 1;
467 sw_params
->start_threshold
= runtime
->boundary
;
469 if (atomic_read(&runtime
->mmap_count
) || substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
470 sw_params
->stop_threshold
= runtime
->boundary
;
472 sw_params
->stop_threshold
= runtime
->buffer_size
;
473 sw_params
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
474 sw_params
->period_step
= 1;
475 sw_params
->sleep_min
= 0;
476 sw_params
->avail_min
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
?
477 1 : runtime
->period_size
;
478 sw_params
->xfer_align
= 1;
479 if (atomic_read(&runtime
->mmap_count
) ||
480 (substream
->oss
.setup
&& substream
->oss
.setup
->nosilence
)) {
481 sw_params
->silence_threshold
= 0;
482 sw_params
->silence_size
= 0;
484 snd_pcm_uframes_t frames
;
485 frames
= runtime
->period_size
+ 16;
486 if (frames
> runtime
->buffer_size
)
487 frames
= runtime
->buffer_size
;
488 sw_params
->silence_threshold
= frames
;
489 sw_params
->silence_size
= frames
;
492 if ((err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_SW_PARAMS
, sw_params
)) < 0) {
493 snd_printd("SW_PARAMS failed: %i\n", err
);
497 runtime
->oss
.periods
= params_periods(sparams
);
498 oss_period_size
= snd_pcm_plug_client_size(substream
, params_period_size(sparams
));
499 snd_assert(oss_period_size
>= 0, err
= -EINVAL
; goto failure
);
500 if (runtime
->oss
.plugin_first
) {
501 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
505 oss_period_size
*= oss_frame_size
;
507 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
508 snd_assert(oss_buffer_size
>= 0, err
= -EINVAL
; goto failure
);
510 runtime
->oss
.period_bytes
= oss_period_size
;
511 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
513 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
514 runtime
->oss
.period_bytes
,
515 runtime
->oss
.buffer_bytes
);
516 pdprintf("slave: period_size = %i, buffer_size = %i\n",
517 params_period_size(sparams
),
518 params_buffer_size(sparams
));
520 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
521 runtime
->oss
.channels
= params_channels(params
);
522 runtime
->oss
.rate
= params_rate(params
);
524 runtime
->oss
.params
= 0;
525 runtime
->oss
.prepare
= 1;
526 vfree(runtime
->oss
.buffer
);
527 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
528 runtime
->oss
.buffer_used
= 0;
529 if (runtime
->dma_area
)
530 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
532 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
542 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t
*pcm_oss_file
, snd_pcm_substream_t
**r_substream
)
545 snd_pcm_substream_t
*asubstream
= NULL
, *substream
;
547 for (idx
= 0; idx
< 2; idx
++) {
548 substream
= pcm_oss_file
->streams
[idx
];
549 if (substream
== NULL
)
551 if (asubstream
== NULL
)
552 asubstream
= substream
;
553 if (substream
->runtime
->oss
.params
) {
554 err
= snd_pcm_oss_change_params(substream
);
559 snd_assert(asubstream
!= NULL
, return -EIO
);
561 *r_substream
= asubstream
;
565 static int snd_pcm_oss_prepare(snd_pcm_substream_t
*substream
)
568 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
570 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
572 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
575 runtime
->oss
.prepare
= 0;
576 runtime
->oss
.prev_hw_ptr_interrupt
= 0;
577 runtime
->oss
.period_ptr
= 0;
578 runtime
->oss
.buffer_used
= 0;
583 static int snd_pcm_oss_make_ready(snd_pcm_substream_t
*substream
)
585 snd_pcm_runtime_t
*runtime
;
588 if (substream
== NULL
)
590 runtime
= substream
->runtime
;
591 if (runtime
->oss
.params
) {
592 err
= snd_pcm_oss_change_params(substream
);
596 if (runtime
->oss
.prepare
) {
597 err
= snd_pcm_oss_prepare(substream
);
604 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t
*substream
, snd_pcm_sframes_t
*delay
)
606 snd_pcm_runtime_t
*runtime
;
607 snd_pcm_uframes_t frames
;
611 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
614 runtime
= substream
->runtime
;
615 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
617 /* in case of overrun, skip whole periods like OSS/Linux driver does */
618 /* until avail(delay) <= buffer_size */
619 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
620 frames
/= runtime
->period_size
;
621 frames
*= runtime
->period_size
;
622 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
629 snd_pcm_sframes_t
snd_pcm_oss_write3(snd_pcm_substream_t
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
631 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
634 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
635 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
637 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
638 printk("pcm_oss: write: recovering from XRUN\n");
640 printk("pcm_oss: write: recovering from SUSPEND\n");
642 ret
= snd_pcm_oss_prepare(substream
);
648 fs
= snd_enter_user();
649 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
652 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
654 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
656 /* test, if we can't store new data, because the stream */
657 /* has not been started */
658 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
664 snd_pcm_sframes_t
snd_pcm_oss_read3(snd_pcm_substream_t
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
666 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
667 snd_pcm_sframes_t delay
;
670 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
671 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
673 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
674 printk("pcm_oss: read: recovering from XRUN\n");
676 printk("pcm_oss: read: recovering from SUSPEND\n");
678 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
681 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
682 ret
= snd_pcm_oss_prepare(substream
);
686 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
691 fs
= snd_enter_user();
692 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
695 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
698 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
699 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
705 if (ret
!= -ESTRPIPE
)
711 snd_pcm_sframes_t
snd_pcm_oss_writev3(snd_pcm_substream_t
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
713 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
716 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
717 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
719 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
720 printk("pcm_oss: writev: recovering from XRUN\n");
722 printk("pcm_oss: writev: recovering from SUSPEND\n");
724 ret
= snd_pcm_oss_prepare(substream
);
730 fs
= snd_enter_user();
731 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
734 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
736 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
739 /* test, if we can't store new data, because the stream */
740 /* has not been started */
741 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
747 snd_pcm_sframes_t
snd_pcm_oss_readv3(snd_pcm_substream_t
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
749 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
752 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
753 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
755 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
756 printk("pcm_oss: readv: recovering from XRUN\n");
758 printk("pcm_oss: readv: recovering from SUSPEND\n");
760 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
763 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
764 ret
= snd_pcm_oss_prepare(substream
);
770 fs
= snd_enter_user();
771 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
774 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
776 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
782 static ssize_t
snd_pcm_oss_write2(snd_pcm_substream_t
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
784 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
785 snd_pcm_sframes_t frames
, frames1
;
786 if (runtime
->oss
.plugin_first
) {
787 snd_pcm_plugin_channel_t
*channels
;
788 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
790 if (copy_from_user(runtime
->oss
.buffer
, (const char __user
*)buf
, bytes
))
792 buf
= runtime
->oss
.buffer
;
794 frames
= bytes
/ oss_frame_bytes
;
795 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
798 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
801 bytes
= frames1
* oss_frame_bytes
;
803 frames
= bytes_to_frames(runtime
, bytes
);
804 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
807 bytes
= frames_to_bytes(runtime
, frames1
);
812 static ssize_t
snd_pcm_oss_write1(snd_pcm_substream_t
*substream
, const char __user
*buf
, size_t bytes
)
816 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
818 if (atomic_read(&runtime
->mmap_count
))
821 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
824 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
826 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
827 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
829 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
))
830 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
832 runtime
->oss
.buffer_used
+= tmp
;
836 if ((substream
->oss
.setup
!= NULL
&& substream
->oss
.setup
->partialfrag
) ||
837 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
838 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
839 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
841 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
842 runtime
->oss
.bytes
+= tmp
;
843 runtime
->oss
.period_ptr
+= tmp
;
844 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
845 if (runtime
->oss
.period_ptr
== 0 ||
846 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
847 runtime
->oss
.buffer_used
= 0;
848 else if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0)
849 return xfer
> 0 ? xfer
: -EAGAIN
;
852 tmp
= snd_pcm_oss_write2(substream
, (const char *)buf
, runtime
->oss
.period_bytes
, 0);
854 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
855 runtime
->oss
.bytes
+= tmp
;
859 if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0 &&
860 tmp
!= runtime
->oss
.period_bytes
)
867 static ssize_t
snd_pcm_oss_read2(snd_pcm_substream_t
*substream
, char *buf
, size_t bytes
, int in_kernel
)
869 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
870 snd_pcm_sframes_t frames
, frames1
;
871 char __user
*final_dst
= (char __user
*)buf
;
872 if (runtime
->oss
.plugin_first
) {
873 snd_pcm_plugin_channel_t
*channels
;
874 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
876 buf
= runtime
->oss
.buffer
;
877 frames
= bytes
/ oss_frame_bytes
;
878 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
881 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
884 bytes
= frames1
* oss_frame_bytes
;
885 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
888 frames
= bytes_to_frames(runtime
, bytes
);
889 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
892 bytes
= frames_to_bytes(runtime
, frames1
);
897 static ssize_t
snd_pcm_oss_read1(snd_pcm_substream_t
*substream
, char __user
*buf
, size_t bytes
)
901 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
903 if (atomic_read(&runtime
->mmap_count
))
906 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
909 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
910 if (runtime
->oss
.buffer_used
== 0) {
911 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
913 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
914 runtime
->oss
.bytes
+= tmp
;
915 runtime
->oss
.period_ptr
= tmp
;
916 runtime
->oss
.buffer_used
= tmp
;
919 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
920 tmp
= runtime
->oss
.buffer_used
;
921 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
))
922 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
926 runtime
->oss
.buffer_used
-= tmp
;
928 tmp
= snd_pcm_oss_read2(substream
, (char *)buf
, runtime
->oss
.period_bytes
, 0);
930 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
931 runtime
->oss
.bytes
+= tmp
;
940 static int snd_pcm_oss_reset(snd_pcm_oss_file_t
*pcm_oss_file
)
942 snd_pcm_substream_t
*substream
;
944 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
945 if (substream
!= NULL
) {
946 snd_pcm_kernel_playback_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
947 substream
->runtime
->oss
.prepare
= 1;
949 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
950 if (substream
!= NULL
) {
951 snd_pcm_kernel_capture_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
952 substream
->runtime
->oss
.prepare
= 1;
957 static int snd_pcm_oss_post(snd_pcm_oss_file_t
*pcm_oss_file
)
959 snd_pcm_substream_t
*substream
;
962 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
963 if (substream
!= NULL
) {
964 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
966 snd_pcm_kernel_playback_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
968 /* note: all errors from the start action are ignored */
969 /* OSS apps do not know, how to handle them */
973 static int snd_pcm_oss_sync1(snd_pcm_substream_t
*substream
, size_t size
)
975 snd_pcm_runtime_t
*runtime
;
980 runtime
= substream
->runtime
;
981 init_waitqueue_entry(&wait
, current
);
982 add_wait_queue(&runtime
->sleep
, &wait
);
984 printk("sync1: size = %li\n", size
);
987 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
989 runtime
->oss
.buffer_used
= 0;
993 if (result
!= 0 && result
!= -EAGAIN
)
996 set_current_state(TASK_INTERRUPTIBLE
);
997 snd_pcm_stream_lock_irq(substream
);
998 res
= runtime
->status
->state
;
999 snd_pcm_stream_unlock_irq(substream
);
1000 if (res
!= SNDRV_PCM_STATE_RUNNING
) {
1001 set_current_state(TASK_RUNNING
);
1004 res
= schedule_timeout(10 * HZ
);
1005 if (signal_pending(current
)) {
1006 result
= -ERESTARTSYS
;
1010 snd_printk(KERN_ERR
"OSS sync error - DMA timeout\n");
1015 remove_wait_queue(&runtime
->sleep
, &wait
);
1019 static int snd_pcm_oss_sync(snd_pcm_oss_file_t
*pcm_oss_file
)
1022 unsigned int saved_f_flags
;
1023 snd_pcm_substream_t
*substream
;
1024 snd_pcm_runtime_t
*runtime
;
1025 snd_pcm_format_t format
;
1026 unsigned long width
;
1029 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1030 if (substream
!= NULL
) {
1031 runtime
= substream
->runtime
;
1032 if (atomic_read(&runtime
->mmap_count
))
1034 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1036 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1037 width
= snd_pcm_format_physical_width(format
);
1038 if (runtime
->oss
.buffer_used
> 0) {
1040 printk("sync: buffer_used\n");
1042 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1043 snd_pcm_format_set_silence(format
,
1044 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1046 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1049 } else if (runtime
->oss
.period_ptr
> 0) {
1051 printk("sync: period_ptr\n");
1053 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1054 snd_pcm_format_set_silence(format
,
1055 runtime
->oss
.buffer
,
1057 err
= snd_pcm_oss_sync1(substream
, size
);
1062 * The ALSA's period might be a bit large than OSS one.
1063 * Fill the remain portion of ALSA period with zeros.
1065 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1067 size
= runtime
->period_size
- size
;
1068 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
1069 size
= (runtime
->frame_bits
* size
) / 8;
1072 size_t size1
= size
< runtime
->oss
.period_bytes
? size
: runtime
->oss
.period_bytes
;
1075 size1
/= runtime
->sample_bits
;
1076 snd_pcm_format_set_silence(runtime
->format
,
1077 runtime
->oss
.buffer
,
1079 fs
= snd_enter_user();
1080 snd_pcm_lib_write(substream
, (void __user
*)runtime
->oss
.buffer
, size1
);
1083 } else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
1084 void __user
*buffers
[runtime
->channels
];
1085 memset(buffers
, 0, runtime
->channels
* sizeof(void *));
1086 snd_pcm_lib_writev(substream
, buffers
, size
);
1090 * finish sync: drain the buffer
1093 saved_f_flags
= substream
->ffile
->f_flags
;
1094 substream
->ffile
->f_flags
&= ~O_NONBLOCK
;
1095 err
= snd_pcm_kernel_playback_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1096 substream
->ffile
->f_flags
= saved_f_flags
;
1099 runtime
->oss
.prepare
= 1;
1102 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1103 if (substream
!= NULL
) {
1104 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1106 runtime
= substream
->runtime
;
1107 err
= snd_pcm_kernel_capture_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1110 runtime
->oss
.buffer_used
= 0;
1111 runtime
->oss
.prepare
= 1;
1116 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t
*pcm_oss_file
, int rate
)
1120 for (idx
= 1; idx
>= 0; --idx
) {
1121 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1122 snd_pcm_runtime_t
*runtime
;
1123 if (substream
== NULL
)
1125 runtime
= substream
->runtime
;
1128 else if (rate
> 192000)
1130 if (runtime
->oss
.rate
!= rate
) {
1131 runtime
->oss
.params
= 1;
1132 runtime
->oss
.rate
= rate
;
1135 return snd_pcm_oss_get_rate(pcm_oss_file
);
1138 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t
*pcm_oss_file
)
1140 snd_pcm_substream_t
*substream
;
1143 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1145 return substream
->runtime
->oss
.rate
;
1148 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t
*pcm_oss_file
, unsigned int channels
)
1155 for (idx
= 1; idx
>= 0; --idx
) {
1156 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1157 snd_pcm_runtime_t
*runtime
;
1158 if (substream
== NULL
)
1160 runtime
= substream
->runtime
;
1161 if (runtime
->oss
.channels
!= channels
) {
1162 runtime
->oss
.params
= 1;
1163 runtime
->oss
.channels
= channels
;
1166 return snd_pcm_oss_get_channels(pcm_oss_file
);
1169 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t
*pcm_oss_file
)
1171 snd_pcm_substream_t
*substream
;
1174 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1176 return substream
->runtime
->oss
.channels
;
1179 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t
*pcm_oss_file
)
1181 snd_pcm_substream_t
*substream
;
1184 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1186 return substream
->runtime
->oss
.period_bytes
;
1189 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t
*pcm_oss_file
)
1191 snd_pcm_substream_t
*substream
;
1194 snd_pcm_hw_params_t
*params
;
1195 unsigned int formats
= 0;
1196 snd_mask_t format_mask
;
1199 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1201 if (atomic_read(&substream
->runtime
->mmap_count
)) {
1204 snd_pcm_oss_setup_t
*setup
= substream
->oss
.setup
;
1205 direct
= (setup
!= NULL
&& setup
->direct
);
1208 return AFMT_MU_LAW
| AFMT_U8
|
1209 AFMT_S16_LE
| AFMT_S16_BE
|
1210 AFMT_S8
| AFMT_U16_LE
|
1212 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1215 _snd_pcm_hw_params_any(params
);
1216 err
= snd_pcm_hw_refine(substream
, params
);
1217 format_mask
= *hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1219 snd_assert(err
>= 0, return err
);
1220 for (fmt
= 0; fmt
< 32; ++fmt
) {
1221 if (snd_mask_test(&format_mask
, fmt
)) {
1222 int f
= snd_pcm_oss_format_to(fmt
);
1230 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t
*pcm_oss_file
, int format
)
1234 if (format
!= AFMT_QUERY
) {
1235 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1236 if (!(formats
& format
))
1238 for (idx
= 1; idx
>= 0; --idx
) {
1239 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1240 snd_pcm_runtime_t
*runtime
;
1241 if (substream
== NULL
)
1243 runtime
= substream
->runtime
;
1244 if (runtime
->oss
.format
!= format
) {
1245 runtime
->oss
.params
= 1;
1246 runtime
->oss
.format
= format
;
1250 return snd_pcm_oss_get_format(pcm_oss_file
);
1253 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t
*pcm_oss_file
)
1255 snd_pcm_substream_t
*substream
;
1258 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1260 return substream
->runtime
->oss
.format
;
1263 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t
*substream
, int subdivide
)
1265 snd_pcm_runtime_t
*runtime
;
1267 if (substream
== NULL
)
1269 runtime
= substream
->runtime
;
1270 if (subdivide
== 0) {
1271 subdivide
= runtime
->oss
.subdivision
;
1276 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1278 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1279 subdivide
!= 8 && subdivide
!= 16)
1281 runtime
->oss
.subdivision
= subdivide
;
1282 runtime
->oss
.params
= 1;
1286 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t
*pcm_oss_file
, int subdivide
)
1288 int err
= -EINVAL
, idx
;
1290 for (idx
= 1; idx
>= 0; --idx
) {
1291 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1292 if (substream
== NULL
)
1294 if ((err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
)) < 0)
1300 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t
*substream
, unsigned int val
)
1302 snd_pcm_runtime_t
*runtime
;
1304 if (substream
== NULL
)
1306 runtime
= substream
->runtime
;
1307 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1309 runtime
->oss
.fragshift
= val
& 0xffff;
1310 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
1311 if (runtime
->oss
.fragshift
< 4) /* < 16 */
1312 runtime
->oss
.fragshift
= 4;
1313 if (runtime
->oss
.maxfrags
< 2)
1314 runtime
->oss
.maxfrags
= 2;
1315 runtime
->oss
.params
= 1;
1319 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t
*pcm_oss_file
, unsigned int val
)
1321 int err
= -EINVAL
, idx
;
1323 for (idx
= 1; idx
>= 0; --idx
) {
1324 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1325 if (substream
== NULL
)
1327 if ((err
= snd_pcm_oss_set_fragment1(substream
, val
)) < 0)
1333 static int snd_pcm_oss_nonblock(struct file
* file
)
1335 file
->f_flags
|= O_NONBLOCK
;
1339 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t
*substream
, int res
)
1342 if (substream
== NULL
) {
1343 res
&= ~DSP_CAP_DUPLEX
;
1346 #ifdef DSP_CAP_MULTI
1347 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1348 if (substream
->pstr
->substream_count
> 1)
1349 res
|= DSP_CAP_MULTI
;
1351 /* DSP_CAP_REALTIME is set all times: */
1352 /* all ALSA drivers can return actual pointer in ring buffer */
1353 #if defined(DSP_CAP_REALTIME) && 0
1355 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1356 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
1357 res
&= ~DSP_CAP_REALTIME
;
1363 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t
*pcm_oss_file
)
1367 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
1368 for (idx
= 0; idx
< 2; idx
++) {
1369 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[idx
];
1370 result
= snd_pcm_oss_get_caps1(substream
, result
);
1372 result
|= 0x0001; /* revision - same as SB AWE 64 */
1376 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t hw_ptr
)
1378 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1379 snd_pcm_uframes_t appl_ptr
;
1380 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
1381 appl_ptr
%= runtime
->boundary
;
1382 runtime
->control
->appl_ptr
= appl_ptr
;
1385 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t
*pcm_oss_file
, int trigger
)
1387 snd_pcm_runtime_t
*runtime
;
1388 snd_pcm_substream_t
*psubstream
= NULL
, *csubstream
= NULL
;
1392 printk("pcm_oss: trigger = 0x%x\n", trigger
);
1395 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1396 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1399 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
1403 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
1407 runtime
= psubstream
->runtime
;
1408 if (trigger
& PCM_ENABLE_OUTPUT
) {
1409 if (runtime
->oss
.trigger
)
1411 if (atomic_read(&psubstream
->runtime
->mmap_count
))
1412 snd_pcm_oss_simulate_fill(psubstream
, runtime
->hw_ptr_interrupt
);
1413 runtime
->oss
.trigger
= 1;
1414 runtime
->start_threshold
= 1;
1415 cmd
= SNDRV_PCM_IOCTL_START
;
1417 if (!runtime
->oss
.trigger
)
1419 runtime
->oss
.trigger
= 0;
1420 runtime
->start_threshold
= runtime
->boundary
;
1421 cmd
= SNDRV_PCM_IOCTL_DROP
;
1422 runtime
->oss
.prepare
= 1;
1424 err
= snd_pcm_kernel_playback_ioctl(psubstream
, cmd
, NULL
);
1430 runtime
= csubstream
->runtime
;
1431 if (trigger
& PCM_ENABLE_INPUT
) {
1432 if (runtime
->oss
.trigger
)
1434 runtime
->oss
.trigger
= 1;
1435 runtime
->start_threshold
= 1;
1436 cmd
= SNDRV_PCM_IOCTL_START
;
1438 if (!runtime
->oss
.trigger
)
1440 runtime
->oss
.trigger
= 0;
1441 runtime
->start_threshold
= runtime
->boundary
;
1442 cmd
= SNDRV_PCM_IOCTL_DROP
;
1443 runtime
->oss
.prepare
= 1;
1445 err
= snd_pcm_kernel_capture_ioctl(csubstream
, cmd
, NULL
);
1453 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t
*pcm_oss_file
)
1455 snd_pcm_substream_t
*psubstream
= NULL
, *csubstream
= NULL
;
1458 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1459 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1460 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
1461 result
|= PCM_ENABLE_OUTPUT
;
1462 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
1463 result
|= PCM_ENABLE_INPUT
;
1467 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t
*pcm_oss_file
)
1469 snd_pcm_substream_t
*substream
;
1470 snd_pcm_runtime_t
*runtime
;
1471 snd_pcm_sframes_t delay
;
1474 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1475 if (substream
== NULL
)
1477 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1479 runtime
= substream
->runtime
;
1480 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
1482 err
= snd_pcm_kernel_playback_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1484 delay
= 0; /* hack for broken OSS applications */
1487 return snd_pcm_oss_bytes(substream
, delay
);
1490 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
1492 snd_pcm_substream_t
*substream
;
1493 snd_pcm_runtime_t
*runtime
;
1494 snd_pcm_sframes_t delay
;
1496 struct count_info info
;
1501 substream
= pcm_oss_file
->streams
[stream
];
1502 if (substream
== NULL
)
1504 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1506 runtime
= substream
->runtime
;
1507 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
1508 memset(&info
, 0, sizeof(info
));
1509 if (copy_to_user(_info
, &info
, sizeof(info
)))
1513 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1514 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1515 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
1520 fixup
= runtime
->oss
.buffer_used
;
1523 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1524 fixup
= -runtime
->oss
.buffer_used
;
1528 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
1529 if (atomic_read(&runtime
->mmap_count
)) {
1530 snd_pcm_sframes_t n
;
1531 n
= (delay
= runtime
->hw_ptr_interrupt
) - runtime
->oss
.prev_hw_ptr_interrupt
;
1533 n
+= runtime
->boundary
;
1534 info
.blocks
= n
/ runtime
->period_size
;
1535 runtime
->oss
.prev_hw_ptr_interrupt
= delay
;
1536 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1537 snd_pcm_oss_simulate_fill(substream
, delay
);
1538 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
1540 delay
= snd_pcm_oss_bytes(substream
, delay
) + fixup
;
1541 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
1542 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1543 if (runtime
->oss
.bytes
== 0)
1545 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
1547 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
1550 if (copy_to_user(_info
, &info
, sizeof(info
)))
1555 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
1557 snd_pcm_substream_t
*substream
;
1558 snd_pcm_runtime_t
*runtime
;
1559 snd_pcm_sframes_t avail
;
1561 struct audio_buf_info info
;
1566 substream
= pcm_oss_file
->streams
[stream
];
1567 if (substream
== NULL
)
1569 runtime
= substream
->runtime
;
1571 if (runtime
->oss
.params
&&
1572 (err
= snd_pcm_oss_change_params(substream
)) < 0)
1575 info
.fragsize
= runtime
->oss
.period_bytes
;
1576 info
.fragstotal
= runtime
->periods
;
1577 if (runtime
->oss
.prepare
) {
1578 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1579 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
1580 info
.fragments
= runtime
->oss
.periods
;
1586 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1587 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
1588 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
1589 avail
= runtime
->buffer_size
;
1593 avail
= runtime
->buffer_size
- avail
;
1594 fixup
= -runtime
->oss
.buffer_used
;
1597 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
1598 fixup
= runtime
->oss
.buffer_used
;
1602 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
1603 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
1607 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
1609 if (copy_to_user(_info
, &info
, sizeof(info
)))
1614 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
1616 // it won't be probably implemented
1617 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1621 static snd_pcm_oss_setup_t
*snd_pcm_oss_look_for_setup(snd_pcm_t
*pcm
, int stream
, const char *task_name
)
1623 const char *ptr
, *ptrl
;
1624 snd_pcm_oss_setup_t
*setup
;
1626 down(&pcm
->streams
[stream
].oss
.setup_mutex
);
1627 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
; setup
= setup
->next
) {
1628 if (!strcmp(setup
->task_name
, task_name
)) {
1629 up(&pcm
->streams
[stream
].oss
.setup_mutex
);
1633 ptr
= ptrl
= task_name
;
1639 if (ptrl
== task_name
) {
1643 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
; setup
= setup
->next
) {
1644 if (!strcmp(setup
->task_name
, ptrl
)) {
1645 up(&pcm
->streams
[stream
].oss
.setup_mutex
);
1650 up(&pcm
->streams
[stream
].oss
.setup_mutex
);
1654 static void snd_pcm_oss_init_substream(snd_pcm_substream_t
*substream
,
1655 snd_pcm_oss_setup_t
*setup
,
1658 snd_pcm_runtime_t
*runtime
;
1660 substream
->oss
.oss
= 1;
1661 substream
->oss
.setup
= setup
;
1662 runtime
= substream
->runtime
;
1663 runtime
->oss
.params
= 1;
1664 runtime
->oss
.trigger
= 1;
1665 runtime
->oss
.rate
= 8000;
1666 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
1667 case SNDRV_MINOR_OSS_PCM_8
:
1668 runtime
->oss
.format
= AFMT_U8
;
1670 case SNDRV_MINOR_OSS_PCM_16
:
1671 runtime
->oss
.format
= AFMT_S16_LE
;
1674 runtime
->oss
.format
= AFMT_MU_LAW
;
1676 runtime
->oss
.channels
= 1;
1677 runtime
->oss
.fragshift
= 0;
1678 runtime
->oss
.maxfrags
= 0;
1679 runtime
->oss
.subdivision
= 0;
1682 static void snd_pcm_oss_release_substream(snd_pcm_substream_t
*substream
)
1684 snd_pcm_runtime_t
*runtime
;
1685 runtime
= substream
->runtime
;
1686 vfree(runtime
->oss
.buffer
);
1687 snd_pcm_oss_plugin_clear(substream
);
1688 substream
->oss
.file
= NULL
;
1689 substream
->oss
.oss
= 0;
1692 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t
*pcm_oss_file
)
1695 snd_assert(pcm_oss_file
!= NULL
, return -ENXIO
);
1696 for (cidx
= 0; cidx
< 2; ++cidx
) {
1697 snd_pcm_substream_t
*substream
= pcm_oss_file
->streams
[cidx
];
1698 snd_pcm_runtime_t
*runtime
;
1699 if (substream
== NULL
)
1701 runtime
= substream
->runtime
;
1703 snd_pcm_stream_lock_irq(substream
);
1704 if (snd_pcm_running(substream
))
1705 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1706 snd_pcm_stream_unlock_irq(substream
);
1707 if (substream
->open_flag
) {
1708 if (substream
->ops
->hw_free
!= NULL
)
1709 substream
->ops
->hw_free(substream
);
1710 substream
->ops
->close(substream
);
1711 substream
->open_flag
= 0;
1713 substream
->ffile
= NULL
;
1714 snd_pcm_oss_release_substream(substream
);
1715 snd_pcm_release_substream(substream
);
1717 kfree(pcm_oss_file
);
1721 static int snd_pcm_oss_open_file(struct file
*file
,
1723 snd_pcm_oss_file_t
**rpcm_oss_file
,
1725 snd_pcm_oss_setup_t
*psetup
,
1726 snd_pcm_oss_setup_t
*csetup
)
1729 snd_pcm_oss_file_t
*pcm_oss_file
;
1730 snd_pcm_substream_t
*psubstream
= NULL
, *csubstream
= NULL
;
1731 unsigned int f_mode
= file
->f_mode
;
1733 snd_assert(rpcm_oss_file
!= NULL
, return -EINVAL
);
1734 *rpcm_oss_file
= NULL
;
1736 pcm_oss_file
= kcalloc(1, sizeof(*pcm_oss_file
), GFP_KERNEL
);
1737 if (pcm_oss_file
== NULL
)
1740 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
1741 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
1742 f_mode
= FMODE_WRITE
;
1743 if ((f_mode
& FMODE_WRITE
) && !(psetup
&& psetup
->disable
)) {
1744 if ((err
= snd_pcm_open_substream(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
1745 &psubstream
)) < 0) {
1746 snd_pcm_oss_release_file(pcm_oss_file
);
1749 pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
] = psubstream
;
1751 if ((f_mode
& FMODE_READ
) && !(csetup
&& csetup
->disable
)) {
1752 if ((err
= snd_pcm_open_substream(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1753 &csubstream
)) < 0) {
1754 if (!(f_mode
& FMODE_WRITE
) || err
!= -ENODEV
) {
1755 snd_pcm_oss_release_file(pcm_oss_file
);
1761 pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
] = csubstream
;
1764 if (psubstream
== NULL
&& csubstream
== NULL
) {
1765 snd_pcm_oss_release_file(pcm_oss_file
);
1768 if (psubstream
!= NULL
) {
1769 psubstream
->oss
.file
= pcm_oss_file
;
1770 err
= snd_pcm_hw_constraints_init(psubstream
);
1772 snd_printd("snd_pcm_hw_constraint_init failed\n");
1773 snd_pcm_oss_release_file(pcm_oss_file
);
1776 if ((err
= psubstream
->ops
->open(psubstream
)) < 0) {
1777 snd_pcm_oss_release_file(pcm_oss_file
);
1780 psubstream
->open_flag
= 1;
1781 err
= snd_pcm_hw_constraints_complete(psubstream
);
1783 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1784 snd_pcm_oss_release_file(pcm_oss_file
);
1787 psubstream
->ffile
= file
;
1788 snd_pcm_oss_init_substream(psubstream
, psetup
, minor
);
1790 if (csubstream
!= NULL
) {
1791 csubstream
->oss
.file
= pcm_oss_file
;
1792 err
= snd_pcm_hw_constraints_init(csubstream
);
1794 snd_printd("snd_pcm_hw_constraint_init failed\n");
1795 snd_pcm_oss_release_file(pcm_oss_file
);
1798 if ((err
= csubstream
->ops
->open(csubstream
)) < 0) {
1799 snd_pcm_oss_release_file(pcm_oss_file
);
1802 csubstream
->open_flag
= 1;
1803 err
= snd_pcm_hw_constraints_complete(csubstream
);
1805 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1806 snd_pcm_oss_release_file(pcm_oss_file
);
1809 csubstream
->ffile
= file
;
1810 snd_pcm_oss_init_substream(csubstream
, csetup
, minor
);
1813 file
->private_data
= pcm_oss_file
;
1814 *rpcm_oss_file
= pcm_oss_file
;
1819 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
1821 int minor
= iminor(inode
);
1822 int cardnum
= SNDRV_MINOR_OSS_CARD(minor
);
1827 snd_pcm_oss_file_t
*pcm_oss_file
;
1828 snd_pcm_oss_setup_t
*psetup
= NULL
, *csetup
= NULL
;
1832 snd_assert(cardnum
>= 0 && cardnum
< SNDRV_CARDS
, return -ENXIO
);
1833 device
= SNDRV_MINOR_OSS_DEVICE(minor
) == SNDRV_MINOR_OSS_PCM1
?
1834 adsp_map
[cardnum
] : dsp_map
[cardnum
];
1836 pcm
= snd_pcm_devices
[(cardnum
* SNDRV_PCM_DEVICES
) + device
];
1841 err
= snd_card_file_add(pcm
->card
, file
);
1844 if (!try_module_get(pcm
->card
->module
)) {
1848 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
1852 if (file
->f_mode
& FMODE_WRITE
)
1853 psetup
= snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, task_name
);
1854 if (file
->f_mode
& FMODE_READ
)
1855 csetup
= snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
, task_name
);
1857 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
1858 if (psetup
&& !psetup
->disable
) {
1859 if (psetup
->nonblock
)
1861 else if (psetup
->block
)
1863 } else if (csetup
&& !csetup
->disable
) {
1864 if (csetup
->nonblock
)
1866 else if (csetup
->block
)
1870 nonblock
= nonblock_open
;
1872 init_waitqueue_entry(&wait
, current
);
1873 add_wait_queue(&pcm
->open_wait
, &wait
);
1874 down(&pcm
->open_mutex
);
1876 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
1877 minor
, psetup
, csetup
);
1880 if (err
== -EAGAIN
) {
1887 set_current_state(TASK_INTERRUPTIBLE
);
1888 up(&pcm
->open_mutex
);
1890 down(&pcm
->open_mutex
);
1891 if (signal_pending(current
)) {
1896 remove_wait_queue(&pcm
->open_wait
, &wait
);
1897 up(&pcm
->open_mutex
);
1903 module_put(pcm
->card
->module
);
1905 snd_card_file_remove(pcm
->card
, file
);
1910 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
1913 snd_pcm_substream_t
*substream
;
1914 snd_pcm_oss_file_t
*pcm_oss_file
;
1916 pcm_oss_file
= file
->private_data
;
1917 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1918 if (substream
== NULL
)
1919 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1920 snd_assert(substream
!= NULL
, return -ENXIO
);
1921 pcm
= substream
->pcm
;
1922 snd_pcm_oss_sync(pcm_oss_file
);
1923 down(&pcm
->open_mutex
);
1924 snd_pcm_oss_release_file(pcm_oss_file
);
1925 up(&pcm
->open_mutex
);
1926 wake_up(&pcm
->open_wait
);
1927 module_put(pcm
->card
->module
);
1928 snd_card_file_remove(pcm
->card
, file
);
1932 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1934 snd_pcm_oss_file_t
*pcm_oss_file
;
1935 int __user
*p
= (int __user
*)arg
;
1938 pcm_oss_file
= file
->private_data
;
1939 if (cmd
== OSS_GETVERSION
)
1940 return put_user(SNDRV_OSS_VERSION
, p
);
1941 if (cmd
== OSS_ALSAEMULVER
)
1942 return put_user(1, p
);
1943 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1944 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1945 snd_pcm_substream_t
*substream
;
1947 for (idx
= 0; idx
< 2; ++idx
) {
1948 substream
= pcm_oss_file
->streams
[idx
];
1949 if (substream
!= NULL
)
1952 snd_assert(substream
!= NULL
, return -ENXIO
);
1953 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
1956 if (((cmd
>> 8) & 0xff) != 'P')
1959 printk("pcm_oss: ioctl = 0x%x\n", cmd
);
1962 case SNDCTL_DSP_RESET
:
1963 return snd_pcm_oss_reset(pcm_oss_file
);
1964 case SNDCTL_DSP_SYNC
:
1965 return snd_pcm_oss_sync(pcm_oss_file
);
1966 case SNDCTL_DSP_SPEED
:
1967 if (get_user(res
, p
))
1969 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
1971 return put_user(res
, p
);
1972 case SOUND_PCM_READ_RATE
:
1973 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
1976 return put_user(res
, p
);
1977 case SNDCTL_DSP_STEREO
:
1978 if (get_user(res
, p
))
1980 res
= res
> 0 ? 2 : 1;
1981 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
1983 return put_user(--res
, p
);
1984 case SNDCTL_DSP_GETBLKSIZE
:
1985 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
1988 return put_user(res
, p
);
1989 case SNDCTL_DSP_SETFMT
:
1990 if (get_user(res
, p
))
1992 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
1995 return put_user(res
, p
);
1996 case SOUND_PCM_READ_BITS
:
1997 res
= snd_pcm_oss_get_format(pcm_oss_file
);
2000 return put_user(res
, p
);
2001 case SNDCTL_DSP_CHANNELS
:
2002 if (get_user(res
, p
))
2004 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
2007 return put_user(res
, p
);
2008 case SOUND_PCM_READ_CHANNELS
:
2009 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
2012 return put_user(res
, p
);
2013 case SOUND_PCM_WRITE_FILTER
:
2014 case SOUND_PCM_READ_FILTER
:
2016 case SNDCTL_DSP_POST
:
2017 return snd_pcm_oss_post(pcm_oss_file
);
2018 case SNDCTL_DSP_SUBDIVIDE
:
2019 if (get_user(res
, p
))
2021 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
2024 return put_user(res
, p
);
2025 case SNDCTL_DSP_SETFRAGMENT
:
2026 if (get_user(res
, p
))
2028 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
2029 case SNDCTL_DSP_GETFMTS
:
2030 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
2033 return put_user(res
, p
);
2034 case SNDCTL_DSP_GETOSPACE
:
2035 case SNDCTL_DSP_GETISPACE
:
2036 return snd_pcm_oss_get_space(pcm_oss_file
,
2037 cmd
== SNDCTL_DSP_GETISPACE
?
2038 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2039 (struct audio_buf_info __user
*) arg
);
2040 case SNDCTL_DSP_NONBLOCK
:
2041 return snd_pcm_oss_nonblock(file
);
2042 case SNDCTL_DSP_GETCAPS
:
2043 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
2046 return put_user(res
, p
);
2047 case SNDCTL_DSP_GETTRIGGER
:
2048 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2051 return put_user(res
, p
);
2052 case SNDCTL_DSP_SETTRIGGER
:
2053 if (get_user(res
, p
))
2055 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2056 case SNDCTL_DSP_GETIPTR
:
2057 case SNDCTL_DSP_GETOPTR
:
2058 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2059 cmd
== SNDCTL_DSP_GETIPTR
?
2060 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2061 (struct count_info __user
*) arg
);
2062 case SNDCTL_DSP_MAPINBUF
:
2063 case SNDCTL_DSP_MAPOUTBUF
:
2064 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2065 cmd
== SNDCTL_DSP_MAPINBUF
?
2066 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2067 (struct buffmem_desc __user
*) arg
);
2068 case SNDCTL_DSP_SETSYNCRO
:
2069 /* stop DMA now.. */
2071 case SNDCTL_DSP_SETDUPLEX
:
2072 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2075 case SNDCTL_DSP_GETODELAY
:
2076 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2078 /* it's for sure, some broken apps don't check for error codes */
2082 return put_user(res
, p
);
2083 case SNDCTL_DSP_PROFILE
:
2084 return 0; /* silently ignore */
2086 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd
);
2091 #ifdef CONFIG_COMPAT
2092 /* all compatible */
2093 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2095 #define snd_pcm_oss_ioctl_compat NULL
2098 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2100 snd_pcm_oss_file_t
*pcm_oss_file
;
2101 snd_pcm_substream_t
*substream
;
2103 pcm_oss_file
= file
->private_data
;
2104 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2105 if (substream
== NULL
)
2108 return snd_pcm_oss_read1(substream
, buf
, count
);
2111 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2112 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count
, (long)res
);
2118 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2120 snd_pcm_oss_file_t
*pcm_oss_file
;
2121 snd_pcm_substream_t
*substream
;
2124 pcm_oss_file
= file
->private_data
;
2125 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2126 if (substream
== NULL
)
2128 up(&file
->f_dentry
->d_inode
->i_sem
);
2129 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2130 down(&file
->f_dentry
->d_inode
->i_sem
);
2132 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count
, (long)result
);
2137 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t
*substream
)
2139 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2140 if (atomic_read(&runtime
->mmap_count
))
2141 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2143 return snd_pcm_playback_avail(runtime
) >= runtime
->oss
.period_frames
;
2146 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t
*substream
)
2148 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2149 if (atomic_read(&runtime
->mmap_count
))
2150 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2152 return snd_pcm_capture_avail(runtime
) >= runtime
->oss
.period_frames
;
2155 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2157 snd_pcm_oss_file_t
*pcm_oss_file
;
2159 snd_pcm_substream_t
*psubstream
= NULL
, *csubstream
= NULL
;
2161 pcm_oss_file
= file
->private_data
;
2163 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2164 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2167 if (psubstream
!= NULL
) {
2168 snd_pcm_runtime_t
*runtime
= psubstream
->runtime
;
2169 poll_wait(file
, &runtime
->sleep
, wait
);
2170 snd_pcm_stream_lock_irq(psubstream
);
2171 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2172 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2173 snd_pcm_oss_playback_ready(psubstream
)))
2174 mask
|= POLLOUT
| POLLWRNORM
;
2175 snd_pcm_stream_unlock_irq(psubstream
);
2177 if (csubstream
!= NULL
) {
2178 snd_pcm_runtime_t
*runtime
= csubstream
->runtime
;
2179 enum sndrv_pcm_state ostate
;
2180 poll_wait(file
, &runtime
->sleep
, wait
);
2181 snd_pcm_stream_lock_irq(csubstream
);
2182 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2183 snd_pcm_oss_capture_ready(csubstream
))
2184 mask
|= POLLIN
| POLLRDNORM
;
2185 snd_pcm_stream_unlock_irq(csubstream
);
2186 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2187 snd_pcm_oss_file_t ofile
;
2188 memset(&ofile
, 0, sizeof(ofile
));
2189 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2190 runtime
->oss
.trigger
= 0;
2191 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2198 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2200 snd_pcm_oss_file_t
*pcm_oss_file
;
2201 snd_pcm_substream_t
*substream
= NULL
;
2202 snd_pcm_runtime_t
*runtime
;
2206 printk("pcm_oss: mmap begin\n");
2208 pcm_oss_file
= file
->private_data
;
2209 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2210 case VM_READ
| VM_WRITE
:
2211 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2216 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2219 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2224 /* set VM_READ access as well to fix memset() routines that do
2225 reads before writes (to improve performance) */
2226 area
->vm_flags
|= VM_READ
;
2227 if (substream
== NULL
)
2229 runtime
= substream
->runtime
;
2230 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2232 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2233 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2237 if (runtime
->oss
.params
) {
2238 if ((err
= snd_pcm_oss_change_params(substream
)) < 0)
2241 if (runtime
->oss
.plugin_first
!= NULL
)
2244 if (area
->vm_pgoff
!= 0)
2247 err
= snd_pcm_mmap_data(substream
, file
, area
);
2250 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2251 runtime
->silence_threshold
= 0;
2252 runtime
->silence_size
= 0;
2254 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime
->oss
.mmap_bytes
);
2256 /* In mmap mode we never stop */
2257 runtime
->stop_threshold
= runtime
->boundary
;
2266 static void snd_pcm_oss_proc_read(snd_info_entry_t
*entry
,
2267 snd_info_buffer_t
* buffer
)
2269 snd_pcm_str_t
*pstr
= (snd_pcm_str_t
*)entry
->private_data
;
2270 snd_pcm_oss_setup_t
*setup
= pstr
->oss
.setup_list
;
2271 down(&pstr
->oss
.setup_mutex
);
2273 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2277 setup
->disable
? " disable" : "",
2278 setup
->direct
? " direct" : "",
2279 setup
->block
? " block" : "",
2280 setup
->nonblock
? " non-block" : "",
2281 setup
->partialfrag
? " partial-frag" : "",
2282 setup
->nosilence
? " no-silence" : "");
2283 setup
= setup
->next
;
2285 up(&pstr
->oss
.setup_mutex
);
2288 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t
* pstr
)
2291 snd_pcm_substream_t
*substream
;
2292 snd_pcm_oss_setup_t
*setup
, *setupn
;
2294 for (idx
= 0, substream
= pstr
->substream
;
2295 idx
< pstr
->substream_count
; idx
++, substream
= substream
->next
)
2296 substream
->oss
.setup
= NULL
;
2297 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
2298 setup
; setup
= setupn
) {
2299 setupn
= setup
->next
;
2300 kfree(setup
->task_name
);
2303 pstr
->oss
.setup_list
= NULL
;
2306 static void snd_pcm_oss_proc_write(snd_info_entry_t
*entry
,
2307 snd_info_buffer_t
* buffer
)
2309 snd_pcm_str_t
*pstr
= (snd_pcm_str_t
*)entry
->private_data
;
2310 char line
[128], str
[32], task_name
[32], *ptr
;
2312 snd_pcm_oss_setup_t
*setup
, *setup1
, template;
2314 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
2315 down(&pstr
->oss
.setup_mutex
);
2316 memset(&template, 0, sizeof(template));
2317 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
2318 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
2319 snd_pcm_oss_proc_free_setup_list(pstr
);
2320 up(&pstr
->oss
.setup_mutex
);
2323 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
2324 if (!strcmp(setup
->task_name
, task_name
)) {
2329 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2330 template.periods
= simple_strtoul(str
, NULL
, 10);
2331 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2332 template.period_size
= simple_strtoul(str
, NULL
, 10);
2333 for (idx1
= 31; idx1
>= 0; idx1
--)
2334 if (template.period_size
& (1 << idx1
))
2336 for (idx1
--; idx1
>= 0; idx1
--)
2337 template.period_size
&= ~(1 << idx1
);
2339 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2340 if (!strcmp(str
, "disable")) {
2341 template.disable
= 1;
2342 } else if (!strcmp(str
, "direct")) {
2343 template.direct
= 1;
2344 } else if (!strcmp(str
, "block")) {
2346 } else if (!strcmp(str
, "non-block")) {
2347 template.nonblock
= 1;
2348 } else if (!strcmp(str
, "partial-frag")) {
2349 template.partialfrag
= 1;
2350 } else if (!strcmp(str
, "no-silence")) {
2351 template.nosilence
= 1;
2354 if (setup
== NULL
) {
2355 setup
= (snd_pcm_oss_setup_t
*) kmalloc(sizeof(snd_pcm_oss_setup_t
), GFP_KERNEL
);
2357 if (pstr
->oss
.setup_list
== NULL
) {
2358 pstr
->oss
.setup_list
= setup
;
2360 for (setup1
= pstr
->oss
.setup_list
; setup1
->next
; setup1
= setup1
->next
);
2361 setup1
->next
= setup
;
2363 template.task_name
= snd_kmalloc_strdup(task_name
, GFP_KERNEL
);
2365 buffer
->error
= -ENOMEM
;
2370 up(&pstr
->oss
.setup_mutex
);
2374 static void snd_pcm_oss_proc_init(snd_pcm_t
*pcm
)
2377 for (stream
= 0; stream
< 2; ++stream
) {
2378 snd_info_entry_t
*entry
;
2379 snd_pcm_str_t
*pstr
= &pcm
->streams
[stream
];
2380 if (pstr
->substream_count
== 0)
2382 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
2383 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
2384 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2385 entry
->c
.text
.read_size
= 8192;
2386 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
2387 entry
->c
.text
.write_size
= 8192;
2388 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
2389 entry
->private_data
= pstr
;
2390 if (snd_info_register(entry
) < 0) {
2391 snd_info_free_entry(entry
);
2395 pstr
->oss
.proc_entry
= entry
;
2399 static void snd_pcm_oss_proc_done(snd_pcm_t
*pcm
)
2402 for (stream
= 0; stream
< 2; ++stream
) {
2403 snd_pcm_str_t
*pstr
= &pcm
->streams
[stream
];
2404 if (pstr
->oss
.proc_entry
) {
2405 snd_info_unregister(pstr
->oss
.proc_entry
);
2406 pstr
->oss
.proc_entry
= NULL
;
2407 snd_pcm_oss_proc_free_setup_list(pstr
);
2416 static struct file_operations snd_pcm_oss_f_reg
=
2418 .owner
= THIS_MODULE
,
2419 .read
= snd_pcm_oss_read
,
2420 .write
= snd_pcm_oss_write
,
2421 .open
= snd_pcm_oss_open
,
2422 .release
= snd_pcm_oss_release
,
2423 .poll
= snd_pcm_oss_poll
,
2424 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
2425 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
2426 .mmap
= snd_pcm_oss_mmap
,
2429 static snd_minor_t snd_pcm_oss_reg
=
2431 .comment
= "digital audio",
2432 .f_ops
= &snd_pcm_oss_f_reg
,
2435 static void register_oss_dsp(snd_pcm_t
*pcm
, int index
)
2438 sprintf(name
, "dsp%i%i", pcm
->card
->number
, pcm
->device
);
2439 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2440 pcm
->card
, index
, &snd_pcm_oss_reg
,
2442 snd_printk("unable to register OSS PCM device %i:%i\n", pcm
->card
->number
, pcm
->device
);
2446 static int snd_pcm_oss_register_minor(snd_pcm_t
* pcm
)
2449 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2452 register_oss_dsp(pcm
, 0);
2453 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
2454 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
2455 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
2456 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
2457 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2458 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
2463 pcm
->oss
.reg_mask
|= 1;
2465 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2466 register_oss_dsp(pcm
, 1);
2468 pcm
->oss
.reg_mask
|= 2;
2472 snd_pcm_oss_proc_init(pcm
);
2477 static int snd_pcm_oss_disconnect_minor(snd_pcm_t
* pcm
)
2480 if (pcm
->oss
.reg_mask
& 1) {
2481 pcm
->oss
.reg_mask
&= ~1;
2482 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2485 if (pcm
->oss
.reg_mask
& 2) {
2486 pcm
->oss
.reg_mask
&= ~2;
2487 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2494 static int snd_pcm_oss_unregister_minor(snd_pcm_t
* pcm
)
2496 snd_pcm_oss_disconnect_minor(pcm
);
2498 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2499 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2500 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
2504 snd_pcm_oss_proc_done(pcm
);
2509 static snd_pcm_notify_t snd_pcm_oss_notify
=
2511 .n_register
= snd_pcm_oss_register_minor
,
2512 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
2513 .n_unregister
= snd_pcm_oss_unregister_minor
,
2516 static int __init
alsa_pcm_oss_init(void)
2521 /* check device map table */
2522 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
2523 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2524 snd_printk("invalid dsp_map[%d] = %d\n", i
, dsp_map
[i
]);
2527 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2528 snd_printk("invalid adsp_map[%d] = %d\n", i
, adsp_map
[i
]);
2532 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
2537 static void __exit
alsa_pcm_oss_exit(void)
2539 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
2542 module_init(alsa_pcm_oss_init
)
2543 module_exit(alsa_pcm_oss_exit
)