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.54 2007/02/23 12:18:34 piso Exp $
34 * $DragonFly: src/sys/dev/video/bktr/bktr_os.c,v 1.21 2008/05/18 03:02:53 pavalos 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>
57 #include <sys/device.h>
59 #include <sys/kernel.h>
60 #include <sys/signalvar.h>
61 #include <sys/malloc.h>
63 #include <sys/event.h>
66 #include <sys/thread2.h>
69 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
73 #include <bus/pci/pcivar.h>
74 #include <bus/pci/pcireg.h>
75 #include <bus/pci/pcidevs.h>
77 #include <sys/sysctl.h>
80 int bt848_reverse_mute = -1;
81 int bt848_format = -1;
82 int bt848_slow_msp_audio = -1;
83 #ifdef BKTR_NEW_MSP34XX_DRIVER
84 int bt848_stereo_once = 0; /* no continuous stereo monitoring */
85 int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
86 the autoscan seems work well only with FM... */
90 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
91 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
92 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
93 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
94 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
95 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
96 #ifdef BKTR_NEW_MSP34XX_DRIVER
97 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
98 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
99 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
102 #include <dev/video/meteor/ioctl_meteor.h>
103 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
104 #include <dev/video/bktr/bktr_reg.h>
105 #include <dev/video/bktr/bktr_tuner.h>
106 #include <dev/video/bktr/bktr_card.h>
107 #include <dev/video/bktr/bktr_audio.h>
108 #include <dev/video/bktr/bktr_core.h>
109 #include <dev/video/bktr/bktr_os.h>
111 #if defined(BKTR_USE_FREEBSD_SMBUS)
112 #include <dev/video/bktr/bktr_i2c.h>
114 #include "iicbb_if.h"
115 #include "smbus_if.h"
118 static int bktr_probe( device_t dev );
119 static int bktr_attach( device_t dev );
120 static int bktr_detach( device_t dev );
121 static int bktr_shutdown( device_t dev );
122 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
124 static device_method_t bktr_methods[] = {
125 /* Device interface */
126 DEVMETHOD(device_probe, bktr_probe),
127 DEVMETHOD(device_attach, bktr_attach),
128 DEVMETHOD(device_detach, bktr_detach),
129 DEVMETHOD(device_shutdown, bktr_shutdown),
131 #if defined(BKTR_USE_FREEBSD_SMBUS)
132 /* iicbb interface */
133 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
134 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
135 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
136 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
137 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
138 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
140 /* smbus interface */
141 DEVMETHOD(smbus_callback, bti2c_smb_callback),
142 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
143 DEVMETHOD(smbus_writew, bti2c_smb_writew),
144 DEVMETHOD(smbus_readb, bti2c_smb_readb),
150 static driver_t bktr_driver = {
153 sizeof(struct bktr_softc),
156 static devclass_t bktr_devclass;
158 static d_open_t bktr_open;
159 static d_close_t bktr_close;
160 static d_read_t bktr_read;
161 static d_write_t bktr_write;
162 static d_ioctl_t bktr_ioctl;
163 static d_mmap_t bktr_mmap;
164 static d_kqfilter_t bktr_kqfilter;
166 static void bktr_filter_detach(struct knote *);
167 static int bktr_filter(struct knote *, long);
169 #define CDEV_MAJOR 92
170 static struct dev_ops bktr_ops = {
171 { "bktr", CDEV_MAJOR, 0 },
173 .d_close = bktr_close,
175 .d_write = bktr_write,
176 .d_ioctl = bktr_ioctl,
177 .d_kqfilter = bktr_kqfilter,
181 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
182 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
183 MODULE_VERSION(bktr, 1);
186 * the boot time probe routine.
189 bktr_probe( device_t dev )
191 unsigned int type = pci_get_devid(dev);
192 unsigned int rev = pci_get_revid(dev);
194 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
196 switch (PCI_PRODUCT(type)) {
197 case PCI_PRODUCT_BROOKTREE_BT848:
199 device_set_desc(dev, "BrookTree 848A");
201 device_set_desc(dev, "BrookTree 848");
202 return BUS_PROBE_DEFAULT;
203 case PCI_PRODUCT_BROOKTREE_BT849:
204 device_set_desc(dev, "BrookTree 849A");
205 return BUS_PROBE_DEFAULT;
206 case PCI_PRODUCT_BROOKTREE_BT878:
207 device_set_desc(dev, "BrookTree 878");
208 return BUS_PROBE_DEFAULT;
209 case PCI_PRODUCT_BROOKTREE_BT879:
210 device_set_desc(dev, "BrookTree 879");
211 return BUS_PROBE_DEFAULT;
220 * the attach routine.
223 bktr_attach( device_t dev )
232 u_long old_irq, new_irq;
235 struct bktr_softc *bktr = device_get_softc(dev);
237 unit = device_get_unit(dev);
239 /* build the device name for bktr_name() */
240 ksnprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
243 * Enable bus mastering and Memory Mapped device
245 val = pci_read_config(dev, PCIR_COMMAND, 4);
246 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
247 pci_write_config(dev, PCIR_COMMAND, val, 4);
250 * Map control/status registers.
252 bktr->mem_rid = PCIR_BAR(0);
253 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
254 &bktr->mem_rid, RF_ACTIVE);
256 if (!bktr->res_mem) {
257 device_printf(dev, "could not map memory\n");
261 bktr->memt = rman_get_bustag(bktr->res_mem);
262 bktr->memh = rman_get_bushandle(bktr->res_mem);
266 * Disable the brooktree device
268 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
269 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
272 #ifdef BROOKTREE_IRQ /* from the configuration file */
273 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
274 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
275 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
276 kprintf("bktr%d: attach: irq changed from %d to %d\n",
277 unit, (old_irq & 0xff), (new_irq & 0xff));
281 * Allocate our interrupt.
284 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
285 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
286 if (bktr->res_irq == NULL) {
287 device_printf(dev, "could not map interrupt\n");
292 error = bus_setup_intr(dev, bktr->res_irq, 0,
293 bktr_intr, bktr, &bktr->res_ih, NULL);
295 device_printf(dev, "could not setup irq\n");
301 /* Update the Device Control Register */
302 /* on Bt878 and Bt879 cards */
303 fun = pci_read_config( dev, 0x40, 2);
304 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
306 #if defined( BKTR_430_FX_MODE )
307 if (bootverbose) kprintf("Using 430 FX chipset compatibilty mode\n");
308 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
311 #if defined( BKTR_SIS_VIA_MODE )
312 if (bootverbose) kprintf("Using SiS/VIA chipset compatibilty mode\n");
313 fun = fun | 4; /* Enable SiS/VIA compatibility mode (useful for
314 OPTi chipset motherboards too */
316 pci_write_config(dev, 0x40, fun, 2);
318 #if defined(BKTR_USE_FREEBSD_SMBUS)
319 if (bt848_i2c_attach(dev))
320 kprintf("bktr%d: i2c_attach: can't attach\n", unit);
324 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
325 * you have more than four, then 16 would probably be a better value.
327 #ifndef BROOKTREE_DEF_LATENCY_VALUE
328 #define BROOKTREE_DEF_LATENCY_VALUE 10
330 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
331 latency = (latency >> 8) & 0xff;
334 kprintf("brooktree%d: PCI bus latency is", unit);
336 kprintf("brooktree%d: PCI bus latency was 0 changing to",
340 latency = BROOKTREE_DEF_LATENCY_VALUE;
341 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
344 kprintf(" %d.\n", (int) latency);
347 /* read the pci device id and revision id */
348 fun = pci_get_devid(dev);
349 rev = pci_get_revid(dev);
351 /* call the common attach code */
352 common_bktr_attach( bktr, unit, fun, rev );
354 /* make the device entries */
355 make_dev(&bktr_ops, unit, 0, 0, 0444, "bktr%d", unit);
356 make_dev(&bktr_ops, unit+16, 0, 0, 0444, "tuner%d", unit);
357 make_dev(&bktr_ops, unit+32, 0, 0, 0444, "vbi%d" , unit);
363 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
365 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
371 * the detach routine.
374 bktr_detach( device_t dev )
376 struct bktr_softc *bktr = device_get_softc(dev);
378 #ifdef BKTR_NEW_MSP34XX_DRIVER
379 /* Disable the soundchip and kernel thread */
380 if (bktr->msp3400c_info != NULL)
384 /* Disable the brooktree device */
385 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
386 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
388 #if defined(BKTR_USE_FREEBSD_SMBUS)
389 if (bt848_i2c_detach(dev))
390 kprintf("bktr%d: i2c_attach: can't attach\n",
391 device_get_unit(dev));
394 mtx_destroy(&bktr->vbimutex);
397 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
398 /* The memory is retained by the bktr_mem module so we can unload and */
399 /* then reload the main bktr driver module */
401 /* removing the ops automatically destroys all related devices */
402 dev_ops_remove_minor(&bktr_ops, /*0x0f, */device_get_unit(dev));
405 * Deallocate resources.
407 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
408 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
409 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
415 * the shutdown routine.
418 bktr_shutdown( device_t dev )
420 struct bktr_softc *bktr = device_get_softc(dev);
422 /* Disable the brooktree device */
423 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
424 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
431 * Special Memory Allocation
434 get_bktr_mem( int unit, unsigned size )
436 vm_offset_t addr = 0;
438 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
439 0xffffffff, 1<<24, 0);
441 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
442 0xffffffff, PAGE_SIZE, 0);
444 kprintf("bktr%d: Unable to allocate %d bytes of memory.\n",
452 /*---------------------------------------------------------
454 ** BrookTree 848 character device driver routines
456 **---------------------------------------------------------
459 #define VIDEO_DEV 0x00
460 #define TUNER_DEV 0x01
463 #define UNIT(x) ((x) & 0x0f)
464 #define FUNCTION(x) (x >> 4)
470 bktr_open(struct dev_open_args *ap)
472 cdev_t dev = ap->a_head.a_dev;
477 unit = UNIT( minor(dev) );
479 /* Get the device data */
480 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
482 /* the device is no longer valid/functioning */
486 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
489 /* Record that the device is now busy */
490 device_busy(devclass_get_device(bktr_devclass, unit));
493 if (bt848_card != -1) {
494 if ((bt848_card >> 8 == unit ) &&
495 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
496 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
497 bktr->bt848_card = (bt848_card & 0xff);
498 probeCard(bktr, FALSE, unit);
503 if (bt848_tuner != -1) {
504 if ((bt848_tuner >> 8 == unit ) &&
505 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
506 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
507 bktr->bt848_tuner = (bt848_tuner & 0xff);
508 probeCard(bktr, FALSE, unit);
513 if (bt848_reverse_mute != -1) {
514 if ((bt848_reverse_mute >> 8) == unit ) {
515 bktr->reverse_mute = bt848_reverse_mute & 0xff;
519 if (bt848_slow_msp_audio != -1) {
520 if ((bt848_slow_msp_audio >> 8) == unit ) {
521 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
525 #ifdef BKTR_NEW_MSP34XX_DRIVER
526 if (bt848_stereo_once != 0) {
527 if ((bt848_stereo_once >> 8) == unit ) {
528 bktr->stereo_once = (bt848_stereo_once & 0xff);
532 if (bt848_amsound != -1) {
533 if ((bt848_amsound >> 8) == unit ) {
534 bktr->amsound = (bt848_amsound & 0xff);
538 if (bt848_dolby != -1) {
539 if ((bt848_dolby >> 8) == unit ) {
540 bktr->dolby = (bt848_dolby & 0xff);
545 switch ( FUNCTION( minor(dev) ) ) {
547 result = video_open( bktr );
550 result = tuner_open( bktr );
553 result = vbi_open( bktr );
560 /* If there was an error opening the device, undo the busy status */
562 device_unbusy(devclass_get_device(bktr_devclass, unit));
571 bktr_close(struct dev_close_args *ap)
573 cdev_t dev = ap->a_head.a_dev;
578 unit = UNIT( minor(dev) );
580 /* Get the device data */
581 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
583 /* the device is no longer valid/functioning */
587 switch ( FUNCTION( minor(dev) ) ) {
589 result = video_close( bktr );
592 result = tuner_close( bktr );
595 result = vbi_close( bktr );
602 device_unbusy(devclass_get_device(bktr_devclass, unit));
611 bktr_read(struct dev_read_args *ap)
613 cdev_t dev = ap->a_head.a_dev;
617 unit = UNIT(minor(dev));
619 /* Get the device data */
620 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
622 /* the device is no longer valid/functioning */
626 switch ( FUNCTION( minor(dev) ) ) {
628 return( video_read( bktr, unit, dev, ap->a_uio ) );
630 return( vbi_read( bktr, ap->a_uio, ap->a_ioflag ) );
640 bktr_write(struct dev_write_args *ap)
642 return( EINVAL ); /* XXX or ENXIO ? */
650 bktr_ioctl(struct dev_ioctl_args *ap)
652 cdev_t dev = ap->a_head.a_dev;
653 u_long cmd = ap->a_cmd;
657 unit = UNIT(minor(dev));
659 /* Get the device data */
660 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
662 /* the device is no longer valid/functioning */
666 #ifdef BKTR_GPIO_ACCESS
667 if (bktr->bigbuf == 0 && cmd != BT848_GPIO_GET_EN &&
668 cmd != BT848_GPIO_SET_EN && cmd != BT848_GPIO_GET_DATA &&
669 cmd != BT848_GPIO_SET_DATA) /* no frame buffer allocated (ioctl failed) */
672 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
676 switch ( FUNCTION( minor(dev) ) ) {
678 return( video_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
680 return( tuner_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
691 bktr_mmap(struct dev_mmap_args *ap)
693 cdev_t dev = ap->a_head.a_dev;
697 unit = UNIT(minor(dev));
699 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
702 /* Get the device data */
703 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
705 /* the device is no longer valid/functioning */
709 if (ap->a_nprot & PROT_EXEC)
712 if (ap->a_offset < 0)
715 if (ap->a_offset >= bktr->alloc_pages * PAGE_SIZE)
718 ap->a_result = atop(vtophys(bktr->bigbuf) + ap->a_offset);
722 static struct filterops bktr_filterops =
723 { FILTEROP_ISFD, NULL, bktr_filter_detach, bktr_filter };
726 bktr_kqfilter(struct dev_kqfilter_args *ap)
728 cdev_t dev = ap->a_head.a_dev;
729 struct knote *kn = ap->a_kn;
736 switch (kn->kn_filter) {
738 if (FUNCTION(minor(dev)) == VBI_DEV) {
739 unit = UNIT(minor(dev));
740 /* Get the device data */
741 bktr = (struct bktr_softc *)
742 devclass_get_softc(bktr_devclass, unit);
743 kn->kn_fop = &bktr_filterops;
744 kn->kn_hook = (caddr_t)bktr;
749 ap->a_result = EOPNOTSUPP;
753 klist = &bktr->vbi_kq.ki_note;
754 knote_insert(klist, kn);
760 bktr_filter_detach(struct knote *kn)
762 bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
765 klist = &bktr->vbi_kq.ki_note;
766 knote_insert(klist, kn);
770 bktr_filter(struct knote *kn, long hint)
772 bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
776 /* the device is no longer valid/functioning */
777 kn->kn_flags |= EV_EOF;
783 if (bktr->vbisize != 0)