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