blob: 4b883595a85acb7f48f0a306c3408f1d36bb0bbe [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)
Takashi Iwai006de2672009-02-05 15:51:04 +01001163 printk(KERN_DEBUG "pcm_oss: write: "
1164 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001166 printk(KERN_DEBUG "pcm_oss: write: "
1167 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168#endif
1169 ret = snd_pcm_oss_prepare(substream);
1170 if (ret < 0)
1171 break;
1172 }
1173 if (in_kernel) {
1174 mm_segment_t fs;
1175 fs = snd_enter_user();
1176 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1177 snd_leave_user(fs);
1178 } else {
1179 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1180 }
1181 if (ret != -EPIPE && ret != -ESTRPIPE)
1182 break;
1183 /* test, if we can't store new data, because the stream */
1184 /* has not been started */
1185 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1186 return -EAGAIN;
1187 }
1188 return ret;
1189}
1190
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001191snd_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 -07001192{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001193 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 snd_pcm_sframes_t delay;
1195 int ret;
1196 while (1) {
1197 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1198 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1199#ifdef OSS_DEBUG
1200 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001201 printk(KERN_DEBUG "pcm_oss: read: "
1202 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001204 printk(KERN_DEBUG "pcm_oss: read: "
1205 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206#endif
1207 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1208 if (ret < 0)
1209 break;
1210 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1211 ret = snd_pcm_oss_prepare(substream);
1212 if (ret < 0)
1213 break;
1214 }
1215 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1216 if (ret < 0)
1217 break;
1218 if (in_kernel) {
1219 mm_segment_t fs;
1220 fs = snd_enter_user();
1221 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1222 snd_leave_user(fs);
1223 } else {
1224 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1225 }
1226 if (ret == -EPIPE) {
1227 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1228 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1229 if (ret < 0)
1230 break;
1231 }
1232 continue;
1233 }
1234 if (ret != -ESTRPIPE)
1235 break;
1236 }
1237 return ret;
1238}
1239
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001240snd_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 -07001241{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001242 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int ret;
1244 while (1) {
1245 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1246 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1247#ifdef OSS_DEBUG
1248 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001249 printk(KERN_DEBUG "pcm_oss: writev: "
1250 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001252 printk(KERN_DEBUG "pcm_oss: writev: "
1253 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254#endif
1255 ret = snd_pcm_oss_prepare(substream);
1256 if (ret < 0)
1257 break;
1258 }
1259 if (in_kernel) {
1260 mm_segment_t fs;
1261 fs = snd_enter_user();
1262 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1263 snd_leave_user(fs);
1264 } else {
1265 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1266 }
1267 if (ret != -EPIPE && ret != -ESTRPIPE)
1268 break;
1269
1270 /* test, if we can't store new data, because the stream */
1271 /* has not been started */
1272 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1273 return -EAGAIN;
1274 }
1275 return ret;
1276}
1277
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001278snd_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 -07001279{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001280 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int ret;
1282 while (1) {
1283 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1284 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1285#ifdef OSS_DEBUG
1286 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001287 printk(KERN_DEBUG "pcm_oss: readv: "
1288 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001290 printk(KERN_DEBUG "pcm_oss: readv: "
1291 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#endif
1293 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1294 if (ret < 0)
1295 break;
1296 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1297 ret = snd_pcm_oss_prepare(substream);
1298 if (ret < 0)
1299 break;
1300 }
1301 if (in_kernel) {
1302 mm_segment_t fs;
1303 fs = snd_enter_user();
1304 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1305 snd_leave_user(fs);
1306 } else {
1307 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1308 }
1309 if (ret != -EPIPE && ret != -ESTRPIPE)
1310 break;
1311 }
1312 return ret;
1313}
1314
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001315static 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 -07001316{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001317 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001319#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001321 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1323 if (!in_kernel) {
1324 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1325 return -EFAULT;
1326 buf = runtime->oss.buffer;
1327 }
1328 frames = bytes / oss_frame_bytes;
1329 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1330 if (frames1 < 0)
1331 return frames1;
1332 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1333 if (frames1 <= 0)
1334 return frames1;
1335 bytes = frames1 * oss_frame_bytes;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001336 } else
1337#endif
1338 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 frames = bytes_to_frames(runtime, bytes);
1340 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1341 if (frames1 <= 0)
1342 return frames1;
1343 bytes = frames_to_bytes(runtime, frames1);
1344 }
1345 return bytes;
1346}
1347
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001348static 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 -07001349{
1350 size_t xfer = 0;
1351 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001352 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001354 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -ENXIO;
1356
1357 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1358 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001359 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 while (bytes > 0) {
1361 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1362 tmp = bytes;
1363 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1364 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1365 if (tmp > 0) {
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001366 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1367 tmp = -EFAULT;
1368 goto err;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 runtime->oss.buffer_used += tmp;
1372 buf += tmp;
1373 bytes -= tmp;
1374 xfer += tmp;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001375 if (substream->oss.setup.partialfrag ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1377 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1378 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1379 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001380 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 runtime->oss.bytes += tmp;
1382 runtime->oss.period_ptr += tmp;
1383 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1384 if (runtime->oss.period_ptr == 0 ||
1385 runtime->oss.period_ptr == runtime->oss.buffer_used)
1386 runtime->oss.buffer_used = 0;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001387 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1388 tmp = -EAGAIN;
1389 goto err;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001393 tmp = snd_pcm_oss_write2(substream,
1394 (const char __force *)buf,
1395 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001397 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 runtime->oss.bytes += tmp;
1399 buf += tmp;
1400 bytes -= tmp;
1401 xfer += tmp;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001402 if ((substream->f_flags & O_NONBLOCK) != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 tmp != runtime->oss.period_bytes)
1404 break;
1405 }
1406 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001407 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001409
1410 err:
1411 mutex_unlock(&runtime->oss.params_lock);
1412 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001415static 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 -07001416{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001417 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001419#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 char __user *final_dst = (char __user *)buf;
1421 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001422 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1424 if (!in_kernel)
1425 buf = runtime->oss.buffer;
1426 frames = bytes / oss_frame_bytes;
1427 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1428 if (frames1 < 0)
1429 return frames1;
1430 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1431 if (frames1 <= 0)
1432 return frames1;
1433 bytes = frames1 * oss_frame_bytes;
1434 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1435 return -EFAULT;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001436 } else
1437#endif
1438 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 frames = bytes_to_frames(runtime, bytes);
1440 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1441 if (frames1 <= 0)
1442 return frames1;
1443 bytes = frames_to_bytes(runtime, frames1);
1444 }
1445 return bytes;
1446}
1447
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001448static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 size_t xfer = 0;
1451 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001452 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001454 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENXIO;
1456
1457 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1458 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001459 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 while (bytes > 0) {
1461 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1462 if (runtime->oss.buffer_used == 0) {
1463 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1464 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001465 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 runtime->oss.bytes += tmp;
1467 runtime->oss.period_ptr = tmp;
1468 runtime->oss.buffer_used = tmp;
1469 }
1470 tmp = bytes;
1471 if ((size_t) tmp > runtime->oss.buffer_used)
1472 tmp = runtime->oss.buffer_used;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001473 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1474 tmp = -EFAULT;
1475 goto err;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 buf += tmp;
1478 bytes -= tmp;
1479 xfer += tmp;
1480 runtime->oss.buffer_used -= tmp;
1481 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001482 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1483 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001485 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 runtime->oss.bytes += tmp;
1487 buf += tmp;
1488 bytes -= tmp;
1489 xfer += tmp;
1490 }
1491 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001492 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001494
1495 err:
1496 mutex_unlock(&runtime->oss.params_lock);
1497 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001500static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001502 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1505 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001506 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 substream->runtime->oss.prepare = 1;
1508 }
1509 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1510 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001511 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 substream->runtime->oss.prepare = 1;
1513 }
1514 return 0;
1515}
1516
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001517static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001519 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int err;
1521
1522 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1523 if (substream != NULL) {
1524 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1525 return err;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001526 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 /* note: all errors from the start action are ignored */
1529 /* OSS apps do not know, how to handle them */
1530 return 0;
1531}
1532
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001533static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001535 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 ssize_t result = 0;
1537 long res;
1538 wait_queue_t wait;
1539
1540 runtime = substream->runtime;
1541 init_waitqueue_entry(&wait, current);
1542 add_wait_queue(&runtime->sleep, &wait);
1543#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001544 printk(KERN_DEBUG "sync1: size = %li\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#endif
1546 while (1) {
1547 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1548 if (result > 0) {
1549 runtime->oss.buffer_used = 0;
1550 result = 0;
1551 break;
1552 }
1553 if (result != 0 && result != -EAGAIN)
1554 break;
1555 result = 0;
1556 set_current_state(TASK_INTERRUPTIBLE);
1557 snd_pcm_stream_lock_irq(substream);
1558 res = runtime->status->state;
1559 snd_pcm_stream_unlock_irq(substream);
1560 if (res != SNDRV_PCM_STATE_RUNNING) {
1561 set_current_state(TASK_RUNNING);
1562 break;
1563 }
1564 res = schedule_timeout(10 * HZ);
1565 if (signal_pending(current)) {
1566 result = -ERESTARTSYS;
1567 break;
1568 }
1569 if (res == 0) {
1570 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1571 result = -EIO;
1572 break;
1573 }
1574 }
1575 remove_wait_queue(&runtime->sleep, &wait);
1576 return result;
1577}
1578
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001579static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 int err = 0;
1582 unsigned int saved_f_flags;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001583 struct snd_pcm_substream *substream;
1584 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 snd_pcm_format_t format;
1586 unsigned long width;
1587 size_t size;
1588
1589 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1590 if (substream != NULL) {
1591 runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001592 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto __direct;
1594 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1595 return err;
1596 format = snd_pcm_oss_format_from(runtime->oss.format);
1597 width = snd_pcm_format_physical_width(format);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001598 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (runtime->oss.buffer_used > 0) {
1600#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001601 printk(KERN_DEBUG "sync: buffer_used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#endif
1603 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1604 snd_pcm_format_set_silence(format,
1605 runtime->oss.buffer + runtime->oss.buffer_used,
1606 size);
1607 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001608 if (err < 0) {
1609 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 } else if (runtime->oss.period_ptr > 0) {
1613#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001614 printk(KERN_DEBUG "sync: period_ptr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#endif
1616 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1617 snd_pcm_format_set_silence(format,
1618 runtime->oss.buffer,
1619 size * 8 / width);
1620 err = snd_pcm_oss_sync1(substream, size);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001621 if (err < 0) {
1622 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 /*
1627 * The ALSA's period might be a bit large than OSS one.
1628 * Fill the remain portion of ALSA period with zeros.
1629 */
1630 size = runtime->control->appl_ptr % runtime->period_size;
1631 if (size > 0) {
1632 size = runtime->period_size - size;
1633 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1634 size = (runtime->frame_bits * size) / 8;
1635 while (size > 0) {
1636 mm_segment_t fs;
1637 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1638 size -= size1;
1639 size1 *= 8;
1640 size1 /= runtime->sample_bits;
1641 snd_pcm_format_set_silence(runtime->format,
1642 runtime->oss.buffer,
1643 size1);
Takashi Iwai4939c662008-01-28 23:53:41 +01001644 size1 /= runtime->channels; /* frames */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 fs = snd_enter_user();
1646 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1647 snd_leave_user(fs);
1648 }
1649 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1650 void __user *buffers[runtime->channels];
1651 memset(buffers, 0, runtime->channels * sizeof(void *));
1652 snd_pcm_lib_writev(substream, buffers, size);
1653 }
1654 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001655 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /*
1657 * finish sync: drain the buffer
1658 */
1659 __direct:
Takashi Iwai0df63e42006-04-28 15:13:41 +02001660 saved_f_flags = substream->f_flags;
1661 substream->f_flags &= ~O_NONBLOCK;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001662 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
Takashi Iwai0df63e42006-04-28 15:13:41 +02001663 substream->f_flags = saved_f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (err < 0)
1665 return err;
1666 runtime->oss.prepare = 1;
1667 }
1668
1669 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1670 if (substream != NULL) {
1671 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1672 return err;
1673 runtime = substream->runtime;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001674 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (err < 0)
1676 return err;
1677 runtime->oss.buffer_used = 0;
1678 runtime->oss.prepare = 1;
1679 }
1680 return 0;
1681}
1682
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001683static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 int idx;
1686
1687 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001688 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1689 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (substream == NULL)
1691 continue;
1692 runtime = substream->runtime;
1693 if (rate < 1000)
1694 rate = 1000;
1695 else if (rate > 192000)
1696 rate = 192000;
1697 if (runtime->oss.rate != rate) {
1698 runtime->oss.params = 1;
1699 runtime->oss.rate = rate;
1700 }
1701 }
1702 return snd_pcm_oss_get_rate(pcm_oss_file);
1703}
1704
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001705static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001707 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int err;
1709
1710 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1711 return err;
1712 return substream->runtime->oss.rate;
1713}
1714
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001715static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 int idx;
1718 if (channels < 1)
1719 channels = 1;
1720 if (channels > 128)
1721 return -EINVAL;
1722 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001723 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1724 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (substream == NULL)
1726 continue;
1727 runtime = substream->runtime;
1728 if (runtime->oss.channels != channels) {
1729 runtime->oss.params = 1;
1730 runtime->oss.channels = channels;
1731 }
1732 }
1733 return snd_pcm_oss_get_channels(pcm_oss_file);
1734}
1735
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001736static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001738 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 int err;
1740
1741 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1742 return err;
1743 return substream->runtime->oss.channels;
1744}
1745
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001746static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001748 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int err;
1750
1751 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1752 return err;
1753 return substream->runtime->oss.period_bytes;
1754}
1755
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001756static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001758 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int err;
1760 int direct;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001761 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned int formats = 0;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001763 struct snd_mask format_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int fmt;
1765
1766 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1767 return err;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001768 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001770 else
1771 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (!direct)
1773 return AFMT_MU_LAW | AFMT_U8 |
1774 AFMT_S16_LE | AFMT_S16_BE |
1775 AFMT_S8 | AFMT_U16_LE |
Takashi Iwai24038a22007-08-08 17:00:32 +02001776 AFMT_U16_BE |
1777 AFMT_S32_LE | AFMT_S32_BE |
1778 AFMT_S24_LE | AFMT_S24_LE |
1779 AFMT_S24_PACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 params = kmalloc(sizeof(*params), GFP_KERNEL);
1781 if (!params)
1782 return -ENOMEM;
1783 _snd_pcm_hw_params_any(params);
1784 err = snd_pcm_hw_refine(substream, params);
1785 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1786 kfree(params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001787 if (err < 0)
1788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 for (fmt = 0; fmt < 32; ++fmt) {
1790 if (snd_mask_test(&format_mask, fmt)) {
1791 int f = snd_pcm_oss_format_to(fmt);
1792 if (f >= 0)
1793 formats |= f;
1794 }
1795 }
1796 return formats;
1797}
1798
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001799static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 int formats, idx;
1802
1803 if (format != AFMT_QUERY) {
1804 formats = snd_pcm_oss_get_formats(pcm_oss_file);
Steven Finney5c59e092006-04-13 12:49:31 +02001805 if (formats < 0)
1806 return formats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!(formats & format))
1808 format = AFMT_U8;
1809 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001810 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1811 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (substream == NULL)
1813 continue;
1814 runtime = substream->runtime;
1815 if (runtime->oss.format != format) {
1816 runtime->oss.params = 1;
1817 runtime->oss.format = format;
1818 }
1819 }
1820 }
1821 return snd_pcm_oss_get_format(pcm_oss_file);
1822}
1823
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001824static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001826 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 int err;
1828
1829 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1830 return err;
1831 return substream->runtime->oss.format;
1832}
1833
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001834static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001836 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 if (substream == NULL)
1839 return 0;
1840 runtime = substream->runtime;
1841 if (subdivide == 0) {
1842 subdivide = runtime->oss.subdivision;
1843 if (subdivide == 0)
1844 subdivide = 1;
1845 return subdivide;
1846 }
1847 if (runtime->oss.subdivision || runtime->oss.fragshift)
1848 return -EINVAL;
1849 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1850 subdivide != 8 && subdivide != 16)
1851 return -EINVAL;
1852 runtime->oss.subdivision = subdivide;
1853 runtime->oss.params = 1;
1854 return subdivide;
1855}
1856
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001857static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 int err = -EINVAL, idx;
1860
1861 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001862 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (substream == NULL)
1864 continue;
1865 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1866 return err;
1867 }
1868 return err;
1869}
1870
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001871static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001873 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (substream == NULL)
1876 return 0;
1877 runtime = substream->runtime;
1878 if (runtime->oss.subdivision || runtime->oss.fragshift)
1879 return -EINVAL;
1880 runtime->oss.fragshift = val & 0xffff;
1881 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1882 if (runtime->oss.fragshift < 4) /* < 16 */
1883 runtime->oss.fragshift = 4;
1884 if (runtime->oss.maxfrags < 2)
1885 runtime->oss.maxfrags = 2;
1886 runtime->oss.params = 1;
1887 return 0;
1888}
1889
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001890static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 int err = -EINVAL, idx;
1893
1894 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001895 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (substream == NULL)
1897 continue;
1898 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1899 return err;
1900 }
1901 return err;
1902}
1903
1904static int snd_pcm_oss_nonblock(struct file * file)
1905{
1906 file->f_flags |= O_NONBLOCK;
1907 return 0;
1908}
1909
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001910static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912
1913 if (substream == NULL) {
1914 res &= ~DSP_CAP_DUPLEX;
1915 return res;
1916 }
1917#ifdef DSP_CAP_MULTI
1918 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1919 if (substream->pstr->substream_count > 1)
1920 res |= DSP_CAP_MULTI;
1921#endif
1922 /* DSP_CAP_REALTIME is set all times: */
1923 /* all ALSA drivers can return actual pointer in ring buffer */
1924#if defined(DSP_CAP_REALTIME) && 0
1925 {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001926 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1928 res &= ~DSP_CAP_REALTIME;
1929 }
1930#endif
1931 return res;
1932}
1933
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001934static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 int result, idx;
1937
1938 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1939 for (idx = 0; idx < 2; idx++) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001940 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 result = snd_pcm_oss_get_caps1(substream, result);
1942 }
1943 result |= 0x0001; /* revision - same as SB AWE 64 */
1944 return result;
1945}
1946
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001947static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001949 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 snd_pcm_uframes_t appl_ptr;
1951 appl_ptr = hw_ptr + runtime->buffer_size;
1952 appl_ptr %= runtime->boundary;
1953 runtime->control->appl_ptr = appl_ptr;
1954}
1955
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001956static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001958 struct snd_pcm_runtime *runtime;
1959 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int err, cmd;
1961
1962#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001963 printk(KERN_DEBUG "pcm_oss: trigger = 0x%x\n", trigger);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964#endif
1965
1966 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1967 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1968
1969 if (psubstream) {
1970 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1971 return err;
1972 }
1973 if (csubstream) {
1974 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1975 return err;
1976 }
1977 if (psubstream) {
1978 runtime = psubstream->runtime;
1979 if (trigger & PCM_ENABLE_OUTPUT) {
1980 if (runtime->oss.trigger)
1981 goto _skip1;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001982 if (atomic_read(&psubstream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1984 runtime->oss.trigger = 1;
1985 runtime->start_threshold = 1;
1986 cmd = SNDRV_PCM_IOCTL_START;
1987 } else {
1988 if (!runtime->oss.trigger)
1989 goto _skip1;
1990 runtime->oss.trigger = 0;
1991 runtime->start_threshold = runtime->boundary;
1992 cmd = SNDRV_PCM_IOCTL_DROP;
1993 runtime->oss.prepare = 1;
1994 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001995 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (err < 0)
1997 return err;
1998 }
1999 _skip1:
2000 if (csubstream) {
2001 runtime = csubstream->runtime;
2002 if (trigger & PCM_ENABLE_INPUT) {
2003 if (runtime->oss.trigger)
2004 goto _skip2;
2005 runtime->oss.trigger = 1;
2006 runtime->start_threshold = 1;
2007 cmd = SNDRV_PCM_IOCTL_START;
2008 } else {
2009 if (!runtime->oss.trigger)
2010 goto _skip2;
2011 runtime->oss.trigger = 0;
2012 runtime->start_threshold = runtime->boundary;
2013 cmd = SNDRV_PCM_IOCTL_DROP;
2014 runtime->oss.prepare = 1;
2015 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002016 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (err < 0)
2018 return err;
2019 }
2020 _skip2:
2021 return 0;
2022}
2023
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002024static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002026 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int result = 0;
2028
2029 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2030 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2031 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2032 result |= PCM_ENABLE_OUTPUT;
2033 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2034 result |= PCM_ENABLE_INPUT;
2035 return result;
2036}
2037
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002038static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002040 struct snd_pcm_substream *substream;
2041 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 snd_pcm_sframes_t delay;
2043 int err;
2044
2045 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2046 if (substream == NULL)
2047 return -EINVAL;
2048 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2049 return err;
2050 runtime = substream->runtime;
2051 if (runtime->oss.params || runtime->oss.prepare)
2052 return 0;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002053 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (err == -EPIPE)
2055 delay = 0; /* hack for broken OSS applications */
2056 else if (err < 0)
2057 return err;
2058 return snd_pcm_oss_bytes(substream, delay);
2059}
2060
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002061static 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 -07002062{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002063 struct snd_pcm_substream *substream;
2064 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 snd_pcm_sframes_t delay;
2066 int fixup;
2067 struct count_info info;
2068 int err;
2069
2070 if (_info == NULL)
2071 return -EFAULT;
2072 substream = pcm_oss_file->streams[stream];
2073 if (substream == NULL)
2074 return -EINVAL;
2075 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2076 return err;
2077 runtime = substream->runtime;
2078 if (runtime->oss.params || runtime->oss.prepare) {
2079 memset(&info, 0, sizeof(info));
2080 if (copy_to_user(_info, &info, sizeof(info)))
2081 return -EFAULT;
2082 return 0;
2083 }
2084 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2085 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2086 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2087 err = 0;
2088 delay = 0;
2089 fixup = 0;
2090 } else {
2091 fixup = runtime->oss.buffer_used;
2092 }
2093 } else {
2094 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2095 fixup = -runtime->oss.buffer_used;
2096 }
2097 if (err < 0)
2098 return err;
2099 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002100 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 snd_pcm_sframes_t n;
2102 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2103 if (n < 0)
2104 n += runtime->boundary;
2105 info.blocks = n / runtime->period_size;
2106 runtime->oss.prev_hw_ptr_interrupt = delay;
2107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2108 snd_pcm_oss_simulate_fill(substream, delay);
2109 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2110 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002111 delay = snd_pcm_oss_bytes(substream, delay);
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002112 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002113 if (substream->oss.setup.buggyptr)
Takashi Iwai10f69f92005-09-08 13:48:34 +02002114 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2115 else
2116 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002118 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002119 delay += fixup;
2120 info.blocks = delay / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 if (copy_to_user(_info, &info, sizeof(info)))
2125 return -EFAULT;
2126 return 0;
2127}
2128
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002129static 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 -07002130{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002131 struct snd_pcm_substream *substream;
2132 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 snd_pcm_sframes_t avail;
2134 int fixup;
2135 struct audio_buf_info info;
2136 int err;
2137
2138 if (_info == NULL)
2139 return -EFAULT;
2140 substream = pcm_oss_file->streams[stream];
2141 if (substream == NULL)
2142 return -EINVAL;
2143 runtime = substream->runtime;
2144
2145 if (runtime->oss.params &&
2146 (err = snd_pcm_oss_change_params(substream)) < 0)
2147 return err;
2148
2149 info.fragsize = runtime->oss.period_bytes;
2150 info.fragstotal = runtime->periods;
2151 if (runtime->oss.prepare) {
2152 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2153 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2154 info.fragments = runtime->oss.periods;
2155 } else {
2156 info.bytes = 0;
2157 info.fragments = 0;
2158 }
2159 } else {
2160 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2161 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2162 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2163 avail = runtime->buffer_size;
2164 err = 0;
2165 fixup = 0;
2166 } else {
2167 avail = runtime->buffer_size - avail;
2168 fixup = -runtime->oss.buffer_used;
2169 }
2170 } else {
2171 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2172 fixup = runtime->oss.buffer_used;
2173 }
2174 if (err < 0)
2175 return err;
2176 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2177 info.fragments = info.bytes / runtime->oss.period_bytes;
2178 }
2179
2180#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002181 printk(KERN_DEBUG "pcm_oss: space: bytes = %i, fragments = %i, "
2182 "fragstotal = %i, fragsize = %i\n",
2183 info.bytes, info.fragments, info.fragstotal, info.fragsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184#endif
2185 if (copy_to_user(_info, &info, sizeof(info)))
2186 return -EFAULT;
2187 return 0;
2188}
2189
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002190static 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 -07002191{
2192 // it won't be probably implemented
2193 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2194 return -EINVAL;
2195}
2196
Takashi Iwai060d77b2006-03-27 16:44:52 +02002197static const char *strip_task_path(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Takashi Iwai060d77b2006-03-27 16:44:52 +02002199 const char *ptr, *ptrl = NULL;
2200 for (ptr = path; *ptr; ptr++) {
2201 if (*ptr == '/')
2202 ptrl = ptr + 1;
2203 }
2204 return ptrl;
2205}
2206
2207static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2208 const char *task_name,
2209 struct snd_pcm_oss_setup *rsetup)
2210{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002211 struct snd_pcm_oss_setup *setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002213 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002214 do {
2215 for (setup = pcm->streams[stream].oss.setup_list; setup;
2216 setup = setup->next) {
2217 if (!strcmp(setup->task_name, task_name))
2218 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002220 } while ((task_name = strip_task_path(task_name)) != NULL);
2221 out:
2222 if (setup)
2223 *rsetup = *setup;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002224 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002227static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2228{
2229 struct snd_pcm_runtime *runtime;
2230 runtime = substream->runtime;
2231 vfree(runtime->oss.buffer);
2232 runtime->oss.buffer = NULL;
2233#ifdef CONFIG_SND_PCM_OSS_PLUGINS
2234 snd_pcm_oss_plugin_clear(substream);
2235#endif
2236 substream->oss.oss = 0;
2237}
2238
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002239static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2240 struct snd_pcm_oss_setup *setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int minor)
2242{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002243 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 substream->oss.oss = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002246 substream->oss.setup = *setup;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002247 if (setup->nonblock)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002248 substream->f_flags |= O_NONBLOCK;
Takashi Iwai15762742006-04-06 19:47:42 +02002249 else if (setup->block)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002250 substream->f_flags &= ~O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 runtime = substream->runtime;
2252 runtime->oss.params = 1;
2253 runtime->oss.trigger = 1;
2254 runtime->oss.rate = 8000;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01002255 mutex_init(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2257 case SNDRV_MINOR_OSS_PCM_8:
2258 runtime->oss.format = AFMT_U8;
2259 break;
2260 case SNDRV_MINOR_OSS_PCM_16:
2261 runtime->oss.format = AFMT_S16_LE;
2262 break;
2263 default:
2264 runtime->oss.format = AFMT_MU_LAW;
2265 }
2266 runtime->oss.channels = 1;
2267 runtime->oss.fragshift = 0;
2268 runtime->oss.maxfrags = 0;
2269 runtime->oss.subdivision = 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002270 substream->pcm_release = snd_pcm_oss_release_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002273static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 int cidx;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002276 if (!pcm_oss_file)
2277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 for (cidx = 0; cidx < 2; ++cidx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002279 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002280 if (substream)
2281 snd_pcm_release_substream(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 kfree(pcm_oss_file);
2284 return 0;
2285}
2286
2287static int snd_pcm_oss_open_file(struct file *file,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002288 struct snd_pcm *pcm,
2289 struct snd_pcm_oss_file **rpcm_oss_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int minor,
Takashi Iwai060d77b2006-03-27 16:44:52 +02002291 struct snd_pcm_oss_setup *setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002293 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002294 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002295 struct snd_pcm_substream *substream;
Al Viroaeb5d722008-09-02 15:28:45 -04002296 fmode_t f_mode = file->f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002298 if (rpcm_oss_file)
2299 *rpcm_oss_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Takashi Iwaica2c0962005-09-09 14:20:23 +02002301 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (pcm_oss_file == NULL)
2303 return -ENOMEM;
2304
2305 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2306 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2307 f_mode = FMODE_WRITE;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002308
Takashi Iwai0df63e42006-04-28 15:13:41 +02002309 file->f_flags &= ~O_APPEND;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002310 for (idx = 0; idx < 2; idx++) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002311 if (setup[idx].disable)
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002312 continue;
Takashi Iwai6cb53e72006-08-02 21:12:09 +02002313 if (! pcm->streams[idx].substream_count)
2314 continue; /* no matching substream */
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002315 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2316 if (! (f_mode & FMODE_WRITE))
2317 continue;
2318 } else {
2319 if (! (f_mode & FMODE_READ))
2320 continue;
2321 }
2322 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2323 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 snd_pcm_oss_release_file(pcm_oss_file);
2325 return err;
2326 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002327
2328 pcm_oss_file->streams[idx] = substream;
Takashi Iwai15762742006-04-06 19:47:42 +02002329 substream->file = pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002330 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332
OGAWA Hirofumibbdc1b72006-04-06 19:42:40 +02002333 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 snd_pcm_oss_release_file(pcm_oss_file);
2335 return -EINVAL;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 file->private_data = pcm_oss_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002339 if (rpcm_oss_file)
2340 *rpcm_oss_file = pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
2343
2344
Takashi Iwai93f2e372005-10-10 11:51:55 +02002345static int snd_task_name(struct task_struct *task, char *name, size_t size)
2346{
2347 unsigned int idx;
2348
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002349 if (snd_BUG_ON(!task || !name || size < 2))
2350 return -EINVAL;
Takashi Iwai93f2e372005-10-10 11:51:55 +02002351 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2352 name[idx] = task->comm[idx];
2353 name[idx] = '\0';
2354 return 0;
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int err;
2360 char task_name[32];
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002361 struct snd_pcm *pcm;
2362 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002363 struct snd_pcm_oss_setup setup[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int nonblock;
2365 wait_queue_t wait;
2366
Clemens Ladischf87135f2005-11-20 14:06:59 +01002367 pcm = snd_lookup_oss_minor_data(iminor(inode),
2368 SNDRV_OSS_DEVICE_TYPE_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (pcm == NULL) {
2370 err = -ENODEV;
2371 goto __error1;
2372 }
2373 err = snd_card_file_add(pcm->card, file);
2374 if (err < 0)
2375 goto __error1;
2376 if (!try_module_get(pcm->card->module)) {
2377 err = -EFAULT;
2378 goto __error2;
2379 }
2380 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2381 err = -EFAULT;
2382 goto __error;
2383 }
Takashi Iwai15762742006-04-06 19:47:42 +02002384 memset(setup, 0, sizeof(setup));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (file->f_mode & FMODE_WRITE)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002386 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2387 task_name, &setup[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (file->f_mode & FMODE_READ)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002389 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2390 task_name, &setup[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 nonblock = !!(file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!nonblock)
2394 nonblock = nonblock_open;
2395
2396 init_waitqueue_entry(&wait, current);
2397 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002398 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 while (1) {
2400 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002401 iminor(inode), setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (err >= 0)
2403 break;
2404 if (err == -EAGAIN) {
2405 if (nonblock) {
2406 err = -EBUSY;
2407 break;
2408 }
2409 } else
2410 break;
2411 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002412 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002414 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (signal_pending(current)) {
2416 err = -ERESTARTSYS;
2417 break;
2418 }
2419 }
2420 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002421 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (err < 0)
2423 goto __error;
2424 return err;
2425
2426 __error:
2427 module_put(pcm->card->module);
2428 __error2:
2429 snd_card_file_remove(pcm->card, file);
2430 __error1:
2431 return err;
2432}
2433
2434static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2435{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002436 struct snd_pcm *pcm;
2437 struct snd_pcm_substream *substream;
2438 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 pcm_oss_file = file->private_data;
2441 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2442 if (substream == NULL)
2443 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002444 if (snd_BUG_ON(!substream))
2445 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 pcm = substream->pcm;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01002447 if (!pcm->card->shutdown)
2448 snd_pcm_oss_sync(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002449 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 snd_pcm_oss_release_file(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002451 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 wake_up(&pcm->open_wait);
2453 module_put(pcm->card->module);
2454 snd_card_file_remove(pcm->card, file);
2455 return 0;
2456}
2457
2458static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2459{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002460 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 int __user *p = (int __user *)arg;
2462 int res;
2463
2464 pcm_oss_file = file->private_data;
2465 if (cmd == OSS_GETVERSION)
2466 return put_user(SNDRV_OSS_VERSION, p);
2467 if (cmd == OSS_ALSAEMULVER)
2468 return put_user(1, p);
2469#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2470 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002471 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 int idx;
2473 for (idx = 0; idx < 2; ++idx) {
2474 substream = pcm_oss_file->streams[idx];
2475 if (substream != NULL)
2476 break;
2477 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002478 if (snd_BUG_ON(idx >= 2))
2479 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2481 }
2482#endif
2483 if (((cmd >> 8) & 0xff) != 'P')
2484 return -EINVAL;
2485#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002486 printk(KERN_DEBUG "pcm_oss: ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#endif
2488 switch (cmd) {
2489 case SNDCTL_DSP_RESET:
2490 return snd_pcm_oss_reset(pcm_oss_file);
2491 case SNDCTL_DSP_SYNC:
2492 return snd_pcm_oss_sync(pcm_oss_file);
2493 case SNDCTL_DSP_SPEED:
2494 if (get_user(res, p))
2495 return -EFAULT;
2496 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2497 return res;
2498 return put_user(res, p);
2499 case SOUND_PCM_READ_RATE:
2500 res = snd_pcm_oss_get_rate(pcm_oss_file);
2501 if (res < 0)
2502 return res;
2503 return put_user(res, p);
2504 case SNDCTL_DSP_STEREO:
2505 if (get_user(res, p))
2506 return -EFAULT;
2507 res = res > 0 ? 2 : 1;
2508 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2509 return res;
2510 return put_user(--res, p);
2511 case SNDCTL_DSP_GETBLKSIZE:
2512 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2513 if (res < 0)
2514 return res;
2515 return put_user(res, p);
2516 case SNDCTL_DSP_SETFMT:
2517 if (get_user(res, p))
2518 return -EFAULT;
2519 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2520 if (res < 0)
2521 return res;
2522 return put_user(res, p);
2523 case SOUND_PCM_READ_BITS:
2524 res = snd_pcm_oss_get_format(pcm_oss_file);
2525 if (res < 0)
2526 return res;
2527 return put_user(res, p);
2528 case SNDCTL_DSP_CHANNELS:
2529 if (get_user(res, p))
2530 return -EFAULT;
2531 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2532 if (res < 0)
2533 return res;
2534 return put_user(res, p);
2535 case SOUND_PCM_READ_CHANNELS:
2536 res = snd_pcm_oss_get_channels(pcm_oss_file);
2537 if (res < 0)
2538 return res;
2539 return put_user(res, p);
2540 case SOUND_PCM_WRITE_FILTER:
2541 case SOUND_PCM_READ_FILTER:
2542 return -EIO;
2543 case SNDCTL_DSP_POST:
2544 return snd_pcm_oss_post(pcm_oss_file);
2545 case SNDCTL_DSP_SUBDIVIDE:
2546 if (get_user(res, p))
2547 return -EFAULT;
2548 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2549 if (res < 0)
2550 return res;
2551 return put_user(res, p);
2552 case SNDCTL_DSP_SETFRAGMENT:
2553 if (get_user(res, p))
2554 return -EFAULT;
2555 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2556 case SNDCTL_DSP_GETFMTS:
2557 res = snd_pcm_oss_get_formats(pcm_oss_file);
2558 if (res < 0)
2559 return res;
2560 return put_user(res, p);
2561 case SNDCTL_DSP_GETOSPACE:
2562 case SNDCTL_DSP_GETISPACE:
2563 return snd_pcm_oss_get_space(pcm_oss_file,
2564 cmd == SNDCTL_DSP_GETISPACE ?
2565 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2566 (struct audio_buf_info __user *) arg);
2567 case SNDCTL_DSP_NONBLOCK:
2568 return snd_pcm_oss_nonblock(file);
2569 case SNDCTL_DSP_GETCAPS:
2570 res = snd_pcm_oss_get_caps(pcm_oss_file);
2571 if (res < 0)
2572 return res;
2573 return put_user(res, p);
2574 case SNDCTL_DSP_GETTRIGGER:
2575 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2576 if (res < 0)
2577 return res;
2578 return put_user(res, p);
2579 case SNDCTL_DSP_SETTRIGGER:
2580 if (get_user(res, p))
2581 return -EFAULT;
2582 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2583 case SNDCTL_DSP_GETIPTR:
2584 case SNDCTL_DSP_GETOPTR:
2585 return snd_pcm_oss_get_ptr(pcm_oss_file,
2586 cmd == SNDCTL_DSP_GETIPTR ?
2587 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2588 (struct count_info __user *) arg);
2589 case SNDCTL_DSP_MAPINBUF:
2590 case SNDCTL_DSP_MAPOUTBUF:
2591 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2592 cmd == SNDCTL_DSP_MAPINBUF ?
2593 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2594 (struct buffmem_desc __user *) arg);
2595 case SNDCTL_DSP_SETSYNCRO:
2596 /* stop DMA now.. */
2597 return 0;
2598 case SNDCTL_DSP_SETDUPLEX:
2599 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2600 return 0;
2601 return -EIO;
2602 case SNDCTL_DSP_GETODELAY:
2603 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2604 if (res < 0) {
2605 /* it's for sure, some broken apps don't check for error codes */
2606 put_user(0, p);
2607 return res;
2608 }
2609 return put_user(res, p);
2610 case SNDCTL_DSP_PROFILE:
2611 return 0; /* silently ignore */
2612 default:
2613 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2614 }
2615 return -EINVAL;
2616}
2617
2618#ifdef CONFIG_COMPAT
2619/* all compatible */
2620#define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2621#else
2622#define snd_pcm_oss_ioctl_compat NULL
2623#endif
2624
2625static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2626{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002627 struct snd_pcm_oss_file *pcm_oss_file;
2628 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 pcm_oss_file = file->private_data;
2631 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2632 if (substream == NULL)
2633 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002634 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635#ifndef OSS_DEBUG
2636 return snd_pcm_oss_read1(substream, buf, count);
2637#else
2638 {
2639 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
Takashi Iwai006de2672009-02-05 15:51:04 +01002640 printk(KERN_DEBUG "pcm_oss: read %li bytes "
2641 "(returned %li bytes)\n", (long)count, (long)res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return res;
2643 }
2644#endif
2645}
2646
2647static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2648{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002649 struct snd_pcm_oss_file *pcm_oss_file;
2650 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 long result;
2652
2653 pcm_oss_file = file->private_data;
2654 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2655 if (substream == NULL)
2656 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002657 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 result = snd_pcm_oss_write1(substream, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002660 printk(KERN_DEBUG "pcm_oss: write %li bytes (wrote %li bytes)\n",
2661 (long)count, (long)result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662#endif
2663 return result;
2664}
2665
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002666static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002668 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002669 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2671 else
2672 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2673}
2674
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002675static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002677 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002678 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2680 else
2681 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2682}
2683
2684static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2685{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002686 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 unsigned int mask;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002688 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 pcm_oss_file = file->private_data;
2691
2692 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2693 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2694
2695 mask = 0;
2696 if (psubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002697 struct snd_pcm_runtime *runtime = psubstream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 poll_wait(file, &runtime->sleep, wait);
2699 snd_pcm_stream_lock_irq(psubstream);
2700 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2701 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2702 snd_pcm_oss_playback_ready(psubstream)))
2703 mask |= POLLOUT | POLLWRNORM;
2704 snd_pcm_stream_unlock_irq(psubstream);
2705 }
2706 if (csubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002707 struct snd_pcm_runtime *runtime = csubstream->runtime;
2708 snd_pcm_state_t ostate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 poll_wait(file, &runtime->sleep, wait);
2710 snd_pcm_stream_lock_irq(csubstream);
2711 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2712 snd_pcm_oss_capture_ready(csubstream))
2713 mask |= POLLIN | POLLRDNORM;
2714 snd_pcm_stream_unlock_irq(csubstream);
2715 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002716 struct snd_pcm_oss_file ofile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 memset(&ofile, 0, sizeof(ofile));
2718 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2719 runtime->oss.trigger = 0;
2720 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2721 }
2722 }
2723
2724 return mask;
2725}
2726
2727static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2728{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002729 struct snd_pcm_oss_file *pcm_oss_file;
2730 struct snd_pcm_substream *substream = NULL;
2731 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 int err;
2733
2734#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002735 printk(KERN_DEBUG "pcm_oss: mmap begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736#endif
2737 pcm_oss_file = file->private_data;
2738 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2739 case VM_READ | VM_WRITE:
2740 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2741 if (substream)
2742 break;
2743 /* Fall through */
2744 case VM_READ:
2745 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2746 break;
2747 case VM_WRITE:
2748 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2749 break;
2750 default:
2751 return -EINVAL;
2752 }
2753 /* set VM_READ access as well to fix memset() routines that do
2754 reads before writes (to improve performance) */
2755 area->vm_flags |= VM_READ;
2756 if (substream == NULL)
2757 return -ENXIO;
2758 runtime = substream->runtime;
2759 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2760 return -EIO;
2761 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2762 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2763 else
2764 return -EIO;
2765
2766 if (runtime->oss.params) {
2767 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2768 return err;
2769 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002770#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (runtime->oss.plugin_first != NULL)
2772 return -EIO;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 if (area->vm_pgoff != 0)
2776 return -EINVAL;
2777
2778 err = snd_pcm_mmap_data(substream, file, area);
2779 if (err < 0)
2780 return err;
2781 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2782 runtime->silence_threshold = 0;
2783 runtime->silence_size = 0;
2784#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002785 printk(KERN_DEBUG "pcm_oss: mmap ok, bytes = 0x%x\n",
2786 runtime->oss.mmap_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787#endif
2788 /* In mmap mode we never stop */
2789 runtime->stop_threshold = runtime->boundary;
2790
2791 return 0;
2792}
2793
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002794#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795/*
2796 * /proc interface
2797 */
2798
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002799static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2800 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002802 struct snd_pcm_str *pstr = entry->private_data;
2803 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002804 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 while (setup) {
2806 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2807 setup->task_name,
2808 setup->periods,
2809 setup->period_size,
2810 setup->disable ? " disable" : "",
2811 setup->direct ? " direct" : "",
2812 setup->block ? " block" : "",
2813 setup->nonblock ? " non-block" : "",
2814 setup->partialfrag ? " partial-frag" : "",
2815 setup->nosilence ? " no-silence" : "");
2816 setup = setup->next;
2817 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002818 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002821static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002823 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2826 setup; setup = setupn) {
2827 setupn = setup->next;
2828 kfree(setup->task_name);
2829 kfree(setup);
2830 }
2831 pstr->oss.setup_list = NULL;
2832}
2833
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002834static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2835 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002837 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 char line[128], str[32], task_name[32], *ptr;
2839 int idx1;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002840 struct snd_pcm_oss_setup *setup, *setup1, template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 while (!snd_info_get_line(buffer, line, sizeof(line))) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002843 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 memset(&template, 0, sizeof(template));
2845 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2846 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2847 snd_pcm_oss_proc_free_setup_list(pstr);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002848 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 continue;
2850 }
2851 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2852 if (!strcmp(setup->task_name, task_name)) {
2853 template = *setup;
2854 break;
2855 }
2856 }
2857 ptr = snd_info_get_str(str, ptr, sizeof(str));
2858 template.periods = simple_strtoul(str, NULL, 10);
2859 ptr = snd_info_get_str(str, ptr, sizeof(str));
2860 template.period_size = simple_strtoul(str, NULL, 10);
2861 for (idx1 = 31; idx1 >= 0; idx1--)
2862 if (template.period_size & (1 << idx1))
2863 break;
2864 for (idx1--; idx1 >= 0; idx1--)
2865 template.period_size &= ~(1 << idx1);
2866 do {
2867 ptr = snd_info_get_str(str, ptr, sizeof(str));
2868 if (!strcmp(str, "disable")) {
2869 template.disable = 1;
2870 } else if (!strcmp(str, "direct")) {
2871 template.direct = 1;
2872 } else if (!strcmp(str, "block")) {
2873 template.block = 1;
2874 } else if (!strcmp(str, "non-block")) {
2875 template.nonblock = 1;
2876 } else if (!strcmp(str, "partial-frag")) {
2877 template.partialfrag = 1;
2878 } else if (!strcmp(str, "no-silence")) {
2879 template.nosilence = 1;
Takashi Iwai10f69f92005-09-08 13:48:34 +02002880 } else if (!strcmp(str, "buggy-ptr")) {
2881 template.buggyptr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
2883 } while (*str);
2884 if (setup == NULL) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002885 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2886 if (! setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 buffer->error = -ENOMEM;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002888 mutex_lock(&pstr->oss.setup_mutex);
2889 return;
2890 }
2891 if (pstr->oss.setup_list == NULL)
2892 pstr->oss.setup_list = setup;
2893 else {
2894 for (setup1 = pstr->oss.setup_list;
2895 setup1->next; setup1 = setup1->next);
2896 setup1->next = setup;
2897 }
2898 template.task_name = kstrdup(task_name, GFP_KERNEL);
2899 if (! template.task_name) {
2900 kfree(setup);
2901 buffer->error = -ENOMEM;
2902 mutex_lock(&pstr->oss.setup_mutex);
2903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002906 *setup = template;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002907 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909}
2910
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002911static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 int stream;
2914 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002915 struct snd_info_entry *entry;
2916 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (pstr->substream_count == 0)
2918 continue;
2919 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2920 entry->content = SNDRV_INFO_CONTENT_TEXT;
2921 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 entry->c.text.read = snd_pcm_oss_proc_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 entry->c.text.write = snd_pcm_oss_proc_write;
2924 entry->private_data = pstr;
2925 if (snd_info_register(entry) < 0) {
2926 snd_info_free_entry(entry);
2927 entry = NULL;
2928 }
2929 }
2930 pstr->oss.proc_entry = entry;
2931 }
2932}
2933
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002934static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 int stream;
2937 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002938 struct snd_pcm_str *pstr = &pcm->streams[stream];
Takashi Iwai746d4a02006-06-23 14:37:59 +02002939 snd_info_free_entry(pstr->oss.proc_entry);
2940 pstr->oss.proc_entry = NULL;
2941 snd_pcm_oss_proc_free_setup_list(pstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
2943}
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002944#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwai04f141a2005-12-01 10:43:51 +01002945#define snd_pcm_oss_proc_init(pcm)
2946#define snd_pcm_oss_proc_done(pcm)
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002947#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949/*
2950 * ENTRY functions
2951 */
2952
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002953static const struct file_operations snd_pcm_oss_f_reg =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 .owner = THIS_MODULE,
2956 .read = snd_pcm_oss_read,
2957 .write = snd_pcm_oss_write,
2958 .open = snd_pcm_oss_open,
2959 .release = snd_pcm_oss_release,
2960 .poll = snd_pcm_oss_poll,
2961 .unlocked_ioctl = snd_pcm_oss_ioctl,
2962 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2963 .mmap = snd_pcm_oss_mmap,
2964};
2965
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002966static void register_oss_dsp(struct snd_pcm *pcm, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
2968 char name[128];
2969 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2970 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002971 pcm->card, index, &snd_pcm_oss_f_reg,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002972 pcm, name) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002973 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2974 pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976}
2977
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002978static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 pcm->oss.reg = 0;
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02002981 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 char name[128];
2983 int duplex;
2984 register_oss_dsp(pcm, 0);
2985 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2986 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2987 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2988 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2989#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2990 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2991 pcm->card->number,
2992 name);
2993#endif
2994 pcm->oss.reg++;
2995 pcm->oss.reg_mask |= 1;
2996 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02002997 if (adsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 register_oss_dsp(pcm, 1);
2999 pcm->oss.reg++;
3000 pcm->oss.reg_mask |= 2;
3001 }
3002
3003 if (pcm->oss.reg)
3004 snd_pcm_oss_proc_init(pcm);
3005
3006 return 0;
3007}
3008
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003009static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 if (pcm->oss.reg) {
3012 if (pcm->oss.reg_mask & 1) {
3013 pcm->oss.reg_mask &= ~1;
3014 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3015 pcm->card, 0);
3016 }
3017 if (pcm->oss.reg_mask & 2) {
3018 pcm->oss.reg_mask &= ~2;
3019 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3020 pcm->card, 1);
3021 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003022 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Takashi Iwaic4614822006-06-23 14:38:23 +02003023#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3024 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3025#endif
3026 }
3027 pcm->oss.reg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 return 0;
3030}
3031
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003032static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 snd_pcm_oss_disconnect_minor(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +02003035 snd_pcm_oss_proc_done(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
3037}
3038
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003039static struct snd_pcm_notify snd_pcm_oss_notify =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 .n_register = snd_pcm_oss_register_minor,
3042 .n_disconnect = snd_pcm_oss_disconnect_minor,
3043 .n_unregister = snd_pcm_oss_unregister_minor,
3044};
3045
3046static int __init alsa_pcm_oss_init(void)
3047{
3048 int i;
3049 int err;
3050
3051 /* check device map table */
3052 for (i = 0; i < SNDRV_CARDS; i++) {
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003053 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003054 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
3055 i, dsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 dsp_map[i] = 0;
3057 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003058 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003059 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3060 i, adsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 adsp_map[i] = 1;
3062 }
3063 }
3064 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3065 return err;
3066 return 0;
3067}
3068
3069static void __exit alsa_pcm_oss_exit(void)
3070{
3071 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3072}
3073
3074module_init(alsa_pcm_oss_init)
3075module_exit(alsa_pcm_oss_exit)