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