2 * 1. Redistributions of source code must retain the
3 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Amancio Hasty and
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.45 2004/03/17 17:50:28 njl Exp $
34 * $DragonFly: src/sys/dev/video/bktr/bktr_os.c,v 1.12 2005/06/10 23:25:05 dillon Exp $
38 * This is part of the Driver for Video Capture Cards (Frame grabbers)
39 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
41 * Copyright Roger Hardiman and Amancio Hasty.
43 * bktr_os : This has all the Operating System dependant code,
44 * probe/attach and open/close/ioctl/read/mmap
49 #include "opt_bktr.h" /* include any kernel config options */
51 #define FIFO_RISC_DISABLED 0
52 #define ALL_INTS_DISABLED 0
54 #include <sys/param.h>
55 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/signalvar.h>
60 #include <sys/malloc.h>
63 #include <sys/select.h>
64 #include <sys/vnode.h>
65 #include <sys/thread2.h>
68 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
72 #include <sys/bus.h> /* used by smbus and newbus */
74 #include <machine/bus_memio.h> /* used by bus space */
75 #include <machine/bus.h> /* used by bus space and newbus */
78 #include <sys/rman.h> /* used by newbus */
79 #include <machine/resource.h> /* used by newbus */
80 #include <bus/pci/pcivar.h>
81 #include <bus/pci/pcireg.h>
82 #include <bus/pci/pcidevs.h>
84 #include <sys/sysctl.h>
87 int bt848_reverse_mute = -1;
88 int bt848_format = -1;
89 int bt848_slow_msp_audio = -1;
90 #ifdef BKTR_NEW_MSP34XX_DRIVER
91 int bt848_stereo_once = 0; /* no continuous stereo monitoring */
92 int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
93 the autoscan seems work well only with FM... */
97 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
98 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
99 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
100 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
101 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
102 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
103 #ifdef BKTR_NEW_MSP34XX_DRIVER
104 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
105 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
106 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
109 #include <dev/video/meteor/ioctl_meteor.h>
110 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
111 #include <dev/video/bktr/bktr_reg.h>
112 #include <dev/video/bktr/bktr_tuner.h>
113 #include <dev/video/bktr/bktr_card.h>
114 #include <dev/video/bktr/bktr_audio.h>
115 #include <dev/video/bktr/bktr_core.h>
116 #include <dev/video/bktr/bktr_os.h>
118 #if defined(BKTR_USE_FREEBSD_SMBUS)
119 #include <dev/video/bktr/bktr_i2c.h>
121 #include "iicbb_if.h"
122 #include "smbus_if.h"
125 static int bktr_probe( device_t dev );
126 static int bktr_attach( device_t dev );
127 static int bktr_detach( device_t dev );
128 static int bktr_shutdown( device_t dev );
129 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
131 static device_method_t bktr_methods[] = {
132 /* Device interface */
133 DEVMETHOD(device_probe, bktr_probe),
134 DEVMETHOD(device_attach, bktr_attach),
135 DEVMETHOD(device_detach, bktr_detach),
136 DEVMETHOD(device_shutdown, bktr_shutdown),
138 #if defined(BKTR_USE_FREEBSD_SMBUS)
139 /* iicbb interface */
140 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
141 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
142 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
143 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
144 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
145 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
147 /* smbus interface */
148 DEVMETHOD(smbus_callback, bti2c_smb_callback),
149 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
150 DEVMETHOD(smbus_writew, bti2c_smb_writew),
151 DEVMETHOD(smbus_readb, bti2c_smb_readb),
157 static driver_t bktr_driver = {
160 sizeof(struct bktr_softc),
163 static devclass_t bktr_devclass;
165 static d_open_t bktr_open;
166 static d_close_t bktr_close;
167 static d_read_t bktr_read;
168 static d_write_t bktr_write;
169 static d_ioctl_t bktr_ioctl;
170 static d_mmap_t bktr_mmap;
171 static d_poll_t bktr_poll;
173 #define CDEV_MAJOR 92
174 static struct cdevsw bktr_cdevsw = {
176 /* maj */ CDEV_MAJOR,
181 /* open */ bktr_open,
182 /* close */ bktr_close,
183 /* read */ bktr_read,
184 /* write */ bktr_write,
185 /* ioctl */ bktr_ioctl,
186 /* poll */ bktr_poll,
187 /* mmap */ bktr_mmap,
188 /* strategy */ nostrategy,
193 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
194 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
195 MODULE_VERSION(bktr, 1);
198 * the boot time probe routine.
201 bktr_probe( device_t dev )
203 unsigned int type = pci_get_devid(dev);
204 unsigned int rev = pci_get_revid(dev);
206 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
208 switch (PCI_PRODUCT(type)) {
209 case PCI_PRODUCT_BROOKTREE_BT848:
211 device_set_desc(dev, "BrookTree 848A");
213 device_set_desc(dev, "BrookTree 848");
215 case PCI_PRODUCT_BROOKTREE_BT849:
216 device_set_desc(dev, "BrookTree 849A");
218 case PCI_PRODUCT_BROOKTREE_BT878:
219 device_set_desc(dev, "BrookTree 878");
221 case PCI_PRODUCT_BROOKTREE_BT879:
222 device_set_desc(dev, "BrookTree 879");
232 * the attach routine.
235 bktr_attach( device_t dev )
244 u_long old_irq, new_irq;
247 struct bktr_softc *bktr = device_get_softc(dev);
249 unit = device_get_unit(dev);
251 /* build the device name for bktr_name() */
252 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
255 * Enable bus mastering and Memory Mapped device
257 val = pci_read_config(dev, PCIR_COMMAND, 4);
258 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
259 pci_write_config(dev, PCIR_COMMAND, val, 4);
262 * Map control/status registers.
264 bktr->mem_rid = PCIR_BAR(0);
265 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
266 &bktr->mem_rid, RF_ACTIVE);
268 if (!bktr->res_mem) {
269 device_printf(dev, "could not map memory\n");
273 bktr->memt = rman_get_bustag(bktr->res_mem);
274 bktr->memh = rman_get_bushandle(bktr->res_mem);
278 * Disable the brooktree device
280 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
281 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
284 #ifdef BROOKTREE_IRQ /* from the configuration file */
285 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
286 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
287 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
288 printf("bktr%d: attach: irq changed from %d to %d\n",
289 unit, (old_irq & 0xff), (new_irq & 0xff));
293 * Allocate our interrupt.
296 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
297 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
298 if (bktr->res_irq == NULL) {
299 device_printf(dev, "could not map interrupt\n");
304 error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
305 bktr_intr, bktr, &bktr->res_ih, NULL);
307 device_printf(dev, "could not setup irq\n");
313 /* Update the Device Control Register */
314 /* on Bt878 and Bt879 cards */
315 fun = pci_read_config( dev, 0x40, 2);
316 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
318 #if defined( BKTR_430_FX_MODE )
319 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
320 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
323 #if defined( BKTR_SIS_VIA_MODE )
324 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
325 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
326 OPTi chipset motherboards too */
328 pci_write_config(dev, 0x40, fun, 2);
330 #if defined(BKTR_USE_FREEBSD_SMBUS)
331 if (bt848_i2c_attach(dev))
332 printf("bktr%d: i2c_attach: can't attach\n", unit);
336 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
337 * you have more than four, then 16 would probably be a better value.
339 #ifndef BROOKTREE_DEF_LATENCY_VALUE
340 #define BROOKTREE_DEF_LATENCY_VALUE 10
342 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
343 latency = (latency >> 8) & 0xff;
346 printf("brooktree%d: PCI bus latency is", unit);
348 printf("brooktree%d: PCI bus latency was 0 changing to",
352 latency = BROOKTREE_DEF_LATENCY_VALUE;
353 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
356 printf(" %d.\n", (int) latency);
359 /* read the pci device id and revision id */
360 fun = pci_get_devid(dev);
361 rev = pci_get_revid(dev);
363 /* call the common attach code */
364 common_bktr_attach( bktr, unit, fun, rev );
366 /* make the device entries */
367 cdevsw_add(&bktr_cdevsw, 0x0f, unit);
368 make_dev(&bktr_cdevsw, unit, 0, 0, 0444, "bktr%d", unit);
369 make_dev(&bktr_cdevsw, unit+16, 0, 0, 0444, "tuner%d", unit);
370 make_dev(&bktr_cdevsw, unit+32, 0, 0, 0444, "vbi%d" , unit);
377 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
379 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
385 * the detach routine.
388 bktr_detach( device_t dev )
390 struct bktr_softc *bktr = device_get_softc(dev);
392 #ifdef BKTR_NEW_MSP34XX_DRIVER
393 /* Disable the soundchip and kernel thread */
394 if (bktr->msp3400c_info != NULL)
398 /* Disable the brooktree device */
399 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
400 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
402 #if defined(BKTR_USE_FREEBSD_SMBUS)
403 if (bt848_i2c_detach(dev))
404 printf("bktr%d: i2c_attach: can't attach\n",
405 device_get_unit(dev));
408 mtx_destroy(&bktr->vbimutex);
411 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
412 /* The memory is retained by the bktr_mem module so we can unload and */
413 /* then reload the main bktr driver module */
415 /* removing the cdevsw automatically destroys all related devices */
416 cdevsw_remove(&bktr_cdevsw, 0x0f, device_get_unit(dev));
419 * Deallocate resources.
421 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
422 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
423 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
429 * the shutdown routine.
432 bktr_shutdown( device_t dev )
434 struct bktr_softc *bktr = device_get_softc(dev);
436 /* Disable the brooktree device */
437 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
438 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
445 * Special Memory Allocation
448 get_bktr_mem( int unit, unsigned size )
450 vm_offset_t addr = 0;
452 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
453 0xffffffff, 1<<24, 0);
455 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
456 0xffffffff, PAGE_SIZE, 0);
458 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
466 /*---------------------------------------------------------
468 ** BrookTree 848 character device driver routines
470 **---------------------------------------------------------
473 #define VIDEO_DEV 0x00
474 #define TUNER_DEV 0x01
477 #define UNIT(x) ((x) & 0x0f)
478 #define FUNCTION(x) (x >> 4)
484 bktr_open( dev_t dev, int flags, int fmt, struct thread *td )
490 unit = UNIT( minor(dev) );
492 /* Get the device data */
493 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
495 /* the device is no longer valid/functioning */
499 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
502 /* Record that the device is now busy */
503 device_busy(devclass_get_device(bktr_devclass, unit));
506 if (bt848_card != -1) {
507 if ((bt848_card >> 8 == unit ) &&
508 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
509 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
510 bktr->bt848_card = (bt848_card & 0xff);
511 probeCard(bktr, FALSE, unit);
516 if (bt848_tuner != -1) {
517 if ((bt848_tuner >> 8 == unit ) &&
518 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
519 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
520 bktr->bt848_tuner = (bt848_tuner & 0xff);
521 probeCard(bktr, FALSE, unit);
526 if (bt848_reverse_mute != -1) {
527 if ((bt848_reverse_mute >> 8) == unit ) {
528 bktr->reverse_mute = bt848_reverse_mute & 0xff;
532 if (bt848_slow_msp_audio != -1) {
533 if ((bt848_slow_msp_audio >> 8) == unit ) {
534 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
538 #ifdef BKTR_NEW_MSP34XX_DRIVER
539 if (bt848_stereo_once != 0) {
540 if ((bt848_stereo_once >> 8) == unit ) {
541 bktr->stereo_once = (bt848_stereo_once & 0xff);
545 if (bt848_amsound != -1) {
546 if ((bt848_amsound >> 8) == unit ) {
547 bktr->amsound = (bt848_amsound & 0xff);
551 if (bt848_dolby != -1) {
552 if ((bt848_dolby >> 8) == unit ) {
553 bktr->dolby = (bt848_dolby & 0xff);
558 switch ( FUNCTION( minor(dev) ) ) {
560 result = video_open( bktr );
563 result = tuner_open( bktr );
566 result = vbi_open( bktr );
573 /* If there was an error opening the device, undo the busy status */
575 device_unbusy(devclass_get_device(bktr_devclass, unit));
584 bktr_close( dev_t dev, int flags, int fmt, struct thread *td )
590 unit = UNIT( minor(dev) );
592 /* Get the device data */
593 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
595 /* the device is no longer valid/functioning */
599 switch ( FUNCTION( minor(dev) ) ) {
601 result = video_close( bktr );
604 result = tuner_close( bktr );
607 result = vbi_close( bktr );
614 device_unbusy(devclass_get_device(bktr_devclass, unit));
623 bktr_read( dev_t dev, struct uio *uio, int ioflag )
628 unit = UNIT(minor(dev));
630 /* Get the device data */
631 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
633 /* the device is no longer valid/functioning */
637 switch ( FUNCTION( minor(dev) ) ) {
639 return( video_read( bktr, unit, dev, uio ) );
641 return( vbi_read( bktr, uio, ioflag ) );
651 bktr_write( dev_t dev, struct uio *uio, int ioflag )
653 return( EINVAL ); /* XXX or ENXIO ? */
661 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct thread *td )
666 unit = UNIT(minor(dev));
668 /* Get the device data */
669 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
671 /* the device is no longer valid/functioning */
675 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
678 switch ( FUNCTION( minor(dev) ) ) {
680 return( video_ioctl( bktr, unit, cmd, arg, td ) );
682 return( tuner_ioctl( bktr, unit, cmd, arg, td ) );
693 bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
698 unit = UNIT(minor(dev));
700 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
703 /* Get the device data */
704 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
706 /* the device is no longer valid/functioning */
710 if (nprot & PROT_EXEC)
716 if (offset >= bktr->alloc_pages * PAGE_SIZE)
719 return(atop(vtophys(bktr->bigbuf) + offset));
723 bktr_poll( dev_t dev, int events, struct thread *td)
729 unit = UNIT(minor(dev));
731 /* Get the device data */
732 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
734 /* the device is no longer valid/functioning */
741 if (events & (POLLIN | POLLRDNORM)) {
743 switch ( FUNCTION( minor(dev) ) ) {
745 if(bktr->vbisize == 0)
746 selrecord(td, &bktr->vbi_select);
748 revents |= events & (POLLIN | POLLRDNORM);