blob: 255ad910077ae906cb4427a120f24ca92d0e9a32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer / OSS compatible
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#if 0
23#define PLUGIN_DEBUG
24#endif
25#if 0
26#define OSS_DEBUG
27#endif
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/time.h>
32#include <linux/vmalloc.h>
33#include <linux/moduleparam.h>
Takashi Iwai3f7440a2009-06-05 17:40:04 +020034#include <linux/math64.h>
Paulo Marques543537b2005-06-23 00:09:02 -070035#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/core.h>
37#include <sound/minors.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include "pcm_plugin.h"
41#include <sound/info.h>
42#include <linux/soundcard.h>
43#include <sound/initval.h>
44
45#define OSS_ALSAEMULVER _SIOR ('M', 249, int)
46
Takashi Iwai6581f4e2006-05-17 17:14:51 +020047static int dsp_map[SNDRV_CARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
49static int nonblock_open = 1;
50
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020051MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
53MODULE_LICENSE("GPL");
54module_param_array(dsp_map, int, NULL, 0444);
55MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
56module_param_array(adsp_map, int, NULL, 0444);
57MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
58module_param(nonblock_open, bool, 0644);
59MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
60MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
61MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
62
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010063extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
64static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
65static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
66static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static inline mm_segment_t snd_enter_user(void)
69{
70 mm_segment_t fs = get_fs();
71 set_fs(get_ds());
72 return fs;
73}
74
75static inline void snd_leave_user(mm_segment_t fs)
76{
77 set_fs(fs);
78}
79
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020080/*
81 * helper functions to process hw_params
82 */
83static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
84{
85 int changed = 0;
86 if (i->min < min) {
87 i->min = min;
88 i->openmin = openmin;
89 changed = 1;
90 } else if (i->min == min && !i->openmin && openmin) {
91 i->openmin = 1;
92 changed = 1;
93 }
94 if (i->integer) {
95 if (i->openmin) {
96 i->min++;
97 i->openmin = 0;
98 }
99 }
100 if (snd_interval_checkempty(i)) {
101 snd_interval_none(i);
102 return -EINVAL;
103 }
104 return changed;
105}
106
107static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
108{
109 int changed = 0;
110 if (i->max > max) {
111 i->max = max;
112 i->openmax = openmax;
113 changed = 1;
114 } else if (i->max == max && !i->openmax && openmax) {
115 i->openmax = 1;
116 changed = 1;
117 }
118 if (i->integer) {
119 if (i->openmax) {
120 i->max--;
121 i->openmax = 0;
122 }
123 }
124 if (snd_interval_checkempty(i)) {
125 snd_interval_none(i);
126 return -EINVAL;
127 }
128 return changed;
129}
130
131static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
132{
133 struct snd_interval t;
134 t.empty = 0;
135 t.min = t.max = val;
136 t.openmin = t.openmax = 0;
137 t.integer = 1;
138 return snd_interval_refine(i, &t);
139}
140
141/**
142 * snd_pcm_hw_param_value_min
143 * @params: the hw_params instance
144 * @var: parameter to retrieve
145 * @dir: pointer to the direction (-1,0,1) or NULL
146 *
147 * Return the minimum value for field PAR.
148 */
149static unsigned int
150snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
151 snd_pcm_hw_param_t var, int *dir)
152{
153 if (hw_is_mask(var)) {
154 if (dir)
155 *dir = 0;
156 return snd_mask_min(hw_param_mask_c(params, var));
157 }
158 if (hw_is_interval(var)) {
159 const struct snd_interval *i = hw_param_interval_c(params, var);
160 if (dir)
161 *dir = i->openmin;
162 return snd_interval_min(i);
163 }
164 return -EINVAL;
165}
166
167/**
168 * snd_pcm_hw_param_value_max
169 * @params: the hw_params instance
170 * @var: parameter to retrieve
171 * @dir: pointer to the direction (-1,0,1) or NULL
172 *
173 * Return the maximum value for field PAR.
174 */
175static unsigned int
176snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
177 snd_pcm_hw_param_t var, int *dir)
178{
179 if (hw_is_mask(var)) {
180 if (dir)
181 *dir = 0;
182 return snd_mask_max(hw_param_mask_c(params, var));
183 }
184 if (hw_is_interval(var)) {
185 const struct snd_interval *i = hw_param_interval_c(params, var);
186 if (dir)
187 *dir = - (int) i->openmax;
188 return snd_interval_max(i);
189 }
190 return -EINVAL;
191}
192
193static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
194 snd_pcm_hw_param_t var,
195 const struct snd_mask *val)
196{
197 int changed;
198 changed = snd_mask_refine(hw_param_mask(params, var), val);
199 if (changed) {
200 params->cmask |= 1 << var;
201 params->rmask |= 1 << var;
202 }
203 return changed;
204}
205
206static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
207 struct snd_pcm_hw_params *params,
208 snd_pcm_hw_param_t var,
209 const struct snd_mask *val)
210{
211 int changed = _snd_pcm_hw_param_mask(params, var, val);
212 if (changed < 0)
213 return changed;
214 if (params->rmask) {
215 int err = snd_pcm_hw_refine(pcm, params);
216 if (err < 0)
217 return err;
218 }
219 return 0;
220}
221
222static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
223 snd_pcm_hw_param_t var, unsigned int val,
224 int dir)
225{
226 int changed;
227 int open = 0;
228 if (dir) {
229 if (dir > 0) {
230 open = 1;
231 } else if (dir < 0) {
232 if (val > 0) {
233 open = 1;
234 val--;
235 }
236 }
237 }
238 if (hw_is_mask(var))
239 changed = snd_mask_refine_min(hw_param_mask(params, var),
240 val + !!open);
241 else if (hw_is_interval(var))
242 changed = snd_interval_refine_min(hw_param_interval(params, var),
243 val, open);
244 else
245 return -EINVAL;
246 if (changed) {
247 params->cmask |= 1 << var;
248 params->rmask |= 1 << var;
249 }
250 return changed;
251}
252
253/**
254 * snd_pcm_hw_param_min
255 * @pcm: PCM instance
256 * @params: the hw_params instance
257 * @var: parameter to retrieve
258 * @val: minimal value
259 * @dir: pointer to the direction (-1,0,1) or NULL
260 *
261 * Inside configuration space defined by PARAMS remove from PAR all
262 * values < VAL. Reduce configuration space accordingly.
263 * Return new minimum or -EINVAL if the configuration space is empty
264 */
265static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
266 struct snd_pcm_hw_params *params,
267 snd_pcm_hw_param_t var, unsigned int val,
268 int *dir)
269{
270 int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
271 if (changed < 0)
272 return changed;
273 if (params->rmask) {
274 int err = snd_pcm_hw_refine(pcm, params);
275 if (err < 0)
276 return err;
277 }
278 return snd_pcm_hw_param_value_min(params, var, dir);
279}
280
281static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
282 snd_pcm_hw_param_t var, unsigned int val,
283 int dir)
284{
285 int changed;
286 int open = 0;
287 if (dir) {
288 if (dir < 0) {
289 open = 1;
290 } else if (dir > 0) {
291 open = 1;
292 val++;
293 }
294 }
295 if (hw_is_mask(var)) {
296 if (val == 0 && open) {
297 snd_mask_none(hw_param_mask(params, var));
298 changed = -EINVAL;
299 } else
300 changed = snd_mask_refine_max(hw_param_mask(params, var),
301 val - !!open);
302 } else if (hw_is_interval(var))
303 changed = snd_interval_refine_max(hw_param_interval(params, var),
304 val, open);
305 else
306 return -EINVAL;
307 if (changed) {
308 params->cmask |= 1 << var;
309 params->rmask |= 1 << var;
310 }
311 return changed;
312}
313
314/**
315 * snd_pcm_hw_param_max
316 * @pcm: PCM instance
317 * @params: the hw_params instance
318 * @var: parameter to retrieve
319 * @val: maximal value
320 * @dir: pointer to the direction (-1,0,1) or NULL
321 *
322 * Inside configuration space defined by PARAMS remove from PAR all
323 * values >= VAL + 1. Reduce configuration space accordingly.
324 * Return new maximum or -EINVAL if the configuration space is empty
325 */
326static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
327 struct snd_pcm_hw_params *params,
328 snd_pcm_hw_param_t var, unsigned int val,
329 int *dir)
330{
331 int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
332 if (changed < 0)
333 return changed;
334 if (params->rmask) {
335 int err = snd_pcm_hw_refine(pcm, params);
336 if (err < 0)
337 return err;
338 }
339 return snd_pcm_hw_param_value_max(params, var, dir);
340}
341
342static int boundary_sub(int a, int adir,
343 int b, int bdir,
344 int *c, int *cdir)
345{
346 adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
347 bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
348 *c = a - b;
349 *cdir = adir - bdir;
350 if (*cdir == -2) {
351 (*c)--;
352 } else if (*cdir == 2) {
353 (*c)++;
354 }
355 return 0;
356}
357
358static int boundary_lt(unsigned int a, int adir,
359 unsigned int b, int bdir)
360{
361 if (adir < 0) {
362 a--;
363 adir = 1;
364 } else if (adir > 0)
365 adir = 1;
366 if (bdir < 0) {
367 b--;
368 bdir = 1;
369 } else if (bdir > 0)
370 bdir = 1;
371 return a < b || (a == b && adir < bdir);
372}
373
374/* Return 1 if min is nearer to best than max */
375static int boundary_nearer(int min, int mindir,
376 int best, int bestdir,
377 int max, int maxdir)
378{
379 int dmin, dmindir;
380 int dmax, dmaxdir;
381 boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
382 boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
383 return boundary_lt(dmin, dmindir, dmax, dmaxdir);
384}
385
386/**
387 * snd_pcm_hw_param_near
388 * @pcm: PCM instance
389 * @params: the hw_params instance
390 * @var: parameter to retrieve
391 * @best: value to set
392 * @dir: pointer to the direction (-1,0,1) or NULL
393 *
394 * Inside configuration space defined by PARAMS set PAR to the available value
395 * nearest to VAL. Reduce configuration space accordingly.
396 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
397 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
398 * Return the value found.
399 */
400static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
401 struct snd_pcm_hw_params *params,
402 snd_pcm_hw_param_t var, unsigned int best,
403 int *dir)
404{
405 struct snd_pcm_hw_params *save = NULL;
406 int v;
407 unsigned int saved_min;
408 int last = 0;
409 int min, max;
410 int mindir, maxdir;
411 int valdir = dir ? *dir : 0;
412 /* FIXME */
413 if (best > INT_MAX)
414 best = INT_MAX;
415 min = max = best;
416 mindir = maxdir = valdir;
417 if (maxdir > 0)
418 maxdir = 0;
419 else if (maxdir == 0)
420 maxdir = -1;
421 else {
422 maxdir = 1;
423 max--;
424 }
425 save = kmalloc(sizeof(*save), GFP_KERNEL);
426 if (save == NULL)
427 return -ENOMEM;
428 *save = *params;
429 saved_min = min;
430 min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
431 if (min >= 0) {
432 struct snd_pcm_hw_params *params1;
433 if (max < 0)
434 goto _end;
435 if ((unsigned int)min == saved_min && mindir == valdir)
436 goto _end;
437 params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
438 if (params1 == NULL) {
439 kfree(save);
440 return -ENOMEM;
441 }
442 *params1 = *save;
443 max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
444 if (max < 0) {
445 kfree(params1);
446 goto _end;
447 }
448 if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
449 *params = *params1;
450 last = 1;
451 }
452 kfree(params1);
453 } else {
454 *params = *save;
455 max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200456 if (max < 0)
457 return max;
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200458 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);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200466 snd_BUG_ON(v < 0);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200467 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 Iwaicdc5c532005-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;
Takashi Iwai3f7440a2009-06-05 17:40:04 +0200621 return div_u64(bsize, buffer_size);
Takashi Iwaicdc5c532005-05-27 12:40:52 +0200622 }
623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100626static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100628 struct snd_pcm_runtime *runtime = substream->runtime;
Jaroslav Kyselaaf081612005-05-27 11:15:20 +0200629 long buffer_size = snd_pcm_lib_buffer_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (buffer_size == runtime->oss.buffer_bytes)
631 return bytes_to_frames(runtime, bytes);
632 return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
633}
634
Jaroslav Kyselaf2404062010-01-05 17:19:34 +0100635static inline
636snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
637{
638 snd_pcm_uframes_t ptr = runtime->status->hw_ptr;
639 return ptr - (ptr % runtime->period_size);
640}
641
Takashi Iwai24038a22007-08-08 17:00:32 +0200642/* define extended formats in the recent OSS versions (if any) */
643/* linear formats */
644#define AFMT_S32_LE 0x00001000
645#define AFMT_S32_BE 0x00002000
646#define AFMT_S24_LE 0x00008000
647#define AFMT_S24_BE 0x00010000
648#define AFMT_S24_PACKED 0x00040000
649
650/* other supported formats */
651#define AFMT_FLOAT 0x00004000
652#define AFMT_SPDIF_RAW 0x00020000
653
654/* unsupported formats */
655#define AFMT_AC3 0x00000400
656#define AFMT_VORBIS 0x00000800
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int snd_pcm_oss_format_from(int format)
659{
660 switch (format) {
661 case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
662 case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
663 case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
664 case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
665 case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
666 case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
667 case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
668 case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
669 case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
670 case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
Takashi Iwai24038a22007-08-08 17:00:32 +0200671 case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
672 case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
673 case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
674 case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
675 case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
676 case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
677 case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 default: return SNDRV_PCM_FORMAT_U8;
679 }
680}
681
682static int snd_pcm_oss_format_to(int format)
683{
684 switch (format) {
685 case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
686 case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
687 case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
688 case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
689 case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
690 case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
691 case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
692 case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
693 case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
694 case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
Takashi Iwai24038a22007-08-08 17:00:32 +0200695 case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
696 case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
697 case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
698 case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
699 case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
700 case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
701 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 default: return -EINVAL;
703 }
704}
705
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100706static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
707 struct snd_pcm_hw_params *oss_params,
708 struct snd_pcm_hw_params *slave_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 size_t s;
711 size_t oss_buffer_size, oss_period_size, oss_periods;
712 size_t min_period_size, max_period_size;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100713 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 size_t oss_frame_size;
715
716 oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
717 params_channels(oss_params) / 8;
718
719 oss_buffer_size = snd_pcm_plug_client_size(substream,
720 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
721 oss_buffer_size = 1 << ld2(oss_buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200722 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (oss_buffer_size > runtime->oss.mmap_bytes)
724 oss_buffer_size = runtime->oss.mmap_bytes;
725 }
726
Takashi Iwai060d77b2006-03-27 16:44:52 +0200727 if (substream->oss.setup.period_size > 16)
728 oss_period_size = substream->oss.setup.period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 else if (runtime->oss.fragshift) {
730 oss_period_size = 1 << runtime->oss.fragshift;
731 if (oss_period_size > oss_buffer_size / 2)
732 oss_period_size = oss_buffer_size / 2;
733 } else {
734 int sd;
735 size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
736
737 oss_period_size = oss_buffer_size;
738 do {
739 oss_period_size /= 2;
740 } while (oss_period_size > bytes_per_sec);
741 if (runtime->oss.subdivision == 0) {
742 sd = 4;
743 if (oss_period_size / sd > 4096)
744 sd *= 2;
745 if (oss_period_size / sd < 4096)
746 sd = 1;
747 } else
748 sd = runtime->oss.subdivision;
749 oss_period_size /= sd;
750 if (oss_period_size < 16)
751 oss_period_size = 16;
752 }
753
754 min_period_size = snd_pcm_plug_client_size(substream,
755 snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
756 min_period_size *= oss_frame_size;
757 min_period_size = 1 << (ld2(min_period_size - 1) + 1);
758 if (oss_period_size < min_period_size)
759 oss_period_size = min_period_size;
760
761 max_period_size = snd_pcm_plug_client_size(substream,
762 snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
763 max_period_size *= oss_frame_size;
764 max_period_size = 1 << ld2(max_period_size);
765 if (oss_period_size > max_period_size)
766 oss_period_size = max_period_size;
767
768 oss_periods = oss_buffer_size / oss_period_size;
769
Takashi Iwai060d77b2006-03-27 16:44:52 +0200770 if (substream->oss.setup.periods > 1)
771 oss_periods = substream->oss.setup.periods;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
774 if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
775 s = runtime->oss.maxfrags;
776 if (oss_periods > s)
777 oss_periods = s;
778
779 s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
780 if (s < 2)
781 s = 2;
782 if (oss_periods < s)
783 oss_periods = s;
784
785 while (oss_period_size * oss_periods > oss_buffer_size)
786 oss_period_size /= 2;
787
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200788 if (oss_period_size < 16)
789 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 runtime->oss.period_bytes = oss_period_size;
791 runtime->oss.period_frames = 1;
792 runtime->oss.periods = oss_periods;
793 return 0;
794}
795
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100796static int choose_rate(struct snd_pcm_substream *substream,
797 struct snd_pcm_hw_params *params, unsigned int best_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100799 struct snd_interval *it;
800 struct snd_pcm_hw_params *save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned int rate, prev;
802
803 save = kmalloc(sizeof(*save), GFP_KERNEL);
804 if (save == NULL)
805 return -ENOMEM;
806 *save = *params;
807 it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
808
809 /* try multiples of the best rate */
810 rate = best_rate;
811 for (;;) {
812 if (it->max < rate || (it->max == rate && it->openmax))
813 break;
814 if (it->min < rate || (it->min == rate && !it->openmin)) {
815 int ret;
816 ret = snd_pcm_hw_param_set(substream, params,
817 SNDRV_PCM_HW_PARAM_RATE,
818 rate, 0);
819 if (ret == (int)rate) {
820 kfree(save);
821 return rate;
822 }
823 *params = *save;
824 }
825 prev = rate;
826 rate += best_rate;
827 if (rate <= prev)
828 break;
829 }
830
831 /* not found, use the nearest rate */
832 kfree(save);
833 return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
834}
835
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100836static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100838 struct snd_pcm_runtime *runtime = substream->runtime;
839 struct snd_pcm_hw_params *params, *sparams;
840 struct snd_pcm_sw_params *sw_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ssize_t oss_buffer_size, oss_period_size;
842 size_t oss_frame_size;
843 int err;
844 int direct;
845 int format, sformat, n;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100846 struct snd_mask sformat_mask;
847 struct snd_mask mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Takashi Iwaie3a5d592006-11-14 13:03:19 +0100849 if (mutex_lock_interruptible(&runtime->oss.params_lock))
850 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
852 params = kmalloc(sizeof(*params), GFP_KERNEL);
853 sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
854 if (!sw_params || !params || !sparams) {
855 snd_printd("No memory\n");
856 err = -ENOMEM;
857 goto failure;
858 }
859
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200860 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +0200862 else
863 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 _snd_pcm_hw_params_any(sparams);
866 _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
867 _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
868 snd_mask_none(&mask);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200869 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
871 else {
872 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
873 if (!direct)
874 snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
875 }
876 err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
877 if (err < 0) {
878 snd_printd("No usable accesses\n");
879 err = -EINVAL;
880 goto failure;
881 }
882 choose_rate(substream, sparams, runtime->oss.rate);
883 snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
884
885 format = snd_pcm_oss_format_from(runtime->oss.format);
886
887 sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
888 if (direct)
889 sformat = format;
890 else
891 sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
892
893 if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
894 for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
895 if (snd_mask_test(&sformat_mask, sformat) &&
896 snd_pcm_oss_format_to(sformat) >= 0)
897 break;
898 }
899 if (sformat > SNDRV_PCM_FORMAT_LAST) {
900 snd_printd("Cannot find a format!!!\n");
901 err = -EINVAL;
902 goto failure;
903 }
904 }
905 err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200906 if (err < 0)
907 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (direct) {
910 memcpy(params, sparams, sizeof(*params));
911 } else {
912 _snd_pcm_hw_params_any(params);
913 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
914 SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
915 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
916 snd_pcm_oss_format_from(runtime->oss.format), 0);
917 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
918 runtime->oss.channels, 0);
919 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
920 runtime->oss.rate, 0);
921 pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
922 params_access(params), params_format(params),
923 params_channels(params), params_rate(params));
924 }
925 pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
926 params_access(sparams), params_format(sparams),
927 params_channels(sparams), params_rate(sparams));
928
929 oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
930 params_channels(params) / 8;
931
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100932#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 snd_pcm_oss_plugin_clear(substream);
934 if (!direct) {
935 /* add necessary plugins */
936 snd_pcm_oss_plugin_clear(substream);
937 if ((err = snd_pcm_plug_format_plugins(substream,
938 params,
939 sparams)) < 0) {
940 snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
941 snd_pcm_oss_plugin_clear(substream);
942 goto failure;
943 }
944 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100945 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
947 snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
948 snd_pcm_oss_plugin_clear(substream);
949 goto failure;
950 }
951 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
952 err = snd_pcm_plugin_append(plugin);
953 } else {
954 err = snd_pcm_plugin_insert(plugin);
955 }
956 if (err < 0) {
957 snd_pcm_oss_plugin_clear(substream);
958 goto failure;
959 }
960 }
961 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 err = snd_pcm_oss_period_size(substream, params, sparams);
965 if (err < 0)
966 goto failure;
967
968 n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
969 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200970 if (err < 0)
971 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
974 runtime->oss.periods, NULL);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200975 if (err < 0)
976 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
979
980 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
981 snd_printd("HW_PARAMS failed: %i\n", err);
982 goto failure;
983 }
984
985 memset(sw_params, 0, sizeof(*sw_params));
986 if (runtime->oss.trigger) {
987 sw_params->start_threshold = 1;
988 } else {
989 sw_params->start_threshold = runtime->boundary;
990 }
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200991 if (atomic_read(&substream->mmap_count) ||
992 substream->stream == SNDRV_PCM_STREAM_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sw_params->stop_threshold = runtime->boundary;
994 else
995 sw_params->stop_threshold = runtime->buffer_size;
996 sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
997 sw_params->period_step = 1;
Takashi Iwai004e6532005-04-12 17:33:59 +0200998 sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
999 1 : runtime->period_size;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001000 if (atomic_read(&substream->mmap_count) ||
Takashi Iwai060d77b2006-03-27 16:44:52 +02001001 substream->oss.setup.nosilence) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 sw_params->silence_threshold = 0;
1003 sw_params->silence_size = 0;
1004 } else {
1005 snd_pcm_uframes_t frames;
1006 frames = runtime->period_size + 16;
1007 if (frames > runtime->buffer_size)
1008 frames = runtime->buffer_size;
1009 sw_params->silence_threshold = frames;
1010 sw_params->silence_size = frames;
1011 }
1012
1013 if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1014 snd_printd("SW_PARAMS failed: %i\n", err);
1015 goto failure;
1016 }
1017
1018 runtime->oss.periods = params_periods(sparams);
1019 oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001020 if (oss_period_size < 0) {
1021 err = -EINVAL;
1022 goto failure;
1023 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001024#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (runtime->oss.plugin_first) {
1026 err = snd_pcm_plug_alloc(substream, oss_period_size);
1027 if (err < 0)
1028 goto failure;
1029 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001030#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 oss_period_size *= oss_frame_size;
1032
1033 oss_buffer_size = oss_period_size * runtime->oss.periods;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001034 if (oss_buffer_size < 0) {
1035 err = -EINVAL;
1036 goto failure;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 runtime->oss.period_bytes = oss_period_size;
1040 runtime->oss.buffer_bytes = oss_buffer_size;
1041
1042 pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1043 runtime->oss.period_bytes,
1044 runtime->oss.buffer_bytes);
1045 pdprintf("slave: period_size = %i, buffer_size = %i\n",
1046 params_period_size(sparams),
1047 params_buffer_size(sparams));
1048
1049 runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1050 runtime->oss.channels = params_channels(params);
1051 runtime->oss.rate = params_rate(params);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 vfree(runtime->oss.buffer);
1054 runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
Roel Kluincbbb0572009-08-31 16:32:12 +02001055 if (!runtime->oss.buffer) {
1056 err = -ENOMEM;
1057 goto failure;
1058 }
1059
1060 runtime->oss.params = 0;
1061 runtime->oss.prepare = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 runtime->oss.buffer_used = 0;
1063 if (runtime->dma_area)
1064 snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
1065
1066 runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1067
1068 err = 0;
1069failure:
1070 kfree(sw_params);
1071 kfree(params);
1072 kfree(sparams);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001073 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return err;
1075}
1076
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001077static 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 -07001078{
1079 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001080 struct snd_pcm_substream *asubstream = NULL, *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 for (idx = 0; idx < 2; idx++) {
1083 substream = pcm_oss_file->streams[idx];
1084 if (substream == NULL)
1085 continue;
1086 if (asubstream == NULL)
1087 asubstream = substream;
1088 if (substream->runtime->oss.params) {
1089 err = snd_pcm_oss_change_params(substream);
1090 if (err < 0)
1091 return err;
1092 }
1093 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001094 if (!asubstream)
1095 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (r_substream)
1097 *r_substream = asubstream;
1098 return 0;
1099}
1100
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001101static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001104 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1107 if (err < 0) {
1108 snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1109 return err;
1110 }
1111 runtime->oss.prepare = 0;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001112 runtime->oss.prev_hw_ptr_period = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 runtime->oss.period_ptr = 0;
1114 runtime->oss.buffer_used = 0;
1115
1116 return 0;
1117}
1118
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001119static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001121 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int err;
1123
1124 if (substream == NULL)
1125 return 0;
1126 runtime = substream->runtime;
1127 if (runtime->oss.params) {
1128 err = snd_pcm_oss_change_params(substream);
1129 if (err < 0)
1130 return err;
1131 }
1132 if (runtime->oss.prepare) {
1133 err = snd_pcm_oss_prepare(substream);
1134 if (err < 0)
1135 return err;
1136 }
1137 return 0;
1138}
1139
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001140static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001142 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 snd_pcm_uframes_t frames;
1144 int err = 0;
1145
1146 while (1) {
1147 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1148 if (err < 0)
1149 break;
1150 runtime = substream->runtime;
1151 if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1152 break;
1153 /* in case of overrun, skip whole periods like OSS/Linux driver does */
1154 /* until avail(delay) <= buffer_size */
1155 frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1156 frames /= runtime->period_size;
1157 frames *= runtime->period_size;
1158 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1159 if (err < 0)
1160 break;
1161 }
1162 return err;
1163}
1164
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001165snd_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 -07001166{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001167 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int ret;
1169 while (1) {
1170 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1171 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1172#ifdef OSS_DEBUG
1173 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001174 printk(KERN_DEBUG "pcm_oss: write: "
1175 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001177 printk(KERN_DEBUG "pcm_oss: write: "
1178 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179#endif
1180 ret = snd_pcm_oss_prepare(substream);
1181 if (ret < 0)
1182 break;
1183 }
1184 if (in_kernel) {
1185 mm_segment_t fs;
1186 fs = snd_enter_user();
1187 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1188 snd_leave_user(fs);
1189 } else {
1190 ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
1191 }
1192 if (ret != -EPIPE && ret != -ESTRPIPE)
1193 break;
1194 /* test, if we can't store new data, because the stream */
1195 /* has not been started */
1196 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1197 return -EAGAIN;
1198 }
1199 return ret;
1200}
1201
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001202snd_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 -07001203{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001204 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 snd_pcm_sframes_t delay;
1206 int ret;
1207 while (1) {
1208 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1209 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1210#ifdef OSS_DEBUG
1211 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001212 printk(KERN_DEBUG "pcm_oss: read: "
1213 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001215 printk(KERN_DEBUG "pcm_oss: read: "
1216 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217#endif
1218 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1219 if (ret < 0)
1220 break;
1221 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1222 ret = snd_pcm_oss_prepare(substream);
1223 if (ret < 0)
1224 break;
1225 }
1226 ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1227 if (ret < 0)
1228 break;
1229 if (in_kernel) {
1230 mm_segment_t fs;
1231 fs = snd_enter_user();
1232 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1233 snd_leave_user(fs);
1234 } else {
1235 ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
1236 }
1237 if (ret == -EPIPE) {
1238 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1239 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1240 if (ret < 0)
1241 break;
1242 }
1243 continue;
1244 }
1245 if (ret != -ESTRPIPE)
1246 break;
1247 }
1248 return ret;
1249}
1250
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001251snd_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 -07001252{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001253 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int ret;
1255 while (1) {
1256 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1257 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1258#ifdef OSS_DEBUG
1259 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001260 printk(KERN_DEBUG "pcm_oss: writev: "
1261 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001263 printk(KERN_DEBUG "pcm_oss: writev: "
1264 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265#endif
1266 ret = snd_pcm_oss_prepare(substream);
1267 if (ret < 0)
1268 break;
1269 }
1270 if (in_kernel) {
1271 mm_segment_t fs;
1272 fs = snd_enter_user();
1273 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1274 snd_leave_user(fs);
1275 } else {
1276 ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
1277 }
1278 if (ret != -EPIPE && ret != -ESTRPIPE)
1279 break;
1280
1281 /* test, if we can't store new data, because the stream */
1282 /* has not been started */
1283 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
1284 return -EAGAIN;
1285 }
1286 return ret;
1287}
1288
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001289snd_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 -07001290{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001291 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int ret;
1293 while (1) {
1294 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
1295 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1296#ifdef OSS_DEBUG
1297 if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
Takashi Iwai006de2672009-02-05 15:51:04 +01001298 printk(KERN_DEBUG "pcm_oss: readv: "
1299 "recovering from XRUN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 else
Takashi Iwai006de2672009-02-05 15:51:04 +01001301 printk(KERN_DEBUG "pcm_oss: readv: "
1302 "recovering from SUSPEND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#endif
1304 ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1305 if (ret < 0)
1306 break;
1307 } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
1308 ret = snd_pcm_oss_prepare(substream);
1309 if (ret < 0)
1310 break;
1311 }
1312 if (in_kernel) {
1313 mm_segment_t fs;
1314 fs = snd_enter_user();
1315 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1316 snd_leave_user(fs);
1317 } else {
1318 ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
1319 }
1320 if (ret != -EPIPE && ret != -ESTRPIPE)
1321 break;
1322 }
1323 return ret;
1324}
1325
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001326static 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 -07001327{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001328 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001330#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001332 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1334 if (!in_kernel) {
1335 if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
1336 return -EFAULT;
1337 buf = runtime->oss.buffer;
1338 }
1339 frames = bytes / oss_frame_bytes;
1340 frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1341 if (frames1 < 0)
1342 return frames1;
1343 frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1344 if (frames1 <= 0)
1345 return frames1;
1346 bytes = frames1 * oss_frame_bytes;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001347 } else
1348#endif
1349 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 frames = bytes_to_frames(runtime, bytes);
1351 frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1352 if (frames1 <= 0)
1353 return frames1;
1354 bytes = frames_to_bytes(runtime, frames1);
1355 }
1356 return bytes;
1357}
1358
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001359static 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 -07001360{
1361 size_t xfer = 0;
1362 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001363 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001365 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -ENXIO;
1367
1368 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1369 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001370 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 while (bytes > 0) {
1372 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1373 tmp = bytes;
1374 if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1375 tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1376 if (tmp > 0) {
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001377 if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1378 tmp = -EFAULT;
1379 goto err;
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 runtime->oss.buffer_used += tmp;
1383 buf += tmp;
1384 bytes -= tmp;
1385 xfer += tmp;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001386 if (substream->oss.setup.partialfrag ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 runtime->oss.buffer_used == runtime->oss.period_bytes) {
1388 tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
1389 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1390 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001391 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 runtime->oss.bytes += tmp;
1393 runtime->oss.period_ptr += tmp;
1394 runtime->oss.period_ptr %= runtime->oss.period_bytes;
1395 if (runtime->oss.period_ptr == 0 ||
1396 runtime->oss.period_ptr == runtime->oss.buffer_used)
1397 runtime->oss.buffer_used = 0;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001398 else if ((substream->f_flags & O_NONBLOCK) != 0) {
1399 tmp = -EAGAIN;
1400 goto err;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001404 tmp = snd_pcm_oss_write2(substream,
1405 (const char __force *)buf,
1406 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001408 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 runtime->oss.bytes += tmp;
1410 buf += tmp;
1411 bytes -= tmp;
1412 xfer += tmp;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001413 if ((substream->f_flags & O_NONBLOCK) != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 tmp != runtime->oss.period_bytes)
1415 break;
1416 }
1417 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001418 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001420
1421 err:
1422 mutex_unlock(&runtime->oss.params_lock);
1423 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001426static 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 -07001427{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001428 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 snd_pcm_sframes_t frames, frames1;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001430#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 char __user *final_dst = (char __user *)buf;
1432 if (runtime->oss.plugin_first) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001433 struct snd_pcm_plugin_channel *channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1435 if (!in_kernel)
1436 buf = runtime->oss.buffer;
1437 frames = bytes / oss_frame_bytes;
1438 frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1439 if (frames1 < 0)
1440 return frames1;
1441 frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1442 if (frames1 <= 0)
1443 return frames1;
1444 bytes = frames1 * oss_frame_bytes;
1445 if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1446 return -EFAULT;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01001447 } else
1448#endif
1449 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 frames = bytes_to_frames(runtime, bytes);
1451 frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1452 if (frames1 <= 0)
1453 return frames1;
1454 bytes = frames_to_bytes(runtime, frames1);
1455 }
1456 return bytes;
1457}
1458
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001459static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 size_t xfer = 0;
1462 ssize_t tmp;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001463 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001465 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -ENXIO;
1467
1468 if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
1469 return tmp;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001470 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 while (bytes > 0) {
1472 if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1473 if (runtime->oss.buffer_used == 0) {
1474 tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1475 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001476 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 runtime->oss.bytes += tmp;
1478 runtime->oss.period_ptr = tmp;
1479 runtime->oss.buffer_used = tmp;
1480 }
1481 tmp = bytes;
1482 if ((size_t) tmp > runtime->oss.buffer_used)
1483 tmp = runtime->oss.buffer_used;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001484 if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1485 tmp = -EFAULT;
1486 goto err;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 buf += tmp;
1489 bytes -= tmp;
1490 xfer += tmp;
1491 runtime->oss.buffer_used -= tmp;
1492 } else {
Clemens Ladisch4d233592005-09-05 10:35:20 +02001493 tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1494 runtime->oss.period_bytes, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (tmp <= 0)
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001496 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 runtime->oss.bytes += tmp;
1498 buf += tmp;
1499 bytes -= tmp;
1500 xfer += tmp;
1501 }
1502 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001503 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return xfer;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001505
1506 err:
1507 mutex_unlock(&runtime->oss.params_lock);
1508 return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001511static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001513 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1516 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001517 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 substream->runtime->oss.prepare = 1;
1519 }
1520 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1521 if (substream != NULL) {
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001522 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 substream->runtime->oss.prepare = 1;
1524 }
1525 return 0;
1526}
1527
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001528static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001530 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int err;
1532
1533 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1534 if (substream != NULL) {
1535 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1536 return err;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001537 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539 /* note: all errors from the start action are ignored */
1540 /* OSS apps do not know, how to handle them */
1541 return 0;
1542}
1543
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001544static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001546 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 ssize_t result = 0;
1548 long res;
1549 wait_queue_t wait;
1550
1551 runtime = substream->runtime;
1552 init_waitqueue_entry(&wait, current);
1553 add_wait_queue(&runtime->sleep, &wait);
1554#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001555 printk(KERN_DEBUG "sync1: size = %li\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#endif
1557 while (1) {
1558 result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1559 if (result > 0) {
1560 runtime->oss.buffer_used = 0;
1561 result = 0;
1562 break;
1563 }
1564 if (result != 0 && result != -EAGAIN)
1565 break;
1566 result = 0;
1567 set_current_state(TASK_INTERRUPTIBLE);
1568 snd_pcm_stream_lock_irq(substream);
1569 res = runtime->status->state;
1570 snd_pcm_stream_unlock_irq(substream);
1571 if (res != SNDRV_PCM_STATE_RUNNING) {
1572 set_current_state(TASK_RUNNING);
1573 break;
1574 }
1575 res = schedule_timeout(10 * HZ);
1576 if (signal_pending(current)) {
1577 result = -ERESTARTSYS;
1578 break;
1579 }
1580 if (res == 0) {
1581 snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
1582 result = -EIO;
1583 break;
1584 }
1585 }
1586 remove_wait_queue(&runtime->sleep, &wait);
1587 return result;
1588}
1589
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001590static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 int err = 0;
1593 unsigned int saved_f_flags;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001594 struct snd_pcm_substream *substream;
1595 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 snd_pcm_format_t format;
1597 unsigned long width;
1598 size_t size;
1599
1600 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1601 if (substream != NULL) {
1602 runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001603 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto __direct;
1605 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1606 return err;
1607 format = snd_pcm_oss_format_from(runtime->oss.format);
1608 width = snd_pcm_format_physical_width(format);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001609 mutex_lock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (runtime->oss.buffer_used > 0) {
1611#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001612 printk(KERN_DEBUG "sync: buffer_used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#endif
1614 size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1615 snd_pcm_format_set_silence(format,
1616 runtime->oss.buffer + runtime->oss.buffer_used,
1617 size);
1618 err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001619 if (err < 0) {
1620 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else if (runtime->oss.period_ptr > 0) {
1624#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001625 printk(KERN_DEBUG "sync: period_ptr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#endif
1627 size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1628 snd_pcm_format_set_silence(format,
1629 runtime->oss.buffer,
1630 size * 8 / width);
1631 err = snd_pcm_oss_sync1(substream, size);
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001632 if (err < 0) {
1633 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return err;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 /*
1638 * The ALSA's period might be a bit large than OSS one.
1639 * Fill the remain portion of ALSA period with zeros.
1640 */
1641 size = runtime->control->appl_ptr % runtime->period_size;
1642 if (size > 0) {
1643 size = runtime->period_size - size;
1644 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1645 size = (runtime->frame_bits * size) / 8;
1646 while (size > 0) {
1647 mm_segment_t fs;
1648 size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
1649 size -= size1;
1650 size1 *= 8;
1651 size1 /= runtime->sample_bits;
1652 snd_pcm_format_set_silence(runtime->format,
1653 runtime->oss.buffer,
1654 size1);
Takashi Iwai4939c662008-01-28 23:53:41 +01001655 size1 /= runtime->channels; /* frames */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 fs = snd_enter_user();
1657 snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
1658 snd_leave_user(fs);
1659 }
1660 } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
1661 void __user *buffers[runtime->channels];
1662 memset(buffers, 0, runtime->channels * sizeof(void *));
1663 snd_pcm_lib_writev(substream, buffers, size);
1664 }
1665 }
Takashi Iwaie3a5d592006-11-14 13:03:19 +01001666 mutex_unlock(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
1668 * finish sync: drain the buffer
1669 */
1670 __direct:
Takashi Iwai0df63e42006-04-28 15:13:41 +02001671 saved_f_flags = substream->f_flags;
1672 substream->f_flags &= ~O_NONBLOCK;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001673 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
Takashi Iwai0df63e42006-04-28 15:13:41 +02001674 substream->f_flags = saved_f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (err < 0)
1676 return err;
1677 runtime->oss.prepare = 1;
1678 }
1679
1680 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1681 if (substream != NULL) {
1682 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
1683 return err;
1684 runtime = substream->runtime;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02001685 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (err < 0)
1687 return err;
1688 runtime->oss.buffer_used = 0;
1689 runtime->oss.prepare = 1;
1690 }
1691 return 0;
1692}
1693
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001694static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 int idx;
1697
1698 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001699 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1700 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (substream == NULL)
1702 continue;
1703 runtime = substream->runtime;
1704 if (rate < 1000)
1705 rate = 1000;
1706 else if (rate > 192000)
1707 rate = 192000;
1708 if (runtime->oss.rate != rate) {
1709 runtime->oss.params = 1;
1710 runtime->oss.rate = rate;
1711 }
1712 }
1713 return snd_pcm_oss_get_rate(pcm_oss_file);
1714}
1715
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001716static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001718 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int err;
1720
1721 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1722 return err;
1723 return substream->runtime->oss.rate;
1724}
1725
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001726static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 int idx;
1729 if (channels < 1)
1730 channels = 1;
1731 if (channels > 128)
1732 return -EINVAL;
1733 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001734 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1735 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (substream == NULL)
1737 continue;
1738 runtime = substream->runtime;
1739 if (runtime->oss.channels != channels) {
1740 runtime->oss.params = 1;
1741 runtime->oss.channels = channels;
1742 }
1743 }
1744 return snd_pcm_oss_get_channels(pcm_oss_file);
1745}
1746
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001747static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001749 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int err;
1751
1752 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1753 return err;
1754 return substream->runtime->oss.channels;
1755}
1756
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001757static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001759 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int err;
1761
1762 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1763 return err;
1764 return substream->runtime->oss.period_bytes;
1765}
1766
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001767static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001769 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 int err;
1771 int direct;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001772 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned int formats = 0;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001774 struct snd_mask format_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int fmt;
1776
1777 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1778 return err;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001779 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 direct = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02001781 else
1782 direct = substream->oss.setup.direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!direct)
1784 return AFMT_MU_LAW | AFMT_U8 |
1785 AFMT_S16_LE | AFMT_S16_BE |
1786 AFMT_S8 | AFMT_U16_LE |
Takashi Iwai24038a22007-08-08 17:00:32 +02001787 AFMT_U16_BE |
1788 AFMT_S32_LE | AFMT_S32_BE |
Roel Kluin7924f0c2009-02-04 18:14:55 +01001789 AFMT_S24_LE | AFMT_S24_BE |
Takashi Iwai24038a22007-08-08 17:00:32 +02001790 AFMT_S24_PACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 params = kmalloc(sizeof(*params), GFP_KERNEL);
1792 if (!params)
1793 return -ENOMEM;
1794 _snd_pcm_hw_params_any(params);
1795 err = snd_pcm_hw_refine(substream, params);
1796 format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1797 kfree(params);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001798 if (err < 0)
1799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 for (fmt = 0; fmt < 32; ++fmt) {
1801 if (snd_mask_test(&format_mask, fmt)) {
1802 int f = snd_pcm_oss_format_to(fmt);
1803 if (f >= 0)
1804 formats |= f;
1805 }
1806 }
1807 return formats;
1808}
1809
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001810static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 int formats, idx;
1813
1814 if (format != AFMT_QUERY) {
1815 formats = snd_pcm_oss_get_formats(pcm_oss_file);
Steven Finney5c59e092006-04-13 12:49:31 +02001816 if (formats < 0)
1817 return formats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (!(formats & format))
1819 format = AFMT_U8;
1820 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001821 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1822 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (substream == NULL)
1824 continue;
1825 runtime = substream->runtime;
1826 if (runtime->oss.format != format) {
1827 runtime->oss.params = 1;
1828 runtime->oss.format = format;
1829 }
1830 }
1831 }
1832 return snd_pcm_oss_get_format(pcm_oss_file);
1833}
1834
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001835static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001837 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int err;
1839
1840 if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
1841 return err;
1842 return substream->runtime->oss.format;
1843}
1844
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001845static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001847 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (substream == NULL)
1850 return 0;
1851 runtime = substream->runtime;
1852 if (subdivide == 0) {
1853 subdivide = runtime->oss.subdivision;
1854 if (subdivide == 0)
1855 subdivide = 1;
1856 return subdivide;
1857 }
1858 if (runtime->oss.subdivision || runtime->oss.fragshift)
1859 return -EINVAL;
1860 if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1861 subdivide != 8 && subdivide != 16)
1862 return -EINVAL;
1863 runtime->oss.subdivision = subdivide;
1864 runtime->oss.params = 1;
1865 return subdivide;
1866}
1867
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001868static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 int err = -EINVAL, idx;
1871
1872 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001873 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (substream == NULL)
1875 continue;
1876 if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
1877 return err;
1878 }
1879 return err;
1880}
1881
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001882static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001884 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 if (substream == NULL)
1887 return 0;
1888 runtime = substream->runtime;
1889 if (runtime->oss.subdivision || runtime->oss.fragshift)
1890 return -EINVAL;
1891 runtime->oss.fragshift = val & 0xffff;
1892 runtime->oss.maxfrags = (val >> 16) & 0xffff;
1893 if (runtime->oss.fragshift < 4) /* < 16 */
1894 runtime->oss.fragshift = 4;
1895 if (runtime->oss.maxfrags < 2)
1896 runtime->oss.maxfrags = 2;
1897 runtime->oss.params = 1;
1898 return 0;
1899}
1900
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001901static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 int err = -EINVAL, idx;
1904
1905 for (idx = 1; idx >= 0; --idx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001906 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (substream == NULL)
1908 continue;
1909 if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
1910 return err;
1911 }
1912 return err;
1913}
1914
1915static int snd_pcm_oss_nonblock(struct file * file)
1916{
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001917 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 file->f_flags |= O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001919 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return 0;
1921}
1922
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001923static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925
1926 if (substream == NULL) {
1927 res &= ~DSP_CAP_DUPLEX;
1928 return res;
1929 }
1930#ifdef DSP_CAP_MULTI
1931 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1932 if (substream->pstr->substream_count > 1)
1933 res |= DSP_CAP_MULTI;
1934#endif
1935 /* DSP_CAP_REALTIME is set all times: */
1936 /* all ALSA drivers can return actual pointer in ring buffer */
1937#if defined(DSP_CAP_REALTIME) && 0
1938 {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001939 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
1941 res &= ~DSP_CAP_REALTIME;
1942 }
1943#endif
1944 return res;
1945}
1946
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001947static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 int result, idx;
1950
1951 result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
1952 for (idx = 0; idx < 2; idx++) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001953 struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 result = snd_pcm_oss_get_caps1(substream, result);
1955 }
1956 result |= 0x0001; /* revision - same as SB AWE 64 */
1957 return result;
1958}
1959
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001960static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
1961 snd_pcm_uframes_t hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001963 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 snd_pcm_uframes_t appl_ptr;
1965 appl_ptr = hw_ptr + runtime->buffer_size;
1966 appl_ptr %= runtime->boundary;
1967 runtime->control->appl_ptr = appl_ptr;
1968}
1969
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001970static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01001972 struct snd_pcm_runtime *runtime;
1973 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int err, cmd;
1975
1976#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01001977 printk(KERN_DEBUG "pcm_oss: trigger = 0x%x\n", trigger);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978#endif
1979
1980 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1981 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1982
1983 if (psubstream) {
1984 if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
1985 return err;
1986 }
1987 if (csubstream) {
1988 if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
1989 return err;
1990 }
1991 if (psubstream) {
1992 runtime = psubstream->runtime;
1993 if (trigger & PCM_ENABLE_OUTPUT) {
1994 if (runtime->oss.trigger)
1995 goto _skip1;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02001996 if (atomic_read(&psubstream->mmap_count))
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01001997 snd_pcm_oss_simulate_fill(psubstream,
1998 get_hw_ptr_period(runtime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 runtime->oss.trigger = 1;
2000 runtime->start_threshold = 1;
2001 cmd = SNDRV_PCM_IOCTL_START;
2002 } else {
2003 if (!runtime->oss.trigger)
2004 goto _skip1;
2005 runtime->oss.trigger = 0;
2006 runtime->start_threshold = runtime->boundary;
2007 cmd = SNDRV_PCM_IOCTL_DROP;
2008 runtime->oss.prepare = 1;
2009 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002010 err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (err < 0)
2012 return err;
2013 }
2014 _skip1:
2015 if (csubstream) {
2016 runtime = csubstream->runtime;
2017 if (trigger & PCM_ENABLE_INPUT) {
2018 if (runtime->oss.trigger)
2019 goto _skip2;
2020 runtime->oss.trigger = 1;
2021 runtime->start_threshold = 1;
2022 cmd = SNDRV_PCM_IOCTL_START;
2023 } else {
2024 if (!runtime->oss.trigger)
2025 goto _skip2;
2026 runtime->oss.trigger = 0;
2027 runtime->start_threshold = runtime->boundary;
2028 cmd = SNDRV_PCM_IOCTL_DROP;
2029 runtime->oss.prepare = 1;
2030 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002031 err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (err < 0)
2033 return err;
2034 }
2035 _skip2:
2036 return 0;
2037}
2038
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002039static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002041 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 int result = 0;
2043
2044 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2045 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2046 if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2047 result |= PCM_ENABLE_OUTPUT;
2048 if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2049 result |= PCM_ENABLE_INPUT;
2050 return result;
2051}
2052
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002053static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002055 struct snd_pcm_substream *substream;
2056 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 snd_pcm_sframes_t delay;
2058 int err;
2059
2060 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2061 if (substream == NULL)
2062 return -EINVAL;
2063 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2064 return err;
2065 runtime = substream->runtime;
2066 if (runtime->oss.params || runtime->oss.prepare)
2067 return 0;
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002068 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (err == -EPIPE)
2070 delay = 0; /* hack for broken OSS applications */
2071 else if (err < 0)
2072 return err;
2073 return snd_pcm_oss_bytes(substream, delay);
2074}
2075
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002076static 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 -07002077{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002078 struct snd_pcm_substream *substream;
2079 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 snd_pcm_sframes_t delay;
2081 int fixup;
2082 struct count_info info;
2083 int err;
2084
2085 if (_info == NULL)
2086 return -EFAULT;
2087 substream = pcm_oss_file->streams[stream];
2088 if (substream == NULL)
2089 return -EINVAL;
2090 if ((err = snd_pcm_oss_make_ready(substream)) < 0)
2091 return err;
2092 runtime = substream->runtime;
2093 if (runtime->oss.params || runtime->oss.prepare) {
2094 memset(&info, 0, sizeof(info));
2095 if (copy_to_user(_info, &info, sizeof(info)))
2096 return -EFAULT;
2097 return 0;
2098 }
2099 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2100 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2101 if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2102 err = 0;
2103 delay = 0;
2104 fixup = 0;
2105 } else {
2106 fixup = runtime->oss.buffer_used;
2107 }
2108 } else {
2109 err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2110 fixup = -runtime->oss.buffer_used;
2111 }
2112 if (err < 0)
2113 return err;
2114 info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002115 if (atomic_read(&substream->mmap_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 snd_pcm_sframes_t n;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002117 delay = get_hw_ptr_period(runtime);
2118 n = delay - runtime->oss.prev_hw_ptr_period;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (n < 0)
2120 n += runtime->boundary;
2121 info.blocks = n / runtime->period_size;
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002122 runtime->oss.prev_hw_ptr_period = delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2124 snd_pcm_oss_simulate_fill(substream, delay);
2125 info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2126 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002127 delay = snd_pcm_oss_bytes(substream, delay);
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002128 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002129 if (substream->oss.setup.buggyptr)
Takashi Iwai10f69f92005-09-08 13:48:34 +02002130 info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2131 else
2132 info.blocks = (delay + fixup) / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002134 } else {
Jaroslav Kysela5ac0fab2005-05-31 16:59:39 +02002135 delay += fixup;
2136 info.blocks = delay / runtime->oss.period_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
Jaroslav Kyselafb4bd0a2005-05-31 15:44:23 +02002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 if (copy_to_user(_info, &info, sizeof(info)))
2141 return -EFAULT;
2142 return 0;
2143}
2144
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002145static 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 -07002146{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002147 struct snd_pcm_substream *substream;
2148 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 snd_pcm_sframes_t avail;
2150 int fixup;
2151 struct audio_buf_info info;
2152 int err;
2153
2154 if (_info == NULL)
2155 return -EFAULT;
2156 substream = pcm_oss_file->streams[stream];
2157 if (substream == NULL)
2158 return -EINVAL;
2159 runtime = substream->runtime;
2160
2161 if (runtime->oss.params &&
2162 (err = snd_pcm_oss_change_params(substream)) < 0)
2163 return err;
2164
2165 info.fragsize = runtime->oss.period_bytes;
2166 info.fragstotal = runtime->periods;
2167 if (runtime->oss.prepare) {
2168 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2169 info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2170 info.fragments = runtime->oss.periods;
2171 } else {
2172 info.bytes = 0;
2173 info.fragments = 0;
2174 }
2175 } else {
2176 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2177 err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2178 if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2179 avail = runtime->buffer_size;
2180 err = 0;
2181 fixup = 0;
2182 } else {
2183 avail = runtime->buffer_size - avail;
2184 fixup = -runtime->oss.buffer_used;
2185 }
2186 } else {
2187 err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2188 fixup = runtime->oss.buffer_used;
2189 }
2190 if (err < 0)
2191 return err;
2192 info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2193 info.fragments = info.bytes / runtime->oss.period_bytes;
2194 }
2195
2196#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002197 printk(KERN_DEBUG "pcm_oss: space: bytes = %i, fragments = %i, "
2198 "fragstotal = %i, fragsize = %i\n",
2199 info.bytes, info.fragments, info.fragstotal, info.fragsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200#endif
2201 if (copy_to_user(_info, &info, sizeof(info)))
2202 return -EFAULT;
2203 return 0;
2204}
2205
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002206static 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 -07002207{
2208 // it won't be probably implemented
2209 // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
2210 return -EINVAL;
2211}
2212
Takashi Iwai060d77b2006-03-27 16:44:52 +02002213static const char *strip_task_path(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Takashi Iwai060d77b2006-03-27 16:44:52 +02002215 const char *ptr, *ptrl = NULL;
2216 for (ptr = path; *ptr; ptr++) {
2217 if (*ptr == '/')
2218 ptrl = ptr + 1;
2219 }
2220 return ptrl;
2221}
2222
2223static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2224 const char *task_name,
2225 struct snd_pcm_oss_setup *rsetup)
2226{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002227 struct snd_pcm_oss_setup *setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002229 mutex_lock(&pcm->streams[stream].oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002230 do {
2231 for (setup = pcm->streams[stream].oss.setup_list; setup;
2232 setup = setup->next) {
2233 if (!strcmp(setup->task_name, task_name))
2234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002236 } while ((task_name = strip_task_path(task_name)) != NULL);
2237 out:
2238 if (setup)
2239 *rsetup = *setup;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002240 mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002243static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2244{
2245 struct snd_pcm_runtime *runtime;
2246 runtime = substream->runtime;
2247 vfree(runtime->oss.buffer);
2248 runtime->oss.buffer = NULL;
2249#ifdef CONFIG_SND_PCM_OSS_PLUGINS
2250 snd_pcm_oss_plugin_clear(substream);
2251#endif
2252 substream->oss.oss = 0;
2253}
2254
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002255static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2256 struct snd_pcm_oss_setup *setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 int minor)
2258{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002259 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 substream->oss.oss = 1;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002262 substream->oss.setup = *setup;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002263 if (setup->nonblock)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002264 substream->f_flags |= O_NONBLOCK;
Takashi Iwai15762742006-04-06 19:47:42 +02002265 else if (setup->block)
Takashi Iwai0df63e42006-04-28 15:13:41 +02002266 substream->f_flags &= ~O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 runtime = substream->runtime;
2268 runtime->oss.params = 1;
2269 runtime->oss.trigger = 1;
2270 runtime->oss.rate = 8000;
Takashi Iwaie3a5d592006-11-14 13:03:19 +01002271 mutex_init(&runtime->oss.params_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2273 case SNDRV_MINOR_OSS_PCM_8:
2274 runtime->oss.format = AFMT_U8;
2275 break;
2276 case SNDRV_MINOR_OSS_PCM_16:
2277 runtime->oss.format = AFMT_S16_LE;
2278 break;
2279 default:
2280 runtime->oss.format = AFMT_MU_LAW;
2281 }
2282 runtime->oss.channels = 1;
2283 runtime->oss.fragshift = 0;
2284 runtime->oss.maxfrags = 0;
2285 runtime->oss.subdivision = 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002286 substream->pcm_release = snd_pcm_oss_release_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002289static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 int cidx;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002292 if (!pcm_oss_file)
2293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 for (cidx = 0; cidx < 2; ++cidx) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002295 struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002296 if (substream)
2297 snd_pcm_release_substream(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 kfree(pcm_oss_file);
2300 return 0;
2301}
2302
2303static int snd_pcm_oss_open_file(struct file *file,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002304 struct snd_pcm *pcm,
2305 struct snd_pcm_oss_file **rpcm_oss_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int minor,
Takashi Iwai060d77b2006-03-27 16:44:52 +02002307 struct snd_pcm_oss_setup *setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002309 int idx, err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002310 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002311 struct snd_pcm_substream *substream;
Al Viroaeb5d722008-09-02 15:28:45 -04002312 fmode_t f_mode = file->f_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002314 if (rpcm_oss_file)
2315 *rpcm_oss_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Takashi Iwaica2c0962005-09-09 14:20:23 +02002317 pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (pcm_oss_file == NULL)
2319 return -ENOMEM;
2320
2321 if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2322 (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2323 f_mode = FMODE_WRITE;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002324
Takashi Iwai0df63e42006-04-28 15:13:41 +02002325 file->f_flags &= ~O_APPEND;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002326 for (idx = 0; idx < 2; idx++) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002327 if (setup[idx].disable)
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002328 continue;
Takashi Iwai6cb53e72006-08-02 21:12:09 +02002329 if (! pcm->streams[idx].substream_count)
2330 continue; /* no matching substream */
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002331 if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2332 if (! (f_mode & FMODE_WRITE))
2333 continue;
2334 } else {
2335 if (! (f_mode & FMODE_READ))
2336 continue;
2337 }
2338 err = snd_pcm_open_substream(pcm, idx, file, &substream);
2339 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 snd_pcm_oss_release_file(pcm_oss_file);
2341 return err;
2342 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002343
2344 pcm_oss_file->streams[idx] = substream;
Takashi Iwai15762742006-04-06 19:47:42 +02002345 substream->file = pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002346 snd_pcm_oss_init_substream(substream, &setup[idx], minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348
OGAWA Hirofumibbdc1b72006-04-06 19:42:40 +02002349 if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 snd_pcm_oss_release_file(pcm_oss_file);
2351 return -EINVAL;
2352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 file->private_data = pcm_oss_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002355 if (rpcm_oss_file)
2356 *rpcm_oss_file = pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return 0;
2358}
2359
2360
Takashi Iwai93f2e372005-10-10 11:51:55 +02002361static int snd_task_name(struct task_struct *task, char *name, size_t size)
2362{
2363 unsigned int idx;
2364
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002365 if (snd_BUG_ON(!task || !name || size < 2))
2366 return -EINVAL;
Takashi Iwai93f2e372005-10-10 11:51:55 +02002367 for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2368 name[idx] = task->comm[idx];
2369 name[idx] = '\0';
2370 return 0;
2371}
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int err;
2376 char task_name[32];
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002377 struct snd_pcm *pcm;
2378 struct snd_pcm_oss_file *pcm_oss_file;
Takashi Iwai060d77b2006-03-27 16:44:52 +02002379 struct snd_pcm_oss_setup setup[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int nonblock;
2381 wait_queue_t wait;
2382
Clemens Ladischf87135f2005-11-20 14:06:59 +01002383 pcm = snd_lookup_oss_minor_data(iminor(inode),
2384 SNDRV_OSS_DEVICE_TYPE_PCM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (pcm == NULL) {
2386 err = -ENODEV;
2387 goto __error1;
2388 }
2389 err = snd_card_file_add(pcm->card, file);
2390 if (err < 0)
2391 goto __error1;
2392 if (!try_module_get(pcm->card->module)) {
2393 err = -EFAULT;
2394 goto __error2;
2395 }
2396 if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2397 err = -EFAULT;
2398 goto __error;
2399 }
Takashi Iwai15762742006-04-06 19:47:42 +02002400 memset(setup, 0, sizeof(setup));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (file->f_mode & FMODE_WRITE)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002402 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2403 task_name, &setup[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (file->f_mode & FMODE_READ)
Takashi Iwai060d77b2006-03-27 16:44:52 +02002405 snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2406 task_name, &setup[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 nonblock = !!(file->f_flags & O_NONBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (!nonblock)
2410 nonblock = nonblock_open;
2411
2412 init_waitqueue_entry(&wait, current);
2413 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002414 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 while (1) {
2416 err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002417 iminor(inode), setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (err >= 0)
2419 break;
2420 if (err == -EAGAIN) {
2421 if (nonblock) {
2422 err = -EBUSY;
2423 break;
2424 }
2425 } else
2426 break;
2427 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002428 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002430 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (signal_pending(current)) {
2432 err = -ERESTARTSYS;
2433 break;
2434 }
2435 }
2436 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002437 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (err < 0)
2439 goto __error;
2440 return err;
2441
2442 __error:
2443 module_put(pcm->card->module);
2444 __error2:
2445 snd_card_file_remove(pcm->card, file);
2446 __error1:
2447 return err;
2448}
2449
2450static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2451{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002452 struct snd_pcm *pcm;
2453 struct snd_pcm_substream *substream;
2454 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 pcm_oss_file = file->private_data;
2457 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2458 if (substream == NULL)
2459 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002460 if (snd_BUG_ON(!substream))
2461 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 pcm = substream->pcm;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01002463 if (!pcm->card->shutdown)
2464 snd_pcm_oss_sync(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002465 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 snd_pcm_oss_release_file(pcm_oss_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002467 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 wake_up(&pcm->open_wait);
2469 module_put(pcm->card->module);
2470 snd_card_file_remove(pcm->card, file);
2471 return 0;
2472}
2473
2474static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2475{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002476 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int __user *p = (int __user *)arg;
2478 int res;
2479
2480 pcm_oss_file = file->private_data;
2481 if (cmd == OSS_GETVERSION)
2482 return put_user(SNDRV_OSS_VERSION, p);
2483 if (cmd == OSS_ALSAEMULVER)
2484 return put_user(1, p);
2485#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
2486 if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002487 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 int idx;
2489 for (idx = 0; idx < 2; ++idx) {
2490 substream = pcm_oss_file->streams[idx];
2491 if (substream != NULL)
2492 break;
2493 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002494 if (snd_BUG_ON(idx >= 2))
2495 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2497 }
2498#endif
2499 if (((cmd >> 8) & 0xff) != 'P')
2500 return -EINVAL;
2501#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002502 printk(KERN_DEBUG "pcm_oss: ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503#endif
2504 switch (cmd) {
2505 case SNDCTL_DSP_RESET:
2506 return snd_pcm_oss_reset(pcm_oss_file);
2507 case SNDCTL_DSP_SYNC:
2508 return snd_pcm_oss_sync(pcm_oss_file);
2509 case SNDCTL_DSP_SPEED:
2510 if (get_user(res, p))
2511 return -EFAULT;
2512 if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
2513 return res;
2514 return put_user(res, p);
2515 case SOUND_PCM_READ_RATE:
2516 res = snd_pcm_oss_get_rate(pcm_oss_file);
2517 if (res < 0)
2518 return res;
2519 return put_user(res, p);
2520 case SNDCTL_DSP_STEREO:
2521 if (get_user(res, p))
2522 return -EFAULT;
2523 res = res > 0 ? 2 : 1;
2524 if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
2525 return res;
2526 return put_user(--res, p);
2527 case SNDCTL_DSP_GETBLKSIZE:
2528 res = snd_pcm_oss_get_block_size(pcm_oss_file);
2529 if (res < 0)
2530 return res;
2531 return put_user(res, p);
2532 case SNDCTL_DSP_SETFMT:
2533 if (get_user(res, p))
2534 return -EFAULT;
2535 res = snd_pcm_oss_set_format(pcm_oss_file, res);
2536 if (res < 0)
2537 return res;
2538 return put_user(res, p);
2539 case SOUND_PCM_READ_BITS:
2540 res = snd_pcm_oss_get_format(pcm_oss_file);
2541 if (res < 0)
2542 return res;
2543 return put_user(res, p);
2544 case SNDCTL_DSP_CHANNELS:
2545 if (get_user(res, p))
2546 return -EFAULT;
2547 res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2548 if (res < 0)
2549 return res;
2550 return put_user(res, p);
2551 case SOUND_PCM_READ_CHANNELS:
2552 res = snd_pcm_oss_get_channels(pcm_oss_file);
2553 if (res < 0)
2554 return res;
2555 return put_user(res, p);
2556 case SOUND_PCM_WRITE_FILTER:
2557 case SOUND_PCM_READ_FILTER:
2558 return -EIO;
2559 case SNDCTL_DSP_POST:
2560 return snd_pcm_oss_post(pcm_oss_file);
2561 case SNDCTL_DSP_SUBDIVIDE:
2562 if (get_user(res, p))
2563 return -EFAULT;
2564 res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2565 if (res < 0)
2566 return res;
2567 return put_user(res, p);
2568 case SNDCTL_DSP_SETFRAGMENT:
2569 if (get_user(res, p))
2570 return -EFAULT;
2571 return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2572 case SNDCTL_DSP_GETFMTS:
2573 res = snd_pcm_oss_get_formats(pcm_oss_file);
2574 if (res < 0)
2575 return res;
2576 return put_user(res, p);
2577 case SNDCTL_DSP_GETOSPACE:
2578 case SNDCTL_DSP_GETISPACE:
2579 return snd_pcm_oss_get_space(pcm_oss_file,
2580 cmd == SNDCTL_DSP_GETISPACE ?
2581 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2582 (struct audio_buf_info __user *) arg);
2583 case SNDCTL_DSP_NONBLOCK:
2584 return snd_pcm_oss_nonblock(file);
2585 case SNDCTL_DSP_GETCAPS:
2586 res = snd_pcm_oss_get_caps(pcm_oss_file);
2587 if (res < 0)
2588 return res;
2589 return put_user(res, p);
2590 case SNDCTL_DSP_GETTRIGGER:
2591 res = snd_pcm_oss_get_trigger(pcm_oss_file);
2592 if (res < 0)
2593 return res;
2594 return put_user(res, p);
2595 case SNDCTL_DSP_SETTRIGGER:
2596 if (get_user(res, p))
2597 return -EFAULT;
2598 return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2599 case SNDCTL_DSP_GETIPTR:
2600 case SNDCTL_DSP_GETOPTR:
2601 return snd_pcm_oss_get_ptr(pcm_oss_file,
2602 cmd == SNDCTL_DSP_GETIPTR ?
2603 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2604 (struct count_info __user *) arg);
2605 case SNDCTL_DSP_MAPINBUF:
2606 case SNDCTL_DSP_MAPOUTBUF:
2607 return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2608 cmd == SNDCTL_DSP_MAPINBUF ?
2609 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2610 (struct buffmem_desc __user *) arg);
2611 case SNDCTL_DSP_SETSYNCRO:
2612 /* stop DMA now.. */
2613 return 0;
2614 case SNDCTL_DSP_SETDUPLEX:
2615 if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2616 return 0;
2617 return -EIO;
2618 case SNDCTL_DSP_GETODELAY:
2619 res = snd_pcm_oss_get_odelay(pcm_oss_file);
2620 if (res < 0) {
2621 /* it's for sure, some broken apps don't check for error codes */
2622 put_user(0, p);
2623 return res;
2624 }
2625 return put_user(res, p);
2626 case SNDCTL_DSP_PROFILE:
2627 return 0; /* silently ignore */
2628 default:
2629 snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
2630 }
2631 return -EINVAL;
2632}
2633
2634#ifdef CONFIG_COMPAT
2635/* all compatible */
2636#define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
2637#else
2638#define snd_pcm_oss_ioctl_compat NULL
2639#endif
2640
2641static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2642{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002643 struct snd_pcm_oss_file *pcm_oss_file;
2644 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 pcm_oss_file = file->private_data;
2647 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2648 if (substream == NULL)
2649 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002650 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651#ifndef OSS_DEBUG
2652 return snd_pcm_oss_read1(substream, buf, count);
2653#else
2654 {
2655 ssize_t res = snd_pcm_oss_read1(substream, buf, count);
Takashi Iwai006de2672009-02-05 15:51:04 +01002656 printk(KERN_DEBUG "pcm_oss: read %li bytes "
2657 "(returned %li bytes)\n", (long)count, (long)res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return res;
2659 }
2660#endif
2661}
2662
2663static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2664{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002665 struct snd_pcm_oss_file *pcm_oss_file;
2666 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 long result;
2668
2669 pcm_oss_file = file->private_data;
2670 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2671 if (substream == NULL)
2672 return -ENXIO;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002673 substream->f_flags = file->f_flags & O_NONBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 result = snd_pcm_oss_write1(substream, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002676 printk(KERN_DEBUG "pcm_oss: write %li bytes (wrote %li bytes)\n",
2677 (long)count, (long)result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678#endif
2679 return result;
2680}
2681
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002682static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002684 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002685 if (atomic_read(&substream->mmap_count))
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002686 return runtime->oss.prev_hw_ptr_period !=
2687 get_hw_ptr_period(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 else
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002689 return snd_pcm_playback_avail(runtime) >=
2690 runtime->oss.period_frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002693static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002695 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02002696 if (atomic_read(&substream->mmap_count))
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002697 return runtime->oss.prev_hw_ptr_period !=
2698 get_hw_ptr_period(runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 else
Jaroslav Kyselaf2404062010-01-05 17:19:34 +01002700 return snd_pcm_capture_avail(runtime) >=
2701 runtime->oss.period_frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
2704static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
2705{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002706 struct snd_pcm_oss_file *pcm_oss_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 unsigned int mask;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002708 struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 pcm_oss_file = file->private_data;
2711
2712 psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2713 csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2714
2715 mask = 0;
2716 if (psubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002717 struct snd_pcm_runtime *runtime = psubstream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 poll_wait(file, &runtime->sleep, wait);
2719 snd_pcm_stream_lock_irq(psubstream);
2720 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
2721 (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
2722 snd_pcm_oss_playback_ready(psubstream)))
2723 mask |= POLLOUT | POLLWRNORM;
2724 snd_pcm_stream_unlock_irq(psubstream);
2725 }
2726 if (csubstream != NULL) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002727 struct snd_pcm_runtime *runtime = csubstream->runtime;
2728 snd_pcm_state_t ostate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 poll_wait(file, &runtime->sleep, wait);
2730 snd_pcm_stream_lock_irq(csubstream);
2731 if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
2732 snd_pcm_oss_capture_ready(csubstream))
2733 mask |= POLLIN | POLLRDNORM;
2734 snd_pcm_stream_unlock_irq(csubstream);
2735 if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002736 struct snd_pcm_oss_file ofile;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 memset(&ofile, 0, sizeof(ofile));
2738 ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2739 runtime->oss.trigger = 0;
2740 snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2741 }
2742 }
2743
2744 return mask;
2745}
2746
2747static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2748{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002749 struct snd_pcm_oss_file *pcm_oss_file;
2750 struct snd_pcm_substream *substream = NULL;
2751 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 int err;
2753
2754#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002755 printk(KERN_DEBUG "pcm_oss: mmap begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756#endif
2757 pcm_oss_file = file->private_data;
2758 switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2759 case VM_READ | VM_WRITE:
2760 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2761 if (substream)
2762 break;
2763 /* Fall through */
2764 case VM_READ:
2765 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2766 break;
2767 case VM_WRITE:
2768 substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2769 break;
2770 default:
2771 return -EINVAL;
2772 }
2773 /* set VM_READ access as well to fix memset() routines that do
2774 reads before writes (to improve performance) */
2775 area->vm_flags |= VM_READ;
2776 if (substream == NULL)
2777 return -ENXIO;
2778 runtime = substream->runtime;
2779 if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2780 return -EIO;
2781 if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2782 runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2783 else
2784 return -EIO;
2785
2786 if (runtime->oss.params) {
2787 if ((err = snd_pcm_oss_change_params(substream)) < 0)
2788 return err;
2789 }
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002790#ifdef CONFIG_SND_PCM_OSS_PLUGINS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (runtime->oss.plugin_first != NULL)
2792 return -EIO;
Jaroslav Kysela21a34792006-01-13 09:12:11 +01002793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 if (area->vm_pgoff != 0)
2796 return -EINVAL;
2797
2798 err = snd_pcm_mmap_data(substream, file, area);
2799 if (err < 0)
2800 return err;
2801 runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2802 runtime->silence_threshold = 0;
2803 runtime->silence_size = 0;
2804#ifdef OSS_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +01002805 printk(KERN_DEBUG "pcm_oss: mmap ok, bytes = 0x%x\n",
2806 runtime->oss.mmap_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807#endif
2808 /* In mmap mode we never stop */
2809 runtime->stop_threshold = runtime->boundary;
2810
2811 return 0;
2812}
2813
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002814#ifdef CONFIG_SND_VERBOSE_PROCFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815/*
2816 * /proc interface
2817 */
2818
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002819static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2820 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002822 struct snd_pcm_str *pstr = entry->private_data;
2823 struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002824 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 while (setup) {
2826 snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2827 setup->task_name,
2828 setup->periods,
2829 setup->period_size,
2830 setup->disable ? " disable" : "",
2831 setup->direct ? " direct" : "",
2832 setup->block ? " block" : "",
2833 setup->nonblock ? " non-block" : "",
2834 setup->partialfrag ? " partial-frag" : "",
2835 setup->nosilence ? " no-silence" : "");
2836 setup = setup->next;
2837 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002838 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002841static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002843 struct snd_pcm_oss_setup *setup, *setupn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2846 setup; setup = setupn) {
2847 setupn = setup->next;
2848 kfree(setup->task_name);
2849 kfree(setup);
2850 }
2851 pstr->oss.setup_list = NULL;
2852}
2853
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002854static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2855 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002857 struct snd_pcm_str *pstr = entry->private_data;
Takashi Iwai4f7454a2009-09-08 14:29:58 +02002858 char line[128], str[32], task_name[32];
2859 const char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int idx1;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002861 struct snd_pcm_oss_setup *setup, *setup1, template;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 while (!snd_info_get_line(buffer, line, sizeof(line))) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002864 mutex_lock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 memset(&template, 0, sizeof(template));
2866 ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2867 if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2868 snd_pcm_oss_proc_free_setup_list(pstr);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002869 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 continue;
2871 }
2872 for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
2873 if (!strcmp(setup->task_name, task_name)) {
2874 template = *setup;
2875 break;
2876 }
2877 }
2878 ptr = snd_info_get_str(str, ptr, sizeof(str));
2879 template.periods = simple_strtoul(str, NULL, 10);
2880 ptr = snd_info_get_str(str, ptr, sizeof(str));
2881 template.period_size = simple_strtoul(str, NULL, 10);
2882 for (idx1 = 31; idx1 >= 0; idx1--)
2883 if (template.period_size & (1 << idx1))
2884 break;
2885 for (idx1--; idx1 >= 0; idx1--)
2886 template.period_size &= ~(1 << idx1);
2887 do {
2888 ptr = snd_info_get_str(str, ptr, sizeof(str));
2889 if (!strcmp(str, "disable")) {
2890 template.disable = 1;
2891 } else if (!strcmp(str, "direct")) {
2892 template.direct = 1;
2893 } else if (!strcmp(str, "block")) {
2894 template.block = 1;
2895 } else if (!strcmp(str, "non-block")) {
2896 template.nonblock = 1;
2897 } else if (!strcmp(str, "partial-frag")) {
2898 template.partialfrag = 1;
2899 } else if (!strcmp(str, "no-silence")) {
2900 template.nosilence = 1;
Takashi Iwai10f69f92005-09-08 13:48:34 +02002901 } else if (!strcmp(str, "buggy-ptr")) {
2902 template.buggyptr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904 } while (*str);
2905 if (setup == NULL) {
Takashi Iwai060d77b2006-03-27 16:44:52 +02002906 setup = kmalloc(sizeof(*setup), GFP_KERNEL);
2907 if (! setup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 buffer->error = -ENOMEM;
Jiri Slaby91054592009-03-11 20:11:40 +01002909 mutex_unlock(&pstr->oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002910 return;
2911 }
2912 if (pstr->oss.setup_list == NULL)
2913 pstr->oss.setup_list = setup;
2914 else {
2915 for (setup1 = pstr->oss.setup_list;
2916 setup1->next; setup1 = setup1->next);
2917 setup1->next = setup;
2918 }
2919 template.task_name = kstrdup(task_name, GFP_KERNEL);
2920 if (! template.task_name) {
2921 kfree(setup);
2922 buffer->error = -ENOMEM;
Jiri Slaby91054592009-03-11 20:11:40 +01002923 mutex_unlock(&pstr->oss.setup_mutex);
Takashi Iwai060d77b2006-03-27 16:44:52 +02002924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926 }
Takashi Iwai060d77b2006-03-27 16:44:52 +02002927 *setup = template;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002928 mutex_unlock(&pstr->oss.setup_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
2930}
2931
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002932static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 int stream;
2935 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002936 struct snd_info_entry *entry;
2937 struct snd_pcm_str *pstr = &pcm->streams[stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (pstr->substream_count == 0)
2939 continue;
2940 if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
2941 entry->content = SNDRV_INFO_CONTENT_TEXT;
2942 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 entry->c.text.read = snd_pcm_oss_proc_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 entry->c.text.write = snd_pcm_oss_proc_write;
2945 entry->private_data = pstr;
2946 if (snd_info_register(entry) < 0) {
2947 snd_info_free_entry(entry);
2948 entry = NULL;
2949 }
2950 }
2951 pstr->oss.proc_entry = entry;
2952 }
2953}
2954
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002955static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 int stream;
2958 for (stream = 0; stream < 2; ++stream) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002959 struct snd_pcm_str *pstr = &pcm->streams[stream];
Takashi Iwai746d4a02006-06-23 14:37:59 +02002960 snd_info_free_entry(pstr->oss.proc_entry);
2961 pstr->oss.proc_entry = NULL;
2962 snd_pcm_oss_proc_free_setup_list(pstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
2964}
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002965#else /* !CONFIG_SND_VERBOSE_PROCFS */
Takashi Iwai04f141a2005-12-01 10:43:51 +01002966#define snd_pcm_oss_proc_init(pcm)
2967#define snd_pcm_oss_proc_done(pcm)
Takashi Iwaib7d90a32006-04-25 12:56:04 +02002968#endif /* CONFIG_SND_VERBOSE_PROCFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970/*
2971 * ENTRY functions
2972 */
2973
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002974static const struct file_operations snd_pcm_oss_f_reg =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 .owner = THIS_MODULE,
2977 .read = snd_pcm_oss_read,
2978 .write = snd_pcm_oss_write,
2979 .open = snd_pcm_oss_open,
2980 .release = snd_pcm_oss_release,
2981 .poll = snd_pcm_oss_poll,
2982 .unlocked_ioctl = snd_pcm_oss_ioctl,
2983 .compat_ioctl = snd_pcm_oss_ioctl_compat,
2984 .mmap = snd_pcm_oss_mmap,
2985};
2986
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002987static void register_oss_dsp(struct snd_pcm *pcm, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 char name[128];
2990 sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
2991 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01002992 pcm->card, index, &snd_pcm_oss_f_reg,
Clemens Ladischf87135f2005-11-20 14:06:59 +01002993 pcm, name) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02002994 snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
2995 pcm->card->number, pcm->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997}
2998
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01002999static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001 pcm->oss.reg = 0;
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003002 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 char name[128];
3004 int duplex;
3005 register_oss_dsp(pcm, 0);
3006 duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
3007 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
3008 !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3009 sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3010#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3011 snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3012 pcm->card->number,
3013 name);
3014#endif
3015 pcm->oss.reg++;
3016 pcm->oss.reg_mask |= 1;
3017 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003018 if (adsp_map[pcm->card->number] == (int)pcm->device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 register_oss_dsp(pcm, 1);
3020 pcm->oss.reg++;
3021 pcm->oss.reg_mask |= 2;
3022 }
3023
3024 if (pcm->oss.reg)
3025 snd_pcm_oss_proc_init(pcm);
3026
3027 return 0;
3028}
3029
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003030static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 if (pcm->oss.reg) {
3033 if (pcm->oss.reg_mask & 1) {
3034 pcm->oss.reg_mask &= ~1;
3035 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3036 pcm->card, 0);
3037 }
3038 if (pcm->oss.reg_mask & 2) {
3039 pcm->oss.reg_mask &= ~2;
3040 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3041 pcm->card, 1);
3042 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003043 if (dsp_map[pcm->card->number] == (int)pcm->device) {
Takashi Iwaic4614822006-06-23 14:38:23 +02003044#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3045 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3046#endif
3047 }
3048 pcm->oss.reg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050 return 0;
3051}
3052
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003053static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 snd_pcm_oss_disconnect_minor(pcm);
Takashi Iwaic4614822006-06-23 14:38:23 +02003056 snd_pcm_oss_proc_done(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return 0;
3058}
3059
Takashi Iwai6ac77bc2005-11-17 14:01:49 +01003060static struct snd_pcm_notify snd_pcm_oss_notify =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 .n_register = snd_pcm_oss_register_minor,
3063 .n_disconnect = snd_pcm_oss_disconnect_minor,
3064 .n_unregister = snd_pcm_oss_unregister_minor,
3065};
3066
3067static int __init alsa_pcm_oss_init(void)
3068{
3069 int i;
3070 int err;
3071
3072 /* check device map table */
3073 for (i = 0; i < SNDRV_CARDS; i++) {
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003074 if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003075 snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
3076 i, dsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 dsp_map[i] = 0;
3078 }
Jaroslav Kysela896e6cc2008-08-01 13:36:04 +02003079 if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +02003080 snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
3081 i, adsp_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 adsp_map[i] = 1;
3083 }
3084 }
3085 if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
3086 return err;
3087 return 0;
3088}
3089
3090static void __exit alsa_pcm_oss_exit(void)
3091{
3092 snd_pcm_notify(&snd_pcm_oss_notify, 1);
3093}
3094
3095module_init(alsa_pcm_oss_init)
3096module_exit(alsa_pcm_oss_exit)