1 /* DVB USB compliant linux driver for IT9137
3 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
4 * IT9137 (C) ITE Tech Inc.
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.
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.
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.
20 * see Documentation/dvb/README.dvb-usb for more information
21 * see Documentation/dvb/it9137.txt for firmware information
24 #define DVB_USB_LOG_PREFIX "it913x"
26 #include <linux/usb.h>
27 #include <linux/usb/input.h>
28 #include <media/rc-core.h>
31 #include "it913x-fe.h"
34 static int dvb_usb_it913x_debug;
35 #define l_dprintk(var, level, args...) do { \
37 printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
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));
47 module_param_named(debug, dvb_usb_it913x_debug, int, 0644);
48 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
49 DVB_USB_DEBUG_STATUS);
51 static int pid_filter;
52 module_param_named(pid, pid_filter, int, 0644);
53 MODULE_PARM_DESC(pid, "set default 0=on 1=off");
57 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
63 struct ite_config it913x_config;
65 static int it913x_bulk_write(struct usb_device *dev,
66 u8 *snd, int len, u8 pipe)
70 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
71 snd, len , &actual_l, 100);
75 static int it913x_bulk_read(struct usb_device *dev,
76 u8 *rev, int len, u8 pipe)
80 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
81 rev, len , &actual_l, 200);
85 static u16 check_sum(u8 *p, u8 len)
90 sum += (i++ & 1) ? (*p++) << 8 : *p++;
94 static int it913x_io(struct usb_device *udev, u8 mode, u8 pro,
95 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
97 int ret = 0, i, buf_size = 1;
102 buff = kzalloc(256, GFP_KERNEL);
104 info("USB Buffer Failed");
108 buff[buf_size++] = pro;
109 buff[buf_size++] = cmd;
110 buff[buf_size++] = cmd_counter;
115 buff[buf_size++] = len;
116 buff[buf_size++] = 2;
117 buff[buf_size++] = (reg >> 24);
118 buff[buf_size++] = (reg >> 16) & 0xff;
119 buff[buf_size++] = (reg >> 8) & 0xff;
120 buff[buf_size++] = reg & 0xff;
123 buff[buf_size++] = addr;
126 buff[buf_size++] = len;
127 buff[buf_size++] = addr;
128 buff[buf_size++] = (reg >> 8) & 0xff;
129 buff[buf_size++] = reg & 0xff;
143 for (i = 0; i < len ; i++)
144 buff[buf_size++] = data[i];
146 chk_sum = check_sum(&buff[1], buf_size);
148 buff[buf_size++] = chk_sum >> 8;
150 buff[buf_size++] = (chk_sum & 0xff);
152 ret = it913x_bulk_write(udev, buff, buf_size , 0x02);
154 ret |= it913x_bulk_read(udev, buff, (mode & 1) ?
157 rlen = (mode & 0x1) ? 0x1 : len;
162 memcpy(data, &buff[3], rlen);
168 return (ret < 0) ? -ENODEV : 0;
171 static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data)
176 ret = it913x_io(udev, WRITE_LONG, pro,
177 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
182 static int it913x_read_reg(struct usb_device *udev, u32 reg)
187 ret = it913x_io(udev, READ_LONG, DEV_0,
188 CMD_DEMOD_READ, reg, 0, &data[0], 1);
190 return (ret < 0) ? ret : data[0];
193 static u32 it913x_query(struct usb_device *udev, u8 pro)
197 ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ,
198 0x1222, 0, &data[0], 3);
200 it913x_config.chip_ver = data[0];
201 it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
203 info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver,
204 it913x_config.chip_type);
206 ret |= it913x_io(udev, READ_SHORT, pro,
207 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
209 it913x_config.firmware = (data[0] << 24) + (data[1] << 16) +
210 (data[2] << 8) + data[3];
212 return (ret < 0) ? 0 : it913x_config.firmware;
215 static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
218 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
220 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
222 deb_info(1, "PID_C (%02x)", onoff);
225 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
227 mutex_unlock(&adap->dev->i2c_mutex);
231 static int it913x_pid_filter(struct dvb_usb_adapter *adap,
232 int index, u16 pid, int onoff)
234 struct usb_device *udev = adap->dev->udev;
236 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
241 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
243 deb_info(1, "PID_F (%02x)", onoff);
245 ret = it913x_wr_reg(udev, pro, PID_EN, 0x1);
247 ret |= it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
249 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
251 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
253 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
257 mutex_unlock(&adap->dev->i2c_mutex);
262 static int it913x_return_status(struct usb_device *udev)
266 firm = it913x_query(udev, DEV_0);
268 info("Firmware Version %d", firm);
270 return (firm > 0) ? firm : 0;
273 static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
276 struct dvb_usb_device *d = i2c_get_adapdata(adap);
281 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
284 debug_data_snipet(1, "Message out", msg[0].buf);
285 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
287 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
288 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
289 memcpy(data, msg[0].buf, msg[0].len);
290 reg = (data[0] << 24) + (data[1] << 16) +
291 (data[2] << 8) + data[3];
293 ret = it913x_io(d->udev, READ_LONG, pro,
294 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
295 memcpy(msg[1].buf, data, msg[1].len);
297 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
298 reg, 0, &data[4], msg[0].len - 4);
300 mutex_unlock(&d->i2c_mutex);
305 static u32 it913x_i2c_func(struct i2c_adapter *adapter)
310 static struct i2c_algorithm it913x_i2c_algo = {
311 .master_xfer = it913x_i2c_xfer,
312 .functionality = it913x_i2c_func,
315 /* Callbacks for DVB USB */
316 #define IT913X_POLL 250
317 static int it913x_rc_query(struct dvb_usb_device *d)
322 /* Avoid conflict with frontends*/
323 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
326 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
327 0, 0, &ibuf[0], sizeof(ibuf));
329 if ((ibuf[2] + ibuf[3]) == 0xff) {
331 key += ibuf[0] << 16;
333 deb_info(1, "NEC Extended Key =%08x", key);
334 if (d->rc_dev != NULL)
335 rc_keydown(d->rc_dev, key, 0);
338 mutex_unlock(&d->i2c_mutex);
343 static int ite_firmware_select(struct usb_device *udev,
344 struct dvb_usb_device_properties *props)
348 if (le16_to_cpu(udev->descriptor.idProduct) ==
349 USB_PID_ITETECH_IT9135)
356 it913x_config.firmware_ver = 0;
357 it913x_config.adc_x2 = 1;
361 it913x_config.firmware_ver = 0;
362 it913x_config.adc_x2 = 0;
368 #define TS_MPEG_PKT_SIZE 188
370 #define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
372 #define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
374 static int it913x_identify_state(struct usb_device *udev,
375 struct dvb_usb_device_properties *props,
376 struct dvb_usb_device_description **desc,
379 int ret = 0, firm_no;
382 firm_no = it913x_return_status(udev);
384 /* checnk for dual mode */
385 it913x_config.dual_mode = it913x_read_reg(udev, 0x49c5);
387 if (udev->speed != USB_SPEED_HIGH) {
388 props->adapter[0].fe[0].pid_filter_count = 5;
389 info("USB 1 low speed mode - connect to USB 2 port");
392 if (it913x_config.dual_mode) {
393 it913x_config.dual_mode = 0;
394 info("Dual mode not supported in USB 1");
396 } else /* For replugging */
397 if(props->adapter[0].fe[0].pid_filter_count == 5)
398 props->adapter[0].fe[0].pid_filter_count = 31;
400 /* TODO different remotes */
401 remote = it913x_read_reg(udev, 0x49ac); /* Remote */
403 props->rc.core.rc_codes = NULL;
405 /* TODO at the moment tuner_id is always assigned to 0x38 */
406 it913x_config.tuner_id_0 = it913x_read_reg(udev, 0x49d0);
408 info("Dual mode=%x Remote=%x Tuner Type=%x", it913x_config.dual_mode
409 , remote, it913x_config.tuner_id_0);
411 /* Select Stream Buffer Size */
413 props->adapter[0].fe[0].stream.u.bulk.buffersize =
416 props->adapter[0].fe[0].stream.u.bulk.buffersize =
418 if (it913x_config.dual_mode)
419 props->adapter[1].fe[0].stream.u.bulk.buffersize =
420 props->adapter[0].fe[0].stream.u.bulk.buffersize;
422 ret = ite_firmware_select(udev, props);
429 if (it913x_config.dual_mode) {
430 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
431 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
432 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
433 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
435 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
437 reg = it913x_read_reg(udev, GPIOH1_O);
439 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
440 ret |= it913x_return_status(udev);
442 ret = it913x_wr_reg(udev, DEV_0,
444 props->num_adapters = 2;
447 props->num_adapters = 1;
449 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
451 if (it913x_config.dual_mode) {
452 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
453 if (it913x_config.firmware_ver == 1)
454 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
456 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
458 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
459 if (it913x_config.firmware_ver == 1)
460 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
462 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
467 return (ret < 0) ? -ENODEV : 0;
470 static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
473 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
475 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
477 deb_info(1, "STM (%02x)", onoff);
480 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
483 mutex_unlock(&adap->dev->i2c_mutex);
488 static int it913x_download_firmware(struct usb_device *udev,
489 const struct firmware *fw)
491 int ret = 0, i = 0, pos = 0;
495 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
497 info("FRM Starting Firmware Download");
499 /* Multi firmware loader */
500 /* This uses scatter write firmware headers */
501 /* The firmware must start with 03 XX 00 */
502 /* and be the extact firmware length */
504 while (i <= fw->size) {
505 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
506 || (i == fw->size)) {
507 packet_size = i - pos;
508 if ((packet_size > 0x19) || (i == fw->size)) {
509 fw_data = (u8 *)(fw->data + pos);
512 ret |= it913x_io(udev, WRITE_DATA,
513 DEV_0, CMD_SCATTER_WRITE, 0,
514 0, fw_data, packet_size);
521 ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
526 info("FRM Firmware Download Failed (%04x)" , ret);
528 info("FRM Firmware Download Completed - Resetting Device");
530 ret |= it913x_return_status(udev);
534 ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
537 if (it913x_config.dual_mode)
538 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
540 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
542 if ((it913x_config.chip_ver == 1) &&
543 (it913x_config.chip_type == 0x9135)) {
544 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
545 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
546 if (it913x_config.dual_mode) {
547 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
548 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
552 return (ret < 0) ? -ENODEV : 0;
555 static int it913x_name(struct dvb_usb_adapter *adap)
557 const char *desc = adap->dev->desc->name;
558 char *fe_name[] = {"_1", "_2", "_3", "_4"};
559 char *name = adap->fe_adap[0].fe->ops.info.name;
561 strlcpy(name, desc, 128);
562 strlcat(name, fe_name[adap->id], 128);
567 static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
569 struct usb_device *udev = adap->dev->udev;
571 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
572 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
575 if (adap->dev->udev->speed != USB_SPEED_HIGH)
578 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
580 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
581 &adap->dev->i2c_adap, adap_addr, &it913x_config);
583 if (adap->id == 0 && adap->fe_adap[0].fe) {
584 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
585 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
586 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
587 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
588 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
589 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
591 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
592 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
593 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
594 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
595 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
597 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
598 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
599 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
600 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
601 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
602 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
603 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
604 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
605 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
606 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
607 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
608 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
612 ret = it913x_name(adap);
618 static struct dvb_usb_device_properties it913x_properties;
620 static int it913x_probe(struct usb_interface *intf,
621 const struct usb_device_id *id)
624 if (0 == dvb_usb_device_init(intf, &it913x_properties,
625 THIS_MODULE, NULL, adapter_nr)) {
626 info("DEV registering device driver");
630 info("DEV it913x Error");
635 static struct usb_device_id it913x_table[] = {
636 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
637 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
638 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
639 {} /* Terminating entry */
642 MODULE_DEVICE_TABLE(usb, it913x_table);
644 static struct dvb_usb_device_properties it913x_properties = {
645 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
646 .usb_ctrl = DEVICE_SPECIFIC,
647 .download_firmware = it913x_download_firmware,
648 .firmware = "dvb-usb-it9137-01.fw",
650 .size_of_priv = sizeof(struct it913x_state),
656 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
657 DVB_USB_ADAP_NEED_PID_FILTERING|
658 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
659 .streaming_ctrl = it913x_streaming_ctrl,
660 .pid_filter_count = 31,
661 .pid_filter = it913x_pid_filter,
662 .pid_filter_ctrl = it913x_pid_filter_ctrl,
663 .frontend_attach = it913x_frontend_attach,
664 /* parameter for the MPEG2-data transfer */
669 .u = {/* Keep Low if PID filter on */
681 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
682 DVB_USB_ADAP_NEED_PID_FILTERING|
683 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
684 .streaming_ctrl = it913x_streaming_ctrl,
685 .pid_filter_count = 31,
686 .pid_filter = it913x_pid_filter,
687 .pid_filter_ctrl = it913x_pid_filter_ctrl,
688 .frontend_attach = it913x_frontend_attach,
689 /* parameter for the MPEG2-data transfer */
704 .identify_state = it913x_identify_state,
706 .protocol = RC_TYPE_NEC,
707 .module_name = "it913x",
708 .rc_query = it913x_rc_query,
709 .rc_interval = IT913X_POLL,
710 .allowed_protos = RC_TYPE_NEC,
711 .rc_codes = RC_MAP_MSI_DIGIVOX_III,
713 .i2c_algo = &it913x_i2c_algo,
714 .num_device_descs = 3,
716 { "Kworld UB499-2T T09(IT9137)",
717 { &it913x_table[0], NULL },
719 { "ITE 9135 Generic",
720 { &it913x_table[1], NULL },
722 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
723 { &it913x_table[2], NULL },
728 static struct usb_driver it913x_driver = {
730 .probe = it913x_probe,
731 .disconnect = dvb_usb_device_exit,
732 .id_table = it913x_table,
736 static int __init it913x_module_init(void)
738 int result = usb_register(&it913x_driver);
740 err("usb_register failed. Error number %d", result);
747 static void __exit it913x_module_exit(void)
749 /* deregister this driver from the USB subsystem */
750 usb_deregister(&it913x_driver);
753 module_init(it913x_module_init);
754 module_exit(it913x_module_exit);
756 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
757 MODULE_DESCRIPTION("it913x USB 2 Driver");
758 MODULE_VERSION("1.11");
759 MODULE_LICENSE("GPL");