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