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