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