xref: /linux/drivers/media/usb/dvb-usb-v2/af9035.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
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) {
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)
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 struct tua9001_config af9035_tua9001_config = {
1238 	.i2c_addr = 0x60,
1239 };
1240 
1241 static const struct fc0011_config af9035_fc0011_config = {
1242 	.i2c_address = 0x60,
1243 };
1244 
1245 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1246 	{
1247 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1248 		.if_freq_hz = MxL_IF_4_57_MHZ,
1249 		.invert_if = 0,
1250 		.loop_thru_enable = 0,
1251 		.clk_out_enable = 0,
1252 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1253 	}, {
1254 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
1255 		.if_freq_hz = MxL_IF_4_57_MHZ,
1256 		.invert_if = 0,
1257 		.loop_thru_enable = 1,
1258 		.clk_out_enable = 1,
1259 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1260 	}
1261 };
1262 
1263 static struct tda18218_config af9035_tda18218_config = {
1264 	.i2c_address = 0x60,
1265 	.i2c_wr_max = 21,
1266 };
1267 
1268 static const struct fc2580_config af9035_fc2580_config = {
1269 	.i2c_addr = 0x56,
1270 	.clock = 16384000,
1271 };
1272 
1273 static const struct fc0012_config af9035_fc0012_config[] = {
1274 	{
1275 		.i2c_address = 0x63,
1276 		.xtal_freq = FC_XTAL_36_MHZ,
1277 		.dual_master = true,
1278 		.loop_through = true,
1279 		.clock_out = true,
1280 	}, {
1281 		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1282 		.xtal_freq = FC_XTAL_36_MHZ,
1283 		.dual_master = true,
1284 	}
1285 };
1286 
1287 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1288 {
1289 	struct state *state = adap_to_priv(adap);
1290 	struct dvb_usb_device *d = adap_to_d(adap);
1291 	int ret;
1292 	struct dvb_frontend *fe;
1293 	struct i2c_msg msg[1];
1294 	u8 tuner_addr;
1295 
1296 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1297 
1298 	/*
1299 	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1300 	 * to carry info about used I2C bus for dual tuner configuration.
1301 	 */
1302 
1303 	switch (state->af9033_config[adap->id].tuner) {
1304 	case AF9033_TUNER_TUA9001:
1305 		/* AF9035 gpiot3 = TUA9001 RESETN
1306 		   AF9035 gpiot2 = TUA9001 RXEN */
1307 
1308 		/* configure gpiot2 and gpiot2 as output */
1309 		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1310 		if (ret < 0)
1311 			goto err;
1312 
1313 		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1314 		if (ret < 0)
1315 			goto err;
1316 
1317 		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1318 		if (ret < 0)
1319 			goto err;
1320 
1321 		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1322 		if (ret < 0)
1323 			goto err;
1324 
1325 		/* attach tuner */
1326 		fe = dvb_attach(tua9001_attach, adap->fe[0],
1327 				&d->i2c_adap, &af9035_tua9001_config);
1328 		break;
1329 	case AF9033_TUNER_FC0011:
1330 		fe = dvb_attach(fc0011_attach, adap->fe[0],
1331 				&d->i2c_adap, &af9035_fc0011_config);
1332 		break;
1333 	case AF9033_TUNER_MXL5007T:
1334 		if (adap->id == 0) {
1335 			ret = af9035_wr_reg(d, 0x00d8e0, 1);
1336 			if (ret < 0)
1337 				goto err;
1338 
1339 			ret = af9035_wr_reg(d, 0x00d8e1, 1);
1340 			if (ret < 0)
1341 				goto err;
1342 
1343 			ret = af9035_wr_reg(d, 0x00d8df, 0);
1344 			if (ret < 0)
1345 				goto err;
1346 
1347 			msleep(30);
1348 
1349 			ret = af9035_wr_reg(d, 0x00d8df, 1);
1350 			if (ret < 0)
1351 				goto err;
1352 
1353 			msleep(300);
1354 
1355 			ret = af9035_wr_reg(d, 0x00d8c0, 1);
1356 			if (ret < 0)
1357 				goto err;
1358 
1359 			ret = af9035_wr_reg(d, 0x00d8c1, 1);
1360 			if (ret < 0)
1361 				goto err;
1362 
1363 			ret = af9035_wr_reg(d, 0x00d8bf, 0);
1364 			if (ret < 0)
1365 				goto err;
1366 
1367 			ret = af9035_wr_reg(d, 0x00d8b4, 1);
1368 			if (ret < 0)
1369 				goto err;
1370 
1371 			ret = af9035_wr_reg(d, 0x00d8b5, 1);
1372 			if (ret < 0)
1373 				goto err;
1374 
1375 			ret = af9035_wr_reg(d, 0x00d8b3, 1);
1376 			if (ret < 0)
1377 				goto err;
1378 
1379 			tuner_addr = 0x60;
1380 		} else {
1381 			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1382 		}
1383 
1384 		/* attach tuner */
1385 		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1386 				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1387 		break;
1388 	case AF9033_TUNER_TDA18218:
1389 		/* attach tuner */
1390 		fe = dvb_attach(tda18218_attach, adap->fe[0],
1391 				&d->i2c_adap, &af9035_tda18218_config);
1392 		break;
1393 	case AF9033_TUNER_FC2580:
1394 		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1395 		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1396 		if (ret < 0)
1397 			goto err;
1398 
1399 		ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1400 		if (ret < 0)
1401 			goto err;
1402 
1403 		ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1404 		if (ret < 0)
1405 			goto err;
1406 
1407 		usleep_range(10000, 50000);
1408 		/* attach tuner */
1409 		fe = dvb_attach(fc2580_attach, adap->fe[0],
1410 				&d->i2c_adap, &af9035_fc2580_config);
1411 		break;
1412 	case AF9033_TUNER_FC0012:
1413 		/*
1414 		 * AF9035 gpiot2 = FC0012 enable
1415 		 * XXX: there seems to be something on gpioh8 too, but on my
1416 		 * my test I didn't find any difference.
1417 		 */
1418 
1419 		if (adap->id == 0) {
1420 			/* configure gpiot2 as output and high */
1421 			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1422 			if (ret < 0)
1423 				goto err;
1424 
1425 			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1426 			if (ret < 0)
1427 				goto err;
1428 
1429 			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1430 			if (ret < 0)
1431 				goto err;
1432 		} else {
1433 			/*
1434 			 * FIXME: That belongs for the FC0012 driver.
1435 			 * Write 02 to FC0012 master tuner register 0d directly
1436 			 * in order to make slave tuner working.
1437 			 */
1438 			msg[0].addr = 0x63;
1439 			msg[0].flags = 0;
1440 			msg[0].len = 2;
1441 			msg[0].buf = "\x0d\x02";
1442 			ret = i2c_transfer(&d->i2c_adap, msg, 1);
1443 			if (ret < 0)
1444 				goto err;
1445 		}
1446 
1447 		usleep_range(10000, 50000);
1448 
1449 		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1450 				&af9035_fc0012_config[adap->id]);
1451 		break;
1452 	case AF9033_TUNER_IT9135_38:
1453 	case AF9033_TUNER_IT9135_51:
1454 	case AF9033_TUNER_IT9135_52:
1455 	{
1456 		struct it913x_config it913x_config = {
1457 			.fe = adap->fe[0],
1458 			.chip_ver = 1,
1459 		};
1460 
1461 		if (state->dual_mode) {
1462 			if (adap->id == 0)
1463 				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1464 			else
1465 				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1466 		}
1467 
1468 		ret = af9035_add_i2c_dev(d, "it913x",
1469 				state->af9033_i2c_addr[adap->id] >> 1,
1470 				&it913x_config, &d->i2c_adap);
1471 		if (ret)
1472 			goto err;
1473 
1474 		fe = adap->fe[0];
1475 		break;
1476 	}
1477 	case AF9033_TUNER_IT9135_60:
1478 	case AF9033_TUNER_IT9135_61:
1479 	case AF9033_TUNER_IT9135_62:
1480 	{
1481 		struct it913x_config it913x_config = {
1482 			.fe = adap->fe[0],
1483 			.chip_ver = 2,
1484 		};
1485 
1486 		if (state->dual_mode) {
1487 			if (adap->id == 0)
1488 				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1489 			else
1490 				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1491 		}
1492 
1493 		ret = af9035_add_i2c_dev(d, "it913x",
1494 				state->af9033_i2c_addr[adap->id] >> 1,
1495 				&it913x_config, &d->i2c_adap);
1496 		if (ret)
1497 			goto err;
1498 
1499 		fe = adap->fe[0];
1500 		break;
1501 	}
1502 	default:
1503 		fe = NULL;
1504 	}
1505 
1506 	if (fe == NULL) {
1507 		ret = -ENODEV;
1508 		goto err;
1509 	}
1510 
1511 	return 0;
1512 
1513 err:
1514 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1515 
1516 	return ret;
1517 }
1518 
1519 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1520 {
1521 	struct state *state = adap_to_priv(adap);
1522 	struct dvb_usb_device *d = adap_to_d(adap);
1523 	int ret;
1524 	struct si2157_config si2157_config;
1525 
1526 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1527 
1528 	/* I2C master bus 2 clock speed 300k */
1529 	ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1530 	if (ret < 0)
1531 		goto err;
1532 
1533 	/* I2C master bus 1,3 clock speed 300k */
1534 	ret = af9035_wr_reg(d, 0x00f103, 0x07);
1535 	if (ret < 0)
1536 		goto err;
1537 
1538 	/* set gpio11 low */
1539 	ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1540 	if (ret < 0)
1541 		goto err;
1542 
1543 	ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1544 	if (ret < 0)
1545 		goto err;
1546 
1547 	ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1548 	if (ret < 0)
1549 		goto err;
1550 
1551 	/* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1552 	ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1553 	if (ret < 0)
1554 		goto err;
1555 
1556 	ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1557 	if (ret < 0)
1558 		goto err;
1559 
1560 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1561 	if (ret < 0)
1562 		goto err;
1563 
1564 	msleep(200);
1565 
1566 	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1567 	if (ret < 0)
1568 		goto err;
1569 
1570 	memset(&si2157_config, 0, sizeof(si2157_config));
1571 	si2157_config.fe = adap->fe[0];
1572 	ret = af9035_add_i2c_dev(d, "si2157", 0x63,
1573 			&si2157_config, state->i2c_adapter_demod);
1574 
1575 	if (ret)
1576 		goto err;
1577 
1578 	return 0;
1579 
1580 err:
1581 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1582 
1583 	return ret;
1584 }
1585 
1586 
1587 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1588 {
1589 	struct state *state = adap_to_priv(adap);
1590 	struct dvb_usb_device *d = adap_to_d(adap);
1591 
1592 	dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id);
1593 
1594 	if (adap->id == 1) {
1595 		if (state->i2c_client[3])
1596 			af9035_del_i2c_dev(d);
1597 	} else if (adap->id == 0) {
1598 		if (state->i2c_client[1])
1599 			af9035_del_i2c_dev(d);
1600 	}
1601 
1602 	return 0;
1603 }
1604 
1605 
1606 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1607 {
1608 	struct state *state = adap_to_priv(adap);
1609 	struct dvb_usb_device *d = adap_to_d(adap);
1610 
1611 	dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1612 
1613 	switch (state->af9033_config[adap->id].tuner) {
1614 	case AF9033_TUNER_IT9135_38:
1615 	case AF9033_TUNER_IT9135_51:
1616 	case AF9033_TUNER_IT9135_52:
1617 	case AF9033_TUNER_IT9135_60:
1618 	case AF9033_TUNER_IT9135_61:
1619 	case AF9033_TUNER_IT9135_62:
1620 		if (adap->id == 1) {
1621 			if (state->i2c_client[3])
1622 				af9035_del_i2c_dev(d);
1623 		} else if (adap->id == 0) {
1624 			if (state->i2c_client[1])
1625 				af9035_del_i2c_dev(d);
1626 		}
1627 	}
1628 
1629 	return 0;
1630 }
1631 
1632 static int af9035_init(struct dvb_usb_device *d)
1633 {
1634 	struct state *state = d_to_priv(d);
1635 	int ret, i;
1636 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1637 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1638 	struct reg_val_mask tab[] = {
1639 		{ 0x80f99d, 0x01, 0x01 },
1640 		{ 0x80f9a4, 0x01, 0x01 },
1641 		{ 0x00dd11, 0x00, 0x20 },
1642 		{ 0x00dd11, 0x00, 0x40 },
1643 		{ 0x00dd13, 0x00, 0x20 },
1644 		{ 0x00dd13, 0x00, 0x40 },
1645 		{ 0x00dd11, 0x20, 0x20 },
1646 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1647 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1648 		{ 0x00dd0c, packet_size, 0xff},
1649 		{ 0x00dd11, state->dual_mode << 6, 0x40 },
1650 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1651 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1652 		{ 0x00dd0d, packet_size, 0xff },
1653 		{ 0x80f9a3, state->dual_mode, 0x01 },
1654 		{ 0x80f9cd, state->dual_mode, 0x01 },
1655 		{ 0x80f99d, 0x00, 0x01 },
1656 		{ 0x80f9a4, 0x00, 0x01 },
1657 	};
1658 
1659 	dev_dbg(&d->udev->dev,
1660 			"%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1661 			__func__, d->udev->speed, frame_size, packet_size);
1662 
1663 	/* init endpoints */
1664 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1665 		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1666 				tab[i].mask);
1667 		if (ret < 0)
1668 			goto err;
1669 	}
1670 
1671 	return 0;
1672 
1673 err:
1674 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1675 
1676 	return ret;
1677 }
1678 
1679 static int it930x_init(struct dvb_usb_device *d)
1680 {
1681 	struct state *state = d_to_priv(d);
1682 	int ret, i;
1683 	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1684 	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1685 	struct reg_val_mask tab[] = {
1686 		{ 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1687 		{ 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1688 		{ 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1689 		{ 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1690 		{ 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1691 		{ 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1692 		{ 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1693 		{ 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1694 		{ 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1695 		{ 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1696 		{ 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1697 		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1698 		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1699 		{ 0x00dd0c, packet_size, 0xff},
1700 		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1701 		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1702 		{ 0x00dd0d, packet_size, 0xff },
1703 		{ 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1704 		{ 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1705 		{ 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1706 		{ 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1707 		{ 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1708 
1709 		/* suspend gpio1 for TS-C */
1710 		{ 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1711 		{ 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1712 		{ 0x00d8af, 0x00, 0xff }, /* gpio1 */
1713 
1714 		/* suspend gpio7 for TS-D */
1715 		{ 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1716 		{ 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1717 		{ 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1718 
1719 		/* suspend gpio13 for TS-B */
1720 		{ 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1721 		{ 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1722 		{ 0x00d8db, 0x00, 0xff }, /* gpio13 */
1723 
1724 		/* suspend gpio14 for TS-E */
1725 		{ 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1726 		{ 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1727 		{ 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1728 
1729 		/* suspend gpio15 for TS-A */
1730 		{ 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1731 		{ 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1732 		{ 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1733 
1734 		{ 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1735 		{ 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1736 		{ 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1737 		{ 0x00da4c, 0x01, 0xff }, /* ts0_en */
1738 		{ 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1739 	};
1740 
1741 	dev_dbg(&d->udev->dev,
1742 			"%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1743 			__func__, d->udev->speed, frame_size, packet_size);
1744 
1745 	/* init endpoints */
1746 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1747 		ret = af9035_wr_reg_mask(d, tab[i].reg,
1748 				tab[i].val, tab[i].mask);
1749 
1750 		if (ret < 0)
1751 			goto err;
1752 	}
1753 
1754 	return 0;
1755 err:
1756 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1757 
1758 	return ret;
1759 }
1760 
1761 
1762 #if IS_ENABLED(CONFIG_RC_CORE)
1763 static int af9035_rc_query(struct dvb_usb_device *d)
1764 {
1765 	int ret;
1766 	u32 key;
1767 	u8 buf[4];
1768 	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1769 
1770 	ret = af9035_ctrl_msg(d, &req);
1771 	if (ret == 1)
1772 		return 0;
1773 	else if (ret < 0)
1774 		goto err;
1775 
1776 	if ((buf[2] + buf[3]) == 0xff) {
1777 		if ((buf[0] + buf[1]) == 0xff) {
1778 			/* NEC standard 16bit */
1779 			key = RC_SCANCODE_NEC(buf[0], buf[2]);
1780 		} else {
1781 			/* NEC extended 24bit */
1782 			key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1783 		}
1784 	} else {
1785 		/* NEC full code 32bit */
1786 		key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1787 					buf[2] << 8  | buf[3]);
1788 	}
1789 
1790 	dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1791 
1792 	rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0);
1793 
1794 	return 0;
1795 
1796 err:
1797 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1798 
1799 	return ret;
1800 }
1801 
1802 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1803 {
1804 	struct state *state = d_to_priv(d);
1805 	int ret;
1806 	u8 tmp;
1807 
1808 	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1809 	if (ret < 0)
1810 		goto err;
1811 
1812 	dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1813 
1814 	/* don't activate rc if in HID mode or if not available */
1815 	if (tmp == 5) {
1816 		ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1817 				&tmp);
1818 		if (ret < 0)
1819 			goto err;
1820 
1821 		dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1822 
1823 		switch (tmp) {
1824 		case 0: /* NEC */
1825 		default:
1826 			rc->allowed_protos = RC_BIT_NEC;
1827 			break;
1828 		case 1: /* RC6 */
1829 			rc->allowed_protos = RC_BIT_RC6_MCE;
1830 			break;
1831 		}
1832 
1833 		rc->query = af9035_rc_query;
1834 		rc->interval = 500;
1835 
1836 		/* load empty to enable rc */
1837 		if (!rc->map_name)
1838 			rc->map_name = RC_MAP_EMPTY;
1839 	}
1840 
1841 	return 0;
1842 
1843 err:
1844 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1845 
1846 	return ret;
1847 }
1848 #else
1849 	#define af9035_get_rc_config NULL
1850 #endif
1851 
1852 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1853 		struct usb_data_stream_properties *stream)
1854 {
1855 	struct dvb_usb_device *d = fe_to_d(fe);
1856 
1857 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1858 
1859 	if (d->udev->speed == USB_SPEED_FULL)
1860 		stream->u.bulk.buffersize = 5 * 188;
1861 
1862 	return 0;
1863 }
1864 
1865 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1866 {
1867 	struct state *state = adap_to_priv(adap);
1868 
1869 	return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1870 }
1871 
1872 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1873 		int onoff)
1874 {
1875 	struct state *state = adap_to_priv(adap);
1876 
1877 	return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1878 }
1879 
1880 static int af9035_probe(struct usb_interface *intf,
1881 		const struct usb_device_id *id)
1882 {
1883 	struct usb_device *udev = interface_to_usbdev(intf);
1884 	char manufacturer[sizeof("Afatech")];
1885 
1886 	memset(manufacturer, 0, sizeof(manufacturer));
1887 	usb_string(udev, udev->descriptor.iManufacturer,
1888 			manufacturer, sizeof(manufacturer));
1889 	/*
1890 	 * There is two devices having same ID but different chipset. One uses
1891 	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1892 	 * is iManufacturer string.
1893 	 *
1894 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1895 	 * idProduct          0x0099
1896 	 * bcdDevice            2.00
1897 	 * iManufacturer           1 Afatech
1898 	 * iProduct                2 DVB-T 2
1899 	 *
1900 	 * idVendor           0x0ccd TerraTec Electronic GmbH
1901 	 * idProduct          0x0099
1902 	 * bcdDevice            2.00
1903 	 * iManufacturer           1 ITE Technologies, Inc.
1904 	 * iProduct                2 DVB-T TV Stick
1905 	 */
1906 	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1907 			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1908 		if (!strcmp("Afatech", manufacturer)) {
1909 			dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1910 			return -ENODEV;
1911 		}
1912 	}
1913 
1914 	return dvb_usbv2_probe(intf, id);
1915 }
1916 
1917 /* interface 0 is used by DVB-T receiver and
1918    interface 1 is for remote controller (HID) */
1919 static const struct dvb_usb_device_properties af9035_props = {
1920 	.driver_name = KBUILD_MODNAME,
1921 	.owner = THIS_MODULE,
1922 	.adapter_nr = adapter_nr,
1923 	.size_of_priv = sizeof(struct state),
1924 
1925 	.generic_bulk_ctrl_endpoint = 0x02,
1926 	.generic_bulk_ctrl_endpoint_response = 0x81,
1927 
1928 	.identify_state = af9035_identify_state,
1929 	.download_firmware = af9035_download_firmware,
1930 
1931 	.i2c_algo = &af9035_i2c_algo,
1932 	.read_config = af9035_read_config,
1933 	.frontend_attach = af9035_frontend_attach,
1934 	.frontend_detach = af9035_frontend_detach,
1935 	.tuner_attach = af9035_tuner_attach,
1936 	.tuner_detach = af9035_tuner_detach,
1937 	.init = af9035_init,
1938 	.get_rc_config = af9035_get_rc_config,
1939 	.get_stream_config = af9035_get_stream_config,
1940 
1941 	.get_adapter_count = af9035_get_adapter_count,
1942 	.adapter = {
1943 		{
1944 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1945 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1946 
1947 			.pid_filter_count = 32,
1948 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
1949 			.pid_filter = af9035_pid_filter,
1950 
1951 			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1952 		}, {
1953 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1954 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1955 
1956 			.pid_filter_count = 32,
1957 			.pid_filter_ctrl = af9035_pid_filter_ctrl,
1958 			.pid_filter = af9035_pid_filter,
1959 
1960 			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1961 		},
1962 	},
1963 };
1964 
1965 static const struct dvb_usb_device_properties it930x_props = {
1966 	.driver_name = KBUILD_MODNAME,
1967 	.owner = THIS_MODULE,
1968 	.adapter_nr = adapter_nr,
1969 	.size_of_priv = sizeof(struct state),
1970 
1971 	.generic_bulk_ctrl_endpoint = 0x02,
1972 	.generic_bulk_ctrl_endpoint_response = 0x81,
1973 
1974 	.identify_state = af9035_identify_state,
1975 	.download_firmware = af9035_download_firmware,
1976 
1977 	.i2c_algo = &af9035_i2c_algo,
1978 	.read_config = af9035_read_config,
1979 	.frontend_attach = it930x_frontend_attach,
1980 	.frontend_detach = af9035_frontend_detach,
1981 	.tuner_attach = it930x_tuner_attach,
1982 	.tuner_detach = it930x_tuner_detach,
1983 	.init = it930x_init,
1984 	.get_stream_config = af9035_get_stream_config,
1985 
1986 	.get_adapter_count = af9035_get_adapter_count,
1987 	.adapter = {
1988 		{
1989 			.stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
1990 		}, {
1991 			.stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
1992 		},
1993 	},
1994 };
1995 
1996 static const struct usb_device_id af9035_id_table[] = {
1997 	/* AF9035 devices */
1998 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1999 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2000 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2001 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2002 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2003 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2004 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2005 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2006 	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2007 		&af9035_props, "Afatech AF9035 reference design", NULL) },
2008 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2009 		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
2010 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2011 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2012 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2013 		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2014 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2015 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2016 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2017 		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2018 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2019 		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2020 	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2021 		&af9035_props, "Asus U3100Mini Plus", NULL) },
2022 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2023 		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2024 	/* IT9135 devices */
2025 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2026 		&af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2027 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2028 		&af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2029 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2030 		&af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2031 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2032 		&af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2033 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2034 		&af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2035 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2036 		&af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2037 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2038 		&af9035_props, "Avermedia A835B(4835)",	RC_MAP_IT913X_V2) },
2039 	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2040 		&af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2041 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2042 		&af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2043 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2044 		&af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2045 							RC_MAP_IT913X_V1) },
2046 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2047 		&af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2048 							RC_MAP_IT913X_V1) },
2049 	/* IT930x devices */
2050 	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2051 		&it930x_props, "ITE 9303 Generic", NULL) },
2052 	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2053 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2054 		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2055 		NULL) },
2056 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2057 		&af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2058 	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2059 		&af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2060 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2061 		&af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2062 	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2063 		&af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2064 	{ }
2065 };
2066 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2067 
2068 static struct usb_driver af9035_usb_driver = {
2069 	.name = KBUILD_MODNAME,
2070 	.id_table = af9035_id_table,
2071 	.probe = af9035_probe,
2072 	.disconnect = dvb_usbv2_disconnect,
2073 	.suspend = dvb_usbv2_suspend,
2074 	.resume = dvb_usbv2_resume,
2075 	.reset_resume = dvb_usbv2_reset_resume,
2076 	.no_dynamic_id = 1,
2077 	.soft_unbind = 1,
2078 };
2079 
2080 module_usb_driver(af9035_usb_driver);
2081 
2082 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2083 MODULE_DESCRIPTION("Afatech AF9035 driver");
2084 MODULE_LICENSE("GPL");
2085 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2086 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2087 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2088 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);
2089