Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[linux-2.6] / drivers / media / dvb / ttpci / av7110.c
1 /*
2  * driver for the SAA7146 based AV110 cards (like the Fujitsu-Siemens DVB)
3  * av7110.c: initialization and demux stuff
4  *
5  * Copyright (C) 1999-2002 Ralph  Metzler
6  *                       & Marcus Metzler for convergence integrated media GmbH
7  *
8  * originally based on code by:
9  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
27  *
28  *
29  * the project's page is at http://www.linuxtv.org/dvb/
30  */
31
32
33 #include <linux/module.h>
34 #include <linux/kmod.h>
35 #include <linux/delay.h>
36 #include <linux/fs.h>
37 #include <linux/timer.h>
38 #include <linux/poll.h>
39 #include <linux/byteorder/swabb.h>
40 #include <linux/smp_lock.h>
41
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/types.h>
45 #include <linux/fcntl.h>
46 #include <linux/interrupt.h>
47 #include <linux/string.h>
48 #include <linux/pci.h>
49 #include <linux/vmalloc.h>
50 #include <linux/firmware.h>
51 #include <linux/crc32.h>
52 #include <linux/i2c.h>
53 #include <linux/kthread.h>
54
55 #include <asm/system.h>
56
57 #include <linux/dvb/frontend.h>
58
59 #include "dvb_frontend.h"
60
61 #include "ttpci-eeprom.h"
62 #include "av7110.h"
63 #include "av7110_hw.h"
64 #include "av7110_av.h"
65 #include "av7110_ca.h"
66 #include "av7110_ipack.h"
67
68 #include "bsbe1.h"
69 #include "lnbp21.h"
70 #include "bsru6.h"
71
72 #define TS_WIDTH  376
73 #define TS_HEIGHT 512
74 #define TS_BUFLEN (TS_WIDTH*TS_HEIGHT)
75 #define TS_MAX_PACKETS (TS_BUFLEN/TS_SIZE)
76
77
78 int av7110_debug;
79
80 static int vidmode = CVBS_RGB_OUT;
81 static int pids_off;
82 static int adac = DVB_ADAC_TI;
83 static int hw_sections;
84 static int rgb_on;
85 static int volume = 255;
86 static int budgetpatch;
87 static int wss_cfg_4_3 = 0x4008;
88 static int wss_cfg_16_9 = 0x0007;
89 static int tv_standard;
90
91 module_param_named(debug, av7110_debug, int, 0644);
92 MODULE_PARM_DESC(debug, "debug level (bitmask, default 0)");
93 module_param(vidmode, int, 0444);
94 MODULE_PARM_DESC(vidmode,"analog video out: 0 off, 1 CVBS+RGB (default), 2 CVBS+YC, 3 YC");
95 module_param(pids_off, int, 0444);
96 MODULE_PARM_DESC(pids_off,"clear video/audio/PCR PID filters when demux is closed");
97 module_param(adac, int, 0444);
98 MODULE_PARM_DESC(adac,"audio DAC type: 0 TI, 1 CRYSTAL, 2 MSP (use if autodetection fails)");
99 module_param(hw_sections, int, 0444);
100 MODULE_PARM_DESC(hw_sections, "0 use software section filter, 1 use hardware");
101 module_param(rgb_on, int, 0444);
102 MODULE_PARM_DESC(rgb_on, "For Siemens DVB-C cards only: Enable RGB control"
103                 " signal on SCART pin 16 to switch SCART video mode from CVBS to RGB");
104 module_param(volume, int, 0444);
105 MODULE_PARM_DESC(volume, "initial volume: default 255 (range 0-255)");
106 module_param(budgetpatch, int, 0444);
107 MODULE_PARM_DESC(budgetpatch, "use budget-patch hardware modification: default 0 (0 no, 1 autodetect, 2 always)");
108 module_param(wss_cfg_4_3, int, 0444);
109 MODULE_PARM_DESC(wss_cfg_4_3, "WSS 4:3 - default 0x4008 - bit 15: disable, 14: burst mode, 13..0: wss data");
110 module_param(wss_cfg_16_9, int, 0444);
111 MODULE_PARM_DESC(wss_cfg_16_9, "WSS 16:9 - default 0x0007 - bit 15: disable, 14: burst mode, 13..0: wss data");
112 module_param(tv_standard, int, 0444);
113 MODULE_PARM_DESC(tv_standard, "TV standard: 0 PAL (default), 1 NTSC");
114
115 static void restart_feeds(struct av7110 *av7110);
116
117 static int av7110_num;
118
119 #define FE_FUNC_OVERRIDE(fe_func, av7110_copy, av7110_func) \
120 {\
121         if (fe_func != NULL) { \
122                 av7110_copy = fe_func; \
123                 fe_func = av7110_func; \
124         } \
125 }
126
127
128 static void init_av7110_av(struct av7110 *av7110)
129 {
130         int ret;
131         struct saa7146_dev *dev = av7110->dev;
132
133         /* set internal volume control to maximum */
134         av7110->adac_type = DVB_ADAC_TI;
135         ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
136         if (ret < 0)
137                 printk("dvb-ttpci:cannot set internal volume to maximum:%d\n",ret);
138
139         ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
140                             1, (u16) av7110->display_ar);
141         if (ret < 0)
142                 printk("dvb-ttpci: unable to set aspect ratio\n");
143         ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
144                             1, av7110->display_panscan);
145         if (ret < 0)
146                 printk("dvb-ttpci: unable to set pan scan\n");
147
148         ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 2, wss_cfg_4_3);
149         if (ret < 0)
150                 printk("dvb-ttpci: unable to configure 4:3 wss\n");
151         ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 3, wss_cfg_16_9);
152         if (ret < 0)
153                 printk("dvb-ttpci: unable to configure 16:9 wss\n");
154
155         ret = av7710_set_video_mode(av7110, vidmode);
156         if (ret < 0)
157                 printk("dvb-ttpci:cannot set video mode:%d\n",ret);
158
159         /* handle different card types */
160         /* remaining inits according to card and frontend type */
161         av7110->analog_tuner_flags = 0;
162         av7110->current_input = 0;
163         if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000a)
164                 av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 0); // SPDIF on
165         if (i2c_writereg(av7110, 0x20, 0x00, 0x00) == 1) {
166                 printk ("dvb-ttpci: Crystal audio DAC @ card %d detected\n",
167                         av7110->dvb_adapter.num);
168                 av7110->adac_type = DVB_ADAC_CRYSTAL;
169                 i2c_writereg(av7110, 0x20, 0x01, 0xd2);
170                 i2c_writereg(av7110, 0x20, 0x02, 0x49);
171                 i2c_writereg(av7110, 0x20, 0x03, 0x00);
172                 i2c_writereg(av7110, 0x20, 0x04, 0x00);
173
174                 /**
175                  * some special handling for the Siemens DVB-C cards...
176                  */
177         } else if (0 == av7110_init_analog_module(av7110)) {
178                 /* done. */
179         }
180         else if (dev->pci->subsystem_vendor == 0x110a) {
181                 printk("dvb-ttpci: DVB-C w/o analog module @ card %d detected\n",
182                         av7110->dvb_adapter.num);
183                 av7110->adac_type = DVB_ADAC_NONE;
184         }
185         else {
186                 av7110->adac_type = adac;
187                 printk("dvb-ttpci: adac type set to %d @ card %d\n",
188                         av7110->adac_type, av7110->dvb_adapter.num);
189         }
190
191         if (av7110->adac_type == DVB_ADAC_NONE || av7110->adac_type == DVB_ADAC_MSP34x0) {
192                 // switch DVB SCART on
193                 ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, MainSwitch, 1, 0);
194                 if (ret < 0)
195                         printk("dvb-ttpci:cannot switch on SCART(Main):%d\n",ret);
196                 ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 1);
197                 if (ret < 0)
198                         printk("dvb-ttpci:cannot switch on SCART(AD):%d\n",ret);
199                 if (rgb_on &&
200                     ((av7110->dev->pci->subsystem_vendor == 0x110a) ||
201                      (av7110->dev->pci->subsystem_vendor == 0x13c2)) &&
202                      (av7110->dev->pci->subsystem_device == 0x0000)) {
203                         saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // RGB on, SCART pin 16
204                         //saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // SCARTpin 8
205                 }
206         }
207
208         if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000e)
209                 av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, SpdifSwitch, 1, 0); // SPDIF on
210
211         ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
212         if (ret < 0)
213                 printk("dvb-ttpci:cannot set volume :%d\n",ret);
214 }
215
216 static void recover_arm(struct av7110 *av7110)
217 {
218         dprintk(4, "%p\n",av7110);
219
220         av7110_bootarm(av7110);
221         msleep(100);
222
223         init_av7110_av(av7110);
224
225         /* card-specific recovery */
226         if (av7110->recover)
227                 av7110->recover(av7110);
228
229         restart_feeds(av7110);
230
231 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
232         av7110_check_ir_config(av7110, true);
233 #endif
234 }
235
236 static void av7110_arm_sync(struct av7110 *av7110)
237 {
238         if (av7110->arm_thread)
239                 kthread_stop(av7110->arm_thread);
240
241         av7110->arm_thread = NULL;
242 }
243
244 static int arm_thread(void *data)
245 {
246         struct av7110 *av7110 = data;
247         u16 newloops = 0;
248         int timeout;
249
250         dprintk(4, "%p\n",av7110);
251
252         for (;;) {
253                 timeout = wait_event_interruptible_timeout(av7110->arm_wait,
254                         kthread_should_stop(), 5 * HZ);
255
256                 if (-ERESTARTSYS == timeout || kthread_should_stop()) {
257                         /* got signal or told to quit*/
258                         break;
259                 }
260
261                 if (!av7110->arm_ready)
262                         continue;
263
264 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
265                 av7110_check_ir_config(av7110, false);
266 #endif
267
268                 if (mutex_lock_interruptible(&av7110->dcomlock))
269                         break;
270                 newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2);
271                 mutex_unlock(&av7110->dcomlock);
272
273                 if (newloops == av7110->arm_loops || av7110->arm_errors > 3) {
274                         printk(KERN_ERR "dvb-ttpci: ARM crashed @ card %d\n",
275                                av7110->dvb_adapter.num);
276
277                         recover_arm(av7110);
278
279                         if (mutex_lock_interruptible(&av7110->dcomlock))
280                                 break;
281                         newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2) - 1;
282                         mutex_unlock(&av7110->dcomlock);
283                 }
284                 av7110->arm_loops = newloops;
285                 av7110->arm_errors = 0;
286         }
287
288         return 0;
289 }
290
291
292 /****************************************************************************
293  * IRQ handling
294  ****************************************************************************/
295
296 static int DvbDmxFilterCallback(u8 *buffer1, size_t buffer1_len,
297                                 u8 *buffer2, size_t buffer2_len,
298                                 struct dvb_demux_filter *dvbdmxfilter,
299                                 enum dmx_success success,
300                                 struct av7110 *av7110)
301 {
302         if (!dvbdmxfilter->feed->demux->dmx.frontend)
303                 return 0;
304         if (dvbdmxfilter->feed->demux->dmx.frontend->source == DMX_MEMORY_FE)
305                 return 0;
306
307         switch (dvbdmxfilter->type) {
308         case DMX_TYPE_SEC:
309                 if ((((buffer1[1] << 8) | buffer1[2]) & 0xfff) + 3 != buffer1_len)
310                         return 0;
311                 if (dvbdmxfilter->doneq) {
312                         struct dmx_section_filter *filter = &dvbdmxfilter->filter;
313                         int i;
314                         u8 xor, neq = 0;
315
316                         for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
317                                 xor = filter->filter_value[i] ^ buffer1[i];
318                                 neq |= dvbdmxfilter->maskandnotmode[i] & xor;
319                         }
320                         if (!neq)
321                                 return 0;
322                 }
323                 return dvbdmxfilter->feed->cb.sec(buffer1, buffer1_len,
324                                                   buffer2, buffer2_len,
325                                                   &dvbdmxfilter->filter,
326                                                   DMX_OK);
327         case DMX_TYPE_TS:
328                 if (!(dvbdmxfilter->feed->ts_type & TS_PACKET))
329                         return 0;
330                 if (dvbdmxfilter->feed->ts_type & TS_PAYLOAD_ONLY)
331                         return dvbdmxfilter->feed->cb.ts(buffer1, buffer1_len,
332                                                          buffer2, buffer2_len,
333                                                          &dvbdmxfilter->feed->feed.ts,
334                                                          DMX_OK);
335                 else
336                         av7110_p2t_write(buffer1, buffer1_len,
337                                          dvbdmxfilter->feed->pid,
338                                          &av7110->p2t_filter[dvbdmxfilter->index]);
339         default:
340                 return 0;
341         }
342 }
343
344
345 //#define DEBUG_TIMING
346 static inline void print_time(char *s)
347 {
348 #ifdef DEBUG_TIMING
349         struct timeval tv;
350         do_gettimeofday(&tv);
351         printk("%s: %d.%d\n", s, (int)tv.tv_sec, (int)tv.tv_usec);
352 #endif
353 }
354
355 #define DEBI_READ 0
356 #define DEBI_WRITE 1
357 static inline void start_debi_dma(struct av7110 *av7110, int dir,
358                                   unsigned long addr, unsigned int len)
359 {
360         dprintk(8, "%c %08lx %u\n", dir == DEBI_READ ? 'R' : 'W', addr, len);
361         if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
362                 printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __FUNCTION__);
363                 return;
364         }
365
366         SAA7146_ISR_CLEAR(av7110->dev, MASK_19); /* for good measure */
367         SAA7146_IER_ENABLE(av7110->dev, MASK_19);
368         if (len < 5)
369                 len = 5; /* we want a real DEBI DMA */
370         if (dir == DEBI_WRITE)
371                 iwdebi(av7110, DEBISWAB, addr, 0, (len + 3) & ~3);
372         else
373                 irdebi(av7110, DEBISWAB, addr, 0, len);
374 }
375
376 static void debiirq(unsigned long data)
377 {
378         struct av7110 *av7110 = (struct av7110 *) data;
379         int type = av7110->debitype;
380         int handle = (type >> 8) & 0x1f;
381         unsigned int xfer = 0;
382
383         print_time("debi");
384         dprintk(4, "type 0x%04x\n", type);
385
386         if (type == -1) {
387                 printk("DEBI irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
388                        jiffies, saa7146_read(av7110->dev, PSR),
389                        saa7146_read(av7110->dev, SSR));
390                 goto debi_done;
391         }
392         av7110->debitype = -1;
393
394         switch (type & 0xff) {
395
396         case DATA_TS_RECORD:
397                 dvb_dmx_swfilter_packets(&av7110->demux,
398                                          (const u8 *) av7110->debi_virt,
399                                          av7110->debilen / 188);
400                 xfer = RX_BUFF;
401                 break;
402
403         case DATA_PES_RECORD:
404                 if (av7110->demux.recording)
405                         av7110_record_cb(&av7110->p2t[handle],
406                                          (u8 *) av7110->debi_virt,
407                                          av7110->debilen);
408                 xfer = RX_BUFF;
409                 break;
410
411         case DATA_IPMPE:
412         case DATA_FSECTION:
413         case DATA_PIPING:
414                 if (av7110->handle2filter[handle])
415                         DvbDmxFilterCallback((u8 *)av7110->debi_virt,
416                                              av7110->debilen, NULL, 0,
417                                              av7110->handle2filter[handle],
418                                              DMX_OK, av7110);
419                 xfer = RX_BUFF;
420                 break;
421
422         case DATA_CI_GET:
423         {
424                 u8 *data = av7110->debi_virt;
425
426                 if ((data[0] < 2) && data[2] == 0xff) {
427                         int flags = 0;
428                         if (data[5] > 0)
429                                 flags |= CA_CI_MODULE_PRESENT;
430                         if (data[5] > 5)
431                                 flags |= CA_CI_MODULE_READY;
432                         av7110->ci_slot[data[0]].flags = flags;
433                 } else
434                         ci_get_data(&av7110->ci_rbuffer,
435                                     av7110->debi_virt,
436                                     av7110->debilen);
437                 xfer = RX_BUFF;
438                 break;
439         }
440
441         case DATA_COMMON_INTERFACE:
442                 CI_handle(av7110, (u8 *)av7110->debi_virt, av7110->debilen);
443 #if 0
444         {
445                 int i;
446
447                 printk("av7110%d: ", av7110->num);
448                 printk("%02x ", *(u8 *)av7110->debi_virt);
449                 printk("%02x ", *(1+(u8 *)av7110->debi_virt));
450                 for (i = 2; i < av7110->debilen; i++)
451                         printk("%02x ", (*(i+(unsigned char *)av7110->debi_virt)));
452                 for (i = 2; i < av7110->debilen; i++)
453                         printk("%c", chtrans(*(i+(unsigned char *)av7110->debi_virt)));
454
455                 printk("\n");
456         }
457 #endif
458                 xfer = RX_BUFF;
459                 break;
460
461         case DATA_DEBUG_MESSAGE:
462                 ((s8*)av7110->debi_virt)[Reserved_SIZE - 1] = 0;
463                 printk("%s\n", (s8 *) av7110->debi_virt);
464                 xfer = RX_BUFF;
465                 break;
466
467         case DATA_CI_PUT:
468                 dprintk(4, "debi DATA_CI_PUT\n");
469         case DATA_MPEG_PLAY:
470                 dprintk(4, "debi DATA_MPEG_PLAY\n");
471         case DATA_BMP_LOAD:
472                 dprintk(4, "debi DATA_BMP_LOAD\n");
473                 xfer = TX_BUFF;
474                 break;
475         default:
476                 break;
477         }
478 debi_done:
479         spin_lock(&av7110->debilock);
480         if (xfer)
481                 iwdebi(av7110, DEBINOSWAP, xfer, 0, 2);
482         ARM_ClearMailBox(av7110);
483         spin_unlock(&av7110->debilock);
484 }
485
486 /* irq from av7110 firmware writing the mailbox register in the DPRAM */
487 static void gpioirq(unsigned long data)
488 {
489         struct av7110 *av7110 = (struct av7110 *) data;
490         u32 rxbuf, txbuf;
491         int len;
492
493         if (av7110->debitype != -1)
494                 /* we shouldn't get any irq while a debi xfer is running */
495                 printk("dvb-ttpci: GPIO0 irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
496                        jiffies, saa7146_read(av7110->dev, PSR),
497                        saa7146_read(av7110->dev, SSR));
498
499         if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
500                 printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __FUNCTION__);
501                 BUG(); /* maybe we should try resetting the debi? */
502         }
503
504         spin_lock(&av7110->debilock);
505         ARM_ClearIrq(av7110);
506
507         /* see what the av7110 wants */
508         av7110->debitype = irdebi(av7110, DEBINOSWAP, IRQ_STATE, 0, 2);
509         av7110->debilen  = irdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
510         rxbuf = irdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
511         txbuf = irdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
512         len = (av7110->debilen + 3) & ~3;
513
514         print_time("gpio");
515         dprintk(8, "GPIO0 irq 0x%04x %d\n", av7110->debitype, av7110->debilen);
516
517         switch (av7110->debitype & 0xff) {
518
519         case DATA_TS_PLAY:
520         case DATA_PES_PLAY:
521                 break;
522
523         case DATA_MPEG_VIDEO_EVENT:
524         {
525                 u32 h_ar;
526                 struct video_event event;
527
528                 av7110->video_size.w = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_WIDTH, 0, 2);
529                 h_ar = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_HEIGHT_AR, 0, 2);
530
531                 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
532                 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
533
534                 av7110->video_size.h = h_ar & 0xfff;
535
536                 event.type = VIDEO_EVENT_SIZE_CHANGED;
537                 event.u.size.w = av7110->video_size.w;
538                 event.u.size.h = av7110->video_size.h;
539                 switch ((h_ar >> 12) & 0xf)
540                 {
541                 case 3:
542                         av7110->video_size.aspect_ratio = VIDEO_FORMAT_16_9;
543                         event.u.size.aspect_ratio = VIDEO_FORMAT_16_9;
544                         av7110->videostate.video_format = VIDEO_FORMAT_16_9;
545                         break;
546                 case 4:
547                         av7110->video_size.aspect_ratio = VIDEO_FORMAT_221_1;
548                         event.u.size.aspect_ratio = VIDEO_FORMAT_221_1;
549                         av7110->videostate.video_format = VIDEO_FORMAT_221_1;
550                         break;
551                 default:
552                         av7110->video_size.aspect_ratio = VIDEO_FORMAT_4_3;
553                         event.u.size.aspect_ratio = VIDEO_FORMAT_4_3;
554                         av7110->videostate.video_format = VIDEO_FORMAT_4_3;
555                 }
556
557                 dprintk(8, "GPIO0 irq: DATA_MPEG_VIDEO_EVENT: w/h/ar = %u/%u/%u\n",
558                         av7110->video_size.w, av7110->video_size.h,
559                         av7110->video_size.aspect_ratio);
560
561                 dvb_video_add_event(av7110, &event);
562                 break;
563         }
564
565         case DATA_CI_PUT:
566         {
567                 int avail;
568                 struct dvb_ringbuffer *cibuf = &av7110->ci_wbuffer;
569
570                 avail = dvb_ringbuffer_avail(cibuf);
571                 if (avail <= 2) {
572                         iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
573                         iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
574                         iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
575                         break;
576                 }
577                 len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
578                 len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
579                 if (avail < len + 2) {
580                         iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
581                         iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
582                         iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
583                         break;
584                 }
585                 DVB_RINGBUFFER_SKIP(cibuf, 2);
586
587                 dvb_ringbuffer_read(cibuf, av7110->debi_virt, len, 0);
588
589                 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
590                 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
591                 dprintk(8, "DMA: CI\n");
592                 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
593                 spin_unlock(&av7110->debilock);
594                 wake_up(&cibuf->queue);
595                 return;
596         }
597
598         case DATA_MPEG_PLAY:
599                 if (!av7110->playing) {
600                         iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
601                         iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
602                         iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
603                         break;
604                 }
605                 len = 0;
606                 if (av7110->debitype & 0x100) {
607                         spin_lock(&av7110->aout.lock);
608                         len = av7110_pes_play(av7110->debi_virt, &av7110->aout, 2048);
609                         spin_unlock(&av7110->aout.lock);
610                 }
611                 if (len <= 0 && (av7110->debitype & 0x200)
612                     &&av7110->videostate.play_state != VIDEO_FREEZED) {
613                         spin_lock(&av7110->avout.lock);
614                         len = av7110_pes_play(av7110->debi_virt, &av7110->avout, 2048);
615                         spin_unlock(&av7110->avout.lock);
616                 }
617                 if (len <= 0) {
618                         iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
619                         iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
620                         iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
621                         break;
622                 }
623                 dprintk(8, "GPIO0 PES_PLAY len=%04x\n", len);
624                 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
625                 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
626                 dprintk(8, "DMA: MPEG_PLAY\n");
627                 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
628                 spin_unlock(&av7110->debilock);
629                 return;
630
631         case DATA_BMP_LOAD:
632                 len = av7110->debilen;
633                 dprintk(8, "gpio DATA_BMP_LOAD len %d\n", len);
634                 if (!len) {
635                         av7110->bmp_state = BMP_LOADED;
636                         iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
637                         iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
638                         iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
639                         wake_up(&av7110->bmpq);
640                         dprintk(8, "gpio DATA_BMP_LOAD done\n");
641                         break;
642                 }
643                 if (len > av7110->bmplen)
644                         len = av7110->bmplen;
645                 if (len > 2 * 1024)
646                         len = 2 * 1024;
647                 iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
648                 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
649                 memcpy(av7110->debi_virt, av7110->bmpbuf+av7110->bmpp, len);
650                 av7110->bmpp += len;
651                 av7110->bmplen -= len;
652                 dprintk(8, "gpio DATA_BMP_LOAD DMA len %d\n", len);
653                 start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE+txbuf, len);
654                 spin_unlock(&av7110->debilock);
655                 return;
656
657         case DATA_CI_GET:
658         case DATA_COMMON_INTERFACE:
659         case DATA_FSECTION:
660         case DATA_IPMPE:
661         case DATA_PIPING:
662                 if (!len || len > 4 * 1024) {
663                         iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
664                         break;
665                 }
666                 /* fall through */
667
668         case DATA_TS_RECORD:
669         case DATA_PES_RECORD:
670                 dprintk(8, "DMA: TS_REC etc.\n");
671                 start_debi_dma(av7110, DEBI_READ, DPRAM_BASE+rxbuf, len);
672                 spin_unlock(&av7110->debilock);
673                 return;
674
675         case DATA_DEBUG_MESSAGE:
676                 if (!len || len > 0xff) {
677                         iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
678                         break;
679                 }
680                 start_debi_dma(av7110, DEBI_READ, Reserved, len);
681                 spin_unlock(&av7110->debilock);
682                 return;
683
684         case DATA_IRCOMMAND:
685                 if (av7110->ir.ir_handler)
686                         av7110->ir.ir_handler(av7110,
687                                 swahw32(irdebi(av7110, DEBINOSWAP, Reserved, 0, 4)));
688                 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
689                 break;
690
691         default:
692                 printk("dvb-ttpci: gpioirq unknown type=%d len=%d\n",
693                        av7110->debitype, av7110->debilen);
694                 break;
695         }
696         av7110->debitype = -1;
697         ARM_ClearMailBox(av7110);
698         spin_unlock(&av7110->debilock);
699 }
700
701
702 #ifdef CONFIG_DVB_AV7110_OSD
703 static int dvb_osd_ioctl(struct inode *inode, struct file *file,
704                          unsigned int cmd, void *parg)
705 {
706         struct dvb_device *dvbdev = file->private_data;
707         struct av7110 *av7110 = dvbdev->priv;
708
709         dprintk(4, "%p\n", av7110);
710
711         if (cmd == OSD_SEND_CMD)
712                 return av7110_osd_cmd(av7110, (osd_cmd_t *) parg);
713         if (cmd == OSD_GET_CAPABILITY)
714                 return av7110_osd_capability(av7110, (osd_cap_t *) parg);
715
716         return -EINVAL;
717 }
718
719
720 static struct file_operations dvb_osd_fops = {
721         .owner          = THIS_MODULE,
722         .ioctl          = dvb_generic_ioctl,
723         .open           = dvb_generic_open,
724         .release        = dvb_generic_release,
725 };
726
727 static struct dvb_device dvbdev_osd = {
728         .priv           = NULL,
729         .users          = 1,
730         .writers        = 1,
731         .fops           = &dvb_osd_fops,
732         .kernel_ioctl   = dvb_osd_ioctl,
733 };
734 #endif /* CONFIG_DVB_AV7110_OSD */
735
736
737 static inline int SetPIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
738                           u16 subpid, u16 pcrpid)
739 {
740         u16 aflags = 0;
741
742         dprintk(4, "%p\n", av7110);
743
744         if (vpid == 0x1fff || apid == 0x1fff ||
745             ttpid == 0x1fff || subpid == 0x1fff || pcrpid == 0x1fff) {
746                 vpid = apid = ttpid = subpid = pcrpid = 0;
747                 av7110->pids[DMX_PES_VIDEO] = 0;
748                 av7110->pids[DMX_PES_AUDIO] = 0;
749                 av7110->pids[DMX_PES_TELETEXT] = 0;
750                 av7110->pids[DMX_PES_PCR] = 0;
751         }
752
753         if (av7110->audiostate.bypass_mode)
754                 aflags |= 0x8000;
755
756         return av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, MultiPID, 6,
757                              pcrpid, vpid, apid, ttpid, subpid, aflags);
758 }
759
760 int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
761                 u16 subpid, u16 pcrpid)
762 {
763         int ret = 0;
764         dprintk(4, "%p\n", av7110);
765
766         if (mutex_lock_interruptible(&av7110->pid_mutex))
767                 return -ERESTARTSYS;
768
769         if (!(vpid & 0x8000))
770                 av7110->pids[DMX_PES_VIDEO] = vpid;
771         if (!(apid & 0x8000))
772                 av7110->pids[DMX_PES_AUDIO] = apid;
773         if (!(ttpid & 0x8000))
774                 av7110->pids[DMX_PES_TELETEXT] = ttpid;
775         if (!(pcrpid & 0x8000))
776                 av7110->pids[DMX_PES_PCR] = pcrpid;
777
778         av7110->pids[DMX_PES_SUBTITLE] = 0;
779
780         if (av7110->fe_synced) {
781                 pcrpid = av7110->pids[DMX_PES_PCR];
782                 ret = SetPIDs(av7110, vpid, apid, ttpid, subpid, pcrpid);
783         }
784
785         mutex_unlock(&av7110->pid_mutex);
786         return ret;
787 }
788
789
790 /******************************************************************************
791  * hardware filter functions
792  ******************************************************************************/
793
794 static int StartHWFilter(struct dvb_demux_filter *dvbdmxfilter)
795 {
796         struct dvb_demux_feed *dvbdmxfeed = dvbdmxfilter->feed;
797         struct av7110 *av7110 = dvbdmxfeed->demux->priv;
798         u16 buf[20];
799         int ret, i;
800         u16 handle;
801 //      u16 mode = 0x0320;
802         u16 mode = 0xb96a;
803
804         dprintk(4, "%p\n", av7110);
805
806         if (dvbdmxfilter->type == DMX_TYPE_SEC) {
807                 if (hw_sections) {
808                         buf[4] = (dvbdmxfilter->filter.filter_value[0] << 8) |
809                                 dvbdmxfilter->maskandmode[0];
810                         for (i = 3; i < 18; i++)
811                                 buf[i + 4 - 2] =
812                                         (dvbdmxfilter->filter.filter_value[i] << 8) |
813                                         dvbdmxfilter->maskandmode[i];
814                         mode = 4;
815                 }
816         } else if ((dvbdmxfeed->ts_type & TS_PACKET) &&
817                    !(dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)) {
818                 av7110_p2t_init(&av7110->p2t_filter[dvbdmxfilter->index], dvbdmxfeed);
819         }
820
821         buf[0] = (COMTYPE_PID_FILTER << 8) + AddPIDFilter;
822         buf[1] = 16;
823         buf[2] = dvbdmxfeed->pid;
824         buf[3] = mode;
825
826         ret = av7110_fw_request(av7110, buf, 20, &handle, 1);
827         if (ret != 0 || handle >= 32) {
828                 printk("dvb-ttpci: %s error  buf %04x %04x %04x %04x  "
829                                 "ret %d  handle %04x\n",
830                                 __FUNCTION__, buf[0], buf[1], buf[2], buf[3],
831                                 ret, handle);
832                 dvbdmxfilter->hw_handle = 0xffff;
833                 if (!ret)
834                         ret = -1;
835                 return ret;
836         }
837
838         av7110->handle2filter[handle] = dvbdmxfilter;
839         dvbdmxfilter->hw_handle = handle;
840
841         return ret;
842 }
843
844 static int StopHWFilter(struct dvb_demux_filter *dvbdmxfilter)
845 {
846         struct av7110 *av7110 = dvbdmxfilter->feed->demux->priv;
847         u16 buf[3];
848         u16 answ[2];
849         int ret;
850         u16 handle;
851
852         dprintk(4, "%p\n", av7110);
853
854         handle = dvbdmxfilter->hw_handle;
855         if (handle >= 32) {
856                 printk("%s tried to stop invalid filter %04x, filter type = %x\n",
857                                 __FUNCTION__, handle, dvbdmxfilter->type);
858                 return -EINVAL;
859         }
860
861         av7110->handle2filter[handle] = NULL;
862
863         buf[0] = (COMTYPE_PID_FILTER << 8) + DelPIDFilter;
864         buf[1] = 1;
865         buf[2] = handle;
866         ret = av7110_fw_request(av7110, buf, 3, answ, 2);
867         if (ret != 0 || answ[1] != handle) {
868                 printk("dvb-ttpci: %s error  cmd %04x %04x %04x  ret %x  "
869                                 "resp %04x %04x  pid %d\n",
870                                 __FUNCTION__, buf[0], buf[1], buf[2], ret,
871                                 answ[0], answ[1], dvbdmxfilter->feed->pid);
872                 if (!ret)
873                         ret = -1;
874         }
875         return ret;
876 }
877
878
879 static int dvb_feed_start_pid(struct dvb_demux_feed *dvbdmxfeed)
880 {
881         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
882         struct av7110 *av7110 = dvbdmx->priv;
883         u16 *pid = dvbdmx->pids, npids[5];
884         int i;
885         int ret = 0;
886
887         dprintk(4, "%p\n", av7110);
888
889         npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
890         i = dvbdmxfeed->pes_type;
891         npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
892         if ((i == 2) && npids[i] && (dvbdmxfeed->ts_type & TS_PACKET)) {
893                 npids[i] = 0;
894                 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
895                 if (!ret)
896                         ret = StartHWFilter(dvbdmxfeed->filter);
897                 return ret;
898         }
899         if (dvbdmxfeed->pes_type <= 2 || dvbdmxfeed->pes_type == 4) {
900                 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
901                 if (ret)
902                         return ret;
903         }
904
905         if (dvbdmxfeed->pes_type < 2 && npids[0])
906                 if (av7110->fe_synced)
907                 {
908                         ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
909                         if (ret)
910                                 return ret;
911                 }
912
913         if ((dvbdmxfeed->ts_type & TS_PACKET)) {
914                 if (dvbdmxfeed->pes_type == 0 && !(dvbdmx->pids[0] & 0x8000))
915                         ret = av7110_av_start_record(av7110, RP_AUDIO, dvbdmxfeed);
916                 if (dvbdmxfeed->pes_type == 1 && !(dvbdmx->pids[1] & 0x8000))
917                         ret = av7110_av_start_record(av7110, RP_VIDEO, dvbdmxfeed);
918         }
919         return ret;
920 }
921
922 static int dvb_feed_stop_pid(struct dvb_demux_feed *dvbdmxfeed)
923 {
924         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
925         struct av7110 *av7110 = dvbdmx->priv;
926         u16 *pid = dvbdmx->pids, npids[5];
927         int i;
928
929         int ret = 0;
930
931         dprintk(4, "%p\n", av7110);
932
933         if (dvbdmxfeed->pes_type <= 1) {
934                 ret = av7110_av_stop(av7110, dvbdmxfeed->pes_type ?  RP_VIDEO : RP_AUDIO);
935                 if (ret)
936                         return ret;
937                 if (!av7110->rec_mode)
938                         dvbdmx->recording = 0;
939                 if (!av7110->playing)
940                         dvbdmx->playing = 0;
941         }
942         npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
943         i = dvbdmxfeed->pes_type;
944         switch (i) {
945         case 2: //teletext
946                 if (dvbdmxfeed->ts_type & TS_PACKET)
947                         ret = StopHWFilter(dvbdmxfeed->filter);
948                 npids[2] = 0;
949                 break;
950         case 0:
951         case 1:
952         case 4:
953                 if (!pids_off)
954                         return 0;
955                 npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
956                 break;
957         }
958         if (!ret)
959                 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
960         return ret;
961 }
962
963 static int av7110_start_feed(struct dvb_demux_feed *feed)
964 {
965         struct dvb_demux *demux = feed->demux;
966         struct av7110 *av7110 = demux->priv;
967         int ret = 0;
968
969         dprintk(4, "%p\n", av7110);
970
971         if (!demux->dmx.frontend)
972                 return -EINVAL;
973
974         if (feed->pid > 0x1fff)
975                 return -EINVAL;
976
977         if (feed->type == DMX_TYPE_TS) {
978                 if ((feed->ts_type & TS_DECODER) &&
979                     (feed->pes_type < DMX_TS_PES_OTHER)) {
980                         switch (demux->dmx.frontend->source) {
981                         case DMX_MEMORY_FE:
982                                 if (feed->ts_type & TS_DECODER)
983                                        if (feed->pes_type < 2 &&
984                                            !(demux->pids[0] & 0x8000) &&
985                                            !(demux->pids[1] & 0x8000)) {
986                                                dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
987                                                dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
988                                                ret = av7110_av_start_play(av7110,RP_AV);
989                                                if (!ret)
990                                                        demux->playing = 1;
991                                         }
992                                 break;
993                         default:
994                                 ret = dvb_feed_start_pid(feed);
995                                 break;
996                         }
997                 } else if ((feed->ts_type & TS_PACKET) &&
998                            (demux->dmx.frontend->source != DMX_MEMORY_FE)) {
999                         ret = StartHWFilter(feed->filter);
1000                 }
1001         }
1002
1003         else if (feed->type == DMX_TYPE_SEC) {
1004                 int i;
1005
1006                 for (i = 0; i < demux->filternum; i++) {
1007                         if (demux->filter[i].state != DMX_STATE_READY)
1008                                 continue;
1009                         if (demux->filter[i].type != DMX_TYPE_SEC)
1010                                 continue;
1011                         if (demux->filter[i].filter.parent != &feed->feed.sec)
1012                                 continue;
1013                         demux->filter[i].state = DMX_STATE_GO;
1014                         if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1015                                 ret = StartHWFilter(&demux->filter[i]);
1016                                 if (ret)
1017                                         break;
1018                         }
1019                 }
1020         }
1021
1022         return ret;
1023 }
1024
1025
1026 static int av7110_stop_feed(struct dvb_demux_feed *feed)
1027 {
1028         struct dvb_demux *demux = feed->demux;
1029         struct av7110 *av7110 = demux->priv;
1030         int i, rc, ret = 0;
1031         dprintk(4, "%p\n", av7110);
1032
1033         if (feed->type == DMX_TYPE_TS) {
1034                 if (feed->ts_type & TS_DECODER) {
1035                         if (feed->pes_type >= DMX_TS_PES_OTHER ||
1036                             !demux->pesfilter[feed->pes_type])
1037                                 return -EINVAL;
1038                         demux->pids[feed->pes_type] |= 0x8000;
1039                         demux->pesfilter[feed->pes_type] = NULL;
1040                 }
1041                 if (feed->ts_type & TS_DECODER &&
1042                     feed->pes_type < DMX_TS_PES_OTHER) {
1043                         ret = dvb_feed_stop_pid(feed);
1044                 } else
1045                         if ((feed->ts_type & TS_PACKET) &&
1046                             (demux->dmx.frontend->source != DMX_MEMORY_FE))
1047                                 ret = StopHWFilter(feed->filter);
1048         }
1049
1050         if (!ret && feed->type == DMX_TYPE_SEC) {
1051                 for (i = 0; i<demux->filternum; i++) {
1052                         if (demux->filter[i].state == DMX_STATE_GO &&
1053                             demux->filter[i].filter.parent == &feed->feed.sec) {
1054                                 demux->filter[i].state = DMX_STATE_READY;
1055                                 if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1056                                         rc = StopHWFilter(&demux->filter[i]);
1057                                         if (!ret)
1058                                                 ret = rc;
1059                                         /* keep going, stop as many filters as possible */
1060                                 }
1061                         }
1062                 }
1063         }
1064
1065         return ret;
1066 }
1067
1068
1069 static void restart_feeds(struct av7110 *av7110)
1070 {
1071         struct dvb_demux *dvbdmx = &av7110->demux;
1072         struct dvb_demux_feed *feed;
1073         int mode;
1074         int i, j;
1075
1076         dprintk(4, "%p\n", av7110);
1077
1078         mode = av7110->playing;
1079         av7110->playing = 0;
1080         av7110->rec_mode = 0;
1081
1082         for (i = 0; i < dvbdmx->feednum; i++) {
1083                 feed = &dvbdmx->feed[i];
1084                 if (feed->state == DMX_STATE_GO) {
1085                         if (feed->type == DMX_TYPE_SEC) {
1086                                 for (j = 0; j < dvbdmx->filternum; j++) {
1087                                         if (dvbdmx->filter[j].type != DMX_TYPE_SEC)
1088                                                 continue;
1089                                         if (dvbdmx->filter[j].filter.parent != &feed->feed.sec)
1090                                                 continue;
1091                                         if (dvbdmx->filter[j].state == DMX_STATE_GO)
1092                                                 dvbdmx->filter[j].state = DMX_STATE_READY;
1093                                 }
1094                         }
1095                         av7110_start_feed(feed);
1096                 }
1097         }
1098
1099         if (mode)
1100                 av7110_av_start_play(av7110, mode);
1101 }
1102
1103 static int dvb_get_stc(struct dmx_demux *demux, unsigned int num,
1104                        uint64_t *stc, unsigned int *base)
1105 {
1106         int ret;
1107         u16 fwstc[4];
1108         u16 tag = ((COMTYPE_REQUEST << 8) + ReqSTC);
1109         struct dvb_demux *dvbdemux;
1110         struct av7110 *av7110;
1111
1112         /* pointer casting paranoia... */
1113         BUG_ON(!demux);
1114         dvbdemux = demux->priv;
1115         BUG_ON(!dvbdemux);
1116         av7110 = dvbdemux->priv;
1117
1118         dprintk(4, "%p\n", av7110);
1119
1120         if (num != 0)
1121                 return -EINVAL;
1122
1123         ret = av7110_fw_request(av7110, &tag, 0, fwstc, 4);
1124         if (ret) {
1125                 printk(KERN_ERR "%s: av7110_fw_request error\n", __FUNCTION__);
1126                 return ret;
1127         }
1128         dprintk(2, "fwstc = %04hx %04hx %04hx %04hx\n",
1129                 fwstc[0], fwstc[1], fwstc[2], fwstc[3]);
1130
1131         *stc =  (((uint64_t) ((fwstc[3] & 0x8000) >> 15)) << 32) |
1132                 (((uint64_t)  fwstc[1]) << 16) | ((uint64_t) fwstc[0]);
1133         *base = 1;
1134
1135         dprintk(4, "stc = %lu\n", (unsigned long)*stc);
1136
1137         return 0;
1138 }
1139
1140
1141 /******************************************************************************
1142  * SEC device file operations
1143  ******************************************************************************/
1144
1145
1146 static int av7110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
1147 {
1148         struct av7110* av7110 = fe->dvb->priv;
1149
1150         switch (tone) {
1151         case SEC_TONE_ON:
1152                 return Set22K(av7110, 1);
1153
1154         case SEC_TONE_OFF:
1155                 return Set22K(av7110, 0);
1156
1157         default:
1158                 return -EINVAL;
1159         }
1160 }
1161
1162 static int av7110_diseqc_send_master_cmd(struct dvb_frontend* fe,
1163                                          struct dvb_diseqc_master_cmd* cmd)
1164 {
1165         struct av7110* av7110 = fe->dvb->priv;
1166
1167         return av7110_diseqc_send(av7110, cmd->msg_len, cmd->msg, -1);
1168 }
1169
1170 static int av7110_diseqc_send_burst(struct dvb_frontend* fe,
1171                                     fe_sec_mini_cmd_t minicmd)
1172 {
1173         struct av7110* av7110 = fe->dvb->priv;
1174
1175         return av7110_diseqc_send(av7110, 0, NULL, minicmd);
1176 }
1177
1178 /* simplified code from budget-core.c */
1179 static int stop_ts_capture(struct av7110 *budget)
1180 {
1181         dprintk(2, "budget: %p\n", budget);
1182
1183         if (--budget->feeding1)
1184                 return budget->feeding1;
1185         saa7146_write(budget->dev, MC1, MASK_20);       /* DMA3 off */
1186         SAA7146_IER_DISABLE(budget->dev, MASK_10);
1187         SAA7146_ISR_CLEAR(budget->dev, MASK_10);
1188         return 0;
1189 }
1190
1191 static int start_ts_capture(struct av7110 *budget)
1192 {
1193         dprintk(2, "budget: %p\n", budget);
1194
1195         if (budget->feeding1)
1196                 return ++budget->feeding1;
1197         memset(budget->grabbing, 0x00, TS_HEIGHT * TS_WIDTH);
1198         budget->tsf = 0xff;
1199         budget->ttbp = 0;
1200         SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
1201         saa7146_write(budget->dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
1202         return ++budget->feeding1;
1203 }
1204
1205 static int budget_start_feed(struct dvb_demux_feed *feed)
1206 {
1207         struct dvb_demux *demux = feed->demux;
1208         struct av7110 *budget = demux->priv;
1209         int status;
1210
1211         dprintk(2, "av7110: %p\n", budget);
1212
1213         spin_lock(&budget->feedlock1);
1214         feed->pusi_seen = 0; /* have a clean section start */
1215         status = start_ts_capture(budget);
1216         spin_unlock(&budget->feedlock1);
1217         return status;
1218 }
1219
1220 static int budget_stop_feed(struct dvb_demux_feed *feed)
1221 {
1222         struct dvb_demux *demux = feed->demux;
1223         struct av7110 *budget = demux->priv;
1224         int status;
1225
1226         dprintk(2, "budget: %p\n", budget);
1227
1228         spin_lock(&budget->feedlock1);
1229         status = stop_ts_capture(budget);
1230         spin_unlock(&budget->feedlock1);
1231         return status;
1232 }
1233
1234 static void vpeirq(unsigned long data)
1235 {
1236         struct av7110 *budget = (struct av7110 *) data;
1237         u8 *mem = (u8 *) (budget->grabbing);
1238         u32 olddma = budget->ttbp;
1239         u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
1240
1241         if (!budgetpatch) {
1242                 printk("av7110.c: vpeirq() called while budgetpatch disabled!"
1243                        " check saa7146 IER register\n");
1244                 BUG();
1245         }
1246         /* nearest lower position divisible by 188 */
1247         newdma -= newdma % 188;
1248
1249         if (newdma >= TS_BUFLEN)
1250                 return;
1251
1252         budget->ttbp = newdma;
1253
1254         if (!budget->feeding1 || (newdma == olddma))
1255                 return;
1256
1257         /* Ensure streamed PCI data is synced to CPU */
1258         pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
1259
1260 #if 0
1261         /* track rps1 activity */
1262         printk("vpeirq: %02x Event Counter 1 0x%04x\n",
1263                mem[olddma],
1264                saa7146_read(budget->dev, EC1R) & 0x3fff);
1265 #endif
1266
1267         if (newdma > olddma)
1268                 /* no wraparound, dump olddma..newdma */
1269                 dvb_dmx_swfilter_packets(&budget->demux1, mem + olddma, (newdma - olddma) / 188);
1270         else {
1271                 /* wraparound, dump olddma..buflen and 0..newdma */
1272                 dvb_dmx_swfilter_packets(&budget->demux1, mem + olddma, (TS_BUFLEN - olddma) / 188);
1273                 dvb_dmx_swfilter_packets(&budget->demux1, mem, newdma / 188);
1274         }
1275 }
1276
1277 static int av7110_register(struct av7110 *av7110)
1278 {
1279         int ret, i;
1280         struct dvb_demux *dvbdemux = &av7110->demux;
1281         struct dvb_demux *dvbdemux1 = &av7110->demux1;
1282
1283         dprintk(4, "%p\n", av7110);
1284
1285         if (av7110->registered)
1286                 return -1;
1287
1288         av7110->registered = 1;
1289
1290         dvbdemux->priv = (void *) av7110;
1291
1292         for (i = 0; i < 32; i++)
1293                 av7110->handle2filter[i] = NULL;
1294
1295         dvbdemux->filternum = 32;
1296         dvbdemux->feednum = 32;
1297         dvbdemux->start_feed = av7110_start_feed;
1298         dvbdemux->stop_feed = av7110_stop_feed;
1299         dvbdemux->write_to_decoder = av7110_write_to_decoder;
1300         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1301                                       DMX_MEMORY_BASED_FILTERING);
1302
1303         dvb_dmx_init(&av7110->demux);
1304         av7110->demux.dmx.get_stc = dvb_get_stc;
1305
1306         av7110->dmxdev.filternum = 32;
1307         av7110->dmxdev.demux = &dvbdemux->dmx;
1308         av7110->dmxdev.capabilities = 0;
1309
1310         dvb_dmxdev_init(&av7110->dmxdev, &av7110->dvb_adapter);
1311
1312         av7110->hw_frontend.source = DMX_FRONTEND_0;
1313
1314         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1315
1316         if (ret < 0)
1317                 return ret;
1318
1319         av7110->mem_frontend.source = DMX_MEMORY_FE;
1320
1321         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1322
1323         if (ret < 0)
1324                 return ret;
1325
1326         ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
1327                                              &av7110->hw_frontend);
1328         if (ret < 0)
1329                 return ret;
1330
1331         av7110_av_register(av7110);
1332         av7110_ca_register(av7110);
1333
1334 #ifdef CONFIG_DVB_AV7110_OSD
1335         dvb_register_device(&av7110->dvb_adapter, &av7110->osd_dev,
1336                             &dvbdev_osd, av7110, DVB_DEVICE_OSD);
1337 #endif
1338
1339         dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net, &dvbdemux->dmx);
1340
1341         if (budgetpatch) {
1342                 /* initialize software demux1 without its own frontend
1343                  * demux1 hardware is connected to frontend0 of demux0
1344                  */
1345                 dvbdemux1->priv = (void *) av7110;
1346
1347                 dvbdemux1->filternum = 256;
1348                 dvbdemux1->feednum = 256;
1349                 dvbdemux1->start_feed = budget_start_feed;
1350                 dvbdemux1->stop_feed = budget_stop_feed;
1351                 dvbdemux1->write_to_decoder = NULL;
1352
1353                 dvbdemux1->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1354                                                DMX_MEMORY_BASED_FILTERING);
1355
1356                 dvb_dmx_init(&av7110->demux1);
1357
1358                 av7110->dmxdev1.filternum = 256;
1359                 av7110->dmxdev1.demux = &dvbdemux1->dmx;
1360                 av7110->dmxdev1.capabilities = 0;
1361
1362                 dvb_dmxdev_init(&av7110->dmxdev1, &av7110->dvb_adapter);
1363
1364                 dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net1, &dvbdemux1->dmx);
1365                 printk("dvb-ttpci: additional demux1 for budget-patch registered\n");
1366         }
1367         return 0;
1368 }
1369
1370
1371 static void dvb_unregister(struct av7110 *av7110)
1372 {
1373         struct dvb_demux *dvbdemux = &av7110->demux;
1374         struct dvb_demux *dvbdemux1 = &av7110->demux1;
1375
1376         dprintk(4, "%p\n", av7110);
1377
1378         if (!av7110->registered)
1379                 return;
1380
1381         if (budgetpatch) {
1382                 dvb_net_release(&av7110->dvb_net1);
1383                 dvbdemux->dmx.close(&dvbdemux1->dmx);
1384                 dvb_dmxdev_release(&av7110->dmxdev1);
1385                 dvb_dmx_release(&av7110->demux1);
1386         }
1387
1388         dvb_net_release(&av7110->dvb_net);
1389
1390         dvbdemux->dmx.close(&dvbdemux->dmx);
1391         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1392         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1393
1394         dvb_dmxdev_release(&av7110->dmxdev);
1395         dvb_dmx_release(&av7110->demux);
1396
1397         if (av7110->fe != NULL) {
1398                 dvb_unregister_frontend(av7110->fe);
1399                 dvb_frontend_detach(av7110->fe);
1400         }
1401         dvb_unregister_device(av7110->osd_dev);
1402         av7110_av_unregister(av7110);
1403         av7110_ca_unregister(av7110);
1404 }
1405
1406
1407 /****************************************************************************
1408  * I2C client commands
1409  ****************************************************************************/
1410
1411 int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val)
1412 {
1413         u8 msg[2] = { reg, val };
1414         struct i2c_msg msgs;
1415
1416         msgs.flags = 0;
1417         msgs.addr = id / 2;
1418         msgs.len = 2;
1419         msgs.buf = msg;
1420         return i2c_transfer(&av7110->i2c_adap, &msgs, 1);
1421 }
1422
1423 #if 0
1424 u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg)
1425 {
1426         u8 mm1[] = {0x00};
1427         u8 mm2[] = {0x00};
1428         struct i2c_msg msgs[2];
1429
1430         msgs[0].flags = 0;
1431         msgs[1].flags = I2C_M_RD;
1432         msgs[0].addr = msgs[1].addr = id / 2;
1433         mm1[0] = reg;
1434         msgs[0].len = 1; msgs[1].len = 1;
1435         msgs[0].buf = mm1; msgs[1].buf = mm2;
1436         i2c_transfer(&av7110->i2c_adap, msgs, 2);
1437
1438         return mm2[0];
1439 }
1440 #endif
1441
1442 /****************************************************************************
1443  * INITIALIZATION
1444  ****************************************************************************/
1445
1446
1447 static int check_firmware(struct av7110* av7110)
1448 {
1449         u32 crc = 0, len = 0;
1450         unsigned char *ptr;
1451
1452         /* check for firmware magic */
1453         ptr = av7110->bin_fw;
1454         if (ptr[0] != 'A' || ptr[1] != 'V' ||
1455             ptr[2] != 'F' || ptr[3] != 'W') {
1456                 printk("dvb-ttpci: this is not an av7110 firmware\n");
1457                 return -EINVAL;
1458         }
1459         ptr += 4;
1460
1461         /* check dpram file */
1462         crc = ntohl(*(u32*) ptr);
1463         ptr += 4;
1464         len = ntohl(*(u32*) ptr);
1465         ptr += 4;
1466         if (len >= 512) {
1467                 printk("dvb-ttpci: dpram file is way too big.\n");
1468                 return -EINVAL;
1469         }
1470         if (crc != crc32_le(0, ptr, len)) {
1471                 printk("dvb-ttpci: crc32 of dpram file does not match.\n");
1472                 return -EINVAL;
1473         }
1474         av7110->bin_dpram = ptr;
1475         av7110->size_dpram = len;
1476         ptr += len;
1477
1478         /* check root file */
1479         crc = ntohl(*(u32*) ptr);
1480         ptr += 4;
1481         len = ntohl(*(u32*) ptr);
1482         ptr += 4;
1483
1484         if (len <= 200000 || len >= 300000 ||
1485             len > ((av7110->bin_fw + av7110->size_fw) - ptr)) {
1486                 printk("dvb-ttpci: root file has strange size (%d). aborting.\n", len);
1487                 return -EINVAL;
1488         }
1489         if( crc != crc32_le(0, ptr, len)) {
1490                 printk("dvb-ttpci: crc32 of root file does not match.\n");
1491                 return -EINVAL;
1492         }
1493         av7110->bin_root = ptr;
1494         av7110->size_root = len;
1495         return 0;
1496 }
1497
1498 #ifdef CONFIG_DVB_AV7110_FIRMWARE_FILE
1499 #include "av7110_firm.h"
1500 static void put_firmware(struct av7110* av7110)
1501 {
1502         av7110->bin_fw = NULL;
1503 }
1504
1505 static inline int get_firmware(struct av7110* av7110)
1506 {
1507         av7110->bin_fw = dvb_ttpci_fw;
1508         av7110->size_fw = sizeof(dvb_ttpci_fw);
1509         return check_firmware(av7110);
1510 }
1511 #else
1512 static void put_firmware(struct av7110* av7110)
1513 {
1514         vfree(av7110->bin_fw);
1515 }
1516
1517 static int get_firmware(struct av7110* av7110)
1518 {
1519         int ret;
1520         const struct firmware *fw;
1521
1522         /* request the av7110 firmware, this will block until someone uploads it */
1523         ret = request_firmware(&fw, "dvb-ttpci-01.fw", &av7110->dev->pci->dev);
1524         if (ret) {
1525                 if (ret == -ENOENT) {
1526                         printk(KERN_ERR "dvb-ttpci: could not load firmware,"
1527                                " file not found: dvb-ttpci-01.fw\n");
1528                         printk(KERN_ERR "dvb-ttpci: usually this should be in "
1529                                "/usr/lib/hotplug/firmware or /lib/firmware\n");
1530                         printk(KERN_ERR "dvb-ttpci: and can be downloaded from"
1531                                " http://www.linuxtv.org/download/dvb/firmware/\n");
1532                 } else
1533                         printk(KERN_ERR "dvb-ttpci: cannot request firmware"
1534                                " (error %i)\n", ret);
1535                 return -EINVAL;
1536         }
1537
1538         if (fw->size <= 200000) {
1539                 printk("dvb-ttpci: this firmware is way too small.\n");
1540                 release_firmware(fw);
1541                 return -EINVAL;
1542         }
1543
1544         /* check if the firmware is available */
1545         av7110->bin_fw = vmalloc(fw->size);
1546         if (NULL == av7110->bin_fw) {
1547                 dprintk(1, "out of memory\n");
1548                 release_firmware(fw);
1549                 return -ENOMEM;
1550         }
1551
1552         memcpy(av7110->bin_fw, fw->data, fw->size);
1553         av7110->size_fw = fw->size;
1554         if ((ret = check_firmware(av7110)))
1555                 vfree(av7110->bin_fw);
1556
1557         release_firmware(fw);
1558         return ret;
1559 }
1560 #endif
1561
1562
1563 static int alps_bsrv2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1564 {
1565         struct av7110* av7110 = fe->dvb->priv;
1566         u8 pwr = 0;
1567         u8 buf[4];
1568         struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
1569         u32 div = (params->frequency + 479500) / 125;
1570
1571         if (params->frequency > 2000000) pwr = 3;
1572         else if (params->frequency > 1800000) pwr = 2;
1573         else if (params->frequency > 1600000) pwr = 1;
1574         else if (params->frequency > 1200000) pwr = 0;
1575         else if (params->frequency >= 1100000) pwr = 1;
1576         else pwr = 2;
1577
1578         buf[0] = (div >> 8) & 0x7f;
1579         buf[1] = div & 0xff;
1580         buf[2] = ((div & 0x18000) >> 10) | 0x95;
1581         buf[3] = (pwr << 6) | 0x30;
1582
1583         // NOTE: since we're using a prescaler of 2, we set the
1584         // divisor frequency to 62.5kHz and divide by 125 above
1585
1586         if (fe->ops.i2c_gate_ctrl)
1587                 fe->ops.i2c_gate_ctrl(fe, 1);
1588         if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1)
1589                 return -EIO;
1590         return 0;
1591 }
1592
1593 static struct ves1x93_config alps_bsrv2_config = {
1594         .demod_address = 0x08,
1595         .xin = 90100000UL,
1596         .invert_pwm = 0,
1597 };
1598
1599 static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1600 {
1601         struct av7110* av7110 = fe->dvb->priv;
1602         u32 div;
1603         u8 data[4];
1604         struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1605
1606         div = (params->frequency + 35937500 + 31250) / 62500;
1607
1608         data[0] = (div >> 8) & 0x7f;
1609         data[1] = div & 0xff;
1610         data[2] = 0x85 | ((div >> 10) & 0x60);
1611         data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1612
1613         if (fe->ops.i2c_gate_ctrl)
1614                 fe->ops.i2c_gate_ctrl(fe, 1);
1615         if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1616                 return -EIO;
1617         return 0;
1618 }
1619
1620 static struct ves1820_config alps_tdbe2_config = {
1621         .demod_address = 0x09,
1622         .xin = 57840000UL,
1623         .invert = 1,
1624         .selagc = VES1820_SELAGC_SIGNAMPERR,
1625 };
1626
1627
1628
1629
1630 static int grundig_29504_451_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1631 {
1632         struct av7110* av7110 = fe->dvb->priv;
1633         u32 div;
1634         u8 data[4];
1635         struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1636
1637         div = params->frequency / 125;
1638         data[0] = (div >> 8) & 0x7f;
1639         data[1] = div & 0xff;
1640         data[2] = 0x8e;
1641         data[3] = 0x00;
1642
1643         if (fe->ops.i2c_gate_ctrl)
1644                 fe->ops.i2c_gate_ctrl(fe, 1);
1645         if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1646                 return -EIO;
1647         return 0;
1648 }
1649
1650 static struct tda8083_config grundig_29504_451_config = {
1651         .demod_address = 0x68,
1652 };
1653
1654
1655
1656 static int philips_cd1516_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1657 {
1658         struct av7110* av7110 = fe->dvb->priv;
1659         u32 div;
1660         u32 f = params->frequency;
1661         u8 data[4];
1662         struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1663
1664         div = (f + 36125000 + 31250) / 62500;
1665
1666         data[0] = (div >> 8) & 0x7f;
1667         data[1] = div & 0xff;
1668         data[2] = 0x8e;
1669         data[3] = (f < 174000000 ? 0xa1 : f < 470000000 ? 0x92 : 0x34);
1670
1671         if (fe->ops.i2c_gate_ctrl)
1672                 fe->ops.i2c_gate_ctrl(fe, 1);
1673         if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1674                 return -EIO;
1675         return 0;
1676 }
1677
1678 static struct ves1820_config philips_cd1516_config = {
1679         .demod_address = 0x09,
1680         .xin = 57840000UL,
1681         .invert = 1,
1682         .selagc = VES1820_SELAGC_SIGNAMPERR,
1683 };
1684
1685
1686
1687 static int alps_tdlb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1688 {
1689         struct av7110* av7110 = fe->dvb->priv;
1690         u32 div, pwr;
1691         u8 data[4];
1692         struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
1693
1694         div = (params->frequency + 36200000) / 166666;
1695
1696         if (params->frequency <= 782000000)
1697                 pwr = 1;
1698         else
1699                 pwr = 2;
1700
1701         data[0] = (div >> 8) & 0x7f;
1702         data[1] = div & 0xff;
1703         data[2] = 0x85;
1704         data[3] = pwr << 6;
1705
1706         if (fe->ops.i2c_gate_ctrl)
1707                 fe->ops.i2c_gate_ctrl(fe, 1);
1708         if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1709                 return -EIO;
1710         return 0;
1711 }
1712
1713 static int alps_tdlb7_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1714 {
1715 #if defined(CONFIG_DVB_SP8870) || defined(CONFIG_DVB_SP8870_MODULE)
1716         struct av7110* av7110 = fe->dvb->priv;
1717
1718         return request_firmware(fw, name, &av7110->dev->pci->dev);
1719 #else
1720         return -EINVAL;
1721 #endif
1722 }
1723
1724 static struct sp8870_config alps_tdlb7_config = {
1725
1726         .demod_address = 0x71,
1727         .request_firmware = alps_tdlb7_request_firmware,
1728 };
1729
1730
1731 static u8 nexusca_stv0297_inittab[] = {
1732         0x80, 0x01,
1733         0x80, 0x00,
1734         0x81, 0x01,
1735         0x81, 0x00,
1736         0x00, 0x09,
1737         0x01, 0x69,
1738         0x03, 0x00,
1739         0x04, 0x00,
1740         0x07, 0x00,
1741         0x08, 0x00,
1742         0x20, 0x00,
1743         0x21, 0x40,
1744         0x22, 0x00,
1745         0x23, 0x00,
1746         0x24, 0x40,
1747         0x25, 0x88,
1748         0x30, 0xff,
1749         0x31, 0x00,
1750         0x32, 0xff,
1751         0x33, 0x00,
1752         0x34, 0x50,
1753         0x35, 0x7f,
1754         0x36, 0x00,
1755         0x37, 0x20,
1756         0x38, 0x00,
1757         0x40, 0x1c,
1758         0x41, 0xff,
1759         0x42, 0x29,
1760         0x43, 0x00,
1761         0x44, 0xff,
1762         0x45, 0x00,
1763         0x46, 0x00,
1764         0x49, 0x04,
1765         0x4a, 0x00,
1766         0x4b, 0x7b,
1767         0x52, 0x30,
1768         0x55, 0xae,
1769         0x56, 0x47,
1770         0x57, 0xe1,
1771         0x58, 0x3a,
1772         0x5a, 0x1e,
1773         0x5b, 0x34,
1774         0x60, 0x00,
1775         0x63, 0x00,
1776         0x64, 0x00,
1777         0x65, 0x00,
1778         0x66, 0x00,
1779         0x67, 0x00,
1780         0x68, 0x00,
1781         0x69, 0x00,
1782         0x6a, 0x02,
1783         0x6b, 0x00,
1784         0x70, 0xff,
1785         0x71, 0x00,
1786         0x72, 0x00,
1787         0x73, 0x00,
1788         0x74, 0x0c,
1789         0x80, 0x00,
1790         0x81, 0x00,
1791         0x82, 0x00,
1792         0x83, 0x00,
1793         0x84, 0x04,
1794         0x85, 0x80,
1795         0x86, 0x24,
1796         0x87, 0x78,
1797         0x88, 0x10,
1798         0x89, 0x00,
1799         0x90, 0x01,
1800         0x91, 0x01,
1801         0xa0, 0x04,
1802         0xa1, 0x00,
1803         0xa2, 0x00,
1804         0xb0, 0x91,
1805         0xb1, 0x0b,
1806         0xc0, 0x53,
1807         0xc1, 0x70,
1808         0xc2, 0x12,
1809         0xd0, 0x00,
1810         0xd1, 0x00,
1811         0xd2, 0x00,
1812         0xd3, 0x00,
1813         0xd4, 0x00,
1814         0xd5, 0x00,
1815         0xde, 0x00,
1816         0xdf, 0x00,
1817         0x61, 0x49,
1818         0x62, 0x0b,
1819         0x53, 0x08,
1820         0x59, 0x08,
1821         0xff, 0xff,
1822 };
1823
1824 static int nexusca_stv0297_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1825 {
1826         struct av7110* av7110 = fe->dvb->priv;
1827         u32 div;
1828         u8 data[4];
1829         struct i2c_msg msg = { .addr = 0x63, .flags = 0, .buf = data, .len = sizeof(data) };
1830         struct i2c_msg readmsg = { .addr = 0x63, .flags = I2C_M_RD, .buf = data, .len = 1 };
1831         int i;
1832
1833         div = (params->frequency + 36150000 + 31250) / 62500;
1834
1835         data[0] = (div >> 8) & 0x7f;
1836         data[1] = div & 0xff;
1837         data[2] = 0xce;
1838
1839         if (params->frequency < 45000000)
1840                 return -EINVAL;
1841         else if (params->frequency < 137000000)
1842                 data[3] = 0x01;
1843         else if (params->frequency < 403000000)
1844                 data[3] = 0x02;
1845         else if (params->frequency < 860000000)
1846                 data[3] = 0x04;
1847         else
1848                 return -EINVAL;
1849
1850         if (fe->ops.i2c_gate_ctrl)
1851                 fe->ops.i2c_gate_ctrl(fe, 1);
1852         if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1) {
1853                 printk("nexusca: pll transfer failed!\n");
1854                 return -EIO;
1855         }
1856
1857         // wait for PLL lock
1858         for(i = 0; i < 20; i++) {
1859                 if (fe->ops.i2c_gate_ctrl)
1860                         fe->ops.i2c_gate_ctrl(fe, 1);
1861                 if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
1862                         if (data[0] & 0x40) break;
1863                 msleep(10);
1864         }
1865
1866         return 0;
1867 }
1868
1869 static struct stv0297_config nexusca_stv0297_config = {
1870
1871         .demod_address = 0x1C,
1872         .inittab = nexusca_stv0297_inittab,
1873         .invert = 1,
1874         .stop_during_read = 1,
1875 };
1876
1877
1878
1879 static int grundig_29504_401_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters *params)
1880 {
1881         struct av7110* av7110 = fe->dvb->priv;
1882         u32 div;
1883         u8 cfg, cpump, band_select;
1884         u8 data[4];
1885         struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1886
1887         div = (36125000 + params->frequency) / 166666;
1888
1889         cfg = 0x88;
1890
1891         if (params->frequency < 175000000) cpump = 2;
1892         else if (params->frequency < 390000000) cpump = 1;
1893         else if (params->frequency < 470000000) cpump = 2;
1894         else if (params->frequency < 750000000) cpump = 1;
1895         else cpump = 3;
1896
1897         if (params->frequency < 175000000) band_select = 0x0e;
1898         else if (params->frequency < 470000000) band_select = 0x05;
1899         else band_select = 0x03;
1900
1901         data[0] = (div >> 8) & 0x7f;
1902         data[1] = div & 0xff;
1903         data[2] = ((div >> 10) & 0x60) | cfg;
1904         data[3] = (cpump << 6) | band_select;
1905
1906         if (fe->ops.i2c_gate_ctrl)
1907                 fe->ops.i2c_gate_ctrl(fe, 1);
1908         if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1) return -EIO;
1909         return 0;
1910 }
1911
1912 static struct l64781_config grundig_29504_401_config = {
1913         .demod_address = 0x55,
1914 };
1915
1916
1917
1918 static int av7110_fe_lock_fix(struct av7110* av7110, fe_status_t status)
1919 {
1920         int ret = 0;
1921         int synced = (status & FE_HAS_LOCK) ? 1 : 0;
1922
1923         av7110->fe_status = status;
1924
1925         if (av7110->fe_synced == synced)
1926                 return 0;
1927
1928         if (av7110->playing) {
1929                 av7110->fe_synced = synced;
1930                 return 0;
1931         }
1932
1933         if (mutex_lock_interruptible(&av7110->pid_mutex))
1934                 return -ERESTARTSYS;
1935
1936         if (synced) {
1937                 ret = SetPIDs(av7110, av7110->pids[DMX_PES_VIDEO],
1938                         av7110->pids[DMX_PES_AUDIO],
1939                         av7110->pids[DMX_PES_TELETEXT], 0,
1940                         av7110->pids[DMX_PES_PCR]);
1941                 if (!ret)
1942                         ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
1943         } else {
1944                 ret = SetPIDs(av7110, 0, 0, 0, 0, 0);
1945                 if (!ret) {
1946                         ret = av7110_fw_cmd(av7110, COMTYPE_PID_FILTER, FlushTSQueue, 0);
1947                         if (!ret)
1948                                 ret = av7110_wait_msgstate(av7110, GPMQBusy);
1949                 }
1950         }
1951
1952         if (!ret)
1953                 av7110->fe_synced = synced;
1954
1955         mutex_unlock(&av7110->pid_mutex);
1956         return ret;
1957 }
1958
1959 static int av7110_fe_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1960 {
1961         struct av7110* av7110 = fe->dvb->priv;
1962
1963         int ret = av7110_fe_lock_fix(av7110, 0);
1964         if (!ret) {
1965                 av7110->saved_fe_params = *params;
1966                 ret = av7110->fe_set_frontend(fe, params);
1967         }
1968         return ret;
1969 }
1970
1971 static int av7110_fe_init(struct dvb_frontend* fe)
1972 {
1973         struct av7110* av7110 = fe->dvb->priv;
1974
1975         int ret = av7110_fe_lock_fix(av7110, 0);
1976         if (!ret)
1977                 ret = av7110->fe_init(fe);
1978         return ret;
1979 }
1980
1981 static int av7110_fe_read_status(struct dvb_frontend* fe, fe_status_t* status)
1982 {
1983         struct av7110* av7110 = fe->dvb->priv;
1984
1985         /* call the real implementation */
1986         int ret = av7110->fe_read_status(fe, status);
1987         if (!ret)
1988                 if (((*status ^ av7110->fe_status) & FE_HAS_LOCK) && (*status & FE_HAS_LOCK))
1989                         ret = av7110_fe_lock_fix(av7110, *status);
1990         return ret;
1991 }
1992
1993 static int av7110_fe_diseqc_reset_overload(struct dvb_frontend* fe)
1994 {
1995         struct av7110* av7110 = fe->dvb->priv;
1996
1997         int ret = av7110_fe_lock_fix(av7110, 0);
1998         if (!ret)
1999                 ret = av7110->fe_diseqc_reset_overload(fe);
2000         return ret;
2001 }
2002
2003 static int av7110_fe_diseqc_send_master_cmd(struct dvb_frontend* fe,
2004                                             struct dvb_diseqc_master_cmd* cmd)
2005 {
2006         struct av7110* av7110 = fe->dvb->priv;
2007
2008         int ret = av7110_fe_lock_fix(av7110, 0);
2009         if (!ret) {
2010                 av7110->saved_master_cmd = *cmd;
2011                 ret = av7110->fe_diseqc_send_master_cmd(fe, cmd);
2012         }
2013         return ret;
2014 }
2015
2016 static int av7110_fe_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
2017 {
2018         struct av7110* av7110 = fe->dvb->priv;
2019
2020         int ret = av7110_fe_lock_fix(av7110, 0);
2021         if (!ret) {
2022                 av7110->saved_minicmd = minicmd;
2023                 ret = av7110->fe_diseqc_send_burst(fe, minicmd);
2024         }
2025         return ret;
2026 }
2027
2028 static int av7110_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
2029 {
2030         struct av7110* av7110 = fe->dvb->priv;
2031
2032         int ret = av7110_fe_lock_fix(av7110, 0);
2033         if (!ret) {
2034                 av7110->saved_tone = tone;
2035                 ret = av7110->fe_set_tone(fe, tone);
2036         }
2037         return ret;
2038 }
2039
2040 static int av7110_fe_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
2041 {
2042         struct av7110* av7110 = fe->dvb->priv;
2043
2044         int ret = av7110_fe_lock_fix(av7110, 0);
2045         if (!ret) {
2046                 av7110->saved_voltage = voltage;
2047                 ret = av7110->fe_set_voltage(fe, voltage);
2048         }
2049         return ret;
2050 }
2051
2052 static int av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend* fe, unsigned long cmd)
2053 {
2054         struct av7110* av7110 = fe->dvb->priv;
2055
2056         int ret = av7110_fe_lock_fix(av7110, 0);
2057         if (!ret)
2058                 ret = av7110->fe_dishnetwork_send_legacy_command(fe, cmd);
2059         return ret;
2060 }
2061
2062 static void dvb_s_recover(struct av7110* av7110)
2063 {
2064         av7110_fe_init(av7110->fe);
2065
2066         av7110_fe_set_voltage(av7110->fe, av7110->saved_voltage);
2067         if (av7110->saved_master_cmd.msg_len) {
2068                 msleep(20);
2069                 av7110_fe_diseqc_send_master_cmd(av7110->fe, &av7110->saved_master_cmd);
2070         }
2071         msleep(20);
2072         av7110_fe_diseqc_send_burst(av7110->fe, av7110->saved_minicmd);
2073         msleep(20);
2074         av7110_fe_set_tone(av7110->fe, av7110->saved_tone);
2075
2076         av7110_fe_set_frontend(av7110->fe, &av7110->saved_fe_params);
2077 }
2078
2079 static u8 read_pwm(struct av7110* av7110)
2080 {
2081         u8 b = 0xff;
2082         u8 pwm;
2083         struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
2084                                  { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
2085
2086         if ((i2c_transfer(&av7110->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
2087                 pwm = 0x48;
2088
2089         return pwm;
2090 }
2091
2092 static int frontend_init(struct av7110 *av7110)
2093 {
2094         int ret;
2095
2096         if (av7110->dev->pci->subsystem_vendor == 0x110a) {
2097                 switch(av7110->dev->pci->subsystem_device) {
2098                 case 0x0000: // Fujitsu/Siemens DVB-Cable (ves1820/Philips CD1516(??))
2099                         av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config,
2100                                                     &av7110->i2c_adap, read_pwm(av7110));
2101                         if (av7110->fe) {
2102                                 av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2103                         }
2104                         break;
2105                 }
2106
2107         } else if (av7110->dev->pci->subsystem_vendor == 0x13c2) {
2108                 switch(av7110->dev->pci->subsystem_device) {
2109                 case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
2110                 case 0x0003: // Hauppauge/TT WinTV Nexus-S Rev 2.X
2111                 case 0x1002: // Hauppauge/TT WinTV DVB-S rev1.3SE
2112
2113                         // try the ALPS BSRV2 first of all
2114                         av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2115                         if (av7110->fe) {
2116                                 av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2117                                 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2118                                 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2119                                 av7110->fe->ops.set_tone = av7110_set_tone;
2120                                 av7110->recover = dvb_s_recover;
2121                                 break;
2122                         }
2123
2124                         // try the ALPS BSRU6 now
2125                         av7110->fe = dvb_attach(stv0299_attach, &alps_bsru6_config, &av7110->i2c_adap);
2126                         if (av7110->fe) {
2127                                 av7110->fe->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
2128                                 av7110->fe->tuner_priv = &av7110->i2c_adap;
2129
2130                                 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2131                                 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2132                                 av7110->fe->ops.set_tone = av7110_set_tone;
2133                                 av7110->recover = dvb_s_recover;
2134                                 break;
2135                         }
2136
2137                         // Try the grundig 29504-451
2138                         av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2139                         if (av7110->fe) {
2140                                 av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2141                                 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2142                                 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2143                                 av7110->fe->ops.set_tone = av7110_set_tone;
2144                                 av7110->recover = dvb_s_recover;
2145                                 break;
2146                         }
2147
2148                         /* Try DVB-C cards */
2149                         switch(av7110->dev->pci->subsystem_device) {
2150                         case 0x0000:
2151                                 /* Siemens DVB-C (full-length card) VES1820/Philips CD1516 */
2152                                 av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config, &av7110->i2c_adap,
2153                                                         read_pwm(av7110));
2154                                 if (av7110->fe) {
2155                                         av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2156                                 }
2157                                 break;
2158                         case 0x0003:
2159                                 /* Hauppauge DVB-C 2.1 VES1820/ALPS TDBE2 */
2160                                 av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap,
2161                                                         read_pwm(av7110));
2162                                 if (av7110->fe) {
2163                                         av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2164                                 }
2165                                 break;
2166                         }
2167                         break;
2168
2169                 case 0x0001: // Hauppauge/TT Nexus-T premium rev1.X
2170                         // try ALPS TDLB7 first, then Grundig 29504-401
2171                         av7110->fe = dvb_attach(sp8870_attach, &alps_tdlb7_config, &av7110->i2c_adap);
2172                         if (av7110->fe) {
2173                                 av7110->fe->ops.tuner_ops.set_params = alps_tdlb7_tuner_set_params;
2174                                 break;
2175                         }
2176                         /* fall-thru */
2177
2178                 case 0x0008: // Hauppauge/TT DVB-T
2179                         // Grundig 29504-401
2180                         av7110->fe = dvb_attach(l64781_attach, &grundig_29504_401_config, &av7110->i2c_adap);
2181                         if (av7110->fe)
2182                                 av7110->fe->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
2183                         break;
2184
2185                 case 0x0002: // Hauppauge/TT DVB-C premium rev2.X
2186
2187                         av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap, read_pwm(av7110));
2188                         if (av7110->fe) {
2189                                 av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2190                         }
2191                         break;
2192
2193                 case 0x0004: // Galaxis DVB-S rev1.3
2194                         /* ALPS BSRV2 */
2195                         av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2196                         if (av7110->fe) {
2197                                 av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2198                                 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2199                                 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2200                                 av7110->fe->ops.set_tone = av7110_set_tone;
2201                                 av7110->recover = dvb_s_recover;
2202                         }
2203                         break;
2204
2205                 case 0x0006: /* Fujitsu-Siemens DVB-S rev 1.6 */
2206                         /* Grundig 29504-451 */
2207                         av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2208                         if (av7110->fe) {
2209                                 av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2210                                 av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2211                                 av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2212                                 av7110->fe->ops.set_tone = av7110_set_tone;
2213                                 av7110->recover = dvb_s_recover;
2214                         }
2215                         break;
2216
2217                 case 0x000A: // Hauppauge/TT Nexus-CA rev1.X
2218
2219                         av7110->fe = dvb_attach(stv0297_attach, &nexusca_stv0297_config, &av7110->i2c_adap);
2220                         if (av7110->fe) {
2221                                 av7110->fe->ops.tuner_ops.set_params = nexusca_stv0297_tuner_set_params;
2222
2223                                 /* set TDA9819 into DVB mode */
2224                                 saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
2225                                 saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
2226
2227                                 /* tuner on this needs a slower i2c bus speed */
2228                                 av7110->dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
2229                                 break;
2230                         }
2231                         break;
2232
2233                 case 0x000E: /* Hauppauge/TT Nexus-S rev 2.3 */
2234                         /* ALPS BSBE1 */
2235                         av7110->fe = dvb_attach(stv0299_attach, &alps_bsbe1_config, &av7110->i2c_adap);
2236                         if (av7110->fe) {
2237                                 av7110->fe->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
2238                                 av7110->fe->tuner_priv = &av7110->i2c_adap;
2239
2240                                 if (dvb_attach(lnbp21_attach, av7110->fe, &av7110->i2c_adap, 0, 0) == NULL) {
2241                                         printk("dvb-ttpci: LNBP21 not found!\n");
2242                                         if (av7110->fe->ops.release)
2243                                                 av7110->fe->ops.release(av7110->fe);
2244                                         av7110->fe = NULL;
2245                                 } else {
2246                                         av7110->fe->ops.dishnetwork_send_legacy_command = NULL;
2247                                         av7110->recover = dvb_s_recover;
2248                                 }
2249                         }
2250                         break;
2251                 }
2252         }
2253
2254         if (!av7110->fe) {
2255                 /* FIXME: propagate the failure code from the lower layers */
2256                 ret = -ENOMEM;
2257                 printk("dvb-ttpci: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
2258                        av7110->dev->pci->vendor,
2259                        av7110->dev->pci->device,
2260                        av7110->dev->pci->subsystem_vendor,
2261                        av7110->dev->pci->subsystem_device);
2262         } else {
2263                 FE_FUNC_OVERRIDE(av7110->fe->ops.init, av7110->fe_init, av7110_fe_init);
2264                 FE_FUNC_OVERRIDE(av7110->fe->ops.read_status, av7110->fe_read_status, av7110_fe_read_status);
2265                 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_reset_overload, av7110->fe_diseqc_reset_overload, av7110_fe_diseqc_reset_overload);
2266                 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_master_cmd, av7110->fe_diseqc_send_master_cmd, av7110_fe_diseqc_send_master_cmd);
2267                 FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_burst, av7110->fe_diseqc_send_burst, av7110_fe_diseqc_send_burst);
2268                 FE_FUNC_OVERRIDE(av7110->fe->ops.set_tone, av7110->fe_set_tone, av7110_fe_set_tone);
2269                 FE_FUNC_OVERRIDE(av7110->fe->ops.set_voltage, av7110->fe_set_voltage, av7110_fe_set_voltage);
2270                 FE_FUNC_OVERRIDE(av7110->fe->ops.dishnetwork_send_legacy_command, av7110->fe_dishnetwork_send_legacy_command, av7110_fe_dishnetwork_send_legacy_command);
2271                 FE_FUNC_OVERRIDE(av7110->fe->ops.set_frontend, av7110->fe_set_frontend, av7110_fe_set_frontend);
2272
2273                 ret = dvb_register_frontend(&av7110->dvb_adapter, av7110->fe);
2274                 if (ret < 0) {
2275                         printk("av7110: Frontend registration failed!\n");
2276                         dvb_frontend_detach(av7110->fe);
2277                         av7110->fe = NULL;
2278                 }
2279         }
2280         return ret;
2281 }
2282
2283 /* Budgetpatch note:
2284  * Original hardware design by Roberto Deza:
2285  * There is a DVB_Wiki at
2286  * http://212.227.36.83/linuxtv/wiki/index.php/Main_Page
2287  * where is described this 'DVB TT Budget Patch', on Card Modding:
2288  * http://212.227.36.83/linuxtv/wiki/index.php/DVB_TT_Budget_Patch
2289  * On the short description there is also a link to a external file,
2290  * with more details:
2291  * http://perso.wanadoo.es/jesussolano/Ttf_tsc1.zip
2292  *
2293  * New software triggering design by Emard that works on
2294  * original Roberto Deza's hardware:
2295  *
2296  * rps1 code for budgetpatch will copy internal HS event to GPIO3 pin.
2297  * GPIO3 is in budget-patch hardware connectd to port B VSYNC
2298  * HS is an internal event of 7146, accessible with RPS
2299  * and temporarily raised high every n lines
2300  * (n in defined in the RPS_THRESH1 counter threshold)
2301  * I think HS is raised high on the beginning of the n-th line
2302  * and remains high until this n-th line that triggered
2303  * it is completely received. When the receiption of n-th line
2304  * ends, HS is lowered.
2305  *
2306  * To transmit data over DMA, 7146 needs changing state at
2307  * port B VSYNC pin. Any changing of port B VSYNC will
2308  * cause some DMA data transfer, with more or less packets loss.
2309  * It depends on the phase and frequency of VSYNC and
2310  * the way of 7146 is instructed to trigger on port B (defined
2311  * in DD1_INIT register, 3rd nibble from the right valid
2312  * numbers are 0-7, see datasheet)
2313  *
2314  * The correct triggering can minimize packet loss,
2315  * dvbtraffic should give this stable bandwidths:
2316  *   22k transponder = 33814 kbit/s
2317  * 27.5k transponder = 38045 kbit/s
2318  * by experiment it is found that the best results
2319  * (stable bandwidths and almost no packet loss)
2320  * are obtained using DD1_INIT triggering number 2
2321  * (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
2322  * and a VSYNC phase that occurs in the middle of DMA transfer
2323  * (about byte 188*512=96256 in the DMA window).
2324  *
2325  * Phase of HS is still not clear to me how to control,
2326  * It just happens to be so. It can be seen if one enables
2327  * RPS_IRQ and print Event Counter 1 in vpeirq(). Every
2328  * time RPS_INTERRUPT is called, the Event Counter 1 will
2329  * increment. That's how the 7146 is programmed to do event
2330  * counting in this budget-patch.c
2331  * I *think* HPS setting has something to do with the phase
2332  * of HS but I cant be 100% sure in that.
2333  *
2334  * hardware debug note: a working budget card (including budget patch)
2335  * with vpeirq() interrupt setup in mode "0x90" (every 64K) will
2336  * generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
2337  * and that means 3*25=75 Hz of interrupt freqency, as seen by
2338  * watch cat /proc/interrupts
2339  *
2340  * If this frequency is 3x lower (and data received in the DMA
2341  * buffer don't start with 0x47, but in the middle of packets,
2342  * whose lengths appear to be like 188 292 188 104 etc.
2343  * this means VSYNC line is not connected in the hardware.
2344  * (check soldering pcb and pins)
2345  * The same behaviour of missing VSYNC can be duplicated on budget
2346  * cards, by seting DD1_INIT trigger mode 7 in 3rd nibble.
2347  */
2348 static int __devinit av7110_attach(struct saa7146_dev* dev,
2349                                    struct saa7146_pci_extension_data *pci_ext)
2350 {
2351         const int length = TS_WIDTH * TS_HEIGHT;
2352         struct pci_dev *pdev = dev->pci;
2353         struct av7110 *av7110;
2354         struct task_struct *thread;
2355         int ret, count = 0;
2356
2357         dprintk(4, "dev: %p\n", dev);
2358
2359         /* Set RPS_IRQ to 1 to track rps1 activity.
2360          * Enabling this won't send any interrupt to PC CPU.
2361          */
2362 #define RPS_IRQ 0
2363
2364         if (budgetpatch == 1) {
2365                 budgetpatch = 0;
2366                 /* autodetect the presence of budget patch
2367                  * this only works if saa7146 has been recently
2368                  * reset with with MASK_31 to MC1
2369                  *
2370                  * will wait for VBI_B event (vertical blank at port B)
2371                  * and will reset GPIO3 after VBI_B is detected.
2372                  * (GPIO3 should be raised high by CPU to
2373                  * test if GPIO3 will generate vertical blank signal
2374                  * in budget patch GPIO3 is connected to VSYNC_B
2375                  */
2376
2377                 /* RESET SAA7146 */
2378                 saa7146_write(dev, MC1, MASK_31);
2379                 /* autodetection success seems to be time-dependend after reset */
2380
2381                 /* Fix VSYNC level */
2382                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2383                 /* set vsync_b triggering */
2384                 saa7146_write(dev, DD1_STREAM_B, 0);
2385                 /* port B VSYNC at rising edge */
2386                 saa7146_write(dev, DD1_INIT, 0x00000200);
2387                 saa7146_write(dev, BRS_CTRL, 0x00000000);  // VBI
2388                 saa7146_write(dev, MC2,
2389                               1 * (MASK_08 | MASK_24)  |   // BRS control
2390                               0 * (MASK_09 | MASK_25)  |   // a
2391                               1 * (MASK_10 | MASK_26)  |   // b
2392                               0 * (MASK_06 | MASK_22)  |   // HPS_CTRL1
2393                               0 * (MASK_05 | MASK_21)  |   // HPS_CTRL2
2394                               0 * (MASK_01 | MASK_15)      // DEBI
2395                 );
2396
2397                 /* start writing RPS1 code from beginning */
2398                 count = 0;
2399                 /* Disable RPS1 */
2400                 saa7146_write(dev, MC1, MASK_29);
2401                 /* RPS1 timeout disable */
2402                 saa7146_write(dev, RPS_TOV1, 0);
2403                 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_VBI_B));
2404                 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2405                 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2406                 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
2407 #if RPS_IRQ
2408                 /* issue RPS1 interrupt to increment counter */
2409                 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2410 #endif
2411                 WRITE_RPS1(cpu_to_le32(CMD_STOP));
2412                 /* Jump to begin of RPS program as safety measure               (p37) */
2413                 WRITE_RPS1(cpu_to_le32(CMD_JUMP));
2414                 WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
2415
2416 #if RPS_IRQ
2417                 /* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2418                  * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2419                  * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2420                  */
2421                 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2422                 /* set event counter 1 treshold to maximum allowed value        (rEC p55) */
2423                 saa7146_write(dev, ECT1R,  0x3fff );
2424 #endif
2425                 /* Set RPS1 Address register to point to RPS code               (r108 p42) */
2426                 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2427                 /* Enable RPS1,                                                 (rFC p33) */
2428                 saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
2429
2430                 mdelay(10);
2431                 /* now send VSYNC_B to rps1 by rising GPIO3 */
2432                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
2433                 mdelay(10);
2434                 /* if rps1 responded by lowering the GPIO3,
2435                  * then we have budgetpatch hardware
2436                  */
2437                 if ((saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0) {
2438                         budgetpatch = 1;
2439                         printk("dvb-ttpci: BUDGET-PATCH DETECTED.\n");
2440                 }
2441                 /* Disable RPS1 */
2442                 saa7146_write(dev, MC1, ( MASK_29 ));
2443 #if RPS_IRQ
2444                 printk("dvb-ttpci: Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
2445 #endif
2446         }
2447
2448         /* prepare the av7110 device struct */
2449         av7110 = kzalloc(sizeof(struct av7110), GFP_KERNEL);
2450         if (!av7110) {
2451                 dprintk(1, "out of memory\n");
2452                 return -ENOMEM;
2453         }
2454
2455         av7110->card_name = (char*) pci_ext->ext_priv;
2456         av7110->dev = dev;
2457         dev->ext_priv = av7110;
2458
2459         ret = get_firmware(av7110);
2460         if (ret < 0)
2461                 goto err_kfree_0;
2462
2463         ret = dvb_register_adapter(&av7110->dvb_adapter, av7110->card_name,
2464                                    THIS_MODULE, &dev->pci->dev);
2465         if (ret < 0)
2466                 goto err_put_firmware_1;
2467
2468         /* the Siemens DVB needs this if you want to have the i2c chips
2469            get recognized before the main driver is fully loaded */
2470         saa7146_write(dev, GPIO_CTRL, 0x500000);
2471
2472 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
2473         av7110->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
2474 #else
2475         av7110->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
2476 #endif
2477         strlcpy(av7110->i2c_adap.name, pci_ext->ext_priv, sizeof(av7110->i2c_adap.name));
2478
2479         saa7146_i2c_adapter_prepare(dev, &av7110->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); /* 275 kHz */
2480
2481         ret = i2c_add_adapter(&av7110->i2c_adap);
2482         if (ret < 0)
2483                 goto err_dvb_unregister_adapter_2;
2484
2485         ttpci_eeprom_parse_mac(&av7110->i2c_adap,
2486                                av7110->dvb_adapter.proposed_mac);
2487         ret = -ENOMEM;
2488
2489         if (budgetpatch) {
2490                 spin_lock_init(&av7110->feedlock1);
2491                 av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2492                                                                  &av7110->pt);
2493                 if (!av7110->grabbing)
2494                         goto err_i2c_del_3;
2495
2496                 saa7146_write(dev, PCI_BT_V1, 0x1c1f101f);
2497                 saa7146_write(dev, BCS_CTRL, 0x80400040);
2498                 /* set dd1 stream a & b */
2499                 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2500                 saa7146_write(dev, DD1_INIT, 0x03000200);
2501                 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2502                 saa7146_write(dev, BRS_CTRL, 0x60000000);
2503                 saa7146_write(dev, BASE_ODD3, 0);
2504                 saa7146_write(dev, BASE_EVEN3, 0);
2505                 saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2506                 saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2507
2508                 saa7146_write(dev, PITCH3, TS_WIDTH);
2509                 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2510
2511                 /* upload all */
2512                 saa7146_write(dev, MC2, 0x077c077c);
2513                 saa7146_write(dev, GPIO_CTRL, 0x000000);
2514 #if RPS_IRQ
2515                 /* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2516                  * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2517                  * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2518                  */
2519                 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2520                 /* set event counter 1 treshold to maximum allowed value        (rEC p55) */
2521                 saa7146_write(dev, ECT1R,  0x3fff );
2522 #endif
2523                 /* Setup BUDGETPATCH MAIN RPS1 "program" (p35) */
2524                 count = 0;
2525
2526                 /* Wait Source Line Counter Threshold                           (p36) */
2527                 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_HS));
2528                 /* Set GPIO3=1                                                  (p42) */
2529                 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2530                 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2531                 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTHI<<24));
2532 #if RPS_IRQ
2533                 /* issue RPS1 interrupt */
2534                 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2535 #endif
2536                 /* Wait reset Source Line Counter Threshold                     (p36) */
2537                 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS));
2538                 /* Set GPIO3=0                                                  (p42) */
2539                 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2540                 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2541                 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
2542 #if RPS_IRQ
2543                 /* issue RPS1 interrupt */
2544                 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2545 #endif
2546                 /* Jump to begin of RPS program                                 (p37) */
2547                 WRITE_RPS1(cpu_to_le32(CMD_JUMP));
2548                 WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
2549
2550                 /* Fix VSYNC level */
2551                 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2552                 /* Set RPS1 Address register to point to RPS code               (r108 p42) */
2553                 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2554                 /* Set Source Line Counter Threshold, using BRS                 (rCC p43)
2555                  * It generates HS event every TS_HEIGHT lines
2556                  * this is related to TS_WIDTH set in register
2557                  * NUM_LINE_BYTE3. If NUM_LINE_BYTE low 16 bits
2558                  * are set to TS_WIDTH bytes (TS_WIDTH=2*188),
2559                  * then RPS_THRESH1 should be set to trigger
2560                  * every TS_HEIGHT (512) lines.
2561                  */
2562                 saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
2563
2564                 /* Enable RPS1                                                  (rFC p33) */
2565                 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
2566
2567                 /* end of budgetpatch register initialization */
2568                 tasklet_init (&av7110->vpe_tasklet,  vpeirq,  (unsigned long) av7110);
2569         } else {
2570                 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
2571                 saa7146_write(dev, BCS_CTRL, 0x80400040);
2572
2573                 /* set dd1 stream a & b */
2574                 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2575                 saa7146_write(dev, DD1_INIT, 0x03000000);
2576                 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2577
2578                 /* upload all */
2579                 saa7146_write(dev, MC2, 0x077c077c);
2580                 saa7146_write(dev, GPIO_CTRL, 0x000000);
2581         }
2582
2583         tasklet_init (&av7110->debi_tasklet, debiirq, (unsigned long) av7110);
2584         tasklet_init (&av7110->gpio_tasklet, gpioirq, (unsigned long) av7110);
2585
2586         mutex_init(&av7110->pid_mutex);
2587
2588         /* locks for data transfers from/to AV7110 */
2589         spin_lock_init(&av7110->debilock);
2590         mutex_init(&av7110->dcomlock);
2591         av7110->debitype = -1;
2592
2593         /* default OSD window */
2594         av7110->osdwin = 1;
2595         mutex_init(&av7110->osd_mutex);
2596
2597         /* TV standard */
2598         av7110->vidmode = tv_standard == 1 ? VIDEO_MODE_NTSC : VIDEO_MODE_PAL;
2599
2600         /* ARM "watchdog" */
2601         init_waitqueue_head(&av7110->arm_wait);
2602         av7110->arm_thread = NULL;
2603
2604         /* allocate and init buffers */
2605         av7110->debi_virt = pci_alloc_consistent(pdev, 8192, &av7110->debi_bus);
2606         if (!av7110->debi_virt)
2607                 goto err_saa71466_vfree_4;
2608
2609
2610         av7110->iobuf = vmalloc(AVOUTLEN+AOUTLEN+BMPLEN+4*IPACKS);
2611         if (!av7110->iobuf)
2612                 goto err_pci_free_5;
2613
2614         ret = av7110_av_init(av7110);
2615         if (ret < 0)
2616                 goto err_iobuf_vfree_6;
2617
2618         /* init BMP buffer */
2619         av7110->bmpbuf = av7110->iobuf+AVOUTLEN+AOUTLEN;
2620         init_waitqueue_head(&av7110->bmpq);
2621
2622         ret = av7110_ca_init(av7110);
2623         if (ret < 0)
2624                 goto err_av7110_av_exit_7;
2625
2626         /* load firmware into AV7110 cards */
2627         ret = av7110_bootarm(av7110);
2628         if (ret < 0)
2629                 goto err_av7110_ca_exit_8;
2630
2631         ret = av7110_firmversion(av7110);
2632         if (ret < 0)
2633                 goto err_stop_arm_9;
2634
2635         if (FW_VERSION(av7110->arm_app)<0x2501)
2636                 printk ("dvb-ttpci: Warning, firmware version 0x%04x is too old. "
2637                         "System might be unstable!\n", FW_VERSION(av7110->arm_app));
2638
2639         thread = kthread_run(arm_thread, (void *) av7110, "arm_mon");
2640         if (IS_ERR(thread)) {
2641                 ret = PTR_ERR(thread);
2642                 goto err_stop_arm_9;
2643         }
2644         av7110->arm_thread = thread;
2645
2646         /* set initial volume in mixer struct */
2647         av7110->mixer.volume_left  = volume;
2648         av7110->mixer.volume_right = volume;
2649
2650         ret = av7110_register(av7110);
2651         if (ret < 0)
2652                 goto err_arm_thread_stop_10;
2653
2654         init_av7110_av(av7110);
2655
2656         /* special case DVB-C: these cards have an analog tuner
2657            plus need some special handling, so we have separate
2658            saa7146_ext_vv data for these... */
2659         ret = av7110_init_v4l(av7110);
2660         if (ret < 0)
2661                 goto err_av7110_unregister_11;
2662
2663         av7110->dvb_adapter.priv = av7110;
2664         ret = frontend_init(av7110);
2665         if (ret < 0)
2666                 goto err_av7110_exit_v4l_12;
2667
2668 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2669         av7110_ir_init(av7110);
2670 #endif
2671         printk(KERN_INFO "dvb-ttpci: found av7110-%d.\n", av7110_num);
2672         av7110_num++;
2673 out:
2674         return ret;
2675
2676 err_av7110_exit_v4l_12:
2677         av7110_exit_v4l(av7110);
2678 err_av7110_unregister_11:
2679         dvb_unregister(av7110);
2680 err_arm_thread_stop_10:
2681         av7110_arm_sync(av7110);
2682 err_stop_arm_9:
2683         /* Nothing to do. Rejoice. */
2684 err_av7110_ca_exit_8:
2685         av7110_ca_exit(av7110);
2686 err_av7110_av_exit_7:
2687         av7110_av_exit(av7110);
2688 err_iobuf_vfree_6:
2689         vfree(av7110->iobuf);
2690 err_pci_free_5:
2691         pci_free_consistent(pdev, 8192, av7110->debi_virt, av7110->debi_bus);
2692 err_saa71466_vfree_4:
2693         if (av7110->grabbing)
2694                 saa7146_vfree_destroy_pgtable(pdev, av7110->grabbing, &av7110->pt);
2695 err_i2c_del_3:
2696         i2c_del_adapter(&av7110->i2c_adap);
2697 err_dvb_unregister_adapter_2:
2698         dvb_unregister_adapter(&av7110->dvb_adapter);
2699 err_put_firmware_1:
2700         put_firmware(av7110);
2701 err_kfree_0:
2702         kfree(av7110);
2703         goto out;
2704 }
2705
2706 static int __devexit av7110_detach(struct saa7146_dev* saa)
2707 {
2708         struct av7110 *av7110 = saa->ext_priv;
2709         dprintk(4, "%p\n", av7110);
2710
2711 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2712         av7110_ir_exit(av7110);
2713 #endif
2714         if (budgetpatch) {
2715                 /* Disable RPS1 */
2716                 saa7146_write(saa, MC1, MASK_29);
2717                 /* VSYNC LOW (inactive) */
2718                 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2719                 saa7146_write(saa, MC1, MASK_20);       /* DMA3 off */
2720                 SAA7146_IER_DISABLE(saa, MASK_10);
2721                 SAA7146_ISR_CLEAR(saa, MASK_10);
2722                 msleep(50);
2723                 tasklet_kill(&av7110->vpe_tasklet);
2724                 saa7146_vfree_destroy_pgtable(saa->pci, av7110->grabbing, &av7110->pt);
2725         }
2726         av7110_exit_v4l(av7110);
2727
2728         av7110_arm_sync(av7110);
2729
2730         tasklet_kill(&av7110->debi_tasklet);
2731         tasklet_kill(&av7110->gpio_tasklet);
2732
2733         dvb_unregister(av7110);
2734
2735         SAA7146_IER_DISABLE(saa, MASK_19 | MASK_03);
2736         SAA7146_ISR_CLEAR(saa, MASK_19 | MASK_03);
2737
2738         av7110_ca_exit(av7110);
2739         av7110_av_exit(av7110);
2740
2741         vfree(av7110->iobuf);
2742         pci_free_consistent(saa->pci, 8192, av7110->debi_virt,
2743                             av7110->debi_bus);
2744
2745         i2c_del_adapter(&av7110->i2c_adap);
2746
2747         dvb_unregister_adapter (&av7110->dvb_adapter);
2748
2749         av7110_num--;
2750
2751         put_firmware(av7110);
2752
2753         kfree(av7110);
2754
2755         saa->ext_priv = NULL;
2756
2757         return 0;
2758 }
2759
2760
2761 static void av7110_irq(struct saa7146_dev* dev, u32 *isr)
2762 {
2763         struct av7110 *av7110 = dev->ext_priv;
2764
2765         //print_time("av7110_irq");
2766
2767         /* Note: Don't try to handle the DEBI error irq (MASK_18), in
2768          * intel mode the timeout is asserted all the time...
2769          */
2770
2771         if (*isr & MASK_19) {
2772                 //printk("av7110_irq: DEBI\n");
2773                 /* Note 1: The DEBI irq is level triggered: We must enable it
2774                  * only after we started a DMA xfer, and disable it here
2775                  * immediately, or it will be signalled all the time while
2776                  * DEBI is idle.
2777                  * Note 2: You would think that an irq which is masked is
2778                  * not signalled by the hardware. Not so for the SAA7146:
2779                  * An irq is signalled as long as the corresponding bit
2780                  * in the ISR is set, and disabling irqs just prevents the
2781                  * hardware from setting the ISR bit. This means a) that we
2782                  * must clear the ISR *after* disabling the irq (which is why
2783                  * we must do it here even though saa7146_core did it already),
2784                  * and b) that if we were to disable an edge triggered irq
2785                  * (like the gpio irqs sadly are) temporarily we would likely
2786                  * loose some. This sucks :-(
2787                  */
2788                 SAA7146_IER_DISABLE(av7110->dev, MASK_19);
2789                 SAA7146_ISR_CLEAR(av7110->dev, MASK_19);
2790                 tasklet_schedule(&av7110->debi_tasklet);
2791         }
2792
2793         if (*isr & MASK_03) {
2794                 //printk("av7110_irq: GPIO\n");
2795                 tasklet_schedule(&av7110->gpio_tasklet);
2796         }
2797
2798         if ((*isr & MASK_10) && budgetpatch)
2799                 tasklet_schedule(&av7110->vpe_tasklet);
2800 }
2801
2802
2803 static struct saa7146_extension av7110_extension;
2804
2805 #define MAKE_AV7110_INFO(x_var,x_name) \
2806 static struct saa7146_pci_extension_data x_var = { \
2807         .ext_priv = x_name, \
2808         .ext = &av7110_extension }
2809
2810 MAKE_AV7110_INFO(tts_1_X_fsc,"Technotrend/Hauppauge WinTV DVB-S rev1.X or Fujitsu Siemens DVB-C");
2811 MAKE_AV7110_INFO(ttt_1_X,    "Technotrend/Hauppauge WinTV DVB-T rev1.X");
2812 MAKE_AV7110_INFO(ttc_1_X,    "Technotrend/Hauppauge WinTV Nexus-CA rev1.X");
2813 MAKE_AV7110_INFO(ttc_2_X,    "Technotrend/Hauppauge WinTV DVB-C rev2.X");
2814 MAKE_AV7110_INFO(tts_2_X,    "Technotrend/Hauppauge WinTV Nexus-S rev2.X");
2815 MAKE_AV7110_INFO(tts_2_3,    "Technotrend/Hauppauge WinTV Nexus-S rev2.3");
2816 MAKE_AV7110_INFO(tts_1_3se,  "Technotrend/Hauppauge WinTV DVB-S rev1.3 SE");
2817 MAKE_AV7110_INFO(ttt,        "Technotrend/Hauppauge DVB-T");
2818 MAKE_AV7110_INFO(fsc,        "Fujitsu Siemens DVB-C");
2819 MAKE_AV7110_INFO(fss,        "Fujitsu Siemens DVB-S rev1.6");
2820 MAKE_AV7110_INFO(gxs_1_3,    "Galaxis DVB-S rev1.3");
2821
2822 static struct pci_device_id pci_tbl[] = {
2823         MAKE_EXTENSION_PCI(fsc,         0x110a, 0x0000),
2824         MAKE_EXTENSION_PCI(tts_1_X_fsc, 0x13c2, 0x0000),
2825         MAKE_EXTENSION_PCI(ttt_1_X,     0x13c2, 0x0001),
2826         MAKE_EXTENSION_PCI(ttc_2_X,     0x13c2, 0x0002),
2827         MAKE_EXTENSION_PCI(tts_2_X,     0x13c2, 0x0003),
2828         MAKE_EXTENSION_PCI(gxs_1_3,     0x13c2, 0x0004),
2829         MAKE_EXTENSION_PCI(fss,         0x13c2, 0x0006),
2830         MAKE_EXTENSION_PCI(ttt,         0x13c2, 0x0008),
2831         MAKE_EXTENSION_PCI(ttc_1_X,     0x13c2, 0x000a),
2832         MAKE_EXTENSION_PCI(tts_2_3,     0x13c2, 0x000e),
2833         MAKE_EXTENSION_PCI(tts_1_3se,   0x13c2, 0x1002),
2834
2835 /*      MAKE_EXTENSION_PCI(???, 0x13c2, 0x0005), UNDEFINED CARD */ // Technisat SkyStar1
2836 /*      MAKE_EXTENSION_PCI(???, 0x13c2, 0x0009), UNDEFINED CARD */ // TT/Hauppauge WinTV Nexus-CA v????
2837
2838         {
2839                 .vendor    = 0,
2840         }
2841 };
2842
2843 MODULE_DEVICE_TABLE(pci, pci_tbl);
2844
2845
2846 static struct saa7146_extension av7110_extension = {
2847         .name           = "dvb",
2848         .flags          = SAA7146_USE_I2C_IRQ,
2849
2850         .module         = THIS_MODULE,
2851         .pci_tbl        = &pci_tbl[0],
2852         .attach         = av7110_attach,
2853         .detach         = __devexit_p(av7110_detach),
2854
2855         .irq_mask       = MASK_19 | MASK_03 | MASK_10,
2856         .irq_func       = av7110_irq,
2857 };
2858
2859
2860 static int __init av7110_init(void)
2861 {
2862         int retval;
2863         retval = saa7146_register_extension(&av7110_extension);
2864         return retval;
2865 }
2866
2867
2868 static void __exit av7110_exit(void)
2869 {
2870         saa7146_unregister_extension(&av7110_extension);
2871 }
2872
2873 module_init(av7110_init);
2874 module_exit(av7110_exit);
2875
2876 MODULE_DESCRIPTION("driver for the SAA7146 based AV110 PCI DVB cards by "
2877                    "Siemens, Technotrend, Hauppauge");
2878 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
2879 MODULE_LICENSE("GPL");