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