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