Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  ALSA driver for Echoaudio soundcards.
   4 *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
   5 *  Copyright (C) 2020 Mark Hills <mark@xwax.org>
   6 */
   7
   8#include <linux/module.h>
   9
  10MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
  11MODULE_LICENSE("GPL v2");
  12MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
 
  13MODULE_DEVICE_TABLE(pci, snd_echo_ids);
  14
  15static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  16static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  17static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  18
  19module_param_array(index, int, NULL, 0444);
  20MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
  21module_param_array(id, charp, NULL, 0444);
  22MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
  23module_param_array(enable, bool, NULL, 0444);
  24MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
  25
  26static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
  27static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
  28
  29
  30
  31static int get_firmware(const struct firmware **fw_entry,
  32			struct echoaudio *chip, const short fw_index)
  33{
  34	int err;
  35	char name[30];
  36
  37#ifdef CONFIG_PM_SLEEP
  38	if (chip->fw_cache[fw_index]) {
  39		dev_dbg(chip->card->dev,
  40			"firmware requested: %s is cached\n",
  41			card_fw[fw_index].data);
  42		*fw_entry = chip->fw_cache[fw_index];
  43		return 0;
  44	}
  45#endif
  46
  47	dev_dbg(chip->card->dev,
  48		"firmware requested: %s\n", card_fw[fw_index].data);
  49	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
  50	err = request_firmware(fw_entry, name, &chip->pci->dev);
  51	if (err < 0)
  52		dev_err(chip->card->dev,
  53			"get_firmware(): Firmware not available (%d)\n", err);
  54#ifdef CONFIG_PM_SLEEP
  55	else
  56		chip->fw_cache[fw_index] = *fw_entry;
  57#endif
  58	return err;
  59}
  60
  61
  62
  63static void free_firmware(const struct firmware *fw_entry,
  64			  struct echoaudio *chip)
  65{
  66#ifdef CONFIG_PM_SLEEP
  67	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
  68#else
  69	release_firmware(fw_entry);
  70#endif
  71}
  72
  73
  74
  75static void free_firmware_cache(struct echoaudio *chip)
  76{
  77#ifdef CONFIG_PM_SLEEP
  78	int i;
  79
  80	for (i = 0; i < 8 ; i++)
  81		if (chip->fw_cache[i]) {
  82			release_firmware(chip->fw_cache[i]);
  83			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
  84		}
  85
  86#endif
  87}
  88
  89
  90
  91/******************************************************************************
  92	PCM interface
  93******************************************************************************/
  94
  95static void audiopipe_free(struct snd_pcm_runtime *runtime)
  96{
  97	struct audiopipe *pipe = runtime->private_data;
  98
  99	if (pipe->sgpage.area)
 100		snd_dma_free_pages(&pipe->sgpage);
 101	kfree(pipe);
 102}
 103
 104
 105
 106static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
 107					      struct snd_pcm_hw_rule *rule)
 108{
 109	struct snd_interval *c = hw_param_interval(params,
 110						   SNDRV_PCM_HW_PARAM_CHANNELS);
 111	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 112	struct snd_mask fmt;
 113
 114	snd_mask_any(&fmt);
 115
 116#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 117	/* >=2 channels cannot be S32_BE */
 118	if (c->min == 2) {
 119		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
 120		return snd_mask_refine(f, &fmt);
 121	}
 122#endif
 123	/* > 2 channels cannot be U8 and S32_BE */
 124	if (c->min > 2) {
 125		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
 126		return snd_mask_refine(f, &fmt);
 127	}
 128	/* Mono is ok with any format */
 129	return 0;
 130}
 131
 132
 133
 134static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
 135					      struct snd_pcm_hw_rule *rule)
 136{
 137	struct snd_interval *c = hw_param_interval(params,
 138						   SNDRV_PCM_HW_PARAM_CHANNELS);
 139	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 140	struct snd_interval ch;
 141
 142	snd_interval_any(&ch);
 143
 144	/* S32_BE is mono (and stereo) only */
 145	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
 146		ch.min = 1;
 147#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 148		ch.max = 2;
 149#else
 150		ch.max = 1;
 151#endif
 152		ch.integer = 1;
 153		return snd_interval_refine(c, &ch);
 154	}
 155	/* U8 can be only mono or stereo */
 156	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
 157		ch.min = 1;
 158		ch.max = 2;
 159		ch.integer = 1;
 160		return snd_interval_refine(c, &ch);
 161	}
 162	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
 163	return 0;
 164}
 165
 166
 167
 168static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
 169					       struct snd_pcm_hw_rule *rule)
 170{
 171	struct snd_interval *c = hw_param_interval(params,
 172						   SNDRV_PCM_HW_PARAM_CHANNELS);
 173	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 174	struct snd_mask fmt;
 175	u64 fmask;
 176	snd_mask_any(&fmt);
 177
 178	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
 179
 180	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
 181	if (c->min > 2) {
 182		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
 183			 SNDRV_PCM_FMTBIT_S24_3LE |
 184			 SNDRV_PCM_FMTBIT_S32_LE;
 185	/* 1 channel must be S32_BE or S32_LE */
 186	} else if (c->max == 1)
 187		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
 188#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 189	/* 2 channels cannot be S32_BE */
 190	else if (c->min == 2 && c->max == 2)
 191		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
 192#endif
 193	else
 194		return 0;
 195
 196	fmt.bits[0] &= (u32)fmask;
 197	fmt.bits[1] &= (u32)(fmask >> 32);
 198	return snd_mask_refine(f, &fmt);
 199}
 200
 201
 202
 203static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
 204					       struct snd_pcm_hw_rule *rule)
 205{
 206	struct snd_interval *c = hw_param_interval(params,
 207						   SNDRV_PCM_HW_PARAM_CHANNELS);
 208	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 209	struct snd_interval ch;
 210	u64 fmask;
 211
 212	snd_interval_any(&ch);
 213	ch.integer = 1;
 214	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
 215
 216	/* S32_BE is mono (and stereo) only */
 217	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
 218		ch.min = 1;
 219#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 220		ch.max = 2;
 221#else
 222		ch.max = 1;
 223#endif
 224	/* U8 is stereo only */
 225	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
 226		ch.min = ch.max = 2;
 227	/* S16_LE and S24_3LE must be at least stereo */
 228	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
 229			       SNDRV_PCM_FMTBIT_S24_3LE)))
 230		ch.min = 2;
 231	else
 232		return 0;
 233
 234	return snd_interval_refine(c, &ch);
 235}
 236
 237
 238
 239/* Since the sample rate is a global setting, do allow the user to change the
 240sample rate only if there is only one pcm device open. */
 241static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
 242			       struct snd_pcm_hw_rule *rule)
 243{
 244	struct snd_interval *rate = hw_param_interval(params,
 245						      SNDRV_PCM_HW_PARAM_RATE);
 246	struct echoaudio *chip = rule->private;
 247	struct snd_interval fixed;
 248	int err;
 249
 250	mutex_lock(&chip->mode_mutex);
 251
 252	if (chip->can_set_rate) {
 253		err = 0;
 254	} else {
 255		snd_interval_any(&fixed);
 256		fixed.min = fixed.max = chip->sample_rate;
 257		err = snd_interval_refine(rate, &fixed);
 258	}
 259
 260	mutex_unlock(&chip->mode_mutex);
 261	return err;
 262}
 263
 264
 265static int pcm_open(struct snd_pcm_substream *substream,
 266		    signed char max_channels)
 267{
 268	struct echoaudio *chip;
 269	struct snd_pcm_runtime *runtime;
 270	struct audiopipe *pipe;
 271	int err, i;
 272
 273	if (max_channels <= 0)
 274		return -EAGAIN;
 275
 276	chip = snd_pcm_substream_chip(substream);
 277	runtime = substream->runtime;
 278
 279	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
 280	if (!pipe)
 281		return -ENOMEM;
 282	pipe->index = -1;		/* Not configured yet */
 283
 284	/* Set up hw capabilities and contraints */
 285	memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
 286	dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
 287	pipe->constr.list = channels_list;
 288	pipe->constr.mask = 0;
 289	for (i = 0; channels_list[i] <= max_channels; i++);
 290	pipe->constr.count = i;
 291	if (pipe->hw.channels_max > max_channels)
 292		pipe->hw.channels_max = max_channels;
 293	if (chip->digital_mode == DIGITAL_MODE_ADAT) {
 294		pipe->hw.rate_max = 48000;
 295		pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
 296	}
 297
 298	runtime->hw = pipe->hw;
 299	runtime->private_data = pipe;
 300	runtime->private_free = audiopipe_free;
 301	snd_pcm_set_sync(substream);
 302
 303	/* Only mono and any even number of channels are allowed */
 304	err = snd_pcm_hw_constraint_list(runtime, 0,
 305					 SNDRV_PCM_HW_PARAM_CHANNELS,
 306					 &pipe->constr);
 307	if (err < 0)
 308		return err;
 309
 310	/* All periods should have the same size */
 311	err = snd_pcm_hw_constraint_integer(runtime,
 312					    SNDRV_PCM_HW_PARAM_PERIODS);
 313	if (err < 0)
 314		return err;
 315
 316	/* The hw accesses memory in chunks 32 frames long and they should be
 317	32-bytes-aligned. It's not a requirement, but it seems that IRQs are
 318	generated with a resolution of 32 frames. Thus we need the following */
 319	err = snd_pcm_hw_constraint_step(runtime, 0,
 320					 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
 321	if (err < 0)
 322		return err;
 323	err = snd_pcm_hw_constraint_step(runtime, 0,
 324					 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
 325	if (err < 0)
 326		return err;
 327
 328	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 329				  SNDRV_PCM_HW_PARAM_RATE,
 330				  hw_rule_sample_rate, chip,
 331				  SNDRV_PCM_HW_PARAM_RATE, -1);
 332	if (err < 0)
 333		return err;
 334
 335	/* Allocate a page for the scatter-gather list */
 336	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
 337				  &chip->pci->dev,
 338				  PAGE_SIZE, &pipe->sgpage);
 339	if (err < 0) {
 340		dev_err(chip->card->dev, "s-g list allocation failed\n");
 341		return err;
 342	}
 343
 344	/*
 345	 * Sole ownership required to set the rate
 346	 */
 347
 348	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
 349		chip->opencount, chip->can_set_rate, chip->rate_set);
 350
 351	chip->opencount++;
 352	if (chip->opencount > 1 && chip->rate_set)
 353		chip->can_set_rate = 0;
 354
 355	return 0;
 356}
 357
 358
 359
 360static int pcm_analog_in_open(struct snd_pcm_substream *substream)
 361{
 362	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 363	int err;
 364
 365	err = pcm_open(substream,
 366		       num_analog_busses_in(chip) - substream->number);
 367	if (err < 0)
 368		return err;
 369	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 370				  SNDRV_PCM_HW_PARAM_CHANNELS,
 371				  hw_rule_capture_channels_by_format, NULL,
 372				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
 373	if (err < 0)
 374		return err;
 375	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 376				  SNDRV_PCM_HW_PARAM_FORMAT,
 377				  hw_rule_capture_format_by_channels, NULL,
 378				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
 379	if (err < 0)
 
 
 
 
 380		return err;
 381
 382	return 0;
 383}
 384
 385
 386
 387static int pcm_analog_out_open(struct snd_pcm_substream *substream)
 388{
 389	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 390	int max_channels, err;
 391
 392#ifdef ECHOCARD_HAS_VMIXER
 393	max_channels = num_pipes_out(chip);
 394#else
 395	max_channels = num_analog_busses_out(chip);
 396#endif
 397	err = pcm_open(substream, max_channels - substream->number);
 398	if (err < 0)
 399		return err;
 400	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 401				  SNDRV_PCM_HW_PARAM_CHANNELS,
 402				  hw_rule_playback_channels_by_format,
 403				  NULL,
 404				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
 405	if (err < 0)
 406		return err;
 407	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 408				  SNDRV_PCM_HW_PARAM_FORMAT,
 409				  hw_rule_playback_format_by_channels,
 410				  NULL,
 411				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
 412	if (err < 0)
 
 
 
 
 
 413		return err;
 414
 415	return 0;
 416}
 417
 418
 419
 420#ifdef ECHOCARD_HAS_DIGITAL_IO
 421
 422static int pcm_digital_in_open(struct snd_pcm_substream *substream)
 423{
 424	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 425	int err, max_channels;
 426
 427	max_channels = num_digital_busses_in(chip) - substream->number;
 428	mutex_lock(&chip->mode_mutex);
 429	if (chip->digital_mode == DIGITAL_MODE_ADAT)
 430		err = pcm_open(substream, max_channels);
 431	else	/* If the card has ADAT, subtract the 6 channels
 432		 * that S/PDIF doesn't have
 433		 */
 434		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
 435
 436	if (err < 0)
 437		goto din_exit;
 438
 439	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 440				  SNDRV_PCM_HW_PARAM_CHANNELS,
 441				  hw_rule_capture_channels_by_format, NULL,
 442				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
 443	if (err < 0)
 444		goto din_exit;
 445	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 446				  SNDRV_PCM_HW_PARAM_FORMAT,
 447				  hw_rule_capture_format_by_channels, NULL,
 448				  SNDRV_PCM_HW_PARAM_CHANNELS, -1);
 449	if (err < 0)
 450		goto din_exit;
 451
 452din_exit:
 453	mutex_unlock(&chip->mode_mutex);
 454	return err;
 455}
 456
 457
 458
 459#ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
 460
 461static int pcm_digital_out_open(struct snd_pcm_substream *substream)
 462{
 463	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 464	int err, max_channels;
 465
 466	max_channels = num_digital_busses_out(chip) - substream->number;
 467	mutex_lock(&chip->mode_mutex);
 468	if (chip->digital_mode == DIGITAL_MODE_ADAT)
 469		err = pcm_open(substream, max_channels);
 470	else	/* If the card has ADAT, subtract the 6 channels
 471		 * that S/PDIF doesn't have
 472		 */
 473		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
 474
 475	if (err < 0)
 476		goto dout_exit;
 477
 478	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 479				  SNDRV_PCM_HW_PARAM_CHANNELS,
 480				  hw_rule_playback_channels_by_format,
 481				  NULL, SNDRV_PCM_HW_PARAM_FORMAT,
 482				  -1);
 483	if (err < 0)
 484		goto dout_exit;
 485	err = snd_pcm_hw_rule_add(substream->runtime, 0,
 486				  SNDRV_PCM_HW_PARAM_FORMAT,
 487				  hw_rule_playback_format_by_channels,
 488				  NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
 489				  -1);
 490	if (err < 0)
 491		goto dout_exit;
 492
 493dout_exit:
 494	mutex_unlock(&chip->mode_mutex);
 495	return err;
 496}
 497
 498#endif /* !ECHOCARD_HAS_VMIXER */
 499
 500#endif /* ECHOCARD_HAS_DIGITAL_IO */
 501
 502
 503
 504static int pcm_close(struct snd_pcm_substream *substream)
 505{
 506	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 507
 508	/* Nothing to do here. Audio is already off and pipe will be
 509	 * freed by its callback
 510	 */
 511
 512	mutex_lock(&chip->mode_mutex);
 513
 514	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
 515		chip->opencount, chip->can_set_rate, chip->rate_set);
 516
 517	chip->opencount--;
 518
 519	switch (chip->opencount) {
 520	case 1:
 521		chip->can_set_rate = 1;
 522		break;
 523
 524	case 0:
 525		chip->rate_set = 0;
 526		break;
 527	}
 528
 529	mutex_unlock(&chip->mode_mutex);
 530	return 0;
 531}
 532
 533
 534
 535/* Channel allocation and scatter-gather list setup */
 536static int init_engine(struct snd_pcm_substream *substream,
 537		       struct snd_pcm_hw_params *hw_params,
 538		       int pipe_index, int interleave)
 539{
 540	struct echoaudio *chip;
 541	int err, per, rest, page, edge, offs;
 542	struct audiopipe *pipe;
 543
 544	chip = snd_pcm_substream_chip(substream);
 545	pipe = (struct audiopipe *) substream->runtime->private_data;
 546
 547	/* Sets up che hardware. If it's already initialized, reset and
 548	 * redo with the new parameters
 549	 */
 550	spin_lock_irq(&chip->lock);
 551	if (pipe->index >= 0) {
 552		dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
 553		err = free_pipes(chip, pipe);
 554		snd_BUG_ON(err);
 555		chip->substream[pipe->index] = NULL;
 556	}
 557
 558	err = allocate_pipes(chip, pipe, pipe_index, interleave);
 559	if (err < 0) {
 560		spin_unlock_irq(&chip->lock);
 561		dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
 562			pipe_index, err);
 563		return err;
 564	}
 565	spin_unlock_irq(&chip->lock);
 566	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
 567
 568	dev_dbg(chip->card->dev,
 569		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
 570		params_buffer_bytes(hw_params), params_periods(hw_params),
 571		params_period_bytes(hw_params));
 572
 573	sglist_init(chip, pipe);
 574	edge = PAGE_SIZE;
 575	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
 576	     per++) {
 577		rest = params_period_bytes(hw_params);
 578		if (offs + rest > params_buffer_bytes(hw_params))
 579			rest = params_buffer_bytes(hw_params) - offs;
 580		while (rest) {
 581			dma_addr_t addr;
 582			addr = snd_pcm_sgbuf_get_addr(substream, offs);
 583			if (rest <= edge - offs) {
 584				sglist_add_mapping(chip, pipe, addr, rest);
 585				sglist_add_irq(chip, pipe);
 586				offs += rest;
 587				rest = 0;
 588			} else {
 589				sglist_add_mapping(chip, pipe, addr,
 590						   edge - offs);
 591				rest -= edge - offs;
 592				offs = edge;
 593			}
 594			if (offs == edge) {
 595				edge += PAGE_SIZE;
 596				page++;
 597			}
 598		}
 599	}
 600
 601	/* Close the ring buffer */
 602	sglist_wrap(chip, pipe);
 603
 604	/* This stuff is used by the irq handler, so it must be
 605	 * initialized before chip->substream
 606	 */
 607	pipe->last_period = 0;
 608	pipe->last_counter = 0;
 609	pipe->position = 0;
 610	smp_wmb();
 611	chip->substream[pipe_index] = substream;
 612	chip->rate_set = 1;
 613	spin_lock_irq(&chip->lock);
 614	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
 615	spin_unlock_irq(&chip->lock);
 616	return 0;
 617}
 618
 619
 620
 621static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
 622				   struct snd_pcm_hw_params *hw_params)
 623{
 624	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 625
 626	return init_engine(substream, hw_params, px_analog_in(chip) +
 627			substream->number, params_channels(hw_params));
 628}
 629
 630
 631
 632static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
 633				    struct snd_pcm_hw_params *hw_params)
 634{
 635	return init_engine(substream, hw_params, substream->number,
 636			   params_channels(hw_params));
 637}
 638
 639
 640
 641#ifdef ECHOCARD_HAS_DIGITAL_IO
 642
 643static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
 644				    struct snd_pcm_hw_params *hw_params)
 645{
 646	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 647
 648	return init_engine(substream, hw_params, px_digital_in(chip) +
 649			substream->number, params_channels(hw_params));
 650}
 651
 652
 653
 654#ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
 655static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
 656				     struct snd_pcm_hw_params *hw_params)
 657{
 658	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 659
 660	return init_engine(substream, hw_params, px_digital_out(chip) +
 661			substream->number, params_channels(hw_params));
 662}
 663#endif /* !ECHOCARD_HAS_VMIXER */
 664
 665#endif /* ECHOCARD_HAS_DIGITAL_IO */
 666
 667
 668
 669static int pcm_hw_free(struct snd_pcm_substream *substream)
 670{
 671	struct echoaudio *chip;
 672	struct audiopipe *pipe;
 673
 674	chip = snd_pcm_substream_chip(substream);
 675	pipe = (struct audiopipe *) substream->runtime->private_data;
 676
 677	spin_lock_irq(&chip->lock);
 678	if (pipe->index >= 0) {
 679		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
 680		free_pipes(chip, pipe);
 681		chip->substream[pipe->index] = NULL;
 682		pipe->index = -1;
 683	}
 684	spin_unlock_irq(&chip->lock);
 685
 686	return 0;
 687}
 688
 689
 690
 691static int pcm_prepare(struct snd_pcm_substream *substream)
 692{
 693	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 694	struct snd_pcm_runtime *runtime = substream->runtime;
 695	struct audioformat format;
 696	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
 697
 698	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
 699		runtime->rate, runtime->format, runtime->channels);
 700	format.interleave = runtime->channels;
 701	format.data_are_bigendian = 0;
 702	format.mono_to_stereo = 0;
 703	switch (runtime->format) {
 704	case SNDRV_PCM_FORMAT_U8:
 705		format.bits_per_sample = 8;
 706		break;
 707	case SNDRV_PCM_FORMAT_S16_LE:
 708		format.bits_per_sample = 16;
 709		break;
 710	case SNDRV_PCM_FORMAT_S24_3LE:
 711		format.bits_per_sample = 24;
 712		break;
 713	case SNDRV_PCM_FORMAT_S32_BE:
 714		format.data_are_bigendian = 1;
 715		fallthrough;
 716	case SNDRV_PCM_FORMAT_S32_LE:
 717		format.bits_per_sample = 32;
 718		break;
 719	default:
 720		dev_err(chip->card->dev,
 721			"Prepare error: unsupported format %d\n",
 722			runtime->format);
 723		return -EINVAL;
 724	}
 725
 726	if (snd_BUG_ON(pipe_index >= px_num(chip)))
 727		return -EINVAL;
 728
 729	/*
 730	 * We passed checks we can do independently; now take
 731	 * exclusive control
 732	 */
 733
 734	spin_lock_irq(&chip->lock);
 735
 736	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
 737		spin_unlock_irq(&chip->lock);
 738		return -EINVAL;
 739	}
 740
 741	set_audio_format(chip, pipe_index, &format);
 742	spin_unlock_irq(&chip->lock);
 743
 744	return 0;
 745}
 746
 747
 748
 749static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 750{
 751	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 752	struct audiopipe *pipe;
 753	int i, err;
 754	u32 channelmask = 0;
 755	struct snd_pcm_substream *s;
 756
 757	snd_pcm_group_for_each_entry(s, substream) {
 758		for (i = 0; i < DSP_MAXPIPES; i++) {
 759			if (s == chip->substream[i]) {
 760				channelmask |= 1 << i;
 761				snd_pcm_trigger_done(s, substream);
 762			}
 763		}
 764	}
 765
 766	spin_lock(&chip->lock);
 767	switch (cmd) {
 768	case SNDRV_PCM_TRIGGER_RESUME:
 769	case SNDRV_PCM_TRIGGER_START:
 770	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 771		for (i = 0; i < DSP_MAXPIPES; i++) {
 772			if (channelmask & (1 << i)) {
 773				pipe = chip->substream[i]->runtime->private_data;
 774				switch (pipe->state) {
 775				case PIPE_STATE_STOPPED:
 776					pipe->last_period = 0;
 777					pipe->last_counter = 0;
 778					pipe->position = 0;
 779					*pipe->dma_counter = 0;
 780					fallthrough;
 781				case PIPE_STATE_PAUSED:
 782					pipe->state = PIPE_STATE_STARTED;
 783					break;
 784				case PIPE_STATE_STARTED:
 785					break;
 786				}
 787			}
 788		}
 789		err = start_transport(chip, channelmask,
 790				      chip->pipe_cyclic_mask);
 791		break;
 792	case SNDRV_PCM_TRIGGER_SUSPEND:
 793	case SNDRV_PCM_TRIGGER_STOP:
 794		for (i = 0; i < DSP_MAXPIPES; i++) {
 795			if (channelmask & (1 << i)) {
 796				pipe = chip->substream[i]->runtime->private_data;
 797				pipe->state = PIPE_STATE_STOPPED;
 798			}
 799		}
 800		err = stop_transport(chip, channelmask);
 801		break;
 802	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 803		for (i = 0; i < DSP_MAXPIPES; i++) {
 804			if (channelmask & (1 << i)) {
 805				pipe = chip->substream[i]->runtime->private_data;
 806				pipe->state = PIPE_STATE_PAUSED;
 807			}
 808		}
 809		err = pause_transport(chip, channelmask);
 810		break;
 811	default:
 812		err = -EINVAL;
 813	}
 814	spin_unlock(&chip->lock);
 815	return err;
 816}
 817
 818
 819
 820static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
 821{
 822	struct snd_pcm_runtime *runtime = substream->runtime;
 823	struct audiopipe *pipe = runtime->private_data;
 824	u32 counter, step;
 825
 826	/*
 827	 * IRQ handling runs concurrently. Do not share tracking of
 828	 * counter with it, which would race or require locking
 829	 */
 830
 831	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
 832
 833	step = counter - pipe->last_counter;  /* handles wrapping */
 834	pipe->last_counter = counter;
 835
 836	/* counter doesn't neccessarily wrap on a multiple of
 837	 * buffer_size, so can't derive the position; must
 838	 * accumulate */
 839
 840	pipe->position += step;
 841	pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
 842
 843	return bytes_to_frames(runtime, pipe->position);
 844}
 845
 846
 847
 848/* pcm *_ops structures */
 849static const struct snd_pcm_ops analog_playback_ops = {
 850	.open = pcm_analog_out_open,
 851	.close = pcm_close,
 852	.hw_params = pcm_analog_out_hw_params,
 853	.hw_free = pcm_hw_free,
 854	.prepare = pcm_prepare,
 855	.trigger = pcm_trigger,
 856	.pointer = pcm_pointer,
 857};
 858static const struct snd_pcm_ops analog_capture_ops = {
 859	.open = pcm_analog_in_open,
 860	.close = pcm_close,
 861	.hw_params = pcm_analog_in_hw_params,
 862	.hw_free = pcm_hw_free,
 863	.prepare = pcm_prepare,
 864	.trigger = pcm_trigger,
 865	.pointer = pcm_pointer,
 866};
 867#ifdef ECHOCARD_HAS_DIGITAL_IO
 868#ifndef ECHOCARD_HAS_VMIXER
 869static const struct snd_pcm_ops digital_playback_ops = {
 870	.open = pcm_digital_out_open,
 871	.close = pcm_close,
 872	.hw_params = pcm_digital_out_hw_params,
 873	.hw_free = pcm_hw_free,
 874	.prepare = pcm_prepare,
 875	.trigger = pcm_trigger,
 876	.pointer = pcm_pointer,
 877};
 878#endif /* !ECHOCARD_HAS_VMIXER */
 879static const struct snd_pcm_ops digital_capture_ops = {
 880	.open = pcm_digital_in_open,
 881	.close = pcm_close,
 882	.hw_params = pcm_digital_in_hw_params,
 883	.hw_free = pcm_hw_free,
 884	.prepare = pcm_prepare,
 885	.trigger = pcm_trigger,
 886	.pointer = pcm_pointer,
 887};
 888#endif /* ECHOCARD_HAS_DIGITAL_IO */
 889
 890
 891
 892/* Preallocate memory only for the first substream because it's the most
 893 * used one
 894 */
 895static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
 896{
 897	struct snd_pcm_substream *ss;
 898	int stream;
 899
 900	for (stream = 0; stream < 2; stream++)
 901		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
 902			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
 903						   dev,
 904						   ss->number ? 0 : 128<<10,
 905						   256<<10);
 906}
 907
 908
 909
 910/*<--snd_echo_probe() */
 911static int snd_echo_new_pcm(struct echoaudio *chip)
 912{
 913	struct snd_pcm *pcm;
 914	int err;
 915
 916#ifdef ECHOCARD_HAS_VMIXER
 917	/* This card has a Vmixer, that is there is no direct mapping from PCM
 918	streams to physical outputs. The user can mix the streams as he wishes
 919	via control interface and it's possible to send any stream to any
 920	output, thus it makes no sense to keep analog and digital outputs
 921	separated */
 922
 923	/* PCM#0 Virtual outputs and analog inputs */
 924	err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
 925			  num_analog_busses_in(chip), &pcm);
 926	if (err < 0)
 927		return err;
 928	pcm->private_data = chip;
 929	chip->analog_pcm = pcm;
 930	strcpy(pcm->name, chip->card->shortname);
 931	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
 932	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
 933	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 934
 935#ifdef ECHOCARD_HAS_DIGITAL_IO
 936	/* PCM#1 Digital inputs, no outputs */
 937	err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
 938			  num_digital_busses_in(chip), &pcm);
 939	if (err < 0)
 940		return err;
 941	pcm->private_data = chip;
 942	chip->digital_pcm = pcm;
 943	strcpy(pcm->name, chip->card->shortname);
 944	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
 945	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 946#endif /* ECHOCARD_HAS_DIGITAL_IO */
 947
 948#else /* ECHOCARD_HAS_VMIXER */
 949
 950	/* The card can manage substreams formed by analog and digital channels
 951	at the same time, but I prefer to keep analog and digital channels
 952	separated, because that mixed thing is confusing and useless. So we
 953	register two PCM devices: */
 954
 955	/* PCM#0 Analog i/o */
 956	err = snd_pcm_new(chip->card, "Analog PCM", 0,
 957			  num_analog_busses_out(chip),
 958			  num_analog_busses_in(chip), &pcm);
 959	if (err < 0)
 960		return err;
 961	pcm->private_data = chip;
 962	chip->analog_pcm = pcm;
 963	strcpy(pcm->name, chip->card->shortname);
 964	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
 965	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
 966	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 967
 968#ifdef ECHOCARD_HAS_DIGITAL_IO
 969	/* PCM#1 Digital i/o */
 970	err = snd_pcm_new(chip->card, "Digital PCM", 1,
 971			  num_digital_busses_out(chip),
 972			  num_digital_busses_in(chip), &pcm);
 973	if (err < 0)
 974		return err;
 975	pcm->private_data = chip;
 976	chip->digital_pcm = pcm;
 977	strcpy(pcm->name, chip->card->shortname);
 978	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
 979	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
 980	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 981#endif /* ECHOCARD_HAS_DIGITAL_IO */
 982
 983#endif /* ECHOCARD_HAS_VMIXER */
 984
 985	return 0;
 986}
 987
 988
 989
 990
 991/******************************************************************************
 992	Control interface
 993******************************************************************************/
 994
 995#if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
 996
 997/******************* PCM output volume *******************/
 998static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
 999				     struct snd_ctl_elem_info *uinfo)
