1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
7 *
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 *   Ronald Bultje    <rbultje@ronald.bitfreak.net>
12 *   Laurent Pinchart <laurent.pinchart@skynet.be>
13 *   Mailinglist      <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/delay.h>
31
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/vmalloc.h>
37
38#include <linux/proc_fs.h>
39#include <linux/i2c.h>
40#include <linux/i2c-algo-bit.h>
41#include <linux/videodev.h>
42#include <media/v4l2-common.h>
43#include <linux/spinlock.h>
44#include <linux/sem.h>
45#include <linux/kmod.h>
46#include <linux/wait.h>
47
48#include <linux/pci.h>
49#include <linux/interrupt.h>
50#include <linux/video_decoder.h>
51#include <linux/video_encoder.h>
52#include <linux/mutex.h>
53
54#include <asm/io.h>
55
56#include "videocodec.h"
57#include "zoran.h"
58#include "zoran_card.h"
59#include "zoran_device.h"
60#include "zoran_procfs.h"
61
62#define I2C_NAME(x) (x)->name
63
64extern const struct zoran_format zoran_formats[];
65
66static int card[BUZ_MAX] = { -1, -1, -1, -1 };
67module_param_array(card, int, NULL, 0);
68MODULE_PARM_DESC(card, "The type of card");
69
70static int encoder[BUZ_MAX] = { -1, -1, -1, -1 };
71module_param_array(encoder, int, NULL, 0);
72MODULE_PARM_DESC(encoder, "i2c TV encoder");
73
74static int decoder[BUZ_MAX] = { -1, -1, -1, -1 };
75module_param_array(decoder, int, NULL, 0);
76MODULE_PARM_DESC(decoder, "i2c TV decoder");
77
78/*
79   The video mem address of the video card.
80   The driver has a little database for some videocards
81   to determine it from there. If your video card is not in there
82   you have either to give it to the driver as a parameter
83   or set in in a VIDIOCSFBUF ioctl
84 */
85
86static unsigned long vidmem = 0;	/* Video memory base address */
87module_param(vidmem, ulong, 0);
88
89/*
90   Default input and video norm at startup of the driver.
91*/
92
93static int default_input = 0;	/* 0=Composite, 1=S-Video */
94module_param(default_input, int, 0);
95MODULE_PARM_DESC(default_input,
96		 "Default input (0=Composite, 1=S-Video, 2=Internal)");
97
98static int default_mux = 1;	/* 6 Eyes input selection */
99module_param(default_mux, int, 0);
100MODULE_PARM_DESC(default_mux,
101		 "Default 6 Eyes mux setting (Input selection)");
102
103static int default_norm = 0;	/* 0=PAL, 1=NTSC 2=SECAM */
104module_param(default_norm, int, 0);
105MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
106
107static int video_nr = -1;	/* /dev/videoN, -1 for autodetect */
108module_param(video_nr, int, 0);
109MODULE_PARM_DESC(video_nr, "video device number");
110
111/*
112   Number and size of grab buffers for Video 4 Linux
113   The vast majority of applications should not need more than 2,
114   the very popular BTTV driver actually does ONLY have 2.
115   Time sensitive applications might need more, the maximum
116   is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
117
118   The size is set so that the maximum possible request
119   can be satisfied. Decrease  it, if bigphys_area alloc'd
120   memory is low. If you don't have the bigphys_area patch,
121   set it to 128 KB. Will you allow only to grab small
122   images with V4L, but that's better than nothing.
123
124   v4l_bufsize has to be given in KB !
125
126*/
127
128int v4l_nbufs = 2;
129int v4l_bufsize = 128;		/* Everybody should be able to work with this setting */
130module_param(v4l_nbufs, int, 0);
131MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
132module_param(v4l_bufsize, int, 0);
133MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
134
135int jpg_nbufs = 32;
136int jpg_bufsize = 512;		/* max size for 100% quality full-PAL frame */
137module_param(jpg_nbufs, int, 0);
138MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
139module_param(jpg_bufsize, int, 0);
140MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
141
142int pass_through = 0;		/* 1=Pass through TV signal when device is not used */
143				/* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
144module_param(pass_through, int, 0);
145MODULE_PARM_DESC(pass_through,
146		 "Pass TV signal through to TV-out when idling");
147
148static int debug = 1;
149int *zr_debug = &debug;
150module_param(debug, int, 0);
151MODULE_PARM_DESC(debug, "Debug level (0-4)");
152
153MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
154MODULE_AUTHOR("Serguei Miridonov");
155MODULE_LICENSE("GPL");
156
157static struct pci_device_id zr36067_pci_tbl[] = {
158	{PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057,
159	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
160	{0}
161};
162MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
163
164#define dprintk(num, format, args...) \
165	do { \
166		if (*zr_debug >= num) \
167			printk(format, ##args); \
168	} while (0)
169
170int zoran_num;			/* number of Buzs in use */
171struct zoran zoran[BUZ_MAX];
172
173/* videocodec bus functions ZR36060 */
174static u32
175zr36060_read (struct videocodec *codec,
176	      u16                reg)
177{
178	struct zoran *zr = (struct zoran *) codec->master_data->data;
179	__u32 data;
180
181	if (post_office_wait(zr)
182	    || post_office_write(zr, 0, 1, reg >> 8)
183	    || post_office_write(zr, 0, 2, reg & 0xff)) {
184		return -1;
185	}
186
187	data = post_office_read(zr, 0, 3) & 0xff;
188	return data;
189}
190
191static void
192zr36060_write (struct videocodec *codec,
193	       u16                reg,
194	       u32                val)
195{
196	struct zoran *zr = (struct zoran *) codec->master_data->data;
197
198	if (post_office_wait(zr)
199	    || post_office_write(zr, 0, 1, reg >> 8)
200	    || post_office_write(zr, 0, 2, reg & 0xff)) {
201		return;
202	}
203
204	post_office_write(zr, 0, 3, val & 0xff);
205}
206
207/* videocodec bus functions ZR36050 */
208static u32
209zr36050_read (struct videocodec *codec,
210	      u16                reg)
211{
212	struct zoran *zr = (struct zoran *) codec->master_data->data;
213	__u32 data;
214
215	if (post_office_wait(zr)
216	    || post_office_write(zr, 1, 0, reg >> 2)) {	// reg. HIGHBYTES
217		return -1;
218	}
219
220	data = post_office_read(zr, 0, reg & 0x03) & 0xff;	// reg. LOWBYTES + read
221	return data;
222}
223
224static void
225zr36050_write (struct videocodec *codec,
226	       u16                reg,
227	       u32                val)
228{
229	struct zoran *zr = (struct zoran *) codec->master_data->data;
230
231	if (post_office_wait(zr)
232	    || post_office_write(zr, 1, 0, reg >> 2)) {	// reg. HIGHBYTES
233		return;
234	}
235
236	post_office_write(zr, 0, reg & 0x03, val & 0xff);	// reg. LOWBYTES + wr. data
237}
238
239/* videocodec bus functions ZR36016 */
240static u32
241zr36016_read (struct videocodec *codec,
242	      u16                reg)
243{
244	struct zoran *zr = (struct zoran *) codec->master_data->data;
245	__u32 data;
246
247	if (post_office_wait(zr)) {
248		return -1;
249	}
250
251	data = post_office_read(zr, 2, reg & 0x03) & 0xff;	// read
252	return data;
253}
254
255/* hack for in zoran_device.c */
256void
257zr36016_write (struct videocodec *codec,
258	       u16                reg,
259	       u32                val)
260{
261	struct zoran *zr = (struct zoran *) codec->master_data->data;
262
263	if (post_office_wait(zr)) {
264		return;
265	}
266
267	post_office_write(zr, 2, reg & 0x03, val & 0x0ff);	// wr. data
268}
269
270/*
271 * Board specific information
272 */
273
274static void
275dc10_init (struct zoran *zr)
276{
277	dprintk(3, KERN_DEBUG "%s: dc10_init()\n", ZR_DEVNAME(zr));
278
279	/* Pixel clock selection */
280	GPIO(zr, 4, 0);
281	GPIO(zr, 5, 1);
282	/* Enable the video bus sync signals */
283	GPIO(zr, 7, 0);
284}
285
286static void
287dc10plus_init (struct zoran *zr)
288{
289	dprintk(3, KERN_DEBUG "%s: dc10plus_init()\n", ZR_DEVNAME(zr));
290}
291
292static void
293buz_init (struct zoran *zr)
294{
295	dprintk(3, KERN_DEBUG "%s: buz_init()\n", ZR_DEVNAME(zr));
296
297	/* some stuff from Iomega */
298	pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
299	pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
300	pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
301}
302
303static void
304lml33_init (struct zoran *zr)
305{
306	dprintk(3, KERN_DEBUG "%s: lml33_init()\n", ZR_DEVNAME(zr));
307
308	GPIO(zr, 2, 1);		// Set Composite input/output
309}
310
311static void
312avs6eyes_init (struct zoran *zr)
313{
314	// AverMedia 6-Eyes original driver by Christer Weinigel
315
316	// Lifted straight from Christer's old driver and
317	// modified slightly by Martin Samuelsson.
318
319	int mux = default_mux; /* 1 = BT866, 7 = VID1 */
320
321	GPIO(zr, 4, 1); /* Bt866 SLEEP on */
322	udelay(2);
323
324	GPIO(zr, 0, 1); /* ZR36060 /RESET on */
325	GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
326	GPIO(zr, 2, mux & 1);   /* MUX S0 */
327	GPIO(zr, 3, 0); /* /FRAME on */
328	GPIO(zr, 4, 0); /* Bt866 SLEEP off */
329	GPIO(zr, 5, mux & 2);   /* MUX S1 */
330	GPIO(zr, 6, 0); /* ? */
331	GPIO(zr, 7, mux & 4);   /* MUX S2 */
332
333}
334
335static char *
336i2cid_to_modulename (u16 i2c_id)
337{
338	char *name = NULL;
339
340	switch (i2c_id) {
341	case I2C_DRIVERID_SAA7110:
342		name = "saa7110";
343		break;
344	case I2C_DRIVERID_SAA7111A:
345		name = "saa7111";
346		break;
347	case I2C_DRIVERID_SAA7114:
348		name = "saa7114";
349		break;
350	case I2C_DRIVERID_SAA7185B:
351		name = "saa7185";
352		break;
353	case I2C_DRIVERID_ADV7170:
354		name = "adv7170";
355		break;
356	case I2C_DRIVERID_ADV7175:
357		name = "adv7175";
358		break;
359	case I2C_DRIVERID_BT819:
360		name = "bt819";
361		break;
362	case I2C_DRIVERID_BT856:
363		name = "bt856";
364		break;
365	case I2C_DRIVERID_VPX3220:
366		name = "vpx3220";
367		break;
368/*	case I2C_DRIVERID_VPX3224:
369		name = "vpx3224";
370		break;
371	case I2C_DRIVERID_MSE3000:
372		name = "mse3000";
373		break;*/
374	default:
375		break;
376	}
377
378	return name;
379}
380
381static char *
382codecid_to_modulename (u16 codecid)
383{
384	char *name = NULL;
385
386	switch (codecid) {
387	case CODEC_TYPE_ZR36060:
388		name = "zr36060";
389		break;
390	case CODEC_TYPE_ZR36050:
391		name = "zr36050";
392		break;
393	case CODEC_TYPE_ZR36016:
394		name = "zr36016";
395		break;
396	default:
397		break;
398	}
399
400	return name;
401}
402
403// struct tvnorm {
404//      u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
405// };
406
407static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
408static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
409static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
410static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
411
412static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
413static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
414
415/* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
416static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
417static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
418
419static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
420static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
421
422static struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
423static struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
424
425static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
426	{
427		.type = DC10_old,
428		.name = "DC10(old)",
429		.i2c_decoder = I2C_DRIVERID_VPX3220,
430		/*.i2c_encoder = I2C_DRIVERID_MSE3000,*/
431		.video_codec = CODEC_TYPE_ZR36050,
432		.video_vfe = CODEC_TYPE_ZR36016,
433
434		.inputs = 3,
435		.input = {
436			{ 1, "Composite" },
437			{ 2, "S-Video" },
438			{ 0, "Internal/comp" }
439		},
440		.norms = 3,
441		.tvn = {
442			&f50sqpixel_dc10,
443			&f60sqpixel_dc10,
444			&f50sqpixel_dc10
445		},
446		.jpeg_int = 0,
447		.vsync_int = ZR36057_ISR_GIRQ1,
448		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
449		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
450		.gpcs = { -1, 0 },
451		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
452		.gws_not_connected = 0,
453		.input_mux = 0,
454		.init = &dc10_init,
455	}, {
456		.type = DC10_new,
457		.name = "DC10(new)",
458		.i2c_decoder = I2C_DRIVERID_SAA7110,
459		.i2c_encoder = I2C_DRIVERID_ADV7175,
460		.video_codec = CODEC_TYPE_ZR36060,
461
462		.inputs = 3,
463		.input = {
464				{ 0, "Composite" },
465				{ 7, "S-Video" },
466				{ 5, "Internal/comp" }
467			},
468		.norms = 3,
469		.tvn = {
470				&f50sqpixel,
471				&f60sqpixel,
472				&f50sqpixel},
473		.jpeg_int = ZR36057_ISR_GIRQ0,
474		.vsync_int = ZR36057_ISR_GIRQ1,
475		.gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
476		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
477		.gpcs = { -1, 1},
478		.vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
479		.gws_not_connected = 0,
480		.input_mux = 0,
481		.init = &dc10plus_init,
482	}, {
483		.type = DC10plus,
484		.name = "DC10plus",
485		.vendor_id = PCI_VENDOR_ID_MIRO,
486		.device_id = PCI_DEVICE_ID_MIRO_DC10PLUS,
487		.i2c_decoder = I2C_DRIVERID_SAA7110,
488		.i2c_encoder = I2C_DRIVERID_ADV7175,
489		.video_codec = CODEC_TYPE_ZR36060,
490
491		.inputs = 3,
492		.input = {
493			{ 0, "Composite" },
494			{ 7, "S-Video" },
495			{ 5, "Internal/comp" }
496		},
497		.norms = 3,
498		.tvn = {
499			&f50sqpixel,
500			&f60sqpixel,
501			&f50sqpixel
502		},
503		.jpeg_int = ZR36057_ISR_GIRQ0,
504		.vsync_int = ZR36057_ISR_GIRQ1,
505		.gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
506		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
507		.gpcs = { -1, 1 },
508		.vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
509		.gws_not_connected = 0,
510		.input_mux = 0,
511		.init = &dc10plus_init,
512	}, {
513		.type = DC30,
514		.name = "DC30",
515		.i2c_decoder = I2C_DRIVERID_VPX3220,
516		.i2c_encoder = I2C_DRIVERID_ADV7175,
517		.video_codec = CODEC_TYPE_ZR36050,
518		.video_vfe = CODEC_TYPE_ZR36016,
519
520		.inputs = 3,
521		.input = {
522			{ 1, "Composite" },
523			{ 2, "S-Video" },
524			{ 0, "Internal/comp" }
525		},
526		.norms = 3,
527		.tvn = {
528			&f50sqpixel_dc10,
529			&f60sqpixel_dc10,
530			&f50sqpixel_dc10
531		},
532		.jpeg_int = 0,
533		.vsync_int = ZR36057_ISR_GIRQ1,
534		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
535		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
536		.gpcs = { -1, 0 },
537		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
538		.gws_not_connected = 0,
539		.input_mux = 0,
540		.init = &dc10_init,
541	}, {
542		.type = DC30plus,
543		.name = "DC30plus",
544		.vendor_id = PCI_VENDOR_ID_MIRO,
545		.device_id = PCI_DEVICE_ID_MIRO_DC30PLUS,
546		.i2c_decoder = I2C_DRIVERID_VPX3220,
547		.i2c_encoder = I2C_DRIVERID_ADV7175,
548		.video_codec = CODEC_TYPE_ZR36050,
549		.video_vfe = CODEC_TYPE_ZR36016,
550
551		.inputs = 3,
552		.input = {
553			{ 1, "Composite" },
554			{ 2, "S-Video" },
555			{ 0, "Internal/comp" }
556		},
557		.norms = 3,
558		.tvn = {
559			&f50sqpixel_dc10,
560			&f60sqpixel_dc10,
561			&f50sqpixel_dc10
562		},
563		.jpeg_int = 0,
564		.vsync_int = ZR36057_ISR_GIRQ1,
565		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
566		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
567		.gpcs = { -1, 0 },
568		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
569		.gws_not_connected = 0,
570		.input_mux = 0,
571		.init = &dc10_init,
572	}, {
573		.type = LML33,
574		.name = "LML33",
575		.i2c_decoder = I2C_DRIVERID_BT819,
576		.i2c_encoder = I2C_DRIVERID_BT856,
577		.video_codec = CODEC_TYPE_ZR36060,
578
579		.inputs = 2,
580		.input = {
581			{ 0, "Composite" },
582			{ 7, "S-Video" }
583		},
584		.norms = 2,
585		.tvn = {
586			&f50ccir601_lml33,
587			&f60ccir601_lml33,
588			NULL
589		},
590		.jpeg_int = ZR36057_ISR_GIRQ1,
591		.vsync_int = ZR36057_ISR_GIRQ0,
592		.gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
593		.gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
594		.gpcs = { 3, 1 },
595		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
596		.gws_not_connected = 1,
597		.input_mux = 0,
598		.init = &lml33_init,
599	}, {
600		.type = LML33R10,
601		.name = "LML33R10",
602		.vendor_id = PCI_VENDOR_ID_ELECTRONICDESIGNGMBH,
603		.device_id = PCI_DEVICE_ID_LML_33R10,
604		.i2c_decoder = I2C_DRIVERID_SAA7114,
605		.i2c_encoder = I2C_DRIVERID_ADV7170,
606		.video_codec = CODEC_TYPE_ZR36060,
607
608		.inputs = 2,
609		.input = {
610			{ 0, "Composite" },
611			{ 7, "S-Video" }
612		},
613		.norms = 2,
614		.tvn = {
615			&f50ccir601_lm33r10,
616			&f60ccir601_lm33r10,
617			NULL
618		},
619		.jpeg_int = ZR36057_ISR_GIRQ1,
620		.vsync_int = ZR36057_ISR_GIRQ0,
621		.gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
622		.gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
623		.gpcs = { 3, 1 },
624		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
625		.gws_not_connected = 1,
626		.input_mux = 0,
627		.init = &lml33_init,
628	}, {
629		.type = BUZ,
630		.name = "Buz",
631		.vendor_id = PCI_VENDOR_ID_IOMEGA,
632		.device_id = PCI_DEVICE_ID_IOMEGA_BUZ,
633		.i2c_decoder = I2C_DRIVERID_SAA7111A,
634		.i2c_encoder = I2C_DRIVERID_SAA7185B,
635		.video_codec = CODEC_TYPE_ZR36060,
636
637		.inputs = 2,
638		.input = {
639			{ 3, "Composite" },
640			{ 7, "S-Video" }
641		},
642		.norms = 3,
643		.tvn = {
644			&f50ccir601,
645			&f60ccir601,
646			&f50ccir601
647		},
648		.jpeg_int = ZR36057_ISR_GIRQ1,
649		.vsync_int = ZR36057_ISR_GIRQ0,
650		.gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
651		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
652		.gpcs = { 3, 1 },
653		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
654		.gws_not_connected = 1,
655		.input_mux = 0,
656		.init = &buz_init,
657	}, {
658		.type = AVS6EYES,
659		.name = "6-Eyes",
660		/* AverMedia chose not to brand the 6-Eyes. Thus it
661		   can't be autodetected, and requires card=x. */
662		.vendor_id = -1,
663		.device_id = -1,
664		.i2c_decoder = I2C_DRIVERID_KS0127,
665		.i2c_encoder = I2C_DRIVERID_BT866,
666		.video_codec = CODEC_TYPE_ZR36060,
667
668		.inputs = 10,
669		.input = {
670			{ 0, "Composite 1" },
671			{ 1, "Composite 2" },
672			{ 2, "Composite 3" },
673			{ 4, "Composite 4" },
674			{ 5, "Composite 5" },
675			{ 6, "Composite 6" },
676			{ 8, "S-Video 1" },
677			{ 9, "S-Video 2" },
678			{10, "S-Video 3" },
679			{15, "YCbCr" }
680		},
681		.norms = 2,
682		.tvn = {
683			&f50ccir601_avs6eyes,
684			&f60ccir601_avs6eyes,
685			NULL
686		},
687		.jpeg_int = ZR36057_ISR_GIRQ1,
688		.vsync_int = ZR36057_ISR_GIRQ0,
689		.gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
690		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
691		.gpcs = { 3, 1 },			// Validity unknown /Sam
692		.vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 },  // Validity unknown /Sam
693		.gws_not_connected = 1,
694		.input_mux = 1,
695		.init = &avs6eyes_init,
696	}
697
698};
699
700/*
701 * I2C functions
702 */
703/* software I2C functions */
704static int
705zoran_i2c_getsda (void *data)
706{
707	struct zoran *zr = (struct zoran *) data;
708
709	return (btread(ZR36057_I2CBR) >> 1) & 1;
710}
711
712static int
713zoran_i2c_getscl (void *data)
714{
715	struct zoran *zr = (struct zoran *) data;
716
717	return btread(ZR36057_I2CBR) & 1;
718}
719
720static void
721zoran_i2c_setsda (void *data,
722		  int   state)
723{
724	struct zoran *zr = (struct zoran *) data;
725
726	if (state)
727		zr->i2cbr |= 2;
728	else
729		zr->i2cbr &= ~2;
730	btwrite(zr->i2cbr, ZR36057_I2CBR);
731}
732
733static void
734zoran_i2c_setscl (void *data,
735		  int   state)
736{
737	struct zoran *zr = (struct zoran *) data;
738
739	if (state)
740		zr->i2cbr |= 1;
741	else
742		zr->i2cbr &= ~1;
743	btwrite(zr->i2cbr, ZR36057_I2CBR);
744}
745
746static int
747zoran_i2c_client_register (struct i2c_client *client)
748{
749	struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
750	int res = 0;
751
752	dprintk(2,
753		KERN_DEBUG "%s: i2c_client_register() - driver id = %d\n",
754		ZR_DEVNAME(zr), client->driver->id);
755
756	mutex_lock(&zr->resource_lock);
757
758	if (zr->user > 0) {
759		/* we're already busy, so we keep a reference to
760		 * them... Could do a lot of stuff here, but this
761		 * is easiest. (Did I ever mention I'm a lazy ass?)
762		 */
763		res = -EBUSY;
764		goto clientreg_unlock_and_return;
765	}
766
767	if (client->driver->id == zr->card.i2c_decoder)
768		zr->decoder = client;
769	else if (client->driver->id == zr->card.i2c_encoder)
770		zr->encoder = client;
771	else {
772		res = -ENODEV;
773		goto clientreg_unlock_and_return;
774	}
775
776clientreg_unlock_and_return:
777	mutex_unlock(&zr->resource_lock);
778
779	return res;
780}
781
782static int
783zoran_i2c_client_unregister (struct i2c_client *client)
784{
785	struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
786	int res = 0;
787
788	dprintk(2, KERN_DEBUG "%s: i2c_client_unregister()\n", ZR_DEVNAME(zr));
789
790	mutex_lock(&zr->resource_lock);
791
792	if (zr->user > 0) {
793		res = -EBUSY;
794		goto clientunreg_unlock_and_return;
795	}
796
797	/* try to locate it */
798	if (client == zr->encoder) {
799		zr->encoder = NULL;
800	} else if (client == zr->decoder) {
801		zr->decoder = NULL;
802		snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%d]", zr->id);
803	}
804clientunreg_unlock_and_return:
805	mutex_unlock(&zr->resource_lock);
806	return res;
807}
808
809static struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
810	.setsda = zoran_i2c_setsda,
811	.setscl = zoran_i2c_setscl,
812	.getsda = zoran_i2c_getsda,
813	.getscl = zoran_i2c_getscl,
814	.udelay = 10,
815	.timeout = 100,
816};
817
818static struct i2c_adapter zoran_i2c_adapter_template = {
819	.name = "zr36057",
820	.id = I2C_HW_B_ZR36067,
821	.algo = NULL,
822	.client_register = zoran_i2c_client_register,
823	.client_unregister = zoran_i2c_client_unregister,
824};
825
826static int
827zoran_register_i2c (struct zoran *zr)
828{
829	memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
830	       sizeof(struct i2c_algo_bit_data));
831	zr->i2c_algo.data = zr;
832	memcpy(&zr->i2c_adapter, &zoran_i2c_adapter_template,
833	       sizeof(struct i2c_adapter));
834	strncpy(I2C_NAME(&zr->i2c_adapter), ZR_DEVNAME(zr),
835		sizeof(I2C_NAME(&zr->i2c_adapter)) - 1);
836	i2c_set_adapdata(&zr->i2c_adapter, zr);
837	zr->i2c_adapter.algo_data = &zr->i2c_algo;
838	zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
839	return i2c_bit_add_bus(&zr->i2c_adapter);
840}
841
842static void
843zoran_unregister_i2c (struct zoran *zr)
844{
845	i2c_del_adapter(&zr->i2c_adapter);
846}
847
848/* Check a zoran_params struct for correctness, insert default params */
849
850int
851zoran_check_jpg_settings (struct zoran              *zr,
852			  struct zoran_jpg_settings *settings)
853{
854	int err = 0, err0 = 0;
855
856	dprintk(4,
857		KERN_DEBUG
858		"%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
859		ZR_DEVNAME(zr), settings->decimation, settings->HorDcm,
860		settings->VerDcm, settings->TmpDcm);
861	dprintk(4,
862		KERN_DEBUG
863		"%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
864		ZR_DEVNAME(zr), settings->img_x, settings->img_y,
865		settings->img_width, settings->img_height);
866	/* Check decimation, set default values for decimation = 1, 2, 4 */
867	switch (settings->decimation) {
868	case 1:
869
870		settings->HorDcm = 1;
871		settings->VerDcm = 1;
872		settings->TmpDcm = 1;
873		settings->field_per_buff = 2;
874		settings->img_x = 0;
875		settings->img_y = 0;
876		settings->img_width = BUZ_MAX_WIDTH;
877		settings->img_height = BUZ_MAX_HEIGHT / 2;
878		break;
879	case 2:
880
881		settings->HorDcm = 2;
882		settings->VerDcm = 1;
883		settings->TmpDcm = 2;
884		settings->field_per_buff = 1;
885		settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
886		settings->img_y = 0;
887		settings->img_width =
888		    (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
889		settings->img_height = BUZ_MAX_HEIGHT / 2;
890		break;
891	case 4:
892
893		if (zr->card.type == DC10_new) {
894			dprintk(1,
895				KERN_DEBUG
896				"%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
897				ZR_DEVNAME(zr));
898			err0++;
899			break;
900		}
901
902		settings->HorDcm = 4;
903		settings->VerDcm = 2;
904		settings->TmpDcm = 2;
905		settings->field_per_buff = 1;
906		settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
907		settings->img_y = 0;
908		settings->img_width =
909		    (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
910		settings->img_height = BUZ_MAX_HEIGHT / 2;
911		break;
912	case 0:
913
914		/* We have to check the data the user has set */
915
916		if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
917		    (zr->card.type == DC10_new || settings->HorDcm != 4))
918			err0++;
919		if (settings->VerDcm != 1 && settings->VerDcm != 2)
920			err0++;
921		if (settings->TmpDcm != 1 && settings->TmpDcm != 2)
922			err0++;
923		if (settings->field_per_buff != 1 &&
924		    settings->field_per_buff != 2)
925			err0++;
926		if (settings->img_x < 0)
927			err0++;
928		if (settings->img_y < 0)
929			err0++;
930		if (settings->img_width < 0)
931			err0++;
932		if (settings->img_height < 0)
933			err0++;
934		if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH)
935			err0++;
936		if (settings->img_y + settings->img_height >
937		    BUZ_MAX_HEIGHT / 2)
938			err0++;
939		if (settings->HorDcm && settings->VerDcm) {
940			if (settings->img_width %
941			    (16 * settings->HorDcm) != 0)
942				err0++;
943			if (settings->img_height %
944			    (8 * settings->VerDcm) != 0)
945				err0++;
946		}
947
948		if (err0) {
949			dprintk(1,
950				KERN_ERR
951				"%s: check_jpg_settings() - error in params for decimation = 0\n",
952				ZR_DEVNAME(zr));
953			err++;
954		}
955		break;
956	default:
957		dprintk(1,
958			KERN_ERR
959			"%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
960			ZR_DEVNAME(zr), settings->decimation);
961		err++;
962		break;
963	}
964
965	if (settings->jpg_comp.quality > 100)
966		settings->jpg_comp.quality = 100;
967	if (settings->jpg_comp.quality < 5)
968		settings->jpg_comp.quality = 5;
969	if (settings->jpg_comp.APPn < 0)
970		settings->jpg_comp.APPn = 0;
971	if (settings->jpg_comp.APPn > 15)
972		settings->jpg_comp.APPn = 15;
973	if (settings->jpg_comp.APP_len < 0)
974		settings->jpg_comp.APP_len = 0;
975	if (settings->jpg_comp.APP_len > 60)
976		settings->jpg_comp.APP_len = 60;
977	if (settings->jpg_comp.COM_len < 0)
978		settings->jpg_comp.COM_len = 0;
979	if (settings->jpg_comp.COM_len > 60)
980		settings->jpg_comp.COM_len = 60;
981	if (err)
982		return -EINVAL;
983	return 0;
984}
985
986void
987zoran_open_init_params (struct zoran *zr)
988{
989	int i;
990
991	/* User must explicitly set a window */
992	zr->overlay_settings.is_set = 0;
993	zr->overlay_mask = NULL;
994	zr->overlay_active = ZORAN_FREE;
995
996	zr->v4l_memgrab_active = 0;
997	zr->v4l_overlay_active = 0;
998	zr->v4l_grab_frame = NO_GRAB_ACTIVE;
999	zr->v4l_grab_seq = 0;
1000	zr->v4l_settings.width = 192;
1001	zr->v4l_settings.height = 144;
1002	zr->v4l_settings.format = &zoran_formats[4];	/* YUY2 - YUV-4:2:2 packed */
1003	zr->v4l_settings.bytesperline =
1004	    zr->v4l_settings.width *
1005	    ((zr->v4l_settings.format->depth + 7) / 8);
1006
1007	/* DMA ring stuff for V4L */
1008	zr->v4l_pend_tail = 0;
1009	zr->v4l_pend_head = 0;
1010	zr->v4l_sync_tail = 0;
1011	zr->v4l_buffers.active = ZORAN_FREE;
1012	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1013		zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
1014	}
1015	zr->v4l_buffers.allocated = 0;
1016
1017	for (i = 0; i < BUZ_MAX_FRAME; i++) {
1018		zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
1019	}
1020	zr->jpg_buffers.active = ZORAN_FREE;
1021	zr->jpg_buffers.allocated = 0;
1022	/* Set necessary params and call zoran_check_jpg_settings to set the defaults */
1023	zr->jpg_settings.decimation = 1;
1024	zr->jpg_settings.jpg_comp.quality = 50;	/* default compression factor 8 */
1025	if (zr->card.type != BUZ)
1026		zr->jpg_settings.odd_even = 1;
1027	else
1028		zr->jpg_settings.odd_even = 0;
1029	zr->jpg_settings.jpg_comp.APPn = 0;
1030	zr->jpg_settings.jpg_comp.APP_len = 0;	/* No APPn marker */
1031	memset(zr->jpg_settings.jpg_comp.APP_data, 0,
1032	       sizeof(zr->jpg_settings.jpg_comp.APP_data));
1033	zr->jpg_settings.jpg_comp.COM_len = 0;	/* No COM marker */
1034	memset(zr->jpg_settings.jpg_comp.COM_data, 0,
1035	       sizeof(zr->jpg_settings.jpg_comp.COM_data));
1036	zr->jpg_settings.jpg_comp.jpeg_markers =
1037	    JPEG_MARKER_DHT | JPEG_MARKER_DQT;
1038	i = zoran_check_jpg_settings(zr, &zr->jpg_settings);
1039	if (i)
1040		dprintk(1,
1041			KERN_ERR
1042			"%s: zoran_open_init_params() internal error\n",
1043			ZR_DEVNAME(zr));
1044
1045	clear_interrupt_counters(zr);
1046	zr->testing = 0;
1047}
1048
1049static void __devinit
1050test_interrupts (struct zoran *zr)
1051{
1052	DEFINE_WAIT(wait);
1053	int timeout, icr;
1054
1055	clear_interrupt_counters(zr);
1056
1057	zr->testing = 1;
1058	icr = btread(ZR36057_ICR);
1059	btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
1060	prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
1061	timeout = schedule_timeout(HZ);
1062	finish_wait(&zr->test_q, &wait);
1063	btwrite(0, ZR36057_ICR);
1064	btwrite(0x78000000, ZR36057_ISR);
1065	zr->testing = 0;
1066	dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
1067	if (timeout) {
1068		dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
1069	}
1070	if (*zr_debug > 1)
1071		print_interrupts(zr);
1072	btwrite(icr, ZR36057_ICR);
1073}
1074
1075static int __devinit
1076zr36057_init (struct zoran *zr)
1077{
1078	int j, err;
1079	int two = 2;
1080	int zero = 0;
1081
1082	dprintk(1,
1083		KERN_INFO
1084		"%s: zr36057_init() - initializing card[%d], zr=%p\n",
1085		ZR_DEVNAME(zr), zr->id, zr);
1086
1087	/* default setup of all parameters which will persist between opens */
1088	zr->user = 0;
1089
1090	init_waitqueue_head(&zr->v4l_capq);
1091	init_waitqueue_head(&zr->jpg_capq);
1092	init_waitqueue_head(&zr->test_q);
1093	zr->jpg_buffers.allocated = 0;
1094	zr->v4l_buffers.allocated = 0;
1095
1096	zr->buffer.base = (void *) vidmem;
1097	zr->buffer.width = 0;
1098	zr->buffer.height = 0;
1099	zr->buffer.depth = 0;
1100	zr->buffer.bytesperline = 0;
1101
1102	/* Avoid nonsense settings from user for default input/norm */
1103	if (default_norm < VIDEO_MODE_PAL &&
1104	    default_norm > VIDEO_MODE_SECAM)
1105		default_norm = VIDEO_MODE_PAL;
1106	zr->norm = default_norm;
1107	if (!(zr->timing = zr->card.tvn[zr->norm])) {
1108		dprintk(1,
1109			KERN_WARNING
1110			"%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
1111			ZR_DEVNAME(zr));
1112		zr->norm = VIDEO_MODE_PAL;
1113		zr->timing = zr->card.tvn[zr->norm];
1114	}
1115
1116	zr->input = default_input = (default_input ? 1 : 0);
1117
1118	/* Should the following be reset at every open ? */
1119	zr->hue = 32768;
1120	zr->contrast = 32768;
1121	zr->saturation = 32768;
1122	zr->brightness = 32768;
1123
1124	/* default setup (will be repeated at every open) */
1125	zoran_open_init_params(zr);
1126
1127	/* allocate memory *before* doing anything to the hardware
1128	 * in case allocation fails */
1129	zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
1130	zr->video_dev = kmalloc(sizeof(struct video_device), GFP_KERNEL);
1131	if (!zr->stat_com || !zr->video_dev) {
1132		dprintk(1,
1133			KERN_ERR
1134			"%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
1135			ZR_DEVNAME(zr));
1136		err = -ENOMEM;
1137		goto exit_free;
1138	}
1139	for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1140		zr->stat_com[j] = 1;	/* mark as unavailable to zr36057 */
1141	}
1142
1143	/*
1144	 *   Now add the template and register the device unit.
1145	 */
1146	memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1147	strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
1148	err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr);
1149	if (err < 0)
1150		goto exit_unregister;
1151
1152	zoran_init_hardware(zr);
1153	if (*zr_debug > 2)
1154		detect_guest_activity(zr);
1155	test_interrupts(zr);
1156	if (!pass_through) {
1157		decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1158		encoder_command(zr, ENCODER_SET_INPUT, &two);
1159	}
1160
1161	zr->zoran_proc = NULL;
1162	zr->initialized = 1;
1163	return 0;
1164
1165exit_unregister:
1166	zoran_unregister_i2c(zr);
1167exit_free:
1168	kfree(zr->stat_com);
1169	kfree(zr->video_dev);
1170	return err;
1171}
1172
1173static void
1174zoran_release (struct zoran *zr)
1175{
1176	if (!zr->initialized)
1177		return;
1178	/* unregister videocodec bus */
1179	if (zr->codec) {
1180		struct videocodec_master *master = zr->codec->master_data;
1181
1182		videocodec_detach(zr->codec);
1183		kfree(master);
1184	}
1185	if (zr->vfe) {
1186		struct videocodec_master *master = zr->vfe->master_data;
1187
1188		videocodec_detach(zr->vfe);
1189		kfree(master);
1190	}
1191
1192	/* unregister i2c bus */
1193	zoran_unregister_i2c(zr);
1194	/* disable PCI bus-mastering */
1195	zoran_set_pci_master(zr, 0);
1196	/* put chip into reset */
1197	btwrite(0, ZR36057_SPGPPCR);
1198	free_irq(zr->pci_dev->irq, zr);
1199	/* unmap and free memory */
1200	kfree(zr->stat_com);
1201	zoran_proc_cleanup(zr);
1202	iounmap(zr->zr36057_mem);
1203	pci_disable_device(zr->pci_dev);
1204	video_unregister_device(zr->video_dev);
1205}
1206
1207void
1208zoran_vdev_release (struct video_device *vdev)
1209{
1210	kfree(vdev);
1211}
1212
1213static struct videocodec_master * __devinit
1214zoran_setup_videocodec (struct zoran *zr,
1215			int           type)
1216{
1217	struct videocodec_master *m = NULL;
1218
1219	m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1220	if (!m) {
1221		dprintk(1,
1222			KERN_ERR
1223			"%s: zoran_setup_videocodec() - no memory\n",
1224			ZR_DEVNAME(zr));
1225		return m;
1226	}
1227
1228	m->magic = 0L; /* magic not used */
1229	m->type = VID_HARDWARE_ZR36067;
1230	m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1231	strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1232	m->data = zr;
1233
1234	switch (type)
1235	{
1236	case CODEC_TYPE_ZR36060:
1237		m->readreg = zr36060_read;
1238		m->writereg = zr36060_write;
1239		m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1240		break;
1241	case CODEC_TYPE_ZR36050:
1242		m->readreg = zr36050_read;
1243		m->writereg = zr36050_write;
1244		m->flags |= CODEC_FLAG_JPEG;
1245		break;
1246	case CODEC_TYPE_ZR36016:
1247		m->readreg = zr36016_read;
1248		m->writereg = zr36016_write;
1249		m->flags |= CODEC_FLAG_VFE;
1250		break;
1251	}
1252
1253	return m;
1254}
1255
1256/*
1257 *   Scan for a Buz card (actually for the PCI contoler ZR36057),
1258 *   request the irq and map the io memory
1259 */
1260static int __devinit
1261find_zr36057 (void)
1262{
1263	unsigned char latency, need_latency;
1264	struct zoran *zr;
1265	struct pci_dev *dev = NULL;
1266	int result;
1267	struct videocodec_master *master_vfe = NULL;
1268	struct videocodec_master *master_codec = NULL;
1269	int card_num;
1270	char *i2c_enc_name, *i2c_dec_name, *codec_name, *vfe_name;
1271
1272	zoran_num = 0;
1273	while (zoran_num < BUZ_MAX &&
1274	       (dev = pci_get_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
1275		card_num = card[zoran_num];
1276		zr = &zoran[zoran_num];
1277		memset(zr, 0, sizeof(struct zoran));	// Just in case if previous cycle failed
1278		zr->pci_dev = dev;
1279		//zr->zr36057_mem = NULL;
1280		zr->id = zoran_num;
1281		snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1282		spin_lock_init(&zr->spinlock);
1283		mutex_init(&zr->resource_lock);
1284		if (pci_enable_device(dev))
1285			continue;
1286		zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
1287		pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
1288				     &zr->revision);
1289		if (zr->revision < 2) {
1290			dprintk(1,
1291				KERN_INFO
1292				"%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
1293				ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1294				zr->zr36057_adr);
1295
1296			if (card_num == -1) {
1297				dprintk(1,
1298					KERN_ERR
1299					"%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
1300					ZR_DEVNAME(zr));
1301				continue;
1302			}
1303		} else {
1304			int i;
1305			unsigned short ss_vendor, ss_device;
1306
1307			ss_vendor = zr->pci_dev->subsystem_vendor;
1308			ss_device = zr->pci_dev->subsystem_device;
1309			dprintk(1,
1310				KERN_INFO
1311				"%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
1312				ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1313				zr->zr36057_adr);
1314			dprintk(1,
1315				KERN_INFO
1316				"%s: subsystem vendor=0x%04x id=0x%04x\n",
1317				ZR_DEVNAME(zr), ss_vendor, ss_device);
1318			if (card_num == -1) {
1319				dprintk(3,
1320					KERN_DEBUG
1321					"%s: find_zr36057() - trying to autodetect card type\n",
1322					ZR_DEVNAME(zr));
1323				for (i=0;i<NUM_CARDS;i++) {
1324					if (ss_vendor == zoran_cards[i].vendor_id &&
1325					    ss_device == zoran_cards[i].device_id) {
1326						dprintk(3,
1327							KERN_DEBUG
1328							"%s: find_zr36057() - card %s detected\n",
1329							ZR_DEVNAME(zr),
1330							zoran_cards[i].name);
1331						card_num = i;
1332						break;
1333					}
1334				}
1335				if (i == NUM_CARDS) {
1336					dprintk(1,
1337						KERN_ERR
1338						"%s: find_zr36057() - unknown card\n",
1339						ZR_DEVNAME(zr));
1340					continue;
1341				}
1342			}
1343		}
1344
1345		if (card_num < 0 || card_num >= NUM_CARDS) {
1346			dprintk(2,
1347				KERN_ERR
1348				"%s: find_zr36057() - invalid cardnum %d\n",
1349				ZR_DEVNAME(zr), card_num);
1350			continue;
1351		}
1352
1353		/* even though we make this a non pointer and thus
1354		 * theoretically allow for making changes to this struct
1355		 * on a per-individual card basis at runtime, this is
1356		 * strongly discouraged. This structure is intended to
1357		 * keep general card information, no settings or anything */
1358		zr->card = zoran_cards[card_num];
1359		snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1360			 "%s[%u]", zr->card.name, zr->id);
1361
1362		zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
1363		if (!zr->zr36057_mem) {
1364			dprintk(1,
1365				KERN_ERR
1366				"%s: find_zr36057() - ioremap failed\n",
1367				ZR_DEVNAME(zr));
1368			continue;
1369		}
1370
1371		result = request_irq(zr->pci_dev->irq,
1372				     zoran_irq,
1373				     IRQF_SHARED | IRQF_DISABLED,
1374				     ZR_DEVNAME(zr),
1375				     (void *) zr);
1376		if (result < 0) {
1377			if (result == -EINVAL) {
1378				dprintk(1,
1379					KERN_ERR
1380					"%s: find_zr36057() - bad irq number or handler\n",
1381					ZR_DEVNAME(zr));
1382			} else if (result == -EBUSY) {
1383				dprintk(1,
1384					KERN_ERR
1385					"%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
1386					ZR_DEVNAME(zr), zr->pci_dev->irq);
1387			} else {
1388				dprintk(1,
1389					KERN_ERR
1390					"%s: find_zr36057() - can't assign irq, error code %d\n",
1391					ZR_DEVNAME(zr), result);
1392			}
1393			goto zr_unmap;
1394		}
1395
1396		/* set PCI latency timer */
1397		pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1398				     &latency);
1399		need_latency = zr->revision > 1 ? 32 : 48;
1400		if (latency != need_latency) {
1401			dprintk(2,
1402				KERN_INFO
1403				"%s: Changing PCI latency from %d to %d.\n",
1404				ZR_DEVNAME(zr), latency, need_latency);
1405			pci_write_config_byte(zr->pci_dev,
1406					      PCI_LATENCY_TIMER,
1407					      need_latency);
1408		}
1409
1410		zr36057_restart(zr);
1411		/* i2c */
1412		dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1413			ZR_DEVNAME(zr));
1414
1415		/* i2c decoder */
1416		if (decoder[zr->id] != -1) {
1417			i2c_dec_name = i2cid_to_modulename(decoder[zr->id]);
1418			zr->card.i2c_decoder = decoder[zr->id];
1419		} else if (zr->card.i2c_decoder != 0) {
1420			i2c_dec_name =
1421				i2cid_to_modulename(zr->card.i2c_decoder);
1422		} else {
1423			i2c_dec_name = NULL;
1424		}
1425
1426		if (i2c_dec_name) {
1427			if ((result = request_module(i2c_dec_name)) < 0) {
1428				dprintk(1,
1429					KERN_ERR
1430					"%s: failed to load module %s: %d\n",
1431					ZR_DEVNAME(zr), i2c_dec_name, result);
1432			}
1433		}
1434
1435		/* i2c encoder */
1436		if (encoder[zr->id] != -1) {
1437			i2c_enc_name = i2cid_to_modulename(encoder[zr->id]);
1438			zr->card.i2c_encoder = encoder[zr->id];
1439		} else if (zr->card.i2c_encoder != 0) {
1440			i2c_enc_name =
1441				i2cid_to_modulename(zr->card.i2c_encoder);
1442		} else {
1443			i2c_enc_name = NULL;
1444		}
1445
1446		if (i2c_enc_name) {
1447			if ((result = request_module(i2c_enc_name)) < 0) {
1448				dprintk(1,
1449					KERN_ERR
1450					"%s: failed to load module %s: %d\n",
1451					ZR_DEVNAME(zr), i2c_enc_name, result);
1452			}
1453		}
1454
1455		if (zoran_register_i2c(zr) < 0) {
1456			dprintk(1,
1457				KERN_ERR
1458				"%s: find_zr36057() - can't initialize i2c bus\n",
1459				ZR_DEVNAME(zr));
1460			goto zr_free_irq;
1461		}
1462
1463		dprintk(2,
1464			KERN_INFO "%s: Initializing videocodec bus...\n",
1465			ZR_DEVNAME(zr));
1466
1467		if (zr->card.video_codec != 0 &&
1468		    (codec_name =
1469		     codecid_to_modulename(zr->card.video_codec)) != NULL) {
1470			if ((result = request_module(codec_name)) < 0) {
1471				dprintk(1,
1472					KERN_ERR
1473					"%s: failed to load modules %s: %d\n",
1474					ZR_DEVNAME(zr), codec_name, result);
1475			}
1476		}
1477		if (zr->card.video_vfe != 0 &&
1478		    (vfe_name =
1479		     codecid_to_modulename(zr->card.video_vfe)) != NULL) {
1480			if ((result = request_module(vfe_name)) < 0) {
1481				dprintk(1,
1482					KERN_ERR
1483					"%s: failed to load modules %s: %d\n",
1484					ZR_DEVNAME(zr), vfe_name, result);
1485			}
1486		}
1487
1488		/* reset JPEG codec */
1489		jpeg_codec_sleep(zr, 1);
1490		jpeg_codec_reset(zr);
1491		/* video bus enabled */
1492		/* display codec revision */
1493		if (zr->card.video_codec != 0) {
1494			master_codec = zoran_setup_videocodec(zr,
1495							      zr->card.video_codec);
1496			if (!master_codec)
1497				goto zr_unreg_i2c;
1498			zr->codec = videocodec_attach(master_codec);
1499			if (!zr->codec) {
1500				dprintk(1,
1501					KERN_ERR
1502					"%s: find_zr36057() - no codec found\n",
1503					ZR_DEVNAME(zr));
1504				goto zr_free_codec;
1505			}
1506			if (zr->codec->type != zr->card.video_codec) {
1507				dprintk(1,
1508					KERN_ERR
1509					"%s: find_zr36057() - wrong codec\n",
1510					ZR_DEVNAME(zr));
1511				goto zr_detach_codec;
1512			}
1513		}
1514		if (zr->card.video_vfe != 0) {
1515			master_vfe = zoran_setup_videocodec(zr,
1516							    zr->card.video_vfe);
1517			if (!master_vfe)
1518				goto zr_detach_codec;
1519			zr->vfe = videocodec_attach(master_vfe);
1520			if (!zr->vfe) {
1521				dprintk(1,
1522					KERN_ERR
1523					"%s: find_zr36057() - no VFE found\n",
1524					ZR_DEVNAME(zr));
1525				goto zr_free_vfe;
1526			}
1527			if (zr->vfe->type != zr->card.video_vfe) {
1528				dprintk(1,
1529					KERN_ERR
1530					"%s: find_zr36057() = wrong VFE\n",
1531					ZR_DEVNAME(zr));
1532				goto zr_detach_vfe;
1533			}
1534		}
1535		/* Success so keep the pci_dev referenced */
1536		pci_dev_get(zr->pci_dev);
1537		zoran_num++;
1538		continue;
1539
1540		// Init errors
1541	      zr_detach_vfe:
1542		videocodec_detach(zr->vfe);
1543	      zr_free_vfe:
1544		kfree(master_vfe);
1545	      zr_detach_codec:
1546		videocodec_detach(zr->codec);
1547	      zr_free_codec:
1548		kfree(master_codec);
1549	      zr_unreg_i2c:
1550		zoran_unregister_i2c(zr);
1551	      zr_free_irq:
1552		btwrite(0, ZR36057_SPGPPCR);
1553		free_irq(zr->pci_dev->irq, zr);
1554	      zr_unmap:
1555		iounmap(zr->zr36057_mem);
1556		continue;
1557	}
1558	if (dev)	/* Clean up ref count on early exit */
1559		pci_dev_put(dev);
1560
1561	if (zoran_num == 0) {
1562		dprintk(1, KERN_INFO "No known MJPEG cards found.\n");
1563	}
1564	return zoran_num;
1565}
1566
1567static int __init
1568init_dc10_cards (void)
1569{
1570	int i;
1571
1572	memset(zoran, 0, sizeof(zoran));
1573	printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
1574	       MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
1575
1576	/* Look for cards */
1577	if (find_zr36057() < 0) {
1578		return -EIO;
1579	}
1580	if (zoran_num == 0)
1581		return -ENODEV;
1582	dprintk(1, KERN_INFO "%s: %d card(s) found\n", ZORAN_NAME,
1583		zoran_num);
1584	/* check the parameters we have been given, adjust if necessary */
1585	if (v4l_nbufs < 2)
1586		v4l_nbufs = 2;
1587	if (v4l_nbufs > VIDEO_MAX_FRAME)
1588		v4l_nbufs = VIDEO_MAX_FRAME;
1589	/* The user specfies the in KB, we want them in byte
1590	 * (and page aligned) */
1591	v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1592	if (v4l_bufsize < 32768)
1593		v4l_bufsize = 32768;
1594	/* 2 MB is arbitrary but sufficient for the maximum possible images */
1595	if (v4l_bufsize > 2048 * 1024)
1596		v4l_bufsize = 2048 * 1024;
1597	if (jpg_nbufs < 4)
1598		jpg_nbufs = 4;
1599	if (jpg_nbufs > BUZ_MAX_FRAME)
1600		jpg_nbufs = BUZ_MAX_FRAME;
1601	jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1602	if (jpg_bufsize < 8192)
1603		jpg_bufsize = 8192;
1604	if (jpg_bufsize > (512 * 1024))
1605		jpg_bufsize = 512 * 1024;
1606	/* Use parameter for vidmem or try to find a video card */
1607	if (vidmem) {
1608		dprintk(1,
1609			KERN_INFO
1610			"%s: Using supplied video memory base address @ 0x%lx\n",
1611			ZORAN_NAME, vidmem);
1612	}
1613
1614	/* random nonsense */
1615	dprintk(5, KERN_DEBUG "Jotti is een held!\n");
1616
1617	/* some mainboards might not do PCI-PCI data transfer well */
1618	if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
1619		dprintk(1,
1620			KERN_WARNING
1621			"%s: chipset does not support reliable PCI-PCI DMA\n",
1622			ZORAN_NAME);
1623	}
1624
1625	/* take care of Natoma chipset and a revision 1 zr36057 */
1626	for (i = 0; i < zoran_num; i++) {
1627		struct zoran *zr = &zoran[i];
1628
1629		if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
1630			zr->jpg_buffers.need_contiguous = 1;
1631			dprintk(1,
1632				KERN_INFO
1633				"%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1634				ZR_DEVNAME(zr));
1635		}
1636
1637		if (zr36057_init(zr) < 0) {
1638			for (i = 0; i < zoran_num; i++)
1639				zoran_release(&zoran[i]);
1640			return -EIO;
1641		}
1642		zoran_proc_init(zr);
1643	}
1644
1645	return 0;
1646}
1647
1648static void __exit
1649unload_dc10_cards (void)
1650{
1651	int i;
1652
1653	for (i = 0; i < zoran_num; i++)
1654		zoran_release(&zoran[i]);
1655}
1656
1657module_init(init_dc10_cards);
1658module_exit(unload_dc10_cards);
1659