V4L/DVB (10219): saa7134: Prevent Oops due to stale IRQ status when enabling interrupts
[deliverable/linux.git] / drivers / media / video / zoran / zoran_card.c
CommitLineData
1da177e4
LT
1/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * This part handles card-specific data and detection
d56410e0 7 *
1da177e4
LT
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
fbe60daa
MS
30#include <linux/delay.h>
31
1da177e4
LT
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/vmalloc.h>
37
38#include <linux/proc_fs.h>
39#include <linux/i2c.h>
40#include <linux/i2c-algo-bit.h>
41#include <linux/videodev.h>
5e87efa3 42#include <media/v4l2-common.h>
1da177e4
LT
43#include <linux/spinlock.h>
44#include <linux/sem.h>
45#include <linux/kmod.h>
46#include <linux/wait.h>
47
48#include <linux/pci.h>
49#include <linux/interrupt.h>
50#include <linux/video_decoder.h>
51#include <linux/video_encoder.h>
384c3689 52#include <linux/mutex.h>
1da177e4
LT
53
54#include <asm/io.h>
55
56#include "videocodec.h"
57#include "zoran.h"
58#include "zoran_card.h"
59#include "zoran_device.h"
60#include "zoran_procfs.h"
61
1da177e4
LT
62extern const struct zoran_format zoran_formats[];
63
64static int card[BUZ_MAX] = { -1, -1, -1, -1 };
60e3cac4 65module_param_array(card, int, NULL, 0444);
1da177e4
LT
66MODULE_PARM_DESC(card, "The type of card");
67
68static int encoder[BUZ_MAX] = { -1, -1, -1, -1 };
60e3cac4 69module_param_array(encoder, int, NULL, 0444);
1da177e4
LT
70MODULE_PARM_DESC(encoder, "i2c TV encoder");
71
72static int decoder[BUZ_MAX] = { -1, -1, -1, -1 };
60e3cac4 73module_param_array(decoder, int, NULL, 0444);
1da177e4
LT
74MODULE_PARM_DESC(decoder, "i2c TV decoder");
75
76/*
77 The video mem address of the video card.
78 The driver has a little database for some videocards
79 to determine it from there. If your video card is not in there
80 you have either to give it to the driver as a parameter
81 or set in in a VIDIOCSFBUF ioctl
82 */
83
ff699e6b 84static unsigned long vidmem; /* default = 0 - Video memory base address */
60e3cac4
TP
85module_param(vidmem, ulong, 0444);
86MODULE_PARM_DESC(vidmem, "Default video memory base address");
1da177e4
LT
87
88/*
89 Default input and video norm at startup of the driver.
90*/
91
ff699e6b 92static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
60e3cac4 93module_param(default_input, uint, 0444);
1da177e4
LT
94MODULE_PARM_DESC(default_input,
95 "Default input (0=Composite, 1=S-Video, 2=Internal)");
96
fbe60daa 97static int default_mux = 1; /* 6 Eyes input selection */
60e3cac4 98module_param(default_mux, int, 0644);
fbe60daa
MS
99MODULE_PARM_DESC(default_mux,
100 "Default 6 Eyes mux setting (Input selection)");
101
ff699e6b 102static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
60e3cac4 103module_param(default_norm, int, 0444);
1da177e4
LT
104MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
105
60e3cac4
TP
106/* /dev/videoN, -1 for autodetect */
107static int video_nr[BUZ_MAX] = {-1, -1, -1, -1};
108module_param_array(video_nr, int, NULL, 0444);
109MODULE_PARM_DESC(video_nr, "video device number (-1=Auto)");
1da177e4
LT
110
111/*
112 Number and size of grab buffers for Video 4 Linux
113 The vast majority of applications should not need more than 2,
114 the very popular BTTV driver actually does ONLY have 2.
115 Time sensitive applications might need more, the maximum
116 is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
117
118 The size is set so that the maximum possible request
119 can be satisfied. Decrease it, if bigphys_area alloc'd
120 memory is low. If you don't have the bigphys_area patch,
121 set it to 128 KB. Will you allow only to grab small
122 images with V4L, but that's better than nothing.
123
124 v4l_bufsize has to be given in KB !
125
126*/
127
128int v4l_nbufs = 2;
129int v4l_bufsize = 128; /* Everybody should be able to work with this setting */
60e3cac4 130module_param(v4l_nbufs, int, 0644);
1da177e4 131MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
60e3cac4 132module_param(v4l_bufsize, int, 0644);
1da177e4
LT
133MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
134
135int jpg_nbufs = 32;
136int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
60e3cac4 137module_param(jpg_nbufs, int, 0644);
1da177e4 138MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
60e3cac4 139module_param(jpg_bufsize, int, 0644);
1da177e4
LT
140MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
141
142int 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) */
60e3cac4 144module_param(pass_through, int, 0644);
1da177e4
LT
145MODULE_PARM_DESC(pass_through,
146 "Pass TV signal through to TV-out when idling");
147
18b548ca
JD
148int zr36067_debug = 1;
149module_param_named(debug, zr36067_debug, int, 0644);
150MODULE_PARM_DESC(debug, "Debug level (0-5)");
1da177e4
LT
151
152MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
153MODULE_AUTHOR("Serguei Miridonov");
154MODULE_LICENSE("GPL");
155
dbdf03b4 156static struct pci_device_id zr36067_pci_tbl[] = {
66aa66ea 157 { PCI_DEVICE(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057), },
dbdf03b4
MCC
158 {0}
159};
160MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
1da177e4 161
66aa66ea 162atomic_t zoran_num = ATOMIC_INIT(0); /* number of Buzs in use */
85b9b8a4 163struct zoran *zoran[BUZ_MAX];
1da177e4
LT
164
165/* videocodec bus functions ZR36060 */
166static u32
167zr36060_read (struct videocodec *codec,
168 u16 reg)
169{
170 struct zoran *zr = (struct zoran *) codec->master_data->data;
171 __u32 data;
172
173 if (post_office_wait(zr)
174 || post_office_write(zr, 0, 1, reg >> 8)
175 || post_office_write(zr, 0, 2, reg & 0xff)) {
176 return -1;
177 }
178
179 data = post_office_read(zr, 0, 3) & 0xff;
180 return data;
181}
182
183static void
184zr36060_write (struct videocodec *codec,
185 u16 reg,
186 u32 val)
187{
188 struct zoran *zr = (struct zoran *) codec->master_data->data;
189
190 if (post_office_wait(zr)
191 || post_office_write(zr, 0, 1, reg >> 8)
192 || post_office_write(zr, 0, 2, reg & 0xff)) {
193 return;
194 }
195
196 post_office_write(zr, 0, 3, val & 0xff);
197}
198
199/* videocodec bus functions ZR36050 */
200static u32
201zr36050_read (struct videocodec *codec,
202 u16 reg)
203{
204 struct zoran *zr = (struct zoran *) codec->master_data->data;
205 __u32 data;
206
207 if (post_office_wait(zr)
208 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
209 return -1;
210 }
211
212 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
213 return data;
214}
215
216static void
217zr36050_write (struct videocodec *codec,
218 u16 reg,
219 u32 val)
220{
221 struct zoran *zr = (struct zoran *) codec->master_data->data;
222
223 if (post_office_wait(zr)
224 || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
225 return;
226 }
227
228 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
229}
230
231/* videocodec bus functions ZR36016 */
232static u32
233zr36016_read (struct videocodec *codec,
234 u16 reg)
235{
236 struct zoran *zr = (struct zoran *) codec->master_data->data;
237 __u32 data;
238
239 if (post_office_wait(zr)) {
240 return -1;
241 }
242
243 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
244 return data;
245}
246
247/* hack for in zoran_device.c */
248void
249zr36016_write (struct videocodec *codec,
250 u16 reg,
251 u32 val)
252{
253 struct zoran *zr = (struct zoran *) codec->master_data->data;
254
255 if (post_office_wait(zr)) {
256 return;
257 }
258
259 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
260}
261
262/*
263 * Board specific information
264 */
265
266static void
267dc10_init (struct zoran *zr)
268{
269 dprintk(3, KERN_DEBUG "%s: dc10_init()\n", ZR_DEVNAME(zr));
270
271 /* Pixel clock selection */
272 GPIO(zr, 4, 0);
273 GPIO(zr, 5, 1);
274 /* Enable the video bus sync signals */
275 GPIO(zr, 7, 0);
276}
277
278static void
279dc10plus_init (struct zoran *zr)
280{
281 dprintk(3, KERN_DEBUG "%s: dc10plus_init()\n", ZR_DEVNAME(zr));
282}
283
284static void
285buz_init (struct zoran *zr)
286{
287 dprintk(3, KERN_DEBUG "%s: buz_init()\n", ZR_DEVNAME(zr));
288
289 /* some stuff from Iomega */
290 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
291 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
292 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
293}
294
295static void
296lml33_init (struct zoran *zr)
297{
298 dprintk(3, KERN_DEBUG "%s: lml33_init()\n", ZR_DEVNAME(zr));
299
300 GPIO(zr, 2, 1); // Set Composite input/output
301}
302
fbe60daa
MS
303static void
304avs6eyes_init (struct zoran *zr)
305{
306 // AverMedia 6-Eyes original driver by Christer Weinigel
307
308 // Lifted straight from Christer's old driver and
309 // modified slightly by Martin Samuelsson.
310
311 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
312
313 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
314 udelay(2);
315
316 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
317 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
318 GPIO(zr, 2, mux & 1); /* MUX S0 */
319 GPIO(zr, 3, 0); /* /FRAME on */
320 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
321 GPIO(zr, 5, mux & 2); /* MUX S1 */
322 GPIO(zr, 6, 0); /* ? */
323 GPIO(zr, 7, mux & 4); /* MUX S2 */
324
325}
326
1da177e4
LT
327static char *
328i2cid_to_modulename (u16 i2c_id)
329{
330 char *name = NULL;
331
332 switch (i2c_id) {
333 case I2C_DRIVERID_SAA7110:
334 name = "saa7110";
335 break;
336 case I2C_DRIVERID_SAA7111A:
337 name = "saa7111";
338 break;
339 case I2C_DRIVERID_SAA7114:
340 name = "saa7114";
341 break;
342 case I2C_DRIVERID_SAA7185B:
343 name = "saa7185";
344 break;
345 case I2C_DRIVERID_ADV7170:
346 name = "adv7170";
347 break;
348 case I2C_DRIVERID_ADV7175:
349 name = "adv7175";
350 break;
351 case I2C_DRIVERID_BT819:
352 name = "bt819";
353 break;
354 case I2C_DRIVERID_BT856:
355 name = "bt856";
356 break;
ed1aedb1
MS
357 case I2C_DRIVERID_BT866:
358 name = "bt866";
359 break;
1da177e4
LT
360 case I2C_DRIVERID_VPX3220:
361 name = "vpx3220";
362 break;
ed1aedb1
MS
363 case I2C_DRIVERID_KS0127:
364 name = "ks0127";
365 break;
1da177e4
LT
366 }
367
368 return name;
369}
370
371static char *
372codecid_to_modulename (u16 codecid)
373{
374 char *name = NULL;
375
376 switch (codecid) {
377 case CODEC_TYPE_ZR36060:
378 name = "zr36060";
379 break;
380 case CODEC_TYPE_ZR36050:
381 name = "zr36050";
382 break;
383 case CODEC_TYPE_ZR36016:
384 name = "zr36016";
385 break;
1da177e4
LT
386 }
387
388 return name;
389}
390
391// struct tvnorm {
392// u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
393// };
394
395static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
396static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
397static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
398static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
399
400static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
401static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
402
403/* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
404static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
405static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
406
407/* FIXME: I cannot swap U and V in saa7114, so i do one
408 * pixel left shift in zoran (75 -> 74)
409 * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
410static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
411static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
412
fbe60daa
MS
413/* FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I
414 * copy Maxim's left shift hack for the 6 Eyes.
415 *
416 * Christer's driver used the unshifted norms, though...
417 * /Sam */
418static struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
419static struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
420
1da177e4
LT
421static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
422 {
423 .type = DC10_old,
424 .name = "DC10(old)",
425 .i2c_decoder = I2C_DRIVERID_VPX3220,
1da177e4
LT
426 .video_codec = CODEC_TYPE_ZR36050,
427 .video_vfe = CODEC_TYPE_ZR36016,
428
429 .inputs = 3,
430 .input = {
431 { 1, "Composite" },
432 { 2, "S-Video" },
433 { 0, "Internal/comp" }
434 },
435 .norms = 3,
436 .tvn = {
437 &f50sqpixel_dc10,
438 &f60sqpixel_dc10,
439 &f50sqpixel_dc10
440 },
441 .jpeg_int = 0,
442 .vsync_int = ZR36057_ISR_GIRQ1,
443 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
444 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
445 .gpcs = { -1, 0 },
446 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
447 .gws_not_connected = 0,
fbe60daa 448 .input_mux = 0,
1da177e4
LT
449 .init = &dc10_init,
450 }, {
451 .type = DC10_new,
452 .name = "DC10(new)",
453 .i2c_decoder = I2C_DRIVERID_SAA7110,
454 .i2c_encoder = I2C_DRIVERID_ADV7175,
455 .video_codec = CODEC_TYPE_ZR36060,
456
457 .inputs = 3,
458 .input = {
459 { 0, "Composite" },
460 { 7, "S-Video" },
461 { 5, "Internal/comp" }
462 },
463 .norms = 3,
464 .tvn = {
465 &f50sqpixel,
466 &f60sqpixel,
467 &f50sqpixel},
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 },
472 .gpcs = { -1, 1},
473 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
474 .gws_not_connected = 0,
fbe60daa 475 .input_mux = 0,
1da177e4
LT
476 .init = &dc10plus_init,
477 }, {
478 .type = DC10plus,
479 .name = "DC10plus",
480 .vendor_id = PCI_VENDOR_ID_MIRO,
481 .device_id = PCI_DEVICE_ID_MIRO_DC10PLUS,
482 .i2c_decoder = I2C_DRIVERID_SAA7110,
483 .i2c_encoder = I2C_DRIVERID_ADV7175,
484 .video_codec = CODEC_TYPE_ZR36060,
485
486 .inputs = 3,
487 .input = {
488 { 0, "Composite" },
489 { 7, "S-Video" },
490 { 5, "Internal/comp" }
491 },
492 .norms = 3,
493 .tvn = {
494 &f50sqpixel,
495 &f60sqpixel,
496 &f50sqpixel
497 },
498 .jpeg_int = ZR36057_ISR_GIRQ0,
499 .vsync_int = ZR36057_ISR_GIRQ1,
500 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
501 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
502 .gpcs = { -1, 1 },
503 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
504 .gws_not_connected = 0,
fbe60daa 505 .input_mux = 0,
1da177e4
LT
506 .init = &dc10plus_init,
507 }, {
508 .type = DC30,
509 .name = "DC30",
510 .i2c_decoder = I2C_DRIVERID_VPX3220,
511 .i2c_encoder = I2C_DRIVERID_ADV7175,
512 .video_codec = CODEC_TYPE_ZR36050,
513 .video_vfe = CODEC_TYPE_ZR36016,
514
515 .inputs = 3,
516 .input = {
517 { 1, "Composite" },
518 { 2, "S-Video" },
519 { 0, "Internal/comp" }
520 },
521 .norms = 3,
522 .tvn = {
523 &f50sqpixel_dc10,
524 &f60sqpixel_dc10,
525 &f50sqpixel_dc10
526 },
527 .jpeg_int = 0,
528 .vsync_int = ZR36057_ISR_GIRQ1,
529 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
530 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
531 .gpcs = { -1, 0 },
532 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
533 .gws_not_connected = 0,
fbe60daa 534 .input_mux = 0,
1da177e4
LT
535 .init = &dc10_init,
536 }, {
537 .type = DC30plus,
538 .name = "DC30plus",
539 .vendor_id = PCI_VENDOR_ID_MIRO,
540 .device_id = PCI_DEVICE_ID_MIRO_DC30PLUS,
541 .i2c_decoder = I2C_DRIVERID_VPX3220,
542 .i2c_encoder = I2C_DRIVERID_ADV7175,
543 .video_codec = CODEC_TYPE_ZR36050,
544 .video_vfe = CODEC_TYPE_ZR36016,
545
546 .inputs = 3,
547 .input = {
548 { 1, "Composite" },
549 { 2, "S-Video" },
550 { 0, "Internal/comp" }
551 },
552 .norms = 3,
553 .tvn = {
554 &f50sqpixel_dc10,
555 &f60sqpixel_dc10,
556 &f50sqpixel_dc10
557 },
558 .jpeg_int = 0,
559 .vsync_int = ZR36057_ISR_GIRQ1,
560 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
561 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
562 .gpcs = { -1, 0 },
563 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
564 .gws_not_connected = 0,
fbe60daa 565 .input_mux = 0,
1da177e4
LT
566 .init = &dc10_init,
567 }, {
568 .type = LML33,
569 .name = "LML33",
570 .i2c_decoder = I2C_DRIVERID_BT819,
571 .i2c_encoder = I2C_DRIVERID_BT856,
572 .video_codec = CODEC_TYPE_ZR36060,
573
574 .inputs = 2,
575 .input = {
576 { 0, "Composite" },
577 { 7, "S-Video" }
578 },
579 .norms = 2,
580 .tvn = {
581 &f50ccir601_lml33,
582 &f60ccir601_lml33,
583 NULL
584 },
585 .jpeg_int = ZR36057_ISR_GIRQ1,
586 .vsync_int = ZR36057_ISR_GIRQ0,
587 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
588 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
589 .gpcs = { 3, 1 },
590 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
591 .gws_not_connected = 1,
fbe60daa 592 .input_mux = 0,
1da177e4
LT
593 .init = &lml33_init,
594 }, {
595 .type = LML33R10,
596 .name = "LML33R10",
597 .vendor_id = PCI_VENDOR_ID_ELECTRONICDESIGNGMBH,
598 .device_id = PCI_DEVICE_ID_LML_33R10,
599 .i2c_decoder = I2C_DRIVERID_SAA7114,
600 .i2c_encoder = I2C_DRIVERID_ADV7170,
601 .video_codec = CODEC_TYPE_ZR36060,
602
603 .inputs = 2,
604 .input = {
605 { 0, "Composite" },
606 { 7, "S-Video" }
607 },
608 .norms = 2,
609 .tvn = {
610 &f50ccir601_lm33r10,
611 &f60ccir601_lm33r10,
612 NULL
613 },
614 .jpeg_int = ZR36057_ISR_GIRQ1,
615 .vsync_int = ZR36057_ISR_GIRQ0,
616 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
617 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
618 .gpcs = { 3, 1 },
619 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
620 .gws_not_connected = 1,
fbe60daa 621 .input_mux = 0,
1da177e4
LT
622 .init = &lml33_init,
623 }, {
624 .type = BUZ,
625 .name = "Buz",
626 .vendor_id = PCI_VENDOR_ID_IOMEGA,
627 .device_id = PCI_DEVICE_ID_IOMEGA_BUZ,
628 .i2c_decoder = I2C_DRIVERID_SAA7111A,
629 .i2c_encoder = I2C_DRIVERID_SAA7185B,
630 .video_codec = CODEC_TYPE_ZR36060,
631
632 .inputs = 2,
633 .input = {
634 { 3, "Composite" },
635 { 7, "S-Video" }
636 },
637 .norms = 3,
638 .tvn = {
639 &f50ccir601,
640 &f60ccir601,
641 &f50ccir601
642 },
643 .jpeg_int = ZR36057_ISR_GIRQ1,
644 .vsync_int = ZR36057_ISR_GIRQ0,
645 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
646 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
647 .gpcs = { 3, 1 },
648 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
649 .gws_not_connected = 1,
fbe60daa 650 .input_mux = 0,
1da177e4 651 .init = &buz_init,
fbe60daa
MS
652 }, {
653 .type = AVS6EYES,
654 .name = "6-Eyes",
655 /* AverMedia chose not to brand the 6-Eyes. Thus it
656 can't be autodetected, and requires card=x. */
657 .vendor_id = -1,
658 .device_id = -1,
659 .i2c_decoder = I2C_DRIVERID_KS0127,
660 .i2c_encoder = I2C_DRIVERID_BT866,
661 .video_codec = CODEC_TYPE_ZR36060,
662
663 .inputs = 10,
664 .input = {
665 { 0, "Composite 1" },
666 { 1, "Composite 2" },
667 { 2, "Composite 3" },
668 { 4, "Composite 4" },
669 { 5, "Composite 5" },
670 { 6, "Composite 6" },
671 { 8, "S-Video 1" },
672 { 9, "S-Video 2" },
673 {10, "S-Video 3" },
674 {15, "YCbCr" }
675 },
676 .norms = 2,
677 .tvn = {
678 &f50ccir601_avs6eyes,
679 &f60ccir601_avs6eyes,
680 NULL
681 },
682 .jpeg_int = ZR36057_ISR_GIRQ1,
683 .vsync_int = ZR36057_ISR_GIRQ0,
684 .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
685 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
686 .gpcs = { 3, 1 }, // Validity unknown /Sam
687 .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
688 .gws_not_connected = 1,
689 .input_mux = 1,
690 .init = &avs6eyes_init,
1da177e4 691 }
fbe60daa 692
1da177e4
LT
693};
694
695/*
696 * I2C functions
697 */
698/* software I2C functions */
699static int
700zoran_i2c_getsda (void *data)
701{
702 struct zoran *zr = (struct zoran *) data;
703
704 return (btread(ZR36057_I2CBR) >> 1) & 1;
705}
706
707static int
708zoran_i2c_getscl (void *data)
709{
710 struct zoran *zr = (struct zoran *) data;
711
712 return btread(ZR36057_I2CBR) & 1;
713}
714
715static void
716zoran_i2c_setsda (void *data,
717 int state)
718{
719 struct zoran *zr = (struct zoran *) data;
720
721 if (state)
722 zr->i2cbr |= 2;
723 else
724 zr->i2cbr &= ~2;
725 btwrite(zr->i2cbr, ZR36057_I2CBR);
726}
727
728static void
729zoran_i2c_setscl (void *data,
730 int state)
731{
732 struct zoran *zr = (struct zoran *) data;
733
734 if (state)
735 zr->i2cbr |= 1;
736 else
737 zr->i2cbr &= ~1;
738 btwrite(zr->i2cbr, ZR36057_I2CBR);
739}
740
741static int
742zoran_i2c_client_register (struct i2c_client *client)
743{
744 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
745 int res = 0;
746
747 dprintk(2,
748 KERN_DEBUG "%s: i2c_client_register() - driver id = %d\n",
749 ZR_DEVNAME(zr), client->driver->id);
750
384c3689 751 mutex_lock(&zr->resource_lock);
1da177e4
LT
752
753 if (zr->user > 0) {
754 /* we're already busy, so we keep a reference to
755 * them... Could do a lot of stuff here, but this
756 * is easiest. (Did I ever mention I'm a lazy ass?)
757 */
758 res = -EBUSY;
759 goto clientreg_unlock_and_return;
760 }
761
762 if (client->driver->id == zr->card.i2c_decoder)
763 zr->decoder = client;
764 else if (client->driver->id == zr->card.i2c_encoder)
765 zr->encoder = client;
766 else {
767 res = -ENODEV;
768 goto clientreg_unlock_and_return;
769 }
770
771clientreg_unlock_and_return:
384c3689 772 mutex_unlock(&zr->resource_lock);
1da177e4
LT
773
774 return res;
775}
776
777static int
778zoran_i2c_client_unregister (struct i2c_client *client)
779{
780 struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
781 int res = 0;
782
783 dprintk(2, KERN_DEBUG "%s: i2c_client_unregister()\n", ZR_DEVNAME(zr));
784
384c3689 785 mutex_lock(&zr->resource_lock);
1da177e4
LT
786
787 if (zr->user > 0) {
788 res = -EBUSY;
789 goto clientunreg_unlock_and_return;
790 }
791
792 /* try to locate it */
793 if (client == zr->encoder) {
794 zr->encoder = NULL;
795 } else if (client == zr->decoder) {
796 zr->decoder = NULL;
797 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%d]", zr->id);
798 }
799clientunreg_unlock_and_return:
384c3689 800 mutex_unlock(&zr->resource_lock);
1da177e4
LT
801 return res;
802}
803
6275163e 804static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
1da177e4
LT
805 .setsda = zoran_i2c_setsda,
806 .setscl = zoran_i2c_setscl,
807 .getsda = zoran_i2c_getsda,
808 .getscl = zoran_i2c_getscl,
809 .udelay = 10,
1da177e4
LT
810 .timeout = 100,
811};
812
1da177e4
LT
813static int
814zoran_register_i2c (struct zoran *zr)
815{
816 memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
817 sizeof(struct i2c_algo_bit_data));
818 zr->i2c_algo.data = zr;
54c776a5 819 zr->i2c_adapter.class = I2C_CLASS_TV_ANALOG;
6275163e
JD
820 zr->i2c_adapter.id = I2C_HW_B_ZR36067;
821 zr->i2c_adapter.client_register = zoran_i2c_client_register;
822 zr->i2c_adapter.client_unregister = zoran_i2c_client_unregister;
823 strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
824 sizeof(zr->i2c_adapter.name));
1da177e4
LT
825 i2c_set_adapdata(&zr->i2c_adapter, zr);
826 zr->i2c_adapter.algo_data = &zr->i2c_algo;
12a917f6 827 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
1da177e4
LT
828 return i2c_bit_add_bus(&zr->i2c_adapter);
829}
830
831static void
832zoran_unregister_i2c (struct zoran *zr)
833{
3269711b 834 i2c_del_adapter(&zr->i2c_adapter);
1da177e4
LT
835}
836
837/* Check a zoran_params struct for correctness, insert default params */
838
839int
840zoran_check_jpg_settings (struct zoran *zr,
841 struct zoran_jpg_settings *settings)
842{
843 int err = 0, err0 = 0;
844
845 dprintk(4,
846 KERN_DEBUG
847 "%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
848 ZR_DEVNAME(zr), settings->decimation, settings->HorDcm,
849 settings->VerDcm, settings->TmpDcm);
850 dprintk(4,
851 KERN_DEBUG
852 "%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
853 ZR_DEVNAME(zr), settings->img_x, settings->img_y,
854 settings->img_width, settings->img_height);
855 /* Check decimation, set default values for decimation = 1, 2, 4 */
856 switch (settings->decimation) {
857 case 1:
858
859 settings->HorDcm = 1;
860 settings->VerDcm = 1;
861 settings->TmpDcm = 1;
862 settings->field_per_buff = 2;
863 settings->img_x = 0;
864 settings->img_y = 0;
865 settings->img_width = BUZ_MAX_WIDTH;
866 settings->img_height = BUZ_MAX_HEIGHT / 2;
867 break;
868 case 2:
869
870 settings->HorDcm = 2;
871 settings->VerDcm = 1;
872 settings->TmpDcm = 2;
873 settings->field_per_buff = 1;
874 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
875 settings->img_y = 0;
876 settings->img_width =
877 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
878 settings->img_height = BUZ_MAX_HEIGHT / 2;
879 break;
880 case 4:
881
882 if (zr->card.type == DC10_new) {
883 dprintk(1,
884 KERN_DEBUG
885 "%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
886 ZR_DEVNAME(zr));
887 err0++;
888 break;
889 }
890
891 settings->HorDcm = 4;
892 settings->VerDcm = 2;
893 settings->TmpDcm = 2;
894 settings->field_per_buff = 1;
895 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
896 settings->img_y = 0;
897 settings->img_width =
898 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
899 settings->img_height = BUZ_MAX_HEIGHT / 2;
900 break;
901 case 0:
902
903 /* We have to check the data the user has set */
904
905 if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
906 (zr->card.type == DC10_new || settings->HorDcm != 4))
907 err0++;
908 if (settings->VerDcm != 1 && settings->VerDcm != 2)
909 err0++;
910 if (settings->TmpDcm != 1 && settings->TmpDcm != 2)
911 err0++;
912 if (settings->field_per_buff != 1 &&
913 settings->field_per_buff != 2)
914 err0++;
915 if (settings->img_x < 0)
916 err0++;
917 if (settings->img_y < 0)
918 err0++;
919 if (settings->img_width < 0)
920 err0++;
921 if (settings->img_height < 0)
922 err0++;
923 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH)
924 err0++;
925 if (settings->img_y + settings->img_height >
926 BUZ_MAX_HEIGHT / 2)
927 err0++;
928 if (settings->HorDcm && settings->VerDcm) {
929 if (settings->img_width %
930 (16 * settings->HorDcm) != 0)
931 err0++;
932 if (settings->img_height %
933 (8 * settings->VerDcm) != 0)
934 err0++;
935 }
936
937 if (err0) {
938 dprintk(1,
939 KERN_ERR
940 "%s: check_jpg_settings() - error in params for decimation = 0\n",
941 ZR_DEVNAME(zr));
942 err++;
943 }
944 break;
945 default:
946 dprintk(1,
947 KERN_ERR
948 "%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
949 ZR_DEVNAME(zr), settings->decimation);
950 err++;
951 break;
952 }
953
954 if (settings->jpg_comp.quality > 100)
955 settings->jpg_comp.quality = 100;
956 if (settings->jpg_comp.quality < 5)
957 settings->jpg_comp.quality = 5;
958 if (settings->jpg_comp.APPn < 0)
959 settings->jpg_comp.APPn = 0;
960 if (settings->jpg_comp.APPn > 15)
961 settings->jpg_comp.APPn = 15;
962 if (settings->jpg_comp.APP_len < 0)
963 settings->jpg_comp.APP_len = 0;
964 if (settings->jpg_comp.APP_len > 60)
965 settings->jpg_comp.APP_len = 60;
966 if (settings->jpg_comp.COM_len < 0)
967 settings->jpg_comp.COM_len = 0;
968 if (settings->jpg_comp.COM_len > 60)
969 settings->jpg_comp.COM_len = 60;
970 if (err)
971 return -EINVAL;
972 return 0;
973}
974
975void
976zoran_open_init_params (struct zoran *zr)
977{
978 int i;
979
980 /* User must explicitly set a window */
981 zr->overlay_settings.is_set = 0;
982 zr->overlay_mask = NULL;
983 zr->overlay_active = ZORAN_FREE;
984
985 zr->v4l_memgrab_active = 0;
986 zr->v4l_overlay_active = 0;
987 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
988 zr->v4l_grab_seq = 0;
989 zr->v4l_settings.width = 192;
990 zr->v4l_settings.height = 144;
c014ec90 991 zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
1da177e4
LT
992 zr->v4l_settings.bytesperline =
993 zr->v4l_settings.width *
994 ((zr->v4l_settings.format->depth + 7) / 8);
995
996 /* DMA ring stuff for V4L */
997 zr->v4l_pend_tail = 0;
998 zr->v4l_pend_head = 0;
999 zr->v4l_sync_tail = 0;
1000 zr->v4l_buffers.active = ZORAN_FREE;
1001 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1002 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1003 }
1004 zr->v4l_buffers.allocated = 0;
1005
1006 for (i = 0; i < BUZ_MAX_FRAME; i++) {
1007 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
1008 }
1009 zr->jpg_buffers.active = ZORAN_FREE;
1010 zr->jpg_buffers.allocated = 0;
1011 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
1012 zr->jpg_settings.decimation = 1;
1013 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
1014 if (zr->card.type != BUZ)
1015 zr->jpg_settings.odd_even = 1;
1016 else
1017 zr->jpg_settings.odd_even = 0;
1018 zr->jpg_settings.jpg_comp.APPn = 0;
1019 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
1020 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
1021 sizeof(zr->jpg_settings.jpg_comp.APP_data));
1022 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
1023 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
1024 sizeof(zr->jpg_settings.jpg_comp.COM_data));
1025 zr->jpg_settings.jpg_comp.jpeg_markers =
1026 JPEG_MARKER_DHT | JPEG_MARKER_DQT;
1027 i = zoran_check_jpg_settings(zr, &zr->jpg_settings);
1028 if (i)
1029 dprintk(1,
1030 KERN_ERR
1031 "%s: zoran_open_init_params() internal error\n",
1032 ZR_DEVNAME(zr));
1033
1034 clear_interrupt_counters(zr);
1035 zr->testing = 0;
1036}
1037
1038static void __devinit
1039test_interrupts (struct zoran *zr)
1040{
1041 DEFINE_WAIT(wait);
1042 int timeout, icr;
1043
1044 clear_interrupt_counters(zr);
1045
1046 zr->testing = 1;
1047 icr = btread(ZR36057_ICR);
1048 btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
1049 prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
1050 timeout = schedule_timeout(HZ);
1051 finish_wait(&zr->test_q, &wait);
1052 btwrite(0, ZR36057_ICR);
1053 btwrite(0x78000000, ZR36057_ISR);
1054 zr->testing = 0;
1055 dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
1056 if (timeout) {
1057 dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
1058 }
18b548ca 1059 if (zr36067_debug > 1)
1da177e4
LT
1060 print_interrupts(zr);
1061 btwrite(icr, ZR36057_ICR);
1062}
1063
1064static int __devinit
1065zr36057_init (struct zoran *zr)
1066{
daf72f40 1067 int j, err;
1da177e4
LT
1068 int two = 2;
1069 int zero = 0;
1070
1071 dprintk(1,
1072 KERN_INFO
1073 "%s: zr36057_init() - initializing card[%d], zr=%p\n",
1074 ZR_DEVNAME(zr), zr->id, zr);
1075
1076 /* default setup of all parameters which will persist between opens */
1077 zr->user = 0;
1078
1079 init_waitqueue_head(&zr->v4l_capq);
1080 init_waitqueue_head(&zr->jpg_capq);
1081 init_waitqueue_head(&zr->test_q);
1082 zr->jpg_buffers.allocated = 0;
1083 zr->v4l_buffers.allocated = 0;
1084
1085 zr->buffer.base = (void *) vidmem;
1086 zr->buffer.width = 0;
1087 zr->buffer.height = 0;
1088 zr->buffer.depth = 0;
1089 zr->buffer.bytesperline = 0;
1090
1091 /* Avoid nonsense settings from user for default input/norm */
1092 if (default_norm < VIDEO_MODE_PAL &&
1093 default_norm > VIDEO_MODE_SECAM)
1094 default_norm = VIDEO_MODE_PAL;
1095 zr->norm = default_norm;
1096 if (!(zr->timing = zr->card.tvn[zr->norm])) {
1097 dprintk(1,
1098 KERN_WARNING
1099 "%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
1100 ZR_DEVNAME(zr));
1101 zr->norm = VIDEO_MODE_PAL;
1102 zr->timing = zr->card.tvn[zr->norm];
1103 }
1104
60e3cac4
TP
1105 if (default_input > zr->card.inputs-1) {
1106 dprintk(1,
1107 KERN_WARNING
1108 "%s: default_input value %d out of range (0-%d)\n",
1109 ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
1110 default_input = 0;
1111 }
1112 zr->input = default_input;
1da177e4
LT
1113
1114 /* Should the following be reset at every open ? */
1115 zr->hue = 32768;
1116 zr->contrast = 32768;
1117 zr->saturation = 32768;
1118 zr->brightness = 32768;
1119
1120 /* default setup (will be repeated at every open) */
1121 zoran_open_init_params(zr);
1122
1123 /* allocate memory *before* doing anything to the hardware
1124 * in case allocation fails */
daf72f40
JD
1125 zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
1126 zr->video_dev = kmalloc(sizeof(struct video_device), GFP_KERNEL);
1127 if (!zr->stat_com || !zr->video_dev) {
1da177e4
LT
1128 dprintk(1,
1129 KERN_ERR
1130 "%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
1131 ZR_DEVNAME(zr));
daf72f40
JD
1132 err = -ENOMEM;
1133 goto exit_free;
1da177e4 1134 }
1da177e4 1135 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
9c169df8 1136 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1da177e4
LT
1137 }
1138
1139 /*
1140 * Now add the template and register the device unit.
1141 */
1da177e4
LT
1142 memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
1143 strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
60e3cac4 1144 err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
daf72f40 1145 if (err < 0)
66aa66ea 1146 goto exit_free;
1da177e4
LT
1147
1148 zoran_init_hardware(zr);
18b548ca 1149 if (zr36067_debug > 2)
1da177e4
LT
1150 detect_guest_activity(zr);
1151 test_interrupts(zr);
1152 if (!pass_through) {
1153 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1154 encoder_command(zr, ENCODER_SET_INPUT, &two);
1155 }
1156
1157 zr->zoran_proc = NULL;
1158 zr->initialized = 1;
1159 return 0;
daf72f40 1160
daf72f40
JD
1161exit_free:
1162 kfree(zr->stat_com);
1163 kfree(zr->video_dev);
1164 return err;
1da177e4
LT
1165}
1166
66aa66ea 1167static void __devexit zoran_remove(struct pci_dev *pdev)
1da177e4 1168{
66aa66ea
TP
1169 struct zoran *zr = pci_get_drvdata(pdev);
1170
1da177e4 1171 if (!zr->initialized)
85b9b8a4 1172 goto exit_free;
66aa66ea 1173
1da177e4
LT
1174 /* unregister videocodec bus */
1175 if (zr->codec) {
1176 struct videocodec_master *master = zr->codec->master_data;
2ea75330 1177
1da177e4 1178 videocodec_detach(zr->codec);
2ea75330 1179 kfree(master);
1da177e4
LT
1180 }
1181 if (zr->vfe) {
1182 struct videocodec_master *master = zr->vfe->master_data;
2ea75330 1183
1da177e4 1184 videocodec_detach(zr->vfe);
2ea75330 1185 kfree(master);
1da177e4
LT
1186 }
1187
1188 /* unregister i2c bus */
1189 zoran_unregister_i2c(zr);
1190 /* disable PCI bus-mastering */
1191 zoran_set_pci_master(zr, 0);
1192 /* put chip into reset */
1193 btwrite(0, ZR36057_SPGPPCR);
1194 free_irq(zr->pci_dev->irq, zr);
1195 /* unmap and free memory */
daf72f40 1196 kfree(zr->stat_com);
1da177e4
LT
1197 zoran_proc_cleanup(zr);
1198 iounmap(zr->zr36057_mem);
1199 pci_disable_device(zr->pci_dev);
1200 video_unregister_device(zr->video_dev);
85b9b8a4 1201exit_free:
66aa66ea 1202 pci_set_drvdata(pdev, NULL);
85b9b8a4 1203 kfree(zr);
1da177e4
LT
1204}
1205
1206void
1207zoran_vdev_release (struct video_device *vdev)
1208{
1209 kfree(vdev);
1210}
1211
1212static struct videocodec_master * __devinit
1213zoran_setup_videocodec (struct zoran *zr,
1214 int type)
1215{
1216 struct videocodec_master *m = NULL;
1217
1218 m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
1219 if (!m) {
1220 dprintk(1,
1221 KERN_ERR
1222 "%s: zoran_setup_videocodec() - no memory\n",
1223 ZR_DEVNAME(zr));
1224 return m;
1225 }
1226
22c4a4e9
MCC
1227 /* magic and type are unused for master struct. Makes sense only at
1228 codec structs.
1229 In the past, .type were initialized to the old V4L1 .hardware
1230 value, as VID_HARDWARE_ZR36067
1231 */
1232 m->magic = 0L;
1233 m->type = 0;
1234
1da177e4
LT
1235 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1236 strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1237 m->data = zr;
1238
1239 switch (type)
1240 {
1241 case CODEC_TYPE_ZR36060:
1242 m->readreg = zr36060_read;
1243 m->writereg = zr36060_write;
1244 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1245 break;
1246 case CODEC_TYPE_ZR36050:
1247 m->readreg = zr36050_read;
1248 m->writereg = zr36050_write;
1249 m->flags |= CODEC_FLAG_JPEG;
1250 break;
1251 case CODEC_TYPE_ZR36016:
1252 m->readreg = zr36016_read;
1253 m->writereg = zr36016_write;
1254 m->flags |= CODEC_FLAG_VFE;
1255 break;
1256 }
1257
1258 return m;
1259}
1260
1261/*
c84e6036 1262 * Scan for a Buz card (actually for the PCI controller ZR36057),
1da177e4
LT
1263 * request the irq and map the io memory
1264 */
66aa66ea
TP
1265static int __devinit zoran_probe(struct pci_dev *pdev,
1266 const struct pci_device_id *ent)
1da177e4
LT
1267{
1268 unsigned char latency, need_latency;
1269 struct zoran *zr;
1da177e4
LT
1270 int result;
1271 struct videocodec_master *master_vfe = NULL;
1272 struct videocodec_master *master_codec = NULL;
1273 int card_num;
1274 char *i2c_enc_name, *i2c_dec_name, *codec_name, *vfe_name;
66aa66ea 1275 unsigned int nr;
1da177e4 1276
1da177e4 1277
66aa66ea
TP
1278 nr = atomic_inc_return(&zoran_num) - 1;
1279 if (nr >= BUZ_MAX) {
1280 dprintk(1,
1281 KERN_ERR
1282 "%s: driver limited to %d card(s) maximum\n",
1283 ZORAN_NAME, BUZ_MAX);
1284 return -ENOENT;
1285 }
1da177e4 1286
66aa66ea
TP
1287 card_num = card[nr];
1288 zr = kzalloc(sizeof(struct zoran), GFP_KERNEL);
1289 if (!zr) {
1290 dprintk(1,
1291 KERN_ERR
1292 "%s: find_zr36057() - kzalloc failed\n",
1293 ZORAN_NAME);
1294 /* The entry in zoran[] gets leaked */
1295 return -ENOMEM;
1296 }
1297 zr->pci_dev = pdev;
1298 zr->id = nr;
1299 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1300 spin_lock_init(&zr->spinlock);
1301 mutex_init(&zr->resource_lock);
1302 if (pci_enable_device(pdev))
1303 goto zr_free_mem;
1304 zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
1305 pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION, &zr->revision);
1306 if (zr->revision < 2) {
1307 dprintk(1,
1308 KERN_INFO
1309 "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.\n",
1310 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1311 zr->zr36057_adr);
1da177e4 1312
66aa66ea 1313 if (card_num == -1) {
1da177e4
LT
1314 dprintk(1,
1315 KERN_ERR
66aa66ea 1316 "%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
1da177e4 1317 ZR_DEVNAME(zr));
85b9b8a4 1318 goto zr_free_mem;
1da177e4 1319 }
66aa66ea
TP
1320 } else {
1321 int i;
1322 unsigned short ss_vendor, ss_device;
1da177e4 1323
66aa66ea
TP
1324 ss_vendor = zr->pci_dev->subsystem_vendor;
1325 ss_device = zr->pci_dev->subsystem_device;
1326 dprintk(1,
1327 KERN_INFO
1328 "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08x\n",
1329 ZR_DEVNAME(zr), zr->revision, zr->pci_dev->irq,
1330 zr->zr36057_adr);
1331 dprintk(1,
1332 KERN_INFO
1333 "%s: subsystem vendor=0x%04x id=0x%04x\n",
1334 ZR_DEVNAME(zr), ss_vendor, ss_device);
1335 if (card_num == -1) {
1336 dprintk(3,
1337 KERN_DEBUG
1338 "%s: find_zr36057() - trying to autodetect card type\n",
1339 ZR_DEVNAME(zr));
1340 for (i = 0; i < NUM_CARDS; i++) {
1341 if (ss_vendor == zoran_cards[i].vendor_id &&
1342 ss_device == zoran_cards[i].device_id) {
1343 dprintk(3,
1344 KERN_DEBUG
1345 "%s: find_zr36057() - card %s detected\n",
1346 ZR_DEVNAME(zr),
1347 zoran_cards[i].name);
1348 card_num = i;
1349 break;
1350 }
1351 }
1352 if (i == NUM_CARDS) {
1da177e4
LT
1353 dprintk(1,
1354 KERN_ERR
66aa66ea 1355 "%s: find_zr36057() - unknown card\n",
1da177e4 1356 ZR_DEVNAME(zr));
66aa66ea 1357 goto zr_free_mem;
1da177e4 1358 }
1da177e4 1359 }
66aa66ea 1360 }
1da177e4 1361
66aa66ea
TP
1362 if (card_num < 0 || card_num >= NUM_CARDS) {
1363 dprintk(2,
1364 KERN_ERR
1365 "%s: find_zr36057() - invalid cardnum %d\n",
1366 ZR_DEVNAME(zr), card_num);
1367 goto zr_free_mem;
1368 }
1da177e4 1369
66aa66ea
TP
1370 /* even though we make this a non pointer and thus
1371 * theoretically allow for making changes to this struct
1372 * on a per-individual card basis at runtime, this is
1373 * strongly discouraged. This structure is intended to
1374 * keep general card information, no settings or anything */
1375 zr->card = zoran_cards[card_num];
1376 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
1377 "%s[%u]", zr->card.name, zr->id);
1378
1379 zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
1380 if (!zr->zr36057_mem) {
1381 dprintk(1,
1382 KERN_ERR
1383 "%s: find_zr36057() - ioremap failed\n",
1da177e4 1384 ZR_DEVNAME(zr));
66aa66ea
TP
1385 goto zr_free_mem;
1386 }
1da177e4 1387
66aa66ea
TP
1388 result = request_irq(zr->pci_dev->irq, zoran_irq,
1389 IRQF_SHARED | IRQF_DISABLED, ZR_DEVNAME(zr), zr);
1390 if (result < 0) {
1391 if (result == -EINVAL) {
1392 dprintk(1,
1393 KERN_ERR
1394 "%s: find_zr36057() - bad irq number or handler\n",
1395 ZR_DEVNAME(zr));
1396 } else if (result == -EBUSY) {
1397 dprintk(1,
1398 KERN_ERR
1399 "%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
1400 ZR_DEVNAME(zr), zr->pci_dev->irq);
1da177e4 1401 } else {
66aa66ea
TP
1402 dprintk(1,
1403 KERN_ERR
1404 "%s: find_zr36057() - can't assign irq, error code %d\n",
1405 ZR_DEVNAME(zr), result);
1da177e4 1406 }
66aa66ea
TP
1407 goto zr_unmap;
1408 }
1da177e4 1409
66aa66ea
TP
1410 /* set PCI latency timer */
1411 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1412 &latency);
1413 need_latency = zr->revision > 1 ? 32 : 48;
1414 if (latency != need_latency) {
1415 dprintk(2,
1416 KERN_INFO
1417 "%s: Changing PCI latency from %d to %d\n",
1418 ZR_DEVNAME(zr), latency, need_latency);
1419 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1420 need_latency);
1421 }
1da177e4 1422
66aa66ea
TP
1423 zr36057_restart(zr);
1424 /* i2c */
1425 dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
1426 ZR_DEVNAME(zr));
1427
1428 /* i2c decoder */
1429 if (decoder[zr->id] != -1) {
1430 i2c_dec_name = i2cid_to_modulename(decoder[zr->id]);
1431 zr->card.i2c_decoder = decoder[zr->id];
1432 } else if (zr->card.i2c_decoder != 0) {
1433 i2c_dec_name = i2cid_to_modulename(zr->card.i2c_decoder);
1434 } else {
1435 i2c_dec_name = NULL;
1436 }
1da177e4 1437
66aa66ea
TP
1438 if (i2c_dec_name) {
1439 result = request_module(i2c_dec_name);
1440 if (result < 0) {
1441 dprintk(1,
1442 KERN_ERR
1443 "%s: failed to load module %s: %d\n",
1444 ZR_DEVNAME(zr), i2c_dec_name, result);
1da177e4 1445 }
66aa66ea 1446 }
1da177e4 1447
66aa66ea
TP
1448 /* i2c encoder */
1449 if (encoder[zr->id] != -1) {
1450 i2c_enc_name = i2cid_to_modulename(encoder[zr->id]);
1451 zr->card.i2c_encoder = encoder[zr->id];
1452 } else if (zr->card.i2c_encoder != 0) {
1453 i2c_enc_name = i2cid_to_modulename(zr->card.i2c_encoder);
1454 } else {
1455 i2c_enc_name = NULL;
1456 }
1457
1458 if (i2c_enc_name) {
1459 result = request_module(i2c_enc_name);
1460 if (result < 0) {
1da177e4
LT
1461 dprintk(1,
1462 KERN_ERR
66aa66ea
TP
1463 "%s: failed to load module %s: %d\n",
1464 ZR_DEVNAME(zr), i2c_enc_name, result);
1da177e4 1465 }
66aa66ea 1466 }
1da177e4 1467
66aa66ea
TP
1468 if (zoran_register_i2c(zr) < 0) {
1469 dprintk(1,
1470 KERN_ERR
1471 "%s: find_zr36057() - can't initialize i2c bus\n",
1da177e4 1472 ZR_DEVNAME(zr));
66aa66ea
TP
1473 goto zr_free_irq;
1474 }
1da177e4 1475
66aa66ea
TP
1476 dprintk(2,
1477 KERN_INFO "%s: Initializing videocodec bus...\n",
1478 ZR_DEVNAME(zr));
1479
1480 if (zr->card.video_codec) {
1481 codec_name = codecid_to_modulename(zr->card.video_codec);
1482 if (codec_name) {
1483 result = request_module(codec_name);
1484 if (result) {
1da177e4
LT
1485 dprintk(1,
1486 KERN_ERR
1487 "%s: failed to load modules %s: %d\n",
1488 ZR_DEVNAME(zr), codec_name, result);
1489 }
1490 }
66aa66ea
TP
1491 }
1492 if (zr->card.video_vfe) {
1493 vfe_name = codecid_to_modulename(zr->card.video_vfe);
1494 if (vfe_name) {
1495 result = request_module(vfe_name);
1496 if (result < 0) {
1da177e4
LT
1497 dprintk(1,
1498 KERN_ERR
1499 "%s: failed to load modules %s: %d\n",
1500 ZR_DEVNAME(zr), vfe_name, result);
1501 }
1502 }
66aa66ea 1503 }
1da177e4 1504
66aa66ea
TP
1505 /* reset JPEG codec */
1506 jpeg_codec_sleep(zr, 1);
1507 jpeg_codec_reset(zr);
1508 /* video bus enabled */
1509 /* display codec revision */
1510 if (zr->card.video_codec != 0) {
1511 master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1512 if (!master_codec)
1513 goto zr_unreg_i2c;
1514 zr->codec = videocodec_attach(master_codec);
1515 if (!zr->codec) {
1516 dprintk(1,
1517 KERN_ERR
1518 "%s: find_zr36057() - no codec found\n",
1519 ZR_DEVNAME(zr));
1520 goto zr_free_codec;
1da177e4 1521 }
66aa66ea
TP
1522 if (zr->codec->type != zr->card.video_codec) {
1523 dprintk(1,
1524 KERN_ERR
1525 "%s: find_zr36057() - wrong codec\n",
1526 ZR_DEVNAME(zr));
1527 goto zr_detach_codec;
1da177e4 1528 }
1da177e4 1529 }
66aa66ea
TP
1530 if (zr->card.video_vfe != 0) {
1531 master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1532 if (!master_vfe)
1533 goto zr_detach_codec;
1534 zr->vfe = videocodec_attach(master_vfe);
1535 if (!zr->vfe) {
1536 dprintk(1,
1537 KERN_ERR
1538 "%s: find_zr36057() - no VFE found\n",
1539 ZR_DEVNAME(zr));
1540 goto zr_free_vfe;
1541 }
1542 if (zr->vfe->type != zr->card.video_vfe) {
1543 dprintk(1,
1544 KERN_ERR
1545 "%s: find_zr36057() = wrong VFE\n",
1546 ZR_DEVNAME(zr));
1547 goto zr_detach_vfe;
1548 }
1549 }
1550 zoran[nr] = zr;
e491cbc8 1551
66aa66ea
TP
1552 /* take care of Natoma chipset and a revision 1 zr36057 */
1553 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
1554 zr->jpg_buffers.need_contiguous = 1;
1555 dprintk(1,
1556 KERN_INFO
1557 "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
1558 ZR_DEVNAME(zr));
1da177e4 1559 }
66aa66ea
TP
1560
1561 if (zr36057_init(zr) < 0)
1562 goto zr_detach_vfe;
1563
1564 zoran_proc_init(zr);
1565
1566 pci_set_drvdata(pdev, zr);
1567
1568 return 0;
1569
1570zr_detach_vfe:
1571 videocodec_detach(zr->vfe);
1572zr_free_vfe:
1573 kfree(master_vfe);
1574zr_detach_codec:
1575 videocodec_detach(zr->codec);
1576zr_free_codec:
1577 kfree(master_codec);
1578zr_unreg_i2c:
1579 zoran_unregister_i2c(zr);
1580zr_free_irq:
1581 btwrite(0, ZR36057_SPGPPCR);
1582 free_irq(zr->pci_dev->irq, zr);
1583zr_unmap:
1584 iounmap(zr->zr36057_mem);
1585zr_free_mem:
1586 kfree(zr);
1587
1588 return -ENODEV;
1da177e4
LT
1589}
1590
66aa66ea
TP
1591static struct pci_driver zoran_driver = {
1592 .name = "zr36067",
1593 .id_table = zr36067_pci_tbl,
1594 .probe = zoran_probe,
1595 .remove = zoran_remove,
1596};
1597
1598static int __init zoran_init(void)
1da177e4 1599{
66aa66ea 1600 int res;
1da177e4
LT
1601
1602 memset(zoran, 0, sizeof(zoran));
1603 printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
1604 MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
1605
1da177e4
LT
1606 /* check the parameters we have been given, adjust if necessary */
1607 if (v4l_nbufs < 2)
1608 v4l_nbufs = 2;
1609 if (v4l_nbufs > VIDEO_MAX_FRAME)
1610 v4l_nbufs = VIDEO_MAX_FRAME;
1611 /* The user specfies the in KB, we want them in byte
1612 * (and page aligned) */
1613 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1614 if (v4l_bufsize < 32768)
1615 v4l_bufsize = 32768;
1616 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1617 if (v4l_bufsize > 2048 * 1024)
1618 v4l_bufsize = 2048 * 1024;
1619 if (jpg_nbufs < 4)
1620 jpg_nbufs = 4;
1621 if (jpg_nbufs > BUZ_MAX_FRAME)
1622 jpg_nbufs = BUZ_MAX_FRAME;
1623 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1624 if (jpg_bufsize < 8192)
1625 jpg_bufsize = 8192;
1626 if (jpg_bufsize > (512 * 1024))
1627 jpg_bufsize = 512 * 1024;
1628 /* Use parameter for vidmem or try to find a video card */
1629 if (vidmem) {
1630 dprintk(1,
1631 KERN_INFO
1632 "%s: Using supplied video memory base address @ 0x%lx\n",
1633 ZORAN_NAME, vidmem);
1634 }
1635
1636 /* random nonsense */
18b548ca 1637 dprintk(6, KERN_DEBUG "Jotti is een held!\n");
1da177e4
LT
1638
1639 /* some mainboards might not do PCI-PCI data transfer well */
e355880b 1640 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
1da177e4
LT
1641 dprintk(1,
1642 KERN_WARNING
e355880b 1643 "%s: chipset does not support reliable PCI-PCI DMA\n",
1da177e4
LT
1644 ZORAN_NAME);
1645 }
1646
66aa66ea
TP
1647 res = pci_register_driver(&zoran_driver);
1648 if (res) {
1649 dprintk(1,
1650 KERN_ERR
1651 "%s: Unable to register ZR36057 driver\n",
1652 ZORAN_NAME);
1653 return res;
1da177e4
LT
1654 }
1655
1656 return 0;
1657}
1658
66aa66ea 1659static void __exit zoran_exit(void)
1da177e4 1660{
66aa66ea 1661 pci_unregister_driver(&zoran_driver);
1da177e4
LT
1662}
1663
66aa66ea
TP
1664module_init(zoran_init);
1665module_exit(zoran_exit);
This page took 0.554952 seconds and 5 git commands to generate.