blob: d4460f18e76cc609af690a2d2427bbfeae3dc2d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer / OSS compatible
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/time.h>
32#include <linux/vmalloc.h>
33#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070034#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/core.h>
36#include <sound/minors.h>
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include "pcm_plugin.h"
40#include <sound/info.h>
41#include <linux/soundcard.h>
42#include <sound/initval.h>
43
44#define OSS_ALSAEMULVER _SIOR ('M', 249, int)
45
Takashi Iwai6581f4e2006-05-17 17:14:51 +020046static int dsp_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
48static int nonblock_open = 1;
49
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020050MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
52MODULE_LICENSE("GPL");
53module_param_array(dsp_map, int, NULL, 0444);
54MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
55module_param_array(adsp_map, int, NULL, 0444);
56MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
57module_param(nonblock_open, bool, 0644);
58MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
59MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
60MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
61
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010062extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
63static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
64static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
65static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static inline mm_segment_t snd_enter_user(void)
68{
69 mm_segment_t fs = get_fs();
70 set_fs(get_ds());
71 return fs;
72}
73
74static inline void snd_leave_user(mm_segment_t fs)
75{
76 set_fs(fs);
77}
78
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020079/*
80 * helper functions to process hw_params
81 */
82static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
83{
84 int changed = 0;
85 if (i->min < min) {
86 i->min = min;
87 i->openmin = openmin;
88 changed = 1;
89 } else if (i->min == min && !i->openmin && openmin) {
90 i->openmin = 1;
91 changed = 1;
92 }
93 if (i->integer) {
94 if (i->openmin) {
95 i->min++;
96 i->openmin = 0;
97 }
98 }
99 if (snd_interval_checkempty(i)) {
100 snd_interval_none(i);
101 return -EINVAL;
102 }
103 return changed;
104}
105
106static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
107{
108 int changed = 0;
109 if (i->max > max) {
110 i->max = max;
111 i->openmax = openmax;
112 changed = 1;
113 } else if (i->max == max && !i->openmax && openmax) {
114 i->openmax = 1;
115 changed = 1;
116 }
117 if (i->integer) {
118 if (i->openmax) {
119 i->max--;
120 i->openmax = 0;
121 }
122 }
123 if (snd_interval_checkempty(i)) {
124 snd_interval_none(i);
125 return -EINVAL;
126 }
127 return changed;
128}
129
130static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
131{
132 struct snd_interval t;
133 t.empty = 0;
134 t.min = t.max = val;
135 t.openmin = t.openmax = 0;
136 t.integer = 1;
137 return snd_interval_refine(i, &t);
138}
139
140/**
141 * snd_pcm_hw_param_value_min
142 * @params: the hw_params instance
143 * @var: parameter to retrieve
144 * @dir: pointer to the direction (-1,0,1) or NULL
145 *
146 * Return the minimum value for field PAR.
147 */
148static unsigned int
149snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
150 snd_pcm_hw_param_t var, int *dir)
151{
152 if (hw_is_mask(var)) {
153 if (dir)
154 *dir = 0;
155 return snd_mask_min(hw_param_mask_c(params, var));
156 }
157 if (hw_is_interval(var)) {
158 const struct snd_interval *i = hw_param_interval_c(params, var);
159 if (dir)
160 *dir = i->openmin;
161 return snd_interval_min(i);
162 }
163 return -EINVAL;
164}
165
166/**
167 * snd_pcm_hw_param_value_max
168 * @params: the hw_params instance
169 * @var: parameter to retrieve
170 * @dir: pointer to the direction (-1,0,1) or NULL
171 *
172 * Return the maximum value for field PAR.
173 */
174static unsigned int
175snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
176 snd_pcm_hw_param_t var, int *dir)
177{
178 if (hw_is_mask(var)) {
179 if (dir)
180 *dir = 0;
181 return snd_mask_max(hw_param_mask_c(params, var));
182 }
183 if (hw_is_interval(var)) {
184 const struct snd_interval *i = hw_param_interval_c(params, var);
185 if (dir)
186 *dir = - (int) i->openmax;
187 return snd_interval_max(i);
188 }
189 return -EINVAL;
190}
191
192static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
193 snd_pcm_hw_param_t var,
194 const struct snd_mask *val)
195{
196 int changed;
197 changed = snd_mask_refine(hw_param_mask(params, var), val);
198 if (changed) {
199 params->cmask |= 1 << var;
200 params->rmask |= 1 << var;
201 }
202 return changed;
203}
204
205static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
206 struct snd_pcm_hw_params *params,
207 snd_pcm_hw_param_t var,
208 const struct snd_mask *val)
209{
210 int changed = _snd_pcm_hw_param_mask(params, var, val);
211 if (changed < 0)
212 return changed;
213 if (params->rmask) {
214 int err = snd_pcm_hw_refine(pcm, params);
215 if (err < 0)
216 return err;
217 }
218 return 0;
219}
220
221static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
222 snd_pcm_hw_param_t var, unsigned int val,
223 int dir)
224{
225 int changed;
226 int open = 0;
227 if (dir) {
228 if (dir > 0) {
229 open = 1;
230 } else if (dir < 0) {
231 if (val > 0) {
232 open = 1;
233 val--;
234 }
235 }
236 }
237 if (hw_is_mask(var))
238 changed = snd_mask_refine_min(hw_param_mask(params, var),
239 val + !!open);
240 else if (hw_is_interval(var))
241 changed = snd_interval_refine_min(hw_param_interval(params, var),
242 val, open);
243 else
244 return -EINVAL;
245 if (changed) {
246 params->cmask |= 1 << var;
247 params->rmask |= 1 << var;
248 }
249 return changed;
250}
251
252/**
253 * snd_pcm_hw_param_min
254 * @pcm: PCM instance
255 * @params: the hw_params instance
256 * @var: parameter to retrieve
257 * @val: minimal value
258 * @dir: pointer to the direction (-1,0,1) or NULL
259 *
260 * Inside configuration space defined by PARAMS remove from PAR all
261 * values < VAL. Reduce configuration space accordingly.
262 * Return new minimum or -EINVAL if the configuration space is empty
263 */
264static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
265 struct snd_pcm_hw_params *params,
266 snd_pcm_hw_param_t var, unsigned int val,
267 int *dir)
268{
269 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
270 if (changed < 0)
271 return changed;
272 if (params->rmask) {
273 int err = snd_pcm_hw_refine(pcm, params);
274 if (err < 0)
275 return err;
276 }
277 return snd_pcm_hw_param_value_min(params, var, dir);
278}
279
280static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
281 snd_pcm_hw_param_t var, unsigned int val,
282 int dir)
283{
284 int changed;
285 int open = 0;
286 if (dir) {
287 if (dir < 0) {
288 open = 1;
289 } else if (dir > 0) {
290 open = 1;
291 val++;
292 }
293 }
294 if (hw_is_mask(var)) {
295 if (val == 0 && open) {
296 snd_mask_none(hw_param_mask(params, var));
297 changed = -EINVAL;
298 } else
299 changed = snd_mask_refine_max(hw_param_mask(params, var),
300 val - !!open);
301 } else if (hw_is_interval(var))
302 changed = snd_interval_refine_max(hw_param_interval(params, var),
303 val, open);
304 else
305 return -EINVAL;
306 if (changed) {
307 params->cmask |= 1 << var;
308 params->rmask |= 1 << var;
309 }
310 return changed;
311}
312
313/**
314 * snd_pcm_hw_param_max
315 * @pcm: PCM instance
316 * @params: the hw_params instance
317 * @var: parameter to retrieve
318 * @val: maximal value
319 * @dir: pointer to the direction (-1,0,1) or NULL
320 *
321 * Inside configuration space defined by PARAMS remove from PAR all
322 * values >= VAL + 1. Reduce configuration space accordingly.
323 * Return new maximum or -EINVAL if the configuration space is empty
324 */
325static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
326 struct snd_pcm_hw_params *params,
327 snd_pcm_hw_param_t var, unsigned int val,
328 int *dir)
329{
330 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
331 if (changed < 0)
332 return changed;
333 if (params->rmask) {
334 int err = snd_pcm_hw_refine(pcm, params);
335 if (err < 0)
336 return err;
337 }
338 return snd_pcm_hw_param_value_max(params, var, dir);
339}
340
341static int boundary_sub(int a, int adir,
342 int b, int bdir,
343 int *c, int *cdir)
344{
345 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
346 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
347 *c = a - b;
348 *cdir = adir - bdir;
349 if (*cdir == -2) {
350 (*c)--;
351 } else if (*cdir == 2) {
352 (*c)++;
353 }
354 return 0;
355}
356
357static int boundary_lt(unsigned int a, int adir,
358 unsigned int b, int bdir)
359{
360 if (adir < 0) {
361 a--;
362 adir = 1;
363 } else if (adir > 0)
364 adir = 1;
365 if (bdir < 0) {
366 b--;
367 bdir = 1;
368 } else if (bdir > 0)
369 bdir = 1;
370 return a < b || (a == b && adir < bdir);
371}
372
373/* Return 1 if min is nearer to best than max */
374static int boundary_nearer(int min, int mindir,
375 int best, int bestdir,
376 int max, int maxdir)
377{
378 int dmin, dmindir;
379 int dmax, dmaxdir;
380 boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
381 boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
382 return boundary_lt(dmin, dmindir, dmax, dmaxdir);
383}
384
385/**
386 * snd_pcm_hw_param_near
387 * @pcm: PCM instance
388 * @params: the hw_params instance
389 * @var: parameter to retrieve
390 * @best: value to set
391 * @dir: pointer to the direction (-1,0,1) or NULL
392 *
393 * Inside configuration space defined by PARAMS set PAR to the available value
394 * nearest to VAL. Reduce configuration space accordingly.
395 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
396 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
397 * Return the value found.
398 */
399static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
400 struct snd_pcm_hw_params *params,
401 snd_pcm_hw_param_t var, unsigned int best,
402 int *dir)
403{
404 struct snd_pcm_hw_params *save = NULL;
405 int v;
406 unsigned int saved_min;
407 int last = 0;
408 int min, max;
409 int mindir, maxdir;
410 int valdir = dir ? *dir : 0;
411 /* FIXME */
412 if (best > INT_MAX)
413 best = INT_MAX;
414 min = max = best;
415 mindir = maxdir = valdir;
416 if (maxdir > 0)
417 maxdir = 0;
418 else if (maxdir == 0)
419 maxdir = -1;
420 else {
421 maxdir = 1;
422 max--;
423 }
424 save = kmalloc(sizeof(*save), GFP_KERNEL);
425 if (save == NULL)
426 return -ENOMEM;
427 *save = *params;
428 saved_min = min;
429 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
430 if (min >= 0) {
431 struct snd_pcm_hw_params *params1;
432 if (max < 0)
433 goto _end;
434 if ((unsigned int)min == saved_min && mindir == valdir)
435 goto _end;
436 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
437 if (params1 == NULL) {
438 kfree(save);
439 return -ENOMEM;
440 }
441 *params1 = *save;
442 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
443 if (max < 0) {
444 kfree(params1);
445 goto _end;
446 }
447 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
448 *params = *params1;
449 last = 1;
450 }
451 kfree(params1);
452 } else {
453 *params = *save;
454 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200455 if (max < 0)
456 return max;
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200457 last = 1;
458 }
459 _end:
460 kfree(save);
461 if (last)
462 v = snd_pcm_hw_param_last(pcm, params, var, dir);
463 else
464 v = snd_pcm_hw_param_first(pcm, params, var, dir);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200465 snd_BUG_ON(v < 0);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200466 return v;
467}
468
469static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
470 snd_pcm_hw_param_t var, unsigned int val,
471 int dir)
472{
473 int changed;
474 if (hw_is_mask(var)) {
475 struct snd_mask *m = hw_param_mask(params, var);
476 if (val == 0 && dir < 0) {
477 changed = -EINVAL;
478 snd_mask_none(m);
479 } else {
480 if (dir > 0)
481 val++;
482 else if (dir < 0)
483 val--;
484 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
485 }
486 } else if (hw_is_interval(var)) {
487 struct snd_interval *i = hw_param_interval(params, var);
488 if (val == 0 && dir < 0) {
489 changed = -EINVAL;
490 snd_interval_none(i);
491 } else if (dir == 0)
492 changed = snd_interval_refine_set(i, val);
493 else {
494 struct snd_interval t;
495 t.openmin = 1;
496 t.openmax = 1;
497 t.empty = 0;
498 t.integer = 0;
499 if (dir < 0) {
500 t.min = val - 1;
501 t.max = val;
502 } else {
503 t.min = val;
504 t.max = val+1;
505 }
506 changed = snd_interval_refine(i, &t);
507 }
508 } else
509 return -EINVAL;
510 if (changed) {
511 params->cmask |= 1 << var;
512 params->rmask |= 1 << var;
513 }
514 return changed;
515}
516
517/**
518 * snd_pcm_hw_param_set
519 * @pcm: PCM instance
520 * @params: the hw_params instance
521 * @var: parameter to retrieve
522 * @val: value to set
523 * @dir: pointer to the direction (-1,0,1) or NULL
524 *
525 * Inside configuration space defined by PARAMS remove from PAR all
526 * values != VAL. Reduce configuration space accordingly.
527 * Return VAL or -EINVAL if the configuration space is empty
528 */
529static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
530 struct snd_pcm_hw_params *params,
531 snd_pcm_hw_param_t var, unsigned int val,
532 int dir)
533{
534 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
535 if (changed < 0)
536 return changed;
537 if (params->rmask) {
538 int err = snd_pcm_hw_refine(pcm, params);
539 if (err < 0)
540 return err;
541 }
542 return snd_pcm_hw_param_value(params, var, NULL);
543}
544
545static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
546 snd_pcm_hw_param_t var)
547{
548 int changed;
549 changed = snd_interval_setinteger(hw_param_interval(params, var));
550 if (changed) {
551 params->cmask |= 1 << var;
552 params->rmask |= 1 << var;
553 }
554 return changed;
555}
556
557/*
558 * plugin
559 */
560
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100561#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100562static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100564 struct snd_pcm_runtime *runtime = substream->runtime;
565 struct snd_pcm_plugin *plugin, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 plugin = runtime->oss.plugin_first;
568 while (plugin) {
569 next = plugin->next;
570 snd_pcm_plugin_free(plugin);
571 plugin = next;
572 }
573 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
574 return 0;
575}
576
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100577static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100579 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 plugin->next = runtime->oss.plugin_first;
581 plugin->prev = NULL;
582 if (runtime->oss.plugin_first) {
583 runtime->oss.plugin_first->prev = plugin;
584 runtime->oss.plugin_first = plugin;
585 } else {
586 runtime->oss.plugin_last =
587 runtime->oss.plugin_first = plugin;
588 }
589 return 0;
590}
591
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100592int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100594 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 plugin->next = NULL;
596 plugin->prev = runtime->oss.plugin_last;
597 if (runtime->oss.plugin_last) {
598 runtime->oss.plugin_last->next = plugin;
599 runtime->oss.plugin_last = plugin;
600 } else {
601 runtime->oss.plugin_last =
602 runtime->oss.plugin_first = plugin;
603 }
604 return 0;
605}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100606#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100608static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100610 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200611 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200612 long bytes = frames_to_bytes(runtime, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (buffer_size == runtime->oss.buffer_bytes)
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200614 return bytes;
Takashi Iwaicdc5c532005-05-27 12:40:52 +0200615#if BITS_PER_LONG >= 64
616 return runtime->oss.buffer_bytes * bytes / buffer_size;
617#else
618 {
619 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
620 u32 rem;
621 div64_32(&bsize, buffer_size, &rem);
622 return (long)bsize;
623 }
624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100627static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100629 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200630 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 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
Takashi Iwai24038a22007-08-08 17:00:32 +0200636/* define extended formats in the recent OSS versions (if any) */
637/* linear formats */
638#define AFMT_S32_LE 0x00001000
639#define AFMT_S32_BE 0x00002000
640#define AFMT_S24_LE 0x00008000
641#define AFMT_S24_BE 0x00010000
642#define AFMT_S24_PACKED 0x00040000
643
644/* other supported formats */
645#define AFMT_FLOAT 0x00004000
646#define AFMT_SPDIF_RAW 0x00020000
647
648/* unsupported formats */
649#define AFMT_AC3 0x00000400
650#define AFMT_VORBIS 0x00000800
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static int snd_pcm_oss_format_from(int format)
653{
654 switch (format) {
655 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
656 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
657 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
658 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
659 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
660 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
661 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
662 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
663 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
664 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
Takashi Iwai24038a22007-08-08 17:00:32 +0200665 case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
666 case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
667 case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
668 case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
669 case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
670 case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
671 case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 default: return SNDRV_PCM_FORMAT_U8;
673 }
674}
675
676static int snd_pcm_oss_format_to(int format)
677{
678 switch (format) {
679 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
680 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
681 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
682 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
683 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
684 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
685 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
686 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
687 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
688 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
Takashi Iwai24038a22007-08-08 17:00:32 +0200689 case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
690 case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
691 case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
692 case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
693 case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
694 case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
695 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 default: return -EINVAL;
697 }
698}
699
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100700static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
701 struct snd_pcm_hw_params *oss_params,
702 struct snd_pcm_hw_params *slave_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 size_t s;
705 size_t oss_buffer_size, oss_period_size, oss_periods;
706 size_t min_period_size, max_period_size;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100707 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 size_t oss_frame_size;
709
710 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
711 params_channels(oss_params) / 8;
712
713 oss_buffer_size = snd_pcm_plug_client_size(substream,
714 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
715 oss_buffer_size = 1 << ld2(oss_buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200716 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (oss_buffer_size > runtime->oss.mmap_bytes)
718 oss_buffer_size = runtime->oss.mmap_bytes;
719 }
720
Takashi Iwai060d77b2006-03-27 16:44:52 +0200721 if (substream->oss.setup.period_size > 16)
722 oss_period_size = substream->oss.setup.period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 else if (runtime->oss.fragshift) {
724 oss_period_size = 1 << runtime->oss.fragshift;
725 if (oss_period_size > oss_buffer_size / 2)
726 oss_period_size = oss_buffer_size / 2;
727 } else {
728 int sd;
729 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
730
731 oss_period_size = oss_buffer_size;
732 do {
733 oss_period_size /= 2;
734 } while (oss_period_size > bytes_per_sec);
735 if (runtime->oss.subdivision == 0) {
736 sd = 4;
737 if (oss_period_size / sd > 4096)
738 sd *= 2;
739 if (oss_period_size / sd < 4096)
740 sd = 1;
741 } else
742 sd = runtime->oss.subdivision;
743 oss_period_size /= sd;
744 if (oss_period_size < 16)
745 oss_period_size = 16;
746 }
747
748 min_period_size = snd_pcm_plug_client_size(substream,
749 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
750 min_period_size *= oss_frame_size;
751 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
752 if (oss_period_size < min_period_size)
753 oss_period_size = min_period_size;
754
755 max_period_size = snd_pcm_plug_client_size(substream,
756 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
757 max_period_size *= oss_frame_size;
758 max_period_size = 1 << ld2(max_period_size);
759 if (oss_period_size > max_period_size)
760 oss_period_size = max_period_size;
761
762 oss_periods = oss_buffer_size / oss_period_size;
763
Takashi Iwai060d77b2006-03-27 16:44:52 +0200764 if (substream->oss.setup.periods > 1)
765 oss_periods = substream->oss.setup.periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
768 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
769 s = runtime->oss.maxfrags;
770 if (oss_periods > s)
771 oss_periods = s;
772
773 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
774 if (s < 2)
775 s = 2;
776 if (oss_periods < s)
777 oss_periods = s;
778
779 while (oss_period_size * oss_periods > oss_buffer_size)
780 oss_period_size /= 2;
781
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200782 if (oss_period_size < 16)
783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 runtime->oss.period_bytes = oss_period_size;
785 runtime->oss.period_frames = 1;
786 runtime->oss.periods = oss_periods;
787 return 0;
788}
789
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100790static int choose_rate(struct snd_pcm_substream *substream,
791 struct snd_pcm_hw_params *params, unsigned int best_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100793 struct snd_interval *it;
794 struct snd_pcm_hw_params *save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unsigned int rate, prev;
796
797 save = kmalloc(sizeof(*save), GFP_KERNEL);
798 if (save == NULL)
799 return -ENOMEM;
800 *save = *params;
801 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
802
803 /* try multiples of the best rate */
804 rate = best_rate;
805 for (;;) {
806 if (it->max < rate || (it->max == rate && it->openmax))
807 break;
808 if (it->min < rate || (it->min == rate && !it->openmin)) {
809 int ret;
810 ret = snd_pcm_hw_param_set(substream, params,
811 SNDRV_PCM_HW_PARAM_RATE,
812 rate, 0);
813 if (ret == (int)rate) {
814 kfree(save);
815 return rate;
816 }
817 *params = *save;
818 }
819 prev = rate;
820 rate += best_rate;
821 if (rate <= prev)
822 break;
823 }
824
825 /* not found, use the nearest rate */
826 kfree(save);
827 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
828}
829
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100830static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100832 struct snd_pcm_runtime *runtime = substream->runtime;
833 struct snd_pcm_hw_params *params, *sparams;
834 struct snd_pcm_sw_params *sw_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ssize_t oss_buffer_size, oss_period_size;
836 size_t oss_frame_size;
837 int err;
838 int direct;
839 int format, sformat, n;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100840 struct snd_mask sformat_mask;
841 struct snd_mask mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Takashi Iwaie3a5d592006-11-14 13:03:19 +0100843 if (mutex_lock_interruptible(&runtime->oss.params_lock))
844 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
846 params = kmalloc(sizeof(*params), GFP_KERNEL);
847 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
848 if (!sw_params || !params || !sparams) {
849 snd_printd("No memory\n");
850 err = -ENOMEM;
851 goto failure;
852 }
853
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200854 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +0200856 else
857 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 _snd_pcm_hw_params_any(sparams);
860 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
861 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
862 snd_mask_none(&mask);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200863 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
865 else {
866 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
867 if (!direct)
868 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
869 }
870 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
871 if (err < 0) {
872 snd_printd("No usable accesses\n");
873 err = -EINVAL;
874 goto failure;
875 }
876 choose_rate(substream, sparams, runtime->oss.rate);
877 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
878
879 format = snd_pcm_oss_format_from(runtime->oss.format);
880
881 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
882 if (direct)
883 sformat = format;
884 else
885 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
886
887 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
888 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
889 if (snd_mask_test(&sformat_mask, sformat) &&
890 snd_pcm_oss_format_to(sformat) >= 0)
891 break;
892 }
893 if (sformat > SNDRV_PCM_FORMAT_LAST) {
894 snd_printd("Cannot find a format!!!\n");
895 err = -EINVAL;
896 goto failure;
897 }
898 }
899 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200900 if (err < 0)
901 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (direct) {
904 memcpy(params, sparams, sizeof(*params));
905 } else {
906 _snd_pcm_hw_params_any(params);
907 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
908 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
909 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
910 snd_pcm_oss_format_from(runtime->oss.format), 0);
911 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
912 runtime->oss.channels, 0);
913 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
914 runtime->oss.rate, 0);
915 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
916 params_access(params), params_format(params),
917 params_channels(params), params_rate(params));
918 }
919 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
920 params_access(sparams), params_format(sparams),
921 params_channels(sparams), params_rate(sparams));
922
923 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
924 params_channels(params) / 8;
925
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100926#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 snd_pcm_oss_plugin_clear(substream);
928 if (!direct) {
929 /* add necessary plugins */
930 snd_pcm_oss_plugin_clear(substream);
931 if ((err = snd_pcm_plug_format_plugins(substream,
932 params,
933 sparams)) < 0) {
934 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
935 snd_pcm_oss_plugin_clear(substream);
936 goto failure;
937 }
938 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100939 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
941 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
942 snd_pcm_oss_plugin_clear(substream);
943 goto failure;
944 }
945 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
946 err = snd_pcm_plugin_append(plugin);
947 } else {
948 err = snd_pcm_plugin_insert(plugin);
949 }
950 if (err < 0) {
951 snd_pcm_oss_plugin_clear(substream);
952 goto failure;
953 }
954 }
955 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 err = snd_pcm_oss_period_size(substream, params, sparams);
959 if (err < 0)
960 goto failure;
961
962 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
963 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200964 if (err < 0)
965 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
968 runtime->oss.periods, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200969 if (err < 0)
970 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
973
974 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
975 snd_printd("HW_PARAMS failed: %i\n", err);
976 goto failure;
977 }
978
979 memset(sw_params, 0, sizeof(*sw_params));
980 if (runtime->oss.trigger) {
981 sw_params->start_threshold = 1;
982 } else {
983 sw_params->start_threshold = runtime->boundary;
984 }
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200985 if (atomic_read(&substream->mmap_count) ||
986 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 sw_params->stop_threshold = runtime->boundary;
988 else
989 sw_params->stop_threshold = runtime->buffer_size;
990 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
991 sw_params->period_step = 1;
Takashi Iwai004e6532005-04-12 17:33:59 +0200992 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
993 1 : runtime->period_size;
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200994 if (atomic_read(&substream->mmap_count) ||
Takashi Iwai060d77b2006-03-27 16:44:52 +0200995 substream->oss.setup.nosilence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sw_params->silence_threshold = 0;
997 sw_params->silence_size = 0;
998 } else {
999 snd_pcm_uframes_t frames;
1000 frames = runtime->period_size + 16;
1001 if (frames > runtime->buffer_size)
1002 frames = runtime->buffer_size;
1003 sw_params->silence_threshold = frames;
1004 sw_params->silence_size = frames;
1005 }
1006
1007 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1008 snd_printd("SW_PARAMS failed: %i\n", err);
1009 goto failure;
1010 }
1011
1012 runtime->oss.periods = params_periods(sparams);
1013 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001014 if (oss_period_size < 0) {
1015 err = -EINVAL;
1016 goto failure;
1017 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001018#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (runtime->oss.plugin_first) {
1020 err = snd_pcm_plug_alloc(substream, oss_period_size);
1021 if (err < 0)
1022 goto failure;
1023 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 oss_period_size *= oss_frame_size;
1026
1027 oss_buffer_size = oss_period_size * runtime->oss.periods;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001028 if (oss_buffer_size < 0) {
1029 err = -EINVAL;
1030 goto failure;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 runtime->oss.period_bytes = oss_period_size;
1034 runtime->oss.buffer_bytes = oss_buffer_size;
1035
1036 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1037 runtime->oss.period_bytes,
1038 runtime->oss.buffer_bytes);
1039 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1040 params_period_size(sparams),
1041 params_buffer_size(sparams));
1042
1043 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1044 runtime->oss.channels = params_channels(params);
1045 runtime->oss.rate = params_rate(params);
1046
1047 runtime->oss.params = 0;
1048 runtime->oss.prepare = 1;
1049 vfree(runtime->oss.buffer);
1050 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1051 runtime->oss.buffer_used = 0;
1052 if (runtime->dma_area)
1053 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1054
1055 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1056
1057 err = 0;
1058failure:
1059 kfree(sw_params);
1060 kfree(params);
1061 kfree(sparams);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001062 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return err;
1064}
1065
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001066static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001069 struct snd_pcm_substream *asubstream = NULL, *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 for (idx = 0; idx < 2; idx++) {
1072 substream = pcm_oss_file->streams[idx];
1073 if (substream == NULL)
1074 continue;
1075 if (asubstream == NULL)
1076 asubstream = substream;
1077 if (substream->runtime->oss.params) {
1078 err = snd_pcm_oss_change_params(substream);
1079 if (err < 0)
1080 return err;
1081 }
1082 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001083 if (!asubstream)
1084 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (r_substream)
1086 *r_substream = asubstream;
1087 return 0;
1088}
1089
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001090static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 int err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001093 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1096 if (err < 0) {
1097 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1098 return err;
1099 }
1100 runtime->oss.prepare = 0;
1101 runtime->oss.prev_hw_ptr_interrupt = 0;
1102 runtime->oss.period_ptr = 0;
1103 runtime->oss.buffer_used = 0;
1104
1105 return 0;
1106}
1107
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001108static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001110 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int err;
1112
1113 if (substream == NULL)
1114 return 0;
1115 runtime = substream->runtime;
1116 if (runtime->oss.params) {
1117 err = snd_pcm_oss_change_params(substream);
1118 if (err < 0)
1119 return err;
1120 }
1121 if (runtime->oss.prepare) {
1122 err = snd_pcm_oss_prepare(substream);
1123 if (err < 0)
1124 return err;
1125 }
1126 return 0;
1127}
1128
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001129static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001131 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 snd_pcm_uframes_t frames;
1133 int err = 0;
1134
1135 while (1) {
1136 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1137 if (err < 0)
1138 break;
1139 runtime = substream->runtime;
1140 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1141 break;
1142 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1143 /* until avail(delay) <= buffer_size */
1144 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1145 frames /= runtime->period_size;
1146 frames *= runtime->period_size;
1147 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1148 if (err < 0)
1149 break;
1150 }
1151 return err;
1152}
1153
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001154snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001156 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int ret;
1158 while (1) {
1159 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1160 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1161#ifdef OSS_DEBUG
1162 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1163 printk("pcm_oss: write: recovering from XRUN\n");
1164 else
1165 printk("pcm_oss: write: recovering from SUSPEND\n");
1166#endif
1167 ret = snd_pcm_oss_prepare(substream);
1168 if (ret < 0)
1169 break;
1170 }
1171 if (in_kernel) {
1172 mm_segment_t fs;
1173 fs = snd_enter_user();
1174 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1175 snd_leave_user(fs);
1176 } else {
1177 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1178 }
1179 if (ret != -EPIPE && ret != -ESTRPIPE)
1180 break;
1181 /* test, if we can't store new data, because the stream */
1182 /* has not been started */
1183 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1184 return -EAGAIN;
1185 }
1186 return ret;
1187}
1188
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001189snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001191 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 snd_pcm_sframes_t delay;
1193 int ret;
1194 while (1) {
1195 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1196 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1197#ifdef OSS_DEBUG
1198 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1199 printk("pcm_oss: read: recovering from XRUN\n");
1200 else
1201 printk("pcm_oss: read: recovering from SUSPEND\n");
1202#endif
1203 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1204 if (ret < 0)
1205 break;
1206 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1207 ret = snd_pcm_oss_prepare(substream);
1208 if (ret < 0)
1209 break;
1210 }
1211 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1212 if (ret < 0)
1213 break;
1214 if (in_kernel) {
1215 mm_segment_t fs;
1216 fs = snd_enter_user();
1217 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1218 snd_leave_user(fs);
1219 } else {
1220 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1221 }
1222 if (ret == -EPIPE) {
1223 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1224 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1225 if (ret < 0)
1226 break;
1227 }
1228 continue;
1229 }
1230 if (ret != -ESTRPIPE)
1231 break;
1232 }
1233 return ret;
1234}
1235
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001236snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001238 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int ret;
1240 while (1) {
1241 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1242 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1243#ifdef OSS_DEBUG
1244 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1245 printk("pcm_oss: writev: recovering from XRUN\n");
1246 else
1247 printk("pcm_oss: writev: recovering from SUSPEND\n");
1248#endif
1249 ret = snd_pcm_oss_prepare(substream);
1250 if (ret < 0)
1251 break;
1252 }
1253 if (in_kernel) {
1254 mm_segment_t fs;
1255 fs = snd_enter_user();
1256 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1257 snd_leave_user(fs);
1258 } else {
1259 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1260 }
1261 if (ret != -EPIPE && ret != -ESTRPIPE)
1262 break;
1263
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
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001272snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001274 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int ret;
1276 while (1) {
1277 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1278 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1279#ifdef OSS_DEBUG
1280 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1281 printk("pcm_oss: readv: recovering from XRUN\n");
1282 else
1283 printk("pcm_oss: readv: recovering from SUSPEND\n");
1284#endif
1285 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1286 if (ret < 0)
1287 break;
1288 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1289 ret = snd_pcm_oss_prepare(substream);
1290 if (ret < 0)
1291 break;
1292 }
1293 if (in_kernel) {
1294 mm_segment_t fs;
1295 fs = snd_enter_user();
1296 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1297 snd_leave_user(fs);
1298 } else {
1299 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1300 }
1301 if (ret != -EPIPE && ret != -ESTRPIPE)
1302 break;
1303 }
1304 return ret;
1305}
1306
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001307static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001309 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001311#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001313 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1315 if (!in_kernel) {
1316 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1317 return -EFAULT;
1318 buf = runtime->oss.buffer;
1319 }
1320 frames = bytes / oss_frame_bytes;
1321 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1322 if (frames1 < 0)
1323 return frames1;
1324 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1325 if (frames1 <= 0)
1326 return frames1;
1327 bytes = frames1 * oss_frame_bytes;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001328 } else
1329#endif
1330 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 frames = bytes_to_frames(runtime, bytes);
1332 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1333 if (frames1 <= 0)
1334 return frames1;
1335 bytes = frames_to_bytes(runtime, frames1);
1336 }
1337 return bytes;
1338}
1339
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001340static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 size_t xfer = 0;
1343 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001344 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001346 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -ENXIO;
1348
1349 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1350 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001351 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 while (bytes > 0) {
1353 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1354 tmp = bytes;
1355 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1356 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1357 if (tmp > 0) {
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001358 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1359 tmp = -EFAULT;
1360 goto err;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 runtime->oss.buffer_used += tmp;
1364 buf += tmp;
1365 bytes -= tmp;
1366 xfer += tmp;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001367 if (substream->oss.setup.partialfrag ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1369 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1370 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1371 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001372 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 runtime->oss.bytes += tmp;
1374 runtime->oss.period_ptr += tmp;
1375 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1376 if (runtime->oss.period_ptr == 0 ||
1377 runtime->oss.period_ptr == runtime->oss.buffer_used)
1378 runtime->oss.buffer_used = 0;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001379 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1380 tmp = -EAGAIN;
1381 goto err;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001385 tmp = snd_pcm_oss_write2(substream,
1386 (const char __force *)buf,
1387 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001389 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 runtime->oss.bytes += tmp;
1391 buf += tmp;
1392 bytes -= tmp;
1393 xfer += tmp;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001394 if ((substream->f_flags & O_NONBLOCK) != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 tmp != runtime->oss.period_bytes)
1396 break;
1397 }
1398 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001399 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001401
1402 err:
1403 mutex_unlock(&runtime->oss.params_lock);
1404 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001407static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001409 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001411#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 char __user *final_dst = (char __user *)buf;
1413 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001414 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1416 if (!in_kernel)
1417 buf = runtime->oss.buffer;
1418 frames = bytes / oss_frame_bytes;
1419 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1420 if (frames1 < 0)
1421 return frames1;
1422 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1423 if (frames1 <= 0)
1424 return frames1;
1425 bytes = frames1 * oss_frame_bytes;
1426 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1427 return -EFAULT;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001428 } else
1429#endif
1430 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 frames = bytes_to_frames(runtime, bytes);
1432 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1433 if (frames1 <= 0)
1434 return frames1;
1435 bytes = frames_to_bytes(runtime, frames1);
1436 }
1437 return bytes;
1438}
1439
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001440static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 size_t xfer = 0;
1443 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001444 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001446 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -ENXIO;
1448
1449 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1450 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001451 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 while (bytes > 0) {
1453 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1454 if (runtime->oss.buffer_used == 0) {
1455 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1456 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001457 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 runtime->oss.bytes += tmp;
1459 runtime->oss.period_ptr = tmp;
1460 runtime->oss.buffer_used = tmp;
1461 }
1462 tmp = bytes;
1463 if ((size_t) tmp > runtime->oss.buffer_used)
1464 tmp = runtime->oss.buffer_used;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001465 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1466 tmp = -EFAULT;
1467 goto err;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 buf += tmp;
1470 bytes -= tmp;
1471 xfer += tmp;
1472 runtime->oss.buffer_used -= tmp;
1473 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001474 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1475 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001477 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 runtime->oss.bytes += tmp;
1479 buf += tmp;
1480 bytes -= tmp;
1481 xfer += tmp;
1482 }
1483 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001484 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001486
1487 err:
1488 mutex_unlock(&runtime->oss.params_lock);
1489 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001492static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001494 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1497 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001498 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 substream->runtime->oss.prepare = 1;
1500 }
1501 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1502 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001503 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 substream->runtime->oss.prepare = 1;
1505 }
1506 return 0;
1507}
1508
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001509static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001511 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int err;
1513
1514 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1515 if (substream != NULL) {
1516 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1517 return err;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001518 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 /* note: all errors from the start action are ignored */
1521 /* OSS apps do not know, how to handle them */
1522 return 0;
1523}
1524
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001525static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001527 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ssize_t result = 0;
1529 long res;
1530 wait_queue_t wait;
1531
1532 runtime = substream->runtime;
1533 init_waitqueue_entry(&wait, current);
1534 add_wait_queue(&runtime->sleep, &wait);
1535#ifdef OSS_DEBUG
1536 printk("sync1: size = %li\n", size);
1537#endif
1538 while (1) {
1539 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1540 if (result > 0) {
1541 runtime->oss.buffer_used = 0;
1542 result = 0;
1543 break;
1544 }
1545 if (result != 0 && result != -EAGAIN)
1546 break;
1547 result = 0;
1548 set_current_state(TASK_INTERRUPTIBLE);
1549 snd_pcm_stream_lock_irq(substream);
1550 res = runtime->status->state;
1551 snd_pcm_stream_unlock_irq(substream);
1552 if (res != SNDRV_PCM_STATE_RUNNING) {
1553 set_current_state(TASK_RUNNING);
1554 break;
1555 }
1556 res = schedule_timeout(10 * HZ);
1557 if (signal_pending(current)) {
1558 result = -ERESTARTSYS;
1559 break;
1560 }
1561 if (res == 0) {
1562 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1563 result = -EIO;
1564 break;
1565 }
1566 }
1567 remove_wait_queue(&runtime->sleep, &wait);
1568 return result;
1569}
1570
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001571static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 int err = 0;
1574 unsigned int saved_f_flags;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001575 struct snd_pcm_substream *substream;
1576 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 snd_pcm_format_t format;
1578 unsigned long width;
1579 size_t size;
1580
1581 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1582 if (substream != NULL) {
1583 runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001584 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto __direct;
1586 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1587 return err;
1588 format = snd_pcm_oss_format_from(runtime->oss.format);
1589 width = snd_pcm_format_physical_width(format);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001590 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (runtime->oss.buffer_used > 0) {
1592#ifdef OSS_DEBUG
1593 printk("sync: buffer_used\n");
1594#endif
1595 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1596 snd_pcm_format_set_silence(format,
1597 runtime->oss.buffer + runtime->oss.buffer_used,
1598 size);
1599 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001600 if (err < 0) {
1601 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 } else if (runtime->oss.period_ptr > 0) {
1605#ifdef OSS_DEBUG
1606 printk("sync: period_ptr\n");
1607#endif
1608 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1609 snd_pcm_format_set_silence(format,
1610 runtime->oss.buffer,
1611 size * 8 / width);
1612 err = snd_pcm_oss_sync1(substream, size);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001613 if (err < 0) {
1614 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 /*
1619 * The ALSA's period might be a bit large than OSS one.
1620 * Fill the remain portion of ALSA period with zeros.
1621 */
1622 size = runtime->control->appl_ptr % runtime->period_size;
1623 if (size > 0) {
1624 size = runtime->period_size - size;
1625 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1626 size = (runtime->frame_bits * size) / 8;
1627 while (size > 0) {
1628 mm_segment_t fs;
1629 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1630 size -= size1;
1631 size1 *= 8;
1632 size1 /= runtime->sample_bits;
1633 snd_pcm_format_set_silence(runtime->format,
1634 runtime->oss.buffer,
1635 size1);
Takashi Iwai4939c662008-01-28 23:53:41 +01001636 size1 /= runtime->channels; /* frames */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 fs = snd_enter_user();
1638 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1639 snd_leave_user(fs);
1640 }
1641 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1642 void __user *buffers[runtime->channels];
1643 memset(buffers, 0, runtime->channels * sizeof(void *));
1644 snd_pcm_lib_writev(substream, buffers, size);
1645 }
1646 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001647 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * finish sync: drain the buffer
1650 */
1651 __direct:
Takashi Iwai0df63e42006-04-28 15:13:41 +02001652 saved_f_flags = substream->f_flags;
1653 substream->f_flags &= ~O_NONBLOCK;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001654 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
Takashi Iwai0df63e42006-04-28 15:13:41 +02001655 substream->f_flags = saved_f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (err < 0)
1657 return err;
1658 runtime->oss.prepare = 1;
1659 }
1660
1661 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1662 if (substream != NULL) {
1663 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1664 return err;
1665 runtime = substream->runtime;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001666 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (err < 0)
1668 return err;
1669 runtime->oss.buffer_used = 0;
1670 runtime->oss.prepare = 1;
1671 }
1672 return 0;
1673}
1674
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001675static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 int idx;
1678
1679 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001680 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1681 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (substream == NULL)
1683 continue;
1684 runtime = substream->runtime;
1685 if (rate < 1000)
1686 rate = 1000;
1687 else if (rate > 192000)
1688 rate = 192000;
1689 if (runtime->oss.rate != rate) {
1690 runtime->oss.params = 1;
1691 runtime->oss.rate = rate;
1692 }
1693 }
1694 return snd_pcm_oss_get_rate(pcm_oss_file);
1695}
1696
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001697static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001699 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int err;
1701
1702 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1703 return err;
1704 return substream->runtime->oss.rate;
1705}
1706
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001707static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 int idx;
1710 if (channels < 1)
1711 channels = 1;
1712 if (channels > 128)
1713 return -EINVAL;
1714 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001715 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1716 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (substream == NULL)
1718 continue;
1719 runtime = substream->runtime;
1720 if (runtime->oss.channels != channels) {
1721 runtime->oss.params = 1;
1722 runtime->oss.channels = channels;
1723 }
1724 }
1725 return snd_pcm_oss_get_channels(pcm_oss_file);
1726}
1727
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001728static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001730 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int err;
1732
1733 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1734 return err;
1735 return substream->runtime->oss.channels;
1736}
1737
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001738static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001740 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int err;
1742
1743 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1744 return err;
1745 return substream->runtime->oss.period_bytes;
1746}
1747
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001748static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001750 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 int err;
1752 int direct;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001753 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 unsigned int formats = 0;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001755 struct snd_mask format_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int fmt;
1757
1758 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1759 return err;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001760 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001762 else
1763 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (!direct)
1765 return AFMT_MU_LAW | AFMT_U8 |
1766 AFMT_S16_LE | AFMT_S16_BE |
1767 AFMT_S8 | AFMT_U16_LE |
Takashi Iwai24038a22007-08-08 17:00:32 +02001768 AFMT_U16_BE |
1769 AFMT_S32_LE | AFMT_S32_BE |
Roel Kluin7924f0c2009-02-04 18:14:55 +01001770 AFMT_S24_LE | AFMT_S24_BE |
Takashi Iwai24038a22007-08-08 17:00:32 +02001771 AFMT_S24_PACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 params = kmalloc(sizeof(*params), GFP_KERNEL);
1773 if (!params)
1774 return -ENOMEM;
1775 _snd_pcm_hw_params_any(params);
1776 err = snd_pcm_hw_refine(substream, params);
1777 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1778 kfree(params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001779 if (err < 0)
1780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 for (fmt = 0; fmt < 32; ++fmt) {
1782 if (snd_mask_test(&format_mask, fmt)) {
1783 int f = snd_pcm_oss_format_to(fmt);
1784 if (f >= 0)
1785 formats |= f;
1786 }
1787 }
1788 return formats;
1789}
1790
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001791static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int formats, idx;
1794
1795 if (format != AFMT_QUERY) {
1796 formats = snd_pcm_oss_get_formats(pcm_oss_file);
Steven Finney5c59e092006-04-13 12:49:31 +02001797 if (formats < 0)
1798 return formats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (!(formats & format))
1800 format = AFMT_U8;
1801 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001802 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1803 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (substream == NULL)
1805 continue;
1806 runtime = substream->runtime;
1807 if (runtime->oss.format != format) {
1808 runtime->oss.params = 1;
1809 runtime->oss.format = format;
1810 }
1811 }
1812 }
1813 return snd_pcm_oss_get_format(pcm_oss_file);
1814}
1815
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001816static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001818 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int err;
1820
1821 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1822 return err;
1823 return substream->runtime->oss.format;
1824}
1825
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001826static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001828 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (substream == NULL)
1831 return 0;
1832 runtime = substream->runtime;
1833 if (subdivide == 0) {
1834 subdivide = runtime->oss.subdivision;
1835 if (subdivide == 0)
1836 subdivide = 1;
1837 return subdivide;
1838 }
1839 if (runtime->oss.subdivision || runtime->oss.fragshift)
1840 return -EINVAL;
1841 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1842 subdivide != 8 && subdivide != 16)
1843 return -EINVAL;
1844 runtime->oss.subdivision = subdivide;
1845 runtime->oss.params = 1;
1846 return subdivide;
1847}
1848
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001849static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 int err = -EINVAL, idx;
1852
1853 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001854 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (substream == NULL)
1856 continue;
1857 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1858 return err;
1859 }
1860 return err;
1861}
1862
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001863static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001865 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 if (substream == NULL)
1868 return 0;
1869 runtime = substream->runtime;
1870 if (runtime->oss.subdivision || runtime->oss.fragshift)
1871 return -EINVAL;
1872 runtime->oss.fragshift = val & 0xffff;
1873 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1874 if (runtime->oss.fragshift < 4) /* < 16 */
1875 runtime->oss.fragshift = 4;
1876 if (runtime->oss.maxfrags < 2)
1877 runtime->oss.maxfrags = 2;
1878 runtime->oss.params = 1;
1879 return 0;
1880}
1881
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001882static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 int err = -EINVAL, idx;
1885
1886 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001887 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (substream == NULL)
1889 continue;
1890 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1891 return err;
1892 }
1893 return err;
1894}
1895
1896static int snd_pcm_oss_nonblock(struct file * file)
1897{
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001898 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 file->f_flags |= O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001900 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902}
1903
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001904static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906
1907 if (substream == NULL) {
1908 res &= ~DSP_CAP_DUPLEX;
1909 return res;
1910 }
1911#ifdef DSP_CAP_MULTI
1912 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1913 if (substream->pstr->substream_count > 1)
1914 res |= DSP_CAP_MULTI;
1915#endif
1916 /* DSP_CAP_REALTIME is set all times: */
1917 /* all ALSA drivers can return actual pointer in ring buffer */
1918#if defined(DSP_CAP_REALTIME) && 0
1919 {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001920 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1922 res &= ~DSP_CAP_REALTIME;
1923 }
1924#endif
1925 return res;
1926}
1927
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001928static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int result, idx;
1931
1932 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1933 for (idx = 0; idx < 2; idx++) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001934 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 result = snd_pcm_oss_get_caps1(substream, result);
1936 }
1937 result |= 0x0001; /* revision - same as SB AWE 64 */
1938 return result;
1939}
1940
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001941static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001943 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 snd_pcm_uframes_t appl_ptr;
1945 appl_ptr = hw_ptr + runtime->buffer_size;
1946 appl_ptr %= runtime->boundary;
1947 runtime->control->appl_ptr = appl_ptr;
1948}
1949
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001950static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001952 struct snd_pcm_runtime *runtime;
1953 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int err, cmd;
1955
1956#ifdef OSS_DEBUG
1957 printk("pcm_oss: trigger = 0x%x\n", trigger);
1958#endif
1959
1960 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1961 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1962
1963 if (psubstream) {
1964 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1965 return err;
1966 }
1967 if (csubstream) {
1968 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1969 return err;
1970 }
1971 if (psubstream) {
1972 runtime = psubstream->runtime;
1973 if (trigger & PCM_ENABLE_OUTPUT) {
1974 if (runtime->oss.trigger)
1975 goto _skip1;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001976 if (atomic_read(&psubstream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1978 runtime->oss.trigger = 1;
1979 runtime->start_threshold = 1;
1980 cmd = SNDRV_PCM_IOCTL_START;
1981 } else {
1982 if (!runtime->oss.trigger)
1983 goto _skip1;
1984 runtime->oss.trigger = 0;
1985 runtime->start_threshold = runtime->boundary;
1986 cmd = SNDRV_PCM_IOCTL_DROP;
1987 runtime->oss.prepare = 1;
1988 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001989 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (err < 0)
1991 return err;
1992 }
1993 _skip1:
1994 if (csubstream) {
1995 runtime = csubstream->runtime;
1996 if (trigger & PCM_ENABLE_INPUT) {
1997 if (runtime->oss.trigger)
1998 goto _skip2;
1999 runtime->oss.trigger = 1;
2000 runtime->start_threshold = 1;
2001 cmd = SNDRV_PCM_IOCTL_START;
2002 } else {
2003 if (!runtime->oss.trigger)
2004 goto _skip2;
2005 runtime->oss.trigger = 0;
2006 runtime->start_threshold = runtime->boundary;
2007 cmd = SNDRV_PCM_IOCTL_DROP;
2008 runtime->oss.prepare = 1;
2009 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002010 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (err < 0)
2012 return err;
2013 }
2014 _skip2:
2015 return 0;
2016}
2017
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002018static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002020 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 int result = 0;
2022
2023 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2024 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2025 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2026 result |= PCM_ENABLE_OUTPUT;
2027 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2028 result |= PCM_ENABLE_INPUT;
2029 return result;
2030}
2031
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002032static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002034 struct snd_pcm_substream *substream;
2035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 snd_pcm_sframes_t delay;
2037 int err;
2038
2039 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2040 if (substream == NULL)
2041 return -EINVAL;
2042 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2043 return err;
2044 runtime = substream->runtime;
2045 if (runtime->oss.params || runtime->oss.prepare)
2046 return 0;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002047 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (err == -EPIPE)
2049 delay = 0; /* hack for broken OSS applications */
2050 else if (err < 0)
2051 return err;
2052 return snd_pcm_oss_bytes(substream, delay);
2053}
2054
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002055static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002057 struct snd_pcm_substream *substream;
2058 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 snd_pcm_sframes_t delay;
2060 int fixup;
2061 struct count_info info;
2062 int err;
2063
2064 if (_info == NULL)
2065 return -EFAULT;
2066 substream = pcm_oss_file->streams[stream];
2067 if (substream == NULL)
2068 return -EINVAL;
2069 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2070 return err;
2071 runtime = substream->runtime;
2072 if (runtime->oss.params || runtime->oss.prepare) {
2073 memset(&info, 0, sizeof(info));
2074 if (copy_to_user(_info, &info, sizeof(info)))
2075 return -EFAULT;
2076 return 0;
2077 }
2078 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2079 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2080 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2081 err = 0;
2082 delay = 0;
2083 fixup = 0;
2084 } else {
2085 fixup = runtime->oss.buffer_used;
2086 }
2087 } else {
2088 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2089 fixup = -runtime->oss.buffer_used;
2090 }
2091 if (err < 0)
2092 return err;
2093 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002094 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 snd_pcm_sframes_t n;
2096 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2097 if (n < 0)
2098 n += runtime->boundary;
2099 info.blocks = n / runtime->period_size;
2100 runtime->oss.prev_hw_ptr_interrupt = delay;
2101 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2102 snd_pcm_oss_simulate_fill(substream, delay);
2103 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2104 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002105 delay = snd_pcm_oss_bytes(substream, delay);
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002106 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002107 if (substream->oss.setup.buggyptr)
Takashi Iwai10f69f92005-09-08 13:48:34 +02002108 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2109 else
2110 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002112 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002113 delay += fixup;
2114 info.blocks = delay / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 if (copy_to_user(_info, &info, sizeof(info)))
2119 return -EFAULT;
2120 return 0;
2121}
2122
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002123static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002125 struct snd_pcm_substream *substream;
2126 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 snd_pcm_sframes_t avail;
2128 int fixup;
2129 struct audio_buf_info info;
2130 int err;
2131
2132 if (_info == NULL)
2133 return -EFAULT;
2134 substream = pcm_oss_file->streams[stream];
2135 if (substream == NULL)
2136 return -EINVAL;
2137 runtime = substream->runtime;
2138
2139 if (runtime->oss.params &&
2140 (err = snd_pcm_oss_change_params(substream)) < 0)
2141 return err;
2142
2143 info.fragsize = runtime->oss.period_bytes;
2144 info.fragstotal = runtime->periods;
2145 if (runtime->oss.prepare) {
2146 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2147 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2148 info.fragments = runtime->oss.periods;
2149 } else {
2150 info.bytes = 0;
2151 info.fragments = 0;
2152 }
2153 } else {
2154 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2155 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2156 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2157 avail = runtime->buffer_size;
2158 err = 0;
2159 fixup = 0;
2160 } else {
2161 avail = runtime->buffer_size - avail;
2162 fixup = -runtime->oss.buffer_used;
2163 }
2164 } else {
2165 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2166 fixup = runtime->oss.buffer_used;
2167 }
2168 if (err < 0)
2169 return err;
2170 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2171 info.fragments = info.bytes / runtime->oss.period_bytes;
2172 }
2173
2174#ifdef OSS_DEBUG
2175 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2176#endif
2177 if (copy_to_user(_info, &info, sizeof(info)))
2178 return -EFAULT;
2179 return 0;
2180}
2181
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002182static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 // it won't be probably implemented
2185 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2186 return -EINVAL;
2187}
2188
Takashi Iwai060d77b2006-03-27 16:44:52 +02002189static const char *strip_task_path(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Takashi Iwai060d77b2006-03-27 16:44:52 +02002191 const char *ptr, *ptrl = NULL;
2192 for (ptr = path; *ptr; ptr++) {
2193 if (*ptr == '/')
2194 ptrl = ptr + 1;
2195 }
2196 return ptrl;
2197}
2198
2199static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2200 const char *task_name,
2201 struct snd_pcm_oss_setup *rsetup)
2202{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002203 struct snd_pcm_oss_setup *setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002205 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002206 do {
2207 for (setup = pcm->streams[stream].oss.setup_list; setup;
2208 setup = setup->next) {
2209 if (!strcmp(setup->task_name, task_name))
2210 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002212 } while ((task_name = strip_task_path(task_name)) != NULL);
2213 out:
2214 if (setup)
2215 *rsetup = *setup;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002216 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002219static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2220{
2221 struct snd_pcm_runtime *runtime;
2222 runtime = substream->runtime;
2223 vfree(runtime->oss.buffer);
2224 runtime->oss.buffer = NULL;
2225#ifdef CONFIG_SND_PCM_OSS_PLUGINS
2226 snd_pcm_oss_plugin_clear(substream);
2227#endif
2228 substream->oss.oss = 0;
2229}
2230
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002231static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2232 struct snd_pcm_oss_setup *setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int minor)
2234{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002235 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 substream->oss.oss = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002238 substream->oss.setup = *setup;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002239 if (setup->nonblock)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002240 substream->f_flags |= O_NONBLOCK;
Takashi Iwai15762742006-04-06 19:47:42 +02002241 else if (setup->block)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002242 substream->f_flags &= ~O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 runtime = substream->runtime;
2244 runtime->oss.params = 1;
2245 runtime->oss.trigger = 1;
2246 runtime->oss.rate = 8000;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01002247 mutex_init(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2249 case SNDRV_MINOR_OSS_PCM_8:
2250 runtime->oss.format = AFMT_U8;
2251 break;
2252 case SNDRV_MINOR_OSS_PCM_16:
2253 runtime->oss.format = AFMT_S16_LE;
2254 break;
2255 default:
2256 runtime->oss.format = AFMT_MU_LAW;
2257 }
2258 runtime->oss.channels = 1;
2259 runtime->oss.fragshift = 0;
2260 runtime->oss.maxfrags = 0;
2261 runtime->oss.subdivision = 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002262 substream->pcm_release = snd_pcm_oss_release_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002265static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 int cidx;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002268 if (!pcm_oss_file)
2269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 for (cidx = 0; cidx < 2; ++cidx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002271 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002272 if (substream)
2273 snd_pcm_release_substream(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 kfree(pcm_oss_file);
2276 return 0;
2277}
2278
2279static int snd_pcm_oss_open_file(struct file *file,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002280 struct snd_pcm *pcm,
2281 struct snd_pcm_oss_file **rpcm_oss_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 int minor,
Takashi Iwai060d77b2006-03-27 16:44:52 +02002283 struct snd_pcm_oss_setup *setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002285 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002286 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002287 struct snd_pcm_substream *substream;
Al Viroaeb5d722008-09-02 15:28:45 -04002288 fmode_t f_mode = file->f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002290 if (rpcm_oss_file)
2291 *rpcm_oss_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Takashi Iwaica2c0962005-09-09 14:20:23 +02002293 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (pcm_oss_file == NULL)
2295 return -ENOMEM;
2296
2297 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2298 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2299 f_mode = FMODE_WRITE;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002300
Takashi Iwai0df63e42006-04-28 15:13:41 +02002301 file->f_flags &= ~O_APPEND;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002302 for (idx = 0; idx < 2; idx++) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002303 if (setup[idx].disable)
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002304 continue;
Takashi Iwai6cb53e72006-08-02 21:12:09 +02002305 if (! pcm->streams[idx].substream_count)
2306 continue; /* no matching substream */
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002307 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2308 if (! (f_mode & FMODE_WRITE))
2309 continue;
2310 } else {
2311 if (! (f_mode & FMODE_READ))
2312 continue;
2313 }
2314 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2315 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 snd_pcm_oss_release_file(pcm_oss_file);
2317 return err;
2318 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002319
2320 pcm_oss_file->streams[idx] = substream;
Takashi Iwai15762742006-04-06 19:47:42 +02002321 substream->file = pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002322 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
OGAWA Hirofumibbdc1b72006-04-06 19:42:40 +02002325 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 snd_pcm_oss_release_file(pcm_oss_file);
2327 return -EINVAL;
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 file->private_data = pcm_oss_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002331 if (rpcm_oss_file)
2332 *rpcm_oss_file = pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
2334}
2335
2336
Takashi Iwai93f2e372005-10-10 11:51:55 +02002337static int snd_task_name(struct task_struct *task, char *name, size_t size)
2338{
2339 unsigned int idx;
2340
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002341 if (snd_BUG_ON(!task || !name || size < 2))
2342 return -EINVAL;
Takashi Iwai93f2e372005-10-10 11:51:55 +02002343 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2344 name[idx] = task->comm[idx];
2345 name[idx] = '\0';
2346 return 0;
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int err;
2352 char task_name[32];
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002353 struct snd_pcm *pcm;
2354 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002355 struct snd_pcm_oss_setup setup[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 int nonblock;
2357 wait_queue_t wait;
2358
Clemens Ladischf87135f2005-11-20 14:06:59 +01002359 pcm = snd_lookup_oss_minor_data(iminor(inode),
2360 SNDRV_OSS_DEVICE_TYPE_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (pcm == NULL) {
2362 err = -ENODEV;
2363 goto __error1;
2364 }
2365 err = snd_card_file_add(pcm->card, file);
2366 if (err < 0)
2367 goto __error1;
2368 if (!try_module_get(pcm->card->module)) {
2369 err = -EFAULT;
2370 goto __error2;
2371 }
2372 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2373 err = -EFAULT;
2374 goto __error;
2375 }
Takashi Iwai15762742006-04-06 19:47:42 +02002376 memset(setup, 0, sizeof(setup));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (file->f_mode & FMODE_WRITE)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002378 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2379 task_name, &setup[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (file->f_mode & FMODE_READ)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002381 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2382 task_name, &setup[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 nonblock = !!(file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (!nonblock)
2386 nonblock = nonblock_open;
2387
2388 init_waitqueue_entry(&wait, current);
2389 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002390 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 while (1) {
2392 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002393 iminor(inode), setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (err >= 0)
2395 break;
2396 if (err == -EAGAIN) {
2397 if (nonblock) {
2398 err = -EBUSY;
2399 break;
2400 }
2401 } else
2402 break;
2403 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002404 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002406 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (signal_pending(current)) {
2408 err = -ERESTARTSYS;
2409 break;
2410 }
2411 }
2412 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002413 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (err < 0)
2415 goto __error;
2416 return err;
2417
2418 __error:
2419 module_put(pcm->card->module);
2420 __error2:
2421 snd_card_file_remove(pcm->card, file);
2422 __error1:
2423 return err;
2424}
2425
2426static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2427{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002428 struct snd_pcm *pcm;
2429 struct snd_pcm_substream *substream;
2430 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 pcm_oss_file = file->private_data;
2433 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2434 if (substream == NULL)
2435 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002436 if (snd_BUG_ON(!substream))
2437 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 pcm = substream->pcm;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01002439 if (!pcm->card->shutdown)
2440 snd_pcm_oss_sync(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002441 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 snd_pcm_oss_release_file(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002443 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 wake_up(&pcm->open_wait);
2445 module_put(pcm->card->module);
2446 snd_card_file_remove(pcm->card, file);
2447 return 0;
2448}
2449
2450static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2451{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002452 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int __user *p = (int __user *)arg;
2454 int res;
2455
2456 pcm_oss_file = file->private_data;
2457 if (cmd == OSS_GETVERSION)
2458 return put_user(SNDRV_OSS_VERSION, p);
2459 if (cmd == OSS_ALSAEMULVER)
2460 return put_user(1, p);
2461#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2462 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002463 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int idx;
2465 for (idx = 0; idx < 2; ++idx) {
2466 substream = pcm_oss_file->streams[idx];
2467 if (substream != NULL)
2468 break;
2469 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002470 if (snd_BUG_ON(idx >= 2))
2471 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2473 }
2474#endif
2475 if (((cmd >> 8) & 0xff) != 'P')
2476 return -EINVAL;
2477#ifdef OSS_DEBUG
2478 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2479#endif
2480 switch (cmd) {
2481 case SNDCTL_DSP_RESET:
2482 return snd_pcm_oss_reset(pcm_oss_file);
2483 case SNDCTL_DSP_SYNC:
2484 return snd_pcm_oss_sync(pcm_oss_file);
2485 case SNDCTL_DSP_SPEED:
2486 if (get_user(res, p))
2487 return -EFAULT;
2488 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2489 return res;
2490 return put_user(res, p);
2491 case SOUND_PCM_READ_RATE:
2492 res = snd_pcm_oss_get_rate(pcm_oss_file);
2493 if (res < 0)
2494 return res;
2495 return put_user(res, p);
2496 case SNDCTL_DSP_STEREO:
2497 if (get_user(res, p))
2498 return -EFAULT;
2499 res = res > 0 ? 2 : 1;
2500 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2501 return res;
2502 return put_user(--res, p);
2503 case SNDCTL_DSP_GETBLKSIZE:
2504 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2505 if (res < 0)
2506 return res;
2507 return put_user(res, p);
2508 case SNDCTL_DSP_SETFMT:
2509 if (get_user(res, p))
2510 return -EFAULT;
2511 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2512 if (res < 0)
2513 return res;
2514 return put_user(res, p);
2515 case SOUND_PCM_READ_BITS:
2516 res = snd_pcm_oss_get_format(pcm_oss_file);
2517 if (res < 0)
2518 return res;
2519 return put_user(res, p);
2520 case SNDCTL_DSP_CHANNELS:
2521 if (get_user(res, p))
2522 return -EFAULT;
2523 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2524 if (res < 0)
2525 return res;
2526 return put_user(res, p);
2527 case SOUND_PCM_READ_CHANNELS:
2528 res = snd_pcm_oss_get_channels(pcm_oss_file);
2529 if (res < 0)
2530 return res;
2531 return put_user(res, p);
2532 case SOUND_PCM_WRITE_FILTER:
2533 case SOUND_PCM_READ_FILTER:
2534 return -EIO;
2535 case SNDCTL_DSP_POST:
2536 return snd_pcm_oss_post(pcm_oss_file);
2537 case SNDCTL_DSP_SUBDIVIDE:
2538 if (get_user(res, p))
2539 return -EFAULT;
2540 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2541 if (res < 0)
2542 return res;
2543 return put_user(res, p);
2544 case SNDCTL_DSP_SETFRAGMENT:
2545 if (get_user(res, p))
2546 return -EFAULT;
2547 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2548 case SNDCTL_DSP_GETFMTS:
2549 res = snd_pcm_oss_get_formats(pcm_oss_file);
2550 if (res < 0)
2551 return res;
2552 return put_user(res, p);
2553 case SNDCTL_DSP_GETOSPACE:
2554 case SNDCTL_DSP_GETISPACE:
2555 return snd_pcm_oss_get_space(pcm_oss_file,
2556 cmd == SNDCTL_DSP_GETISPACE ?
2557 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2558 (struct audio_buf_info __user *) arg);
2559 case SNDCTL_DSP_NONBLOCK:
2560 return snd_pcm_oss_nonblock(file);
2561 case SNDCTL_DSP_GETCAPS:
2562 res = snd_pcm_oss_get_caps(pcm_oss_file);
2563 if (res < 0)
2564 return res;
2565 return put_user(res, p);
2566 case SNDCTL_DSP_GETTRIGGER:
2567 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2568 if (res < 0)
2569 return res;
2570 return put_user(res, p);
2571 case SNDCTL_DSP_SETTRIGGER:
2572 if (get_user(res, p))
2573 return -EFAULT;
2574 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2575 case SNDCTL_DSP_GETIPTR:
2576 case SNDCTL_DSP_GETOPTR:
2577 return snd_pcm_oss_get_ptr(pcm_oss_file,
2578 cmd == SNDCTL_DSP_GETIPTR ?
2579 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2580 (struct count_info __user *) arg);
2581 case SNDCTL_DSP_MAPINBUF:
2582 case SNDCTL_DSP_MAPOUTBUF:
2583 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2584 cmd == SNDCTL_DSP_MAPINBUF ?
2585 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2586 (struct buffmem_desc __user *) arg);
2587 case SNDCTL_DSP_SETSYNCRO:
2588 /* stop DMA now.. */
2589 return 0;
2590 case SNDCTL_DSP_SETDUPLEX:
2591 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2592 return 0;
2593 return -EIO;
2594 case SNDCTL_DSP_GETODELAY:
2595 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2596 if (res < 0) {
2597 /* it's for sure, some broken apps don't check for error codes */
2598 put_user(0, p);
2599 return res;
2600 }
2601 return put_user(res, p);
2602 case SNDCTL_DSP_PROFILE:
2603 return 0; /* silently ignore */
2604 default:
2605 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2606 }
2607 return -EINVAL;
2608}
2609
2610#ifdef CONFIG_COMPAT
2611/* all compatible */
2612#define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2613#else
2614#define snd_pcm_oss_ioctl_compat NULL
2615#endif
2616
2617static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2618{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002619 struct snd_pcm_oss_file *pcm_oss_file;
2620 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 pcm_oss_file = file->private_data;
2623 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2624 if (substream == NULL)
2625 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002626 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627#ifndef OSS_DEBUG
2628 return snd_pcm_oss_read1(substream, buf, count);
2629#else
2630 {
2631 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2632 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2633 return res;
2634 }
2635#endif
2636}
2637
2638static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2639{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002640 struct snd_pcm_oss_file *pcm_oss_file;
2641 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 long result;
2643
2644 pcm_oss_file = file->private_data;
2645 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2646 if (substream == NULL)
2647 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002648 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 result = snd_pcm_oss_write1(substream, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650#ifdef OSS_DEBUG
2651 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2652#endif
2653 return result;
2654}
2655
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002656static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002658 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002659 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2661 else
2662 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2663}
2664
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002665static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002667 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002668 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2670 else
2671 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2672}
2673
2674static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2675{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002676 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 unsigned int mask;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002678 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 pcm_oss_file = file->private_data;
2681
2682 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2683 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2684
2685 mask = 0;
2686 if (psubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002687 struct snd_pcm_runtime *runtime = psubstream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 poll_wait(file, &runtime->sleep, wait);
2689 snd_pcm_stream_lock_irq(psubstream);
2690 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2691 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2692 snd_pcm_oss_playback_ready(psubstream)))
2693 mask |= POLLOUT | POLLWRNORM;
2694 snd_pcm_stream_unlock_irq(psubstream);
2695 }
2696 if (csubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002697 struct snd_pcm_runtime *runtime = csubstream->runtime;
2698 snd_pcm_state_t ostate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 poll_wait(file, &runtime->sleep, wait);
2700 snd_pcm_stream_lock_irq(csubstream);
2701 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2702 snd_pcm_oss_capture_ready(csubstream))
2703 mask |= POLLIN | POLLRDNORM;
2704 snd_pcm_stream_unlock_irq(csubstream);
2705 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002706 struct snd_pcm_oss_file ofile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 memset(&ofile, 0, sizeof(ofile));
2708 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2709 runtime->oss.trigger = 0;
2710 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2711 }
2712 }
2713
2714 return mask;
2715}
2716
2717static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2718{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002719 struct snd_pcm_oss_file *pcm_oss_file;
2720 struct snd_pcm_substream *substream = NULL;
2721 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int err;
2723
2724#ifdef OSS_DEBUG
2725 printk("pcm_oss: mmap begin\n");
2726#endif
2727 pcm_oss_file = file->private_data;
2728 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2729 case VM_READ | VM_WRITE:
2730 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2731 if (substream)
2732 break;
2733 /* Fall through */
2734 case VM_READ:
2735 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2736 break;
2737 case VM_WRITE:
2738 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2739 break;
2740 default:
2741 return -EINVAL;
2742 }
2743 /* set VM_READ access as well to fix memset() routines that do
2744 reads before writes (to improve performance) */
2745 area->vm_flags |= VM_READ;
2746 if (substream == NULL)
2747 return -ENXIO;
2748 runtime = substream->runtime;
2749 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2750 return -EIO;
2751 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2752 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2753 else
2754 return -EIO;
2755
2756 if (runtime->oss.params) {
2757 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2758 return err;
2759 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002760#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (runtime->oss.plugin_first != NULL)
2762 return -EIO;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002763#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 if (area->vm_pgoff != 0)
2766 return -EINVAL;
2767
2768 err = snd_pcm_mmap_data(substream, file, area);
2769 if (err < 0)
2770 return err;
2771 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2772 runtime->silence_threshold = 0;
2773 runtime->silence_size = 0;
2774#ifdef OSS_DEBUG
2775 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2776#endif
2777 /* In mmap mode we never stop */
2778 runtime->stop_threshold = runtime->boundary;
2779
2780 return 0;
2781}
2782
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002783#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784/*
2785 * /proc interface
2786 */
2787
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002788static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2789 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002791 struct snd_pcm_str *pstr = entry->private_data;
2792 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002793 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 while (setup) {
2795 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2796 setup->task_name,
2797 setup->periods,
2798 setup->period_size,
2799 setup->disable ? " disable" : "",
2800 setup->direct ? " direct" : "",
2801 setup->block ? " block" : "",
2802 setup->nonblock ? " non-block" : "",
2803 setup->partialfrag ? " partial-frag" : "",
2804 setup->nosilence ? " no-silence" : "");
2805 setup = setup->next;
2806 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002807 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002810static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002812 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2815 setup; setup = setupn) {
2816 setupn = setup->next;
2817 kfree(setup->task_name);
2818 kfree(setup);
2819 }
2820 pstr->oss.setup_list = NULL;
2821}
2822
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002823static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2824 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002826 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 char line[128], str[32], task_name[32], *ptr;
2828 int idx1;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002829 struct snd_pcm_oss_setup *setup, *setup1, template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 while (!snd_info_get_line(buffer, line, sizeof(line))) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002832 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 memset(&template, 0, sizeof(template));
2834 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2835 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2836 snd_pcm_oss_proc_free_setup_list(pstr);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002837 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 continue;
2839 }
2840 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2841 if (!strcmp(setup->task_name, task_name)) {
2842 template = *setup;
2843 break;
2844 }
2845 }
2846 ptr = snd_info_get_str(str, ptr, sizeof(str));
2847 template.periods = simple_strtoul(str, NULL, 10);
2848 ptr = snd_info_get_str(str, ptr, sizeof(str));
2849 template.period_size = simple_strtoul(str, NULL, 10);
2850 for (idx1 = 31; idx1 >= 0; idx1--)
2851 if (template.period_size & (1 << idx1))
2852 break;
2853 for (idx1--; idx1 >= 0; idx1--)
2854 template.period_size &= ~(1 << idx1);
2855 do {
2856 ptr = snd_info_get_str(str, ptr, sizeof(str));
2857 if (!strcmp(str, "disable")) {
2858 template.disable = 1;
2859 } else if (!strcmp(str, "direct")) {
2860 template.direct = 1;
2861 } else if (!strcmp(str, "block")) {
2862 template.block = 1;
2863 } else if (!strcmp(str, "non-block")) {
2864 template.nonblock = 1;
2865 } else if (!strcmp(str, "partial-frag")) {
2866 template.partialfrag = 1;
2867 } else if (!strcmp(str, "no-silence")) {
2868 template.nosilence = 1;
Takashi Iwai10f69f92005-09-08 13:48:34 +02002869 } else if (!strcmp(str, "buggy-ptr")) {
2870 template.buggyptr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
2872 } while (*str);
2873 if (setup == NULL) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002874 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2875 if (! setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 buffer->error = -ENOMEM;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002877 mutex_lock(&pstr->oss.setup_mutex);
2878 return;
2879 }
2880 if (pstr->oss.setup_list == NULL)
2881 pstr->oss.setup_list = setup;
2882 else {
2883 for (setup1 = pstr->oss.setup_list;
2884 setup1->next; setup1 = setup1->next);
2885 setup1->next = setup;
2886 }
2887 template.task_name = kstrdup(task_name, GFP_KERNEL);
2888 if (! template.task_name) {
2889 kfree(setup);
2890 buffer->error = -ENOMEM;
2891 mutex_lock(&pstr->oss.setup_mutex);
2892 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002895 *setup = template;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002896 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898}
2899
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002900static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 int stream;
2903 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002904 struct snd_info_entry *entry;
2905 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (pstr->substream_count == 0)
2907 continue;
2908 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2909 entry->content = SNDRV_INFO_CONTENT_TEXT;
2910 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 entry->c.text.read = snd_pcm_oss_proc_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 entry->c.text.write = snd_pcm_oss_proc_write;
2913 entry->private_data = pstr;
2914 if (snd_info_register(entry) < 0) {
2915 snd_info_free_entry(entry);
2916 entry = NULL;
2917 }
2918 }
2919 pstr->oss.proc_entry = entry;
2920 }
2921}
2922
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002923static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 int stream;
2926 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002927 struct snd_pcm_str *pstr = &pcm->streams[stream];
Takashi Iwai746d4a02006-06-23 14:37:59 +02002928 snd_info_free_entry(pstr->oss.proc_entry);
2929 pstr->oss.proc_entry = NULL;
2930 snd_pcm_oss_proc_free_setup_list(pstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
2932}
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002933#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwai04f141a2005-12-01 10:43:51 +01002934#define snd_pcm_oss_proc_init(pcm)
2935#define snd_pcm_oss_proc_done(pcm)
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002936#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938/*
2939 * ENTRY functions
2940 */
2941
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002942static const struct file_operations snd_pcm_oss_f_reg =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 .owner = THIS_MODULE,
2945 .read = snd_pcm_oss_read,
2946 .write = snd_pcm_oss_write,
2947 .open = snd_pcm_oss_open,
2948 .release = snd_pcm_oss_release,
2949 .poll = snd_pcm_oss_poll,
2950 .unlocked_ioctl = snd_pcm_oss_ioctl,
2951 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2952 .mmap = snd_pcm_oss_mmap,
2953};
2954
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002955static void register_oss_dsp(struct snd_pcm *pcm, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 char name[128];
2958 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2959 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002960 pcm->card, index, &snd_pcm_oss_f_reg,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002961 pcm, name) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002962 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2963 pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965}
2966
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002967static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 pcm->oss.reg = 0;
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02002970 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 char name[128];
2972 int duplex;
2973 register_oss_dsp(pcm, 0);
2974 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2975 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2976 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2977 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2978#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2979 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2980 pcm->card->number,
2981 name);
2982#endif
2983 pcm->oss.reg++;
2984 pcm->oss.reg_mask |= 1;
2985 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02002986 if (adsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 register_oss_dsp(pcm, 1);
2988 pcm->oss.reg++;
2989 pcm->oss.reg_mask |= 2;
2990 }
2991
2992 if (pcm->oss.reg)
2993 snd_pcm_oss_proc_init(pcm);
2994
2995 return 0;
2996}
2997
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002998static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 if (pcm->oss.reg) {
3001 if (pcm->oss.reg_mask & 1) {
3002 pcm->oss.reg_mask &= ~1;
3003 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3004 pcm->card, 0);
3005 }
3006 if (pcm->oss.reg_mask & 2) {
3007 pcm->oss.reg_mask &= ~2;
3008 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3009 pcm->card, 1);
3010 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003011 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Takashi Iwaic4614822006-06-23 14:38:23 +02003012#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3013 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3014#endif
3015 }
3016 pcm->oss.reg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018 return 0;
3019}
3020
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003021static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 snd_pcm_oss_disconnect_minor(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +02003024 snd_pcm_oss_proc_done(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return 0;
3026}
3027
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003028static struct snd_pcm_notify snd_pcm_oss_notify =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 .n_register = snd_pcm_oss_register_minor,
3031 .n_disconnect = snd_pcm_oss_disconnect_minor,
3032 .n_unregister = snd_pcm_oss_unregister_minor,
3033};
3034
3035static int __init alsa_pcm_oss_init(void)
3036{
3037 int i;
3038 int err;
3039
3040 /* check device map table */
3041 for (i = 0; i < SNDRV_CARDS; i++) {
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003042 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003043 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
3044 i, dsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 dsp_map[i] = 0;
3046 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003047 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003048 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3049 i, adsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 adsp_map[i] = 1;
3051 }
3052 }
3053 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3054 return err;
3055 return 0;
3056}
3057
3058static void __exit alsa_pcm_oss_exit(void)
3059{
3060 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3061}
3062
3063module_init(alsa_pcm_oss_init)
3064module_exit(alsa_pcm_oss_exit)