bktr_os.c revision 1.10       1 /*	$NetBSD: bktr_os.c,v 1.10 2000/07/01 01:39:02 wiz Exp $	*/
      2 
      3 /* FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.10 2000/06/28 15:09:12 roger Exp */
      4 
      5 /*
      6  * This is part of the Driver for Video Capture Cards (Frame grabbers)
      7  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
      8  * chipset.
      9  * Copyright Roger Hardiman and Amancio Hasty.
     10  *
     11  * bktr_os : This has all the Operating System dependant code,
     12  *             probe/attach and open/close/ioctl/read/mmap
     13  *             memory allocation
     14  *             PCI bus interfacing
     15  *
     16  *
     17  */
     18 
     19 /*
     20  * 1. Redistributions of source code must retain the
     21  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
     22  * All rights reserved.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  * 1. Redistributions of source code must retain the above copyright
     28  *    notice, this list of conditions and the following disclaimer.
     29  * 2. Redistributions in binary form must reproduce the above copyright
     30  *    notice, this list of conditions and the following disclaimer in the
     31  *    documentation and/or other materials provided with the distribution.
     32  * 3. All advertising materials mentioning features or use of this software
     33  *    must display the following acknowledgement:
     34  *	This product includes software developed by Amancio Hasty and
     35  *      Roger Hardiman
     36  * 4. The name of the author may not be used to endorse or promote products
     37  *    derived from this software without specific prior written permission.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     40  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     41  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     42  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     43  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     44  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     45  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     49  * POSSIBILITY OF SUCH DAMAGE.
     50  */
     51 
     52 
     53 #ifdef __FreeBSD__
     54 #include "bktr.h"
     55 #include "opt_devfs.h"
     56 #endif /* __FreeBSD__ */
     57 
     58 #include "opt_bktr.h"		/* include any kernel config options */
     59 
     60 #define FIFO_RISC_DISABLED      0
     61 #define ALL_INTS_DISABLED       0
     62 
     63 
     64 /*******************/
     65 /* *** FreeBSD *** */
     66 /*******************/
     67 #ifdef __FreeBSD__
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/conf.h>
     72 #include <sys/uio.h>
     73 #include <sys/kernel.h>
     74 #include <sys/signalvar.h>
     75 #include <sys/mman.h>
     76 #include <sys/poll.h>
     77 #include <sys/select.h>
     78 #include <sys/vnode.h>
     79 
     80 #ifndef __NetBSD__
     81 #include <vm/vm.h>
     82 #include <vm/vm_kern.h>
     83 #include <vm/pmap.h>
     84 #include <vm/vm_extern.h>
     85 #endif
     86 
     87 /*******************/
     88 /* *** FreeBSD *** */
     89 /*******************/
     90 #ifdef __FreeBSD__
     91 
     92 #if (__FreeBSD_version < 400000)
     93 #ifdef DEVFS
     94 #include <sys/devfsext.h>
     95 #endif /* DEVFS */
     96 #endif
     97 
     98 #if (__FreeBSD_version >=400000) || (NSMBUS > 0)
     99 #include <sys/bus.h>		/* used by smbus and newbus */
    100 #endif
    101 
    102 #if (__FreeBSD_version >=300000)
    103 #include <machine/bus_memio.h>	/* used by bus space */
    104 #include <machine/bus.h>	/* used by bus space and newbus */
    105 #include <sys/bus.h>
    106 #endif
    107 
    108 #if (__FreeBSD_version >=400000)
    109 #include <sys/rman.h>		/* used by newbus */
    110 #include <machine/resource.h>	/* used by newbus */
    111 #endif
    112 
    113 
    114 #include <machine/clock.h>      /* for DELAY */
    115 #include <pci/pcivar.h>
    116 #include <pci/pcireg.h>
    117 
    118 #if (NSMBUS > 0)
    119 #include <dev/bktr/bktr_i2c.h>
    120 #endif
    121 
    122 #include <sys/sysctl.h>
    123 int bt848_card = -1;
    124 int bt848_tuner = -1;
    125 int bt848_reverse_mute = -1;
    126 int bt848_format = -1;
    127 int bt848_slow_msp_audio = -1;
    128 
    129 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
    130 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
    131 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
    132 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
    133 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
    134 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
    135 
    136 #if (__FreeBSD__ == 2)
    137 #define PCIR_REVID     PCI_CLASS_REG
    138 #endif
    139 
    140 #endif /* end freebsd section */
    141 
    142 
    143 
    144 /****************/
    145 /* *** BSDI *** */
    146 /****************/
    147 #ifdef __bsdi__
    148 #endif /* __bsdi__ */
    149 
    150 
    151 /**************************/
    152 /* *** OpenBSD/NetBSD *** */
    153 /**************************/
    154 #if defined(__NetBSD__) || defined(__OpenBSD__)
    155 
    156 #include <sys/param.h>
    157 #include <sys/systm.h>
    158 #include <sys/conf.h>
    159 #include <sys/uio.h>
    160 #include <sys/kernel.h>
    161 #include <sys/signalvar.h>
    162 #include <sys/mman.h>
    163 #include <sys/poll.h>
    164 #include <sys/select.h>
    165 #include <sys/vnode.h>
    166 
    167 #include <vm/vm.h>
    168 
    169 #ifndef __NetBSD__
    170 #include <vm/vm_kern.h>
    171 #include <vm/pmap.h>
    172 #include <vm/vm_extern.h>
    173 #endif
    174 
    175 #include <sys/device.h>
    176 #include <dev/pci/pcivar.h>
    177 #include <dev/pci/pcireg.h>
    178 #include <dev/pci/pcidevs.h>
    179 
    180 #define BKTR_DEBUG
    181 #ifdef BKTR_DEBUG
    182 int bktr_debug = 0;
    183 #define DPR(x)	(bktr_debug ? printf x : 0)
    184 #else
    185 #define DPR(x)
    186 #endif
    187 #endif /* __NetBSD__ || __OpenBSD__ */
    188 
    189 
    190 #ifdef __NetBSD__
    191 #include <dev/ic/bt8xx.h>	/* NetBSD location for .h files */
    192 #include <dev/pci/bktr/bktr_reg.h>
    193 #include <dev/pci/bktr/bktr_tuner.h>
    194 #include <dev/pci/bktr/bktr_card.h>
    195 #include <dev/pci/bktr/bktr_audio.h>
    196 #include <dev/pci/bktr/bktr_core.h>
    197 #include <dev/pci/bktr/bktr_os.h>
    198 #else					/* Traditional location for .h files */
    199 #include <machine/ioctl_meteor.h>
    200 #include <machine/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
    201 #include <dev/bktr/bktr_reg.h>
    202 #include <dev/bktr/bktr_tuner.h>
    203 #include <dev/bktr/bktr_card.h>
    204 #include <dev/bktr/bktr_audio.h>
    205 #include <dev/bktr/bktr_core.h>
    206 #include <dev/bktr/bktr_os.h>
    207 #endif
    208 
    209 
    210 
    211 /****************************/
    212 /* *** FreeBSD 4.x code *** */
    213 /****************************/
    214 #if (__FreeBSD_version >= 400000)
    215 
    216 static int	bktr_probe( device_t dev );
    217 static int	bktr_attach( device_t dev );
    218 static int	bktr_detach( device_t dev );
    219 static int	bktr_shutdown( device_t dev );
    220 static void	bktr_intr(void *arg) { common_bktr_intr(arg); }
    221 
    222 static device_method_t bktr_methods[] = {
    223 	/* Device interface */
    224 	DEVMETHOD(device_probe,         bktr_probe),
    225 	DEVMETHOD(device_attach,        bktr_attach),
    226 	DEVMETHOD(device_detach,        bktr_detach),
    227 	DEVMETHOD(device_shutdown,      bktr_shutdown),
    228 
    229 	{ 0, 0 }
    230 };
    231 
    232 static driver_t bktr_driver = {
    233 	"bktr",
    234 	bktr_methods,
    235 	sizeof(struct bktr_softc),
    236 };
    237 
    238 static devclass_t bktr_devclass;
    239 
    240 static	d_open_t	bktr_open;
    241 static	d_close_t	bktr_close;
    242 static	d_read_t	bktr_read;
    243 static	d_write_t	bktr_write;
    244 static	d_ioctl_t	bktr_ioctl;
    245 static	d_mmap_t	bktr_mmap;
    246 static	d_poll_t	bktr_poll;
    247 
    248 #define CDEV_MAJOR 92
    249 static struct cdevsw bktr_cdevsw = {
    250 	/* open */	bktr_open,
    251 	/* close */	bktr_close,
    252 	/* read */	bktr_read,
    253 	/* write */	bktr_write,
    254 	/* ioctl */	bktr_ioctl,
    255 	/* poll */	bktr_poll,
    256 	/* mmap */	bktr_mmap,
    257 	/* strategy */	nostrategy,
    258 	/* name */	"bktr",
    259 	/* maj */	CDEV_MAJOR,
    260 	/* dump */	nodump,
    261 	/* psize */	nopsize,
    262 	/* flags */	0,
    263 	/* bmaj */	-1
    264 };
    265 
    266 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
    267 
    268 
    269 /*
    270  * the boot time probe routine.
    271  */
    272 static int
    273 bktr_probe( device_t dev )
    274 {
    275 	unsigned int type = pci_get_devid(dev);
    276         unsigned int rev  = pci_get_revid(dev);
    277 
    278 	switch (type) {
    279 	case BROOKTREE_848_PCI_ID:
    280 		if (rev == 0x12) device_set_desc(dev, "BrookTree 848A");
    281 		else             device_set_desc(dev, "BrookTree 848");
    282                 return 0;
    283         case BROOKTREE_849_PCI_ID:
    284                 device_set_desc(dev, "BrookTree 849A");
    285                 return 0;
    286         case BROOKTREE_878_PCI_ID:
    287                 device_set_desc(dev, "BrookTree 878");
    288                 return 0;
    289         case BROOKTREE_879_PCI_ID:
    290                 device_set_desc(dev, "BrookTree 879");
    291                 return 0;
    292 	};
    293 
    294         return ENXIO;
    295 }
    296 
    297 
    298 /*
    299  * the attach routine.
    300  */
    301 static int
    302 bktr_attach( device_t dev )
    303 {
    304 	u_long		latency;
    305 	u_long		fun;
    306 	u_long		val;
    307 	unsigned int	rev;
    308 	unsigned int	unit;
    309 	int		error = 0;
    310 	int		rid;
    311 #ifdef BROOKTREE_IRQ
    312 	u_long		old_irq, new_irq;
    313 #endif
    314 
    315         struct bktr_softc *bktr = device_get_softc(dev);
    316 
    317 	unit = device_get_unit(dev);
    318 
    319 	/* build the device name for bktr_name() */
    320 	snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
    321 
    322 	/*
    323 	 * Enable bus mastering and Memory Mapped device
    324 	 */
    325 	val = pci_read_config(dev, PCIR_COMMAND, 4);
    326 	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
    327 	pci_write_config(dev, PCIR_COMMAND, val, 4);
    328 
    329 	/*
    330 	 * Map control/status registers.
    331 	 */
    332 	rid = PCIR_MAPS;
    333 	bktr->res_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
    334                                   0, ~0, 1, RF_ACTIVE);
    335 
    336 	if (!bktr->res_mem) {
    337 		device_printf(dev, "could not map memory\n");
    338 		error = ENXIO;
    339 		goto fail;
    340 	}
    341 	bktr->memt = rman_get_bustag(bktr->res_mem);
    342 	bktr->memh = rman_get_bushandle(bktr->res_mem);
    343 
    344 
    345 	/*
    346 	 * Disable the brooktree device
    347 	 */
    348 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
    349 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
    350 
    351 
    352 #ifdef BROOKTREE_IRQ		/* from the configuration file */
    353 	old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
    354 	pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
    355 	new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
    356 	printf("bktr%d: attach: irq changed from %d to %d\n",
    357 		unit, (old_irq & 0xff), (new_irq & 0xff));
    358 #endif
    359 
    360 	/*
    361 	 * Allocate our interrupt.
    362 	 */
    363 	rid = 0;
    364 	bktr->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
    365                                  RF_SHAREABLE | RF_ACTIVE);
    366 	if (bktr->res_irq == NULL) {
    367 		device_printf(dev, "could not map interrupt\n");
    368 		error = ENXIO;
    369 		goto fail;
    370 	}
    371 
    372 	error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
    373                                bktr_intr, bktr, &bktr->res_ih);
    374 	if (error) {
    375 		device_printf(dev, "could not setup irq\n");
    376 		goto fail;
    377 
    378 	}
    379 
    380 
    381 	/* Update the Device Control Register */
    382 	/* on Bt878 and Bt879 cards           */
    383 	fun = pci_read_config( dev, 0x40, 2);
    384         fun = fun | 1;	/* Enable writes to the sub-system vendor ID */
    385 
    386 #if defined( BKTR_430_FX_MODE )
    387 	if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
    388         fun = fun | 2;	/* Enable Intel 430 FX compatibility mode */
    389 #endif
    390 
    391 #if defined( BKTR_SIS_VIA_MODE )
    392 	if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
    393         fun = fun | 4;	/* Enable SiS/VIA compatibility mode (usefull for
    394                            OPTi chipset motherboards too */
    395 #endif
    396 	pci_write_config(dev, 0x40, fun, 2);
    397 
    398 
    399 	/* XXX call bt848_i2c dependent attach() routine */
    400 #if (NSMBUS > 0)
    401 	if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
    402 		printf("bktr%d: i2c_attach: can't attach\n", unit);
    403 #endif
    404 
    405 
    406 /*
    407  * PCI latency timer.  32 is a good value for 4 bus mastering slots, if
    408  * you have more than four, then 16 would probably be a better value.
    409  */
    410 #ifndef BROOKTREE_DEF_LATENCY_VALUE
    411 #define BROOKTREE_DEF_LATENCY_VALUE	10
    412 #endif
    413 	latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
    414 	latency = (latency >> 8) & 0xff;
    415 	if ( bootverbose ) {
    416 		if (latency)
    417 			printf("brooktree%d: PCI bus latency is", unit);
    418 		else
    419 			printf("brooktree%d: PCI bus latency was 0 changing to",
    420 				unit);
    421 	}
    422 	if ( !latency ) {
    423 		latency = BROOKTREE_DEF_LATENCY_VALUE;
    424 		pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
    425 	}
    426 	if ( bootverbose ) {
    427 		printf(" %d.\n", (int) latency);
    428 	}
    429 
    430 	/* read the pci device id and revision id */
    431 	fun = pci_get_devid(dev);
    432         rev = pci_get_revid(dev);
    433 
    434 	/* call the common attach code */
    435 	common_bktr_attach( bktr, unit, fun, rev );
    436 
    437 	make_dev(&bktr_cdevsw, unit,    0, 0, 0444, "bktr%d",  unit);
    438 	make_dev(&bktr_cdevsw, unit+16, 0, 0, 0444, "tuner%d", unit);
    439 	make_dev(&bktr_cdevsw, unit+32, 0, 0, 0444, "vbi%d", unit);
    440 
    441 	return 0;
    442 
    443 fail:
    444 	return error;
    445 
    446 }
    447 
    448 /*
    449  * the detach routine.
    450  */
    451 static int
    452 bktr_detach( device_t dev )
    453 {
    454 	struct bktr_softc *bktr = device_get_softc(dev);
    455 
    456 	/* Disable the brooktree device */
    457 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
    458 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
    459 
    460 	/* FIXME - Free memory for RISC programs, grab buffer, vbi buffers */
    461 
    462 	/*
    463 	 * Deallocate resources.
    464 	 */
    465 	bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
    466 	bus_release_resource(dev, SYS_RES_IRQ, 0, bktr->res_irq);
    467 	bus_release_resource(dev, SYS_RES_MEMORY, PCIR_MAPS, bktr->res_mem);
    468 
    469 	return 0;
    470 }
    471 
    472 /*
    473  * the shutdown routine.
    474  */
    475 static int
    476 bktr_shutdown( device_t dev )
    477 {
    478 	struct bktr_softc *bktr = device_get_softc(dev);
    479 
    480 	/* Disable the brooktree device */
    481 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
    482 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
    483 
    484 	return 0;
    485 }
    486 
    487 
    488 /*
    489  * Special Memory Allocation
    490  */
    491 vm_offset_t
    492 get_bktr_mem( int unit, unsigned size )
    493 {
    494 	vm_offset_t	addr = 0;
    495 
    496 	addr = vm_page_alloc_contig(size, 0, 0xffffffff, 1<<24);
    497 	if (addr == 0)
    498 		addr = vm_page_alloc_contig(size, 0, 0xffffffff, PAGE_SIZE);
    499 	if (addr == 0) {
    500 		printf("bktr%d: Unable to allocate %d bytes of memory.\n",
    501 			unit, size);
    502 	}
    503 
    504 	return( addr );
    505 }
    506 
    507 
    508 /*---------------------------------------------------------
    509 **
    510 **	BrookTree 848 character device driver routines
    511 **
    512 **---------------------------------------------------------
    513 */
    514 
    515 #define VIDEO_DEV	0x00
    516 #define TUNER_DEV	0x01
    517 #define VBI_DEV		0x02
    518 
    519 #define UNIT(x)		((x) & 0x0f)
    520 #define FUNCTION(x)	(x >> 4)
    521 
    522 /*
    523  *
    524  */
    525 int
    526 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
    527 {
    528 	bktr_ptr_t	bktr;
    529 	int		unit;
    530 	int		result;
    531 
    532 	unit = UNIT( minor(dev) );
    533 
    534 	/* Get the device data */
    535 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    536 	if (bktr == NULL) {
    537 		/* the device is no longer valid/functioning */
    538 		return (ENXIO);
    539 	}
    540 
    541 	if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
    542 		return( ENXIO );
    543 
    544 	/* Record that the device is now busy */
    545 	device_busy(devclass_get_device(bktr_devclass, unit));
    546 
    547 
    548 	if (bt848_card != -1) {
    549 	  if ((bt848_card >> 8   == unit ) &&
    550 	     ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
    551 	    if ( bktr->bt848_card != (bt848_card & 0xff) ) {
    552 	      bktr->bt848_card = (bt848_card & 0xff);
    553 	      probeCard(bktr, FALSE, unit);
    554 	    }
    555 	  }
    556 	}
    557 
    558 	if (bt848_tuner != -1) {
    559 	  if ((bt848_tuner >> 8   == unit ) &&
    560 	     ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
    561 	    if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
    562 	      bktr->bt848_tuner = (bt848_tuner & 0xff);
    563 	      probeCard(bktr, FALSE, unit);
    564 	    }
    565 	  }
    566 	}
    567 
    568 	if (bt848_reverse_mute != -1) {
    569 	  if ((bt848_reverse_mute >> 8)   == unit ) {
    570 	    bktr->reverse_mute = bt848_reverse_mute & 0xff;
    571 	  }
    572 	}
    573 
    574 	if (bt848_slow_msp_audio != -1) {
    575 	  if ((bt848_slow_msp_audio >> 8) == unit ) {
    576 	      bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
    577 	  }
    578 	}
    579 
    580 	switch ( FUNCTION( minor(dev) ) ) {
    581 	case VIDEO_DEV:
    582 		result = video_open( bktr );
    583 		break;
    584 	case TUNER_DEV:
    585 		result = tuner_open( bktr );
    586 		break;
    587 	case VBI_DEV:
    588 		result = vbi_open( bktr );
    589 		break;
    590 	default:
    591 		result = ENXIO;
    592 		break;
    593 	}
    594 
    595 	/* If there was an error opening the device, undo the busy status */
    596 	if (result != 0)
    597 		device_unbusy(devclass_get_device(bktr_devclass, unit));
    598 	return( result );
    599 }
    600 
    601 
    602 /*
    603  *
    604  */
    605 int
    606 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
    607 {
    608 	bktr_ptr_t	bktr;
    609 	int		unit;
    610 	int		result;
    611 
    612 	unit = UNIT( minor(dev) );
    613 
    614 	/* Get the device data */
    615 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    616 	if (bktr == NULL) {
    617 		/* the device is no longer valid/functioning */
    618 		return (ENXIO);
    619 	}
    620 
    621 	switch ( FUNCTION( minor(dev) ) ) {
    622 	case VIDEO_DEV:
    623 		result = video_close( bktr );
    624 		break;
    625 	case TUNER_DEV:
    626 		result = tuner_close( bktr );
    627 		break;
    628 	case VBI_DEV:
    629 		result = vbi_close( bktr );
    630 		break;
    631 	default:
    632 		return (ENXIO);
    633 		break;
    634 	}
    635 
    636 	device_unbusy(devclass_get_device(bktr_devclass, unit));
    637 	return( result );
    638 }
    639 
    640 
    641 /*
    642  *
    643  */
    644 int
    645 bktr_read( dev_t dev, struct uio *uio, int ioflag )
    646 {
    647 	bktr_ptr_t	bktr;
    648 	int		unit;
    649 
    650 	unit = UNIT(minor(dev));
    651 
    652 	/* Get the device data */
    653 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    654 	if (bktr == NULL) {
    655 		/* the device is no longer valid/functioning */
    656 		return (ENXIO);
    657 	}
    658 
    659 	switch ( FUNCTION( minor(dev) ) ) {
    660 	case VIDEO_DEV:
    661 		return( video_read( bktr, unit, dev, uio ) );
    662 	case VBI_DEV:
    663 		return( vbi_read( bktr, uio, ioflag ) );
    664 	}
    665         return( ENXIO );
    666 }
    667 
    668 
    669 /*
    670  *
    671  */
    672 int
    673 bktr_write( dev_t dev, struct uio *uio, int ioflag )
    674 {
    675 	return( EINVAL ); /* XXX or ENXIO ? */
    676 }
    677 
    678 
    679 /*
    680  *
    681  */
    682 int
    683 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
    684 {
    685 	bktr_ptr_t	bktr;
    686 	int		unit;
    687 
    688 	unit = UNIT(minor(dev));
    689 
    690 	/* Get the device data */
    691 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    692 	if (bktr == NULL) {
    693 		/* the device is no longer valid/functioning */
    694 		return (ENXIO);
    695 	}
    696 
    697 	if (bktr->bigbuf == 0)	/* no frame buffer allocated (ioctl failed) */
    698 		return( ENOMEM );
    699 
    700 	switch ( FUNCTION( minor(dev) ) ) {
    701 	case VIDEO_DEV:
    702 		return( video_ioctl( bktr, unit, cmd, arg, pr ) );
    703 	case TUNER_DEV:
    704 		return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
    705 	}
    706 
    707 	return( ENXIO );
    708 }
    709 
    710 
    711 /*
    712  *
    713  */
    714 int
    715 bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
    716 {
    717 	int		unit;
    718 	bktr_ptr_t	bktr;
    719 
    720 	unit = UNIT(minor(dev));
    721 
    722 	if (FUNCTION(minor(dev)) > 0)	/* only allow mmap on /dev/bktr[n] */
    723 		return( -1 );
    724 
    725 	/* Get the device data */
    726 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    727 	if (bktr == NULL) {
    728 		/* the device is no longer valid/functioning */
    729 		return (ENXIO);
    730 	}
    731 
    732 	if (nprot & PROT_EXEC)
    733 		return( -1 );
    734 
    735 	if (offset < 0)
    736 		return( -1 );
    737 
    738 	if (offset >= bktr->alloc_pages * PAGE_SIZE)
    739 		return( -1 );
    740 
    741 	return( atop(vtophys(bktr->bigbuf) + offset) );
    742 }
    743 
    744 int bktr_poll( dev_t dev, int events, struct proc *p)
    745 {
    746 	int		unit;
    747 	bktr_ptr_t	bktr;
    748 	int revents = 0;
    749 	DECLARE_INTR_MASK(s);
    750 
    751 	unit = UNIT(minor(dev));
    752 
    753 	/* Get the device data */
    754 	bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
    755 	if (bktr == NULL) {
    756 		/* the device is no longer valid/functioning */
    757 		return (ENXIO);
    758 	}
    759 
    760 	DISABLE_INTR(s);
    761 
    762 	if (events & (POLLIN | POLLRDNORM)) {
    763 
    764 		switch ( FUNCTION( minor(dev) ) ) {
    765 		case VBI_DEV:
    766 			if(bktr->vbisize == 0)
    767 				selrecord(p, &bktr->vbi_select);
    768 			else
    769 				revents |= events & (POLLIN | POLLRDNORM);
    770 			break;
    771 		}
    772 	}
    773 
    774 	ENABLE_INTR(s);
    775 
    776 	return (revents);
    777 }
    778 
    779 #endif		/* FreeBSD 4.x specific kernel interface routines */
    780 
    781 /**********************************/
    782 /* *** FreeBSD 2.2.x and 3.x  *** */
    783 /**********************************/
    784 
    785 #if ((__FreeBSD__ == 2) || (__FreeBSD__ == 3))
    786 
    787 static bktr_reg_t brooktree[ NBKTR ];
    788 
    789 static const char*	bktr_probe( pcici_t tag, pcidi_t type );
    790 static void		bktr_attach( pcici_t tag, int unit );
    791 static void		bktr_intr(void *arg) { common_bktr_intr(arg); }
    792 
    793 static u_long	bktr_count;
    794 
    795 static struct	pci_device bktr_device = {
    796 	"bktr",
    797 	bktr_probe,
    798 	bktr_attach,
    799 	&bktr_count
    800 };
    801 
    802 DATA_SET (pcidevice_set, bktr_device);
    803 
    804 static	d_open_t	bktr_open;
    805 static	d_close_t	bktr_close;
    806 static	d_read_t	bktr_read;
    807 static	d_write_t	bktr_write;
    808 static	d_ioctl_t	bktr_ioctl;
    809 static	d_mmap_t	bktr_mmap;
    810 static	d_poll_t	bktr_poll;
    811 
    812 #define CDEV_MAJOR 92
    813 static struct cdevsw bktr_cdevsw =
    814 {
    815 	bktr_open,	bktr_close,	bktr_read,	bktr_write,
    816 	bktr_ioctl,	nostop,		nullreset,	nodevtotty,
    817 	bktr_poll,	bktr_mmap,	NULL,		"bktr",
    818 	NULL,		-1
    819 };
    820 
    821 static int bktr_devsw_installed;
    822 
    823 static void
    824 bktr_drvinit( void *unused )
    825 {
    826 	dev_t dev;
    827 
    828 	if ( ! bktr_devsw_installed ) {
    829 		dev = makedev(CDEV_MAJOR, 0);
    830 		cdevsw_add(&dev,&bktr_cdevsw, NULL);
    831 		bktr_devsw_installed = 1;
    832 	}
    833 }
    834 
    835 SYSINIT(bktrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bktr_drvinit,NULL)
    836 
    837 /*
    838  * the boot time probe routine.
    839  */
    840 static const char*
    841 bktr_probe( pcici_t tag, pcidi_t type )
    842 {
    843         unsigned int rev = pci_conf_read( tag, PCIR_REVID) & 0x000000ff;
    844 
    845 	switch (type) {
    846 	case BROOKTREE_848_PCI_ID:
    847 		if (rev == 0x12) return("BrookTree 848A");
    848 		else             return("BrookTree 848");
    849         case BROOKTREE_849_PCI_ID:
    850                 return("BrookTree 849A");
    851         case BROOKTREE_878_PCI_ID:
    852                 return("BrookTree 878");
    853         case BROOKTREE_879_PCI_ID:
    854                 return("BrookTree 879");
    855 	};
    856 
    857 	return ((char *)0);
    858 }
    859 
    860 /*
    861  * the attach routine.
    862  */
    863 static	void
    864 bktr_attach( pcici_t tag, int unit )
    865 {
    866 	bktr_ptr_t	bktr;
    867 	u_long		latency;
    868 	u_long		fun;
    869 	unsigned int	rev;
    870 	unsigned long	base;
    871 #ifdef BROOKTREE_IRQ
    872 	u_long		old_irq, new_irq;
    873 #endif
    874 
    875 	bktr = &brooktree[unit];
    876 
    877 	if (unit >= NBKTR) {
    878 		printf("brooktree%d: attach: only %d units configured.\n",
    879 		        unit, NBKTR);
    880 		printf("brooktree%d: attach: invalid unit number.\n", unit);
    881 		return;
    882 	}
    883 
    884 	/* build the device name for bktr_name() */
    885 	snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
    886 
    887 	/* Enable Memory Mapping */
    888 	fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
    889 	pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 2);
    890 
    891 	/* Enable Bus Mastering */
    892 	fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
    893 	pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 4);
    894 
    895 	bktr->tag = tag;
    896 
    897 
    898 	/*
    899 	 * Map control/status registers
    900 	 */
    901 	pci_map_mem( tag, PCI_MAP_REG_START, (vm_offset_t *) &base,
    902 		     &bktr->phys_base );
    903 #if (__FreeBSD_version >= 300000)
    904 	bktr->memt = I386_BUS_SPACE_MEM; /* XXX should use proper bus space */
    905 	bktr->memh = (bus_space_handle_t)base; /* XXX functions here */
    906 #endif
    907 
    908 	/*
    909 	 * Disable the brooktree device
    910 	 */
    911 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
    912 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
    913 
    914 #ifdef BROOKTREE_IRQ		/* from the configuration file */
    915 	old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
    916 	pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
    917 	new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
    918 	printf("bktr%d: attach: irq changed from %d to %d\n",
    919 		unit, (old_irq & 0xff), (new_irq & 0xff));
    920 #endif
    921 
    922 	/*
    923 	 * setup the interrupt handling routine
    924 	 */
    925 	pci_map_int(tag, bktr_intr, (void*) bktr, &tty_imask);
    926 
    927 
    928 	/* Update the Device Control Register */
    929 	/* on Bt878 and Bt879 cards */
    930 	fun = pci_conf_read(tag, 0x40);
    931         fun = fun | 1;	/* Enable writes to the sub-system vendor ID */
    932 
    933 #if defined( BKTR_430_FX_MODE )
    934 	if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
    935         fun = fun | 2;	/* Enable Intel 430 FX compatibility mode */
    936 #endif
    937 
    938 #if defined( BKTR_SIS_VIA_MODE )
    939 	if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
    940         fun = fun | 4;	/* Enable SiS/VIA compatibility mode (usefull for
    941                            OPTi chipset motherboards too */
    942 #endif
    943 	pci_conf_write(tag, 0x40, fun);
    944 
    945 
    946 	/* XXX call bt848_i2c dependent attach() routine */
    947 #if (NSMBUS > 0)
    948 	if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
    949 		printf("bktr%d: i2c_attach: can't attach\n", unit);
    950 #endif
    951 
    952 
    953 /*
    954  * PCI latency timer.  32 is a good value for 4 bus mastering slots, if
    955  * you have more than four, then 16 would probably be a better value.
    956  */
    957 #ifndef BROOKTREE_DEF_LATENCY_VALUE
    958 #define BROOKTREE_DEF_LATENCY_VALUE	10
    959 #endif
    960 	latency = pci_conf_read(tag, PCI_LATENCY_TIMER);
    961 	latency = (latency >> 8) & 0xff;
    962 	if ( bootverbose ) {
    963 		if (latency)
    964 			printf("brooktree%d: PCI bus latency is", unit);
    965 		else
    966 			printf("brooktree%d: PCI bus latency was 0 changing to",
    967 				unit);
    968 	}
    969 	if ( !latency ) {
    970 		latency = BROOKTREE_DEF_LATENCY_VALUE;
    971 		pci_conf_write(tag, PCI_LATENCY_TIMER,	latency<<8);
    972 	}
    973 	if ( bootverbose ) {
    974 		printf(" %d.\n", (int) latency);
    975 	}
    976 
    977 
    978 	/* read the pci device id and revision id */
    979 	fun = pci_conf_read(tag, PCI_ID_REG);
    980         rev = pci_conf_read(tag, PCIR_REVID) & 0x000000ff;
    981 
    982 	/* call the common attach code */
    983 	common_bktr_attach( bktr, unit, fun, rev );
    984 
    985 #ifdef DEVFS
    986 	/* XXX This just throw away the token, which should probably be fixed when
    987 	   DEVFS is finally made really operational. */
    988 	devfs_add_devswf(&bktr_cdevsw, unit,    DV_CHR, 0, 0, 0444, "bktr%d",  unit);
    989 	devfs_add_devswf(&bktr_cdevsw, unit+16, DV_CHR, 0, 0, 0444, "tuner%d", unit);
    990 	devfs_add_devswf(&bktr_cdevsw, unit+32, DV_CHR, 0, 0, 0444, "vbi%d", unit);
    991 #endif /* DEVFS */
    992 
    993 }
    994 
    995 
    996 /*
    997  * Special Memory Allocation
    998  */
    999 vm_offset_t
   1000 get_bktr_mem( int unit, unsigned size )
   1001 {
   1002 	vm_offset_t	addr = 0;
   1003 
   1004 	addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff, 1<<24);
   1005 	if (addr == 0)
   1006 		addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff,
   1007 								PAGE_SIZE);
   1008 	if (addr == 0) {
   1009 		printf("bktr%d: Unable to allocate %d bytes of memory.\n",
   1010 			unit, size);
   1011 	}
   1012 
   1013 	return( addr );
   1014 }
   1015 
   1016 /*---------------------------------------------------------
   1017 **
   1018 **	BrookTree 848 character device driver routines
   1019 **
   1020 **---------------------------------------------------------
   1021 */
   1022 
   1023 
   1024 #define VIDEO_DEV	0x00
   1025 #define TUNER_DEV	0x01
   1026 #define VBI_DEV		0x02
   1027 
   1028 #define UNIT(x)		((x) & 0x0f)
   1029 #define FUNCTION(x)	((x >> 4) & 0x0f)
   1030 
   1031 
   1032 /*
   1033  *
   1034  */
   1035 int
   1036 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
   1037 {
   1038 	bktr_ptr_t	bktr;
   1039 	int		unit;
   1040 
   1041 	unit = UNIT( minor(dev) );
   1042 	if (unit >= NBKTR)			/* unit out of range */
   1043 		return( ENXIO );
   1044 
   1045 	bktr = &(brooktree[ unit ]);
   1046 
   1047 	if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
   1048 		return( ENXIO );
   1049 
   1050 
   1051 	if (bt848_card != -1) {
   1052 	  if ((bt848_card >> 8   == unit ) &&
   1053 	     ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
   1054 	    if ( bktr->bt848_card != (bt848_card & 0xff) ) {
   1055 	      bktr->bt848_card = (bt848_card & 0xff);
   1056 	      probeCard(bktr, FALSE, unit);
   1057 	    }
   1058 	  }
   1059 	}
   1060 
   1061 	if (bt848_tuner != -1) {
   1062 	  if ((bt848_tuner >> 8   == unit ) &&
   1063 	     ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
   1064 	    if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
   1065 	      bktr->bt848_tuner = (bt848_tuner & 0xff);
   1066 	      probeCard(bktr, FALSE, unit);
   1067 	    }
   1068 	  }
   1069 	}
   1070 
   1071 	if (bt848_reverse_mute != -1) {
   1072 	  if ((bt848_reverse_mute >> 8)   == unit ) {
   1073 	    bktr->reverse_mute = bt848_reverse_mute & 0xff;
   1074 	  }
   1075 	}
   1076 
   1077 	if (bt848_slow_msp_audio != -1) {
   1078 	  if ((bt848_slow_msp_audio >> 8) == unit ) {
   1079 	      bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
   1080 	  }
   1081 	}
   1082 
   1083 	switch ( FUNCTION( minor(dev) ) ) {
   1084 	case VIDEO_DEV:
   1085 		return( video_open( bktr ) );
   1086 	case TUNER_DEV:
   1087 		return( tuner_open( bktr ) );
   1088 	case VBI_DEV:
   1089 		return( vbi_open( bktr ) );
   1090 	}
   1091 	return( ENXIO );
   1092 }
   1093 
   1094 
   1095 /*
   1096  *
   1097  */
   1098 int
   1099 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
   1100 {
   1101 	bktr_ptr_t	bktr;
   1102 	int		unit;
   1103 
   1104 	unit = UNIT( minor(dev) );
   1105 	if (unit >= NBKTR)			/* unit out of range */
   1106 		return( ENXIO );
   1107 
   1108 	bktr = &(brooktree[ unit ]);
   1109 
   1110 	switch ( FUNCTION( minor(dev) ) ) {
   1111 	case VIDEO_DEV:
   1112 		return( video_close( bktr ) );
   1113 	case TUNER_DEV:
   1114 		return( tuner_close( bktr ) );
   1115 	case VBI_DEV:
   1116 		return( vbi_close( bktr ) );
   1117 	}
   1118 
   1119 	return( ENXIO );
   1120 }
   1121 
   1122 /*
   1123  *
   1124  */
   1125 int
   1126 bktr_read( dev_t dev, struct uio *uio, int ioflag )
   1127 {
   1128 	bktr_ptr_t	bktr;
   1129 	int		unit;
   1130 
   1131 	unit = UNIT(minor(dev));
   1132 	if (unit >= NBKTR)	/* unit out of range */
   1133 		return( ENXIO );
   1134 
   1135 	bktr = &(brooktree[unit]);
   1136 
   1137 	switch ( FUNCTION( minor(dev) ) ) {
   1138 	case VIDEO_DEV:
   1139 		return( video_read( bktr, unit, dev, uio ) );
   1140 	case VBI_DEV:
   1141 		return( vbi_read( bktr, uio, ioflag ) );
   1142 	}
   1143         return( ENXIO );
   1144 }
   1145 
   1146 
   1147 /*
   1148  *
   1149  */
   1150 int
   1151 bktr_write( dev_t dev, struct uio *uio, int ioflag )
   1152 {
   1153 	return( EINVAL ); /* XXX or ENXIO ? */
   1154 }
   1155 
   1156 /*
   1157  *
   1158  */
   1159 int
   1160 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
   1161 {
   1162 	bktr_ptr_t	bktr;
   1163 	int		unit;
   1164 
   1165 	unit = UNIT(minor(dev));
   1166 	if (unit >= NBKTR)	/* unit out of range */
   1167 		return( ENXIO );
   1168 
   1169 	bktr = &(brooktree[ unit ]);
   1170 
   1171 	if (bktr->bigbuf == 0)	/* no frame buffer allocated (ioctl failed) */
   1172 		return( ENOMEM );
   1173 
   1174 	switch ( FUNCTION( minor(dev) ) ) {
   1175 	case VIDEO_DEV:
   1176 		return( video_ioctl( bktr, unit, cmd, arg, pr ) );
   1177 	case TUNER_DEV:
   1178 		return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
   1179 	}
   1180 
   1181 	return( ENXIO );
   1182 }
   1183 
   1184 /*
   1185  * bktr_mmap.
   1186  * Note: 2.2.5/2.2.6/2.2.7/3.0 users must manually
   1187  * edit the line below and change  "vm_offset_t" to "int"
   1188  */
   1189 int bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
   1190 
   1191 {
   1192 	int		unit;
   1193 	bktr_ptr_t	bktr;
   1194 
   1195 	unit = UNIT(minor(dev));
   1196 
   1197 	if (unit >= NBKTR || FUNCTION(minor(dev)) > 0)
   1198 		return( -1 );
   1199 
   1200 	bktr = &(brooktree[ unit ]);
   1201 
   1202 	if (nprot & PROT_EXEC)
   1203 		return( -1 );
   1204 
   1205 	if (offset < 0)
   1206 		return( -1 );
   1207 
   1208 	if (offset >= bktr->alloc_pages * PAGE_SIZE)
   1209 		return( -1 );
   1210 
   1211 	return( i386_btop(vtophys(bktr->bigbuf) + offset) );
   1212 }
   1213 
   1214 int bktr_poll( dev_t dev, int events, struct proc *p)
   1215 {
   1216 	int		unit;
   1217 	bktr_ptr_t	bktr;
   1218 	int revents = 0;
   1219 
   1220 	unit = UNIT(minor(dev));
   1221 
   1222 	if (unit >= NBKTR)
   1223 		return( -1 );
   1224 
   1225 	bktr = &(brooktree[ unit ]);
   1226 
   1227 	disable_intr();
   1228 
   1229 	if (events & (POLLIN | POLLRDNORM)) {
   1230 
   1231 		switch ( FUNCTION( minor(dev) ) ) {
   1232 		case VBI_DEV:
   1233 			if(bktr->vbisize == 0)
   1234 				selrecord(p, &bktr->vbi_select);
   1235 			else
   1236 				revents |= events & (POLLIN | POLLRDNORM);
   1237 			break;
   1238 		}
   1239 	}
   1240 
   1241 	enable_intr();
   1242 
   1243 	return (revents);
   1244 }
   1245 
   1246 
   1247 #endif		/* FreeBSD 2.2.x and 3.x specific kernel interface routines */
   1248 
   1249 
   1250 /*****************/
   1251 /* *** BSDI  *** */
   1252 /*****************/
   1253 
   1254 #if defined(__bsdi__)
   1255 #endif		/* __bsdi__ BSDI specific kernel interface routines */
   1256 
   1257 
   1258 /*****************************/
   1259 /* *** OpenBSD / NetBSD  *** */
   1260 /*****************************/
   1261 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1262 
   1263 #define IPL_VIDEO       IPL_BIO         /* XXX */
   1264 
   1265 static	int		bktr_intr(void *arg) { return common_bktr_intr(arg); }
   1266 
   1267 #define bktr_open       bktropen
   1268 #define bktr_close      bktrclose
   1269 #define bktr_read       bktrread
   1270 #define bktr_write      bktrwrite
   1271 #define bktr_ioctl      bktrioctl
   1272 #define bktr_mmap       bktrmmap
   1273 
   1274 vm_offset_t vm_page_alloc_contig(vm_offset_t, vm_offset_t,
   1275                                  vm_offset_t, vm_offset_t);
   1276 
   1277 #if defined(__OpenBSD__)
   1278 static int      bktr_probe __P((struct device *, void *, void *));
   1279 #else
   1280 static int      bktr_probe __P((struct device *, struct cfdata *, void *));
   1281 #endif
   1282 static void     bktr_attach __P((struct device *, struct device *, void *));
   1283 
   1284 struct cfattach bktr_ca = {
   1285         sizeof(struct bktr_softc), bktr_probe, bktr_attach
   1286 };
   1287 
   1288 #if defined(__NetBSD__)
   1289 extern struct cfdriver bktr_cd;
   1290 #else
   1291 struct cfdriver bktr_cd = {
   1292         NULL, "bktr", DV_DULL
   1293 };
   1294 #endif
   1295 
   1296 int
   1297 bktr_probe(parent, match, aux)
   1298 	struct device *parent;
   1299 #if defined(__OpenBSD__)
   1300         void *match;
   1301 #else
   1302         struct cfdata *match;
   1303 #endif
   1304         void *aux;
   1305 {
   1306         struct pci_attach_args *pa = aux;
   1307 
   1308         if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROOKTREE &&
   1309             (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT848 ||
   1310              PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT849 ||
   1311              PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT878 ||
   1312              PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT879))
   1313                 return 1;
   1314 
   1315         return 0;
   1316 }
   1317 
   1318 
   1319 /*
   1320  * the attach routine.
   1321  */
   1322 static void
   1323 bktr_attach(struct device *parent, struct device *self, void *aux)
   1324 {
   1325 	bktr_ptr_t	bktr;
   1326 	u_long		latency;
   1327 	u_long		fun;
   1328 	unsigned int	rev;
   1329 
   1330 #if defined(__OpenBSD__)
   1331 	struct pci_attach_args *pa = aux;
   1332 	pci_chipset_tag_t pc = pa->pa_pc;
   1333 
   1334 	pci_intr_handle_t ih;
   1335 	const char *intrstr;
   1336 	int retval;
   1337 	int unit;
   1338 
   1339 	bktr = (bktr_ptr_t)self;
   1340 	unit = bktr->bktr_dev.dv_unit;
   1341 
   1342 	bktr->pc = pa->pa_pc;
   1343 	bktr->tag = pa->pa_tag;
   1344         bktr->dmat = pa->pa_dmat;
   1345 
   1346 	/*
   1347 	 * map memory
   1348 	 */
   1349 	bktr->memt = pa->pa_memt;
   1350 	retval = pci_mem_find(pc, pa->pa_tag, PCI_MAPREG_START,
   1351 			      &bktr->phys_base, &bktr->obmemsz, NULL);
   1352 	if (!retval)
   1353 		retval = bus_space_map(pa->pa_memt, bktr->phys_base,
   1354 				       bktr->obmemsz, 0, &bktr->memh);
   1355 	if (retval) {
   1356 		printf(": couldn't map memory\n");
   1357 		return;
   1358 	}
   1359 
   1360 
   1361 	/*
   1362 	 * map interrupt
   1363 	 */
   1364 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
   1365 			 pa->pa_intrline, &ih)) {
   1366 		printf(": couldn't map interrupt\n");
   1367 		return;
   1368 	}
   1369 	intrstr = pci_intr_string(pa->pa_pc, ih);
   1370 
   1371 	bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
   1372 				      bktr_intr, bktr, bktr->bktr_dev.dv_xname);
   1373 	if (bktr->ih == NULL) {
   1374 		printf(": couldn't establish interrupt");
   1375 		if (intrstr != NULL)
   1376 			printf(" at %s", intrstr);
   1377 		printf("\n");
   1378 		return;
   1379 	}
   1380 
   1381 	if (intrstr != NULL)
   1382 		printf(": %s\n", intrstr);
   1383 #endif /* __OpenBSD__ */
   1384 
   1385 #if defined(__NetBSD__)
   1386 	struct pci_attach_args *pa = aux;
   1387 	pci_intr_handle_t ih;
   1388 	const char *intrstr;
   1389 	int retval;
   1390 	int unit;
   1391 
   1392 	bktr = (bktr_ptr_t)self;
   1393 	unit = bktr->bktr_dev.dv_unit;
   1394         bktr->dmat = pa->pa_dmat;
   1395 
   1396 	printf("\n");
   1397 
   1398 	/*
   1399 	 * map memory
   1400 	 */
   1401 	retval = pci_mapreg_map(pa, PCI_MAPREG_START,
   1402 				PCI_MAPREG_TYPE_MEM
   1403 				| PCI_MAPREG_MEM_TYPE_32BIT, 0,
   1404 				&bktr->memt, &bktr->memh, NULL,
   1405 				&bktr->obmemsz);
   1406 	DPR(("pci_mapreg_map: memt %x, memh %x, size %x\n",
   1407 	     bktr->memt, (u_int)bktr->memh, (u_int)bktr->obmemsz));
   1408 	if (retval) {
   1409 		printf("%s: couldn't map memory\n", bktr_name(bktr));
   1410 		return;
   1411 	}
   1412 
   1413 	/*
   1414 	 * Disable the brooktree device
   1415 	 */
   1416 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
   1417 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
   1418 
   1419 	/*
   1420 	 * map interrupt
   1421 	 */
   1422 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
   1423 			 pa->pa_intrline, &ih)) {
   1424 		printf("%s: couldn't map interrupt\n",
   1425 		       bktr_name(bktr));
   1426 		return;
   1427 	}
   1428 	intrstr = pci_intr_string(pa->pa_pc, ih);
   1429 	bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
   1430 				      bktr_intr, bktr);
   1431 	if (bktr->ih == NULL) {
   1432 		printf("%s: couldn't establish interrupt",
   1433 		       bktr_name(bktr));
   1434 		if (intrstr != NULL)
   1435 			printf(" at %s", intrstr);
   1436 		printf("\n");
   1437 		return;
   1438 	}
   1439 	if (intrstr != NULL)
   1440 		printf("%s: interrupting at %s\n", bktr_name(bktr),
   1441 		       intrstr);
   1442 #endif /* __NetBSD__ */
   1443 
   1444 /*
   1445  * PCI latency timer.  32 is a good value for 4 bus mastering slots, if
   1446  * you have more than four, then 16 would probably be a better value.
   1447  */
   1448 #ifndef BROOKTREE_DEF_LATENCY_VALUE
   1449 #define BROOKTREE_DEF_LATENCY_VALUE	10
   1450 #endif
   1451 	latency = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_LATENCY_TIMER);
   1452 	latency = (latency >> 8) & 0xff;
   1453 
   1454 	if (!latency) {
   1455 		if (bootverbose) {
   1456 			printf("%s: PCI bus latency was 0 changing to %d",
   1457 			       bktr_name(bktr), BROOKTREE_DEF_LATENCY_VALUE);
   1458 		}
   1459 		latency = BROOKTREE_DEF_LATENCY_VALUE;
   1460 		pci_conf_write(pa->pa_pc, pa->pa_tag,
   1461 			       PCI_LATENCY_TIMER, latency<<8);
   1462 	}
   1463 
   1464 
   1465 	/* Enabled Bus Master
   1466 	   XXX: check if all old DMA is stopped first (e.g. after warm
   1467 	   boot) */
   1468 	fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1469 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
   1470 		       fun | PCI_COMMAND_MASTER_ENABLE);
   1471 
   1472 	/* read the pci id and determine the card type */
   1473 	fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
   1474         rev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG) & 0x000000ff;
   1475 
   1476 	common_bktr_attach(bktr, unit, fun, rev);
   1477 }
   1478 
   1479 
   1480 /*
   1481  * Special Memory Allocation
   1482  */
   1483 vm_offset_t
   1484 get_bktr_mem(bktr, dmapp, size)
   1485         bktr_ptr_t bktr;
   1486         bus_dmamap_t *dmapp;
   1487         unsigned int size;
   1488 {
   1489         bus_dma_tag_t dmat = bktr->dmat;
   1490         bus_dma_segment_t seg;
   1491         bus_size_t align;
   1492         int rseg;
   1493         caddr_t kva;
   1494 
   1495         /*
   1496          * Allocate a DMA area
   1497          */
   1498         align = 1 << 24;
   1499         if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
   1500                              &rseg, BUS_DMA_NOWAIT)) {
   1501                 align = PAGE_SIZE;
   1502                 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
   1503                                      &rseg, BUS_DMA_NOWAIT)) {
   1504                         printf("%s: Unable to dmamem_alloc of %d bytes\n",
   1505 			       bktr_name(bktr), size);
   1506                         return 0;
   1507                 }
   1508         }
   1509         if (bus_dmamem_map(dmat, &seg, rseg, size,
   1510                            &kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
   1511                 printf("%s: Unable to dmamem_map of %d bytes\n",
   1512                         bktr_name(bktr), size);
   1513                 bus_dmamem_free(dmat, &seg, rseg);
   1514                 return 0;
   1515         }
   1516 #ifdef __OpenBSD__
   1517         bktr->dm_mapsize = size;
   1518 #endif
   1519         /*
   1520          * Create and locd the DMA map for the DMA area
   1521          */
   1522         if (bus_dmamap_create(dmat, size, 1, size, 0, BUS_DMA_NOWAIT, dmapp)) {
   1523                 printf("%s: Unable to dmamap_create of %d bytes\n",
   1524                         bktr_name(bktr), size);
   1525                 bus_dmamem_unmap(dmat, kva, size);
   1526                 bus_dmamem_free(dmat, &seg, rseg);
   1527                 return 0;
   1528         }
   1529         if (bus_dmamap_load(dmat, *dmapp, kva, size, NULL, BUS_DMA_NOWAIT)) {
   1530                 printf("%s: Unable to dmamap_load of %d bytes\n",
   1531                         bktr_name(bktr), size);
   1532                 bus_dmamem_unmap(dmat, kva, size);
   1533                 bus_dmamem_free(dmat, &seg, rseg);
   1534                 bus_dmamap_destroy(dmat, *dmapp);
   1535                 return 0;
   1536         }
   1537         return (vm_offset_t)kva;
   1538 }
   1539 
   1540 void
   1541 free_bktr_mem(bktr, dmap, kva)
   1542         bktr_ptr_t bktr;
   1543         bus_dmamap_t dmap;
   1544         vm_offset_t kva;
   1545 {
   1546         bus_dma_tag_t dmat = bktr->dmat;
   1547 
   1548 #ifdef __NetBSD__
   1549         bus_dmamem_unmap(dmat, (caddr_t)kva, dmap->dm_mapsize);
   1550 #else
   1551         bus_dmamem_unmap(dmat, (caddr_t)kva, bktr->dm_mapsize);
   1552 #endif
   1553         bus_dmamem_free(dmat, dmap->dm_segs, 1);
   1554         bus_dmamap_destroy(dmat, dmap);
   1555 }
   1556 
   1557 
   1558 /*---------------------------------------------------------
   1559 **
   1560 **	BrookTree 848 character device driver routines
   1561 **
   1562 **---------------------------------------------------------
   1563 */
   1564 
   1565 
   1566 #define VIDEO_DEV	0x00
   1567 #define TUNER_DEV	0x01
   1568 #define VBI_DEV		0x02
   1569 
   1570 #define UNIT(x)         (minor((x) & 0x0f))
   1571 #define FUNCTION(x)     (minor((x >> 4) & 0x0f))
   1572 
   1573 /*
   1574  *
   1575  */
   1576 int
   1577 bktr_open(dev_t dev, int flags, int fmt, struct proc *p)
   1578 {
   1579 	bktr_ptr_t	bktr;
   1580 	int		unit;
   1581 
   1582 	unit = UNIT(dev);
   1583 
   1584 	/* unit out of range */
   1585 	if ((unit > bktr_cd.cd_ndevs) || (bktr_cd.cd_devs[unit] == NULL))
   1586 		return(ENXIO);
   1587 
   1588 	bktr = bktr_cd.cd_devs[unit];
   1589 
   1590 	if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
   1591 		return(ENXIO);
   1592 
   1593 	switch (FUNCTION(dev)) {
   1594 	case VIDEO_DEV:
   1595 		return(video_open(bktr));
   1596 	case TUNER_DEV:
   1597 		return(tuner_open(bktr));
   1598 	case VBI_DEV:
   1599 		return(vbi_open(bktr));
   1600 	}
   1601 
   1602 	return(ENXIO);
   1603 }
   1604 
   1605 
   1606 /*
   1607  *
   1608  */
   1609 int
   1610 bktr_close(dev_t dev, int flags, int fmt, struct proc *p)
   1611 {
   1612 	bktr_ptr_t	bktr;
   1613 	int		unit;
   1614 
   1615 	unit = UNIT(dev);
   1616 
   1617 	bktr = bktr_cd.cd_devs[unit];
   1618 
   1619 	switch (FUNCTION(dev)) {
   1620 	case VIDEO_DEV:
   1621 		return(video_close(bktr));
   1622 	case TUNER_DEV:
   1623 		return(tuner_close(bktr));
   1624 	case VBI_DEV:
   1625 		return(vbi_close(bktr));
   1626 	}
   1627 
   1628 	return(ENXIO);
   1629 }
   1630 
   1631 /*
   1632  *
   1633  */
   1634 int
   1635 bktr_read(dev_t dev, struct uio *uio, int ioflag)
   1636 {
   1637 	bktr_ptr_t	bktr;
   1638 	int		unit;
   1639 
   1640 	unit = UNIT(dev);
   1641 
   1642 	bktr = bktr_cd.cd_devs[unit];
   1643 
   1644 	switch (FUNCTION(dev)) {
   1645 	case VIDEO_DEV:
   1646 		return(video_read(bktr, unit, dev, uio));
   1647 	case VBI_DEV:
   1648 		return(vbi_read(bktr, uio, ioflag));
   1649 	}
   1650 
   1651         return(ENXIO);
   1652 }
   1653 
   1654 
   1655 /*
   1656  *
   1657  */
   1658 int
   1659 bktr_write(dev_t dev, struct uio *uio, int ioflag)
   1660 {
   1661 	/* operation not supported */
   1662 	return(EOPNOTSUPP);
   1663 }
   1664 
   1665 /*
   1666  *
   1667  */
   1668 int
   1669 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr)
   1670 {
   1671 	bktr_ptr_t	bktr;
   1672 	int		unit;
   1673 
   1674 	unit = UNIT(dev);
   1675 
   1676 	bktr = bktr_cd.cd_devs[unit];
   1677 
   1678 	if (bktr->bigbuf == 0)	/* no frame buffer allocated (ioctl failed) */
   1679 		return(ENOMEM);
   1680 
   1681 	switch (FUNCTION(dev)) {
   1682 	case VIDEO_DEV:
   1683 		return(video_ioctl(bktr, unit, cmd, arg, pr));
   1684 	case TUNER_DEV:
   1685 		return(tuner_ioctl(bktr, unit, cmd, arg, pr));
   1686 	}
   1687 
   1688 	return(ENXIO);
   1689 }
   1690 
   1691 /*
   1692  *
   1693  */
   1694 paddr_t
   1695 bktr_mmap(dev_t dev, off_t offset, int nprot)
   1696 {
   1697 	int		unit;
   1698 	bktr_ptr_t	bktr;
   1699 
   1700 	unit = UNIT(dev);
   1701 
   1702 	if (FUNCTION(dev) > 0)	/* only allow mmap on /dev/bktr[n] */
   1703 		return(-1);
   1704 
   1705 	bktr = bktr_cd.cd_devs[unit];
   1706 
   1707 	if ((vaddr_t)offset < 0)
   1708 		return(-1);
   1709 
   1710 	if ((vaddr_t)offset >= bktr->alloc_pages * PAGE_SIZE)
   1711 		return(-1);
   1712 
   1713 #ifdef __NetBSD__
   1714 	return (bus_dmamem_mmap(bktr->dmat, bktr->dm_mem->dm_segs, 1,
   1715 				(vaddr_t)offset, nprot, BUS_DMA_WAITOK));
   1716 #else
   1717 	return(i386_btop(vtophys(bktr->bigbuf) + offset));
   1718 #endif
   1719 }
   1720 
   1721 #endif /* __NetBSD__ || __OpenBSD__ */
   1722