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/config.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/vmalloc.h>
37 #include <linux/proc_fs.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-algo-bit.h>
40 #include <linux/videodev.h>
41 #include <linux/spinlock.h>
42 #include <linux/sem.h>
43 #include <linux/kmod.h>
45 #include <linux/pci.h>
46 #include <linux/interrupt.h>
47 #include <linux/video_decoder.h>
48 #include <linux/video_encoder.h>
52 #include "videocodec.h"
54 #include "zoran_card.h"
55 #include "zoran_device.h"
56 #include "zoran_procfs.h"
58 #define I2C_NAME(x) (x)->name
60 extern const struct zoran_format zoran_formats[];
62 static int card[BUZ_MAX] = { -1, -1, -1, -1 };
63 module_param_array(card, int, NULL, 0);
64 MODULE_PARM_DESC(card, "The type of card");
66 static int encoder[BUZ_MAX] = { -1, -1, -1, -1 };
67 module_param_array(encoder, int, NULL, 0);
68 MODULE_PARM_DESC(encoder, "i2c TV encoder");
70 static int decoder[BUZ_MAX] = { -1, -1, -1, -1 };
71 module_param_array(decoder, int, NULL, 0);
72 MODULE_PARM_DESC(decoder, "i2c TV decoder");
75 The video mem address of the video card.
76 The driver has a little database for some videocards
77 to determine it from there. If your video card is not in there
78 you have either to give it to the driver as a parameter
79 or set in in a VIDIOCSFBUF ioctl
82 static unsigned long vidmem = 0; /* Video memory base address */
83 module_param(vidmem, ulong, 0);
86 Default input and video norm at startup of the driver.
89 static int default_input = 0; /* 0=Composite, 1=S-Video */
90 module_param(default_input, int, 0);
91 MODULE_PARM_DESC(default_input,
92 "Default input (0=Composite, 1=S-Video, 2=Internal)");
94 static int default_norm = 0; /* 0=PAL, 1=NTSC 2=SECAM */
95 module_param(default_norm, int, 0);
96 MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
98 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
99 module_param(video_nr, int, 0);
100 MODULE_PARM_DESC(video_nr, "video device number");
103 Number and size of grab buffers for Video 4 Linux
104 The vast majority of applications should not need more than 2,
105 the very popular BTTV driver actually does ONLY have 2.
106 Time sensitive applications might need more, the maximum
107 is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
109 The size is set so that the maximum possible request
110 can be satisfied. Decrease it, if bigphys_area alloc'd
111 memory is low. If you don't have the bigphys_area patch,
112 set it to 128 KB. Will you allow only to grab small
113 images with V4L, but that's better than nothing.
115 v4l_bufsize has to be given in KB !
120 int v4l_bufsize = 128; /* Everybody should be able to work with this setting */
121 module_param(v4l_nbufs, int, 0);
122 MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
123 module_param(v4l_bufsize, int, 0);
124 MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
127 int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
128 module_param(jpg_nbufs, int, 0);
129 MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
130 module_param(jpg_bufsize, int, 0);
131 MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
133 int pass_through = 0; /* 1=Pass through TV signal when device is not used */
134 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
135 module_param(pass_through, int, 0);
136 MODULE_PARM_DESC(pass_through,
137 "Pass TV signal through to TV-out when idling");
139 static int debug = 1;
140 int *zr_debug = &debug;
141 module_param(debug, int, 0);
142 MODULE_PARM_DESC(debug, "Debug level (0-4)");
144 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
145 MODULE_AUTHOR("Serguei Miridonov");
146 MODULE_LICENSE("GPL");
148 static struct pci_device_id zr36067_pci_tbl[] = {
149 {PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057,
150 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
153 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
155 #define dprintk(num, format, args...) \
157 if (*zr_debug >= num) \
158 printk(format, ##args); \
161 int zoran_num; /* number of Buzs in use */
162 struct zoran zoran[BUZ_MAX];
164 /* videocodec bus functions ZR36060 */
166 zr36060_read (struct videocodec *codec,
169 struct zoran *zr = (struct zoran *) codec->master_data->data;
172 if (post_office_wait(zr)
173 || post_office_write(zr, 0, 1, reg >> 8)
174 || post_office_write(zr, 0, 2, reg & 0xff)) {
178 data = post_office_read(zr, 0, 3) & 0xff;
183 zr36060_write (struct videocodec *codec,
187 struct zoran *zr = (struct zoran *) codec->master_data->data;
189 if (post_office_wait(zr)
190 || post_office_write(zr, 0, 1, reg >> 8)
191 || post_office_write(zr, 0, 2, reg & 0xff)) {
195 post_office_write(zr, 0, 3, val & 0xff);
198 /* videocodec bus functions ZR36050 */
200 zr36050_read (struct videocodec *codec,
203 struct zoran *zr = (struct zoran *) codec->master_data->data;
206 if (post_office_wait(zr)
207 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
211 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
216 zr36050_write (struct videocodec *codec,
220 struct zoran *zr = (struct zoran *) codec->master_data->data;
222 if (post_office_wait(zr)
223 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
227 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
230 /* videocodec bus functions ZR36016 */
232 zr36016_read (struct videocodec *codec,
235 struct zoran *zr = (struct zoran *) codec->master_data->data;
238 if (post_office_wait(zr)) {
242 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
246 /* hack for in zoran_device.c */
248 zr36016_write (struct videocodec *codec,
252 struct zoran *zr = (struct zoran *) codec->master_data->data;
254 if (post_office_wait(zr)) {
258 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
262 * Board specific information
266 dc10_init (struct zoran *zr)
268 dprintk(3, KERN_DEBUG "%s: dc10_init()\n", ZR_DEVNAME(zr));
270 /* Pixel clock selection */
273 /* Enable the video bus sync signals */
278 dc10plus_init (struct zoran *zr)
280 dprintk(3, KERN_DEBUG "%s: dc10plus_init()\n", ZR_DEVNAME(zr));
284 buz_init (struct zoran *zr)
286 dprintk(3, KERN_DEBUG "%s: buz_init()\n", ZR_DEVNAME(zr));
288 /* some stuff from Iomega */
289 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
290 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
291 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
295 lml33_init (struct zoran *zr)
297 dprintk(3, KERN_DEBUG "%s: lml33_init()\n", ZR_DEVNAME(zr));
299 GPIO(zr, 2, 1); // Set Composite input/output
303 i2cid_to_modulename (u16 i2c_id)
308 case I2C_DRIVERID_SAA7110:
311 case I2C_DRIVERID_SAA7111A:
314 case I2C_DRIVERID_SAA7114:
317 case I2C_DRIVERID_SAA7185B:
320 case I2C_DRIVERID_ADV7170:
323 case I2C_DRIVERID_ADV7175:
326 case I2C_DRIVERID_BT819:
329 case I2C_DRIVERID_BT856:
332 case I2C_DRIVERID_VPX3220:
335 /* case I2C_DRIVERID_VPX3224:
338 case I2C_DRIVERID_MSE3000:
349 codecid_to_modulename (u16 codecid)
354 case CODEC_TYPE_ZR36060:
357 case CODEC_TYPE_ZR36050:
360 case CODEC_TYPE_ZR36016:
371 // u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
374 static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
375 static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
376 static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
377 static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
379 static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
380 static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
382 /* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
383 static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
384 static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
386 /* FIXME: I cannot swap U and V in saa7114, so i do one
387 * pixel left shift in zoran (75 -> 74)
388 * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
389 static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
390 static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
392 static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
396 .i2c_decoder = I2C_DRIVERID_VPX3220,
397 /*.i2c_encoder = I2C_DRIVERID_MSE3000,*/
398 .video_codec = CODEC_TYPE_ZR36050,
399 .video_vfe = CODEC_TYPE_ZR36016,
405 { 0, "Internal/comp" }
414 .vsync_int = ZR36057_ISR_GIRQ1,
415 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
416 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
418 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
419 .gws_not_connected = 0,
424 .i2c_decoder = I2C_DRIVERID_SAA7110,
425 .i2c_encoder = I2C_DRIVERID_ADV7175,
426 .video_codec = CODEC_TYPE_ZR36060,
432 { 5, "Internal/comp" }
439 .jpeg_int = ZR36057_ISR_GIRQ0,
440 .vsync_int = ZR36057_ISR_GIRQ1,
441 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
442 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
444 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
445 .gws_not_connected = 0,
446 .init = &dc10plus_init,
450 .vendor_id = PCI_VENDOR_ID_MIRO,
451 .device_id = PCI_DEVICE_ID_MIRO_DC10PLUS,
452 .i2c_decoder = I2C_DRIVERID_SAA7110,
453 .i2c_encoder = I2C_DRIVERID_ADV7175,
454 .video_codec = CODEC_TYPE_ZR36060,
460 { 5, "Internal/comp" }
468 .jpeg_int = ZR36057_ISR_GIRQ0,
469 .vsync_int = ZR36057_ISR_GIRQ1,
470 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
471 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
473 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
474 .gws_not_connected = 0,
475 .init = &dc10plus_init,
479 .i2c_decoder = I2C_DRIVERID_VPX3220,
480 .i2c_encoder = I2C_DRIVERID_ADV7175,
481 .video_codec = CODEC_TYPE_ZR36050,
482 .video_vfe = CODEC_TYPE_ZR36016,
488 { 0, "Internal/comp" }
497 .vsync_int = ZR36057_ISR_GIRQ1,
498 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
499 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
501 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
502 .gws_not_connected = 0,
507 .vendor_id = PCI_VENDOR_ID_MIRO,
508 .device_id = PCI_DEVICE_ID_MIRO_DC30PLUS,
509 .i2c_decoder = I2C_DRIVERID_VPX3220,
510 .i2c_encoder = I2C_DRIVERID_ADV7175,
511 .video_codec = CODEC_TYPE_ZR36050,
512 .video_vfe = CODEC_TYPE_ZR36016,
518 { 0, "Internal/comp" }
527 .vsync_int = ZR36057_ISR_GIRQ1,
528 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
529 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
531 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
532 .gws_not_connected = 0,
537 .i2c_decoder = I2C_DRIVERID_BT819,
538 .i2c_encoder = I2C_DRIVERID_BT856,
539 .video_codec = CODEC_TYPE_ZR36060,
552 .jpeg_int = ZR36057_ISR_GIRQ1,
553 .vsync_int = ZR36057_ISR_GIRQ0,
554 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
555 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
557 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
558 .gws_not_connected = 1,
563 .vendor_id = PCI_VENDOR_ID_ELECTRONICDESIGNGMBH,
564 .device_id = PCI_DEVICE_ID_LML_33R10,
565 .i2c_decoder = I2C_DRIVERID_SAA7114,
566 .i2c_encoder = I2C_DRIVERID_ADV7170,
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,
591 .vendor_id = PCI_VENDOR_ID_IOMEGA,
592 .device_id = PCI_DEVICE_ID_IOMEGA_BUZ,
593 .i2c_decoder = I2C_DRIVERID_SAA7111A,
594 .i2c_encoder = I2C_DRIVERID_SAA7185B,
595 .video_codec = CODEC_TYPE_ZR36060,
608 .jpeg_int = ZR36057_ISR_GIRQ1,
609 .vsync_int = ZR36057_ISR_GIRQ0,
610 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
611 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
613 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
614 .gws_not_connected = 1,
622 /* software I2C functions */
624 zoran_i2c_getsda (void *data)
626 struct zoran *zr = (struct zoran *) data;
628 return (btread(ZR36057_I2CBR) >> 1) & 1;
632 zoran_i2c_getscl (void *data)
634 struct zoran *zr = (struct zoran *) data;
636 return btread(ZR36057_I2CBR) & 1;
640 zoran_i2c_setsda (void *data,
643 struct zoran *zr = (struct zoran *) data;
649 btwrite(zr->i2cbr, ZR36057_I2CBR);
653 zoran_i2c_setscl (void *data,
656 struct zoran *zr = (struct zoran *) data;
662 btwrite(zr->i2cbr, ZR36057_I2CBR);
666 zoran_i2c_client_register (struct i2c_client *client)
668 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
672 KERN_DEBUG "%s: i2c_client_register() - driver id = %d\n",
673 ZR_DEVNAME(zr), client->driver->id);
675 down(&zr->resource_lock);
678 /* we're already busy, so we keep a reference to
679 * them... Could do a lot of stuff here, but this
680 * is easiest. (Did I ever mention I'm a lazy ass?)
683 goto clientreg_unlock_and_return;
686 if (client->driver->id == zr->card.i2c_decoder)
687 zr->decoder = client;
688 else if (client->driver->id == zr->card.i2c_encoder)
689 zr->encoder = client;
692 goto clientreg_unlock_and_return;
695 clientreg_unlock_and_return:
696 up(&zr->resource_lock);
702 zoran_i2c_client_unregister (struct i2c_client *client)
704 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
707 dprintk(2, KERN_DEBUG "%s: i2c_client_unregister()\n", ZR_DEVNAME(zr));
709 down(&zr->resource_lock);
713 goto clientunreg_unlock_and_return;
716 /* try to locate it */
717 if (client == zr->encoder) {
719 } else if (client == zr->decoder) {
721 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%d]", zr->id);
723 clientunreg_unlock_and_return:
724 up(&zr->resource_lock);
728 static struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
729 .setsda = zoran_i2c_setsda,
730 .setscl = zoran_i2c_setscl,
731 .getsda = zoran_i2c_getsda,
732 .getscl = zoran_i2c_getscl,
738 static struct i2c_adapter zoran_i2c_adapter_template = {
739 I2C_DEVNAME("zr36057"),
740 .id = I2C_HW_B_ZR36067,
742 .client_register = zoran_i2c_client_register,
743 .client_unregister = zoran_i2c_client_unregister,
747 zoran_register_i2c (struct zoran *zr)
749 memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
750 sizeof(struct i2c_algo_bit_data));
751 zr->i2c_algo.data = zr;
752 memcpy(&zr->i2c_adapter, &zoran_i2c_adapter_template,
753 sizeof(struct i2c_adapter));
754 strncpy(I2C_NAME(&zr->i2c_adapter), ZR_DEVNAME(zr),
755 sizeof(I2C_NAME(&zr->i2c_adapter)) - 1);
756 i2c_set_adapdata(&zr->i2c_adapter, zr);
757 zr->i2c_adapter.algo_data = &zr->i2c_algo;
758 return i2c_bit_add_bus(&zr->i2c_adapter);
762 zoran_unregister_i2c (struct zoran *zr)
764 i2c_bit_del_bus((&zr->i2c_adapter));
767 /* Check a zoran_params struct for correctness, insert default params */
770 zoran_check_jpg_settings (struct zoran *zr,
771 struct zoran_jpg_settings *settings)
773 int err = 0, err0 = 0;
777 "%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
778 ZR_DEVNAME(zr), settings->decimation, settings->HorDcm,
779 settings->VerDcm, settings->TmpDcm);
782 "%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
783 ZR_DEVNAME(zr), settings->img_x, settings->img_y,
784 settings->img_width, settings->img_height);
785 /* Check decimation, set default values for decimation = 1, 2, 4 */
786 switch (settings->decimation) {
789 settings->HorDcm = 1;
790 settings->VerDcm = 1;
791 settings->TmpDcm = 1;
792 settings->field_per_buff = 2;
795 settings->img_width = BUZ_MAX_WIDTH;
796 settings->img_height = BUZ_MAX_HEIGHT / 2;
800 settings->HorDcm = 2;
801 settings->VerDcm = 1;
802 settings->TmpDcm = 2;
803 settings->field_per_buff = 1;
804 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
806 settings->img_width =
807 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
808 settings->img_height = BUZ_MAX_HEIGHT / 2;
812 if (zr->card.type == DC10_new) {
815 "%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
821 settings->HorDcm = 4;
822 settings->VerDcm = 2;
823 settings->TmpDcm = 2;
824 settings->field_per_buff = 1;
825 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
827 settings->img_width =
828 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
829 settings->img_height = BUZ_MAX_HEIGHT / 2;
833 /* We have to check the data the user has set */
835 if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
836 (zr->card.type == DC10_new || settings->HorDcm != 4))
838 if (settings->VerDcm != 1 && settings->VerDcm != 2)
840 if (settings->TmpDcm != 1 && settings->TmpDcm != 2)
842 if (settings->field_per_buff != 1 &&
843 settings->field_per_buff != 2)
845 if (settings->img_x < 0)
847 if (settings->img_y < 0)
849 if (settings->img_width < 0)
851 if (settings->img_height < 0)
853 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH)
855 if (settings->img_y + settings->img_height >
858 if (settings->HorDcm && settings->VerDcm) {
859 if (settings->img_width %
860 (16 * settings->HorDcm) != 0)
862 if (settings->img_height %
863 (8 * settings->VerDcm) != 0)
870 "%s: check_jpg_settings() - error in params for decimation = 0\n",
878 "%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
879 ZR_DEVNAME(zr), settings->decimation);
884 if (settings->jpg_comp.quality > 100)
885 settings->jpg_comp.quality = 100;
886 if (settings->jpg_comp.quality < 5)
887 settings->jpg_comp.quality = 5;
888 if (settings->jpg_comp.APPn < 0)
889 settings->jpg_comp.APPn = 0;
890 if (settings->jpg_comp.APPn > 15)
891 settings->jpg_comp.APPn = 15;
892 if (settings->jpg_comp.APP_len < 0)
893 settings->jpg_comp.APP_len = 0;
894 if (settings->jpg_comp.APP_len > 60)
895 settings->jpg_comp.APP_len = 60;
896 if (settings->jpg_comp.COM_len < 0)
897 settings->jpg_comp.COM_len = 0;
898 if (settings->jpg_comp.COM_len > 60)
899 settings->jpg_comp.COM_len = 60;
906 zoran_open_init_params (struct zoran *zr)
910 /* User must explicitly set a window */
911 zr->overlay_settings.is_set = 0;
912 zr->overlay_mask = NULL;
913 zr->overlay_active = ZORAN_FREE;
915 zr->v4l_memgrab_active = 0;
916 zr->v4l_overlay_active = 0;
917 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
918 zr->v4l_grab_seq = 0;
919 zr->v4l_settings.width = 192;
920 zr->v4l_settings.height = 144;
921 zr->v4l_settings.format = &zoran_formats[4]; /* YUY2 - YUV-4:2:2 packed */
922 zr->v4l_settings.bytesperline =
923 zr->v4l_settings.width *
924 ((zr->v4l_settings.format->depth + 7) / 8);
926 /* DMA ring stuff for V4L */
927 zr->v4l_pend_tail = 0;
928 zr->v4l_pend_head = 0;
929 zr->v4l_sync_tail = 0;
930 zr->v4l_buffers.active = ZORAN_FREE;
931 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
932 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
934 zr->v4l_buffers.allocated = 0;
936 for (i = 0; i < BUZ_MAX_FRAME; i++) {
937 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
939 zr->jpg_buffers.active = ZORAN_FREE;
940 zr->jpg_buffers.allocated = 0;
941 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
942 zr->jpg_settings.decimation = 1;
943 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
944 if (zr->card.type != BUZ)
945 zr->jpg_settings.odd_even = 1;
947 zr->jpg_settings.odd_even = 0;
948 zr->jpg_settings.jpg_comp.APPn = 0;
949 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
950 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
951 sizeof(zr->jpg_settings.jpg_comp.APP_data));
952 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
953 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
954 sizeof(zr->jpg_settings.jpg_comp.COM_data));
955 zr->jpg_settings.jpg_comp.jpeg_markers =
956 JPEG_MARKER_DHT | JPEG_MARKER_DQT;
957 i = zoran_check_jpg_settings(zr, &zr->jpg_settings);
961 "%s: zoran_open_init_params() internal error\n",
964 clear_interrupt_counters(zr);
968 static void __devinit
969 test_interrupts (struct zoran *zr)
973 clear_interrupt_counters(zr);
976 icr = btread(ZR36057_ICR);
977 btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
978 timeout = interruptible_sleep_on_timeout(&zr->test_q, 1 * HZ);
979 btwrite(0, ZR36057_ICR);
980 btwrite(0x78000000, ZR36057_ISR);
982 dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
984 dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
987 print_interrupts(zr);
988 btwrite(icr, ZR36057_ICR);
992 zr36057_init (struct zoran *zr)
1003 "%s: zr36057_init() - initializing card[%d], zr=%p\n",
1004 ZR_DEVNAME(zr), zr->id, zr);
1006 /* default setup of all parameters which will persist between opens */
1009 init_waitqueue_head(&zr->v4l_capq);
1010 init_waitqueue_head(&zr->jpg_capq);
1011 init_waitqueue_head(&zr->test_q);
1012 zr->jpg_buffers.allocated = 0;
1013 zr->v4l_buffers.allocated = 0;
1015 zr->buffer.base = (void *) vidmem;
1016 zr->buffer.width = 0;
1017 zr->buffer.height = 0;
1018 zr->buffer.depth = 0;
1019 zr->buffer.bytesperline = 0;
1021 /* Avoid nonsense settings from user for default input/norm */
1022 if (default_norm < VIDEO_MODE_PAL &&
1023 default_norm > VIDEO_MODE_SECAM)
1024 default_norm = VIDEO_MODE_PAL;
1025 zr->norm = default_norm;
1026 if (!(zr->timing = zr->card.tvn[zr->norm])) {
1029 "%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
1031 zr->norm = VIDEO_MODE_PAL;
1032 zr->timing = zr->card.tvn[zr->norm];
1035 zr->input = default_input = (default_input ? 1 : 0);
1037 /* Should the following be reset at every open ? */
1039 zr->contrast = 32768;
1040 zr->saturation = 32768;
1041 zr->brightness = 32768;
1043 /* default setup (will be repeated at every open) */
1044 zoran_open_init_params(zr);
1046 /* allocate memory *before* doing anything to the hardware
1047 * in case allocation fails */
1048 mem_needed = BUZ_NUM_STAT_COM * 4;
1049 mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
1050 vdev = (void *) kmalloc(sizeof(struct video_device), GFP_KERNEL);
1051 if (!mem || !vdev) {
1054 "%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
1062 memset((void *) mem, 0, mem_needed);
1063 zr->stat_com = (u32 *) mem;
1064 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1065 zr->stat_com[j] = 1; /* mark as unavailable to zr36057 */
1069 * Now add the template and register the device unit.
1071 zr->video_dev = vdev;
1072 memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1073 strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
1074 if (video_register_device(zr->video_dev, VFL_TYPE_GRABBER,
1076 zoran_unregister_i2c(zr);
1077 kfree((void *) zr->stat_com);
1082 zoran_init_hardware(zr);
1084 detect_guest_activity(zr);
1085 test_interrupts(zr);
1086 if (!pass_through) {
1087 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1088 encoder_command(zr, ENCODER_SET_INPUT, &two);
1091 zr->zoran_proc = NULL;
1092 zr->initialized = 1;
1097 zoran_release (struct zoran *zr)
1099 if (!zr->initialized)
1101 /* unregister videocodec bus */
1103 struct videocodec_master *master = zr->codec->master_data;
1104 videocodec_detach(zr->codec);
1109 struct videocodec_master *master = zr->vfe->master_data;
1110 videocodec_detach(zr->vfe);
1115 /* unregister i2c bus */
1116 zoran_unregister_i2c(zr);
1117 /* disable PCI bus-mastering */
1118 zoran_set_pci_master(zr, 0);
1119 /* put chip into reset */
1120 btwrite(0, ZR36057_SPGPPCR);
1121 free_irq(zr->pci_dev->irq, zr);
1122 /* unmap and free memory */
1123 kfree((void *) zr->stat_com);
1124 zoran_proc_cleanup(zr);
1125 iounmap(zr->zr36057_mem);
1126 pci_disable_device(zr->pci_dev);
1127 video_unregister_device(zr->video_dev);
1131 zoran_vdev_release (struct video_device *vdev)
1136 static struct videocodec_master * __devinit
1137 zoran_setup_videocodec (struct zoran *zr,
1140 struct videocodec_master *m = NULL;
1142 m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1146 "%s: zoran_setup_videocodec() - no memory\n",
1151 m->magic = 0L; /* magic not used */
1152 m->type = VID_HARDWARE_ZR36067;
1153 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1154 strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1159 case CODEC_TYPE_ZR36060:
1160 m->readreg = zr36060_read;
1161 m->writereg = zr36060_write;
1162 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1164 case CODEC_TYPE_ZR36050:
1165 m->readreg = zr36050_read;
1166 m->writereg = zr36050_write;
1167 m->flags |= CODEC_FLAG_JPEG;
1169 case CODEC_TYPE_ZR36016:
1170 m->readreg = zr36016_read;
1171 m->writereg = zr36016_write;
1172 m->flags |= CODEC_FLAG_VFE;
1180 * Scan for a Buz card (actually for the PCI contoler ZR36057),
1181 * request the irq and map the io memory
1183 static int __devinit
1186 unsigned char latency, need_latency;
1188 struct pci_dev *dev = NULL;
1190 struct videocodec_master *master_vfe = NULL;
1191 struct videocodec_master *master_codec = NULL;
1193 char *i2c_enc_name, *i2c_dec_name, *codec_name, *vfe_name;
1196 while (zoran_num < BUZ_MAX &&
1198 pci_find_device(PCI_VENDOR_ID_ZORAN,
1199 PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
1200 card_num = card[zoran_num];
1201 zr = &zoran[zoran_num];
1202 memset(zr, 0, sizeof(struct zoran)); // Just in case if previous cycle failed
1204 //zr->zr36057_mem = NULL;
1206 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1207 spin_lock_init(&zr->spinlock);
1208 init_MUTEX(&zr->resource_lock);
1209 if (pci_enable_device(dev))
1211 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
1212 pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
1214 if (zr->revision < 2) {
1217 "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
1218 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1221 if (card_num == -1) {
1224 "%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
1230 unsigned short ss_vendor, ss_device;
1232 ss_vendor = zr->pci_dev->subsystem_vendor;
1233 ss_device = zr->pci_dev->subsystem_device;
1236 "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
1237 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1241 "%s: subsystem vendor=0x%04x id=0x%04x\n",
1242 ZR_DEVNAME(zr), ss_vendor, ss_device);
1243 if (card_num == -1) {
1246 "%s: find_zr36057() - trying to autodetect card type\n",
1248 for (i=0;i<NUM_CARDS;i++) {
1249 if (ss_vendor == zoran_cards[i].vendor_id &&
1250 ss_device == zoran_cards[i].device_id) {
1253 "%s: find_zr36057() - card %s detected\n",
1255 zoran_cards[i].name);
1260 if (i == NUM_CARDS) {
1263 "%s: find_zr36057() - unknown card\n",
1270 if (card_num < 0 || card_num >= NUM_CARDS) {
1273 "%s: find_zr36057() - invalid cardnum %d\n",
1274 ZR_DEVNAME(zr), card_num);
1278 /* even though we make this a non pointer and thus
1279 * theoretically allow for making changes to this struct
1280 * on a per-individual card basis at runtime, this is
1281 * strongly discouraged. This structure is intended to
1282 * keep general card information, no settings or anything */
1283 zr->card = zoran_cards[card_num];
1284 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1285 "%s[%u]", zr->card.name, zr->id);
1287 zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
1288 if (!zr->zr36057_mem) {
1291 "%s: find_zr36057() - ioremap failed\n",
1296 result = request_irq(zr->pci_dev->irq,
1298 SA_SHIRQ | SA_INTERRUPT,
1302 if (result == -EINVAL) {
1305 "%s: find_zr36057() - bad irq number or handler\n",
1307 } else if (result == -EBUSY) {
1310 "%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
1311 ZR_DEVNAME(zr), zr->pci_dev->irq);
1315 "%s: find_zr36057() - can't assign irq, error code %d\n",
1316 ZR_DEVNAME(zr), result);
1321 /* set PCI latency timer */
1322 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1324 need_latency = zr->revision > 1 ? 32 : 48;
1325 if (latency != need_latency) {
1328 "%s: Changing PCI latency from %d to %d.\n",
1329 ZR_DEVNAME(zr), latency, need_latency);
1330 pci_write_config_byte(zr->pci_dev,
1335 zr36057_restart(zr);
1337 dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1341 if (decoder[zr->id] != -1) {
1342 i2c_dec_name = i2cid_to_modulename(decoder[zr->id]);
1343 zr->card.i2c_decoder = decoder[zr->id];
1344 } else if (zr->card.i2c_decoder != 0) {
1346 i2cid_to_modulename(zr->card.i2c_decoder);
1348 i2c_dec_name = NULL;
1352 if ((result = request_module(i2c_dec_name)) < 0) {
1355 "%s: failed to load module %s: %d\n",
1356 ZR_DEVNAME(zr), i2c_dec_name, result);
1361 if (encoder[zr->id] != -1) {
1362 i2c_enc_name = i2cid_to_modulename(encoder[zr->id]);
1363 zr->card.i2c_encoder = encoder[zr->id];
1364 } else if (zr->card.i2c_encoder != 0) {
1366 i2cid_to_modulename(zr->card.i2c_encoder);
1368 i2c_enc_name = NULL;
1372 if ((result = request_module(i2c_enc_name)) < 0) {
1375 "%s: failed to load module %s: %d\n",
1376 ZR_DEVNAME(zr), i2c_enc_name, result);
1380 if (zoran_register_i2c(zr) < 0) {
1383 "%s: find_zr36057() - can't initialize i2c bus\n",
1389 KERN_INFO "%s: Initializing videocodec bus...\n",
1392 if (zr->card.video_codec != 0 &&
1394 codecid_to_modulename(zr->card.video_codec)) != NULL) {
1395 if ((result = request_module(codec_name)) < 0) {
1398 "%s: failed to load modules %s: %d\n",
1399 ZR_DEVNAME(zr), codec_name, result);
1402 if (zr->card.video_vfe != 0 &&
1404 codecid_to_modulename(zr->card.video_vfe)) != NULL) {
1405 if ((result = request_module(vfe_name)) < 0) {
1408 "%s: failed to load modules %s: %d\n",
1409 ZR_DEVNAME(zr), vfe_name, result);
1413 /* reset JPEG codec */
1414 jpeg_codec_sleep(zr, 1);
1415 jpeg_codec_reset(zr);
1416 /* video bus enabled */
1417 /* display codec revision */
1418 if (zr->card.video_codec != 0) {
1419 master_codec = zoran_setup_videocodec(zr,
1420 zr->card.video_codec);
1423 zr->codec = videocodec_attach(master_codec);
1427 "%s: find_zr36057() - no codec found\n",
1431 if (zr->codec->type != zr->card.video_codec) {
1434 "%s: find_zr36057() - wrong codec\n",
1436 goto zr_detach_codec;
1439 if (zr->card.video_vfe != 0) {
1440 master_vfe = zoran_setup_videocodec(zr,
1441 zr->card.video_vfe);
1443 goto zr_detach_codec;
1444 zr->vfe = videocodec_attach(master_vfe);
1448 "%s: find_zr36057() - no VFE found\n",
1452 if (zr->vfe->type != zr->card.video_vfe) {
1455 "%s: find_zr36057() = wrong VFE\n",
1466 videocodec_detach(zr->vfe);
1470 videocodec_detach(zr->codec);
1472 kfree(master_codec);
1474 zoran_unregister_i2c(zr);
1476 btwrite(0, ZR36057_SPGPPCR);
1477 free_irq(zr->pci_dev->irq, zr);
1479 iounmap(zr->zr36057_mem);
1482 if (zoran_num == 0) {
1483 dprintk(1, KERN_INFO "No known MJPEG cards found.\n");
1489 init_dc10_cards (void)
1493 memset(zoran, 0, sizeof(zoran));
1494 printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
1495 MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
1497 /* Look for cards */
1498 if (find_zr36057() < 0) {
1503 dprintk(1, KERN_INFO "%s: %d card(s) found\n", ZORAN_NAME,
1505 /* check the parameters we have been given, adjust if necessary */
1508 if (v4l_nbufs > VIDEO_MAX_FRAME)
1509 v4l_nbufs = VIDEO_MAX_FRAME;
1510 /* The user specfies the in KB, we want them in byte
1511 * (and page aligned) */
1512 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1513 if (v4l_bufsize < 32768)
1514 v4l_bufsize = 32768;
1515 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1516 if (v4l_bufsize > 2048 * 1024)
1517 v4l_bufsize = 2048 * 1024;
1520 if (jpg_nbufs > BUZ_MAX_FRAME)
1521 jpg_nbufs = BUZ_MAX_FRAME;
1522 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1523 if (jpg_bufsize < 8192)
1525 if (jpg_bufsize > (512 * 1024))
1526 jpg_bufsize = 512 * 1024;
1527 /* Use parameter for vidmem or try to find a video card */
1531 "%s: Using supplied video memory base address @ 0x%lx\n",
1532 ZORAN_NAME, vidmem);
1535 /* random nonsense */
1536 dprintk(5, KERN_DEBUG "Jotti is een held!\n");
1538 /* some mainboards might not do PCI-PCI data transfer well */
1539 if (pci_pci_problems & PCIPCI_FAIL) {
1542 "%s: chipset may not support reliable PCI-PCI DMA\n",
1546 /* take care of Natoma chipset and a revision 1 zr36057 */
1547 for (i = 0; i < zoran_num; i++) {
1548 struct zoran *zr = &zoran[i];
1550 if (pci_pci_problems & PCIPCI_NATOMA && zr->revision <= 1) {
1551 zr->jpg_buffers.need_contiguous = 1;
1554 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1558 if (zr36057_init(zr) < 0) {
1559 for (i = 0; i < zoran_num; i++)
1560 zoran_release(&zoran[i]);
1563 zoran_proc_init(zr);
1570 unload_dc10_cards (void)
1574 for (i = 0; i < zoran_num; i++)
1575 zoran_release(&zoran[i]);
1578 module_init(init_dc10_cards);
1579 module_exit(unload_dc10_cards);