blob: 8772fe880b344ce870784f5a0e316dc19fc3cbf4 [file] [log] [blame]
Antti Palosaari831e0b72011-07-08 23:36:07 -03001/*
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "rtl28xxu.h"
23
24#include "rtl2830.h"
25
26#include "qt1010.h"
27#include "mt2060.h"
28#include "mxl5005s.h"
29
30/* debug */
31static int dvb_usb_rtl28xxu_debug;
32module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644);
33MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
34DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35
36static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
37{
38 int ret;
39 unsigned int pipe;
40 u8 requesttype;
41 u8 *buf;
42
43 buf = kmalloc(req->size, GFP_KERNEL);
44 if (!buf) {
45 ret = -ENOMEM;
46 goto err;
47 }
48
49 if (req->index & CMD_WR_FLAG) {
50 /* write */
51 memcpy(buf, req->data, req->size);
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
53 pipe = usb_sndctrlpipe(d->udev, 0);
54 } else {
55 /* read */
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
57 pipe = usb_rcvctrlpipe(d->udev, 0);
58 }
59
60 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
61 req->index, buf, req->size, 1000);
62
63 deb_dump(0, requesttype, req->value, req->index, buf, req->size,
64 deb_xfer);
65
66 if (ret < 0)
67 goto err_dealloc;
68 else
69 ret = 0;
70
71 /* read request, copy returned data to return buf */
72 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
73 memcpy(req->data, buf, req->size);
74
75 kfree(buf);
76 return ret;
77
78err_dealloc:
79 kfree(buf);
80err:
81 deb_info("%s: failed=%d\n", __func__, ret);
82 return ret;
83}
84
85static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
86{
87 struct rtl28xxu_req req;
88
89 if (reg < 0x3000)
90 req.index = CMD_USB_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -030091 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -030092 req.index = CMD_SYS_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -030093 else
94 req.index = CMD_IR_WR;
Antti Palosaari831e0b72011-07-08 23:36:07 -030095
96 req.value = reg;
97 req.size = len;
98 req.data = val;
99
100 return rtl28xxu_ctrl_msg(d, &req);
101}
102
103static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
104{
105 struct rtl28xxu_req req;
106
107 if (reg < 0x3000)
108 req.index = CMD_USB_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300109 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300110 req.index = CMD_SYS_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300111 else
112 req.index = CMD_IR_RD;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300113
114 req.value = reg;
115 req.size = len;
116 req.data = val;
117
118 return rtl28xxu_ctrl_msg(d, &req);
119}
120
121static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
122{
123 return rtl2831_wr_regs(d, reg, &val, 1);
124}
125
126static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
127{
128 return rtl2831_rd_regs(d, reg, val, 1);
129}
130
131/* I2C */
132static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
133 int num)
134{
135 int ret;
136 struct dvb_usb_device *d = i2c_get_adapdata(adap);
137 struct rtl28xxu_req req;
138
139 /*
140 * It is not known which are real I2C bus xfer limits, but testing
141 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
142 */
143
144 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
145 return -EAGAIN;
146
147 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
148 (msg[1].flags & I2C_M_RD)) {
149 if (msg[0].len > 2 || msg[1].len > 24) {
150 ret = -EOPNOTSUPP;
151 goto err_unlock;
152 }
153 if (msg[0].addr == 0x10) {
154 /* integrated demod */
155 req.value = (msg[0].buf[1] << 8) | (msg[0].addr << 1);
156 req.index = CMD_DEMOD_RD | msg[0].buf[0];
157 req.size = msg[1].len;
158 req.data = &msg[1].buf[0];
159 ret = rtl28xxu_ctrl_msg(d, &req);
160 } else {
161 /* real I2C */
162 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
163 req.index = CMD_I2C_RD;
164 req.size = msg[1].len;
165 req.data = &msg[1].buf[0];
166 ret = rtl28xxu_ctrl_msg(d, &req);
167 }
168 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
169 if (msg[0].len > 22) {
170 ret = -EOPNOTSUPP;
171 goto err_unlock;
172 }
173 if (msg[0].addr == 0x10) {
174 /* integrated demod */
175 req.value = (msg[0].buf[1] << 8) | (msg[0].addr << 1);
176 req.index = CMD_DEMOD_WR | msg[0].buf[0];
177 req.size = msg[0].len-2;
178 req.data = &msg[0].buf[2];
179 ret = rtl28xxu_ctrl_msg(d, &req);
180 } else {
181 /* real I2C */
182 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
183 req.index = CMD_I2C_WR;
184 req.size = msg[0].len-1;
185 req.data = &msg[0].buf[1];
186 ret = rtl28xxu_ctrl_msg(d, &req);
187 }
188 } else {
189 ret = -EINVAL;
190 }
191
192err_unlock:
193 mutex_unlock(&d->i2c_mutex);
194
195 return ret ? ret : num;
196}
197
198static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
199{
200 return I2C_FUNC_I2C;
201}
202
203static struct i2c_algorithm rtl28xxu_i2c_algo = {
204 .master_xfer = rtl28xxu_i2c_xfer,
205 .functionality = rtl28xxu_i2c_func,
206};
207
208static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
209 .i2c_addr = 0x10, /* 0x20 */
210 .xtal = 28800000,
211 .ts_mode = 0,
212 .spec_inv = 1,
213 .if_dvbt = 36150000,
214 .vtop = 0x20,
215 .krf = 0x04,
216 .agc_targ_val = 0x2d,
217
218};
219
220static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
221 .i2c_addr = 0x10, /* 0x20 */
222 .xtal = 28800000,
223 .ts_mode = 0,
224 .spec_inv = 1,
225 .if_dvbt = 36125000,
226 .vtop = 0x20,
227 .krf = 0x04,
228 .agc_targ_val = 0x2d,
229};
230
231static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
232 .i2c_addr = 0x10, /* 0x20 */
233 .xtal = 28800000,
234 .ts_mode = 0,
235 .spec_inv = 0,
236 .if_dvbt = 4570000,
237 .vtop = 0x3f,
238 .krf = 0x04,
239 .agc_targ_val = 0x3e,
240};
241
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300242static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300243{
244 int ret;
245 struct rtl28xxu_priv *priv = adap->dev->priv;
246 u8 buf[1];
247 struct rtl2830_config *rtl2830_config;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300248 /* open RTL2831U/RTL2830 I2C gate */
249 struct rtl28xxu_req req_gate = {0x0120, 0x0011, 0x0001, "\x08"};
Antti Palosaari831e0b72011-07-08 23:36:07 -0300250 /* for MT2060 tuner probe */
251 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
252 /* for QT1010 tuner probe */
253 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
Antti Palosaari831e0b72011-07-08 23:36:07 -0300254
255 deb_info("%s:\n", __func__);
256
257 /*
258 * RTL2831U GPIOs
259 * =========================================================
260 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
261 * GPIO2 | LED | 0 off | 1 on |
262 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
263 */
264
265 /* GPIO direction */
266 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
267 if (ret)
268 goto err;
269
270 /* enable as output GPIO0, GPIO2, GPIO4 */
271 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
272 if (ret)
273 goto err;
274
275 /*
276 * Probe used tuner. We need to know used tuner before demod attach
277 * since there is some demod params needed to set according to tuner.
278 */
279
280 /* open demod I2C gate */
281 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
282 if (ret)
283 goto err;
284
285 /* check QT1010 ID(?) register; reg=0f val=2c */
286 ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010);
287 if (ret == 0 && buf[0] == 0x2c) {
288 priv->tuner = TUNER_RTL2830_QT1010;
289 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
290 deb_info("%s: QT1010\n", __func__);
291 goto found;
292 } else {
293 deb_info("%s: QT1010 probe failed=%d - %02x\n",
294 __func__, ret, buf[0]);
295 }
296
297 /* open demod I2C gate */
298 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
299 if (ret)
300 goto err;
301
302 /* check MT2060 ID register; reg=00 val=63 */
303 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060);
304 if (ret == 0 && buf[0] == 0x63) {
305 priv->tuner = TUNER_RTL2830_MT2060;
306 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
307 deb_info("%s: MT2060\n", __func__);
308 goto found;
309 } else {
310 deb_info("%s: MT2060 probe failed=%d - %02x\n",
311 __func__, ret, buf[0]);
312 }
313
314 /* assume MXL5005S */
Antti Palosaarie9320ec2011-08-03 04:35:30 -0300315 ret = 0;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300316 priv->tuner = TUNER_RTL2830_MXL5005S;
317 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
318 deb_info("%s: MXL5005S\n", __func__);
319 goto found;
320
321found:
322 /* attach demodulator */
323 adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config,
324 &adap->dev->i2c_adap);
325 if (adap->fe[0] == NULL) {
326 ret = -ENODEV;
327 goto err;
328 }
329
330 return ret;
331err:
332 deb_info("%s: failed=%d\n", __func__, ret);
333 return ret;
334}
335
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300336static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
337{
338 int ret;
339 struct rtl28xxu_priv *priv = adap->dev->priv;
340 u8 buf[1];
341 /* open RTL2832U/RTL2832 I2C gate */
342 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
343 /* close RTL2832U/RTL2832 I2C gate */
344 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
345 /* for FC2580 tuner probe */
346 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
347
348 deb_info("%s:\n", __func__);
349
350 /* GPIO direction */
351 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
352 if (ret)
353 goto err;
354
355 /* enable as output GPIO0, GPIO2, GPIO4 */
356 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
357 if (ret)
358 goto err;
359
360 ret = rtl2831_wr_reg(adap->dev, SYS_DEMOD_CTL, 0xe8);
361 if (ret)
362 goto err;
363
364 /*
365 * Probe used tuner. We need to know used tuner before demod attach
366 * since there is some demod params needed to set according to tuner.
367 */
368
369 /* open demod I2C gate */
370 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open);
371 if (ret)
372 goto err;
373
374 /* check FC2580 ID register; reg=01 val=56 */
375 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580);
376 if (ret == 0 && buf[0] == 0x56) {
377 priv->tuner = TUNER_RTL2832_FC2580;
378 deb_info("%s: FC2580\n", __func__);
379 goto found;
380 } else {
381 deb_info("%s: FC2580 probe failed=%d - %02x\n",
382 __func__, ret, buf[0]);
383 }
384
385 /* close demod I2C gate */
386 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
387 if (ret)
388 goto err;
389
390 /* tuner not found */
391 ret = -ENODEV;
392 goto err;
393
394found:
395 /* close demod I2C gate */
396 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
397 if (ret)
398 goto err;
399
400 /* attach demodulator */
401 /* TODO: */
402
403 return ret;
404err:
405 deb_info("%s: failed=%d\n", __func__, ret);
406 return ret;
407}
408
Antti Palosaari831e0b72011-07-08 23:36:07 -0300409static struct qt1010_config rtl28xxu_qt1010_config = {
410 .i2c_address = 0x62, /* 0xc4 */
411};
412
413static struct mt2060_config rtl28xxu_mt2060_config = {
414 .i2c_address = 0x60, /* 0xc0 */
415 .clock_out = 0,
416};
417
418static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
419 .i2c_address = 0x63, /* 0xc6 */
420 .if_freq = IF_FREQ_4570000HZ,
421 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
422 .agc_mode = MXL_SINGLE_AGC,
423 .tracking_filter = MXL_TF_C_H,
424 .rssi_enable = MXL_RSSI_ENABLE,
425 .cap_select = MXL_CAP_SEL_ENABLE,
426 .div_out = MXL_DIV_OUT_4,
427 .clock_out = MXL_CLOCK_OUT_DISABLE,
428 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
429 .top = MXL5005S_TOP_25P2,
430 .mod_mode = MXL_DIGITAL_MODE,
431 .if_mode = MXL_ZERO_IF,
432 .AgcMasterByte = 0x00,
433};
434
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300435static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300436{
437 int ret;
438 struct rtl28xxu_priv *priv = adap->dev->priv;
439 struct i2c_adapter *rtl2830_tuner_i2c;
440 struct dvb_frontend *fe = NULL;
441
442 deb_info("%s:\n", __func__);
443
444 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
445 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
446
447 switch (priv->tuner) {
448 case TUNER_RTL2830_QT1010:
449 fe = dvb_attach(qt1010_attach, adap->fe[0], rtl2830_tuner_i2c,
450 &rtl28xxu_qt1010_config);
451 break;
452 case TUNER_RTL2830_MT2060:
453 fe = dvb_attach(mt2060_attach, adap->fe[0], rtl2830_tuner_i2c,
454 &rtl28xxu_mt2060_config, 1220);
455 break;
456 case TUNER_RTL2830_MXL5005S:
457 fe = dvb_attach(mxl5005s_attach, adap->fe[0], rtl2830_tuner_i2c,
458 &rtl28xxu_mxl5005s_config);
459 break;
460 default:
461 err("unknown tuner=%d", priv->tuner);
462 }
463
464 if (fe == NULL) {
465 ret = -ENODEV;
466 goto err;
467 }
468
469 return 0;
470err:
471 deb_info("%s: failed=%d\n", __func__, ret);
472 return ret;
473}
474
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300475static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
476{
477 int ret;
478 struct rtl28xxu_priv *priv = adap->dev->priv;
479 struct dvb_frontend *fe = NULL;
480
481 deb_info("%s:\n", __func__);
482
483 switch (priv->tuner) {
484 case TUNER_RTL2832_FC2580:
485 /* TODO: */
486 break;
487 default:
488 err("unknown tuner=%d", priv->tuner);
489 }
490
491 if (fe == NULL) {
492 ret = -ENODEV;
493 goto err;
494 }
495
496 return 0;
497err:
498 deb_info("%s: failed=%d\n", __func__, ret);
499 return ret;
500}
501
Antti Palosaari831e0b72011-07-08 23:36:07 -0300502static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
503{
504 int ret;
505 u8 buf[2], gpio;
506
507 deb_info("%s: onoff=%d\n", __func__, onoff);
508
509 ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
510 if (ret)
511 goto err;
512
513 if (onoff) {
514 buf[0] = 0x00;
515 buf[1] = 0x00;
516 gpio |= 0x04; /* LED on */
517 } else {
518 buf[0] = 0x10; /* stall EPA */
519 buf[1] = 0x02; /* reset EPA */
520 gpio &= (~0x04); /* LED off */
521 }
522
523 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
524 if (ret)
525 goto err;
526
527 ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
528 if (ret)
529 goto err;
530
531 return ret;
532err:
533 deb_info("%s: failed=%d\n", __func__, ret);
534 return ret;
535}
536
537static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
538{
539 int ret;
540 u8 gpio, sys0;
541
542 deb_info("%s: onoff=%d\n", __func__, onoff);
543
544 /* demod adc */
545 ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0);
546 if (ret)
547 goto err;
548
549 /* tuner power, read GPIOs */
550 ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
551 if (ret)
552 goto err;
553
554 deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
555
556 if (onoff) {
557 gpio |= 0x01; /* GPIO0 = 1 */
558 gpio &= (~0x10); /* GPIO4 = 0 */
559 sys0 = sys0 & 0x0f;
560 sys0 |= 0xe0;
561 } else {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300562 /*
563 * FIXME: Use .fe_ioctl_override() to prevent demod
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300564 * IOCTLs in case of device is powered off. Or change
565 * RTL2830 demod not perform requestesd IOCTL & IO when sleep.
Antti Palosaari831e0b72011-07-08 23:36:07 -0300566 */
567 gpio &= (~0x01); /* GPIO0 = 0 */
568 gpio |= 0x10; /* GPIO4 = 1 */
569 sys0 = sys0 & (~0xc0);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300570 }
571
572 deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
573
574 /* demod adc */
575 ret = rtl2831_wr_reg(d, SYS_SYS0, sys0);
576 if (ret)
577 goto err;
578
579 /* tuner power, write GPIOs */
580 ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
581 if (ret)
582 goto err;
583
584 return ret;
585err:
586 deb_info("%s: failed=%d\n", __func__, ret);
587 return ret;
588}
589
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300590static int rtl2831u_rc_query(struct dvb_usb_device *d)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300591{
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300592 int ret, i;
593 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300594 u8 buf[5];
595 u32 rc_code;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300596 struct rtl28xxu_reg_val rc_nec_tab[] = {
597 { 0x3033, 0x80 },
598 { 0x3020, 0x43 },
599 { 0x3021, 0x16 },
600 { 0x3022, 0x16 },
601 { 0x3023, 0x5a },
602 { 0x3024, 0x2d },
603 { 0x3025, 0x16 },
604 { 0x3026, 0x01 },
605 { 0x3028, 0xb0 },
606 { 0x3029, 0x04 },
607 { 0x302c, 0x88 },
608 { 0x302e, 0x13 },
609 { 0x3030, 0xdf },
610 { 0x3031, 0x05 },
611 };
612
613 /* init remote controller */
614 if (!priv->rc_active) {
615 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
616 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
617 rc_nec_tab[i].val);
618 if (ret)
619 goto err;
620 }
621 priv->rc_active = true;
622 }
Antti Palosaari831e0b72011-07-08 23:36:07 -0300623
624 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
625 if (ret)
626 goto err;
627
628 if (buf[4] & 0x01) {
629 if (buf[2] == (u8) ~buf[3]) {
630 if (buf[0] == (u8) ~buf[1]) {
631 /* NEC standard (16 bit) */
632 rc_code = buf[0] << 8 | buf[2];
633 } else {
634 /* NEC extended (24 bit) */
635 rc_code = buf[0] << 16 |
636 buf[1] << 8 | buf[2];
637 }
638 } else {
639 /* NEC full (32 bit) */
640 rc_code = buf[0] << 24 | buf[1] << 16 |
641 buf[2] << 8 | buf[3];
642 }
643
644 rc_keydown(d->rc_dev, rc_code, 0);
645
646 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
647 if (ret)
648 goto err;
649
650 /* repeated intentionally to avoid extra keypress */
651 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
652 if (ret)
653 goto err;
654 }
655
656 return ret;
657err:
658 deb_info("%s: failed=%d\n", __func__, ret);
659 return ret;
660}
661
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300662
663static int rtl2832u_rc_query(struct dvb_usb_device *d)
664{
665 int ret, i;
666 struct rtl28xxu_priv *priv = d->priv;
667 u8 buf[128];
668 int len;
669 struct rtl28xxu_reg_val rc_nec_tab[] = {
670 {IR_RX_CTRL, 0x20},
671 {IR_RX_BUF_CTRL, 0x80},
672 {IR_RX_IF, 0xff},
673 {IR_RX_IE, 0xff},
674 {IR_MAX_DURATION0, 0xd0},
675 {IR_MAX_DURATION1, 0x07},
676 {IR_IDLE_LEN0, 0xc0},
677 {IR_IDLE_LEN1, 0x00},
678 {IR_GLITCH_LEN, 0x03},
679 {IR_RX_CLK, 0x09},
680 {IR_RX_CFG, 0x1c},
681 {IR_MAX_H_TOL_LEN, 0x1e},
682 {IR_MAX_L_TOL_LEN, 0x1e},
683 {IR_RX_CTRL, 0x80},
684 };
685
686 /* init remote controller */
687 if (!priv->rc_active) {
688 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
689 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
690 rc_nec_tab[i].val);
691 if (ret)
692 goto err;
693 }
694 priv->rc_active = true;
695 }
696
697 ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]);
698 if (ret)
699 goto err;
700
701 if (buf[0] != 0x83)
702 goto exit;
703
704 ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]);
705 if (ret)
706 goto err;
707
708 len = buf[0];
709 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
710
711 /* TODO: pass raw IR to Kernel IR decoder */
712
713 ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03);
714 ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
715 ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80);
716
717exit:
718 return ret;
719err:
720 deb_info("%s: failed=%d\n", __func__, ret);
721 return ret;
722}
723
724
Antti Palosaari831e0b72011-07-08 23:36:07 -0300725/* DVB USB Driver stuff */
726#define USB_VID_REALTEK 0x0bda
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300727#define USB_VID_DEXATEK 0x1D19
Antti Palosaari831e0b72011-07-08 23:36:07 -0300728#define USB_PID_RTL2831U 0x2831
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300729#define USB_PID_RTL2832U 0x2832
Antti Palosaari831e0b72011-07-08 23:36:07 -0300730#define USB_PID_FREECOM 0x0160
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300731#define USB_PID_DEXATEK_1101 0x1101
Antti Palosaari831e0b72011-07-08 23:36:07 -0300732
733#define RTL2831U_0BDA_2831 0
734#define RTL2831U_14AA_0160 1
735
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300736#define RTL2832U_1ST_ID (RTL2831U_14AA_0160 + 1)
737
738#define RTL2832U_0BDA_2832 (0 + RTL2832U_1ST_ID)
739#define RTL2832U_1D19_1101 (1 + RTL2832U_1ST_ID)
740
741
Antti Palosaari831e0b72011-07-08 23:36:07 -0300742static struct usb_device_id rtl28xxu_table[] = {
743 [RTL2831U_0BDA_2831] = {
744 USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2831U)},
745 [RTL2831U_14AA_0160] = {
746 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM)},
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300747
748 [RTL2832U_0BDA_2832] = {
749 USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2832U)},
750 [RTL2832U_1D19_1101] = {
751 USB_DEVICE(USB_VID_DEXATEK, USB_PID_DEXATEK_1101)},
752
Antti Palosaari831e0b72011-07-08 23:36:07 -0300753 {} /* terminating entry */
754};
755
756MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
757
758static struct dvb_usb_device_properties rtl28xxu_properties[] = {
759 {
760 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
761
762 .usb_ctrl = DEVICE_SPECIFIC,
763 .no_reconnect = 1,
764
765 .size_of_priv = sizeof(struct rtl28xxu_priv),
766
767 .num_adapters = 1,
768 .adapter = {
769 {
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300770 .frontend_attach = rtl2831u_frontend_attach,
771 .tuner_attach = rtl2831u_tuner_attach,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300772 .streaming_ctrl = rtl28xxu_streaming_ctrl,
773 .stream = {
774 .type = USB_BULK,
775 .count = 6,
776 .endpoint = 0x81,
777 .u = {
778 .bulk = {
779 .buffersize = 4096,
780 }
781 }
782 },
783 }
784 },
785
786 .power_ctrl = rtl28xxu_power_ctrl,
787
788 .rc.core = {
789 .protocol = RC_TYPE_NEC,
790 .module_name = "rtl28xxu",
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300791 .rc_query = rtl2831u_rc_query,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300792 .rc_interval = 400,
793 .allowed_protos = RC_TYPE_NEC,
794 .rc_codes = RC_MAP_EMPTY,
795 },
796
797 .i2c_algo = &rtl28xxu_i2c_algo,
798
799 .num_device_descs = 2,
800 .devices = {
801 {
802 .name = "Realtek RTL2831U reference design",
803 .cold_ids = {NULL},
804 .warm_ids = {
805 &rtl28xxu_table[RTL2831U_0BDA_2831], NULL},
806 },
807 {
808 .name = "Freecom USB2.0 DVB-T",
809 .cold_ids = {NULL},
810 .warm_ids = {
811 &rtl28xxu_table[RTL2831U_14AA_0160], NULL},
812 },
813 }
814 },
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300815 {
816 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
817
818 .usb_ctrl = DEVICE_SPECIFIC,
819 .no_reconnect = 1,
820
821 .size_of_priv = sizeof(struct rtl28xxu_priv),
822
823 .num_adapters = 1,
824 .adapter = {
825 {
826 .frontend_attach = rtl2832u_frontend_attach,
827 .tuner_attach = rtl2832u_tuner_attach,
828 .streaming_ctrl = rtl28xxu_streaming_ctrl,
829 .stream = {
830 .type = USB_BULK,
831 .count = 6,
832 .endpoint = 0x81,
833 .u = {
834 .bulk = {
835 .buffersize = 4096,
836 }
837 }
838 },
839 }
840 },
841
842 .power_ctrl = rtl28xxu_power_ctrl,
843
844 .rc.core = {
845 .protocol = RC_TYPE_NEC,
846 .module_name = "rtl28xxu",
847 .rc_query = rtl2832u_rc_query,
848 .rc_interval = 400,
849 .allowed_protos = RC_TYPE_NEC,
850 .rc_codes = RC_MAP_EMPTY,
851 },
852
853 .i2c_algo = &rtl28xxu_i2c_algo,
854
855 .num_device_descs = 2,
856 .devices = {
857 {
858 .name = "Realtek RTL2832U reference design",
859 .cold_ids = {NULL},
860 .warm_ids = {
861 &rtl28xxu_table[RTL2832U_0BDA_2832], NULL},
862 },
863 {
864 .name = "Dexatek dongle",
865 .cold_ids = {NULL},
866 .warm_ids = {
867 &rtl28xxu_table[RTL2832U_1D19_1101], NULL},
868 },
869 }
870 },
871
Antti Palosaari831e0b72011-07-08 23:36:07 -0300872};
873
874static int rtl28xxu_probe(struct usb_interface *intf,
875 const struct usb_device_id *id)
876{
877 int ret, i;
878 int properties_count = ARRAY_SIZE(rtl28xxu_properties);
879 struct dvb_usb_device *d = NULL;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300880
881 deb_info("%s: interface=%d\n", __func__,
882 intf->cur_altsetting->desc.bInterfaceNumber);
883
884 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
885 return 0;
886
887 for (i = 0; i < properties_count; i++) {
888 ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
889 THIS_MODULE, &d, adapter_nr);
890 if (ret == 0 || ret != -ENODEV)
891 break;
892 }
893
894 if (ret)
895 goto err;
896
897 /* init USB endpoints */
898 ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09);
899 if (ret)
900 goto err;
901
902 ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
903 if (ret)
904 goto err;
905
906 ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
907 if (ret)
908 goto err;
909
Antti Palosaari831e0b72011-07-08 23:36:07 -0300910 return ret;
911err:
912 deb_info("%s: failed=%d\n", __func__, ret);
913 return ret;
914}
915
916static struct usb_driver rtl28xxu_driver = {
917 .name = "dvb_usb_rtl28xxu",
918 .probe = rtl28xxu_probe,
919 .disconnect = dvb_usb_device_exit,
920 .id_table = rtl28xxu_table,
921};
922
923/* module stuff */
924static int __init rtl28xxu_module_init(void)
925{
926 int ret;
927 deb_info("%s:\n", __func__);
928 ret = usb_register(&rtl28xxu_driver);
929 if (ret)
930 err("usb_register failed=%d", ret);
931
932 return ret;
933}
934
935static void __exit rtl28xxu_module_exit(void)
936{
937 deb_info("%s:\n", __func__);
938 /* deregister this driver from the USB subsystem */
939 usb_deregister(&rtl28xxu_driver);
940}
941
942module_init(rtl28xxu_module_init);
943module_exit(rtl28xxu_module_exit);
944
945MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
946MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
947MODULE_LICENSE("GPL");