Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / drivers / media / dvb / ttpci / budget-av.c
1 /*
2  * budget-av.c: driver for the SAA7146 based Budget DVB cards
3  *              with analog video in
4  *
5  * Compiled from various sources by Michael Hunold <michael@mihu.de>
6  *
7  * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
8  *                               Andrew de Quincey <adq_dvb@lidskialf.net>
9  *
10  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
11  *
12  * Copyright (C) 1999-2002 Ralph  Metzler
13  *                       & Marcus Metzler for convergence integrated media GmbH
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31  *
32  *
33  * the project's page is at http://www.linuxtv.org/dvb/
34  */
35
36 #include "budget.h"
37 #include "stv0299.h"
38 #include "tda10021.h"
39 #include "tda1004x.h"
40 #include "dvb-pll.h"
41 #include <media/saa7146_vv.h>
42 #include <linux/module.h>
43 #include <linux/errno.h>
44 #include <linux/slab.h>
45 #include <linux/interrupt.h>
46 #include <linux/input.h>
47 #include <linux/spinlock.h>
48
49 #include "dvb_ca_en50221.h"
50
51 #define DEBICICAM               0x02420000
52
53 struct budget_av {
54         struct budget budget;
55         struct video_device *vd;
56         int cur_input;
57         int has_saa7113;
58         struct tasklet_struct ciintf_irq_tasklet;
59         int slot_status;
60         struct dvb_ca_en50221 ca;
61         u8 reinitialise_demod:1;
62 };
63
64 /* GPIO Connections:
65  * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
66  * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
67  * 2 - CI Card Enable (Active Low)
68  * 3 - CI Card Detect
69  */
70
71 /****************************************************************************
72  * INITIALIZATION
73  ****************************************************************************/
74
75 static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
76 {
77         u8 mm1[] = { 0x00 };
78         u8 mm2[] = { 0x00 };
79         struct i2c_msg msgs[2];
80
81         msgs[0].flags = 0;
82         msgs[1].flags = I2C_M_RD;
83         msgs[0].addr = msgs[1].addr = id / 2;
84         mm1[0] = reg;
85         msgs[0].len = 1;
86         msgs[1].len = 1;
87         msgs[0].buf = mm1;
88         msgs[1].buf = mm2;
89
90         i2c_transfer(i2c, msgs, 2);
91
92         return mm2[0];
93 }
94
95 static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
96 {
97         u8 mm1[] = { reg };
98         struct i2c_msg msgs[2] = {
99                 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
100                 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
101         };
102
103         if (i2c_transfer(i2c, msgs, 2) != 2)
104                 return -EIO;
105
106         return 0;
107 }
108
109 static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
110 {
111         u8 msg[2] = { reg, val };
112         struct i2c_msg msgs;
113
114         msgs.flags = 0;
115         msgs.addr = id / 2;
116         msgs.len = 2;
117         msgs.buf = msg;
118         return i2c_transfer(i2c, &msgs, 1);
119 }
120
121 static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
122 {
123         struct budget_av *budget_av = (struct budget_av *) ca->data;
124         int result;
125
126         if (slot != 0)
127                 return -EINVAL;
128
129         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
130         udelay(1);
131
132         result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
133
134         if (result == -ETIMEDOUT)
135                 budget_av->slot_status = 0;
136         return result;
137 }
138
139 static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
140 {
141         struct budget_av *budget_av = (struct budget_av *) ca->data;
142         int result;
143
144         if (slot != 0)
145                 return -EINVAL;
146
147         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
148         udelay(1);
149
150         result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
151
152         if (result == -ETIMEDOUT)
153                 budget_av->slot_status = 0;
154         return result;
155 }
156
157 static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
158 {
159         struct budget_av *budget_av = (struct budget_av *) ca->data;
160         int result;
161
162         if (slot != 0)
163                 return -EINVAL;
164
165         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
166         udelay(1);
167
168         result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
169
170         if (result == -ETIMEDOUT)
171                 budget_av->slot_status = 0;
172         return result;
173 }
174
175 static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
176 {
177         struct budget_av *budget_av = (struct budget_av *) ca->data;
178         int result;
179
180         if (slot != 0)
181                 return -EINVAL;
182
183         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
184         udelay(1);
185
186         result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
187
188         if (result == -ETIMEDOUT)
189                 budget_av->slot_status = 0;
190         return result;
191 }
192
193 static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
194 {
195         struct budget_av *budget_av = (struct budget_av *) ca->data;
196         struct saa7146_dev *saa = budget_av->budget.dev;
197         int timeout = 50; // 5 seconds (4.4.6 Ready)
198
199         if (slot != 0)
200                 return -EINVAL;
201
202         dprintk(1, "ciintf_slot_reset\n");
203
204         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
205
206         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
207         msleep(2);
208         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
209         msleep(20); /* 20 ms Vcc settling time */
210
211         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
212
213         /* This should have been based on pin 16 READY of the pcmcia port,
214          * but AFAICS it is not routed to the saa7146 */
215         while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
216                 msleep(100);
217
218         /* reinitialise the frontend if necessary */
219         if (budget_av->reinitialise_demod)
220                 dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
221
222         if (timeout <= 0)
223         {
224                 printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
225                 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
226                 return -ETIMEDOUT;
227         }
228
229         return 0;
230 }
231
232 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
233 {
234         struct budget_av *budget_av = (struct budget_av *) ca->data;
235         struct saa7146_dev *saa = budget_av->budget.dev;
236
237         if (slot != 0)
238                 return -EINVAL;
239
240         dprintk(1, "ciintf_slot_shutdown\n");
241
242         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
243         budget_av->slot_status = 0;
244         return 0;
245 }
246
247 static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
248 {
249         struct budget_av *budget_av = (struct budget_av *) ca->data;
250         struct saa7146_dev *saa = budget_av->budget.dev;
251
252         if (slot != 0)
253                 return -EINVAL;
254
255         dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
256
257         ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
258         return 0;
259 }
260
261 static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
262 {
263         struct budget_av *budget_av = (struct budget_av *) ca->data;
264         struct saa7146_dev *saa = budget_av->budget.dev;
265         int cam_present = 0;
266
267         if (slot != 0)
268                 return -EINVAL;
269
270         if (!budget_av->slot_status)
271         {
272                 // first of all test the card detect line
273                 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
274                 udelay(1);
275                 if (saa7146_read(saa, PSR) & MASK_06)
276                 {
277                         cam_present = 1;
278                 }
279                 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
280
281                 // that is unreliable however, so try and read from IO memory
282                 if (!cam_present)
283                 {
284                         saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
285                         if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) != -ETIMEDOUT)
286                         {
287                                 cam_present = 1;
288                         }
289                 }
290
291                 // did we find something?
292                 if (cam_present) {
293                         printk(KERN_INFO "budget-av: cam inserted\n");
294                         budget_av->slot_status = 1;
295                 }
296         } else if (!open) {
297                 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
298                 if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
299                 {
300                         printk(KERN_INFO "budget-av: cam ejected\n");
301                         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
302                         budget_av->slot_status = 0;
303                 }
304         }
305
306         if (budget_av->slot_status == 1)
307                 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
308
309         return 0;
310 }
311
312 static int ciintf_init(struct budget_av *budget_av)
313 {
314         struct saa7146_dev *saa = budget_av->budget.dev;
315         int result;
316
317         memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
318
319         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
320         saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
321         saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
322         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
323
324         /* Enable DEBI pins */
325         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
326
327         /* register CI interface */
328         budget_av->ca.owner = THIS_MODULE;
329         budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
330         budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
331         budget_av->ca.read_cam_control = ciintf_read_cam_control;
332         budget_av->ca.write_cam_control = ciintf_write_cam_control;
333         budget_av->ca.slot_reset = ciintf_slot_reset;
334         budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
335         budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
336         budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
337         budget_av->ca.data = budget_av;
338
339         if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
340                                           &budget_av->ca, 0, 1)) != 0) {
341                 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
342                 goto error;
343         }
344
345         printk(KERN_INFO "budget-av: ci interface initialised.\n");
346         budget_av->budget.ci_present = 1;
347         return 0;
348
349 error:
350         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
351         return result;
352 }
353
354 static void ciintf_deinit(struct budget_av *budget_av)
355 {
356         struct saa7146_dev *saa = budget_av->budget.dev;
357
358         saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
359         saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
360         saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
361         saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
362
363         /* release the CA device */
364         dvb_ca_en50221_release(&budget_av->ca);
365
366         /* disable DEBI pins */
367         saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
368 }
369
370
371 static const u8 saa7113_tab[] = {
372         0x01, 0x08,
373         0x02, 0xc0,
374         0x03, 0x33,
375         0x04, 0x00,
376         0x05, 0x00,
377         0x06, 0xeb,
378         0x07, 0xe0,
379         0x08, 0x28,
380         0x09, 0x00,
381         0x0a, 0x80,
382         0x0b, 0x47,
383         0x0c, 0x40,
384         0x0d, 0x00,
385         0x0e, 0x01,
386         0x0f, 0x44,
387
388         0x10, 0x08,
389         0x11, 0x0c,
390         0x12, 0x7b,
391         0x13, 0x00,
392         0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
393
394         0x57, 0xff,
395         0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
396         0x5b, 0x83, 0x5e, 0x00,
397         0xff
398 };
399
400 static int saa7113_init(struct budget_av *budget_av)
401 {
402         struct budget *budget = &budget_av->budget;
403         struct saa7146_dev *saa = budget->dev;
404         const u8 *data = saa7113_tab;
405
406         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
407         msleep(200);
408
409         if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
410                 dprintk(1, "saa7113 not found on KNC card\n");
411                 return -ENODEV;
412         }
413
414         dprintk(1, "saa7113 detected and initializing\n");
415
416         while (*data != 0xff) {
417                 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
418                 data += 2;
419         }
420
421         dprintk(1, "saa7113  status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
422
423         return 0;
424 }
425
426 static int saa7113_setinput(struct budget_av *budget_av, int input)
427 {
428         struct budget *budget = &budget_av->budget;
429
430         if (1 != budget_av->has_saa7113)
431                 return -ENODEV;
432
433         if (input == 1) {
434                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
435                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
436         } else if (input == 0) {
437                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
438                 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
439         } else
440                 return -EINVAL;
441
442         budget_av->cur_input = input;
443         return 0;
444 }
445
446
447 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
448 {
449         u8 aclk = 0;
450         u8 bclk = 0;
451         u8 m1;
452
453         aclk = 0xb5;
454         if (srate < 2000000)
455                 bclk = 0x86;
456         else if (srate < 5000000)
457                 bclk = 0x89;
458         else if (srate < 15000000)
459                 bclk = 0x8f;
460         else if (srate < 45000000)
461                 bclk = 0x95;
462
463         m1 = 0x14;
464         if (srate < 4000000)
465                 m1 = 0x10;
466
467         stv0299_writereg(fe, 0x13, aclk);
468         stv0299_writereg(fe, 0x14, bclk);
469         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
470         stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
471         stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
472         stv0299_writereg(fe, 0x0f, 0x80 | m1);
473
474         return 0;
475 }
476
477 static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
478                                         struct i2c_adapter *i2c,
479                                         struct dvb_frontend_parameters *params)
480 {
481         u32 div;
482         u8 buf[4];
483         struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
484
485         if ((params->frequency < 950000) || (params->frequency > 2150000))
486                 return -EINVAL;
487
488         div = (params->frequency + (125 - 1)) / 125;    // round correctly
489         buf[0] = (div >> 8) & 0x7f;
490         buf[1] = div & 0xff;
491         buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
492         buf[3] = 0x20;
493
494         if (params->u.qpsk.symbol_rate < 4000000)
495                 buf[3] |= 1;
496
497         if (params->frequency < 1250000)
498                 buf[3] |= 0;
499         else if (params->frequency < 1550000)
500                 buf[3] |= 0x40;
501         else if (params->frequency < 2050000)
502                 buf[3] |= 0x80;
503         else if (params->frequency < 2150000)
504                 buf[3] |= 0xC0;
505
506         if (i2c_transfer(i2c, &msg, 1) != 1)
507                 return -EIO;
508         return 0;
509 }
510
511 #define MIN2(a,b) ((a) < (b) ? (a) : (b))
512 #define MIN3(a,b,c) MIN2(MIN2(a,b),c)
513
514 static int philips_su1278sh2_tua6100_pll_set(struct dvb_frontend *fe,
515                                         struct i2c_adapter *i2c,
516                                         struct dvb_frontend_parameters *params)
517 {
518         u8 reg0 [2] = { 0x00, 0x00 };
519         u8 reg1 [4] = { 0x01, 0x00, 0x00, 0x00 };
520         u8 reg2 [3] = { 0x02, 0x00, 0x00 };
521         int _fband;
522         int first_ZF;
523         int R, A, N, P, M;
524         struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = NULL,.len = 0 };
525         int freq = params->frequency;
526
527         first_ZF = (freq) / 1000;
528
529         if (abs(MIN2(abs(first_ZF-1190),abs(first_ZF-1790))) <
530                    abs(MIN3(abs(first_ZF-1202),abs(first_ZF-1542),abs(first_ZF-1890))))
531                 _fband = 2;
532         else
533                 _fband = 3;
534
535         if (_fband == 2) {
536                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
537                                     ((first_ZF >= 1430) && (first_ZF < 1950)))
538                         reg0[1] = 0x07;
539                 else if (((first_ZF >= 1350) && (first_ZF < 1430)) ||
540                                          ((first_ZF >= 1950) && (first_ZF < 2150)))
541                         reg0[1] = 0x0B;
542         }
543
544         if(_fband == 3) {
545                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
546                                     ((first_ZF >= 1455) && (first_ZF < 1950)))
547                         reg0[1] = 0x07;
548                 else if (((first_ZF >= 1350) && (first_ZF < 1420)) ||
549                                          ((first_ZF >= 1950) && (first_ZF < 2150)))
550                         reg0[1] = 0x0B;
551                 else if ((first_ZF >= 1420) && (first_ZF < 1455))
552                         reg0[1] = 0x0F;
553         }
554
555         if (first_ZF > 1525)
556                 reg1[1] |= 0x80;
557         else
558                 reg1[1] &= 0x7F;
559
560         if (_fband == 2) {
561                 if (first_ZF > 1430) { /* 1430MHZ */
562                         reg1[1] &= 0xCF; /* N2 */
563                         reg2[1] &= 0xCF; /* R2 */
564                         reg2[1] |= 0x10;
565                 } else {
566                         reg1[1] &= 0xCF; /* N2 */
567                         reg1[1] |= 0x20;
568                         reg2[1] &= 0xCF; /* R2 */
569                         reg2[1] |= 0x10;
570                 }
571         }
572
573         if (_fband == 3) {
574                 if ((first_ZF >= 1455) &&
575                                    (first_ZF < 1630)) {
576                         reg1[1] &= 0xCF; /* N2 */
577                         reg1[1] |= 0x20;
578                         reg2[1] &= 0xCF; /* R2 */
579                                    } else {
580                                            if (first_ZF < 1455) {
581                                                    reg1[1] &= 0xCF; /* N2 */
582                                                    reg1[1] |= 0x20;
583                                                    reg2[1] &= 0xCF; /* R2 */
584                                                    reg2[1] |= 0x10;
585                                            } else {
586                                                    if (first_ZF >= 1630) {
587                                                            reg1[1] &= 0xCF; /* N2 */
588                                                            reg2[1] &= 0xCF; /* R2 */
589                                                            reg2[1] |= 0x10;
590                                                    }
591                                            }
592                                    }
593         }
594
595         /* set ports, enable P0 for symbol rates > 4Ms/s */
596         if (params->u.qpsk.symbol_rate >= 4000000)
597                 reg1[1] |= 0x0c;
598         else
599                 reg1[1] |= 0x04;
600
601         reg2[1] |= 0x0c;
602
603         R = 64;
604         A = 64;
605         P = 64;  //32
606
607         M = (freq * R) / 4;             /* in Mhz */
608         N = (M - A * 1000) / (P * 1000);
609
610         reg1[1] |= (N >> 9) & 0x03;
611         reg1[2]  = (N >> 1) & 0xff;
612         reg1[3]  = (N << 7) & 0x80;
613
614         reg2[1] |= (R >> 8) & 0x03;
615         reg2[2]  = R & 0xFF;    /* R */
616
617         reg1[3] |= A & 0x7f;    /* A */
618
619         if (P == 64)
620                 reg1[1] |= 0x40; /* Prescaler 64/65 */
621
622         reg0[1] |= 0x03;
623
624         /* already enabled - do not reenable i2c repeater or TX fails */
625         msg.buf = reg0;
626         msg.len = sizeof(reg0);
627         if (i2c_transfer(i2c, &msg, 1) != 1)
628                 return -EIO;
629
630         stv0299_enable_plli2c(fe);
631         msg.buf = reg1;
632         msg.len = sizeof(reg1);
633         if (i2c_transfer(i2c, &msg, 1) != 1)
634                 return -EIO;
635
636         stv0299_enable_plli2c(fe);
637         msg.buf = reg2;
638         msg.len = sizeof(reg2);
639         if (i2c_transfer(i2c, &msg, 1) != 1)
640                 return -EIO;
641
642         return 0;
643 }
644
645 static u8 typhoon_cinergy1200s_inittab[] = {
646         0x01, 0x15,
647         0x02, 0x30,
648         0x03, 0x00,
649         0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
650         0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
651         0x06, 0x40,             /* DAC not used, set to high impendance mode */
652         0x07, 0x00,             /* DAC LSB */
653         0x08, 0x40,             /* DiSEqC off */
654         0x09, 0x00,             /* FIFO */
655         0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
656         0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
657         0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
658         0x10, 0x3f,             // AGC2  0x3d
659         0x11, 0x84,
660         0x12, 0xb9,
661         0x15, 0xc9,             // lock detector threshold
662         0x16, 0x00,
663         0x17, 0x00,
664         0x18, 0x00,
665         0x19, 0x00,
666         0x1a, 0x00,
667         0x1f, 0x50,
668         0x20, 0x00,
669         0x21, 0x00,
670         0x22, 0x00,
671         0x23, 0x00,
672         0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
673         0x29, 0x1e,             // 1/2 threshold
674         0x2a, 0x14,             // 2/3 threshold
675         0x2b, 0x0f,             // 3/4 threshold
676         0x2c, 0x09,             // 5/6 threshold
677         0x2d, 0x05,             // 7/8 threshold
678         0x2e, 0x01,
679         0x31, 0x1f,             // test all FECs
680         0x32, 0x19,             // viterbi and synchro search
681         0x33, 0xfc,             // rs control
682         0x34, 0x93,             // error control
683         0x0f, 0x92,
684         0xff, 0xff
685 };
686
687 static struct stv0299_config typhoon_config = {
688         .demod_address = 0x68,
689         .inittab = typhoon_cinergy1200s_inittab,
690         .mclk = 88000000UL,
691         .invert = 0,
692         .skip_reinit = 0,
693         .lock_output = STV0229_LOCKOUTPUT_1,
694         .volt13_op0_op1 = STV0299_VOLT13_OP0,
695         .min_delay_ms = 100,
696         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
697         .pll_set = philips_su1278_ty_ci_pll_set,
698 };
699
700
701 static struct stv0299_config cinergy_1200s_config = {
702         .demod_address = 0x68,
703         .inittab = typhoon_cinergy1200s_inittab,
704         .mclk = 88000000UL,
705         .invert = 0,
706         .skip_reinit = 0,
707         .lock_output = STV0229_LOCKOUTPUT_0,
708         .volt13_op0_op1 = STV0299_VOLT13_OP0,
709         .min_delay_ms = 100,
710         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
711         .pll_set = philips_su1278_ty_ci_pll_set,
712 };
713
714 static struct stv0299_config cinergy_1200s_1894_0010_config = {
715         .demod_address = 0x68,
716         .inittab = typhoon_cinergy1200s_inittab,
717         .mclk = 88000000UL,
718         .invert = 1,
719         .skip_reinit = 0,
720         .lock_output = STV0229_LOCKOUTPUT_1,
721         .volt13_op0_op1 = STV0299_VOLT13_OP0,
722         .min_delay_ms = 100,
723         .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
724         .pll_set = philips_su1278sh2_tua6100_pll_set,
725 };
726
727 static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
728 {
729         struct budget *budget = (struct budget *) fe->dvb->priv;
730         u8 buf[4];
731         struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
732
733 #define TUNER_MUL 62500
734
735         u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
736
737         buf[0] = (div >> 8) & 0x7f;
738         buf[1] = div & 0xff;
739         buf[2] = 0x86;
740         buf[3] = (params->frequency < 150000000 ? 0x01 :
741                   params->frequency < 445000000 ? 0x02 : 0x04);
742
743         if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
744                 return -EIO;
745         return 0;
746 }
747
748 static struct tda10021_config philips_cu1216_config = {
749         .demod_address = 0x0c,
750         .pll_set = philips_cu1216_pll_set,
751 };
752
753
754
755
756 static int philips_tu1216_pll_init(struct dvb_frontend *fe)
757 {
758         struct budget *budget = (struct budget *) fe->dvb->priv;
759         static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
760         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
761
762         // setup PLL configuration
763         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
764                 return -EIO;
765         msleep(1);
766
767         return 0;
768 }
769
770 static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
771 {
772         struct budget *budget = (struct budget *) fe->dvb->priv;
773         u8 tuner_buf[4];
774         struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
775                         sizeof(tuner_buf) };
776         int tuner_frequency = 0;
777         u8 band, cp, filter;
778
779         // determine charge pump
780         tuner_frequency = params->frequency + 36166000;
781         if (tuner_frequency < 87000000)
782                 return -EINVAL;
783         else if (tuner_frequency < 130000000)
784                 cp = 3;
785         else if (tuner_frequency < 160000000)
786                 cp = 5;
787         else if (tuner_frequency < 200000000)
788                 cp = 6;
789         else if (tuner_frequency < 290000000)
790                 cp = 3;
791         else if (tuner_frequency < 420000000)
792                 cp = 5;
793         else if (tuner_frequency < 480000000)
794                 cp = 6;
795         else if (tuner_frequency < 620000000)
796                 cp = 3;
797         else if (tuner_frequency < 830000000)
798                 cp = 5;
799         else if (tuner_frequency < 895000000)
800                 cp = 7;
801         else
802                 return -EINVAL;
803
804         // determine band
805         if (params->frequency < 49000000)
806                 return -EINVAL;
807         else if (params->frequency < 161000000)
808                 band = 1;
809         else if (params->frequency < 444000000)
810                 band = 2;
811         else if (params->frequency < 861000000)
812                 band = 4;
813         else
814                 return -EINVAL;
815
816         // setup PLL filter
817         switch (params->u.ofdm.bandwidth) {
818         case BANDWIDTH_6_MHZ:
819                 filter = 0;
820                 break;
821
822         case BANDWIDTH_7_MHZ:
823                 filter = 0;
824                 break;
825
826         case BANDWIDTH_8_MHZ:
827                 filter = 1;
828                 break;
829
830         default:
831                 return -EINVAL;
832         }
833
834         // calculate divisor
835         // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
836         tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
837
838         // setup tuner buffer
839         tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
840         tuner_buf[1] = tuner_frequency & 0xff;
841         tuner_buf[2] = 0xca;
842         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
843
844         if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
845                 return -EIO;
846
847         msleep(1);
848         return 0;
849 }
850
851 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
852                                            const struct firmware **fw, char *name)
853 {
854         struct budget *budget = (struct budget *) fe->dvb->priv;
855
856         return request_firmware(fw, name, &budget->dev->pci->dev);
857 }
858
859 static struct tda1004x_config philips_tu1216_config = {
860
861         .demod_address = 0x8,
862         .invert = 1,
863         .invert_oclk = 1,
864         .xtal_freq = TDA10046_XTAL_4M,
865         .agc_config = TDA10046_AGC_DEFAULT,
866         .if_freq = TDA10046_FREQ_3617,
867         .pll_init = philips_tu1216_pll_init,
868         .pll_set = philips_tu1216_pll_set,
869         .pll_sleep = NULL,
870         .request_firmware = philips_tu1216_request_firmware,
871 };
872
873 static u8 philips_sd1878_inittab[] = {
874         0x01, 0x15,
875         0x02, 0x30,
876         0x03, 0x00,
877         0x04, 0x7d,
878         0x05, 0x35,
879         0x06, 0x40,
880         0x07, 0x00,
881         0x08, 0x43,
882         0x09, 0x02,
883         0x0C, 0x51,
884         0x0D, 0x82,
885         0x0E, 0x23,
886         0x10, 0x3f,
887         0x11, 0x84,
888         0x12, 0xb9,
889         0x15, 0xc9,
890         0x16, 0x19,
891         0x17, 0x8c,
892         0x18, 0x59,
893         0x19, 0xf8,
894         0x1a, 0xfe,
895         0x1c, 0x7f,
896         0x1d, 0x00,
897         0x1e, 0x00,
898         0x1f, 0x50,
899         0x20, 0x00,
900         0x21, 0x00,
901         0x22, 0x00,
902         0x23, 0x00,
903         0x28, 0x00,
904         0x29, 0x28,
905         0x2a, 0x14,
906         0x2b, 0x0f,
907         0x2c, 0x09,
908         0x2d, 0x09,
909         0x31, 0x1f,
910         0x32, 0x19,
911         0x33, 0xfc,
912         0x34, 0x93,
913         0xff, 0xff
914 };
915
916 static int philips_sd1878_tda8261_pll_set(struct dvb_frontend *fe,
917                 struct i2c_adapter *i2c,
918                 struct dvb_frontend_parameters *params)
919 {
920         u8              buf[4];
921         int             rc;
922         struct i2c_msg  tuner_msg = {.addr=0x60,.flags=0,.buf=buf,.len=sizeof(buf)};
923
924         if((params->frequency < 950000) || (params->frequency > 2150000))
925                 return -EINVAL;
926
927         rc=dvb_pll_configure(&dvb_pll_philips_sd1878_tda8261, buf,
928                         params->frequency, 0);
929         if(rc < 0) return rc;
930
931         if(i2c_transfer(i2c, &tuner_msg, 1) != 1)
932                 return -EIO;
933
934     return 0;
935 }
936
937 static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
938                 u32 srate, u32 ratio)
939 {
940         u8 aclk = 0;
941         u8 bclk = 0;
942         u8 m1;
943
944         aclk = 0xb5;
945         if (srate < 2000000)
946                 bclk = 0x86;
947         else if (srate < 5000000)
948                 bclk = 0x89;
949         else if (srate < 15000000)
950                 bclk = 0x8f;
951         else if (srate < 45000000)
952                 bclk = 0x95;
953
954         m1 = 0x14;
955         if (srate < 4000000)
956                 m1 = 0x10;
957
958         stv0299_writereg(fe, 0x0e, 0x23);
959         stv0299_writereg(fe, 0x0f, 0x94);
960         stv0299_writereg(fe, 0x10, 0x39);
961         stv0299_writereg(fe, 0x13, aclk);
962         stv0299_writereg(fe, 0x14, bclk);
963         stv0299_writereg(fe, 0x15, 0xc9);
964         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
965         stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
966         stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
967         stv0299_writereg(fe, 0x0f, 0x80 | m1);
968
969         return 0;
970 }
971
972 static struct stv0299_config philips_sd1878_config = {
973         .demod_address = 0x68,
974         .inittab = philips_sd1878_inittab,
975         .mclk = 88000000UL,
976         .invert = 0,
977         .skip_reinit = 0,
978         .lock_output = STV0229_LOCKOUTPUT_1,
979         .volt13_op0_op1 = STV0299_VOLT13_OP0,
980         .min_delay_ms = 100,
981         .set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
982         .pll_set = philips_sd1878_tda8261_pll_set,
983 };
984
985 static u8 read_pwm(struct budget_av *budget_av)
986 {
987         u8 b = 0xff;
988         u8 pwm;
989         struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
990         {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
991         };
992
993         if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
994             || (pwm == 0xff))
995                 pwm = 0x48;
996
997         return pwm;
998 }
999
1000 #define SUBID_DVBS_KNC1         0x0010
1001 #define SUBID_DVBS_KNC1_PLUS    0x0011
1002 #define SUBID_DVBS_TYPHOON      0x4f56
1003 #define SUBID_DVBS_CINERGY1200  0x1154
1004 #define SUBID_DVBS_CYNERGY1200N 0x1155
1005
1006 #define SUBID_DVBS_TV_STAR      0x0014
1007 #define SUBID_DVBS_TV_STAR_CI   0x0016
1008 #define SUBID_DVBS_EASYWATCH    0x001e
1009 #define SUBID_DVBC_KNC1         0x0020
1010 #define SUBID_DVBC_KNC1_PLUS    0x0021
1011 #define SUBID_DVBC_CINERGY1200  0x1156
1012
1013 #define SUBID_DVBT_KNC1_PLUS    0x0031
1014 #define SUBID_DVBT_KNC1         0x0030
1015 #define SUBID_DVBT_CINERGY1200  0x1157
1016
1017 static void frontend_init(struct budget_av *budget_av)
1018 {
1019         struct saa7146_dev * saa = budget_av->budget.dev;
1020         struct dvb_frontend * fe = NULL;
1021
1022         /* Enable / PowerON Frontend */
1023         saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
1024
1025         switch (saa->pci->subsystem_device) {
1026                 case SUBID_DVBS_KNC1_PLUS:
1027                 case SUBID_DVBC_KNC1_PLUS:
1028                 case SUBID_DVBT_KNC1_PLUS:
1029                         saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
1030                         break;
1031         }
1032
1033         switch (saa->pci->subsystem_device) {
1034
1035         case SUBID_DVBS_KNC1:
1036                 if (saa->pci->subsystem_vendor == 0x1894) {
1037                         fe = stv0299_attach(&cinergy_1200s_1894_0010_config,
1038                                              &budget_av->budget.i2c_adap);
1039                 } else {
1040                         fe = stv0299_attach(&typhoon_config,
1041                                              &budget_av->budget.i2c_adap);
1042                 }
1043                 break;
1044
1045         case SUBID_DVBS_TV_STAR:
1046         case SUBID_DVBS_TV_STAR_CI:
1047         case SUBID_DVBS_CYNERGY1200N:
1048         case SUBID_DVBS_EASYWATCH:
1049                 fe = stv0299_attach(&philips_sd1878_config,
1050                                 &budget_av->budget.i2c_adap);
1051                 break;
1052
1053         case SUBID_DVBS_KNC1_PLUS:
1054         case SUBID_DVBS_TYPHOON:
1055                 fe = stv0299_attach(&typhoon_config,
1056                                     &budget_av->budget.i2c_adap);
1057                 break;
1058
1059         case SUBID_DVBS_CINERGY1200:
1060                 fe = stv0299_attach(&cinergy_1200s_config,
1061                                     &budget_av->budget.i2c_adap);
1062                 break;
1063
1064         case SUBID_DVBC_KNC1:
1065                 budget_av->reinitialise_demod = 1;
1066                 fe = tda10021_attach(&philips_cu1216_config,
1067                                      &budget_av->budget.i2c_adap,
1068                                      read_pwm(budget_av));
1069                 break;
1070
1071         case SUBID_DVBC_KNC1_PLUS:
1072                 budget_av->reinitialise_demod = 1;
1073                 fe = tda10021_attach(&philips_cu1216_config,
1074                                      &budget_av->budget.i2c_adap,
1075                                      read_pwm(budget_av));
1076                 break;
1077
1078         case SUBID_DVBT_KNC1:
1079         case SUBID_DVBT_KNC1_PLUS:
1080                 fe = tda10046_attach(&philips_tu1216_config,
1081                                      &budget_av->budget.i2c_adap);
1082                 break;
1083
1084         case SUBID_DVBC_CINERGY1200:
1085                 fe = tda10021_attach(&philips_cu1216_config,
1086                                      &budget_av->budget.i2c_adap,
1087                                      read_pwm(budget_av));
1088                 break;
1089
1090         case SUBID_DVBT_CINERGY1200:
1091                 fe = tda10046_attach(&philips_tu1216_config,
1092                                      &budget_av->budget.i2c_adap);
1093                 break;
1094         }
1095
1096         if (fe == NULL) {
1097                 printk(KERN_ERR "budget-av: A frontend driver was not found "
1098                                 "for device %04x/%04x subsystem %04x/%04x\n",
1099                        saa->pci->vendor,
1100                        saa->pci->device,
1101                        saa->pci->subsystem_vendor,
1102                        saa->pci->subsystem_device);
1103                 return;
1104         }
1105
1106         budget_av->budget.dvb_frontend = fe;
1107
1108         if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
1109                                   budget_av->budget.dvb_frontend)) {
1110                 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
1111                 if (budget_av->budget.dvb_frontend->ops->release)
1112                         budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
1113                 budget_av->budget.dvb_frontend = NULL;
1114         }
1115 }
1116
1117
1118 static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
1119 {
1120         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1121
1122         dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
1123
1124         if (*isr & MASK_10)
1125                 ttpci_budget_irq10_handler(dev, isr);
1126 }
1127
1128 static int budget_av_detach(struct saa7146_dev *dev)
1129 {
1130         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1131         int err;
1132
1133         dprintk(2, "dev: %p\n", dev);
1134
1135         if (1 == budget_av->has_saa7113) {
1136                 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1137
1138                 msleep(200);
1139
1140                 saa7146_unregister_device(&budget_av->vd, dev);
1141         }
1142
1143         if (budget_av->budget.ci_present)
1144                 ciintf_deinit(budget_av);
1145
1146         if (budget_av->budget.dvb_frontend != NULL)
1147                 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
1148         err = ttpci_budget_deinit(&budget_av->budget);
1149
1150         kfree(budget_av);
1151
1152         return err;
1153 }
1154
1155 static struct saa7146_ext_vv vv_data;
1156
1157 static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1158 {
1159         struct budget_av *budget_av;
1160         u8 *mac;
1161         int err;
1162
1163         dprintk(2, "dev: %p\n", dev);
1164
1165         if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
1166                 return -ENOMEM;
1167
1168         budget_av->has_saa7113 = 0;
1169         budget_av->budget.ci_present = 0;
1170
1171         dev->ext_priv = budget_av;
1172
1173         if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
1174                 kfree(budget_av);
1175                 return err;
1176         }
1177
1178         /* knc1 initialization */
1179         saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1180         saa7146_write(dev, DD1_INIT, 0x07000600);
1181         saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1182
1183         if (saa7113_init(budget_av) == 0) {
1184                 budget_av->has_saa7113 = 1;
1185
1186                 if (0 != saa7146_vv_init(dev, &vv_data)) {
1187                         /* fixme: proper cleanup here */
1188                         ERR(("cannot init vv subsystem.\n"));
1189                         return err;
1190                 }
1191
1192                 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1193                         /* fixme: proper cleanup here */
1194                         ERR(("cannot register capture v4l2 device.\n"));
1195                         return err;
1196                 }
1197
1198                 /* beware: this modifies dev->vv ... */
1199                 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1200                                                 SAA7146_HPS_SYNC_PORT_A);
1201
1202                 saa7113_setinput(budget_av, 0);
1203         }
1204
1205         /* fixme: find some sane values here... */
1206         saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1207
1208         mac = budget_av->budget.dvb_adapter.proposed_mac;
1209         if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
1210                 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
1211                        budget_av->budget.dvb_adapter.num);
1212                 memset(mac, 0, 6);
1213         } else {
1214                 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1215                        budget_av->budget.dvb_adapter.num,
1216                        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1217         }
1218
1219         budget_av->budget.dvb_adapter.priv = budget_av;
1220         frontend_init(budget_av);
1221         ciintf_init(budget_av);
1222         return 0;
1223 }
1224
1225 #define KNC1_INPUTS 2
1226 static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1227         {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1228         {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1229 };
1230
1231 static struct saa7146_extension_ioctls ioctls[] = {
1232         {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
1233         {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
1234         {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
1235         {0, 0}
1236 };
1237
1238 static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
1239 {
1240         struct saa7146_dev *dev = fh->dev;
1241         struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1242
1243         switch (cmd) {
1244         case VIDIOC_ENUMINPUT:{
1245                 struct v4l2_input *i = arg;
1246
1247                 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1248                 if (i->index < 0 || i->index >= KNC1_INPUTS) {
1249                         return -EINVAL;
1250                 }
1251                 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1252                 return 0;
1253         }
1254         case VIDIOC_G_INPUT:{
1255                 int *input = (int *) arg;
1256
1257                 *input = budget_av->cur_input;
1258
1259                 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
1260                 return 0;
1261         }
1262         case VIDIOC_S_INPUT:{
1263                 int input = *(int *) arg;
1264                 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1265                 return saa7113_setinput(budget_av, input);
1266         }
1267         default:
1268                 return -ENOIOCTLCMD;
1269         }
1270         return 0;
1271 }
1272
1273 static struct saa7146_standard standard[] = {
1274         {.name = "PAL",.id = V4L2_STD_PAL,
1275          .v_offset = 0x17,.v_field = 288,
1276          .h_offset = 0x14,.h_pixels = 680,
1277          .v_max_out = 576,.h_max_out = 768 },
1278
1279         {.name = "NTSC",.id = V4L2_STD_NTSC,
1280          .v_offset = 0x16,.v_field = 240,
1281          .h_offset = 0x06,.h_pixels = 708,
1282          .v_max_out = 480,.h_max_out = 640, },
1283 };
1284
1285 static struct saa7146_ext_vv vv_data = {
1286         .inputs = 2,
1287         .capabilities = 0,      // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1288         .flags = 0,
1289         .stds = &standard[0],
1290         .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
1291         .ioctls = &ioctls[0],
1292         .ioctl = av_ioctl,
1293 };
1294
1295 static struct saa7146_extension budget_extension;
1296
1297 MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1298 MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1299 MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
1300 MAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
1301 MAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
1302 MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1303 MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1304 MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1305 MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1306 MAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1307 MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1308 MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1309
1310 static struct pci_device_id pci_tbl[] = {
1311         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1312         MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1313         MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
1314         MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1315         MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
1316         MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
1317         MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
1318         MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1319         MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1320         MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1321         MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1322         MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1323         MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
1324         MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1325         MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1326         {
1327          .vendor = 0,
1328         }
1329 };
1330
1331 MODULE_DEVICE_TABLE(pci, pci_tbl);
1332
1333 static struct saa7146_extension budget_extension = {
1334         .name = "budget_av",
1335         .flags = SAA7146_I2C_SHORT_DELAY,
1336
1337         .pci_tbl = pci_tbl,
1338
1339         .module = THIS_MODULE,
1340         .attach = budget_av_attach,
1341         .detach = budget_av_detach,
1342
1343         .irq_mask = MASK_10,
1344         .irq_func = budget_av_irq,
1345 };
1346
1347 static int __init budget_av_init(void)
1348 {
1349         return saa7146_register_extension(&budget_extension);
1350 }
1351
1352 static void __exit budget_av_exit(void)
1353 {
1354         saa7146_unregister_extension(&budget_extension);
1355 }
1356
1357 module_init(budget_av_init);
1358 module_exit(budget_av_exit);
1359
1360 MODULE_LICENSE("GPL");
1361 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1362 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1363                    "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");