fedora core 6 1.2949 + vserver 2.2.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         if (mutex_lock_interruptible(&runtime->oss.params_lock))
814                 return -EINTR;
815         sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
816         params = kmalloc(sizeof(*params), GFP_KERNEL);
817         sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
818         if (!sw_params || !params || !sparams) {
819                 snd_printd("No memory\n");
820                 err = -ENOMEM;
821                 goto failure;
822         }
823
824         if (atomic_read(&substream->mmap_count))
825                 direct = 1;
826         else
827                 direct = substream->oss.setup.direct;
828
829         _snd_pcm_hw_params_any(sparams);
830         _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
831         _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
832         snd_mask_none(&mask);
833         if (atomic_read(&substream->mmap_count))
834                 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
835         else {
836                 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
837                 if (!direct)
838                         snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
839         }
840         err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
841         if (err < 0) {
842                 snd_printd("No usable accesses\n");
843                 err = -EINVAL;
844                 goto failure;
845         }
846         choose_rate(substream, sparams, runtime->oss.rate);
847         snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
848
849         format = snd_pcm_oss_format_from(runtime->oss.format);
850
851         sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
852         if (direct)
853                 sformat = format;
854         else
855                 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
856
857         if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
858                 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
859                         if (snd_mask_test(&sformat_mask, sformat) &&
860                             snd_pcm_oss_format_to(sformat) >= 0)
861                                 break;
862                 }
863                 if (sformat > SNDRV_PCM_FORMAT_LAST) {
864                         snd_printd("Cannot find a format!!!\n");
865                         err = -EINVAL;
866                         goto failure;
867                 }
868         }
869         err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
870         snd_assert(err >= 0, goto failure);
871
872         if (direct) {
873                 memcpy(params, sparams, sizeof(*params));
874         } else {
875                 _snd_pcm_hw_params_any(params);
876                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
877                                       SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
878                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
879                                       snd_pcm_oss_format_from(runtime->oss.format), 0);
880                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
881                                       runtime->oss.channels, 0);
882                 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
883                                       runtime->oss.rate, 0);
884                 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
885                          params_access(params), params_format(params),
886                          params_channels(params), params_rate(params));
887         }
888         pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
889                  params_access(sparams), params_format(sparams),
890                  params_channels(sparams), params_rate(sparams));
891
892         oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
893                          params_channels(params) / 8;
894
895 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
896         snd_pcm_oss_plugin_clear(substream);
897         if (!direct) {
898                 /* add necessary plugins */
899                 snd_pcm_oss_plugin_clear(substream);
900                 if ((err = snd_pcm_plug_format_plugins(substream,
901                                                        params, 
902                                                        sparams)) < 0) {
903                         snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
904                         snd_pcm_oss_plugin_clear(substream);
905                         goto failure;
906                 }
907                 if (runtime->oss.plugin_first) {
908                         struct snd_pcm_plugin *plugin;
909                         if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
910                                 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
911                                 snd_pcm_oss_plugin_clear(substream);
912                                 goto failure;
913                         }
914                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
915                                 err = snd_pcm_plugin_append(plugin);
916                         } else {
917                                 err = snd_pcm_plugin_insert(plugin);
918                         }
919                         if (err < 0) {
920                                 snd_pcm_oss_plugin_clear(substream);
921                                 goto failure;
922                         }
923                 }
924         }
925 #endif
926
927         err = snd_pcm_oss_period_size(substream, params, sparams);
928         if (err < 0)
929                 goto failure;
930
931         n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
932         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
933         snd_assert(err >= 0, goto failure);
934
935         err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
936                                      runtime->oss.periods, NULL);
937         snd_assert(err >= 0, goto failure);
938
939         snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
940
941         if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
942                 snd_printd("HW_PARAMS failed: %i\n", err);
943                 goto failure;
944         }
945
946         memset(sw_params, 0, sizeof(*sw_params));
947         if (runtime->oss.trigger) {
948                 sw_params->start_threshold = 1;
949         } else {
950                 sw_params->start_threshold = runtime->boundary;
951         }
952         if (atomic_read(&substream->mmap_count) ||
953             substream->stream == SNDRV_PCM_STREAM_CAPTURE)
954                 sw_params->stop_threshold = runtime->boundary;
955         else
956                 sw_params->stop_threshold = runtime->buffer_size;
957         sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
958         sw_params->period_step = 1;
959         sw_params->sleep_min = 0;
960         sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
961                 1 : runtime->period_size;
962         sw_params->xfer_align = 1;
963         if (atomic_read(&substream->mmap_count) ||
964             substream->oss.setup.nosilence) {
965                 sw_params->silence_threshold = 0;
966                 sw_params->silence_size = 0;
967         } else {
968                 snd_pcm_uframes_t frames;
969                 frames = runtime->period_size + 16;
970                 if (frames > runtime->buffer_size)
971                         frames = runtime->buffer_size;
972                 sw_params->silence_threshold = frames;
973                 sw_params->silence_size = frames;
974         }
975
976         if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
977                 snd_printd("SW_PARAMS failed: %i\n", err);
978                 goto failure;
979         }
980
981         runtime->oss.periods = params_periods(sparams);
982         oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
983         snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
984 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
985         if (runtime->oss.plugin_first) {
986                 err = snd_pcm_plug_alloc(substream, oss_period_size);
987                 if (err < 0)
988                         goto failure;
989         }
990 #endif
991         oss_period_size *= oss_frame_size;
992
993         oss_buffer_size = oss_period_size * runtime->oss.periods;
994         snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
995
996         runtime->oss.period_bytes = oss_period_size;
997         runtime->oss.buffer_bytes = oss_buffer_size;
998
999         pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1000                  runtime->oss.period_bytes,
1001                  runtime->oss.buffer_bytes);
1002         pdprintf("slave: period_size = %i, buffer_size = %i\n",
1003                  params_period_size(sparams),
1004                  params_buffer_size(sparams));
1005
1006         runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1007         runtime->oss.channels = params_channels(params);
1008         runtime->oss.rate = params_rate(params);
1009
1010         runtime->oss.params = 0;
1011         runtime->oss.prepare = 1;
1012         vfree(runtime->oss.buffer);
1013         runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1014         runtime->oss.buffer_used = 0;
1015         if (runtime->dma_area)
1016                 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1017
1018         runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1019
1020         err = 0;
1021 failure:
1022         kfree(sw_params);
1023         kfree(params);
1024         kfree(sparams);
1025         mutex_unlock(&runtime->oss.params_lock);
1026         return err;
1027 }
1028
1029 static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1030 {
1031         int idx, err;
1032         struct snd_pcm_substream *asubstream = NULL, *substream;
1033
1034         for (idx = 0; idx < 2; idx++) {
1035                 substream = pcm_oss_file->streams[idx];
1036                 if (substream == NULL)
1037                         continue;
1038                 if (asubstream == NULL)
1039                         asubstream = substream;
1040                 if (substream->runtime->oss.params) {
1041                         err = snd_pcm_oss_change_params(substream);
1042                         if (err < 0)
1043                                 return err;
1044                 }
1045         }
1046         snd_assert(asubstream != NULL, return -EIO);
1047         if (r_substream)
1048                 *r_substream = asubstream;
1049         return 0;
1050 }
1051
1052 static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1053 {
1054         int err;
1055         struct snd_pcm_runtime *runtime = substream->runtime;
1056
1057         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1058         if (err < 0) {
1059                 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1060                 return err;
1061         }
1062         runtime->oss.prepare = 0;
1063         runtime->oss.prev_hw_ptr_interrupt = 0;
1064         runtime->oss.period_ptr = 0;
1065         runtime->oss.buffer_used = 0;
1066
1067         return 0;
1068 }
1069
1070 static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1071 {
1072         struct snd_pcm_runtime *runtime;
1073         int err;
1074
1075         if (substream == NULL)
1076                 return 0;
1077         runtime = substream->runtime;
1078         if (runtime->oss.params) {
1079                 err = snd_pcm_oss_change_params(substream);
1080                 if (err < 0)
1081                         return err;
1082         }
1083         if (runtime->oss.prepare) {
1084                 err = snd_pcm_oss_prepare(substream);
1085                 if (err < 0)
1086                         return err;
1087         }
1088         return 0;
1089 }
1090
1091 static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1092 {
1093         struct snd_pcm_runtime *runtime;
1094         snd_pcm_uframes_t frames;
1095         int err = 0;
1096
1097         while (1) {
1098                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1099                 if (err < 0)
1100                         break;
1101                 runtime = substream->runtime;
1102                 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1103                         break;
1104                 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1105                 /* until avail(delay) <= buffer_size */
1106                 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1107                 frames /= runtime->period_size;
1108                 frames *= runtime->period_size;
1109                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1110                 if (err < 0)
1111                         break;
1112         }
1113         return err;
1114 }
1115
1116 snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1117 {
1118         struct snd_pcm_runtime *runtime = substream->runtime;
1119         int ret;
1120         while (1) {
1121                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1122                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1123 #ifdef OSS_DEBUG
1124                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1125                                 printk("pcm_oss: write: recovering from XRUN\n");
1126                         else
1127                                 printk("pcm_oss: write: recovering from SUSPEND\n");
1128 #endif
1129                         ret = snd_pcm_oss_prepare(substream);
1130                         if (ret < 0)
1131                                 break;
1132                 }
1133                 if (in_kernel) {
1134                         mm_segment_t fs;
1135                         fs = snd_enter_user();
1136                         ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1137                         snd_leave_user(fs);
1138                 } else {
1139                         ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1140                 }
1141                 if (ret != -EPIPE && ret != -ESTRPIPE)
1142                         break;
1143                 /* test, if we can't store new data, because the stream */
1144                 /* has not been started */
1145                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1146                         return -EAGAIN;
1147         }
1148         return ret;
1149 }
1150
1151 snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1152 {
1153         struct snd_pcm_runtime *runtime = substream->runtime;
1154         snd_pcm_sframes_t delay;
1155         int ret;
1156         while (1) {
1157                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1158                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1159 #ifdef OSS_DEBUG
1160                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1161                                 printk("pcm_oss: read: recovering from XRUN\n");
1162                         else
1163                                 printk("pcm_oss: read: recovering from SUSPEND\n");
1164 #endif
1165                         ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1166                         if (ret < 0)
1167                                 break;
1168                 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1169                         ret = snd_pcm_oss_prepare(substream);
1170                         if (ret < 0)
1171                                 break;
1172                 }
1173                 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1174                 if (ret < 0)
1175                         break;
1176                 if (in_kernel) {
1177                         mm_segment_t fs;
1178                         fs = snd_enter_user();
1179                         ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1180                         snd_leave_user(fs);
1181                 } else {
1182                         ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1183                 }
1184                 if (ret == -EPIPE) {
1185                         if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1186                                 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1187                                 if (ret < 0)
1188                                         break;
1189                         }
1190                         continue;
1191                 }
1192                 if (ret != -ESTRPIPE)
1193                         break;
1194         }
1195         return ret;
1196 }
1197
1198 snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1199 {
1200         struct snd_pcm_runtime *runtime = substream->runtime;
1201         int ret;
1202         while (1) {
1203                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1204                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1205 #ifdef OSS_DEBUG
1206                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1207                                 printk("pcm_oss: writev: recovering from XRUN\n");
1208                         else
1209                                 printk("pcm_oss: writev: recovering from SUSPEND\n");
1210 #endif
1211                         ret = snd_pcm_oss_prepare(substream);
1212                         if (ret < 0)
1213                                 break;
1214                 }
1215                 if (in_kernel) {
1216                         mm_segment_t fs;
1217                         fs = snd_enter_user();
1218                         ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1219                         snd_leave_user(fs);
1220                 } else {
1221                         ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1222                 }
1223                 if (ret != -EPIPE && ret != -ESTRPIPE)
1224                         break;
1225
1226                 /* test, if we can't store new data, because the stream */
1227                 /* has not been started */
1228                 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1229                         return -EAGAIN;
1230         }
1231         return ret;
1232 }
1233         
1234 snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
1235 {
1236         struct snd_pcm_runtime *runtime = substream->runtime;
1237         int ret;
1238         while (1) {
1239                 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1240                     runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1241 #ifdef OSS_DEBUG
1242                         if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1243                                 printk("pcm_oss: readv: recovering from XRUN\n");
1244                         else
1245                                 printk("pcm_oss: readv: recovering from SUSPEND\n");
1246 #endif
1247                         ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1248                         if (ret < 0)
1249                                 break;
1250                 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1251                         ret = snd_pcm_oss_prepare(substream);
1252                         if (ret < 0)
1253                                 break;
1254                 }
1255                 if (in_kernel) {
1256                         mm_segment_t fs;
1257                         fs = snd_enter_user();
1258                         ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1259                         snd_leave_user(fs);
1260                 } else {
1261                         ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1262                 }
1263                 if (ret != -EPIPE && ret != -ESTRPIPE)
1264                         break;
1265         }
1266         return ret;
1267 }
1268
1269 static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1270 {
1271         struct snd_pcm_runtime *runtime = substream->runtime;
1272         snd_pcm_sframes_t frames, frames1;
1273 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1274         if (runtime->oss.plugin_first) {
1275                 struct snd_pcm_plugin_channel *channels;
1276                 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1277                 if (!in_kernel) {
1278                         if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1279                                 return -EFAULT;
1280                         buf = runtime->oss.buffer;
1281                 }
1282                 frames = bytes / oss_frame_bytes;
1283                 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1284                 if (frames1 < 0)
1285                         return frames1;
1286                 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1287                 if (frames1 <= 0)
1288                         return frames1;
1289                 bytes = frames1 * oss_frame_bytes;
1290         } else
1291 #endif
1292         {
1293                 frames = bytes_to_frames(runtime, bytes);
1294                 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1295                 if (frames1 <= 0)
1296                         return frames1;
1297                 bytes = frames_to_bytes(runtime, frames1);
1298         }
1299         return bytes;
1300 }
1301
1302 static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1303 {
1304         size_t xfer = 0;
1305         ssize_t tmp;
1306         struct snd_pcm_runtime *runtime = substream->runtime;
1307
1308         if (atomic_read(&substream->mmap_count))
1309                 return -ENXIO;
1310
1311         if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1312                 return tmp;
1313         mutex_lock(&runtime->oss.params_lock);
1314         while (bytes > 0) {
1315                 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1316                         tmp = bytes;
1317                         if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1318                                 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1319                         if (tmp > 0) {
1320                                 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1321                                         tmp = -EFAULT;
1322                                         goto err;
1323                                 }
1324                         }
1325                         runtime->oss.buffer_used += tmp;
1326                         buf += tmp;
1327                         bytes -= tmp;
1328                         xfer += tmp;
1329                         if (substream->oss.setup.partialfrag ||
1330                             runtime->oss.buffer_used == runtime->oss.period_bytes) {
1331                                 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr, 
1332                                                          runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1333                                 if (tmp <= 0)
1334                                         goto err;
1335                                 runtime->oss.bytes += tmp;
1336                                 runtime->oss.period_ptr += tmp;
1337                                 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1338                                 if (runtime->oss.period_ptr == 0 ||
1339                                     runtime->oss.period_ptr == runtime->oss.buffer_used)
1340                                         runtime->oss.buffer_used = 0;
1341                                 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1342                                         tmp = -EAGAIN;
1343                                         goto err;
1344                                 }
1345                         }
1346                 } else {
1347                         tmp = snd_pcm_oss_write2(substream,
1348                                                  (const char __force *)buf,
1349                                                  runtime->oss.period_bytes, 0);
1350                         if (tmp <= 0)
1351                                 goto err;
1352                         runtime->oss.bytes += tmp;
1353                         buf += tmp;
1354                         bytes -= tmp;
1355                         xfer += tmp;
1356                         if ((substream->f_flags & O_NONBLOCK) != 0 &&
1357                             tmp != runtime->oss.period_bytes)
1358                                 break;
1359                 }
1360         }
1361         mutex_unlock(&runtime->oss.params_lock);
1362         return xfer;
1363
1364  err:
1365         mutex_unlock(&runtime->oss.params_lock);
1366         return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1367 }
1368
1369 static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1370 {
1371         struct snd_pcm_runtime *runtime = substream->runtime;
1372         snd_pcm_sframes_t frames, frames1;
1373 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
1374         char __user *final_dst = (char __user *)buf;
1375         if (runtime->oss.plugin_first) {
1376                 struct snd_pcm_plugin_channel *channels;
1377                 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1378                 if (!in_kernel)
1379                         buf = runtime->oss.buffer;
1380                 frames = bytes / oss_frame_bytes;
1381                 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1382                 if (frames1 < 0)
1383                         return frames1;
1384                 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1385                 if (frames1 <= 0)
1386                         return frames1;
1387                 bytes = frames1 * oss_frame_bytes;
1388                 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1389                         return -EFAULT;
1390         } else
1391 #endif
1392         {
1393                 frames = bytes_to_frames(runtime, bytes);
1394                 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1395                 if (frames1 <= 0)
1396                         return frames1;
1397                 bytes = frames_to_bytes(runtime, frames1);
1398         }
1399         return bytes;
1400 }
1401
1402 static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1403 {
1404         size_t xfer = 0;
1405         ssize_t tmp;
1406         struct snd_pcm_runtime *runtime = substream->runtime;
1407
1408         if (atomic_read(&substream->mmap_count))
1409                 return -ENXIO;
1410
1411         if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1412                 return tmp;
1413         mutex_lock(&runtime->oss.params_lock);
1414         while (bytes > 0) {
1415                 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1416                         if (runtime->oss.buffer_used == 0) {
1417                                 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1418                                 if (tmp <= 0)
1419                                         goto err;
1420                                 runtime->oss.bytes += tmp;
1421                                 runtime->oss.period_ptr = tmp;
1422                                 runtime->oss.buffer_used = tmp;
1423                         }
1424                         tmp = bytes;
1425                         if ((size_t) tmp > runtime->oss.buffer_used)
1426                                 tmp = runtime->oss.buffer_used;
1427                         if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1428                                 tmp = -EFAULT;
1429                                 goto err;
1430                         }
1431                         buf += tmp;
1432                         bytes -= tmp;
1433                         xfer += tmp;
1434                         runtime->oss.buffer_used -= tmp;
1435                 } else {
1436                         tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1437                                                 runtime->oss.period_bytes, 0);
1438                         if (tmp <= 0)
1439                                 goto err;
1440                         runtime->oss.bytes += tmp;
1441                         buf += tmp;
1442                         bytes -= tmp;
1443                         xfer += tmp;
1444                 }
1445         }
1446         mutex_unlock(&runtime->oss.params_lock);
1447         return xfer;
1448
1449  err:
1450         mutex_unlock(&runtime->oss.params_lock);
1451         return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1452 }
1453
1454 static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1455 {
1456         struct snd_pcm_substream *substream;
1457
1458         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459         if (substream != NULL) {
1460                 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1461                 substream->runtime->oss.prepare = 1;
1462         }
1463         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1464         if (substream != NULL) {
1465                 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1466                 substream->runtime->oss.prepare = 1;
1467         }
1468         return 0;
1469 }
1470
1471 static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1472 {
1473         struct snd_pcm_substream *substream;
1474         int err;
1475
1476         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1477         if (substream != NULL) {
1478                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1479                         return err;
1480                 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1481         }
1482         /* note: all errors from the start action are ignored */
1483         /* OSS apps do not know, how to handle them */
1484         return 0;
1485 }
1486
1487 static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1488 {
1489         struct snd_pcm_runtime *runtime;
1490         ssize_t result = 0;
1491         long res;
1492         wait_queue_t wait;
1493         static char printed_comm[16];
1494
1495         if (strncmp(printed_comm, current->comm, 16)) {
1496                 printk(KERN_DEBUG "application %s uses obsolete OSS audio interface\n",
1497                        current->comm);
1498                 memcpy(printed_comm, current->comm, 16);
1499         }
1500
1501         runtime = substream->runtime;
1502         init_waitqueue_entry(&wait, current);
1503         add_wait_queue(&runtime->sleep, &wait);
1504 #ifdef OSS_DEBUG
1505         printk("sync1: size = %li\n", size);
1506 #endif
1507         while (1) {
1508                 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1509                 if (result > 0) {
1510                         runtime->oss.buffer_used = 0;
1511                         result = 0;
1512                         break;
1513                 }
1514                 if (result != 0 && result != -EAGAIN)
1515                         break;
1516                 result = 0;
1517                 set_current_state(TASK_INTERRUPTIBLE);
1518                 snd_pcm_stream_lock_irq(substream);
1519                 res = runtime->status->state;
1520                 snd_pcm_stream_unlock_irq(substream);
1521                 if (res != SNDRV_PCM_STATE_RUNNING) {
1522                         set_current_state(TASK_RUNNING);
1523                         break;
1524                 }
1525                 res = schedule_timeout(10 * HZ);
1526                 if (signal_pending(current)) {
1527                         result = -ERESTARTSYS;
1528                         break;
1529                 }
1530                 if (res == 0) {
1531                         snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1532                         result = -EIO;
1533                         break;
1534                 }
1535         }
1536         remove_wait_queue(&runtime->sleep, &wait);
1537         return result;
1538 }
1539
1540 static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1541 {
1542         int err = 0;
1543         unsigned int saved_f_flags;
1544         struct snd_pcm_substream *substream;
1545         struct snd_pcm_runtime *runtime;
1546         snd_pcm_format_t format;
1547         unsigned long width;
1548         size_t size;
1549
1550         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1551         if (substream != NULL) {
1552                 runtime = substream->runtime;
1553                 if (atomic_read(&substream->mmap_count))
1554                         goto __direct;
1555                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1556                         return err;
1557                 format = snd_pcm_oss_format_from(runtime->oss.format);
1558                 width = snd_pcm_format_physical_width(format);
1559                 mutex_lock(&runtime->oss.params_lock);
1560                 if (runtime->oss.buffer_used > 0) {
1561 #ifdef OSS_DEBUG
1562                         printk("sync: buffer_used\n");
1563 #endif
1564                         size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1565                         snd_pcm_format_set_silence(format,
1566                                                    runtime->oss.buffer + runtime->oss.buffer_used,
1567                                                    size);
1568                         err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1569                         if (err < 0) {
1570                                 mutex_unlock(&runtime->oss.params_lock);
1571                                 return err;
1572                         }
1573                 } else if (runtime->oss.period_ptr > 0) {
1574 #ifdef OSS_DEBUG
1575                         printk("sync: period_ptr\n");
1576 #endif
1577                         size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1578                         snd_pcm_format_set_silence(format,
1579                                                    runtime->oss.buffer,
1580                                                    size * 8 / width);
1581                         err = snd_pcm_oss_sync1(substream, size);
1582                         if (err < 0) {
1583                                 mutex_unlock(&runtime->oss.params_lock);
1584                                 return err;
1585                         }
1586                 }
1587                 /*
1588                  * The ALSA's period might be a bit large than OSS one.
1589                  * Fill the remain portion of ALSA period with zeros.
1590                  */
1591                 size = runtime->control->appl_ptr % runtime->period_size;
1592                 if (size > 0) {
1593                         size = runtime->period_size - size;
1594                         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1595                                 size = (runtime->frame_bits * size) / 8;
1596                                 while (size > 0) {
1597                                         mm_segment_t fs;
1598                                         size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1599                                         size -= size1;
1600                                         size1 *= 8;
1601                                         size1 /= runtime->sample_bits;
1602                                         snd_pcm_format_set_silence(runtime->format,
1603                                                                    runtime->oss.buffer,
1604                                                                    size1);
1605                                         fs = snd_enter_user();
1606                                         snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1607                                         snd_leave_user(fs);
1608                                 }
1609                         } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1610                                 void __user *buffers[runtime->channels];
1611                                 memset(buffers, 0, runtime->channels * sizeof(void *));
1612                                 snd_pcm_lib_writev(substream, buffers, size);
1613                         }
1614                 }
1615                 mutex_unlock(&runtime->oss.params_lock);
1616                 /*
1617                  * finish sync: drain the buffer
1618                  */
1619               __direct:
1620                 saved_f_flags = substream->f_flags;
1621                 substream->f_flags &= ~O_NONBLOCK;
1622                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1623                 substream->f_flags = saved_f_flags;
1624                 if (err < 0)
1625                         return err;
1626                 runtime->oss.prepare = 1;
1627         }
1628
1629         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1630         if (substream != NULL) {
1631                 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1632                         return err;
1633                 runtime = substream->runtime;
1634                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1635                 if (err < 0)
1636                         return err;
1637                 runtime->oss.buffer_used = 0;
1638                 runtime->oss.prepare = 1;
1639         }
1640         return 0;
1641 }
1642
1643 static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1644 {
1645         int idx;
1646
1647         for (idx = 1; idx >= 0; --idx) {
1648                 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1649                 struct snd_pcm_runtime *runtime;
1650                 if (substream == NULL)
1651                         continue;
1652                 runtime = substream->runtime;
1653                 if (rate < 1000)
1654                         rate = 1000;
1655                 else if (rate > 192000)
1656                         rate = 192000;
1657                 if (runtime->oss.rate != rate) {
1658                         runtime->oss.params = 1;
1659                         runtime->oss.rate = rate;
1660                 }
1661         }
1662         return snd_pcm_oss_get_rate(pcm_oss_file);
1663 }
1664
1665 static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1666 {
1667         struct snd_pcm_substream *substream;
1668         int err;
1669         
1670         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1671                 return err;
1672         return substream->runtime->oss.rate;
1673 }
1674
1675 static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1676 {
1677         int idx;
1678         if (channels < 1)
1679                 channels = 1;
1680         if (channels > 128)
1681                 return -EINVAL;
1682         for (idx = 1; idx >= 0; --idx) {
1683                 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1684                 struct snd_pcm_runtime *runtime;
1685                 if (substream == NULL)
1686                         continue;
1687                 runtime = substream->runtime;
1688                 if (runtime->oss.channels != channels) {
1689                         runtime->oss.params = 1;
1690                         runtime->oss.channels = channels;
1691                 }
1692         }
1693         return snd_pcm_oss_get_channels(pcm_oss_file);
1694 }
1695
1696 static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1697 {
1698         struct snd_pcm_substream *substream;
1699         int err;
1700         
1701         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1702                 return err;
1703         return substream->runtime->oss.channels;
1704 }
1705
1706 static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1707 {
1708         struct snd_pcm_substream *substream;
1709         int err;
1710         
1711         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1712                 return err;
1713         return substream->runtime->oss.period_bytes;
1714 }
1715
1716 static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1717 {
1718         struct snd_pcm_substream *substream;
1719         int err;
1720         int direct;
1721         struct snd_pcm_hw_params *params;
1722         unsigned int formats = 0;
1723         struct snd_mask format_mask;
1724         int fmt;
1725
1726         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1727                 return err;
1728         if (atomic_read(&substream->mmap_count))
1729                 direct = 1;
1730         else
1731                 direct = substream->oss.setup.direct;
1732         if (!direct)
1733                 return AFMT_MU_LAW | AFMT_U8 |
1734                        AFMT_S16_LE | AFMT_S16_BE |
1735                        AFMT_S8 | AFMT_U16_LE |
1736                        AFMT_U16_BE;
1737         params = kmalloc(sizeof(*params), GFP_KERNEL);
1738         if (!params)
1739                 return -ENOMEM;
1740         _snd_pcm_hw_params_any(params);
1741         err = snd_pcm_hw_refine(substream, params);
1742         format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 
1743         kfree(params);
1744         snd_assert(err >= 0, return err);
1745         for (fmt = 0; fmt < 32; ++fmt) {
1746                 if (snd_mask_test(&format_mask, fmt)) {
1747                         int f = snd_pcm_oss_format_to(fmt);
1748                         if (f >= 0)
1749                                 formats |= f;
1750                 }
1751         }
1752         return formats;
1753 }
1754
1755 static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1756 {
1757         int formats, idx;
1758         
1759         if (format != AFMT_QUERY) {
1760                 formats = snd_pcm_oss_get_formats(pcm_oss_file);
1761                 if (formats < 0)
1762                         return formats;
1763                 if (!(formats & format))
1764                         format = AFMT_U8;
1765                 for (idx = 1; idx >= 0; --idx) {
1766                         struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1767                         struct snd_pcm_runtime *runtime;
1768                         if (substream == NULL)
1769                                 continue;
1770                         runtime = substream->runtime;
1771                         if (runtime->oss.format != format) {
1772                                 runtime->oss.params = 1;
1773                                 runtime->oss.format = format;
1774                         }
1775                 }
1776         }
1777         return snd_pcm_oss_get_format(pcm_oss_file);
1778 }
1779
1780 static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1781 {
1782         struct snd_pcm_substream *substream;
1783         int err;
1784         
1785         if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1786                 return err;
1787         return substream->runtime->oss.format;
1788 }
1789
1790 static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1791 {
1792         struct snd_pcm_runtime *runtime;
1793
1794         if (substream == NULL)
1795                 return 0;
1796         runtime = substream->runtime;
1797         if (subdivide == 0) {
1798                 subdivide = runtime->oss.subdivision;
1799                 if (subdivide == 0)
1800                         subdivide = 1;
1801                 return subdivide;
1802         }
1803         if (runtime->oss.subdivision || runtime->oss.fragshift)
1804                 return -EINVAL;
1805         if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1806             subdivide != 8 && subdivide != 16)
1807                 return -EINVAL;
1808         runtime->oss.subdivision = subdivide;
1809         runtime->oss.params = 1;
1810         return subdivide;
1811 }
1812
1813 static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1814 {
1815         int err = -EINVAL, idx;
1816
1817         for (idx = 1; idx >= 0; --idx) {
1818                 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1819                 if (substream == NULL)
1820                         continue;
1821                 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1822                         return err;
1823         }
1824         return err;
1825 }
1826
1827 static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1828 {
1829         struct snd_pcm_runtime *runtime;
1830
1831         if (substream == NULL)
1832                 return 0;
1833         runtime = substream->runtime;
1834         if (runtime->oss.subdivision || runtime->oss.fragshift)
1835                 return -EINVAL;
1836         runtime->oss.fragshift = val & 0xffff;
1837         runtime->oss.maxfrags = (val >> 16) & 0xffff;
1838         if (runtime->oss.fragshift < 4)         /* < 16 */
1839                 runtime->oss.fragshift = 4;
1840         if (runtime->oss.maxfrags < 2)
1841                 runtime->oss.maxfrags = 2;
1842         runtime->oss.params = 1;
1843         return 0;
1844 }
1845
1846 static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1847 {
1848         int err = -EINVAL, idx;
1849
1850         for (idx = 1; idx >= 0; --idx) {
1851                 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1852                 if (substream == NULL)
1853                         continue;
1854                 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1855                         return err;
1856         }
1857         return err;
1858 }
1859
1860 static int snd_pcm_oss_nonblock(struct file * file)
1861 {
1862         file->f_flags |= O_NONBLOCK;
1863         return 0;
1864 }
1865
1866 static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
1867 {
1868
1869         if (substream == NULL) {
1870                 res &= ~DSP_CAP_DUPLEX;
1871                 return res;
1872         }
1873 #ifdef DSP_CAP_MULTI
1874         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1875                 if (substream->pstr->substream_count > 1)
1876                         res |= DSP_CAP_MULTI;
1877 #endif
1878         /* DSP_CAP_REALTIME is set all times: */
1879         /* all ALSA drivers can return actual pointer in ring buffer */
1880 #if defined(DSP_CAP_REALTIME) && 0
1881         {
1882                 struct snd_pcm_runtime *runtime = substream->runtime;
1883                 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1884                         res &= ~DSP_CAP_REALTIME;
1885         }
1886 #endif
1887         return res;
1888 }
1889
1890 static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
1891 {
1892         int result, idx;
1893         
1894         result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1895         for (idx = 0; idx < 2; idx++) {
1896                 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1897                 result = snd_pcm_oss_get_caps1(substream, result);
1898         }
1899         result |= 0x0001;       /* revision - same as SB AWE 64 */
1900         return result;
1901 }
1902
1903 static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
1904 {
1905         struct snd_pcm_runtime *runtime = substream->runtime;
1906         snd_pcm_uframes_t appl_ptr;
1907         appl_ptr = hw_ptr + runtime->buffer_size;
1908         appl_ptr %= runtime->boundary;
1909         runtime->control->appl_ptr = appl_ptr;
1910 }
1911
1912 static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
1913 {
1914         struct snd_pcm_runtime *runtime;
1915         struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1916         int err, cmd;
1917
1918 #ifdef OSS_DEBUG
1919         printk("pcm_oss: trigger = 0x%x\n", trigger);
1920 #endif
1921         
1922         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1923         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1924
1925         if (psubstream) {
1926                 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1927                         return err;
1928         }
1929         if (csubstream) {
1930                 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1931                         return err;
1932         }
1933         if (psubstream) {
1934                 runtime = psubstream->runtime;
1935                 if (trigger & PCM_ENABLE_OUTPUT) {
1936                         if (runtime->oss.trigger)
1937                                 goto _skip1;
1938                         if (atomic_read(&psubstream->mmap_count))
1939                                 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1940                         runtime->oss.trigger = 1;
1941                         runtime->start_threshold = 1;
1942                         cmd = SNDRV_PCM_IOCTL_START;
1943                 } else {
1944                         if (!runtime->oss.trigger)
1945                                 goto _skip1;
1946                         runtime->oss.trigger = 0;
1947                         runtime->start_threshold = runtime->boundary;
1948                         cmd = SNDRV_PCM_IOCTL_DROP;
1949                         runtime->oss.prepare = 1;
1950                 }
1951                 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
1952                 if (err < 0)
1953                         return err;
1954         }
1955  _skip1:
1956         if (csubstream) {
1957                 runtime = csubstream->runtime;
1958                 if (trigger & PCM_ENABLE_INPUT) {
1959                         if (runtime->oss.trigger)
1960                                 goto _skip2;
1961                         runtime->oss.trigger = 1;
1962                         runtime->start_threshold = 1;
1963                         cmd = SNDRV_PCM_IOCTL_START;
1964                 } else {
1965                         if (!runtime->oss.trigger)
1966                                 goto _skip2;
1967                         runtime->oss.trigger = 0;
1968                         runtime->start_threshold = runtime->boundary;
1969                         cmd = SNDRV_PCM_IOCTL_DROP;
1970                         runtime->oss.prepare = 1;
1971                 }
1972                 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
1973                 if (err < 0)
1974                         return err;
1975         }
1976  _skip2:
1977         return 0;
1978 }
1979
1980 static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
1981 {
1982         struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1983         int result = 0;
1984
1985         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1986         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1987         if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1988                 result |= PCM_ENABLE_OUTPUT;
1989         if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1990                 result |= PCM_ENABLE_INPUT;
1991         return result;
1992 }
1993
1994 static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
1995 {
1996         struct snd_pcm_substream *substream;
1997         struct snd_pcm_runtime *runtime;
1998         snd_pcm_sframes_t delay;
1999         int err;
2000
2001         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
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                 return 0;
2009         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2010         if (err == -EPIPE)
2011                 delay = 0;      /* hack for broken OSS applications */
2012         else if (err < 0)
2013                 return err;
2014         return snd_pcm_oss_bytes(substream, delay);
2015 }
2016
2017 static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2018 {       
2019         struct snd_pcm_substream *substream;
2020         struct snd_pcm_runtime *runtime;
2021         snd_pcm_sframes_t delay;
2022         int fixup;
2023         struct count_info info;
2024         int err;
2025
2026         if (_info == NULL)
2027                 return -EFAULT;
2028         substream = pcm_oss_file->streams[stream];
2029         if (substream == NULL)
2030                 return -EINVAL;
2031         if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2032                 return err;
2033         runtime = substream->runtime;
2034         if (runtime->oss.params || runtime->oss.prepare) {
2035                 memset(&info, 0, sizeof(info));
2036                 if (copy_to_user(_info, &info, sizeof(info)))
2037                         return -EFAULT;
2038                 return 0;
2039         }
2040         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2041                 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2042                 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2043                         err = 0;
2044                         delay = 0;
2045                         fixup = 0;
2046                 } else {
2047                         fixup = runtime->oss.buffer_used;
2048                 }
2049         } else {
2050                 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2051                 fixup = -runtime->oss.buffer_used;
2052         }
2053         if (err < 0)
2054                 return err;
2055         info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2056         if (atomic_read(&substream->mmap_count)) {
2057                 snd_pcm_sframes_t n;
2058                 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2059                 if (n < 0)
2060                         n += runtime->boundary;
2061                 info.blocks = n / runtime->period_size;
2062                 runtime->oss.prev_hw_ptr_interrupt = delay;
2063                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2064                         snd_pcm_oss_simulate_fill(substream, delay);
2065                 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2066         } else {
2067                 delay = snd_pcm_oss_bytes(substream, delay);
2068                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2069                         if (substream->oss.setup.buggyptr)
2070                                 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2071                         else
2072                                 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2073                         info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2074                 } else {
2075                         delay += fixup;
2076                         info.blocks = delay / runtime->oss.period_bytes;
2077                         info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2078                 }
2079         }
2080         if (copy_to_user(_info, &info, sizeof(info)))
2081                 return -EFAULT;
2082         return 0;
2083 }
2084
2085 static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2086 {
2087         struct snd_pcm_substream *substream;
2088         struct snd_pcm_runtime *runtime;
2089         snd_pcm_sframes_t avail;
2090         int fixup;
2091         struct audio_buf_info info;
2092         int err;
2093
2094         if (_info == NULL)
2095                 return -EFAULT;
2096         substream = pcm_oss_file->streams[stream];
2097         if (substream == NULL)
2098                 return -EINVAL;
2099         runtime = substream->runtime;
2100
2101         if (runtime->oss.params &&
2102             (err = snd_pcm_oss_change_params(substream)) < 0)
2103                 return err;
2104
2105         info.fragsize = runtime->oss.period_bytes;
2106         info.fragstotal = runtime->periods;
2107         if (runtime->oss.prepare) {
2108                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2109                         info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2110                         info.fragments = runtime->oss.periods;
2111                 } else {
2112                         info.bytes = 0;
2113                         info.fragments = 0;
2114                 }
2115         } else {
2116                 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2117                         err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2118                         if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2119                                 avail = runtime->buffer_size;
2120                                 err = 0;
2121                                 fixup = 0;
2122                         } else {
2123                                 avail = runtime->buffer_size - avail;
2124                                 fixup = -runtime->oss.buffer_used;
2125                         }
2126                 } else {
2127                         err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2128                         fixup = runtime->oss.buffer_used;
2129                 }
2130                 if (err < 0)
2131                         return err;
2132                 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2133                 info.fragments = info.bytes / runtime->oss.period_bytes;
2134         }
2135
2136 #ifdef OSS_DEBUG
2137         printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2138 #endif
2139         if (copy_to_user(_info, &info, sizeof(info)))
2140                 return -EFAULT;
2141         return 0;
2142 }
2143
2144 static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2145 {
2146         // it won't be probably implemented
2147         // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2148         return -EINVAL;
2149 }
2150
2151 static const char *strip_task_path(const char *path)
2152 {
2153         const char *ptr, *ptrl = NULL;
2154         for (ptr = path; *ptr; ptr++) {
2155                 if (*ptr == '/')
2156                         ptrl = ptr + 1;
2157         }
2158         return ptrl;
2159 }
2160
2161 static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2162                                       const char *task_name,
2163                                       struct snd_pcm_oss_setup *rsetup)
2164 {
2165         struct snd_pcm_oss_setup *setup;
2166
2167         mutex_lock(&pcm->streams[stream].oss.setup_mutex);
2168         do {
2169                 for (setup = pcm->streams[stream].oss.setup_list; setup;
2170                      setup = setup->next) {
2171                         if (!strcmp(setup->task_name, task_name))
2172                                 goto out;
2173                 }
2174         } while ((task_name = strip_task_path(task_name)) != NULL);
2175  out:
2176         if (setup)
2177                 *rsetup = *setup;
2178         mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
2179 }
2180
2181 static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2182 {
2183         struct snd_pcm_runtime *runtime;
2184         runtime = substream->runtime;
2185         vfree(runtime->oss.buffer);
2186         runtime->oss.buffer = NULL;
2187 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2188         snd_pcm_oss_plugin_clear(substream);
2189 #endif
2190         substream->oss.oss = 0;
2191 }
2192
2193 static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2194                                        struct snd_pcm_oss_setup *setup,
2195                                        int minor)
2196 {
2197         struct snd_pcm_runtime *runtime;
2198
2199         substream->oss.oss = 1;
2200         substream->oss.setup = *setup;
2201         if (setup->nonblock)
2202                 substream->f_flags |= O_NONBLOCK;
2203         else if (setup->block)
2204                 substream->f_flags &= ~O_NONBLOCK;
2205         runtime = substream->runtime;
2206         runtime->oss.params = 1;
2207         runtime->oss.trigger = 1;
2208         runtime->oss.rate = 8000;
2209         mutex_init(&runtime->oss.params_lock);
2210         switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2211         case SNDRV_MINOR_OSS_PCM_8:
2212                 runtime->oss.format = AFMT_U8;
2213                 break;
2214         case SNDRV_MINOR_OSS_PCM_16:
2215                 runtime->oss.format = AFMT_S16_LE;
2216                 break;
2217         default:
2218                 runtime->oss.format = AFMT_MU_LAW;
2219         }
2220         runtime->oss.channels = 1;
2221         runtime->oss.fragshift = 0;
2222         runtime->oss.maxfrags = 0;
2223         runtime->oss.subdivision = 0;
2224         substream->pcm_release = snd_pcm_oss_release_substream;
2225 }
2226
2227 static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2228 {
2229         int cidx;
2230         snd_assert(pcm_oss_file != NULL, return -ENXIO);
2231         for (cidx = 0; cidx < 2; ++cidx) {
2232                 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2233                 if (substream)
2234                         snd_pcm_release_substream(substream);
2235         }
2236         kfree(pcm_oss_file);
2237         return 0;
2238 }
2239
2240 static int snd_pcm_oss_open_file(struct file *file,
2241                                  struct snd_pcm *pcm,
2242                                  struct snd_pcm_oss_file **rpcm_oss_file,
2243                                  int minor,
2244                                  struct snd_pcm_oss_setup *setup)
2245 {
2246         int idx, err;
2247         struct snd_pcm_oss_file *pcm_oss_file;
2248         struct snd_pcm_substream *substream;
2249         unsigned int f_mode = file->f_mode;
2250
2251         snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2252         *rpcm_oss_file = NULL;
2253
2254         pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2255         if (pcm_oss_file == NULL)
2256                 return -ENOMEM;
2257
2258         if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2259             (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2260                 f_mode = FMODE_WRITE;
2261
2262         file->f_flags &= ~O_APPEND;
2263         for (idx = 0; idx < 2; idx++) {
2264                 if (setup[idx].disable)
2265                         continue;
2266                 if (! pcm->streams[idx].substream_count)
2267                         continue; /* no matching substream */
2268                 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2269                         if (! (f_mode & FMODE_WRITE))
2270                                 continue;
2271                 } else {
2272                         if (! (f_mode & FMODE_READ))
2273                                 continue;
2274                 }
2275                 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2276                 if (err < 0) {
2277                         snd_pcm_oss_release_file(pcm_oss_file);
2278                         return err;
2279                 }
2280
2281                 pcm_oss_file->streams[idx] = substream;
2282                 substream->file = pcm_oss_file;
2283                 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2284         }
2285         
2286         if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2287                 snd_pcm_oss_release_file(pcm_oss_file);
2288                 return -EINVAL;
2289         }
2290
2291         file->private_data = pcm_oss_file;
2292         *rpcm_oss_file = pcm_oss_file;
2293         return 0;
2294 }
2295
2296
2297 static int snd_task_name(struct task_struct *task, char *name, size_t size)
2298 {
2299         unsigned int idx;
2300
2301         snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2302         for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2303                 name[idx] = task->comm[idx];
2304         name[idx] = '\0';
2305         return 0;
2306 }
2307
2308 static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2309 {
2310         int err;
2311         char task_name[32];
2312         struct snd_pcm *pcm;
2313         struct snd_pcm_oss_file *pcm_oss_file;
2314         struct snd_pcm_oss_setup setup[2];
2315         int nonblock;
2316         wait_queue_t wait;
2317
2318         pcm = snd_lookup_oss_minor_data(iminor(inode),
2319                                         SNDRV_OSS_DEVICE_TYPE_PCM);
2320         if (pcm == NULL) {
2321                 err = -ENODEV;
2322                 goto __error1;
2323         }
2324         err = snd_card_file_add(pcm->card, file);
2325         if (err < 0)
2326                 goto __error1;
2327         if (!try_module_get(pcm->card->module)) {
2328                 err = -EFAULT;
2329                 goto __error2;
2330         }
2331         if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2332                 err = -EFAULT;
2333                 goto __error;
2334         }
2335         memset(setup, 0, sizeof(setup));
2336         if (file->f_mode & FMODE_WRITE)
2337                 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2338                                            task_name, &setup[0]);
2339         if (file->f_mode & FMODE_READ)
2340                 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2341                                            task_name, &setup[1]);
2342
2343         nonblock = !!(file->f_flags & O_NONBLOCK);
2344         if (!nonblock)
2345                 nonblock = nonblock_open;
2346
2347         init_waitqueue_entry(&wait, current);
2348         add_wait_queue(&pcm->open_wait, &wait);
2349         mutex_lock(&pcm->open_mutex);
2350         while (1) {
2351                 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2352                                             iminor(inode), setup);
2353                 if (err >= 0)
2354                         break;
2355                 if (err == -EAGAIN) {
2356                         if (nonblock) {
2357                                 err = -EBUSY;
2358                                 break;
2359                         }
2360                 } else
2361                         break;
2362                 set_current_state(TASK_INTERRUPTIBLE);
2363                 mutex_unlock(&pcm->open_mutex);
2364                 schedule();
2365                 mutex_lock(&pcm->open_mutex);
2366                 if (signal_pending(current)) {
2367                         err = -ERESTARTSYS;
2368                         break;
2369                 }
2370         }
2371         remove_wait_queue(&pcm->open_wait, &wait);
2372         mutex_unlock(&pcm->open_mutex);
2373         if (err < 0)
2374                 goto __error;
2375         return err;
2376
2377       __error:
2378         module_put(pcm->card->module);
2379       __error2:
2380         snd_card_file_remove(pcm->card, file);
2381       __error1:
2382         return err;
2383 }
2384
2385 static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2386 {
2387         struct snd_pcm *pcm;
2388         struct snd_pcm_substream *substream;
2389         struct snd_pcm_oss_file *pcm_oss_file;
2390
2391         pcm_oss_file = file->private_data;
2392         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2393         if (substream == NULL)
2394                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2395         snd_assert(substream != NULL, return -ENXIO);
2396         pcm = substream->pcm;
2397         if (!pcm->card->shutdown)
2398                 snd_pcm_oss_sync(pcm_oss_file);
2399         mutex_lock(&pcm->open_mutex);
2400         snd_pcm_oss_release_file(pcm_oss_file);
2401         mutex_unlock(&pcm->open_mutex);
2402         wake_up(&pcm->open_wait);
2403         module_put(pcm->card->module);
2404         snd_card_file_remove(pcm->card, file);
2405         return 0;
2406 }
2407
2408 static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2409 {
2410         struct snd_pcm_oss_file *pcm_oss_file;
2411         int __user *p = (int __user *)arg;
2412         int res;
2413
2414         pcm_oss_file = file->private_data;
2415         if (cmd == OSS_GETVERSION)
2416                 return put_user(SNDRV_OSS_VERSION, p);
2417         if (cmd == OSS_ALSAEMULVER)
2418                 return put_user(1, p);
2419 #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2420         if (((cmd >> 8) & 0xff) == 'M') {       /* mixer ioctl - for OSS compatibility */
2421                 struct snd_pcm_substream *substream;
2422                 int idx;
2423                 for (idx = 0; idx < 2; ++idx) {
2424                         substream = pcm_oss_file->streams[idx];
2425                         if (substream != NULL)
2426                                 break;
2427                 }
2428                 snd_assert(substream != NULL, return -ENXIO);
2429                 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2430         }
2431 #endif
2432         if (((cmd >> 8) & 0xff) != 'P')
2433                 return -EINVAL;
2434 #ifdef OSS_DEBUG
2435         printk("pcm_oss: ioctl = 0x%x\n", cmd);
2436 #endif
2437         switch (cmd) {
2438         case SNDCTL_DSP_RESET:
2439                 return snd_pcm_oss_reset(pcm_oss_file);
2440         case SNDCTL_DSP_SYNC:
2441                 return snd_pcm_oss_sync(pcm_oss_file);
2442         case SNDCTL_DSP_SPEED:
2443                 if (get_user(res, p))
2444                         return -EFAULT;
2445                 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2446                         return res;
2447                 return put_user(res, p);
2448         case SOUND_PCM_READ_RATE:
2449                 res = snd_pcm_oss_get_rate(pcm_oss_file);
2450                 if (res < 0)
2451                         return res;
2452                 return put_user(res, p);
2453         case SNDCTL_DSP_STEREO:
2454                 if (get_user(res, p))
2455                         return -EFAULT;
2456                 res = res > 0 ? 2 : 1;
2457                 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2458                         return res;
2459                 return put_user(--res, p);
2460         case SNDCTL_DSP_GETBLKSIZE:
2461                 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2462                 if (res < 0)
2463                         return res;
2464                 return put_user(res, p);
2465         case SNDCTL_DSP_SETFMT:
2466                 if (get_user(res, p))
2467                         return -EFAULT;
2468                 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2469                 if (res < 0)
2470                         return res;
2471                 return put_user(res, p);
2472         case SOUND_PCM_READ_BITS:
2473                 res = snd_pcm_oss_get_format(pcm_oss_file);
2474                 if (res < 0)
2475                         return res;
2476                 return put_user(res, p);
2477         case SNDCTL_DSP_CHANNELS:
2478                 if (get_user(res, p))
2479                         return -EFAULT;
2480                 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2481                 if (res < 0)
2482                         return res;
2483                 return put_user(res, p);
2484         case SOUND_PCM_READ_CHANNELS:
2485                 res = snd_pcm_oss_get_channels(pcm_oss_file);
2486                 if (res < 0)
2487                         return res;
2488                 return put_user(res, p);
2489         case SOUND_PCM_WRITE_FILTER:
2490         case SOUND_PCM_READ_FILTER:
2491                 return -EIO;
2492         case SNDCTL_DSP_POST:
2493                 return snd_pcm_oss_post(pcm_oss_file);
2494         case SNDCTL_DSP_SUBDIVIDE:
2495                 if (get_user(res, p))
2496                         return -EFAULT;
2497                 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2498                 if (res < 0)
2499                         return res;
2500                 return put_user(res, p);
2501         case SNDCTL_DSP_SETFRAGMENT:
2502                 if (get_user(res, p))
2503                         return -EFAULT;
2504                 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2505         case SNDCTL_DSP_GETFMTS:
2506                 res = snd_pcm_oss_get_formats(pcm_oss_file);
2507                 if (res < 0)
2508                         return res;
2509                 return put_user(res, p);
2510         case SNDCTL_DSP_GETOSPACE:
2511         case SNDCTL_DSP_GETISPACE:
2512                 return snd_pcm_oss_get_space(pcm_oss_file,
2513                         cmd == SNDCTL_DSP_GETISPACE ?
2514                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2515                         (struct audio_buf_info __user *) arg);
2516         case SNDCTL_DSP_NONBLOCK:
2517                 return snd_pcm_oss_nonblock(file);
2518         case SNDCTL_DSP_GETCAPS:
2519                 res = snd_pcm_oss_get_caps(pcm_oss_file);
2520                 if (res < 0)
2521                         return res;
2522                 return put_user(res, p);
2523         case SNDCTL_DSP_GETTRIGGER:
2524                 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2525                 if (res < 0)
2526                         return res;
2527                 return put_user(res, p);
2528         case SNDCTL_DSP_SETTRIGGER:
2529                 if (get_user(res, p))
2530                         return -EFAULT;
2531                 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2532         case SNDCTL_DSP_GETIPTR:
2533         case SNDCTL_DSP_GETOPTR:
2534                 return snd_pcm_oss_get_ptr(pcm_oss_file,
2535                         cmd == SNDCTL_DSP_GETIPTR ?
2536                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2537                         (struct count_info __user *) arg);
2538         case SNDCTL_DSP_MAPINBUF:
2539         case SNDCTL_DSP_MAPOUTBUF:
2540                 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2541                         cmd == SNDCTL_DSP_MAPINBUF ?
2542                                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2543                         (struct buffmem_desc __user *) arg);
2544         case SNDCTL_DSP_SETSYNCRO:
2545                 /* stop DMA now.. */
2546                 return 0;
2547         case SNDCTL_DSP_SETDUPLEX:
2548                 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2549                         return 0;
2550                 return -EIO;
2551         case SNDCTL_DSP_GETODELAY:
2552                 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2553                 if (res < 0) {
2554                         /* it's for sure, some broken apps don't check for error codes */
2555                         put_user(0, p);
2556                         return res;
2557                 }
2558                 return put_user(res, p);
2559         case SNDCTL_DSP_PROFILE:
2560                 return 0;       /* silently ignore */
2561         default:
2562                 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2563         }
2564         return -EINVAL;
2565 }
2566
2567 #ifdef CONFIG_COMPAT
2568 /* all compatible */
2569 #define snd_pcm_oss_ioctl_compat        snd_pcm_oss_ioctl
2570 #else
2571 #define snd_pcm_oss_ioctl_compat        NULL
2572 #endif
2573
2574 static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2575 {
2576         struct snd_pcm_oss_file *pcm_oss_file;
2577         struct snd_pcm_substream *substream;
2578
2579         pcm_oss_file = file->private_data;
2580         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2581         if (substream == NULL)
2582                 return -ENXIO;
2583         substream->f_flags = file->f_flags & O_NONBLOCK;
2584 #ifndef OSS_DEBUG
2585         return snd_pcm_oss_read1(substream, buf, count);
2586 #else
2587         {
2588                 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2589                 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2590                 return res;
2591         }
2592 #endif
2593 }
2594
2595 static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2596 {
2597         struct snd_pcm_oss_file *pcm_oss_file;
2598         struct snd_pcm_substream *substream;
2599         long result;
2600
2601         pcm_oss_file = file->private_data;
2602         substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2603         if (substream == NULL)
2604                 return -ENXIO;
2605         substream->f_flags = file->f_flags & O_NONBLOCK;
2606         result = snd_pcm_oss_write1(substream, buf, count);
2607 #ifdef OSS_DEBUG
2608         printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2609 #endif
2610         return result;
2611 }
2612
2613 static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2614 {
2615         struct snd_pcm_runtime *runtime = substream->runtime;
2616         if (atomic_read(&substream->mmap_count))
2617                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2618         else
2619                 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2620 }
2621
2622 static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2623 {
2624         struct snd_pcm_runtime *runtime = substream->runtime;
2625         if (atomic_read(&substream->mmap_count))
2626                 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2627         else
2628                 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2629 }
2630
2631 static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2632 {
2633         struct snd_pcm_oss_file *pcm_oss_file;
2634         unsigned int mask;
2635         struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2636         
2637         pcm_oss_file = file->private_data;
2638
2639         psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2640         csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2641
2642         mask = 0;
2643         if (psubstream != NULL) {
2644                 struct snd_pcm_runtime *runtime = psubstream->runtime;
2645                 poll_wait(file, &runtime->sleep, wait);
2646                 snd_pcm_stream_lock_irq(psubstream);
2647                 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2648                     (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2649                      snd_pcm_oss_playback_ready(psubstream)))
2650                         mask |= POLLOUT | POLLWRNORM;
2651                 snd_pcm_stream_unlock_irq(psubstream);
2652         }
2653         if (csubstream != NULL) {
2654                 struct snd_pcm_runtime *runtime = csubstream->runtime;
2655                 snd_pcm_state_t ostate;
2656                 poll_wait(file, &runtime->sleep, wait);
2657                 snd_pcm_stream_lock_irq(csubstream);
2658                 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2659                     snd_pcm_oss_capture_ready(csubstream))
2660                         mask |= POLLIN | POLLRDNORM;
2661                 snd_pcm_stream_unlock_irq(csubstream);
2662                 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2663                         struct snd_pcm_oss_file ofile;
2664                         memset(&ofile, 0, sizeof(ofile));
2665                         ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2666                         runtime->oss.trigger = 0;
2667                         snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2668                 }
2669         }
2670
2671         return mask;
2672 }
2673
2674 static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2675 {
2676         struct snd_pcm_oss_file *pcm_oss_file;
2677         struct snd_pcm_substream *substream = NULL;
2678         struct snd_pcm_runtime *runtime;
2679         int err;
2680
2681 #ifdef OSS_DEBUG
2682         printk("pcm_oss: mmap begin\n");
2683 #endif
2684         pcm_oss_file = file->private_data;
2685         switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2686         case VM_READ | VM_WRITE:
2687                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2688                 if (substream)
2689                         break;
2690                 /* Fall through */
2691         case VM_READ:
2692                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2693                 break;
2694         case VM_WRITE:
2695                 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2696                 break;
2697         default:
2698                 return -EINVAL;
2699         }
2700         /* set VM_READ access as well to fix memset() routines that do
2701            reads before writes (to improve performance) */
2702         area->vm_flags |= VM_READ;
2703         if (substream == NULL)
2704                 return -ENXIO;
2705         runtime = substream->runtime;
2706         if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2707                 return -EIO;
2708         if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2709                 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2710         else
2711                 return -EIO;
2712         
2713         if (runtime->oss.params) {
2714                 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2715                         return err;
2716         }
2717 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
2718         if (runtime->oss.plugin_first != NULL)
2719                 return -EIO;
2720 #endif
2721
2722         if (area->vm_pgoff != 0)
2723                 return -EINVAL;
2724
2725         err = snd_pcm_mmap_data(substream, file, area);
2726         if (err < 0)
2727                 return err;
2728         runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2729         runtime->silence_threshold = 0;
2730         runtime->silence_size = 0;
2731 #ifdef OSS_DEBUG
2732         printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2733 #endif
2734         /* In mmap mode we never stop */
2735         runtime->stop_threshold = runtime->boundary;
2736
2737         return 0;
2738 }
2739
2740 #ifdef CONFIG_SND_VERBOSE_PROCFS
2741 /*
2742  *  /proc interface
2743  */
2744
2745 static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2746                                   struct snd_info_buffer *buffer)
2747 {
2748         struct snd_pcm_str *pstr = entry->private_data;
2749         struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2750         mutex_lock(&pstr->oss.setup_mutex);
2751         while (setup) {
2752                 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2753                             setup->task_name,
2754                             setup->periods,
2755                             setup->period_size,
2756                             setup->disable ? " disable" : "",
2757                             setup->direct ? " direct" : "",
2758                             setup->block ? " block" : "",
2759                             setup->nonblock ? " non-block" : "",
2760                             setup->partialfrag ? " partial-frag" : "",
2761                             setup->nosilence ? " no-silence" : "");
2762                 setup = setup->next;
2763         }
2764         mutex_unlock(&pstr->oss.setup_mutex);
2765 }
2766
2767 static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2768 {
2769         struct snd_pcm_oss_setup *setup, *setupn;
2770
2771         for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2772              setup; setup = setupn) {
2773                 setupn = setup->next;
2774                 kfree(setup->task_name);
2775                 kfree(setup);
2776         }
2777         pstr->oss.setup_list = NULL;
2778 }
2779
2780 static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2781                                    struct snd_info_buffer *buffer)
2782 {
2783         struct snd_pcm_str *pstr = entry->private_data;
2784         char line[128], str[32], task_name[32], *ptr;
2785         int idx1;
2786         struct snd_pcm_oss_setup *setup, *setup1, template;
2787
2788         while (!snd_info_get_line(buffer, line, sizeof(line))) {
2789                 mutex_lock(&pstr->oss.setup_mutex);
2790                 memset(&template, 0, sizeof(template));
2791                 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2792                 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2793                         snd_pcm_oss_proc_free_setup_list(pstr);
2794                         mutex_unlock(&pstr->oss.setup_mutex);
2795                         continue;
2796                 }
2797                 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2798                         if (!strcmp(setup->task_name, task_name)) {
2799                                 template = *setup;
2800                                 break;
2801                         }
2802                 }
2803                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2804                 template.periods = simple_strtoul(str, NULL, 10);
2805                 ptr = snd_info_get_str(str, ptr, sizeof(str));
2806                 template.period_size = simple_strtoul(str, NULL, 10);
2807                 for (idx1 = 31; idx1 >= 0; idx1--)
2808                         if (template.period_size & (1 << idx1))
2809                                 break;
2810                 for (idx1--; idx1 >= 0; idx1--)
2811                         template.period_size &= ~(1 << idx1);
2812                 do {
2813                         ptr = snd_info_get_str(str, ptr, sizeof(str));
2814                         if (!strcmp(str, "disable")) {
2815                                 template.disable = 1;
2816                         } else if (!strcmp(str, "direct")) {
2817                                 template.direct = 1;
2818                         } else if (!strcmp(str, "block")) {
2819                                 template.block = 1;
2820                         } else if (!strcmp(str, "non-block")) {
2821                                 template.nonblock = 1;
2822                         } else if (!strcmp(str, "partial-frag")) {
2823                                 template.partialfrag = 1;
2824                         } else if (!strcmp(str, "no-silence")) {
2825                                 template.nosilence = 1;
2826                         } else if (!strcmp(str, "buggy-ptr")) {
2827                                 template.buggyptr = 1;
2828                         }
2829                 } while (*str);
2830                 if (setup == NULL) {
2831                         setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2832                         if (! setup) {
2833                                 buffer->error = -ENOMEM;
2834                                 mutex_lock(&pstr->oss.setup_mutex);
2835                                 return;
2836                         }
2837                         if (pstr->oss.setup_list == NULL)
2838                                 pstr->oss.setup_list = setup;
2839                         else {
2840                                 for (setup1 = pstr->oss.setup_list;
2841                                      setup1->next; setup1 = setup1->next);
2842                                 setup1->next = setup;
2843                         }
2844                         template.task_name = kstrdup(task_name, GFP_KERNEL);
2845                         if (! template.task_name) {
2846                                 kfree(setup);
2847                                 buffer->error = -ENOMEM;
2848                                 mutex_lock(&pstr->oss.setup_mutex);
2849                                 return;
2850                         }
2851                 }
2852                 *setup = template;
2853                 mutex_unlock(&pstr->oss.setup_mutex);
2854         }
2855 }
2856
2857 static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
2858 {
2859         int stream;
2860         for (stream = 0; stream < 2; ++stream) {
2861                 struct snd_info_entry *entry;
2862                 struct snd_pcm_str *pstr = &pcm->streams[stream];
2863                 if (pstr->substream_count == 0)
2864                         continue;
2865                 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2866                         entry->content = SNDRV_INFO_CONTENT_TEXT;
2867                         entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
2868                         entry->c.text.read = snd_pcm_oss_proc_read;
2869                         entry->c.text.write = snd_pcm_oss_proc_write;
2870                         entry->private_data = pstr;
2871                         if (snd_info_register(entry) < 0) {
2872                                 snd_info_free_entry(entry);
2873                                 entry = NULL;
2874                         }
2875                 }
2876                 pstr->oss.proc_entry = entry;
2877         }
2878 }
2879
2880 static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
2881 {
2882         int stream;
2883         for (stream = 0; stream < 2; ++stream) {
2884                 struct snd_pcm_str *pstr = &pcm->streams[stream];
2885                 snd_info_free_entry(pstr->oss.proc_entry);
2886                 pstr->oss.proc_entry = NULL;
2887                 snd_pcm_oss_proc_free_setup_list(pstr);
2888         }
2889 }
2890 #else /* !CONFIG_SND_VERBOSE_PROCFS */
2891 #define snd_pcm_oss_proc_init(pcm)
2892 #define snd_pcm_oss_proc_done(pcm)
2893 #endif /* CONFIG_SND_VERBOSE_PROCFS */
2894
2895 /*
2896  *  ENTRY functions
2897  */
2898
2899 static struct file_operations snd_pcm_oss_f_reg =
2900 {
2901         .owner =        THIS_MODULE,
2902         .read =         snd_pcm_oss_read,
2903         .write =        snd_pcm_oss_write,
2904         .open =         snd_pcm_oss_open,
2905         .release =      snd_pcm_oss_release,
2906         .poll =         snd_pcm_oss_poll,
2907         .unlocked_ioctl =       snd_pcm_oss_ioctl,
2908         .compat_ioctl = snd_pcm_oss_ioctl_compat,
2909         .mmap =         snd_pcm_oss_mmap,
2910 };
2911
2912 static void register_oss_dsp(struct snd_pcm *pcm, int index)
2913 {
2914         char name[128];
2915         sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2916         if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2917                                     pcm->card, index, &snd_pcm_oss_f_reg,
2918                                     pcm, name) < 0) {
2919                 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2920                            pcm->card->number, pcm->device);
2921         }
2922 }
2923
2924 static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
2925 {
2926         pcm->oss.reg = 0;
2927         if (dsp_map[pcm->card->number] == (int)pcm->device) {
2928                 char name[128];
2929                 int duplex;
2930                 register_oss_dsp(pcm, 0);
2931                 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
2932                               pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
2933                               !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2934                 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2935 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2936                 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2937                                       pcm->card->number,
2938                                       name);
2939 #endif
2940                 pcm->oss.reg++;
2941                 pcm->oss.reg_mask |= 1;
2942         }
2943         if (adsp_map[pcm->card->number] == (int)pcm->device) {
2944                 register_oss_dsp(pcm, 1);
2945                 pcm->oss.reg++;
2946                 pcm->oss.reg_mask |= 2;
2947         }
2948
2949         if (pcm->oss.reg)
2950                 snd_pcm_oss_proc_init(pcm);
2951
2952         return 0;
2953 }
2954
2955 static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
2956 {
2957         if (pcm->oss.reg) {
2958                 if (pcm->oss.reg_mask & 1) {
2959                         pcm->oss.reg_mask &= ~1;
2960                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2961                                                   pcm->card, 0);
2962                 }
2963                 if (pcm->oss.reg_mask & 2) {
2964                         pcm->oss.reg_mask &= ~2;
2965                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2966                                                   pcm->card, 1);
2967                 }
2968                 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2969 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
2970                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2971 #endif
2972                 }
2973                 pcm->oss.reg = 0;
2974         }
2975         return 0;
2976 }
2977
2978 static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
2979 {
2980         snd_pcm_oss_disconnect_minor(pcm);
2981         snd_pcm_oss_proc_done(pcm);
2982         return 0;
2983 }
2984
2985 static struct snd_pcm_notify snd_pcm_oss_notify =
2986 {
2987         .n_register =   snd_pcm_oss_register_minor,
2988         .n_disconnect = snd_pcm_oss_disconnect_minor,
2989         .n_unregister = snd_pcm_oss_unregister_minor,
2990 };
2991
2992 static int __init alsa_pcm_oss_init(void)
2993 {
2994         int i;
2995         int err;
2996
2997         /* check device map table */
2998         for (i = 0; i < SNDRV_CARDS; i++) {
2999                 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3000                         snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
3001                                    i, dsp_map[i]);
3002                         dsp_map[i] = 0;
3003                 }
3004                 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3005                         snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3006                                    i, adsp_map[i]);
3007                         adsp_map[i] = 1;
3008                 }
3009         }
3010         if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3011                 return err;
3012         return 0;
3013 }
3014
3015 static void __exit alsa_pcm_oss_exit(void)
3016 {
3017         snd_pcm_notify(&snd_pcm_oss_notify, 1);
3018 }
3019
3020 module_init(alsa_pcm_oss_init)
3021 module_exit(alsa_pcm_oss_exit)