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