aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/media/dvb/dvb-usb-v2/Kconfig7
-rw-r--r--drivers/media/dvb/dvb-usb-v2/Makefile3
-rw-r--r--drivers/media/dvb/dvb-usb-v2/it913x.c799
-rw-r--r--drivers/media/dvb/dvb-usb/Kconfig8
-rw-r--r--drivers/media/dvb/dvb-usb/Makefile3
-rw-r--r--drivers/media/dvb/dvb-usb/it913x.c931
6 files changed, 809 insertions, 942 deletions
diff --git a/drivers/media/dvb/dvb-usb-v2/Kconfig b/drivers/media/dvb/dvb-usb-v2/Kconfig
index ed87935dc8d4..276374fbaf4f 100644
--- a/drivers/media/dvb/dvb-usb-v2/Kconfig
+++ b/drivers/media/dvb/dvb-usb-v2/Kconfig
@@ -102,6 +102,13 @@ config DVB_USB_GL861
102 Say Y here to support the MSI Megasky 580 (55801) DVB-T USB2.0 102 Say Y here to support the MSI Megasky 580 (55801) DVB-T USB2.0
103 receiver with USB ID 0db0:5581. 103 receiver with USB ID 0db0:5581.
104 104
105config DVB_USB_IT913X
106 tristate "ITE IT913X DVB-T USB2.0 support"
107 depends on DVB_USB_V2
108 select DVB_IT913X_FE
109 help
110 Say Y here to support the ITE IT913X DVB-T USB2.0
111
105config DVB_USB_LME2510 112config DVB_USB_LME2510
106 tristate "LME DM04/QQBOX DVB-S USB2.0 support" 113 tristate "LME DM04/QQBOX DVB-S USB2.0 support"
107 depends on DVB_USB_V2 114 depends on DVB_USB_V2
diff --git a/drivers/media/dvb/dvb-usb-v2/Makefile b/drivers/media/dvb/dvb-usb-v2/Makefile
index 002e88883827..61b0f53669e8 100644
--- a/drivers/media/dvb/dvb-usb-v2/Makefile
+++ b/drivers/media/dvb/dvb-usb-v2/Makefile
@@ -25,6 +25,9 @@ obj-$(CONFIG_DVB_USB_CE6230) += dvb-usb-ce6230.o
25dvb-usb-ec168-objs = ec168.o 25dvb-usb-ec168-objs = ec168.o
26obj-$(CONFIG_DVB_USB_EC168) += dvb-usb-ec168.o 26obj-$(CONFIG_DVB_USB_EC168) += dvb-usb-ec168.o
27 27
28dvb-usb-it913x-objs = it913x.o
29obj-$(CONFIG_DVB_USB_IT913X) += dvb-usb-it913x.o
30
28dvb-usb-lmedm04-objs = lmedm04.o 31dvb-usb-lmedm04-objs = lmedm04.o
29obj-$(CONFIG_DVB_USB_LME2510) += dvb-usb-lmedm04.o 32obj-$(CONFIG_DVB_USB_LME2510) += dvb-usb-lmedm04.o
30 33
diff --git a/drivers/media/dvb/dvb-usb-v2/it913x.c b/drivers/media/dvb/dvb-usb-v2/it913x.c
new file mode 100644
index 000000000000..695f9106bc54
--- /dev/null
+++ b/drivers/media/dvb/dvb-usb-v2/it913x.c
@@ -0,0 +1,799 @@
1/*
2 * DVB USB compliant linux driver for ITE IT9135 and IT9137
3 *
4 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
5 * IT9135 (C) ITE Tech Inc.
6 * IT9137 (C) ITE Tech Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License Version 2, as
10 * published by the Free Software Foundation.
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * see Documentation/dvb/README.dvb-usb for more information
23 * see Documentation/dvb/it9137.txt for firmware information
24 *
25 */
26#define DVB_USB_LOG_PREFIX "it913x"
27
28#include <linux/usb.h>
29#include <linux/usb/input.h>
30#include <media/rc-core.h>
31
32#include "dvb_usb.h"
33#include "it913x-fe.h"
34
35/* debug */
36static int dvb_usb_it913x_debug;
37#define it_debug(var, level, args...) \
38 do { if ((var & level)) pr_debug(DVB_USB_LOG_PREFIX": " args); \
39} while (0)
40#define deb_info(level, args...) it_debug(dvb_usb_it913x_debug, level, args)
41#define info(args...) pr_info(DVB_USB_LOG_PREFIX": " args)
42
43module_param_named(debug, dvb_usb_it913x_debug, int, 0644);
44MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
45
46static int dvb_usb_it913x_firmware;
47module_param_named(firmware, dvb_usb_it913x_firmware, int, 0644);
48MODULE_PARM_DESC(firmware, "set firmware 0=auto"\
49 "1=IT9137 2=IT9135 V1 3=IT9135 V2");
50#define FW_IT9137 "dvb-usb-it9137-01.fw"
51#define FW_IT9135_V1 "dvb-usb-it9135-01.fw"
52#define FW_IT9135_V2 "dvb-usb-it9135-02.fw"
53
54DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
55
56struct it913x_state {
57 struct ite_config it913x_config;
58 u8 pid_filter_onoff;
59 bool proprietary_ir;
60 int cmd_counter;
61};
62
63static u16 check_sum(u8 *p, u8 len)
64{
65 u16 sum = 0;
66 u8 i = 1;
67 while (i < len)
68 sum += (i++ & 1) ? (*p++) << 8 : *p++;
69 return ~sum;
70}
71
72static int it913x_io(struct dvb_usb_device *d, u8 mode, u8 pro,
73 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
74{
75 struct it913x_state *st = d->priv;
76 int ret = 0, i, buf_size = 1;
77 u8 *buff;
78 u8 rlen;
79 u16 chk_sum;
80
81 buff = kzalloc(256, GFP_KERNEL);
82 if (!buff) {
83 info("USB Buffer Failed");
84 return -ENOMEM;
85 }
86
87 buff[buf_size++] = pro;
88 buff[buf_size++] = cmd;
89 buff[buf_size++] = st->cmd_counter;
90
91 switch (mode) {
92 case READ_LONG:
93 case WRITE_LONG:
94 buff[buf_size++] = len;
95 buff[buf_size++] = 2;
96 buff[buf_size++] = (reg >> 24);
97 buff[buf_size++] = (reg >> 16) & 0xff;
98 buff[buf_size++] = (reg >> 8) & 0xff;
99 buff[buf_size++] = reg & 0xff;
100 break;
101 case READ_SHORT:
102 buff[buf_size++] = addr;
103 break;
104 case WRITE_SHORT:
105 buff[buf_size++] = len;
106 buff[buf_size++] = addr;
107 buff[buf_size++] = (reg >> 8) & 0xff;
108 buff[buf_size++] = reg & 0xff;
109 break;
110 case READ_DATA:
111 case WRITE_DATA:
112 break;
113 case WRITE_CMD:
114 mode = 7;
115 break;
116 default:
117 kfree(buff);
118 return -EINVAL;
119 }
120
121 if (mode & 1) {
122 for (i = 0; i < len ; i++)
123 buff[buf_size++] = data[i];
124 }
125 chk_sum = check_sum(&buff[1], buf_size);
126
127 buff[buf_size++] = chk_sum >> 8;
128 buff[0] = buf_size;
129 buff[buf_size++] = (chk_sum & 0xff);
130
131 ret = dvb_usbv2_generic_rw(d, buff, buf_size, buff, (mode & 1) ?
132 5 : len + 5);
133 if (ret < 0)
134 goto error;
135
136 rlen = (mode & 0x1) ? 0x1 : len;
137
138 if (mode & 1)
139 ret = buff[2];
140 else
141 memcpy(data, &buff[3], rlen);
142
143 st->cmd_counter++;
144
145error: kfree(buff);
146
147 return ret;
148}
149
150static int it913x_wr_reg(struct dvb_usb_device *d, u8 pro, u32 reg , u8 data)
151{
152 int ret;
153 u8 b[1];
154 b[0] = data;
155 ret = it913x_io(d, WRITE_LONG, pro,
156 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
157
158 return ret;
159}
160
161static int it913x_read_reg(struct dvb_usb_device *d, u32 reg)
162{
163 int ret;
164 u8 data[1];
165
166 ret = it913x_io(d, READ_LONG, DEV_0,
167 CMD_DEMOD_READ, reg, 0, &data[0], sizeof(data));
168
169 return (ret < 0) ? ret : data[0];
170}
171
172static int it913x_query(struct dvb_usb_device *d, u8 pro)
173{
174 struct it913x_state *st = d->priv;
175 int ret, i;
176 u8 data[4];
177 u8 ver;
178
179 for (i = 0; i < 5; i++) {
180 ret = it913x_io(d, READ_LONG, pro, CMD_DEMOD_READ,
181 0x1222, 0, &data[0], 3);
182 ver = data[0];
183 if (ver > 0 && ver < 3)
184 break;
185 msleep(100);
186 }
187
188 if (ver < 1 || ver > 2) {
189 info("Failed to identify chip version applying 1");
190 st->it913x_config.chip_ver = 0x1;
191 st->it913x_config.chip_type = 0x9135;
192 return 0;
193 }
194
195 st->it913x_config.chip_ver = ver;
196 st->it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
197
198 info("Chip Version=%02x Chip Type=%04x", st->it913x_config.chip_ver,
199 st->it913x_config.chip_type);
200
201 ret = it913x_io(d, READ_SHORT, pro,
202 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
203
204 st->it913x_config.firmware = (data[0] << 24) | (data[1] << 16) |
205 (data[2] << 8) | data[3];
206
207 return ret;
208}
209
210static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
211{
212 struct dvb_usb_device *d = adap_to_d(adap);
213 struct it913x_state *st = adap_to_priv(adap);
214 int ret;
215 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
216
217 mutex_lock(&d->i2c_mutex);
218
219 deb_info(1, "PID_C (%02x)", onoff);
220
221 ret = it913x_wr_reg(d, pro, PID_EN, st->pid_filter_onoff);
222
223 mutex_unlock(&d->i2c_mutex);
224 return ret;
225}
226
227static int it913x_pid_filter(struct dvb_usb_adapter *adap,
228 int index, u16 pid, int onoff)
229{
230 struct dvb_usb_device *d = adap_to_d(adap);
231 struct it913x_state *st = adap_to_priv(adap);
232 int ret;
233 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
234
235 mutex_lock(&d->i2c_mutex);
236
237 deb_info(1, "PID_F (%02x)", onoff);
238
239 ret = it913x_wr_reg(d, pro, PID_LSB, (u8)(pid & 0xff));
240
241 ret |= it913x_wr_reg(d, pro, PID_MSB, (u8)(pid >> 8));
242
243 ret |= it913x_wr_reg(d, pro, PID_INX_EN, (u8)onoff);
244
245 ret |= it913x_wr_reg(d, pro, PID_INX, (u8)(index & 0x1f));
246
247 if (d->udev->speed == USB_SPEED_HIGH && pid == 0x2000) {
248 ret |= it913x_wr_reg(d , pro, PID_EN, !onoff);
249 st->pid_filter_onoff = !onoff;
250 } else
251 st->pid_filter_onoff =
252 adap->pid_filtering;
253
254 mutex_unlock(&d->i2c_mutex);
255 return 0;
256}
257
258
259static int it913x_return_status(struct dvb_usb_device *d)
260{
261 struct it913x_state *st = d->priv;
262 int ret = it913x_query(d, DEV_0);
263 if (st->it913x_config.firmware > 0)
264 info("Firmware Version %d", st->it913x_config.firmware);
265
266 return ret;
267}
268
269static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
270 int num)
271{
272 struct dvb_usb_device *d = i2c_get_adapdata(adap);
273 static u8 data[256];
274 int ret;
275 u32 reg;
276 u8 pro;
277
278 mutex_lock(&d->i2c_mutex);
279
280 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
281
282 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
283 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
284 memcpy(data, msg[0].buf, msg[0].len);
285 reg = (data[0] << 24) + (data[1] << 16) +
286 (data[2] << 8) + data[3];
287 if (num == 2) {
288 ret = it913x_io(d, READ_LONG, pro,
289 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
290 memcpy(msg[1].buf, data, msg[1].len);
291 } else
292 ret = it913x_io(d, WRITE_LONG, pro, CMD_DEMOD_WRITE,
293 reg, 0, &data[4], msg[0].len - 4);
294
295 mutex_unlock(&d->i2c_mutex);
296
297 return ret;
298}
299
300static u32 it913x_i2c_func(struct i2c_adapter *adapter)
301{
302 return I2C_FUNC_I2C;
303}
304
305static struct i2c_algorithm it913x_i2c_algo = {
306 .master_xfer = it913x_i2c_xfer,
307 .functionality = it913x_i2c_func,
308};
309
310/* Callbacks for DVB USB */
311#define IT913X_POLL 250
312static int it913x_rc_query(struct dvb_usb_device *d)
313{
314 u8 ibuf[4];
315 int ret;
316 u32 key;
317 /* Avoid conflict with frontends*/
318 mutex_lock(&d->i2c_mutex);
319
320 ret = it913x_io(d, READ_LONG, PRO_LINK, CMD_IR_GET,
321 0, 0, &ibuf[0], sizeof(ibuf));
322
323 if ((ibuf[2] + ibuf[3]) == 0xff) {
324 key = ibuf[2];
325 key += ibuf[0] << 16;
326 key += ibuf[1] << 8;
327 deb_info(1, "NEC Extended Key =%08x", key);
328 if (d->rc_dev != NULL)
329 rc_keydown(d->rc_dev, key, 0);
330 }
331
332 mutex_unlock(&d->i2c_mutex);
333
334 return ret;
335}
336
337/* Firmware sets raw */
338static const char fw_it9135_v1[] = FW_IT9135_V1;
339static const char fw_it9135_v2[] = FW_IT9135_V2;
340static const char fw_it9137[] = FW_IT9137;
341
342static void ite_get_firmware_name(struct dvb_usb_device *d,
343 const char **name)
344{
345 struct it913x_state *st = d->priv;
346 int sw;
347 /* auto switch */
348 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_KWORLD_2)
349 sw = IT9137_FW;
350 else if (st->it913x_config.chip_ver == 1)
351 sw = IT9135_V1_FW;
352 else
353 sw = IT9135_V2_FW;
354
355 /* force switch */
356 if (dvb_usb_it913x_firmware != IT9135_AUTO)
357 sw = dvb_usb_it913x_firmware;
358
359 switch (sw) {
360 case IT9135_V1_FW:
361 st->it913x_config.firmware_ver = 1;
362 st->it913x_config.adc_x2 = 1;
363 st->it913x_config.read_slevel = false;
364 *name = fw_it9135_v1;
365 break;
366 case IT9135_V2_FW:
367 st->it913x_config.firmware_ver = 1;
368 st->it913x_config.adc_x2 = 1;
369 st->it913x_config.read_slevel = false;
370 *name = fw_it9135_v2;
371 switch (st->it913x_config.tuner_id_0) {
372 case IT9135_61:
373 case IT9135_62:
374 break;
375 default:
376 info("Unknown tuner ID applying default 0x60");
377 case IT9135_60:
378 st->it913x_config.tuner_id_0 = IT9135_60;
379 }
380 break;
381 case IT9137_FW:
382 default:
383 st->it913x_config.firmware_ver = 0;
384 st->it913x_config.adc_x2 = 0;
385 st->it913x_config.read_slevel = true;
386 *name = fw_it9137;
387 }
388
389 return;
390}
391
392#define TS_MPEG_PKT_SIZE 188
393#define EP_LOW 21
394#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
395#define EP_HIGH 348
396#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
397
398static int it913x_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
399 struct usb_data_stream_properties *stream)
400{
401 struct dvb_usb_adapter *adap = fe_to_adap(fe);
402 if (adap->pid_filtering)
403 stream->u.bulk.buffersize = TS_BUFFER_SIZE_PID;
404 else
405 stream->u.bulk.buffersize = TS_BUFFER_SIZE_MAX;
406
407 return 0;
408}
409
410static int it913x_select_config(struct dvb_usb_device *d)
411{
412 struct it913x_state *st = d->priv;
413 int ret, reg;
414
415 ret = it913x_return_status(d);
416 if (ret < 0)
417 return ret;
418
419 if (st->it913x_config.chip_ver == 0x02
420 && st->it913x_config.chip_type == 0x9135)
421 reg = it913x_read_reg(d, 0x461d);
422 else
423 reg = it913x_read_reg(d, 0x461b);
424
425 if (reg < 0)
426 return reg;
427
428 if (reg == 0) {
429 st->it913x_config.dual_mode = 0;
430 st->it913x_config.tuner_id_0 = IT9135_38;
431 st->proprietary_ir = true;
432 } else {
433 /* TS mode */
434 reg = it913x_read_reg(d, 0x49c5);
435 if (reg < 0)
436 return reg;
437 st->it913x_config.dual_mode = reg;
438
439 /* IR mode type */
440 reg = it913x_read_reg(d, 0x49ac);
441 if (reg < 0)
442 return reg;
443 if (reg == 5) {
444 info("Remote propriety (raw) mode");
445 st->proprietary_ir = true;
446 } else if (reg == 1) {
447 info("Remote HID mode NOT SUPPORTED");
448 st->proprietary_ir = false;
449 }
450
451 /* Tuner_id */
452 reg = it913x_read_reg(d, 0x49d0);
453 if (reg < 0)
454 return reg;
455 st->it913x_config.tuner_id_0 = reg;
456 }
457
458 info("Dual mode=%x Tuner Type=%x", st->it913x_config.dual_mode,
459 st->it913x_config.tuner_id_0);
460
461 return ret;
462}
463
464static int it913x_streaming_ctrl(struct dvb_frontend *fe, int onoff)
465{
466 struct dvb_usb_adapter *adap = fe_to_adap(fe);
467 struct dvb_usb_device *d = adap_to_d(adap);
468 struct it913x_state *st = fe_to_priv(fe);
469 int ret = 0;
470 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
471
472 deb_info(1, "STM (%02x)", onoff);
473
474 if (!onoff) {
475 mutex_lock(&d->i2c_mutex);
476
477 ret = it913x_wr_reg(d, pro, PID_RST, 0x1);
478
479 mutex_unlock(&d->i2c_mutex);
480 st->pid_filter_onoff =
481 adap->pid_filtering;
482
483 }
484
485 return ret;
486}
487
488static int it913x_identify_state(struct dvb_usb_device *d, const char **name)
489{
490 struct it913x_state *st = d->priv;
491 int ret;
492 u8 reg;
493
494 /* Read and select config */
495 ret = it913x_select_config(d);
496 if (ret < 0)
497 return ret;
498
499 ite_get_firmware_name(d, name);
500
501 if (st->it913x_config.firmware > 0)
502 return WARM;
503
504 if (st->it913x_config.dual_mode) {
505 st->it913x_config.tuner_id_1 = it913x_read_reg(d, 0x49e0);
506 ret = it913x_wr_reg(d, DEV_0, GPIOH1_EN, 0x1);
507 ret |= it913x_wr_reg(d, DEV_0, GPIOH1_ON, 0x1);
508 ret |= it913x_wr_reg(d, DEV_0, GPIOH1_O, 0x1);
509 msleep(50);
510 ret |= it913x_wr_reg(d, DEV_0, GPIOH1_O, 0x0);
511 msleep(50);
512 reg = it913x_read_reg(d, GPIOH1_O);
513 if (reg == 0) {
514 ret |= it913x_wr_reg(d, DEV_0, GPIOH1_O, 0x1);
515 ret |= it913x_return_status(d);
516 if (ret != 0)
517 ret = it913x_wr_reg(d, DEV_0,
518 GPIOH1_O, 0x0);
519 }
520 }
521
522 reg = it913x_read_reg(d, IO_MUX_POWER_CLK);
523
524 if (st->it913x_config.dual_mode) {
525 ret |= it913x_wr_reg(d, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
526 if (st->it913x_config.firmware_ver == 1)
527 ret |= it913x_wr_reg(d, DEV_0, 0xcfff, 0x1);
528 else
529 ret |= it913x_wr_reg(d, DEV_0, CLK_O_EN, 0x1);
530 } else {
531 ret |= it913x_wr_reg(d, DEV_0, 0x4bfb, 0x0);
532 if (st->it913x_config.firmware_ver == 1)
533 ret |= it913x_wr_reg(d, DEV_0, 0xcfff, 0x0);
534 else
535 ret |= it913x_wr_reg(d, DEV_0, CLK_O_EN, 0x0);
536 }
537
538 ret |= it913x_wr_reg(d, DEV_0, I2C_CLK, I2C_CLK_100);
539
540 return (ret < 0) ? ret : COLD;
541}
542
543static int it913x_download_firmware(struct dvb_usb_device *d,
544 const struct firmware *fw)
545{
546 struct it913x_state *st = d->priv;
547 int ret = 0, i = 0, pos = 0;
548 u8 packet_size, min_pkt;
549 u8 *fw_data;
550
551 ret = it913x_wr_reg(d, DEV_0, I2C_CLK, I2C_CLK_100);
552
553 info("FRM Starting Firmware Download");
554
555 /* Multi firmware loader */
556 /* This uses scatter write firmware headers */
557 /* The firmware must start with 03 XX 00 */
558 /* and be the extact firmware length */
559
560 if (st->it913x_config.chip_ver == 2)
561 min_pkt = 0x11;
562 else
563 min_pkt = 0x19;
564
565 while (i <= fw->size) {
566 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
567 || (i == fw->size)) {
568 packet_size = i - pos;
569 if ((packet_size > min_pkt) || (i == fw->size)) {
570 fw_data = (u8 *)(fw->data + pos);
571 pos += packet_size;
572 if (packet_size > 0) {
573 ret = it913x_io(d, WRITE_DATA,
574 DEV_0, CMD_SCATTER_WRITE, 0,
575 0, fw_data, packet_size);
576 if (ret < 0)
577 break;
578 }
579 udelay(1000);
580 }
581 }
582 i++;
583 }
584
585 if (ret < 0)
586 info("FRM Firmware Download Failed (%d)" , ret);
587 else
588 info("FRM Firmware Download Completed - Resetting Device");
589
590 msleep(30);
591
592 ret = it913x_io(d, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
593 if (ret < 0)
594 info("FRM Device not responding to reboot");
595
596 ret = it913x_return_status(d);
597 if (st->it913x_config.firmware == 0) {
598 info("FRM Failed to reboot device");
599 return -ENODEV;
600 }
601
602 msleep(30);
603
604 ret = it913x_wr_reg(d, DEV_0, I2C_CLK, I2C_CLK_400);
605
606 msleep(30);
607
608 /* Tuner function */
609 if (st->it913x_config.dual_mode)
610 ret |= it913x_wr_reg(d, DEV_0_DMOD , 0xec4c, 0xa0);
611 else
612 ret |= it913x_wr_reg(d, DEV_0_DMOD , 0xec4c, 0x68);
613
614 if ((st->it913x_config.chip_ver == 1) &&
615 (st->it913x_config.chip_type == 0x9135)) {
616 ret |= it913x_wr_reg(d, DEV_0, PADODPU, 0x0);
617 ret |= it913x_wr_reg(d, DEV_0, AGC_O_D, 0x0);
618 if (st->it913x_config.dual_mode) {
619 ret |= it913x_wr_reg(d, DEV_1, PADODPU, 0x0);
620 ret |= it913x_wr_reg(d, DEV_1, AGC_O_D, 0x0);
621 }
622 }
623
624 return (ret < 0) ? -ENODEV : 0;
625}
626
627static int it913x_name(struct dvb_usb_adapter *adap)
628{
629 struct dvb_usb_device *d = adap_to_d(adap);
630 const char *desc = d->name;
631 char *fe_name[] = {"_1", "_2", "_3", "_4"};
632 char *name = adap->fe[0]->ops.info.name;
633
634 strlcpy(name, desc, 128);
635 strlcat(name, fe_name[adap->id], 128);
636
637 return 0;
638}
639
640static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
641{
642 struct dvb_usb_device *d = adap_to_d(adap);
643 struct it913x_state *st = d->priv;
644 int ret = 0;
645 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
646 u16 ep_size = adap->stream.buf_size / 4;
647 u8 pkt_size = 0x80;
648
649 if (d->udev->speed != USB_SPEED_HIGH)
650 pkt_size = 0x10;
651
652 st->it913x_config.adf = it913x_read_reg(d, IO_MUX_POWER_CLK);
653
654 adap->fe[0] = dvb_attach(it913x_fe_attach,
655 &d->i2c_adap, adap_addr, &st->it913x_config);
656
657 if (adap->id == 0 && adap->fe[0]) {
658 it913x_wr_reg(d, DEV_0_DMOD, MP2_SW_RST, 0x1);
659 it913x_wr_reg(d, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
660 it913x_wr_reg(d, DEV_0, EP0_TX_EN, 0x0f);
661 it913x_wr_reg(d, DEV_0, EP0_TX_NAK, 0x1b);
662 it913x_wr_reg(d, DEV_0, EP0_TX_EN, 0x2f);
663 it913x_wr_reg(d, DEV_0, EP4_TX_LEN_LSB,
664 ep_size & 0xff);
665 it913x_wr_reg(d, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
666 ret = it913x_wr_reg(d, DEV_0, EP4_MAX_PKT, pkt_size);
667 } else if (adap->id == 1 && adap->fe[0]) {
668 it913x_wr_reg(d, DEV_0, EP0_TX_EN, 0x6f);
669 it913x_wr_reg(d, DEV_0, EP5_TX_LEN_LSB,
670 ep_size & 0xff);
671 it913x_wr_reg(d, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
672 it913x_wr_reg(d, DEV_0, EP5_MAX_PKT, pkt_size);
673 it913x_wr_reg(d, DEV_0_DMOD, MP2IF2_EN, 0x1);
674 it913x_wr_reg(d, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
675 it913x_wr_reg(d, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
676 it913x_wr_reg(d, DEV_0_DMOD, TSIS_ENABLE, 0x1);
677 it913x_wr_reg(d, DEV_0_DMOD, MP2_SW_RST, 0x0);
678 it913x_wr_reg(d, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
679 it913x_wr_reg(d, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
680 it913x_wr_reg(d, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
681 it913x_wr_reg(d, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
682 ret = it913x_wr_reg(d, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
683 } else
684 return -ENODEV;
685
686 ret |= it913x_name(adap);
687
688 return ret;
689}
690
691/* DVB USB Driver */
692static int it913x_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
693{
694 struct it913x_state *st = d->priv;
695
696 if (st->proprietary_ir == false) {
697 rc->map_name = NULL;
698 return 0;
699 }
700
701 rc->allowed_protos = RC_TYPE_NEC;
702 rc->query = it913x_rc_query;
703 rc->interval = 250;
704
705 return 0;
706}
707
708static int it913x_get_adapter_count(struct dvb_usb_device *d)
709{
710 struct it913x_state *st = d->priv;
711 if (st->it913x_config.dual_mode)
712 return 2;
713 return 1;
714}
715
716static struct dvb_usb_device_properties it913x_properties = {
717 .driver_name = KBUILD_MODNAME,
718 .owner = THIS_MODULE,
719 .bInterfaceNumber = 0,
720 .generic_bulk_ctrl_endpoint = 0x02,
721 .generic_bulk_ctrl_endpoint_response = 0x81,
722
723 .adapter_nr = adapter_nr,
724 .size_of_priv = sizeof(struct it913x_state),
725
726 .identify_state = it913x_identify_state,
727 .i2c_algo = &it913x_i2c_algo,
728
729 .download_firmware = it913x_download_firmware,
730
731 .frontend_attach = it913x_frontend_attach,
732 .get_rc_config = it913x_get_rc_config,
733 .get_stream_config = it913x_get_stream_config,
734 .get_adapter_count = it913x_get_adapter_count,
735 .streaming_ctrl = it913x_streaming_ctrl,
736
737
738 .adapter = {
739 {
740 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
741 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
742 .pid_filter_count = 32,
743 .pid_filter = it913x_pid_filter,
744 .pid_filter_ctrl = it913x_pid_filter_ctrl,
745 .stream =
746 DVB_USB_STREAM_BULK(0x84, 10, TS_BUFFER_SIZE_MAX),
747 },
748 {
749 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
750 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
751 .pid_filter_count = 32,
752 .pid_filter = it913x_pid_filter,
753 .pid_filter_ctrl = it913x_pid_filter_ctrl,
754 .stream =
755 DVB_USB_STREAM_BULK(0x85, 10, TS_BUFFER_SIZE_MAX),
756 }
757 }
758};
759
760static const struct usb_device_id it913x_id_table[] = {
761 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
762 &it913x_properties, "Kworld UB499-2T T09(IT9137)",
763 RC_MAP_IT913X_V1) },
764 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
765 &it913x_properties, "ITE 9135 Generic",
766 RC_MAP_IT913X_V1) },
767 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
768 &it913x_properties, "Sveon STV22 Dual DVB-T HDTV(IT9137)",
769 RC_MAP_IT913X_V1) },
770 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
771 &it913x_properties, "ITE 9135(9005) Generic",
772 RC_MAP_IT913X_V2) },
773 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
774 &it913x_properties, "ITE 9135(9006) Generic",
775 RC_MAP_IT913X_V1) },
776 {} /* Terminating entry */
777};
778
779MODULE_DEVICE_TABLE(usb, it913x_id_table);
780
781static struct usb_driver it913x_driver = {
782 .name = KBUILD_MODNAME,
783 .probe = dvb_usbv2_probe,
784 .disconnect = dvb_usbv2_disconnect,
785 .suspend = dvb_usbv2_suspend,
786 .resume = dvb_usbv2_resume,
787 .id_table = it913x_id_table,
788};
789
790module_usb_driver(it913x_driver);
791
792MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
793MODULE_DESCRIPTION("it913x USB 2 Driver");
794MODULE_VERSION("1.32");
795MODULE_LICENSE("GPL");
796MODULE_FIRMWARE(FW_IT9135_V1);
797MODULE_FIRMWARE(FW_IT9135_V2);
798MODULE_FIRMWARE(FW_IT9137);
799
diff --git a/drivers/media/dvb/dvb-usb/Kconfig b/drivers/media/dvb/dvb-usb/Kconfig
index ec10c0f2f554..00173ee15d4a 100644
--- a/drivers/media/dvb/dvb-usb/Kconfig
+++ b/drivers/media/dvb/dvb-usb/Kconfig
@@ -311,11 +311,3 @@ config DVB_USB_TECHNISAT_USB2
311 select DVB_STV6110x if !DVB_FE_CUSTOMISE 311 select DVB_STV6110x if !DVB_FE_CUSTOMISE
312 help 312 help
313 Say Y here to support the Technisat USB2 DVB-S/S2 device 313 Say Y here to support the Technisat USB2 DVB-S/S2 device
314
315config DVB_USB_IT913X
316 tristate "it913x driver"
317 depends on DVB_USB
318 select DVB_IT913X_FE
319 help
320 Say Y here to support the it913x device
321
diff --git a/drivers/media/dvb/dvb-usb/Makefile b/drivers/media/dvb/dvb-usb/Makefile
index d1912e0e1681..061d5448fa1e 100644
--- a/drivers/media/dvb/dvb-usb/Makefile
+++ b/drivers/media/dvb/dvb-usb/Makefile
@@ -75,9 +75,6 @@ obj-$(CONFIG_DVB_USB_AZ6027) += dvb-usb-az6027.o
75dvb-usb-technisat-usb2-objs = technisat-usb2.o 75dvb-usb-technisat-usb2-objs = technisat-usb2.o
76obj-$(CONFIG_DVB_USB_TECHNISAT_USB2) += dvb-usb-technisat-usb2.o 76obj-$(CONFIG_DVB_USB_TECHNISAT_USB2) += dvb-usb-technisat-usb2.o
77 77
78dvb-usb-it913x-objs := it913x.o
79obj-$(CONFIG_DVB_USB_IT913X) += dvb-usb-it913x.o
80
81ccflags-y += -I$(srctree)/drivers/media/dvb/dvb-core 78ccflags-y += -I$(srctree)/drivers/media/dvb/dvb-core
82ccflags-y += -I$(srctree)/drivers/media/dvb/frontends/ 79ccflags-y += -I$(srctree)/drivers/media/dvb/frontends/
83# due to tuner-xc3028 80# due to tuner-xc3028
diff --git a/drivers/media/dvb/dvb-usb/it913x.c b/drivers/media/dvb/dvb-usb/it913x.c
deleted file mode 100644
index 6244fe9d1a3a..000000000000
--- a/drivers/media/dvb/dvb-usb/it913x.c
+++ /dev/null
@@ -1,931 +0,0 @@
1/* DVB USB compliant linux driver for IT9137
2 *
3 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
4 * IT9137 (C) ITE Tech Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 *
20 * see Documentation/dvb/README.dvb-usb for more information
21 * see Documentation/dvb/it9137.txt for firmware information
22 *
23 */
24#define DVB_USB_LOG_PREFIX "it913x"
25
26#include <linux/usb.h>
27#include <linux/usb/input.h>
28#include <media/rc-core.h>
29
30#include "dvb-usb.h"
31#include "it913x-fe.h"
32
33/* debug */
34static int dvb_usb_it913x_debug;
35#define l_dprintk(var, level, args...) do { \
36 if ((var >= level)) \
37 printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
38} while (0)
39
40#define deb_info(level, args...) l_dprintk(dvb_usb_it913x_debug, level, args)
41#define debug_data_snipet(level, name, p) \
42 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
43 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
44 *(p+5), *(p+6), *(p+7));
45
46
47module_param_named(debug, dvb_usb_it913x_debug, int, 0644);
48MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
49 DVB_USB_DEBUG_STATUS);
50
51static int pid_filter;
52module_param_named(pid, pid_filter, int, 0644);
53MODULE_PARM_DESC(pid, "set default 0=on 1=off");
54
55static int dvb_usb_it913x_firmware;
56module_param_named(firmware, dvb_usb_it913x_firmware, int, 0644);
57MODULE_PARM_DESC(firmware, "set firmware 0=auto 1=IT9137 2=IT9135V1");
58
59
60int cmd_counter;
61
62DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
63
64struct it913x_state {
65 u8 id;
66 struct ite_config it913x_config;
67 u8 pid_filter_onoff;
68};
69
70struct ite_config it913x_config;
71
72#define IT913X_RETRY 10
73#define IT913X_SND_TIMEOUT 100
74#define IT913X_RCV_TIMEOUT 200
75
76static int it913x_bulk_write(struct usb_device *dev,
77 u8 *snd, int len, u8 pipe)
78{
79 int ret, actual_l, i;
80
81 for (i = 0; i < IT913X_RETRY; i++) {
82 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
83 snd, len , &actual_l, IT913X_SND_TIMEOUT);
84 if (ret != -EBUSY && ret != -ETIMEDOUT)
85 break;
86 }
87
88 if (len != actual_l && ret == 0)
89 ret = -EAGAIN;
90
91 return ret;
92}
93
94static int it913x_bulk_read(struct usb_device *dev,
95 u8 *rev, int len, u8 pipe)
96{
97 int ret, actual_l, i;
98
99 for (i = 0; i < IT913X_RETRY; i++) {
100 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
101 rev, len , &actual_l, IT913X_RCV_TIMEOUT);
102 if (ret != -EBUSY && ret != -ETIMEDOUT)
103 break;
104 }
105
106 if (len != actual_l && ret == 0)
107 ret = -EAGAIN;
108
109 return ret;
110}
111
112static u16 check_sum(u8 *p, u8 len)
113{
114 u16 sum = 0;
115 u8 i = 1;
116 while (i < len)
117 sum += (i++ & 1) ? (*p++) << 8 : *p++;
118 return ~sum;
119}
120
121static int it913x_usb_talk(struct usb_device *udev, u8 mode, u8 pro,
122 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
123{
124 int ret = 0, i, buf_size = 1;
125 u8 *buff;
126 u8 rlen;
127 u16 chk_sum;
128
129 buff = kzalloc(256, GFP_KERNEL);
130 if (!buff) {
131 info("USB Buffer Failed");
132 return -ENOMEM;
133 }
134
135 buff[buf_size++] = pro;
136 buff[buf_size++] = cmd;
137 buff[buf_size++] = cmd_counter;
138
139 switch (mode) {
140 case READ_LONG:
141 case WRITE_LONG:
142 buff[buf_size++] = len;
143 buff[buf_size++] = 2;
144 buff[buf_size++] = (reg >> 24);
145 buff[buf_size++] = (reg >> 16) & 0xff;
146 buff[buf_size++] = (reg >> 8) & 0xff;
147 buff[buf_size++] = reg & 0xff;
148 break;
149 case READ_SHORT:
150 buff[buf_size++] = addr;
151 break;
152 case WRITE_SHORT:
153 buff[buf_size++] = len;
154 buff[buf_size++] = addr;
155 buff[buf_size++] = (reg >> 8) & 0xff;
156 buff[buf_size++] = reg & 0xff;
157 break;
158 case READ_DATA:
159 case WRITE_DATA:
160 break;
161 case WRITE_CMD:
162 mode = 7;
163 break;
164 default:
165 kfree(buff);
166 return -EINVAL;
167 }
168
169 if (mode & 1) {
170 for (i = 0; i < len ; i++)
171 buff[buf_size++] = data[i];
172 }
173 chk_sum = check_sum(&buff[1], buf_size);
174
175 buff[buf_size++] = chk_sum >> 8;
176 buff[0] = buf_size;
177 buff[buf_size++] = (chk_sum & 0xff);
178
179 ret = it913x_bulk_write(udev, buff, buf_size , 0x02);
180 if (ret < 0)
181 goto error;
182
183 ret = it913x_bulk_read(udev, buff, (mode & 1) ?
184 5 : len + 5 , 0x01);
185 if (ret < 0)
186 goto error;
187
188 rlen = (mode & 0x1) ? 0x1 : len;
189
190 if (mode & 1)
191 ret = buff[2];
192 else
193 memcpy(data, &buff[3], rlen);
194
195 cmd_counter++;
196
197error: kfree(buff);
198
199 return ret;
200}
201
202static int it913x_io(struct usb_device *udev, u8 mode, u8 pro,
203 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
204{
205 int ret, i;
206
207 for (i = 0; i < IT913X_RETRY; i++) {
208 ret = it913x_usb_talk(udev, mode, pro,
209 cmd, reg, addr, data, len);
210 if (ret != -EAGAIN)
211 break;
212 }
213
214 return ret;
215}
216
217static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data)
218{
219 int ret;
220 u8 b[1];
221 b[0] = data;
222 ret = it913x_io(udev, WRITE_LONG, pro,
223 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
224
225 return ret;
226}
227
228static int it913x_read_reg(struct usb_device *udev, u32 reg)
229{
230 int ret;
231 u8 data[1];
232
233 ret = it913x_io(udev, READ_LONG, DEV_0,
234 CMD_DEMOD_READ, reg, 0, &data[0], 1);
235
236 return (ret < 0) ? ret : data[0];
237}
238
239static u32 it913x_query(struct usb_device *udev, u8 pro)
240{
241 int ret, i;
242 u8 data[4];
243 u8 ver;
244
245 for (i = 0; i < 5; i++) {
246 ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ,
247 0x1222, 0, &data[0], 3);
248 ver = data[0];
249 if (ver > 0 && ver < 3)
250 break;
251 msleep(100);
252 }
253
254 if (ver < 1 || ver > 2) {
255 info("Failed to identify chip version applying 1");
256 it913x_config.chip_ver = 0x1;
257 it913x_config.chip_type = 0x9135;
258 return 0;
259 }
260
261 it913x_config.chip_ver = ver;
262 it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
263
264 info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver,
265 it913x_config.chip_type);
266
267 ret |= it913x_io(udev, READ_SHORT, pro,
268 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
269
270 it913x_config.firmware = (data[0] << 24) + (data[1] << 16) +
271 (data[2] << 8) + data[3];
272
273 return (ret < 0) ? 0 : it913x_config.firmware;
274}
275
276static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
277{
278 struct it913x_state *st = adap->dev->priv;
279 struct usb_device *udev = adap->dev->udev;
280 int ret;
281 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
282
283 mutex_lock(&adap->dev->i2c_mutex);
284
285 deb_info(1, "PID_C (%02x)", onoff);
286
287 ret = it913x_wr_reg(udev, pro, PID_EN, st->pid_filter_onoff);
288
289 mutex_unlock(&adap->dev->i2c_mutex);
290 return ret;
291}
292
293static int it913x_pid_filter(struct dvb_usb_adapter *adap,
294 int index, u16 pid, int onoff)
295{
296 struct it913x_state *st = adap->dev->priv;
297 struct usb_device *udev = adap->dev->udev;
298 int ret;
299 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
300
301 mutex_lock(&adap->dev->i2c_mutex);
302
303 deb_info(1, "PID_F (%02x)", onoff);
304
305 ret = it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
306
307 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
308
309 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
310
311 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
312
313 if (udev->speed == USB_SPEED_HIGH && pid == 0x2000) {
314 ret |= it913x_wr_reg(udev, pro, PID_EN, !onoff);
315 st->pid_filter_onoff = !onoff;
316 } else
317 st->pid_filter_onoff =
318 adap->fe_adap[adap->active_fe].pid_filtering;
319
320 mutex_unlock(&adap->dev->i2c_mutex);
321 return 0;
322}
323
324
325static int it913x_return_status(struct usb_device *udev)
326{
327 u32 firm = 0;
328
329 firm = it913x_query(udev, DEV_0);
330 if (firm > 0)
331 info("Firmware Version %d", firm);
332
333 return (firm > 0) ? firm : 0;
334}
335
336static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
337 int num)
338{
339 struct dvb_usb_device *d = i2c_get_adapdata(adap);
340 static u8 data[256];
341 int ret;
342 u32 reg;
343 u8 pro;
344
345 mutex_lock(&d->i2c_mutex);
346
347 debug_data_snipet(1, "Message out", msg[0].buf);
348 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
349
350 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
351 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
352 memcpy(data, msg[0].buf, msg[0].len);
353 reg = (data[0] << 24) + (data[1] << 16) +
354 (data[2] << 8) + data[3];
355 if (num == 2) {
356 ret = it913x_io(d->udev, READ_LONG, pro,
357 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
358 memcpy(msg[1].buf, data, msg[1].len);
359 } else
360 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
361 reg, 0, &data[4], msg[0].len - 4);
362
363 mutex_unlock(&d->i2c_mutex);
364
365 return ret;
366}
367
368static u32 it913x_i2c_func(struct i2c_adapter *adapter)
369{
370 return I2C_FUNC_I2C;
371}
372
373static struct i2c_algorithm it913x_i2c_algo = {
374 .master_xfer = it913x_i2c_xfer,
375 .functionality = it913x_i2c_func,
376};
377
378/* Callbacks for DVB USB */
379#define IT913X_POLL 250
380static int it913x_rc_query(struct dvb_usb_device *d)
381{
382 u8 ibuf[4];
383 int ret;
384 u32 key;
385 /* Avoid conflict with frontends*/
386 mutex_lock(&d->i2c_mutex);
387
388 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
389 0, 0, &ibuf[0], sizeof(ibuf));
390
391 if ((ibuf[2] + ibuf[3]) == 0xff) {
392 key = ibuf[2];
393 key += ibuf[0] << 16;
394 key += ibuf[1] << 8;
395 deb_info(1, "NEC Extended Key =%08x", key);
396 if (d->rc_dev != NULL)
397 rc_keydown(d->rc_dev, key, 0);
398 }
399
400 mutex_unlock(&d->i2c_mutex);
401
402 return ret;
403}
404
405/* Firmware sets raw */
406const char fw_it9135_v1[] = "dvb-usb-it9135-01.fw";
407const char fw_it9135_v2[] = "dvb-usb-it9135-02.fw";
408const char fw_it9137[] = "dvb-usb-it9137-01.fw";
409
410static int ite_firmware_select(struct usb_device *udev,
411 struct dvb_usb_device_properties *props)
412{
413 int sw;
414 /* auto switch */
415 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_KWORLD_2)
416 sw = IT9137_FW;
417 else if (it913x_config.chip_ver == 1)
418 sw = IT9135_V1_FW;
419 else
420 sw = IT9135_V2_FW;
421
422 /* force switch */
423 if (dvb_usb_it913x_firmware != IT9135_AUTO)
424 sw = dvb_usb_it913x_firmware;
425
426 switch (sw) {
427 case IT9135_V1_FW:
428 it913x_config.firmware_ver = 1;
429 it913x_config.adc_x2 = 1;
430 it913x_config.read_slevel = false;
431 props->firmware = fw_it9135_v1;
432 break;
433 case IT9135_V2_FW:
434 it913x_config.firmware_ver = 1;
435 it913x_config.adc_x2 = 1;
436 it913x_config.read_slevel = false;
437 props->firmware = fw_it9135_v2;
438 switch (it913x_config.tuner_id_0) {
439 case IT9135_61:
440 case IT9135_62:
441 break;
442 default:
443 info("Unknown tuner ID applying default 0x60");
444 case IT9135_60:
445 it913x_config.tuner_id_0 = IT9135_60;
446 }
447 break;
448 case IT9137_FW:
449 default:
450 it913x_config.firmware_ver = 0;
451 it913x_config.adc_x2 = 0;
452 it913x_config.read_slevel = true;
453 props->firmware = fw_it9137;
454 }
455
456 return 0;
457}
458
459static void it913x_select_remote(struct usb_device *udev,
460 struct dvb_usb_device_properties *props)
461{
462 switch (le16_to_cpu(udev->descriptor.idProduct)) {
463 case USB_PID_ITETECH_IT9135_9005:
464 props->rc.core.rc_codes = RC_MAP_IT913X_V2;
465 return;
466 default:
467 props->rc.core.rc_codes = RC_MAP_IT913X_V1;
468 }
469 return;
470}
471
472#define TS_MPEG_PKT_SIZE 188
473#define EP_LOW 21
474#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
475#define EP_HIGH 348
476#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
477
478static int it913x_select_config(struct usb_device *udev,
479 struct dvb_usb_device_properties *props)
480{
481 int ret = 0, reg;
482 bool proprietary_ir = false;
483
484 if (it913x_config.chip_ver == 0x02
485 && it913x_config.chip_type == 0x9135)
486 reg = it913x_read_reg(udev, 0x461d);
487 else
488 reg = it913x_read_reg(udev, 0x461b);
489
490 if (reg < 0)
491 return reg;
492
493 if (reg == 0) {
494 it913x_config.dual_mode = 0;
495 it913x_config.tuner_id_0 = IT9135_38;
496 proprietary_ir = true;
497 } else {
498 /* TS mode */
499 reg = it913x_read_reg(udev, 0x49c5);
500 if (reg < 0)
501 return reg;
502 it913x_config.dual_mode = reg;
503
504 /* IR mode type */
505 reg = it913x_read_reg(udev, 0x49ac);
506 if (reg < 0)
507 return reg;
508 if (reg == 5) {
509 info("Remote propriety (raw) mode");
510 proprietary_ir = true;
511 } else if (reg == 1) {
512 info("Remote HID mode NOT SUPPORTED");
513 proprietary_ir = false;
514 props->rc.core.rc_codes = NULL;
515 } else
516 props->rc.core.rc_codes = NULL;
517
518 /* Tuner_id */
519 reg = it913x_read_reg(udev, 0x49d0);
520 if (reg < 0)
521 return reg;
522 it913x_config.tuner_id_0 = reg;
523 }
524
525 if (proprietary_ir)
526 it913x_select_remote(udev, props);
527
528 if (udev->speed != USB_SPEED_HIGH) {
529 props->adapter[0].fe[0].pid_filter_count = 5;
530 info("USB 1 low speed mode - connect to USB 2 port");
531 if (pid_filter > 0)
532 pid_filter = 0;
533 if (it913x_config.dual_mode) {
534 it913x_config.dual_mode = 0;
535 info("Dual mode not supported in USB 1");
536 }
537 } else /* For replugging */
538 if(props->adapter[0].fe[0].pid_filter_count == 5)
539 props->adapter[0].fe[0].pid_filter_count = 31;
540
541 /* Select Stream Buffer Size and pid filter option*/
542 if (pid_filter) {
543 props->adapter[0].fe[0].stream.u.bulk.buffersize =
544 TS_BUFFER_SIZE_MAX;
545 props->adapter[0].fe[0].caps &=
546 ~DVB_USB_ADAP_NEED_PID_FILTERING;
547 } else
548 props->adapter[0].fe[0].stream.u.bulk.buffersize =
549 TS_BUFFER_SIZE_PID;
550
551 if (it913x_config.dual_mode) {
552 props->adapter[1].fe[0].stream.u.bulk.buffersize =
553 props->adapter[0].fe[0].stream.u.bulk.buffersize;
554 props->num_adapters = 2;
555 if (pid_filter)
556 props->adapter[1].fe[0].caps =
557 props->adapter[0].fe[0].caps;
558 } else
559 props->num_adapters = 1;
560
561 info("Dual mode=%x Tuner Type=%x", it913x_config.dual_mode,
562 it913x_config.tuner_id_0);
563
564 ret = ite_firmware_select(udev, props);
565
566 return ret;
567}
568
569static int it913x_identify_state(struct usb_device *udev,
570 struct dvb_usb_device_properties *props,
571 struct dvb_usb_device_description **desc,
572 int *cold)
573{
574 int ret = 0, firm_no;
575 u8 reg;
576
577 firm_no = it913x_return_status(udev);
578
579 /* Read and select config */
580 ret = it913x_select_config(udev, props);
581 if (ret < 0)
582 return ret;
583
584 if (firm_no > 0) {
585 *cold = 0;
586 return 0;
587 }
588
589 if (it913x_config.dual_mode) {
590 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
591 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
592 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
593 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
594 msleep(50);
595 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
596 msleep(50);
597 reg = it913x_read_reg(udev, GPIOH1_O);
598 if (reg == 0) {
599 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
600 ret |= it913x_return_status(udev);
601 if (ret != 0)
602 ret = it913x_wr_reg(udev, DEV_0,
603 GPIOH1_O, 0x0);
604 }
605 }
606
607 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
608
609 if (it913x_config.dual_mode) {
610 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
611 if (it913x_config.firmware_ver == 1)
612 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
613 else
614 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
615 } else {
616 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
617 if (it913x_config.firmware_ver == 1)
618 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
619 else
620 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
621 }
622
623 *cold = 1;
624
625 return (ret < 0) ? -ENODEV : 0;
626}
627
628static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
629{
630 struct it913x_state *st = adap->dev->priv;
631 int ret = 0;
632 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
633
634 deb_info(1, "STM (%02x)", onoff);
635
636 if (!onoff) {
637 mutex_lock(&adap->dev->i2c_mutex);
638
639 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
640
641 mutex_unlock(&adap->dev->i2c_mutex);
642 st->pid_filter_onoff =
643 adap->fe_adap[adap->active_fe].pid_filtering;
644
645 }
646
647 return ret;
648}
649
650static int it913x_download_firmware(struct usb_device *udev,
651 const struct firmware *fw)
652{
653 int ret = 0, i = 0, pos = 0;
654 u8 packet_size, min_pkt;
655 u8 *fw_data;
656
657 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
658
659 info("FRM Starting Firmware Download");
660
661 /* Multi firmware loader */
662 /* This uses scatter write firmware headers */
663 /* The firmware must start with 03 XX 00 */
664 /* and be the extact firmware length */
665
666 if (it913x_config.chip_ver == 2)
667 min_pkt = 0x11;
668 else
669 min_pkt = 0x19;
670
671 while (i <= fw->size) {
672 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
673 || (i == fw->size)) {
674 packet_size = i - pos;
675 if ((packet_size > min_pkt) || (i == fw->size)) {
676 fw_data = (u8 *)(fw->data + pos);
677 pos += packet_size;
678 if (packet_size > 0) {
679 ret = it913x_io(udev, WRITE_DATA,
680 DEV_0, CMD_SCATTER_WRITE, 0,
681 0, fw_data, packet_size);
682 if (ret < 0)
683 break;
684 }
685 udelay(1000);
686 }
687 }
688 i++;
689 }
690
691 if (ret < 0)
692 info("FRM Firmware Download Failed (%d)" , ret);
693 else
694 info("FRM Firmware Download Completed - Resetting Device");
695
696 msleep(30);
697
698 ret = it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
699 if (ret < 0)
700 info("FRM Device not responding to reboot");
701
702 ret = it913x_return_status(udev);
703 if (ret == 0) {
704 info("FRM Failed to reboot device");
705 return -ENODEV;
706 }
707
708 msleep(30);
709
710 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
711
712 msleep(30);
713
714 /* Tuner function */
715 if (it913x_config.dual_mode)
716 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
717 else
718 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
719
720 if ((it913x_config.chip_ver == 1) &&
721 (it913x_config.chip_type == 0x9135)) {
722 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
723 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
724 if (it913x_config.dual_mode) {
725 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
726 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
727 }
728 }
729
730 return (ret < 0) ? -ENODEV : 0;
731}
732
733static int it913x_name(struct dvb_usb_adapter *adap)
734{
735 const char *desc = adap->dev->desc->name;
736 char *fe_name[] = {"_1", "_2", "_3", "_4"};
737 char *name = adap->fe_adap[0].fe->ops.info.name;
738
739 strlcpy(name, desc, 128);
740 strlcat(name, fe_name[adap->id], 128);
741
742 return 0;
743}
744
745static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
746{
747 struct usb_device *udev = adap->dev->udev;
748 struct it913x_state *st = adap->dev->priv;
749 int ret = 0;
750 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
751 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
752 u8 pkt_size = 0x80;
753
754 if (adap->dev->udev->speed != USB_SPEED_HIGH)
755 pkt_size = 0x10;
756
757 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
758
759 if (adap->id == 0)
760 memcpy(&st->it913x_config, &it913x_config,
761 sizeof(struct ite_config));
762
763 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
764 &adap->dev->i2c_adap, adap_addr, &st->it913x_config);
765
766 if (adap->id == 0 && adap->fe_adap[0].fe) {
767 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
768 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
769 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
770 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
771 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
772 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
773 ep_size & 0xff);
774 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
775 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
776 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
777 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
778 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
779 ep_size & 0xff);
780 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
781 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
782 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
783 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
784 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
785 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
786 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
787 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
788 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
789 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
790 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
791 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
792 } else
793 return -ENODEV;
794
795 ret = it913x_name(adap);
796
797 return ret;
798}
799
800/* DVB USB Driver */
801static struct dvb_usb_device_properties it913x_properties;
802
803static int it913x_probe(struct usb_interface *intf,
804 const struct usb_device_id *id)
805{
806 cmd_counter = 0;
807 if (0 == dvb_usb_device_init(intf, &it913x_properties,
808 THIS_MODULE, NULL, adapter_nr)) {
809 info("DEV registering device driver");
810 return 0;
811 }
812
813 info("DEV it913x Error");
814 return -ENODEV;
815
816}
817
818static struct usb_device_id it913x_table[] = {
819 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
820 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
821 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
822 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005) },
823 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006) },
824 {} /* Terminating entry */
825};
826
827MODULE_DEVICE_TABLE(usb, it913x_table);
828
829static struct dvb_usb_device_properties it913x_properties = {
830 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
831 .usb_ctrl = DEVICE_SPECIFIC,
832 .download_firmware = it913x_download_firmware,
833 .firmware = "dvb-usb-it9137-01.fw",
834 .no_reconnect = 1,
835 .size_of_priv = sizeof(struct it913x_state),
836 .num_adapters = 2,
837 .adapter = {
838 {
839 .num_frontends = 1,
840 .fe = {{
841 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
842 DVB_USB_ADAP_NEED_PID_FILTERING|
843 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
844 .streaming_ctrl = it913x_streaming_ctrl,
845 .pid_filter_count = 31,
846 .pid_filter = it913x_pid_filter,
847 .pid_filter_ctrl = it913x_pid_filter_ctrl,
848 .frontend_attach = it913x_frontend_attach,
849 /* parameter for the MPEG2-data transfer */
850 .stream = {
851 .type = USB_BULK,
852 .count = 10,
853 .endpoint = 0x04,
854 .u = {/* Keep Low if PID filter on */
855 .bulk = {
856 .buffersize =
857 TS_BUFFER_SIZE_PID,
858 }
859 }
860 }
861 }},
862 },
863 {
864 .num_frontends = 1,
865 .fe = {{
866 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
867 DVB_USB_ADAP_NEED_PID_FILTERING|
868 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
869 .streaming_ctrl = it913x_streaming_ctrl,
870 .pid_filter_count = 31,
871 .pid_filter = it913x_pid_filter,
872 .pid_filter_ctrl = it913x_pid_filter_ctrl,
873 .frontend_attach = it913x_frontend_attach,
874 /* parameter for the MPEG2-data transfer */
875 .stream = {
876 .type = USB_BULK,
877 .count = 5,
878 .endpoint = 0x05,
879 .u = {
880 .bulk = {
881 .buffersize =
882 TS_BUFFER_SIZE_PID,
883 }
884 }
885 }
886 }},
887 }
888 },
889 .identify_state = it913x_identify_state,
890 .rc.core = {
891 .protocol = RC_TYPE_NEC,
892 .module_name = "it913x",
893 .rc_query = it913x_rc_query,
894 .rc_interval = IT913X_POLL,
895 .allowed_protos = RC_TYPE_NEC,
896 .rc_codes = RC_MAP_IT913X_V1,
897 },
898 .i2c_algo = &it913x_i2c_algo,
899 .num_device_descs = 5,
900 .devices = {
901 { "Kworld UB499-2T T09(IT9137)",
902 { &it913x_table[0], NULL },
903 },
904 { "ITE 9135 Generic",
905 { &it913x_table[1], NULL },
906 },
907 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
908 { &it913x_table[2], NULL },
909 },
910 { "ITE 9135(9005) Generic",
911 { &it913x_table[3], NULL },
912 },
913 { "ITE 9135(9006) Generic",
914 { &it913x_table[4], NULL },
915 },
916 }
917};
918
919static struct usb_driver it913x_driver = {
920 .name = "it913x",
921 .probe = it913x_probe,
922 .disconnect = dvb_usb_device_exit,
923 .id_table = it913x_table,
924};
925
926module_usb_driver(it913x_driver);
927
928MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
929MODULE_DESCRIPTION("it913x USB 2 Driver");
930MODULE_VERSION("1.28");
931MODULE_LICENSE("GPL");