cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

pctv452e.c (25857B)


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