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