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