blob: 786a82e68890c86247052513f6c72b2d9e3523d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer / OSS compatible
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#if 0
23#define PLUGIN_DEBUG
24#endif
25#if 0
26#define OSS_DEBUG
27#endif
28
29#include <sound/driver.h>
30#include <linux/init.h>
31#include <linux/smp_lock.h>
32#include <linux/slab.h>
33#include <linux/time.h>
34#include <linux/vmalloc.h>
35#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070036#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/minors.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include "pcm_plugin.h"
42#include <sound/info.h>
43#include <linux/soundcard.h>
44#include <sound/initval.h>
45
46#define OSS_ALSAEMULVER _SIOR ('M', 249, int)
47
Takashi Iwai6581f4e2006-05-17 17:14:51 +020048static int dsp_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
50static int nonblock_open = 1;
51
52MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");
53MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
54MODULE_LICENSE("GPL");
55module_param_array(dsp_map, int, NULL, 0444);
56MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
57module_param_array(adsp_map, int, NULL, 0444);
58MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
59module_param(nonblock_open, bool, 0644);
60MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
61MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
62MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
63
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010064extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
65static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
66static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
67static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static inline mm_segment_t snd_enter_user(void)
70{
71 mm_segment_t fs = get_fs();
72 set_fs(get_ds());
73 return fs;
74}
75
76static inline void snd_leave_user(mm_segment_t fs)
77{
78 set_fs(fs);
79}
80
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020081/*
82 * helper functions to process hw_params
83 */
84static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
85{
86 int changed = 0;
87 if (i->min < min) {
88 i->min = min;
89 i->openmin = openmin;
90 changed = 1;
91 } else if (i->min == min && !i->openmin && openmin) {
92 i->openmin = 1;
93 changed = 1;
94 }
95 if (i->integer) {
96 if (i->openmin) {
97 i->min++;
98 i->openmin = 0;
99 }
100 }
101 if (snd_interval_checkempty(i)) {
102 snd_interval_none(i);
103 return -EINVAL;
104 }
105 return changed;
106}
107
108static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
109{
110 int changed = 0;
111 if (i->max > max) {
112 i->max = max;
113 i->openmax = openmax;
114 changed = 1;
115 } else if (i->max == max && !i->openmax && openmax) {
116 i->openmax = 1;
117 changed = 1;
118 }
119 if (i->integer) {
120 if (i->openmax) {
121 i->max--;
122 i->openmax = 0;
123 }
124 }
125 if (snd_interval_checkempty(i)) {
126 snd_interval_none(i);
127 return -EINVAL;
128 }
129 return changed;
130}
131
132static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
133{
134 struct snd_interval t;
135 t.empty = 0;
136 t.min = t.max = val;
137 t.openmin = t.openmax = 0;
138 t.integer = 1;
139 return snd_interval_refine(i, &t);
140}
141
142/**
143 * snd_pcm_hw_param_value_min
144 * @params: the hw_params instance
145 * @var: parameter to retrieve
146 * @dir: pointer to the direction (-1,0,1) or NULL
147 *
148 * Return the minimum value for field PAR.
149 */
150static unsigned int
151snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
152 snd_pcm_hw_param_t var, int *dir)
153{
154 if (hw_is_mask(var)) {
155 if (dir)
156 *dir = 0;
157 return snd_mask_min(hw_param_mask_c(params, var));
158 }
159 if (hw_is_interval(var)) {
160 const struct snd_interval *i = hw_param_interval_c(params, var);
161 if (dir)
162 *dir = i->openmin;
163 return snd_interval_min(i);
164 }
165 return -EINVAL;
166}
167
168/**
169 * snd_pcm_hw_param_value_max
170 * @params: the hw_params instance
171 * @var: parameter to retrieve
172 * @dir: pointer to the direction (-1,0,1) or NULL
173 *
174 * Return the maximum value for field PAR.
175 */
176static unsigned int
177snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
178 snd_pcm_hw_param_t var, int *dir)
179{
180 if (hw_is_mask(var)) {
181 if (dir)
182 *dir = 0;
183 return snd_mask_max(hw_param_mask_c(params, var));
184 }
185 if (hw_is_interval(var)) {
186 const struct snd_interval *i = hw_param_interval_c(params, var);
187 if (dir)
188 *dir = - (int) i->openmax;
189 return snd_interval_max(i);
190 }
191 return -EINVAL;
192}
193
194static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
195 snd_pcm_hw_param_t var,
196 const struct snd_mask *val)
197{
198 int changed;
199 changed = snd_mask_refine(hw_param_mask(params, var), val);
200 if (changed) {
201 params->cmask |= 1 << var;
202 params->rmask |= 1 << var;
203 }
204 return changed;
205}
206
207static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
208 struct snd_pcm_hw_params *params,
209 snd_pcm_hw_param_t var,
210 const struct snd_mask *val)
211{
212 int changed = _snd_pcm_hw_param_mask(params, var, val);
213 if (changed < 0)
214 return changed;
215 if (params->rmask) {
216 int err = snd_pcm_hw_refine(pcm, params);
217 if (err < 0)
218 return err;
219 }
220 return 0;
221}
222
223static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
224 snd_pcm_hw_param_t var, unsigned int val,
225 int dir)
226{
227 int changed;
228 int open = 0;
229 if (dir) {
230 if (dir > 0) {
231 open = 1;
232 } else if (dir < 0) {
233 if (val > 0) {
234 open = 1;
235 val--;
236 }
237 }
238 }
239 if (hw_is_mask(var))
240 changed = snd_mask_refine_min(hw_param_mask(params, var),
241 val + !!open);
242 else if (hw_is_interval(var))
243 changed = snd_interval_refine_min(hw_param_interval(params, var),
244 val, open);
245 else
246 return -EINVAL;
247 if (changed) {
248 params->cmask |= 1 << var;
249 params->rmask |= 1 << var;
250 }
251 return changed;
252}
253
254/**
255 * snd_pcm_hw_param_min
256 * @pcm: PCM instance
257 * @params: the hw_params instance
258 * @var: parameter to retrieve
259 * @val: minimal value
260 * @dir: pointer to the direction (-1,0,1) or NULL
261 *
262 * Inside configuration space defined by PARAMS remove from PAR all
263 * values < VAL. Reduce configuration space accordingly.
264 * Return new minimum or -EINVAL if the configuration space is empty
265 */
266static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
267 struct snd_pcm_hw_params *params,
268 snd_pcm_hw_param_t var, unsigned int val,
269 int *dir)
270{
271 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
272 if (changed < 0)
273 return changed;
274 if (params->rmask) {
275 int err = snd_pcm_hw_refine(pcm, params);
276 if (err < 0)
277 return err;
278 }
279 return snd_pcm_hw_param_value_min(params, var, dir);
280}
281
282static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
283 snd_pcm_hw_param_t var, unsigned int val,
284 int dir)
285{
286 int changed;
287 int open = 0;
288 if (dir) {
289 if (dir < 0) {
290 open = 1;
291 } else if (dir > 0) {
292 open = 1;
293 val++;
294 }
295 }
296 if (hw_is_mask(var)) {
297 if (val == 0 && open) {
298 snd_mask_none(hw_param_mask(params, var));
299 changed = -EINVAL;
300 } else
301 changed = snd_mask_refine_max(hw_param_mask(params, var),
302 val - !!open);
303 } else if (hw_is_interval(var))
304 changed = snd_interval_refine_max(hw_param_interval(params, var),
305 val, open);
306 else
307 return -EINVAL;
308 if (changed) {
309 params->cmask |= 1 << var;
310 params->rmask |= 1 << var;
311 }
312 return changed;
313}
314
315/**
316 * snd_pcm_hw_param_max
317 * @pcm: PCM instance
318 * @params: the hw_params instance
319 * @var: parameter to retrieve
320 * @val: maximal value
321 * @dir: pointer to the direction (-1,0,1) or NULL
322 *
323 * Inside configuration space defined by PARAMS remove from PAR all
324 * values >= VAL + 1. Reduce configuration space accordingly.
325 * Return new maximum or -EINVAL if the configuration space is empty
326 */
327static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
328 struct snd_pcm_hw_params *params,
329 snd_pcm_hw_param_t var, unsigned int val,
330 int *dir)
331{
332 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
333 if (changed < 0)
334 return changed;
335 if (params->rmask) {
336 int err = snd_pcm_hw_refine(pcm, params);
337 if (err < 0)
338 return err;
339 }
340 return snd_pcm_hw_param_value_max(params, var, dir);
341}
342
343static int boundary_sub(int a, int adir,
344 int b, int bdir,
345 int *c, int *cdir)
346{
347 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
348 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
349 *c = a - b;
350 *cdir = adir - bdir;
351 if (*cdir == -2) {
352 (*c)--;
353 } else if (*cdir == 2) {
354 (*c)++;
355 }
356 return 0;
357}
358
359static int boundary_lt(unsigned int a, int adir,
360 unsigned int b, int bdir)
361{
362 if (adir < 0) {
363 a--;
364 adir = 1;
365 } else if (adir > 0)
366 adir = 1;
367 if (bdir < 0) {
368 b--;
369 bdir = 1;
370 } else if (bdir > 0)
371 bdir = 1;
372 return a < b || (a == b && adir < bdir);
373}
374
375/* Return 1 if min is nearer to best than max */
376static int boundary_nearer(int min, int mindir,
377 int best, int bestdir,
378 int max, int maxdir)
379{
380 int dmin, dmindir;
381 int dmax, dmaxdir;
382 boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
383 boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
384 return boundary_lt(dmin, dmindir, dmax, dmaxdir);
385}
386
387/**
388 * snd_pcm_hw_param_near
389 * @pcm: PCM instance
390 * @params: the hw_params instance
391 * @var: parameter to retrieve
392 * @best: value to set
393 * @dir: pointer to the direction (-1,0,1) or NULL
394 *
395 * Inside configuration space defined by PARAMS set PAR to the available value
396 * nearest to VAL. Reduce configuration space accordingly.
397 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
398 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
399 * Return the value found.
400 */
401static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
402 struct snd_pcm_hw_params *params,
403 snd_pcm_hw_param_t var, unsigned int best,
404 int *dir)
405{
406 struct snd_pcm_hw_params *save = NULL;
407 int v;
408 unsigned int saved_min;
409 int last = 0;
410 int min, max;
411 int mindir, maxdir;
412 int valdir = dir ? *dir : 0;
413 /* FIXME */
414 if (best > INT_MAX)
415 best = INT_MAX;
416 min = max = best;
417 mindir = maxdir = valdir;
418 if (maxdir > 0)
419 maxdir = 0;
420 else if (maxdir == 0)
421 maxdir = -1;
422 else {
423 maxdir = 1;
424 max--;
425 }
426 save = kmalloc(sizeof(*save), GFP_KERNEL);
427 if (save == NULL)
428 return -ENOMEM;
429 *save = *params;
430 saved_min = min;
431 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
432 if (min >= 0) {
433 struct snd_pcm_hw_params *params1;
434 if (max < 0)
435 goto _end;
436 if ((unsigned int)min == saved_min && mindir == valdir)
437 goto _end;
438 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
439 if (params1 == NULL) {
440 kfree(save);
441 return -ENOMEM;
442 }
443 *params1 = *save;
444 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
445 if (max < 0) {
446 kfree(params1);
447 goto _end;
448 }
449 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
450 *params = *params1;
451 last = 1;
452 }
453 kfree(params1);
454 } else {
455 *params = *save;
456 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
457 snd_assert(max >= 0, return -EINVAL);
458 last = 1;
459 }
460 _end:
461 kfree(save);
462 if (last)
463 v = snd_pcm_hw_param_last(pcm, params, var, dir);
464 else
465 v = snd_pcm_hw_param_first(pcm, params, var, dir);
466 snd_assert(v >= 0, return -EINVAL);
467 return v;
468}
469
470static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
471 snd_pcm_hw_param_t var, unsigned int val,
472 int dir)
473{
474 int changed;
475 if (hw_is_mask(var)) {
476 struct snd_mask *m = hw_param_mask(params, var);
477 if (val == 0 && dir < 0) {
478 changed = -EINVAL;
479 snd_mask_none(m);
480 } else {
481 if (dir > 0)
482 val++;
483 else if (dir < 0)
484 val--;
485 changed = snd_mask_refine_set(hw_param_mask(params, var), val);
486 }
487 } else if (hw_is_interval(var)) {
488 struct snd_interval *i = hw_param_interval(params, var);
489 if (val == 0 && dir < 0) {
490 changed = -EINVAL;
491 snd_interval_none(i);
492 } else if (dir == 0)
493 changed = snd_interval_refine_set(i, val);
494 else {
495 struct snd_interval t;
496 t.openmin = 1;
497 t.openmax = 1;
498 t.empty = 0;
499 t.integer = 0;
500 if (dir < 0) {
501 t.min = val - 1;
502 t.max = val;
503 } else {
504 t.min = val;
505 t.max = val+1;
506 }
507 changed = snd_interval_refine(i, &t);
508 }
509 } else
510 return -EINVAL;
511 if (changed) {
512 params->cmask |= 1 << var;
513 params->rmask |= 1 << var;
514 }
515 return changed;
516}
517
518/**
519 * snd_pcm_hw_param_set
520 * @pcm: PCM instance
521 * @params: the hw_params instance
522 * @var: parameter to retrieve
523 * @val: value to set
524 * @dir: pointer to the direction (-1,0,1) or NULL
525 *
526 * Inside configuration space defined by PARAMS remove from PAR all
527 * values != VAL. Reduce configuration space accordingly.
528 * Return VAL or -EINVAL if the configuration space is empty
529 */
530static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
531 struct snd_pcm_hw_params *params,
532 snd_pcm_hw_param_t var, unsigned int val,
533 int dir)
534{
535 int changed = _snd_pcm_hw_param_set(params, var, val, dir);
536 if (changed < 0)
537 return changed;
538 if (params->rmask) {
539 int err = snd_pcm_hw_refine(pcm, params);
540 if (err < 0)
541 return err;
542 }
543 return snd_pcm_hw_param_value(params, var, NULL);
544}
545
546static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
547 snd_pcm_hw_param_t var)
548{
549 int changed;
550 changed = snd_interval_setinteger(hw_param_interval(params, var));
551 if (changed) {
552 params->cmask |= 1 << var;
553 params->rmask |= 1 << var;
554 }
555 return changed;
556}
557
558/*
559 * plugin
560 */
561
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100562#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100563static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100565 struct snd_pcm_runtime *runtime = substream->runtime;
566 struct snd_pcm_plugin *plugin, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 plugin = runtime->oss.plugin_first;
569 while (plugin) {
570 next = plugin->next;
571 snd_pcm_plugin_free(plugin);
572 plugin = next;
573 }
574 runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
575 return 0;
576}
577
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100578static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100580 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 plugin->next = runtime->oss.plugin_first;
582 plugin->prev = NULL;
583 if (runtime->oss.plugin_first) {
584 runtime->oss.plugin_first->prev = plugin;
585 runtime->oss.plugin_first = plugin;
586 } else {
587 runtime->oss.plugin_last =
588 runtime->oss.plugin_first = plugin;
589 }
590 return 0;
591}
592
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100593int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100595 struct snd_pcm_runtime *runtime = plugin->plug->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 plugin->next = NULL;
597 plugin->prev = runtime->oss.plugin_last;
598 if (runtime->oss.plugin_last) {
599 runtime->oss.plugin_last->next = plugin;
600 runtime->oss.plugin_last = plugin;
601 } else {
602 runtime->oss.plugin_last =
603 runtime->oss.plugin_first = plugin;
604 }
605 return 0;
606}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100607#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100609static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100611 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200612 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200613 long bytes = frames_to_bytes(runtime, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (buffer_size == runtime->oss.buffer_bytes)
Jaroslav Kyselabfc5bdd2005-05-27 11:12:35 +0200615 return bytes;
Takashi Iwaicdc5c53f2005-05-27 12:40:52 +0200616#if BITS_PER_LONG >= 64
617 return runtime->oss.buffer_bytes * bytes / buffer_size;
618#else
619 {
620 u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
621 u32 rem;
622 div64_32(&bsize, buffer_size, &rem);
623 return (long)bsize;
624 }
625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100628static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100630 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200631 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (buffer_size == runtime->oss.buffer_bytes)
633 return bytes_to_frames(runtime, bytes);
634 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
635}
636
637static int snd_pcm_oss_format_from(int format)
638{
639 switch (format) {
640 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
641 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
642 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
643 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
644 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
645 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
646 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
647 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
648 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
649 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
650 default: return SNDRV_PCM_FORMAT_U8;
651 }
652}
653
654static int snd_pcm_oss_format_to(int format)
655{
656 switch (format) {
657 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
658 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
659 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
660 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
661 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
662 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
663 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
664 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
665 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
666 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
667 default: return -EINVAL;
668 }
669}
670
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100671static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
672 struct snd_pcm_hw_params *oss_params,
673 struct snd_pcm_hw_params *slave_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 size_t s;
676 size_t oss_buffer_size, oss_period_size, oss_periods;
677 size_t min_period_size, max_period_size;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100678 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 size_t oss_frame_size;
680
681 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
682 params_channels(oss_params) / 8;
683
684 oss_buffer_size = snd_pcm_plug_client_size(substream,
685 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
686 oss_buffer_size = 1 << ld2(oss_buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200687 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (oss_buffer_size > runtime->oss.mmap_bytes)
689 oss_buffer_size = runtime->oss.mmap_bytes;
690 }
691
Takashi Iwai060d77b2006-03-27 16:44:52 +0200692 if (substream->oss.setup.period_size > 16)
693 oss_period_size = substream->oss.setup.period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 else if (runtime->oss.fragshift) {
695 oss_period_size = 1 << runtime->oss.fragshift;
696 if (oss_period_size > oss_buffer_size / 2)
697 oss_period_size = oss_buffer_size / 2;
698 } else {
699 int sd;
700 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
701
702 oss_period_size = oss_buffer_size;
703 do {
704 oss_period_size /= 2;
705 } while (oss_period_size > bytes_per_sec);
706 if (runtime->oss.subdivision == 0) {
707 sd = 4;
708 if (oss_period_size / sd > 4096)
709 sd *= 2;
710 if (oss_period_size / sd < 4096)
711 sd = 1;
712 } else
713 sd = runtime->oss.subdivision;
714 oss_period_size /= sd;
715 if (oss_period_size < 16)
716 oss_period_size = 16;
717 }
718
719 min_period_size = snd_pcm_plug_client_size(substream,
720 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
721 min_period_size *= oss_frame_size;
722 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
723 if (oss_period_size < min_period_size)
724 oss_period_size = min_period_size;
725
726 max_period_size = snd_pcm_plug_client_size(substream,
727 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
728 max_period_size *= oss_frame_size;
729 max_period_size = 1 << ld2(max_period_size);
730 if (oss_period_size > max_period_size)
731 oss_period_size = max_period_size;
732
733 oss_periods = oss_buffer_size / oss_period_size;
734
Takashi Iwai060d77b2006-03-27 16:44:52 +0200735 if (substream->oss.setup.periods > 1)
736 oss_periods = substream->oss.setup.periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
739 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
740 s = runtime->oss.maxfrags;
741 if (oss_periods > s)
742 oss_periods = s;
743
744 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
745 if (s < 2)
746 s = 2;
747 if (oss_periods < s)
748 oss_periods = s;
749
750 while (oss_period_size * oss_periods > oss_buffer_size)
751 oss_period_size /= 2;
752
753 snd_assert(oss_period_size >= 16, return -EINVAL);
754 runtime->oss.period_bytes = oss_period_size;
755 runtime->oss.period_frames = 1;
756 runtime->oss.periods = oss_periods;
757 return 0;
758}
759
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100760static int choose_rate(struct snd_pcm_substream *substream,
761 struct snd_pcm_hw_params *params, unsigned int best_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100763 struct snd_interval *it;
764 struct snd_pcm_hw_params *save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int rate, prev;
766
767 save = kmalloc(sizeof(*save), GFP_KERNEL);
768 if (save == NULL)
769 return -ENOMEM;
770 *save = *params;
771 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
772
773 /* try multiples of the best rate */
774 rate = best_rate;
775 for (;;) {
776 if (it->max < rate || (it->max == rate && it->openmax))
777 break;
778 if (it->min < rate || (it->min == rate && !it->openmin)) {
779 int ret;
780 ret = snd_pcm_hw_param_set(substream, params,
781 SNDRV_PCM_HW_PARAM_RATE,
782 rate, 0);
783 if (ret == (int)rate) {
784 kfree(save);
785 return rate;
786 }
787 *params = *save;
788 }
789 prev = rate;
790 rate += best_rate;
791 if (rate <= prev)
792 break;
793 }
794
795 /* not found, use the nearest rate */
796 kfree(save);
797 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
798}
799
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100800static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100802 struct snd_pcm_runtime *runtime = substream->runtime;
803 struct snd_pcm_hw_params *params, *sparams;
804 struct snd_pcm_sw_params *sw_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ssize_t oss_buffer_size, oss_period_size;
806 size_t oss_frame_size;
807 int err;
808 int direct;
809 int format, sformat, n;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100810 struct snd_mask sformat_mask;
811 struct snd_mask mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Takashi Iwaie3a5d592006-11-14 13:03:19 +0100813 if (mutex_lock_interruptible(&runtime->oss.params_lock))
814 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
816 params = kmalloc(sizeof(*params), GFP_KERNEL);
817 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
818 if (!sw_params || !params || !sparams) {
819 snd_printd("No memory\n");
820 err = -ENOMEM;
821 goto failure;
822 }
823
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200824 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +0200826 else
827 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 _snd_pcm_hw_params_any(sparams);
830 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
831 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
832 snd_mask_none(&mask);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200833 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
835 else {
836 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
837 if (!direct)
838 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
839 }
840 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
841 if (err < 0) {
842 snd_printd("No usable accesses\n");
843 err = -EINVAL;
844 goto failure;
845 }
846 choose_rate(substream, sparams, runtime->oss.rate);
847 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
848
849 format = snd_pcm_oss_format_from(runtime->oss.format);
850
851 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
852 if (direct)
853 sformat = format;
854 else
855 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
856
857 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
858 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
859 if (snd_mask_test(&sformat_mask, sformat) &&
860 snd_pcm_oss_format_to(sformat) >= 0)
861 break;
862 }
863 if (sformat > SNDRV_PCM_FORMAT_LAST) {
864 snd_printd("Cannot find a format!!!\n");
865 err = -EINVAL;
866 goto failure;
867 }
868 }
869 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
870 snd_assert(err >= 0, goto failure);
871
872 if (direct) {
873 memcpy(params, sparams, sizeof(*params));
874 } else {
875 _snd_pcm_hw_params_any(params);
876 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
877 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
878 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
879 snd_pcm_oss_format_from(runtime->oss.format), 0);
880 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
881 runtime->oss.channels, 0);
882 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
883 runtime->oss.rate, 0);
884 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
885 params_access(params), params_format(params),
886 params_channels(params), params_rate(params));
887 }
888 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
889 params_access(sparams), params_format(sparams),
890 params_channels(sparams), params_rate(sparams));
891
892 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
893 params_channels(params) / 8;
894
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100895#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 snd_pcm_oss_plugin_clear(substream);
897 if (!direct) {
898 /* add necessary plugins */
899 snd_pcm_oss_plugin_clear(substream);
900 if ((err = snd_pcm_plug_format_plugins(substream,
901 params,
902 sparams)) < 0) {
903 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
904 snd_pcm_oss_plugin_clear(substream);
905 goto failure;
906 }
907 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100908 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
910 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
911 snd_pcm_oss_plugin_clear(substream);
912 goto failure;
913 }
914 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
915 err = snd_pcm_plugin_append(plugin);
916 } else {
917 err = snd_pcm_plugin_insert(plugin);
918 }
919 if (err < 0) {
920 snd_pcm_oss_plugin_clear(substream);
921 goto failure;
922 }
923 }
924 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 err = snd_pcm_oss_period_size(substream, params, sparams);
928 if (err < 0)
929 goto failure;
930
931 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
932 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
933 snd_assert(err >= 0, goto failure);
934
935 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
936 runtime->oss.periods, NULL);
937 snd_assert(err >= 0, goto failure);
938
939 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
940
941 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
942 snd_printd("HW_PARAMS failed: %i\n", err);
943 goto failure;
944 }
945
946 memset(sw_params, 0, sizeof(*sw_params));
947 if (runtime->oss.trigger) {
948 sw_params->start_threshold = 1;
949 } else {
950 sw_params->start_threshold = runtime->boundary;
951 }
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200952 if (atomic_read(&substream->mmap_count) ||
953 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 sw_params->stop_threshold = runtime->boundary;
955 else
956 sw_params->stop_threshold = runtime->buffer_size;
957 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
958 sw_params->period_step = 1;
959 sw_params->sleep_min = 0;
Takashi Iwai004e6532005-04-12 17:33:59 +0200960 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
961 1 : runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sw_params->xfer_align = 1;
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200963 if (atomic_read(&substream->mmap_count) ||
Takashi Iwai060d77b2006-03-27 16:44:52 +0200964 substream->oss.setup.nosilence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sw_params->silence_threshold = 0;
966 sw_params->silence_size = 0;
967 } else {
968 snd_pcm_uframes_t frames;
969 frames = runtime->period_size + 16;
970 if (frames > runtime->buffer_size)
971 frames = runtime->buffer_size;
972 sw_params->silence_threshold = frames;
973 sw_params->silence_size = frames;
974 }
975
976 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
977 snd_printd("SW_PARAMS failed: %i\n", err);
978 goto failure;
979 }
980
981 runtime->oss.periods = params_periods(sparams);
982 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
983 snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100984#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (runtime->oss.plugin_first) {
986 err = snd_pcm_plug_alloc(substream, oss_period_size);
987 if (err < 0)
988 goto failure;
989 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 oss_period_size *= oss_frame_size;
992
993 oss_buffer_size = oss_period_size * runtime->oss.periods;
994 snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
995
996 runtime->oss.period_bytes = oss_period_size;
997 runtime->oss.buffer_bytes = oss_buffer_size;
998
999 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1000 runtime->oss.period_bytes,
1001 runtime->oss.buffer_bytes);
1002 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1003 params_period_size(sparams),
1004 params_buffer_size(sparams));
1005
1006 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1007 runtime->oss.channels = params_channels(params);
1008 runtime->oss.rate = params_rate(params);
1009
1010 runtime->oss.params = 0;
1011 runtime->oss.prepare = 1;
1012 vfree(runtime->oss.buffer);
1013 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1014 runtime->oss.buffer_used = 0;
1015 if (runtime->dma_area)
1016 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1017
1018 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1019
1020 err = 0;
1021failure:
1022 kfree(sw_params);
1023 kfree(params);
1024 kfree(sparams);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001025 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return err;
1027}
1028
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001029static 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 -07001030{
1031 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001032 struct snd_pcm_substream *asubstream = NULL, *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 for (idx = 0; idx < 2; idx++) {
1035 substream = pcm_oss_file->streams[idx];
1036 if (substream == NULL)
1037 continue;
1038 if (asubstream == NULL)
1039 asubstream = substream;
1040 if (substream->runtime->oss.params) {
1041 err = snd_pcm_oss_change_params(substream);
1042 if (err < 0)
1043 return err;
1044 }
1045 }
1046 snd_assert(asubstream != NULL, return -EIO);
1047 if (r_substream)
1048 *r_substream = asubstream;
1049 return 0;
1050}
1051
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001052static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 int err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001055 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1058 if (err < 0) {
1059 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1060 return err;
1061 }
1062 runtime->oss.prepare = 0;
1063 runtime->oss.prev_hw_ptr_interrupt = 0;
1064 runtime->oss.period_ptr = 0;
1065 runtime->oss.buffer_used = 0;
1066
1067 return 0;
1068}
1069
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001070static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001072 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int err;
1074
1075 if (substream == NULL)
1076 return 0;
1077 runtime = substream->runtime;
1078 if (runtime->oss.params) {
1079 err = snd_pcm_oss_change_params(substream);
1080 if (err < 0)
1081 return err;
1082 }
1083 if (runtime->oss.prepare) {
1084 err = snd_pcm_oss_prepare(substream);
1085 if (err < 0)
1086 return err;
1087 }
1088 return 0;
1089}
1090
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001091static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001093 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 snd_pcm_uframes_t frames;
1095 int err = 0;
1096
1097 while (1) {
1098 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1099 if (err < 0)
1100 break;
1101 runtime = substream->runtime;
1102 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1103 break;
1104 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1105 /* until avail(delay) <= buffer_size */
1106 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1107 frames /= runtime->period_size;
1108 frames *= runtime->period_size;
1109 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1110 if (err < 0)
1111 break;
1112 }
1113 return err;
1114}
1115
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001116snd_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 -07001117{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001118 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int ret;
1120 while (1) {
1121 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1122 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1123#ifdef OSS_DEBUG
1124 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1125 printk("pcm_oss: write: recovering from XRUN\n");
1126 else
1127 printk("pcm_oss: write: recovering from SUSPEND\n");
1128#endif
1129 ret = snd_pcm_oss_prepare(substream);
1130 if (ret < 0)
1131 break;
1132 }
1133 if (in_kernel) {
1134 mm_segment_t fs;
1135 fs = snd_enter_user();
1136 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1137 snd_leave_user(fs);
1138 } else {
1139 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1140 }
1141 if (ret != -EPIPE && ret != -ESTRPIPE)
1142 break;
1143 /* test, if we can't store new data, because the stream */
1144 /* has not been started */
1145 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1146 return -EAGAIN;
1147 }
1148 return ret;
1149}
1150
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001151snd_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 -07001152{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001153 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 snd_pcm_sframes_t delay;
1155 int ret;
1156 while (1) {
1157 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1158 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1159#ifdef OSS_DEBUG
1160 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1161 printk("pcm_oss: read: recovering from XRUN\n");
1162 else
1163 printk("pcm_oss: read: recovering from SUSPEND\n");
1164#endif
1165 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1166 if (ret < 0)
1167 break;
1168 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1169 ret = snd_pcm_oss_prepare(substream);
1170 if (ret < 0)
1171 break;
1172 }
1173 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1174 if (ret < 0)
1175 break;
1176 if (in_kernel) {
1177 mm_segment_t fs;
1178 fs = snd_enter_user();
1179 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1180 snd_leave_user(fs);
1181 } else {
1182 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1183 }
1184 if (ret == -EPIPE) {
1185 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1186 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1187 if (ret < 0)
1188 break;
1189 }
1190 continue;
1191 }
1192 if (ret != -ESTRPIPE)
1193 break;
1194 }
1195 return ret;
1196}
1197
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001198snd_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 -07001199{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001200 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int ret;
1202 while (1) {
1203 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1204 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1205#ifdef OSS_DEBUG
1206 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1207 printk("pcm_oss: writev: recovering from XRUN\n");
1208 else
1209 printk("pcm_oss: writev: recovering from SUSPEND\n");
1210#endif
1211 ret = snd_pcm_oss_prepare(substream);
1212 if (ret < 0)
1213 break;
1214 }
1215 if (in_kernel) {
1216 mm_segment_t fs;
1217 fs = snd_enter_user();
1218 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1219 snd_leave_user(fs);
1220 } else {
1221 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1222 }
1223 if (ret != -EPIPE && ret != -ESTRPIPE)
1224 break;
1225
1226 /* test, if we can't store new data, because the stream */
1227 /* has not been started */
1228 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1229 return -EAGAIN;
1230 }
1231 return ret;
1232}
1233
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001234snd_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 -07001235{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001236 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int ret;
1238 while (1) {
1239 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1240 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1241#ifdef OSS_DEBUG
1242 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
1243 printk("pcm_oss: readv: recovering from XRUN\n");
1244 else
1245 printk("pcm_oss: readv: recovering from SUSPEND\n");
1246#endif
1247 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1248 if (ret < 0)
1249 break;
1250 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1251 ret = snd_pcm_oss_prepare(substream);
1252 if (ret < 0)
1253 break;
1254 }
1255 if (in_kernel) {
1256 mm_segment_t fs;
1257 fs = snd_enter_user();
1258 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1259 snd_leave_user(fs);
1260 } else {
1261 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1262 }
1263 if (ret != -EPIPE && ret != -ESTRPIPE)
1264 break;
1265 }
1266 return ret;
1267}
1268
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001269static 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 -07001270{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001271 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001273#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001275 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1277 if (!in_kernel) {
1278 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1279 return -EFAULT;
1280 buf = runtime->oss.buffer;
1281 }
1282 frames = bytes / oss_frame_bytes;
1283 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1284 if (frames1 < 0)
1285 return frames1;
1286 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1287 if (frames1 <= 0)
1288 return frames1;
1289 bytes = frames1 * oss_frame_bytes;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001290 } else
1291#endif
1292 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 frames = bytes_to_frames(runtime, bytes);
1294 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1295 if (frames1 <= 0)
1296 return frames1;
1297 bytes = frames_to_bytes(runtime, frames1);
1298 }
1299 return bytes;
1300}
1301
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001302static 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 -07001303{
1304 size_t xfer = 0;
1305 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001306 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001308 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -ENXIO;
1310
1311 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1312 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001313 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 while (bytes > 0) {
1315 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1316 tmp = bytes;
1317 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1318 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1319 if (tmp > 0) {
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001320 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1321 tmp = -EFAULT;
1322 goto err;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 runtime->oss.buffer_used += tmp;
1326 buf += tmp;
1327 bytes -= tmp;
1328 xfer += tmp;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001329 if (substream->oss.setup.partialfrag ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1331 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1332 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1333 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001334 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 runtime->oss.bytes += tmp;
1336 runtime->oss.period_ptr += tmp;
1337 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1338 if (runtime->oss.period_ptr == 0 ||
1339 runtime->oss.period_ptr == runtime->oss.buffer_used)
1340 runtime->oss.buffer_used = 0;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001341 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1342 tmp = -EAGAIN;
1343 goto err;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001347 tmp = snd_pcm_oss_write2(substream,
1348 (const char __force *)buf,
1349 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001351 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 runtime->oss.bytes += tmp;
1353 buf += tmp;
1354 bytes -= tmp;
1355 xfer += tmp;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001356 if ((substream->f_flags & O_NONBLOCK) != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 tmp != runtime->oss.period_bytes)
1358 break;
1359 }
1360 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001361 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001363
1364 err:
1365 mutex_unlock(&runtime->oss.params_lock);
1366 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001369static 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 -07001370{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001371 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001373#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 char __user *final_dst = (char __user *)buf;
1375 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001376 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1378 if (!in_kernel)
1379 buf = runtime->oss.buffer;
1380 frames = bytes / oss_frame_bytes;
1381 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1382 if (frames1 < 0)
1383 return frames1;
1384 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1385 if (frames1 <= 0)
1386 return frames1;
1387 bytes = frames1 * oss_frame_bytes;
1388 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1389 return -EFAULT;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001390 } else
1391#endif
1392 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 frames = bytes_to_frames(runtime, bytes);
1394 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1395 if (frames1 <= 0)
1396 return frames1;
1397 bytes = frames_to_bytes(runtime, frames1);
1398 }
1399 return bytes;
1400}
1401
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001402static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 size_t xfer = 0;
1405 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001406 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001408 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -ENXIO;
1410
1411 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1412 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001413 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 while (bytes > 0) {
1415 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1416 if (runtime->oss.buffer_used == 0) {
1417 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1418 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001419 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 runtime->oss.bytes += tmp;
1421 runtime->oss.period_ptr = tmp;
1422 runtime->oss.buffer_used = tmp;
1423 }
1424 tmp = bytes;
1425 if ((size_t) tmp > runtime->oss.buffer_used)
1426 tmp = runtime->oss.buffer_used;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001427 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1428 tmp = -EFAULT;
1429 goto err;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 buf += tmp;
1432 bytes -= tmp;
1433 xfer += tmp;
1434 runtime->oss.buffer_used -= tmp;
1435 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001436 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1437 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001439 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 runtime->oss.bytes += tmp;
1441 buf += tmp;
1442 bytes -= tmp;
1443 xfer += tmp;
1444 }
1445 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001446 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001448
1449 err:
1450 mutex_unlock(&runtime->oss.params_lock);
1451 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001454static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001456 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1459 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001460 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 substream->runtime->oss.prepare = 1;
1462 }
1463 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1464 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001465 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 substream->runtime->oss.prepare = 1;
1467 }
1468 return 0;
1469}
1470
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001471static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001473 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int err;
1475
1476 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1477 if (substream != NULL) {
1478 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1479 return err;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001480 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 /* note: all errors from the start action are ignored */
1483 /* OSS apps do not know, how to handle them */
1484 return 0;
1485}
1486
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001487static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001489 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 ssize_t result = 0;
1491 long res;
1492 wait_queue_t wait;
1493
1494 runtime = substream->runtime;
1495 init_waitqueue_entry(&wait, current);
1496 add_wait_queue(&runtime->sleep, &wait);
1497#ifdef OSS_DEBUG
1498 printk("sync1: size = %li\n", size);
1499#endif
1500 while (1) {
1501 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1502 if (result > 0) {
1503 runtime->oss.buffer_used = 0;
1504 result = 0;
1505 break;
1506 }
1507 if (result != 0 && result != -EAGAIN)
1508 break;
1509 result = 0;
1510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_lock_irq(substream);
1512 res = runtime->status->state;
1513 snd_pcm_stream_unlock_irq(substream);
1514 if (res != SNDRV_PCM_STATE_RUNNING) {
1515 set_current_state(TASK_RUNNING);
1516 break;
1517 }
1518 res = schedule_timeout(10 * HZ);
1519 if (signal_pending(current)) {
1520 result = -ERESTARTSYS;
1521 break;
1522 }
1523 if (res == 0) {
1524 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1525 result = -EIO;
1526 break;
1527 }
1528 }
1529 remove_wait_queue(&runtime->sleep, &wait);
1530 return result;
1531}
1532
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001533static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 int err = 0;
1536 unsigned int saved_f_flags;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001537 struct snd_pcm_substream *substream;
1538 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 snd_pcm_format_t format;
1540 unsigned long width;
1541 size_t size;
1542
1543 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1544 if (substream != NULL) {
1545 runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001546 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto __direct;
1548 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1549 return err;
1550 format = snd_pcm_oss_format_from(runtime->oss.format);
1551 width = snd_pcm_format_physical_width(format);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001552 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (runtime->oss.buffer_used > 0) {
1554#ifdef OSS_DEBUG
1555 printk("sync: buffer_used\n");
1556#endif
1557 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1558 snd_pcm_format_set_silence(format,
1559 runtime->oss.buffer + runtime->oss.buffer_used,
1560 size);
1561 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001562 if (err < 0) {
1563 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 } else if (runtime->oss.period_ptr > 0) {
1567#ifdef OSS_DEBUG
1568 printk("sync: period_ptr\n");
1569#endif
1570 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1571 snd_pcm_format_set_silence(format,
1572 runtime->oss.buffer,
1573 size * 8 / width);
1574 err = snd_pcm_oss_sync1(substream, size);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001575 if (err < 0) {
1576 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 /*
1581 * The ALSA's period might be a bit large than OSS one.
1582 * Fill the remain portion of ALSA period with zeros.
1583 */
1584 size = runtime->control->appl_ptr % runtime->period_size;
1585 if (size > 0) {
1586 size = runtime->period_size - size;
1587 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1588 size = (runtime->frame_bits * size) / 8;
1589 while (size > 0) {
1590 mm_segment_t fs;
1591 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1592 size -= size1;
1593 size1 *= 8;
1594 size1 /= runtime->sample_bits;
1595 snd_pcm_format_set_silence(runtime->format,
1596 runtime->oss.buffer,
1597 size1);
1598 fs = snd_enter_user();
1599 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1600 snd_leave_user(fs);
1601 }
1602 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1603 void __user *buffers[runtime->channels];
1604 memset(buffers, 0, runtime->channels * sizeof(void *));
1605 snd_pcm_lib_writev(substream, buffers, size);
1606 }
1607 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001608 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * finish sync: drain the buffer
1611 */
1612 __direct:
Takashi Iwai0df63e42006-04-28 15:13:41 +02001613 saved_f_flags = substream->f_flags;
1614 substream->f_flags &= ~O_NONBLOCK;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001615 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
Takashi Iwai0df63e42006-04-28 15:13:41 +02001616 substream->f_flags = saved_f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (err < 0)
1618 return err;
1619 runtime->oss.prepare = 1;
1620 }
1621
1622 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1623 if (substream != NULL) {
1624 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1625 return err;
1626 runtime = substream->runtime;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001627 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (err < 0)
1629 return err;
1630 runtime->oss.buffer_used = 0;
1631 runtime->oss.prepare = 1;
1632 }
1633 return 0;
1634}
1635
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001636static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 int idx;
1639
1640 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001641 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1642 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (substream == NULL)
1644 continue;
1645 runtime = substream->runtime;
1646 if (rate < 1000)
1647 rate = 1000;
1648 else if (rate > 192000)
1649 rate = 192000;
1650 if (runtime->oss.rate != rate) {
1651 runtime->oss.params = 1;
1652 runtime->oss.rate = rate;
1653 }
1654 }
1655 return snd_pcm_oss_get_rate(pcm_oss_file);
1656}
1657
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001658static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001660 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 int err;
1662
1663 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1664 return err;
1665 return substream->runtime->oss.rate;
1666}
1667
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001668static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 int idx;
1671 if (channels < 1)
1672 channels = 1;
1673 if (channels > 128)
1674 return -EINVAL;
1675 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001676 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1677 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (substream == NULL)
1679 continue;
1680 runtime = substream->runtime;
1681 if (runtime->oss.channels != channels) {
1682 runtime->oss.params = 1;
1683 runtime->oss.channels = channels;
1684 }
1685 }
1686 return snd_pcm_oss_get_channels(pcm_oss_file);
1687}
1688
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001689static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001691 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int err;
1693
1694 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1695 return err;
1696 return substream->runtime->oss.channels;
1697}
1698
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001699static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001701 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int err;
1703
1704 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1705 return err;
1706 return substream->runtime->oss.period_bytes;
1707}
1708
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001709static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001711 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int err;
1713 int direct;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001714 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned int formats = 0;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001716 struct snd_mask format_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int fmt;
1718
1719 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1720 return err;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001721 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001723 else
1724 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (!direct)
1726 return AFMT_MU_LAW | AFMT_U8 |
1727 AFMT_S16_LE | AFMT_S16_BE |
1728 AFMT_S8 | AFMT_U16_LE |
1729 AFMT_U16_BE;
1730 params = kmalloc(sizeof(*params), GFP_KERNEL);
1731 if (!params)
1732 return -ENOMEM;
1733 _snd_pcm_hw_params_any(params);
1734 err = snd_pcm_hw_refine(substream, params);
1735 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1736 kfree(params);
1737 snd_assert(err >= 0, return err);
1738 for (fmt = 0; fmt < 32; ++fmt) {
1739 if (snd_mask_test(&format_mask, fmt)) {
1740 int f = snd_pcm_oss_format_to(fmt);
1741 if (f >= 0)
1742 formats |= f;
1743 }
1744 }
1745 return formats;
1746}
1747
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001748static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int formats, idx;
1751
1752 if (format != AFMT_QUERY) {
1753 formats = snd_pcm_oss_get_formats(pcm_oss_file);
Steven Finney5c59e092006-04-13 12:49:31 +02001754 if (formats < 0)
1755 return formats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (!(formats & format))
1757 format = AFMT_U8;
1758 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001759 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1760 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (substream == NULL)
1762 continue;
1763 runtime = substream->runtime;
1764 if (runtime->oss.format != format) {
1765 runtime->oss.params = 1;
1766 runtime->oss.format = format;
1767 }
1768 }
1769 }
1770 return snd_pcm_oss_get_format(pcm_oss_file);
1771}
1772
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001773static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001775 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int err;
1777
1778 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1779 return err;
1780 return substream->runtime->oss.format;
1781}
1782
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001783static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001785 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (substream == NULL)
1788 return 0;
1789 runtime = substream->runtime;
1790 if (subdivide == 0) {
1791 subdivide = runtime->oss.subdivision;
1792 if (subdivide == 0)
1793 subdivide = 1;
1794 return subdivide;
1795 }
1796 if (runtime->oss.subdivision || runtime->oss.fragshift)
1797 return -EINVAL;
1798 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1799 subdivide != 8 && subdivide != 16)
1800 return -EINVAL;
1801 runtime->oss.subdivision = subdivide;
1802 runtime->oss.params = 1;
1803 return subdivide;
1804}
1805
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001806static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 int err = -EINVAL, idx;
1809
1810 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001811 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (substream == NULL)
1813 continue;
1814 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1815 return err;
1816 }
1817 return err;
1818}
1819
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001820static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001822 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (substream == NULL)
1825 return 0;
1826 runtime = substream->runtime;
1827 if (runtime->oss.subdivision || runtime->oss.fragshift)
1828 return -EINVAL;
1829 runtime->oss.fragshift = val & 0xffff;
1830 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1831 if (runtime->oss.fragshift < 4) /* < 16 */
1832 runtime->oss.fragshift = 4;
1833 if (runtime->oss.maxfrags < 2)
1834 runtime->oss.maxfrags = 2;
1835 runtime->oss.params = 1;
1836 return 0;
1837}
1838
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001839static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 int err = -EINVAL, idx;
1842
1843 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001844 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (substream == NULL)
1846 continue;
1847 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1848 return err;
1849 }
1850 return err;
1851}
1852
1853static int snd_pcm_oss_nonblock(struct file * file)
1854{
1855 file->f_flags |= O_NONBLOCK;
1856 return 0;
1857}
1858
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001859static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861
1862 if (substream == NULL) {
1863 res &= ~DSP_CAP_DUPLEX;
1864 return res;
1865 }
1866#ifdef DSP_CAP_MULTI
1867 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1868 if (substream->pstr->substream_count > 1)
1869 res |= DSP_CAP_MULTI;
1870#endif
1871 /* DSP_CAP_REALTIME is set all times: */
1872 /* all ALSA drivers can return actual pointer in ring buffer */
1873#if defined(DSP_CAP_REALTIME) && 0
1874 {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001875 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1877 res &= ~DSP_CAP_REALTIME;
1878 }
1879#endif
1880 return res;
1881}
1882
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001883static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 int result, idx;
1886
1887 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1888 for (idx = 0; idx < 2; idx++) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001889 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 result = snd_pcm_oss_get_caps1(substream, result);
1891 }
1892 result |= 0x0001; /* revision - same as SB AWE 64 */
1893 return result;
1894}
1895
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001896static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001898 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 snd_pcm_uframes_t appl_ptr;
1900 appl_ptr = hw_ptr + runtime->buffer_size;
1901 appl_ptr %= runtime->boundary;
1902 runtime->control->appl_ptr = appl_ptr;
1903}
1904
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001905static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001907 struct snd_pcm_runtime *runtime;
1908 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int err, cmd;
1910
1911#ifdef OSS_DEBUG
1912 printk("pcm_oss: trigger = 0x%x\n", trigger);
1913#endif
1914
1915 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1916 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1917
1918 if (psubstream) {
1919 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1920 return err;
1921 }
1922 if (csubstream) {
1923 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1924 return err;
1925 }
1926 if (psubstream) {
1927 runtime = psubstream->runtime;
1928 if (trigger & PCM_ENABLE_OUTPUT) {
1929 if (runtime->oss.trigger)
1930 goto _skip1;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001931 if (atomic_read(&psubstream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
1933 runtime->oss.trigger = 1;
1934 runtime->start_threshold = 1;
1935 cmd = SNDRV_PCM_IOCTL_START;
1936 } else {
1937 if (!runtime->oss.trigger)
1938 goto _skip1;
1939 runtime->oss.trigger = 0;
1940 runtime->start_threshold = runtime->boundary;
1941 cmd = SNDRV_PCM_IOCTL_DROP;
1942 runtime->oss.prepare = 1;
1943 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001944 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (err < 0)
1946 return err;
1947 }
1948 _skip1:
1949 if (csubstream) {
1950 runtime = csubstream->runtime;
1951 if (trigger & PCM_ENABLE_INPUT) {
1952 if (runtime->oss.trigger)
1953 goto _skip2;
1954 runtime->oss.trigger = 1;
1955 runtime->start_threshold = 1;
1956 cmd = SNDRV_PCM_IOCTL_START;
1957 } else {
1958 if (!runtime->oss.trigger)
1959 goto _skip2;
1960 runtime->oss.trigger = 0;
1961 runtime->start_threshold = runtime->boundary;
1962 cmd = SNDRV_PCM_IOCTL_DROP;
1963 runtime->oss.prepare = 1;
1964 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001965 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (err < 0)
1967 return err;
1968 }
1969 _skip2:
1970 return 0;
1971}
1972
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001973static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001975 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int result = 0;
1977
1978 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1979 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1980 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
1981 result |= PCM_ENABLE_OUTPUT;
1982 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
1983 result |= PCM_ENABLE_INPUT;
1984 return result;
1985}
1986
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001987static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001989 struct snd_pcm_substream *substream;
1990 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 snd_pcm_sframes_t delay;
1992 int err;
1993
1994 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1995 if (substream == NULL)
1996 return -EINVAL;
1997 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1998 return err;
1999 runtime = substream->runtime;
2000 if (runtime->oss.params || runtime->oss.prepare)
2001 return 0;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002002 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (err == -EPIPE)
2004 delay = 0; /* hack for broken OSS applications */
2005 else if (err < 0)
2006 return err;
2007 return snd_pcm_oss_bytes(substream, delay);
2008}
2009
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002010static 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 -07002011{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002012 struct snd_pcm_substream *substream;
2013 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 snd_pcm_sframes_t delay;
2015 int fixup;
2016 struct count_info info;
2017 int err;
2018
2019 if (_info == NULL)
2020 return -EFAULT;
2021 substream = pcm_oss_file->streams[stream];
2022 if (substream == NULL)
2023 return -EINVAL;
2024 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2025 return err;
2026 runtime = substream->runtime;
2027 if (runtime->oss.params || runtime->oss.prepare) {
2028 memset(&info, 0, sizeof(info));
2029 if (copy_to_user(_info, &info, sizeof(info)))
2030 return -EFAULT;
2031 return 0;
2032 }
2033 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2034 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2035 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2036 err = 0;
2037 delay = 0;
2038 fixup = 0;
2039 } else {
2040 fixup = runtime->oss.buffer_used;
2041 }
2042 } else {
2043 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2044 fixup = -runtime->oss.buffer_used;
2045 }
2046 if (err < 0)
2047 return err;
2048 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002049 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 snd_pcm_sframes_t n;
2051 n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
2052 if (n < 0)
2053 n += runtime->boundary;
2054 info.blocks = n / runtime->period_size;
2055 runtime->oss.prev_hw_ptr_interrupt = delay;
2056 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2057 snd_pcm_oss_simulate_fill(substream, delay);
2058 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2059 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002060 delay = snd_pcm_oss_bytes(substream, delay);
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002061 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002062 if (substream->oss.setup.buggyptr)
Takashi Iwai10f69f92005-09-08 13:48:34 +02002063 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2064 else
2065 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002067 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002068 delay += fixup;
2069 info.blocks = delay / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 if (copy_to_user(_info, &info, sizeof(info)))
2074 return -EFAULT;
2075 return 0;
2076}
2077
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002078static 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 -07002079{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002080 struct snd_pcm_substream *substream;
2081 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 snd_pcm_sframes_t avail;
2083 int fixup;
2084 struct audio_buf_info info;
2085 int err;
2086
2087 if (_info == NULL)
2088 return -EFAULT;
2089 substream = pcm_oss_file->streams[stream];
2090 if (substream == NULL)
2091 return -EINVAL;
2092 runtime = substream->runtime;
2093
2094 if (runtime->oss.params &&
2095 (err = snd_pcm_oss_change_params(substream)) < 0)
2096 return err;
2097
2098 info.fragsize = runtime->oss.period_bytes;
2099 info.fragstotal = runtime->periods;
2100 if (runtime->oss.prepare) {
2101 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2102 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2103 info.fragments = runtime->oss.periods;
2104 } else {
2105 info.bytes = 0;
2106 info.fragments = 0;
2107 }
2108 } else {
2109 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2110 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2111 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2112 avail = runtime->buffer_size;
2113 err = 0;
2114 fixup = 0;
2115 } else {
2116 avail = runtime->buffer_size - avail;
2117 fixup = -runtime->oss.buffer_used;
2118 }
2119 } else {
2120 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2121 fixup = runtime->oss.buffer_used;
2122 }
2123 if (err < 0)
2124 return err;
2125 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2126 info.fragments = info.bytes / runtime->oss.period_bytes;
2127 }
2128
2129#ifdef OSS_DEBUG
2130 printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
2131#endif
2132 if (copy_to_user(_info, &info, sizeof(info)))
2133 return -EFAULT;
2134 return 0;
2135}
2136
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002137static 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 -07002138{
2139 // it won't be probably implemented
2140 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2141 return -EINVAL;
2142}
2143
Takashi Iwai060d77b2006-03-27 16:44:52 +02002144static const char *strip_task_path(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwai060d77b2006-03-27 16:44:52 +02002146 const char *ptr, *ptrl = NULL;
2147 for (ptr = path; *ptr; ptr++) {
2148 if (*ptr == '/')
2149 ptrl = ptr + 1;
2150 }
2151 return ptrl;
2152}
2153
2154static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2155 const char *task_name,
2156 struct snd_pcm_oss_setup *rsetup)
2157{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002158 struct snd_pcm_oss_setup *setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002160 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002161 do {
2162 for (setup = pcm->streams[stream].oss.setup_list; setup;
2163 setup = setup->next) {
2164 if (!strcmp(setup->task_name, task_name))
2165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002167 } while ((task_name = strip_task_path(task_name)) != NULL);
2168 out:
2169 if (setup)
2170 *rsetup = *setup;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002171 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002174static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2175{
2176 struct snd_pcm_runtime *runtime;
2177 runtime = substream->runtime;
2178 vfree(runtime->oss.buffer);
2179 runtime->oss.buffer = NULL;
2180#ifdef CONFIG_SND_PCM_OSS_PLUGINS
2181 snd_pcm_oss_plugin_clear(substream);
2182#endif
2183 substream->oss.oss = 0;
2184}
2185
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002186static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2187 struct snd_pcm_oss_setup *setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int minor)
2189{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002190 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 substream->oss.oss = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002193 substream->oss.setup = *setup;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002194 if (setup->nonblock)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002195 substream->f_flags |= O_NONBLOCK;
Takashi Iwai15762742006-04-06 19:47:42 +02002196 else if (setup->block)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002197 substream->f_flags &= ~O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 runtime = substream->runtime;
2199 runtime->oss.params = 1;
2200 runtime->oss.trigger = 1;
2201 runtime->oss.rate = 8000;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01002202 mutex_init(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2204 case SNDRV_MINOR_OSS_PCM_8:
2205 runtime->oss.format = AFMT_U8;
2206 break;
2207 case SNDRV_MINOR_OSS_PCM_16:
2208 runtime->oss.format = AFMT_S16_LE;
2209 break;
2210 default:
2211 runtime->oss.format = AFMT_MU_LAW;
2212 }
2213 runtime->oss.channels = 1;
2214 runtime->oss.fragshift = 0;
2215 runtime->oss.maxfrags = 0;
2216 runtime->oss.subdivision = 0;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002217 substream->pcm_release = snd_pcm_oss_release_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002220static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 int cidx;
2223 snd_assert(pcm_oss_file != NULL, return -ENXIO);
2224 for (cidx = 0; cidx < 2; ++cidx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002225 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002226 if (substream)
2227 snd_pcm_release_substream(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 kfree(pcm_oss_file);
2230 return 0;
2231}
2232
2233static int snd_pcm_oss_open_file(struct file *file,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002234 struct snd_pcm *pcm,
2235 struct snd_pcm_oss_file **rpcm_oss_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 int minor,
Takashi Iwai060d77b2006-03-27 16:44:52 +02002237 struct snd_pcm_oss_setup *setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002239 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002240 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002241 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 unsigned int f_mode = file->f_mode;
2243
2244 snd_assert(rpcm_oss_file != NULL, return -EINVAL);
2245 *rpcm_oss_file = NULL;
2246
Takashi Iwaica2c0962005-09-09 14:20:23 +02002247 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (pcm_oss_file == NULL)
2249 return -ENOMEM;
2250
2251 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2252 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2253 f_mode = FMODE_WRITE;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002254
Takashi Iwai0df63e42006-04-28 15:13:41 +02002255 file->f_flags &= ~O_APPEND;
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002256 for (idx = 0; idx < 2; idx++) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002257 if (setup[idx].disable)
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002258 continue;
Takashi Iwai6cb53e72006-08-02 21:12:09 +02002259 if (! pcm->streams[idx].substream_count)
2260 continue; /* no matching substream */
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002261 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2262 if (! (f_mode & FMODE_WRITE))
2263 continue;
2264 } else {
2265 if (! (f_mode & FMODE_READ))
2266 continue;
2267 }
2268 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2269 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 snd_pcm_oss_release_file(pcm_oss_file);
2271 return err;
2272 }
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002273
2274 pcm_oss_file->streams[idx] = substream;
Takashi Iwai15762742006-04-06 19:47:42 +02002275 substream->file = pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002276 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
OGAWA Hirofumibbdc1b72006-04-06 19:42:40 +02002279 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 snd_pcm_oss_release_file(pcm_oss_file);
2281 return -EINVAL;
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 file->private_data = pcm_oss_file;
2285 *rpcm_oss_file = pcm_oss_file;
2286 return 0;
2287}
2288
2289
Takashi Iwai93f2e372005-10-10 11:51:55 +02002290static int snd_task_name(struct task_struct *task, char *name, size_t size)
2291{
2292 unsigned int idx;
2293
2294 snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
2295 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2296 name[idx] = task->comm[idx];
2297 name[idx] = '\0';
2298 return 0;
2299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 int err;
2304 char task_name[32];
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002305 struct snd_pcm *pcm;
2306 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002307 struct snd_pcm_oss_setup setup[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int nonblock;
2309 wait_queue_t wait;
2310
Clemens Ladischf87135f2005-11-20 14:06:59 +01002311 pcm = snd_lookup_oss_minor_data(iminor(inode),
2312 SNDRV_OSS_DEVICE_TYPE_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (pcm == NULL) {
2314 err = -ENODEV;
2315 goto __error1;
2316 }
2317 err = snd_card_file_add(pcm->card, file);
2318 if (err < 0)
2319 goto __error1;
2320 if (!try_module_get(pcm->card->module)) {
2321 err = -EFAULT;
2322 goto __error2;
2323 }
2324 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2325 err = -EFAULT;
2326 goto __error;
2327 }
Takashi Iwai15762742006-04-06 19:47:42 +02002328 memset(setup, 0, sizeof(setup));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (file->f_mode & FMODE_WRITE)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002330 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2331 task_name, &setup[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (file->f_mode & FMODE_READ)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002333 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2334 task_name, &setup[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 nonblock = !!(file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (!nonblock)
2338 nonblock = nonblock_open;
2339
2340 init_waitqueue_entry(&wait, current);
2341 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002342 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 while (1) {
2344 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
Takashi Iwai3bf75f9b2006-03-27 16:40:49 +02002345 iminor(inode), setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (err >= 0)
2347 break;
2348 if (err == -EAGAIN) {
2349 if (nonblock) {
2350 err = -EBUSY;
2351 break;
2352 }
2353 } else
2354 break;
2355 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002356 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002358 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (signal_pending(current)) {
2360 err = -ERESTARTSYS;
2361 break;
2362 }
2363 }
2364 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002365 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (err < 0)
2367 goto __error;
2368 return err;
2369
2370 __error:
2371 module_put(pcm->card->module);
2372 __error2:
2373 snd_card_file_remove(pcm->card, file);
2374 __error1:
2375 return err;
2376}
2377
2378static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2379{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002380 struct snd_pcm *pcm;
2381 struct snd_pcm_substream *substream;
2382 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 pcm_oss_file = file->private_data;
2385 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2386 if (substream == NULL)
2387 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2388 snd_assert(substream != NULL, return -ENXIO);
2389 pcm = substream->pcm;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01002390 if (!pcm->card->shutdown)
2391 snd_pcm_oss_sync(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002392 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 snd_pcm_oss_release_file(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002394 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 wake_up(&pcm->open_wait);
2396 module_put(pcm->card->module);
2397 snd_card_file_remove(pcm->card, file);
2398 return 0;
2399}
2400
2401static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2402{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002403 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 int __user *p = (int __user *)arg;
2405 int res;
2406
2407 pcm_oss_file = file->private_data;
2408 if (cmd == OSS_GETVERSION)
2409 return put_user(SNDRV_OSS_VERSION, p);
2410 if (cmd == OSS_ALSAEMULVER)
2411 return put_user(1, p);
2412#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2413 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002414 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int idx;
2416 for (idx = 0; idx < 2; ++idx) {
2417 substream = pcm_oss_file->streams[idx];
2418 if (substream != NULL)
2419 break;
2420 }
2421 snd_assert(substream != NULL, return -ENXIO);
2422 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2423 }
2424#endif
2425 if (((cmd >> 8) & 0xff) != 'P')
2426 return -EINVAL;
2427#ifdef OSS_DEBUG
2428 printk("pcm_oss: ioctl = 0x%x\n", cmd);
2429#endif
2430 switch (cmd) {
2431 case SNDCTL_DSP_RESET:
2432 return snd_pcm_oss_reset(pcm_oss_file);
2433 case SNDCTL_DSP_SYNC:
2434 return snd_pcm_oss_sync(pcm_oss_file);
2435 case SNDCTL_DSP_SPEED:
2436 if (get_user(res, p))
2437 return -EFAULT;
2438 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2439 return res;
2440 return put_user(res, p);
2441 case SOUND_PCM_READ_RATE:
2442 res = snd_pcm_oss_get_rate(pcm_oss_file);
2443 if (res < 0)
2444 return res;
2445 return put_user(res, p);
2446 case SNDCTL_DSP_STEREO:
2447 if (get_user(res, p))
2448 return -EFAULT;
2449 res = res > 0 ? 2 : 1;
2450 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2451 return res;
2452 return put_user(--res, p);
2453 case SNDCTL_DSP_GETBLKSIZE:
2454 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2455 if (res < 0)
2456 return res;
2457 return put_user(res, p);
2458 case SNDCTL_DSP_SETFMT:
2459 if (get_user(res, p))
2460 return -EFAULT;
2461 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2462 if (res < 0)
2463 return res;
2464 return put_user(res, p);
2465 case SOUND_PCM_READ_BITS:
2466 res = snd_pcm_oss_get_format(pcm_oss_file);
2467 if (res < 0)
2468 return res;
2469 return put_user(res, p);
2470 case SNDCTL_DSP_CHANNELS:
2471 if (get_user(res, p))
2472 return -EFAULT;
2473 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2474 if (res < 0)
2475 return res;
2476 return put_user(res, p);
2477 case SOUND_PCM_READ_CHANNELS:
2478 res = snd_pcm_oss_get_channels(pcm_oss_file);
2479 if (res < 0)
2480 return res;
2481 return put_user(res, p);
2482 case SOUND_PCM_WRITE_FILTER:
2483 case SOUND_PCM_READ_FILTER:
2484 return -EIO;
2485 case SNDCTL_DSP_POST:
2486 return snd_pcm_oss_post(pcm_oss_file);
2487 case SNDCTL_DSP_SUBDIVIDE:
2488 if (get_user(res, p))
2489 return -EFAULT;
2490 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2491 if (res < 0)
2492 return res;
2493 return put_user(res, p);
2494 case SNDCTL_DSP_SETFRAGMENT:
2495 if (get_user(res, p))
2496 return -EFAULT;
2497 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2498 case SNDCTL_DSP_GETFMTS:
2499 res = snd_pcm_oss_get_formats(pcm_oss_file);
2500 if (res < 0)
2501 return res;
2502 return put_user(res, p);
2503 case SNDCTL_DSP_GETOSPACE:
2504 case SNDCTL_DSP_GETISPACE:
2505 return snd_pcm_oss_get_space(pcm_oss_file,
2506 cmd == SNDCTL_DSP_GETISPACE ?
2507 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2508 (struct audio_buf_info __user *) arg);
2509 case SNDCTL_DSP_NONBLOCK:
2510 return snd_pcm_oss_nonblock(file);
2511 case SNDCTL_DSP_GETCAPS:
2512 res = snd_pcm_oss_get_caps(pcm_oss_file);
2513 if (res < 0)
2514 return res;
2515 return put_user(res, p);
2516 case SNDCTL_DSP_GETTRIGGER:
2517 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2518 if (res < 0)
2519 return res;
2520 return put_user(res, p);
2521 case SNDCTL_DSP_SETTRIGGER:
2522 if (get_user(res, p))
2523 return -EFAULT;
2524 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2525 case SNDCTL_DSP_GETIPTR:
2526 case SNDCTL_DSP_GETOPTR:
2527 return snd_pcm_oss_get_ptr(pcm_oss_file,
2528 cmd == SNDCTL_DSP_GETIPTR ?
2529 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2530 (struct count_info __user *) arg);
2531 case SNDCTL_DSP_MAPINBUF:
2532 case SNDCTL_DSP_MAPOUTBUF:
2533 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2534 cmd == SNDCTL_DSP_MAPINBUF ?
2535 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2536 (struct buffmem_desc __user *) arg);
2537 case SNDCTL_DSP_SETSYNCRO:
2538 /* stop DMA now.. */
2539 return 0;
2540 case SNDCTL_DSP_SETDUPLEX:
2541 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2542 return 0;
2543 return -EIO;
2544 case SNDCTL_DSP_GETODELAY:
2545 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2546 if (res < 0) {
2547 /* it's for sure, some broken apps don't check for error codes */
2548 put_user(0, p);
2549 return res;
2550 }
2551 return put_user(res, p);
2552 case SNDCTL_DSP_PROFILE:
2553 return 0; /* silently ignore */
2554 default:
2555 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2556 }
2557 return -EINVAL;
2558}
2559
2560#ifdef CONFIG_COMPAT
2561/* all compatible */
2562#define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2563#else
2564#define snd_pcm_oss_ioctl_compat NULL
2565#endif
2566
2567static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2568{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002569 struct snd_pcm_oss_file *pcm_oss_file;
2570 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 pcm_oss_file = file->private_data;
2573 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2574 if (substream == NULL)
2575 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002576 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577#ifndef OSS_DEBUG
2578 return snd_pcm_oss_read1(substream, buf, count);
2579#else
2580 {
2581 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2582 printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
2583 return res;
2584 }
2585#endif
2586}
2587
2588static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2589{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002590 struct snd_pcm_oss_file *pcm_oss_file;
2591 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 long result;
2593
2594 pcm_oss_file = file->private_data;
2595 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2596 if (substream == NULL)
2597 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002598 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 result = snd_pcm_oss_write1(substream, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#ifdef OSS_DEBUG
2601 printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
2602#endif
2603 return result;
2604}
2605
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002606static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002608 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002609 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2611 else
2612 return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
2613}
2614
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002615static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002617 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002618 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
2620 else
2621 return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
2622}
2623
2624static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2625{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002626 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 unsigned int mask;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002628 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 pcm_oss_file = file->private_data;
2631
2632 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2633 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2634
2635 mask = 0;
2636 if (psubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002637 struct snd_pcm_runtime *runtime = psubstream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 poll_wait(file, &runtime->sleep, wait);
2639 snd_pcm_stream_lock_irq(psubstream);
2640 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2641 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2642 snd_pcm_oss_playback_ready(psubstream)))
2643 mask |= POLLOUT | POLLWRNORM;
2644 snd_pcm_stream_unlock_irq(psubstream);
2645 }
2646 if (csubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002647 struct snd_pcm_runtime *runtime = csubstream->runtime;
2648 snd_pcm_state_t ostate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 poll_wait(file, &runtime->sleep, wait);
2650 snd_pcm_stream_lock_irq(csubstream);
2651 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2652 snd_pcm_oss_capture_ready(csubstream))
2653 mask |= POLLIN | POLLRDNORM;
2654 snd_pcm_stream_unlock_irq(csubstream);
2655 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002656 struct snd_pcm_oss_file ofile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 memset(&ofile, 0, sizeof(ofile));
2658 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2659 runtime->oss.trigger = 0;
2660 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2661 }
2662 }
2663
2664 return mask;
2665}
2666
2667static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2668{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002669 struct snd_pcm_oss_file *pcm_oss_file;
2670 struct snd_pcm_substream *substream = NULL;
2671 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int err;
2673
2674#ifdef OSS_DEBUG
2675 printk("pcm_oss: mmap begin\n");
2676#endif
2677 pcm_oss_file = file->private_data;
2678 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2679 case VM_READ | VM_WRITE:
2680 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2681 if (substream)
2682 break;
2683 /* Fall through */
2684 case VM_READ:
2685 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2686 break;
2687 case VM_WRITE:
2688 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2689 break;
2690 default:
2691 return -EINVAL;
2692 }
2693 /* set VM_READ access as well to fix memset() routines that do
2694 reads before writes (to improve performance) */
2695 area->vm_flags |= VM_READ;
2696 if (substream == NULL)
2697 return -ENXIO;
2698 runtime = substream->runtime;
2699 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2700 return -EIO;
2701 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2702 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2703 else
2704 return -EIO;
2705
2706 if (runtime->oss.params) {
2707 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2708 return err;
2709 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002710#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (runtime->oss.plugin_first != NULL)
2712 return -EIO;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 if (area->vm_pgoff != 0)
2716 return -EINVAL;
2717
2718 err = snd_pcm_mmap_data(substream, file, area);
2719 if (err < 0)
2720 return err;
2721 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2722 runtime->silence_threshold = 0;
2723 runtime->silence_size = 0;
2724#ifdef OSS_DEBUG
2725 printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
2726#endif
2727 /* In mmap mode we never stop */
2728 runtime->stop_threshold = runtime->boundary;
2729
2730 return 0;
2731}
2732
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002733#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734/*
2735 * /proc interface
2736 */
2737
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002738static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2739 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002741 struct snd_pcm_str *pstr = entry->private_data;
2742 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002743 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 while (setup) {
2745 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2746 setup->task_name,
2747 setup->periods,
2748 setup->period_size,
2749 setup->disable ? " disable" : "",
2750 setup->direct ? " direct" : "",
2751 setup->block ? " block" : "",
2752 setup->nonblock ? " non-block" : "",
2753 setup->partialfrag ? " partial-frag" : "",
2754 setup->nosilence ? " no-silence" : "");
2755 setup = setup->next;
2756 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002757 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002760static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002762 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2765 setup; setup = setupn) {
2766 setupn = setup->next;
2767 kfree(setup->task_name);
2768 kfree(setup);
2769 }
2770 pstr->oss.setup_list = NULL;
2771}
2772
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002773static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2774 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002776 struct snd_pcm_str *pstr = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 char line[128], str[32], task_name[32], *ptr;
2778 int idx1;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002779 struct snd_pcm_oss_setup *setup, *setup1, template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 while (!snd_info_get_line(buffer, line, sizeof(line))) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002782 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 memset(&template, 0, sizeof(template));
2784 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2785 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2786 snd_pcm_oss_proc_free_setup_list(pstr);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002787 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 continue;
2789 }
2790 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2791 if (!strcmp(setup->task_name, task_name)) {
2792 template = *setup;
2793 break;
2794 }
2795 }
2796 ptr = snd_info_get_str(str, ptr, sizeof(str));
2797 template.periods = simple_strtoul(str, NULL, 10);
2798 ptr = snd_info_get_str(str, ptr, sizeof(str));
2799 template.period_size = simple_strtoul(str, NULL, 10);
2800 for (idx1 = 31; idx1 >= 0; idx1--)
2801 if (template.period_size & (1 << idx1))
2802 break;
2803 for (idx1--; idx1 >= 0; idx1--)
2804 template.period_size &= ~(1 << idx1);
2805 do {
2806 ptr = snd_info_get_str(str, ptr, sizeof(str));
2807 if (!strcmp(str, "disable")) {
2808 template.disable = 1;
2809 } else if (!strcmp(str, "direct")) {
2810 template.direct = 1;
2811 } else if (!strcmp(str, "block")) {
2812 template.block = 1;
2813 } else if (!strcmp(str, "non-block")) {
2814 template.nonblock = 1;
2815 } else if (!strcmp(str, "partial-frag")) {
2816 template.partialfrag = 1;
2817 } else if (!strcmp(str, "no-silence")) {
2818 template.nosilence = 1;
Takashi Iwai10f69f92005-09-08 13:48:34 +02002819 } else if (!strcmp(str, "buggy-ptr")) {
2820 template.buggyptr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822 } while (*str);
2823 if (setup == NULL) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002824 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2825 if (! setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 buffer->error = -ENOMEM;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002827 mutex_lock(&pstr->oss.setup_mutex);
2828 return;
2829 }
2830 if (pstr->oss.setup_list == NULL)
2831 pstr->oss.setup_list = setup;
2832 else {
2833 for (setup1 = pstr->oss.setup_list;
2834 setup1->next; setup1 = setup1->next);
2835 setup1->next = setup;
2836 }
2837 template.task_name = kstrdup(task_name, GFP_KERNEL);
2838 if (! template.task_name) {
2839 kfree(setup);
2840 buffer->error = -ENOMEM;
2841 mutex_lock(&pstr->oss.setup_mutex);
2842 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
2844 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002845 *setup = template;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002846 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848}
2849
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002850static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
2852 int stream;
2853 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002854 struct snd_info_entry *entry;
2855 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (pstr->substream_count == 0)
2857 continue;
2858 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2859 entry->content = SNDRV_INFO_CONTENT_TEXT;
2860 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 entry->c.text.read = snd_pcm_oss_proc_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 entry->c.text.write = snd_pcm_oss_proc_write;
2863 entry->private_data = pstr;
2864 if (snd_info_register(entry) < 0) {
2865 snd_info_free_entry(entry);
2866 entry = NULL;
2867 }
2868 }
2869 pstr->oss.proc_entry = entry;
2870 }
2871}
2872
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002873static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 int stream;
2876 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002877 struct snd_pcm_str *pstr = &pcm->streams[stream];
Takashi Iwai746d4a02006-06-23 14:37:59 +02002878 snd_info_free_entry(pstr->oss.proc_entry);
2879 pstr->oss.proc_entry = NULL;
2880 snd_pcm_oss_proc_free_setup_list(pstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
2882}
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002883#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwai04f141a2005-12-01 10:43:51 +01002884#define snd_pcm_oss_proc_init(pcm)
2885#define snd_pcm_oss_proc_done(pcm)
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002886#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888/*
2889 * ENTRY functions
2890 */
2891
2892static struct file_operations snd_pcm_oss_f_reg =
2893{
2894 .owner = THIS_MODULE,
2895 .read = snd_pcm_oss_read,
2896 .write = snd_pcm_oss_write,
2897 .open = snd_pcm_oss_open,
2898 .release = snd_pcm_oss_release,
2899 .poll = snd_pcm_oss_poll,
2900 .unlocked_ioctl = snd_pcm_oss_ioctl,
2901 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2902 .mmap = snd_pcm_oss_mmap,
2903};
2904
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002905static void register_oss_dsp(struct snd_pcm *pcm, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 char name[128];
2908 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2909 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002910 pcm->card, index, &snd_pcm_oss_f_reg,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002911 pcm, name) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002912 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2913 pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915}
2916
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002917static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 pcm->oss.reg = 0;
2920 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2921 char name[128];
2922 int duplex;
2923 register_oss_dsp(pcm, 0);
2924 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
2925 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
2926 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
2927 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
2928#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2929 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
2930 pcm->card->number,
2931 name);
2932#endif
2933 pcm->oss.reg++;
2934 pcm->oss.reg_mask |= 1;
2935 }
2936 if (adsp_map[pcm->card->number] == (int)pcm->device) {
2937 register_oss_dsp(pcm, 1);
2938 pcm->oss.reg++;
2939 pcm->oss.reg_mask |= 2;
2940 }
2941
2942 if (pcm->oss.reg)
2943 snd_pcm_oss_proc_init(pcm);
2944
2945 return 0;
2946}
2947
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002948static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 if (pcm->oss.reg) {
2951 if (pcm->oss.reg_mask & 1) {
2952 pcm->oss.reg_mask &= ~1;
2953 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2954 pcm->card, 0);
2955 }
2956 if (pcm->oss.reg_mask & 2) {
2957 pcm->oss.reg_mask &= ~2;
2958 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
2959 pcm->card, 1);
2960 }
Takashi Iwaic4614822006-06-23 14:38:23 +02002961 if (dsp_map[pcm->card->number] == (int)pcm->device) {
2962#ifdef SNDRV_OSS_INFO_DEV_AUDIO
2963 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
2964#endif
2965 }
2966 pcm->oss.reg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968 return 0;
2969}
2970
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002971static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 snd_pcm_oss_disconnect_minor(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +02002974 snd_pcm_oss_proc_done(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return 0;
2976}
2977
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002978static struct snd_pcm_notify snd_pcm_oss_notify =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 .n_register = snd_pcm_oss_register_minor,
2981 .n_disconnect = snd_pcm_oss_disconnect_minor,
2982 .n_unregister = snd_pcm_oss_unregister_minor,
2983};
2984
2985static int __init alsa_pcm_oss_init(void)
2986{
2987 int i;
2988 int err;
2989
2990 /* check device map table */
2991 for (i = 0; i < SNDRV_CARDS; i++) {
2992 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002993 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
2994 i, dsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 dsp_map[i] = 0;
2996 }
2997 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002998 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
2999 i, adsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 adsp_map[i] = 1;
3001 }
3002 }
3003 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3004 return err;
3005 return 0;
3006}
3007
3008static void __exit alsa_pcm_oss_exit(void)
3009{
3010 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3011}
3012
3013module_init(alsa_pcm_oss_init)
3014module_exit(alsa_pcm_oss_exit)