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

#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/firmware.h>
29
#include <linux/module.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/asoundef.h>
#include <sound/info.h>
#include <asm/io.h>
#include <sound/vx_core.h>
#include "vx_cmd.h"

MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
MODULE_DESCRIPTION("Common routines for Digigram VX drivers");
MODULE_LICENSE("GPL");


/*
 * vx_check_reg_bit - wait for the specified bit is set/reset on a register
 * @reg: register to check
 * @mask: bit mask
 * @bit: resultant bit to be checked
 * @time: time-out of loop in msec
 *
 * returns zero if a bit matches, or a negative error code.
 */
52
int snd_vx_check_reg_bit(struct vx_core *chip, int reg, int mask, int bit, int time)
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61
{
	unsigned long end_time = jiffies + (time * HZ + 999) / 1000;
	static char *reg_names[VX_REG_MAX] = {
		"ICR", "CVR", "ISR", "IVR", "RXH", "RXM", "RXL",
		"DMA", "CDSP", "RFREQ", "RUER/V2", "DATA", "MEMIRQ",
		"ACQ", "BIT0", "BIT1", "MIC0", "MIC1", "MIC2",
		"MIC3", "INTCSR", "CNTRL", "GPIOC",
		"LOFREQ", "HIFREQ", "CSUER", "RUER"
	};
62

L
Linus Torvalds 已提交
63 64 65
	do {
		if ((snd_vx_inb(chip, reg) & mask) == bit)
			return 0;
66
		//msleep(10);
L
Linus Torvalds 已提交
67 68 69 70 71
	} while (time_after_eq(end_time, jiffies));
	snd_printd(KERN_DEBUG "vx_check_reg_bit: timeout, reg=%s, mask=0x%x, val=0x%x\n", reg_names[reg], mask, snd_vx_inb(chip, reg));
	return -EIO;
}

72 73
EXPORT_SYMBOL(snd_vx_check_reg_bit);

L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81
/*
 * vx_send_irq_dsp - set command irq bit
 * @num: the requested IRQ type, IRQ_XXX
 *
 * this triggers the specified IRQ request
 * returns 0 if successful, or a negative error code.
 * 
 */
82
static int vx_send_irq_dsp(struct vx_core *chip, int num)
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	int nirq;

	/* wait for Hc = 0 */
	if (snd_vx_check_reg_bit(chip, VX_CVR, CVR_HC, 0, 200) < 0)
		return -EIO;

	nirq = num;
	if (vx_has_new_dsp(chip))
		nirq += VXP_IRQ_OFFSET;
	vx_outb(chip, CVR, (nirq >> 1) | CVR_HC);
	return 0;
}


/*
 * vx_reset_chk - reset CHK bit on ISR
 *
 * returns 0 if successful, or a negative error code.
 */
103
static int vx_reset_chk(struct vx_core *chip)
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
	/* Reset irq CHK */
	if (vx_send_irq_dsp(chip, IRQ_RESET_CHK) < 0)
		return -EIO;
	/* Wait until CHK = 0 */
	if (vx_check_isr(chip, ISR_CHK, 0, 200) < 0)
		return -EIO;
	return 0;
}

/*
 * vx_transfer_end - terminate message transfer
 * @cmd: IRQ message to send (IRQ_MESS_XXX_END)
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
T
Takashi Iwai 已提交
120
 * NB: call with mutex held!
L
Linus Torvalds 已提交
121
 */
122
static int vx_transfer_end(struct vx_core *chip, int cmd)
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
	int err;

	if ((err = vx_reset_chk(chip)) < 0)
		return err;

	/* irq MESS_READ/WRITE_END */
	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
		return err;

	/* Wait CHK = 1 */
	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
		return err;

	/* If error, Read RX */
	if ((err = vx_inb(chip, ISR)) & ISR_ERR) {
		if ((err = vx_wait_for_rx_full(chip)) < 0) {
			snd_printd(KERN_DEBUG "transfer_end: error in rx_full\n");
			return err;
		}
		err = vx_inb(chip, RXH) << 16;
		err |= vx_inb(chip, RXM) << 8;
		err |= vx_inb(chip, RXL);
		snd_printd(KERN_DEBUG "transfer_end: error = 0x%x\n", err);
		return -(VX_ERR_MASK | err);
	}
	return 0;
}

