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