pcm_oss.c 85.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Digital Audio (PCM) abstract layer / OSS compatible
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#if 0
#define PLUGIN_DEBUG
#endif
#if 0
#define OSS_DEBUG
#endif

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/vmalloc.h>
33
#include <linux/module.h>
34
#include <linux/math64.h>
35
#include <linux/string.h>
36
#include <linux/compat.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44
#include <sound/core.h>
#include <sound/minors.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include "pcm_plugin.h"
#include <sound/info.h>
#include <linux/soundcard.h>
#include <sound/initval.h>
C
Clemens Ladisch 已提交
45
#include <sound/mixer_oss.h>
L
Linus Torvalds 已提交
46 47 48

#define OSS_ALSAEMULVER		_SIOR ('M', 249, int)

49
static int dsp_map[SNDRV_CARDS];
L
Linus Torvalds 已提交
50
static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
51
static bool nonblock_open = 1;
L
Linus Torvalds 已提交
52

53
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63 64
MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
MODULE_LICENSE("GPL");
module_param_array(dsp_map, int, NULL, 0444);
MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
module_param_array(adsp_map, int, NULL, 0444);
MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
module_param(nonblock_open, bool, 0644);
MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);

65 66 67
static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80

static inline mm_segment_t snd_enter_user(void)
{
	mm_segment_t fs = get_fs();
	set_fs(get_ds());
	return fs;
}

static inline void snd_leave_user(mm_segment_t fs)
{
	set_fs(fs);
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/*
 * helper functions to process hw_params
 */
static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
{
	int changed = 0;
	if (i->min < min) {
		i->min = min;
		i->openmin = openmin;
		changed = 1;
	} else if (i->min == min && !i->openmin && openmin) {
		i->openmin = 1;
		changed = 1;
	}
	if (i->integer) {
		if (i->openmin) {
			i->min++;
			i->openmin = 0;
		}
	}
	if (snd_interval_checkempty(i)) {
		snd_interval_none(i);
		return -EINVAL;
	}
	return changed;
}

static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
{
	int changed = 0;
	if (i->max > max) {
		i->max = max;
		i->openmax = openmax;
		changed = 1;
	} else if (i->max == max && !i->openmax && openmax) {
		i->openmax = 1;
		changed = 1;
	}
	if (i->integer) {
		if (i->openmax) {
			i->max--;
			i->openmax = 0;
		}
	}
	if (snd_interval_checkempty(i)) {
		snd_interval_none(i);
		return -EINVAL;
	}
	return changed;
}

static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
{
	struct snd_interval t;
	t.empty = 0;
	t.min = t.max = val;
	t.openmin = t.openmax = 0;
	t.integer = 1;
	return snd_interval_refine(i, &t);
}

/**
 * snd_pcm_hw_param_value_min
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Return the minimum value for field PAR.
 */
static unsigned int
snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
			   snd_pcm_hw_param_t var, int *dir)
{
	if (hw_is_mask(var)) {
		if (dir)
			*dir = 0;
		return snd_mask_min(hw_param_mask_c(params, var));
	}
	if (hw_is_interval(var)) {
		const struct snd_interval *i = hw_param_interval_c(params, var);
		if (dir)
			*dir = i->openmin;
		return snd_interval_min(i);
	}
	return -EINVAL;
}

/**
 * snd_pcm_hw_param_value_max
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Return the maximum value for field PAR.
 */
static unsigned int
snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
			   snd_pcm_hw_param_t var, int *dir)
{
	if (hw_is_mask(var)) {
		if (dir)
			*dir = 0;
		return snd_mask_max(hw_param_mask_c(params, var));
	}
	if (hw_is_interval(var)) {
		const struct snd_interval *i = hw_param_interval_c(params, var);
		if (dir)
			*dir = - (int) i->openmax;
		return snd_interval_max(i);
	}
	return -EINVAL;
}

static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
				  snd_pcm_hw_param_t var,
				  const struct snd_mask *val)
{
	int changed;
	changed = snd_mask_refine(hw_param_mask(params, var), val);
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}

static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
				 struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var,
				 const struct snd_mask *val)
{
	int changed = _snd_pcm_hw_param_mask(params, var, val);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
		if (err < 0)
			return err;
	}
	return 0;
}

static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var, unsigned int val,
				 int dir)
{
	int changed;
	int open = 0;
	if (dir) {
		if (dir > 0) {
			open = 1;
		} else if (dir < 0) {
			if (val > 0) {
				open = 1;
				val--;
			}
		}
	}
	if (hw_is_mask(var))
		changed = snd_mask_refine_min(hw_param_mask(params, var),
					      val + !!open);
	else if (hw_is_interval(var))
		changed = snd_interval_refine_min(hw_param_interval(params, var),
						  val, open);
	else
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}

/**
 * snd_pcm_hw_param_min
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @val: minimal value
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Inside configuration space defined by PARAMS remove from PAR all 
 * values < VAL. Reduce configuration space accordingly.
 * Return new minimum or -EINVAL if the configuration space is empty
 */
static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
				struct snd_pcm_hw_params *params,
				snd_pcm_hw_param_t var, unsigned int val,
				int *dir)
{
	int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
		if (err < 0)
			return err;
	}
	return snd_pcm_hw_param_value_min(params, var, dir);
}

static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var, unsigned int val,
				 int dir)
{
	int changed;
	int open = 0;
	if (dir) {
		if (dir < 0) {
			open = 1;
		} else if (dir > 0) {
			open = 1;
			val++;
		}
	}
	if (hw_is_mask(var)) {
		if (val == 0 && open) {
			snd_mask_none(hw_param_mask(params, var));
			changed = -EINVAL;
		} else
			changed = snd_mask_refine_max(hw_param_mask(params, var),
						      val - !!open);
	} else if (hw_is_interval(var))
		changed = snd_interval_refine_max(hw_param_interval(params, var),
						  val, open);
	else
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}

/**
 * snd_pcm_hw_param_max
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @val: maximal value
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Inside configuration space defined by PARAMS remove from PAR all 
 *  values >= VAL + 1. Reduce configuration space accordingly.
 *  Return new maximum or -EINVAL if the configuration space is empty
 */
static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
				struct snd_pcm_hw_params *params,
				snd_pcm_hw_param_t var, unsigned int val,
				int *dir)
{
	int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
		if (err < 0)
			return err;
	}
	return snd_pcm_hw_param_value_max(params, var, dir);
}

static int boundary_sub(int a, int adir,
			int b, int bdir,
			int *c, int *cdir)
{
	adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
	bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
	*c = a - b;
	*cdir = adir - bdir;
	if (*cdir == -2) {
		(*c)--;
	} else if (*cdir == 2) {
		(*c)++;
	}
	return 0;
}

static int boundary_lt(unsigned int a, int adir,
		       unsigned int b, int bdir)
{
	if (adir < 0) {
		a--;
		adir = 1;
	} else if (adir > 0)
		adir = 1;
	if (bdir < 0) {
		b--;
		bdir = 1;
	} else if (bdir > 0)
		bdir = 1;
	return a < b || (a == b && adir < bdir);
}

/* Return 1 if min is nearer to best than max */
static int boundary_nearer(int min, int mindir,
			   int best, int bestdir,
			   int max, int maxdir)
{
	int dmin, dmindir;
	int dmax, dmaxdir;
	boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
	boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
	return boundary_lt(dmin, dmindir, dmax, dmaxdir);
}

/**
 * snd_pcm_hw_param_near
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @best: value to set
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Inside configuration space defined by PARAMS set PAR to the available value
 * nearest to VAL. Reduce configuration space accordingly.
 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
 * Return the value found.
  */
static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
				 struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var, unsigned int best,
				 int *dir)
{
	struct snd_pcm_hw_params *save = NULL;
	int v;
	unsigned int saved_min;
	int last = 0;
	int min, max;
	int mindir, maxdir;
	int valdir = dir ? *dir : 0;
	/* FIXME */
	if (best > INT_MAX)
		best = INT_MAX;
	min = max = best;
	mindir = maxdir = valdir;
	if (maxdir > 0)
		maxdir = 0;
	else if (maxdir == 0)
		maxdir = -1;
	else {
		maxdir = 1;
		max--;
	}
	save = kmalloc(sizeof(*save), GFP_KERNEL);
	if (save == NULL)
		return -ENOMEM;
	*save = *params;
	saved_min = min;
	min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
	if (min >= 0) {
		struct snd_pcm_hw_params *params1;
		if (max < 0)
			goto _end;
		if ((unsigned int)min == saved_min && mindir == valdir)
			goto _end;
		params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
		if (params1 == NULL) {
			kfree(save);
			return -ENOMEM;
		}
		*params1 = *save;
		max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
		if (max < 0) {
			kfree(params1);
			goto _end;
		}
		if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
			*params = *params1;
			last = 1;
		}
		kfree(params1);
	} else {
		*params = *save;
		max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
457 458
		if (max < 0) {
			kfree(save);
459
			return max;
460
		}
461 462 463 464 465 466 467 468
		last = 1;
	}
 _end:
 	kfree(save);
	if (last)
		v = snd_pcm_hw_param_last(pcm, params, var, dir);
	else
		v = snd_pcm_hw_param_first(pcm, params, var, dir);
469
	snd_BUG_ON(v < 0);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	return v;
}

static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var, unsigned int val,
				 int dir)
{
	int changed;
	if (hw_is_mask(var)) {
		struct snd_mask *m = hw_param_mask(params, var);
		if (val == 0 && dir < 0) {
			changed = -EINVAL;
			snd_mask_none(m);
		} else {
			if (dir > 0)
				val++;
			else if (dir < 0)
				val--;
			changed = snd_mask_refine_set(hw_param_mask(params, var), val);
		}
	} else if (hw_is_interval(var)) {
		struct snd_interval *i = hw_param_interval(params, var);
		if (val == 0 && dir < 0) {
			changed = -EINVAL;
			snd_interval_none(i);
		} else if (dir == 0)
			changed = snd_interval_refine_set(i, val);
		else {
			struct snd_interval t;
			t.openmin = 1;
			t.openmax = 1;
			t.empty = 0;
			t.integer = 0;
			if (dir < 0) {
				t.min = val - 1;
				t.max = val;
			} else {
				t.min = val;
				t.max = val+1;
			}
			changed = snd_interval_refine(i, &t);
		}
	} else
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}

/**
 * snd_pcm_hw_param_set
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @val: value to set
 * @dir: pointer to the direction (-1,0,1) or NULL
 *
 * Inside configuration space defined by PARAMS remove from PAR all 
 * values != VAL. Reduce configuration space accordingly.
 *  Return VAL or -EINVAL if the configuration space is empty
 */
static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
				struct snd_pcm_hw_params *params,
				snd_pcm_hw_param_t var, unsigned int val,
				int dir)
{
	int changed = _snd_pcm_hw_param_set(params, var, val, dir);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
		if (err < 0)
			return err;
	}
	return snd_pcm_hw_param_value(params, var, NULL);
}

static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
					snd_pcm_hw_param_t var)
{
	int changed;
	changed = snd_interval_setinteger(hw_param_interval(params, var));
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}
	
/*
 * plugin
 */

565
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
566
static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
567
{
568 569
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_pcm_plugin *plugin, *next;
L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578 579 580
	
	plugin = runtime->oss.plugin_first;
	while (plugin) {
		next = plugin->next;
		snd_pcm_plugin_free(plugin);
		plugin = next;
	}
	runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
	return 0;
}

581
static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
L
Linus Torvalds 已提交
582
{
583
	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595
	plugin->next = runtime->oss.plugin_first;
	plugin->prev = NULL;
	if (runtime->oss.plugin_first) {
		runtime->oss.plugin_first->prev = plugin;
		runtime->oss.plugin_first = plugin;
	} else {
		runtime->oss.plugin_last =
		runtime->oss.plugin_first = plugin;
	}
	return 0;
}