/*
 * vx_read_status - return the status rmh
 * @rmh: rmh record to store the status
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
T
Takashi Iwai 已提交
158
 * NB: call with mutex held!
L
Linus Torvalds 已提交
159
 */
160
static int vx_read_status(struct vx_core *chip, struct vx_rmh *rmh)
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
{
	int i, err, val, size;

	/* no read necessary? */
	if (rmh->DspStat == RMH_SSIZE_FIXED && rmh->LgStat == 0)
		return 0;

	/* Wait for RX full (with timeout protection)
	 * The first word of status is in RX
	 */
	err = vx_wait_for_rx_full(chip);
	if (err < 0)
		return err;

	/* Read RX */
	val = vx_inb(chip, RXH) << 16;
	val |= vx_inb(chip, RXM) << 8;
	val |= vx_inb(chip, RXL);

	/* If status given by DSP, let's decode its size */
	switch (rmh->DspStat) {
	case RMH_SSIZE_ARG:
		size = val & 0xff;
		rmh->Stat[0] = val & 0xffff00;
		rmh->LgStat = size + 1;
		break;
	case RMH_SSIZE_MASK:
		/* Let's count the arg numbers from a mask */
		rmh->Stat[0] = val;
		size = 0;
		while (val) {
			if (val & 0x01)
				size++;
			val >>= 1;
		}
		rmh->LgStat = size + 1;
		break;
	default:
		/* else retrieve the status length given by the driver */
		size = rmh->LgStat;
		rmh->Stat[0] = val;  /* Val is the status 1st word */
		size--;              /* hence adjust remaining length */
		break;
        }

	if (size < 1)
		return 0;
208
	if (snd_BUG_ON(size >= SIZE_MAX_STATUS))
209
		return -EINVAL;
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

	for (i = 1; i <= size; i++) {
		/* trigger an irq MESS_WRITE_NEXT */
		err = vx_send_irq_dsp(chip, IRQ_MESS_WRITE_NEXT);
		if (err < 0)
			return err;
		/* Wait for RX full (with timeout protection) */
		err = vx_wait_for_rx_full(chip);
		if (err < 0)
			return err;
		rmh->Stat[i] = vx_inb(chip, RXH) << 16;
		rmh->Stat[i] |= vx_inb(chip, RXM) <<  8;
		rmh->Stat[i] |= vx_inb(chip, RXL);
	}

	return vx_transfer_end(chip, IRQ_MESS_WRITE_END);
}


#define MASK_MORE_THAN_1_WORD_COMMAND   0x00008000
#define MASK_1_WORD_COMMAND             0x00ff7fff

/*
 * vx_send_msg_nolock - send a DSP message and read back the status
 * @rmh: the rmh record to send and receive
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 * 
T
Takashi Iwai 已提交
239
 * this function doesn't call mutex lock at all.
L
Linus Torvalds 已提交
240
 */
241
int vx_send_msg_nolock(struct vx_core *chip, struct vx_rmh *rmh)
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
	int i, err;
	
	if (chip->chip_status & VX_STAT_IS_STALE)
		return -EBUSY;

	if ((err = vx_reset_chk(chip)) < 0) {
		snd_printd(KERN_DEBUG "vx_send_msg: vx_reset_chk error\n");
		return err;
	}

#if 0
	printk(KERN_DEBUG "rmh: cmd = 0x%06x, length = %d, stype = %d\n",
	       rmh->Cmd[0], rmh->LgCmd, rmh->DspStat);
	if (rmh->LgCmd > 1) {
		printk(KERN_DEBUG "  ");
		for (i = 1; i < rmh->LgCmd; i++)
			printk("0x%06x ", rmh->Cmd[i]);
		printk("\n");
	}
