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