Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 31 Jul 2007 10:09:12 GMT
From:      Christopher Davis <loafier@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 124412 for review
Message-ID:  <200707311009.l6VA9C4C007942@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=124412

Change 124412 by loafier@chrisdsoc on 2007/07/31 10:08:56

	Edit for bus_alloc_resources(), etc.

Affected files ...

.. //depot/projects/soc2007/loafier_busalloc/src/sys/dev/sound/pci/emu10k1.c#2 edit

Differences ...

==== //depot/projects/soc2007/loafier_busalloc/src/sys/dev/sound/pci/emu10k1.c#2 (text+ko) ====

@@ -116,6 +116,18 @@
 	struct sc_info *parent;
 };
 
+enum {
+	RES_MEM,
+	RES_IRQ,
+	RES_SZ
+};
+
+static struct resource_spec emu_res_spec[] = {
+	{SYS_RES_IOPORT, PCIR_BAR(0), RF_ACTIVE},
+	{SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
+	{-1, 0, 0}
+};
+
 /* device private data */
 struct sc_info {
 	device_t	dev;
@@ -123,11 +135,9 @@
 	u_int32_t	tos_link:1, APS:1, audigy:1, audigy2:1;
 	u_int32_t	addrmask;	/* wider if audigy */
 
-	bus_space_tag_t st;
-	bus_space_handle_t sh;
 	bus_dma_tag_t parent_dmat;
 
-	struct resource *reg, *irq;
+	struct resource *res[RES_SZ];
 	void		*ih;
 	struct mtx	*lock;
 
@@ -162,8 +172,12 @@
 #endif
 
 /* talk to the card */
-static u_int32_t emu_rd(struct sc_info *, int, int);
-static void emu_wr(struct sc_info *, int, u_int32_t, int);
+#define emu_rd1(_sc, _reg) bus_read_1((_sc)->res[RES_MEM], _reg)
+#define emu_rd2(_sc, _reg) bus_read_2((_sc)->res[RES_MEM], _reg)
+#define emu_rd4(_sc, _reg) bus_read_4((_sc)->res[RES_MEM], _reg)
+#define emu_wr1(_sc, _reg, _val) bus_write_1((_sc)->res[RES_MEM], _reg, _val)
+#define emu_wr2(_sc, _reg, _val) bus_write_2((_sc)->res[RES_MEM], _reg, _val)
+#define emu_wr4(_sc, _reg, _val) bus_write_4((_sc)->res[RES_MEM], _reg, _val)
 
 /* -------------------------------------------------------------------- */
 
@@ -207,45 +221,15 @@
 
 /* -------------------------------------------------------------------- */
 /* Hardware */
-static u_int32_t
-emu_rd(struct sc_info *sc, int regno, int size)
-{
-	switch (size) {
-	case 1:
-		return bus_space_read_1(sc->st, sc->sh, regno);
-	case 2:
-		return bus_space_read_2(sc->st, sc->sh, regno);
-	case 4:
-		return bus_space_read_4(sc->st, sc->sh, regno);
-	default:
-		return 0xffffffff;
-	}
-}
 
-static void
-emu_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
-{
-	switch (size) {
-	case 1:
-		bus_space_write_1(sc->st, sc->sh, regno, data);
-		break;
-	case 2:
-		bus_space_write_2(sc->st, sc->sh, regno, data);
-		break;
-	case 4:
-		bus_space_write_4(sc->st, sc->sh, regno, data);
-		break;
-	}
-}
-
 static u_int32_t
 emu_rdptr(struct sc_info *sc, int chn, int reg)
 {
 	u_int32_t ptr, val, mask, size, offset;
 
 	ptr = ((reg << 16) & sc->addrmask) | (chn & PTR_CHANNELNUM_MASK);
-	emu_wr(sc, PTR, ptr, 4);
-	val = emu_rd(sc, DATA, 4);
+	emu_wr4(sc, PTR, ptr);
+	val = emu_rd4(sc, DATA);
 	if (reg & 0xff000000) {
 		size = (reg >> 24) & 0x3f;
 		offset = (reg >> 16) & 0x1f;
@@ -262,16 +246,16 @@
 	u_int32_t ptr, mask, size, offset;
 
 	ptr = ((reg << 16) & sc->addrmask) | (chn & PTR_CHANNELNUM_MASK);
-	emu_wr(sc, PTR, ptr, 4);
+	emu_wr4(sc, PTR, ptr);
 	if (reg & 0xff000000) {
 		size = (reg >> 24) & 0x3f;
 		offset = (reg >> 16) & 0x1f;
 		mask = ((1 << size) - 1) << offset;
 		data <<= offset;
 		data &= mask;
-		data |= emu_rd(sc, DATA, 4) & ~mask;
+		data |= emu_rd4(sc, DATA) & ~mask;
 	}
-	emu_wr(sc, DATA, data, 4);
+	emu_wr4(sc, DATA, data);
 }
 
 static void
@@ -290,8 +274,8 @@
 {
 	struct sc_info *sc = (struct sc_info *)devinfo;
 
-	emu_wr(sc, AC97ADDRESS, regno, 1);
-	return emu_rd(sc, AC97DATA, 2);
+	emu_wr1(sc, AC97ADDRESS, regno);
+	return emu_rd2(sc, AC97DATA);
 }
 
 static int
@@ -299,8 +283,8 @@
 {
 	struct sc_info *sc = (struct sc_info *)devinfo;
 
-	emu_wr(sc, AC97ADDRESS, regno, 1);
-	emu_wr(sc, AC97DATA, data, 2);
+	emu_wr1(sc, AC97ADDRESS, regno);
+	emu_wr2(sc, AC97DATA, data);
 	return 0;
 }
 
@@ -342,7 +326,7 @@
 	}
 	RANGE(rate, 48, 9600);
 	sc->timerinterval = 48000 / rate;
-	emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
+	emu_wr2(sc, TIMER, sc->timerinterval & 0x03ff);
 
 	return sc->timerinterval;
 }
@@ -353,15 +337,15 @@
 	u_int32_t x;
 	if (go) {
 		if (sc->timer++ == 0) {
-			x = emu_rd(sc, INTE, 4);
+			x = emu_rd4(sc, INTE);
 			x |= INTE_INTERVALTIMERENB;
-			emu_wr(sc, INTE, x, 4);
+			emu_wr4(sc, INTE, x);
 		}
 	} else {
 		sc->timer = 0;
-		x = emu_rd(sc, INTE, 4);
+		x = emu_rd4(sc, INTE);
 		x &= ~INTE_INTERVALTIMERENB;
-		emu_wr(sc, INTE, x, 4);
+		emu_wr4(sc, INTE, x);
 	}
 	return 0;
 }
