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 $
37 * This is part of the Driver for Video Capture Cards (Frame grabbers)
38 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
40 * Copyright Roger Hardiman and Amancio Hasty.
42 * bktr_os : This has all the Operating System dependant code,
43 * probe/attach and open/close/ioctl/read/mmap
48 #include "opt_bktr.h" /* include any kernel config options */
50 #define FIFO_RISC_DISABLED 0
51 #define ALL_INTS_DISABLED 0
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <sys/device.h>
58 #include <sys/kernel.h>
59 #include <sys/signalvar.h>
60 #include <sys/malloc.h>
62 #include <sys/event.h>
65 #include <sys/thread2.h>
68 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
72 #include <bus/pci/pcivar.h>
73 #include <bus/pci/pcireg.h>
74 #include <bus/pci/pcidevs.h>
76 #include <sys/sysctl.h>
79 int bt848_reverse_mute = -1;
80 int bt848_format = -1;
81 int bt848_slow_msp_audio = -1;
82 #ifdef BKTR_NEW_MSP34XX_DRIVER
83 int bt848_stereo_once = 0; /* no continuous stereo monitoring */
84 int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
85 the autoscan seems work well only with FM... */
89 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
90 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
91 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
92 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
93 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
94 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
95 #ifdef BKTR_NEW_MSP34XX_DRIVER
96 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
97 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
98 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
101 #include <dev/video/meteor/ioctl_meteor.h>
102 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
103 #include <dev/video/bktr/bktr_reg.h>
104 #include <dev/video/bktr/bktr_tuner.h>
105 #include <dev/video/bktr/bktr_card.h>
106 #include <dev/video/bktr/bktr_audio.h>
107 #include <dev/video/bktr/bktr_core.h>
108 #include <dev/video/bktr/bktr_os.h>
110 #if defined(BKTR_USE_FREEBSD_SMBUS)
111 #include <dev/video/bktr/bktr_i2c.h>
113 #include "iicbb_if.h"
114 #include "smbus_if.h"
117 static int bktr_probe( device_t dev );
118 static int bktr_attach( device_t dev );
119 static int bktr_detach( device_t dev );
120 static int bktr_shutdown( device_t dev );
121 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
123 static device_method_t bktr_methods[] = {
124 /* Device interface */
125 DEVMETHOD(device_probe, bktr_probe),
126 DEVMETHOD(device_attach, bktr_attach),
127 DEVMETHOD(device_detach, bktr_detach),
128 DEVMETHOD(device_shutdown, bktr_shutdown),
130 #if defined(BKTR_USE_FREEBSD_SMBUS)
131 /* iicbb interface */
132 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
133 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
134 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
135 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
136 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
137 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
139 /* smbus interface */
140 DEVMETHOD(smbus_callback, bti2c_smb_callback),
141 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
142 DEVMETHOD(smbus_writew, bti2c_smb_writew),
143 DEVMETHOD(smbus_readb, bti2c_smb_readb),
149 static driver_t bktr_driver = {
152 sizeof(struct bktr_softc),
155 static devclass_t bktr_devclass;
157 static d_open_t bktr_open;
158 static d_close_t bktr_close;
159 static d_read_t bktr_read;
160 static d_write_t bktr_write;
161 static d_ioctl_t bktr_ioctl;
162 static d_mmap_t bktr_mmap;
163 static d_kqfilter_t bktr_kqfilter;
165 static void bktr_filter_detach(struct knote *);
166 static int bktr_filter(struct knote *, long);
168 static struct dev_ops bktr_ops = {
171 .d_close = bktr_close,
173 .d_write = bktr_write,
174 .d_ioctl = bktr_ioctl,
175 .d_kqfilter = bktr_kqfilter,
179 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, NULL, NULL);
180 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
181 MODULE_VERSION(bktr, 1);
184 * the boot time probe routine.
187 bktr_probe( device_t dev )
189 unsigned int type = pci_get_devid(dev);
190 unsigned int rev = pci_get_revid(dev);
192 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
194 switch (PCI_PRODUCT(type)) {
195 case PCI_PRODUCT_BROOKTREE_BT848:
197 device_set_desc(dev, "BrookTree 848A");
199 device_set_desc(dev, "BrookTree 848");
200 return BUS_PROBE_DEFAULT;
201 case PCI_PRODUCT_BROOKTREE_BT849:
202 device_set_desc(dev, "BrookTree 849A");
203 return BUS_PROBE_DEFAULT;
204 case PCI_PRODUCT_BROOKTREE_BT878:
205 device_set_desc(dev, "BrookTree 878");
206 return BUS_PROBE_DEFAULT;
207 case PCI_PRODUCT_BROOKTREE_BT879:
208 device_set_desc(dev, "BrookTree 879");
209 return BUS_PROBE_DEFAULT;
218 * the attach routine.
221 bktr_attach( device_t dev )
230 u_long old_irq, new_irq;
233 struct bktr_softc *bktr = device_get_softc(dev);
235 unit = device_get_unit(dev);
237 /* build the device name for bktr_name() */
238 ksnprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
241 * Enable bus mastering and Memory Mapped device
243 val = pci_read_config(dev, PCIR_COMMAND, 4);
244 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
245 pci_write_config(dev, PCIR_COMMAND, val, 4);
248 * Map control/status registers.
250 bktr->mem_rid = PCIR_BAR(0);
251 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
252 &bktr->mem_rid, RF_ACTIVE);
254 if (!bktr->res_mem) {
255 device_printf(dev, "could not map memory\n");
259 bktr->memt = rman_get_bustag(bktr->res_mem);
260 bktr->memh = rman_get_bushandle(bktr->res_mem);
264 * Disable the brooktree device
266 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
267 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
270 #ifdef BROOKTREE_IRQ /* from the configuration file */
271 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
272 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
273 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
274 kprintf("bktr%d: attach: irq changed from %d to %d\n",
275 unit, (old_irq & 0xff), (new_irq & 0xff));
279 * Allocate our interrupt.
282 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
283 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
284 if (bktr->res_irq == NULL) {
285 device_printf(dev, "could not map interrupt\n");
290 error = bus_setup_intr(dev, bktr->res_irq, 0,
291 bktr_intr, bktr, &bktr->res_ih, NULL);
293 device_printf(dev, "could not setup irq\n");
299 /* Update the Device Control Register */
300 /* on Bt878 and Bt879 cards */
301 fun = pci_read_config( dev, 0x40, 2);
302 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
304 #if defined( BKTR_430_FX_MODE )
305 if (bootverbose) kprintf("Using 430 FX chipset compatibility mode\n");
306 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
309 #if defined( BKTR_SIS_VIA_MODE )
310 if (bootverbose) kprintf("Using SiS/VIA chipset compatibility mode\n");
311 fun = fun | 4; /* Enable SiS/VIA compatibility mode (useful for
312 OPTi chipset motherboards too */
314 pci_write_config(dev, 0x40, fun, 2);
316 #if defined(BKTR_USE_FREEBSD_SMBUS)
317 if (bt848_i2c_attach(dev))
318 kprintf("bktr%d: i2c_attach: can't attach\n", unit);
322 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
323 * you have more than four, then 16 would probably be a better value.
325 #ifndef BROOKTREE_DEF_LATENCY_VALUE
326 #define BROOKTREE_DEF_LATENCY_VALUE 10
328 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
329 latency = (latency >> 8) & 0xff;
332 kprintf("brooktree%d: PCI bus latency is", unit);
334 kprintf("brooktree%d: PCI bus latency was 0 changing to",
338 latency = BROOKTREE_DEF_LATENCY_VALUE;
339 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
342 kprintf(" %d.\n", (int) latency);
345 /* read the pci device id and revision id */
346 fun = pci_get_devid(dev);
347 rev = pci_get_revid(dev);
349 /* call the common attach code */
350 common_bktr_attach( bktr, unit, fun, rev );
352 /* make the device entries */
353 make_dev(&bktr_ops, unit, 0, 0, 0444, "bktr%d", unit);
354 make_dev(&bktr_ops, unit+16, 0, 0, 0444, "tuner%d", unit);
355 make_dev(&bktr_ops, unit+32, 0, 0, 0444, "vbi%d" , unit);
361 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
363 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
369 * the detach routine.
372 bktr_detach( device_t dev )
374 struct bktr_softc *bktr = device_get_softc(dev);
376 #ifdef BKTR_NEW_MSP34XX_DRIVER
377 /* Disable the soundchip and kernel thread */
378 if (bktr->msp3400c_info != NULL)
382 /* Disable the brooktree device */
383 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
384 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
386 #if defined(BKTR_USE_FREEBSD_SMBUS)
387 if (bt848_i2c_detach(dev))
388 kprintf("bktr%d: i2c_attach: can't attach\n",
389 device_get_unit(dev));
392 mtx_destroy(&bktr->vbimutex);
395 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
396 /* The memory is retained by the bktr_mem module so we can unload and */
397 /* then reload the main bktr driver module */
399 /* removing the ops automatically destroys all related devices */
400 dev_ops_remove_minor(&bktr_ops, /*0x0f, */device_get_unit(dev));
403 * Deallocate resources.
405 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
406 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
407 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
413 * the shutdown routine.
416 bktr_shutdown( device_t dev )
418 struct bktr_softc *bktr = device_get_softc(dev);
420 /* Disable the brooktree device */
421 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
422 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
429 * Special Memory Allocation
432 get_bktr_mem( int unit, unsigned size )
434 vm_offset_t addr = 0;
436 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
437 0xffffffff, 1<<24, 0);
439 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
440 0xffffffff, PAGE_SIZE, 0);
442 kprintf("bktr%d: Unable to allocate %d bytes of memory.\n",
450 /*---------------------------------------------------------
452 ** BrookTree 848 character device driver routines
454 **---------------------------------------------------------
457 #define VIDEO_DEV 0x00
458 #define TUNER_DEV 0x01
461 #define UNIT(x) ((x) & 0x0f)
462 #define FUNCTION(x) (x >> 4)
468 bktr_open(struct dev_open_args *ap)
470 cdev_t dev = ap->a_head.a_dev;
475 unit = UNIT( minor(dev) );
477 /* Get the device data */
478 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
480 /* the device is no longer valid/functioning */
484 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
487 /* Record that the device is now busy */
488 device_busy(devclass_get_device(bktr_devclass, unit));
491 if (bt848_card != -1) {
492 if ((bt848_card >> 8 == unit ) &&
493 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
494 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
495 bktr->bt848_card = (bt848_card & 0xff);
496 probeCard(bktr, FALSE, unit);
501 if (bt848_tuner != -1) {
502 if ((bt848_tuner >> 8 == unit ) &&
503 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
504 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
505 bktr->bt848_tuner = (bt848_tuner & 0xff);
506 probeCard(bktr, FALSE, unit);
511 if (bt848_reverse_mute != -1) {
512 if ((bt848_reverse_mute >> 8) == unit ) {
513 bktr->reverse_mute = bt848_reverse_mute & 0xff;
517 if (bt848_slow_msp_audio != -1) {
518 if ((bt848_slow_msp_audio >> 8) == unit ) {
519 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
523 #ifdef BKTR_NEW_MSP34XX_DRIVER
524 if (bt848_stereo_once != 0) {
525 if ((bt848_stereo_once >> 8) == unit ) {
526 bktr->stereo_once = (bt848_stereo_once & 0xff);
530 if (bt848_amsound != -1) {
531 if ((bt848_amsound >> 8) == unit ) {
532 bktr->amsound = (bt848_amsound & 0xff);
536 if (bt848_dolby != -1) {
537 if ((bt848_dolby >> 8) == unit ) {
538 bktr->dolby = (bt848_dolby & 0xff);
543 switch ( FUNCTION( minor(dev) ) ) {
545 result = video_open( bktr );
548 result = tuner_open( bktr );
551 result = vbi_open( bktr );
558 /* If there was an error opening the device, undo the busy status */
560 device_unbusy(devclass_get_device(bktr_devclass, unit));
569 bktr_close(struct dev_close_args *ap)
571 cdev_t dev = ap->a_head.a_dev;
576 unit = UNIT( minor(dev) );
578 /* Get the device data */
579 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
581 /* the device is no longer valid/functioning */
585 switch ( FUNCTION( minor(dev) ) ) {
587 result = video_close( bktr );
590 result = tuner_close( bktr );
593 result = vbi_close( bktr );
600 device_unbusy(devclass_get_device(bktr_devclass, unit));
609 bktr_read(struct dev_read_args *ap)
611 cdev_t dev = ap->a_head.a_dev;
615 unit = UNIT(minor(dev));
617 /* Get the device data */
618 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
620 /* the device is no longer valid/functioning */
624 switch ( FUNCTION( minor(dev) ) ) {
626 return( video_read( bktr, unit, dev, ap->a_uio ) );
628 return( vbi_read( bktr, ap->a_uio, ap->a_ioflag ) );
638 bktr_write(struct dev_write_args *ap)
640 return( EINVAL ); /* XXX or ENXIO ? */
648 bktr_ioctl(struct dev_ioctl_args *ap)
650 cdev_t dev = ap->a_head.a_dev;
651 u_long cmd = ap->a_cmd;
655 unit = UNIT(minor(dev));
657 /* Get the device data */
658 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
660 /* the device is no longer valid/functioning */
664 #ifdef BKTR_GPIO_ACCESS
665 if (bktr->bigbuf == 0 && cmd != BT848_GPIO_GET_EN &&
666 cmd != BT848_GPIO_SET_EN && cmd != BT848_GPIO_GET_DATA &&
667 cmd != BT848_GPIO_SET_DATA) /* no frame buffer allocated (ioctl failed) */
670 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
674 switch ( FUNCTION( minor(dev) ) ) {
676 return( video_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
678 return( tuner_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
689 bktr_mmap(struct dev_mmap_args *ap)
691 cdev_t dev = ap->a_head.a_dev;
695 unit = UNIT(minor(dev));
697 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
700 /* Get the device data */
701 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
703 /* the device is no longer valid/functioning */
707 if (ap->a_nprot & PROT_EXEC)
710 if (ap->a_offset < 0)
713 if (ap->a_offset >= bktr->alloc_pages * PAGE_SIZE)
716 ap->a_result = atop(vtophys(bktr->bigbuf) + ap->a_offset);
720 static struct filterops bktr_filterops =
721 { FILTEROP_ISFD, NULL, bktr_filter_detach, bktr_filter };
724 bktr_kqfilter(struct dev_kqfilter_args *ap)
726 cdev_t dev = ap->a_head.a_dev;
727 struct knote *kn = ap->a_kn;
734 switch (kn->kn_filter) {
736 if (FUNCTION(minor(dev)) == VBI_DEV) {
737 unit = UNIT(minor(dev));
738 /* Get the device data */
739 bktr = (struct bktr_softc *)
740 devclass_get_softc(bktr_devclass, unit);
741 kn->kn_fop = &bktr_filterops;
742 kn->kn_hook = (caddr_t)bktr;
747 ap->a_result = EOPNOTSUPP;
751 klist = &bktr->vbi_kq.ki_note;
752 knote_insert(klist, kn);
758 bktr_filter_detach(struct knote *kn)
760 bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
763 klist = &bktr->vbi_kq.ki_note;
764 knote_insert(klist, kn);
768 bktr_filter(struct knote *kn, long hint)
770 bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
774 /* the device is no longer valid/functioning */
775 kn->kn_flags |= (EV_EOF | EV_NODATA);
781 if (bktr->vbisize != 0)