596
int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
L
Linus Torvalds 已提交
597
{
598
	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607 608 609
	plugin->next = NULL;
	plugin->prev = runtime->oss.plugin_last;
	if (runtime->oss.plugin_last) {
		runtime->oss.plugin_last->next = plugin;
		runtime->oss.plugin_last = plugin;
	} else {
		runtime->oss.plugin_last =
		runtime->oss.plugin_first = plugin;
	}
	return 0;
}
610
#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
L
Linus Torvalds 已提交
611

612
static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
L
Linus Torvalds 已提交
613
{
614
	struct snd_pcm_runtime *runtime = substream->runtime;
615
	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
616
	long bytes = frames_to_bytes(runtime, frames);
L
Linus Torvalds 已提交
617
	if (buffer_size == runtime->oss.buffer_bytes)
618
		return bytes;
619 620 621 622 623
#if BITS_PER_LONG >= 64
	return runtime->oss.buffer_bytes * bytes / buffer_size;
#else
	{
		u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
624
		return div_u64(bsize, buffer_size);
625 626
	}
#endif
L
Linus Torvalds 已提交
627 628
}

629
static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
L
Linus Torvalds 已提交
630
{
631
	struct snd_pcm_runtime *runtime = substream->runtime;
632
	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
L
Linus Torvalds 已提交
633 634 635 636 637
	if (buffer_size == runtime->oss.buffer_bytes)
		return bytes_to_frames(runtime, bytes);
	return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
}

638 639 640
static inline
snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
{
641
	return runtime->hw_ptr_interrupt;
642 643
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/* define extended formats in the recent OSS versions (if any) */
/* linear formats */
#define AFMT_S32_LE      0x00001000
#define AFMT_S32_BE      0x00002000
#define AFMT_S24_LE      0x00008000
#define AFMT_S24_BE      0x00010000
#define AFMT_S24_PACKED  0x00040000

/* other supported formats */
#define AFMT_FLOAT       0x00004000
#define AFMT_SPDIF_RAW   0x00020000

/* unsupported formats */
#define AFMT_AC3         0x00000400
#define AFMT_VORBIS      0x00000800

C
Clemens Ladisch 已提交
660
static snd_pcm_format_t snd_pcm_oss_format_from(int format)
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671 672
{
	switch (format) {
	case AFMT_MU_LAW:	return SNDRV_PCM_FORMAT_MU_LAW;
	case AFMT_A_LAW:	return SNDRV_PCM_FORMAT_A_LAW;
	case AFMT_IMA_ADPCM:	return SNDRV_PCM_FORMAT_IMA_ADPCM;
	case AFMT_U8:		return SNDRV_PCM_FORMAT_U8;
	case AFMT_S16_LE:	return SNDRV_PCM_FORMAT_S16_LE;
	case AFMT_S16_BE:	return SNDRV_PCM_FORMAT_S16_BE;
	case AFMT_S8:		return SNDRV_PCM_FORMAT_S8;
	case AFMT_U16_LE:	return SNDRV_PCM_FORMAT_U16_LE;
	case AFMT_U16_BE:	return SNDRV_PCM_FORMAT_U16_BE;
	case AFMT_MPEG:		return SNDRV_PCM_FORMAT_MPEG;
673 674 675 676 677 678 679
	case AFMT_S32_LE:	return SNDRV_PCM_FORMAT_S32_LE;
	case AFMT_S32_BE:	return SNDRV_PCM_FORMAT_S32_BE;
	case AFMT_S24_LE:	return SNDRV_PCM_FORMAT_S24_LE;
	case AFMT_S24_BE:	return SNDRV_PCM_FORMAT_S24_BE;
	case AFMT_S24_PACKED:	return SNDRV_PCM_FORMAT_S24_3LE;
	case AFMT_FLOAT:	return SNDRV_PCM_FORMAT_FLOAT;
	case AFMT_SPDIF_RAW:	return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
L
Linus Torvalds 已提交
680 681 682 683
	default:		return SNDRV_PCM_FORMAT_U8;
	}
}

C
Clemens Ladisch 已提交
684
static int snd_pcm_oss_format_to(snd_pcm_format_t format)
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693 694 695 696
{
	switch (format) {
	case SNDRV_PCM_FORMAT_MU_LAW:	return AFMT_MU_LAW;
	case SNDRV_PCM_FORMAT_A_LAW:	return AFMT_A_LAW;
	case SNDRV_PCM_FORMAT_IMA_ADPCM:	return AFMT_IMA_ADPCM;
	case SNDRV_PCM_FORMAT_U8:		return AFMT_U8;
	case SNDRV_PCM_FORMAT_S16_LE:	return AFMT_S16_LE;
	case SNDRV_PCM_FORMAT_S16_BE:	return AFMT_S16_BE;
	case SNDRV_PCM_FORMAT_S8:		return AFMT_S8;
	case SNDRV_PCM_FORMAT_U16_LE:	return AFMT_U16_LE;
	case SNDRV_PCM_FORMAT_U16_BE:	return AFMT_U16_BE;
	case SNDRV_PCM_FORMAT_MPEG:		return AFMT_MPEG;
697 698 699 700 701 702 703
	case SNDRV_PCM_FORMAT_S32_LE:	return AFMT_S32_LE;
	case SNDRV_PCM_FORMAT_S32_BE:	return AFMT_S32_BE;
	case SNDRV_PCM_FORMAT_S24_LE:	return AFMT_S24_LE;
	case SNDRV_PCM_FORMAT_S24_BE:	return AFMT_S24_BE;
	case SNDRV_PCM_FORMAT_S24_3LE:	return AFMT_S24_PACKED;
	case SNDRV_PCM_FORMAT_FLOAT:	return AFMT_FLOAT;
	case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
L
Linus Torvalds 已提交
704 705 706 707
	default:			return -EINVAL;
	}
}

708 709 710
static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream, 
				   struct snd_pcm_hw_params *oss_params,
				   struct snd_pcm_hw_params *slave_params)
L
Linus Torvalds 已提交
711 712 713 714
{
	size_t s;
	size_t oss_buffer_size, oss_period_size, oss_periods;
	size_t min_period_size, max_period_size;
715
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
716 717 718 719 720 721 722
	size_t oss_frame_size;

	oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
			 params_channels(oss_params) / 8;

	oss_buffer_size = snd_pcm_plug_client_size(substream,
						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
723
	oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
724
	if (atomic_read(&substream->mmap_count)) {
L
Linus Torvalds 已提交
725 726 727 728
		if (oss_buffer_size > runtime->oss.mmap_bytes)
			oss_buffer_size = runtime->oss.mmap_bytes;
	}

729 730
	if (substream->oss.setup.period_size > 16)
		oss_period_size = substream->oss.setup.period_size;
L
Linus Torvalds 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	else if (runtime->oss.fragshift) {
		oss_period_size = 1 << runtime->oss.fragshift;
		if (oss_period_size > oss_buffer_size / 2)
			oss_period_size = oss_buffer_size / 2;
	} else {
		int sd;
		size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;

		oss_period_size = oss_buffer_size;
		do {
			oss_period_size /= 2;
		} while (oss_period_size > bytes_per_sec);
		if (runtime->oss.subdivision == 0) {
			sd = 4;
			if (oss_period_size / sd > 4096)
				sd *= 2;
			if (oss_period_size / sd < 4096)
				sd = 1;
		} else
			sd = runtime->oss.subdivision;
		oss_period_size /= sd;
		if (oss_period_size < 16)
			oss_period_size = 16;
	}

	min_period_size = snd_pcm_plug_client_size(substream,
						   snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
	min_period_size *= oss_frame_size;
759
	min_period_size = roundup_pow_of_two(min_period_size);
L
Linus Torvalds 已提交
760 761 762 763 764 765
	if (oss_period_size < min_period_size)
		oss_period_size = min_period_size;

	max_period_size = snd_pcm_plug_client_size(substream,
						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
	max_period_size *= oss_frame_size;
766
	max_period_size = rounddown_pow_of_two(max_period_size);
L
Linus Torvalds 已提交
767 768 769 770 771
	if (oss_period_size > max_period_size)
		oss_period_size = max_period_size;

	oss_periods = oss_buffer_size / oss_period_size;

772 773
	if (substream->oss.setup.periods > 1)
		oss_periods = substream->oss.setup.periods;
L
Linus Torvalds 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

	s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
	if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
		s = runtime->oss.maxfrags;
	if (oss_periods > s)
		oss_periods = s;

	s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
	if (s < 2)
		s = 2;
	if (oss_periods < s)
		oss_periods = s;

	while (oss_period_size * oss_periods > oss_buffer_size)
		oss_period_size /= 2;

790 791
	if (oss_period_size < 16)
		return -EINVAL;
L
Linus Torvalds 已提交
792 793 794 795 796 797
	runtime->oss.period_bytes = oss_period_size;
	runtime->oss.period_frames = 1;
	runtime->oss.periods = oss_periods;
	return 0;
}

798 799
static int choose_rate(struct snd_pcm_substream *substream,
		       struct snd_pcm_hw_params *params, unsigned int best_rate)
L
Linus Torvalds 已提交
800
{
801 802
	struct snd_interval *it;
	struct snd_pcm_hw_params *save;
L
Linus Torvalds 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	unsigned int rate, prev;

	save = kmalloc(sizeof(*save), GFP_KERNEL);
	if (save == NULL)
		return -ENOMEM;
	*save = *params;
	it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);

	/* try multiples of the best rate */
	rate = best_rate;
	for (;;) {
		if (it->max < rate || (it->max == rate && it->openmax))
			break;
		if (it->min < rate || (it->min == rate && !it->openmin)) {
			int ret;
			ret = snd_pcm_hw_param_set(substream, params,
						   SNDRV_PCM_HW_PARAM_RATE,
						   rate, 0);
			if (ret == (int)rate) {
				kfree(save);
				return rate;
			}
			*params = *save;
		}
		prev = rate;
		rate += best_rate;
		if (rate <= prev)
			break;
	}

	/* not found, use the nearest rate */
	kfree(save);
	return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
}

838 839
static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
				     bool trylock)
L
Linus Torvalds 已提交
840
{
841 842 843
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_pcm_hw_params *params, *sparams;
	struct snd_pcm_sw_params *sw_params;
L
Linus Torvalds 已提交
844 845 846 847
	ssize_t oss_buffer_size, oss_period_size;
	size_t oss_frame_size;
	int err;
	int direct;
C
Clemens Ladisch 已提交
848 849
	snd_pcm_format_t format, sformat;
	int n;
850 851
	struct snd_mask sformat_mask;
	struct snd_mask mask;
L
Linus Torvalds 已提交
852

853 854 855 856
	if (trylock) {
		if (!(mutex_trylock(&runtime->oss.params_lock)))
			return -EAGAIN;
	} else if (mutex_lock_interruptible(&runtime->oss.params_lock))
857
		return -EINTR;
858
	sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
L
Linus Torvalds 已提交
859 860 861 862 863 864 865
	params = kmalloc(sizeof(*params), GFP_KERNEL);
	sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
	if (!sw_params || !params || !sparams) {
		err = -ENOMEM;
		goto failure;
	}

866
	if (atomic_read(&substream->mmap_count))
L
Linus Torvalds 已提交
867
		direct = 1;
868 869
	else
		direct = substream->oss.setup.direct;
L
Linus Torvalds 已提交
870 871 872 873 874

	_snd_pcm_hw_params_any(sparams);
	_snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
	_snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
	snd_mask_none(&mask);
875
	if (atomic_read(&substream->mmap_count))
C
Clemens Ladisch 已提交
876
		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
L
Linus Torvalds 已提交
877
	else {
C
Clemens Ladisch 已提交
878
		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
L
Linus Torvalds 已提交
879
		if (!direct)
C
Clemens Ladisch 已提交
880
			snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
L
Linus Torvalds 已提交
881 882 883
	}
	err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
	if (err < 0) {
884
		pcm_dbg(substream->pcm, "No usable accesses\n");
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898
		err = -EINVAL;
		goto failure;
	}
	choose_rate(substream, sparams, runtime->oss.rate);
	snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);

	format = snd_pcm_oss_format_from(runtime->oss.format);

	sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
	if (direct)
		sformat = format;
	else
		sformat = snd_pcm_plug_slave_format(format, &sformat_mask);

C
Clemens Ladisch 已提交
899 900 901 902 903 904
	if ((__force int)sformat < 0 ||
	    !snd_mask_test(&sformat_mask, (__force int)sformat)) {
		for (sformat = (__force snd_pcm_format_t)0;
		     (__force int)sformat <= (__force int)SNDRV_PCM_FORMAT_LAST;
		     sformat = (__force snd_pcm_format_t)((__force int)sformat + 1)) {
			if (snd_mask_test(&sformat_mask, (__force int)sformat) &&
L
Linus Torvalds 已提交
905 906 907
			    snd_pcm_oss_format_to(sformat) >= 0)
				break;
		}
C
Clemens Ladisch 已提交
908
		if ((__force int)sformat > (__force int)SNDRV_PCM_FORMAT_LAST) {
909
			pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
L
Linus Torvalds 已提交
910 911 912 913
			err = -EINVAL;
			goto failure;
		}
	}
C
Clemens Ladisch 已提交
914
	err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
915 916
	if (err < 0)
		goto failure;
L
Linus Torvalds 已提交
917 918 919 920 921 922

	if (direct) {
		memcpy(params, sparams, sizeof(*params));
	} else {
		_snd_pcm_hw_params_any(params);
		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
C
Clemens Ladisch 已提交
923
				      (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
L
Linus Torvalds 已提交
924
		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
C
Clemens Ladisch 已提交
925
				      (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
L
Linus Torvalds 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
				      runtime->oss.channels, 0);
		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
				      runtime->oss.rate, 0);
		pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
			 params_access(params), params_format(params),
			 params_channels(params), params_rate(params));
	}
	pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
		 params_access(sparams), params_format(sparams),
		 params_channels(sparams), params_rate(sparams));

	oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
			 params_channels(params) / 8;

