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 = 1;
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
52 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
53 MODULE_LICENSE("GPL");
54 module_param_array(dsp_map, int, NULL, 0444);
55 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
56 module_param_array(adsp_map, int, NULL, 0444);
57 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
58 module_param(nonblock_open, bool, 0644);
59 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
64 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
65 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
66 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
68 static inline mm_segment_t snd_enter_user(void)
70 mm_segment_t fs = get_fs();
75 static inline void snd_leave_user(mm_segment_t fs)
80 static int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
82 snd_pcm_runtime_t *runtime = substream->runtime;
83 snd_pcm_plugin_t *plugin, *next;
85 plugin = runtime->oss.plugin_first;
88 snd_pcm_plugin_free(plugin);
91 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
95 static int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
97 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
98 plugin->next = runtime->oss.plugin_first;
100 if (runtime->oss.plugin_first) {
101 runtime->oss.plugin_first->prev = plugin;
102 runtime->oss.plugin_first = plugin;
104 runtime->oss.plugin_last =
105 runtime->oss.plugin_first = plugin;
110 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
112 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
114 plugin->prev = runtime->oss.plugin_last;
115 if (runtime->oss.plugin_last) {
116 runtime->oss.plugin_last->next = plugin;
117 runtime->oss.plugin_last = plugin;
119 runtime->oss.plugin_last =
120 runtime->oss.plugin_first = plugin;
125 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
127 snd_pcm_runtime_t *runtime = substream->runtime;
128 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
129 long bytes = frames_to_bytes(runtime, frames);
130 if (buffer_size == runtime->oss.buffer_bytes)
132 #if BITS_PER_LONG >= 64
133 return runtime->oss.buffer_bytes * bytes / buffer_size;
136 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
138 div64_32(&bsize, buffer_size, &rem);
144 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
146 snd_pcm_runtime_t *runtime = substream->runtime;
147 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
148 if (buffer_size == runtime->oss.buffer_bytes)
149 return bytes_to_frames(runtime, bytes);
150 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
153 static int snd_pcm_oss_format_from(int format)
156 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
157 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
158 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
159 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
160 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
161 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
162 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
163 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
164 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
165 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
166 default: return SNDRV_PCM_FORMAT_U8;
170 static int snd_pcm_oss_format_to(int format)
173 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
174 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
175 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
176 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
177 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
178 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
179 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
180 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
181 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
182 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
183 default: return -EINVAL;
187 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream,
188 snd_pcm_hw_params_t *oss_params,
189 snd_pcm_hw_params_t *slave_params)
192 size_t oss_buffer_size, oss_period_size, oss_periods;
193 size_t min_period_size, max_period_size;
194 snd_pcm_runtime_t *runtime = substream->runtime;
195 size_t oss_frame_size;
197 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
198 params_channels(oss_params) / 8;
200 oss_buffer_size = snd_pcm_plug_client_size(substream,
201 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
202 oss_buffer_size = 1 << ld2(oss_buffer_size);
203 if (atomic_read(&runtime->mmap_count)) {
204 if (oss_buffer_size > runtime->oss.mmap_bytes)
205 oss_buffer_size = runtime->oss.mmap_bytes;
208 if (substream->oss.setup &&
209 substream->oss.setup->period_size > 16)
210 oss_period_size = substream->oss.setup->period_size;
211 else if (runtime->oss.fragshift) {
212 oss_period_size = 1 << runtime->oss.fragshift;
213 if (oss_period_size > oss_buffer_size / 2)
214 oss_period_size = oss_buffer_size / 2;
217 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
219 oss_period_size = oss_buffer_size;
221 oss_period_size /= 2;
222 } while (oss_period_size > bytes_per_sec);
223 if (runtime->oss.subdivision == 0) {
225 if (oss_period_size / sd > 4096)
227 if (oss_period_size / sd < 4096)
230 sd = runtime->oss.subdivision;
231 oss_period_size /= sd;
232 if (oss_period_size < 16)
233 oss_period_size = 16;
236 min_period_size = snd_pcm_plug_client_size(substream,
237 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
238 min_period_size *= oss_frame_size;
239 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
240 if (oss_period_size < min_period_size)
241 oss_period_size = min_period_size;
243 max_period_size = snd_pcm_plug_client_size(substream,
244 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
245 max_period_size *= oss_frame_size;
246 max_period_size = 1 << ld2(max_period_size);
247 if (oss_period_size > max_period_size)
248 oss_period_size = max_period_size;
250 oss_periods = oss_buffer_size / oss_period_size;
252 if (substream->oss.setup) {
253 if (substream->oss.setup->periods > 1)
254 oss_periods = substream->oss.setup->periods;
257 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
258 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
259 s = runtime->oss.maxfrags;
263 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
269 while (oss_period_size * oss_periods > oss_buffer_size)
270 oss_period_size /= 2;
272 snd_assert(oss_period_size >= 16, return -EINVAL);
273 runtime->oss.period_bytes = oss_period_size;
274 runtime->oss.period_frames = 1;
275 runtime->oss.periods = oss_periods;
279 static int choose_rate(snd_pcm_substream_t *substream,
280 snd_pcm_hw_params_t *params, unsigned int best_rate)
283 snd_pcm_hw_params_t *save;
284 unsigned int rate, prev;
286 save = kmalloc(sizeof(*save), GFP_KERNEL);
290 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
292 /* try multiples of the best rate */
295 if (it->max < rate || (it->max == rate && it->openmax))
297 if (it->min < rate || (it->min == rate && !it->openmin)) {
299 ret = snd_pcm_hw_param_set(substream, params,
300 SNDRV_PCM_HW_PARAM_RATE,
302 if (ret == (int)rate) {
314 /* not found, use the nearest rate */
316 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
319 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
321 snd_pcm_runtime_t *runtime = substream->runtime;
322 snd_pcm_hw_params_t *params, *sparams;
323 snd_pcm_sw_params_t *sw_params;
324 ssize_t oss_buffer_size, oss_period_size;
325 size_t oss_frame_size;
328 int format, sformat, n;
329 snd_mask_t sformat_mask;
332 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
333 params = kmalloc(sizeof(*params), GFP_KERNEL);
334 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
335 if (!sw_params || !params || !sparams) {
336 snd_printd("No memory\n");
341 if (atomic_read(&runtime->mmap_count)) {
344 snd_pcm_oss_setup_t *setup = substream->oss.setup;
345 direct = (setup != NULL && setup->direct);
348 _snd_pcm_hw_params_any(sparams);
349 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
350 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
351 snd_mask_none(&mask);
352 if (atomic_read(&runtime->mmap_count))
353 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
355 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
357 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
359 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
361 snd_printd("No usable accesses\n");
365 choose_rate(substream, sparams, runtime->oss.rate);
366 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
368 format = snd_pcm_oss_format_from(runtime->oss.format);
370 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
374 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
376 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
377 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
378 if (snd_mask_test(&sformat_mask, sformat) &&
379 snd_pcm_oss_format_to(sformat) >= 0)
382 if (sformat > SNDRV_PCM_FORMAT_LAST) {
383 snd_printd("Cannot find a format!!!\n");
388 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
389 snd_assert(err >= 0, goto failure);
392 memcpy(params, sparams, sizeof(*params));
394 _snd_pcm_hw_params_any(params);
395 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
396 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
397 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
398 snd_pcm_oss_format_from(runtime->oss.format), 0);
399 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
400 runtime->oss.channels, 0);
401 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
402 runtime->oss.rate, 0);
403 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
404 params_access(params), params_format(params),
405 params_channels(params), params_rate(params));
407 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
408 params_access(sparams), params_format(sparams),
409 params_channels(sparams), params_rate(sparams));
411 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
412 params_channels(params) / 8;
414 snd_pcm_oss_plugin_clear(substream);
416 /* add necessary plugins */
417 snd_pcm_oss_plugin_clear(substream);
418 if ((err = snd_pcm_plug_format_plugins(substream,
421 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
422 snd_pcm_oss_plugin_clear(substream);
425 if (runtime->oss.plugin_first) {
426 snd_pcm_plugin_t *plugin;
427 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
428 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
429 snd_pcm_oss_plugin_clear(substream);
432 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
433 err = snd_pcm_plugin_append(plugin);
435 err = snd_pcm_plugin_insert(plugin);
438 snd_pcm_oss_plugin_clear(substream);
444 err = snd_pcm_oss_period_size(substream, params, sparams);
448 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
449 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
450 snd_assert(err >= 0, goto failure);
452 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
453 runtime->oss.periods, NULL);
454 snd_assert(err >= 0, goto failure);
456 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
458 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
459 snd_printd("HW_PARAMS failed: %i\n", err);
463 memset(sw_params, 0, sizeof(*sw_params));
464 if (runtime->oss.trigger) {
465 sw_params->start_threshold = 1;
467 sw_params->start_threshold = runtime->boundary;
469 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
470 sw_params->stop_threshold = runtime->boundary;
472 sw_params->stop_threshold = runtime->buffer_size;
473 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
474 sw_params->period_step = 1;
475 sw_params->sleep_min = 0;
476 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
477 1 : runtime->period_size;
478 sw_params->xfer_align = 1;
479 if (atomic_read(&runtime->mmap_count) ||
480 (substream->oss.setup && substream->oss.setup->nosilence)) {
481 sw_params->silence_threshold = 0;
482 sw_params->silence_size = 0;
484 snd_pcm_uframes_t frames;
485 frames = runtime->period_size + 16;
486 if (frames > runtime->buffer_size)
487 frames = runtime->buffer_size;
488 sw_params->silence_threshold = frames;
489 sw_params->silence_size = frames;
492 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
493 snd_printd("SW_PARAMS failed: %i\n", err);
497 runtime->oss.periods = params_periods(sparams);
498 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
499 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
500 if (runtime->oss.plugin_first) {
501 err = snd_pcm_plug_alloc(substream, oss_period_size);
505 oss_period_size *= oss_frame_size;
507 oss_buffer_size = oss_period_size * runtime->oss.periods;
508 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
510 runtime->oss.period_bytes = oss_period_size;
511 runtime->oss.buffer_bytes = oss_buffer_size;
513 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
514 runtime->oss.period_bytes,
515 runtime->oss.buffer_bytes);
516 pdprintf("slave: period_size = %i, buffer_size = %i\n",
517 params_period_size(sparams),
518 params_buffer_size(sparams));
520 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
521 runtime->oss.channels = params_channels(params);
522 runtime->oss.rate = params_rate(params);
524 runtime->oss.params = 0;
525 runtime->oss.prepare = 1;
526 vfree(runtime->oss.buffer);
527 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
528 runtime->oss.buffer_used = 0;
529 if (runtime->dma_area)
530 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
532 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
542 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
545 snd_pcm_substream_t *asubstream = NULL, *substream;
547 for (idx = 0; idx < 2; idx++) {
548 substream = pcm_oss_file->streams[idx];
549 if (substream == NULL)
551 if (asubstream == NULL)
552 asubstream = substream;
553 if (substream->runtime->oss.params) {
554 err = snd_pcm_oss_change_params(substream);
559 snd_assert(asubstream != NULL, return -EIO);
561 *r_substream = asubstream;
565 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
568 snd_pcm_runtime_t *runtime = substream->runtime;
570 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
572 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
575 runtime->oss.prepare = 0;
576 runtime->oss.prev_hw_ptr_interrupt = 0;
577 runtime->oss.period_ptr = 0;
578 runtime->oss.buffer_used = 0;
583 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
585 snd_pcm_runtime_t *runtime;
588 if (substream == NULL)
590 runtime = substream->runtime;
591 if (runtime->oss.params) {
592 err = snd_pcm_oss_change_params(substream);
596 if (runtime->oss.prepare) {
597 err = snd_pcm_oss_prepare(substream);
604 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
606 snd_pcm_runtime_t *runtime;
607 snd_pcm_uframes_t frames;
611 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
614 runtime = substream->runtime;
615 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
617 /* in case of overrun, skip whole periods like OSS/Linux driver does */
618 /* until avail(delay) <= buffer_size */
619 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
620 frames /= runtime->period_size;
621 frames *= runtime->period_size;
622 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
629 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
631 snd_pcm_runtime_t *runtime = substream->runtime;
634 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
635 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
637 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
638 printk("pcm_oss: write: recovering from XRUN\n");
640 printk("pcm_oss: write: recovering from SUSPEND\n");
642 ret = snd_pcm_oss_prepare(substream);
648 fs = snd_enter_user();
649 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
652 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
654 if (ret != -EPIPE && ret != -ESTRPIPE)
656 /* test, if we can't store new data, because the stream */
657 /* has not been started */
658 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
664 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
666 snd_pcm_runtime_t *runtime = substream->runtime;
667 snd_pcm_sframes_t delay;
670 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
671 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
673 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
674 printk("pcm_oss: read: recovering from XRUN\n");
676 printk("pcm_oss: read: recovering from SUSPEND\n");
678 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
681 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
682 ret = snd_pcm_oss_prepare(substream);
686 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
691 fs = snd_enter_user();
692 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
695 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
698 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
699 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
705 if (ret != -ESTRPIPE)
711 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
713 snd_pcm_runtime_t *runtime = substream->runtime;
716 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
717 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
719 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
720 printk("pcm_oss: writev: recovering from XRUN\n");
722 printk("pcm_oss: writev: recovering from SUSPEND\n");
724 ret = snd_pcm_oss_prepare(substream);
730 fs = snd_enter_user();
731 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
734 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
736 if (ret != -EPIPE && ret != -ESTRPIPE)
739 /* test, if we can't store new data, because the stream */
740 /* has not been started */
741 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
747 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
749 snd_pcm_runtime_t *runtime = substream->runtime;
752 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
753 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
755 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
756 printk("pcm_oss: readv: recovering from XRUN\n");
758 printk("pcm_oss: readv: recovering from SUSPEND\n");
760 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
763 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
764 ret = snd_pcm_oss_prepare(substream);
770 fs = snd_enter_user();
771 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
774 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
776 if (ret != -EPIPE && ret != -ESTRPIPE)
782 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
784 snd_pcm_runtime_t *runtime = substream->runtime;
785 snd_pcm_sframes_t frames, frames1;
786 if (runtime->oss.plugin_first) {
787 snd_pcm_plugin_channel_t *channels;
788 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
790 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
792 buf = runtime->oss.buffer;
794 frames = bytes / oss_frame_bytes;
795 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
798 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
801 bytes = frames1 * oss_frame_bytes;
803 frames = bytes_to_frames(runtime, bytes);
804 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
807 bytes = frames_to_bytes(runtime, frames1);
812 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char __user *buf, size_t bytes)
816 snd_pcm_runtime_t *runtime = substream->runtime;
818 if (atomic_read(&runtime->mmap_count))
821 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
824 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
826 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
827 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
829 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
830 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
832 runtime->oss.buffer_used += tmp;
836 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
837 runtime->oss.buffer_used == runtime->oss.period_bytes) {
838 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
839 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
841 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
842 runtime->oss.bytes += tmp;
843 runtime->oss.period_ptr += tmp;
844 runtime->oss.period_ptr %= runtime->oss.period_bytes;
845 if (runtime->oss.period_ptr == 0 ||
846 runtime->oss.period_ptr == runtime->oss.buffer_used)
847 runtime->oss.buffer_used = 0;
848 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
849 return xfer > 0 ? xfer : -EAGAIN;
852 tmp = snd_pcm_oss_write2(substream, (const char *)buf, runtime->oss.period_bytes, 0);
854 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
855 runtime->oss.bytes += tmp;
859 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
860 tmp != runtime->oss.period_bytes)
867 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
869 snd_pcm_runtime_t *runtime = substream->runtime;
870 snd_pcm_sframes_t frames, frames1;
871 char __user *final_dst = (char __user *)buf;
872 if (runtime->oss.plugin_first) {
873 snd_pcm_plugin_channel_t *channels;
874 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
876 buf = runtime->oss.buffer;
877 frames = bytes / oss_frame_bytes;
878 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
881 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
884 bytes = frames1 * oss_frame_bytes;
885 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
888 frames = bytes_to_frames(runtime, bytes);
889 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
892 bytes = frames_to_bytes(runtime, frames1);
897 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char __user *buf, size_t bytes)
901 snd_pcm_runtime_t *runtime = substream->runtime;
903 if (atomic_read(&runtime->mmap_count))
906 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
909 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
910 if (runtime->oss.buffer_used == 0) {
911 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
913 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
914 runtime->oss.bytes += tmp;
915 runtime->oss.period_ptr = tmp;
916 runtime->oss.buffer_used = tmp;
919 if ((size_t) tmp > runtime->oss.buffer_used)
920 tmp = runtime->oss.buffer_used;
921 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
922 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
926 runtime->oss.buffer_used -= tmp;
928 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
930 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
931 runtime->oss.bytes += tmp;
940 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
942 snd_pcm_substream_t *substream;
944 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
945 if (substream != NULL) {
946 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
947 substream->runtime->oss.prepare = 1;
949 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
950 if (substream != NULL) {
951 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
952 substream->runtime->oss.prepare = 1;
957 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
959 snd_pcm_substream_t *substream;
962 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
963 if (substream != NULL) {
964 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
966 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
968 /* note: all errors from the start action are ignored */
969 /* OSS apps do not know, how to handle them */
973 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
975 snd_pcm_runtime_t *runtime;
980 runtime = substream->runtime;
981 init_waitqueue_entry(&wait, current);
982 add_wait_queue(&runtime->sleep, &wait);
984 printk("sync1: size = %li\n", size);
987 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
989 runtime->oss.buffer_used = 0;
993 if (result != 0 && result != -EAGAIN)
996 set_current_state(TASK_INTERRUPTIBLE);
997 snd_pcm_stream_lock_irq(substream);
998 res = runtime->status->state;
999 snd_pcm_stream_unlock_irq(substream);
1000 if (res != SNDRV_PCM_STATE_RUNNING) {
1001 set_current_state(TASK_RUNNING);
1004 res = schedule_timeout(10 * HZ);
1005 if (signal_pending(current)) {
1006 result = -ERESTARTSYS;
1010 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1015 remove_wait_queue(&runtime->sleep, &wait);
1019 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1022 unsigned int saved_f_flags;
1023 snd_pcm_substream_t *substream;
1024 snd_pcm_runtime_t *runtime;
1025 snd_pcm_format_t format;
1026 unsigned long width;
1029 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1030 if (substream != NULL) {
1031 runtime = substream->runtime;
1032 if (atomic_read(&runtime->mmap_count))
1034 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1036 format = snd_pcm_oss_format_from(runtime->oss.format);
1037 width = snd_pcm_format_physical_width(format);
1038 if (runtime->oss.buffer_used > 0) {
1040 printk("sync: buffer_used\n");
1042 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1043 snd_pcm_format_set_silence(format,
1044 runtime->oss.buffer + runtime->oss.buffer_used,
1046 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1049 } else if (runtime->oss.period_ptr > 0) {
1051 printk("sync: period_ptr\n");
1053 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1054 snd_pcm_format_set_silence(format,
1055 runtime->oss.buffer,
1057 err = snd_pcm_oss_sync1(substream, size);
1062 * The ALSA's period might be a bit large than OSS one.
1063 * Fill the remain portion of ALSA period with zeros.
1065 size = runtime->control->appl_ptr % runtime->period_size;
1067 size = runtime->period_size - size;
1068 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1069 size = (runtime->frame_bits * size) / 8;
1072 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1075 size1 /= runtime->sample_bits;
1076 snd_pcm_format_set_silence(runtime->format,
1077 runtime->oss.buffer,
1079 fs = snd_enter_user();
1080 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1083 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1084 void __user *buffers[runtime->channels];
1085 memset(buffers, 0, runtime->channels * sizeof(void *));
1086 snd_pcm_lib_writev(substream, buffers, size);
1090 * finish sync: drain the buffer
1093 saved_f_flags = substream->ffile->f_flags;
1094 substream->ffile->f_flags &= ~O_NONBLOCK;
1095 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1096 substream->ffile->f_flags = saved_f_flags;
1099 runtime->oss.prepare = 1;
1102 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1103 if (substream != NULL) {
1104 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1106 runtime = substream->runtime;
1107 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1110 runtime->oss.buffer_used = 0;
1111 runtime->oss.prepare = 1;
1116 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1120 for (idx = 1; idx >= 0; --idx) {
1121 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1122 snd_pcm_runtime_t *runtime;
1123 if (substream == NULL)
1125 runtime = substream->runtime;
1128 else if (rate > 192000)
1130 if (runtime->oss.rate != rate) {
1131 runtime->oss.params = 1;
1132 runtime->oss.rate = rate;
1135 return snd_pcm_oss_get_rate(pcm_oss_file);
1138 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1140 snd_pcm_substream_t *substream;
1143 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1145 return substream->runtime->oss.rate;
1148 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1155 for (idx = 1; idx >= 0; --idx) {
1156 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1157 snd_pcm_runtime_t *runtime;
1158 if (substream == NULL)
1160 runtime = substream->runtime;
1161 if (runtime->oss.channels != channels) {
1162 runtime->oss.params = 1;
1163 runtime->oss.channels = channels;
1166 return snd_pcm_oss_get_channels(pcm_oss_file);
1169 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1171 snd_pcm_substream_t *substream;
1174 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1176 return substream->runtime->oss.channels;
1179 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1181 snd_pcm_substream_t *substream;
1184 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1186 return substream->runtime->oss.period_bytes;
1189 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1191 snd_pcm_substream_t *substream;
1194 snd_pcm_hw_params_t *params;
1195 unsigned int formats = 0;
1196 snd_mask_t format_mask;
1199 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1201 if (atomic_read(&substream->runtime->mmap_count)) {
1204 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1205 direct = (setup != NULL && setup->direct);
1208 return AFMT_MU_LAW | AFMT_U8 |
1209 AFMT_S16_LE | AFMT_S16_BE |
1210 AFMT_S8 | AFMT_U16_LE |
1212 params = kmalloc(sizeof(*params), GFP_KERNEL);
1215 _snd_pcm_hw_params_any(params);
1216 err = snd_pcm_hw_refine(substream, params);
1217 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1219 snd_assert(err >= 0, return err);
1220 for (fmt = 0; fmt < 32; ++fmt) {
1221 if (snd_mask_test(&format_mask, fmt)) {
1222 int f = snd_pcm_oss_format_to(fmt);
1230 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1234 if (format != AFMT_QUERY) {
1235 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1236 if (!(formats & format))
1238 for (idx = 1; idx >= 0; --idx) {
1239 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1240 snd_pcm_runtime_t *runtime;
1241 if (substream == NULL)
1243 runtime = substream->runtime;
1244 if (runtime->oss.format != format) {
1245 runtime->oss.params = 1;
1246 runtime->oss.format = format;
1250 return snd_pcm_oss_get_format(pcm_oss_file);
1253 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1255 snd_pcm_substream_t *substream;
1258 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1260 return substream->runtime->oss.format;
1263 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1265 snd_pcm_runtime_t *runtime;
1267 if (substream == NULL)
1269 runtime = substream->runtime;
1270 if (subdivide == 0) {
1271 subdivide = runtime->oss.subdivision;
1276 if (runtime->oss.subdivision || runtime->oss.fragshift)
1278 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1279 subdivide != 8 && subdivide != 16)
1281 runtime->oss.subdivision = subdivide;
1282 runtime->oss.params = 1;
1286 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1288 int err = -EINVAL, idx;
1290 for (idx = 1; idx >= 0; --idx) {
1291 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1292 if (substream == NULL)
1294 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1300 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1302 snd_pcm_runtime_t *runtime;
1304 if (substream == NULL)
1306 runtime = substream->runtime;
1307 if (runtime->oss.subdivision || runtime->oss.fragshift)
1309 runtime->oss.fragshift = val & 0xffff;
1310 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1311 if (runtime->oss.fragshift < 4) /* < 16 */
1312 runtime->oss.fragshift = 4;
1313 if (runtime->oss.maxfrags < 2)
1314 runtime->oss.maxfrags = 2;
1315 runtime->oss.params = 1;
1319 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1321 int err = -EINVAL, idx;
1323 for (idx = 1; idx >= 0; --idx) {
1324 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1325 if (substream == NULL)
1327 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1333 static int snd_pcm_oss_nonblock(struct file * file)
1335 file->f_flags |= O_NONBLOCK;
1339 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1342 if (substream == NULL) {
1343 res &= ~DSP_CAP_DUPLEX;
1346 #ifdef DSP_CAP_MULTI
1347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1348 if (substream->pstr->substream_count > 1)
1349 res |= DSP_CAP_MULTI;
1351 /* DSP_CAP_REALTIME is set all times: */
1352 /* all ALSA drivers can return actual pointer in ring buffer */
1353 #if defined(DSP_CAP_REALTIME) && 0
1355 snd_pcm_runtime_t *runtime = substream->runtime;
1356 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1357 res &= ~DSP_CAP_REALTIME;
1363 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1367 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1368 for (idx = 0; idx < 2; idx++) {
1369 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1370 result = snd_pcm_oss_get_caps1(substream, result);
1372 result |= 0x0001; /* revision - same as SB AWE 64 */
1376 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1378 snd_pcm_runtime_t *runtime = substream->runtime;
1379 snd_pcm_uframes_t appl_ptr;
1380 appl_ptr = hw_ptr + runtime->buffer_size;
1381 appl_ptr %= runtime->boundary;
1382 runtime->control->appl_ptr = appl_ptr;
1385 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1387 snd_pcm_runtime_t *runtime;
1388 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1392 printk("pcm_oss: trigger = 0x%x\n", trigger);
1395 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1396 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1399 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1403 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1407 runtime = psubstream->runtime;
1408 if (trigger & PCM_ENABLE_OUTPUT) {
1409 if (runtime->oss.trigger)
1411 if (atomic_read(&psubstream->runtime->mmap_count))
1412 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
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_playback_ioctl(psubstream, cmd, NULL);
1430 runtime = csubstream->runtime;
1431 if (trigger & PCM_ENABLE_INPUT) {
1432 if (runtime->oss.trigger)
1434 runtime->oss.trigger = 1;
1435 runtime->start_threshold = 1;
1436 cmd = SNDRV_PCM_IOCTL_START;
1438 if (!runtime->oss.trigger)
1440 runtime->oss.trigger = 0;
1441 runtime->start_threshold = runtime->boundary;
1442 cmd = SNDRV_PCM_IOCTL_DROP;
1443 runtime->oss.prepare = 1;
1445 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1453 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1455 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1458 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1460 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1461 result |= PCM_ENABLE_OUTPUT;
1462 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1463 result |= PCM_ENABLE_INPUT;
1467 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1469 snd_pcm_substream_t *substream;
1470 snd_pcm_runtime_t *runtime;
1471 snd_pcm_sframes_t delay;
1474 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1475 if (substream == NULL)
1477 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1479 runtime = substream->runtime;
1480 if (runtime->oss.params || runtime->oss.prepare)
1482 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1484 delay = 0; /* hack for broken OSS applications */
1487 return snd_pcm_oss_bytes(substream, delay);
1490 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info __user * _info)
1492 snd_pcm_substream_t *substream;
1493 snd_pcm_runtime_t *runtime;
1494 snd_pcm_sframes_t delay;
1496 struct count_info info;
1501 substream = pcm_oss_file->streams[stream];
1502 if (substream == NULL)
1504 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1506 runtime = substream->runtime;
1507 if (runtime->oss.params || runtime->oss.prepare) {
1508 memset(&info, 0, sizeof(info));
1509 if (copy_to_user(_info, &info, sizeof(info)))
1513 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1514 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1515 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1520 fixup = runtime->oss.buffer_used;
1523 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1524 fixup = -runtime->oss.buffer_used;
1528 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1529 if (atomic_read(&runtime->mmap_count)) {
1530 snd_pcm_sframes_t n;
1531 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1533 n += runtime->boundary;
1534 info.blocks = n / runtime->period_size;
1535 runtime->oss.prev_hw_ptr_interrupt = delay;
1536 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1537 snd_pcm_oss_simulate_fill(substream, delay);
1538 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1540 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1541 info.blocks = delay / runtime->oss.period_bytes;
1542 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1543 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1545 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1547 if (copy_to_user(_info, &info, sizeof(info)))
1552 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1554 snd_pcm_substream_t *substream;
1555 snd_pcm_runtime_t *runtime;
1556 snd_pcm_sframes_t avail;
1558 struct audio_buf_info info;
1563 substream = pcm_oss_file->streams[stream];
1564 if (substream == NULL)
1566 runtime = substream->runtime;
1568 if (runtime->oss.params &&
1569 (err = snd_pcm_oss_change_params(substream)) < 0)
1572 info.fragsize = runtime->oss.period_bytes;
1573 info.fragstotal = runtime->periods;
1574 if (runtime->oss.prepare) {
1575 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1576 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1577 info.fragments = runtime->oss.periods;
1583 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1584 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1585 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1586 avail = runtime->buffer_size;
1590 avail = runtime->buffer_size - avail;
1591 fixup = -runtime->oss.buffer_used;
1594 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1595 fixup = runtime->oss.buffer_used;
1599 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1600 info.fragments = info.bytes / runtime->oss.period_bytes;
1604 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1606 if (copy_to_user(_info, &info, sizeof(info)))
1611 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1613 // it won't be probably implemented
1614 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1618 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1620 const char *ptr, *ptrl;
1621 snd_pcm_oss_setup_t *setup;
1623 down(&pcm->streams[stream].oss.setup_mutex);
1624 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1625 if (!strcmp(setup->task_name, task_name)) {
1626 up(&pcm->streams[stream].oss.setup_mutex);
1630 ptr = ptrl = task_name;
1636 if (ptrl == task_name) {
1640 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1641 if (!strcmp(setup->task_name, ptrl)) {
1642 up(&pcm->streams[stream].oss.setup_mutex);
1647 up(&pcm->streams[stream].oss.setup_mutex);
1651 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1652 snd_pcm_oss_setup_t *setup,
1655 snd_pcm_runtime_t *runtime;
1657 substream->oss.oss = 1;
1658 substream->oss.setup = setup;
1659 runtime = substream->runtime;
1660 runtime->oss.params = 1;
1661 runtime->oss.trigger = 1;
1662 runtime->oss.rate = 8000;
1663 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1664 case SNDRV_MINOR_OSS_PCM_8:
1665 runtime->oss.format = AFMT_U8;
1667 case SNDRV_MINOR_OSS_PCM_16:
1668 runtime->oss.format = AFMT_S16_LE;
1671 runtime->oss.format = AFMT_MU_LAW;
1673 runtime->oss.channels = 1;
1674 runtime->oss.fragshift = 0;
1675 runtime->oss.maxfrags = 0;
1676 runtime->oss.subdivision = 0;
1679 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1681 snd_pcm_runtime_t *runtime;
1682 runtime = substream->runtime;
1683 vfree(runtime->oss.buffer);
1684 snd_pcm_oss_plugin_clear(substream);
1685 substream->oss.file = NULL;
1686 substream->oss.oss = 0;
1689 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1692 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1693 for (cidx = 0; cidx < 2; ++cidx) {
1694 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1695 snd_pcm_runtime_t *runtime;
1696 if (substream == NULL)
1698 runtime = substream->runtime;
1700 snd_pcm_stream_lock_irq(substream);
1701 if (snd_pcm_running(substream))
1702 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1703 snd_pcm_stream_unlock_irq(substream);
1704 if (substream->open_flag) {
1705 if (substream->ops->hw_free != NULL)
1706 substream->ops->hw_free(substream);
1707 substream->ops->close(substream);
1708 substream->open_flag = 0;
1710 substream->ffile = NULL;
1711 snd_pcm_oss_release_substream(substream);
1712 snd_pcm_release_substream(substream);
1714 kfree(pcm_oss_file);
1718 static int snd_pcm_oss_open_file(struct file *file,
1720 snd_pcm_oss_file_t **rpcm_oss_file,
1722 snd_pcm_oss_setup_t *psetup,
1723 snd_pcm_oss_setup_t *csetup)
1726 snd_pcm_oss_file_t *pcm_oss_file;
1727 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1728 unsigned int f_mode = file->f_mode;
1730 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1731 *rpcm_oss_file = NULL;
1733 pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1734 if (pcm_oss_file == NULL)
1737 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1738 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1739 f_mode = FMODE_WRITE;
1740 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1741 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1742 &psubstream)) < 0) {
1743 snd_pcm_oss_release_file(pcm_oss_file);
1746 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1748 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1749 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1750 &csubstream)) < 0) {
1751 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1752 snd_pcm_oss_release_file(pcm_oss_file);
1758 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1761 if (psubstream == NULL && csubstream == NULL) {
1762 snd_pcm_oss_release_file(pcm_oss_file);
1765 if (psubstream != NULL) {
1766 psubstream->oss.file = pcm_oss_file;
1767 err = snd_pcm_hw_constraints_init(psubstream);
1769 snd_printd("snd_pcm_hw_constraint_init failed\n");
1770 snd_pcm_oss_release_file(pcm_oss_file);
1773 if ((err = psubstream->ops->open(psubstream)) < 0) {
1774 snd_pcm_oss_release_file(pcm_oss_file);
1777 psubstream->open_flag = 1;
1778 err = snd_pcm_hw_constraints_complete(psubstream);
1780 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1781 snd_pcm_oss_release_file(pcm_oss_file);
1784 psubstream->ffile = file;
1785 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1787 if (csubstream != NULL) {
1788 csubstream->oss.file = pcm_oss_file;
1789 err = snd_pcm_hw_constraints_init(csubstream);
1791 snd_printd("snd_pcm_hw_constraint_init failed\n");
1792 snd_pcm_oss_release_file(pcm_oss_file);
1795 if ((err = csubstream->ops->open(csubstream)) < 0) {
1796 snd_pcm_oss_release_file(pcm_oss_file);
1799 csubstream->open_flag = 1;
1800 err = snd_pcm_hw_constraints_complete(csubstream);
1802 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1803 snd_pcm_oss_release_file(pcm_oss_file);
1806 csubstream->ffile = file;
1807 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1810 file->private_data = pcm_oss_file;
1811 *rpcm_oss_file = pcm_oss_file;
1816 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1818 int minor = iminor(inode);
1819 int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1824 snd_pcm_oss_file_t *pcm_oss_file;
1825 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1829 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1830 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1831 adsp_map[cardnum] : dsp_map[cardnum];
1833 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1838 err = snd_card_file_add(pcm->card, file);
1841 if (!try_module_get(pcm->card->module)) {
1845 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1849 if (file->f_mode & FMODE_WRITE)
1850 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1851 if (file->f_mode & FMODE_READ)
1852 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1854 nonblock = !!(file->f_flags & O_NONBLOCK);
1855 if (psetup && !psetup->disable) {
1856 if (psetup->nonblock)
1858 else if (psetup->block)
1860 } else if (csetup && !csetup->disable) {
1861 if (csetup->nonblock)
1863 else if (csetup->block)
1867 nonblock = nonblock_open;
1869 init_waitqueue_entry(&wait, current);
1870 add_wait_queue(&pcm->open_wait, &wait);
1871 down(&pcm->open_mutex);
1873 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1874 minor, psetup, csetup);
1877 if (err == -EAGAIN) {
1884 set_current_state(TASK_INTERRUPTIBLE);
1885 up(&pcm->open_mutex);
1887 down(&pcm->open_mutex);
1888 if (signal_pending(current)) {
1893 remove_wait_queue(&pcm->open_wait, &wait);
1894 up(&pcm->open_mutex);
1900 module_put(pcm->card->module);
1902 snd_card_file_remove(pcm->card, file);
1907 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1910 snd_pcm_substream_t *substream;
1911 snd_pcm_oss_file_t *pcm_oss_file;
1913 pcm_oss_file = file->private_data;
1914 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1915 if (substream == NULL)
1916 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1917 snd_assert(substream != NULL, return -ENXIO);
1918 pcm = substream->pcm;
1919 snd_pcm_oss_sync(pcm_oss_file);
1920 down(&pcm->open_mutex);
1921 snd_pcm_oss_release_file(pcm_oss_file);
1922 up(&pcm->open_mutex);
1923 wake_up(&pcm->open_wait);
1924 module_put(pcm->card->module);
1925 snd_card_file_remove(pcm->card, file);
1929 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1931 snd_pcm_oss_file_t *pcm_oss_file;
1932 int __user *p = (int __user *)arg;
1935 pcm_oss_file = file->private_data;
1936 if (cmd == OSS_GETVERSION)
1937 return put_user(SNDRV_OSS_VERSION, p);
1938 if (cmd == OSS_ALSAEMULVER)
1939 return put_user(1, p);
1940 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1941 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1942 snd_pcm_substream_t *substream;
1944 for (idx = 0; idx < 2; ++idx) {
1945 substream = pcm_oss_file->streams[idx];
1946 if (substream != NULL)
1949 snd_assert(substream != NULL, return -ENXIO);
1950 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1953 if (((cmd >> 8) & 0xff) != 'P')
1956 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1959 case SNDCTL_DSP_RESET:
1960 return snd_pcm_oss_reset(pcm_oss_file);
1961 case SNDCTL_DSP_SYNC:
1962 return snd_pcm_oss_sync(pcm_oss_file);
1963 case SNDCTL_DSP_SPEED:
1964 if (get_user(res, p))
1966 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1968 return put_user(res, p);
1969 case SOUND_PCM_READ_RATE:
1970 res = snd_pcm_oss_get_rate(pcm_oss_file);
1973 return put_user(res, p);
1974 case SNDCTL_DSP_STEREO:
1975 if (get_user(res, p))
1977 res = res > 0 ? 2 : 1;
1978 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1980 return put_user(--res, p);
1981 case SNDCTL_DSP_GETBLKSIZE:
1982 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1985 return put_user(res, p);
1986 case SNDCTL_DSP_SETFMT:
1987 if (get_user(res, p))
1989 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1992 return put_user(res, p);
1993 case SOUND_PCM_READ_BITS:
1994 res = snd_pcm_oss_get_format(pcm_oss_file);
1997 return put_user(res, p);
1998 case SNDCTL_DSP_CHANNELS:
1999 if (get_user(res, p))
2001 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2004 return put_user(res, p);
2005 case SOUND_PCM_READ_CHANNELS:
2006 res = snd_pcm_oss_get_channels(pcm_oss_file);
2009 return put_user(res, p);
2010 case SOUND_PCM_WRITE_FILTER:
2011 case SOUND_PCM_READ_FILTER:
2013 case SNDCTL_DSP_POST:
2014 return snd_pcm_oss_post(pcm_oss_file);
2015 case SNDCTL_DSP_SUBDIVIDE:
2016 if (get_user(res, p))
2018 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2021 return put_user(res, p);
2022 case SNDCTL_DSP_SETFRAGMENT:
2023 if (get_user(res, p))
2025 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2026 case SNDCTL_DSP_GETFMTS:
2027 res = snd_pcm_oss_get_formats(pcm_oss_file);
2030 return put_user(res, p);
2031 case SNDCTL_DSP_GETOSPACE:
2032 case SNDCTL_DSP_GETISPACE:
2033 return snd_pcm_oss_get_space(pcm_oss_file,
2034 cmd == SNDCTL_DSP_GETISPACE ?
2035 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2036 (struct audio_buf_info __user *) arg);
2037 case SNDCTL_DSP_NONBLOCK:
2038 return snd_pcm_oss_nonblock(file);
2039 case SNDCTL_DSP_GETCAPS:
2040 res = snd_pcm_oss_get_caps(pcm_oss_file);
2043 return put_user(res, p);
2044 case SNDCTL_DSP_GETTRIGGER:
2045 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2048 return put_user(res, p);
2049 case SNDCTL_DSP_SETTRIGGER:
2050 if (get_user(res, p))
2052 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2053 case SNDCTL_DSP_GETIPTR:
2054 case SNDCTL_DSP_GETOPTR:
2055 return snd_pcm_oss_get_ptr(pcm_oss_file,
2056 cmd == SNDCTL_DSP_GETIPTR ?
2057 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2058 (struct count_info __user *) arg);
2059 case SNDCTL_DSP_MAPINBUF:
2060 case SNDCTL_DSP_MAPOUTBUF:
2061 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2062 cmd == SNDCTL_DSP_MAPINBUF ?
2063 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2064 (struct buffmem_desc __user *) arg);
2065 case SNDCTL_DSP_SETSYNCRO:
2066 /* stop DMA now.. */
2068 case SNDCTL_DSP_SETDUPLEX:
2069 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2072 case SNDCTL_DSP_GETODELAY:
2073 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2075 /* it's for sure, some broken apps don't check for error codes */
2079 return put_user(res, p);
2080 case SNDCTL_DSP_PROFILE:
2081 return 0; /* silently ignore */
2083 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2088 #ifdef CONFIG_COMPAT
2089 /* all compatible */
2090 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2092 #define snd_pcm_oss_ioctl_compat NULL
2095 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2097 snd_pcm_oss_file_t *pcm_oss_file;
2098 snd_pcm_substream_t *substream;
2100 pcm_oss_file = file->private_data;
2101 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2102 if (substream == NULL)
2105 return snd_pcm_oss_read1(substream, buf, count);
2108 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2109 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2115 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2117 snd_pcm_oss_file_t *pcm_oss_file;
2118 snd_pcm_substream_t *substream;
2121 pcm_oss_file = file->private_data;
2122 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2123 if (substream == NULL)
2125 up(&file->f_dentry->d_inode->i_sem);
2126 result = snd_pcm_oss_write1(substream, buf, count);
2127 down(&file->f_dentry->d_inode->i_sem);
2129 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2134 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2136 snd_pcm_runtime_t *runtime = substream->runtime;
2137 if (atomic_read(&runtime->mmap_count))
2138 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2140 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2143 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2145 snd_pcm_runtime_t *runtime = substream->runtime;
2146 if (atomic_read(&runtime->mmap_count))
2147 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2149 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2152 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2154 snd_pcm_oss_file_t *pcm_oss_file;
2156 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2158 pcm_oss_file = file->private_data;
2160 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2161 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2164 if (psubstream != NULL) {
2165 snd_pcm_runtime_t *runtime = psubstream->runtime;
2166 poll_wait(file, &runtime->sleep, wait);
2167 snd_pcm_stream_lock_irq(psubstream);
2168 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2169 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2170 snd_pcm_oss_playback_ready(psubstream)))
2171 mask |= POLLOUT | POLLWRNORM;
2172 snd_pcm_stream_unlock_irq(psubstream);
2174 if (csubstream != NULL) {
2175 snd_pcm_runtime_t *runtime = csubstream->runtime;
2176 enum sndrv_pcm_state ostate;
2177 poll_wait(file, &runtime->sleep, wait);
2178 snd_pcm_stream_lock_irq(csubstream);
2179 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2180 snd_pcm_oss_capture_ready(csubstream))
2181 mask |= POLLIN | POLLRDNORM;
2182 snd_pcm_stream_unlock_irq(csubstream);
2183 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2184 snd_pcm_oss_file_t ofile;
2185 memset(&ofile, 0, sizeof(ofile));
2186 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2187 runtime->oss.trigger = 0;
2188 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2195 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2197 snd_pcm_oss_file_t *pcm_oss_file;
2198 snd_pcm_substream_t *substream = NULL;
2199 snd_pcm_runtime_t *runtime;
2203 printk("pcm_oss: mmap begin\n");
2205 pcm_oss_file = file->private_data;
2206 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2207 case VM_READ | VM_WRITE:
2208 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2213 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2216 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2221 /* set VM_READ access as well to fix memset() routines that do
2222 reads before writes (to improve performance) */
2223 area->vm_flags |= VM_READ;
2224 if (substream == NULL)
2226 runtime = substream->runtime;
2227 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2229 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2230 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2234 if (runtime->oss.params) {
2235 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2238 if (runtime->oss.plugin_first != NULL)
2241 if (area->vm_pgoff != 0)
2244 err = snd_pcm_mmap_data(substream, file, area);
2247 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2248 runtime->silence_threshold = 0;
2249 runtime->silence_size = 0;
2251 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2253 /* In mmap mode we never stop */
2254 runtime->stop_threshold = runtime->boundary;
2263 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2264 snd_info_buffer_t * buffer)
2266 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2267 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2268 down(&pstr->oss.setup_mutex);
2270 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2274 setup->disable ? " disable" : "",
2275 setup->direct ? " direct" : "",
2276 setup->block ? " block" : "",
2277 setup->nonblock ? " non-block" : "",
2278 setup->partialfrag ? " partial-frag" : "",
2279 setup->nosilence ? " no-silence" : "");
2280 setup = setup->next;
2282 up(&pstr->oss.setup_mutex);
2285 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2288 snd_pcm_substream_t *substream;
2289 snd_pcm_oss_setup_t *setup, *setupn;
2291 for (idx = 0, substream = pstr->substream;
2292 idx < pstr->substream_count; idx++, substream = substream->next)
2293 substream->oss.setup = NULL;
2294 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2295 setup; setup = setupn) {
2296 setupn = setup->next;
2297 kfree(setup->task_name);
2300 pstr->oss.setup_list = NULL;
2303 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2304 snd_info_buffer_t * buffer)
2306 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2307 char line[128], str[32], task_name[32], *ptr;
2309 snd_pcm_oss_setup_t *setup, *setup1, template;
2311 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2312 down(&pstr->oss.setup_mutex);
2313 memset(&template, 0, sizeof(template));
2314 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2315 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2316 snd_pcm_oss_proc_free_setup_list(pstr);
2317 up(&pstr->oss.setup_mutex);
2320 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2321 if (!strcmp(setup->task_name, task_name)) {
2326 ptr = snd_info_get_str(str, ptr, sizeof(str));
2327 template.periods = simple_strtoul(str, NULL, 10);
2328 ptr = snd_info_get_str(str, ptr, sizeof(str));
2329 template.period_size = simple_strtoul(str, NULL, 10);
2330 for (idx1 = 31; idx1 >= 0; idx1--)
2331 if (template.period_size & (1 << idx1))
2333 for (idx1--; idx1 >= 0; idx1--)
2334 template.period_size &= ~(1 << idx1);
2336 ptr = snd_info_get_str(str, ptr, sizeof(str));
2337 if (!strcmp(str, "disable")) {
2338 template.disable = 1;
2339 } else if (!strcmp(str, "direct")) {
2340 template.direct = 1;
2341 } else if (!strcmp(str, "block")) {
2343 } else if (!strcmp(str, "non-block")) {
2344 template.nonblock = 1;
2345 } else if (!strcmp(str, "partial-frag")) {
2346 template.partialfrag = 1;
2347 } else if (!strcmp(str, "no-silence")) {
2348 template.nosilence = 1;
2351 if (setup == NULL) {
2352 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2354 if (pstr->oss.setup_list == NULL) {
2355 pstr->oss.setup_list = setup;
2357 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2358 setup1->next = setup;
2360 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2362 buffer->error = -ENOMEM;
2367 up(&pstr->oss.setup_mutex);
2371 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2374 for (stream = 0; stream < 2; ++stream) {
2375 snd_info_entry_t *entry;
2376 snd_pcm_str_t *pstr = &pcm->streams[stream];
2377 if (pstr->substream_count == 0)
2379 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2380 entry->content = SNDRV_INFO_CONTENT_TEXT;
2381 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2382 entry->c.text.read_size = 8192;
2383 entry->c.text.read = snd_pcm_oss_proc_read;
2384 entry->c.text.write_size = 8192;
2385 entry->c.text.write = snd_pcm_oss_proc_write;
2386 entry->private_data = pstr;
2387 if (snd_info_register(entry) < 0) {
2388 snd_info_free_entry(entry);
2392 pstr->oss.proc_entry = entry;
2396 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2399 for (stream = 0; stream < 2; ++stream) {
2400 snd_pcm_str_t *pstr = &pcm->streams[stream];
2401 if (pstr->oss.proc_entry) {
2402 snd_info_unregister(pstr->oss.proc_entry);
2403 pstr->oss.proc_entry = NULL;
2404 snd_pcm_oss_proc_free_setup_list(pstr);
2413 static struct file_operations snd_pcm_oss_f_reg =
2415 .owner = THIS_MODULE,
2416 .read = snd_pcm_oss_read,
2417 .write = snd_pcm_oss_write,
2418 .open = snd_pcm_oss_open,
2419 .release = snd_pcm_oss_release,
2420 .poll = snd_pcm_oss_poll,
2421 .unlocked_ioctl = snd_pcm_oss_ioctl,
2422 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2423 .mmap = snd_pcm_oss_mmap,
2426 static snd_minor_t snd_pcm_oss_reg =
2428 .comment = "digital audio",
2429 .f_ops = &snd_pcm_oss_f_reg,
2432 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2435 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2436 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2437 pcm->card, index, &snd_pcm_oss_reg,
2439 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2443 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2446 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2449 register_oss_dsp(pcm, 0);
2450 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2451 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2452 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2453 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2454 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2455 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2460 pcm->oss.reg_mask |= 1;
2462 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2463 register_oss_dsp(pcm, 1);
2465 pcm->oss.reg_mask |= 2;
2469 snd_pcm_oss_proc_init(pcm);
2474 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2477 if (pcm->oss.reg_mask & 1) {
2478 pcm->oss.reg_mask &= ~1;
2479 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2482 if (pcm->oss.reg_mask & 2) {
2483 pcm->oss.reg_mask &= ~2;
2484 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2491 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2493 snd_pcm_oss_disconnect_minor(pcm);
2495 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2496 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2497 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2501 snd_pcm_oss_proc_done(pcm);
2506 static snd_pcm_notify_t snd_pcm_oss_notify =
2508 .n_register = snd_pcm_oss_register_minor,
2509 .n_disconnect = snd_pcm_oss_disconnect_minor,
2510 .n_unregister = snd_pcm_oss_unregister_minor,
2513 static int __init alsa_pcm_oss_init(void)
2518 /* check device map table */
2519 for (i = 0; i < SNDRV_CARDS; i++) {
2520 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2521 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2524 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2525 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2529 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2534 static void __exit alsa_pcm_oss_exit(void)
2536 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2539 module_init(alsa_pcm_oss_init)
2540 module_exit(alsa_pcm_oss_exit)