2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/vmalloc.h>
35 #include <linux/moduleparam.h>
36 #include <sound/core.h>
37 #include <sound/minors.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include "pcm_plugin.h"
41 #include <sound/info.h>
42 #include <linux/soundcard.h>
43 #include <sound/initval.h>
45 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
47 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
48 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
49 static int nonblock_open;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
52 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
53 MODULE_LICENSE("GPL");
54 module_param_array(dsp_map, int, NULL, 0444);
55 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
56 module_param_array(adsp_map, int, NULL, 0444);
57 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
58 module_param(nonblock_open, bool, 0644);
59 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
64 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
65 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
66 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
68 static inline mm_segment_t snd_enter_user(void)
70 mm_segment_t fs = get_fs();
75 static inline void snd_leave_user(mm_segment_t fs)
80 int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
82 snd_pcm_runtime_t *runtime = substream->runtime;
83 snd_pcm_plugin_t *plugin, *next;
85 plugin = runtime->oss.plugin_first;
88 snd_pcm_plugin_free(plugin);
91 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
95 int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
97 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
98 plugin->next = runtime->oss.plugin_first;
100 if (runtime->oss.plugin_first) {
101 runtime->oss.plugin_first->prev = plugin;
102 runtime->oss.plugin_first = plugin;
104 runtime->oss.plugin_last =
105 runtime->oss.plugin_first = plugin;
110 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
112 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
114 plugin->prev = runtime->oss.plugin_last;
115 if (runtime->oss.plugin_last) {
116 runtime->oss.plugin_last->next = plugin;
117 runtime->oss.plugin_last = plugin;
119 runtime->oss.plugin_last =
120 runtime->oss.plugin_first = plugin;
125 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
127 snd_pcm_runtime_t *runtime = substream->runtime;
128 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
129 frames = frames_to_bytes(runtime, frames);
130 if (buffer_size == runtime->oss.buffer_bytes)
132 return (runtime->oss.buffer_bytes * frames) / buffer_size;
135 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
137 snd_pcm_runtime_t *runtime = substream->runtime;
138 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
139 if (buffer_size == runtime->oss.buffer_bytes)
140 return bytes_to_frames(runtime, bytes);
141 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
144 static int snd_pcm_oss_format_from(int format)
147 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
148 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
149 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
150 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
151 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
152 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
153 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
154 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
155 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
156 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
157 default: return SNDRV_PCM_FORMAT_U8;
161 static int snd_pcm_oss_format_to(int format)
164 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
165 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
166 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
167 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
168 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
169 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
170 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
171 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
172 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
173 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
174 default: return -EINVAL;
178 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream,
179 snd_pcm_hw_params_t *oss_params,
180 snd_pcm_hw_params_t *slave_params)
183 size_t oss_buffer_size, oss_period_size, oss_periods;
184 size_t min_period_size, max_period_size;
185 snd_pcm_runtime_t *runtime = substream->runtime;
186 size_t oss_frame_size;
188 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
189 params_channels(oss_params) / 8;
191 oss_buffer_size = snd_pcm_plug_client_size(substream,
192 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
193 oss_buffer_size = 1 << ld2(oss_buffer_size);
194 if (atomic_read(&runtime->mmap_count)) {
195 if (oss_buffer_size > runtime->oss.mmap_bytes)
196 oss_buffer_size = runtime->oss.mmap_bytes;
199 if (substream->oss.setup &&
200 substream->oss.setup->period_size > 16)
201 oss_period_size = substream->oss.setup->period_size;
202 else if (runtime->oss.fragshift) {
203 oss_period_size = 1 << runtime->oss.fragshift;
204 if (oss_period_size > oss_buffer_size / 2)
205 oss_period_size = oss_buffer_size / 2;
208 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
210 oss_period_size = oss_buffer_size;
212 oss_period_size /= 2;
213 } while (oss_period_size > bytes_per_sec);
214 if (runtime->oss.subdivision == 0) {
216 if (oss_period_size / sd > 4096)
218 if (oss_period_size / sd < 4096)
221 sd = runtime->oss.subdivision;
222 oss_period_size /= sd;
223 if (oss_period_size < 16)
224 oss_period_size = 16;
227 min_period_size = snd_pcm_plug_client_size(substream,
228 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
229 min_period_size *= oss_frame_size;
230 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
231 if (oss_period_size < min_period_size)
232 oss_period_size = min_period_size;
234 max_period_size = snd_pcm_plug_client_size(substream,
235 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
236 max_period_size *= oss_frame_size;
237 max_period_size = 1 << ld2(max_period_size);
238 if (oss_period_size > max_period_size)
239 oss_period_size = max_period_size;
241 oss_periods = oss_buffer_size / oss_period_size;
243 if (substream->oss.setup) {
244 if (substream->oss.setup->periods > 1)
245 oss_periods = substream->oss.setup->periods;
248 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
249 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
250 s = runtime->oss.maxfrags;
254 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
260 while (oss_period_size * oss_periods > oss_buffer_size)
261 oss_period_size /= 2;
263 snd_assert(oss_period_size >= 16, return -EINVAL);
264 runtime->oss.period_bytes = oss_period_size;
265 runtime->oss.period_frames = 1;
266 runtime->oss.periods = oss_periods;
270 static int choose_rate(snd_pcm_substream_t *substream,
271 snd_pcm_hw_params_t *params, unsigned int best_rate)
274 snd_pcm_hw_params_t *save;
275 unsigned int rate, prev;
277 save = kmalloc(sizeof(*save), GFP_KERNEL);
281 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
283 /* try multiples of the best rate */
286 if (it->max < rate || (it->max == rate && it->openmax))
288 if (it->min < rate || (it->min == rate && !it->openmin)) {
290 ret = snd_pcm_hw_param_set(substream, params,
291 SNDRV_PCM_HW_PARAM_RATE,
293 if (ret == (int)rate) {
305 /* not found, use the nearest rate */
307 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
310 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
312 snd_pcm_runtime_t *runtime = substream->runtime;
313 snd_pcm_hw_params_t *params, *sparams;
314 snd_pcm_sw_params_t *sw_params;
315 ssize_t oss_buffer_size, oss_period_size;
316 size_t oss_frame_size;
319 int format, sformat, n;
320 snd_mask_t sformat_mask;
323 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
324 params = kmalloc(sizeof(*params), GFP_KERNEL);
325 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
326 if (!sw_params || !params || !sparams) {
327 snd_printd("No memory\n");
332 if (atomic_read(&runtime->mmap_count)) {
335 snd_pcm_oss_setup_t *setup = substream->oss.setup;
336 direct = (setup != NULL && setup->direct);
339 _snd_pcm_hw_params_any(sparams);
340 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
341 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
342 snd_mask_none(&mask);
343 if (atomic_read(&runtime->mmap_count))
344 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
346 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
348 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
350 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
352 snd_printd("No usable accesses\n");
356 choose_rate(substream, sparams, runtime->oss.rate);
357 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
359 format = snd_pcm_oss_format_from(runtime->oss.format);
361 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
365 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
367 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
368 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
369 if (snd_mask_test(&sformat_mask, sformat) &&
370 snd_pcm_oss_format_to(sformat) >= 0)
373 if (sformat > SNDRV_PCM_FORMAT_LAST) {
374 snd_printd("Cannot find a format!!!\n");
379 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
380 snd_assert(err >= 0, goto failure);
383 memcpy(params, sparams, sizeof(*params));
385 _snd_pcm_hw_params_any(params);
386 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
387 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
388 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
389 snd_pcm_oss_format_from(runtime->oss.format), 0);
390 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
391 runtime->oss.channels, 0);
392 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
393 runtime->oss.rate, 0);
394 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
395 params_access(params), params_format(params),
396 params_channels(params), params_rate(params));
398 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
399 params_access(sparams), params_format(sparams),
400 params_channels(sparams), params_rate(sparams));
402 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
403 params_channels(params) / 8;
405 snd_pcm_oss_plugin_clear(substream);
407 /* add necessary plugins */
408 snd_pcm_oss_plugin_clear(substream);
409 if ((err = snd_pcm_plug_format_plugins(substream,
412 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
413 snd_pcm_oss_plugin_clear(substream);
416 if (runtime->oss.plugin_first) {
417 snd_pcm_plugin_t *plugin;
418 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
419 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
420 snd_pcm_oss_plugin_clear(substream);
423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
424 err = snd_pcm_plugin_append(plugin);
426 err = snd_pcm_plugin_insert(plugin);
429 snd_pcm_oss_plugin_clear(substream);
435 err = snd_pcm_oss_period_size(substream, params, sparams);
439 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
440 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
441 snd_assert(err >= 0, goto failure);
443 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
444 runtime->oss.periods, NULL);
445 snd_assert(err >= 0, goto failure);
447 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
449 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
450 snd_printd("HW_PARAMS failed: %i\n", err);
454 memset(sw_params, 0, sizeof(*sw_params));
455 if (runtime->oss.trigger) {
456 sw_params->start_threshold = 1;
458 sw_params->start_threshold = runtime->boundary;
460 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
461 sw_params->stop_threshold = runtime->boundary;
463 sw_params->stop_threshold = runtime->buffer_size;
464 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
465 sw_params->period_step = 1;
466 sw_params->sleep_min = 0;
467 sw_params->avail_min = 1;
468 sw_params->xfer_align = 1;
469 if (atomic_read(&runtime->mmap_count) ||
470 (substream->oss.setup && substream->oss.setup->nosilence)) {
471 sw_params->silence_threshold = 0;
472 sw_params->silence_size = 0;
474 snd_pcm_uframes_t frames;
475 frames = runtime->period_size + 16;
476 if (frames > runtime->buffer_size)
477 frames = runtime->buffer_size;
478 sw_params->silence_threshold = frames;
479 sw_params->silence_size = frames;
482 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
483 snd_printd("SW_PARAMS failed: %i\n", err);
487 runtime->oss.periods = params_periods(sparams);
488 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
489 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
490 if (runtime->oss.plugin_first) {
491 err = snd_pcm_plug_alloc(substream, oss_period_size);
495 oss_period_size *= oss_frame_size;
497 oss_buffer_size = oss_period_size * runtime->oss.periods;
498 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
500 runtime->oss.period_bytes = oss_period_size;
501 runtime->oss.buffer_bytes = oss_buffer_size;
503 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
504 runtime->oss.period_bytes,
505 runtime->oss.buffer_bytes);
506 pdprintf("slave: period_size = %i, buffer_size = %i\n",
507 params_period_size(sparams),
508 params_buffer_size(sparams));
510 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
511 runtime->oss.channels = params_channels(params);
512 runtime->oss.rate = params_rate(params);
514 runtime->oss.params = 0;
515 runtime->oss.prepare = 1;
516 if (runtime->oss.buffer != NULL)
517 vfree(runtime->oss.buffer);
518 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
519 runtime->oss.buffer_used = 0;
520 if (runtime->dma_area)
521 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
523 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
536 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
539 snd_pcm_substream_t *asubstream = NULL, *substream;
541 for (idx = 0; idx < 2; idx++) {
542 substream = pcm_oss_file->streams[idx];
543 if (substream == NULL)
545 if (asubstream == NULL)
546 asubstream = substream;
547 if (substream->runtime->oss.params) {
548 err = snd_pcm_oss_change_params(substream);
553 snd_assert(asubstream != NULL, return -EIO);
555 *r_substream = asubstream;
559 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
562 snd_pcm_runtime_t *runtime = substream->runtime;
564 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
566 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
569 runtime->oss.prepare = 0;
570 runtime->oss.prev_hw_ptr_interrupt = 0;
571 runtime->oss.period_ptr = 0;
572 runtime->oss.buffer_used = 0;
577 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
579 snd_pcm_runtime_t *runtime;
582 if (substream == NULL)
584 runtime = substream->runtime;
585 if (runtime->oss.params) {
586 err = snd_pcm_oss_change_params(substream);
590 if (runtime->oss.prepare) {
591 err = snd_pcm_oss_prepare(substream);
598 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
600 snd_pcm_runtime_t *runtime;
601 snd_pcm_uframes_t frames;
605 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
608 runtime = substream->runtime;
609 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
611 /* in case of overrun, skip whole periods like OSS/Linux driver does */
612 /* until avail(delay) <= buffer_size */
613 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
614 frames /= runtime->period_size;
615 frames *= runtime->period_size;
616 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
623 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
625 snd_pcm_runtime_t *runtime = substream->runtime;
628 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
629 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
631 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
632 printk("pcm_oss: write: recovering from XRUN\n");
634 printk("pcm_oss: write: recovering from SUSPEND\n");
636 ret = snd_pcm_oss_prepare(substream);
642 fs = snd_enter_user();
643 ret = snd_pcm_lib_write(substream, ptr, frames);
646 ret = snd_pcm_lib_write(substream, ptr, frames);
648 if (ret != -EPIPE && ret != -ESTRPIPE)
650 /* test, if we can't store new data, because the stream */
651 /* has not been started */
652 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
658 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
660 snd_pcm_runtime_t *runtime = substream->runtime;
661 snd_pcm_sframes_t delay;
664 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
665 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
667 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
668 printk("pcm_oss: read: recovering from XRUN\n");
670 printk("pcm_oss: read: recovering from SUSPEND\n");
672 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
675 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
676 ret = snd_pcm_oss_prepare(substream);
680 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
685 fs = snd_enter_user();
686 ret = snd_pcm_lib_read(substream, ptr, frames);
689 ret = snd_pcm_lib_read(substream, ptr, frames);
692 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
693 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
699 if (ret != -ESTRPIPE)
705 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
707 snd_pcm_runtime_t *runtime = substream->runtime;
710 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
711 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
713 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
714 printk("pcm_oss: writev: recovering from XRUN\n");
716 printk("pcm_oss: writev: recovering from SUSPEND\n");
718 ret = snd_pcm_oss_prepare(substream);
724 fs = snd_enter_user();
725 ret = snd_pcm_lib_writev(substream, bufs, frames);
728 ret = snd_pcm_lib_writev(substream, bufs, frames);
730 if (ret != -EPIPE && ret != -ESTRPIPE)
733 /* test, if we can't store new data, because the stream */
734 /* has not been started */
735 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
741 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
743 snd_pcm_runtime_t *runtime = substream->runtime;
746 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
747 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
749 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
750 printk("pcm_oss: readv: recovering from XRUN\n");
752 printk("pcm_oss: readv: recovering from SUSPEND\n");
754 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
757 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
758 ret = snd_pcm_oss_prepare(substream);
764 fs = snd_enter_user();
765 ret = snd_pcm_lib_readv(substream, bufs, frames);
768 ret = snd_pcm_lib_readv(substream, bufs, frames);
770 if (ret != -EPIPE && ret != -ESTRPIPE)
776 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
778 snd_pcm_runtime_t *runtime = substream->runtime;
779 snd_pcm_sframes_t frames, frames1;
780 if (runtime->oss.plugin_first) {
781 snd_pcm_plugin_channel_t *channels;
782 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
784 if (copy_from_user(runtime->oss.buffer, buf, bytes))
786 buf = runtime->oss.buffer;
788 frames = bytes / oss_frame_bytes;
789 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
792 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
795 bytes = frames1 * oss_frame_bytes;
797 frames = bytes_to_frames(runtime, bytes);
798 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
801 bytes = frames_to_bytes(runtime, frames1);
806 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char __user *buf, size_t bytes)
810 snd_pcm_runtime_t *runtime = substream->runtime;
812 if (atomic_read(&runtime->mmap_count))
815 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
818 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
820 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
821 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
823 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
824 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
826 runtime->oss.buffer_used += tmp;
830 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
831 runtime->oss.buffer_used == runtime->oss.period_bytes) {
832 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
833 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
835 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
836 runtime->oss.bytes += tmp;
837 runtime->oss.period_ptr += tmp;
838 runtime->oss.period_ptr %= runtime->oss.period_bytes;
839 if (runtime->oss.period_ptr == 0 ||
840 runtime->oss.period_ptr == runtime->oss.buffer_used)
841 runtime->oss.buffer_used = 0;
842 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
843 return xfer > 0 ? xfer : -EAGAIN;
846 tmp = snd_pcm_oss_write2(substream, (char *)buf, runtime->oss.period_bytes, 0);
848 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
849 runtime->oss.bytes += tmp;
853 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
854 tmp != runtime->oss.period_bytes)
861 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
863 snd_pcm_runtime_t *runtime = substream->runtime;
864 snd_pcm_sframes_t frames, frames1;
865 char *final_dst = buf;
866 if (runtime->oss.plugin_first) {
867 snd_pcm_plugin_channel_t *channels;
868 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
870 buf = runtime->oss.buffer;
871 frames = bytes / oss_frame_bytes;
872 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
875 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
878 bytes = frames1 * oss_frame_bytes;
879 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
882 frames = bytes_to_frames(runtime, bytes);
883 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
886 bytes = frames_to_bytes(runtime, frames1);
891 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char __user *buf, size_t bytes)
895 snd_pcm_runtime_t *runtime = substream->runtime;
897 if (atomic_read(&runtime->mmap_count))
900 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
903 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
904 if (runtime->oss.buffer_used == 0) {
905 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
907 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
908 runtime->oss.bytes += tmp;
909 runtime->oss.period_ptr = tmp;
910 runtime->oss.buffer_used = tmp;
913 if ((size_t) tmp > runtime->oss.buffer_used)
914 tmp = runtime->oss.buffer_used;
915 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
916 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
920 runtime->oss.buffer_used -= tmp;
922 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
924 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
925 runtime->oss.bytes += tmp;
934 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
936 snd_pcm_substream_t *substream;
938 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
939 if (substream != NULL) {
940 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
941 substream->runtime->oss.prepare = 1;
943 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
944 if (substream != NULL) {
945 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
946 substream->runtime->oss.prepare = 1;
951 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
953 snd_pcm_substream_t *substream;
956 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
957 if (substream != NULL) {
958 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
960 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
962 /* note: all errors from the start action are ignored */
963 /* OSS apps do not know, how to handle them */
967 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
969 snd_pcm_runtime_t *runtime;
974 runtime = substream->runtime;
975 init_waitqueue_entry(&wait, current);
976 add_wait_queue(&runtime->sleep, &wait);
978 printk("sync1: size = %li\n", size);
981 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
983 runtime->oss.buffer_used = 0;
987 if (result != 0 && result != -EAGAIN)
990 set_current_state(TASK_INTERRUPTIBLE);
991 snd_pcm_stream_lock_irq(substream);
992 res = runtime->status->state;
993 snd_pcm_stream_unlock_irq(substream);
994 if (res != SNDRV_PCM_STATE_RUNNING) {
995 set_current_state(TASK_RUNNING);
998 res = schedule_timeout(10 * HZ);
999 if (signal_pending(current)) {
1000 result = -ERESTARTSYS;
1004 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1009 remove_wait_queue(&runtime->sleep, &wait);
1013 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1016 unsigned int saved_f_flags;
1017 snd_pcm_substream_t *substream;
1018 snd_pcm_runtime_t *runtime;
1019 snd_pcm_format_t format;
1020 unsigned long width;
1023 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1024 if (substream != NULL) {
1025 runtime = substream->runtime;
1026 if (atomic_read(&runtime->mmap_count))
1028 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1030 format = snd_pcm_oss_format_from(runtime->oss.format);
1031 width = snd_pcm_format_physical_width(format);
1032 if (runtime->oss.buffer_used > 0) {
1034 printk("sync: buffer_used\n");
1036 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1037 snd_pcm_format_set_silence(format,
1038 runtime->oss.buffer + runtime->oss.buffer_used,
1040 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1043 } else if (runtime->oss.period_ptr > 0) {
1045 printk("sync: period_ptr\n");
1047 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1048 snd_pcm_format_set_silence(format,
1049 runtime->oss.buffer,
1051 err = snd_pcm_oss_sync1(substream, size);
1056 * The ALSA's period might be a bit large than OSS one.
1057 * Fill the remain portion of ALSA period with zeros.
1059 size = runtime->control->appl_ptr % runtime->period_size;
1061 size = runtime->period_size - size;
1062 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1063 size = (runtime->frame_bits * size) / 8;
1065 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1068 size1 /= runtime->sample_bits;
1069 snd_pcm_format_set_silence(runtime->format,
1070 runtime->oss.buffer,
1072 snd_pcm_lib_write(substream, runtime->oss.buffer, size1);
1074 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1075 void __user *buffers[runtime->channels];
1076 memset(buffers, 0, runtime->channels * sizeof(void *));
1077 snd_pcm_lib_writev(substream, buffers, size);
1081 * finish sync: drain the buffer
1084 saved_f_flags = substream->ffile->f_flags;
1085 substream->ffile->f_flags &= ~O_NONBLOCK;
1086 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1087 substream->ffile->f_flags = saved_f_flags;
1090 runtime->oss.prepare = 1;
1093 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1094 if (substream != NULL) {
1095 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1097 runtime = substream->runtime;
1098 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1101 runtime->oss.buffer_used = 0;
1102 runtime->oss.prepare = 1;
1107 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1111 for (idx = 1; idx >= 0; --idx) {
1112 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1113 snd_pcm_runtime_t *runtime;
1114 if (substream == NULL)
1116 runtime = substream->runtime;
1119 else if (rate > 192000)
1121 if (runtime->oss.rate != rate) {
1122 runtime->oss.params = 1;
1123 runtime->oss.rate = rate;
1126 return snd_pcm_oss_get_rate(pcm_oss_file);
1129 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1131 snd_pcm_substream_t *substream;
1134 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1136 return substream->runtime->oss.rate;
1139 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1146 for (idx = 1; idx >= 0; --idx) {
1147 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1148 snd_pcm_runtime_t *runtime;
1149 if (substream == NULL)
1151 runtime = substream->runtime;
1152 if (runtime->oss.channels != channels) {
1153 runtime->oss.params = 1;
1154 runtime->oss.channels = channels;
1157 return snd_pcm_oss_get_channels(pcm_oss_file);
1160 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1162 snd_pcm_substream_t *substream;
1165 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1167 return substream->runtime->oss.channels;
1170 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1172 snd_pcm_substream_t *substream;
1175 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1177 return substream->runtime->oss.period_bytes;
1180 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1182 snd_pcm_substream_t *substream;
1185 snd_pcm_hw_params_t *params;
1186 unsigned int formats = 0;
1187 snd_mask_t format_mask;
1190 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1192 if (atomic_read(&substream->runtime->mmap_count)) {
1195 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1196 direct = (setup != NULL && setup->direct);
1199 return AFMT_MU_LAW | AFMT_U8 |
1200 AFMT_S16_LE | AFMT_S16_BE |
1201 AFMT_S8 | AFMT_U16_LE |
1203 params = kmalloc(sizeof(*params), GFP_KERNEL);
1206 _snd_pcm_hw_params_any(params);
1207 err = snd_pcm_hw_refine(substream, params);
1208 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1210 snd_assert(err >= 0, return err);
1211 for (fmt = 0; fmt < 32; ++fmt) {
1212 if (snd_mask_test(&format_mask, fmt)) {
1213 int f = snd_pcm_oss_format_to(fmt);
1221 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1225 if (format != AFMT_QUERY) {
1226 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1227 if (!(formats & format))
1229 for (idx = 1; idx >= 0; --idx) {
1230 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1231 snd_pcm_runtime_t *runtime;
1232 if (substream == NULL)
1234 runtime = substream->runtime;
1235 if (runtime->oss.format != format) {
1236 runtime->oss.params = 1;
1237 runtime->oss.format = format;
1241 return snd_pcm_oss_get_format(pcm_oss_file);
1244 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1246 snd_pcm_substream_t *substream;
1249 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1251 return substream->runtime->oss.format;
1254 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1256 snd_pcm_runtime_t *runtime;
1258 if (substream == NULL)
1260 runtime = substream->runtime;
1261 if (subdivide == 0) {
1262 subdivide = runtime->oss.subdivision;
1267 if (runtime->oss.subdivision || runtime->oss.fragshift)
1269 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1270 subdivide != 8 && subdivide != 16)
1272 runtime->oss.subdivision = subdivide;
1273 runtime->oss.params = 1;
1277 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1279 int err = -EINVAL, idx;
1281 for (idx = 1; idx >= 0; --idx) {
1282 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1283 if (substream == NULL)
1285 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1291 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1293 snd_pcm_runtime_t *runtime;
1295 if (substream == NULL)
1297 runtime = substream->runtime;
1298 if (runtime->oss.subdivision || runtime->oss.fragshift)
1300 runtime->oss.fragshift = val & 0xffff;
1301 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1302 if (runtime->oss.fragshift < 4) /* < 16 */
1303 runtime->oss.fragshift = 4;
1304 if (runtime->oss.maxfrags < 2)
1305 runtime->oss.maxfrags = 2;
1306 runtime->oss.params = 1;
1310 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1312 int err = -EINVAL, idx;
1314 for (idx = 1; idx >= 0; --idx) {
1315 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1316 if (substream == NULL)
1318 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1324 static int snd_pcm_oss_nonblock(struct file * file)
1326 file->f_flags |= O_NONBLOCK;
1330 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1333 if (substream == NULL) {
1334 res &= ~DSP_CAP_DUPLEX;
1337 #ifdef DSP_CAP_MULTI
1338 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1339 if (substream->pstr->substream_count > 1)
1340 res |= DSP_CAP_MULTI;
1342 /* DSP_CAP_REALTIME is set all times: */
1343 /* all ALSA drivers can return actual pointer in ring buffer */
1344 #if defined(DSP_CAP_REALTIME) && 0
1346 snd_pcm_runtime_t *runtime = substream->runtime;
1347 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1348 res &= ~DSP_CAP_REALTIME;
1354 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1358 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1359 for (idx = 0; idx < 2; idx++) {
1360 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1361 result = snd_pcm_oss_get_caps1(substream, result);
1363 result |= 0x0001; /* revision - same as SB AWE 64 */
1367 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1369 snd_pcm_runtime_t *runtime = substream->runtime;
1370 snd_pcm_uframes_t appl_ptr;
1371 appl_ptr = hw_ptr + runtime->buffer_size;
1372 appl_ptr %= runtime->boundary;
1373 runtime->control->appl_ptr = appl_ptr;
1376 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1378 snd_pcm_runtime_t *runtime;
1379 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1383 printk("pcm_oss: trigger = 0x%x\n", trigger);
1386 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1387 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1390 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1394 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1398 runtime = psubstream->runtime;
1399 if (trigger & PCM_ENABLE_OUTPUT) {
1400 if (runtime->oss.trigger)
1402 if (atomic_read(&psubstream->runtime->mmap_count))
1403 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1404 runtime->oss.trigger = 1;
1405 runtime->start_threshold = 1;
1406 cmd = SNDRV_PCM_IOCTL_START;
1408 if (!runtime->oss.trigger)
1410 runtime->oss.trigger = 0;
1411 runtime->start_threshold = runtime->boundary;
1412 cmd = SNDRV_PCM_IOCTL_DROP;
1413 runtime->oss.prepare = 1;
1415 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, NULL);
1421 runtime = csubstream->runtime;
1422 if (trigger & PCM_ENABLE_INPUT) {
1423 if (runtime->oss.trigger)
1425 runtime->oss.trigger = 1;
1426 runtime->start_threshold = 1;
1427 cmd = SNDRV_PCM_IOCTL_START;
1429 if (!runtime->oss.trigger)
1431 runtime->oss.trigger = 0;
1432 runtime->start_threshold = runtime->boundary;
1433 cmd = SNDRV_PCM_IOCTL_DROP;
1434 runtime->oss.prepare = 1;
1436 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1444 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1446 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1449 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1450 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1451 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1452 result |= PCM_ENABLE_OUTPUT;
1453 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1454 result |= PCM_ENABLE_INPUT;
1458 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1460 snd_pcm_substream_t *substream;
1461 snd_pcm_runtime_t *runtime;
1462 snd_pcm_sframes_t delay;
1465 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1466 if (substream == NULL)
1468 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1470 runtime = substream->runtime;
1471 if (runtime->oss.params || runtime->oss.prepare)
1473 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1475 delay = 0; /* hack for broken OSS applications */
1478 return snd_pcm_oss_bytes(substream, delay);
1481 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info __user * _info)
1483 snd_pcm_substream_t *substream;
1484 snd_pcm_runtime_t *runtime;
1485 snd_pcm_sframes_t delay;
1487 struct count_info info;
1492 substream = pcm_oss_file->streams[stream];
1493 if (substream == NULL)
1495 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1497 runtime = substream->runtime;
1498 if (runtime->oss.params || runtime->oss.prepare) {
1499 memset(&info, 0, sizeof(info));
1500 if (copy_to_user(_info, &info, sizeof(info)))
1504 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1505 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1506 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1511 fixup = runtime->oss.buffer_used;
1514 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1515 fixup = -runtime->oss.buffer_used;
1519 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1520 if (atomic_read(&runtime->mmap_count)) {
1521 snd_pcm_sframes_t n;
1522 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1524 n += runtime->boundary;
1525 info.blocks = n / runtime->period_size;
1526 runtime->oss.prev_hw_ptr_interrupt = delay;
1527 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1528 snd_pcm_oss_simulate_fill(substream, delay);
1529 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1531 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1532 info.blocks = delay / runtime->oss.period_bytes;
1533 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1534 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1536 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1538 if (copy_to_user(_info, &info, sizeof(info)))
1543 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1545 snd_pcm_substream_t *substream;
1546 snd_pcm_runtime_t *runtime;
1547 snd_pcm_sframes_t avail;
1549 struct audio_buf_info info;
1554 substream = pcm_oss_file->streams[stream];
1555 if (substream == NULL)
1557 runtime = substream->runtime;
1559 if (runtime->oss.params &&
1560 (err = snd_pcm_oss_change_params(substream)) < 0)
1563 info.fragsize = runtime->oss.period_bytes;
1564 info.fragstotal = runtime->periods;
1565 if (runtime->oss.prepare) {
1566 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1567 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1568 info.fragments = runtime->oss.periods;
1574 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1575 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1576 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1577 avail = runtime->buffer_size;
1581 avail = runtime->buffer_size - avail;
1582 fixup = -runtime->oss.buffer_used;
1585 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1586 fixup = runtime->oss.buffer_used;
1590 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1591 info.fragments = info.bytes / runtime->oss.period_bytes;
1595 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1597 if (copy_to_user(_info, &info, sizeof(info)))
1602 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1604 // it won't be probably implemented
1605 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1609 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1611 const char *ptr, *ptrl;
1612 snd_pcm_oss_setup_t *setup;
1614 down(&pcm->streams[stream].oss.setup_mutex);
1615 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1616 if (!strcmp(setup->task_name, task_name)) {
1617 up(&pcm->streams[stream].oss.setup_mutex);
1621 ptr = ptrl = task_name;
1627 if (ptrl == task_name) {
1631 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1632 if (!strcmp(setup->task_name, ptrl)) {
1633 up(&pcm->streams[stream].oss.setup_mutex);
1638 up(&pcm->streams[stream].oss.setup_mutex);
1642 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1643 snd_pcm_oss_setup_t *setup,
1646 snd_pcm_runtime_t *runtime;
1648 substream->oss.oss = 1;
1649 substream->oss.setup = setup;
1650 runtime = substream->runtime;
1651 runtime->oss.params = 1;
1652 runtime->oss.trigger = 1;
1653 runtime->oss.rate = 8000;
1654 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1655 case SNDRV_MINOR_OSS_PCM_8:
1656 runtime->oss.format = AFMT_U8;
1658 case SNDRV_MINOR_OSS_PCM_16:
1659 runtime->oss.format = AFMT_S16_LE;
1662 runtime->oss.format = AFMT_MU_LAW;
1664 runtime->oss.channels = 1;
1665 runtime->oss.fragshift = 0;
1666 runtime->oss.maxfrags = 0;
1667 runtime->oss.subdivision = 0;
1670 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1672 snd_pcm_runtime_t *runtime;
1673 runtime = substream->runtime;
1674 if (runtime->oss.buffer)
1675 vfree(runtime->oss.buffer);
1676 snd_pcm_oss_plugin_clear(substream);
1677 substream->oss.file = NULL;
1678 substream->oss.oss = 0;
1681 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1684 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1685 for (cidx = 0; cidx < 2; ++cidx) {
1686 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1687 snd_pcm_runtime_t *runtime;
1688 if (substream == NULL)
1690 runtime = substream->runtime;
1692 snd_pcm_stream_lock_irq(substream);
1693 if (snd_pcm_running(substream))
1694 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1695 snd_pcm_stream_unlock_irq(substream);
1696 if (substream->open_flag) {
1697 if (substream->ops->hw_free != NULL)
1698 substream->ops->hw_free(substream);
1699 substream->ops->close(substream);
1700 substream->open_flag = 0;
1702 substream->ffile = NULL;
1703 snd_pcm_oss_release_substream(substream);
1704 snd_pcm_release_substream(substream);
1706 kfree(pcm_oss_file);
1710 static int snd_pcm_oss_open_file(struct file *file,
1712 snd_pcm_oss_file_t **rpcm_oss_file,
1714 snd_pcm_oss_setup_t *psetup,
1715 snd_pcm_oss_setup_t *csetup)
1718 snd_pcm_oss_file_t *pcm_oss_file;
1719 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1720 unsigned int f_mode = file->f_mode;
1722 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1723 *rpcm_oss_file = NULL;
1725 pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1726 if (pcm_oss_file == NULL)
1729 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1730 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1731 f_mode = FMODE_WRITE;
1732 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1733 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1734 &psubstream)) < 0) {
1735 snd_pcm_oss_release_file(pcm_oss_file);
1738 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1740 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1741 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1742 &csubstream)) < 0) {
1743 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1744 snd_pcm_oss_release_file(pcm_oss_file);
1750 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1753 if (psubstream == NULL && csubstream == NULL) {
1754 snd_pcm_oss_release_file(pcm_oss_file);
1757 if (psubstream != NULL) {
1758 psubstream->oss.file = pcm_oss_file;
1759 err = snd_pcm_hw_constraints_init(psubstream);
1761 snd_printd("snd_pcm_hw_constraint_init failed\n");
1762 snd_pcm_oss_release_file(pcm_oss_file);
1765 if ((err = psubstream->ops->open(psubstream)) < 0) {
1766 snd_pcm_oss_release_file(pcm_oss_file);
1769 psubstream->open_flag = 1;
1770 err = snd_pcm_hw_constraints_complete(psubstream);
1772 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1773 snd_pcm_oss_release_file(pcm_oss_file);
1776 psubstream->ffile = file;
1777 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1779 if (csubstream != NULL) {
1780 csubstream->oss.file = pcm_oss_file;
1781 err = snd_pcm_hw_constraints_init(csubstream);
1783 snd_printd("snd_pcm_hw_constraint_init failed\n");
1784 snd_pcm_oss_release_file(pcm_oss_file);
1787 if ((err = csubstream->ops->open(csubstream)) < 0) {
1788 snd_pcm_oss_release_file(pcm_oss_file);
1791 csubstream->open_flag = 1;
1792 err = snd_pcm_hw_constraints_complete(csubstream);
1794 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1795 snd_pcm_oss_release_file(pcm_oss_file);
1798 csubstream->ffile = file;
1799 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1802 file->private_data = pcm_oss_file;
1803 *rpcm_oss_file = pcm_oss_file;
1808 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1810 int minor = iminor(inode);
1811 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1816 snd_pcm_oss_file_t *pcm_oss_file;
1817 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1820 static char printed_comm[16];
1822 if (strncmp(printed_comm, current->comm, 16)) {
1823 printk(KERN_DEBUG "application %s uses obsolete OSS audio interface\n",
1825 memcpy(printed_comm, current->comm, 16);
1828 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1829 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1830 adsp_map[cardnum] : dsp_map[cardnum];
1832 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1837 err = snd_card_file_add(pcm->card, file);
1840 if (!try_module_get(pcm->card->module)) {
1844 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1848 if (file->f_mode & FMODE_WRITE)
1849 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1850 if (file->f_mode & FMODE_READ)
1851 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1853 nonblock = !!(file->f_flags & O_NONBLOCK);
1854 if (psetup && !psetup->disable) {
1855 if (psetup->nonblock)
1857 else if (psetup->block)
1859 } else if (csetup && !csetup->disable) {
1860 if (csetup->nonblock)
1862 else if (csetup->block)
1866 nonblock = nonblock_open;
1868 init_waitqueue_entry(&wait, current);
1869 add_wait_queue(&pcm->open_wait, &wait);
1870 down(&pcm->open_mutex);
1872 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1873 minor, psetup, csetup);
1876 if (err == -EAGAIN) {
1883 set_current_state(TASK_INTERRUPTIBLE);
1884 up(&pcm->open_mutex);
1886 down(&pcm->open_mutex);
1887 if (signal_pending(current)) {
1892 remove_wait_queue(&pcm->open_wait, &wait);
1893 up(&pcm->open_mutex);
1899 module_put(pcm->card->module);
1901 snd_card_file_remove(pcm->card, file);
1906 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1909 snd_pcm_substream_t *substream;
1910 snd_pcm_oss_file_t *pcm_oss_file;
1912 pcm_oss_file = file->private_data;
1913 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1914 if (substream == NULL)
1915 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1916 snd_assert(substream != NULL, return -ENXIO);
1917 pcm = substream->pcm;
1918 snd_pcm_oss_sync(pcm_oss_file);
1919 down(&pcm->open_mutex);
1920 snd_pcm_oss_release_file(pcm_oss_file);
1921 up(&pcm->open_mutex);
1922 wake_up(&pcm->open_wait);
1923 module_put(pcm->card->module);
1924 snd_card_file_remove(pcm->card, file);
1928 static inline int _snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1929 unsigned int cmd, unsigned long arg)
1931 snd_pcm_oss_file_t *pcm_oss_file;
1932 int __user *p = (int __user *)arg;
1935 pcm_oss_file = file->private_data;
1936 if (cmd == OSS_GETVERSION)
1937 return put_user(SNDRV_OSS_VERSION, p);
1938 if (cmd == OSS_ALSAEMULVER)
1939 return put_user(1, p);
1940 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1941 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1942 snd_pcm_substream_t *substream;
1944 for (idx = 0; idx < 2; ++idx) {
1945 substream = pcm_oss_file->streams[idx];
1946 if (substream != NULL)
1949 snd_assert(substream != NULL, return -ENXIO);
1950 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1953 if (((cmd >> 8) & 0xff) != 'P')
1956 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1959 case SNDCTL_DSP_RESET:
1960 return snd_pcm_oss_reset(pcm_oss_file);
1961 case SNDCTL_DSP_SYNC:
1962 return snd_pcm_oss_sync(pcm_oss_file);
1963 case SNDCTL_DSP_SPEED:
1964 if (get_user(res, p))
1966 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1968 return put_user(res, p);
1969 case SOUND_PCM_READ_RATE:
1970 res = snd_pcm_oss_get_rate(pcm_oss_file);
1973 return put_user(res, p);
1974 case SNDCTL_DSP_STEREO:
1975 if (get_user(res, p))
1977 res = res > 0 ? 2 : 1;
1978 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1980 return put_user(--res, p);
1981 case SNDCTL_DSP_GETBLKSIZE:
1982 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1985 return put_user(res, p);
1986 case SNDCTL_DSP_SETFMT:
1987 if (get_user(res, p))
1989 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1992 return put_user(res, p);
1993 case SOUND_PCM_READ_BITS:
1994 res = snd_pcm_oss_get_format(pcm_oss_file);
1997 return put_user(res, p);
1998 case SNDCTL_DSP_CHANNELS:
1999 if (get_user(res, p))
2001 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2004 return put_user(res, p);
2005 case SOUND_PCM_READ_CHANNELS:
2006 res = snd_pcm_oss_get_channels(pcm_oss_file);
2009 return put_user(res, p);
2010 case SOUND_PCM_WRITE_FILTER:
2011 case SOUND_PCM_READ_FILTER:
2013 case SNDCTL_DSP_POST:
2014 return snd_pcm_oss_post(pcm_oss_file);
2015 case SNDCTL_DSP_SUBDIVIDE:
2016 if (get_user(res, p))
2018 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2021 return put_user(res, p);
2022 case SNDCTL_DSP_SETFRAGMENT:
2023 if (get_user(res, p))
2025 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2026 case SNDCTL_DSP_GETFMTS:
2027 res = snd_pcm_oss_get_formats(pcm_oss_file);
2030 return put_user(res, p);
2031 case SNDCTL_DSP_GETOSPACE:
2032 case SNDCTL_DSP_GETISPACE:
2033 return snd_pcm_oss_get_space(pcm_oss_file,
2034 cmd == SNDCTL_DSP_GETISPACE ?
2035 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2036 (struct audio_buf_info __user *) arg);
2037 case SNDCTL_DSP_NONBLOCK:
2038 return snd_pcm_oss_nonblock(file);
2039 case SNDCTL_DSP_GETCAPS:
2040 res = snd_pcm_oss_get_caps(pcm_oss_file);
2043 return put_user(res, p);
2044 case SNDCTL_DSP_GETTRIGGER:
2045 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2048 return put_user(res, p);
2049 case SNDCTL_DSP_SETTRIGGER:
2050 if (get_user(res, p))
2052 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2053 case SNDCTL_DSP_GETIPTR:
2054 case SNDCTL_DSP_GETOPTR:
2055 return snd_pcm_oss_get_ptr(pcm_oss_file,
2056 cmd == SNDCTL_DSP_GETIPTR ?
2057 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2058 (struct count_info __user *) arg);
2059 case SNDCTL_DSP_MAPINBUF:
2060 case SNDCTL_DSP_MAPOUTBUF:
2061 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2062 cmd == SNDCTL_DSP_MAPINBUF ?
2063 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2064 (struct buffmem_desc __user *) arg);
2065 case SNDCTL_DSP_SETSYNCRO:
2066 /* stop DMA now.. */
2068 case SNDCTL_DSP_SETDUPLEX:
2069 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2072 case SNDCTL_DSP_GETODELAY:
2073 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2075 /* it's for sure, some broken apps don't check for error codes */
2079 return put_user(res, p);
2080 case SNDCTL_DSP_PROFILE:
2081 return 0; /* silently ignore */
2083 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2088 /* FIXME: need to unlock BKL to allow preemption */
2089 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
2090 unsigned int cmd, unsigned long arg)
2094 err = _snd_pcm_oss_ioctl(inode, file, cmd, arg);
2099 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2101 snd_pcm_oss_file_t *pcm_oss_file;
2102 snd_pcm_substream_t *substream;
2104 pcm_oss_file = file->private_data;
2105 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2106 if (substream == NULL)
2109 return snd_pcm_oss_read1(substream, buf, count);
2112 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2113 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2119 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2121 snd_pcm_oss_file_t *pcm_oss_file;
2122 snd_pcm_substream_t *substream;
2125 pcm_oss_file = file->private_data;
2126 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2127 if (substream == NULL)
2129 up(&file->f_dentry->d_inode->i_sem);
2130 result = snd_pcm_oss_write1(substream, buf, count);
2131 down(&file->f_dentry->d_inode->i_sem);
2133 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2138 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2140 snd_pcm_runtime_t *runtime = substream->runtime;
2141 if (atomic_read(&runtime->mmap_count))
2142 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2144 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2147 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2149 snd_pcm_runtime_t *runtime = substream->runtime;
2150 if (atomic_read(&runtime->mmap_count))
2151 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2153 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2156 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2158 snd_pcm_oss_file_t *pcm_oss_file;
2160 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2162 pcm_oss_file = file->private_data;
2164 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2165 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2168 if (psubstream != NULL) {
2169 snd_pcm_runtime_t *runtime = psubstream->runtime;
2170 poll_wait(file, &runtime->sleep, wait);
2171 snd_pcm_stream_lock_irq(psubstream);
2172 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2173 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2174 snd_pcm_oss_playback_ready(psubstream)))
2175 mask |= POLLOUT | POLLWRNORM;
2176 snd_pcm_stream_unlock_irq(psubstream);
2178 if (csubstream != NULL) {
2179 snd_pcm_runtime_t *runtime = csubstream->runtime;
2180 enum sndrv_pcm_state ostate;
2181 poll_wait(file, &runtime->sleep, wait);
2182 snd_pcm_stream_lock_irq(csubstream);
2183 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2184 snd_pcm_oss_capture_ready(csubstream))
2185 mask |= POLLIN | POLLRDNORM;
2186 snd_pcm_stream_unlock_irq(csubstream);
2187 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2188 snd_pcm_oss_file_t ofile;
2189 memset(&ofile, 0, sizeof(ofile));
2190 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2191 runtime->oss.trigger = 0;
2192 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2199 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2201 snd_pcm_oss_file_t *pcm_oss_file;
2202 snd_pcm_substream_t *substream = NULL;
2203 snd_pcm_runtime_t *runtime;
2207 printk("pcm_oss: mmap begin\n");
2209 pcm_oss_file = file->private_data;
2210 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2211 case VM_READ | VM_WRITE:
2212 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2217 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2220 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2225 /* set VM_READ access as well to fix memset() routines that do
2226 reads before writes (to improve performance) */
2227 area->vm_flags |= VM_READ;
2228 if (substream == NULL)
2230 runtime = substream->runtime;
2231 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2233 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2234 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2238 if (runtime->oss.params) {
2239 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2242 if (runtime->oss.plugin_first != NULL)
2245 if (area->vm_pgoff != 0)
2248 err = snd_pcm_mmap_data(substream, file, area);
2251 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2252 runtime->silence_threshold = 0;
2253 runtime->silence_size = 0;
2255 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2257 /* In mmap mode we never stop */
2258 runtime->stop_threshold = runtime->boundary;
2267 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2268 snd_info_buffer_t * buffer)
2270 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2271 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2272 down(&pstr->oss.setup_mutex);
2274 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2278 setup->disable ? " disable" : "",
2279 setup->direct ? " direct" : "",
2280 setup->block ? " block" : "",
2281 setup->nonblock ? " non-block" : "",
2282 setup->partialfrag ? " partial-frag" : "",
2283 setup->nosilence ? " no-silence" : "");
2284 setup = setup->next;
2286 up(&pstr->oss.setup_mutex);
2289 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2292 snd_pcm_substream_t *substream;
2293 snd_pcm_oss_setup_t *setup, *setupn;
2295 for (idx = 0, substream = pstr->substream;
2296 idx < pstr->substream_count; idx++, substream = substream->next)
2297 substream->oss.setup = NULL;
2298 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2299 setup; setup = setupn) {
2300 setupn = setup->next;
2301 kfree(setup->task_name);
2304 pstr->oss.setup_list = NULL;
2307 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2308 snd_info_buffer_t * buffer)
2310 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2311 char line[256], str[32], task_name[32], *ptr;
2313 snd_pcm_oss_setup_t *setup, *setup1, template;
2315 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2316 down(&pstr->oss.setup_mutex);
2317 memset(&template, 0, sizeof(template));
2318 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2319 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2320 snd_pcm_oss_proc_free_setup_list(pstr);
2321 up(&pstr->oss.setup_mutex);
2324 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2325 if (!strcmp(setup->task_name, task_name)) {
2330 ptr = snd_info_get_str(str, ptr, sizeof(str));
2331 template.periods = simple_strtoul(str, NULL, 10);
2332 ptr = snd_info_get_str(str, ptr, sizeof(str));
2333 template.period_size = simple_strtoul(str, NULL, 10);
2334 for (idx1 = 31; idx1 >= 0; idx1--)
2335 if (template.period_size & (1 << idx1))
2337 for (idx1--; idx1 >= 0; idx1--)
2338 template.period_size &= ~(1 << idx1);
2340 ptr = snd_info_get_str(str, ptr, sizeof(str));
2341 if (!strcmp(str, "disable")) {
2342 template.disable = 1;
2343 } else if (!strcmp(str, "direct")) {
2344 template.direct = 1;
2345 } else if (!strcmp(str, "block")) {
2347 } else if (!strcmp(str, "non-block")) {
2348 template.nonblock = 1;
2349 } else if (!strcmp(str, "partial-frag")) {
2350 template.partialfrag = 1;
2351 } else if (!strcmp(str, "no-silence")) {
2352 template.nosilence = 1;
2355 if (setup == NULL) {
2356 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2358 if (pstr->oss.setup_list == NULL) {
2359 pstr->oss.setup_list = setup;
2361 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2362 setup1->next = setup;
2364 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2366 buffer->error = -ENOMEM;
2371 up(&pstr->oss.setup_mutex);
2375 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2378 for (stream = 0; stream < 2; ++stream) {
2379 snd_info_entry_t *entry;
2380 snd_pcm_str_t *pstr = &pcm->streams[stream];
2381 if (pstr->substream_count == 0)
2383 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2384 entry->content = SNDRV_INFO_CONTENT_TEXT;
2385 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2386 entry->c.text.read_size = 8192;
2387 entry->c.text.read = snd_pcm_oss_proc_read;
2388 entry->c.text.write_size = 8192;
2389 entry->c.text.write = snd_pcm_oss_proc_write;
2390 entry->private_data = pstr;
2391 if (snd_info_register(entry) < 0) {
2392 snd_info_free_entry(entry);
2396 pstr->oss.proc_entry = entry;
2400 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2403 for (stream = 0; stream < 2; ++stream) {
2404 snd_pcm_str_t *pstr = &pcm->streams[stream];
2405 if (pstr->oss.proc_entry) {
2406 snd_info_unregister(pstr->oss.proc_entry);
2407 pstr->oss.proc_entry = NULL;
2408 snd_pcm_oss_proc_free_setup_list(pstr);
2417 static struct file_operations snd_pcm_oss_f_reg =
2419 .owner = THIS_MODULE,
2420 .read = snd_pcm_oss_read,
2421 .write = snd_pcm_oss_write,
2422 .open = snd_pcm_oss_open,
2423 .release = snd_pcm_oss_release,
2424 .poll = snd_pcm_oss_poll,
2425 .ioctl = snd_pcm_oss_ioctl,
2426 .mmap = snd_pcm_oss_mmap,
2429 static snd_minor_t snd_pcm_oss_reg =
2431 .comment = "digital audio",
2432 .f_ops = &snd_pcm_oss_f_reg,
2435 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2438 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2439 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2440 pcm->card, index, &snd_pcm_oss_reg,
2442 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2446 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2449 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2452 register_oss_dsp(pcm, 0);
2453 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2454 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2455 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2456 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2457 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2458 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2463 pcm->oss.reg_mask |= 1;
2465 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2466 register_oss_dsp(pcm, 1);
2468 pcm->oss.reg_mask |= 2;
2472 snd_pcm_oss_proc_init(pcm);
2477 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2480 if (pcm->oss.reg_mask & 1) {
2481 pcm->oss.reg_mask &= ~1;
2482 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2485 if (pcm->oss.reg_mask & 2) {
2486 pcm->oss.reg_mask &= ~2;
2487 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2494 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2496 snd_pcm_oss_disconnect_minor(pcm);
2498 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2499 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2500 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2504 snd_pcm_oss_proc_done(pcm);
2509 static snd_pcm_notify_t snd_pcm_oss_notify =
2511 .n_register = snd_pcm_oss_register_minor,
2512 .n_disconnect = snd_pcm_oss_disconnect_minor,
2513 .n_unregister = snd_pcm_oss_unregister_minor,
2516 static int __init alsa_pcm_oss_init(void)
2521 /* check device map table */
2522 for (i = 0; i < SNDRV_CARDS; i++) {
2523 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2524 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2527 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2528 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2532 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2537 static void __exit alsa_pcm_oss_exit(void)
2539 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2542 module_init(alsa_pcm_oss_init)
2543 module_exit(alsa_pcm_oss_exit)