@@ -1007,9 +991,9 @@
 			emu_wrptr(sc, 0, ch->setupreg, 0);
 			emu_wrptr(sc, 0, ch->setupreg, val);
 		}
-		val = emu_rd(sc, INTE, 4);
+		val = emu_rd4(sc, INTE);
 		val |= ch->irqmask;
-		emu_wr(sc, INTE, val, 4);
+		emu_wr4(sc, INTE, val);
 		break;
 
 	case PCMTRIG_STOP:
@@ -1018,9 +1002,9 @@
 		emu_wrptr(sc, 0, ch->sizereg, 0);
 		if (ch->setupreg)
 			emu_wrptr(sc, 0, ch->setupreg, 0);
-		val = emu_rd(sc, INTE, 4);
+		val = emu_rd4(sc, INTE);
 		val &= ~ch->irqmask;
-		emu_wr(sc, INTE, val, 4);
+		emu_wr4(sc, INTE, val);
 		break;
 
 	case PCMTRIG_EMLDMAWR:
@@ -1072,7 +1056,7 @@
 {	
 	unsigned int d;
 
-	d = emu_rd(sc, 0x18 + reg, 1); 
+	d = emu_rd1(sc, 0x18 + reg); 
 	return d;
 }
 
@@ -1080,7 +1064,7 @@
 emu_mwrite(void *arg, struct sc_info *sc, int reg, unsigned char b)
 {
 
-	emu_wr(sc, 0x18 + reg, b, 1);
+	emu_wr1(sc, 0x18 + reg, b);
 }
 
 static int
@@ -1117,9 +1101,9 @@
 {
 	int i;
 
-	i = emu_rd(sc, INTE, 4);
+	i = emu_rd4(sc, INTE);
 	i |= INTE_MIDIRXENABLE;
-	emu_wr(sc, INTE, i, 4);
+	emu_wr4(sc, INTE, i);
 
 	sc->mpu = mpu401_init(&emu_mpu_class, sc, emu_intr2, &sc->mpu_intr);
 }