1000{
1001	struct echoaudio *chip;
1002
1003	chip = snd_kcontrol_chip(kcontrol);
1004	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1005	uinfo->count = num_busses_out(chip);
1006	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1007	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1008	return 0;
1009}
1010
1011static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1012				    struct snd_ctl_elem_value *ucontrol)
1013{
1014	struct echoaudio *chip;
1015	int c;
1016
1017	chip = snd_kcontrol_chip(kcontrol);
1018	for (c = 0; c < num_busses_out(chip); c++)
1019		ucontrol->value.integer.value[c] = chip->output_gain[c];
1020	return 0;
1021}
1022
1023static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1024				    struct snd_ctl_elem_value *ucontrol)
1025{
1026	struct echoaudio *chip;
1027	int c, changed, gain;
1028
1029	changed = 0;
1030	chip = snd_kcontrol_chip(kcontrol);
1031	spin_lock_irq(&chip->lock);
1032	for (c = 0; c < num_busses_out(chip); c++) {
1033		gain = ucontrol->value.integer.value[c];
1034		/* Ignore out of range values */
1035		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1036			continue;
1037		if (chip->output_gain[c] != gain) {
1038			set_output_gain(chip, c, gain);
1039			changed = 1;
1040		}
1041	}
1042	if (changed)
1043		update_output_line_level(chip);
1044	spin_unlock_irq(&chip->lock);
1045	return changed;
1046}
1047
1048#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1049/* On the Mia this one controls the line-out volume */
1050static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1051	.name = "Line Playback Volume",
1052	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1053	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1054		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1055	.info = snd_echo_output_gain_info,
1056	.get = snd_echo_output_gain_get,
1057	.put = snd_echo_output_gain_put,
1058	.tlv = {.p = db_scale_output_gain},
1059};
1060#else
1061static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1062	.name = "PCM Playback Volume",
1063	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1064	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1065	.info = snd_echo_output_gain_info,
1066	.get = snd_echo_output_gain_get,
1067	.put = snd_echo_output_gain_put,
1068	.tlv = {.p = db_scale_output_gain},
1069};
1070#endif
1071
1072#endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1073
1074
1075
1076#ifdef ECHOCARD_HAS_INPUT_GAIN
1077
1078/******************* Analog input volume *******************/
1079static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1080				    struct snd_ctl_elem_info *uinfo)
1081{
1082	struct echoaudio *chip;
1083
1084	chip = snd_kcontrol_chip(kcontrol);
1085	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1086	uinfo->count = num_analog_busses_in(chip);
1087	uinfo->value.integer.min = ECHOGAIN_MININP;
1088	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1089	return 0;
1090}
1091
1092static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1093				   struct snd_ctl_elem_value *ucontrol)
1094{
1095	struct echoaudio *chip;
1096	int c;
1097
1098	chip = snd_kcontrol_chip(kcontrol);
1099	for (c = 0; c < num_analog_busses_in(chip); c++)
1100		ucontrol->value.integer.value[c] = chip->input_gain[c];
1101	return 0;
1102}
1103
1104static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1105				   struct snd_ctl_elem_value *ucontrol)
1106{
1107	struct echoaudio *chip;
1108	int c, gain, changed;
1109
1110	changed = 0;
1111	chip = snd_kcontrol_chip(kcontrol);
1112	spin_lock_irq(&chip->lock);
1113	for (c = 0; c < num_analog_busses_in(chip); c++) {
1114		gain = ucontrol->value.integer.value[c];
1115		/* Ignore out of range values */
1116		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1117			continue;
1118		if (chip->input_gain[c] != gain) {
1119			set_input_gain(chip, c, gain);
1120			changed = 1;
1121		}
1122	}
1123	if (changed)
1124		update_input_line_level(chip);
1125	spin_unlock_irq(&chip->lock);
1126	return changed;
1127}
1128
1129static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1130
1131static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1132	.name = "Line Capture Volume",
1133	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1135	.info = snd_echo_input_gain_info,
1136	.get = snd_echo_input_gain_get,
1137	.put = snd_echo_input_gain_put,
1138	.tlv = {.p = db_scale_input_gain},
1139};
1140
1141#endif /* ECHOCARD_HAS_INPUT_GAIN */
1142
1143
1144
1145#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1146
1147/************ Analog output nominal level (+4dBu / -10dBV) ***************/
1148static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1149					 struct snd_ctl_elem_info *uinfo)
1150{
1151	struct echoaudio *chip;
1152
1153	chip = snd_kcontrol_chip(kcontrol);
1154	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1155	uinfo->count = num_analog_busses_out(chip);
1156	uinfo->value.integer.min = 0;
1157	uinfo->value.integer.max = 1;
1158	return 0;
1159}
1160
1161static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1162				       struct snd_ctl_elem_value *ucontrol)
1163{
1164	struct echoaudio *chip;
1165	int c;
1166
1167	chip = snd_kcontrol_chip(kcontrol);
1168	for (c = 0; c < num_analog_busses_out(chip); c++)
1169		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1170	return 0;
1171}
1172
1173static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1174				       struct snd_ctl_elem_value *ucontrol)
1175{
1176	struct echoaudio *chip;
1177	int c, changed;
1178
1179	changed = 0;
1180	chip = snd_kcontrol_chip(kcontrol);
1181	spin_lock_irq(&chip->lock);
1182	for (c = 0; c < num_analog_busses_out(chip); c++) {
1183		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1184			set_nominal_level(chip, c,
1185					  ucontrol->value.integer.value[c]);
1186			changed = 1;
1187		}
1188	}
1189	if (changed)
1190		update_output_line_level(chip);
1191	spin_unlock_irq(&chip->lock);
1192	return changed;
1193}
1194
1195static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1196	.name = "Line Playback Switch (-10dBV)",
1197	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1198	.info = snd_echo_output_nominal_info,
1199	.get = snd_echo_output_nominal_get,
1200	.put = snd_echo_output_nominal_put,
1201};
1202
1203#endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1204
1205
1206
1207#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1208
1209/*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1210static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1211				       struct snd_ctl_elem_info *uinfo)
1212{
1213	struct echoaudio *chip;
1214
1215	chip = snd_kcontrol_chip(kcontrol);
1216	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1217	uinfo->count = num_analog_busses_in(chip);
1218	uinfo->value.integer.min = 0;
1219	uinfo->value.integer.max = 1;
1220	return 0;
1221}
1222
1223static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1224				      struct snd_ctl_elem_value *ucontrol)
1225{
1226	struct echoaudio *chip;
1227	int c;
1228
1229	chip = snd_kcontrol_chip(kcontrol);
1230	for (c = 0; c < num_analog_busses_in(chip); c++)
1231		ucontrol->value.integer.value[c] =
1232			chip->nominal_level[bx_analog_in(chip) + c];
1233	return 0;
1234}
1235
1236static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1237				      struct snd_ctl_elem_value *ucontrol)
1238{
1239	struct echoaudio *chip;
1240	int c, changed;
1241
1242	changed = 0;
1243	chip = snd_kcontrol_chip(kcontrol);
1244	spin_lock_irq(&chip->lock);
1245	for (c = 0; c < num_analog_busses_in(chip); c++) {
1246		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1247		    ucontrol->value.integer.value[c]) {
1248			set_nominal_level(chip, bx_analog_in(chip) + c,
1249					  ucontrol->value.integer.value[c]);
1250			changed = 1;
1251		}
1252	}
1253	if (changed)
1254		update_output_line_level(chip);	/* "Output" is not a mistake
1255						 * here.
1256						 */
1257	spin_unlock_irq(&chip->lock);
1258	return changed;
1259}
1260
1261static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1262	.name = "Line Capture Switch (-10dBV)",
1263	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1264	.info = snd_echo_input_nominal_info,
1265	.get = snd_echo_input_nominal_get,
1266	.put = snd_echo_input_nominal_put,
1267};
1268
1269#endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1270
1271
1272
1273#ifdef ECHOCARD_HAS_MONITOR
1274
1275/******************* Monitor mixer *******************/
1276static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1277			       struct snd_ctl_elem_info *uinfo)
1278{
1279	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1280	uinfo->count = 1;
1281	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1282	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1283	return 0;
1284}
1285
1286static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1287			      struct snd_ctl_elem_value *ucontrol)
1288{
1289	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1290	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1291	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1292
1293	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1294		return -EINVAL;
1295
1296	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1297	return 0;
1298}
1299
1300static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1301			      struct snd_ctl_elem_value *ucontrol)
1302{
1303	struct echoaudio *chip;
1304	int changed,  gain;
1305	unsigned int out, in;
1306
1307	changed = 0;
1308	chip = snd_kcontrol_chip(kcontrol);
1309	out = ucontrol->id.index / num_busses_in(chip);
1310	in = ucontrol->id.index % num_busses_in(chip);
1311	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1312		return -EINVAL;
1313	gain = ucontrol->value.integer.value[0];
1314	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1315		return -EINVAL;
1316	if (chip->monitor_gain[out][in] != gain) {
1317		spin_lock_irq(&chip->lock);
1318		set_monitor_gain(chip, out, in, gain);
1319		update_output_line_level(chip);
1320		spin_unlock_irq(&chip->lock);
1321		changed = 1;
1322	}
1323	return changed;
1324}
1325
1326static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1327	.name = "Monitor Mixer Volume",
1328	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1329	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1330	.info = snd_echo_mixer_info,
1331	.get = snd_echo_mixer_get,
1332	.put = snd_echo_mixer_put,
1333	.tlv = {.p = db_scale_output_gain},
1334};
1335
1336#endif /* ECHOCARD_HAS_MONITOR */
1337
1338
1339
1340#ifdef ECHOCARD_HAS_VMIXER
1341
1342/******************* Vmixer *******************/
1343static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1344				struct snd_ctl_elem_info *uinfo)
1345{
1346	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1347	uinfo->count = 1;
1348	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1349	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1350	return 0;
1351}
1352
1353static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1354			       struct snd_ctl_elem_value *ucontrol)
1355{
1356	struct echoaudio *chip;
1357
1358	chip = snd_kcontrol_chip(kcontrol);
1359	ucontrol->value.integer.value[0] =
1360		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1361			[ucontrol->id.index % num_pipes_out(chip)];
1362	return 0;
1363}
1364
1365static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1366			       struct snd_ctl_elem_value *ucontrol)
1367{
1368	struct echoaudio *chip;
1369	int gain, changed;
1370	short vch, out;
1371
1372	changed = 0;
1373	chip = snd_kcontrol_chip(kcontrol);
1374	out = ucontrol->id.index / num_pipes_out(chip);
1375	vch = ucontrol->id.index % num_pipes_out(chip);
1376	gain = ucontrol->value.integer.value[0];
1377	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1378		return -EINVAL;
1379	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1380		spin_lock_irq(&chip->lock);
1381		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1382		update_vmixer_level(chip);
1383		spin_unlock_irq(&chip->lock);
1384		changed = 1;
1385	}
1386	return changed;
1387}
1388
1389static struct snd_kcontrol_new snd_echo_vmixer = {
1390	.name = "VMixer Volume",
1391	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1392	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1393	.info = snd_echo_vmixer_info,
1394	.get = snd_echo_vmixer_get,
1395	.put = snd_echo_vmixer_put,
1396	.tlv = {.p = db_scale_output_gain},
1397};
1398
1399#endif /* ECHOCARD_HAS_VMIXER */
1400
1401
1402
1403#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1404
1405/******************* Digital mode switch *******************/
1406static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1407				      struct snd_ctl_elem_info *uinfo)
1408{
1409	static const char * const names[4] = {
1410		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1411		"S/PDIF Cdrom"
1412	};
1413	struct echoaudio *chip;
1414
1415	chip = snd_kcontrol_chip(kcontrol);
1416	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1417}
1418
1419static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1420				     struct snd_ctl_elem_value *ucontrol)
1421{
1422	struct echoaudio *chip;
1423	int i, mode;
1424
1425	chip = snd_kcontrol_chip(kcontrol);
1426	mode = chip->digital_mode;
1427	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1428		if (mode == chip->digital_mode_list[i]) {
1429			ucontrol->value.enumerated.item[0] = i;
1430			break;
1431		}
1432	return 0;
1433}
1434
1435static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1436				     struct snd_ctl_elem_value *ucontrol)
1437{
1438	struct echoaudio *chip;
1439	int changed;
1440	unsigned short emode, dmode;
1441
1442	changed = 0;
1443	chip = snd_kcontrol_chip(kcontrol);
1444
1445	emode = ucontrol->value.enumerated.item[0];
1446	if (emode >= chip->num_digital_modes)
1447		return -EINVAL;
1448	dmode = chip->digital_mode_list[emode];
1449
1450	if (dmode != chip->digital_mode) {
1451		/* mode_mutex is required to make this operation atomic wrt
1452		pcm_digital_*_open() and set_input_clock() functions. */
1453		mutex_lock(&chip->mode_mutex);
1454
1455		/* Do not allow the user to change the digital mode when a pcm
1456		device is open because it also changes the number of channels
1457		and the allowed sample rates */
1458		if (chip->opencount) {
1459			changed = -EAGAIN;
1460		} else {
1461			changed = set_digital_mode(chip, dmode);
1462			/* If we had to change the clock source, report it */
1463			if (changed > 0 && chip->clock_src_ctl) {
1464				snd_ctl_notify(chip->card,
1465					       SNDRV_CTL_EVENT_MASK_VALUE,
1466					       &chip->clock_src_ctl->id);
1467				dev_dbg(chip->card->dev,
1468					"SDM() =%d\n", changed);
1469			}
1470			if (changed >= 0)
1471				changed = 1;	/* No errors */
1472		}
1473		mutex_unlock(&chip->mode_mutex);
1474	}
1475	return changed;
1476}
1477
1478static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1479	.name = "Digital mode Switch",
1480	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1481	.info = snd_echo_digital_mode_info,
1482	.get = snd_echo_digital_mode_get,
1483	.put = snd_echo_digital_mode_put,
1484};
1485
1486#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1487
1488
1489
1490#ifdef ECHOCARD_HAS_DIGITAL_IO
1491
1492/******************* S/PDIF mode switch *******************/
1493static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1494				    struct snd_ctl_elem_info *uinfo)
1495{
1496	static const char * const names[2] = {"Consumer", "Professional"};
1497
1498	return snd_ctl_enum_info(uinfo, 1, 2, names);
1499}
1500
1501static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1502				   struct snd_ctl_elem_value *ucontrol)
1503{
1504	struct echoaudio *chip;
1505
1506	chip = snd_kcontrol_chip(kcontrol);
1507	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1508	return 0;
1509}
1510
1511static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1512				   struct snd_ctl_elem_value *ucontrol)
1513{
1514	struct echoaudio *chip;
1515	int mode;
1516
1517	chip = snd_kcontrol_chip(kcontrol);
1518	mode = !!ucontrol->value.enumerated.item[0];
1519	if (mode != chip->professional_spdif) {
1520		spin_lock_irq(&chip->lock);
1521		set_professional_spdif(chip, mode);
1522		spin_unlock_irq(&chip->lock);
1523		return 1;
1524	}
1525	return 0;
1526}
1527
1528static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1529	.name = "S/PDIF mode Switch",
1530	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1531	.info = snd_echo_spdif_mode_info,
1532	.get = snd_echo_spdif_mode_get,
1533	.put = snd_echo_spdif_mode_put,
1534};
1535
1536#endif /* ECHOCARD_HAS_DIGITAL_IO */
1537
1538
1539
1540#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1541
1542/******************* Select input clock source *******************/
1543static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1544				      struct snd_ctl_elem_info *uinfo)
1545{
1546	static const char * const names[8] = {
1547		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1548		"ESync96", "MTC"
1549	};
1550	struct echoaudio *chip;
1551
1552	chip = snd_kcontrol_chip(kcontrol);
1553	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1554}
1555
1556static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1557				     struct snd_ctl_elem_value *ucontrol)
1558{
1559	struct echoaudio *chip;
1560	int i, clock;
1561
1562	chip = snd_kcontrol_chip(kcontrol);
1563	clock = chip->input_clock;
1564
1565	for (i = 0; i < chip->num_clock_sources; i++)
1566		if (clock == chip->clock_source_list[i])
1567			ucontrol->value.enumerated.item[0] = i;
1568
1569	return 0;
1570}
1571
1572static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1573				     struct snd_ctl_elem_value *ucontrol)
1574{
1575	struct echoaudio *chip;
1576	int changed;
1577	unsigned int eclock, dclock;
1578
1579	changed = 0;
1580	chip = snd_kcontrol_chip(kcontrol);
1581	eclock = ucontrol->value.enumerated.item[0];
1582	if (eclock >= chip->input_clock_types)
1583		return -EINVAL;
1584	dclock = chip->clock_source_list[eclock];
1585	if (chip->input_clock != dclock) {
1586		mutex_lock(&chip->mode_mutex);
1587		spin_lock_irq(&chip->lock);
1588		changed = set_input_clock(chip, dclock);
1589		if (!changed)
1590			changed = 1;	/* no errors */
1591		spin_unlock_irq(&chip->lock);
1592		mutex_unlock(&chip->mode_mutex);
1593	}
1594
1595	if (changed < 0)
1596		dev_dbg(chip->card->dev,
1597			"seticlk val%d err 0x%x\n", dclock, changed);
1598
1599	return changed;
1600}
1601
1602static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1603	.name = "Sample Clock Source",
1604	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1605	.info = snd_echo_clock_source_info,
1606	.get = snd_echo_clock_source_get,
1607	.put = snd_echo_clock_source_put,
1608};
1609
1610#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1611
1612
1613
1614#ifdef ECHOCARD_HAS_PHANTOM_POWER
1615
1616/******************* Phantom power switch *******************/
1617#define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1618
1619static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1620				      struct snd_ctl_elem_value *ucontrol)
1621{
1622	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1623
1624	ucontrol->value.integer.value[0] = chip->phantom_power;
1625	return 0;
1626}
1627
1628static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1629				      struct snd_ctl_elem_value *ucontrol)
1630{
1631	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1632	int power, changed = 0;
1633
1634	power = !!ucontrol->value.integer.value[0];
1635	if (chip->phantom_power != power) {
1636		spin_lock_irq(&chip->lock);
1637		changed = set_phantom_power(chip, power);
1638		spin_unlock_irq(&chip->lock);
1639		if (changed == 0)
1640			changed = 1;	/* no errors */
1641	}
1642	return changed;
1643}
1644
1645static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1646	.name = "Phantom power Switch",
1647	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1648	.info = snd_echo_phantom_power_info,
1649	.get = snd_echo_phantom_power_get,
1650	.put = snd_echo_phantom_power_put,
1651};
1652
1653#endif /* ECHOCARD_HAS_PHANTOM_POWER */
1654
1655
1656
1657#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1658
1659/******************* Digital input automute switch *******************/
1660#define snd_echo_automute_info		snd_ctl_boolean_mono_info
1661
1662static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1663				 struct snd_ctl_elem_value *ucontrol)
1664{
1665	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1666
1667	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1668	return 0;
1669}
1670
1671static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1672				 struct snd_ctl_elem_value *ucontrol)
1673{
1674	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1675	int automute, changed = 0;
1676
1677	automute = !!ucontrol->value.integer.value[0];
1678	if (chip->digital_in_automute != automute) {
1679		spin_lock_irq(&chip->lock);
1680		changed = set_input_auto_mute(chip, automute);
1681		spin_unlock_irq(&chip->lock);
1682		if (changed == 0)
1683			changed = 1;	/* no errors */
1684	}
1685	return changed;
1686}
1687
1688static const struct snd_kcontrol_new snd_echo_automute_switch = {
1689	.name = "Digital Capture Switch (automute)",
1690	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1691	.info = snd_echo_automute_info,
1692	.get = snd_echo_automute_get,
1693	.put = snd_echo_automute_put,
1694};
1695
1696#endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1697
1698
1699
1700/******************* VU-meters switch *******************/
1701#define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1702
1703static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1704					struct snd_ctl_elem_value *ucontrol)
1705{
1706	struct echoaudio *chip;
1707
1708	chip = snd_kcontrol_chip(kcontrol);
1709	spin_lock_irq(&chip->lock);
1710	set_meters_on(chip, ucontrol->value.integer.value[0]);
1711	spin_unlock_irq(&chip->lock);
1712	return 1;
1713}
1714
1715static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1716	.name = "VU-meters Switch",
1717	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1718	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1719	.info = snd_echo_vumeters_switch_info,
1720	.put = snd_echo_vumeters_switch_put,
1721};
1722
1723
1724
1725/***** Read VU-meters (input, output, analog and digital together) *****/
1726static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1727				  struct snd_ctl_elem_info *uinfo)
1728{
1729	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1730	uinfo->count = 96;
1731	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1732	uinfo->value.integer.max = 0;
1733	return 0;
1734}
1735
1736static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1737				 struct snd_ctl_elem_value *ucontrol)
1738{
1739	struct echoaudio *chip;
1740
1741	chip = snd_kcontrol_chip(kcontrol);
1742	get_audio_meters(chip, ucontrol->value.integer.value);
1743	return 0;
1744}
1745
1746static const struct snd_kcontrol_new snd_echo_vumeters = {
1747	.name = "VU-meters",
1748	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1750		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1751		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1752	.info = snd_echo_vumeters_info,
1753	.get = snd_echo_vumeters_get,
1754	.tlv = {.p = db_scale_output_gain},
1755};
1756
1757
1758
1759/*** Channels info - it exports informations about the number of channels ***/
1760static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1761				       struct snd_ctl_elem_info *uinfo)
1762{
1763	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1764	uinfo->count = 6;
1765	uinfo->value.integer.min = 0;
1766	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1767	return 0;
1768}
1769
1770static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1771				      struct snd_ctl_elem_value *ucontrol)
1772{
1773	struct echoaudio *chip;
1774	int detected, clocks, bit, src;
1775
1776	chip = snd_kcontrol_chip(kcontrol);
1777	ucontrol->value.integer.value[0] = num_busses_in(chip);
1778	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1779	ucontrol->value.integer.value[2] = num_busses_out(chip);
1780	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1781	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1782
1783	/* Compute the bitmask of the currently valid input clocks */
1784	detected = detect_input_clocks(chip);
1785	clocks = 0;
1786	src = chip->num_clock_sources - 1;
1787	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1788		if (detected & (1 << bit))
1789			for (; src >= 0; src--)
1790				if (bit == chip->clock_source_list[src]) {
1791					clocks |= 1 << src;
1792					break;
1793				}
1794	ucontrol->value.integer.value[5] = clocks;
1795
1796	return 0;
1797}
1798
1799static const struct snd_kcontrol_new snd_echo_channels_info = {
1800	.name = "Channels info",
1801	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1802	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1803	.info = snd_echo_channels_info_info,
1804	.get = snd_echo_channels_info_get,
1805};
1806
1807
1808
1809
1810/******************************************************************************
1811	IRQ Handling
1812******************************************************************************/
1813/* Check if a period has elapsed since last interrupt
1814 *
1815 * Don't make any updates to state; PCM core handles this with the
1816 * correct locks.
1817 *
1818 * \return true if a period has elapsed, otherwise false
1819 */
1820static bool period_has_elapsed(struct snd_pcm_substream *substream)
1821{
1822	struct snd_pcm_runtime *runtime = substream->runtime;
1823	struct audiopipe *pipe = runtime->private_data;
1824	u32 counter, step;
1825	size_t period_bytes;
1826
1827	if (pipe->state != PIPE_STATE_STARTED)
1828		return false;
1829
1830	period_bytes = frames_to_bytes(runtime, runtime->period_size);
1831
1832	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1833
1834	step = counter - pipe->last_period;  /* handles wrapping */
1835	step -= step % period_bytes;  /* acknowledge whole periods only */
1836
1837	if (step == 0)
1838		return false;  /* haven't advanced a whole period yet */
1839
1840	pipe->last_period += step;  /* used exclusively by us */
1841	return true;
1842}
1843
1844static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1845{
1846	struct echoaudio *chip = dev_id;
1847	int ss, st;
1848
1849	spin_lock(&chip->lock);
1850	st = service_irq(chip);
1851	if (st < 0) {
1852		spin_unlock(&chip->lock);
1853		return IRQ_NONE;
1854	}
1855	/* The hardware doesn't tell us which substream caused the irq,
1856	thus we have to check all running substreams. */
1857	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1858		struct snd_pcm_substream *substream;
1859
1860		substream = chip->substream[ss];
1861		if (substream && period_has_elapsed(substream)) {
1862			spin_unlock(&chip->lock);
1863			snd_pcm_period_elapsed(substream);
1864			spin_lock(&chip->lock);
1865		}
1866	}
1867	spin_unlock(&chip->lock);
1868
1869#ifdef ECHOCARD_HAS_MIDI
1870	if (st > 0 && chip->midi_in) {
1871		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1872		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1873	}
1874#endif
1875	return IRQ_HANDLED;
1876}
1877
1878
1879
1880
1881/******************************************************************************
1882	Module construction / destruction
1883******************************************************************************/
1884
1885static void snd_echo_free(struct snd_card *card)
1886{
1887	struct echoaudio *chip = card->private_data;
1888
1889	if (chip->comm_page)
1890		rest_in_peace(chip);
1891
1892	if (chip->irq >= 0)
1893		free_irq(chip->irq, chip);
1894
 
 
 
 
 
 
 
1895	/* release chip data */
1896	free_firmware_cache(chip);
 
 
1897}
1898
 
 
 
 
 
 
 
 
 
 
 
