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