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;
52 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54 MODULE_LICENSE("GPL");
55 module_param_array(dsp_map, int, boot_devs, 0444);
56 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map, int, boot_devs, 0444);
58 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59 module_param(nonblock_open, bool, 0644);
60 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
64 extern int snd_mixer_oss_ioctl_card(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, NULL)) * 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, NULL));
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, NULL));
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, NULL);
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, NULL);
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, NULL);
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, NULL);
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, NULL);
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, NULL);
446 snd_assert(err >= 0, goto failure);
448 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
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, NULL);
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, NULL);
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, NULL);
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, NULL);
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 __user *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 __user *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, NULL);
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, NULL);
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, NULL);
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 __user *buffers[runtime->channels];
1069 memset(buffers, 0, runtime->channels * sizeof(void *));
1070 snd_pcm_lib_writev(substream, 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, NULL);
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, NULL);
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;
1183 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1185 if (atomic_read(&substream->runtime->mmap_count)) {
1188 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1189 direct = (setup != NULL && setup->direct);
1192 return AFMT_MU_LAW | AFMT_U8 |
1193 AFMT_S16_LE | AFMT_S16_BE |
1194 AFMT_S8 | AFMT_U16_LE |
1196 params = kmalloc(sizeof(*params), GFP_KERNEL);
1199 _snd_pcm_hw_params_any(params);
1200 err = snd_pcm_hw_refine(substream, params);
1201 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1203 snd_assert(err >= 0, return err);
1204 for (fmt = 0; fmt < 32; ++fmt) {
1205 if (snd_mask_test(&format_mask, fmt)) {
1206 int f = snd_pcm_oss_format_to(fmt);
1214 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1218 if (format != AFMT_QUERY) {
1219 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1220 if (!(formats & format))
1222 for (idx = 1; idx >= 0; --idx) {
1223 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1224 snd_pcm_runtime_t *runtime;
1225 if (substream == NULL)
1227 runtime = substream->runtime;
1228 if (runtime->oss.format != format) {
1229 runtime->oss.params = 1;
1230 runtime->oss.format = format;
1234 return snd_pcm_oss_get_format(pcm_oss_file);
1237 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1239 snd_pcm_substream_t *substream;
1242 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1244 return substream->runtime->oss.format;
1247 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1249 snd_pcm_runtime_t *runtime;
1251 if (substream == NULL)
1253 runtime = substream->runtime;
1254 if (subdivide == 0) {
1255 subdivide = runtime->oss.subdivision;
1260 if (runtime->oss.subdivision || runtime->oss.fragshift)
1262 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1263 subdivide != 8 && subdivide != 16)
1265 runtime->oss.subdivision = subdivide;
1266 runtime->oss.params = 1;
1270 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1272 int err = -EINVAL, idx;
1274 for (idx = 1; idx >= 0; --idx) {
1275 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1276 if (substream == NULL)
1278 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1284 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1286 snd_pcm_runtime_t *runtime;
1288 if (substream == NULL)
1290 runtime = substream->runtime;
1291 if (runtime->oss.subdivision || runtime->oss.fragshift)
1293 runtime->oss.fragshift = val & 0xffff;
1294 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1295 if (runtime->oss.fragshift < 4) /* < 16 */
1296 runtime->oss.fragshift = 4;
1297 if (runtime->oss.maxfrags < 2)
1298 runtime->oss.maxfrags = 2;
1299 runtime->oss.params = 1;
1303 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1305 int err = -EINVAL, idx;
1307 for (idx = 1; idx >= 0; --idx) {
1308 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1309 if (substream == NULL)
1311 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1317 static int snd_pcm_oss_nonblock(struct file * file)
1319 file->f_flags |= O_NONBLOCK;
1323 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1326 if (substream == NULL) {
1327 res &= ~DSP_CAP_DUPLEX;
1330 #ifdef DSP_CAP_MULTI
1331 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1332 if (substream->pstr->substream_count > 1)
1333 res |= DSP_CAP_MULTI;
1335 /* DSP_CAP_REALTIME is set all times: */
1336 /* all ALSA drivers can return actual pointer in ring buffer */
1337 #if defined(DSP_CAP_REALTIME) && 0
1339 snd_pcm_runtime_t *runtime = substream->runtime;
1340 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1341 res &= ~DSP_CAP_REALTIME;
1347 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1351 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1352 for (idx = 0; idx < 2; idx++) {
1353 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1354 result = snd_pcm_oss_get_caps1(substream, result);
1356 result |= 0x0001; /* revision - same as SB AWE 64 */
1360 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1362 snd_pcm_runtime_t *runtime = substream->runtime;
1363 snd_pcm_uframes_t appl_ptr;
1364 appl_ptr = hw_ptr + runtime->buffer_size;
1365 appl_ptr %= runtime->boundary;
1366 runtime->control->appl_ptr = appl_ptr;
1369 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1371 snd_pcm_runtime_t *runtime;
1372 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1376 printk("pcm_oss: trigger = 0x%x\n", trigger);
1379 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1380 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1383 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1387 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1391 runtime = psubstream->runtime;
1392 if (trigger & PCM_ENABLE_OUTPUT) {
1393 if (runtime->oss.trigger)
1395 if (atomic_read(&psubstream->runtime->mmap_count))
1396 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1397 runtime->oss.trigger = 1;
1398 runtime->start_threshold = 1;
1399 cmd = SNDRV_PCM_IOCTL_START;
1401 if (!runtime->oss.trigger)
1403 runtime->oss.trigger = 0;
1404 runtime->start_threshold = runtime->boundary;
1405 cmd = SNDRV_PCM_IOCTL_DROP;
1406 runtime->oss.prepare = 1;
1408 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, NULL);
1414 runtime = csubstream->runtime;
1415 if (trigger & PCM_ENABLE_INPUT) {
1416 if (runtime->oss.trigger)
1418 runtime->oss.trigger = 1;
1419 runtime->start_threshold = 1;
1420 cmd = SNDRV_PCM_IOCTL_START;
1422 if (!runtime->oss.trigger)
1424 runtime->oss.trigger = 0;
1425 runtime->start_threshold = runtime->boundary;
1426 cmd = SNDRV_PCM_IOCTL_DROP;
1427 runtime->oss.prepare = 1;
1429 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1437 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1439 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1442 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1443 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1444 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1445 result |= PCM_ENABLE_OUTPUT;
1446 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1447 result |= PCM_ENABLE_INPUT;
1451 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1453 snd_pcm_substream_t *substream;
1454 snd_pcm_runtime_t *runtime;
1455 snd_pcm_sframes_t delay;
1458 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459 if (substream == NULL)
1461 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1463 runtime = substream->runtime;
1464 if (runtime->oss.params || runtime->oss.prepare)
1466 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1468 delay = 0; /* hack for broken OSS applications */
1471 return snd_pcm_oss_bytes(substream, delay);
1474 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info __user * _info)
1476 snd_pcm_substream_t *substream;
1477 snd_pcm_runtime_t *runtime;
1478 snd_pcm_sframes_t delay;
1480 struct count_info info;
1485 substream = pcm_oss_file->streams[stream];
1486 if (substream == NULL)
1488 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1490 runtime = substream->runtime;
1491 if (runtime->oss.params || runtime->oss.prepare) {
1492 memset(&info, 0, sizeof(info));
1493 if (copy_to_user(_info, &info, sizeof(info)))
1497 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1498 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1499 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1504 fixup = runtime->oss.buffer_used;
1507 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1508 fixup = -runtime->oss.buffer_used;
1512 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1513 if (atomic_read(&runtime->mmap_count)) {
1514 snd_pcm_sframes_t n;
1515 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1517 n += runtime->boundary;
1518 info.blocks = n / runtime->period_size;
1519 runtime->oss.prev_hw_ptr_interrupt = delay;
1520 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1521 snd_pcm_oss_simulate_fill(substream, delay);
1522 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1524 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1525 info.blocks = delay / runtime->oss.period_bytes;
1526 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1527 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1529 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1531 if (copy_to_user(_info, &info, sizeof(info)))
1536 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1538 snd_pcm_substream_t *substream;
1539 snd_pcm_runtime_t *runtime;
1540 snd_pcm_sframes_t avail;
1542 struct audio_buf_info info;
1547 substream = pcm_oss_file->streams[stream];
1548 if (substream == NULL)
1550 runtime = substream->runtime;
1552 if (runtime->oss.params &&
1553 (err = snd_pcm_oss_change_params(substream)) < 0)
1556 info.fragsize = runtime->oss.period_bytes;
1557 info.fragstotal = runtime->periods;
1558 if (runtime->oss.prepare) {
1559 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1560 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1561 info.fragments = runtime->oss.periods;
1567 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1568 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1569 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1570 avail = runtime->buffer_size;
1574 avail = runtime->buffer_size - avail;
1575 fixup = -runtime->oss.buffer_used;
1578 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1579 fixup = runtime->oss.buffer_used;
1583 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1584 info.fragments = info.bytes / runtime->oss.period_bytes;
1588 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1590 if (copy_to_user(_info, &info, sizeof(info)))
1595 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1597 // it won't be probably implemented
1598 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1602 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1604 const char *ptr, *ptrl;
1605 snd_pcm_oss_setup_t *setup;
1607 down(&pcm->streams[stream].oss.setup_mutex);
1608 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1609 if (!strcmp(setup->task_name, task_name)) {
1610 up(&pcm->streams[stream].oss.setup_mutex);
1614 ptr = ptrl = task_name;
1620 if (ptrl == task_name) {
1624 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1625 if (!strcmp(setup->task_name, ptrl)) {
1626 up(&pcm->streams[stream].oss.setup_mutex);
1631 up(&pcm->streams[stream].oss.setup_mutex);
1635 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1636 snd_pcm_oss_setup_t *setup,
1639 snd_pcm_runtime_t *runtime;
1641 substream->oss.oss = 1;
1642 substream->oss.setup = setup;
1643 runtime = substream->runtime;
1644 runtime->oss.params = 1;
1645 runtime->oss.trigger = 1;
1646 runtime->oss.rate = 8000;
1647 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1648 case SNDRV_MINOR_OSS_PCM_8:
1649 runtime->oss.format = AFMT_U8;
1651 case SNDRV_MINOR_OSS_PCM_16:
1652 runtime->oss.format = AFMT_S16_LE;
1655 runtime->oss.format = AFMT_MU_LAW;
1657 runtime->oss.channels = 1;
1658 runtime->oss.fragshift = 0;
1659 runtime->oss.maxfrags = 0;
1660 runtime->oss.subdivision = 0;
1663 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1665 snd_pcm_runtime_t *runtime;
1666 runtime = substream->runtime;
1667 if (runtime->oss.buffer)
1668 vfree(runtime->oss.buffer);
1669 snd_pcm_oss_plugin_clear(substream);
1670 substream->oss.file = NULL;
1671 substream->oss.oss = 0;
1674 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1677 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1678 for (cidx = 0; cidx < 2; ++cidx) {
1679 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1680 snd_pcm_runtime_t *runtime;
1681 if (substream == NULL)
1683 runtime = substream->runtime;
1685 snd_pcm_stream_lock_irq(substream);
1686 if (snd_pcm_running(substream))
1687 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1688 snd_pcm_stream_unlock_irq(substream);
1689 if (substream->open_flag) {
1690 if (substream->ops->hw_free != NULL)
1691 substream->ops->hw_free(substream);
1692 substream->ops->close(substream);
1693 substream->open_flag = 0;
1695 substream->ffile = NULL;
1696 snd_pcm_oss_release_substream(substream);
1697 snd_pcm_release_substream(substream);
1699 kfree(pcm_oss_file);
1703 static int snd_pcm_oss_open_file(struct file *file,
1705 snd_pcm_oss_file_t **rpcm_oss_file,
1707 snd_pcm_oss_setup_t *psetup,
1708 snd_pcm_oss_setup_t *csetup)
1711 snd_pcm_oss_file_t *pcm_oss_file;
1712 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1713 unsigned int f_mode = file->f_mode;
1715 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1716 *rpcm_oss_file = NULL;
1718 pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1719 if (pcm_oss_file == NULL)
1722 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1723 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1724 f_mode = FMODE_WRITE;
1725 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1726 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1727 &psubstream)) < 0) {
1728 snd_pcm_oss_release_file(pcm_oss_file);
1731 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1733 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1734 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1735 &csubstream)) < 0) {
1736 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1737 snd_pcm_oss_release_file(pcm_oss_file);
1743 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1746 if (psubstream == NULL && csubstream == NULL) {
1747 snd_pcm_oss_release_file(pcm_oss_file);
1750 if (psubstream != NULL) {
1751 psubstream->oss.file = pcm_oss_file;
1752 err = snd_pcm_hw_constraints_init(psubstream);
1754 snd_printd("snd_pcm_hw_constraint_init failed\n");
1755 snd_pcm_oss_release_file(pcm_oss_file);
1758 if ((err = psubstream->ops->open(psubstream)) < 0) {
1759 snd_pcm_oss_release_file(pcm_oss_file);
1762 psubstream->open_flag = 1;
1763 err = snd_pcm_hw_constraints_complete(psubstream);
1765 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1766 snd_pcm_oss_release_file(pcm_oss_file);
1769 psubstream->ffile = file;
1770 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1772 if (csubstream != NULL) {
1773 csubstream->oss.file = pcm_oss_file;
1774 err = snd_pcm_hw_constraints_init(csubstream);
1776 snd_printd("snd_pcm_hw_constraint_init failed\n");
1777 snd_pcm_oss_release_file(pcm_oss_file);
1780 if ((err = csubstream->ops->open(csubstream)) < 0) {
1781 snd_pcm_oss_release_file(pcm_oss_file);
1784 csubstream->open_flag = 1;
1785 err = snd_pcm_hw_constraints_complete(csubstream);
1787 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1788 snd_pcm_oss_release_file(pcm_oss_file);
1791 csubstream->ffile = file;
1792 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1795 file->private_data = pcm_oss_file;
1796 *rpcm_oss_file = pcm_oss_file;
1801 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1803 int minor = iminor(inode);
1804 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1809 snd_pcm_oss_file_t *pcm_oss_file;
1810 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1813 static char printed_comm[16];
1815 if (strncmp(printed_comm, current->comm, 16)) {
1816 printk(KERN_DEBUG "application %s uses obsolete OSS audio interface\n",
1818 memcpy(printed_comm, current->comm, 16);
1821 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1822 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1823 adsp_map[cardnum] : dsp_map[cardnum];
1825 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1830 err = snd_card_file_add(pcm->card, file);
1833 if (!try_module_get(pcm->card->module)) {
1837 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1841 if (file->f_mode & FMODE_WRITE)
1842 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1843 if (file->f_mode & FMODE_READ)
1844 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1846 nonblock = !!(file->f_flags & O_NONBLOCK);
1847 if (psetup && !psetup->disable) {
1848 if (psetup->nonblock)
1850 else if (psetup->block)
1852 } else if (csetup && !csetup->disable) {
1853 if (csetup->nonblock)
1855 else if (csetup->block)
1859 nonblock = nonblock_open;
1861 init_waitqueue_entry(&wait, current);
1862 add_wait_queue(&pcm->open_wait, &wait);
1863 down(&pcm->open_mutex);
1865 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1866 minor, psetup, csetup);
1869 if (err == -EAGAIN) {
1876 set_current_state(TASK_INTERRUPTIBLE);
1877 up(&pcm->open_mutex);
1879 down(&pcm->open_mutex);
1880 if (signal_pending(current)) {
1885 remove_wait_queue(&pcm->open_wait, &wait);
1886 up(&pcm->open_mutex);
1892 module_put(pcm->card->module);
1894 snd_card_file_remove(pcm->card, file);
1899 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1902 snd_pcm_substream_t *substream;
1903 snd_pcm_oss_file_t *pcm_oss_file;
1905 pcm_oss_file = file->private_data;
1906 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1907 if (substream == NULL)
1908 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1909 snd_assert(substream != NULL, return -ENXIO);
1910 pcm = substream->pcm;
1911 snd_pcm_oss_sync(pcm_oss_file);
1912 down(&pcm->open_mutex);
1913 snd_pcm_oss_release_file(pcm_oss_file);
1914 up(&pcm->open_mutex);
1915 wake_up(&pcm->open_wait);
1916 module_put(pcm->card->module);
1917 snd_card_file_remove(pcm->card, file);
1921 static inline int _snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1922 unsigned int cmd, unsigned long arg)
1924 snd_pcm_oss_file_t *pcm_oss_file;
1925 int __user *p = (int __user *)arg;
1928 pcm_oss_file = file->private_data;
1929 if (cmd == OSS_GETVERSION)
1930 return put_user(SNDRV_OSS_VERSION, p);
1931 if (cmd == OSS_ALSAEMULVER)
1932 return put_user(1, p);
1933 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1934 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1935 snd_pcm_substream_t *substream;
1937 for (idx = 0; idx < 2; ++idx) {
1938 substream = pcm_oss_file->streams[idx];
1939 if (substream != NULL)
1942 snd_assert(substream != NULL, return -ENXIO);
1943 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1946 if (((cmd >> 8) & 0xff) != 'P')
1949 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1952 case SNDCTL_DSP_RESET:
1953 return snd_pcm_oss_reset(pcm_oss_file);
1954 case SNDCTL_DSP_SYNC:
1955 return snd_pcm_oss_sync(pcm_oss_file);
1956 case SNDCTL_DSP_SPEED:
1957 if (get_user(res, p))
1959 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1961 return put_user(res, p);
1962 case SOUND_PCM_READ_RATE:
1963 res = snd_pcm_oss_get_rate(pcm_oss_file);
1966 return put_user(res, p);
1967 case SNDCTL_DSP_STEREO:
1968 if (get_user(res, p))
1970 res = res > 0 ? 2 : 1;
1971 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1973 return put_user(--res, p);
1974 case SNDCTL_DSP_GETBLKSIZE:
1975 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1978 return put_user(res, p);
1979 case SNDCTL_DSP_SETFMT:
1980 if (get_user(res, p))
1982 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1985 return put_user(res, p);
1986 case SOUND_PCM_READ_BITS:
1987 res = snd_pcm_oss_get_format(pcm_oss_file);
1990 return put_user(res, p);
1991 case SNDCTL_DSP_CHANNELS:
1992 if (get_user(res, p))
1994 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1997 return put_user(res, p);
1998 case SOUND_PCM_READ_CHANNELS:
1999 res = snd_pcm_oss_get_channels(pcm_oss_file);
2002 return put_user(res, p);
2003 case SOUND_PCM_WRITE_FILTER:
2004 case SOUND_PCM_READ_FILTER:
2006 case SNDCTL_DSP_POST:
2007 return snd_pcm_oss_post(pcm_oss_file);
2008 case SNDCTL_DSP_SUBDIVIDE:
2009 if (get_user(res, p))
2011 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2014 return put_user(res, p);
2015 case SNDCTL_DSP_SETFRAGMENT:
2016 if (get_user(res, p))
2018 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2019 case SNDCTL_DSP_GETFMTS:
2020 res = snd_pcm_oss_get_formats(pcm_oss_file);
2023 return put_user(res, p);
2024 case SNDCTL_DSP_GETOSPACE:
2025 case SNDCTL_DSP_GETISPACE:
2026 return snd_pcm_oss_get_space(pcm_oss_file,
2027 cmd == SNDCTL_DSP_GETISPACE ?
2028 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2029 (struct audio_buf_info __user *) arg);
2030 case SNDCTL_DSP_NONBLOCK:
2031 return snd_pcm_oss_nonblock(file);
2032 case SNDCTL_DSP_GETCAPS:
2033 res = snd_pcm_oss_get_caps(pcm_oss_file);
2036 return put_user(res, p);
2037 case SNDCTL_DSP_GETTRIGGER:
2038 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2041 return put_user(res, p);
2042 case SNDCTL_DSP_SETTRIGGER:
2043 if (get_user(res, p))
2045 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2046 case SNDCTL_DSP_GETIPTR:
2047 case SNDCTL_DSP_GETOPTR:
2048 return snd_pcm_oss_get_ptr(pcm_oss_file,
2049 cmd == SNDCTL_DSP_GETIPTR ?
2050 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2051 (struct count_info __user *) arg);
2052 case SNDCTL_DSP_MAPINBUF:
2053 case SNDCTL_DSP_MAPOUTBUF:
2054 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2055 cmd == SNDCTL_DSP_MAPINBUF ?
2056 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2057 (struct buffmem_desc __user *) arg);
2058 case SNDCTL_DSP_SETSYNCRO:
2059 /* stop DMA now.. */
2061 case SNDCTL_DSP_SETDUPLEX:
2062 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2065 case SNDCTL_DSP_GETODELAY:
2066 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2068 /* it's for sure, some broken apps don't check for error codes */
2072 return put_user(res, p);
2073 case SNDCTL_DSP_PROFILE:
2074 return 0; /* silently ignore */
2076 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2081 /* FIXME: need to unlock BKL to allow preemption */
2082 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
2083 unsigned int cmd, unsigned long arg)
2087 err = _snd_pcm_oss_ioctl(inode, file, cmd, arg);
2092 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2094 snd_pcm_oss_file_t *pcm_oss_file;
2095 snd_pcm_substream_t *substream;
2097 pcm_oss_file = file->private_data;
2098 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2099 if (substream == NULL)
2102 return snd_pcm_oss_read1(substream, buf, count);
2105 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2106 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2112 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2114 snd_pcm_oss_file_t *pcm_oss_file;
2115 snd_pcm_substream_t *substream;
2118 pcm_oss_file = file->private_data;
2119 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2120 if (substream == NULL)
2122 up(&file->f_dentry->d_inode->i_sem);
2123 result = snd_pcm_oss_write1(substream, buf, count);
2124 down(&file->f_dentry->d_inode->i_sem);
2126 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2131 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2133 snd_pcm_runtime_t *runtime = substream->runtime;
2134 if (atomic_read(&runtime->mmap_count))
2135 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2137 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2140 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2142 snd_pcm_runtime_t *runtime = substream->runtime;
2143 if (atomic_read(&runtime->mmap_count))
2144 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2146 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2149 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2151 snd_pcm_oss_file_t *pcm_oss_file;
2153 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2155 pcm_oss_file = file->private_data;
2157 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2158 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2161 if (psubstream != NULL) {
2162 snd_pcm_runtime_t *runtime = psubstream->runtime;
2163 poll_wait(file, &runtime->sleep, wait);
2164 snd_pcm_stream_lock_irq(psubstream);
2165 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2166 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2167 snd_pcm_oss_playback_ready(psubstream)))
2168 mask |= POLLOUT | POLLWRNORM;
2169 snd_pcm_stream_unlock_irq(psubstream);
2171 if (csubstream != NULL) {
2172 snd_pcm_runtime_t *runtime = csubstream->runtime;
2173 enum sndrv_pcm_state ostate;
2174 poll_wait(file, &runtime->sleep, wait);
2175 snd_pcm_stream_lock_irq(csubstream);
2176 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2177 snd_pcm_oss_capture_ready(csubstream))
2178 mask |= POLLIN | POLLRDNORM;
2179 snd_pcm_stream_unlock_irq(csubstream);
2180 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2181 snd_pcm_oss_file_t ofile;
2182 memset(&ofile, 0, sizeof(ofile));
2183 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2184 runtime->oss.trigger = 0;
2185 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2192 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2194 snd_pcm_oss_file_t *pcm_oss_file;
2195 snd_pcm_substream_t *substream = NULL;
2196 snd_pcm_runtime_t *runtime;
2200 printk("pcm_oss: mmap begin\n");
2202 pcm_oss_file = file->private_data;
2203 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2204 case VM_READ | VM_WRITE:
2205 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2210 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2213 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2218 /* set VM_READ access as well to fix memset() routines that do
2219 reads before writes (to improve performance) */
2220 area->vm_flags |= VM_READ;
2221 if (substream == NULL)
2223 runtime = substream->runtime;
2224 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2226 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2227 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2231 if (runtime->oss.params) {
2232 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2235 if (runtime->oss.plugin_first != NULL)
2238 if (area->vm_pgoff != 0)
2241 err = snd_pcm_mmap_data(substream, file, area);
2244 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2245 runtime->silence_threshold = 0;
2246 runtime->silence_size = 0;
2248 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2250 /* In mmap mode we never stop */
2251 runtime->stop_threshold = runtime->boundary;
2260 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2261 snd_info_buffer_t * buffer)
2263 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2264 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2265 down(&pstr->oss.setup_mutex);
2267 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2271 setup->disable ? " disable" : "",
2272 setup->direct ? " direct" : "",
2273 setup->block ? " block" : "",
2274 setup->nonblock ? " non-block" : "",
2275 setup->partialfrag ? " partial-frag" : "",
2276 setup->nosilence ? " no-silence" : "");
2277 setup = setup->next;
2279 up(&pstr->oss.setup_mutex);
2282 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2285 snd_pcm_substream_t *substream;
2286 snd_pcm_oss_setup_t *setup, *setupn;
2288 for (idx = 0, substream = pstr->substream;
2289 idx < pstr->substream_count; idx++, substream = substream->next)
2290 substream->oss.setup = NULL;
2291 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2292 setup; setup = setupn) {
2293 setupn = setup->next;
2294 kfree(setup->task_name);
2297 pstr->oss.setup_list = NULL;
2300 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2301 snd_info_buffer_t * buffer)
2303 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2304 char line[256], str[32], task_name[32], *ptr;
2306 snd_pcm_oss_setup_t *setup, *setup1, template;
2308 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2309 down(&pstr->oss.setup_mutex);
2310 memset(&template, 0, sizeof(template));
2311 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2312 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2313 snd_pcm_oss_proc_free_setup_list(pstr);
2314 up(&pstr->oss.setup_mutex);
2317 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2318 if (!strcmp(setup->task_name, task_name)) {
2323 ptr = snd_info_get_str(str, ptr, sizeof(str));
2324 template.periods = simple_strtoul(str, NULL, 10);
2325 ptr = snd_info_get_str(str, ptr, sizeof(str));
2326 template.period_size = simple_strtoul(str, NULL, 10);
2327 for (idx1 = 31; idx1 >= 0; idx1--)
2328 if (template.period_size & (1 << idx1))
2330 for (idx1--; idx1 >= 0; idx1--)
2331 template.period_size &= ~(1 << idx1);
2333 ptr = snd_info_get_str(str, ptr, sizeof(str));
2334 if (!strcmp(str, "disable")) {
2335 template.disable = 1;
2336 } else if (!strcmp(str, "direct")) {
2337 template.direct = 1;
2338 } else if (!strcmp(str, "block")) {
2340 } else if (!strcmp(str, "non-block")) {
2341 template.nonblock = 1;
2342 } else if (!strcmp(str, "partial-frag")) {
2343 template.partialfrag = 1;
2344 } else if (!strcmp(str, "no-silence")) {
2345 template.nosilence = 1;
2348 if (setup == NULL) {
2349 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2351 if (pstr->oss.setup_list == NULL) {
2352 pstr->oss.setup_list = setup;
2354 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2355 setup1->next = setup;
2357 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2359 buffer->error = -ENOMEM;
2364 up(&pstr->oss.setup_mutex);
2368 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2371 for (stream = 0; stream < 2; ++stream) {
2372 snd_info_entry_t *entry;
2373 snd_pcm_str_t *pstr = &pcm->streams[stream];
2374 if (pstr->substream_count == 0)
2376 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2377 entry->content = SNDRV_INFO_CONTENT_TEXT;
2378 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2379 entry->c.text.read_size = 8192;
2380 entry->c.text.read = snd_pcm_oss_proc_read;
2381 entry->c.text.write_size = 8192;
2382 entry->c.text.write = snd_pcm_oss_proc_write;
2383 entry->private_data = pstr;
2384 if (snd_info_register(entry) < 0) {
2385 snd_info_free_entry(entry);
2389 pstr->oss.proc_entry = entry;
2393 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2396 for (stream = 0; stream < 2; ++stream) {
2397 snd_pcm_str_t *pstr = &pcm->streams[stream];
2398 if (pstr->oss.proc_entry) {
2399 snd_info_unregister(pstr->oss.proc_entry);
2400 pstr->oss.proc_entry = NULL;
2401 snd_pcm_oss_proc_free_setup_list(pstr);
2410 static struct file_operations snd_pcm_oss_f_reg =
2412 .owner = THIS_MODULE,
2413 .read = snd_pcm_oss_read,
2414 .write = snd_pcm_oss_write,
2415 .open = snd_pcm_oss_open,
2416 .release = snd_pcm_oss_release,
2417 .poll = snd_pcm_oss_poll,
2418 .ioctl = snd_pcm_oss_ioctl,
2419 .mmap = snd_pcm_oss_mmap,
2422 static snd_minor_t snd_pcm_oss_reg =
2424 .comment = "digital audio",
2425 .f_ops = &snd_pcm_oss_f_reg,
2428 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2431 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2432 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2433 pcm->card, index, &snd_pcm_oss_reg,
2435 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2439 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2442 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2445 register_oss_dsp(pcm, 0);
2446 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2447 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2448 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2449 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2450 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2451 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2456 pcm->oss.reg_mask |= 1;
2458 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2459 register_oss_dsp(pcm, 1);
2461 pcm->oss.reg_mask |= 2;
2465 snd_pcm_oss_proc_init(pcm);
2470 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2473 if (pcm->oss.reg_mask & 1) {
2474 pcm->oss.reg_mask &= ~1;
2475 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2478 if (pcm->oss.reg_mask & 2) {
2479 pcm->oss.reg_mask &= ~2;
2480 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2487 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2489 snd_pcm_oss_disconnect_minor(pcm);
2491 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2492 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2493 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2497 snd_pcm_oss_proc_done(pcm);
2502 static snd_pcm_notify_t snd_pcm_oss_notify =
2504 .n_register = snd_pcm_oss_register_minor,
2505 .n_disconnect = snd_pcm_oss_disconnect_minor,
2506 .n_unregister = snd_pcm_oss_unregister_minor,
2509 static int __init alsa_pcm_oss_init(void)
2514 /* check device map table */
2515 for (i = 0; i < SNDRV_CARDS; i++) {
2516 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2517 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2520 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2521 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2525 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2530 static void __exit alsa_pcm_oss_exit(void)
2532 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2535 module_init(alsa_pcm_oss_init)
2536 module_exit(alsa_pcm_oss_exit)