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