941
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
L
Linus Torvalds 已提交
942 943 944 945 946 947 948
	snd_pcm_oss_plugin_clear(substream);
	if (!direct) {
		/* add necessary plugins */
		snd_pcm_oss_plugin_clear(substream);
		if ((err = snd_pcm_plug_format_plugins(substream,
						       params, 
						       sparams)) < 0) {
949 950
			pcm_dbg(substream->pcm,
				"snd_pcm_plug_format_plugins failed: %i\n", err);
L
Linus Torvalds 已提交
951 952 953 954
			snd_pcm_oss_plugin_clear(substream);
			goto failure;
		}
		if (runtime->oss.plugin_first) {
955
			struct snd_pcm_plugin *plugin;
L
Linus Torvalds 已提交
956
			if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
957 958
				pcm_dbg(substream->pcm,
					"snd_pcm_plugin_build_io failed: %i\n", err);
L
Linus Torvalds 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972
				snd_pcm_oss_plugin_clear(substream);
				goto failure;
			}
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				err = snd_pcm_plugin_append(plugin);
			} else {
				err = snd_pcm_plugin_insert(plugin);
			}
			if (err < 0) {
				snd_pcm_oss_plugin_clear(substream);
				goto failure;
			}
		}
	}
973
#endif
L
Linus Torvalds 已提交
974 975 976 977 978 979 980

	err = snd_pcm_oss_period_size(substream, params, sparams);
	if (err < 0)
		goto failure;

	n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
981 982
	if (err < 0)
		goto failure;
L
Linus Torvalds 已提交
983 984 985

	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
				     runtime->oss.periods, NULL);
986 987
	if (err < 0)
		goto failure;
L
Linus Torvalds 已提交
988 989 990 991

	snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);

	if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
992
		pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
L
Linus Torvalds 已提交
993 994 995 996 997 998 999 1000
		goto failure;
	}

	if (runtime->oss.trigger) {
		sw_params->start_threshold = 1;
	} else {
		sw_params->start_threshold = runtime->boundary;
	}
1001 1002
	if (atomic_read(&substream->mmap_count) ||
	    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007
		sw_params->stop_threshold = runtime->boundary;
	else
		sw_params->stop_threshold = runtime->buffer_size;
	sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
	sw_params->period_step = 1;
1008 1009
	sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
		1 : runtime->period_size;
1010
	if (atomic_read(&substream->mmap_count) ||
1011
	    substream->oss.setup.nosilence) {
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
		sw_params->silence_threshold = 0;
		sw_params->silence_size = 0;
	} else {
		snd_pcm_uframes_t frames;
		frames = runtime->period_size + 16;
		if (frames > runtime->buffer_size)
			frames = runtime->buffer_size;
		sw_params->silence_threshold = frames;
		sw_params->silence_size = frames;
	}

	if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
1024
		pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029
		goto failure;
	}

	runtime->oss.periods = params_periods(sparams);
	oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1030 1031 1032 1033
	if (oss_period_size < 0) {
		err = -EINVAL;
		goto failure;
	}
1034
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039
	if (runtime->oss.plugin_first) {
		err = snd_pcm_plug_alloc(substream, oss_period_size);
		if (err < 0)
			goto failure;
	}
1040
#endif
L
Linus Torvalds 已提交
1041 1042 1043
	oss_period_size *= oss_frame_size;

	oss_buffer_size = oss_period_size * runtime->oss.periods;
1044 1045 1046 1047
	if (oss_buffer_size < 0) {
		err = -EINVAL;
		goto failure;
	}
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

	runtime->oss.period_bytes = oss_period_size;
	runtime->oss.buffer_bytes = oss_buffer_size;

	pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
		 runtime->oss.period_bytes,
		 runtime->oss.buffer_bytes);
	pdprintf("slave: period_size = %i, buffer_size = %i\n",
		 params_period_size(sparams),
		 params_buffer_size(sparams));

	runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
	runtime->oss.channels = params_channels(params);
	runtime->oss.rate = params_rate(params);

	vfree(runtime->oss.buffer);
	runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
1065 1066 1067 1068 1069 1070 1071
	if (!runtime->oss.buffer) {
		err = -ENOMEM;
		goto failure;
	}

	runtime->oss.params = 0;
	runtime->oss.prepare = 1;
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	runtime->oss.buffer_used = 0;
	if (runtime->dma_area)
		snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));

	runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);

	err = 0;
failure:
	kfree(sw_params);
	kfree(params);
	kfree(sparams);
1083
	mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1084 1085 1086
	return err;
}

1087
static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
L
Linus Torvalds 已提交
1088 1089
{
	int idx, err;
1090
	struct snd_pcm_substream *asubstream = NULL, *substream;
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098

	for (idx = 0; idx < 2; idx++) {
		substream = pcm_oss_file->streams[idx];
		if (substream == NULL)
			continue;
		if (asubstream == NULL)
			asubstream = substream;
		if (substream->runtime->oss.params) {
1099
			err = snd_pcm_oss_change_params(substream, false);
L
Linus Torvalds 已提交
1100 1101 1102 1103
			if (err < 0)
				return err;
		}
	}
1104 1105
	if (!asubstream)
		return -EIO;
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110
	if (r_substream)
		*r_substream = asubstream;
	return 0;
}

1111
static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1112 1113
{
	int err;
1114
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1115 1116 1117

	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
	if (err < 0) {
1118 1119
		pcm_dbg(substream->pcm,
			"snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
L
Linus Torvalds 已提交
1120 1121 1122
		return err;
	}
	runtime->oss.prepare = 0;
1123
	runtime->oss.prev_hw_ptr_period = 0;
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129
	runtime->oss.period_ptr = 0;
	runtime->oss.buffer_used = 0;

	return 0;
}

1130
static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1131
{
1132
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138
	int err;

	if (substream == NULL)
		return 0;
	runtime = substream->runtime;
	if (runtime->oss.params) {
1139
		err = snd_pcm_oss_change_params(substream, false);
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
		if (err < 0)
			return err;
	}
	if (runtime->oss.prepare) {
		err = snd_pcm_oss_prepare(substream);
		if (err < 0)
			return err;
	}
	return 0;
}

1151
static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
L
Linus Torvalds 已提交
1152
{
1153
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	snd_pcm_uframes_t frames;
	int err = 0;

	while (1) {
		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
		if (err < 0)
			break;
		runtime = substream->runtime;
		if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
			break;
		/* in case of overrun, skip whole periods like OSS/Linux driver does */
		/* until avail(delay) <= buffer_size */
		frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
		frames /= runtime->period_size;
		frames *= runtime->period_size;
		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
		if (err < 0)
			break;
	}
	return err;
}

1176
snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
L
Linus Torvalds 已提交
1177
{
1178
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183
	int ret;
	while (1) {
		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
#ifdef OSS_DEBUG
1184 1185 1186 1187
			pcm_dbg(substream->pcm,
				"pcm_oss: write: recovering from %s\n",
				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
				"XRUN" : "SUSPEND");
L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193 1194 1195
#endif
			ret = snd_pcm_oss_prepare(substream);
			if (ret < 0)
				break;
		}
		if (in_kernel) {
			mm_segment_t fs;
			fs = snd_enter_user();
C
Clemens Ladisch 已提交
1196
			ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
L
Linus Torvalds 已提交
1197 1198
			snd_leave_user(fs);
		} else {
C
Clemens Ladisch 已提交
1199
			ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
		}
		if (ret != -EPIPE && ret != -ESTRPIPE)
			break;
		/* test, if we can't store new data, because the stream */
		/* has not been started */
		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
			return -EAGAIN;
	}
	return ret;
}

1211
snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
L
Linus Torvalds 已提交
1212
{
1213
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219
	snd_pcm_sframes_t delay;
	int ret;
	while (1) {
		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
#ifdef OSS_DEBUG
1220 1221 1222 1223
			pcm_dbg(substream->pcm,
				"pcm_oss: read: recovering from %s\n",
				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
				"XRUN" : "SUSPEND");
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
#endif
			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
			if (ret < 0)
				break;
		} else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
			ret = snd_pcm_oss_prepare(substream);
			if (ret < 0)
				break;
		}
		ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
		if (ret < 0)
			break;
		if (in_kernel) {
			mm_segment_t fs;
			fs = snd_enter_user();
C
Clemens Ladisch 已提交
1239
			ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
L
Linus Torvalds 已提交
1240 1241
			snd_leave_user(fs);
		} else {
C
Clemens Ladisch 已提交
1242
			ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
		}
		if (ret == -EPIPE) {
			if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
				ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
				if (ret < 0)
					break;
			}
			continue;
		}
		if (ret != -ESTRPIPE)
			break;
	}
	return ret;
}

1258
snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
L
Linus Torvalds 已提交
1259
{
1260
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265
	int ret;
	while (1) {
		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
#ifdef OSS_DEBUG
1266 1267 1268 1269
			pcm_dbg(substream->pcm,
				"pcm_oss: writev: recovering from %s\n",
				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
				"XRUN" : "SUSPEND");
L
Linus Torvalds 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
#endif
			ret = snd_pcm_oss_prepare(substream);
			if (ret < 0)
				break;
		}
		if (in_kernel) {
			mm_segment_t fs;
			fs = snd_enter_user();
			ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
			snd_leave_user(fs);
		} else {
			ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
		}
		if (ret != -EPIPE && ret != -ESTRPIPE)
			break;

		/* test, if we can't store new data, because the stream */
		/* has not been started */
		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
			return -EAGAIN;
	}
	return ret;
}
	
1294
snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
L
Linus Torvalds 已提交
1295
{
1296
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301
	int ret;
	while (1) {
		if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
		    runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
#ifdef OSS_DEBUG
1302 1303 1304 1305
			pcm_dbg(substream->pcm,
				"pcm_oss: readv: recovering from %s\n",
				runtime->status->state == SNDRV_PCM_STATE_XRUN ?
				"XRUN" : "SUSPEND");
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
#endif
			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
			if (ret < 0)
				break;
		} else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
			ret = snd_pcm_oss_prepare(substream);
			if (ret < 0)
				break;
		}
		if (in_kernel) {
			mm_segment_t fs;
			fs = snd_enter_user();
			ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
			snd_leave_user(fs);
		} else {
			ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
		}
		if (ret != -EPIPE && ret != -ESTRPIPE)
			break;
	}
	return ret;
}