#endif
	/* Check bit M is set according to length of the command */
	if (rmh->LgCmd > 1)
		rmh->Cmd[0] |= MASK_MORE_THAN_1_WORD_COMMAND;
	else
		rmh->Cmd[0] &= MASK_1_WORD_COMMAND;

	/* Wait for TX empty */
	if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
		snd_printd(KERN_DEBUG "vx_send_msg: wait tx empty error\n");
		return err;
	}

	/* Write Cmd[0] */
	vx_outb(chip, TXH, (rmh->Cmd[0] >> 16) & 0xff);
	vx_outb(chip, TXM, (rmh->Cmd[0] >> 8) & 0xff);
	vx_outb(chip, TXL, rmh->Cmd[0] & 0xff);

	/* Trigger irq MESSAGE */
	if ((err = vx_send_irq_dsp(chip, IRQ_MESSAGE)) < 0) {
		snd_printd(KERN_DEBUG "vx_send_msg: send IRQ_MESSAGE error\n");
		return err;
	}

	/* Wait for CHK = 1 */
	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
		return err;

	/* If error, get error value from RX */
	if (vx_inb(chip, ISR) & ISR_ERR) {
		if ((err = vx_wait_for_rx_full(chip)) < 0) {
			snd_printd(KERN_DEBUG "vx_send_msg: rx_full read error\n");
			return err;
		}
		err = vx_inb(chip, RXH) << 16;
		err |= vx_inb(chip, RXM) << 8;
		err |= vx_inb(chip, RXL);
		snd_printd(KERN_DEBUG "msg got error = 0x%x at cmd[0]\n", err);
		err = -(VX_ERR_MASK | err);
		return err;
	}

	/* Send the other words */
	if (rmh->LgCmd > 1) {
		for (i = 1; i < rmh->LgCmd; i++) {
			/* Wait for TX ready */
			if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
				snd_printd(KERN_DEBUG "vx_send_msg: tx_ready error\n");
				return err;
			}

			/* Write Cmd[i] */
			vx_outb(chip, TXH, (rmh->Cmd[i] >> 16) & 0xff);
			vx_outb(chip, TXM, (rmh->Cmd[i] >> 8) & 0xff);
			vx_outb(chip, TXL, rmh->Cmd[i] & 0xff);

			/* Trigger irq MESS_READ_NEXT */
			if ((err = vx_send_irq_dsp(chip, IRQ_MESS_READ_NEXT)) < 0) {
				snd_printd(KERN_DEBUG "vx_send_msg: IRQ_READ_NEXT error\n");
				return err;
			}
		}
		/* Wait for TX empty */
		if ((err = vx_wait_isr_bit(chip, ISR_TX_READY)) < 0) {
			snd_printd(KERN_DEBUG "vx_send_msg: TX_READY error\n");
			return err;
		}
		/* End of transfer */
		err = vx_transfer_end(chip, IRQ_MESS_READ_END);
		if (err < 0)
			return err;
	}

	return vx_read_status(chip, rmh);
}


/*
T
Takashi Iwai 已提交
340
 * vx_send_msg - send a DSP message with mutex
L
Linus Torvalds 已提交
341 342 343 344 345
 * @rmh: the rmh record to send and receive
 *
 * returns 0 if successful, or a negative error code.
 * see vx_send_msg_nolock().
 */
346
int vx_send_msg(struct vx_core *chip, struct vx_rmh *rmh)
L
Linus Torvalds 已提交
347 348 349
{
	int err;

T
Takashi Iwai 已提交
350
	mutex_lock(&chip->lock);
L
Linus Torvalds 已提交
351
	err = vx_send_msg_nolock(chip, rmh);
T
Takashi Iwai 已提交
352
	mutex_unlock(&chip->lock);
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360 361 362 363
	return err;
}


/*
 * vx_send_rih_nolock - send an RIH to xilinx
 * @cmd: the command to send
 *
 * returns 0 if successful, or a negative error code.
 * the error code can be VX-specific, retrieved via vx_get_error().
 *
T
Takashi Iwai 已提交
364
 * this function doesn't call mutex at all.
L
Linus Torvalds 已提交
365 366 367
 *
 * unlike RMH, no command is sent to DSP.
 */
368
int vx_send_rih_nolock(struct vx_core *chip, int cmd)
L
Linus Torvalds 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
{
	int err;

	if (chip->chip_status & VX_STAT_IS_STALE)
		return -EBUSY;

#if 0
	printk(KERN_DEBUG "send_rih: cmd = 0x%x\n", cmd);
#endif
	if ((err = vx_reset_chk(chip)) < 0)
		return err;
	/* send the IRQ */
	if ((err = vx_send_irq_dsp(chip, cmd)) < 0)
		return err;
	/* Wait CHK = 1 */
	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
		return err;
	/* If error, read RX */
	if (vx_inb(chip, ISR) & ISR_ERR) {
		if ((err = vx_wait_for_rx_full(chip)) < 0)
			return err;
		err = vx_inb(chip, RXH) << 16;
		err |= vx_inb(chip, RXM) << 8;
		err |= vx_inb(chip, RXL);
		return -(VX_ERR_MASK | err);
	}
	return 0;
}


