/*
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
 *                   Thomas Sailer <sailer@ife.ee.ethz.ch>
 *  Routines for control of Ensoniq AudioPCI ES1370/ES1371 chips
 *
 *  BUGS:
 *    --
 *
 *  TODO:
 *    --
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define SND_MAIN_OBJECT_FILE
#include "../../include/driver.h"
#include "../../include/control.h"
#include "../../include/info.h"
#include "../../include/audiopci.h"

#ifndef CHIP1371
#undef CHIP1370
#define CHIP1370
#endif

#ifdef CHIP1370
#define snd_ensoniq_free snd_ensoniq_1370_free
#define snd_ensoniq_create snd_ensoniq_1370_create
#define snd_ensoniq_pcm snd_ensoniq_1370_pcm
#define snd_ensoniq_pcm2 snd_ensoniq_1370_pcm2
#define snd_ensoniq_midi snd_ensoniq_1370_midi
#define snd_ensoniq_interrupt snd_ensoniq_1370_interrupt
#endif
#ifdef CHIP1371
#define snd_ensoniq_free snd_ensoniq_1371_free
#define snd_ensoniq_create snd_ensoniq_1371_create
#define snd_ensoniq_pcm snd_ensoniq_1371_pcm
#define snd_ensoniq_pcm2 snd_ensoniq_1371_pcm2
#define snd_ensoniq_midi snd_ensoniq_1371_midi
#define snd_ensoniq_interrupt snd_ensoniq_1371_interrupt
#endif

/*
 *  constants
 */

#ifdef CHIP1370
static unsigned char bugbuf[16] __attribute__ ((aligned (16)));
static const unsigned int snd_es1370_fixed_rates[] =
	{5512, 11025, 22050, 44100};
#endif
static const unsigned int snd_ensoniq_sample_shift[] =
	{0, 1, 1, 2};

/*
 *  common I/O routines
 */

#ifdef CHIP1371

static unsigned int snd_es1371_wait_src_ready(ensoniq_t * ensoniq)
{
	unsigned int t, r;

	for (t = 0; t < 500; t++) {
		if (!((r = inl(ES_REG(ensoniq, 1371_SMPRATE))) & ES_1371_SRC_RAM_BUSY))
			return r;
		snd_delay(1);
	}
	snd_printk("es1371: wait source ready timeout 0x%x [0x%x]\n", ES_REG(ensoniq, 1371_SMPRATE), r);
	return 0;
}

static unsigned int snd_es1371_src_read(ensoniq_t * ensoniq, unsigned short reg)
{
	unsigned int r;

	r = snd_es1371_wait_src_ready(ensoniq) &
	    (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
	     ES_1371_DIS_P2 | ES_1371_DIS_R1);
	r |= ES_1371_SRC_RAM_ADDRO(reg);
	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
	return ES_1371_SRC_RAM_DATAI(snd_es1371_wait_src_ready(ensoniq));
}

static void snd_es1371_src_write(ensoniq_t * ensoniq,
				 unsigned short reg, unsigned short data)
{
	unsigned int r;

	r = snd_es1371_wait_src_ready(ensoniq) &
	    (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
	     ES_1371_DIS_P2 | ES_1371_DIS_R1);
	r |= ES_1371_SRC_RAM_ADDRO(reg) | ES_1371_SRC_RAM_DATAO(data);
	outl(r | ES_1371_SRC_RAM_WE, ES_REG(ensoniq, 1371_SMPRATE));
}

#endif /* CHIP1371 */

#ifdef CHIP1370

static void snd_es1370_codec_write(void *private_data,
				   unsigned short reg, unsigned short val)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	unsigned long flags;
	long time = HZ / 10;

#if 0
	printk("CODEC WRITE: reg = 0x%x, val = 0x%x (0x%x), creg = 0x%x\n", reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
#endif
	do {
		spin_lock_irqsave(&ensoniq->reg_lock, flags);
		if (!(inl(ES_REG(ensoniq, STATUS)) & ES_1370_CSTAT)) {
			outw(ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			return;
		}
		spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
		current->state = TASK_INTERRUPTIBLE;
		time -= schedule_timeout(1) ? 1 : 0;
	} while (time > 0);
	snd_printk("es1370: codec write timeout, status = 0x%x\n", inl(ES_REG(ensoniq, STATUS)));
}

#endif /* CHIP1370 */

#ifdef CHIP1371

static void snd_es1371_codec_write(void *private_data,
				   unsigned short reg, unsigned short val)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	unsigned long flags;
	unsigned int t, x;

	for (t = 0; t < 0x1000; t++) {
		spin_lock_irqsave(&ensoniq->reg_lock, flags);
		if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
			/* save the current state for latter */
			x = inl(ES_REG(ensoniq, 1371_SMPRATE));
			outl(snd_es1371_wait_src_ready(ensoniq) &
			     (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
			      ES_1371_DIS_P2 | ES_1371_DIS_R1),
			     ES_REG(ensoniq, 1371_SMPRATE));
			/* wait for a SAFE time to write addr/data and then do it, dammit */
			for (t = 0; t < 0x1000; t++)
				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00070000) == 0x00010000)
					break;
			outl(ES_1371_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1371_CODEC));
			/* restore SRC reg */
			snd_es1371_wait_src_ready(ensoniq);
			outl(x, ES_REG(ensoniq, 1371_SMPRATE));
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			return;
		}
		spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	}
	snd_printk("es1371: codec write timeout at 0x%x [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
}

