xref: /linux/drivers/media/usb/dvb-usb/pctv452e.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2786baecfSMauro Carvalho Chehab /*
3786baecfSMauro Carvalho Chehab  * PCTV 452e DVB driver
4786baecfSMauro Carvalho Chehab  *
5786baecfSMauro Carvalho Chehab  * Copyright (c) 2006-2008 Dominik Kuhlen <dkuhlen@gmx.net>
6786baecfSMauro Carvalho Chehab  *
7786baecfSMauro Carvalho Chehab  * TT connect S2-3650-CI Common Interface support, MAC readout
8786baecfSMauro Carvalho Chehab  * Copyright (C) 2008 Michael H. Schimek <mschimek@gmx.at>
9786baecfSMauro Carvalho Chehab  */
10786baecfSMauro Carvalho Chehab 
11786baecfSMauro Carvalho Chehab /* dvb usb framework */
12786baecfSMauro Carvalho Chehab #define DVB_USB_LOG_PREFIX "pctv452e"
13786baecfSMauro Carvalho Chehab #include "dvb-usb.h"
14786baecfSMauro Carvalho Chehab 
15786baecfSMauro Carvalho Chehab /* Demodulator */
16786baecfSMauro Carvalho Chehab #include "stb0899_drv.h"
17786baecfSMauro Carvalho Chehab #include "stb0899_reg.h"
18786baecfSMauro Carvalho Chehab #include "stb0899_cfg.h"
19786baecfSMauro Carvalho Chehab /* Tuner */
20786baecfSMauro Carvalho Chehab #include "stb6100.h"
21786baecfSMauro Carvalho Chehab #include "stb6100_cfg.h"
22786baecfSMauro Carvalho Chehab /* FE Power */
23786baecfSMauro Carvalho Chehab #include "lnbp22.h"
24786baecfSMauro Carvalho Chehab 
25fada1935SMauro Carvalho Chehab #include <media/dvb_ca_en50221.h>
26786baecfSMauro Carvalho Chehab #include "ttpci-eeprom.h"
27786baecfSMauro Carvalho Chehab 
28786baecfSMauro Carvalho Chehab static int debug;
29786baecfSMauro Carvalho Chehab module_param(debug, int, 0644);
30786baecfSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
31786baecfSMauro Carvalho Chehab 
32786baecfSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33786baecfSMauro Carvalho Chehab 
34786baecfSMauro Carvalho Chehab #define ISOC_INTERFACE_ALTERNATIVE 3
35786baecfSMauro Carvalho Chehab 
36786baecfSMauro Carvalho Chehab #define SYNC_BYTE_OUT 0xaa
37786baecfSMauro Carvalho Chehab #define SYNC_BYTE_IN  0x55
38786baecfSMauro Carvalho Chehab 
39786baecfSMauro Carvalho Chehab /* guessed: (copied from ttusb-budget) */
40786baecfSMauro Carvalho Chehab #define PCTV_CMD_RESET 0x15
41786baecfSMauro Carvalho Chehab /* command to poll IR receiver */
42786baecfSMauro Carvalho Chehab #define PCTV_CMD_IR    0x1b
43786baecfSMauro Carvalho Chehab /* command to send I2C  */
44786baecfSMauro Carvalho Chehab #define PCTV_CMD_I2C   0x31
45786baecfSMauro Carvalho Chehab 
46786baecfSMauro Carvalho Chehab #define I2C_ADDR_STB0899 (0xd0 >> 1)
47786baecfSMauro Carvalho Chehab #define I2C_ADDR_STB6100 (0xc0 >> 1)
48786baecfSMauro Carvalho Chehab #define I2C_ADDR_LNBP22  (0x10 >> 1)
49786baecfSMauro Carvalho Chehab #define I2C_ADDR_24C16   (0xa0 >> 1)
50786baecfSMauro Carvalho Chehab #define I2C_ADDR_24C64   (0xa2 >> 1)
51786baecfSMauro Carvalho Chehab 
52786baecfSMauro Carvalho Chehab 
53786baecfSMauro Carvalho Chehab /* pctv452e sends us this amount of data for each issued usb-command */
54786baecfSMauro Carvalho Chehab #define PCTV_ANSWER_LEN 64
55786baecfSMauro Carvalho Chehab /* Wait up to 1000ms for device  */
56786baecfSMauro Carvalho Chehab #define PCTV_TIMEOUT 1000
57786baecfSMauro Carvalho Chehab 
58786baecfSMauro Carvalho Chehab 
59786baecfSMauro Carvalho Chehab #define PCTV_LED_GPIO   STB0899_GPIO01
60786baecfSMauro Carvalho Chehab #define PCTV_LED_GREEN  0x82
61786baecfSMauro Carvalho Chehab #define PCTV_LED_ORANGE 0x02
62786baecfSMauro Carvalho Chehab 
63786baecfSMauro Carvalho Chehab #define ci_dbg(format, arg...)				\
64786baecfSMauro Carvalho Chehab do {							\
65786baecfSMauro Carvalho Chehab 	if (0)						\
66786baecfSMauro Carvalho Chehab 		printk(KERN_DEBUG DVB_USB_LOG_PREFIX	\
67786baecfSMauro Carvalho Chehab 			": " format "\n" , ## arg);	\
68786baecfSMauro Carvalho Chehab } while (0)
69786baecfSMauro Carvalho Chehab 
70786baecfSMauro Carvalho Chehab enum {
71786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_TEST = 0x40,
72786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_RD_CTRL,
73786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_WR_CTRL,
74786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_RD_ATTR,
75786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_WR_ATTR,
76786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_RESET,
77786baecfSMauro Carvalho Chehab 	TT3650_CMD_CI_SET_VIDEO_PORT
78786baecfSMauro Carvalho Chehab };
79786baecfSMauro Carvalho Chehab 
80786baecfSMauro Carvalho Chehab 
81786baecfSMauro Carvalho Chehab static struct stb0899_postproc pctv45e_postproc[] = {
82786baecfSMauro Carvalho Chehab 	{ PCTV_LED_GPIO, STB0899_GPIOPULLUP },
83786baecfSMauro Carvalho Chehab 	{ 0, 0 }
84786baecfSMauro Carvalho Chehab };
85786baecfSMauro Carvalho Chehab 
86786baecfSMauro Carvalho Chehab /*
87786baecfSMauro Carvalho Chehab  * stores all private variables for communication with the PCTV452e DVB-S2
88786baecfSMauro Carvalho Chehab  */
89786baecfSMauro Carvalho Chehab struct pctv452e_state {
90786baecfSMauro Carvalho Chehab 	struct dvb_ca_en50221 ca;
91786baecfSMauro Carvalho Chehab 	struct mutex ca_mutex;
92786baecfSMauro Carvalho Chehab 
93786baecfSMauro Carvalho Chehab 	u8 c;	   /* transaction counter, wraps around...  */
94786baecfSMauro Carvalho Chehab 	u8 initialized; /* set to 1 if 0x15 has been sent */
95786baecfSMauro Carvalho Chehab 	u16 last_rc_key;
96786baecfSMauro Carvalho Chehab };
97786baecfSMauro Carvalho Chehab 
98786baecfSMauro Carvalho Chehab static int tt3650_ci_msg(struct dvb_usb_device *d, u8 cmd, u8 *data,
99786baecfSMauro Carvalho Chehab 			 unsigned int write_len, unsigned int read_len)
100786baecfSMauro Carvalho Chehab {
101786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
10248775cb7SMax Kellermann 	u8 *buf;
103786baecfSMauro Carvalho Chehab 	u8 id;
104786baecfSMauro Carvalho Chehab 	unsigned int rlen;
105786baecfSMauro Carvalho Chehab 	int ret;
106786baecfSMauro Carvalho Chehab 
1075dfd2c8fSMauro Carvalho Chehab 	if (!data || (write_len > 64 - 4) || (read_len > 64 - 4)) {
1085dfd2c8fSMauro Carvalho Chehab 		err("%s: transfer data invalid", __func__);
1095dfd2c8fSMauro Carvalho Chehab 		return -EIO;
11059dde8e7Skbuild test robot 	}
111786baecfSMauro Carvalho Chehab 
11248775cb7SMax Kellermann 	buf = kmalloc(64, GFP_KERNEL);
11348775cb7SMax Kellermann 	if (!buf)
11448775cb7SMax Kellermann 		return -ENOMEM;
11548775cb7SMax Kellermann 
116786baecfSMauro Carvalho Chehab 	id = state->c++;
117786baecfSMauro Carvalho Chehab 
11848775cb7SMax Kellermann 	buf[0] = SYNC_BYTE_OUT;
11948775cb7SMax Kellermann 	buf[1] = id;
12048775cb7SMax Kellermann 	buf[2] = cmd;
12148775cb7SMax Kellermann 	buf[3] = write_len;
122786baecfSMauro Carvalho Chehab 
12348775cb7SMax Kellermann 	memcpy(buf + 4, data, write_len);
124786baecfSMauro Carvalho Chehab 
125786baecfSMauro Carvalho Chehab 	rlen = (read_len > 0) ? 64 : 0;
12648775cb7SMax Kellermann 	ret = dvb_usb_generic_rw(d, buf, 4 + write_len,
12748775cb7SMax Kellermann 				  buf, rlen, /* delay_ms */ 0);
128786baecfSMauro Carvalho Chehab 	if (0 != ret)
129786baecfSMauro Carvalho Chehab 		goto failed;
130786baecfSMauro Carvalho Chehab 
131786baecfSMauro Carvalho Chehab 	ret = -EIO;
13248775cb7SMax Kellermann 	if (SYNC_BYTE_IN != buf[0] || id != buf[1])
133786baecfSMauro Carvalho Chehab 		goto failed;
134786baecfSMauro Carvalho Chehab 
13548775cb7SMax Kellermann 	memcpy(data, buf + 4, read_len);
136786baecfSMauro Carvalho Chehab 
13748775cb7SMax Kellermann 	kfree(buf);
138786baecfSMauro Carvalho Chehab 	return 0;
139786baecfSMauro Carvalho Chehab 
140786baecfSMauro Carvalho Chehab failed:
141786baecfSMauro Carvalho Chehab 	err("CI error %d; %02X %02X %02X -> %*ph.",
14248775cb7SMax Kellermann 	     ret, SYNC_BYTE_OUT, id, cmd, 3, buf);
143786baecfSMauro Carvalho Chehab 
14448775cb7SMax Kellermann 	kfree(buf);
145786baecfSMauro Carvalho Chehab 	return ret;
146786baecfSMauro Carvalho Chehab }
147786baecfSMauro Carvalho Chehab 
148786baecfSMauro Carvalho Chehab static int tt3650_ci_msg_locked(struct dvb_ca_en50221 *ca,
149786baecfSMauro Carvalho Chehab 				u8 cmd, u8 *data, unsigned int write_len,
150786baecfSMauro Carvalho Chehab 				unsigned int read_len)
151786baecfSMauro Carvalho Chehab {
152786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
153786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
154786baecfSMauro Carvalho Chehab 	int ret;
155786baecfSMauro Carvalho Chehab 
156786baecfSMauro Carvalho Chehab 	mutex_lock(&state->ca_mutex);
157786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg(d, cmd, data, write_len, read_len);
158786baecfSMauro Carvalho Chehab 	mutex_unlock(&state->ca_mutex);
159786baecfSMauro Carvalho Chehab 
160786baecfSMauro Carvalho Chehab 	return ret;
161786baecfSMauro Carvalho Chehab }
162786baecfSMauro Carvalho Chehab 
163786baecfSMauro Carvalho Chehab static int tt3650_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
164786baecfSMauro Carvalho Chehab 				 int slot, int address)
165786baecfSMauro Carvalho Chehab {
166786baecfSMauro Carvalho Chehab 	u8 buf[3];
167786baecfSMauro Carvalho Chehab 	int ret;
168786baecfSMauro Carvalho Chehab 
169786baecfSMauro Carvalho Chehab 	if (0 != slot)
170786baecfSMauro Carvalho Chehab 		return -EINVAL;
171786baecfSMauro Carvalho Chehab 
172786baecfSMauro Carvalho Chehab 	buf[0] = (address >> 8) & 0x0F;
173786baecfSMauro Carvalho Chehab 	buf[1] = address;
174786baecfSMauro Carvalho Chehab 
175786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_ATTR, buf, 2, 3);
176786baecfSMauro Carvalho Chehab 
177786baecfSMauro Carvalho Chehab 	ci_dbg("%s %04x -> %d 0x%02x",
178786baecfSMauro Carvalho Chehab 		__func__, address, ret, buf[2]);
179786baecfSMauro Carvalho Chehab 
180786baecfSMauro Carvalho Chehab 	if (ret < 0)
181786baecfSMauro Carvalho Chehab 		return ret;
182786baecfSMauro Carvalho Chehab 
183786baecfSMauro Carvalho Chehab 	return buf[2];
184786baecfSMauro Carvalho Chehab }
185786baecfSMauro Carvalho Chehab 
186786baecfSMauro Carvalho Chehab static int tt3650_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
187786baecfSMauro Carvalho Chehab 				 int slot, int address, u8 value)
188786baecfSMauro Carvalho Chehab {
189786baecfSMauro Carvalho Chehab 	u8 buf[3];
190786baecfSMauro Carvalho Chehab 
191786baecfSMauro Carvalho Chehab 	ci_dbg("%s %d 0x%04x 0x%02x",
192786baecfSMauro Carvalho Chehab 		__func__, slot, address, value);
193786baecfSMauro Carvalho Chehab 
194786baecfSMauro Carvalho Chehab 	if (0 != slot)
195786baecfSMauro Carvalho Chehab 		return -EINVAL;
196786baecfSMauro Carvalho Chehab 
197786baecfSMauro Carvalho Chehab 	buf[0] = (address >> 8) & 0x0F;
198786baecfSMauro Carvalho Chehab 	buf[1] = address;
199786baecfSMauro Carvalho Chehab 	buf[2] = value;
200786baecfSMauro Carvalho Chehab 
201786baecfSMauro Carvalho Chehab 	return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_ATTR, buf, 3, 3);
202786baecfSMauro Carvalho Chehab }
203786baecfSMauro Carvalho Chehab 
204786baecfSMauro Carvalho Chehab static int tt3650_ci_read_cam_control(struct dvb_ca_en50221 *ca,
205786baecfSMauro Carvalho Chehab 				 int			slot,
206786baecfSMauro Carvalho Chehab 				 u8			address)
207786baecfSMauro Carvalho Chehab {
208786baecfSMauro Carvalho Chehab 	u8 buf[2];
209786baecfSMauro Carvalho Chehab 	int ret;
210786baecfSMauro Carvalho Chehab 
211786baecfSMauro Carvalho Chehab 	if (0 != slot)
212786baecfSMauro Carvalho Chehab 		return -EINVAL;
213786baecfSMauro Carvalho Chehab 
214786baecfSMauro Carvalho Chehab 	buf[0] = address & 3;
215786baecfSMauro Carvalho Chehab 
216786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_RD_CTRL, buf, 1, 2);
217786baecfSMauro Carvalho Chehab 
218786baecfSMauro Carvalho Chehab 	ci_dbg("%s 0x%02x -> %d 0x%02x",
219786baecfSMauro Carvalho Chehab 		__func__, address, ret, buf[1]);
220786baecfSMauro Carvalho Chehab 
221786baecfSMauro Carvalho Chehab 	if (ret < 0)
222786baecfSMauro Carvalho Chehab 		return ret;
223786baecfSMauro Carvalho Chehab 
224786baecfSMauro Carvalho Chehab 	return buf[1];
225786baecfSMauro Carvalho Chehab }
226786baecfSMauro Carvalho Chehab 
227786baecfSMauro Carvalho Chehab static int tt3650_ci_write_cam_control(struct dvb_ca_en50221 *ca,
228786baecfSMauro Carvalho Chehab 				 int			slot,
229786baecfSMauro Carvalho Chehab 				 u8			address,
230786baecfSMauro Carvalho Chehab 				 u8			value)
231786baecfSMauro Carvalho Chehab {
232786baecfSMauro Carvalho Chehab 	u8 buf[2];
233786baecfSMauro Carvalho Chehab 
234786baecfSMauro Carvalho Chehab 	ci_dbg("%s %d 0x%02x 0x%02x",
235786baecfSMauro Carvalho Chehab 		__func__, slot, address, value);
236786baecfSMauro Carvalho Chehab 
237786baecfSMauro Carvalho Chehab 	if (0 != slot)
238786baecfSMauro Carvalho Chehab 		return -EINVAL;
239786baecfSMauro Carvalho Chehab 
240786baecfSMauro Carvalho Chehab 	buf[0] = address;
241786baecfSMauro Carvalho Chehab 	buf[1] = value;
242786baecfSMauro Carvalho Chehab 
243786baecfSMauro Carvalho Chehab 	return tt3650_ci_msg_locked(ca, TT3650_CMD_CI_WR_CTRL, buf, 2, 2);
244786baecfSMauro Carvalho Chehab }
245786baecfSMauro Carvalho Chehab 
246786baecfSMauro Carvalho Chehab static int tt3650_ci_set_video_port(struct dvb_ca_en50221 *ca,
247786baecfSMauro Carvalho Chehab 				 int			slot,
248786baecfSMauro Carvalho Chehab 				 int			enable)
249786baecfSMauro Carvalho Chehab {
250786baecfSMauro Carvalho Chehab 	u8 buf[1];
251786baecfSMauro Carvalho Chehab 	int ret;
252786baecfSMauro Carvalho Chehab 
253786baecfSMauro Carvalho Chehab 	ci_dbg("%s %d %d", __func__, slot, enable);
254786baecfSMauro Carvalho Chehab 
255786baecfSMauro Carvalho Chehab 	if (0 != slot)
256786baecfSMauro Carvalho Chehab 		return -EINVAL;
257786baecfSMauro Carvalho Chehab 
258786baecfSMauro Carvalho Chehab 	enable = !!enable;
259786baecfSMauro Carvalho Chehab 	buf[0] = enable;
260786baecfSMauro Carvalho Chehab 
261786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
262786baecfSMauro Carvalho Chehab 	if (ret < 0)
263786baecfSMauro Carvalho Chehab 		return ret;
264786baecfSMauro Carvalho Chehab 
265786baecfSMauro Carvalho Chehab 	if (enable != buf[0]) {
266786baecfSMauro Carvalho Chehab 		err("CI not %sabled.", enable ? "en" : "dis");
267786baecfSMauro Carvalho Chehab 		return -EIO;
268786baecfSMauro Carvalho Chehab 	}
269786baecfSMauro Carvalho Chehab 
270786baecfSMauro Carvalho Chehab 	return 0;
271786baecfSMauro Carvalho Chehab }
272786baecfSMauro Carvalho Chehab 
273786baecfSMauro Carvalho Chehab static int tt3650_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
274786baecfSMauro Carvalho Chehab {
275786baecfSMauro Carvalho Chehab 	return tt3650_ci_set_video_port(ca, slot, /* enable */ 0);
276786baecfSMauro Carvalho Chehab }
277786baecfSMauro Carvalho Chehab 
278786baecfSMauro Carvalho Chehab static int tt3650_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
279786baecfSMauro Carvalho Chehab {
280786baecfSMauro Carvalho Chehab 	return tt3650_ci_set_video_port(ca, slot, /* enable */ 1);
281786baecfSMauro Carvalho Chehab }
282786baecfSMauro Carvalho Chehab 
283786baecfSMauro Carvalho Chehab static int tt3650_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
284786baecfSMauro Carvalho Chehab {
285786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
286786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
287786baecfSMauro Carvalho Chehab 	u8 buf[1];
288786baecfSMauro Carvalho Chehab 	int ret;
289786baecfSMauro Carvalho Chehab 
290786baecfSMauro Carvalho Chehab 	ci_dbg("%s %d", __func__, slot);
291786baecfSMauro Carvalho Chehab 
292786baecfSMauro Carvalho Chehab 	if (0 != slot)
293786baecfSMauro Carvalho Chehab 		return -EINVAL;
294786baecfSMauro Carvalho Chehab 
295786baecfSMauro Carvalho Chehab 	buf[0] = 0;
296786baecfSMauro Carvalho Chehab 
297786baecfSMauro Carvalho Chehab 	mutex_lock(&state->ca_mutex);
298786baecfSMauro Carvalho Chehab 
299786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
300786baecfSMauro Carvalho Chehab 	if (0 != ret)
301786baecfSMauro Carvalho Chehab 		goto failed;
302786baecfSMauro Carvalho Chehab 
303786baecfSMauro Carvalho Chehab 	msleep(500);
304786baecfSMauro Carvalho Chehab 
305786baecfSMauro Carvalho Chehab 	buf[0] = 1;
306786baecfSMauro Carvalho Chehab 
307786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_RESET, buf, 1, 1);
308786baecfSMauro Carvalho Chehab 	if (0 != ret)
309786baecfSMauro Carvalho Chehab 		goto failed;
310786baecfSMauro Carvalho Chehab 
311786baecfSMauro Carvalho Chehab 	msleep(500);
312786baecfSMauro Carvalho Chehab 
313786baecfSMauro Carvalho Chehab 	buf[0] = 0; /* FTA */
314786baecfSMauro Carvalho Chehab 
315786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg(d, TT3650_CMD_CI_SET_VIDEO_PORT, buf, 1, 1);
316786baecfSMauro Carvalho Chehab 
317786baecfSMauro Carvalho Chehab  failed:
318786baecfSMauro Carvalho Chehab 	mutex_unlock(&state->ca_mutex);
319786baecfSMauro Carvalho Chehab 
320786baecfSMauro Carvalho Chehab 	return ret;
321786baecfSMauro Carvalho Chehab }
322786baecfSMauro Carvalho Chehab 
323786baecfSMauro Carvalho Chehab static int tt3650_ci_poll_slot_status(struct dvb_ca_en50221 *ca,
324786baecfSMauro Carvalho Chehab 				 int			slot,
325786baecfSMauro Carvalho Chehab 				 int			open)
326786baecfSMauro Carvalho Chehab {
327786baecfSMauro Carvalho Chehab 	u8 buf[1];
328786baecfSMauro Carvalho Chehab 	int ret;
329786baecfSMauro Carvalho Chehab 
330786baecfSMauro Carvalho Chehab 	if (0 != slot)
331786baecfSMauro Carvalho Chehab 		return -EINVAL;
332786baecfSMauro Carvalho Chehab 
333786baecfSMauro Carvalho Chehab 	ret = tt3650_ci_msg_locked(ca, TT3650_CMD_CI_TEST, buf, 0, 1);
334786baecfSMauro Carvalho Chehab 	if (0 != ret)
335786baecfSMauro Carvalho Chehab 		return ret;
336786baecfSMauro Carvalho Chehab 
337786baecfSMauro Carvalho Chehab 	if (1 == buf[0])
338786baecfSMauro Carvalho Chehab 		return DVB_CA_EN50221_POLL_CAM_PRESENT |
339786baecfSMauro Carvalho Chehab 			DVB_CA_EN50221_POLL_CAM_READY;
340786baecfSMauro Carvalho Chehab 
341786baecfSMauro Carvalho Chehab 	return 0;
342786baecfSMauro Carvalho Chehab 
343786baecfSMauro Carvalho Chehab }
344786baecfSMauro Carvalho Chehab 
345786baecfSMauro Carvalho Chehab static void tt3650_ci_uninit(struct dvb_usb_device *d)
346786baecfSMauro Carvalho Chehab {
347786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state;
348786baecfSMauro Carvalho Chehab 
349786baecfSMauro Carvalho Chehab 	ci_dbg("%s", __func__);
350786baecfSMauro Carvalho Chehab 
351786baecfSMauro Carvalho Chehab 	if (NULL == d)
352786baecfSMauro Carvalho Chehab 		return;
353786baecfSMauro Carvalho Chehab 
354786baecfSMauro Carvalho Chehab 	state = (struct pctv452e_state *)d->priv;
355786baecfSMauro Carvalho Chehab 	if (NULL == state)
356786baecfSMauro Carvalho Chehab 		return;
357786baecfSMauro Carvalho Chehab 
358786baecfSMauro Carvalho Chehab 	if (NULL == state->ca.data)
359786baecfSMauro Carvalho Chehab 		return;
360786baecfSMauro Carvalho Chehab 
361786baecfSMauro Carvalho Chehab 	/* Error ignored. */
362786baecfSMauro Carvalho Chehab 	tt3650_ci_set_video_port(&state->ca, /* slot */ 0, /* enable */ 0);
363786baecfSMauro Carvalho Chehab 
364786baecfSMauro Carvalho Chehab 	dvb_ca_en50221_release(&state->ca);
365786baecfSMauro Carvalho Chehab 
366786baecfSMauro Carvalho Chehab 	memset(&state->ca, 0, sizeof(state->ca));
367786baecfSMauro Carvalho Chehab }
368786baecfSMauro Carvalho Chehab 
369786baecfSMauro Carvalho Chehab static int tt3650_ci_init(struct dvb_usb_adapter *a)
370786baecfSMauro Carvalho Chehab {
371786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = a->dev;
372786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
373786baecfSMauro Carvalho Chehab 	int ret;
374786baecfSMauro Carvalho Chehab 
375786baecfSMauro Carvalho Chehab 	ci_dbg("%s", __func__);
376786baecfSMauro Carvalho Chehab 
377786baecfSMauro Carvalho Chehab 	mutex_init(&state->ca_mutex);
378786baecfSMauro Carvalho Chehab 
379786baecfSMauro Carvalho Chehab 	state->ca.owner = THIS_MODULE;
380786baecfSMauro Carvalho Chehab 	state->ca.read_attribute_mem = tt3650_ci_read_attribute_mem;
381786baecfSMauro Carvalho Chehab 	state->ca.write_attribute_mem = tt3650_ci_write_attribute_mem;
382786baecfSMauro Carvalho Chehab 	state->ca.read_cam_control = tt3650_ci_read_cam_control;
383786baecfSMauro Carvalho Chehab 	state->ca.write_cam_control = tt3650_ci_write_cam_control;
384786baecfSMauro Carvalho Chehab 	state->ca.slot_reset = tt3650_ci_slot_reset;
385786baecfSMauro Carvalho Chehab 	state->ca.slot_shutdown = tt3650_ci_slot_shutdown;
386786baecfSMauro Carvalho Chehab 	state->ca.slot_ts_enable = tt3650_ci_slot_ts_enable;
387786baecfSMauro Carvalho Chehab 	state->ca.poll_slot_status = tt3650_ci_poll_slot_status;
388786baecfSMauro Carvalho Chehab 	state->ca.data = d;
389786baecfSMauro Carvalho Chehab 
390786baecfSMauro Carvalho Chehab 	ret = dvb_ca_en50221_init(&a->dvb_adap,
391786baecfSMauro Carvalho Chehab 				   &state->ca,
392786baecfSMauro Carvalho Chehab 				   /* flags */ 0,
393786baecfSMauro Carvalho Chehab 				   /* n_slots */ 1);
394786baecfSMauro Carvalho Chehab 	if (0 != ret) {
395786baecfSMauro Carvalho Chehab 		err("Cannot initialize CI: Error %d.", ret);
396786baecfSMauro Carvalho Chehab 		memset(&state->ca, 0, sizeof(state->ca));
397786baecfSMauro Carvalho Chehab 		return ret;
398786baecfSMauro Carvalho Chehab 	}
399786baecfSMauro Carvalho Chehab 
400786baecfSMauro Carvalho Chehab 	info("CI initialized.");
401786baecfSMauro Carvalho Chehab 
402786baecfSMauro Carvalho Chehab 	return 0;
403786baecfSMauro Carvalho Chehab }
404786baecfSMauro Carvalho Chehab 
405786baecfSMauro Carvalho Chehab #define CMD_BUFFER_SIZE 0x28
406786baecfSMauro Carvalho Chehab static int pctv452e_i2c_msg(struct dvb_usb_device *d, u8 addr,
407786baecfSMauro Carvalho Chehab 				const u8 *snd_buf, u8 snd_len,
408786baecfSMauro Carvalho Chehab 				u8 *rcv_buf, u8 rcv_len)
409786baecfSMauro Carvalho Chehab {
410786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
41148775cb7SMax Kellermann 	u8 *buf;
412786baecfSMauro Carvalho Chehab 	u8 id;
413786baecfSMauro Carvalho Chehab 	int ret;
414786baecfSMauro Carvalho Chehab 
41548775cb7SMax Kellermann 	buf = kmalloc(64, GFP_KERNEL);
41648775cb7SMax Kellermann 	if (!buf)
41748775cb7SMax Kellermann 		return -ENOMEM;
41848775cb7SMax Kellermann 
419786baecfSMauro Carvalho Chehab 	id = state->c++;
420786baecfSMauro Carvalho Chehab 
421786baecfSMauro Carvalho Chehab 	ret = -EINVAL;
422786baecfSMauro Carvalho Chehab 	if (snd_len > 64 - 7 || rcv_len > 64 - 7)
423786baecfSMauro Carvalho Chehab 		goto failed;
424786baecfSMauro Carvalho Chehab 
42548775cb7SMax Kellermann 	buf[0] = SYNC_BYTE_OUT;
42648775cb7SMax Kellermann 	buf[1] = id;
42748775cb7SMax Kellermann 	buf[2] = PCTV_CMD_I2C;
42848775cb7SMax Kellermann 	buf[3] = snd_len + 3;
42948775cb7SMax Kellermann 	buf[4] = addr << 1;
43048775cb7SMax Kellermann 	buf[5] = snd_len;
43148775cb7SMax Kellermann 	buf[6] = rcv_len;
432786baecfSMauro Carvalho Chehab 
43348775cb7SMax Kellermann 	memcpy(buf + 7, snd_buf, snd_len);
434786baecfSMauro Carvalho Chehab 
43548775cb7SMax Kellermann 	ret = dvb_usb_generic_rw(d, buf, 7 + snd_len,
43648775cb7SMax Kellermann 				  buf, /* rcv_len */ 64,
437786baecfSMauro Carvalho Chehab 				  /* delay_ms */ 0);
438786baecfSMauro Carvalho Chehab 	if (ret < 0)
439786baecfSMauro Carvalho Chehab 		goto failed;
440786baecfSMauro Carvalho Chehab 
441786baecfSMauro Carvalho Chehab 	/* TT USB protocol error. */
442786baecfSMauro Carvalho Chehab 	ret = -EIO;
44348775cb7SMax Kellermann 	if (SYNC_BYTE_IN != buf[0] || id != buf[1])
444786baecfSMauro Carvalho Chehab 		goto failed;
445786baecfSMauro Carvalho Chehab 
446786baecfSMauro Carvalho Chehab 	/* I2C device didn't respond as expected. */
447786baecfSMauro Carvalho Chehab 	ret = -EREMOTEIO;
44848775cb7SMax Kellermann 	if (buf[5] < snd_len || buf[6] < rcv_len)
449786baecfSMauro Carvalho Chehab 		goto failed;
450786baecfSMauro Carvalho Chehab 
45148775cb7SMax Kellermann 	memcpy(rcv_buf, buf + 7, rcv_len);
452786baecfSMauro Carvalho Chehab 
45348775cb7SMax Kellermann 	kfree(buf);
454786baecfSMauro Carvalho Chehab 	return rcv_len;
455786baecfSMauro Carvalho Chehab 
456786baecfSMauro Carvalho Chehab failed:
45773d5c5c8SMauro Carvalho Chehab 	err("I2C error %d; %02X %02X  %02X %02X %02X -> %*ph",
458786baecfSMauro Carvalho Chehab 	     ret, SYNC_BYTE_OUT, id, addr << 1, snd_len, rcv_len,
45948775cb7SMax Kellermann 	     7, buf);
460786baecfSMauro Carvalho Chehab 
46148775cb7SMax Kellermann 	kfree(buf);
462786baecfSMauro Carvalho Chehab 	return ret;
463786baecfSMauro Carvalho Chehab }
464786baecfSMauro Carvalho Chehab 
465786baecfSMauro Carvalho Chehab static int pctv452e_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg,
466786baecfSMauro Carvalho Chehab 				int num)
467786baecfSMauro Carvalho Chehab {
468786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = i2c_get_adapdata(adapter);
469786baecfSMauro Carvalho Chehab 	int i;
470786baecfSMauro Carvalho Chehab 
471786baecfSMauro Carvalho Chehab 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
472786baecfSMauro Carvalho Chehab 		return -EAGAIN;
473786baecfSMauro Carvalho Chehab 
474786baecfSMauro Carvalho Chehab 	for (i = 0; i < num; i++) {
475786baecfSMauro Carvalho Chehab 		u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
476786baecfSMauro Carvalho Chehab 		int ret;
477786baecfSMauro Carvalho Chehab 
478786baecfSMauro Carvalho Chehab 		if (msg[i].flags & I2C_M_RD) {
479786baecfSMauro Carvalho Chehab 			addr = msg[i].addr;
480786baecfSMauro Carvalho Chehab 			snd_buf = NULL;
481786baecfSMauro Carvalho Chehab 			snd_len = 0;
482786baecfSMauro Carvalho Chehab 			rcv_buf = msg[i].buf;
483786baecfSMauro Carvalho Chehab 			rcv_len = msg[i].len;
484786baecfSMauro Carvalho Chehab 		} else {
485786baecfSMauro Carvalho Chehab 			addr = msg[i].addr;
486786baecfSMauro Carvalho Chehab 			snd_buf = msg[i].buf;
487786baecfSMauro Carvalho Chehab 			snd_len = msg[i].len;
488786baecfSMauro Carvalho Chehab 			rcv_buf = NULL;
489786baecfSMauro Carvalho Chehab 			rcv_len = 0;
490786baecfSMauro Carvalho Chehab 		}
491786baecfSMauro Carvalho Chehab 
492786baecfSMauro Carvalho Chehab 		ret = pctv452e_i2c_msg(d, addr, snd_buf, snd_len, rcv_buf,
493786baecfSMauro Carvalho Chehab 					rcv_len);
494786baecfSMauro Carvalho Chehab 		if (ret < rcv_len)
495786baecfSMauro Carvalho Chehab 			break;
496786baecfSMauro Carvalho Chehab 	}
497786baecfSMauro Carvalho Chehab 
498786baecfSMauro Carvalho Chehab 	mutex_unlock(&d->i2c_mutex);
499786baecfSMauro Carvalho Chehab 	return i;
500786baecfSMauro Carvalho Chehab }
501786baecfSMauro Carvalho Chehab 
502786baecfSMauro Carvalho Chehab static u32 pctv452e_i2c_func(struct i2c_adapter *adapter)
503786baecfSMauro Carvalho Chehab {
504786baecfSMauro Carvalho Chehab 	return I2C_FUNC_I2C;
505786baecfSMauro Carvalho Chehab }
506786baecfSMauro Carvalho Chehab 
507786baecfSMauro Carvalho Chehab static int pctv452e_power_ctrl(struct dvb_usb_device *d, int i)
508786baecfSMauro Carvalho Chehab {
509786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
51048775cb7SMax Kellermann 	u8 *b0, *rx;
511786baecfSMauro Carvalho Chehab 	int ret;
512786baecfSMauro Carvalho Chehab 
513786baecfSMauro Carvalho Chehab 	info("%s: %d\n", __func__, i);
514786baecfSMauro Carvalho Chehab 
515786baecfSMauro Carvalho Chehab 	if (!i)
516786baecfSMauro Carvalho Chehab 		return 0;
517786baecfSMauro Carvalho Chehab 
518786baecfSMauro Carvalho Chehab 	if (state->initialized)
519786baecfSMauro Carvalho Chehab 		return 0;
520786baecfSMauro Carvalho Chehab 
52148775cb7SMax Kellermann 	b0 = kmalloc(5 + PCTV_ANSWER_LEN, GFP_KERNEL);
52248775cb7SMax Kellermann 	if (!b0)
52373d5c5c8SMauro Carvalho Chehab 		return -ENOMEM;
52473d5c5c8SMauro Carvalho Chehab 
52548775cb7SMax Kellermann 	rx = b0 + 5;
52648775cb7SMax Kellermann 
5273e4d8f48SMauro Carvalho Chehab 	/* hmm where should this should go? */
528786baecfSMauro Carvalho Chehab 	ret = usb_set_interface(d->udev, 0, ISOC_INTERFACE_ALTERNATIVE);
529786baecfSMauro Carvalho Chehab 	if (ret != 0)
530786baecfSMauro Carvalho Chehab 		info("%s: Warning set interface returned: %d\n",
531786baecfSMauro Carvalho Chehab 			__func__, ret);
532786baecfSMauro Carvalho Chehab 
5333e4d8f48SMauro Carvalho Chehab 	/* this is a one-time initialization, don't know where to put */
53448775cb7SMax Kellermann 	b0[0] = 0xaa;
53548775cb7SMax Kellermann 	b0[1] = state->c++;
53648775cb7SMax Kellermann 	b0[2] = PCTV_CMD_RESET;
53748775cb7SMax Kellermann 	b0[3] = 1;
53848775cb7SMax Kellermann 	b0[4] = 0;
539786baecfSMauro Carvalho Chehab 	/* reset board */
54048775cb7SMax Kellermann 	ret = dvb_usb_generic_rw(d, b0, 5, rx, PCTV_ANSWER_LEN, 0);
541786baecfSMauro Carvalho Chehab 	if (ret)
54273d5c5c8SMauro Carvalho Chehab 		goto ret;
543786baecfSMauro Carvalho Chehab 
54448775cb7SMax Kellermann 	b0[1] = state->c++;
54548775cb7SMax Kellermann 	b0[4] = 1;
546786baecfSMauro Carvalho Chehab 	/* reset board (again?) */
54748775cb7SMax Kellermann 	ret = dvb_usb_generic_rw(d, b0, 5, rx, PCTV_ANSWER_LEN, 0);
548786baecfSMauro Carvalho Chehab 	if (ret)
54973d5c5c8SMauro Carvalho Chehab 		goto ret;
550786baecfSMauro Carvalho Chehab 
551786baecfSMauro Carvalho Chehab 	state->initialized = 1;
552786baecfSMauro Carvalho Chehab 
55373d5c5c8SMauro Carvalho Chehab ret:
55448775cb7SMax Kellermann 	kfree(b0);
55573d5c5c8SMauro Carvalho Chehab 	return ret;
556786baecfSMauro Carvalho Chehab }
557786baecfSMauro Carvalho Chehab 
558786baecfSMauro Carvalho Chehab static int pctv452e_rc_query(struct dvb_usb_device *d)
559786baecfSMauro Carvalho Chehab {
560786baecfSMauro Carvalho Chehab 	struct pctv452e_state *state = (struct pctv452e_state *)d->priv;
56148775cb7SMax Kellermann 	u8 *b, *rx;
562786baecfSMauro Carvalho Chehab 	int ret, i;
56373d5c5c8SMauro Carvalho Chehab 	u8 id;
56473d5c5c8SMauro Carvalho Chehab 
56548775cb7SMax Kellermann 	b = kmalloc(CMD_BUFFER_SIZE + PCTV_ANSWER_LEN, GFP_KERNEL);
56648775cb7SMax Kellermann 	if (!b)
56748775cb7SMax Kellermann 		return -ENOMEM;
56848775cb7SMax Kellermann 
56948775cb7SMax Kellermann 	rx = b + CMD_BUFFER_SIZE;
57048775cb7SMax Kellermann 
57173d5c5c8SMauro Carvalho Chehab 	id = state->c++;
572786baecfSMauro Carvalho Chehab 
573786baecfSMauro Carvalho Chehab 	/* prepare command header  */
57448775cb7SMax Kellermann 	b[0] = SYNC_BYTE_OUT;
57548775cb7SMax Kellermann 	b[1] = id;
57648775cb7SMax Kellermann 	b[2] = PCTV_CMD_IR;
57748775cb7SMax Kellermann 	b[3] = 0;
578786baecfSMauro Carvalho Chehab 
579786baecfSMauro Carvalho Chehab 	/* send ir request */
58048775cb7SMax Kellermann 	ret = dvb_usb_generic_rw(d, b, 4, rx, PCTV_ANSWER_LEN, 0);
581786baecfSMauro Carvalho Chehab 	if (ret != 0)
58273d5c5c8SMauro Carvalho Chehab 		goto ret;
583786baecfSMauro Carvalho Chehab 
584786baecfSMauro Carvalho Chehab 	if (debug > 3) {
58548775cb7SMax Kellermann 		info("%s: read: %2d: %*ph: ", __func__, ret, 3, rx);
58648775cb7SMax Kellermann 		for (i = 0; (i < rx[3]) && ((i+3) < PCTV_ANSWER_LEN); i++)
58748775cb7SMax Kellermann 			info(" %02x", rx[i+3]);
588786baecfSMauro Carvalho Chehab 
589786baecfSMauro Carvalho Chehab 		info("\n");
590786baecfSMauro Carvalho Chehab 	}
591786baecfSMauro Carvalho Chehab 
59248775cb7SMax Kellermann 	if ((rx[3] == 9) &&  (rx[12] & 0x01)) {
593786baecfSMauro Carvalho Chehab 		/* got a "press" event */
59448775cb7SMax Kellermann 		state->last_rc_key = RC_SCANCODE_RC5(rx[7], rx[6]);
595786baecfSMauro Carvalho Chehab 		if (debug > 2)
596786baecfSMauro Carvalho Chehab 			info("%s: cmd=0x%02x sys=0x%02x\n",
59748775cb7SMax Kellermann 				__func__, rx[6], rx[7]);
598786baecfSMauro Carvalho Chehab 
5996d741bfeSSean Young 		rc_keydown(d->rc_dev, RC_PROTO_RC5, state->last_rc_key, 0);
600786baecfSMauro Carvalho Chehab 	} else if (state->last_rc_key) {
601786baecfSMauro Carvalho Chehab 		rc_keyup(d->rc_dev);
602786baecfSMauro Carvalho Chehab 		state->last_rc_key = 0;
603786baecfSMauro Carvalho Chehab 	}
60473d5c5c8SMauro Carvalho Chehab ret:
60548775cb7SMax Kellermann 	kfree(b);
60673d5c5c8SMauro Carvalho Chehab 	return ret;
607786baecfSMauro Carvalho Chehab }
608786baecfSMauro Carvalho Chehab 
609786baecfSMauro Carvalho Chehab static int pctv452e_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
610786baecfSMauro Carvalho Chehab {
611786baecfSMauro Carvalho Chehab 	const u8 mem_addr[] = { 0x1f, 0xcc };
612786baecfSMauro Carvalho Chehab 	u8 encoded_mac[20];
613786baecfSMauro Carvalho Chehab 	int ret;
614786baecfSMauro Carvalho Chehab 
615786baecfSMauro Carvalho Chehab 	ret = -EAGAIN;
616786baecfSMauro Carvalho Chehab 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
617786baecfSMauro Carvalho Chehab 		goto failed;
618786baecfSMauro Carvalho Chehab 
619786baecfSMauro Carvalho Chehab 	ret = pctv452e_i2c_msg(d, I2C_ADDR_24C16,
620786baecfSMauro Carvalho Chehab 				mem_addr + 1, /* snd_len */ 1,
621786baecfSMauro Carvalho Chehab 				encoded_mac, /* rcv_len */ 20);
622786baecfSMauro Carvalho Chehab 	if (-EREMOTEIO == ret)
623786baecfSMauro Carvalho Chehab 		/* Caution! A 24C16 interprets 0xA2 0x1F 0xCC as a
624786baecfSMauro Carvalho Chehab 		   byte write if /WC is low. */
625786baecfSMauro Carvalho Chehab 		ret = pctv452e_i2c_msg(d, I2C_ADDR_24C64,
626786baecfSMauro Carvalho Chehab 					mem_addr, 2,
627786baecfSMauro Carvalho Chehab 					encoded_mac, 20);
628786baecfSMauro Carvalho Chehab 
629786baecfSMauro Carvalho Chehab 	mutex_unlock(&d->i2c_mutex);
630786baecfSMauro Carvalho Chehab 
631786baecfSMauro Carvalho Chehab 	if (20 != ret)
632786baecfSMauro Carvalho Chehab 		goto failed;
633786baecfSMauro Carvalho Chehab 
634786baecfSMauro Carvalho Chehab 	ret = ttpci_eeprom_decode_mac(mac, encoded_mac);
635786baecfSMauro Carvalho Chehab 	if (0 != ret)
636786baecfSMauro Carvalho Chehab 		goto failed;
637786baecfSMauro Carvalho Chehab 
638786baecfSMauro Carvalho Chehab 	return 0;
639786baecfSMauro Carvalho Chehab 
640786baecfSMauro Carvalho Chehab failed:
64138dcdca3SVaishali Thakkar 	eth_zero_addr(mac);
642786baecfSMauro Carvalho Chehab 
643786baecfSMauro Carvalho Chehab 	return ret;
644786baecfSMauro Carvalho Chehab }
645786baecfSMauro Carvalho Chehab 
646786baecfSMauro Carvalho Chehab static const struct stb0899_s1_reg pctv452e_init_dev[] = {
647786baecfSMauro Carvalho Chehab 	{ STB0899_DISCNTRL1,	0x26 },
648786baecfSMauro Carvalho Chehab 	{ STB0899_DISCNTRL2,	0x80 },
649786baecfSMauro Carvalho Chehab 	{ STB0899_DISRX_ST0,	0x04 },
650786baecfSMauro Carvalho Chehab 	{ STB0899_DISRX_ST1,	0x20 },
651786baecfSMauro Carvalho Chehab 	{ STB0899_DISPARITY,	0x00 },
652786baecfSMauro Carvalho Chehab 	{ STB0899_DISFIFO,	0x00 },
653786baecfSMauro Carvalho Chehab 	{ STB0899_DISF22,	0x99 },
654786baecfSMauro Carvalho Chehab 	{ STB0899_DISF22RX,	0x85 }, /* 0xa8 */
655786baecfSMauro Carvalho Chehab 	{ STB0899_ACRPRESC,	0x11 },
656786baecfSMauro Carvalho Chehab 	{ STB0899_ACRDIV1,	0x0a },
657786baecfSMauro Carvalho Chehab 	{ STB0899_ACRDIV2,	0x05 },
658786baecfSMauro Carvalho Chehab 	{ STB0899_DACR1	,	0x00 },
659786baecfSMauro Carvalho Chehab 	{ STB0899_DACR2	,	0x00 },
660786baecfSMauro Carvalho Chehab 	{ STB0899_OUTCFG,	0x00 },
661786baecfSMauro Carvalho Chehab 	{ STB0899_MODECFG,	0x00 }, /* Inversion */
662786baecfSMauro Carvalho Chehab 	{ STB0899_IRQMSK_3,	0xf3 },
663786baecfSMauro Carvalho Chehab 	{ STB0899_IRQMSK_2,	0xfc },
664786baecfSMauro Carvalho Chehab 	{ STB0899_IRQMSK_1,	0xff },
665786baecfSMauro Carvalho Chehab 	{ STB0899_IRQMSK_0,	0xff },
666786baecfSMauro Carvalho Chehab 	{ STB0899_I2CCFG,	0x88 },
667786baecfSMauro Carvalho Chehab 	{ STB0899_I2CRPT,	0x58 },
668786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO00CFG,	0x82 },
669786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO01CFG,	0x82 }, /* LED: 0x02 green, 0x82 orange */
670786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO02CFG,	0x82 },
671786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO03CFG,	0x82 },
672786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO04CFG,	0x82 },
673786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO05CFG,	0x82 },
674786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO06CFG,	0x82 },
675786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO07CFG,	0x82 },
676786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO08CFG,	0x82 },
677786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO09CFG,	0x82 },
678786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO10CFG,	0x82 },
679786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO11CFG,	0x82 },
680786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO12CFG,	0x82 },
681786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO13CFG,	0x82 },
682786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO14CFG,	0x82 },
683786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO15CFG,	0x82 },
684786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO16CFG,	0x82 },
685786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO17CFG,	0x82 },
686786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO18CFG,	0x82 },
687786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO19CFG,	0x82 },
688786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO20CFG,	0x82 },
689786baecfSMauro Carvalho Chehab 	{ STB0899_SDATCFG,	0xb8 },
690786baecfSMauro Carvalho Chehab 	{ STB0899_SCLTCFG,	0xba },
691786baecfSMauro Carvalho Chehab 	{ STB0899_AGCRFCFG,	0x1c }, /* 0x11 DVB-S; 0x1c DVB-S2 (1c, rjkm) */
692786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO22,	0x82 },
693786baecfSMauro Carvalho Chehab 	{ STB0899_GPIO21,	0x91 },
694786baecfSMauro Carvalho Chehab 	{ STB0899_DIRCLKCFG,	0x82 },
695786baecfSMauro Carvalho Chehab 	{ STB0899_CLKOUT27CFG,	0x7e },
696786baecfSMauro Carvalho Chehab 	{ STB0899_STDBYCFG,	0x82 },
697786baecfSMauro Carvalho Chehab 	{ STB0899_CS0CFG,	0x82 },
698786baecfSMauro Carvalho Chehab 	{ STB0899_CS1CFG,	0x82 },
699786baecfSMauro Carvalho Chehab 	{ STB0899_DISEQCOCFG,	0x20 },
700786baecfSMauro Carvalho Chehab 	{ STB0899_NCOARSE,	0x15 }, /* 0x15 27Mhz, F/3 198MHz, F/6 108MHz */
701786baecfSMauro Carvalho Chehab 	{ STB0899_SYNTCTRL,	0x00 }, /* 0x00 CLKI, 0x02 XTALI */
702786baecfSMauro Carvalho Chehab 	{ STB0899_FILTCTRL,	0x00 },
703786baecfSMauro Carvalho Chehab 	{ STB0899_SYSCTRL,	0x00 },
704786baecfSMauro Carvalho Chehab 	{ STB0899_STOPCLK1,	0x20 }, /* orig: 0x00 budget-ci: 0x20 */
705786baecfSMauro Carvalho Chehab 	{ STB0899_STOPCLK2,	0x00 },
706786baecfSMauro Carvalho Chehab 	{ STB0899_INTBUFCTRL,	0x0a },
707786baecfSMauro Carvalho Chehab 	{ STB0899_AGC2I1,	0x00 },
708786baecfSMauro Carvalho Chehab 	{ STB0899_AGC2I2,	0x00 },
709786baecfSMauro Carvalho Chehab 	{ STB0899_AGCIQIN,	0x00 },
710786baecfSMauro Carvalho Chehab 	{ STB0899_TSTRES,	0x40 }, /* rjkm */
711786baecfSMauro Carvalho Chehab 	{ 0xffff,		0xff },
712786baecfSMauro Carvalho Chehab };
713786baecfSMauro Carvalho Chehab 
714786baecfSMauro Carvalho Chehab static const struct stb0899_s1_reg pctv452e_init_s1_demod[] = {
715786baecfSMauro Carvalho Chehab 	{ STB0899_DEMOD,	0x00 },
716786baecfSMauro Carvalho Chehab 	{ STB0899_RCOMPC,	0xc9 },
717786baecfSMauro Carvalho Chehab 	{ STB0899_AGC1CN,	0x01 },
718786baecfSMauro Carvalho Chehab 	{ STB0899_AGC1REF,	0x10 },
719786baecfSMauro Carvalho Chehab 	{ STB0899_RTC,		0x23 },
720786baecfSMauro Carvalho Chehab 	{ STB0899_TMGCFG,	0x4e },
721786baecfSMauro Carvalho Chehab 	{ STB0899_AGC2REF,	0x34 },
722786baecfSMauro Carvalho Chehab 	{ STB0899_TLSR,		0x84 },
723786baecfSMauro Carvalho Chehab 	{ STB0899_CFD,		0xf7 },
724786baecfSMauro Carvalho Chehab 	{ STB0899_ACLC,		0x87 },
725786baecfSMauro Carvalho Chehab 	{ STB0899_BCLC,		0x94 },
726786baecfSMauro Carvalho Chehab 	{ STB0899_EQON,		0x41 },
727786baecfSMauro Carvalho Chehab 	{ STB0899_LDT,		0xf1 },
728786baecfSMauro Carvalho Chehab 	{ STB0899_LDT2,		0xe3 },
729786baecfSMauro Carvalho Chehab 	{ STB0899_EQUALREF,	0xb4 },
730786baecfSMauro Carvalho Chehab 	{ STB0899_TMGRAMP,	0x10 },
731786baecfSMauro Carvalho Chehab 	{ STB0899_TMGTHD,	0x30 },
732786baecfSMauro Carvalho Chehab 	{ STB0899_IDCCOMP,	0xfd },
733786baecfSMauro Carvalho Chehab 	{ STB0899_QDCCOMP,	0xff },
734786baecfSMauro Carvalho Chehab 	{ STB0899_POWERI,	0x0c },
735786baecfSMauro Carvalho Chehab 	{ STB0899_POWERQ,	0x0f },
736786baecfSMauro Carvalho Chehab 	{ STB0899_RCOMP,	0x6c },
737786baecfSMauro Carvalho Chehab 	{ STB0899_AGCIQIN,	0x80 },
738786baecfSMauro Carvalho Chehab 	{ STB0899_AGC2I1,	0x06 },
739786baecfSMauro Carvalho Chehab 	{ STB0899_AGC2I2,	0x00 },
740786baecfSMauro Carvalho Chehab 	{ STB0899_TLIR,		0x30 },
741786baecfSMauro Carvalho Chehab 	{ STB0899_RTF,		0x7f },
742786baecfSMauro Carvalho Chehab 	{ STB0899_DSTATUS,	0x00 },
743786baecfSMauro Carvalho Chehab 	{ STB0899_LDI,		0xbc },
744786baecfSMauro Carvalho Chehab 	{ STB0899_CFRM,		0xea },
745786baecfSMauro Carvalho Chehab 	{ STB0899_CFRL,		0x31 },
746786baecfSMauro Carvalho Chehab 	{ STB0899_NIRM,		0x2b },
747786baecfSMauro Carvalho Chehab 	{ STB0899_NIRL,		0x80 },
748786baecfSMauro Carvalho Chehab 	{ STB0899_ISYMB,	0x1d },
749786baecfSMauro Carvalho Chehab 	{ STB0899_QSYMB,	0xa6 },
750786baecfSMauro Carvalho Chehab 	{ STB0899_SFRH,		0x2f },
751786baecfSMauro Carvalho Chehab 	{ STB0899_SFRM,		0x68 },
752786baecfSMauro Carvalho Chehab 	{ STB0899_SFRL,		0x40 },
753786baecfSMauro Carvalho Chehab 	{ STB0899_SFRUPH,	0x2f },
754786baecfSMauro Carvalho Chehab 	{ STB0899_SFRUPM,	0x68 },
755786baecfSMauro Carvalho Chehab 	{ STB0899_SFRUPL,	0x40 },
756786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAI1,	0x02 },
757786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAQ1,	0xff },
758786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAI2,	0x04 },
759786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAQ2,	0x05 },
760786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAI3,	0x02 },
761786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAQ3,	0xfd },
762786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAI4,	0x03 },
763786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAQ4,	0x07 },
764786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAI5,	0x08 },
765786baecfSMauro Carvalho Chehab 	{ STB0899_EQUAQ5,	0xf5 },
766786baecfSMauro Carvalho Chehab 	{ STB0899_DSTATUS2,	0x00 },
767786baecfSMauro Carvalho Chehab 	{ STB0899_VSTATUS,	0x00 },
768786baecfSMauro Carvalho Chehab 	{ STB0899_VERROR,	0x86 },
769786baecfSMauro Carvalho Chehab 	{ STB0899_IQSWAP,	0x2a },
770786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT1M,	0x00 },
771786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT1L,	0x00 },
772786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT2M,	0x00 },
773786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT2L,	0x00 },
774786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT3M,	0x0a },
775786baecfSMauro Carvalho Chehab 	{ STB0899_ECNT3L,	0xad },
776786baecfSMauro Carvalho Chehab 	{ STB0899_FECAUTO1,	0x06 },
777786baecfSMauro Carvalho Chehab 	{ STB0899_FECM,		0x01 },
778786baecfSMauro Carvalho Chehab 	{ STB0899_VTH12,	0xb0 },
779786baecfSMauro Carvalho Chehab 	{ STB0899_VTH23,	0x7a },
780786baecfSMauro Carvalho Chehab 	{ STB0899_VTH34,	0x58 },
781786baecfSMauro Carvalho Chehab 	{ STB0899_VTH56,	0x38 },
782786baecfSMauro Carvalho Chehab 	{ STB0899_VTH67,	0x34 },
783786baecfSMauro Carvalho Chehab 	{ STB0899_VTH78,	0x24 },
784786baecfSMauro Carvalho Chehab 	{ STB0899_PRVIT,	0xff },
785786baecfSMauro Carvalho Chehab 	{ STB0899_VITSYNC,	0x19 },
786786baecfSMauro Carvalho Chehab 	{ STB0899_RSULC,	0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
787786baecfSMauro Carvalho Chehab 	{ STB0899_TSULC,	0x42 },
788786baecfSMauro Carvalho Chehab 	{ STB0899_RSLLC,	0x41 },
789786baecfSMauro Carvalho Chehab 	{ STB0899_TSLPL,	0x12 },
790786baecfSMauro Carvalho Chehab 	{ STB0899_TSCFGH,	0x0c },
791786baecfSMauro Carvalho Chehab 	{ STB0899_TSCFGM,	0x00 },
792786baecfSMauro Carvalho Chehab 	{ STB0899_TSCFGL,	0x00 },
793786baecfSMauro Carvalho Chehab 	{ STB0899_TSOUT,	0x69 }, /* 0x0d for CAM */
794786baecfSMauro Carvalho Chehab 	{ STB0899_RSSYNCDEL,	0x00 },
795786baecfSMauro Carvalho Chehab 	{ STB0899_TSINHDELH,	0x02 },
796786baecfSMauro Carvalho Chehab 	{ STB0899_TSINHDELM,	0x00 },
797786baecfSMauro Carvalho Chehab 	{ STB0899_TSINHDELL,	0x00 },
798786baecfSMauro Carvalho Chehab 	{ STB0899_TSLLSTKM,	0x1b },
799786baecfSMauro Carvalho Chehab 	{ STB0899_TSLLSTKL,	0xb3 },
800786baecfSMauro Carvalho Chehab 	{ STB0899_TSULSTKM,	0x00 },
801786baecfSMauro Carvalho Chehab 	{ STB0899_TSULSTKL,	0x00 },
802786baecfSMauro Carvalho Chehab 	{ STB0899_PCKLENUL,	0xbc },
803786baecfSMauro Carvalho Chehab 	{ STB0899_PCKLENLL,	0xcc },
804786baecfSMauro Carvalho Chehab 	{ STB0899_RSPCKLEN,	0xbd },
805786baecfSMauro Carvalho Chehab 	{ STB0899_TSSTATUS,	0x90 },
806786baecfSMauro Carvalho Chehab 	{ STB0899_ERRCTRL1,	0xb6 },
807786baecfSMauro Carvalho Chehab 	{ STB0899_ERRCTRL2,	0x95 },
808786baecfSMauro Carvalho Chehab 	{ STB0899_ERRCTRL3,	0x8d },
809786baecfSMauro Carvalho Chehab 	{ STB0899_DMONMSK1,	0x27 },
810786baecfSMauro Carvalho Chehab 	{ STB0899_DMONMSK0,	0x03 },
811786baecfSMauro Carvalho Chehab 	{ STB0899_DEMAPVIT,	0x5c },
812786baecfSMauro Carvalho Chehab 	{ STB0899_PLPARM,	0x19 },
813786baecfSMauro Carvalho Chehab 	{ STB0899_PDELCTRL,	0x48 },
814786baecfSMauro Carvalho Chehab 	{ STB0899_PDELCTRL2,	0x00 },
815786baecfSMauro Carvalho Chehab 	{ STB0899_BBHCTRL1,	0x00 },
816786baecfSMauro Carvalho Chehab 	{ STB0899_BBHCTRL2,	0x00 },
817786baecfSMauro Carvalho Chehab 	{ STB0899_HYSTTHRESH,	0x77 },
818786baecfSMauro Carvalho Chehab 	{ STB0899_MATCSTM,	0x00 },
819786baecfSMauro Carvalho Chehab 	{ STB0899_MATCSTL,	0x00 },
820786baecfSMauro Carvalho Chehab 	{ STB0899_UPLCSTM,	0x00 },
821786baecfSMauro Carvalho Chehab 	{ STB0899_UPLCSTL,	0x00 },
822786baecfSMauro Carvalho Chehab 	{ STB0899_DFLCSTM,	0x00 },
823786baecfSMauro Carvalho Chehab 	{ STB0899_DFLCSTL,	0x00 },
824786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCCST,	0x00 },
825786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCDCSTM,	0x00 },
826786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCDCSTL,	0x00 },
827786baecfSMauro Carvalho Chehab 	{ STB0899_ISI_ENTRY,	0x00 },
828786baecfSMauro Carvalho Chehab 	{ STB0899_ISI_BIT_EN,	0x00 },
829786baecfSMauro Carvalho Chehab 	{ STB0899_MATSTRM,	0xf0 },
830786baecfSMauro Carvalho Chehab 	{ STB0899_MATSTRL,	0x02 },
831786baecfSMauro Carvalho Chehab 	{ STB0899_UPLSTRM,	0x45 },
832786baecfSMauro Carvalho Chehab 	{ STB0899_UPLSTRL,	0x60 },
833786baecfSMauro Carvalho Chehab 	{ STB0899_DFLSTRM,	0xe3 },
834786baecfSMauro Carvalho Chehab 	{ STB0899_DFLSTRL,	0x00 },
835786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCSTR,	0x47 },
836786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCDSTRM,	0x05 },
837786baecfSMauro Carvalho Chehab 	{ STB0899_SYNCDSTRL,	0x18 },
838786baecfSMauro Carvalho Chehab 	{ STB0899_CFGPDELSTATUS1, 0x19 },
839786baecfSMauro Carvalho Chehab 	{ STB0899_CFGPDELSTATUS2, 0x2b },
840786baecfSMauro Carvalho Chehab 	{ STB0899_BBFERRORM,	0x00 },
841786baecfSMauro Carvalho Chehab 	{ STB0899_BBFERRORL,	0x01 },
842786baecfSMauro Carvalho Chehab 	{ STB0899_UPKTERRORM,	0x00 },
843786baecfSMauro Carvalho Chehab 	{ STB0899_UPKTERRORL,	0x00 },
844786baecfSMauro Carvalho Chehab 	{ 0xffff,		0xff },
845786baecfSMauro Carvalho Chehab };
846786baecfSMauro Carvalho Chehab 
847786baecfSMauro Carvalho Chehab static struct stb0899_config stb0899_config = {
848786baecfSMauro Carvalho Chehab 	.init_dev	= pctv452e_init_dev,
849786baecfSMauro Carvalho Chehab 	.init_s2_demod	= stb0899_s2_init_2,
850786baecfSMauro Carvalho Chehab 	.init_s1_demod	= pctv452e_init_s1_demod,
851786baecfSMauro Carvalho Chehab 	.init_s2_fec	= stb0899_s2_init_4,
852786baecfSMauro Carvalho Chehab 	.init_tst	= stb0899_s1_init_5,
853786baecfSMauro Carvalho Chehab 
854786baecfSMauro Carvalho Chehab 	.demod_address   = I2C_ADDR_STB0899, /* I2C Address */
855786baecfSMauro Carvalho Chehab 	.block_sync_mode = STB0899_SYNC_FORCED, /* ? */
856786baecfSMauro Carvalho Chehab 
857786baecfSMauro Carvalho Chehab 	.xtal_freq       = 27000000,	 /* Assume Hz ? */
8580735647cSReinhard Nissl 	.inversion       = IQ_SWAP_ON,
859786baecfSMauro Carvalho Chehab 
860786baecfSMauro Carvalho Chehab 	.lo_clk	  = 76500000,
861786baecfSMauro Carvalho Chehab 	.hi_clk	  = 99000000,
862786baecfSMauro Carvalho Chehab 
863786baecfSMauro Carvalho Chehab 	.ts_output_mode  = 0,	/* Use parallel mode */
864786baecfSMauro Carvalho Chehab 	.clock_polarity  = 0,
865786baecfSMauro Carvalho Chehab 	.data_clk_parity = 0,
866786baecfSMauro Carvalho Chehab 	.fec_mode	= 0,
867786baecfSMauro Carvalho Chehab 
868786baecfSMauro Carvalho Chehab 	.esno_ave	    = STB0899_DVBS2_ESNO_AVE,
869786baecfSMauro Carvalho Chehab 	.esno_quant	  = STB0899_DVBS2_ESNO_QUANT,
870786baecfSMauro Carvalho Chehab 	.avframes_coarse     = STB0899_DVBS2_AVFRAMES_COARSE,
871786baecfSMauro Carvalho Chehab 	.avframes_fine       = STB0899_DVBS2_AVFRAMES_FINE,
872786baecfSMauro Carvalho Chehab 	.miss_threshold      = STB0899_DVBS2_MISS_THRESHOLD,
873786baecfSMauro Carvalho Chehab 	.uwp_threshold_acq   = STB0899_DVBS2_UWP_THRESHOLD_ACQ,
874786baecfSMauro Carvalho Chehab 	.uwp_threshold_track = STB0899_DVBS2_UWP_THRESHOLD_TRACK,
875786baecfSMauro Carvalho Chehab 	.uwp_threshold_sof   = STB0899_DVBS2_UWP_THRESHOLD_SOF,
876786baecfSMauro Carvalho Chehab 	.sof_search_timeout  = STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
877786baecfSMauro Carvalho Chehab 
878786baecfSMauro Carvalho Chehab 	.btr_nco_bits	  = STB0899_DVBS2_BTR_NCO_BITS,
879786baecfSMauro Carvalho Chehab 	.btr_gain_shift_offset = STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
880786baecfSMauro Carvalho Chehab 	.crl_nco_bits	  = STB0899_DVBS2_CRL_NCO_BITS,
881786baecfSMauro Carvalho Chehab 	.ldpc_max_iter	 = STB0899_DVBS2_LDPC_MAX_ITER,
882786baecfSMauro Carvalho Chehab 
883786baecfSMauro Carvalho Chehab 	.tuner_get_frequency	= stb6100_get_frequency,
884786baecfSMauro Carvalho Chehab 	.tuner_set_frequency	= stb6100_set_frequency,
885786baecfSMauro Carvalho Chehab 	.tuner_set_bandwidth	= stb6100_set_bandwidth,
886786baecfSMauro Carvalho Chehab 	.tuner_get_bandwidth	= stb6100_get_bandwidth,
887786baecfSMauro Carvalho Chehab 	.tuner_set_rfsiggain	= NULL,
888786baecfSMauro Carvalho Chehab 
889786baecfSMauro Carvalho Chehab 	/* helper for switching LED green/orange */
890786baecfSMauro Carvalho Chehab 	.postproc = pctv45e_postproc
891786baecfSMauro Carvalho Chehab };
892786baecfSMauro Carvalho Chehab 
893786baecfSMauro Carvalho Chehab static struct stb6100_config stb6100_config = {
894786baecfSMauro Carvalho Chehab 	.tuner_address = I2C_ADDR_STB6100,
895786baecfSMauro Carvalho Chehab 	.refclock      = 27000000
896786baecfSMauro Carvalho Chehab };
897786baecfSMauro Carvalho Chehab 
898786baecfSMauro Carvalho Chehab 
899786baecfSMauro Carvalho Chehab static struct i2c_algorithm pctv452e_i2c_algo = {
900786baecfSMauro Carvalho Chehab 	.master_xfer   = pctv452e_i2c_xfer,
901786baecfSMauro Carvalho Chehab 	.functionality = pctv452e_i2c_func
902786baecfSMauro Carvalho Chehab };
903786baecfSMauro Carvalho Chehab 
904786baecfSMauro Carvalho Chehab static int pctv452e_frontend_attach(struct dvb_usb_adapter *a)
905786baecfSMauro Carvalho Chehab {
906786baecfSMauro Carvalho Chehab 	struct usb_device_id *id;
907786baecfSMauro Carvalho Chehab 
908786baecfSMauro Carvalho Chehab 	a->fe_adap[0].fe = dvb_attach(stb0899_attach, &stb0899_config,
909786baecfSMauro Carvalho Chehab 						&a->dev->i2c_adap);
910786baecfSMauro Carvalho Chehab 	if (!a->fe_adap[0].fe)
911786baecfSMauro Carvalho Chehab 		return -ENODEV;
9126cdeaed3SWolfgang Rohdewald 
9136cdeaed3SWolfgang Rohdewald 	/*
9146cdeaed3SWolfgang Rohdewald 	 * dvb_frontend will call dvb_detach for both stb0899_detach
9156cdeaed3SWolfgang Rohdewald 	 * and stb0899_release but we only do dvb_attach(stb0899_attach).
9166cdeaed3SWolfgang Rohdewald 	 * Increment the module refcount instead.
9176cdeaed3SWolfgang Rohdewald 	 */
9186cdeaed3SWolfgang Rohdewald 	symbol_get(stb0899_attach);
9196cdeaed3SWolfgang Rohdewald 
920786baecfSMauro Carvalho Chehab 	if ((dvb_attach(lnbp22_attach, a->fe_adap[0].fe,
921ee40d32fSHans Verkuil 					&a->dev->i2c_adap)) == NULL)
922786baecfSMauro Carvalho Chehab 		err("Cannot attach lnbp22\n");
923786baecfSMauro Carvalho Chehab 
924786baecfSMauro Carvalho Chehab 	id = a->dev->desc->warm_ids[0];
925786baecfSMauro Carvalho Chehab 	if (USB_VID_TECHNOTREND == id->idVendor
926786baecfSMauro Carvalho Chehab 	    && USB_PID_TECHNOTREND_CONNECT_S2_3650_CI == id->idProduct)
927786baecfSMauro Carvalho Chehab 		/* Error ignored. */
928786baecfSMauro Carvalho Chehab 		tt3650_ci_init(a);
929786baecfSMauro Carvalho Chehab 
930786baecfSMauro Carvalho Chehab 	return 0;
931786baecfSMauro Carvalho Chehab }
932786baecfSMauro Carvalho Chehab 
933786baecfSMauro Carvalho Chehab static int pctv452e_tuner_attach(struct dvb_usb_adapter *a)
934786baecfSMauro Carvalho Chehab {
935786baecfSMauro Carvalho Chehab 	if (!a->fe_adap[0].fe)
936786baecfSMauro Carvalho Chehab 		return -ENODEV;
937786baecfSMauro Carvalho Chehab 	if (dvb_attach(stb6100_attach, a->fe_adap[0].fe, &stb6100_config,
938ee40d32fSHans Verkuil 					&a->dev->i2c_adap) == NULL) {
939786baecfSMauro Carvalho Chehab 		err("%s failed\n", __func__);
940786baecfSMauro Carvalho Chehab 		return -ENODEV;
941786baecfSMauro Carvalho Chehab 	}
942786baecfSMauro Carvalho Chehab 
943786baecfSMauro Carvalho Chehab 	return 0;
944786baecfSMauro Carvalho Chehab }
945786baecfSMauro Carvalho Chehab 
946786baecfSMauro Carvalho Chehab static struct usb_device_id pctv452e_usb_table[] = {
947786baecfSMauro Carvalho Chehab 	{USB_DEVICE(USB_VID_PINNACLE, USB_PID_PCTV_452E)},
948786baecfSMauro Carvalho Chehab 	{USB_DEVICE(USB_VID_TECHNOTREND, USB_PID_TECHNOTREND_CONNECT_S2_3600)},
949786baecfSMauro Carvalho Chehab 	{USB_DEVICE(USB_VID_TECHNOTREND,
950786baecfSMauro Carvalho Chehab 				USB_PID_TECHNOTREND_CONNECT_S2_3650_CI)},
951786baecfSMauro Carvalho Chehab 	{}
952786baecfSMauro Carvalho Chehab };
953786baecfSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, pctv452e_usb_table);
954786baecfSMauro Carvalho Chehab 
955786baecfSMauro Carvalho Chehab static struct dvb_usb_device_properties pctv452e_properties = {
956786baecfSMauro Carvalho Chehab 	.caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
957786baecfSMauro Carvalho Chehab 	.usb_ctrl = DEVICE_SPECIFIC,
958786baecfSMauro Carvalho Chehab 
959786baecfSMauro Carvalho Chehab 	.size_of_priv     = sizeof(struct pctv452e_state),
960786baecfSMauro Carvalho Chehab 
961786baecfSMauro Carvalho Chehab 	.power_ctrl       = pctv452e_power_ctrl,
962786baecfSMauro Carvalho Chehab 
963786baecfSMauro Carvalho Chehab 	.rc.core = {
964786baecfSMauro Carvalho Chehab 		.rc_codes	= RC_MAP_DIB0700_RC5_TABLE,
9656d741bfeSSean Young 		.allowed_protos	= RC_PROTO_BIT_RC5,
966786baecfSMauro Carvalho Chehab 		.rc_query	= pctv452e_rc_query,
967786baecfSMauro Carvalho Chehab 		.rc_interval	= 100,
968786baecfSMauro Carvalho Chehab 	},
969786baecfSMauro Carvalho Chehab 
970786baecfSMauro Carvalho Chehab 	.num_adapters     = 1,
971786baecfSMauro Carvalho Chehab 	.adapter = {{
972786baecfSMauro Carvalho Chehab 		.num_frontends = 1,
973786baecfSMauro Carvalho Chehab 		.fe = {{
974786baecfSMauro Carvalho Chehab 			.frontend_attach  = pctv452e_frontend_attach,
975786baecfSMauro Carvalho Chehab 			.tuner_attach     = pctv452e_tuner_attach,
976786baecfSMauro Carvalho Chehab 
977786baecfSMauro Carvalho Chehab 			/* parameter for the MPEG2-data transfer */
978786baecfSMauro Carvalho Chehab 			.stream = {
979786baecfSMauro Carvalho Chehab 				.type     = USB_ISOC,
980786baecfSMauro Carvalho Chehab 				.count    = 4,
981786baecfSMauro Carvalho Chehab 				.endpoint = 0x02,
982786baecfSMauro Carvalho Chehab 				.u = {
983786baecfSMauro Carvalho Chehab 					.isoc = {
984786baecfSMauro Carvalho Chehab 						.framesperurb = 4,
985786baecfSMauro Carvalho Chehab 						.framesize    = 940,
986786baecfSMauro Carvalho Chehab 						.interval     = 1
987786baecfSMauro Carvalho Chehab 					}
988786baecfSMauro Carvalho Chehab 				}
989786baecfSMauro Carvalho Chehab 			},
990786baecfSMauro Carvalho Chehab 		} },
991786baecfSMauro Carvalho Chehab 	} },
992786baecfSMauro Carvalho Chehab 
993786baecfSMauro Carvalho Chehab 	.i2c_algo = &pctv452e_i2c_algo,
994786baecfSMauro Carvalho Chehab 
995786baecfSMauro Carvalho Chehab 	.generic_bulk_ctrl_endpoint = 1, /* allow generice rw function */
996786baecfSMauro Carvalho Chehab 
997786baecfSMauro Carvalho Chehab 	.num_device_descs = 1,
998786baecfSMauro Carvalho Chehab 	.devices = {
999786baecfSMauro Carvalho Chehab 		{ .name = "PCTV HDTV USB",
1000786baecfSMauro Carvalho Chehab 		  .cold_ids = { NULL, NULL }, /* this is a warm only device */
1001786baecfSMauro Carvalho Chehab 		  .warm_ids = { &pctv452e_usb_table[0], NULL }
1002786baecfSMauro Carvalho Chehab 		},
1003ee40d32fSHans Verkuil 		{ NULL },
1004786baecfSMauro Carvalho Chehab 	}
1005786baecfSMauro Carvalho Chehab };
1006786baecfSMauro Carvalho Chehab 
1007786baecfSMauro Carvalho Chehab static struct dvb_usb_device_properties tt_connect_s2_3600_properties = {
1008786baecfSMauro Carvalho Chehab 	.caps = DVB_USB_IS_AN_I2C_ADAPTER, /* more ? */
1009786baecfSMauro Carvalho Chehab 	.usb_ctrl = DEVICE_SPECIFIC,
1010786baecfSMauro Carvalho Chehab 
1011786baecfSMauro Carvalho Chehab 	.size_of_priv		= sizeof(struct pctv452e_state),
1012786baecfSMauro Carvalho Chehab 
1013786baecfSMauro Carvalho Chehab 	.power_ctrl		= pctv452e_power_ctrl,
1014786baecfSMauro Carvalho Chehab 	.read_mac_address	= pctv452e_read_mac_address,
1015786baecfSMauro Carvalho Chehab 
1016786baecfSMauro Carvalho Chehab 	.rc.core = {
1017786baecfSMauro Carvalho Chehab 		.rc_codes	= RC_MAP_TT_1500,
10186d741bfeSSean Young 		.allowed_protos	= RC_PROTO_BIT_RC5,
1019786baecfSMauro Carvalho Chehab 		.rc_query	= pctv452e_rc_query,
1020786baecfSMauro Carvalho Chehab 		.rc_interval	= 100,
1021786baecfSMauro Carvalho Chehab 	},
1022786baecfSMauro Carvalho Chehab 
1023786baecfSMauro Carvalho Chehab 	.num_adapters		= 1,
1024786baecfSMauro Carvalho Chehab 	.adapter = {{
1025786baecfSMauro Carvalho Chehab 		.num_frontends = 1,
1026786baecfSMauro Carvalho Chehab 		.fe = {{
1027786baecfSMauro Carvalho Chehab 			.frontend_attach = pctv452e_frontend_attach,
1028786baecfSMauro Carvalho Chehab 			.tuner_attach = pctv452e_tuner_attach,
1029786baecfSMauro Carvalho Chehab 
1030786baecfSMauro Carvalho Chehab 			/* parameter for the MPEG2-data transfer */
1031786baecfSMauro Carvalho Chehab 			.stream = {
1032786baecfSMauro Carvalho Chehab 				.type = USB_ISOC,
1033a403ceebSOlli Salonen 				.count = 4,
1034786baecfSMauro Carvalho Chehab 				.endpoint = 0x02,
1035786baecfSMauro Carvalho Chehab 				.u = {
1036786baecfSMauro Carvalho Chehab 					.isoc = {
1037a403ceebSOlli Salonen 						.framesperurb = 64,
1038786baecfSMauro Carvalho Chehab 						.framesize = 940,
1039786baecfSMauro Carvalho Chehab 						.interval = 1
1040786baecfSMauro Carvalho Chehab 					}
1041786baecfSMauro Carvalho Chehab 				}
1042786baecfSMauro Carvalho Chehab 			},
1043786baecfSMauro Carvalho Chehab 
1044786baecfSMauro Carvalho Chehab 		} },
1045786baecfSMauro Carvalho Chehab 	} },
1046786baecfSMauro Carvalho Chehab 
1047786baecfSMauro Carvalho Chehab 	.i2c_algo = &pctv452e_i2c_algo,
1048786baecfSMauro Carvalho Chehab 
1049786baecfSMauro Carvalho Chehab 	.generic_bulk_ctrl_endpoint = 1, /* allow generic rw function*/
1050786baecfSMauro Carvalho Chehab 
1051786baecfSMauro Carvalho Chehab 	.num_device_descs = 2,
1052786baecfSMauro Carvalho Chehab 	.devices = {
1053786baecfSMauro Carvalho Chehab 		{ .name = "Technotrend TT Connect S2-3600",
1054786baecfSMauro Carvalho Chehab 		  .cold_ids = { NULL, NULL }, /* this is a warm only device */
1055786baecfSMauro Carvalho Chehab 		  .warm_ids = { &pctv452e_usb_table[1], NULL }
1056786baecfSMauro Carvalho Chehab 		},
1057786baecfSMauro Carvalho Chehab 		{ .name = "Technotrend TT Connect S2-3650-CI",
1058786baecfSMauro Carvalho Chehab 		  .cold_ids = { NULL, NULL },
1059786baecfSMauro Carvalho Chehab 		  .warm_ids = { &pctv452e_usb_table[2], NULL }
1060786baecfSMauro Carvalho Chehab 		},
1061ee40d32fSHans Verkuil 		{ NULL },
1062786baecfSMauro Carvalho Chehab 	}
1063786baecfSMauro Carvalho Chehab };
1064786baecfSMauro Carvalho Chehab 
1065786baecfSMauro Carvalho Chehab static void pctv452e_usb_disconnect(struct usb_interface *intf)
1066786baecfSMauro Carvalho Chehab {
1067786baecfSMauro Carvalho Chehab 	struct dvb_usb_device *d = usb_get_intfdata(intf);
1068786baecfSMauro Carvalho Chehab 
1069786baecfSMauro Carvalho Chehab 	tt3650_ci_uninit(d);
1070786baecfSMauro Carvalho Chehab 	dvb_usb_device_exit(intf);
1071786baecfSMauro Carvalho Chehab }
1072786baecfSMauro Carvalho Chehab 
1073786baecfSMauro Carvalho Chehab static int pctv452e_usb_probe(struct usb_interface *intf,
1074786baecfSMauro Carvalho Chehab 				const struct usb_device_id *id)
1075786baecfSMauro Carvalho Chehab {
1076786baecfSMauro Carvalho Chehab 	if (0 == dvb_usb_device_init(intf, &pctv452e_properties,
1077786baecfSMauro Carvalho Chehab 					THIS_MODULE, NULL, adapter_nr) ||
1078786baecfSMauro Carvalho Chehab 	    0 == dvb_usb_device_init(intf, &tt_connect_s2_3600_properties,
1079786baecfSMauro Carvalho Chehab 					THIS_MODULE, NULL, adapter_nr))
1080786baecfSMauro Carvalho Chehab 		return 0;
1081786baecfSMauro Carvalho Chehab 
1082786baecfSMauro Carvalho Chehab 	return -ENODEV;
1083786baecfSMauro Carvalho Chehab }
1084786baecfSMauro Carvalho Chehab 
1085786baecfSMauro Carvalho Chehab static struct usb_driver pctv452e_usb_driver = {
1086786baecfSMauro Carvalho Chehab 	.name       = "pctv452e",
1087786baecfSMauro Carvalho Chehab 	.probe      = pctv452e_usb_probe,
1088786baecfSMauro Carvalho Chehab 	.disconnect = pctv452e_usb_disconnect,
1089786baecfSMauro Carvalho Chehab 	.id_table   = pctv452e_usb_table,
1090786baecfSMauro Carvalho Chehab };
1091786baecfSMauro Carvalho Chehab 
1092786baecfSMauro Carvalho Chehab module_usb_driver(pctv452e_usb_driver);
1093786baecfSMauro Carvalho Chehab 
1094786baecfSMauro Carvalho Chehab MODULE_AUTHOR("Dominik Kuhlen <dkuhlen@gmx.net>");
1095786baecfSMauro Carvalho Chehab MODULE_AUTHOR("Andre Weidemann <Andre.Weidemann@web.de>");
1096786baecfSMauro Carvalho Chehab MODULE_AUTHOR("Michael H. Schimek <mschimek@gmx.at>");
1097786baecfSMauro Carvalho Chehab MODULE_DESCRIPTION("Pinnacle PCTV HDTV USB DVB / TT connect S2-3600 Driver");
1098786baecfSMauro Carvalho Chehab MODULE_LICENSE("GPL");
1099