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.14 2006/07/28 02:17:39 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>
57 #include <sys/device.h>
59 #include <sys/kernel.h>
60 #include <sys/signalvar.h>
61 #include <sys/malloc.h>
64 #include <sys/select.h>
65 #include <sys/vnode.h>
66 #include <sys/thread2.h>
69 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
73 #include <sys/bus.h> /* used by smbus and newbus */
75 #include <machine/bus_memio.h> /* used by bus space */
76 #include <machine/bus.h> /* used by bus space and newbus */
79 #include <sys/rman.h> /* used by newbus */
80 #include <machine/resource.h> /* used by newbus */
81 #include <bus/pci/pcivar.h>
82 #include <bus/pci/pcireg.h>
83 #include <bus/pci/pcidevs.h>
85 #include <sys/sysctl.h>
88 int bt848_reverse_mute = -1;
89 int bt848_format = -1;
90 int bt848_slow_msp_audio = -1;
91 #ifdef BKTR_NEW_MSP34XX_DRIVER
92 int bt848_stereo_once = 0; /* no continuous stereo monitoring */
93 int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
94 the autoscan seems work well only with FM... */
98 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
99 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
100 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
101 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
102 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
103 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
104 #ifdef BKTR_NEW_MSP34XX_DRIVER
105 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
106 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
107 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
110 #include <dev/video/meteor/ioctl_meteor.h>
111 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
112 #include <dev/video/bktr/bktr_reg.h>
113 #include <dev/video/bktr/bktr_tuner.h>
114 #include <dev/video/bktr/bktr_card.h>
115 #include <dev/video/bktr/bktr_audio.h>
116 #include <dev/video/bktr/bktr_core.h>
117 #include <dev/video/bktr/bktr_os.h>
119 #if defined(BKTR_USE_FREEBSD_SMBUS)
120 #include <dev/video/bktr/bktr_i2c.h>
122 #include "iicbb_if.h"
123 #include "smbus_if.h"
126 static int bktr_probe( device_t dev );
127 static int bktr_attach( device_t dev );
128 static int bktr_detach( device_t dev );
129 static int bktr_shutdown( device_t dev );
130 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
132 static device_method_t bktr_methods[] = {
133 /* Device interface */
134 DEVMETHOD(device_probe, bktr_probe),
135 DEVMETHOD(device_attach, bktr_attach),
136 DEVMETHOD(device_detach, bktr_detach),
137 DEVMETHOD(device_shutdown, bktr_shutdown),
139 #if defined(BKTR_USE_FREEBSD_SMBUS)
140 /* iicbb interface */
141 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
142 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
143 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
144 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
145 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
146 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
148 /* smbus interface */
149 DEVMETHOD(smbus_callback, bti2c_smb_callback),
150 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
151 DEVMETHOD(smbus_writew, bti2c_smb_writew),
152 DEVMETHOD(smbus_readb, bti2c_smb_readb),
158 static driver_t bktr_driver = {
161 sizeof(struct bktr_softc),
164 static devclass_t bktr_devclass;
166 static d_open_t bktr_open;
167 static d_close_t bktr_close;
168 static d_read_t bktr_read;
169 static d_write_t bktr_write;
170 static d_ioctl_t bktr_ioctl;
171 static d_mmap_t bktr_mmap;
172 static d_poll_t bktr_poll;
174 #define CDEV_MAJOR 92
175 static struct dev_ops bktr_ops = {
176 { "bktr", CDEV_MAJOR, 0 },
178 .d_close = bktr_close,
180 .d_write = bktr_write,
181 .d_ioctl = bktr_ioctl,
186 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
187 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
188 MODULE_VERSION(bktr, 1);
191 * the boot time probe routine.
194 bktr_probe( device_t dev )
196 unsigned int type = pci_get_devid(dev);
197 unsigned int rev = pci_get_revid(dev);
199 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
201 switch (PCI_PRODUCT(type)) {
202 case PCI_PRODUCT_BROOKTREE_BT848:
204 device_set_desc(dev, "BrookTree 848A");
206 device_set_desc(dev, "BrookTree 848");
208 case PCI_PRODUCT_BROOKTREE_BT849:
209 device_set_desc(dev, "BrookTree 849A");
211 case PCI_PRODUCT_BROOKTREE_BT878:
212 device_set_desc(dev, "BrookTree 878");
214 case PCI_PRODUCT_BROOKTREE_BT879:
215 device_set_desc(dev, "BrookTree 879");
225 * the attach routine.
228 bktr_attach( device_t dev )
237 u_long old_irq, new_irq;
240 struct bktr_softc *bktr = device_get_softc(dev);
242 unit = device_get_unit(dev);
244 /* build the device name for bktr_name() */
245 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
248 * Enable bus mastering and Memory Mapped device
250 val = pci_read_config(dev, PCIR_COMMAND, 4);
251 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
252 pci_write_config(dev, PCIR_COMMAND, val, 4);
255 * Map control/status registers.
257 bktr->mem_rid = PCIR_BAR(0);
258 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
259 &bktr->mem_rid, RF_ACTIVE);
261 if (!bktr->res_mem) {
262 device_printf(dev, "could not map memory\n");
266 bktr->memt = rman_get_bustag(bktr->res_mem);
267 bktr->memh = rman_get_bushandle(bktr->res_mem);
271 * Disable the brooktree device
273 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
274 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
277 #ifdef BROOKTREE_IRQ /* from the configuration file */
278 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
279 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
280 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
281 printf("bktr%d: attach: irq changed from %d to %d\n",
282 unit, (old_irq & 0xff), (new_irq & 0xff));
286 * Allocate our interrupt.
289 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
290 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
291 if (bktr->res_irq == NULL) {
292 device_printf(dev, "could not map interrupt\n");
297 error = bus_setup_intr(dev, bktr->res_irq, 0,
298 bktr_intr, bktr, &bktr->res_ih, NULL);
300 device_printf(dev, "could not setup irq\n");
306 /* Update the Device Control Register */
307 /* on Bt878 and Bt879 cards */
308 fun = pci_read_config( dev, 0x40, 2);
309 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
311 #if defined( BKTR_430_FX_MODE )
312 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
313 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
316 #if defined( BKTR_SIS_VIA_MODE )
317 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
318 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
319 OPTi chipset motherboards too */
321 pci_write_config(dev, 0x40, fun, 2);
323 #if defined(BKTR_USE_FREEBSD_SMBUS)
324 if (bt848_i2c_attach(dev))
325 printf("bktr%d: i2c_attach: can't attach\n", unit);
329 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
330 * you have more than four, then 16 would probably be a better value.
332 #ifndef BROOKTREE_DEF_LATENCY_VALUE
333 #define BROOKTREE_DEF_LATENCY_VALUE 10
335 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
336 latency = (latency >> 8) & 0xff;
339 printf("brooktree%d: PCI bus latency is", unit);
341 printf("brooktree%d: PCI bus latency was 0 changing to",
345 latency = BROOKTREE_DEF_LATENCY_VALUE;
346 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
349 printf(" %d.\n", (int) latency);
352 /* read the pci device id and revision id */
353 fun = pci_get_devid(dev);
354 rev = pci_get_revid(dev);
356 /* call the common attach code */
357 common_bktr_attach( bktr, unit, fun, rev );
359 /* make the device entries */
360 dev_ops_add(&bktr_ops, 0x0f, unit);
361 make_dev(&bktr_ops, unit, 0, 0, 0444, "bktr%d", unit);
362 make_dev(&bktr_ops, unit+16, 0, 0, 0444, "tuner%d", unit);
363 make_dev(&bktr_ops, unit+32, 0, 0, 0444, "vbi%d" , unit);
370 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
372 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
378 * the detach routine.
381 bktr_detach( device_t dev )
383 struct bktr_softc *bktr = device_get_softc(dev);
385 #ifdef BKTR_NEW_MSP34XX_DRIVER
386 /* Disable the soundchip and kernel thread */
387 if (bktr->msp3400c_info != NULL)
391 /* Disable the brooktree device */
392 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
393 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
395 #if defined(BKTR_USE_FREEBSD_SMBUS)
396 if (bt848_i2c_detach(dev))
397 printf("bktr%d: i2c_attach: can't attach\n",
398 device_get_unit(dev));
401 mtx_destroy(&bktr->vbimutex);
404 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
405 /* The memory is retained by the bktr_mem module so we can unload and */
406 /* then reload the main bktr driver module */
408 /* removing the ops automatically destroys all related devices */
409 dev_ops_remove(&bktr_ops, 0x0f, device_get_unit(dev));
412 * Deallocate resources.
414 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
415 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
416 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
422 * the shutdown routine.
425 bktr_shutdown( device_t dev )
427 struct bktr_softc *bktr = device_get_softc(dev);
429 /* Disable the brooktree device */
430 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
431 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
438 * Special Memory Allocation
441 get_bktr_mem( int unit, unsigned size )
443 vm_offset_t addr = 0;
445 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
446 0xffffffff, 1<<24, 0);
448 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
449 0xffffffff, PAGE_SIZE, 0);
451 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
459 /*---------------------------------------------------------
461 ** BrookTree 848 character device driver routines
463 **---------------------------------------------------------
466 #define VIDEO_DEV 0x00
467 #define TUNER_DEV 0x01
470 #define UNIT(x) ((x) & 0x0f)
471 #define FUNCTION(x) (x >> 4)
477 bktr_open(struct dev_open_args *ap)
479 dev_t dev = ap->a_head.a_dev;
484 unit = UNIT( minor(dev) );
486 /* Get the device data */
487 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
489 /* the device is no longer valid/functioning */
493 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
496 /* Record that the device is now busy */
497 device_busy(devclass_get_device(bktr_devclass, unit));
500 if (bt848_card != -1) {
501 if ((bt848_card >> 8 == unit ) &&
502 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
503 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
504 bktr->bt848_card = (bt848_card & 0xff);
505 probeCard(bktr, FALSE, unit);
510 if (bt848_tuner != -1) {
511 if ((bt848_tuner >> 8 == unit ) &&
512 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
513 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
514 bktr->bt848_tuner = (bt848_tuner & 0xff);
515 probeCard(bktr, FALSE, unit);
520 if (bt848_reverse_mute != -1) {
521 if ((bt848_reverse_mute >> 8) == unit ) {
522 bktr->reverse_mute = bt848_reverse_mute & 0xff;
526 if (bt848_slow_msp_audio != -1) {
527 if ((bt848_slow_msp_audio >> 8) == unit ) {
528 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
532 #ifdef BKTR_NEW_MSP34XX_DRIVER
533 if (bt848_stereo_once != 0) {
534 if ((bt848_stereo_once >> 8) == unit ) {
535 bktr->stereo_once = (bt848_stereo_once & 0xff);
539 if (bt848_amsound != -1) {
540 if ((bt848_amsound >> 8) == unit ) {
541 bktr->amsound = (bt848_amsound & 0xff);
545 if (bt848_dolby != -1) {
546 if ((bt848_dolby >> 8) == unit ) {
547 bktr->dolby = (bt848_dolby & 0xff);
552 switch ( FUNCTION( minor(dev) ) ) {
554 result = video_open( bktr );
557 result = tuner_open( bktr );
560 result = vbi_open( bktr );
567 /* If there was an error opening the device, undo the busy status */
569 device_unbusy(devclass_get_device(bktr_devclass, unit));
578 bktr_close(struct dev_close_args *ap)
580 dev_t dev = ap->a_head.a_dev;
585 unit = UNIT( minor(dev) );
587 /* Get the device data */
588 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
590 /* the device is no longer valid/functioning */
594 switch ( FUNCTION( minor(dev) ) ) {
596 result = video_close( bktr );
599 result = tuner_close( bktr );
602 result = vbi_close( bktr );
609 device_unbusy(devclass_get_device(bktr_devclass, unit));
618 bktr_read(struct dev_read_args *ap)
620 dev_t dev = ap->a_head.a_dev;
624 unit = UNIT(minor(dev));
626 /* Get the device data */
627 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
629 /* the device is no longer valid/functioning */
633 switch ( FUNCTION( minor(dev) ) ) {
635 return( video_read( bktr, unit, dev, ap->a_uio ) );
637 return( vbi_read( bktr, ap->a_uio, ap->a_ioflag ) );
647 bktr_write(struct dev_write_args *ap)
649 return( EINVAL ); /* XXX or ENXIO ? */
657 bktr_ioctl(struct dev_ioctl_args *ap)
659 dev_t dev = ap->a_head.a_dev;
663 unit = UNIT(minor(dev));
665 /* Get the device data */
666 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
668 /* the device is no longer valid/functioning */
672 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
675 switch ( FUNCTION( minor(dev) ) ) {
677 return( video_ioctl( bktr, unit, ap->a_cmd, ap->a_data, curthread ) );
679 return( tuner_ioctl( bktr, unit, ap->a_cmd, ap->a_data, curthread ) );
690 bktr_mmap(struct dev_mmap_args *ap)
692 dev_t dev = ap->a_head.a_dev;
696 unit = UNIT(minor(dev));
698 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
701 /* Get the device data */
702 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
704 /* the device is no longer valid/functioning */
708 if (ap->a_nprot & PROT_EXEC)
711 if (ap->a_offset < 0)
714 if (ap->a_offset >= bktr->alloc_pages * PAGE_SIZE)
717 ap->a_result = atop(vtophys(bktr->bigbuf) + ap->a_offset);
722 bktr_poll(struct dev_poll_args *ap)
724 dev_t dev = ap->a_head.a_dev;
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 (ap->a_events & (POLLIN | POLLRDNORM)) {
743 switch ( FUNCTION( minor(dev) ) ) {
745 if(bktr->vbisize == 0)
746 selrecord(curthread, &bktr->vbi_select);
748 revents |= ap->a_events & (POLLIN | POLLRDNORM);
756 ap->a_events = revents;