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/slab.h>
32 #include <linux/time.h>
33 #include <linux/vmalloc.h>
34 #include <linux/moduleparam.h>
35 #include <sound/core.h>
36 #include <sound/minors.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include "pcm_plugin.h"
40 #include <sound/info.h>
41 #include <linux/soundcard.h>
42 #include <sound/initval.h>
44 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
46 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
47 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48 static int nonblock_open;
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, boot_devs, 0444);
55 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
56 MODULE_PARM_SYNTAX(dsp_map, "default:0,skill:advanced");
57 module_param_array(adsp_map, int, boot_devs, 0444);
58 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59 MODULE_PARM_SYNTAX(adsp_map, "default:1,skill:advanced");
60 module_param(nonblock_open, bool, 0644);
61 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
62 MODULE_PARM_SYNTAX(nonblock_open, "default:0,skill:advanced");
63 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
64 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
66 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
67 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
68 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
69 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
71 static inline mm_segment_t snd_enter_user(void)
73 mm_segment_t fs = get_fs();
78 static inline void snd_leave_user(mm_segment_t fs)
83 int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
85 snd_pcm_runtime_t *runtime = substream->runtime;
86 snd_pcm_plugin_t *plugin, *next;
88 plugin = runtime->oss.plugin_first;
91 snd_pcm_plugin_free(plugin);
94 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
98 int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
100 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
101 plugin->next = runtime->oss.plugin_first;
103 if (runtime->oss.plugin_first) {
104 runtime->oss.plugin_first->prev = plugin;
105 runtime->oss.plugin_first = plugin;
107 runtime->oss.plugin_last =
108 runtime->oss.plugin_first = plugin;
113 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
115 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
117 plugin->prev = runtime->oss.plugin_last;
118 if (runtime->oss.plugin_last) {
119 runtime->oss.plugin_last->next = plugin;
120 runtime->oss.plugin_last = plugin;
122 runtime->oss.plugin_last =
123 runtime->oss.plugin_first = plugin;
128 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
130 snd_pcm_runtime_t *runtime = substream->runtime;
131 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
132 frames = frames_to_bytes(runtime, frames);
133 if (buffer_size == runtime->oss.buffer_bytes)
135 return (runtime->oss.buffer_bytes * frames) / buffer_size;
138 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
140 snd_pcm_runtime_t *runtime = substream->runtime;
141 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
142 if (buffer_size == runtime->oss.buffer_bytes)
143 return bytes_to_frames(runtime, bytes);
144 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
147 static int snd_pcm_oss_format_from(int format)
150 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
151 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
152 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
153 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
154 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
155 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
156 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
157 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
158 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
159 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
160 default: return SNDRV_PCM_FORMAT_U8;
164 static int snd_pcm_oss_format_to(int format)
167 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
168 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
169 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
170 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
171 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
172 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
173 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
174 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
175 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
176 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
177 default: return -EINVAL;
181 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream,
182 snd_pcm_hw_params_t *oss_params,
183 snd_pcm_hw_params_t *slave_params)
186 size_t oss_buffer_size, oss_period_size, oss_periods;
187 size_t min_period_size, max_period_size;
188 snd_pcm_runtime_t *runtime = substream->runtime;
189 size_t oss_frame_size;
191 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
192 params_channels(oss_params) / 8;
194 oss_buffer_size = snd_pcm_plug_client_size(substream,
195 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0)) * oss_frame_size;
196 oss_buffer_size = 1 << ld2(oss_buffer_size);
197 if (atomic_read(&runtime->mmap_count)) {
198 if (oss_buffer_size > runtime->oss.mmap_bytes)
199 oss_buffer_size = runtime->oss.mmap_bytes;
202 if (substream->oss.setup &&
203 substream->oss.setup->period_size > 16)
204 oss_period_size = substream->oss.setup->period_size;
205 else if (runtime->oss.fragshift) {
206 oss_period_size = 1 << runtime->oss.fragshift;
207 if (oss_period_size > oss_buffer_size / 2)
208 oss_period_size = oss_buffer_size / 2;
211 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
213 oss_period_size = oss_buffer_size;
215 oss_period_size /= 2;
216 } while (oss_period_size > bytes_per_sec);
217 if (runtime->oss.subdivision == 0) {
219 if (oss_period_size / sd > 4096)
221 if (oss_period_size / sd < 4096)
224 sd = runtime->oss.subdivision;
225 oss_period_size /= sd;
226 if (oss_period_size < 16)
227 oss_period_size = 16;
230 min_period_size = snd_pcm_plug_client_size(substream,
231 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
232 min_period_size *= oss_frame_size;
233 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
234 if (oss_period_size < min_period_size)
235 oss_period_size = min_period_size;
237 max_period_size = snd_pcm_plug_client_size(substream,
238 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
239 max_period_size *= oss_frame_size;
240 max_period_size = 1 << ld2(max_period_size);
241 if (oss_period_size > max_period_size)
242 oss_period_size = max_period_size;
244 oss_periods = oss_buffer_size / oss_period_size;
246 if (substream->oss.setup) {
247 if (substream->oss.setup->periods > 1)
248 oss_periods = substream->oss.setup->periods;
251 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
252 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
253 s = runtime->oss.maxfrags;
257 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
263 while (oss_period_size * oss_periods > oss_buffer_size)
264 oss_period_size /= 2;
266 snd_assert(oss_period_size >= 16, return -EINVAL);
267 runtime->oss.period_bytes = oss_period_size;
268 runtime->oss.period_frames = 1;
269 runtime->oss.periods = oss_periods;
273 static int choose_rate(snd_pcm_substream_t *substream,
274 snd_pcm_hw_params_t *params, unsigned int best_rate)
277 snd_pcm_hw_params_t *save;
278 unsigned int rate, prev;
280 save = kmalloc(sizeof(*save), GFP_KERNEL);
284 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
286 /* try multiples of the best rate */
289 if (it->max < rate || (it->max == rate && it->openmax))
291 if (it->min < rate || (it->min == rate && !it->openmin)) {
293 ret = snd_pcm_hw_param_set(substream, params,
294 SNDRV_PCM_HW_PARAM_RATE,
296 if (ret == (int)rate) {
308 /* not found, use the nearest rate */
310 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, 0);
313 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
315 snd_pcm_runtime_t *runtime = substream->runtime;
316 snd_pcm_hw_params_t *params, *sparams;
317 snd_pcm_sw_params_t *sw_params;
318 ssize_t oss_buffer_size, oss_period_size;
319 size_t oss_frame_size;
322 int format, sformat, n;
323 snd_mask_t sformat_mask;
326 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
327 params = kmalloc(sizeof(*params), GFP_KERNEL);
328 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
329 if (!sw_params || !params || !sparams) {
330 snd_printd("No memory\n");
335 if (atomic_read(&runtime->mmap_count)) {
338 snd_pcm_oss_setup_t *setup = substream->oss.setup;
339 direct = (setup != NULL && setup->direct);
342 _snd_pcm_hw_params_any(sparams);
343 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
344 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
345 snd_mask_none(&mask);
346 if (atomic_read(&runtime->mmap_count))
347 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
349 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
351 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
353 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
355 snd_printd("No usable accesses\n");
359 choose_rate(substream, sparams, runtime->oss.rate);
360 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, 0);
362 format = snd_pcm_oss_format_from(runtime->oss.format);
364 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
368 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
370 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
371 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
372 if (snd_mask_test(&sformat_mask, sformat) &&
373 snd_pcm_oss_format_to(sformat) >= 0)
376 if (sformat > SNDRV_PCM_FORMAT_LAST) {
377 snd_printd("Cannot find a format!!!\n");
382 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
383 snd_assert(err >= 0, goto failure);
386 memcpy(params, sparams, sizeof(*params));
388 _snd_pcm_hw_params_any(params);
389 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
390 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
391 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
392 snd_pcm_oss_format_from(runtime->oss.format), 0);
393 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
394 runtime->oss.channels, 0);
395 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
396 runtime->oss.rate, 0);
397 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
398 params_access(params), params_format(params),
399 params_channels(params), params_rate(params));
401 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
402 params_access(sparams), params_format(sparams),
403 params_channels(sparams), params_rate(sparams));
405 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
406 params_channels(params) / 8;
408 snd_pcm_oss_plugin_clear(substream);
410 /* add necessary plugins */
411 snd_pcm_oss_plugin_clear(substream);
412 if ((err = snd_pcm_plug_format_plugins(substream,
415 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
416 snd_pcm_oss_plugin_clear(substream);
419 if (runtime->oss.plugin_first) {
420 snd_pcm_plugin_t *plugin;
421 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
422 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
423 snd_pcm_oss_plugin_clear(substream);
426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
427 err = snd_pcm_plugin_append(plugin);
429 err = snd_pcm_plugin_insert(plugin);
432 snd_pcm_oss_plugin_clear(substream);
438 err = snd_pcm_oss_period_size(substream, params, sparams);
442 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
443 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, 0);
444 snd_assert(err >= 0, goto failure);
446 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
447 runtime->oss.periods, 0);
448 snd_assert(err >= 0, goto failure);
450 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
452 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
453 snd_printd("HW_PARAMS failed: %i\n", err);
457 memset(sw_params, 0, sizeof(*sw_params));
458 if (runtime->oss.trigger) {
459 sw_params->start_threshold = 1;
461 sw_params->start_threshold = runtime->boundary;
463 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
464 sw_params->stop_threshold = runtime->boundary;
466 sw_params->stop_threshold = runtime->buffer_size;
467 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
468 sw_params->period_step = 1;
469 sw_params->sleep_min = 0;
470 sw_params->avail_min = 1;
471 sw_params->xfer_align = 1;
472 if (atomic_read(&runtime->mmap_count) ||
473 (substream->oss.setup && substream->oss.setup->nosilence)) {
474 sw_params->silence_threshold = 0;
475 sw_params->silence_size = 0;
477 snd_pcm_uframes_t frames;
478 frames = runtime->period_size + 16;
479 if (frames > runtime->buffer_size)
480 frames = runtime->buffer_size;
481 sw_params->silence_threshold = frames;
482 sw_params->silence_size = frames;
485 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
486 snd_printd("SW_PARAMS failed: %i\n", err);
490 runtime->oss.periods = params_periods(sparams);
491 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
492 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
493 if (runtime->oss.plugin_first) {
494 err = snd_pcm_plug_alloc(substream, oss_period_size);
498 oss_period_size *= oss_frame_size;
500 oss_buffer_size = oss_period_size * runtime->oss.periods;
501 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
503 runtime->oss.period_bytes = oss_period_size;
504 runtime->oss.buffer_bytes = oss_buffer_size;
506 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
507 runtime->oss.period_bytes,
508 runtime->oss.buffer_bytes);
509 pdprintf("slave: period_size = %i, buffer_size = %i\n",
510 params_period_size(sparams),
511 params_buffer_size(sparams));
513 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
514 runtime->oss.channels = params_channels(params);
515 runtime->oss.rate = params_rate(params);
517 runtime->oss.params = 0;
518 runtime->oss.prepare = 1;
519 if (runtime->oss.buffer != NULL)
520 vfree(runtime->oss.buffer);
521 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
522 runtime->oss.buffer_used = 0;
523 if (runtime->dma_area)
524 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
526 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
539 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
542 snd_pcm_substream_t *asubstream = NULL, *substream;
544 for (idx = 0; idx < 2; idx++) {
545 substream = pcm_oss_file->streams[idx];
546 if (substream == NULL)
548 if (asubstream == NULL)
549 asubstream = substream;
550 if (substream->runtime->oss.params) {
551 err = snd_pcm_oss_change_params(substream);
556 snd_assert(asubstream != NULL, return -EIO);
558 *r_substream = asubstream;
562 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
565 snd_pcm_runtime_t *runtime = substream->runtime;
567 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, 0);
569 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
572 runtime->oss.prepare = 0;
573 runtime->oss.prev_hw_ptr_interrupt = 0;
574 runtime->oss.period_ptr = 0;
575 runtime->oss.buffer_used = 0;
580 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
582 snd_pcm_runtime_t *runtime;
585 if (substream == NULL)
587 runtime = substream->runtime;
588 if (runtime->oss.params) {
589 err = snd_pcm_oss_change_params(substream);
593 if (runtime->oss.prepare) {
594 err = snd_pcm_oss_prepare(substream);
601 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
603 snd_pcm_runtime_t *runtime;
604 snd_pcm_uframes_t frames;
608 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
611 runtime = substream->runtime;
612 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
614 /* in case of overrun, skip whole periods like OSS/Linux driver does */
615 /* until avail(delay) <= buffer_size */
616 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
617 frames /= runtime->period_size;
618 frames *= runtime->period_size;
619 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
626 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
628 snd_pcm_runtime_t *runtime = substream->runtime;
631 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
632 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
634 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
635 printk("pcm_oss: write: recovering from XRUN\n");
637 printk("pcm_oss: write: recovering from SUSPEND\n");
639 ret = snd_pcm_oss_prepare(substream);
645 fs = snd_enter_user();
646 ret = snd_pcm_lib_write(substream, ptr, frames);
649 ret = snd_pcm_lib_write(substream, ptr, frames);
651 if (ret != -EPIPE && ret != -ESTRPIPE)
653 /* test, if we can't store new data, because the stream */
654 /* has not been started */
655 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
661 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
663 snd_pcm_runtime_t *runtime = substream->runtime;
664 snd_pcm_sframes_t delay;
667 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
668 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
670 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
671 printk("pcm_oss: read: recovering from XRUN\n");
673 printk("pcm_oss: read: recovering from SUSPEND\n");
675 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
678 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
679 ret = snd_pcm_oss_prepare(substream);
683 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
688 fs = snd_enter_user();
689 ret = snd_pcm_lib_read(substream, ptr, frames);
692 ret = snd_pcm_lib_read(substream, ptr, frames);
695 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
696 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
702 if (ret != -ESTRPIPE)
708 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
710 snd_pcm_runtime_t *runtime = substream->runtime;
713 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
714 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
716 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
717 printk("pcm_oss: writev: recovering from XRUN\n");
719 printk("pcm_oss: writev: recovering from SUSPEND\n");
721 ret = snd_pcm_oss_prepare(substream);
727 fs = snd_enter_user();
728 ret = snd_pcm_lib_writev(substream, bufs, frames);
731 ret = snd_pcm_lib_writev(substream, bufs, frames);
733 if (ret != -EPIPE && ret != -ESTRPIPE)
736 /* test, if we can't store new data, because the stream */
737 /* has not been started */
738 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
744 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
746 snd_pcm_runtime_t *runtime = substream->runtime;
749 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
750 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
752 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
753 printk("pcm_oss: readv: recovering from XRUN\n");
755 printk("pcm_oss: readv: recovering from SUSPEND\n");
757 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
760 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
761 ret = snd_pcm_oss_prepare(substream);
767 fs = snd_enter_user();
768 ret = snd_pcm_lib_readv(substream, bufs, frames);
771 ret = snd_pcm_lib_readv(substream, bufs, frames);
773 if (ret != -EPIPE && ret != -ESTRPIPE)
779 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
781 snd_pcm_runtime_t *runtime = substream->runtime;
782 snd_pcm_sframes_t frames, frames1;
783 if (runtime->oss.plugin_first) {
784 snd_pcm_plugin_channel_t *channels;
785 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
787 if (copy_from_user(runtime->oss.buffer, buf, bytes))
789 buf = runtime->oss.buffer;
791 frames = bytes / oss_frame_bytes;
792 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
795 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
798 bytes = frames1 * oss_frame_bytes;
800 frames = bytes_to_frames(runtime, bytes);
801 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
804 bytes = frames_to_bytes(runtime, frames1);
809 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char *buf, size_t bytes)
813 snd_pcm_runtime_t *runtime = substream->runtime;
815 if (atomic_read(&runtime->mmap_count))
818 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
821 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
823 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
824 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
826 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
827 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
829 runtime->oss.buffer_used += tmp;
833 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
834 runtime->oss.buffer_used == runtime->oss.period_bytes) {
835 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer, runtime->oss.buffer_used, 1);
837 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
838 runtime->oss.bytes += tmp;
839 runtime->oss.buffer_used = 0;
840 runtime->oss.period_ptr += tmp;
841 runtime->oss.period_ptr %= runtime->oss.period_bytes;
844 tmp = snd_pcm_oss_write2(substream, (char *)buf, runtime->oss.period_bytes, 0);
846 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
847 runtime->oss.bytes += tmp;
856 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
858 snd_pcm_runtime_t *runtime = substream->runtime;
859 snd_pcm_sframes_t frames, frames1;
860 char *final_dst = buf;
861 if (runtime->oss.plugin_first) {
862 snd_pcm_plugin_channel_t *channels;
863 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
865 buf = runtime->oss.buffer;
866 frames = bytes / oss_frame_bytes;
867 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
870 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
873 bytes = frames1 * oss_frame_bytes;
874 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
877 frames = bytes_to_frames(runtime, bytes);
878 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
881 bytes = frames_to_bytes(runtime, frames1);
886 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char *buf, size_t bytes)
890 snd_pcm_runtime_t *runtime = substream->runtime;
892 if (atomic_read(&runtime->mmap_count))
895 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
898 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
899 if (runtime->oss.buffer_used == 0) {
900 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
902 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
903 runtime->oss.bytes += tmp;
904 runtime->oss.period_ptr = tmp;
905 runtime->oss.buffer_used = tmp;
908 if ((size_t) tmp > runtime->oss.buffer_used)
909 tmp = runtime->oss.buffer_used;
910 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
911 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
915 runtime->oss.buffer_used -= tmp;
917 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
919 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
920 runtime->oss.bytes += tmp;
929 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
931 snd_pcm_substream_t *substream;
933 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
934 if (substream != NULL) {
935 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
936 substream->runtime->oss.prepare = 1;
938 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
939 if (substream != NULL) {
940 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
941 substream->runtime->oss.prepare = 1;
946 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
948 snd_pcm_substream_t *substream;
951 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
952 if (substream != NULL) {
953 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
955 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, 0);
957 /* note: all errors from the start action are ignored */
958 /* OSS apps do not know, how to handle them */
962 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
964 snd_pcm_runtime_t *runtime;
969 runtime = substream->runtime;
970 init_waitqueue_entry(&wait, current);
971 add_wait_queue(&runtime->sleep, &wait);
973 printk("sync1: size = %li\n", size);
976 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
978 runtime->oss.buffer_used = 0;
982 if (result != 0 && result != -EAGAIN)
985 set_current_state(TASK_INTERRUPTIBLE);
986 snd_pcm_stream_lock_irq(substream);
987 res = runtime->status->state;
988 snd_pcm_stream_unlock_irq(substream);
989 if (res != SNDRV_PCM_STATE_RUNNING) {
990 set_current_state(TASK_RUNNING);
993 res = schedule_timeout(10 * HZ);
994 if (signal_pending(current)) {
995 result = -ERESTARTSYS;
999 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1004 remove_wait_queue(&runtime->sleep, &wait);
1008 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1011 unsigned int saved_f_flags;
1012 snd_pcm_substream_t *substream;
1013 snd_pcm_runtime_t *runtime;
1014 snd_pcm_format_t format;
1015 unsigned long width;
1018 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1019 if (substream != NULL) {
1020 runtime = substream->runtime;
1021 if (atomic_read(&runtime->mmap_count))
1023 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1025 format = snd_pcm_oss_format_from(runtime->oss.format);
1026 width = snd_pcm_format_physical_width(format);
1027 if (runtime->oss.buffer_used > 0) {
1029 printk("sync: buffer_used\n");
1031 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1032 snd_pcm_format_set_silence(format,
1033 runtime->oss.buffer + runtime->oss.buffer_used,
1035 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1038 } else if (runtime->oss.period_ptr > 0) {
1040 printk("sync: period_ptr\n");
1042 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1043 snd_pcm_format_set_silence(format,
1044 runtime->oss.buffer,
1046 err = snd_pcm_oss_sync1(substream, size);
1051 * The ALSA's period might be a bit large than OSS one.
1052 * Fill the remain portion of ALSA period with zeros.
1054 size = runtime->control->appl_ptr % runtime->period_size;
1056 size = runtime->period_size - size;
1057 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1058 size = (runtime->frame_bits * size) / 8;
1060 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1063 size1 /= runtime->sample_bits;
1064 snd_pcm_format_set_silence(runtime->format,
1065 runtime->oss.buffer,
1067 snd_pcm_lib_write(substream, runtime->oss.buffer, size1);
1069 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1070 void *buffers[runtime->channels];
1071 memset(buffers, 0, runtime->channels * sizeof(void *));
1072 snd_pcm_lib_writev(substream, (void **)buffers, size);
1076 * finish sync: drain the buffer
1079 saved_f_flags = substream->ffile->f_flags;
1080 substream->ffile->f_flags &= ~O_NONBLOCK;
1081 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
1082 substream->ffile->f_flags = saved_f_flags;
1085 runtime->oss.prepare = 1;
1088 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1089 if (substream != NULL) {
1090 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1092 runtime = substream->runtime;
1093 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
1096 runtime->oss.buffer_used = 0;
1097 runtime->oss.prepare = 1;
1102 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1106 for (idx = 1; idx >= 0; --idx) {
1107 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1108 snd_pcm_runtime_t *runtime;
1109 if (substream == NULL)
1111 runtime = substream->runtime;
1114 else if (rate > 192000)
1116 if (runtime->oss.rate != rate) {
1117 runtime->oss.params = 1;
1118 runtime->oss.rate = rate;
1121 return snd_pcm_oss_get_rate(pcm_oss_file);
1124 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1126 snd_pcm_substream_t *substream;
1129 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1131 return substream->runtime->oss.rate;
1134 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1141 for (idx = 1; idx >= 0; --idx) {
1142 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1143 snd_pcm_runtime_t *runtime;
1144 if (substream == NULL)
1146 runtime = substream->runtime;
1147 if (runtime->oss.channels != channels) {
1148 runtime->oss.params = 1;
1149 runtime->oss.channels = channels;
1152 return snd_pcm_oss_get_channels(pcm_oss_file);
1155 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1157 snd_pcm_substream_t *substream;
1160 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1162 return substream->runtime->oss.channels;
1165 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1167 snd_pcm_substream_t *substream;
1170 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1172 return substream->runtime->oss.period_bytes;
1175 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1177 snd_pcm_substream_t *substream;
1180 snd_pcm_hw_params_t params;
1181 unsigned int formats = 0;
1182 snd_mask_t format_mask;
1184 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1186 if (atomic_read(&substream->runtime->mmap_count)) {
1189 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1190 direct = (setup != NULL && setup->direct);
1193 return AFMT_MU_LAW | AFMT_U8 |
1194 AFMT_S16_LE | AFMT_S16_BE |
1195 AFMT_S8 | AFMT_U16_LE |
1197 _snd_pcm_hw_params_any(¶ms);
1198 err = snd_pcm_hw_refine(substream, ¶ms);
1199 snd_assert(err >= 0, return err);
1200 format_mask = *hw_param_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT);
1201 for (fmt = 0; fmt < 32; ++fmt) {
1202 if (snd_mask_test(&format_mask, fmt)) {
1203 int f = snd_pcm_oss_format_to(fmt);
1211 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1215 if (format != AFMT_QUERY) {
1216 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1217 if (!(formats & format))
1219 for (idx = 1; idx >= 0; --idx) {
1220 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1221 snd_pcm_runtime_t *runtime;
1222 if (substream == NULL)
1224 runtime = substream->runtime;
1225 if (runtime->oss.format != format) {
1226 runtime->oss.params = 1;
1227 runtime->oss.format = format;
1231 return snd_pcm_oss_get_format(pcm_oss_file);
1234 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1236 snd_pcm_substream_t *substream;
1239 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1241 return substream->runtime->oss.format;
1244 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1246 snd_pcm_runtime_t *runtime;
1248 if (substream == NULL)
1250 runtime = substream->runtime;
1251 if (subdivide == 0) {
1252 subdivide = runtime->oss.subdivision;
1257 if (runtime->oss.subdivision || runtime->oss.fragshift)
1259 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1260 subdivide != 8 && subdivide != 16)
1262 runtime->oss.subdivision = subdivide;
1263 runtime->oss.params = 1;
1267 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1269 int err = -EINVAL, idx;
1271 for (idx = 1; idx >= 0; --idx) {
1272 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1273 if (substream == NULL)
1275 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1281 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1283 snd_pcm_runtime_t *runtime;
1285 if (substream == NULL)
1287 runtime = substream->runtime;
1288 if (runtime->oss.subdivision || runtime->oss.fragshift)
1290 runtime->oss.fragshift = val & 0xffff;
1291 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1292 if (runtime->oss.fragshift < 4) /* < 16 */
1293 runtime->oss.fragshift = 4;
1294 if (runtime->oss.maxfrags < 2)
1295 runtime->oss.maxfrags = 2;
1296 runtime->oss.params = 1;
1300 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1302 int err = -EINVAL, idx;
1304 for (idx = 1; idx >= 0; --idx) {
1305 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1306 if (substream == NULL)
1308 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1314 static int snd_pcm_oss_nonblock(struct file * file)
1316 file->f_flags |= O_NONBLOCK;
1320 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1323 if (substream == NULL) {
1324 res &= ~DSP_CAP_DUPLEX;
1327 #ifdef DSP_CAP_MULTI
1328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1329 if (substream->pstr->substream_count > 1)
1330 res |= DSP_CAP_MULTI;
1332 /* DSP_CAP_REALTIME is set all times: */
1333 /* all ALSA drivers can return actual pointer in ring buffer */
1334 #if defined(DSP_CAP_REALTIME) && 0
1336 snd_pcm_runtime_t *runtime = substream->runtime;
1337 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1338 res &= ~DSP_CAP_REALTIME;
1344 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1348 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1349 for (idx = 0; idx < 2; idx++) {
1350 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1351 result = snd_pcm_oss_get_caps1(substream, result);
1353 result |= 0x0001; /* revision - same as SB AWE 64 */
1357 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1359 snd_pcm_runtime_t *runtime = substream->runtime;
1360 snd_pcm_uframes_t appl_ptr;
1361 appl_ptr = hw_ptr + runtime->buffer_size;
1362 appl_ptr %= runtime->boundary;
1363 runtime->control->appl_ptr = appl_ptr;
1366 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1368 snd_pcm_runtime_t *runtime;
1369 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1373 printk("pcm_oss: trigger = 0x%x\n", trigger);
1376 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1377 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1380 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1384 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1388 runtime = psubstream->runtime;
1389 if (trigger & PCM_ENABLE_OUTPUT) {
1390 if (runtime->oss.trigger)
1392 if (atomic_read(&psubstream->runtime->mmap_count))
1393 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1394 runtime->oss.trigger = 1;
1395 runtime->start_threshold = 1;
1396 cmd = SNDRV_PCM_IOCTL_START;
1398 if (!runtime->oss.trigger)
1400 runtime->oss.trigger = 0;
1401 runtime->start_threshold = runtime->boundary;
1402 cmd = SNDRV_PCM_IOCTL_DROP;
1403 runtime->oss.prepare = 1;
1405 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, 0);
1411 runtime = csubstream->runtime;
1412 if (trigger & PCM_ENABLE_INPUT) {
1413 if (runtime->oss.trigger)
1415 runtime->oss.trigger = 1;
1416 runtime->start_threshold = 1;
1417 cmd = SNDRV_PCM_IOCTL_START;
1419 if (!runtime->oss.trigger)
1421 runtime->oss.trigger = 0;
1422 runtime->start_threshold = runtime->boundary;
1423 cmd = SNDRV_PCM_IOCTL_DROP;
1424 runtime->oss.prepare = 1;
1426 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, 0);
1434 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1436 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1439 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1440 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1441 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1442 result |= PCM_ENABLE_OUTPUT;
1443 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1444 result |= PCM_ENABLE_INPUT;
1448 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1450 snd_pcm_substream_t *substream;
1451 snd_pcm_runtime_t *runtime;
1452 snd_pcm_sframes_t delay;
1455 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1456 if (substream == NULL)
1458 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1460 runtime = substream->runtime;
1461 if (runtime->oss.params || runtime->oss.prepare)
1463 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1465 delay = 0; /* hack for broken OSS applications */
1468 return snd_pcm_oss_bytes(substream, delay);
1471 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info * _info)
1473 snd_pcm_substream_t *substream;
1474 snd_pcm_runtime_t *runtime;
1475 snd_pcm_sframes_t delay;
1477 struct count_info info;
1482 substream = pcm_oss_file->streams[stream];
1483 if (substream == NULL)
1485 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1487 runtime = substream->runtime;
1488 if (runtime->oss.params || runtime->oss.prepare) {
1489 memset(&info, 0, sizeof(info));
1490 if (copy_to_user(_info, &info, sizeof(info)))
1494 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1495 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1496 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1501 fixup = runtime->oss.buffer_used;
1504 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1505 fixup = -runtime->oss.buffer_used;
1509 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1510 if (atomic_read(&runtime->mmap_count)) {
1511 snd_pcm_sframes_t n;
1512 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1514 n += runtime->boundary;
1515 info.blocks = n / runtime->period_size;
1516 runtime->oss.prev_hw_ptr_interrupt = delay;
1517 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1518 snd_pcm_oss_simulate_fill(substream, delay);
1519 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1521 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1522 info.blocks = delay / runtime->oss.period_bytes;
1523 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1524 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1526 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1528 if (copy_to_user(_info, &info, sizeof(info)))
1533 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info *_info)
1535 snd_pcm_substream_t *substream;
1536 snd_pcm_runtime_t *runtime;
1537 snd_pcm_sframes_t avail;
1539 struct audio_buf_info info;
1544 substream = pcm_oss_file->streams[stream];
1545 if (substream == NULL)
1547 runtime = substream->runtime;
1549 if (runtime->oss.params &&
1550 (err = snd_pcm_oss_change_params(substream)) < 0)
1553 info.fragsize = runtime->oss.period_bytes;
1554 info.fragstotal = runtime->periods;
1555 if (runtime->oss.prepare) {
1556 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1557 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1558 info.fragments = runtime->oss.periods;
1564 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1565 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1566 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1567 avail = runtime->buffer_size;
1571 avail = runtime->buffer_size - avail;
1572 fixup = -runtime->oss.buffer_used;
1575 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1576 fixup = runtime->oss.buffer_used;
1580 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1581 info.fragments = info.bytes / runtime->oss.period_bytes;
1585 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1587 if (copy_to_user(_info, &info, sizeof(info)))
1592 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc * _info)
1594 // it won't be probably implemented
1595 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1599 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1601 const char *ptr, *ptrl;
1602 snd_pcm_oss_setup_t *setup;
1604 down(&pcm->streams[stream].oss.setup_mutex);
1605 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1606 if (!strcmp(setup->task_name, task_name)) {
1607 up(&pcm->streams[stream].oss.setup_mutex);
1611 ptr = ptrl = task_name;
1617 if (ptrl == task_name) {
1621 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1622 if (!strcmp(setup->task_name, ptrl)) {
1623 up(&pcm->streams[stream].oss.setup_mutex);
1628 up(&pcm->streams[stream].oss.setup_mutex);
1632 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1633 snd_pcm_oss_setup_t *setup,
1636 snd_pcm_runtime_t *runtime;
1638 substream->oss.oss = 1;
1639 substream->oss.setup = setup;
1640 runtime = substream->runtime;
1641 runtime->oss.params = 1;
1642 runtime->oss.trigger = 1;
1643 runtime->oss.rate = 8000;
1644 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1645 case SNDRV_MINOR_OSS_PCM_8:
1646 runtime->oss.format = AFMT_U8;
1648 case SNDRV_MINOR_OSS_PCM_16:
1649 runtime->oss.format = AFMT_S16_LE;
1652 runtime->oss.format = AFMT_MU_LAW;
1654 runtime->oss.channels = 1;
1655 runtime->oss.fragshift = 0;
1656 runtime->oss.maxfrags = 0;
1657 runtime->oss.subdivision = 0;
1660 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1662 snd_pcm_runtime_t *runtime;
1663 runtime = substream->runtime;
1664 if (runtime->oss.buffer)
1665 vfree(runtime->oss.buffer);
1666 snd_pcm_oss_plugin_clear(substream);
1667 substream->oss.file = NULL;
1668 substream->oss.oss = 0;
1671 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1674 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1675 for (cidx = 0; cidx < 2; ++cidx) {
1676 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1677 snd_pcm_runtime_t *runtime;
1678 if (substream == NULL)
1680 runtime = substream->runtime;
1682 snd_pcm_stream_lock_irq(substream);
1683 if (snd_pcm_running(substream))
1684 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1685 snd_pcm_stream_unlock_irq(substream);
1686 if (substream->open_flag) {
1687 if (substream->ops->hw_free != NULL)
1688 substream->ops->hw_free(substream);
1689 substream->ops->close(substream);
1690 substream->open_flag = 0;
1692 substream->ffile = NULL;
1693 snd_pcm_oss_release_substream(substream);
1694 snd_pcm_release_substream(substream);
1696 snd_magic_kfree(pcm_oss_file);
1700 static int snd_pcm_oss_open_file(struct file *file,
1702 snd_pcm_oss_file_t **rpcm_oss_file,
1704 snd_pcm_oss_setup_t *psetup,
1705 snd_pcm_oss_setup_t *csetup)
1708 snd_pcm_oss_file_t *pcm_oss_file;
1709 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1710 unsigned int f_mode = file->f_mode;
1712 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1713 *rpcm_oss_file = NULL;
1715 pcm_oss_file = snd_magic_kcalloc(snd_pcm_oss_file_t, 0, GFP_KERNEL);
1716 if (pcm_oss_file == NULL)
1719 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1720 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1721 f_mode = FMODE_WRITE;
1722 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1723 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1724 &psubstream)) < 0) {
1725 snd_pcm_oss_release_file(pcm_oss_file);
1728 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1730 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1731 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1732 &csubstream)) < 0) {
1733 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1734 snd_pcm_oss_release_file(pcm_oss_file);
1740 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1743 if (psubstream == NULL && csubstream == NULL) {
1744 snd_pcm_oss_release_file(pcm_oss_file);
1747 if (psubstream != NULL) {
1748 psubstream->oss.file = pcm_oss_file;
1749 err = snd_pcm_hw_constraints_init(psubstream);
1751 snd_printd("snd_pcm_hw_constraint_init failed\n");
1752 snd_pcm_oss_release_file(pcm_oss_file);
1755 if ((err = psubstream->ops->open(psubstream)) < 0) {
1756 snd_pcm_oss_release_file(pcm_oss_file);
1759 psubstream->open_flag = 1;
1760 err = snd_pcm_hw_constraints_complete(psubstream);
1762 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1763 snd_pcm_oss_release_file(pcm_oss_file);
1766 psubstream->ffile = file;
1767 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1769 if (csubstream != NULL) {
1770 csubstream->oss.file = pcm_oss_file;
1771 err = snd_pcm_hw_constraints_init(csubstream);
1773 snd_printd("snd_pcm_hw_constraint_init failed\n");
1774 snd_pcm_oss_release_file(pcm_oss_file);
1777 if ((err = csubstream->ops->open(csubstream)) < 0) {
1778 snd_pcm_oss_release_file(pcm_oss_file);
1781 csubstream->open_flag = 1;
1782 err = snd_pcm_hw_constraints_complete(csubstream);
1784 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1785 snd_pcm_oss_release_file(pcm_oss_file);
1788 csubstream->ffile = file;
1789 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1792 file->private_data = pcm_oss_file;
1793 *rpcm_oss_file = pcm_oss_file;
1798 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1800 int minor = iminor(inode);
1801 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1806 snd_pcm_oss_file_t *pcm_oss_file;
1807 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1811 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1812 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1813 adsp_map[cardnum] : dsp_map[cardnum];
1815 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1820 err = snd_card_file_add(pcm->card, file);
1823 if (!try_module_get(pcm->card->module)) {
1827 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1831 if (file->f_mode & FMODE_WRITE)
1832 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1833 if (file->f_mode & FMODE_READ)
1834 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1836 nonblock = !!(file->f_flags & O_NONBLOCK);
1837 if (psetup && !psetup->disable) {
1838 if (psetup->nonblock)
1840 else if (psetup->block)
1842 } else if (csetup && !csetup->disable) {
1843 if (csetup->nonblock)
1845 else if (csetup->block)
1849 nonblock = nonblock_open;
1851 init_waitqueue_entry(&wait, current);
1852 add_wait_queue(&pcm->open_wait, &wait);
1853 down(&pcm->open_mutex);
1855 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1856 minor, psetup, csetup);
1859 if (err == -EAGAIN) {
1866 set_current_state(TASK_INTERRUPTIBLE);
1867 up(&pcm->open_mutex);
1869 down(&pcm->open_mutex);
1870 if (signal_pending(current)) {
1875 remove_wait_queue(&pcm->open_wait, &wait);
1876 up(&pcm->open_mutex);
1882 module_put(pcm->card->module);
1884 snd_card_file_remove(pcm->card, file);
1889 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1892 snd_pcm_substream_t *substream;
1893 snd_pcm_oss_file_t *pcm_oss_file;
1895 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1896 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1897 if (substream == NULL)
1898 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1899 snd_assert(substream != NULL, return -ENXIO);
1900 pcm = substream->pcm;
1901 snd_pcm_oss_sync(pcm_oss_file);
1902 down(&pcm->open_mutex);
1903 snd_pcm_oss_release_file(pcm_oss_file);
1904 up(&pcm->open_mutex);
1905 wake_up(&pcm->open_wait);
1906 module_put(pcm->card->module);
1907 snd_card_file_remove(pcm->card, file);
1911 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1912 unsigned int cmd, unsigned long arg)
1914 snd_pcm_oss_file_t *pcm_oss_file;
1917 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1918 if (cmd == OSS_GETVERSION)
1919 return put_user(SNDRV_OSS_VERSION, (int *)arg);
1920 if (cmd == OSS_ALSAEMULVER)
1921 return put_user(1, (int *)arg);
1922 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1923 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1924 snd_pcm_substream_t *substream;
1926 for (idx = 0; idx < 2; ++idx) {
1927 substream = pcm_oss_file->streams[idx];
1928 if (substream != NULL)
1931 snd_assert(substream != NULL, return -ENXIO);
1932 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1935 if (((cmd >> 8) & 0xff) != 'P')
1938 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1941 case SNDCTL_DSP_RESET:
1942 return snd_pcm_oss_reset(pcm_oss_file);
1943 case SNDCTL_DSP_SYNC:
1944 return snd_pcm_oss_sync(pcm_oss_file);
1945 case SNDCTL_DSP_SPEED:
1946 if (get_user(res, (int *)arg))
1948 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1950 return put_user(res, (int *)arg);
1951 case SOUND_PCM_READ_RATE:
1952 res = snd_pcm_oss_get_rate(pcm_oss_file);
1955 return put_user(res, (int *)arg);
1956 case SNDCTL_DSP_STEREO:
1957 if (get_user(res, (int *)arg))
1959 res = res > 0 ? 2 : 1;
1960 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1962 return put_user(--res, (int *)arg);
1963 case SNDCTL_DSP_GETBLKSIZE:
1964 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1967 return put_user(res, (int *)arg);
1968 case SNDCTL_DSP_SETFMT:
1969 if (get_user(res, (int *)arg))
1971 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1974 return put_user(res, (int *)arg);
1975 case SOUND_PCM_READ_BITS:
1976 res = snd_pcm_oss_get_format(pcm_oss_file);
1979 return put_user(res, (int *)arg);
1980 case SNDCTL_DSP_CHANNELS:
1981 if (get_user(res, (int *)arg))
1983 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1986 return put_user(res, (int *)arg);
1987 case SOUND_PCM_READ_CHANNELS:
1988 res = snd_pcm_oss_get_channels(pcm_oss_file);
1991 return put_user(res, (int *)arg);
1992 case SOUND_PCM_WRITE_FILTER:
1993 case SOUND_PCM_READ_FILTER:
1995 case SNDCTL_DSP_POST:
1996 return snd_pcm_oss_post(pcm_oss_file);
1997 case SNDCTL_DSP_SUBDIVIDE:
1998 if (get_user(res, (int *)arg))
2000 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2003 return put_user(res, (int *)arg);
2004 case SNDCTL_DSP_SETFRAGMENT:
2005 if (get_user(res, (int *)arg))
2007 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2008 case SNDCTL_DSP_GETFMTS:
2009 res = snd_pcm_oss_get_formats(pcm_oss_file);
2012 return put_user(res, (int *)arg);
2013 case SNDCTL_DSP_GETOSPACE:
2014 case SNDCTL_DSP_GETISPACE:
2015 return snd_pcm_oss_get_space(pcm_oss_file,
2016 cmd == SNDCTL_DSP_GETISPACE ?
2017 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2018 (struct audio_buf_info *) arg);
2019 case SNDCTL_DSP_NONBLOCK:
2020 return snd_pcm_oss_nonblock(file);
2021 case SNDCTL_DSP_GETCAPS:
2022 res = snd_pcm_oss_get_caps(pcm_oss_file);
2025 return put_user(res, (int *)arg);
2026 case SNDCTL_DSP_GETTRIGGER:
2027 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2030 return put_user(res, (int *)arg);
2031 case SNDCTL_DSP_SETTRIGGER:
2032 if (get_user(res, (int *)arg))
2034 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2035 case SNDCTL_DSP_GETIPTR:
2036 case SNDCTL_DSP_GETOPTR:
2037 return snd_pcm_oss_get_ptr(pcm_oss_file,
2038 cmd == SNDCTL_DSP_GETIPTR ?
2039 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2040 (struct count_info *) arg);
2041 case SNDCTL_DSP_MAPINBUF:
2042 case SNDCTL_DSP_MAPOUTBUF:
2043 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2044 cmd == SNDCTL_DSP_MAPINBUF ?
2045 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2046 (struct buffmem_desc *) arg);
2047 case SNDCTL_DSP_SETSYNCRO:
2048 /* stop DMA now.. */
2050 case SNDCTL_DSP_SETDUPLEX:
2051 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2054 case SNDCTL_DSP_GETODELAY:
2055 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2057 /* it's for sure, some broken apps don't check for error codes */
2058 put_user(0, (int *)arg);
2061 return put_user(res, (int *)arg);
2062 case SNDCTL_DSP_PROFILE:
2063 return 0; /* silently ignore */
2065 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2070 static ssize_t snd_pcm_oss_read(struct file *file, char *buf, size_t count, loff_t *offset)
2072 snd_pcm_oss_file_t *pcm_oss_file;
2073 snd_pcm_substream_t *substream;
2075 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2076 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2077 if (substream == NULL)
2080 return snd_pcm_oss_read1(substream, buf, count);
2083 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2084 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2090 static ssize_t snd_pcm_oss_write(struct file *file, const char *buf, size_t count, loff_t *offset)
2092 snd_pcm_oss_file_t *pcm_oss_file;
2093 snd_pcm_substream_t *substream;
2096 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2097 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2098 if (substream == NULL)
2100 up(&file->f_dentry->d_inode->i_sem);
2101 result = snd_pcm_oss_write1(substream, buf, count);
2102 down(&file->f_dentry->d_inode->i_sem);
2104 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2109 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2111 snd_pcm_runtime_t *runtime = substream->runtime;
2112 if (atomic_read(&runtime->mmap_count))
2113 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2115 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2118 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2120 snd_pcm_runtime_t *runtime = substream->runtime;
2121 if (atomic_read(&runtime->mmap_count))
2122 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2124 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2127 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2129 snd_pcm_oss_file_t *pcm_oss_file;
2131 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2133 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return 0);
2135 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2136 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2139 if (psubstream != NULL) {
2140 snd_pcm_runtime_t *runtime = psubstream->runtime;
2141 poll_wait(file, &runtime->sleep, wait);
2142 snd_pcm_stream_lock_irq(psubstream);
2143 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2144 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2145 snd_pcm_oss_playback_ready(psubstream)))
2146 mask |= POLLOUT | POLLWRNORM;
2147 snd_pcm_stream_unlock_irq(psubstream);
2149 if (csubstream != NULL) {
2150 snd_pcm_runtime_t *runtime = csubstream->runtime;
2151 enum sndrv_pcm_state ostate;
2152 poll_wait(file, &runtime->sleep, wait);
2153 snd_pcm_stream_lock_irq(csubstream);
2154 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2155 snd_pcm_oss_capture_ready(csubstream))
2156 mask |= POLLIN | POLLRDNORM;
2157 snd_pcm_stream_unlock_irq(csubstream);
2158 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2159 snd_pcm_oss_file_t ofile;
2160 memset(&ofile, 0, sizeof(ofile));
2161 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2162 runtime->oss.trigger = 0;
2163 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2170 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2172 snd_pcm_oss_file_t *pcm_oss_file;
2173 snd_pcm_substream_t *substream = NULL;
2174 snd_pcm_runtime_t *runtime;
2178 printk("pcm_oss: mmap begin\n");
2180 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2181 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2182 case VM_READ | VM_WRITE:
2183 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2188 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2191 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2196 /* set VM_READ access as well to fix memset() routines that do
2197 reads before writes (to improve performance) */
2198 area->vm_flags |= VM_READ;
2199 if (substream == NULL)
2201 runtime = substream->runtime;
2202 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2204 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2205 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2209 if (runtime->oss.params) {
2210 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2213 if (runtime->oss.plugin_first != NULL)
2216 if (area->vm_pgoff != 0)
2219 err = snd_pcm_mmap_data(substream, file, area);
2222 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2223 runtime->silence_threshold = 0;
2224 runtime->silence_size = 0;
2226 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2228 /* In mmap mode we never stop */
2229 runtime->stop_threshold = runtime->boundary;
2238 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2239 snd_info_buffer_t * buffer)
2241 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2242 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2243 down(&pstr->oss.setup_mutex);
2245 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2249 setup->disable ? " disable" : "",
2250 setup->direct ? " direct" : "",
2251 setup->block ? " block" : "",
2252 setup->nonblock ? " non-block" : "",
2253 setup->partialfrag ? " partial-frag" : "",
2254 setup->nosilence ? " no-silence" : "");
2255 setup = setup->next;
2257 up(&pstr->oss.setup_mutex);
2260 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2263 snd_pcm_substream_t *substream;
2264 snd_pcm_oss_setup_t *setup, *setupn;
2266 for (idx = 0, substream = pstr->substream;
2267 idx < pstr->substream_count; idx++, substream = substream->next)
2268 substream->oss.setup = NULL;
2269 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2270 setup; setup = setupn) {
2271 setupn = setup->next;
2272 kfree(setup->task_name);
2275 pstr->oss.setup_list = NULL;
2278 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2279 snd_info_buffer_t * buffer)
2281 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2282 char line[512], str[32], task_name[32], *ptr;
2284 snd_pcm_oss_setup_t *setup, *setup1, template;
2286 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2287 down(&pstr->oss.setup_mutex);
2288 memset(&template, 0, sizeof(template));
2289 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2290 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2291 snd_pcm_oss_proc_free_setup_list(pstr);
2292 up(&pstr->oss.setup_mutex);
2295 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2296 if (!strcmp(setup->task_name, task_name)) {
2301 ptr = snd_info_get_str(str, ptr, sizeof(str));
2302 template.periods = simple_strtoul(str, NULL, 10);
2303 ptr = snd_info_get_str(str, ptr, sizeof(str));
2304 template.period_size = simple_strtoul(str, NULL, 10);
2305 for (idx1 = 31; idx1 >= 0; idx1--)
2306 if (template.period_size & (1 << idx1))
2308 for (idx1--; idx1 >= 0; idx1--)
2309 template.period_size &= ~(1 << idx1);
2311 ptr = snd_info_get_str(str, ptr, sizeof(str));
2312 if (!strcmp(str, "disable")) {
2313 template.disable = 1;
2314 } else if (!strcmp(str, "direct")) {
2315 template.direct = 1;
2316 } else if (!strcmp(str, "block")) {
2318 } else if (!strcmp(str, "non-block")) {
2319 template.nonblock = 1;
2320 } else if (!strcmp(str, "partial-frag")) {
2321 template.partialfrag = 1;
2322 } else if (!strcmp(str, "no-silence")) {
2323 template.nosilence = 1;
2326 if (setup == NULL) {
2327 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2329 if (pstr->oss.setup_list == NULL) {
2330 pstr->oss.setup_list = setup;
2332 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2333 setup1->next = setup;
2335 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2337 buffer->error = -ENOMEM;
2342 up(&pstr->oss.setup_mutex);
2346 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2349 for (stream = 0; stream < 2; ++stream) {
2350 snd_info_entry_t *entry;
2351 snd_pcm_str_t *pstr = &pcm->streams[stream];
2352 if (pstr->substream_count == 0)
2354 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2355 entry->content = SNDRV_INFO_CONTENT_TEXT;
2356 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2357 entry->c.text.read_size = 8192;
2358 entry->c.text.read = snd_pcm_oss_proc_read;
2359 entry->c.text.write_size = 8192;
2360 entry->c.text.write = snd_pcm_oss_proc_write;
2361 entry->private_data = pstr;
2362 if (snd_info_register(entry) < 0) {
2363 snd_info_free_entry(entry);
2367 pstr->oss.proc_entry = entry;
2371 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2374 for (stream = 0; stream < 2; ++stream) {
2375 snd_pcm_str_t *pstr = &pcm->streams[stream];
2376 if (pstr->oss.proc_entry) {
2377 snd_info_unregister(pstr->oss.proc_entry);
2378 pstr->oss.proc_entry = NULL;
2379 snd_pcm_oss_proc_free_setup_list(pstr);
2388 static struct file_operations snd_pcm_oss_f_reg =
2390 .owner = THIS_MODULE,
2391 .read = snd_pcm_oss_read,
2392 .write = snd_pcm_oss_write,
2393 .open = snd_pcm_oss_open,
2394 .release = snd_pcm_oss_release,
2395 .poll = snd_pcm_oss_poll,
2396 .ioctl = snd_pcm_oss_ioctl,
2397 .mmap = snd_pcm_oss_mmap,
2400 static snd_minor_t snd_pcm_oss_reg =
2402 .comment = "digital audio",
2403 .f_ops = &snd_pcm_oss_f_reg,
2406 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2409 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2410 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2411 pcm->card, index, &snd_pcm_oss_reg,
2413 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2417 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2420 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2423 register_oss_dsp(pcm, 0);
2424 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2425 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2426 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2427 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2428 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2429 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2434 pcm->oss.reg_mask |= 1;
2436 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2437 register_oss_dsp(pcm, 1);
2439 pcm->oss.reg_mask |= 2;
2443 snd_pcm_oss_proc_init(pcm);
2448 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2451 if (pcm->oss.reg_mask & 1) {
2452 pcm->oss.reg_mask &= ~1;
2453 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2456 if (pcm->oss.reg_mask & 2) {
2457 pcm->oss.reg_mask &= ~2;
2458 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2465 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2467 snd_pcm_oss_disconnect_minor(pcm);
2469 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2470 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2471 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2475 snd_pcm_oss_proc_done(pcm);
2480 static snd_pcm_notify_t snd_pcm_oss_notify =
2482 .n_register = snd_pcm_oss_register_minor,
2483 .n_disconnect = snd_pcm_oss_disconnect_minor,
2484 .n_unregister = snd_pcm_oss_unregister_minor,
2487 static int __init alsa_pcm_oss_init(void)
2492 /* check device map table */
2493 for (i = 0; i < SNDRV_CARDS; i++) {
2494 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2495 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2498 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2499 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2503 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2508 static void __exit alsa_pcm_oss_exit(void)
2510 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2513 module_init(alsa_pcm_oss_init)
2514 module_exit(alsa_pcm_oss_exit)