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