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
.period_size
> 16)
212 oss_period_size
= substream
->oss
.setup
.period_size
;
213 else if (runtime
->oss
.fragshift
) {
214 oss_period_size
= 1 << runtime
->oss
.fragshift
;
215 if (oss_period_size
> oss_buffer_size
/ 2)
216 oss_period_size
= oss_buffer_size
/ 2;
219 size_t bytes_per_sec
= params_rate(oss_params
) * snd_pcm_format_physical_width(params_format(oss_params
)) * params_channels(oss_params
) / 8;
221 oss_period_size
= oss_buffer_size
;
223 oss_period_size
/= 2;
224 } while (oss_period_size
> bytes_per_sec
);
225 if (runtime
->oss
.subdivision
== 0) {
227 if (oss_period_size
/ sd
> 4096)
229 if (oss_period_size
/ sd
< 4096)
232 sd
= runtime
->oss
.subdivision
;
233 oss_period_size
/= sd
;
234 if (oss_period_size
< 16)
235 oss_period_size
= 16;
238 min_period_size
= snd_pcm_plug_client_size(substream
,
239 snd_pcm_hw_param_value_min(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
240 min_period_size
*= oss_frame_size
;
241 min_period_size
= 1 << (ld2(min_period_size
- 1) + 1);
242 if (oss_period_size
< min_period_size
)
243 oss_period_size
= min_period_size
;
245 max_period_size
= snd_pcm_plug_client_size(substream
,
246 snd_pcm_hw_param_value_max(slave_params
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, NULL
));
247 max_period_size
*= oss_frame_size
;
248 max_period_size
= 1 << ld2(max_period_size
);
249 if (oss_period_size
> max_period_size
)
250 oss_period_size
= max_period_size
;
252 oss_periods
= oss_buffer_size
/ oss_period_size
;
254 if (substream
->oss
.setup
.periods
> 1)
255 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(struct snd_pcm_substream
*substream
,
280 struct snd_pcm_hw_params
*params
, unsigned int best_rate
)
282 struct snd_interval
*it
;
283 struct snd_pcm_hw_params
*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(struct snd_pcm_substream
*substream
)
321 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
322 struct snd_pcm_hw_params
*params
, *sparams
;
323 struct snd_pcm_sw_params
*sw_params
;
324 ssize_t oss_buffer_size
, oss_period_size
;
325 size_t oss_frame_size
;
328 int format
, sformat
, n
;
329 struct snd_mask sformat_mask
;
330 struct snd_mask 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 direct
= substream
->oss
.setup
.direct
;
346 _snd_pcm_hw_params_any(sparams
);
347 _snd_pcm_hw_param_setinteger(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
);
348 _snd_pcm_hw_param_min(sparams
, SNDRV_PCM_HW_PARAM_PERIODS
, 2, 0);
349 snd_mask_none(&mask
);
350 if (atomic_read(&runtime
->mmap_count
))
351 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
);
353 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
355 snd_mask_set(&mask
, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
357 err
= snd_pcm_hw_param_mask(substream
, sparams
, SNDRV_PCM_HW_PARAM_ACCESS
, &mask
);
359 snd_printd("No usable accesses\n");
363 choose_rate(substream
, sparams
, runtime
->oss
.rate
);
364 snd_pcm_hw_param_near(substream
, sparams
, SNDRV_PCM_HW_PARAM_CHANNELS
, runtime
->oss
.channels
, NULL
);
366 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
368 sformat_mask
= *hw_param_mask(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
);
372 sformat
= snd_pcm_plug_slave_format(format
, &sformat_mask
);
374 if (sformat
< 0 || !snd_mask_test(&sformat_mask
, sformat
)) {
375 for (sformat
= 0; sformat
<= SNDRV_PCM_FORMAT_LAST
; sformat
++) {
376 if (snd_mask_test(&sformat_mask
, sformat
) &&
377 snd_pcm_oss_format_to(sformat
) >= 0)
380 if (sformat
> SNDRV_PCM_FORMAT_LAST
) {
381 snd_printd("Cannot find a format!!!\n");
386 err
= _snd_pcm_hw_param_set(sparams
, SNDRV_PCM_HW_PARAM_FORMAT
, sformat
, 0);
387 snd_assert(err
>= 0, goto failure
);
390 memcpy(params
, sparams
, sizeof(*params
));
392 _snd_pcm_hw_params_any(params
);
393 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_ACCESS
,
394 SNDRV_PCM_ACCESS_RW_INTERLEAVED
, 0);
395 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_FORMAT
,
396 snd_pcm_oss_format_from(runtime
->oss
.format
), 0);
397 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_CHANNELS
,
398 runtime
->oss
.channels
, 0);
399 _snd_pcm_hw_param_set(params
, SNDRV_PCM_HW_PARAM_RATE
,
400 runtime
->oss
.rate
, 0);
401 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
402 params_access(params
), params_format(params
),
403 params_channels(params
), params_rate(params
));
405 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
406 params_access(sparams
), params_format(sparams
),
407 params_channels(sparams
), params_rate(sparams
));
409 oss_frame_size
= snd_pcm_format_physical_width(params_format(params
)) *
410 params_channels(params
) / 8;
412 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
413 snd_pcm_oss_plugin_clear(substream
);
415 /* add necessary plugins */
416 snd_pcm_oss_plugin_clear(substream
);
417 if ((err
= snd_pcm_plug_format_plugins(substream
,
420 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err
);
421 snd_pcm_oss_plugin_clear(substream
);
424 if (runtime
->oss
.plugin_first
) {
425 struct snd_pcm_plugin
*plugin
;
426 if ((err
= snd_pcm_plugin_build_io(substream
, sparams
, &plugin
)) < 0) {
427 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err
);
428 snd_pcm_oss_plugin_clear(substream
);
431 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
432 err
= snd_pcm_plugin_append(plugin
);
434 err
= snd_pcm_plugin_insert(plugin
);
437 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
.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 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
501 if (runtime
->oss
.plugin_first
) {
502 err
= snd_pcm_plug_alloc(substream
, oss_period_size
);
507 oss_period_size
*= oss_frame_size
;
509 oss_buffer_size
= oss_period_size
* runtime
->oss
.periods
;
510 snd_assert(oss_buffer_size
>= 0, err
= -EINVAL
; goto failure
);
512 runtime
->oss
.period_bytes
= oss_period_size
;
513 runtime
->oss
.buffer_bytes
= oss_buffer_size
;
515 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
516 runtime
->oss
.period_bytes
,
517 runtime
->oss
.buffer_bytes
);
518 pdprintf("slave: period_size = %i, buffer_size = %i\n",
519 params_period_size(sparams
),
520 params_buffer_size(sparams
));
522 runtime
->oss
.format
= snd_pcm_oss_format_to(params_format(params
));
523 runtime
->oss
.channels
= params_channels(params
);
524 runtime
->oss
.rate
= params_rate(params
);
526 runtime
->oss
.params
= 0;
527 runtime
->oss
.prepare
= 1;
528 vfree(runtime
->oss
.buffer
);
529 runtime
->oss
.buffer
= vmalloc(runtime
->oss
.period_bytes
);
530 runtime
->oss
.buffer_used
= 0;
531 if (runtime
->dma_area
)
532 snd_pcm_format_set_silence(runtime
->format
, runtime
->dma_area
, bytes_to_samples(runtime
, runtime
->dma_bytes
));
534 runtime
->oss
.period_frames
= snd_pcm_alsa_frames(substream
, oss_period_size
);
544 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file
*pcm_oss_file
, struct snd_pcm_substream
**r_substream
)
547 struct snd_pcm_substream
*asubstream
= NULL
, *substream
;
549 for (idx
= 0; idx
< 2; idx
++) {
550 substream
= pcm_oss_file
->streams
[idx
];
551 if (substream
== NULL
)
553 if (asubstream
== NULL
)
554 asubstream
= substream
;
555 if (substream
->runtime
->oss
.params
) {
556 err
= snd_pcm_oss_change_params(substream
);
561 snd_assert(asubstream
!= NULL
, return -EIO
);
563 *r_substream
= asubstream
;
567 static int snd_pcm_oss_prepare(struct snd_pcm_substream
*substream
)
570 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
572 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_PREPARE
, NULL
);
574 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
577 runtime
->oss
.prepare
= 0;
578 runtime
->oss
.prev_hw_ptr_interrupt
= 0;
579 runtime
->oss
.period_ptr
= 0;
580 runtime
->oss
.buffer_used
= 0;
585 static int snd_pcm_oss_make_ready(struct snd_pcm_substream
*substream
)
587 struct snd_pcm_runtime
*runtime
;
590 if (substream
== NULL
)
592 runtime
= substream
->runtime
;
593 if (runtime
->oss
.params
) {
594 err
= snd_pcm_oss_change_params(substream
);
598 if (runtime
->oss
.prepare
) {
599 err
= snd_pcm_oss_prepare(substream
);
606 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream
*substream
, snd_pcm_sframes_t
*delay
)
608 struct snd_pcm_runtime
*runtime
;
609 snd_pcm_uframes_t frames
;
613 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, delay
);
616 runtime
= substream
->runtime
;
617 if (*delay
<= (snd_pcm_sframes_t
)runtime
->buffer_size
)
619 /* in case of overrun, skip whole periods like OSS/Linux driver does */
620 /* until avail(delay) <= buffer_size */
621 frames
= (*delay
- runtime
->buffer_size
) + runtime
->period_size
- 1;
622 frames
/= runtime
->period_size
;
623 frames
*= runtime
->period_size
;
624 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_FORWARD
, &frames
);
631 snd_pcm_sframes_t
snd_pcm_oss_write3(struct snd_pcm_substream
*substream
, const char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
633 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
636 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
637 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
639 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
640 printk("pcm_oss: write: recovering from XRUN\n");
642 printk("pcm_oss: write: recovering from SUSPEND\n");
644 ret
= snd_pcm_oss_prepare(substream
);
650 fs
= snd_enter_user();
651 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
654 ret
= snd_pcm_lib_write(substream
, (void __user
*)ptr
, frames
);
656 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
658 /* test, if we can't store new data, because the stream */
659 /* has not been started */
660 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
666 snd_pcm_sframes_t
snd_pcm_oss_read3(struct snd_pcm_substream
*substream
, char *ptr
, snd_pcm_uframes_t frames
, int in_kernel
)
668 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
669 snd_pcm_sframes_t delay
;
672 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
673 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
675 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
676 printk("pcm_oss: read: recovering from XRUN\n");
678 printk("pcm_oss: read: recovering from SUSPEND\n");
680 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
683 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
684 ret
= snd_pcm_oss_prepare(substream
);
688 ret
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
693 fs
= snd_enter_user();
694 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
697 ret
= snd_pcm_lib_read(substream
, (void __user
*)ptr
, frames
);
700 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
701 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
707 if (ret
!= -ESTRPIPE
)
713 snd_pcm_sframes_t
snd_pcm_oss_writev3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
715 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
718 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
719 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
721 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
722 printk("pcm_oss: writev: recovering from XRUN\n");
724 printk("pcm_oss: writev: recovering from SUSPEND\n");
726 ret
= snd_pcm_oss_prepare(substream
);
732 fs
= snd_enter_user();
733 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
736 ret
= snd_pcm_lib_writev(substream
, (void __user
**)bufs
, frames
);
738 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
741 /* test, if we can't store new data, because the stream */
742 /* has not been started */
743 if (runtime
->status
->state
== SNDRV_PCM_STATE_PREPARED
)
749 snd_pcm_sframes_t
snd_pcm_oss_readv3(struct snd_pcm_substream
*substream
, void **bufs
, snd_pcm_uframes_t frames
, int in_kernel
)
751 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
754 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
||
755 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
757 if (runtime
->status
->state
== SNDRV_PCM_STATE_XRUN
)
758 printk("pcm_oss: readv: recovering from XRUN\n");
760 printk("pcm_oss: readv: recovering from SUSPEND\n");
762 ret
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
765 } else if (runtime
->status
->state
== SNDRV_PCM_STATE_SETUP
) {
766 ret
= snd_pcm_oss_prepare(substream
);
772 fs
= snd_enter_user();
773 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
776 ret
= snd_pcm_lib_readv(substream
, (void __user
**)bufs
, frames
);
778 if (ret
!= -EPIPE
&& ret
!= -ESTRPIPE
)
784 static ssize_t
snd_pcm_oss_write2(struct snd_pcm_substream
*substream
, const char *buf
, size_t bytes
, int in_kernel
)
786 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
787 snd_pcm_sframes_t frames
, frames1
;
788 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
789 if (runtime
->oss
.plugin_first
) {
790 struct snd_pcm_plugin_channel
*channels
;
791 size_t oss_frame_bytes
= (runtime
->oss
.plugin_first
->src_width
* runtime
->oss
.plugin_first
->src_format
.channels
) / 8;
793 if (copy_from_user(runtime
->oss
.buffer
, (const char __user
*)buf
, bytes
))
795 buf
= runtime
->oss
.buffer
;
797 frames
= bytes
/ oss_frame_bytes
;
798 frames1
= snd_pcm_plug_client_channels_buf(substream
, (char *)buf
, frames
, &channels
);
801 frames1
= snd_pcm_plug_write_transfer(substream
, channels
, frames1
);
804 bytes
= frames1
* oss_frame_bytes
;
808 frames
= bytes_to_frames(runtime
, bytes
);
809 frames1
= snd_pcm_oss_write3(substream
, buf
, frames
, in_kernel
);
812 bytes
= frames_to_bytes(runtime
, frames1
);
817 static ssize_t
snd_pcm_oss_write1(struct snd_pcm_substream
*substream
, const char __user
*buf
, size_t bytes
)
821 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
823 if (atomic_read(&runtime
->mmap_count
))
826 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
829 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
831 if (tmp
+ runtime
->oss
.buffer_used
> runtime
->oss
.period_bytes
)
832 tmp
= runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
;
834 if (copy_from_user(runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
, buf
, tmp
))
835 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
837 runtime
->oss
.buffer_used
+= tmp
;
841 if (substream
->oss
.setup
.partialfrag
||
842 runtime
->oss
.buffer_used
== runtime
->oss
.period_bytes
) {
843 tmp
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
+ runtime
->oss
.period_ptr
,
844 runtime
->oss
.buffer_used
- runtime
->oss
.period_ptr
, 1);
846 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
847 runtime
->oss
.bytes
+= tmp
;
848 runtime
->oss
.period_ptr
+= tmp
;
849 runtime
->oss
.period_ptr
%= runtime
->oss
.period_bytes
;
850 if (runtime
->oss
.period_ptr
== 0 ||
851 runtime
->oss
.period_ptr
== runtime
->oss
.buffer_used
)
852 runtime
->oss
.buffer_used
= 0;
853 else if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0)
854 return xfer
> 0 ? xfer
: -EAGAIN
;
857 tmp
= snd_pcm_oss_write2(substream
,
858 (const char __force
*)buf
,
859 runtime
->oss
.period_bytes
, 0);
861 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
862 runtime
->oss
.bytes
+= tmp
;
866 if ((substream
->ffile
->f_flags
& O_NONBLOCK
) != 0 &&
867 tmp
!= runtime
->oss
.period_bytes
)
874 static ssize_t
snd_pcm_oss_read2(struct snd_pcm_substream
*substream
, char *buf
, size_t bytes
, int in_kernel
)
876 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
877 snd_pcm_sframes_t frames
, frames1
;
878 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
879 char __user
*final_dst
= (char __user
*)buf
;
880 if (runtime
->oss
.plugin_first
) {
881 struct snd_pcm_plugin_channel
*channels
;
882 size_t oss_frame_bytes
= (runtime
->oss
.plugin_last
->dst_width
* runtime
->oss
.plugin_last
->dst_format
.channels
) / 8;
884 buf
= runtime
->oss
.buffer
;
885 frames
= bytes
/ oss_frame_bytes
;
886 frames1
= snd_pcm_plug_client_channels_buf(substream
, buf
, frames
, &channels
);
889 frames1
= snd_pcm_plug_read_transfer(substream
, channels
, frames1
);
892 bytes
= frames1
* oss_frame_bytes
;
893 if (!in_kernel
&& copy_to_user(final_dst
, buf
, bytes
))
898 frames
= bytes_to_frames(runtime
, bytes
);
899 frames1
= snd_pcm_oss_read3(substream
, buf
, frames
, in_kernel
);
902 bytes
= frames_to_bytes(runtime
, frames1
);
907 static ssize_t
snd_pcm_oss_read1(struct snd_pcm_substream
*substream
, char __user
*buf
, size_t bytes
)
911 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
913 if (atomic_read(&runtime
->mmap_count
))
916 if ((tmp
= snd_pcm_oss_make_ready(substream
)) < 0)
919 if (bytes
< runtime
->oss
.period_bytes
|| runtime
->oss
.buffer_used
> 0) {
920 if (runtime
->oss
.buffer_used
== 0) {
921 tmp
= snd_pcm_oss_read2(substream
, runtime
->oss
.buffer
, runtime
->oss
.period_bytes
, 1);
923 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
924 runtime
->oss
.bytes
+= tmp
;
925 runtime
->oss
.period_ptr
= tmp
;
926 runtime
->oss
.buffer_used
= tmp
;
929 if ((size_t) tmp
> runtime
->oss
.buffer_used
)
930 tmp
= runtime
->oss
.buffer_used
;
931 if (copy_to_user(buf
, runtime
->oss
.buffer
+ (runtime
->oss
.period_ptr
- runtime
->oss
.buffer_used
), tmp
))
932 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: -EFAULT
;
936 runtime
->oss
.buffer_used
-= tmp
;
938 tmp
= snd_pcm_oss_read2(substream
, (char __force
*)buf
,
939 runtime
->oss
.period_bytes
, 0);
941 return xfer
> 0 ? (snd_pcm_sframes_t
)xfer
: tmp
;
942 runtime
->oss
.bytes
+= tmp
;
951 static int snd_pcm_oss_reset(struct snd_pcm_oss_file
*pcm_oss_file
)
953 struct snd_pcm_substream
*substream
;
955 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
956 if (substream
!= NULL
) {
957 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
958 substream
->runtime
->oss
.prepare
= 1;
960 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
961 if (substream
!= NULL
) {
962 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
963 substream
->runtime
->oss
.prepare
= 1;
968 static int snd_pcm_oss_post(struct snd_pcm_oss_file
*pcm_oss_file
)
970 struct snd_pcm_substream
*substream
;
973 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
974 if (substream
!= NULL
) {
975 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
977 snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_START
, NULL
);
979 /* note: all errors from the start action are ignored */
980 /* OSS apps do not know, how to handle them */
984 static int snd_pcm_oss_sync1(struct snd_pcm_substream
*substream
, size_t size
)
986 struct snd_pcm_runtime
*runtime
;
991 runtime
= substream
->runtime
;
992 init_waitqueue_entry(&wait
, current
);
993 add_wait_queue(&runtime
->sleep
, &wait
);
995 printk("sync1: size = %li\n", size
);
998 result
= snd_pcm_oss_write2(substream
, runtime
->oss
.buffer
, size
, 1);
1000 runtime
->oss
.buffer_used
= 0;
1004 if (result
!= 0 && result
!= -EAGAIN
)
1007 set_current_state(TASK_INTERRUPTIBLE
);
1008 snd_pcm_stream_lock_irq(substream
);
1009 res
= runtime
->status
->state
;
1010 snd_pcm_stream_unlock_irq(substream
);
1011 if (res
!= SNDRV_PCM_STATE_RUNNING
) {
1012 set_current_state(TASK_RUNNING
);
1015 res
= schedule_timeout(10 * HZ
);
1016 if (signal_pending(current
)) {
1017 result
= -ERESTARTSYS
;
1021 snd_printk(KERN_ERR
"OSS sync error - DMA timeout\n");
1026 remove_wait_queue(&runtime
->sleep
, &wait
);
1030 static int snd_pcm_oss_sync(struct snd_pcm_oss_file
*pcm_oss_file
)
1033 unsigned int saved_f_flags
;
1034 struct snd_pcm_substream
*substream
;
1035 struct snd_pcm_runtime
*runtime
;
1036 snd_pcm_format_t format
;
1037 unsigned long width
;
1040 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1041 if (substream
!= NULL
) {
1042 runtime
= substream
->runtime
;
1043 if (atomic_read(&runtime
->mmap_count
))
1045 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1047 format
= snd_pcm_oss_format_from(runtime
->oss
.format
);
1048 width
= snd_pcm_format_physical_width(format
);
1049 if (runtime
->oss
.buffer_used
> 0) {
1051 printk("sync: buffer_used\n");
1053 size
= (8 * (runtime
->oss
.period_bytes
- runtime
->oss
.buffer_used
) + 7) / width
;
1054 snd_pcm_format_set_silence(format
,
1055 runtime
->oss
.buffer
+ runtime
->oss
.buffer_used
,
1057 err
= snd_pcm_oss_sync1(substream
, runtime
->oss
.period_bytes
);
1060 } else if (runtime
->oss
.period_ptr
> 0) {
1062 printk("sync: period_ptr\n");
1064 size
= runtime
->oss
.period_bytes
- runtime
->oss
.period_ptr
;
1065 snd_pcm_format_set_silence(format
,
1066 runtime
->oss
.buffer
,
1068 err
= snd_pcm_oss_sync1(substream
, size
);
1073 * The ALSA's period might be a bit large than OSS one.
1074 * Fill the remain portion of ALSA period with zeros.
1076 size
= runtime
->control
->appl_ptr
% runtime
->period_size
;
1078 size
= runtime
->period_size
- size
;
1079 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
1080 size
= (runtime
->frame_bits
* size
) / 8;
1083 size_t size1
= size
< runtime
->oss
.period_bytes
? size
: runtime
->oss
.period_bytes
;
1086 size1
/= runtime
->sample_bits
;
1087 snd_pcm_format_set_silence(runtime
->format
,
1088 runtime
->oss
.buffer
,
1090 fs
= snd_enter_user();
1091 snd_pcm_lib_write(substream
, (void __user
*)runtime
->oss
.buffer
, size1
);
1094 } else if (runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
1095 void __user
*buffers
[runtime
->channels
];
1096 memset(buffers
, 0, runtime
->channels
* sizeof(void *));
1097 snd_pcm_lib_writev(substream
, buffers
, size
);
1101 * finish sync: drain the buffer
1104 saved_f_flags
= substream
->ffile
->f_flags
;
1105 substream
->ffile
->f_flags
&= ~O_NONBLOCK
;
1106 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DRAIN
, NULL
);
1107 substream
->ffile
->f_flags
= saved_f_flags
;
1110 runtime
->oss
.prepare
= 1;
1113 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1114 if (substream
!= NULL
) {
1115 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1117 runtime
= substream
->runtime
;
1118 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DROP
, NULL
);
1121 runtime
->oss
.buffer_used
= 0;
1122 runtime
->oss
.prepare
= 1;
1127 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file
*pcm_oss_file
, int rate
)
1131 for (idx
= 1; idx
>= 0; --idx
) {
1132 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1133 struct snd_pcm_runtime
*runtime
;
1134 if (substream
== NULL
)
1136 runtime
= substream
->runtime
;
1139 else if (rate
> 192000)
1141 if (runtime
->oss
.rate
!= rate
) {
1142 runtime
->oss
.params
= 1;
1143 runtime
->oss
.rate
= rate
;
1146 return snd_pcm_oss_get_rate(pcm_oss_file
);
1149 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file
*pcm_oss_file
)
1151 struct snd_pcm_substream
*substream
;
1154 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1156 return substream
->runtime
->oss
.rate
;
1159 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int channels
)
1166 for (idx
= 1; idx
>= 0; --idx
) {
1167 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1168 struct snd_pcm_runtime
*runtime
;
1169 if (substream
== NULL
)
1171 runtime
= substream
->runtime
;
1172 if (runtime
->oss
.channels
!= channels
) {
1173 runtime
->oss
.params
= 1;
1174 runtime
->oss
.channels
= channels
;
1177 return snd_pcm_oss_get_channels(pcm_oss_file
);
1180 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file
*pcm_oss_file
)
1182 struct snd_pcm_substream
*substream
;
1185 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1187 return substream
->runtime
->oss
.channels
;
1190 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file
*pcm_oss_file
)
1192 struct snd_pcm_substream
*substream
;
1195 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1197 return substream
->runtime
->oss
.period_bytes
;
1200 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file
*pcm_oss_file
)
1202 struct snd_pcm_substream
*substream
;
1205 struct snd_pcm_hw_params
*params
;
1206 unsigned int formats
= 0;
1207 struct snd_mask format_mask
;
1210 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1212 if (atomic_read(&substream
->runtime
->mmap_count
))
1215 direct
= substream
->oss
.setup
.direct
;
1217 return AFMT_MU_LAW
| AFMT_U8
|
1218 AFMT_S16_LE
| AFMT_S16_BE
|
1219 AFMT_S8
| AFMT_U16_LE
|
1221 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
1224 _snd_pcm_hw_params_any(params
);
1225 err
= snd_pcm_hw_refine(substream
, params
);
1226 format_mask
= *hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1228 snd_assert(err
>= 0, return err
);
1229 for (fmt
= 0; fmt
< 32; ++fmt
) {
1230 if (snd_mask_test(&format_mask
, fmt
)) {
1231 int f
= snd_pcm_oss_format_to(fmt
);
1239 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file
*pcm_oss_file
, int format
)
1243 if (format
!= AFMT_QUERY
) {
1244 formats
= snd_pcm_oss_get_formats(pcm_oss_file
);
1245 if (!(formats
& format
))
1247 for (idx
= 1; idx
>= 0; --idx
) {
1248 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1249 struct snd_pcm_runtime
*runtime
;
1250 if (substream
== NULL
)
1252 runtime
= substream
->runtime
;
1253 if (runtime
->oss
.format
!= format
) {
1254 runtime
->oss
.params
= 1;
1255 runtime
->oss
.format
= format
;
1259 return snd_pcm_oss_get_format(pcm_oss_file
);
1262 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file
*pcm_oss_file
)
1264 struct snd_pcm_substream
*substream
;
1267 if ((err
= snd_pcm_oss_get_active_substream(pcm_oss_file
, &substream
)) < 0)
1269 return substream
->runtime
->oss
.format
;
1272 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream
*substream
, int subdivide
)
1274 struct snd_pcm_runtime
*runtime
;
1276 if (substream
== NULL
)
1278 runtime
= substream
->runtime
;
1279 if (subdivide
== 0) {
1280 subdivide
= runtime
->oss
.subdivision
;
1285 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1287 if (subdivide
!= 1 && subdivide
!= 2 && subdivide
!= 4 &&
1288 subdivide
!= 8 && subdivide
!= 16)
1290 runtime
->oss
.subdivision
= subdivide
;
1291 runtime
->oss
.params
= 1;
1295 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file
*pcm_oss_file
, int subdivide
)
1297 int err
= -EINVAL
, idx
;
1299 for (idx
= 1; idx
>= 0; --idx
) {
1300 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1301 if (substream
== NULL
)
1303 if ((err
= snd_pcm_oss_set_subdivide1(substream
, subdivide
)) < 0)
1309 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream
*substream
, unsigned int val
)
1311 struct snd_pcm_runtime
*runtime
;
1313 if (substream
== NULL
)
1315 runtime
= substream
->runtime
;
1316 if (runtime
->oss
.subdivision
|| runtime
->oss
.fragshift
)
1318 runtime
->oss
.fragshift
= val
& 0xffff;
1319 runtime
->oss
.maxfrags
= (val
>> 16) & 0xffff;
1320 if (runtime
->oss
.fragshift
< 4) /* < 16 */
1321 runtime
->oss
.fragshift
= 4;
1322 if (runtime
->oss
.maxfrags
< 2)
1323 runtime
->oss
.maxfrags
= 2;
1324 runtime
->oss
.params
= 1;
1328 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file
*pcm_oss_file
, unsigned int val
)
1330 int err
= -EINVAL
, idx
;
1332 for (idx
= 1; idx
>= 0; --idx
) {
1333 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1334 if (substream
== NULL
)
1336 if ((err
= snd_pcm_oss_set_fragment1(substream
, val
)) < 0)
1342 static int snd_pcm_oss_nonblock(struct file
* file
)
1344 file
->f_flags
|= O_NONBLOCK
;
1348 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream
*substream
, int res
)
1351 if (substream
== NULL
) {
1352 res
&= ~DSP_CAP_DUPLEX
;
1355 #ifdef DSP_CAP_MULTI
1356 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1357 if (substream
->pstr
->substream_count
> 1)
1358 res
|= DSP_CAP_MULTI
;
1360 /* DSP_CAP_REALTIME is set all times: */
1361 /* all ALSA drivers can return actual pointer in ring buffer */
1362 #if defined(DSP_CAP_REALTIME) && 0
1364 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1365 if (runtime
->info
& (SNDRV_PCM_INFO_BLOCK_TRANSFER
|SNDRV_PCM_INFO_BATCH
))
1366 res
&= ~DSP_CAP_REALTIME
;
1372 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file
*pcm_oss_file
)
1376 result
= DSP_CAP_TRIGGER
| DSP_CAP_MMAP
| DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
;
1377 for (idx
= 0; idx
< 2; idx
++) {
1378 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[idx
];
1379 result
= snd_pcm_oss_get_caps1(substream
, result
);
1381 result
|= 0x0001; /* revision - same as SB AWE 64 */
1385 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream
*substream
, snd_pcm_uframes_t hw_ptr
)
1387 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1388 snd_pcm_uframes_t appl_ptr
;
1389 appl_ptr
= hw_ptr
+ runtime
->buffer_size
;
1390 appl_ptr
%= runtime
->boundary
;
1391 runtime
->control
->appl_ptr
= appl_ptr
;
1394 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file
*pcm_oss_file
, int trigger
)
1396 struct snd_pcm_runtime
*runtime
;
1397 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1401 printk("pcm_oss: trigger = 0x%x\n", trigger
);
1404 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1405 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1408 if ((err
= snd_pcm_oss_make_ready(psubstream
)) < 0)
1412 if ((err
= snd_pcm_oss_make_ready(csubstream
)) < 0)
1416 runtime
= psubstream
->runtime
;
1417 if (trigger
& PCM_ENABLE_OUTPUT
) {
1418 if (runtime
->oss
.trigger
)
1420 if (atomic_read(&psubstream
->runtime
->mmap_count
))
1421 snd_pcm_oss_simulate_fill(psubstream
, runtime
->hw_ptr_interrupt
);
1422 runtime
->oss
.trigger
= 1;
1423 runtime
->start_threshold
= 1;
1424 cmd
= SNDRV_PCM_IOCTL_START
;
1426 if (!runtime
->oss
.trigger
)
1428 runtime
->oss
.trigger
= 0;
1429 runtime
->start_threshold
= runtime
->boundary
;
1430 cmd
= SNDRV_PCM_IOCTL_DROP
;
1431 runtime
->oss
.prepare
= 1;
1433 err
= snd_pcm_kernel_ioctl(psubstream
, cmd
, NULL
);
1439 runtime
= csubstream
->runtime
;
1440 if (trigger
& PCM_ENABLE_INPUT
) {
1441 if (runtime
->oss
.trigger
)
1443 runtime
->oss
.trigger
= 1;
1444 runtime
->start_threshold
= 1;
1445 cmd
= SNDRV_PCM_IOCTL_START
;
1447 if (!runtime
->oss
.trigger
)
1449 runtime
->oss
.trigger
= 0;
1450 runtime
->start_threshold
= runtime
->boundary
;
1451 cmd
= SNDRV_PCM_IOCTL_DROP
;
1452 runtime
->oss
.prepare
= 1;
1454 err
= snd_pcm_kernel_ioctl(csubstream
, cmd
, NULL
);
1462 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file
*pcm_oss_file
)
1464 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
1467 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1468 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1469 if (psubstream
&& psubstream
->runtime
&& psubstream
->runtime
->oss
.trigger
)
1470 result
|= PCM_ENABLE_OUTPUT
;
1471 if (csubstream
&& csubstream
->runtime
&& csubstream
->runtime
->oss
.trigger
)
1472 result
|= PCM_ENABLE_INPUT
;
1476 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file
*pcm_oss_file
)
1478 struct snd_pcm_substream
*substream
;
1479 struct snd_pcm_runtime
*runtime
;
1480 snd_pcm_sframes_t delay
;
1483 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1484 if (substream
== NULL
)
1486 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1488 runtime
= substream
->runtime
;
1489 if (runtime
->oss
.params
|| runtime
->oss
.prepare
)
1491 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1493 delay
= 0; /* hack for broken OSS applications */
1496 return snd_pcm_oss_bytes(substream
, delay
);
1499 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct count_info __user
* _info
)
1501 struct snd_pcm_substream
*substream
;
1502 struct snd_pcm_runtime
*runtime
;
1503 snd_pcm_sframes_t delay
;
1505 struct count_info info
;
1510 substream
= pcm_oss_file
->streams
[stream
];
1511 if (substream
== NULL
)
1513 if ((err
= snd_pcm_oss_make_ready(substream
)) < 0)
1515 runtime
= substream
->runtime
;
1516 if (runtime
->oss
.params
|| runtime
->oss
.prepare
) {
1517 memset(&info
, 0, sizeof(info
));
1518 if (copy_to_user(_info
, &info
, sizeof(info
)))
1522 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1523 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &delay
);
1524 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& delay
< 0)) {
1529 fixup
= runtime
->oss
.buffer_used
;
1532 err
= snd_pcm_oss_capture_position_fixup(substream
, &delay
);
1533 fixup
= -runtime
->oss
.buffer_used
;
1537 info
.ptr
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
% runtime
->buffer_size
);
1538 if (atomic_read(&runtime
->mmap_count
)) {
1539 snd_pcm_sframes_t n
;
1540 n
= (delay
= runtime
->hw_ptr_interrupt
) - runtime
->oss
.prev_hw_ptr_interrupt
;
1542 n
+= runtime
->boundary
;
1543 info
.blocks
= n
/ runtime
->period_size
;
1544 runtime
->oss
.prev_hw_ptr_interrupt
= delay
;
1545 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1546 snd_pcm_oss_simulate_fill(substream
, delay
);
1547 info
.bytes
= snd_pcm_oss_bytes(substream
, runtime
->status
->hw_ptr
) & INT_MAX
;
1549 delay
= snd_pcm_oss_bytes(substream
, delay
);
1550 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1551 if (substream
->oss
.setup
.buggyptr
)
1552 info
.blocks
= (runtime
->oss
.buffer_bytes
- delay
- fixup
) / runtime
->oss
.period_bytes
;
1554 info
.blocks
= (delay
+ fixup
) / runtime
->oss
.period_bytes
;
1555 info
.bytes
= (runtime
->oss
.bytes
- delay
) & INT_MAX
;
1558 info
.blocks
= delay
/ runtime
->oss
.period_bytes
;
1559 info
.bytes
= (runtime
->oss
.bytes
+ delay
) & INT_MAX
;
1562 if (copy_to_user(_info
, &info
, sizeof(info
)))
1567 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct audio_buf_info __user
*_info
)
1569 struct snd_pcm_substream
*substream
;
1570 struct snd_pcm_runtime
*runtime
;
1571 snd_pcm_sframes_t avail
;
1573 struct audio_buf_info info
;
1578 substream
= pcm_oss_file
->streams
[stream
];
1579 if (substream
== NULL
)
1581 runtime
= substream
->runtime
;
1583 if (runtime
->oss
.params
&&
1584 (err
= snd_pcm_oss_change_params(substream
)) < 0)
1587 info
.fragsize
= runtime
->oss
.period_bytes
;
1588 info
.fragstotal
= runtime
->periods
;
1589 if (runtime
->oss
.prepare
) {
1590 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1591 info
.bytes
= runtime
->oss
.period_bytes
* runtime
->oss
.periods
;
1592 info
.fragments
= runtime
->oss
.periods
;
1598 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1599 err
= snd_pcm_kernel_ioctl(substream
, SNDRV_PCM_IOCTL_DELAY
, &avail
);
1600 if (err
== -EPIPE
|| err
== -ESTRPIPE
|| (! err
&& avail
< 0)) {
1601 avail
= runtime
->buffer_size
;
1605 avail
= runtime
->buffer_size
- avail
;
1606 fixup
= -runtime
->oss
.buffer_used
;
1609 err
= snd_pcm_oss_capture_position_fixup(substream
, &avail
);
1610 fixup
= runtime
->oss
.buffer_used
;
1614 info
.bytes
= snd_pcm_oss_bytes(substream
, avail
) + fixup
;
1615 info
.fragments
= info
.bytes
/ runtime
->oss
.period_bytes
;
1619 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info
.bytes
, info
.fragments
, info
.fragstotal
, info
.fragsize
);
1621 if (copy_to_user(_info
, &info
, sizeof(info
)))
1626 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file
*pcm_oss_file
, int stream
, struct buffmem_desc __user
* _info
)
1628 // it won't be probably implemented
1629 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1633 static const char *strip_task_path(const char *path
)
1635 const char *ptr
, *ptrl
= NULL
;
1636 for (ptr
= path
; *ptr
; ptr
++) {
1643 static void snd_pcm_oss_look_for_setup(struct snd_pcm
*pcm
, int stream
,
1644 const char *task_name
,
1645 struct snd_pcm_oss_setup
*rsetup
)
1647 struct snd_pcm_oss_setup
*setup
;
1649 mutex_lock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1651 for (setup
= pcm
->streams
[stream
].oss
.setup_list
; setup
;
1652 setup
= setup
->next
) {
1653 if (!strcmp(setup
->task_name
, task_name
))
1656 } while ((task_name
= strip_task_path(task_name
)) != NULL
);
1660 mutex_unlock(&pcm
->streams
[stream
].oss
.setup_mutex
);
1663 static void snd_pcm_oss_release_substream(struct snd_pcm_substream
*substream
)
1665 struct snd_pcm_runtime
*runtime
;
1666 runtime
= substream
->runtime
;
1667 vfree(runtime
->oss
.buffer
);
1668 runtime
->oss
.buffer
= NULL
;
1669 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1670 snd_pcm_oss_plugin_clear(substream
);
1672 substream
->oss
.oss
= 0;
1675 static void snd_pcm_oss_init_substream(struct snd_pcm_substream
*substream
,
1676 struct snd_pcm_oss_setup
*setup
,
1679 struct snd_pcm_runtime
*runtime
;
1681 substream
->oss
.oss
= 1;
1682 substream
->oss
.setup
= *setup
;
1683 if (setup
->nonblock
)
1684 substream
->ffile
->f_flags
|= O_NONBLOCK
;
1686 substream
->ffile
->f_flags
&= ~O_NONBLOCK
;
1687 runtime
= substream
->runtime
;
1688 runtime
->oss
.params
= 1;
1689 runtime
->oss
.trigger
= 1;
1690 runtime
->oss
.rate
= 8000;
1691 switch (SNDRV_MINOR_OSS_DEVICE(minor
)) {
1692 case SNDRV_MINOR_OSS_PCM_8
:
1693 runtime
->oss
.format
= AFMT_U8
;
1695 case SNDRV_MINOR_OSS_PCM_16
:
1696 runtime
->oss
.format
= AFMT_S16_LE
;
1699 runtime
->oss
.format
= AFMT_MU_LAW
;
1701 runtime
->oss
.channels
= 1;
1702 runtime
->oss
.fragshift
= 0;
1703 runtime
->oss
.maxfrags
= 0;
1704 runtime
->oss
.subdivision
= 0;
1705 substream
->pcm_release
= snd_pcm_oss_release_substream
;
1708 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file
*pcm_oss_file
)
1711 snd_assert(pcm_oss_file
!= NULL
, return -ENXIO
);
1712 for (cidx
= 0; cidx
< 2; ++cidx
) {
1713 struct snd_pcm_substream
*substream
= pcm_oss_file
->streams
[cidx
];
1715 snd_pcm_release_substream(substream
);
1717 kfree(pcm_oss_file
);
1721 static int snd_pcm_oss_open_file(struct file
*file
,
1722 struct snd_pcm
*pcm
,
1723 struct snd_pcm_oss_file
**rpcm_oss_file
,
1725 struct snd_pcm_oss_setup
*setup
)
1728 struct snd_pcm_oss_file
*pcm_oss_file
;
1729 struct snd_pcm_substream
*substream
;
1730 unsigned int f_mode
= file
->f_mode
;
1732 snd_assert(rpcm_oss_file
!= NULL
, return -EINVAL
);
1733 *rpcm_oss_file
= NULL
;
1735 pcm_oss_file
= kzalloc(sizeof(*pcm_oss_file
), GFP_KERNEL
);
1736 if (pcm_oss_file
== NULL
)
1739 if ((f_mode
& (FMODE_WRITE
|FMODE_READ
)) == (FMODE_WRITE
|FMODE_READ
) &&
1740 (pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
))
1741 f_mode
= FMODE_WRITE
;
1743 for (idx
= 0; idx
< 2; idx
++) {
1744 if (setup
[idx
].disable
)
1746 if (idx
== SNDRV_PCM_STREAM_PLAYBACK
) {
1747 if (! (f_mode
& FMODE_WRITE
))
1750 if (! (f_mode
& FMODE_READ
))
1753 err
= snd_pcm_open_substream(pcm
, idx
, file
, &substream
);
1755 snd_pcm_oss_release_file(pcm_oss_file
);
1759 pcm_oss_file
->streams
[idx
] = substream
;
1760 snd_pcm_oss_init_substream(substream
, &setup
[idx
], minor
);
1763 if (! pcm_oss_file
->streams
[0] && pcm_oss_file
->streams
[1]) {
1764 snd_pcm_oss_release_file(pcm_oss_file
);
1768 file
->private_data
= pcm_oss_file
;
1769 *rpcm_oss_file
= pcm_oss_file
;
1774 static int snd_task_name(struct task_struct
*task
, char *name
, size_t size
)
1778 snd_assert(task
!= NULL
&& name
!= NULL
&& size
>= 2, return -EINVAL
);
1779 for (idx
= 0; idx
< sizeof(task
->comm
) && idx
+ 1 < size
; idx
++)
1780 name
[idx
] = task
->comm
[idx
];
1785 static int snd_pcm_oss_open(struct inode
*inode
, struct file
*file
)
1789 struct snd_pcm
*pcm
;
1790 struct snd_pcm_oss_file
*pcm_oss_file
;
1791 struct snd_pcm_oss_setup setup
[2];
1795 pcm
= snd_lookup_oss_minor_data(iminor(inode
),
1796 SNDRV_OSS_DEVICE_TYPE_PCM
);
1801 err
= snd_card_file_add(pcm
->card
, file
);
1804 if (!try_module_get(pcm
->card
->module
)) {
1808 if (snd_task_name(current
, task_name
, sizeof(task_name
)) < 0) {
1812 memset(setup
, 0, sizeof(*setup
));
1813 if (file
->f_mode
& FMODE_WRITE
)
1814 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
1815 task_name
, &setup
[0]);
1816 if (file
->f_mode
& FMODE_READ
)
1817 snd_pcm_oss_look_for_setup(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1818 task_name
, &setup
[1]);
1820 nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
1822 nonblock
= nonblock_open
;
1824 init_waitqueue_entry(&wait
, current
);
1825 add_wait_queue(&pcm
->open_wait
, &wait
);
1826 mutex_lock(&pcm
->open_mutex
);
1828 err
= snd_pcm_oss_open_file(file
, pcm
, &pcm_oss_file
,
1829 iminor(inode
), setup
);
1832 if (err
== -EAGAIN
) {
1839 set_current_state(TASK_INTERRUPTIBLE
);
1840 mutex_unlock(&pcm
->open_mutex
);
1842 mutex_lock(&pcm
->open_mutex
);
1843 if (signal_pending(current
)) {
1848 remove_wait_queue(&pcm
->open_wait
, &wait
);
1849 mutex_unlock(&pcm
->open_mutex
);
1855 module_put(pcm
->card
->module
);
1857 snd_card_file_remove(pcm
->card
, file
);
1862 static int snd_pcm_oss_release(struct inode
*inode
, struct file
*file
)
1864 struct snd_pcm
*pcm
;
1865 struct snd_pcm_substream
*substream
;
1866 struct snd_pcm_oss_file
*pcm_oss_file
;
1868 pcm_oss_file
= file
->private_data
;
1869 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
1870 if (substream
== NULL
)
1871 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
1872 snd_assert(substream
!= NULL
, return -ENXIO
);
1873 pcm
= substream
->pcm
;
1874 snd_pcm_oss_sync(pcm_oss_file
);
1875 mutex_lock(&pcm
->open_mutex
);
1876 snd_pcm_oss_release_file(pcm_oss_file
);
1877 mutex_unlock(&pcm
->open_mutex
);
1878 wake_up(&pcm
->open_wait
);
1879 module_put(pcm
->card
->module
);
1880 snd_card_file_remove(pcm
->card
, file
);
1884 static long snd_pcm_oss_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1886 struct snd_pcm_oss_file
*pcm_oss_file
;
1887 int __user
*p
= (int __user
*)arg
;
1890 pcm_oss_file
= file
->private_data
;
1891 if (cmd
== OSS_GETVERSION
)
1892 return put_user(SNDRV_OSS_VERSION
, p
);
1893 if (cmd
== OSS_ALSAEMULVER
)
1894 return put_user(1, p
);
1895 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1896 if (((cmd
>> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1897 struct snd_pcm_substream
*substream
;
1899 for (idx
= 0; idx
< 2; ++idx
) {
1900 substream
= pcm_oss_file
->streams
[idx
];
1901 if (substream
!= NULL
)
1904 snd_assert(substream
!= NULL
, return -ENXIO
);
1905 return snd_mixer_oss_ioctl_card(substream
->pcm
->card
, cmd
, arg
);
1908 if (((cmd
>> 8) & 0xff) != 'P')
1911 printk("pcm_oss: ioctl = 0x%x\n", cmd
);
1914 case SNDCTL_DSP_RESET
:
1915 return snd_pcm_oss_reset(pcm_oss_file
);
1916 case SNDCTL_DSP_SYNC
:
1917 return snd_pcm_oss_sync(pcm_oss_file
);
1918 case SNDCTL_DSP_SPEED
:
1919 if (get_user(res
, p
))
1921 if ((res
= snd_pcm_oss_set_rate(pcm_oss_file
, res
))<0)
1923 return put_user(res
, p
);
1924 case SOUND_PCM_READ_RATE
:
1925 res
= snd_pcm_oss_get_rate(pcm_oss_file
);
1928 return put_user(res
, p
);
1929 case SNDCTL_DSP_STEREO
:
1930 if (get_user(res
, p
))
1932 res
= res
> 0 ? 2 : 1;
1933 if ((res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
)) < 0)
1935 return put_user(--res
, p
);
1936 case SNDCTL_DSP_GETBLKSIZE
:
1937 res
= snd_pcm_oss_get_block_size(pcm_oss_file
);
1940 return put_user(res
, p
);
1941 case SNDCTL_DSP_SETFMT
:
1942 if (get_user(res
, p
))
1944 res
= snd_pcm_oss_set_format(pcm_oss_file
, res
);
1947 return put_user(res
, p
);
1948 case SOUND_PCM_READ_BITS
:
1949 res
= snd_pcm_oss_get_format(pcm_oss_file
);
1952 return put_user(res
, p
);
1953 case SNDCTL_DSP_CHANNELS
:
1954 if (get_user(res
, p
))
1956 res
= snd_pcm_oss_set_channels(pcm_oss_file
, res
);
1959 return put_user(res
, p
);
1960 case SOUND_PCM_READ_CHANNELS
:
1961 res
= snd_pcm_oss_get_channels(pcm_oss_file
);
1964 return put_user(res
, p
);
1965 case SOUND_PCM_WRITE_FILTER
:
1966 case SOUND_PCM_READ_FILTER
:
1968 case SNDCTL_DSP_POST
:
1969 return snd_pcm_oss_post(pcm_oss_file
);
1970 case SNDCTL_DSP_SUBDIVIDE
:
1971 if (get_user(res
, p
))
1973 res
= snd_pcm_oss_set_subdivide(pcm_oss_file
, res
);
1976 return put_user(res
, p
);
1977 case SNDCTL_DSP_SETFRAGMENT
:
1978 if (get_user(res
, p
))
1980 return snd_pcm_oss_set_fragment(pcm_oss_file
, res
);
1981 case SNDCTL_DSP_GETFMTS
:
1982 res
= snd_pcm_oss_get_formats(pcm_oss_file
);
1985 return put_user(res
, p
);
1986 case SNDCTL_DSP_GETOSPACE
:
1987 case SNDCTL_DSP_GETISPACE
:
1988 return snd_pcm_oss_get_space(pcm_oss_file
,
1989 cmd
== SNDCTL_DSP_GETISPACE
?
1990 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
1991 (struct audio_buf_info __user
*) arg
);
1992 case SNDCTL_DSP_NONBLOCK
:
1993 return snd_pcm_oss_nonblock(file
);
1994 case SNDCTL_DSP_GETCAPS
:
1995 res
= snd_pcm_oss_get_caps(pcm_oss_file
);
1998 return put_user(res
, p
);
1999 case SNDCTL_DSP_GETTRIGGER
:
2000 res
= snd_pcm_oss_get_trigger(pcm_oss_file
);
2003 return put_user(res
, p
);
2004 case SNDCTL_DSP_SETTRIGGER
:
2005 if (get_user(res
, p
))
2007 return snd_pcm_oss_set_trigger(pcm_oss_file
, res
);
2008 case SNDCTL_DSP_GETIPTR
:
2009 case SNDCTL_DSP_GETOPTR
:
2010 return snd_pcm_oss_get_ptr(pcm_oss_file
,
2011 cmd
== SNDCTL_DSP_GETIPTR
?
2012 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2013 (struct count_info __user
*) arg
);
2014 case SNDCTL_DSP_MAPINBUF
:
2015 case SNDCTL_DSP_MAPOUTBUF
:
2016 return snd_pcm_oss_get_mapbuf(pcm_oss_file
,
2017 cmd
== SNDCTL_DSP_MAPINBUF
?
2018 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
,
2019 (struct buffmem_desc __user
*) arg
);
2020 case SNDCTL_DSP_SETSYNCRO
:
2021 /* stop DMA now.. */
2023 case SNDCTL_DSP_SETDUPLEX
:
2024 if (snd_pcm_oss_get_caps(pcm_oss_file
) & DSP_CAP_DUPLEX
)
2027 case SNDCTL_DSP_GETODELAY
:
2028 res
= snd_pcm_oss_get_odelay(pcm_oss_file
);
2030 /* it's for sure, some broken apps don't check for error codes */
2034 return put_user(res
, p
);
2035 case SNDCTL_DSP_PROFILE
:
2036 return 0; /* silently ignore */
2038 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd
);
2043 #ifdef CONFIG_COMPAT
2044 /* all compatible */
2045 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2047 #define snd_pcm_oss_ioctl_compat NULL
2050 static ssize_t
snd_pcm_oss_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
2052 struct snd_pcm_oss_file
*pcm_oss_file
;
2053 struct snd_pcm_substream
*substream
;
2055 pcm_oss_file
= file
->private_data
;
2056 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2057 if (substream
== NULL
)
2060 return snd_pcm_oss_read1(substream
, buf
, count
);
2063 ssize_t res
= snd_pcm_oss_read1(substream
, buf
, count
);
2064 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count
, (long)res
);
2070 static ssize_t
snd_pcm_oss_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
2072 struct snd_pcm_oss_file
*pcm_oss_file
;
2073 struct snd_pcm_substream
*substream
;
2076 pcm_oss_file
= file
->private_data
;
2077 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2078 if (substream
== NULL
)
2080 result
= snd_pcm_oss_write1(substream
, buf
, count
);
2082 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count
, (long)result
);
2087 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream
*substream
)
2089 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2090 if (atomic_read(&runtime
->mmap_count
))
2091 return runtime
->oss
.prev_hw_ptr_interrupt
!= runtime
->hw_ptr_interrupt
;
2093 return snd_pcm_playback_avail(runtime
) >= runtime
->oss
.period_frames
;
2096 static int snd_pcm_oss_capture_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_capture_avail(runtime
) >= runtime
->oss
.period_frames
;
2105 static unsigned int snd_pcm_oss_poll(struct file
*file
, poll_table
* wait
)
2107 struct snd_pcm_oss_file
*pcm_oss_file
;
2109 struct snd_pcm_substream
*psubstream
= NULL
, *csubstream
= NULL
;
2111 pcm_oss_file
= file
->private_data
;
2113 psubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2114 csubstream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2117 if (psubstream
!= NULL
) {
2118 struct snd_pcm_runtime
*runtime
= psubstream
->runtime
;
2119 poll_wait(file
, &runtime
->sleep
, wait
);
2120 snd_pcm_stream_lock_irq(psubstream
);
2121 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
&&
2122 (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
||
2123 snd_pcm_oss_playback_ready(psubstream
)))
2124 mask
|= POLLOUT
| POLLWRNORM
;
2125 snd_pcm_stream_unlock_irq(psubstream
);
2127 if (csubstream
!= NULL
) {
2128 struct snd_pcm_runtime
*runtime
= csubstream
->runtime
;
2129 snd_pcm_state_t ostate
;
2130 poll_wait(file
, &runtime
->sleep
, wait
);
2131 snd_pcm_stream_lock_irq(csubstream
);
2132 if ((ostate
= runtime
->status
->state
) != SNDRV_PCM_STATE_RUNNING
||
2133 snd_pcm_oss_capture_ready(csubstream
))
2134 mask
|= POLLIN
| POLLRDNORM
;
2135 snd_pcm_stream_unlock_irq(csubstream
);
2136 if (ostate
!= SNDRV_PCM_STATE_RUNNING
&& runtime
->oss
.trigger
) {
2137 struct snd_pcm_oss_file ofile
;
2138 memset(&ofile
, 0, sizeof(ofile
));
2139 ofile
.streams
[SNDRV_PCM_STREAM_CAPTURE
] = pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2140 runtime
->oss
.trigger
= 0;
2141 snd_pcm_oss_set_trigger(&ofile
, PCM_ENABLE_INPUT
);
2148 static int snd_pcm_oss_mmap(struct file
*file
, struct vm_area_struct
*area
)
2150 struct snd_pcm_oss_file
*pcm_oss_file
;
2151 struct snd_pcm_substream
*substream
= NULL
;
2152 struct snd_pcm_runtime
*runtime
;
2156 printk("pcm_oss: mmap begin\n");
2158 pcm_oss_file
= file
->private_data
;
2159 switch ((area
->vm_flags
& (VM_READ
| VM_WRITE
))) {
2160 case VM_READ
| VM_WRITE
:
2161 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2166 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
2169 substream
= pcm_oss_file
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
2174 /* set VM_READ access as well to fix memset() routines that do
2175 reads before writes (to improve performance) */
2176 area
->vm_flags
|= VM_READ
;
2177 if (substream
== NULL
)
2179 runtime
= substream
->runtime
;
2180 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP_VALID
))
2182 if (runtime
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2183 runtime
->access
= SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2187 if (runtime
->oss
.params
) {
2188 if ((err
= snd_pcm_oss_change_params(substream
)) < 0)
2191 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2192 if (runtime
->oss
.plugin_first
!= NULL
)
2196 if (area
->vm_pgoff
!= 0)
2199 err
= snd_pcm_mmap_data(substream
, file
, area
);
2202 runtime
->oss
.mmap_bytes
= area
->vm_end
- area
->vm_start
;
2203 runtime
->silence_threshold
= 0;
2204 runtime
->silence_size
= 0;
2206 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime
->oss
.mmap_bytes
);
2208 /* In mmap mode we never stop */
2209 runtime
->stop_threshold
= runtime
->boundary
;
2214 #ifdef CONFIG_PROC_FS
2219 static void snd_pcm_oss_proc_read(struct snd_info_entry
*entry
,
2220 struct snd_info_buffer
*buffer
)
2222 struct snd_pcm_str
*pstr
= entry
->private_data
;
2223 struct snd_pcm_oss_setup
*setup
= pstr
->oss
.setup_list
;
2224 mutex_lock(&pstr
->oss
.setup_mutex
);
2226 snd_iprintf(buffer
, "%s %u %u%s%s%s%s%s%s\n",
2230 setup
->disable
? " disable" : "",
2231 setup
->direct
? " direct" : "",
2232 setup
->block
? " block" : "",
2233 setup
->nonblock
? " non-block" : "",
2234 setup
->partialfrag
? " partial-frag" : "",
2235 setup
->nosilence
? " no-silence" : "");
2236 setup
= setup
->next
;
2238 mutex_unlock(&pstr
->oss
.setup_mutex
);
2241 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str
* pstr
)
2243 struct snd_pcm_oss_setup
*setup
, *setupn
;
2245 for (setup
= pstr
->oss
.setup_list
, pstr
->oss
.setup_list
= NULL
;
2246 setup
; setup
= setupn
) {
2247 setupn
= setup
->next
;
2248 kfree(setup
->task_name
);
2251 pstr
->oss
.setup_list
= NULL
;
2254 static void snd_pcm_oss_proc_write(struct snd_info_entry
*entry
,
2255 struct snd_info_buffer
*buffer
)
2257 struct snd_pcm_str
*pstr
= entry
->private_data
;
2258 char line
[128], str
[32], task_name
[32], *ptr
;
2260 struct snd_pcm_oss_setup
*setup
, *setup1
, template;
2262 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
2263 mutex_lock(&pstr
->oss
.setup_mutex
);
2264 memset(&template, 0, sizeof(template));
2265 ptr
= snd_info_get_str(task_name
, line
, sizeof(task_name
));
2266 if (!strcmp(task_name
, "clear") || !strcmp(task_name
, "erase")) {
2267 snd_pcm_oss_proc_free_setup_list(pstr
);
2268 mutex_unlock(&pstr
->oss
.setup_mutex
);
2271 for (setup
= pstr
->oss
.setup_list
; setup
; setup
= setup
->next
) {
2272 if (!strcmp(setup
->task_name
, task_name
)) {
2277 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2278 template.periods
= simple_strtoul(str
, NULL
, 10);
2279 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2280 template.period_size
= simple_strtoul(str
, NULL
, 10);
2281 for (idx1
= 31; idx1
>= 0; idx1
--)
2282 if (template.period_size
& (1 << idx1
))
2284 for (idx1
--; idx1
>= 0; idx1
--)
2285 template.period_size
&= ~(1 << idx1
);
2287 ptr
= snd_info_get_str(str
, ptr
, sizeof(str
));
2288 if (!strcmp(str
, "disable")) {
2289 template.disable
= 1;
2290 } else if (!strcmp(str
, "direct")) {
2291 template.direct
= 1;
2292 } else if (!strcmp(str
, "block")) {
2294 } else if (!strcmp(str
, "non-block")) {
2295 template.nonblock
= 1;
2296 } else if (!strcmp(str
, "partial-frag")) {
2297 template.partialfrag
= 1;
2298 } else if (!strcmp(str
, "no-silence")) {
2299 template.nosilence
= 1;
2300 } else if (!strcmp(str
, "buggy-ptr")) {
2301 template.buggyptr
= 1;
2304 if (setup
== NULL
) {
2305 setup
= kmalloc(sizeof(*setup
), GFP_KERNEL
);
2307 buffer
->error
= -ENOMEM
;
2308 mutex_lock(&pstr
->oss
.setup_mutex
);
2311 if (pstr
->oss
.setup_list
== NULL
)
2312 pstr
->oss
.setup_list
= setup
;
2314 for (setup1
= pstr
->oss
.setup_list
;
2315 setup1
->next
; setup1
= setup1
->next
);
2316 setup1
->next
= setup
;
2318 template.task_name
= kstrdup(task_name
, GFP_KERNEL
);
2319 if (! template.task_name
) {
2321 buffer
->error
= -ENOMEM
;
2322 mutex_lock(&pstr
->oss
.setup_mutex
);
2327 mutex_unlock(&pstr
->oss
.setup_mutex
);
2331 static void snd_pcm_oss_proc_init(struct snd_pcm
*pcm
)
2334 for (stream
= 0; stream
< 2; ++stream
) {
2335 struct snd_info_entry
*entry
;
2336 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2337 if (pstr
->substream_count
== 0)
2339 if ((entry
= snd_info_create_card_entry(pcm
->card
, "oss", pstr
->proc_root
)) != NULL
) {
2340 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
2341 entry
->mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2342 entry
->c
.text
.read_size
= 8192;
2343 entry
->c
.text
.read
= snd_pcm_oss_proc_read
;
2344 entry
->c
.text
.write_size
= 8192;
2345 entry
->c
.text
.write
= snd_pcm_oss_proc_write
;
2346 entry
->private_data
= pstr
;
2347 if (snd_info_register(entry
) < 0) {
2348 snd_info_free_entry(entry
);
2352 pstr
->oss
.proc_entry
= entry
;
2356 static void snd_pcm_oss_proc_done(struct snd_pcm
*pcm
)
2359 for (stream
= 0; stream
< 2; ++stream
) {
2360 struct snd_pcm_str
*pstr
= &pcm
->streams
[stream
];
2361 if (pstr
->oss
.proc_entry
) {
2362 snd_info_unregister(pstr
->oss
.proc_entry
);
2363 pstr
->oss
.proc_entry
= NULL
;
2364 snd_pcm_oss_proc_free_setup_list(pstr
);
2368 #else /* !CONFIG_PROC_FS */
2369 #define snd_pcm_oss_proc_init(pcm)
2370 #define snd_pcm_oss_proc_done(pcm)
2371 #endif /* CONFIG_PROC_FS */
2377 static struct file_operations snd_pcm_oss_f_reg
=
2379 .owner
= THIS_MODULE
,
2380 .read
= snd_pcm_oss_read
,
2381 .write
= snd_pcm_oss_write
,
2382 .open
= snd_pcm_oss_open
,
2383 .release
= snd_pcm_oss_release
,
2384 .poll
= snd_pcm_oss_poll
,
2385 .unlocked_ioctl
= snd_pcm_oss_ioctl
,
2386 .compat_ioctl
= snd_pcm_oss_ioctl_compat
,
2387 .mmap
= snd_pcm_oss_mmap
,
2390 static void register_oss_dsp(struct snd_pcm
*pcm
, int index
)
2393 sprintf(name
, "dsp%i%i", pcm
->card
->number
, pcm
->device
);
2394 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2395 pcm
->card
, index
, &snd_pcm_oss_f_reg
,
2397 snd_printk(KERN_ERR
"unable to register OSS PCM device %i:%i\n",
2398 pcm
->card
->number
, pcm
->device
);
2402 static int snd_pcm_oss_register_minor(struct snd_pcm
*pcm
)
2405 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2408 register_oss_dsp(pcm
, 0);
2409 duplex
= (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream_count
> 0 &&
2410 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream_count
&&
2411 !(pcm
->info_flags
& SNDRV_PCM_INFO_HALF_DUPLEX
));
2412 sprintf(name
, "%s%s", pcm
->name
, duplex
? " (DUPLEX)" : "");
2413 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2414 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO
,
2419 pcm
->oss
.reg_mask
|= 1;
2421 if (adsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2422 register_oss_dsp(pcm
, 1);
2424 pcm
->oss
.reg_mask
|= 2;
2428 snd_pcm_oss_proc_init(pcm
);
2433 static int snd_pcm_oss_disconnect_minor(struct snd_pcm
*pcm
)
2436 if (pcm
->oss
.reg_mask
& 1) {
2437 pcm
->oss
.reg_mask
&= ~1;
2438 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2441 if (pcm
->oss
.reg_mask
& 2) {
2442 pcm
->oss
.reg_mask
&= ~2;
2443 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM
,
2450 static int snd_pcm_oss_unregister_minor(struct snd_pcm
*pcm
)
2452 snd_pcm_oss_disconnect_minor(pcm
);
2454 if (dsp_map
[pcm
->card
->number
] == (int)pcm
->device
) {
2455 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2456 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO
, pcm
->card
->number
);
2460 snd_pcm_oss_proc_done(pcm
);
2465 static struct snd_pcm_notify snd_pcm_oss_notify
=
2467 .n_register
= snd_pcm_oss_register_minor
,
2468 .n_disconnect
= snd_pcm_oss_disconnect_minor
,
2469 .n_unregister
= snd_pcm_oss_unregister_minor
,
2472 static int __init
alsa_pcm_oss_init(void)
2477 /* check device map table */
2478 for (i
= 0; i
< SNDRV_CARDS
; i
++) {
2479 if (dsp_map
[i
] < 0 || dsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2480 snd_printk(KERN_ERR
"invalid dsp_map[%d] = %d\n",
2484 if (adsp_map
[i
] < 0 || adsp_map
[i
] >= SNDRV_PCM_DEVICES
) {
2485 snd_printk(KERN_ERR
"invalid adsp_map[%d] = %d\n",
2490 if ((err
= snd_pcm_notify(&snd_pcm_oss_notify
, 0)) < 0)
2495 static void __exit
alsa_pcm_oss_exit(void)
2497 snd_pcm_notify(&snd_pcm_oss_notify
, 1);
2500 module_init(alsa_pcm_oss_init
)
2501 module_exit(alsa_pcm_oss_exit
)