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]; |
Malcolm Priestley | c725ff6 | 2011-11-28 18:22:41 -0300 | [diff] [blame] | 331 | key += ibuf[0] << 16; |
| 332 | key += ibuf[1] << 8; |
| 333 | deb_info(1, "NEC Extended Key =%08x", key); |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 334 | if (d->rc_dev != NULL) |
| 335 | rc_keydown(d->rc_dev, key, 0); |
| 336 | } |
Malcolm Priestley | c725ff6 | 2011-11-28 18:22:41 -0300 | [diff] [blame] | 337 | |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 338 | mutex_unlock(&d->i2c_mutex); |
| 339 | |
| 340 | return ret; |
| 341 | } |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 342 | |
Malcolm Priestley | 990f49a | 2011-11-28 18:04:21 -0300 | [diff] [blame] | 343 | static int ite_firmware_select(struct usb_device *udev, |
| 344 | struct dvb_usb_device_properties *props) |
| 345 | { |
| 346 | int sw; |
| 347 | /* auto switch */ |
| 348 | if (le16_to_cpu(udev->descriptor.idProduct) == |
| 349 | USB_PID_ITETECH_IT9135) |
| 350 | sw = IT9135_V1_FW; |
| 351 | else |
| 352 | sw = IT9137_FW; |
| 353 | |
| 354 | switch (sw) { |
| 355 | case IT9135_V1_FW: |
| 356 | it913x_config.firmware_ver = 0; |
| 357 | it913x_config.adc_x2 = 1; |
| 358 | break; |
| 359 | case IT9137_FW: |
| 360 | default: |
| 361 | it913x_config.firmware_ver = 0; |
| 362 | it913x_config.adc_x2 = 0; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 368 | #define TS_MPEG_PKT_SIZE 188 |
| 369 | #define EP_LOW 21 |
| 370 | #define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE) |
| 371 | #define EP_HIGH 348 |
| 372 | #define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE) |
| 373 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 374 | static int it913x_identify_state(struct usb_device *udev, |
| 375 | struct dvb_usb_device_properties *props, |
| 376 | struct dvb_usb_device_description **desc, |
| 377 | int *cold) |
| 378 | { |
| 379 | int ret = 0, firm_no; |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 380 | u8 reg, remote; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 381 | |
| 382 | firm_no = it913x_return_status(udev); |
| 383 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 384 | /* checnk for dual mode */ |
| 385 | it913x_config.dual_mode = it913x_read_reg(udev, 0x49c5); |
| 386 | |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame] | 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"); |
| 390 | if (pid_filter > 0) |
| 391 | pid_filter = 0; |
| 392 | if (it913x_config.dual_mode) { |
| 393 | it913x_config.dual_mode = 0; |
| 394 | info("Dual mode not supported in USB 1"); |
| 395 | } |
| 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; |
| 399 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 400 | /* TODO different remotes */ |
| 401 | remote = it913x_read_reg(udev, 0x49ac); /* Remote */ |
| 402 | if (remote == 0) |
| 403 | props->rc.core.rc_codes = NULL; |
| 404 | |
| 405 | /* TODO at the moment tuner_id is always assigned to 0x38 */ |
| 406 | it913x_config.tuner_id_0 = it913x_read_reg(udev, 0x49d0); |
| 407 | |
| 408 | info("Dual mode=%x Remote=%x Tuner Type=%x", it913x_config.dual_mode |
| 409 | , remote, it913x_config.tuner_id_0); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 410 | |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 411 | /* Select Stream Buffer Size */ |
| 412 | if (pid_filter) |
| 413 | props->adapter[0].fe[0].stream.u.bulk.buffersize = |
| 414 | TS_BUFFER_SIZE_MAX; |
| 415 | else |
| 416 | props->adapter[0].fe[0].stream.u.bulk.buffersize = |
| 417 | TS_BUFFER_SIZE_PID; |
| 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; |
| 421 | |
Malcolm Priestley | 990f49a | 2011-11-28 18:04:21 -0300 | [diff] [blame] | 422 | ret = ite_firmware_select(udev, props); |
| 423 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 424 | if (firm_no > 0) { |
| 425 | *cold = 0; |
| 426 | return 0; |
| 427 | } |
| 428 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 429 | if (it913x_config.dual_mode) { |
| 430 | it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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); |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 434 | msleep(50); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 435 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0); |
| 436 | msleep(50); |
| 437 | reg = it913x_read_reg(udev, GPIOH1_O); |
| 438 | if (reg == 0) { |
| 439 | ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1); |
| 440 | ret |= it913x_return_status(udev); |
| 441 | if (ret != 0) |
| 442 | ret = it913x_wr_reg(udev, DEV_0, |
| 443 | GPIOH1_O, 0x0); |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 444 | props->num_adapters = 2; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 445 | } |
| 446 | } else |
| 447 | props->num_adapters = 1; |
| 448 | |
| 449 | reg = it913x_read_reg(udev, IO_MUX_POWER_CLK); |
| 450 | |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 451 | if (it913x_config.dual_mode) { |
| 452 | ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR); |
Malcolm Priestley | 990f49a | 2011-11-28 18:04:21 -0300 | [diff] [blame] | 453 | if (it913x_config.firmware_ver == 1) |
| 454 | ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1); |
| 455 | else |
| 456 | ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1); |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 457 | } else { |
| 458 | ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0); |
Malcolm Priestley | 990f49a | 2011-11-28 18:04:21 -0300 | [diff] [blame] | 459 | if (it913x_config.firmware_ver == 1) |
| 460 | ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0); |
| 461 | else |
| 462 | ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0); |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 463 | } |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 464 | |
| 465 | *cold = 1; |
| 466 | |
| 467 | return (ret < 0) ? -ENODEV : 0; |
| 468 | } |
| 469 | |
| 470 | static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) |
| 471 | { |
| 472 | int ret = 0; |
| 473 | u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD; |
| 474 | |
| 475 | if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0) |
| 476 | return -EAGAIN; |
| 477 | deb_info(1, "STM (%02x)", onoff); |
| 478 | |
| 479 | if (!onoff) |
| 480 | ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1); |
| 481 | |
| 482 | |
| 483 | mutex_unlock(&adap->dev->i2c_mutex); |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 488 | static int it913x_download_firmware(struct usb_device *udev, |
| 489 | const struct firmware *fw) |
| 490 | { |
Malcolm Priestley | b699029 | 2011-11-30 17:14:47 -0300 | [diff] [blame^] | 491 | int ret = 0, i = 0, pos = 0; |
| 492 | u8 packet_size; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 493 | u8 *fw_data; |
| 494 | |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 495 | ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100); |
| 496 | |
| 497 | info("FRM Starting Firmware Download"); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 498 | |
Malcolm Priestley | b699029 | 2011-11-30 17:14:47 -0300 | [diff] [blame^] | 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 */ |
| 503 | |
| 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); |
| 510 | pos += packet_size; |
| 511 | if (packet_size > 0) |
| 512 | ret |= it913x_io(udev, WRITE_DATA, |
| 513 | DEV_0, CMD_SCATTER_WRITE, 0, |
| 514 | 0, fw_data, packet_size); |
| 515 | udelay(1000); |
| 516 | } |
| 517 | } |
| 518 | i++; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 519 | } |
| 520 | |
Malcolm Priestley | b699029 | 2011-11-30 17:14:47 -0300 | [diff] [blame^] | 521 | ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 522 | |
| 523 | msleep(100); |
| 524 | |
| 525 | if (ret < 0) |
| 526 | info("FRM Firmware Download Failed (%04x)" , ret); |
| 527 | else |
| 528 | info("FRM Firmware Download Completed - Resetting Device"); |
| 529 | |
| 530 | ret |= it913x_return_status(udev); |
| 531 | |
| 532 | msleep(30); |
| 533 | |
| 534 | ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400); |
| 535 | |
| 536 | /* Tuner function */ |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 537 | if (it913x_config.dual_mode) |
| 538 | ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0); |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 539 | else |
| 540 | ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 541 | |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 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); |
| 549 | } |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | return (ret < 0) ? -ENODEV : 0; |
| 553 | } |
| 554 | |
| 555 | static int it913x_name(struct dvb_usb_adapter *adap) |
| 556 | { |
| 557 | const char *desc = adap->dev->desc->name; |
| 558 | char *fe_name[] = {"_1", "_2", "_3", "_4"}; |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 559 | char *name = adap->fe_adap[0].fe->ops.info.name; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 560 | |
| 561 | strlcpy(name, desc, 128); |
| 562 | strlcat(name, fe_name[adap->id], 128); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int it913x_frontend_attach(struct dvb_usb_adapter *adap) |
| 568 | { |
| 569 | struct usb_device *udev = adap->dev->udev; |
| 570 | int ret = 0; |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 571 | u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5); |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 572 | u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4; |
Malcolm Priestley | 3822c7c | 2011-11-06 10:24:30 -0300 | [diff] [blame] | 573 | u8 pkt_size = 0x80; |
| 574 | |
| 575 | if (adap->dev->udev->speed != USB_SPEED_HIGH) |
| 576 | pkt_size = 0x10; |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 577 | |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 578 | it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 579 | |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 580 | adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach, |
Malcolm Priestley | b7d425d39 | 2011-10-31 12:02:08 -0300 | [diff] [blame] | 581 | &adap->dev->i2c_adap, adap_addr, &it913x_config); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 582 | |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 583 | if (adap->id == 0 && adap->fe_adap[0].fe) { |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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, |
| 590 | ep_size & 0xff); |
| 591 | 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] | 592 | ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size); |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 593 | } else if (adap->id == 1 && adap->fe_adap[0].fe) { |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 594 | ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f); |
| 595 | ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB, |
| 596 | ep_size & 0xff); |
| 597 | 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] | 598 | ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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); |
| 609 | } else |
| 610 | return -ENODEV; |
| 611 | |
| 612 | ret = it913x_name(adap); |
| 613 | |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | /* DVB USB Driver */ |
| 618 | static struct dvb_usb_device_properties it913x_properties; |
| 619 | |
| 620 | static int it913x_probe(struct usb_interface *intf, |
| 621 | const struct usb_device_id *id) |
| 622 | { |
| 623 | cmd_counter = 0; |
| 624 | if (0 == dvb_usb_device_init(intf, &it913x_properties, |
| 625 | THIS_MODULE, NULL, adapter_nr)) { |
| 626 | info("DEV registering device driver"); |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | info("DEV it913x Error"); |
| 631 | return -ENODEV; |
| 632 | |
| 633 | } |
| 634 | |
| 635 | static struct usb_device_id it913x_table[] = { |
| 636 | { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) }, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 637 | { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) }, |
Malcolm Priestley | fdb5a91 | 2011-11-06 18:30:26 -0300 | [diff] [blame] | 638 | { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 639 | {} /* Terminating entry */ |
| 640 | }; |
| 641 | |
| 642 | MODULE_DEVICE_TABLE(usb, it913x_table); |
| 643 | |
| 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", |
| 649 | .no_reconnect = 1, |
| 650 | .size_of_priv = sizeof(struct it913x_state), |
| 651 | .num_adapters = 2, |
| 652 | .adapter = { |
| 653 | { |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 654 | .num_frontends = 1, |
| 655 | .fe = {{ |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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 */ |
| 665 | .stream = { |
| 666 | .type = USB_BULK, |
| 667 | .count = 10, |
| 668 | .endpoint = 0x04, |
| 669 | .u = {/* Keep Low if PID filter on */ |
| 670 | .bulk = { |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 671 | .buffersize = |
| 672 | TS_BUFFER_SIZE_PID, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 676 | }}, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 677 | }, |
| 678 | { |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 679 | .num_frontends = 1, |
| 680 | .fe = {{ |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 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 */ |
| 690 | .stream = { |
| 691 | .type = USB_BULK, |
| 692 | .count = 5, |
| 693 | .endpoint = 0x05, |
| 694 | .u = { |
| 695 | .bulk = { |
Malcolm Priestley | 9c1133c | 2011-11-27 17:35:06 -0300 | [diff] [blame] | 696 | .buffersize = |
| 697 | TS_BUFFER_SIZE_PID, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
Michael Krufky | 77eed21 | 2011-09-06 09:31:57 -0300 | [diff] [blame] | 701 | }}, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 702 | } |
| 703 | }, |
| 704 | .identify_state = it913x_identify_state, |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 705 | .rc.core = { |
| 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, |
Malcolm Priestley | c725ff6 | 2011-11-28 18:22:41 -0300 | [diff] [blame] | 711 | .rc_codes = RC_MAP_MSI_DIGIVOX_III, |
tvboxspy | 2ba0f94 | 2011-09-21 18:57:41 -0300 | [diff] [blame] | 712 | }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 713 | .i2c_algo = &it913x_i2c_algo, |
Malcolm Priestley | fdb5a91 | 2011-11-06 18:30:26 -0300 | [diff] [blame] | 714 | .num_device_descs = 3, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 715 | .devices = { |
| 716 | { "Kworld UB499-2T T09(IT9137)", |
| 717 | { &it913x_table[0], NULL }, |
| 718 | }, |
Malcolm Priestley | bc54919 | 2011-10-14 19:54:11 -0300 | [diff] [blame] | 719 | { "ITE 9135 Generic", |
| 720 | { &it913x_table[1], NULL }, |
| 721 | }, |
Malcolm Priestley | fdb5a91 | 2011-11-06 18:30:26 -0300 | [diff] [blame] | 722 | { "Sveon STV22 Dual DVB-T HDTV(IT9137)", |
| 723 | { &it913x_table[2], NULL }, |
| 724 | }, |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 725 | } |
| 726 | }; |
| 727 | |
| 728 | static struct usb_driver it913x_driver = { |
| 729 | .name = "it913x", |
| 730 | .probe = it913x_probe, |
| 731 | .disconnect = dvb_usb_device_exit, |
| 732 | .id_table = it913x_table, |
| 733 | }; |
| 734 | |
| 735 | /* module stuff */ |
| 736 | static int __init it913x_module_init(void) |
| 737 | { |
| 738 | int result = usb_register(&it913x_driver); |
| 739 | if (result) { |
| 740 | err("usb_register failed. Error number %d", result); |
| 741 | return result; |
| 742 | } |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static void __exit it913x_module_exit(void) |
| 748 | { |
| 749 | /* deregister this driver from the USB subsystem */ |
| 750 | usb_deregister(&it913x_driver); |
| 751 | } |
| 752 | |
| 753 | module_init(it913x_module_init); |
| 754 | module_exit(it913x_module_exit); |
| 755 | |
| 756 | MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>"); |
| 757 | MODULE_DESCRIPTION("it913x USB 2 Driver"); |
Malcolm Priestley | c725ff6 | 2011-11-28 18:22:41 -0300 | [diff] [blame] | 758 | MODULE_VERSION("1.11"); |
Malcolm Priestley | f6d8735 | 2011-07-25 15:35:03 -0300 | [diff] [blame] | 759 | MODULE_LICENSE("GPL"); |