[PATCH] kref: avoid an atomic operation in kref_put()
[linux-2.6/openmoko-kernel/knife-kernel.git] / sound / core / oss / pcm_oss.c
blob7fd072392c7e89ad0eec524a51563c42477fcbf9
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 <linux/string.h>
37 #include <sound/core.h>
38 #include <sound/minors.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include "pcm_plugin.h"
42 #include <sound/info.h>
43 #include <linux/soundcard.h>
44 #include <sound/initval.h>
46 #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
48 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
49 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50 static int nonblock_open = 1;
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, NULL, 0444);
56 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57 module_param_array(adsp_map, int, NULL, 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(struct snd_card *card, unsigned int cmd, unsigned long arg);
65 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
66 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
67 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *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 static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
83 struct snd_pcm_runtime *runtime = substream->runtime;
84 struct snd_pcm_plugin *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 static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
98 struct snd_pcm_runtime *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(struct snd_pcm_plugin *plugin)
113 struct snd_pcm_runtime *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(struct snd_pcm_substream *substream, long frames)
128 struct snd_pcm_runtime *runtime = substream->runtime;
129 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
130 long bytes = frames_to_bytes(runtime, frames);
131 if (buffer_size == runtime->oss.buffer_bytes)
132 return bytes;
133 #if BITS_PER_LONG >= 64
134 return runtime->oss.buffer_bytes * bytes / buffer_size;
135 #else
137 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
138 u32 rem;
139 div64_32(&bsize, buffer_size, &rem);
140 return (long)bsize;
142 #endif
145 static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
147 struct snd_pcm_runtime *runtime = substream->runtime;
148 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
149 if (buffer_size == runtime->oss.buffer_bytes)
150 return bytes_to_frames(runtime, bytes);
151 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
154 static int snd_pcm_oss_format_from(int format)
156 switch (format) {
157 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
158 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
159 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
160 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
161 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
162 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
163 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
164 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
165 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
166 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
167 default: return SNDRV_PCM_FORMAT_U8;
171 static int snd_pcm_oss_format_to(int format)
173 switch (format) {
174 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
175 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
176 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
177 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
178 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
179 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
180 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
181 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
182 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
183 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
184 default: return -EINVAL;
188 static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
189 struct snd_pcm_hw_params *oss_params,
190 struct snd_pcm_hw_params *slave_params)
192 size_t s;
193 size_t oss_buffer_size, oss_period_size, oss_periods;
194 size_t min_period_size, max_period_size;
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 size_t oss_frame_size;
198 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
199 params_channels(oss_params) / 8;
201 oss_buffer_size = snd_pcm_plug_client_size(substream,
202 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
203 oss_buffer_size = 1 << ld2(oss_buffer_size);
204 if (atomic_read(&runtime->mmap_count)) {
205 if (oss_buffer_size > runtime->oss.mmap_bytes)
206 oss_buffer_size = runtime->oss.mmap_bytes;
209 if (substream->oss.setup &&
210 substream->oss.setup->period_size > 16)
211 oss_period_size = substream->oss.setup->period_size;
212 else if (runtime->oss.fragshift) {
213 oss_period_size = 1 << runtime->oss.fragshift;
214 if (oss_period_size > oss_buffer_size / 2)
215 oss_period_size = oss_buffer_size / 2;
216 } else {
217 int sd;
218 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
220 oss_period_size = oss_buffer_size;
221 do {
222 oss_period_size /= 2;
223 } while (oss_period_size > bytes_per_sec);
224 if (runtime->oss.subdivision == 0) {
225 sd = 4;
226 if (oss_period_size / sd > 4096)
227 sd *= 2;
228 if (oss_period_size / sd < 4096)
229 sd = 1;
230 } else
231 sd = runtime->oss.subdivision;
232 oss_period_size /= sd;
233 if (oss_period_size < 16)
234 oss_period_size = 16;
237 min_period_size = snd_pcm_plug_client_size(substream,
238 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
239 min_period_size *= oss_frame_size;
240 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
241 if (oss_period_size < min_period_size)
242 oss_period_size = min_period_size;
244 max_period_size = snd_pcm_plug_client_size(substream,
245 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
246 max_period_size *= oss_frame_size;
247 max_period_size = 1 << ld2(max_period_size);
248 if (oss_period_size > max_period_size)
249 oss_period_size = max_period_size;
251 oss_periods = oss_buffer_size / oss_period_size;
253 if (substream->oss.setup) {
254 if (substream->oss.setup->periods > 1)
255 oss_periods = substream->oss.setup->periods;
258 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
259 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
260 s = runtime->oss.maxfrags;
261 if (oss_periods > s)
262 oss_periods = s;
264 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
265 if (s < 2)
266 s = 2;
267 if (oss_periods < s)
268 oss_periods = s;
270 while (oss_period_size * oss_periods > oss_buffer_size)
271 oss_period_size /= 2;
273 snd_assert(oss_period_size >= 16, return -EINVAL);
274 runtime->oss.period_bytes = oss_period_size;
275 runtime->oss.period_frames = 1;
276 runtime->oss.periods = oss_periods;
277 return 0;
280 static int choose_rate(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params, unsigned int best_rate)
283 struct snd_interval *it;
284 struct snd_pcm_hw_params *save;
285 unsigned int rate, prev;
287 save = kmalloc(sizeof(*save), GFP_KERNEL);
288 if (save == NULL)
289 return -ENOMEM;
290 *save = *params;
291 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
293 /* try multiples of the best rate */
294 rate = best_rate;
295 for (;;) {
296 if (it->max < rate || (it->max == rate && it->openmax))
297 break;
298 if (it->min < rate || (it->min == rate && !it->openmin)) {
299 int ret;
300 ret = snd_pcm_hw_param_set(substream, params,
301 SNDRV_PCM_HW_PARAM_RATE,
302 rate, 0);
303 if (ret == (int)rate) {
304 kfree(save);
305 return rate;
307 *params = *save;
309 prev = rate;
310 rate += best_rate;
311 if (rate <= prev)
312 break;
315 /* not found, use the nearest rate */
316 kfree(save);
317 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
320 static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
322 struct snd_pcm_runtime *runtime = substream->runtime;
323 struct snd_pcm_hw_params *params, *sparams;
324 struct snd_pcm_sw_params *sw_params;
325 ssize_t oss_buffer_size, oss_period_size;
326 size_t oss_frame_size;
327 int err;
328 int direct;
329 int format, sformat, n;
330 struct snd_mask sformat_mask;
331 struct snd_mask mask;
333 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
334 params = kmalloc(sizeof(*params), GFP_KERNEL);
335 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
336 if (!sw_params || !params || !sparams) {
337 snd_printd("No memory\n");
338 err = -ENOMEM;
339 goto failure;
342 if (atomic_read(&runtime->mmap_count)) {
343 direct = 1;
344 } else {
345 struct snd_pcm_oss_setup *setup = substream->oss.setup;
346 direct = (setup != NULL && setup->direct);
349 _snd_pcm_hw_params_any(sparams);
350 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
351 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
352 snd_mask_none(&mask);
353 if (atomic_read(&runtime->mmap_count))
354 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
355 else {
356 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
357 if (!direct)
358 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
360 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
361 if (err < 0) {
362 snd_printd("No usable accesses\n");
363 err = -EINVAL;
364 goto failure;
366 choose_rate(substream, sparams, runtime->oss.rate);
367 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
369 format = snd_pcm_oss_format_from(runtime->oss.format);
371 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
372 if (direct)
373 sformat = format;
374 else
375 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
377 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
378 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
379 if (snd_mask_test(&sformat_mask, sformat) &&
380 snd_pcm_oss_format_to(sformat) >= 0)
381 break;
383 if (sformat > SNDRV_PCM_FORMAT_LAST) {
384 snd_printd("Cannot find a format!!!\n");
385 err = -EINVAL;
386 goto failure;
389 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
390 snd_assert(err >= 0, goto failure);
392 if (direct) {
393 memcpy(params, sparams, sizeof(*params));
394 } else {
395 _snd_pcm_hw_params_any(params);
396 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
397 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
398 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
399 snd_pcm_oss_format_from(runtime->oss.format), 0);
400 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
401 runtime->oss.channels, 0);
402 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
403 runtime->oss.rate, 0);
404 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
405 params_access(params), params_format(params),
406 params_channels(params), params_rate(params));
408 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
409 params_access(sparams), params_format(sparams),
410 params_channels(sparams), params_rate(sparams));
412 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
413 params_channels(params) / 8;
415 snd_pcm_oss_plugin_clear(substream);
416 if (!direct) {
417 /* add necessary plugins */
418 snd_pcm_oss_plugin_clear(substream);
419 if ((err = snd_pcm_plug_format_plugins(substream,
420 params,
421 sparams)) < 0) {
422 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
423 snd_pcm_oss_plugin_clear(substream);
424 goto failure;
426 if (runtime->oss.plugin_first) {
427 struct snd_pcm_plugin *plugin;
428 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
429 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
430 snd_pcm_oss_plugin_clear(substream);
431 goto failure;
433 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
434 err = snd_pcm_plugin_append(plugin);
435 } else {
436 err = snd_pcm_plugin_insert(plugin);
438 if (err < 0) {
439 snd_pcm_oss_plugin_clear(substream);
440 goto failure;
445 err = snd_pcm_oss_period_size(substream, params, sparams);
446 if (err < 0)
447 goto failure;
449 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
450 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
451 snd_assert(err >= 0, goto failure);
453 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
454 runtime->oss.periods, NULL);
455 snd_assert(err >= 0, goto failure);
457 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
459 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
460 snd_printd("HW_PARAMS failed: %i\n", err);
461 goto failure;
464 memset(sw_params, 0, sizeof(*sw_params));
465 if (runtime->oss.trigger) {
466 sw_params->start_threshold = 1;
467 } else {
468 sw_params->start_threshold = runtime->boundary;
470 if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
471 sw_params->stop_threshold = runtime->boundary;
472 else
473 sw_params->stop_threshold = runtime->buffer_size;
474 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
475 sw_params->period_step = 1;
476 sw_params->sleep_min = 0;
477 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
478 1 : runtime->period_size;
479 sw_params->xfer_align = 1;
480 if (atomic_read(&runtime->mmap_count) ||
481 (substream->oss.setup && substream->oss.setup->nosilence)) {
482 sw_params->silence_threshold = 0;
483 sw_params->silence_size = 0;
484 } else {
485 snd_pcm_uframes_t frames;
486 frames = runtime->period_size + 16;
487 if (frames > runtime->buffer_size)
488 frames = runtime->buffer_size;
489 sw_params->silence_threshold = frames;
490 sw_params->silence_size = frames;
493 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
494 snd_printd("SW_PARAMS failed: %i\n", err);
495 goto failure;
498 runtime->oss.periods = params_periods(sparams);
499 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
500 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
501 if (runtime->oss.plugin_first) {
502 err = snd_pcm_plug_alloc(substream, oss_period_size);
503 if (err < 0)
504 goto failure;
506 oss_period_size *= oss_frame_size;
508 oss_buffer_size = oss_period_size * runtime->oss.periods;
509 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
511 runtime->oss.period_bytes = oss_period_size;
512 runtime->oss.buffer_bytes = oss_buffer_size;
514 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
515 runtime->oss.period_bytes,
516 runtime->oss.buffer_bytes);
517 pdprintf("slave: period_size = %i, buffer_size = %i\n",
518 params_period_size(sparams),
519 params_buffer_size(sparams));
521 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
522 runtime->oss.channels = params_channels(params);
523 runtime->oss.rate = params_rate(params);
525 runtime->oss.params = 0;
526 runtime->oss.prepare = 1;
527 vfree(runtime->oss.buffer);
528 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
529 runtime->oss.buffer_used = 0;
530 if (runtime->dma_area)
531 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
533 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
535 err = 0;
536 failure:
537 kfree(sw_params);
538 kfree(params);
539 kfree(sparams);
540 return err;
543 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
545 int idx, err;
546 struct snd_pcm_substream *asubstream = NULL, *substream;
548 for (idx = 0; idx < 2; idx++) {
549 substream = pcm_oss_file->streams[idx];
550 if (substream == NULL)
551 continue;
552 if (asubstream == NULL)
553 asubstream = substream;
554 if (substream->runtime->oss.params) {
555 err = snd_pcm_oss_change_params(substream);
556 if (err < 0)
557 return err;
560 snd_assert(asubstream != NULL, return -EIO);
561 if (r_substream)
562 *r_substream = asubstream;
563 return 0;
566 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
568 int err;
569 struct snd_pcm_runtime *runtime = substream->runtime;
571 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
572 if (err < 0) {
573 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
574 return err;
576 runtime->oss.prepare = 0;
577 runtime->oss.prev_hw_ptr_interrupt = 0;
578 runtime->oss.period_ptr = 0;
579 runtime->oss.buffer_used = 0;
581 return 0;
584 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
586 struct snd_pcm_runtime *runtime;
587 int err;
589 if (substream == NULL)
590 return 0;
591 runtime = substream->runtime;
592 if (runtime->oss.params) {
593 err = snd_pcm_oss_change_params(substream);
594 if (err < 0)
595 return err;
597 if (runtime->oss.prepare) {
598 err = snd_pcm_oss_prepare(substream);
599 if (err < 0)
600 return err;
602 return 0;
605 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
607 struct snd_pcm_runtime *runtime;
608 snd_pcm_uframes_t frames;
609 int err = 0;
611 while (1) {
612 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
613 if (err < 0)
614 break;
615 runtime = substream->runtime;
616 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
617 break;
618 /* in case of overrun, skip whole periods like OSS/Linux driver does */
619 /* until avail(delay) <= buffer_size */
620 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
621 frames /= runtime->period_size;
622 frames *= runtime->period_size;
623 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
624 if (err < 0)
625 break;
627 return err;
630 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
632 struct snd_pcm_runtime *runtime = substream->runtime;
633 int ret;
634 while (1) {
635 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
636 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
637 #ifdef OSS_DEBUG
638 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
639 printk("pcm_oss: write: recovering from XRUN\n");
640 else
641 printk("pcm_oss: write: recovering from SUSPEND\n");
642 #endif
643 ret = snd_pcm_oss_prepare(substream);
644 if (ret < 0)
645 break;
647 if (in_kernel) {
648 mm_segment_t fs;
649 fs = snd_enter_user();
650 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
651 snd_leave_user(fs);
652 } else {
653 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
655 if (ret != -EPIPE && ret != -ESTRPIPE)
656 break;
657 /* test, if we can't store new data, because the stream */
658 /* has not been started */
659 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
660 return -EAGAIN;
662 return ret;
665 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
667 struct snd_pcm_runtime *runtime = substream->runtime;
668 snd_pcm_sframes_t delay;
669 int ret;
670 while (1) {
671 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
672 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
673 #ifdef OSS_DEBUG
674 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
675 printk("pcm_oss: read: recovering from XRUN\n");
676 else
677 printk("pcm_oss: read: recovering from SUSPEND\n");
678 #endif
679 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
680 if (ret < 0)
681 break;
682 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
683 ret = snd_pcm_oss_prepare(substream);
684 if (ret < 0)
685 break;
687 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
688 if (ret < 0)
689 break;
690 if (in_kernel) {
691 mm_segment_t fs;
692 fs = snd_enter_user();
693 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
694 snd_leave_user(fs);
695 } else {
696 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
698 if (ret == -EPIPE) {
699 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
700 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
701 if (ret < 0)
702 break;
704 continue;
706 if (ret != -ESTRPIPE)
707 break;
709 return ret;
712 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
714 struct snd_pcm_runtime *runtime = substream->runtime;
715 int ret;
716 while (1) {
717 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
718 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
719 #ifdef OSS_DEBUG
720 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
721 printk("pcm_oss: writev: recovering from XRUN\n");
722 else
723 printk("pcm_oss: writev: recovering from SUSPEND\n");
724 #endif
725 ret = snd_pcm_oss_prepare(substream);
726 if (ret < 0)
727 break;
729 if (in_kernel) {
730 mm_segment_t fs;
731 fs = snd_enter_user();
732 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
733 snd_leave_user(fs);
734 } else {
735 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
737 if (ret != -EPIPE && ret != -ESTRPIPE)
738 break;
740 /* test, if we can't store new data, because the stream */
741 /* has not been started */
742 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
743 return -EAGAIN;
745 return ret;
748 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 int ret;
752 while (1) {
753 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
754 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
755 #ifdef OSS_DEBUG
756 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
757 printk("pcm_oss: readv: recovering from XRUN\n");
758 else
759 printk("pcm_oss: readv: recovering from SUSPEND\n");
760 #endif
761 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
762 if (ret < 0)
763 break;
764 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
765 ret = snd_pcm_oss_prepare(substream);
766 if (ret < 0)
767 break;
769 if (in_kernel) {
770 mm_segment_t fs;
771 fs = snd_enter_user();
772 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
773 snd_leave_user(fs);
774 } else {
775 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
777 if (ret != -EPIPE && ret != -ESTRPIPE)
778 break;
780 return ret;
783 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
785 struct snd_pcm_runtime *runtime = substream->runtime;
786 snd_pcm_sframes_t frames, frames1;
787 if (runtime->oss.plugin_first) {
788 struct snd_pcm_plugin_channel *channels;
789 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
790 if (!in_kernel) {
791 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
792 return -EFAULT;
793 buf = runtime->oss.buffer;
795 frames = bytes / oss_frame_bytes;
796 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
797 if (frames1 < 0)
798 return frames1;
799 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
800 if (frames1 <= 0)
801 return frames1;
802 bytes = frames1 * oss_frame_bytes;
803 } else {
804 frames = bytes_to_frames(runtime, bytes);
805 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
806 if (frames1 <= 0)
807 return frames1;
808 bytes = frames_to_bytes(runtime, frames1);
810 return bytes;
813 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
815 size_t xfer = 0;
816 ssize_t tmp;
817 struct snd_pcm_runtime *runtime = substream->runtime;
819 if (atomic_read(&runtime->mmap_count))
820 return -ENXIO;
822 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
823 return tmp;
824 while (bytes > 0) {
825 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
826 tmp = bytes;
827 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
828 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
829 if (tmp > 0) {
830 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
831 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
833 runtime->oss.buffer_used += tmp;
834 buf += tmp;
835 bytes -= tmp;
836 xfer += tmp;
837 if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
838 runtime->oss.buffer_used == runtime->oss.period_bytes) {
839 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
840 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
841 if (tmp <= 0)
842 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
843 runtime->oss.bytes += tmp;
844 runtime->oss.period_ptr += tmp;
845 runtime->oss.period_ptr %= runtime->oss.period_bytes;
846 if (runtime->oss.period_ptr == 0 ||
847 runtime->oss.period_ptr == runtime->oss.buffer_used)
848 runtime->oss.buffer_used = 0;
849 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
850 return xfer > 0 ? xfer : -EAGAIN;
852 } else {
853 tmp = snd_pcm_oss_write2(substream,
854 (const char __force *)buf,
855 runtime->oss.period_bytes, 0);
856 if (tmp <= 0)
857 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
858 runtime->oss.bytes += tmp;
859 buf += tmp;
860 bytes -= tmp;
861 xfer += tmp;
862 if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
863 tmp != runtime->oss.period_bytes)
864 break;
867 return xfer;
870 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
872 struct snd_pcm_runtime *runtime = substream->runtime;
873 snd_pcm_sframes_t frames, frames1;
874 char __user *final_dst = (char __user *)buf;
875 if (runtime->oss.plugin_first) {
876 struct snd_pcm_plugin_channel *channels;
877 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
878 if (!in_kernel)
879 buf = runtime->oss.buffer;
880 frames = bytes / oss_frame_bytes;
881 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
882 if (frames1 < 0)
883 return frames1;
884 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
885 if (frames1 <= 0)
886 return frames1;
887 bytes = frames1 * oss_frame_bytes;
888 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
889 return -EFAULT;
890 } else {
891 frames = bytes_to_frames(runtime, bytes);
892 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
893 if (frames1 <= 0)
894 return frames1;
895 bytes = frames_to_bytes(runtime, frames1);
897 return bytes;
900 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
902 size_t xfer = 0;
903 ssize_t tmp;
904 struct snd_pcm_runtime *runtime = substream->runtime;
906 if (atomic_read(&runtime->mmap_count))
907 return -ENXIO;
909 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
910 return tmp;
911 while (bytes > 0) {
912 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
913 if (runtime->oss.buffer_used == 0) {
914 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
915 if (tmp <= 0)
916 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
917 runtime->oss.bytes += tmp;
918 runtime->oss.period_ptr = tmp;
919 runtime->oss.buffer_used = tmp;
921 tmp = bytes;
922 if ((size_t) tmp > runtime->oss.buffer_used)
923 tmp = runtime->oss.buffer_used;
924 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
925 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
926 buf += tmp;
927 bytes -= tmp;
928 xfer += tmp;
929 runtime->oss.buffer_used -= tmp;
930 } else {
931 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
932 runtime->oss.period_bytes, 0);
933 if (tmp <= 0)
934 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
935 runtime->oss.bytes += tmp;
936 buf += tmp;
937 bytes -= tmp;
938 xfer += tmp;
941 return xfer;
944 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
946 struct snd_pcm_substream *substream;
948 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
949 if (substream != NULL) {
950 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
951 substream->runtime->oss.prepare = 1;
953 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
954 if (substream != NULL) {
955 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
956 substream->runtime->oss.prepare = 1;
958 return 0;
961 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
963 struct snd_pcm_substream *substream;
964 int err;
966 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
967 if (substream != NULL) {
968 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
969 return err;
970 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
972 /* note: all errors from the start action are ignored */
973 /* OSS apps do not know, how to handle them */
974 return 0;
977 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
979 struct snd_pcm_runtime *runtime;
980 ssize_t result = 0;
981 long res;
982 wait_queue_t wait;
984 runtime = substream->runtime;
985 init_waitqueue_entry(&wait, current);
986 add_wait_queue(&runtime->sleep, &wait);
987 #ifdef OSS_DEBUG
988 printk("sync1: size = %li\n", size);
989 #endif
990 while (1) {
991 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
992 if (result > 0) {
993 runtime->oss.buffer_used = 0;
994 result = 0;
995 break;
997 if (result != 0 && result != -EAGAIN)
998 break;
999 result = 0;
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 snd_pcm_stream_lock_irq(substream);
1002 res = runtime->status->state;
1003 snd_pcm_stream_unlock_irq(substream);
1004 if (res != SNDRV_PCM_STATE_RUNNING) {
1005 set_current_state(TASK_RUNNING);
1006 break;
1008 res = schedule_timeout(10 * HZ);
1009 if (signal_pending(current)) {
1010 result = -ERESTARTSYS;
1011 break;
1013 if (res == 0) {
1014 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1015 result = -EIO;
1016 break;
1019 remove_wait_queue(&runtime->sleep, &wait);
1020 return result;
1023 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1025 int err = 0;
1026 unsigned int saved_f_flags;
1027 struct snd_pcm_substream *substream;
1028 struct snd_pcm_runtime *runtime;
1029 snd_pcm_format_t format;
1030 unsigned long width;
1031 size_t size;
1033 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1034 if (substream != NULL) {
1035 runtime = substream->runtime;
1036 if (atomic_read(&runtime->mmap_count))
1037 goto __direct;
1038 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1039 return err;
1040 format = snd_pcm_oss_format_from(runtime->oss.format);
1041 width = snd_pcm_format_physical_width(format);
1042 if (runtime->oss.buffer_used > 0) {
1043 #ifdef OSS_DEBUG
1044 printk("sync: buffer_used\n");
1045 #endif
1046 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1047 snd_pcm_format_set_silence(format,
1048 runtime->oss.buffer + runtime->oss.buffer_used,
1049 size);
1050 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1051 if (err < 0)
1052 return err;
1053 } else if (runtime->oss.period_ptr > 0) {
1054 #ifdef OSS_DEBUG
1055 printk("sync: period_ptr\n");
1056 #endif
1057 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1058 snd_pcm_format_set_silence(format,
1059 runtime->oss.buffer,
1060 size * 8 / width);
1061 err = snd_pcm_oss_sync1(substream, size);
1062 if (err < 0)
1063 return err;
1066 * The ALSA's period might be a bit large than OSS one.
1067 * Fill the remain portion of ALSA period with zeros.
1069 size = runtime->control->appl_ptr % runtime->period_size;
1070 if (size > 0) {
1071 size = runtime->period_size - size;
1072 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1073 size = (runtime->frame_bits * size) / 8;
1074 while (size > 0) {
1075 mm_segment_t fs;
1076 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1077 size -= size1;
1078 size1 *= 8;
1079 size1 /= runtime->sample_bits;
1080 snd_pcm_format_set_silence(runtime->format,
1081 runtime->oss.buffer,
1082 size1);
1083 fs = snd_enter_user();
1084 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1085 snd_leave_user(fs);
1087 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1088 void __user *buffers[runtime->channels];
1089 memset(buffers, 0, runtime->channels * sizeof(void *));
1090 snd_pcm_lib_writev(substream, buffers, size);
1094 * finish sync: drain the buffer
1096 __direct:
1097 saved_f_flags = substream->ffile->f_flags;
1098 substream->ffile->f_flags &= ~O_NONBLOCK;
1099 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1100 substream->ffile->f_flags = saved_f_flags;
1101 if (err < 0)
1102 return err;
1103 runtime->oss.prepare = 1;
1106 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1107 if (substream != NULL) {
1108 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1109 return err;
1110 runtime = substream->runtime;
1111 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1112 if (err < 0)
1113 return err;
1114 runtime->oss.buffer_used = 0;
1115 runtime->oss.prepare = 1;
1117 return 0;
1120 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1122 int idx;
1124 for (idx = 1; idx >= 0; --idx) {
1125 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1126 struct snd_pcm_runtime *runtime;
1127 if (substream == NULL)
1128 continue;
1129 runtime = substream->runtime;
1130 if (rate < 1000)
1131 rate = 1000;
1132 else if (rate > 192000)
1133 rate = 192000;
1134 if (runtime->oss.rate != rate) {
1135 runtime->oss.params = 1;
1136 runtime->oss.rate = rate;
1139 return snd_pcm_oss_get_rate(pcm_oss_file);
1142 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1144 struct snd_pcm_substream *substream;
1145 int err;
1147 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1148 return err;
1149 return substream->runtime->oss.rate;
1152 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1154 int idx;
1155 if (channels < 1)
1156 channels = 1;
1157 if (channels > 128)
1158 return -EINVAL;
1159 for (idx = 1; idx >= 0; --idx) {
1160 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1161 struct snd_pcm_runtime *runtime;
1162 if (substream == NULL)
1163 continue;
1164 runtime = substream->runtime;
1165 if (runtime->oss.channels != channels) {
1166 runtime->oss.params = 1;
1167 runtime->oss.channels = channels;
1170 return snd_pcm_oss_get_channels(pcm_oss_file);
1173 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1175 struct snd_pcm_substream *substream;
1176 int err;
1178 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1179 return err;
1180 return substream->runtime->oss.channels;
1183 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1185 struct snd_pcm_substream *substream;
1186 int err;
1188 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1189 return err;
1190 return substream->runtime->oss.period_bytes;
1193 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1195 struct snd_pcm_substream *substream;
1196 int err;
1197 int direct;
1198 struct snd_pcm_hw_params *params;
1199 unsigned int formats = 0;
1200 struct snd_mask format_mask;
1201 int fmt;
1203 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1204 return err;
1205 if (atomic_read(&substream->runtime->mmap_count)) {
1206 direct = 1;
1207 } else {
1208 struct snd_pcm_oss_setup *setup = substream->oss.setup;
1209 direct = (setup != NULL && setup->direct);
1211 if (!direct)
1212 return AFMT_MU_LAW | AFMT_U8 |
1213 AFMT_S16_LE | AFMT_S16_BE |
1214 AFMT_S8 | AFMT_U16_LE |
1215 AFMT_U16_BE;
1216 params = kmalloc(sizeof(*params), GFP_KERNEL);
1217 if (!params)
1218 return -ENOMEM;
1219 _snd_pcm_hw_params_any(params);
1220 err = snd_pcm_hw_refine(substream, params);
1221 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1222 kfree(params);
1223 snd_assert(err >= 0, return err);
1224 for (fmt = 0; fmt < 32; ++fmt) {
1225 if (snd_mask_test(&format_mask, fmt)) {
1226 int f = snd_pcm_oss_format_to(fmt);
1227 if (f >= 0)
1228 formats |= f;
1231 return formats;
1234 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1236 int formats, idx;
1238 if (format != AFMT_QUERY) {
1239 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1240 if (!(formats & format))
1241 format = AFMT_U8;
1242 for (idx = 1; idx >= 0; --idx) {
1243 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1244 struct snd_pcm_runtime *runtime;
1245 if (substream == NULL)
1246 continue;
1247 runtime = substream->runtime;
1248 if (runtime->oss.format != format) {
1249 runtime->oss.params = 1;
1250 runtime->oss.format = format;
1254 return snd_pcm_oss_get_format(pcm_oss_file);
1257 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1259 struct snd_pcm_substream *substream;
1260 int err;
1262 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1263 return err;
1264 return substream->runtime->oss.format;
1267 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1269 struct snd_pcm_runtime *runtime;
1271 if (substream == NULL)
1272 return 0;
1273 runtime = substream->runtime;
1274 if (subdivide == 0) {
1275 subdivide = runtime->oss.subdivision;
1276 if (subdivide == 0)
1277 subdivide = 1;
1278 return subdivide;
1280 if (runtime->oss.subdivision || runtime->oss.fragshift)
1281 return -EINVAL;
1282 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1283 subdivide != 8 && subdivide != 16)
1284 return -EINVAL;
1285 runtime->oss.subdivision = subdivide;
1286 runtime->oss.params = 1;
1287 return subdivide;
1290 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1292 int err = -EINVAL, idx;
1294 for (idx = 1; idx >= 0; --idx) {
1295 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1296 if (substream == NULL)
1297 continue;
1298 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1299 return err;
1301 return err;
1304 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1306 struct snd_pcm_runtime *runtime;
1308 if (substream == NULL)
1309 return 0;
1310 runtime = substream->runtime;
1311 if (runtime->oss.subdivision || runtime->oss.fragshift)
1312 return -EINVAL;
1313 runtime->oss.fragshift = val & 0xffff;
1314 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1315 if (runtime->oss.fragshift < 4) /* < 16 */
1316 runtime->oss.fragshift = 4;
1317 if (runtime->oss.maxfrags < 2)
1318 runtime->oss.maxfrags = 2;
1319 runtime->oss.params = 1;
1320 return 0;
1323 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1325 int err = -EINVAL, idx;
1327 for (idx = 1; idx >= 0; --idx) {
1328 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1329 if (substream == NULL)
1330 continue;
1331 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1332 return err;
1334 return err;
1337 static int snd_pcm_oss_nonblock(struct file * file)
1339 file->f_flags |= O_NONBLOCK;
1340 return 0;
1343 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1346 if (substream == NULL) {
1347 res &= ~DSP_CAP_DUPLEX;
1348 return res;
1350 #ifdef DSP_CAP_MULTI
1351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1352 if (substream->pstr->substream_count > 1)
1353 res |= DSP_CAP_MULTI;
1354 #endif
1355 /* DSP_CAP_REALTIME is set all times: */
1356 /* all ALSA drivers can return actual pointer in ring buffer */
1357 #if defined(DSP_CAP_REALTIME) && 0
1359 struct snd_pcm_runtime *runtime = substream->runtime;
1360 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1361 res &= ~DSP_CAP_REALTIME;
1363 #endif
1364 return res;
1367 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1369 int result, idx;
1371 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1372 for (idx = 0; idx < 2; idx++) {
1373 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1374 result = snd_pcm_oss_get_caps1(substream, result);
1376 result |= 0x0001; /* revision - same as SB AWE 64 */
1377 return result;
1380 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 snd_pcm_uframes_t appl_ptr;
1384 appl_ptr = hw_ptr + runtime->buffer_size;
1385 appl_ptr %= runtime->boundary;
1386 runtime->control->appl_ptr = appl_ptr;
1389 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1391 struct snd_pcm_runtime *runtime;
1392 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1393 int err, cmd;
1395 #ifdef OSS_DEBUG
1396 printk("pcm_oss: trigger = 0x%x\n", trigger);
1397 #endif
1399 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1400 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1402 if (psubstream) {
1403 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1404 return err;
1406 if (csubstream) {
1407 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1408 return err;
1410 if (psubstream) {
1411 runtime = psubstream->runtime;
1412 if (trigger & PCM_ENABLE_OUTPUT) {
1413 if (runtime->oss.trigger)
1414 goto _skip1;
1415 if (atomic_read(&psubstream->runtime->mmap_count))
1416 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1417 runtime->oss.trigger = 1;
1418 runtime->start_threshold = 1;
1419 cmd = SNDRV_PCM_IOCTL_START;
1420 } else {
1421 if (!runtime->oss.trigger)
1422 goto _skip1;
1423 runtime->oss.trigger = 0;
1424 runtime->start_threshold = runtime->boundary;
1425 cmd = SNDRV_PCM_IOCTL_DROP;
1426 runtime->oss.prepare = 1;
1428 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, NULL);
1429 if (err < 0)
1430 return err;
1432 _skip1:
1433 if (csubstream) {
1434 runtime = csubstream->runtime;
1435 if (trigger & PCM_ENABLE_INPUT) {
1436 if (runtime->oss.trigger)
1437 goto _skip2;
1438 runtime->oss.trigger = 1;
1439 runtime->start_threshold = 1;
1440 cmd = SNDRV_PCM_IOCTL_START;
1441 } else {
1442 if (!runtime->oss.trigger)
1443 goto _skip2;
1444 runtime->oss.trigger = 0;
1445 runtime->start_threshold = runtime->boundary;
1446 cmd = SNDRV_PCM_IOCTL_DROP;
1447 runtime->oss.prepare = 1;
1449 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1450 if (err < 0)
1451 return err;
1453 _skip2:
1454 return 0;
1457 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1459 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1460 int result = 0;
1462 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1463 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1464 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1465 result |= PCM_ENABLE_OUTPUT;
1466 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1467 result |= PCM_ENABLE_INPUT;
1468 return result;
1471 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1473 struct snd_pcm_substream *substream;
1474 struct snd_pcm_runtime *runtime;
1475 snd_pcm_sframes_t delay;
1476 int err;
1478 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1479 if (substream == NULL)
1480 return -EINVAL;
1481 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1482 return err;
1483 runtime = substream->runtime;
1484 if (runtime->oss.params || runtime->oss.prepare)
1485 return 0;
1486 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1487 if (err == -EPIPE)
1488 delay = 0; /* hack for broken OSS applications */
1489 else if (err < 0)
1490 return err;
1491 return snd_pcm_oss_bytes(substream, delay);
1494 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
1496 struct snd_pcm_substream *substream;
1497 struct snd_pcm_runtime *runtime;
1498 snd_pcm_sframes_t delay;
1499 int fixup;
1500 struct count_info info;
1501 int err;
1503 if (_info == NULL)
1504 return -EFAULT;
1505 substream = pcm_oss_file->streams[stream];
1506 if (substream == NULL)
1507 return -EINVAL;
1508 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1509 return err;
1510 runtime = substream->runtime;
1511 if (runtime->oss.params || runtime->oss.prepare) {
1512 memset(&info, 0, sizeof(info));
1513 if (copy_to_user(_info, &info, sizeof(info)))
1514 return -EFAULT;
1515 return 0;
1517 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1518 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1519 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1520 err = 0;
1521 delay = 0;
1522 fixup = 0;
1523 } else {
1524 fixup = runtime->oss.buffer_used;
1526 } else {
1527 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1528 fixup = -runtime->oss.buffer_used;
1530 if (err < 0)
1531 return err;
1532 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1533 if (atomic_read(&runtime->mmap_count)) {
1534 snd_pcm_sframes_t n;
1535 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1536 if (n < 0)
1537 n += runtime->boundary;
1538 info.blocks = n / runtime->period_size;
1539 runtime->oss.prev_hw_ptr_interrupt = delay;
1540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1541 snd_pcm_oss_simulate_fill(substream, delay);
1542 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1543 } else {
1544 delay = snd_pcm_oss_bytes(substream, delay);
1545 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1546 struct snd_pcm_oss_setup *setup = substream->oss.setup;
1547 if (setup && setup->buggyptr)
1548 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
1549 else
1550 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
1551 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1552 } else {
1553 delay += fixup;
1554 info.blocks = delay / runtime->oss.period_bytes;
1555 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1558 if (copy_to_user(_info, &info, sizeof(info)))
1559 return -EFAULT;
1560 return 0;
1563 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1565 struct snd_pcm_substream *substream;
1566 struct snd_pcm_runtime *runtime;
1567 snd_pcm_sframes_t avail;
1568 int fixup;
1569 struct audio_buf_info info;
1570 int err;
1572 if (_info == NULL)
1573 return -EFAULT;
1574 substream = pcm_oss_file->streams[stream];
1575 if (substream == NULL)
1576 return -EINVAL;
1577 runtime = substream->runtime;
1579 if (runtime->oss.params &&
1580 (err = snd_pcm_oss_change_params(substream)) < 0)
1581 return err;
1583 info.fragsize = runtime->oss.period_bytes;
1584 info.fragstotal = runtime->periods;
1585 if (runtime->oss.prepare) {
1586 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1587 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1588 info.fragments = runtime->oss.periods;
1589 } else {
1590 info.bytes = 0;
1591 info.fragments = 0;
1593 } else {
1594 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1595 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1596 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1597 avail = runtime->buffer_size;
1598 err = 0;
1599 fixup = 0;
1600 } else {
1601 avail = runtime->buffer_size - avail;
1602 fixup = -runtime->oss.buffer_used;
1604 } else {
1605 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1606 fixup = runtime->oss.buffer_used;
1608 if (err < 0)
1609 return err;
1610 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1611 info.fragments = info.bytes / runtime->oss.period_bytes;
1614 #ifdef OSS_DEBUG
1615 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1616 #endif
1617 if (copy_to_user(_info, &info, sizeof(info)))
1618 return -EFAULT;
1619 return 0;
1622 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1624 // it won't be probably implemented
1625 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1626 return -EINVAL;
1629 static struct snd_pcm_oss_setup *snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream, const char *task_name)
1631 const char *ptr, *ptrl;
1632 struct snd_pcm_oss_setup *setup;
1634 down(&pcm->streams[stream].oss.setup_mutex);
1635 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1636 if (!strcmp(setup->task_name, task_name)) {
1637 up(&pcm->streams[stream].oss.setup_mutex);
1638 return setup;
1641 ptr = ptrl = task_name;
1642 while (*ptr) {
1643 if (*ptr == '/')
1644 ptrl = ptr + 1;
1645 ptr++;
1647 if (ptrl == task_name) {
1648 goto __not_found;
1649 return NULL;
1651 for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1652 if (!strcmp(setup->task_name, ptrl)) {
1653 up(&pcm->streams[stream].oss.setup_mutex);
1654 return setup;
1657 __not_found:
1658 up(&pcm->streams[stream].oss.setup_mutex);
1659 return NULL;
1662 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
1663 struct snd_pcm_oss_setup *setup,
1664 int minor)
1666 struct snd_pcm_runtime *runtime;
1668 substream->oss.oss = 1;
1669 substream->oss.setup = setup;
1670 runtime = substream->runtime;
1671 runtime->oss.params = 1;
1672 runtime->oss.trigger = 1;
1673 runtime->oss.rate = 8000;
1674 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1675 case SNDRV_MINOR_OSS_PCM_8:
1676 runtime->oss.format = AFMT_U8;
1677 break;
1678 case SNDRV_MINOR_OSS_PCM_16:
1679 runtime->oss.format = AFMT_S16_LE;
1680 break;
1681 default:
1682 runtime->oss.format = AFMT_MU_LAW;
1684 runtime->oss.channels = 1;
1685 runtime->oss.fragshift = 0;
1686 runtime->oss.maxfrags = 0;
1687 runtime->oss.subdivision = 0;
1690 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
1692 struct snd_pcm_runtime *runtime;
1693 runtime = substream->runtime;
1694 vfree(runtime->oss.buffer);
1695 snd_pcm_oss_plugin_clear(substream);
1696 substream->oss.file = NULL;
1697 substream->oss.oss = 0;
1700 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
1702 int cidx;
1703 snd_assert(pcm_oss_file != NULL, return -ENXIO);
1704 for (cidx = 0; cidx < 2; ++cidx) {
1705 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
1706 struct snd_pcm_runtime *runtime;
1707 if (substream == NULL)
1708 continue;
1709 runtime = substream->runtime;
1711 snd_pcm_stream_lock_irq(substream);
1712 if (snd_pcm_running(substream))
1713 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1714 snd_pcm_stream_unlock_irq(substream);
1715 if (substream->ffile != NULL) {
1716 if (substream->ops->hw_free != NULL)
1717 substream->ops->hw_free(substream);
1718 substream->ops->close(substream);
1719 substream->ffile = NULL;
1721 snd_pcm_oss_release_substream(substream);
1722 snd_pcm_release_substream(substream);
1724 kfree(pcm_oss_file);
1725 return 0;
1728 static int snd_pcm_oss_open_file(struct file *file,
1729 struct snd_pcm *pcm,
1730 struct snd_pcm_oss_file **rpcm_oss_file,
1731 int minor,
1732 struct snd_pcm_oss_setup *psetup,
1733 struct snd_pcm_oss_setup *csetup)
1735 int err = 0;
1736 struct snd_pcm_oss_file *pcm_oss_file;
1737 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1738 unsigned int f_mode = file->f_mode;
1740 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1741 *rpcm_oss_file = NULL;
1743 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
1744 if (pcm_oss_file == NULL)
1745 return -ENOMEM;
1747 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1748 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1749 f_mode = FMODE_WRITE;
1750 if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1751 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1752 &psubstream)) < 0) {
1753 snd_pcm_oss_release_file(pcm_oss_file);
1754 return err;
1756 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1758 if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1759 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1760 &csubstream)) < 0) {
1761 if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1762 snd_pcm_oss_release_file(pcm_oss_file);
1763 return err;
1764 } else {
1765 csubstream = NULL;
1768 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1771 if (psubstream == NULL && csubstream == NULL) {
1772 snd_pcm_oss_release_file(pcm_oss_file);
1773 return -EINVAL;
1775 if (psubstream != NULL) {
1776 psubstream->oss.file = pcm_oss_file;
1777 err = snd_pcm_hw_constraints_init(psubstream);
1778 if (err < 0) {
1779 snd_printd("snd_pcm_hw_constraint_init failed\n");
1780 snd_pcm_oss_release_file(pcm_oss_file);
1781 return err;
1783 if ((err = psubstream->ops->open(psubstream)) < 0) {
1784 snd_pcm_oss_release_file(pcm_oss_file);
1785 return err;
1787 psubstream->ffile = file;
1788 err = snd_pcm_hw_constraints_complete(psubstream);
1789 if (err < 0) {
1790 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1791 snd_pcm_oss_release_file(pcm_oss_file);
1792 return err;
1794 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1796 if (csubstream != NULL) {
1797 csubstream->oss.file = pcm_oss_file;
1798 err = snd_pcm_hw_constraints_init(csubstream);
1799 if (err < 0) {
1800 snd_printd("snd_pcm_hw_constraint_init failed\n");
1801 snd_pcm_oss_release_file(pcm_oss_file);
1802 return err;
1804 if ((err = csubstream->ops->open(csubstream)) < 0) {
1805 snd_pcm_oss_release_file(pcm_oss_file);
1806 return err;
1808 csubstream->ffile = file;
1809 err = snd_pcm_hw_constraints_complete(csubstream);
1810 if (err < 0) {
1811 snd_printd("snd_pcm_hw_constraint_complete failed\n");
1812 snd_pcm_oss_release_file(pcm_oss_file);
1813 return err;
1815 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1818 file->private_data = pcm_oss_file;
1819 *rpcm_oss_file = pcm_oss_file;
1820 return 0;
1824 static int snd_task_name(struct task_struct *task, char *name, size_t size)
1826 unsigned int idx;
1828 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
1829 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
1830 name[idx] = task->comm[idx];
1831 name[idx] = '\0';
1832 return 0;
1835 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1837 int err;
1838 char task_name[32];
1839 struct snd_pcm *pcm;
1840 struct snd_pcm_oss_file *pcm_oss_file;
1841 struct snd_pcm_oss_setup *psetup = NULL, *csetup = NULL;
1842 int nonblock;
1843 wait_queue_t wait;
1845 pcm = snd_lookup_oss_minor_data(iminor(inode),
1846 SNDRV_OSS_DEVICE_TYPE_PCM);
1847 if (pcm == NULL) {
1848 err = -ENODEV;
1849 goto __error1;
1851 err = snd_card_file_add(pcm->card, file);
1852 if (err < 0)
1853 goto __error1;
1854 if (!try_module_get(pcm->card->module)) {
1855 err = -EFAULT;
1856 goto __error2;
1858 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1859 err = -EFAULT;
1860 goto __error;
1862 if (file->f_mode & FMODE_WRITE)
1863 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1864 if (file->f_mode & FMODE_READ)
1865 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1867 nonblock = !!(file->f_flags & O_NONBLOCK);
1868 if (psetup && !psetup->disable) {
1869 if (psetup->nonblock)
1870 nonblock = 1;
1871 else if (psetup->block)
1872 nonblock = 0;
1873 } else if (csetup && !csetup->disable) {
1874 if (csetup->nonblock)
1875 nonblock = 1;
1876 else if (csetup->block)
1877 nonblock = 0;
1879 if (!nonblock)
1880 nonblock = nonblock_open;
1882 init_waitqueue_entry(&wait, current);
1883 add_wait_queue(&pcm->open_wait, &wait);
1884 down(&pcm->open_mutex);
1885 while (1) {
1886 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1887 iminor(inode), psetup, csetup);
1888 if (err >= 0)
1889 break;
1890 if (err == -EAGAIN) {
1891 if (nonblock) {
1892 err = -EBUSY;
1893 break;
1895 } else
1896 break;
1897 set_current_state(TASK_INTERRUPTIBLE);
1898 up(&pcm->open_mutex);
1899 schedule();
1900 down(&pcm->open_mutex);
1901 if (signal_pending(current)) {
1902 err = -ERESTARTSYS;
1903 break;
1906 remove_wait_queue(&pcm->open_wait, &wait);
1907 up(&pcm->open_mutex);
1908 if (err < 0)
1909 goto __error;
1910 return err;
1912 __error:
1913 module_put(pcm->card->module);
1914 __error2:
1915 snd_card_file_remove(pcm->card, file);
1916 __error1:
1917 return err;
1920 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1922 struct snd_pcm *pcm;
1923 struct snd_pcm_substream *substream;
1924 struct snd_pcm_oss_file *pcm_oss_file;
1926 pcm_oss_file = file->private_data;
1927 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1928 if (substream == NULL)
1929 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1930 snd_assert(substream != NULL, return -ENXIO);
1931 pcm = substream->pcm;
1932 snd_pcm_oss_sync(pcm_oss_file);
1933 down(&pcm->open_mutex);
1934 snd_pcm_oss_release_file(pcm_oss_file);
1935 up(&pcm->open_mutex);
1936 wake_up(&pcm->open_wait);
1937 module_put(pcm->card->module);
1938 snd_card_file_remove(pcm->card, file);
1939 return 0;
1942 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1944 struct snd_pcm_oss_file *pcm_oss_file;
1945 int __user *p = (int __user *)arg;
1946 int res;
1948 pcm_oss_file = file->private_data;
1949 if (cmd == OSS_GETVERSION)
1950 return put_user(SNDRV_OSS_VERSION, p);
1951 if (cmd == OSS_ALSAEMULVER)
1952 return put_user(1, p);
1953 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1954 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
1955 struct snd_pcm_substream *substream;
1956 int idx;
1957 for (idx = 0; idx < 2; ++idx) {
1958 substream = pcm_oss_file->streams[idx];
1959 if (substream != NULL)
1960 break;
1962 snd_assert(substream != NULL, return -ENXIO);
1963 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1965 #endif
1966 if (((cmd >> 8) & 0xff) != 'P')
1967 return -EINVAL;
1968 #ifdef OSS_DEBUG
1969 printk("pcm_oss: ioctl = 0x%x\n", cmd);
1970 #endif
1971 switch (cmd) {
1972 case SNDCTL_DSP_RESET:
1973 return snd_pcm_oss_reset(pcm_oss_file);
1974 case SNDCTL_DSP_SYNC:
1975 return snd_pcm_oss_sync(pcm_oss_file);
1976 case SNDCTL_DSP_SPEED:
1977 if (get_user(res, p))
1978 return -EFAULT;
1979 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1980 return res;
1981 return put_user(res, p);
1982 case SOUND_PCM_READ_RATE:
1983 res = snd_pcm_oss_get_rate(pcm_oss_file);
1984 if (res < 0)
1985 return res;
1986 return put_user(res, p);
1987 case SNDCTL_DSP_STEREO:
1988 if (get_user(res, p))
1989 return -EFAULT;
1990 res = res > 0 ? 2 : 1;
1991 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1992 return res;
1993 return put_user(--res, p);
1994 case SNDCTL_DSP_GETBLKSIZE:
1995 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1996 if (res < 0)
1997 return res;
1998 return put_user(res, p);
1999 case SNDCTL_DSP_SETFMT:
2000 if (get_user(res, p))
2001 return -EFAULT;
2002 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2003 if (res < 0)
2004 return res;
2005 return put_user(res, p);
2006 case SOUND_PCM_READ_BITS:
2007 res = snd_pcm_oss_get_format(pcm_oss_file);
2008 if (res < 0)
2009 return res;
2010 return put_user(res, p);
2011 case SNDCTL_DSP_CHANNELS:
2012 if (get_user(res, p))
2013 return -EFAULT;
2014 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2015 if (res < 0)
2016 return res;
2017 return put_user(res, p);
2018 case SOUND_PCM_READ_CHANNELS:
2019 res = snd_pcm_oss_get_channels(pcm_oss_file);
2020 if (res < 0)
2021 return res;
2022 return put_user(res, p);
2023 case SOUND_PCM_WRITE_FILTER:
2024 case SOUND_PCM_READ_FILTER:
2025 return -EIO;
2026 case SNDCTL_DSP_POST:
2027 return snd_pcm_oss_post(pcm_oss_file);
2028 case SNDCTL_DSP_SUBDIVIDE:
2029 if (get_user(res, p))
2030 return -EFAULT;
2031 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2032 if (res < 0)
2033 return res;
2034 return put_user(res, p);
2035 case SNDCTL_DSP_SETFRAGMENT:
2036 if (get_user(res, p))
2037 return -EFAULT;
2038 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2039 case SNDCTL_DSP_GETFMTS:
2040 res = snd_pcm_oss_get_formats(pcm_oss_file);
2041 if (res < 0)
2042 return res;
2043 return put_user(res, p);
2044 case SNDCTL_DSP_GETOSPACE:
2045 case SNDCTL_DSP_GETISPACE:
2046 return snd_pcm_oss_get_space(pcm_oss_file,
2047 cmd == SNDCTL_DSP_GETISPACE ?
2048 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2049 (struct audio_buf_info __user *) arg);
2050 case SNDCTL_DSP_NONBLOCK:
2051 return snd_pcm_oss_nonblock(file);
2052 case SNDCTL_DSP_GETCAPS:
2053 res = snd_pcm_oss_get_caps(pcm_oss_file);
2054 if (res < 0)
2055 return res;
2056 return put_user(res, p);
2057 case SNDCTL_DSP_GETTRIGGER:
2058 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2059 if (res < 0)
2060 return res;
2061 return put_user(res, p);
2062 case SNDCTL_DSP_SETTRIGGER:
2063 if (get_user(res, p))
2064 return -EFAULT;
2065 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2066 case SNDCTL_DSP_GETIPTR:
2067 case SNDCTL_DSP_GETOPTR:
2068 return snd_pcm_oss_get_ptr(pcm_oss_file,
2069 cmd == SNDCTL_DSP_GETIPTR ?
2070 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2071 (struct count_info __user *) arg);
2072 case SNDCTL_DSP_MAPINBUF:
2073 case SNDCTL_DSP_MAPOUTBUF:
2074 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2075 cmd == SNDCTL_DSP_MAPINBUF ?
2076 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2077 (struct buffmem_desc __user *) arg);
2078 case SNDCTL_DSP_SETSYNCRO:
2079 /* stop DMA now.. */
2080 return 0;
2081 case SNDCTL_DSP_SETDUPLEX:
2082 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2083 return 0;
2084 return -EIO;
2085 case SNDCTL_DSP_GETODELAY:
2086 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2087 if (res < 0) {
2088 /* it's for sure, some broken apps don't check for error codes */
2089 put_user(0, p);
2090 return res;
2092 return put_user(res, p);
2093 case SNDCTL_DSP_PROFILE:
2094 return 0; /* silently ignore */
2095 default:
2096 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2098 return -EINVAL;
2101 #ifdef CONFIG_COMPAT
2102 /* all compatible */
2103 #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2104 #else
2105 #define snd_pcm_oss_ioctl_compat NULL
2106 #endif
2108 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2110 struct snd_pcm_oss_file *pcm_oss_file;
2111 struct snd_pcm_substream *substream;
2113 pcm_oss_file = file->private_data;
2114 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2115 if (substream == NULL)
2116 return -ENXIO;
2117 #ifndef OSS_DEBUG
2118 return snd_pcm_oss_read1(substream, buf, count);
2119 #else
2121 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2122 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2123 return res;
2125 #endif
2128 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2130 struct snd_pcm_oss_file *pcm_oss_file;
2131 struct snd_pcm_substream *substream;
2132 long result;
2134 pcm_oss_file = file->private_data;
2135 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2136 if (substream == NULL)
2137 return -ENXIO;
2138 result = snd_pcm_oss_write1(substream, buf, count);
2139 #ifdef OSS_DEBUG
2140 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2141 #endif
2142 return result;
2145 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2147 struct snd_pcm_runtime *runtime = substream->runtime;
2148 if (atomic_read(&runtime->mmap_count))
2149 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2150 else
2151 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2154 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2156 struct snd_pcm_runtime *runtime = substream->runtime;
2157 if (atomic_read(&runtime->mmap_count))
2158 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2159 else
2160 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2163 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2165 struct snd_pcm_oss_file *pcm_oss_file;
2166 unsigned int mask;
2167 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2169 pcm_oss_file = file->private_data;
2171 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2172 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2174 mask = 0;
2175 if (psubstream != NULL) {
2176 struct snd_pcm_runtime *runtime = psubstream->runtime;
2177 poll_wait(file, &runtime->sleep, wait);
2178 snd_pcm_stream_lock_irq(psubstream);
2179 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2180 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2181 snd_pcm_oss_playback_ready(psubstream)))
2182 mask |= POLLOUT | POLLWRNORM;
2183 snd_pcm_stream_unlock_irq(psubstream);
2185 if (csubstream != NULL) {
2186 struct snd_pcm_runtime *runtime = csubstream->runtime;
2187 snd_pcm_state_t ostate;
2188 poll_wait(file, &runtime->sleep, wait);
2189 snd_pcm_stream_lock_irq(csubstream);
2190 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2191 snd_pcm_oss_capture_ready(csubstream))
2192 mask |= POLLIN | POLLRDNORM;
2193 snd_pcm_stream_unlock_irq(csubstream);
2194 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2195 struct snd_pcm_oss_file ofile;
2196 memset(&ofile, 0, sizeof(ofile));
2197 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2198 runtime->oss.trigger = 0;
2199 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2203 return mask;
2206 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2208 struct snd_pcm_oss_file *pcm_oss_file;
2209 struct snd_pcm_substream *substream = NULL;
2210 struct snd_pcm_runtime *runtime;
2211 int err;
2213 #ifdef OSS_DEBUG
2214 printk("pcm_oss: mmap begin\n");
2215 #endif
2216 pcm_oss_file = file->private_data;
2217 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2218 case VM_READ | VM_WRITE:
2219 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2220 if (substream)
2221 break;
2222 /* Fall through */
2223 case VM_READ:
2224 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2225 break;
2226 case VM_WRITE:
2227 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2228 break;
2229 default:
2230 return -EINVAL;
2232 /* set VM_READ access as well to fix memset() routines that do
2233 reads before writes (to improve performance) */
2234 area->vm_flags |= VM_READ;
2235 if (substream == NULL)
2236 return -ENXIO;
2237 runtime = substream->runtime;
2238 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2239 return -EIO;
2240 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2241 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2242 else
2243 return -EIO;
2245 if (runtime->oss.params) {
2246 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2247 return err;
2249 if (runtime->oss.plugin_first != NULL)
2250 return -EIO;
2252 if (area->vm_pgoff != 0)
2253 return -EINVAL;
2255 err = snd_pcm_mmap_data(substream, file, area);
2256 if (err < 0)
2257 return err;
2258 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2259 runtime->silence_threshold = 0;
2260 runtime->silence_size = 0;
2261 #ifdef OSS_DEBUG
2262 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2263 #endif
2264 /* In mmap mode we never stop */
2265 runtime->stop_threshold = runtime->boundary;
2267 return 0;
2270 #ifdef CONFIG_PROC_FS
2272 * /proc interface
2275 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2276 struct snd_info_buffer *buffer)
2278 struct snd_pcm_str *pstr = entry->private_data;
2279 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2280 down(&pstr->oss.setup_mutex);
2281 while (setup) {
2282 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2283 setup->task_name,
2284 setup->periods,
2285 setup->period_size,
2286 setup->disable ? " disable" : "",
2287 setup->direct ? " direct" : "",
2288 setup->block ? " block" : "",
2289 setup->nonblock ? " non-block" : "",
2290 setup->partialfrag ? " partial-frag" : "",
2291 setup->nosilence ? " no-silence" : "");
2292 setup = setup->next;
2294 up(&pstr->oss.setup_mutex);
2297 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2299 unsigned int idx;
2300 struct snd_pcm_substream *substream;
2301 struct snd_pcm_oss_setup *setup, *setupn;
2303 for (idx = 0, substream = pstr->substream;
2304 idx < pstr->substream_count; idx++, substream = substream->next)
2305 substream->oss.setup = NULL;
2306 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2307 setup; setup = setupn) {
2308 setupn = setup->next;
2309 kfree(setup->task_name);
2310 kfree(setup);
2312 pstr->oss.setup_list = NULL;
2315 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2316 struct snd_info_buffer *buffer)
2318 struct snd_pcm_str *pstr = entry->private_data;
2319 char line[128], str[32], task_name[32], *ptr;
2320 int idx1;
2321 struct snd_pcm_oss_setup *setup, *setup1, template;
2323 while (!snd_info_get_line(buffer, line, sizeof(line))) {
2324 down(&pstr->oss.setup_mutex);
2325 memset(&template, 0, sizeof(template));
2326 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2327 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2328 snd_pcm_oss_proc_free_setup_list(pstr);
2329 up(&pstr->oss.setup_mutex);
2330 continue;
2332 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2333 if (!strcmp(setup->task_name, task_name)) {
2334 template = *setup;
2335 break;
2338 ptr = snd_info_get_str(str, ptr, sizeof(str));
2339 template.periods = simple_strtoul(str, NULL, 10);
2340 ptr = snd_info_get_str(str, ptr, sizeof(str));
2341 template.period_size = simple_strtoul(str, NULL, 10);
2342 for (idx1 = 31; idx1 >= 0; idx1--)
2343 if (template.period_size & (1 << idx1))
2344 break;
2345 for (idx1--; idx1 >= 0; idx1--)
2346 template.period_size &= ~(1 << idx1);
2347 do {
2348 ptr = snd_info_get_str(str, ptr, sizeof(str));
2349 if (!strcmp(str, "disable")) {
2350 template.disable = 1;
2351 } else if (!strcmp(str, "direct")) {
2352 template.direct = 1;
2353 } else if (!strcmp(str, "block")) {
2354 template.block = 1;
2355 } else if (!strcmp(str, "non-block")) {
2356 template.nonblock = 1;
2357 } else if (!strcmp(str, "partial-frag")) {
2358 template.partialfrag = 1;
2359 } else if (!strcmp(str, "no-silence")) {
2360 template.nosilence = 1;
2361 } else if (!strcmp(str, "buggy-ptr")) {
2362 template.buggyptr = 1;
2364 } while (*str);
2365 if (setup == NULL) {
2366 setup = kmalloc(sizeof(struct snd_pcm_oss_setup), GFP_KERNEL);
2367 if (setup) {
2368 if (pstr->oss.setup_list == NULL) {
2369 pstr->oss.setup_list = setup;
2370 } else {
2371 for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2372 setup1->next = setup;
2374 template.task_name = kstrdup(task_name, GFP_KERNEL);
2375 } else {
2376 buffer->error = -ENOMEM;
2379 if (setup)
2380 *setup = template;
2381 up(&pstr->oss.setup_mutex);
2385 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2387 int stream;
2388 for (stream = 0; stream < 2; ++stream) {
2389 struct snd_info_entry *entry;
2390 struct snd_pcm_str *pstr = &pcm->streams[stream];
2391 if (pstr->substream_count == 0)
2392 continue;
2393 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2394 entry->content = SNDRV_INFO_CONTENT_TEXT;
2395 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2396 entry->c.text.read_size = 8192;
2397 entry->c.text.read = snd_pcm_oss_proc_read;
2398 entry->c.text.write_size = 8192;
2399 entry->c.text.write = snd_pcm_oss_proc_write;
2400 entry->private_data = pstr;
2401 if (snd_info_register(entry) < 0) {
2402 snd_info_free_entry(entry);
2403 entry = NULL;
2406 pstr->oss.proc_entry = entry;
2410 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2412 int stream;
2413 for (stream = 0; stream < 2; ++stream) {
2414 struct snd_pcm_str *pstr = &pcm->streams[stream];
2415 if (pstr->oss.proc_entry) {
2416 snd_info_unregister(pstr->oss.proc_entry);
2417 pstr->oss.proc_entry = NULL;
2418 snd_pcm_oss_proc_free_setup_list(pstr);
2422 #else /* !CONFIG_PROC_FS */
2423 #define snd_pcm_oss_proc_init(pcm)
2424 #define snd_pcm_oss_proc_done(pcm)
2425 #endif /* CONFIG_PROC_FS */
2428 * ENTRY functions
2431 static struct file_operations snd_pcm_oss_f_reg =
2433 .owner = THIS_MODULE,
2434 .read = snd_pcm_oss_read,
2435 .write = snd_pcm_oss_write,
2436 .open = snd_pcm_oss_open,
2437 .release = snd_pcm_oss_release,
2438 .poll = snd_pcm_oss_poll,
2439 .unlocked_ioctl = snd_pcm_oss_ioctl,
2440 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2441 .mmap = snd_pcm_oss_mmap,
2444 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2446 char name[128];
2447 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2448 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2449 pcm->card, index, &snd_pcm_oss_f_reg,
2450 pcm, name) < 0) {
2451 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2452 pcm->card->number, pcm->device);
2456 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2458 pcm->oss.reg = 0;
2459 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2460 char name[128];
2461 int duplex;
2462 register_oss_dsp(pcm, 0);
2463 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2464 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2465 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2466 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2467 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2468 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2469 pcm->card->number,
2470 name);
2471 #endif
2472 pcm->oss.reg++;
2473 pcm->oss.reg_mask |= 1;
2475 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2476 register_oss_dsp(pcm, 1);
2477 pcm->oss.reg++;
2478 pcm->oss.reg_mask |= 2;
2481 if (pcm->oss.reg)
2482 snd_pcm_oss_proc_init(pcm);
2484 return 0;
2487 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2489 if (pcm->oss.reg) {
2490 if (pcm->oss.reg_mask & 1) {
2491 pcm->oss.reg_mask &= ~1;
2492 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2493 pcm->card, 0);
2495 if (pcm->oss.reg_mask & 2) {
2496 pcm->oss.reg_mask &= ~2;
2497 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2498 pcm->card, 1);
2501 return 0;
2504 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2506 snd_pcm_oss_disconnect_minor(pcm);
2507 if (pcm->oss.reg) {
2508 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2509 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2510 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2511 #endif
2513 pcm->oss.reg = 0;
2514 snd_pcm_oss_proc_done(pcm);
2516 return 0;
2519 static struct snd_pcm_notify snd_pcm_oss_notify =
2521 .n_register = snd_pcm_oss_register_minor,
2522 .n_disconnect = snd_pcm_oss_disconnect_minor,
2523 .n_unregister = snd_pcm_oss_unregister_minor,
2526 static int __init alsa_pcm_oss_init(void)
2528 int i;
2529 int err;
2531 /* check device map table */
2532 for (i = 0; i < SNDRV_CARDS; i++) {
2533 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2534 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2535 i, dsp_map[i]);
2536 dsp_map[i] = 0;
2538 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2539 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
2540 i, adsp_map[i]);
2541 adsp_map[i] = 1;
2544 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2545 return err;
2546 return 0;
2549 static void __exit alsa_pcm_oss_exit(void)
2551 snd_pcm_notify(&snd_pcm_oss_notify, 1);
2554 module_init(alsa_pcm_oss_init)
2555 module_exit(alsa_pcm_oss_exit)