File Index
Files and directories
Sign in
FILE

pcm_oss.c

File preview with syntax highlighting.

linux-6.19.14/sound/core/oss/pcm_oss.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Digital Audio (PCM) abstract layer / OSS compatible
   4 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   5 */
   6
   7#if 0
   8#define PLUGIN_DEBUG
   9#endif
  10#if 0
  11#define OSS_DEBUG
  12#endif
  13
  14#include <linux/init.h>
  15#include <linux/slab.h>
  16#include <linux/sched/signal.h>
  17#include <linux/time.h>
  18#include <linux/vmalloc.h>
  19#include <linux/module.h>
  20#include <linux/math64.h>
  21#include <linux/string.h>
  22#include <linux/compat.h>
  23#include <sound/core.h>
  24#include <sound/minors.h>
  25#include <sound/pcm.h>
  26#include <sound/pcm_params.h>
  27#include "pcm_plugin.h"
  28#include <sound/info.h>
  29#include <linux/soundcard.h>
  30#include <sound/initval.h>
  31#include <sound/mixer_oss.h>
  32
  33#define OSS_ALSAEMULVER		_SIOR ('M', 249, int)
  34
  35static int dsp_map[SNDRV_CARDS];
  36static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  37static bool nonblock_open = 1;
  38
  39MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
  40MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
  41MODULE_LICENSE("GPL");
  42module_param_array(dsp_map, int, NULL, 0444);
  43MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
  44module_param_array(adsp_map, int, NULL, 0444);
  45MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
  46module_param(nonblock_open, bool, 0644);
  47MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
  48MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
  49MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
  50
  51static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
  52static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
  53static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
  54
  55/*
  56 * helper functions to process hw_params
  57 */
  58static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
  59{
  60	int changed = 0;
  61	if (i->min < min) {
  62		i->min = min;
  63		i->openmin = openmin;
  64		changed = 1;
  65	} else if (i->min == min && !i->openmin && openmin) {
  66		i->openmin = 1;
  67		changed = 1;
  68	}
  69	if (i->integer) {
  70		if (i->openmin) {
  71			i->min++;
  72			i->openmin = 0;
  73		}
  74	}
  75	if (snd_interval_checkempty(i)) {
  76		snd_interval_none(i);
  77		return -EINVAL;
  78	}
  79	return changed;
  80}
  81
  82static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
  83{
  84	int changed = 0;
  85	if (i->max > max) {
  86		i->max = max;
  87		i->openmax = openmax;
  88		changed = 1;
  89	} else if (i->max == max && !i->openmax && openmax) {
  90		i->openmax = 1;
  91		changed = 1;
  92	}
  93	if (i->integer) {
  94		if (i->openmax) {
  95			i->max--;
  96			i->openmax = 0;
  97		}
  98	}
  99	if (snd_interval_checkempty(i)) {
 100		snd_interval_none(i);
 101		return -EINVAL;
 102	}
 103	return changed;
 104}
 105
 106static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
 107{
 108	struct snd_interval t;
 109	t.empty = 0;
 110	t.min = t.max = val;
 111	t.openmin = t.openmax = 0;
 112	t.integer = 1;
 113	return snd_interval_refine(i, &t);
 114}
 115
 116/**
 117 * snd_pcm_hw_param_value_min
 118 * @params: the hw_params instance
 119 * @var: parameter to retrieve
 120 * @dir: pointer to the direction (-1,0,1) or NULL
 121 *
 122 * Return the minimum value for field PAR.
 123 */
 124static unsigned int
 125snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
 126			   snd_pcm_hw_param_t var, int *dir)
 127{
 128	if (hw_is_mask(var)) {
 129		if (dir)
 130			*dir = 0;
 131		return snd_mask_min(hw_param_mask_c(params, var));
 132	}
 133	if (hw_is_interval(var)) {
 134		const struct snd_interval *i = hw_param_interval_c(params, var);
 135		if (dir)
 136			*dir = i->openmin;
 137		return snd_interval_min(i);
 138	}
 139	return -EINVAL;
 140}
 141
 142/**
 143 * snd_pcm_hw_param_value_max
 144 * @params: the hw_params instance
 145 * @var: parameter to retrieve
 146 * @dir: pointer to the direction (-1,0,1) or NULL
 147 *
 148 * Return the maximum value for field PAR.
 149 */
 150static int
 151snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
 152			   snd_pcm_hw_param_t var, int *dir)
 153{
 154	if (hw_is_mask(var)) {
 155		if (dir)
 156			*dir = 0;
 157		return snd_mask_max(hw_param_mask_c(params, var));
 158	}
 159	if (hw_is_interval(var)) {
 160		const struct snd_interval *i = hw_param_interval_c(params, var);
 161		if (dir)
 162			*dir = - (int) i->openmax;
 163		return snd_interval_max(i);
 164	}
 165	return -EINVAL;
 166}
 167
 168static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
 169				  snd_pcm_hw_param_t var,
 170				  const struct snd_mask *val)
 171{
 172	int changed;
 173	changed = snd_mask_refine(hw_param_mask(params, var), val);
 174	if (changed > 0) {
 175		params->cmask |= 1 << var;
 176		params->rmask |= 1 << var;
 177	}
 178	return changed;
 179}
 180
 181static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
 182				 struct snd_pcm_hw_params *params,
 183				 snd_pcm_hw_param_t var,
 184				 const struct snd_mask *val)
 185{
 186	int changed = _snd_pcm_hw_param_mask(params, var, val);
 187	if (changed < 0)
 188		return changed;
 189	if (params->rmask) {
 190		int err = snd_pcm_hw_refine(pcm, params);
 191		if (err < 0)
 192			return err;
 193	}
 194	return 0;
 195}
 196
 197static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
 198				 snd_pcm_hw_param_t var, unsigned int val,
 199				 int dir)
 200{
 201	int changed;
 202	int open = 0;
 203	if (dir) {
 204		if (dir > 0) {
 205			open = 1;
 206		} else if (dir < 0) {
 207			if (val > 0) {
 208				open = 1;
 209				val--;
 210			}
 211		}
 212	}
 213	if (hw_is_mask(var))
 214		changed = snd_mask_refine_min(hw_param_mask(params, var),
 215					      val + !!open);
 216	else if (hw_is_interval(var))
 217		changed = snd_interval_refine_min(hw_param_interval(params, var),
 218						  val, open);
 219	else
 220		return -EINVAL;
 221	if (changed > 0) {
 222		params->cmask |= 1 << var;
 223		params->rmask |= 1 << var;
 224	}
 225	return changed;
 226}
 227
 228/**
 229 * snd_pcm_hw_param_min
 230 * @pcm: PCM instance
 231 * @params: the hw_params instance
 232 * @var: parameter to retrieve
 233 * @val: minimal value
 234 * @dir: pointer to the direction (-1,0,1) or NULL
 235 *
 236 * Inside configuration space defined by PARAMS remove from PAR all 
 237 * values < VAL. Reduce configuration space accordingly.
 238 * Return new minimum or -EINVAL if the configuration space is empty
 239 */
 240static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
 241				struct snd_pcm_hw_params *params,
 242				snd_pcm_hw_param_t var, unsigned int val,
 243				int *dir)
 244{
 245	int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
 246	if (changed < 0)
 247		return changed;
 248	if (params->rmask) {
 249		int err = snd_pcm_hw_refine(pcm, params);
 250		if (err < 0)
 251			return err;
 252	}
 253	return snd_pcm_hw_param_value_min(params, var, dir);
 254}
 255
 256static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
 257				 snd_pcm_hw_param_t var, unsigned int val,
 258				 int dir)
 259{
 260	int changed;
 261	int open = 0;
 262	if (dir) {
 263		if (dir < 0) {
 264			open = 1;
 265		} else if (dir > 0) {
 266			open = 1;
 267			val++;
 268		}
 269	}
 270	if (hw_is_mask(var)) {
 271		if (val == 0 && open) {
 272			snd_mask_none(hw_param_mask(params, var));
 273			changed = -EINVAL;
 274		} else
 275			changed = snd_mask_refine_max(hw_param_mask(params, var),
 276						      val - !!open);
 277	} else if (hw_is_interval(var))
 278		changed = snd_interval_refine_max(hw_param_interval(params, var),
 279						  val, open);
 280	else
 281		return -EINVAL;
 282	if (changed > 0) {
 283		params->cmask |= 1 << var;
 284		params->rmask |= 1 << var;
 285	}
 286	return changed;
 287}
 288
 289/**
 290 * snd_pcm_hw_param_max
 291 * @pcm: PCM instance
 292 * @params: the hw_params instance
 293 * @var: parameter to retrieve
 294 * @val: maximal value
 295 * @dir: pointer to the direction (-1,0,1) or NULL
 296 *
 297 * Inside configuration space defined by PARAMS remove from PAR all 
 298 *  values >= VAL + 1. Reduce configuration space accordingly.
 299 *  Return new maximum or -EINVAL if the configuration space is empty
 300 */
 301static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
 302				struct snd_pcm_hw_params *params,
 303				snd_pcm_hw_param_t var, unsigned int val,
 304				int *dir)
 305{
 306	int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
 307	if (changed < 0)
 308		return changed;
 309	if (params->rmask) {
 310		int err = snd_pcm_hw_refine(pcm, params);
 311		if (err < 0)
 312			return err;
 313	}
 314	return snd_pcm_hw_param_value_max(params, var, dir);
 315}
 316
 317static int boundary_sub(int a, int adir,
 318			int b, int bdir,
 319			int *c, int *cdir)
 320{
 321	adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
 322	bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
 323	*c = a - b;
 324	*cdir = adir - bdir;
 325	if (*cdir == -2) {
 326		(*c)--;
 327	} else if (*cdir == 2) {
 328		(*c)++;
 329	}
 330	return 0;
 331}
 332
 333static int boundary_lt(unsigned int a, int adir,
 334		       unsigned int b, int bdir)
 335{
 336	if (adir < 0) {
 337		a--;
 338		adir = 1;
 339	} else if (adir > 0)
 340		adir = 1;
 341	if (bdir < 0) {
 342		b--;
 343		bdir = 1;
 344	} else if (bdir > 0)
 345		bdir = 1;
 346	return a < b || (a == b && adir < bdir);
 347}
 348
 349/* Return 1 if min is nearer to best than max */
 350static int boundary_nearer(int min, int mindir,
 351			   int best, int bestdir,
 352			   int max, int maxdir)
 353{
 354	int dmin, dmindir;
 355	int dmax, dmaxdir;
 356	boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
 357	boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
 358	return boundary_lt(dmin, dmindir, dmax, dmaxdir);
 359}
 360
 361/**
 362 * snd_pcm_hw_param_near
 363 * @pcm: PCM instance
 364 * @params: the hw_params instance
 365 * @var: parameter to retrieve
 366 * @best: value to set
 367 * @dir: pointer to the direction (-1,0,1) or NULL
 368 *
 369 * Inside configuration space defined by PARAMS set PAR to the available value
 370 * nearest to VAL. Reduce configuration space accordingly.
 371 * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
 372 * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
 373 * Return the value found.
 374  */
 375static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
 376				 struct snd_pcm_hw_params *params,
 377				 snd_pcm_hw_param_t var, unsigned int best,
 378				 int *dir)
 379{
 380	int v;
 381	unsigned int saved_min;
 382	int last = 0;
 383	int min, max;
 384	int mindir, maxdir;
 385	int valdir = dir ? *dir : 0;
 386	/* FIXME */
 387	if (best > INT_MAX)
 388		best = INT_MAX;
 389	min = max = best;
 390	mindir = maxdir = valdir;
 391	if (maxdir > 0)
 392		maxdir = 0;
 393	else if (maxdir == 0)
 394		maxdir = -1;
 395	else {
 396		maxdir = 1;
 397		max--;
 398	}
 399
 400	struct snd_pcm_hw_params *save __free(kfree) =
 401		kmalloc(sizeof(*save), GFP_KERNEL);
 402	if (save == NULL)
 403		return -ENOMEM;
 404	*save = *params;
 405	saved_min = min;
 406	min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
 407	if (min >= 0) {
 408		if (max < 0)
 409			goto _end;
 410		if ((unsigned int)min == saved_min && mindir == valdir)
 411			goto _end;
 412
 413		struct snd_pcm_hw_params *params1 __free(kfree) =
 414			kmalloc(sizeof(*params1), GFP_KERNEL);
 415		if (params1 == NULL)
 416			return -ENOMEM;
 417		*params1 = *save;
 418		max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
 419		if (max < 0)
 420			goto _end;
 421		if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
 422			*params = *params1;
 423			last = 1;
 424		}
 425	} else {
 426		*params = *save;
 427		max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
 428		if (max < 0)
 429			return max;
 430		last = 1;
 431	}
 432 _end:
 433	if (last)
 434		v = snd_pcm_hw_param_last(pcm, params, var, dir);
 435	else
 436		v = snd_pcm_hw_param_first(pcm, params, var, dir);
 437	return v;
 438}
 439
 440static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
 441				 snd_pcm_hw_param_t var, unsigned int val,
 442				 int dir)
 443{
 444	int changed;
 445	if (hw_is_mask(var)) {
 446		struct snd_mask *m = hw_param_mask(params, var);
 447		if (val == 0 && dir < 0) {
 448			changed = -EINVAL;
 449			snd_mask_none(m);
 450		} else {
 451			if (dir > 0)
 452				val++;
 453			else if (dir < 0)
 454				val--;
 455			changed = snd_mask_refine_set(hw_param_mask(params, var), val);
 456		}
 457	} else if (hw_is_interval(var)) {
 458		struct snd_interval *i = hw_param_interval(params, var);
 459		if (val == 0 && dir < 0) {
 460			changed = -EINVAL;
 461			snd_interval_none(i);
 462		} else if (dir == 0)
 463			changed = snd_interval_refine_set(i, val);
 464		else {
 465			struct snd_interval t;
 466			t.openmin = 1;
 467			t.openmax = 1;
 468			t.empty = 0;
 469			t.integer = 0;
 470			if (dir < 0) {
 471				t.min = val - 1;
 472				t.max = val;
 473			} else {
 474				t.min = val;
 475				t.max = val+1;
 476			}
 477			changed = snd_interval_refine(i, &t);
 478		}
 479	} else
 480		return -EINVAL;
 481	if (changed > 0) {
 482		params->cmask |= 1 << var;
 483		params->rmask |= 1 << var;
 484	}
 485	return changed;
 486}
 487
 488/**
 489 * snd_pcm_hw_param_set
 490 * @pcm: PCM instance
 491 * @params: the hw_params instance
 492 * @var: parameter to retrieve
 493 * @val: value to set
 494 * @dir: pointer to the direction (-1,0,1) or NULL
 495 *
 496 * Inside configuration space defined by PARAMS remove from PAR all 
 497 * values != VAL. Reduce configuration space accordingly.
 498 *  Return VAL or -EINVAL if the configuration space is empty
 499 */
 500static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
 501				struct snd_pcm_hw_params *params,
 502				snd_pcm_hw_param_t var, unsigned int val,
 503				int dir)
 504{
 505	int changed = _snd_pcm_hw_param_set(params, var, val, dir);
 506	if (changed < 0)
 507		return changed;
 508	if (params->rmask) {
 509		int err = snd_pcm_hw_refine(pcm, params);
 510		if (err < 0)
 511			return err;
 512	}
 513	return snd_pcm_hw_param_value(params, var, NULL);
 514}
 515
 516static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
 517					snd_pcm_hw_param_t var)
 518{
 519	int changed;
 520	changed = snd_interval_setinteger(hw_param_interval(params, var));
 521	if (changed > 0) {
 522		params->cmask |= 1 << var;
 523		params->rmask |= 1 << var;
 524	}
 525	return changed;
 526}
 527	
 528/*
 529 * plugin
 530 */
 531
 532#ifdef CONFIG_SND_PCM_OSS_PLUGINS
 533static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
 534{
 535	struct snd_pcm_runtime *runtime = substream->runtime;
 536	struct snd_pcm_plugin *plugin, *next;
 537	
 538	plugin = runtime->oss.plugin_first;
 539	while (plugin) {
 540		next = plugin->next;
 541		snd_pcm_plugin_free(plugin);
 542		plugin = next;
 543	}
 544	runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
 545	return 0;
 546}
 547
 548static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
 549{
 550	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
 551	plugin->next = runtime->oss.plugin_first;
 552	plugin->prev = NULL;
 553	if (runtime->oss.plugin_first) {
 554		runtime->oss.plugin_first->prev = plugin;
 555		runtime->oss.plugin_first = plugin;
 556	} else {
 557		runtime->oss.plugin_last =
 558		runtime->oss.plugin_first = plugin;
 559	}
 560	return 0;
 561}
 562
 563int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
 564{
 565	struct snd_pcm_runtime *runtime = plugin->plug->runtime;
 566	plugin->next = NULL;
 567	plugin->prev = runtime->oss.plugin_last;
 568	if (runtime->oss.plugin_last) {
 569		runtime->oss.plugin_last->next = plugin;
 570		runtime->oss.plugin_last = plugin;
 571	} else {
 572		runtime->oss.plugin_last =
 573		runtime->oss.plugin_first = plugin;
 574	}
 575	return 0;
 576}
 577#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
 578
 579static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
 580{
 581	struct snd_pcm_runtime *runtime = substream->runtime;
 582	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
 583	long bytes = frames_to_bytes(runtime, frames);
 584	if (buffer_size == runtime->oss.buffer_bytes)
 585		return bytes;
 586#if BITS_PER_LONG >= 64
 587	return runtime->oss.buffer_bytes * bytes / buffer_size;
 588#else
 589	{
 590		u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
 591		return div_u64(bsize, buffer_size);
 592	}
 593#endif
 594}
 595
 596static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
 597{
 598	struct snd_pcm_runtime *runtime = substream->runtime;
 599	long buffer_size = snd_pcm_lib_buffer_bytes(substream);
 600	if (buffer_size == runtime->oss.buffer_bytes)
 601		return bytes_to_frames(runtime, bytes);
 602	return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
 603}
 604
 605static inline
 606snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
 607{
 608	return runtime->hw_ptr_interrupt;
 609}
 610
 611/* define extended formats in the recent OSS versions (if any) */
 612/* linear formats */
 613#define AFMT_S32_LE      0x00001000
 614#define AFMT_S32_BE      0x00002000
 615#define AFMT_S24_LE      0x00008000
 616#define AFMT_S24_BE      0x00010000
 617#define AFMT_S24_PACKED  0x00040000
 618
 619/* other supported formats */
 620#define AFMT_FLOAT       0x00004000
 621#define AFMT_SPDIF_RAW   0x00020000
 622
 623/* unsupported formats */
 624#define AFMT_AC3         0x00000400
 625#define AFMT_VORBIS      0x00000800
 626
 627static snd_pcm_format_t snd_pcm_oss_format_from(int format)
 628{
 629	switch (format) {
 630	case AFMT_MU_LAW:	return SNDRV_PCM_FORMAT_MU_LAW;
 631	case AFMT_A_LAW:	return SNDRV_PCM_FORMAT_A_LAW;
 632	case AFMT_IMA_ADPCM:	return SNDRV_PCM_FORMAT_IMA_ADPCM;
 633	case AFMT_U8:		return SNDRV_PCM_FORMAT_U8;
 634	case AFMT_S16_LE:	return SNDRV_PCM_FORMAT_S16_LE;
 635	case AFMT_S16_BE:	return SNDRV_PCM_FORMAT_S16_BE;
 636	case AFMT_S8:		return SNDRV_PCM_FORMAT_S8;
 637	case AFMT_U16_LE:	return SNDRV_PCM_FORMAT_U16_LE;
 638	case AFMT_U16_BE:	return SNDRV_PCM_FORMAT_U16_BE;
 639	case AFMT_MPEG:		return SNDRV_PCM_FORMAT_MPEG;
 640	case AFMT_S32_LE:	return SNDRV_PCM_FORMAT_S32_LE;
 641	case AFMT_S32_BE:	return SNDRV_PCM_FORMAT_S32_BE;
 642	case AFMT_S24_LE:	return SNDRV_PCM_FORMAT_S24_LE;
 643	case AFMT_S24_BE:	return SNDRV_PCM_FORMAT_S24_BE;
 644	case AFMT_S24_PACKED:	return SNDRV_PCM_FORMAT_S24_3LE;
 645	case AFMT_FLOAT:	return SNDRV_PCM_FORMAT_FLOAT;
 646	case AFMT_SPDIF_RAW:	return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
 647	default:		return SNDRV_PCM_FORMAT_U8;
 648	}
 649}
 650
 651static int snd_pcm_oss_format_to(snd_pcm_format_t format)
 652{
 653	switch (format) {
 654	case SNDRV_PCM_FORMAT_MU_LAW:	return AFMT_MU_LAW;
 655	case SNDRV_PCM_FORMAT_A_LAW:	return AFMT_A_LAW;
 656	case SNDRV_PCM_FORMAT_IMA_ADPCM:	return AFMT_IMA_ADPCM;
 657	case SNDRV_PCM_FORMAT_U8:		return AFMT_U8;
 658	case SNDRV_PCM_FORMAT_S16_LE:	return AFMT_S16_LE;
 659	case SNDRV_PCM_FORMAT_S16_BE:	return AFMT_S16_BE;
 660	case SNDRV_PCM_FORMAT_S8:		return AFMT_S8;
 661	case SNDRV_PCM_FORMAT_U16_LE:	return AFMT_U16_LE;
 662	case SNDRV_PCM_FORMAT_U16_BE:	return AFMT_U16_BE;
 663	case SNDRV_PCM_FORMAT_MPEG:		return AFMT_MPEG;
 664	case SNDRV_PCM_FORMAT_S32_LE:	return AFMT_S32_LE;
 665	case SNDRV_PCM_FORMAT_S32_BE:	return AFMT_S32_BE;
 666	case SNDRV_PCM_FORMAT_S24_LE:	return AFMT_S24_LE;
 667	case SNDRV_PCM_FORMAT_S24_BE:	return AFMT_S24_BE;
 668	case SNDRV_PCM_FORMAT_S24_3LE:	return AFMT_S24_PACKED;
 669	case SNDRV_PCM_FORMAT_FLOAT:	return AFMT_FLOAT;
 670	case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
 671	default:			return -EINVAL;
 672	}
 673}
 674
 675static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream, 
 676				   struct snd_pcm_hw_params *oss_params,
 677				   struct snd_pcm_hw_params *slave_params)
 678{
 679	ssize_t s;
 680	ssize_t oss_buffer_size;
 681	ssize_t oss_period_size, oss_periods;
 682	ssize_t min_period_size, max_period_size;
 683	struct snd_pcm_runtime *runtime = substream->runtime;
 684	size_t oss_frame_size;
 685
 686	oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
 687			 params_channels(oss_params) / 8;
 688
 689	oss_buffer_size = snd_pcm_hw_param_value_max(slave_params,
 690						     SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 691						     NULL);
 692	if (oss_buffer_size <= 0)
 693		return -EINVAL;
 694	oss_buffer_size = snd_pcm_plug_client_size(substream,
 695						   oss_buffer_size * oss_frame_size);
 696	if (oss_buffer_size <= 0)
 697		return -EINVAL;
 698	oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
 699	if (atomic_read(&substream->mmap_count)) {
 700		if (oss_buffer_size > runtime->oss.mmap_bytes)
 701			oss_buffer_size = runtime->oss.mmap_bytes;
 702	}
 703
 704	if (substream->oss.setup.period_size > 16)
 705		oss_period_size = substream->oss.setup.period_size;
 706	else if (runtime->oss.fragshift) {
 707		oss_period_size = 1 << runtime->oss.fragshift;
 708		if (oss_period_size > oss_buffer_size / 2)
 709			oss_period_size = oss_buffer_size / 2;
 710	} else {
 711		int sd;
 712		size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
 713
 714		oss_period_size = oss_buffer_size;
 715		do {
 716			oss_period_size /= 2;
 717		} while (oss_period_size > bytes_per_sec);
 718		if (runtime->oss.subdivision == 0) {
 719			sd = 4;
 720			if (oss_period_size / sd > 4096)
 721				sd *= 2;
 722			if (oss_period_size / sd < 4096)
 723				sd = 1;
 724		} else
 725			sd = runtime->oss.subdivision;
 726		oss_period_size /= sd;
 727		if (oss_period_size < 16)
 728			oss_period_size = 16;
 729	}
 730
 731	min_period_size = snd_pcm_plug_client_size(substream,
 732						   snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
 733	if (min_period_size > 0) {
 734		min_period_size *= oss_frame_size;
 735		min_period_size = roundup_pow_of_two(min_period_size);
 736		if (oss_period_size < min_period_size)
 737			oss_period_size = min_period_size;
 738	}
 739
 740	max_period_size = snd_pcm_plug_client_size(substream,
 741						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
 742	if (max_period_size > 0) {
 743		max_period_size *= oss_frame_size;
 744		max_period_size = rounddown_pow_of_two(max_period_size);
 745		if (oss_period_size > max_period_size)
 746			oss_period_size = max_period_size;
 747	}
 748
 749	oss_periods = oss_buffer_size / oss_period_size;
 750
 751	if (substream->oss.setup.periods > 1)
 752		oss_periods = substream->oss.setup.periods;
 753
 754	s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
 755	if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
 756		s = runtime->oss.maxfrags;
 757	if (oss_periods > s)
 758		oss_periods = s;
 759
 760	s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
 761	if (s < 2)
 762		s = 2;
 763	if (oss_periods < s)
 764		oss_periods = s;
 765
 766	while (oss_period_size * oss_periods > oss_buffer_size)
 767		oss_period_size /= 2;
 768
 769	if (oss_period_size < 16)
 770		return -EINVAL;
 771
 772	/* don't allocate too large period; 1MB period must be enough */
 773	if (oss_period_size > 1024 * 1024)
 774		return -ENOMEM;
 775
 776	runtime->oss.period_bytes = oss_period_size;
 777	runtime->oss.period_frames = 1;
 778	runtime->oss.periods = oss_periods;
 779	return 0;
 780}
 781
 782static int choose_rate(struct snd_pcm_substream *substream,
 783		       struct snd_pcm_hw_params *params, unsigned int best_rate)
 784{
 785	const struct snd_interval *it;
 786	unsigned int rate, prev;
 787
 788	struct snd_pcm_hw_params *save __free(kfree) =
 789		kmalloc(sizeof(*save), GFP_KERNEL);
 790	if (save == NULL)
 791		return -ENOMEM;
 792	*save = *params;
 793	it = hw_param_interval_c(save, SNDRV_PCM_HW_PARAM_RATE);
 794
 795	/* try multiples of the best rate */
 796	rate = best_rate;
 797	for (;;) {
 798		if (it->max < rate || (it->max == rate && it->openmax))
 799			break;
 800		if (it->min < rate || (it->min == rate && !it->openmin)) {
 801			int ret;
 802			ret = snd_pcm_hw_param_set(substream, params,
 803						   SNDRV_PCM_HW_PARAM_RATE,
 804						   rate, 0);
 805			if (ret == (int)rate)
 806				return rate;
 807			*params = *save;
 808		}
 809		prev = rate;
 810		rate += best_rate;
 811		if (rate <= prev)
 812			break;
 813	}
 814
 815	/* not found, use the nearest rate */
 816	return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
 817}
 818
 819/* parameter locking: returns immediately if tried during streaming */
 820static int lock_params(struct snd_pcm_runtime *runtime)
 821{
 822	if (mutex_lock_interruptible(&runtime->oss.params_lock))
 823		return -ERESTARTSYS;
 824	if (atomic_read(&runtime->oss.rw_ref)) {
 825		mutex_unlock(&runtime->oss.params_lock);
 826		return -EBUSY;
 827	}
 828	return 0;
 829}
 830
 831static void unlock_params(struct snd_pcm_runtime *runtime)
 832{
 833	mutex_unlock(&runtime->oss.params_lock);
 834}
 835
 836static void snd_pcm_oss_release_buffers(struct snd_pcm_substream *substream)
 837{
 838	struct snd_pcm_runtime *runtime = substream->runtime;
 839
 840	kvfree(runtime->oss.buffer);
 841	runtime->oss.buffer = NULL;
 842#ifdef CONFIG_SND_PCM_OSS_PLUGINS
 843	snd_pcm_oss_plugin_clear(substream);
 844#endif
 845}
 846
 847/* call with params_lock held */
 848static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
 849{
 850	struct snd_pcm_runtime *runtime = substream->runtime;
 851	struct snd_pcm_hw_params *params, *sparams;
 852	struct snd_pcm_sw_params *sw_params;
 853	ssize_t oss_buffer_size, oss_period_size;
 854	size_t oss_frame_size;
 855	int err;
 856	int direct;
 857	snd_pcm_format_t format, sformat;
 858	int n;
 859	const struct snd_mask *sformat_mask;
 860	struct snd_mask mask;
 861
 862	if (!runtime->oss.params)
 863		return 0;
 864	sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
 865	params = kmalloc(sizeof(*params), GFP_KERNEL);
 866	sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
 867	if (!sw_params || !params || !sparams) {
 868		err = -ENOMEM;
 869		goto failure;
 870	}
 871
 872	if (atomic_read(&substream->mmap_count))
 873		direct = 1;
 874	else
 875		direct = substream->oss.setup.direct;
 876
 877	_snd_pcm_hw_params_any(sparams);
 878	_snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
 879	_snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
 880	snd_mask_none(&mask);
 881	if (atomic_read(&substream->mmap_count))
 882		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
 883	else {
 884		snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
 885		if (!direct)
 886			snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
 887	}
 888	err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
 889	if (err < 0) {
 890		pcm_dbg(substream->pcm, "No usable accesses\n");
 891		err = -EINVAL;
 892		goto failure;
 893	}
 894
 895	err = choose_rate(substream, sparams, runtime->oss.rate);
 896	if (err < 0)
 897		goto failure;
 898	err = snd_pcm_hw_param_near(substream, sparams,
 899				    SNDRV_PCM_HW_PARAM_CHANNELS,
 900				    runtime->oss.channels, NULL);
 901	if (err < 0)
 902		goto failure;
 903
 904	format = snd_pcm_oss_format_from(runtime->oss.format);
 905
 906	sformat_mask = hw_param_mask_c(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
 907	if (direct)
 908		sformat = format;
 909	else
 910		sformat = snd_pcm_plug_slave_format(format, sformat_mask);
 911
 912	if ((__force int)sformat < 0 ||
 913	    !snd_mask_test_format(sformat_mask, sformat)) {
 914		pcm_for_each_format(sformat) {
 915			if (snd_mask_test_format(sformat_mask, sformat) &&
 916			    snd_pcm_oss_format_to(sformat) >= 0)
 917				goto format_found;
 918		}
 919		pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
 920		err = -EINVAL;
 921		goto failure;
 922	}
 923 format_found:
 924	err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
 925	if (err < 0)
 926		goto failure;
 927
 928	if (direct) {
 929		memcpy(params, sparams, sizeof(*params));
 930	} else {
 931		_snd_pcm_hw_params_any(params);
 932		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
 933				      (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
 934		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
 935				      (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
 936		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
 937				      runtime->oss.channels, 0);
 938		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
 939				      runtime->oss.rate, 0);
 940		pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
 941			 params_access(params), params_format(params),
 942			 params_channels(params), params_rate(params));
 943	}
 944	pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
 945		 params_access(sparams), params_format(sparams),
 946		 params_channels(sparams), params_rate(sparams));
 947
 948	oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
 949			 params_channels(params) / 8;
 950
 951	err = snd_pcm_oss_period_size(substream, params, sparams);
 952	if (err < 0)
 953		goto failure;
 954
 955	n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
 956	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
 957	if (err < 0)
 958		goto failure;
 959
 960	err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
 961				     runtime->oss.periods, NULL);
 962	if (err < 0)
 963		goto failure;
 964
 965	snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
 966
 967	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
 968	if (err < 0) {
 969		pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
 970		goto failure;
 971	}
 972
 973#ifdef CONFIG_SND_PCM_OSS_PLUGINS
 974	snd_pcm_oss_plugin_clear(substream);
 975	if (!direct) {
 976		/* add necessary plugins */
 977		err = snd_pcm_plug_format_plugins(substream, params, sparams);
 978		if (err < 0) {
 979			pcm_dbg(substream->pcm,
 980				"snd_pcm_plug_format_plugins failed: %i\n", err);
 981			goto failure;
 982		}
 983		if (runtime->oss.plugin_first) {
 984			struct snd_pcm_plugin *plugin;
 985			err = snd_pcm_plugin_build_io(substream, sparams, &plugin);
 986			if (err < 0) {
 987				pcm_dbg(substream->pcm,
 988					"snd_pcm_plugin_build_io failed: %i\n", err);
 989				goto failure;
 990			}
 991			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 992				err = snd_pcm_plugin_append(plugin);
 993			} else {
 994				err = snd_pcm_plugin_insert(plugin);
 995			}
 996			if (err < 0)
 997				goto failure;
 998		}
 999	}
