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