1329
static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
L
Linus Torvalds 已提交
1330
{
1331
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1332
	snd_pcm_sframes_t frames, frames1;
1333
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
L
Linus Torvalds 已提交
1334
	if (runtime->oss.plugin_first) {
1335
		struct snd_pcm_plugin_channel *channels;
L
Linus Torvalds 已提交
1336 1337
		size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
		if (!in_kernel) {
C
Clemens Ladisch 已提交
1338
			if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
				return -EFAULT;
			buf = runtime->oss.buffer;
		}
		frames = bytes / oss_frame_bytes;
		frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
		if (frames1 < 0)
			return frames1;
		frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
		if (frames1 <= 0)
			return frames1;
		bytes = frames1 * oss_frame_bytes;
1350 1351 1352
	} else
#endif
	{
L
Linus Torvalds 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361
		frames = bytes_to_frames(runtime, bytes);
		frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
		if (frames1 <= 0)
			return frames1;
		bytes = frames_to_bytes(runtime, frames1);
	}
	return bytes;
}

1362
static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
L
Linus Torvalds 已提交
1363 1364 1365
{
	size_t xfer = 0;
	ssize_t tmp;
1366
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1367

1368
	if (atomic_read(&substream->mmap_count))
L
Linus Torvalds 已提交
1369 1370 1371 1372
		return -ENXIO;

	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
		return tmp;
1373
	mutex_lock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1374 1375 1376 1377 1378 1379
	while (bytes > 0) {
		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
			tmp = bytes;
			if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
				tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
			if (tmp > 0) {
1380 1381 1382 1383
				if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
					tmp = -EFAULT;
					goto err;
				}
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388
			}
			runtime->oss.buffer_used += tmp;
			buf += tmp;
			bytes -= tmp;
			xfer += tmp;
1389
			if (substream->oss.setup.partialfrag ||
L
Linus Torvalds 已提交
1390 1391 1392 1393
			    runtime->oss.buffer_used == runtime->oss.period_bytes) {
				tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr, 
							 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
				if (tmp <= 0)
1394
					goto err;
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400
				runtime->oss.bytes += tmp;
				runtime->oss.period_ptr += tmp;
				runtime->oss.period_ptr %= runtime->oss.period_bytes;
				if (runtime->oss.period_ptr == 0 ||
				    runtime->oss.period_ptr == runtime->oss.buffer_used)
					runtime->oss.buffer_used = 0;
1401 1402 1403 1404
				else if ((substream->f_flags & O_NONBLOCK) != 0) {
					tmp = -EAGAIN;
					goto err;
				}
L
Linus Torvalds 已提交
1405 1406
			}
		} else {
1407 1408 1409
			tmp = snd_pcm_oss_write2(substream,
						 (const char __force *)buf,
						 runtime->oss.period_bytes, 0);
L
Linus Torvalds 已提交
1410
			if (tmp <= 0)
1411
				goto err;
L
Linus Torvalds 已提交
1412 1413 1414 1415
			runtime->oss.bytes += tmp;
			buf += tmp;
			bytes -= tmp;
			xfer += tmp;
1416
			if ((substream->f_flags & O_NONBLOCK) != 0 &&
L
Linus Torvalds 已提交
1417 1418 1419 1420
			    tmp != runtime->oss.period_bytes)
				break;
		}
	}
1421
	mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1422
	return xfer;
1423 1424 1425 1426

 err:
	mutex_unlock(&runtime->oss.params_lock);
	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
L
Linus Torvalds 已提交
1427 1428
}

1429
static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
L
Linus Torvalds 已提交
1430
{
1431
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1432
	snd_pcm_sframes_t frames, frames1;
1433
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
C
Clemens Ladisch 已提交
1434
	char __user *final_dst = (char __force __user *)buf;
L
Linus Torvalds 已提交
1435
	if (runtime->oss.plugin_first) {
1436
		struct snd_pcm_plugin_channel *channels;
L
Linus Torvalds 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
		if (!in_kernel)
			buf = runtime->oss.buffer;
		frames = bytes / oss_frame_bytes;
		frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
		if (frames1 < 0)
			return frames1;
		frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
		if (frames1 <= 0)
			return frames1;
		bytes = frames1 * oss_frame_bytes;
		if (!in_kernel && copy_to_user(final_dst, buf, bytes))
			return -EFAULT;
1450 1451 1452
	} else
#endif
	{
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461
		frames = bytes_to_frames(runtime, bytes);
		frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
		if (frames1 <= 0)
			return frames1;
		bytes = frames_to_bytes(runtime, frames1);
	}
	return bytes;
}

1462
static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
L
Linus Torvalds 已提交
1463 1464 1465
{
	size_t xfer = 0;
	ssize_t tmp;
1466
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1467

1468
	if (atomic_read(&substream->mmap_count))
L
Linus Torvalds 已提交
1469 1470 1471 1472
		return -ENXIO;

	if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
		return tmp;
1473
	mutex_lock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478
	while (bytes > 0) {
		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
			if (runtime->oss.buffer_used == 0) {
				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
				if (tmp <= 0)
1479
					goto err;
L
Linus Torvalds 已提交
1480 1481 1482 1483 1484 1485 1486
				runtime->oss.bytes += tmp;
				runtime->oss.period_ptr = tmp;
				runtime->oss.buffer_used = tmp;
			}
			tmp = bytes;
			if ((size_t) tmp > runtime->oss.buffer_used)
				tmp = runtime->oss.buffer_used;
1487 1488 1489 1490
			if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
				tmp = -EFAULT;
				goto err;
			}
L
Linus Torvalds 已提交
1491 1492 1493 1494 1495
			buf += tmp;
			bytes -= tmp;
			xfer += tmp;
			runtime->oss.buffer_used -= tmp;
		} else {
1496 1497
			tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
						runtime->oss.period_bytes, 0);
L
Linus Torvalds 已提交
1498
			if (tmp <= 0)
1499
				goto err;
L
Linus Torvalds 已提交
1500 1501 1502 1503 1504 1505
			runtime->oss.bytes += tmp;
			buf += tmp;
			bytes -= tmp;
			xfer += tmp;
		}
	}
1506
	mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1507
	return xfer;
1508 1509 1510 1511

 err:
	mutex_unlock(&runtime->oss.params_lock);
	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
L
Linus Torvalds 已提交
1512 1513
}

1514
static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1515
{
1516
	struct snd_pcm_substream *substream;
1517 1518
	struct snd_pcm_runtime *runtime;
	int i;
L
Linus Torvalds 已提交
1519

1520 1521 1522 1523 1524
	for (i = 0; i < 2; i++) { 
		substream = pcm_oss_file->streams[i];
		if (!substream)
			continue;
		runtime = substream->runtime;
T
Takashi Iwai 已提交
1525
		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1526 1527 1528 1529
		runtime->oss.prepare = 1;
		runtime->oss.buffer_used = 0;
		runtime->oss.prev_hw_ptr_period = 0;
		runtime->oss.period_ptr = 0;
L
Linus Torvalds 已提交
1530 1531 1532 1533
	}
	return 0;
}

1534
static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1535
{
1536
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542
	int err;

	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	if (substream != NULL) {
		if ((err = snd_pcm_oss_make_ready(substream)) < 0)
			return err;
T
Takashi Iwai 已提交
1543
		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549
	}
	/* note: all errors from the start action are ignored */
	/* OSS apps do not know, how to handle them */
	return 0;
}

1550
static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
L
Linus Torvalds 已提交
1551
{
1552
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1553
	ssize_t result = 0;
C
Clemens Ladisch 已提交
1554
	snd_pcm_state_t state;
L
Linus Torvalds 已提交
1555 1556 1557 1558 1559 1560 1561
	long res;
	wait_queue_t wait;

	runtime = substream->runtime;
	init_waitqueue_entry(&wait, current);
	add_wait_queue(&runtime->sleep, &wait);
#ifdef OSS_DEBUG
1562
	pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
L
Linus Torvalds 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
#endif
	while (1) {
		result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
		if (result > 0) {
			runtime->oss.buffer_used = 0;
			result = 0;
			break;
		}
		if (result != 0 && result != -EAGAIN)
			break;
		result = 0;
		set_current_state(TASK_INTERRUPTIBLE);
		snd_pcm_stream_lock_irq(substream);
C
Clemens Ladisch 已提交
1576
		state = runtime->status->state;
L
Linus Torvalds 已提交
1577
		snd_pcm_stream_unlock_irq(substream);
C
Clemens Ladisch 已提交
1578
		if (state != SNDRV_PCM_STATE_RUNNING) {
L
Linus Torvalds 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587
			set_current_state(TASK_RUNNING);
			break;
		}
		res = schedule_timeout(10 * HZ);
		if (signal_pending(current)) {
			result = -ERESTARTSYS;
			break;
		}
		if (res == 0) {
1588 1589
			pcm_err(substream->pcm,
				"OSS sync error - DMA timeout\n");
L
Linus Torvalds 已提交
1590 1591 1592 1593 1594 1595 1596 1597
			result = -EIO;
			break;
		}
	}
	remove_wait_queue(&runtime->sleep, &wait);
	return result;
}

1598
static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1599 1600 1601
{
	int err = 0;
	unsigned int saved_f_flags;
1602 1603
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610
	snd_pcm_format_t format;
	unsigned long width;
	size_t size;

	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	if (substream != NULL) {
		runtime = substream->runtime;
1611
		if (atomic_read(&substream->mmap_count))
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616
			goto __direct;
		if ((err = snd_pcm_oss_make_ready(substream)) < 0)
			return err;
		format = snd_pcm_oss_format_from(runtime->oss.format);
		width = snd_pcm_format_physical_width(format);
1617
		mutex_lock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1618 1619
		if (runtime->oss.buffer_used > 0) {
#ifdef OSS_DEBUG
1620
			pcm_dbg(substream->pcm, "sync: buffer_used\n");
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625 1626
#endif
			size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
			snd_pcm_format_set_silence(format,
						   runtime->oss.buffer + runtime->oss.buffer_used,
						   size);
			err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1627 1628
			if (err < 0) {
				mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1629
				return err;
1630
			}
L
Linus Torvalds 已提交
1631 1632
		} else if (runtime->oss.period_ptr > 0) {
#ifdef OSS_DEBUG
1633
			pcm_dbg(substream->pcm, "sync: period_ptr\n");
L
Linus Torvalds 已提交
1634 1635 1636 1637 1638 1639
#endif
			size = runtime->oss.period_bytes - runtime->oss.period_ptr;
			snd_pcm_format_set_silence(format,
						   runtime->oss.buffer,
						   size * 8 / width);
			err = snd_pcm_oss_sync1(substream, size);
1640 1641
			if (err < 0) {
				mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1642
				return err;
1643
			}
L
Linus Torvalds 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
		}
		/*
		 * The ALSA's period might be a bit large than OSS one.
		 * Fill the remain portion of ALSA period with zeros.
		 */
		size = runtime->control->appl_ptr % runtime->period_size;
		if (size > 0) {
			size = runtime->period_size - size;
			if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
				size = (runtime->frame_bits * size) / 8;
				while (size > 0) {
					mm_segment_t fs;
					size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
					size -= size1;
					size1 *= 8;
					size1 /= runtime->sample_bits;
					snd_pcm_format_set_silence(runtime->format,
								   runtime->oss.buffer,
								   size1);
T
Takashi Iwai 已提交
1663
					size1 /= runtime->channels; /* frames */
L
Linus Torvalds 已提交
1664
					fs = snd_enter_user();
C
Clemens Ladisch 已提交
1665
					snd_pcm_lib_write(substream, (void __force __user *)runtime->oss.buffer, size1);
L
Linus Torvalds 已提交
1666 1667 1668 1669 1670 1671 1672 1673
					snd_leave_user(fs);
				}
			} else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
				void __user *buffers[runtime->channels];
				memset(buffers, 0, runtime->channels * sizeof(void *));
				snd_pcm_lib_writev(substream, buffers, size);
			}
		}
