initial commit with v2.6.9
[linux-2.6.9-moxart.git] / sound / core / oss / pcm_oss.c
blob5d87847c616e5fde5e43223aa6f7d2606d2f0b00
1 /*
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
22 #if 0
23 #define PLUGIN_DEBUG
24 #endif
25 #if 0
26 #define OSS_DEBUG
27 #endif
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;
50 static int boot_devs;
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();
72 set_fs(get_ds());
73 return fs;
76 static inline void snd_leave_user(mm_segment_t fs)
78 set_fs(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;
87 while (plugin) {
88 next = plugin->next;
89 snd_pcm_plugin_free(plugin);
90 plugin = next;
92 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
93 return 0;
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;
100 plugin->prev = NULL;
101 if (runtime->oss.plugin_first) {
102 runtime->oss.plugin_first->prev = plugin;
103 runtime->oss.plugin_first = plugin;
104 } else {
105 runtime->oss.plugin_last =
106 runtime->oss.plugin_first = plugin;
108 return 0;
111 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
113 snd_pcm_runtime_t *runtime = plugin->plug->runtime;
114 plugin->next = NULL;
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;
119 } else {
120 runtime->oss.plugin_last =
121 runtime->oss.plugin_first = plugin;
123 return 0;
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)
132 return frames;
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)
147 switch (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)
164 switch (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)
183 size_t s;
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;
207 } else {
208 int sd;
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;
212 do {
213 oss_period_size /= 2;
214 } while (oss_period_size > bytes_per_sec);
215 if (runtime->oss.subdivision == 0) {
216 sd = 4;
217 if (oss_period_size / sd > 4096)
218 sd *= 2;
219 if (oss_period_size / sd < 4096)
220 sd = 1;
221 } else
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;
252 if (oss_periods > s)
253 oss_periods = s;
255 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
256 if (s < 2)
257 s = 2;
258 if (oss_periods < s)
259 oss_periods = s;
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;
268 return 0;
271 static int choose_rate(snd_pcm_substream_t *substream,
272 snd_pcm_hw_params_t *params, unsigned int best_rate)
274 snd_interval_t *it;
275 snd_pcm_hw_params_t *save;
276 unsigned int rate, prev;
278 save = kmalloc(sizeof(*save), GFP_KERNEL);
279 if (save == NULL)
280 return -ENOMEM;
281 *save = *params;
282 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
284 /* try multiples of the best rate */
285 rate = best_rate;
286 for (;;) {
287 if (it->max < rate || (it->max == rate && it->openmax))
288 break;
289 if (it->min < rate || (it->min == rate && !it->openmin)) {
290 int ret;
291 ret = snd_pcm_hw_param_set(substream, params,
292 SNDRV_PCM_HW_PARAM_RATE,
293 rate, 0);
294 if (ret == (int)rate) {
295 kfree(save);
296 return rate;
298 *params = *save;
300 prev = rate;
301 rate += best_rate;
302 if (rate <= prev)
303 break;
306 /* not found, use the nearest rate */
307 kfree(save);
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;
318 int err;
319 int direct;
320 int format, sformat, n;
321 snd_mask_t sformat_mask;
322 snd_mask_t 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");
329 err = -ENOMEM;
330 goto failure;
333 if (atomic_read(&runtime->mmap_count)) {
334 direct = 1;
335 } else {
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);
346 else {
347 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
348 if (!direct)
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);
352 if (err < 0) {
353 snd_printd("No usable accesses\n");
354 err = -EINVAL;
355 goto failure;
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);
363 if (direct)
364 sformat = format;
365 else
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)
372 break;
374 if (sformat > SNDRV_PCM_FORMAT_LAST) {
375 snd_printd("Cannot find a format!!!\n");
376 err = -EINVAL;
377 goto failure;
380 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
381 snd_assert(err >= 0, goto failure);
383 if (direct) {
384 memcpy(params, sparams, sizeof(*params));
385 } else {
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);
407 if (!direct) {
408 /* add necessary plugins */
409 snd_pcm_oss_plugin_clear(substream);
410 if ((err = snd_pcm_plug_format_plugins(substream,
411 params,
412 sparams)) < 0) {
413 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
414 snd_pcm_oss_plugin_clear(substream);
415 goto failure;
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);
422 goto failure;
424 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
425 err = snd_pcm_plugin_append(plugin);
426 } else {
427 err = snd_pcm_plugin_insert(plugin);
429 if (err < 0) {
430 snd_pcm_oss_plugin_clear(substream);
431 goto failure;
436 err = snd_pcm_oss_period_size(substream, params, sparams);
437 if (err < 0)
438 goto failure;
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);
452 goto failure;
455 memset(sw_params, 0, sizeof(*sw_params));
456 if (runtime->oss.trigger) {
457 sw_params->start_threshold = 1;
458 } else {
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;
463 else
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;
474 } else {
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);
485 goto failure;
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);
493 if (err < 0)
494 goto failure;
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);
526 err = 0;
527 failure:
528 if (sw_params)
529 kfree(sw_params);
530 if (params)
531 kfree(params);
532 if (sparams)
533 kfree(sparams);
534 return err;
537 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
539 int idx, err;
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)
545 continue;
546 if (asubstream == NULL)
547 asubstream = substream;
548 if (substream->runtime->oss.params) {
549 err = snd_pcm_oss_change_params(substream);
550 if (err < 0)
551 return err;
554 snd_assert(asubstream != NULL, return -EIO);
555 if (r_substream)
556 *r_substream = asubstream;
557 return 0;
560 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
562 int err;
563 snd_pcm_runtime_t *runtime = substream->runtime;
565 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
566 if (err < 0) {
567 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
568 return err;
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;
575 return 0;
578 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
580 snd_pcm_runtime_t *runtime;
581 int err;
583 if (substream == NULL)
584 return 0;
585 runtime = substream->runtime;
586 if (runtime->oss.params) {
587 err = snd_pcm_oss_change_params(substream);
588 if (err < 0)
589 return err;
591 if (runtime->oss.prepare) {
592 err = snd_pcm_oss_prepare(substream);
593 if (err < 0)
594 return err;
596 return 0;
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;
603 int err = 0;
605 while (1) {
606 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
607 if (err < 0)
608 break;
609 runtime = substream->runtime;
610 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
611 break;
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);
618 if (err < 0)
619 break;
621 return err;
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;
627 int ret;
628 while (1) {
629 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
630 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
631 #ifdef OSS_DEBUG
632 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
633 printk("pcm_oss: write: recovering from XRUN\n");
634 else
635 printk("pcm_oss: write: recovering from SUSPEND\n");
636 #endif
637 ret = snd_pcm_oss_prepare(substream);
638 if (ret < 0)
639 break;
641 if (in_kernel) {
642 mm_segment_t fs;
643 fs = snd_enter_user();
644 ret = snd_pcm_lib_write(substream, ptr, frames);
645 snd_leave_user(fs);
646 } else {
647 ret = snd_pcm_lib_write(substream, ptr, frames);
649 if (ret != -EPIPE && ret != -ESTRPIPE)
650 break;
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)
654 return -EAGAIN;
656 return ret;
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;
663 int ret;
664 while (1) {
665 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
666 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
667 #ifdef OSS_DEBUG
668 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
669 printk("pcm_oss: read: recovering from XRUN\n");
670 else
671 printk("pcm_oss: read: recovering from SUSPEND\n");
672 #endif
673 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
674 if (ret < 0)
675 break;
676 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
677 ret = snd_pcm_oss_prepare(substream);
678 if (ret < 0)
679 break;
681 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
682 if (ret < 0)
683 break;
684 if (in_kernel) {
685 mm_segment_t fs;
686 fs = snd_enter_user();
687 ret = snd_pcm_lib_read(substream, ptr, frames);
688 snd_leave_user(fs);
689 } else {
690 ret = snd_pcm_lib_read(substream, ptr, frames);
692 if (ret == -EPIPE) {
693 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
694 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
695 if (ret < 0)
696 break;
698 continue;
700 if (ret != -ESTRPIPE)
701 break;
703 return ret;
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;
709 int ret;
710 while (1) {
711 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
712 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
713 #ifdef OSS_DEBUG
714 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
715 printk("pcm_oss: writev: recovering from XRUN\n");
716 else
717 printk("pcm_oss: writev: recovering from SUSPEND\n");
718 #endif
719 ret = snd_pcm_oss_prepare(substream);
720 if (ret < 0)
721 break;
723 if (in_kernel) {
724 mm_segment_t fs;
725 fs = snd_enter_user();
726 ret = snd_pcm_lib_writev(substream, bufs, frames);
727 snd_leave_user(fs);
728 } else {
729 ret = snd_pcm_lib_writev(substream, bufs, frames);
731 if (ret != -EPIPE && ret != -ESTRPIPE)
732 break;
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)
737 return -EAGAIN;
739 return ret;
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;
745 int ret;
746 while (1) {
747 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
748 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
749 #ifdef OSS_DEBUG
750 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
751 printk("pcm_oss: readv: recovering from XRUN\n");
752 else
753 printk("pcm_oss: readv: recovering from SUSPEND\n");
754 #endif
755 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
756 if (ret < 0)
757 break;
758 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
759 ret = snd_pcm_oss_prepare(substream);
760 if (ret < 0)
761 break;
763 if (in_kernel) {
764 mm_segment_t fs;
765 fs = snd_enter_user();
766 ret = snd_pcm_lib_readv(substream, bufs, frames);
767 snd_leave_user(fs);
768 } else {
769 ret = snd_pcm_lib_readv(substream, bufs, frames);
771 if (ret != -EPIPE && ret != -ESTRPIPE)
772 break;
774 return ret;
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;
784 if (!in_kernel) {
785 if (copy_from_user(runtime->oss.buffer, buf, bytes))
786 return -EFAULT;
787 buf = runtime->oss.buffer;
789 frames = bytes / oss_frame_bytes;
790 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
791 if (frames1 < 0)
792 return frames1;
793 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
794 if (frames1 <= 0)
795 return frames1;
796 bytes = frames1 * oss_frame_bytes;
797 } else {
798 frames = bytes_to_frames(runtime, bytes);
799 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
800 if (frames1 <= 0)
801 return frames1;
802 bytes = frames_to_bytes(runtime, frames1);
804 return bytes;
807 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char __user *buf, size_t bytes)
809 size_t xfer = 0;
810 ssize_t tmp;
811 snd_pcm_runtime_t *runtime = substream->runtime;
813 if (atomic_read(&runtime->mmap_count))
814 return -ENXIO;
816 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
817 return tmp;
818 while (bytes > 0) {
819 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
820 tmp = bytes;
821 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
822 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
823 if (tmp > 0) {
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;
828 buf += tmp;
829 bytes -= tmp;
830 xfer += 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);
834 if (tmp <= 0)
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;
841 } else {
842 tmp = snd_pcm_oss_write2(substream, (char *)buf, runtime->oss.period_bytes, 0);
843 if (tmp <= 0)
844 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
845 runtime->oss.bytes += tmp;
846 buf += tmp;
847 bytes -= tmp;
848 xfer += tmp;
851 return xfer;
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;
862 if (!in_kernel)
863 buf = runtime->oss.buffer;
864 frames = bytes / oss_frame_bytes;
865 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
866 if (frames1 < 0)
867 return frames1;
868 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
869 if (frames1 <= 0)
870 return frames1;
871 bytes = frames1 * oss_frame_bytes;
872 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
873 return -EFAULT;
874 } else {
875 frames = bytes_to_frames(runtime, bytes);
876 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
877 if (frames1 <= 0)
878 return frames1;
879 bytes = frames_to_bytes(runtime, frames1);
881 return bytes;
884 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char __user *buf, size_t bytes)
886 size_t xfer = 0;
887 ssize_t tmp;
888 snd_pcm_runtime_t *runtime = substream->runtime;
890 if (atomic_read(&runtime->mmap_count))
891 return -ENXIO;
893 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
894 return tmp;
895 while (bytes > 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);
899 if (tmp <= 0)
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;
905 tmp = bytes;
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;
910 buf += tmp;
911 bytes -= tmp;
912 xfer += tmp;
913 runtime->oss.buffer_used -= tmp;
914 } else {
915 tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
916 if (tmp <= 0)
917 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
918 runtime->oss.bytes += tmp;
919 buf += tmp;
920 bytes -= tmp;
921 xfer += tmp;
924 return xfer;
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;
941 return 0;
944 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
946 snd_pcm_substream_t *substream;
947 int err;
949 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
950 if (substream != NULL) {
951 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
952 return err;
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 */
957 return 0;
960 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
962 snd_pcm_runtime_t *runtime;
963 ssize_t result = 0;
964 long res;
965 wait_queue_t wait;
967 runtime = substream->runtime;
968 init_waitqueue_entry(&wait, current);
969 add_wait_queue(&runtime->sleep, &wait);
970 #ifdef OSS_DEBUG
971 printk("sync1: size = %li\n", size);
972 #endif
973 while (1) {
974 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
975 if (result > 0) {
976 runtime->oss.buffer_used = 0;
977 result = 0;
978 break;
980 if (result != 0 && result != -EAGAIN)
981 break;
982 result = 0;
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);
989 break;
991 res = schedule_timeout(10 * HZ);
992 if (signal_pending(current)) {
993 result = -ERESTARTSYS;
994 break;
996 if (res == 0) {
997 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
998 result = -EIO;
999 break;
1002 remove_wait_queue(&runtime->sleep, &wait);
1003 return result;
1006 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1008 int err = 0;
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;
1014 size_t size;
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))
1020 goto __direct;
1021 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1022 return err;
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) {
1026 #ifdef OSS_DEBUG
1027 printk("sync: buffer_used\n");
1028 #endif
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,
1032 size);
1033 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1034 if (err < 0)
1035 return err;
1036 } else if (runtime->oss.period_ptr > 0) {
1037 #ifdef OSS_DEBUG
1038 printk("sync: period_ptr\n");
1039 #endif
1040 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1041 snd_pcm_format_set_silence(format,
1042 runtime->oss.buffer,
1043 size * 8 / width);
1044 err = snd_pcm_oss_sync1(substream, size);
1045 if (err < 0)
1046 return err;
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;
1053 if (size > 0) {
1054 size = runtime->period_size - size;
1055 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1056 size = (runtime->frame_bits * size) / 8;
1057 while (size > 0) {
1058 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1059 size -= size1;
1060 size1 *= 8;
1061 size1 /= runtime->sample_bits;
1062 snd_pcm_format_set_silence(runtime->format,
1063 runtime->oss.buffer,
1064 size1);
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
1076 __direct:
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;
1081 if (err < 0)
1082 return err;
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)
1089 return err;
1090 runtime = substream->runtime;
1091 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1092 if (err < 0)
1093 return err;
1094 runtime->oss.buffer_used = 0;
1095 runtime->oss.prepare = 1;
1097 return 0;
1100 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1102 int idx;
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)
1108 continue;
1109 runtime = substream->runtime;
1110 if (rate < 1000)
1111 rate = 1000;
1112 else if (rate > 192000)
1113 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;
1125 int err;
1127 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1128 return err;
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)
1134 int idx;
1135 if (channels < 1)
1136 channels = 1;
1137 if (channels > 128)
1138 return -EINVAL;
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)
1143 continue;
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;
1156 int err;
1158 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1159 return err;
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;
1166 int err;
1168 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1169 return err;
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;
1176 int err;
1177 int direct;
1178 snd_pcm_hw_params_t *params;
1179 unsigned int formats = 0;
1180 snd_mask_t format_mask;
1181 int fmt;
1183 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1184 return err;
1185 if (atomic_read(&substream->runtime->mmap_count)) {
1186 direct = 1;
1187 } else {
1188 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1189 direct = (setup != NULL && setup->direct);
1191 if (!direct)
1192 return AFMT_MU_LAW | AFMT_U8 |
1193 AFMT_S16_LE | AFMT_S16_BE |
1194 AFMT_S8 | AFMT_U16_LE |
1195 AFMT_U16_BE;
1196 params = kmalloc(sizeof(*params), GFP_KERNEL);
1197 if (!params)
1198 return -ENOMEM;
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);
1202 kfree(params);
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);
1207 if (f >= 0)
1208 formats |= f;
1211 return formats;
1214 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1216 int formats, idx;
1218 if (format != AFMT_QUERY) {
1219 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1220 if (!(formats & format))
1221 format = AFMT_U8;
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)
1226 continue;
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;
1240 int err;
1242 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1243 return err;
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)
1252 return 0;
1253 runtime = substream->runtime;
1254 if (subdivide == 0) {
1255 subdivide = runtime->oss.subdivision;
1256 if (subdivide == 0)
1257 subdivide = 1;
1258 return subdivide;
1260 if (runtime->oss.subdivision || runtime->oss.fragshift)
1261 return -EINVAL;
1262 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1263 subdivide != 8 && subdivide != 16)
1264 return -EINVAL;
1265 runtime->oss.subdivision = subdivide;
1266 runtime->oss.params = 1;
1267 return subdivide;
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)
1277 continue;
1278 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1279 return err;
1281 return err;
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)
1289 return 0;
1290 runtime = substream->runtime;
1291 if (runtime->oss.subdivision || runtime->oss.fragshift)
1292 return -EINVAL;
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;
1300 return 0;
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)
1310 continue;
1311 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1312 return err;
1314 return err;
1317 static int snd_pcm_oss_nonblock(struct file * file)
1319 file->f_flags |= O_NONBLOCK;
1320 return 0;
1323 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1326 if (substream == NULL) {
1327 res &= ~DSP_CAP_DUPLEX;
1328 return res;
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;
1334 #endif
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;
1343 #endif
1344 return res;
1347 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1349 int result, idx;
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 */
1357 return result;
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;
1373 int err, cmd;
1375 #ifdef OSS_DEBUG
1376 printk("pcm_oss: trigger = 0x%x\n", trigger);
1377 #endif
1379 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1380 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1382 if (psubstream) {
1383 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1384 return err;
1386 if (csubstream) {
1387 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1388 return err;
1390 if (psubstream) {
1391 runtime = psubstream->runtime;
1392 if (trigger & PCM_ENABLE_OUTPUT) {
1393 if (runtime->oss.trigger)
1394 goto _skip1;
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;
1400 } else {
1401 if (!runtime->oss.trigger)
1402 goto _skip1;
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);
1409 if (err < 0)
1410 return err;
1412 _skip1:
1413 if (csubstream) {
1414 runtime = csubstream->runtime;
1415 if (trigger & PCM_ENABLE_INPUT) {
1416 if (runtime->oss.trigger)
1417 goto _skip2;
1418 runtime->oss.trigger = 1;
1419 runtime->start_threshold = 1;
1420 cmd = SNDRV_PCM_IOCTL_START;
1421 } else {
1422 if (!runtime->oss.trigger)
1423 goto _skip2;
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);
1430 if (err < 0)
1431 return err;
1433 _skip2:
1434 return 0;
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;
1440 int result = 0;
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;
1448 return result;
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;
1456 int err;
1458 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459 if (substream == NULL)
1460 return -EINVAL;
1461 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1462 return err;
1463 runtime = substream->runtime;
1464 if (runtime->oss.params || runtime->oss.prepare)
1465 return 0;
1466 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1467 if (err == -EPIPE)
1468 delay = 0; /* hack for broken OSS applications */
1469 else if (err < 0)
1470 return err;
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;
1479 int fixup;
1480 struct count_info info;
1481 int err;
1483 if (_info == NULL)
1484 return -EFAULT;
1485 substream = pcm_oss_file->streams[stream];
1486 if (substream == NULL)
1487 return -EINVAL;
1488 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1489 return err;
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)))
1494 return -EFAULT;
1495 return 0;
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)) {
1500 err = 0;
1501 delay = 0;
1502 fixup = 0;
1503 } else {
1504 fixup = runtime->oss.buffer_used;
1506 } else {
1507 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1508 fixup = -runtime->oss.buffer_used;
1510 if (err < 0)
1511 return err;
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;
1516 if (n < 0)
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;
1523 } else {
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;
1528 else
1529 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1531 if (copy_to_user(_info, &info, sizeof(info)))
1532 return -EFAULT;
1533 return 0;
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;
1541 int fixup;
1542 struct audio_buf_info info;
1543 int err;
1545 if (_info == NULL)
1546 return -EFAULT;
1547 substream = pcm_oss_file->streams[stream];
1548 if (substream == NULL)
1549 return -EINVAL;
1550 runtime = substream->runtime;
1552 if (runtime->oss.params &&
1553 (err = snd_pcm_oss_change_params(substream)) < 0)
1554 return err;
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;
1562 } else {
1563 info.bytes = 0;
1564 info.fragments = 0;
1566 } else {
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;
1571 err = 0;
1572 fixup = 0;
1573 } else {
1574 avail = runtime->buffer_size - avail;
1575 fixup = -runtime->oss.buffer_used;
1577 } else {
1578 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1579 fixup = runtime->oss.buffer_used;
1581 if (err < 0)
1582 return err;
1583 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1584 info.fragments = info.bytes / runtime->oss.period_bytes;
1587 #ifdef OSS_DEBUG
1588 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1589 #endif
1590 if (copy_to_user(_info, &info, sizeof(info)))
1591 return -EFAULT;
1592 return 0;
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");
1599 return -EINVAL;
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);
1611 return setup;
1614 ptr = ptrl = task_name;
1615 while (*ptr) {
1616 if (*ptr == '/')
1617 ptrl = ptr + 1;
1618 ptr++;
1620 if (ptrl == task_name) {
1621 goto __not_found;
1622 return NULL;
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);
1627 return setup;
1630 __not_found:
1631 up(&pcm->streams[stream].oss.setup_mutex);
1632 return NULL;
1635 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1636 snd_pcm_oss_setup_t *setup,
1637 int minor)
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;
1650 break;
1651 case SNDRV_MINOR_OSS_PCM_16:
1652 runtime->oss.format = AFMT_S16_LE;
1653 break;
1654 default:
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)
1676 int cidx;
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)
1682 continue;
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);
1700 return 0;
1703 static int snd_pcm_oss_open_file(struct file *file,
1704 snd_pcm_t *pcm,
1705 snd_pcm_oss_file_t **rpcm_oss_file,
1706 int minor,
1707 snd_pcm_oss_setup_t *psetup,
1708 snd_pcm_oss_setup_t *csetup)
1710 int err = 0;
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)
1720 return -ENOMEM;
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);
1729 return err;
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);
1738 return err;
1739 } else {
1740 csubstream = NULL;
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);
1748 return -EINVAL;
1750 if (psubstream != NULL) {
1751 psubstream->oss.file = pcm_oss_file;
1752 err = snd_pcm_hw_constraints_init(psubstream);
1753 if (err < 0) {
1754 snd_printd("snd_pcm_hw_constraint_init failed\n");
1755 snd_pcm_oss_release_file(pcm_oss_file);
1756 return err;
1758 if ((err = psubstream->ops->open(psubstream)) < 0) {
1759 snd_pcm_oss_release_file(pcm_oss_file);
1760 return err;
1762 psubstream->open_flag = 1;
1763 err = snd_pcm_hw_constraints_complete(psubstream);
1764 if (err < 0) {
1765 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1766 snd_pcm_oss_release_file(pcm_oss_file);
1767 return err;
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);
1775 if (err < 0) {
1776 snd_printd("snd_pcm_hw_constraint_init failed\n");
1777 snd_pcm_oss_release_file(pcm_oss_file);
1778 return err;
1780 if ((err = csubstream->ops->open(csubstream)) < 0) {
1781 snd_pcm_oss_release_file(pcm_oss_file);
1782 return err;
1784 csubstream->open_flag = 1;
1785 err = snd_pcm_hw_constraints_complete(csubstream);
1786 if (err < 0) {
1787 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1788 snd_pcm_oss_release_file(pcm_oss_file);
1789 return err;
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;
1797 return 0;
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);
1805 int device;
1806 int err;
1807 char task_name[32];
1808 snd_pcm_t *pcm;
1809 snd_pcm_oss_file_t *pcm_oss_file;
1810 snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1811 int nonblock;
1812 wait_queue_t wait;
1814 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1815 device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1816 adsp_map[cardnum] : dsp_map[cardnum];
1818 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1819 if (pcm == NULL) {
1820 err = -ENODEV;
1821 goto __error1;
1823 err = snd_card_file_add(pcm->card, file);
1824 if (err < 0)
1825 goto __error1;
1826 if (!try_module_get(pcm->card->module)) {
1827 err = -EFAULT;
1828 goto __error2;
1830 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1831 err = -EFAULT;
1832 goto __error;
1834 if (file->f_mode & FMODE_WRITE)
1835 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1836 if (file->f_mode & FMODE_READ)
1837 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1839 nonblock = !!(file->f_flags & O_NONBLOCK);
1840 if (psetup && !psetup->disable) {
1841 if (psetup->nonblock)
1842 nonblock = 1;
1843 else if (psetup->block)
1844 nonblock = 0;
1845 } else if (csetup && !csetup->disable) {
1846 if (csetup->nonblock)
1847 nonblock = 1;
1848 else if (csetup->block)
1849 nonblock = 0;
1851 if (!nonblock)
1852 nonblock = nonblock_open;
1854 init_waitqueue_entry(&wait, current);
1855 add_wait_queue(&pcm->open_wait, &wait);
1856 down(&pcm->open_mutex);
1857 while (1) {
1858 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1859 minor, psetup, csetup);
1860 if (err >= 0)
1861 break;
1862 if (err == -EAGAIN) {
1863 if (nonblock) {
1864 err = -EBUSY;
1865 break;
1867 } else
1868 break;
1869 set_current_state(TASK_INTERRUPTIBLE);
1870 up(&pcm->open_mutex);
1871 schedule();
1872 down(&pcm->open_mutex);
1873 if (signal_pending(current)) {
1874 err = -ERESTARTSYS;
1875 break;
1878 remove_wait_queue(&pcm->open_wait, &wait);
1879 up(&pcm->open_mutex);
1880 if (err < 0)
1881 goto __error;
1882 return err;
1884 __error:
1885 module_put(pcm->card->module);
1886 __error2:
1887 snd_card_file_remove(pcm->card, file);
1888 __error1:
1889 return err;
1892 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1894 snd_pcm_t *pcm;
1895 snd_pcm_substream_t *substream;
1896 snd_pcm_oss_file_t *pcm_oss_file;
1898 pcm_oss_file = file->private_data;
1899 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1900 if (substream == NULL)
1901 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1902 snd_assert(substream != NULL, return -ENXIO);
1903 pcm = substream->pcm;
1904 snd_pcm_oss_sync(pcm_oss_file);
1905 down(&pcm->open_mutex);
1906 snd_pcm_oss_release_file(pcm_oss_file);
1907 up(&pcm->open_mutex);
1908 wake_up(&pcm->open_wait);
1909 module_put(pcm->card->module);
1910 snd_card_file_remove(pcm->card, file);
1911 return 0;
1914 static inline int _snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1915 unsigned int cmd, unsigned long arg)
1917 snd_pcm_oss_file_t *pcm_oss_file;
1918 int __user *p = (int __user *)arg;
1919 int res;
1921 pcm_oss_file = file->private_data;
1922 if (cmd == OSS_GETVERSION)
1923 return put_user(SNDRV_OSS_VERSION, p);
1924 if (cmd == OSS_ALSAEMULVER)
1925 return put_user(1, p);
1926 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1927 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1928 snd_pcm_substream_t *substream;
1929 int idx;
1930 for (idx = 0; idx < 2; ++idx) {
1931 substream = pcm_oss_file->streams[idx];
1932 if (substream != NULL)
1933 break;
1935 snd_assert(substream != NULL, return -ENXIO);
1936 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1938 #endif
1939 if (((cmd >> 8) & 0xff) != 'P')
1940 return -EINVAL;
1941 #ifdef OSS_DEBUG
1942 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1943 #endif
1944 switch (cmd) {
1945 case SNDCTL_DSP_RESET:
1946 return snd_pcm_oss_reset(pcm_oss_file);
1947 case SNDCTL_DSP_SYNC:
1948 return snd_pcm_oss_sync(pcm_oss_file);
1949 case SNDCTL_DSP_SPEED:
1950 if (get_user(res, p))
1951 return -EFAULT;
1952 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1953 return res;
1954 return put_user(res, p);
1955 case SOUND_PCM_READ_RATE:
1956 res = snd_pcm_oss_get_rate(pcm_oss_file);
1957 if (res < 0)
1958 return res;
1959 return put_user(res, p);
1960 case SNDCTL_DSP_STEREO:
1961 if (get_user(res, p))
1962 return -EFAULT;
1963 res = res > 0 ? 2 : 1;
1964 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1965 return res;
1966 return put_user(--res, p);
1967 case SNDCTL_DSP_GETBLKSIZE:
1968 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1969 if (res < 0)
1970 return res;
1971 return put_user(res, p);
1972 case SNDCTL_DSP_SETFMT:
1973 if (get_user(res, p))
1974 return -EFAULT;
1975 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1976 if (res < 0)
1977 return res;
1978 return put_user(res, p);
1979 case SOUND_PCM_READ_BITS:
1980 res = snd_pcm_oss_get_format(pcm_oss_file);
1981 if (res < 0)
1982 return res;
1983 return put_user(res, p);
1984 case SNDCTL_DSP_CHANNELS:
1985 if (get_user(res, p))
1986 return -EFAULT;
1987 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1988 if (res < 0)
1989 return res;
1990 return put_user(res, p);
1991 case SOUND_PCM_READ_CHANNELS:
1992 res = snd_pcm_oss_get_channels(pcm_oss_file);
1993 if (res < 0)
1994 return res;
1995 return put_user(res, p);
1996 case SOUND_PCM_WRITE_FILTER:
1997 case SOUND_PCM_READ_FILTER:
1998 return -EIO;
1999 case SNDCTL_DSP_POST:
2000 return snd_pcm_oss_post(pcm_oss_file);
2001 case SNDCTL_DSP_SUBDIVIDE:
2002 if (get_user(res, p))
2003 return -EFAULT;
2004 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2005 if (res < 0)
2006 return res;
2007 return put_user(res, p);
2008 case SNDCTL_DSP_SETFRAGMENT:
2009 if (get_user(res, p))
2010 return -EFAULT;
2011 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2012 case SNDCTL_DSP_GETFMTS:
2013 res = snd_pcm_oss_get_formats(pcm_oss_file);
2014 if (res < 0)
2015 return res;
2016 return put_user(res, p);
2017 case SNDCTL_DSP_GETOSPACE:
2018 case SNDCTL_DSP_GETISPACE:
2019 return snd_pcm_oss_get_space(pcm_oss_file,
2020 cmd == SNDCTL_DSP_GETISPACE ?
2021 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2022 (struct audio_buf_info __user *) arg);
2023 case SNDCTL_DSP_NONBLOCK:
2024 return snd_pcm_oss_nonblock(file);
2025 case SNDCTL_DSP_GETCAPS:
2026 res = snd_pcm_oss_get_caps(pcm_oss_file);
2027 if (res < 0)
2028 return res;
2029 return put_user(res, p);
2030 case SNDCTL_DSP_GETTRIGGER:
2031 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2032 if (res < 0)
2033 return res;
2034 return put_user(res, p);
2035 case SNDCTL_DSP_SETTRIGGER:
2036 if (get_user(res, p))
2037 return -EFAULT;
2038 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2039 case SNDCTL_DSP_GETIPTR:
2040 case SNDCTL_DSP_GETOPTR:
2041 return snd_pcm_oss_get_ptr(pcm_oss_file,
2042 cmd == SNDCTL_DSP_GETIPTR ?
2043 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2044 (struct count_info __user *) arg);
2045 case SNDCTL_DSP_MAPINBUF:
2046 case SNDCTL_DSP_MAPOUTBUF:
2047 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2048 cmd == SNDCTL_DSP_MAPINBUF ?
2049 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2050 (struct buffmem_desc __user *) arg);
2051 case SNDCTL_DSP_SETSYNCRO:
2052 /* stop DMA now.. */
2053 return 0;
2054 case SNDCTL_DSP_SETDUPLEX:
2055 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2056 return 0;
2057 return -EIO;
2058 case SNDCTL_DSP_GETODELAY:
2059 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2060 if (res < 0) {
2061 /* it's for sure, some broken apps don't check for error codes */
2062 put_user(0, p);
2063 return res;
2065 return put_user(res, p);
2066 case SNDCTL_DSP_PROFILE:
2067 return 0; /* silently ignore */
2068 default:
2069 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2071 return -EINVAL;
2074 /* FIXME: need to unlock BKL to allow preemption */
2075 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
2076 unsigned int cmd, unsigned long arg)
2078 int err;
2079 unlock_kernel();
2080 err = _snd_pcm_oss_ioctl(inode, file, cmd, arg);
2081 lock_kernel();
2082 return err;
2085 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2087 snd_pcm_oss_file_t *pcm_oss_file;
2088 snd_pcm_substream_t *substream;
2090 pcm_oss_file = file->private_data;
2091 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2092 if (substream == NULL)
2093 return -ENXIO;
2094 #ifndef OSS_DEBUG
2095 return snd_pcm_oss_read1(substream, buf, count);
2096 #else
2098 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2099 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2100 return res;
2102 #endif
2105 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2107 snd_pcm_oss_file_t *pcm_oss_file;
2108 snd_pcm_substream_t *substream;
2109 long result;
2111 pcm_oss_file = file->private_data;
2112 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2113 if (substream == NULL)
2114 return -ENXIO;
2115 up(&file->f_dentry->d_inode->i_sem);
2116 result = snd_pcm_oss_write1(substream, buf, count);
2117 down(&file->f_dentry->d_inode->i_sem);
2118 #ifdef OSS_DEBUG
2119 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2120 #endif
2121 return result;
2124 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2126 snd_pcm_runtime_t *runtime = substream->runtime;
2127 if (atomic_read(&runtime->mmap_count))
2128 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2129 else
2130 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2133 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2135 snd_pcm_runtime_t *runtime = substream->runtime;
2136 if (atomic_read(&runtime->mmap_count))
2137 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2138 else
2139 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2142 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2144 snd_pcm_oss_file_t *pcm_oss_file;
2145 unsigned int mask;
2146 snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2148 pcm_oss_file = file->private_data;
2150 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2151 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2153 mask = 0;
2154 if (psubstream != NULL) {
2155 snd_pcm_runtime_t *runtime = psubstream->runtime;
2156 poll_wait(file, &runtime->sleep, wait);
2157 snd_pcm_stream_lock_irq(psubstream);
2158 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2159 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2160 snd_pcm_oss_playback_ready(psubstream)))
2161 mask |= POLLOUT | POLLWRNORM;
2162 snd_pcm_stream_unlock_irq(psubstream);
2164 if (csubstream != NULL) {
2165 snd_pcm_runtime_t *runtime = csubstream->runtime;
2166 enum sndrv_pcm_state ostate;
2167 poll_wait(file, &runtime->sleep, wait);
2168 snd_pcm_stream_lock_irq(csubstream);
2169 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2170 snd_pcm_oss_capture_ready(csubstream))
2171 mask |= POLLIN | POLLRDNORM;
2172 snd_pcm_stream_unlock_irq(csubstream);
2173 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2174 snd_pcm_oss_file_t ofile;
2175 memset(&ofile, 0, sizeof(ofile));
2176 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2177 runtime->oss.trigger = 0;
2178 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2182 return mask;
2185 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2187 snd_pcm_oss_file_t *pcm_oss_file;
2188 snd_pcm_substream_t *substream = NULL;
2189 snd_pcm_runtime_t *runtime;
2190 int err;
2192 #ifdef OSS_DEBUG
2193 printk("pcm_oss: mmap begin\n");
2194 #endif
2195 pcm_oss_file = file->private_data;
2196 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2197 case VM_READ | VM_WRITE:
2198 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2199 if (substream)
2200 break;
2201 /* Fall through */
2202 case VM_READ:
2203 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2204 break;
2205 case VM_WRITE:
2206 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2207 break;
2208 default:
2209 return -EINVAL;
2211 /* set VM_READ access as well to fix memset() routines that do
2212 reads before writes (to improve performance) */
2213 area->vm_flags |= VM_READ;
2214 if (substream == NULL)
2215 return -ENXIO;
2216 runtime = substream->runtime;
2217 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2218 return -EIO;
2219 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2220 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2221 else
2222 return -EIO;
2224 if (runtime->oss.params) {
2225 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2226 return err;
2228 if (runtime->oss.plugin_first != NULL)
2229 return -EIO;
2231 if (area->vm_pgoff != 0)
2232 return -EINVAL;
2234 err = snd_pcm_mmap_data(substream, file, area);
2235 if (err < 0)
2236 return err;
2237 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2238 runtime->silence_threshold = 0;
2239 runtime->silence_size = 0;
2240 #ifdef OSS_DEBUG
2241 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2242 #endif
2243 /* In mmap mode we never stop */
2244 runtime->stop_threshold = runtime->boundary;
2246 return 0;
2250 * /proc interface
2253 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2254 snd_info_buffer_t * buffer)
2256 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2257 snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2258 down(&pstr->oss.setup_mutex);
2259 while (setup) {
2260 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2261 setup->task_name,
2262 setup->periods,
2263 setup->period_size,
2264 setup->disable ? " disable" : "",
2265 setup->direct ? " direct" : "",
2266 setup->block ? " block" : "",
2267 setup->nonblock ? " non-block" : "",
2268 setup->partialfrag ? " partial-frag" : "",
2269 setup->nosilence ? " no-silence" : "");
2270 setup = setup->next;
2272 up(&pstr->oss.setup_mutex);
2275 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2277 unsigned int idx;
2278 snd_pcm_substream_t *substream;
2279 snd_pcm_oss_setup_t *setup, *setupn;
2281 for (idx = 0, substream = pstr->substream;
2282 idx < pstr->substream_count; idx++, substream = substream->next)
2283 substream->oss.setup = NULL;
2284 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2285 setup; setup = setupn) {
2286 setupn = setup->next;
2287 kfree(setup->task_name);
2288 kfree(setup);
2290 pstr->oss.setup_list = NULL;
2293 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2294 snd_info_buffer_t * buffer)
2296 snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2297 char line[256], str[32], task_name[32], *ptr;
2298 int idx1;
2299 snd_pcm_oss_setup_t *setup, *setup1, template;
2301 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2302 down(&pstr->oss.setup_mutex);
2303 memset(&template, 0, sizeof(template));
2304 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2305 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2306 snd_pcm_oss_proc_free_setup_list(pstr);
2307 up(&pstr->oss.setup_mutex);
2308 continue;
2310 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2311 if (!strcmp(setup->task_name, task_name)) {
2312 template = *setup;
2313 break;
2316 ptr = snd_info_get_str(str, ptr, sizeof(str));
2317 template.periods = simple_strtoul(str, NULL, 10);
2318 ptr = snd_info_get_str(str, ptr, sizeof(str));
2319 template.period_size = simple_strtoul(str, NULL, 10);
2320 for (idx1 = 31; idx1 >= 0; idx1--)
2321 if (template.period_size & (1 << idx1))
2322 break;
2323 for (idx1--; idx1 >= 0; idx1--)
2324 template.period_size &= ~(1 << idx1);
2325 do {
2326 ptr = snd_info_get_str(str, ptr, sizeof(str));
2327 if (!strcmp(str, "disable")) {
2328 template.disable = 1;
2329 } else if (!strcmp(str, "direct")) {
2330 template.direct = 1;
2331 } else if (!strcmp(str, "block")) {
2332 template.block = 1;
2333 } else if (!strcmp(str, "non-block")) {
2334 template.nonblock = 1;
2335 } else if (!strcmp(str, "partial-frag")) {
2336 template.partialfrag = 1;
2337 } else if (!strcmp(str, "no-silence")) {
2338 template.nosilence = 1;
2340 } while (*str);
2341 if (setup == NULL) {
2342 setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2343 if (setup) {
2344 if (pstr->oss.setup_list == NULL) {
2345 pstr->oss.setup_list = setup;
2346 } else {
2347 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2348 setup1->next = setup;
2350 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2351 } else {
2352 buffer->error = -ENOMEM;
2355 if (setup)
2356 *setup = template;
2357 up(&pstr->oss.setup_mutex);
2361 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2363 int stream;
2364 for (stream = 0; stream < 2; ++stream) {
2365 snd_info_entry_t *entry;
2366 snd_pcm_str_t *pstr = &pcm->streams[stream];
2367 if (pstr->substream_count == 0)
2368 continue;
2369 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2370 entry->content = SNDRV_INFO_CONTENT_TEXT;
2371 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2372 entry->c.text.read_size = 8192;
2373 entry->c.text.read = snd_pcm_oss_proc_read;
2374 entry->c.text.write_size = 8192;
2375 entry->c.text.write = snd_pcm_oss_proc_write;
2376 entry->private_data = pstr;
2377 if (snd_info_register(entry) < 0) {
2378 snd_info_free_entry(entry);
2379 entry = NULL;
2382 pstr->oss.proc_entry = entry;
2386 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2388 int stream;
2389 for (stream = 0; stream < 2; ++stream) {
2390 snd_pcm_str_t *pstr = &pcm->streams[stream];
2391 if (pstr->oss.proc_entry) {
2392 snd_info_unregister(pstr->oss.proc_entry);
2393 pstr->oss.proc_entry = NULL;
2394 snd_pcm_oss_proc_free_setup_list(pstr);
2400 * ENTRY functions
2403 static struct file_operations snd_pcm_oss_f_reg =
2405 .owner = THIS_MODULE,
2406 .read = snd_pcm_oss_read,
2407 .write = snd_pcm_oss_write,
2408 .open = snd_pcm_oss_open,
2409 .release = snd_pcm_oss_release,
2410 .poll = snd_pcm_oss_poll,
2411 .ioctl = snd_pcm_oss_ioctl,
2412 .mmap = snd_pcm_oss_mmap,
2415 static snd_minor_t snd_pcm_oss_reg =
2417 .comment = "digital audio",
2418 .f_ops = &snd_pcm_oss_f_reg,
2421 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2423 char name[128];
2424 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2425 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2426 pcm->card, index, &snd_pcm_oss_reg,
2427 name) < 0) {
2428 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2432 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2434 pcm->oss.reg = 0;
2435 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2436 char name[128];
2437 int duplex;
2438 register_oss_dsp(pcm, 0);
2439 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2440 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2441 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2442 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2443 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2444 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2445 pcm->card->number,
2446 name);
2447 #endif
2448 pcm->oss.reg++;
2449 pcm->oss.reg_mask |= 1;
2451 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2452 register_oss_dsp(pcm, 1);
2453 pcm->oss.reg++;
2454 pcm->oss.reg_mask |= 2;
2457 if (pcm->oss.reg)
2458 snd_pcm_oss_proc_init(pcm);
2460 return 0;
2463 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2465 if (pcm->oss.reg) {
2466 if (pcm->oss.reg_mask & 1) {
2467 pcm->oss.reg_mask &= ~1;
2468 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2469 pcm->card, 0);
2471 if (pcm->oss.reg_mask & 2) {
2472 pcm->oss.reg_mask &= ~2;
2473 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2474 pcm->card, 1);
2477 return 0;
2480 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2482 snd_pcm_oss_disconnect_minor(pcm);
2483 if (pcm->oss.reg) {
2484 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2485 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2486 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2487 #endif
2489 pcm->oss.reg = 0;
2490 snd_pcm_oss_proc_done(pcm);
2492 return 0;
2495 static snd_pcm_notify_t snd_pcm_oss_notify =
2497 .n_register = snd_pcm_oss_register_minor,
2498 .n_disconnect = snd_pcm_oss_disconnect_minor,
2499 .n_unregister = snd_pcm_oss_unregister_minor,
2502 static int __init alsa_pcm_oss_init(void)
2504 int i;
2505 int err;
2507 /* check device map table */
2508 for (i = 0; i < SNDRV_CARDS; i++) {
2509 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2510 snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2511 dsp_map[i] = 0;
2513 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2514 snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2515 adsp_map[i] = 1;
2518 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2519 return err;
2520 return 0;
2523 static void __exit alsa_pcm_oss_exit(void)
2525 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2528 module_init(alsa_pcm_oss_init)
2529 module_exit(alsa_pcm_oss_exit)