static unsigned short snd_es1371_codec_read(void *private_data,
					    unsigned short reg)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	unsigned long flags;
	unsigned int t, x, fail = 0;

      __again:
	for (t = 0; t < 0x1000; t++) {
		spin_lock_irqsave(&ensoniq->reg_lock, flags);
		if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
			/* save the current state for latter */
			x = inl(ES_REG(ensoniq, 1371_SMPRATE));
			outl(snd_es1371_wait_src_ready(ensoniq) &
			     (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
			      ES_1371_DIS_P2 | ES_1371_DIS_R1),
			     ES_REG(ensoniq, 1371_SMPRATE));
			/* wait for a SAFE time to write addr/data and then do it, dammit */
			for (t = 0; t < 0x1000; t++)
				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00070000) == 0x00010000)
					break;
			outl(ES_1371_CODEC_READS(reg), ES_REG(ensoniq, 1371_CODEC));
			/* restore SRC reg */
			snd_es1371_wait_src_ready(ensoniq);
			outl(x, ES_REG(ensoniq, 1371_SMPRATE));
			for (t = 0; t < 0x1000; t++) {
				if ((x = inl(ES_REG(ensoniq, 1371_CODEC))) & ES_1371_CODEC_RDY) {
					spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
					return ES_1371_CODEC_READ(x);
				}
			}
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			if (++fail > 10) {
				snd_printk("es1371: codec read timeout (final) at 0x%x, reg = 0x%x [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), reg, inl(ES_REG(ensoniq, 1371_CODEC)));
				return 0;
			}
			goto __again;
		}
		spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	}
	snd_printk("es1371: codec read timeout at 0x%x [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
	return 0;
}

#endif /* CHIP1371 */

#ifdef CHIP1370

static unsigned int snd_es1370_fixed_rate(unsigned int rate)
{
	int idx;

	for (idx = 0; idx < 3; idx++) {
		if (((snd_es1370_fixed_rates[idx] + snd_es1370_fixed_rates[idx + 1]) >> 1) > rate)
			return idx;
	}
	return 3;
}

#endif /* CHIP1370 */

#ifdef CHIP1371

static unsigned int snd_es1371_adc_rate(ensoniq_t * ensoniq,
				        unsigned int rate, int set)
{
	unsigned int n, truncm, freq, result;

	if (rate > 48000)
		rate = 48000;
	if (rate < 4000)
		rate = 4000;
	n = rate / 3000;
	if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
			n--;
	truncm = (21 * n - 1) | 1;
	freq = ((48000UL << 15) / rate) * n;
	result = (48000UL << 15) / (freq / n);
	if (set) {
		if (rate >= 24000) {
			if (truncm > 239)
				truncm = 239;
			snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
				(((239 - truncm) >> 1) << 9) | (n << 4));
		} else {
			if (truncm > 119)
				truncm = 119;
			snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
				0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
		}
		snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
				     (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) & 0x00ff) |
				     ((freq >> 5) & 0xfc00));
		snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
		snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
		snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
	}
	return result;
}

static unsigned int snd_es1371_dac1_rate(ensoniq_t * ensoniq,
					 unsigned int rate, int set)
{
	unsigned int freq, r, result;

	if (rate > 48000)
		rate = 48000;
	if (rate < 4000)
		rate = 4000;
	freq = (rate << 15) / 3000;
	result = (freq * 3000) >> 15;
	if (set) {
		r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P2 | ES_1371_DIS_R1)) | ES_1371_DIS_P1;
		outl(r, ES_REG(ensoniq, 1371_SMPRATE));
		snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
				     (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS) & 0x00ff) |
				     ((freq >> 5) & 0xfc00));
		snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
		r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P2 | ES_1371_DIS_R1));
		outl(r, ES_REG(ensoniq, 1371_SMPRATE));
	}
	return result;
}

static unsigned int snd_es1371_dac2_rate(ensoniq_t * ensoniq,
					 unsigned int rate, int set)
{
	unsigned int freq, r, result;

	if (rate > 48000)
		rate = 48000;
	if (rate < 4000)
		rate = 4000;
	freq = (rate << 15) / 3000;
	result = (freq * 3000) >> 15;
	if (set) {
		r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 | ES_1371_DIS_R1)) | ES_1371_DIS_P2;
		outl(r, ES_REG(ensoniq, 1371_SMPRATE));
		snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
				     (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS) & 0x00ff) |
				     ((freq >> 5) & 0xfc00));
		snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
		r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 | ES_1371_DIS_R1));
		outl(r, ES_REG(ensoniq, 1371_SMPRATE));
	}
	return result;
}

#endif /* CHIP1371 */

static void snd_ensoniq_trigger(ensoniq_t *ensoniq, int up, unsigned int what)
{
	unsigned long flags;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (up) {
		ensoniq->ctrl |= what;
	} else {
		ensoniq->ctrl &= ~what;
	}
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

/*
 *  PCM part
 */

static int snd_ensoniq_playback1_ioctl(void *private_data,
				       snd_pcm_subchn_t * subchn,
				       unsigned int cmd,
				       unsigned long *arg)
{
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;

	switch (cmd) {
	case SND_PCM1_IOCTL_RATE:
#ifdef CHIP1370 
		subchn1->real_rate = snd_es1370_fixed_rates[snd_es1370_fixed_rate(subchn1->rate)];
#else
		{
			ensoniq_t *ensoniq = (ensoniq_t *) private_data;

			subchn1->real_rate = snd_es1371_dac1_rate(ensoniq, subchn1->rate, 0);
		}
#endif
		return 0;
	}
	return -ENXIO;
}

static int snd_ensoniq_playback2_ioctl(void *private_data,
				       snd_pcm_subchn_t * subchn,
				       unsigned int cmd,
				       unsigned long *arg)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;

	switch (cmd) {
	case SND_PCM1_IOCTL_RATE:
#ifdef CHIP1370
		{
			unsigned long flags;
			unsigned int pclkdiv;
		
			spin_lock_irqsave(&ensoniq->reg_lock, flags);
			if (ensoniq->u.es1370.pclkdiv_lock)
				pclkdiv = ES_1370_PCLKDIVI(ensoniq->ctrl);
			else
				pclkdiv = ES_1370_SRTODIV(subchn1->rate);
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			subchn1->real_rate = ES_1370_DIVTOSR(pclkdiv);
		}
#else
		subchn1->real_rate = snd_es1371_dac2_rate(ensoniq, subchn1->rate, 0);
#endif
		return 0;
	}
	return -ENXIO;
}