/*
T
Takashi Iwai 已提交
400
 * vx_send_rih - send an RIH with mutex
L
Linus Torvalds 已提交
401 402 403 404
 * @cmd: the command to send
 *
 * see vx_send_rih_nolock().
 */
405
int vx_send_rih(struct vx_core *chip, int cmd)
L
Linus Torvalds 已提交
406 407 408
{
	int err;

T
Takashi Iwai 已提交
409
	mutex_lock(&chip->lock);
L
Linus Torvalds 已提交
410
	err = vx_send_rih_nolock(chip, cmd);
T
Takashi Iwai 已提交
411
	mutex_unlock(&chip->lock);
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420
	return err;
}

#define END_OF_RESET_WAIT_TIME		500	/* us */

/**
 * snd_vx_boot_xilinx - boot up the xilinx interface
 * @boot: the boot record to load
 */
421
int snd_vx_load_boot_image(struct vx_core *chip, const struct firmware *boot)
L
Linus Torvalds 已提交
422 423 424 425 426
{
	unsigned int i;
	int no_fillup = vx_has_new_dsp(chip);

	/* check the length of boot image */
427 428 429 430
	if (boot->size <= 0)
		return -EINVAL;
	if (boot->size % 3)
		return -EINVAL;
L
Linus Torvalds 已提交
431 432 433 434
#if 0
	{
		/* more strict check */
		unsigned int c = ((u32)boot->data[0] << 16) | ((u32)boot->data[1] << 8) | boot->data[2];
435 436
		if (boot->size != (c + 2) * 3)
			return -EINVAL;
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	}
#endif

	/* reset dsp */
	vx_reset_dsp(chip);
	
	udelay(END_OF_RESET_WAIT_TIME); /* another wait? */

	/* download boot strap */
	for (i = 0; i < 0x600; i += 3) {
		if (i >= boot->size) {
			if (no_fillup)
				break;
			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
				return -EIO;
			}
			vx_outb(chip, TXH, 0);
			vx_outb(chip, TXM, 0);
			vx_outb(chip, TXL, 0);
		} else {
458
			const unsigned char *image = boot->data + i;
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466 467 468 469 470
			if (vx_wait_isr_bit(chip, ISR_TX_EMPTY) < 0) {
				snd_printk(KERN_ERR "dsp boot failed at %d\n", i);
				return -EIO;
			}
			vx_outb(chip, TXH, image[0]);
			vx_outb(chip, TXM, image[1]);
			vx_outb(chip, TXL, image[2]);
		}
	}
	return 0;
}

471 472
EXPORT_SYMBOL(snd_vx_load_boot_image);

L
Linus Torvalds 已提交
473 474 475 476 477
/*
 * vx_test_irq_src - query the source of interrupts
 *
 * called from irq handler only
 */
478
static int vx_test_irq_src(struct vx_core *chip, unsigned int *ret)
L
Linus Torvalds 已提交
479 480 481 482
{
	int err;

	vx_init_rmh(&chip->irq_rmh, CMD_TEST_IT);
T
Takashi Iwai 已提交
483
	mutex_lock(&chip->lock);
L
Linus Torvalds 已提交
484 485 486 487 488
	err = vx_send_msg_nolock(chip, &chip->irq_rmh);
	if (err < 0)
		*ret = 0;
	else
		*ret = chip->irq_rmh.Stat[0];
T
Takashi Iwai 已提交
489
	mutex_unlock(&chip->lock);
L
Linus Torvalds 已提交
490 491 492 493 494
	return err;
}


/*
T
Takashi Iwai 已提交
495
 * snd_vx_threaded_irq_handler - threaded irq handler
L
Linus Torvalds 已提交
496
 */
