1 /*
2  * Afatech AF9035 DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License along
18  *    with this program; if not, write to the Free Software Foundation, Inc.,
19  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "af9035.h"
23 
24 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE  64
26 
27 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
28 
29 static u16 af9035_checksum(const u8 *buf, size_t len)
30 {
31 	size_t i;
32 	u16 checksum = 0;
33 
34 	for (i = 1; i < len; i++) {
35 		if (i % 2)
36 			checksum += buf[i] << 8;
37 		else
38 			checksum += buf[i];
39 	}
40 	checksum = ~checksum;
41 
42 	return checksum;
43 }
44 
45 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
46 {
47 #define REQ_HDR_LEN 4 /* send header size */
48 #define ACK_HDR_LEN 3 /* rece header size */
49 #define CHECKSUM_LEN 2
50 #define USB_TIMEOUT 2000
51 	struct state *state = d_to_priv(d);
52 	struct usb_interface *intf = d->intf;
53 	int ret, wlen, rlen;
54 	u16 checksum, tmp_checksum;
55 
56 	mutex_lock(&d->usb_mutex);
57 
58 	/* buffer overflow check */
59 	if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
60 			req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
61 		dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
62 			req->wlen, req->rlen);
63 		ret = -EINVAL;
64 		goto exit;
65 	}
66 
67 	state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
68 	state->buf[1] = req->mbox;
69 	state->buf[2] = req->cmd;
70 	state->buf[3] = state->seq++;
71 	memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
72 
73 	wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
74 	rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
75 
76 	/* calc and add checksum */
77 	checksum = af9035_checksum(state->buf, state->buf[0] - 1);
78 	state->buf[state->buf[0] - 1] = (checksum >> 8);
79 	state->buf[state->buf[0] - 0] = (checksum & 0xff);
80 
81 	/* no ack for these packets */
82 	if (req->cmd == CMD_FW_DL)
83 		rlen = 0;
84 
85 	ret = dvb_usbv2_generic_rw_locked(d,
86 			state->buf, wlen, state->buf, rlen);
87 	if (ret)
88 		goto exit;
89 
90 	/* no ack for those packets */
91 	if (req->cmd == CMD_FW_DL)
92 		goto exit;
93 
94 	/* verify checksum */
95 	checksum = af9035_checksum(state->buf, rlen - 2);
96 	tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
97 	if (tmp_checksum != checksum) {
98 		dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
99 			req->cmd, tmp_checksum, checksum);
100 		ret = -EIO;
101 		goto exit;
102 	}
103 
104 	/* check status */
105 	if (state->buf[2]) {
106 		/* fw returns status 1 when IR code was not received */
107 		if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
108 			ret = 1;
109 			goto exit;
110 		}
111 
112 		dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
113 			req->cmd, state->buf[2]);
114 		ret = -EIO;
115 		goto exit;
116 	}
117 
118 	/* read request, copy returned data to return buf */
119 	if (req->rlen)
120 		memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
121 exit:
122 	mutex_unlock(&d->usb_mutex);
123 	if (ret < 0)
124 		dev_dbg(&intf->dev, "failed=%d\n", ret);
125 	return ret;
126 }
127 
128 /* write multiple registers */
129 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
130 {
131 	struct usb_interface *intf = d->intf;
132 	u8 wbuf[MAX_XFER_SIZE];
133 	u8 mbox = (reg >> 16) & 0xff;
134 	struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
135 
136 	if (6 + len > sizeof(wbuf)) {
137 		dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
138 		return -EOPNOTSUPP;
139 	}
140 
141 	wbuf[0] = len;
142 	wbuf[1] = 2;
143 	wbuf[2] = 0;
144 	wbuf[3] = 0;
145 	wbuf[4] = (reg >> 8) & 0xff;
146 	wbuf[5] = (reg >> 0) & 0xff;
147 	memcpy(&wbuf[6], val, len);
148 
149 	return af9035_ctrl_msg(d, &req);
150 }
151 
152 /* read multiple registers */
153 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
154 {
155 	u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
156 	u8 mbox = (reg >> 16) & 0xff;
157 	struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
158 
159 	return af9035_ctrl_msg(d, &req);
160 }
161 
162 /* write single register */
163 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
164 {
165 	return af9035_wr_regs(d, reg, &val, 1);
166 }
167 
168 /* read single register */
169 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
170 {
171 	return af9035_rd_regs(d, reg, val, 1);
172 }
173 
174 /* write single register with mask */
175 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
176 		u8 mask)
177 {
178 	int ret;
179 	u8 tmp;
180 
181 	/* no need for read if whole reg is written */
182 	if (mask != 0xff) {
183 		ret = af9035_rd_regs(d, reg, &tmp, 1);
184 		if (ret)
185 			return ret;
186 
187 		val &= mask;
188 		tmp &= ~mask;
189 		val |= tmp;
190 	}
191 
192 	return af9035_wr_regs(d, reg, &val, 1);
193 }
194 
195 static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
196 		u8 addr, void *platform_data, struct i2c_adapter *adapter)
197 {
198 	int ret, num;
199 	struct state *state = d_to_priv(d);
200 	struct usb_interface *intf = d->intf;
201 	struct i2c_client *client;
202 	struct i2c_board_info board_info = {
203 		.addr = addr,
204 		.platform_data = platform_data,
205 	};
206 
207 	strscpy(board_info.type, type, I2C_NAME_SIZE);
208 
209 	/* find first free client */
210 	for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
211 		if (state->i2c_client[num] == NULL)
212 			break;
213 	}
214 
215 	dev_dbg(&intf->dev, "num=%d\n", num);
216 
217 	if (num == AF9035_I2C_CLIENT_MAX) {
218 		dev_err(&intf->dev, "I2C client out of index\n");
219 		ret = -ENODEV;
220 		goto err;
221 	}
222 
223 	request_module("%s", board_info.type);
224 
225 	/* register I2C device */
226 	client = i2c_new_device(adapter, &board_info);
227 	if (client == NULL || client->dev.driver == NULL) {
228 		ret = -ENODEV;
229 		goto err;
230 	}
231 
232 	/* increase I2C driver usage count */
233 	if (!try_module_get(client->dev.driver->owner)) {
234 		i2c_unregister_device(client);
235 		ret = -ENODEV;
236 		goto err;
237 	}
238 
239 	state->i2c_client[num] = client;
240 	return 0;
241 err:
242 	dev_dbg(&intf->dev, "failed=%d\n", ret);
243 	return ret;
244 }
245 
246 static void af9035_del_i2c_dev(struct dvb_usb_device *d)
247 {
248 	int num;
249 	struct state *state = d_to_priv(d);
250 	struct usb_interface *intf = d->intf;
251 	struct i2c_client *client;
252 
253 	/* find last used client */
254 	num = AF9035_I2C_CLIENT_MAX;
255 	while (num--) {
256 		if (state->i2c_client[num] != NULL)
257 			break;
258 	}
259 
260 	dev_dbg(&intf->dev, "num=%d\n", num);
261 
262 	if (num == -1) {
263 		dev_err(&intf->dev, "I2C client out of index\n");
264 		goto err;
265 	}
266 
267 	client = state->i2c_client[num];
268 
269 	/* decrease I2C driver usage count */
270 	module_put(client->dev.driver->owner);
271 
272 	/* unregister I2C device */
273 	i2c_unregister_device(client);
274 
275 	state->i2c_client[num] = NULL;
276 	return;
277 err:
278 	dev_dbg(&intf->dev, "failed\n");
279 }
280 
281 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
282 		struct i2c_msg msg[], int num)
283 {
284 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
285 	struct state *state = d_to_priv(d);
286 	int ret;
287 
288 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
289 		return -EAGAIN;
290 
291 	/*
292 	 * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
293 	 * 0: data len
294 	 * 1: I2C addr << 1
295 	 * 2: reg addr len
296 	 *    byte 3 and 4 can be used as reg addr
297 	 * 3: reg addr MSB
298 	 *    used when reg addr len is set to 2
299 	 * 4: reg addr LSB
300 	 *    used when reg addr len is set to 1 or 2
301 	 *
302 	 * For the simplify we do not use register addr at all.
303 	 * NOTE: As a firmware knows tuner type there is very small possibility
304 	 * there could be some tuner I2C hacks done by firmware and this may
305 	 * lead problems if firmware expects those bytes are used.
306 	 *
307 	 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
308 	 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
309 	 * tuner devices, there is also external AF9033 demodulator connected
310 	 * via external I2C bus. All AF9033 demod I2C traffic, both single and
311 	 * dual tuner configuration, is covered by firmware - actual USB IO
312 	 * looks just like a memory access.
313 	 * In case of IT913x chip, there is own tuner driver. It is implemented
314 	 * currently as a I2C driver, even tuner IP block is likely build
315 	 * directly into the demodulator memory space and there is no own I2C
316 	 * bus. I2C subsystem does not allow register multiple devices to same
317 	 * bus, having same slave address. Due to that we reuse demod address,
318 	 * shifted by one bit, on that case.
319 	 *
320 	 * For IT930x we use a different command and the sub header is
321 	 * different as well:
322 	 * 0: data len
323 	 * 1: I2C bus (0x03 seems to be only value used)
324 	 * 2: I2C addr << 1
325 	 */
326 #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
327 	(_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
328 #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
329 	(_num == 1 && !(_msg[0].flags & I2C_M_RD))
330 #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
331 	(_num == 1 && (_msg[0].flags & I2C_M_RD))
332 
333 	if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
334 		if (msg[0].len > 40 || msg[1].len > 40) {
335 			/* TODO: correct limits > 40 */
336 			ret = -EOPNOTSUPP;
337 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
338 			   (msg[0].addr == state->af9033_i2c_addr[1])) {
339 			/* demod access via firmware interface */
340 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
341 					msg[0].buf[2];
342 
343 			if (msg[0].addr == state->af9033_i2c_addr[1])
344 				reg |= 0x100000;
345 
346 			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
347 					msg[1].len);
348 		} else if (state->no_read) {
349 			memset(msg[1].buf, 0, msg[1].len);
350 			ret = 0;
351 		} else {
352 			/* I2C write + read */
353 			u8 buf[MAX_XFER_SIZE];
354 			struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
355 					buf, msg[1].len, msg[1].buf };
356 
357 			if (state->chip_type == 0x9306) {
358 				req.cmd = CMD_GENERIC_I2C_RD;
359 				req.wlen = 3 + msg[0].len;
360 			}
361 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
362 
363 			buf[0] = msg[1].len;
364 			if (state->chip_type == 0x9306) {
365 				buf[1] = 0x03; /* I2C bus */
366 				buf[2] = msg[0].addr << 1;
367 				memcpy(&buf[3], msg[0].buf, msg[0].len);
368 			} else {
369 				buf[1] = msg[0].addr << 1;
370 				buf[3] = 0x00; /* reg addr MSB */
371 				buf[4] = 0x00; /* reg addr LSB */
372 
373 				/* Keep prev behavior for write req len > 2*/
374 				if (msg[0].len > 2) {
375 					buf[2] = 0x00; /* reg addr len */
376 					memcpy(&buf[5], msg[0].buf, msg[0].len);
377 
378 				/* Use reg addr fields if write req len <= 2 */
379 				} else {
380 					req.wlen = 5;
381 					buf[2] = msg[0].len;
382 					if (msg[0].len == 2) {
383 						buf[3] = msg[0].buf[0];
384 						buf[4] = msg[0].buf[1];
385 					} else if (msg[0].len == 1) {
386 						buf[4] = msg[0].buf[0];
387 					}
388 				}
389 			}
390 			ret = af9035_ctrl_msg(d, &req);
391 		}
392 	} else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
393 		if (msg[0].len > 40) {
394 			/* TODO: correct limits > 40 */
395 			ret = -EOPNOTSUPP;
396 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
397 			   (msg[0].addr == state->af9033_i2c_addr[1])) {
398 			/* demod access via firmware interface */
399 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
400 					msg[0].buf[2];
401 
402 			if (msg[0].addr == state->af9033_i2c_addr[1])
403 				reg |= 0x100000;
404 
405 			ret = (msg[0].len >= 3) ? af9035_wr_regs(d, reg,
406 							         &msg[0].buf[3],
407 							         msg[0].len - 3)
408 					        : -EOPNOTSUPP;
409 		} else {
410 			/* I2C write */
411 			u8 buf[MAX_XFER_SIZE];
412 			struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
413 					buf, 0, NULL };
414 
415 			if (state->chip_type == 0x9306) {
416 				req.cmd = CMD_GENERIC_I2C_WR;
417 				req.wlen = 3 + msg[0].len;
418 			}
419 
420 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
421 			buf[0] = msg[0].len;
422 			if (state->chip_type == 0x9306) {
423 				buf[1] = 0x03; /* I2C bus */
424 				buf[2] = msg[0].addr << 1;
425 				memcpy(&buf[3], msg[0].buf, msg[0].len);
426 			} else {
427 				buf[1] = msg[0].addr << 1;
428 				buf[2] = 0x00; /* reg addr len */
429 				buf[3] = 0x00; /* reg addr MSB */
430 				buf[4] = 0x00; /* reg addr LSB */
431 				memcpy(&buf[5], msg[0].buf, msg[0].len);
432 			}
433 			ret = af9035_ctrl_msg(d, &req);
434 		}
435 	} else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
436 		if (msg[0].len > 40) {
437 			/* TODO: correct limits > 40 */
438 			ret = -EOPNOTSUPP;
439 		} else if (state->no_read) {
440 			memset(msg[0].buf, 0, msg[0].len);
441 			ret = 0;
442 		} else {
443 			/* I2C read */
444 			u8 buf[5];
445 			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
446 						buf, msg[0].len, msg[0].buf };
447 
448 			if (state->chip_type == 0x9306) {
449 				req.cmd = CMD_GENERIC_I2C_RD;
450 				req.wlen = 3;
451 			}
452 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
453 			buf[0] = msg[0].len;
454 			if (state->chip_type == 0x9306) {
455 				buf[1] = 0x03; /* I2C bus */
456 				buf[2] = msg[0].addr << 1;
457 			} else {
458 				buf[1] = msg[0].addr << 1;
459 				buf[2] = 0x00; /* reg addr len */
460 				buf[3] = 0x00; /* reg addr MSB */
461 				buf[4] = 0x00; /* reg addr LSB */
462 			}
463 			ret = af9035_ctrl_msg(d, &req);
464 		}
465 	} else {
466 		/*
467 		 * We support only three kind of I2C transactions:
468 		 * 1) 1 x write + 1 x read (repeated start)
469 		 * 2) 1 x write
470 		 * 3) 1 x read
471 		 */
472 		ret = -EOPNOTSUPP;
473 	}
474 
475 	mutex_unlock(&d->i2c_mutex);
476 
477 	if (ret < 0)
478 		return ret;
479 	else
480 		return num;
481 }
482 
483 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
484 {
485 	return I2C_FUNC_I2C;
486 }
487 
488 static struct i2c_algorithm af9035_i2c_algo = {
489 	.master_xfer = af9035_i2c_master_xfer,
490 	.functionality = af9035_i2c_functionality,
491 };
492 
493 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
494 {
495 	struct state *state = d_to_priv(d);
496 	struct usb_interface *intf = d->intf;
497 	int ret, i, ts_mode_invalid;
498 	unsigned int utmp, eeprom_addr;
499 	u8 tmp;
500 	u8 wbuf[1] = { 1 };
501 	u8 rbuf[4];
502 	struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
503 			sizeof(rbuf), rbuf };
504 
505 	ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
506 	if (ret < 0)
507 		goto err;
508 
509 	state->chip_version = rbuf[0];
510 	state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
511 
512 	ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
513 	if (ret < 0)
514 		goto err;
515 
516 	dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
517 		 state->prechip_version, state->chip_version, state->chip_type);
518 
519 	if (state->chip_type == 0x9135) {
520 		if (state->chip_version == 0x02) {
521 			*name = AF9035_FIRMWARE_IT9135_V2;
522 			utmp = 0x00461d;
523 		} else {
524 			*name = AF9035_FIRMWARE_IT9135_V1;
525 			utmp = 0x00461b;
526 		}
527 
528 		/* Check if eeprom exists */
529 		ret = af9035_rd_reg(d, utmp, &tmp);
530 		if (ret < 0)
531 			goto err;
532 
533 		if (tmp == 0x00) {
534 			dev_dbg(&intf->dev, "no eeprom\n");
535 			state->no_eeprom = true;
536 			goto check_firmware_status;
537 		}
538 
539 		eeprom_addr = EEPROM_BASE_IT9135;
540 	} else if (state->chip_type == 0x9306) {
541 		*name = AF9035_FIRMWARE_IT9303;
542 		state->no_eeprom = true;
543 		goto check_firmware_status;
544 	} else {
545 		*name = AF9035_FIRMWARE_AF9035;
546 		eeprom_addr = EEPROM_BASE_AF9035;
547 	}
548 
549 	/* Read and store eeprom */
550 	for (i = 0; i < 256; i += 32) {
551 		ret = af9035_rd_regs(d, eeprom_addr + i, &state->eeprom[i], 32);
552 		if (ret < 0)
553 			goto err;
554 	}
555 
556 	dev_dbg(&intf->dev, "eeprom dump:\n");
557 	for (i = 0; i < 256; i += 16)
558 		dev_dbg(&intf->dev, "%*ph\n", 16, &state->eeprom[i]);
559 
560 	/* check for dual tuner mode */
561 	tmp = state->eeprom[EEPROM_TS_MODE];
562 	ts_mode_invalid = 0;
563 	switch (tmp) {
564 	case 0:
565 		break;
566 	case 1:
567 	case 3:
568 		state->dual_mode = true;
569 		break;
570 	case 5:
571 		if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
572 			state->dual_mode = true;	/* AF9035 */
573 		else
574 			ts_mode_invalid = 1;
575 		break;
576 	default:
577 		ts_mode_invalid = 1;
578 	}
579 
580 	dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);
581 
582 	if (ts_mode_invalid)
583 		dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);
584 
585 check_firmware_status:
586 	ret = af9035_ctrl_msg(d, &req);
587 	if (ret < 0)
588 		goto err;
589 
590 	dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
591 	if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
592 		ret = WARM;
593 	else
594 		ret = COLD;
595 
596 	return ret;
597 
598 err:
599 	dev_dbg(&intf->dev, "failed=%d\n", ret);
600 
601 	return ret;
602 }
603 
604 static int af9035_download_firmware_old(struct dvb_usb_device *d,
605 		const struct firmware *fw)
606 {
607 	struct usb_interface *intf = d->intf;
608 	int ret, i, j, len;
609 	u8 wbuf[1];
610 	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
611 	struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
612 	u8 hdr_core;
613 	u16 hdr_addr, hdr_data_len, hdr_checksum;
614 	#define MAX_DATA 58
615 	#define HDR_SIZE 7
616 
617 	/*
618 	 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
619 	 *
620 	 * byte 0: MCS 51 core
621 	 *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
622 	 *  address spaces
623 	 * byte 1-2: Big endian destination address
624 	 * byte 3-4: Big endian number of data bytes following the header
625 	 * byte 5-6: Big endian header checksum, apparently ignored by the chip
626 	 *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
627 	 */
628 
629 	for (i = fw->size; i > HDR_SIZE;) {
630 		hdr_core = fw->data[fw->size - i + 0];
631 		hdr_addr = fw->data[fw->size - i + 1] << 8;
632 		hdr_addr |= fw->data[fw->size - i + 2] << 0;
633 		hdr_data_len = fw->data[fw->size - i + 3] << 8;
634 		hdr_data_len |= fw->data[fw->size - i + 4] << 0;
635 		hdr_checksum = fw->data[fw->size - i + 5] << 8;
636 		hdr_checksum |= fw->data[fw->size - i + 6] << 0;
637 
638 		dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
639 			hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
640 
641 		if (((hdr_core != 1) && (hdr_core != 2)) ||
642 				(hdr_data_len > i)) {
643 			dev_dbg(&intf->dev, "bad firmware\n");
644 			break;
645 		}
646 
647 		/* download begin packet */
648 		req.cmd = CMD_FW_DL_BEGIN;
649 		ret = af9035_ctrl_msg(d, &req);
650 		if (ret < 0)
651 			goto err;
652 
653 		/* download firmware packet(s) */
654 		for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
655 			len = j;
656 			if (len > MAX_DATA)
657 				len = MAX_DATA;
658 			req_fw_dl.wlen = len;
659 			req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
660 					HDR_SIZE + hdr_data_len - j];
661 			ret = af9035_ctrl_msg(d, &req_fw_dl);
662 			if (ret < 0)
663 				goto err;
664 		}
665 
666 		/* download end packet */
667 		req.cmd = CMD_FW_DL_END;
668 		ret = af9035_ctrl_msg(d, &req);
669 		if (ret < 0)
670 			goto err;
671 
672 		i -= hdr_data_len + HDR_SIZE;
673 
674 		dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
675 	}
676 
677 	/* print warn if firmware is bad, continue and see what happens */
678 	if (i)
679 		dev_warn(&intf->dev, "bad firmware\n");
680 
681 	return 0;
682 
683 err:
684 	dev_dbg(&intf->dev, "failed=%d\n", ret);
685 
686 	return ret;
687 }
688 
689 static int af9035_download_firmware_new(struct dvb_usb_device *d,
690 		const struct firmware *fw)
691 {
692 	struct usb_interface *intf = d->intf;
693 	int ret, i, i_prev;
694 	struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
695 	#define HDR_SIZE 7
696 
697 	/*
698 	 * There seems to be following firmware header. Meaning of bytes 0-3
699 	 * is unknown.
700 	 *
701 	 * 0: 3
702 	 * 1: 0, 1
703 	 * 2: 0
704 	 * 3: 1, 2, 3
705 	 * 4: addr MSB
706 	 * 5: addr LSB
707 	 * 6: count of data bytes ?
708 	 */
709 	for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
710 		if (i == fw->size ||
711 				(fw->data[i + 0] == 0x03 &&
712 				(fw->data[i + 1] == 0x00 ||
713 				fw->data[i + 1] == 0x01) &&
714 				fw->data[i + 2] == 0x00)) {
715 			req_fw_dl.wlen = i - i_prev;
716 			req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
717 			i_prev = i;
718 			ret = af9035_ctrl_msg(d, &req_fw_dl);
719 			if (ret < 0)
720 				goto err;
721 
722 			dev_dbg(&intf->dev, "data uploaded=%d\n", i);
723 		}
724 	}
725 
726 	return 0;
727 
728 err:
729 	dev_dbg(&intf->dev, "failed=%d\n", ret);
730 
731 	return ret;
732 }
733 
734 static int af9035_download_firmware(struct dvb_usb_device *d,
735 		const struct firmware *fw)
736 {
737 	struct usb_interface *intf = d->intf;
738 	struct state *state = d_to_priv(d);
739 	int ret;
740 	u8 wbuf[1];
741 	u8 rbuf[4];
742 	u8 tmp;
743 	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
744 	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
745 
746 	dev_dbg(&intf->dev, "\n");
747 
748 	/*
749 	 * In case of dual tuner configuration we need to do some extra
750 	 * initialization in order to download firmware to slave demod too,
751 	 * which is done by master demod.
752 	 * Master feeds also clock and controls power via GPIO.
753 	 */
754 	if (state->dual_mode) {
755 		/* configure gpioh1, reset & power slave demod */
756 		ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
757 		if (ret < 0)
758 			goto err;
759 
760 		ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
761 		if (ret < 0)
762 			goto err;
763 
764 		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
765 		if (ret < 0)
766 			goto err;
767 
768 		usleep_range(10000, 50000);
769 
770 		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
771 		if (ret < 0)
772 			goto err;
773 
774 		/* tell the slave I2C address */
775 		tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
776 
777 		/* Use default I2C address if eeprom has no address set */
778 		if (!tmp)
779 			tmp = 0x1d << 1; /* 8-bit format used by chip */
780 
781 		if ((state->chip_type == 0x9135) ||
782 				(state->chip_type == 0x9306)) {
783 			ret = af9035_wr_reg(d, 0x004bfb, tmp);
784 			if (ret < 0)
785 				goto err;
786 		} else {
787 			ret = af9035_wr_reg(d, 0x00417f, tmp);
788 			if (ret < 0)
789 				goto err;
790 
791 			/* enable clock out */
792 			ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
793 			if (ret < 0)
794 				goto err;
795 		}
796 	}
797 
798 	if (fw->data[0] == 0x01)
799 		ret = af9035_download_firmware_old(d, fw);
800 	else
801 		ret = af9035_download_firmware_new(d, fw);
802 	if (ret < 0)
803 		goto err;
804 
805 	/* firmware loaded, request boot */
806 	req.cmd = CMD_FW_BOOT;
807 	ret = af9035_ctrl_msg(d, &req);
808 	if (ret < 0)
809 		goto err;
810 
811 	/* ensure firmware starts */
812 	wbuf[0] = 1;
813 	ret = af9035_ctrl_msg(d, &req_fw_ver);
814 	if (ret < 0)
815 		goto err;
816 
817 	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
818 		dev_err(&intf->dev, "firmware did not run\n");
819 		ret = -ENODEV;
820 		goto err;
821 	}
822 
823 	dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
824 		 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
825 
826 	return 0;
827 
828 err:
829 	dev_dbg(&intf->dev, "failed=%d\n", ret);
830 
831 	return ret;
832 }
833 
834 static int af9035_read_config(struct dvb_usb_device *d)
835 {
836 	struct usb_interface *intf = d->intf;
837 	struct state *state = d_to_priv(d);
838 	int ret, i;
839 	u8 tmp;
840 	u16 tmp16;
841 
842 	/* Demod I2C address */
843 	state->af9033_i2c_addr[0] = 0x1c;
844 	state->af9033_i2c_addr[1] = 0x1d;
845 	state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
846 	state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
847 	state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
848 	state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
849 
850 	if (state->chip_type == 0x9135) {
851 		/* feed clock for integrated RF tuner */
852 		state->af9033_config[0].dyn0_clk = true;
853 		state->af9033_config[1].dyn0_clk = true;
854 
855 		if (state->chip_version == 0x02) {
856 			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
857 			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
858 		} else {
859 			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
860 			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
861 		}
862 
863 		if (state->no_eeprom) {
864 			/* Remote controller to NEC polling by default */
865 			state->ir_mode = 0x05;
866 			state->ir_type = 0x00;
867 
868 			goto skip_eeprom;
869 		}
870 	} else if (state->chip_type == 0x9306) {
871 		/*
872 		 * IT930x is an USB bridge, only single demod-single tuner
873 		 * configurations seen so far.
874 		 */
875 		return 0;
876 	}
877 
878 	/* Remote controller */
879 	state->ir_mode = state->eeprom[EEPROM_IR_MODE];
880 	state->ir_type = state->eeprom[EEPROM_IR_TYPE];
881 
882 	if (state->dual_mode) {
883 		/* Read 2nd demodulator I2C address. 8-bit format on eeprom */
884 		tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
885 		if (tmp)
886 			state->af9033_i2c_addr[1] = tmp >> 1;
887 
888 		dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n",
889 			state->af9033_i2c_addr[1]);
890 	}
891 
892 	for (i = 0; i < state->dual_mode + 1; i++) {
893 		unsigned int eeprom_offset = 0;
894 
895 		/* tuner */
896 		tmp = state->eeprom[EEPROM_1_TUNER_ID + eeprom_offset];
897 		dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
898 
899 		/* tuner sanity check */
900 		if (state->chip_type == 0x9135) {
901 			if (state->chip_version == 0x02) {
902 				/* IT9135 BX (v2) */
903 				switch (tmp) {
904 				case AF9033_TUNER_IT9135_60:
905 				case AF9033_TUNER_IT9135_61:
906 				case AF9033_TUNER_IT9135_62:
907 					state->af9033_config[i].tuner = tmp;
908 					break;
909 				}
910 			} else {
911 				/* IT9135 AX (v1) */
912 				switch (tmp) {
913 				case AF9033_TUNER_IT9135_38:
914 				case AF9033_TUNER_IT9135_51:
915 				case AF9033_TUNER_IT9135_52:
916 					state->af9033_config[i].tuner = tmp;
917 					break;
918 				}
919 			}
920 		} else {
921 			/* AF9035 */
922 			state->af9033_config[i].tuner = tmp;
923 		}
924 
925 		if (state->af9033_config[i].tuner != tmp) {
926 			dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
927 				 i, tmp, state->af9033_config[i].tuner);
928 		}
929 
930 		switch (state->af9033_config[i].tuner) {
931 		case AF9033_TUNER_TUA9001:
932 		case AF9033_TUNER_FC0011:
933 		case AF9033_TUNER_MXL5007T:
934 		case AF9033_TUNER_TDA18218:
935 		case AF9033_TUNER_FC2580:
936 		case AF9033_TUNER_FC0012:
937 			state->af9033_config[i].spec_inv = 1;
938 			break;
939 		case AF9033_TUNER_IT9135_38:
940 		case AF9033_TUNER_IT9135_51:
941 		case AF9033_TUNER_IT9135_52:
942 		case AF9033_TUNER_IT9135_60:
943 		case AF9033_TUNER_IT9135_61:
944 		case AF9033_TUNER_IT9135_62:
945 			break;
946 		default:
947 			dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
948 				 tmp);
949 		}
950 
951 		/* disable dual mode if driver does not support it */
952 		if (i == 1)
953 			switch (state->af9033_config[i].tuner) {
954 			case AF9033_TUNER_FC0012:
955 			case AF9033_TUNER_IT9135_38:
956 			case AF9033_TUNER_IT9135_51:
957 			case AF9033_TUNER_IT9135_52:
958 			case AF9033_TUNER_IT9135_60:
959 			case AF9033_TUNER_IT9135_61:
960 			case AF9033_TUNER_IT9135_62:
961 			case AF9033_TUNER_MXL5007T:
962 				break;
963 			default:
964 				state->dual_mode = false;
965 				dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
966 		}
967 
968 		/* tuner IF frequency */
969 		tmp = state->eeprom[EEPROM_1_IF_L + eeprom_offset];
970 		tmp16 = tmp << 0;
971 		tmp = state->eeprom[EEPROM_1_IF_H + eeprom_offset];
972 		tmp16 |= tmp << 8;
973 		dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
974 
975 		eeprom_offset += 0x10; /* shift for the 2nd tuner params */
976 	}
977 
978 skip_eeprom:
979 	/* get demod clock */
980 	ret = af9035_rd_reg(d, 0x00d800, &tmp);
981 	if (ret < 0)
982 		goto err;
983 
984 	tmp = (tmp >> 0) & 0x0f;
985 
986 	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
987 		if (state->chip_type == 0x9135)
988 			state->af9033_config[i].clock = clock_lut_it9135[tmp];
989 		else
990 			state->af9033_config[i].clock = clock_lut_af9035[tmp];
991 	}
992 
993 	state->no_read = false;
994 	/* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
995 	if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
996 		le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)
997 
998 		switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
999 		case USB_PID_AVERMEDIA_A867:
1000 		case USB_PID_AVERMEDIA_TWINSTAR:
1001 			dev_info(&intf->dev,
1002 				 "Device may have issues with I2C read operations. Enabling fix.\n");
1003 			state->no_read = true;
1004 			break;
1005 		}
1006 
1007 	return 0;
1008 
1009 err:
1010 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1011 
1012 	return ret;
1013 }
1014 
1015 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
1016 		int cmd, int arg)
1017 {
1018 	struct usb_interface *intf = d->intf;
1019 	int ret;
1020 	u8 val;
1021 
1022 	dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
1023 
1024 	/*
1025 	 * CEN     always enabled by hardware wiring
1026 	 * RESETN  GPIOT3
1027 	 * RXEN    GPIOT2
1028 	 */
1029 
1030 	switch (cmd) {
1031 	case TUA9001_CMD_RESETN:
1032 		if (arg)
1033 			val = 0x00;
1034 		else
1035 			val = 0x01;
1036 
1037 		ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
1038 		if (ret < 0)
1039 			goto err;
1040 		break;
1041 	case TUA9001_CMD_RXEN:
1042 		if (arg)
1043 			val = 0x01;
1044 		else
1045 			val = 0x00;
1046 
1047 		ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
1048 		if (ret < 0)
1049 			goto err;
1050 		break;
1051 	}
1052 
1053 	return 0;
1054 
1055 err:
1056 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1057 
1058 	return ret;
1059 }
1060 
1061 
1062 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1063 		int cmd, int arg)
1064 {
1065 	struct usb_interface *intf = d->intf;
1066 	int ret;
1067 
1068 	switch (cmd) {
1069 	case FC0011_FE_CALLBACK_POWER:
1070 		/* Tuner enable */
1071 		ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
1072 		if (ret < 0)
1073 			goto err;
1074 
1075 		ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
1076 		if (ret < 0)
1077 			goto err;
1078 
1079 		ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
1080 		if (ret < 0)
1081 			goto err;
1082 
1083 		/* LED */
1084 		ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
1085 		if (ret < 0)
1086 			goto err;
1087 
1088 		ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
1089 		if (ret < 0)
1090 			goto err;
1091 
1092 		usleep_range(10000, 50000);
1093 		break;
1094 	case FC0011_FE_CALLBACK_RESET:
1095 		ret = af9035_wr_reg(d, 0xd8e9, 1);
1096 		if (ret < 0)
1097 			goto err;
1098 
1099 		ret = af9035_wr_reg(d, 0xd8e8, 1);
1100 		if (ret < 0)
1101 			goto err;
1102 
1103 		ret = af9035_wr_reg(d, 0xd8e7, 1);
1104 		if (ret < 0)
1105 			goto err;
1106 
1107 		usleep_range(10000, 20000);
1108 
1109 		ret = af9035_wr_reg(d, 0xd8e7, 0);
1110 		if (ret < 0)
1111 			goto err;
1112 
1113 		usleep_range(10000, 20000);
1114 		break;
1115 	default:
1116 		ret = -EINVAL;
1117 		goto err;
1118 	}
1119 
1120 	return 0;
1121 
1122 err:
1123 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1124 
1125 	return ret;
1126 }
1127 
1128 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1129 {
1130 	struct state *state = d_to_priv(d);
1131 
1132 	switch (state->af9033_config[0].tuner) {
1133 	case AF9033_TUNER_FC0011:
1134 		return af9035_fc0011_tuner_callback(d, cmd, arg);
1135 	case AF9033_TUNER_TUA9001:
1136 		return af9035_tua9001_tuner_callback(d, cmd, arg);
1137 	default:
1138 		break;
1139 	}
1140 
1141 	return 0;
1142 }
1143 
1144 static int af9035_frontend_callback(void *adapter_priv, int component,
1145 				    int cmd, int arg)
1146 {
1147 	struct i2c_adapter *adap = adapter_priv;
1148 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
1149 	struct usb_interface *intf = d->intf;
1150 
1151 	dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
1152 		component, cmd, arg);
1153 
1154 	switch (component) {
1155 	case DVB_FRONTEND_COMPONENT_TUNER:
1156 		return af9035_tuner_callback(d, cmd, arg);
1157 	default:
1158 		break;
1159 	}
1160 
1161 	return 0;
1162 }
1163 
1164 static int af9035_get_adapter_count(struct dvb_usb_device *d)
1165 {
1166 	struct state *state = d_to_priv(d);
1167 
1168 	return state->dual_mode + 1;
1169 }
1170 
1171 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1172 {
1173 	struct state *state = adap_to_priv(adap);
1174 	struct dvb_usb_device *d = adap_to_d(adap);
1175 	struct usb_interface *intf = d->intf;
1176 	int ret;
1177 
1178 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1179 
1180 	if (!state->af9033_config[adap->id].tuner) {
1181 		/* unsupported tuner */
1182 		ret = -ENODEV;
1183 		goto err;
1184 	}
1185 
1186 	state->af9033_config[adap->id].fe = &adap->fe[0];
1187 	state->af9033_config[adap->id].ops = &state->ops;
1188 	ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1189 			&state->af9033_config[adap->id], &d->i2c_adap);
1190 	if (ret)
1191 		goto err;
1192 
1193 	if (adap->fe[0] == NULL) {
1194 		ret = -ENODEV;
1195 		goto err;
1196 	}
1197 
1198 	/* disable I2C-gate */
1199 	adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1200 	adap->fe[0]->callback = af9035_frontend_callback;
1201 
1202 	return 0;
1203 
1204 err:
1205 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1206 
1207 	return ret;
1208 }
1209 
1210 static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1211 {
1212 	struct state *state = adap_to_priv(adap);
1213 	struct dvb_usb_device *d = adap_to_d(adap);
1214 	struct usb_interface *intf = d->intf;
1215 	int ret;
1216 	struct si2168_config si2168_config;
1217 	struct i2c_adapter *adapter;
1218 
1219 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1220 
1221 	memset(&si2168_config, 0, sizeof(si2168_config));
1222 	si2168_config.i2c_adapter = &adapter;
1223 	si2168_config.fe = &adap->fe[0];
1224 	si2168_config.ts_mode = SI2168_TS_SERIAL;
1225 
1226 	state->af9033_config[adap->id].fe = &adap->fe[0];
1227 	state->af9033_config[adap->id].ops = &state->ops;
1228 	ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config,
1229 				&d->i2c_adap);
1230 	if (ret)
1231 		goto err;
1232 
1233 	if (adap->fe[0] == NULL) {
1234 		ret = -ENODEV;
1235 		goto err;
1236 	}
1237 	state->i2c_adapter_demod = adapter;
1238 
1239 	return 0;
1240 
1241 err:
1242 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1243 
1244 	return ret;
1245 }
1246 
1247 static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1248 {
1249 	struct state *state = adap_to_priv(adap);
1250 	struct dvb_usb_device *d = adap_to_d(adap);
1251 	struct usb_interface *intf = d->intf;
1252 
1253 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1254 
1255 	if (adap->id == 1) {
1256 		if (state->i2c_client[1])
1257 			af9035_del_i2c_dev(d);
1258 	} else if (adap->id == 0) {
1259 		if (state->i2c_client[0])
1260 			af9035_del_i2c_dev(d);
1261 	}
1262 
1263 	return 0;
1264 }
1265 
1266 static const struct fc0011_config af9035_fc0011_config = {
1267 	.i2c_address = 0x60,
1268 };
1269 
1270 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1271 	{
1272 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1273 		.if_freq_hz = MxL_IF_4_57_MHZ,
1274 		.invert_if = 0,
1275 		.loop_thru_enable = 0,
1276 		.clk_out_enable = 0,
1277 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1278 	}, {
1279 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1280 		.if_freq_hz = MxL_IF_4_57_MHZ,
1281 		.invert_if = 0,
1282 		.loop_thru_enable = 1,
1283 		.clk_out_enable = 1,
1284 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1285 	}
1286 };
1287 
1288 static struct tda18218_config af9035_tda18218_config = {
1289 	.i2c_address = 0x60,
1290 	.i2c_wr_max = 21,
1291 };
1292 
1293 static const struct fc0012_config af9035_fc0012_config[] = {
1294 	{
1295 		.i2c_address = 0x63,
1296 		.xtal_freq = FC_XTAL_36_MHZ,
1297 		.dual_master = true,
1298 		.loop_through = true,
1299 		.clock_out = true,
1300 	}, {
1301 		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1302 		.xtal_freq = FC_XTAL_36_MHZ,
1303 		.dual_master = true,
1304 	}
1305 };
1306 
1307 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1308 {
1309 	struct state *state = adap_to_priv(adap);
1310 	struct dvb_usb_device *d = adap_to_d(adap);
1311 	struct usb_interface *intf = d->intf;
1312 	int ret;
1313 	struct dvb_frontend *fe;
1314 	struct i2c_msg msg[1];
1315 	u8 tuner_addr;
1316 
1317 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1318 
1319 	/*
1320 	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1321 	 * to carry info about used I2C bus for dual tuner configuration.
1322 	 */
1323 
1324 	switch (state->af9033_config[adap->id].tuner) {
1325 	case AF9033_TUNER_TUA9001: {
1326 		struct tua9001_platform_data tua9001_pdata = {
1327 			.dvb_frontend = adap->fe[0],
1328 		};
1329 
1330 		/*
1331 		 * AF9035 gpiot3 = TUA9001 RESETN
1332 		 * AF9035 gpiot2 = TUA9001 RXEN
1333 		 */
1334 
1335 		/* configure gpiot2 and gpiot2 as output */
1336 		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1337 		if (ret < 0)
1338 			goto err;
1339 
1340 		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1341 		if (ret < 0)
1342 			goto err;
1343 
1344 		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1345 		if (ret < 0)
1346 			goto err;
1347 
1348 		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1349 		if (ret < 0)
1350 			goto err;
1351 
1352 		/* attach tuner */
1353 		ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
1354 					 &d->i2c_adap);
1355 		if (ret)
1356 			goto err;
1357 
1358 		fe = adap->fe[0];
1359 		break;
1360 	}
1361 	case AF9033_TUNER_FC0011:
1362 		fe = dvb_attach(fc0011_attach, adap->fe[0],
1363 				&d->i2c_adap, &af9035_fc0011_config);
1364 		break;
1365 	case AF9033_TUNER_MXL5007T:
1366 		if (adap->id == 0) {
1367 			ret = af9035_wr_reg(d, 0x00d8e0, 1);
1368 			if (ret < 0)
1369 				goto err;
1370 
1371 			ret = af9035_wr_reg(d, 0x00d8e1, 1);
1372 			if (ret < 0)
1373 				goto err;
1374 
1375 			ret = af9035_wr_reg(d, 0x00d8df, 0);
1376 			if (ret < 0)
1377 				goto err;
1378 
1379 			msleep(30);
1380 
1381 			ret = af9035_wr_reg(d, 0x00d8df, 1);
1382 			if (ret < 0)
1383 				goto err;
1384 
1385 			msleep(300);
1386 
1387 			ret = af9035_wr_reg(d, 0x00d8c0, 1);
1388 			if (ret < 0)
1389 				goto err;
1390 
1391 			ret = af9035_wr_reg(d, 0x00d8c1, 1);
1392 			if (ret < 0)
1393 				goto err;
1394 
1395 			ret = af9035_wr_reg(d, 0x00d8bf, 0);
1396 			if (ret < 0)
1397 				goto err;
1398 
1399 			ret = af9035_wr_reg(d, 0x00d8b4, 1);
1400 			if (ret < 0)
1401 				goto err;
1402 
1403 			ret = af9035_wr_reg(d, 0x00d8b5, 1);
1404 			if (ret < 0)
1405 				goto err;
1406 
1407 			ret = af9035_wr_reg(d, 0x00d8b3, 1);
1408 			if (ret < 0)
1409 				goto err;
1410 
1411 			tuner_addr = 0x60;
1412 		} else {
1413 			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1414 		}
1415 
1416 		/* attach tuner */
1417 		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1418 				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1419 		break;
1420 	case AF9033_TUNER_TDA18218:
1421 		/* attach tuner */
1422 		fe = dvb_attach(tda18218_attach, adap->fe[0],
1423 				&d->i2c_adap, &af9035_tda18218_config);
1424 		break;
1425 	case AF9033_TUNER_FC2580: {
1426 		struct fc2580_platform_data fc2580_pdata = {
1427 			.dvb_frontend = adap->fe[0],
1428 		};
1429 
1430 		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1431 		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1432 		if (ret < 0)
1433 			goto err;
1434 
1435 		ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1436 		if (ret < 0)
1437 			goto err;
1438 
1439 		ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1440 		if (ret < 0)
1441 			goto err;
1442 
1443 		usleep_range(10000, 50000);
1444 		/* attach tuner */
1445 		ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
1446 					 &d->i2c_adap);
1447 		if (ret)
1448 			goto err;
1449 
1450 		fe = adap->fe[0];
1451 		break;
1452 	}
1453 	case AF9033_TUNER_FC0012:
1454 		/*
1455 		 * AF9035 gpiot2 = FC0012 enable
1456 		 * XXX: there seems to be something on gpioh8 too, but on my
1457 		 * my test I didn't find any difference.
1458 		 */
1459 
1460 		if (adap->id == 0) {
1461 			/* configure gpiot2 as output and high */
1462 			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1463 			if (ret < 0)
1464 				goto err;
1465 
1466 			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1467 			if (ret < 0)
1468 				goto err;
1469 
1470 			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1471 			if (ret < 0)
1472 				goto err;
1473 		} else {
1474 			/*
1475 			 * FIXME: That belongs for the FC0012 driver.
1476 			 * Write 02 to FC0012 master tuner register 0d directly
1477 			 * in order to make slave tuner working.
1478 			 */
1479 			msg[0].addr = 0x63;
1480 			msg[0].flags = 0;
1481 			msg[0].len = 2;
1482 			msg[0].buf = "\x0d\x02";
1483 			ret = i2c_transfer(&d->i2c_adap, msg, 1);
1484 			if (ret < 0)
1485 				goto err;
1486 		}
1487 
1488 		usleep_range(10000, 50000);
1489 
1490 		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1491 				&af9035_fc0012_config[adap->id]);
1492 		break;
1493 	case AF9033_TUNER_IT9135_38:
1494 	case AF9033_TUNER_IT9135_51:
1495 	case AF9033_TUNER_IT9135_52:
1496 	case AF9033_TUNER_IT9135_60:
1497 	case AF9033_TUNER_IT9135_61:
1498 	case AF9033_TUNER_IT9135_62:
1499 	{
1500 		struct platform_device *pdev;
1501 		const char *name;
1502 		struct it913x_platform_data it913x_pdata = {
1503 			.regmap = state->af9033_config[adap->id].regmap,
1504 			.fe = adap->fe[0],
1505 		};
1506 
1507 		switch (state->af9033_config[adap->id].tuner) {
1508 		case AF9033_TUNER_IT9135_38:
1509 		case AF9033_TUNER_IT9135_51:
1510 		case AF9033_TUNER_IT9135_52:
1511 			name = "it9133ax-tuner";
1512 			break;
1513 		case AF9033_TUNER_IT9135_60:
1514 		case AF9033_TUNER_IT9135_61:
1515 		case AF9033_TUNER_IT9135_62:
1516 			name = "it9133bx-tuner";
1517 			break;
1518 		default:
1519 			ret = -ENODEV;
1520 			goto err;
1521 		}
1522 
1523 		if (state->dual_mode) {
1524 			if (adap->id == 0)
1525 				it913x_pdata.role = IT913X_ROLE_DUAL_MASTER;
1526 			else
1527 				it913x_pdata.role = IT913X_ROLE_DUAL_SLAVE;
1528 		} else {
1529 			it913x_pdata.role = IT913X_ROLE_SINGLE;
1530 		}
1531 
1532 		request_module("%s", "it913x");
1533 		pdev = platform_device_register_data(&d->intf->dev, name,
1534 						     PLATFORM_DEVID_AUTO,
1535 						     &it913x_pdata,
1536 						     sizeof(it913x_pdata));
1537 		if (IS_ERR(pdev) || !pdev->dev.driver) {
1538 			ret = -ENODEV;
1539 			goto err;
1540 		}
1541 		if (!try_module_get(pdev->dev.driver->owner)) {
1542 			platform_device_unregister(pdev);
1543 			ret = -ENODEV;
1544 			goto err;
1545 		}
1546 
1547 		state->platform_device_tuner[adap->id] = pdev;
1548 		fe = adap->fe[0];
1549 		break;
1550 	}
1551 	default:
1552 		fe = NULL;
1553 	}
1554 
1555 	if (fe == NULL) {
1556 		ret = -ENODEV;
1557 		goto err;
1558 	}
1559 
1560 	return 0;
1561 
1562 err:
1563 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1564 
1565 	return ret;
1566 }
1567 
1568 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1569 {
1570 	struct state *state = adap_to_priv(adap);
1571 	struct dvb_usb_device *d = adap_to_d(adap);
1572 	struct usb_interface *intf = d->intf;
1573 	int ret;
1574 	struct si2157_config si2157_config;
1575 
1576 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1577 
1578 	/* I2C master bus 2 clock speed 300k */
1579 	ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1580 	if (ret < 0)
1581 		goto err;
1582 
1583 	/* I2C master bus 1,3 clock speed 300k */
1584 	ret = af9035_wr_reg(d, 0x00f103, 0x07);
1585 	if (ret < 0)
1586 		goto err;
1587 
1588 	/* set gpio11 low */
1589 	ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1590 	if (ret < 0)
1591 		goto err;
1592 
1593 	ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1594 	if (ret < 0)
1595 		goto err;
1596 
1597 	ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1598 	if (ret < 0)
1599 		goto err;
1600 
1601 	/* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1602 	ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1603 	if (ret < 0)
1604 		goto err;
1605 
1606 	ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1607 	if (ret < 0)
1608 		goto err;
1609 
1610 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1611 	if (ret < 0)
1612 		goto err;
1613 
1614 	msleep(200);
1615 
1616 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1617 	if (ret < 0)
1618 		goto err;
1619 
1620 	memset(&si2157_config, 0, sizeof(si2157_config));
1621 	si2157_config.fe = adap->fe[0];
1622 	si2157_config.if_port = 1;
1623 	ret = af9035_add_i2c_dev(d, "si2157", 0x63,
1624 			&si2157_config, state->i2c_adapter_demod);
1625 
1626 	if (ret)
1627 		goto err;
1628 
1629 	return 0;
1630 
1631 err:
1632 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1633 
1634 	return ret;
1635 }
1636 
1637 
1638 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1639 {
1640 	struct state *state = adap_to_priv(adap);
1641 	struct dvb_usb_device *d = adap_to_d(adap);
1642 	struct usb_interface *intf = d->intf;
1643 
1644 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1645 
1646 	if (adap->id == 1) {
1647 		if (state->i2c_client[3])
1648 			af9035_del_i2c_dev(d);
1649 	} else if (adap->id == 0) {
1650 		if (state->i2c_client[1])
1651 			af9035_del_i2c_dev(d);
1652 	}
1653 
1654 	return 0;
1655 }
1656 
1657 
1658 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1659 {
1660 	struct state *state = adap_to_priv(adap);
1661 	struct dvb_usb_device *d = adap_to_d(adap);
1662 	struct usb_interface *intf = d->intf;
1663 
1664 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1665 
1666 	switch (state->af9033_config[adap->id].tuner) {
1667 	case AF9033_TUNER_TUA9001:
1668 	case AF9033_TUNER_FC2580:
1669 		if (adap->id == 1) {
1670 			if (state->i2c_client[3])
1671 				af9035_del_i2c_dev(d);
1672 		} else if (adap->id == 0) {
1673 			if (state->i2c_client[1])
1674 				af9035_del_i2c_dev(d);
1675 		}
1676 		break;
1677 	case AF9033_TUNER_IT9135_38:
1678 	case AF9033_TUNER_IT9135_51:
1679 	case AF9033_TUNER_IT9135_52:
1680 	case AF9033_TUNER_IT9135_60:
1681 	case AF9033_TUNER_IT9135_61:
1682 	case AF9033_TUNER_IT9135_62:
1683 	{
1684 		struct platform_device *pdev;
1685 
1686 		pdev = state->platform_device_tuner[adap->id];
1687 		if (pdev) {
1688 			module_put(pdev->dev.driver->owner);
1689 			platform_device_unregister(pdev);
1690 		}
1691 		break;
1692 	}
1693 	}
1694 
1695 	return 0;
1696 }
1697 
1698 static int af9035_init(struct dvb_usb_device *d)
1699 {
1700 	struct state *state = d_to_priv(d);
1701 	struct usb_interface *intf = d->intf;
1702 	int ret, i;
1703 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1704 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1705 	struct reg_val_mask tab[] = {
1706 		{ 0x80f99d, 0x01, 0x01 },
1707 		{ 0x80f9a4, 0x01, 0x01 },
1708 		{ 0x00dd11, 0x00, 0x20 },
1709 		{ 0x00dd11, 0x00, 0x40 },
1710 		{ 0x00dd13, 0x00, 0x20 },
1711 		{ 0x00dd13, 0x00, 0x40 },
1712 		{ 0x00dd11, 0x20, 0x20 },
1713 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1714 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1715 		{ 0x00dd0c, packet_size, 0xff},
1716 		{ 0x00dd11, state->dual_mode << 6, 0x40 },
1717 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1718 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1719 		{ 0x00dd0d, packet_size, 0xff },
1720 		{ 0x80f9a3, state->dual_mode, 0x01 },
1721 		{ 0x80f9cd, state->dual_mode, 0x01 },
1722 		{ 0x80f99d, 0x00, 0x01 },
1723 		{ 0x80f9a4, 0x00, 0x01 },
1724 	};
1725 
1726 	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1727 		d->udev->speed, frame_size, packet_size);
1728 
1729 	/* init endpoints */
1730 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1731 		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1732 				tab[i].mask);
1733 		if (ret < 0)
1734 			goto err;
1735 	}
1736 
1737 	return 0;
1738 
1739 err:
1740 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1741 
1742 	return ret;
1743 }
1744 
1745 static int it930x_init(struct dvb_usb_device *d)
1746 {
1747 	struct state *state = d_to_priv(d);
1748 	struct usb_interface *intf = d->intf;
1749 	int ret, i;
1750 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1751 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1752 	struct reg_val_mask tab[] = {
1753 		{ 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1754 		{ 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1755 		{ 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1756 		{ 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1757 		{ 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1758 		{ 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1759 		{ 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1760 		{ 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1761 		{ 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1762 		{ 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1763 		{ 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1764 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1765 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1766 		{ 0x00dd0c, packet_size, 0xff},
1767 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1768 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1769 		{ 0x00dd0d, packet_size, 0xff },
1770 		{ 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1771 		{ 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1772 		{ 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1773 		{ 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1774 		{ 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1775 
1776 		/* suspend gpio1 for TS-C */
1777 		{ 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1778 		{ 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1779 		{ 0x00d8af, 0x00, 0xff }, /* gpio1 */
1780 
1781 		/* suspend gpio7 for TS-D */
1782 		{ 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1783 		{ 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1784 		{ 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1785 
1786 		/* suspend gpio13 for TS-B */
1787 		{ 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1788 		{ 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1789 		{ 0x00d8db, 0x00, 0xff }, /* gpio13 */
1790 
1791 		/* suspend gpio14 for TS-E */
1792 		{ 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1793 		{ 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1794 		{ 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1795 
1796 		/* suspend gpio15 for TS-A */
1797 		{ 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1798 		{ 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1799 		{ 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1800 
1801 		{ 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1802 		{ 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1803 		{ 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1804 		{ 0x00da4c, 0x01, 0xff }, /* ts0_en */
1805 		{ 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1806 	};
1807 
1808 	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1809 		d->udev->speed, frame_size, packet_size);
1810 
1811 	/* init endpoints */
1812 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1813 		ret = af9035_wr_reg_mask(d, tab[i].reg,
1814 				tab[i].val, tab[i].mask);
1815 
1816 		if (ret < 0)
1817 			goto err;
1818 	}
1819 
1820 	return 0;
1821 err:
1822 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1823 
1824 	return ret;
1825 }
1826 
1827 
1828 #if IS_ENABLED(CONFIG_RC_CORE)
1829 static int af9035_rc_query(struct dvb_usb_device *d)
1830 {
1831 	struct usb_interface *intf = d->intf;
1832 	int ret;
1833 	enum rc_proto proto;
1834 	u32 key;
1835 	u8 buf[4];
1836 	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1837 
1838 	ret = af9035_ctrl_msg(d, &req);
1839 	if (ret == 1)
1840 		return 0;
1841 	else if (ret < 0)
1842 		goto err;
1843 
1844 	if ((buf[2] + buf[3]) == 0xff) {
1845 		if ((buf[0] + buf[1]) == 0xff) {
1846 			/* NEC standard 16bit */
1847 			key = RC_SCANCODE_NEC(buf[0], buf[2]);
1848 			proto = RC_PROTO_NEC;
1849 		} else {
1850 			/* NEC extended 24bit */
1851 			key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1852 			proto = RC_PROTO_NECX;
1853 		}
1854 	} else {
1855 		/* NEC full code 32bit */
1856 		key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1857 					buf[2] << 8  | buf[3]);
1858 		proto = RC_PROTO_NEC32;
1859 	}
1860 
1861 	dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1862 
1863 	rc_keydown(d->rc_dev, proto, key, 0);
1864 
1865 	return 0;
1866 
1867 err:
1868 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1869 
1870 	return ret;
1871 }
1872 
1873 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1874 {
1875 	struct state *state = d_to_priv(d);
1876 	struct usb_interface *intf = d->intf;
1877 
1878 	dev_dbg(&intf->dev, "ir_mode=%02x ir_type=%02x\n",
1879 		state->ir_mode, state->ir_type);
1880 
1881 	/* don't activate rc if in HID mode or if not available */
1882 	if (state->ir_mode == 0x05) {
1883 		switch (state->ir_type) {
1884 		case 0: /* NEC */
1885 		default:
1886 			rc->allowed_protos = RC_PROTO_BIT_NEC |
1887 					RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32;
1888 			break;
1889 		case 1: /* RC6 */
1890 			rc->allowed_protos = RC_PROTO_BIT_RC6_MCE;
1891 			break;
1892 		}
1893 
1894 		rc->query = af9035_rc_query;
1895 		rc->interval = 500;
1896 
1897 		/* load empty to enable rc */
1898 		if (!rc->map_name)
1899 			rc->map_name = RC_MAP_EMPTY;
1900 	}
1901 
1902 	return 0;
1903 }
1904 #else
1905 	#define af9035_get_rc_config NULL
1906 #endif
1907 
1908 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1909 		struct usb_data_stream_properties *stream)
1910 {
1911 	struct dvb_usb_device *d = fe_to_d(fe);
1912 	struct usb_interface *intf = d->intf;
1913 
1914 	dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1915 
1916 	if (d->udev->speed == USB_SPEED_FULL)
1917 		stream->u.bulk.buffersize = 5 * 188;
1918 
1919 	return 0;
1920 }
1921 
1922 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1923 {
1924 	struct state *state = adap_to_priv(adap);
1925 
1926 	return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1927 }
1928 
1929 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1930 		int onoff)
1931 {
1932 	struct state *state = adap_to_priv(adap);
1933 
1934 	return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1935 }
1936 
1937 static int af9035_probe(struct usb_interface *intf,
1938 		const struct usb_device_id *id)
1939 {
1940 	struct usb_device *udev = interface_to_usbdev(intf);
1941 	char manufacturer[sizeof("Afatech")];
1942 
1943 	memset(manufacturer, 0, sizeof(manufacturer));
1944 	usb_string(udev, udev->descriptor.iManufacturer,
1945 			manufacturer, sizeof(manufacturer));
1946 	/*
1947 	 * There is two devices having same ID but different chipset. One uses
1948 	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1949 	 * is iManufacturer string.
1950 	 *
1951 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1952 	 * idProduct          0x0099
1953 	 * bcdDevice            2.00
1954 	 * iManufacturer           1 Afatech
1955 	 * iProduct                2 DVB-T 2
1956 	 *
1957 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1958 	 * idProduct          0x0099
1959 	 * bcdDevice            2.00
1960 	 * iManufacturer           1 ITE Technologies, Inc.
1961 	 * iProduct                2 DVB-T TV Stick
1962 	 */
1963 	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1964 			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1965 		if (!strcmp("Afatech", manufacturer)) {
1966 			dev_dbg(&udev->dev, "rejecting device\n");
1967 			return -ENODEV;
1968 		}
1969 	}
1970 
1971 	return dvb_usbv2_probe(intf, id);
1972 }
1973 
1974 /* interface 0 is used by DVB-T receiver and
1975    interface 1 is for remote controller (HID) */
1976 static const struct dvb_usb_device_properties af9035_props = {
1977 	.driver_name = KBUILD_MODNAME,
1978 	.owner = THIS_MODULE,
1979 	.adapter_nr = adapter_nr,
1980 	.size_of_priv = sizeof(struct state),
1981 
1982 	.generic_bulk_ctrl_endpoint = 0x02,
1983 	.generic_bulk_ctrl_endpoint_response = 0x81,
1984 
1985 	.identify_state = af9035_identify_state,
1986 	.download_firmware = af9035_download_firmware,
1987 
1988 	.i2c_algo = &af9035_i2c_algo,
1989 	.read_config = af9035_read_config,
1990 	.frontend_attach = af9035_frontend_attach,
1991 	.frontend_detach = af9035_frontend_detach,
1992 	.tuner_attach = af9035_tuner_attach,
1993 	.tuner_detach = af9035_tuner_detach,
1994 	.init = af9035_init,
1995 	.get_rc_config = af9035_get_rc_config,
1996 	.get_stream_config = af9035_get_stream_config,
1997 
1998 	.get_adapter_count = af9035_get_adapter_count,
1999 	.adapter = {
2000 		{
2001 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
2002 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2003 
2004 			.pid_filter_count = 32,
2005 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
2006 			.pid_filter = af9035_pid_filter,
2007 
2008 			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
2009 		}, {
2010 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
2011 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2012 
2013 			.pid_filter_count = 32,
2014 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
2015 			.pid_filter = af9035_pid_filter,
2016 
2017 			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
2018 		},
2019 	},
2020 };
2021 
2022 static const struct dvb_usb_device_properties it930x_props = {
2023 	.driver_name = KBUILD_MODNAME,
2024 	.owner = THIS_MODULE,
2025 	.adapter_nr = adapter_nr,
2026 	.size_of_priv = sizeof(struct state),
2027 
2028 	.generic_bulk_ctrl_endpoint = 0x02,
2029 	.generic_bulk_ctrl_endpoint_response = 0x81,
2030 
2031 	.identify_state = af9035_identify_state,
2032 	.download_firmware = af9035_download_firmware,
2033 
2034 	.i2c_algo = &af9035_i2c_algo,
2035 	.read_config = af9035_read_config,
2036 	.frontend_attach = it930x_frontend_attach,
2037 	.frontend_detach = af9035_frontend_detach,
2038 	.tuner_attach = it930x_tuner_attach,
2039 	.tuner_detach = it930x_tuner_detach,
2040 	.init = it930x_init,
2041 	.get_stream_config = af9035_get_stream_config,
2042 
2043 	.get_adapter_count = af9035_get_adapter_count,
2044 	.adapter = {
2045 		{
2046 			.stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
2047 		}, {
2048 			.stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
2049 		},
2050 	},
2051 };
2052 
2053 static const struct usb_device_id af9035_id_table[] = {
2054 	/* AF9035 devices */
2055 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
2056 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2057 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2058 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2059 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2060 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2061 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2062 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2063 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2064 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2065 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2066 		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
2067 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2068 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2069 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2070 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2071 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2072 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2073 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2074 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2075 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2076 		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2077 	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2078 		&af9035_props, "Asus U3100Mini Plus", NULL) },
2079 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2080 		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2081 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
2082 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2083        { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_EVOLVEO_XTRATV_STICK,
2084 	       &af9035_props, "EVOLVEO XtraTV stick", NULL) },
2085 
2086 	/* IT9135 devices */
2087 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2088 		&af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2089 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2090 		&af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2091 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2092 		&af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2093 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2094 		&af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2095 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2096 		&af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2097 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2098 		&af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2099 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2100 		&af9035_props, "Avermedia A835B(4835)",	RC_MAP_IT913X_V2) },
2101 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
2102 		&af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2103 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2104 		&af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2105 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2106 		&af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2107 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2108 		&af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2109 							RC_MAP_IT913X_V1) },
2110 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2111 		&af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2112 							RC_MAP_IT913X_V1) },
2113 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_T1,
2114 		&af9035_props, "TerraTec T1", RC_MAP_IT913X_V1) },
2115 	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2116 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2117 		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2118 		NULL) },
2119 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2120 		&af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2121 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2122 		&af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2123 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2124 		&af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2125 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2126 		&af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2127 
2128 	/* IT930x devices */
2129 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2130 		&it930x_props, "ITE 9303 Generic", NULL) },
2131 	{ }
2132 };
2133 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2134 
2135 static struct usb_driver af9035_usb_driver = {
2136 	.name = KBUILD_MODNAME,
2137 	.id_table = af9035_id_table,
2138 	.probe = af9035_probe,
2139 	.disconnect = dvb_usbv2_disconnect,
2140 	.suspend = dvb_usbv2_suspend,
2141 	.resume = dvb_usbv2_resume,
2142 	.reset_resume = dvb_usbv2_reset_resume,
2143 	.no_dynamic_id = 1,
2144 	.soft_unbind = 1,
2145 };
2146 
2147 module_usb_driver(af9035_usb_driver);
2148 
2149 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2150 MODULE_DESCRIPTION("Afatech AF9035 driver");
2151 MODULE_LICENSE("GPL");
2152 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2153 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2154 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2155 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);
2156