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