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