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