static int snd_ensoniq_capture_ioctl(void *private_data,
				    snd_pcm_subchn_t * subchn,
				    unsigned int cmd,
				    unsigned long *arg)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;

	switch (cmd) {
	case SND_PCM1_IOCTL_RATE:
#ifdef CHIP1370
		{
			unsigned long flags;
			unsigned int pclkdiv;

			spin_lock_irqsave(&ensoniq->reg_lock, flags);
			if (ensoniq->u.es1370.pclkdiv_lock)
				pclkdiv = ES_1370_PCLKDIVI(ensoniq->ctrl);
			else
				pclkdiv = ES_1370_SRTODIV(subchn1->rate);
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			subchn1->real_rate = ES_1370_DIVTOSR(pclkdiv);
		}
#else
		subchn1->real_rate = snd_es1371_adc_rate(ensoniq, subchn1->rate, 0);
#endif
		return 0;
	}
	return -ENXIO;
}

static void snd_ensoniq_playback1_trigger(void *private_data,
					  snd_pcm_subchn_t * pcm,
					  int up)
{
	snd_ensoniq_trigger((ensoniq_t *)private_data, up, ES_DAC1_EN);
}

static void snd_ensoniq_playback2_trigger(void *private_data,
					  snd_pcm_subchn_t * subchn,
					  int up)
{
	snd_ensoniq_trigger((ensoniq_t *)private_data, up, ES_DAC2_EN);
}

static void snd_ensoniq_capture_trigger(void *private_data,
				       snd_pcm_subchn_t * subchn,
				       int up)
{
	snd_ensoniq_trigger((ensoniq_t *)private_data, up, ES_ADC_EN);
}

static void snd_ensoniq_playback1_prepare(void *private_data,
					  snd_pcm_subchn_t * subchn,
					  unsigned char *buffer,
					  unsigned int size,
					  unsigned int offset,
					  unsigned int count)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	unsigned int mode = 0;

	if (subchn1->mode & SND_PCM1_MODE_16)
		mode |= 0x02;
	if (subchn1->voices > 1)
		mode |= 0x01;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	ensoniq->ctrl &= ~ES_DAC1_EN;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
	outl(virt_to_bus(buffer), ES_REG(ensoniq, DAC1_FRAME));
	outl((size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
	ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
	ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	outl((count >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, DAC1_COUNT));
#ifdef CHIP1370
	ensoniq->ctrl &= ~ES_1370_WTSRSELM;
	ensoniq->ctrl |= ES_1370_WTSRSEL(snd_es1370_fixed_rate(subchn1->rate));
#else
	snd_es1371_dac1_rate(ensoniq, subchn1->real_rate, 1);
#endif
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static void snd_ensoniq_playback2_prepare(void *private_data,
					  snd_pcm_subchn_t * subchn,
					  unsigned char *buffer,
					  unsigned int size,
					  unsigned int offset,
					  unsigned int count)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	unsigned int mode = 0;

	if (subchn1->mode & SND_PCM1_MODE_16)
		mode |= 0x02;
	if (subchn1->voices > 1)
		mode |= 0x01;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	ensoniq->ctrl &= ~ES_DAC2_EN;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
	outl(virt_to_bus(buffer), ES_REG(ensoniq, DAC2_FRAME));
	outl((size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
	ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
			    ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
	ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
	    ES_P2_END_INCO(mode ? 2 : 1) |
	    ES_P2_ST_INCO(0);
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	outl((count >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, DAC2_COUNT));
#ifdef CHIP1370
	if (ensoniq->u.es1370.pclkdiv_lock == 0 || ensoniq->u.es1370.pclkdiv_lock == ES_MODE_PLAY2) {
		ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
		ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(subchn1->rate));
		ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
	}
#else
	snd_es1371_dac2_rate(ensoniq, subchn1->real_rate, 1);
#endif
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static void snd_ensoniq_capture_prepare(void *private_data,
					snd_pcm_subchn_t * subchn,
				        unsigned char *buffer,
				        unsigned int size,
				        unsigned int offset,
				        unsigned int count)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	snd_pcm1_subchn_t *subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	unsigned int mode = 0;

	if (subchn1->mode & SND_PCM1_MODE_16)
		mode |= 0x02;
	if (subchn1->voices > 1)
		mode |= 0x01;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	ensoniq->ctrl &= ~ES_ADC_EN;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
	outl(virt_to_bus(buffer), ES_REG(ensoniq, ADC_FRAME));
	outl((size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
	ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
	ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	outl((count >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, ADC_COUNT));
#ifdef CHIP1370
	if (ensoniq->u.es1370.pclkdiv_lock == 0 || ensoniq->u.es1370.pclkdiv_lock == ES_MODE_CAPTURE) {
		ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
		ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(subchn1->rate));
		ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
	}
#else
	snd_es1371_adc_rate(ensoniq, subchn1->real_rate, 1);
#endif
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static int snd_ensoniq_playback1_open(void *private_data,
				      snd_pcm_subchn_t * subchn)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	int err;

	if ((err = snd_pcm1_dma_alloc(subchn, ensoniq->dma1ptr,
				      "Ensoniq AudioPCI - DAC1")) < 0)
		return err;
	ensoniq->mode |= ES_MODE_PLAY1;
	ensoniq->playback_subchn = subchn;
	ensoniq->playback_subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	return 0;
}

static int snd_ensoniq_playback2_open(void *private_data,
				      snd_pcm_subchn_t * subchn)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	int err;

	if ((err = snd_pcm1_dma_alloc(subchn, ensoniq->dma2ptr,
				      "Ensoniq AudioPCI - DAC2")) < 0)
		return err;
	ensoniq->mode |= ES_MODE_PLAY2;
	ensoniq->playback2_subchn = subchn;
	ensoniq->playback2_subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	return 0;
}

static int snd_ensoniq_capture_open(void *private_data,
				    snd_pcm_subchn_t * subchn)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	int err;

	if ((err = snd_pcm1_dma_alloc(subchn, ensoniq->dma3ptr,
				      "Ensoniq AudioPCI - ADC")) < 0)
		return err;
	ensoniq->mode |= ES_MODE_CAPTURE;
	ensoniq->capture_subchn = subchn;
	ensoniq->capture_subchn1 = (snd_pcm1_subchn_t *)subchn->private_data;
	return 0;
}

static void snd_ensoniq_playback1_close(void *private_data,
					snd_pcm_subchn_t * subchn)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ensoniq->playback_subchn = NULL;
	ensoniq->playback_subchn1 = NULL;
	snd_pcm1_dma_free(subchn);
	ensoniq->mode &= ~ES_MODE_PLAY1;
}

