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>
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>
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>
56 #include "videocodec.h"
58 #include "zoran_card.h"
59 #include "zoran_device.h"
60 #include "zoran_procfs.h"
62 extern const struct zoran_format zoran_formats[];
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");
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");
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");
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
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");
89 Default input and video norm at startup of the driver.
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)");
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)");
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)");
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)");
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>).
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.
124 v4l_bufsize has to be given in KB !
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)");
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)");
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");
148 int zr36067_debug = 1;
149 module_param_named(debug, zr36067_debug, int, 0644);
150 MODULE_PARM_DESC(debug, "Debug level (0-5)");
152 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
153 MODULE_AUTHOR("Serguei Miridonov");
154 MODULE_LICENSE("GPL");
156 static struct pci_device_id zr36067_pci_tbl[] = {
157 {PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057,
158 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
161 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
163 int zoran_num; /* number of Buzs in use */
164 struct zoran zoran[BUZ_MAX];
166 /* videocodec bus functions ZR36060 */
168 zr36060_read (struct videocodec *codec,
171 struct zoran *zr = (struct zoran *) codec->master_data->data;
174 if (post_office_wait(zr)
175 || post_office_write(zr, 0, 1, reg >> 8)
176 || post_office_write(zr, 0, 2, reg & 0xff)) {
180 data = post_office_read(zr, 0, 3) & 0xff;
185 zr36060_write (struct videocodec *codec,
189 struct zoran *zr = (struct zoran *) codec->master_data->data;
191 if (post_office_wait(zr)
192 || post_office_write(zr, 0, 1, reg >> 8)
193 || post_office_write(zr, 0, 2, reg & 0xff)) {
197 post_office_write(zr, 0, 3, val & 0xff);
200 /* videocodec bus functions ZR36050 */
202 zr36050_read (struct videocodec *codec,
205 struct zoran *zr = (struct zoran *) codec->master_data->data;
208 if (post_office_wait(zr)
209 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
213 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
218 zr36050_write (struct videocodec *codec,
222 struct zoran *zr = (struct zoran *) codec->master_data->data;
224 if (post_office_wait(zr)
225 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
229 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
232 /* videocodec bus functions ZR36016 */
234 zr36016_read (struct videocodec *codec,
237 struct zoran *zr = (struct zoran *) codec->master_data->data;
240 if (post_office_wait(zr)) {
244 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
248 /* hack for in zoran_device.c */
250 zr36016_write (struct videocodec *codec,
254 struct zoran *zr = (struct zoran *) codec->master_data->data;
256 if (post_office_wait(zr)) {
260 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
264 * Board specific information
268 dc10_init (struct zoran *zr)
270 dprintk(3, KERN_DEBUG "%s: dc10_init()\n", ZR_DEVNAME(zr));
272 /* Pixel clock selection */
275 /* Enable the video bus sync signals */
280 dc10plus_init (struct zoran *zr)
282 dprintk(3, KERN_DEBUG "%s: dc10plus_init()\n", ZR_DEVNAME(zr));
286 buz_init (struct zoran *zr)
288 dprintk(3, KERN_DEBUG "%s: buz_init()\n", ZR_DEVNAME(zr));
290 /* some stuff from Iomega */
291 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
292 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
293 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
297 lml33_init (struct zoran *zr)
299 dprintk(3, KERN_DEBUG "%s: lml33_init()\n", ZR_DEVNAME(zr));
301 GPIO(zr, 2, 1); // Set Composite input/output
305 avs6eyes_init (struct zoran *zr)
307 // AverMedia 6-Eyes original driver by Christer Weinigel
309 // Lifted straight from Christer's old driver and
310 // modified slightly by Martin Samuelsson.
312 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
314 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
317 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
318 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
319 GPIO(zr, 2, mux & 1); /* MUX S0 */
320 GPIO(zr, 3, 0); /* /FRAME on */
321 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
322 GPIO(zr, 5, mux & 2); /* MUX S1 */
323 GPIO(zr, 6, 0); /* ? */
324 GPIO(zr, 7, mux & 4); /* MUX S2 */
329 i2cid_to_modulename (u16 i2c_id)
334 case I2C_DRIVERID_SAA7110:
337 case I2C_DRIVERID_SAA7111A:
340 case I2C_DRIVERID_SAA7114:
343 case I2C_DRIVERID_SAA7185B:
346 case I2C_DRIVERID_ADV7170:
349 case I2C_DRIVERID_ADV7175:
352 case I2C_DRIVERID_BT819:
355 case I2C_DRIVERID_BT856:
358 case I2C_DRIVERID_VPX3220:
367 codecid_to_modulename (u16 codecid)
372 case CODEC_TYPE_ZR36060:
375 case CODEC_TYPE_ZR36050:
378 case CODEC_TYPE_ZR36016:
387 // u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
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 };
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 };
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 };
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 };
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.
411 * Christer's driver used the unshifted norms, though...
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 };
416 static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
420 .i2c_decoder = I2C_DRIVERID_VPX3220,
421 .video_codec = CODEC_TYPE_ZR36050,
422 .video_vfe = CODEC_TYPE_ZR36016,
428 { 0, "Internal/comp" }
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 },
441 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
442 .gws_not_connected = 0,
448 .i2c_decoder = I2C_DRIVERID_SAA7110,
449 .i2c_encoder = I2C_DRIVERID_ADV7175,
450 .video_codec = CODEC_TYPE_ZR36060,
456 { 5, "Internal/comp" }
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 },
468 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
469 .gws_not_connected = 0,
471 .init = &dc10plus_init,
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,
485 { 5, "Internal/comp" }
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 },
498 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
499 .gws_not_connected = 0,
501 .init = &dc10plus_init,
505 .i2c_decoder = I2C_DRIVERID_VPX3220,
506 .i2c_encoder = I2C_DRIVERID_ADV7175,
507 .video_codec = CODEC_TYPE_ZR36050,
508 .video_vfe = CODEC_TYPE_ZR36016,
514 { 0, "Internal/comp" }
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 },
527 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
528 .gws_not_connected = 0,
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,
545 { 0, "Internal/comp" }
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 },
558 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
559 .gws_not_connected = 0,
565 .i2c_decoder = I2C_DRIVERID_BT819,
566 .i2c_encoder = I2C_DRIVERID_BT856,
567 .video_codec = CODEC_TYPE_ZR36060,
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 },
585 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
586 .gws_not_connected = 1,
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,
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 },
614 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
615 .gws_not_connected = 1,
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,
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 },
643 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
644 .gws_not_connected = 1,
650 /* AverMedia chose not to brand the 6-Eyes. Thus it
651 can't be autodetected, and requires card=x. */
654 .i2c_decoder = I2C_DRIVERID_KS0127,
655 .i2c_encoder = I2C_DRIVERID_BT866,
656 .video_codec = CODEC_TYPE_ZR36060,
660 { 0, "Composite 1" },
661 { 1, "Composite 2" },
662 { 2, "Composite 3" },
663 { 4, "Composite 4" },
664 { 5, "Composite 5" },
665 { 6, "Composite 6" },
673 &f50ccir601_avs6eyes,
674 &f60ccir601_avs6eyes,
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,
685 .init = &avs6eyes_init,
693 /* software I2C functions */
695 zoran_i2c_getsda (void *data)
697 struct zoran *zr = (struct zoran *) data;
699 return (btread(ZR36057_I2CBR) >> 1) & 1;
703 zoran_i2c_getscl (void *data)
705 struct zoran *zr = (struct zoran *) data;
707 return btread(ZR36057_I2CBR) & 1;
711 zoran_i2c_setsda (void *data,
714 struct zoran *zr = (struct zoran *) data;
720 btwrite(zr->i2cbr, ZR36057_I2CBR);
724 zoran_i2c_setscl (void *data,
727 struct zoran *zr = (struct zoran *) data;
733 btwrite(zr->i2cbr, ZR36057_I2CBR);
737 zoran_i2c_client_register (struct i2c_client *client)
739 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
743 KERN_DEBUG "%s: i2c_client_register() - driver id = %d\n",
744 ZR_DEVNAME(zr), client->driver->id);
746 mutex_lock(&zr->resource_lock);
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?)
754 goto clientreg_unlock_and_return;
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;
763 goto clientreg_unlock_and_return;
766 clientreg_unlock_and_return:
767 mutex_unlock(&zr->resource_lock);
773 zoran_i2c_client_unregister (struct i2c_client *client)
775 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
778 dprintk(2, KERN_DEBUG "%s: i2c_client_unregister()\n", ZR_DEVNAME(zr));
780 mutex_lock(&zr->resource_lock);
784 goto clientunreg_unlock_and_return;
787 /* try to locate it */
788 if (client == zr->encoder) {
790 } else if (client == zr->decoder) {
792 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%d]", zr->id);
794 clientunreg_unlock_and_return:
795 mutex_unlock(&zr->resource_lock);
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,
809 zoran_register_i2c (struct zoran *zr)
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.id = I2C_HW_B_ZR36067;
815 zr->i2c_adapter.client_register = zoran_i2c_client_register;
816 zr->i2c_adapter.client_unregister = zoran_i2c_client_unregister;
817 strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
818 sizeof(zr->i2c_adapter.name));
819 i2c_set_adapdata(&zr->i2c_adapter, zr);
820 zr->i2c_adapter.algo_data = &zr->i2c_algo;
821 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
822 return i2c_bit_add_bus(&zr->i2c_adapter);
826 zoran_unregister_i2c (struct zoran *zr)
828 i2c_del_adapter(&zr->i2c_adapter);
831 /* Check a zoran_params struct for correctness, insert default params */
834 zoran_check_jpg_settings (struct zoran *zr,
835 struct zoran_jpg_settings *settings)
837 int err = 0, err0 = 0;
841 "%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
842 ZR_DEVNAME(zr), settings->decimation, settings->HorDcm,
843 settings->VerDcm, settings->TmpDcm);
846 "%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
847 ZR_DEVNAME(zr), settings->img_x, settings->img_y,
848 settings->img_width, settings->img_height);
849 /* Check decimation, set default values for decimation = 1, 2, 4 */
850 switch (settings->decimation) {
853 settings->HorDcm = 1;
854 settings->VerDcm = 1;
855 settings->TmpDcm = 1;
856 settings->field_per_buff = 2;
859 settings->img_width = BUZ_MAX_WIDTH;
860 settings->img_height = BUZ_MAX_HEIGHT / 2;
864 settings->HorDcm = 2;
865 settings->VerDcm = 1;
866 settings->TmpDcm = 2;
867 settings->field_per_buff = 1;
868 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
870 settings->img_width =
871 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
872 settings->img_height = BUZ_MAX_HEIGHT / 2;
876 if (zr->card.type == DC10_new) {
879 "%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
885 settings->HorDcm = 4;
886 settings->VerDcm = 2;
887 settings->TmpDcm = 2;
888 settings->field_per_buff = 1;
889 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
891 settings->img_width =
892 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
893 settings->img_height = BUZ_MAX_HEIGHT / 2;
897 /* We have to check the data the user has set */
899 if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
900 (zr->card.type == DC10_new || settings->HorDcm != 4))
902 if (settings->VerDcm != 1 && settings->VerDcm != 2)
904 if (settings->TmpDcm != 1 && settings->TmpDcm != 2)
906 if (settings->field_per_buff != 1 &&
907 settings->field_per_buff != 2)
909 if (settings->img_x < 0)
911 if (settings->img_y < 0)
913 if (settings->img_width < 0)
915 if (settings->img_height < 0)
917 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH)
919 if (settings->img_y + settings->img_height >
922 if (settings->HorDcm && settings->VerDcm) {
923 if (settings->img_width %
924 (16 * settings->HorDcm) != 0)
926 if (settings->img_height %
927 (8 * settings->VerDcm) != 0)
934 "%s: check_jpg_settings() - error in params for decimation = 0\n",
942 "%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
943 ZR_DEVNAME(zr), settings->decimation);
948 if (settings->jpg_comp.quality > 100)
949 settings->jpg_comp.quality = 100;
950 if (settings->jpg_comp.quality < 5)
951 settings->jpg_comp.quality = 5;
952 if (settings->jpg_comp.APPn < 0)
953 settings->jpg_comp.APPn = 0;
954 if (settings->jpg_comp.APPn > 15)
955 settings->jpg_comp.APPn = 15;
956 if (settings->jpg_comp.APP_len < 0)
957 settings->jpg_comp.APP_len = 0;
958 if (settings->jpg_comp.APP_len > 60)
959 settings->jpg_comp.APP_len = 60;
960 if (settings->jpg_comp.COM_len < 0)
961 settings->jpg_comp.COM_len = 0;
962 if (settings->jpg_comp.COM_len > 60)
963 settings->jpg_comp.COM_len = 60;
970 zoran_open_init_params (struct zoran *zr)
974 /* User must explicitly set a window */
975 zr->overlay_settings.is_set = 0;
976 zr->overlay_mask = NULL;
977 zr->overlay_active = ZORAN_FREE;
979 zr->v4l_memgrab_active = 0;
980 zr->v4l_overlay_active = 0;
981 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
982 zr->v4l_grab_seq = 0;
983 zr->v4l_settings.width = 192;
984 zr->v4l_settings.height = 144;
985 zr->v4l_settings.format = &zoran_formats[4]; /* YUY2 - YUV-4:2:2 packed */
986 zr->v4l_settings.bytesperline =
987 zr->v4l_settings.width *
988 ((zr->v4l_settings.format->depth + 7) / 8);
990 /* DMA ring stuff for V4L */
991 zr->v4l_pend_tail = 0;
992 zr->v4l_pend_head = 0;
993 zr->v4l_sync_tail = 0;
994 zr->v4l_buffers.active = ZORAN_FREE;
995 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
996 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
998 zr->v4l_buffers.allocated = 0;
1000 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1001 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1003 zr->jpg_buffers.active = ZORAN_FREE;
1004 zr->jpg_buffers.allocated = 0;
1005 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
1006 zr->jpg_settings.decimation = 1;
1007 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
1008 if (zr->card.type != BUZ)
1009 zr->jpg_settings.odd_even = 1;
1011 zr->jpg_settings.odd_even = 0;
1012 zr->jpg_settings.jpg_comp.APPn = 0;
1013 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
1014 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
1015 sizeof(zr->jpg_settings.jpg_comp.APP_data));
1016 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
1017 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
1018 sizeof(zr->jpg_settings.jpg_comp.COM_data));
1019 zr->jpg_settings.jpg_comp.jpeg_markers =
1020 JPEG_MARKER_DHT | JPEG_MARKER_DQT;
1021 i = zoran_check_jpg_settings(zr, &zr->jpg_settings);
1025 "%s: zoran_open_init_params() internal error\n",
1028 clear_interrupt_counters(zr);
1032 static void __devinit
1033 test_interrupts (struct zoran *zr)
1038 clear_interrupt_counters(zr);
1041 icr = btread(ZR36057_ICR);
1042 btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
1043 prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
1044 timeout = schedule_timeout(HZ);
1045 finish_wait(&zr->test_q, &wait);
1046 btwrite(0, ZR36057_ICR);
1047 btwrite(0x78000000, ZR36057_ISR);
1049 dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
1051 dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
1053 if (zr36067_debug > 1)
1054 print_interrupts(zr);
1055 btwrite(icr, ZR36057_ICR);
1058 static int __devinit
1059 zr36057_init (struct zoran *zr)
1067 "%s: zr36057_init() - initializing card[%d], zr=%p\n",
1068 ZR_DEVNAME(zr), zr->id, zr);
1070 /* default setup of all parameters which will persist between opens */
1073 init_waitqueue_head(&zr->v4l_capq);
1074 init_waitqueue_head(&zr->jpg_capq);
1075 init_waitqueue_head(&zr->test_q);
1076 zr->jpg_buffers.allocated = 0;
1077 zr->v4l_buffers.allocated = 0;
1079 zr->buffer.base = (void *) vidmem;
1080 zr->buffer.width = 0;
1081 zr->buffer.height = 0;
1082 zr->buffer.depth = 0;
1083 zr->buffer.bytesperline = 0;
1085 /* Avoid nonsense settings from user for default input/norm */
1086 if (default_norm < VIDEO_MODE_PAL &&
1087 default_norm > VIDEO_MODE_SECAM)
1088 default_norm = VIDEO_MODE_PAL;
1089 zr->norm = default_norm;
1090 if (!(zr->timing = zr->card.tvn[zr->norm])) {
1093 "%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
1095 zr->norm = VIDEO_MODE_PAL;
1096 zr->timing = zr->card.tvn[zr->norm];
1099 if (default_input > zr->card.inputs-1) {
1102 "%s: default_input value %d out of range (0-%d)\n",
1103 ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
1106 zr->input = default_input;
1108 /* Should the following be reset at every open ? */
1110 zr->contrast = 32768;
1111 zr->saturation = 32768;
1112 zr->brightness = 32768;
1114 /* default setup (will be repeated at every open) */
1115 zoran_open_init_params(zr);
1117 /* allocate memory *before* doing anything to the hardware
1118 * in case allocation fails */
1119 zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
1120 zr->video_dev = kmalloc(sizeof(struct video_device), GFP_KERNEL);
1121 if (!zr->stat_com || !zr->video_dev) {
1124 "%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
1129 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1130 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1134 * Now add the template and register the device unit.
1136 memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1137 strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
1138 err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
1140 goto exit_unregister;
1142 zoran_init_hardware(zr);
1143 if (zr36067_debug > 2)
1144 detect_guest_activity(zr);
1145 test_interrupts(zr);
1146 if (!pass_through) {
1147 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1148 encoder_command(zr, ENCODER_SET_INPUT, &two);
1151 zr->zoran_proc = NULL;
1152 zr->initialized = 1;
1156 zoran_unregister_i2c(zr);
1158 kfree(zr->stat_com);
1159 kfree(zr->video_dev);
1164 zoran_release (struct zoran *zr)
1166 if (!zr->initialized)
1168 /* unregister videocodec bus */
1170 struct videocodec_master *master = zr->codec->master_data;
1172 videocodec_detach(zr->codec);
1176 struct videocodec_master *master = zr->vfe->master_data;
1178 videocodec_detach(zr->vfe);
1182 /* unregister i2c bus */
1183 zoran_unregister_i2c(zr);
1184 /* disable PCI bus-mastering */
1185 zoran_set_pci_master(zr, 0);
1186 /* put chip into reset */
1187 btwrite(0, ZR36057_SPGPPCR);
1188 free_irq(zr->pci_dev->irq, zr);
1189 /* unmap and free memory */
1190 kfree(zr->stat_com);
1191 zoran_proc_cleanup(zr);
1192 iounmap(zr->zr36057_mem);
1193 pci_disable_device(zr->pci_dev);
1194 video_unregister_device(zr->video_dev);
1198 zoran_vdev_release (struct video_device *vdev)
1203 static struct videocodec_master * __devinit
1204 zoran_setup_videocodec (struct zoran *zr,
1207 struct videocodec_master *m = NULL;
1209 m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1213 "%s: zoran_setup_videocodec() - no memory\n",
1218 /* magic and type are unused for master struct. Makes sense only at
1220 In the past, .type were initialized to the old V4L1 .hardware
1221 value, as VID_HARDWARE_ZR36067
1226 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1227 strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1232 case CODEC_TYPE_ZR36060:
1233 m->readreg = zr36060_read;
1234 m->writereg = zr36060_write;
1235 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1237 case CODEC_TYPE_ZR36050:
1238 m->readreg = zr36050_read;
1239 m->writereg = zr36050_write;
1240 m->flags |= CODEC_FLAG_JPEG;
1242 case CODEC_TYPE_ZR36016:
1243 m->readreg = zr36016_read;
1244 m->writereg = zr36016_write;
1245 m->flags |= CODEC_FLAG_VFE;
1253 * Scan for a Buz card (actually for the PCI controller ZR36057),
1254 * request the irq and map the io memory
1256 static int __devinit
1259 unsigned char latency, need_latency;
1261 struct pci_dev *dev = NULL;
1263 struct videocodec_master *master_vfe = NULL;
1264 struct videocodec_master *master_codec = NULL;
1266 char *i2c_enc_name, *i2c_dec_name, *codec_name, *vfe_name;
1269 while (zoran_num < BUZ_MAX &&
1270 (dev = pci_get_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
1271 card_num = card[zoran_num];
1272 zr = &zoran[zoran_num];
1273 memset(zr, 0, sizeof(struct zoran)); // Just in case if previous cycle failed
1275 //zr->zr36057_mem = NULL;
1277 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1278 spin_lock_init(&zr->spinlock);
1279 mutex_init(&zr->resource_lock);
1280 if (pci_enable_device(dev))
1282 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
1283 pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
1285 if (zr->revision < 2) {
1288 "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
1289 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1292 if (card_num == -1) {
1295 "%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
1301 unsigned short ss_vendor, ss_device;
1303 ss_vendor = zr->pci_dev->subsystem_vendor;
1304 ss_device = zr->pci_dev->subsystem_device;
1307 "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
1308 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1312 "%s: subsystem vendor=0x%04x id=0x%04x\n",
1313 ZR_DEVNAME(zr), ss_vendor, ss_device);
1314 if (card_num == -1) {
1317 "%s: find_zr36057() - trying to autodetect card type\n",
1319 for (i=0;i<NUM_CARDS;i++) {
1320 if (ss_vendor == zoran_cards[i].vendor_id &&
1321 ss_device == zoran_cards[i].device_id) {
1324 "%s: find_zr36057() - card %s detected\n",
1326 zoran_cards[i].name);
1331 if (i == NUM_CARDS) {
1334 "%s: find_zr36057() - unknown card\n",
1341 if (card_num < 0 || card_num >= NUM_CARDS) {
1344 "%s: find_zr36057() - invalid cardnum %d\n",
1345 ZR_DEVNAME(zr), card_num);
1349 /* even though we make this a non pointer and thus
1350 * theoretically allow for making changes to this struct
1351 * on a per-individual card basis at runtime, this is
1352 * strongly discouraged. This structure is intended to
1353 * keep general card information, no settings or anything */
1354 zr->card = zoran_cards[card_num];
1355 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1356 "%s[%u]", zr->card.name, zr->id);
1358 zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
1359 if (!zr->zr36057_mem) {
1362 "%s: find_zr36057() - ioremap failed\n",
1367 result = request_irq(zr->pci_dev->irq,
1369 IRQF_SHARED | IRQF_DISABLED,
1373 if (result == -EINVAL) {
1376 "%s: find_zr36057() - bad irq number or handler\n",
1378 } else if (result == -EBUSY) {
1381 "%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
1382 ZR_DEVNAME(zr), zr->pci_dev->irq);
1386 "%s: find_zr36057() - can't assign irq, error code %d\n",
1387 ZR_DEVNAME(zr), result);
1392 /* set PCI latency timer */
1393 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1395 need_latency = zr->revision > 1 ? 32 : 48;
1396 if (latency != need_latency) {
1399 "%s: Changing PCI latency from %d to %d.\n",
1400 ZR_DEVNAME(zr), latency, need_latency);
1401 pci_write_config_byte(zr->pci_dev,
1406 zr36057_restart(zr);
1408 dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1412 if (decoder[zr->id] != -1) {
1413 i2c_dec_name = i2cid_to_modulename(decoder[zr->id]);
1414 zr->card.i2c_decoder = decoder[zr->id];
1415 } else if (zr->card.i2c_decoder != 0) {
1417 i2cid_to_modulename(zr->card.i2c_decoder);
1419 i2c_dec_name = NULL;
1423 if ((result = request_module(i2c_dec_name)) < 0) {
1426 "%s: failed to load module %s: %d\n",
1427 ZR_DEVNAME(zr), i2c_dec_name, result);
1432 if (encoder[zr->id] != -1) {
1433 i2c_enc_name = i2cid_to_modulename(encoder[zr->id]);
1434 zr->card.i2c_encoder = encoder[zr->id];
1435 } else if (zr->card.i2c_encoder != 0) {
1437 i2cid_to_modulename(zr->card.i2c_encoder);
1439 i2c_enc_name = NULL;
1443 if ((result = request_module(i2c_enc_name)) < 0) {
1446 "%s: failed to load module %s: %d\n",
1447 ZR_DEVNAME(zr), i2c_enc_name, result);
1451 if (zoran_register_i2c(zr) < 0) {
1454 "%s: find_zr36057() - can't initialize i2c bus\n",
1460 KERN_INFO "%s: Initializing videocodec bus...\n",
1463 if (zr->card.video_codec != 0 &&
1465 codecid_to_modulename(zr->card.video_codec)) != NULL) {
1466 if ((result = request_module(codec_name)) < 0) {
1469 "%s: failed to load modules %s: %d\n",
1470 ZR_DEVNAME(zr), codec_name, result);
1473 if (zr->card.video_vfe != 0 &&
1475 codecid_to_modulename(zr->card.video_vfe)) != NULL) {
1476 if ((result = request_module(vfe_name)) < 0) {
1479 "%s: failed to load modules %s: %d\n",
1480 ZR_DEVNAME(zr), vfe_name, result);
1484 /* reset JPEG codec */
1485 jpeg_codec_sleep(zr, 1);
1486 jpeg_codec_reset(zr);
1487 /* video bus enabled */
1488 /* display codec revision */
1489 if (zr->card.video_codec != 0) {
1490 master_codec = zoran_setup_videocodec(zr,
1491 zr->card.video_codec);
1494 zr->codec = videocodec_attach(master_codec);
1498 "%s: find_zr36057() - no codec found\n",
1502 if (zr->codec->type != zr->card.video_codec) {
1505 "%s: find_zr36057() - wrong codec\n",
1507 goto zr_detach_codec;
1510 if (zr->card.video_vfe != 0) {
1511 master_vfe = zoran_setup_videocodec(zr,
1512 zr->card.video_vfe);
1514 goto zr_detach_codec;
1515 zr->vfe = videocodec_attach(master_vfe);
1519 "%s: find_zr36057() - no VFE found\n",
1523 if (zr->vfe->type != zr->card.video_vfe) {
1526 "%s: find_zr36057() = wrong VFE\n",
1531 /* Success so keep the pci_dev referenced */
1532 pci_dev_get(zr->pci_dev);
1538 videocodec_detach(zr->vfe);
1542 videocodec_detach(zr->codec);
1544 kfree(master_codec);
1546 zoran_unregister_i2c(zr);
1548 btwrite(0, ZR36057_SPGPPCR);
1549 free_irq(zr->pci_dev->irq, zr);
1551 iounmap(zr->zr36057_mem);
1554 if (dev) /* Clean up ref count on early exit */
1557 if (zoran_num == 0) {
1558 dprintk(1, KERN_INFO "No known MJPEG cards found.\n");
1564 init_dc10_cards (void)
1568 memset(zoran, 0, sizeof(zoran));
1569 printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
1570 MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
1572 /* Look for cards */
1573 if (find_zr36057() < 0) {
1578 dprintk(1, KERN_INFO "%s: %d card(s) found\n", ZORAN_NAME,
1580 /* check the parameters we have been given, adjust if necessary */
1583 if (v4l_nbufs > VIDEO_MAX_FRAME)
1584 v4l_nbufs = VIDEO_MAX_FRAME;
1585 /* The user specfies the in KB, we want them in byte
1586 * (and page aligned) */
1587 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1588 if (v4l_bufsize < 32768)
1589 v4l_bufsize = 32768;
1590 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1591 if (v4l_bufsize > 2048 * 1024)
1592 v4l_bufsize = 2048 * 1024;
1595 if (jpg_nbufs > BUZ_MAX_FRAME)
1596 jpg_nbufs = BUZ_MAX_FRAME;
1597 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1598 if (jpg_bufsize < 8192)
1600 if (jpg_bufsize > (512 * 1024))
1601 jpg_bufsize = 512 * 1024;
1602 /* Use parameter for vidmem or try to find a video card */
1606 "%s: Using supplied video memory base address @ 0x%lx\n",
1607 ZORAN_NAME, vidmem);
1610 /* random nonsense */
1611 dprintk(6, KERN_DEBUG "Jotti is een held!\n");
1613 /* some mainboards might not do PCI-PCI data transfer well */
1614 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
1617 "%s: chipset does not support reliable PCI-PCI DMA\n",
1621 /* take care of Natoma chipset and a revision 1 zr36057 */
1622 for (i = 0; i < zoran_num; i++) {
1623 struct zoran *zr = &zoran[i];
1625 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
1626 zr->jpg_buffers.need_contiguous = 1;
1629 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1633 if (zr36057_init(zr) < 0) {
1634 for (i = 0; i < zoran_num; i++)
1635 zoran_release(&zoran[i]);
1638 zoran_proc_init(zr);
1645 unload_dc10_cards (void)
1649 for (i = 0; i < zoran_num; i++)
1650 zoran_release(&zoran[i]);
1653 module_init(init_dc10_cards);
1654 module_exit(unload_dc10_cards);