T
Takashi Iwai 已提交
497
irqreturn_t snd_vx_threaded_irq_handler(int irq, void *dev)
L
Linus Torvalds 已提交
498
{
T
Takashi Iwai 已提交
499
	struct vx_core *chip = dev;
L
Linus Torvalds 已提交
500 501 502
	unsigned int events;
		
	if (chip->chip_status & VX_STAT_IS_STALE)
T
Takashi Iwai 已提交
503
		return IRQ_HANDLED;
L
Linus Torvalds 已提交
504 505

	if (vx_test_irq_src(chip, &events) < 0)
T
Takashi Iwai 已提交
506
		return IRQ_HANDLED;
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519
    
#if 0
	if (events & 0x000800)
		printk(KERN_ERR "DSP Stream underrun ! IRQ events = 0x%x\n", events);
#endif
	// printk(KERN_DEBUG "IRQ events = 0x%x\n", events);

	/* We must prevent any application using this DSP
	 * and block any further request until the application
	 * either unregisters or reloads the DSP
	 */
	if (events & FATAL_DSP_ERROR) {
		snd_printk(KERN_ERR "vx_core: fatal DSP error!!\n");
T
Takashi Iwai 已提交
520
		return IRQ_HANDLED;
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534
	}

	/* The start on time code conditions are filled (ie the time code
	 * received by the board is equal to one of those given to it).
	 */
	if (events & TIME_CODE_EVENT_PENDING)
		; /* so far, nothing to do yet */

	/* The frequency has changed on the board (UER mode). */
	if (events & FREQUENCY_CHANGE_EVENT_PENDING)
		vx_change_frequency(chip);

	/* update the pcm streams */
	vx_pcm_update_intr(chip, events);
T
Takashi Iwai 已提交
535
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
536
}
T
Takashi Iwai 已提交
537
EXPORT_SYMBOL(snd_vx_threaded_irq_handler);
L
Linus Torvalds 已提交
538 539 540 541

/**
 * snd_vx_irq_handler - interrupt handler
 */
542
irqreturn_t snd_vx_irq_handler(int irq, void *dev)
L
Linus Torvalds 已提交
543
{
544
	struct vx_core *chip = dev;
L
Linus Torvalds 已提交
545 546 547 548 549

	if (! (chip->chip_status & VX_STAT_CHIP_INIT) ||
	    (chip->chip_status & VX_STAT_IS_STALE))
		return IRQ_NONE;
	if (! vx_test_and_ack(chip))
T
Takashi Iwai 已提交
550 551
		return IRQ_WAKE_THREAD;
	return IRQ_NONE;
L
Linus Torvalds 已提交
552 553
}

554
EXPORT_SYMBOL(snd_vx_irq_handler);
L
Linus Torvalds 已提交
555 556 557

/*
 */
558
static void vx_reset_board(struct vx_core *chip, int cold_reset)
L
Linus Torvalds 已提交
559
{
560 561
	if (snd_BUG_ON(!chip->ops->reset_board))
		return;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

	/* current source, later sync'ed with target */
	chip->audio_source = VX_AUDIO_SRC_LINE;
	if (cold_reset) {
		chip->audio_source_target = chip->audio_source;
		chip->clock_source = INTERNAL_QUARTZ;
		chip->clock_mode = VX_CLOCK_MODE_AUTO;
		chip->freq = 48000;
		chip->uer_detected = VX_UER_MODE_NOT_PRESENT;
		chip->uer_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
	}

	chip->ops->reset_board(chip, cold_reset);

	vx_reset_codec(chip, cold_reset);

	vx_set_internal_clock(chip, chip->freq);

	/* Reset the DSP */
	vx_reset_dsp(chip);

	if (vx_is_pcmcia(chip)) {
		/* Acknowledge any pending IRQ and reset the MEMIRQ flag. */
		vx_test_and_ack(chip);
		vx_validate_irq(chip, 1);
	}

	/* init CBits */
	vx_set_iec958_status(chip, chip->uer_bits);
}


/*
 * proc interface
 */

