sbc.c revision 70668
1/*-
2 * Copyright (c) 1999 Seigo Tanimura
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, WHETHER IN 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: head/sys/dev/sound/isa/sbc.c 70668 2001-01-04 17:12:57Z imp $
27 */
28
29#include <dev/sound/chip.h>
30#include <dev/sound/pcm/sound.h>
31#include <dev/sound/isa/sb.h>
32
33#define IO_MAX	3
34#define IRQ_MAX	1
35#define DRQ_MAX	2
36#define INTR_MAX	2
37
38struct sbc_ihl {
39	driver_intr_t *intr[INTR_MAX];
40	void *intr_arg[INTR_MAX];
41};
42
43/* Here is the parameter structure per a device. */
44struct sbc_softc {
45	device_t dev; /* device */
46
47	int io_rid[IO_MAX]; /* io port rids */
48	struct resource *io[IO_MAX]; /* io port resources */
49	int io_alloced[IO_MAX]; /* io port alloc flag */
50
51	int irq_rid[IRQ_MAX]; /* irq rids */
52	struct resource *irq[IRQ_MAX]; /* irq resources */
53	int irq_alloced[IRQ_MAX]; /* irq alloc flag */
54
55	int drq_rid[DRQ_MAX]; /* drq rids */
56	struct resource *drq[DRQ_MAX]; /* drq resources */
57	int drq_alloced[DRQ_MAX]; /* drq alloc flag */
58
59	struct sbc_ihl ihl[IRQ_MAX];
60
61	void *ih[IRQ_MAX];
62
63	u_int32_t bd_ver;
64};
65
66static int sbc_probe(device_t dev);
67static int sbc_attach(device_t dev);
68static void sbc_intr(void *p);
69
70static struct resource *sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
71					   u_long start, u_long end, u_long count, u_int flags);
72static int sbc_release_resource(device_t bus, device_t child, int type, int rid,
73				struct resource *r);
74static int sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
75   	       int flags, driver_intr_t *intr, void *arg,
76   	       void **cookiep);
77static int sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
78  		  void *cookie);
79
80static int alloc_resource(struct sbc_softc *scp);
81static int release_resource(struct sbc_softc *scp);
82
83static devclass_t sbc_devclass;
84
85static int io_range[3] = {0x10, 0x2, 0x4};
86
87#ifdef PC98 /* I/O address table for PC98 */
88static bus_addr_t pcm_iat[] = {
89	0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700,
90	0x800, 0x900, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00
91};
92static bus_addr_t midi_iat[] = {
93	0x000, 0x100
94};
95static bus_addr_t opl_iat[] = {
96	0x000, 0x100, 0x200, 0x300
97};
98static bus_addr_t *sb_iat[] = { pcm_iat, midi_iat, opl_iat };
99#endif
100
101static int sb_rd(struct resource *io, int reg);
102static void sb_wr(struct resource *io, int reg, u_int8_t val);
103static int sb_dspready(struct resource *io);
104static int sb_cmd(struct resource *io, u_char val);
105static u_int sb_get_byte(struct resource *io);
106static void sb_setmixer(struct resource *io, u_int port, u_int value);
107
108static int
109sb_rd(struct resource *io, int reg)
110{
111	return bus_space_read_1(rman_get_bustag(io),
112				rman_get_bushandle(io),
113				reg);
114}
115
116static void
117sb_wr(struct resource *io, int reg, u_int8_t val)
118{
119	return bus_space_write_1(rman_get_bustag(io),
120				 rman_get_bushandle(io),
121				 reg, val);
122}
123
124static int
125sb_dspready(struct resource *io)
126{
127	return ((sb_rd(io, SBDSP_STATUS) & 0x80) == 0);
128}
129
130static int
131sb_dspwr(struct resource *io, u_char val)
132{
133    	int  i;
134
135    	for (i = 0; i < 1000; i++) {
136		if (sb_dspready(io)) {
137	    		sb_wr(io, SBDSP_CMD, val);
138	    		return 1;
139		}
140		if (i > 10) DELAY((i > 100)? 1000 : 10);
141    	}
142    	printf("sb_dspwr(0x%02x) timed out.\n", val);
143    	return 0;
144}
145
146static int
147sb_cmd(struct resource *io, u_char val)
148{
149    	return sb_dspwr(io, val);
150}
151
152static void
153sb_setmixer(struct resource *io, u_int port, u_int value)
154{
155    	u_long   flags;
156
157    	flags = spltty();
158    	sb_wr(io, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
159    	DELAY(10);
160    	sb_wr(io, SB_MIX_DATA, (u_char) (value & 0xff));
161    	DELAY(10);
162    	splx(flags);
163}
164
165static u_int
166sb_get_byte(struct resource *io)
167{
168    	int i;
169
170    	for (i = 1000; i > 0; i--) {
171		if (sb_rd(io, DSP_DATA_AVAIL) & 0x80)
172			return sb_rd(io, DSP_READ);
173		else
174			DELAY(20);
175    	}
176    	return 0xffff;
177}
178
179static int
180sb_reset_dsp(struct resource *io)
181{
182    	sb_wr(io, SBDSP_RST, 3);
183    	DELAY(100);
184    	sb_wr(io, SBDSP_RST, 0);
185    	return (sb_get_byte(io) == 0xAA)? 0 : ENXIO;
186}
187
188static int
189sb_identify_board(struct resource *io)
190{
191	int ver, essver, rev;
192
193    	sb_cmd(io, DSP_CMD_GETVER);	/* Get version */
194    	ver = (sb_get_byte(io) << 8) | sb_get_byte(io);
195	if (ver < 0x100 || ver > 0x4ff) return 0;
196    	if (ver == 0x0301) {
197	    	/* Try to detect ESS chips. */
198	    	sb_cmd(io, DSP_CMD_GETID); /* Return ident. bytes. */
199	    	essver = (sb_get_byte(io) << 8) | sb_get_byte(io);
200	    	rev = essver & 0x000f;
201	    	essver &= 0xfff0;
202	    	if (essver == 0x4880) ver |= 0x1000;
203	    	else if (essver == 0x6880) ver = 0x0500 | rev;
204	}
205	return ver;
206}
207
208static struct isa_pnp_id sbc_ids[] = {
209	{0x01008c0e, "Creative ViBRA16C"},		/* CTL0001 */
210	{0x31008c0e, "Creative SB16/SB32"},		/* CTL0031 */
211	{0x41008c0e, "Creative SB16/SB32"},		/* CTL0041 */
212	{0x42008c0e, "Creative SB AWE64"},		/* CTL0042 */
213	{0x43008c0e, "Creative ViBRA16X"},		/* CTL0043 */
214	{0x44008c0e, "Creative SB AWE64 Gold"},		/* CTL0044 */
215	{0x45008c0e, "Creative SB AWE64"},		/* CTL0045 */
216
217	{0x01000000, "Avance Logic ALS100+"},		/* @@@0001 - ViBRA16X clone */
218	{0x01100000, "Avance Asound 110"},		/* @@@1001 */
219	{0x01200000, "Avance Logic ALS120"},		/* @@@2001 - ViBRA16X clone */
220
221	{0x81167316, "ESS ES1681"},			/* ESS1681 */
222	{0x02017316, "ESS ES1688"},			/* ESS1688 */
223	{0x68187316, "ESS ES1868"},			/* ESS1868 */
224	{0x03007316, "ESS ES1869"},			/* ESS1869 */
225	{0x69187316, "ESS ES1869"},			/* ESS1869 */
226	{0xabb0110e, "ESS ES1869 (Compaq OEM)"},	/* CPQb0ab */
227	{0xacb0110e, "ESS ES1869 (Compaq OEM)"},	/* CPQb0ac */
228	{0x78187316, "ESS ES1878"},			/* ESS1878 */
229	{0x79187316, "ESS ES1879"},			/* ESS1879 */
230	{0x88187316, "ESS ES1888"},			/* ESS1888 */
231	{0x07017316, "ESS ES1888 (DEC OEM)"},		/* ESS0107 */
232	{0x06017316, "ESS ES1888 (Dell OEM)"},          /* ESS0106 */
233	{0}
234};
235
236static int
237sbc_probe(device_t dev)
238{
239	char *s = NULL;
240	u_int32_t lid, vid;
241
242	lid = isa_get_logicalid(dev);
243	vid = isa_get_vendorid(dev);
244	if (lid) {
245		if (lid == 0x01000000 && vid != 0x01009305) /* ALS0001 */
246			return ENXIO;
247		/* Check pnp ids */
248		return ISA_PNP_PROBE(device_get_parent(dev), dev, sbc_ids);
249	} else {
250		int rid = 0, ver;
251	    	struct resource *io;
252
253#ifdef PC98
254		io = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid,
255					 pcm_iat, 16, RF_ACTIVE);
256#else
257		io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
258		  		    	0, ~0, 16, RF_ACTIVE);
259#endif
260		if (!io) goto bad;
261#ifdef PC98
262		isa_load_resourcev(io, pcm_iat, 16);
263#endif
264    		if (sb_reset_dsp(io)) goto bad2;
265		ver = sb_identify_board(io);
266		if (ver == 0) goto bad2;
267		switch ((ver & 0x00000f00) >> 8) {
268		case 1:
269			device_set_desc(dev, "SoundBlaster 1.0 (not supported)");
270			s = NULL;
271			break;
272
273		case 2:
274			s = "SoundBlaster 2.0";
275			break;
276
277		case 3:
278			s = (ver & 0x0000f000)? "ESS 488" : "SoundBlaster Pro";
279			break;
280
281		case 4:
282			s = "SoundBlaster 16";
283			break;
284
285		case 5:
286			s = (ver & 0x00000008)? "ESS 688" : "ESS 1688";
287			break;
288	     	}
289		if (s) device_set_desc(dev, s);
290bad2:		bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
291bad:		return s? 0 : ENXIO;
292	}
293}
294
295static int
296sbc_attach(device_t dev)
297{
298	char *err = NULL;
299	struct sbc_softc *scp;
300	struct sndcard_func *func;
301	device_t child;
302	u_int32_t logical_id = isa_get_logicalid(dev);
303    	int flags = device_get_flags(dev);
304	int f, dh, dl, x, irq, i;
305
306    	if (!logical_id && (flags & DV_F_DUAL_DMA)) {
307        	bus_set_resource(dev, SYS_RES_DRQ, 1,
308				 flags & DV_F_DRQ_MASK, 1);
309    	}
310
311	scp = device_get_softc(dev);
312	bzero(scp, sizeof(*scp));
313	scp->dev = dev;
314	err = "alloc_resource";
315	if (alloc_resource(scp)) goto bad;
316
317	err = "sb_reset_dsp";
318	if (sb_reset_dsp(scp->io[0])) goto bad;
319	err = "sb_identify_board";
320	scp->bd_ver = sb_identify_board(scp->io[0]) & 0x00000fff;
321	if (scp->bd_ver == 0) goto bad;
322	f = 0;
323	if (logical_id == 0x01200000 && scp->bd_ver < 0x0400) scp->bd_ver = 0x0499;
324	switch ((scp->bd_ver & 0x0f00) >> 8) {
325    	case 1: /* old sound blaster has nothing... */
326		break;
327
328    	case 2:
329		f |= BD_F_DUP_MIDI;
330		if (scp->bd_ver > 0x200) f |= BD_F_MIX_CT1335;
331		break;
332
333	case 5:
334		f |= BD_F_ESS;
335		scp->bd_ver = 0x0301;
336    	case 3:
337		f |= BD_F_DUP_MIDI | BD_F_MIX_CT1345;
338		break;
339
340    	case 4:
341    		f |= BD_F_SB16 | BD_F_MIX_CT1745;
342		if (scp->drq[0]) dl = rman_get_start(scp->drq[0]); else dl = -1;
343		if (scp->drq[1]) dh = rman_get_start(scp->drq[1]); else dh = dl;
344		if (!logical_id && (dh < dl)) {
345			struct resource *r;
346			r = scp->drq[0];
347			scp->drq[0] = scp->drq[1];
348			scp->drq[1] = r;
349			dl = rman_get_start(scp->drq[0]);
350			dh = rman_get_start(scp->drq[1]);
351		}
352		/* soft irq/dma configuration */
353		x = -1;
354		irq = rman_get_start(scp->irq[0]);
355#ifdef PC98
356		/* SB16 in PC98 use different IRQ table */
357		if	(irq == 3) x = 1;
358		else if (irq == 5) x = 8;
359		else if (irq == 10) x = 2;
360		else if (irq == 12) x = 4;
361		if (x == -1) {
362			err = "bad irq (3/5/10/12 valid)";
363			goto bad;
364		}
365		else sb_setmixer(scp->io[0], IRQ_NR, x);
366		/* SB16 in PC98 use different dma setting */
367		sb_setmixer(scp->io[0], DMA_NR, dh == 0 ? 1 : 2);
368#else
369		if      (irq == 5) x = 2;
370		else if (irq == 7) x = 4;
371		else if (irq == 9) x = 1;
372		else if (irq == 10) x = 8;
373		if (x == -1) {
374			err = "bad irq (5/7/9/10 valid)";
375			goto bad;
376		}
377		else sb_setmixer(scp->io[0], IRQ_NR, x);
378		sb_setmixer(scp->io[0], DMA_NR, (1 << dh) | (1 << dl));
379#endif
380		if (bootverbose) {
381			device_printf(dev, "setting card to irq %d, drq %d", irq, dl);
382			if (dl != dh) printf(", %d", dh);
383			printf("\n");
384    		}
385		break;
386    	}
387
388	switch (logical_id) {
389    	case 0x43008c0e:	/* CTL0043 */
390	case 0x01200000:
391	case 0x01000000:
392		f |= BD_F_SB16X;
393		break;
394	}
395	scp->bd_ver |= f << 16;
396
397	err = "setup_intr";
398	for (i = 0; i < IRQ_MAX; i++) {
399		if (bus_setup_intr(dev, scp->irq[i], INTR_TYPE_TTY, sbc_intr, &scp->ihl[i], &scp->ih[i]))
400			goto bad;
401	}
402
403	/* PCM Audio */
404	func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
405	if (func == NULL) goto bad;
406	bzero(func, sizeof(*func));
407	func->func = SCF_PCM;
408	child = device_add_child(dev, "pcm", -1);
409	device_set_ivars(child, func);
410
411	/* Midi Interface */
412	func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
413	if (func == NULL) goto bad;
414	bzero(func, sizeof(*func));
415	func->func = SCF_MIDI;
416	child = device_add_child(dev, "midi", -1);
417	device_set_ivars(child, func);
418
419	/* OPL FM Synthesizer */
420	func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
421	if (func == NULL) goto bad;
422	bzero(func, sizeof(*func));
423	func->func = SCF_SYNTH;
424	child = device_add_child(dev, "midi", -1);
425	device_set_ivars(child, func);
426
427	/* probe/attach kids */
428	bus_generic_attach(dev);
429
430	return (0);
431
432bad:	if (err) device_printf(dev, "%s\n", err);
433	release_resource(scp);
434	return (ENXIO);
435}
436
437static int
438sbc_detach(device_t dev)
439{
440	struct sbc_softc *scp = device_get_softc(dev);
441
442	release_resource(scp);
443	return bus_generic_detach(dev);
444}
445
446static void
447sbc_intr(void *p)
448{
449	struct sbc_ihl *ihl = p;
450	int i;
451
452	i = 0;
453	while (i < INTR_MAX) {
454		if (ihl->intr[i] != NULL) ihl->intr[i](ihl->intr_arg[i]);
455		i++;
456	}
457}
458
459static int
460sbc_setup_intr(device_t dev, device_t child, struct resource *irq,
461   	       int flags, driver_intr_t *intr, void *arg,
462   	       void **cookiep)
463{
464	struct sbc_softc *scp = device_get_softc(dev);
465	struct sbc_ihl *ihl = NULL;
466	int i;
467
468	i = 0;
469	while (i < IRQ_MAX) {
470		if (irq == scp->irq[i]) ihl = &scp->ihl[i];
471		i++;
472	}
473	if (ihl == NULL) return (EINVAL);
474	i = 0;
475	while (i < INTR_MAX) {
476		if (ihl->intr[i] == NULL) {
477			ihl->intr[i] = intr;
478			ihl->intr_arg[i] = arg;
479			*cookiep = &ihl->intr[i];
480			return 0;
481		} else i++;
482	}
483	return (EINVAL);
484}
485
486static int
487sbc_teardown_intr(device_t dev, device_t child, struct resource *irq,
488  		  void *cookie)
489{
490	struct sbc_softc *scp = device_get_softc(dev);
491	struct sbc_ihl *ihl = NULL;
492	int i;
493
494	i = 0;
495	while (i < IRQ_MAX) {
496		if (irq == scp->irq[i]) ihl = &scp->ihl[i];
497		i++;
498	}
499	if (ihl == NULL) return (EINVAL);
500	i = 0;
501	while (i < INTR_MAX) {
502		if (cookie == &ihl->intr[i]) {
503			ihl->intr[i] = NULL;
504			ihl->intr_arg[i] = NULL;
505			return 0;
506		} else i++;
507	}
508	return (EINVAL);
509}
510
511static struct resource *
512sbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
513		      u_long start, u_long end, u_long count, u_int flags)
514{
515	struct sbc_softc *scp;
516	int *alloced, rid_max, alloced_max;
517	struct resource **res;
518#ifdef PC98
519	int i;
520#endif
521
522	scp = device_get_softc(bus);
523	switch (type) {
524	case SYS_RES_IOPORT:
525		alloced = scp->io_alloced;
526		res = scp->io;
527#ifdef PC98
528		rid_max = 0;
529		for (i = 0; i < IO_MAX; i++)
530			rid_max += io_range[i];
531#else
532		rid_max = IO_MAX - 1;
533#endif
534		alloced_max = 1;
535		break;
536	case SYS_RES_DRQ:
537		alloced = scp->drq_alloced;
538		res = scp->drq;
539		rid_max = DRQ_MAX - 1;
540		alloced_max = 1;
541		break;
542	case SYS_RES_IRQ:
543		alloced = scp->irq_alloced;
544		res = scp->irq;
545		rid_max = IRQ_MAX - 1;
546		alloced_max = INTR_MAX; /* pcm and mpu may share the irq. */
547		break;
548	default:
549		return (NULL);
550	}
551
552	if (*rid > rid_max || alloced[*rid] == alloced_max)
553		return (NULL);
554
555	alloced[*rid]++;
556	return (res[*rid]);
557}
558
559static int
560sbc_release_resource(device_t bus, device_t child, int type, int rid,
561			struct resource *r)
562{
563	struct sbc_softc *scp;
564	int *alloced, rid_max;
565
566	scp = device_get_softc(bus);
567	switch (type) {
568	case SYS_RES_IOPORT:
569		alloced = scp->io_alloced;
570		rid_max = IO_MAX - 1;
571		break;
572	case SYS_RES_DRQ:
573		alloced = scp->drq_alloced;
574		rid_max = DRQ_MAX - 1;
575		break;
576	case SYS_RES_IRQ:
577		alloced = scp->irq_alloced;
578		rid_max = IRQ_MAX - 1;
579		break;
580	default:
581		return (1);
582	}
583
584	if (rid > rid_max || alloced[rid] == 0)
585		return (1);
586
587	alloced[rid]--;
588	return (0);
589}
590
591static int
592sbc_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
593{
594	struct sbc_softc *scp = device_get_softc(bus);
595	struct sndcard_func *func = device_get_ivars(dev);
596
597	switch (index) {
598	case 0:
599		*result = func->func;
600		break;
601
602	case 1:
603		*result = scp->bd_ver;
604	      	break;
605
606	default:
607		return ENOENT;
608	}
609
610	return 0;
611}
612
613static int
614sbc_write_ivar(device_t bus, device_t dev,
615	       int index, uintptr_t value)
616{
617	switch (index) {
618	case 0:
619	case 1:
620  		return EINVAL;
621
622	default:
623		return (ENOENT);
624	}
625}
626
627static int
628alloc_resource(struct sbc_softc *scp)
629{
630	int i;
631
632	for (i = 0 ; i < IO_MAX ; i++) {
633		if (scp->io[i] == NULL) {
634#ifdef PC98
635			scp->io_rid[i] = i > 0 ?
636				scp->io_rid[i - 1] + io_range[i - 1] : 0;
637			scp->io[i] = isa_alloc_resourcev(scp->dev,
638							 SYS_RES_IOPORT,
639							 &scp->io_rid[i],
640							 sb_iat[i],
641							 io_range[i],
642							 RF_ACTIVE);
643			if (scp->io[i] != NULL)
644				isa_load_resourcev(scp->io[i], sb_iat[i],
645						   io_range[i]);
646#else
647			scp->io_rid[i] = i;
648			scp->io[i] = bus_alloc_resource(scp->dev, SYS_RES_IOPORT, &scp->io_rid[i],
649							0, ~0, io_range[i], RF_ACTIVE);
650#endif
651			if (i == 0 && scp->io[i] == NULL)
652				return (1);
653			scp->io_alloced[i] = 0;
654		}
655	}
656	for (i = 0 ; i < DRQ_MAX ; i++) {
657		if (scp->drq[i] == NULL) {
658			scp->drq_rid[i] = i;
659			scp->drq[i] = bus_alloc_resource(scp->dev, SYS_RES_DRQ, &scp->drq_rid[i],
660							 0, ~0, 1, RF_ACTIVE);
661			if (i == 0 && scp->drq[i] == NULL)
662				return (1);
663			scp->drq_alloced[i] = 0;
664		}
665	}
666	for (i = 0 ; i < IRQ_MAX ; i++) {
667	 	if (scp->irq[i] == NULL) {
668			scp->irq_rid[i] = i;
669			scp->irq[i] = bus_alloc_resource(scp->dev, SYS_RES_IRQ, &scp->irq_rid[i],
670							 0, ~0, 1, RF_ACTIVE);
671			if (i == 0 && scp->irq[i] == NULL)
672				return (1);
673			scp->irq_alloced[i] = 0;
674		}
675	}
676	return (0);
677}
678
679static int
680release_resource(struct sbc_softc *scp)
681{
682	int i;
683
684	for (i = 0 ; i < IO_MAX ; i++) {
685		if (scp->io[i] != NULL) {
686			bus_release_resource(scp->dev, SYS_RES_IOPORT, scp->io_rid[i], scp->io[i]);
687			scp->io[i] = NULL;
688		}
689	}
690	for (i = 0 ; i < DRQ_MAX ; i++) {
691		if (scp->drq[i] != NULL) {
692			bus_release_resource(scp->dev, SYS_RES_DRQ, scp->drq_rid[i], scp->drq[i]);
693			scp->drq[i] = NULL;
694		}
695	}
696	for (i = 0 ; i < IRQ_MAX ; i++) {
697		if (scp->irq[i] != NULL) {
698			if (scp->ih[i] != NULL)
699				bus_teardown_intr(scp->dev, scp->irq[i], scp->ih[i]);
700			scp->ih[i] = NULL;
701			bus_release_resource(scp->dev, SYS_RES_IRQ, scp->irq_rid[i], scp->irq[i]);
702			scp->irq[i] = NULL;
703		}
704	}
705	return (0);
706}
707
708static device_method_t sbc_methods[] = {
709	/* Device interface */
710	DEVMETHOD(device_probe,		sbc_probe),
711	DEVMETHOD(device_attach,	sbc_attach),
712	DEVMETHOD(device_detach,	sbc_detach),
713	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
714	DEVMETHOD(device_suspend,	bus_generic_suspend),
715	DEVMETHOD(device_resume,	bus_generic_resume),
716
717	/* Bus interface */
718	DEVMETHOD(bus_read_ivar,	sbc_read_ivar),
719	DEVMETHOD(bus_write_ivar,	sbc_write_ivar),
720	DEVMETHOD(bus_print_child,	bus_generic_print_child),
721	DEVMETHOD(bus_alloc_resource,	sbc_alloc_resource),
722	DEVMETHOD(bus_release_resource,	sbc_release_resource),
723	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
724	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
725	DEVMETHOD(bus_setup_intr,	sbc_setup_intr),
726	DEVMETHOD(bus_teardown_intr,	sbc_teardown_intr),
727
728	{ 0, 0 }
729};
730
731static driver_t sbc_driver = {
732	"sbc",
733	sbc_methods,
734	sizeof(struct sbc_softc),
735};
736
737/* sbc can be attached to an isa bus. */
738DRIVER_MODULE(snd_sbc, isa, sbc_driver, sbc_devclass, 0, 0);
739MODULE_DEPEND(snd_sbc, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
740MODULE_VERSION(snd_sbc, 1);
741