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