xref: /linux/drivers/media/usb/dvb-usb/pctv452e.c (revision d30c1683aaecb93d2ab95685dc4300a33d3cea7a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PCTV 452e DVB driver
4  *
5  * Copyright (c) 2006-2008 Dominik Kuhlen <dkuhlen@gmx.net>
6  *
7  * TT connect S2-3650-CI Common Interface support, MAC readout
8  * Copyright (C) 2008 Michael H. Schimek <mschimek@gmx.at>
9  */
10 
11 /* dvb usb framework */
12 #define DVB_USB_LOG_PREFIX "pctv452e"
13 #include "dvb-usb.h"
14 
15 /* Demodulator */
16 #include "stb0899_drv.h"
17 #include "stb0899_reg.h"
18 #include "stb0899_cfg.h"
19 /* Tuner */
20 #include "stb6100.h"
21 #include "stb6100_cfg.h"
22 /* FE Power */
23 #include "isl6423.h"
24 #include "lnbp22.h"
25 
26 #include <media/dvb_ca_en50221.h>
27 #include "ttpci-eeprom.h"
28 
29 #include <linux/etherdevice.h>
30 
31 static int debug;
32 module_param(debug, int, 0644);
33 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
34 
35 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
36 
37 #define ISOC_INTERFACE_ALTERNATIVE 3
38 
39 #define SYNC_BYTE_OUT 0xaa
40 #define SYNC_BYTE_IN  0x55
41 
42 /* guessed: (copied from ttusb-budget) */
43 #define PCTV_CMD_RESET 0x15
44 /* command to poll IR receiver */
45 #define PCTV_CMD_IR    0x1b
46 /* command to send I2C  */
47 #define PCTV_CMD_I2C   0x31
48 
49 #define I2C_ADDR_STB0899 (0xd0 >> 1)
50 #define I2C_ADDR_STB6100 (0xc0 >> 1)
51 #define I2C_ADDR_LNBP22  (0x10 >> 1)
52 #define I2C_ADDR_24C16   (0xa0 >> 1)
53 #define I2C_ADDR_24C64   (0xa2 >> 1)
54 
55 
56 /* pctv452e sends us this amount of data for each issued usb-command */
57 #define PCTV_ANSWER_LEN 64
58 /* Wait up to 1000ms for device  */
59 #define PCTV_TIMEOUT 1000
60 
61 
62 #define PCTV_LED_GPIO   STB0899_GPIO01
63 #define PCTV_LED_GREEN  0x82
64 #define PCTV_LED_ORANGE 0x02
65 
66 #define ci_dbg(format, arg...)				\
67 do {							\
68 	if (0)						\
69 		printk(KERN_DEBUG DVB_USB_LOG_PREFIX	\
70 			": " format "\n" , ## arg);	\
71 } while (0)
72 
73 enum {
74 	TT3650_CMD_CI_TEST = 0x40,
75 	TT3650_CMD_CI_RD_CTRL,
76 	TT3650_CMD_CI_WR_CTRL,
77 	TT3650_CMD_CI_RD_ATTR,
78 	TT3650_CMD_CI_WR_ATTR,
79 	TT3650_CMD_CI_RESET,
80 	TT3650_CMD_CI_SET_VIDEO_PORT
81 };
82 
83 
84 static struct stb0899_postproc pctv45e_postproc[] = {
85 	{ PCTV_LED_GPIO, STB0899_GPIOPULLUP },
86 	{ 0, 0 }
87 };
88 
89 static struct isl6423_config pctv452e_isl6423_config = {
90 	.current_max		= SEC_CURRENT_515m,
91 	.curlim			= SEC_CURRENT_LIM_ON,
92 	.mod_extern		= 1,
93 	.addr			= 0x08,
94 };
95 
96 /*
97  * stores all private variables for communication with the PCTV452e DVB-S2
98  */
99 struct pctv452e_state {
100 	struct dvb_ca_en50221 ca;
101 	struct mutex ca_mutex;
102 
103 	u8 c;	   /* transaction counter, wraps around...  */
104 	u8 initialized; /* set to 1 if 0x15 has been sent */
105 	u16 last_rc_key;
106 };
107 
108 static int tt3650_ci_msg(struct dvb_usb_device *d, u8 cmd, u8 *data,
109 			 unsigned int write_len, unsigned int read_len)
110 {
111 	struct pctv452e_state *state = d->priv;
112 	u8 *buf;
113 	u8 id;
114 	unsigned int rlen;
115 	int ret;
116 
117 	if (!data || (write_len > 64 - 4) || (read_len > 64 - 4)) {
118 		err("%s: transfer data invalid", __func__);
119 		return -EIO;
120 	}
121 
122 	buf = kmalloc(64, GFP_KERNEL);
123 	if (!buf)
124 		return -ENOMEM;
125 
126 	id = state->c++;
127 
128 	buf[0] = SYNC_BYTE_OUT;
129 	buf[1] = id;
130 	buf[2] = cmd;
131 	buf[3] = write_len;
132 
133 	memcpy(buf + 4, data, write_len);
134 
135 	rlen = (read_len > 0) ? 64 : 0;
136 	ret = dvb_usb_generic_rw(d, buf, 4 + write_len,
137 				  buf, rlen, /* delay_ms */ 0);
138 	if (0 != ret)
139 		goto failed;
140 
141 	ret = -EIO;
142 	if (SYNC_BYTE_IN != buf[0] || id != buf[1])
143 		goto failed;
144 
145 	memcpy(data, buf + 4, read_len);
146 
147 	kfree(buf);
148 	return 0;
149 
150 failed:
151 	err("CI error %d; %02X %02X %02X -> %*ph.",
152 	     ret, SYNC_BYTE_OUT, id, cmd, 3, buf);
153 
154 	kfree(buf);
155 	return ret;
156 }
157 
158 static int tt3650_ci_msg_locked(struct dvb_ca_en50221 *ca,
159 				u8 cmd, u8 *data, unsigned int write_len,
160 				unsigned int read_len)
161 {
162 	struct dvb_usb_device *d = ca->data;
163 	struct pctv452e_state *state = d->priv;
164 	int ret;
165 
166 	mutex_lock(&state->ca_mutex);
167 	ret = tt3650_ci_msg(d, cmd, data, write_len, read_len);
168 	mutex_unlock(&state->ca_mutex);
169 
170 	return ret;
171 }
172 
173 static int tt3650_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
174 				 int slot, int address)
175 {
176 	u8 buf[3];
177 	int ret;
178 
179 	if (0 != slot)
180 		return -EINVAL;
181 
182 	buf[0] = (address >> 8) & 0x0F;
183 	buf[1] = address;
184 
185 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_ATTR, buf, 2, 3);
186 
187 	ci_dbg("%s %04x -> %d 0x%02x",
188 		__func__, address, ret, buf[2]);
189 
190 	if (ret < 0)
191 		return ret;
192 
193 	return buf[2];
194 }
195 
196 static int tt3650_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
197 				 int slot, int address, u8 value)
198 {
199 	u8 buf[3];
200 
201 	ci_dbg("%s %d 0x%04x 0x%02x",
202 		__func__, slot, address, value);
203 
204 	if (0 != slot)
205 		return -EINVAL;
206 
207 	buf[0] = (address >> 8) & 0x0F;
208 	buf[1] = address;
209 	buf[2] = value;
210 
211 	return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_ATTR, buf, 3, 3);
212 }
213 
214 static int tt3650_ci_read_cam_control(struct dvb_ca_en50221 *ca,
215 				 int			slot,
216 				 u8			address)
217 {
218 	u8 buf[2];
219 	int ret;
220 
221 	if (0 != slot)
222 		return -EINVAL;
223 
224 	buf[0] = address & 3;
225 
226 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_CTRL, buf, 1, 2);
227 
228 	ci_dbg("%s 0x%02x -> %d 0x%02x",
229 		__func__, address, ret, buf[1]);
230 
231 	if (ret < 0)
232 		return ret;
233 
234 	return buf[1];
235 }
236 
237 static int tt3650_ci_write_cam_control(struct dvb_ca_en50221 *ca,
238 				 int			slot,
239 				 u8			address,
240 				 u8			value)
241 {
242 	u8 buf[2];
243 
244 	ci_dbg("%s %d 0x%02x 0x%02x",
245 		__func__, slot, address, value);
246 
247 	if (0 != slot)
248 		return -EINVAL;
249 
250 	buf[0] = address;
251 	buf[1] = value;
252 
253 	return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_CTRL, buf, 2, 2);
254 }
255 
256 static int tt3650_ci_set_video_port(struct dvb_ca_en50221 *ca,
257 				 int			slot,
258 				 int			enable)
259 {
260 	u8 buf[1];
261 	int ret;
262 
263 	ci_dbg("%s %d %d", __func__, slot, enable);
264 
265 	if (0 != slot)
266 		return -EINVAL;
267 
268 	enable = !!enable;
269 	buf[0] = enable;
270 
271 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
272 	if (ret < 0)
273 		return ret;
274 
275 	if (enable != buf[0]) {
276 		err("CI not %sabled.", enable ? "en" : "dis");
277 		return -EIO;
278 	}
279 
280 	return 0;
281 }
282 
283 static int tt3650_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
284 {
285 	return tt3650_ci_set_video_port(ca, slot, /* enable */ 0);
286 }
287 
288 static int tt3650_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
289 {
290 	return tt3650_ci_set_video_port(ca, slot, /* enable */ 1);
291 }
292 
293 static int tt3650_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
294 {
295 	struct dvb_usb_device *d = ca->data;
296 	struct pctv452e_state *state = d->priv;
297 	u8 buf[1];
298 	int ret;
299 
300 	ci_dbg("%s %d", __func__, slot);
301 
302 	if (0 != slot)
303 		return -EINVAL;
304 
305 	buf[0] = 0;
306 
307 	mutex_lock(&state->ca_mutex);
308 
309 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
310 	if (0 != ret)
311 		goto failed;
312 
313 	msleep(500);
314 
315 	buf[0] = 1;
316 
317 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
318 	if (0 != ret)
319 		goto failed;
320 
321 	msleep(500);
322 
323 	buf[0] = 0; /* FTA */
324 
325 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
326 
327  failed:
328 	mutex_unlock(&state->ca_mutex);
329 
330 	return ret;
331 }
332 
333 static int tt3650_ci_poll_slot_status(struct dvb_ca_en50221 *ca,
334 				 int			slot,
335 				 int			open)
336 {
337 	u8 buf[1];
338 	int ret;
339 
340 	if (0 != slot)
341 		return -EINVAL;
342 
343 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_TEST, buf, 0, 1);
344 	if (0 != ret)
345 		return ret;
346 
347 	if (1 == buf[0])
348 		return DVB_CA_EN50221_POLL_CAM_PRESENT |
349 			DVB_CA_EN50221_POLL_CAM_READY;
350 
351 	return 0;
352 
353 }
354 
355 static void tt3650_ci_uninit(struct dvb_usb_device *d)
356 {
357 	struct pctv452e_state *state;
358 
359 	ci_dbg("%s", __func__);
360 
361 	if (NULL == d)
362 		return;
363 
364 	state = d->priv;
365 	if (NULL == state)
366 		return;
367 
368 	if (NULL == state->ca.data)
369 		return;
370 
371 	/* Error ignored. */
372 	tt3650_ci_set_video_port(&state->ca, /* slot */ 0, /* enable */ 0);
373 
374 	dvb_ca_en50221_release(&state->ca);
375 
376 	memset(&state->ca, 0, sizeof(state->ca));
377 }
378 
379 static int tt3650_ci_init(struct dvb_usb_adapter *a)
380 {
381 	struct dvb_usb_device *d = a->dev;
382 	struct pctv452e_state *state = d->priv;
383 	int ret;
384 
385 	ci_dbg("%s", __func__);
386 
387 	mutex_init(&state->ca_mutex);
388 
389 	state->ca.owner = THIS_MODULE;
390 	state->ca.read_attribute_mem = tt3650_ci_read_attribute_mem;
391 	state->ca.write_attribute_mem = tt3650_ci_write_attribute_mem;
392 	state->ca.read_cam_control = tt3650_ci_read_cam_control;
393 	state->ca.write_cam_control = tt3650_ci_write_cam_control;
394 	state->ca.slot_reset = tt3650_ci_slot_reset;
395 	state->ca.slot_shutdown = tt3650_ci_slot_shutdown;
396 	state->ca.slot_ts_enable = tt3650_ci_slot_ts_enable;
397 	state->ca.poll_slot_status = tt3650_ci_poll_slot_status;
398 	state->ca.data = d;
399 
400 	ret = dvb_ca_en50221_init(&a->dvb_adap,
401 				   &state->ca,
402 				   /* flags */ 0,
403 				   /* n_slots */ 1);
404 	if (0 != ret) {
405 		err("Cannot initialize CI: Error %d.", ret);
406 		memset(&state->ca, 0, sizeof(state->ca));
407 		return ret;
408 	}
409 
410 	info("CI initialized.");
411 
412 	return 0;
413 }
414 
415 #define CMD_BUFFER_SIZE 0x28
416 static int pctv452e_i2c_msg(struct dvb_usb_device *d, u8 addr,
417 				const u8 *snd_buf, u8 snd_len,
418 				u8 *rcv_buf, u8 rcv_len)
419 {
420 	struct pctv452e_state *state = d->priv;
421 	u8 *buf;
422 	u8 id;
423 	int ret;
424 
425 	if (snd_len > 64 - 7 || rcv_len > 64 - 7)
426 		return -EINVAL;
427 
428 	buf = kmalloc(64, GFP_KERNEL);
429 	if (!buf)
430 		return -ENOMEM;
431 
432 	id = state->c++;
433 
434 	buf[0] = SYNC_BYTE_OUT;
435 	buf[1] = id;
436 	buf[2] = PCTV_CMD_I2C;
437 	buf[3] = snd_len + 3;
438 	buf[4] = addr << 1;
439 	buf[5] = snd_len;
440 	buf[6] = rcv_len;
441 
442 	memcpy(buf + 7, snd_buf, snd_len);
443 
444 	ret = dvb_usb_generic_rw(d, buf, 7 + snd_len,
445 				  buf, /* rcv_len */ 64,
446 				  /* delay_ms */ 0);
447 	if (ret < 0)
448 		goto failed;
449 
450 	/* TT USB protocol error. */
451 	ret = -EIO;
452 	if (SYNC_BYTE_IN != buf[0] || id != buf[1])
453 		goto failed;
454 
455 	/* I2C device didn't respond as expected. */
456 	ret = -EREMOTEIO;
457 	if (buf[5] < snd_len || buf[6] < rcv_len)
458 		goto failed;
459 
460 	memcpy(rcv_buf, buf + 7, rcv_len);
461 
462 	kfree(buf);
463 	return rcv_len;
464 
465 failed:
466 	err("I2C error %d; %02X %02X  %02X %02X %02X -> %*ph",
467 	     ret, SYNC_BYTE_OUT, id, addr << 1, snd_len, rcv_len,
468 	     7, buf);
469 
470 	kfree(buf);
471 	return ret;
472 }
473 
474 static int pctv452e_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg,
475 				int num)
476 {
477 	struct dvb_usb_device *d = i2c_get_adapdata(adapter);
478 	int i;
479 
480 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
481 		return -EAGAIN;
482 
483 	for (i = 0; i < num; i++) {
484 		u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
485 		int ret;
486 
487 		if (msg[i].flags & I2C_M_RD) {
488 			addr = msg[i].addr;
489 			snd_buf = NULL;
490 			snd_len = 0;
491 			rcv_buf = msg[i].buf;
492 			rcv_len = msg[i].len;
493 		} else {
494 			addr = msg[i].addr;
495 			snd_buf = msg[i].buf;
496 			snd_len = msg[i].len;
497 			rcv_buf = NULL;
498 			rcv_len = 0;
499 		}
500 
501 		ret = pctv452e_i2c_msg(d, addr, snd_buf, snd_len, rcv_buf,
502 					rcv_len);
503 		if (ret < rcv_len)
504 			break;
505 	}
506 
507 	mutex_unlock(&d->i2c_mutex);
508 	return i;
509 }
510 
511 static u32 pctv452e_i2c_func(struct i2c_adapter *adapter)
512 {
513 	return I2C_FUNC_I2C;
514 }
515 
516 static int pctv452e_power_ctrl(struct dvb_usb_device *d, int i)
517 {
518 	struct pctv452e_state *state = d->priv;
519 	u8 *b0, *rx;
520 	int ret;
521 
522 	info("%s: %d\n", __func__, i);
523 
524 	if (!i)
525 		return 0;
526 
527 	if (state->initialized)
528 		return 0;
529 
530 	b0 = kmalloc(5 + PCTV_ANSWER_LEN, GFP_KERNEL);
531 	if (!b0)
532 		return -ENOMEM;
533 
534 	rx = b0 + 5;
535 
536 	/* hmm where should this should go? */
537 	ret = usb_set_interface(d->udev, 0, ISOC_INTERFACE_ALTERNATIVE);
538 	if (ret != 0)
539 		info("%s: Warning set interface returned: %d\n",
540 			__func__, ret);
541 
542 	/* this is a one-time initialization, don't know where to put */
543 	b0[0] = 0xaa;
544 	b0[1] = state->c++;
545 	b0[2] = PCTV_CMD_RESET;
546 	b0[3] = 1;
547 	b0[4] = 0;
548 	/* reset board */
549 	ret = dvb_usb_generic_rw(d, b0, 5, rx, PCTV_ANSWER_LEN, 0);
550 	if (ret)
551 		goto ret;
552 
553 	b0[1] = state->c++;
554 	b0[4] = 1;
555 	/* reset board (again?) */
556 	ret = dvb_usb_generic_rw(d, b0, 5, rx, PCTV_ANSWER_LEN, 0);
557 	if (ret)
558 		goto ret;
559 
560 	state->initialized = 1;
561 
562 ret:
563 	kfree(b0);
564 	return ret;
565 }
566 
567 static int pctv452e_rc_query(struct dvb_usb_device *d)
568 {
569 	struct pctv452e_state *state = d->priv;
570 	u8 *b, *rx;
571 	int ret, i;
572 	u8 id;
573 
574 	b = kmalloc(CMD_BUFFER_SIZE + PCTV_ANSWER_LEN, GFP_KERNEL);
575 	if (!b)
576 		return -ENOMEM;
577 
578 	rx = b + CMD_BUFFER_SIZE;
579 
580 	id = state->c++;
581 
582 	/* prepare command header  */
583 	b[0] = SYNC_BYTE_OUT;
584 	b[1] = id;
585 	b[2] = PCTV_CMD_IR;
586 	b[3] = 0;
587 
588 	/* send ir request */
589 	ret = dvb_usb_generic_rw(d, b, 4, rx, PCTV_ANSWER_LEN, 0);
590 	if (ret != 0)
591 		goto ret;
592 
593 	if (debug > 3) {
594 		info("%s: read: %2d: %*ph: ", __func__, ret, 3, rx);
595 		for (i = 0; (i < rx[3]) && ((i+3) < PCTV_ANSWER_LEN); i++)
596 			info(" %02x", rx[i+3]);
597 
598 		info("\n");
599 	}
600 
601 	if ((rx[3] == 9) &&  (rx[12] & 0x01)) {
602 		/* got a "press" event */
603 		state->last_rc_key = RC_SCANCODE_RC5(rx[7], rx[6]);
604 		if (debug > 2)
605 			info("%s: cmd=0x%02x sys=0x%02x\n",
606 				__func__, rx[6], rx[7]);
607 
608 		rc_keydown(d->rc_dev, RC_PROTO_RC5, state->last_rc_key, 0);
609 	} else if (state->last_rc_key) {
610 		rc_keyup(d->rc_dev);
611 		state->last_rc_key = 0;
612 	}
613 ret:
614 	kfree(b);
615 	return ret;
616 }
617 
618 static int pctv452e_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
619 {
620 	const u8 mem_addr[] = { 0x1f, 0xcc };
621 	u8 encoded_mac[20];
622 	int ret;
623 
624 	ret = -EAGAIN;
625 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
626 		goto failed;
627 
628 	ret = pctv452e_i2c_msg(d, I2C_ADDR_24C16,
629 				mem_addr + 1, /* snd_len */ 1,
630 				encoded_mac, /* rcv_len */ 20);
631 	if (-EREMOTEIO == ret)
632 		/* Caution! A 24C16 interprets 0xA2 0x1F 0xCC as a
633 		   byte write if /WC is low. */
634 		ret = pctv452e_i2c_msg(d, I2C_ADDR_24C64,
635 					mem_addr, 2,
636 					encoded_mac, 20);
637 
638 	mutex_unlock(&d->i2c_mutex);
639 
640 	if (20 != ret)
641 		goto failed;
642 
643 	ret = ttpci_eeprom_decode_mac(mac, encoded_mac);
644 	if (0 != ret)
645 		goto failed;
646 
647 	return 0;
648 
649 failed:
650 	eth_zero_addr(mac);
651 
652 	return ret;
653 }
654 
655 static const struct stb0899_s1_reg pctv452e_init_dev[] = {
656 	{ STB0899_DISCNTRL1,	0x26 },
657 	{ STB0899_DISCNTRL2,	0x80 },
658 	{ STB0899_DISRX_ST0,	0x04 },
659 	{ STB0899_DISRX_ST1,	0x20 },
660 	{ STB0899_DISPARITY,	0x00 },
661 	{ STB0899_DISFIFO,	0x00 },
662 	{ STB0899_DISF22,	0x99 },
663 	{ STB0899_DISF22RX,	0x85 }, /* 0xa8 */
664 	{ STB0899_ACRPRESC,	0x11 },
665 	{ STB0899_ACRDIV1,	0x0a },
666 	{ STB0899_ACRDIV2,	0x05 },
667 	{ STB0899_DACR1	,	0x00 },
668 	{ STB0899_DACR2	,	0x00 },
669 	{ STB0899_OUTCFG,	0x00 },
670 	{ STB0899_MODECFG,	0x00 }, /* Inversion */
671 	{ STB0899_IRQMSK_3,	0xf3 },
672 	{ STB0899_IRQMSK_2,	0xfc },
673 	{ STB0899_IRQMSK_1,	0xff },
674 	{ STB0899_IRQMSK_0,	0xff },
675 	{ STB0899_I2CCFG,	0x88 },
676 	{ STB0899_I2CRPT,	0x58 },
677 	{ STB0899_GPIO00CFG,	0x82 },
678 	{ STB0899_GPIO01CFG,	0x82 }, /* LED: 0x02 green, 0x82 orange */
679 	{ STB0899_GPIO02CFG,	0x82 },
680 	{ STB0899_GPIO03CFG,	0x82 },
681 	{ STB0899_GPIO04CFG,	0x82 },
682 	{ STB0899_GPIO05CFG,	0x82 },
683 	{ STB0899_GPIO06CFG,	0x82 },
684 	{ STB0899_GPIO07CFG,	0x82 },
685 	{ STB0899_GPIO08CFG,	0x82 },
686 	{ STB0899_GPIO09CFG,	0x82 },
687 	{ STB0899_GPIO10CFG,	0x82 },
688 	{ STB0899_GPIO11CFG,	0x82 },
689 	{ STB0899_GPIO12CFG,	0x82 },
690 	{ STB0899_GPIO13CFG,	0x82 },
691 	{ STB0899_GPIO14CFG,	0x82 },
692 	{ STB0899_GPIO15CFG,	0x82 },
693 	{ STB0899_GPIO16CFG,	0x82 },
694 	{ STB0899_GPIO17CFG,	0x82 },
695 	{ STB0899_GPIO18CFG,	0x82 },
696 	{ STB0899_GPIO19CFG,	0x82 },
697 	{ STB0899_GPIO20CFG,	0x82 },
698 	{ STB0899_SDATCFG,	0xb8 },
699 	{ STB0899_SCLTCFG,	0xba },
700 	{ STB0899_AGCRFCFG,	0x1c }, /* 0x11 DVB-S; 0x1c DVB-S2 (1c, rjkm) */
701 	{ STB0899_GPIO22,	0x82 },
702 	{ STB0899_GPIO21,	0x91 },
703 	{ STB0899_DIRCLKCFG,	0x82 },
704 	{ STB0899_CLKOUT27CFG,	0x7e },
705 	{ STB0899_STDBYCFG,	0x82 },
706 	{ STB0899_CS0CFG,	0x82 },
707 	{ STB0899_CS1CFG,	0x82 },
708 	{ STB0899_DISEQCOCFG,	0x20 },
709 	{ STB0899_NCOARSE,	0x15 }, /* 0x15 27Mhz, F/3 198MHz, F/6 108MHz */
710 	{ STB0899_SYNTCTRL,	0x00 }, /* 0x00 CLKI, 0x02 XTALI */
711 	{ STB0899_FILTCTRL,	0x00 },
712 	{ STB0899_SYSCTRL,	0x00 },
713 	{ STB0899_STOPCLK1,	0x20 }, /* orig: 0x00 budget-ci: 0x20 */
714 	{ STB0899_STOPCLK2,	0x00 },
715 	{ STB0899_INTBUFCTRL,	0x0a },
716 	{ STB0899_AGC2I1,	0x00 },
717 	{ STB0899_AGC2I2,	0x00 },
718 	{ STB0899_AGCIQIN,	0x00 },
719 	{ STB0899_TSTRES,	0x40 }, /* rjkm */
720 	{ 0xffff,		0xff },
721 };
722 
723 static const struct stb0899_s1_reg pctv452e_init_s1_demod[] = {
724 	{ STB0899_DEMOD,	0x00 },
725 	{ STB0899_RCOMPC,	0xc9 },
726 	{ STB0899_AGC1CN,	0x01 },
727 	{ STB0899_AGC1REF,	0x10 },
728 	{ STB0899_RTC,		0x23 },
729 	{ STB0899_TMGCFG,	0x4e },
730 	{ STB0899_AGC2REF,	0x34 },
731 	{ STB0899_TLSR,		0x84 },
732 	{ STB0899_CFD,		0xf7 },
733 	{ STB0899_ACLC,		0x87 },
734 	{ STB0899_BCLC,		0x94 },
735 	{ STB0899_EQON,		0x41 },
736 	{ STB0899_LDT,		0xf1 },
737 	{ STB0899_LDT2,		0xe3 },
738 	{ STB0899_EQUALREF,	0xb4 },
739 	{ STB0899_TMGRAMP,	0x10 },
740 	{ STB0899_TMGTHD,	0x30 },
741 	{ STB0899_IDCCOMP,	0xfd },
742 	{ STB0899_QDCCOMP,	0xff },
743 	{ STB0899_POWERI,	0x0c },
744 	{ STB0899_POWERQ,	0x0f },
745 	{ STB0899_RCOMP,	0x6c },
746 	{ STB0899_AGCIQIN,	0x80 },
747 	{ STB0899_AGC2I1,	0x06 },
748 	{ STB0899_AGC2I2,	0x00 },
749 	{ STB0899_TLIR,		0x30 },
750 	{ STB0899_RTF,		0x7f },
751 	{ STB0899_DSTATUS,	0x00 },
752 	{ STB0899_LDI,		0xbc },
753 	{ STB0899_CFRM,		0xea },
754 	{ STB0899_CFRL,		0x31 },
755 	{ STB0899_NIRM,		0x2b },
756 	{ STB0899_NIRL,		0x80 },
757 	{ STB0899_ISYMB,	0x1d },
758 	{ STB0899_QSYMB,	0xa6 },
759 	{ STB0899_SFRH,		0x2f },
760 	{ STB0899_SFRM,		0x68 },
761 	{ STB0899_SFRL,		0x40 },
762 	{ STB0899_SFRUPH,	0x2f },
763 	{ STB0899_SFRUPM,	0x68 },
764 	{ STB0899_SFRUPL,	0x40 },
765 	{ STB0899_EQUAI1,	0x02 },
766 	{ STB0899_EQUAQ1,	0xff },
767 	{ STB0899_EQUAI2,	0x04 },
768 	{ STB0899_EQUAQ2,	0x05 },
769 	{ STB0899_EQUAI3,	0x02 },
770 	{ STB0899_EQUAQ3,	0xfd },
771 	{ STB0899_EQUAI4,	0x03 },
772 	{ STB0899_EQUAQ4,	0x07 },
773 	{ STB0899_EQUAI5,	0x08 },
774 	{ STB0899_EQUAQ5,	0xf5 },
775 	{ STB0899_DSTATUS2,	0x00 },
776 	{ STB0899_VSTATUS,	0x00 },
777 	{ STB0899_VERROR,	0x86 },
778 	{ STB0899_IQSWAP,	0x2a },
779 	{ STB0899_ECNT1M,	0x00 },
780 	{ STB0899_ECNT1L,	0x00 },
781 	{ STB0899_ECNT2M,	0x00 },
782 	{ STB0899_ECNT2L,	0x00 },
783 	{ STB0899_ECNT3M,	0x0a },
784 	{ STB0899_ECNT3L,	0xad },
785 	{ STB0899_FECAUTO1,	0x06 },
786 	{ STB0899_FECM,		0x01 },
787 	{ STB0899_VTH12,	0xb0 },
788 	{ STB0899_VTH23,	0x7a },
789 	{ STB0899_VTH34,	0x58 },
790 	{ STB0899_VTH56,	0x38 },
791 	{ STB0899_VTH67,	0x34 },
792 	{ STB0899_VTH78,	0x24 },
793 	{ STB0899_PRVIT,	0xff },
794 	{ STB0899_VITSYNC,	0x19 },
795 	{ STB0899_RSULC,	0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
796 	{ STB0899_TSULC,	0x42 },
797 	{ STB0899_RSLLC,	0x41 },
798 	{ STB0899_TSLPL,	0x12 },
799 	{ STB0899_TSCFGH,	0x0c },
800 	{ STB0899_TSCFGM,	0x00 },
801 	{ STB0899_TSCFGL,	0x00 },
802 	{ STB0899_TSOUT,	0x69 }, /* 0x0d for CAM */
803 	{ STB0899_RSSYNCDEL,	0x00 },
804 	{ STB0899_TSINHDELH,	0x02 },
805 	{ STB0899_TSINHDELM,	0x00 },
806 	{ STB0899_TSINHDELL,	0x00 },
807 	{ STB0899_TSLLSTKM,	0x1b },
808 	{ STB0899_TSLLSTKL,	0xb3 },
809 	{ STB0899_TSULSTKM,	0x00 },
810 	{ STB0899_TSULSTKL,	0x00 },
811 	{ STB0899_PCKLENUL,	0xbc },
812 	{ STB0899_PCKLENLL,	0xcc },
813 	{ STB0899_RSPCKLEN,	0xbd },
814 	{ STB0899_TSSTATUS,	0x90 },
815 	{ STB0899_ERRCTRL1,	0xb6 },
816 	{ STB0899_ERRCTRL2,	0x95 },
817 	{ STB0899_ERRCTRL3,	0x8d },
818 	{ STB0899_DMONMSK1,	0x27 },
819 	{ STB0899_DMONMSK0,	0x03 },
820 	{ STB0899_DEMAPVIT,	0x5c },
821 	{ STB0899_PLPARM,	0x19 },
822 	{ STB0899_PDELCTRL,	0x48 },
823 	{ STB0899_PDELCTRL2,	0x00 },
824 	{ STB0899_BBHCTRL1,	0x00 },
825 	{ STB0899_BBHCTRL2,	0x00 },
826 	{ STB0899_HYSTTHRESH,	0x77 },
827 	{ STB0899_MATCSTM,	0x00 },
828 	{ STB0899_MATCSTL,	0x00 },
829 	{ STB0899_UPLCSTM,	0x00 },
830 	{ STB0899_UPLCSTL,	0x00 },
831 	{ STB0899_DFLCSTM,	0x00 },
832 	{ STB0899_DFLCSTL,	0x00 },
833 	{ STB0899_SYNCCST,	0x00 },
834 	{ STB0899_SYNCDCSTM,	0x00 },
835 	{ STB0899_SYNCDCSTL,	0x00 },
836 	{ STB0899_ISI_ENTRY,	0x00 },
837 	{ STB0899_ISI_BIT_EN,	0x00 },
838 	{ STB0899_MATSTRM,	0xf0 },
839 	{ STB0899_MATSTRL,	0x02 },
840 	{ STB0899_UPLSTRM,	0x45 },
841 	{ STB0899_UPLSTRL,	0x60 },
842 	{ STB0899_DFLSTRM,	0xe3 },
843 	{ STB0899_DFLSTRL,	0x00 },
844 	{ STB0899_SYNCSTR,	0x47 },
845 	{ STB0899_SYNCDSTRM,	0x05 },
846 	{ STB0899_SYNCDSTRL,	0x18 },
847 	{ STB0899_CFGPDELSTATUS1, 0x19 },
848 	{ STB0899_CFGPDELSTATUS2, 0x2b },
849 	{ STB0899_BBFERRORM,	0x00 },
850 	{ STB0899_BBFERRORL,	0x01 },
851 	{ STB0899_UPKTERRORM,	0x00 },
852 	{ STB0899_UPKTERRORL,	0x00 },
853 	{ 0xffff,		0xff },
854 };
855 
856 static struct stb0899_config stb0899_config = {
857 	.init_dev	= pctv452e_init_dev,
858 	.init_s2_demod	= stb0899_s2_init_2,
859 	.init_s1_demod	= pctv452e_init_s1_demod,
860 	.init_s2_fec	= stb0899_s2_init_4,
861 	.init_tst	= stb0899_s1_init_5,
862 
863 	.demod_address   = I2C_ADDR_STB0899, /* I2C Address */
864 	.block_sync_mode = STB0899_SYNC_FORCED, /* ? */
865 
866 	.xtal_freq       = 27000000,	 /* Assume Hz ? */
867 	.inversion       = IQ_SWAP_ON,
868 
869 	.lo_clk	  = 76500000,
870 	.hi_clk	  = 99000000,
871 
872 	.ts_output_mode  = 0,	/* Use parallel mode */
873 	.clock_polarity  = 0,
874 	.data_clk_parity = 0,
875 	.fec_mode	= 0,
876 
877 	.esno_ave	    = STB0899_DVBS2_ESNO_AVE,
878 	.esno_quant	  = STB0899_DVBS2_ESNO_QUANT,
879 	.avframes_coarse     = STB0899_DVBS2_AVFRAMES_COARSE,
880 	.avframes_fine       = STB0899_DVBS2_AVFRAMES_FINE,
881 	.miss_threshold      = STB0899_DVBS2_MISS_THRESHOLD,
882 	.uwp_threshold_acq   = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
883 	.uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
884 	.uwp_threshold_sof   = STB0899_DVBS2_UWP_THRESHOLD_SOF,
885 	.sof_search_timeout  = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
886 
887 	.btr_nco_bits	  = STB0899_DVBS2_BTR_NCO_BITS,
888 	.btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
889 	.crl_nco_bits	  = STB0899_DVBS2_CRL_NCO_BITS,
890 	.ldpc_max_iter	 = STB0899_DVBS2_LDPC_MAX_ITER,
891 
892 	.tuner_get_frequency	= stb6100_get_frequency,
893 	.tuner_set_frequency	= stb6100_set_frequency,
894 	.tuner_set_bandwidth	= stb6100_set_bandwidth,
895 	.tuner_get_bandwidth	= stb6100_get_bandwidth,
896 	.tuner_set_rfsiggain	= NULL,
897 
898 	/* helper for switching LED green/orange */
899 	.postproc = pctv45e_postproc
900 };
901 
902 static struct stb6100_config stb6100_config = {
903 	.tuner_address = I2C_ADDR_STB6100,
904 	.refclock      = 27000000
905 };
906 
907 
908 static const struct i2c_algorithm pctv452e_i2c_algo = {
909 	.master_xfer   = pctv452e_i2c_xfer,
910 	.functionality = pctv452e_i2c_func
911 };
912 
913 static int pctv452e_frontend_attach(struct dvb_usb_adapter *a)
914 {
915 	const struct usb_device_id *id;
916 
917 	a->fe_adap[0].fe = dvb_attach(stb0899_attach, &stb0899_config,
918 						&a->dev->i2c_adap);
919 	if (!a->fe_adap[0].fe)
920 		return -ENODEV;
921 
922 	id = a->dev->desc->warm_ids[0];
923 	if (id->idVendor == USB_VID_TECHNOTREND &&
924 	    id->idProduct == USB_PID_TECHNOTREND_CONNECT_S2_3650_CI) {
925 		if (dvb_attach(lnbp22_attach,
926 			       a->fe_adap[0].fe,
927 			       &a->dev->i2c_adap) == NULL) {
928 			err("Cannot attach lnbp22\n");
929 		}
930 		/* Error ignored. */
931 		tt3650_ci_init(a);
932 	} else if (dvb_attach(isl6423_attach,
933 			      a->fe_adap[0].fe,
934 			      &a->dev->i2c_adap,
935 			      &pctv452e_isl6423_config) == NULL) {
936 		err("Cannot attach isl6423\n");
937 	}
938 
939 	return 0;
940 }
941 
942 static int pctv452e_tuner_attach(struct dvb_usb_adapter *a)
943 {
944 	if (!a->fe_adap[0].fe)
945 		return -ENODEV;
946 	if (dvb_attach(stb6100_attach, a->fe_adap[0].fe, &stb6100_config,
947 					&a->dev->i2c_adap) == NULL) {
948 		err("%s failed\n", __func__);
949 		return -ENODEV;
950 	}
951 
952 	return 0;
953 }
954 
955 enum {
956 	PINNACLE_PCTV_452E,
957 	TECHNOTREND_CONNECT_S2_3600,
958 	TECHNOTREND_CONNECT_S2_3650_CI,
959 };
960 
961 static const struct usb_device_id pctv452e_usb_table[] = {
962 	DVB_USB_DEV(PINNACLE, PINNACLE_PCTV_452E),
963 	DVB_USB_DEV(TECHNOTREND, TECHNOTREND_CONNECT_S2_3600),
964 	DVB_USB_DEV(TECHNOTREND, TECHNOTREND_CONNECT_S2_3650_CI),
965 	{ }
966 };
967 
968 MODULE_DEVICE_TABLE(usb, pctv452e_usb_table);
969 
970 static struct dvb_usb_device_properties pctv452e_properties = {
971 	.caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
972 	.usb_ctrl = DEVICE_SPECIFIC,
973 
974 	.size_of_priv     = sizeof(struct pctv452e_state),
975 
976 	.power_ctrl       = pctv452e_power_ctrl,
977 
978 	.rc.core = {
979 		.rc_codes	= RC_MAP_DIB0700_RC5_TABLE,
980 		.allowed_protos	= RC_PROTO_BIT_RC5,
981 		.rc_query	= pctv452e_rc_query,
982 		.rc_interval	= 100,
983 	},
984 
985 	.num_adapters     = 1,
986 	.adapter = {{
987 		.num_frontends = 1,
988 		.fe = {{
989 			.frontend_attach  = pctv452e_frontend_attach,
990 			.tuner_attach     = pctv452e_tuner_attach,
991 
992 			/* parameter for the MPEG2-data transfer */
993 			.stream = {
994 				.type     = USB_ISOC,
995 				.count    = 4,
996 				.endpoint = 0x02,
997 				.u = {
998 					.isoc = {
999 						.framesperurb = 4,
1000 						.framesize    = 940,
1001 						.interval     = 1
1002 					}
1003 				}
1004 			},
1005 		} },
1006 	} },
1007 
1008 	.i2c_algo = &pctv452e_i2c_algo,
1009 
1010 	.generic_bulk_ctrl_endpoint = 1, /* allow generice rw function */
1011 
1012 	.num_device_descs = 1,
1013 	.devices = {
1014 		{ .name = "PCTV HDTV USB",
1015 		  .cold_ids = { NULL, NULL }, /* this is a warm only device */
1016 		  .warm_ids = { &pctv452e_usb_table[PINNACLE_PCTV_452E], NULL }
1017 		},
1018 		{ NULL },
1019 	}
1020 };
1021 
1022 static struct dvb_usb_device_properties tt_connect_s2_3600_properties = {
1023 	.caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
1024 	.usb_ctrl = DEVICE_SPECIFIC,
1025 
1026 	.size_of_priv		= sizeof(struct pctv452e_state),
1027 
1028 	.power_ctrl		= pctv452e_power_ctrl,
1029 	.read_mac_address	= pctv452e_read_mac_address,
1030 
1031 	.rc.core = {
1032 		.rc_codes	= RC_MAP_TT_1500,
1033 		.allowed_protos	= RC_PROTO_BIT_RC5,
1034 		.rc_query	= pctv452e_rc_query,
1035 		.rc_interval	= 100,
1036 	},
1037 
1038 	.num_adapters		= 1,
1039 	.adapter = {{
1040 		.num_frontends = 1,
1041 		.fe = {{
1042 			.frontend_attach = pctv452e_frontend_attach,
1043 			.tuner_attach = pctv452e_tuner_attach,
1044 
1045 			/* parameter for the MPEG2-data transfer */
1046 			.stream = {
1047 				.type = USB_ISOC,
1048 				.count = 4,
1049 				.endpoint = 0x02,
1050 				.u = {
1051 					.isoc = {
1052 						.framesperurb = 64,
1053 						.framesize = 940,
1054 						.interval = 1
1055 					}
1056 				}
1057 			},
1058 
1059 		} },
1060 	} },
1061 
1062 	.i2c_algo = &pctv452e_i2c_algo,
1063 
1064 	.generic_bulk_ctrl_endpoint = 1, /* allow generic rw function*/
1065 
1066 	.num_device_descs = 2,
1067 	.devices = {
1068 		{ .name = "Technotrend TT Connect S2-3600",
1069 		  .cold_ids = { NULL, NULL }, /* this is a warm only device */
1070 		  .warm_ids = { &pctv452e_usb_table[TECHNOTREND_CONNECT_S2_3600], NULL }
1071 		},
1072 		{ .name = "Technotrend TT Connect S2-3650-CI",
1073 		  .cold_ids = { NULL, NULL },
1074 		  .warm_ids = { &pctv452e_usb_table[TECHNOTREND_CONNECT_S2_3650_CI], NULL }
1075 		},
1076 		{ NULL },
1077 	}
1078 };
1079 
1080 static void pctv452e_usb_disconnect(struct usb_interface *intf)
1081 {
1082 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1083 
1084 	tt3650_ci_uninit(d);
1085 	dvb_usb_device_exit(intf);
1086 }
1087 
1088 static int pctv452e_usb_probe(struct usb_interface *intf,
1089 				const struct usb_device_id *id)
1090 {
1091 	if (0 == dvb_usb_device_init(intf, &pctv452e_properties,
1092 					THIS_MODULE, NULL, adapter_nr) ||
1093 	    0 == dvb_usb_device_init(intf, &tt_connect_s2_3600_properties,
1094 					THIS_MODULE, NULL, adapter_nr))
1095 		return 0;
1096 
1097 	return -ENODEV;
1098 }
1099 
1100 static struct usb_driver pctv452e_usb_driver = {
1101 	.name       = "pctv452e",
1102 	.probe      = pctv452e_usb_probe,
1103 	.disconnect = pctv452e_usb_disconnect,
1104 	.id_table   = pctv452e_usb_table,
1105 };
1106 
1107 module_usb_driver(pctv452e_usb_driver);
1108 
1109 MODULE_AUTHOR("Dominik Kuhlen <dkuhlen@gmx.net>");
1110 MODULE_AUTHOR("Andre Weidemann <Andre.Weidemann@web.de>");
1111 MODULE_AUTHOR("Michael H. Schimek <mschimek@gmx.at>");
1112 MODULE_DESCRIPTION("Pinnacle PCTV HDTV USB DVB / TT connect S2-3600 Driver");
1113 MODULE_LICENSE("GPL");
1114