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 <sound/core.h>
35 #include <sound/minors.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include "pcm_plugin.h"
39 #include <sound/info.h>
40 #include <linux/soundcard.h>
41 #include <sound/initval.h>
43 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
45 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
46 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47 static int nonblock_open;
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
50 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
51 MODULE_LICENSE("GPL");
52 MODULE_PARM(dsp_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
53 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
54 MODULE_PARM_SYNTAX(dsp_map, "default:0,skill:advanced");
55 MODULE_PARM(adsp_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
56 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
57 MODULE_PARM_SYNTAX(adsp_map, "default:1,skill:advanced");
58 MODULE_PARM(nonblock_open, "i");
59 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60 MODULE_PARM_SYNTAX(nonblock_open, "default:0,skill:advanced");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
64 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
65 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
66 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
67 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
69 static inline mm_segment_t snd_enter_user(void)
71 mm_segment_t fs = get_fs();
76 static inline void snd_leave_user(mm_segment_t fs)
81 int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
83 snd_pcm_runtime_t *runtime = substream->runtime;
84 snd_pcm_plugin_t *plugin, *next;
86 plugin = runtime->oss.plugin_first;
89 snd_pcm_plugin_free(plugin);
92 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
96 int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
98 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
99 plugin->next = runtime->oss.plugin_first;
101 if (runtime->oss.plugin_first) {
102 runtime->oss.plugin_first->prev = plugin;
103 runtime->oss.plugin_first = plugin;
105 runtime->oss.plugin_last =
106 runtime->oss.plugin_first = plugin;
111 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
113 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
115 plugin->prev = runtime->oss.plugin_last;
116 if (runtime->oss.plugin_last) {
117 runtime->oss.plugin_last->next = plugin;
118 runtime->oss.plugin_last = plugin;
120 runtime->oss.plugin_last =
121 runtime->oss.plugin_first = plugin;
126 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
128 snd_pcm_runtime_t *runtime = substream->runtime;
129 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
130 frames = frames_to_bytes(runtime, frames);
131 if (buffer_size == runtime->oss.buffer_bytes)
133 return (runtime->oss.buffer_bytes * frames) / buffer_size;
136 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
138 snd_pcm_runtime_t *runtime = substream->runtime;
139 snd_pcm_uframes_t buffer_size = snd_pcm_lib_buffer_bytes(substream);
140 if (buffer_size == runtime->oss.buffer_bytes)
141 return bytes_to_frames(runtime, bytes);
142 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
145 static int snd_pcm_oss_format_from(int format)
148 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
149 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
150 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
151 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
152 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
153 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
154 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
155 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
156 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
157 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
158 default: return SNDRV_PCM_FORMAT_U8;
162 static int snd_pcm_oss_format_to(int format)
165 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
166 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
167 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
168 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
169 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
170 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
171 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
172 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
173 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
174 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
175 default: return -EINVAL;
179 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream,
180 snd_pcm_hw_params_t *oss_params,
181 snd_pcm_hw_params_t *slave_params)
184 size_t oss_buffer_size, oss_period_size, oss_periods;
185 size_t min_period_size, max_period_size;
186 snd_pcm_runtime_t *runtime = substream->runtime;
187 size_t oss_frame_size;
189 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
190 params_channels(oss_params) / 8;
192 oss_buffer_size = snd_pcm_plug_client_size(substream,
193 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0)) * oss_frame_size;
194 oss_buffer_size = 1 << ld2(oss_buffer_size);
195 if (atomic_read(&runtime->mmap_count)) {
196 if (oss_buffer_size > runtime->oss.mmap_bytes)
197 oss_buffer_size = runtime->oss.mmap_bytes;
200 if (substream->oss.setup &&
201 substream->oss.setup->period_size > 16)
202 oss_period_size = substream->oss.setup->period_size;
203 else if (runtime->oss.fragshift) {
204 oss_period_size = 1 << runtime->oss.fragshift;
205 if (oss_period_size > oss_buffer_size / 2)
206 oss_period_size = oss_buffer_size / 2;
209 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
211 oss_period_size = oss_buffer_size;
213 oss_period_size /= 2;
214 } while (oss_period_size > bytes_per_sec);
215 if (runtime->oss.subdivision == 0) {
217 if (oss_period_size / sd > 4096)
219 if (oss_period_size / sd < 4096)
222 sd = runtime->oss.subdivision;
223 oss_period_size /= sd;
224 if (oss_period_size < 16)
225 oss_period_size = 16;
228 min_period_size = snd_pcm_plug_client_size(substream,
229 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
230 min_period_size *= oss_frame_size;
231 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
232 if (oss_period_size < min_period_size)
233 oss_period_size = min_period_size;
235 max_period_size = snd_pcm_plug_client_size(substream,
236 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
237 max_period_size *= oss_frame_size;
238 max_period_size = 1 << ld2(max_period_size);
239 if (oss_period_size > max_period_size)
240 oss_period_size = max_period_size;
242 oss_periods = oss_buffer_size / oss_period_size;
244 if (substream->oss.setup) {
245 if (substream->oss.setup->periods > 1)
246 oss_periods = substream->oss.setup->periods;
249 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
250 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
251 s = runtime->oss.maxfrags;
255 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
261 while (oss_period_size * oss_periods > oss_buffer_size)
262 oss_period_size /= 2;
264 snd_assert(oss_period_size >= 16, return -EINVAL);
265 runtime->oss.period_bytes = oss_period_size;
266 runtime->oss.period_frames = 1;
267 runtime->oss.periods = oss_periods;
271 static int choose_rate(snd_pcm_substream_t *substream,
272 snd_pcm_hw_params_t *params, unsigned int best_rate)
275 snd_pcm_hw_params_t *save;
276 unsigned int rate, prev;
278 save = kmalloc(sizeof(*save), GFP_KERNEL);
282 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
284 /* try multiples of the best rate */
287 if (it->max < rate || (it->max == rate && it->openmax))
289 if (it->min < rate || (it->min == rate && !it->openmin)) {
291 ret = snd_pcm_hw_param_set(substream, params,
292 SNDRV_PCM_HW_PARAM_RATE,
294 if (ret == (int)rate) {
306 /* not found, use the nearest rate */
308 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, 0);
311 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
313 snd_pcm_runtime_t *runtime = substream->runtime;
314 snd_pcm_hw_params_t *params, *sparams;
315 snd_pcm_sw_params_t *sw_params;
316 ssize_t oss_buffer_size, oss_period_size;
317 size_t oss_frame_size;
320 int format, sformat, n;
321 snd_mask_t sformat_mask;
324 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
325 params = kmalloc(sizeof(*params), GFP_KERNEL);
326 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
327 if (!sw_params || !params || !sparams) {
328 snd_printd("No memory\n");
333 if (atomic_read(&runtime->mmap_count)) {
336 snd_pcm_oss_setup_t *setup = substream->oss.setup;
337 direct = (setup != NULL && setup->direct);
340 _snd_pcm_hw_params_any(sparams);
341 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
342 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
343 snd_mask_none(&mask);
344 if (atomic_read(&runtime->mmap_count))
345 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
347 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
349 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
351 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
353 snd_printd("No usable accesses\n");
357 choose_rate(substream, sparams, runtime->oss.rate);
358 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, 0);
360 format = snd_pcm_oss_format_from(runtime->oss.format);
362 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
366 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
368 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
369 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
370 if (snd_mask_test(&sformat_mask, sformat) &&
371 snd_pcm_oss_format_to(sformat) >= 0)
374 if (sformat > SNDRV_PCM_FORMAT_LAST) {
375 snd_printd("Cannot find a format!!!\n");
380 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
381 snd_assert(err >= 0, goto failure);
384 memcpy(params, sparams, sizeof(*params));
386 _snd_pcm_hw_params_any(params);
387 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
388 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
389 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
390 snd_pcm_oss_format_from(runtime->oss.format), 0);
391 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
392 runtime->oss.channels, 0);
393 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
394 runtime->oss.rate, 0);
395 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
396 params_access(params), params_format(params),
397 params_channels(params), params_rate(params));
399 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
400 params_access(sparams), params_format(sparams),
401 params_channels(sparams), params_rate(sparams));
403 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
404 params_channels(params) / 8;
406 snd_pcm_oss_plugin_clear(substream);
408 /* add necessary plugins */
409 snd_pcm_oss_plugin_clear(substream);
410 if ((err = snd_pcm_plug_format_plugins(substream,
413 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
414 snd_pcm_oss_plugin_clear(substream);
417 if (runtime->oss.plugin_first) {
418 snd_pcm_plugin_t *plugin;
419 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
420 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
421 snd_pcm_oss_plugin_clear(substream);
424 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
425 err = snd_pcm_plugin_append(plugin);
427 err = snd_pcm_plugin_insert(plugin);
430 snd_pcm_oss_plugin_clear(substream);
436 err = snd_pcm_oss_period_size(substream, params, sparams);
440 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
441 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, 0);
442 snd_assert(err >= 0, goto failure);
444 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
445 runtime->oss.periods, 0);
446 snd_assert(err >= 0, goto failure);
448 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
450 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
451 snd_printd("HW_PARAMS failed: %i\n", err);
455 memset(sw_params, 0, sizeof(*sw_params));
456 if (runtime->oss.trigger) {
457 sw_params->start_threshold = 1;
459 sw_params->start_threshold = runtime->boundary;
461 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
462 sw_params->stop_threshold = runtime->boundary;
464 sw_params->stop_threshold = runtime->buffer_size;
465 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
466 sw_params->period_step = 1;
467 sw_params->sleep_min = 0;
468 sw_params->avail_min = 1;
469 sw_params->xfer_align = 1;
470 if (atomic_read(&runtime->mmap_count) ||
471 (substream->oss.setup && substream->oss.setup->nosilence)) {
472 sw_params->silence_threshold = 0;
473 sw_params->silence_size = 0;
475 snd_pcm_uframes_t frames;
476 frames = runtime->period_size + 16;
477 if (frames > runtime->buffer_size)
478 frames = runtime->buffer_size;
479 sw_params->silence_threshold = frames;
480 sw_params->silence_size = frames;
483 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
484 snd_printd("SW_PARAMS failed: %i\n", err);
488 runtime->oss.periods = params_periods(sparams);
489 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
490 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
491 if (runtime->oss.plugin_first) {
492 err = snd_pcm_plug_alloc(substream, oss_period_size);
496 oss_period_size *= oss_frame_size;
498 oss_buffer_size = oss_period_size * runtime->oss.periods;
499 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
501 runtime->oss.period_bytes = oss_period_size;
502 runtime->oss.buffer_bytes = oss_buffer_size;
504 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
505 runtime->oss.period_bytes,
506 runtime->oss.buffer_bytes);
507 pdprintf("slave: period_size = %i, buffer_size = %i\n",
508 params_period_size(sparams),
509 params_buffer_size(sparams));
511 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
512 runtime->oss.channels = params_channels(params);
513 runtime->oss.rate = params_rate(params);
515 runtime->oss.params = 0;
516 runtime->oss.prepare = 1;
517 if (runtime->oss.buffer != NULL)
518 vfree(runtime->oss.buffer);
519 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
520 runtime->oss.buffer_used = 0;
521 if (runtime->dma_area)
522 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
524 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
537 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
540 snd_pcm_substream_t *asubstream = NULL, *substream;
542 for (idx = 0; idx < 2; idx++) {
543 substream = pcm_oss_file->streams[idx];
544 if (substream == NULL)
546 if (asubstream == NULL)
547 asubstream = substream;
548 if (substream->runtime->oss.params) {
549 err = snd_pcm_oss_change_params(substream);
554 snd_assert(asubstream != NULL, return -EIO);
556 *r_substream = asubstream;
560 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
563 snd_pcm_runtime_t *runtime = substream->runtime;
565 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, 0);
567 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
570 runtime->oss.prepare = 0;
571 runtime->oss.prev_hw_ptr_interrupt = 0;
572 runtime->oss.period_ptr = 0;
573 runtime->oss.buffer_used = 0;
578 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
580 snd_pcm_runtime_t *runtime;
583 if (substream == NULL)
585 runtime = substream->runtime;
586 if (runtime->oss.params) {
587 err = snd_pcm_oss_change_params(substream);
591 if (runtime->oss.prepare) {
592 err = snd_pcm_oss_prepare(substream);
599 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
601 snd_pcm_runtime_t *runtime;
602 snd_pcm_uframes_t frames;
606 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
609 runtime = substream->runtime;
610 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
612 /* in case of overrun, skip whole periods like OSS/Linux driver does */
613 /* until avail(delay) <= buffer_size */
614 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
615 frames /= runtime->period_size;
616 frames *= runtime->period_size;
617 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
624 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
626 snd_pcm_runtime_t *runtime = substream->runtime;
629 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
630 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
632 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
633 printk("pcm_oss: write: recovering from XRUN\n");
635 printk("pcm_oss: write: recovering from SUSPEND\n");
637 ret = snd_pcm_oss_prepare(substream);
643 fs = snd_enter_user();
644 ret = snd_pcm_lib_write(substream, ptr, frames);
647 ret = snd_pcm_lib_write(substream, ptr, frames);
649 if (ret != -EPIPE && ret != -ESTRPIPE)
651 /* test, if we can't store new data, because the stream */
652 /* has not been started */
653 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
659 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
661 snd_pcm_runtime_t *runtime = substream->runtime;
662 snd_pcm_sframes_t delay;
665 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
666 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
668 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
669 printk("pcm_oss: read: recovering from XRUN\n");
671 printk("pcm_oss: read: recovering from SUSPEND\n");
673 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
676 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
677 ret = snd_pcm_oss_prepare(substream);
681 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
686 fs = snd_enter_user();
687 ret = snd_pcm_lib_read(substream, ptr, frames);
690 ret = snd_pcm_lib_read(substream, ptr, frames);
693 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
694 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
700 if (ret != -ESTRPIPE)
706 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
708 snd_pcm_runtime_t *runtime = substream->runtime;
711 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
712 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
714 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
715 printk("pcm_oss: writev: recovering from XRUN\n");
717 printk("pcm_oss: writev: recovering from SUSPEND\n");
719 ret = snd_pcm_oss_prepare(substream);
725 fs = snd_enter_user();
726 ret = snd_pcm_lib_writev(substream, bufs, frames);
729 ret = snd_pcm_lib_writev(substream, bufs, frames);
731 if (ret != -EPIPE && ret != -ESTRPIPE)
734 /* test, if we can't store new data, because the stream */
735 /* has not been started */
736 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
742 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
744 snd_pcm_runtime_t *runtime = substream->runtime;
747 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
748 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
750 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
751 printk("pcm_oss: readv: recovering from XRUN\n");
753 printk("pcm_oss: readv: recovering from SUSPEND\n");
755 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
758 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
759 ret = snd_pcm_oss_prepare(substream);
765 fs = snd_enter_user();
766 ret = snd_pcm_lib_readv(substream, bufs, frames);
769 ret = snd_pcm_lib_readv(substream, bufs, frames);
771 if (ret != -EPIPE && ret != -ESTRPIPE)
777 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
779 snd_pcm_runtime_t *runtime = substream->runtime;
780 snd_pcm_sframes_t frames, frames1;
781 if (runtime->oss.plugin_first) {
782 snd_pcm_plugin_channel_t *channels;
783 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
785 if (copy_from_user(runtime->oss.buffer, buf, bytes))
787 buf = runtime->oss.buffer;
789 frames = bytes / oss_frame_bytes;
790 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
793 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
796 bytes = frames1 * oss_frame_bytes;
798 frames = bytes_to_frames(runtime, bytes);
799 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
802 bytes = frames_to_bytes(runtime, frames1);
807 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char *buf, size_t bytes)
811 snd_pcm_runtime_t *runtime = substream->runtime;
813 if (atomic_read(&runtime->mmap_count))
816 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
819 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
821 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
822 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
824 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
825 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
827 runtime->oss.buffer_used += tmp;
831 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
832 runtime->oss.buffer_used == runtime->oss.period_bytes) {
833 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer, runtime->oss.buffer_used, 1);
835 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
836 runtime->oss.bytes += tmp;
837 runtime->oss.buffer_used = 0;
838 runtime->oss.period_ptr += tmp;
839 runtime->oss.period_ptr %= runtime->oss.period_bytes;
842 tmp = snd_pcm_oss_write2(substream, (char *)buf, runtime->oss.period_bytes, 0);
844 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
845 runtime->oss.bytes += tmp;
854 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
856 snd_pcm_runtime_t *runtime = substream->runtime;
857 snd_pcm_sframes_t frames, frames1;
858 char *final_dst = buf;
859 if (runtime->oss.plugin_first) {
860 snd_pcm_plugin_channel_t *channels;
861 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
863 buf = runtime->oss.buffer;
864 frames = bytes / oss_frame_bytes;
865 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
868 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
871 bytes = frames1 * oss_frame_bytes;
872 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
875 frames = bytes_to_frames(runtime, bytes);
876 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
879 bytes = frames_to_bytes(runtime, frames1);
884 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char *buf, size_t bytes)
888 snd_pcm_runtime_t *runtime = substream->runtime;
890 if (atomic_read(&runtime->mmap_count))
893 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
896 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
897 if (runtime->oss.buffer_used == 0) {
898 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
900 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
901 runtime->oss.bytes += tmp;
902 runtime->oss.period_ptr = tmp;
903 runtime->oss.buffer_used = tmp;
906 if ((size_t) tmp > runtime->oss.buffer_used)
907 tmp = runtime->oss.buffer_used;
908 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
909 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
913 runtime->oss.buffer_used -= tmp;
915 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
917 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
918 runtime->oss.bytes += tmp;
927 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
929 snd_pcm_substream_t *substream;
931 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
932 if (substream != NULL) {
933 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
934 substream->runtime->oss.prepare = 1;
936 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
937 if (substream != NULL) {
938 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
939 substream->runtime->oss.prepare = 1;
944 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
946 snd_pcm_substream_t *substream;
949 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
950 if (substream != NULL) {
951 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
953 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, 0);
955 /* note: all errors from the start action are ignored */
956 /* OSS apps do not know, how to handle them */
960 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
962 snd_pcm_runtime_t *runtime;
967 runtime = substream->runtime;
968 init_waitqueue_entry(&wait, current);
969 add_wait_queue(&runtime->sleep, &wait);
971 printk("sync1: size = %li\n", size);
974 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
976 runtime->oss.buffer_used = 0;
980 if (result != 0 && result != -EAGAIN)
983 set_current_state(TASK_INTERRUPTIBLE);
984 snd_pcm_stream_lock_irq(substream);
985 res = runtime->status->state;
986 snd_pcm_stream_unlock_irq(substream);
987 if (res != SNDRV_PCM_STATE_RUNNING) {
988 set_current_state(TASK_RUNNING);
991 res = schedule_timeout(10 * HZ);
992 if (signal_pending(current)) {
993 result = -ERESTARTSYS;
997 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1002 remove_wait_queue(&runtime->sleep, &wait);
1006 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1009 unsigned int saved_f_flags;
1010 snd_pcm_substream_t *substream;
1011 snd_pcm_runtime_t *runtime;
1012 snd_pcm_format_t format;
1013 unsigned long width;
1016 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1017 if (substream != NULL) {
1018 runtime = substream->runtime;
1019 if (atomic_read(&runtime->mmap_count))
1021 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1023 format = snd_pcm_oss_format_from(runtime->oss.format);
1024 width = snd_pcm_format_physical_width(format);
1025 if (runtime->oss.buffer_used > 0) {
1027 printk("sync: buffer_used\n");
1029 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1030 snd_pcm_format_set_silence(format,
1031 runtime->oss.buffer + runtime->oss.buffer_used,
1033 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1036 } else if (runtime->oss.period_ptr > 0) {
1038 printk("sync: period_ptr\n");
1040 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1041 snd_pcm_format_set_silence(format,
1042 runtime->oss.buffer,
1044 err = snd_pcm_oss_sync1(substream, size);
1049 * The ALSA's period might be a bit large than OSS one.
1050 * Fill the remain portion of ALSA period with zeros.
1052 size = runtime->control->appl_ptr % runtime->period_size;
1054 size = runtime->period_size - size;
1055 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1056 size = (runtime->frame_bits * size) / 8;
1058 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1061 size1 /= runtime->sample_bits;
1062 snd_pcm_format_set_silence(runtime->format,
1063 runtime->oss.buffer,
1065 snd_pcm_lib_write(substream, runtime->oss.buffer, size1);
1067 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1068 void *buffers[runtime->channels];
1069 memset(buffers, 0, runtime->channels * sizeof(void *));
1070 snd_pcm_lib_writev(substream, (void **)buffers, size);
1074 * finish sync: drain the buffer
1077 saved_f_flags = substream->ffile->f_flags;
1078 substream->ffile->f_flags &= ~O_NONBLOCK;
1079 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, 0);
1080 substream->ffile->f_flags = saved_f_flags;
1083 runtime->oss.prepare = 1;
1086 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1087 if (substream != NULL) {
1088 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1090 runtime = substream->runtime;
1091 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
1094 runtime->oss.buffer_used = 0;
1095 runtime->oss.prepare = 1;
1100 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1104 for (idx = 1; idx >= 0; --idx) {
1105 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1106 snd_pcm_runtime_t *runtime;
1107 if (substream == NULL)
1109 runtime = substream->runtime;
1112 else if (rate > 192000)
1114 if (runtime->oss.rate != rate) {
1115 runtime->oss.params = 1;
1116 runtime->oss.rate = rate;
1119 return snd_pcm_oss_get_rate(pcm_oss_file);
1122 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1124 snd_pcm_substream_t *substream;
1127 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1129 return substream->runtime->oss.rate;
1132 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1139 for (idx = 1; idx >= 0; --idx) {
1140 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1141 snd_pcm_runtime_t *runtime;
1142 if (substream == NULL)
1144 runtime = substream->runtime;
1145 if (runtime->oss.channels != channels) {
1146 runtime->oss.params = 1;
1147 runtime->oss.channels = channels;
1150 return snd_pcm_oss_get_channels(pcm_oss_file);
1153 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1155 snd_pcm_substream_t *substream;
1158 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1160 return substream->runtime->oss.channels;
1163 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1165 snd_pcm_substream_t *substream;
1168 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1170 return substream->runtime->oss.period_bytes;
1173 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1175 snd_pcm_substream_t *substream;
1178 snd_pcm_hw_params_t params;
1179 unsigned int formats = 0;
1180 snd_mask_t format_mask;
1182 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1184 if (atomic_read(&substream->runtime->mmap_count)) {
1187 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1188 direct = (setup != NULL && setup->direct);
1191 return AFMT_MU_LAW | AFMT_U8 |
1192 AFMT_S16_LE | AFMT_S16_BE |
1193 AFMT_S8 | AFMT_U16_LE |
1195 _snd_pcm_hw_params_any(¶ms);
1196 err = snd_pcm_hw_refine(substream, ¶ms);
1197 snd_assert(err >= 0, return err);
1198 format_mask = *hw_param_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT);
1199 for (fmt = 0; fmt < 32; ++fmt) {
1200 if (snd_mask_test(&format_mask, fmt)) {
1201 int f = snd_pcm_oss_format_to(fmt);
1209 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1213 if (format != AFMT_QUERY) {
1214 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1215 if (!(formats & format))
1217 for (idx = 1; idx >= 0; --idx) {
1218 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1219 snd_pcm_runtime_t *runtime;
1220 if (substream == NULL)
1222 runtime = substream->runtime;
1223 if (runtime->oss.format != format) {
1224 runtime->oss.params = 1;
1225 runtime->oss.format = format;
1229 return snd_pcm_oss_get_format(pcm_oss_file);
1232 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1234 snd_pcm_substream_t *substream;
1237 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1239 return substream->runtime->oss.format;
1242 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1244 snd_pcm_runtime_t *runtime;
1246 if (substream == NULL)
1248 runtime = substream->runtime;
1249 if (subdivide == 0) {
1250 subdivide = runtime->oss.subdivision;
1255 if (runtime->oss.subdivision || runtime->oss.fragshift)
1257 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1258 subdivide != 8 && subdivide != 16)
1260 runtime->oss.subdivision = subdivide;
1261 runtime->oss.params = 1;
1265 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1267 int err = -EINVAL, idx;
1269 for (idx = 1; idx >= 0; --idx) {
1270 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1271 if (substream == NULL)
1273 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1279 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1281 snd_pcm_runtime_t *runtime;
1283 if (substream == NULL)
1285 runtime = substream->runtime;
1286 if (runtime->oss.subdivision || runtime->oss.fragshift)
1288 runtime->oss.fragshift = val & 0xffff;
1289 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1290 if (runtime->oss.fragshift < 4) /* < 16 */
1291 runtime->oss.fragshift = 4;
1292 if (runtime->oss.maxfrags < 2)
1293 runtime->oss.maxfrags = 2;
1294 runtime->oss.params = 1;
1298 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1300 int err = -EINVAL, idx;
1302 for (idx = 1; idx >= 0; --idx) {
1303 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1304 if (substream == NULL)
1306 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1312 static int snd_pcm_oss_nonblock(struct file * file)
1314 file->f_flags |= O_NONBLOCK;
1318 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1321 if (substream == NULL) {
1322 res &= ~DSP_CAP_DUPLEX;
1325 #ifdef DSP_CAP_MULTI
1326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1327 if (substream->pstr->substream_count > 1)
1328 res |= DSP_CAP_MULTI;
1330 /* DSP_CAP_REALTIME is set all times: */
1331 /* all ALSA drivers can return actual pointer in ring buffer */
1332 #if defined(DSP_CAP_REALTIME) && 0
1334 snd_pcm_runtime_t *runtime = substream->runtime;
1335 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1336 res &= ~DSP_CAP_REALTIME;
1342 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1346 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1347 for (idx = 0; idx < 2; idx++) {
1348 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1349 result = snd_pcm_oss_get_caps1(substream, result);
1351 result |= 0x0001; /* revision - same as SB AWE 64 */
1355 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1357 snd_pcm_runtime_t *runtime = substream->runtime;
1358 snd_pcm_uframes_t appl_ptr;
1359 appl_ptr = hw_ptr + runtime->buffer_size;
1360 appl_ptr %= runtime->boundary;
1361 runtime->control->appl_ptr = appl_ptr;
1364 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1366 snd_pcm_runtime_t *runtime;
1367 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1371 printk("pcm_oss: trigger = 0x%x\n", trigger);
1374 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1375 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1378 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1382 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1386 runtime = psubstream->runtime;
1387 if (trigger & PCM_ENABLE_OUTPUT) {
1388 if (runtime->oss.trigger)
1390 if (atomic_read(&psubstream->runtime->mmap_count))
1391 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1392 runtime->oss.trigger = 1;
1393 runtime->start_threshold = 1;
1394 cmd = SNDRV_PCM_IOCTL_START;
1396 if (!runtime->oss.trigger)
1398 runtime->oss.trigger = 0;
1399 runtime->start_threshold = runtime->boundary;
1400 cmd = SNDRV_PCM_IOCTL_DROP;
1401 runtime->oss.prepare = 1;
1403 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, 0);
1409 runtime = csubstream->runtime;
1410 if (trigger & PCM_ENABLE_INPUT) {
1411 if (runtime->oss.trigger)
1413 runtime->oss.trigger = 1;
1414 runtime->start_threshold = 1;
1415 cmd = SNDRV_PCM_IOCTL_START;
1417 if (!runtime->oss.trigger)
1419 runtime->oss.trigger = 0;
1420 runtime->start_threshold = runtime->boundary;
1421 cmd = SNDRV_PCM_IOCTL_DROP;
1422 runtime->oss.prepare = 1;
1424 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, 0);
1432 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1434 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1437 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1438 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1439 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1440 result |= PCM_ENABLE_OUTPUT;
1441 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1442 result |= PCM_ENABLE_INPUT;
1446 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1448 snd_pcm_substream_t *substream;
1449 snd_pcm_runtime_t *runtime;
1450 snd_pcm_sframes_t delay;
1453 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1454 if (substream == NULL)
1456 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1458 runtime = substream->runtime;
1459 if (runtime->oss.params || runtime->oss.prepare)
1461 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1463 delay = 0; /* hack for broken OSS applications */
1466 return snd_pcm_oss_bytes(substream, delay);
1469 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info * _info)
1471 snd_pcm_substream_t *substream;
1472 snd_pcm_runtime_t *runtime;
1473 snd_pcm_sframes_t delay;
1475 struct count_info info;
1480 substream = pcm_oss_file->streams[stream];
1481 if (substream == NULL)
1483 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1485 runtime = substream->runtime;
1486 if (runtime->oss.params || runtime->oss.prepare) {
1487 memset(&info, 0, sizeof(info));
1488 if (copy_to_user(_info, &info, sizeof(info)))
1492 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1493 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1494 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1499 fixup = runtime->oss.buffer_used;
1502 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1503 fixup = -runtime->oss.buffer_used;
1507 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1508 if (atomic_read(&runtime->mmap_count)) {
1509 snd_pcm_sframes_t n;
1510 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1512 n += runtime->boundary;
1513 info.blocks = n / runtime->period_size;
1514 runtime->oss.prev_hw_ptr_interrupt = delay;
1515 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1516 snd_pcm_oss_simulate_fill(substream, delay);
1517 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr);
1519 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1520 info.blocks = delay / runtime->oss.period_bytes;
1521 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1522 info.bytes = runtime->oss.bytes - delay;
1524 info.bytes = runtime->oss.bytes + delay;
1526 if (copy_to_user(_info, &info, sizeof(info)))
1531 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info *_info)
1533 snd_pcm_substream_t *substream;
1534 snd_pcm_runtime_t *runtime;
1535 snd_pcm_sframes_t avail;
1537 struct audio_buf_info info;
1542 substream = pcm_oss_file->streams[stream];
1543 if (substream == NULL)
1545 runtime = substream->runtime;
1547 if (runtime->oss.params &&
1548 (err = snd_pcm_oss_change_params(substream)) < 0)
1551 info.fragsize = runtime->oss.period_bytes;
1552 info.fragstotal = runtime->periods;
1553 if (runtime->oss.prepare) {
1554 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1555 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1556 info.fragments = runtime->oss.periods;
1562 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1563 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1564 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1565 avail = runtime->buffer_size;
1569 avail = runtime->buffer_size - avail;
1570 fixup = -runtime->oss.buffer_used;
1573 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1574 fixup = runtime->oss.buffer_used;
1578 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1579 info.fragments = info.bytes / runtime->oss.period_bytes;
1583 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1585 if (copy_to_user(_info, &info, sizeof(info)))
1590 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc * _info)
1592 // it won't be probably implemented
1593 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1597 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1599 const char *ptr, *ptrl;
1600 snd_pcm_oss_setup_t *setup;
1602 down(&pcm->streams[stream].oss.setup_mutex);
1603 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1604 if (!strcmp(setup->task_name, task_name)) {
1605 up(&pcm->streams[stream].oss.setup_mutex);
1609 ptr = ptrl = task_name;
1615 if (ptrl == task_name) {
1619 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1620 if (!strcmp(setup->task_name, ptrl)) {
1621 up(&pcm->streams[stream].oss.setup_mutex);
1626 up(&pcm->streams[stream].oss.setup_mutex);
1630 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1631 snd_pcm_oss_setup_t *setup,
1634 snd_pcm_runtime_t *runtime;
1636 substream->oss.oss = 1;
1637 substream->oss.setup = setup;
1638 runtime = substream->runtime;
1639 runtime->oss.params = 1;
1640 runtime->oss.trigger = 1;
1641 runtime->oss.rate = 8000;
1642 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1643 case SNDRV_MINOR_OSS_PCM_8:
1644 runtime->oss.format = AFMT_U8;
1646 case SNDRV_MINOR_OSS_PCM_16:
1647 runtime->oss.format = AFMT_S16_LE;
1650 runtime->oss.format = AFMT_MU_LAW;
1652 runtime->oss.channels = 1;
1653 runtime->oss.fragshift = 0;
1654 runtime->oss.maxfrags = 0;
1655 runtime->oss.subdivision = 0;
1658 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1660 snd_pcm_runtime_t *runtime;
1661 runtime = substream->runtime;
1662 if (runtime->oss.buffer)
1663 vfree(runtime->oss.buffer);
1664 snd_pcm_oss_plugin_clear(substream);
1665 substream->oss.file = NULL;
1666 substream->oss.oss = 0;
1669 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1672 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1673 for (cidx = 0; cidx < 2; ++cidx) {
1674 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1675 snd_pcm_runtime_t *runtime;
1676 if (substream == NULL)
1678 runtime = substream->runtime;
1680 snd_pcm_stream_lock_irq(substream);
1681 if (snd_pcm_running(substream))
1682 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1683 snd_pcm_stream_unlock_irq(substream);
1684 if (substream->open_flag) {
1685 if (substream->ops->hw_free != NULL)
1686 substream->ops->hw_free(substream);
1687 substream->ops->close(substream);
1688 substream->open_flag = 0;
1690 substream->ffile = NULL;
1691 snd_pcm_oss_release_substream(substream);
1692 snd_pcm_release_substream(substream);
1694 snd_magic_kfree(pcm_oss_file);
1698 static int snd_pcm_oss_open_file(struct file *file,
1700 snd_pcm_oss_file_t **rpcm_oss_file,
1702 snd_pcm_oss_setup_t *psetup,
1703 snd_pcm_oss_setup_t *csetup)
1706 snd_pcm_oss_file_t *pcm_oss_file;
1707 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1708 unsigned int f_mode = file->f_mode;
1710 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1711 *rpcm_oss_file = NULL;
1713 pcm_oss_file = snd_magic_kcalloc(snd_pcm_oss_file_t, 0, GFP_KERNEL);
1714 if (pcm_oss_file == NULL)
1717 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1718 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1719 f_mode = FMODE_WRITE;
1720 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1721 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1722 &psubstream)) < 0) {
1723 snd_pcm_oss_release_file(pcm_oss_file);
1726 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1728 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1729 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1730 &csubstream)) < 0) {
1731 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1732 snd_pcm_oss_release_file(pcm_oss_file);
1738 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1741 if (psubstream == NULL && csubstream == NULL) {
1742 snd_pcm_oss_release_file(pcm_oss_file);
1745 if (psubstream != NULL) {
1746 psubstream->oss.file = pcm_oss_file;
1747 err = snd_pcm_hw_constraints_init(psubstream);
1749 snd_printd("snd_pcm_hw_constraint_init failed\n");
1750 snd_pcm_oss_release_file(pcm_oss_file);
1753 if ((err = psubstream->ops->open(psubstream)) < 0) {
1754 snd_pcm_oss_release_file(pcm_oss_file);
1757 psubstream->open_flag = 1;
1758 err = snd_pcm_hw_constraints_complete(psubstream);
1760 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1761 snd_pcm_oss_release_file(pcm_oss_file);
1764 psubstream->ffile = file;
1765 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1767 if (csubstream != NULL) {
1768 csubstream->oss.file = pcm_oss_file;
1769 err = snd_pcm_hw_constraints_init(csubstream);
1771 snd_printd("snd_pcm_hw_constraint_init failed\n");
1772 snd_pcm_oss_release_file(pcm_oss_file);
1775 if ((err = csubstream->ops->open(csubstream)) < 0) {
1776 snd_pcm_oss_release_file(pcm_oss_file);
1779 csubstream->open_flag = 1;
1780 err = snd_pcm_hw_constraints_complete(csubstream);
1782 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1783 snd_pcm_oss_release_file(pcm_oss_file);
1786 csubstream->ffile = file;
1787 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1790 file->private_data = pcm_oss_file;
1791 *rpcm_oss_file = pcm_oss_file;
1796 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1798 int minor = iminor(inode);
1799 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1804 snd_pcm_oss_file_t *pcm_oss_file;
1805 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1809 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1810 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1811 adsp_map[cardnum] : dsp_map[cardnum];
1813 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1818 err = snd_card_file_add(pcm->card, file);
1821 if (!try_module_get(pcm->card->module)) {
1825 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1829 if (file->f_mode & FMODE_WRITE)
1830 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1831 if (file->f_mode & FMODE_READ)
1832 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1834 nonblock = !!(file->f_flags & O_NONBLOCK);
1835 if (psetup && !psetup->disable) {
1836 if (psetup->nonblock)
1838 else if (psetup->block)
1840 } else if (csetup && !csetup->disable) {
1841 if (csetup->nonblock)
1843 else if (csetup->block)
1847 nonblock = nonblock_open;
1849 init_waitqueue_entry(&wait, current);
1850 add_wait_queue(&pcm->open_wait, &wait);
1851 down(&pcm->open_mutex);
1853 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1854 minor, psetup, csetup);
1857 if (err == -EAGAIN) {
1864 set_current_state(TASK_INTERRUPTIBLE);
1865 up(&pcm->open_mutex);
1867 down(&pcm->open_mutex);
1868 if (signal_pending(current)) {
1873 remove_wait_queue(&pcm->open_wait, &wait);
1874 up(&pcm->open_mutex);
1880 module_put(pcm->card->module);
1882 snd_card_file_remove(pcm->card, file);
1887 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1890 snd_pcm_substream_t *substream;
1891 snd_pcm_oss_file_t *pcm_oss_file;
1893 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1894 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1895 if (substream == NULL)
1896 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1897 snd_assert(substream != NULL, return -ENXIO);
1898 pcm = substream->pcm;
1899 snd_pcm_oss_sync(pcm_oss_file);
1900 down(&pcm->open_mutex);
1901 snd_pcm_oss_release_file(pcm_oss_file);
1902 up(&pcm->open_mutex);
1903 wake_up(&pcm->open_wait);
1904 module_put(pcm->card->module);
1905 snd_card_file_remove(pcm->card, file);
1909 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1910 unsigned int cmd, unsigned long arg)
1912 snd_pcm_oss_file_t *pcm_oss_file;
1915 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1916 if (cmd == OSS_GETVERSION)
1917 return put_user(SNDRV_OSS_VERSION, (int *)arg);
1918 if (cmd == OSS_ALSAEMULVER)
1919 return put_user(1, (int *)arg);
1920 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1921 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1922 snd_pcm_substream_t *substream;
1924 for (idx = 0; idx < 2; ++idx) {
1925 substream = pcm_oss_file->streams[idx];
1926 if (substream != NULL)
1929 snd_assert(substream != NULL, return -ENXIO);
1930 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1933 if (((cmd >> 8) & 0xff) != 'P')
1936 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1939 case SNDCTL_DSP_RESET:
1940 return snd_pcm_oss_reset(pcm_oss_file);
1941 case SNDCTL_DSP_SYNC:
1942 return snd_pcm_oss_sync(pcm_oss_file);
1943 case SNDCTL_DSP_SPEED:
1944 if (get_user(res, (int *)arg))
1946 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1948 return put_user(res, (int *)arg);
1949 case SOUND_PCM_READ_RATE:
1950 res = snd_pcm_oss_get_rate(pcm_oss_file);
1953 return put_user(res, (int *)arg);
1954 case SNDCTL_DSP_STEREO:
1955 if (get_user(res, (int *)arg))
1957 res = res > 0 ? 2 : 1;
1958 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1960 return put_user(--res, (int *)arg);
1961 case SNDCTL_DSP_GETBLKSIZE:
1962 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1965 return put_user(res, (int *)arg);
1966 case SNDCTL_DSP_SETFMT:
1967 if (get_user(res, (int *)arg))
1969 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1972 return put_user(res, (int *)arg);
1973 case SOUND_PCM_READ_BITS:
1974 res = snd_pcm_oss_get_format(pcm_oss_file);
1977 return put_user(res, (int *)arg);
1978 case SNDCTL_DSP_CHANNELS:
1979 if (get_user(res, (int *)arg))
1981 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1984 return put_user(res, (int *)arg);
1985 case SOUND_PCM_READ_CHANNELS:
1986 res = snd_pcm_oss_get_channels(pcm_oss_file);
1989 return put_user(res, (int *)arg);
1990 case SOUND_PCM_WRITE_FILTER:
1991 case SOUND_PCM_READ_FILTER:
1993 case SNDCTL_DSP_POST:
1994 return snd_pcm_oss_post(pcm_oss_file);
1995 case SNDCTL_DSP_SUBDIVIDE:
1996 if (get_user(res, (int *)arg))
1998 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2001 return put_user(res, (int *)arg);
2002 case SNDCTL_DSP_SETFRAGMENT:
2003 if (get_user(res, (int *)arg))
2005 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2006 case SNDCTL_DSP_GETFMTS:
2007 res = snd_pcm_oss_get_formats(pcm_oss_file);
2010 return put_user(res, (int *)arg);
2011 case SNDCTL_DSP_GETOSPACE:
2012 case SNDCTL_DSP_GETISPACE:
2013 return snd_pcm_oss_get_space(pcm_oss_file,
2014 cmd == SNDCTL_DSP_GETISPACE ?
2015 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2016 (struct audio_buf_info *) arg);
2017 case SNDCTL_DSP_NONBLOCK:
2018 return snd_pcm_oss_nonblock(file);
2019 case SNDCTL_DSP_GETCAPS:
2020 res = snd_pcm_oss_get_caps(pcm_oss_file);
2023 return put_user(res, (int *)arg);
2024 case SNDCTL_DSP_GETTRIGGER:
2025 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2028 return put_user(res, (int *)arg);
2029 case SNDCTL_DSP_SETTRIGGER:
2030 if (get_user(res, (int *)arg))
2032 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2033 case SNDCTL_DSP_GETIPTR:
2034 case SNDCTL_DSP_GETOPTR:
2035 return snd_pcm_oss_get_ptr(pcm_oss_file,
2036 cmd == SNDCTL_DSP_GETIPTR ?
2037 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2038 (struct count_info *) arg);
2039 case SNDCTL_DSP_MAPINBUF:
2040 case SNDCTL_DSP_MAPOUTBUF:
2041 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2042 cmd == SNDCTL_DSP_MAPINBUF ?
2043 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2044 (struct buffmem_desc *) arg);
2045 case SNDCTL_DSP_SETSYNCRO:
2046 /* stop DMA now.. */
2048 case SNDCTL_DSP_SETDUPLEX:
2049 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2052 case SNDCTL_DSP_GETODELAY:
2053 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2055 /* it's for sure, some broken apps don't check for error codes */
2056 put_user(0, (int *)arg);
2059 return put_user(res, (int *)arg);
2060 case SNDCTL_DSP_PROFILE:
2061 return 0; /* silently ignore */
2063 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2068 static ssize_t snd_pcm_oss_read(struct file *file, char *buf, size_t count, loff_t *offset)
2070 snd_pcm_oss_file_t *pcm_oss_file;
2071 snd_pcm_substream_t *substream;
2073 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2074 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2075 if (substream == NULL)
2078 return snd_pcm_oss_read1(substream, buf, count);
2081 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2082 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2088 static ssize_t snd_pcm_oss_write(struct file *file, const char *buf, size_t count, loff_t *offset)
2090 snd_pcm_oss_file_t *pcm_oss_file;
2091 snd_pcm_substream_t *substream;
2094 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2095 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2096 if (substream == NULL)
2098 up(&file->f_dentry->d_inode->i_sem);
2099 result = snd_pcm_oss_write1(substream, buf, count);
2100 down(&file->f_dentry->d_inode->i_sem);
2102 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2107 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2109 snd_pcm_runtime_t *runtime = substream->runtime;
2110 if (atomic_read(&runtime->mmap_count))
2111 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2113 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2116 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2118 snd_pcm_runtime_t *runtime = substream->runtime;
2119 if (atomic_read(&runtime->mmap_count))
2120 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2122 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2125 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2127 snd_pcm_oss_file_t *pcm_oss_file;
2129 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2131 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return 0);
2133 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2134 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2137 if (psubstream != NULL) {
2138 snd_pcm_runtime_t *runtime = psubstream->runtime;
2139 poll_wait(file, &runtime->sleep, wait);
2140 snd_pcm_stream_lock_irq(psubstream);
2141 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2142 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2143 snd_pcm_oss_playback_ready(psubstream)))
2144 mask |= POLLOUT | POLLWRNORM;
2145 snd_pcm_stream_unlock_irq(psubstream);
2147 if (csubstream != NULL) {
2148 snd_pcm_runtime_t *runtime = csubstream->runtime;
2149 enum sndrv_pcm_state ostate;
2150 poll_wait(file, &runtime->sleep, wait);
2151 snd_pcm_stream_lock_irq(csubstream);
2152 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2153 snd_pcm_oss_capture_ready(csubstream))
2154 mask |= POLLIN | POLLRDNORM;
2155 snd_pcm_stream_unlock_irq(csubstream);
2156 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2157 snd_pcm_oss_file_t ofile;
2158 memset(&ofile, 0, sizeof(ofile));
2159 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2160 runtime->oss.trigger = 0;
2161 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2168 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2170 snd_pcm_oss_file_t *pcm_oss_file;
2171 snd_pcm_substream_t *substream = NULL;
2172 snd_pcm_runtime_t *runtime;
2176 printk("pcm_oss: mmap begin\n");
2178 pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2179 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2180 case VM_READ | VM_WRITE:
2181 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2186 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2189 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2194 /* set VM_READ access as well to fix memset() routines that do
2195 reads before writes (to improve performance) */
2196 area->vm_flags |= VM_READ;
2197 if (substream == NULL)
2199 runtime = substream->runtime;
2200 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2202 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2203 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2207 if (runtime->oss.params) {
2208 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2211 if (runtime->oss.plugin_first != NULL)
2214 if (area->vm_pgoff != 0)
2217 err = snd_pcm_mmap_data(substream, file, area);
2220 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2221 runtime->silence_threshold = 0;
2222 runtime->silence_size = 0;
2224 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2226 /* In mmap mode we never stop */
2227 runtime->stop_threshold = runtime->boundary;
2236 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2237 snd_info_buffer_t * buffer)
2239 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2240 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2241 down(&pstr->oss.setup_mutex);
2243 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2247 setup->disable ? " disable" : "",
2248 setup->direct ? " direct" : "",
2249 setup->block ? " block" : "",
2250 setup->nonblock ? " non-block" : "",
2251 setup->partialfrag ? " partial-frag" : "",
2252 setup->nosilence ? " no-silence" : "");
2253 setup = setup->next;
2255 up(&pstr->oss.setup_mutex);
2258 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2261 snd_pcm_substream_t *substream;
2262 snd_pcm_oss_setup_t *setup, *setupn;
2264 for (idx = 0, substream = pstr->substream;
2265 idx < pstr->substream_count; idx++, substream = substream->next)
2266 substream->oss.setup = NULL;
2267 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2268 setup; setup = setupn) {
2269 setupn = setup->next;
2270 kfree(setup->task_name);
2273 pstr->oss.setup_list = NULL;
2276 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2277 snd_info_buffer_t * buffer)
2279 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2280 char line[512], str[32], task_name[32], *ptr;
2282 snd_pcm_oss_setup_t *setup, *setup1, template;
2284 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2285 down(&pstr->oss.setup_mutex);
2286 memset(&template, 0, sizeof(template));
2287 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2288 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2289 snd_pcm_oss_proc_free_setup_list(pstr);
2290 up(&pstr->oss.setup_mutex);
2293 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2294 if (!strcmp(setup->task_name, task_name)) {
2299 ptr = snd_info_get_str(str, ptr, sizeof(str));
2300 template.periods = simple_strtoul(str, NULL, 10);
2301 ptr = snd_info_get_str(str, ptr, sizeof(str));
2302 template.period_size = simple_strtoul(str, NULL, 10);
2303 for (idx1 = 31; idx1 >= 0; idx1--)
2304 if (template.period_size & (1 << idx1))
2306 for (idx1--; idx1 >= 0; idx1--)
2307 template.period_size &= ~(1 << idx1);
2309 ptr = snd_info_get_str(str, ptr, sizeof(str));
2310 if (!strcmp(str, "disable")) {
2311 template.disable = 1;
2312 } else if (!strcmp(str, "direct")) {
2313 template.direct = 1;
2314 } else if (!strcmp(str, "block")) {
2316 } else if (!strcmp(str, "non-block")) {
2317 template.nonblock = 1;
2318 } else if (!strcmp(str, "partial-frag")) {
2319 template.partialfrag = 1;
2320 } else if (!strcmp(str, "no-silence")) {
2321 template.nosilence = 1;
2324 if (setup == NULL) {
2325 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2327 if (pstr->oss.setup_list == NULL) {
2328 pstr->oss.setup_list = setup;
2330 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2331 setup1->next = setup;
2333 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2335 buffer->error = -ENOMEM;
2340 up(&pstr->oss.setup_mutex);
2344 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2347 for (stream = 0; stream < 2; ++stream) {
2348 snd_info_entry_t *entry;
2349 snd_pcm_str_t *pstr = &pcm->streams[stream];
2350 if (pstr->substream_count == 0)
2352 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2353 entry->content = SNDRV_INFO_CONTENT_TEXT;
2354 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2355 entry->c.text.read_size = 8192;
2356 entry->c.text.read = snd_pcm_oss_proc_read;
2357 entry->c.text.write_size = 8192;
2358 entry->c.text.write = snd_pcm_oss_proc_write;
2359 entry->private_data = pstr;
2360 if (snd_info_register(entry) < 0) {
2361 snd_info_free_entry(entry);
2365 pstr->oss.proc_entry = entry;
2369 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2372 for (stream = 0; stream < 2; ++stream) {
2373 snd_pcm_str_t *pstr = &pcm->streams[stream];
2374 if (pstr->oss.proc_entry) {
2375 snd_info_unregister(pstr->oss.proc_entry);
2376 pstr->oss.proc_entry = NULL;
2377 snd_pcm_oss_proc_free_setup_list(pstr);
2386 static struct file_operations snd_pcm_oss_f_reg =
2388 .owner = THIS_MODULE,
2389 .read = snd_pcm_oss_read,
2390 .write = snd_pcm_oss_write,
2391 .open = snd_pcm_oss_open,
2392 .release = snd_pcm_oss_release,
2393 .poll = snd_pcm_oss_poll,
2394 .ioctl = snd_pcm_oss_ioctl,
2395 .mmap = snd_pcm_oss_mmap,
2398 static snd_minor_t snd_pcm_oss_reg =
2400 .comment = "digital audio",
2401 .f_ops = &snd_pcm_oss_f_reg,
2404 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2407 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2408 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2409 pcm->card, index, &snd_pcm_oss_reg,
2411 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2415 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2418 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2421 register_oss_dsp(pcm, 0);
2422 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2423 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2424 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2425 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2426 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2427 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2432 pcm->oss.reg_mask |= 1;
2434 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2435 register_oss_dsp(pcm, 1);
2437 pcm->oss.reg_mask |= 2;
2441 snd_pcm_oss_proc_init(pcm);
2446 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2449 if (pcm->oss.reg_mask & 1) {
2450 pcm->oss.reg_mask &= ~1;
2451 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2454 if (pcm->oss.reg_mask & 2) {
2455 pcm->oss.reg_mask &= ~2;
2456 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2463 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2465 snd_pcm_oss_disconnect_minor(pcm);
2467 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2468 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2469 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2473 snd_pcm_oss_proc_done(pcm);
2478 static snd_pcm_notify_t snd_pcm_oss_notify =
2480 .n_register = snd_pcm_oss_register_minor,
2481 .n_disconnect = snd_pcm_oss_disconnect_minor,
2482 .n_unregister = snd_pcm_oss_unregister_minor,
2485 static int __init alsa_pcm_oss_init(void)
2490 /* check device map table */
2491 for (i = 0; i < SNDRV_CARDS; i++) {
2492 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2493 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2496 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2497 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2501 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2506 static void __exit alsa_pcm_oss_exit(void)
2508 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2511 module_init(alsa_pcm_oss_init)
2512 module_exit(alsa_pcm_oss_exit)
2516 /* format is: snd-pcm-oss=dsp_map,adsp_map[,nonblock_open] */
2518 static int __init alsa_pcm_oss_setup(char *str)
2520 static unsigned __initdata nr_dev = 0;
2522 if (nr_dev >= SNDRV_CARDS)
2524 (void)(get_option(&str,&dsp_map[nr_dev]) == 2 &&
2525 get_option(&str,&adsp_map[nr_dev]) == 2);
2526 (void)(get_option(&str,&nonblock_open) == 2);
2531 __setup("snd-pcm-oss=", alsa_pcm_oss_setup);
2533 #endif /* !MODULE */