1674
		mutex_unlock(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
1675 1676 1677 1678
		/*
		 * finish sync: drain the buffer
		 */
	      __direct:
1679 1680
		saved_f_flags = substream->f_flags;
		substream->f_flags &= ~O_NONBLOCK;
T
Takashi Iwai 已提交
1681
		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1682
		substream->f_flags = saved_f_flags;
L
Linus Torvalds 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
		if (err < 0)
			return err;
		runtime->oss.prepare = 1;
	}

	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
	if (substream != NULL) {
		if ((err = snd_pcm_oss_make_ready(substream)) < 0)
			return err;
		runtime = substream->runtime;
T
Takashi Iwai 已提交
1693
		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701
		if (err < 0)
			return err;
		runtime->oss.buffer_used = 0;
		runtime->oss.prepare = 1;
	}
	return 0;
}

1702
static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
L
Linus Torvalds 已提交
1703 1704 1705 1706
{
	int idx;

	for (idx = 1; idx >= 0; --idx) {
1707 1708
		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
		struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
		if (substream == NULL)
			continue;
		runtime = substream->runtime;
		if (rate < 1000)
			rate = 1000;
		else if (rate > 192000)
			rate = 192000;
		if (runtime->oss.rate != rate) {
			runtime->oss.params = 1;
			runtime->oss.rate = rate;
		}
	}
	return snd_pcm_oss_get_rate(pcm_oss_file);
}

1724
static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1725
{
1726
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1727 1728 1729 1730 1731 1732 1733
	int err;
	
	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
		return err;
	return substream->runtime->oss.rate;
}

1734
static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
L
Linus Torvalds 已提交
1735 1736 1737 1738 1739 1740 1741
{
	int idx;
	if (channels < 1)
		channels = 1;
	if (channels > 128)
		return -EINVAL;
	for (idx = 1; idx >= 0; --idx) {
1742 1743
		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
		struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
		if (substream == NULL)
			continue;
		runtime = substream->runtime;
		if (runtime->oss.channels != channels) {
			runtime->oss.params = 1;
			runtime->oss.channels = channels;
		}
	}
	return snd_pcm_oss_get_channels(pcm_oss_file);
}

1755
static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1756
{
1757
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1758 1759 1760 1761 1762 1763 1764
	int err;
	
	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
		return err;
	return substream->runtime->oss.channels;
}

1765
static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1766
{
1767
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774
	int err;
	
	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
		return err;
	return substream->runtime->oss.period_bytes;
}

1775
static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1776
{
1777
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1778 1779
	int err;
	int direct;
1780
	struct snd_pcm_hw_params *params;
L
Linus Torvalds 已提交
1781
	unsigned int formats = 0;
1782
	struct snd_mask format_mask;
L
Linus Torvalds 已提交
1783 1784 1785 1786
	int fmt;

	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
		return err;
1787
	if (atomic_read(&substream->mmap_count))
L
Linus Torvalds 已提交
1788
		direct = 1;
1789 1790
	else
		direct = substream->oss.setup.direct;
L
Linus Torvalds 已提交
1791 1792 1793 1794
	if (!direct)
		return AFMT_MU_LAW | AFMT_U8 |
		       AFMT_S16_LE | AFMT_S16_BE |
		       AFMT_S8 | AFMT_U16_LE |
1795 1796
		       AFMT_U16_BE |
			AFMT_S32_LE | AFMT_S32_BE |
1797
			AFMT_S24_LE | AFMT_S24_BE |
1798
			AFMT_S24_PACKED;
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803 1804 1805
	params = kmalloc(sizeof(*params), GFP_KERNEL);
	if (!params)
		return -ENOMEM;
	_snd_pcm_hw_params_any(params);
	err = snd_pcm_hw_refine(substream, params);
	format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 
	kfree(params);
1806 1807
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
	for (fmt = 0; fmt < 32; ++fmt) {
		if (snd_mask_test(&format_mask, fmt)) {
			int f = snd_pcm_oss_format_to(fmt);
			if (f >= 0)
				formats |= f;
		}
	}
	return formats;
}

1818
static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823
{
	int formats, idx;
	
	if (format != AFMT_QUERY) {
		formats = snd_pcm_oss_get_formats(pcm_oss_file);
1824 1825
		if (formats < 0)
			return formats;
L
Linus Torvalds 已提交
1826 1827 1828
		if (!(formats & format))
			format = AFMT_U8;
		for (idx = 1; idx >= 0; --idx) {
1829 1830
			struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
			struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
			if (substream == NULL)
				continue;
			runtime = substream->runtime;
			if (runtime->oss.format != format) {
				runtime->oss.params = 1;
				runtime->oss.format = format;
			}
		}
	}
	return snd_pcm_oss_get_format(pcm_oss_file);
}

1843
static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1844
{
1845
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852
	int err;
	
	if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
		return err;
	return substream->runtime->oss.format;
}

1853
static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
L
Linus Torvalds 已提交
1854
{
1855
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875

	if (substream == NULL)
		return 0;
	runtime = substream->runtime;
	if (subdivide == 0) {
		subdivide = runtime->oss.subdivision;
		if (subdivide == 0)
			subdivide = 1;
		return subdivide;
	}
	if (runtime->oss.subdivision || runtime->oss.fragshift)
		return -EINVAL;
	if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
	    subdivide != 8 && subdivide != 16)
		return -EINVAL;
	runtime->oss.subdivision = subdivide;
	runtime->oss.params = 1;
	return subdivide;
}

1876
static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
L
Linus Torvalds 已提交
1877 1878 1879 1880
{
	int err = -EINVAL, idx;

	for (idx = 1; idx >= 0; --idx) {
1881
		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886 1887 1888 1889
		if (substream == NULL)
			continue;
		if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
			return err;
	}
	return err;
}

1890
static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
L
Linus Torvalds 已提交
1891
{
1892
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

	if (substream == NULL)
		return 0;
	runtime = substream->runtime;
	if (runtime->oss.subdivision || runtime->oss.fragshift)
		return -EINVAL;
	runtime->oss.fragshift = val & 0xffff;
	runtime->oss.maxfrags = (val >> 16) & 0xffff;
	if (runtime->oss.fragshift < 4)		/* < 16 */
		runtime->oss.fragshift = 4;
	if (runtime->oss.maxfrags < 2)
		runtime->oss.maxfrags = 2;
	runtime->oss.params = 1;
	return 0;
}

1909
static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
L
Linus Torvalds 已提交
1910 1911 1912 1913
{
	int err = -EINVAL, idx;

	for (idx = 1; idx >= 0; --idx) {
1914
		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
		if (substream == NULL)
			continue;
		if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
			return err;
	}
	return err;
}

static int snd_pcm_oss_nonblock(struct file * file)
{
J
Jonathan Corbet 已提交
1925
	spin_lock(&file->f_lock);
L
Linus Torvalds 已提交
1926
	file->f_flags |= O_NONBLOCK;
J
Jonathan Corbet 已提交
1927
	spin_unlock(&file->f_lock);
L
Linus Torvalds 已提交
1928 1929 1930
	return 0;
}

1931
static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
{

	if (substream == NULL) {
		res &= ~DSP_CAP_DUPLEX;
		return res;
	}
#ifdef DSP_CAP_MULTI
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		if (substream->pstr->substream_count > 1)
			res |= DSP_CAP_MULTI;
#endif
	/* DSP_CAP_REALTIME is set all times: */
	/* all ALSA drivers can return actual pointer in ring buffer */
#if defined(DSP_CAP_REALTIME) && 0
	{
1947
		struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1948 1949 1950 1951 1952 1953 1954
		if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
			res &= ~DSP_CAP_REALTIME;
	}
#endif
	return res;
}

1955
static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960
{
	int result, idx;
	
	result = DSP_CAP_TRIGGER | DSP_CAP_MMAP	| DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
	for (idx = 0; idx < 2; idx++) {
1961
		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
L
Linus Torvalds 已提交
1962 1963 1964 1965 1966 1967
		result = snd_pcm_oss_get_caps1(substream, result);
	}
	result |= 0x0001;	/* revision - same as SB AWE 64 */
	return result;
}

1968 1969
static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
				      snd_pcm_uframes_t hw_ptr)
L
Linus Torvalds 已提交
1970
{
1971
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1972 1973 1974 1975 1976 1977
	snd_pcm_uframes_t appl_ptr;
	appl_ptr = hw_ptr + runtime->buffer_size;
	appl_ptr %= runtime->boundary;
	runtime->control->appl_ptr = appl_ptr;
}

1978
static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
L
Linus Torvalds 已提交
1979
{
1980 1981
	struct snd_pcm_runtime *runtime;
	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
L
Linus Torvalds 已提交
1982 1983 1984
	int err, cmd;

#ifdef OSS_DEBUG
1985
	pcm_dbg(substream->pcm, "pcm_oss: trigger = 0x%x\n", trigger);
L
Linus Torvalds 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
#endif
	
	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];

	if (psubstream) {
		if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
			return err;
	}
	if (csubstream) {
		if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
			return err;
	}
      	if (psubstream) {
      		runtime = psubstream->runtime;
		if (trigger & PCM_ENABLE_OUTPUT) {
			if (runtime->oss.trigger)
				goto _skip1;
2004
			if (atomic_read(&psubstream->mmap_count))
2005 2006
				snd_pcm_oss_simulate_fill(psubstream,
						get_hw_ptr_period(runtime));
L
Linus Torvalds 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
			runtime->oss.trigger = 1;
			runtime->start_threshold = 1;
			cmd = SNDRV_PCM_IOCTL_START;
		} else {
			if (!runtime->oss.trigger)
				goto _skip1;
			runtime->oss.trigger = 0;
			runtime->start_threshold = runtime->boundary;
			cmd = SNDRV_PCM_IOCTL_DROP;
			runtime->oss.prepare = 1;
		}
T
Takashi Iwai 已提交
2018
		err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
L
Linus Torvalds 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
		if (err < 0)
			return err;
	}
 _skip1:
	if (csubstream) {
      		runtime = csubstream->runtime;
		if (trigger & PCM_ENABLE_INPUT) {
			if (runtime->oss.trigger)
				goto _skip2;
			runtime->oss.trigger = 1;
			runtime->start_threshold = 1;
			cmd = SNDRV_PCM_IOCTL_START;
		} else {
			if (!runtime->oss.trigger)
				goto _skip2;
			runtime->oss.trigger = 0;
			runtime->start_threshold = runtime->boundary;
			cmd = SNDRV_PCM_IOCTL_DROP;
			runtime->oss.prepare = 1;
		}
T
Takashi Iwai 已提交
2039
		err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
L
Linus Torvalds 已提交
2040 2041 2042 2043 2044 2045 2046
		if (err < 0)
			return err;
	}
 _skip2:
	return 0;
}

2047
static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
2048
{
2049
	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
L
Linus Torvalds 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
	int result = 0;

	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
	if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
		result |= PCM_ENABLE_OUTPUT;
	if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
		result |= PCM_ENABLE_INPUT;
	return result;
}

2061
static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
2062
{
2063 2064
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	snd_pcm_sframes_t delay;
	int err;

	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	if (substream == NULL)
		return -EINVAL;
	if ((err = snd_pcm_oss_make_ready(substream)) < 0)
		return err;
	runtime = substream->runtime;
	if (runtime->oss.params || runtime->oss.prepare)
		return 0;
T
Takashi Iwai 已提交
2076
	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
L
Linus Torvalds 已提交
2077 2078 2079 2080 2081 2082 2083
	if (err == -EPIPE)
		delay = 0;	/* hack for broken OSS applications */
	else if (err < 0)
		return err;
	return snd_pcm_oss_bytes(substream, delay);
}

