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