File:  [DragonFly] / src / sys / dev / sound / pci / emu10k1.c
Revision 1.4: download - view: text, annotated - select for diffs
Tue Jan 6 16:07:40 2004 UTC (10 years, 11 months ago) by asmodai
Branches: MAIN
CVS tags: HEAD
Add support for SoundBlaster Audigy and Audigy 2.

Based on patches by Orlando Bassotto, which were taken from the ALSA Project
and the SoundBlaster OSS repository.

    1: /*
    2:  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
    3:  * All rights reserved.
    4:  *
    5:  * Redistribution and use in source and binary forms, with or without
    6:  * modification, are permitted provided that the following conditions
    7:  * are met:
    8:  * 1. Redistributions of source code must retain the above copyright
    9:  *    notice, this list of conditions and the following disclaimer.
   10:  * 2. Redistributions in binary form must reproduce the above copyright
   11:  *    notice, this list of conditions and the following disclaimer in the
   12:  *    documentation and/or other materials provided with the distribution.
   13:  *
   14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
   22:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24:  * SUCH DAMAGE.
   25:  *
   26:  * $FreeBSD: src/sys/dev/sound/pci/emu10k1.c,v 1.6.2.9 2002/04/22 15:49:32 cg Exp $
   27:  * $DragonFly: src/sys/dev/sound/pci/emu10k1.c,v 1.4 2004/01/06 16:07:40 asmodai Exp $
   28:  */
   29: 
   30: #include <dev/sound/pcm/sound.h>
   31: #include <dev/sound/pcm/ac97.h>
   32: #include "gnu/emu10k1.h"
   33: 
   34: #include <bus/pci/pcireg.h>
   35: #include <bus/pci/pcivar.h>
   36: #include <sys/queue.h>
   37: 
   38: SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/emu10k1.c,v 1.4 2004/01/06 16:07:40 asmodai Exp $");
   39: 
   40: /* -------------------------------------------------------------------- */
   41: 
   42: #define EMU10K1_PCI_ID 		0x00021102
   43: #define EMU10K2_PCI_ID		0x00041102
   44: #define EMU_DEFAULT_BUFSZ	4096
   45: #define EMU_MAX_CHANS		8
   46: #undef EMUDEBUG
   47: 
   48: struct emu_memblk {
   49: 	SLIST_ENTRY(emu_memblk) link;
   50: 	void *buf;
   51: 	u_int32_t pte_start, pte_size;
   52: };
   53: 
   54: struct emu_mem {
   55: 	u_int8_t bmap[MAXPAGES / 8];
   56: 	u_int32_t *ptb_pages;
   57: 	void *silent_page;
   58:        	SLIST_HEAD(, emu_memblk) blocks;
   59: };
   60: 
   61: struct emu_voice {
   62: 	int vnum;
   63: 	int b16:1, stereo:1, busy:1, running:1, ismaster:1;
   64: 	int speed;
   65: 	int start, end, vol;
   66: 	int fxrt1;	/* FX routing */
   67: 	int fxrt2;	/* FX routing (only for audigy) */
   68: 	u_int32_t buf;
   69: 	struct emu_voice *slave;
   70: 	struct pcm_channel *channel;
   71: };
   72: 
   73: struct sc_info;
   74: 
   75: /* channel registers */
   76: struct sc_pchinfo {
   77: 	int spd, fmt, blksz, run;
   78: 	struct emu_voice *master, *slave;
   79: 	struct snd_dbuf *buffer;
   80: 	struct pcm_channel *channel;
   81: 	struct sc_info *parent;
   82: };
   83: 
   84: struct sc_rchinfo {
   85: 	int spd, fmt, run, blksz, num;
   86: 	u_int32_t idxreg, basereg, sizereg, setupreg, irqmask;
   87: 	struct snd_dbuf *buffer;
   88: 	struct pcm_channel *channel;
   89: 	struct sc_info *parent;
   90: };
   91: 
   92: /* device private data */
   93: struct sc_info {
   94: 	device_t	dev;
   95: 	u_int32_t 	type, rev;
   96: 	u_int32_t	tos_link:1, APS:1, audigy:1, audigy2:1;
   97: 	u_int32_t	addrmask;	/* wider if audigy */
   98: 
   99: 	bus_space_tag_t st;
  100: 	bus_space_handle_t sh;
  101: 	bus_dma_tag_t parent_dmat;
  102: 
  103: 	struct resource *reg, *irq;
  104: 	void		*ih;
  105: 	void		*lock;
  106: 
  107: 	unsigned int bufsz;
  108: 	int timer, timerinterval;
  109: 	int pnum, rnum;
  110: 	int nchans;
  111: 	struct emu_mem mem;
  112: 	struct emu_voice voice[64];
  113: 	struct sc_pchinfo pch[EMU_MAX_CHANS];
  114: 	struct sc_rchinfo rch[3];
  115: };
  116: 
  117: /* -------------------------------------------------------------------- */
  118: 
  119: /*
  120:  * prototypes
  121:  */
  122: 
  123: /* stuff */
  124: static int emu_init(struct sc_info *);
  125: static void emu_intr(void *);
  126: static void *emu_malloc(struct sc_info *sc, u_int32_t sz);
  127: static void *emu_memalloc(struct sc_info *sc, u_int32_t sz);
  128: static int emu_memfree(struct sc_info *sc, void *buf);
  129: static int emu_memstart(struct sc_info *sc, void *buf);
  130: #ifdef EMUDEBUG
  131: static void emu_vdump(struct sc_info *sc, struct emu_voice *v);
  132: #endif
  133: 
  134: /* talk to the card */
  135: static u_int32_t emu_rd(struct sc_info *, int, int);
  136: static void emu_wr(struct sc_info *, int, u_int32_t, int);
  137: 
  138: /* -------------------------------------------------------------------- */
  139: 
  140: static u_int32_t emu_rfmt_ac97[] = {
  141: 	AFMT_S16_LE,
  142: 	AFMT_STEREO | AFMT_S16_LE,
  143: 	0
  144: };
  145: 
  146: static u_int32_t emu_rfmt_mic[] = {
  147: 	AFMT_U8,
  148: 	0
  149: };
  150: 
  151: static u_int32_t emu_rfmt_efx[] = {
  152: 	AFMT_STEREO | AFMT_S16_LE,
  153: 	0
  154: };
  155: 
  156: static struct pcmchan_caps emu_reccaps[3] = {
  157: 	{8000, 48000, emu_rfmt_ac97, 0},
  158: 	{8000, 8000, emu_rfmt_mic, 0},
  159: 	{48000, 48000, emu_rfmt_efx, 0},
  160: };
  161: 
  162: static u_int32_t emu_pfmt[] = {
  163: 	AFMT_U8,
  164: 	AFMT_STEREO | AFMT_U8,
  165: 	AFMT_S16_LE,
  166: 	AFMT_STEREO | AFMT_S16_LE,
  167: 	0
  168: };
  169: 
  170: static struct pcmchan_caps emu_playcaps = {4000, 48000, emu_pfmt, 0};
  171: 
  172: static int adcspeed[8] = {48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000};
  173: /* audigy supports 12kHz. */
  174: static int audigy_adcspeed[9] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000};
  175: 
  176: /* -------------------------------------------------------------------- */
  177: /* Hardware */
  178: static u_int32_t
  179: emu_rd(struct sc_info *sc, int regno, int size)
  180: {
  181: 	switch (size) {
  182: 	case 1:
  183: 		return bus_space_read_1(sc->st, sc->sh, regno);
  184: 	case 2:
  185: 		return bus_space_read_2(sc->st, sc->sh, regno);
  186: 	case 4:
  187: 		return bus_space_read_4(sc->st, sc->sh, regno);
  188: 	default:
  189: 		return 0xffffffff;
  190: 	}
  191: }
  192: 
  193: static void
  194: emu_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
  195: {
  196: 	switch (size) {
  197: 	case 1:
  198: 		bus_space_write_1(sc->st, sc->sh, regno, data);
  199: 		break;
  200: 	case 2:
  201: 		bus_space_write_2(sc->st, sc->sh, regno, data);
  202: 		break;
  203: 	case 4:
  204: 		bus_space_write_4(sc->st, sc->sh, regno, data);
  205: 		break;
  206: 	}
  207: }
  208: 
  209: static u_int32_t
  210: emu_rdptr(struct sc_info *sc, int chn, int reg)
  211: {
  212: 	u_int32_t ptr, val, mask, size, offset;
  213: 
  214: 	ptr = ((reg << 16) & sc->addrmask) | (chn & PTR_CHANNELNUM_MASK);
  215: 	emu_wr(sc, PTR, ptr, 4);
  216: 	val = emu_rd(sc, DATA, 4);
  217: 	if (reg & 0xff000000) {
  218: 		size = (reg >> 24) & 0x3f;
  219: 		offset = (reg >> 16) & 0x1f;
  220: 		mask = ((1 << size) - 1) << offset;
  221: 		val &= mask;
  222: 		val >>= offset;
  223: 	}
  224: 	return val;
  225: }
  226: 
  227: static void
  228: emu_wrptr(struct sc_info *sc, int chn, int reg, u_int32_t data)
  229: {
  230: 	u_int32_t ptr, mask, size, offset;
  231: 
  232: 	ptr = ((reg << 16) & sc->addrmask) | (chn & PTR_CHANNELNUM_MASK);
  233: 	emu_wr(sc, PTR, ptr, 4);
  234: 	if (reg & 0xff000000) {
  235: 		size = (reg >> 24) & 0x3f;
  236: 		offset = (reg >> 16) & 0x1f;
  237: 		mask = ((1 << size) - 1) << offset;
  238: 		data <<= offset;
  239: 		data &= mask;
  240: 		data |= emu_rd(sc, DATA, 4) & ~mask;
  241: 	}
  242: 	emu_wr(sc, DATA, data, 4);
  243: }
  244: 
  245: static void
  246: emu_wrefx(struct sc_info *sc, unsigned int pc, unsigned int data)
  247: {
  248: 	pc += sc->audigy ? AUDIGY_CODEBASE : MICROCODEBASE;
  249: 	emu_wrptr(sc, 0, pc, data);
  250: }
  251: 
  252: /* -------------------------------------------------------------------- */
  253: /* ac97 codec */
  254: /* no locking needed */
  255: 
  256: static int
  257: emu_rdcd(kobj_t obj, void *devinfo, int regno)
  258: {
  259: 	struct sc_info *sc = (struct sc_info *)devinfo;
  260: 
  261: 	emu_wr(sc, AC97ADDRESS, regno, 1);
  262: 	return emu_rd(sc, AC97DATA, 2);
  263: }
  264: 
  265: static int
  266: emu_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
  267: {
  268: 	struct sc_info *sc = (struct sc_info *)devinfo;
  269:   
  270: 	emu_wr(sc, AC97ADDRESS, regno, 1);
  271: 	emu_wr(sc, AC97DATA, data, 2);
  272: 	return 0;
  273: }
  274: 
  275: static kobj_method_t emu_ac97_methods[] = {
  276:     	KOBJMETHOD(ac97_read,		emu_rdcd),
  277:     	KOBJMETHOD(ac97_write,		emu_wrcd),
  278: 	{ 0, 0 }
  279: };
  280: AC97_DECLARE(emu_ac97);
  281: 
  282: /* -------------------------------------------------------------------- */
  283: /* stuff */
  284: static int
  285: emu_settimer(struct sc_info *sc)
  286: {
  287: 	struct sc_pchinfo *pch;
  288: 	struct sc_rchinfo *rch;
  289: 	int i, tmp, rate;
  290: 
  291: 	rate = 0;
  292: 	for (i = 0; i < sc->nchans; i++) {
  293: 		pch = &sc->pch[i];
  294: 		if (pch->buffer) {
  295: 			tmp = (pch->spd * sndbuf_getbps(pch->buffer)) / pch->blksz;
  296: 			if (tmp > rate)
  297: 				rate = tmp;
  298: 		}
  299: 	}
  300: 
  301: 	for (i = 0; i < 3; i++) {
  302: 		rch = &sc->rch[i];
  303: 		if (rch->buffer) {
  304: 			tmp = (rch->spd * sndbuf_getbps(rch->buffer)) / rch->blksz;
  305: 			if (tmp > rate)
  306: 				rate = tmp;
  307: 		}
  308: 	}
  309: 	RANGE(rate, 48, 9600);
  310: 	sc->timerinterval = 48000 / rate;
  311: 	emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
  312: 
  313: 	return sc->timerinterval;
  314: }
  315: 
  316: static int
  317: emu_enatimer(struct sc_info *sc, int go)
  318: {
  319: 	u_int32_t x;
  320: 	if (go) {
  321: 		if (sc->timer++ == 0) {
  322: 			x = emu_rd(sc, INTE, 4);
  323: 			x |= INTE_INTERVALTIMERENB;
  324: 			emu_wr(sc, INTE, x, 4);
  325: 		}
  326: 	} else {
  327: 		sc->timer = 0;
  328: 		x = emu_rd(sc, INTE, 4);
  329: 		x &= ~INTE_INTERVALTIMERENB;
  330: 		emu_wr(sc, INTE, x, 4);
  331: 	}
  332: 	return 0;
  333: }
  334: 
  335: static void
  336: emu_enastop(struct sc_info *sc, char channel, int enable)
  337: {
  338: 	int reg = (channel & 0x20)? SOLEH : SOLEL;
  339: 	channel &= 0x1f;
  340: 	reg |= 1 << 24;
  341: 	reg |= channel << 16;
  342: 	emu_wrptr(sc, 0, reg, enable);
  343: }
  344: 
  345: static int
  346: emu_recval(int speed) {
  347: 	int val;
  348: 
  349: 	val = 0;
  350: 	while (val < 7 && speed < adcspeed[val])
  351: 		val++;
  352: 	return val;
  353: }
  354: 
  355: static int
  356: audigy_recval(int speed) {
  357: 	int val;
  358: 
  359: 	val = 0;
  360: 	while (val < 8 && speed < audigy_adcspeed[val])
  361: 		val++;
  362: 	return val;
  363: }
  364: 
  365: static u_int32_t
  366: emu_rate_to_pitch(u_int32_t rate)
  367: {
  368: 	static u_int32_t logMagTable[128] = {
  369: 		0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
  370: 		0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
  371: 		0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
  372: 		0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
  373: 		0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
  374: 		0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
  375: 		0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
  376: 		0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
  377: 		0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
  378: 		0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
  379: 		0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
  380: 		0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
  381: 		0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
  382: 		0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
  383: 		0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
  384: 		0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
  385: 	};
  386: 	static char logSlopeTable[128] = {
  387: 		0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
  388: 		0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
  389: 		0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
  390: 		0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
  391: 		0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
  392: 		0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
  393: 		0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
  394: 		0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
  395: 		0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
  396: 		0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
  397: 		0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
  398: 		0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
  399: 		0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
  400: 		0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
  401: 		0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
  402: 		0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
  403: 	};
  404: 	int i;
  405: 
  406: 	if (rate == 0)
  407: 		return 0;	/* Bail out if no leading "1" */
  408: 	rate *= 11185;	/* Scale 48000 to 0x20002380 */
  409: 	for (i = 31; i > 0; i--) {
  410: 		if (rate & 0x80000000) {	/* Detect leading "1" */
  411: 			return (((u_int32_t) (i - 15) << 20) +
  412: 			       logMagTable[0x7f & (rate >> 24)] +
  413: 				      (0x7f & (rate >> 17)) *
  414: 			     logSlopeTable[0x7f & (rate >> 24)]);
  415: 		}
  416: 		rate <<= 1;
  417: 	}
  418: 
  419: 	return 0;		/* Should never reach this point */
  420: }
  421: 
  422: static u_int32_t
  423: emu_rate_to_linearpitch(u_int32_t rate)
  424: {
  425: 	rate = (rate << 8) / 375;
  426: 	return (rate >> 1) + (rate & 1);
  427: }
  428: 
  429: static struct emu_voice *
  430: emu_valloc(struct sc_info *sc)
  431: {
  432: 	struct emu_voice *v;
  433: 	int i;
  434: 
  435: 	v = NULL;
  436: 	for (i = 0; i < 64 && sc->voice[i].busy; i++);
  437: 	if (i < 64) {
  438: 		v = &sc->voice[i];
  439: 		v->busy = 1;
  440: 	}
  441: 	return v;
  442: }
  443: 
  444: static int
  445: emu_vinit(struct sc_info *sc, struct emu_voice *m, struct emu_voice *s,
  446: 	  u_int32_t sz, struct snd_dbuf *b)
  447: {
  448: 	void *buf;
  449: 
  450: 	buf = emu_memalloc(sc, sz);
  451: 	if (buf == NULL)
  452: 		return -1;
  453: 	if (b != NULL)
  454: 		sndbuf_setup(b, buf, sz);
  455: 	m->start = emu_memstart(sc, buf) * EMUPAGESIZE;
  456: 	m->end = m->start + sz;
  457: 	m->channel = NULL;
  458: 	m->speed = 0;
  459: 	m->b16 = 0;
  460: 	m->stereo = 0;
  461: 	m->running = 0;
  462: 	m->ismaster = 1;
  463: 	m->vol = 0xff;
  464: 	m->buf = vtophys(buf);
  465: 	m->slave = s;
  466: 	if (sc->audigy) {
  467: 		m->fxrt1 = A_FXBUS_PCM_LEFT | A_FXBUS_PCM_RIGHT << 8 |
  468: 			A_FXBUS_PCM_LEFT_REAR << 16 |
  469: 			A_FXBUS_PCM_RIGHT_REAR << 24;
  470: 		m->fxrt2 = A_FXBUS_PCM_CENTER | A_FXBUS_PCM_LFE << 8 |
  471: 			A_FXBUS_MIDI_CHORUS << 16 | 
  472: 			A_FXBUS_MIDI_REVERB << 24;
  473: 	} else {
  474: 		m->fxrt1 = FXBUS_PCM_LEFT | FXBUS_PCM_RIGHT << 4 |
  475: 			FXBUS_MIDI_CHORUS << 8 |
  476: 			FXBUS_MIDI_REVERB << 12;
  477: 		m->fxrt2 = 0;
  478: 	}
  479: 
  480: 	if (s != NULL) {
  481: 		s->start = m->start;
  482: 		s->end = m->end;
  483: 		s->channel = NULL;
  484: 		s->speed = 0;
  485: 		s->b16 = 0;
  486: 		s->stereo = 0;
  487: 		s->running = 0;
  488: 		s->ismaster = 0;
  489: 		s->vol = m->vol;
  490: 		s->buf = m->buf;
  491: 		s->fxrt1 = m->fxrt1;
  492: 		s->fxrt2 = m->fxrt2;
  493: 		s->slave = NULL;
  494: 	}
  495: 	return 0;
  496: }
  497: 
  498: static void
  499: emu_vsetup(struct sc_pchinfo *ch)
  500: {
  501: 	struct emu_voice *v = ch->master;
  502: 
  503: 	if (ch->fmt) {
  504: 		v->b16 = (ch->fmt & AFMT_16BIT)? 1 : 0;
  505: 		v->stereo = (ch->fmt & AFMT_STEREO)? 1 : 0;
  506: 		if (v->slave != NULL) {
  507: 			v->slave->b16 = v->b16;
  508: 			v->slave->stereo = v->stereo;
  509: 		}
  510: 	}
  511: 	if (ch->spd) {
  512: 		v->speed = ch->spd;
  513: 		if (v->slave != NULL)
  514: 			v->slave->speed = v->speed;
  515: 	}
  516: }
  517: 
  518: static void
  519: emu_vwrite(struct sc_info *sc, struct emu_voice *v)
  520: {
  521: 	int s;
  522: 	int l, r, x, y;
  523: 	u_int32_t sa, ea, start, val, silent_page;
  524: 
  525: 	s = (v->stereo? 1 : 0) + (v->b16? 1 : 0);
  526: 
  527: 	sa = v->start >> s;
  528: 	ea = v->end >> s;
  529: 
  530: 	l = r = x = y = v->vol;
  531: 	if (v->stereo) {
  532: 		l = v->ismaster? l : 0;
  533: 		r = v->ismaster? 0 : r;
  534: 	}
  535: 
  536: 	emu_wrptr(sc, v->vnum, CPF, v->stereo? CPF_STEREO_MASK : 0);
  537: 	val = v->stereo? 28 : 30;
  538: 	val *= v->b16? 1 : 2;
  539: 	start = sa + val;
  540: 
  541: 	if (sc->audigy) {
  542: 		emu_wrptr(sc, v->vnum, A_FXRT1, v->fxrt1);
  543: 		emu_wrptr(sc, v->vnum, A_FXRT2, v->fxrt2);
  544: 		emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, 0);
  545: 	}
  546: 	else
  547: 		emu_wrptr(sc, v->vnum, FXRT, v->fxrt1 << 16);
  548: 
  549: 	emu_wrptr(sc, v->vnum, PTRX, (x << 8) | r);
  550: 	emu_wrptr(sc, v->vnum, DSL, ea | (y << 24));
  551: 	emu_wrptr(sc, v->vnum, PSST, sa | (l << 24));
  552: 	emu_wrptr(sc, v->vnum, CCCA, start | (v->b16? 0 : CCCA_8BITSELECT));
  553: 
  554: 	emu_wrptr(sc, v->vnum, Z1, 0);
  555: 	emu_wrptr(sc, v->vnum, Z2, 0);
  556: 
  557: 	silent_page = ((u_int32_t)vtophys(sc->mem.silent_page) << 1) | MAP_PTI_MASK;
  558: 	emu_wrptr(sc, v->vnum, MAPA, silent_page);
  559: 	emu_wrptr(sc, v->vnum, MAPB, silent_page);
  560: 
  561: 	emu_wrptr(sc, v->vnum, CVCF, CVCF_CURRENTFILTER_MASK);
  562: 	emu_wrptr(sc, v->vnum, VTFT, VTFT_FILTERTARGET_MASK);
  563: 	emu_wrptr(sc, v->vnum, ATKHLDM, 0);
  564: 	emu_wrptr(sc, v->vnum, DCYSUSM, DCYSUSM_DECAYTIME_MASK);
  565: 	emu_wrptr(sc, v->vnum, LFOVAL1, 0x8000);
  566: 	emu_wrptr(sc, v->vnum, LFOVAL2, 0x8000);
  567: 	emu_wrptr(sc, v->vnum, FMMOD, 0);
  568: 	emu_wrptr(sc, v->vnum, TREMFRQ, 0);
  569: 	emu_wrptr(sc, v->vnum, FM2FRQ2, 0);
  570: 	emu_wrptr(sc, v->vnum, ENVVAL, 0x8000);
  571: 
  572: 	emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
  573: 	emu_wrptr(sc, v->vnum, ENVVOL, 0x8000);
  574: 
  575: 	emu_wrptr(sc, v->vnum, PEFE_FILTERAMOUNT, 0x7f);
  576: 	emu_wrptr(sc, v->vnum, PEFE_PITCHAMOUNT, 0);
  577: 
  578: 	if (v->slave != NULL)
  579: 		emu_vwrite(sc, v->slave);
  580: }
  581: 
  582: static void
  583: emu_vtrigger(struct sc_info *sc, struct emu_voice *v, int go)
  584: {
  585: 	u_int32_t pitch_target, initial_pitch;
  586: 	u_int32_t cra, cs, ccis;
  587: 	u_int32_t sample, i;
  588: 
  589: 	if (go) {
  590: 		cra = 64;
  591: 		cs = v->stereo? 4 : 2;
  592: 		ccis = v->stereo? 28 : 30;
  593: 		ccis *= v->b16? 1 : 2;
  594: 		sample = v->b16? 0x00000000 : 0x80808080;
  595: 
  596: 		for (i = 0; i < cs; i++)
  597: 			emu_wrptr(sc, v->vnum, CD0 + i, sample);
  598: 		emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, 0);
  599: 		emu_wrptr(sc, v->vnum, CCR_READADDRESS, cra);
  600: 		emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, ccis);
  601: 
  602: 		emu_wrptr(sc, v->vnum, IFATN, 0xff00);
  603: 		emu_wrptr(sc, v->vnum, VTFT, 0xffffffff);
  604: 		emu_wrptr(sc, v->vnum, CVCF, 0xffffffff);
  605: 		emu_wrptr(sc, v->vnum, DCYSUSV, 0x00007f7f);
  606: 		emu_enastop(sc, v->vnum, 0);
  607: 
  608: 		pitch_target = emu_rate_to_linearpitch(v->speed);
  609: 		initial_pitch = emu_rate_to_pitch(v->speed) >> 8;
  610: 		emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, pitch_target);
  611: 		emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, pitch_target);
  612: 		emu_wrptr(sc, v->vnum, IP, initial_pitch);
  613: 	} else {
  614: 		emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, 0);
  615: 		emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, 0);
  616: 		emu_wrptr(sc, v->vnum, IFATN, 0xffff);
  617: 		emu_wrptr(sc, v->vnum, VTFT, 0x0000ffff);
  618: 		emu_wrptr(sc, v->vnum, CVCF, 0x0000ffff);
  619: 		emu_wrptr(sc, v->vnum, IP, 0);
  620: 		emu_enastop(sc, v->vnum, 1);
  621: 	}
  622: 	if (v->slave != NULL)
  623: 		emu_vtrigger(sc, v->slave, go);
  624: }
  625: 
  626: static int
  627: emu_vpos(struct sc_info *sc, struct emu_voice *v)
  628: {
  629: 	int s, ptr;
  630: 
  631: 	s = (v->b16? 1 : 0) + (v->stereo? 1 : 0);
  632: 	ptr = (emu_rdptr(sc, v->vnum, CCCA_CURRADDR) - (v->start >> s)) << s;
  633: 	return ptr & ~0x0000001f;
  634: }
  635: 
  636: #ifdef EMUDEBUG
  637: static void
  638: emu_vdump(struct sc_info *sc, struct emu_voice *v)
  639: {
  640: 	char *regname[] = { "cpf", "ptrx", "cvcf", "vtft", "z2", "z1", "psst", "dsl",
  641: 			    "ccca", "ccr", "clp", "fxrt", "mapa", "mapb", NULL, NULL,
  642: 			    "envvol", "atkhldv", "dcysusv", "lfoval1",
  643: 			    "envval", "atkhldm", "dcysusm", "lfoval2",
  644: 			    "ip", "ifatn", "pefe", "fmmod", "tremfrq", "fmfrq2",
  645: 			    "tempenv" };
  646: 	char *regname2[] = { "mudata1", "mustat1", "mudata2", "mustat2",
  647: 			     "fxwc1", "fxwc2", "spdrate", NULL, NULL,
  648: 			     NULL, NULL, NULL, "fxrt2", "sndamnt", "fxrt1",
  649: 			     NULL, NULL };
  650: 	int i, x;
  651: 
  652: 	printf("voice number %d\n", v->vnum);
  653: 	for (i = 0, x = 0; i <= 0x1e; i++) {
  654: 		if (regname[i] == NULL)
  655: 			continue;
  656: 		printf("%s\t[%08x]", regname[i], emu_rdptr(sc, v->vnum, i));
  657: 		printf("%s", (x == 2)? "\n" : "\t");
  658: 		x++;
  659: 		if (x > 2)
  660: 			x = 0;
  661: 	}
  662: 
  663: 	/* Print out audigy extra registers */
  664: 	if (sc->audigy) {
  665: 		for (i = 0; i <= 0xe; i++) {
  666: 			if (regname2[i] == NULL)
  667: 				continue;
  668: 			printf("%s\t[%08x]", regname2[i], emu_rdptr(sc, v->vnum, i + 0x70));
  669: 			printf("%s", (x == 2)? "\n" : "\t");
  670: 			x++;
  671: 			if (x > 2)
  672: 				x = 0;
  673: 		}
  674: 	}
  675: 
  676: 	printf("\n\n");
  677: }
  678: #endif
  679: 
  680: /* channel interface */
  681: static void *
  682: emupchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
  683: {
  684: 	struct sc_info *sc = devinfo;
  685: 	struct sc_pchinfo *ch;
  686: 	void *r;
  687: 
  688: 	KASSERT(dir == PCMDIR_PLAY, ("emupchan_init: bad direction"));
  689: 	ch = &sc->pch[sc->pnum++];
  690: 	ch->buffer = b;
  691: 	ch->parent = sc;
  692: 	ch->channel = c;
  693: 	ch->blksz = sc->bufsz / 2;
  694: 	ch->fmt = AFMT_U8;
  695: 	ch->spd = 8000;
  696: 	snd_mtxlock(sc->lock);
  697: 	ch->master = emu_valloc(sc);
  698: 	ch->slave = emu_valloc(sc);
  699: 	r = (emu_vinit(sc, ch->master, ch->slave, sc->bufsz, ch->buffer))? NULL : ch;
  700: 	snd_mtxunlock(sc->lock);
  701: 
  702: 	return r;
  703: }
  704: 
  705: static int
  706: emupchan_free(kobj_t obj, void *data)
  707: {
  708: 	struct sc_pchinfo *ch = data;
  709: 	struct sc_info *sc = ch->parent;
  710: 	int r;
  711: 
  712: 	snd_mtxlock(sc->lock);
  713: 	r = emu_memfree(sc, sndbuf_getbuf(ch->buffer));
  714: 	snd_mtxunlock(sc->lock);
  715: 
  716: 	return r;
  717: }
  718: 
  719: static int
  720: emupchan_setformat(kobj_t obj, void *data, u_int32_t format)
  721: {
  722: 	struct sc_pchinfo *ch = data;
  723: 
  724: 	ch->fmt = format;
  725: 	return 0;
  726: }
  727: 
  728: static int
  729: emupchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
  730: {
  731: 	struct sc_pchinfo *ch = data;
  732: 
  733: 	ch->spd = speed;
  734: 	return ch->spd;
  735: }
  736: 
  737: static int
  738: emupchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
  739: {
  740: 	struct sc_pchinfo *ch = data;
  741: 	struct sc_info *sc = ch->parent;
  742: 	int irqrate, blksz;
  743: 
  744: 	ch->blksz = blocksize;
  745: 	snd_mtxlock(sc->lock);
  746: 	emu_settimer(sc);
  747: 	irqrate = 48000 / sc->timerinterval;
  748: 	snd_mtxunlock(sc->lock);
  749: 	blksz = (ch->spd * sndbuf_getbps(ch->buffer)) / irqrate;
  750: 	return blocksize;
  751: }
  752: 
  753: static int
  754: emupchan_trigger(kobj_t obj, void *data, int go)
  755: {
  756: 	struct sc_pchinfo *ch = data;
  757: 	struct sc_info *sc = ch->parent;
  758: 
  759: 	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
  760: 		return 0;
  761: 
  762: 	snd_mtxlock(sc->lock);
  763: 	if (go == PCMTRIG_START) {
  764: 		emu_vsetup(ch);
  765: 		emu_vwrite(sc, ch->master);
  766: 		emu_settimer(sc);
  767: 		emu_enatimer(sc, 1);
  768: #ifdef EMUDEBUG
  769: 		printf("start [%d bit, %s, %d hz]\n",
  770: 			ch->master->b16? 16 : 8,
  771: 			ch->master->stereo? "stereo" : "mono",
  772: 			ch->master->speed);
  773: 		emu_vdump(sc, ch->master);
  774: 		emu_vdump(sc, ch->slave);
  775: #endif
  776: 	}
  777: 	ch->run = (go == PCMTRIG_START)? 1 : 0;
  778: 	emu_vtrigger(sc, ch->master, ch->run);
  779: 	snd_mtxunlock(sc->lock);
  780: 	return 0;
  781: }
  782: 
  783: static int
  784: emupchan_getptr(kobj_t obj, void *data)
  785: {
  786: 	struct sc_pchinfo *ch = data;
  787: 	struct sc_info *sc = ch->parent;
  788: 	int r;
  789: 
  790: 	snd_mtxlock(sc->lock);
  791: 	r = emu_vpos(sc, ch->master);
  792: 	snd_mtxunlock(sc->lock);
  793: 
  794: 	return r;
  795: }
  796: 
  797: static struct pcmchan_caps *
  798: emupchan_getcaps(kobj_t obj, void *data)
  799: {
  800: 	return &emu_playcaps;
  801: }
  802: 
  803: static kobj_method_t emupchan_methods[] = {
  804:     	KOBJMETHOD(channel_init,		emupchan_init),
  805:     	KOBJMETHOD(channel_free,		emupchan_free),
  806:     	KOBJMETHOD(channel_setformat,		emupchan_setformat),
  807:     	KOBJMETHOD(channel_setspeed,		emupchan_setspeed),
  808:     	KOBJMETHOD(channel_setblocksize,	emupchan_setblocksize),
  809:     	KOBJMETHOD(channel_trigger,		emupchan_trigger),
  810:     	KOBJMETHOD(channel_getptr,		emupchan_getptr),
  811:     	KOBJMETHOD(channel_getcaps,		emupchan_getcaps),
  812: 	{ 0, 0 }
  813: };
  814: CHANNEL_DECLARE(emupchan);
  815: 
  816: /* channel interface */
  817: static void *
  818: emurchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
  819: {
  820: 	struct sc_info *sc = devinfo;
  821: 	struct sc_rchinfo *ch;
  822: 
  823: 	KASSERT(dir == PCMDIR_REC, ("emurchan_init: bad direction"));
  824: 	ch = &sc->rch[sc->rnum];
  825: 	ch->buffer = b;
  826: 	ch->parent = sc;
  827: 	ch->channel = c;
  828: 	ch->blksz = sc->bufsz / 2;
  829: 	ch->fmt = AFMT_U8;
  830: 	ch->spd = 8000;
  831: 	ch->num = sc->rnum;
  832: 	switch(sc->rnum) {
  833: 	case 0:
  834: 		ch->idxreg = sc->audigy ? A_ADCIDX : ADCIDX;
  835: 		ch->basereg = ADCBA;
  836: 		ch->sizereg = ADCBS;
  837: 		ch->setupreg = ADCCR;
  838: 		ch->irqmask = INTE_ADCBUFENABLE;
  839: 		break;
  840: 
  841: 	case 1:
  842: 		ch->idxreg = FXIDX;
  843: 		ch->basereg = FXBA;
  844: 		ch->sizereg = FXBS;
  845: 		ch->setupreg = FXWC;
  846: 		ch->irqmask = INTE_EFXBUFENABLE;
  847: 		break;
  848: 
  849: 	case 2:
  850: 		ch->idxreg = MICIDX;
  851: 		ch->basereg = MICBA;
  852: 		ch->sizereg = MICBS;
  853: 		ch->setupreg = 0;
  854: 		ch->irqmask = INTE_MICBUFENABLE;
  855: 		break;
  856: 	}
  857: 	sc->rnum++;
  858: 	if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsz) == -1)
  859: 		return NULL;
  860: 	else {
  861: 		snd_mtxlock(sc->lock);
  862: 		emu_wrptr(sc, 0, ch->basereg, vtophys(sndbuf_getbuf(ch->buffer)));
  863: 		emu_wrptr(sc, 0, ch->sizereg, 0); /* off */
  864: 		snd_mtxunlock(sc->lock);
  865: 		return ch;
  866: 	}
  867: }
  868: 
  869: static int
  870: emurchan_setformat(kobj_t obj, void *data, u_int32_t format)
  871: {
  872: 	struct sc_rchinfo *ch = data;
  873: 
  874: 	ch->fmt = format;
  875: 	return 0;
  876: }
  877: 
  878: static int
  879: emurchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
  880: {
  881: 	struct sc_rchinfo *ch = data;
  882: 
  883: 	if (ch->num == 0) {
  884: 		if (ch->parent->audigy)
  885: 			speed = audigy_adcspeed[audigy_recval(speed)];
  886: 		else
  887: 			speed = adcspeed[emu_recval(speed)];
  888: 	}
  889: 	if (ch->num == 1)
  890: 		speed = 48000;
  891: 	if (ch->num == 2)
  892: 		speed = 8000;
  893: 	ch->spd = speed;
  894: 	return ch->spd;
  895: }
  896: 
  897: static int
  898: emurchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
  899: {
  900: 	struct sc_rchinfo *ch = data;
  901: 	struct sc_info *sc = ch->parent;
  902: 	int irqrate, blksz;
  903: 
  904: 	ch->blksz = blocksize;
  905: 	snd_mtxlock(sc->lock);
  906: 	emu_settimer(sc);
  907: 	irqrate = 48000 / sc->timerinterval;
  908: 	snd_mtxunlock(sc->lock);
  909: 	blksz = (ch->spd * sndbuf_getbps(ch->buffer)) / irqrate;
  910: 	return blocksize;
  911: }
  912: 
  913: /* semantic note: must start at beginning of buffer */
  914: static int
  915: emurchan_trigger(kobj_t obj, void *data, int go)
  916: {
  917: 	struct sc_rchinfo *ch = data;
  918: 	struct sc_info *sc = ch->parent;
  919: 	u_int32_t val, sz;
  920: 
  921: 	switch(sc->bufsz) {
  922: 	case 4096:
  923: 		sz = ADCBS_BUFSIZE_4096;
  924: 		break;
  925: 
  926: 	case 8192:
  927: 		sz = ADCBS_BUFSIZE_8192;
  928: 		break;
  929: 
  930: 	case 16384:
  931: 		sz = ADCBS_BUFSIZE_16384;
  932: 		break;
  933: 
  934: 	case 32768:
  935: 		sz = ADCBS_BUFSIZE_32768;
  936: 		break;
  937: 
  938: 	case 65536:
  939: 		sz = ADCBS_BUFSIZE_65536;
  940: 		break;
  941: 
  942: 	default:
  943: 		sz = ADCBS_BUFSIZE_4096;
  944: 	}
  945: 
  946: 	snd_mtxlock(sc->lock);
  947: 	switch(go) {
  948: 	case PCMTRIG_START:
  949: 		ch->run = 1;
  950: 		emu_wrptr(sc, 0, ch->sizereg, sz);
  951: 		if (ch->num == 0) {
  952: 			if (sc->audigy) {
  953: 				val = A_ADCCR_LCHANENABLE;
  954: 				if (ch->fmt & AFMT_STEREO)
  955: 					val |= A_ADCCR_RCHANENABLE;
  956: 				val |= audigy_recval(ch->spd);
  957: 			} else {
  958: 				val = ADCCR_LCHANENABLE;
  959: 				if (ch->fmt & AFMT_STEREO)
  960: 					val |= ADCCR_RCHANENABLE;
  961: 				val |= emu_recval(ch->spd);
  962: 			}
  963: 
  964: 			emu_wrptr(sc, 0, ch->setupreg, 0);
  965: 			emu_wrptr(sc, 0, ch->setupreg, val);
  966: 		}
  967: 		val = emu_rd(sc, INTE, 4);
  968: 		val |= ch->irqmask;
  969: 		emu_wr(sc, INTE, val, 4);
  970: 		break;
  971: 
  972: 	case PCMTRIG_STOP:
  973: 	case PCMTRIG_ABORT:
  974: 		ch->run = 0;
  975: 		emu_wrptr(sc, 0, ch->sizereg, 0);
  976: 		if (ch->setupreg)
  977: 			emu_wrptr(sc, 0, ch->setupreg, 0);
  978: 		val = emu_rd(sc, INTE, 4);
  979: 		val &= ~ch->irqmask;
  980: 		emu_wr(sc, INTE, val, 4);
  981: 		break;
  982: 
  983: 	case PCMTRIG_EMLDMAWR:
  984: 	case PCMTRIG_EMLDMARD:
  985: 	default:
  986: 		break;
  987: 	}
  988: 	snd_mtxunlock(sc->lock);
  989: 
  990: 	return 0;
  991: }
  992: 
  993: static int
  994: emurchan_getptr(kobj_t obj, void *data)
  995: {
  996: 	struct sc_rchinfo *ch = data;
  997: 	struct sc_info *sc = ch->parent;
  998: 	int r;
  999: 
 1000: 	snd_mtxlock(sc->lock);
 1001: 	r = emu_rdptr(sc, 0, ch->idxreg) & 0x0000ffff;
 1002: 	snd_mtxunlock(sc->lock);
 1003: 
 1004: 	return r;
 1005: }
 1006: 
 1007: static struct pcmchan_caps *
 1008: emurchan_getcaps(kobj_t obj, void *data)
 1009: {
 1010: 	struct sc_rchinfo *ch = data;
 1011: 
 1012: 	return &emu_reccaps[ch->num];
 1013: }
 1014: 
 1015: static kobj_method_t emurchan_methods[] = {
 1016:     	KOBJMETHOD(channel_init,		emurchan_init),
 1017:     	KOBJMETHOD(channel_setformat,		emurchan_setformat),
 1018:     	KOBJMETHOD(channel_setspeed,		emurchan_setspeed),
 1019:     	KOBJMETHOD(channel_setblocksize,	emurchan_setblocksize),
 1020:     	KOBJMETHOD(channel_trigger,		emurchan_trigger),
 1021:     	KOBJMETHOD(channel_getptr,		emurchan_getptr),
 1022:     	KOBJMETHOD(channel_getcaps,		emurchan_getcaps),
 1023: 	{ 0, 0 }
 1024: };
 1025: CHANNEL_DECLARE(emurchan);
 1026: 
 1027: /* -------------------------------------------------------------------- */
 1028: /* The interrupt handler */
 1029: static void
 1030: emu_intr(void *p)
 1031: {
 1032: 	struct sc_info *sc = (struct sc_info *)p;
 1033: 	u_int32_t stat, ack, i, x;
 1034: 
 1035: 	while (1) {
 1036: 		stat = emu_rd(sc, IPR, 4);
 1037: 		if (stat == 0)
 1038: 			break;
 1039: 		ack = 0;
 1040: 
 1041: 		/* process irq */
 1042: 		if (stat & IPR_INTERVALTIMER) {
 1043: 			ack |= IPR_INTERVALTIMER;
 1044: 			x = 0;
 1045: 			for (i = 0; i < sc->nchans; i++) {
 1046: 				if (sc->pch[i].run) {
 1047: 					x = 1;
 1048: 					chn_intr(sc->pch[i].channel);
 1049: 				}
 1050: 			}
 1051: 			if (x == 0)
 1052: 				emu_enatimer(sc, 0);
 1053: 		}
 1054: 
 1055: 
 1056: 		if (stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL)) {
 1057: 			ack |= stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL);
 1058: 			if (sc->rch[0].channel)
 1059: 				chn_intr(sc->rch[0].channel);
 1060: 		}
 1061: 		if (stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL)) {
 1062: 			ack |= stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL);
 1063: 			if (sc->rch[1].channel)
 1064: 				chn_intr(sc->rch[1].channel);
 1065: 		}
 1066: 		if (stat & (IPR_MICBUFFULL | IPR_MICBUFHALFFULL)) {
 1067: 			ack |= stat & (IPR_MICBUFFULL | IPR_MICBUFHALFFULL);
 1068: 			if (sc->rch[2].channel)
 1069: 				chn_intr(sc->rch[2].channel);
 1070: 		}
 1071: 		if (stat & IPR_PCIERROR) {
 1072: 			ack |= IPR_PCIERROR;
 1073: 			device_printf(sc->dev, "pci error\n");
 1074: 			/* we still get an nmi with ecc ram even if we ack this */
 1075: 		}
 1076: 		if (stat & IPR_SAMPLERATETRACKER) {
 1077: 			ack |= IPR_SAMPLERATETRACKER;
 1078: 			/* device_printf(sc->dev, "sample rate tracker lock status change\n"); */
 1079: 		}
 1080: 
 1081: 		if (stat & ~ack)
 1082: 			device_printf(sc->dev, "dodgy irq: %x (harmless)\n", stat & ~ack);
 1083: 
 1084: 		emu_wr(sc, IPR, stat, 4);
 1085: 	}
 1086: }
 1087: 
 1088: /* -------------------------------------------------------------------- */
 1089: 
 1090: static void
 1091: emu_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1092: {
 1093: 	void **phys = arg;
 1094: 
 1095: 	*phys = error? 0 : (void *)segs->ds_addr;
 1096: 
 1097: 	if (bootverbose) {
 1098: 		printf("emu: setmap (%lx, %lx), nseg=%d, error=%d\n",
 1099: 		       (unsigned long)segs->ds_addr, (unsigned long)segs->ds_len,
 1100: 		       nseg, error);
 1101: 	}
 1102: }
 1103: 
 1104: static void *
 1105: emu_malloc(struct sc_info *sc, u_int32_t sz)
 1106: {
 1107: 	void *buf, *phys = 0;
 1108: 	bus_dmamap_t map;
 1109: 
 1110: 	if (bus_dmamem_alloc(sc->parent_dmat, &buf, BUS_DMA_NOWAIT, &map))
 1111: 		return NULL;
 1112: 	if (bus_dmamap_load(sc->parent_dmat, map, buf, sz, emu_setmap, &phys, 0)
 1113: 	    || !phys)
 1114: 		return NULL;
 1115: 	return buf;
 1116: }
 1117: 
 1118: static void
 1119: emu_free(struct sc_info *sc, void *buf)
 1120: {
 1121: 	bus_dmamem_free(sc->parent_dmat, buf, NULL);
 1122: }
 1123: 
 1124: static void *
 1125: emu_memalloc(struct sc_info *sc, u_int32_t sz)
 1126: {
 1127: 	u_int32_t blksz, start, idx, ofs, tmp, found;
 1128: 	struct emu_mem *mem = &sc->mem;
 1129: 	struct emu_memblk *blk;
 1130: 	void *buf;
 1131: 
 1132: 	blksz = sz / EMUPAGESIZE;
 1133: 	if (sz > (blksz * EMUPAGESIZE))
 1134: 		blksz++;
 1135: 	/* find a free block in the bitmap */
 1136: 	found = 0;
 1137: 	start = 1;
 1138: 	while (!found && start + blksz < MAXPAGES) {
 1139: 		found = 1;
 1140: 		for (idx = start; idx < start + blksz; idx++)
 1141: 			if (mem->bmap[idx >> 3] & (1 << (idx & 7)))
 1142: 				found = 0;
 1143: 		if (!found)
 1144: 			start++;
 1145: 	}
 1146: 	if (!found)
 1147: 		return NULL;
 1148: 	blk = malloc(sizeof(*blk), M_DEVBUF, M_NOWAIT);
 1149: 	if (blk == NULL)
 1150: 		return NULL;
 1151: 	buf = emu_malloc(sc, sz);
 1152: 	if (buf == NULL) {
 1153: 		free(blk, M_DEVBUF);
 1154: 		return NULL;
 1155: 	}
 1156: 	blk->buf = buf;
 1157: 	blk->pte_start = start;
 1158: 	blk->pte_size = blksz;
 1159: 	/* printf("buf %p, pte_start %d, pte_size %d\n", blk->buf, blk->pte_start, blk->pte_size); */
 1160: 	ofs = 0;
 1161: 	for (idx = start; idx < start + blksz; idx++) {
 1162: 		mem->bmap[idx >> 3] |= 1 << (idx & 7);
 1163: 		tmp = (u_int32_t)vtophys((u_int8_t *)buf + ofs);
 1164: 		/* printf("pte[%d] -> %x phys, %x virt\n", idx, tmp, ((u_int32_t)buf) + ofs); */
 1165: 		mem->ptb_pages[idx] = (tmp << 1) | idx;
 1166: 		ofs += EMUPAGESIZE;
 1167: 	}
 1168: 	SLIST_INSERT_HEAD(&mem->blocks, blk, link);
 1169: 	return buf;
 1170: }
 1171: 
 1172: static int
 1173: emu_memfree(struct sc_info *sc, void *buf)
 1174: {
 1175: 	u_int32_t idx, tmp;
 1176: 	struct emu_mem *mem = &sc->mem;
 1177: 	struct emu_memblk *blk, *i;
 1178: 
 1179: 	blk = NULL;
 1180: 	SLIST_FOREACH(i, &mem->blocks, link) {
 1181: 		if (i->buf == buf)
 1182: 			blk = i;
 1183: 	}
 1184: 	if (blk == NULL)
 1185: 		return EINVAL;
 1186: 	SLIST_REMOVE(&mem->blocks, blk, emu_memblk, link);
 1187: 	emu_free(sc, buf);
 1188: 	tmp = (u_int32_t)vtophys(sc->mem.silent_page) << 1;
 1189: 	for (idx = blk->pte_start; idx < blk->pte_start + blk->pte_size; idx++) {
 1190: 		mem->bmap[idx >> 3] &= ~(1 << (idx & 7));
 1191: 		mem->ptb_pages[idx] = tmp | idx;
 1192: 	}
 1193: 	free(blk, M_DEVBUF);
 1194: 	return 0;
 1195: }
 1196: 
 1197: static int
 1198: emu_memstart(struct sc_info *sc, void *buf)
 1199: {
 1200: 	struct emu_mem *mem = &sc->mem;
 1201: 	struct emu_memblk *blk, *i;
 1202: 
 1203: 	blk = NULL;
 1204: 	SLIST_FOREACH(i, &mem->blocks, link) {
 1205: 		if (i->buf == buf)
 1206: 			blk = i;
 1207: 	}
 1208: 	if (blk == NULL)
 1209: 		return -EINVAL;
 1210: 	return blk->pte_start;
 1211: }
 1212: 
 1213: static void
 1214: emu_addefxop(struct sc_info *sc, int op, int z, int w, int x, int y, u_int32_t *pc)
 1215: {
 1216: 	emu_wrefx(sc, (*pc) * 2, (x << 10) | y);
 1217: 	emu_wrefx(sc, (*pc) * 2 + 1, (op << 20) | (z << 10) | w);
 1218: 	(*pc)++;
 1219: }
 1220: 
 1221: static void
 1222: audigy_addefxop(struct sc_info *sc, int op, int z, int w, int x, int y, u_int32_t *pc)
 1223: {
 1224: 	emu_wrefx(sc, (*pc) * 2, (x << 12) | y);
 1225: 	emu_wrefx(sc, (*pc) * 2 + 1, (op << 24) | (z << 12) | w);
 1226: 	(*pc)++;
 1227: }
 1228: 
 1229: static void
 1230: audigy_initefx(struct sc_info *sc)
 1231: {
 1232: 	int i;
 1233: 	u_int32_t pc = 0;
 1234: 
 1235: 	/* skip 0, 0, -1, 0 - NOPs */
 1236: 	for (i = 0; i < 512; i++)
 1237: 		audigy_addefxop(sc, 0x0f, 0x0c0, 0x0c0, 0x0cf, 0x0c0, &pc);
 1238: 
 1239: 	for (i = 0; i < 512; i++)
 1240: 		emu_wrptr(sc, 0, A_FXGPREGBASE + i, 0x0);
 1241: 
 1242: 	pc = 16;
 1243: 
 1244: 	/* stop fx processor */
 1245: 	emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
 1246: 
 1247: 	/* Audigy 2 (EMU10K2) DSP Registers:
 1248: 	   FX Bus
 1249: 		0x000-0x00f : 16 registers (???)
 1250: 	   Input
 1251: 		0x040/0x041 : AC97 Codec (l/r)
 1252: 		0x042/0x043 : ADC, S/PDIF (l/r)
 1253: 		0x044/0x045 : Optical S/PDIF in (l/r)
 1254: 		0x046/0x047 : ???
 1255: 		0x048/0x049 : Line/Mic 2 (l/r)
 1256: 		0x04a/0x04b : RCA S/PDIF (l/r)
 1257: 		0x04c/0x04d : Aux 2 (l/r)
 1258: 	   Output
 1259: 		0x060/0x061 : Digital Front (l/r)
 1260: 		0x062/0x063 : Digital Center/LFE
 1261: 		0x064/0x065 : AudigyDrive Heaphone (l/r)
 1262: 		0x066/0x067 : Digital Rear (l/r)
 1263: 		0x068/0x069 : Analog Front (l/r)
 1264: 		0x06a/0x06b : Analog Center/LFE
 1265: 		0x06c/0x06d : ???
 1266: 		0x06e/0x06f : Analog Rear (l/r)
 1267: 		0x070/0x071 : AC97 Output (l/r)
 1268: 		0x072/0x073 : ???
 1269: 		0x074/0x075 : ???
 1270: 		0x076/0x077 : ADC Recording Buffer (l/r)
 1271: 	   Constants
 1272: 		0x0c0 - 0x0c4 = 0 - 4
 1273: 		0x0c5 = 0x8, 0x0c6 = 0x10, 0x0c7 = 0x20
 1274: 		0x0c8 = 0x100, 0x0c9 = 0x10000, 0x0ca = 0x80000
 1275: 		0x0cb = 0x10000000, 0x0cc = 0x20000000, 0x0cd = 0x40000000
 1276: 		0x0ce = 0x80000000, 0x0cf = 0x7fffffff, 0x0d0 = 0xffffffff
 1277: 		0x0d1 = 0xfffffffe, 0x0d2 = 0xc0000000, 0x0d3 = 0x41fbbcdc
 1278: 		0x0d4 = 0x5a7ef9db, 0x0d5 = 0x00100000, 0x0dc = 0x00000001 (???)
 1279: 	   Temporary Values
 1280: 		0x0d6 : Accumulator (???)
 1281: 		0x0d7 : Condition Register
 1282: 		0x0d8 : Noise source
 1283: 		0x0d9 : Noise source
 1284: 	   Tank Memory Data Registers
 1285: 		0x200 - 0x2ff
 1286: 	   Tank Memory Address Registers
 1287: 		0x300 - 0x3ff
 1288: 	   General Purpose Registers
 1289: 		0x400 - 0x5ff
 1290: 	 */
 1291: 
 1292: 	/* AC97Output[l/r] = FXBus PCM[l/r] */
 1293: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AC97_L), A_C_00000000,
 1294: 			A_C_00000000, A_FXBUS(A_FXBUS_PCM_LEFT), &pc);
 1295: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AC97_R), A_C_00000000,
 1296: 			A_C_00000000, A_FXBUS(A_FXBUS_PCM_RIGHT), &pc);
 1297: 
 1298: 	/* GPR[0/1] = RCA S/PDIF[l/r] -- Master volume */
 1299: 	audigy_addefxop(sc, iACC3, A_GPR(0), A_C_00000000,
 1300: 			A_C_00000000, A_EXTIN(A_EXTIN_RCA_SPDIF_L), &pc);
 1301: 	audigy_addefxop(sc, iACC3, A_GPR(1), A_C_00000000,
 1302: 			A_C_00000000, A_EXTIN(A_EXTIN_RCA_SPDIF_R), &pc);
 1303: 
 1304: 	/* GPR[2] = GPR[0] (Left) / 2 + GPR[1] (Right) / 2 -- Central volume */
 1305: 	audigy_addefxop(sc, iINTERP, A_GPR(2), A_GPR(1),
 1306: 			A_C_40000000, A_GPR(0), &pc);
 1307: 
 1308: 	/* Headphones[l/r] = GPR[0/1] */
 1309: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_HEADPHONE_L),
 1310: 			A_C_00000000, A_C_00000000, A_GPR(0), &pc);
 1311: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_HEADPHONE_R),
 1312: 			A_C_00000000, A_C_00000000, A_GPR(1), &pc);
 1313: 
 1314: 	/* Analog Front[l/r] = GPR[0/1] */
 1315: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AFRONT_L), A_C_00000000,
 1316: 			A_C_00000000, A_GPR(0), &pc);
 1317: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AFRONT_R), A_C_00000000,
 1318: 			A_C_00000000, A_GPR(1), &pc);
 1319: 
 1320: 	/* Digital Front[l/r] = GPR[0/1] */
 1321: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_FRONT_L), A_C_00000000,
 1322: 			A_C_00000000, A_GPR(0), &pc);
 1323: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_FRONT_R), A_C_00000000,
 1324: 			A_C_00000000, A_GPR(1), &pc);
 1325: 
 1326: 	/* Center and Subwoofer configuration */
 1327: 	/* Analog Center = GPR[0] + GPR[2] */
 1328: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ACENTER), A_C_00000000,
 1329: 			A_GPR(0), A_GPR(2), &pc);
 1330: 	/* Analog Sub = GPR[1] + GPR[2] */
 1331: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ALFE), A_C_00000000,
 1332: 			A_GPR(1), A_GPR(2), &pc);
 1333: 
 1334: 	/* Digital Center = GPR[0] + GPR[2] */
 1335: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_CENTER), A_C_00000000,
 1336: 			A_GPR(0), A_GPR(2), &pc);
 1337: 	/* Digital Sub = GPR[1] + GPR[2] */
 1338: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_LFE), A_C_00000000,
 1339: 			A_GPR(1), A_GPR(2), &pc);
 1340: 
 1341: #if 0
 1342: 	/* Analog Rear[l/r] = (GPR[0/1] * RearVolume[l/r]) >> 31 */
 1343: 	/*   RearVolume = GPR[0x10/0x11] (Will this ever be implemented?) */
 1344: 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_AREAR_L), A_C_00000000,
 1345: 			A_GPR(16), A_GPR(0), &pc);
 1346: 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_AREAR_R), A_C_00000000,
 1347: 			A_GPR(17), A_GPR(1), &pc);
 1348: 
 1349: 	/* Digital Rear[l/r] = (GPR[0/1] * RearVolume[l/r]) >> 31 */
 1350: 	/*   RearVolume = GPR[0x10/0x11] (Will this ever be implemented?) */
 1351: 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_REAR_L), A_C_00000000,
 1352: 			A_GPR(16), A_GPR(0), &pc);
 1353: 	audigy_addefxop(sc, iMAC0, A_EXTOUT(A_EXTOUT_REAR_R), A_C_00000000,
 1354: 			A_GPR(17), A_GPR(1), &pc);
 1355: #else
 1356: 	/* XXX This is just a copy to the channel, since we do not have
 1357: 	 *     a patch manager, it is useful for have another output enabled.
 1358: 	 */
 1359: 
 1360: 	/* Analog Rear[l/r] = GPR[0/1] */
 1361: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AREAR_L), A_C_00000000,
 1362: 			A_C_00000000, A_GPR(0), &pc);
 1363: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_AREAR_R), A_C_00000000,
 1364: 			A_C_00000000, A_GPR(1), &pc);
 1365: 
 1366: 	/* Digital Rear[l/r] = GPR[0/1] */
 1367: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_REAR_L), A_C_00000000,
 1368: 			A_C_00000000, A_GPR(0), &pc);
 1369: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_REAR_R), A_C_00000000,
 1370: 			A_C_00000000, A_GPR(1), &pc);
 1371: #endif
 1372: 
 1373: 	/* ADC Recording buffer[l/r] = AC97Input[l/r] */
 1374: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ADC_CAP_L), A_C_00000000,
 1375: 			A_C_00000000, A_EXTIN(A_EXTIN_AC97_L), &pc);
 1376: 	audigy_addefxop(sc, iACC3, A_EXTOUT(A_EXTOUT_ADC_CAP_R), A_C_00000000,
 1377: 			A_C_00000000, A_EXTIN(A_EXTIN_AC97_R), &pc);
 1378: 
 1379: 	/* resume normal operations */
 1380: 	emu_wrptr(sc, 0, A_DBG, 0);
 1381: }
 1382: 
 1383: static void
 1384: emu_initefx(struct sc_info *sc)
 1385: {
 1386: 	int i;
 1387: 	u_int32_t pc = 16;
 1388: 
 1389: 	/* acc3 0,0,0,0 - NOPs */
 1390: 	for (i = 0; i < 512; i++) {
 1391: 		emu_wrefx(sc, i * 2, 0x10040);
 1392: 		emu_wrefx(sc, i * 2 + 1, 0x610040);
 1393: 	}
 1394: 
 1395: 	for (i = 0; i < 256; i++)
 1396: 		emu_wrptr(sc, 0, FXGPREGBASE + i, 0);
 1397: 
 1398: 	/* FX-8010 DSP Registers:
 1399: 	   FX Bus
 1400: 	     0x000-0x00f : 16 registers
 1401: 	   Input
 1402: 	     0x010/0x011 : AC97 Codec (l/r)
 1403: 	     0x012/0x013 : ADC, S/PDIF (l/r)
 1404: 	     0x014/0x015 : Mic(left), Zoom (l/r)
 1405: 	     0x016/0x017 : TOS link in (l/r)
 1406: 	     0x018/0x019 : Line/Mic 1 (l/r)
 1407: 	     0x01a/0x01b : COAX S/PDIF (l/r)
 1408: 	     0x01c/0x01d : Line/Mic 2 (l/r)
 1409: 	   Output
 1410: 	     0x020/0x021 : AC97 Output (l/r)
 1411: 	     0x022/0x023 : TOS link out (l/r)
 1412: 	     0x024/0x025 : Center/LFE
 1413: 	     0x026/0x027 : LiveDrive Headphone (l/r)
 1414: 	     0x028/0x029 : Rear Channel (l/r)
 1415: 	     0x02a/0x02b : ADC Recording Buffer (l/r)
 1416: 	     0x02c       : Mic Recording Buffer
 1417: 	     0x031/0x032 : Analog Center/LFE
 1418: 	   Constants
 1419: 	     0x040 - 0x044 = 0 - 4
 1420: 	     0x045 = 0x8, 0x046 = 0x10, 0x047 = 0x20
 1421: 	     0x048 = 0x100, 0x049 = 0x10000, 0x04a = 0x80000
 1422: 	     0x04b = 0x10000000, 0x04c = 0x20000000, 0x04d = 0x40000000
 1423: 	     0x04e = 0x80000000, 0x04f = 0x7fffffff, 0x050 = 0xffffffff
 1424: 	     0x051 = 0xfffffffe, 0x052 = 0xc0000000, 0x053 = 0x41fbbcdc
 1425: 	     0x054 = 0x5a7ef9db, 0x055 = 0x00100000
 1426: 	   Temporary Values
 1427: 	     0x056 : Accumulator
 1428: 	     0x057 : Condition Register
 1429: 	     0x058 : Noise source
 1430: 	     0x059 : Noise source
 1431: 	     0x05a : IRQ Register
 1432: 	     0x05b : TRAM Delay Base Address Count
 1433: 	   General Purpose Registers
 1434: 	     0x100 - 0x1ff
 1435: 	   Tank Memory Data Registers
 1436: 	     0x200 - 0x2ff
 1437: 	   Tank Memory Address Registers
 1438: 	     0x300 - 0x3ff
 1439: 	 */
 1440: 
 1441: 	/* Routing - this will be configurable in later version */
 1442: 
 1443: 	/* GPR[0/1] = FX * 4 + SPDIF-in */
 1444: 	emu_addefxop(sc, iMACINT0, GPR(0), EXTIN(EXTIN_SPDIF_CD_L),
 1445: 			FXBUS(FXBUS_PCM_LEFT), C_00000004, &pc);
 1446: 	emu_addefxop(sc, iMACINT0, GPR(1), EXTIN(EXTIN_SPDIF_CD_R),
 1447: 			FXBUS(FXBUS_PCM_RIGHT), C_00000004, &pc);
 1448: 
 1449: 	/* GPR[0/1] += APS-input */
 1450: 	emu_addefxop(sc, iACC3, GPR(0), GPR(0), C_00000000,
 1451: 			sc->APS ? EXTIN(EXTIN_TOSLINK_L) : C_00000000, &pc);
 1452: 	emu_addefxop(sc, iACC3, GPR(1), GPR(1), C_00000000,
 1453: 			sc->APS ? EXTIN(EXTIN_TOSLINK_R) : C_00000000, &pc);
 1454: 
 1455: 	/* FrontOut (AC97) = GPR[0/1] */
 1456: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_L), C_00000000,
 1457: 			C_00000000, GPR(0), &pc);
 1458: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_AC97_R), C_00000000,
 1459: 			C_00000001, GPR(1), &pc);
 1460: 
 1461: 	/* GPR[2] = GPR[0] (Left) / 2 + GPR[1] (Right) / 2 -- Central volume */
 1462: 	emu_addefxop(sc, iINTERP, GPR(2), GPR(1), C_40000000, GPR(0), &pc);
 1463: 
 1464: #if 0
 1465: 	/* RearOut = (GPR[0/1] * RearVolume) >> 31 */
 1466: 	/*   RearVolume = GPR[0x10/0x11] */
 1467: 	emu_addefxop(sc, iMAC0, EXTOUT(EXTOUT_REAR_L), C_00000000,
 1468: 			GPR(16), GPR(0), &pc);
 1469: 	emu_addefxop(sc, iMAC0, EXTOUT(EXTOUT_REAR_R), C_00000000,
 1470: 			GPR(17), GPR(1), &pc);
 1471: #else
 1472: 	/* XXX This is just a copy to the channel, since we do not have
 1473: 	 *     a patch manager, it is useful for have another output enabled.
 1474: 	 */
 1475: 
 1476: 	/* Rear[l/r] = GPR[0/1] */
 1477: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_REAR_L), C_00000000,
 1478: 			C_00000000, GPR(0), &pc);
 1479: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_REAR_R), C_00000000,
 1480: 			C_00000000, GPR(1), &pc);
 1481: #endif
 1482: 
 1483: 	/* TOS out[l/r] = GPR[0/1] */
 1484: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_TOSLINK_L), C_00000000,
 1485: 			C_00000000, GPR(0), &pc);
 1486: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_TOSLINK_R), C_00000000,
 1487: 			C_00000000, GPR(1), &pc);
 1488: 
 1489: 	/* Center and Subwoofer configuration */
 1490: 	/* Analog Center = GPR[0] + GPR[2] */
 1491: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ACENTER), C_00000000,
 1492: 			GPR(0), GPR(2), &pc);
 1493: 	/* Analog Sub = GPR[1] + GPR[2] */
 1494: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ALFE), C_00000000,
 1495: 			GPR(1), GPR(2), &pc);
 1496: 	/* Digital Center = GPR[0] + GPR[2] */
 1497: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_CENTER), C_00000000,
 1498: 			GPR(0), GPR(2), &pc);
 1499: 	/* Digital Sub = GPR[1] + GPR[2] */
 1500: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_LFE), C_00000000,
 1501: 			GPR(1), GPR(2), &pc);
 1502: 
 1503: 	/* Headphones[l/r] = GPR[0/1] */
 1504: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_HEADPHONE_L), C_00000000,
 1505: 			C_00000000, GPR(0), &pc);
 1506: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_HEADPHONE_R), C_00000000,
 1507: 			C_00000000, GPR(1), &pc);
 1508: 
 1509: 	/* ADC Recording buffer[l/r] = AC97Input[l/r] */
 1510: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ADC_CAP_L), C_00000000,
 1511: 			C_00000000, EXTIN(EXTIN_AC97_L), &pc);
 1512: 	emu_addefxop(sc, iACC3, EXTOUT(EXTOUT_ADC_CAP_R), C_00000000,
 1513: 			C_00000000, EXTIN(EXTIN_AC97_R), &pc);
 1514: 
 1515: 	/* resume normal operations */
 1516: 	emu_wrptr(sc, 0, DBG, 0);
 1517: }
 1518: 
 1519: /* Probe and attach the card */
 1520: static int
 1521: emu_init(struct sc_info *sc)
 1522: {
 1523: 	u_int32_t spcs, ch, tmp, i;
 1524: 
 1525: 	/* enable additional AC97 slots */
 1526: 	emu_wrptr(sc, 0, AC97SLOT, AC97SLOT_CNTR | AC97SLOT_LFE);
 1527: 
 1528:    	/* disable audio and lock cache */
 1529: 	emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE | HCFG_MUTEBUTTONENABLE, 4);
 1530: 
 1531: 	/* reset recording buffers */
 1532: 	emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
 1533: 	emu_wrptr(sc, 0, MICBA, 0);
 1534: 	emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
 1535: 	emu_wrptr(sc, 0, FXBA, 0);
 1536: 	emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
 1537: 	emu_wrptr(sc, 0, ADCBA, 0);
 1538: 
 1539: 	/* disable channel interrupt */
 1540: 	emu_wr(sc, INTE, INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE, 4);
 1541: 	emu_wrptr(sc, 0, CLIEL, 0);
 1542: 	emu_wrptr(sc, 0, CLIEH, 0);
 1543: 	emu_wrptr(sc, 0, SOLEL, 0);
 1544: 	emu_wrptr(sc, 0, SOLEH, 0);
 1545: 
 1546: 	/* wonder what these do... */
 1547: 	if (sc->audigy) {
 1548: 		emu_wrptr(sc, 0, SPBYPASS, 0xf00);
 1549: 		emu_wrptr(sc, 0, AC97SLOT, 0x3);
 1550: 	}
 1551: 
 1552: 	/* init envelope engine */
 1553: 	for (ch = 0; ch < NUM_G; ch++) {
 1554: 		emu_wrptr(sc, ch, DCYSUSV, ENV_OFF);
 1555: 		emu_wrptr(sc, ch, IP, 0);
 1556: 		emu_wrptr(sc, ch, VTFT, 0xffff);
 1557: 		emu_wrptr(sc, ch, CVCF, 0xffff);
 1558: 		emu_wrptr(sc, ch, PTRX, 0);
 1559: 		emu_wrptr(sc, ch, CPF, 0);
 1560: 		emu_wrptr(sc, ch, CCR, 0);
 1561: 
 1562: 		emu_wrptr(sc, ch, PSST, 0);
 1563: 		emu_wrptr(sc, ch, DSL, 0x10);
 1564: 		emu_wrptr(sc, ch, CCCA, 0);
 1565: 		emu_wrptr(sc, ch, Z1, 0);
 1566: 		emu_wrptr(sc, ch, Z2, 0);
 1567: 		emu_wrptr(sc, ch, FXRT, 0xd01c0000);
 1568: 
 1569: 		emu_wrptr(sc, ch, ATKHLDM, 0);
 1570: 		emu_wrptr(sc, ch, DCYSUSM, 0);
 1571: 		emu_wrptr(sc, ch, IFATN, 0xffff);
 1572: 		emu_wrptr(sc, ch, PEFE, 0);
 1573: 		emu_wrptr(sc, ch, FMMOD, 0);
 1574: 		emu_wrptr(sc, ch, TREMFRQ, 24);	/* 1 Hz */
 1575: 		emu_wrptr(sc, ch, FM2FRQ2, 24);	/* 1 Hz */
 1576: 		emu_wrptr(sc, ch, TEMPENV, 0);
 1577: 
 1578: 		/*** these are last so OFF prevents writing ***/
 1579: 		emu_wrptr(sc, ch, LFOVAL2, 0);
 1580: 		emu_wrptr(sc, ch, LFOVAL1, 0);
 1581: 		emu_wrptr(sc, ch, ATKHLDV, 0);
 1582: 		emu_wrptr(sc, ch, ENVVOL, 0);
 1583: 		emu_wrptr(sc, ch, ENVVAL, 0);
 1584: 
 1585: 		if (sc->audigy) {
 1586: 			/* audigy cards need this to initialize correctly */
 1587: 			emu_wrptr(sc, ch, 0x4c, 0);
 1588: 			emu_wrptr(sc, ch, 0x4d, 0);
 1589: 			emu_wrptr(sc, ch, 0x4e, 0);
 1590: 			emu_wrptr(sc, ch, 0x4f, 0);
 1591: 			/* set default routing */
 1592: 			emu_wrptr(sc, ch, A_FXRT1, 0x03020100);
 1593: 			emu_wrptr(sc, ch, A_FXRT2, 0x3f3f3f3f);
 1594: 			emu_wrptr(sc, ch, A_SENDAMOUNTS, 0);
 1595: 		}
 1596: 
 1597: 		sc->voice[ch].vnum = ch;
 1598: 		sc->voice[ch].slave = NULL;
 1599: 		sc->voice[ch].busy = 0;
 1600: 		sc->voice[ch].ismaster = 0;
 1601: 		sc->voice[ch].running = 0;
 1602: 		sc->voice[ch].b16 = 0;
 1603: 		sc->voice[ch].stereo = 0;
 1604: 		sc->voice[ch].speed = 0;
 1605: 		sc->voice[ch].start = 0;
 1606: 		sc->voice[ch].end = 0;
 1607: 		sc->voice[ch].channel = NULL;
 1608:        }
 1609:        sc->pnum = sc->rnum = 0;
 1610: 
 1611: 	/*
 1612: 	 *  Init to 0x02109204 :
 1613: 	 *  Clock accuracy    = 0     (1000ppm)
 1614: 	 *  Sample Rate       = 2     (48kHz)
 1615: 	 *  Audio Channel     = 1     (Left of 2)
 1616: 	 *  Source Number     = 0     (Unspecified)
 1617: 	 *  Generation Status = 1     (Original for Cat Code 12)
 1618: 	 *  Cat Code          = 12    (Digital Signal Mixer)
 1619: 	 *  Mode              = 0     (Mode 0)
 1620: 	 *  Emphasis          = 0     (None)
 1621: 	 *  CP                = 1     (Copyright unasserted)
 1622: 	 *  AN                = 0     (Audio data)
 1623: 	 *  P                 = 0     (Consumer)
 1624: 	 */
 1625: 	spcs = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
 1626: 	       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
 1627: 	       SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
 1628: 	       SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
 1629: 	emu_wrptr(sc, 0, SPCS0, spcs);
 1630: 	emu_wrptr(sc, 0, SPCS1, spcs);
 1631: 	emu_wrptr(sc, 0, SPCS2, spcs);
 1632: 
 1633: 	if (!sc->audigy)
 1634: 		emu_initefx(sc);
 1635: 	else if (sc->audigy2) { /* Audigy 2 */
 1636: 		/* from ALSA initialization code: */
 1637: 
 1638: 		/* Hack for Alice3 to work independent of haP16V driver */
 1639: 		u_int32_t tmp;
 1640: 
 1641: 		/* Setup SRCMulti_I2S SamplingRate */
 1642: 		tmp = emu_rdptr(sc, 0, A_SPDIF_SAMPLERATE) & 0xfffff1ff;
 1643: 		emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, tmp | 0x400);
 1644: 
 1645: 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
 1646: 		emu_wr(sc, 0x20, 0x00600000, 4);
 1647: 		emu_wr(sc, 0x24, 0x00000014, 4);
 1648: 
 1649: 		/* Setup SRCMulti Input Audio Enable */
 1650: 		emu_wr(sc, 0x20, 0x006e0000, 4);
 1651: 		emu_wr(sc, 0x24, 0xff00ff00, 4);
 1652: 	}
 1653: 
 1654: 	SLIST_INIT(&sc->mem.blocks);
 1655: 	sc->mem.ptb_pages = emu_malloc(sc, MAXPAGES * sizeof(u_int32_t));
 1656: 	if (sc->mem.ptb_pages == NULL)
 1657: 		return -1;
 1658: 
 1659: 	sc->mem.silent_page = emu_malloc(sc, EMUPAGESIZE);
 1660: 	if (sc->mem.silent_page == NULL) {
 1661: 		emu_free(sc, sc->mem.ptb_pages);
 1662: 		return -1;
 1663: 	}
 1664: 	/* Clear page with silence & setup all pointers to this page */
 1665: 	bzero(sc->mem.silent_page, EMUPAGESIZE);
 1666: 	tmp = (u_int32_t)vtophys(sc->mem.silent_page) << 1;
 1667: 	for (i = 0; i < MAXPAGES; i++)
 1668: 		sc->mem.ptb_pages[i] = tmp | i;
 1669: 
 1670: 	emu_wrptr(sc, 0, PTB, vtophys(sc->mem.ptb_pages));
 1671: 	emu_wrptr(sc, 0, TCB, 0);	/* taken from original driver */
 1672: 	emu_wrptr(sc, 0, TCBS, 0);	/* taken from original driver */
 1673: 
 1674: 	for (ch = 0; ch < NUM_G; ch++) {
 1675: 		emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK);
 1676: 		emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK);
 1677: 	}
 1678: 
 1679: 	/* emu_memalloc(sc, EMUPAGESIZE); */
 1680: 	/*
 1681: 	 *  Hokay, now enable the AUD bit
 1682: 	 *
 1683: 	 *  Audigy
 1684: 	 *   Enable Audio = 0 (enabled after fx processor initialization)
 1685: 	 *   Mute Disable Audio = 0
 1686: 	 *   Joystick = 1
 1687: 	 *
 1688: 	 *  Audigy 2
 1689: 	 *   Enable Audio = 1
 1690: 	 *   Mute Disable Audio = 0
 1691: 	 *   Joystick = 1
 1692: 	 *   GP S/PDIF AC3 Enable = 1
 1693: 	 *   CD S/PDIF AC3 Enable = 1
 1694: 	 *
 1695: 	 *  EMU10K1
 1696: 	 *   Enable Audio = 1
 1697: 	 *   Mute Disable Audio = 0
 1698: 	 *   Lock Tank Memory = 1
 1699: 	 *   Lock Sound Memory = 0
 1700: 	 *   Auto Mute = 1
 1701: 	 */
 1702: 	if (sc->audigy) {
 1703: 		tmp = HCFG_AUTOMUTE | HCFG_JOYENABLE;
 1704: 		if (sc->audigy2) /* Audigy 2 */
 1705: 			tmp = HCFG_AUDIOENABLE | HCFG_AC3ENABLE_CDSPDIF |
 1706: 				HCFG_AC3ENABLE_GPSPDIF;
 1707: 		emu_wr(sc, HCFG, tmp, 4);
 1708: 
 1709: 		audigy_initefx(sc);
 1710: 
 1711: 		/* from ALSA initialization code: */
 1712: 
 1713: 		/* enable audio and disable both audio/digital outputs */
 1714: 		emu_wr(sc, HCFG, emu_rd(sc, HCFG, 4) | HCFG_AUDIOENABLE, 4);
 1715: 		emu_wr(sc, A_IOCFG, emu_rd(sc, A_IOCFG, 4) & ~A_IOCFG_GPOUT_AD, 4);
 1716: 		if (sc->audigy2) { /* Audigy 2 */
 1717: 			/* Unmute Analog.  Set GPO6 to 1 for Apollo.
 1718: 			 * This has to be done after init Alice3 I2SOut beyond 48kHz.
 1719: 			 * So, sequence is important.
 1720: 			 */
 1721: 			emu_wr(sc, A_IOCFG, emu_rd(sc, A_IOCFG, 4) | A_IOCFG_GPOUT_A, 4);
 1722: 		}
 1723: 	} else {
 1724: 		/* EMU10K1 initialization code */
 1725: 		tmp = HCFG_AUDIOENABLE | HCFG_AUTOMUTE | HCFG_LOCKTANKCACHE;
 1726: 		if (sc->rev >= 6)
 1727: 			tmp |= HCFG_JOYENABLE;
 1728: 
 1729: 		emu_wr(sc, HCFG, tmp, 4);
 1730: 
 1731: 		/* TOSLink detection */
 1732: 		sc->tos_link = 0;
 1733: 		tmp = emu_rd(sc, HCFG, 4);
 1734: 		if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
 1735: 			emu_wr(sc, HCFG, tmp | HCFG_GPOUT1, 4);
 1736: 			DELAY(50);
 1737: 			if (tmp != (emu_rd(sc, HCFG, 4) & ~HCFG_GPOUT1)) {
 1738: 				sc->tos_link = 1;
 1739: 				emu_wr(sc, HCFG, tmp, 4);
 1740: 			}
 1741: 		}
 1742: 	}
 1743: 
 1744: 	return 0;
 1745: }
 1746: 
 1747: static int
 1748: emu_uninit(struct sc_info *sc)
 1749: {
 1750: 	u_int32_t ch;
 1751: 
 1752: 	emu_wr(sc, INTE, 0, 4);
 1753: 	for (ch = 0; ch < NUM_G; ch++)
 1754: 		emu_wrptr(sc, ch, DCYSUSV, ENV_OFF);
 1755: 	for (ch = 0; ch < NUM_G; ch++) {
 1756: 		emu_wrptr(sc, ch, VTFT, 0);
 1757: 		emu_wrptr(sc, ch, CVCF, 0);
 1758: 		emu_wrptr(sc, ch, PTRX, 0);
 1759: 		emu_wrptr(sc, ch, CPF, 0);
 1760: 	}
 1761: 
 1762: 	if (sc->audigy) {
 1763: 		/* stop fx processor */
 1764: 		emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
 1765: 	}
 1766: 
 1767: 	/* disable audio and lock cache */
 1768: 	emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE | HCFG_MUTEBUTTONENABLE, 4);
 1769: 
 1770: 	emu_wrptr(sc, 0, PTB, 0);
 1771: 	/* reset recording buffers */
 1772: 	emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
 1773: 	emu_wrptr(sc, 0, MICBA, 0);
 1774: 	emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
 1775: 	emu_wrptr(sc, 0, FXBA, 0);
 1776: 	emu_wrptr(sc, 0, FXWC, 0);
 1777: 	emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
 1778: 	emu_wrptr(sc, 0, ADCBA, 0);
 1779: 	emu_wrptr(sc, 0, TCB, 0);
 1780: 	emu_wrptr(sc, 0, TCBS, 0);
 1781: 
 1782: 	/* disable channel interrupt */
 1783: 	emu_wrptr(sc, 0, CLIEL, 0);
 1784: 	emu_wrptr(sc, 0, CLIEH, 0);
 1785: 	emu_wrptr(sc, 0, SOLEL, 0);
 1786: 	emu_wrptr(sc, 0, SOLEH, 0);
 1787: 
 1788: 
 1789: 	/* init envelope engine */
 1790: 	if (!SLIST_EMPTY(&sc->mem.blocks))
 1791: 		device_printf(sc->dev, "warning: memblock list not empty\n");
 1792: 	emu_free(sc, sc->mem.ptb_pages);
 1793: 	emu_free(sc, sc->mem.silent_page);
 1794: 
 1795: 	return 0;
 1796: }
 1797: 
 1798: static int
 1799: emu_pci_probe(device_t dev)
 1800: {
 1801: 	char *s = NULL;
 1802: 
 1803: 	switch (pci_get_devid(dev)) {
 1804: 	case EMU10K1_PCI_ID:
 1805: 		s = "Creative EMU10K1";
 1806: 		break;
 1807: 
 1808: 	case EMU10K2_PCI_ID:
 1809: 		if (pci_get_revid(dev) == 0x04)
 1810: 			s = "Creative Audigy 2 (EMU10K2)";
 1811: 		else
 1812: 			s = "Creative Audigy (EMU10K2)";
 1813: 		break;
 1814: 
 1815: 	default:
 1816: 		return ENXIO;
 1817: 	}
 1818: 
 1819: 	device_set_desc(dev, s);
 1820: 	return 0;
 1821: }
 1822: 
 1823: static int
 1824: emu_pci_attach(device_t dev)
 1825: {
 1826: 	struct ac97_info *codec = NULL;
 1827: 	struct sc_info *sc;
 1828: 	u_int32_t data;
 1829: 	int i, gotmic;
 1830: 	char status[SND_STATUSLEN];
 1831: 
 1832: 	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) {
 1833: 		device_printf(dev, "cannot allocate softc\n");
 1834: 		return ENXIO;
 1835: 	}
 1836: 
 1837: 	sc->lock = snd_mtxcreate(device_get_nameunit(dev), "sound softc");
 1838: 	sc->dev = dev;
 1839: 	sc->type = pci_get_devid(dev);
 1840: 	sc->rev = pci_get_revid(dev);
 1841: 	sc->audigy = (sc->type == EMU10K2_PCI_ID);
 1842: 	sc->audigy2 = (sc->audigy && sc->rev == 0x04);
 1843: 	sc->nchans = sc->audigy ? 8 : 4;
 1844: 	sc->addrmask = sc->audigy ? A_PTR_ADDRESS_MASK : PTR_ADDRESS_MASK;
 1845: 
 1846: 	data = pci_read_config(dev, PCIR_COMMAND, 2);
 1847: 	data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
 1848: 	pci_write_config(dev, PCIR_COMMAND, data, 2);
 1849: 	data = pci_read_config(dev, PCIR_COMMAND, 2);
 1850: 
 1851: 	i = PCIR_MAPS;
 1852: 	sc->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &i, 0, ~0, 1, RF_ACTIVE);
 1853: 	if (sc->reg == NULL) {
 1854: 		device_printf(dev, "unable to map register space\n");
 1855: 		goto bad;
 1856: 	}
 1857: 	sc->st = rman_get_bustag(sc->reg);
 1858: 	sc->sh = rman_get_bushandle(sc->reg);
 1859: 
 1860: 	sc->bufsz = pcm_getbuffersize(dev, 4096, EMU_DEFAULT_BUFSZ, 65536);
 1861: 
 1862: 	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
 1863: 		/*lowaddr*/1 << 31, /* can only access 0-2gb */
 1864: 		/*highaddr*/BUS_SPACE_MAXADDR,
 1865: 		/*filter*/NULL, /*filterarg*/NULL,
 1866: 		/*maxsize*/sc->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
 1867: 		/*flags*/0, &sc->parent_dmat) != 0) {
 1868: 		device_printf(dev, "unable to create dma tag\n");
 1869: 		goto bad;
 1870: 	}
 1871: 
 1872: 	if (emu_init(sc) == -1) {
 1873: 		device_printf(dev, "unable to initialize the card\n");
 1874: 		goto bad;
 1875: 	}
 1876: 
 1877: 	codec = AC97_CREATE(dev, sc, emu_ac97);
 1878: 	if (codec == NULL) goto bad;
 1879: 	gotmic = (ac97_getcaps(codec) & AC97_CAP_MICCHANNEL)? 1 : 0;
 1880: 	if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto bad;
 1881: 
 1882: 	i = 0;
 1883: 	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &i, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
 1884: 	if (!sc->irq || snd_setup_intr(dev, sc->irq, INTR_MPSAFE, emu_intr, sc, &sc->ih)) {
 1885: 		device_printf(dev, "unable to map interrupt\n");
 1886: 		goto bad;
 1887: 	}
 1888: 
 1889: 	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld", rman_get_start(sc->reg), rman_get_start(sc->irq));
 1890: 
 1891: 	if (pcm_register(dev, sc, sc->nchans, gotmic? 3 : 2)) goto bad;
 1892: 	for (i = 0; i < sc->nchans; i++)
 1893: 		pcm_addchan(dev, PCMDIR_PLAY, &emupchan_class, sc);
 1894: 	for (i = 0; i < (gotmic? 3 : 2); i++)
 1895: 		pcm_addchan(dev, PCMDIR_REC, &emurchan_class, sc);
 1896: 
 1897: 	pcm_setstatus(dev, status);
 1898: 
 1899: 	return 0;
 1900: 
 1901: bad:
 1902: 	if (codec) ac97_destroy(codec);
 1903: 	if (sc->reg) bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS, sc->reg);
 1904: 	if (sc->ih) bus_teardown_intr(dev, sc->irq, sc->ih);
 1905: 	if (sc->irq) bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
 1906: 	if (sc->parent_dmat) bus_dma_tag_destroy(sc->parent_dmat);
 1907: 	if (sc->lock) snd_mtxfree(sc->lock);
 1908: 	free(sc, M_DEVBUF);
 1909: 	return ENXIO;
 1910: }
 1911: 
 1912: static int
 1913: emu_pci_detach(device_t dev)
 1914: {
 1915: 	int r;
 1916: 	struct sc_info *sc;
 1917: 
 1918: 	r = pcm_unregister(dev);
 1919: 	if (r)
 1920: 		return r;
 1921: 
 1922: 	sc = pcm_getdevinfo(dev);
 1923: 	/* shutdown chip */
 1924: 	emu_uninit(sc);
 1925: 
 1926: 	bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS, sc->reg);
 1927: 	bus_teardown_intr(dev, sc->irq, sc->ih);
 1928: 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
 1929: 	bus_dma_tag_destroy(sc->parent_dmat);
 1930: 	snd_mtxfree(sc->lock);
 1931: 	free(sc, M_DEVBUF);
 1932: 
 1933: 	return 0;
 1934: }
 1935: 
 1936: /* add suspend, resume */
 1937: static device_method_t emu_methods[] = {
 1938: 	/* Device interface */
 1939: 	DEVMETHOD(device_probe,		emu_pci_probe),
 1940: 	DEVMETHOD(device_attach,	emu_pci_attach),
 1941: 	DEVMETHOD(device_detach,	emu_pci_detach),
 1942: 
 1943: 	{ 0, 0 }
 1944: };
 1945: 
 1946: static driver_t emu_driver = {
 1947: 	"pcm",
 1948: 	emu_methods,
 1949: 	PCM_SOFTC_SIZE,
 1950: };
 1951: 
 1952: DRIVER_MODULE(snd_emu10k1, pci, emu_driver, pcm_devclass, 0, 0);
 1953: MODULE_DEPEND(snd_emu10k1, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
 1954: MODULE_VERSION(snd_emu10k1, 1);
 1955: 
 1956: /* dummy driver to silence the joystick device */
 1957: static int
 1958: emujoy_pci_probe(device_t dev)
 1959: {
 1960: 	char *s = NULL;
 1961: 
 1962: 	switch (pci_get_devid(dev)) {
 1963: 	case 0x70021102:
 1964: 		s = "Creative EMU10K1 Joystick";
 1965: 		device_quiet(dev);
 1966: 		break;
 1967: 	case 0x70031102:
 1968: 		s = "Creative EMU10K2 Joystick";
 1969: 		device_quiet(dev);
 1970: 		break;
 1971: 	}
 1972: 
 1973: 	if (s) device_set_desc(dev, s);
 1974: 	return s? -1000 : ENXIO;
 1975: }
 1976: 
 1977: static int
 1978: emujoy_pci_attach(device_t dev)
 1979: {
 1980: 	return 0;
 1981: }
 1982: 
 1983: static int
 1984: emujoy_pci_detach(device_t dev)
 1985: {
 1986: 	return 0;
 1987: }
 1988: 
 1989: static device_method_t emujoy_methods[] = {
 1990: 	DEVMETHOD(device_probe,		emujoy_pci_probe),
 1991: 	DEVMETHOD(device_attach,	emujoy_pci_attach),
 1992: 	DEVMETHOD(device_detach,	emujoy_pci_detach),
 1993: 
 1994: 	{ 0, 0 }
 1995: };
 1996: 
 1997: static driver_t emujoy_driver = {
 1998: 	"emujoy",
 1999: 	emujoy_methods,
 2000: 	8,
 2001: };
 2002: 
 2003: static devclass_t emujoy_devclass;
 2004: 
 2005: DRIVER_MODULE(emujoy, pci, emujoy_driver, emujoy_devclass, 0, 0);
 2006: