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