Input: wacom - fix touch parsing on newer Bamboos
[linux-btrfs-devel.git] / drivers / media / video / zoran / zoran_card.c
blob79b04ac0f1adb97cc6d7c7c9912877bc2d8505b5
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.
6 * This part handles card-specific data and detection
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
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>
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.
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.
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.
30 #include <linux/delay.h>
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 #include <linux/slab.h>
39 #include <linux/proc_fs.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-algo-bit.h>
42 #include <linux/videodev2.h>
43 #include <linux/spinlock.h>
44 #include <linux/sem.h>
45 #include <linux/kmod.h>
46 #include <linux/wait.h>
48 #include <linux/pci.h>
49 #include <linux/interrupt.h>
50 #include <linux/mutex.h>
51 #include <linux/io.h>
52 #include <media/v4l2-common.h>
53 #include <media/bt819.h>
55 #include "videocodec.h"
56 #include "zoran.h"
57 #include "zoran_card.h"
58 #include "zoran_device.h"
59 #include "zoran_procfs.h"
61 extern const struct zoran_format zoran_formats[];
63 static int card[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
64 module_param_array(card, int, NULL, 0444);
65 MODULE_PARM_DESC(card, "Card type");
68 The video mem address of the video card.
69 The driver has a little database for some videocards
70 to determine it from there. If your video card is not in there
71 you have either to give it to the driver as a parameter
72 or set in in a VIDIOCSFBUF ioctl
75 static unsigned long vidmem; /* default = 0 - Video memory base address */
76 module_param(vidmem, ulong, 0444);
77 MODULE_PARM_DESC(vidmem, "Default video memory base address");
80 Default input and video norm at startup of the driver.
83 static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
84 module_param(default_input, uint, 0444);
85 MODULE_PARM_DESC(default_input,
86 "Default input (0=Composite, 1=S-Video, 2=Internal)");
88 static int default_mux = 1; /* 6 Eyes input selection */
89 module_param(default_mux, int, 0644);
90 MODULE_PARM_DESC(default_mux,
91 "Default 6 Eyes mux setting (Input selection)");
93 static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
94 module_param(default_norm, int, 0444);
95 MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
97 /* /dev/videoN, -1 for autodetect */
98 static int video_nr[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
99 module_param_array(video_nr, int, NULL, 0444);
100 MODULE_PARM_DESC(video_nr, "Video device number (-1=Auto)");
102 int v4l_nbufs = 4;
103 int v4l_bufsize = 864; /* Everybody should be able to work with this setting */
104 module_param(v4l_nbufs, int, 0644);
105 MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
106 module_param(v4l_bufsize, int, 0644);
107 MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
109 int jpg_nbufs = 32;
110 int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
111 module_param(jpg_nbufs, int, 0644);
112 MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
113 module_param(jpg_bufsize, int, 0644);
114 MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
116 int pass_through = 0; /* 1=Pass through TV signal when device is not used */
117 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
118 module_param(pass_through, int, 0644);
119 MODULE_PARM_DESC(pass_through,
120 "Pass TV signal through to TV-out when idling");
122 int zr36067_debug = 1;
123 module_param_named(debug, zr36067_debug, int, 0644);
124 MODULE_PARM_DESC(debug, "Debug level (0-5)");
126 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
127 MODULE_AUTHOR("Serguei Miridonov");
128 MODULE_LICENSE("GPL");
130 #define ZR_DEVICE(subven, subdev, data) { \
131 .vendor = PCI_VENDOR_ID_ZORAN, .device = PCI_DEVICE_ID_ZORAN_36057, \
132 .subvendor = (subven), .subdevice = (subdev), .driver_data = (data) }
134 static struct pci_device_id zr36067_pci_tbl[] = {
135 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC10PLUS, DC10plus),
136 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC30PLUS, DC30plus),
137 ZR_DEVICE(PCI_VENDOR_ID_ELECTRONICDESIGNGMBH, PCI_DEVICE_ID_LML_33R10, LML33R10),
138 ZR_DEVICE(PCI_VENDOR_ID_IOMEGA, PCI_DEVICE_ID_IOMEGA_BUZ, BUZ),
139 ZR_DEVICE(PCI_ANY_ID, PCI_ANY_ID, NUM_CARDS),
142 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
144 static unsigned int zoran_num; /* number of cards found */
146 /* videocodec bus functions ZR36060 */
147 static u32
148 zr36060_read (struct videocodec *codec,
149 u16 reg)
151 struct zoran *zr = (struct zoran *) codec->master_data->data;
152 __u32 data;
154 if (post_office_wait(zr)
155 || post_office_write(zr, 0, 1, reg >> 8)
156 || post_office_write(zr, 0, 2, reg & 0xff)) {
157 return -1;
160 data = post_office_read(zr, 0, 3) & 0xff;
161 return data;
164 static void
165 zr36060_write (struct videocodec *codec,
166 u16 reg,
167 u32 val)
169 struct zoran *zr = (struct zoran *) codec->master_data->data;
171 if (post_office_wait(zr)
172 || post_office_write(zr, 0, 1, reg >> 8)
173 || post_office_write(zr, 0, 2, reg & 0xff)) {
174 return;
177 post_office_write(zr, 0, 3, val & 0xff);
180 /* videocodec bus functions ZR36050 */
181 static u32
182 zr36050_read (struct videocodec *codec,
183 u16 reg)
185 struct zoran *zr = (struct zoran *) codec->master_data->data;
186 __u32 data;
188 if (post_office_wait(zr)
189 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
190 return -1;
193 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
194 return data;
197 static void
198 zr36050_write (struct videocodec *codec,
199 u16 reg,
200 u32 val)
202 struct zoran *zr = (struct zoran *) codec->master_data->data;
204 if (post_office_wait(zr)
205 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
206 return;
209 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
212 /* videocodec bus functions ZR36016 */
213 static u32
214 zr36016_read (struct videocodec *codec,
215 u16 reg)
217 struct zoran *zr = (struct zoran *) codec->master_data->data;
218 __u32 data;
220 if (post_office_wait(zr)) {
221 return -1;
224 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
225 return data;
228 /* hack for in zoran_device.c */
229 void
230 zr36016_write (struct videocodec *codec,
231 u16 reg,
232 u32 val)
234 struct zoran *zr = (struct zoran *) codec->master_data->data;
236 if (post_office_wait(zr)) {
237 return;
240 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
244 * Board specific information
247 static void
248 dc10_init (struct zoran *zr)
250 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
252 /* Pixel clock selection */
253 GPIO(zr, 4, 0);
254 GPIO(zr, 5, 1);
255 /* Enable the video bus sync signals */
256 GPIO(zr, 7, 0);
259 static void
260 dc10plus_init (struct zoran *zr)
262 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
265 static void
266 buz_init (struct zoran *zr)
268 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
270 /* some stuff from Iomega */
271 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
272 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
273 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
276 static void
277 lml33_init (struct zoran *zr)
279 dprintk(3, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
281 GPIO(zr, 2, 1); // Set Composite input/output
284 static void
285 avs6eyes_init (struct zoran *zr)
287 // AverMedia 6-Eyes original driver by Christer Weinigel
289 // Lifted straight from Christer's old driver and
290 // modified slightly by Martin Samuelsson.
292 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
294 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
295 udelay(2);
297 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
298 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
299 GPIO(zr, 2, mux & 1); /* MUX S0 */
300 GPIO(zr, 3, 0); /* /FRAME on */
301 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
302 GPIO(zr, 5, mux & 2); /* MUX S1 */
303 GPIO(zr, 6, 0); /* ? */
304 GPIO(zr, 7, mux & 4); /* MUX S2 */
308 static char *
309 codecid_to_modulename (u16 codecid)
311 char *name = NULL;
313 switch (codecid) {
314 case CODEC_TYPE_ZR36060:
315 name = "zr36060";
316 break;
317 case CODEC_TYPE_ZR36050:
318 name = "zr36050";
319 break;
320 case CODEC_TYPE_ZR36016:
321 name = "zr36016";
322 break;
325 return name;
328 // struct tvnorm {
329 // u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
330 // };
332 static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
333 static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
334 static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
335 static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
337 static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
338 static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
340 /* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
341 static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
342 static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
344 /* FIXME: I cannot swap U and V in saa7114, so i do one
345 * pixel left shift in zoran (75 -> 74)
346 * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
347 static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
348 static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
350 /* FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I
351 * copy Maxim's left shift hack for the 6 Eyes.
353 * Christer's driver used the unshifted norms, though...
354 * /Sam */
355 static struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
356 static struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
358 static const unsigned short vpx3220_addrs[] = { 0x43, 0x47, I2C_CLIENT_END };
359 static const unsigned short saa7110_addrs[] = { 0x4e, 0x4f, I2C_CLIENT_END };
360 static const unsigned short saa7111_addrs[] = { 0x25, 0x24, I2C_CLIENT_END };
361 static const unsigned short saa7114_addrs[] = { 0x21, 0x20, I2C_CLIENT_END };
362 static const unsigned short adv717x_addrs[] = { 0x6a, 0x6b, 0x2a, 0x2b, I2C_CLIENT_END };
363 static const unsigned short ks0127_addrs[] = { 0x6c, 0x6d, I2C_CLIENT_END };
364 static const unsigned short saa7185_addrs[] = { 0x44, I2C_CLIENT_END };
365 static const unsigned short bt819_addrs[] = { 0x45, I2C_CLIENT_END };
366 static const unsigned short bt856_addrs[] = { 0x44, I2C_CLIENT_END };
367 static const unsigned short bt866_addrs[] = { 0x44, I2C_CLIENT_END };
369 static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
371 .type = DC10_old,
372 .name = "DC10(old)",
373 .i2c_decoder = "vpx3220a",
374 .addrs_decoder = vpx3220_addrs,
375 .video_codec = CODEC_TYPE_ZR36050,
376 .video_vfe = CODEC_TYPE_ZR36016,
378 .inputs = 3,
379 .input = {
380 { 1, "Composite" },
381 { 2, "S-Video" },
382 { 0, "Internal/comp" }
384 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
385 .tvn = {
386 &f50sqpixel_dc10,
387 &f60sqpixel_dc10,
388 &f50sqpixel_dc10
390 .jpeg_int = 0,
391 .vsync_int = ZR36057_ISR_GIRQ1,
392 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
393 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
394 .gpcs = { -1, 0 },
395 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
396 .gws_not_connected = 0,
397 .input_mux = 0,
398 .init = &dc10_init,
399 }, {
400 .type = DC10_new,
401 .name = "DC10(new)",
402 .i2c_decoder = "saa7110",
403 .addrs_decoder = saa7110_addrs,
404 .i2c_encoder = "adv7175",
405 .addrs_encoder = adv717x_addrs,
406 .video_codec = CODEC_TYPE_ZR36060,
408 .inputs = 3,
409 .input = {
410 { 0, "Composite" },
411 { 7, "S-Video" },
412 { 5, "Internal/comp" }
414 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
415 .tvn = {
416 &f50sqpixel,
417 &f60sqpixel,
418 &f50sqpixel},
419 .jpeg_int = ZR36057_ISR_GIRQ0,
420 .vsync_int = ZR36057_ISR_GIRQ1,
421 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
422 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
423 .gpcs = { -1, 1},
424 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
425 .gws_not_connected = 0,
426 .input_mux = 0,
427 .init = &dc10plus_init,
428 }, {
429 .type = DC10plus,
430 .name = "DC10plus",
431 .i2c_decoder = "saa7110",
432 .addrs_decoder = saa7110_addrs,
433 .i2c_encoder = "adv7175",
434 .addrs_encoder = adv717x_addrs,
435 .video_codec = CODEC_TYPE_ZR36060,
437 .inputs = 3,
438 .input = {
439 { 0, "Composite" },
440 { 7, "S-Video" },
441 { 5, "Internal/comp" }
443 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
444 .tvn = {
445 &f50sqpixel,
446 &f60sqpixel,
447 &f50sqpixel
449 .jpeg_int = ZR36057_ISR_GIRQ0,
450 .vsync_int = ZR36057_ISR_GIRQ1,
451 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
452 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
453 .gpcs = { -1, 1 },
454 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
455 .gws_not_connected = 0,
456 .input_mux = 0,
457 .init = &dc10plus_init,
458 }, {
459 .type = DC30,
460 .name = "DC30",
461 .i2c_decoder = "vpx3220a",
462 .addrs_decoder = vpx3220_addrs,
463 .i2c_encoder = "adv7175",
464 .addrs_encoder = adv717x_addrs,
465 .video_codec = CODEC_TYPE_ZR36050,
466 .video_vfe = CODEC_TYPE_ZR36016,
468 .inputs = 3,
469 .input = {
470 { 1, "Composite" },
471 { 2, "S-Video" },
472 { 0, "Internal/comp" }
474 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
475 .tvn = {
476 &f50sqpixel_dc10,
477 &f60sqpixel_dc10,
478 &f50sqpixel_dc10
480 .jpeg_int = 0,
481 .vsync_int = ZR36057_ISR_GIRQ1,
482 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
483 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
484 .gpcs = { -1, 0 },
485 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
486 .gws_not_connected = 0,
487 .input_mux = 0,
488 .init = &dc10_init,
489 }, {
490 .type = DC30plus,
491 .name = "DC30plus",
492 .i2c_decoder = "vpx3220a",
493 .addrs_decoder = vpx3220_addrs,
494 .i2c_encoder = "adv7175",
495 .addrs_encoder = adv717x_addrs,
496 .video_codec = CODEC_TYPE_ZR36050,
497 .video_vfe = CODEC_TYPE_ZR36016,
499 .inputs = 3,
500 .input = {
501 { 1, "Composite" },
502 { 2, "S-Video" },
503 { 0, "Internal/comp" }
505 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
506 .tvn = {
507 &f50sqpixel_dc10,
508 &f60sqpixel_dc10,
509 &f50sqpixel_dc10
511 .jpeg_int = 0,
512 .vsync_int = ZR36057_ISR_GIRQ1,
513 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
514 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
515 .gpcs = { -1, 0 },
516 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
517 .gws_not_connected = 0,
518 .input_mux = 0,
519 .init = &dc10_init,
520 }, {
521 .type = LML33,
522 .name = "LML33",
523 .i2c_decoder = "bt819a",
524 .addrs_decoder = bt819_addrs,
525 .i2c_encoder = "bt856",
526 .addrs_encoder = bt856_addrs,
527 .video_codec = CODEC_TYPE_ZR36060,
529 .inputs = 2,
530 .input = {
531 { 0, "Composite" },
532 { 7, "S-Video" }
534 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
535 .tvn = {
536 &f50ccir601_lml33,
537 &f60ccir601_lml33,
538 NULL
540 .jpeg_int = ZR36057_ISR_GIRQ1,
541 .vsync_int = ZR36057_ISR_GIRQ0,
542 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
543 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
544 .gpcs = { 3, 1 },
545 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
546 .gws_not_connected = 1,
547 .input_mux = 0,
548 .init = &lml33_init,
549 }, {
550 .type = LML33R10,
551 .name = "LML33R10",
552 .i2c_decoder = "saa7114",
553 .addrs_decoder = saa7114_addrs,
554 .i2c_encoder = "adv7170",
555 .addrs_encoder = adv717x_addrs,
556 .video_codec = CODEC_TYPE_ZR36060,
558 .inputs = 2,
559 .input = {
560 { 0, "Composite" },
561 { 7, "S-Video" }
563 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
564 .tvn = {
565 &f50ccir601_lm33r10,
566 &f60ccir601_lm33r10,
567 NULL
569 .jpeg_int = ZR36057_ISR_GIRQ1,
570 .vsync_int = ZR36057_ISR_GIRQ0,
571 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
572 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
573 .gpcs = { 3, 1 },
574 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
575 .gws_not_connected = 1,
576 .input_mux = 0,
577 .init = &lml33_init,
578 }, {
579 .type = BUZ,
580 .name = "Buz",
581 .i2c_decoder = "saa7111",
582 .addrs_decoder = saa7111_addrs,
583 .i2c_encoder = "saa7185",
584 .addrs_encoder = saa7185_addrs,
585 .video_codec = CODEC_TYPE_ZR36060,
587 .inputs = 2,
588 .input = {
589 { 3, "Composite" },
590 { 7, "S-Video" }
592 .norms = V4L2_STD_NTSC|V4L2_STD_PAL|V4L2_STD_SECAM,
593 .tvn = {
594 &f50ccir601,
595 &f60ccir601,
596 &f50ccir601
598 .jpeg_int = ZR36057_ISR_GIRQ1,
599 .vsync_int = ZR36057_ISR_GIRQ0,
600 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
601 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
602 .gpcs = { 3, 1 },
603 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
604 .gws_not_connected = 1,
605 .input_mux = 0,
606 .init = &buz_init,
607 }, {
608 .type = AVS6EYES,
609 .name = "6-Eyes",
610 /* AverMedia chose not to brand the 6-Eyes. Thus it
611 can't be autodetected, and requires card=x. */
612 .i2c_decoder = "ks0127",
613 .addrs_decoder = ks0127_addrs,
614 .i2c_encoder = "bt866",
615 .addrs_encoder = bt866_addrs,
616 .video_codec = CODEC_TYPE_ZR36060,
618 .inputs = 10,
619 .input = {
620 { 0, "Composite 1" },
621 { 1, "Composite 2" },
622 { 2, "Composite 3" },
623 { 4, "Composite 4" },
624 { 5, "Composite 5" },
625 { 6, "Composite 6" },
626 { 8, "S-Video 1" },
627 { 9, "S-Video 2" },
628 {10, "S-Video 3" },
629 {15, "YCbCr" }
631 .norms = V4L2_STD_NTSC|V4L2_STD_PAL,
632 .tvn = {
633 &f50ccir601_avs6eyes,
634 &f60ccir601_avs6eyes,
635 NULL
637 .jpeg_int = ZR36057_ISR_GIRQ1,
638 .vsync_int = ZR36057_ISR_GIRQ0,
639 .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
640 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
641 .gpcs = { 3, 1 }, // Validity unknown /Sam
642 .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
643 .gws_not_connected = 1,
644 .input_mux = 1,
645 .init = &avs6eyes_init,
651 * I2C functions
653 /* software I2C functions */
654 static int
655 zoran_i2c_getsda (void *data)
657 struct zoran *zr = (struct zoran *) data;
659 return (btread(ZR36057_I2CBR) >> 1) & 1;
662 static int
663 zoran_i2c_getscl (void *data)
665 struct zoran *zr = (struct zoran *) data;
667 return btread(ZR36057_I2CBR) & 1;
670 static void
671 zoran_i2c_setsda (void *data,
672 int state)
674 struct zoran *zr = (struct zoran *) data;
676 if (state)
677 zr->i2cbr |= 2;
678 else
679 zr->i2cbr &= ~2;
680 btwrite(zr->i2cbr, ZR36057_I2CBR);
683 static void
684 zoran_i2c_setscl (void *data,
685 int state)
687 struct zoran *zr = (struct zoran *) data;
689 if (state)
690 zr->i2cbr |= 1;
691 else
692 zr->i2cbr &= ~1;
693 btwrite(zr->i2cbr, ZR36057_I2CBR);
696 static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
697 .setsda = zoran_i2c_setsda,
698 .setscl = zoran_i2c_setscl,
699 .getsda = zoran_i2c_getsda,
700 .getscl = zoran_i2c_getscl,
701 .udelay = 10,
702 .timeout = 100,
705 static int
706 zoran_register_i2c (struct zoran *zr)
708 memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
709 sizeof(struct i2c_algo_bit_data));
710 zr->i2c_algo.data = zr;
711 strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
712 sizeof(zr->i2c_adapter.name));
713 i2c_set_adapdata(&zr->i2c_adapter, &zr->v4l2_dev);
714 zr->i2c_adapter.algo_data = &zr->i2c_algo;
715 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
716 return i2c_bit_add_bus(&zr->i2c_adapter);
719 static void
720 zoran_unregister_i2c (struct zoran *zr)
722 i2c_del_adapter(&zr->i2c_adapter);
725 /* Check a zoran_params struct for correctness, insert default params */
728 zoran_check_jpg_settings (struct zoran *zr,
729 struct zoran_jpg_settings *settings,
730 int try)
732 int err = 0, err0 = 0;
734 dprintk(4,
735 KERN_DEBUG
736 "%s: %s - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
737 ZR_DEVNAME(zr), __func__, settings->decimation, settings->HorDcm,
738 settings->VerDcm, settings->TmpDcm);
739 dprintk(4,
740 KERN_DEBUG
741 "%s: %s - x: %d, y: %d, w: %d, y: %d\n",
742 ZR_DEVNAME(zr), __func__, settings->img_x, settings->img_y,
743 settings->img_width, settings->img_height);
744 /* Check decimation, set default values for decimation = 1, 2, 4 */
745 switch (settings->decimation) {
746 case 1:
748 settings->HorDcm = 1;
749 settings->VerDcm = 1;
750 settings->TmpDcm = 1;
751 settings->field_per_buff = 2;
752 settings->img_x = 0;
753 settings->img_y = 0;
754 settings->img_width = BUZ_MAX_WIDTH;
755 settings->img_height = BUZ_MAX_HEIGHT / 2;
756 break;
757 case 2:
759 settings->HorDcm = 2;
760 settings->VerDcm = 1;
761 settings->TmpDcm = 2;
762 settings->field_per_buff = 1;
763 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
764 settings->img_y = 0;
765 settings->img_width =
766 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
767 settings->img_height = BUZ_MAX_HEIGHT / 2;
768 break;
769 case 4:
771 if (zr->card.type == DC10_new) {
772 dprintk(1,
773 KERN_DEBUG
774 "%s: %s - HDec by 4 is not supported on the DC10\n",
775 ZR_DEVNAME(zr), __func__);
776 err0++;
777 break;
780 settings->HorDcm = 4;
781 settings->VerDcm = 2;
782 settings->TmpDcm = 2;
783 settings->field_per_buff = 1;
784 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
785 settings->img_y = 0;
786 settings->img_width =
787 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
788 settings->img_height = BUZ_MAX_HEIGHT / 2;
789 break;
790 case 0:
792 /* We have to check the data the user has set */
794 if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
795 (zr->card.type == DC10_new || settings->HorDcm != 4)) {
796 settings->HorDcm = clamp(settings->HorDcm, 1, 2);
797 err0++;
799 if (settings->VerDcm != 1 && settings->VerDcm != 2) {
800 settings->VerDcm = clamp(settings->VerDcm, 1, 2);
801 err0++;
803 if (settings->TmpDcm != 1 && settings->TmpDcm != 2) {
804 settings->TmpDcm = clamp(settings->TmpDcm, 1, 2);
805 err0++;
807 if (settings->field_per_buff != 1 &&
808 settings->field_per_buff != 2) {
809 settings->field_per_buff = clamp(settings->field_per_buff, 1, 2);
810 err0++;
812 if (settings->img_x < 0) {
813 settings->img_x = 0;
814 err0++;
816 if (settings->img_y < 0) {
817 settings->img_y = 0;
818 err0++;
820 if (settings->img_width < 0 || settings->img_width > BUZ_MAX_WIDTH) {
821 settings->img_width = clamp(settings->img_width, 0, (int)BUZ_MAX_WIDTH);
822 err0++;
824 if (settings->img_height < 0 || settings->img_height > BUZ_MAX_HEIGHT / 2) {
825 settings->img_height = clamp(settings->img_height, 0, BUZ_MAX_HEIGHT / 2);
826 err0++;
828 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH) {
829 settings->img_x = BUZ_MAX_WIDTH - settings->img_width;
830 err0++;
832 if (settings->img_y + settings->img_height > BUZ_MAX_HEIGHT / 2) {
833 settings->img_y = BUZ_MAX_HEIGHT / 2 - settings->img_height;
834 err0++;
836 if (settings->img_width % (16 * settings->HorDcm) != 0) {
837 settings->img_width -= settings->img_width % (16 * settings->HorDcm);
838 if (settings->img_width == 0)
839 settings->img_width = 16 * settings->HorDcm;
840 err0++;
842 if (settings->img_height % (8 * settings->VerDcm) != 0) {
843 settings->img_height -= settings->img_height % (8 * settings->VerDcm);
844 if (settings->img_height == 0)
845 settings->img_height = 8 * settings->VerDcm;
846 err0++;
849 if (!try && err0) {
850 dprintk(1,
851 KERN_ERR
852 "%s: %s - error in params for decimation = 0\n",
853 ZR_DEVNAME(zr), __func__);
854 err++;
856 break;
857 default:
858 dprintk(1,
859 KERN_ERR
860 "%s: %s - decimation = %d, must be 0, 1, 2 or 4\n",
861 ZR_DEVNAME(zr), __func__, settings->decimation);
862 err++;
863 break;
866 if (settings->jpg_comp.quality > 100)
867 settings->jpg_comp.quality = 100;
868 if (settings->jpg_comp.quality < 5)
869 settings->jpg_comp.quality = 5;
870 if (settings->jpg_comp.APPn < 0)
871 settings->jpg_comp.APPn = 0;
872 if (settings->jpg_comp.APPn > 15)
873 settings->jpg_comp.APPn = 15;
874 if (settings->jpg_comp.APP_len < 0)
875 settings->jpg_comp.APP_len = 0;
876 if (settings->jpg_comp.APP_len > 60)
877 settings->jpg_comp.APP_len = 60;
878 if (settings->jpg_comp.COM_len < 0)
879 settings->jpg_comp.COM_len = 0;
880 if (settings->jpg_comp.COM_len > 60)
881 settings->jpg_comp.COM_len = 60;
882 if (err)
883 return -EINVAL;
884 return 0;
887 void
888 zoran_open_init_params (struct zoran *zr)
890 int i;
892 /* User must explicitly set a window */
893 zr->overlay_settings.is_set = 0;
894 zr->overlay_mask = NULL;
895 zr->overlay_active = ZORAN_FREE;
897 zr->v4l_memgrab_active = 0;
898 zr->v4l_overlay_active = 0;
899 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
900 zr->v4l_grab_seq = 0;
901 zr->v4l_settings.width = 192;
902 zr->v4l_settings.height = 144;
903 zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
904 zr->v4l_settings.bytesperline =
905 zr->v4l_settings.width *
906 ((zr->v4l_settings.format->depth + 7) / 8);
908 /* DMA ring stuff for V4L */
909 zr->v4l_pend_tail = 0;
910 zr->v4l_pend_head = 0;
911 zr->v4l_sync_tail = 0;
912 zr->v4l_buffers.active = ZORAN_FREE;
913 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
914 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
916 zr->v4l_buffers.allocated = 0;
918 for (i = 0; i < BUZ_MAX_FRAME; i++) {
919 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
921 zr->jpg_buffers.active = ZORAN_FREE;
922 zr->jpg_buffers.allocated = 0;
923 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
924 zr->jpg_settings.decimation = 1;
925 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
926 if (zr->card.type != BUZ)
927 zr->jpg_settings.odd_even = 1;
928 else
929 zr->jpg_settings.odd_even = 0;
930 zr->jpg_settings.jpg_comp.APPn = 0;
931 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
932 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
933 sizeof(zr->jpg_settings.jpg_comp.APP_data));
934 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
935 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
936 sizeof(zr->jpg_settings.jpg_comp.COM_data));
937 zr->jpg_settings.jpg_comp.jpeg_markers =
938 V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT;
939 i = zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
940 if (i)
941 dprintk(1, KERN_ERR "%s: %s internal error\n",
942 ZR_DEVNAME(zr), __func__);
944 clear_interrupt_counters(zr);
945 zr->testing = 0;
948 static void __devinit
949 test_interrupts (struct zoran *zr)
951 DEFINE_WAIT(wait);
952 int timeout, icr;
954 clear_interrupt_counters(zr);
956 zr->testing = 1;
957 icr = btread(ZR36057_ICR);
958 btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
959 prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
960 timeout = schedule_timeout(HZ);
961 finish_wait(&zr->test_q, &wait);
962 btwrite(0, ZR36057_ICR);
963 btwrite(0x78000000, ZR36057_ISR);
964 zr->testing = 0;
965 dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
966 if (timeout) {
967 dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
969 if (zr36067_debug > 1)
970 print_interrupts(zr);
971 btwrite(icr, ZR36057_ICR);
974 static int __devinit
975 zr36057_init (struct zoran *zr)
977 int j, err;
979 dprintk(1,
980 KERN_INFO
981 "%s: %s - initializing card[%d], zr=%p\n",
982 ZR_DEVNAME(zr), __func__, zr->id, zr);
984 /* default setup of all parameters which will persist between opens */
985 zr->user = 0;
987 init_waitqueue_head(&zr->v4l_capq);
988 init_waitqueue_head(&zr->jpg_capq);
989 init_waitqueue_head(&zr->test_q);
990 zr->jpg_buffers.allocated = 0;
991 zr->v4l_buffers.allocated = 0;
993 zr->vbuf_base = (void *) vidmem;
994 zr->vbuf_width = 0;
995 zr->vbuf_height = 0;
996 zr->vbuf_depth = 0;
997 zr->vbuf_bytesperline = 0;
999 /* Avoid nonsense settings from user for default input/norm */
1000 if (default_norm < 0 || default_norm > 2)
1001 default_norm = 0;
1002 if (default_norm == 0) {
1003 zr->norm = V4L2_STD_PAL;
1004 zr->timing = zr->card.tvn[0];
1005 } else if (default_norm == 1) {
1006 zr->norm = V4L2_STD_NTSC;
1007 zr->timing = zr->card.tvn[1];
1008 } else {
1009 zr->norm = V4L2_STD_SECAM;
1010 zr->timing = zr->card.tvn[2];
1012 if (zr->timing == NULL) {
1013 dprintk(1,
1014 KERN_WARNING
1015 "%s: %s - default TV standard not supported by hardware. PAL will be used.\n",
1016 ZR_DEVNAME(zr), __func__);
1017 zr->norm = V4L2_STD_PAL;
1018 zr->timing = zr->card.tvn[0];
1021 if (default_input > zr->card.inputs-1) {
1022 dprintk(1,
1023 KERN_WARNING
1024 "%s: default_input value %d out of range (0-%d)\n",
1025 ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
1026 default_input = 0;
1028 zr->input = default_input;
1030 /* default setup (will be repeated at every open) */
1031 zoran_open_init_params(zr);
1033 /* allocate memory *before* doing anything to the hardware
1034 * in case allocation fails */
1035 zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
1036 zr->video_dev = video_device_alloc();
1037 if (!zr->stat_com || !zr->video_dev) {
1038 dprintk(1,
1039 KERN_ERR
1040 "%s: %s - kmalloc (STAT_COM) failed\n",
1041 ZR_DEVNAME(zr), __func__);
1042 err = -ENOMEM;
1043 goto exit_free;
1045 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1046 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1050 * Now add the template and register the device unit.
1052 memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1053 zr->video_dev->parent = &zr->pci_dev->dev;
1054 strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
1055 err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
1056 if (err < 0)
1057 goto exit_free;
1058 video_set_drvdata(zr->video_dev, zr);
1060 zoran_init_hardware(zr);
1061 if (zr36067_debug > 2)
1062 detect_guest_activity(zr);
1063 test_interrupts(zr);
1064 if (!pass_through) {
1065 decoder_call(zr, video, s_stream, 0);
1066 encoder_call(zr, video, s_routing, 2, 0, 0);
1069 zr->zoran_proc = NULL;
1070 zr->initialized = 1;
1071 return 0;
1073 exit_free:
1074 kfree(zr->stat_com);
1075 kfree(zr->video_dev);
1076 return err;
1079 static void __devexit zoran_remove(struct pci_dev *pdev)
1081 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
1082 struct zoran *zr = to_zoran(v4l2_dev);
1084 if (!zr->initialized)
1085 goto exit_free;
1087 /* unregister videocodec bus */
1088 if (zr->codec) {
1089 struct videocodec_master *master = zr->codec->master_data;
1091 videocodec_detach(zr->codec);
1092 kfree(master);
1094 if (zr->vfe) {
1095 struct videocodec_master *master = zr->vfe->master_data;
1097 videocodec_detach(zr->vfe);
1098 kfree(master);
1101 /* unregister i2c bus */
1102 zoran_unregister_i2c(zr);
1103 /* disable PCI bus-mastering */
1104 zoran_set_pci_master(zr, 0);
1105 /* put chip into reset */
1106 btwrite(0, ZR36057_SPGPPCR);
1107 free_irq(zr->pci_dev->irq, zr);
1108 /* unmap and free memory */
1109 kfree(zr->stat_com);
1110 zoran_proc_cleanup(zr);
1111 iounmap(zr->zr36057_mem);
1112 pci_disable_device(zr->pci_dev);
1113 video_unregister_device(zr->video_dev);
1114 exit_free:
1115 v4l2_device_unregister(&zr->v4l2_dev);
1116 kfree(zr);
1119 void
1120 zoran_vdev_release (struct video_device *vdev)
1122 kfree(vdev);
1125 static struct videocodec_master * __devinit
1126 zoran_setup_videocodec (struct zoran *zr,
1127 int type)
1129 struct videocodec_master *m = NULL;
1131 m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1132 if (!m) {
1133 dprintk(1, KERN_ERR "%s: %s - no memory\n",
1134 ZR_DEVNAME(zr), __func__);
1135 return m;
1138 /* magic and type are unused for master struct. Makes sense only at
1139 codec structs.
1140 In the past, .type were initialized to the old V4L1 .hardware
1141 value, as VID_HARDWARE_ZR36067
1143 m->magic = 0L;
1144 m->type = 0;
1146 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1147 strlcpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1148 m->data = zr;
1150 switch (type)
1152 case CODEC_TYPE_ZR36060:
1153 m->readreg = zr36060_read;
1154 m->writereg = zr36060_write;
1155 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1156 break;
1157 case CODEC_TYPE_ZR36050:
1158 m->readreg = zr36050_read;
1159 m->writereg = zr36050_write;
1160 m->flags |= CODEC_FLAG_JPEG;
1161 break;
1162 case CODEC_TYPE_ZR36016:
1163 m->readreg = zr36016_read;
1164 m->writereg = zr36016_write;
1165 m->flags |= CODEC_FLAG_VFE;
1166 break;
1169 return m;
1172 static void zoran_subdev_notify(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1174 struct zoran *zr = to_zoran(sd->v4l2_dev);
1176 /* Bt819 needs to reset its FIFO buffer using #FRST pin and
1177 LML33 card uses GPIO(7) for that. */
1178 if (cmd == BT819_FIFO_RESET_LOW)
1179 GPIO(zr, 7, 0);
1180 else if (cmd == BT819_FIFO_RESET_HIGH)
1181 GPIO(zr, 7, 1);
1185 * Scan for a Buz card (actually for the PCI controller ZR36057),
1186 * request the irq and map the io memory
1188 static int __devinit zoran_probe(struct pci_dev *pdev,
1189 const struct pci_device_id *ent)
1191 unsigned char latency, need_latency;
1192 struct zoran *zr;
1193 int result;
1194 struct videocodec_master *master_vfe = NULL;
1195 struct videocodec_master *master_codec = NULL;
1196 int card_num;
1197 char *codec_name, *vfe_name;
1198 unsigned int nr;
1201 nr = zoran_num++;
1202 if (nr >= BUZ_MAX) {
1203 dprintk(1, KERN_ERR "%s: driver limited to %d card(s) maximum\n",
1204 ZORAN_NAME, BUZ_MAX);
1205 return -ENOENT;
1208 zr = kzalloc(sizeof(struct zoran), GFP_KERNEL);
1209 if (!zr) {
1210 dprintk(1, KERN_ERR "%s: %s - kzalloc failed\n",
1211 ZORAN_NAME, __func__);
1212 return -ENOMEM;
1214 zr->v4l2_dev.notify = zoran_subdev_notify;
1215 if (v4l2_device_register(&pdev->dev, &zr->v4l2_dev))
1216 goto zr_free_mem;
1217 zr->pci_dev = pdev;
1218 zr->id = nr;
1219 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1220 spin_lock_init(&zr->spinlock);
1221 mutex_init(&zr->resource_lock);
1222 mutex_init(&zr->other_lock);
1223 if (pci_enable_device(pdev))
1224 goto zr_unreg;
1225 zr->revision = zr->pci_dev->revision;
1227 dprintk(1,
1228 KERN_INFO
1229 "%s: Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
1230 ZR_DEVNAME(zr), zr->revision < 2 ? '5' : '6', zr->revision,
1231 zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
1232 if (zr->revision >= 2) {
1233 dprintk(1,
1234 KERN_INFO
1235 "%s: Subsystem vendor=0x%04x id=0x%04x\n",
1236 ZR_DEVNAME(zr), zr->pci_dev->subsystem_vendor,
1237 zr->pci_dev->subsystem_device);
1240 /* Use auto-detected card type? */
1241 if (card[nr] == -1) {
1242 if (zr->revision < 2) {
1243 dprintk(1,
1244 KERN_ERR
1245 "%s: No card type specified, please use the card=X module parameter\n",
1246 ZR_DEVNAME(zr));
1247 dprintk(1,
1248 KERN_ERR
1249 "%s: It is not possible to auto-detect ZR36057 based cards\n",
1250 ZR_DEVNAME(zr));
1251 goto zr_unreg;
1254 card_num = ent->driver_data;
1255 if (card_num >= NUM_CARDS) {
1256 dprintk(1,
1257 KERN_ERR
1258 "%s: Unknown card, try specifying card=X module parameter\n",
1259 ZR_DEVNAME(zr));
1260 goto zr_unreg;
1262 dprintk(3,
1263 KERN_DEBUG
1264 "%s: %s() - card %s detected\n",
1265 ZR_DEVNAME(zr), __func__, zoran_cards[card_num].name);
1266 } else {
1267 card_num = card[nr];
1268 if (card_num >= NUM_CARDS || card_num < 0) {
1269 dprintk(1,
1270 KERN_ERR
1271 "%s: User specified card type %d out of range (0 .. %d)\n",
1272 ZR_DEVNAME(zr), card_num, NUM_CARDS - 1);
1273 goto zr_unreg;
1277 /* even though we make this a non pointer and thus
1278 * theoretically allow for making changes to this struct
1279 * on a per-individual card basis at runtime, this is
1280 * strongly discouraged. This structure is intended to
1281 * keep general card information, no settings or anything */
1282 zr->card = zoran_cards[card_num];
1283 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1284 "%s[%u]", zr->card.name, zr->id);
1286 zr->zr36057_mem = pci_ioremap_bar(zr->pci_dev, 0);
1287 if (!zr->zr36057_mem) {
1288 dprintk(1, KERN_ERR "%s: %s() - ioremap failed\n",
1289 ZR_DEVNAME(zr), __func__);
1290 goto zr_unreg;
1293 result = request_irq(zr->pci_dev->irq, zoran_irq,
1294 IRQF_SHARED | IRQF_DISABLED, ZR_DEVNAME(zr), zr);
1295 if (result < 0) {
1296 if (result == -EINVAL) {
1297 dprintk(1,
1298 KERN_ERR
1299 "%s: %s - bad irq number or handler\n",
1300 ZR_DEVNAME(zr), __func__);
1301 } else if (result == -EBUSY) {
1302 dprintk(1,
1303 KERN_ERR
1304 "%s: %s - IRQ %d busy, change your PnP config in BIOS\n",
1305 ZR_DEVNAME(zr), __func__, zr->pci_dev->irq);
1306 } else {
1307 dprintk(1,
1308 KERN_ERR
1309 "%s: %s - can't assign irq, error code %d\n",
1310 ZR_DEVNAME(zr), __func__, result);
1312 goto zr_unmap;
1315 /* set PCI latency timer */
1316 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1317 &latency);
1318 need_latency = zr->revision > 1 ? 32 : 48;
1319 if (latency != need_latency) {
1320 dprintk(2, KERN_INFO "%s: Changing PCI latency from %d to %d\n",
1321 ZR_DEVNAME(zr), latency, need_latency);
1322 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1323 need_latency);
1326 zr36057_restart(zr);
1327 /* i2c */
1328 dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1329 ZR_DEVNAME(zr));
1331 if (zoran_register_i2c(zr) < 0) {
1332 dprintk(1, KERN_ERR "%s: %s - can't initialize i2c bus\n",
1333 ZR_DEVNAME(zr), __func__);
1334 goto zr_free_irq;
1337 zr->decoder = v4l2_i2c_new_subdev(&zr->v4l2_dev,
1338 &zr->i2c_adapter, zr->card.i2c_decoder,
1339 0, zr->card.addrs_decoder);
1341 if (zr->card.i2c_encoder)
1342 zr->encoder = v4l2_i2c_new_subdev(&zr->v4l2_dev,
1343 &zr->i2c_adapter, zr->card.i2c_encoder,
1344 0, zr->card.addrs_encoder);
1346 dprintk(2,
1347 KERN_INFO "%s: Initializing videocodec bus...\n",
1348 ZR_DEVNAME(zr));
1350 if (zr->card.video_codec) {
1351 codec_name = codecid_to_modulename(zr->card.video_codec);
1352 if (codec_name) {
1353 result = request_module(codec_name);
1354 if (result) {
1355 dprintk(1,
1356 KERN_ERR
1357 "%s: failed to load modules %s: %d\n",
1358 ZR_DEVNAME(zr), codec_name, result);
1362 if (zr->card.video_vfe) {
1363 vfe_name = codecid_to_modulename(zr->card.video_vfe);
1364 if (vfe_name) {
1365 result = request_module(vfe_name);
1366 if (result < 0) {
1367 dprintk(1,
1368 KERN_ERR
1369 "%s: failed to load modules %s: %d\n",
1370 ZR_DEVNAME(zr), vfe_name, result);
1375 /* reset JPEG codec */
1376 jpeg_codec_sleep(zr, 1);
1377 jpeg_codec_reset(zr);
1378 /* video bus enabled */
1379 /* display codec revision */
1380 if (zr->card.video_codec != 0) {
1381 master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1382 if (!master_codec)
1383 goto zr_unreg_i2c;
1384 zr->codec = videocodec_attach(master_codec);
1385 if (!zr->codec) {
1386 dprintk(1, KERN_ERR "%s: %s - no codec found\n",
1387 ZR_DEVNAME(zr), __func__);
1388 goto zr_free_codec;
1390 if (zr->codec->type != zr->card.video_codec) {
1391 dprintk(1, KERN_ERR "%s: %s - wrong codec\n",
1392 ZR_DEVNAME(zr), __func__);
1393 goto zr_detach_codec;
1396 if (zr->card.video_vfe != 0) {
1397 master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1398 if (!master_vfe)
1399 goto zr_detach_codec;
1400 zr->vfe = videocodec_attach(master_vfe);
1401 if (!zr->vfe) {
1402 dprintk(1, KERN_ERR "%s: %s - no VFE found\n",
1403 ZR_DEVNAME(zr), __func__);
1404 goto zr_free_vfe;
1406 if (zr->vfe->type != zr->card.video_vfe) {
1407 dprintk(1, KERN_ERR "%s: %s = wrong VFE\n",
1408 ZR_DEVNAME(zr), __func__);
1409 goto zr_detach_vfe;
1413 /* take care of Natoma chipset and a revision 1 zr36057 */
1414 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
1415 zr->jpg_buffers.need_contiguous = 1;
1416 dprintk(1, KERN_INFO
1417 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1418 ZR_DEVNAME(zr));
1421 if (zr36057_init(zr) < 0)
1422 goto zr_detach_vfe;
1424 zoran_proc_init(zr);
1426 return 0;
1428 zr_detach_vfe:
1429 videocodec_detach(zr->vfe);
1430 zr_free_vfe:
1431 kfree(master_vfe);
1432 zr_detach_codec:
1433 videocodec_detach(zr->codec);
1434 zr_free_codec:
1435 kfree(master_codec);
1436 zr_unreg_i2c:
1437 zoran_unregister_i2c(zr);
1438 zr_free_irq:
1439 btwrite(0, ZR36057_SPGPPCR);
1440 free_irq(zr->pci_dev->irq, zr);
1441 zr_unmap:
1442 iounmap(zr->zr36057_mem);
1443 zr_unreg:
1444 v4l2_device_unregister(&zr->v4l2_dev);
1445 zr_free_mem:
1446 kfree(zr);
1448 return -ENODEV;
1451 static struct pci_driver zoran_driver = {
1452 .name = "zr36067",
1453 .id_table = zr36067_pci_tbl,
1454 .probe = zoran_probe,
1455 .remove = __devexit_p(zoran_remove),
1458 static int __init zoran_init(void)
1460 int res;
1462 printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
1463 MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
1465 /* check the parameters we have been given, adjust if necessary */
1466 if (v4l_nbufs < 2)
1467 v4l_nbufs = 2;
1468 if (v4l_nbufs > VIDEO_MAX_FRAME)
1469 v4l_nbufs = VIDEO_MAX_FRAME;
1470 /* The user specfies the in KB, we want them in byte
1471 * (and page aligned) */
1472 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1473 if (v4l_bufsize < 32768)
1474 v4l_bufsize = 32768;
1475 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1476 if (v4l_bufsize > 2048 * 1024)
1477 v4l_bufsize = 2048 * 1024;
1478 if (jpg_nbufs < 4)
1479 jpg_nbufs = 4;
1480 if (jpg_nbufs > BUZ_MAX_FRAME)
1481 jpg_nbufs = BUZ_MAX_FRAME;
1482 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1483 if (jpg_bufsize < 8192)
1484 jpg_bufsize = 8192;
1485 if (jpg_bufsize > (512 * 1024))
1486 jpg_bufsize = 512 * 1024;
1487 /* Use parameter for vidmem or try to find a video card */
1488 if (vidmem) {
1489 dprintk(1,
1490 KERN_INFO
1491 "%s: Using supplied video memory base address @ 0x%lx\n",
1492 ZORAN_NAME, vidmem);
1495 /* some mainboards might not do PCI-PCI data transfer well */
1496 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
1497 dprintk(1,
1498 KERN_WARNING
1499 "%s: chipset does not support reliable PCI-PCI DMA\n",
1500 ZORAN_NAME);
1503 res = pci_register_driver(&zoran_driver);
1504 if (res) {
1505 dprintk(1,
1506 KERN_ERR
1507 "%s: Unable to register ZR36057 driver\n",
1508 ZORAN_NAME);
1509 return res;
1512 return 0;
1515 static void __exit zoran_exit(void)
1517 pci_unregister_driver(&zoran_driver);
1520 module_init(zoran_init);
1521 module_exit(zoran_exit);