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