2084
static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
L
Linus Torvalds 已提交
2085
{	
2086 2087
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
	snd_pcm_sframes_t delay;
	int fixup;
	struct count_info info;
	int err;

	if (_info == NULL)
		return -EFAULT;
	substream = pcm_oss_file->streams[stream];
	if (substream == NULL)
		return -EINVAL;
	if ((err = snd_pcm_oss_make_ready(substream)) < 0)
		return err;
	runtime = substream->runtime;
	if (runtime->oss.params || runtime->oss.prepare) {
		memset(&info, 0, sizeof(info));
		if (copy_to_user(_info, &info, sizeof(info)))
			return -EFAULT;
		return 0;
	}
	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
		if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
			err = 0;
			delay = 0;
			fixup = 0;
		} else {
			fixup = runtime->oss.buffer_used;
		}
	} else {
		err = snd_pcm_oss_capture_position_fixup(substream, &delay);
		fixup = -runtime->oss.buffer_used;
	}
	if (err < 0)
		return err;
	info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2123
	if (atomic_read(&substream->mmap_count)) {
L
Linus Torvalds 已提交
2124
		snd_pcm_sframes_t n;
2125 2126
		delay = get_hw_ptr_period(runtime);
		n = delay - runtime->oss.prev_hw_ptr_period;
L
Linus Torvalds 已提交
2127 2128 2129
		if (n < 0)
			n += runtime->boundary;
		info.blocks = n / runtime->period_size;
2130
		runtime->oss.prev_hw_ptr_period = delay;
L
Linus Torvalds 已提交
2131 2132 2133 2134
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
			snd_pcm_oss_simulate_fill(substream, delay);
		info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
	} else {
2135
		delay = snd_pcm_oss_bytes(substream, delay);
2136
		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2137
			if (substream->oss.setup.buggyptr)
2138 2139 2140
				info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
			else
				info.blocks = (delay + fixup) / runtime->oss.period_bytes;
L
Linus Torvalds 已提交
2141
			info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2142
		} else {
2143 2144
			delay += fixup;
			info.blocks = delay / runtime->oss.period_bytes;
L
Linus Torvalds 已提交
2145
			info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2146
		}
L
Linus Torvalds 已提交
2147 2148 2149 2150 2151 2152
	}
	if (copy_to_user(_info, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

2153
static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
L
Linus Torvalds 已提交
2154
{
2155 2156
	struct snd_pcm_substream *substream;
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
	snd_pcm_sframes_t avail;
	int fixup;
	struct audio_buf_info info;
	int err;

	if (_info == NULL)
		return -EFAULT;
	substream = pcm_oss_file->streams[stream];
	if (substream == NULL)
		return -EINVAL;
	runtime = substream->runtime;

	if (runtime->oss.params &&
2170
	    (err = snd_pcm_oss_change_params(substream, false)) < 0)
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
		return err;

	info.fragsize = runtime->oss.period_bytes;
	info.fragstotal = runtime->periods;
	if (runtime->oss.prepare) {
		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
			info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
			info.fragments = runtime->oss.periods;
		} else {
			info.bytes = 0;
			info.fragments = 0;
		}
	} else {
		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
			err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
			if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
				avail = runtime->buffer_size;
				err = 0;
				fixup = 0;
			} else {
				avail = runtime->buffer_size - avail;
				fixup = -runtime->oss.buffer_used;
			}
		} else {
			err = snd_pcm_oss_capture_position_fixup(substream, &avail);
			fixup = runtime->oss.buffer_used;
		}
		if (err < 0)
			return err;
		info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
		info.fragments = info.bytes / runtime->oss.period_bytes;
	}

#ifdef OSS_DEBUG
2205 2206 2207
	pcm_dbg(substream->pcm,
		"pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
		info.bytes, info.fragments, info.fragstotal, info.fragsize);
L
Linus Torvalds 已提交
2208 2209 2210 2211 2212 2213
#endif
	if (copy_to_user(_info, &info, sizeof(info)))
		return -EFAULT;
	return 0;
}

2214
static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
L
Linus Torvalds 已提交
2215 2216
{
	// it won't be probably implemented
2217
	// pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
L
Linus Torvalds 已提交
2218 2219 2220
	return -EINVAL;
}

2221
static const char *strip_task_path(const char *path)
L
Linus Torvalds 已提交
2222
{
2223 2224
	const char *ptr, *ptrl = NULL;
	for (ptr = path; *ptr; ptr++) {
L
Linus Torvalds 已提交
2225 2226 2227
		if (*ptr == '/')
			ptrl = ptr + 1;
	}
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	return ptrl;
}

static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
				      const char *task_name,
				      struct snd_pcm_oss_setup *rsetup)
{
	struct snd_pcm_oss_setup *setup;

	mutex_lock(&pcm->streams[stream].oss.setup_mutex);
	do {
		for (setup = pcm->streams[stream].oss.setup_list; setup;
		     setup = setup->next) {
			if (!strcmp(setup->task_name, task_name))
				goto out;
L
Linus Torvalds 已提交
2243
		}
2244 2245 2246 2247
	} while ((task_name = strip_task_path(task_name)) != NULL);
 out:
	if (setup)
		*rsetup = *setup;
2248
	mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
L
Linus Torvalds 已提交
2249 2250
}

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime;
	runtime = substream->runtime;
	vfree(runtime->oss.buffer);
	runtime->oss.buffer = NULL;
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
	snd_pcm_oss_plugin_clear(substream);
#endif
	substream->oss.oss = 0;
}

2263 2264
static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
				       struct snd_pcm_oss_setup *setup,
L
Linus Torvalds 已提交
2265 2266
				       int minor)
{
2267
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
2268 2269

	substream->oss.oss = 1;
2270
	substream->oss.setup = *setup;
2271
	if (setup->nonblock)
2272
		substream->f_flags |= O_NONBLOCK;
2273
	else if (setup->block)
2274
		substream->f_flags &= ~O_NONBLOCK;
L
Linus Torvalds 已提交
2275 2276 2277 2278
	runtime = substream->runtime;
	runtime->oss.params = 1;
	runtime->oss.trigger = 1;
	runtime->oss.rate = 8000;
2279
	mutex_init(&runtime->oss.params_lock);
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
	case SNDRV_MINOR_OSS_PCM_8:
		runtime->oss.format = AFMT_U8;
		break;
	case SNDRV_MINOR_OSS_PCM_16:
		runtime->oss.format = AFMT_S16_LE;
		break;
	default:
		runtime->oss.format = AFMT_MU_LAW;
	}
	runtime->oss.channels = 1;
	runtime->oss.fragshift = 0;
	runtime->oss.maxfrags = 0;
	runtime->oss.subdivision = 0;
2294
	substream->pcm_release = snd_pcm_oss_release_substream;
L
Linus Torvalds 已提交
2295 2296
}

2297
static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
L
Linus Torvalds 已提交
2298 2299
{
	int cidx;
2300 2301
	if (!pcm_oss_file)
		return 0;
L
Linus Torvalds 已提交
2302
	for (cidx = 0; cidx < 2; ++cidx) {
2303
		struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2304 2305
		if (substream)
			snd_pcm_release_substream(substream);
L
Linus Torvalds 已提交
2306 2307 2308 2309 2310 2311
	}
	kfree(pcm_oss_file);
	return 0;
}

static int snd_pcm_oss_open_file(struct file *file,
2312 2313
				 struct snd_pcm *pcm,
				 struct snd_pcm_oss_file **rpcm_oss_file,
L
Linus Torvalds 已提交
2314
				 int minor,
2315
				 struct snd_pcm_oss_setup *setup)
L
Linus Torvalds 已提交
2316
{
2317
	int idx, err;
2318
	struct snd_pcm_oss_file *pcm_oss_file;
2319
	struct snd_pcm_substream *substream;
2320
	fmode_t f_mode = file->f_mode;
L
Linus Torvalds 已提交
2321

2322 2323
	if (rpcm_oss_file)
		*rpcm_oss_file = NULL;
L
Linus Torvalds 已提交
2324

2325
	pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
L
Linus Torvalds 已提交
2326 2327 2328 2329 2330 2331
	if (pcm_oss_file == NULL)
		return -ENOMEM;

	if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
	    (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
		f_mode = FMODE_WRITE;
2332

2333
	file->f_flags &= ~O_APPEND;
2334
	for (idx = 0; idx < 2; idx++) {
2335
		if (setup[idx].disable)
2336
			continue;
2337 2338
		if (! pcm->streams[idx].substream_count)
			continue; /* no matching substream */
2339 2340 2341 2342 2343 2344 2345 2346 2347
		if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
			if (! (f_mode & FMODE_WRITE))
				continue;
		} else {
			if (! (f_mode & FMODE_READ))
				continue;
		}
		err = snd_pcm_open_substream(pcm, idx, file, &substream);
		if (err < 0) {
L
Linus Torvalds 已提交
2348 2349 2350
			snd_pcm_oss_release_file(pcm_oss_file);
			return err;
		}
2351 2352

		pcm_oss_file->streams[idx] = substream;
2353
		substream->file = pcm_oss_file;
2354
		snd_pcm_oss_init_substream(substream, &setup[idx], minor);
L
Linus Torvalds 已提交
2355 2356
	}
	
2357
	if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
L
Linus Torvalds 已提交
2358 2359 2360 2361 2362
		snd_pcm_oss_release_file(pcm_oss_file);
		return -EINVAL;
	}

	file->private_data = pcm_oss_file;
2363 2364
	if (rpcm_oss_file)
		*rpcm_oss_file = pcm_oss_file;
L
Linus Torvalds 已提交
2365 2366 2367 2368
	return 0;
}


2369 2370 2371 2372
static int snd_task_name(struct task_struct *task, char *name, size_t size)
{
	unsigned int idx;

2373 2374
	if (snd_BUG_ON(!task || !name || size < 2))
		return -EINVAL;
2375 2376 2377 2378 2379 2380
	for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
		name[idx] = task->comm[idx];
	name[idx] = '\0';
	return 0;
}

L
Linus Torvalds 已提交
2381 2382 2383 2384
static int snd_pcm_oss_open(struct inode *inode, struct file *file)
{
	int err;
	char task_name[32];
2385 2386
	struct snd_pcm *pcm;
	struct snd_pcm_oss_file *pcm_oss_file;
2387
	struct snd_pcm_oss_setup setup[2];
L
Linus Torvalds 已提交
2388 2389 2390
	int nonblock;
	wait_queue_t wait;

T
Takashi Iwai 已提交
2391 2392 2393 2394
	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;

2395 2396
	pcm = snd_lookup_oss_minor_data(iminor(inode),
					SNDRV_OSS_DEVICE_TYPE_PCM);
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
	if (pcm == NULL) {
		err = -ENODEV;
		goto __error1;
	}
	err = snd_card_file_add(pcm->card, file);
	if (err < 0)
		goto __error1;
	if (!try_module_get(pcm->card->module)) {
		err = -EFAULT;
		goto __error2;
	}
	if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
		err = -EFAULT;
		goto __error;
	}
2412
	memset(setup, 0, sizeof(setup));
L
Linus Torvalds 已提交
2413
	if (file->f_mode & FMODE_WRITE)
2414 2415
		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
					   task_name, &setup[0]);
L
Linus Torvalds 已提交
2416
	if (file->f_mode & FMODE_READ)
2417 2418
		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
					   task_name, &setup[1]);
L
Linus Torvalds 已提交
2419 2420 2421 2422 2423 2424 2425

	nonblock = !!(file->f_flags & O_NONBLOCK);
	if (!nonblock)
		nonblock = nonblock_open;

	init_waitqueue_entry(&wait, current);
	add_wait_queue(&pcm->open_wait, &wait);
2426
	mutex_lock(&pcm->open_mutex);
L
Linus Torvalds 已提交
2427 2428
	while (1) {
		err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2429
					    iminor(inode), setup);
L
Linus Torvalds 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
		if (err >= 0)
			break;
		if (err == -EAGAIN) {
			if (nonblock) {
				err = -EBUSY;
				break;
			}
		} else
			break;
		set_current_state(TASK_INTERRUPTIBLE);