1000#endif
1001
1002	if (runtime->oss.trigger) {
1003		sw_params->start_threshold = 1;
1004	} else {
1005		sw_params->start_threshold = runtime->boundary;
1006	}
1007	if (atomic_read(&substream->mmap_count) ||
1008	    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1009		sw_params->stop_threshold = runtime->boundary;
1010	else
1011		sw_params->stop_threshold = runtime->buffer_size;
1012	sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
1013	sw_params->period_step = 1;
1014	sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
1015		1 : runtime->period_size;
1016	if (atomic_read(&substream->mmap_count) ||
1017	    substream->oss.setup.nosilence) {
1018		sw_params->silence_threshold = 0;
1019		sw_params->silence_size = 0;
1020	} else {
1021		snd_pcm_uframes_t frames;
1022		frames = runtime->period_size + 16;
1023		if (frames > runtime->buffer_size)
1024			frames = runtime->buffer_size;
1025		sw_params->silence_threshold = frames;
1026		sw_params->silence_size = frames;
1027	}
1028
1029	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params);
1030	if (err < 0) {
1031		pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
1032		goto failure;
1033	}
1034
1035	runtime->oss.periods = params_periods(sparams);
1036	oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
1037	if (oss_period_size < 0) {
1038		err = -EINVAL;
1039		goto failure;
1040	}
1041#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1042	if (runtime->oss.plugin_first) {
1043		err = snd_pcm_plug_alloc(substream, oss_period_size);
1044		if (err < 0)
1045			goto failure;
1046	}
1047#endif
1048	oss_period_size = array_size(oss_period_size, oss_frame_size);
1049	oss_buffer_size = array_size(oss_period_size, runtime->oss.periods);
1050	if (oss_buffer_size <= 0) {
1051		err = -EINVAL;
1052		goto failure;
1053	}
1054
1055	runtime->oss.period_bytes = oss_period_size;
1056	runtime->oss.buffer_bytes = oss_buffer_size;
1057
1058	pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
1059		 runtime->oss.period_bytes,
1060		 runtime->oss.buffer_bytes);
1061	pdprintf("slave: period_size = %i, buffer_size = %i\n",
1062		 params_period_size(sparams),
1063		 params_buffer_size(sparams));
1064
1065	runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
1066	runtime->oss.channels = params_channels(params);
1067	runtime->oss.rate = params_rate(params);
1068
1069	kvfree(runtime->oss.buffer);
1070	runtime->oss.buffer = kvzalloc(runtime->oss.period_bytes, GFP_KERNEL);
1071	if (!runtime->oss.buffer) {
1072		err = -ENOMEM;
1073		goto failure;
1074	}
1075
1076	runtime->oss.params = 0;
1077	runtime->oss.prepare = 1;
1078	runtime->oss.buffer_used = 0;
1079	err = snd_pcm_runtime_buffer_set_silence(runtime);
1080	if (err < 0)
1081		goto failure;
1082
1083	runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
1084
1085	err = 0;
1086failure:
1087	if (err)
1088		snd_pcm_oss_release_buffers(substream);
1089	kfree(sw_params);
1090	kfree(params);
1091	kfree(sparams);
1092	return err;
1093}
1094
1095/* this one takes the lock by itself */
1096static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
1097				     bool trylock)
1098{
1099	struct snd_pcm_runtime *runtime = substream->runtime;
1100	int err;
1101
1102	if (trylock) {
1103		if (!(mutex_trylock(&runtime->oss.params_lock)))
1104			return -EAGAIN;
1105	} else if (mutex_lock_interruptible(&runtime->oss.params_lock))
1106		return -ERESTARTSYS;
1107
1108	err = snd_pcm_oss_change_params_locked(substream);
1109	mutex_unlock(&runtime->oss.params_lock);
1110	return err;
1111}
1112
1113static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
1114{
1115	int idx, err;
1116	struct snd_pcm_substream *asubstream = NULL, *substream;
1117
1118	for (idx = 0; idx < 2; idx++) {
1119		substream = pcm_oss_file->streams[idx];
1120		if (substream == NULL)
1121			continue;
1122		if (asubstream == NULL)
1123			asubstream = substream;
1124		if (substream->runtime->oss.params) {
1125			err = snd_pcm_oss_change_params(substream, false);
1126			if (err < 0)
1127				return err;
1128		}
1129	}
1130	if (!asubstream)
1131		return -EIO;
1132	if (r_substream)
1133		*r_substream = asubstream;
1134	return 0;
1135}
1136
1137/* call with params_lock held */
1138/* NOTE: this always call PREPARE unconditionally no matter whether
1139 * runtime->oss.prepare is set or not
1140 */
1141static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
1142{
1143	int err;
1144	struct snd_pcm_runtime *runtime = substream->runtime;
1145
1146	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
1147	if (err < 0) {
1148		pcm_dbg(substream->pcm,
1149			"snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
1150		return err;
1151	}
1152	runtime->oss.prepare = 0;
1153	runtime->oss.prev_hw_ptr_period = 0;
1154	runtime->oss.period_ptr = 0;
1155	runtime->oss.buffer_used = 0;
1156
1157	return 0;
1158}
1159
1160static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
1161{
1162	struct snd_pcm_runtime *runtime;
1163	int err;
1164
1165	runtime = substream->runtime;
1166	if (runtime->oss.params) {
1167		err = snd_pcm_oss_change_params(substream, false);
1168		if (err < 0)
1169			return err;
1170	}
1171	if (runtime->oss.prepare) {
1172		if (mutex_lock_interruptible(&runtime->oss.params_lock))
1173			return -ERESTARTSYS;
1174		err = snd_pcm_oss_prepare(substream);
1175		mutex_unlock(&runtime->oss.params_lock);
1176		if (err < 0)
1177			return err;
1178	}
1179	return 0;
1180}
1181
1182/* call with params_lock held */
1183static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
1184{
1185	struct snd_pcm_runtime *runtime;
1186	int err;
1187
1188	runtime = substream->runtime;
1189	if (runtime->oss.params) {
1190		err = snd_pcm_oss_change_params_locked(substream);
1191		if (err < 0)
1192			return err;
1193	}
1194	if (runtime->oss.prepare) {
1195		err = snd_pcm_oss_prepare(substream);
1196		if (err < 0)
1197			return err;
1198	}
1199	return 0;
1200}
1201
1202static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
1203{
1204	struct snd_pcm_runtime *runtime;
1205	snd_pcm_uframes_t frames;
1206	int err = 0;
1207
1208	while (1) {
1209		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
1210		if (err < 0)
1211			break;
1212		runtime = substream->runtime;
1213		if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
1214			break;
1215		/* in case of overrun, skip whole periods like OSS/Linux driver does */
1216		/* until avail(delay) <= buffer_size */
1217		frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
1218		frames /= runtime->period_size;
1219		frames *= runtime->period_size;
1220		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
1221		if (err < 0)
1222			break;
1223	}
1224	return err;
1225}
1226
1227snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1228{
1229	struct snd_pcm_runtime *runtime = substream->runtime;
1230	int ret;
1231	while (1) {
1232		if (runtime->state == SNDRV_PCM_STATE_XRUN ||
1233		    runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
1234#ifdef OSS_DEBUG
1235			pcm_dbg(substream->pcm,
1236				"pcm_oss: write: recovering from %s\n",
1237				runtime->state == SNDRV_PCM_STATE_XRUN ?
1238				"XRUN" : "SUSPEND");
1239#endif
1240			ret = snd_pcm_oss_prepare(substream);
1241			if (ret < 0)
1242				break;
1243		}
1244		mutex_unlock(&runtime->oss.params_lock);
1245		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1246					 frames, in_kernel);
1247		mutex_lock(&runtime->oss.params_lock);
1248		if (ret != -EPIPE && ret != -ESTRPIPE)
1249			break;
1250		/* test, if we can't store new data, because the stream */
1251		/* has not been started */
1252		if (runtime->state == SNDRV_PCM_STATE_PREPARED)
1253			return -EAGAIN;
1254	}
1255	return ret;
1256}
1257
1258snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
1259{
1260	struct snd_pcm_runtime *runtime = substream->runtime;
1261	snd_pcm_sframes_t delay;
1262	int ret;
1263	while (1) {
1264		if (runtime->state == SNDRV_PCM_STATE_XRUN ||
1265		    runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
1266#ifdef OSS_DEBUG
1267			pcm_dbg(substream->pcm,
1268				"pcm_oss: read: recovering from %s\n",
1269				runtime->state == SNDRV_PCM_STATE_XRUN ?
1270				"XRUN" : "SUSPEND");
1271#endif
1272			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1273			if (ret < 0)
1274				break;
1275		} else if (runtime->state == SNDRV_PCM_STATE_SETUP) {
1276			ret = snd_pcm_oss_prepare(substream);
1277			if (ret < 0)
1278				break;
1279		}
1280		ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
1281		if (ret < 0)
1282			break;
1283		mutex_unlock(&runtime->oss.params_lock);
1284		ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
1285					 frames, in_kernel);
1286		mutex_lock(&runtime->oss.params_lock);
1287		if (ret == -EPIPE) {
1288			if (runtime->state == SNDRV_PCM_STATE_DRAINING) {
1289				ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1290				if (ret < 0)
1291					break;
1292			}
1293			continue;
1294		}
1295		if (ret != -ESTRPIPE)
1296			break;
1297	}
1298	return ret;
1299}
1300
1301#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1302snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1303{
1304	struct snd_pcm_runtime *runtime = substream->runtime;
1305	int ret;
1306	while (1) {
1307		if (runtime->state == SNDRV_PCM_STATE_XRUN ||
1308		    runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
1309#ifdef OSS_DEBUG
1310			pcm_dbg(substream->pcm,
1311				"pcm_oss: writev: recovering from %s\n",
1312				runtime->state == SNDRV_PCM_STATE_XRUN ?
1313				"XRUN" : "SUSPEND");
1314#endif
1315			ret = snd_pcm_oss_prepare(substream);
1316			if (ret < 0)
1317				break;
1318		}
1319		ret = snd_pcm_kernel_writev(substream, bufs, frames);
1320		if (ret != -EPIPE && ret != -ESTRPIPE)
1321			break;
1322
1323		/* test, if we can't store new data, because the stream */
1324		/* has not been started */
1325		if (runtime->state == SNDRV_PCM_STATE_PREPARED)
1326			return -EAGAIN;
1327	}
1328	return ret;
1329}
1330	
1331snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
1332{
1333	struct snd_pcm_runtime *runtime = substream->runtime;
1334	int ret;
1335	while (1) {
1336		if (runtime->state == SNDRV_PCM_STATE_XRUN ||
1337		    runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
1338#ifdef OSS_DEBUG
1339			pcm_dbg(substream->pcm,
1340				"pcm_oss: readv: recovering from %s\n",
1341				runtime->state == SNDRV_PCM_STATE_XRUN ?
1342				"XRUN" : "SUSPEND");
1343#endif
1344			ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1345			if (ret < 0)
1346				break;
1347		} else if (runtime->state == SNDRV_PCM_STATE_SETUP) {
1348			ret = snd_pcm_oss_prepare(substream);
1349			if (ret < 0)
1350				break;
1351		}
1352		ret = snd_pcm_kernel_readv(substream, bufs, frames);
1353		if (ret != -EPIPE && ret != -ESTRPIPE)
1354			break;
1355	}
1356	return ret;
1357}
1358#endif /* CONFIG_SND_PCM_OSS_PLUGINS */
1359
1360static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
1361{
1362	struct snd_pcm_runtime *runtime = substream->runtime;
1363	snd_pcm_sframes_t frames, frames1;
1364#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1365	if (runtime->oss.plugin_first) {
1366		struct snd_pcm_plugin_channel *channels;
1367		size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
1368		if (!in_kernel) {
1369			if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
1370				return -EFAULT;
1371			buf = runtime->oss.buffer;
1372		}
1373		frames = bytes / oss_frame_bytes;
1374		frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
1375		if (frames1 < 0)
1376			return frames1;
1377		frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
1378		if (frames1 <= 0)
1379			return frames1;
1380		bytes = frames1 * oss_frame_bytes;
1381	} else
1382#endif
1383	{
1384		frames = bytes_to_frames(runtime, bytes);
1385		frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
1386		if (frames1 <= 0)
1387			return frames1;
1388		bytes = frames_to_bytes(runtime, frames1);
1389	}
1390	return bytes;
1391}
1392
1393static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
1394{
1395	size_t xfer = 0;
1396	ssize_t tmp = 0;
1397	struct snd_pcm_runtime *runtime = substream->runtime;
1398
1399	if (atomic_read(&substream->mmap_count))
1400		return -ENXIO;
1401
1402	atomic_inc(&runtime->oss.rw_ref);
1403	while (bytes > 0) {
1404		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1405			tmp = -ERESTARTSYS;
1406			break;
1407		}
1408		tmp = snd_pcm_oss_make_ready_locked(substream);
1409		if (tmp < 0)
1410			goto err;
1411		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1412			tmp = bytes;
1413			if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
1414				tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
1415			if (tmp > 0) {
1416				if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1417					tmp = -EFAULT;
1418					goto err;
1419				}
1420			}
1421			runtime->oss.buffer_used += tmp;
1422			buf += tmp;
1423			bytes -= tmp;
1424			xfer += tmp;
1425			if (substream->oss.setup.partialfrag ||
1426			    runtime->oss.buffer_used == runtime->oss.period_bytes) {
1427				tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr, 
1428							 runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
1429				if (tmp <= 0)
1430					goto err;
1431				runtime->oss.bytes += tmp;
1432				runtime->oss.period_ptr += tmp;
1433				runtime->oss.period_ptr %= runtime->oss.period_bytes;
1434				if (runtime->oss.period_ptr == 0 ||
1435				    runtime->oss.period_ptr == runtime->oss.buffer_used)
1436					runtime->oss.buffer_used = 0;
1437				else if ((substream->f_flags & O_NONBLOCK) != 0) {
1438					tmp = -EAGAIN;
1439					goto err;
1440				}
1441			}
1442		} else {
1443			tmp = snd_pcm_oss_write2(substream,
1444						 (const char __force *)buf,
1445						 runtime->oss.period_bytes, 0);
1446			if (tmp <= 0)
1447				goto err;
1448			runtime->oss.bytes += tmp;
1449			buf += tmp;
1450			bytes -= tmp;
1451			xfer += tmp;
1452			if ((substream->f_flags & O_NONBLOCK) != 0 &&
1453			    tmp != runtime->oss.period_bytes)
1454				tmp = -EAGAIN;
1455		}
1456 err:
1457		mutex_unlock(&runtime->oss.params_lock);
1458		if (tmp < 0)
1459			break;
1460		if (signal_pending(current)) {
1461			tmp = -ERESTARTSYS;
1462			break;
1463		}
1464		tmp = 0;
1465	}
1466	atomic_dec(&runtime->oss.rw_ref);
1467	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1468}
1469
1470static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
1471{
1472	struct snd_pcm_runtime *runtime = substream->runtime;
1473	snd_pcm_sframes_t frames, frames1;
1474#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1475	char __user *final_dst = (char __force __user *)buf;
1476	if (runtime->oss.plugin_first) {
1477		struct snd_pcm_plugin_channel *channels;
1478		size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
1479		if (!in_kernel)
1480			buf = runtime->oss.buffer;
1481		frames = bytes / oss_frame_bytes;
1482		frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
1483		if (frames1 < 0)
1484			return frames1;
1485		frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
1486		if (frames1 <= 0)
1487			return frames1;
1488		bytes = frames1 * oss_frame_bytes;
1489		if (!in_kernel && copy_to_user(final_dst, buf, bytes))
1490			return -EFAULT;
1491	} else
1492#endif
1493	{
1494		frames = bytes_to_frames(runtime, bytes);
1495		frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
1496		if (frames1 <= 0)
1497			return frames1;
1498		bytes = frames_to_bytes(runtime, frames1);
1499	}
1500	return bytes;
1501}
1502
1503static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
1504{
1505	size_t xfer = 0;
1506	ssize_t tmp = 0;
1507	struct snd_pcm_runtime *runtime = substream->runtime;
1508
1509	if (atomic_read(&substream->mmap_count))
1510		return -ENXIO;
1511
1512	atomic_inc(&runtime->oss.rw_ref);
1513	while (bytes > 0) {
1514		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1515			tmp = -ERESTARTSYS;
1516			break;
1517		}
1518		tmp = snd_pcm_oss_make_ready_locked(substream);
1519		if (tmp < 0)
1520			goto err;
1521		if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
1522			if (runtime->oss.buffer_used == 0) {
1523				tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
1524				if (tmp <= 0)
1525					goto err;
1526				runtime->oss.bytes += tmp;
1527				runtime->oss.period_ptr = tmp;
1528				runtime->oss.buffer_used = tmp;
1529			}
1530			tmp = bytes;
1531			if ((size_t) tmp > runtime->oss.buffer_used)
1532				tmp = runtime->oss.buffer_used;
1533			if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1534				tmp = -EFAULT;
1535				goto err;
1536			}
1537			buf += tmp;
1538			bytes -= tmp;
1539			xfer += tmp;
1540			runtime->oss.buffer_used -= tmp;
1541		} else {
1542			tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
1543						runtime->oss.period_bytes, 0);
1544			if (tmp <= 0)
1545				goto err;
1546			runtime->oss.bytes += tmp;
1547			buf += tmp;
1548			bytes -= tmp;
1549			xfer += tmp;
1550		}
1551 err:
1552		mutex_unlock(&runtime->oss.params_lock);
1553		if (tmp < 0)
1554			break;
1555		if (signal_pending(current)) {
1556			tmp = -ERESTARTSYS;
1557			break;
1558		}
1559		tmp = 0;
1560	}
1561	atomic_dec(&runtime->oss.rw_ref);
1562	return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1563}
1564
1565static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
1566{
1567	struct snd_pcm_substream *substream;
1568	struct snd_pcm_runtime *runtime;
1569	int i;
1570
1571	for (i = 0; i < 2; i++) { 
1572		substream = pcm_oss_file->streams[i];
1573		if (!substream)
1574			continue;
1575		runtime = substream->runtime;
1576		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1577		mutex_lock(&runtime->oss.params_lock);
1578		runtime->oss.prepare = 1;
1579		runtime->oss.buffer_used = 0;
1580		runtime->oss.prev_hw_ptr_period = 0;
1581		runtime->oss.period_ptr = 0;
1582		mutex_unlock(&runtime->oss.params_lock);
1583	}
1584	return 0;
1585}
1586
1587static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
1588{
1589	struct snd_pcm_substream *substream;
1590	int err;
1591
1592	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1593	if (substream != NULL) {
1594		err = snd_pcm_oss_make_ready(substream);
1595		if (err < 0)
1596			return err;
1597		snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
1598	}
1599	/* note: all errors from the start action are ignored */
1600	/* OSS apps do not know, how to handle them */
1601	return 0;
1602}
1603
1604static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
1605{
1606	struct snd_pcm_runtime *runtime;
1607	ssize_t result = 0;
1608	snd_pcm_state_t state;
1609	long res;
1610	wait_queue_entry_t wait;
1611
1612	runtime = substream->runtime;
1613	init_waitqueue_entry(&wait, current);
1614	add_wait_queue(&runtime->sleep, &wait);
1615#ifdef OSS_DEBUG
1616	pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
1617#endif
1618	while (1) {
1619		result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
1620		if (result > 0) {
1621			runtime->oss.buffer_used = 0;
1622			result = 0;
1623			break;
1624		}
1625		if (result != 0 && result != -EAGAIN)
1626			break;
1627		result = 0;
1628		set_current_state(TASK_INTERRUPTIBLE);
1629		scoped_guard(pcm_stream_lock_irq, substream)
1630			state = runtime->state;
1631		if (state != SNDRV_PCM_STATE_RUNNING) {
1632			set_current_state(TASK_RUNNING);
1633			break;
1634		}
1635		res = schedule_timeout(10 * HZ);
1636		if (signal_pending(current)) {
1637			result = -ERESTARTSYS;
1638			break;
1639		}
1640		if (res == 0) {
1641			pcm_err(substream->pcm,
1642				"OSS sync error - DMA timeout\n");
1643			result = -EIO;
1644			break;
1645		}
1646	}
1647	remove_wait_queue(&runtime->sleep, &wait);
1648	return result;
1649}
1650
1651static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
1652{
1653	int err = 0;
1654	unsigned int saved_f_flags;
1655	struct snd_pcm_substream *substream;
1656	struct snd_pcm_runtime *runtime;
1657	snd_pcm_format_t format;
1658	unsigned long width;
1659	size_t size;
1660
1661	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
1662	if (substream != NULL) {
1663		runtime = substream->runtime;
1664		if (atomic_read(&substream->mmap_count))
1665			goto __direct;
1666		atomic_inc(&runtime->oss.rw_ref);
1667		if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
1668			atomic_dec(&runtime->oss.rw_ref);
1669			return -ERESTARTSYS;
1670		}
1671		err = snd_pcm_oss_make_ready_locked(substream);
1672		if (err < 0)
1673			goto unlock;
1674		format = snd_pcm_oss_format_from(runtime->oss.format);
1675		width = snd_pcm_format_physical_width(format);
1676		if (runtime->oss.buffer_used > 0) {
1677#ifdef OSS_DEBUG
1678			pcm_dbg(substream->pcm, "sync: buffer_used\n");
1679#endif
1680			size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
1681			snd_pcm_format_set_silence(format,
1682						   runtime->oss.buffer + runtime->oss.buffer_used,
1683						   size);
1684			err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1685			if (err < 0)
1686				goto unlock;
1687		} else if (runtime->oss.period_ptr > 0) {
1688#ifdef OSS_DEBUG
1689			pcm_dbg(substream->pcm, "sync: period_ptr\n");
1690#endif
1691			size = runtime->oss.period_bytes - runtime->oss.period_ptr;
1692			snd_pcm_format_set_silence(format,
1693						   runtime->oss.buffer,
1694						   size * 8 / width);
1695			err = snd_pcm_oss_sync1(substream, size);
1696			if (err < 0)
1697				goto unlock;
1698		}
1699		/*
1700		 * The ALSA's period might be a bit large than OSS one.
1701		 * Fill the remain portion of ALSA period with zeros.
1702		 */
1703		size = runtime->control->appl_ptr % runtime->period_size;
1704		if (size > 0) {
1705			size = runtime->period_size - size;
1706			if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1707				snd_pcm_lib_write(substream, NULL, size);
1708			else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1709				snd_pcm_lib_writev(substream, NULL, size);
1710		}
1711unlock:
1712		mutex_unlock(&runtime->oss.params_lock);
1713		atomic_dec(&runtime->oss.rw_ref);
1714		if (err < 0)
1715			return err;
1716		/*
1717		 * finish sync: drain the buffer
1718		 */
1719	      __direct:
1720		saved_f_flags = substream->f_flags;
1721		substream->f_flags &= ~O_NONBLOCK;
1722		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
1723		substream->f_flags = saved_f_flags;
1724		if (err < 0)
1725			return err;
1726		mutex_lock(&runtime->oss.params_lock);
1727		runtime->oss.prepare = 1;
1728		mutex_unlock(&runtime->oss.params_lock);
1729	}
1730
1731	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
1732	if (substream != NULL) {
1733		err = snd_pcm_oss_make_ready(substream);
1734		if (err < 0)
1735			return err;
1736		runtime = substream->runtime;
1737		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
1738		if (err < 0)
1739			return err;
1740		mutex_lock(&runtime->oss.params_lock);
1741		runtime->oss.buffer_used = 0;
1742		runtime->oss.prepare = 1;
1743		mutex_unlock(&runtime->oss.params_lock);
1744	}
1745	return 0;
1746}
1747
1748static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
1749{
1750	int idx;
1751
1752	for (idx = 1; idx >= 0; --idx) {
1753		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1754		struct snd_pcm_runtime *runtime;
1755		int err;
1756
1757		if (substream == NULL)
1758			continue;
1759		runtime = substream->runtime;
1760		if (rate < 1000)
1761			rate = 1000;
1762		else if (rate > 192000)
1763			rate = 192000;
1764		err = lock_params(runtime);
1765		if (err < 0)
1766			return err;
1767		if (runtime->oss.rate != rate) {
1768			runtime->oss.params = 1;
1769			runtime->oss.rate = rate;
1770		}
1771		unlock_params(runtime);
1772	}
1773	return snd_pcm_oss_get_rate(pcm_oss_file);
1774}
1775
1776static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
1777{
1778	struct snd_pcm_substream *substream;
1779	int err;
1780	
1781	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1782	if (err < 0)
1783		return err;
1784	return substream->runtime->oss.rate;
1785}
1786
1787static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
1788{
1789	int idx;
1790	if (channels < 1)
1791		channels = 1;
1792	if (channels > 128)
1793		return -EINVAL;
1794	for (idx = 1; idx >= 0; --idx) {
1795		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1796		struct snd_pcm_runtime *runtime;
1797		int err;
1798
1799		if (substream == NULL)
1800			continue;
1801		runtime = substream->runtime;
1802		err = lock_params(runtime);
1803		if (err < 0)
1804			return err;
1805		if (runtime->oss.channels != channels) {
1806			runtime->oss.params = 1;
1807			runtime->oss.channels = channels;
1808		}
1809		unlock_params(runtime);
1810	}
1811	return snd_pcm_oss_get_channels(pcm_oss_file);
1812}
1813
1814static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
1815{
1816	struct snd_pcm_substream *substream;
1817	int err;
1818	
1819	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1820	if (err < 0)
1821		return err;
1822	return substream->runtime->oss.channels;
1823}
1824
1825static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
1826{
1827	struct snd_pcm_substream *substream;
1828	int err;
1829	
1830	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1831	if (err < 0)
1832		return err;
1833	return substream->runtime->oss.period_bytes;
1834}
1835
1836static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
1837{
1838	struct snd_pcm_substream *substream;
1839	int err;
1840	int direct;
1841	unsigned int formats = 0;
1842	const struct snd_mask *format_mask;
1843	int fmt;
1844
1845	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1846	if (err < 0)
1847		return err;
1848	if (atomic_read(&substream->mmap_count))
1849		direct = 1;
1850	else
1851		direct = substream->oss.setup.direct;
1852	if (!direct)
1853		return AFMT_MU_LAW | AFMT_U8 |
1854		       AFMT_S16_LE | AFMT_S16_BE |
1855		       AFMT_S8 | AFMT_U16_LE |
1856		       AFMT_U16_BE |
1857			AFMT_S32_LE | AFMT_S32_BE |
1858			AFMT_S24_LE | AFMT_S24_BE |
1859			AFMT_S24_PACKED;
1860
1861	struct snd_pcm_hw_params *params __free(kfree) =
1862		kmalloc(sizeof(*params), GFP_KERNEL);
1863	if (!params)
1864		return -ENOMEM;
1865	_snd_pcm_hw_params_any(params);
1866	err = snd_pcm_hw_refine(substream, params);
1867	if (err < 0)
1868		return err;
1869	format_mask = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
1870	for (fmt = 0; fmt < 32; ++fmt) {
1871		if (snd_mask_test(format_mask, fmt)) {
1872			int f = snd_pcm_oss_format_to((__force snd_pcm_format_t)fmt);
1873			if (f >= 0)
1874				formats |= f;
1875		}
1876	}
1877
1878	return formats;
1879}
1880
1881static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
1882{
1883	int formats, idx;
1884	int err;
1885	
1886	if (format != AFMT_QUERY) {
1887		formats = snd_pcm_oss_get_formats(pcm_oss_file);
1888		if (formats < 0)
1889			return formats;
1890		if (!(formats & format))
1891			format = AFMT_U8;
1892		for (idx = 1; idx >= 0; --idx) {
1893			struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1894			struct snd_pcm_runtime *runtime;
1895			if (substream == NULL)
1896				continue;
1897			runtime = substream->runtime;
1898			err = lock_params(runtime);
1899			if (err < 0)
1900				return err;
1901			if (runtime->oss.format != format) {
1902				runtime->oss.params = 1;
1903				runtime->oss.format = format;
1904			}
1905			unlock_params(runtime);
1906		}
1907	}
1908	return snd_pcm_oss_get_format(pcm_oss_file);
1909}
1910
1911static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
1912{
1913	struct snd_pcm_substream *substream;
1914	int err;
1915	
1916	err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
1917	if (err < 0)
1918		return err;
1919	return substream->runtime->oss.format;
1920}
1921
1922static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
1923{
1924	struct snd_pcm_runtime *runtime;
1925
1926	runtime = substream->runtime;
1927	if (subdivide == 0) {
1928		subdivide = runtime->oss.subdivision;
1929		if (subdivide == 0)
1930			subdivide = 1;
1931		return subdivide;
1932	}
1933	if (runtime->oss.subdivision || runtime->oss.fragshift)
1934		return -EINVAL;
1935	if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
1936	    subdivide != 8 && subdivide != 16)
1937		return -EINVAL;
1938	runtime->oss.subdivision = subdivide;
1939	runtime->oss.params = 1;
1940	return subdivide;
1941}
1942
1943static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
1944{
1945	int err = -EINVAL, idx;
1946
1947	for (idx = 1; idx >= 0; --idx) {
1948		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1949		struct snd_pcm_runtime *runtime;
1950
1951		if (substream == NULL)
1952			continue;
1953		runtime = substream->runtime;
1954		err = lock_params(runtime);
1955		if (err < 0)
1956			return err;
1957		err = snd_pcm_oss_set_subdivide1(substream, subdivide);
1958		unlock_params(runtime);
1959		if (err < 0)
1960			return err;
1961	}
1962	return err;
1963}
1964
1965static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
1966{
1967	struct snd_pcm_runtime *runtime;
1968	int fragshift;
1969
1970	runtime = substream->runtime;
1971	if (runtime->oss.subdivision || runtime->oss.fragshift)
1972		return -EINVAL;
1973	fragshift = val & 0xffff;
1974	if (fragshift >= 25) /* should be large enough */
1975		return -EINVAL;
1976	runtime->oss.fragshift = fragshift;
1977	runtime->oss.maxfrags = (val >> 16) & 0xffff;
1978	if (runtime->oss.fragshift < 4)		/* < 16 */
1979		runtime->oss.fragshift = 4;
1980	if (runtime->oss.maxfrags < 2)
1981		runtime->oss.maxfrags = 2;
1982	runtime->oss.params = 1;
1983	return 0;
1984}
1985
1986static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
1987{
1988	int err = -EINVAL, idx;
1989
1990	for (idx = 1; idx >= 0; --idx) {
1991		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
1992		struct snd_pcm_runtime *runtime;
1993
1994		if (substream == NULL)
1995			continue;
1996		runtime = substream->runtime;
1997		err = lock_params(runtime);
1998		if (err < 0)
1999			return err;
2000		err = snd_pcm_oss_set_fragment1(substream, val);
2001		unlock_params(runtime);
2002		if (err < 0)
2003			return err;
2004	}
2005	return err;
2006}
2007
2008static int snd_pcm_oss_nonblock(struct file * file)
2009{
2010	guard(spinlock)(&file->f_lock);
2011	file->f_flags |= O_NONBLOCK;
2012	return 0;
2013}
2014
2015static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
2016{
2017
2018	if (substream == NULL) {
2019		res &= ~DSP_CAP_DUPLEX;
2020		return res;
2021	}
2022#ifdef DSP_CAP_MULTI
2023	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2024		if (substream->pstr->substream_count > 1)
2025			res |= DSP_CAP_MULTI;
2026#endif
2027	/* DSP_CAP_REALTIME is set all times: */
2028	/* all ALSA drivers can return actual pointer in ring buffer */
2029#if defined(DSP_CAP_REALTIME) && 0
2030	{
2031		struct snd_pcm_runtime *runtime = substream->runtime;
2032		if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
2033			res &= ~DSP_CAP_REALTIME;
2034	}
2035#endif
2036	return res;
2037}
2038
2039static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
2040{
2041	int result, idx;
2042	
2043	result = DSP_CAP_TRIGGER | DSP_CAP_MMAP	| DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
2044	for (idx = 0; idx < 2; idx++) {
2045		struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
2046		result = snd_pcm_oss_get_caps1(substream, result);
2047	}
2048	result |= 0x0001;	/* revision - same as SB AWE 64 */
2049	return result;
2050}
2051
2052static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
2053				      snd_pcm_uframes_t hw_ptr)
2054{
2055	struct snd_pcm_runtime *runtime = substream->runtime;
2056	snd_pcm_uframes_t appl_ptr;
2057	appl_ptr = hw_ptr + runtime->buffer_size;
2058	appl_ptr %= runtime->boundary;
2059	runtime->control->appl_ptr = appl_ptr;
2060}
2061
2062static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
2063{
2064	struct snd_pcm_runtime *runtime;
2065	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2066	int err, cmd;
2067
2068#ifdef OSS_DEBUG
2069	pr_debug("pcm_oss: trigger = 0x%x\n", trigger);
2070#endif
2071	
2072	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2073	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2074
2075	if (psubstream) {
2076		err = snd_pcm_oss_make_ready(psubstream);
2077		if (err < 0)
2078			return err;
2079	}
2080	if (csubstream) {
2081		err = snd_pcm_oss_make_ready(csubstream);
2082		if (err < 0)
2083			return err;
2084	}
2085      	if (psubstream) {
2086      		runtime = psubstream->runtime;
2087		cmd = 0;
2088		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2089			return -ERESTARTSYS;
2090		if (trigger & PCM_ENABLE_OUTPUT) {
2091			if (runtime->oss.trigger)
2092				goto _skip1;
2093			if (atomic_read(&psubstream->mmap_count))
2094				snd_pcm_oss_simulate_fill(psubstream,
2095						get_hw_ptr_period(runtime));
2096			runtime->oss.trigger = 1;
2097			runtime->start_threshold = 1;
2098			cmd = SNDRV_PCM_IOCTL_START;
2099		} else {
2100			if (!runtime->oss.trigger)
2101				goto _skip1;
2102			runtime->oss.trigger = 0;
2103			runtime->start_threshold = runtime->boundary;
2104			cmd = SNDRV_PCM_IOCTL_DROP;
2105			runtime->oss.prepare = 1;
2106		}
2107 _skip1:
2108		mutex_unlock(&runtime->oss.params_lock);
2109		if (cmd) {
2110			err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
2111			if (err < 0)
2112				return err;
2113		}
2114	}
2115	if (csubstream) {
2116      		runtime = csubstream->runtime;
2117		cmd = 0;
2118		if (mutex_lock_interruptible(&runtime->oss.params_lock))
2119			return -ERESTARTSYS;
2120		if (trigger & PCM_ENABLE_INPUT) {
2121			if (runtime->oss.trigger)
2122				goto _skip2;
2123			runtime->oss.trigger = 1;
2124			runtime->start_threshold = 1;
2125			cmd = SNDRV_PCM_IOCTL_START;
2126		} else {
2127			if (!runtime->oss.trigger)
2128				goto _skip2;
2129			runtime->oss.trigger = 0;
2130			runtime->start_threshold = runtime->boundary;
2131			cmd = SNDRV_PCM_IOCTL_DROP;
2132			runtime->oss.prepare = 1;
2133		}
2134 _skip2:
2135		mutex_unlock(&runtime->oss.params_lock);
2136		if (cmd) {
2137			err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
2138			if (err < 0)
2139				return err;
2140		}
2141	}
2142	return 0;
2143}
2144
2145static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
2146{
2147	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2148	int result = 0;
2149
2150	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2151	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2152	if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
2153		result |= PCM_ENABLE_OUTPUT;
2154	if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
2155		result |= PCM_ENABLE_INPUT;
2156	return result;
2157}
2158
2159static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
2160{
2161	struct snd_pcm_substream *substream;
2162	struct snd_pcm_runtime *runtime;
2163	snd_pcm_sframes_t delay;
2164	int err;
2165
2166	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2167	if (substream == NULL)
2168		return -EINVAL;
2169	err = snd_pcm_oss_make_ready(substream);
2170	if (err < 0)
2171		return err;
2172	runtime = substream->runtime;
2173	if (runtime->oss.params || runtime->oss.prepare)
2174		return 0;
2175	err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2176	if (err == -EPIPE)
2177		delay = 0;	/* hack for broken OSS applications */
2178	else if (err < 0)
2179		return err;
2180	return snd_pcm_oss_bytes(substream, delay);
2181}
2182
2183static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
2184{	
2185	struct snd_pcm_substream *substream;
2186	struct snd_pcm_runtime *runtime;
2187	snd_pcm_sframes_t delay;
2188	int fixup;
2189	struct count_info info;
2190	int err;
2191
2192	if (_info == NULL)
2193		return -EFAULT;
2194	substream = pcm_oss_file->streams[stream];
2195	if (substream == NULL)
2196		return -EINVAL;
2197	err = snd_pcm_oss_make_ready(substream);
2198	if (err < 0)
2199		return err;
2200	runtime = substream->runtime;
2201	if (runtime->oss.params || runtime->oss.prepare) {
2202		memset(&info, 0, sizeof(info));
2203		if (copy_to_user(_info, &info, sizeof(info)))
2204			return -EFAULT;
2205		return 0;
2206	}
2207	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2208		err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
2209		if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
2210			err = 0;
2211			delay = 0;
2212			fixup = 0;
2213		} else {
2214			fixup = runtime->oss.buffer_used;
2215		}
2216	} else {
2217		err = snd_pcm_oss_capture_position_fixup(substream, &delay);
2218		fixup = -runtime->oss.buffer_used;
2219	}
2220	if (err < 0)
2221		return err;
2222	info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
2223	if (atomic_read(&substream->mmap_count)) {
2224		snd_pcm_sframes_t n;
2225		delay = get_hw_ptr_period(runtime);
2226		n = delay - runtime->oss.prev_hw_ptr_period;
2227		if (n < 0)
2228			n += runtime->boundary;
2229		info.blocks = n / runtime->period_size;
2230		runtime->oss.prev_hw_ptr_period = delay;
2231		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2232			snd_pcm_oss_simulate_fill(substream, delay);
2233		info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
2234	} else {
2235		delay = snd_pcm_oss_bytes(substream, delay);
2236		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2237			if (substream->oss.setup.buggyptr)
2238				info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
2239			else
2240				info.blocks = (delay + fixup) / runtime->oss.period_bytes;
2241			info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
2242		} else {
2243			delay += fixup;
2244			info.blocks = delay / runtime->oss.period_bytes;
2245			info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
2246		}
2247	}
2248	if (copy_to_user(_info, &info, sizeof(info)))
2249		return -EFAULT;
2250	return 0;
2251}
2252
2253static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
2254{
2255	struct snd_pcm_substream *substream;
2256	struct snd_pcm_runtime *runtime;
2257	snd_pcm_sframes_t avail;
2258	int fixup;
2259	struct audio_buf_info info;
2260	int err;
2261
2262	if (_info == NULL)
2263		return -EFAULT;
2264	substream = pcm_oss_file->streams[stream];
2265	if (substream == NULL)
2266		return -EINVAL;
2267	runtime = substream->runtime;
2268
2269	if (runtime->oss.params) {
2270		err = snd_pcm_oss_change_params(substream, false);
2271		if (err < 0)
2272			return err;
2273	}
2274
2275	info.fragsize = runtime->oss.period_bytes;
2276	info.fragstotal = runtime->periods;
2277	if (runtime->oss.prepare) {
2278		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2279			info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
2280			info.fragments = runtime->oss.periods;
2281		} else {
2282			info.bytes = 0;
2283			info.fragments = 0;
2284		}
2285	} else {
2286		if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
2287			err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
2288			if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
2289				avail = runtime->buffer_size;
2290				err = 0;
2291				fixup = 0;
2292			} else {
2293				avail = runtime->buffer_size - avail;
2294				fixup = -runtime->oss.buffer_used;
2295			}
2296		} else {
2297			err = snd_pcm_oss_capture_position_fixup(substream, &avail);
2298			fixup = runtime->oss.buffer_used;
2299		}
2300		if (err < 0)
2301			return err;
2302		info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
2303		info.fragments = info.bytes / runtime->oss.period_bytes;
2304	}
2305
2306#ifdef OSS_DEBUG
2307	pcm_dbg(substream->pcm,
2308		"pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
2309		info.bytes, info.fragments, info.fragstotal, info.fragsize);
2310#endif
2311	if (copy_to_user(_info, &info, sizeof(info)))
2312		return -EFAULT;
2313	return 0;
2314}
2315
2316static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
2317{
2318	// it won't be probably implemented
2319	// pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
2320	return -EINVAL;
2321}
2322
2323static const char *strip_task_path(const char *path)
2324{
2325	const char *ptr, *ptrl = NULL;
2326	for (ptr = path; *ptr; ptr++) {
2327		if (*ptr == '/')
2328			ptrl = ptr + 1;
2329	}
2330	return ptrl;
2331}
2332
2333static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
2334				      const char *task_name,
2335				      struct snd_pcm_oss_setup *rsetup)
2336{
2337	struct snd_pcm_oss_setup *setup;
2338
2339	guard(mutex)(&pcm->streams[stream].oss.setup_mutex);
2340	do {
2341		for (setup = pcm->streams[stream].oss.setup_list; setup;
2342		     setup = setup->next) {
2343			if (!strcmp(setup->task_name, task_name))
2344				goto out;
2345		}
2346	} while ((task_name = strip_task_path(task_name)) != NULL);
2347 out:
2348	if (setup)
2349		*rsetup = *setup;
2350}
2351
2352static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
2353{
2354	snd_pcm_oss_release_buffers(substream);
2355	substream->oss.oss = 0;
2356}
2357
2358static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
2359				       struct snd_pcm_oss_setup *setup,
2360				       int minor)
2361{
2362	struct snd_pcm_runtime *runtime;
2363
2364	substream->oss.oss = 1;
2365	substream->oss.setup = *setup;
2366	if (setup->nonblock)
2367		substream->f_flags |= O_NONBLOCK;
2368	else if (setup->block)
2369		substream->f_flags &= ~O_NONBLOCK;
2370	runtime = substream->runtime;
2371	runtime->oss.params = 1;
2372	runtime->oss.trigger = 1;
2373	runtime->oss.rate = 8000;
2374	mutex_init(&runtime->oss.params_lock);
2375	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
2376	case SNDRV_MINOR_OSS_PCM_8:
2377		runtime->oss.format = AFMT_U8;
2378		break;
2379	case SNDRV_MINOR_OSS_PCM_16:
2380		runtime->oss.format = AFMT_S16_LE;
2381		break;
2382	default:
2383		runtime->oss.format = AFMT_MU_LAW;
2384	}
2385	runtime->oss.channels = 1;
2386	runtime->oss.fragshift = 0;
2387	runtime->oss.maxfrags = 0;
2388	runtime->oss.subdivision = 0;
2389	substream->pcm_release = snd_pcm_oss_release_substream;
2390	atomic_set(&runtime->oss.rw_ref, 0);
2391}
2392
2393static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
2394{
2395	int cidx;
2396	if (!pcm_oss_file)
2397		return 0;
2398	for (cidx = 0; cidx < 2; ++cidx) {
2399		struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
2400		if (substream)
2401			snd_pcm_release_substream(substream);
2402	}
2403	kfree(pcm_oss_file);
2404	return 0;
2405}
2406
2407static int snd_pcm_oss_open_file(struct file *file,
2408				 struct snd_pcm *pcm,
2409				 struct snd_pcm_oss_file **rpcm_oss_file,
2410				 int minor,
2411				 struct snd_pcm_oss_setup *setup)
2412{
2413	int idx, err;
2414	struct snd_pcm_oss_file *pcm_oss_file;
2415	struct snd_pcm_substream *substream;
2416	fmode_t f_mode = file->f_mode;
2417
2418	if (rpcm_oss_file)
2419		*rpcm_oss_file = NULL;
2420
2421	pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
2422	if (pcm_oss_file == NULL)
2423		return -ENOMEM;
2424
2425	if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
2426	    (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
2427		f_mode = FMODE_WRITE;
2428
2429	file->f_flags &= ~O_APPEND;
2430	for (idx = 0; idx < 2; idx++) {
2431		if (setup[idx].disable)
2432			continue;
2433		if (! pcm->streams[idx].substream_count)
2434			continue; /* no matching substream */
2435		if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
2436			if (! (f_mode & FMODE_WRITE))
2437				continue;
2438		} else {
2439			if (! (f_mode & FMODE_READ))
2440				continue;
2441		}
2442		err = snd_pcm_open_substream(pcm, idx, file, &substream);
2443		if (err < 0) {
2444			snd_pcm_oss_release_file(pcm_oss_file);
2445			return err;
2446		}
2447
2448		pcm_oss_file->streams[idx] = substream;
2449		snd_pcm_oss_init_substream(substream, &setup[idx], minor);
2450	}
2451	
2452	if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
2453		snd_pcm_oss_release_file(pcm_oss_file);
2454		return -EINVAL;
2455	}
2456
2457	file->private_data = pcm_oss_file;
2458	if (rpcm_oss_file)
2459		*rpcm_oss_file = pcm_oss_file;
2460	return 0;
2461}
2462
2463
2464static int snd_task_name(struct task_struct *task, char *name, size_t size)
2465{
2466	unsigned int idx;
2467
2468	if (snd_BUG_ON(!task || !name || size < 2))
2469		return -EINVAL;
2470	for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
2471		name[idx] = task->comm[idx];
2472	name[idx] = '\0';
2473	return 0;
2474}
2475
2476static int snd_pcm_oss_open(struct inode *inode, struct file *file)
2477{
2478	int err;
2479	char task_name[32];
2480	struct snd_pcm *pcm;
2481	struct snd_pcm_oss_file *pcm_oss_file;
2482	struct snd_pcm_oss_setup setup[2];
2483	int nonblock;
2484	wait_queue_entry_t wait;
2485
2486	err = nonseekable_open(inode, file);
2487	if (err < 0)
2488		return err;
2489
2490	pcm = snd_lookup_oss_minor_data(iminor(inode),
2491					SNDRV_OSS_DEVICE_TYPE_PCM);
2492	if (pcm == NULL) {
2493		err = -ENODEV;
2494		goto __error1;
2495	}
2496	err = snd_card_file_add(pcm->card, file);
2497	if (err < 0)
2498		goto __error1;
2499	if (!try_module_get(pcm->card->module)) {
2500		err = -EFAULT;
2501		goto __error2;
2502	}
2503	if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
2504		err = -EFAULT;
2505		goto __error;
2506	}
2507	memset(setup, 0, sizeof(setup));
2508	if (file->f_mode & FMODE_WRITE)
2509		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2510					   task_name, &setup[0]);
2511	if (file->f_mode & FMODE_READ)
2512		snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
2513					   task_name, &setup[1]);
2514
2515	nonblock = !!(file->f_flags & O_NONBLOCK);
2516	if (!nonblock)
2517		nonblock = nonblock_open;
2518
2519	init_waitqueue_entry(&wait, current);
2520	add_wait_queue(&pcm->open_wait, &wait);
2521	mutex_lock(&pcm->open_mutex);
2522	while (1) {
2523		err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
2524					    iminor(inode), setup);
2525		if (err >= 0)
2526			break;
2527		if (err == -EAGAIN) {
2528			if (nonblock) {
2529				err = -EBUSY;
2530				break;
2531			}
2532		} else
2533			break;
2534		set_current_state(TASK_INTERRUPTIBLE);
2535		mutex_unlock(&pcm->open_mutex);
2536		schedule();
2537		mutex_lock(&pcm->open_mutex);
2538		if (pcm->card->shutdown) {
2539			err = -ENODEV;
2540			break;
2541		}
2542		if (signal_pending(current)) {
2543			err = -ERESTARTSYS;
2544			break;
2545		}
2546	}
2547	remove_wait_queue(&pcm->open_wait, &wait);
2548	mutex_unlock(&pcm->open_mutex);
2549	if (err < 0)
2550		goto __error;
2551	snd_card_unref(pcm->card);
2552	return err;
2553
2554      __error:
2555     	module_put(pcm->card->module);
2556      __error2:
2557      	snd_card_file_remove(pcm->card, file);
2558      __error1:
2559	if (pcm)
2560		snd_card_unref(pcm->card);
2561	return err;
2562}
2563
2564static int snd_pcm_oss_release(struct inode *inode, struct file *file)
2565{
2566	struct snd_pcm *pcm;
2567	struct snd_pcm_substream *substream;
2568	struct snd_pcm_oss_file *pcm_oss_file;
2569
2570	pcm_oss_file = file->private_data;
2571	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2572	if (substream == NULL)
2573		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2574	if (snd_BUG_ON(!substream))
2575		return -ENXIO;
2576	pcm = substream->pcm;
2577	if (!pcm->card->shutdown)
2578		snd_pcm_oss_sync(pcm_oss_file);
2579	mutex_lock(&pcm->open_mutex);
2580	snd_pcm_oss_release_file(pcm_oss_file);
2581	mutex_unlock(&pcm->open_mutex);
2582	wake_up(&pcm->open_wait);
2583	module_put(pcm->card->module);
2584	snd_card_file_remove(pcm->card, file);
2585	return 0;
2586}
2587
2588static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2589{
2590	struct snd_pcm_oss_file *pcm_oss_file;
2591	int __user *p = (int __user *)arg;
2592	int res;
2593
2594	pcm_oss_file = file->private_data;
2595	if (cmd == OSS_GETVERSION)
2596		return put_user(SNDRV_OSS_VERSION, p);
2597	if (cmd == OSS_ALSAEMULVER)
2598		return put_user(1, p);
2599#if IS_REACHABLE(CONFIG_SND_MIXER_OSS)
2600	if (((cmd >> 8) & 0xff) == 'M')	{	/* mixer ioctl - for OSS compatibility */
2601		struct snd_pcm_substream *substream;
2602		int idx;
2603		for (idx = 0; idx < 2; ++idx) {
2604			substream = pcm_oss_file->streams[idx];
2605			if (substream != NULL)
2606				break;
2607		}
2608		if (snd_BUG_ON(idx >= 2))
2609			return -ENXIO;
2610		return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
2611	}
2612#endif
2613	if (((cmd >> 8) & 0xff) != 'P')
2614		return -EINVAL;
2615#ifdef OSS_DEBUG
2616	pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
2617#endif
2618	switch (cmd) {
2619	case SNDCTL_DSP_RESET:
2620		return snd_pcm_oss_reset(pcm_oss_file);
2621	case SNDCTL_DSP_SYNC:
2622		return snd_pcm_oss_sync(pcm_oss_file);
2623	case SNDCTL_DSP_SPEED:
2624		if (get_user(res, p))
2625			return -EFAULT;
2626		res = snd_pcm_oss_set_rate(pcm_oss_file, res);
2627		if (res < 0)
2628			return res;
2629		return put_user(res, p);
2630	case SOUND_PCM_READ_RATE:
2631		res = snd_pcm_oss_get_rate(pcm_oss_file);
2632		if (res < 0)
2633			return res;
2634		return put_user(res, p);
2635	case SNDCTL_DSP_STEREO:
2636		if (get_user(res, p))
2637			return -EFAULT;
2638		res = res > 0 ? 2 : 1;
2639		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2640		if (res < 0)
2641			return res;
2642		return put_user(--res, p);
2643	case SNDCTL_DSP_GETBLKSIZE:
2644		res = snd_pcm_oss_get_block_size(pcm_oss_file);
2645		if (res < 0)
2646			return res;
2647		return put_user(res, p);
2648	case SNDCTL_DSP_SETFMT:
2649		if (get_user(res, p))
2650			return -EFAULT;
2651		res = snd_pcm_oss_set_format(pcm_oss_file, res);
2652		if (res < 0)
2653			return res;
2654		return put_user(res, p);
2655	case SOUND_PCM_READ_BITS:
2656		res = snd_pcm_oss_get_format(pcm_oss_file);
2657		if (res < 0)
2658			return res;
2659		return put_user(res, p);
2660	case SNDCTL_DSP_CHANNELS:
2661		if (get_user(res, p))
2662			return -EFAULT;
2663		res = snd_pcm_oss_set_channels(pcm_oss_file, res);
2664		if (res < 0)
2665			return res;
2666		return put_user(res, p);
2667	case SOUND_PCM_READ_CHANNELS:
2668		res = snd_pcm_oss_get_channels(pcm_oss_file);
2669		if (res < 0)
2670			return res;
2671		return put_user(res, p);
2672	case SOUND_PCM_WRITE_FILTER:
2673	case SOUND_PCM_READ_FILTER:
2674		return -EIO;
2675	case SNDCTL_DSP_POST:
2676		return snd_pcm_oss_post(pcm_oss_file);
2677	case SNDCTL_DSP_SUBDIVIDE:
2678		if (get_user(res, p))
2679			return -EFAULT;
2680		res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
2681		if (res < 0)
2682			return res;
2683		return put_user(res, p);
2684	case SNDCTL_DSP_SETFRAGMENT:
2685		if (get_user(res, p))
2686			return -EFAULT;
2687		return snd_pcm_oss_set_fragment(pcm_oss_file, res);
2688	case SNDCTL_DSP_GETFMTS:
2689		res = snd_pcm_oss_get_formats(pcm_oss_file);
2690		if (res < 0)
2691			return res;
2692		return put_user(res, p);
2693	case SNDCTL_DSP_GETOSPACE:
2694	case SNDCTL_DSP_GETISPACE:
2695		return snd_pcm_oss_get_space(pcm_oss_file,
2696			cmd == SNDCTL_DSP_GETISPACE ?
2697				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2698			(struct audio_buf_info __user *) arg);
2699	case SNDCTL_DSP_NONBLOCK:
2700		return snd_pcm_oss_nonblock(file);
2701	case SNDCTL_DSP_GETCAPS:
2702		res = snd_pcm_oss_get_caps(pcm_oss_file);
2703		if (res < 0)
2704			return res;
2705		return put_user(res, p);
2706	case SNDCTL_DSP_GETTRIGGER:
2707		res = snd_pcm_oss_get_trigger(pcm_oss_file);
2708		if (res < 0)
2709			return res;
2710		return put_user(res, p);
2711	case SNDCTL_DSP_SETTRIGGER:
2712		if (get_user(res, p))
2713			return -EFAULT;
2714		return snd_pcm_oss_set_trigger(pcm_oss_file, res);
2715	case SNDCTL_DSP_GETIPTR:
2716	case SNDCTL_DSP_GETOPTR:
2717		return snd_pcm_oss_get_ptr(pcm_oss_file,
2718			cmd == SNDCTL_DSP_GETIPTR ?
2719				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2720			(struct count_info __user *) arg);
2721	case SNDCTL_DSP_MAPINBUF:
2722	case SNDCTL_DSP_MAPOUTBUF:
2723		return snd_pcm_oss_get_mapbuf(pcm_oss_file,
2724			cmd == SNDCTL_DSP_MAPINBUF ?
2725				SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
2726			(struct buffmem_desc __user *) arg);
2727	case SNDCTL_DSP_SETSYNCRO:
2728		/* stop DMA now.. */
2729		return 0;
2730	case SNDCTL_DSP_SETDUPLEX:
2731		if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
2732			return 0;
2733		return -EIO;
2734	case SNDCTL_DSP_GETODELAY:
2735		res = snd_pcm_oss_get_odelay(pcm_oss_file);
2736		if (res < 0) {
2737			/* it's for sure, some broken apps don't check for error codes */
2738			put_user(0, p);
2739			return res;
2740		}
2741		return put_user(res, p);
2742	case SNDCTL_DSP_PROFILE:
2743		return 0;	/* silently ignore */
2744	default:
2745		pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
2746	}
2747	return -EINVAL;
2748}
2749
2750#ifdef CONFIG_COMPAT
2751/* all compatible */
2752static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
2753				     unsigned long arg)
2754{
2755	/*
2756	 * Everything is compatbile except SNDCTL_DSP_MAPINBUF/SNDCTL_DSP_MAPOUTBUF,
2757	 * which are not implemented for the native case either
2758	 */
2759	return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
2760}
2761#else
2762#define snd_pcm_oss_ioctl_compat	NULL
2763#endif
2764
2765static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2766{
2767	struct snd_pcm_oss_file *pcm_oss_file;
2768	struct snd_pcm_substream *substream;
2769
2770	pcm_oss_file = file->private_data;
2771	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2772	if (substream == NULL)
2773		return -ENXIO;
2774	substream->f_flags = file->f_flags & O_NONBLOCK;
2775#ifndef OSS_DEBUG
2776	return snd_pcm_oss_read1(substream, buf, count);
2777#else
2778	{
2779		ssize_t res = snd_pcm_oss_read1(substream, buf, count);
2780		pcm_dbg(substream->pcm,
2781			"pcm_oss: read %li bytes (returned %li bytes)\n",
2782			(long)count, (long)res);
2783		return res;
2784	}
2785#endif
2786}
2787
2788static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
2789{
2790	struct snd_pcm_oss_file *pcm_oss_file;
2791	struct snd_pcm_substream *substream;
2792	long result;
2793
2794	pcm_oss_file = file->private_data;
2795	substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2796	if (substream == NULL)
2797		return -ENXIO;
2798	substream->f_flags = file->f_flags & O_NONBLOCK;
2799	result = snd_pcm_oss_write1(substream, buf, count);
2800#ifdef OSS_DEBUG
2801	pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
2802	       (long)count, (long)result);
2803#endif
2804	return result;
2805}
2806
2807static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
2808{
2809	struct snd_pcm_runtime *runtime = substream->runtime;
2810	if (atomic_read(&substream->mmap_count))
2811		return runtime->oss.prev_hw_ptr_period !=
2812						get_hw_ptr_period(runtime);
2813	else
2814		return snd_pcm_playback_avail(runtime) >=
2815						runtime->oss.period_frames;
2816}
2817
2818static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
2819{
2820	struct snd_pcm_runtime *runtime = substream->runtime;
2821	if (atomic_read(&substream->mmap_count))
2822		return runtime->oss.prev_hw_ptr_period !=
2823						get_hw_ptr_period(runtime);
2824	else
2825		return snd_pcm_capture_avail(runtime) >=
2826						runtime->oss.period_frames;
2827}
2828
2829static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait)
2830{
2831	struct snd_pcm_oss_file *pcm_oss_file;
2832	__poll_t mask;
2833	struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
2834	
2835	pcm_oss_file = file->private_data;
2836
2837	psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2838	csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2839
2840	mask = 0;
2841	if (psubstream != NULL) {
2842		struct snd_pcm_runtime *runtime = psubstream->runtime;
2843		poll_wait(file, &runtime->sleep, wait);
2844		scoped_guard(pcm_stream_lock_irq, psubstream) {
2845			if (runtime->state != SNDRV_PCM_STATE_DRAINING &&
2846			    (runtime->state != SNDRV_PCM_STATE_RUNNING ||
2847			     snd_pcm_oss_playback_ready(psubstream)))
2848				mask |= EPOLLOUT | EPOLLWRNORM;
2849		}
2850	}
2851	if (csubstream != NULL) {
2852		struct snd_pcm_runtime *runtime = csubstream->runtime;
2853		snd_pcm_state_t ostate;
2854		poll_wait(file, &runtime->sleep, wait);
2855		scoped_guard(pcm_stream_lock_irq, csubstream) {
2856			ostate = runtime->state;
2857			if (ostate != SNDRV_PCM_STATE_RUNNING ||
2858			    snd_pcm_oss_capture_ready(csubstream))
2859				mask |= EPOLLIN | EPOLLRDNORM;
2860		}
2861		if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
2862			struct snd_pcm_oss_file ofile;
2863			memset(&ofile, 0, sizeof(ofile));
2864			ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2865			runtime->oss.trigger = 0;
2866			snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
2867		}
2868	}
2869
2870	return mask;
2871}
2872
2873static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
2874{
2875	struct snd_pcm_oss_file *pcm_oss_file;
2876	struct snd_pcm_substream *substream = NULL;
2877	struct snd_pcm_runtime *runtime;
2878	int err;
2879
2880#ifdef OSS_DEBUG
2881	pr_debug("pcm_oss: mmap begin\n");
2882#endif
2883	pcm_oss_file = file->private_data;
2884	switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
2885	case VM_READ | VM_WRITE:
2886		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2887		if (substream)
2888			break;
2889		fallthrough;
2890	case VM_READ:
2891		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
2892		break;
2893	case VM_WRITE:
2894		substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
2895		break;
2896	default:
2897		return -EINVAL;
2898	}
2899	/* set VM_READ access as well to fix memset() routines that do
2900	   reads before writes (to improve performance) */
2901	vm_flags_set(area, VM_READ);
2902	if (substream == NULL)
2903		return -ENXIO;
2904	runtime = substream->runtime;
2905	if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
2906		return -EIO;
2907	if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
2908		runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2909	else
2910		return -EIO;
2911	
2912	if (runtime->oss.params) {
2913		/* use mutex_trylock() for params_lock for avoiding a deadlock
2914		 * between mmap_lock and params_lock taken by
2915		 * copy_from/to_user() in snd_pcm_oss_write/read()
2916		 */
2917		err = snd_pcm_oss_change_params(substream, true);
2918		if (err < 0)
2919			return err;
2920	}
2921#ifdef CONFIG_SND_PCM_OSS_PLUGINS
2922	if (runtime->oss.plugin_first != NULL)
2923		return -EIO;
2924#endif
2925
2926	if (area->vm_pgoff != 0)
2927		return -EINVAL;
2928
2929	err = snd_pcm_mmap_data(substream, file, area);
2930	if (err < 0)
2931		return err;
2932	runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
2933	runtime->silence_threshold = 0;
2934	runtime->silence_size = 0;
2935#ifdef OSS_DEBUG
2936	pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
2937	       runtime->oss.mmap_bytes);
2938#endif
2939	/* In mmap mode we never stop */
2940	runtime->stop_threshold = runtime->boundary;
2941
2942	return 0;
2943}
2944
2945#ifdef CONFIG_SND_VERBOSE_PROCFS
2946/*
2947 *  /proc interface
2948 */
2949
2950static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
2951				  struct snd_info_buffer *buffer)
2952{
2953	struct snd_pcm_str *pstr = entry->private_data;
2954	struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
2955	guard(mutex)(&pstr->oss.setup_mutex);
2956	while (setup) {
2957		snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
2958			    setup->task_name,
2959			    setup->periods,
2960			    setup->period_size,
2961			    setup->disable ? " disable" : "",
2962			    setup->direct ? " direct" : "",
2963			    setup->block ? " block" : "",
2964			    setup->nonblock ? " non-block" : "",
2965			    setup->partialfrag ? " partial-frag" : "",
2966			    setup->nosilence ? " no-silence" : "");
2967		setup = setup->next;
2968	}
2969}
2970
2971static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
2972{
2973	struct snd_pcm_oss_setup *setup, *setupn;
2974
2975	for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
2976	     setup; setup = setupn) {
2977		setupn = setup->next;
2978		kfree(setup->task_name);
2979		kfree(setup);
2980	}
2981	pstr->oss.setup_list = NULL;
2982}
2983
2984static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
2985				   struct snd_info_buffer *buffer)
2986{
2987	struct snd_pcm_str *pstr = entry->private_data;
2988	char line[128], str[32], task_name[32];
2989	const char *ptr;
2990	int idx1;
2991	struct snd_pcm_oss_setup *setup, *setup1, template;
2992
2993	while (!snd_info_get_line(buffer, line, sizeof(line))) {
2994		guard(mutex)(&pstr->oss.setup_mutex);
2995		memset(&template, 0, sizeof(template));
2996		ptr = snd_info_get_str(task_name, line, sizeof(task_name));
2997		if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
2998			snd_pcm_oss_proc_free_setup_list(pstr);
2999			continue;
3000		}
3001		for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
3002			if (!strcmp(setup->task_name, task_name)) {
3003				template = *setup;
3004				break;
3005			}
3006		}
3007		ptr = snd_info_get_str(str, ptr, sizeof(str));
3008		template.periods = simple_strtoul(str, NULL, 10);
3009		ptr = snd_info_get_str(str, ptr, sizeof(str));
3010		template.period_size = simple_strtoul(str, NULL, 10);
3011		for (idx1 = 31; idx1 >= 0; idx1--)
3012			if (template.period_size & (1 << idx1))
3013				break;
3014		for (idx1--; idx1 >= 0; idx1--)
3015			template.period_size &= ~(1 << idx1);
3016		do {
3017			ptr = snd_info_get_str(str, ptr, sizeof(str));
3018			if (!strcmp(str, "disable")) {
3019				template.disable = 1;
3020			} else if (!strcmp(str, "direct")) {
3021				template.direct = 1;
3022			} else if (!strcmp(str, "block")) {
3023				template.block = 1;
3024			} else if (!strcmp(str, "non-block")) {
3025				template.nonblock = 1;
3026			} else if (!strcmp(str, "partial-frag")) {
3027				template.partialfrag = 1;
3028			} else if (!strcmp(str, "no-silence")) {
3029				template.nosilence = 1;
3030			} else if (!strcmp(str, "buggy-ptr")) {
3031				template.buggyptr = 1;
3032			}
3033		} while (*str);
3034		if (setup == NULL) {
3035			setup = kmalloc(sizeof(*setup), GFP_KERNEL);
3036			if (! setup) {
3037				buffer->error = -ENOMEM;
3038				return;
3039			}
3040			if (pstr->oss.setup_list == NULL)
3041				pstr->oss.setup_list = setup;
3042			else {
3043				for (setup1 = pstr->oss.setup_list;
3044				     setup1->next; setup1 = setup1->next);
3045				setup1->next = setup;
3046			}
3047			template.task_name = kstrdup(task_name, GFP_KERNEL);
3048			if (! template.task_name) {
3049				kfree(setup);
3050				buffer->error = -ENOMEM;
3051				return;
3052			}
3053		}
3054		*setup = template;
3055	}
3056}
3057
3058static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3059{
3060	int stream;
3061	for (stream = 0; stream < 2; ++stream) {
3062		struct snd_info_entry *entry;
3063		struct snd_pcm_str *pstr = &pcm->streams[stream];
3064		if (pstr->substream_count == 0)
3065			continue;
3066		entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root);
3067		if (entry) {
3068			entry->content = SNDRV_INFO_CONTENT_TEXT;
3069			entry->mode = S_IFREG | 0644;
3070			entry->c.text.read = snd_pcm_oss_proc_read;
3071			entry->c.text.write = snd_pcm_oss_proc_write;
3072			entry->private_data = pstr;
3073			if (snd_info_register(entry) < 0) {
3074				snd_info_free_entry(entry);
3075				entry = NULL;
3076			}
3077		}
3078		pstr->oss.proc_entry = entry;
3079	}
3080}
3081
3082static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3083{
3084	int stream;
3085	for (stream = 0; stream < 2; ++stream) {
3086		struct snd_pcm_str *pstr = &pcm->streams[stream];
3087		snd_info_free_entry(pstr->oss.proc_entry);
3088		pstr->oss.proc_entry = NULL;
3089		snd_pcm_oss_proc_free_setup_list(pstr);
3090	}
3091}
3092#else /* !CONFIG_SND_VERBOSE_PROCFS */
3093static inline void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
3094{
3095}
3096static inline void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
3097{
3098}
3099#endif /* CONFIG_SND_VERBOSE_PROCFS */
3100
3101/*
3102 *  ENTRY functions
3103 */
3104
3105static const struct file_operations snd_pcm_oss_f_reg =
3106{
3107	.owner =	THIS_MODULE,
3108	.read =		snd_pcm_oss_read,
3109	.write =	snd_pcm_oss_write,
3110	.open =		snd_pcm_oss_open,
3111	.release =	snd_pcm_oss_release,
3112	.poll =		snd_pcm_oss_poll,
3113	.unlocked_ioctl =	snd_pcm_oss_ioctl,
3114	.compat_ioctl =	snd_pcm_oss_ioctl_compat,
3115	.mmap =		snd_pcm_oss_mmap,
3116};
3117
3118static void register_oss_dsp(struct snd_pcm *pcm, int index)
3119{
3120	if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3121				    pcm->card, index, &snd_pcm_oss_f_reg,
3122				    pcm) < 0) {
3123		pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
3124			   pcm->card->number, pcm->device);
3125	}
3126}
3127
3128static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
3129{
3130	pcm->oss.reg = 0;
3131	if (dsp_map[pcm->card->number] == (int)pcm->device) {
3132		char name[128];
3133		int duplex;
3134		register_oss_dsp(pcm, 0);
3135		duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 && 
3136			      pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count && 
3137			      !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
3138		sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
3139#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3140		snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
3141				      pcm->card->number,
3142				      name);
3143#endif
3144		pcm->oss.reg++;
3145		pcm->oss.reg_mask |= 1;
3146	}
3147	if (adsp_map[pcm->card->number] == (int)pcm->device) {
3148		register_oss_dsp(pcm, 1);
3149		pcm->oss.reg++;
3150		pcm->oss.reg_mask |= 2;
3151	}
3152
3153	if (pcm->oss.reg)
3154		snd_pcm_oss_proc_init(pcm);
3155
3156	return 0;
3157}
3158
3159static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
3160{
3161	if (pcm->oss.reg) {
3162		if (pcm->oss.reg_mask & 1) {
3163			pcm->oss.reg_mask &= ~1;
3164			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3165						  pcm->card, 0);
3166		}
3167		if (pcm->oss.reg_mask & 2) {
3168			pcm->oss.reg_mask &= ~2;
3169			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
3170						  pcm->card, 1);
3171		}
3172		if (dsp_map[pcm->card->number] == (int)pcm->device) {
3173#ifdef SNDRV_OSS_INFO_DEV_AUDIO
3174			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
3175#endif
3176		}
3177		pcm->oss.reg = 0;
3178	}
3179	return 0;
3180}
3181
3182static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
3183{
3184	snd_pcm_oss_disconnect_minor(pcm);
3185	snd_pcm_oss_proc_done(pcm);
3186	return 0;
3187}
3188
3189static struct snd_pcm_notify snd_pcm_oss_notify =
3190{
3191	.n_register =	snd_pcm_oss_register_minor,
3192	.n_disconnect = snd_pcm_oss_disconnect_minor,
3193	.n_unregister =	snd_pcm_oss_unregister_minor,
3194};
3195
3196static int __init alsa_pcm_oss_init(void)
3197{
3198	int i;
3199	int err;
3200
3201	/* check device map table */
3202	for (i = 0; i < SNDRV_CARDS; i++) {
3203		if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
3204			pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
3205				   i, dsp_map[i]);
3206			dsp_map[i] = 0;
3207		}
3208		if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
3209			pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
3210				   i, adsp_map[i]);
3211			adsp_map[i] = 1;
3212		}
3213	}
3214	err = snd_pcm_notify(&snd_pcm_oss_notify, 0);
3215	if (err < 0)
3216		return err;
3217	return 0;
3218}
3219
3220static void __exit alsa_pcm_oss_exit(void)
3221{
3222	snd_pcm_notify(&snd_pcm_oss_notify, 1);
3223}
3224
3225module_init(alsa_pcm_oss_init)
3226module_exit(alsa_pcm_oss_exit)
~/ Read-only file preview.