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