Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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 */ |
| 34 | static 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 | |
| 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); |
| 50 | |
| 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"); |
| 54 | |
| 55 | int cmd_counter; |
| 56 | |
| 57 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 58 | |
| 59 | struct it913x_state { |
| 60 | u8 id; |
| 61 | }; |
| 62 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 63 | struct ite_config it913x_config; |
| 64 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 65 | static int it913x_bulk_write(struct usb_device *dev, |
| 66 | u8 *snd, int len, u8 pipe) |
| 67 | { |
| 68 | int ret, actual_l; |
| 69 | |
| 70 | ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe), |
| 71 | snd, len , &actual_l, 100); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | static int it913x_bulk_read(struct usb_device *dev, |
| 76 | u8 *rev, int len, u8 pipe) |
| 77 | { |
| 78 | int ret, actual_l; |
| 79 | |
| 80 | ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe), |
| 81 | rev, len , &actual_l, 200); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static u16 check_sum(u8 *p, u8 len) |
| 86 | { |
| 87 | u16 sum = 0; |
| 88 | u8 i = 1; |
| 89 | while (i < len) |
| 90 | sum += (i++ & 1) ? (*p++) << 8 : *p++; |
| 91 | return ~sum; |
| 92 | } |
| 93 | |
| 94 | static int it913x_io(struct usb_device *udev, u8 mode, u8 pro, |
| 95 | u8 cmd, u32 reg, u8 addr, u8 *data, u8 len) |
| 96 | { |
| 97 | int ret = 0, i, buf_size = 1; |
| 98 | u8 *buff; |
| 99 | u8 rlen; |
| 100 | u16 chk_sum; |
| 101 | |
| 102 | buff = kzalloc(256, GFP_KERNEL); |
| 103 | if (!buff) { |
| 104 | info("USB Buffer Failed"); |
| 105 | return -ENOMEM; |
| 106 | } |
| 107 | |
| 108 | buff[buf_size++] = pro; |
| 109 | buff[buf_size++] = cmd; |
| 110 | buff[buf_size++] = cmd_counter; |
| 111 | |
| 112 | switch (mode) { |
| 113 | case READ_LONG: |
| 114 | case WRITE_LONG: |
| 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; |
| 121 | break; |
| 122 | case READ_SHORT: |
| 123 | buff[buf_size++] = addr; |
| 124 | break; |
| 125 | case WRITE_SHORT: |
| 126 | buff[buf_size++] = len; |
| 127 | buff[buf_size++] = addr; |
| 128 | buff[buf_size++] = (reg >> 8) & 0xff; |
| 129 | buff[buf_size++] = reg & 0xff; |
| 130 | break; |
| 131 | case READ_DATA: |
| 132 | case WRITE_DATA: |
| 133 | break; |
| 134 | case WRITE_CMD: |
| 135 | mode = 7; |
| 136 | break; |
| 137 | default: |
| 138 | kfree(buff); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | if (mode & 1) { |
| 143 | for (i = 0; i < len ; i++) |
| 144 | buff[buf_size++] = data[i]; |
| 145 | } |
| 146 | chk_sum = check_sum(&buff[1], buf_size); |
| 147 | |
| 148 | buff[buf_size++] = chk_sum >> 8; |
| 149 | buff[0] = buf_size; |
| 150 | buff[buf_size++] = (chk_sum & 0xff); |
| 151 | |
| 152 | ret = it913x_bulk_write(udev, buff, buf_size , 0x02); |
| 153 | |
| 154 | ret |= it913x_bulk_read(udev, buff, (mode & 1) ? |
| 155 | 5 : len + 5 , 0x01); |
| 156 | |
| 157 | rlen = (mode & 0x1) ? 0x1 : len; |
| 158 | |
| 159 | if (mode & 1) |
| 160 | ret |= buff[2]; |
| 161 | else |
| 162 | memcpy(data, &buff[3], rlen); |
| 163 | |
| 164 | cmd_counter++; |
| 165 | |
| 166 | kfree(buff); |
| 167 | |
| 168 | return (ret < 0) ? -ENODEV : 0; |
| 169 | } |
| 170 | |
| 171 | static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data) |
| 172 | { |
| 173 | int ret; |
| 174 | u8 b[1]; |
| 175 | b[0] = data; |
| 176 | ret = it913x_io(udev, WRITE_LONG, pro, |
| 177 | CMD_DEMOD_WRITE, reg, 0, b, sizeof(b)); |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int it913x_read_reg(struct usb_device *udev, u32 reg) |
| 183 | { |
| 184 | int ret; |
| 185 | u8 data[1]; |
| 186 | |
| 187 | ret = it913x_io(udev, READ_LONG, DEV_0, |
| 188 | CMD_DEMOD_READ, reg, 0, &data[0], 1); |
| 189 | |
| 190 | return (ret < 0) ? ret : data[0]; |
| 191 | } |
| 192 | |
| 193 | static u32 it913x_query(struct usb_device *udev, u8 pro) |
| 194 | { |
| 195 | int ret; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 196 | u8 data[4]; |
| 197 | ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 198 | 0x1222, 0, &data[0], 3); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 199 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 200 | it913x_config.chip_ver = data[0]; |
| 201 | it913x_config.chip_type = (u16)(data[2] << 8) + data[1]; |
| 202 | |
| 203 | info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver, |
| 204 | it913x_config.chip_type); |
| 205 | |
| 206 | ret |= it913x_io(udev, READ_SHORT, pro, |
| 207 | CMD_QUERYINFO, 0, 0x1, &data[0], 4); |
| 208 | |
| 209 | it913x_config.firmware = (data[0] << 24) + (data[1] << 16) + |
| 210 | (data[2] << 8) + data[3]; |
| 211 | |
| 212 | return (ret < 0) ? 0 : it913x_config.firmware; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) |
| 216 | { |
| 217 | int ret = 0; |
| 218 | u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD; |
| 219 | |
| 220 | if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0) |
| 221 | return -EAGAIN; |
| 222 | deb_info(1, "PID_C (%02x)", onoff); |
| 223 | |
| 224 | if (!onoff) |
| 225 | ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1); |
| 226 | |
| 227 | mutex_unlock(&adap->dev->i2c_mutex); |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static int it913x_pid_filter(struct dvb_usb_adapter *adap, |
| 232 | int index, u16 pid, int onoff) |
| 233 | { |
| 234 | struct usb_device *udev = adap->dev->udev; |
| 235 | int ret = 0; |
| 236 | u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD; |
| 237 | |
| 238 | if (pid_filter > 0) |
| 239 | return 0; |
| 240 | |
| 241 | if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0) |
| 242 | return -EAGAIN; |
| 243 | deb_info(1, "PID_F (%02x)", onoff); |
| 244 | if (onoff) { |
| 245 | ret = it913x_wr_reg(udev, pro, PID_EN, 0x1); |
| 246 | |
| 247 | ret |= it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff)); |
| 248 | |
| 249 | ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8)); |
| 250 | |
| 251 | ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff); |
| 252 | |
| 253 | ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f)); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | mutex_unlock(&adap->dev->i2c_mutex); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | static int it913x_return_status(struct usb_device *udev) |
| 263 | { |
| 264 | u32 firm = 0; |
| 265 | |
| 266 | firm = it913x_query(udev, DEV_0); |
| 267 | if (firm > 0) |
| 268 | info("Firmware Version %d", firm); |
| 269 | |
| 270 | return (firm > 0) ? firm : 0; |
| 271 | } |
| 272 | |
| 273 | static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], |
| 274 | int num) |
| 275 | { |
| 276 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 277 | static u8 data[256]; |
| 278 | int ret; |
| 279 | u32 reg; |
| 280 | u8 pro; |
| 281 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 282 | return -EAGAIN; |
| 283 | |
| 284 | debug_data_snipet(1, "Message out", msg[0].buf); |
| 285 | deb_info(2, "num of messages %d address %02x", num, msg[0].addr); |
| 286 | |
| 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]; |
| 292 | if (num == 2) { |
| 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); |
| 296 | } else |
| 297 | ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE, |
| 298 | reg, 0, &data[4], msg[0].len - 4); |
| 299 | |
| 300 | mutex_unlock(&d->i2c_mutex); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static u32 it913x_i2c_func(struct i2c_adapter *adapter) |
| 306 | { |
| 307 | return I2C_FUNC_I2C; |
| 308 | } |
| 309 | |
| 310 | static struct i2c_algorithm it913x_i2c_algo = { |
| 311 | .master_xfer = it913x_i2c_xfer, |
| 312 | .functionality = it913x_i2c_func, |
| 313 | }; |
| 314 | |
| 315 | /* Callbacks for DVB USB */ |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 316 | #define IT913X_POLL 250 |
| 317 | static int it913x_rc_query(struct dvb_usb_device *d) |
| 318 | { |
| 319 | u8 ibuf[4]; |
| 320 | int ret; |
| 321 | u32 key; |
| 322 | /* Avoid conflict with frontends*/ |
| 323 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 324 | return -EAGAIN; |
| 325 | |
| 326 | ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET, |
| 327 | 0, 0, &ibuf[0], sizeof(ibuf)); |
| 328 | |
| 329 | if ((ibuf[2] + ibuf[3]) == 0xff) { |
| 330 | key = ibuf[2]; |
| 331 | key += ibuf[0] << 8; |
| 332 | deb_info(1, "INT Key =%08x", key); |
| 333 | if (d->rc_dev != NULL) |
| 334 | rc_keydown(d->rc_dev, key, 0); |
| 335 | } |
| 336 | mutex_unlock(&d->i2c_mutex); |
| 337 | |
| 338 | return ret; |
| 339 | } |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 340 | static int it913x_identify_state(struct usb_device *udev, |
| 341 | struct dvb_usb_device_properties *props, |
| 342 | struct dvb_usb_device_description **desc, |
| 343 | int *cold) |
| 344 | { |
| 345 | int ret = 0, firm_no; |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 346 | u8 reg, remote; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 347 | |
| 348 | firm_no = it913x_return_status(udev); |
| 349 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 350 | /* checnk for dual mode */ |
| 351 | it913x_config.dual_mode = it913x_read_reg(udev, 0x49c5); |
| 352 | |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame^] | 353 | if (udev->speed != USB_SPEED_HIGH) { |
| 354 | props->adapter[0].fe[0].pid_filter_count = 5; |
| 355 | info("USB 1 low speed mode - connect to USB 2 port"); |
| 356 | if (pid_filter > 0) |
| 357 | pid_filter = 0; |
| 358 | if (it913x_config.dual_mode) { |
| 359 | it913x_config.dual_mode = 0; |
| 360 | info("Dual mode not supported in USB 1"); |
| 361 | } |
| 362 | } else /* For replugging */ |
| 363 | if(props->adapter[0].fe[0].pid_filter_count == 5) |
| 364 | props->adapter[0].fe[0].pid_filter_count = 31; |
| 365 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 366 | /* TODO different remotes */ |
| 367 | remote = it913x_read_reg(udev, 0x49ac); /* Remote */ |
| 368 | if (remote == 0) |
| 369 | props->rc.core.rc_codes = NULL; |
| 370 | |
| 371 | /* TODO at the moment tuner_id is always assigned to 0x38 */ |
| 372 | it913x_config.tuner_id_0 = it913x_read_reg(udev, 0x49d0); |
| 373 | |
| 374 | info("Dual mode=%x Remote=%x Tuner Type=%x", it913x_config.dual_mode |
| 375 | , remote, it913x_config.tuner_id_0); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 376 | |
| 377 | if (firm_no > 0) { |
| 378 | *cold = 0; |
| 379 | return 0; |
| 380 | } |
| 381 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 382 | if (it913x_config.dual_mode) { |
| 383 | it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 384 | ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1); |
| 385 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1); |
| 386 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1); |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 387 | msleep(50); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 388 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0); |
| 389 | msleep(50); |
| 390 | reg = it913x_read_reg(udev, GPIOH1_O); |
| 391 | if (reg == 0) { |
| 392 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1); |
| 393 | ret |= it913x_return_status(udev); |
| 394 | if (ret != 0) |
| 395 | ret = it913x_wr_reg(udev, DEV_0, |
| 396 | GPIOH1_O, 0x0); |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 397 | props->num_adapters = 2; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 398 | } |
| 399 | } else |
| 400 | props->num_adapters = 1; |
| 401 | |
| 402 | reg = it913x_read_reg(udev, IO_MUX_POWER_CLK); |
| 403 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 404 | if (it913x_config.dual_mode) { |
| 405 | ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR); |
| 406 | ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1); |
| 407 | } else { |
| 408 | ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0); |
| 409 | ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0); |
| 410 | } |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 411 | |
| 412 | *cold = 1; |
| 413 | |
| 414 | return (ret < 0) ? -ENODEV : 0; |
| 415 | } |
| 416 | |
| 417 | static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) |
| 418 | { |
| 419 | int ret = 0; |
| 420 | u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD; |
| 421 | |
| 422 | if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0) |
| 423 | return -EAGAIN; |
| 424 | deb_info(1, "STM (%02x)", onoff); |
| 425 | |
| 426 | if (!onoff) |
| 427 | ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1); |
| 428 | |
| 429 | |
| 430 | mutex_unlock(&adap->dev->i2c_mutex); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | static int it913x_download_firmware(struct usb_device *udev, |
| 437 | const struct firmware *fw) |
| 438 | { |
| 439 | int ret = 0, i; |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 440 | u8 packet_size, dlen; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 441 | u8 *fw_data; |
| 442 | |
| 443 | packet_size = 0x29; |
| 444 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 445 | ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100); |
| 446 | |
| 447 | info("FRM Starting Firmware Download"); |
| 448 | /* This uses scatter write firmware headers follow */ |
| 449 | /* 03 XX 00 XX = chip number? */ |
| 450 | |
| 451 | for (i = 0; i < fw->size; i += packet_size) { |
| 452 | if (i > 0) |
| 453 | packet_size = 0x39; |
| 454 | fw_data = (u8 *)(fw->data + i); |
| 455 | dlen = ((i + packet_size) > fw->size) |
| 456 | ? (fw->size - i) : packet_size; |
| 457 | ret |= it913x_io(udev, WRITE_DATA, DEV_0, |
| 458 | CMD_SCATTER_WRITE, 0, 0, fw_data, dlen); |
| 459 | udelay(1000); |
| 460 | } |
| 461 | |
| 462 | ret |= it913x_io(udev, WRITE_CMD, DEV_0, |
| 463 | CMD_BOOT, 0, 0, NULL, 0); |
| 464 | |
| 465 | msleep(100); |
| 466 | |
| 467 | if (ret < 0) |
| 468 | info("FRM Firmware Download Failed (%04x)" , ret); |
| 469 | else |
| 470 | info("FRM Firmware Download Completed - Resetting Device"); |
| 471 | |
| 472 | ret |= it913x_return_status(udev); |
| 473 | |
| 474 | msleep(30); |
| 475 | |
| 476 | ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400); |
| 477 | |
| 478 | /* Tuner function */ |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 479 | if (it913x_config.dual_mode) |
| 480 | ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0); |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 481 | else |
| 482 | ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 483 | |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 484 | if ((it913x_config.chip_ver == 1) && |
| 485 | (it913x_config.chip_type == 0x9135)) { |
| 486 | ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0); |
| 487 | ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0); |
| 488 | if (it913x_config.dual_mode) { |
| 489 | ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0); |
| 490 | ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0); |
| 491 | } |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return (ret < 0) ? -ENODEV : 0; |
| 495 | } |
| 496 | |
| 497 | static int it913x_name(struct dvb_usb_adapter *adap) |
| 498 | { |
| 499 | const char *desc = adap->dev->desc->name; |
| 500 | char *fe_name[] = {"_1", "_2", "_3", "_4"}; |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 501 | char *name = adap->fe_adap[0].fe->ops.info.name; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 502 | |
| 503 | strlcpy(name, desc, 128); |
| 504 | strlcat(name, fe_name[adap->id], 128); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int it913x_frontend_attach(struct dvb_usb_adapter *adap) |
| 510 | { |
| 511 | struct usb_device *udev = adap->dev->udev; |
| 512 | int ret = 0; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 513 | u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5); |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 514 | u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize; |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame^] | 515 | u8 pkt_size = 0x80; |
| 516 | |
| 517 | if (adap->dev->udev->speed != USB_SPEED_HIGH) |
| 518 | pkt_size = 0x10; |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 519 | |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 520 | it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 521 | |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 522 | adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach, |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 523 | &adap->dev->i2c_adap, adap_addr, &it913x_config); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 524 | |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 525 | if (adap->id == 0 && adap->fe_adap[0].fe) { |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 526 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1); |
| 527 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1); |
| 528 | ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f); |
| 529 | ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b); |
| 530 | ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f); |
| 531 | ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB, |
| 532 | ep_size & 0xff); |
| 533 | ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8); |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame^] | 534 | ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size); |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 535 | } else if (adap->id == 1 && adap->fe_adap[0].fe) { |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 536 | ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f); |
| 537 | ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB, |
| 538 | ep_size & 0xff); |
| 539 | ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8); |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame^] | 540 | ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 541 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1); |
| 542 | ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1); |
| 543 | ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1); |
| 544 | ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1); |
| 545 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0); |
| 546 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0); |
| 547 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0); |
| 548 | ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1); |
| 549 | ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0); |
| 550 | ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0); |
| 551 | } else |
| 552 | return -ENODEV; |
| 553 | |
| 554 | ret = it913x_name(adap); |
| 555 | |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | /* DVB USB Driver */ |
| 560 | static struct dvb_usb_device_properties it913x_properties; |
| 561 | |
| 562 | static int it913x_probe(struct usb_interface *intf, |
| 563 | const struct usb_device_id *id) |
| 564 | { |
| 565 | cmd_counter = 0; |
| 566 | if (0 == dvb_usb_device_init(intf, &it913x_properties, |
| 567 | THIS_MODULE, NULL, adapter_nr)) { |
| 568 | info("DEV registering device driver"); |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | info("DEV it913x Error"); |
| 573 | return -ENODEV; |
| 574 | |
| 575 | } |
| 576 | |
| 577 | static struct usb_device_id it913x_table[] = { |
| 578 | { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) }, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 579 | { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 580 | {} /* Terminating entry */ |
| 581 | }; |
| 582 | |
| 583 | MODULE_DEVICE_TABLE(usb, it913x_table); |
| 584 | |
| 585 | static struct dvb_usb_device_properties it913x_properties = { |
| 586 | .caps = DVB_USB_IS_AN_I2C_ADAPTER, |
| 587 | .usb_ctrl = DEVICE_SPECIFIC, |
| 588 | .download_firmware = it913x_download_firmware, |
| 589 | .firmware = "dvb-usb-it9137-01.fw", |
| 590 | .no_reconnect = 1, |
| 591 | .size_of_priv = sizeof(struct it913x_state), |
| 592 | .num_adapters = 2, |
| 593 | .adapter = { |
| 594 | { |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 595 | .num_frontends = 1, |
| 596 | .fe = {{ |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 597 | .caps = DVB_USB_ADAP_HAS_PID_FILTER| |
| 598 | DVB_USB_ADAP_NEED_PID_FILTERING| |
| 599 | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, |
| 600 | .streaming_ctrl = it913x_streaming_ctrl, |
| 601 | .pid_filter_count = 31, |
| 602 | .pid_filter = it913x_pid_filter, |
| 603 | .pid_filter_ctrl = it913x_pid_filter_ctrl, |
| 604 | .frontend_attach = it913x_frontend_attach, |
| 605 | /* parameter for the MPEG2-data transfer */ |
| 606 | .stream = { |
| 607 | .type = USB_BULK, |
| 608 | .count = 10, |
| 609 | .endpoint = 0x04, |
| 610 | .u = {/* Keep Low if PID filter on */ |
| 611 | .bulk = { |
| 612 | .buffersize = 3584, |
| 613 | |
| 614 | } |
| 615 | } |
| 616 | } |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 617 | }}, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 618 | }, |
| 619 | { |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 620 | .num_frontends = 1, |
| 621 | .fe = {{ |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 622 | .caps = DVB_USB_ADAP_HAS_PID_FILTER| |
| 623 | DVB_USB_ADAP_NEED_PID_FILTERING| |
| 624 | DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, |
| 625 | .streaming_ctrl = it913x_streaming_ctrl, |
| 626 | .pid_filter_count = 31, |
| 627 | .pid_filter = it913x_pid_filter, |
| 628 | .pid_filter_ctrl = it913x_pid_filter_ctrl, |
| 629 | .frontend_attach = it913x_frontend_attach, |
| 630 | /* parameter for the MPEG2-data transfer */ |
| 631 | .stream = { |
| 632 | .type = USB_BULK, |
| 633 | .count = 5, |
| 634 | .endpoint = 0x05, |
| 635 | .u = { |
| 636 | .bulk = { |
| 637 | .buffersize = 3584, |
| 638 | |
| 639 | } |
| 640 | } |
| 641 | } |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 642 | }}, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 643 | } |
| 644 | }, |
| 645 | .identify_state = it913x_identify_state, |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 646 | .rc.core = { |
| 647 | .protocol = RC_TYPE_NEC, |
| 648 | .module_name = "it913x", |
| 649 | .rc_query = it913x_rc_query, |
| 650 | .rc_interval = IT913X_POLL, |
| 651 | .allowed_protos = RC_TYPE_NEC, |
| 652 | .rc_codes = RC_MAP_KWORLD_315U, |
| 653 | }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 654 | .i2c_algo = &it913x_i2c_algo, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 655 | .num_device_descs = 2, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 656 | .devices = { |
| 657 | { "Kworld UB499-2T T09(IT9137)", |
| 658 | { &it913x_table[0], NULL }, |
| 659 | }, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 660 | { "ITE 9135 Generic", |
| 661 | { &it913x_table[1], NULL }, |
| 662 | }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 663 | } |
| 664 | }; |
| 665 | |
| 666 | static struct usb_driver it913x_driver = { |
| 667 | .name = "it913x", |
| 668 | .probe = it913x_probe, |
| 669 | .disconnect = dvb_usb_device_exit, |
| 670 | .id_table = it913x_table, |
| 671 | }; |
| 672 | |
| 673 | /* module stuff */ |
| 674 | static int __init it913x_module_init(void) |
| 675 | { |
| 676 | int result = usb_register(&it913x_driver); |
| 677 | if (result) { |
| 678 | err("usb_register failed. Error number %d", result); |
| 679 | return result; |
| 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static void __exit it913x_module_exit(void) |
| 686 | { |
| 687 | /* deregister this driver from the USB subsystem */ |
| 688 | usb_deregister(&it913x_driver); |
| 689 | } |
| 690 | |
| 691 | module_init(it913x_module_init); |
| 692 | module_exit(it913x_module_exit); |
| 693 | |
| 694 | MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>"); |
| 695 | MODULE_DESCRIPTION("it913x USB 2 Driver"); |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame^] | 696 | MODULE_VERSION("1.09"); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 697 | MODULE_LICENSE("GPL"); |