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