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 	if (ret < 0)
111 		dev_dbg(&intf->dev, "failed=%d\n", ret);
112 	return ret;
113 }
114 
115 /* write multiple registers */
116 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
117 {
118 	struct usb_interface *intf = d->intf;
119 	u8 wbuf[MAX_XFER_SIZE];
120 	u8 mbox = (reg >> 16) & 0xff;
121 	struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
122 
123 	if (6 + len > sizeof(wbuf)) {
124 		dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
125 		return -EOPNOTSUPP;
126 	}
127 
128 	wbuf[0] = len;
129 	wbuf[1] = 2;
130 	wbuf[2] = 0;
131 	wbuf[3] = 0;
132 	wbuf[4] = (reg >> 8) & 0xff;
133 	wbuf[5] = (reg >> 0) & 0xff;
134 	memcpy(&wbuf[6], val, len);
135 
136 	return af9035_ctrl_msg(d, &req);
137 }
138 
139 /* read multiple registers */
140 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
141 {
142 	u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
143 	u8 mbox = (reg >> 16) & 0xff;
144 	struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
145 
146 	return af9035_ctrl_msg(d, &req);
147 }
148 
149 /* write single register */
150 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
151 {
152 	return af9035_wr_regs(d, reg, &val, 1);
153 }
154 
155 /* read single register */
156 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
157 {
158 	return af9035_rd_regs(d, reg, val, 1);
159 }
160 
161 /* write single register with mask */
162 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
163 		u8 mask)
164 {
165 	int ret;
166 	u8 tmp;
167 
168 	/* no need for read if whole reg is written */
169 	if (mask != 0xff) {
170 		ret = af9035_rd_regs(d, reg, &tmp, 1);
171 		if (ret)
172 			return ret;
173 
174 		val &= mask;
175 		tmp &= ~mask;
176 		val |= tmp;
177 	}
178 
179 	return af9035_wr_regs(d, reg, &val, 1);
180 }
181 
182 static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
183 		u8 addr, void *platform_data, struct i2c_adapter *adapter)
184 {
185 	int ret, num;
186 	struct state *state = d_to_priv(d);
187 	struct usb_interface *intf = d->intf;
188 	struct i2c_client *client;
189 	struct i2c_board_info board_info = {
190 		.addr = addr,
191 		.platform_data = platform_data,
192 	};
193 
194 	strscpy(board_info.type, type, I2C_NAME_SIZE);
195 
196 	/* find first free client */
197 	for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
198 		if (state->i2c_client[num] == NULL)
199 			break;
200 	}
201 
202 	dev_dbg(&intf->dev, "num=%d\n", num);
203 
204 	if (num == AF9035_I2C_CLIENT_MAX) {
205 		dev_err(&intf->dev, "I2C client out of index\n");
206 		ret = -ENODEV;
207 		goto err;
208 	}
209 
210 	request_module("%s", board_info.type);
211 
212 	/* register I2C device */
213 	client = i2c_new_device(adapter, &board_info);
214 	if (client == NULL || client->dev.driver == NULL) {
215 		ret = -ENODEV;
216 		goto err;
217 	}
218 
219 	/* increase I2C driver usage count */
220 	if (!try_module_get(client->dev.driver->owner)) {
221 		i2c_unregister_device(client);
222 		ret = -ENODEV;
223 		goto err;
224 	}
225 
226 	state->i2c_client[num] = client;
227 	return 0;
228 err:
229 	dev_dbg(&intf->dev, "failed=%d\n", ret);
230 	return ret;
231 }
232 
233 static void af9035_del_i2c_dev(struct dvb_usb_device *d)
234 {
235 	int num;
236 	struct state *state = d_to_priv(d);
237 	struct usb_interface *intf = d->intf;
238 	struct i2c_client *client;
239 
240 	/* find last used client */
241 	num = AF9035_I2C_CLIENT_MAX;
242 	while (num--) {
243 		if (state->i2c_client[num] != NULL)
244 			break;
245 	}
246 
247 	dev_dbg(&intf->dev, "num=%d\n", num);
248 
249 	if (num == -1) {
250 		dev_err(&intf->dev, "I2C client out of index\n");
251 		goto err;
252 	}
253 
254 	client = state->i2c_client[num];
255 
256 	/* decrease I2C driver usage count */
257 	module_put(client->dev.driver->owner);
258 
259 	/* unregister I2C device */
260 	i2c_unregister_device(client);
261 
262 	state->i2c_client[num] = NULL;
263 	return;
264 err:
265 	dev_dbg(&intf->dev, "failed\n");
266 }
267 
268 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
269 		struct i2c_msg msg[], int num)
270 {
271 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
272 	struct state *state = d_to_priv(d);
273 	int ret;
274 
275 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
276 		return -EAGAIN;
277 
278 	/*
279 	 * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
280 	 * 0: data len
281 	 * 1: I2C addr << 1
282 	 * 2: reg addr len
283 	 *    byte 3 and 4 can be used as reg addr
284 	 * 3: reg addr MSB
285 	 *    used when reg addr len is set to 2
286 	 * 4: reg addr LSB
287 	 *    used when reg addr len is set to 1 or 2
288 	 *
289 	 * For the simplify we do not use register addr at all.
290 	 * NOTE: As a firmware knows tuner type there is very small possibility
291 	 * there could be some tuner I2C hacks done by firmware and this may
292 	 * lead problems if firmware expects those bytes are used.
293 	 *
294 	 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
295 	 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
296 	 * tuner devices, there is also external AF9033 demodulator connected
297 	 * via external I2C bus. All AF9033 demod I2C traffic, both single and
298 	 * dual tuner configuration, is covered by firmware - actual USB IO
299 	 * looks just like a memory access.
300 	 * In case of IT913x chip, there is own tuner driver. It is implemented
301 	 * currently as a I2C driver, even tuner IP block is likely build
302 	 * directly into the demodulator memory space and there is no own I2C
303 	 * bus. I2C subsystem does not allow register multiple devices to same
304 	 * bus, having same slave address. Due to that we reuse demod address,
305 	 * shifted by one bit, on that case.
306 	 *
307 	 * For IT930x we use a different command and the sub header is
308 	 * different as well:
309 	 * 0: data len
310 	 * 1: I2C bus (0x03 seems to be only value used)
311 	 * 2: I2C addr << 1
312 	 */
313 #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
314 	(_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
315 #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
316 	(_num == 1 && !(_msg[0].flags & I2C_M_RD))
317 #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
318 	(_num == 1 && (_msg[0].flags & I2C_M_RD))
319 
320 	if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
321 		if (msg[0].len > 40 || msg[1].len > 40) {
322 			/* TODO: correct limits > 40 */
323 			ret = -EOPNOTSUPP;
324 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
325 			   (msg[0].addr == state->af9033_i2c_addr[1])) {
326 			/* demod access via firmware interface */
327 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
328 					msg[0].buf[2];
329 
330 			if (msg[0].addr == state->af9033_i2c_addr[1])
331 				reg |= 0x100000;
332 
333 			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
334 					msg[1].len);
335 		} else if (state->no_read) {
336 			memset(msg[1].buf, 0, msg[1].len);
337 			ret = 0;
338 		} else {
339 			/* I2C write + read */
340 			u8 buf[MAX_XFER_SIZE];
341 			struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
342 					buf, msg[1].len, msg[1].buf };
343 
344 			if (state->chip_type == 0x9306) {
345 				req.cmd = CMD_GENERIC_I2C_RD;
346 				req.wlen = 3 + msg[0].len;
347 			}
348 			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
349 
350 			buf[0] = msg[1].len;
351 			if (state->chip_type == 0x9306) {
352 				buf[1] = 0x03; /* I2C bus */
353 				buf[2] = msg[0].addr << 1;
354 				memcpy(&buf[3], msg[0].buf, msg[0].len);
355 			} else {
356 				buf[1] = msg[0].addr << 1;
357 				buf[3] = 0x00; /* reg addr MSB */
358 				buf[4] = 0x00; /* reg addr LSB */
359 
360 				/* Keep prev behavior for write req len > 2*/
361 				if (msg[0].len > 2) {
362 					buf[2] = 0x00; /* reg addr len */
363 					memcpy(&buf[5], msg[0].buf, msg[0].len);
364 
365 				/* Use reg addr fields if write req len <= 2 */
366 				} else {
367 					req.wlen = 5;
368 					buf[2] = msg[0].len;
369 					if (msg[0].len == 2) {
370 						buf[3] = msg[0].buf[0];
371 						buf[4] = msg[0].buf[1];
372 					} else if (msg[0].len == 1) {
373 						buf[4] = msg[0].buf[0];
374 					}
375 				}
376 			}
377 			ret = af9035_ctrl_msg(d, &req);
378 		}
379 	} else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
380 		if (msg[0].len > 40) {
381 			/* TODO: correct limits > 40 */
382 			ret = -EOPNOTSUPP;
383 		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
384 			   (msg[0].addr == state->af9033_i2c_addr[1])) {
385 			/* demod access via firmware interface */
386 			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
387 					msg[0].buf[2];
388 
389 			if (msg[0].addr == state->af9033_i2c_addr[1])
390 				reg |= 0x100000;
391 
392 			ret = (msg[0].len >= 3) ? af9035_wr_regs(d, reg,
393 							         &msg[0].buf[3],
394 							         msg[0].len - 3)
395 					        : -EOPNOTSUPP;
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 static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1203 {
1204 	struct state *state = adap_to_priv(adap);
1205 	struct dvb_usb_device *d = adap_to_d(adap);
1206 	struct usb_interface *intf = d->intf;
1207 	int ret;
1208 	struct si2168_config si2168_config;
1209 	struct i2c_adapter *adapter;
1210 
1211 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1212 
1213 	/* I2C master bus 2 clock speed 300k */
1214 	ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1215 	if (ret < 0)
1216 		goto err;
1217 
1218 	/* I2C master bus 1,3 clock speed 300k */
1219 	ret = af9035_wr_reg(d, 0x00f103, 0x07);
1220 	if (ret < 0)
1221 		goto err;
1222 
1223 	/* set gpio11 low */
1224 	ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1225 	if (ret < 0)
1226 		goto err;
1227 
1228 	ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1229 	if (ret < 0)
1230 		goto err;
1231 
1232 	ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1233 	if (ret < 0)
1234 		goto err;
1235 
1236 	/* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1237 	ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1238 	if (ret < 0)
1239 		goto err;
1240 
1241 	ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1242 	if (ret < 0)
1243 		goto err;
1244 
1245 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1246 	if (ret < 0)
1247 		goto err;
1248 
1249 	msleep(200);
1250 
1251 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1252 	if (ret < 0)
1253 		goto err;
1254 
1255 	memset(&si2168_config, 0, sizeof(si2168_config));
1256 	si2168_config.i2c_adapter = &adapter;
1257 	si2168_config.fe = &adap->fe[0];
1258 	si2168_config.ts_mode = SI2168_TS_SERIAL;
1259 
1260 	state->af9033_config[adap->id].fe = &adap->fe[0];
1261 	state->af9033_config[adap->id].ops = &state->ops;
1262 	ret = af9035_add_i2c_dev(d, "si2168",
1263 				 it930x_addresses_table[state->it930x_addresses].frontend_i2c_addr,
1264 				 &si2168_config, &d->i2c_adap);
1265 	if (ret)
1266 		goto err;
1267 
1268 	if (adap->fe[0] == NULL) {
1269 		ret = -ENODEV;
1270 		goto err;
1271 	}
1272 	state->i2c_adapter_demod = adapter;
1273 
1274 	return 0;
1275 
1276 err:
1277 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1278 
1279 	return ret;
1280 }
1281 
1282 static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1283 {
1284 	struct state *state = adap_to_priv(adap);
1285 	struct dvb_usb_device *d = adap_to_d(adap);
1286 	struct usb_interface *intf = d->intf;
1287 
1288 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1289 
1290 	if (adap->id == 1) {
1291 		if (state->i2c_client[1])
1292 			af9035_del_i2c_dev(d);
1293 	} else if (adap->id == 0) {
1294 		if (state->i2c_client[0])
1295 			af9035_del_i2c_dev(d);
1296 	}
1297 
1298 	return 0;
1299 }
1300 
1301 static const struct fc0011_config af9035_fc0011_config = {
1302 	.i2c_address = 0x60,
1303 };
1304 
1305 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1306 	{
1307 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1308 		.if_freq_hz = MxL_IF_4_57_MHZ,
1309 		.invert_if = 0,
1310 		.loop_thru_enable = 0,
1311 		.clk_out_enable = 0,
1312 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1313 	}, {
1314 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1315 		.if_freq_hz = MxL_IF_4_57_MHZ,
1316 		.invert_if = 0,
1317 		.loop_thru_enable = 1,
1318 		.clk_out_enable = 1,
1319 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1320 	}
1321 };
1322 
1323 static struct tda18218_config af9035_tda18218_config = {
1324 	.i2c_address = 0x60,
1325 	.i2c_wr_max = 21,
1326 };
1327 
1328 static const struct fc0012_config af9035_fc0012_config[] = {
1329 	{
1330 		.i2c_address = 0x63,
1331 		.xtal_freq = FC_XTAL_36_MHZ,
1332 		.dual_master = true,
1333 		.loop_through = true,
1334 		.clock_out = true,
1335 	}, {
1336 		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1337 		.xtal_freq = FC_XTAL_36_MHZ,
1338 		.dual_master = true,
1339 	}
1340 };
1341 
1342 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1343 {
1344 	struct state *state = adap_to_priv(adap);
1345 	struct dvb_usb_device *d = adap_to_d(adap);
1346 	struct usb_interface *intf = d->intf;
1347 	int ret;
1348 	struct dvb_frontend *fe;
1349 	struct i2c_msg msg[1];
1350 	u8 tuner_addr;
1351 
1352 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1353 
1354 	/*
1355 	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1356 	 * to carry info about used I2C bus for dual tuner configuration.
1357 	 */
1358 
1359 	switch (state->af9033_config[adap->id].tuner) {
1360 	case AF9033_TUNER_TUA9001: {
1361 		struct tua9001_platform_data tua9001_pdata = {
1362 			.dvb_frontend = adap->fe[0],
1363 		};
1364 
1365 		/*
1366 		 * AF9035 gpiot3 = TUA9001 RESETN
1367 		 * AF9035 gpiot2 = TUA9001 RXEN
1368 		 */
1369 
1370 		/* configure gpiot2 and gpiot2 as output */
1371 		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1372 		if (ret < 0)
1373 			goto err;
1374 
1375 		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1376 		if (ret < 0)
1377 			goto err;
1378 
1379 		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1380 		if (ret < 0)
1381 			goto err;
1382 
1383 		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1384 		if (ret < 0)
1385 			goto err;
1386 
1387 		/* attach tuner */
1388 		ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
1389 					 &d->i2c_adap);
1390 		if (ret)
1391 			goto err;
1392 
1393 		fe = adap->fe[0];
1394 		break;
1395 	}
1396 	case AF9033_TUNER_FC0011:
1397 		fe = dvb_attach(fc0011_attach, adap->fe[0],
1398 				&d->i2c_adap, &af9035_fc0011_config);
1399 		break;
1400 	case AF9033_TUNER_MXL5007T:
1401 		if (adap->id == 0) {
1402 			ret = af9035_wr_reg(d, 0x00d8e0, 1);
1403 			if (ret < 0)
1404 				goto err;
1405 
1406 			ret = af9035_wr_reg(d, 0x00d8e1, 1);
1407 			if (ret < 0)
1408 				goto err;
1409 
1410 			ret = af9035_wr_reg(d, 0x00d8df, 0);
1411 			if (ret < 0)
1412 				goto err;
1413 
1414 			msleep(30);
1415 
1416 			ret = af9035_wr_reg(d, 0x00d8df, 1);
1417 			if (ret < 0)
1418 				goto err;
1419 
1420 			msleep(300);
1421 
1422 			ret = af9035_wr_reg(d, 0x00d8c0, 1);
1423 			if (ret < 0)
1424 				goto err;
1425 
1426 			ret = af9035_wr_reg(d, 0x00d8c1, 1);
1427 			if (ret < 0)
1428 				goto err;
1429 
1430 			ret = af9035_wr_reg(d, 0x00d8bf, 0);
1431 			if (ret < 0)
1432 				goto err;
1433 
1434 			ret = af9035_wr_reg(d, 0x00d8b4, 1);
1435 			if (ret < 0)
1436 				goto err;
1437 
1438 			ret = af9035_wr_reg(d, 0x00d8b5, 1);
1439 			if (ret < 0)
1440 				goto err;
1441 
1442 			ret = af9035_wr_reg(d, 0x00d8b3, 1);
1443 			if (ret < 0)
1444 				goto err;
1445 
1446 			tuner_addr = 0x60;
1447 		} else {
1448 			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1449 		}
1450 
1451 		/* attach tuner */
1452 		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1453 				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1454 		break;
1455 	case AF9033_TUNER_TDA18218:
1456 		/* attach tuner */
1457 		fe = dvb_attach(tda18218_attach, adap->fe[0],
1458 				&d->i2c_adap, &af9035_tda18218_config);
1459 		break;
1460 	case AF9033_TUNER_FC2580: {
1461 		struct fc2580_platform_data fc2580_pdata = {
1462 			.dvb_frontend = adap->fe[0],
1463 		};
1464 
1465 		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1466 		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1467 		if (ret < 0)
1468 			goto err;
1469 
1470 		ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1471 		if (ret < 0)
1472 			goto err;
1473 
1474 		ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1475 		if (ret < 0)
1476 			goto err;
1477 
1478 		usleep_range(10000, 50000);
1479 		/* attach tuner */
1480 		ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
1481 					 &d->i2c_adap);
1482 		if (ret)
1483 			goto err;
1484 
1485 		fe = adap->fe[0];
1486 		break;
1487 	}
1488 	case AF9033_TUNER_FC0012:
1489 		/*
1490 		 * AF9035 gpiot2 = FC0012 enable
1491 		 * XXX: there seems to be something on gpioh8 too, but on my
1492 		 * my test I didn't find any difference.
1493 		 */
1494 
1495 		if (adap->id == 0) {
1496 			/* configure gpiot2 as output and high */
1497 			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1498 			if (ret < 0)
1499 				goto err;
1500 
1501 			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1502 			if (ret < 0)
1503 				goto err;
1504 
1505 			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1506 			if (ret < 0)
1507 				goto err;
1508 		} else {
1509 			/*
1510 			 * FIXME: That belongs for the FC0012 driver.
1511 			 * Write 02 to FC0012 master tuner register 0d directly
1512 			 * in order to make slave tuner working.
1513 			 */
1514 			msg[0].addr = 0x63;
1515 			msg[0].flags = 0;
1516 			msg[0].len = 2;
1517 			msg[0].buf = "\x0d\x02";
1518 			ret = i2c_transfer(&d->i2c_adap, msg, 1);
1519 			if (ret < 0)
1520 				goto err;
1521 		}
1522 
1523 		usleep_range(10000, 50000);
1524 
1525 		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1526 				&af9035_fc0012_config[adap->id]);
1527 		break;
1528 	case AF9033_TUNER_IT9135_38:
1529 	case AF9033_TUNER_IT9135_51:
1530 	case AF9033_TUNER_IT9135_52:
1531 	case AF9033_TUNER_IT9135_60:
1532 	case AF9033_TUNER_IT9135_61:
1533 	case AF9033_TUNER_IT9135_62:
1534 	{
1535 		struct platform_device *pdev;
1536 		const char *name;
1537 		struct it913x_platform_data it913x_pdata = {
1538 			.regmap = state->af9033_config[adap->id].regmap,
1539 			.fe = adap->fe[0],
1540 		};
1541 
1542 		switch (state->af9033_config[adap->id].tuner) {
1543 		case AF9033_TUNER_IT9135_38:
1544 		case AF9033_TUNER_IT9135_51:
1545 		case AF9033_TUNER_IT9135_52:
1546 			name = "it9133ax-tuner";
1547 			break;
1548 		case AF9033_TUNER_IT9135_60:
1549 		case AF9033_TUNER_IT9135_61:
1550 		case AF9033_TUNER_IT9135_62:
1551 			name = "it9133bx-tuner";
1552 			break;
1553 		default:
1554 			ret = -ENODEV;
1555 			goto err;
1556 		}
1557 
1558 		if (state->dual_mode) {
1559 			if (adap->id == 0)
1560 				it913x_pdata.role = IT913X_ROLE_DUAL_MASTER;
1561 			else
1562 				it913x_pdata.role = IT913X_ROLE_DUAL_SLAVE;
1563 		} else {
1564 			it913x_pdata.role = IT913X_ROLE_SINGLE;
1565 		}
1566 
1567 		request_module("%s", "it913x");
1568 		pdev = platform_device_register_data(&d->intf->dev, name,
1569 						     PLATFORM_DEVID_AUTO,
1570 						     &it913x_pdata,
1571 						     sizeof(it913x_pdata));
1572 		if (IS_ERR(pdev) || !pdev->dev.driver) {
1573 			ret = -ENODEV;
1574 			goto err;
1575 		}
1576 		if (!try_module_get(pdev->dev.driver->owner)) {
1577 			platform_device_unregister(pdev);
1578 			ret = -ENODEV;
1579 			goto err;
1580 		}
1581 
1582 		state->platform_device_tuner[adap->id] = pdev;
1583 		fe = adap->fe[0];
1584 		break;
1585 	}
1586 	default:
1587 		fe = NULL;
1588 	}
1589 
1590 	if (fe == NULL) {
1591 		ret = -ENODEV;
1592 		goto err;
1593 	}
1594 
1595 	return 0;
1596 
1597 err:
1598 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1599 
1600 	return ret;
1601 }
1602 
1603 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1604 {
1605 	struct state *state = adap_to_priv(adap);
1606 	struct dvb_usb_device *d = adap_to_d(adap);
1607 	struct usb_interface *intf = d->intf;
1608 	int ret;
1609 	struct si2157_config si2157_config;
1610 
1611 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1612 
1613 	memset(&si2157_config, 0, sizeof(si2157_config));
1614 	si2157_config.fe = adap->fe[0];
1615 	si2157_config.if_port = it930x_addresses_table[state->it930x_addresses].tuner_if_port;
1616 	ret = af9035_add_i2c_dev(d, "si2157",
1617 				 it930x_addresses_table[state->it930x_addresses].tuner_i2c_addr,
1618 				 &si2157_config, state->i2c_adapter_demod);
1619 	if (ret)
1620 		goto err;
1621 
1622 	return 0;
1623 
1624 err:
1625 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1626 
1627 	return ret;
1628 }
1629 
1630 
1631 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1632 {
1633 	struct state *state = adap_to_priv(adap);
1634 	struct dvb_usb_device *d = adap_to_d(adap);
1635 	struct usb_interface *intf = d->intf;
1636 
1637 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1638 
1639 	if (adap->id == 1) {
1640 		if (state->i2c_client[3])
1641 			af9035_del_i2c_dev(d);
1642 	} else if (adap->id == 0) {
1643 		if (state->i2c_client[1])
1644 			af9035_del_i2c_dev(d);
1645 	}
1646 
1647 	return 0;
1648 }
1649 
1650 
1651 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1652 {
1653 	struct state *state = adap_to_priv(adap);
1654 	struct dvb_usb_device *d = adap_to_d(adap);
1655 	struct usb_interface *intf = d->intf;
1656 
1657 	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1658 
1659 	switch (state->af9033_config[adap->id].tuner) {
1660 	case AF9033_TUNER_TUA9001:
1661 	case AF9033_TUNER_FC2580:
1662 		if (adap->id == 1) {
1663 			if (state->i2c_client[3])
1664 				af9035_del_i2c_dev(d);
1665 		} else if (adap->id == 0) {
1666 			if (state->i2c_client[1])
1667 				af9035_del_i2c_dev(d);
1668 		}
1669 		break;
1670 	case AF9033_TUNER_IT9135_38:
1671 	case AF9033_TUNER_IT9135_51:
1672 	case AF9033_TUNER_IT9135_52:
1673 	case AF9033_TUNER_IT9135_60:
1674 	case AF9033_TUNER_IT9135_61:
1675 	case AF9033_TUNER_IT9135_62:
1676 	{
1677 		struct platform_device *pdev;
1678 
1679 		pdev = state->platform_device_tuner[adap->id];
1680 		if (pdev) {
1681 			module_put(pdev->dev.driver->owner);
1682 			platform_device_unregister(pdev);
1683 		}
1684 		break;
1685 	}
1686 	}
1687 
1688 	return 0;
1689 }
1690 
1691 static int af9035_init(struct dvb_usb_device *d)
1692 {
1693 	struct state *state = d_to_priv(d);
1694 	struct usb_interface *intf = d->intf;
1695 	int ret, i;
1696 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1697 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1698 	struct reg_val_mask tab[] = {
1699 		{ 0x80f99d, 0x01, 0x01 },
1700 		{ 0x80f9a4, 0x01, 0x01 },
1701 		{ 0x00dd11, 0x00, 0x20 },
1702 		{ 0x00dd11, 0x00, 0x40 },
1703 		{ 0x00dd13, 0x00, 0x20 },
1704 		{ 0x00dd13, 0x00, 0x40 },
1705 		{ 0x00dd11, 0x20, 0x20 },
1706 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1707 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1708 		{ 0x00dd0c, packet_size, 0xff},
1709 		{ 0x00dd11, state->dual_mode << 6, 0x40 },
1710 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1711 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1712 		{ 0x00dd0d, packet_size, 0xff },
1713 		{ 0x80f9a3, state->dual_mode, 0x01 },
1714 		{ 0x80f9cd, state->dual_mode, 0x01 },
1715 		{ 0x80f99d, 0x00, 0x01 },
1716 		{ 0x80f9a4, 0x00, 0x01 },
1717 	};
1718 
1719 	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1720 		d->udev->speed, frame_size, packet_size);
1721 
1722 	/* init endpoints */
1723 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1724 		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1725 				tab[i].mask);
1726 		if (ret < 0)
1727 			goto err;
1728 	}
1729 
1730 	return 0;
1731 
1732 err:
1733 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1734 
1735 	return ret;
1736 }
1737 
1738 static int it930x_init(struct dvb_usb_device *d)
1739 {
1740 	struct state *state = d_to_priv(d);
1741 	struct usb_interface *intf = d->intf;
1742 	int ret, i;
1743 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1744 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1745 	struct reg_val_mask tab[] = {
1746 		{ 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1747 		{ 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1748 		{ 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1749 		{ 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1750 		{ 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1751 		{ 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1752 		{ 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1753 		{ 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1754 		{ 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1755 		{ 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1756 		{ 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1757 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1758 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1759 		{ 0x00dd0c, packet_size, 0xff},
1760 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1761 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1762 		{ 0x00dd0d, packet_size, 0xff },
1763 		{ 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1764 		{ 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1765 		{ 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1766 		{ 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1767 		{ 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1768 
1769 		/* suspend gpio1 for TS-C */
1770 		{ 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1771 		{ 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1772 		{ 0x00d8af, 0x00, 0xff }, /* gpio1 */
1773 
1774 		/* suspend gpio7 for TS-D */
1775 		{ 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1776 		{ 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1777 		{ 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1778 
1779 		/* suspend gpio13 for TS-B */
1780 		{ 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1781 		{ 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1782 		{ 0x00d8db, 0x00, 0xff }, /* gpio13 */
1783 
1784 		/* suspend gpio14 for TS-E */
1785 		{ 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1786 		{ 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1787 		{ 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1788 
1789 		/* suspend gpio15 for TS-A */
1790 		{ 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1791 		{ 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1792 		{ 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1793 
1794 		{ 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1795 		{ 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1796 		{ 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1797 		{ 0x00da4c, 0x01, 0xff }, /* ts0_en */
1798 		{ 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1799 	};
1800 
1801 	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1802 		d->udev->speed, frame_size, packet_size);
1803 
1804 	/* init endpoints */
1805 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1806 		ret = af9035_wr_reg_mask(d, tab[i].reg,
1807 				tab[i].val, tab[i].mask);
1808 
1809 		if (ret < 0)
1810 			goto err;
1811 	}
1812 
1813 	return 0;
1814 err:
1815 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1816 
1817 	return ret;
1818 }
1819 
1820 
1821 #if IS_ENABLED(CONFIG_RC_CORE)
1822 static int af9035_rc_query(struct dvb_usb_device *d)
1823 {
1824 	struct usb_interface *intf = d->intf;
1825 	int ret;
1826 	enum rc_proto proto;
1827 	u32 key;
1828 	u8 buf[4];
1829 	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1830 
1831 	ret = af9035_ctrl_msg(d, &req);
1832 	if (ret == 1)
1833 		return 0;
1834 	else if (ret < 0)
1835 		goto err;
1836 
1837 	if ((buf[2] + buf[3]) == 0xff) {
1838 		if ((buf[0] + buf[1]) == 0xff) {
1839 			/* NEC standard 16bit */
1840 			key = RC_SCANCODE_NEC(buf[0], buf[2]);
1841 			proto = RC_PROTO_NEC;
1842 		} else {
1843 			/* NEC extended 24bit */
1844 			key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1845 			proto = RC_PROTO_NECX;
1846 		}
1847 	} else {
1848 		/* NEC full code 32bit */
1849 		key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1850 					buf[2] << 8  | buf[3]);
1851 		proto = RC_PROTO_NEC32;
1852 	}
1853 
1854 	dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1855 
1856 	rc_keydown(d->rc_dev, proto, key, 0);
1857 
1858 	return 0;
1859 
1860 err:
1861 	dev_dbg(&intf->dev, "failed=%d\n", ret);
1862 
1863 	return ret;
1864 }
1865 
1866 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1867 {
1868 	struct state *state = d_to_priv(d);
1869 	struct usb_interface *intf = d->intf;
1870 
1871 	dev_dbg(&intf->dev, "ir_mode=%02x ir_type=%02x\n",
1872 		state->ir_mode, state->ir_type);
1873 
1874 	/* don't activate rc if in HID mode or if not available */
1875 	if (state->ir_mode == 0x05) {
1876 		switch (state->ir_type) {
1877 		case 0: /* NEC */
1878 		default:
1879 			rc->allowed_protos = RC_PROTO_BIT_NEC |
1880 					RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32;
1881 			break;
1882 		case 1: /* RC6 */
1883 			rc->allowed_protos = RC_PROTO_BIT_RC6_MCE;
1884 			break;
1885 		}
1886 
1887 		rc->query = af9035_rc_query;
1888 		rc->interval = 500;
1889 
1890 		/* load empty to enable rc */
1891 		if (!rc->map_name)
1892 			rc->map_name = RC_MAP_EMPTY;
1893 	}
1894 
1895 	return 0;
1896 }
1897 #else
1898 	#define af9035_get_rc_config NULL
1899 #endif
1900 
1901 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1902 		struct usb_data_stream_properties *stream)
1903 {
1904 	struct dvb_usb_device *d = fe_to_d(fe);
1905 	struct usb_interface *intf = d->intf;
1906 
1907 	dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1908 
1909 	if (d->udev->speed == USB_SPEED_FULL)
1910 		stream->u.bulk.buffersize = 5 * 188;
1911 
1912 	return 0;
1913 }
1914 
1915 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1916 {
1917 	struct state *state = adap_to_priv(adap);
1918 
1919 	return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1920 }
1921 
1922 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1923 		int onoff)
1924 {
1925 	struct state *state = adap_to_priv(adap);
1926 
1927 	return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1928 }
1929 
1930 static int af9035_probe(struct usb_interface *intf,
1931 		const struct usb_device_id *id)
1932 {
1933 	struct usb_device *udev = interface_to_usbdev(intf);
1934 	char manufacturer[sizeof("Afatech")];
1935 
1936 	memset(manufacturer, 0, sizeof(manufacturer));
1937 	usb_string(udev, udev->descriptor.iManufacturer,
1938 			manufacturer, sizeof(manufacturer));
1939 	/*
1940 	 * There is two devices having same ID but different chipset. One uses
1941 	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1942 	 * is iManufacturer string.
1943 	 *
1944 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1945 	 * idProduct          0x0099
1946 	 * bcdDevice            2.00
1947 	 * iManufacturer           1 Afatech
1948 	 * iProduct                2 DVB-T 2
1949 	 *
1950 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1951 	 * idProduct          0x0099
1952 	 * bcdDevice            2.00
1953 	 * iManufacturer           1 ITE Technologies, Inc.
1954 	 * iProduct                2 DVB-T TV Stick
1955 	 */
1956 	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1957 			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1958 		if (!strcmp("Afatech", manufacturer)) {
1959 			dev_dbg(&udev->dev, "rejecting device\n");
1960 			return -ENODEV;
1961 		}
1962 	}
1963 
1964 	return dvb_usbv2_probe(intf, id);
1965 }
1966 
1967 /* interface 0 is used by DVB-T receiver and
1968    interface 1 is for remote controller (HID) */
1969 static const struct dvb_usb_device_properties af9035_props = {
1970 	.driver_name = KBUILD_MODNAME,
1971 	.owner = THIS_MODULE,
1972 	.adapter_nr = adapter_nr,
1973 	.size_of_priv = sizeof(struct state),
1974 
1975 	.generic_bulk_ctrl_endpoint = 0x02,
1976 	.generic_bulk_ctrl_endpoint_response = 0x81,
1977 
1978 	.identify_state = af9035_identify_state,
1979 	.download_firmware = af9035_download_firmware,
1980 
1981 	.i2c_algo = &af9035_i2c_algo,
1982 	.read_config = af9035_read_config,
1983 	.frontend_attach = af9035_frontend_attach,
1984 	.frontend_detach = af9035_frontend_detach,
1985 	.tuner_attach = af9035_tuner_attach,
1986 	.tuner_detach = af9035_tuner_detach,
1987 	.init = af9035_init,
1988 	.get_rc_config = af9035_get_rc_config,
1989 	.get_stream_config = af9035_get_stream_config,
1990 
1991 	.get_adapter_count = af9035_get_adapter_count,
1992 	.adapter = {
1993 		{
1994 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1995 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1996 
1997 			.pid_filter_count = 32,
1998 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
1999 			.pid_filter = af9035_pid_filter,
2000 
2001 			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
2002 		}, {
2003 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
2004 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2005 
2006 			.pid_filter_count = 32,
2007 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
2008 			.pid_filter = af9035_pid_filter,
2009 
2010 			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
2011 		},
2012 	},
2013 };
2014 
2015 static const struct dvb_usb_device_properties it930x_props = {
2016 	.driver_name = KBUILD_MODNAME,
2017 	.owner = THIS_MODULE,
2018 	.adapter_nr = adapter_nr,
2019 	.size_of_priv = sizeof(struct state),
2020 
2021 	.generic_bulk_ctrl_endpoint = 0x02,
2022 	.generic_bulk_ctrl_endpoint_response = 0x81,
2023 
2024 	.identify_state = af9035_identify_state,
2025 	.download_firmware = af9035_download_firmware,
2026 
2027 	.i2c_algo = &af9035_i2c_algo,
2028 	.read_config = af9035_read_config,
2029 	.frontend_attach = it930x_frontend_attach,
2030 	.frontend_detach = af9035_frontend_detach,
2031 	.tuner_attach = it930x_tuner_attach,
2032 	.tuner_detach = it930x_tuner_detach,
2033 	.init = it930x_init,
2034 	.get_stream_config = af9035_get_stream_config,
2035 
2036 	.get_adapter_count = af9035_get_adapter_count,
2037 	.adapter = {
2038 		{
2039 			.stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
2040 		}, {
2041 			.stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
2042 		},
2043 	},
2044 };
2045 
2046 static const struct usb_device_id af9035_id_table[] = {
2047 	/* AF9035 devices */
2048 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
2049 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2050 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2051 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2052 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2053 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2054 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2055 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2056 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2057 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2058 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2059 		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
2060 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2061 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2062 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2063 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2064 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2065 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2066 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2067 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2068 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2069 		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2070 	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2071 		&af9035_props, "Asus U3100Mini Plus", NULL) },
2072 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2073 		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2074 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
2075 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2076        { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_EVOLVEO_XTRATV_STICK,
2077 	       &af9035_props, "EVOLVEO XtraTV stick", NULL) },
2078 
2079 	/* IT9135 devices */
2080 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2081 		&af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2082 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2083 		&af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2084 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2085 		&af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2086 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2087 		&af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2088 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2089 		&af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2090 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2091 		&af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2092 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2093 		&af9035_props, "Avermedia A835B(4835)",	RC_MAP_IT913X_V2) },
2094 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
2095 		&af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2096 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2097 		&af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2098 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2099 		&af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2100 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2101 		&af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2102 							RC_MAP_IT913X_V1) },
2103 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2104 		&af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2105 							RC_MAP_IT913X_V1) },
2106 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_T1,
2107 		&af9035_props, "TerraTec T1", RC_MAP_IT913X_V1) },
2108 	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2109 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2110 		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2111 		NULL) },
2112 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2113 		&af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2114 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2115 		&af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2116 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2117 		&af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2118 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2119 		&af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2120 
2121 	/* IT930x devices */
2122 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2123 		&it930x_props, "ITE 9303 Generic", NULL) },
2124 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD310,
2125 		&it930x_props, "AVerMedia TD310 DVB-T2", NULL) },
2126 	{ }
2127 };
2128 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2129 
2130 static struct usb_driver af9035_usb_driver = {
2131 	.name = KBUILD_MODNAME,
2132 	.id_table = af9035_id_table,
2133 	.probe = af9035_probe,
2134 	.disconnect = dvb_usbv2_disconnect,
2135 	.suspend = dvb_usbv2_suspend,
2136 	.resume = dvb_usbv2_resume,
2137 	.reset_resume = dvb_usbv2_reset_resume,
2138 	.no_dynamic_id = 1,
2139 	.soft_unbind = 1,
2140 };
2141 
2142 module_usb_driver(af9035_usb_driver);
2143 
2144 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2145 MODULE_DESCRIPTION("Afatech AF9035 driver");
2146 MODULE_LICENSE("GPL");
2147 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2148 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2149 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2150 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);
2151