ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / oss / pcm_oss.c
1 /*
2  *  Digital Audio (PCM) abstract layer / OSS compatible
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #if 0
23 #define PLUGIN_DEBUG
24 #endif
25 #if 0
26 #define OSS_DEBUG
27 #endif
28
29 #include <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/vmalloc.h>
34 #include <sound/core.h>
35 #include <sound/minors.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include "pcm_plugin.h"
39 #include <sound/info.h>
40 #include <linux/soundcard.h>
41 #include <sound/initval.h>
42
43 #define OSS_ALSAEMULVER         _SIOR ('M', 249, int)
44
45 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
46 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47 static int nonblock_open;
48
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
50 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
51 MODULE_LICENSE("GPL");
52 MODULE_PARM(dsp_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
53 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
54 MODULE_PARM_SYNTAX(dsp_map, "default:0,skill:advanced");
55 MODULE_PARM(adsp_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
56 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
57 MODULE_PARM_SYNTAX(adsp_map, "default:1,skill:advanced");
58 MODULE_PARM(nonblock_open, "i");
59 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60 MODULE_PARM_SYNTAX(nonblock_open, "default:0,skill:advanced");
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63
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);
68
69 static inline mm_segment_t snd_enter_user(void)
70 {
71         mm_segment_t fs = get_fs();
72         set_fs(get_ds());
73         return fs;
74 }
75
76 static inline void snd_leave_user(mm_segment_t fs)
77 {
78         set_fs(fs);
79 }
80
81 int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
82 {
83         snd_pcm_runtime_t *runtime = substream->runtime;
84         snd_pcm_plugin_t *plugin, *next;
85         
86         plugin = runtime->oss.plugin_first;
87         while (plugin) {
88                 next = plugin->next;
89                 snd_pcm_plugin_free(plugin);
90                 plugin = next;
91         }
92         runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
93         return 0;
94 }
95
96 int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
97 {
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;
107         }
108         return 0;
109 }
110
111 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
112 {
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;
122         }
123         return 0;
124 }
125
126 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
127 {
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;
134 }
135
136 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
137 {
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);
143 }
144
145 static int snd_pcm_oss_format_from(int format)
146 {
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;
159         }
160 }
161
162 static int snd_pcm_oss_format_to(int format)
163 {
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;
176         }
177 }
178
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)
182 {
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;
188
189         oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
190                          params_channels(oss_params) / 8;
191
192         oss_buffer_size = snd_pcm_plug_client_size(substream,
193                                                    snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0)) * oss_frame_size;
194         oss_buffer_size = 1 << ld2(oss_buffer_size);
195         if (atomic_read(&runtime->mmap_count)) {
196                 if (oss_buffer_size > runtime->oss.mmap_bytes)
197                         oss_buffer_size = runtime->oss.mmap_bytes;
198         }
199
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;
210
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;
226         }
227
228         min_period_size = snd_pcm_plug_client_size(substream,
229                                                    snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
230         min_period_size *= oss_frame_size;
231         min_period_size = 1 << (ld2(min_period_size - 1) + 1);
232         if (oss_period_size < min_period_size)
233                 oss_period_size = min_period_size;
234
235         max_period_size = snd_pcm_plug_client_size(substream,
236                                                    snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 0));
237         max_period_size *= oss_frame_size;
238         max_period_size = 1 << ld2(max_period_size);
239         if (oss_period_size > max_period_size)
240                 oss_period_size = max_period_size;
241
242         oss_periods = oss_buffer_size / oss_period_size;
243
244         if (substream->oss.setup) {
245                 if (substream->oss.setup->periods > 1)
246                         oss_periods = substream->oss.setup->periods;
247         }
248
249         s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
250         if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
251                 s = runtime->oss.maxfrags;
252         if (oss_periods > s)
253                 oss_periods = s;
254
255         s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, 0);
256         if (s < 2)
257                 s = 2;
258         if (oss_periods < s)
259                 oss_periods = s;
260
261         while (oss_period_size * oss_periods > oss_buffer_size)
262                 oss_period_size /= 2;
263
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;
269 }
270
271 static int choose_rate(snd_pcm_substream_t *substream,
272                        snd_pcm_hw_params_t *params, unsigned int best_rate)
273 {
274         snd_interval_t *it;
275         snd_pcm_hw_params_t *save;
276         unsigned int rate, prev;
277
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);
283
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;
297                         }
298                         *params = *save;
299                 }
300                 prev = rate;
301                 rate += best_rate;
302                 if (rate <= prev)
303                         break;
304         }
305
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, 0);
309 }
310
311 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
312 {
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;
323
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;
331         }
332
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);
338         }
339
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);
350         }
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;
356         }
357         choose_rate(substream, sparams, runtime->oss.rate);
358         snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, 0);
359
360         format = snd_pcm_oss_format_from(runtime->oss.format);
361
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);
367
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;
373                 }
374                 if (sformat > SNDRV_PCM_FORMAT_LAST) {
375                         snd_printd("Cannot find a format!!!\n");
376                         err = -EINVAL;
377                         goto failure;
378                 }
379         }
380         err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
381         snd_assert(err >= 0, goto failure);
382
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));
398         }
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));
402
403         oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
404                          params_channels(params) / 8;
405
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;
416                 }
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;
423                         }
424                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
425                                 err = snd_pcm_plugin_append(plugin);
426                         } else {
427                                 err = snd_pcm_plugin_insert(plugin);
428                         }
429                         if (err < 0) {
430                                 snd_pcm_oss_plugin_clear(substream);
431                                 goto failure;
432                         }
433                 }
434         }
435
436         err = snd_pcm_oss_period_size(substream, params, sparams);
437         if (err < 0)
438                 goto failure;
439
440         n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
441         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, 0);
442         snd_assert(err >= 0, goto failure);
443
444         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
445                                      runtime->oss.periods, 0);
446         snd_assert(err >= 0, goto failure);
447
448         snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
449
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;
453         }
454
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;
460         }
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;
481         }
482
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;
486         }
487
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;
495         }
496         oss_period_size *= oss_frame_size;
497
498         oss_buffer_size = oss_period_size * runtime->oss.periods;
499         snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
500
501         runtime->oss.period_bytes = oss_period_size;
502         runtime->oss.buffer_bytes = oss_buffer_size;
503
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));
510
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);
514
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));
523
524         runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
525
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;
535 }
536
537 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
538 {
539         int idx, err;
540         snd_pcm_substream_t *asubstream = NULL, *substream;
541
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;
552                 }
553         }
554         snd_assert(asubstream != NULL, return -EIO);
555         if (r_substream)
556                 *r_substream = asubstream;
557         return 0;
558 }
559
560 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
561 {
562         int err;
563         snd_pcm_runtime_t *runtime = substream->runtime;
564
565         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, 0);
566         if (err < 0) {
567                 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
568                 return err;
569         }
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;
574
575         return 0;
576 }
577
578 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
579 {
580         snd_pcm_runtime_t *runtime;
581         int err;
582
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;
590         }
591         if (runtime->oss.prepare) {
592                 err = snd_pcm_oss_prepare(substream);
593                 if (err < 0)
594                         return err;
595         }
596         return 0;
597 }
598
599 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
600 {
601         snd_pcm_runtime_t *runtime;
602         snd_pcm_uframes_t frames;
603         int err = 0;
604
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;
620         }
621         return err;
622 }
623
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)
625 {
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;
640                 }
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);
648                 }
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;
655         }
656         return ret;
657 }
658
659 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
660 {
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, 0);
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;
680                 }
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);
691                 }
692                 if (ret == -EPIPE) {
693                         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
694                                 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
695                                 if (ret < 0)
696                                         break;
697                         }
698                         continue;
699                 }
700                 if (ret != -ESTRPIPE)
701                         break;
702         }
703         return ret;
704 }
705
706 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
707 {
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;
722                 }
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);
730                 }
731                 if (ret != -EPIPE && ret != -ESTRPIPE)
732                         break;
733
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;
738         }
739         return ret;
740 }
741         
742 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
743 {
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, 0);
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;
762                 }
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);
770                 }
771                 if (ret != -EPIPE && ret != -ESTRPIPE)
772                         break;
773         }
774         return ret;
775 }
776
777 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
778 {
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;
788                 }
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);
803         }
804         return bytes;
805 }
806
807 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char *buf, size_t bytes)
808 {
809         size_t xfer = 0;
810         ssize_t tmp;
811         snd_pcm_runtime_t *runtime = substream->runtime;
812
813         if (atomic_read(&runtime->mmap_count))
814                 return -ENXIO;
815
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;
826                         }
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;
840                         }
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;
849                 }
850         }
851         return xfer;
852 }
853
854 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
855 {
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);
880         }
881         return bytes;
882 }
883
884 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char *buf, size_t bytes)
885 {
886         size_t xfer = 0;
887         ssize_t tmp;
888         snd_pcm_runtime_t *runtime = substream->runtime;
889
890         if (atomic_read(&runtime->mmap_count))
891                 return -ENXIO;
892
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;
904                         }
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;
922                 }
923         }
924         return xfer;
925 }
926
927 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
928 {
929         snd_pcm_substream_t *substream;
930
931         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
932         if (substream != NULL) {
933                 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
934                 substream->runtime->oss.prepare = 1;
935         }
936         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
937         if (substream != NULL) {
938                 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, 0);
939                 substream->runtime->oss.prepare = 1;
940         }
941         return 0;
942 }
943
944 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
945 {
946         snd_pcm_substream_t *substream;
947         int err;
948
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, 0);
954         }
955         /* note: all errors from the start action are ignored */
956         /* OSS apps do not know, how to handle them */
957         return 0;
958 }
959
960 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
961 {
962         snd_pcm_runtime_t *runtime;
963         ssize_t result = 0;
964         long res;
965         wait_queue_t wait;
966
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;
979                 }
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;
990                 }
991                 res = schedule_timeout(10 * HZ);
992                 if (signal_pending(current)) {
993                         result = -ERESTARTSYS;
994                         break;
995                 }
996                 if (res == 0) {
997                         snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
998                         result = -EIO;
999                         break;
1000                 }
1001         }
1002         remove_wait_queue(&runtime->sleep, &wait);
1003         return result;
1004 }
1005
1006 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1007 {
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;
1015
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;
1047                 }
1048                 /*
1049                  * The ALSA's period might be a bit large than OSS one.
1050                  * Fill the remain portion of ALSA period with zeros.
1051                  */
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);
1066                                 }
1067                         } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1068                                 void *buffers[runtime->channels];
1069                                 memset(buffers, 0, runtime->channels * sizeof(void *));
1070                                 snd_pcm_lib_writev(substream, (void **)buffers, size);
1071                         }
1072                 }
1073                 /*
1074                  * finish sync: drain the buffer
1075                  */
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, 0);
1080                 substream->ffile->f_flags = saved_f_flags;
1081                 if (err < 0)
1082                         return err;
1083                 runtime->oss.prepare = 1;
1084         }
1085
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, 0);
1092                 if (err < 0)
1093                         return err;
1094                 runtime->oss.buffer_used = 0;
1095                 runtime->oss.prepare = 1;
1096         }
1097         return 0;
1098 }
1099
1100 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1101 {
1102         int idx;
1103
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;
1117                 }
1118         }
1119         return snd_pcm_oss_get_rate(pcm_oss_file);
1120 }
1121
1122 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1123 {
1124         snd_pcm_substream_t *substream;
1125         int err;
1126         
1127         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1128                 return err;
1129         return substream->runtime->oss.rate;
1130 }
1131
1132 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1133 {
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;
1148                 }
1149         }
1150         return snd_pcm_oss_get_channels(pcm_oss_file);
1151 }
1152
1153 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1154 {
1155         snd_pcm_substream_t *substream;
1156         int err;
1157         
1158         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1159                 return err;
1160         return substream->runtime->oss.channels;
1161 }
1162
1163 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1164 {
1165         snd_pcm_substream_t *substream;
1166         int err;
1167         
1168         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1169                 return err;
1170         return substream->runtime->oss.period_bytes;
1171 }
1172
1173 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1174 {
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;
1182         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1183                 return err;
1184         if (atomic_read(&substream->runtime->mmap_count)) {
1185                 direct = 1;
1186         } else {
1187                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1188                 direct = (setup != NULL && setup->direct);
1189         }
1190         if (!direct)
1191                 return AFMT_MU_LAW | AFMT_U8 |
1192                        AFMT_S16_LE | AFMT_S16_BE |
1193                        AFMT_S8 | AFMT_U16_LE |
1194                        AFMT_U16_BE;
1195         _snd_pcm_hw_params_any(&params);
1196         err = snd_pcm_hw_refine(substream, &params);
1197         snd_assert(err >= 0, return err);
1198         format_mask = *hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT); 
1199         for (fmt = 0; fmt < 32; ++fmt) {
1200                 if (snd_mask_test(&format_mask, fmt)) {
1201                         int f = snd_pcm_oss_format_to(fmt);
1202                         if (f >= 0)
1203                                 formats |= f;
1204                 }
1205         }
1206         return formats;
1207 }
1208
1209 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1210 {
1211         int formats, idx;
1212         
1213         if (format != AFMT_QUERY) {
1214                 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1215                 if (!(formats & format))
1216                         format = AFMT_U8;
1217                 for (idx = 1; idx >= 0; --idx) {
1218                         snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1219                         snd_pcm_runtime_t *runtime;
1220                         if (substream == NULL)
1221                                 continue;
1222                         runtime = substream->runtime;
1223                         if (runtime->oss.format != format) {
1224                                 runtime->oss.params = 1;
1225                                 runtime->oss.format = format;
1226                         }
1227                 }
1228         }
1229         return snd_pcm_oss_get_format(pcm_oss_file);
1230 }
1231
1232 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1233 {
1234         snd_pcm_substream_t *substream;
1235         int err;
1236         
1237         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1238                 return err;
1239         return substream->runtime->oss.format;
1240 }
1241
1242 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1243 {
1244         snd_pcm_runtime_t *runtime;
1245
1246         if (substream == NULL)
1247                 return 0;
1248         runtime = substream->runtime;
1249         if (subdivide == 0) {
1250                 subdivide = runtime->oss.subdivision;
1251                 if (subdivide == 0)
1252                         subdivide = 1;
1253                 return subdivide;
1254         }
1255         if (runtime->oss.subdivision || runtime->oss.fragshift)
1256                 return -EINVAL;
1257         if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1258             subdivide != 8 && subdivide != 16)
1259                 return -EINVAL;
1260         runtime->oss.subdivision = subdivide;
1261         runtime->oss.params = 1;
1262         return subdivide;
1263 }
1264
1265 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1266 {
1267         int err = -EINVAL, idx;
1268
1269         for (idx = 1; idx >= 0; --idx) {
1270                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1271                 if (substream == NULL)
1272                         continue;
1273                 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1274                         return err;
1275         }
1276         return err;
1277 }
1278
1279 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1280 {
1281         snd_pcm_runtime_t *runtime;
1282
1283         if (substream == NULL)
1284                 return 0;
1285         runtime = substream->runtime;
1286         if (runtime->oss.subdivision || runtime->oss.fragshift)
1287                 return -EINVAL;
1288         runtime->oss.fragshift = val & 0xffff;
1289         runtime->oss.maxfrags = (val >> 16) & 0xffff;
1290         if (runtime->oss.fragshift < 4)         /* < 16 */
1291                 runtime->oss.fragshift = 4;
1292         if (runtime->oss.maxfrags < 2)
1293                 runtime->oss.maxfrags = 2;
1294         runtime->oss.params = 1;
1295         return 0;
1296 }
1297
1298 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1299 {
1300         int err = -EINVAL, idx;
1301
1302         for (idx = 1; idx >= 0; --idx) {
1303                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1304                 if (substream == NULL)
1305                         continue;
1306                 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1307                         return err;
1308         }
1309         return err;
1310 }
1311
1312 static int snd_pcm_oss_nonblock(struct file * file)
1313 {
1314         file->f_flags |= O_NONBLOCK;
1315         return 0;
1316 }
1317
1318 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1319 {
1320
1321         if (substream == NULL) {
1322                 res &= ~DSP_CAP_DUPLEX;
1323                 return res;
1324         }
1325 #ifdef DSP_CAP_MULTI
1326         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1327                 if (substream->pstr->substream_count > 1)
1328                         res |= DSP_CAP_MULTI;
1329 #endif
1330         /* DSP_CAP_REALTIME is set all times: */
1331         /* all ALSA drivers can return actual pointer in ring buffer */
1332 #if defined(DSP_CAP_REALTIME) && 0
1333         {
1334                 snd_pcm_runtime_t *runtime = substream->runtime;
1335                 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1336                         res &= ~DSP_CAP_REALTIME;
1337         }
1338 #endif
1339         return res;
1340 }
1341
1342 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1343 {
1344         int result, idx;
1345         
1346         result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1347         for (idx = 0; idx < 2; idx++) {
1348                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1349                 result = snd_pcm_oss_get_caps1(substream, result);
1350         }
1351         result |= 0x0001;       /* revision - same as SB AWE 64 */
1352         return result;
1353 }
1354
1355 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1356 {
1357         snd_pcm_runtime_t *runtime = substream->runtime;
1358         snd_pcm_uframes_t appl_ptr;
1359         appl_ptr = hw_ptr + runtime->buffer_size;
1360         appl_ptr %= runtime->boundary;
1361         runtime->control->appl_ptr = appl_ptr;
1362 }
1363
1364 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1365 {
1366         snd_pcm_runtime_t *runtime;
1367         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1368         int err, cmd;
1369
1370 #ifdef OSS_DEBUG
1371         printk("pcm_oss: trigger = 0x%x\n", trigger);
1372 #endif
1373         
1374         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1375         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1376
1377         if (psubstream) {
1378                 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1379                         return err;
1380         }
1381         if (csubstream) {
1382                 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1383                         return err;
1384         }
1385         if (psubstream) {
1386                 runtime = psubstream->runtime;
1387                 if (trigger & PCM_ENABLE_OUTPUT) {
1388                         if (runtime->oss.trigger)
1389                                 goto _skip1;
1390                         if (atomic_read(&psubstream->runtime->mmap_count))
1391                                 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1392                         runtime->oss.trigger = 1;
1393                         runtime->start_threshold = 1;
1394                         cmd = SNDRV_PCM_IOCTL_START;
1395                 } else {
1396                         if (!runtime->oss.trigger)
1397                                 goto _skip1;
1398                         runtime->oss.trigger = 0;
1399                         runtime->start_threshold = runtime->boundary;
1400                         cmd = SNDRV_PCM_IOCTL_DROP;
1401                         runtime->oss.prepare = 1;
1402                 }
1403                 err = snd_pcm_kernel_playback_ioctl(psubstream, cmd, 0);
1404                 if (err < 0)
1405                         return err;
1406         }
1407  _skip1:
1408         if (csubstream) {
1409                 runtime = csubstream->runtime;
1410                 if (trigger & PCM_ENABLE_INPUT) {
1411                         if (runtime->oss.trigger)
1412                                 goto _skip2;
1413                         runtime->oss.trigger = 1;
1414                         runtime->start_threshold = 1;
1415                         cmd = SNDRV_PCM_IOCTL_START;
1416                 } else {
1417                         if (!runtime->oss.trigger)
1418                                 goto _skip2;
1419                         runtime->oss.trigger = 0;
1420                         runtime->start_threshold = runtime->boundary;
1421                         cmd = SNDRV_PCM_IOCTL_DROP;
1422                         runtime->oss.prepare = 1;
1423                 }
1424                 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, 0);
1425                 if (err < 0)
1426                         return err;
1427         }
1428  _skip2:
1429         return 0;
1430 }
1431
1432 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1433 {
1434         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1435         int result = 0;
1436
1437         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1438         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1439         if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1440                 result |= PCM_ENABLE_OUTPUT;
1441         if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1442                 result |= PCM_ENABLE_INPUT;
1443         return result;
1444 }
1445
1446 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1447 {
1448         snd_pcm_substream_t *substream;
1449         snd_pcm_runtime_t *runtime;
1450         snd_pcm_sframes_t delay;
1451         int err;
1452
1453         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1454         if (substream == NULL)
1455                 return -EINVAL;
1456         if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1457                 return err;
1458         runtime = substream->runtime;
1459         if (runtime->oss.params || runtime->oss.prepare)
1460                 return 0;
1461         err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1462         if (err == -EPIPE)
1463                 delay = 0;      /* hack for broken OSS applications */
1464         else if (err < 0)
1465                 return err;
1466         return snd_pcm_oss_bytes(substream, delay);
1467 }
1468
1469 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info * _info)
1470 {       
1471         snd_pcm_substream_t *substream;
1472         snd_pcm_runtime_t *runtime;
1473         snd_pcm_sframes_t delay;
1474         int fixup;
1475         struct count_info info;
1476         int err;
1477
1478         if (_info == NULL)
1479                 return -EFAULT;
1480         substream = pcm_oss_file->streams[stream];
1481         if (substream == NULL)
1482                 return -EINVAL;
1483         if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1484                 return err;
1485         runtime = substream->runtime;
1486         if (runtime->oss.params || runtime->oss.prepare) {
1487                 memset(&info, 0, sizeof(info));
1488                 if (copy_to_user(_info, &info, sizeof(info)))
1489                         return -EFAULT;
1490                 return 0;
1491         }
1492         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1493                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1494                 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1495                         err = 0;
1496                         delay = 0;
1497                         fixup = 0;
1498                 } else {
1499                         fixup = runtime->oss.buffer_used;
1500                 }
1501         } else {
1502                 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1503                 fixup = -runtime->oss.buffer_used;
1504         }
1505         if (err < 0)
1506                 return err;
1507         info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1508         if (atomic_read(&runtime->mmap_count)) {
1509                 snd_pcm_sframes_t n;
1510                 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1511                 if (n < 0)
1512                         n += runtime->boundary;
1513                 info.blocks = n / runtime->period_size;
1514                 runtime->oss.prev_hw_ptr_interrupt = delay;
1515                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1516                         snd_pcm_oss_simulate_fill(substream, delay);
1517                 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr);
1518         } else {
1519                 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1520                 info.blocks = delay / runtime->oss.period_bytes;
1521                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1522                         info.bytes = runtime->oss.bytes - delay;
1523                 else
1524                         info.bytes = runtime->oss.bytes + delay;
1525         }
1526         if (copy_to_user(_info, &info, sizeof(info)))
1527                 return -EFAULT;
1528         return 0;
1529 }
1530
1531 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info *_info)
1532 {
1533         snd_pcm_substream_t *substream;
1534         snd_pcm_runtime_t *runtime;
1535         snd_pcm_sframes_t avail;
1536         int fixup;
1537         struct audio_buf_info info;
1538         int err;
1539
1540         if (_info == NULL)
1541                 return -EFAULT;
1542         substream = pcm_oss_file->streams[stream];
1543         if (substream == NULL)
1544                 return -EINVAL;
1545         runtime = substream->runtime;
1546
1547         if (runtime->oss.params &&
1548             (err = snd_pcm_oss_change_params(substream)) < 0)
1549                 return err;
1550
1551         info.fragsize = runtime->oss.period_bytes;
1552         info.fragstotal = runtime->periods;
1553         if (runtime->oss.prepare) {
1554                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1555                         info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1556                         info.fragments = runtime->oss.periods;
1557                 } else {
1558                         info.bytes = 0;
1559                         info.fragments = 0;
1560                 }
1561         } else {
1562                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1563                         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1564                         if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1565                                 avail = runtime->buffer_size;
1566                                 err = 0;
1567                                 fixup = 0;
1568                         } else {
1569                                 avail = runtime->buffer_size - avail;
1570                                 fixup = -runtime->oss.buffer_used;
1571                         }
1572                 } else {
1573                         err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1574                         fixup = runtime->oss.buffer_used;
1575                 }
1576                 if (err < 0)
1577                         return err;
1578                 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1579                 info.fragments = info.bytes / runtime->oss.period_bytes;
1580         }
1581
1582 #ifdef OSS_DEBUG
1583         printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1584 #endif
1585         if (copy_to_user(_info, &info, sizeof(info)))
1586                 return -EFAULT;
1587         return 0;
1588 }
1589
1590 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc * _info)
1591 {
1592         // it won't be probably implemented
1593         // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1594         return -EINVAL;
1595 }
1596
1597 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1598 {
1599         const char *ptr, *ptrl;
1600         snd_pcm_oss_setup_t *setup;
1601
1602         down(&pcm->streams[stream].oss.setup_mutex);
1603         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1604                 if (!strcmp(setup->task_name, task_name)) {
1605                         up(&pcm->streams[stream].oss.setup_mutex);
1606                         return setup;
1607                 }
1608         }
1609         ptr = ptrl = task_name;
1610         while (*ptr) {
1611                 if (*ptr == '/')
1612                         ptrl = ptr + 1;
1613                 ptr++;
1614         }
1615         if (ptrl == task_name) {
1616                 goto __not_found;
1617                 return NULL;
1618         }
1619         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1620                 if (!strcmp(setup->task_name, ptrl)) {
1621                         up(&pcm->streams[stream].oss.setup_mutex);
1622                         return setup;
1623                 }
1624         }
1625       __not_found:
1626         up(&pcm->streams[stream].oss.setup_mutex);
1627         return NULL;
1628 }
1629
1630 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1631                                        snd_pcm_oss_setup_t *setup,
1632                                        int minor)
1633 {
1634         snd_pcm_runtime_t *runtime;
1635
1636         substream->oss.oss = 1;
1637         substream->oss.setup = setup;
1638         runtime = substream->runtime;
1639         runtime->oss.params = 1;
1640         runtime->oss.trigger = 1;
1641         runtime->oss.rate = 8000;
1642         switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1643         case SNDRV_MINOR_OSS_PCM_8:
1644                 runtime->oss.format = AFMT_U8;
1645                 break;
1646         case SNDRV_MINOR_OSS_PCM_16:
1647                 runtime->oss.format = AFMT_S16_LE;
1648                 break;
1649         default:
1650                 runtime->oss.format = AFMT_MU_LAW;
1651         }
1652         runtime->oss.channels = 1;
1653         runtime->oss.fragshift = 0;
1654         runtime->oss.maxfrags = 0;
1655         runtime->oss.subdivision = 0;
1656 }
1657
1658 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1659 {
1660         snd_pcm_runtime_t *runtime;
1661         runtime = substream->runtime;
1662         if (runtime->oss.buffer)
1663                 vfree(runtime->oss.buffer);
1664         snd_pcm_oss_plugin_clear(substream);
1665         substream->oss.file = NULL;
1666         substream->oss.oss = 0;
1667 }
1668
1669 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1670 {
1671         int cidx;
1672         snd_assert(pcm_oss_file != NULL, return -ENXIO);
1673         for (cidx = 0; cidx < 2; ++cidx) {
1674                 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1675                 snd_pcm_runtime_t *runtime;
1676                 if (substream == NULL)
1677                         continue;
1678                 runtime = substream->runtime;
1679                 
1680                 snd_pcm_stream_lock_irq(substream);
1681                 if (snd_pcm_running(substream))
1682                         snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1683                 snd_pcm_stream_unlock_irq(substream);
1684                 if (substream->open_flag) {
1685                         if (substream->ops->hw_free != NULL)
1686                                 substream->ops->hw_free(substream);
1687                         substream->ops->close(substream);
1688                         substream->open_flag = 0;
1689                 }
1690                 substream->ffile = NULL;
1691                 snd_pcm_oss_release_substream(substream);
1692                 snd_pcm_release_substream(substream);
1693         }
1694         snd_magic_kfree(pcm_oss_file);
1695         return 0;
1696 }
1697
1698 static int snd_pcm_oss_open_file(struct file *file,
1699                                  snd_pcm_t *pcm,
1700                                  snd_pcm_oss_file_t **rpcm_oss_file,
1701                                  int minor,
1702                                  snd_pcm_oss_setup_t *psetup,
1703                                  snd_pcm_oss_setup_t *csetup)
1704 {
1705         int err = 0;
1706         snd_pcm_oss_file_t *pcm_oss_file;
1707         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1708         unsigned int f_mode = file->f_mode;
1709
1710         snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1711         *rpcm_oss_file = NULL;
1712
1713         pcm_oss_file = snd_magic_kcalloc(snd_pcm_oss_file_t, 0, GFP_KERNEL);
1714         if (pcm_oss_file == NULL)
1715                 return -ENOMEM;
1716
1717         if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1718             (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1719                 f_mode = FMODE_WRITE;
1720         if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1721                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1722                                                &psubstream)) < 0) {
1723                         snd_pcm_oss_release_file(pcm_oss_file);
1724                         return err;
1725                 }
1726                 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1727         }
1728         if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1729                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE, 
1730                                                &csubstream)) < 0) {
1731                         if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1732                                 snd_pcm_oss_release_file(pcm_oss_file);
1733                                 return err;
1734                         } else {
1735                                 csubstream = NULL;
1736                         }
1737                 }
1738                 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1739         }
1740         
1741         if (psubstream == NULL && csubstream == NULL) {
1742                 snd_pcm_oss_release_file(pcm_oss_file);
1743                 return -EINVAL;
1744         }
1745         if (psubstream != NULL) {
1746                 psubstream->oss.file = pcm_oss_file;
1747                 err = snd_pcm_hw_constraints_init(psubstream);
1748                 if (err < 0) {
1749                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1750                         snd_pcm_oss_release_file(pcm_oss_file);
1751                         return err;
1752                 }
1753                 if ((err = psubstream->ops->open(psubstream)) < 0) {
1754                         snd_pcm_oss_release_file(pcm_oss_file);
1755                         return err;
1756                 }
1757                 psubstream->open_flag = 1;
1758                 err = snd_pcm_hw_constraints_complete(psubstream);
1759                 if (err < 0) {
1760                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1761                         snd_pcm_oss_release_file(pcm_oss_file);
1762                         return err;
1763                 }
1764                 psubstream->ffile = file;
1765                 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1766         }
1767         if (csubstream != NULL) {
1768                 csubstream->oss.file = pcm_oss_file;
1769                 err = snd_pcm_hw_constraints_init(csubstream);
1770                 if (err < 0) {
1771                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1772                         snd_pcm_oss_release_file(pcm_oss_file);
1773                         return err;
1774                 }
1775                 if ((err = csubstream->ops->open(csubstream)) < 0) {
1776                         snd_pcm_oss_release_file(pcm_oss_file);
1777                         return err;
1778                 }
1779                 csubstream->open_flag = 1;
1780                 err = snd_pcm_hw_constraints_complete(csubstream);
1781                 if (err < 0) {
1782                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1783                         snd_pcm_oss_release_file(pcm_oss_file);
1784                         return err;
1785                 }
1786                 csubstream->ffile = file;
1787                 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1788         }
1789
1790         file->private_data = pcm_oss_file;
1791         *rpcm_oss_file = pcm_oss_file;
1792         return 0;
1793 }
1794
1795
1796 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1797 {
1798         int minor = iminor(inode);
1799         int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1800         int device;
1801         int err;
1802         char task_name[32];
1803         snd_pcm_t *pcm;
1804         snd_pcm_oss_file_t *pcm_oss_file;
1805         snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1806         int nonblock;
1807         wait_queue_t wait;
1808
1809         snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1810         device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1811                 adsp_map[cardnum] : dsp_map[cardnum];
1812
1813         pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1814         if (pcm == NULL) {
1815                 err = -ENODEV;
1816                 goto __error1;
1817         }
1818         err = snd_card_file_add(pcm->card, file);
1819         if (err < 0)
1820                 goto __error1;
1821         if (!try_module_get(pcm->card->module)) {
1822                 err = -EFAULT;
1823                 goto __error2;
1824         }
1825         if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1826                 err = -EFAULT;
1827                 goto __error;
1828         }
1829         if (file->f_mode & FMODE_WRITE)
1830                 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1831         if (file->f_mode & FMODE_READ)
1832                 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1833
1834         nonblock = !!(file->f_flags & O_NONBLOCK);
1835         if (psetup && !psetup->disable) {
1836                 if (psetup->nonblock)
1837                         nonblock = 1;
1838                 else if (psetup->block)
1839                         nonblock = 0;
1840         } else if (csetup && !csetup->disable) {
1841                 if (csetup->nonblock)
1842                         nonblock = 1;
1843                 else if (csetup->block)
1844                         nonblock = 0;
1845         }
1846         if (!nonblock)
1847                 nonblock = nonblock_open;
1848
1849         init_waitqueue_entry(&wait, current);
1850         add_wait_queue(&pcm->open_wait, &wait);
1851         down(&pcm->open_mutex);
1852         while (1) {
1853                 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1854                                             minor, psetup, csetup);
1855                 if (err >= 0)
1856                         break;
1857                 if (err == -EAGAIN) {
1858                         if (nonblock) {
1859                                 err = -EBUSY;
1860                                 break;
1861                         }
1862                 } else
1863                         break;
1864                 set_current_state(TASK_INTERRUPTIBLE);
1865                 up(&pcm->open_mutex);
1866                 schedule();
1867                 down(&pcm->open_mutex);
1868                 if (signal_pending(current)) {
1869                         err = -ERESTARTSYS;
1870                         break;
1871                 }
1872         }
1873         remove_wait_queue(&pcm->open_wait, &wait);
1874         up(&pcm->open_mutex);
1875         if (err < 0)
1876                 goto __error;
1877         return err;
1878
1879       __error:
1880         module_put(pcm->card->module);
1881       __error2:
1882         snd_card_file_remove(pcm->card, file);
1883       __error1:
1884         return err;
1885 }
1886
1887 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1888 {
1889         snd_pcm_t *pcm;
1890         snd_pcm_substream_t *substream;
1891         snd_pcm_oss_file_t *pcm_oss_file;
1892
1893         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1894         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1895         if (substream == NULL)
1896                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1897         snd_assert(substream != NULL, return -ENXIO);
1898         pcm = substream->pcm;
1899         snd_pcm_oss_sync(pcm_oss_file);
1900         down(&pcm->open_mutex);
1901         snd_pcm_oss_release_file(pcm_oss_file);
1902         up(&pcm->open_mutex);
1903         wake_up(&pcm->open_wait);
1904         module_put(pcm->card->module);
1905         snd_card_file_remove(pcm->card, file);
1906         return 0;
1907 }
1908
1909 static int snd_pcm_oss_ioctl(struct inode *inode, struct file *file,
1910                              unsigned int cmd, unsigned long arg)
1911 {
1912         snd_pcm_oss_file_t *pcm_oss_file;
1913         int res;
1914
1915         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
1916         if (cmd == OSS_GETVERSION)
1917                 return put_user(SNDRV_OSS_VERSION, (int *)arg);
1918         if (cmd == OSS_ALSAEMULVER)
1919                 return put_user(1, (int *)arg);
1920 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1921         if (((cmd >> 8) & 0xff) == 'M') {       /* mixer ioctl - for OSS compatibility */
1922                 snd_pcm_substream_t *substream;
1923                 int idx;
1924                 for (idx = 0; idx < 2; ++idx) {
1925                         substream = pcm_oss_file->streams[idx];
1926                         if (substream != NULL)
1927                                 break;
1928                 }
1929                 snd_assert(substream != NULL, return -ENXIO);
1930                 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1931         }
1932 #endif
1933         if (((cmd >> 8) & 0xff) != 'P')
1934                 return -EINVAL;
1935 #ifdef OSS_DEBUG
1936         printk("pcm_oss: ioctl = 0x%x\n", cmd);
1937 #endif
1938         switch (cmd) {
1939         case SNDCTL_DSP_RESET:
1940                 return snd_pcm_oss_reset(pcm_oss_file);
1941         case SNDCTL_DSP_SYNC:
1942                 return snd_pcm_oss_sync(pcm_oss_file);
1943         case SNDCTL_DSP_SPEED:
1944                 if (get_user(res, (int *)arg))
1945                         return -EFAULT;
1946                 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1947                         return res;
1948                 return put_user(res, (int *)arg);
1949         case SOUND_PCM_READ_RATE:
1950                 res = snd_pcm_oss_get_rate(pcm_oss_file);
1951                 if (res < 0)
1952                         return res;
1953                 return put_user(res, (int *)arg);
1954         case SNDCTL_DSP_STEREO:
1955                 if (get_user(res, (int *)arg))
1956                         return -EFAULT;
1957                 res = res > 0 ? 2 : 1;
1958                 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1959                         return res;
1960                 return put_user(--res, (int *)arg);
1961         case SNDCTL_DSP_GETBLKSIZE:
1962                 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1963                 if (res < 0)
1964                         return res;
1965                 return put_user(res, (int *)arg);
1966         case SNDCTL_DSP_SETFMT:
1967                 if (get_user(res, (int *)arg))
1968                         return -EFAULT;
1969                 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1970                 if (res < 0)
1971                         return res;
1972                 return put_user(res, (int *)arg);
1973         case SOUND_PCM_READ_BITS:
1974                 res = snd_pcm_oss_get_format(pcm_oss_file);
1975                 if (res < 0)
1976                         return res;
1977                 return put_user(res, (int *)arg);
1978         case SNDCTL_DSP_CHANNELS:
1979                 if (get_user(res, (int *)arg))
1980                         return -EFAULT;
1981                 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
1982                 if (res < 0)
1983                         return res;
1984                 return put_user(res, (int *)arg);
1985         case SOUND_PCM_READ_CHANNELS:
1986                 res = snd_pcm_oss_get_channels(pcm_oss_file);
1987                 if (res < 0)
1988                         return res;
1989                 return put_user(res, (int *)arg);
1990         case SOUND_PCM_WRITE_FILTER:
1991         case SOUND_PCM_READ_FILTER:
1992                 return -EIO;
1993         case SNDCTL_DSP_POST:
1994                 return snd_pcm_oss_post(pcm_oss_file);
1995         case SNDCTL_DSP_SUBDIVIDE:
1996                 if (get_user(res, (int *)arg))
1997                         return -EFAULT;
1998                 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
1999                 if (res < 0)
2000                         return res;
2001                 return put_user(res, (int *)arg);
2002         case SNDCTL_DSP_SETFRAGMENT:
2003                 if (get_user(res, (int *)arg))
2004                         return -EFAULT;
2005                 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2006         case SNDCTL_DSP_GETFMTS:
2007                 res = snd_pcm_oss_get_formats(pcm_oss_file);
2008                 if (res < 0)
2009                         return res;
2010                 return put_user(res, (int *)arg);
2011         case SNDCTL_DSP_GETOSPACE:
2012         case SNDCTL_DSP_GETISPACE:
2013                 return snd_pcm_oss_get_space(pcm_oss_file,
2014                         cmd == SNDCTL_DSP_GETISPACE ?
2015                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2016                         (struct audio_buf_info *) arg);
2017         case SNDCTL_DSP_NONBLOCK:
2018                 return snd_pcm_oss_nonblock(file);
2019         case SNDCTL_DSP_GETCAPS:
2020                 res = snd_pcm_oss_get_caps(pcm_oss_file);
2021                 if (res < 0)
2022                         return res;
2023                 return put_user(res, (int *)arg);
2024         case SNDCTL_DSP_GETTRIGGER:
2025                 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2026                 if (res < 0)
2027                         return res;
2028                 return put_user(res, (int *)arg);
2029         case SNDCTL_DSP_SETTRIGGER:
2030                 if (get_user(res, (int *)arg))
2031                         return -EFAULT;
2032                 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2033         case SNDCTL_DSP_GETIPTR:
2034         case SNDCTL_DSP_GETOPTR:
2035                 return snd_pcm_oss_get_ptr(pcm_oss_file,
2036                         cmd == SNDCTL_DSP_GETIPTR ?
2037                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2038                         (struct count_info *) arg);
2039         case SNDCTL_DSP_MAPINBUF:
2040         case SNDCTL_DSP_MAPOUTBUF:
2041                 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2042                         cmd == SNDCTL_DSP_MAPINBUF ?
2043                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2044                         (struct buffmem_desc *) arg);
2045         case SNDCTL_DSP_SETSYNCRO:
2046                 /* stop DMA now.. */
2047                 return 0;
2048         case SNDCTL_DSP_SETDUPLEX:
2049                 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2050                         return 0;
2051                 return -EIO;
2052         case SNDCTL_DSP_GETODELAY:
2053                 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2054                 if (res < 0) {
2055                         /* it's for sure, some broken apps don't check for error codes */
2056                         put_user(0, (int *)arg);
2057                         return res;
2058                 }
2059                 return put_user(res, (int *)arg);
2060         case SNDCTL_DSP_PROFILE:
2061                 return 0;       /* silently ignore */
2062         default:
2063                 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2064         }
2065         return -EINVAL;
2066 }
2067
2068 static ssize_t snd_pcm_oss_read(struct file *file, char *buf, size_t count, loff_t *offset)
2069 {
2070         snd_pcm_oss_file_t *pcm_oss_file;
2071         snd_pcm_substream_t *substream;
2072
2073         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2074         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2075         if (substream == NULL)
2076                 return -ENXIO;
2077 #ifndef OSS_DEBUG
2078         return snd_pcm_oss_read1(substream, buf, count);
2079 #else
2080         {
2081                 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2082                 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2083                 return res;
2084         }
2085 #endif
2086 }
2087
2088 static ssize_t snd_pcm_oss_write(struct file *file, const char *buf, size_t count, loff_t *offset)
2089 {
2090         snd_pcm_oss_file_t *pcm_oss_file;
2091         snd_pcm_substream_t *substream;
2092         long result;
2093
2094         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2095         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2096         if (substream == NULL)
2097                 return -ENXIO;
2098         up(&file->f_dentry->d_inode->i_sem);
2099         result = snd_pcm_oss_write1(substream, buf, count);
2100         down(&file->f_dentry->d_inode->i_sem);
2101 #ifdef OSS_DEBUG
2102         printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2103 #endif
2104         return result;
2105 }
2106
2107 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2108 {
2109         snd_pcm_runtime_t *runtime = substream->runtime;
2110         if (atomic_read(&runtime->mmap_count))
2111                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2112         else
2113                 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2114 }
2115
2116 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2117 {
2118         snd_pcm_runtime_t *runtime = substream->runtime;
2119         if (atomic_read(&runtime->mmap_count))
2120                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2121         else
2122                 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2123 }
2124
2125 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2126 {
2127         snd_pcm_oss_file_t *pcm_oss_file;
2128         unsigned int mask;
2129         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2130         
2131         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return 0);
2132
2133         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2134         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2135
2136         mask = 0;
2137         if (psubstream != NULL) {
2138                 snd_pcm_runtime_t *runtime = psubstream->runtime;
2139                 poll_wait(file, &runtime->sleep, wait);
2140                 snd_pcm_stream_lock_irq(psubstream);
2141                 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2142                     (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2143                      snd_pcm_oss_playback_ready(psubstream)))
2144                         mask |= POLLOUT | POLLWRNORM;
2145                 snd_pcm_stream_unlock_irq(psubstream);
2146         }
2147         if (csubstream != NULL) {
2148                 snd_pcm_runtime_t *runtime = csubstream->runtime;
2149                 enum sndrv_pcm_state ostate;
2150                 poll_wait(file, &runtime->sleep, wait);
2151                 snd_pcm_stream_lock_irq(csubstream);
2152                 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2153                     snd_pcm_oss_capture_ready(csubstream))
2154                         mask |= POLLIN | POLLRDNORM;
2155                 snd_pcm_stream_unlock_irq(csubstream);
2156                 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2157                         snd_pcm_oss_file_t ofile;
2158                         memset(&ofile, 0, sizeof(ofile));
2159                         ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2160                         runtime->oss.trigger = 0;
2161                         snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2162                 }
2163         }
2164
2165         return mask;
2166 }
2167
2168 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2169 {
2170         snd_pcm_oss_file_t *pcm_oss_file;
2171         snd_pcm_substream_t *substream = NULL;
2172         snd_pcm_runtime_t *runtime;
2173         int err;
2174
2175 #ifdef OSS_DEBUG
2176         printk("pcm_oss: mmap begin\n");
2177 #endif
2178         pcm_oss_file = snd_magic_cast(snd_pcm_oss_file_t, file->private_data, return -ENXIO);
2179         switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2180         case VM_READ | VM_WRITE:
2181                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2182                 if (substream)
2183                         break;
2184                 /* Fall through */
2185         case VM_READ:
2186                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2187                 break;
2188         case VM_WRITE:
2189                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2190                 break;
2191         default:
2192                 return -EINVAL;
2193         }
2194         /* set VM_READ access as well to fix memset() routines that do
2195            reads before writes (to improve performance) */
2196         area->vm_flags |= VM_READ;
2197         if (substream == NULL)
2198                 return -ENXIO;
2199         runtime = substream->runtime;
2200         if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2201                 return -EIO;
2202         if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2203                 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2204         else
2205                 return -EIO;
2206         
2207         if (runtime->oss.params) {
2208                 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2209                         return err;
2210         }
2211         if (runtime->oss.plugin_first != NULL)
2212                 return -EIO;
2213
2214         if (area->vm_pgoff != 0)
2215                 return -EINVAL;
2216
2217         err = snd_pcm_mmap_data(substream, file, area);
2218         if (err < 0)
2219                 return err;
2220         runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2221         runtime->silence_threshold = 0;
2222         runtime->silence_size = 0;
2223 #ifdef OSS_DEBUG
2224         printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2225 #endif
2226         /* In mmap mode we never stop */
2227         runtime->stop_threshold = runtime->boundary;
2228
2229         return 0;
2230 }
2231
2232 /*
2233  *  /proc interface
2234  */
2235
2236 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2237                                   snd_info_buffer_t * buffer)
2238 {
2239         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2240         snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2241         down(&pstr->oss.setup_mutex);
2242         while (setup) {
2243                 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2244                             setup->task_name,
2245                             setup->periods,
2246                             setup->period_size,
2247                             setup->disable ? " disable" : "",
2248                             setup->direct ? " direct" : "",
2249                             setup->block ? " block" : "",
2250                             setup->nonblock ? " non-block" : "",
2251                             setup->partialfrag ? " partial-frag" : "",
2252                             setup->nosilence ? " no-silence" : "");
2253                 setup = setup->next;
2254         }
2255         up(&pstr->oss.setup_mutex);
2256 }
2257
2258 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2259 {
2260         unsigned int idx;
2261         snd_pcm_substream_t *substream;
2262         snd_pcm_oss_setup_t *setup, *setupn;
2263
2264         for (idx = 0, substream = pstr->substream;
2265              idx < pstr->substream_count; idx++, substream = substream->next)
2266                 substream->oss.setup = NULL;
2267         for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2268              setup; setup = setupn) {
2269                 setupn = setup->next;
2270                 kfree(setup->task_name);
2271                 kfree(setup);
2272         }
2273         pstr->oss.setup_list = NULL;
2274 }
2275
2276 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2277                                    snd_info_buffer_t * buffer)
2278 {
2279         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2280         char line[512], str[32], task_name[32], *ptr;
2281         int idx1;
2282         snd_pcm_oss_setup_t *setup, *setup1, template;
2283
2284         while (!snd_info_get_line(buffer, line, sizeof(line))) {
2285                 down(&pstr->oss.setup_mutex);
2286                 memset(&template, 0, sizeof(template));
2287                 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2288                 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2289                         snd_pcm_oss_proc_free_setup_list(pstr);
2290                         up(&pstr->oss.setup_mutex);
2291                         continue;
2292                 }
2293                 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2294                         if (!strcmp(setup->task_name, task_name)) {
2295                                 template = *setup;
2296                                 break;
2297                         }
2298                 }
2299                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2300                 template.periods = simple_strtoul(str, NULL, 10);
2301                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2302                 template.period_size = simple_strtoul(str, NULL, 10);
2303                 for (idx1 = 31; idx1 >= 0; idx1--)
2304                         if (template.period_size & (1 << idx1))
2305                                 break;
2306                 for (idx1--; idx1 >= 0; idx1--)
2307                         template.period_size &= ~(1 << idx1);
2308                 do {
2309                         ptr = snd_info_get_str(str, ptr, sizeof(str));
2310                         if (!strcmp(str, "disable")) {
2311                                 template.disable = 1;
2312                         } else if (!strcmp(str, "direct")) {
2313                                 template.direct = 1;
2314                         } else if (!strcmp(str, "block")) {
2315                                 template.block = 1;
2316                         } else if (!strcmp(str, "non-block")) {
2317                                 template.nonblock = 1;
2318                         } else if (!strcmp(str, "partial-frag")) {
2319                                 template.partialfrag = 1;
2320                         } else if (!strcmp(str, "no-silence")) {
2321                                 template.nosilence = 1;
2322                         }
2323                 } while (*str);
2324                 if (setup == NULL) {
2325                         setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2326                         if (setup) {
2327                                 if (pstr->oss.setup_list == NULL) {
2328                                         pstr->oss.setup_list = setup;
2329                                 } else {
2330                                         for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2331                                         setup1->next = setup;
2332                                 }
2333                                 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2334                         } else {
2335                                 buffer->error = -ENOMEM;
2336                         }
2337                 }
2338                 if (setup)
2339                         *setup = template;
2340                 up(&pstr->oss.setup_mutex);
2341         }
2342 }
2343
2344 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2345 {
2346         int stream;
2347         for (stream = 0; stream < 2; ++stream) {
2348                 snd_info_entry_t *entry;
2349                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2350                 if (pstr->substream_count == 0)
2351                         continue;
2352                 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2353                         entry->content = SNDRV_INFO_CONTENT_TEXT;
2354                         entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2355                         entry->c.text.read_size = 8192;
2356                         entry->c.text.read = snd_pcm_oss_proc_read;
2357                         entry->c.text.write_size = 8192;
2358                         entry->c.text.write = snd_pcm_oss_proc_write;
2359                         entry->private_data = pstr;
2360                         if (snd_info_register(entry) < 0) {
2361                                 snd_info_free_entry(entry);
2362                                 entry = NULL;
2363                         }
2364                 }
2365                 pstr->oss.proc_entry = entry;
2366         }
2367 }
2368
2369 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2370 {
2371         int stream;
2372         for (stream = 0; stream < 2; ++stream) {
2373                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2374                 if (pstr->oss.proc_entry) {
2375                         snd_info_unregister(pstr->oss.proc_entry);
2376                         pstr->oss.proc_entry = NULL;
2377                         snd_pcm_oss_proc_free_setup_list(pstr);
2378                 }
2379         }
2380 }
2381
2382 /*
2383  *  ENTRY functions
2384  */
2385
2386 static struct file_operations snd_pcm_oss_f_reg =
2387 {
2388         .owner =        THIS_MODULE,
2389         .read =         snd_pcm_oss_read,
2390         .write =        snd_pcm_oss_write,
2391         .open =         snd_pcm_oss_open,
2392         .release =      snd_pcm_oss_release,
2393         .poll =         snd_pcm_oss_poll,
2394         .ioctl =        snd_pcm_oss_ioctl,
2395         .mmap =         snd_pcm_oss_mmap,
2396 };
2397
2398 static snd_minor_t snd_pcm_oss_reg =
2399 {
2400         .comment =      "digital audio",
2401         .f_ops =        &snd_pcm_oss_f_reg,
2402 };
2403
2404 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2405 {
2406         char name[128];
2407         sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2408         if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2409                                     pcm->card, index, &snd_pcm_oss_reg,
2410                                     name) < 0) {
2411                 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2412         }
2413 }
2414
2415 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2416 {
2417         pcm->oss.reg = 0;
2418         if (dsp_map[pcm->card->number] == (int)pcm->device) {
2419                 char name[128];
2420                 int duplex;
2421                 register_oss_dsp(pcm, 0);
2422                 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
2423                               pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
2424                               !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2425                 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2426 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2427                 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2428                                       pcm->card->number,
2429                                       name);
2430 #endif
2431                 pcm->oss.reg++;
2432                 pcm->oss.reg_mask |= 1;
2433         }
2434         if (adsp_map[pcm->card->number] == (int)pcm->device) {
2435                 register_oss_dsp(pcm, 1);
2436                 pcm->oss.reg++;
2437                 pcm->oss.reg_mask |= 2;
2438         }
2439
2440         if (pcm->oss.reg)
2441                 snd_pcm_oss_proc_init(pcm);
2442
2443         return 0;
2444 }
2445
2446 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2447 {
2448         if (pcm->oss.reg) {
2449                 if (pcm->oss.reg_mask & 1) {
2450                         pcm->oss.reg_mask &= ~1;
2451                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2452                                                   pcm->card, 0);
2453                 }
2454                 if (pcm->oss.reg_mask & 2) {
2455                         pcm->oss.reg_mask &= ~2;
2456                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2457                                                   pcm->card, 1);
2458                 }
2459         }
2460         return 0;
2461 }
2462
2463 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2464 {
2465         snd_pcm_oss_disconnect_minor(pcm);
2466         if (pcm->oss.reg) {
2467                 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2468 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2469                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2470 #endif
2471                 }
2472                 pcm->oss.reg = 0;
2473                 snd_pcm_oss_proc_done(pcm);
2474         }
2475         return 0;
2476 }
2477
2478 static snd_pcm_notify_t snd_pcm_oss_notify =
2479 {
2480         .n_register =   snd_pcm_oss_register_minor,
2481         .n_disconnect = snd_pcm_oss_disconnect_minor,
2482         .n_unregister = snd_pcm_oss_unregister_minor,
2483 };
2484
2485 static int __init alsa_pcm_oss_init(void)
2486 {
2487         int i;
2488         int err;
2489
2490         /* check device map table */
2491         for (i = 0; i < SNDRV_CARDS; i++) {
2492                 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2493                         snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2494                         dsp_map[i] = 0;
2495                 }
2496                 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2497                         snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2498                         adsp_map[i] = 1;
2499                 }
2500         }
2501         if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2502                 return err;
2503         return 0;
2504 }
2505
2506 static void __exit alsa_pcm_oss_exit(void)
2507 {
2508         snd_pcm_notify(&snd_pcm_oss_notify, 1);
2509 }
2510
2511 module_init(alsa_pcm_oss_init)
2512 module_exit(alsa_pcm_oss_exit)
2513
2514 #ifndef MODULE
2515
2516 /* format is: snd-pcm-oss=dsp_map,adsp_map[,nonblock_open] */
2517
2518 static int __init alsa_pcm_oss_setup(char *str)
2519 {
2520         static unsigned __initdata nr_dev = 0;
2521
2522         if (nr_dev >= SNDRV_CARDS)
2523                 return 0;
2524         (void)(get_option(&str,&dsp_map[nr_dev]) == 2 &&
2525                get_option(&str,&adsp_map[nr_dev]) == 2);
2526         (void)(get_option(&str,&nonblock_open) == 2);
2527         nr_dev++;
2528         return 1;
2529 }
2530
2531 __setup("snd-pcm-oss=", alsa_pcm_oss_setup);
2532
2533 #endif /* !MODULE */