xref: /dragonfly/sys/dev/video/bktr/bktr_core.c (revision 7b1120e5)
1 /*-
2  * 1. Redistributions of source code must retain the
3  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Amancio Hasty and
17  *      Roger Hardiman
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * 1. Redistributions of source code must retain the
35  * Copyright (c) 1995 Mark Tinguely and Jim Lowe
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Mark Tinguely and Jim Lowe
49  * 4. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
56  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
58  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
60  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
61  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  *
64  * $FreeBSD: src/sys/dev/bktr/bktr_core.c,v 1.140 2005/12/04 10:06:03 ru Exp $
65  */
66 
67 /*
68  * This is part of the Driver for Video Capture Cards (Frame grabbers)
69  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
70  * chipset.
71  * Copyright Roger Hardiman and Amancio Hasty.
72  *
73  * bktr_core : This deals with the Bt848/849/878/879 PCI Frame Grabber,
74  *               Handles all the open, close, ioctl and read userland calls.
75  *               Sets the Bt848 registers and generates RISC pograms.
76  *               Controls the i2c bus and GPIO interface.
77  *               Contains the interface to the kernel.
78  *               (eg probe/attach and open/close/ioctl)
79  */
80 
81  /*
82    The Brooktree BT848 Driver driver is based upon Mark Tinguely and
83    Jim Lowe's driver for the Matrox Meteor PCI card . The
84    Philips SAA 7116 and SAA 7196 are very different chipsets than
85    the BT848.
86 
87    The original copyright notice by Mark and Jim is included mostly
88    to honor their fantastic work in the Matrox Meteor driver!
89  */
90 
91 #include "opt_bktr.h"		/* Include any kernel config options */
92 
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/kernel.h>
96 #include <sys/lock.h>
97 #include <sys/proc.h>
98 #include <sys/signalvar.h>
99 #include <sys/vnode.h>
100 #include <sys/bus.h>		/* used by smbus and newbus */
101 #include <sys/thread2.h>
102 
103 #include <vm/vm.h>
104 #include <vm/vm_kern.h>
105 #include <vm/pmap.h>
106 #include <vm/vm_extern.h>
107 
108 #define	PROC_LOCK(p)
109 #define	PROC_UNLOCK(p)
110 #include <bus/pci/pcivar.h>
111 #include "pcidevs.h"
112 
113 #include <dev/video/meteor/ioctl_meteor.h>
114 #include <dev/video/bktr/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
115 #include <dev/video/bktr/bktr_reg.h>
116 #include <dev/video/bktr/bktr_tuner.h>
117 #include <dev/video/bktr/bktr_card.h>
118 #include <dev/video/bktr/bktr_audio.h>
119 #include <dev/video/bktr/bktr_os.h>
120 #include <dev/video/bktr/bktr_core.h>
121 #if defined(KLD_MODULE)
122 #include <dev/video/bktr/bktr_mem.h>
123 #endif
124 
125 const char *
126 bktr_name(bktr_ptr_t bktr)
127 {
128   return bktr->bktr_xname;
129 }
130 
131 typedef u_char bool_t;
132 
133 #define BKTRPRI PCATCH
134 #define VBIPRI  PCATCH
135 
136 
137 /*
138  * memory allocated for DMA programs
139  */
140 #define DMA_PROG_ALLOC		(8 * PAGE_SIZE)
141 
142 /* When to split a dma transfer , the bt848 has timing as well as
143    dma transfer size limitations so that we have to split dma
144    transfers into two dma requests
145    */
146 #define DMA_BT848_SPLIT 319*2
147 
148 /*
149  * Allocate enough memory for:
150  *	768x576 RGB 16 or YUV (16 storage bits/pixel) = 884736 = 216 pages
151  *
152  * You may override this using the options "BROOKTREE_ALLOC_PAGES=value"
153  * in your  kernel configuration file.
154  */
155 
156 #ifndef BROOKTREE_ALLOC_PAGES
157 #define BROOKTREE_ALLOC_PAGES	217*4
158 #endif
159 #define BROOKTREE_ALLOC		(BROOKTREE_ALLOC_PAGES * PAGE_SIZE)
160 
161 /* Definitions for VBI capture.
162  * There are 16 VBI lines in a PAL video field (32 in a frame),
163  * and we take 2044 samples from each line (placed in a 2048 byte buffer
164  * for alignment).
165  * VBI lines are held in a circular buffer before being read by a
166  * user program from /dev/vbi.
167  */
168 
169 #define MAX_VBI_LINES	      16   /* Maximum for all vidoe formats */
170 #define VBI_LINE_SIZE         2048 /* Store upto 2048 bytes per line */
171 #define VBI_BUFFER_ITEMS      20   /* Number of frames we buffer */
172 #define VBI_DATA_SIZE         (VBI_LINE_SIZE * MAX_VBI_LINES * 2)
173 #define VBI_BUFFER_SIZE       (VBI_DATA_SIZE * VBI_BUFFER_ITEMS)
174 
175 
176 /*  Defines for fields  */
177 #define ODD_F  0x01
178 #define EVEN_F 0x02
179 
180 
181 /*
182  * Parameters describing size of transmitted image.
183  */
184 
185 static struct format_params format_params[] = {
186 /* # define BT848_IFORM_F_AUTO             (0x0) - don't matter. */
187   { 525, 26, 480,  910, 135, 754, 640,  780, 30, 0x68, 0x5d, BT848_IFORM_X_AUTO,
188     12,  1600 },
189 /* # define BT848_IFORM_F_NTSCM            (0x1) */
190   { 525, 26, 480,  910, 135, 754, 640,  780, 30, 0x68, 0x5d, BT848_IFORM_X_XT0,
191     12, 1600 },
192 /* # define BT848_IFORM_F_NTSCJ            (0x2) */
193   { 525, 22, 480,  910, 135, 754, 640,  780, 30, 0x68, 0x5d, BT848_IFORM_X_XT0,
194     12, 1600 },
195 /* # define BT848_IFORM_F_PALBDGHI         (0x3) */
196   { 625, 32, 576, 1135, 186, 924, 768,  944, 25, 0x7f, 0x72, BT848_IFORM_X_XT1,
197     16,  2044 },
198 /* # define BT848_IFORM_F_PALM             (0x4) */
199   { 525, 22, 480,  910, 135, 754, 640,  780, 30, 0x68, 0x5d, BT848_IFORM_X_XT0,
200     12, 1600 },
201 /* # define BT848_IFORM_F_PALN             (0x5) */
202   { 625, 32, 576, 1135, 186, 924, 768,  944, 25, 0x7f, 0x72, BT848_IFORM_X_XT1,
203     16, 2044 },
204 /* # define BT848_IFORM_F_SECAM            (0x6) */
205   { 625, 32, 576, 1135, 186, 924, 768,  944, 25, 0x7f, 0xa0, BT848_IFORM_X_XT1,
206     16, 2044 },
207 /* # define BT848_IFORM_F_RSVD             (0x7) - ???? */
208   { 625, 32, 576, 1135, 186, 924, 768,  944, 25, 0x7f, 0x72, BT848_IFORM_X_XT0,
209     16, 2044 },
210 };
211 
212 /*
213  * Table of supported Pixel Formats
214  */
215 
216 static struct meteor_pixfmt_internal {
217 	struct meteor_pixfmt public;
218 	u_int                color_fmt;
219 } pixfmt_table[] = {
220 
221 { { 0, METEOR_PIXTYPE_RGB, 2, {   0x7c00,  0x03e0,  0x001f }, 0,0 }, 0x33 },
222 { { 0, METEOR_PIXTYPE_RGB, 2, {   0x7c00,  0x03e0,  0x001f }, 1,0 }, 0x33 },
223 
224 { { 0, METEOR_PIXTYPE_RGB, 2, {   0xf800,  0x07e0,  0x001f }, 0,0 }, 0x22 },
225 { { 0, METEOR_PIXTYPE_RGB, 2, {   0xf800,  0x07e0,  0x001f }, 1,0 }, 0x22 },
226 
227 { { 0, METEOR_PIXTYPE_RGB, 3, { 0xff0000,0x00ff00,0x0000ff }, 1,0 }, 0x11 },
228 
229 { { 0, METEOR_PIXTYPE_RGB, 4, { 0xff0000,0x00ff00,0x0000ff }, 0,0 }, 0x00 },
230 { { 0, METEOR_PIXTYPE_RGB, 4, { 0xff0000,0x00ff00,0x0000ff }, 0,1 }, 0x00 },
231 { { 0, METEOR_PIXTYPE_RGB, 4, { 0xff0000,0x00ff00,0x0000ff }, 1,0 }, 0x00 },
232 { { 0, METEOR_PIXTYPE_RGB, 4, { 0xff0000,0x00ff00,0x0000ff }, 1,1 }, 0x00 },
233 { { 0, METEOR_PIXTYPE_YUV, 2, { 0xff0000,0x00ff00,0x0000ff }, 1,1 }, 0x88 },
234 { { 0, METEOR_PIXTYPE_YUV_PACKED, 2, { 0xff0000,0x00ff00,0x0000ff }, 0,1 }, 0x44 },
235 { { 0, METEOR_PIXTYPE_YUV_12, 2, { 0xff0000,0x00ff00,0x0000ff }, 1,1 }, 0x88 },
236 
237 };
238 #define PIXFMT_TABLE_SIZE NELEM(pixfmt_table)
239 
240 /*
241  * Table of Meteor-supported Pixel Formats (for SETGEO compatibility)
242  */
243 
244 /*  FIXME:  Also add YUV_422 and YUV_PACKED as well  */
245 static struct {
246 	u_long               meteor_format;
247 	struct meteor_pixfmt public;
248 } meteor_pixfmt_table[] = {
249     { METEOR_GEO_YUV_12,
250       { 0, METEOR_PIXTYPE_YUV_12, 2, { 0xff0000,0x00ff00,0x0000ff }, 1,1 }
251     },
252 
253       /* FIXME: Should byte swap flag be on for this one; negative in drvr? */
254     { METEOR_GEO_YUV_422,
255       { 0, METEOR_PIXTYPE_YUV, 2, { 0xff0000,0x00ff00,0x0000ff }, 1,1 }
256     },
257     { METEOR_GEO_YUV_PACKED,
258       { 0, METEOR_PIXTYPE_YUV_PACKED, 2, { 0xff0000,0x00ff00,0x0000ff }, 0,1 }
259     },
260     { METEOR_GEO_RGB16,
261       { 0, METEOR_PIXTYPE_RGB, 2, {   0x7c00,   0x03e0,   0x001f }, 0, 0 }
262     },
263     { METEOR_GEO_RGB24,
264       { 0, METEOR_PIXTYPE_RGB, 4, { 0xff0000, 0x00ff00, 0x0000ff }, 0, 0 }
265     },
266 
267 };
268 #define METEOR_PIXFMT_TABLE_SIZE NELEM(meteor_pixfmt_table)
269 
270 
271 #define BSWAP (BT848_COLOR_CTL_BSWAP_ODD | BT848_COLOR_CTL_BSWAP_EVEN)
272 #define WSWAP (BT848_COLOR_CTL_WSWAP_ODD | BT848_COLOR_CTL_WSWAP_EVEN)
273 
274 
275 
276 /* sync detect threshold */
277 #if 0
278 #define SYNC_LEVEL		(BT848_ADC_RESERVED |	\
279 				 BT848_ADC_CRUSH)	/* threshold ~125 mV */
280 #else
281 #define SYNC_LEVEL		(BT848_ADC_RESERVED |	\
282 				 BT848_ADC_SYNC_T)	/* threshold ~75 mV */
283 #endif
284 
285 
286 
287 
288 /* debug utility for holding previous INT_STAT contents */
289 #define STATUS_SUM
290 static u_long	status_sum = 0;
291 
292 /*
293  * defines to make certain bit-fiddles understandable
294  */
295 #define FIFO_ENABLED		BT848_DMA_CTL_FIFO_EN
296 #define RISC_ENABLED		BT848_DMA_CTL_RISC_EN
297 #define FIFO_RISC_ENABLED	(BT848_DMA_CTL_FIFO_EN | BT848_DMA_CTL_RISC_EN)
298 #define FIFO_RISC_DISABLED	0
299 
300 #define ALL_INTS_DISABLED	0
301 #define ALL_INTS_CLEARED	0xffffffff
302 #define CAPTURE_OFF		0
303 
304 #define BIT_SEVEN_HIGH		(1<<7)
305 #define BIT_EIGHT_HIGH		(1<<8)
306 
307 #define I2C_BITS		(BT848_INT_RACK | BT848_INT_I2CDONE)
308 #define TDEC_BITS               (BT848_INT_FDSR | BT848_INT_FBUS)
309 
310 
311 
312 static int		oformat_meteor_to_bt( u_long format );
313 
314 static u_int		pixfmt_swap_flags( int pixfmt );
315 
316 /*
317  * bt848 RISC programming routines.
318  */
319 #ifdef BT848_DUMP
320 static int	dump_bt848( bktr_ptr_t bktr );
321 #endif
322 
323 static void	yuvpack_prog( bktr_ptr_t bktr, char i_flag, int cols,
324 			      int rows,  int interlace );
325 static void	yuv422_prog( bktr_ptr_t bktr, char i_flag, int cols,
326 			     int rows, int interlace );
327 static void	yuv12_prog( bktr_ptr_t bktr, char i_flag, int cols,
328 			     int rows, int interlace );
329 static void	rgb_prog( bktr_ptr_t bktr, char i_flag, int cols,
330 			  int rows, int interlace );
331 static void	rgb_vbi_prog( bktr_ptr_t bktr, char i_flag, int cols,
332 			  int rows, int interlace );
333 static void	build_dma_prog( bktr_ptr_t bktr, char i_flag );
334 
335 static bool_t   getline(bktr_reg_t *, int);
336 static bool_t   notclipped(bktr_reg_t * , int , int);
337 static bool_t   split(bktr_reg_t *, volatile uint32_t **, int, u_long, int,
338 		      volatile u_char ** , int  );
339 
340 static void	start_capture( bktr_ptr_t bktr, unsigned type );
341 static void	set_fps( bktr_ptr_t bktr, u_short fps );
342 
343 
344 
345 /*
346  * Remote Control Functions
347  */
348 static void	remote_read(bktr_ptr_t bktr, struct bktr_remote *remote);
349 
350 
351 /*
352  * ioctls common to both video & tuner.
353  */
354 static int	common_ioctl( bktr_ptr_t bktr, ioctl_cmd_t cmd, caddr_t arg );
355 
356 
357 /*
358  * i2c primitives for low level control of i2c bus. Added for MSP34xx control
359  */
360 static void     i2c_start( bktr_ptr_t bktr);
361 static void     i2c_stop( bktr_ptr_t bktr);
362 static int      i2c_write_byte( bktr_ptr_t bktr, unsigned char data);
363 static int      i2c_read_byte( bktr_ptr_t bktr, unsigned char *data, int last );
364 
365 
366 
367 /*
368  * the common attach code, used by all OS versions.
369  */
370 void
371 common_bktr_attach( bktr_ptr_t bktr, int unit, u_long pci_id, u_int rev )
372 {
373 	vm_offset_t	buf = 0;
374 	int		need_to_allocate_memory = 1;
375 #ifdef BKTR_NEW_MSP34XX_DRIVER
376 	int 		err;
377 #endif
378 
379 /* If this is a module, check if there is any currently saved contiguous memory */
380 #if defined(KLD_MODULE)
381 	if (bktr_has_stored_addresses(unit) == 1) {
382 		/* recover the addresses */
383 		bktr->dma_prog     = bktr_retrieve_address(unit, BKTR_MEM_DMA_PROG);
384 		bktr->odd_dma_prog = bktr_retrieve_address(unit, BKTR_MEM_ODD_DMA_PROG);
385 		bktr->vbidata      = bktr_retrieve_address(unit, BKTR_MEM_VBIDATA);
386 		bktr->vbibuffer    = bktr_retrieve_address(unit, BKTR_MEM_VBIBUFFER);
387 		buf                = bktr_retrieve_address(unit, BKTR_MEM_BUF);
388 		need_to_allocate_memory = 0;
389 	}
390 #endif
391 
392 	if (need_to_allocate_memory == 1) {
393 		/* allocate space for dma program */
394 		bktr->dma_prog     = get_bktr_mem(unit, DMA_PROG_ALLOC);
395 		bktr->odd_dma_prog = get_bktr_mem(unit, DMA_PROG_ALLOC);
396 
397 		/* allocte space for the VBI buffer */
398 		bktr->vbidata  = get_bktr_mem(unit, VBI_DATA_SIZE);
399 		bktr->vbibuffer = get_bktr_mem(unit, VBI_BUFFER_SIZE);
400 
401 		/* allocate space for pixel buffer */
402 		if ( BROOKTREE_ALLOC )
403 			buf = get_bktr_mem(unit, BROOKTREE_ALLOC);
404 		else
405 			buf = 0;
406 	}
407 
408 #ifdef USE_VBIMUTEX
409 	mtx_init(&bktr->vbimutex, "bktr vbi lock", NULL, MTX_DEF);
410 #endif
411 
412 /* If this is a module, save the current contiguous memory */
413 #if defined(KLD_MODULE)
414 bktr_store_address(unit, BKTR_MEM_DMA_PROG,     bktr->dma_prog);
415 bktr_store_address(unit, BKTR_MEM_ODD_DMA_PROG, bktr->odd_dma_prog);
416 bktr_store_address(unit, BKTR_MEM_VBIDATA,      bktr->vbidata);
417 bktr_store_address(unit, BKTR_MEM_VBIBUFFER,    bktr->vbibuffer);
418 bktr_store_address(unit, BKTR_MEM_BUF,          buf);
419 #endif
420 
421 
422 	if ( bootverbose ) {
423 		kprintf("%s: buffer size %d, addr %p\n",
424 			bktr_name(bktr), BROOKTREE_ALLOC,
425 			(void *)(uintptr_t)vtophys(buf));
426 	}
427 
428 	if ( buf != 0 ) {
429 		bktr->bigbuf = buf;
430 		bktr->alloc_pages = BROOKTREE_ALLOC_PAGES;
431 		bzero((caddr_t) bktr->bigbuf, BROOKTREE_ALLOC);
432 	} else {
433 		bktr->alloc_pages = 0;
434 	}
435 
436 
437 	bktr->flags = METEOR_INITALIZED | METEOR_AUTOMODE |
438 		      METEOR_DEV0 | METEOR_RGB16;
439 	bktr->dma_prog_loaded = FALSE;
440 	bktr->cols = 640;
441 	bktr->rows = 480;
442 	bktr->frames = 1;		/* one frame */
443 	bktr->format = METEOR_GEO_RGB16;
444 	bktr->pixfmt = oformat_meteor_to_bt( bktr->format );
445 	bktr->pixfmt_compat = TRUE;
446 
447 
448 	bktr->vbiinsert = 0;
449 	bktr->vbistart = 0;
450 	bktr->vbisize = 0;
451 	bktr->vbiflags = 0;
452 
453 
454 	/* using the pci device id and revision id */
455 	/* and determine the card type            */
456 	if (PCI_VENDOR(pci_id) == PCI_VENDOR_BROOKTREE)
457 	{
458 		switch (PCI_PRODUCT(pci_id)) {
459 		case PCI_PRODUCT_BROOKTREE_BT848:
460 			if (rev == 0x12)
461 				bktr->id = BROOKTREE_848A;
462 			else
463 				bktr->id = BROOKTREE_848;
464 			break;
465 		case PCI_PRODUCT_BROOKTREE_BT849:
466 			bktr->id = BROOKTREE_849A;
467 			break;
468 		case PCI_PRODUCT_BROOKTREE_BT878:
469 			bktr->id = BROOKTREE_878;
470 			break;
471 		case PCI_PRODUCT_BROOKTREE_BT879:
472 			bktr->id = BROOKTREE_879;
473 			break;
474 		}
475 	}
476 
477 	bktr->clr_on_start = FALSE;
478 
479 	/* defaults for the tuner section of the card */
480 	bktr->tflags = TUNER_INITALIZED;
481 	bktr->tuner.frequency = 0;
482 	bktr->tuner.channel = 0;
483 	bktr->tuner.chnlset = DEFAULT_CHNLSET;
484 	bktr->tuner.afc = 0;
485 	bktr->tuner.radio_mode = 0;
486 	bktr->audio_mux_select = 0;
487 	bktr->audio_mute_state = FALSE;
488 	bktr->bt848_card = -1;
489 	bktr->bt848_tuner = -1;
490 	bktr->reverse_mute = -1;
491 	bktr->slow_msp_audio = 0;
492 	bktr->msp_use_mono_source = 0;
493         bktr->msp_source_selected = -1;
494 	bktr->audio_mux_present = 1;
495 
496 #ifdef BKTR_NEW_MSP34XX_DRIVER
497 	/* get hint on short programming of the msp34xx, so we know */
498 	/* if the decision what thread to start should be overwritten */
499 	if ( (err = resource_int_value("bktr", unit, "mspsimple",
500 			&(bktr->mspsimple)) ) != 0 )
501 		bktr->mspsimple = -1;	/* fall back to default */
502 #endif
503 
504 	probeCard( bktr, TRUE, unit );
505 
506 	/* Initialise any MSP34xx or TDA98xx audio chips */
507 	init_audio_devices( bktr );
508 
509 #ifdef BKTR_NEW_MSP34XX_DRIVER
510 	/* setup the kenrel thread */
511 	err = msp_attach( bktr );
512 	if ( err != 0 ) /* error doing kernel thread stuff, disable msp3400c */
513 		bktr->card.msp3400c = 0;
514 #endif
515 
516 
517 }
518 
519 
520 /* Copy the vbi lines from 'vbidata' into the circular buffer, 'vbibuffer'.
521  * The circular buffer holds 'n' fixed size data blocks.
522  * vbisize   is the number of bytes in the circular buffer
523  * vbiread   is the point we reading data out of the circular buffer
524  * vbiinsert is the point we insert data into the circular buffer
525  */
526 static void vbidecode(bktr_ptr_t bktr) {
527         unsigned char *dest;
528 	unsigned int *seq_dest;
529 
530 	/* Check if there is room in the buffer to insert the data. */
531 	if (bktr->vbisize + VBI_DATA_SIZE > VBI_BUFFER_SIZE) return;
532 
533 	/* Copy the VBI data into the next free slot in the buffer. */
534 	/* 'dest' is the point in vbibuffer where we want to insert new data */
535         dest = (unsigned char *)bktr->vbibuffer + bktr->vbiinsert;
536         memcpy(dest, (unsigned char*)bktr->vbidata, VBI_DATA_SIZE);
537 
538 	/* Write the VBI sequence number to the end of the vbi data */
539 	/* This is used by the AleVT teletext program */
540 	seq_dest = (unsigned int *)((unsigned char *)bktr->vbibuffer
541 			+ bktr->vbiinsert
542 			+ (VBI_DATA_SIZE - sizeof(bktr->vbi_sequence_number)));
543 	*seq_dest = bktr->vbi_sequence_number;
544 
545 	/* And increase the VBI sequence number */
546 	/* This can wrap around */
547 	bktr->vbi_sequence_number++;
548 
549 
550 	/* Increment the vbiinsert pointer */
551 	/* This can wrap around */
552 	bktr->vbiinsert += VBI_DATA_SIZE;
553 	bktr->vbiinsert = (bktr->vbiinsert % VBI_BUFFER_SIZE);
554 
555 	/* And increase the amount of vbi data in the buffer */
556 	bktr->vbisize = bktr->vbisize + VBI_DATA_SIZE;
557 
558 }
559 
560 
561 /*
562  * the common interrupt handler.
563  * Returns a 0 or 1 depending on whether the interrupt has handled.
564  * In the OS specific section, bktr_intr() is defined which calls this
565  * common interrupt handler.
566  */
567 int
568 common_bktr_intr( void *arg )
569 {
570 	bktr_ptr_t		bktr;
571 	u_long			bktr_status;
572 	u_char			dstatus;
573 	u_long                  field;
574 	u_long                  w_field;
575 	u_long                  req_field;
576 
577 	bktr = (bktr_ptr_t) arg;
578 
579 	/*
580 	 * check to see if any interrupts are unmasked on this device.  If
581 	 * none are, then we likely got here by way of being on a PCI shared
582 	 * interrupt dispatch list.
583 	 */
584 	if (INL(bktr, BKTR_INT_MASK) == ALL_INTS_DISABLED)
585 	  	return 0;	/* bail out now, before we do something we
586 				   shouldn't */
587 
588 	if (!(bktr->flags & METEOR_OPEN)) {
589 		OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
590 		OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
591 		/* return; ?? */
592 	}
593 
594 	/* record and clear the INTerrupt status bits */
595 	bktr_status = INL(bktr, BKTR_INT_STAT);
596 	OUTL(bktr, BKTR_INT_STAT, bktr_status & ~I2C_BITS);	/* don't touch i2c */
597 
598 	/* record and clear the device status register */
599 	dstatus = INB(bktr, BKTR_DSTATUS);
600 	OUTB(bktr, BKTR_DSTATUS, 0x00);
601 
602 #if defined( STATUS_SUM )
603 	/* add any new device status or INTerrupt status bits */
604 	status_sum |= (bktr_status & ~(BT848_INT_RSV0|BT848_INT_RSV1));
605 	status_sum |= ((dstatus & (BT848_DSTATUS_COF|BT848_DSTATUS_LOF)) << 6);
606 #endif /* STATUS_SUM */
607 	/* kprintf( "%s: STATUS %x %x %x \n", bktr_name(bktr),
608 		dstatus, bktr_status, INL(bktr, BKTR_RISC_COUNT) );
609 	*/
610 
611 
612 	/* if risc was disabled re-start process again */
613 	/* if there was one of the following errors re-start again */
614 	if ( !(bktr_status & BT848_INT_RISC_EN) ||
615 	     ((bktr_status &(/* BT848_INT_FBUS   | */
616 			     /* BT848_INT_FTRGT  | */
617 			     /* BT848_INT_FDSR   | */
618 			      BT848_INT_PPERR  |
619 			      BT848_INT_RIPERR | BT848_INT_PABORT |
620 			      BT848_INT_OCERR  | BT848_INT_SCERR) ) != 0)
621 		|| ((INB(bktr, BKTR_TDEC) == 0) && (bktr_status & TDEC_BITS)) ) {
622 
623 		u_short	tdec_save = INB(bktr, BKTR_TDEC);
624 
625 		OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
626 		OUTB(bktr, BKTR_CAP_CTL, CAPTURE_OFF);
627 
628 		OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
629 
630 		/*  Reset temporal decimation counter  */
631 		OUTB(bktr, BKTR_TDEC, 0);
632 		OUTB(bktr, BKTR_TDEC, tdec_save);
633 
634 		/*  Reset to no-fields captured state  */
635 		if (bktr->flags & (METEOR_CONTIN | METEOR_SYNCAP)) {
636 			switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
637 			case METEOR_ONLY_ODD_FIELDS:
638 				bktr->flags |= METEOR_WANT_ODD;
639 				break;
640 			case METEOR_ONLY_EVEN_FIELDS:
641 				bktr->flags |= METEOR_WANT_EVEN;
642 				break;
643 			default:
644 				bktr->flags |= METEOR_WANT_MASK;
645 				break;
646 			}
647 		}
648 
649 		OUTL(bktr, BKTR_RISC_STRT_ADD, vtophys(bktr->dma_prog));
650 		OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
651 		OUTW(bktr, BKTR_GPIO_DMA_CTL, bktr->capcontrol);
652 
653 		OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT |
654 				    BT848_INT_RISCI      |
655 				    BT848_INT_VSYNC      |
656 				    BT848_INT_FMTCHG);
657 
658 		OUTB(bktr, BKTR_CAP_CTL, bktr->bktr_cap_ctl);
659 		return 1;
660 	}
661 
662 	/* If this is not a RISC program interrupt, return */
663 	if (!(bktr_status & BT848_INT_RISCI))
664 		return 0;
665 
666 /**
667 	kprintf( "%s: intr status %x %x %x\n", bktr_name(bktr),
668 		bktr_status, dstatus, INL(bktr, BKTR_RISC_COUNT) );
669  */
670 
671 
672 	/*
673 	 * Disable future interrupts if a capture mode is not selected.
674 	 * This can happen when we are in the process of closing or
675 	 * changing capture modes, otherwise it shouldn't happen.
676 	 */
677 	if (!(bktr->flags & METEOR_CAP_MASK))
678 		OUTB(bktr, BKTR_CAP_CTL, CAPTURE_OFF);
679 
680 
681 	/* Determine which field generated this interrupt */
682 	field = ( bktr_status & BT848_INT_FIELD ) ? EVEN_F : ODD_F;
683 
684 
685 	/*
686 	 * Process the VBI data if it is being captured. We do this once
687 	 * both Odd and Even VBI data is captured. Therefore we do this
688 	 * in the Even field interrupt handler.
689 	 */
690 	LOCK_VBI(bktr);
691 	if (  (bktr->vbiflags & VBI_CAPTURE)
692 	    &&(bktr->vbiflags & VBI_OPEN)
693             &&(field==EVEN_F)) {
694 		/* Put VBI data into circular buffer */
695                	vbidecode(bktr);
696 
697 		/* If someone is blocked on reading from /dev/vbi, wake them */
698 		if (bktr->vbi_read_blocked) {
699 			bktr->vbi_read_blocked = FALSE;
700           	     	wakeup(VBI_SLEEP);
701 		}
702 
703 		/* Inform anyone who is polling */
704 		KNOTE(&bktr->vbi_kq.ki_note, 0);
705 
706 	}
707 	UNLOCK_VBI(bktr);
708 
709 	/*
710 	 *  Register the completed field
711 	 *    (For dual-field mode, require fields from the same frame)
712 	 */
713 	switch ( bktr->flags & METEOR_WANT_MASK ) {
714 		case METEOR_WANT_ODD  : w_field = ODD_F         ;  break;
715 		case METEOR_WANT_EVEN : w_field = EVEN_F        ;  break;
716 		default               : w_field = (ODD_F|EVEN_F);  break;
717 	}
718 	switch ( bktr->flags & METEOR_ONLY_FIELDS_MASK ) {
719 		case METEOR_ONLY_ODD_FIELDS  : req_field = ODD_F  ;  break;
720 		case METEOR_ONLY_EVEN_FIELDS : req_field = EVEN_F ;  break;
721 		default                      : req_field = (ODD_F|EVEN_F);
722 			                       break;
723 	}
724 
725 	if (( field == EVEN_F ) && ( w_field == EVEN_F ))
726 		bktr->flags &= ~METEOR_WANT_EVEN;
727 	else if (( field == ODD_F ) && ( req_field == ODD_F ) &&
728 		 ( w_field == ODD_F ))
729 		bktr->flags &= ~METEOR_WANT_ODD;
730 	else if (( field == ODD_F ) && ( req_field == (ODD_F|EVEN_F) ) &&
731 		 ( w_field == (ODD_F|EVEN_F) ))
732 		bktr->flags &= ~METEOR_WANT_ODD;
733 	else if (( field == ODD_F ) && ( req_field == (ODD_F|EVEN_F) ) &&
734 		 ( w_field == ODD_F )) {
735 		bktr->flags &= ~METEOR_WANT_ODD;
736 		bktr->flags |=  METEOR_WANT_EVEN;
737 	}
738 	else {
739 		/*  We're out of sync.  Start over.  */
740 		if (bktr->flags & (METEOR_CONTIN | METEOR_SYNCAP)) {
741 			switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
742 			case METEOR_ONLY_ODD_FIELDS:
743 				bktr->flags |= METEOR_WANT_ODD;
744 				break;
745 			case METEOR_ONLY_EVEN_FIELDS:
746 				bktr->flags |= METEOR_WANT_EVEN;
747 				break;
748 			default:
749 				bktr->flags |= METEOR_WANT_MASK;
750 				break;
751 			}
752 		}
753 		return 1;
754 	}
755 
756 	/*
757 	 * If we have a complete frame.
758 	 */
759 	if (!(bktr->flags & METEOR_WANT_MASK)) {
760 		bktr->frames_captured++;
761 		/*
762 		 * post the completion time.
763 		 */
764 		if (bktr->flags & METEOR_WANT_TS) {
765 			struct timeval *ts;
766 
767 			if ((u_int) bktr->alloc_pages * PAGE_SIZE
768 			   <= (bktr->frame_size + sizeof(struct timeval))) {
769 				ts =(struct timeval *)bktr->bigbuf +
770 				  bktr->frame_size;
771 				/* doesn't work in synch mode except
772 				 *  for first frame */
773 				/* XXX */
774 				microtime(ts);
775 			}
776 		}
777 
778 
779 		/*
780 		 * Wake up the user in single capture mode.
781 		 */
782 		if (bktr->flags & METEOR_SINGLE) {
783 
784 			/* stop dma */
785 			OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
786 
787 			/* disable risc, leave fifo running */
788 			OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
789 			wakeup(BKTR_SLEEP);
790 		}
791 
792 		/*
793 		 * If the user requested to be notified via signal,
794 		 * let them know the frame is complete.
795 		 */
796 
797 		if (bktr->proc != NULL) {
798 			PROC_LOCK(bktr->proc);
799 			ksignal( bktr->proc, bktr->signal);
800 			PROC_UNLOCK(bktr->proc);
801 		}
802 
803 		/*
804 		 * Reset the want flags if in continuous or
805 		 * synchronous capture mode.
806 		 */
807 /*
808 * XXX NOTE (Luigi):
809 * currently we only support 3 capture modes: odd only, even only,
810 * odd+even interlaced (odd field first). A fourth mode (non interlaced,
811 * either even OR odd) could provide 60 (50 for PAL) pictures per
812 * second, but it would require this routine to toggle the desired frame
813 * each time, and one more different DMA program for the Bt848.
814 * As a consequence, this fourth mode is currently unsupported.
815 */
816 
817 		if (bktr->flags & (METEOR_CONTIN | METEOR_SYNCAP)) {
818 			switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
819 			case METEOR_ONLY_ODD_FIELDS:
820 				bktr->flags |= METEOR_WANT_ODD;
821 				break;
822 			case METEOR_ONLY_EVEN_FIELDS:
823 				bktr->flags |= METEOR_WANT_EVEN;
824 				break;
825 			default:
826 				bktr->flags |= METEOR_WANT_MASK;
827 				break;
828 			}
829 		}
830 	}
831 
832 	return 1;
833 }
834 
835 
836 
837 
838 /*
839  *
840  */
841 extern int bt848_format; /* used to set the default format, PAL or NTSC */
842 int
843 video_open( bktr_ptr_t bktr )
844 {
845 	int frame_rate, video_format=0;
846 
847 	if (bktr->flags & METEOR_OPEN)		/* device is busy */
848 		return( EBUSY );
849 
850 	bktr->flags |= METEOR_OPEN;
851 
852 #ifdef BT848_DUMP
853 	dump_bt848(bktr);
854 #endif
855 
856         bktr->clr_on_start = FALSE;
857 
858 	OUTB(bktr, BKTR_DSTATUS, 0x00);			/* clear device status reg. */
859 
860 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
861 
862 #if defined(BKTR_SYSTEM_DEFAULT) && BKTR_SYSTEM_DEFAULT == BROOKTREE_PAL
863 	video_format = 0;
864 #else
865 	video_format = 1;
866 #endif
867 
868 	if (bt848_format == 0 )
869 	  video_format = 0;
870 
871 	if (bt848_format == 1 )
872 	  video_format = 1;
873 
874 	if (video_format == 1 ) {
875 	  OUTB(bktr, BKTR_IFORM, BT848_IFORM_F_NTSCM);
876 	  bktr->format_params = BT848_IFORM_F_NTSCM;
877 
878 	} else {
879 	  OUTB(bktr, BKTR_IFORM, BT848_IFORM_F_PALBDGHI);
880 	  bktr->format_params = BT848_IFORM_F_PALBDGHI;
881 
882 	}
883 
884 	OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | format_params[bktr->format_params].iform_xtsel);
885 
886 	/* work around for new Hauppauge 878 cards */
887 	if ((bktr->card.card_id == CARD_HAUPPAUGE) &&
888 	    (bktr->id==BROOKTREE_878 || bktr->id==BROOKTREE_879) )
889 		OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX3);
890 	else
891 		OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX1);
892 
893 	OUTB(bktr, BKTR_ADELAY, format_params[bktr->format_params].adelay);
894 	OUTB(bktr, BKTR_BDELAY, format_params[bktr->format_params].bdelay);
895 	frame_rate    = format_params[bktr->format_params].frame_rate;
896 
897 	/* enable PLL mode using 28Mhz crystal for PAL/SECAM users */
898 	if (bktr->xtal_pll_mode == BT848_USE_PLL) {
899 		OUTB(bktr, BKTR_TGCTRL, 0);
900 		OUTB(bktr, BKTR_PLL_F_LO, 0xf9);
901 		OUTB(bktr, BKTR_PLL_F_HI, 0xdc);
902 		OUTB(bktr, BKTR_PLL_F_XCI, 0x8e);
903 	}
904 
905 	bktr->flags = (bktr->flags & ~METEOR_DEV_MASK) | METEOR_DEV0;
906 
907 	bktr->max_clip_node = 0;
908 
909 	OUTB(bktr, BKTR_COLOR_CTL, BT848_COLOR_CTL_GAMMA | BT848_COLOR_CTL_RGB_DED);
910 
911 	OUTB(bktr, BKTR_E_HSCALE_LO, 170);
912 	OUTB(bktr, BKTR_O_HSCALE_LO, 170);
913 
914 	OUTB(bktr, BKTR_E_DELAY_LO, 0x72);
915 	OUTB(bktr, BKTR_O_DELAY_LO, 0x72);
916 	OUTB(bktr, BKTR_E_SCLOOP, 0);
917 	OUTB(bktr, BKTR_O_SCLOOP, 0);
918 
919 	OUTB(bktr, BKTR_VBI_PACK_SIZE, 0);
920 	OUTB(bktr, BKTR_VBI_PACK_DEL, 0);
921 
922 	bktr->fifo_errors = 0;
923 	bktr->dma_errors = 0;
924 	bktr->frames_captured = 0;
925 	bktr->even_fields_captured = 0;
926 	bktr->odd_fields_captured = 0;
927 	bktr->proc = NULL;
928 	set_fps(bktr, frame_rate);
929 	bktr->video.addr = 0;
930 	bktr->video.width = 0;
931 	bktr->video.banksize = 0;
932 	bktr->video.ramsize = 0;
933 	bktr->pixfmt_compat = TRUE;
934 	bktr->format = METEOR_GEO_RGB16;
935 	bktr->pixfmt = oformat_meteor_to_bt( bktr->format );
936 
937 	bktr->capture_area_enabled = FALSE;
938 
939 	OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT);	/* if you take this out triton
940                                                    based motherboards will
941 						   operate unreliably */
942 	return( 0 );
943 }
944 
945 int
946 vbi_open( bktr_ptr_t bktr )
947 {
948 
949 	LOCK_VBI(bktr);
950 
951 	if (bktr->vbiflags & VBI_OPEN) {	/* device is busy */
952 		UNLOCK_VBI(bktr);
953 		return( EBUSY );
954 	}
955 
956 	bktr->vbiflags |= VBI_OPEN;
957 
958 	/* reset the VBI circular buffer pointers and clear the buffers */
959 	bktr->vbiinsert = 0;
960 	bktr->vbistart = 0;
961 	bktr->vbisize = 0;
962 	bktr->vbi_sequence_number = 0;
963 	bktr->vbi_read_blocked = FALSE;
964 
965 	bzero((caddr_t) bktr->vbibuffer, VBI_BUFFER_SIZE);
966 	bzero((caddr_t) bktr->vbidata,  VBI_DATA_SIZE);
967 
968 	UNLOCK_VBI(bktr);
969 
970 	return( 0 );
971 }
972 
973 /*
974  *
975  */
976 int
977 tuner_open( bktr_ptr_t bktr )
978 {
979 	if ( !(bktr->tflags & TUNER_INITALIZED) )	/* device not found */
980 		return( ENXIO );
981 
982 	if ( bktr->tflags & TUNER_OPEN )		/* already open */
983 		return( 0 );
984 
985 	bktr->tflags |= TUNER_OPEN;
986 	bktr->tuner.frequency = 0;
987 	bktr->tuner.channel = 0;
988 	bktr->tuner.chnlset = DEFAULT_CHNLSET;
989 	bktr->tuner.afc = 0;
990 	bktr->tuner.radio_mode = 0;
991 
992 	/* enable drivers on the GPIO port that control the MUXes */
993 	OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) | bktr->card.gpio_mux_bits);
994 
995 	/* unmute the audio stream */
996 	set_audio( bktr, AUDIO_UNMUTE );
997 
998 	/* Initialise any audio chips, eg MSP34xx or TDA98xx */
999 	init_audio_devices( bktr );
1000 
1001 	return( 0 );
1002 }
1003 
1004 
1005 
1006 
1007 /*
1008  *
1009  */
1010 int
1011 video_close( bktr_ptr_t bktr )
1012 {
1013 	bktr->flags &= ~(METEOR_OPEN     |
1014 			 METEOR_SINGLE   |
1015 			 METEOR_CAP_MASK |
1016 			 METEOR_WANT_MASK);
1017 
1018 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1019 	OUTB(bktr, BKTR_CAP_CTL, CAPTURE_OFF);
1020 
1021 	bktr->dma_prog_loaded = FALSE;
1022 	OUTB(bktr, BKTR_TDEC, 0);
1023 	OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1024 
1025 /** FIXME: is 0xf magic, wouldn't 0x00 work ??? */
1026 	OUTL(bktr, BKTR_SRESET, 0xf);
1027 	OUTL(bktr, BKTR_INT_STAT, ALL_INTS_CLEARED);
1028 
1029 	return( 0 );
1030 }
1031 
1032 
1033 /*
1034  * tuner close handle,
1035  *  place holder for tuner specific operations on a close.
1036  */
1037 int
1038 tuner_close( bktr_ptr_t bktr )
1039 {
1040 	bktr->tflags &= ~TUNER_OPEN;
1041 
1042 	/* mute the audio by switching the mux */
1043 	set_audio( bktr, AUDIO_MUTE );
1044 
1045 	/* disable drivers on the GPIO port that control the MUXes */
1046 	OUTL(bktr, BKTR_GPIO_OUT_EN, INL(bktr, BKTR_GPIO_OUT_EN) & ~bktr->card.gpio_mux_bits);
1047 
1048 	return( 0 );
1049 }
1050 
1051 int
1052 vbi_close( bktr_ptr_t bktr )
1053 {
1054 
1055 	LOCK_VBI(bktr);
1056 
1057 	bktr->vbiflags &= ~VBI_OPEN;
1058 
1059 	UNLOCK_VBI(bktr);
1060 
1061 	return( 0 );
1062 }
1063 
1064 /*
1065  *
1066  */
1067 int
1068 video_read(bktr_ptr_t bktr, int unit, cdev_t dev, struct uio *uio)
1069 {
1070         int             status;
1071         int             count;
1072 
1073 
1074 	if (bktr->bigbuf == 0)	/* no frame buffer allocated (ioctl failed) */
1075 		return( ENOMEM );
1076 
1077 	if (bktr->flags & METEOR_CAP_MASK)
1078 		return( EIO );	/* already capturing */
1079 
1080         OUTB(bktr, BKTR_CAP_CTL, bktr->bktr_cap_ctl);
1081 
1082 
1083 	count = bktr->rows * bktr->cols *
1084 		pixfmt_table[ bktr->pixfmt ].public.Bpp;
1085 
1086 	if ((int) uio->uio_iov->iov_len < count)
1087 		return( EINVAL );
1088 
1089 	bktr->flags &= ~(METEOR_CAP_MASK | METEOR_WANT_MASK);
1090 
1091 	/* capture one frame */
1092 	start_capture(bktr, METEOR_SINGLE);
1093 	/* wait for capture to complete */
1094 	OUTL(bktr, BKTR_INT_STAT, ALL_INTS_CLEARED);
1095 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
1096 	OUTW(bktr, BKTR_GPIO_DMA_CTL, bktr->capcontrol);
1097 	OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT |
1098                             BT848_INT_RISCI      |
1099                             BT848_INT_VSYNC      |
1100                             BT848_INT_FMTCHG);
1101 
1102 
1103 	status = tsleep(BKTR_SLEEP, BKTRPRI, "captur", 0);
1104 	if (!status)		/* successful capture */
1105 		status = uiomove((caddr_t)bktr->bigbuf, count, uio);
1106 	else
1107 		kprintf ("%s: read: tsleep error %d\n",
1108 			bktr_name(bktr), status);
1109 
1110 	bktr->flags &= ~(METEOR_SINGLE | METEOR_WANT_MASK);
1111 
1112 	return( status );
1113 }
1114 
1115 /*
1116  * Read VBI data from the vbi circular buffer
1117  * The buffer holds vbi data blocks which are the same size
1118  * vbiinsert is the position we will insert the next item into the buffer
1119  * vbistart is the actual position in the buffer we want to read from
1120  * vbisize is the exact number of bytes in the buffer left to read
1121  */
1122 int
1123 vbi_read(bktr_ptr_t bktr, struct uio *uio, int ioflag)
1124 {
1125 	int             readsize, readsize2, start;
1126 	int             status;
1127 
1128 	/*
1129 	 * XXX - vbi_read() should be protected against being re-entered
1130 	 * while it is unlocked for the uiomove.
1131 	 */
1132 	LOCK_VBI(bktr);
1133 
1134 	while(bktr->vbisize == 0) {
1135 		if (ioflag & IO_NDELAY) {
1136 			status = EWOULDBLOCK;
1137 			goto out;
1138 		}
1139 
1140 		bktr->vbi_read_blocked = TRUE;
1141 #ifdef USE_VBIMUTEX
1142 		if ((status = msleep(VBI_SLEEP, &bktr->vbimutex, VBIPRI, "vbi",
1143 		    0))) {
1144 			goto out;
1145 		}
1146 #else
1147 		if ((status = tsleep(VBI_SLEEP, VBIPRI, "vbi", 0))) {
1148 			goto out;
1149 		}
1150 #endif
1151 	}
1152 
1153 	/* Now we have some data to give to the user */
1154 
1155 	/* We cannot read more bytes than there are in
1156 	 * the circular buffer
1157 	 */
1158 	readsize = (int)uio->uio_iov->iov_len;
1159 
1160 	if (readsize > bktr->vbisize) readsize = bktr->vbisize;
1161 
1162 	/* Check if we can read this number of bytes without having
1163 	 * to wrap around the circular buffer */
1164 	if((bktr->vbistart + readsize) >= VBI_BUFFER_SIZE) {
1165 		/* We need to wrap around */
1166 
1167 		readsize2 = VBI_BUFFER_SIZE - bktr->vbistart;
1168 		start =  bktr->vbistart;
1169 		UNLOCK_VBI(bktr);
1170                	status = uiomove((caddr_t)bktr->vbibuffer + start, readsize2, uio);
1171 		if (status == 0)
1172 			status = uiomove((caddr_t)bktr->vbibuffer, (readsize - readsize2), uio);
1173 	} else {
1174 		UNLOCK_VBI(bktr);
1175 		/* We do not need to wrap around */
1176 		status = uiomove((caddr_t)bktr->vbibuffer + bktr->vbistart, readsize, uio);
1177 	}
1178 
1179 	LOCK_VBI(bktr);
1180 
1181 	/* Update the number of bytes left to read */
1182 	bktr->vbisize -= readsize;
1183 
1184 	/* Update vbistart */
1185 	bktr->vbistart += readsize;
1186 	bktr->vbistart = bktr->vbistart % VBI_BUFFER_SIZE; /* wrap around if needed */
1187 
1188 out:
1189 	UNLOCK_VBI(bktr);
1190 
1191 	return( status );
1192 
1193 }
1194 
1195 
1196 
1197 /*
1198  * video ioctls
1199  */
1200 int
1201 video_ioctl( bktr_ptr_t bktr, int unit, ioctl_cmd_t cmd, caddr_t arg, struct thread* td )
1202 {
1203 	volatile u_char		c_temp;
1204 	unsigned int		temp;
1205 	unsigned int		temp_iform;
1206 	unsigned int		error;
1207 	struct meteor_geomet	*geo;
1208 	struct meteor_counts	*counts;
1209 	struct meteor_video	*video;
1210 	struct bktr_capture_area *cap_area;
1211 	vm_offset_t		buf;
1212 	int                     i;
1213 	int			sig;
1214 	char                    char_temp;
1215 
1216 	switch ( cmd ) {
1217 
1218 	case BT848SCLIP: /* set clip region */
1219 	    bktr->max_clip_node = 0;
1220 	    memcpy(&bktr->clip_list, arg, sizeof(bktr->clip_list));
1221 
1222 	    for (i = 0; i < BT848_MAX_CLIP_NODE; i++) {
1223 		if (bktr->clip_list[i].y_min ==  0 &&
1224 		    bktr->clip_list[i].y_max == 0)
1225 		    break;
1226 	    }
1227 	    bktr->max_clip_node = i;
1228 
1229 	    /* make sure that the list contains a valid clip secquence */
1230 	    /* the clip rectangles should be sorted by x then by y as the
1231                second order sort key */
1232 
1233 	    /* clip rectangle list is terminated by y_min and y_max set to 0 */
1234 
1235 	    /* to disable clipping set  y_min and y_max to 0 in the first
1236                clip rectangle . The first clip rectangle is clip_list[0].
1237              */
1238 
1239 
1240 
1241 	    if (bktr->max_clip_node == 0 &&
1242 		(bktr->clip_list[0].y_min != 0 &&
1243 		 bktr->clip_list[0].y_max != 0)) {
1244 		return EINVAL;
1245 	    }
1246 
1247 	    for (i = 0; i < BT848_MAX_CLIP_NODE - 1 ; i++) {
1248 		if (bktr->clip_list[i].y_min == 0 &&
1249 		    bktr->clip_list[i].y_max == 0) {
1250 		    break;
1251 		}
1252 		if ( bktr->clip_list[i+1].y_min != 0 &&
1253 		     bktr->clip_list[i+1].y_max != 0 &&
1254 		     bktr->clip_list[i].x_min > bktr->clip_list[i+1].x_min ) {
1255 
1256 		    bktr->max_clip_node = 0;
1257 		    return (EINVAL);
1258 
1259 		 }
1260 
1261 		if (bktr->clip_list[i].x_min >= bktr->clip_list[i].x_max ||
1262 		    bktr->clip_list[i].y_min >= bktr->clip_list[i].y_max ||
1263 		    bktr->clip_list[i].x_min < 0 ||
1264 		    bktr->clip_list[i].x_max < 0 ||
1265 		    bktr->clip_list[i].y_min < 0 ||
1266 		    bktr->clip_list[i].y_max < 0 ) {
1267 		    bktr->max_clip_node = 0;
1268 		    return (EINVAL);
1269 		}
1270 	    }
1271 
1272 	    bktr->dma_prog_loaded = FALSE;
1273 
1274 	    break;
1275 
1276 	case METEORSTATUS:	/* get Bt848 status */
1277 		c_temp = INB(bktr, BKTR_DSTATUS);
1278 		temp = 0;
1279 		if (!(c_temp & 0x40)) temp |= METEOR_STATUS_HCLK;
1280 		if (!(c_temp & 0x10)) temp |= METEOR_STATUS_FIDT;
1281 		*(u_short *)arg = temp;
1282 		break;
1283 
1284 	case BT848SFMT:		/* set input format */
1285 		temp = *(unsigned long*)arg & BT848_IFORM_FORMAT;
1286 		temp_iform = INB(bktr, BKTR_IFORM);
1287 		temp_iform &= ~BT848_IFORM_FORMAT;
1288 		temp_iform &= ~BT848_IFORM_XTSEL;
1289 		OUTB(bktr, BKTR_IFORM, (temp_iform | temp | format_params[temp].iform_xtsel));
1290 		switch( temp ) {
1291 		case BT848_IFORM_F_AUTO:
1292 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1293 			METEOR_AUTOMODE;
1294 			break;
1295 
1296 		case BT848_IFORM_F_NTSCM:
1297 		case BT848_IFORM_F_NTSCJ:
1298 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1299 				METEOR_NTSC;
1300 			OUTB(bktr, BKTR_ADELAY, format_params[temp].adelay);
1301 			OUTB(bktr, BKTR_BDELAY, format_params[temp].bdelay);
1302 			bktr->format_params = temp;
1303 			break;
1304 
1305 		case BT848_IFORM_F_PALBDGHI:
1306 		case BT848_IFORM_F_PALN:
1307 		case BT848_IFORM_F_SECAM:
1308 		case BT848_IFORM_F_RSVD:
1309 		case BT848_IFORM_F_PALM:
1310 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1311 				METEOR_PAL;
1312 			OUTB(bktr, BKTR_ADELAY, format_params[temp].adelay);
1313 			OUTB(bktr, BKTR_BDELAY, format_params[temp].bdelay);
1314 			bktr->format_params = temp;
1315 			break;
1316 
1317 		}
1318 		bktr->dma_prog_loaded = FALSE;
1319 		break;
1320 
1321 	case METEORSFMT:	/* set input format */
1322 		temp_iform = INB(bktr, BKTR_IFORM);
1323 		temp_iform &= ~BT848_IFORM_FORMAT;
1324 		temp_iform &= ~BT848_IFORM_XTSEL;
1325 		switch(*(unsigned long *)arg & METEOR_FORM_MASK ) {
1326 		case 0:		/* default */
1327 		case METEOR_FMT_NTSC:
1328 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1329 				METEOR_NTSC;
1330 			OUTB(bktr, BKTR_IFORM, temp_iform | BT848_IFORM_F_NTSCM |
1331 		                         format_params[BT848_IFORM_F_NTSCM].iform_xtsel);
1332 			OUTB(bktr, BKTR_ADELAY, format_params[BT848_IFORM_F_NTSCM].adelay);
1333 			OUTB(bktr, BKTR_BDELAY, format_params[BT848_IFORM_F_NTSCM].bdelay);
1334 			bktr->format_params = BT848_IFORM_F_NTSCM;
1335 			break;
1336 
1337 		case METEOR_FMT_PAL:
1338 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1339 				METEOR_PAL;
1340 			OUTB(bktr, BKTR_IFORM, temp_iform | BT848_IFORM_F_PALBDGHI |
1341 		                         format_params[BT848_IFORM_F_PALBDGHI].iform_xtsel);
1342 			OUTB(bktr, BKTR_ADELAY, format_params[BT848_IFORM_F_PALBDGHI].adelay);
1343 			OUTB(bktr, BKTR_BDELAY, format_params[BT848_IFORM_F_PALBDGHI].bdelay);
1344 			bktr->format_params = BT848_IFORM_F_PALBDGHI;
1345 			break;
1346 
1347 		case METEOR_FMT_AUTOMODE:
1348 			bktr->flags = (bktr->flags & ~METEOR_FORM_MASK) |
1349 				METEOR_AUTOMODE;
1350 			OUTB(bktr, BKTR_IFORM, temp_iform | BT848_IFORM_F_AUTO |
1351 		                         format_params[BT848_IFORM_F_AUTO].iform_xtsel);
1352 			break;
1353 
1354 		default:
1355 			return( EINVAL );
1356 		}
1357 		bktr->dma_prog_loaded = FALSE;
1358 		break;
1359 
1360 	case METEORGFMT:	/* get input format */
1361 		*(u_long *)arg = bktr->flags & METEOR_FORM_MASK;
1362 		break;
1363 
1364 
1365 	case BT848GFMT:		/* get input format */
1366 	        *(u_long *)arg = INB(bktr, BKTR_IFORM) & BT848_IFORM_FORMAT;
1367 		break;
1368 
1369 	case METEORSCOUNT:	/* (re)set error counts */
1370 		counts = (struct meteor_counts *) arg;
1371 		bktr->fifo_errors = counts->fifo_errors;
1372 		bktr->dma_errors = counts->dma_errors;
1373 		bktr->frames_captured = counts->frames_captured;
1374 		bktr->even_fields_captured = counts->even_fields_captured;
1375 		bktr->odd_fields_captured = counts->odd_fields_captured;
1376 		break;
1377 
1378 	case METEORGCOUNT:	/* get error counts */
1379 		counts = (struct meteor_counts *) arg;
1380 		counts->fifo_errors = bktr->fifo_errors;
1381 		counts->dma_errors = bktr->dma_errors;
1382 		counts->frames_captured = bktr->frames_captured;
1383 		counts->even_fields_captured = bktr->even_fields_captured;
1384 		counts->odd_fields_captured = bktr->odd_fields_captured;
1385 		break;
1386 
1387 	case METEORGVIDEO:
1388 		video = (struct meteor_video *)arg;
1389 		video->addr = bktr->video.addr;
1390 		video->width = bktr->video.width;
1391 		video->banksize = bktr->video.banksize;
1392 		video->ramsize = bktr->video.ramsize;
1393 		break;
1394 
1395 	case METEORSVIDEO:
1396 		video = (struct meteor_video *)arg;
1397 		bktr->video.addr = video->addr;
1398 		bktr->video.width = video->width;
1399 		bktr->video.banksize = video->banksize;
1400 		bktr->video.ramsize = video->ramsize;
1401 		break;
1402 
1403 	case METEORSFPS:
1404 		set_fps(bktr, *(u_short *)arg);
1405 		break;
1406 
1407 	case METEORGFPS:
1408 		*(u_short *)arg = bktr->fps;
1409 		break;
1410 
1411 	case METEORSHUE:	/* set hue */
1412 		OUTB(bktr, BKTR_HUE, (*(u_char *) arg) & 0xff);
1413 		break;
1414 
1415 	case METEORGHUE:	/* get hue */
1416 		*(u_char *)arg = INB(bktr, BKTR_HUE);
1417 		break;
1418 
1419 	case METEORSBRIG:	/* set brightness */
1420 	        char_temp =    ( *(u_char *)arg & 0xff) - 128;
1421 		OUTB(bktr, BKTR_BRIGHT, char_temp);
1422 
1423 		break;
1424 
1425 	case METEORGBRIG:	/* get brightness */
1426 		*(u_char *)arg = INB(bktr, BKTR_BRIGHT);
1427 		break;
1428 
1429 	case METEORSCSAT:	/* set chroma saturation */
1430 		temp = (int)*(u_char *)arg;
1431 
1432 		OUTB(bktr, BKTR_SAT_U_LO, (temp << 1) & 0xff);
1433 		OUTB(bktr, BKTR_SAT_V_LO, (temp << 1) & 0xff);
1434 		OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL)
1435 		                     & ~(BT848_E_CONTROL_SAT_U_MSB
1436 					 | BT848_E_CONTROL_SAT_V_MSB));
1437 		OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL)
1438 		                     & ~(BT848_O_CONTROL_SAT_U_MSB |
1439 					 BT848_O_CONTROL_SAT_V_MSB));
1440 
1441 		if ( temp & BIT_SEVEN_HIGH ) {
1442 		        OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL)
1443 			                     | (BT848_E_CONTROL_SAT_U_MSB
1444 						| BT848_E_CONTROL_SAT_V_MSB));
1445 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL)
1446 			                     | (BT848_O_CONTROL_SAT_U_MSB
1447 						| BT848_O_CONTROL_SAT_V_MSB));
1448 		}
1449 		break;
1450 
1451 	case METEORGCSAT:	/* get chroma saturation */
1452 		temp = (INB(bktr, BKTR_SAT_V_LO) >> 1) & 0xff;
1453 		if ( INB(bktr, BKTR_E_CONTROL) & BT848_E_CONTROL_SAT_V_MSB )
1454 			temp |= BIT_SEVEN_HIGH;
1455 		*(u_char *)arg = (u_char)temp;
1456 		break;
1457 
1458 	case METEORSCONT:	/* set contrast */
1459 		temp = (int)*(u_char *)arg & 0xff;
1460 		temp <<= 1;
1461 		OUTB(bktr, BKTR_CONTRAST_LO, temp & 0xff);
1462 		OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_E_CONTROL_CON_MSB);
1463 		OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) & ~BT848_O_CONTROL_CON_MSB);
1464 		OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) |
1465 			(((temp & 0x100) >> 6 ) & BT848_E_CONTROL_CON_MSB));
1466 		OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) |
1467 			(((temp & 0x100) >> 6 ) & BT848_O_CONTROL_CON_MSB));
1468 		break;
1469 
1470 	case METEORGCONT:	/* get contrast */
1471 		temp = (int)INB(bktr, BKTR_CONTRAST_LO) & 0xff;
1472 		temp |= ((int)INB(bktr, BKTR_O_CONTROL) & 0x04) << 6;
1473 		*(u_char *)arg = (u_char)((temp >> 1) & 0xff);
1474 		break;
1475 
1476 	case BT848SCBUF:	/* set Clear-Buffer-on-start flag */
1477 		bktr->clr_on_start = (*(int *)arg != 0);
1478 		break;
1479 
1480 	case BT848GCBUF:	/* get Clear-Buffer-on-start flag */
1481 		*(int *)arg = (int) bktr->clr_on_start;
1482 		break;
1483 
1484 	case METEORSSIGNAL:
1485 		sig = *(int *)arg;
1486 		/* Historically, applications used METEOR_SIG_MODE_MASK
1487 		 * to reset signal delivery.
1488 		 */
1489 		if (sig == METEOR_SIG_MODE_MASK)
1490 			sig = 0;
1491 		if (sig < 0 || sig > _SIG_MAXSIG)
1492 			return (EINVAL);
1493 		bktr->signal = sig;
1494 		bktr->proc = sig ? td->td_proc : NULL;
1495 		break;
1496 
1497 	case METEORGSIGNAL:
1498 		*(int *)arg = bktr->signal;
1499 		break;
1500 
1501 	case METEORCAPTUR:
1502 		temp = bktr->flags;
1503 		switch (*(int *) arg) {
1504 		case METEOR_CAP_SINGLE:
1505 
1506 			if (bktr->bigbuf==0)	/* no frame buffer allocated */
1507 				return( ENOMEM );
1508 			/* already capturing */
1509 			if (temp & METEOR_CAP_MASK)
1510 				return( EIO );
1511 
1512 
1513 
1514 			start_capture(bktr, METEOR_SINGLE);
1515 
1516 			/* wait for capture to complete */
1517 			OUTL(bktr, BKTR_INT_STAT, ALL_INTS_CLEARED);
1518 			OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
1519 			OUTW(bktr, BKTR_GPIO_DMA_CTL, bktr->capcontrol);
1520 
1521 			OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT |
1522 			     		    BT848_INT_RISCI      |
1523 					    BT848_INT_VSYNC      |
1524 					    BT848_INT_FMTCHG);
1525 
1526 			OUTB(bktr, BKTR_CAP_CTL, bktr->bktr_cap_ctl);
1527 			error = tsleep(BKTR_SLEEP, BKTRPRI, "captur", hz);
1528 			if (error && (error != ERESTART)) {
1529 				/*  Here if we didn't get complete frame  */
1530 #ifdef DIAGNOSTIC
1531 				kprintf( "%s: ioctl: tsleep error %d %x\n",
1532 					bktr_name(bktr), error,
1533 					INL(bktr, BKTR_RISC_COUNT));
1534 #endif
1535 
1536 				/* stop dma */
1537 				OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1538 
1539 				/* disable risc, leave fifo running */
1540 				OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
1541 			}
1542 
1543 			bktr->flags &= ~(METEOR_SINGLE|METEOR_WANT_MASK);
1544 			/* FIXME: should we set bt848->int_stat ??? */
1545 			break;
1546 
1547 		case METEOR_CAP_CONTINOUS:
1548 			if (bktr->bigbuf==0)	/* no frame buffer allocated */
1549 				return( ENOMEM );
1550 			/* already capturing */
1551 			if (temp & METEOR_CAP_MASK)
1552 			    return( EIO );
1553 
1554 
1555 			start_capture(bktr, METEOR_CONTIN);
1556 
1557 			/* Clear the interrypt status register */
1558 			OUTL(bktr, BKTR_INT_STAT, INL(bktr, BKTR_INT_STAT));
1559 
1560 			OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
1561 			OUTW(bktr, BKTR_GPIO_DMA_CTL, bktr->capcontrol);
1562 			OUTB(bktr, BKTR_CAP_CTL, bktr->bktr_cap_ctl);
1563 
1564 			OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT |
1565 					    BT848_INT_RISCI      |
1566 			                    BT848_INT_VSYNC      |
1567 					    BT848_INT_FMTCHG);
1568 #ifdef BT848_DUMP
1569 			dump_bt848(bktr);
1570 #endif
1571 			break;
1572 
1573 		case METEOR_CAP_STOP_CONT:
1574 			if (bktr->flags & METEOR_CONTIN) {
1575 				/* turn off capture */
1576 				OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1577 				OUTB(bktr, BKTR_CAP_CTL, CAPTURE_OFF);
1578 				OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1579 				bktr->flags &=
1580 					~(METEOR_CONTIN | METEOR_WANT_MASK);
1581 
1582 			}
1583 		}
1584 		break;
1585 
1586 	case METEORSETGEO:
1587 		/* can't change parameters while capturing */
1588 		if (bktr->flags & METEOR_CAP_MASK)
1589 			return( EBUSY );
1590 
1591 
1592 		geo = (struct meteor_geomet *) arg;
1593 
1594 		error = 0;
1595 		/* Either even or odd, if even & odd, then these a zero */
1596 		if ((geo->oformat & METEOR_GEO_ODD_ONLY) &&
1597 			(geo->oformat & METEOR_GEO_EVEN_ONLY)) {
1598 			kprintf( "%s: ioctl: Geometry odd or even only.\n",
1599 				bktr_name(bktr));
1600 			return( EINVAL );
1601 		}
1602 
1603 		/* set/clear even/odd flags */
1604 		if (geo->oformat & METEOR_GEO_ODD_ONLY)
1605 			bktr->flags |= METEOR_ONLY_ODD_FIELDS;
1606 		else
1607 			bktr->flags &= ~METEOR_ONLY_ODD_FIELDS;
1608 		if (geo->oformat & METEOR_GEO_EVEN_ONLY)
1609 			bktr->flags |= METEOR_ONLY_EVEN_FIELDS;
1610 		else
1611 			bktr->flags &= ~METEOR_ONLY_EVEN_FIELDS;
1612 
1613 		if (geo->columns <= 0) {
1614 			kprintf(
1615 			"%s: ioctl: %d: columns must be greater than zero.\n",
1616 				bktr_name(bktr), geo->columns);
1617 			error = EINVAL;
1618 		}
1619 		else if ((geo->columns & 0x3fe) != geo->columns) {
1620 			kprintf(
1621 			"%s: ioctl: %d: columns too large or not even.\n",
1622 				bktr_name(bktr), geo->columns);
1623 			error = EINVAL;
1624 		}
1625 
1626 		if (geo->rows <= 0) {
1627 			kprintf(
1628 			"%s: ioctl: %d: rows must be greater than zero.\n",
1629 				bktr_name(bktr), geo->rows);
1630 			error = EINVAL;
1631 		}
1632 		else if (((geo->rows & 0x7fe) != geo->rows) ||
1633 			((geo->oformat & METEOR_GEO_FIELD_MASK) &&
1634 				((geo->rows & 0x3fe) != geo->rows)) ) {
1635 			kprintf(
1636 			"%s: ioctl: %d: rows too large or not even.\n",
1637 				bktr_name(bktr), geo->rows);
1638 			error = EINVAL;
1639 		}
1640 
1641 		if (geo->frames > 32) {
1642 			kprintf("%s: ioctl: too many frames.\n",
1643 			       bktr_name(bktr));
1644 
1645 			error = EINVAL;
1646 		}
1647 
1648 		if (error)
1649 			return( error );
1650 
1651 		bktr->dma_prog_loaded = FALSE;
1652 		OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
1653 
1654 		OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
1655 
1656 		if ((temp=(geo->rows * geo->columns * geo->frames * 2))) {
1657 			if (geo->oformat & METEOR_GEO_RGB24) temp = temp * 2;
1658 
1659 			/* meteor_mem structure for SYNC Capture */
1660 			if (geo->frames > 1) temp += PAGE_SIZE;
1661 
1662 			temp = btoc(temp);
1663 			if ((int) temp > bktr->alloc_pages
1664 			    && bktr->video.addr == 0) {
1665 
1666                                 buf = get_bktr_mem(unit, temp*PAGE_SIZE);
1667                                 if (buf != 0) {
1668                                         kmem_free(&kernel_map, bktr->bigbuf,
1669                                           (bktr->alloc_pages * PAGE_SIZE));
1670 
1671 					bktr->bigbuf = buf;
1672 					bktr->alloc_pages = temp;
1673 					if (bootverbose)
1674 						kprintf(
1675 				"%s: ioctl: Allocating %d bytes\n",
1676 							bktr_name(bktr), temp*PAGE_SIZE);
1677 				}
1678 				else
1679 					error = ENOMEM;
1680 			}
1681 		}
1682 
1683 		if (error)
1684 			return error;
1685 
1686 		bktr->rows = geo->rows;
1687 		bktr->cols = geo->columns;
1688 		bktr->frames = geo->frames;
1689 
1690 		/*  Pixel format (if in meteor pixfmt compatibility mode)  */
1691 		if ( bktr->pixfmt_compat ) {
1692 			bktr->format = METEOR_GEO_YUV_422;
1693 			switch (geo->oformat & METEOR_GEO_OUTPUT_MASK) {
1694 			case 0:			/* default */
1695 			case METEOR_GEO_RGB16:
1696 				    bktr->format = METEOR_GEO_RGB16;
1697 				    break;
1698 			case METEOR_GEO_RGB24:
1699 				    bktr->format = METEOR_GEO_RGB24;
1700 				    break;
1701 			case METEOR_GEO_YUV_422:
1702 				    bktr->format = METEOR_GEO_YUV_422;
1703                                     if (geo->oformat & METEOR_GEO_YUV_12)
1704 					bktr->format = METEOR_GEO_YUV_12;
1705 				    break;
1706 			case METEOR_GEO_YUV_PACKED:
1707 				    bktr->format = METEOR_GEO_YUV_PACKED;
1708 				    break;
1709 			}
1710 			bktr->pixfmt = oformat_meteor_to_bt( bktr->format );
1711 		}
1712 
1713 		if (bktr->flags & METEOR_CAP_MASK) {
1714 
1715 			if (bktr->flags & (METEOR_CONTIN|METEOR_SYNCAP)) {
1716 				switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
1717 				case METEOR_ONLY_ODD_FIELDS:
1718 					bktr->flags |= METEOR_WANT_ODD;
1719 					break;
1720 				case METEOR_ONLY_EVEN_FIELDS:
1721 					bktr->flags |= METEOR_WANT_EVEN;
1722 					break;
1723 				default:
1724 					bktr->flags |= METEOR_WANT_MASK;
1725 					break;
1726 				}
1727 
1728 				start_capture(bktr, METEOR_CONTIN);
1729 				OUTL(bktr, BKTR_INT_STAT, INL(bktr, BKTR_INT_STAT));
1730 				OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_ENABLED);
1731 				OUTW(bktr, BKTR_GPIO_DMA_CTL, bktr->capcontrol);
1732 				OUTL(bktr, BKTR_INT_MASK, BT848_INT_MYSTERYBIT |
1733 						    BT848_INT_VSYNC      |
1734 						    BT848_INT_FMTCHG);
1735 			}
1736 		}
1737 		break;
1738 	/* end of METEORSETGEO */
1739 
1740 	/* FIXME. The Capture Area currently has the following restrictions:
1741 	GENERAL
1742 	 y_offset may need to be even in interlaced modes
1743 	RGB24 - Interlaced mode
1744 	 x_size must be greater than or equal to 1.666*METEORSETGEO width (cols)
1745 	 y_size must be greater than or equal to METEORSETGEO height (rows)
1746 	RGB24 - Even Only (or Odd Only) mode
1747 	 x_size must be greater than or equal to 1.666*METEORSETGEO width (cols)
1748 	 y_size must be greater than or equal to 2*METEORSETGEO height (rows)
1749 	YUV12 - Interlaced mode
1750 	 x_size must be greater than or equal to METEORSETGEO width (cols)
1751 	 y_size must be greater than or equal to METEORSETGEO height (rows)
1752 	YUV12 - Even Only (or Odd Only) mode
1753 	 x_size must be greater than or equal to METEORSETGEO width (cols)
1754 	 y_size must be greater than or equal to 2*METEORSETGEO height (rows)
1755 	*/
1756 
1757 	case BT848_SCAPAREA: /* set capture area of each video frame */
1758 		/* can't change parameters while capturing */
1759 		if (bktr->flags & METEOR_CAP_MASK)
1760 			return( EBUSY );
1761 
1762 		cap_area = (struct bktr_capture_area *) arg;
1763 		bktr->capture_area_x_offset = cap_area->x_offset;
1764 		bktr->capture_area_y_offset = cap_area->y_offset;
1765 		bktr->capture_area_x_size   = cap_area->x_size;
1766 		bktr->capture_area_y_size   = cap_area->y_size;
1767 		bktr->capture_area_enabled  = TRUE;
1768 
1769 		bktr->dma_prog_loaded = FALSE;
1770 		break;
1771 
1772 	case BT848_GCAPAREA: /* get capture area of each video frame */
1773 		cap_area = (struct bktr_capture_area *) arg;
1774 		if (bktr->capture_area_enabled == FALSE) {
1775 			cap_area->x_offset = 0;
1776 			cap_area->y_offset = 0;
1777 			cap_area->x_size   = format_params[
1778 				bktr->format_params].scaled_hactive;
1779 			cap_area->y_size   = format_params[
1780 				bktr->format_params].vactive;
1781 		} else {
1782 			cap_area->x_offset = bktr->capture_area_x_offset;
1783 			cap_area->y_offset = bktr->capture_area_y_offset;
1784 			cap_area->x_size   = bktr->capture_area_x_size;
1785 			cap_area->y_size   = bktr->capture_area_y_size;
1786 		}
1787 		break;
1788 
1789 	default:
1790 		return common_ioctl( bktr, cmd, arg );
1791 	}
1792 
1793 	return( 0 );
1794 }
1795 
1796 /*
1797  * tuner ioctls
1798  */
1799 int
1800 tuner_ioctl( bktr_ptr_t bktr, int unit, ioctl_cmd_t cmd, caddr_t arg, struct thread* td )
1801 {
1802 	int		tmp_int;
1803 	int		temp, temp1;
1804 	int		offset;
1805 	int		count;
1806 	u_char		*buf;
1807 	u_long          par;
1808 	u_char          write;
1809 	int             i2c_addr;
1810 	int             i2c_port;
1811 	u_long          data;
1812 
1813 	switch ( cmd ) {
1814 
1815 	case REMOTE_GETKEY:
1816 		/* Read the last key pressed by the Remote Control */
1817 		if (bktr->remote_control == 0) return (EINVAL);
1818 		remote_read(bktr, (struct bktr_remote *)arg);
1819 		break;
1820 
1821 #if defined( TUNER_AFC )
1822 	case TVTUNER_SETAFC:
1823 		bktr->tuner.afc = (*(int *)arg != 0);
1824 		break;
1825 
1826 	case TVTUNER_GETAFC:
1827 		*(int *)arg = bktr->tuner.afc;
1828 		/* XXX Perhaps use another bit to indicate AFC success? */
1829 		break;
1830 #endif /* TUNER_AFC */
1831 
1832 	case TVTUNER_SETCHNL:
1833 		temp_mute( bktr, TRUE );
1834 		temp = tv_channel( bktr, (int)*(unsigned long *)arg );
1835 		if ( temp < 0 ) {
1836 			temp_mute( bktr, FALSE );
1837 			return( EINVAL );
1838 		}
1839 		*(unsigned long *)arg = temp;
1840 
1841 		/* after every channel change, we must restart the MSP34xx */
1842 		/* audio chip to reselect NICAM STEREO or MONO audio */
1843 		if ( bktr->card.msp3400c )
1844 		  msp_autodetect( bktr );
1845 
1846 		/* after every channel change, we must restart the DPL35xx */
1847 		if ( bktr->card.dpl3518a )
1848 		  dpl_autodetect( bktr );
1849 
1850 		temp_mute( bktr, FALSE );
1851 		break;
1852 
1853 	case TVTUNER_GETCHNL:
1854 		*(unsigned long *)arg = bktr->tuner.channel;
1855 		break;
1856 
1857 	case TVTUNER_SETTYPE:
1858 		temp = *(unsigned long *)arg;
1859 		if ( (temp < CHNLSET_MIN) || (temp > CHNLSET_MAX) )
1860 			return( EINVAL );
1861 		bktr->tuner.chnlset = temp;
1862 		break;
1863 
1864 	case TVTUNER_GETTYPE:
1865 		*(unsigned long *)arg = bktr->tuner.chnlset;
1866 		break;
1867 
1868 	case TVTUNER_GETSTATUS:
1869 		temp = get_tuner_status( bktr );
1870 		*(unsigned long *)arg = temp & 0xff;
1871 		break;
1872 
1873 	case TVTUNER_SETFREQ:
1874 		temp_mute( bktr, TRUE );
1875 		temp = tv_freq( bktr, (int)*(unsigned long *)arg, TV_FREQUENCY);
1876 		temp_mute( bktr, FALSE );
1877 		if ( temp < 0 ) {
1878 			temp_mute( bktr, FALSE );
1879 			return( EINVAL );
1880 		}
1881 		*(unsigned long *)arg = temp;
1882 
1883 		/* after every channel change, we must restart the MSP34xx */
1884 		/* audio chip to reselect NICAM STEREO or MONO audio */
1885 		if ( bktr->card.msp3400c )
1886 		  msp_autodetect( bktr );
1887 
1888 		/* after every channel change, we must restart the DPL35xx */
1889 		if ( bktr->card.dpl3518a )
1890 		  dpl_autodetect( bktr );
1891 
1892 		temp_mute( bktr, FALSE );
1893 		break;
1894 
1895 	case TVTUNER_GETFREQ:
1896 		*(unsigned long *)arg = bktr->tuner.frequency;
1897 		break;
1898 
1899 	case TVTUNER_GETCHNLSET:
1900 		return tuner_getchnlset((struct bktr_chnlset *)arg);
1901 
1902 	case BT848_SAUDIO:	/* set audio channel */
1903 		if ( set_audio( bktr, *(int*)arg ) < 0 )
1904 			return( EIO );
1905 		break;
1906 
1907 	/* hue is a 2's compliment number, -90' to +89.3' in 0.7' steps */
1908 	case BT848_SHUE:	/* set hue */
1909 		OUTB(bktr, BKTR_HUE, (u_char)(*(int*)arg & 0xff));
1910 		break;
1911 
1912 	case BT848_GHUE:	/* get hue */
1913 		*(int*)arg = (signed char)(INB(bktr, BKTR_HUE) & 0xff);
1914 		break;
1915 
1916 	/* brightness is a 2's compliment #, -50 to +%49.6% in 0.39% steps */
1917 	case BT848_SBRIG:	/* set brightness */
1918 		OUTB(bktr, BKTR_BRIGHT, (u_char)(*(int *)arg & 0xff));
1919 		break;
1920 
1921 	case BT848_GBRIG:	/* get brightness */
1922 		*(int *)arg = (signed char)(INB(bktr, BKTR_BRIGHT) & 0xff);
1923 		break;
1924 
1925 	/*  */
1926 	case BT848_SCSAT:	/* set chroma saturation */
1927 		tmp_int = *(int*)arg;
1928 
1929 		temp = INB(bktr, BKTR_E_CONTROL);
1930 		temp1 = INB(bktr, BKTR_O_CONTROL);
1931 		if ( tmp_int & BIT_EIGHT_HIGH ) {
1932 			temp |= (BT848_E_CONTROL_SAT_U_MSB |
1933 				 BT848_E_CONTROL_SAT_V_MSB);
1934 			temp1 |= (BT848_O_CONTROL_SAT_U_MSB |
1935 				  BT848_O_CONTROL_SAT_V_MSB);
1936 		}
1937 		else {
1938 			temp &= ~(BT848_E_CONTROL_SAT_U_MSB |
1939 				  BT848_E_CONTROL_SAT_V_MSB);
1940 			temp1 &= ~(BT848_O_CONTROL_SAT_U_MSB |
1941 				   BT848_O_CONTROL_SAT_V_MSB);
1942 		}
1943 
1944 		OUTB(bktr, BKTR_SAT_U_LO, (u_char)(tmp_int & 0xff));
1945 		OUTB(bktr, BKTR_SAT_V_LO, (u_char)(tmp_int & 0xff));
1946 		OUTB(bktr, BKTR_E_CONTROL, temp);
1947 		OUTB(bktr, BKTR_O_CONTROL, temp1);
1948 		break;
1949 
1950 	case BT848_GCSAT:	/* get chroma saturation */
1951 		tmp_int = (int)(INB(bktr, BKTR_SAT_V_LO) & 0xff);
1952 		if ( INB(bktr, BKTR_E_CONTROL) & BT848_E_CONTROL_SAT_V_MSB )
1953 			tmp_int |= BIT_EIGHT_HIGH;
1954 		*(int*)arg = tmp_int;
1955 		break;
1956 
1957 	/*  */
1958 	case BT848_SVSAT:	/* set chroma V saturation */
1959 		tmp_int = *(int*)arg;
1960 
1961 		temp = INB(bktr, BKTR_E_CONTROL);
1962 		temp1 = INB(bktr, BKTR_O_CONTROL);
1963 		if ( tmp_int & BIT_EIGHT_HIGH) {
1964 			temp |= BT848_E_CONTROL_SAT_V_MSB;
1965 			temp1 |= BT848_O_CONTROL_SAT_V_MSB;
1966 		}
1967 		else {
1968 			temp &= ~BT848_E_CONTROL_SAT_V_MSB;
1969 			temp1 &= ~BT848_O_CONTROL_SAT_V_MSB;
1970 		}
1971 
1972 		OUTB(bktr, BKTR_SAT_V_LO, (u_char)(tmp_int & 0xff));
1973 		OUTB(bktr, BKTR_E_CONTROL, temp);
1974 		OUTB(bktr, BKTR_O_CONTROL, temp1);
1975 		break;
1976 
1977 	case BT848_GVSAT:	/* get chroma V saturation */
1978 		tmp_int = (int)INB(bktr, BKTR_SAT_V_LO) & 0xff;
1979 		if ( INB(bktr, BKTR_E_CONTROL) & BT848_E_CONTROL_SAT_V_MSB )
1980 			tmp_int |= BIT_EIGHT_HIGH;
1981 		*(int*)arg = tmp_int;
1982 		break;
1983 
1984 	/*  */
1985 	case BT848_SUSAT:	/* set chroma U saturation */
1986 		tmp_int = *(int*)arg;
1987 
1988 		temp = INB(bktr, BKTR_E_CONTROL);
1989 		temp1 = INB(bktr, BKTR_O_CONTROL);
1990 		if ( tmp_int & BIT_EIGHT_HIGH ) {
1991 			temp |= BT848_E_CONTROL_SAT_U_MSB;
1992 			temp1 |= BT848_O_CONTROL_SAT_U_MSB;
1993 		}
1994 		else {
1995 			temp &= ~BT848_E_CONTROL_SAT_U_MSB;
1996 			temp1 &= ~BT848_O_CONTROL_SAT_U_MSB;
1997 		}
1998 
1999 		OUTB(bktr, BKTR_SAT_U_LO, (u_char)(tmp_int & 0xff));
2000 		OUTB(bktr, BKTR_E_CONTROL, temp);
2001 		OUTB(bktr, BKTR_O_CONTROL, temp1);
2002 		break;
2003 
2004 	case BT848_GUSAT:	/* get chroma U saturation */
2005 		tmp_int = (int)INB(bktr, BKTR_SAT_U_LO) & 0xff;
2006 		if ( INB(bktr, BKTR_E_CONTROL) & BT848_E_CONTROL_SAT_U_MSB )
2007 			tmp_int |= BIT_EIGHT_HIGH;
2008 		*(int*)arg = tmp_int;
2009 		break;
2010 
2011 /* lr 970528 luma notch etc - 3 high bits of e_control/o_control */
2012 
2013 	case BT848_SLNOTCH:	/* set luma notch */
2014 		tmp_int = (*(int *)arg & 0x7) << 5 ;
2015 		OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~0xe0);
2016 		OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) & ~0xe0);
2017 		OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) | tmp_int);
2018 		OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) | tmp_int);
2019 		break;
2020 
2021 	case BT848_GLNOTCH:	/* get luma notch */
2022 		*(int *)arg = (int) ( (INB(bktr, BKTR_E_CONTROL) & 0xe0) >> 5) ;
2023 		break;
2024 
2025 
2026 	/*  */
2027 	case BT848_SCONT:	/* set contrast */
2028 		tmp_int = *(int*)arg;
2029 
2030 		temp = INB(bktr, BKTR_E_CONTROL);
2031 		temp1 = INB(bktr, BKTR_O_CONTROL);
2032 		if ( tmp_int & BIT_EIGHT_HIGH ) {
2033 			temp |= BT848_E_CONTROL_CON_MSB;
2034 			temp1 |= BT848_O_CONTROL_CON_MSB;
2035 		}
2036 		else {
2037 			temp &= ~BT848_E_CONTROL_CON_MSB;
2038 			temp1 &= ~BT848_O_CONTROL_CON_MSB;
2039 		}
2040 
2041 		OUTB(bktr, BKTR_CONTRAST_LO, (u_char)(tmp_int & 0xff));
2042 		OUTB(bktr, BKTR_E_CONTROL, temp);
2043 		OUTB(bktr, BKTR_O_CONTROL, temp1);
2044 		break;
2045 
2046 	case BT848_GCONT:	/* get contrast */
2047 		tmp_int = (int)INB(bktr, BKTR_CONTRAST_LO) & 0xff;
2048 		if ( INB(bktr, BKTR_E_CONTROL) & BT848_E_CONTROL_CON_MSB )
2049 			tmp_int |= BIT_EIGHT_HIGH;
2050 		*(int*)arg = tmp_int;
2051 		break;
2052 
2053 		/*  FIXME:  SCBARS and CCBARS require a valid int *        */
2054 		/*    argument to succeed, but its not used; consider      */
2055 		/*    using the arg to store the on/off state so           */
2056 		/*    there's only one ioctl() needed to turn cbars on/off */
2057 	case BT848_SCBARS:	/* set colorbar output */
2058 		OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) | BT848_COLOR_CTL_COLOR_BARS);
2059 		break;
2060 
2061 	case BT848_CCBARS:	/* clear colorbar output */
2062 		OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) & ~(BT848_COLOR_CTL_COLOR_BARS));
2063 		break;
2064 
2065 	case BT848_GAUDIO:	/* get audio channel */
2066 		temp = bktr->audio_mux_select;
2067 		if ( bktr->audio_mute_state == TRUE )
2068 			temp |= AUDIO_MUTE;
2069 		*(int*)arg = temp;
2070 		break;
2071 
2072 	case BT848_SBTSC:	/* set audio channel */
2073 		if ( set_BTSC( bktr, *(int*)arg ) < 0 )
2074 			return( EIO );
2075 		break;
2076 
2077 	case BT848_WEEPROM:	/* write eeprom */
2078 		offset = (((struct eeProm *)arg)->offset);
2079 		count = (((struct eeProm *)arg)->count);
2080 		buf = &(((struct eeProm *)arg)->bytes[ 0 ]);
2081 		if ( writeEEProm( bktr, offset, count, buf ) < 0 )
2082 			return( EIO );
2083 		break;
2084 
2085 	case BT848_REEPROM:	/* read eeprom */
2086 		offset = (((struct eeProm *)arg)->offset);
2087 		count = (((struct eeProm *)arg)->count);
2088 		buf = &(((struct eeProm *)arg)->bytes[ 0 ]);
2089 		if ( readEEProm( bktr, offset, count, buf ) < 0 )
2090 			return( EIO );
2091 		break;
2092 
2093 	case BT848_SIGNATURE:
2094 		offset = (((struct eeProm *)arg)->offset);
2095 		count = (((struct eeProm *)arg)->count);
2096 		buf = &(((struct eeProm *)arg)->bytes[ 0 ]);
2097 		if ( signCard( bktr, offset, count, buf ) < 0 )
2098 			return( EIO );
2099 		break;
2100 
2101         /* Ioctl's for direct gpio access */
2102 #ifdef BKTR_GPIO_ACCESS
2103         case BT848_GPIO_GET_EN:
2104                 *(int*)arg = INL(bktr, BKTR_GPIO_OUT_EN);
2105                 break;
2106 
2107         case BT848_GPIO_SET_EN:
2108                 OUTL(bktr, BKTR_GPIO_OUT_EN, *(int*)arg);
2109                 break;
2110 
2111         case BT848_GPIO_GET_DATA:
2112                 *(int*)arg = INL(bktr, BKTR_GPIO_DATA);
2113                 break;
2114 
2115         case BT848_GPIO_SET_DATA:
2116                 OUTL(bktr, BKTR_GPIO_DATA, *(int*)arg);
2117                 break;
2118 #endif /* BKTR_GPIO_ACCESS */
2119 
2120 	/* Ioctl's for running the tuner device in radio mode		*/
2121 
2122 	case RADIO_GETMODE:
2123             *(unsigned char *)arg = bktr->tuner.radio_mode;
2124 	    break;
2125 
2126 	case RADIO_SETMODE:
2127             bktr->tuner.radio_mode = *(unsigned char *)arg;
2128             break;
2129 
2130  	case RADIO_GETFREQ:
2131             *(unsigned long *)arg = bktr->tuner.frequency;
2132             break;
2133 
2134 	case RADIO_SETFREQ:
2135 	    /* The argument to this ioctl is NOT freq*16. It is
2136 	    ** freq*100.
2137 	    */
2138 
2139             temp=(int)*(unsigned long *)arg;
2140 
2141 #ifdef BKTR_RADIO_DEBUG
2142 	    kprintf("%s: arg=%d temp=%d\n", bktr_name(bktr),
2143 		   (int)*(unsigned long *)arg, temp);
2144 #endif
2145 
2146 #ifndef BKTR_RADIO_NOFREQCHECK
2147 	    /* According to the spec. sheet the band: 87.5MHz-108MHz	*/
2148 	    /* is supported.						*/
2149 	    if(temp<8750 || temp>10800) {
2150 	      kprintf("%s: Radio frequency out of range\n", bktr_name(bktr));
2151 	      return(EINVAL);
2152 	      }
2153 #endif
2154 	    temp_mute( bktr, TRUE );
2155 	    temp = tv_freq( bktr, temp, FM_RADIO_FREQUENCY );
2156 	    temp_mute( bktr, FALSE );
2157 #ifdef BKTR_RADIO_DEBUG
2158   if(temp)
2159     kprintf("%s: tv_freq returned: %d\n", bktr_name(bktr), temp);
2160 #endif
2161 	    if ( temp < 0 )
2162 		    return( EINVAL );
2163 	    *(unsigned long *)arg = temp;
2164 	    break;
2165 
2166 	/* Luigi's I2CWR ioctl */
2167 	case BT848_I2CWR:
2168 		par = *(u_long *)arg;
2169 		write = (par >> 24) & 0xff ;
2170 		i2c_addr = (par >> 16) & 0xff ;
2171 		i2c_port = (par >> 8) & 0xff ;
2172 		data = (par) & 0xff ;
2173 
2174 		if (write) {
2175 			i2cWrite( bktr, i2c_addr, i2c_port, data);
2176 		} else {
2177 			data = i2cRead( bktr, i2c_addr);
2178 		}
2179 		*(u_long *)arg = (par & 0xffffff00) | ( data & 0xff );
2180 		break;
2181 
2182 
2183 #ifdef BT848_MSP_READ
2184 	/* I2C ioctls to allow userland access to the MSP chip */
2185 	case BT848_MSP_READ:
2186 		{
2187 		struct bktr_msp_control *msp;
2188 		msp = (struct bktr_msp_control *) arg;
2189 		msp->data = msp_dpl_read(bktr, bktr->msp_addr,
2190 		                         msp->function, msp->address);
2191 		break;
2192 		}
2193 
2194 	case BT848_MSP_WRITE:
2195 		{
2196 		struct bktr_msp_control *msp;
2197 		msp = (struct bktr_msp_control *) arg;
2198 		msp_dpl_write(bktr, bktr->msp_addr, msp->function,
2199 		             msp->address, msp->data );
2200 		break;
2201 		}
2202 
2203 	case BT848_MSP_RESET:
2204 		msp_dpl_reset(bktr, bktr->msp_addr);
2205 		break;
2206 #endif
2207 
2208 	default:
2209 		return common_ioctl( bktr, cmd, arg );
2210 	}
2211 
2212 	return( 0 );
2213 }
2214 
2215 
2216 /*
2217  * common ioctls
2218  */
2219 static int
2220 common_ioctl( bktr_ptr_t bktr, ioctl_cmd_t cmd, caddr_t arg )
2221 {
2222         int                           pixfmt;
2223 	unsigned int	              temp;
2224 	struct meteor_pixfmt          *pf_pub;
2225 
2226 	switch (cmd) {
2227 
2228 	case METEORSINPUT:	/* set input device */
2229 		/*Bt848 has 3 MUX Inputs. Bt848A/849A/878/879 has 4 MUX Inputs*/
2230 		/* On the original bt848 boards, */
2231 		/*   Tuner is MUX0, RCA is MUX1, S-Video is MUX2 */
2232 		/* On the Hauppauge bt878 boards, */
2233 		/*   Tuner is MUX0, RCA is MUX3 */
2234 		/* Unfortunatly Meteor driver codes DEV_RCA as DEV_0, so we */
2235 		/* stick with this system in our Meteor Emulation */
2236 
2237 		switch(*(unsigned long *)arg & METEOR_DEV_MASK) {
2238 
2239 		/* this is the RCA video input */
2240 		case 0:		/* default */
2241 		case METEOR_INPUT_DEV0:
2242 		  /* METEOR_INPUT_DEV_RCA: */
2243 		        bktr->flags = (bktr->flags & ~METEOR_DEV_MASK)
2244 			  | METEOR_DEV0;
2245 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM)
2246 			                 & ~BT848_IFORM_MUXSEL);
2247 
2248 			/* work around for new Hauppauge 878 cards */
2249 			if ((bktr->card.card_id == CARD_HAUPPAUGE) &&
2250 				(bktr->id==BROOKTREE_878 ||
2251 				 bktr->id==BROOKTREE_879) )
2252 				OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX3);
2253 			else
2254 				OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX1);
2255 
2256 			OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_E_CONTROL_COMP);
2257 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) & ~BT848_O_CONTROL_COMP);
2258 			set_audio( bktr, AUDIO_EXTERN );
2259 			break;
2260 
2261 		/* this is the tuner input */
2262 		case METEOR_INPUT_DEV1:
2263 			bktr->flags = (bktr->flags & ~METEOR_DEV_MASK)
2264 				| METEOR_DEV1;
2265 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) & ~BT848_IFORM_MUXSEL);
2266 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX0);
2267 			OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_E_CONTROL_COMP);
2268 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) & ~BT848_O_CONTROL_COMP);
2269 			set_audio( bktr, AUDIO_TUNER );
2270 			break;
2271 
2272 		/* this is the S-VHS input, but with a composite camera */
2273 		case METEOR_INPUT_DEV2:
2274 			bktr->flags = (bktr->flags & ~METEOR_DEV_MASK)
2275 				| METEOR_DEV2;
2276 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) & ~BT848_IFORM_MUXSEL);
2277 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX2);
2278 			OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_E_CONTROL_COMP);
2279 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_O_CONTROL_COMP);
2280 			set_audio( bktr, AUDIO_EXTERN );
2281 			break;
2282 
2283 		/* this is the S-VHS input */
2284 		case METEOR_INPUT_DEV_SVIDEO:
2285 			bktr->flags = (bktr->flags & ~METEOR_DEV_MASK)
2286 				| METEOR_DEV_SVIDEO;
2287 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) & ~BT848_IFORM_MUXSEL);
2288 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX2);
2289 			OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) | BT848_E_CONTROL_COMP);
2290 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) | BT848_O_CONTROL_COMP);
2291 			set_audio( bktr, AUDIO_EXTERN );
2292 			break;
2293 
2294 		case METEOR_INPUT_DEV3:
2295 		  if ((bktr->id == BROOKTREE_848A) ||
2296 		      (bktr->id == BROOKTREE_849A) ||
2297 		      (bktr->id == BROOKTREE_878) ||
2298 		      (bktr->id == BROOKTREE_879) ) {
2299 			bktr->flags = (bktr->flags & ~METEOR_DEV_MASK)
2300 				| METEOR_DEV3;
2301 			OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) & ~BT848_IFORM_MUXSEL);
2302 
2303 			/* work around for new Hauppauge 878 cards */
2304 			if ((bktr->card.card_id == CARD_HAUPPAUGE) &&
2305 				(bktr->id==BROOKTREE_878 ||
2306 				 bktr->id==BROOKTREE_879) )
2307 				OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX1);
2308 			else
2309 				OUTB(bktr, BKTR_IFORM, INB(bktr, BKTR_IFORM) | BT848_IFORM_M_MUX3);
2310 
2311 			OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) & ~BT848_E_CONTROL_COMP);
2312 			OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) & ~BT848_O_CONTROL_COMP);
2313 			set_audio( bktr, AUDIO_EXTERN );
2314 
2315 			break;
2316 		  }
2317 
2318 		default:
2319 			return( EINVAL );
2320 		}
2321 		break;
2322 
2323 	case METEORGINPUT:	/* get input device */
2324 		*(u_long *)arg = bktr->flags & METEOR_DEV_MASK;
2325 		break;
2326 
2327 	case METEORSACTPIXFMT:
2328 		if (( *(int *)arg < 0 ) ||
2329 		    ( *(int *)arg >= PIXFMT_TABLE_SIZE ))
2330 			return( EINVAL );
2331 
2332 		bktr->pixfmt          = *(int *)arg;
2333 		OUTB(bktr, BKTR_COLOR_CTL, (INB(bktr, BKTR_COLOR_CTL) & 0xf0)
2334 		     | pixfmt_swap_flags( bktr->pixfmt ));
2335 		bktr->pixfmt_compat   = FALSE;
2336 		break;
2337 
2338 	case METEORGACTPIXFMT:
2339 		*(int *)arg = bktr->pixfmt;
2340 		break;
2341 
2342 	case METEORGSUPPIXFMT :
2343 		pf_pub = (struct meteor_pixfmt *)arg;
2344 		pixfmt = pf_pub->index;
2345 
2346 		if (( pixfmt < 0 ) || ( pixfmt >= PIXFMT_TABLE_SIZE ))
2347 			return( EINVAL );
2348 
2349 		memcpy( pf_pub, &pixfmt_table[ pixfmt ].public,
2350 			sizeof( *pf_pub ) );
2351 
2352 		/*  Patch in our format index  */
2353 		pf_pub->index       = pixfmt;
2354 		break;
2355 
2356 #if defined( STATUS_SUM )
2357 	case BT848_GSTATUS:	/* reap status */
2358 		{
2359 		crit_enter();
2360 		temp = status_sum;
2361 		status_sum = 0;
2362 		crit_exit();
2363 		*(u_int*)arg = temp;
2364 		break;
2365 		}
2366 #endif /* STATUS_SUM */
2367 
2368 	default:
2369 		return( ENOTTY );
2370 	}
2371 
2372 	return( 0 );
2373 }
2374 
2375 
2376 
2377 
2378 /******************************************************************************
2379  * bt848 RISC programming routines:
2380  */
2381 
2382 
2383 /*
2384  *
2385  */
2386 #ifdef BT848_DEBUG
2387 static int
2388 dump_bt848( bktr_ptr_t bktr )
2389 {
2390 	int	r[60]={
2391 			   4,    8, 0xc, 0x8c, 0x10, 0x90, 0x14, 0x94,
2392 			0x18, 0x98, 0x1c, 0x9c, 0x20, 0xa0, 0x24, 0xa4,
2393 			0x28, 0x2c, 0xac, 0x30, 0x34, 0x38, 0x3c, 0x40,
2394 			0xc0, 0x48, 0x4c, 0xcc, 0x50, 0xd0, 0xd4, 0x60,
2395 			0x64, 0x68, 0x6c, 0xec, 0xd8, 0xdc, 0xe0, 0xe4,
2396 			0,	 0,    0,    0
2397 		   };
2398 	int	i;
2399 
2400 	for (i = 0; i < 40; i+=4) {
2401 		kprintf("%s: Reg:value : \t%x:%x \t%x:%x \t %x:%x \t %x:%x\n",
2402 		       bktr_name(bktr),
2403 		       r[i], INL(bktr, r[i]),
2404 		       r[i+1], INL(bktr, r[i+1]),
2405 		       r[i+2], INL(bktr, r[i+2]),
2406 		       r[i+3], INL(bktr, r[i+3]));
2407 	}
2408 
2409 	kprintf("%s: INT STAT %x \n", bktr_name(bktr),
2410 	       INL(bktr, BKTR_INT_STAT));
2411 	kprintf("%s: Reg INT_MASK %x \n", bktr_name(bktr),
2412 	       INL(bktr, BKTR_INT_MASK));
2413 	kprintf("%s: Reg GPIO_DMA_CTL %x \n", bktr_name(bktr),
2414 	       INW(bktr, BKTR_GPIO_DMA_CTL));
2415 
2416 	return( 0 );
2417 }
2418 
2419 #endif
2420 
2421 /*
2422  * build write instruction
2423  */
2424 #define BKTR_FM1      0x6	/* packed data to follow */
2425 #define BKTR_FM3      0xe	/* planar data to follow */
2426 #define BKTR_VRE      0x4	/* Marks the end of the even field */
2427 #define BKTR_VRO      0xC	/* Marks the end of the odd field */
2428 #define BKTR_PXV      0x0	/* valid word (never used) */
2429 #define BKTR_EOL      0x1	/* last dword, 4 bytes */
2430 #define BKTR_SOL      0x2	/* first dword */
2431 
2432 #define OP_WRITE      (0x1 << 28)
2433 #define OP_SKIP       (0x2 << 28)
2434 #define OP_WRITEC     (0x5 << 28)
2435 #define OP_JUMP	      (0x7 << 28)
2436 #define OP_SYNC	      (0x8 << 28)
2437 #define OP_WRITE123   (0x9 << 28)
2438 #define OP_WRITES123  (0xb << 28)
2439 #define OP_SOL	      (1 << 27)		/* first instr for scanline */
2440 #define OP_EOL	      (1 << 26)
2441 
2442 #define BKTR_RESYNC   (1 << 15)
2443 #define BKTR_GEN_IRQ  (1 << 24)
2444 
2445 /*
2446  * The RISC status bits can be set/cleared in the RISC programs
2447  * and tested in the Interrupt Handler
2448  */
2449 #define BKTR_SET_RISC_STATUS_BIT0 (1 << 16)
2450 #define BKTR_SET_RISC_STATUS_BIT1 (1 << 17)
2451 #define BKTR_SET_RISC_STATUS_BIT2 (1 << 18)
2452 #define BKTR_SET_RISC_STATUS_BIT3 (1 << 19)
2453 
2454 #define BKTR_CLEAR_RISC_STATUS_BIT0 (1 << 20)
2455 #define BKTR_CLEAR_RISC_STATUS_BIT1 (1 << 21)
2456 #define BKTR_CLEAR_RISC_STATUS_BIT2 (1 << 22)
2457 #define BKTR_CLEAR_RISC_STATUS_BIT3 (1 << 23)
2458 
2459 #define BKTR_TEST_RISC_STATUS_BIT0 (1 << 28)
2460 #define BKTR_TEST_RISC_STATUS_BIT1 (1 << 29)
2461 #define BKTR_TEST_RISC_STATUS_BIT2 (1 << 30)
2462 #define BKTR_TEST_RISC_STATUS_BIT3 (1 << 31)
2463 
2464 static bool_t notclipped (bktr_reg_t * bktr, int x, int width) {
2465     int i;
2466     bktr_clip_t * clip_node;
2467     bktr->clip_start = -1;
2468     bktr->last_y = 0;
2469     bktr->y = 0;
2470     bktr->y2 = width;
2471     bktr->line_length = width;
2472     bktr->yclip = -1;
2473     bktr->yclip2 = -1;
2474     bktr->current_col = 0;
2475 
2476     if (bktr->max_clip_node == 0 ) return TRUE;
2477     clip_node = (bktr_clip_t *) &bktr->clip_list[0];
2478 
2479 
2480     for (i = 0; i < bktr->max_clip_node; i++ ) {
2481 	clip_node = (bktr_clip_t *) &bktr->clip_list[i];
2482 	if (x >= clip_node->x_min && x <= clip_node->x_max  ) {
2483 	    bktr->clip_start = i;
2484 	    return FALSE;
2485 	}
2486     }
2487 
2488     return TRUE;
2489 }
2490 
2491 static bool_t getline(bktr_reg_t *bktr, int x ) {
2492     int i, j;
2493     bktr_clip_t * clip_node ;
2494 
2495     if (bktr->line_length == 0 ||
2496 	bktr->current_col >= bktr->line_length) return FALSE;
2497 
2498     bktr->y = min(bktr->last_y, bktr->line_length);
2499     bktr->y2 = bktr->line_length;
2500 
2501     bktr->yclip = bktr->yclip2 = -1;
2502     for (i = bktr->clip_start; i < bktr->max_clip_node; i++ ) {
2503 	clip_node = (bktr_clip_t *) &bktr->clip_list[i];
2504 	if (x >= clip_node->x_min && x <= clip_node->x_max) {
2505 	    if (bktr->last_y <= clip_node->y_min) {
2506 		bktr->y =      min(bktr->last_y, bktr->line_length);
2507 		bktr->y2 =     min(clip_node->y_min, bktr->line_length);
2508 		bktr->yclip =  min(clip_node->y_min, bktr->line_length);
2509 		bktr->yclip2 = min(clip_node->y_max, bktr->line_length);
2510 		bktr->last_y = bktr->yclip2;
2511 		bktr->clip_start = i;
2512 
2513 		for (j = i+1; j  < bktr->max_clip_node; j++ ) {
2514 		    clip_node = (bktr_clip_t *) &bktr->clip_list[j];
2515 		    if (x >= clip_node->x_min && x <= clip_node->x_max) {
2516 			if (bktr->last_y >= clip_node->y_min) {
2517 			    bktr->yclip2 = min(clip_node->y_max, bktr->line_length);
2518 			    bktr->last_y = bktr->yclip2;
2519 			    bktr->clip_start = j;
2520 			}
2521 		    } else break  ;
2522 		}
2523 		return TRUE;
2524 	    }
2525 	}
2526     }
2527 
2528     if (bktr->current_col <= bktr->line_length) {
2529 	bktr->current_col = bktr->line_length;
2530 	return TRUE;
2531     }
2532     return FALSE;
2533 }
2534 
2535 static bool_t split(bktr_reg_t * bktr, volatile uint32_t **dma_prog, int width ,
2536 		    u_long operation, int pixel_width,
2537 		    volatile u_char ** target_buffer, int cols ) {
2538 
2539  u_long flag, flag2;
2540  struct meteor_pixfmt *pf = &pixfmt_table[ bktr->pixfmt ].public;
2541  u_int  skip, start_skip;
2542 
2543   /*  For RGB24, we need to align the component in FIFO Byte Lane 0         */
2544   /*    to the 1st byte in the mem dword containing our start addr.         */
2545   /*    BTW, we know this pixfmt's 1st byte is Blue; thus the start addr    */
2546   /*     must be Blue.                                                      */
2547   start_skip = 0;
2548   if (( pf->type == METEOR_PIXTYPE_RGB ) && ( pf->Bpp == 3 ))
2549 	  switch ( ((uintptr_t) (volatile void *) *target_buffer) % 4 ) {
2550 	  case 2 : start_skip = 4 ; break;
2551 	  case 1 : start_skip = 8 ; break;
2552 	  }
2553 
2554  if ((width * pixel_width) < DMA_BT848_SPLIT ) {
2555      if (  width == cols) {
2556 	 flag = OP_SOL | OP_EOL;
2557        } else if (bktr->current_col == 0 ) {
2558 	    flag  = OP_SOL;
2559        } else if (bktr->current_col == cols) {
2560 	    flag = OP_EOL;
2561        } else flag = 0;
2562 
2563      skip = 0;
2564      if (( flag & OP_SOL ) && ( start_skip > 0 )) {
2565 	     *(*dma_prog)++ = OP_SKIP | OP_SOL | start_skip;
2566 	     flag &= ~OP_SOL;
2567 	     skip = start_skip;
2568      }
2569 
2570      *(*dma_prog)++ = operation | flag  | (width * pixel_width - skip);
2571      if (operation != OP_SKIP )
2572 	 *(*dma_prog)++ = (uintptr_t) (volatile void *) *target_buffer;
2573 
2574      *target_buffer += width * pixel_width;
2575      bktr->current_col += width;
2576 
2577  } else {
2578 
2579 	if (bktr->current_col == 0 && width == cols) {
2580 	    flag = OP_SOL ;
2581 	    flag2 = OP_EOL;
2582         } else if (bktr->current_col == 0 ) {
2583 	    flag = OP_SOL;
2584 	    flag2 = 0;
2585 	} else if (bktr->current_col >= cols)  {
2586 	    flag =  0;
2587 	    flag2 = OP_EOL;
2588 	} else {
2589 	    flag =  0;
2590 	    flag2 = 0;
2591 	}
2592 
2593 	skip = 0;
2594 	if (( flag & OP_SOL ) && ( start_skip > 0 )) {
2595 		*(*dma_prog)++ = OP_SKIP | OP_SOL | start_skip;
2596 		flag &= ~OP_SOL;
2597 		skip = start_skip;
2598 	}
2599 
2600 	*(*dma_prog)++ = operation  | flag |
2601 	      (width * pixel_width / 2 - skip);
2602 	if (operation != OP_SKIP )
2603 	      *(*dma_prog)++ = (uintptr_t) (volatile void *) *target_buffer ;
2604 	*target_buffer +=  (width * pixel_width / 2) ;
2605 
2606 	if ( operation == OP_WRITE )
2607 		operation = OP_WRITEC;
2608 	*(*dma_prog)++ = operation | flag2 |
2609 	    (width * pixel_width / 2);
2610 	*target_buffer +=  (width * pixel_width / 2) ;
2611 	  bktr->current_col += width;
2612 
2613     }
2614  return TRUE;
2615 }
2616 
2617 
2618 /*
2619  * Generate the RISC instructions to capture both VBI and video images
2620  */
2621 static void
2622 rgb_vbi_prog( bktr_ptr_t bktr, char i_flag, int cols, int rows, int interlace )
2623 {
2624 	int			i;
2625 	volatile uint32_t	target_buffer, buffer, target,width;
2626 	volatile uint32_t	pitch;
2627 	volatile uint32_t	*dma_prog;	/* DMA prog is an array of
2628 						32 bit RISC instructions */
2629 	volatile uint32_t	*loop_point;
2630         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
2631 	u_int                   Bpp = pf_int->public.Bpp;
2632 	unsigned int            vbisamples;     /* VBI samples per line */
2633 	unsigned int            vbilines;       /* VBI lines per field */
2634 	unsigned int            num_dwords;     /* DWORDS per line */
2635 
2636 	vbisamples = format_params[bktr->format_params].vbi_num_samples;
2637 	vbilines   = format_params[bktr->format_params].vbi_num_lines;
2638 	num_dwords = vbisamples/4;
2639 
2640 	OUTB(bktr, BKTR_COLOR_FMT, pf_int->color_fmt);
2641 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
2642 	OUTB(bktr, BKTR_VBI_PACK_SIZE, ((num_dwords)) & 0xff);
2643 	OUTB(bktr, BKTR_VBI_PACK_DEL, ((num_dwords)>> 8) & 0x01); /* no hdelay    */
2644 							    /* no ext frame */
2645 
2646 	OUTB(bktr, BKTR_OFORM, 0x00);
2647 
2648  	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) | 0x40); /* set chroma comb */
2649  	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) | 0x40);
2650 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x80); /* clear Ycomb */
2651 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x80);
2652 
2653  	/* disable gamma correction removal */
2654  	OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) | BT848_COLOR_CTL_GAMMA);
2655 
2656 	if (cols > 385 ) {
2657 	    OUTB(bktr, BKTR_E_VTC, 0);
2658 	    OUTB(bktr, BKTR_O_VTC, 0);
2659 	} else {
2660 	    OUTB(bktr, BKTR_E_VTC, 1);
2661 	    OUTB(bktr, BKTR_O_VTC, 1);
2662 	}
2663 	bktr->capcontrol = 3 << 2 |  3;
2664 
2665 	dma_prog = (uint32_t *) bktr->dma_prog;
2666 
2667 	/* Construct Write */
2668 
2669 	if (bktr->video.addr) {
2670 		target_buffer = (u_long) bktr->video.addr;
2671 		pitch = bktr->video.width;
2672 	}
2673 	else {
2674 		target_buffer = (u_long) vtophys(bktr->bigbuf);
2675 		pitch = cols*Bpp;
2676 	}
2677 
2678 	buffer = target_buffer;
2679 
2680 	/* Wait for the VRE sync marking the end of the Even and
2681 	 * the start of the Odd field. Resync here.
2682 	 */
2683 	*dma_prog++ = OP_SYNC | BKTR_RESYNC |BKTR_VRE;
2684 	*dma_prog++ = 0;
2685 
2686 	loop_point = dma_prog;
2687 
2688 	/* store the VBI data */
2689 	/* look for sync with packed data */
2690 	*dma_prog++ = OP_SYNC | BKTR_FM1;
2691 	*dma_prog++ = 0;
2692 	for(i = 0; i < vbilines; i++) {
2693 		*dma_prog++ = OP_WRITE | OP_SOL | OP_EOL | vbisamples;
2694 		*dma_prog++ = (u_long) vtophys((caddr_t)bktr->vbidata +
2695 					(i * VBI_LINE_SIZE));
2696 	}
2697 
2698 	if ( (i_flag == 2/*Odd*/) || (i_flag==3) /*interlaced*/ ) {
2699 		/* store the Odd field video image */
2700 		/* look for sync with packed data */
2701 		*dma_prog++ = OP_SYNC  | BKTR_FM1;
2702 		*dma_prog++ = 0;  /* NULL WORD */
2703 		width = cols;
2704 		for (i = 0; i < (rows/interlace); i++) {
2705 		    target = target_buffer;
2706 		    if ( notclipped(bktr, i, width)) {
2707 			split(bktr, &dma_prog, bktr->y2 - bktr->y, OP_WRITE,
2708 			      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2709 
2710 		    } else {
2711 			while(getline(bktr, i)) {
2712 			    if (bktr->y != bktr->y2 ) {
2713 				split(bktr, &dma_prog,
2714 				      bktr->y2 - bktr->y, OP_WRITE,
2715 				      Bpp, (volatile u_char **)(uintptr_t)&target, cols);
2716 			    }
2717 			    if (bktr->yclip != bktr->yclip2 ) {
2718 				split(bktr,&dma_prog,
2719 				      bktr->yclip2 - bktr->yclip,
2720 				      OP_SKIP,
2721 				      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2722 			    }
2723 			}
2724 
2725 		    }
2726 
2727 		    target_buffer += interlace * pitch;
2728 
2729 		}
2730 
2731 	} /* end if */
2732 
2733 	/* Grab the Even field */
2734 	/* Look for the VRO, end of Odd field, marker */
2735 	*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_RESYNC | BKTR_VRO;
2736 	*dma_prog++ = 0;  /* NULL WORD */
2737 
2738 	/* store the VBI data */
2739 	/* look for sync with packed data */
2740 	*dma_prog++ = OP_SYNC | BKTR_FM1;
2741 	*dma_prog++ = 0;
2742 	for(i = 0; i < vbilines; i++) {
2743 		*dma_prog++ = OP_WRITE | OP_SOL | OP_EOL | vbisamples;
2744 		*dma_prog++ = (u_long) vtophys((caddr_t)bktr->vbidata +
2745 				((i+MAX_VBI_LINES) * VBI_LINE_SIZE));
2746 	}
2747 
2748 	/* store the video image */
2749 	if (i_flag == 1) /*Even Only*/
2750 	        target_buffer = buffer;
2751 	if (i_flag == 3) /*interlaced*/
2752 	        target_buffer = buffer+pitch;
2753 
2754 
2755 	if ((i_flag == 1) /*Even Only*/ || (i_flag==3) /*interlaced*/) {
2756 		/* look for sync with packed data */
2757 		*dma_prog++ = OP_SYNC | BKTR_FM1;
2758 		*dma_prog++ = 0;  /* NULL WORD */
2759 		width = cols;
2760 		for (i = 0; i < (rows/interlace); i++) {
2761 		    target = target_buffer;
2762 		    if ( notclipped(bktr, i, width)) {
2763 			split(bktr, &dma_prog, bktr->y2 - bktr->y, OP_WRITE,
2764 			      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2765 		    } else {
2766 			while(getline(bktr, i)) {
2767 			    if (bktr->y != bktr->y2 ) {
2768 				split(bktr, &dma_prog,
2769 				      bktr->y2 - bktr->y, OP_WRITE,
2770 				      Bpp, (volatile u_char **)(uintptr_t)&target,
2771 				      cols);
2772 			    }
2773 			    if (bktr->yclip != bktr->yclip2 ) {
2774 				split(bktr, &dma_prog,
2775 				      bktr->yclip2 - bktr->yclip, OP_SKIP,
2776 				      Bpp, (volatile u_char **)(uintptr_t) &target,  cols);
2777 			    }
2778 
2779 			}
2780 
2781 		    }
2782 
2783 		    target_buffer += interlace * pitch;
2784 
2785 		}
2786 	}
2787 
2788 	/* Look for end of 'Even Field' */
2789 	*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_RESYNC | BKTR_VRE;
2790 	*dma_prog++ = 0;  /* NULL WORD */
2791 
2792 	*dma_prog++ = OP_JUMP ;
2793 	*dma_prog++ = (u_long ) vtophys(loop_point) ;
2794 	*dma_prog++ = 0;  /* NULL WORD */
2795 
2796 }
2797 
2798 
2799 
2800 
2801 static void
2802 rgb_prog( bktr_ptr_t bktr, char i_flag, int cols, int rows, int interlace )
2803 {
2804 	int			i;
2805 	volatile uint32_t		target_buffer, buffer, target,width;
2806 	volatile uint32_t	pitch;
2807 	volatile  uint32_t	*dma_prog;
2808         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
2809 	u_int                   Bpp = pf_int->public.Bpp;
2810 
2811 	OUTB(bktr, BKTR_COLOR_FMT, pf_int->color_fmt);
2812 	OUTB(bktr, BKTR_VBI_PACK_SIZE, 0);
2813 	OUTB(bktr, BKTR_VBI_PACK_DEL, 0);
2814 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
2815 
2816 	OUTB(bktr, BKTR_OFORM, 0x00);
2817 
2818  	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) | 0x40); /* set chroma comb */
2819  	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) | 0x40);
2820 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x80); /* clear Ycomb */
2821 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x80);
2822 
2823  	/* disable gamma correction removal */
2824 	OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) | BT848_COLOR_CTL_GAMMA);
2825 
2826 	if (cols > 385 ) {
2827 	    OUTB(bktr, BKTR_E_VTC, 0);
2828 	    OUTB(bktr, BKTR_O_VTC, 0);
2829 	} else {
2830 	    OUTB(bktr, BKTR_E_VTC, 1);
2831 	    OUTB(bktr, BKTR_O_VTC, 1);
2832 	}
2833 	bktr->capcontrol = 3 << 2 |  3;
2834 
2835 	dma_prog = (uint32_t *) bktr->dma_prog;
2836 
2837 	/* Construct Write */
2838 
2839 	if (bktr->video.addr) {
2840 		target_buffer = (uint32_t) bktr->video.addr;
2841 		pitch = bktr->video.width;
2842 	}
2843 	else {
2844 		target_buffer = (uint32_t) vtophys(bktr->bigbuf);
2845 		pitch = cols*Bpp;
2846 	}
2847 
2848 	buffer = target_buffer;
2849 
2850 	/* contruct sync : for video packet format */
2851 	*dma_prog++ = OP_SYNC  | BKTR_RESYNC | BKTR_FM1;
2852 
2853 	/* sync, mode indicator packed data */
2854 	*dma_prog++ = 0;  /* NULL WORD */
2855 	width = cols;
2856 	for (i = 0; i < (rows/interlace); i++) {
2857 	    target = target_buffer;
2858 	    if ( notclipped(bktr, i, width)) {
2859 		split(bktr, &dma_prog,
2860 		      bktr->y2 - bktr->y, OP_WRITE,
2861 		      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2862 
2863 	    } else {
2864 		while(getline(bktr, i)) {
2865 		    if (bktr->y != bktr->y2 ) {
2866 			split(bktr, &dma_prog,
2867 			      bktr->y2 - bktr->y, OP_WRITE,
2868 			      Bpp, (volatile u_char **)(uintptr_t)&target, cols);
2869 		    }
2870 		    if (bktr->yclip != bktr->yclip2 ) {
2871 			split(bktr,&dma_prog,
2872 			      bktr->yclip2 - bktr->yclip,
2873 			      OP_SKIP,
2874 			      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2875 		    }
2876 		}
2877 
2878 	    }
2879 
2880 	    target_buffer += interlace * pitch;
2881 
2882 	}
2883 
2884 	switch (i_flag) {
2885 	case 1:
2886 		/* sync vre */
2887 		*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_VRO;
2888 		*dma_prog++ = 0;  /* NULL WORD */
2889 
2890 		*dma_prog++ = OP_JUMP;
2891 		*dma_prog++ = (uint32_t ) vtophys(bktr->dma_prog);
2892 		return;
2893 
2894 	case 2:
2895 		/* sync vro */
2896 		*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_VRE;
2897 		*dma_prog++ = 0;  /* NULL WORD */
2898 
2899 		*dma_prog++ = OP_JUMP;
2900 		*dma_prog++ = (uint32_t ) vtophys(bktr->dma_prog);
2901 		return;
2902 
2903 	case 3:
2904 		/* sync vro */
2905 		*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_RESYNC | BKTR_VRO;
2906 		*dma_prog++ = 0;  /* NULL WORD */
2907 		*dma_prog++ = OP_JUMP;
2908 		*dma_prog = (uint32_t ) vtophys(bktr->odd_dma_prog);
2909 		break;
2910 	}
2911 
2912 	if (interlace == 2) {
2913 
2914 	        target_buffer = buffer + pitch;
2915 
2916 		dma_prog = (uint32_t *) bktr->odd_dma_prog;
2917 
2918 		/* sync vre IRQ bit */
2919 		*dma_prog++ = OP_SYNC | BKTR_RESYNC | BKTR_FM1;
2920 		*dma_prog++ = 0;  /* NULL WORD */
2921                 width = cols;
2922 		for (i = 0; i < (rows/interlace); i++) {
2923 		    target = target_buffer;
2924 		    if ( notclipped(bktr, i, width)) {
2925 			split(bktr, &dma_prog,
2926 			      bktr->y2 - bktr->y, OP_WRITE,
2927 			      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2928 		    } else {
2929 			while(getline(bktr, i)) {
2930 			    if (bktr->y != bktr->y2 ) {
2931 				split(bktr, &dma_prog,
2932 				      bktr->y2 - bktr->y, OP_WRITE,
2933 				      Bpp, (volatile u_char **)(uintptr_t)&target,
2934 				      cols);
2935 			    }
2936 			    if (bktr->yclip != bktr->yclip2 ) {
2937 				split(bktr, &dma_prog,
2938 				      bktr->yclip2 - bktr->yclip, OP_SKIP,
2939 				      Bpp, (volatile u_char **)(uintptr_t)&target,  cols);
2940 			    }
2941 
2942 			}
2943 
2944 		    }
2945 
2946 		    target_buffer += interlace * pitch;
2947 
2948 		}
2949 	}
2950 
2951 	/* sync vre IRQ bit */
2952 	*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_RESYNC | BKTR_VRE;
2953 	*dma_prog++ = 0;  /* NULL WORD */
2954 	*dma_prog++ = OP_JUMP ;
2955 	*dma_prog++ = (uint32_t ) vtophys(bktr->dma_prog) ;
2956 	*dma_prog++ = 0;  /* NULL WORD */
2957 }
2958 
2959 
2960 /*
2961  *
2962  */
2963 static void
2964 yuvpack_prog( bktr_ptr_t bktr, char i_flag,
2965 	      int cols, int rows, int interlace )
2966 {
2967 	int			i;
2968 	volatile unsigned int	inst;
2969 	volatile unsigned int	inst3;
2970 	volatile uint32_t	target_buffer, buffer;
2971 	volatile  uint32_t	*dma_prog;
2972         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
2973 	int			b;
2974 
2975 	OUTB(bktr, BKTR_COLOR_FMT, pf_int->color_fmt);
2976 
2977 	OUTB(bktr, BKTR_E_SCLOOP, INB(bktr, BKTR_E_SCLOOP) | BT848_E_SCLOOP_CAGC); /* enable chroma comb */
2978 	OUTB(bktr, BKTR_O_SCLOOP, INB(bktr, BKTR_O_SCLOOP) | BT848_O_SCLOOP_CAGC);
2979 
2980 	OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) | BT848_COLOR_CTL_RGB_DED | BT848_COLOR_CTL_GAMMA);
2981 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
2982 
2983 	bktr->capcontrol = 3 << 2 |  3;
2984 
2985 	dma_prog = (uint32_t *) bktr->dma_prog;
2986 
2987 	/* Construct Write */
2988 
2989 	/* write , sol, eol */
2990 	inst = OP_WRITE	 | OP_SOL | (cols);
2991 	/* write , sol, eol */
2992 	inst3 = OP_WRITE | OP_EOL | (cols);
2993 
2994 	if (bktr->video.addr)
2995 		target_buffer = (uint32_t) bktr->video.addr;
2996 	else
2997 		target_buffer = (uint32_t) vtophys(bktr->bigbuf);
2998 
2999 	buffer = target_buffer;
3000 
3001 	/* contruct sync : for video packet format */
3002 	/* sync, mode indicator packed data */
3003 	*dma_prog++ = OP_SYNC | BKTR_RESYNC | BKTR_FM1;
3004 	*dma_prog++ = 0;  /* NULL WORD */
3005 
3006 	b = cols;
3007 
3008 	for (i = 0; i < (rows/interlace); i++) {
3009 		*dma_prog++ = inst;
3010 		*dma_prog++ = target_buffer;
3011 		*dma_prog++ = inst3;
3012 		*dma_prog++ = target_buffer + b;
3013 		target_buffer += interlace*(cols * 2);
3014 	}
3015 
3016 	switch (i_flag) {
3017 	case 1:
3018 		/* sync vre */
3019 		*dma_prog++ = OP_SYNC  | BKTR_GEN_IRQ | BKTR_VRE;
3020 		*dma_prog++ = 0;  /* NULL WORD */
3021 
3022 		*dma_prog++ = OP_JUMP;
3023 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3024 		return;
3025 
3026 	case 2:
3027 		/* sync vro */
3028 		*dma_prog++ = OP_SYNC  | BKTR_GEN_IRQ | BKTR_VRO;
3029 		*dma_prog++ = 0;  /* NULL WORD */
3030 		*dma_prog++ = OP_JUMP;
3031 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3032 		return;
3033 
3034 	case 3:
3035 		/* sync vro */
3036 		*dma_prog++ = OP_SYNC | BKTR_GEN_IRQ | BKTR_RESYNC | BKTR_VRO;
3037 		*dma_prog++ = 0;  /* NULL WORD */
3038 		*dma_prog++ = OP_JUMP  ;
3039 		*dma_prog = (uint32_t) vtophys(bktr->odd_dma_prog);
3040 		break;
3041 	}
3042 
3043 	if (interlace == 2) {
3044 
3045 		target_buffer =	 (uint32_t) buffer + cols*2;
3046 
3047 		dma_prog = (uint32_t *) bktr->odd_dma_prog;
3048 
3049 		/* sync vre */
3050 		*dma_prog++ = OP_SYNC | BKTR_RESYNC | BKTR_FM1;
3051 		*dma_prog++ = 0;  /* NULL WORD */
3052 
3053 		for (i = 0; i < (rows/interlace) ; i++) {
3054 			*dma_prog++ = inst;
3055 			*dma_prog++ = target_buffer;
3056 			*dma_prog++ = inst3;
3057 			*dma_prog++ = target_buffer + b;
3058 			target_buffer += interlace * ( cols*2);
3059 		}
3060 	}
3061 
3062 	/* sync vro IRQ bit */
3063 	*dma_prog++ = OP_SYNC   |  BKTR_GEN_IRQ  | BKTR_RESYNC |  BKTR_VRE;
3064 	*dma_prog++ = 0;  /* NULL WORD */
3065 	*dma_prog++ = OP_JUMP ;
3066 	*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3067 
3068 	*dma_prog++ = OP_JUMP;
3069 	*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3070 	*dma_prog++ = 0;  /* NULL WORD */
3071 }
3072 
3073 
3074 /*
3075  *
3076  */
3077 static void
3078 yuv422_prog( bktr_ptr_t bktr, char i_flag,
3079 	     int cols, int rows, int interlace ){
3080 
3081 	int			i;
3082 	volatile unsigned int	inst;
3083 	volatile uint32_t	target_buffer, t1, buffer;
3084 	volatile uint32_t	*dma_prog;
3085         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
3086 
3087 	OUTB(bktr, BKTR_COLOR_FMT, pf_int->color_fmt);
3088 
3089 	dma_prog = (uint32_t*) bktr->dma_prog;
3090 
3091 	bktr->capcontrol =   1 << 6 | 1 << 4 |	3;
3092 
3093 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
3094 	OUTB(bktr, BKTR_OFORM, 0x00);
3095 
3096 	OUTB(bktr, BKTR_E_CONTROL, INB(bktr, BKTR_E_CONTROL) | BT848_E_CONTROL_LDEC); /* disable luma decimation */
3097 	OUTB(bktr, BKTR_O_CONTROL, INB(bktr, BKTR_O_CONTROL) | BT848_O_CONTROL_LDEC);
3098 
3099 	OUTB(bktr, BKTR_E_SCLOOP, INB(bktr, BKTR_E_SCLOOP) | BT848_E_SCLOOP_CAGC);	/* chroma agc enable */
3100 	OUTB(bktr, BKTR_O_SCLOOP, INB(bktr, BKTR_O_SCLOOP) | BT848_O_SCLOOP_CAGC);
3101 
3102 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x80); /* clear Ycomb */
3103 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x80);
3104 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) | 0x40); /* set chroma comb */
3105 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) | 0x40);
3106 
3107 	/* disable gamma correction removal */
3108 	OUTB(bktr, BKTR_COLOR_CTL, INB(bktr, BKTR_COLOR_CTL) | BT848_COLOR_CTL_GAMMA);
3109 
3110 	/* Construct Write */
3111 	inst  = OP_WRITE123  | OP_SOL | OP_EOL |  (cols);
3112 	if (bktr->video.addr)
3113 		target_buffer = (uint32_t) bktr->video.addr;
3114 	else
3115 		target_buffer = (uint32_t) vtophys(bktr->bigbuf);
3116 
3117 	buffer = target_buffer;
3118 
3119 	t1 = buffer;
3120 
3121 	/* contruct sync : for video packet format */
3122 	*dma_prog++ = OP_SYNC  | 1 << 15 |	BKTR_FM3; /*sync, mode indicator packed data*/
3123 	*dma_prog++ = 0;  /* NULL WORD */
3124 
3125 	for (i = 0; i < (rows/interlace ) ; i++) {
3126 		*dma_prog++ = inst;
3127 		*dma_prog++ = cols/2 | cols/2 << 16;
3128 		*dma_prog++ = target_buffer;
3129 		*dma_prog++ = t1 + (cols*rows) + i*cols/2 * interlace;
3130 		*dma_prog++ = t1 + (cols*rows) + (cols*rows/2) + i*cols/2 * interlace;
3131 		target_buffer += interlace*cols;
3132 	}
3133 
3134 	switch (i_flag) {
3135 	case 1:
3136 		*dma_prog++ = OP_SYNC  | 1 << 24 | BKTR_VRE;  /*sync vre*/
3137 		*dma_prog++ = 0;  /* NULL WORD */
3138 
3139 		*dma_prog++ = OP_JUMP ;
3140 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3141 		return;
3142 
3143 	case 2:
3144 		*dma_prog++ = OP_SYNC  | 1 << 24 | BKTR_VRO;  /*sync vre*/
3145 		*dma_prog++ = 0;  /* NULL WORD */
3146 
3147 		*dma_prog++ = OP_JUMP;
3148 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3149 		return;
3150 
3151 	case 3:
3152 		*dma_prog++ = OP_SYNC	| 1 << 24 |  1 << 15 |   BKTR_VRO;
3153 		*dma_prog++ = 0;  /* NULL WORD */
3154 
3155 		*dma_prog++ = OP_JUMP  ;
3156 		*dma_prog = (uint32_t) vtophys(bktr->odd_dma_prog);
3157 		break;
3158 	}
3159 
3160 	if (interlace == 2) {
3161 
3162 		dma_prog = (uint32_t *) bktr->odd_dma_prog;
3163 
3164 		target_buffer  = (uint32_t) buffer + cols;
3165 		t1 = buffer + cols/2;
3166 		*dma_prog++ = OP_SYNC	|   1 << 15 | BKTR_FM3;
3167 		*dma_prog++ = 0;  /* NULL WORD */
3168 
3169 		for (i = 0; i < (rows/interlace )  ; i++) {
3170 			*dma_prog++ = inst;
3171 			*dma_prog++ = cols/2 | cols/2 << 16;
3172 			*dma_prog++ = target_buffer;
3173 			*dma_prog++ = t1 + (cols*rows) + i*cols/2 * interlace;
3174 			*dma_prog++ = t1 + (cols*rows) + (cols*rows/2) + i*cols/2 * interlace;
3175 			target_buffer += interlace*cols;
3176 		}
3177 	}
3178 
3179 	*dma_prog++ = OP_SYNC  | 1 << 24 | 1 << 15 |   BKTR_VRE;
3180 	*dma_prog++ = 0;  /* NULL WORD */
3181 	*dma_prog++ = OP_JUMP ;
3182 	*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog) ;
3183 	*dma_prog++ = 0;  /* NULL WORD */
3184 }
3185 
3186 
3187 /*
3188  *
3189  */
3190 static void
3191 yuv12_prog( bktr_ptr_t bktr, char i_flag,
3192 	     int cols, int rows, int interlace ){
3193 
3194 	int			i;
3195 	volatile unsigned int	inst;
3196 	volatile unsigned int	inst1;
3197 	volatile uint32_t	target_buffer, t1, buffer;
3198 	volatile uint32_t	*dma_prog;
3199         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
3200 
3201 	OUTB(bktr, BKTR_COLOR_FMT, pf_int->color_fmt);
3202 
3203 	dma_prog = (uint32_t *) bktr->dma_prog;
3204 
3205 	bktr->capcontrol =   1 << 6 | 1 << 4 |	3;
3206 
3207 	OUTB(bktr, BKTR_ADC, SYNC_LEVEL);
3208 	OUTB(bktr, BKTR_OFORM, 0x0);
3209 
3210 	/* Construct Write */
3211  	inst  = OP_WRITE123  | OP_SOL | OP_EOL |  (cols);
3212  	inst1  = OP_WRITES123  | OP_SOL | OP_EOL |  (cols);
3213  	if (bktr->video.addr)
3214  		target_buffer = (uint32_t) bktr->video.addr;
3215  	else
3216  		target_buffer = (uint32_t) vtophys(bktr->bigbuf);
3217 
3218 	buffer = target_buffer;
3219  	t1 = buffer;
3220 
3221  	*dma_prog++ = OP_SYNC  | 1 << 15 |	BKTR_FM3; /*sync, mode indicator packed data*/
3222  	*dma_prog++ = 0;  /* NULL WORD */
3223 
3224  	for (i = 0; i < (rows/interlace )/2 ; i++) {
3225 		*dma_prog++ = inst;
3226  		*dma_prog++ = cols/2 | (cols/2 << 16);
3227  		*dma_prog++ = target_buffer;
3228  		*dma_prog++ = t1 + (cols*rows) + i*cols/2 * interlace;
3229  		*dma_prog++ = t1 + (cols*rows) + (cols*rows/4) + i*cols/2 * interlace;
3230  		target_buffer += interlace*cols;
3231  		*dma_prog++ = inst1;
3232  		*dma_prog++ = cols/2 | (cols/2 << 16);
3233  		*dma_prog++ = target_buffer;
3234  		target_buffer += interlace*cols;
3235 
3236  	}
3237 
3238  	switch (i_flag) {
3239  	case 1:
3240  		*dma_prog++ = OP_SYNC  | 1 << 24 | BKTR_VRE;  /*sync vre*/
3241  		*dma_prog++ = 0;  /* NULL WORD */
3242 
3243 		*dma_prog++ = OP_JUMP;
3244 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3245  		return;
3246 
3247  	case 2:
3248  		*dma_prog++ = OP_SYNC  | 1 << 24 | BKTR_VRO;  /*sync vro*/
3249  		*dma_prog++ = 0;  /* NULL WORD */
3250 
3251 		*dma_prog++ = OP_JUMP;
3252 		*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3253  		return;
3254 
3255  	case 3:
3256  		*dma_prog++ = OP_SYNC |  1 << 24 | 1 << 15 | BKTR_VRO;
3257 		*dma_prog++ = 0;  /* NULL WORD */
3258 		*dma_prog++ = OP_JUMP ;
3259 		*dma_prog = (uint32_t) vtophys(bktr->odd_dma_prog);
3260 		break;
3261 	}
3262 
3263 	if (interlace == 2) {
3264 
3265 		dma_prog = (uint32_t *) bktr->odd_dma_prog;
3266 
3267 		target_buffer  = (uint32_t) buffer + cols;
3268 		t1 = buffer + cols/2;
3269 		*dma_prog++ = OP_SYNC   | 1 << 15 | BKTR_FM3;
3270 		*dma_prog++ = 0;  /* NULL WORD */
3271 
3272 		for (i = 0; i < ((rows/interlace )/2 ) ; i++) {
3273 		    *dma_prog++ = inst;
3274 		    *dma_prog++ = cols/2 | (cols/2 << 16);
3275          	    *dma_prog++ = target_buffer;
3276 		    *dma_prog++ = t1 + (cols*rows) + i*cols/2 * interlace;
3277 		    *dma_prog++ = t1 + (cols*rows) + (cols*rows/4) + i*cols/2 * interlace;
3278 		    target_buffer += interlace*cols;
3279 		    *dma_prog++ = inst1;
3280 		    *dma_prog++ = cols/2 | (cols/2 << 16);
3281 		    *dma_prog++ = target_buffer;
3282 		    target_buffer += interlace*cols;
3283 
3284 		}
3285 
3286 
3287 	}
3288 
3289 	*dma_prog++ = OP_SYNC |  1 << 24 | 1 << 15 | BKTR_VRE;
3290 	*dma_prog++ = 0;  /* NULL WORD */
3291 	*dma_prog++ = OP_JUMP;
3292 	*dma_prog++ = (uint32_t) vtophys(bktr->dma_prog);
3293 	*dma_prog++ = 0;  /* NULL WORD */
3294 }
3295 
3296 
3297 
3298 /*
3299  *
3300  */
3301 static void
3302 build_dma_prog( bktr_ptr_t bktr, char i_flag )
3303 {
3304 	int			rows, cols,  interlace;
3305 	int			tmp_int;
3306 	unsigned int		temp;
3307 	struct format_params	*fp;
3308         struct meteor_pixfmt_internal *pf_int = &pixfmt_table[ bktr->pixfmt ];
3309 
3310 
3311 	fp = &format_params[bktr->format_params];
3312 
3313 	OUTL(bktr, BKTR_INT_MASK,  ALL_INTS_DISABLED);
3314 
3315 	/* disable FIFO & RISC, leave other bits alone */
3316 	OUTW(bktr, BKTR_GPIO_DMA_CTL, INW(bktr, BKTR_GPIO_DMA_CTL) & ~FIFO_RISC_ENABLED);
3317 
3318 	/* set video parameters */
3319 	if (bktr->capture_area_enabled)
3320 	  temp = ((quad_t ) fp->htotal* (quad_t) bktr->capture_area_x_size * 4096
3321 		  / fp->scaled_htotal / bktr->cols) -  4096;
3322 	else
3323 	  temp = ((quad_t ) fp->htotal* (quad_t) fp->scaled_hactive * 4096
3324 		  / fp->scaled_htotal / bktr->cols) -  4096;
3325 
3326 	/* kprintf("%s: HSCALE value is %d\n", bktr_name(bktr), temp); */
3327 	OUTB(bktr, BKTR_E_HSCALE_LO, temp & 0xff);
3328 	OUTB(bktr, BKTR_O_HSCALE_LO, temp & 0xff);
3329 	OUTB(bktr, BKTR_E_HSCALE_HI, (temp >> 8) & 0xff);
3330 	OUTB(bktr, BKTR_O_HSCALE_HI, (temp >> 8) & 0xff);
3331 
3332 	/* horizontal active */
3333 	temp = bktr->cols;
3334 	/* kprintf("%s: HACTIVE value is %d\n", bktr_name(bktr), temp); */
3335 	OUTB(bktr, BKTR_E_HACTIVE_LO, temp & 0xff);
3336 	OUTB(bktr, BKTR_O_HACTIVE_LO, temp & 0xff);
3337 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) & ~0x3);
3338 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) & ~0x3);
3339 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) | ((temp >> 8) & 0x3));
3340 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) | ((temp >> 8) & 0x3));
3341 
3342 	/* horizontal delay */
3343 	if (bktr->capture_area_enabled)
3344 	  temp = ( (fp->hdelay* fp->scaled_hactive + bktr->capture_area_x_offset* fp->scaled_htotal)
3345 		 * bktr->cols) / (bktr->capture_area_x_size * fp->hactive);
3346 	else
3347 	  temp = (fp->hdelay * bktr->cols) / fp->hactive;
3348 
3349 	temp = temp & 0x3fe;
3350 
3351 	/* kprintf("%s: HDELAY value is %d\n", bktr_name(bktr), temp); */
3352 	OUTB(bktr, BKTR_E_DELAY_LO, temp & 0xff);
3353 	OUTB(bktr, BKTR_O_DELAY_LO, temp & 0xff);
3354 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) & ~0xc);
3355 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) & ~0xc);
3356 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) | ((temp >> 6) & 0xc));
3357 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) | ((temp >> 6) & 0xc));
3358 
3359 	/* vertical scale */
3360 
3361 	if (bktr->capture_area_enabled) {
3362 	  if (bktr->flags  & METEOR_ONLY_ODD_FIELDS ||
3363 	      bktr->flags & METEOR_ONLY_EVEN_FIELDS)
3364 	    tmp_int = 65536 -
3365 	    (((bktr->capture_area_y_size  * 256 + (bktr->rows/2)) / bktr->rows) - 512);
3366 	  else {
3367 	    tmp_int = 65536 -
3368 	    (((bktr->capture_area_y_size * 512 + (bktr->rows / 2)) /  bktr->rows) - 512);
3369 	  }
3370 	} else {
3371 	  if (bktr->flags  & METEOR_ONLY_ODD_FIELDS ||
3372 	      bktr->flags & METEOR_ONLY_EVEN_FIELDS)
3373 	    tmp_int = 65536 -
3374 	    (((fp->vactive  * 256 + (bktr->rows/2)) / bktr->rows) - 512);
3375 	  else {
3376 	    tmp_int = 65536  -
3377 	    (((fp->vactive * 512 + (bktr->rows / 2)) /  bktr->rows) - 512);
3378 	  }
3379 	}
3380 
3381 	tmp_int &= 0x1fff;
3382 	/* kprintf("%s: VSCALE value is %d\n", bktr_name(bktr), tmp_int); */
3383 	OUTB(bktr, BKTR_E_VSCALE_LO, tmp_int & 0xff);
3384 	OUTB(bktr, BKTR_O_VSCALE_LO, tmp_int & 0xff);
3385 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x1f);
3386 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x1f);
3387 	OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) | ((tmp_int >> 8) & 0x1f));
3388 	OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) | ((tmp_int >> 8) & 0x1f));
3389 
3390 
3391 	/* vertical active */
3392 	if (bktr->capture_area_enabled)
3393 	  temp = bktr->capture_area_y_size;
3394 	else
3395 	  temp = fp->vactive;
3396 	/* kprintf("%s: VACTIVE is %d\n", bktr_name(bktr), temp); */
3397 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) & ~0x30);
3398 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) | ((temp >> 4) & 0x30));
3399 	OUTB(bktr, BKTR_E_VACTIVE_LO, temp & 0xff);
3400 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) & ~0x30);
3401 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) | ((temp >> 4) & 0x30));
3402 	OUTB(bktr, BKTR_O_VACTIVE_LO, temp & 0xff);
3403 
3404 	/* vertical delay */
3405 	if (bktr->capture_area_enabled)
3406 	  temp = fp->vdelay + (bktr->capture_area_y_offset);
3407 	else
3408 	  temp = fp->vdelay;
3409 	/* kprintf("%s: VDELAY is %d\n", bktr_name(bktr), temp); */
3410 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) & ~0xC0);
3411 	OUTB(bktr, BKTR_E_CROP, INB(bktr, BKTR_E_CROP) | ((temp >> 2) & 0xC0));
3412 	OUTB(bktr, BKTR_E_VDELAY_LO, temp & 0xff);
3413 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) & ~0xC0);
3414 	OUTB(bktr, BKTR_O_CROP, INB(bktr, BKTR_O_CROP) | ((temp >> 2) & 0xC0));
3415 	OUTB(bktr, BKTR_O_VDELAY_LO, temp & 0xff);
3416 
3417 	/* end of video params */
3418 
3419 	if ((bktr->xtal_pll_mode == BT848_USE_PLL)
3420 	   && (fp->iform_xtsel==BT848_IFORM_X_XT1)) {
3421 		OUTB(bktr, BKTR_TGCTRL, BT848_TGCTRL_TGCKI_PLL); /* Select PLL mode */
3422 	} else {
3423 		OUTB(bktr, BKTR_TGCTRL, BT848_TGCTRL_TGCKI_XTAL); /* Select Normal xtal 0/xtal 1 mode */
3424 	}
3425 
3426 	/* capture control */
3427 	switch (i_flag) {
3428 	case 1:
3429 	        bktr->bktr_cap_ctl =
3430 		    (BT848_CAP_CTL_DITH_FRAME | BT848_CAP_CTL_EVEN);
3431 		OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x20);
3432 		OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x20);
3433 		interlace = 1;
3434 		break;
3435 	 case 2:
3436  	        bktr->bktr_cap_ctl =
3437 			(BT848_CAP_CTL_DITH_FRAME | BT848_CAP_CTL_ODD);
3438 		OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) & ~0x20);
3439 		OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) & ~0x20);
3440 		interlace = 1;
3441 		break;
3442 	 default:
3443  	        bktr->bktr_cap_ctl =
3444 			(BT848_CAP_CTL_DITH_FRAME |
3445 			 BT848_CAP_CTL_EVEN | BT848_CAP_CTL_ODD);
3446 		OUTB(bktr, BKTR_E_VSCALE_HI, INB(bktr, BKTR_E_VSCALE_HI) | 0x20);
3447 		OUTB(bktr, BKTR_O_VSCALE_HI, INB(bktr, BKTR_O_VSCALE_HI) | 0x20);
3448 		interlace = 2;
3449 		break;
3450 	}
3451 
3452 	OUTL(bktr, BKTR_RISC_STRT_ADD, vtophys(bktr->dma_prog));
3453 
3454 	rows = bktr->rows;
3455 	cols = bktr->cols;
3456 
3457 	bktr->vbiflags &= ~VBI_CAPTURE;	/* default - no vbi capture */
3458 
3459 	/* RGB Grabs. If /dev/vbi is already open, or we are a PAL/SECAM */
3460 	/* user, then use the rgb_vbi RISC program. */
3461 	/* Otherwise, use the normal rgb RISC program */
3462 	if (pf_int->public.type == METEOR_PIXTYPE_RGB) {
3463 		if ( (bktr->vbiflags & VBI_OPEN)
3464 		   ||(bktr->format_params == BT848_IFORM_F_PALBDGHI)
3465 		   ||(bktr->format_params == BT848_IFORM_F_SECAM)
3466                    ){
3467 			bktr->bktr_cap_ctl |=
3468 		                BT848_CAP_CTL_VBI_EVEN | BT848_CAP_CTL_VBI_ODD;
3469 			bktr->vbiflags |= VBI_CAPTURE;
3470 			rgb_vbi_prog(bktr, i_flag, cols, rows, interlace);
3471 			return;
3472 		} else {
3473 			rgb_prog(bktr, i_flag, cols, rows, interlace);
3474 			return;
3475 		}
3476 	}
3477 
3478 	if ( pf_int->public.type  == METEOR_PIXTYPE_YUV ) {
3479 		yuv422_prog(bktr, i_flag, cols, rows, interlace);
3480 		OUTB(bktr, BKTR_COLOR_CTL, (INB(bktr, BKTR_COLOR_CTL) & 0xf0)
3481 		     | pixfmt_swap_flags( bktr->pixfmt ));
3482 		return;
3483 	}
3484 
3485 	if ( pf_int->public.type  == METEOR_PIXTYPE_YUV_PACKED ) {
3486 		yuvpack_prog(bktr, i_flag, cols, rows, interlace);
3487 		OUTB(bktr, BKTR_COLOR_CTL, (INB(bktr, BKTR_COLOR_CTL) & 0xf0)
3488 		     | pixfmt_swap_flags( bktr->pixfmt ));
3489 		return;
3490 	}
3491 
3492 	if ( pf_int->public.type  == METEOR_PIXTYPE_YUV_12 ) {
3493 		yuv12_prog(bktr, i_flag, cols, rows, interlace);
3494 		OUTB(bktr, BKTR_COLOR_CTL, (INB(bktr, BKTR_COLOR_CTL) & 0xf0)
3495 		     | pixfmt_swap_flags( bktr->pixfmt ));
3496 		return;
3497 	}
3498 	return;
3499 }
3500 
3501 
3502 /******************************************************************************
3503  * video & video capture specific routines:
3504  */
3505 
3506 
3507 /*
3508  *
3509  */
3510 static void
3511 start_capture( bktr_ptr_t bktr, unsigned type )
3512 {
3513 	u_char			i_flag;
3514 	struct format_params   *fp;
3515 
3516 	fp = &format_params[bktr->format_params];
3517 
3518 	/*  If requested, clear out capture buf first  */
3519 	if (bktr->clr_on_start && (bktr->video.addr == 0)) {
3520 		bzero((caddr_t)bktr->bigbuf,
3521 		      (size_t)bktr->rows * bktr->cols * bktr->frames *
3522 			pixfmt_table[ bktr->pixfmt ].public.Bpp);
3523 	}
3524 
3525 	OUTB(bktr, BKTR_DSTATUS,  0);
3526 	OUTL(bktr, BKTR_INT_STAT, INL(bktr, BKTR_INT_STAT));
3527 
3528 	bktr->flags |= type;
3529 	bktr->flags &= ~METEOR_WANT_MASK;
3530 	switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
3531 	case METEOR_ONLY_EVEN_FIELDS:
3532 		bktr->flags |= METEOR_WANT_EVEN;
3533 		i_flag = 1;
3534 		break;
3535 	case METEOR_ONLY_ODD_FIELDS:
3536 		bktr->flags |= METEOR_WANT_ODD;
3537 		i_flag = 2;
3538 		break;
3539 	default:
3540 		bktr->flags |= METEOR_WANT_MASK;
3541 		i_flag = 3;
3542 		break;
3543 	}
3544 
3545 	/*  TDEC is only valid for continuous captures  */
3546 	if ( type == METEOR_SINGLE ) {
3547 		u_short	fps_save = bktr->fps;
3548 
3549 		set_fps(bktr, fp->frame_rate);
3550 		bktr->fps = fps_save;
3551 	}
3552 	else
3553 		set_fps(bktr, bktr->fps);
3554 
3555 	if (bktr->dma_prog_loaded == FALSE) {
3556 		build_dma_prog(bktr, i_flag);
3557 		bktr->dma_prog_loaded = TRUE;
3558 	}
3559 
3560 
3561 	OUTL(bktr, BKTR_RISC_STRT_ADD, vtophys(bktr->dma_prog));
3562 
3563 }
3564 
3565 
3566 /*
3567  *
3568  */
3569 static void
3570 set_fps( bktr_ptr_t bktr, u_short fps )
3571 {
3572 	struct format_params	*fp;
3573 	int i_flag;
3574 
3575 	fp = &format_params[bktr->format_params];
3576 
3577 	switch(bktr->flags & METEOR_ONLY_FIELDS_MASK) {
3578 	case METEOR_ONLY_EVEN_FIELDS:
3579 		bktr->flags |= METEOR_WANT_EVEN;
3580 		i_flag = 1;
3581 		break;
3582 	case METEOR_ONLY_ODD_FIELDS:
3583 		bktr->flags |= METEOR_WANT_ODD;
3584 		i_flag = 1;
3585 		break;
3586 	default:
3587 		bktr->flags |= METEOR_WANT_MASK;
3588 		i_flag = 2;
3589 		break;
3590 	}
3591 
3592 	OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
3593 	OUTL(bktr, BKTR_INT_STAT, ALL_INTS_CLEARED);
3594 
3595 	bktr->fps = fps;
3596 	OUTB(bktr, BKTR_TDEC, 0);
3597 
3598 	if (fps < fp->frame_rate)
3599 		OUTB(bktr, BKTR_TDEC, i_flag*(fp->frame_rate - fps) & 0x3f);
3600 	else
3601 		OUTB(bktr, BKTR_TDEC, 0);
3602 	return;
3603 
3604 }
3605 
3606 
3607 
3608 
3609 
3610 /*
3611  * Given a pixfmt index, compute the bt848 swap_flags necessary to
3612  *   achieve the specified swapping.
3613  * Note that without bt swapping, 2Bpp and 3Bpp modes are written
3614  *   byte-swapped, and 4Bpp modes are byte and word swapped (see Table 6
3615  *   and read R->L).
3616  * Note also that for 3Bpp, we may additionally need to do some creative
3617  *   SKIPing to align the FIFO bytelines with the target buffer (see split()).
3618  * This is abstracted here: e.g. no swaps = RGBA; byte & short swap = ABGR
3619  *   as one would expect.
3620  */
3621 
3622 static u_int pixfmt_swap_flags( int pixfmt )
3623 {
3624 	struct meteor_pixfmt *pf = &pixfmt_table[ pixfmt ].public;
3625 	u_int		      swapf = 0;
3626 
3627 	switch ( pf->Bpp ) {
3628 	case 2 : swapf = ( pf->swap_bytes ? 0 : BSWAP );
3629 		 break;
3630 
3631 	case 3 : /* no swaps supported for 3bpp - makes no sense w/ bt848 */
3632 		 break;
3633 
3634 	case 4 : if ( pf->swap_bytes )
3635 			swapf = pf->swap_shorts ? 0 : WSWAP;
3636 		 else
3637 			swapf = pf->swap_shorts ? BSWAP : (BSWAP | WSWAP);
3638 		 break;
3639 	}
3640 	return swapf;
3641 }
3642 
3643 
3644 
3645 /*
3646  * Converts meteor-defined pixel formats (e.g. METEOR_GEO_RGB16) into
3647  *   our pixfmt_table indices.
3648  */
3649 
3650 static int oformat_meteor_to_bt( u_long format )
3651 {
3652 	int    i;
3653         struct meteor_pixfmt *pf1, *pf2;
3654 
3655 	/*  Find format in compatibility table  */
3656 	for ( i = 0; i < METEOR_PIXFMT_TABLE_SIZE; i++ )
3657 		if ( meteor_pixfmt_table[i].meteor_format == format )
3658 			break;
3659 
3660 	if ( i >= METEOR_PIXFMT_TABLE_SIZE )
3661 		return -1;
3662 	pf1 = &meteor_pixfmt_table[i].public;
3663 
3664 	/*  Match it with an entry in master pixel format table  */
3665 	for ( i = 0; i < PIXFMT_TABLE_SIZE; i++ ) {
3666 		pf2 = &pixfmt_table[i].public;
3667 
3668 		if (( pf1->type        == pf2->type        ) &&
3669 		    ( pf1->Bpp         == pf2->Bpp         ) &&
3670 		    !bcmp( pf1->masks, pf2->masks, sizeof( pf1->masks )) &&
3671 		    ( pf1->swap_bytes  == pf2->swap_bytes  ) &&
3672 		    ( pf1->swap_shorts == pf2->swap_shorts ))
3673 			break;
3674 	}
3675 	if ( i >= PIXFMT_TABLE_SIZE )
3676 		return -1;
3677 
3678 	return i;
3679 }
3680 
3681 /******************************************************************************
3682  * i2c primitives:
3683  */
3684 
3685 /* */
3686 #define I2CBITTIME		(0x5<<4)	/* 5 * 0.48uS */
3687 #define I2CBITTIME_878              (1 << 7)
3688 #define I2C_READ		0x01
3689 #define I2C_COMMAND		(I2CBITTIME |			\
3690 				 BT848_DATA_CTL_I2CSCL |	\
3691 				 BT848_DATA_CTL_I2CSDA)
3692 
3693 #define I2C_COMMAND_878		(I2CBITTIME_878 |			\
3694 				 BT848_DATA_CTL_I2CSCL |	\
3695 				 BT848_DATA_CTL_I2CSDA)
3696 
3697 /*
3698  * Program the i2c bus directly
3699  */
3700 int
3701 i2cWrite( bktr_ptr_t bktr, int addr, int byte1, int byte2 )
3702 {
3703 	u_long		x;
3704 	u_long		data;
3705 
3706 	/* clear status bits */
3707 	OUTL(bktr, BKTR_INT_STAT, BT848_INT_RACK | BT848_INT_I2CDONE);
3708 
3709 	/* build the command datum */
3710 	if (bktr->id == BROOKTREE_848  ||
3711 	    bktr->id == BROOKTREE_848A ||
3712 	    bktr->id == BROOKTREE_849A) {
3713 	  data = ((addr & 0xff) << 24) | ((byte1 & 0xff) << 16) | I2C_COMMAND;
3714 	} else {
3715 	  data = ((addr & 0xff) << 24) | ((byte1 & 0xff) << 16) | I2C_COMMAND_878;
3716 	}
3717 	if ( byte2 != -1 ) {
3718 		data |= ((byte2 & 0xff) << 8);
3719 		data |= BT848_DATA_CTL_I2CW3B;
3720 	}
3721 
3722 	/* write the address and data */
3723 	OUTL(bktr, BKTR_I2C_DATA_CTL, data);
3724 
3725 	/* wait for completion */
3726 	for ( x = 0x7fffffff; x; --x ) {	/* safety valve */
3727 		if ( INL(bktr, BKTR_INT_STAT) & BT848_INT_I2CDONE )
3728 			break;
3729 	}
3730 
3731 	/* check for ACK */
3732 	if ( !x || !(INL(bktr, BKTR_INT_STAT) & BT848_INT_RACK) )
3733 		return( -1 );
3734 
3735 	/* return OK */
3736 	return( 0 );
3737 }
3738 
3739 
3740 /*
3741  *
3742  */
3743 int
3744 i2cRead( bktr_ptr_t bktr, int addr )
3745 {
3746 	u_long		x;
3747 
3748 	/* clear status bits */
3749 	OUTL(bktr, BKTR_INT_STAT, BT848_INT_RACK | BT848_INT_I2CDONE);
3750 
3751 	/* write the READ address */
3752 	/* The Bt878 and Bt879  differed on the treatment of i2c commands */
3753 
3754 	if (bktr->id == BROOKTREE_848  ||
3755 	    bktr->id == BROOKTREE_848A ||
3756 	    bktr->id == BROOKTREE_849A) {
3757 		OUTL(bktr, BKTR_I2C_DATA_CTL, ((addr & 0xff) << 24) | I2C_COMMAND);
3758 	} else {
3759 		OUTL(bktr, BKTR_I2C_DATA_CTL, ((addr & 0xff) << 24) | I2C_COMMAND_878);
3760 	}
3761 
3762 	/* wait for completion */
3763 	for ( x = 0x7fffffff; x; --x ) {	/* safety valve */
3764 		if ( INL(bktr, BKTR_INT_STAT) & BT848_INT_I2CDONE )
3765 			break;
3766 	}
3767 
3768 	/* check for ACK */
3769 	if ( !x || !(INL(bktr, BKTR_INT_STAT) & BT848_INT_RACK) )
3770 		return( -1 );
3771 
3772 	/* it was a read */
3773 	return( (INL(bktr, BKTR_I2C_DATA_CTL) >> 8) & 0xff );
3774 }
3775 
3776 /* The MSP34xx Audio chip require i2c bus writes of up to 5 bytes which the */
3777 /* bt848 automated i2c bus controller cannot handle */
3778 /* Therefore we need low level control of the i2c bus hardware */
3779 /* Idea for the following functions are from elsewhere in this driver and */
3780 /* from the Linux BTTV i2c driver by Gerd Knorr <kraxel@cs.tu-berlin.de> */
3781 
3782 #define BITD    40
3783 static void i2c_start( bktr_ptr_t bktr) {
3784         OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD ); /* release data */
3785         OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD ); /* release clock */
3786         OUTL(bktr, BKTR_I2C_DATA_CTL, 2); DELAY( BITD ); /* lower data */
3787         OUTL(bktr, BKTR_I2C_DATA_CTL, 0); DELAY( BITD ); /* lower clock */
3788 }
3789 
3790 static void i2c_stop( bktr_ptr_t bktr) {
3791         OUTL(bktr, BKTR_I2C_DATA_CTL, 0); DELAY( BITD ); /* lower clock & data */
3792         OUTL(bktr, BKTR_I2C_DATA_CTL, 2); DELAY( BITD ); /* release clock */
3793         OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD ); /* release data */
3794 }
3795 
3796 static int i2c_write_byte( bktr_ptr_t bktr, unsigned char data) {
3797         int x;
3798         int status;
3799 
3800         /* write out the byte */
3801         for ( x = 7; x >= 0; --x ) {
3802                 if ( data & (1<<x) ) {
3803 			OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3804                         DELAY( BITD );          /* assert HI data */
3805 			OUTL(bktr, BKTR_I2C_DATA_CTL, 3);
3806                         DELAY( BITD );          /* strobe clock */
3807 			OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3808                         DELAY( BITD );          /* release clock */
3809                 }
3810                 else {
3811 			OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3812                         DELAY( BITD );          /* assert LO data */
3813 			OUTL(bktr, BKTR_I2C_DATA_CTL, 2);
3814                         DELAY( BITD );          /* strobe clock */
3815 			OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3816                         DELAY( BITD );          /* release clock */
3817                 }
3818         }
3819 
3820         /* look for an ACK */
3821 	OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD ); /* float data */
3822 	OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD ); /* strobe clock */
3823         status = INL(bktr, BKTR_I2C_DATA_CTL) & 1;       /* read the ACK bit */
3824         OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD ); /* release clock */
3825 
3826         return( status );
3827 }
3828 
3829 static int i2c_read_byte( bktr_ptr_t bktr, unsigned char *data, int last ) {
3830         int x;
3831         int bit;
3832         int byte = 0;
3833 
3834         /* read in the byte */
3835 	OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3836         DELAY( BITD );                          /* float data */
3837         for ( x = 7; x >= 0; --x ) {
3838 		OUTL(bktr, BKTR_I2C_DATA_CTL, 3);
3839                 DELAY( BITD );                  /* strobe clock */
3840                 bit = INL(bktr, BKTR_I2C_DATA_CTL) & 1;  /* read the data bit */
3841                 if ( bit ) byte |= (1<<x);
3842 		OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3843                 DELAY( BITD );                  /* release clock */
3844         }
3845         /* After reading the byte, send an ACK */
3846         /* (unless that was the last byte, for which we send a NAK */
3847         if (last) { /* send NAK - same a writing a 1 */
3848 		OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3849                 DELAY( BITD );                  /* set data bit */
3850 		OUTL(bktr, BKTR_I2C_DATA_CTL, 3);
3851                 DELAY( BITD );                  /* strobe clock */
3852 		OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3853                 DELAY( BITD );                  /* release clock */
3854         } else { /* send ACK - same as writing a 0 */
3855 		OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3856                 DELAY( BITD );                  /* set data bit */
3857 		OUTL(bktr, BKTR_I2C_DATA_CTL, 2);
3858                 DELAY( BITD );                  /* strobe clock */
3859 		OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3860                 DELAY( BITD );                  /* release clock */
3861         }
3862 
3863         *data=byte;
3864 	return 0;
3865 }
3866 #undef BITD
3867 
3868 /* Write to the MSP or DPL registers */
3869 void msp_dpl_write( bktr_ptr_t bktr, int i2c_addr, unsigned char dev, unsigned int addr,
3870 		    unsigned int data){
3871 	unsigned int msp_w_addr = i2c_addr;
3872 	unsigned char addr_l, addr_h, data_h, data_l ;
3873 	addr_h = (addr >>8) & 0xff;
3874 	addr_l = addr & 0xff;
3875 	data_h = (data >>8) & 0xff;
3876 	data_l = data & 0xff;
3877 
3878 	i2c_start(bktr);
3879 	i2c_write_byte(bktr, msp_w_addr);
3880 	i2c_write_byte(bktr, dev);
3881 	i2c_write_byte(bktr, addr_h);
3882 	i2c_write_byte(bktr, addr_l);
3883 	i2c_write_byte(bktr, data_h);
3884 	i2c_write_byte(bktr, data_l);
3885 	i2c_stop(bktr);
3886 }
3887 
3888 /* Read from the MSP or DPL registers */
3889 unsigned int msp_dpl_read(bktr_ptr_t bktr, int i2c_addr, unsigned char dev, unsigned int addr){
3890 	unsigned int data;
3891 	unsigned char addr_l, addr_h, data_1, data_2, dev_r ;
3892 	addr_h = (addr >>8) & 0xff;
3893 	addr_l = addr & 0xff;
3894 	dev_r = dev+1;
3895 
3896 	i2c_start(bktr);
3897 	i2c_write_byte(bktr,i2c_addr);
3898 	i2c_write_byte(bktr,dev_r);
3899 	i2c_write_byte(bktr,addr_h);
3900 	i2c_write_byte(bktr,addr_l);
3901 
3902 	i2c_start(bktr);
3903 	i2c_write_byte(bktr,i2c_addr+1);
3904 	i2c_read_byte(bktr,&data_1, 0);
3905 	i2c_read_byte(bktr,&data_2, 1);
3906 	i2c_stop(bktr);
3907 	data = (data_1<<8) | data_2;
3908 	return data;
3909 }
3910 
3911 /* Reset the MSP or DPL chip */
3912 /* The user can block the reset (which is handy if you initialise the
3913  * MSP audio in another operating system first (eg in Windows)
3914  */
3915 void msp_dpl_reset( bktr_ptr_t bktr, int i2c_addr ) {
3916 
3917 #ifndef BKTR_NO_MSP_RESET
3918 	/* put into reset mode */
3919 	i2c_start(bktr);
3920 	i2c_write_byte(bktr, i2c_addr);
3921 	i2c_write_byte(bktr, 0x00);
3922 	i2c_write_byte(bktr, 0x80);
3923 	i2c_write_byte(bktr, 0x00);
3924 	i2c_stop(bktr);
3925 
3926 	/* put back to operational mode */
3927 	i2c_start(bktr);
3928 	i2c_write_byte(bktr, i2c_addr);
3929 	i2c_write_byte(bktr, 0x00);
3930 	i2c_write_byte(bktr, 0x00);
3931 	i2c_write_byte(bktr, 0x00);
3932 	i2c_stop(bktr);
3933 #endif
3934 	return;
3935 
3936 }
3937 
3938 static void remote_read(bktr_ptr_t bktr, struct bktr_remote *remote) {
3939 
3940 	/* XXX errors ignored */
3941 	i2c_start(bktr);
3942 	i2c_write_byte(bktr,bktr->remote_control_addr);
3943 	i2c_read_byte(bktr,&(remote->data[0]), 0);
3944 	i2c_read_byte(bktr,&(remote->data[1]), 0);
3945 	i2c_read_byte(bktr,&(remote->data[2]), 0);
3946 	i2c_stop(bktr);
3947 
3948 	return;
3949 }
3950 
3951 
3952 #if defined( I2C_SOFTWARE_PROBE )
3953 
3954 /*
3955  * we are keeping this around for any parts that we need to probe
3956  * but that CANNOT be probed via an i2c read.
3957  * this is necessary because the hardware i2c mechanism
3958  * cannot be programmed for 1 byte writes.
3959  * currently there are no known i2c parts that we need to probe
3960  * and that cannot be safely read.
3961  */
3962 static int	i2cProbe( bktr_ptr_t bktr, int addr );
3963 #define BITD		40
3964 #define EXTRA_START
3965 
3966 /*
3967  * probe for an I2C device at addr.
3968  */
3969 static int
3970 i2cProbe( bktr_ptr_t bktr, int addr )
3971 {
3972 	int		x, status;
3973 
3974 	/* the START */
3975 #if defined( EXTRA_START )
3976 	OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD );	/* release data */
3977 	OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD );	/* release clock */
3978 #endif /* EXTRA_START */
3979 	OUTL(bktr, BKTR_I2C_DATA_CTL, 2); DELAY( BITD );	/* lower data */
3980 	OUTL(bktr, BKTR_I2C_DATA_CTL, 0); DELAY( BITD );	/* lower clock */
3981 
3982 	/* write addr */
3983 	for ( x = 7; x >= 0; --x ) {
3984 		if ( addr & (1<<x) ) {
3985 			OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3986 			DELAY( BITD );		/* assert HI data */
3987 			OUTL(bktr, BKTR_I2C_DATA_CTL, 3);
3988 			DELAY( BITD );		/* strobe clock */
3989 			OUTL(bktr, BKTR_I2C_DATA_CTL, 1);
3990 			DELAY( BITD );		/* release clock */
3991 		}
3992 		else {
3993 			OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3994 			DELAY( BITD );		/* assert LO data */
3995 			OUTL(bktr, BKTR_I2C_DATA_CTL, 2);
3996 			DELAY( BITD );		/* strobe clock */
3997 			OUTL(bktr, BKTR_I2C_DATA_CTL, 0);
3998 			DELAY( BITD );		/* release clock */
3999 		}
4000 	}
4001 
4002 	/* look for an ACK */
4003 	OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD );	/* float data */
4004 	OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD );	/* strobe clock */
4005 	status = INL(bktr, BKTR_I2C_DATA_CTL) & 1;	/* read the ACK bit */
4006 	OUTL(bktr, BKTR_I2C_DATA_CTL, 1); DELAY( BITD );	/* release clock */
4007 
4008 	/* the STOP */
4009 	OUTL(bktr, BKTR_I2C_DATA_CTL, 0); DELAY( BITD );	/* lower clock & data */
4010 	OUTL(bktr, BKTR_I2C_DATA_CTL, 2); DELAY( BITD );	/* release clock */
4011 	OUTL(bktr, BKTR_I2C_DATA_CTL, 3); DELAY( BITD );	/* release data */
4012 
4013 	return( status );
4014 }
4015 #undef EXTRA_START
4016 #undef BITD
4017 
4018 #endif /* I2C_SOFTWARE_PROBE */
4019 
4020 
4021 #define ABSENT		(-1)
4022