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