[ALSA] Fix compilation on 32bit arch
[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                         info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
1544                 else
1545                         info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
1546         }
1547         if (copy_to_user(_info, &info, sizeof(info)))
1548                 return -EFAULT;
1549         return 0;
1550 }
1551
1552 static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
1553 {
1554         snd_pcm_substream_t *substream;
1555         snd_pcm_runtime_t *runtime;
1556         snd_pcm_sframes_t avail;
1557         int fixup;
1558         struct audio_buf_info info;
1559         int err;
1560
1561         if (_info == NULL)
1562                 return -EFAULT;
1563         substream = pcm_oss_file->streams[stream];
1564         if (substream == NULL)
1565                 return -EINVAL;
1566         runtime = substream->runtime;
1567
1568         if (runtime->oss.params &&
1569             (err = snd_pcm_oss_change_params(substream)) < 0)
1570                 return err;
1571
1572         info.fragsize = runtime->oss.period_bytes;
1573         info.fragstotal = runtime->periods;
1574         if (runtime->oss.prepare) {
1575                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1576                         info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
1577                         info.fragments = runtime->oss.periods;
1578                 } else {
1579                         info.bytes = 0;
1580                         info.fragments = 0;
1581                 }
1582         } else {
1583                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1584                         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
1585                         if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
1586                                 avail = runtime->buffer_size;
1587                                 err = 0;
1588                                 fixup = 0;
1589                         } else {
1590                                 avail = runtime->buffer_size - avail;
1591                                 fixup = -runtime->oss.buffer_used;
1592                         }
1593                 } else {
1594                         err = snd_pcm_oss_capture_position_fixup(substream, &avail);
1595                         fixup = runtime->oss.buffer_used;
1596                 }
1597                 if (err < 0)
1598                         return err;
1599                 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
1600                 info.fragments = info.bytes / runtime->oss.period_bytes;
1601         }
1602
1603 #ifdef OSS_DEBUG
1604         printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
1605 #endif
1606         if (copy_to_user(_info, &info, sizeof(info)))
1607                 return -EFAULT;
1608         return 0;
1609 }
1610
1611 static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
1612 {
1613         // it won't be probably implemented
1614         // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
1615         return -EINVAL;
1616 }
1617
1618 static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
1619 {
1620         const char *ptr, *ptrl;
1621         snd_pcm_oss_setup_t *setup;
1622
1623         down(&pcm->streams[stream].oss.setup_mutex);
1624         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1625                 if (!strcmp(setup->task_name, task_name)) {
1626                         up(&pcm->streams[stream].oss.setup_mutex);
1627                         return setup;
1628                 }
1629         }
1630         ptr = ptrl = task_name;
1631         while (*ptr) {
1632                 if (*ptr == '/')
1633                         ptrl = ptr + 1;
1634                 ptr++;
1635         }
1636         if (ptrl == task_name) {
1637                 goto __not_found;
1638                 return NULL;
1639         }
1640         for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
1641                 if (!strcmp(setup->task_name, ptrl)) {
1642                         up(&pcm->streams[stream].oss.setup_mutex);
1643                         return setup;
1644                 }
1645         }
1646       __not_found:
1647         up(&pcm->streams[stream].oss.setup_mutex);
1648         return NULL;
1649 }
1650
1651 static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
1652                                        snd_pcm_oss_setup_t *setup,
1653                                        int minor)
1654 {
1655         snd_pcm_runtime_t *runtime;
1656
1657         substream->oss.oss = 1;
1658         substream->oss.setup = setup;
1659         runtime = substream->runtime;
1660         runtime->oss.params = 1;
1661         runtime->oss.trigger = 1;
1662         runtime->oss.rate = 8000;
1663         switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
1664         case SNDRV_MINOR_OSS_PCM_8:
1665                 runtime->oss.format = AFMT_U8;
1666                 break;
1667         case SNDRV_MINOR_OSS_PCM_16:
1668                 runtime->oss.format = AFMT_S16_LE;
1669                 break;
1670         default:
1671                 runtime->oss.format = AFMT_MU_LAW;
1672         }
1673         runtime->oss.channels = 1;
1674         runtime->oss.fragshift = 0;
1675         runtime->oss.maxfrags = 0;
1676         runtime->oss.subdivision = 0;
1677 }
1678
1679 static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
1680 {
1681         snd_pcm_runtime_t *runtime;
1682         runtime = substream->runtime;
1683         vfree(runtime->oss.buffer);
1684         snd_pcm_oss_plugin_clear(substream);
1685         substream->oss.file = NULL;
1686         substream->oss.oss = 0;
1687 }
1688
1689 static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
1690 {
1691         int cidx;
1692         snd_assert(pcm_oss_file != NULL, return -ENXIO);
1693         for (cidx = 0; cidx < 2; ++cidx) {
1694                 snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
1695                 snd_pcm_runtime_t *runtime;
1696                 if (substream == NULL)
1697                         continue;
1698                 runtime = substream->runtime;
1699                 
1700                 snd_pcm_stream_lock_irq(substream);
1701                 if (snd_pcm_running(substream))
1702                         snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1703                 snd_pcm_stream_unlock_irq(substream);
1704                 if (substream->open_flag) {
1705                         if (substream->ops->hw_free != NULL)
1706                                 substream->ops->hw_free(substream);
1707                         substream->ops->close(substream);
1708                         substream->open_flag = 0;
1709                 }
1710                 substream->ffile = NULL;
1711                 snd_pcm_oss_release_substream(substream);
1712                 snd_pcm_release_substream(substream);
1713         }
1714         kfree(pcm_oss_file);
1715         return 0;
1716 }
1717
1718 static int snd_pcm_oss_open_file(struct file *file,
1719                                  snd_pcm_t *pcm,
1720                                  snd_pcm_oss_file_t **rpcm_oss_file,
1721                                  int minor,
1722                                  snd_pcm_oss_setup_t *psetup,
1723                                  snd_pcm_oss_setup_t *csetup)
1724 {
1725         int err = 0;
1726         snd_pcm_oss_file_t *pcm_oss_file;
1727         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
1728         unsigned int f_mode = file->f_mode;
1729
1730         snd_assert(rpcm_oss_file != NULL, return -EINVAL);
1731         *rpcm_oss_file = NULL;
1732
1733         pcm_oss_file = kcalloc(1, sizeof(*pcm_oss_file), GFP_KERNEL);
1734         if (pcm_oss_file == NULL)
1735                 return -ENOMEM;
1736
1737         if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
1738             (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
1739                 f_mode = FMODE_WRITE;
1740         if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1741                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1742                                                &psubstream)) < 0) {
1743                         snd_pcm_oss_release_file(pcm_oss_file);
1744                         return err;
1745                 }
1746                 pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1747         }
1748         if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1749                 if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE, 
1750                                                &csubstream)) < 0) {
1751                         if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1752                                 snd_pcm_oss_release_file(pcm_oss_file);
1753                                 return err;
1754                         } else {
1755                                 csubstream = NULL;
1756                         }
1757                 }
1758                 pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1759         }
1760         
1761         if (psubstream == NULL && csubstream == NULL) {
1762                 snd_pcm_oss_release_file(pcm_oss_file);
1763                 return -EINVAL;
1764         }
1765         if (psubstream != NULL) {
1766                 psubstream->oss.file = pcm_oss_file;
1767                 err = snd_pcm_hw_constraints_init(psubstream);
1768                 if (err < 0) {
1769                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1770                         snd_pcm_oss_release_file(pcm_oss_file);
1771                         return err;
1772                 }
1773                 if ((err = psubstream->ops->open(psubstream)) < 0) {
1774                         snd_pcm_oss_release_file(pcm_oss_file);
1775                         return err;
1776                 }
1777                 psubstream->open_flag = 1;
1778                 err = snd_pcm_hw_constraints_complete(psubstream);
1779                 if (err < 0) {
1780                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1781                         snd_pcm_oss_release_file(pcm_oss_file);
1782                         return err;
1783                 }
1784                 psubstream->ffile = file;
1785                 snd_pcm_oss_init_substream(psubstream, psetup, minor);
1786         }
1787         if (csubstream != NULL) {
1788                 csubstream->oss.file = pcm_oss_file;
1789                 err = snd_pcm_hw_constraints_init(csubstream);
1790                 if (err < 0) {
1791                         snd_printd("snd_pcm_hw_constraint_init failed\n");
1792                         snd_pcm_oss_release_file(pcm_oss_file);
1793                         return err;
1794                 }
1795                 if ((err = csubstream->ops->open(csubstream)) < 0) {
1796                         snd_pcm_oss_release_file(pcm_oss_file);
1797                         return err;
1798                 }
1799                 csubstream->open_flag = 1;
1800                 err = snd_pcm_hw_constraints_complete(csubstream);
1801                 if (err < 0) {
1802                         snd_printd("snd_pcm_hw_constraint_complete failed\n");
1803                         snd_pcm_oss_release_file(pcm_oss_file);
1804                         return err;
1805                 }
1806                 csubstream->ffile = file;
1807                 snd_pcm_oss_init_substream(csubstream, csetup, minor);
1808         }
1809
1810         file->private_data = pcm_oss_file;
1811         *rpcm_oss_file = pcm_oss_file;
1812         return 0;
1813 }
1814
1815
1816 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
1817 {
1818         int minor = iminor(inode);
1819         int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1820         int device;
1821         int err;
1822         char task_name[32];
1823         snd_pcm_t *pcm;
1824         snd_pcm_oss_file_t *pcm_oss_file;
1825         snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
1826         int nonblock;
1827         wait_queue_t wait;
1828
1829         snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1830         device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1831                 adsp_map[cardnum] : dsp_map[cardnum];
1832
1833         pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1834         if (pcm == NULL) {
1835                 err = -ENODEV;
1836                 goto __error1;
1837         }
1838         err = snd_card_file_add(pcm->card, file);
1839         if (err < 0)
1840                 goto __error1;
1841         if (!try_module_get(pcm->card->module)) {
1842                 err = -EFAULT;
1843                 goto __error2;
1844         }
1845         if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
1846                 err = -EFAULT;
1847                 goto __error;
1848         }
1849         if (file->f_mode & FMODE_WRITE)
1850                 psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1851         if (file->f_mode & FMODE_READ)
1852                 csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1853
1854         nonblock = !!(file->f_flags & O_NONBLOCK);
1855         if (psetup && !psetup->disable) {
1856                 if (psetup->nonblock)
1857                         nonblock = 1;
1858                 else if (psetup->block)
1859                         nonblock = 0;
1860         } else if (csetup && !csetup->disable) {
1861                 if (csetup->nonblock)
1862                         nonblock = 1;
1863                 else if (csetup->block)
1864                         nonblock = 0;
1865         }
1866         if (!nonblock)
1867                 nonblock = nonblock_open;
1868
1869         init_waitqueue_entry(&wait, current);
1870         add_wait_queue(&pcm->open_wait, &wait);
1871         down(&pcm->open_mutex);
1872         while (1) {
1873                 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1874                                             minor, psetup, csetup);
1875                 if (err >= 0)
1876                         break;
1877                 if (err == -EAGAIN) {
1878                         if (nonblock) {
1879                                 err = -EBUSY;
1880                                 break;
1881                         }
1882                 } else
1883                         break;
1884                 set_current_state(TASK_INTERRUPTIBLE);
1885                 up(&pcm->open_mutex);
1886                 schedule();
1887                 down(&pcm->open_mutex);
1888                 if (signal_pending(current)) {
1889                         err = -ERESTARTSYS;
1890                         break;
1891                 }
1892         }
1893         remove_wait_queue(&pcm->open_wait, &wait);
1894         up(&pcm->open_mutex);
1895         if (err < 0)
1896                 goto __error;
1897         return err;
1898
1899       __error:
1900         module_put(pcm->card->module);
1901       __error2:
1902         snd_card_file_remove(pcm->card, file);
1903       __error1:
1904         return err;
1905 }
1906
1907 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
1908 {
1909         snd_pcm_t *pcm;
1910         snd_pcm_substream_t *substream;
1911         snd_pcm_oss_file_t *pcm_oss_file;
1912
1913         pcm_oss_file = file->private_data;
1914         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1915         if (substream == NULL)
1916                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1917         snd_assert(substream != NULL, return -ENXIO);
1918         pcm = substream->pcm;
1919         snd_pcm_oss_sync(pcm_oss_file);
1920         down(&pcm->open_mutex);
1921         snd_pcm_oss_release_file(pcm_oss_file);
1922         up(&pcm->open_mutex);
1923         wake_up(&pcm->open_wait);
1924         module_put(pcm->card->module);
1925         snd_card_file_remove(pcm->card, file);
1926         return 0;
1927 }
1928
1929 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1930 {
1931         snd_pcm_oss_file_t *pcm_oss_file;
1932         int __user *p = (int __user *)arg;
1933         int res;
1934
1935         pcm_oss_file = file->private_data;
1936         if (cmd == OSS_GETVERSION)
1937                 return put_user(SNDRV_OSS_VERSION, p);
1938         if (cmd == OSS_ALSAEMULVER)
1939                 return put_user(1, p);
1940 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
1941         if (((cmd >> 8) & 0xff) == 'M') {       /* mixer ioctl - for OSS compatibility */
1942                 snd_pcm_substream_t *substream;
1943                 int idx;
1944                 for (idx = 0; idx < 2; ++idx) {
1945                         substream = pcm_oss_file->streams[idx];
1946                         if (substream != NULL)
1947                                 break;
1948                 }
1949                 snd_assert(substream != NULL, return -ENXIO);
1950                 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
1951         }
1952 #endif
1953         if (((cmd >> 8) & 0xff) != 'P')
1954                 return -EINVAL;
1955 #ifdef OSS_DEBUG
1956         printk("pcm_oss: ioctl = 0x%x\n", cmd);
1957 #endif
1958         switch (cmd) {
1959         case SNDCTL_DSP_RESET:
1960                 return snd_pcm_oss_reset(pcm_oss_file);
1961         case SNDCTL_DSP_SYNC:
1962                 return snd_pcm_oss_sync(pcm_oss_file);
1963         case SNDCTL_DSP_SPEED:
1964                 if (get_user(res, p))
1965                         return -EFAULT;
1966                 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
1967                         return res;
1968                 return put_user(res, p);
1969         case SOUND_PCM_READ_RATE:
1970                 res = snd_pcm_oss_get_rate(pcm_oss_file);
1971                 if (res < 0)
1972                         return res;
1973                 return put_user(res, p);
1974         case SNDCTL_DSP_STEREO:
1975                 if (get_user(res, p))
1976                         return -EFAULT;
1977                 res = res > 0 ? 2 : 1;
1978                 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
1979                         return res;
1980                 return put_user(--res, p);
1981         case SNDCTL_DSP_GETBLKSIZE:
1982                 res = snd_pcm_oss_get_block_size(pcm_oss_file);
1983                 if (res < 0)
1984                         return res;
1985                 return put_user(res, p);
1986         case SNDCTL_DSP_SETFMT:
1987                 if (get_user(res, p))
1988                         return -EFAULT;
1989                 res = snd_pcm_oss_set_format(pcm_oss_file, res);
1990                 if (res < 0)
1991                         return res;
1992                 return put_user(res, p);
1993         case SOUND_PCM_READ_BITS:
1994                 res = snd_pcm_oss_get_format(pcm_oss_file);
1995                 if (res < 0)
1996                         return res;
1997                 return put_user(res, p);
1998         case SNDCTL_DSP_CHANNELS:
1999                 if (get_user(res, p))
2000                         return -EFAULT;
2001                 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2002                 if (res < 0)
2003                         return res;
2004                 return put_user(res, p);
2005         case SOUND_PCM_READ_CHANNELS:
2006                 res = snd_pcm_oss_get_channels(pcm_oss_file);
2007                 if (res < 0)
2008                         return res;
2009                 return put_user(res, p);
2010         case SOUND_PCM_WRITE_FILTER:
2011         case SOUND_PCM_READ_FILTER:
2012                 return -EIO;
2013         case SNDCTL_DSP_POST:
2014                 return snd_pcm_oss_post(pcm_oss_file);
2015         case SNDCTL_DSP_SUBDIVIDE:
2016                 if (get_user(res, p))
2017                         return -EFAULT;
2018                 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2019                 if (res < 0)
2020                         return res;
2021                 return put_user(res, p);
2022         case SNDCTL_DSP_SETFRAGMENT:
2023                 if (get_user(res, p))
2024                         return -EFAULT;
2025                 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2026         case SNDCTL_DSP_GETFMTS:
2027                 res = snd_pcm_oss_get_formats(pcm_oss_file);
2028                 if (res < 0)
2029                         return res;
2030                 return put_user(res, p);
2031         case SNDCTL_DSP_GETOSPACE:
2032         case SNDCTL_DSP_GETISPACE:
2033                 return snd_pcm_oss_get_space(pcm_oss_file,
2034                         cmd == SNDCTL_DSP_GETISPACE ?
2035                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2036                         (struct audio_buf_info __user *) arg);
2037         case SNDCTL_DSP_NONBLOCK:
2038                 return snd_pcm_oss_nonblock(file);
2039         case SNDCTL_DSP_GETCAPS:
2040                 res = snd_pcm_oss_get_caps(pcm_oss_file);
2041                 if (res < 0)
2042                         return res;
2043                 return put_user(res, p);
2044         case SNDCTL_DSP_GETTRIGGER:
2045                 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2046                 if (res < 0)
2047                         return res;
2048                 return put_user(res, p);
2049         case SNDCTL_DSP_SETTRIGGER:
2050                 if (get_user(res, p))
2051                         return -EFAULT;
2052                 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2053         case SNDCTL_DSP_GETIPTR:
2054         case SNDCTL_DSP_GETOPTR:
2055                 return snd_pcm_oss_get_ptr(pcm_oss_file,
2056                         cmd == SNDCTL_DSP_GETIPTR ?
2057                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2058                         (struct count_info __user *) arg);
2059         case SNDCTL_DSP_MAPINBUF:
2060         case SNDCTL_DSP_MAPOUTBUF:
2061                 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2062                         cmd == SNDCTL_DSP_MAPINBUF ?
2063                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2064                         (struct buffmem_desc __user *) arg);
2065         case SNDCTL_DSP_SETSYNCRO:
2066                 /* stop DMA now.. */
2067                 return 0;
2068         case SNDCTL_DSP_SETDUPLEX:
2069                 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2070                         return 0;
2071                 return -EIO;
2072         case SNDCTL_DSP_GETODELAY:
2073                 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2074                 if (res < 0) {
2075                         /* it's for sure, some broken apps don't check for error codes */
2076                         put_user(0, p);
2077                         return res;
2078                 }
2079                 return put_user(res, p);
2080         case SNDCTL_DSP_PROFILE:
2081                 return 0;       /* silently ignore */
2082         default:
2083                 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2084         }
2085         return -EINVAL;
2086 }
2087
2088 #ifdef CONFIG_COMPAT
2089 /* all compatible */
2090 #define snd_pcm_oss_ioctl_compat        snd_pcm_oss_ioctl
2091 #else
2092 #define snd_pcm_oss_ioctl_compat        NULL
2093 #endif
2094
2095 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2096 {
2097         snd_pcm_oss_file_t *pcm_oss_file;
2098         snd_pcm_substream_t *substream;
2099
2100         pcm_oss_file = file->private_data;
2101         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2102         if (substream == NULL)
2103                 return -ENXIO;
2104 #ifndef OSS_DEBUG
2105         return snd_pcm_oss_read1(substream, buf, count);
2106 #else
2107         {
2108                 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2109                 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2110                 return res;
2111         }
2112 #endif
2113 }
2114
2115 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2116 {
2117         snd_pcm_oss_file_t *pcm_oss_file;
2118         snd_pcm_substream_t *substream;
2119         long result;
2120
2121         pcm_oss_file = file->private_data;
2122         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2123         if (substream == NULL)
2124                 return -ENXIO;
2125         up(&file->f_dentry->d_inode->i_sem);
2126         result = snd_pcm_oss_write1(substream, buf, count);
2127         down(&file->f_dentry->d_inode->i_sem);
2128 #ifdef OSS_DEBUG
2129         printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2130 #endif
2131         return result;
2132 }
2133
2134 static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
2135 {
2136         snd_pcm_runtime_t *runtime = substream->runtime;
2137         if (atomic_read(&runtime->mmap_count))
2138                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2139         else
2140                 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2141 }
2142
2143 static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
2144 {
2145         snd_pcm_runtime_t *runtime = substream->runtime;
2146         if (atomic_read(&runtime->mmap_count))
2147                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2148         else
2149                 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2150 }
2151
2152 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2153 {
2154         snd_pcm_oss_file_t *pcm_oss_file;
2155         unsigned int mask;
2156         snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
2157         
2158         pcm_oss_file = file->private_data;
2159
2160         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2161         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2162
2163         mask = 0;
2164         if (psubstream != NULL) {
2165                 snd_pcm_runtime_t *runtime = psubstream->runtime;
2166                 poll_wait(file, &runtime->sleep, wait);
2167                 snd_pcm_stream_lock_irq(psubstream);
2168                 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2169                     (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2170                      snd_pcm_oss_playback_ready(psubstream)))
2171                         mask |= POLLOUT | POLLWRNORM;
2172                 snd_pcm_stream_unlock_irq(psubstream);
2173         }
2174         if (csubstream != NULL) {
2175                 snd_pcm_runtime_t *runtime = csubstream->runtime;
2176                 enum sndrv_pcm_state ostate;
2177                 poll_wait(file, &runtime->sleep, wait);
2178                 snd_pcm_stream_lock_irq(csubstream);
2179                 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2180                     snd_pcm_oss_capture_ready(csubstream))
2181                         mask |= POLLIN | POLLRDNORM;
2182                 snd_pcm_stream_unlock_irq(csubstream);
2183                 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2184                         snd_pcm_oss_file_t ofile;
2185                         memset(&ofile, 0, sizeof(ofile));
2186                         ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2187                         runtime->oss.trigger = 0;
2188                         snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2189                 }
2190         }
2191
2192         return mask;
2193 }
2194
2195 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2196 {
2197         snd_pcm_oss_file_t *pcm_oss_file;
2198         snd_pcm_substream_t *substream = NULL;
2199         snd_pcm_runtime_t *runtime;
2200         int err;
2201
2202 #ifdef OSS_DEBUG
2203         printk("pcm_oss: mmap begin\n");
2204 #endif
2205         pcm_oss_file = file->private_data;
2206         switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2207         case VM_READ | VM_WRITE:
2208                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2209                 if (substream)
2210                         break;
2211                 /* Fall through */
2212         case VM_READ:
2213                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2214                 break;
2215         case VM_WRITE:
2216                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2217                 break;
2218         default:
2219                 return -EINVAL;
2220         }
2221         /* set VM_READ access as well to fix memset() routines that do
2222            reads before writes (to improve performance) */
2223         area->vm_flags |= VM_READ;
2224         if (substream == NULL)
2225                 return -ENXIO;
2226         runtime = substream->runtime;
2227         if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2228                 return -EIO;
2229         if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2230                 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2231         else
2232                 return -EIO;
2233         
2234         if (runtime->oss.params) {
2235                 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2236                         return err;
2237         }
2238         if (runtime->oss.plugin_first != NULL)
2239                 return -EIO;
2240
2241         if (area->vm_pgoff != 0)
2242                 return -EINVAL;
2243
2244         err = snd_pcm_mmap_data(substream, file, area);
2245         if (err < 0)
2246                 return err;
2247         runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2248         runtime->silence_threshold = 0;
2249         runtime->silence_size = 0;
2250 #ifdef OSS_DEBUG
2251         printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2252 #endif
2253         /* In mmap mode we never stop */
2254         runtime->stop_threshold = runtime->boundary;
2255
2256         return 0;
2257 }
2258
2259 /*
2260  *  /proc interface
2261  */
2262
2263 static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
2264                                   snd_info_buffer_t * buffer)
2265 {
2266         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2267         snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
2268         down(&pstr->oss.setup_mutex);
2269         while (setup) {
2270                 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2271                             setup->task_name,
2272                             setup->periods,
2273                             setup->period_size,
2274                             setup->disable ? " disable" : "",
2275                             setup->direct ? " direct" : "",
2276                             setup->block ? " block" : "",
2277                             setup->nonblock ? " non-block" : "",
2278                             setup->partialfrag ? " partial-frag" : "",
2279                             setup->nosilence ? " no-silence" : "");
2280                 setup = setup->next;
2281         }
2282         up(&pstr->oss.setup_mutex);
2283 }
2284
2285 static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
2286 {
2287         unsigned int idx;
2288         snd_pcm_substream_t *substream;
2289         snd_pcm_oss_setup_t *setup, *setupn;
2290
2291         for (idx = 0, substream = pstr->substream;
2292              idx < pstr->substream_count; idx++, substream = substream->next)
2293                 substream->oss.setup = NULL;
2294         for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2295              setup; setup = setupn) {
2296                 setupn = setup->next;
2297                 kfree(setup->task_name);
2298                 kfree(setup);
2299         }
2300         pstr->oss.setup_list = NULL;
2301 }
2302
2303 static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
2304                                    snd_info_buffer_t * buffer)
2305 {
2306         snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
2307         char line[128], str[32], task_name[32], *ptr;
2308         int idx1;
2309         snd_pcm_oss_setup_t *setup, *setup1, template;
2310
2311         while (!snd_info_get_line(buffer, line, sizeof(line))) {
2312                 down(&pstr->oss.setup_mutex);
2313                 memset(&template, 0, sizeof(template));
2314                 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2315                 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2316                         snd_pcm_oss_proc_free_setup_list(pstr);
2317                         up(&pstr->oss.setup_mutex);
2318                         continue;
2319                 }
2320                 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2321                         if (!strcmp(setup->task_name, task_name)) {
2322                                 template = *setup;
2323                                 break;
2324                         }
2325                 }
2326                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2327                 template.periods = simple_strtoul(str, NULL, 10);
2328                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2329                 template.period_size = simple_strtoul(str, NULL, 10);
2330                 for (idx1 = 31; idx1 >= 0; idx1--)
2331                         if (template.period_size & (1 << idx1))
2332                                 break;
2333                 for (idx1--; idx1 >= 0; idx1--)
2334                         template.period_size &= ~(1 << idx1);
2335                 do {
2336                         ptr = snd_info_get_str(str, ptr, sizeof(str));
2337                         if (!strcmp(str, "disable")) {
2338                                 template.disable = 1;
2339                         } else if (!strcmp(str, "direct")) {
2340                                 template.direct = 1;
2341                         } else if (!strcmp(str, "block")) {
2342                                 template.block = 1;
2343                         } else if (!strcmp(str, "non-block")) {
2344                                 template.nonblock = 1;
2345                         } else if (!strcmp(str, "partial-frag")) {
2346                                 template.partialfrag = 1;
2347                         } else if (!strcmp(str, "no-silence")) {
2348                                 template.nosilence = 1;
2349                         }
2350                 } while (*str);
2351                 if (setup == NULL) {
2352                         setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
2353                         if (setup) {
2354                                 if (pstr->oss.setup_list == NULL) {
2355                                         pstr->oss.setup_list = setup;
2356                                 } else {
2357                                         for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
2358                                         setup1->next = setup;
2359                                 }
2360                                 template.task_name = snd_kmalloc_strdup(task_name, GFP_KERNEL);
2361                         } else {
2362                                 buffer->error = -ENOMEM;
2363                         }
2364                 }
2365                 if (setup)
2366                         *setup = template;
2367                 up(&pstr->oss.setup_mutex);
2368         }
2369 }
2370
2371 static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
2372 {
2373         int stream;
2374         for (stream = 0; stream < 2; ++stream) {
2375                 snd_info_entry_t *entry;
2376                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2377                 if (pstr->substream_count == 0)
2378                         continue;
2379                 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2380                         entry->content = SNDRV_INFO_CONTENT_TEXT;
2381                         entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2382                         entry->c.text.read_size = 8192;
2383                         entry->c.text.read = snd_pcm_oss_proc_read;
2384                         entry->c.text.write_size = 8192;
2385                         entry->c.text.write = snd_pcm_oss_proc_write;
2386                         entry->private_data = pstr;
2387                         if (snd_info_register(entry) < 0) {
2388                                 snd_info_free_entry(entry);
2389                                 entry = NULL;
2390                         }
2391                 }
2392                 pstr->oss.proc_entry = entry;
2393         }
2394 }
2395
2396 static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
2397 {
2398         int stream;
2399         for (stream = 0; stream < 2; ++stream) {
2400                 snd_pcm_str_t *pstr = &pcm->streams[stream];
2401                 if (pstr->oss.proc_entry) {
2402                         snd_info_unregister(pstr->oss.proc_entry);
2403                         pstr->oss.proc_entry = NULL;
2404                         snd_pcm_oss_proc_free_setup_list(pstr);
2405                 }
2406         }
2407 }
2408
2409 /*
2410  *  ENTRY functions
2411  */
2412
2413 static struct file_operations snd_pcm_oss_f_reg =
2414 {
2415         .owner =        THIS_MODULE,
2416         .read =         snd_pcm_oss_read,
2417         .write =        snd_pcm_oss_write,
2418         .open =         snd_pcm_oss_open,
2419         .release =      snd_pcm_oss_release,
2420         .poll =         snd_pcm_oss_poll,
2421         .unlocked_ioctl =       snd_pcm_oss_ioctl,
2422         .compat_ioctl = snd_pcm_oss_ioctl_compat,
2423         .mmap =         snd_pcm_oss_mmap,
2424 };
2425
2426 static snd_minor_t snd_pcm_oss_reg =
2427 {
2428         .comment =      "digital audio",
2429         .f_ops =        &snd_pcm_oss_f_reg,
2430 };
2431
2432 static void register_oss_dsp(snd_pcm_t *pcm, int index)
2433 {
2434         char name[128];
2435         sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2436         if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2437                                     pcm->card, index, &snd_pcm_oss_reg,
2438                                     name) < 0) {
2439                 snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
2440         }
2441 }
2442
2443 static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
2444 {
2445         pcm->oss.reg = 0;
2446         if (dsp_map[pcm->card->number] == (int)pcm->device) {
2447                 char name[128];
2448                 int duplex;
2449                 register_oss_dsp(pcm, 0);
2450                 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
2451                               pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
2452                               !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2453                 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2454 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2455                 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2456                                       pcm->card->number,
2457                                       name);
2458 #endif
2459                 pcm->oss.reg++;
2460                 pcm->oss.reg_mask |= 1;
2461         }
2462         if (adsp_map[pcm->card->number] == (int)pcm->device) {
2463                 register_oss_dsp(pcm, 1);
2464                 pcm->oss.reg++;
2465                 pcm->oss.reg_mask |= 2;
2466         }
2467
2468         if (pcm->oss.reg)
2469                 snd_pcm_oss_proc_init(pcm);
2470
2471         return 0;
2472 }
2473
2474 static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
2475 {
2476         if (pcm->oss.reg) {
2477                 if (pcm->oss.reg_mask & 1) {
2478                         pcm->oss.reg_mask &= ~1;
2479                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2480                                                   pcm->card, 0);
2481                 }
2482                 if (pcm->oss.reg_mask & 2) {
2483                         pcm->oss.reg_mask &= ~2;
2484                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2485                                                   pcm->card, 1);
2486                 }
2487         }
2488         return 0;
2489 }
2490
2491 static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
2492 {
2493         snd_pcm_oss_disconnect_minor(pcm);
2494         if (pcm->oss.reg) {
2495                 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2496 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2497                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2498 #endif
2499                 }
2500                 pcm->oss.reg = 0;
2501                 snd_pcm_oss_proc_done(pcm);
2502         }
2503         return 0;
2504 }
2505
2506 static snd_pcm_notify_t snd_pcm_oss_notify =
2507 {
2508         .n_register =   snd_pcm_oss_register_minor,
2509         .n_disconnect = snd_pcm_oss_disconnect_minor,
2510         .n_unregister = snd_pcm_oss_unregister_minor,
2511 };
2512
2513 static int __init alsa_pcm_oss_init(void)
2514 {
2515         int i;
2516         int err;
2517
2518         /* check device map table */
2519         for (i = 0; i < SNDRV_CARDS; i++) {
2520                 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
2521                         snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
2522                         dsp_map[i] = 0;
2523                 }
2524                 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
2525                         snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
2526                         adsp_map[i] = 1;
2527                 }
2528         }
2529         if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
2530                 return err;
2531         return 0;
2532 }
2533
2534 static void __exit alsa_pcm_oss_exit(void)
2535 {
2536         snd_pcm_notify(&snd_pcm_oss_notify, 1);
2537 }
2538
2539 module_init(alsa_pcm_oss_init)
2540 module_exit(alsa_pcm_oss_exit)