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.10 2004/05/19 22:52:52 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>
67 #include <vm/vm_kern.h>
69 #include <vm/vm_extern.h>
71 #include <sys/bus.h> /* used by smbus and newbus */
73 #include <machine/bus_memio.h> /* used by bus space */
74 #include <machine/bus.h> /* used by bus space and newbus */
77 #include <sys/rman.h> /* used by newbus */
78 #include <machine/resource.h> /* used by newbus */
79 #include <bus/pci/pcivar.h>
80 #include <bus/pci/pcireg.h>
81 #include <bus/pci/pcidevs.h>
83 #include <sys/sysctl.h>
86 int bt848_reverse_mute = -1;
87 int bt848_format = -1;
88 int bt848_slow_msp_audio = -1;
89 #ifdef BKTR_NEW_MSP34XX_DRIVER
90 int bt848_stereo_once = 0; /* no continuous stereo monitoring */
91 int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
92 the autoscan seems work well only with FM... */
96 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
97 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
98 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
99 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
100 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
101 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
102 #ifdef BKTR_NEW_MSP34XX_DRIVER
103 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
104 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
105 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
108 #include <dev/video/meteor/ioctl_meteor.h>
109 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
110 #include <dev/video/bktr/bktr_reg.h>
111 #include <dev/video/bktr/bktr_tuner.h>
112 #include <dev/video/bktr/bktr_card.h>
113 #include <dev/video/bktr/bktr_audio.h>
114 #include <dev/video/bktr/bktr_core.h>
115 #include <dev/video/bktr/bktr_os.h>
117 #if defined(BKTR_USE_FREEBSD_SMBUS)
118 #include <dev/video/bktr/bktr_i2c.h>
120 #include "iicbb_if.h"
121 #include "smbus_if.h"
124 static int bktr_probe( device_t dev );
125 static int bktr_attach( device_t dev );
126 static int bktr_detach( device_t dev );
127 static int bktr_shutdown( device_t dev );
128 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
130 static device_method_t bktr_methods[] = {
131 /* Device interface */
132 DEVMETHOD(device_probe, bktr_probe),
133 DEVMETHOD(device_attach, bktr_attach),
134 DEVMETHOD(device_detach, bktr_detach),
135 DEVMETHOD(device_shutdown, bktr_shutdown),
137 #if defined(BKTR_USE_FREEBSD_SMBUS)
138 /* iicbb interface */
139 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
140 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
141 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
142 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
143 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
144 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
146 /* smbus interface */
147 DEVMETHOD(smbus_callback, bti2c_smb_callback),
148 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
149 DEVMETHOD(smbus_writew, bti2c_smb_writew),
150 DEVMETHOD(smbus_readb, bti2c_smb_readb),
156 static driver_t bktr_driver = {
159 sizeof(struct bktr_softc),
162 static devclass_t bktr_devclass;
164 static d_open_t bktr_open;
165 static d_close_t bktr_close;
166 static d_read_t bktr_read;
167 static d_write_t bktr_write;
168 static d_ioctl_t bktr_ioctl;
169 static d_mmap_t bktr_mmap;
170 static d_poll_t bktr_poll;
172 #define CDEV_MAJOR 92
173 static struct cdevsw bktr_cdevsw = {
175 /* maj */ CDEV_MAJOR,
180 /* open */ bktr_open,
181 /* close */ bktr_close,
182 /* read */ bktr_read,
183 /* write */ bktr_write,
184 /* ioctl */ bktr_ioctl,
185 /* poll */ bktr_poll,
186 /* mmap */ bktr_mmap,
187 /* strategy */ nostrategy,
192 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
193 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
194 MODULE_VERSION(bktr, 1);
197 * the boot time probe routine.
200 bktr_probe( device_t dev )
202 unsigned int type = pci_get_devid(dev);
203 unsigned int rev = pci_get_revid(dev);
205 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
207 switch (PCI_PRODUCT(type)) {
208 case PCI_PRODUCT_BROOKTREE_BT848:
210 device_set_desc(dev, "BrookTree 848A");
212 device_set_desc(dev, "BrookTree 848");
214 case PCI_PRODUCT_BROOKTREE_BT849:
215 device_set_desc(dev, "BrookTree 849A");
217 case PCI_PRODUCT_BROOKTREE_BT878:
218 device_set_desc(dev, "BrookTree 878");
220 case PCI_PRODUCT_BROOKTREE_BT879:
221 device_set_desc(dev, "BrookTree 879");
231 * the attach routine.
234 bktr_attach( device_t dev )
243 u_long old_irq, new_irq;
246 struct bktr_softc *bktr = device_get_softc(dev);
248 unit = device_get_unit(dev);
250 /* build the device name for bktr_name() */
251 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
254 * Enable bus mastering and Memory Mapped device
256 val = pci_read_config(dev, PCIR_COMMAND, 4);
257 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
258 pci_write_config(dev, PCIR_COMMAND, val, 4);
261 * Map control/status registers.
263 bktr->mem_rid = PCIR_BAR(0);
264 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
265 &bktr->mem_rid, RF_ACTIVE);
267 if (!bktr->res_mem) {
268 device_printf(dev, "could not map memory\n");
272 bktr->memt = rman_get_bustag(bktr->res_mem);
273 bktr->memh = rman_get_bushandle(bktr->res_mem);
277 * Disable the brooktree device
279 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
280 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
283 #ifdef BROOKTREE_IRQ /* from the configuration file */
284 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
285 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
286 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
287 printf("bktr%d: attach: irq changed from %d to %d\n",
288 unit, (old_irq & 0xff), (new_irq & 0xff));
292 * Allocate our interrupt.
295 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
296 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
297 if (bktr->res_irq == NULL) {
298 device_printf(dev, "could not map interrupt\n");
303 error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
304 bktr_intr, bktr, &bktr->res_ih);
306 device_printf(dev, "could not setup irq\n");
312 /* Update the Device Control Register */
313 /* on Bt878 and Bt879 cards */
314 fun = pci_read_config( dev, 0x40, 2);
315 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
317 #if defined( BKTR_430_FX_MODE )
318 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
319 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
322 #if defined( BKTR_SIS_VIA_MODE )
323 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
324 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
325 OPTi chipset motherboards too */
327 pci_write_config(dev, 0x40, fun, 2);
329 #if defined(BKTR_USE_FREEBSD_SMBUS)
330 if (bt848_i2c_attach(dev))
331 printf("bktr%d: i2c_attach: can't attach\n", unit);
335 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
336 * you have more than four, then 16 would probably be a better value.
338 #ifndef BROOKTREE_DEF_LATENCY_VALUE
339 #define BROOKTREE_DEF_LATENCY_VALUE 10
341 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
342 latency = (latency >> 8) & 0xff;
345 printf("brooktree%d: PCI bus latency is", unit);
347 printf("brooktree%d: PCI bus latency was 0 changing to",
351 latency = BROOKTREE_DEF_LATENCY_VALUE;
352 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
355 printf(" %d.\n", (int) latency);
358 /* read the pci device id and revision id */
359 fun = pci_get_devid(dev);
360 rev = pci_get_revid(dev);
362 /* call the common attach code */
363 common_bktr_attach( bktr, unit, fun, rev );
365 /* make the device entries */
366 cdevsw_add(&bktr_cdevsw, 0x0f, unit);
367 make_dev(&bktr_cdevsw, unit, 0, 0, 0444, "bktr%d", unit);
368 make_dev(&bktr_cdevsw, unit+16, 0, 0, 0444, "tuner%d", unit);
369 make_dev(&bktr_cdevsw, unit+32, 0, 0, 0444, "vbi%d" , unit);
376 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
378 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
384 * the detach routine.
387 bktr_detach( device_t dev )
389 struct bktr_softc *bktr = device_get_softc(dev);
391 #ifdef BKTR_NEW_MSP34XX_DRIVER
392 /* Disable the soundchip and kernel thread */
393 if (bktr->msp3400c_info != NULL)
397 /* Disable the brooktree device */
398 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
399 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
401 #if defined(BKTR_USE_FREEBSD_SMBUS)
402 if (bt848_i2c_detach(dev))
403 printf("bktr%d: i2c_attach: can't attach\n",
404 device_get_unit(dev));
407 mtx_destroy(&bktr->vbimutex);
410 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
411 /* The memory is retained by the bktr_mem module so we can unload and */
412 /* then reload the main bktr driver module */
414 /* removing the cdevsw automatically destroys all related devices */
415 cdevsw_remove(&bktr_cdevsw, 0x0f, device_get_unit(dev));
418 * Deallocate resources.
420 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
421 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
422 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
428 * the shutdown routine.
431 bktr_shutdown( device_t dev )
433 struct bktr_softc *bktr = device_get_softc(dev);
435 /* Disable the brooktree device */
436 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
437 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
444 * Special Memory Allocation
447 get_bktr_mem( int unit, unsigned size )
449 vm_offset_t addr = 0;
451 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
452 0xffffffff, 1<<24, 0);
454 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
455 0xffffffff, PAGE_SIZE, 0);
457 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
465 /*---------------------------------------------------------
467 ** BrookTree 848 character device driver routines
469 **---------------------------------------------------------
472 #define VIDEO_DEV 0x00
473 #define TUNER_DEV 0x01
476 #define UNIT(x) ((x) & 0x0f)
477 #define FUNCTION(x) (x >> 4)
483 bktr_open( dev_t dev, int flags, int fmt, struct thread *td )
489 unit = UNIT( minor(dev) );
491 /* Get the device data */
492 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
494 /* the device is no longer valid/functioning */
498 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
501 /* Record that the device is now busy */
502 device_busy(devclass_get_device(bktr_devclass, unit));
505 if (bt848_card != -1) {
506 if ((bt848_card >> 8 == unit ) &&
507 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
508 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
509 bktr->bt848_card = (bt848_card & 0xff);
510 probeCard(bktr, FALSE, unit);
515 if (bt848_tuner != -1) {
516 if ((bt848_tuner >> 8 == unit ) &&
517 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
518 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
519 bktr->bt848_tuner = (bt848_tuner & 0xff);
520 probeCard(bktr, FALSE, unit);
525 if (bt848_reverse_mute != -1) {
526 if ((bt848_reverse_mute >> 8) == unit ) {
527 bktr->reverse_mute = bt848_reverse_mute & 0xff;
531 if (bt848_slow_msp_audio != -1) {
532 if ((bt848_slow_msp_audio >> 8) == unit ) {
533 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
537 #ifdef BKTR_NEW_MSP34XX_DRIVER
538 if (bt848_stereo_once != 0) {
539 if ((bt848_stereo_once >> 8) == unit ) {
540 bktr->stereo_once = (bt848_stereo_once & 0xff);
544 if (bt848_amsound != -1) {
545 if ((bt848_amsound >> 8) == unit ) {
546 bktr->amsound = (bt848_amsound & 0xff);
550 if (bt848_dolby != -1) {
551 if ((bt848_dolby >> 8) == unit ) {
552 bktr->dolby = (bt848_dolby & 0xff);
557 switch ( FUNCTION( minor(dev) ) ) {
559 result = video_open( bktr );
562 result = tuner_open( bktr );
565 result = vbi_open( bktr );
572 /* If there was an error opening the device, undo the busy status */
574 device_unbusy(devclass_get_device(bktr_devclass, unit));
583 bktr_close( dev_t dev, int flags, int fmt, struct thread *td )
589 unit = UNIT( minor(dev) );
591 /* Get the device data */
592 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
594 /* the device is no longer valid/functioning */
598 switch ( FUNCTION( minor(dev) ) ) {
600 result = video_close( bktr );
603 result = tuner_close( bktr );
606 result = vbi_close( bktr );
613 device_unbusy(devclass_get_device(bktr_devclass, unit));
622 bktr_read( dev_t dev, struct uio *uio, int ioflag )
627 unit = UNIT(minor(dev));
629 /* Get the device data */
630 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
632 /* the device is no longer valid/functioning */
636 switch ( FUNCTION( minor(dev) ) ) {
638 return( video_read( bktr, unit, dev, uio ) );
640 return( vbi_read( bktr, uio, ioflag ) );
650 bktr_write( dev_t dev, struct uio *uio, int ioflag )
652 return( EINVAL ); /* XXX or ENXIO ? */
660 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct thread *td )
665 unit = UNIT(minor(dev));
667 /* Get the device data */
668 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
670 /* the device is no longer valid/functioning */
674 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
677 switch ( FUNCTION( minor(dev) ) ) {
679 return( video_ioctl( bktr, unit, cmd, arg, td ) );
681 return( tuner_ioctl( bktr, unit, cmd, arg, td ) );
692 bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
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 (nprot & PROT_EXEC)
715 if (offset >= bktr->alloc_pages * PAGE_SIZE)
718 return(atop(vtophys(bktr->bigbuf) + offset));
722 bktr_poll( dev_t dev, int events, struct thread *td)
727 DECLARE_INTR_MASK(s);
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);