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