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>
44 #include <linux/wait.h>
46 #include <linux/pci.h>
47 #include <linux/interrupt.h>
48 #include <linux/video_decoder.h>
49 #include <linux/video_encoder.h>
50 #include <linux/mutex.h>
54 #include "videocodec.h"
56 #include "zoran_card.h"
57 #include "zoran_device.h"
58 #include "zoran_procfs.h"
60 #define I2C_NAME(x) (x)->name
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
, 0);
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
, 0);
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
, 0);
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
= 0; /* Video memory base address */
85 module_param(vidmem
, ulong
, 0);
88 Default input and video norm at startup of the driver.
91 static int default_input
= 0; /* 0=Composite, 1=S-Video */
92 module_param(default_input
, int, 0);
93 MODULE_PARM_DESC(default_input
,
94 "Default input (0=Composite, 1=S-Video, 2=Internal)");
96 static int default_norm
= 0; /* 0=PAL, 1=NTSC 2=SECAM */
97 module_param(default_norm
, int, 0);
98 MODULE_PARM_DESC(default_norm
, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
100 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
101 module_param(video_nr
, int, 0);
102 MODULE_PARM_DESC(video_nr
, "video device number");
105 Number and size of grab buffers for Video 4 Linux
106 The vast majority of applications should not need more than 2,
107 the very popular BTTV driver actually does ONLY have 2.
108 Time sensitive applications might need more, the maximum
109 is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
111 The size is set so that the maximum possible request
112 can be satisfied. Decrease it, if bigphys_area alloc'd
113 memory is low. If you don't have the bigphys_area patch,
114 set it to 128 KB. Will you allow only to grab small
115 images with V4L, but that's better than nothing.
117 v4l_bufsize has to be given in KB !
122 int v4l_bufsize
= 128; /* Everybody should be able to work with this setting */
123 module_param(v4l_nbufs
, int, 0);
124 MODULE_PARM_DESC(v4l_nbufs
, "Maximum number of V4L buffers to use");
125 module_param(v4l_bufsize
, int, 0);
126 MODULE_PARM_DESC(v4l_bufsize
, "Maximum size per V4L buffer (in kB)");
129 int jpg_bufsize
= 512; /* max size for 100% quality full-PAL frame */
130 module_param(jpg_nbufs
, int, 0);
131 MODULE_PARM_DESC(jpg_nbufs
, "Maximum number of JPG buffers to use");
132 module_param(jpg_bufsize
, int, 0);
133 MODULE_PARM_DESC(jpg_bufsize
, "Maximum size per JPG buffer (in kB)");
135 int pass_through
= 0; /* 1=Pass through TV signal when device is not used */
136 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
137 module_param(pass_through
, int, 0);
138 MODULE_PARM_DESC(pass_through
,
139 "Pass TV signal through to TV-out when idling");
141 static int debug
= 1;
142 int *zr_debug
= &debug
;
143 module_param(debug
, int, 0);
144 MODULE_PARM_DESC(debug
, "Debug level (0-4)");
146 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
147 MODULE_AUTHOR("Serguei Miridonov");
148 MODULE_LICENSE("GPL");
150 static struct pci_device_id zr36067_pci_tbl
[] = {
151 {PCI_VENDOR_ID_ZORAN
, PCI_DEVICE_ID_ZORAN_36057
,
152 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
155 MODULE_DEVICE_TABLE(pci
, zr36067_pci_tbl
);
157 #define dprintk(num, format, args...) \
159 if (*zr_debug >= num) \
160 printk(format, ##args); \
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 i2cid_to_modulename (u16 i2c_id
)
310 case I2C_DRIVERID_SAA7110
:
313 case I2C_DRIVERID_SAA7111A
:
316 case I2C_DRIVERID_SAA7114
:
319 case I2C_DRIVERID_SAA7185B
:
322 case I2C_DRIVERID_ADV7170
:
325 case I2C_DRIVERID_ADV7175
:
328 case I2C_DRIVERID_BT819
:
331 case I2C_DRIVERID_BT856
:
334 case I2C_DRIVERID_VPX3220
:
337 /* case I2C_DRIVERID_VPX3224:
340 case I2C_DRIVERID_MSE3000:
351 codecid_to_modulename (u16 codecid
)
356 case CODEC_TYPE_ZR36060
:
359 case CODEC_TYPE_ZR36050
:
362 case CODEC_TYPE_ZR36016
:
373 // u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
376 static struct tvnorm f50sqpixel
= { 944, 768, 83, 880, 625, 576, 16 };
377 static struct tvnorm f60sqpixel
= { 780, 640, 51, 716, 525, 480, 12 };
378 static struct tvnorm f50ccir601
= { 864, 720, 75, 804, 625, 576, 18 };
379 static struct tvnorm f60ccir601
= { 858, 720, 57, 788, 525, 480, 16 };
381 static struct tvnorm f50ccir601_lml33
= { 864, 720, 75+34, 804, 625, 576, 18 };
382 static struct tvnorm f60ccir601_lml33
= { 858, 720, 57+34, 788, 525, 480, 16 };
384 /* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
385 static struct tvnorm f50sqpixel_dc10
= { 944, 768, 0, 880, 625, 576, 0 };
386 static struct tvnorm f60sqpixel_dc10
= { 780, 640, 0, 716, 525, 480, 12 };
388 /* FIXME: I cannot swap U and V in saa7114, so i do one
389 * pixel left shift in zoran (75 -> 74)
390 * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
391 static struct tvnorm f50ccir601_lm33r10
= { 864, 720, 74+54, 804, 625, 576, 18 };
392 static struct tvnorm f60ccir601_lm33r10
= { 858, 720, 56+54, 788, 525, 480, 16 };
394 static struct card_info zoran_cards
[NUM_CARDS
] __devinitdata
= {
398 .i2c_decoder
= I2C_DRIVERID_VPX3220
,
399 /*.i2c_encoder = I2C_DRIVERID_MSE3000,*/
400 .video_codec
= CODEC_TYPE_ZR36050
,
401 .video_vfe
= CODEC_TYPE_ZR36016
,
407 { 0, "Internal/comp" }
416 .vsync_int
= ZR36057_ISR_GIRQ1
,
417 .gpio
= { 2, 1, -1, 3, 7, 0, 4, 5 },
418 .gpio_pol
= { 0, 0, 0, 1, 0, 0, 0, 0 },
420 .vfe_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
421 .gws_not_connected
= 0,
426 .i2c_decoder
= I2C_DRIVERID_SAA7110
,
427 .i2c_encoder
= I2C_DRIVERID_ADV7175
,
428 .video_codec
= CODEC_TYPE_ZR36060
,
434 { 5, "Internal/comp" }
441 .jpeg_int
= ZR36057_ISR_GIRQ0
,
442 .vsync_int
= ZR36057_ISR_GIRQ1
,
443 .gpio
= { 3, 0, 6, 1, 2, -1, 4, 5 },
444 .gpio_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
446 .vfe_pol
= { 1, 1, 1, 1, 0, 0, 0, 0 },
447 .gws_not_connected
= 0,
448 .init
= &dc10plus_init
,
452 .vendor_id
= PCI_VENDOR_ID_MIRO
,
453 .device_id
= PCI_DEVICE_ID_MIRO_DC10PLUS
,
454 .i2c_decoder
= I2C_DRIVERID_SAA7110
,
455 .i2c_encoder
= I2C_DRIVERID_ADV7175
,
456 .video_codec
= CODEC_TYPE_ZR36060
,
462 { 5, "Internal/comp" }
470 .jpeg_int
= ZR36057_ISR_GIRQ0
,
471 .vsync_int
= ZR36057_ISR_GIRQ1
,
472 .gpio
= { 3, 0, 6, 1, 2, -1, 4, 5 },
473 .gpio_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
475 .vfe_pol
= { 1, 1, 1, 1, 0, 0, 0, 0 },
476 .gws_not_connected
= 0,
477 .init
= &dc10plus_init
,
481 .i2c_decoder
= I2C_DRIVERID_VPX3220
,
482 .i2c_encoder
= I2C_DRIVERID_ADV7175
,
483 .video_codec
= CODEC_TYPE_ZR36050
,
484 .video_vfe
= CODEC_TYPE_ZR36016
,
490 { 0, "Internal/comp" }
499 .vsync_int
= ZR36057_ISR_GIRQ1
,
500 .gpio
= { 2, 1, -1, 3, 7, 0, 4, 5 },
501 .gpio_pol
= { 0, 0, 0, 1, 0, 0, 0, 0 },
503 .vfe_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
504 .gws_not_connected
= 0,
509 .vendor_id
= PCI_VENDOR_ID_MIRO
,
510 .device_id
= PCI_DEVICE_ID_MIRO_DC30PLUS
,
511 .i2c_decoder
= I2C_DRIVERID_VPX3220
,
512 .i2c_encoder
= I2C_DRIVERID_ADV7175
,
513 .video_codec
= CODEC_TYPE_ZR36050
,
514 .video_vfe
= CODEC_TYPE_ZR36016
,
520 { 0, "Internal/comp" }
529 .vsync_int
= ZR36057_ISR_GIRQ1
,
530 .gpio
= { 2, 1, -1, 3, 7, 0, 4, 5 },
531 .gpio_pol
= { 0, 0, 0, 1, 0, 0, 0, 0 },
533 .vfe_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
534 .gws_not_connected
= 0,
539 .i2c_decoder
= I2C_DRIVERID_BT819
,
540 .i2c_encoder
= I2C_DRIVERID_BT856
,
541 .video_codec
= CODEC_TYPE_ZR36060
,
554 .jpeg_int
= ZR36057_ISR_GIRQ1
,
555 .vsync_int
= ZR36057_ISR_GIRQ0
,
556 .gpio
= { 1, -1, 3, 5, 7, -1, -1, -1 },
557 .gpio_pol
= { 0, 0, 0, 0, 1, 0, 0, 0 },
559 .vfe_pol
= { 1, 1, 0, 0, 0, 1, 0, 0 },
560 .gws_not_connected
= 1,
565 .vendor_id
= PCI_VENDOR_ID_ELECTRONICDESIGNGMBH
,
566 .device_id
= PCI_DEVICE_ID_LML_33R10
,
567 .i2c_decoder
= I2C_DRIVERID_SAA7114
,
568 .i2c_encoder
= I2C_DRIVERID_ADV7170
,
569 .video_codec
= CODEC_TYPE_ZR36060
,
582 .jpeg_int
= ZR36057_ISR_GIRQ1
,
583 .vsync_int
= ZR36057_ISR_GIRQ0
,
584 .gpio
= { 1, -1, 3, 5, 7, -1, -1, -1 },
585 .gpio_pol
= { 0, 0, 0, 0, 1, 0, 0, 0 },
587 .vfe_pol
= { 1, 1, 0, 0, 0, 1, 0, 0 },
588 .gws_not_connected
= 1,
593 .vendor_id
= PCI_VENDOR_ID_IOMEGA
,
594 .device_id
= PCI_DEVICE_ID_IOMEGA_BUZ
,
595 .i2c_decoder
= I2C_DRIVERID_SAA7111A
,
596 .i2c_encoder
= I2C_DRIVERID_SAA7185B
,
597 .video_codec
= CODEC_TYPE_ZR36060
,
610 .jpeg_int
= ZR36057_ISR_GIRQ1
,
611 .vsync_int
= ZR36057_ISR_GIRQ0
,
612 .gpio
= { 1, -1, 3, -1, -1, -1, -1, -1 },
613 .gpio_pol
= { 0, 0, 0, 0, 0, 0, 0, 0 },
615 .vfe_pol
= { 1, 1, 0, 0, 0, 1, 0, 0 },
616 .gws_not_connected
= 1,
624 /* software I2C functions */
626 zoran_i2c_getsda (void *data
)
628 struct zoran
*zr
= (struct zoran
*) data
;
630 return (btread(ZR36057_I2CBR
) >> 1) & 1;
634 zoran_i2c_getscl (void *data
)
636 struct zoran
*zr
= (struct zoran
*) data
;
638 return btread(ZR36057_I2CBR
) & 1;
642 zoran_i2c_setsda (void *data
,
645 struct zoran
*zr
= (struct zoran
*) data
;
651 btwrite(zr
->i2cbr
, ZR36057_I2CBR
);
655 zoran_i2c_setscl (void *data
,
658 struct zoran
*zr
= (struct zoran
*) data
;
664 btwrite(zr
->i2cbr
, ZR36057_I2CBR
);
668 zoran_i2c_client_register (struct i2c_client
*client
)
670 struct zoran
*zr
= (struct zoran
*) i2c_get_adapdata(client
->adapter
);
674 KERN_DEBUG
"%s: i2c_client_register() - driver id = %d\n",
675 ZR_DEVNAME(zr
), client
->driver
->id
);
677 mutex_lock(&zr
->resource_lock
);
680 /* we're already busy, so we keep a reference to
681 * them... Could do a lot of stuff here, but this
682 * is easiest. (Did I ever mention I'm a lazy ass?)
685 goto clientreg_unlock_and_return
;
688 if (client
->driver
->id
== zr
->card
.i2c_decoder
)
689 zr
->decoder
= client
;
690 else if (client
->driver
->id
== zr
->card
.i2c_encoder
)
691 zr
->encoder
= client
;
694 goto clientreg_unlock_and_return
;
697 clientreg_unlock_and_return
:
698 mutex_unlock(&zr
->resource_lock
);
704 zoran_i2c_client_unregister (struct i2c_client
*client
)
706 struct zoran
*zr
= (struct zoran
*) i2c_get_adapdata(client
->adapter
);
709 dprintk(2, KERN_DEBUG
"%s: i2c_client_unregister()\n", ZR_DEVNAME(zr
));
711 mutex_lock(&zr
->resource_lock
);
715 goto clientunreg_unlock_and_return
;
718 /* try to locate it */
719 if (client
== zr
->encoder
) {
721 } else if (client
== zr
->decoder
) {
723 snprintf(ZR_DEVNAME(zr
), sizeof(ZR_DEVNAME(zr
)), "MJPEG[%d]", zr
->id
);
725 clientunreg_unlock_and_return
:
726 mutex_unlock(&zr
->resource_lock
);
730 static struct i2c_algo_bit_data zoran_i2c_bit_data_template
= {
731 .setsda
= zoran_i2c_setsda
,
732 .setscl
= zoran_i2c_setscl
,
733 .getsda
= zoran_i2c_getsda
,
734 .getscl
= zoran_i2c_getscl
,
740 static struct i2c_adapter zoran_i2c_adapter_template
= {
742 .id
= I2C_HW_B_ZR36067
,
744 .client_register
= zoran_i2c_client_register
,
745 .client_unregister
= zoran_i2c_client_unregister
,
749 zoran_register_i2c (struct zoran
*zr
)
751 memcpy(&zr
->i2c_algo
, &zoran_i2c_bit_data_template
,
752 sizeof(struct i2c_algo_bit_data
));
753 zr
->i2c_algo
.data
= zr
;
754 memcpy(&zr
->i2c_adapter
, &zoran_i2c_adapter_template
,
755 sizeof(struct i2c_adapter
));
756 strncpy(I2C_NAME(&zr
->i2c_adapter
), ZR_DEVNAME(zr
),
757 sizeof(I2C_NAME(&zr
->i2c_adapter
)) - 1);
758 i2c_set_adapdata(&zr
->i2c_adapter
, zr
);
759 zr
->i2c_adapter
.algo_data
= &zr
->i2c_algo
;
760 return i2c_bit_add_bus(&zr
->i2c_adapter
);
764 zoran_unregister_i2c (struct zoran
*zr
)
766 i2c_bit_del_bus((&zr
->i2c_adapter
));
769 /* Check a zoran_params struct for correctness, insert default params */
772 zoran_check_jpg_settings (struct zoran
*zr
,
773 struct zoran_jpg_settings
*settings
)
775 int err
= 0, err0
= 0;
779 "%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
780 ZR_DEVNAME(zr
), settings
->decimation
, settings
->HorDcm
,
781 settings
->VerDcm
, settings
->TmpDcm
);
784 "%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
785 ZR_DEVNAME(zr
), settings
->img_x
, settings
->img_y
,
786 settings
->img_width
, settings
->img_height
);
787 /* Check decimation, set default values for decimation = 1, 2, 4 */
788 switch (settings
->decimation
) {
791 settings
->HorDcm
= 1;
792 settings
->VerDcm
= 1;
793 settings
->TmpDcm
= 1;
794 settings
->field_per_buff
= 2;
797 settings
->img_width
= BUZ_MAX_WIDTH
;
798 settings
->img_height
= BUZ_MAX_HEIGHT
/ 2;
802 settings
->HorDcm
= 2;
803 settings
->VerDcm
= 1;
804 settings
->TmpDcm
= 2;
805 settings
->field_per_buff
= 1;
806 settings
->img_x
= (BUZ_MAX_WIDTH
== 720) ? 8 : 0;
808 settings
->img_width
=
809 (BUZ_MAX_WIDTH
== 720) ? 704 : BUZ_MAX_WIDTH
;
810 settings
->img_height
= BUZ_MAX_HEIGHT
/ 2;
814 if (zr
->card
.type
== DC10_new
) {
817 "%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
823 settings
->HorDcm
= 4;
824 settings
->VerDcm
= 2;
825 settings
->TmpDcm
= 2;
826 settings
->field_per_buff
= 1;
827 settings
->img_x
= (BUZ_MAX_WIDTH
== 720) ? 8 : 0;
829 settings
->img_width
=
830 (BUZ_MAX_WIDTH
== 720) ? 704 : BUZ_MAX_WIDTH
;
831 settings
->img_height
= BUZ_MAX_HEIGHT
/ 2;
835 /* We have to check the data the user has set */
837 if (settings
->HorDcm
!= 1 && settings
->HorDcm
!= 2 &&
838 (zr
->card
.type
== DC10_new
|| settings
->HorDcm
!= 4))
840 if (settings
->VerDcm
!= 1 && settings
->VerDcm
!= 2)
842 if (settings
->TmpDcm
!= 1 && settings
->TmpDcm
!= 2)
844 if (settings
->field_per_buff
!= 1 &&
845 settings
->field_per_buff
!= 2)
847 if (settings
->img_x
< 0)
849 if (settings
->img_y
< 0)
851 if (settings
->img_width
< 0)
853 if (settings
->img_height
< 0)
855 if (settings
->img_x
+ settings
->img_width
> BUZ_MAX_WIDTH
)
857 if (settings
->img_y
+ settings
->img_height
>
860 if (settings
->HorDcm
&& settings
->VerDcm
) {
861 if (settings
->img_width
%
862 (16 * settings
->HorDcm
) != 0)
864 if (settings
->img_height
%
865 (8 * settings
->VerDcm
) != 0)
872 "%s: check_jpg_settings() - error in params for decimation = 0\n",
880 "%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
881 ZR_DEVNAME(zr
), settings
->decimation
);
886 if (settings
->jpg_comp
.quality
> 100)
887 settings
->jpg_comp
.quality
= 100;
888 if (settings
->jpg_comp
.quality
< 5)
889 settings
->jpg_comp
.quality
= 5;
890 if (settings
->jpg_comp
.APPn
< 0)
891 settings
->jpg_comp
.APPn
= 0;
892 if (settings
->jpg_comp
.APPn
> 15)
893 settings
->jpg_comp
.APPn
= 15;
894 if (settings
->jpg_comp
.APP_len
< 0)
895 settings
->jpg_comp
.APP_len
= 0;
896 if (settings
->jpg_comp
.APP_len
> 60)
897 settings
->jpg_comp
.APP_len
= 60;
898 if (settings
->jpg_comp
.COM_len
< 0)
899 settings
->jpg_comp
.COM_len
= 0;
900 if (settings
->jpg_comp
.COM_len
> 60)
901 settings
->jpg_comp
.COM_len
= 60;
908 zoran_open_init_params (struct zoran
*zr
)
912 /* User must explicitly set a window */
913 zr
->overlay_settings
.is_set
= 0;
914 zr
->overlay_mask
= NULL
;
915 zr
->overlay_active
= ZORAN_FREE
;
917 zr
->v4l_memgrab_active
= 0;
918 zr
->v4l_overlay_active
= 0;
919 zr
->v4l_grab_frame
= NO_GRAB_ACTIVE
;
920 zr
->v4l_grab_seq
= 0;
921 zr
->v4l_settings
.width
= 192;
922 zr
->v4l_settings
.height
= 144;
923 zr
->v4l_settings
.format
= &zoran_formats
[4]; /* YUY2 - YUV-4:2:2 packed */
924 zr
->v4l_settings
.bytesperline
=
925 zr
->v4l_settings
.width
*
926 ((zr
->v4l_settings
.format
->depth
+ 7) / 8);
928 /* DMA ring stuff for V4L */
929 zr
->v4l_pend_tail
= 0;
930 zr
->v4l_pend_head
= 0;
931 zr
->v4l_sync_tail
= 0;
932 zr
->v4l_buffers
.active
= ZORAN_FREE
;
933 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
934 zr
->v4l_buffers
.buffer
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
936 zr
->v4l_buffers
.allocated
= 0;
938 for (i
= 0; i
< BUZ_MAX_FRAME
; i
++) {
939 zr
->jpg_buffers
.buffer
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
941 zr
->jpg_buffers
.active
= ZORAN_FREE
;
942 zr
->jpg_buffers
.allocated
= 0;
943 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
944 zr
->jpg_settings
.decimation
= 1;
945 zr
->jpg_settings
.jpg_comp
.quality
= 50; /* default compression factor 8 */
946 if (zr
->card
.type
!= BUZ
)
947 zr
->jpg_settings
.odd_even
= 1;
949 zr
->jpg_settings
.odd_even
= 0;
950 zr
->jpg_settings
.jpg_comp
.APPn
= 0;
951 zr
->jpg_settings
.jpg_comp
.APP_len
= 0; /* No APPn marker */
952 memset(zr
->jpg_settings
.jpg_comp
.APP_data
, 0,
953 sizeof(zr
->jpg_settings
.jpg_comp
.APP_data
));
954 zr
->jpg_settings
.jpg_comp
.COM_len
= 0; /* No COM marker */
955 memset(zr
->jpg_settings
.jpg_comp
.COM_data
, 0,
956 sizeof(zr
->jpg_settings
.jpg_comp
.COM_data
));
957 zr
->jpg_settings
.jpg_comp
.jpeg_markers
=
958 JPEG_MARKER_DHT
| JPEG_MARKER_DQT
;
959 i
= zoran_check_jpg_settings(zr
, &zr
->jpg_settings
);
963 "%s: zoran_open_init_params() internal error\n",
966 clear_interrupt_counters(zr
);
970 static void __devinit
971 test_interrupts (struct zoran
*zr
)
976 clear_interrupt_counters(zr
);
979 icr
= btread(ZR36057_ICR
);
980 btwrite(0x78000000 | ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
981 prepare_to_wait(&zr
->test_q
, &wait
, TASK_INTERRUPTIBLE
);
982 timeout
= schedule_timeout(HZ
);
983 finish_wait(&zr
->test_q
, &wait
);
984 btwrite(0, ZR36057_ICR
);
985 btwrite(0x78000000, ZR36057_ISR
);
987 dprintk(5, KERN_INFO
"%s: Testing interrupts...\n", ZR_DEVNAME(zr
));
989 dprintk(1, ": time spent: %d\n", 1 * HZ
- timeout
);
992 print_interrupts(zr
);
993 btwrite(icr
, ZR36057_ICR
);
997 zr36057_init (struct zoran
*zr
)
1005 "%s: zr36057_init() - initializing card[%d], zr=%p\n",
1006 ZR_DEVNAME(zr
), zr
->id
, zr
);
1008 /* default setup of all parameters which will persist between opens */
1011 init_waitqueue_head(&zr
->v4l_capq
);
1012 init_waitqueue_head(&zr
->jpg_capq
);
1013 init_waitqueue_head(&zr
->test_q
);
1014 zr
->jpg_buffers
.allocated
= 0;
1015 zr
->v4l_buffers
.allocated
= 0;
1017 zr
->buffer
.base
= (void *) vidmem
;
1018 zr
->buffer
.width
= 0;
1019 zr
->buffer
.height
= 0;
1020 zr
->buffer
.depth
= 0;
1021 zr
->buffer
.bytesperline
= 0;
1023 /* Avoid nonsense settings from user for default input/norm */
1024 if (default_norm
< VIDEO_MODE_PAL
&&
1025 default_norm
> VIDEO_MODE_SECAM
)
1026 default_norm
= VIDEO_MODE_PAL
;
1027 zr
->norm
= default_norm
;
1028 if (!(zr
->timing
= zr
->card
.tvn
[zr
->norm
])) {
1031 "%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
1033 zr
->norm
= VIDEO_MODE_PAL
;
1034 zr
->timing
= zr
->card
.tvn
[zr
->norm
];
1037 zr
->input
= default_input
= (default_input
? 1 : 0);
1039 /* Should the following be reset at every open ? */
1041 zr
->contrast
= 32768;
1042 zr
->saturation
= 32768;
1043 zr
->brightness
= 32768;
1045 /* default setup (will be repeated at every open) */
1046 zoran_open_init_params(zr
);
1048 /* allocate memory *before* doing anything to the hardware
1049 * in case allocation fails */
1050 zr
->stat_com
= kzalloc(BUZ_NUM_STAT_COM
* 4, GFP_KERNEL
);
1051 zr
->video_dev
= kmalloc(sizeof(struct video_device
), GFP_KERNEL
);
1052 if (!zr
->stat_com
|| !zr
->video_dev
) {
1055 "%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
1060 for (j
= 0; j
< BUZ_NUM_STAT_COM
; j
++) {
1061 zr
->stat_com
[j
] = 1; /* mark as unavailable to zr36057 */
1065 * Now add the template and register the device unit.
1067 memcpy(zr
->video_dev
, &zoran_template
, sizeof(zoran_template
));
1068 strcpy(zr
->video_dev
->name
, ZR_DEVNAME(zr
));
1069 err
= video_register_device(zr
->video_dev
, VFL_TYPE_GRABBER
, video_nr
);
1071 goto exit_unregister
;
1073 zoran_init_hardware(zr
);
1075 detect_guest_activity(zr
);
1076 test_interrupts(zr
);
1077 if (!pass_through
) {
1078 decoder_command(zr
, DECODER_ENABLE_OUTPUT
, &zero
);
1079 encoder_command(zr
, ENCODER_SET_INPUT
, &two
);
1082 zr
->zoran_proc
= NULL
;
1083 zr
->initialized
= 1;
1087 zoran_unregister_i2c(zr
);
1089 kfree(zr
->stat_com
);
1090 kfree(zr
->video_dev
);
1095 zoran_release (struct zoran
*zr
)
1097 if (!zr
->initialized
)
1099 /* unregister videocodec bus */
1101 struct videocodec_master
*master
= zr
->codec
->master_data
;
1103 videocodec_detach(zr
->codec
);
1107 struct videocodec_master
*master
= zr
->vfe
->master_data
;
1109 videocodec_detach(zr
->vfe
);
1113 /* unregister i2c bus */
1114 zoran_unregister_i2c(zr
);
1115 /* disable PCI bus-mastering */
1116 zoran_set_pci_master(zr
, 0);
1117 /* put chip into reset */
1118 btwrite(0, ZR36057_SPGPPCR
);
1119 free_irq(zr
->pci_dev
->irq
, zr
);
1120 /* unmap and free memory */
1121 kfree(zr
->stat_com
);
1122 zoran_proc_cleanup(zr
);
1123 iounmap(zr
->zr36057_mem
);
1124 pci_disable_device(zr
->pci_dev
);
1125 video_unregister_device(zr
->video_dev
);
1129 zoran_vdev_release (struct video_device
*vdev
)
1134 static struct videocodec_master
* __devinit
1135 zoran_setup_videocodec (struct zoran
*zr
,
1138 struct videocodec_master
*m
= NULL
;
1140 m
= kmalloc(sizeof(struct videocodec_master
), GFP_KERNEL
);
1144 "%s: zoran_setup_videocodec() - no memory\n",
1149 m
->magic
= 0L; /* magic not used */
1150 m
->type
= VID_HARDWARE_ZR36067
;
1151 m
->flags
= CODEC_FLAG_ENCODER
| CODEC_FLAG_DECODER
;
1152 strncpy(m
->name
, ZR_DEVNAME(zr
), sizeof(m
->name
));
1157 case CODEC_TYPE_ZR36060
:
1158 m
->readreg
= zr36060_read
;
1159 m
->writereg
= zr36060_write
;
1160 m
->flags
|= CODEC_FLAG_JPEG
| CODEC_FLAG_VFE
;
1162 case CODEC_TYPE_ZR36050
:
1163 m
->readreg
= zr36050_read
;
1164 m
->writereg
= zr36050_write
;
1165 m
->flags
|= CODEC_FLAG_JPEG
;
1167 case CODEC_TYPE_ZR36016
:
1168 m
->readreg
= zr36016_read
;
1169 m
->writereg
= zr36016_write
;
1170 m
->flags
|= CODEC_FLAG_VFE
;
1178 * Scan for a Buz card (actually for the PCI contoler ZR36057),
1179 * request the irq and map the io memory
1181 static int __devinit
1184 unsigned char latency
, need_latency
;
1186 struct pci_dev
*dev
= NULL
;
1188 struct videocodec_master
*master_vfe
= NULL
;
1189 struct videocodec_master
*master_codec
= NULL
;
1191 char *i2c_enc_name
, *i2c_dec_name
, *codec_name
, *vfe_name
;
1194 while (zoran_num
< BUZ_MAX
&&
1196 pci_find_device(PCI_VENDOR_ID_ZORAN
,
1197 PCI_DEVICE_ID_ZORAN_36057
, dev
)) != NULL
) {
1198 card_num
= card
[zoran_num
];
1199 zr
= &zoran
[zoran_num
];
1200 memset(zr
, 0, sizeof(struct zoran
)); // Just in case if previous cycle failed
1202 //zr->zr36057_mem = NULL;
1204 snprintf(ZR_DEVNAME(zr
), sizeof(ZR_DEVNAME(zr
)), "MJPEG[%u]", zr
->id
);
1205 spin_lock_init(&zr
->spinlock
);
1206 mutex_init(&zr
->resource_lock
);
1207 if (pci_enable_device(dev
))
1209 zr
->zr36057_adr
= pci_resource_start(zr
->pci_dev
, 0);
1210 pci_read_config_byte(zr
->pci_dev
, PCI_CLASS_REVISION
,
1212 if (zr
->revision
< 2) {
1215 "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
1216 ZR_DEVNAME(zr
), zr
->revision
, zr
->pci_dev
->irq
,
1219 if (card_num
== -1) {
1222 "%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
1228 unsigned short ss_vendor
, ss_device
;
1230 ss_vendor
= zr
->pci_dev
->subsystem_vendor
;
1231 ss_device
= zr
->pci_dev
->subsystem_device
;
1234 "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
1235 ZR_DEVNAME(zr
), zr
->revision
, zr
->pci_dev
->irq
,
1239 "%s: subsystem vendor=0x%04x id=0x%04x\n",
1240 ZR_DEVNAME(zr
), ss_vendor
, ss_device
);
1241 if (card_num
== -1) {
1244 "%s: find_zr36057() - trying to autodetect card type\n",
1246 for (i
=0;i
<NUM_CARDS
;i
++) {
1247 if (ss_vendor
== zoran_cards
[i
].vendor_id
&&
1248 ss_device
== zoran_cards
[i
].device_id
) {
1251 "%s: find_zr36057() - card %s detected\n",
1253 zoran_cards
[i
].name
);
1258 if (i
== NUM_CARDS
) {
1261 "%s: find_zr36057() - unknown card\n",
1268 if (card_num
< 0 || card_num
>= NUM_CARDS
) {
1271 "%s: find_zr36057() - invalid cardnum %d\n",
1272 ZR_DEVNAME(zr
), card_num
);
1276 /* even though we make this a non pointer and thus
1277 * theoretically allow for making changes to this struct
1278 * on a per-individual card basis at runtime, this is
1279 * strongly discouraged. This structure is intended to
1280 * keep general card information, no settings or anything */
1281 zr
->card
= zoran_cards
[card_num
];
1282 snprintf(ZR_DEVNAME(zr
), sizeof(ZR_DEVNAME(zr
)),
1283 "%s[%u]", zr
->card
.name
, zr
->id
);
1285 zr
->zr36057_mem
= ioremap_nocache(zr
->zr36057_adr
, 0x1000);
1286 if (!zr
->zr36057_mem
) {
1289 "%s: find_zr36057() - ioremap failed\n",
1294 result
= request_irq(zr
->pci_dev
->irq
,
1296 SA_SHIRQ
| SA_INTERRUPT
,
1300 if (result
== -EINVAL
) {
1303 "%s: find_zr36057() - bad irq number or handler\n",
1305 } else if (result
== -EBUSY
) {
1308 "%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
1309 ZR_DEVNAME(zr
), zr
->pci_dev
->irq
);
1313 "%s: find_zr36057() - can't assign irq, error code %d\n",
1314 ZR_DEVNAME(zr
), result
);
1319 /* set PCI latency timer */
1320 pci_read_config_byte(zr
->pci_dev
, PCI_LATENCY_TIMER
,
1322 need_latency
= zr
->revision
> 1 ? 32 : 48;
1323 if (latency
!= need_latency
) {
1326 "%s: Changing PCI latency from %d to %d.\n",
1327 ZR_DEVNAME(zr
), latency
, need_latency
);
1328 pci_write_config_byte(zr
->pci_dev
,
1333 zr36057_restart(zr
);
1335 dprintk(2, KERN_INFO
"%s: Initializing i2c bus...\n",
1339 if (decoder
[zr
->id
] != -1) {
1340 i2c_dec_name
= i2cid_to_modulename(decoder
[zr
->id
]);
1341 zr
->card
.i2c_decoder
= decoder
[zr
->id
];
1342 } else if (zr
->card
.i2c_decoder
!= 0) {
1344 i2cid_to_modulename(zr
->card
.i2c_decoder
);
1346 i2c_dec_name
= NULL
;
1350 if ((result
= request_module(i2c_dec_name
)) < 0) {
1353 "%s: failed to load module %s: %d\n",
1354 ZR_DEVNAME(zr
), i2c_dec_name
, result
);
1359 if (encoder
[zr
->id
] != -1) {
1360 i2c_enc_name
= i2cid_to_modulename(encoder
[zr
->id
]);
1361 zr
->card
.i2c_encoder
= encoder
[zr
->id
];
1362 } else if (zr
->card
.i2c_encoder
!= 0) {
1364 i2cid_to_modulename(zr
->card
.i2c_encoder
);
1366 i2c_enc_name
= NULL
;
1370 if ((result
= request_module(i2c_enc_name
)) < 0) {
1373 "%s: failed to load module %s: %d\n",
1374 ZR_DEVNAME(zr
), i2c_enc_name
, result
);
1378 if (zoran_register_i2c(zr
) < 0) {
1381 "%s: find_zr36057() - can't initialize i2c bus\n",
1387 KERN_INFO
"%s: Initializing videocodec bus...\n",
1390 if (zr
->card
.video_codec
!= 0 &&
1392 codecid_to_modulename(zr
->card
.video_codec
)) != NULL
) {
1393 if ((result
= request_module(codec_name
)) < 0) {
1396 "%s: failed to load modules %s: %d\n",
1397 ZR_DEVNAME(zr
), codec_name
, result
);
1400 if (zr
->card
.video_vfe
!= 0 &&
1402 codecid_to_modulename(zr
->card
.video_vfe
)) != NULL
) {
1403 if ((result
= request_module(vfe_name
)) < 0) {
1406 "%s: failed to load modules %s: %d\n",
1407 ZR_DEVNAME(zr
), vfe_name
, result
);
1411 /* reset JPEG codec */
1412 jpeg_codec_sleep(zr
, 1);
1413 jpeg_codec_reset(zr
);
1414 /* video bus enabled */
1415 /* display codec revision */
1416 if (zr
->card
.video_codec
!= 0) {
1417 master_codec
= zoran_setup_videocodec(zr
,
1418 zr
->card
.video_codec
);
1421 zr
->codec
= videocodec_attach(master_codec
);
1425 "%s: find_zr36057() - no codec found\n",
1429 if (zr
->codec
->type
!= zr
->card
.video_codec
) {
1432 "%s: find_zr36057() - wrong codec\n",
1434 goto zr_detach_codec
;
1437 if (zr
->card
.video_vfe
!= 0) {
1438 master_vfe
= zoran_setup_videocodec(zr
,
1439 zr
->card
.video_vfe
);
1441 goto zr_detach_codec
;
1442 zr
->vfe
= videocodec_attach(master_vfe
);
1446 "%s: find_zr36057() - no VFE found\n",
1450 if (zr
->vfe
->type
!= zr
->card
.video_vfe
) {
1453 "%s: find_zr36057() = wrong VFE\n",
1464 videocodec_detach(zr
->vfe
);
1468 videocodec_detach(zr
->codec
);
1470 kfree(master_codec
);
1472 zoran_unregister_i2c(zr
);
1474 btwrite(0, ZR36057_SPGPPCR
);
1475 free_irq(zr
->pci_dev
->irq
, zr
);
1477 iounmap(zr
->zr36057_mem
);
1480 if (zoran_num
== 0) {
1481 dprintk(1, KERN_INFO
"No known MJPEG cards found.\n");
1487 init_dc10_cards (void)
1491 memset(zoran
, 0, sizeof(zoran
));
1492 printk(KERN_INFO
"Zoran MJPEG board driver version %d.%d.%d\n",
1493 MAJOR_VERSION
, MINOR_VERSION
, RELEASE_VERSION
);
1495 /* Look for cards */
1496 if (find_zr36057() < 0) {
1501 dprintk(1, KERN_INFO
"%s: %d card(s) found\n", ZORAN_NAME
,
1503 /* check the parameters we have been given, adjust if necessary */
1506 if (v4l_nbufs
> VIDEO_MAX_FRAME
)
1507 v4l_nbufs
= VIDEO_MAX_FRAME
;
1508 /* The user specfies the in KB, we want them in byte
1509 * (and page aligned) */
1510 v4l_bufsize
= PAGE_ALIGN(v4l_bufsize
* 1024);
1511 if (v4l_bufsize
< 32768)
1512 v4l_bufsize
= 32768;
1513 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1514 if (v4l_bufsize
> 2048 * 1024)
1515 v4l_bufsize
= 2048 * 1024;
1518 if (jpg_nbufs
> BUZ_MAX_FRAME
)
1519 jpg_nbufs
= BUZ_MAX_FRAME
;
1520 jpg_bufsize
= PAGE_ALIGN(jpg_bufsize
* 1024);
1521 if (jpg_bufsize
< 8192)
1523 if (jpg_bufsize
> (512 * 1024))
1524 jpg_bufsize
= 512 * 1024;
1525 /* Use parameter for vidmem or try to find a video card */
1529 "%s: Using supplied video memory base address @ 0x%lx\n",
1530 ZORAN_NAME
, vidmem
);
1533 /* random nonsense */
1534 dprintk(5, KERN_DEBUG
"Jotti is een held!\n");
1536 /* some mainboards might not do PCI-PCI data transfer well */
1537 if (pci_pci_problems
& PCIPCI_FAIL
) {
1540 "%s: chipset may not support reliable PCI-PCI DMA\n",
1544 /* take care of Natoma chipset and a revision 1 zr36057 */
1545 for (i
= 0; i
< zoran_num
; i
++) {
1546 struct zoran
*zr
= &zoran
[i
];
1548 if (pci_pci_problems
& PCIPCI_NATOMA
&& zr
->revision
<= 1) {
1549 zr
->jpg_buffers
.need_contiguous
= 1;
1552 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1556 if (zr36057_init(zr
) < 0) {
1557 for (i
= 0; i
< zoran_num
; i
++)
1558 zoran_release(&zoran
[i
]);
1561 zoran_proc_init(zr
);
1568 unload_dc10_cards (void)
1572 for (i
= 0; i
< zoran_num
; i
++)
1573 zoran_release(&zoran
[i
]);
1576 module_init(init_dc10_cards
);
1577 module_exit(unload_dc10_cards
);