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