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