2440
		mutex_unlock(&pcm->open_mutex);
L
Linus Torvalds 已提交
2441
		schedule();
2442
		mutex_lock(&pcm->open_mutex);
2443 2444 2445 2446
		if (pcm->card->shutdown) {
			err = -ENODEV;
			break;
		}
L
Linus Torvalds 已提交
2447 2448 2449 2450 2451 2452
		if (signal_pending(current)) {
			err = -ERESTARTSYS;
			break;
		}
	}
	remove_wait_queue(&pcm->open_wait, &wait);
2453
	mutex_unlock(&pcm->open_mutex);
L
Linus Torvalds 已提交
2454 2455
	if (err < 0)
		goto __error;
T
Takashi Iwai 已提交
2456
	snd_card_unref(pcm->card);
L
Linus Torvalds 已提交
2457 2458 2459 2460 2461 2462 2463
	return err;

      __error:
     	module_put(pcm->card->module);
      __error2:
      	snd_card_file_remove(pcm->card, file);
      __error1:
2464 2465
	if (pcm)
		snd_card_unref(pcm->card);
L
Linus Torvalds 已提交
2466 2467 2468 2469 2470
	return err;
}

static int snd_pcm_oss_release(struct inode *inode, struct file *file)
{
2471 2472 2473
	struct snd_pcm *pcm;
	struct snd_pcm_substream *substream;
	struct snd_pcm_oss_file *pcm_oss_file;
L
Linus Torvalds 已提交
2474 2475 2476 2477 2478

	pcm_oss_file = file->private_data;
	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	if (substream == NULL)
		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2479 2480
	if (snd_BUG_ON(!substream))
		return -ENXIO;
L
Linus Torvalds 已提交
2481
	pcm = substream->pcm;
2482 2483
	if (!pcm->card->shutdown)
		snd_pcm_oss_sync(pcm_oss_file);
2484
	mutex_lock(&pcm->open_mutex);
L
Linus Torvalds 已提交
2485
	snd_pcm_oss_release_file(pcm_oss_file);
2486
	mutex_unlock(&pcm->open_mutex);
L
Linus Torvalds 已提交
2487 2488 2489 2490 2491 2492 2493 2494
	wake_up(&pcm->open_wait);
	module_put(pcm->card->module);
	snd_card_file_remove(pcm->card, file);
	return 0;
}

static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
2495
	struct snd_pcm_oss_file *pcm_oss_file;
L
Linus Torvalds 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
	int __user *p = (int __user *)arg;
	int res;

	pcm_oss_file = file->private_data;
	if (cmd == OSS_GETVERSION)
		return put_user(SNDRV_OSS_VERSION, p);
	if (cmd == OSS_ALSAEMULVER)
		return put_user(1, p);
#if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
	if (((cmd >> 8) & 0xff) == 'M')	{	/* mixer ioctl - for OSS compatibility */
2506
		struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
2507 2508 2509 2510 2511 2512
		int idx;
		for (idx = 0; idx < 2; ++idx) {
			substream = pcm_oss_file->streams[idx];
			if (substream != NULL)
				break;
		}
2513 2514
		if (snd_BUG_ON(idx >= 2))
			return -ENXIO;
L
Linus Torvalds 已提交
2515 2516 2517 2518 2519 2520
		return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
	}
#endif
	if (((cmd >> 8) & 0xff) != 'P')
		return -EINVAL;
#ifdef OSS_DEBUG
2521
	pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
L
Linus Torvalds 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
#endif
	switch (cmd) {
	case SNDCTL_DSP_RESET:
		return snd_pcm_oss_reset(pcm_oss_file);
	case SNDCTL_DSP_SYNC:
		return snd_pcm_oss_sync(pcm_oss_file);
	case SNDCTL_DSP_SPEED:
		if (get_user(res, p))
			return -EFAULT;
		if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
			return res;
		return put_user(res, p);
	case SOUND_PCM_READ_RATE:
		res = snd_pcm_oss_get_rate(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_STEREO:
		if (get_user(res, p))
			return -EFAULT;
		res = res > 0 ? 2 : 1;
		if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
			return res;
		return put_user(--res, p);
	case SNDCTL_DSP_GETBLKSIZE:
		res = snd_pcm_oss_get_block_size(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_SETFMT:
		if (get_user(res, p))
			return -EFAULT;
		res = snd_pcm_oss_set_format(pcm_oss_file, res);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SOUND_PCM_READ_BITS:
		res = snd_pcm_oss_get_format(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_CHANNELS:
		if (get_user(res, p))
			return -EFAULT;
		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SOUND_PCM_READ_CHANNELS:
		res = snd_pcm_oss_get_channels(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SOUND_PCM_WRITE_FILTER:
	case SOUND_PCM_READ_FILTER:
		return -EIO;
	case SNDCTL_DSP_POST:
		return snd_pcm_oss_post(pcm_oss_file);
	case SNDCTL_DSP_SUBDIVIDE:
		if (get_user(res, p))
			return -EFAULT;
		res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_SETFRAGMENT:
		if (get_user(res, p))
			return -EFAULT;
		return snd_pcm_oss_set_fragment(pcm_oss_file, res);
	case SNDCTL_DSP_GETFMTS:
		res = snd_pcm_oss_get_formats(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_GETOSPACE:
	case SNDCTL_DSP_GETISPACE:
		return snd_pcm_oss_get_space(pcm_oss_file,
			cmd == SNDCTL_DSP_GETISPACE ?
				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
			(struct audio_buf_info __user *) arg);
	case SNDCTL_DSP_NONBLOCK:
		return snd_pcm_oss_nonblock(file);
	case SNDCTL_DSP_GETCAPS:
		res = snd_pcm_oss_get_caps(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_GETTRIGGER:
		res = snd_pcm_oss_get_trigger(pcm_oss_file);
		if (res < 0)
			return res;
		return put_user(res, p);
	case SNDCTL_DSP_SETTRIGGER:
		if (get_user(res, p))
			return -EFAULT;
		return snd_pcm_oss_set_trigger(pcm_oss_file, res);
	case SNDCTL_DSP_GETIPTR:
	case SNDCTL_DSP_GETOPTR:
		return snd_pcm_oss_get_ptr(pcm_oss_file,
			cmd == SNDCTL_DSP_GETIPTR ?
				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
			(struct count_info __user *) arg);
	case SNDCTL_DSP_MAPINBUF:
	case SNDCTL_DSP_MAPOUTBUF:
		return snd_pcm_oss_get_mapbuf(pcm_oss_file,
			cmd == SNDCTL_DSP_MAPINBUF ?
				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
			(struct buffmem_desc __user *) arg);
	case SNDCTL_DSP_SETSYNCRO:
		/* stop DMA now.. */
		return 0;
	case SNDCTL_DSP_SETDUPLEX:
		if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
			return 0;
		return -EIO;
	case SNDCTL_DSP_GETODELAY:
		res = snd_pcm_oss_get_odelay(pcm_oss_file);
		if (res < 0) {
			/* it's for sure, some broken apps don't check for error codes */
			put_user(0, p);
			return res;
		}
		return put_user(res, p);
	case SNDCTL_DSP_PROFILE:
		return 0;	/* silently ignore */
	default:
2648
		pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
L
Linus Torvalds 已提交
2649 2650 2651 2652 2653 2654
	}
	return -EINVAL;
}

#ifdef CONFIG_COMPAT
/* all compatible */
2655 2656 2657 2658 2659
static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
				     unsigned long arg)
{
	return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
}
L
Linus Torvalds 已提交
2660 2661 2662 2663 2664 2665
#else
#define snd_pcm_oss_ioctl_compat	NULL
#endif

static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
2666 2667
	struct snd_pcm_oss_file *pcm_oss_file;
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
2668 2669 2670 2671 2672

	pcm_oss_file = file->private_data;
	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
	if (substream == NULL)
		return -ENXIO;
2673
	substream->f_flags = file->f_flags & O_NONBLOCK;
L
Linus Torvalds 已提交
2674 2675 2676 2677 2678
#ifndef OSS_DEBUG
	return snd_pcm_oss_read1(substream, buf, count);
#else
	{
		ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2679 2680 2681
		pcm_dbg(substream->pcm,
			"pcm_oss: read %li bytes (returned %li bytes)\n",
			(long)count, (long)res);
L
Linus Torvalds 已提交
2682 2683 2684 2685 2686 2687 2688
		return res;
	}
#endif
}

static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
2689 2690
	struct snd_pcm_oss_file *pcm_oss_file;
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
2691 2692 2693 2694 2695 2696
	long result;

	pcm_oss_file = file->private_data;
	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	if (substream == NULL)
		return -ENXIO;
2697
	substream->f_flags = file->f_flags & O_NONBLOCK;
L
Linus Torvalds 已提交
2698 2699
	result = snd_pcm_oss_write1(substream, buf, count);
#ifdef OSS_DEBUG
2700
	pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2701
	       (long)count, (long)result);
L
Linus Torvalds 已提交
2702 2703 2704 2705
#endif
	return result;
}

2706
static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
2707
{
2708
	struct snd_pcm_runtime *runtime = substream->runtime;
2709
	if (atomic_read(&substream->mmap_count))
2710 2711
		return runtime->oss.prev_hw_ptr_period !=
						get_hw_ptr_period(runtime);
L
Linus Torvalds 已提交
2712
	else
2713 2714
		return snd_pcm_playback_avail(runtime) >=
						runtime->oss.period_frames;
L
Linus Torvalds 已提交
2715 2716
}

2717
static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
2718
{
2719
	struct snd_pcm_runtime *runtime = substream->runtime;
2720
	if (atomic_read(&substream->mmap_count))
2721 2722
		return runtime->oss.prev_hw_ptr_period !=
						get_hw_ptr_period(runtime);
L
Linus Torvalds 已提交
2723
	else
2724 2725
		return snd_pcm_capture_avail(runtime) >=
						runtime->oss.period_frames;
L
Linus Torvalds 已提交
2726 2727 2728 2729
}

static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
{
2730
	struct snd_pcm_oss_file *pcm_oss_file;
L
Linus Torvalds 已提交
2731
	unsigned int mask;
2732
	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
L
Linus Torvalds 已提交
2733 2734 2735 2736 2737 2738 2739 2740
	
	pcm_oss_file = file->private_data;

	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];

	mask = 0;
	if (psubstream != NULL) {
2741
		struct snd_pcm_runtime *runtime = psubstream->runtime;
L
Linus Torvalds 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750
		poll_wait(file, &runtime->sleep, wait);
		snd_pcm_stream_lock_irq(psubstream);
		if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
		    (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
		     snd_pcm_oss_playback_ready(psubstream)))
			mask |= POLLOUT | POLLWRNORM;
		snd_pcm_stream_unlock_irq(psubstream);
	}
	if (csubstream != NULL) {
2751 2752
		struct snd_pcm_runtime *runtime = csubstream->runtime;
		snd_pcm_state_t ostate;
L
Linus Torvalds 已提交
2753 2754 2755 2756 2757 2758 2759
		poll_wait(file, &runtime->sleep, wait);
		snd_pcm_stream_lock_irq(csubstream);
		if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
		    snd_pcm_oss_capture_ready(csubstream))
			mask |= POLLIN | POLLRDNORM;
		snd_pcm_stream_unlock_irq(csubstream);
		if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2760
			struct snd_pcm_oss_file ofile;
L
Linus Torvalds 已提交
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
			memset(&ofile, 0, sizeof(ofile));
			ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
			runtime->oss.trigger = 0;
			snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
		}
	}

	return mask;
}

