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