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