598
static void vx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
599
{
600
	struct vx_core *chip = entry->private_data;
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	static char *audio_src_vxp[] = { "Line", "Mic", "Digital" };
	static char *audio_src_vx2[] = { "Analog", "Analog", "Digital" };
	static char *clock_mode[] = { "Auto", "Internal", "External" };
	static char *clock_src[] = { "Internal", "External" };
	static char *uer_type[] = { "Consumer", "Professional", "Not Present" };
	
	snd_iprintf(buffer, "%s\n", chip->card->longname);
	snd_iprintf(buffer, "Xilinx Firmware: %s\n",
		    chip->chip_status & VX_STAT_XILINX_LOADED ? "Loaded" : "No");
	snd_iprintf(buffer, "Device Initialized: %s\n",
		    chip->chip_status & VX_STAT_DEVICE_INIT ? "Yes" : "No");
	snd_iprintf(buffer, "DSP audio info:");
	if (chip->audio_info & VX_AUDIO_INFO_REAL_TIME)
		snd_iprintf(buffer, " realtime");
	if (chip->audio_info & VX_AUDIO_INFO_OFFLINE)
		snd_iprintf(buffer, " offline");
	if (chip->audio_info & VX_AUDIO_INFO_MPEG1)
		snd_iprintf(buffer, " mpeg1");
	if (chip->audio_info & VX_AUDIO_INFO_MPEG2)
		snd_iprintf(buffer, " mpeg2");
	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_8)
		snd_iprintf(buffer, " linear8");
	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_16)
		snd_iprintf(buffer, " linear16");
	if (chip->audio_info & VX_AUDIO_INFO_LINEAR_24)
		snd_iprintf(buffer, " linear24");
	snd_iprintf(buffer, "\n");
	snd_iprintf(buffer, "Input Source: %s\n", vx_is_pcmcia(chip) ?
		    audio_src_vxp[chip->audio_source] :
		    audio_src_vx2[chip->audio_source]);
	snd_iprintf(buffer, "Clock Mode: %s\n", clock_mode[chip->clock_mode]);
	snd_iprintf(buffer, "Clock Source: %s\n", clock_src[chip->clock_source]);
	snd_iprintf(buffer, "Frequency: %d\n", chip->freq);
	snd_iprintf(buffer, "Detected Frequency: %d\n", chip->freq_detected);
	snd_iprintf(buffer, "Detected UER type: %s\n", uer_type[chip->uer_detected]);
	snd_iprintf(buffer, "Min/Max/Cur IBL: %d/%d/%d (granularity=%d)\n",
		    chip->ibl.min_size, chip->ibl.max_size, chip->ibl.size,
		    chip->ibl.granularity);
}

641
static void vx_proc_init(struct vx_core *chip)
L
Linus Torvalds 已提交
642
{
643
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
644 645

	if (! snd_card_proc_new(chip->card, "vx-status", &entry))
646
		snd_info_set_text_ops(entry, chip, vx_proc_read);
L
Linus Torvalds 已提交
647 648 649 650 651 652
}


/**
 * snd_vx_dsp_boot - load the DSP boot
 */
653
int snd_vx_dsp_boot(struct vx_core *chip, const struct firmware *boot)
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662
{
	int err;
	int cold_reset = !(chip->chip_status & VX_STAT_DEVICE_INIT);

	vx_reset_board(chip, cold_reset);
	vx_validate_irq(chip, 0);

	if ((err = snd_vx_load_boot_image(chip, boot)) < 0)
		return err;
663
	msleep(10);
L
Linus Torvalds 已提交
664 665 666 667

	return 0;
}

668 669
EXPORT_SYMBOL(snd_vx_dsp_boot);

L
Linus Torvalds 已提交
670 671 672
/**
 * snd_vx_dsp_load - load the DSP image
 */
673
int snd_vx_dsp_load(struct vx_core *chip, const struct firmware *dsp)
L
Linus Torvalds 已提交
674 675 676 677
{
	unsigned int i;
	int err;
	unsigned int csum = 0;
678
	const unsigned char *image, *cptr;
L
Linus Torvalds 已提交
679

680 681
	if (dsp->size % 3)
		return -EINVAL;
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689

	vx_toggle_dac_mute(chip, 1);

	/* Transfert data buffer from PC to DSP */
	for (i = 0; i < dsp->size; i += 3) {
		image = dsp->data + i;
		/* Wait DSP ready for a new read */
		if ((err = vx_wait_isr_bit(chip, ISR_TX_EMPTY)) < 0) {
690 691
			printk(KERN_ERR
			       "dsp loading error at position %d\n", i);
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
			return err;
		}
		cptr = image;
		csum ^= *cptr;
		csum = (csum >> 24) | (csum << 8);
		vx_outb(chip, TXH, *cptr++);
		csum ^= *cptr;
		csum = (csum >> 24) | (csum << 8);
		vx_outb(chip, TXM, *cptr++);
		csum ^= *cptr;
		csum = (csum >> 24) | (csum << 8);
		vx_outb(chip, TXL, *cptr++);
	}
	snd_printdd(KERN_DEBUG "checksum = 0x%08x\n", csum);

707
	msleep(200);
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715 716 717 718 719

	if ((err = vx_wait_isr_bit(chip, ISR_CHK)) < 0)
		return err;

	vx_toggle_dac_mute(chip, 0);

	vx_test_and_ack(chip);
	vx_validate_irq(chip, 1);

	return 0;
}

