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