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