1899/* <--snd_echo_probe() */
1900static int snd_echo_create(struct snd_card *card,
1901			   struct pci_dev *pci)
 
1902{
1903	struct echoaudio *chip = card->private_data;
1904	int err;
1905	size_t sz;
 
 
 
 
 
1906
1907	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1908
1909	err = pcim_enable_device(pci);
1910	if (err < 0)
1911		return err;
1912	pci_set_master(pci);
1913
1914	/* Allocate chip if needed */
1915	spin_lock_init(&chip->lock);
1916	chip->card = card;
1917	chip->pci = pci;
1918	chip->irq = -1;
1919	chip->opencount = 0;
1920	mutex_init(&chip->mode_mutex);
1921	chip->can_set_rate = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
1922
1923	/* PCI resource allocation */
1924	err = pci_request_regions(pci, ECHOCARD_NAME);
1925	if (err < 0)
1926		return err;
1927
1928	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1929	sz = pci_resource_len(pci, 0);
1930	if (sz > PAGE_SIZE)
1931		sz = PAGE_SIZE;		/* We map only the required part */
1932
1933	chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
 
 
 
 
 
 
1934	if (!chip->dsp_registers) {
1935		dev_err(chip->card->dev, "ioremap failed\n");
 
1936		return -ENOMEM;
1937	}
1938
1939	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1940			KBUILD_MODNAME, chip)) {
1941		dev_err(chip->card->dev, "cannot grab irq\n");
 
1942		return -EBUSY;
1943	}
1944	chip->irq = pci->irq;
1945	card->sync_irq = chip->irq;
1946	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1947		chip->pci, chip->irq, chip->pci->subsystem_device);
1948
1949	card->private_free = snd_echo_free;
1950
1951	/* Create the DSP comm page - this is the area of memory used for most
1952	of the communication with the DSP, which accesses it via bus mastering */
1953	chip->commpage_dma_buf =
1954		snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
1955				     sizeof(struct comm_page));
1956	if (!chip->commpage_dma_buf)
 
