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