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