static void snd_ensoniq_playback2_close(void *private_data,
					snd_pcm_subchn_t * subchn)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ensoniq->playback2_subchn = NULL;
	ensoniq->playback2_subchn1 = NULL;
	snd_pcm1_dma_free(subchn);
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
#ifdef CHIP1370
	ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
#endif
	ensoniq->mode &= ~ES_MODE_PLAY2;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static void snd_ensoniq_capture_close(void *private_data,
				      snd_pcm_subchn_t * subchn)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ensoniq->capture_subchn = NULL;
	ensoniq->capture_subchn1 = NULL;
	snd_pcm1_dma_free(subchn);
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
#ifdef CHIP1370
	ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
#endif
	ensoniq->mode &= ~ES_MODE_CAPTURE;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static unsigned int snd_ensoniq_playback1_pointer(void *private_data,
						  snd_pcm_subchn_t * subchn,
						  unsigned int used_size)
{
	unsigned int ptr;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ptr = ES_REG_CURR_COUNT(inl(ES_REG(ensoniq, DAC1_COUNT))) + 1;
	ptr <<= snd_ensoniq_sample_shift[ES_P1_MODEI(ensoniq->sctrl)];
	return used_size - ptr;
}

static unsigned int snd_ensoniq_playback2_pointer(void *private_data,
						  snd_pcm_subchn_t * subchn,
						  unsigned int used_size)
{
	unsigned int ptr;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ptr = ES_REG_CURR_COUNT(inl(ES_REG(ensoniq, DAC2_COUNT))) + 1;
	ptr <<= snd_ensoniq_sample_shift[ES_P2_MODEI(ensoniq->sctrl)];
	return used_size - ptr;
}

static unsigned int snd_ensoniq_capture_pointer(void *private_data,
						snd_pcm_subchn_t * subchn,
					        unsigned int used_size)
{
	unsigned int ptr;
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

	ptr = ES_REG_CURR_COUNT(inl(ES_REG(ensoniq, ADC_COUNT))) + 1;
	ptr <<= snd_ensoniq_sample_shift[ES_R1_MODEI(ensoniq->sctrl)];
	return used_size - ptr;
}

static struct snd_stru_pcm1_hardware snd_ensoniq_playback1 =
{
	SND_PCM1_HW_AUTODMA | SND_PCM1_HW_BLOCKPTR,	/* flags */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* formats */
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* hardware formats */
	3,			/* align value */
	6,			/* minimal fragment */
	4000,			/* min. rate */
	48000,			/* max. rate */
	2,			/* max. voices */
	snd_ensoniq_playback1_open,
	snd_ensoniq_playback1_close,
	snd_ensoniq_playback1_ioctl,
	snd_ensoniq_playback1_prepare,
	snd_ensoniq_playback1_trigger,
	snd_ensoniq_playback1_pointer,
	snd_pcm1_playback_dma_ulaw_loud,
	snd_pcm1_dma_move,
	snd_pcm1_playback_dma_neutral
};

static struct snd_stru_pcm1_hardware snd_ensoniq_playback2 =
{
	SND_PCM1_HW_AUTODMA | SND_PCM1_HW_BLOCKPTR,	/* flags */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* formats */
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* hardware formats */
	3,			/* align value */
	6,			/* minimal fragment */
	4000,			/* min. rate */
	48000,			/* max. rate */
	2,			/* max. voices */
	snd_ensoniq_playback2_open,
	snd_ensoniq_playback2_close,
	snd_ensoniq_playback2_ioctl,
	snd_ensoniq_playback2_prepare,
	snd_ensoniq_playback2_trigger,
	snd_ensoniq_playback2_pointer,
	snd_pcm1_playback_dma_ulaw_loud,
	snd_pcm1_dma_move,
	snd_pcm1_playback_dma_neutral
};

static struct snd_stru_pcm1_hardware snd_ensoniq_capture =
{
	SND_PCM1_HW_AUTODMA | SND_PCM1_HW_BLOCKPTR,	/* flags */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* formats */
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* hardware formats */
	3,			/* align value */
	6,			/* minimal fragment */
	4000,			/* min. rate */
	48000,			/* max. rate */
	2,			/* max. voices */
	snd_ensoniq_capture_open,
	snd_ensoniq_capture_close,
	snd_ensoniq_capture_ioctl,
	snd_ensoniq_capture_prepare,
	snd_ensoniq_capture_trigger,
	snd_ensoniq_capture_pointer,
	snd_pcm1_capture_dma_ulaw,
	snd_pcm1_dma_move,
	NULL
};

static void snd_ensoniq_pcm_free(void *private_data)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	ensoniq->pcm = NULL;
}

