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