1957		return -ENOMEM;
1958	chip->comm_page_phys = chip->commpage_dma_buf->addr;
1959	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
 
1960
1961	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1962	if (err >= 0)
1963		err = set_mixer_defaults(chip);
1964	if (err < 0) {
1965		dev_err(card->dev, "init_hw err=%d\n", err);
 
1966		return err;
1967	}
1968
 
 
 
 
 
 
1969	return 0;
1970}
1971
 
 
1972/* constructor */
1973static int __snd_echo_probe(struct pci_dev *pci,
1974			    const struct pci_device_id *pci_id)
1975{
1976	static int dev;
1977	struct snd_card *card;
1978	struct echoaudio *chip;
1979	char *dsp;
1980	__maybe_unused int i;
1981	int err;
1982
1983	if (dev >= SNDRV_CARDS)
1984		return -ENODEV;
1985	if (!enable[dev]) {
1986		dev++;
1987		return -ENOENT;
1988	}
1989
1990	i = 0;
1991	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1992				sizeof(*chip), &card);
1993	if (err < 0)
1994		return err;
1995	chip = card->private_data;
1996
1997	err = snd_echo_create(card, pci);
1998	if (err < 0)
 
1999		return err;
 
2000
2001	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2002	strcpy(card->shortname, chip->card_name);
2003
2004	dsp = "56301";
2005	if (pci_id->device == 0x3410)
2006		dsp = "56361";
2007
2008	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2009		card->shortname, pci_id->subdevice & 0x000f, dsp,
2010		chip->dsp_registers_phys, chip->irq);
2011
2012	err = snd_echo_new_pcm(chip);
2013	if (err < 0) {
2014		dev_err(chip->card->dev, "new pcm error %d\n", err);
 
2015		return err;
2016	}
2017
2018#ifdef ECHOCARD_HAS_MIDI
2019	if (chip->has_midi) {	/* Some Mia's do not have midi */
2020		err = snd_echo_midi_create(card, chip);
2021		if (err < 0) {
2022			dev_err(chip->card->dev, "new midi error %d\n", err);
 
2023			return err;
2024		}
2025	}
2026#endif
2027
2028#ifdef ECHOCARD_HAS_VMIXER
2029	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2030	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
2031	if (err < 0)
2032		return err;
2033#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2034	err = snd_ctl_add(chip->card,
2035			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2036	if (err < 0)
2037		return err;
2038#endif
2039#else /* ECHOCARD_HAS_VMIXER */
2040	err = snd_ctl_add(chip->card,
2041			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2042	if (err < 0)
2043		return err;
2044#endif /* ECHOCARD_HAS_VMIXER */
2045
2046#ifdef ECHOCARD_HAS_INPUT_GAIN
2047	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2048	if (err < 0)
2049		return err;
2050#endif
2051
2052#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2053	if (!chip->hasnt_input_nominal_level) {
2054		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2055		if (err < 0)
2056			return err;
2057	}
2058#endif
2059
2060#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2061	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2062	if (err < 0)
2063		return err;
2064#endif
2065
2066	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2067	if (err < 0)
2068		return err;
2069
2070	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2071	if (err < 0)
2072		return err;
2073
2074#ifdef ECHOCARD_HAS_MONITOR
2075	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2076	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2077	if (err < 0)
2078		return err;
2079#endif
2080
2081#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2082	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2083	if (err < 0)
2084		return err;
2085#endif
2086
2087	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2088	if (err < 0)
2089		return err;
2090
2091#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2092	/* Creates a list of available digital modes */
2093	chip->num_digital_modes = 0;
2094	for (i = 0; i < 6; i++)
2095		if (chip->digital_modes & (1 << i))
2096			chip->digital_mode_list[chip->num_digital_modes++] = i;
2097
2098	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2099	if (err < 0)
2100		return err;
2101#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2102
2103#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2104	/* Creates a list of available clock sources */
2105	chip->num_clock_sources = 0;
2106	for (i = 0; i < 10; i++)
2107		if (chip->input_clock_types & (1 << i))
2108			chip->clock_source_list[chip->num_clock_sources++] = i;
2109
2110	if (chip->num_clock_sources > 1) {
2111		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2112		err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2113		if (err < 0)
2114			return err;
2115	}
2116#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2117
2118#ifdef ECHOCARD_HAS_DIGITAL_IO
2119	err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2120	if (err < 0)
2121		return err;
2122#endif
2123
2124#ifdef ECHOCARD_HAS_PHANTOM_POWER
2125	if (chip->has_phantom_power) {
2126		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2127		if (err < 0)
2128			return err;
2129	}
2130#endif
2131
2132	err = snd_card_register(card);
2133	if (err < 0)
2134		return err;
2135	dev_info(card->dev, "Card registered: %s\n", card->longname);
2136
2137	pci_set_drvdata(pci, chip);
2138	dev++;
2139	return 0;
2140}
2141
2142static int snd_echo_probe(struct pci_dev *pci,
2143			  const struct pci_device_id *pci_id)
2144{
2145	return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2146}
2147
2148
 