static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
{
2773 2774 2775
	struct snd_pcm_oss_file *pcm_oss_file;
	struct snd_pcm_substream *substream = NULL;
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
2776 2777 2778
	int err;

#ifdef OSS_DEBUG
2779
	pr_debug("pcm_oss: mmap begin\n");
L
Linus Torvalds 已提交
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
#endif
	pcm_oss_file = file->private_data;
	switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
	case VM_READ | VM_WRITE:
		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
		if (substream)
			break;
		/* Fall through */
	case VM_READ:
		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
		break;
	case VM_WRITE:
		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
		break;
	default:
		return -EINVAL;
	}
	/* set VM_READ access as well to fix memset() routines that do
	   reads before writes (to improve performance) */
	area->vm_flags |= VM_READ;
	if (substream == NULL)
		return -ENXIO;
	runtime = substream->runtime;
	if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
		return -EIO;
	if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
		runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
	else
		return -EIO;
	
	if (runtime->oss.params) {
2811 2812 2813 2814 2815 2816
		/* use mutex_trylock() for params_lock for avoiding a deadlock
		 * between mmap_sem and params_lock taken by
		 * copy_from/to_user() in snd_pcm_oss_write/read()
		 */
		err = snd_pcm_oss_change_params(substream, true);
		if (err < 0)
L
Linus Torvalds 已提交
2817 2818
			return err;
	}
2819
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
L
Linus Torvalds 已提交
2820 2821
	if (runtime->oss.plugin_first != NULL)
		return -EIO;
2822
#endif
L
Linus Torvalds 已提交
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833

	if (area->vm_pgoff != 0)
		return -EINVAL;

	err = snd_pcm_mmap_data(substream, file, area);
	if (err < 0)
		return err;
	runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
	runtime->silence_threshold = 0;
	runtime->silence_size = 0;
#ifdef OSS_DEBUG
2834
	pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2835
	       runtime->oss.mmap_bytes);
L
Linus Torvalds 已提交
2836 2837 2838 2839 2840 2841 2842
#endif
	/* In mmap mode we never stop */
	runtime->stop_threshold = runtime->boundary;

	return 0;
}

2843
#ifdef CONFIG_SND_VERBOSE_PROCFS
L
Linus Torvalds 已提交
2844 2845 2846 2847
/*
 *  /proc interface
 */

2848 2849
static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
				  struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
2850
{
2851 2852
	struct snd_pcm_str *pstr = entry->private_data;
	struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2853
	mutex_lock(&pstr->oss.setup_mutex);
L
Linus Torvalds 已提交
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
	while (setup) {
		snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
			    setup->task_name,
			    setup->periods,
			    setup->period_size,
			    setup->disable ? " disable" : "",
			    setup->direct ? " direct" : "",
			    setup->block ? " block" : "",
			    setup->nonblock ? " non-block" : "",
			    setup->partialfrag ? " partial-frag" : "",
			    setup->nosilence ? " no-silence" : "");
		setup = setup->next;
	}
2867
	mutex_unlock(&pstr->oss.setup_mutex);
L
Linus Torvalds 已提交
2868 2869
}

2870
static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
L
Linus Torvalds 已提交
2871
{
2872
	struct snd_pcm_oss_setup *setup, *setupn;
L
Linus Torvalds 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882

	for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
	     setup; setup = setupn) {
		setupn = setup->next;
		kfree(setup->task_name);
		kfree(setup);
	}
	pstr->oss.setup_list = NULL;
}

2883 2884
static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
				   struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
2885
{
2886
	struct snd_pcm_str *pstr = entry->private_data;
2887 2888
	char line[128], str[32], task_name[32];
	const char *ptr;
L
Linus Torvalds 已提交
2889
	int idx1;
2890
	struct snd_pcm_oss_setup *setup, *setup1, template;
L
Linus Torvalds 已提交
2891 2892

	while (!snd_info_get_line(buffer, line, sizeof(line))) {
2893
		mutex_lock(&pstr->oss.setup_mutex);
L
Linus Torvalds 已提交
2894 2895 2896 2897
		memset(&template, 0, sizeof(template));
		ptr = snd_info_get_str(task_name, line, sizeof(task_name));
		if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
			snd_pcm_oss_proc_free_setup_list(pstr);
2898
			mutex_unlock(&pstr->oss.setup_mutex);
L
Linus Torvalds 已提交
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
			continue;
		}
		for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
			if (!strcmp(setup->task_name, task_name)) {
				template = *setup;
				break;
			}
		}
		ptr = snd_info_get_str(str, ptr, sizeof(str));
		template.periods = simple_strtoul(str, NULL, 10);
		ptr = snd_info_get_str(str, ptr, sizeof(str));
		template.period_size = simple_strtoul(str, NULL, 10);
		for (idx1 = 31; idx1 >= 0; idx1--)
			if (template.period_size & (1 << idx1))
				break;
		for (idx1--; idx1 >= 0; idx1--)
			template.period_size &= ~(1 << idx1);
		do {
			ptr = snd_info_get_str(str, ptr, sizeof(str));
			if (!strcmp(str, "disable")) {
				template.disable = 1;
			} else if (!strcmp(str, "direct")) {
				template.direct = 1;
			} else if (!strcmp(str, "block")) {
				template.block = 1;
			} else if (!strcmp(str, "non-block")) {
				template.nonblock = 1;
			} else if (!strcmp(str, "partial-frag")) {
				template.partialfrag = 1;
			} else if (!strcmp(str, "no-silence")) {
				template.nosilence = 1;
2930 2931
			} else if (!strcmp(str, "buggy-ptr")) {
				template.buggyptr = 1;
L
Linus Torvalds 已提交
2932 2933 2934
			}
		} while (*str);
		if (setup == NULL) {
2935 2936 2937
			setup = kmalloc(sizeof(*setup), GFP_KERNEL);
			if (! setup) {
				buffer->error = -ENOMEM;
J
Jiri Slaby 已提交
2938
				mutex_unlock(&pstr->oss.setup_mutex);
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
				return;
			}
			if (pstr->oss.setup_list == NULL)
				pstr->oss.setup_list = setup;
			else {
				for (setup1 = pstr->oss.setup_list;
				     setup1->next; setup1 = setup1->next);
				setup1->next = setup;
			}
			template.task_name = kstrdup(task_name, GFP_KERNEL);
			if (! template.task_name) {
				kfree(setup);
L
Linus Torvalds 已提交
2951
				buffer->error = -ENOMEM;
J
Jiri Slaby 已提交
2952
				mutex_unlock(&pstr->oss.setup_mutex);
2953
				return;
L
Linus Torvalds 已提交
2954 2955
			}
		}
2956
		*setup = template;
2957
		mutex_unlock(&pstr->oss.setup_mutex);
L
Linus Torvalds 已提交
2958 2959 2960
	}
}

2961
static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
2962 2963 2964
{
	int stream;
	for (stream = 0; stream < 2; ++stream) {
2965 2966
		struct snd_info_entry *entry;
		struct snd_pcm_str *pstr = &pcm->streams[stream];
L
Linus Torvalds 已提交
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
		if (pstr->substream_count == 0)
			continue;
		if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
			entry->content = SNDRV_INFO_CONTENT_TEXT;
			entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
			entry->c.text.read = snd_pcm_oss_proc_read;
			entry->c.text.write = snd_pcm_oss_proc_write;
			entry->private_data = pstr;
			if (snd_info_register(entry) < 0) {
				snd_info_free_entry(entry);
				entry = NULL;
			}
		}
		pstr->oss.proc_entry = entry;
	}
}

2984
static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
2985 2986 2987
{
	int stream;
	for (stream = 0; stream < 2; ++stream) {
2988
		struct snd_pcm_str *pstr = &pcm->streams[stream];
2989 2990 2991
		snd_info_free_entry(pstr->oss.proc_entry);
		pstr->oss.proc_entry = NULL;
		snd_pcm_oss_proc_free_setup_list(pstr);
L
Linus Torvalds 已提交
2992 2993
	}
}
2994
#else /* !CONFIG_SND_VERBOSE_PROCFS */
2995 2996
#define snd_pcm_oss_proc_init(pcm)
#define snd_pcm_oss_proc_done(pcm)
2997
#endif /* CONFIG_SND_VERBOSE_PROCFS */
L
Linus Torvalds 已提交
2998 2999 3000 3001 3002

/*
 *  ENTRY functions
 */

3003
static const struct file_operations snd_pcm_oss_f_reg =
L
Linus Torvalds 已提交
3004 3005 3006 3007 3008 3009
{
	.owner =	THIS_MODULE,
	.read =		snd_pcm_oss_read,
	.write =	snd_pcm_oss_write,
	.open =		snd_pcm_oss_open,
	.release =	snd_pcm_oss_release,
T
Takashi Iwai 已提交
3010
	.llseek =	no_llseek,
L
Linus Torvalds 已提交
3011 3012 3013 3014 3015 3016
	.poll =		snd_pcm_oss_poll,
	.unlocked_ioctl =	snd_pcm_oss_ioctl,
	.compat_ioctl =	snd_pcm_oss_ioctl_compat,
	.mmap =		snd_pcm_oss_mmap,
};

3017
static void register_oss_dsp(struct snd_pcm *pcm, int index)
L
Linus Torvalds 已提交
3018 3019
{
	if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3020
				    pcm->card, index, &snd_pcm_oss_f_reg,
3021
				    pcm) < 0) {
3022
		pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
T
Takashi Iwai 已提交
3023
			   pcm->card->number, pcm->device);
L
Linus Torvalds 已提交
3024 3025 3026
	}
}

3027
static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
3028 3029
{
	pcm->oss.reg = 0;
3030
	if (dsp_map[pcm->card->number] == (int)pcm->device) {
L
Linus Torvalds 已提交
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
		char name[128];
		int duplex;
		register_oss_dsp(pcm, 0);
		duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
			      pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
			      !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
		sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
#ifdef SNDRV_OSS_INFO_DEV_AUDIO
		snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
				      pcm->card->number,
				      name);
#endif
		pcm->oss.reg++;
		pcm->oss.reg_mask |= 1;
	}
3046
	if (adsp_map[pcm->card->number] == (int)pcm->device) {
L
Linus Torvalds 已提交
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
		register_oss_dsp(pcm, 1);
		pcm->oss.reg++;
		pcm->oss.reg_mask |= 2;
	}

	if (pcm->oss.reg)
		snd_pcm_oss_proc_init(pcm);

	return 0;
}

3058
static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
L
Linus Torvalds 已提交
3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
{
	if (pcm->oss.reg) {
		if (pcm->oss.reg_mask & 1) {
			pcm->oss.reg_mask &= ~1;
			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
						  pcm->card, 0);
		}
		if (pcm->oss.reg_mask & 2) {
			pcm->oss.reg_mask &= ~2;
			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
						  pcm->card, 1);
		}
3071
		if (dsp_map[pcm->card->number] == (int)pcm->device) {
L
Linus Torvalds 已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080
#ifdef SNDRV_OSS_INFO_DEV_AUDIO
			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
#endif
		}
		pcm->oss.reg = 0;
	}
	return 0;
}

3081 3082 3083 3084 3085 3086 3087
static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
{
	snd_pcm_oss_disconnect_minor(pcm);
	snd_pcm_oss_proc_done(pcm);
	return 0;
}

3088
static struct snd_pcm_notify snd_pcm_oss_notify =
L
Linus Torvalds 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
{
	.n_register =	snd_pcm_oss_register_minor,
	.n_disconnect = snd_pcm_oss_disconnect_minor,
	.n_unregister =	snd_pcm_oss_unregister_minor,
};

static int __init alsa_pcm_oss_init(void)
{
	int i;
	int err;

	/* check device map table */
	for (i = 0; i < SNDRV_CARDS; i++) {
3102
		if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3103
			pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
T
Takashi Iwai 已提交
3104
				   i, dsp_map[i]);
L
Linus Torvalds 已提交
3105 3106
			dsp_map[i] = 0;
		}
3107
		if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3108
			pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
T
Takashi Iwai 已提交
3109
				   i, adsp_map[i]);
L
Linus Torvalds 已提交
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
			adsp_map[i] = 1;
		}
	}
	if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
		return err;
	return 0;
}

static void __exit alsa_pcm_oss_exit(void)
{
	snd_pcm_notify(&snd_pcm_oss_notify, 1);
}

module_init(alsa_pcm_oss_init)
module_exit(alsa_pcm_oss_exit)