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