720 721
EXPORT_SYMBOL(snd_vx_dsp_load);

L
Linus Torvalds 已提交
722 723 724 725
#ifdef CONFIG_PM
/*
 * suspend
 */
726
int snd_vx_suspend(struct vx_core *chip)
L
Linus Torvalds 已提交
727 728 729
{
	unsigned int i;

T
Takashi Iwai 已提交
730
	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
L
Linus Torvalds 已提交
731 732 733 734 735 736 737
	chip->chip_status |= VX_STAT_IN_SUSPEND;
	for (i = 0; i < chip->hw->num_codecs; i++)
		snd_pcm_suspend_all(chip->pcm[i]);

	return 0;
}

738 739
EXPORT_SYMBOL(snd_vx_suspend);

L
Linus Torvalds 已提交
740 741 742
/*
 * resume
 */
T
Takashi Iwai 已提交
743
int snd_vx_resume(struct vx_core *chip)
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
{
	int i, err;

	chip->chip_status &= ~VX_STAT_CHIP_INIT;

	for (i = 0; i < 4; i++) {
		if (! chip->firmware[i])
			continue;
		err = chip->ops->load_dsp(chip, i, chip->firmware[i]);
		if (err < 0) {
			snd_printk(KERN_ERR "vx: firmware resume error at DSP %d\n", i);
			return -EIO;
		}
	}

	chip->chip_status |= VX_STAT_CHIP_INIT;
	chip->chip_status &= ~VX_STAT_IN_SUSPEND;

T
Takashi Iwai 已提交
762
	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
L
Linus Torvalds 已提交
763 764 765
	return 0;
}

766
EXPORT_SYMBOL(snd_vx_resume);
L
Linus Torvalds 已提交
767 768 769
#endif

/**
770
 * snd_vx_create - constructor for struct vx_core
L
Linus Torvalds 已提交
771 772 773 774 775 776 777
 * @hw: hardware specific record
 *
 * this function allocates the instance and prepare for the hardware
 * initialization.
 *
 * return the instance pointer if successful, NULL in error.
 */
778 779 780
struct vx_core *snd_vx_create(struct snd_card *card, struct snd_vx_hardware *hw,
			      struct snd_vx_ops *ops,
			      int extra_size)
L
Linus Torvalds 已提交
781
{
782
	struct vx_core *chip;
L
Linus Torvalds 已提交
783

784 785
	if (snd_BUG_ON(!card || !hw || !ops))
		return NULL;
L
Linus Torvalds 已提交
786

787
	chip = kzalloc(sizeof(*chip) + extra_size, GFP_KERNEL);
L
Linus Torvalds 已提交
788 789 790 791
	if (! chip) {
		snd_printk(KERN_ERR "vx_core: no memory\n");
		return NULL;
	}
T
Takashi Iwai 已提交
792
	mutex_init(&chip->lock);
L
Linus Torvalds 已提交
793 794 795 796
	chip->irq = -1;
	chip->hw = hw;
	chip->type = hw->type;
	chip->ops = ops;
797
	mutex_init(&chip->mixer_mutex);
L
Linus Torvalds 已提交
798 799 800 801 802 803 804 805 806 807 808

	chip->card = card;
	card->private_data = chip;
	strcpy(card->driver, hw->name);
	sprintf(card->shortname, "Digigram %s", hw->name);

	vx_proc_init(chip);

	return chip;
}

809 810
EXPORT_SYMBOL(snd_vx_create);

L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824
/*
 * module entries
 */
static int __init alsa_vx_core_init(void)
{
	return 0;
}

static void __exit alsa_vx_core_exit(void)
{
}

module_init(alsa_vx_core_init)
module_exit(alsa_vx_core_exit)