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 #include "rtl2830.h"
26 #include "rtl2832.h"
27 
28 #include "qt1010.h"
29 #include "mt2060.h"
30 #include "mxl5005s.h"
31 #include "fc0012.h"
32 #include "fc0013.h"
33 #include "e4000.h"
34 #include "fc2580.h"
35 #include "tua9001.h"
36 #include "r820t.h"
37 
38 /*
39  * RTL2832_SDR module is in staging. That logic is added in order to avoid any
40  * hard dependency to drivers/staging/ directory as we want compile mainline
41  * driver even whole staging directory is missing.
42  */
43 #include <media/v4l2-subdev.h>
44 
45 #if IS_ENABLED(CONFIG_DVB_RTL2832_SDR)
46 struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
47 	struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
48 	struct v4l2_subdev *sd);
49 #else
50 static inline struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
51 	struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
52 	struct v4l2_subdev *sd)
53 {
54 	return NULL;
55 }
56 #endif
57 
58 #ifdef CONFIG_MEDIA_ATTACH
59 #define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
60 	void *__r = NULL; \
61 	typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
62 	if (__a) { \
63 		__r = (void *) __a(ARGS); \
64 		if (__r == NULL) \
65 			symbol_put(FUNCTION); \
66 	} \
67 	__r; \
68 })
69 
70 #else
71 #define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
72 	FUNCTION(ARGS); \
73 })
74 
75 #endif
76 
77 static int rtl28xxu_disable_rc;
78 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644);
79 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller");
80 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
81 
82 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
83 {
84 	int ret;
85 	unsigned int pipe;
86 	u8 requesttype;
87 	u8 *buf;
88 
89 	buf = kmalloc(req->size, GFP_KERNEL);
90 	if (!buf) {
91 		ret = -ENOMEM;
92 		goto err;
93 	}
94 
95 	if (req->index & CMD_WR_FLAG) {
96 		/* write */
97 		memcpy(buf, req->data, req->size);
98 		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
99 		pipe = usb_sndctrlpipe(d->udev, 0);
100 	} else {
101 		/* read */
102 		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
103 		pipe = usb_rcvctrlpipe(d->udev, 0);
104 	}
105 
106 	ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
107 			req->index, buf, req->size, 1000);
108 
109 	dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
110 			req->index, buf, req->size);
111 
112 	if (ret > 0)
113 		ret = 0;
114 
115 	/* read request, copy returned data to return buf */
116 	if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
117 		memcpy(req->data, buf, req->size);
118 
119 	kfree(buf);
120 
121 	if (ret)
122 		goto err;
123 
124 	return ret;
125 err:
126 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
127 	return ret;
128 }
129 
130 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
131 {
132 	struct rtl28xxu_req req;
133 
134 	if (reg < 0x3000)
135 		req.index = CMD_USB_WR;
136 	else if (reg < 0x4000)
137 		req.index = CMD_SYS_WR;
138 	else
139 		req.index = CMD_IR_WR;
140 
141 	req.value = reg;
142 	req.size = len;
143 	req.data = val;
144 
145 	return rtl28xxu_ctrl_msg(d, &req);
146 }
147 
148 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
149 {
150 	struct rtl28xxu_req req;
151 
152 	if (reg < 0x3000)
153 		req.index = CMD_USB_RD;
154 	else if (reg < 0x4000)
155 		req.index = CMD_SYS_RD;
156 	else
157 		req.index = CMD_IR_RD;
158 
159 	req.value = reg;
160 	req.size = len;
161 	req.data = val;
162 
163 	return rtl28xxu_ctrl_msg(d, &req);
164 }
165 
166 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
167 {
168 	return rtl28xx_wr_regs(d, reg, &val, 1);
169 }
170 
171 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
172 {
173 	return rtl2831_rd_regs(d, reg, val, 1);
174 }
175 
176 static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
177 		u8 mask)
178 {
179 	int ret;
180 	u8 tmp;
181 
182 	/* no need for read if whole reg is written */
183 	if (mask != 0xff) {
184 		ret = rtl28xx_rd_reg(d, reg, &tmp);
185 		if (ret)
186 			return ret;
187 
188 		val &= mask;
189 		tmp &= ~mask;
190 		val |= tmp;
191 	}
192 
193 	return rtl28xx_wr_reg(d, reg, val);
194 }
195 
196 /* I2C */
197 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
198 	int num)
199 {
200 	int ret;
201 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
202 	struct rtl28xxu_priv *priv = d->priv;
203 	struct rtl28xxu_req req;
204 
205 	/*
206 	 * It is not known which are real I2C bus xfer limits, but testing
207 	 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
208 	 * TODO: find out RTL2832U lens
209 	 */
210 
211 	/*
212 	 * I2C adapter logic looks rather complicated due to fact it handles
213 	 * three different access methods. Those methods are;
214 	 * 1) integrated demod access
215 	 * 2) old I2C access
216 	 * 3) new I2C access
217 	 *
218 	 * Used method is selected in order 1, 2, 3. Method 3 can handle all
219 	 * requests but there is two reasons why not use it always;
220 	 * 1) It is most expensive, usually two USB messages are needed
221 	 * 2) At least RTL2831U does not support it
222 	 *
223 	 * Method 3 is needed in case of I2C write+read (typical register read)
224 	 * where write is more than one byte.
225 	 */
226 
227 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
228 		return -EAGAIN;
229 
230 	if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
231 		(msg[1].flags & I2C_M_RD)) {
232 		if (msg[0].len > 24 || msg[1].len > 24) {
233 			/* TODO: check msg[0].len max */
234 			ret = -EOPNOTSUPP;
235 			goto err_mutex_unlock;
236 		} else if (msg[0].addr == 0x10) {
237 			/* method 1 - integrated demod */
238 			req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
239 			req.index = CMD_DEMOD_RD | priv->page;
240 			req.size = msg[1].len;
241 			req.data = &msg[1].buf[0];
242 			ret = rtl28xxu_ctrl_msg(d, &req);
243 		} else if (msg[0].len < 2) {
244 			/* method 2 - old I2C */
245 			req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
246 			req.index = CMD_I2C_RD;
247 			req.size = msg[1].len;
248 			req.data = &msg[1].buf[0];
249 			ret = rtl28xxu_ctrl_msg(d, &req);
250 		} else {
251 			/* method 3 - new I2C */
252 			req.value = (msg[0].addr << 1);
253 			req.index = CMD_I2C_DA_WR;
254 			req.size = msg[0].len;
255 			req.data = msg[0].buf;
256 			ret = rtl28xxu_ctrl_msg(d, &req);
257 			if (ret)
258 				goto err_mutex_unlock;
259 
260 			req.value = (msg[0].addr << 1);
261 			req.index = CMD_I2C_DA_RD;
262 			req.size = msg[1].len;
263 			req.data = msg[1].buf;
264 			ret = rtl28xxu_ctrl_msg(d, &req);
265 		}
266 	} else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
267 		if (msg[0].len > 22) {
268 			/* TODO: check msg[0].len max */
269 			ret = -EOPNOTSUPP;
270 			goto err_mutex_unlock;
271 		} else if (msg[0].addr == 0x10) {
272 			/* method 1 - integrated demod */
273 			if (msg[0].buf[0] == 0x00) {
274 				/* save demod page for later demod access */
275 				priv->page = msg[0].buf[1];
276 				ret = 0;
277 			} else {
278 				req.value = (msg[0].buf[0] << 8) |
279 					(msg[0].addr << 1);
280 				req.index = CMD_DEMOD_WR | priv->page;
281 				req.size = msg[0].len-1;
282 				req.data = &msg[0].buf[1];
283 				ret = rtl28xxu_ctrl_msg(d, &req);
284 			}
285 		} else if (msg[0].len < 23) {
286 			/* method 2 - old I2C */
287 			req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
288 			req.index = CMD_I2C_WR;
289 			req.size = msg[0].len-1;
290 			req.data = &msg[0].buf[1];
291 			ret = rtl28xxu_ctrl_msg(d, &req);
292 		} else {
293 			/* method 3 - new I2C */
294 			req.value = (msg[0].addr << 1);
295 			req.index = CMD_I2C_DA_WR;
296 			req.size = msg[0].len;
297 			req.data = msg[0].buf;
298 			ret = rtl28xxu_ctrl_msg(d, &req);
299 		}
300 	} else {
301 		ret = -EINVAL;
302 	}
303 
304 err_mutex_unlock:
305 	mutex_unlock(&d->i2c_mutex);
306 
307 	return ret ? ret : num;
308 }
309 
310 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
311 {
312 	return I2C_FUNC_I2C;
313 }
314 
315 static struct i2c_algorithm rtl28xxu_i2c_algo = {
316 	.master_xfer   = rtl28xxu_i2c_xfer,
317 	.functionality = rtl28xxu_i2c_func,
318 };
319 
320 static int rtl2831u_read_config(struct dvb_usb_device *d)
321 {
322 	struct rtl28xxu_priv *priv = d_to_priv(d);
323 	int ret;
324 	u8 buf[1];
325 	/* open RTL2831U/RTL2830 I2C gate */
326 	struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
327 	/* tuner probes */
328 	struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
329 	struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
330 
331 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
332 
333 	/*
334 	 * RTL2831U GPIOs
335 	 * =========================================================
336 	 * GPIO0 | tuner#0 | 0 off | 1 on  | MXL5005S (?)
337 	 * GPIO2 | LED     | 0 off | 1 on  |
338 	 * GPIO4 | tuner#1 | 0 on  | 1 off | MT2060
339 	 */
340 
341 	/* GPIO direction */
342 	ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
343 	if (ret)
344 		goto err;
345 
346 	/* enable as output GPIO0, GPIO2, GPIO4 */
347 	ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
348 	if (ret)
349 		goto err;
350 
351 	/*
352 	 * Probe used tuner. We need to know used tuner before demod attach
353 	 * since there is some demod params needed to set according to tuner.
354 	 */
355 
356 	/* demod needs some time to wake up */
357 	msleep(20);
358 
359 	priv->tuner_name = "NONE";
360 
361 	/* open demod I2C gate */
362 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
363 	if (ret)
364 		goto err;
365 
366 	/* check QT1010 ID(?) register; reg=0f val=2c */
367 	ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
368 	if (ret == 0 && buf[0] == 0x2c) {
369 		priv->tuner = TUNER_RTL2830_QT1010;
370 		priv->tuner_name = "QT1010";
371 		goto found;
372 	}
373 
374 	/* open demod I2C gate */
375 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
376 	if (ret)
377 		goto err;
378 
379 	/* check MT2060 ID register; reg=00 val=63 */
380 	ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
381 	if (ret == 0 && buf[0] == 0x63) {
382 		priv->tuner = TUNER_RTL2830_MT2060;
383 		priv->tuner_name = "MT2060";
384 		goto found;
385 	}
386 
387 	/* assume MXL5005S */
388 	priv->tuner = TUNER_RTL2830_MXL5005S;
389 	priv->tuner_name = "MXL5005S";
390 	goto found;
391 
392 found:
393 	dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
394 
395 	return 0;
396 err:
397 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
398 	return ret;
399 }
400 
401 static int rtl2832u_read_config(struct dvb_usb_device *d)
402 {
403 	struct rtl28xxu_priv *priv = d_to_priv(d);
404 	int ret;
405 	u8 buf[2];
406 	/* open RTL2832U/RTL2832 I2C gate */
407 	struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
408 	/* close RTL2832U/RTL2832 I2C gate */
409 	struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
410 	/* tuner probes */
411 	struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
412 	struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
413 	struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
414 	struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
415 	struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
416 	struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
417 	struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
418 	struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
419 	struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
420 	struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
421 	struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
422 	struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
423 
424 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
425 
426 	/* enable GPIO3 and GPIO6 as output */
427 	ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
428 	if (ret)
429 		goto err;
430 
431 	ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
432 	if (ret)
433 		goto err;
434 
435 	/*
436 	 * Probe used tuner. We need to know used tuner before demod attach
437 	 * since there is some demod params needed to set according to tuner.
438 	 */
439 
440 	/* open demod I2C gate */
441 	ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
442 	if (ret)
443 		goto err;
444 
445 	priv->tuner_name = "NONE";
446 
447 	/* check FC0012 ID register; reg=00 val=a1 */
448 	ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
449 	if (ret == 0 && buf[0] == 0xa1) {
450 		priv->tuner = TUNER_RTL2832_FC0012;
451 		priv->tuner_name = "FC0012";
452 		goto found;
453 	}
454 
455 	/* check FC0013 ID register; reg=00 val=a3 */
456 	ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
457 	if (ret == 0 && buf[0] == 0xa3) {
458 		priv->tuner = TUNER_RTL2832_FC0013;
459 		priv->tuner_name = "FC0013";
460 		goto found;
461 	}
462 
463 	/* check MT2266 ID register; reg=00 val=85 */
464 	ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
465 	if (ret == 0 && buf[0] == 0x85) {
466 		priv->tuner = TUNER_RTL2832_MT2266;
467 		priv->tuner_name = "MT2266";
468 		goto found;
469 	}
470 
471 	/* check FC2580 ID register; reg=01 val=56 */
472 	ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
473 	if (ret == 0 && buf[0] == 0x56) {
474 		priv->tuner = TUNER_RTL2832_FC2580;
475 		priv->tuner_name = "FC2580";
476 		goto found;
477 	}
478 
479 	/* check MT2063 ID register; reg=00 val=9e || 9c */
480 	ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
481 	if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
482 		priv->tuner = TUNER_RTL2832_MT2063;
483 		priv->tuner_name = "MT2063";
484 		goto found;
485 	}
486 
487 	/* check MAX3543 ID register; reg=00 val=38 */
488 	ret = rtl28xxu_ctrl_msg(d, &req_max3543);
489 	if (ret == 0 && buf[0] == 0x38) {
490 		priv->tuner = TUNER_RTL2832_MAX3543;
491 		priv->tuner_name = "MAX3543";
492 		goto found;
493 	}
494 
495 	/* check TUA9001 ID register; reg=7e val=2328 */
496 	ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
497 	if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
498 		priv->tuner = TUNER_RTL2832_TUA9001;
499 		priv->tuner_name = "TUA9001";
500 		goto found;
501 	}
502 
503 	/* check MXL5007R ID register; reg=d9 val=14 */
504 	ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
505 	if (ret == 0 && buf[0] == 0x14) {
506 		priv->tuner = TUNER_RTL2832_MXL5007T;
507 		priv->tuner_name = "MXL5007T";
508 		goto found;
509 	}
510 
511 	/* check E4000 ID register; reg=02 val=40 */
512 	ret = rtl28xxu_ctrl_msg(d, &req_e4000);
513 	if (ret == 0 && buf[0] == 0x40) {
514 		priv->tuner = TUNER_RTL2832_E4000;
515 		priv->tuner_name = "E4000";
516 		goto found;
517 	}
518 
519 	/* check TDA18272 ID register; reg=00 val=c760  */
520 	ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
521 	if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
522 		priv->tuner = TUNER_RTL2832_TDA18272;
523 		priv->tuner_name = "TDA18272";
524 		goto found;
525 	}
526 
527 	/* check R820T ID register; reg=00 val=69 */
528 	ret = rtl28xxu_ctrl_msg(d, &req_r820t);
529 	if (ret == 0 && buf[0] == 0x69) {
530 		priv->tuner = TUNER_RTL2832_R820T;
531 		priv->tuner_name = "R820T";
532 		goto found;
533 	}
534 
535 	/* check R828D ID register; reg=00 val=69 */
536 	ret = rtl28xxu_ctrl_msg(d, &req_r828d);
537 	if (ret == 0 && buf[0] == 0x69) {
538 		priv->tuner = TUNER_RTL2832_R828D;
539 		priv->tuner_name = "R828D";
540 		goto found;
541 	}
542 
543 
544 found:
545 	dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
546 
547 	/* close demod I2C gate */
548 	ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
549 	if (ret < 0)
550 		goto err;
551 
552 	return 0;
553 err:
554 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
555 	return ret;
556 }
557 
558 static const struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
559 	.i2c_addr = 0x10, /* 0x20 */
560 	.xtal = 28800000,
561 	.ts_mode = 0,
562 	.spec_inv = 1,
563 	.vtop = 0x20,
564 	.krf = 0x04,
565 	.agc_targ_val = 0x2d,
566 
567 };
568 
569 static const struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
570 	.i2c_addr = 0x10, /* 0x20 */
571 	.xtal = 28800000,
572 	.ts_mode = 0,
573 	.spec_inv = 1,
574 	.vtop = 0x20,
575 	.krf = 0x04,
576 	.agc_targ_val = 0x2d,
577 };
578 
579 static const struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
580 	.i2c_addr = 0x10, /* 0x20 */
581 	.xtal = 28800000,
582 	.ts_mode = 0,
583 	.spec_inv = 0,
584 	.vtop = 0x3f,
585 	.krf = 0x04,
586 	.agc_targ_val = 0x3e,
587 };
588 
589 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
590 {
591 	struct dvb_usb_device *d = adap_to_d(adap);
592 	struct rtl28xxu_priv *priv = d_to_priv(d);
593 	const struct rtl2830_config *rtl2830_config;
594 	int ret;
595 
596 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
597 
598 	switch (priv->tuner) {
599 	case TUNER_RTL2830_QT1010:
600 		rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
601 		break;
602 	case TUNER_RTL2830_MT2060:
603 		rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
604 		break;
605 	case TUNER_RTL2830_MXL5005S:
606 		rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
607 		break;
608 	default:
609 		dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
610 				KBUILD_MODNAME, priv->tuner_name);
611 		ret = -ENODEV;
612 		goto err;
613 	}
614 
615 	/* attach demodulator */
616 	adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
617 	if (!adap->fe[0]) {
618 		ret = -ENODEV;
619 		goto err;
620 	}
621 
622 	return 0;
623 err:
624 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
625 	return ret;
626 }
627 
628 static const struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
629 	.i2c_addr = 0x10, /* 0x20 */
630 	.xtal = 28800000,
631 	.tuner = TUNER_RTL2832_FC0012
632 };
633 
634 static const struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
635 	.i2c_addr = 0x10, /* 0x20 */
636 	.xtal = 28800000,
637 	.tuner = TUNER_RTL2832_FC0013
638 };
639 
640 static const struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
641 	.i2c_addr = 0x10, /* 0x20 */
642 	.xtal = 28800000,
643 	.tuner = TUNER_RTL2832_TUA9001,
644 };
645 
646 static const struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
647 	.i2c_addr = 0x10, /* 0x20 */
648 	.xtal = 28800000,
649 	.tuner = TUNER_RTL2832_E4000,
650 };
651 
652 static const struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
653 	.i2c_addr = 0x10,
654 	.xtal = 28800000,
655 	.tuner = TUNER_RTL2832_R820T,
656 };
657 
658 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
659 		int cmd, int arg)
660 {
661 	int ret;
662 	u8 val;
663 
664 	dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
665 
666 	switch (cmd) {
667 	case FC_FE_CALLBACK_VHF_ENABLE:
668 		/* set output values */
669 		ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
670 		if (ret)
671 			goto err;
672 
673 		if (arg)
674 			val &= 0xbf; /* set GPIO6 low */
675 		else
676 			val |= 0x40; /* set GPIO6 high */
677 
678 
679 		ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
680 		if (ret)
681 			goto err;
682 		break;
683 	default:
684 		ret = -EINVAL;
685 		goto err;
686 	}
687 	return 0;
688 err:
689 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
690 	return ret;
691 }
692 
693 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
694 		int cmd, int arg)
695 {
696 	int ret;
697 	u8 val;
698 
699 	dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
700 
701 	/*
702 	 * CEN     always enabled by hardware wiring
703 	 * RESETN  GPIO4
704 	 * RXEN    GPIO1
705 	 */
706 
707 	switch (cmd) {
708 	case TUA9001_CMD_RESETN:
709 		if (arg)
710 			val = (1 << 4);
711 		else
712 			val = (0 << 4);
713 
714 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
715 		if (ret)
716 			goto err;
717 		break;
718 	case TUA9001_CMD_RXEN:
719 		if (arg)
720 			val = (1 << 1);
721 		else
722 			val = (0 << 1);
723 
724 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
725 		if (ret)
726 			goto err;
727 		break;
728 	}
729 
730 	return 0;
731 err:
732 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
733 	return ret;
734 }
735 
736 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
737 {
738 	struct rtl28xxu_priv *priv = d->priv;
739 
740 	switch (priv->tuner) {
741 	case TUNER_RTL2832_FC0012:
742 		return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
743 	case TUNER_RTL2832_TUA9001:
744 		return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
745 	default:
746 		break;
747 	}
748 
749 	return 0;
750 }
751 
752 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
753 		int cmd, int arg)
754 {
755 	struct i2c_adapter *adap = adapter_priv;
756 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
757 
758 	dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
759 			__func__, component, cmd, arg);
760 
761 	switch (component) {
762 	case DVB_FRONTEND_COMPONENT_TUNER:
763 		return rtl2832u_tuner_callback(d, cmd, arg);
764 	default:
765 		break;
766 	}
767 
768 	return 0;
769 }
770 
771 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
772 {
773 	int ret;
774 	struct dvb_usb_device *d = adap_to_d(adap);
775 	struct rtl28xxu_priv *priv = d_to_priv(d);
776 	const struct rtl2832_config *rtl2832_config;
777 
778 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
779 
780 	switch (priv->tuner) {
781 	case TUNER_RTL2832_FC0012:
782 		rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
783 		break;
784 	case TUNER_RTL2832_FC0013:
785 		rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
786 		break;
787 	case TUNER_RTL2832_FC2580:
788 		/* FIXME: do not abuse fc0012 settings */
789 		rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
790 		break;
791 	case TUNER_RTL2832_TUA9001:
792 		rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
793 		break;
794 	case TUNER_RTL2832_E4000:
795 		rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
796 		break;
797 	case TUNER_RTL2832_R820T:
798 	case TUNER_RTL2832_R828D:
799 		rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
800 		break;
801 	default:
802 		dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
803 				KBUILD_MODNAME, priv->tuner_name);
804 		ret = -ENODEV;
805 		goto err;
806 	}
807 
808 	/* attach demodulator */
809 	adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
810 	if (!adap->fe[0]) {
811 		ret = -ENODEV;
812 		goto err;
813 	}
814 
815 	/* RTL2832 I2C repeater */
816 	priv->demod_i2c_adapter = rtl2832_get_i2c_adapter(adap->fe[0]);
817 
818 	/* set fe callback */
819 	adap->fe[0]->callback = rtl2832u_frontend_callback;
820 
821 	return 0;
822 err:
823 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
824 	return ret;
825 }
826 
827 static struct qt1010_config rtl28xxu_qt1010_config = {
828 	.i2c_address = 0x62, /* 0xc4 */
829 };
830 
831 static struct mt2060_config rtl28xxu_mt2060_config = {
832 	.i2c_address = 0x60, /* 0xc0 */
833 	.clock_out = 0,
834 };
835 
836 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
837 	.i2c_address     = 0x63, /* 0xc6 */
838 	.if_freq         = IF_FREQ_4570000HZ,
839 	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
840 	.agc_mode        = MXL_SINGLE_AGC,
841 	.tracking_filter = MXL_TF_C_H,
842 	.rssi_enable     = MXL_RSSI_ENABLE,
843 	.cap_select      = MXL_CAP_SEL_ENABLE,
844 	.div_out         = MXL_DIV_OUT_4,
845 	.clock_out       = MXL_CLOCK_OUT_DISABLE,
846 	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
847 	.top		 = MXL5005S_TOP_25P2,
848 	.mod_mode        = MXL_DIGITAL_MODE,
849 	.if_mode         = MXL_ZERO_IF,
850 	.AgcMasterByte   = 0x00,
851 };
852 
853 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
854 {
855 	int ret;
856 	struct dvb_usb_device *d = adap_to_d(adap);
857 	struct rtl28xxu_priv *priv = d_to_priv(d);
858 	struct i2c_adapter *rtl2830_tuner_i2c;
859 	struct dvb_frontend *fe;
860 
861 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
862 
863 	/* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
864 	rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
865 
866 	switch (priv->tuner) {
867 	case TUNER_RTL2830_QT1010:
868 		fe = dvb_attach(qt1010_attach, adap->fe[0],
869 				rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
870 		break;
871 	case TUNER_RTL2830_MT2060:
872 		fe = dvb_attach(mt2060_attach, adap->fe[0],
873 				rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
874 				1220);
875 		break;
876 	case TUNER_RTL2830_MXL5005S:
877 		fe = dvb_attach(mxl5005s_attach, adap->fe[0],
878 				rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
879 		break;
880 	default:
881 		fe = NULL;
882 		dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
883 				priv->tuner);
884 	}
885 
886 	if (fe == NULL) {
887 		ret = -ENODEV;
888 		goto err;
889 	}
890 
891 	return 0;
892 err:
893 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
894 	return ret;
895 }
896 
897 static const struct fc2580_config rtl2832u_fc2580_config = {
898 	.i2c_addr = 0x56,
899 	.clock = 16384000,
900 };
901 
902 static struct tua9001_config rtl2832u_tua9001_config = {
903 	.i2c_addr = 0x60,
904 };
905 
906 static const struct fc0012_config rtl2832u_fc0012_config = {
907 	.i2c_address = 0x63, /* 0xc6 >> 1 */
908 	.xtal_freq = FC_XTAL_28_8_MHZ,
909 };
910 
911 static const struct r820t_config rtl2832u_r820t_config = {
912 	.i2c_addr = 0x1a,
913 	.xtal = 28800000,
914 	.max_i2c_msg_len = 2,
915 	.rafael_chip = CHIP_R820T,
916 };
917 
918 static const struct r820t_config rtl2832u_r828d_config = {
919 	.i2c_addr = 0x3a,
920 	.xtal = 16000000,
921 	.max_i2c_msg_len = 2,
922 	.rafael_chip = CHIP_R828D,
923 };
924 
925 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
926 {
927 	int ret;
928 	struct dvb_usb_device *d = adap_to_d(adap);
929 	struct rtl28xxu_priv *priv = d_to_priv(d);
930 	struct dvb_frontend *fe = NULL;
931 	struct i2c_board_info info;
932 	struct i2c_client *client;
933 
934 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
935 
936 	memset(&info, 0, sizeof(struct i2c_board_info));
937 
938 	switch (priv->tuner) {
939 	case TUNER_RTL2832_FC0012:
940 		fe = dvb_attach(fc0012_attach, adap->fe[0],
941 			&d->i2c_adap, &rtl2832u_fc0012_config);
942 
943 		/* since fc0012 includs reading the signal strength delegate
944 		 * that to the tuner driver */
945 		adap->fe[0]->ops.read_signal_strength =
946 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
947 
948 		/* attach SDR */
949 		dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
950 				&rtl28xxu_rtl2832_fc0012_config, NULL);
951 		break;
952 	case TUNER_RTL2832_FC0013:
953 		fe = dvb_attach(fc0013_attach, adap->fe[0],
954 			&d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
955 
956 		/* fc0013 also supports signal strength reading */
957 		adap->fe[0]->ops.read_signal_strength =
958 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
959 
960 		/* attach SDR */
961 		dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
962 				&rtl28xxu_rtl2832_fc0013_config, NULL);
963 		break;
964 	case TUNER_RTL2832_E4000: {
965 			struct v4l2_subdev *sd;
966 			struct i2c_adapter *i2c_adap_internal =
967 					rtl2832_get_private_i2c_adapter(adap->fe[0]);
968 			struct e4000_config e4000_config = {
969 				.fe = adap->fe[0],
970 				.clock = 28800000,
971 			};
972 
973 			strlcpy(info.type, "e4000", I2C_NAME_SIZE);
974 			info.addr = 0x64;
975 			info.platform_data = &e4000_config;
976 
977 			request_module(info.type);
978 			client = i2c_new_device(priv->demod_i2c_adapter, &info);
979 			if (client == NULL || client->dev.driver == NULL)
980 				break;
981 
982 			if (!try_module_get(client->dev.driver->owner)) {
983 				i2c_unregister_device(client);
984 				break;
985 			}
986 
987 			priv->client = client;
988 			sd = i2c_get_clientdata(client);
989 			i2c_set_adapdata(i2c_adap_internal, d);
990 
991 			/* attach SDR */
992 			dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0],
993 					i2c_adap_internal,
994 					&rtl28xxu_rtl2832_e4000_config, sd);
995 		}
996 		break;
997 	case TUNER_RTL2832_FC2580:
998 		fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
999 				&rtl2832u_fc2580_config);
1000 		break;
1001 	case TUNER_RTL2832_TUA9001:
1002 		/* enable GPIO1 and GPIO4 as output */
1003 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1004 		if (ret)
1005 			goto err;
1006 
1007 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1008 		if (ret)
1009 			goto err;
1010 
1011 		fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
1012 				&rtl2832u_tua9001_config);
1013 		break;
1014 	case TUNER_RTL2832_R820T:
1015 		fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
1016 				&rtl2832u_r820t_config);
1017 
1018 		/* Use tuner to get the signal strength */
1019 		adap->fe[0]->ops.read_signal_strength =
1020 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1021 
1022 		/* attach SDR */
1023 		dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
1024 				&rtl28xxu_rtl2832_r820t_config, NULL);
1025 		break;
1026 	case TUNER_RTL2832_R828D:
1027 		/* power off mn88472 demod on GPIO0 */
1028 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
1029 		if (ret)
1030 			goto err;
1031 
1032 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
1033 		if (ret)
1034 			goto err;
1035 
1036 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
1037 		if (ret)
1038 			goto err;
1039 
1040 		fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
1041 				&rtl2832u_r828d_config);
1042 
1043 		/* Use tuner to get the signal strength */
1044 		adap->fe[0]->ops.read_signal_strength =
1045 				adap->fe[0]->ops.tuner_ops.get_rf_strength;
1046 		break;
1047 	default:
1048 		dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
1049 				priv->tuner);
1050 	}
1051 
1052 	if (fe == NULL && priv->client == NULL) {
1053 		ret = -ENODEV;
1054 		goto err;
1055 	}
1056 
1057 	return 0;
1058 err:
1059 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1060 	return ret;
1061 }
1062 
1063 static int rtl28xxu_init(struct dvb_usb_device *d)
1064 {
1065 	int ret;
1066 	u8 val;
1067 
1068 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1069 
1070 	/* init USB endpoints */
1071 	ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
1072 	if (ret)
1073 		goto err;
1074 
1075 	/* enable DMA and Full Packet Mode*/
1076 	val |= 0x09;
1077 	ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
1078 	if (ret)
1079 		goto err;
1080 
1081 	/* set EPA maximum packet size to 0x0200 */
1082 	ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1083 	if (ret)
1084 		goto err;
1085 
1086 	/* change EPA FIFO length */
1087 	ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1088 	if (ret)
1089 		goto err;
1090 
1091 	return ret;
1092 err:
1093 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1094 	return ret;
1095 }
1096 
1097 static void rtl28xxu_exit(struct dvb_usb_device *d)
1098 {
1099 	struct rtl28xxu_priv *priv = d->priv;
1100 	struct i2c_client *client = priv->client;
1101 
1102 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
1103 
1104 	/* remove I2C tuner */
1105 	if (client) {
1106 		module_put(client->dev.driver->owner);
1107 		i2c_unregister_device(client);
1108 	}
1109 
1110 	return;
1111 }
1112 
1113 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1114 {
1115 	int ret;
1116 	u8 gpio, sys0, epa_ctl[2];
1117 
1118 	dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1119 
1120 	/* demod adc */
1121 	ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
1122 	if (ret)
1123 		goto err;
1124 
1125 	/* tuner power, read GPIOs */
1126 	ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1127 	if (ret)
1128 		goto err;
1129 
1130 	dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1131 			sys0, gpio);
1132 
1133 	if (onoff) {
1134 		gpio |= 0x01; /* GPIO0 = 1 */
1135 		gpio &= (~0x10); /* GPIO4 = 0 */
1136 		gpio |= 0x04; /* GPIO2 = 1, LED on */
1137 		sys0 = sys0 & 0x0f;
1138 		sys0 |= 0xe0;
1139 		epa_ctl[0] = 0x00; /* clear stall */
1140 		epa_ctl[1] = 0x00; /* clear reset */
1141 	} else {
1142 		gpio &= (~0x01); /* GPIO0 = 0 */
1143 		gpio |= 0x10; /* GPIO4 = 1 */
1144 		gpio &= (~0x04); /* GPIO2 = 1, LED off */
1145 		sys0 = sys0 & (~0xc0);
1146 		epa_ctl[0] = 0x10; /* set stall */
1147 		epa_ctl[1] = 0x02; /* set reset */
1148 	}
1149 
1150 	dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1151 			sys0, gpio);
1152 
1153 	/* demod adc */
1154 	ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
1155 	if (ret)
1156 		goto err;
1157 
1158 	/* tuner power, write GPIOs */
1159 	ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1160 	if (ret)
1161 		goto err;
1162 
1163 	/* streaming EP: stall & reset */
1164 	ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1165 	if (ret)
1166 		goto err;
1167 
1168 	if (onoff)
1169 		usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1170 
1171 	return ret;
1172 err:
1173 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1174 	return ret;
1175 }
1176 
1177 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1178 {
1179 	int ret;
1180 
1181 	dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1182 
1183 	if (onoff) {
1184 		/* GPIO3=1, GPIO4=0 */
1185 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1186 		if (ret)
1187 			goto err;
1188 
1189 		/* suspend? */
1190 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1191 		if (ret)
1192 			goto err;
1193 
1194 		/* enable PLL */
1195 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1196 		if (ret)
1197 			goto err;
1198 
1199 		/* disable reset */
1200 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1201 		if (ret)
1202 			goto err;
1203 
1204 		mdelay(5);
1205 
1206 		/* enable ADC */
1207 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48);
1208 		if (ret)
1209 			goto err;
1210 
1211 		/* streaming EP: clear stall & reset */
1212 		ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1213 		if (ret)
1214 			goto err;
1215 
1216 		ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1217 		if (ret)
1218 			goto err;
1219 	} else {
1220 		/* GPIO4=1 */
1221 		ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1222 		if (ret)
1223 			goto err;
1224 
1225 		/* disable ADC */
1226 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48);
1227 		if (ret)
1228 			goto err;
1229 
1230 		/* disable PLL */
1231 		ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1232 		if (ret)
1233 			goto err;
1234 
1235 		/* streaming EP: set stall & reset */
1236 		ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1237 		if (ret)
1238 			goto err;
1239 	}
1240 
1241 	return ret;
1242 err:
1243 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1244 	return ret;
1245 }
1246 
1247 #if IS_ENABLED(CONFIG_RC_CORE)
1248 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1249 {
1250 	int ret, i;
1251 	struct rtl28xxu_priv *priv = d->priv;
1252 	u8 buf[5];
1253 	u32 rc_code;
1254 	struct rtl28xxu_reg_val rc_nec_tab[] = {
1255 		{ 0x3033, 0x80 },
1256 		{ 0x3020, 0x43 },
1257 		{ 0x3021, 0x16 },
1258 		{ 0x3022, 0x16 },
1259 		{ 0x3023, 0x5a },
1260 		{ 0x3024, 0x2d },
1261 		{ 0x3025, 0x16 },
1262 		{ 0x3026, 0x01 },
1263 		{ 0x3028, 0xb0 },
1264 		{ 0x3029, 0x04 },
1265 		{ 0x302c, 0x88 },
1266 		{ 0x302e, 0x13 },
1267 		{ 0x3030, 0xdf },
1268 		{ 0x3031, 0x05 },
1269 	};
1270 
1271 	/* init remote controller */
1272 	if (!priv->rc_active) {
1273 		for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1274 			ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1275 					rc_nec_tab[i].val);
1276 			if (ret)
1277 				goto err;
1278 		}
1279 		priv->rc_active = true;
1280 	}
1281 
1282 	ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1283 	if (ret)
1284 		goto err;
1285 
1286 	if (buf[4] & 0x01) {
1287 		if (buf[2] == (u8) ~buf[3]) {
1288 			if (buf[0] == (u8) ~buf[1]) {
1289 				/* NEC standard (16 bit) */
1290 				rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1291 			} else {
1292 				/* NEC extended (24 bit) */
1293 				rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1294 							   buf[2]);
1295 			}
1296 		} else {
1297 			/* NEC full (32 bit) */
1298 			rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1299 						    buf[2] << 8  | buf[3]);
1300 		}
1301 
1302 		rc_keydown(d->rc_dev, RC_TYPE_NEC, rc_code, 0);
1303 
1304 		ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1305 		if (ret)
1306 			goto err;
1307 
1308 		/* repeated intentionally to avoid extra keypress */
1309 		ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1310 		if (ret)
1311 			goto err;
1312 	}
1313 
1314 	return ret;
1315 err:
1316 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1317 	return ret;
1318 }
1319 
1320 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1321 		struct dvb_usb_rc *rc)
1322 {
1323 	rc->map_name = RC_MAP_EMPTY;
1324 	rc->allowed_protos = RC_BIT_NEC;
1325 	rc->query = rtl2831u_rc_query;
1326 	rc->interval = 400;
1327 
1328 	return 0;
1329 }
1330 
1331 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1332 {
1333 	int ret, i, len;
1334 	struct rtl28xxu_priv *priv = d->priv;
1335 	struct ir_raw_event ev;
1336 	u8 buf[128];
1337 	static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1338 		{IR_RX_IF,               0x03, 0xff},
1339 		{IR_RX_BUF_CTRL,         0x80, 0xff},
1340 		{IR_RX_CTRL,             0x80, 0xff},
1341 	};
1342 
1343 	/* init remote controller */
1344 	if (!priv->rc_active) {
1345 		static const struct rtl28xxu_reg_val_mask init_tab[] = {
1346 			{SYS_DEMOD_CTL1,         0x00, 0x04},
1347 			{SYS_DEMOD_CTL1,         0x00, 0x08},
1348 			{USB_CTRL,               0x20, 0x20},
1349 			{SYS_GPIO_DIR,           0x00, 0x08},
1350 			{SYS_GPIO_OUT_EN,        0x08, 0x08},
1351 			{SYS_GPIO_OUT_VAL,       0x08, 0x08},
1352 			{IR_MAX_DURATION0,       0xd0, 0xff},
1353 			{IR_MAX_DURATION1,       0x07, 0xff},
1354 			{IR_IDLE_LEN0,           0xc0, 0xff},
1355 			{IR_IDLE_LEN1,           0x00, 0xff},
1356 			{IR_GLITCH_LEN,          0x03, 0xff},
1357 			{IR_RX_CLK,              0x09, 0xff},
1358 			{IR_RX_CFG,              0x1c, 0xff},
1359 			{IR_MAX_H_TOL_LEN,       0x1e, 0xff},
1360 			{IR_MAX_L_TOL_LEN,       0x1e, 0xff},
1361 			{IR_RX_CTRL,             0x80, 0xff},
1362 		};
1363 
1364 		for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1365 			ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
1366 					init_tab[i].val, init_tab[i].mask);
1367 			if (ret)
1368 				goto err;
1369 		}
1370 
1371 		priv->rc_active = true;
1372 	}
1373 
1374 	ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1375 	if (ret)
1376 		goto err;
1377 
1378 	if (buf[0] != 0x83)
1379 		goto exit;
1380 
1381 	ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1382 	if (ret)
1383 		goto err;
1384 
1385 	len = buf[0];
1386 
1387 	/* read raw code from hw */
1388 	ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1389 	if (ret)
1390 		goto err;
1391 
1392 	/* let hw receive new code */
1393 	for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1394 		ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
1395 				refresh_tab[i].val, refresh_tab[i].mask);
1396 		if (ret)
1397 			goto err;
1398 	}
1399 
1400 	/* pass data to Kernel IR decoder */
1401 	init_ir_raw_event(&ev);
1402 
1403 	for (i = 0; i < len; i++) {
1404 		ev.pulse = buf[i] >> 7;
1405 		ev.duration = 50800 * (buf[i] & 0x7f);
1406 		ir_raw_event_store_with_filter(d->rc_dev, &ev);
1407 	}
1408 
1409 	/* 'flush' ir_raw_event_store_with_filter() */
1410 	ir_raw_event_set_idle(d->rc_dev, true);
1411 	ir_raw_event_handle(d->rc_dev);
1412 exit:
1413 	return ret;
1414 err:
1415 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1416 	return ret;
1417 }
1418 
1419 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1420 		struct dvb_usb_rc *rc)
1421 {
1422 	/* disable IR interrupts in order to avoid SDR sample loss */
1423 	if (rtl28xxu_disable_rc)
1424 		return rtl28xx_wr_reg(d, IR_RX_IE, 0x00);
1425 
1426 	/* load empty to enable rc */
1427 	if (!rc->map_name)
1428 		rc->map_name = RC_MAP_EMPTY;
1429 	rc->allowed_protos = RC_BIT_ALL;
1430 	rc->driver_type = RC_DRIVER_IR_RAW;
1431 	rc->query = rtl2832u_rc_query;
1432 	rc->interval = 400;
1433 
1434 	return 0;
1435 }
1436 #else
1437 #define rtl2831u_get_rc_config NULL
1438 #define rtl2832u_get_rc_config NULL
1439 #endif
1440 
1441 static const struct dvb_usb_device_properties rtl2831u_props = {
1442 	.driver_name = KBUILD_MODNAME,
1443 	.owner = THIS_MODULE,
1444 	.adapter_nr = adapter_nr,
1445 	.size_of_priv = sizeof(struct rtl28xxu_priv),
1446 
1447 	.power_ctrl = rtl2831u_power_ctrl,
1448 	.i2c_algo = &rtl28xxu_i2c_algo,
1449 	.read_config = rtl2831u_read_config,
1450 	.frontend_attach = rtl2831u_frontend_attach,
1451 	.tuner_attach = rtl2831u_tuner_attach,
1452 	.init = rtl28xxu_init,
1453 	.get_rc_config = rtl2831u_get_rc_config,
1454 
1455 	.num_adapters = 1,
1456 	.adapter = {
1457 		{
1458 			.stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1459 		},
1460 	},
1461 };
1462 
1463 static const struct dvb_usb_device_properties rtl2832u_props = {
1464 	.driver_name = KBUILD_MODNAME,
1465 	.owner = THIS_MODULE,
1466 	.adapter_nr = adapter_nr,
1467 	.size_of_priv = sizeof(struct rtl28xxu_priv),
1468 
1469 	.power_ctrl = rtl2832u_power_ctrl,
1470 	.i2c_algo = &rtl28xxu_i2c_algo,
1471 	.read_config = rtl2832u_read_config,
1472 	.frontend_attach = rtl2832u_frontend_attach,
1473 	.tuner_attach = rtl2832u_tuner_attach,
1474 	.init = rtl28xxu_init,
1475 	.exit = rtl28xxu_exit,
1476 	.get_rc_config = rtl2832u_get_rc_config,
1477 
1478 	.num_adapters = 1,
1479 	.adapter = {
1480 		{
1481 			.stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1482 		},
1483 	},
1484 };
1485 
1486 static const struct usb_device_id rtl28xxu_id_table[] = {
1487 	/* RTL2831U devices: */
1488 	{ DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1489 		&rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1490 	{ DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1491 		&rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1492 	{ DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1493 		&rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1494 
1495 	/* RTL2832U devices: */
1496 	{ DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1497 		&rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1498 	{ DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1499 		&rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1500 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1501 		&rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1502 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1503 		&rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1504 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1505 		&rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
1506 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1507 		&rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1508 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1509 		&rtl2832u_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1510 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1511 		&rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1512 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1513 		&rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
1514 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1515 		&rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1516 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1517 		&rtl2832u_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1518 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1519 		&rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1520 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1521 		&rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1522 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1523 		&rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
1524 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1525 		&rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1526 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1527 		&rtl2832u_props, "GIGABYTE U7300", NULL) },
1528 	{ DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1529 		&rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
1530 	{ DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1531 		&rtl2832u_props, "Compro VideoMate U620F", NULL) },
1532 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1533 		&rtl2832u_props, "MaxMedia HU394-T", NULL) },
1534 	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1535 		&rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1536 	{ DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1537 		&rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
1538 	{ DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1539 		&rtl2832u_props, "Genius TVGo DVB-T03", NULL) },
1540 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1541 		&rtl2832u_props, "Peak DVB-T USB", NULL) },
1542 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1543 		&rtl2832u_props, "Sveon STV20", NULL) },
1544 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1545 		&rtl2832u_props, "Sveon STV21", NULL) },
1546 	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1547 		&rtl2832u_props, "Sveon STV27", NULL) },
1548 
1549 	/* RTL2832P devices: */
1550 	{ DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1551 		&rtl2832u_props, "Astrometa DVB-T2", NULL) },
1552 	{ }
1553 };
1554 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1555 
1556 static struct usb_driver rtl28xxu_usb_driver = {
1557 	.name = KBUILD_MODNAME,
1558 	.id_table = rtl28xxu_id_table,
1559 	.probe = dvb_usbv2_probe,
1560 	.disconnect = dvb_usbv2_disconnect,
1561 	.suspend = dvb_usbv2_suspend,
1562 	.resume = dvb_usbv2_resume,
1563 	.reset_resume = dvb_usbv2_reset_resume,
1564 	.no_dynamic_id = 1,
1565 	.soft_unbind = 1,
1566 };
1567 
1568 module_usb_driver(rtl28xxu_usb_driver);
1569 
1570 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1571 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1572 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1573 MODULE_LICENSE("GPL");
1574