[NETFILTER]: Move ip6_masked_addrcmp to include/net/ipv6.h
[linux-2.6/verdex.git] / drivers / media / dvb / ttpci / av7110.c
blob7c6ccb96b1574dd4e8d24bcc9159a3ecb093d803
1 /*
2 * driver for the SAA7146 based AV110 cards (like the Fujitsu-Siemens DVB)
3 * av7110.c: initialization and demux stuff
5 * Copyright (C) 1999-2002 Ralph Metzler
6 * & Marcus Metzler for convergence integrated media GmbH
8 * originally based on code by:
9 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
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.
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.
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
29 * the project's page is at http://www.linuxtv.org/dvb/
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>
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>
56 #include <asm/system.h>
57 #include <asm/semaphore.h>
59 #include <linux/dvb/frontend.h>
61 #include "dvb_frontend.h"
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"
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)
76 int av7110_debug;
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;
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)");
104 static void restart_feeds(struct av7110 *av7110);
106 static int av7110_num;
108 #define FE_FUNC_OVERRIDE(fe_func, av7110_copy, av7110_func) \
110 if (fe_func != NULL) { \
111 av7110_copy = fe_func; \
112 fe_func = av7110_func; \
117 static void init_av7110_av(struct av7110 *av7110)
119 int ret;
120 struct saa7146_dev *dev = av7110->dev;
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);
128 ret = av7710_set_video_mode(av7110, vidmode);
129 if (ret < 0)
130 printk("dvb-ttpci:cannot set video mode:%d\n",ret);
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
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);
152 * some special handling for the Siemens DVB-C cards...
154 } else if (0 == av7110_init_analog_module(av7110)) {
155 /* done. */
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;
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);
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
185 if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000e)
186 av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, SpdifSwitch, 1, 0); // SPDIF on
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);
193 static void recover_arm(struct av7110 *av7110)
195 dprintk(4, "%p\n",av7110);
197 av7110_bootarm(av7110);
198 msleep(100);
200 init_av7110_av(av7110);
202 /* card-specific recovery */
203 if (av7110->recover)
204 av7110->recover(av7110);
206 restart_feeds(av7110);
207 av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1, av7110->ir_config);
210 static void av7110_arm_sync(struct av7110 *av7110)
212 av7110->arm_rmmod = 1;
213 wake_up_interruptible(&av7110->arm_wait);
215 while (av7110->arm_thread)
216 msleep(1);
219 static int arm_thread(void *data)
221 struct av7110 *av7110 = data;
222 u16 newloops = 0;
223 int timeout;
225 dprintk(4, "%p\n",av7110);
227 lock_kernel();
228 daemonize("arm_mon");
229 sigfillset(&current->blocked);
230 unlock_kernel();
232 av7110->arm_thread = current;
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;
242 if (!av7110->arm_ready)
243 continue;
245 if (down_interruptible(&av7110->dcomlock))
246 break;
247 newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2);
248 up(&av7110->dcomlock);
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);
254 recover_arm(av7110);
256 if (down_interruptible(&av7110->dcomlock))
257 break;
258 newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2) - 1;
259 up(&av7110->dcomlock);
261 av7110->arm_loops = newloops;
262 av7110->arm_errors = 0;
265 av7110->arm_thread = NULL;
266 return 0;
270 /****************************************************************************
271 * IRQ handling
272 ****************************************************************************/
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)
280 if (!dvbdmxfilter->feed->demux->dmx.frontend)
281 return 0;
282 if (dvbdmxfilter->feed->demux->dmx.frontend->source == DMX_MEMORY_FE)
283 return 0;
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;
294 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
295 xor = filter->filter_value[i] ^ buffer1[i];
296 neq |= dvbdmxfilter->maskandnotmode[i] & xor;
298 if (!neq)
299 return 0;
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;
323 //#define DEBUG_TIMING
324 static inline void print_time(char *s)
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
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)
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;
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);
354 static void debiirq(unsigned long data)
356 struct av7110 *av7110 = (struct av7110 *) data;
357 int type = av7110->debitype;
358 int handle = (type >> 8) & 0x1f;
359 unsigned int xfer = 0;
361 print_time("debi");
362 dprintk(4, "type 0x%04x\n", type);
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;
370 av7110->debitype = -1;
372 switch (type & 0xff) {
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;
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;
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;
400 case DATA_CI_GET:
402 u8 *data = av7110->debi_virt;
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;
419 case DATA_COMMON_INTERFACE:
420 CI_handle(av7110, (u8 *)av7110->debi_virt, av7110->debilen);
421 #if 0
423 int i;
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)));
433 printk("\n");
435 #endif
436 xfer = RX_BUFF;
437 break;
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;
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;
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);
464 /* irq from av7110 firmware writing the mailbox register in the DPRAM */
465 static void gpioirq(unsigned long data)
467 struct av7110 *av7110 = (struct av7110 *) data;
468 u32 rxbuf, txbuf;
469 int len;
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));
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? */
482 spin_lock(&av7110->debilock);
483 ARM_ClearIrq(av7110);
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;
492 print_time("gpio");
493 dprintk(8, "GPIO0 irq 0x%04x %d\n", av7110->debitype, av7110->debilen);
495 switch (av7110->debitype & 0xff) {
497 case DATA_TS_PLAY:
498 case DATA_PES_PLAY:
499 break;
501 case DATA_MPEG_VIDEO_EVENT:
503 u32 h_ar;
504 struct video_event event;
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);
509 iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
510 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
512 av7110->video_size.h = h_ar & 0xfff;
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)
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;
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);
539 dvb_video_add_event(av7110, &event);
540 break;
543 case DATA_CI_PUT:
545 int avail;
546 struct dvb_ringbuffer *cibuf = &av7110->ci_wbuffer;
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;
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;
563 DVB_RINGBUFFER_SKIP(cibuf, 2);
565 dvb_ringbuffer_read(cibuf, av7110->debi_virt, len, 0);
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;
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;
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);
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);
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;
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;
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;
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;
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;
644 /* fall through */
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;
653 case DATA_DEBUG_MESSAGE:
654 if (!len || len > 0xff) {
655 iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
656 break;
658 start_debi_dma(av7110, DEBI_READ, Reserved, len);
659 spin_unlock(&av7110->debilock);
660 return;
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;
669 default:
670 printk("dvb-ttpci: gpioirq unknown type=%d len=%d\n",
671 av7110->debitype, av7110->debilen);
672 break;
674 av7110->debitype = -1;
675 ARM_ClearMailBox(av7110);
676 spin_unlock(&av7110->debilock);
680 #ifdef CONFIG_DVB_AV7110_OSD
681 static int dvb_osd_ioctl(struct inode *inode, struct file *file,
682 unsigned int cmd, void *parg)
684 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
685 struct av7110 *av7110 = (struct av7110 *) dvbdev->priv;
687 dprintk(4, "%p\n", av7110);
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);
694 return -EINVAL;
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,
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,
712 #endif /* CONFIG_DVB_AV7110_OSD */
715 static inline int SetPIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
716 u16 subpid, u16 pcrpid)
718 u16 aflags = 0;
720 dprintk(4, "%p\n", av7110);
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;
731 if (av7110->audiostate.bypass_mode)
732 aflags |= 0x8000;
734 return av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, MultiPID, 6,
735 pcrpid, vpid, apid, ttpid, subpid, aflags);
738 int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
739 u16 subpid, u16 pcrpid)
741 int ret = 0;
742 dprintk(4, "%p\n", av7110);
744 if (down_interruptible(&av7110->pid_mutex))
745 return -ERESTARTSYS;
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;
756 av7110->pids[DMX_PES_SUBTITLE] = 0;
758 if (av7110->fe_synced) {
759 pcrpid = av7110->pids[DMX_PES_PCR];
760 ret = SetPIDs(av7110, vpid, apid, ttpid, subpid, pcrpid);
763 up(&av7110->pid_mutex);
764 return ret;
768 /******************************************************************************
769 * hardware filter functions
770 ******************************************************************************/
772 static int StartHWFilter(struct dvb_demux_filter *dvbdmxfilter)
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;
782 dprintk(4, "%p\n", av7110);
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;
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);
799 buf[0] = (COMTYPE_PID_FILTER << 8) + AddPIDFilter;
800 buf[1] = 16;
801 buf[2] = dvbdmxfeed->pid;
802 buf[3] = mode;
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;
816 av7110->handle2filter[handle] = dvbdmxfilter;
817 dvbdmxfilter->hw_handle = handle;
819 return ret;
822 static int StopHWFilter(struct dvb_demux_filter *dvbdmxfilter)
824 struct av7110 *av7110 = (struct av7110 *) dvbdmxfilter->feed->demux->priv;
825 u16 buf[3];
826 u16 answ[2];
827 int ret;
828 u16 handle;
830 dprintk(4, "%p\n", av7110);
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;
839 av7110->handle2filter[handle] = NULL;
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;
853 return ret;
857 static int dvb_feed_start_pid(struct dvb_demux_feed *dvbdmxfeed)
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;
865 dprintk(4, "%p\n", av7110);
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;
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;
883 if (dvbdmxfeed->pes_type < 2 && npids[0])
884 if (av7110->fe_synced)
886 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
887 if (ret)
888 return ret;
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);
897 return ret;
900 static int dvb_feed_stop_pid(struct dvb_demux_feed *dvbdmxfeed)
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;
907 int ret = 0;
909 dprintk(4, "%p\n", av7110);
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;
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;
936 if (!ret)
937 ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
938 return ret;
941 static int av7110_start_feed(struct dvb_demux_feed *feed)
943 struct dvb_demux *demux = feed->demux;
944 struct av7110 *av7110 = demux->priv;
945 int ret = 0;
947 dprintk(4, "%p\n", av7110);
949 if (!demux->dmx.frontend)
950 return -EINVAL;
952 if (feed->pid > 0x1fff)
953 return -EINVAL;
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;
970 break;
971 default:
972 ret = dvb_feed_start_pid(feed);
973 break;
975 } else if ((feed->ts_type & TS_PACKET) &&
976 (demux->dmx.frontend->source != DMX_MEMORY_FE)) {
977 ret = StartHWFilter(feed->filter);
981 else if (feed->type == DMX_TYPE_SEC) {
982 int i;
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;
1000 return ret;
1004 static int av7110_stop_feed(struct dvb_demux_feed *feed)
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);
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;
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);
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 */
1043 return ret;
1047 static void restart_feeds(struct av7110 *av7110)
1049 struct dvb_demux *dvbdmx = &av7110->demux;
1050 struct dvb_demux_feed *feed;
1051 int mode;
1052 int i, j;
1054 dprintk(4, "%p\n", av7110);
1056 mode = av7110->playing;
1057 av7110->playing = 0;
1058 av7110->rec_mode = 0;
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;
1073 av7110_start_feed(feed);
1077 if (mode)
1078 av7110_av_start_play(av7110, mode);
1081 static int dvb_get_stc(struct dmx_demux *demux, unsigned int num,
1082 uint64_t *stc, unsigned int *base)
1084 int ret;
1085 u16 fwstc[4];
1086 u16 tag = ((COMTYPE_REQUEST << 8) + ReqSTC);
1087 struct dvb_demux *dvbdemux;
1088 struct av7110 *av7110;
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;
1098 dprintk(4, "%p\n", av7110);
1100 if (num != 0)
1101 return -EINVAL;
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;
1108 dprintk(2, "fwstc = %04hx %04hx %04hx %04hx\n",
1109 fwstc[0], fwstc[1], fwstc[2], fwstc[3]);
1111 *stc = (((uint64_t) ((fwstc[3] & 0x8000) >> 15)) << 32) |
1112 (((uint64_t) fwstc[1]) << 16) | ((uint64_t) fwstc[0]);
1113 *base = 1;
1115 dprintk(4, "stc = %lu\n", (unsigned long)*stc);
1117 return 0;
1121 /******************************************************************************
1122 * SEC device file operations
1123 ******************************************************************************/
1126 static int av7110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
1128 struct av7110* av7110 = (struct av7110*) fe->dvb->priv;
1130 switch (tone) {
1131 case SEC_TONE_ON:
1132 return Set22K(av7110, 1);
1134 case SEC_TONE_OFF:
1135 return Set22K(av7110, 0);
1137 default:
1138 return -EINVAL;
1142 static int av7110_diseqc_send_master_cmd(struct dvb_frontend* fe,
1143 struct dvb_diseqc_master_cmd* cmd)
1145 struct av7110* av7110 = fe->dvb->priv;
1147 return av7110_diseqc_send(av7110, cmd->msg_len, cmd->msg, -1);
1150 static int av7110_diseqc_send_burst(struct dvb_frontend* fe,
1151 fe_sec_mini_cmd_t minicmd)
1153 struct av7110* av7110 = fe->dvb->priv;
1155 return av7110_diseqc_send(av7110, 0, NULL, minicmd);
1158 /* simplified code from budget-core.c */
1159 static int stop_ts_capture(struct av7110 *budget)
1161 dprintk(2, "budget: %p\n", budget);
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;
1171 static int start_ts_capture(struct av7110 *budget)
1173 dprintk(2, "budget: %p\n", budget);
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;
1185 static int budget_start_feed(struct dvb_demux_feed *feed)
1187 struct dvb_demux *demux = feed->demux;
1188 struct av7110 *budget = (struct av7110 *) demux->priv;
1189 int status;
1191 dprintk(2, "av7110: %p\n", budget);
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;
1200 static int budget_stop_feed(struct dvb_demux_feed *feed)
1202 struct dvb_demux *demux = feed->demux;
1203 struct av7110 *budget = (struct av7110 *) demux->priv;
1204 int status;
1206 dprintk(2, "budget: %p\n", budget);
1208 spin_lock(&budget->feedlock1);
1209 status = stop_ts_capture(budget);
1210 spin_unlock(&budget->feedlock1);
1211 return status;
1214 static void vpeirq(unsigned long data)
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);
1221 if (!budgetpatch) {
1222 printk("av7110.c: vpeirq() called while budgetpatch disabled!"
1223 " check saa7146 IER register\n");
1224 BUG();
1226 /* nearest lower position divisible by 188 */
1227 newdma -= newdma % 188;
1229 if (newdma >= TS_BUFLEN)
1230 return;
1232 budget->ttbp = newdma;
1234 if (!budget->feeding1 || (newdma == olddma))
1235 return;
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
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);
1254 static int av7110_register(struct av7110 *av7110)
1256 int ret, i;
1257 struct dvb_demux *dvbdemux = &av7110->demux;
1258 struct dvb_demux *dvbdemux1 = &av7110->demux1;
1260 dprintk(4, "%p\n", av7110);
1262 if (av7110->registered)
1263 return -1;
1265 av7110->registered = 1;
1267 dvbdemux->priv = (void *) av7110;
1269 for (i = 0; i < 32; i++)
1270 av7110->handle2filter[i] = NULL;
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);
1280 dvb_dmx_init(&av7110->demux);
1281 av7110->demux.dmx.get_stc = dvb_get_stc;
1283 av7110->dmxdev.filternum = 32;
1284 av7110->dmxdev.demux = &dvbdemux->dmx;
1285 av7110->dmxdev.capabilities = 0;
1287 dvb_dmxdev_init(&av7110->dmxdev, &av7110->dvb_adapter);
1289 av7110->hw_frontend.source = DMX_FRONTEND_0;
1291 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1293 if (ret < 0)
1294 return ret;
1296 av7110->mem_frontend.source = DMX_MEMORY_FE;
1298 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1300 if (ret < 0)
1301 return ret;
1303 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
1304 &av7110->hw_frontend);
1305 if (ret < 0)
1306 return ret;
1308 av7110_av_register(av7110);
1309 av7110_ca_register(av7110);
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
1316 dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net, &dvbdemux->dmx);
1318 if (budgetpatch) {
1319 /* initialize software demux1 without its own frontend
1320 * demux1 hardware is connected to frontend0 of demux0
1322 dvbdemux1->priv = (void *) av7110;
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;
1330 dvbdemux1->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1331 DMX_MEMORY_BASED_FILTERING);
1333 dvb_dmx_init(&av7110->demux1);
1335 av7110->dmxdev1.filternum = 256;
1336 av7110->dmxdev1.demux = &dvbdemux1->dmx;
1337 av7110->dmxdev1.capabilities = 0;
1339 dvb_dmxdev_init(&av7110->dmxdev1, &av7110->dvb_adapter);
1341 dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net1, &dvbdemux1->dmx);
1342 printk("dvb-ttpci: additional demux1 for budget-patch registered\n");
1344 return 0;
1348 static void dvb_unregister(struct av7110 *av7110)
1350 struct dvb_demux *dvbdemux = &av7110->demux;
1351 struct dvb_demux *dvbdemux1 = &av7110->demux1;
1353 dprintk(4, "%p\n", av7110);
1355 if (!av7110->registered)
1356 return;
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);
1365 dvb_net_release(&av7110->dvb_net);
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);
1371 dvb_dmxdev_release(&av7110->dmxdev);
1372 dvb_dmx_release(&av7110->demux);
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);
1382 /****************************************************************************
1383 * I2C client commands
1384 ****************************************************************************/
1386 int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val)
1388 u8 msg[2] = { reg, val };
1389 struct i2c_msg msgs;
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);
1398 #if 0
1399 u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg)
1401 u8 mm1[] = {0x00};
1402 u8 mm2[] = {0x00};
1403 struct i2c_msg msgs[2];
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);
1413 return mm2[0];
1415 #endif
1417 /****************************************************************************
1418 * INITIALIZATION
1419 ****************************************************************************/
1422 static int check_firmware(struct av7110* av7110)
1424 u32 crc = 0, len = 0;
1425 unsigned char *ptr;
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;
1434 ptr += 4;
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 too big.\n");
1443 return -EINVAL;
1445 if (crc != crc32_le(0, ptr, len)) {
1446 printk("dvb-ttpci: crc32 of dpram file does not match.\n");
1447 return -EINVAL;
1449 av7110->bin_dpram = ptr;
1450 av7110->size_dpram = len;
1451 ptr += len;
1453 /* check root file */
1454 crc = ntohl(*(u32*) ptr);
1455 ptr += 4;
1456 len = ntohl(*(u32*) ptr);
1457 ptr += 4;
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;
1464 if( crc != crc32_le(0, ptr, len)) {
1465 printk("dvb-ttpci: crc32 of root file does not match.\n");
1466 return -EINVAL;
1468 av7110->bin_root = ptr;
1469 av7110->size_root = len;
1470 return 0;
1473 #ifdef CONFIG_DVB_AV7110_FIRMWARE_FILE
1474 #include "av7110_firm.h"
1475 static void put_firmware(struct av7110* av7110)
1477 av7110->bin_fw = NULL;
1480 static inline int get_firmware(struct av7110* av7110)
1482 av7110->bin_fw = dvb_ttpci_fw;
1483 av7110->size_fw = sizeof(dvb_ttpci_fw);
1484 return check_firmware(av7110);
1486 #else
1487 static void put_firmware(struct av7110* av7110)
1489 vfree(av7110->bin_fw);
1492 static int get_firmware(struct av7110* av7110)
1494 int ret;
1495 const struct firmware *fw;
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;
1513 if (fw->size <= 200000) {
1514 printk("dvb-ttpci: this firmware is way too small.\n");
1515 release_firmware(fw);
1516 return -EINVAL;
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;
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);
1532 release_firmware(fw);
1533 return ret;
1535 #endif
1538 static int alps_bsrv2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
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;
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;
1553 buf[0] = (div >> 8) & 0x7f;
1554 buf[1] = div & 0xff;
1555 buf[2] = ((div & 0x18000) >> 10) | 0x95;
1556 buf[3] = (pwr << 6) | 0x30;
1558 // NOTE: since we're using a prescaler of 2, we set the
1559 // divisor frequency to 62.5kHz and divide by 125 above
1561 if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1)
1562 return -EIO;
1563 return 0;
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,
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
1616 static int alps_bsru6_set_symbol_rate(struct dvb_frontend* fe, u32 srate, u32 ratio)
1618 u8 aclk = 0;
1619 u8 bclk = 0;
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; }
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);
1634 return 0;
1637 static int alps_bsru6_pll_set(struct dvb_frontend* fe, struct i2c_adapter *i2c, struct dvb_frontend_parameters* params)
1639 int ret;
1640 u8 data[4];
1641 u32 div;
1642 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1644 if ((params->frequency < 950000) || (params->frequency > 2150000))
1645 return -EINVAL;
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;
1653 if (params->frequency > 1530000) data[3] = 0xc0;
1655 ret = i2c_transfer(i2c, &msg, 1);
1656 if (ret != 1)
1657 return -EIO;
1658 return 0;
1661 static struct stv0299_config alps_bsru6_config = {
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,
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
1718 static int alps_bsbe1_pll_set(struct dvb_frontend* fe, struct i2c_adapter *i2c, struct dvb_frontend_parameters* params)
1720 int ret;
1721 u8 data[4];
1722 u32 div;
1723 struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1725 if ((params->frequency < 950000) || (params->frequency > 2150000))
1726 return -EINVAL;
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;
1734 ret = i2c_transfer(i2c, &msg, 1);
1735 return (ret != 1) ? -EIO : 0;
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,
1749 static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
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) };
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;
1770 ret = i2c_transfer(&av7110->i2c_adap, &msg, 1);
1771 return (ret != 1) ? -EIO : 0;
1775 static int alps_tdbe2_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
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) };
1782 div = (params->frequency + 35937500 + 31250) / 62500;
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);
1789 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1790 return -EIO;
1791 return 0;
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,
1805 static int grundig_29504_451_pll_set(struct dvb_frontend* fe,
1806 struct dvb_frontend_parameters* params)
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) };
1813 div = params->frequency / 125;
1814 data[0] = (div >> 8) & 0x7f;
1815 data[1] = div & 0xff;
1816 data[2] = 0x8e;
1817 data[3] = 0x00;
1819 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1820 return -EIO;
1821 return 0;
1824 static struct tda8083_config grundig_29504_451_config = {
1825 .demod_address = 0x68,
1826 .pll_set = grundig_29504_451_pll_set,
1831 static int philips_cd1516_pll_set(struct dvb_frontend* fe,
1832 struct dvb_frontend_parameters* params)
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) };
1840 div = (f + 36125000 + 31250) / 62500;
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);
1847 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1848 return -EIO;
1849 return 0;
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,
1862 static int alps_tdlb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
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) };
1869 div = (params->frequency + 36200000) / 166666;
1871 if (params->frequency <= 782000000)
1872 pwr = 1;
1873 else
1874 pwr = 2;
1876 data[0] = (div >> 8) & 0x7f;
1877 data[1] = div & 0xff;
1878 data[2] = 0x85;
1879 data[3] = pwr << 6;
1881 if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1882 return -EIO;
1883 return 0;
1886 static int alps_tdlb7_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1888 struct av7110* av7110 = (struct av7110*) fe->dvb->priv;
1890 return request_firmware(fw, name, &av7110->dev->pci->dev);
1893 static struct sp8870_config alps_tdlb7_config = {
1895 .demod_address = 0x71,
1896 .pll_set = alps_tdlb7_pll_set,
1897 .request_firmware = alps_tdlb7_request_firmware,
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,
1994 static int nexusca_stv0297_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
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;
2003 div = (params->frequency + 36150000 + 31250) / 62500;
2005 data[0] = (div >> 8) & 0x7f;
2006 data[1] = div & 0xff;
2007 data[2] = 0xce;
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;
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;
2026 // wait for PLL lock
2027 for(i = 0; i < 20; i++) {
2029 stv0297_enable_plli2c(fe);
2030 if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
2031 if (data[0] & 0x40) break;
2032 msleep(10);
2035 return 0;
2038 static struct stv0297_config nexusca_stv0297_config = {
2040 .demod_address = 0x1C,
2041 .inittab = nexusca_stv0297_inittab,
2042 .invert = 1,
2043 .pll_set = nexusca_stv0297_pll_set,
2048 static int grundig_29504_401_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
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) };
2056 div = (36125000 + params->frequency) / 166666;
2058 cfg = 0x88;
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;
2066 if (params->frequency < 175000000) band_select = 0x0e;
2067 else if (params->frequency < 470000000) band_select = 0x05;
2068 else band_select = 0x03;
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;
2075 if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1) return -EIO;
2076 return 0;
2079 static struct l64781_config grundig_29504_401_config = {
2080 .demod_address = 0x55,
2081 .pll_set = grundig_29504_401_pll_set,
2086 static int av7110_fe_lock_fix(struct av7110* av7110, fe_status_t status)
2088 int ret = 0;
2089 int synced = (status & FE_HAS_LOCK) ? 1 : 0;
2091 av7110->fe_status = status;
2093 if (av7110->fe_synced == synced)
2094 return 0;
2096 if (av7110->playing)
2097 return 0;
2099 if (down_interruptible(&av7110->pid_mutex))
2100 return -ERESTARTSYS;
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);
2118 if (!ret)
2119 av7110->fe_synced = synced;
2121 up(&av7110->pid_mutex);
2122 return ret;
2125 static int av7110_fe_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
2127 struct av7110* av7110 = fe->dvb->priv;
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);
2134 return ret;
2137 static int av7110_fe_init(struct dvb_frontend* fe)
2139 struct av7110* av7110 = fe->dvb->priv;
2141 int ret = av7110_fe_lock_fix(av7110, 0);
2142 if (!ret)
2143 ret = av7110->fe_init(fe);
2144 return ret;
2147 static int av7110_fe_read_status(struct dvb_frontend* fe, fe_status_t* status)
2149 struct av7110* av7110 = fe->dvb->priv;
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;
2159 static int av7110_fe_diseqc_reset_overload(struct dvb_frontend* fe)
2161 struct av7110* av7110 = fe->dvb->priv;
2163 int ret = av7110_fe_lock_fix(av7110, 0);
2164 if (!ret)
2165 ret = av7110->fe_diseqc_reset_overload(fe);
2166 return ret;
2169 static int av7110_fe_diseqc_send_master_cmd(struct dvb_frontend* fe,
2170 struct dvb_diseqc_master_cmd* cmd)
2172 struct av7110* av7110 = fe->dvb->priv;
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);
2179 return ret;
2182 static int av7110_fe_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
2184 struct av7110* av7110 = fe->dvb->priv;
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);
2191 return ret;
2194 static int av7110_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
2196 struct av7110* av7110 = fe->dvb->priv;
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);
2203 return ret;
2206 static int av7110_fe_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
2208 struct av7110* av7110 = fe->dvb->priv;
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);
2215 return ret;
2218 static int av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend* fe, unsigned long cmd)
2220 struct av7110* av7110 = fe->dvb->priv;
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;
2228 static void dvb_s_recover(struct av7110* av7110)
2230 av7110_fe_init(av7110->fe);
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);
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);
2242 av7110_fe_set_frontend(av7110->fe, &av7110->saved_fe_params);
2245 static u8 read_pwm(struct av7110* av7110)
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} };
2252 if ((i2c_transfer(&av7110->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
2253 pwm = 0x48;
2255 return pwm;
2258 static int frontend_init(struct av7110 *av7110)
2260 int ret;
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;
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
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;
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;
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;
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;
2319 break;
2321 case 0x0001: // Hauppauge/TT Nexus-T premium rev1.X
2323 // ALPS TDLB7
2324 av7110->fe = sp8870_attach(&alps_tdlb7_config, &av7110->i2c_adap);
2325 break;
2327 case 0x0002: // Hauppauge/TT DVB-C premium rev2.X
2329 av7110->fe = ves1820_attach(&alps_tdbe2_config, &av7110->i2c_adap, read_pwm(av7110));
2330 break;
2332 case 0x0004: // Galaxis DVB-S rev1.3
2333 /* ALPS BSRV2 */
2334 av7110->fe = ves1x93_attach(&alps_bsrv2_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;
2341 break;
2343 case 0x0006: /* Fujitsu-Siemens DVB-S rev 1.6 */
2344 /* Grundig 29504-451 */
2345 av7110->fe = tda8083_attach(&grundig_29504_451_config, &av7110->i2c_adap);
2346 if (av7110->fe) {
2347 av7110->fe->ops->diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2348 av7110->fe->ops->diseqc_send_burst = av7110_diseqc_send_burst;
2349 av7110->fe->ops->set_tone = av7110_set_tone;
2350 av7110->recover = dvb_s_recover;
2352 break;
2354 case 0x0008: // Hauppauge/TT DVB-T
2356 av7110->fe = l64781_attach(&grundig_29504_401_config, &av7110->i2c_adap);
2357 break;
2359 case 0x000A: // Hauppauge/TT Nexus-CA rev1.X
2361 av7110->fe = stv0297_attach(&nexusca_stv0297_config, &av7110->i2c_adap);
2362 if (av7110->fe) {
2363 /* set TDA9819 into DVB mode */
2364 saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9198 pin9(STD)
2365 saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9198 pin30(VIF)
2367 /* tuner on this needs a slower i2c bus speed */
2368 av7110->dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
2369 break;
2371 break;
2373 case 0x000E: /* Hauppauge/TT Nexus-S rev 2.3 */
2374 /* ALPS BSBE1 */
2375 av7110->fe = stv0299_attach(&alps_bsbe1_config, &av7110->i2c_adap);
2376 if (av7110->fe) {
2377 av7110->fe->ops->set_voltage = lnbp21_set_voltage;
2378 av7110->fe->ops->dishnetwork_send_legacy_command = NULL;
2379 av7110->recover = dvb_s_recover;
2381 break;
2385 if (!av7110->fe) {
2386 /* FIXME: propagate the failure code from the lower layers */
2387 ret = -ENOMEM;
2388 printk("dvb-ttpci: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
2389 av7110->dev->pci->vendor,
2390 av7110->dev->pci->device,
2391 av7110->dev->pci->subsystem_vendor,
2392 av7110->dev->pci->subsystem_device);
2393 } else {
2394 FE_FUNC_OVERRIDE(av7110->fe->ops->init, av7110->fe_init, av7110_fe_init);
2395 FE_FUNC_OVERRIDE(av7110->fe->ops->read_status, av7110->fe_read_status, av7110_fe_read_status);
2396 FE_FUNC_OVERRIDE(av7110->fe->ops->diseqc_reset_overload, av7110->fe_diseqc_reset_overload, av7110_fe_diseqc_reset_overload);
2397 FE_FUNC_OVERRIDE(av7110->fe->ops->diseqc_send_master_cmd, av7110->fe_diseqc_send_master_cmd, av7110_fe_diseqc_send_master_cmd);
2398 FE_FUNC_OVERRIDE(av7110->fe->ops->diseqc_send_burst, av7110->fe_diseqc_send_burst, av7110_fe_diseqc_send_burst);
2399 FE_FUNC_OVERRIDE(av7110->fe->ops->set_tone, av7110->fe_set_tone, av7110_fe_set_tone);
2400 FE_FUNC_OVERRIDE(av7110->fe->ops->set_voltage, av7110->fe_set_voltage, av7110_fe_set_voltage;)
2401 FE_FUNC_OVERRIDE(av7110->fe->ops->dishnetwork_send_legacy_command, av7110->fe_dishnetwork_send_legacy_command, av7110_fe_dishnetwork_send_legacy_command);
2402 FE_FUNC_OVERRIDE(av7110->fe->ops->set_frontend, av7110->fe_set_frontend, av7110_fe_set_frontend);
2404 ret = dvb_register_frontend(&av7110->dvb_adapter, av7110->fe);
2405 if (ret < 0) {
2406 printk("av7110: Frontend registration failed!\n");
2407 if (av7110->fe->ops->release)
2408 av7110->fe->ops->release(av7110->fe);
2409 av7110->fe = NULL;
2412 return ret;
2415 /* Budgetpatch note:
2416 * Original hardware design by Roberto Deza:
2417 * There is a DVB_Wiki at
2418 * http://212.227.36.83/linuxtv/wiki/index.php/Main_Page
2419 * where is described this 'DVB TT Budget Patch', on Card Modding:
2420 * http://212.227.36.83/linuxtv/wiki/index.php/DVB_TT_Budget_Patch
2421 * On the short description there is also a link to a external file,
2422 * with more details:
2423 * http://perso.wanadoo.es/jesussolano/Ttf_tsc1.zip
2425 * New software triggering design by Emard that works on
2426 * original Roberto Deza's hardware:
2428 * rps1 code for budgetpatch will copy internal HS event to GPIO3 pin.
2429 * GPIO3 is in budget-patch hardware connectd to port B VSYNC
2430 * HS is an internal event of 7146, accessible with RPS
2431 * and temporarily raised high every n lines
2432 * (n in defined in the RPS_THRESH1 counter threshold)
2433 * I think HS is raised high on the beginning of the n-th line
2434 * and remains high until this n-th line that triggered
2435 * it is completely received. When the receiption of n-th line
2436 * ends, HS is lowered.
2438 * To transmit data over DMA, 7146 needs changing state at
2439 * port B VSYNC pin. Any changing of port B VSYNC will
2440 * cause some DMA data transfer, with more or less packets loss.
2441 * It depends on the phase and frequency of VSYNC and
2442 * the way of 7146 is instructed to trigger on port B (defined
2443 * in DD1_INIT register, 3rd nibble from the right valid
2444 * numbers are 0-7, see datasheet)
2446 * The correct triggering can minimize packet loss,
2447 * dvbtraffic should give this stable bandwidths:
2448 * 22k transponder = 33814 kbit/s
2449 * 27.5k transponder = 38045 kbit/s
2450 * by experiment it is found that the best results
2451 * (stable bandwidths and almost no packet loss)
2452 * are obtained using DD1_INIT triggering number 2
2453 * (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
2454 * and a VSYNC phase that occurs in the middle of DMA transfer
2455 * (about byte 188*512=96256 in the DMA window).
2457 * Phase of HS is still not clear to me how to control,
2458 * It just happens to be so. It can be seen if one enables
2459 * RPS_IRQ and print Event Counter 1 in vpeirq(). Every
2460 * time RPS_INTERRUPT is called, the Event Counter 1 will
2461 * increment. That's how the 7146 is programmed to do event
2462 * counting in this budget-patch.c
2463 * I *think* HPS setting has something to do with the phase
2464 * of HS but I cant be 100% sure in that.
2466 * hardware debug note: a working budget card (including budget patch)
2467 * with vpeirq() interrupt setup in mode "0x90" (every 64K) will
2468 * generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
2469 * and that means 3*25=75 Hz of interrupt freqency, as seen by
2470 * watch cat /proc/interrupts
2472 * If this frequency is 3x lower (and data received in the DMA
2473 * buffer don't start with 0x47, but in the middle of packets,
2474 * whose lengths appear to be like 188 292 188 104 etc.
2475 * this means VSYNC line is not connected in the hardware.
2476 * (check soldering pcb and pins)
2477 * The same behaviour of missing VSYNC can be duplicated on budget
2478 * cards, by seting DD1_INIT trigger mode 7 in 3rd nibble.
2480 static int __devinit av7110_attach(struct saa7146_dev* dev,
2481 struct saa7146_pci_extension_data *pci_ext)
2483 const int length = TS_WIDTH * TS_HEIGHT;
2484 struct pci_dev *pdev = dev->pci;
2485 struct av7110 *av7110;
2486 int ret, count = 0;
2488 dprintk(4, "dev: %p\n", dev);
2490 /* Set RPS_IRQ to 1 to track rps1 activity.
2491 * Enabling this won't send any interrupt to PC CPU.
2493 #define RPS_IRQ 0
2495 if (budgetpatch == 1) {
2496 budgetpatch = 0;
2497 /* autodetect the presence of budget patch
2498 * this only works if saa7146 has been recently
2499 * reset with with MASK_31 to MC1
2501 * will wait for VBI_B event (vertical blank at port B)
2502 * and will reset GPIO3 after VBI_B is detected.
2503 * (GPIO3 should be raised high by CPU to
2504 * test if GPIO3 will generate vertical blank signal
2505 * in budget patch GPIO3 is connected to VSYNC_B
2508 /* RESET SAA7146 */
2509 saa7146_write(dev, MC1, MASK_31);
2510 /* autodetection success seems to be time-dependend after reset */
2512 /* Fix VSYNC level */
2513 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2514 /* set vsync_b triggering */
2515 saa7146_write(dev, DD1_STREAM_B, 0);
2516 /* port B VSYNC at rising edge */
2517 saa7146_write(dev, DD1_INIT, 0x00000200);
2518 saa7146_write(dev, BRS_CTRL, 0x00000000); // VBI
2519 saa7146_write(dev, MC2,
2520 1 * (MASK_08 | MASK_24) | // BRS control
2521 0 * (MASK_09 | MASK_25) | // a
2522 1 * (MASK_10 | MASK_26) | // b
2523 0 * (MASK_06 | MASK_22) | // HPS_CTRL1
2524 0 * (MASK_05 | MASK_21) | // HPS_CTRL2
2525 0 * (MASK_01 | MASK_15) // DEBI
2528 /* start writing RPS1 code from beginning */
2529 count = 0;
2530 /* Disable RPS1 */
2531 saa7146_write(dev, MC1, MASK_29);
2532 /* RPS1 timeout disable */
2533 saa7146_write(dev, RPS_TOV1, 0);
2534 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_VBI_B));
2535 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2536 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2537 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
2538 #if RPS_IRQ
2539 /* issue RPS1 interrupt to increment counter */
2540 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2541 #endif
2542 WRITE_RPS1(cpu_to_le32(CMD_STOP));
2543 /* Jump to begin of RPS program as safety measure (p37) */
2544 WRITE_RPS1(cpu_to_le32(CMD_JUMP));
2545 WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
2547 #if RPS_IRQ
2548 /* set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
2549 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2550 * use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
2552 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2553 /* set event counter 1 treshold to maximum allowed value (rEC p55) */
2554 saa7146_write(dev, ECT1R, 0x3fff );
2555 #endif
2556 /* Set RPS1 Address register to point to RPS code (r108 p42) */
2557 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2558 /* Enable RPS1, (rFC p33) */
2559 saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
2561 mdelay(10);
2562 /* now send VSYNC_B to rps1 by rising GPIO3 */
2563 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
2564 mdelay(10);
2565 /* if rps1 responded by lowering the GPIO3,
2566 * then we have budgetpatch hardware
2568 if ((saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0) {
2569 budgetpatch = 1;
2570 printk("dvb-ttpci: BUDGET-PATCH DETECTED.\n");
2572 /* Disable RPS1 */
2573 saa7146_write(dev, MC1, ( MASK_29 ));
2574 #if RPS_IRQ
2575 printk("dvb-ttpci: Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
2576 #endif
2579 /* prepare the av7110 device struct */
2580 av7110 = kzalloc(sizeof(struct av7110), GFP_KERNEL);
2581 if (!av7110) {
2582 dprintk(1, "out of memory\n");
2583 return -ENOMEM;
2586 av7110->card_name = (char*) pci_ext->ext_priv;
2587 av7110->dev = dev;
2588 dev->ext_priv = av7110;
2590 ret = get_firmware(av7110);
2591 if (ret < 0)
2592 goto err_kfree_0;
2594 ret = dvb_register_adapter(&av7110->dvb_adapter, av7110->card_name,
2595 THIS_MODULE);
2596 if (ret < 0)
2597 goto err_put_firmware_1;
2599 /* the Siemens DVB needs this if you want to have the i2c chips
2600 get recognized before the main driver is fully loaded */
2601 saa7146_write(dev, GPIO_CTRL, 0x500000);
2603 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
2604 av7110->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
2605 #else
2606 av7110->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
2607 #endif
2608 strlcpy(av7110->i2c_adap.name, pci_ext->ext_priv, sizeof(av7110->i2c_adap.name));
2610 saa7146_i2c_adapter_prepare(dev, &av7110->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); /* 275 kHz */
2612 ret = i2c_add_adapter(&av7110->i2c_adap);
2613 if (ret < 0)
2614 goto err_dvb_unregister_adapter_2;
2616 ttpci_eeprom_parse_mac(&av7110->i2c_adap,
2617 av7110->dvb_adapter.proposed_mac);
2618 ret = -ENOMEM;
2620 if (budgetpatch) {
2621 spin_lock_init(&av7110->feedlock1);
2622 av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2623 &av7110->pt);
2624 if (!av7110->grabbing)
2625 goto err_i2c_del_3;
2627 saa7146_write(dev, PCI_BT_V1, 0x1c1f101f);
2628 saa7146_write(dev, BCS_CTRL, 0x80400040);
2629 /* set dd1 stream a & b */
2630 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2631 saa7146_write(dev, DD1_INIT, 0x03000200);
2632 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2633 saa7146_write(dev, BRS_CTRL, 0x60000000);
2634 saa7146_write(dev, BASE_ODD3, 0);
2635 saa7146_write(dev, BASE_EVEN3, 0);
2636 saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2637 saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2639 saa7146_write(dev, PITCH3, TS_WIDTH);
2640 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2642 /* upload all */
2643 saa7146_write(dev, MC2, 0x077c077c);
2644 saa7146_write(dev, GPIO_CTRL, 0x000000);
2645 #if RPS_IRQ
2646 /* set event counter 1 source as RPS1 interrupt (0x03) (rE4 p53)
2647 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2648 * use 0x15 to track VPE interrupts - increase by 1 every vpeirq() is called
2650 saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2651 /* set event counter 1 treshold to maximum allowed value (rEC p55) */
2652 saa7146_write(dev, ECT1R, 0x3fff );
2653 #endif
2654 /* Setup BUDGETPATCH MAIN RPS1 "program" (p35) */
2655 count = 0;
2657 /* Wait Source Line Counter Threshold (p36) */
2658 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | EVT_HS));
2659 /* Set GPIO3=1 (p42) */
2660 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2661 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2662 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTHI<<24));
2663 #if RPS_IRQ
2664 /* issue RPS1 interrupt */
2665 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2666 #endif
2667 /* Wait reset Source Line Counter Threshold (p36) */
2668 WRITE_RPS1(cpu_to_le32(CMD_PAUSE | RPS_INV | EVT_HS));
2669 /* Set GPIO3=0 (p42) */
2670 WRITE_RPS1(cpu_to_le32(CMD_WR_REG_MASK | (GPIO_CTRL>>2)));
2671 WRITE_RPS1(cpu_to_le32(GPIO3_MSK));
2672 WRITE_RPS1(cpu_to_le32(SAA7146_GPIO_OUTLO<<24));
2673 #if RPS_IRQ
2674 /* issue RPS1 interrupt */
2675 WRITE_RPS1(cpu_to_le32(CMD_INTERRUPT));
2676 #endif
2677 /* Jump to begin of RPS program (p37) */
2678 WRITE_RPS1(cpu_to_le32(CMD_JUMP));
2679 WRITE_RPS1(cpu_to_le32(dev->d_rps1.dma_handle));
2681 /* Fix VSYNC level */
2682 saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2683 /* Set RPS1 Address register to point to RPS code (r108 p42) */
2684 saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2685 /* Set Source Line Counter Threshold, using BRS (rCC p43)
2686 * It generates HS event every TS_HEIGHT lines
2687 * this is related to TS_WIDTH set in register
2688 * NUM_LINE_BYTE3. If NUM_LINE_BYTE low 16 bits
2689 * are set to TS_WIDTH bytes (TS_WIDTH=2*188),
2690 * then RPS_THRESH1 should be set to trigger
2691 * every TS_HEIGHT (512) lines.
2693 saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
2695 /* Enable RPS1 (rFC p33) */
2696 saa7146_write(dev, MC1, (MASK_13 | MASK_29));
2698 /* end of budgetpatch register initialization */
2699 tasklet_init (&av7110->vpe_tasklet, vpeirq, (unsigned long) av7110);
2700 } else {
2701 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
2702 saa7146_write(dev, BCS_CTRL, 0x80400040);
2704 /* set dd1 stream a & b */
2705 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2706 saa7146_write(dev, DD1_INIT, 0x03000000);
2707 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2709 /* upload all */
2710 saa7146_write(dev, MC2, 0x077c077c);
2711 saa7146_write(dev, GPIO_CTRL, 0x000000);
2714 tasklet_init (&av7110->debi_tasklet, debiirq, (unsigned long) av7110);
2715 tasklet_init (&av7110->gpio_tasklet, gpioirq, (unsigned long) av7110);
2717 sema_init(&av7110->pid_mutex, 1);
2719 /* locks for data transfers from/to AV7110 */
2720 spin_lock_init(&av7110->debilock);
2721 sema_init(&av7110->dcomlock, 1);
2722 av7110->debitype = -1;
2724 /* default OSD window */
2725 av7110->osdwin = 1;
2726 sema_init(&av7110->osd_sema, 1);
2728 /* ARM "watchdog" */
2729 init_waitqueue_head(&av7110->arm_wait);
2730 av7110->arm_thread = NULL;
2732 /* allocate and init buffers */
2733 av7110->debi_virt = pci_alloc_consistent(pdev, 8192, &av7110->debi_bus);
2734 if (!av7110->debi_virt)
2735 goto err_saa71466_vfree_4;
2738 av7110->iobuf = vmalloc(AVOUTLEN+AOUTLEN+BMPLEN+4*IPACKS);
2739 if (!av7110->iobuf)
2740 goto err_pci_free_5;
2742 ret = av7110_av_init(av7110);
2743 if (ret < 0)
2744 goto err_iobuf_vfree_6;
2746 /* init BMP buffer */
2747 av7110->bmpbuf = av7110->iobuf+AVOUTLEN+AOUTLEN;
2748 init_waitqueue_head(&av7110->bmpq);
2750 ret = av7110_ca_init(av7110);
2751 if (ret < 0)
2752 goto err_av7110_av_exit_7;
2754 /* load firmware into AV7110 cards */
2755 ret = av7110_bootarm(av7110);
2756 if (ret < 0)
2757 goto err_av7110_ca_exit_8;
2759 ret = av7110_firmversion(av7110);
2760 if (ret < 0)
2761 goto err_stop_arm_9;
2763 if (FW_VERSION(av7110->arm_app)<0x2501)
2764 printk ("dvb-ttpci: Warning, firmware version 0x%04x is too old. "
2765 "System might be unstable!\n", FW_VERSION(av7110->arm_app));
2767 ret = kernel_thread(arm_thread, (void *) av7110, 0);
2768 if (ret < 0)
2769 goto err_stop_arm_9;
2771 /* set initial volume in mixer struct */
2772 av7110->mixer.volume_left = volume;
2773 av7110->mixer.volume_right = volume;
2775 init_av7110_av(av7110);
2777 ret = av7110_register(av7110);
2778 if (ret < 0)
2779 goto err_arm_thread_stop_10;
2781 /* special case DVB-C: these cards have an analog tuner
2782 plus need some special handling, so we have separate
2783 saa7146_ext_vv data for these... */
2784 ret = av7110_init_v4l(av7110);
2785 if (ret < 0)
2786 goto err_av7110_unregister_11;
2788 av7110->dvb_adapter.priv = av7110;
2789 ret = frontend_init(av7110);
2790 if (ret < 0)
2791 goto err_av7110_exit_v4l_12;
2793 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2794 av7110_ir_init(av7110);
2795 #endif
2796 printk(KERN_INFO "dvb-ttpci: found av7110-%d.\n", av7110_num);
2797 av7110_num++;
2798 out:
2799 return ret;
2801 err_av7110_exit_v4l_12:
2802 av7110_exit_v4l(av7110);
2803 err_av7110_unregister_11:
2804 dvb_unregister(av7110);
2805 err_arm_thread_stop_10:
2806 av7110_arm_sync(av7110);
2807 err_stop_arm_9:
2808 /* Nothing to do. Rejoice. */
2809 err_av7110_ca_exit_8:
2810 av7110_ca_exit(av7110);
2811 err_av7110_av_exit_7:
2812 av7110_av_exit(av7110);
2813 err_iobuf_vfree_6:
2814 vfree(av7110->iobuf);
2815 err_pci_free_5:
2816 pci_free_consistent(pdev, 8192, av7110->debi_virt, av7110->debi_bus);
2817 err_saa71466_vfree_4:
2818 if (!av7110->grabbing)
2819 saa7146_pgtable_free(pdev, &av7110->pt);
2820 err_i2c_del_3:
2821 i2c_del_adapter(&av7110->i2c_adap);
2822 err_dvb_unregister_adapter_2:
2823 dvb_unregister_adapter(&av7110->dvb_adapter);
2824 err_put_firmware_1:
2825 put_firmware(av7110);
2826 err_kfree_0:
2827 kfree(av7110);
2828 goto out;
2831 static int __devexit av7110_detach(struct saa7146_dev* saa)
2833 struct av7110 *av7110 = saa->ext_priv;
2834 dprintk(4, "%p\n", av7110);
2836 #if defined(CONFIG_INPUT_EVDEV) || defined(CONFIG_INPUT_EVDEV_MODULE)
2837 av7110_ir_exit(av7110);
2838 #endif
2839 if (budgetpatch) {
2840 /* Disable RPS1 */
2841 saa7146_write(saa, MC1, MASK_29);
2842 /* VSYNC LOW (inactive) */
2843 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2844 saa7146_write(saa, MC1, MASK_20); /* DMA3 off */
2845 SAA7146_IER_DISABLE(saa, MASK_10);
2846 SAA7146_ISR_CLEAR(saa, MASK_10);
2847 msleep(50);
2848 tasklet_kill(&av7110->vpe_tasklet);
2849 saa7146_pgtable_free(saa->pci, &av7110->pt);
2851 av7110_exit_v4l(av7110);
2853 av7110_arm_sync(av7110);
2855 tasklet_kill(&av7110->debi_tasklet);
2856 tasklet_kill(&av7110->gpio_tasklet);
2858 dvb_unregister(av7110);
2860 SAA7146_IER_DISABLE(saa, MASK_19 | MASK_03);
2861 SAA7146_ISR_CLEAR(saa, MASK_19 | MASK_03);
2863 av7110_ca_exit(av7110);
2864 av7110_av_exit(av7110);
2866 vfree(av7110->iobuf);
2867 pci_free_consistent(saa->pci, 8192, av7110->debi_virt,
2868 av7110->debi_bus);
2870 i2c_del_adapter(&av7110->i2c_adap);
2872 dvb_unregister_adapter (&av7110->dvb_adapter);
2874 av7110_num--;
2876 put_firmware(av7110);
2878 kfree(av7110);
2880 saa->ext_priv = NULL;
2882 return 0;
2886 static void av7110_irq(struct saa7146_dev* dev, u32 *isr)
2888 struct av7110 *av7110 = dev->ext_priv;
2890 //print_time("av7110_irq");
2892 /* Note: Don't try to handle the DEBI error irq (MASK_18), in
2893 * intel mode the timeout is asserted all the time...
2896 if (*isr & MASK_19) {
2897 //printk("av7110_irq: DEBI\n");
2898 /* Note 1: The DEBI irq is level triggered: We must enable it
2899 * only after we started a DMA xfer, and disable it here
2900 * immediately, or it will be signalled all the time while
2901 * DEBI is idle.
2902 * Note 2: You would think that an irq which is masked is
2903 * not signalled by the hardware. Not so for the SAA7146:
2904 * An irq is signalled as long as the corresponding bit
2905 * in the ISR is set, and disabling irqs just prevents the
2906 * hardware from setting the ISR bit. This means a) that we
2907 * must clear the ISR *after* disabling the irq (which is why
2908 * we must do it here even though saa7146_core did it already),
2909 * and b) that if we were to disable an edge triggered irq
2910 * (like the gpio irqs sadly are) temporarily we would likely
2911 * loose some. This sucks :-(
2913 SAA7146_IER_DISABLE(av7110->dev, MASK_19);
2914 SAA7146_ISR_CLEAR(av7110->dev, MASK_19);
2915 tasklet_schedule(&av7110->debi_tasklet);
2918 if (*isr & MASK_03) {
2919 //printk("av7110_irq: GPIO\n");
2920 tasklet_schedule(&av7110->gpio_tasklet);
2923 if ((*isr & MASK_10) && budgetpatch)
2924 tasklet_schedule(&av7110->vpe_tasklet);
2928 static struct saa7146_extension av7110_extension;
2930 #define MAKE_AV7110_INFO(x_var,x_name) \
2931 static struct saa7146_pci_extension_data x_var = { \
2932 .ext_priv = x_name, \
2933 .ext = &av7110_extension }
2935 MAKE_AV7110_INFO(tts_1_X_fsc,"Technotrend/Hauppauge WinTV DVB-S rev1.X or Fujitsu Siemens DVB-C");
2936 MAKE_AV7110_INFO(ttt_1_X, "Technotrend/Hauppauge WinTV DVB-T rev1.X");
2937 MAKE_AV7110_INFO(ttc_1_X, "Technotrend/Hauppauge WinTV Nexus-CA rev1.X");
2938 MAKE_AV7110_INFO(ttc_2_X, "Technotrend/Hauppauge WinTV DVB-C rev2.X");
2939 MAKE_AV7110_INFO(tts_2_X, "Technotrend/Hauppauge WinTV Nexus-S rev2.X");
2940 MAKE_AV7110_INFO(tts_2_3, "Technotrend/Hauppauge WinTV Nexus-S rev2.3");
2941 MAKE_AV7110_INFO(tts_1_3se, "Technotrend/Hauppauge WinTV DVB-S rev1.3 SE");
2942 MAKE_AV7110_INFO(ttt, "Technotrend/Hauppauge DVB-T");
2943 MAKE_AV7110_INFO(fsc, "Fujitsu Siemens DVB-C");
2944 MAKE_AV7110_INFO(fss, "Fujitsu Siemens DVB-S rev1.6");
2945 MAKE_AV7110_INFO(gxs_1_3, "Galaxis DVB-S rev1.3");
2947 static struct pci_device_id pci_tbl[] = {
2948 MAKE_EXTENSION_PCI(fsc, 0x110a, 0x0000),
2949 MAKE_EXTENSION_PCI(tts_1_X_fsc, 0x13c2, 0x0000),
2950 MAKE_EXTENSION_PCI(ttt_1_X, 0x13c2, 0x0001),
2951 MAKE_EXTENSION_PCI(ttc_2_X, 0x13c2, 0x0002),
2952 MAKE_EXTENSION_PCI(tts_2_X, 0x13c2, 0x0003),
2953 MAKE_EXTENSION_PCI(gxs_1_3, 0x13c2, 0x0004),
2954 MAKE_EXTENSION_PCI(fss, 0x13c2, 0x0006),
2955 MAKE_EXTENSION_PCI(ttt, 0x13c2, 0x0008),
2956 MAKE_EXTENSION_PCI(ttc_1_X, 0x13c2, 0x000a),
2957 MAKE_EXTENSION_PCI(tts_2_3, 0x13c2, 0x000e),
2958 MAKE_EXTENSION_PCI(tts_1_3se, 0x13c2, 0x1002),
2960 /* MAKE_EXTENSION_PCI(???, 0x13c2, 0x0005), UNDEFINED CARD */ // Technisat SkyStar1
2961 /* MAKE_EXTENSION_PCI(???, 0x13c2, 0x0009), UNDEFINED CARD */ // TT/Hauppauge WinTV Nexus-CA v????
2964 .vendor = 0,
2968 MODULE_DEVICE_TABLE(pci, pci_tbl);
2971 static struct saa7146_extension av7110_extension = {
2972 .name = "dvb\0",
2973 .flags = SAA7146_I2C_SHORT_DELAY,
2975 .module = THIS_MODULE,
2976 .pci_tbl = &pci_tbl[0],
2977 .attach = av7110_attach,
2978 .detach = __devexit_p(av7110_detach),
2980 .irq_mask = MASK_19 | MASK_03 | MASK_10,
2981 .irq_func = av7110_irq,
2985 static int __init av7110_init(void)
2987 int retval;
2988 retval = saa7146_register_extension(&av7110_extension);
2989 return retval;
2993 static void __exit av7110_exit(void)
2995 saa7146_unregister_extension(&av7110_extension);
2998 module_init(av7110_init);
2999 module_exit(av7110_exit);
3001 MODULE_DESCRIPTION("driver for the SAA7146 based AV110 PCI DVB cards by "
3002 "Siemens, Technotrend, Hauppauge");
3003 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
3004 MODULE_LICENSE("GPL");