blob: d7c86c2b6a2d65991edd36531178a17a40387003 [file] [log] [blame]
Malcolm Priestleyf6d87352011-07-25 15:35:03 -03001/* 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
Malcolm Priestley5e642c02011-11-30 17:16:09 -030055static 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
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030060int cmd_counter;
61
62DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
63
64struct it913x_state {
65 u8 id;
66};
67
Malcolm Priestleybc549192011-10-14 19:54:11 -030068struct ite_config it913x_config;
69
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030070static int it913x_bulk_write(struct usb_device *dev,
71 u8 *snd, int len, u8 pipe)
72{
73 int ret, actual_l;
74
75 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
76 snd, len , &actual_l, 100);
77 return ret;
78}
79
80static int it913x_bulk_read(struct usb_device *dev,
81 u8 *rev, int len, u8 pipe)
82{
83 int ret, actual_l;
84
85 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
86 rev, len , &actual_l, 200);
87 return ret;
88}
89
90static u16 check_sum(u8 *p, u8 len)
91{
92 u16 sum = 0;
93 u8 i = 1;
94 while (i < len)
95 sum += (i++ & 1) ? (*p++) << 8 : *p++;
96 return ~sum;
97}
98
99static int it913x_io(struct usb_device *udev, u8 mode, u8 pro,
100 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
101{
102 int ret = 0, i, buf_size = 1;
103 u8 *buff;
104 u8 rlen;
105 u16 chk_sum;
106
107 buff = kzalloc(256, GFP_KERNEL);
108 if (!buff) {
109 info("USB Buffer Failed");
110 return -ENOMEM;
111 }
112
113 buff[buf_size++] = pro;
114 buff[buf_size++] = cmd;
115 buff[buf_size++] = cmd_counter;
116
117 switch (mode) {
118 case READ_LONG:
119 case WRITE_LONG:
120 buff[buf_size++] = len;
121 buff[buf_size++] = 2;
122 buff[buf_size++] = (reg >> 24);
123 buff[buf_size++] = (reg >> 16) & 0xff;
124 buff[buf_size++] = (reg >> 8) & 0xff;
125 buff[buf_size++] = reg & 0xff;
126 break;
127 case READ_SHORT:
128 buff[buf_size++] = addr;
129 break;
130 case WRITE_SHORT:
131 buff[buf_size++] = len;
132 buff[buf_size++] = addr;
133 buff[buf_size++] = (reg >> 8) & 0xff;
134 buff[buf_size++] = reg & 0xff;
135 break;
136 case READ_DATA:
137 case WRITE_DATA:
138 break;
139 case WRITE_CMD:
140 mode = 7;
141 break;
142 default:
143 kfree(buff);
144 return -EINVAL;
145 }
146
147 if (mode & 1) {
148 for (i = 0; i < len ; i++)
149 buff[buf_size++] = data[i];
150 }
151 chk_sum = check_sum(&buff[1], buf_size);
152
153 buff[buf_size++] = chk_sum >> 8;
154 buff[0] = buf_size;
155 buff[buf_size++] = (chk_sum & 0xff);
156
157 ret = it913x_bulk_write(udev, buff, buf_size , 0x02);
158
159 ret |= it913x_bulk_read(udev, buff, (mode & 1) ?
160 5 : len + 5 , 0x01);
161
162 rlen = (mode & 0x1) ? 0x1 : len;
163
164 if (mode & 1)
165 ret |= buff[2];
166 else
167 memcpy(data, &buff[3], rlen);
168
169 cmd_counter++;
170
171 kfree(buff);
172
173 return (ret < 0) ? -ENODEV : 0;
174}
175
176static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data)
177{
178 int ret;
179 u8 b[1];
180 b[0] = data;
181 ret = it913x_io(udev, WRITE_LONG, pro,
182 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
183
184 return ret;
185}
186
187static int it913x_read_reg(struct usb_device *udev, u32 reg)
188{
189 int ret;
190 u8 data[1];
191
192 ret = it913x_io(udev, READ_LONG, DEV_0,
193 CMD_DEMOD_READ, reg, 0, &data[0], 1);
194
195 return (ret < 0) ? ret : data[0];
196}
197
198static u32 it913x_query(struct usb_device *udev, u8 pro)
199{
200 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300201 u8 data[4];
202 ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ,
Malcolm Priestleybc549192011-10-14 19:54:11 -0300203 0x1222, 0, &data[0], 3);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300204
Malcolm Priestleybc549192011-10-14 19:54:11 -0300205 it913x_config.chip_ver = data[0];
206 it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
207
208 info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver,
209 it913x_config.chip_type);
210
211 ret |= it913x_io(udev, READ_SHORT, pro,
212 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
213
214 it913x_config.firmware = (data[0] << 24) + (data[1] << 16) +
215 (data[2] << 8) + data[3];
216
217 return (ret < 0) ? 0 : it913x_config.firmware;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300218}
219
220static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
221{
222 int ret = 0;
223 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
224
225 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
226 return -EAGAIN;
227 deb_info(1, "PID_C (%02x)", onoff);
228
229 if (!onoff)
230 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
231
232 mutex_unlock(&adap->dev->i2c_mutex);
233 return ret;
234}
235
236static int it913x_pid_filter(struct dvb_usb_adapter *adap,
237 int index, u16 pid, int onoff)
238{
239 struct usb_device *udev = adap->dev->udev;
240 int ret = 0;
241 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
242
243 if (pid_filter > 0)
244 return 0;
245
246 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
247 return -EAGAIN;
248 deb_info(1, "PID_F (%02x)", onoff);
249 if (onoff) {
250 ret = it913x_wr_reg(udev, pro, PID_EN, 0x1);
251
252 ret |= it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
253
254 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
255
256 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
257
258 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
259
260 }
261
262 mutex_unlock(&adap->dev->i2c_mutex);
263 return 0;
264}
265
266
267static int it913x_return_status(struct usb_device *udev)
268{
269 u32 firm = 0;
270
271 firm = it913x_query(udev, DEV_0);
272 if (firm > 0)
273 info("Firmware Version %d", firm);
274
275 return (firm > 0) ? firm : 0;
276}
277
278static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
279 int num)
280{
281 struct dvb_usb_device *d = i2c_get_adapdata(adap);
282 static u8 data[256];
283 int ret;
284 u32 reg;
285 u8 pro;
286 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
287 return -EAGAIN;
288
289 debug_data_snipet(1, "Message out", msg[0].buf);
290 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
291
292 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
293 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
294 memcpy(data, msg[0].buf, msg[0].len);
295 reg = (data[0] << 24) + (data[1] << 16) +
296 (data[2] << 8) + data[3];
297 if (num == 2) {
298 ret = it913x_io(d->udev, READ_LONG, pro,
299 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
300 memcpy(msg[1].buf, data, msg[1].len);
301 } else
302 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
303 reg, 0, &data[4], msg[0].len - 4);
304
305 mutex_unlock(&d->i2c_mutex);
306
307 return ret;
308}
309
310static u32 it913x_i2c_func(struct i2c_adapter *adapter)
311{
312 return I2C_FUNC_I2C;
313}
314
315static struct i2c_algorithm it913x_i2c_algo = {
316 .master_xfer = it913x_i2c_xfer,
317 .functionality = it913x_i2c_func,
318};
319
320/* Callbacks for DVB USB */
tvboxspy2ba0f942011-09-21 18:57:41 -0300321#define IT913X_POLL 250
322static int it913x_rc_query(struct dvb_usb_device *d)
323{
324 u8 ibuf[4];
325 int ret;
326 u32 key;
327 /* Avoid conflict with frontends*/
328 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
329 return -EAGAIN;
330
331 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
332 0, 0, &ibuf[0], sizeof(ibuf));
333
334 if ((ibuf[2] + ibuf[3]) == 0xff) {
335 key = ibuf[2];
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300336 key += ibuf[0] << 16;
337 key += ibuf[1] << 8;
338 deb_info(1, "NEC Extended Key =%08x", key);
tvboxspy2ba0f942011-09-21 18:57:41 -0300339 if (d->rc_dev != NULL)
340 rc_keydown(d->rc_dev, key, 0);
341 }
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300342
tvboxspy2ba0f942011-09-21 18:57:41 -0300343 mutex_unlock(&d->i2c_mutex);
344
345 return ret;
346}
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300347
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300348/* Firmware sets raw */
349const char fw_it9135_v1[] = "dvb-usb-it9135-01.fw";
350const char fw_it9137[] = "dvb-usb-it9137-01.fw";
351
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300352static int ite_firmware_select(struct usb_device *udev,
353 struct dvb_usb_device_properties *props)
354{
355 int sw;
356 /* auto switch */
357 if (le16_to_cpu(udev->descriptor.idProduct) ==
358 USB_PID_ITETECH_IT9135)
359 sw = IT9135_V1_FW;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300360 else if (le16_to_cpu(udev->descriptor.idProduct) ==
361 USB_PID_ITETECH_IT9135_9005)
362 sw = IT9135_V1_FW;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300363 else
364 sw = IT9137_FW;
365
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300366 /* force switch */
367 if (dvb_usb_it913x_firmware != IT9135_AUTO)
368 sw = dvb_usb_it913x_firmware;
369
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300370 switch (sw) {
371 case IT9135_V1_FW:
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300372 it913x_config.firmware_ver = 1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300373 it913x_config.adc_x2 = 1;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300374 props->firmware = fw_it9135_v1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300375 break;
376 case IT9137_FW:
377 default:
378 it913x_config.firmware_ver = 0;
379 it913x_config.adc_x2 = 0;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300380 props->firmware = fw_it9137;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300381 }
382
383 return 0;
384}
385
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300386#define TS_MPEG_PKT_SIZE 188
387#define EP_LOW 21
388#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
389#define EP_HIGH 348
390#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
391
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300392static int it913x_identify_state(struct usb_device *udev,
393 struct dvb_usb_device_properties *props,
394 struct dvb_usb_device_description **desc,
395 int *cold)
396{
397 int ret = 0, firm_no;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300398 u8 reg, remote;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300399
400 firm_no = it913x_return_status(udev);
401
Malcolm Priestleybc549192011-10-14 19:54:11 -0300402 /* checnk for dual mode */
403 it913x_config.dual_mode = it913x_read_reg(udev, 0x49c5);
404
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300405 if (udev->speed != USB_SPEED_HIGH) {
406 props->adapter[0].fe[0].pid_filter_count = 5;
407 info("USB 1 low speed mode - connect to USB 2 port");
408 if (pid_filter > 0)
409 pid_filter = 0;
410 if (it913x_config.dual_mode) {
411 it913x_config.dual_mode = 0;
412 info("Dual mode not supported in USB 1");
413 }
414 } else /* For replugging */
415 if(props->adapter[0].fe[0].pid_filter_count == 5)
416 props->adapter[0].fe[0].pid_filter_count = 31;
417
Malcolm Priestleybc549192011-10-14 19:54:11 -0300418 /* TODO different remotes */
419 remote = it913x_read_reg(udev, 0x49ac); /* Remote */
420 if (remote == 0)
421 props->rc.core.rc_codes = NULL;
422
423 /* TODO at the moment tuner_id is always assigned to 0x38 */
424 it913x_config.tuner_id_0 = it913x_read_reg(udev, 0x49d0);
425
426 info("Dual mode=%x Remote=%x Tuner Type=%x", it913x_config.dual_mode
427 , remote, it913x_config.tuner_id_0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300428
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300429 /* Select Stream Buffer Size */
430 if (pid_filter)
431 props->adapter[0].fe[0].stream.u.bulk.buffersize =
432 TS_BUFFER_SIZE_MAX;
433 else
434 props->adapter[0].fe[0].stream.u.bulk.buffersize =
435 TS_BUFFER_SIZE_PID;
436 if (it913x_config.dual_mode)
437 props->adapter[1].fe[0].stream.u.bulk.buffersize =
438 props->adapter[0].fe[0].stream.u.bulk.buffersize;
439
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300440 ret = ite_firmware_select(udev, props);
441
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300442 if (firm_no > 0) {
443 *cold = 0;
444 return 0;
445 }
446
Malcolm Priestleybc549192011-10-14 19:54:11 -0300447 if (it913x_config.dual_mode) {
448 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300449 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
450 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
451 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300452 msleep(50);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300453 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
454 msleep(50);
455 reg = it913x_read_reg(udev, GPIOH1_O);
456 if (reg == 0) {
457 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
458 ret |= it913x_return_status(udev);
459 if (ret != 0)
460 ret = it913x_wr_reg(udev, DEV_0,
461 GPIOH1_O, 0x0);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300462 props->num_adapters = 2;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300463 }
464 } else
465 props->num_adapters = 1;
466
467 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
468
Malcolm Priestleybc549192011-10-14 19:54:11 -0300469 if (it913x_config.dual_mode) {
470 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300471 if (it913x_config.firmware_ver == 1)
472 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
473 else
474 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300475 } else {
476 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300477 if (it913x_config.firmware_ver == 1)
478 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
479 else
480 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300481 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300482
483 *cold = 1;
484
485 return (ret < 0) ? -ENODEV : 0;
486}
487
488static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
489{
490 int ret = 0;
491 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
492
493 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
494 return -EAGAIN;
495 deb_info(1, "STM (%02x)", onoff);
496
497 if (!onoff)
498 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
499
500
501 mutex_unlock(&adap->dev->i2c_mutex);
502
503 return ret;
504}
505
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300506static int it913x_download_firmware(struct usb_device *udev,
507 const struct firmware *fw)
508{
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300509 int ret = 0, i = 0, pos = 0;
510 u8 packet_size;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300511 u8 *fw_data;
512
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300513 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
514
515 info("FRM Starting Firmware Download");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300516
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300517 /* Multi firmware loader */
518 /* This uses scatter write firmware headers */
519 /* The firmware must start with 03 XX 00 */
520 /* and be the extact firmware length */
521
522 while (i <= fw->size) {
523 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
524 || (i == fw->size)) {
525 packet_size = i - pos;
526 if ((packet_size > 0x19) || (i == fw->size)) {
527 fw_data = (u8 *)(fw->data + pos);
528 pos += packet_size;
529 if (packet_size > 0)
530 ret |= it913x_io(udev, WRITE_DATA,
531 DEV_0, CMD_SCATTER_WRITE, 0,
532 0, fw_data, packet_size);
533 udelay(1000);
534 }
535 }
536 i++;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300537 }
538
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300539 ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300540
541 msleep(100);
542
543 if (ret < 0)
544 info("FRM Firmware Download Failed (%04x)" , ret);
545 else
546 info("FRM Firmware Download Completed - Resetting Device");
547
548 ret |= it913x_return_status(udev);
549
550 msleep(30);
551
552 ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
553
554 /* Tuner function */
Malcolm Priestleybc549192011-10-14 19:54:11 -0300555 if (it913x_config.dual_mode)
556 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300557 else
558 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300559
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300560 if ((it913x_config.chip_ver == 1) &&
561 (it913x_config.chip_type == 0x9135)) {
562 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
563 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
564 if (it913x_config.dual_mode) {
565 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
566 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
567 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300568 }
569
570 return (ret < 0) ? -ENODEV : 0;
571}
572
573static int it913x_name(struct dvb_usb_adapter *adap)
574{
575 const char *desc = adap->dev->desc->name;
576 char *fe_name[] = {"_1", "_2", "_3", "_4"};
Michael Krufky77eed212011-09-06 09:31:57 -0300577 char *name = adap->fe_adap[0].fe->ops.info.name;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300578
579 strlcpy(name, desc, 128);
580 strlcat(name, fe_name[adap->id], 128);
581
582 return 0;
583}
584
585static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
586{
587 struct usb_device *udev = adap->dev->udev;
588 int ret = 0;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300589 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300590 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300591 u8 pkt_size = 0x80;
592
593 if (adap->dev->udev->speed != USB_SPEED_HIGH)
594 pkt_size = 0x10;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300595
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300596 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300597
Michael Krufky77eed212011-09-06 09:31:57 -0300598 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300599 &adap->dev->i2c_adap, adap_addr, &it913x_config);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300600
Michael Krufky77eed212011-09-06 09:31:57 -0300601 if (adap->id == 0 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300602 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
603 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
604 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
605 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
606 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
607 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
608 ep_size & 0xff);
609 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300610 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
Michael Krufky77eed212011-09-06 09:31:57 -0300611 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300612 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
613 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
614 ep_size & 0xff);
615 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300616 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300617 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
618 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
619 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
620 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
621 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
622 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
623 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
624 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
625 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
626 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
627 } else
628 return -ENODEV;
629
630 ret = it913x_name(adap);
631
632 return ret;
633}
634
635/* DVB USB Driver */
636static struct dvb_usb_device_properties it913x_properties;
637
638static int it913x_probe(struct usb_interface *intf,
639 const struct usb_device_id *id)
640{
641 cmd_counter = 0;
642 if (0 == dvb_usb_device_init(intf, &it913x_properties,
643 THIS_MODULE, NULL, adapter_nr)) {
644 info("DEV registering device driver");
645 return 0;
646 }
647
648 info("DEV it913x Error");
649 return -ENODEV;
650
651}
652
653static struct usb_device_id it913x_table[] = {
654 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300655 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300656 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300657 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005) },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300658 {} /* Terminating entry */
659};
660
661MODULE_DEVICE_TABLE(usb, it913x_table);
662
663static struct dvb_usb_device_properties it913x_properties = {
664 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
665 .usb_ctrl = DEVICE_SPECIFIC,
666 .download_firmware = it913x_download_firmware,
667 .firmware = "dvb-usb-it9137-01.fw",
668 .no_reconnect = 1,
669 .size_of_priv = sizeof(struct it913x_state),
670 .num_adapters = 2,
671 .adapter = {
672 {
Michael Krufky77eed212011-09-06 09:31:57 -0300673 .num_frontends = 1,
674 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300675 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
676 DVB_USB_ADAP_NEED_PID_FILTERING|
677 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
678 .streaming_ctrl = it913x_streaming_ctrl,
679 .pid_filter_count = 31,
680 .pid_filter = it913x_pid_filter,
681 .pid_filter_ctrl = it913x_pid_filter_ctrl,
682 .frontend_attach = it913x_frontend_attach,
683 /* parameter for the MPEG2-data transfer */
684 .stream = {
685 .type = USB_BULK,
686 .count = 10,
687 .endpoint = 0x04,
688 .u = {/* Keep Low if PID filter on */
689 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300690 .buffersize =
691 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300692 }
693 }
694 }
Michael Krufky77eed212011-09-06 09:31:57 -0300695 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300696 },
697 {
Michael Krufky77eed212011-09-06 09:31:57 -0300698 .num_frontends = 1,
699 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300700 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
701 DVB_USB_ADAP_NEED_PID_FILTERING|
702 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
703 .streaming_ctrl = it913x_streaming_ctrl,
704 .pid_filter_count = 31,
705 .pid_filter = it913x_pid_filter,
706 .pid_filter_ctrl = it913x_pid_filter_ctrl,
707 .frontend_attach = it913x_frontend_attach,
708 /* parameter for the MPEG2-data transfer */
709 .stream = {
710 .type = USB_BULK,
711 .count = 5,
712 .endpoint = 0x05,
713 .u = {
714 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300715 .buffersize =
716 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300717 }
718 }
719 }
Michael Krufky77eed212011-09-06 09:31:57 -0300720 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300721 }
722 },
723 .identify_state = it913x_identify_state,
tvboxspy2ba0f942011-09-21 18:57:41 -0300724 .rc.core = {
725 .protocol = RC_TYPE_NEC,
726 .module_name = "it913x",
727 .rc_query = it913x_rc_query,
728 .rc_interval = IT913X_POLL,
729 .allowed_protos = RC_TYPE_NEC,
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300730 .rc_codes = RC_MAP_MSI_DIGIVOX_III,
tvboxspy2ba0f942011-09-21 18:57:41 -0300731 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300732 .i2c_algo = &it913x_i2c_algo,
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300733 .num_device_descs = 3,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300734 .devices = {
735 { "Kworld UB499-2T T09(IT9137)",
736 { &it913x_table[0], NULL },
737 },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300738 { "ITE 9135 Generic",
739 { &it913x_table[1], NULL },
740 },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300741 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
742 { &it913x_table[2], NULL },
743 },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300744 { "ITE 9135(9005) Generic",
745 { &it913x_table[3], NULL },
746 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300747 }
748};
749
750static struct usb_driver it913x_driver = {
751 .name = "it913x",
752 .probe = it913x_probe,
753 .disconnect = dvb_usb_device_exit,
754 .id_table = it913x_table,
755};
756
757/* module stuff */
758static int __init it913x_module_init(void)
759{
760 int result = usb_register(&it913x_driver);
761 if (result) {
762 err("usb_register failed. Error number %d", result);
763 return result;
764 }
765
766 return 0;
767}
768
769static void __exit it913x_module_exit(void)
770{
771 /* deregister this driver from the USB subsystem */
772 usb_deregister(&it913x_driver);
773}
774
775module_init(it913x_module_init);
776module_exit(it913x_module_exit);
777
778MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
779MODULE_DESCRIPTION("it913x USB 2 Driver");
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300780MODULE_VERSION("1.14");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300781MODULE_LICENSE("GPL");