@@ -1134,7 +1118,7 @@
 
 	snd_mtxlock(sc->lock);
 	while (1) {
-		stat = emu_rd(sc, IPR, 4);
+		stat = emu_rd4(sc, IPR);
 		if (stat == 0)
 			break;
 		ack = 0;
@@ -1174,7 +1158,7 @@
 			device_printf(sc->dev, "dodgy irq: %x (harmless)\n",
 			    stat & ~ack);
 
-		emu_wr(sc, IPR, stat, 4);
+		emu_wr4(sc, IPR, stat);
 
 		if (ack) {
 			snd_mtxunlock(sc->lock);
@@ -1664,9 +1648,8 @@
 	}
 
 	/* disable audio and lock cache */
-	emu_wr(sc, HCFG,
-	    HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE,
-	    4);
+	emu_wr4(sc, HCFG,
+	    HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
 
 	/* reset recording buffers */
 	emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
@@ -1677,9 +1660,8 @@
 	emu_wrptr(sc, 0, ADCBA, 0);
 
 	/* disable channel interrupt */
-	emu_wr(sc, INTE,
-	    INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE,
-	    4);
+	emu_wr4(sc, INTE,
+	    INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE);
 	emu_wrptr(sc, 0, CLIEL, 0);
 	emu_wrptr(sc, 0, CLIEH, 0);
 	emu_wrptr(sc, 0, SOLEL, 0);
@@ -1785,12 +1767,12 @@
 		emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, tmp | 0x400);
 
 		/* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
-		emu_wr(sc, 0x20, 0x00600000, 4);
-		emu_wr(sc, 0x24, 0x00000014, 4);
+		emu_wr4(sc, 0x20, 0x00600000);
+		emu_wr4(sc, 0x24, 0x00000014);
 
 		/* Setup SRCMulti Input Audio Enable */
-		emu_wr(sc, 0x20, 0x006e0000, 4);
-		emu_wr(sc, 0x24, 0xff00ff00, 4);
+		emu_wr4(sc, 0x20, 0x006e0000);
+		emu_wr4(sc, 0x24, 0xff00ff00);
 	}
 
 	SLIST_INIT(&sc->mem.blocks);
@@ -1849,24 +1831,23 @@
 		if (sc->audigy2)	/* Audigy 2 */
 			tmp = HCFG_AUDIOENABLE | HCFG_AC3ENABLE_CDSPDIF |
 			    HCFG_AC3ENABLE_GPSPDIF;
-		emu_wr(sc, HCFG, tmp, 4);
+		emu_wr4(sc, HCFG, tmp);
 
 		audigy_initefx(sc);
 
 		/* from ALSA initialization code: */
 
 		/* enable audio and disable both audio/digital outputs */
-		emu_wr(sc, HCFG, emu_rd(sc, HCFG, 4) | HCFG_AUDIOENABLE, 4);
-		emu_wr(sc, A_IOCFG, emu_rd(sc, A_IOCFG, 4) & ~A_IOCFG_GPOUT_AD,
-		    4);
+		emu_wr4(sc, HCFG, emu_rd4(sc, HCFG) | HCFG_AUDIOENABLE);
+		emu_wr4(sc, A_IOCFG, emu_rd4(sc, A_IOCFG) & ~A_IOCFG_GPOUT_AD);
 		if (sc->audigy2) {	/* Audigy 2 */
 			/* Unmute Analog.
 			 * Set GPO6 to 1 for Apollo. This has to be done after
 			 * init Alice3 I2SOut beyond 48kHz.
 			 * So, sequence is important.
 			 */
-			emu_wr(sc, A_IOCFG,
-			    emu_rd(sc, A_IOCFG, 4) | A_IOCFG_GPOUT_A, 4);
+			emu_wr4(sc, A_IOCFG,
+			    emu_rd4(sc, A_IOCFG) | A_IOCFG_GPOUT_A);
 		}
 	} else {
 		/* EMU10K1 initialization code */
@@ -1875,17 +1856,17 @@
 		if (sc->rev >= 6)
 			tmp |= HCFG_JOYENABLE;
 
-		emu_wr(sc, HCFG, tmp, 4);
+		emu_wr4(sc, HCFG, tmp);
 
 		/* TOSLink detection */
 		sc->tos_link = 0;
-		tmp = emu_rd(sc, HCFG, 4);
+		tmp = emu_rd4(sc, HCFG);
 		if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
-			emu_wr(sc, HCFG, tmp | HCFG_GPOUT1, 4);
+			emu_wr4(sc, HCFG, tmp | HCFG_GPOUT1);
 			DELAY(50);
-			if (tmp != (emu_rd(sc, HCFG, 4) & ~HCFG_GPOUT1)) {
+			if (tmp != (emu_rd4(sc, HCFG) & ~HCFG_GPOUT1)) {
 				sc->tos_link = 1;
-				emu_wr(sc, HCFG, tmp, 4);
+				emu_wr4(sc, HCFG, tmp);
 			}
 		}
 	}