snd_pcm_t *snd_ensoniq_pcm(ensoniq_t * ensoniq)
{
	snd_pcm_t *pcm;
	snd_pcm1_channel_t *pchn1;

#ifdef CHIP1370
	pcm = snd_pcm1_new_device(ensoniq->card, "ES1370/1", 1, 1);
#else
	pcm = snd_pcm1_new_device(ensoniq->card, "ES1371/1", 1, 1);
#endif
	if (!pcm)
		return NULL;
	pchn1 = (snd_pcm1_channel_t *)pcm->playback.private_data;
	memcpy(&pchn1->hw, &snd_ensoniq_playback2, sizeof(snd_ensoniq_playback2));
	pchn1->private_data = ensoniq;
	pchn1 = (snd_pcm1_channel_t *)pcm->capture.private_data;
	memcpy(&pchn1->hw, &snd_ensoniq_capture, sizeof(snd_ensoniq_capture));
	pchn1->private_data = ensoniq;
	pcm->private_data = ensoniq;
	pcm->private_free = snd_ensoniq_pcm_free;
	pcm->info_flags = SND_PCM_INFO_CODEC | SND_PCM_INFO_MMAP |
			  SND_PCM_INFO_PLAYBACK | SND_PCM_INFO_CAPTURE |
			  SND_PCM_INFO_DUPLEX;
#ifdef CHIP1370
	strcpy(pcm->name, "ES1370 DAC2/ADC");
#else
	strcpy(pcm->name, "ES1371 DAC2/ADC");
#endif
	return ensoniq->pcm = pcm;
}

static void snd_ensoniq_pcm_free2(void *private_data)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;
	ensoniq->pcm2 = NULL;
}

snd_pcm_t *snd_ensoniq_pcm2(ensoniq_t * ensoniq)
{
	snd_pcm_t *pcm;
	snd_pcm1_channel_t *pchn1;

#ifdef CHIP1370
	pcm = snd_pcm1_new_device(ensoniq->card, "ES1370/2", 1, 0);
#else
	pcm = snd_pcm1_new_device(ensoniq->card, "ES1371/2", 1, 0);
#endif
	if (!pcm)
		return NULL;
	pchn1 = (snd_pcm1_channel_t *)pcm->playback.private_data;
	memcpy(&pchn1->hw, &snd_ensoniq_playback1, sizeof(snd_ensoniq_playback1));
	pchn1->private_data = ensoniq;
	pcm->private_data = ensoniq;
	pcm->private_free = snd_ensoniq_pcm_free2;
	pcm->info_flags = SND_PCM_INFO_CODEC | SND_PCM_INFO_MMAP |
			  SND_PCM_INFO_PLAYBACK;
#ifdef CHIP1370
	strcpy(pcm->name, "ES1370 DAC1");
#else
	strcpy(pcm->name, "ES1371 DAC1");
#endif
	return ensoniq->pcm2 = pcm;
}

/*
 *  Mixer section
 */

#ifdef CHIP1371

static void snd_ensoniq_mixer_free_ac97(ac97_t * ac97)
{
	((ensoniq_t *) ac97->private_data)->mixer = NULL;
	snd_kfree(ac97);
}

snd_kmixer_t *snd_ensoniq_1371_mixer(ensoniq_t * ensoniq, int pcm_count, int *pcm_devs)
{
	ac97_t *ac97;
	snd_kmixer_t *mixer;

	ac97 = snd_kcalloc(sizeof(ac97_t), GFP_KERNEL);
	if (!ac97)
		return NULL;
	ac97->write = snd_es1371_codec_write;
	ac97->read = snd_es1371_codec_read;
	ac97->private_data = ensoniq;
	ac97->private_free = snd_ensoniq_mixer_free_ac97;
	mixer = snd_ac97_mixer(ensoniq->card, ac97, pcm_count, pcm_devs);
	if (!mixer) {
		snd_kfree(ac97);
		return NULL;
	}
	return ensoniq->mixer = mixer;
}

#endif /* CHIP1371 */

#ifdef CHIP1370

static int snd_es1370_get_switch(snd_kmixer_t * mixer,
				 snd_kswitch_t * kswitch,
				 snd_switch_t * uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;
	unsigned int mask;

	mask = kswitch->private_value ? ES_1370_XCTL0 : ES_1370_XCTL1;
	uswitch->type = SND_SW_TYPE_BOOLEAN;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	uswitch->value.enable = ensoniq->ctrl & mask ? 1 : 0;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_es1370_set_switch(snd_kmixer_t * mixer,
				 snd_kswitch_t * kswitch,
				 snd_switch_t * uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;
	unsigned int mask;
	int change = 0;

	mask = kswitch->private_value ? ES_1370_XCTL0 : ES_1370_XCTL1;
	if (uswitch->type != SND_SW_TYPE_BOOLEAN)
		return -EINVAL;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	change = (ensoniq->ctrl & mask) != (uswitch->value.enable ? mask : 0);
	ensoniq->ctrl &= ~mask;
	if (uswitch->value.enable)
		ensoniq->ctrl |= mask;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return change;
}

static snd_kswitch_t snd_es1370_mixer_switch1 =
{
	"MIC +5V bias",
	(snd_get_switch_t *)snd_es1370_get_switch,
	(snd_set_switch_t *)snd_es1370_set_switch,
	0,
	NULL,
	NULL
};

static snd_kswitch_t snd_es1370_mixer_switch2 =
{
	"Line-In to Output",
	(snd_get_switch_t *)snd_es1370_get_switch,
	(snd_set_switch_t *)snd_es1370_set_switch,
	1,
	NULL,
	NULL
};

static void snd_ensoniq_mixer_free_ak4531(ak4531_t * ak4531)
{
	((ensoniq_t *) ak4531->private_data)->mixer = NULL;
	snd_kfree(ak4531);
}

snd_kmixer_t *snd_ensoniq_1370_mixer(ensoniq_t * ensoniq, int pcm_count, int *pcm_devs)
{
	ak4531_t *ak4531;
	snd_kmixer_t *mixer;

	/* try reset AK4531 */
	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
	snd_delay(10);
	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
	snd_delay(10);

	ak4531 = snd_kcalloc(sizeof(ak4531_t), GFP_KERNEL);
	if (!ak4531)
		return NULL;
	ak4531->write = snd_es1370_codec_write;
	ak4531->private_data = ensoniq;
	ak4531->private_free = snd_ensoniq_mixer_free_ak4531;
	mixer = snd_ak4531_mixer(ensoniq->card, ak4531, pcm_count, pcm_devs);
	if (!mixer) {
		snd_kfree(ak4531);
		return NULL;
	}
	snd_mixer_switch_new(mixer, &snd_es1370_mixer_switch1, ensoniq);
	snd_mixer_switch_new(mixer, &snd_es1370_mixer_switch2, ensoniq);
	return ensoniq->mixer = mixer;
}

#endif /* CHIP1370 */

/*
 *  General Switches...
 */

static int snd_ensoniq_get_ctl_switch(snd_card_t * card,
				      snd_kswitch_t * kswitch,
				      snd_switch_t *uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;

	uswitch->type = SND_SW_TYPE_BOOLEAN;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	uswitch->value.enable = ensoniq->ctrl & ES_JYSTK_EN ? 1 : 0;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_ensoniq_set_ctl_switch(snd_card_t * card,
				      snd_kswitch_t * kswitch,
				      snd_switch_t * uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;
	int change = 0;

	if (uswitch->type != SND_SW_TYPE_BOOLEAN)
		return -EINVAL;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	change = (ensoniq->ctrl & ES_JYSTK_EN) != (uswitch->value.enable ? ES_JYSTK_EN : 0);
	ensoniq->ctrl &= ~ES_JYSTK_EN;
	if (uswitch->value.enable)
		ensoniq->ctrl |= ES_JYSTK_EN;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return change;
}

static snd_kswitch_t snd_ensoniq_ctl_switch =
{
	SND_CTL_SW_JOYSTICK,
	(snd_get_switch_t *)snd_ensoniq_get_ctl_switch,
	(snd_set_switch_t *)snd_ensoniq_set_ctl_switch,
	0,
	NULL,
	NULL
};

#ifdef CHIP1371

static int snd_es1371_get_ctl_switch(snd_card_t * card,
				     snd_kswitch_t * kswitch,
				     snd_switch_t *uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;

	uswitch->type = SND_SW_TYPE_WORD;
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	uswitch->low = 0x200;
	uswitch->high = 0x218;
	uswitch->value.data16[0] = (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200;
	uswitch->value.data16[1] = 0x200;
	uswitch->value.data16[2] = 0x208;
	uswitch->value.data16[3] = 0x210;
	uswitch->value.data16[4] = 0x218;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_es1371_set_ctl_switch(snd_card_t * card,
				     snd_kswitch_t * kswitch,
				     snd_switch_t *uswitch)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) kswitch->private_data;
	unsigned short port, val;
	int change = 0;

	if (uswitch->type != SND_SW_TYPE_WORD)
		return -EINVAL;
	port = uswitch->value.data16[0];
	if (port < 0x200 || port > 0x218)
		return -EINVAL;
	val = 0;
	if (port & 0x0008)
		val |= ES_1371_JOY_ASEL(1);
	if (port & 0x0010)
		val |= ES_1371_JOY_ASEL(2);
	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	change = (ensoniq->ctrl & ES_1371_JOY_ASELM) != val;
	ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
	ensoniq->ctrl |= val;
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return change;
}

static snd_kswitch_t snd_es1371_ctl_switch =
{
	SND_CTL_SW_JOYSTICK_ADDRESS,
	(snd_get_switch_t *)snd_es1371_get_ctl_switch,
	(snd_set_switch_t *)snd_es1371_set_ctl_switch,
	0,
	NULL,
	NULL
};

#endif /* CHIP1371 */

/*

 */

static void snd_ensoniq_proc_read(snd_info_buffer_t * buffer,
				  void *private_data)
{
	ensoniq_t *ensoniq = (ensoniq_t *) private_data;

#ifdef CHIP1370
	snd_iprintf(buffer, "Ensoniq AudioPCI ES1370\n\n");
#else
	snd_iprintf(buffer, "Ensoniq AudioPCI ES1371\n\n");
#endif
	snd_iprintf(buffer, "Joystick enable  : %s\n", ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
#ifdef CHIP1370
	snd_iprintf(buffer, "MIC +5V bias     : %s\n", ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
	snd_iprintf(buffer, "Line In to AOUT  : %s\n", ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
#else
	snd_iprintf(buffer, "Joystick port    : 0x%x\n", (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
#endif
}

static void snd_ensoniq_proc_init(ensoniq_t * ensoniq)
{
	snd_info_entry_t *entry;

	if ((entry = snd_info_create_entry(ensoniq->card, "audiopci")) != NULL) {
		entry->private_data = ensoniq;
		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
		entry->t.text.read_size = 256;
		entry->t.text.read = snd_ensoniq_proc_read;
		if (snd_info_register(entry) < 0) {
			snd_info_free_entry(entry);
			entry = NULL;
		}
	}
	ensoniq->proc_entry = entry;
}

static void snd_ensoniq_proc_done(ensoniq_t * ensoniq)
{
	if (ensoniq->proc_entry) {
		snd_info_unregister(ensoniq->proc_entry);
		ensoniq->proc_entry = NULL;
	}
}

/*

 */

ensoniq_t *snd_ensoniq_create(snd_card_t * card,
			      struct pci_dev *pci,
			      snd_dma_t * dma1ptr,
			      snd_dma_t * dma2ptr,
			      snd_dma_t * dma3ptr,
			      snd_irq_t * irqptr)
{
#ifdef CHIP1371
	int idx;
#endif
	ensoniq_t *ensoniq;
	unsigned short cmdw;
	unsigned char cmdb;

	ensoniq = (ensoniq_t *) snd_kcalloc(sizeof(ensoniq_t), GFP_KERNEL);
	if (!ensoniq)
		return NULL;
	ensoniq->reg_lock = SPIN_LOCK_UNLOCKED;
	ensoniq->card = card;
	ensoniq->pci = pci;
	ensoniq->dma1ptr = dma1ptr;
	ensoniq->dma2ptr = dma2ptr;
	ensoniq->dma3ptr = dma3ptr;
	ensoniq->irqptr = irqptr;
#ifdef NEW_PCI
	ensoniq->port = pci->resource[0].start;
#else
	ensoniq->port = pci->base_address[0] & ~PCI_BASE_ADDRESS_SPACE;
#endif
	pci_read_config_word(pci, PCI_COMMAND, &cmdw);
	if ((cmdw & (PCI_COMMAND_IO|PCI_COMMAND_MASTER)) != (PCI_COMMAND_IO|PCI_COMMAND_MASTER)) {
		cmdw |= PCI_COMMAND_IO | PCI_COMMAND_MASTER;
		pci_write_config_word(pci, PCI_COMMAND, cmdw);
	}
	pci_read_config_byte(pci, PCI_LATENCY_TIMER, &cmdb);
	if (cmdb < 32)
		cmdb = 32;
	pci_write_config_byte(pci, PCI_LATENCY_TIMER, cmdb);
	snd_ensoniq_proc_init(ensoniq);
#ifdef CHIP1370
#if 0
	ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
#else	/* get microphone working */
	ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
#endif
	ensoniq->sctrl = 0;
	/* initialize the chips */
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
	outl(virt_to_bus(bugbuf), ES_REG(ensoniq, PHANTOM_FRAME));
	outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
#else
	ensoniq->ctrl = 0;
	ensoniq->sctrl = 0;
	/* initialize the chips */
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	outl(0, ES_REG(ensoniq, 1371_LEGACY));
	/* AC'97 warm reset to start the bitclk */
	outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
	snd_delay(2);
	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
	/* Init the sample rate converter */
	outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
	for (idx = 0; idx < 0x80; idx++)
		snd_es1371_src_write(ensoniq, idx, 0);
	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
	snd_es1371_adc_rate(ensoniq, 22050, 1);
	snd_es1371_dac1_rate(ensoniq, 22050, 1);
	snd_es1371_dac2_rate(ensoniq, 22050, 1);
	/* WARNING:
	 * enabling the sample rate converter without properly programming
	 * its parameters causes the chip to lock up (the SRC busy bit will
	 * be stuck high, and I've found no way to rectify this other than
	 * power cycle) - Thomas Sailer
	 */
	outl(0, ES_REG(ensoniq, 1371_SMPRATE));
	/* try reset codec directly */
	outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
#endif
	outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
	outb(0x00, ES_REG(ensoniq, UART_RES));
	snd_control_switch_new(card, &snd_ensoniq_ctl_switch, ensoniq);
#ifdef CHIP1371
	snd_control_switch_new(card, &snd_es1371_ctl_switch, ensoniq);
#endif
	return ensoniq;
}

void snd_ensoniq_free(ensoniq_t * ensoniq)
{
	snd_ensoniq_proc_done(ensoniq);
#ifdef CHIP1370
	outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL));	/* switch everything off */
	outl(0, ES_REG(ensoniq, SERIAL));	/* clear serial interface */
#else
	outl(0, ES_REG(ensoniq, CONTROL));	/* switch everything off */
	outl(0, ES_REG(ensoniq, SERIAL));	/* clear serial interface */
#endif
	snd_kfree(ensoniq);
}

/*
 *  MIDI section
 */

static void snd_ensoniq_midi_interrupt(ensoniq_t * ensoniq,
				       snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	unsigned char status, mask = 0, byte;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (ensoniq->uartm & ES_MODE_INPUT)
		mask |= ES_RXRDY;
	if (ensoniq->uartm & ES_MODE_OUTPUT)
		mask |= ES_TXRDY;
	while (1) {
		status = inb(ES_REG(ensoniq, UART_STATUS));
		if ((status & mask) == 0)
			break;
		if (status & ES_RXRDY) {
			byte = inb(ES_REG(ensoniq, UART_DATA));
			if (mask & ES_RXRDY)
				rmidi->input.data(rmidi, &byte, 1);
		} else if (status & ES_TXRDY) {
			if (mask & ES_TXRDY) {
				spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
				if (rmidi->output.data(rmidi, &byte, 1) != 1) {
					spin_lock_irqsave(&ensoniq->reg_lock, flags);
					ensoniq->uartc &= ~ES_TXINTENM;
					mask &= ~ES_TXRDY;
				} else {
					spin_lock_irqsave(&ensoniq->reg_lock, flags);
					outb(byte, ES_REG(ensoniq, UART_DATA));
				}
			} else {
				ensoniq->uartc &= ~ES_TXINTENM;
			}
		}
	}
	outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static int snd_ensoniq_midi_input_open(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	ensoniq->uartm |= ES_MODE_INPUT;
	if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
		outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
		outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
	}
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_ensoniq_midi_input_close(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
		outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
	} else {
		outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
	}
	ensoniq->uartm &= ~ES_MODE_INPUT;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_ensoniq_midi_output_open(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	ensoniq->uartm |= ES_MODE_OUTPUT;
	if (!(ensoniq->uartm & ES_MODE_INPUT)) {
		outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
		outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
	}
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static int snd_ensoniq_midi_output_close(snd_rawmidi_t * rmidi)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (!(ensoniq->uartm & ES_MODE_INPUT)) {
		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
		outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
	} else {
		outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
	}
	ensoniq->uartm &= ~ES_MODE_OUTPUT;
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
	return 0;
}

static void snd_ensoniq_midi_input_trigger(snd_rawmidi_t * rmidi, int up)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (up)
		ensoniq->uartc |= ES_RXINTEN;
	else
		ensoniq->uartc &= ~ES_RXINTEN;
	outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static void snd_ensoniq_midi_output_trigger(snd_rawmidi_t * rmidi, int up)
{
	unsigned long flags;
	ensoniq_t *ensoniq = (ensoniq_t *) rmidi->private_data;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	if (up) {
		ensoniq->uartc |= ES_TXINTENO(1);
		/* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
		while (ES_TXINTENI(ensoniq->uartc) == 1 &&
		       (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
			unsigned char byte;
			spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
			if (rmidi->output.data(rmidi, &byte, 1) != 1) {
				spin_lock_irqsave(&ensoniq->reg_lock, flags);
				ensoniq->uartc &= ~ES_TXINTENM;
			} else {
				spin_lock_irqsave(&ensoniq->reg_lock, flags);
				outb(byte, ES_REG(ensoniq, UART_DATA));
			}
		}
	} else {
		ensoniq->uartc &= ~ES_TXINTENM;
	}
	outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
}

static struct snd_stru_rawmidi_direction_hw snd_ensoniq_midi_output =
{
	0,			/* flags */
	NULL,			/* private_data */
	NULL,			/* private_free */
	snd_ensoniq_midi_output_open,	/* open */
	snd_ensoniq_midi_output_close,	/* close */
	snd_ensoniq_midi_output_trigger,/* trigger */
	{NULL},			/* io.write */
	NULL,			/* abort */
};

static struct snd_stru_rawmidi_direction_hw snd_ensoniq_midi_input =
{
	0,			/* flags */
	NULL,			/* private_data */
	NULL,			/* private_free */
	snd_ensoniq_midi_input_open,	/* open */
	snd_ensoniq_midi_input_close,	/* close */
	snd_ensoniq_midi_input_trigger,	/* trigger */
	{NULL},			/* io.read */
	NULL,			/* abort */
};

snd_rawmidi_t *snd_ensoniq_midi(ensoniq_t * ensoniq)
{
	snd_rawmidi_t *rmidi;

	if ((rmidi = snd_rawmidi_new_device(ensoniq->card, "ES1370/1")) == NULL)
		return NULL;
#ifdef CHIP1370
	strcpy(rmidi->name, "ES1370");
#else
	strcpy(rmidi->name, "ES1371");
#endif
	memcpy(&rmidi->output.hw, &snd_ensoniq_midi_output, sizeof(snd_ensoniq_midi_output));
	memcpy(&rmidi->input.hw, &snd_ensoniq_midi_input, sizeof(snd_ensoniq_midi_input));
	rmidi->info_flags |= SND_RAWMIDI_INFO_OUTPUT | SND_RAWMIDI_INFO_INPUT | SND_RAWMIDI_INFO_DUPLEX;
	rmidi->private_data = ensoniq;
	ensoniq->rmidi = rmidi;
	return rmidi;
}

/*
 *  Interrupt handler
 */

void snd_ensoniq_interrupt(ensoniq_t * ensoniq)
{
	unsigned int status, sctrl;
	unsigned long flags;

	status = inl(ES_REG(ensoniq, STATUS));
	if (!(status & ES_INTR))
		return;

	spin_lock_irqsave(&ensoniq->reg_lock, flags);
	sctrl = ensoniq->sctrl;
	if (status & ES_DAC1)
		sctrl &= ~ES_P1_INT_EN;
	if (status & ES_DAC2)
		sctrl &= ~ES_P2_INT_EN;
	if (status & ES_ADC)
		sctrl &= ~ES_R1_INT_EN;
	outl(sctrl, ES_REG(ensoniq, SERIAL));
	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);

	if (ensoniq->pcm) {
		if (status & ES_DAC2)
			ensoniq->playback2_subchn1->ack(ensoniq->playback2_subchn);
		if (status & ES_ADC)
			ensoniq->capture_subchn1->ack(ensoniq->capture_subchn);
	}
	if (ensoniq->pcm2) {
		if (status & ES_DAC1)
			ensoniq->playback_subchn1->ack(ensoniq->playback_subchn);
	}
	if (ensoniq->rmidi) {
		if (status & ES_UART)
			snd_ensoniq_midi_interrupt(ensoniq, ensoniq->rmidi);
	}
}

#ifdef CHIP1370
EXPORT_SYMBOL(snd_ensoniq_1370_create);
EXPORT_SYMBOL(snd_ensoniq_1370_free);
EXPORT_SYMBOL(snd_ensoniq_1370_interrupt);
EXPORT_SYMBOL(snd_ensoniq_1370_pcm);
EXPORT_SYMBOL(snd_ensoniq_1370_pcm2);
EXPORT_SYMBOL(snd_ensoniq_1370_midi);
EXPORT_SYMBOL(snd_ensoniq_1370_mixer);
#endif
#ifdef CHIP1371
EXPORT_SYMBOL(snd_ensoniq_1371_create);
EXPORT_SYMBOL(snd_ensoniq_1371_free);
EXPORT_SYMBOL(snd_ensoniq_1371_interrupt);
EXPORT_SYMBOL(snd_ensoniq_1371_pcm);
EXPORT_SYMBOL(snd_ensoniq_1371_pcm2);
EXPORT_SYMBOL(snd_ensoniq_1371_midi);
EXPORT_SYMBOL(snd_ensoniq_1371_mixer);
#endif

/*
 *  INIT part
 */

int init_module(void)
{
	return 0;
}

void cleanup_module(void)
{
}
