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