Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / bktr / bktr_os.c
blob60c0cf84f8493fd106a4d7f54821c201c590734d
1 /* $SourceForge: bktr_os.c,v 1.5 2003/03/11 23:11:25 thomasklausner Exp $ */
3 /* $NetBSD: bktr_os.c,v 1.56 2009/05/06 09:25:17 cegger Exp $ */
4 /* $FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.20 2000/10/20 08:16:53 roger Exp$ */
6 /*
7 * This is part of the Driver for Video Capture Cards (Frame grabbers)
8 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
9 * chipset.
10 * Copyright Roger Hardiman and Amancio Hasty.
12 * bktr_os : This has all the Operating System dependant code,
13 * probe/attach and open/close/ioctl/read/mmap
14 * memory allocation
15 * PCI bus interfacing
21 * 1. Redistributions of source code must retain the
22 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
23 * All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. All advertising materials mentioning features or use of this software
34 * must display the following acknowledgement:
35 * This product includes software developed by Amancio Hasty and
36 * Roger Hardiman
37 * 4. The name of the author may not be used to endorse or promote products
38 * derived from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
44 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
46 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
49 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: bktr_os.c,v 1.56 2009/05/06 09:25:17 cegger Exp $");
56 #ifdef __FreeBSD__
57 #include "bktr.h"
58 #endif /* __FreeBSD__ */
60 #include "opt_bktr.h" /* include any kernel config options */
62 #define FIFO_RISC_DISABLED 0
63 #define ALL_INTS_DISABLED 0
65 /*******************/
66 /* *** FreeBSD *** */
67 /*******************/
68 #ifdef __FreeBSD__
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/conf.h>
73 #include <sys/uio.h>
74 #include <sys/kernel.h>
75 #include <sys/signalvar.h>
76 #include <sys/mman.h>
77 #include <sys/poll.h>
78 #include <sys/select.h>
79 #include <sys/vnode.h>
81 #include <vm/vm.h>
82 #include <vm/vm_kern.h>
83 #include <vm/pmap.h>
84 #include <vm/vm_extern.h>
86 #if (__FreeBSD_version >=400000) || (NSMBUS > 0)
87 #include <sys/bus.h> /* used by smbus and newbus */
88 #endif
90 #if (__FreeBSD_version >=300000)
91 #include <machine/bus_memio.h> /* used by bus space */
92 #include <sys/bus.h> /* used by bus space and newbus */
93 #include <sys/bus.h>
94 #endif
96 #if (__FreeBSD_version >=400000)
97 #include <sys/rman.h> /* used by newbus */
98 #include <machine/resource.h> /* used by newbus */
99 #endif
101 #if (__FreeBSD_version < 500000)
102 #include <machine/clock.h> /* for DELAY */
103 #endif
105 #include <pci/pcivar.h>
106 #include <pci/pcireg.h>
108 #include <sys/sysctl.h>
109 int bt848_card = -1;
110 int bt848_tuner = -1;
111 int bt848_reverse_mute = -1;
112 int bt848_format = -1;
113 int bt848_slow_msp_audio = -1;
115 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
116 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
117 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
118 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
119 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
120 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
122 #if (__FreeBSD__ == 2)
123 #define PCIR_REVID PCI_CLASS_REG
124 #endif
126 #endif /* end freebsd section */
130 /****************/
131 /* *** BSDI *** */
132 /****************/
133 #ifdef __bsdi__
134 #endif /* __bsdi__ */
137 /**************************/
138 /* *** OpenBSD/NetBSD *** */
139 /**************************/
140 #if defined(__NetBSD__) || defined(__OpenBSD__)
142 #include <sys/param.h>
143 #include <sys/systm.h>
144 #include <sys/conf.h>
145 #include <sys/uio.h>
146 #include <sys/kernel.h>
147 #include <sys/signalvar.h>
148 #include <sys/mman.h>
149 #include <sys/poll.h>
150 #include <sys/select.h>
151 #include <sys/vnode.h>
153 #ifndef __NetBSD__
154 #include <vm/vm.h>
155 #include <vm/vm_kern.h>
156 #include <vm/pmap.h>
157 #include <vm/vm_extern.h>
158 #endif
160 #include <sys/device.h>
161 #include <dev/pci/pcivar.h>
162 #include <dev/pci/pcireg.h>
163 #include <dev/pci/pcidevs.h>
165 #define BKTR_DEBUG
166 #ifdef BKTR_DEBUG
167 int bktr_debug = 0;
168 #define DPR(x) if (bktr_debug) printf x
169 #else
170 #define DPR(x)
171 #endif
172 #endif /* __NetBSD__ || __OpenBSD__ */
174 #ifdef __NetBSD__
175 dev_type_open(bktr_open);
176 dev_type_close(bktr_close);
177 dev_type_read(bktr_read);
178 dev_type_write(bktr_write);
179 dev_type_ioctl(bktr_ioctl);
180 dev_type_mmap(bktr_mmap);
182 const struct cdevsw bktr_cdevsw = {
183 bktr_open, bktr_close, bktr_read, bktr_write, bktr_ioctl,
184 nostop, notty, nopoll, bktr_mmap, nokqfilter, D_OTHER
186 #endif /* __NetBSD __ */
188 #ifdef __NetBSD__
189 #include <dev/ic/bt8xx.h> /* NetBSD location for .h files */
190 #include <dev/pci/bktr/bktr_reg.h>
191 #include <dev/pci/bktr/bktr_tuner.h>
192 #include <dev/pci/bktr/bktr_card.h>
193 #include <dev/pci/bktr/bktr_audio.h>
194 #include <dev/pci/bktr/bktr_core.h>
195 #include <dev/pci/bktr/bktr_os.h>
196 #else /* Traditional location for .h files */
197 #include <machine/ioctl_meteor.h>
198 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
199 #include <dev/bktr/bktr_reg.h>
200 #include <dev/bktr/bktr_tuner.h>
201 #include <dev/bktr/bktr_card.h>
202 #include <dev/bktr/bktr_audio.h>
203 #include <dev/bktr/bktr_core.h>
204 #include <dev/bktr/bktr_os.h>
205 #if defined(BKTR_USE_FREEBSD_SMBUS)
206 #include <dev/bktr/bktr_i2c.h>
207 #endif
208 #endif
210 /* Support for radio(4) under NetBSD */
211 #ifdef __NetBSD__
212 #include "radio.h"
213 #if NRADIO > 0
214 #include <sys/radioio.h>
215 #include <dev/radio_if.h>
216 #endif
217 #else
218 #define NRADIO 0
219 #endif
221 /****************************/
222 /* *** FreeBSD 4.x code *** */
223 /****************************/
224 #if (__FreeBSD_version >= 400000)
226 static int bktr_probe(device_t dev);
227 static int bktr_attach(device_t dev);
228 static int bktr_detach(device_t dev);
229 static int bktr_shutdown(device_t dev);
230 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
232 static device_method_t bktr_methods[] = {
233 /* Device interface */
234 DEVMETHOD(device_probe, bktr_probe),
235 DEVMETHOD(device_attach, bktr_attach),
236 DEVMETHOD(device_detach, bktr_detach),
237 DEVMETHOD(device_shutdown, bktr_shutdown),
239 { 0, 0 }
242 static driver_t bktr_driver = {
243 "bktr",
244 bktr_methods,
245 sizeof(struct bktr_softc),
248 static devclass_t bktr_devclass;
250 static d_open_t bktr_open;
251 static d_close_t bktr_close;
252 static d_read_t bktr_read;
253 static d_write_t bktr_write;
254 static d_ioctl_t bktr_ioctl;
255 static d_mmap_t bktr_mmap;
256 static d_poll_t bktr_poll;
258 #define CDEV_MAJOR 92
259 static struct cdevsw bktr_cdevsw = {
260 /* open */ bktr_open,
261 /* close */ bktr_close,
262 /* read */ bktr_read,
263 /* write */ bktr_write,
264 /* ioctl */ bktr_ioctl,
265 /* poll */ bktr_poll,
266 /* mmap */ bktr_mmap,
267 /* strategy */ nostrategy,
268 /* name */ "bktr",
269 /* maj */ CDEV_MAJOR,
270 /* dump */ nodump,
271 /* psize */ nopsize,
272 /* flags */ 0,
273 /* bmaj */ -1
276 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
277 #if (__FreeBSD_version > 410000)
278 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
279 MODULE_VERSION(bktr, 1);
280 #endif
284 * the boot time probe routine.
286 static int
287 bktr_probe(device_t dev)
289 unsigned int type = pci_get_devid(dev);
290 unsigned int rev = pci_get_revid(dev);
292 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
294 switch (PCI_PRODUCT(type)) {
295 case PCI_PRODUCT_BROOKTREE_BT848:
296 if (rev == 0x12)
297 device_set_desc(dev, "BrookTree 848A");
298 else
299 device_set_desc(dev, "BrookTree 848");
300 return 0;
301 case PCI_PRODUCT_BROOKTREE_BT849:
302 device_set_desc(dev, "BrookTree 849A");
303 return 0;
304 case PCI_PRODUCT_BROOKTREE_BT878:
305 device_set_desc(dev, "BrookTree 878");
306 return 0;
307 case PCI_PRODUCT_BROOKTREE_BT879:
308 device_set_desc(dev, "BrookTree 879");
309 return 0;
313 return ENXIO;
318 * the attach routine.
320 static int
321 bktr_attach(device_t dev)
323 u_int latency;
324 u_int fun;
325 u_int val;
326 unsigned int rev;
327 unsigned int unit;
328 int error = 0;
329 #ifdef BROOKTREE_IRQ
330 u_int old_irq, new_irq;
331 #endif
333 struct bktr_softc *bktr = device_get_softc(dev);
335 unit = device_get_unit(dev);
337 /* build the device name for bktr_name() */
338 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
341 * Enable bus mastering and Memory Mapped device
343 val = pci_read_config(dev, PCIR_COMMAND, 4);
344 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
345 pci_write_config(dev, PCIR_COMMAND, val, 4);
348 * Map control/status registers.
350 bktr->mem_rid = PCIR_MAPS;
351 bktr->res_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &bktr->mem_rid,
352 0, ~0, 1, RF_ACTIVE);
355 if (!bktr->res_mem) {
356 device_printf(dev, "could not map memory\n");
357 error = ENXIO;
358 goto fail;
360 bktr->memt = rman_get_bustag(bktr->res_mem);
361 bktr->memh = rman_get_bushandle(bktr->res_mem);
365 * Disable the brooktree device
367 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
368 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
371 #ifdef BROOKTREE_IRQ /* from the configuration file */
372 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
373 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
374 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
375 printf("bktr%d: attach: irq changed from %d to %d\n",
376 unit, (old_irq & 0xff), (new_irq & 0xff));
377 #endif
380 * Allocate our interrupt.
382 bktr->irq_rid = 0;
383 bktr->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &bktr->irq_rid,
384 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
385 if (bktr->res_irq == NULL) {
386 device_printf(dev, "could not map interrupt\n");
387 error = ENXIO;
388 goto fail;
391 error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
392 bktr_intr, bktr, &bktr->res_ih);
393 if (error) {
394 device_printf(dev, "could not setup irq\n");
395 goto fail;
400 /* Update the Device Control Register */
401 /* on Bt878 and Bt879 cards */
402 fun = pci_read_config(dev, 0x40, 2);
403 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
405 #if defined(BKTR_430_FX_MODE)
406 if (bootverbose) printf("Using 430 FX chipset compatibility mode\n");
407 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
408 #endif
410 #if defined(BKTR_SIS_VIA_MODE)
411 if (bootverbose) printf("Using SiS/VIA chipset compatibility mode\n");
412 fun = fun | 4; /* Enable SiS/VIA compatibility mode (useful for
413 OPTi chipset motherboards too */
414 #endif
415 pci_write_config(dev, 0x40, fun, 2);
418 /* XXX call bt848_i2c dependent attach() routine */
419 #if defined(BKTR_USE_FREEBSD_SMBUS)
420 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
421 printf("bktr%d: i2c_attach: can't attach\n", unit);
422 #endif
426 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
427 * you have more than four, then 16 would probably be a better value.
429 #ifndef BROOKTREE_DEF_LATENCY_VALUE
430 #define BROOKTREE_DEF_LATENCY_VALUE 10
431 #endif
432 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
433 latency = (latency >> 8) & 0xff;
434 if (bootverbose) {
435 if (latency)
436 printf("brooktree%d: PCI bus latency is", unit);
437 else
438 printf("brooktree%d: PCI bus latency was 0 changing to",
439 unit);
441 if (!latency) {
442 latency = BROOKTREE_DEF_LATENCY_VALUE;
443 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
445 if (bootverbose) {
446 printf(" %d.\n", (int) latency);
449 /* read the pci device id and revision id */
450 fun = pci_get_devid(dev);
451 rev = pci_get_revid(dev);
453 /* call the common attach code */
454 if (common_bktr_attach(bktr, unit, fun, rev) == 0)
455 return;
457 /* make the device entries */
458 bktr->bktrdev = make_dev(&bktr_cdevsw, unit,
459 0, 0, 0444, "bktr%d", unit);
460 bktr->tunerdev= make_dev(&bktr_cdevsw, unit+16,
461 0, 0, 0444, "tuner%d", unit);
462 bktr->vbidev = make_dev(&bktr_cdevsw, unit+32,
463 0, 0, 0444, "vbi%d" , unit);
466 /* if this is unit 0 (/dev/bktr0, /dev/tuner0, /dev/vbi0) then make */
467 /* alias entries to /dev/bktr /dev/tuner and /dev/vbi */
468 #if (__FreeBSD_version >=500000)
469 if (unit == 0) {
470 bktr->bktrdev_alias = make_dev_alias(bktr->bktrdev, "bktr");
471 bktr->tunerdev_alias= make_dev_alias(bktr->tunerdev, "tuner");
472 bktr->vbidev_alias = make_dev_alias(bktr->vbidev, "vbi");
474 #endif
476 return 0;
478 fail:
479 if (bktr->res_irq)
480 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
481 if (bktr->res_mem)
482 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
483 return error;
488 * the detach routine.
490 static int
491 bktr_detach(device_t dev)
493 unsigned int unit;
495 struct bktr_softc *bktr = device_get_softc(dev);
497 unit = device_get_unit(dev);
499 /* Disable the brooktree device */
500 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
501 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
503 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
504 /* The memory is retained by the bktr_mem module so we can unload and */
505 /* then reload the main bktr driver module */
507 /* Unregister the /dev/bktrN, tunerN and vbiN devices */
508 destroy_dev(bktr->vbidev);
509 destroy_dev(bktr->tunerdev);
510 destroy_dev(bktr->bktrdev);
512 /* If this is unit 0, then destroy the alias entries too */
513 #if (__FreeBSD_version >=500000)
514 if (unit == 0) {
515 destroy_dev(bktr->vbidev_alias);
516 destroy_dev(bktr->tunerdev_alias);
517 destroy_dev(bktr->bktrdev_alias);
519 #endif
520 seldestroy(&bktr->vbi_select);
523 * Deallocate resources.
525 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
526 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
527 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
529 return 0;
533 * the shutdown routine.
535 static int
536 bktr_shutdown(device_t dev)
538 struct bktr_softc *bktr = device_get_softc(dev);
540 /* Disable the brooktree device */
541 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
542 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
544 return 0;
549 * Special Memory Allocation
551 vm_offset_t
552 get_bktr_mem(int unit, unsigned size)
554 vm_offset_t addr = 0;
556 addr = vm_page_alloc_contig(size, 0, 0xffffffff, 1<<24);
557 if (addr == 0)
558 addr = vm_page_alloc_contig(size, 0, 0xffffffff, PAGE_SIZE);
559 if (addr == 0) {
560 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
561 unit, size);
564 return(addr);
568 /*---------------------------------------------------------
570 ** BrookTree 848 character device driver routines
572 **---------------------------------------------------------
575 #define VIDEO_DEV 0x00
576 #define TUNER_DEV 0x01
577 #define VBI_DEV 0x02
579 #define UNIT(x) ((x) & 0x0f)
580 #define FUNCTION(x) (x >> 4)
586 bktr_open(dev_t dev, int flags, int fmt, struct lwp *l)
588 bktr_ptr_t bktr;
589 int unit;
590 int result;
592 unit = UNIT(minor(dev));
594 /* Get the device data */
595 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
596 if (bktr == NULL) {
597 /* the device is no longer valid/functioning */
598 return (ENXIO);
601 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
602 return(ENXIO);
604 /* Record that the device is now busy */
605 device_busy(devclass_get_device(bktr_devclass, unit));
608 if (bt848_card != -1) {
609 if ((bt848_card >> 8 == unit) &&
610 ((bt848_card & 0xff) < Bt848_MAX_CARD)) {
611 if (bktr->bt848_card != (bt848_card & 0xff)) {
612 bktr->bt848_card = (bt848_card & 0xff);
613 probeCard(bktr, FALSE, unit);
618 if (bt848_tuner != -1) {
619 if ((bt848_tuner >> 8 == unit) &&
620 ((bt848_tuner & 0xff) < Bt848_MAX_TUNER)) {
621 if (bktr->bt848_tuner != (bt848_tuner & 0xff)) {
622 bktr->bt848_tuner = (bt848_tuner & 0xff);
623 probeCard(bktr, FALSE, unit);
628 if (bt848_reverse_mute != -1) {
629 if ((bt848_reverse_mute >> 8) == unit) {
630 bktr->reverse_mute = bt848_reverse_mute & 0xff;
634 if (bt848_slow_msp_audio != -1) {
635 if ((bt848_slow_msp_audio >> 8) == unit) {
636 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
640 switch (FUNCTION(minor(dev))) {
641 case VIDEO_DEV:
642 result = video_open(bktr);
643 break;
644 case TUNER_DEV:
645 result = tuner_open(bktr);
646 break;
647 case VBI_DEV:
648 result = vbi_open(bktr);
649 break;
650 default:
651 result = ENXIO;
652 break;
655 /* If there was an error opening the device, undo the busy status */
656 if (result != 0)
657 device_unbusy(devclass_get_device(bktr_devclass, unit));
658 return(result);
666 bktr_close(dev_t dev, int flags, int fmt, struct lwp *l)
668 bktr_ptr_t bktr;
669 int unit;
670 int result;
672 unit = UNIT(minor(dev));
674 /* Get the device data */
675 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
676 if (bktr == NULL) {
677 /* the device is no longer valid/functioning */
678 return (ENXIO);
681 switch (FUNCTION(minor(dev))) {
682 case VIDEO_DEV:
683 result = video_close(bktr);
684 break;
685 case TUNER_DEV:
686 result = tuner_close(bktr);
687 break;
688 case VBI_DEV:
689 result = vbi_close(bktr);
690 break;
691 default:
692 return (ENXIO);
693 break;
696 device_unbusy(devclass_get_device(bktr_devclass, unit));
697 return(result);
705 bktr_read(dev_t dev, struct uio *uio, int ioflag)
707 bktr_ptr_t bktr;
708 int unit;
710 unit = UNIT(minor(dev));
712 /* Get the device data */
713 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
714 if (bktr == NULL) {
715 /* the device is no longer valid/functioning */
716 return (ENXIO);
719 switch (FUNCTION(minor(dev))) {
720 case VIDEO_DEV:
721 return(video_read(bktr, unit, dev, uio));
722 case VBI_DEV:
723 return(vbi_read(bktr, uio, ioflag));
725 return(ENXIO);
733 bktr_write(dev_t dev, struct uio *uio, int ioflag)
735 return(EINVAL); /* XXX or ENXIO ? */
743 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, void *arg, int flag, struct proc* pr)
745 bktr_ptr_t bktr;
746 int unit;
748 unit = UNIT(minor(dev));
750 /* Get the device data */
751 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
752 if (bktr == NULL) {
753 /* the device is no longer valid/functioning */
754 return (ENXIO);
757 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
758 return(ENOMEM);
760 switch (FUNCTION(minor(dev))) {
761 case VIDEO_DEV:
762 return(video_ioctl(bktr, unit, cmd, arg, pr));
763 case TUNER_DEV:
764 return(tuner_ioctl(bktr, unit, cmd, arg, pr));
767 return(ENXIO);
775 bktr_mmap(dev_t dev, vm_offset_t offset, int nprot)
777 int unit;
778 bktr_ptr_t bktr;
780 unit = UNIT(minor(dev));
782 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
783 return(-1);
785 /* Get the device data */
786 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
787 if (bktr == NULL) {
788 /* the device is no longer valid/functioning */
789 return (-1);
792 if (nprot & PROT_EXEC)
793 return(-1);
795 if (offset < 0)
796 return(-1);
798 if (offset >= bktr->alloc_pages * PAGE_SIZE)
799 return(-1);
801 return(atop(vtophys(bktr->bigbuf) + offset));
804 int bktr_poll(dev_t dev, int events, struct lwp *l)
806 int unit;
807 bktr_ptr_t bktr;
808 int revents = 0;
809 DECLARE_INTR_MASK(s);
811 unit = UNIT(minor(dev));
813 /* Get the device data */
814 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
815 if (bktr == NULL) {
816 /* the device is no longer valid/functioning */
817 return (ENXIO);
820 DISABLE_INTR(s);
822 if (events & (POLLIN | POLLRDNORM)) {
824 switch (FUNCTION(minor(dev))) {
825 case VBI_DEV:
826 if(bktr->vbisize == 0)
827 selrecord(p, &bktr->vbi_select);
828 else
829 revents |= events & (POLLIN | POLLRDNORM);
830 break;
834 ENABLE_INTR(s);
836 return (revents);
839 #endif /* FreeBSD 4.x specific kernel interface routines */
841 /**********************************/
842 /* *** FreeBSD 2.2.x and 3.x *** */
843 /**********************************/
845 #if ((__FreeBSD__ == 2) || (__FreeBSD__ == 3))
847 static bktr_reg_t brooktree[NBKTR];
849 static const char* bktr_probe(pcici_t tag, pcidi_t type);
850 static void bktr_attach(pcici_t tag, int unit);
851 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
853 static u_int bktr_count;
855 static struct pci_device bktr_device = {
856 "bktr",
857 bktr_probe,
858 bktr_attach,
859 &bktr_count
862 DATA_SET (pcidevice_set, bktr_device);
864 static d_open_t bktr_open;
865 static d_close_t bktr_close;
866 static d_read_t bktr_read;
867 static d_write_t bktr_write;
868 static d_ioctl_t bktr_ioctl;
869 static d_mmap_t bktr_mmap;
870 static d_poll_t bktr_poll;
872 #define CDEV_MAJOR 92
873 static struct cdevsw bktr_cdevsw =
875 bktr_open, bktr_close, bktr_read, bktr_write,
876 bktr_ioctl, nostop, nullreset, nodevtotty,
877 bktr_poll, bktr_mmap, NULL, "bktr",
878 NULL, -1
881 static int bktr_devsw_installed;
883 static void
884 bktr_drvinit(void *unused)
886 dev_t dev;
888 if (! bktr_devsw_installed) {
889 dev = makedev(CDEV_MAJOR, 0);
890 cdevsw_add(&dev,&bktr_cdevsw, NULL);
891 bktr_devsw_installed = 1;
895 SYSINIT(bktrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bktr_drvinit,NULL)
898 * the boot time probe routine.
900 static const char*
901 bktr_probe(pcici_t tag, pcidi_t type)
903 unsigned int rev = pci_conf_read(tag, PCIR_REVID) & 0x000000ff;
905 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
907 switch (PCI_PRODUCT(type)) {
908 case PCI_PRODUCT_BROOKTREE_BT848:
909 if (rev == 0x12) return("BrookTree 848A");
910 else return("BrookTree 848");
911 case PCI_PRODUCT_BROOKTREE_BT849:
912 return("BrookTree 849A");
913 case PCI_PRODUCT_BROOKTREE_BT878:
914 return("BrookTree 878");
915 case PCI_PRODUCT_BROOKTREE_BT879:
916 return("BrookTree 879");
920 return ((char *)0);
924 * the attach routine.
926 static void
927 bktr_attach(pcici_t tag, int unit)
929 bktr_ptr_t bktr;
930 u_int latency;
931 u_int fun;
932 unsigned int rev;
933 unsigned long base;
934 #ifdef BROOKTREE_IRQ
935 u_int old_irq, new_irq;
936 #endif
938 bktr = &brooktree[unit];
940 if (unit >= NBKTR) {
941 printf("brooktree%d: attach: only %d units configured.\n",
942 unit, NBKTR);
943 printf("brooktree%d: attach: invalid unit number.\n", unit);
944 return;
947 /* build the device name for bktr_name() */
948 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
950 /* Enable Memory Mapping */
951 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
952 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 2);
954 /* Enable Bus Mastering */
955 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
956 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 4);
958 bktr->tag = tag;
962 * Map control/status registers
964 pci_map_mem(tag, PCI_MAP_REG_START, (vm_offset_t *) &base,
965 &bktr->phys_base);
966 #if (__FreeBSD_version >= 300000)
967 bktr->memt = I386_BUS_SPACE_MEM; /* XXX should use proper bus space */
968 bktr->memh = (bus_space_handle_t)base; /* XXX functions here */
969 #endif
972 * Disable the brooktree device
974 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
975 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
977 #ifdef BROOKTREE_IRQ /* from the configuration file */
978 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
979 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
980 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
981 printf("bktr%d: attach: irq changed from %d to %d\n",
982 unit, (old_irq & 0xff), (new_irq & 0xff));
983 #endif
986 * setup the interrupt handling routine
988 pci_map_int(tag, bktr_intr, (void*) bktr, &tty_imask);
991 /* Update the Device Control Register */
992 /* on Bt878 and Bt879 cards */
993 fun = pci_conf_read(tag, 0x40);
994 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
996 #if defined(BKTR_430_FX_MODE)
997 if (bootverbose) printf("Using 430 FX chipset compatibility mode\n");
998 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
999 #endif
1001 #if defined(BKTR_SIS_VIA_MODE)
1002 if (bootverbose) printf("Using SiS/VIA chipset compatibility mode\n");
1003 fun = fun | 4; /* Enable SiS/VIA compatibility mode (useful for
1004 OPTi chipset motherboards too */
1005 #endif
1006 pci_conf_write(tag, 0x40, fun);
1009 /* XXX call bt848_i2c dependent attach() routine */
1010 #if defined(BKTR_USE_FREEBSD_SMBUS)
1011 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
1012 printf("bktr%d: i2c_attach: can't attach\n", unit);
1013 #endif
1017 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
1018 * you have more than four, then 16 would probably be a better value.
1020 #ifndef BROOKTREE_DEF_LATENCY_VALUE
1021 #define BROOKTREE_DEF_LATENCY_VALUE 10
1022 #endif
1023 latency = pci_conf_read(tag, PCI_LATENCY_TIMER);
1024 latency = (latency >> 8) & 0xff;
1025 if (bootverbose) {
1026 if (latency)
1027 printf("brooktree%d: PCI bus latency is", unit);
1028 else
1029 printf("brooktree%d: PCI bus latency was 0 changing to",
1030 unit);
1032 if (!latency) {
1033 latency = BROOKTREE_DEF_LATENCY_VALUE;
1034 pci_conf_write(tag, PCI_LATENCY_TIMER, latency<<8);
1036 if (bootverbose) {
1037 printf(" %d.\n", (int) latency);
1041 /* read the pci device id and revision id */
1042 fun = pci_conf_read(tag, PCI_ID_REG);
1043 rev = pci_conf_read(tag, PCIR_REVID) & 0x000000ff;
1045 /* call the common attach code */
1046 common_bktr_attach(bktr, unit, fun, rev);
1052 * Special Memory Allocation
1054 vm_offset_t
1055 get_bktr_mem(int unit, unsigned size)
1057 vm_offset_t addr = 0;
1059 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff, 1<<24);
1060 if (addr == 0)
1061 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff,
1062 PAGE_SIZE);
1063 if (addr == 0) {
1064 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
1065 unit, size);
1068 return(addr);
1071 /*---------------------------------------------------------
1073 ** BrookTree 848 character device driver routines
1075 **---------------------------------------------------------
1079 #define VIDEO_DEV 0x00
1080 #define TUNER_DEV 0x01
1081 #define VBI_DEV 0x02
1083 #define UNIT(x) ((x) & 0x0f)
1084 #define FUNCTION(x) ((x >> 4) & 0x0f)
1091 bktr_open(dev_t dev, int flags, int fmt, struct lwp *l)
1093 bktr_ptr_t bktr;
1094 int unit;
1096 unit = UNIT(minor(dev));
1097 if (unit >= NBKTR) /* unit out of range */
1098 return(ENXIO);
1100 bktr = &(brooktree[unit]);
1102 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
1103 return(ENXIO);
1106 if (bt848_card != -1) {
1107 if ((bt848_card >> 8 == unit) &&
1108 ((bt848_card & 0xff) < Bt848_MAX_CARD)) {
1109 if (bktr->bt848_card != (bt848_card & 0xff)) {
1110 bktr->bt848_card = (bt848_card & 0xff);
1111 probeCard(bktr, FALSE, unit);
1116 if (bt848_tuner != -1) {
1117 if ((bt848_tuner >> 8 == unit) &&
1118 ((bt848_tuner & 0xff) < Bt848_MAX_TUNER)) {
1119 if (bktr->bt848_tuner != (bt848_tuner & 0xff)) {
1120 bktr->bt848_tuner = (bt848_tuner & 0xff);
1121 probeCard(bktr, FALSE, unit);
1126 if (bt848_reverse_mute != -1) {
1127 if ((bt848_reverse_mute >> 8) == unit) {
1128 bktr->reverse_mute = bt848_reverse_mute & 0xff;
1132 if (bt848_slow_msp_audio != -1) {
1133 if ((bt848_slow_msp_audio >> 8) == unit) {
1134 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
1138 switch (FUNCTION(minor(dev))) {
1139 case VIDEO_DEV:
1140 return(video_open(bktr));
1141 case TUNER_DEV:
1142 return(tuner_open(bktr));
1143 case VBI_DEV:
1144 return(vbi_open(bktr));
1146 return(ENXIO);
1154 bktr_close(dev_t dev, int flags, int fmt, struct lwp *l)
1156 bktr_ptr_t bktr;
1157 int unit;
1159 unit = UNIT(minor(dev));
1160 if (unit >= NBKTR) /* unit out of range */
1161 return(ENXIO);
1163 bktr = &(brooktree[unit]);
1165 switch (FUNCTION(minor(dev))) {
1166 case VIDEO_DEV:
1167 return(video_close(bktr));
1168 case TUNER_DEV:
1169 return(tuner_close(bktr));
1170 case VBI_DEV:
1171 return(vbi_close(bktr));
1174 return(ENXIO);
1181 bktr_read(dev_t dev, struct uio *uio, int ioflag)
1183 bktr_ptr_t bktr;
1184 int unit;
1186 unit = UNIT(minor(dev));
1187 if (unit >= NBKTR) /* unit out of range */
1188 return(ENXIO);
1190 bktr = &(brooktree[unit]);
1192 switch (FUNCTION(minor(dev))) {
1193 case VIDEO_DEV:
1194 return(video_read(bktr, unit, dev, uio));
1195 case VBI_DEV:
1196 return(vbi_read(bktr, uio, ioflag));
1198 return(ENXIO);
1206 bktr_write(dev_t dev, struct uio *uio, int ioflag)
1208 return(EINVAL); /* XXX or ENXIO ? */
1215 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, void *arg, int flag, struct proc* pr)
1217 bktr_ptr_t bktr;
1218 int unit;
1220 unit = UNIT(minor(dev));
1221 if (unit >= NBKTR) /* unit out of range */
1222 return(ENXIO);
1224 bktr = &(brooktree[unit]);
1226 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1227 return(ENOMEM);
1229 switch (FUNCTION(minor(dev))) {
1230 case VIDEO_DEV:
1231 return(video_ioctl(bktr, unit, cmd, arg, pr));
1232 case TUNER_DEV:
1233 return(tuner_ioctl(bktr, unit, cmd, arg, pr));
1236 return(ENXIO);
1240 * bktr_mmap.
1241 * Note: 2.2.5/2.2.6/2.2.7/3.0 users must manually
1242 * edit the line below and change "vm_offset_t" to "int"
1244 int bktr_mmap(dev_t dev, vm_offset_t offset, int nprot)
1247 int unit;
1248 bktr_ptr_t bktr;
1250 unit = UNIT(minor(dev));
1252 if (unit >= NBKTR || FUNCTION(minor(dev)) > 0)
1253 return(-1);
1255 bktr = &(brooktree[unit]);
1257 if (nprot & PROT_EXEC)
1258 return(-1);
1260 if (offset < 0)
1261 return(-1);
1263 if (offset >= bktr->alloc_pages * PAGE_SIZE)
1264 return(-1);
1266 return(i386_btop(vtophys(bktr->bigbuf) + offset));
1269 int bktr_poll(dev_t dev, int events, struct lwp *l)
1271 int unit;
1272 bktr_ptr_t bktr;
1273 int revents = 0;
1275 unit = UNIT(minor(dev));
1277 if (unit >= NBKTR)
1278 return(-1);
1280 bktr = &(brooktree[unit]);
1282 disable_intr();
1284 if (events & (POLLIN | POLLRDNORM)) {
1286 switch (FUNCTION(minor(dev))) {
1287 case VBI_DEV:
1288 if(bktr->vbisize == 0)
1289 selrecord(p, &bktr->vbi_select);
1290 else
1291 revents |= events & (POLLIN | POLLRDNORM);
1292 break;
1296 enable_intr();
1298 return (revents);
1302 #endif /* FreeBSD 2.2.x and 3.x specific kernel interface routines */
1305 /*****************/
1306 /* *** BSDI *** */
1307 /*****************/
1309 #if defined(__bsdi__)
1310 #endif /* __bsdi__ BSDI specific kernel interface routines */
1313 /*****************************/
1314 /* *** OpenBSD / NetBSD *** */
1315 /*****************************/
1316 #if defined(__NetBSD__) || defined(__OpenBSD__)
1318 #define IPL_VIDEO IPL_BIO /* XXX */
1320 static int bktr_intr(void *arg) { return common_bktr_intr(arg); }
1322 #if defined(__OpenBSD__)
1323 #define bktr_open bktropen
1324 #define bktr_close bktrclose
1325 #define bktr_read bktrread
1326 #define bktr_write bktrwrite
1327 #define bktr_ioctl bktrioctl
1328 #define bktr_mmap bktrmmap
1330 static int bktr_probe(struct device *, void *, void *);
1331 #else
1332 static int bktr_probe(device_t, cfdata_t, void *);
1333 #endif
1334 static void bktr_attach(device_t, device_t, void *);
1336 CFATTACH_DECL(bktr, sizeof(struct bktr_softc),
1337 bktr_probe, bktr_attach, NULL, NULL);
1339 #if defined(__NetBSD__)
1340 extern struct cfdriver bktr_cd;
1341 #else
1342 struct cfdriver bktr_cd = {
1343 NULL, "bktr", DV_DULL
1345 #endif
1348 #if NRADIO > 0
1349 /* for radio(4) */
1350 int bktr_get_info(void *, struct radio_info *);
1351 int bktr_set_info(void *, struct radio_info *);
1353 static struct radio_hw_if bktr_hw_if = {
1354 NULL, /* open */
1355 NULL, /* close */
1356 bktr_get_info,
1357 bktr_set_info,
1358 NULL /* search */
1360 #endif
1363 bktr_probe(device_t parent, cfdata_t match, void *aux)
1365 struct pci_attach_args *pa = aux;
1367 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROOKTREE &&
1368 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT848 ||
1369 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT849 ||
1370 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT878 ||
1371 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT879))
1372 return 1;
1374 return 0;
1379 * the attach routine.
1381 static void
1382 bktr_attach(device_t parent, device_t self, void *aux)
1384 bktr_ptr_t bktr;
1385 u_int latency;
1387 #if defined(__OpenBSD__)
1388 u_int fun;
1389 unsigned int rev;
1390 struct pci_attach_args *pa = aux;
1391 pci_chipset_tag_t pc = pa->pa_pc;
1393 pci_intr_handle_t ih;
1394 const char *intrstr;
1395 int retval;
1396 int unit;
1398 bktr = (bktr_ptr_t)self;
1399 unit = bktr->bktr_dev.dv_unit;
1401 bktr->pc = pa->pa_pc;
1402 bktr->tag = pa->pa_tag;
1403 bktr->dmat = pa->pa_dmat;
1406 * map memory
1408 bktr->memt = pa->pa_memt;
1409 retval = pci_mem_find(pc, pa->pa_tag, PCI_MAPREG_START,
1410 &bktr->phys_base, &bktr->obmemsz, NULL);
1411 if (!retval)
1412 retval = bus_space_map(pa->pa_memt, bktr->phys_base,
1413 bktr->obmemsz, 0, &bktr->memh);
1414 if (retval) {
1415 printf(": couldn't map memory\n");
1416 return;
1421 * map interrupt
1423 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1424 pa->pa_intrline, &ih)) {
1425 printf(": couldn't map interrupt\n");
1426 return;
1428 intrstr = pci_intr_string(pa->pa_pc, ih);
1430 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1431 bktr_intr, bktr, device_xname(&bktr->bktr_dev));
1432 if (bktr->ih == NULL) {
1433 printf(": couldn't establish interrupt");
1434 if (intrstr != NULL)
1435 printf(" at %s", intrstr);
1436 printf("\n");
1437 return;
1440 if (intrstr != NULL)
1441 printf(": %s\n", intrstr);
1442 #endif /* __OpenBSD__ */
1444 #if defined(__NetBSD__)
1445 struct pci_attach_args *pa = aux;
1446 pci_intr_handle_t ih;
1447 pcireg_t command;
1448 const char *intrstr;
1449 int retval;
1450 int unit;
1452 bktr = (bktr_ptr_t)self;
1453 unit = device_unit(&bktr->bktr_dev);
1454 bktr->dmat = pa->pa_dmat;
1456 printf("\n");
1458 /* Enable Bus Master
1459 XXX: check if all old DMA is stopped first (e.g. after warm
1460 boot) */
1461 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1462 command |= PCI_COMMAND_MASTER_ENABLE;
1463 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1466 * map memory
1468 retval = pci_mapreg_map(pa, PCI_MAPREG_START,
1469 PCI_MAPREG_TYPE_MEM
1470 | PCI_MAPREG_MEM_TYPE_32BIT, 0,
1471 &bktr->memt, &bktr->memh, NULL,
1472 &bktr->obmemsz);
1473 DPR(("pci_mapreg_map: memt %lx, memh %x, size %x\n",
1474 (unsigned long)bktr->memt, (u_int)bktr->memh,
1475 (u_int)bktr->obmemsz));
1476 if (retval) {
1477 printf("%s: couldn't map memory\n", bktr_name(bktr));
1478 return;
1482 * Disable the brooktree device
1484 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1485 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1488 * map interrupt
1490 if (pci_intr_map(pa, &ih)) {
1491 printf("%s: couldn't map interrupt\n",
1492 bktr_name(bktr));
1493 return;
1495 intrstr = pci_intr_string(pa->pa_pc, ih);
1496 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1497 bktr_intr, bktr);
1498 if (bktr->ih == NULL) {
1499 printf("%s: couldn't establish interrupt",
1500 bktr_name(bktr));
1501 if (intrstr != NULL)
1502 printf(" at %s", intrstr);
1503 printf("\n");
1504 return;
1506 if (intrstr != NULL)
1507 printf("%s: interrupting at %s\n", bktr_name(bktr),
1508 intrstr);
1509 selinit(&bktr->vbi_select);
1510 #endif /* __NetBSD__ */
1513 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
1514 * you have more than four, then 16 would probably be a better value.
1516 #ifndef BROOKTREE_DEF_LATENCY_VALUE
1517 #define BROOKTREE_DEF_LATENCY_VALUE 0x10
1518 #endif
1519 latency = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_LATENCY_TIMER);
1520 latency = (latency >> 8) & 0xff;
1522 if (!latency) {
1523 if (bootverbose) {
1524 printf("%s: PCI bus latency was 0 changing to %d",
1525 bktr_name(bktr), BROOKTREE_DEF_LATENCY_VALUE);
1527 latency = BROOKTREE_DEF_LATENCY_VALUE;
1528 pci_conf_write(pa->pa_pc, pa->pa_tag,
1529 PCI_LATENCY_TIMER, latency<<8);
1532 if (common_bktr_attach(bktr, unit, pa->pa_id,
1533 PCI_REVISION(pa->pa_class)) == 0)
1534 return;
1536 #if NRADIO > 0
1537 /* attach to radio(4) */
1538 if (bktr->card.tuner->pllControl[3] != 0x00)
1539 radio_attach_mi(&bktr_hw_if, bktr, &bktr->bktr_dev);
1540 #endif
1545 * Special Memory Allocation
1547 #if defined (__NetBSD__)
1548 vaddr_t
1549 #else
1550 vm_offset_t
1551 #endif
1552 get_bktr_mem(bktr_ptr_t bktr, bus_dmamap_t *dmapp, unsigned int size)
1554 bus_dma_tag_t dmat = bktr->dmat;
1555 bus_dma_segment_t seg;
1556 bus_size_t align;
1557 int rseg;
1558 void *kva;
1561 * Allocate a DMA area
1563 align = 1 << 24;
1564 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1565 &rseg, BUS_DMA_NOWAIT)) {
1566 align = PAGE_SIZE;
1567 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1568 &rseg, BUS_DMA_NOWAIT)) {
1569 printf("%s: Unable to dmamem_alloc of %d bytes\n",
1570 bktr_name(bktr), size);
1571 return 0;
1574 if (bus_dmamem_map(dmat, &seg, rseg, size,
1575 &kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
1576 printf("%s: Unable to dmamem_map of %d bytes\n",
1577 bktr_name(bktr), size);
1578 bus_dmamem_free(dmat, &seg, rseg);
1579 return 0;
1581 #ifdef __OpenBSD__
1582 bktr->dm_mapsize = size;
1583 #endif
1585 * Create and locd the DMA map for the DMA area
1587 if (bus_dmamap_create(dmat, size, 1, size, 0, BUS_DMA_NOWAIT, dmapp)) {
1588 printf("%s: Unable to dmamap_create of %d bytes\n",
1589 bktr_name(bktr), size);
1590 bus_dmamem_unmap(dmat, kva, size);
1591 bus_dmamem_free(dmat, &seg, rseg);
1592 return 0;
1594 if (bus_dmamap_load(dmat, *dmapp, kva, size, NULL, BUS_DMA_NOWAIT)) {
1595 printf("%s: Unable to dmamap_load of %d bytes\n",
1596 bktr_name(bktr), size);
1597 bus_dmamem_unmap(dmat, kva, size);
1598 bus_dmamem_free(dmat, &seg, rseg);
1599 bus_dmamap_destroy(dmat, *dmapp);
1600 return 0;
1602 #if defined(__NetBSD__)
1603 return (vaddr_t)kva;
1604 #else
1605 return (vm_offset_t)kva;
1606 #endif
1609 void
1610 free_bktr_mem(bktr_ptr_t bktr, bus_dmamap_t dmap, vaddr_t kva)
1612 bus_dma_tag_t dmat = bktr->dmat;
1614 #ifdef __NetBSD__
1615 bus_dmamem_unmap(dmat, (void *)kva, dmap->dm_mapsize);
1616 #else
1617 bus_dmamem_unmap(dmat, (void *)kva, bktr->dm_mapsize);
1618 #endif
1619 bus_dmamem_free(dmat, dmap->dm_segs, 1);
1620 bus_dmamap_destroy(dmat, dmap);
1624 /*---------------------------------------------------------
1626 ** BrookTree 848 character device driver routines
1628 **---------------------------------------------------------
1632 #define VIDEO_DEV 0x00
1633 #define TUNER_DEV 0x01
1634 #define VBI_DEV 0x02
1636 #define UNIT(x) (minor((x) & 0x0f))
1637 #define FUNCTION(x) (minor((x >> 4) & 0x0f))
1643 bktr_open(dev_t dev, int flags, int fmt,
1644 struct lwp *l)
1646 bktr_ptr_t bktr;
1647 int unit;
1649 unit = UNIT(dev);
1651 /* unit out of range */
1652 bktr = device_lookup_private(&bktr_cd, unit);
1653 if (bktr == NULL)
1654 return(ENXIO);
1656 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
1657 return(ENXIO);
1659 switch (FUNCTION(dev)) {
1660 case VIDEO_DEV:
1661 return(video_open(bktr));
1662 case TUNER_DEV:
1663 return(tuner_open(bktr));
1664 case VBI_DEV:
1665 return(vbi_open(bktr));
1668 return(ENXIO);
1676 bktr_close(dev_t dev, int flags, int fmt,
1677 struct lwp *l)
1679 bktr_ptr_t bktr;
1680 int unit;
1682 unit = UNIT(dev);
1684 bktr = device_lookup_private(&bktr_cd, unit);
1686 switch (FUNCTION(dev)) {
1687 case VIDEO_DEV:
1688 return(video_close(bktr));
1689 case TUNER_DEV:
1690 return(tuner_close(bktr));
1691 case VBI_DEV:
1692 return(vbi_close(bktr));
1695 return(ENXIO);
1702 bktr_read(dev_t dev, struct uio *uio, int ioflag)
1704 bktr_ptr_t bktr;
1705 int unit;
1707 unit = UNIT(dev);
1709 bktr = device_lookup_private(&bktr_cd, unit);
1711 switch (FUNCTION(dev)) {
1712 case VIDEO_DEV:
1713 return(video_read(bktr, unit, dev, uio));
1714 case VBI_DEV:
1715 return(vbi_read(bktr, uio, ioflag));
1718 return(ENXIO);
1726 bktr_write(dev_t dev, struct uio *uio, int ioflag)
1728 /* operation not supported */
1729 return(EOPNOTSUPP);
1736 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, void *arg, int flag,
1737 struct lwp *l)
1739 bktr_ptr_t bktr;
1740 int unit;
1742 unit = UNIT(dev);
1744 bktr = device_lookup_private(&bktr_cd, unit);
1746 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1747 return(ENOMEM);
1749 switch (FUNCTION(dev)) {
1750 case VIDEO_DEV:
1751 return(video_ioctl(bktr, unit, cmd, arg, l));
1752 case TUNER_DEV:
1753 return(tuner_ioctl(bktr, unit, cmd, arg, l));
1756 return(ENXIO);
1762 paddr_t
1763 bktr_mmap(dev_t dev, off_t offset, int nprot)
1765 int unit;
1766 bktr_ptr_t bktr;
1768 unit = UNIT(dev);
1770 if (FUNCTION(dev) > 0) /* only allow mmap on /dev/bktr[n] */
1771 return(-1);
1773 bktr = device_lookup_private(&bktr_cd, unit);
1775 if ((vaddr_t)offset >= bktr->alloc_pages * PAGE_SIZE)
1776 return(-1);
1778 #ifdef __NetBSD__
1779 return (bus_dmamem_mmap(bktr->dmat, bktr->dm_mem->dm_segs, 1,
1780 (vaddr_t)offset, nprot, BUS_DMA_WAITOK));
1781 #else
1782 return(i386_btop(vtophys(bktr->bigbuf) + offset));
1783 #endif
1786 #if NRADIO > 0
1788 bktr_set_info(void *v, struct radio_info *ri)
1790 struct bktr_softc *sc = v;
1791 u_int32_t freq;
1793 if (ri->mute) {
1794 /* mute the audio stream by switching the mux */
1795 set_audio(sc, AUDIO_MUTE);
1797 /* disable drivers on the GPIO port that controls the MUXes */
1798 OUTL(sc, BKTR_GPIO_OUT_EN, INL(sc, BKTR_GPIO_OUT_EN) &
1799 ~sc->card.gpio_mux_bits);
1800 } else {
1801 /* enable drivers on the GPIO port that controls the MUXes */
1802 OUTL(sc, BKTR_GPIO_OUT_EN, INL(sc, BKTR_GPIO_OUT_EN) |
1803 sc->card.gpio_mux_bits);
1805 /* unmute the audio stream */
1806 set_audio(sc, AUDIO_UNMUTE);
1807 init_audio_devices(sc);
1810 freq = ri->freq / 10;
1811 set_audio(sc, AUDIO_INTERN); /* use internal audio */
1812 temp_mute(sc, TRUE);
1813 ri->freq = tv_freq(sc, freq, FM_RADIO_FREQUENCY) * 10;
1814 temp_mute(sc, FALSE);
1816 return (0);
1820 bktr_get_info(void *v, struct radio_info *ri)
1822 struct bktr_softc *sc = v;
1823 struct TVTUNER *tv = &sc->tuner;
1824 int status;
1826 status = get_tuner_status(sc);
1828 #define STATUSBIT_STEREO 0x10
1829 ri->mute = (int)sc->audio_mute_state ? 1 : 0;
1830 ri->stereo = (status & STATUSBIT_STEREO) ? 1 : 0;
1831 ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_HW_AFC;
1832 ri->freq = tv->frequency * 10;
1833 ri->info = (status & STATUSBIT_STEREO) ? RADIO_INFO_STEREO : 0;
1834 #undef STATUSBIT_STEREO
1836 /* not yet supported */
1837 ri->volume = ri->rfreq = ri->lock = 0;
1839 return (0);
1841 #endif
1845 #endif /* __NetBSD__ || __OpenBSD__ */
1847 #if defined(__NetBSD__)
1849 u_int8_t
1850 bktr_INB(struct bktr_softc *bktr, int offset)
1852 u_int8_t val = bus_space_read_1(bktr->memt, bktr->memh, offset);
1853 bus_space_barrier(bktr->memt, bktr->memh, offset, 1,
1854 BUS_SPACE_BARRIER_READ);
1855 return val;
1858 u_int16_t
1859 bktr_INW(struct bktr_softc *bktr, int offset)
1861 u_int16_t val = bus_space_read_2(bktr->memt, bktr->memh, offset);
1862 bus_space_barrier(bktr->memt, bktr->memh, offset, 2,
1863 BUS_SPACE_BARRIER_READ);
1864 return val;
1867 u_int32_t
1868 bktr_INL(struct bktr_softc *bktr, int offset)
1870 u_int32_t val = bus_space_read_4(bktr->memt, bktr->memh, offset);
1871 bus_space_barrier(bktr->memt, bktr->memh, offset, 4,
1872 BUS_SPACE_BARRIER_READ);
1873 return val;
1876 void
1877 bktr_OUTB(struct bktr_softc *bktr, int offset, u_int8_t value)
1879 bus_space_write_1(bktr->memt, bktr->memh, offset, value);
1880 bus_space_barrier(bktr->memt, bktr->memh, offset, 1,
1881 BUS_SPACE_BARRIER_WRITE);
1884 void
1885 bktr_OUTW(struct bktr_softc *bktr, int offset, u_int16_t value)
1887 bus_space_write_2(bktr->memt, bktr->memh, offset, value);
1888 bus_space_barrier(bktr->memt, bktr->memh, offset, 2,
1889 BUS_SPACE_BARRIER_WRITE);
1892 void
1893 bktr_OUTL(struct bktr_softc *bktr, int offset, u_int32_t value)
1895 bus_space_write_4(bktr->memt, bktr->memh, offset, value);
1896 bus_space_barrier(bktr->memt, bktr->memh, offset, 4,
1897 BUS_SPACE_BARRIER_WRITE);
1900 #endif /* __NetBSD__ */