bktr_os.c revision 1.28 1 /* $NetBSD: bktr_os.c,v 1.28 2002/01/07 18:05:34 jmcneill Exp $ */
2
3 /* FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.20 2000/10/20 08:16:53 roger Exp */
4
5 /*
6 * This is part of the Driver for Video Capture Cards (Frame grabbers)
7 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
8 * chipset.
9 * Copyright Roger Hardiman and Amancio Hasty.
10 *
11 * bktr_os : This has all the Operating System dependant code,
12 * probe/attach and open/close/ioctl/read/mmap
13 * memory allocation
14 * PCI bus interfacing
15 *
16 *
17 */
18
19 /*
20 * 1. Redistributions of source code must retain the
21 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by Amancio Hasty and
35 * Roger Hardiman
36 * 4. The name of the author may not be used to endorse or promote products
37 * derived from this software without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
43 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: bktr_os.c,v 1.28 2002/01/07 18:05:34 jmcneill Exp $");
54
55 #ifdef __FreeBSD__
56 #include "bktr.h"
57 #endif /* __FreeBSD__ */
58
59 #include "opt_bktr.h" /* include any kernel config options */
60
61 #define FIFO_RISC_DISABLED 0
62 #define ALL_INTS_DISABLED 0
63
64 /*******************/
65 /* *** FreeBSD *** */
66 /*******************/
67 #ifdef __FreeBSD__
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/conf.h>
72 #include <sys/uio.h>
73 #include <sys/kernel.h>
74 #include <sys/signalvar.h>
75 #include <sys/mman.h>
76 #include <sys/poll.h>
77 #include <sys/select.h>
78 #include <sys/vnode.h>
79
80 #include <vm/vm.h>
81 #include <vm/vm_kern.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_extern.h>
84
85 #if (__FreeBSD_version >=400000) || (NSMBUS > 0)
86 #include <sys/bus.h> /* used by smbus and newbus */
87 #endif
88
89 #if (__FreeBSD_version >=300000)
90 #include <machine/bus_memio.h> /* used by bus space */
91 #include <machine/bus.h> /* used by bus space and newbus */
92 #include <sys/bus.h>
93 #endif
94
95 #if (__FreeBSD_version >=400000)
96 #include <sys/rman.h> /* used by newbus */
97 #include <machine/resource.h> /* used by newbus */
98 #endif
99
100 #if (__FreeBSD_version < 500000)
101 #include <machine/clock.h> /* for DELAY */
102 #endif
103
104 #include <pci/pcivar.h>
105 #include <pci/pcireg.h>
106
107 #include <sys/sysctl.h>
108 int bt848_card = -1;
109 int bt848_tuner = -1;
110 int bt848_reverse_mute = -1;
111 int bt848_format = -1;
112 int bt848_slow_msp_audio = -1;
113
114 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
115 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
116 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
117 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
118 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
119 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
120
121 #if (__FreeBSD__ == 2)
122 #define PCIR_REVID PCI_CLASS_REG
123 #endif
124
125 #endif /* end freebsd section */
126
127
128
129 /****************/
130 /* *** BSDI *** */
131 /****************/
132 #ifdef __bsdi__
133 #endif /* __bsdi__ */
134
135
136 /**************************/
137 /* *** OpenBSD/NetBSD *** */
138 /**************************/
139 #if defined(__NetBSD__) || defined(__OpenBSD__)
140
141 #include <sys/param.h>
142 #include <sys/systm.h>
143 #include <sys/conf.h>
144 #include <sys/uio.h>
145 #include <sys/kernel.h>
146 #include <sys/signalvar.h>
147 #include <sys/mman.h>
148 #include <sys/poll.h>
149 #include <sys/select.h>
150 #include <sys/vnode.h>
151
152 #ifndef __NetBSD__
153 #include <vm/vm.h>
154 #include <vm/vm_kern.h>
155 #include <vm/pmap.h>
156 #include <vm/vm_extern.h>
157 #endif
158
159 #include <sys/device.h>
160 #include <dev/pci/pcivar.h>
161 #include <dev/pci/pcireg.h>
162 #include <dev/pci/pcidevs.h>
163
164 #define BKTR_DEBUG
165 #ifdef BKTR_DEBUG
166 int bktr_debug = 0;
167 #define DPR(x) (bktr_debug ? printf x : 0)
168 #else
169 #define DPR(x)
170 #endif
171 #endif /* __NetBSD__ || __OpenBSD__ */
172
173
174 #ifdef __NetBSD__
175 #include <dev/ic/bt8xx.h> /* NetBSD location for .h files */
176 #include <dev/pci/bktr/bktr_reg.h>
177 #include <dev/pci/bktr/bktr_tuner.h>
178 #include <dev/pci/bktr/bktr_card.h>
179 #include <dev/pci/bktr/bktr_audio.h>
180 #include <dev/pci/bktr/bktr_core.h>
181 #include <dev/pci/bktr/bktr_os.h>
182 #else /* Traditional location for .h files */
183 #include <machine/ioctl_meteor.h>
184 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
185 #include <dev/bktr/bktr_reg.h>
186 #include <dev/bktr/bktr_tuner.h>
187 #include <dev/bktr/bktr_card.h>
188 #include <dev/bktr/bktr_audio.h>
189 #include <dev/bktr/bktr_core.h>
190 #include <dev/bktr/bktr_os.h>
191 #if defined(BKTR_USE_FREEBSD_SMBUS)
192 #include <dev/bktr/bktr_i2c.h>
193 #endif
194 #endif
195
196 /* Support for radio(4) under NetBSD */
197 #ifdef __NetBSD__
198 #include "radio.h"
199 #if NRADIO > 0
200 #include <sys/radioio.h>
201 #include <dev/radio_if.h>
202 #endif
203 #else
204 #define NRADIO 0
205 #endif
206
207 /****************************/
208 /* *** FreeBSD 4.x code *** */
209 /****************************/
210 #if (__FreeBSD_version >= 400000)
211
212 static int bktr_probe( device_t dev );
213 static int bktr_attach( device_t dev );
214 static int bktr_detach( device_t dev );
215 static int bktr_shutdown( device_t dev );
216 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
217
218 static device_method_t bktr_methods[] = {
219 /* Device interface */
220 DEVMETHOD(device_probe, bktr_probe),
221 DEVMETHOD(device_attach, bktr_attach),
222 DEVMETHOD(device_detach, bktr_detach),
223 DEVMETHOD(device_shutdown, bktr_shutdown),
224
225 { 0, 0 }
226 };
227
228 static driver_t bktr_driver = {
229 "bktr",
230 bktr_methods,
231 sizeof(struct bktr_softc),
232 };
233
234 static devclass_t bktr_devclass;
235
236 static d_open_t bktr_open;
237 static d_close_t bktr_close;
238 static d_read_t bktr_read;
239 static d_write_t bktr_write;
240 static d_ioctl_t bktr_ioctl;
241 static d_mmap_t bktr_mmap;
242 static d_poll_t bktr_poll;
243
244 #define CDEV_MAJOR 92
245 static struct cdevsw bktr_cdevsw = {
246 /* open */ bktr_open,
247 /* close */ bktr_close,
248 /* read */ bktr_read,
249 /* write */ bktr_write,
250 /* ioctl */ bktr_ioctl,
251 /* poll */ bktr_poll,
252 /* mmap */ bktr_mmap,
253 /* strategy */ nostrategy,
254 /* name */ "bktr",
255 /* maj */ CDEV_MAJOR,
256 /* dump */ nodump,
257 /* psize */ nopsize,
258 /* flags */ 0,
259 /* bmaj */ -1
260 };
261
262 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
263 #if (__FreeBSD_version > 410000)
264 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
265 MODULE_VERSION(bktr, 1);
266 #endif
267
268
269 /*
270 * the boot time probe routine.
271 */
272 static int
273 bktr_probe( device_t dev )
274 {
275 unsigned int type = pci_get_devid(dev);
276 unsigned int rev = pci_get_revid(dev);
277
278 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
279 {
280 switch (PCI_PRODUCT(type)) {
281 case PCI_PRODUCT_BROOKTREE_BT848:
282 if (rev == 0x12)
283 device_set_desc(dev, "BrookTree 848A");
284 else
285 device_set_desc(dev, "BrookTree 848");
286 return 0;
287 case PCI_PRODUCT_BROOKTREE_BT849:
288 device_set_desc(dev, "BrookTree 849A");
289 return 0;
290 case PCI_PRODUCT_BROOKTREE_BT878:
291 device_set_desc(dev, "BrookTree 878");
292 return 0;
293 case PCI_PRODUCT_BROOKTREE_BT879:
294 device_set_desc(dev, "BrookTree 879");
295 return 0;
296 }
297 };
298
299 return ENXIO;
300 }
301
302
303 /*
304 * the attach routine.
305 */
306 static int
307 bktr_attach( device_t dev )
308 {
309 u_long latency;
310 u_long fun;
311 u_long val;
312 unsigned int rev;
313 unsigned int unit;
314 int error = 0;
315 #ifdef BROOKTREE_IRQ
316 u_long old_irq, new_irq;
317 #endif
318
319 struct bktr_softc *bktr = device_get_softc(dev);
320
321 unit = device_get_unit(dev);
322
323 /* build the device name for bktr_name() */
324 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
325
326 /*
327 * Enable bus mastering and Memory Mapped device
328 */
329 val = pci_read_config(dev, PCIR_COMMAND, 4);
330 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
331 pci_write_config(dev, PCIR_COMMAND, val, 4);
332
333 /*
334 * Map control/status registers.
335 */
336 bktr->mem_rid = PCIR_MAPS;
337 bktr->res_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &bktr->mem_rid,
338 0, ~0, 1, RF_ACTIVE);
339
340
341 if (!bktr->res_mem) {
342 device_printf(dev, "could not map memory\n");
343 error = ENXIO;
344 goto fail;
345 }
346 bktr->memt = rman_get_bustag(bktr->res_mem);
347 bktr->memh = rman_get_bushandle(bktr->res_mem);
348
349
350 /*
351 * Disable the brooktree device
352 */
353 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
354 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
355
356
357 #ifdef BROOKTREE_IRQ /* from the configuration file */
358 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
359 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
360 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
361 printf("bktr%d: attach: irq changed from %d to %d\n",
362 unit, (old_irq & 0xff), (new_irq & 0xff));
363 #endif
364
365 /*
366 * Allocate our interrupt.
367 */
368 bktr->irq_rid = 0;
369 bktr->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &bktr->irq_rid,
370 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
371 if (bktr->res_irq == NULL) {
372 device_printf(dev, "could not map interrupt\n");
373 error = ENXIO;
374 goto fail;
375 }
376
377 error = bus_setup_intr(dev, bktr->res_irq, INTR_TYPE_TTY,
378 bktr_intr, bktr, &bktr->res_ih);
379 if (error) {
380 device_printf(dev, "could not setup irq\n");
381 goto fail;
382
383 }
384
385
386 /* Update the Device Control Register */
387 /* on Bt878 and Bt879 cards */
388 fun = pci_read_config( dev, 0x40, 2);
389 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
390
391 #if defined( BKTR_430_FX_MODE )
392 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
393 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
394 #endif
395
396 #if defined( BKTR_SIS_VIA_MODE )
397 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
398 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
399 OPTi chipset motherboards too */
400 #endif
401 pci_write_config(dev, 0x40, fun, 2);
402
403
404 /* XXX call bt848_i2c dependent attach() routine */
405 #if defined(BKTR_USE_FREEBSD_SMBUS)
406 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
407 printf("bktr%d: i2c_attach: can't attach\n", unit);
408 #endif
409
410
411 /*
412 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
413 * you have more than four, then 16 would probably be a better value.
414 */
415 #ifndef BROOKTREE_DEF_LATENCY_VALUE
416 #define BROOKTREE_DEF_LATENCY_VALUE 10
417 #endif
418 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
419 latency = (latency >> 8) & 0xff;
420 if ( bootverbose ) {
421 if (latency)
422 printf("brooktree%d: PCI bus latency is", unit);
423 else
424 printf("brooktree%d: PCI bus latency was 0 changing to",
425 unit);
426 }
427 if ( !latency ) {
428 latency = BROOKTREE_DEF_LATENCY_VALUE;
429 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
430 }
431 if ( bootverbose ) {
432 printf(" %d.\n", (int) latency);
433 }
434
435 /* read the pci device id and revision id */
436 fun = pci_get_devid(dev);
437 rev = pci_get_revid(dev);
438
439 /* call the common attach code */
440 common_bktr_attach( bktr, unit, fun, rev );
441
442 /* make the device entries */
443 bktr->bktrdev = make_dev(&bktr_cdevsw, unit,
444 0, 0, 0444, "bktr%d", unit);
445 bktr->tunerdev= make_dev(&bktr_cdevsw, unit+16,
446 0, 0, 0444, "tuner%d", unit);
447 bktr->vbidev = make_dev(&bktr_cdevsw, unit+32,
448 0, 0, 0444, "vbi%d" , unit);
449
450
451 /* if this is unit 0 (/dev/bktr0, /dev/tuner0, /dev/vbi0) then make */
452 /* alias entries to /dev/bktr /dev/tuner and /dev/vbi */
453 #if (__FreeBSD_version >=500000)
454 if (unit == 0) {
455 bktr->bktrdev_alias = make_dev_alias(bktr->bktrdev, "bktr");
456 bktr->tunerdev_alias= make_dev_alias(bktr->tunerdev, "tuner");
457 bktr->vbidev_alias = make_dev_alias(bktr->vbidev, "vbi");
458 }
459 #endif
460
461 return 0;
462
463 fail:
464 if (bktr->res_irq)
465 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
466 if (bktr->res_mem)
467 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
468 return error;
469
470 }
471
472 /*
473 * the detach routine.
474 */
475 static int
476 bktr_detach( device_t dev )
477 {
478 unsigned int unit;
479
480 struct bktr_softc *bktr = device_get_softc(dev);
481
482 unit = device_get_unit(dev);
483
484 /* Disable the brooktree device */
485 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
486 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
487
488 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
489 /* The memory is retained by the bktr_mem module so we can unload and */
490 /* then reload the main bktr driver module */
491
492 /* Unregister the /dev/bktrN, tunerN and vbiN devices */
493 destroy_dev(bktr->vbidev);
494 destroy_dev(bktr->tunerdev);
495 destroy_dev(bktr->bktrdev);
496
497 /* If this is unit 0, then destroy the alias entries too */
498 #if (__FreeBSD_version >=500000)
499 if (unit == 0) {
500 destroy_dev(bktr->vbidev_alias);
501 destroy_dev(bktr->tunerdev_alias);
502 destroy_dev(bktr->bktrdev_alias);
503 }
504 #endif
505
506 /*
507 * Deallocate resources.
508 */
509 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
510 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
511 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
512
513 return 0;
514 }
515
516 /*
517 * the shutdown routine.
518 */
519 static int
520 bktr_shutdown( device_t dev )
521 {
522 struct bktr_softc *bktr = device_get_softc(dev);
523
524 /* Disable the brooktree device */
525 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
526 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
527
528 return 0;
529 }
530
531
532 /*
533 * Special Memory Allocation
534 */
535 vm_offset_t
536 get_bktr_mem( int unit, unsigned size )
537 {
538 vm_offset_t addr = 0;
539
540 addr = vm_page_alloc_contig(size, 0, 0xffffffff, 1<<24);
541 if (addr == 0)
542 addr = vm_page_alloc_contig(size, 0, 0xffffffff, PAGE_SIZE);
543 if (addr == 0) {
544 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
545 unit, size);
546 }
547
548 return( addr );
549 }
550
551
552 /*---------------------------------------------------------
553 **
554 ** BrookTree 848 character device driver routines
555 **
556 **---------------------------------------------------------
557 */
558
559 #define VIDEO_DEV 0x00
560 #define TUNER_DEV 0x01
561 #define VBI_DEV 0x02
562
563 #define UNIT(x) ((x) & 0x0f)
564 #define FUNCTION(x) (x >> 4)
565
566 /*
567 *
568 */
569 int
570 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
571 {
572 bktr_ptr_t bktr;
573 int unit;
574 int result;
575
576 unit = UNIT( minor(dev) );
577
578 /* Get the device data */
579 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
580 if (bktr == NULL) {
581 /* the device is no longer valid/functioning */
582 return (ENXIO);
583 }
584
585 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
586 return( ENXIO );
587
588 /* Record that the device is now busy */
589 device_busy(devclass_get_device(bktr_devclass, unit));
590
591
592 if (bt848_card != -1) {
593 if ((bt848_card >> 8 == unit ) &&
594 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
595 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
596 bktr->bt848_card = (bt848_card & 0xff);
597 probeCard(bktr, FALSE, unit);
598 }
599 }
600 }
601
602 if (bt848_tuner != -1) {
603 if ((bt848_tuner >> 8 == unit ) &&
604 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
605 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
606 bktr->bt848_tuner = (bt848_tuner & 0xff);
607 probeCard(bktr, FALSE, unit);
608 }
609 }
610 }
611
612 if (bt848_reverse_mute != -1) {
613 if ((bt848_reverse_mute >> 8) == unit ) {
614 bktr->reverse_mute = bt848_reverse_mute & 0xff;
615 }
616 }
617
618 if (bt848_slow_msp_audio != -1) {
619 if ((bt848_slow_msp_audio >> 8) == unit ) {
620 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
621 }
622 }
623
624 switch ( FUNCTION( minor(dev) ) ) {
625 case VIDEO_DEV:
626 result = video_open( bktr );
627 break;
628 case TUNER_DEV:
629 result = tuner_open( bktr );
630 break;
631 case VBI_DEV:
632 result = vbi_open( bktr );
633 break;
634 default:
635 result = ENXIO;
636 break;
637 }
638
639 /* If there was an error opening the device, undo the busy status */
640 if (result != 0)
641 device_unbusy(devclass_get_device(bktr_devclass, unit));
642 return( result );
643 }
644
645
646 /*
647 *
648 */
649 int
650 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
651 {
652 bktr_ptr_t bktr;
653 int unit;
654 int result;
655
656 unit = UNIT( minor(dev) );
657
658 /* Get the device data */
659 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
660 if (bktr == NULL) {
661 /* the device is no longer valid/functioning */
662 return (ENXIO);
663 }
664
665 switch ( FUNCTION( minor(dev) ) ) {
666 case VIDEO_DEV:
667 result = video_close( bktr );
668 break;
669 case TUNER_DEV:
670 result = tuner_close( bktr );
671 break;
672 case VBI_DEV:
673 result = vbi_close( bktr );
674 break;
675 default:
676 return (ENXIO);
677 break;
678 }
679
680 device_unbusy(devclass_get_device(bktr_devclass, unit));
681 return( result );
682 }
683
684
685 /*
686 *
687 */
688 int
689 bktr_read( dev_t dev, struct uio *uio, int ioflag )
690 {
691 bktr_ptr_t bktr;
692 int unit;
693
694 unit = UNIT(minor(dev));
695
696 /* Get the device data */
697 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
698 if (bktr == NULL) {
699 /* the device is no longer valid/functioning */
700 return (ENXIO);
701 }
702
703 switch ( FUNCTION( minor(dev) ) ) {
704 case VIDEO_DEV:
705 return( video_read( bktr, unit, dev, uio ) );
706 case VBI_DEV:
707 return( vbi_read( bktr, uio, ioflag ) );
708 }
709 return( ENXIO );
710 }
711
712
713 /*
714 *
715 */
716 int
717 bktr_write( dev_t dev, struct uio *uio, int ioflag )
718 {
719 return( EINVAL ); /* XXX or ENXIO ? */
720 }
721
722
723 /*
724 *
725 */
726 int
727 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
728 {
729 bktr_ptr_t bktr;
730 int unit;
731
732 unit = UNIT(minor(dev));
733
734 /* Get the device data */
735 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
736 if (bktr == NULL) {
737 /* the device is no longer valid/functioning */
738 return (ENXIO);
739 }
740
741 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
742 return( ENOMEM );
743
744 switch ( FUNCTION( minor(dev) ) ) {
745 case VIDEO_DEV:
746 return( video_ioctl( bktr, unit, cmd, arg, pr ) );
747 case TUNER_DEV:
748 return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
749 }
750
751 return( ENXIO );
752 }
753
754
755 /*
756 *
757 */
758 int
759 bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
760 {
761 int unit;
762 bktr_ptr_t bktr;
763
764 unit = UNIT(minor(dev));
765
766 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
767 return( -1 );
768
769 /* Get the device data */
770 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
771 if (bktr == NULL) {
772 /* the device is no longer valid/functioning */
773 return (-1);
774 }
775
776 if (nprot & PROT_EXEC)
777 return( -1 );
778
779 if (offset < 0)
780 return( -1 );
781
782 if (offset >= bktr->alloc_pages * PAGE_SIZE)
783 return( -1 );
784
785 return( atop(vtophys(bktr->bigbuf) + offset) );
786 }
787
788 int bktr_poll( dev_t dev, int events, struct proc *p)
789 {
790 int unit;
791 bktr_ptr_t bktr;
792 int revents = 0;
793 DECLARE_INTR_MASK(s);
794
795 unit = UNIT(minor(dev));
796
797 /* Get the device data */
798 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
799 if (bktr == NULL) {
800 /* the device is no longer valid/functioning */
801 return (ENXIO);
802 }
803
804 DISABLE_INTR(s);
805
806 if (events & (POLLIN | POLLRDNORM)) {
807
808 switch ( FUNCTION( minor(dev) ) ) {
809 case VBI_DEV:
810 if(bktr->vbisize == 0)
811 selrecord(p, &bktr->vbi_select);
812 else
813 revents |= events & (POLLIN | POLLRDNORM);
814 break;
815 }
816 }
817
818 ENABLE_INTR(s);
819
820 return (revents);
821 }
822
823 #endif /* FreeBSD 4.x specific kernel interface routines */
824
825 /**********************************/
826 /* *** FreeBSD 2.2.x and 3.x *** */
827 /**********************************/
828
829 #if ((__FreeBSD__ == 2) || (__FreeBSD__ == 3))
830
831 static bktr_reg_t brooktree[ NBKTR ];
832
833 static const char* bktr_probe( pcici_t tag, pcidi_t type );
834 static void bktr_attach( pcici_t tag, int unit );
835 static void bktr_intr(void *arg) { common_bktr_intr(arg); }
836
837 static u_long bktr_count;
838
839 static struct pci_device bktr_device = {
840 "bktr",
841 bktr_probe,
842 bktr_attach,
843 &bktr_count
844 };
845
846 DATA_SET (pcidevice_set, bktr_device);
847
848 static d_open_t bktr_open;
849 static d_close_t bktr_close;
850 static d_read_t bktr_read;
851 static d_write_t bktr_write;
852 static d_ioctl_t bktr_ioctl;
853 static d_mmap_t bktr_mmap;
854 static d_poll_t bktr_poll;
855
856 #define CDEV_MAJOR 92
857 static struct cdevsw bktr_cdevsw =
858 {
859 bktr_open, bktr_close, bktr_read, bktr_write,
860 bktr_ioctl, nostop, nullreset, nodevtotty,
861 bktr_poll, bktr_mmap, NULL, "bktr",
862 NULL, -1
863 };
864
865 static int bktr_devsw_installed;
866
867 static void
868 bktr_drvinit( void *unused )
869 {
870 dev_t dev;
871
872 if ( ! bktr_devsw_installed ) {
873 dev = makedev(CDEV_MAJOR, 0);
874 cdevsw_add(&dev,&bktr_cdevsw, NULL);
875 bktr_devsw_installed = 1;
876 }
877 }
878
879 SYSINIT(bktrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bktr_drvinit,NULL)
880
881 /*
882 * the boot time probe routine.
883 */
884 static const char*
885 bktr_probe( pcici_t tag, pcidi_t type )
886 {
887 unsigned int rev = pci_conf_read( tag, PCIR_REVID) & 0x000000ff;
888
889 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
890 {
891 switch (PCI_PRODUCT(type)) {
892 case PCI_PRODUCT_BROOKTREE_BT848:
893 if (rev == 0x12) return("BrookTree 848A");
894 else return("BrookTree 848");
895 case PCI_PRODUCT_BROOKTREE_BT849:
896 return("BrookTree 849A");
897 case PCI_PRODUCT_BROOKTREE_BT878:
898 return("BrookTree 878");
899 case PCI_PRODUCT_BROOKTREE_BT879:
900 return("BrookTree 879");
901 }
902 };
903
904 return ((char *)0);
905 }
906
907 /*
908 * the attach routine.
909 */
910 static void
911 bktr_attach( pcici_t tag, int unit )
912 {
913 bktr_ptr_t bktr;
914 u_long latency;
915 u_long fun;
916 unsigned int rev;
917 unsigned long base;
918 #ifdef BROOKTREE_IRQ
919 u_long old_irq, new_irq;
920 #endif
921
922 bktr = &brooktree[unit];
923
924 if (unit >= NBKTR) {
925 printf("brooktree%d: attach: only %d units configured.\n",
926 unit, NBKTR);
927 printf("brooktree%d: attach: invalid unit number.\n", unit);
928 return;
929 }
930
931 /* build the device name for bktr_name() */
932 snprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
933
934 /* Enable Memory Mapping */
935 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
936 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 2);
937
938 /* Enable Bus Mastering */
939 fun = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
940 pci_conf_write(tag, PCI_COMMAND_STATUS_REG, fun | 4);
941
942 bktr->tag = tag;
943
944
945 /*
946 * Map control/status registers
947 */
948 pci_map_mem( tag, PCI_MAP_REG_START, (vm_offset_t *) &base,
949 &bktr->phys_base );
950 #if (__FreeBSD_version >= 300000)
951 bktr->memt = I386_BUS_SPACE_MEM; /* XXX should use proper bus space */
952 bktr->memh = (bus_space_handle_t)base; /* XXX functions here */
953 #endif
954
955 /*
956 * Disable the brooktree device
957 */
958 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
959 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
960
961 #ifdef BROOKTREE_IRQ /* from the configuration file */
962 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
963 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
964 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
965 printf("bktr%d: attach: irq changed from %d to %d\n",
966 unit, (old_irq & 0xff), (new_irq & 0xff));
967 #endif
968
969 /*
970 * setup the interrupt handling routine
971 */
972 pci_map_int(tag, bktr_intr, (void*) bktr, &tty_imask);
973
974
975 /* Update the Device Control Register */
976 /* on Bt878 and Bt879 cards */
977 fun = pci_conf_read(tag, 0x40);
978 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
979
980 #if defined( BKTR_430_FX_MODE )
981 if (bootverbose) printf("Using 430 FX chipset compatibilty mode\n");
982 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
983 #endif
984
985 #if defined( BKTR_SIS_VIA_MODE )
986 if (bootverbose) printf("Using SiS/VIA chipset compatibilty mode\n");
987 fun = fun | 4; /* Enable SiS/VIA compatibility mode (usefull for
988 OPTi chipset motherboards too */
989 #endif
990 pci_conf_write(tag, 0x40, fun);
991
992
993 /* XXX call bt848_i2c dependent attach() routine */
994 #if defined(BKTR_USE_FREEBSD_SMBUS)
995 if (bt848_i2c_attach(unit, bktr, &bktr->i2c_sc))
996 printf("bktr%d: i2c_attach: can't attach\n", unit);
997 #endif
998
999
1000 /*
1001 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
1002 * you have more than four, then 16 would probably be a better value.
1003 */
1004 #ifndef BROOKTREE_DEF_LATENCY_VALUE
1005 #define BROOKTREE_DEF_LATENCY_VALUE 10
1006 #endif
1007 latency = pci_conf_read(tag, PCI_LATENCY_TIMER);
1008 latency = (latency >> 8) & 0xff;
1009 if ( bootverbose ) {
1010 if (latency)
1011 printf("brooktree%d: PCI bus latency is", unit);
1012 else
1013 printf("brooktree%d: PCI bus latency was 0 changing to",
1014 unit);
1015 }
1016 if ( !latency ) {
1017 latency = BROOKTREE_DEF_LATENCY_VALUE;
1018 pci_conf_write(tag, PCI_LATENCY_TIMER, latency<<8);
1019 }
1020 if ( bootverbose ) {
1021 printf(" %d.\n", (int) latency);
1022 }
1023
1024
1025 /* read the pci device id and revision id */
1026 fun = pci_conf_read(tag, PCI_ID_REG);
1027 rev = pci_conf_read(tag, PCIR_REVID) & 0x000000ff;
1028
1029 /* call the common attach code */
1030 common_bktr_attach( bktr, unit, fun, rev );
1031
1032 }
1033
1034
1035 /*
1036 * Special Memory Allocation
1037 */
1038 vm_offset_t
1039 get_bktr_mem( int unit, unsigned size )
1040 {
1041 vm_offset_t addr = 0;
1042
1043 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff, 1<<24);
1044 if (addr == 0)
1045 addr = vm_page_alloc_contig(size, 0x100000, 0xffffffff,
1046 PAGE_SIZE);
1047 if (addr == 0) {
1048 printf("bktr%d: Unable to allocate %d bytes of memory.\n",
1049 unit, size);
1050 }
1051
1052 return( addr );
1053 }
1054
1055 /*---------------------------------------------------------
1056 **
1057 ** BrookTree 848 character device driver routines
1058 **
1059 **---------------------------------------------------------
1060 */
1061
1062
1063 #define VIDEO_DEV 0x00
1064 #define TUNER_DEV 0x01
1065 #define VBI_DEV 0x02
1066
1067 #define UNIT(x) ((x) & 0x0f)
1068 #define FUNCTION(x) ((x >> 4) & 0x0f)
1069
1070
1071 /*
1072 *
1073 */
1074 int
1075 bktr_open( dev_t dev, int flags, int fmt, struct proc *p )
1076 {
1077 bktr_ptr_t bktr;
1078 int unit;
1079
1080 unit = UNIT( minor(dev) );
1081 if (unit >= NBKTR) /* unit out of range */
1082 return( ENXIO );
1083
1084 bktr = &(brooktree[ unit ]);
1085
1086 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
1087 return( ENXIO );
1088
1089
1090 if (bt848_card != -1) {
1091 if ((bt848_card >> 8 == unit ) &&
1092 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
1093 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
1094 bktr->bt848_card = (bt848_card & 0xff);
1095 probeCard(bktr, FALSE, unit);
1096 }
1097 }
1098 }
1099
1100 if (bt848_tuner != -1) {
1101 if ((bt848_tuner >> 8 == unit ) &&
1102 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
1103 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
1104 bktr->bt848_tuner = (bt848_tuner & 0xff);
1105 probeCard(bktr, FALSE, unit);
1106 }
1107 }
1108 }
1109
1110 if (bt848_reverse_mute != -1) {
1111 if ((bt848_reverse_mute >> 8) == unit ) {
1112 bktr->reverse_mute = bt848_reverse_mute & 0xff;
1113 }
1114 }
1115
1116 if (bt848_slow_msp_audio != -1) {
1117 if ((bt848_slow_msp_audio >> 8) == unit ) {
1118 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
1119 }
1120 }
1121
1122 switch ( FUNCTION( minor(dev) ) ) {
1123 case VIDEO_DEV:
1124 return( video_open( bktr ) );
1125 case TUNER_DEV:
1126 return( tuner_open( bktr ) );
1127 case VBI_DEV:
1128 return( vbi_open( bktr ) );
1129 }
1130 return( ENXIO );
1131 }
1132
1133
1134 /*
1135 *
1136 */
1137 int
1138 bktr_close( dev_t dev, int flags, int fmt, struct proc *p )
1139 {
1140 bktr_ptr_t bktr;
1141 int unit;
1142
1143 unit = UNIT( minor(dev) );
1144 if (unit >= NBKTR) /* unit out of range */
1145 return( ENXIO );
1146
1147 bktr = &(brooktree[ unit ]);
1148
1149 switch ( FUNCTION( minor(dev) ) ) {
1150 case VIDEO_DEV:
1151 return( video_close( bktr ) );
1152 case TUNER_DEV:
1153 return( tuner_close( bktr ) );
1154 case VBI_DEV:
1155 return( vbi_close( bktr ) );
1156 }
1157
1158 return( ENXIO );
1159 }
1160
1161 /*
1162 *
1163 */
1164 int
1165 bktr_read( dev_t dev, struct uio *uio, int ioflag )
1166 {
1167 bktr_ptr_t bktr;
1168 int unit;
1169
1170 unit = UNIT(minor(dev));
1171 if (unit >= NBKTR) /* unit out of range */
1172 return( ENXIO );
1173
1174 bktr = &(brooktree[unit]);
1175
1176 switch ( FUNCTION( minor(dev) ) ) {
1177 case VIDEO_DEV:
1178 return( video_read( bktr, unit, dev, uio ) );
1179 case VBI_DEV:
1180 return( vbi_read( bktr, uio, ioflag ) );
1181 }
1182 return( ENXIO );
1183 }
1184
1185
1186 /*
1187 *
1188 */
1189 int
1190 bktr_write( dev_t dev, struct uio *uio, int ioflag )
1191 {
1192 return( EINVAL ); /* XXX or ENXIO ? */
1193 }
1194
1195 /*
1196 *
1197 */
1198 int
1199 bktr_ioctl( dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr )
1200 {
1201 bktr_ptr_t bktr;
1202 int unit;
1203
1204 unit = UNIT(minor(dev));
1205 if (unit >= NBKTR) /* unit out of range */
1206 return( ENXIO );
1207
1208 bktr = &(brooktree[ unit ]);
1209
1210 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1211 return( ENOMEM );
1212
1213 switch ( FUNCTION( minor(dev) ) ) {
1214 case VIDEO_DEV:
1215 return( video_ioctl( bktr, unit, cmd, arg, pr ) );
1216 case TUNER_DEV:
1217 return( tuner_ioctl( bktr, unit, cmd, arg, pr ) );
1218 }
1219
1220 return( ENXIO );
1221 }
1222
1223 /*
1224 * bktr_mmap.
1225 * Note: 2.2.5/2.2.6/2.2.7/3.0 users must manually
1226 * edit the line below and change "vm_offset_t" to "int"
1227 */
1228 int bktr_mmap( dev_t dev, vm_offset_t offset, int nprot )
1229
1230 {
1231 int unit;
1232 bktr_ptr_t bktr;
1233
1234 unit = UNIT(minor(dev));
1235
1236 if (unit >= NBKTR || FUNCTION(minor(dev)) > 0)
1237 return( -1 );
1238
1239 bktr = &(brooktree[ unit ]);
1240
1241 if (nprot & PROT_EXEC)
1242 return( -1 );
1243
1244 if (offset < 0)
1245 return( -1 );
1246
1247 if (offset >= bktr->alloc_pages * PAGE_SIZE)
1248 return( -1 );
1249
1250 return( i386_btop(vtophys(bktr->bigbuf) + offset) );
1251 }
1252
1253 int bktr_poll( dev_t dev, int events, struct proc *p)
1254 {
1255 int unit;
1256 bktr_ptr_t bktr;
1257 int revents = 0;
1258
1259 unit = UNIT(minor(dev));
1260
1261 if (unit >= NBKTR)
1262 return( -1 );
1263
1264 bktr = &(brooktree[ unit ]);
1265
1266 disable_intr();
1267
1268 if (events & (POLLIN | POLLRDNORM)) {
1269
1270 switch ( FUNCTION( minor(dev) ) ) {
1271 case VBI_DEV:
1272 if(bktr->vbisize == 0)
1273 selrecord(p, &bktr->vbi_select);
1274 else
1275 revents |= events & (POLLIN | POLLRDNORM);
1276 break;
1277 }
1278 }
1279
1280 enable_intr();
1281
1282 return (revents);
1283 }
1284
1285
1286 #endif /* FreeBSD 2.2.x and 3.x specific kernel interface routines */
1287
1288
1289 /*****************/
1290 /* *** BSDI *** */
1291 /*****************/
1292
1293 #if defined(__bsdi__)
1294 #endif /* __bsdi__ BSDI specific kernel interface routines */
1295
1296
1297 /*****************************/
1298 /* *** OpenBSD / NetBSD *** */
1299 /*****************************/
1300 #if defined(__NetBSD__) || defined(__OpenBSD__)
1301
1302 #define IPL_VIDEO IPL_BIO /* XXX */
1303
1304 static int bktr_intr(void *arg) { return common_bktr_intr(arg); }
1305
1306 #define bktr_open bktropen
1307 #define bktr_close bktrclose
1308 #define bktr_read bktrread
1309 #define bktr_write bktrwrite
1310 #define bktr_ioctl bktrioctl
1311 #define bktr_mmap bktrmmap
1312
1313 #if defined(__OpenBSD__)
1314 static int bktr_probe __P((struct device *, void *, void *));
1315 #else
1316 static int bktr_probe __P((struct device *, struct cfdata *, void *));
1317 #endif
1318 static void bktr_attach __P((struct device *, struct device *, void *));
1319
1320 struct cfattach bktr_ca = {
1321 sizeof(struct bktr_softc), bktr_probe, bktr_attach
1322 };
1323
1324 #if defined(__NetBSD__)
1325 extern struct cfdriver bktr_cd;
1326 #else
1327 struct cfdriver bktr_cd = {
1328 NULL, "bktr", DV_DULL
1329 };
1330 #endif
1331
1332
1333 #if NRADIO > 0
1334 /* for radio(4) */
1335 int bktr_get_info(void *, struct radio_info *);
1336 int bktr_set_info(void *, struct radio_info *);
1337
1338 struct radio_hw_if bktr_hw_if = {
1339 NULL, /* open */
1340 NULL, /* close */
1341 bktr_get_info,
1342 bktr_set_info,
1343 NULL /* search */
1344 };
1345 #endif
1346
1347 int
1348 bktr_probe(parent, match, aux)
1349 struct device *parent;
1350 #if defined(__OpenBSD__)
1351 void *match;
1352 #else
1353 struct cfdata *match;
1354 #endif
1355 void *aux;
1356 {
1357 struct pci_attach_args *pa = aux;
1358
1359 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROOKTREE &&
1360 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT848 ||
1361 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT849 ||
1362 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT878 ||
1363 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT879))
1364 return 1;
1365
1366 return 0;
1367 }
1368
1369
1370 /*
1371 * the attach routine.
1372 */
1373 static void
1374 bktr_attach(struct device *parent, struct device *self, void *aux)
1375 {
1376 bktr_ptr_t bktr;
1377 u_long latency;
1378 u_long fun;
1379 unsigned int rev;
1380
1381 #if defined(__OpenBSD__)
1382 struct pci_attach_args *pa = aux;
1383 pci_chipset_tag_t pc = pa->pa_pc;
1384
1385 pci_intr_handle_t ih;
1386 const char *intrstr;
1387 int retval;
1388 int unit;
1389
1390 bktr = (bktr_ptr_t)self;
1391 unit = bktr->bktr_dev.dv_unit;
1392
1393 bktr->pc = pa->pa_pc;
1394 bktr->tag = pa->pa_tag;
1395 bktr->dmat = pa->pa_dmat;
1396
1397 /*
1398 * map memory
1399 */
1400 bktr->memt = pa->pa_memt;
1401 retval = pci_mem_find(pc, pa->pa_tag, PCI_MAPREG_START,
1402 &bktr->phys_base, &bktr->obmemsz, NULL);
1403 if (!retval)
1404 retval = bus_space_map(pa->pa_memt, bktr->phys_base,
1405 bktr->obmemsz, 0, &bktr->memh);
1406 if (retval) {
1407 printf(": couldn't map memory\n");
1408 return;
1409 }
1410
1411
1412 /*
1413 * map interrupt
1414 */
1415 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1416 pa->pa_intrline, &ih)) {
1417 printf(": couldn't map interrupt\n");
1418 return;
1419 }
1420 intrstr = pci_intr_string(pa->pa_pc, ih);
1421
1422 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1423 bktr_intr, bktr, bktr->bktr_dev.dv_xname);
1424 if (bktr->ih == NULL) {
1425 printf(": couldn't establish interrupt");
1426 if (intrstr != NULL)
1427 printf(" at %s", intrstr);
1428 printf("\n");
1429 return;
1430 }
1431
1432 if (intrstr != NULL)
1433 printf(": %s\n", intrstr);
1434 #endif /* __OpenBSD__ */
1435
1436 #if defined(__NetBSD__)
1437 struct pci_attach_args *pa = aux;
1438 pci_intr_handle_t ih;
1439 const char *intrstr;
1440 int retval;
1441 int unit;
1442
1443 bktr = (bktr_ptr_t)self;
1444 unit = bktr->bktr_dev.dv_unit;
1445 bktr->dmat = pa->pa_dmat;
1446
1447 printf("\n");
1448
1449 /*
1450 * map memory
1451 */
1452 retval = pci_mapreg_map(pa, PCI_MAPREG_START,
1453 PCI_MAPREG_TYPE_MEM
1454 | PCI_MAPREG_MEM_TYPE_32BIT, 0,
1455 &bktr->memt, &bktr->memh, NULL,
1456 &bktr->obmemsz);
1457 DPR(("pci_mapreg_map: memt %lx, memh %x, size %x\n",
1458 (long unsigned)bktr->memt, (u_int)bktr->memh,
1459 (u_int)bktr->obmemsz));
1460 if (retval) {
1461 printf("%s: couldn't map memory\n", bktr_name(bktr));
1462 return;
1463 }
1464
1465 /*
1466 * Disable the brooktree device
1467 */
1468 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1469 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1470
1471 /*
1472 * map interrupt
1473 */
1474 if (pci_intr_map(pa, &ih)) {
1475 printf("%s: couldn't map interrupt\n",
1476 bktr_name(bktr));
1477 return;
1478 }
1479 intrstr = pci_intr_string(pa->pa_pc, ih);
1480 bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
1481 bktr_intr, bktr);
1482 if (bktr->ih == NULL) {
1483 printf("%s: couldn't establish interrupt",
1484 bktr_name(bktr));
1485 if (intrstr != NULL)
1486 printf(" at %s", intrstr);
1487 printf("\n");
1488 return;
1489 }
1490 if (intrstr != NULL)
1491 printf("%s: interrupting at %s\n", bktr_name(bktr),
1492 intrstr);
1493 #endif /* __NetBSD__ */
1494
1495 /*
1496 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
1497 * you have more than four, then 16 would probably be a better value.
1498 */
1499 #ifndef BROOKTREE_DEF_LATENCY_VALUE
1500 #define BROOKTREE_DEF_LATENCY_VALUE 10
1501 #endif
1502 latency = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_LATENCY_TIMER);
1503 latency = (latency >> 8) & 0xff;
1504
1505 if (!latency) {
1506 if (bootverbose) {
1507 printf("%s: PCI bus latency was 0 changing to %d",
1508 bktr_name(bktr), BROOKTREE_DEF_LATENCY_VALUE);
1509 }
1510 latency = BROOKTREE_DEF_LATENCY_VALUE;
1511 pci_conf_write(pa->pa_pc, pa->pa_tag,
1512 PCI_LATENCY_TIMER, latency<<8);
1513 }
1514
1515
1516 /* Enabled Bus Master
1517 XXX: check if all old DMA is stopped first (e.g. after warm
1518 boot) */
1519 fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1520 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
1521 fun | PCI_COMMAND_MASTER_ENABLE);
1522
1523 /* read the pci id and determine the card type */
1524 fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
1525 rev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG) & 0x000000ff;
1526
1527 common_bktr_attach(bktr, unit, fun, rev);
1528
1529 #if NRADIO > 0
1530 /* attach to radio(4) */
1531 if (bktr->card.tuner->pllControl[3] != 0x00)
1532 radio_attach_mi(&bktr_hw_if, bktr, &bktr->bktr_dev);
1533 #endif
1534 }
1535
1536
1537 /*
1538 * Special Memory Allocation
1539 */
1540 #if defined (__NetBSD__)
1541 vaddr_t
1542 #else
1543 vm_offset_t
1544 #endif
1545 get_bktr_mem(bktr, dmapp, size)
1546 bktr_ptr_t bktr;
1547 bus_dmamap_t *dmapp;
1548 unsigned int size;
1549 {
1550 bus_dma_tag_t dmat = bktr->dmat;
1551 bus_dma_segment_t seg;
1552 bus_size_t align;
1553 int rseg;
1554 caddr_t kva;
1555
1556 /*
1557 * Allocate a DMA area
1558 */
1559 align = 1 << 24;
1560 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1561 &rseg, BUS_DMA_NOWAIT)) {
1562 align = PAGE_SIZE;
1563 if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
1564 &rseg, BUS_DMA_NOWAIT)) {
1565 printf("%s: Unable to dmamem_alloc of %d bytes\n",
1566 bktr_name(bktr), size);
1567 return 0;
1568 }
1569 }
1570 if (bus_dmamem_map(dmat, &seg, rseg, size,
1571 &kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
1572 printf("%s: Unable to dmamem_map of %d bytes\n",
1573 bktr_name(bktr), size);
1574 bus_dmamem_free(dmat, &seg, rseg);
1575 return 0;
1576 }
1577 #ifdef __OpenBSD__
1578 bktr->dm_mapsize = size;
1579 #endif
1580 /*
1581 * Create and locd the DMA map for the DMA area
1582 */
1583 if (bus_dmamap_create(dmat, size, 1, size, 0, BUS_DMA_NOWAIT, dmapp)) {
1584 printf("%s: Unable to dmamap_create of %d bytes\n",
1585 bktr_name(bktr), size);
1586 bus_dmamem_unmap(dmat, kva, size);
1587 bus_dmamem_free(dmat, &seg, rseg);
1588 return 0;
1589 }
1590 if (bus_dmamap_load(dmat, *dmapp, kva, size, NULL, BUS_DMA_NOWAIT)) {
1591 printf("%s: Unable to dmamap_load of %d bytes\n",
1592 bktr_name(bktr), size);
1593 bus_dmamem_unmap(dmat, kva, size);
1594 bus_dmamem_free(dmat, &seg, rseg);
1595 bus_dmamap_destroy(dmat, *dmapp);
1596 return 0;
1597 }
1598 #if defined(__NetBSD__)
1599 return (vaddr_t)kva;
1600 #else
1601 return (vm_offset_t)kva;
1602 #endif
1603 }
1604
1605 void
1606 free_bktr_mem(bktr, dmap, kva)
1607 bktr_ptr_t bktr;
1608 bus_dmamap_t dmap;
1609 #if defined(__NetBSD__)
1610 vaddr_t kva;
1611 #else
1612 vm_offset_t kva;
1613 #endif
1614 {
1615 bus_dma_tag_t dmat = bktr->dmat;
1616
1617 #ifdef __NetBSD__
1618 bus_dmamem_unmap(dmat, (caddr_t)kva, dmap->dm_mapsize);
1619 #else
1620 bus_dmamem_unmap(dmat, (caddr_t)kva, bktr->dm_mapsize);
1621 #endif
1622 bus_dmamem_free(dmat, dmap->dm_segs, 1);
1623 bus_dmamap_destroy(dmat, dmap);
1624 }
1625
1626
1627 /*---------------------------------------------------------
1628 **
1629 ** BrookTree 848 character device driver routines
1630 **
1631 **---------------------------------------------------------
1632 */
1633
1634
1635 #define VIDEO_DEV 0x00
1636 #define TUNER_DEV 0x01
1637 #define VBI_DEV 0x02
1638
1639 #define UNIT(x) (minor((x) & 0x0f))
1640 #define FUNCTION(x) (minor((x >> 4) & 0x0f))
1641
1642 /*
1643 *
1644 */
1645 int
1646 bktr_open(dev_t dev, int flags, int fmt, struct proc *p)
1647 {
1648 bktr_ptr_t bktr;
1649 int unit;
1650
1651 unit = UNIT(dev);
1652
1653 /* unit out of range */
1654 if ((unit >= bktr_cd.cd_ndevs) || (bktr_cd.cd_devs[unit] == NULL))
1655 return(ENXIO);
1656
1657 bktr = bktr_cd.cd_devs[unit];
1658
1659 if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
1660 return(ENXIO);
1661
1662 switch (FUNCTION(dev)) {
1663 case VIDEO_DEV:
1664 return(video_open(bktr));
1665 case TUNER_DEV:
1666 return(tuner_open(bktr));
1667 case VBI_DEV:
1668 return(vbi_open(bktr));
1669 }
1670
1671 return(ENXIO);
1672 }
1673
1674
1675 /*
1676 *
1677 */
1678 int
1679 bktr_close(dev_t dev, int flags, int fmt, struct proc *p)
1680 {
1681 bktr_ptr_t bktr;
1682 int unit;
1683
1684 unit = UNIT(dev);
1685
1686 bktr = bktr_cd.cd_devs[unit];
1687
1688 switch (FUNCTION(dev)) {
1689 case VIDEO_DEV:
1690 return(video_close(bktr));
1691 case TUNER_DEV:
1692 return(tuner_close(bktr));
1693 case VBI_DEV:
1694 return(vbi_close(bktr));
1695 }
1696
1697 return(ENXIO);
1698 }
1699
1700 /*
1701 *
1702 */
1703 int
1704 bktr_read(dev_t dev, struct uio *uio, int ioflag)
1705 {
1706 bktr_ptr_t bktr;
1707 int unit;
1708
1709 unit = UNIT(dev);
1710
1711 bktr = bktr_cd.cd_devs[unit];
1712
1713 switch (FUNCTION(dev)) {
1714 case VIDEO_DEV:
1715 return(video_read(bktr, unit, dev, uio));
1716 case VBI_DEV:
1717 return(vbi_read(bktr, uio, ioflag));
1718 }
1719
1720 return(ENXIO);
1721 }
1722
1723
1724 /*
1725 *
1726 */
1727 int
1728 bktr_write(dev_t dev, struct uio *uio, int ioflag)
1729 {
1730 /* operation not supported */
1731 return(EOPNOTSUPP);
1732 }
1733
1734 /*
1735 *
1736 */
1737 int
1738 bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr)
1739 {
1740 bktr_ptr_t bktr;
1741 int unit;
1742
1743 unit = UNIT(dev);
1744
1745 bktr = bktr_cd.cd_devs[unit];
1746
1747 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
1748 return(ENOMEM);
1749
1750 switch (FUNCTION(dev)) {
1751 case VIDEO_DEV:
1752 return(video_ioctl(bktr, unit, cmd, arg, pr));
1753 case TUNER_DEV:
1754 return(tuner_ioctl(bktr, unit, cmd, arg, pr));
1755 }
1756
1757 return(ENXIO);
1758 }
1759
1760 /*
1761 *
1762 */
1763 paddr_t
1764 bktr_mmap(dev_t dev, off_t offset, int nprot)
1765 {
1766 int unit;
1767 bktr_ptr_t bktr;
1768
1769 unit = UNIT(dev);
1770
1771 if (FUNCTION(dev) > 0) /* only allow mmap on /dev/bktr[n] */
1772 return(-1);
1773
1774 bktr = bktr_cd.cd_devs[unit];
1775
1776 if ((vaddr_t)offset < 0)
1777 return(-1);
1778
1779 if ((vaddr_t)offset >= bktr->alloc_pages * PAGE_SIZE)
1780 return(-1);
1781
1782 #ifdef __NetBSD__
1783 return (bus_dmamem_mmap(bktr->dmat, bktr->dm_mem->dm_segs, 1,
1784 (vaddr_t)offset, nprot, BUS_DMA_WAITOK));
1785 #else
1786 return(i386_btop(vtophys(bktr->bigbuf) + offset));
1787 #endif
1788 }
1789
1790 #if NRADIO > 0
1791 int
1792 bktr_set_info(void *v, struct radio_info *ri)
1793 {
1794 struct bktr_softc *sc = v;
1795 u_int32_t freq;
1796
1797 if (ri->mute) {
1798 /* mute the audio stream by switching the mux */
1799 set_audio(sc, AUDIO_MUTE);
1800
1801 /* disable 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);
1804 } else {
1805 /* enable drivers on the GPIO port that controls the MUXes */
1806 OUTL(sc, BKTR_GPIO_OUT_EN, INL(sc, BKTR_GPIO_OUT_EN) |
1807 sc->card.gpio_mux_bits);
1808
1809 /* unmute the audio stream */
1810 set_audio(sc, AUDIO_UNMUTE);
1811 init_audio_devices(sc);
1812 }
1813
1814 freq = ri->freq / 10;
1815 set_audio(sc, AUDIO_INTERN); /* use internal audio */
1816 temp_mute(sc, TRUE);
1817 ri->freq = tv_freq(sc, freq, FM_RADIO_FREQUENCY) * 10;
1818 temp_mute(sc, FALSE);
1819
1820 return (0);
1821 }
1822
1823 int
1824 bktr_get_info(void *v, struct radio_info *ri)
1825 {
1826 struct bktr_softc *sc = v;
1827 struct TVTUNER *tv = &sc->tuner;
1828 int status;
1829
1830 status = get_tuner_status(sc);
1831
1832 #define STATUSBIT_STEREO 0x10
1833 ri->mute = (int)sc->audio_mute_state ? 1 : 0;
1834 ri->stereo = (status & STATUSBIT_STEREO) ? 1 : 0;
1835 ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_HW_AFC;
1836 ri->freq = tv->frequency * 10;
1837 ri->info = (status & STATUSBIT_STEREO) ? RADIO_INFO_STEREO : 0;
1838 #undef STATUSBIT_STEREO
1839
1840 /* not yet supported */
1841 ri->volume = ri->rfreq = ri->lock = 0;
1842
1843 return (0);
1844 }
1845 #endif
1846
1847 #endif /* __NetBSD__ || __OpenBSD__ */
1848