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