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