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