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