1 /*
2  * Realtek RTL28xxU DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5  * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6  * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License along
19  *    with this program; if not, write to the Free Software Foundation, Inc.,
20  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "rtl28xxu.h"
24 
25 static int rtl28xxu_disable_rc;
26 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644);
27 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller");
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
29 
30 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
31 {
32 	struct rtl28xxu_dev *dev = d->priv;
33 	int ret;
34 	unsigned int pipe;
35 	u8 requesttype;
36 
37 	mutex_lock(&d->usb_mutex);
38 
39 	if (req->size > sizeof(dev->buf)) {
40 		dev_err(&d->intf->dev, "too large message %u\n", req->size);
41 		ret = -EINVAL;
42 		goto err_mutex_unlock;
43 	}
44 
45 	if (req->index & CMD_WR_FLAG) {
46 		/* write */
47 		memcpy(dev->buf, req->data, req->size);
48 		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
49 		pipe = usb_sndctrlpipe(d->udev, 0);
50 	} else {
51 		/* read */
52 		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
53 		pipe = usb_rcvctrlpipe(d->udev, 0);
54 	}
55 
56 	ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
57 			req->index, dev->buf, req->size, 1000);
58 	dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
59 			req->index, dev->buf, req->size);
60 	if (ret < 0)
61 		goto err_mutex_unlock;
62 
63 	/* read request, copy returned data to return buf */
64 	if (requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
65 		memcpy(req->data, dev->buf, req->size);
66 
67 	mutex_unlock(&d->usb_mutex);
68 
69 	return 0;
70 err_mutex_unlock:
71 	mutex_unlock(&d->usb_mutex);
72 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
73 	return ret;
74 }
75 
76 static int rtl28xxu_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
77 {
78 	struct rtl28xxu_req req;
79 
80 	if (reg < 0x3000)
81 		req.index = CMD_USB_WR;
82 	else if (reg < 0x4000)
83 		req.index = CMD_SYS_WR;
84 	else
85 		req.index = CMD_IR_WR;
86 
87 	req.value = reg;
88 	req.size = len;
89 	req.data = val;
90 
91 	return rtl28xxu_ctrl_msg(d, &req);
92 }
93 
94 static int rtl28xxu_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
95 {
96 	struct rtl28xxu_req req;
97 
98 	if (reg < 0x3000)
99 		req.index = CMD_USB_RD;
100 	else if (reg < 0x4000)
101 		req.index = CMD_SYS_RD;
102 	else
103 		req.index = CMD_IR_RD;
104 
105 	req.value = reg;
106 	req.size = len;
107 	req.data = val;
108 
109 	return rtl28xxu_ctrl_msg(d, &req);
110 }
111 
112 static int rtl28xxu_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
113 {
114 	return rtl28xxu_wr_regs(d, reg, &val, 1);
115 }
116 
117 static int rtl28xxu_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
118 {
119 	return rtl28xxu_rd_regs(d, reg, val, 1);
120 }
121 
122 static int rtl28xxu_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
123 		u8 mask)
124 {
125 	int ret;
126 	u8 tmp;
127 
128 	/* no need for read if whole reg is written */
129 	if (mask != 0xff) {
130 		ret = rtl28xxu_rd_reg(d, reg, &tmp);
131 		if (ret)
132 			return ret;
133 
134 		val &= mask;
135 		tmp &= ~mask;
136 		val |= tmp;
137 	}
138 
139 	return rtl28xxu_wr_reg(d, reg, val);
140 }
141 
142 /* I2C */
143 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
144 	int num)
145 {
146 	int ret;
147 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
148 	struct rtl28xxu_dev *dev = d->priv;
149 	struct rtl28xxu_req req;
150 
151 	/*
152 	 * It is not known which are real I2C bus xfer limits, but testing
153 	 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
154 	 * TODO: find out RTL2832U lens
155 	 */
156 
157 	/*
158 	 * I2C adapter logic looks rather complicated due to fact it handles
159 	 * three different access methods. Those methods are;
160 	 * 1) integrated demod access
161 	 * 2) old I2C access
162 	 * 3) new I2C access
163 	 *
164 	 * Used method is selected in order 1, 2, 3. Method 3 can handle all
165 	 * requests but there is two reasons why not use it always;
166 	 * 1) It is most expensive, usually two USB messages are needed
167 	 * 2) At least RTL2831U does not support it
168 	 *
169 	 * Method 3 is needed in case of I2C write+read (typical register read)
170 	 * where write is more than one byte.
171 	 */
172 
173 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
174 		return -EAGAIN;
175 
176 	if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
177 		(msg[1].flags & I2C_M_RD)) {
178 		if (msg[0].len > 24 || msg[1].len > 24) {
179 			/* TODO: check msg[0].len max */
180 			ret = -EOPNOTSUPP;
181 			goto err_mutex_unlock;
182 		} else if (msg[0].addr == 0x10) {
183 			/* method 1 - integrated demod */
184 			if (msg[0].buf[0] == 0x00) {
185 				/* return demod page from driver cache */
186 				msg[1].buf[0] = dev->page;
187 				ret = 0;
188 			} else {
189 				req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
190 				req.index = CMD_DEMOD_RD | dev->page;
191 				req.size = msg[1].len;
192 				req.data = &msg[1].buf[0];
193 				ret = rtl28xxu_ctrl_msg(d, &req);
194 			}
195 		} else if (msg[0].len < 2) {
196 			/* method 2 - old I2C */
197 			req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
198 			req.index = CMD_I2C_RD;
199 			req.size = msg[1].len;
200 			req.data = &msg[1].buf[0];
201 			ret = rtl28xxu_ctrl_msg(d, &req);
202 		} else {
203 			/* method 3 - new I2C */
204 			req.value = (msg[0].addr << 1);
205 			req.index = CMD_I2C_DA_WR;
206 			req.size = msg[0].len;
207 			req.data = msg[0].buf;
208 			ret = rtl28xxu_ctrl_msg(d, &req);
209 			if (ret)
210 				goto err_mutex_unlock;
211 
212 			req.value = (msg[0].addr << 1);
213 			req.index = CMD_I2C_DA_RD;
214 			req.size = msg[1].len;
215 			req.data = msg[1].buf;
216 			ret = rtl28xxu_ctrl_msg(d, &req);
217 		}
218 	} else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
219 		if (msg[0].len > 22) {
220 			/* TODO: check msg[0].len max */
221 			ret = -EOPNOTSUPP;
222 			goto err_mutex_unlock;
223 		} else if (msg[0].addr == 0x10) {
224 			/* method 1 - integrated demod */
225 			if (msg[0].buf[0] == 0x00) {
226 				/* save demod page for later demod access */
227 				dev->page = msg[0].buf[1];
228 				ret = 0;
229 			} else {
230 				req.value = (msg[0].buf[0] << 8) |
231 					(msg[0].addr << 1);
232 				req.index = CMD_DEMOD_WR | dev->page;
233 				req.size = msg[0].len-1;
234 				req.data = &msg[0].buf[1];
235 				ret = rtl28xxu_ctrl_msg(d, &req);
236 			}
237 		} else if ((msg[0].len < 23) && (!dev->new_i2c_write)) {
238 			/* method 2 - old I2C */
239 			req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
240 			req.index = CMD_I2C_WR;
241 			req.size = msg[0].len-1;
242 			req.data = &msg[0].buf[1];
243 			ret = rtl28xxu_ctrl_msg(d, &req);
244 		} else {
245 			/* method 3 - new I2C */
246 			req.value = (msg[0].addr << 1);
247 			req.index = CMD_I2C_DA_WR;
248 			req.size = msg[0].len;
249 			req.data = msg[0].buf;
250 			ret = rtl28xxu_ctrl_msg(d, &req);
251 		}
252 	} else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
253 		req.value = (msg[0].addr << 1);
254 		req.index = CMD_I2C_DA_RD;
255 		req.size = msg[0].len;
256 		req.data = msg[0].buf;
257 		ret = rtl28xxu_ctrl_msg(d, &req);
258 	} else {
259 		ret = -EOPNOTSUPP;
260 	}
261 
262 	/* Retry failed I2C messages */
263 	if (ret == -EPIPE)
264 		ret = -EAGAIN;
265 
266 err_mutex_unlock:
267 	mutex_unlock(&d->i2c_mutex);
268 
269 	return ret ? ret : num;
270 }
271 
272 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
273 {
274 	return I2C_FUNC_I2C;
275 }
276 
277 static struct i2c_algorithm rtl28xxu_i2c_algo = {
278 	.master_xfer   = rtl28xxu_i2c_xfer,
279 	.functionality = rtl28xxu_i2c_func,
280 };
281 
282 static int rtl2831u_read_config(struct dvb_usb_device *d)
283 {
284 	struct rtl28xxu_dev *dev = d_to_priv(d);
285 	int ret;
286 	u8 buf[1];
287 	/* open RTL2831U/RTL2830 I2C gate */
288 	struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
289 	/* tuner probes */
290 	struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
291 	struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
292 
293 	dev_dbg(&d->intf->dev, "\n");
294 
295 	/*
296 	 * RTL2831U GPIOs
297 	 * =========================================================
298 	 * GPIO0 | tuner#0 | 0 off | 1 on  | MXL5005S (?)
299 	 * GPIO2 | LED     | 0 off | 1 on  |
300 	 * GPIO4 | tuner#1 | 0 on  | 1 off | MT2060
301 	 */
302 
303 	/* GPIO direction */
304 	ret = rtl28xxu_wr_reg(d, SYS_GPIO_DIR, 0x0a);
305 	if (ret)
306 		goto err;
307 
308 	/* enable as output GPIO0, GPIO2, GPIO4 */
309 	ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
310 	if (ret)
311 		goto err;
312 
313 	/*
314 	 * Probe used tuner. We need to know used tuner before demod attach
315 	 * since there is some demod params needed to set according to tuner.
316 	 */
317 
318 	/* demod needs some time to wake up */
319 	msleep(20);
320 
321 	dev->tuner_name = "NONE";
322 
323 	/* open demod I2C gate */
324 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
325 	if (ret)
326 		goto err;
327 
328 	/* check QT1010 ID(?) register; reg=0f val=2c */
329 	ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
330 	if (ret == 0 && buf[0] == 0x2c) {
331 		dev->tuner = TUNER_RTL2830_QT1010;
332 		dev->tuner_name = "QT1010";
333 		goto found;
334 	}
335 
336 	/* open demod I2C gate */
337 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
338 	if (ret)
339 		goto err;
340 
341 	/* check MT2060 ID register; reg=00 val=63 */
342 	ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
343 	if (ret == 0 && buf[0] == 0x63) {
344 		dev->tuner = TUNER_RTL2830_MT2060;
345 		dev->tuner_name = "MT2060";
346 		goto found;
347 	}
348 
349 	/* assume MXL5005S */
350 	dev->tuner = TUNER_RTL2830_MXL5005S;
351 	dev->tuner_name = "MXL5005S";
352 	goto found;
353 
354 found:
355 	dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
356 
357 	return 0;
358 err:
359 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
360 	return ret;
361 }
362 
363 static int rtl2832u_read_config(struct dvb_usb_device *d)
364 {
365 	struct rtl28xxu_dev *dev = d_to_priv(d);
366 	int ret;
367 	u8 buf[2];
368 	/* open RTL2832U/RTL2832 I2C gate */
369 	struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
370 	/* close RTL2832U/RTL2832 I2C gate */
371 	struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
372 	/* tuner probes */
373 	struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
374 	struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
375 	struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
376 	struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
377 	struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
378 	struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
379 	struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
380 	struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
381 	struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
382 	struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
383 	struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
384 	struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
385 	struct rtl28xxu_req req_mn88472 = {0xff38, CMD_I2C_RD, 1, buf};
386 	struct rtl28xxu_req req_mn88473 = {0xff38, CMD_I2C_RD, 1, buf};
387 	struct rtl28xxu_req req_cxd2837er = {0xfdd8, CMD_I2C_RD, 1, buf};
388 	struct rtl28xxu_req req_si2157 = {0x00c0, CMD_I2C_RD, 1, buf};
389 	struct rtl28xxu_req req_si2168 = {0x00c8, CMD_I2C_RD, 1, buf};
390 
391 	dev_dbg(&d->intf->dev, "\n");
392 
393 	/* enable GPIO3 and GPIO6 as output */
394 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
395 	if (ret)
396 		goto err;
397 
398 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
399 	if (ret)
400 		goto err;
401 
402 	/*
403 	 * Probe used tuner. We need to know used tuner before demod attach
404 	 * since there is some demod params needed to set according to tuner.
405 	 */
406 
407 	/* open demod I2C gate */
408 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
409 	if (ret)
410 		goto err;
411 
412 	dev->tuner_name = "NONE";
413 
414 	/* check FC0012 ID register; reg=00 val=a1 */
415 	ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
416 	if (ret == 0 && buf[0] == 0xa1) {
417 		dev->tuner = TUNER_RTL2832_FC0012;
418 		dev->tuner_name = "FC0012";
419 		goto tuner_found;
420 	}
421 
422 	/* check FC0013 ID register; reg=00 val=a3 */
423 	ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
424 	if (ret == 0 && buf[0] == 0xa3) {
425 		dev->tuner = TUNER_RTL2832_FC0013;
426 		dev->tuner_name = "FC0013";
427 		goto tuner_found;
428 	}
429 
430 	/* check MT2266 ID register; reg=00 val=85 */
431 	ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
432 	if (ret == 0 && buf[0] == 0x85) {
433 		dev->tuner = TUNER_RTL2832_MT2266;
434 		dev->tuner_name = "MT2266";
435 		goto tuner_found;
436 	}
437 
438 	/* check FC2580 ID register; reg=01 val=56 */
439 	ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
440 	if (ret == 0 && buf[0] == 0x56) {
441 		dev->tuner = TUNER_RTL2832_FC2580;
442 		dev->tuner_name = "FC2580";
443 		goto tuner_found;
444 	}
445 
446 	/* check MT2063 ID register; reg=00 val=9e || 9c */
447 	ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
448 	if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
449 		dev->tuner = TUNER_RTL2832_MT2063;
450 		dev->tuner_name = "MT2063";
451 		goto tuner_found;
452 	}
453 
454 	/* check MAX3543 ID register; reg=00 val=38 */
455 	ret = rtl28xxu_ctrl_msg(d, &req_max3543);
456 	if (ret == 0 && buf[0] == 0x38) {
457 		dev->tuner = TUNER_RTL2832_MAX3543;
458 		dev->tuner_name = "MAX3543";
459 		goto tuner_found;
460 	}
461 
462 	/* check TUA9001 ID register; reg=7e val=2328 */
463 	ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
464 	if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
465 		dev->tuner = TUNER_RTL2832_TUA9001;
466 		dev->tuner_name = "TUA9001";
467 		goto tuner_found;
468 	}
469 
470 	/* check MXL5007R ID register; reg=d9 val=14 */
471 	ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
472 	if (ret == 0 && buf[0] == 0x14) {
473 		dev->tuner = TUNER_RTL2832_MXL5007T;
474 		dev->tuner_name = "MXL5007T";
475 		goto tuner_found;
476 	}
477 
478 	/* check E4000 ID register; reg=02 val=40 */
479 	ret = rtl28xxu_ctrl_msg(d, &req_e4000);
480 	if (ret == 0 && buf[0] == 0x40) {
481 		dev->tuner = TUNER_RTL2832_E4000;
482 		dev->tuner_name = "E4000";
483 		goto tuner_found;
484 	}
485 
486 	/* check TDA18272 ID register; reg=00 val=c760  */
487 	ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
488 	if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
489 		dev->tuner = TUNER_RTL2832_TDA18272;
490 		dev->tuner_name = "TDA18272";
491 		goto tuner_found;
492 	}
493 
494 	/* check R820T ID register; reg=00 val=69 */
495 	ret = rtl28xxu_ctrl_msg(d, &req_r820t);
496 	if (ret == 0 && buf[0] == 0x69) {
497 		dev->tuner = TUNER_RTL2832_R820T;
498 		dev->tuner_name = "R820T";
499 		goto tuner_found;
500 	}
501 
502 	/* check R828D ID register; reg=00 val=69 */
503 	ret = rtl28xxu_ctrl_msg(d, &req_r828d);
504 	if (ret == 0 && buf[0] == 0x69) {
505 		dev->tuner = TUNER_RTL2832_R828D;
506 		dev->tuner_name = "R828D";
507 		goto tuner_found;
508 	}
509 
510 	/* GPIO0 and GPIO5 to reset Si2157/Si2168 tuner and demod */
511 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x21);
512 	if (ret)
513 		goto err;
514 
515 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x21);
516 	if (ret)
517 		goto err;
518 
519 	msleep(50);
520 
521 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x21, 0x21);
522 	if (ret)
523 		goto err;
524 
525 	ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x21, 0x21);
526 	if (ret)
527 		goto err;
528 
529 	msleep(50);
530 
531 	/* check Si2157 ID register; reg=c0 val=80 */
532 	ret = rtl28xxu_ctrl_msg(d, &req_si2157);
533 	if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
534 		dev->tuner = TUNER_RTL2832_SI2157;
535 		dev->tuner_name = "SI2157";
536 		goto tuner_found;
537 	}
538 
539 tuner_found:
540 	dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
541 
542 	/* probe slave demod */
543 	if (dev->tuner == TUNER_RTL2832_R828D) {
544 		/* power off slave demod on GPIO0 to reset CXD2837ER */
545 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
546 		if (ret)
547 			goto err;
548 
549 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x01);
550 		if (ret)
551 			goto err;
552 
553 		msleep(50);
554 
555 		/* power on slave demod on GPIO0 */
556 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x01, 0x01);
557 		if (ret)
558 			goto err;
559 
560 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
561 		if (ret)
562 			goto err;
563 
564 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
565 		if (ret)
566 			goto err;
567 
568 		/* check slave answers */
569 		ret = rtl28xxu_ctrl_msg(d, &req_mn88472);
570 		if (ret == 0 && buf[0] == 0x02) {
571 			dev_dbg(&d->intf->dev, "MN88472 found\n");
572 			dev->slave_demod = SLAVE_DEMOD_MN88472;
573 			goto demod_found;
574 		}
575 
576 		ret = rtl28xxu_ctrl_msg(d, &req_mn88473);
577 		if (ret == 0 && buf[0] == 0x03) {
578 			dev_dbg(&d->intf->dev, "MN88473 found\n");
579 			dev->slave_demod = SLAVE_DEMOD_MN88473;
580 			goto demod_found;
581 		}
582 
583 		ret = rtl28xxu_ctrl_msg(d, &req_cxd2837er);
584 		if (ret == 0 && buf[0] == 0xb1) {
585 			dev_dbg(&d->intf->dev, "CXD2837ER found\n");
586 			dev->slave_demod = SLAVE_DEMOD_CXD2837ER;
587 			goto demod_found;
588 		}
589 	}
590 	if (dev->tuner == TUNER_RTL2832_SI2157) {
591 		/* check Si2168 ID register; reg=c8 val=80 */
592 		ret = rtl28xxu_ctrl_msg(d, &req_si2168);
593 		if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
594 			dev_dbg(&d->intf->dev, "Si2168 found\n");
595 			dev->slave_demod = SLAVE_DEMOD_SI2168;
596 			goto demod_found;
597 		}
598 	}
599 
600 demod_found:
601 	/* close demod I2C gate */
602 	ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
603 	if (ret < 0)
604 		goto err;
605 
606 	return 0;
607 err:
608 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
609 	return ret;
610 }
611 
612 static int rtl28xxu_read_config(struct dvb_usb_device *d)
613 {
614 	struct rtl28xxu_dev *dev = d_to_priv(d);
615 
616 	if (dev->chip_id == CHIP_ID_RTL2831U)
617 		return rtl2831u_read_config(d);
618 	else
619 		return rtl2832u_read_config(d);
620 }
621 
622 static int rtl28xxu_identify_state(struct dvb_usb_device *d, const char **name)
623 {
624 	struct rtl28xxu_dev *dev = d_to_priv(d);
625 	int ret;
626 	struct rtl28xxu_req req_demod_i2c = {0x0020, CMD_I2C_DA_RD, 0, NULL};
627 
628 	dev_dbg(&d->intf->dev, "\n");
629 
630 	/*
631 	 * Detect chip type using I2C command that is not supported
632 	 * by old RTL2831U.
633 	 */
634 	ret = rtl28xxu_ctrl_msg(d, &req_demod_i2c);
635 	if (ret == -EPIPE) {
636 		dev->chip_id = CHIP_ID_RTL2831U;
637 	} else if (ret == 0) {
638 		dev->chip_id = CHIP_ID_RTL2832U;
639 	} else {
640 		dev_err(&d->intf->dev, "chip type detection failed %d\n", ret);
641 		goto err;
642 	}
643 	dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id);
644 
645 	/* Retry failed I2C messages */
646 	d->i2c_adap.retries = 3;
647 	d->i2c_adap.timeout = msecs_to_jiffies(10);
648 
649 	return WARM;
650 err:
651 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
652 	return ret;
653 }
654 
655 static const struct rtl2830_platform_data rtl2830_mt2060_platform_data = {
656 	.clk = 28800000,
657 	.spec_inv = 1,
658 	.vtop = 0x20,
659 	.krf = 0x04,
660 	.agc_targ_val = 0x2d,
661 
662 };
663 
664 static const struct rtl2830_platform_data rtl2830_qt1010_platform_data = {
665 	.clk = 28800000,
666 	.spec_inv = 1,
667 	.vtop = 0x20,
668 	.krf = 0x04,
669 	.agc_targ_val = 0x2d,
670 };
671 
672 static const struct rtl2830_platform_data rtl2830_mxl5005s_platform_data = {
673 	.clk = 28800000,
674 	.spec_inv = 0,
675 	.vtop = 0x3f,
676 	.krf = 0x04,
677 	.agc_targ_val = 0x3e,
678 };
679 
680 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
681 {
682 	struct dvb_usb_device *d = adap_to_d(adap);
683 	struct rtl28xxu_dev *dev = d_to_priv(d);
684 	struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
685 	struct i2c_board_info board_info;
686 	struct i2c_client *client;
687 	int ret;
688 
689 	dev_dbg(&d->intf->dev, "\n");
690 
691 	switch (dev->tuner) {
692 	case TUNER_RTL2830_QT1010:
693 		*pdata = rtl2830_qt1010_platform_data;
694 		break;
695 	case TUNER_RTL2830_MT2060:
696 		*pdata = rtl2830_mt2060_platform_data;
697 		break;
698 	case TUNER_RTL2830_MXL5005S:
699 		*pdata = rtl2830_mxl5005s_platform_data;
700 		break;
701 	default:
702 		dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
703 		ret = -ENODEV;
704 		goto err;
705 	}
706 
707 	/* attach demodulator */
708 	memset(&board_info, 0, sizeof(board_info));
709 	strscpy(board_info.type, "rtl2830", I2C_NAME_SIZE);
710 	board_info.addr = 0x10;
711 	board_info.platform_data = pdata;
712 	request_module("%s", board_info.type);
713 	client = i2c_new_device(&d->i2c_adap, &board_info);
714 	if (client == NULL || client->dev.driver == NULL) {
715 		ret = -ENODEV;
716 		goto err;
717 	}
718 
719 	if (!try_module_get(client->dev.driver->owner)) {
720 		i2c_unregister_device(client);
721 		ret = -ENODEV;
722 		goto err;
723 	}
724 
725 	adap->fe[0] = pdata->get_dvb_frontend(client);
726 	dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
727 
728 	dev->i2c_client_demod = client;
729 
730 	return 0;
731 err:
732 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
733 	return ret;
734 }
735 
736 static const struct rtl2832_platform_data rtl2832_fc2580_platform_data = {
737 	.clk = 28800000,
738 	.tuner = TUNER_RTL2832_FC2580,
739 };
740 
741 static const struct rtl2832_platform_data rtl2832_fc0012_platform_data = {
742 	.clk = 28800000,
743 	.tuner = TUNER_RTL2832_FC0012
744 };
745 
746 static const struct rtl2832_platform_data rtl2832_fc0013_platform_data = {
747 	.clk = 28800000,
748 	.tuner = TUNER_RTL2832_FC0013
749 };
750 
751 static const struct rtl2832_platform_data rtl2832_tua9001_platform_data = {
752 	.clk = 28800000,
753 	.tuner = TUNER_RTL2832_TUA9001,
754 };
755 
756 static const struct rtl2832_platform_data rtl2832_e4000_platform_data = {
757 	.clk = 28800000,
758 	.tuner = TUNER_RTL2832_E4000,
759 };
760 
761 static const struct rtl2832_platform_data rtl2832_r820t_platform_data = {
762 	.clk = 28800000,
763 	.tuner = TUNER_RTL2832_R820T,
764 };
765 
766 static const struct rtl2832_platform_data rtl2832_si2157_platform_data = {
767 	.clk = 28800000,
768 	.tuner = TUNER_RTL2832_SI2157,
769 };
770 
771 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
772 		int cmd, int arg)
773 {
774 	int ret;
775 	u8 val;
776 
777 	dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
778 
779 	switch (cmd) {
780 	case FC_FE_CALLBACK_VHF_ENABLE:
781 		/* set output values */
782 		ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
783 		if (ret)
784 			goto err;
785 
786 		if (arg)
787 			val &= 0xbf; /* set GPIO6 low */
788 		else
789 			val |= 0x40; /* set GPIO6 high */
790 
791 
792 		ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, val);
793 		if (ret)
794 			goto err;
795 		break;
796 	default:
797 		ret = -EINVAL;
798 		goto err;
799 	}
800 	return 0;
801 err:
802 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
803 	return ret;
804 }
805 
806 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
807 		int cmd, int arg)
808 {
809 	int ret;
810 	u8 val;
811 
812 	dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
813 
814 	/*
815 	 * CEN     always enabled by hardware wiring
816 	 * RESETN  GPIO4
817 	 * RXEN    GPIO1
818 	 */
819 
820 	switch (cmd) {
821 	case TUA9001_CMD_RESETN:
822 		if (arg)
823 			val = (1 << 4);
824 		else
825 			val = (0 << 4);
826 
827 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
828 		if (ret)
829 			goto err;
830 		break;
831 	case TUA9001_CMD_RXEN:
832 		if (arg)
833 			val = (1 << 1);
834 		else
835 			val = (0 << 1);
836 
837 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
838 		if (ret)
839 			goto err;
840 		break;
841 	}
842 
843 	return 0;
844 err:
845 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
846 	return ret;
847 }
848 
849 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
850 		int cmd, int arg)
851 {
852 	struct i2c_adapter *adapter = adapter_priv;
853 	struct device *parent = adapter->dev.parent;
854 	struct i2c_adapter *parent_adapter;
855 	struct dvb_usb_device *d;
856 	struct rtl28xxu_dev *dev;
857 
858 	/*
859 	 * All tuners are connected to demod muxed I2C adapter. We have to
860 	 * resolve its parent adapter in order to get handle for this driver
861 	 * private data. That is a bit hackish solution, GPIO or direct driver
862 	 * callback would be better...
863 	 */
864 	if (parent != NULL && parent->type == &i2c_adapter_type)
865 		parent_adapter = to_i2c_adapter(parent);
866 	else
867 		return -EINVAL;
868 
869 	d = i2c_get_adapdata(parent_adapter);
870 	dev = d->priv;
871 
872 	dev_dbg(&d->intf->dev, "component=%d cmd=%d arg=%d\n",
873 		component, cmd, arg);
874 
875 	switch (component) {
876 	case DVB_FRONTEND_COMPONENT_TUNER:
877 		switch (dev->tuner) {
878 		case TUNER_RTL2832_FC0012:
879 			return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
880 		case TUNER_RTL2832_TUA9001:
881 			return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
882 		}
883 	}
884 
885 	return 0;
886 }
887 
888 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
889 {
890 	struct dvb_usb_device *d = adap_to_d(adap);
891 	struct rtl28xxu_dev *dev = d_to_priv(d);
892 	struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
893 	struct i2c_board_info board_info;
894 	struct i2c_client *client;
895 	int ret;
896 
897 	dev_dbg(&d->intf->dev, "\n");
898 
899 	switch (dev->tuner) {
900 	case TUNER_RTL2832_FC0012:
901 		*pdata = rtl2832_fc0012_platform_data;
902 		break;
903 	case TUNER_RTL2832_FC0013:
904 		*pdata = rtl2832_fc0013_platform_data;
905 		break;
906 	case TUNER_RTL2832_FC2580:
907 		*pdata = rtl2832_fc2580_platform_data;
908 		break;
909 	case TUNER_RTL2832_TUA9001:
910 		*pdata = rtl2832_tua9001_platform_data;
911 		break;
912 	case TUNER_RTL2832_E4000:
913 		*pdata = rtl2832_e4000_platform_data;
914 		break;
915 	case TUNER_RTL2832_R820T:
916 	case TUNER_RTL2832_R828D:
917 		*pdata = rtl2832_r820t_platform_data;
918 		break;
919 	case TUNER_RTL2832_SI2157:
920 		*pdata = rtl2832_si2157_platform_data;
921 		break;
922 	default:
923 		dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
924 		ret = -ENODEV;
925 		goto err;
926 	}
927 
928 	/* attach demodulator */
929 	memset(&board_info, 0, sizeof(board_info));
930 	strscpy(board_info.type, "rtl2832", I2C_NAME_SIZE);
931 	board_info.addr = 0x10;
932 	board_info.platform_data = pdata;
933 	request_module("%s", board_info.type);
934 	client = i2c_new_device(&d->i2c_adap, &board_info);
935 	if (client == NULL || client->dev.driver == NULL) {
936 		ret = -ENODEV;
937 		goto err;
938 	}
939 
940 	if (!try_module_get(client->dev.driver->owner)) {
941 		i2c_unregister_device(client);
942 		ret = -ENODEV;
943 		goto err;
944 	}
945 
946 	adap->fe[0] = pdata->get_dvb_frontend(client);
947 	dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
948 
949 	dev->i2c_client_demod = client;
950 
951 	/* set fe callback */
952 	adap->fe[0]->callback = rtl2832u_frontend_callback;
953 
954 	if (dev->slave_demod) {
955 		struct i2c_board_info info = {};
956 
957 		/*
958 		 * We continue on reduced mode, without DVB-T2/C, using master
959 		 * demod, when slave demod fails.
960 		 */
961 		ret = 0;
962 
963 		/* attach slave demodulator */
964 		if (dev->slave_demod == SLAVE_DEMOD_MN88472) {
965 			struct mn88472_config mn88472_config = {};
966 
967 			mn88472_config.fe = &adap->fe[1];
968 			mn88472_config.i2c_wr_max = 22,
969 			strscpy(info.type, "mn88472", I2C_NAME_SIZE);
970 			mn88472_config.xtal = 20500000;
971 			mn88472_config.ts_mode = SERIAL_TS_MODE;
972 			mn88472_config.ts_clock = VARIABLE_TS_CLOCK;
973 			info.addr = 0x18;
974 			info.platform_data = &mn88472_config;
975 			request_module(info.type);
976 			client = i2c_new_device(&d->i2c_adap, &info);
977 			if (client == NULL || client->dev.driver == NULL) {
978 				dev->slave_demod = SLAVE_DEMOD_NONE;
979 				goto err_slave_demod_failed;
980 			}
981 
982 			if (!try_module_get(client->dev.driver->owner)) {
983 				i2c_unregister_device(client);
984 				dev->slave_demod = SLAVE_DEMOD_NONE;
985 				goto err_slave_demod_failed;
986 			}
987 
988 			dev->i2c_client_slave_demod = client;
989 		} else if (dev->slave_demod == SLAVE_DEMOD_MN88473) {
990 			struct mn88473_config mn88473_config = {};
991 
992 			mn88473_config.fe = &adap->fe[1];
993 			mn88473_config.i2c_wr_max = 22,
994 			strscpy(info.type, "mn88473", I2C_NAME_SIZE);
995 			info.addr = 0x18;
996 			info.platform_data = &mn88473_config;
997 			request_module(info.type);
998 			client = i2c_new_device(&d->i2c_adap, &info);
999 			if (client == NULL || client->dev.driver == NULL) {
1000 				dev->slave_demod = SLAVE_DEMOD_NONE;
1001 				goto err_slave_demod_failed;
1002 			}
1003 
1004 			if (!try_module_get(client->dev.driver->owner)) {
1005 				i2c_unregister_device(client);
1006 				dev->slave_demod = SLAVE_DEMOD_NONE;
1007 				goto err_slave_demod_failed;
1008 			}
1009 
1010 			dev->i2c_client_slave_demod = client;
1011 		} else if (dev->slave_demod == SLAVE_DEMOD_CXD2837ER) {
1012 			struct cxd2841er_config cxd2837er_config = {};
1013 
1014 			cxd2837er_config.i2c_addr = 0xd8;
1015 			cxd2837er_config.xtal = SONY_XTAL_20500;
1016 			cxd2837er_config.flags = (CXD2841ER_AUTO_IFHZ |
1017 				CXD2841ER_NO_AGCNEG | CXD2841ER_TSBITS |
1018 				CXD2841ER_EARLY_TUNE | CXD2841ER_TS_SERIAL);
1019 			adap->fe[1] = dvb_attach(cxd2841er_attach_t_c,
1020 						 &cxd2837er_config,
1021 						 &d->i2c_adap);
1022 			if (!adap->fe[1]) {
1023 				dev->slave_demod = SLAVE_DEMOD_NONE;
1024 				goto err_slave_demod_failed;
1025 			}
1026 			adap->fe[1]->id = 1;
1027 			dev->i2c_client_slave_demod = NULL;
1028 		} else {
1029 			struct si2168_config si2168_config = {};
1030 			struct i2c_adapter *adapter;
1031 
1032 			si2168_config.i2c_adapter = &adapter;
1033 			si2168_config.fe = &adap->fe[1];
1034 			si2168_config.ts_mode = SI2168_TS_SERIAL;
1035 			si2168_config.ts_clock_inv = false;
1036 			si2168_config.ts_clock_gapped = true;
1037 			strscpy(info.type, "si2168", I2C_NAME_SIZE);
1038 			info.addr = 0x64;
1039 			info.platform_data = &si2168_config;
1040 			request_module(info.type);
1041 			client = i2c_new_device(&d->i2c_adap, &info);
1042 			if (client == NULL || client->dev.driver == NULL) {
1043 				dev->slave_demod = SLAVE_DEMOD_NONE;
1044 				goto err_slave_demod_failed;
1045 			}
1046 
1047 			if (!try_module_get(client->dev.driver->owner)) {
1048 				i2c_unregister_device(client);
1049 				dev->slave_demod = SLAVE_DEMOD_NONE;
1050 				goto err_slave_demod_failed;
1051 			}
1052 
1053 			dev->i2c_client_slave_demod = client;
1054 
1055 			/* for Si2168 devices use only new I2C write method */
1056 			dev->new_i2c_write = true;
1057 		}
1058 	}
1059 	return 0;
1060 err_slave_demod_failed:
1061 err:
1062 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1063 	return ret;
1064 }
1065 
1066 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter *adap)
1067 {
1068 	struct rtl28xxu_dev *dev = adap_to_priv(adap);
1069 
1070 	if (dev->chip_id == CHIP_ID_RTL2831U)
1071 		return rtl2831u_frontend_attach(adap);
1072 	else
1073 		return rtl2832u_frontend_attach(adap);
1074 }
1075 
1076 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter *adap)
1077 {
1078 	struct dvb_usb_device *d = adap_to_d(adap);
1079 	struct rtl28xxu_dev *dev = d_to_priv(d);
1080 	struct i2c_client *client;
1081 
1082 	dev_dbg(&d->intf->dev, "\n");
1083 
1084 	/* remove I2C slave demod */
1085 	client = dev->i2c_client_slave_demod;
1086 	if (client) {
1087 		module_put(client->dev.driver->owner);
1088 		i2c_unregister_device(client);
1089 	}
1090 
1091 	/* remove I2C demod */
1092 	client = dev->i2c_client_demod;
1093 	if (client) {
1094 		module_put(client->dev.driver->owner);
1095 		i2c_unregister_device(client);
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 static struct qt1010_config rtl28xxu_qt1010_config = {
1102 	.i2c_address = 0x62, /* 0xc4 */
1103 };
1104 
1105 static struct mt2060_config rtl28xxu_mt2060_config = {
1106 	.i2c_address = 0x60, /* 0xc0 */
1107 	.clock_out = 0,
1108 };
1109 
1110 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
1111 	.i2c_address     = 0x63, /* 0xc6 */
1112 	.if_freq         = IF_FREQ_4570000HZ,
1113 	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
1114 	.agc_mode        = MXL_SINGLE_AGC,
1115 	.tracking_filter = MXL_TF_C_H,
1116 	.rssi_enable     = MXL_RSSI_ENABLE,
1117 	.cap_select      = MXL_CAP_SEL_ENABLE,
1118 	.div_out         = MXL_DIV_OUT_4,
1119 	.clock_out       = MXL_CLOCK_OUT_DISABLE,
1120 	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1121 	.top		 = MXL5005S_TOP_25P2,
1122 	.mod_mode        = MXL_DIGITAL_MODE,
1123 	.if_mode         = MXL_ZERO_IF,
1124 	.AgcMasterByte   = 0x00,
1125 };
1126 
1127 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
1128 {
1129 	int ret;
1130 	struct dvb_usb_device *d = adap_to_d(adap);
1131 	struct rtl28xxu_dev *dev = d_to_priv(d);
1132 	struct dvb_frontend *fe;
1133 
1134 	dev_dbg(&d->intf->dev, "\n");
1135 
1136 	switch (dev->tuner) {
1137 	case TUNER_RTL2830_QT1010:
1138 		fe = dvb_attach(qt1010_attach, adap->fe[0],
1139 				dev->demod_i2c_adapter,
1140 				&rtl28xxu_qt1010_config);
1141 		break;
1142 	case TUNER_RTL2830_MT2060:
1143 		fe = dvb_attach(mt2060_attach, adap->fe[0],
1144 				dev->demod_i2c_adapter,
1145 				&rtl28xxu_mt2060_config, 1220);
1146 		break;
1147 	case TUNER_RTL2830_MXL5005S:
1148 		fe = dvb_attach(mxl5005s_attach, adap->fe[0],
1149 				dev->demod_i2c_adapter,
1150 				&rtl28xxu_mxl5005s_config);
1151 		break;
1152 	default:
1153 		fe = NULL;
1154 		dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1155 	}
1156 
1157 	if (fe == NULL) {
1158 		ret = -ENODEV;
1159 		goto err;
1160 	}
1161 
1162 	return 0;
1163 err:
1164 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1165 	return ret;
1166 }
1167 
1168 static const struct fc0012_config rtl2832u_fc0012_config = {
1169 	.i2c_address = 0x63, /* 0xc6 >> 1 */
1170 	.xtal_freq = FC_XTAL_28_8_MHZ,
1171 };
1172 
1173 static const struct r820t_config rtl2832u_r820t_config = {
1174 	.i2c_addr = 0x1a,
1175 	.xtal = 28800000,
1176 	.max_i2c_msg_len = 2,
1177 	.rafael_chip = CHIP_R820T,
1178 };
1179 
1180 static const struct r820t_config rtl2832u_r828d_config = {
1181 	.i2c_addr = 0x3a,
1182 	.xtal = 16000000,
1183 	.max_i2c_msg_len = 2,
1184 	.rafael_chip = CHIP_R828D,
1185 };
1186 
1187 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
1188 {
1189 	int ret;
1190 	struct dvb_usb_device *d = adap_to_d(adap);
1191 	struct rtl28xxu_dev *dev = d_to_priv(d);
1192 	struct dvb_frontend *fe = NULL;
1193 	struct i2c_board_info info;
1194 	struct i2c_client *client;
1195 	struct v4l2_subdev *subdev = NULL;
1196 	struct platform_device *pdev;
1197 	struct rtl2832_sdr_platform_data pdata;
1198 
1199 	dev_dbg(&d->intf->dev, "\n");
1200 
1201 	memset(&info, 0, sizeof(struct i2c_board_info));
1202 	memset(&pdata, 0, sizeof(pdata));
1203 
1204 	switch (dev->tuner) {
1205 	case TUNER_RTL2832_FC0012:
1206 		fe = dvb_attach(fc0012_attach, adap->fe[0],
1207 			dev->demod_i2c_adapter, &rtl2832u_fc0012_config);
1208 
1209 		/* since fc0012 includs reading the signal strength delegate
1210 		 * that to the tuner driver */
1211 		adap->fe[0]->ops.read_signal_strength =
1212 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1213 		break;
1214 	case TUNER_RTL2832_FC0013:
1215 		fe = dvb_attach(fc0013_attach, adap->fe[0],
1216 			dev->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
1217 
1218 		/* fc0013 also supports signal strength reading */
1219 		adap->fe[0]->ops.read_signal_strength =
1220 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1221 		break;
1222 	case TUNER_RTL2832_E4000: {
1223 			struct e4000_config e4000_config = {
1224 				.fe = adap->fe[0],
1225 				.clock = 28800000,
1226 			};
1227 
1228 			strscpy(info.type, "e4000", I2C_NAME_SIZE);
1229 			info.addr = 0x64;
1230 			info.platform_data = &e4000_config;
1231 
1232 			request_module(info.type);
1233 			client = i2c_new_device(dev->demod_i2c_adapter, &info);
1234 			if (client == NULL || client->dev.driver == NULL)
1235 				break;
1236 
1237 			if (!try_module_get(client->dev.driver->owner)) {
1238 				i2c_unregister_device(client);
1239 				break;
1240 			}
1241 
1242 			dev->i2c_client_tuner = client;
1243 			subdev = i2c_get_clientdata(client);
1244 		}
1245 		break;
1246 	case TUNER_RTL2832_FC2580: {
1247 			struct fc2580_platform_data fc2580_pdata = {
1248 				.dvb_frontend = adap->fe[0],
1249 			};
1250 			struct i2c_board_info board_info = {};
1251 
1252 			strscpy(board_info.type, "fc2580", I2C_NAME_SIZE);
1253 			board_info.addr = 0x56;
1254 			board_info.platform_data = &fc2580_pdata;
1255 			request_module("fc2580");
1256 			client = i2c_new_device(dev->demod_i2c_adapter,
1257 						&board_info);
1258 			if (client == NULL || client->dev.driver == NULL)
1259 				break;
1260 			if (!try_module_get(client->dev.driver->owner)) {
1261 				i2c_unregister_device(client);
1262 				break;
1263 			}
1264 			dev->i2c_client_tuner = client;
1265 			subdev = fc2580_pdata.get_v4l2_subdev(client);
1266 		}
1267 		break;
1268 	case TUNER_RTL2832_TUA9001: {
1269 		struct tua9001_platform_data tua9001_pdata = {
1270 			.dvb_frontend = adap->fe[0],
1271 		};
1272 		struct i2c_board_info board_info = {};
1273 
1274 		/* enable GPIO1 and GPIO4 as output */
1275 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1276 		if (ret)
1277 			goto err;
1278 
1279 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1280 		if (ret)
1281 			goto err;
1282 
1283 		strscpy(board_info.type, "tua9001", I2C_NAME_SIZE);
1284 		board_info.addr = 0x60;
1285 		board_info.platform_data = &tua9001_pdata;
1286 		request_module("tua9001");
1287 		client = i2c_new_device(dev->demod_i2c_adapter, &board_info);
1288 		if (client == NULL || client->dev.driver == NULL)
1289 			break;
1290 		if (!try_module_get(client->dev.driver->owner)) {
1291 			i2c_unregister_device(client);
1292 			break;
1293 		}
1294 		dev->i2c_client_tuner = client;
1295 		break;
1296 	}
1297 	case TUNER_RTL2832_R820T:
1298 		fe = dvb_attach(r820t_attach, adap->fe[0],
1299 				dev->demod_i2c_adapter,
1300 				&rtl2832u_r820t_config);
1301 
1302 		/* Use tuner to get the signal strength */
1303 		adap->fe[0]->ops.read_signal_strength =
1304 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1305 		break;
1306 	case TUNER_RTL2832_R828D:
1307 		fe = dvb_attach(r820t_attach, adap->fe[0],
1308 				dev->demod_i2c_adapter,
1309 				&rtl2832u_r828d_config);
1310 		adap->fe[0]->ops.read_signal_strength =
1311 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1312 
1313 		if (adap->fe[1]) {
1314 			fe = dvb_attach(r820t_attach, adap->fe[1],
1315 					dev->demod_i2c_adapter,
1316 					&rtl2832u_r828d_config);
1317 			adap->fe[1]->ops.read_signal_strength =
1318 					adap->fe[1]->ops.tuner_ops.get_rf_strength;
1319 		}
1320 		break;
1321 	case TUNER_RTL2832_SI2157: {
1322 			struct si2157_config si2157_config = {
1323 				.fe = adap->fe[0],
1324 				.if_port = 0,
1325 				.inversion = false,
1326 			};
1327 
1328 			strscpy(info.type, "si2157", I2C_NAME_SIZE);
1329 			info.addr = 0x60;
1330 			info.platform_data = &si2157_config;
1331 			request_module(info.type);
1332 			client = i2c_new_device(&d->i2c_adap, &info);
1333 			if (client == NULL || client->dev.driver == NULL)
1334 				break;
1335 
1336 			if (!try_module_get(client->dev.driver->owner)) {
1337 				i2c_unregister_device(client);
1338 				break;
1339 			}
1340 
1341 			dev->i2c_client_tuner = client;
1342 			subdev = i2c_get_clientdata(client);
1343 
1344 			/* copy tuner ops for 2nd FE as tuner is shared */
1345 			if (adap->fe[1]) {
1346 				adap->fe[1]->tuner_priv =
1347 						adap->fe[0]->tuner_priv;
1348 				memcpy(&adap->fe[1]->ops.tuner_ops,
1349 						&adap->fe[0]->ops.tuner_ops,
1350 						sizeof(struct dvb_tuner_ops));
1351 			}
1352 		}
1353 		break;
1354 	default:
1355 		dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1356 	}
1357 	if (fe == NULL && dev->i2c_client_tuner == NULL) {
1358 		ret = -ENODEV;
1359 		goto err;
1360 	}
1361 
1362 	/* register SDR */
1363 	switch (dev->tuner) {
1364 	case TUNER_RTL2832_FC2580:
1365 	case TUNER_RTL2832_FC0012:
1366 	case TUNER_RTL2832_FC0013:
1367 	case TUNER_RTL2832_E4000:
1368 	case TUNER_RTL2832_R820T:
1369 	case TUNER_RTL2832_R828D:
1370 		pdata.clk = dev->rtl2832_platform_data.clk;
1371 		pdata.tuner = dev->tuner;
1372 		pdata.regmap = dev->rtl2832_platform_data.regmap;
1373 		pdata.dvb_frontend = adap->fe[0];
1374 		pdata.dvb_usb_device = d;
1375 		pdata.v4l2_subdev = subdev;
1376 
1377 		request_module("%s", "rtl2832_sdr");
1378 		pdev = platform_device_register_data(&d->intf->dev,
1379 						     "rtl2832_sdr",
1380 						     PLATFORM_DEVID_AUTO,
1381 						     &pdata, sizeof(pdata));
1382 		if (IS_ERR(pdev) || pdev->dev.driver == NULL)
1383 			break;
1384 		dev->platform_device_sdr = pdev;
1385 		break;
1386 	default:
1387 		dev_dbg(&d->intf->dev, "no SDR for tuner=%d\n", dev->tuner);
1388 	}
1389 
1390 	return 0;
1391 err:
1392 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1393 	return ret;
1394 }
1395 
1396 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter *adap)
1397 {
1398 	struct rtl28xxu_dev *dev = adap_to_priv(adap);
1399 
1400 	if (dev->chip_id == CHIP_ID_RTL2831U)
1401 		return rtl2831u_tuner_attach(adap);
1402 	else
1403 		return rtl2832u_tuner_attach(adap);
1404 }
1405 
1406 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter *adap)
1407 {
1408 	struct dvb_usb_device *d = adap_to_d(adap);
1409 	struct rtl28xxu_dev *dev = d_to_priv(d);
1410 	struct i2c_client *client;
1411 	struct platform_device *pdev;
1412 
1413 	dev_dbg(&d->intf->dev, "\n");
1414 
1415 	/* remove platform SDR */
1416 	pdev = dev->platform_device_sdr;
1417 	if (pdev)
1418 		platform_device_unregister(pdev);
1419 
1420 	/* remove I2C tuner */
1421 	client = dev->i2c_client_tuner;
1422 	if (client) {
1423 		module_put(client->dev.driver->owner);
1424 		i2c_unregister_device(client);
1425 	}
1426 
1427 	return 0;
1428 }
1429 
1430 static int rtl28xxu_init(struct dvb_usb_device *d)
1431 {
1432 	int ret;
1433 	u8 val;
1434 
1435 	dev_dbg(&d->intf->dev, "\n");
1436 
1437 	/* init USB endpoints */
1438 	ret = rtl28xxu_rd_reg(d, USB_SYSCTL_0, &val);
1439 	if (ret)
1440 		goto err;
1441 
1442 	/* enable DMA and Full Packet Mode*/
1443 	val |= 0x09;
1444 	ret = rtl28xxu_wr_reg(d, USB_SYSCTL_0, val);
1445 	if (ret)
1446 		goto err;
1447 
1448 	/* set EPA maximum packet size to 0x0200 */
1449 	ret = rtl28xxu_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1450 	if (ret)
1451 		goto err;
1452 
1453 	/* change EPA FIFO length */
1454 	ret = rtl28xxu_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1455 	if (ret)
1456 		goto err;
1457 
1458 	return ret;
1459 err:
1460 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1461 	return ret;
1462 }
1463 
1464 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1465 {
1466 	int ret;
1467 	u8 gpio, sys0, epa_ctl[2];
1468 
1469 	dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1470 
1471 	/* demod adc */
1472 	ret = rtl28xxu_rd_reg(d, SYS_SYS0, &sys0);
1473 	if (ret)
1474 		goto err;
1475 
1476 	/* tuner power, read GPIOs */
1477 	ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1478 	if (ret)
1479 		goto err;
1480 
1481 	dev_dbg(&d->intf->dev, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1482 
1483 	if (onoff) {
1484 		gpio |= 0x01; /* GPIO0 = 1 */
1485 		gpio &= (~0x10); /* GPIO4 = 0 */
1486 		gpio |= 0x04; /* GPIO2 = 1, LED on */
1487 		sys0 = sys0 & 0x0f;
1488 		sys0 |= 0xe0;
1489 		epa_ctl[0] = 0x00; /* clear stall */
1490 		epa_ctl[1] = 0x00; /* clear reset */
1491 	} else {
1492 		gpio &= (~0x01); /* GPIO0 = 0 */
1493 		gpio |= 0x10; /* GPIO4 = 1 */
1494 		gpio &= (~0x04); /* GPIO2 = 1, LED off */
1495 		sys0 = sys0 & (~0xc0);
1496 		epa_ctl[0] = 0x10; /* set stall */
1497 		epa_ctl[1] = 0x02; /* set reset */
1498 	}
1499 
1500 	dev_dbg(&d->intf->dev, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1501 
1502 	/* demod adc */
1503 	ret = rtl28xxu_wr_reg(d, SYS_SYS0, sys0);
1504 	if (ret)
1505 		goto err;
1506 
1507 	/* tuner power, write GPIOs */
1508 	ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1509 	if (ret)
1510 		goto err;
1511 
1512 	/* streaming EP: stall & reset */
1513 	ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1514 	if (ret)
1515 		goto err;
1516 
1517 	if (onoff)
1518 		usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1519 
1520 	return ret;
1521 err:
1522 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1523 	return ret;
1524 }
1525 
1526 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1527 {
1528 	int ret;
1529 
1530 	dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1531 
1532 	if (onoff) {
1533 		/* GPIO3=1, GPIO4=0 */
1534 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1535 		if (ret)
1536 			goto err;
1537 
1538 		/* suspend? */
1539 		ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1540 		if (ret)
1541 			goto err;
1542 
1543 		/* enable PLL */
1544 		ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1545 		if (ret)
1546 			goto err;
1547 
1548 		/* disable reset */
1549 		ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1550 		if (ret)
1551 			goto err;
1552 
1553 		/* streaming EP: clear stall & reset */
1554 		ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1555 		if (ret)
1556 			goto err;
1557 
1558 		ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1559 		if (ret)
1560 			goto err;
1561 	} else {
1562 		/* GPIO4=1 */
1563 		ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1564 		if (ret)
1565 			goto err;
1566 
1567 		/* disable PLL */
1568 		ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1569 		if (ret)
1570 			goto err;
1571 
1572 		/* streaming EP: set stall & reset */
1573 		ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1574 		if (ret)
1575 			goto err;
1576 	}
1577 
1578 	return ret;
1579 err:
1580 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1581 	return ret;
1582 }
1583 
1584 static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
1585 {
1586 	struct rtl28xxu_dev *dev = d_to_priv(d);
1587 
1588 	if (dev->chip_id == CHIP_ID_RTL2831U)
1589 		return rtl2831u_power_ctrl(d, onoff);
1590 	else
1591 		return rtl2832u_power_ctrl(d, onoff);
1592 }
1593 
1594 static int rtl28xxu_frontend_ctrl(struct dvb_frontend *fe, int onoff)
1595 {
1596 	struct dvb_usb_device *d = fe_to_d(fe);
1597 	struct rtl28xxu_dev *dev = fe_to_priv(fe);
1598 	struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1599 	int ret;
1600 	u8 val;
1601 
1602 	dev_dbg(&d->intf->dev, "fe=%d onoff=%d\n", fe->id, onoff);
1603 
1604 	if (dev->chip_id == CHIP_ID_RTL2831U)
1605 		return 0;
1606 
1607 	if (fe->id == 0) {
1608 		/* control internal demod ADC */
1609 		if (onoff)
1610 			val = 0x48; /* enable ADC */
1611 		else
1612 			val = 0x00; /* disable ADC */
1613 
1614 		ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48);
1615 		if (ret)
1616 			goto err;
1617 	} else if (fe->id == 1) {
1618 		/* bypass slave demod TS through master demod */
1619 		ret = pdata->slave_ts_ctrl(dev->i2c_client_demod, onoff);
1620 		if (ret)
1621 			goto err;
1622 	}
1623 
1624 	return 0;
1625 err:
1626 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1627 	return ret;
1628 }
1629 
1630 #if IS_ENABLED(CONFIG_RC_CORE)
1631 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1632 {
1633 	int ret, i;
1634 	struct rtl28xxu_dev *dev = d->priv;
1635 	u8 buf[5];
1636 	u32 rc_code;
1637 	static const struct rtl28xxu_reg_val rc_nec_tab[] = {
1638 		{ 0x3033, 0x80 },
1639 		{ 0x3020, 0x43 },
1640 		{ 0x3021, 0x16 },
1641 		{ 0x3022, 0x16 },
1642 		{ 0x3023, 0x5a },
1643 		{ 0x3024, 0x2d },
1644 		{ 0x3025, 0x16 },
1645 		{ 0x3026, 0x01 },
1646 		{ 0x3028, 0xb0 },
1647 		{ 0x3029, 0x04 },
1648 		{ 0x302c, 0x88 },
1649 		{ 0x302e, 0x13 },
1650 		{ 0x3030, 0xdf },
1651 		{ 0x3031, 0x05 },
1652 	};
1653 
1654 	/* init remote controller */
1655 	if (!dev->rc_active) {
1656 		for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1657 			ret = rtl28xxu_wr_reg(d, rc_nec_tab[i].reg,
1658 					rc_nec_tab[i].val);
1659 			if (ret)
1660 				goto err;
1661 		}
1662 		dev->rc_active = true;
1663 	}
1664 
1665 	ret = rtl28xxu_rd_regs(d, SYS_IRRC_RP, buf, 5);
1666 	if (ret)
1667 		goto err;
1668 
1669 	if (buf[4] & 0x01) {
1670 		enum rc_proto proto;
1671 
1672 		if (buf[2] == (u8) ~buf[3]) {
1673 			if (buf[0] == (u8) ~buf[1]) {
1674 				/* NEC standard (16 bit) */
1675 				rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1676 				proto = RC_PROTO_NEC;
1677 			} else {
1678 				/* NEC extended (24 bit) */
1679 				rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1680 							   buf[2]);
1681 				proto = RC_PROTO_NECX;
1682 			}
1683 		} else {
1684 			/* NEC full (32 bit) */
1685 			rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1686 						    buf[2] << 8  | buf[3]);
1687 			proto = RC_PROTO_NEC32;
1688 		}
1689 
1690 		rc_keydown(d->rc_dev, proto, rc_code, 0);
1691 
1692 		ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1693 		if (ret)
1694 			goto err;
1695 
1696 		/* repeated intentionally to avoid extra keypress */
1697 		ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1698 		if (ret)
1699 			goto err;
1700 	}
1701 
1702 	return ret;
1703 err:
1704 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1705 	return ret;
1706 }
1707 
1708 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1709 		struct dvb_usb_rc *rc)
1710 {
1711 	rc->map_name = RC_MAP_EMPTY;
1712 	rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
1713 							RC_PROTO_BIT_NEC32;
1714 	rc->query = rtl2831u_rc_query;
1715 	rc->interval = 400;
1716 
1717 	return 0;
1718 }
1719 
1720 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1721 {
1722 	int ret, i, len;
1723 	struct rtl28xxu_dev *dev = d->priv;
1724 	struct ir_raw_event ev = {};
1725 	u8 buf[128];
1726 	static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1727 		{IR_RX_IF,               0x03, 0xff},
1728 		{IR_RX_BUF_CTRL,         0x80, 0xff},
1729 		{IR_RX_CTRL,             0x80, 0xff},
1730 	};
1731 
1732 	/* init remote controller */
1733 	if (!dev->rc_active) {
1734 		static const struct rtl28xxu_reg_val_mask init_tab[] = {
1735 			{SYS_DEMOD_CTL1,         0x00, 0x04},
1736 			{SYS_DEMOD_CTL1,         0x00, 0x08},
1737 			{USB_CTRL,               0x20, 0x20},
1738 			{SYS_GPIO_DIR,           0x00, 0x08},
1739 			{SYS_GPIO_OUT_EN,        0x08, 0x08},
1740 			{SYS_GPIO_OUT_VAL,       0x08, 0x08},
1741 			{IR_MAX_DURATION0,       0xd0, 0xff},
1742 			{IR_MAX_DURATION1,       0x07, 0xff},
1743 			{IR_IDLE_LEN0,           0xc0, 0xff},
1744 			{IR_IDLE_LEN1,           0x00, 0xff},
1745 			{IR_GLITCH_LEN,          0x03, 0xff},
1746 			{IR_RX_CLK,              0x09, 0xff},
1747 			{IR_RX_CFG,              0x1c, 0xff},
1748 			{IR_MAX_H_TOL_LEN,       0x1e, 0xff},
1749 			{IR_MAX_L_TOL_LEN,       0x1e, 0xff},
1750 			{IR_RX_CTRL,             0x80, 0xff},
1751 		};
1752 
1753 		for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1754 			ret = rtl28xxu_wr_reg_mask(d, init_tab[i].reg,
1755 					init_tab[i].val, init_tab[i].mask);
1756 			if (ret)
1757 				goto err;
1758 		}
1759 
1760 		dev->rc_active = true;
1761 	}
1762 
1763 	ret = rtl28xxu_rd_reg(d, IR_RX_IF, &buf[0]);
1764 	if (ret)
1765 		goto err;
1766 
1767 	if (buf[0] != 0x83)
1768 		goto exit;
1769 
1770 	ret = rtl28xxu_rd_reg(d, IR_RX_BC, &buf[0]);
1771 	if (ret || buf[0] > sizeof(buf))
1772 		goto err;
1773 
1774 	len = buf[0];
1775 
1776 	/* read raw code from hw */
1777 	ret = rtl28xxu_rd_regs(d, IR_RX_BUF, buf, len);
1778 	if (ret)
1779 		goto err;
1780 
1781 	/* let hw receive new code */
1782 	for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1783 		ret = rtl28xxu_wr_reg_mask(d, refresh_tab[i].reg,
1784 				refresh_tab[i].val, refresh_tab[i].mask);
1785 		if (ret)
1786 			goto err;
1787 	}
1788 
1789 	/* pass data to Kernel IR decoder */
1790 	for (i = 0; i < len; i++) {
1791 		ev.pulse = buf[i] >> 7;
1792 		ev.duration = 50800 * (buf[i] & 0x7f);
1793 		ir_raw_event_store_with_filter(d->rc_dev, &ev);
1794 	}
1795 
1796 	/* 'flush' ir_raw_event_store_with_filter() */
1797 	ir_raw_event_set_idle(d->rc_dev, true);
1798 	ir_raw_event_handle(d->rc_dev);
1799 exit:
1800 	return ret;
1801 err:
1802 	dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1803 	return ret;
1804 }
1805 
1806 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1807 		struct dvb_usb_rc *rc)
1808 {
1809 	/* disable IR interrupts in order to avoid SDR sample loss */
1810 	if (rtl28xxu_disable_rc)
1811 		return rtl28xxu_wr_reg(d, IR_RX_IE, 0x00);
1812 
1813 	/* load empty to enable rc */
1814 	if (!rc->map_name)
1815 		rc->map_name = RC_MAP_EMPTY;
1816 	rc->allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
1817 	rc->driver_type = RC_DRIVER_IR_RAW;
1818 	rc->query = rtl2832u_rc_query;
1819 	rc->interval = 200;
1820 
1821 	return 0;
1822 }
1823 
1824 static int rtl28xxu_get_rc_config(struct dvb_usb_device *d,
1825 		struct dvb_usb_rc *rc)
1826 {
1827 	struct rtl28xxu_dev *dev = d_to_priv(d);
1828 
1829 	if (dev->chip_id == CHIP_ID_RTL2831U)
1830 		return rtl2831u_get_rc_config(d, rc);
1831 	else
1832 		return rtl2832u_get_rc_config(d, rc);
1833 }
1834 #else
1835 #define rtl28xxu_get_rc_config NULL
1836 #endif
1837 
1838 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1839 {
1840 	struct rtl28xxu_dev *dev = adap_to_priv(adap);
1841 
1842 	if (dev->chip_id == CHIP_ID_RTL2831U) {
1843 		struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1844 
1845 		return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1846 	} else {
1847 		struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1848 
1849 		return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1850 	}
1851 }
1852 
1853 static int rtl28xxu_pid_filter(struct dvb_usb_adapter *adap, int index,
1854 			       u16 pid, int onoff)
1855 {
1856 	struct rtl28xxu_dev *dev = adap_to_priv(adap);
1857 
1858 	if (dev->chip_id == CHIP_ID_RTL2831U) {
1859 		struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1860 
1861 		return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1862 	} else {
1863 		struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1864 
1865 		return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1866 	}
1867 }
1868 
1869 static const struct dvb_usb_device_properties rtl28xxu_props = {
1870 	.driver_name = KBUILD_MODNAME,
1871 	.owner = THIS_MODULE,
1872 	.adapter_nr = adapter_nr,
1873 	.size_of_priv = sizeof(struct rtl28xxu_dev),
1874 
1875 	.identify_state = rtl28xxu_identify_state,
1876 	.power_ctrl = rtl28xxu_power_ctrl,
1877 	.frontend_ctrl = rtl28xxu_frontend_ctrl,
1878 	.i2c_algo = &rtl28xxu_i2c_algo,
1879 	.read_config = rtl28xxu_read_config,
1880 	.frontend_attach = rtl28xxu_frontend_attach,
1881 	.frontend_detach = rtl28xxu_frontend_detach,
1882 	.tuner_attach = rtl28xxu_tuner_attach,
1883 	.tuner_detach = rtl28xxu_tuner_detach,
1884 	.init = rtl28xxu_init,
1885 	.get_rc_config = rtl28xxu_get_rc_config,
1886 
1887 	.num_adapters = 1,
1888 	.adapter = {
1889 		{
1890 			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
1891 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1892 
1893 			.pid_filter_count = 32,
1894 			.pid_filter_ctrl = rtl28xxu_pid_filter_ctrl,
1895 			.pid_filter = rtl28xxu_pid_filter,
1896 
1897 			.stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1898 		},
1899 	},
1900 };
1901 
1902 static const struct usb_device_id rtl28xxu_id_table[] = {
1903 	/* RTL2831U devices: */
1904 	{ DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1905 		&rtl28xxu_props, "Realtek RTL2831U reference design", NULL) },
1906 	{ DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1907 		&rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1908 	{ DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1909 		&rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1910 
1911 	/* RTL2832U devices: */
1912 	{ DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1913 		&rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1914 	{ DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1915 		&rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1916 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1917 		&rtl28xxu_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1918 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1919 		&rtl28xxu_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1920 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1921 		&rtl28xxu_props, "TerraTec NOXON DAB Stick", NULL) },
1922 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1923 		&rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1924 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1925 		&rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1926 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1927 		&rtl28xxu_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1928 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1929 		&rtl28xxu_props, "Dexatek DK DVB-T Dongle", NULL) },
1930 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1931 		&rtl28xxu_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1932 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1933 		&rtl28xxu_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1934 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS_PLUS,
1935 		&rtl28xxu_props, "Leadtek WinFast DTV2000DS Plus", RC_MAP_LEADTEK_Y04G0051) },
1936 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1937 		&rtl28xxu_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1938 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1939 		&rtl28xxu_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1940 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1941 		&rtl28xxu_props, "TerraTec Cinergy T Stick+", NULL) },
1942 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1943 		&rtl28xxu_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1944 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1945 		&rtl28xxu_props, "GIGABYTE U7300", NULL) },
1946 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1947 		&rtl28xxu_props, "MSI DIGIVOX Micro HD", NULL) },
1948 	{ DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1949 		&rtl28xxu_props, "Compro VideoMate U620F", NULL) },
1950 	{ DVB_USB_DEVICE(USB_VID_COMPRO, 0x0650,
1951 		&rtl28xxu_props, "Compro VideoMate U650F", NULL) },
1952 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1953 		&rtl28xxu_props, "MaxMedia HU394-T", NULL) },
1954 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1955 		&rtl28xxu_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1956 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1957 		&rtl28xxu_props, "Crypto ReDi PC 50 A", NULL) },
1958 	{ DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1959 		&rtl28xxu_props, "Genius TVGo DVB-T03", NULL) },
1960 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1961 		&rtl28xxu_props, "Peak DVB-T USB", NULL) },
1962 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1963 		&rtl28xxu_props, "Sveon STV20", NULL) },
1964 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1965 		&rtl28xxu_props, "Sveon STV21", NULL) },
1966 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1967 		&rtl28xxu_props, "Sveon STV27", NULL) },
1968 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TURBOX_DTT_2000,
1969 		&rtl28xxu_props, "TURBO-X Pure TV Tuner DTT-2000", NULL) },
1970 
1971 	/* RTL2832P devices: */
1972 	{ DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1973 		&rtl28xxu_props, "Astrometa DVB-T2", NULL) },
1974 	{ DVB_USB_DEVICE(0x5654, 0xca42,
1975 		&rtl28xxu_props, "GoTView MasterHD 3", NULL) },
1976 	{ }
1977 };
1978 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1979 
1980 static struct usb_driver rtl28xxu_usb_driver = {
1981 	.name = KBUILD_MODNAME,
1982 	.id_table = rtl28xxu_id_table,
1983 	.probe = dvb_usbv2_probe,
1984 	.disconnect = dvb_usbv2_disconnect,
1985 	.suspend = dvb_usbv2_suspend,
1986 	.resume = dvb_usbv2_resume,
1987 	.reset_resume = dvb_usbv2_reset_resume,
1988 	.no_dynamic_id = 1,
1989 	.soft_unbind = 1,
1990 };
1991 
1992 module_usb_driver(rtl28xxu_usb_driver);
1993 
1994 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1995 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1996 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1997 MODULE_LICENSE("GPL");
1998