2149#if defined(CONFIG_PM_SLEEP)
2150
2151static int snd_echo_suspend(struct device *dev)
2152{
2153	struct echoaudio *chip = dev_get_drvdata(dev);
2154
2155#ifdef ECHOCARD_HAS_MIDI
2156	/* This call can sleep */
2157	if (chip->midi_out)
2158		snd_echo_midi_output_trigger(chip->midi_out, 0);
2159#endif
2160	spin_lock_irq(&chip->lock);
2161	if (wait_handshake(chip)) {
2162		spin_unlock_irq(&chip->lock);
2163		return -EIO;
2164	}
2165	clear_handshake(chip);
2166	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2167		spin_unlock_irq(&chip->lock);
2168		return -EIO;
2169	}
2170	spin_unlock_irq(&chip->lock);
2171
2172	chip->dsp_code = NULL;
2173	free_irq(chip->irq, chip);
2174	chip->irq = -1;
2175	chip->card->sync_irq = -1;
2176	return 0;
2177}
2178
2179
2180
2181static int snd_echo_resume(struct device *dev)
2182{
2183	struct pci_dev *pci = to_pci_dev(dev);
2184	struct echoaudio *chip = dev_get_drvdata(dev);
2185	struct comm_page *commpage, *commpage_bak;
2186	u32 pipe_alloc_mask;
2187	int err;
2188
2189	commpage = chip->comm_page;
2190	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2191	if (commpage_bak == NULL)
2192		return -ENOMEM;
2193
2194	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2195	if (err < 0) {
2196		kfree(commpage_bak);
2197		dev_err(dev, "resume init_hw err=%d\n", err);
2198		return err;
2199	}
2200
2201	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2202	 * restore_dsp_settings() fails.
2203	 */
2204	pipe_alloc_mask = chip->pipe_alloc_mask;
2205	chip->pipe_alloc_mask = 0;
2206	err = restore_dsp_rettings(chip);
2207	chip->pipe_alloc_mask = pipe_alloc_mask;
2208	if (err < 0) {
2209		kfree(commpage_bak);
2210		return err;
2211	}
2212
2213	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2214		sizeof(commpage->audio_format));
2215	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2216		sizeof(commpage->sglist_addr));
2217	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2218		sizeof(commpage->midi_output));
2219	kfree(commpage_bak);
2220
2221	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2222			KBUILD_MODNAME, chip)) {
2223		dev_err(chip->card->dev, "cannot grab irq\n");
2224		return -EBUSY;
2225	}
2226	chip->irq = pci->irq;
2227	chip->card->sync_irq = chip->irq;
2228	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2229
2230#ifdef ECHOCARD_HAS_MIDI
2231	if (chip->midi_input_enabled)
2232		enable_midi_input(chip, true);
2233	if (chip->midi_out)
2234		snd_echo_midi_output_trigger(chip->midi_out, 1);
2235#endif
2236
2237	return 0;
2238}
2239
2240static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2241#define SND_ECHO_PM_OPS	&snd_echo_pm
2242#else
2243#define SND_ECHO_PM_OPS	NULL
2244#endif /* CONFIG_PM_SLEEP */
2245
 
 
 
 
 
 
 
 
 
 
 
 
2246/******************************************************************************
2247	Everything starts and ends here
2248******************************************************************************/
2249
2250/* pci_driver definition */
2251static struct pci_driver echo_driver = {
2252	.name = KBUILD_MODNAME,
2253	.id_table = snd_echo_ids,
2254	.probe = snd_echo_probe,
 
2255	.driver = {
2256		.pm = SND_ECHO_PM_OPS,
2257	},
2258};
2259
2260module_pci_driver(echo_driver);
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  ALSA driver for Echoaudio soundcards.
   4 *  Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
   5 *  Copyright (C) 2020 Mark Hills <mark@xwax.org>
   6 */
   7
   8#include <linux/module.h>
   9
  10MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
  11MODULE_LICENSE("GPL v2");
  12MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
  13MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
  14MODULE_DEVICE_TABLE(pci, snd_echo_ids);
  15
  16static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  17static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  18static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  19
  20module_param_array(index, int, NULL, 0444);
  21MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
  22module_param_array(id, charp, NULL, 0444);
  23MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
  24module_param_array(enable, bool, NULL, 0444);
  25MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
  26
  27static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
  28static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
  29
  30
  31
  32static int get_firmware(const struct firmware **fw_entry,
  33			struct echoaudio *chip, const short fw_index)
  34{
  35	int err;
  36	char name[30];
  37
  38#ifdef CONFIG_PM_SLEEP
  39	if (chip->fw_cache[fw_index]) {
  40		dev_dbg(chip->card->dev,
  41			"firmware requested: %s is cached\n",
  42			card_fw[fw_index].data);
  43		*fw_entry = chip->fw_cache[fw_index];
  44		return 0;
  45	}
  46#endif
  47
  48	dev_dbg(chip->card->dev,
  49		"firmware requested: %s\n", card_fw[fw_index].data);
  50	snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
  51	err = request_firmware(fw_entry, name, &chip->pci->dev);
  52	if (err < 0)
  53		dev_err(chip->card->dev,
  54			"get_firmware(): Firmware not available (%d)\n", err);
  55#ifdef CONFIG_PM_SLEEP
  56	else
  57		chip->fw_cache[fw_index] = *fw_entry;
  58#endif
  59	return err;
  60}
  61
  62
  63
  64static void free_firmware(const struct firmware *fw_entry,
  65			  struct echoaudio *chip)
  66{
  67#ifdef CONFIG_PM_SLEEP
  68	dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
  69#else
  70	release_firmware(fw_entry);
  71#endif
  72}
  73
  74
  75
  76static void free_firmware_cache(struct echoaudio *chip)
  77{
  78#ifdef CONFIG_PM_SLEEP
  79	int i;
  80
  81	for (i = 0; i < 8 ; i++)
  82		if (chip->fw_cache[i]) {
  83			release_firmware(chip->fw_cache[i]);
  84			dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
  85		}
  86
  87#endif
  88}
  89
  90
  91
  92/******************************************************************************
  93	PCM interface
  94******************************************************************************/
  95
  96static void audiopipe_free(struct snd_pcm_runtime *runtime)
  97{
  98	struct audiopipe *pipe = runtime->private_data;
  99
 100	if (pipe->sgpage.area)
 101		snd_dma_free_pages(&pipe->sgpage);
 102	kfree(pipe);
 103}
 104
 105
 106
 107static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
 108					      struct snd_pcm_hw_rule *rule)
 109{
 110	struct snd_interval *c = hw_param_interval(params,
 111						   SNDRV_PCM_HW_PARAM_CHANNELS);
 112	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 113	struct snd_mask fmt;
 114
 115	snd_mask_any(&fmt);
 116
 117#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 118	/* >=2 channels cannot be S32_BE */
 119	if (c->min == 2) {
 120		fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
 121		return snd_mask_refine(f, &fmt);
 122	}
 123#endif
 124	/* > 2 channels cannot be U8 and S32_BE */
 125	if (c->min > 2) {
 126		fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
 127		return snd_mask_refine(f, &fmt);
 128	}
 129	/* Mono is ok with any format */
 130	return 0;
 131}
 132
 133
 134
 135static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
 136					      struct snd_pcm_hw_rule *rule)
 137{
 138	struct snd_interval *c = hw_param_interval(params,
 139						   SNDRV_PCM_HW_PARAM_CHANNELS);
 140	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 141	struct snd_interval ch;
 142
 143	snd_interval_any(&ch);
 144
 145	/* S32_BE is mono (and stereo) only */
 146	if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
 147		ch.min = 1;
 148#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 149		ch.max = 2;
 150#else
 151		ch.max = 1;
 152#endif
 153		ch.integer = 1;
 154		return snd_interval_refine(c, &ch);
 155	}
 156	/* U8 can be only mono or stereo */
 157	if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
 158		ch.min = 1;
 159		ch.max = 2;
 160		ch.integer = 1;
 161		return snd_interval_refine(c, &ch);
 162	}
 163	/* S16_LE, S24_3LE and S32_LE support any number of channels. */
 164	return 0;
 165}
 166
 167
 168
 169static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
 170					       struct snd_pcm_hw_rule *rule)
 171{
 172	struct snd_interval *c = hw_param_interval(params,
 173						   SNDRV_PCM_HW_PARAM_CHANNELS);
 174	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 175	struct snd_mask fmt;
 176	u64 fmask;
 177	snd_mask_any(&fmt);
 178
 179	fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
 180
 181	/* >2 channels must be S16_LE, S24_3LE or S32_LE */
 182	if (c->min > 2) {
 183		fmask &= SNDRV_PCM_FMTBIT_S16_LE |
 184			 SNDRV_PCM_FMTBIT_S24_3LE |
 185			 SNDRV_PCM_FMTBIT_S32_LE;
 186	/* 1 channel must be S32_BE or S32_LE */
 187	} else if (c->max == 1)
 188		fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
 189#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 190	/* 2 channels cannot be S32_BE */
 191	else if (c->min == 2 && c->max == 2)
 192		fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
 193#endif
 194	else
 195		return 0;
 196
 197	fmt.bits[0] &= (u32)fmask;
 198	fmt.bits[1] &= (u32)(fmask >> 32);
 199	return snd_mask_refine(f, &fmt);
 200}
 201
 202
 203
 204static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
 205					       struct snd_pcm_hw_rule *rule)
 206{
 207	struct snd_interval *c = hw_param_interval(params,
 208						   SNDRV_PCM_HW_PARAM_CHANNELS);
 209	struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
 210	struct snd_interval ch;
 211	u64 fmask;
 212
 213	snd_interval_any(&ch);
 214	ch.integer = 1;
 215	fmask = f->bits[0] + ((u64)f->bits[1] << 32);
 216
 217	/* S32_BE is mono (and stereo) only */
 218	if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
 219		ch.min = 1;
 220#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 221		ch.max = 2;
 222#else
 223		ch.max = 1;
 224#endif
 225	/* U8 is stereo only */
 226	} else if (fmask == SNDRV_PCM_FMTBIT_U8)
 227		ch.min = ch.max = 2;
 228	/* S16_LE and S24_3LE must be at least stereo */
 229	else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
 230			       SNDRV_PCM_FMTBIT_S24_3LE)))
 231		ch.min = 2;
 232	else
 233		return 0;
 234
 235	return snd_interval_refine(c, &ch);
 236}
 237
 238
 239
 240/* Since the sample rate is a global setting, do allow the user to change the
 241sample rate only if there is only one pcm device open. */
 242static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
 243			       struct snd_pcm_hw_rule *rule)
 244{
 245	struct snd_interval *rate = hw_param_interval(params,
 246						      SNDRV_PCM_HW_PARAM_RATE);
 247	struct echoaudio *chip = rule->private;
 248	struct snd_interval fixed;
 249	int err;
 250
 251	mutex_lock(&chip->mode_mutex);
 252
 253	if (chip->can_set_rate) {
 254		err = 0;
 255	} else {
 256		snd_interval_any(&fixed);
 257		fixed.min = fixed.max = chip->sample_rate;
 258		err = snd_interval_refine(rate, &fixed);
 259	}
 260
 261	mutex_unlock(&chip->mode_mutex);
 262	return err;
 263}
 264
 265
 266static int pcm_open(struct snd_pcm_substream *substream,
 267		    signed char max_channels)
 268{
 269	struct echoaudio *chip;
 270	struct snd_pcm_runtime *runtime;
 271	struct audiopipe *pipe;
 272	int err, i;
 273
 274	if (max_channels <= 0)
 275		return -EAGAIN;
 276
 277	chip = snd_pcm_substream_chip(substream);
 278	runtime = substream->runtime;
 279
 280	pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
 281	if (!pipe)
 282		return -ENOMEM;
 283	pipe->index = -1;		/* Not configured yet */
 284
 285	/* Set up hw capabilities and contraints */
 286	memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
 287	dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
 288	pipe->constr.list = channels_list;
 289	pipe->constr.mask = 0;
 290	for (i = 0; channels_list[i] <= max_channels; i++);
 291	pipe->constr.count = i;
 292	if (pipe->hw.channels_max > max_channels)
 293		pipe->hw.channels_max = max_channels;
 294	if (chip->digital_mode == DIGITAL_MODE_ADAT) {
 295		pipe->hw.rate_max = 48000;
 296		pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
 297	}
 298
 299	runtime->hw = pipe->hw;
 300	runtime->private_data = pipe;
 301	runtime->private_free = audiopipe_free;
 302	snd_pcm_set_sync(substream);
 303
 304	/* Only mono and any even number of channels are allowed */
 305	if ((err = snd_pcm_hw_constraint_list(runtime, 0,
 306					      SNDRV_PCM_HW_PARAM_CHANNELS,
 307					      &pipe->constr)) < 0)
 
 308		return err;
 309
 310	/* All periods should have the same size */
 311	if ((err = snd_pcm_hw_constraint_integer(runtime,
 312						 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
 
 313		return err;
 314
 315	/* The hw accesses memory in chunks 32 frames long and they should be
 316	32-bytes-aligned. It's not a requirement, but it seems that IRQs are
 317	generated with a resolution of 32 frames. Thus we need the following */
 318	if ((err = snd_pcm_hw_constraint_step(runtime, 0,
 319					      SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
 320					      32)) < 0)
 321		return err;
 322	if ((err = snd_pcm_hw_constraint_step(runtime, 0,
 323					      SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 324					      32)) < 0)
 325		return err;
 326
 327	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 328				       SNDRV_PCM_HW_PARAM_RATE,
 329					hw_rule_sample_rate, chip,
 330				       SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
 
 331		return err;
 332
 333	/* Allocate a page for the scatter-gather list */
 334	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
 335				       &chip->pci->dev,
 336				       PAGE_SIZE, &pipe->sgpage)) < 0) {
 
 337		dev_err(chip->card->dev, "s-g list allocation failed\n");
 338		return err;
 339	}
 340
 341	/*
 342	 * Sole ownership required to set the rate
 343	 */
 344
 345	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
 346		chip->opencount, chip->can_set_rate, chip->rate_set);
 347
 348	chip->opencount++;
 349	if (chip->opencount > 1 && chip->rate_set)
 350		chip->can_set_rate = 0;
 351
 352	return 0;
 353}
 354
 355
 356
 357static int pcm_analog_in_open(struct snd_pcm_substream *substream)
 358{
 359	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 360	int err;
 361
 362	if ((err = pcm_open(substream, num_analog_busses_in(chip) -
 363			    substream->number)) < 0)
 
 
 
 
 
 
 
 364		return err;
 365	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 366				       SNDRV_PCM_HW_PARAM_CHANNELS,
 367				       hw_rule_capture_channels_by_format, NULL,
 368				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
 369		return err;
 370	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 371				       SNDRV_PCM_HW_PARAM_FORMAT,
 372				       hw_rule_capture_format_by_channels, NULL,
 373				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
 374		return err;
 375
 376	return 0;
 377}
 378
 379
 380
 381static int pcm_analog_out_open(struct snd_pcm_substream *substream)
 382{
 383	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 384	int max_channels, err;
 385
 386#ifdef ECHOCARD_HAS_VMIXER
 387	max_channels = num_pipes_out(chip);
 388#else
 389	max_channels = num_analog_busses_out(chip);
 390#endif
 391	if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
 
 
 
 
 
 
 
 
 392		return err;
 393	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 394				       SNDRV_PCM_HW_PARAM_CHANNELS,
 395				       hw_rule_playback_channels_by_format,
 396				       NULL,
 397				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
 398		return err;
 399	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 400				       SNDRV_PCM_HW_PARAM_FORMAT,
 401				       hw_rule_playback_format_by_channels,
 402				       NULL,
 403				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
 404		return err;
 405
 406	return 0;
 407}
 408
 409
 410
 411#ifdef ECHOCARD_HAS_DIGITAL_IO
 412
 413static int pcm_digital_in_open(struct snd_pcm_substream *substream)
 414{
 415	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 416	int err, max_channels;
 417
 418	max_channels = num_digital_busses_in(chip) - substream->number;
 419	mutex_lock(&chip->mode_mutex);
 420	if (chip->digital_mode == DIGITAL_MODE_ADAT)
 421		err = pcm_open(substream, max_channels);
 422	else	/* If the card has ADAT, subtract the 6 channels
 423		 * that S/PDIF doesn't have
 424		 */
 425		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
 426
 427	if (err < 0)
 428		goto din_exit;
 429
 430	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 431				       SNDRV_PCM_HW_PARAM_CHANNELS,
 432				       hw_rule_capture_channels_by_format, NULL,
 433				       SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
 
 434		goto din_exit;
 435	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 436				       SNDRV_PCM_HW_PARAM_FORMAT,
 437				       hw_rule_capture_format_by_channels, NULL,
 438				       SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
 
 439		goto din_exit;
 440
 441din_exit:
 442	mutex_unlock(&chip->mode_mutex);
 443	return err;
 444}
 445
 446
 447
 448#ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
 449
 450static int pcm_digital_out_open(struct snd_pcm_substream *substream)
 451{
 452	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 453	int err, max_channels;
 454
 455	max_channels = num_digital_busses_out(chip) - substream->number;
 456	mutex_lock(&chip->mode_mutex);
 457	if (chip->digital_mode == DIGITAL_MODE_ADAT)
 458		err = pcm_open(substream, max_channels);
 459	else	/* If the card has ADAT, subtract the 6 channels
 460		 * that S/PDIF doesn't have
 461		 */
 462		err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
 463
 464	if (err < 0)
 465		goto dout_exit;
 466
 467	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 468				       SNDRV_PCM_HW_PARAM_CHANNELS,
 469				       hw_rule_playback_channels_by_format,
 470				       NULL, SNDRV_PCM_HW_PARAM_FORMAT,
 471				       -1)) < 0)
 
 472		goto dout_exit;
 473	if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
 474				       SNDRV_PCM_HW_PARAM_FORMAT,
 475				       hw_rule_playback_format_by_channels,
 476				       NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
 477				       -1)) < 0)
 
 478		goto dout_exit;
 479
 480dout_exit:
 481	mutex_unlock(&chip->mode_mutex);
 482	return err;
 483}
 484
 485#endif /* !ECHOCARD_HAS_VMIXER */
 486
 487#endif /* ECHOCARD_HAS_DIGITAL_IO */
 488
 489
 490
 491static int pcm_close(struct snd_pcm_substream *substream)
 492{
 493	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 494
 495	/* Nothing to do here. Audio is already off and pipe will be
 496	 * freed by its callback
 497	 */
 498
 499	mutex_lock(&chip->mode_mutex);
 500
 501	dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
 502		chip->opencount, chip->can_set_rate, chip->rate_set);
 503
 504	chip->opencount--;
 505
 506	switch (chip->opencount) {
 507	case 1:
 508		chip->can_set_rate = 1;
 509		break;
 510
 511	case 0:
 512		chip->rate_set = 0;
 513		break;
 514	}
 515
 516	mutex_unlock(&chip->mode_mutex);
 517	return 0;
 518}
 519
 520
 521
 522/* Channel allocation and scatter-gather list setup */
 523static int init_engine(struct snd_pcm_substream *substream,
 524		       struct snd_pcm_hw_params *hw_params,
 525		       int pipe_index, int interleave)
 526{
 527	struct echoaudio *chip;
 528	int err, per, rest, page, edge, offs;
 529	struct audiopipe *pipe;
 530
 531	chip = snd_pcm_substream_chip(substream);
 532	pipe = (struct audiopipe *) substream->runtime->private_data;
 533
 534	/* Sets up che hardware. If it's already initialized, reset and
 535	 * redo with the new parameters
 536	 */
 537	spin_lock_irq(&chip->lock);
 538	if (pipe->index >= 0) {
 539		dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
 540		err = free_pipes(chip, pipe);
 541		snd_BUG_ON(err);
 542		chip->substream[pipe->index] = NULL;
 543	}
 544
 545	err = allocate_pipes(chip, pipe, pipe_index, interleave);
 546	if (err < 0) {
 547		spin_unlock_irq(&chip->lock);
 548		dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
 549			pipe_index, err);
 550		return err;
 551	}
 552	spin_unlock_irq(&chip->lock);
 553	dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
 554
 555	dev_dbg(chip->card->dev,
 556		"pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
 557		params_buffer_bytes(hw_params), params_periods(hw_params),
 558		params_period_bytes(hw_params));
 559
 560	sglist_init(chip, pipe);
 561	edge = PAGE_SIZE;
 562	for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
 563	     per++) {
 564		rest = params_period_bytes(hw_params);
 565		if (offs + rest > params_buffer_bytes(hw_params))
 566			rest = params_buffer_bytes(hw_params) - offs;
 567		while (rest) {
 568			dma_addr_t addr;
 569			addr = snd_pcm_sgbuf_get_addr(substream, offs);
 570			if (rest <= edge - offs) {
 571				sglist_add_mapping(chip, pipe, addr, rest);
 572				sglist_add_irq(chip, pipe);
 573				offs += rest;
 574				rest = 0;
 575			} else {
 576				sglist_add_mapping(chip, pipe, addr,
 577						   edge - offs);
 578				rest -= edge - offs;
 579				offs = edge;
 580			}
 581			if (offs == edge) {
 582				edge += PAGE_SIZE;
 583				page++;
 584			}
 585		}
 586	}
 587
 588	/* Close the ring buffer */
 589	sglist_wrap(chip, pipe);
 590
 591	/* This stuff is used by the irq handler, so it must be
 592	 * initialized before chip->substream
 593	 */
 594	pipe->last_period = 0;
 595	pipe->last_counter = 0;
 596	pipe->position = 0;
 597	smp_wmb();
 598	chip->substream[pipe_index] = substream;
 599	chip->rate_set = 1;
 600	spin_lock_irq(&chip->lock);
 601	set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
 602	spin_unlock_irq(&chip->lock);
 603	return 0;
 604}
 605
 606
 607
 608static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
 609				   struct snd_pcm_hw_params *hw_params)
 610{
 611	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 612
 613	return init_engine(substream, hw_params, px_analog_in(chip) +
 614			substream->number, params_channels(hw_params));
 615}
 616
 617
 618
 619static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
 620				    struct snd_pcm_hw_params *hw_params)
 621{
 622	return init_engine(substream, hw_params, substream->number,
 623			   params_channels(hw_params));
 624}
 625
 626
 627
 628#ifdef ECHOCARD_HAS_DIGITAL_IO
 629
 630static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
 631				    struct snd_pcm_hw_params *hw_params)
 632{
 633	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 634
 635	return init_engine(substream, hw_params, px_digital_in(chip) +
 636			substream->number, params_channels(hw_params));
 637}
 638
 639
 640
 641#ifndef ECHOCARD_HAS_VMIXER	/* See the note in snd_echo_new_pcm() */
 642static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
 643				     struct snd_pcm_hw_params *hw_params)
 644{
 645	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 646
 647	return init_engine(substream, hw_params, px_digital_out(chip) +
 648			substream->number, params_channels(hw_params));
 649}
 650#endif /* !ECHOCARD_HAS_VMIXER */
 651
 652#endif /* ECHOCARD_HAS_DIGITAL_IO */
 653
 654
 655
 656static int pcm_hw_free(struct snd_pcm_substream *substream)
 657{
 658	struct echoaudio *chip;
 659	struct audiopipe *pipe;
 660
 661	chip = snd_pcm_substream_chip(substream);
 662	pipe = (struct audiopipe *) substream->runtime->private_data;
 663
 664	spin_lock_irq(&chip->lock);
 665	if (pipe->index >= 0) {
 666		dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
 667		free_pipes(chip, pipe);
 668		chip->substream[pipe->index] = NULL;
 669		pipe->index = -1;
 670	}
 671	spin_unlock_irq(&chip->lock);
 672
 673	return 0;
 674}
 675
 676
 677
 678static int pcm_prepare(struct snd_pcm_substream *substream)
 679{
 680	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 681	struct snd_pcm_runtime *runtime = substream->runtime;
 682	struct audioformat format;
 683	int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
 684
 685	dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
 686		runtime->rate, runtime->format, runtime->channels);
 687	format.interleave = runtime->channels;
 688	format.data_are_bigendian = 0;
 689	format.mono_to_stereo = 0;
 690	switch (runtime->format) {
 691	case SNDRV_PCM_FORMAT_U8:
 692		format.bits_per_sample = 8;
 693		break;
 694	case SNDRV_PCM_FORMAT_S16_LE:
 695		format.bits_per_sample = 16;
 696		break;
 697	case SNDRV_PCM_FORMAT_S24_3LE:
 698		format.bits_per_sample = 24;
 699		break;
 700	case SNDRV_PCM_FORMAT_S32_BE:
 701		format.data_are_bigendian = 1;
 702		fallthrough;
 703	case SNDRV_PCM_FORMAT_S32_LE:
 704		format.bits_per_sample = 32;
 705		break;
 706	default:
 707		dev_err(chip->card->dev,
 708			"Prepare error: unsupported format %d\n",
 709			runtime->format);
 710		return -EINVAL;
 711	}
 712
 713	if (snd_BUG_ON(pipe_index >= px_num(chip)))
 714		return -EINVAL;
 715
 716	/*
 717	 * We passed checks we can do independently; now take
 718	 * exclusive control
 719	 */
 720
 721	spin_lock_irq(&chip->lock);
 722
 723	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
 724		spin_unlock_irq(&chip->lock);
 725		return -EINVAL;
 726	}
 727
 728	set_audio_format(chip, pipe_index, &format);
 729	spin_unlock_irq(&chip->lock);
 730
 731	return 0;
 732}
 733
 734
 735
 736static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 737{
 738	struct echoaudio *chip = snd_pcm_substream_chip(substream);
 739	struct audiopipe *pipe;
 740	int i, err;
 741	u32 channelmask = 0;
 742	struct snd_pcm_substream *s;
 743
 744	snd_pcm_group_for_each_entry(s, substream) {
 745		for (i = 0; i < DSP_MAXPIPES; i++) {
 746			if (s == chip->substream[i]) {
 747				channelmask |= 1 << i;
 748				snd_pcm_trigger_done(s, substream);
 749			}
 750		}
 751	}
 752
 753	spin_lock(&chip->lock);
 754	switch (cmd) {
 755	case SNDRV_PCM_TRIGGER_RESUME:
 756	case SNDRV_PCM_TRIGGER_START:
 757	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 758		for (i = 0; i < DSP_MAXPIPES; i++) {
 759			if (channelmask & (1 << i)) {
 760				pipe = chip->substream[i]->runtime->private_data;
 761				switch (pipe->state) {
 762				case PIPE_STATE_STOPPED:
 763					pipe->last_period = 0;
 764					pipe->last_counter = 0;
 765					pipe->position = 0;
 766					*pipe->dma_counter = 0;
 767					fallthrough;
 768				case PIPE_STATE_PAUSED:
 769					pipe->state = PIPE_STATE_STARTED;
 770					break;
 771				case PIPE_STATE_STARTED:
 772					break;
 773				}
 774			}
 775		}
 776		err = start_transport(chip, channelmask,
 777				      chip->pipe_cyclic_mask);
 778		break;
 779	case SNDRV_PCM_TRIGGER_SUSPEND:
 780	case SNDRV_PCM_TRIGGER_STOP:
 781		for (i = 0; i < DSP_MAXPIPES; i++) {
 782			if (channelmask & (1 << i)) {
 783				pipe = chip->substream[i]->runtime->private_data;
 784				pipe->state = PIPE_STATE_STOPPED;
 785			}
 786		}
 787		err = stop_transport(chip, channelmask);
 788		break;
 789	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 790		for (i = 0; i < DSP_MAXPIPES; i++) {
 791			if (channelmask & (1 << i)) {
 792				pipe = chip->substream[i]->runtime->private_data;
 793				pipe->state = PIPE_STATE_PAUSED;
 794			}
 795		}
 796		err = pause_transport(chip, channelmask);
 797		break;
 798	default:
 799		err = -EINVAL;
 800	}
 801	spin_unlock(&chip->lock);
 802	return err;
 803}
 804
 805
 806
 807static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
 808{
 809	struct snd_pcm_runtime *runtime = substream->runtime;
 810	struct audiopipe *pipe = runtime->private_data;
 811	u32 counter, step;
 812
 813	/*
 814	 * IRQ handling runs concurrently. Do not share tracking of
 815	 * counter with it, which would race or require locking
 816	 */
 817
 818	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
 819
 820	step = counter - pipe->last_counter;  /* handles wrapping */
 821	pipe->last_counter = counter;
 822
 823	/* counter doesn't neccessarily wrap on a multiple of
 824	 * buffer_size, so can't derive the position; must
 825	 * accumulate */
 826
 827	pipe->position += step;
 828	pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
 829
 830	return bytes_to_frames(runtime, pipe->position);
 831}
 832
 833
 834
 835/* pcm *_ops structures */
 836static const struct snd_pcm_ops analog_playback_ops = {
 837	.open = pcm_analog_out_open,
 838	.close = pcm_close,
 839	.hw_params = pcm_analog_out_hw_params,
 840	.hw_free = pcm_hw_free,
 841	.prepare = pcm_prepare,
 842	.trigger = pcm_trigger,
 843	.pointer = pcm_pointer,
 844};
 845static const struct snd_pcm_ops analog_capture_ops = {
 846	.open = pcm_analog_in_open,
 847	.close = pcm_close,
 848	.hw_params = pcm_analog_in_hw_params,
 849	.hw_free = pcm_hw_free,
 850	.prepare = pcm_prepare,
 851	.trigger = pcm_trigger,
 852	.pointer = pcm_pointer,
 853};
 854#ifdef ECHOCARD_HAS_DIGITAL_IO
 855#ifndef ECHOCARD_HAS_VMIXER
 856static const struct snd_pcm_ops digital_playback_ops = {
 857	.open = pcm_digital_out_open,
 858	.close = pcm_close,
 859	.hw_params = pcm_digital_out_hw_params,
 860	.hw_free = pcm_hw_free,
 861	.prepare = pcm_prepare,
 862	.trigger = pcm_trigger,
 863	.pointer = pcm_pointer,
 864};
 865#endif /* !ECHOCARD_HAS_VMIXER */
 866static const struct snd_pcm_ops digital_capture_ops = {
 867	.open = pcm_digital_in_open,
 868	.close = pcm_close,
 869	.hw_params = pcm_digital_in_hw_params,
 870	.hw_free = pcm_hw_free,
 871	.prepare = pcm_prepare,
 872	.trigger = pcm_trigger,
 873	.pointer = pcm_pointer,
 874};
 875#endif /* ECHOCARD_HAS_DIGITAL_IO */
 876
 877
 878
 879/* Preallocate memory only for the first substream because it's the most
 880 * used one
 881 */
 882static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
 883{
 884	struct snd_pcm_substream *ss;
 885	int stream;
 886
 887	for (stream = 0; stream < 2; stream++)
 888		for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
 889			snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
 890						   dev,
 891						   ss->number ? 0 : 128<<10,
 892						   256<<10);
 893}
 894
 895
 896
 897/*<--snd_echo_probe() */
 898static int snd_echo_new_pcm(struct echoaudio *chip)
 899{
 900	struct snd_pcm *pcm;
 901	int err;
 902
 903#ifdef ECHOCARD_HAS_VMIXER
 904	/* This card has a Vmixer, that is there is no direct mapping from PCM
 905	streams to physical outputs. The user can mix the streams as he wishes
 906	via control interface and it's possible to send any stream to any
 907	output, thus it makes no sense to keep analog and digital outputs
 908	separated */
 909
 910	/* PCM#0 Virtual outputs and analog inputs */
 911	if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
 912				num_analog_busses_in(chip), &pcm)) < 0)
 
 913		return err;
 914	pcm->private_data = chip;
 915	chip->analog_pcm = pcm;
 916	strcpy(pcm->name, chip->card->shortname);
 917	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
 918	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
 919	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 920
 921#ifdef ECHOCARD_HAS_DIGITAL_IO
 922	/* PCM#1 Digital inputs, no outputs */
 923	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
 924			       num_digital_busses_in(chip), &pcm)) < 0)
 
 925		return err;
 926	pcm->private_data = chip;
 927	chip->digital_pcm = pcm;
 928	strcpy(pcm->name, chip->card->shortname);
 929	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
 930	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 931#endif /* ECHOCARD_HAS_DIGITAL_IO */
 932
 933#else /* ECHOCARD_HAS_VMIXER */
 934
 935	/* The card can manage substreams formed by analog and digital channels
 936	at the same time, but I prefer to keep analog and digital channels
 937	separated, because that mixed thing is confusing and useless. So we
 938	register two PCM devices: */
 939
 940	/* PCM#0 Analog i/o */
 941	if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
 942			       num_analog_busses_out(chip),
 943			       num_analog_busses_in(chip), &pcm)) < 0)
 
 944		return err;
 945	pcm->private_data = chip;
 946	chip->analog_pcm = pcm;
 947	strcpy(pcm->name, chip->card->shortname);
 948	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
 949	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
 950	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 951
 952#ifdef ECHOCARD_HAS_DIGITAL_IO
 953	/* PCM#1 Digital i/o */
 954	if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
 955			       num_digital_busses_out(chip),
 956			       num_digital_busses_in(chip), &pcm)) < 0)
 
 957		return err;
 958	pcm->private_data = chip;
 959	chip->digital_pcm = pcm;
 960	strcpy(pcm->name, chip->card->shortname);
 961	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
 962	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
 963	snd_echo_preallocate_pages(pcm, &chip->pci->dev);
 964#endif /* ECHOCARD_HAS_DIGITAL_IO */
 965
 966#endif /* ECHOCARD_HAS_VMIXER */
 967
 968	return 0;
 969}
 970
 971
 972
 973
 974/******************************************************************************
 975	Control interface
 976******************************************************************************/
 977
 978#if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
 979
 980/******************* PCM output volume *******************/
 981static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
 982				     struct snd_ctl_elem_info *uinfo)
 983{
 984	struct echoaudio *chip;
 985
 986	chip = snd_kcontrol_chip(kcontrol);
 987	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 988	uinfo->count = num_busses_out(chip);
 989	uinfo->value.integer.min = ECHOGAIN_MINOUT;
 990	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
 991	return 0;
 992}
 993
 994static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
 995				    struct snd_ctl_elem_value *ucontrol)
 996{
 997	struct echoaudio *chip;
 998	int c;
 999
1000	chip = snd_kcontrol_chip(kcontrol);
1001	for (c = 0; c < num_busses_out(chip); c++)
1002		ucontrol->value.integer.value[c] = chip->output_gain[c];
1003	return 0;
1004}
1005
1006static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1007				    struct snd_ctl_elem_value *ucontrol)
1008{
1009	struct echoaudio *chip;
1010	int c, changed, gain;
1011
1012	changed = 0;
1013	chip = snd_kcontrol_chip(kcontrol);
1014	spin_lock_irq(&chip->lock);
1015	for (c = 0; c < num_busses_out(chip); c++) {
1016		gain = ucontrol->value.integer.value[c];
1017		/* Ignore out of range values */
1018		if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1019			continue;
1020		if (chip->output_gain[c] != gain) {
1021			set_output_gain(chip, c, gain);
1022			changed = 1;
1023		}
1024	}
1025	if (changed)
1026		update_output_line_level(chip);
1027	spin_unlock_irq(&chip->lock);
1028	return changed;
1029}
1030
1031#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1032/* On the Mia this one controls the line-out volume */
1033static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1034	.name = "Line Playback Volume",
1035	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1036	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1037		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1038	.info = snd_echo_output_gain_info,
1039	.get = snd_echo_output_gain_get,
1040	.put = snd_echo_output_gain_put,
1041	.tlv = {.p = db_scale_output_gain},
1042};
1043#else
1044static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1045	.name = "PCM Playback Volume",
1046	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1047	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1048	.info = snd_echo_output_gain_info,
1049	.get = snd_echo_output_gain_get,
1050	.put = snd_echo_output_gain_put,
1051	.tlv = {.p = db_scale_output_gain},
1052};
1053#endif
1054
1055#endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1056
1057
1058
1059#ifdef ECHOCARD_HAS_INPUT_GAIN
1060
1061/******************* Analog input volume *******************/
1062static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1063				    struct snd_ctl_elem_info *uinfo)
1064{
1065	struct echoaudio *chip;
1066
1067	chip = snd_kcontrol_chip(kcontrol);
1068	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1069	uinfo->count = num_analog_busses_in(chip);
1070	uinfo->value.integer.min = ECHOGAIN_MININP;
1071	uinfo->value.integer.max = ECHOGAIN_MAXINP;
1072	return 0;
1073}
1074
1075static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1076				   struct snd_ctl_elem_value *ucontrol)
1077{
1078	struct echoaudio *chip;
1079	int c;
1080
1081	chip = snd_kcontrol_chip(kcontrol);
1082	for (c = 0; c < num_analog_busses_in(chip); c++)
1083		ucontrol->value.integer.value[c] = chip->input_gain[c];
1084	return 0;
1085}
1086
1087static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1088				   struct snd_ctl_elem_value *ucontrol)
1089{
1090	struct echoaudio *chip;
1091	int c, gain, changed;
1092
1093	changed = 0;
1094	chip = snd_kcontrol_chip(kcontrol);
1095	spin_lock_irq(&chip->lock);
1096	for (c = 0; c < num_analog_busses_in(chip); c++) {
1097		gain = ucontrol->value.integer.value[c];
1098		/* Ignore out of range values */
1099		if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1100			continue;
1101		if (chip->input_gain[c] != gain) {
1102			set_input_gain(chip, c, gain);
1103			changed = 1;
1104		}
1105	}
1106	if (changed)
1107		update_input_line_level(chip);
1108	spin_unlock_irq(&chip->lock);
1109	return changed;
1110}
1111
1112static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1113
1114static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1115	.name = "Line Capture Volume",
1116	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1117	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1118	.info = snd_echo_input_gain_info,
1119	.get = snd_echo_input_gain_get,
1120	.put = snd_echo_input_gain_put,
1121	.tlv = {.p = db_scale_input_gain},
1122};
1123
1124#endif /* ECHOCARD_HAS_INPUT_GAIN */
1125
1126
1127
1128#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1129
1130/************ Analog output nominal level (+4dBu / -10dBV) ***************/
1131static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1132					 struct snd_ctl_elem_info *uinfo)
1133{
1134	struct echoaudio *chip;
1135
1136	chip = snd_kcontrol_chip(kcontrol);
1137	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1138	uinfo->count = num_analog_busses_out(chip);
1139	uinfo->value.integer.min = 0;
1140	uinfo->value.integer.max = 1;
1141	return 0;
1142}
1143
1144static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1145				       struct snd_ctl_elem_value *ucontrol)
1146{
1147	struct echoaudio *chip;
1148	int c;
1149
1150	chip = snd_kcontrol_chip(kcontrol);
1151	for (c = 0; c < num_analog_busses_out(chip); c++)
1152		ucontrol->value.integer.value[c] = chip->nominal_level[c];
1153	return 0;
1154}
1155
1156static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1157				       struct snd_ctl_elem_value *ucontrol)
1158{
1159	struct echoaudio *chip;
1160	int c, changed;
1161
1162	changed = 0;
1163	chip = snd_kcontrol_chip(kcontrol);
1164	spin_lock_irq(&chip->lock);
1165	for (c = 0; c < num_analog_busses_out(chip); c++) {
1166		if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1167			set_nominal_level(chip, c,
1168					  ucontrol->value.integer.value[c]);
1169			changed = 1;
1170		}
1171	}
1172	if (changed)
1173		update_output_line_level(chip);
1174	spin_unlock_irq(&chip->lock);
1175	return changed;
1176}
1177
1178static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1179	.name = "Line Playback Switch (-10dBV)",
1180	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1181	.info = snd_echo_output_nominal_info,
1182	.get = snd_echo_output_nominal_get,
1183	.put = snd_echo_output_nominal_put,
1184};
1185
1186#endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1187
1188
1189
1190#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1191
1192/*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1193static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1194				       struct snd_ctl_elem_info *uinfo)
1195{
1196	struct echoaudio *chip;
1197
1198	chip = snd_kcontrol_chip(kcontrol);
1199	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1200	uinfo->count = num_analog_busses_in(chip);
1201	uinfo->value.integer.min = 0;
1202	uinfo->value.integer.max = 1;
1203	return 0;
1204}
1205
1206static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1207				      struct snd_ctl_elem_value *ucontrol)
1208{
1209	struct echoaudio *chip;
1210	int c;
1211
1212	chip = snd_kcontrol_chip(kcontrol);
1213	for (c = 0; c < num_analog_busses_in(chip); c++)
1214		ucontrol->value.integer.value[c] =
1215			chip->nominal_level[bx_analog_in(chip) + c];
1216	return 0;
1217}
1218
1219static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1220				      struct snd_ctl_elem_value *ucontrol)
1221{
1222	struct echoaudio *chip;
1223	int c, changed;
1224
1225	changed = 0;
1226	chip = snd_kcontrol_chip(kcontrol);
1227	spin_lock_irq(&chip->lock);
1228	for (c = 0; c < num_analog_busses_in(chip); c++) {
1229		if (chip->nominal_level[bx_analog_in(chip) + c] !=
1230		    ucontrol->value.integer.value[c]) {
1231			set_nominal_level(chip, bx_analog_in(chip) + c,
1232					  ucontrol->value.integer.value[c]);
1233			changed = 1;
1234		}
1235	}
1236	if (changed)
1237		update_output_line_level(chip);	/* "Output" is not a mistake
1238						 * here.
1239						 */
1240	spin_unlock_irq(&chip->lock);
1241	return changed;
1242}
1243
1244static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1245	.name = "Line Capture Switch (-10dBV)",
1246	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1247	.info = snd_echo_input_nominal_info,
1248	.get = snd_echo_input_nominal_get,
1249	.put = snd_echo_input_nominal_put,
1250};
1251
1252#endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1253
1254
1255
1256#ifdef ECHOCARD_HAS_MONITOR
1257
1258/******************* Monitor mixer *******************/
1259static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1260			       struct snd_ctl_elem_info *uinfo)
1261{
1262	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1263	uinfo->count = 1;
1264	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1265	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1266	return 0;
1267}
1268
1269static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1270			      struct snd_ctl_elem_value *ucontrol)
1271{
1272	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1273	unsigned int out = ucontrol->id.index / num_busses_in(chip);
1274	unsigned int in = ucontrol->id.index % num_busses_in(chip);
1275
1276	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1277		return -EINVAL;
1278
1279	ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1280	return 0;
1281}
1282
1283static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1284			      struct snd_ctl_elem_value *ucontrol)
1285{
1286	struct echoaudio *chip;
1287	int changed,  gain;
1288	unsigned int out, in;
1289
1290	changed = 0;
1291	chip = snd_kcontrol_chip(kcontrol);
1292	out = ucontrol->id.index / num_busses_in(chip);
1293	in = ucontrol->id.index % num_busses_in(chip);
1294	if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1295		return -EINVAL;
1296	gain = ucontrol->value.integer.value[0];
1297	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1298		return -EINVAL;
1299	if (chip->monitor_gain[out][in] != gain) {
1300		spin_lock_irq(&chip->lock);
1301		set_monitor_gain(chip, out, in, gain);
1302		update_output_line_level(chip);
1303		spin_unlock_irq(&chip->lock);
1304		changed = 1;
1305	}
1306	return changed;
1307}
1308
1309static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1310	.name = "Monitor Mixer Volume",
1311	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1312	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1313	.info = snd_echo_mixer_info,
1314	.get = snd_echo_mixer_get,
1315	.put = snd_echo_mixer_put,
1316	.tlv = {.p = db_scale_output_gain},
1317};
1318
1319#endif /* ECHOCARD_HAS_MONITOR */
1320
1321
1322
1323#ifdef ECHOCARD_HAS_VMIXER
1324
1325/******************* Vmixer *******************/
1326static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1327				struct snd_ctl_elem_info *uinfo)
1328{
1329	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1330	uinfo->count = 1;
1331	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1332	uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1333	return 0;
1334}
1335
1336static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1337			       struct snd_ctl_elem_value *ucontrol)
1338{
1339	struct echoaudio *chip;
1340
1341	chip = snd_kcontrol_chip(kcontrol);
1342	ucontrol->value.integer.value[0] =
1343		chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1344			[ucontrol->id.index % num_pipes_out(chip)];
1345	return 0;
1346}
1347
1348static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1349			       struct snd_ctl_elem_value *ucontrol)
1350{
1351	struct echoaudio *chip;
1352	int gain, changed;
1353	short vch, out;
1354
1355	changed = 0;
1356	chip = snd_kcontrol_chip(kcontrol);
1357	out = ucontrol->id.index / num_pipes_out(chip);
1358	vch = ucontrol->id.index % num_pipes_out(chip);
1359	gain = ucontrol->value.integer.value[0];
1360	if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1361		return -EINVAL;
1362	if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1363		spin_lock_irq(&chip->lock);
1364		set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1365		update_vmixer_level(chip);
1366		spin_unlock_irq(&chip->lock);
1367		changed = 1;
1368	}
1369	return changed;
1370}
1371
1372static struct snd_kcontrol_new snd_echo_vmixer = {
1373	.name = "VMixer Volume",
1374	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1375	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1376	.info = snd_echo_vmixer_info,
1377	.get = snd_echo_vmixer_get,
1378	.put = snd_echo_vmixer_put,
1379	.tlv = {.p = db_scale_output_gain},
1380};
1381
1382#endif /* ECHOCARD_HAS_VMIXER */
1383
1384
1385
1386#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1387
1388/******************* Digital mode switch *******************/
1389static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1390				      struct snd_ctl_elem_info *uinfo)
1391{
1392	static const char * const names[4] = {
1393		"S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1394		"S/PDIF Cdrom"
1395	};
1396	struct echoaudio *chip;
1397
1398	chip = snd_kcontrol_chip(kcontrol);
1399	return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1400}
1401
1402static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1403				     struct snd_ctl_elem_value *ucontrol)
1404{
1405	struct echoaudio *chip;
1406	int i, mode;
1407
1408	chip = snd_kcontrol_chip(kcontrol);
1409	mode = chip->digital_mode;
1410	for (i = chip->num_digital_modes - 1; i >= 0; i--)
1411		if (mode == chip->digital_mode_list[i]) {
1412			ucontrol->value.enumerated.item[0] = i;
1413			break;
1414		}
1415	return 0;
1416}
1417
1418static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1419				     struct snd_ctl_elem_value *ucontrol)
1420{
1421	struct echoaudio *chip;
1422	int changed;
1423	unsigned short emode, dmode;
1424
1425	changed = 0;
1426	chip = snd_kcontrol_chip(kcontrol);
1427
1428	emode = ucontrol->value.enumerated.item[0];
1429	if (emode >= chip->num_digital_modes)
1430		return -EINVAL;
1431	dmode = chip->digital_mode_list[emode];
1432
1433	if (dmode != chip->digital_mode) {
1434		/* mode_mutex is required to make this operation atomic wrt
1435		pcm_digital_*_open() and set_input_clock() functions. */
1436		mutex_lock(&chip->mode_mutex);
1437
1438		/* Do not allow the user to change the digital mode when a pcm
1439		device is open because it also changes the number of channels
1440		and the allowed sample rates */
1441		if (chip->opencount) {
1442			changed = -EAGAIN;
1443		} else {
1444			changed = set_digital_mode(chip, dmode);
1445			/* If we had to change the clock source, report it */
1446			if (changed > 0 && chip->clock_src_ctl) {
1447				snd_ctl_notify(chip->card,
1448					       SNDRV_CTL_EVENT_MASK_VALUE,
1449					       &chip->clock_src_ctl->id);
1450				dev_dbg(chip->card->dev,
1451					"SDM() =%d\n", changed);
1452			}
1453			if (changed >= 0)
1454				changed = 1;	/* No errors */
1455		}
1456		mutex_unlock(&chip->mode_mutex);
1457	}
1458	return changed;
1459}
1460
1461static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1462	.name = "Digital mode Switch",
1463	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1464	.info = snd_echo_digital_mode_info,
1465	.get = snd_echo_digital_mode_get,
1466	.put = snd_echo_digital_mode_put,
1467};
1468
1469#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1470
1471
1472
1473#ifdef ECHOCARD_HAS_DIGITAL_IO
1474
1475/******************* S/PDIF mode switch *******************/
1476static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1477				    struct snd_ctl_elem_info *uinfo)
1478{
1479	static const char * const names[2] = {"Consumer", "Professional"};
1480
1481	return snd_ctl_enum_info(uinfo, 1, 2, names);
1482}
1483
1484static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1485				   struct snd_ctl_elem_value *ucontrol)
1486{
1487	struct echoaudio *chip;
1488
1489	chip = snd_kcontrol_chip(kcontrol);
1490	ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1491	return 0;
1492}
1493
1494static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1495				   struct snd_ctl_elem_value *ucontrol)
1496{
1497	struct echoaudio *chip;
1498	int mode;
1499
1500	chip = snd_kcontrol_chip(kcontrol);
1501	mode = !!ucontrol->value.enumerated.item[0];
1502	if (mode != chip->professional_spdif) {
1503		spin_lock_irq(&chip->lock);
1504		set_professional_spdif(chip, mode);
1505		spin_unlock_irq(&chip->lock);
1506		return 1;
1507	}
1508	return 0;
1509}
1510
1511static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1512	.name = "S/PDIF mode Switch",
1513	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1514	.info = snd_echo_spdif_mode_info,
1515	.get = snd_echo_spdif_mode_get,
1516	.put = snd_echo_spdif_mode_put,
1517};
1518
1519#endif /* ECHOCARD_HAS_DIGITAL_IO */
1520
1521
1522
1523#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1524
1525/******************* Select input clock source *******************/
1526static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1527				      struct snd_ctl_elem_info *uinfo)
1528{
1529	static const char * const names[8] = {
1530		"Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1531		"ESync96", "MTC"
1532	};
1533	struct echoaudio *chip;
1534
1535	chip = snd_kcontrol_chip(kcontrol);
1536	return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1537}
1538
1539static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1540				     struct snd_ctl_elem_value *ucontrol)
1541{
1542	struct echoaudio *chip;
1543	int i, clock;
1544
1545	chip = snd_kcontrol_chip(kcontrol);
1546	clock = chip->input_clock;
1547
1548	for (i = 0; i < chip->num_clock_sources; i++)
1549		if (clock == chip->clock_source_list[i])
1550			ucontrol->value.enumerated.item[0] = i;
1551
1552	return 0;
1553}
1554
1555static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1556				     struct snd_ctl_elem_value *ucontrol)
1557{
1558	struct echoaudio *chip;
1559	int changed;
1560	unsigned int eclock, dclock;
1561
1562	changed = 0;
1563	chip = snd_kcontrol_chip(kcontrol);
1564	eclock = ucontrol->value.enumerated.item[0];
1565	if (eclock >= chip->input_clock_types)
1566		return -EINVAL;
1567	dclock = chip->clock_source_list[eclock];
1568	if (chip->input_clock != dclock) {
1569		mutex_lock(&chip->mode_mutex);
1570		spin_lock_irq(&chip->lock);
1571		if ((changed = set_input_clock(chip, dclock)) == 0)
 
1572			changed = 1;	/* no errors */
1573		spin_unlock_irq(&chip->lock);
1574		mutex_unlock(&chip->mode_mutex);
1575	}
1576
1577	if (changed < 0)
1578		dev_dbg(chip->card->dev,
1579			"seticlk val%d err 0x%x\n", dclock, changed);
1580
1581	return changed;
1582}
1583
1584static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1585	.name = "Sample Clock Source",
1586	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1587	.info = snd_echo_clock_source_info,
1588	.get = snd_echo_clock_source_get,
1589	.put = snd_echo_clock_source_put,
1590};
1591
1592#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1593
1594
1595
1596#ifdef ECHOCARD_HAS_PHANTOM_POWER
1597
1598/******************* Phantom power switch *******************/
1599#define snd_echo_phantom_power_info	snd_ctl_boolean_mono_info
1600
1601static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1602				      struct snd_ctl_elem_value *ucontrol)
1603{
1604	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1605
1606	ucontrol->value.integer.value[0] = chip->phantom_power;
1607	return 0;
1608}
1609
1610static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1611				      struct snd_ctl_elem_value *ucontrol)
1612{
1613	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1614	int power, changed = 0;
1615
1616	power = !!ucontrol->value.integer.value[0];
1617	if (chip->phantom_power != power) {
1618		spin_lock_irq(&chip->lock);
1619		changed = set_phantom_power(chip, power);
1620		spin_unlock_irq(&chip->lock);
1621		if (changed == 0)
1622			changed = 1;	/* no errors */
1623	}
1624	return changed;
1625}
1626
1627static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1628	.name = "Phantom power Switch",
1629	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1630	.info = snd_echo_phantom_power_info,
1631	.get = snd_echo_phantom_power_get,
1632	.put = snd_echo_phantom_power_put,
1633};
1634
1635#endif /* ECHOCARD_HAS_PHANTOM_POWER */
1636
1637
1638
1639#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1640
1641/******************* Digital input automute switch *******************/
1642#define snd_echo_automute_info		snd_ctl_boolean_mono_info
1643
1644static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1645				 struct snd_ctl_elem_value *ucontrol)
1646{
1647	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1648
1649	ucontrol->value.integer.value[0] = chip->digital_in_automute;
1650	return 0;
1651}
1652
1653static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1654				 struct snd_ctl_elem_value *ucontrol)
1655{
1656	struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1657	int automute, changed = 0;
1658
1659	automute = !!ucontrol->value.integer.value[0];
1660	if (chip->digital_in_automute != automute) {
1661		spin_lock_irq(&chip->lock);
1662		changed = set_input_auto_mute(chip, automute);
1663		spin_unlock_irq(&chip->lock);
1664		if (changed == 0)
1665			changed = 1;	/* no errors */
1666	}
1667	return changed;
1668}
1669
1670static const struct snd_kcontrol_new snd_echo_automute_switch = {
1671	.name = "Digital Capture Switch (automute)",
1672	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1673	.info = snd_echo_automute_info,
1674	.get = snd_echo_automute_get,
1675	.put = snd_echo_automute_put,
1676};
1677
1678#endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1679
1680
1681
1682/******************* VU-meters switch *******************/
1683#define snd_echo_vumeters_switch_info		snd_ctl_boolean_mono_info
1684
1685static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1686					struct snd_ctl_elem_value *ucontrol)
1687{
1688	struct echoaudio *chip;
1689
1690	chip = snd_kcontrol_chip(kcontrol);
1691	spin_lock_irq(&chip->lock);
1692	set_meters_on(chip, ucontrol->value.integer.value[0]);
1693	spin_unlock_irq(&chip->lock);
1694	return 1;
1695}
1696
1697static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1698	.name = "VU-meters Switch",
1699	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
1700	.access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1701	.info = snd_echo_vumeters_switch_info,
1702	.put = snd_echo_vumeters_switch_put,
1703};
1704
1705
1706
1707/***** Read VU-meters (input, output, analog and digital together) *****/
1708static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1709				  struct snd_ctl_elem_info *uinfo)
1710{
1711	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1712	uinfo->count = 96;
1713	uinfo->value.integer.min = ECHOGAIN_MINOUT;
1714	uinfo->value.integer.max = 0;
1715	return 0;
1716}
1717
1718static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1719				 struct snd_ctl_elem_value *ucontrol)
1720{
1721	struct echoaudio *chip;
1722
1723	chip = snd_kcontrol_chip(kcontrol);
1724	get_audio_meters(chip, ucontrol->value.integer.value);
1725	return 0;
1726}
1727
1728static const struct snd_kcontrol_new snd_echo_vumeters = {
1729	.name = "VU-meters",
1730	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1731	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1732		  SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1733		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1734	.info = snd_echo_vumeters_info,
1735	.get = snd_echo_vumeters_get,
1736	.tlv = {.p = db_scale_output_gain},
1737};
1738
1739
1740
1741/*** Channels info - it exports informations about the number of channels ***/
1742static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1743				       struct snd_ctl_elem_info *uinfo)
1744{
1745	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1746	uinfo->count = 6;
1747	uinfo->value.integer.min = 0;
1748	uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1749	return 0;
1750}
1751
1752static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1753				      struct snd_ctl_elem_value *ucontrol)
1754{
1755	struct echoaudio *chip;
1756	int detected, clocks, bit, src;
1757
1758	chip = snd_kcontrol_chip(kcontrol);
1759	ucontrol->value.integer.value[0] = num_busses_in(chip);
1760	ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1761	ucontrol->value.integer.value[2] = num_busses_out(chip);
1762	ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1763	ucontrol->value.integer.value[4] = num_pipes_out(chip);
1764
1765	/* Compute the bitmask of the currently valid input clocks */
1766	detected = detect_input_clocks(chip);
1767	clocks = 0;
1768	src = chip->num_clock_sources - 1;
1769	for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1770		if (detected & (1 << bit))
1771			for (; src >= 0; src--)
1772				if (bit == chip->clock_source_list[src]) {
1773					clocks |= 1 << src;
1774					break;
1775				}
1776	ucontrol->value.integer.value[5] = clocks;
1777
1778	return 0;
1779}
1780
1781static const struct snd_kcontrol_new snd_echo_channels_info = {
1782	.name = "Channels info",
1783	.iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1784	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1785	.info = snd_echo_channels_info_info,
1786	.get = snd_echo_channels_info_get,
1787};
1788
1789
1790
1791
1792/******************************************************************************
1793	IRQ Handling
1794******************************************************************************/
1795/* Check if a period has elapsed since last interrupt
1796 *
1797 * Don't make any updates to state; PCM core handles this with the
1798 * correct locks.
1799 *
1800 * \return true if a period has elapsed, otherwise false
1801 */
1802static bool period_has_elapsed(struct snd_pcm_substream *substream)
1803{
1804	struct snd_pcm_runtime *runtime = substream->runtime;
1805	struct audiopipe *pipe = runtime->private_data;
1806	u32 counter, step;
1807	size_t period_bytes;
1808
1809	if (pipe->state != PIPE_STATE_STARTED)
1810		return false;
1811
1812	period_bytes = frames_to_bytes(runtime, runtime->period_size);
1813
1814	counter = le32_to_cpu(*pipe->dma_counter);  /* presumed atomic */
1815
1816	step = counter - pipe->last_period;  /* handles wrapping */
1817	step -= step % period_bytes;  /* acknowledge whole periods only */
1818
1819	if (step == 0)
1820		return false;  /* haven't advanced a whole period yet */
1821
1822	pipe->last_period += step;  /* used exclusively by us */
1823	return true;
1824}
1825
1826static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1827{
1828	struct echoaudio *chip = dev_id;
1829	int ss, st;
1830
1831	spin_lock(&chip->lock);
1832	st = service_irq(chip);
1833	if (st < 0) {
1834		spin_unlock(&chip->lock);
1835		return IRQ_NONE;
1836	}
1837	/* The hardware doesn't tell us which substream caused the irq,
1838	thus we have to check all running substreams. */
1839	for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1840		struct snd_pcm_substream *substream;
1841
1842		substream = chip->substream[ss];
1843		if (substream && period_has_elapsed(substream)) {
1844			spin_unlock(&chip->lock);
1845			snd_pcm_period_elapsed(substream);
1846			spin_lock(&chip->lock);
1847		}
1848	}
1849	spin_unlock(&chip->lock);
1850
1851#ifdef ECHOCARD_HAS_MIDI
1852	if (st > 0 && chip->midi_in) {
1853		snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1854		dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1855	}
1856#endif
1857	return IRQ_HANDLED;
1858}
1859
1860
1861
1862
1863/******************************************************************************
1864	Module construction / destruction
1865******************************************************************************/
1866
1867static int snd_echo_free(struct echoaudio *chip)
1868{
 
 
1869	if (chip->comm_page)
1870		rest_in_peace(chip);
1871
1872	if (chip->irq >= 0)
1873		free_irq(chip->irq, chip);
1874
1875	if (chip->comm_page)
1876		snd_dma_free_pages(&chip->commpage_dma_buf);
1877
1878	iounmap(chip->dsp_registers);
1879	release_and_free_resource(chip->iores);
1880	pci_disable_device(chip->pci);
1881
1882	/* release chip data */
1883	free_firmware_cache(chip);
1884	kfree(chip);
1885	return 0;
1886}
1887
1888
1889
1890static int snd_echo_dev_free(struct snd_device *device)
1891{
1892	struct echoaudio *chip = device->device_data;
1893
1894	return snd_echo_free(chip);
1895}
1896
1897
1898
1899/* <--snd_echo_probe() */
1900static int snd_echo_create(struct snd_card *card,
1901			   struct pci_dev *pci,
1902			   struct echoaudio **rchip)
1903{
1904	struct echoaudio *chip;
1905	int err;
1906	size_t sz;
1907	static const struct snd_device_ops ops = {
1908		.dev_free = snd_echo_dev_free,
1909	};
1910
1911	*rchip = NULL;
1912
1913	pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1914
1915	if ((err = pci_enable_device(pci)) < 0)
 
1916		return err;
1917	pci_set_master(pci);
1918
1919	/* Allocate chip if needed */
1920	if (!*rchip) {
1921		chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1922		if (!chip) {
1923			pci_disable_device(pci);
1924			return -ENOMEM;
1925		}
1926		dev_dbg(card->dev, "chip=%p\n", chip);
1927		spin_lock_init(&chip->lock);
1928		chip->card = card;
1929		chip->pci = pci;
1930		chip->irq = -1;
1931		chip->opencount = 0;
1932		mutex_init(&chip->mode_mutex);
1933		chip->can_set_rate = 1;
1934	} else {
1935		/* If this was called from the resume function, chip is
1936		 * already allocated and it contains current card settings.
1937		 */
1938		chip = *rchip;
1939	}
1940
1941	/* PCI resource allocation */
 
 
 
 
1942	chip->dsp_registers_phys = pci_resource_start(pci, 0);
1943	sz = pci_resource_len(pci, 0);
1944	if (sz > PAGE_SIZE)
1945		sz = PAGE_SIZE;		/* We map only the required part */
1946
1947	if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1948					      ECHOCARD_NAME)) == NULL) {
1949		dev_err(chip->card->dev, "cannot get memory region\n");
1950		snd_echo_free(chip);
1951		return -EBUSY;
1952	}
1953	chip->dsp_registers = ioremap(chip->dsp_registers_phys, sz);
1954	if (!chip->dsp_registers) {
1955		dev_err(chip->card->dev, "ioremap failed\n");
1956		snd_echo_free(chip);
1957		return -ENOMEM;
1958	}
1959
1960	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1961			KBUILD_MODNAME, chip)) {
1962		dev_err(chip->card->dev, "cannot grab irq\n");
1963		snd_echo_free(chip);
1964		return -EBUSY;
1965	}
1966	chip->irq = pci->irq;
1967	card->sync_irq = chip->irq;
1968	dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1969		chip->pci, chip->irq, chip->pci->subsystem_device);
1970
 
 
1971	/* Create the DSP comm page - this is the area of memory used for most
1972	of the communication with the DSP, which accesses it via bus mastering */
1973	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
1974				sizeof(struct comm_page),
1975				&chip->commpage_dma_buf) < 0) {
1976		dev_err(chip->card->dev, "cannot allocate the comm page\n");
1977		snd_echo_free(chip);
1978		return -ENOMEM;
1979	}
1980	chip->comm_page_phys = chip->commpage_dma_buf.addr;
1981	chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1982
1983	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1984	if (err >= 0)
1985		err = set_mixer_defaults(chip);
1986	if (err < 0) {
1987		dev_err(card->dev, "init_hw err=%d\n", err);
1988		snd_echo_free(chip);
1989		return err;
1990	}
1991
1992	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1993		snd_echo_free(chip);
1994		return err;
1995	}
1996	*rchip = chip;
1997	/* Init done ! */
1998	return 0;
1999}
2000
2001
2002
2003/* constructor */
2004static int snd_echo_probe(struct pci_dev *pci,
2005			  const struct pci_device_id *pci_id)
2006{
2007	static int dev;
2008	struct snd_card *card;
2009	struct echoaudio *chip;
2010	char *dsp;
2011	__maybe_unused int i;
2012	int err;
2013
2014	if (dev >= SNDRV_CARDS)
2015		return -ENODEV;
2016	if (!enable[dev]) {
2017		dev++;
2018		return -ENOENT;
2019	}
2020
2021	i = 0;
2022	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2023			   0, &card);
2024	if (err < 0)
2025		return err;
 
