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