@@ -1898,7 +1879,7 @@
 {
 	u_int32_t ch;
 
-	emu_wr(sc, INTE, 0, 4);
+	emu_wr4(sc, INTE, 0);
 	for (ch = 0; ch < NUM_G; ch++)
 		emu_wrptr(sc, ch, DCYSUSV, ENV_OFF);
 	for (ch = 0; ch < NUM_G; ch++) {
@@ -1913,9 +1894,8 @@
 	}
 
 	/* disable audio and lock cache */
-	emu_wr(sc, HCFG,
-	    HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE,
-	    4);
+	emu_wr4(sc, HCFG,
+	    HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
 
 	emu_wrptr(sc, 0, PTB, 0);
 	/* reset recording buffers */
@@ -1946,6 +1926,19 @@
 	return 0;
 }
 
+static void
+emu_destroy(device_t dev, struct sc_info *sc)
+{
+	if (!sc)
+		return;
+
+	if (sc->ih) bus_teardown_intr(dev, sc->res[RES_IRQ], sc->ih);
+	bus_release_resources(dev, emu_res_spec, sc->res);
+	if (sc->parent_dmat) bus_dma_tag_destroy(sc->parent_dmat);
+	if (sc->lock) snd_mtxfree(sc->lock);
+	free(sc, M_DEVBUF);
+}
+
 static int
 emu_pci_probe(device_t dev)
 {
@@ -1980,7 +1973,6 @@
 {
 	struct ac97_info *codec = NULL;
 	struct sc_info *sc;
-	u_int32_t data;
 	int i, gotmic;
 	char status[SND_STATUSLEN];
 
@@ -1994,19 +1986,13 @@
 	sc->nchans = sc->audigy ? 8 : 4;
 	sc->addrmask = sc->audigy ? A_PTR_ADDRESS_MASK : PTR_ADDRESS_MASK;
 
-	data = pci_read_config(dev, PCIR_COMMAND, 2);
-	data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
-	pci_write_config(dev, PCIR_COMMAND, data, 2);
-	data = pci_read_config(dev, PCIR_COMMAND, 2);
+	pci_enable_busmaster(dev);
+	pci_enable_io(dev, SYS_RES_IOPORT);
 
-	i = PCIR_BAR(0);
-	sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &i, RF_ACTIVE);
-	if (sc->reg == NULL) {
-		device_printf(dev, "unable to map register space\n");
+	if (bus_alloc_resources(dev, emu_res_spec, sc->res) != 0) {
+		device_printf(dev, "unable to allocate resources\n");
 		goto bad;
 	}
-	sc->st = rman_get_bustag(sc->reg);
-	sc->sh = rman_get_bushandle(sc->reg);
 
 	sc->bufsz = pcm_getbuffersize(dev, 4096, EMU_DEFAULT_BUFSZ, 65536);
 
@@ -2035,16 +2021,15 @@
 	emu_midiattach(sc);
 
 	i = 0;
-	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
-	    RF_ACTIVE | RF_SHAREABLE);
-	if (!sc->irq ||
-	    snd_setup_intr(dev, sc->irq, INTR_MPSAFE, emu_intr, sc, &sc->ih)) {
+	if (snd_setup_intr(dev, sc->res[RES_IRQ], INTR_MPSAFE, 
+	    emu_intr, sc, &sc->ih)) {
 		device_printf(dev, "unable to map interrupt\n");
 		goto bad;
 	}
 
 	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s",
-	    rman_get_start(sc->reg), rman_get_start(sc->irq),
+	    rman_get_start(sc->res[RES_MEM]), 
+	    rman_get_start(sc->res[RES_IRQ]),
 	    PCM_KLDSTRING(snd_emu10k1));
 
 	if (pcm_register(dev, sc, sc->nchans, gotmic ? 3 : 2)) goto bad;
@@ -2059,12 +2044,8 @@
 
 bad:
 	if (codec) ac97_destroy(codec);
-	if (sc->reg) bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
-	if (sc->ih) bus_teardown_intr(dev, sc->irq, sc->ih);
-	if (sc->irq) bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
-	if (sc->parent_dmat) bus_dma_tag_destroy(sc->parent_dmat);
-	if (sc->lock) snd_mtxfree(sc->lock);
-	free(sc, M_DEVBUF);
+	emu_destroy(dev, sc);	
+
 	return ENXIO;
 }
 
@@ -2081,13 +2062,7 @@
 	sc = pcm_getdevinfo(dev);
 	/* shutdown chip */
 	emu_uninit(sc);
-
-	bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0), sc->reg);
-	bus_teardown_intr(dev, sc->irq, sc->ih);
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
-	bus_dma_tag_destroy(sc->parent_dmat);
-	snd_mtxfree(sc->lock);
-	free(sc, M_DEVBUF);
+	emu_destroy(dev, sc);
 
 	return 0;
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200707311009.l6VA9C4C007942>