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