203470df5857b9eb6ab47cccf9f6e3ea27e6793f
[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/smp_lock.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/vmalloc.h>
35 #include <linux/moduleparam.h>
36 #include <sound/core.h>
37 #include <sound/minors.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include "pcm_plugin.h"
41 #include <sound/info.h>
42 #include <linux/soundcard.h>
43 #include <sound/initval.h>
44
45 #define OSS_ALSAEMULVER         _SIOR ('M', 249, int)
46
47 static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
48 static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
49 static int nonblock_open = 1;
50
51 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
52 MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
53 MODULE_LICENSE("GPL");
54 module_param_array(dsp_map, int, NULL, 0444);
55 MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
56 module_param_array(adsp_map, int, NULL, 0444);
57 MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
58 module_param(nonblock_open, bool, 0644);
59 MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
61 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
62
63 extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);
64 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);
65 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);
66 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);
67
68 static inline mm_segment_t snd_enter_user(void)
69 {
70         mm_segment_t fs = get_fs();
71         set_fs(get_ds());
72         return fs;
73 }
74
75 static inline void snd_leave_user(mm_segment_t fs)
76 {
77         set_fs(fs);
78 }
79
80 static int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream)
81 {
82         snd_pcm_runtime_t *runtime = substream->runtime;
83         snd_pcm_plugin_t *plugin, *next;
84         
85         plugin = runtime->oss.plugin_first;
86         while (plugin) {
87                 next = plugin->next;
88                 snd_pcm_plugin_free(plugin);
89                 plugin = next;
90         }
91         runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
92         return 0;
93 }
94
95 static int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin)
96 {
97         snd_pcm_runtime_t *runtime = plugin->plug->runtime;
98         plugin->next = runtime->oss.plugin_first;
99         plugin->prev = NULL;
100         if (runtime->oss.plugin_first) {
101                 runtime->oss.plugin_first->prev = plugin;
102                 runtime->oss.plugin_first = plugin;
103         } else {
104                 runtime->oss.plugin_last =
105                 runtime->oss.plugin_first = plugin;
106         }
107         return 0;
108 }
109
110 int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin)
111 {
112         snd_pcm_runtime_t *runtime = plugin->plug->runtime;
113         plugin->next = NULL;
114         plugin->prev = runtime->oss.plugin_last;
115         if (runtime->oss.plugin_last) {
116                 runtime->oss.plugin_last->next = plugin;
117                 runtime->oss.plugin_last = plugin;
118         } else {
119                 runtime->oss.plugin_last =
120                 runtime->oss.plugin_first = plugin;
121         }
122         return 0;
123 }
124
125 static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames)
126 {
127         snd_pcm_runtime_t *runtime = substream->runtime;
128         long buffer_size = snd_pcm_lib_buffer_bytes(substream);
129         long bytes = frames_to_bytes(runtime, frames);
130         if (buffer_size == runtime->oss.buffer_bytes)
131                 return bytes;
132 #if BITS_PER_LONG >= 64
133         return runtime->oss.buffer_bytes * bytes / buffer_size;
134 #else
135         {
136                 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
137                 u32 rem;
138                 div64_32(&bsize, buffer_size, &rem);
139                 return (long)bsize;
140         }
141 #endif
142 }
143
144 static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes)
145 {
146         snd_pcm_runtime_t *runtime = substream->runtime;
147         long buffer_size = snd_pcm_lib_buffer_bytes(substream);
148         if (buffer_size == runtime->oss.buffer_bytes)
149                 return bytes_to_frames(runtime, bytes);
150         return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
151 }
152
153 static int snd_pcm_oss_format_from(int format)
154 {
155         switch (format) {
156         case AFMT_MU_LAW:       return SNDRV_PCM_FORMAT_MU_LAW;
157         case AFMT_A_LAW:        return SNDRV_PCM_FORMAT_A_LAW;
158         case AFMT_IMA_ADPCM:    return SNDRV_PCM_FORMAT_IMA_ADPCM;
159         case AFMT_U8:           return SNDRV_PCM_FORMAT_U8;
160         case AFMT_S16_LE:       return SNDRV_PCM_FORMAT_S16_LE;
161         case AFMT_S16_BE:       return SNDRV_PCM_FORMAT_S16_BE;
162         case AFMT_S8:           return SNDRV_PCM_FORMAT_S8;
163         case AFMT_U16_LE:       return SNDRV_PCM_FORMAT_U16_LE;
164         case AFMT_U16_BE:       return SNDRV_PCM_FORMAT_U16_BE;
165         case AFMT_MPEG:         return SNDRV_PCM_FORMAT_MPEG;
166         default:                return SNDRV_PCM_FORMAT_U8;
167         }
168 }
169
170 static int snd_pcm_oss_format_to(int format)
171 {
172         switch (format) {
173         case SNDRV_PCM_FORMAT_MU_LAW:   return AFMT_MU_LAW;
174         case SNDRV_PCM_FORMAT_A_LAW:    return AFMT_A_LAW;
175         case SNDRV_PCM_FORMAT_IMA_ADPCM:        return AFMT_IMA_ADPCM;
176         case SNDRV_PCM_FORMAT_U8:               return AFMT_U8;
177         case SNDRV_PCM_FORMAT_S16_LE:   return AFMT_S16_LE;
178         case SNDRV_PCM_FORMAT_S16_BE:   return AFMT_S16_BE;
179         case SNDRV_PCM_FORMAT_S8:               return AFMT_S8;
180         case SNDRV_PCM_FORMAT_U16_LE:   return AFMT_U16_LE;
181         case SNDRV_PCM_FORMAT_U16_BE:   return AFMT_U16_BE;
182         case SNDRV_PCM_FORMAT_MPEG:             return AFMT_MPEG;
183         default:                        return -EINVAL;
184         }
185 }
186
187 static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream, 
188                                    snd_pcm_hw_params_t *oss_params,
189                                    snd_pcm_hw_params_t *slave_params)
190 {
191         size_t s;
192         size_t oss_buffer_size, oss_period_size, oss_periods;
193         size_t min_period_size, max_period_size;
194         snd_pcm_runtime_t *runtime = substream->runtime;
195         size_t oss_frame_size;
196
197         oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
198                          params_channels(oss_params) / 8;
199
200         oss_buffer_size = snd_pcm_plug_client_size(substream,
201                                                    snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
202         oss_buffer_size = 1 << ld2(oss_buffer_size);
203         if (atomic_read(&runtime->mmap_count)) {
204                 if (oss_buffer_size > runtime->oss.mmap_bytes)
205                         oss_buffer_size = runtime->oss.mmap_bytes;
206         }
207
208         if (substream->oss.setup &&
209             substream->oss.setup->period_size > 16)
210                 oss_period_size = substream->oss.setup->period_size;
211         else if (runtime->oss.fragshift) {
212                 oss_period_size = 1 << runtime->oss.fragshift;
213                 if (oss_period_size > oss_buffer_size / 2)
214                         oss_period_size = oss_buffer_size / 2;
215         } else {
216                 int sd;
217                 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
218
219                 oss_period_size = oss_buffer_size;
220                 do {
221                         oss_period_size /= 2;
222                 } while (oss_period_size > bytes_per_sec);
223                 if (runtime->oss.subdivision == 0) {
224                         sd = 4;
225                         if (oss_period_size / sd > 4096)
226                                 sd *= 2;
227                         if (oss_period_size / sd < 4096)
228                                 sd = 1;
229                 } else
230                         sd = runtime->oss.subdivision;
231                 oss_period_size /= sd;
232                 if (oss_period_size < 16)
233                         oss_period_size = 16;
234         }
235
236         min_period_size = snd_pcm_plug_client_size(substream,
237                                                    snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
238         min_period_size *= oss_frame_size;
239         min_period_size = 1 << (ld2(min_period_size - 1) + 1);
240         if (oss_period_size < min_period_size)
241                 oss_period_size = min_period_size;
242
243         max_period_size = snd_pcm_plug_client_size(substream,
244                                                    snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
245         max_period_size *= oss_frame_size;
246         max_period_size = 1 << ld2(max_period_size);
247         if (oss_period_size > max_period_size)
248                 oss_period_size = max_period_size;
249
250         oss_periods = oss_buffer_size / oss_period_size;
251
252         if (substream->oss.setup) {
253                 if (substream->oss.setup->periods > 1)
254                         oss_periods = substream->oss.setup->periods;
255         }
256
257         s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
258         if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
259                 s = runtime->oss.maxfrags;
260         if (oss_periods > s)
261                 oss_periods = s;
262
263         s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
264         if (s < 2)
265                 s = 2;
266         if (oss_periods < s)
267                 oss_periods = s;
268
269         while (oss_period_size * oss_periods > oss_buffer_size)
270                 oss_period_size /= 2;
271
272         snd_assert(oss_period_size >= 16, return -EINVAL);
273         runtime->oss.period_bytes = oss_period_size;
274         runtime->oss.period_frames = 1;
275         runtime->oss.periods = oss_periods;
276         return 0;
277 }
278
279 static int choose_rate(snd_pcm_substream_t *substream,
280                        snd_pcm_hw_params_t *params, unsigned int best_rate)
281 {
282         snd_interval_t *it;
283         snd_pcm_hw_params_t *save;
284         unsigned int rate, prev;
285
286         save = kmalloc(sizeof(*save), GFP_KERNEL);
287         if (save == NULL)
288                 return -ENOMEM;
289         *save = *params;
290         it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
291
292         /* try multiples of the best rate */
293         rate = best_rate;
294         for (;;) {
295                 if (it->max < rate || (it->max == rate && it->openmax))
296                         break;
297                 if (it->min < rate || (it->min == rate && !it->openmin)) {
298                         int ret;
299                         ret = snd_pcm_hw_param_set(substream, params,
300                                                    SNDRV_PCM_HW_PARAM_RATE,
301                                                    rate, 0);
302                         if (ret == (int)rate) {
303                                 kfree(save);
304                                 return rate;
305                         }
306                         *params = *save;
307                 }
308                 prev = rate;
309                 rate += best_rate;
310                 if (rate <= prev)
311                         break;
312         }
313
314         /* not found, use the nearest rate */
315         kfree(save);
316         return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
317 }
318
319 static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream)
320 {
321         snd_pcm_runtime_t *runtime = substream->runtime;
322         snd_pcm_hw_params_t *params, *sparams;
323         snd_pcm_sw_params_t *sw_params;
324         ssize_t oss_buffer_size, oss_period_size;
325         size_t oss_frame_size;
326         int err;
327         int direct;
328         int format, sformat, n;
329         snd_mask_t sformat_mask;
330         snd_mask_t mask;
331
332         sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
333         params = kmalloc(sizeof(*params), GFP_KERNEL);
334         sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
335         if (!sw_params || !params || !sparams) {
336                 snd_printd("No memory\n");
337                 err = -ENOMEM;
338                 goto failure;
339         }
340
341         if (atomic_read(&runtime->mmap_count)) {
342                 direct = 1;
343         } else {
344                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
345                 direct = (setup != NULL && setup->direct);
346         }
347
348         _snd_pcm_hw_params_any(sparams);
349         _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
350         _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
351         snd_mask_none(&mask);
352         if (atomic_read(&runtime->mmap_count))
353                 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
354         else {
355                 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
356                 if (!direct)
357                         snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
358         }
359         err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
360         if (err < 0) {
361                 snd_printd("No usable accesses\n");
362                 err = -EINVAL;
363                 goto failure;
364         }
365         choose_rate(substream, sparams, runtime->oss.rate);
366         snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
367
368         format = snd_pcm_oss_format_from(runtime->oss.format);
369
370         sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
371         if (direct)
372                 sformat = format;
373         else
374                 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
375
376         if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
377                 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
378                         if (snd_mask_test(&sformat_mask, sformat) &&
379                             snd_pcm_oss_format_to(sformat) >= 0)
380                                 break;
381                 }
382                 if (sformat > SNDRV_PCM_FORMAT_LAST) {
383                         snd_printd("Cannot find a format!!!\n");
384                         err = -EINVAL;
385                         goto failure;
386                 }
387         }
388         err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
389         snd_assert(err >= 0, goto failure);
390
391         if (direct) {
392                 memcpy(params, sparams, sizeof(*params));
393         } else {
394                 _snd_pcm_hw_params_any(params);
395                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
396                                       SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
397                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
398                                       snd_pcm_oss_format_from(runtime->oss.format), 0);
399                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
400                                       runtime->oss.channels, 0);
401                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
402                                       runtime->oss.rate, 0);
403                 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
404                          params_access(params), params_format(params),
405                          params_channels(params), params_rate(params));
406         }
407         pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
408                  params_access(sparams), params_format(sparams),
409                  params_channels(sparams), params_rate(sparams));
410
411         oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
412                          params_channels(params) / 8;
413
414         snd_pcm_oss_plugin_clear(substream);
415         if (!direct) {
416                 /* add necessary plugins */
417                 snd_pcm_oss_plugin_clear(substream);
418                 if ((err = snd_pcm_plug_format_plugins(substream,
419                                                        params, 
420                                                        sparams)) < 0) {
421                         snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
422                         snd_pcm_oss_plugin_clear(substream);
423                         goto failure;
424                 }
425                 if (runtime->oss.plugin_first) {
426                         snd_pcm_plugin_t *plugin;
427                         if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
428                                 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
429                                 snd_pcm_oss_plugin_clear(substream);
430                                 goto failure;
431                         }
432                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
433                                 err = snd_pcm_plugin_append(plugin);
434                         } else {
435                                 err = snd_pcm_plugin_insert(plugin);
436                         }
437                         if (err < 0) {
438                                 snd_pcm_oss_plugin_clear(substream);
439                                 goto failure;
440                         }
441                 }
442         }
443
444         err = snd_pcm_oss_period_size(substream, params, sparams);
445         if (err < 0)
446                 goto failure;
447
448         n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
449         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
450         snd_assert(err >= 0, goto failure);
451
452         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
453                                      runtime->oss.periods, NULL);
454         snd_assert(err >= 0, goto failure);
455
456         snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
457
458         if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
459                 snd_printd("HW_PARAMS failed: %i\n", err);
460                 goto failure;
461         }
462
463         memset(sw_params, 0, sizeof(*sw_params));
464         if (runtime->oss.trigger) {
465                 sw_params->start_threshold = 1;
466         } else {
467                 sw_params->start_threshold = runtime->boundary;
468         }
469         if (atomic_read(&runtime->mmap_count) || substream->stream == SNDRV_PCM_STREAM_CAPTURE)
470                 sw_params->stop_threshold = runtime->boundary;
471         else
472                 sw_params->stop_threshold = runtime->buffer_size;
473         sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
474         sw_params->period_step = 1;
475         sw_params->sleep_min = 0;
476         sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
477                 1 : runtime->period_size;
478         sw_params->xfer_align = 1;
479         if (atomic_read(&runtime->mmap_count) ||
480             (substream->oss.setup && substream->oss.setup->nosilence)) {
481                 sw_params->silence_threshold = 0;
482                 sw_params->silence_size = 0;
483         } else {
484                 snd_pcm_uframes_t frames;
485                 frames = runtime->period_size + 16;
486                 if (frames > runtime->buffer_size)
487                         frames = runtime->buffer_size;
488                 sw_params->silence_threshold = frames;
489                 sw_params->silence_size = frames;
490         }
491
492         if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
493                 snd_printd("SW_PARAMS failed: %i\n", err);
494                 goto failure;
495         }
496
497         runtime->oss.periods = params_periods(sparams);
498         oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
499         snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
500         if (runtime->oss.plugin_first) {
501                 err = snd_pcm_plug_alloc(substream, oss_period_size);
502                 if (err < 0)
503                         goto failure;
504         }
505         oss_period_size *= oss_frame_size;
506
507         oss_buffer_size = oss_period_size * runtime->oss.periods;
508         snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
509
510         runtime->oss.period_bytes = oss_period_size;
511         runtime->oss.buffer_bytes = oss_buffer_size;
512
513         pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
514                  runtime->oss.period_bytes,
515                  runtime->oss.buffer_bytes);
516         pdprintf("slave: period_size = %i, buffer_size = %i\n",
517                  params_period_size(sparams),
518                  params_buffer_size(sparams));
519
520         runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
521         runtime->oss.channels = params_channels(params);
522         runtime->oss.rate = params_rate(params);
523
524         runtime->oss.params = 0;
525         runtime->oss.prepare = 1;
526         vfree(runtime->oss.buffer);
527         runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
528         runtime->oss.buffer_used = 0;
529         if (runtime->dma_area)
530                 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
531
532         runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
533
534         err = 0;
535 failure:
536         kfree(sw_params);
537         kfree(params);
538         kfree(sparams);
539         return err;
540 }
541
542 static int snd_pcm_oss_get_active_substream(snd_pcm_oss_file_t *pcm_oss_file, snd_pcm_substream_t **r_substream)
543 {
544         int idx, err;
545         snd_pcm_substream_t *asubstream = NULL, *substream;
546
547         for (idx = 0; idx < 2; idx++) {
548                 substream = pcm_oss_file->streams[idx];
549                 if (substream == NULL)
550                         continue;
551                 if (asubstream == NULL)
552                         asubstream = substream;
553                 if (substream->runtime->oss.params) {
554                         err = snd_pcm_oss_change_params(substream);
555                         if (err < 0)
556                                 return err;
557                 }
558         }
559         snd_assert(asubstream != NULL, return -EIO);
560         if (r_substream)
561                 *r_substream = asubstream;
562         return 0;
563 }
564
565 static int snd_pcm_oss_prepare(snd_pcm_substream_t *substream)
566 {
567         int err;
568         snd_pcm_runtime_t *runtime = substream->runtime;
569
570         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
571         if (err < 0) {
572                 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
573                 return err;
574         }
575         runtime->oss.prepare = 0;
576         runtime->oss.prev_hw_ptr_interrupt = 0;
577         runtime->oss.period_ptr = 0;
578         runtime->oss.buffer_used = 0;
579
580         return 0;
581 }
582
583 static int snd_pcm_oss_make_ready(snd_pcm_substream_t *substream)
584 {
585         snd_pcm_runtime_t *runtime;
586         int err;
587
588         if (substream == NULL)
589                 return 0;
590         runtime = substream->runtime;
591         if (runtime->oss.params) {
592                 err = snd_pcm_oss_change_params(substream);
593                 if (err < 0)
594                         return err;
595         }
596         if (runtime->oss.prepare) {
597                 err = snd_pcm_oss_prepare(substream);
598                 if (err < 0)
599                         return err;
600         }
601         return 0;
602 }
603
604 static int snd_pcm_oss_capture_position_fixup(snd_pcm_substream_t *substream, snd_pcm_sframes_t *delay)
605 {
606         snd_pcm_runtime_t *runtime;
607         snd_pcm_uframes_t frames;
608         int err = 0;
609
610         while (1) {
611                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
612                 if (err < 0)
613                         break;
614                 runtime = substream->runtime;
615                 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
616                         break;
617                 /* in case of overrun, skip whole periods like OSS/Linux driver does */
618                 /* until avail(delay) <= buffer_size */
619                 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
620                 frames /= runtime->period_size;
621                 frames *= runtime->period_size;
622                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
623                 if (err < 0)
624                         break;
625         }
626         return err;
627 }
628
629 snd_pcm_sframes_t snd_pcm_oss_write3(snd_pcm_substream_t *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
630 {
631         snd_pcm_runtime_t *runtime = substream->runtime;
632         int ret;
633         while (1) {
634                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
635                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
636 #ifdef OSS_DEBUG
637                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
638                                 printk("pcm_oss: write: recovering from XRUN\n");
639                         else
640                                 printk("pcm_oss: write: recovering from SUSPEND\n");
641 #endif
642                         ret = snd_pcm_oss_prepare(substream);
643                         if (ret < 0)
644                                 break;
645                 }
646                 if (in_kernel) {
647                         mm_segment_t fs;
648                         fs = snd_enter_user();
649                         ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
650                         snd_leave_user(fs);
651                 } else {
652                         ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
653                 }
654                 if (ret != -EPIPE && ret != -ESTRPIPE)
655                         break;
656                 /* test, if we can't store new data, because the stream */
657                 /* has not been started */
658                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
659                         return -EAGAIN;
660         }
661         return ret;
662 }
663
664 snd_pcm_sframes_t snd_pcm_oss_read3(snd_pcm_substream_t *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
665 {
666         snd_pcm_runtime_t *runtime = substream->runtime;
667         snd_pcm_sframes_t delay;
668         int ret;
669         while (1) {
670                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
671                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
672 #ifdef OSS_DEBUG
673                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
674                                 printk("pcm_oss: read: recovering from XRUN\n");
675                         else
676                                 printk("pcm_oss: read: recovering from SUSPEND\n");
677 #endif
678                         ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
679                         if (ret < 0)
680                                 break;
681                 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
682                         ret = snd_pcm_oss_prepare(substream);
683                         if (ret < 0)
684                                 break;
685                 }
686                 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
687                 if (ret < 0)
688                         break;
689                 if (in_kernel) {
690                         mm_segment_t fs;
691                         fs = snd_enter_user();
692                         ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
693                         snd_leave_user(fs);
694                 } else {
695                         ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
696                 }
697                 if (ret == -EPIPE) {
698                         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
699                                 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
700                                 if (ret < 0)
701                                         break;
702                         }
703                         continue;
704                 }
705                 if (ret != -ESTRPIPE)
706                         break;
707         }
708         return ret;
709 }
710
711 snd_pcm_sframes_t snd_pcm_oss_writev3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
712 {
713         snd_pcm_runtime_t *runtime = substream->runtime;
714         int ret;
715         while (1) {
716                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
717                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
718 #ifdef OSS_DEBUG
719                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
720                                 printk("pcm_oss: writev: recovering from XRUN\n");
721                         else
722                                 printk("pcm_oss: writev: recovering from SUSPEND\n");
723 #endif
724                         ret = snd_pcm_oss_prepare(substream);
725                         if (ret < 0)
726                                 break;
727                 }
728                 if (in_kernel) {
729                         mm_segment_t fs;
730                         fs = snd_enter_user();
731                         ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
732                         snd_leave_user(fs);
733                 } else {
734                         ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
735                 }
736                 if (ret != -EPIPE && ret != -ESTRPIPE)
737                         break;
738
739                 /* test, if we can't store new data, because the stream */
740                 /* has not been started */
741                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
742                         return -EAGAIN;
743         }
744         return ret;
745 }
746         
747 snd_pcm_sframes_t snd_pcm_oss_readv3(snd_pcm_substream_t *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
748 {
749         snd_pcm_runtime_t *runtime = substream->runtime;
750         int ret;
751         while (1) {
752                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
753                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
754 #ifdef OSS_DEBUG
755                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
756                                 printk("pcm_oss: readv: recovering from XRUN\n");
757                         else
758                                 printk("pcm_oss: readv: recovering from SUSPEND\n");
759 #endif
760                         ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
761                         if (ret < 0)
762                                 break;
763                 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
764                         ret = snd_pcm_oss_prepare(substream);
765                         if (ret < 0)
766                                 break;
767                 }
768                 if (in_kernel) {
769                         mm_segment_t fs;
770                         fs = snd_enter_user();
771                         ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
772                         snd_leave_user(fs);
773                 } else {
774                         ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
775                 }
776                 if (ret != -EPIPE && ret != -ESTRPIPE)
777                         break;
778         }
779         return ret;
780 }
781
782 static ssize_t snd_pcm_oss_write2(snd_pcm_substream_t *substream, const char *buf, size_t bytes, int in_kernel)
783 {
784         snd_pcm_runtime_t *runtime = substream->runtime;
785         snd_pcm_sframes_t frames, frames1;
786         if (runtime->oss.plugin_first) {
787                 snd_pcm_plugin_channel_t *channels;
788                 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
789                 if (!in_kernel) {
790                         if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
791                                 return -EFAULT;
792                         buf = runtime->oss.buffer;
793                 }
794                 frames = bytes / oss_frame_bytes;
795                 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
796                 if (frames1 < 0)
797                         return frames1;
798                 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
799                 if (frames1 <= 0)
800                         return frames1;
801                 bytes = frames1 * oss_frame_bytes;
802         } else {
803                 frames = bytes_to_frames(runtime, bytes);
804                 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
805                 if (frames1 <= 0)
806                         return frames1;
807                 bytes = frames_to_bytes(runtime, frames1);
808         }
809         return bytes;
810 }
811
812 static ssize_t snd_pcm_oss_write1(snd_pcm_substream_t *substream, const char __user *buf, size_t bytes)
813 {
814         size_t xfer = 0;
815         ssize_t tmp;
816         snd_pcm_runtime_t *runtime = substream->runtime;
817
818         if (atomic_read(&runtime->mmap_count))
819                 return -ENXIO;
820
821         if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
822                 return tmp;
823         while (bytes > 0) {
824                 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
825                         tmp = bytes;
826                         if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
827                                 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
828                         if (tmp > 0) {
829                                 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
830                                         return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
831                         }
832                         runtime->oss.buffer_used += tmp;
833                         buf += tmp;
834                         bytes -= tmp;
835                         xfer += tmp;
836                         if ((substream->oss.setup != NULL && substream->oss.setup->partialfrag) ||
837                             runtime->oss.buffer_used == runtime->oss.period_bytes) {
838                                 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr, 
839                                                          runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
840                                 if (tmp <= 0)
841                                         return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
842                                 runtime->oss.bytes += tmp;
843                                 runtime->oss.period_ptr += tmp;
844                                 runtime->oss.period_ptr %= runtime->oss.period_bytes;
845                                 if (runtime->oss.period_ptr == 0 ||
846                                     runtime->oss.period_ptr == runtime->oss.buffer_used)
847                                         runtime->oss.buffer_used = 0;
848                                 else if ((substream->ffile->f_flags & O_NONBLOCK) != 0)
849                                         return xfer > 0 ? xfer : -EAGAIN;
850                         }
851                 } else {
852                         tmp = snd_pcm_oss_write2(substream, (const char *)buf, runtime->oss.period_bytes, 0);
853                         if (tmp <= 0)
854                                 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
855                         runtime->oss.bytes += tmp;
856                         buf += tmp;
857                         bytes -= tmp;
858                         xfer += tmp;
859                         if ((substream->ffile->f_flags & O_NONBLOCK) != 0 &&
860                             tmp != runtime->oss.period_bytes)
861                                 break;
862                 }
863         }
864         return xfer;
865 }
866
867 static ssize_t snd_pcm_oss_read2(snd_pcm_substream_t *substream, char *buf, size_t bytes, int in_kernel)
868 {
869         snd_pcm_runtime_t *runtime = substream->runtime;
870         snd_pcm_sframes_t frames, frames1;
871         char __user *final_dst = (char __user *)buf;
872         if (runtime->oss.plugin_first) {
873                 snd_pcm_plugin_channel_t *channels;
874                 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
875                 if (!in_kernel)
876                         buf = runtime->oss.buffer;
877                 frames = bytes / oss_frame_bytes;
878                 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
879                 if (frames1 < 0)
880                         return frames1;
881                 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
882                 if (frames1 <= 0)
883                         return frames1;
884                 bytes = frames1 * oss_frame_bytes;
885                 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
886                         return -EFAULT;
887         } else {
888                 frames = bytes_to_frames(runtime, bytes);
889                 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
890                 if (frames1 <= 0)
891                         return frames1;
892                 bytes = frames_to_bytes(runtime, frames1);
893         }
894         return bytes;
895 }
896
897 static ssize_t snd_pcm_oss_read1(snd_pcm_substream_t *substream, char __user *buf, size_t bytes)
898 {
899         size_t xfer = 0;
900         ssize_t tmp;
901         snd_pcm_runtime_t *runtime = substream->runtime;
902
903         if (atomic_read(&runtime->mmap_count))
904                 return -ENXIO;
905
906         if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
907                 return tmp;
908         while (bytes > 0) {
909                 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
910                         if (runtime->oss.buffer_used == 0) {
911                                 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
912                                 if (tmp <= 0)
913                                         return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
914                                 runtime->oss.bytes += tmp;
915                                 runtime->oss.period_ptr = tmp;
916                                 runtime->oss.buffer_used = tmp;
917                         }
918                         tmp = bytes;
919                         if ((size_t) tmp > runtime->oss.buffer_used)
920                                 tmp = runtime->oss.buffer_used;
921                         if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
922                                 return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
923                         buf += tmp;
924                         bytes -= tmp;
925                         xfer += tmp;
926                         runtime->oss.buffer_used -= tmp;
927                 } else {
928                         tmp = snd_pcm_oss_read2(substream, (char *)buf, runtime->oss.period_bytes, 0);
929                         if (tmp <= 0)
930                                 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
931                         runtime->oss.bytes += tmp;
932                         buf += tmp;
933                         bytes -= tmp;
934                         xfer += tmp;
935                 }
936         }
937         return xfer;
938 }
939
940 static int snd_pcm_oss_reset(snd_pcm_oss_file_t *pcm_oss_file)
941 {
942         snd_pcm_substream_t *substream;
943
944         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
945         if (substream != NULL) {
946                 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
947                 substream->runtime->oss.prepare = 1;
948         }
949         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
950         if (substream != NULL) {
951                 snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
952                 substream->runtime->oss.prepare = 1;
953         }
954         return 0;
955 }
956
957 static int snd_pcm_oss_post(snd_pcm_oss_file_t *pcm_oss_file)
958 {
959         snd_pcm_substream_t *substream;
960         int err;
961
962         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
963         if (substream != NULL) {
964                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
965                         return err;
966                 snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
967         }
968         /* note: all errors from the start action are ignored */
969         /* OSS apps do not know, how to handle them */
970         return 0;
971 }
972
973 static int snd_pcm_oss_sync1(snd_pcm_substream_t *substream, size_t size)
974 {
975         snd_pcm_runtime_t *runtime;
976         ssize_t result = 0;
977         long res;
978         wait_queue_t wait;
979
980         runtime = substream->runtime;
981         init_waitqueue_entry(&wait, current);
982         add_wait_queue(&runtime->sleep, &wait);
983 #ifdef OSS_DEBUG
984         printk("sync1: size = %li\n", size);
985 #endif
986         while (1) {
987                 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
988                 if (result > 0) {
989                         runtime->oss.buffer_used = 0;
990                         result = 0;
991                         break;
992                 }
993                 if (result != 0 && result != -EAGAIN)
994                         break;
995                 result = 0;
996                 set_current_state(TASK_INTERRUPTIBLE);
997                 snd_pcm_stream_lock_irq(substream);
998                 res = runtime->status->state;
999                 snd_pcm_stream_unlock_irq(substream);
1000                 if (res != SNDRV_PCM_STATE_RUNNING) {
1001                         set_current_state(TASK_RUNNING);
1002                         break;
1003                 }
1004                 res = schedule_timeout(10 * HZ);
1005                 if (signal_pending(current)) {
1006                         result = -ERESTARTSYS;
1007                         break;
1008                 }
1009                 if (res == 0) {
1010                         snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1011                         result = -EIO;
1012                         break;
1013                 }
1014         }
1015         remove_wait_queue(&runtime->sleep, &wait);
1016         return result;
1017 }
1018
1019 static int snd_pcm_oss_sync(snd_pcm_oss_file_t *pcm_oss_file)
1020 {
1021         int err = 0;
1022         unsigned int saved_f_flags;
1023         snd_pcm_substream_t *substream;
1024         snd_pcm_runtime_t *runtime;
1025         snd_pcm_format_t format;
1026         unsigned long width;
1027         size_t size;
1028
1029         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1030         if (substream != NULL) {
1031                 runtime = substream->runtime;
1032                 if (atomic_read(&runtime->mmap_count))
1033                         goto __direct;
1034                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1035                         return err;
1036                 format = snd_pcm_oss_format_from(runtime->oss.format);
1037                 width = snd_pcm_format_physical_width(format);
1038                 if (runtime->oss.buffer_used > 0) {
1039 #ifdef OSS_DEBUG
1040                         printk("sync: buffer_used\n");
1041 #endif
1042                         size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1043                         snd_pcm_format_set_silence(format,
1044                                                    runtime->oss.buffer + runtime->oss.buffer_used,
1045                                                    size);
1046                         err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1047                         if (err < 0)
1048                                 return err;
1049                 } else if (runtime->oss.period_ptr > 0) {
1050 #ifdef OSS_DEBUG
1051                         printk("sync: period_ptr\n");
1052 #endif
1053                         size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1054                         snd_pcm_format_set_silence(format,
1055                                                    runtime->oss.buffer,
1056                                                    size * 8 / width);
1057                         err = snd_pcm_oss_sync1(substream, size);
1058                         if (err < 0)
1059                                 return err;
1060                 }
1061                 /*
1062                  * The ALSA's period might be a bit large than OSS one.
1063                  * Fill the remain portion of ALSA period with zeros.
1064                  */
1065                 size = runtime->control->appl_ptr % runtime->period_size;
1066                 if (size > 0) {
1067                         size = runtime->period_size - size;
1068                         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1069                                 size = (runtime->frame_bits * size) / 8;
1070                                 while (size > 0) {
1071                                         mm_segment_t fs;
1072                                         size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1073                                         size -= size1;
1074                                         size1 *= 8;
1075                                         size1 /= runtime->sample_bits;
1076                                         snd_pcm_format_set_silence(runtime->format,
1077                                                                    runtime->oss.buffer,
1078                                                                    size1);
1079                                         fs = snd_enter_user();
1080                                         snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1081                                         snd_leave_user(fs);
1082                                 }
1083                         } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1084                                 void __user *buffers[runtime->channels];
1085                                 memset(buffers, 0, runtime->channels * sizeof(void *));
1086                                 snd_pcm_lib_writev(substream, buffers, size);
1087                         }
1088                 }
1089                 /*
1090                  * finish sync: drain the buffer
1091                  */
1092               __direct:
1093                 saved_f_flags = substream->ffile->f_flags;
1094                 substream->ffile->f_flags &= ~O_NONBLOCK;
1095                 err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1096                 substream->ffile->f_flags = saved_f_flags;
1097                 if (err < 0)
1098                         return err;
1099                 runtime->oss.prepare = 1;
1100         }
1101
1102         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1103         if (substream != NULL) {
1104                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1105                         return err;
1106                 runtime = substream->runtime;
1107                 err = snd_pcm_kernel_capture_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1108                 if (err < 0)
1109                         return err;
1110                 runtime->oss.buffer_used = 0;
1111                 runtime->oss.prepare = 1;
1112         }
1113         return 0;
1114 }
1115
1116 static int snd_pcm_oss_set_rate(snd_pcm_oss_file_t *pcm_oss_file, int rate)
1117 {
1118         int idx;
1119
1120         for (idx = 1; idx >= 0; --idx) {
1121                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1122                 snd_pcm_runtime_t *runtime;
1123                 if (substream == NULL)
1124                         continue;
1125                 runtime = substream->runtime;
1126                 if (rate < 1000)
1127                         rate = 1000;
1128                 else if (rate > 192000)
1129                         rate = 192000;
1130                 if (runtime->oss.rate != rate) {
1131                         runtime->oss.params = 1;
1132                         runtime->oss.rate = rate;
1133                 }
1134         }
1135         return snd_pcm_oss_get_rate(pcm_oss_file);
1136 }
1137
1138 static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file)
1139 {
1140         snd_pcm_substream_t *substream;
1141         int err;
1142         
1143         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1144                 return err;
1145         return substream->runtime->oss.rate;
1146 }
1147
1148 static int snd_pcm_oss_set_channels(snd_pcm_oss_file_t *pcm_oss_file, unsigned int channels)
1149 {
1150         int idx;
1151         if (channels < 1)
1152                 channels = 1;
1153         if (channels > 128)
1154                 return -EINVAL;
1155         for (idx = 1; idx >= 0; --idx) {
1156                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1157                 snd_pcm_runtime_t *runtime;
1158                 if (substream == NULL)
1159                         continue;
1160                 runtime = substream->runtime;
1161                 if (runtime->oss.channels != channels) {
1162                         runtime->oss.params = 1;
1163                         runtime->oss.channels = channels;
1164                 }
1165         }
1166         return snd_pcm_oss_get_channels(pcm_oss_file);
1167 }
1168
1169 static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file)
1170 {
1171         snd_pcm_substream_t *substream;
1172         int err;
1173         
1174         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1175                 return err;
1176         return substream->runtime->oss.channels;
1177 }
1178
1179 static int snd_pcm_oss_get_block_size(snd_pcm_oss_file_t *pcm_oss_file)
1180 {
1181         snd_pcm_substream_t *substream;
1182         int err;
1183         
1184         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1185                 return err;
1186         return substream->runtime->oss.period_bytes;
1187 }
1188
1189 static int snd_pcm_oss_get_formats(snd_pcm_oss_file_t *pcm_oss_file)
1190 {
1191         snd_pcm_substream_t *substream;
1192         int err;
1193         int direct;
1194         snd_pcm_hw_params_t *params;
1195         unsigned int formats = 0;
1196         snd_mask_t format_mask;
1197         int fmt;
1198
1199         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1200                 return err;
1201         if (atomic_read(&substream->runtime->mmap_count)) {
1202                 direct = 1;
1203         } else {
1204                 snd_pcm_oss_setup_t *setup = substream->oss.setup;
1205                 direct = (setup != NULL && setup->direct);
1206         }
1207         if (!direct)
1208                 return AFMT_MU_LAW | AFMT_U8 |
1209                        AFMT_S16_LE | AFMT_S16_BE |
1210                        AFMT_S8 | AFMT_U16_LE |
1211                        AFMT_U16_BE;
1212         params = kmalloc(sizeof(*params), GFP_KERNEL);
1213         if (!params)
1214                 return -ENOMEM;
1215         _snd_pcm_hw_params_any(params);
1216         err = snd_pcm_hw_refine(substream, params);
1217         format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 
1218         kfree(params);
1219         snd_assert(err >= 0, return err);
1220         for (fmt = 0; fmt < 32; ++fmt) {
1221                 if (snd_mask_test(&format_mask, fmt)) {
1222                         int f = snd_pcm_oss_format_to(fmt);
1223                         if (f >= 0)
1224                                 formats |= f;
1225                 }
1226         }
1227         return formats;
1228 }
1229
1230 static int snd_pcm_oss_set_format(snd_pcm_oss_file_t *pcm_oss_file, int format)
1231 {
1232         int formats, idx;
1233         
1234         if (format != AFMT_QUERY) {
1235                 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1236                 if (!(formats & format))
1237                         format = AFMT_U8;
1238                 for (idx = 1; idx >= 0; --idx) {
1239                         snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1240                         snd_pcm_runtime_t *runtime;
1241                         if (substream == NULL)
1242                                 continue;
1243                         runtime = substream->runtime;
1244                         if (runtime->oss.format != format) {
1245                                 runtime->oss.params = 1;
1246                                 runtime->oss.format = format;
1247                         }
1248                 }
1249         }
1250         return snd_pcm_oss_get_format(pcm_oss_file);
1251 }
1252
1253 static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file)
1254 {
1255         snd_pcm_substream_t *substream;
1256         int err;
1257         
1258         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1259                 return err;
1260         return substream->runtime->oss.format;
1261 }
1262
1263 static int snd_pcm_oss_set_subdivide1(snd_pcm_substream_t *substream, int subdivide)
1264 {
1265         snd_pcm_runtime_t *runtime;
1266
1267         if (substream == NULL)
1268                 return 0;
1269         runtime = substream->runtime;
1270         if (subdivide == 0) {
1271                 subdivide = runtime->oss.subdivision;
1272                 if (subdivide == 0)
1273                         subdivide = 1;
1274                 return subdivide;
1275         }
1276         if (runtime->oss.subdivision || runtime->oss.fragshift)
1277                 return -EINVAL;
1278         if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1279             subdivide != 8 && subdivide != 16)
1280                 return -EINVAL;
1281         runtime->oss.subdivision = subdivide;
1282         runtime->oss.params = 1;
1283         return subdivide;
1284 }
1285
1286 static int snd_pcm_oss_set_subdivide(snd_pcm_oss_file_t *pcm_oss_file, int subdivide)
1287 {
1288         int err = -EINVAL, idx;
1289
1290         for (idx = 1; idx >= 0; --idx) {
1291                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1292                 if (substream == NULL)
1293                         continue;
1294                 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1295                         return err;
1296         }
1297         return err;
1298 }
1299
1300 static int snd_pcm_oss_set_fragment1(snd_pcm_substream_t *substream, unsigned int val)
1301 {
1302         snd_pcm_runtime_t *runtime;
1303
1304         if (substream == NULL)
1305                 return 0;
1306         runtime = substream->runtime;
1307         if (runtime->oss.subdivision || runtime->oss.fragshift)
1308                 return -EINVAL;
1309         runtime->oss.fragshift = val & 0xffff;
1310         runtime->oss.maxfrags = (val >> 16) & 0xffff;
1311         if (runtime->oss.fragshift < 4)         /* < 16 */
1312                 runtime->oss.fragshift = 4;
1313         if (runtime->oss.maxfrags < 2)
1314                 runtime->oss.maxfrags = 2;
1315         runtime->oss.params = 1;
1316         return 0;
1317 }
1318
1319 static int snd_pcm_oss_set_fragment(snd_pcm_oss_file_t *pcm_oss_file, unsigned int val)
1320 {
1321         int err = -EINVAL, idx;
1322
1323         for (idx = 1; idx >= 0; --idx) {
1324                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1325                 if (substream == NULL)
1326                         continue;
1327                 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1328                         return err;
1329         }
1330         return err;
1331 }
1332
1333 static int snd_pcm_oss_nonblock(struct file * file)
1334 {
1335         file->f_flags |= O_NONBLOCK;
1336         return 0;
1337 }
1338
1339 static int snd_pcm_oss_get_caps1(snd_pcm_substream_t *substream, int res)
1340 {
1341
1342         if (substream == NULL) {
1343                 res &= ~DSP_CAP_DUPLEX;
1344                 return res;
1345         }
1346 #ifdef DSP_CAP_MULTI
1347         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1348                 if (substream->pstr->substream_count > 1)
1349                         res |= DSP_CAP_MULTI;
1350 #endif
1351         /* DSP_CAP_REALTIME is set all times: */
1352         /* all ALSA drivers can return actual pointer in ring buffer */
1353 #if defined(DSP_CAP_REALTIME) && 0
1354         {
1355                 snd_pcm_runtime_t *runtime = substream->runtime;
1356                 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1357                         res &= ~DSP_CAP_REALTIME;
1358         }
1359 #endif
1360         return res;
1361 }
1362
1363 static int snd_pcm_oss_get_caps(snd_pcm_oss_file_t *pcm_oss_file)
1364 {
1365         int result, idx;
1366         
1367         result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1368         for (idx = 0; idx < 2; idx++) {
1369                 snd_pcm_substream_t *substream = pcm_oss_file->streams[idx];
1370                 result = snd_pcm_oss_get_caps1(substream, result);
1371         }
1372         result |= 0x0001;       /* revision - same as SB AWE 64 */
1373         return result;
1374 }
1375
1376 static void snd_pcm_oss_simulate_fill(snd_pcm_substream_t *substream, snd_pcm_uframes_t hw_ptr)
1377 {
1378         snd_pcm_runtime_t *runtime = substream->runtime;
1379         snd_pcm_uframes_t appl_ptr;
1380         appl_ptr = hw_ptr + runtime->buffer_size;
1381         appl_ptr %= runtime->boundary;
1382         runtime->control->appl_ptr = appl_ptr;
1383 }
1384
1385 static int snd_pcm_oss_set_trigger(snd_pcm_oss_file_t *pcm_oss_file, int trigger)
1386 {
1387         snd_pcm_runtime_t *runtime;
1388         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1389         int err, cmd;
1390
1391 #ifdef OSS_DEBUG
1392         printk("pcm_oss: trigger = 0x%x\n", trigger);
1393 #endif
1394         
1395         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1396         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1397
1398         if (psubstream) {
1399                 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1400                         return err;
1401         }
1402         if (csubstream) {
1403                 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1404                         return err;
1405         }
1406         if (psubstream) {
1407                 runtime = psubstream->runtime;
1408                 if (trigger & PCM_ENABLE_OUTPUT) {
1409                         if (runtime->oss.trigger)
1410                                 goto _skip1;
1411                         if (atomic_read(&psubstream->runtime->mmap_count))
1412                                 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1413                         runtime->oss.trigger = 1;
1414                         runtime->start_threshold = 1;
1415                         cmd = SNDRV_PCM_IOCTL_START;
1416                 } else {
1417                         if (!runtime->oss.trigger)
1418                                 goto _skip1;
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_playback_ioctl(psubstream, cmd, NULL);
1425                 if (err < 0)
1426                         return err;
1427         }
1428  _skip1:
1429         if (csubstream) {
1430                 runtime = csubstream->runtime;
1431                 if (trigger & PCM_ENABLE_INPUT) {
1432                         if (runtime->oss.trigger)
1433                                 goto _skip2;
1434                         runtime->oss.trigger = 1;
1435                         runtime->start_threshold = 1;
1436                         cmd = SNDRV_PCM_IOCTL_START;
1437                 } else {
1438                         if (!runtime->oss.trigger)
1439                                 goto _skip2;
1440                         runtime->oss.trigger = 0;
1441                         runtime->start_threshold = runtime->boundary;
1442                         cmd = SNDRV_PCM_IOCTL_DROP;
1443                         runtime->oss.prepare = 1;
1444                 }
1445                 err = snd_pcm_kernel_capture_ioctl(csubstream, cmd, NULL);
1446                 if (err < 0)
1447                         return err;
1448         }
1449  _skip2:
1450         return 0;
1451 }
1452
1453 static int snd_pcm_oss_get_trigger(snd_pcm_oss_file_t *pcm_oss_file)
1454 {
1455         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1456         int result = 0;
1457
1458         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1460         if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1461                 result |= PCM_ENABLE_OUTPUT;
1462         if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1463                 result |= PCM_ENABLE_INPUT;
1464         return result;
1465 }
1466
1467 static int snd_pcm_oss_get_odelay(snd_pcm_oss_file_t *pcm_oss_file)
1468 {
1469         snd_pcm_substream_t *substream;
1470         snd_pcm_runtime_t *runtime;
1471         snd_pcm_sframes_t delay;
1472         int err;
1473
1474         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1475         if (substream == NULL)
1476                 return -EINVAL;
1477         if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1478                 return err;
1479         runtime = substream->runtime;
1480         if (runtime->oss.params || runtime->oss.prepare)
1481                 return 0;
1482         err = snd_pcm_kernel_playback_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1483         if (err == -EPIPE)
1484                 delay = 0;      /* hack for broken OSS applications */
1485         else if (err < 0)
1486                 return err;
1487         return snd_pcm_oss_bytes(substream, delay);
1488 }
1489
1490 static int snd_pcm_oss_get_ptr(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct count_info __user * _info)
1491 {       
1492         snd_pcm_substream_t *substream;
1493         snd_pcm_runtime_t *runtime;
1494         snd_pcm_sframes_t delay;
1495         int fixup;
1496         struct count_info info;
1497         int err;
1498
1499         if (_info == NULL)
1500                 return -EFAULT;
1501         substream = pcm_oss_file->streams[stream];
1502         if (substream == NULL)
1503                 return -EINVAL;
1504         if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1505                 return err;
1506         runtime = substream->runtime;
1507         if (runtime->oss.params || runtime->oss.prepare) {
1508                 memset(&info, 0, sizeof(info));
1509                 if (copy_to_user(_info, &info, sizeof(info)))
1510                         return -EFAULT;
1511                 return 0;
1512         }
1513         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1514                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
1515                 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
1516                         err = 0;
1517                         delay = 0;
1518                         fixup = 0;
1519                 } else {
1520                         fixup = runtime->oss.buffer_used;
1521                 }
1522         } else {
1523                 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
1524                 fixup = -runtime->oss.buffer_used;
1525         }
1526         if (err < 0)
1527                 return err;
1528         info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
1529         if (atomic_read(&runtime->mmap_count)) {
1530                 snd_pcm_sframes_t n;
1531                 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
1532                 if (n < 0)
1533                         n += runtime->boundary;
1534                 info.blocks = n / runtime->period_size;
1535                 runtime->oss.prev_hw_ptr_interrupt = delay;
1536                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1537                         snd_pcm_oss_simulate_fill(substream, delay);
1538                 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
1539         } else {
1540                 delay = snd_pcm_oss_bytes(substream, delay) + fixup;
1541                 info.blocks = delay / runtime->oss.period_bytes;
1542                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1543                         if (runtime->oss.bytes == 0)
1544                                 delay = 0;
1545                         info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1546                 } else {
1547                         info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1548                 }
1549         }
1550         if (copy_to_user(_info, &info, sizeof(info)))
1551                 return -EFAULT;
1552         return 0;
1553 }
1554
1555 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1556 {
1557         snd_pcm_substream_t *substream;
1558         snd_pcm_runtime_t *runtime;
1559         snd_pcm_sframes_t avail;
1560         int fixup;
1561         struct audio_buf_info info;
1562         int err;
1563
1564         if (_info == NULL)
1565                 return -EFAULT;
1566         substream = pcm_oss_file->streams[stream];
1567         if (substream == NULL)
1568                 return -EINVAL;
1569         runtime = substream->runtime;
1570
1571         if (runtime->oss.params &&
1572             (err = snd_pcm_oss_change_params(substream)) < 0)
1573                 return err;
1574
1575         info.fragsize = runtime->oss.period_bytes;
1576         info.fragstotal = runtime->periods;
1577         if (runtime->oss.prepare) {
1578                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1579                         info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1580                         info.fragments = runtime->oss.periods;
1581                 } else {
1582                         info.bytes = 0;
1583                         info.fragments = 0;
1584                 }
1585         } else {
1586                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1587                         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1588                         if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1589                                 avail = runtime->buffer_size;
1590                                 err = 0;
1591                                 fixup = 0;
1592                         } else {
1593                                 avail = runtime->buffer_size - avail;
1594                                 fixup = -runtime->oss.buffer_used;
1595                         }
1596                 } else {
1597                         err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1598                         fixup = runtime->oss.buffer_used;
1599                 }
1600                 if (err < 0)
1601                         return err;
1602                 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1603                 info.fragments = info.bytes / runtime->oss.period_bytes;
1604         }
1605
1606 #ifdef OSS_DEBUG
1607         printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1608 #endif
1609         if (copy_to_user(_info, &info, sizeof(info)))
1610                 return -EFAULT;
1611         return 0;
1612 }
1613
1614 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1615 {
1616         // it won't be probably implemented
1617         // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1618         return -EINVAL;
1619 }
1620
1621 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1622 {
1623         const char *ptr, *ptrl;
1624         snd_pcm_oss_setup_t *setup;
1625
1626         down(&pcm->streams[stream].oss.setup_mutex);
1627         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1628                 if (!strcmp(setup->task_name, task_name)) {
1629                         up(&pcm->streams[stream].oss.setup_mutex);
1630                         return setup;
1631                 }
1632         }
1633         ptr = ptrl = task_name;
1634         while (*ptr) {
1635                 if (*ptr == '/')
1636                         ptrl = ptr + 1;
1637                 ptr++;
1638         }
1639         if (ptrl == task_name) {
1640                 goto __not_found;
1641                 return NULL;
1642         }
1643         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1644                 if (!strcmp(setup->task_name, ptrl)) {
1645                         up(&pcm->streams[stream].oss.setup_mutex);
1646                         return setup;
1647                 }
1648         }
1649       __not_found:
1650         up(&pcm->streams[stream].oss.setup_mutex);
1651         return NULL;
1652 }
1653
1654 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1655                                        snd_pcm_oss_setup_t *setup,
1656                                        int minor)
1657 {
1658         snd_pcm_runtime_t *runtime;
1659
1660         substream->oss.oss = 1;
1661         substream->oss.setup = setup;
1662         runtime = substream->runtime;
1663         runtime->oss.params = 1;
1664         runtime->oss.trigger = 1;
1665         runtime->oss.rate = 8000;
1666         switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1667         case SNDRV_MINOR_OSS_PCM_8:
1668                 runtime->oss.format = AFMT_U8;
1669                 break;
1670         case SNDRV_MINOR_OSS_PCM_16:
1671                 runtime->oss.format = AFMT_S16_LE;
1672                 break;
1673         default:
1674                 runtime->oss.format = AFMT_MU_LAW;
1675         }
1676         runtime->oss.channels = 1;
1677         runtime->oss.fragshift = 0;
1678         runtime->oss.maxfrags = 0;
1679         runtime->oss.subdivision = 0;
1680 }
1681
1682 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1683 {
1684         snd_pcm_runtime_t *runtime;
1685         runtime = substream->runtime;
1686         vfree(runtime->oss.buffer);
1687         snd_pcm_oss_plugin_clear(substream);
1688         substream->oss.file = NULL;
1689         substream->oss.oss = 0;
1690 }
1691
1692 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1693 {
1694         int cidx;
1695         snd_assert(pcm_oss_file != NULL, return -ENXIO);
1696         for (cidx = 0; cidx < 2; ++cidx) {
1697                 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1698                 snd_pcm_runtime_t *runtime;
1699                 if (substream == NULL)
1700                         continue;
1701                 runtime = substream->runtime;
1702                 
1703                 snd_pcm_stream_lock_irq(substream);
1704                 if (snd_pcm_running(substream))
1705                         snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1706                 snd_pcm_stream_unlock_irq(substream);
1707                 if (substream->open_flag) {
1708                         if (substream->ops->hw_free != NULL)
1709                                 substream->ops->hw_free(substream);
1710                         substream->ops->close(substream);
1711                         substream->open_flag = 0;
1712                 }
1713                 substream->ffile = NULL;
1714                 snd_pcm_oss_release_substream(substream);
1715                 snd_pcm_release_substream(substream);
1716         }
1717         kfree(pcm_oss_file);
1718         return 0;
1719 }
1720
1721 static int snd_pcm_oss_open_file(struct file *file,
1722                                  snd_pcm_t *pcm,
1723                                  snd_pcm_oss_file_t **rpcm_oss_file,
1724                                  int minor,
1725                                  snd_pcm_oss_setup_t *psetup,
1726                                  snd_pcm_oss_setup_t *csetup)
1727 {
1728         int err = 0;
1729         snd_pcm_oss_file_t *pcm_oss_file;
1730         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1731         unsigned int f_mode = file->f_mode;
1732
1733         snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1734         *rpcm_oss_file = NULL;
1735
1736         pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1737         if (pcm_oss_file == NULL)
1738                 return -ENOMEM;
1739
1740         if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1741             (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1742                 f_mode = FMODE_WRITE;
1743         if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1744                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1745                                                &psubstream)) < 0) {
1746                         snd_pcm_oss_release_file(pcm_oss_file);
1747                         return err;
1748                 }
1749                 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1750         }
1751         if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1752                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE, 
1753                                                &csubstream)) < 0) {
1754                         if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1755                                 snd_pcm_oss_release_file(pcm_oss_file);
1756                                 return err;
1757                         } else {
1758                                 csubstream = NULL;
1759                         }
1760                 }
1761                 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1762         }
1763         
1764         if (psubstream == NULL && csubstream == NULL) {
1765                 snd_pcm_oss_release_file(pcm_oss_file);
1766                 return -EINVAL;
1767         }
1768         if (psubstream != NULL) {
1769                 psubstream->oss.file = pcm_oss_file;
1770                 err = snd_pcm_hw_constraints_init(psubstream);
1771                 if (err < 0) {
1772                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1773                         snd_pcm_oss_release_file(pcm_oss_file);
1774                         return err;
1775                 }
1776                 if ((err = psubstream->ops->open(psubstream)) < 0) {
1777                         snd_pcm_oss_release_file(pcm_oss_file);
1778                         return err;
1779                 }
1780                 psubstream->open_flag = 1;
1781                 err = snd_pcm_hw_constraints_complete(psubstream);
1782                 if (err < 0) {
1783                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1784                         snd_pcm_oss_release_file(pcm_oss_file);
1785                         return err;
1786                 }
1787                 psubstream->ffile = file;
1788                 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1789         }
1790         if (csubstream != NULL) {
1791                 csubstream->oss.file = pcm_oss_file;
1792                 err = snd_pcm_hw_constraints_init(csubstream);
1793                 if (err < 0) {
1794                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1795                         snd_pcm_oss_release_file(pcm_oss_file);
1796                         return err;
1797                 }
1798                 if ((err = csubstream->ops->open(csubstream)) < 0) {
1799                         snd_pcm_oss_release_file(pcm_oss_file);
1800                         return err;
1801                 }
1802                 csubstream->open_flag = 1;
1803                 err = snd_pcm_hw_constraints_complete(csubstream);
1804                 if (err < 0) {
1805                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1806                         snd_pcm_oss_release_file(pcm_oss_file);
1807                         return err;
1808                 }
1809                 csubstream->ffile = file;
1810                 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1811         }
1812
1813         file->private_data = pcm_oss_file;
1814         *rpcm_oss_file = pcm_oss_file;
1815         return 0;
1816 }
1817
1818
1819 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1820 {
1821         int minor = iminor(inode);
1822         int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1823         int device;
1824         int err;
1825         char task_name[32];
1826         snd_pcm_t *pcm;
1827         snd_pcm_oss_file_t *pcm_oss_file;
1828         snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1829         int nonblock;
1830         wait_queue_t wait;
1831
1832         snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1833         device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1834                 adsp_map[cardnum] : dsp_map[cardnum];
1835
1836         pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1837         if (pcm == NULL) {
1838                 err = -ENODEV;
1839                 goto __error1;
1840         }
1841         err = snd_card_file_add(pcm->card, file);
1842         if (err < 0)
1843                 goto __error1;
1844         if (!try_module_get(pcm->card->module)) {
1845                 err = -EFAULT;
1846                 goto __error2;
1847         }
1848         if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1849                 err = -EFAULT;
1850                 goto __error;
1851         }
1852         if (file->f_mode & FMODE_WRITE)
1853                 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1854         if (file->f_mode & FMODE_READ)
1855                 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1856
1857         nonblock = !!(file->f_flags & O_NONBLOCK);
1858         if (psetup && !psetup->disable) {
1859                 if (psetup->nonblock)
1860                         nonblock = 1;
1861                 else if (psetup->block)
1862                         nonblock = 0;
1863         } else if (csetup && !csetup->disable) {
1864                 if (csetup->nonblock)
1865                         nonblock = 1;
1866                 else if (csetup->block)
1867                         nonblock = 0;
1868         }
1869         if (!nonblock)
1870                 nonblock = nonblock_open;
1871
1872         init_waitqueue_entry(&wait, current);
1873         add_wait_queue(&pcm->open_wait, &wait);
1874         down(&pcm->open_mutex);
1875         while (1) {
1876                 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1877                                             minor, psetup, csetup);
1878                 if (err >= 0)
1879                         break;
1880                 if (err == -EAGAIN) {
1881                         if (nonblock) {
1882                                 err = -EBUSY;
1883                                 break;
1884                         }
1885                 } else
1886                         break;
1887                 set_current_state(TASK_INTERRUPTIBLE);
1888                 up(&pcm->open_mutex);
1889                 schedule();
1890                 down(&pcm->open_mutex);
1891                 if (signal_pending(current)) {
1892                         err = -ERESTARTSYS;
1893                         break;
1894                 }
1895         }
1896         remove_wait_queue(&pcm->open_wait, &wait);
1897         up(&pcm->open_mutex);
1898         if (err < 0)
1899                 goto __error;
1900         return err;
1901
1902       __error:
1903         module_put(pcm->card->module);
1904       __error2:
1905         snd_card_file_remove(pcm->card, file);
1906       __error1:
1907         return err;
1908 }
1909
1910 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1911 {
1912         snd_pcm_t *pcm;
1913         snd_pcm_substream_t *substream;
1914         snd_pcm_oss_file_t *pcm_oss_file;
1915
1916         pcm_oss_file = file->private_data;
1917         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1918         if (substream == NULL)
1919                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1920         snd_assert(substream != NULL, return -ENXIO);
1921         pcm = substream->pcm;
1922         snd_pcm_oss_sync(pcm_oss_file);
1923         down(&pcm->open_mutex);
1924         snd_pcm_oss_release_file(pcm_oss_file);
1925         up(&pcm->open_mutex);
1926         wake_up(&pcm->open_wait);
1927         module_put(pcm->card->module);
1928         snd_card_file_remove(pcm->card, file);
1929         return 0;
1930 }
1931
1932 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1933 {
1934         snd_pcm_oss_file_t *pcm_oss_file;
1935         int __user *p = (int __user *)arg;
1936         int res;
1937
1938         pcm_oss_file = file->private_data;
1939         if (cmd == OSS_GETVERSION)
1940                 return put_user(SNDRV_OSS_VERSION, p);
1941         if (cmd == OSS_ALSAEMULVER)
1942                 return put_user(1, p);
1943 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1944         if (((cmd >> 8) & 0xff) == 'M') {       /* mixer ioctl - for OSS compatibility */
1945                 snd_pcm_substream_t *substream;
1946                 int idx;
1947                 for (idx = 0; idx < 2; ++idx) {
1948                         substream = pcm_oss_file->streams[idx];
1949                         if (substream != NULL)
1950                                 break;
1951                 }
1952                 snd_assert(substream != NULL, return -ENXIO);
1953                 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1954         }
1955 #endif
1956         if (((cmd >> 8) & 0xff) != 'P')
1957                 return -EINVAL;
1958 #ifdef OSS_DEBUG
1959         printk("pcm_oss: ioctl = 0x%x\n", cmd);
1960 #endif
1961         switch (cmd) {
1962         case SNDCTL_DSP_RESET:
1963                 return snd_pcm_oss_reset(pcm_oss_file);
1964         case SNDCTL_DSP_SYNC:
1965                 return snd_pcm_oss_sync(pcm_oss_file);
1966         case SNDCTL_DSP_SPEED:
1967                 if (get_user(res, p))
1968                         return -EFAULT;
1969                 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1970                         return res;
1971                 return put_user(res, p);
1972         case SOUND_PCM_READ_RATE:
1973                 res = snd_pcm_oss_get_rate(pcm_oss_file);
1974                 if (res < 0)
1975                         return res;
1976                 return put_user(res, p);
1977         case SNDCTL_DSP_STEREO:
1978                 if (get_user(res, p))
1979                         return -EFAULT;
1980                 res = res > 0 ? 2 : 1;
1981                 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1982                         return res;
1983                 return put_user(--res, p);
1984         case SNDCTL_DSP_GETBLKSIZE:
1985                 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1986                 if (res < 0)
1987                         return res;
1988                 return put_user(res, p);
1989         case SNDCTL_DSP_SETFMT:
1990                 if (get_user(res, p))
1991                         return -EFAULT;
1992                 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1993                 if (res < 0)
1994                         return res;
1995                 return put_user(res, p);
1996         case SOUND_PCM_READ_BITS:
1997                 res = snd_pcm_oss_get_format(pcm_oss_file);
1998                 if (res < 0)
1999                         return res;
2000                 return put_user(res, p);
2001         case SNDCTL_DSP_CHANNELS:
2002                 if (get_user(res, p))
2003                         return -EFAULT;
2004                 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2005                 if (res < 0)
2006                         return res;
2007                 return put_user(res, p);
2008         case SOUND_PCM_READ_CHANNELS:
2009                 res = snd_pcm_oss_get_channels(pcm_oss_file);
2010                 if (res < 0)
2011                         return res;
2012                 return put_user(res, p);
2013         case SOUND_PCM_WRITE_FILTER:
2014         case SOUND_PCM_READ_FILTER:
2015                 return -EIO;
2016         case SNDCTL_DSP_POST:
2017                 return snd_pcm_oss_post(pcm_oss_file);
2018         case SNDCTL_DSP_SUBDIVIDE:
2019                 if (get_user(res, p))
2020                         return -EFAULT;
2021                 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2022                 if (res < 0)
2023                         return res;
2024                 return put_user(res, p);
2025         case SNDCTL_DSP_SETFRAGMENT:
2026                 if (get_user(res, p))
2027                         return -EFAULT;
2028                 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2029         case SNDCTL_DSP_GETFMTS:
2030                 res = snd_pcm_oss_get_formats(pcm_oss_file);
2031                 if (res < 0)
2032                         return res;
2033                 return put_user(res, p);
2034         case SNDCTL_DSP_GETOSPACE:
2035         case SNDCTL_DSP_GETISPACE:
2036                 return snd_pcm_oss_get_space(pcm_oss_file,
2037                         cmd == SNDCTL_DSP_GETISPACE ?
2038                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2039                         (struct audio_buf_info __user *) arg);
2040         case SNDCTL_DSP_NONBLOCK:
2041                 return snd_pcm_oss_nonblock(file);
2042         case SNDCTL_DSP_GETCAPS:
2043                 res = snd_pcm_oss_get_caps(pcm_oss_file);
2044                 if (res < 0)
2045                         return res;
2046                 return put_user(res, p);
2047         case SNDCTL_DSP_GETTRIGGER:
2048                 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2049                 if (res < 0)
2050                         return res;
2051                 return put_user(res, p);
2052         case SNDCTL_DSP_SETTRIGGER:
2053                 if (get_user(res, p))
2054                         return -EFAULT;
2055                 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2056         case SNDCTL_DSP_GETIPTR:
2057         case SNDCTL_DSP_GETOPTR:
2058                 return snd_pcm_oss_get_ptr(pcm_oss_file,
2059                         cmd == SNDCTL_DSP_GETIPTR ?
2060                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2061                         (struct count_info __user *) arg);
2062         case SNDCTL_DSP_MAPINBUF:
2063         case SNDCTL_DSP_MAPOUTBUF:
2064                 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2065                         cmd == SNDCTL_DSP_MAPINBUF ?
2066                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2067                         (struct buffmem_desc __user *) arg);
2068         case SNDCTL_DSP_SETSYNCRO:
2069                 /* stop DMA now.. */
2070                 return 0;
2071         case SNDCTL_DSP_SETDUPLEX:
2072                 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2073                         return 0;
2074                 return -EIO;
2075         case SNDCTL_DSP_GETODELAY:
2076                 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2077                 if (res < 0) {
2078                         /* it's for sure, some broken apps don't check for error codes */
2079                         put_user(0, p);
2080                         return res;
2081                 }
2082                 return put_user(res, p);
2083         case SNDCTL_DSP_PROFILE:
2084                 return 0;       /* silently ignore */
2085         default:
2086                 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2087         }
2088         return -EINVAL;
2089 }
2090
2091 #ifdef CONFIG_COMPAT
2092 /* all compatible */
2093 #define snd_pcm_oss_ioctl_compat        snd_pcm_oss_ioctl
2094 #else
2095 #define snd_pcm_oss_ioctl_compat        NULL
2096 #endif
2097
2098 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2099 {
2100         snd_pcm_oss_file_t *pcm_oss_file;
2101         snd_pcm_substream_t *substream;
2102
2103         pcm_oss_file = file->private_data;
2104         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2105         if (substream == NULL)
2106                 return -ENXIO;
2107 #ifndef OSS_DEBUG
2108         return snd_pcm_oss_read1(substream, buf, count);
2109 #else
2110         {
2111                 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2112                 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2113                 return res;
2114         }
2115 #endif
2116 }
2117
2118 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2119 {
2120         snd_pcm_oss_file_t *pcm_oss_file;
2121         snd_pcm_substream_t *substream;
2122         long result;
2123
2124         pcm_oss_file = file->private_data;
2125         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2126         if (substream == NULL)
2127                 return -ENXIO;
2128         up(&file->f_dentry->d_inode->i_sem);
2129         result = snd_pcm_oss_write1(substream, buf, count);
2130         down(&file->f_dentry->d_inode->i_sem);
2131 #ifdef OSS_DEBUG
2132         printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2133 #endif
2134         return result;
2135 }
2136
2137 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2138 {
2139         snd_pcm_runtime_t *runtime = substream->runtime;
2140         if (atomic_read(&runtime->mmap_count))
2141                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2142         else
2143                 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2144 }
2145
2146 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2147 {
2148         snd_pcm_runtime_t *runtime = substream->runtime;
2149         if (atomic_read(&runtime->mmap_count))
2150                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2151         else
2152                 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2153 }
2154
2155 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2156 {
2157         snd_pcm_oss_file_t *pcm_oss_file;
2158         unsigned int mask;
2159         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2160         
2161         pcm_oss_file = file->private_data;
2162
2163         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2164         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2165
2166         mask = 0;
2167         if (psubstream != NULL) {
2168                 snd_pcm_runtime_t *runtime = psubstream->runtime;
2169                 poll_wait(file, &runtime->sleep, wait);
2170                 snd_pcm_stream_lock_irq(psubstream);
2171                 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2172                     (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2173                      snd_pcm_oss_playback_ready(psubstream)))
2174                         mask |= POLLOUT | POLLWRNORM;
2175                 snd_pcm_stream_unlock_irq(psubstream);
2176         }
2177         if (csubstream != NULL) {
2178                 snd_pcm_runtime_t *runtime = csubstream->runtime;
2179                 enum sndrv_pcm_state ostate;
2180                 poll_wait(file, &runtime->sleep, wait);
2181                 snd_pcm_stream_lock_irq(csubstream);
2182                 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2183                     snd_pcm_oss_capture_ready(csubstream))
2184                         mask |= POLLIN | POLLRDNORM;
2185                 snd_pcm_stream_unlock_irq(csubstream);
2186                 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2187                         snd_pcm_oss_file_t ofile;
2188                         memset(&ofile, 0, sizeof(ofile));
2189                         ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2190                         runtime->oss.trigger = 0;
2191                         snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2192                 }
2193         }
2194
2195         return mask;
2196 }
2197
2198 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2199 {
2200         snd_pcm_oss_file_t *pcm_oss_file;
2201         snd_pcm_substream_t *substream = NULL;
2202         snd_pcm_runtime_t *runtime;
2203         int err;
2204
2205 #ifdef OSS_DEBUG
2206         printk("pcm_oss: mmap begin\n");
2207 #endif
2208         pcm_oss_file = file->private_data;
2209         switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2210         case VM_READ | VM_WRITE:
2211                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2212                 if (substream)
2213                         break;
2214                 /* Fall through */
2215         case VM_READ:
2216                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2217                 break;
2218         case VM_WRITE:
2219                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2220                 break;
2221         default:
2222                 return -EINVAL;
2223         }
2224         /* set VM_READ access as well to fix memset() routines that do
2225            reads before writes (to improve performance) */
2226         area->vm_flags |= VM_READ;
2227         if (substream == NULL)
2228                 return -ENXIO;
2229         runtime = substream->runtime;
2230         if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2231                 return -EIO;
2232         if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2233                 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2234         else
2235                 return -EIO;
2236         
2237         if (runtime->oss.params) {
2238                 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2239                         return err;
2240         }
2241         if (runtime->oss.plugin_first != NULL)
2242                 return -EIO;
2243
2244         if (area->vm_pgoff != 0)
2245                 return -EINVAL;
2246
2247         err = snd_pcm_mmap_data(substream, file, area);
2248         if (err < 0)
2249                 return err;
2250         runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2251         runtime->silence_threshold = 0;
2252         runtime->silence_size = 0;
2253 #ifdef OSS_DEBUG
2254         printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2255 #endif
2256         /* In mmap mode we never stop */
2257         runtime->stop_threshold = runtime->boundary;
2258
2259         return 0;
2260 }
2261
2262 /*
2263  *  /proc interface
2264  */
2265
2266 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2267                                   snd_info_buffer_t * buffer)
2268 {
2269         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2270         snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2271         down(&pstr->oss.setup_mutex);
2272         while (setup) {
2273                 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2274                             setup->task_name,
2275                             setup->periods,
2276                             setup->period_size,
2277                             setup->disable ? " disable" : "",
2278                             setup->direct ? " direct" : "",
2279                             setup->block ? " block" : "",
2280                             setup->nonblock ? " non-block" : "",
2281                             setup->partialfrag ? " partial-frag" : "",
2282                             setup->nosilence ? " no-silence" : "");
2283                 setup = setup->next;
2284         }
2285         up(&pstr->oss.setup_mutex);
2286 }
2287
2288 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2289 {
2290         unsigned int idx;
2291         snd_pcm_substream_t *substream;
2292         snd_pcm_oss_setup_t *setup, *setupn;
2293
2294         for (idx = 0, substream = pstr->substream;
2295              idx < pstr->substream_count; idx++, substream = substream->next)
2296                 substream->oss.setup = NULL;
2297         for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2298              setup; setup = setupn) {
2299                 setupn = setup->next;
2300                 kfree(setup->task_name);
2301                 kfree(setup);
2302         }
2303         pstr->oss.setup_list = NULL;
2304 }
2305
2306 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2307                                    snd_info_buffer_t * buffer)
2308 {
2309         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2310         char line[128], str[32], task_name[32], *ptr;
2311         int idx1;
2312         snd_pcm_oss_setup_t *setup, *setup1, template;
2313
2314         while (!snd_info_get_line(buffer, line, sizeof(line))) {
2315                 down(&pstr->oss.setup_mutex);
2316                 memset(&template, 0, sizeof(template));
2317                 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2318                 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2319                         snd_pcm_oss_proc_free_setup_list(pstr);
2320                         up(&pstr->oss.setup_mutex);
2321                         continue;
2322                 }
2323                 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2324                         if (!strcmp(setup->task_name, task_name)) {
2325                                 template = *setup;
2326                                 break;
2327                         }
2328                 }
2329                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2330                 template.periods = simple_strtoul(str, NULL, 10);
2331                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2332                 template.period_size = simple_strtoul(str, NULL, 10);
2333                 for (idx1 = 31; idx1 >= 0; idx1--)
2334                         if (template.period_size & (1 << idx1))
2335                                 break;
2336                 for (idx1--; idx1 >= 0; idx1--)
2337                         template.period_size &= ~(1 << idx1);
2338                 do {
2339                         ptr = snd_info_get_str(str, ptr, sizeof(str));
2340                         if (!strcmp(str, "disable")) {
2341                                 template.disable = 1;
2342                         } else if (!strcmp(str, "direct")) {
2343                                 template.direct = 1;
2344                         } else if (!strcmp(str, "block")) {
2345                                 template.block = 1;
2346                         } else if (!strcmp(str, "non-block")) {
2347                                 template.nonblock = 1;
2348                         } else if (!strcmp(str, "partial-frag")) {
2349                                 template.partialfrag = 1;
2350                         } else if (!strcmp(str, "no-silence")) {
2351                                 template.nosilence = 1;
2352                         }
2353                 } while (*str);
2354                 if (setup == NULL) {
2355                         setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2356                         if (setup) {
2357                                 if (pstr->oss.setup_list == NULL) {
2358                                         pstr->oss.setup_list = setup;
2359                                 } else {
2360                                         for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2361                                         setup1->next = setup;
2362                                 }
2363                                 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2364                         } else {
2365                                 buffer->error = -ENOMEM;
2366                         }
2367                 }
2368                 if (setup)
2369                         *setup = template;
2370                 up(&pstr->oss.setup_mutex);
2371         }
2372 }
2373
2374 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2375 {
2376         int stream;
2377         for (stream = 0; stream < 2; ++stream) {
2378                 snd_info_entry_t *entry;
2379                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2380                 if (pstr->substream_count == 0)
2381                         continue;
2382                 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2383                         entry->content = SNDRV_INFO_CONTENT_TEXT;
2384                         entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2385                         entry->c.text.read_size = 8192;
2386                         entry->c.text.read = snd_pcm_oss_proc_read;
2387                         entry->c.text.write_size = 8192;
2388                         entry->c.text.write = snd_pcm_oss_proc_write;
2389                         entry->private_data = pstr;
2390                         if (snd_info_register(entry) < 0) {
2391                                 snd_info_free_entry(entry);
2392                                 entry = NULL;
2393                         }
2394                 }
2395                 pstr->oss.proc_entry = entry;
2396         }
2397 }
2398
2399 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2400 {
2401         int stream;
2402         for (stream = 0; stream < 2; ++stream) {
2403                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2404                 if (pstr->oss.proc_entry) {
2405                         snd_info_unregister(pstr->oss.proc_entry);
2406                         pstr->oss.proc_entry = NULL;
2407                         snd_pcm_oss_proc_free_setup_list(pstr);
2408                 }
2409         }
2410 }
2411
2412 /*
2413  *  ENTRY functions
2414  */
2415
2416 static struct file_operations snd_pcm_oss_f_reg =
2417 {
2418         .owner =        THIS_MODULE,
2419         .read =         snd_pcm_oss_read,
2420         .write =        snd_pcm_oss_write,
2421         .open =         snd_pcm_oss_open,
2422         .release =      snd_pcm_oss_release,
2423         .poll =         snd_pcm_oss_poll,
2424         .unlocked_ioctl =       snd_pcm_oss_ioctl,
2425         .compat_ioctl = snd_pcm_oss_ioctl_compat,
2426         .mmap =         snd_pcm_oss_mmap,
2427 };
2428
2429 static snd_minor_t snd_pcm_oss_reg =
2430 {
2431         .comment =      "digital audio",
2432         .f_ops =        &snd_pcm_oss_f_reg,
2433 };
2434
2435 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2436 {
2437         char name[128];
2438         sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2439         if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2440                                     pcm->card, index, &snd_pcm_oss_reg,
2441                                     name) < 0) {
2442                 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2443         }
2444 }
2445
2446 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2447 {
2448         pcm->oss.reg = 0;
2449         if (dsp_map[pcm->card->number] == (int)pcm->device) {
2450                 char name[128];
2451                 int duplex;
2452                 register_oss_dsp(pcm, 0);
2453                 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
2454                               pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
2455                               !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2456                 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2457 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2458                 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2459                                       pcm->card->number,
2460                                       name);
2461 #endif
2462                 pcm->oss.reg++;
2463                 pcm->oss.reg_mask |= 1;
2464         }
2465         if (adsp_map[pcm->card->number] == (int)pcm->device) {
2466                 register_oss_dsp(pcm, 1);
2467                 pcm->oss.reg++;
2468                 pcm->oss.reg_mask |= 2;
2469         }
2470
2471         if (pcm->oss.reg)
2472                 snd_pcm_oss_proc_init(pcm);
2473
2474         return 0;
2475 }
2476
2477 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2478 {
2479         if (pcm->oss.reg) {
2480                 if (pcm->oss.reg_mask & 1) {
2481                         pcm->oss.reg_mask &= ~1;
2482                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2483                                                   pcm->card, 0);
2484                 }
2485                 if (pcm->oss.reg_mask & 2) {
2486                         pcm->oss.reg_mask &= ~2;
2487                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2488                                                   pcm->card, 1);
2489                 }
2490         }
2491         return 0;
2492 }
2493
2494 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2495 {
2496         snd_pcm_oss_disconnect_minor(pcm);
2497         if (pcm->oss.reg) {
2498                 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2499 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2500                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2501 #endif
2502                 }
2503                 pcm->oss.reg = 0;
2504                 snd_pcm_oss_proc_done(pcm);
2505         }
2506         return 0;
2507 }
2508
2509 static snd_pcm_notify_t snd_pcm_oss_notify =
2510 {
2511         .n_register =   snd_pcm_oss_register_minor,
2512         .n_disconnect = snd_pcm_oss_disconnect_minor,
2513         .n_unregister = snd_pcm_oss_unregister_minor,
2514 };
2515
2516 static int __init alsa_pcm_oss_init(void)
2517 {
2518         int i;
2519         int err;
2520
2521         /* check device map table */
2522         for (i = 0; i < SNDRV_CARDS; i++) {
2523                 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2524                         snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2525                         dsp_map[i] = 0;
2526                 }
2527                 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2528                         snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2529                         adsp_map[i] = 1;
2530                 }
2531         }
2532         if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2533                 return err;
2534         return 0;
2535 }
2536
2537 static void __exit alsa_pcm_oss_exit(void)
2538 {
2539         snd_pcm_notify(&snd_pcm_oss_notify, 1);
2540 }
2541
2542 module_init(alsa_pcm_oss_init)
2543 module_exit(alsa_pcm_oss_exit)