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