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