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