2026
2027	chip = NULL;	/* Tells snd_echo_create to allocate chip */
2028	if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2029		snd_card_free(card);
2030		return err;
2031	}
2032
2033	strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2034	strcpy(card->shortname, chip->card_name);
2035
2036	dsp = "56301";
2037	if (pci_id->device == 0x3410)
2038		dsp = "56361";
2039
2040	sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2041		card->shortname, pci_id->subdevice & 0x000f, dsp,
2042		chip->dsp_registers_phys, chip->irq);
2043
2044	if ((err = snd_echo_new_pcm(chip)) < 0) {
 
2045		dev_err(chip->card->dev, "new pcm error %d\n", err);
2046		snd_card_free(card);
2047		return err;
2048	}
2049
2050#ifdef ECHOCARD_HAS_MIDI
2051	if (chip->has_midi) {	/* Some Mia's do not have midi */
2052		if ((err = snd_echo_midi_create(card, chip)) < 0) {
 
2053			dev_err(chip->card->dev, "new midi error %d\n", err);
2054			snd_card_free(card);
2055			return err;
2056		}
2057	}
2058#endif
2059
2060#ifdef ECHOCARD_HAS_VMIXER
2061	snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2062	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2063		goto ctl_error;
 
2064#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2065	err = snd_ctl_add(chip->card,
2066			  snd_ctl_new1(&snd_echo_line_output_gain, chip));
2067	if (err < 0)
2068		goto ctl_error;
2069#endif
2070#else /* ECHOCARD_HAS_VMIXER */
2071	err = snd_ctl_add(chip->card,
2072			  snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2073	if (err < 0)
2074		goto ctl_error;
2075#endif /* ECHOCARD_HAS_VMIXER */
2076
2077#ifdef ECHOCARD_HAS_INPUT_GAIN
2078	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2079		goto ctl_error;
 
2080#endif
2081
2082#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2083	if (!chip->hasnt_input_nominal_level)
2084		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2085			goto ctl_error;
 
 
2086#endif
2087
2088#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2089	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2090		goto ctl_error;
 
2091#endif
2092
2093	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2094		goto ctl_error;
 
2095
2096	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2097		goto ctl_error;
 
2098
2099#ifdef ECHOCARD_HAS_MONITOR
2100	snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2101	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2102		goto ctl_error;
 
2103#endif
2104
2105#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2106	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2107		goto ctl_error;
 
2108#endif
2109
2110	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2111		goto ctl_error;
 
2112
2113#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2114	/* Creates a list of available digital modes */
2115	chip->num_digital_modes = 0;
2116	for (i = 0; i < 6; i++)
2117		if (chip->digital_modes & (1 << i))
2118			chip->digital_mode_list[chip->num_digital_modes++] = i;
2119
2120	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2121		goto ctl_error;
 
2122#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2123
2124#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2125	/* Creates a list of available clock sources */
2126	chip->num_clock_sources = 0;
2127	for (i = 0; i < 10; i++)
2128		if (chip->input_clock_types & (1 << i))
2129			chip->clock_source_list[chip->num_clock_sources++] = i;
2130
2131	if (chip->num_clock_sources > 1) {
2132		chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2133		if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2134			goto ctl_error;
 
2135	}
2136#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2137
2138#ifdef ECHOCARD_HAS_DIGITAL_IO
2139	if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2140		goto ctl_error;
 
2141#endif
2142
2143#ifdef ECHOCARD_HAS_PHANTOM_POWER
2144	if (chip->has_phantom_power)
2145		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2146			goto ctl_error;
 
 
2147#endif
2148
2149	err = snd_card_register(card);
2150	if (err < 0)
2151		goto ctl_error;
2152	dev_info(card->dev, "Card registered: %s\n", card->longname);
2153
2154	pci_set_drvdata(pci, chip);
2155	dev++;
2156	return 0;
 
2157
2158ctl_error:
2159	dev_err(card->dev, "new control error %d\n", err);
2160	snd_card_free(card);
2161	return err;
2162}
2163
2164
2165
2166#if defined(CONFIG_PM_SLEEP)
2167
2168static int snd_echo_suspend(struct device *dev)
2169{
2170	struct echoaudio *chip = dev_get_drvdata(dev);
2171
2172#ifdef ECHOCARD_HAS_MIDI
2173	/* This call can sleep */
2174	if (chip->midi_out)
2175		snd_echo_midi_output_trigger(chip->midi_out, 0);
2176#endif
2177	spin_lock_irq(&chip->lock);
2178	if (wait_handshake(chip)) {
2179		spin_unlock_irq(&chip->lock);
2180		return -EIO;
2181	}
2182	clear_handshake(chip);
2183	if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2184		spin_unlock_irq(&chip->lock);
2185		return -EIO;
2186	}
2187	spin_unlock_irq(&chip->lock);
2188
2189	chip->dsp_code = NULL;
2190	free_irq(chip->irq, chip);
2191	chip->irq = -1;
2192	chip->card->sync_irq = -1;
2193	return 0;
2194}
2195
2196
2197
2198static int snd_echo_resume(struct device *dev)
2199{
2200	struct pci_dev *pci = to_pci_dev(dev);
2201	struct echoaudio *chip = dev_get_drvdata(dev);
2202	struct comm_page *commpage, *commpage_bak;
2203	u32 pipe_alloc_mask;
2204	int err;
2205
2206	commpage = chip->comm_page;
2207	commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2208	if (commpage_bak == NULL)
2209		return -ENOMEM;
2210
2211	err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2212	if (err < 0) {
2213		kfree(commpage_bak);
2214		dev_err(dev, "resume init_hw err=%d\n", err);
2215		return err;
2216	}
2217
2218	/* Temporarily set chip->pipe_alloc_mask=0 otherwise
2219	 * restore_dsp_settings() fails.
2220	 */
2221	pipe_alloc_mask = chip->pipe_alloc_mask;
2222	chip->pipe_alloc_mask = 0;
2223	err = restore_dsp_rettings(chip);
2224	chip->pipe_alloc_mask = pipe_alloc_mask;
2225	if (err < 0) {
2226		kfree(commpage_bak);
2227		return err;
2228	}
2229
2230	memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2231		sizeof(commpage->audio_format));
2232	memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2233		sizeof(commpage->sglist_addr));
2234	memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2235		sizeof(commpage->midi_output));
2236	kfree(commpage_bak);
2237
2238	if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2239			KBUILD_MODNAME, chip)) {
2240		dev_err(chip->card->dev, "cannot grab irq\n");
2241		return -EBUSY;
2242	}
2243	chip->irq = pci->irq;
2244	chip->card->sync_irq = chip->irq;
2245	dev_dbg(dev, "resume irq=%d\n", chip->irq);
2246
2247#ifdef ECHOCARD_HAS_MIDI
2248	if (chip->midi_input_enabled)
2249		enable_midi_input(chip, true);
2250	if (chip->midi_out)
2251		snd_echo_midi_output_trigger(chip->midi_out, 1);
2252#endif
2253
2254	return 0;
2255}
2256
2257static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2258#define SND_ECHO_PM_OPS	&snd_echo_pm
2259#else
2260#define SND_ECHO_PM_OPS	NULL
2261#endif /* CONFIG_PM_SLEEP */
2262
2263
2264static void snd_echo_remove(struct pci_dev *pci)
2265{
2266	struct echoaudio *chip;
2267
2268	chip = pci_get_drvdata(pci);
2269	if (chip)
2270		snd_card_free(chip->card);
2271}
2272
2273
2274
2275/******************************************************************************
2276	Everything starts and ends here
2277******************************************************************************/
2278
2279/* pci_driver definition */
2280static struct pci_driver echo_driver = {
2281	.name = KBUILD_MODNAME,
2282	.id_table = snd_echo_ids,
2283	.probe = snd_echo_probe,
2284	.remove = snd_echo_remove,
2285	.driver = {
2286		.pm = SND_ECHO_PM_OPS,
2287	},
2288};
2289
2290module_pci_driver(echo_driver);