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