1 /* DVB USB compliant linux driver for 2 * 3 * DM04/QQBOX DVB-S USB BOX LME2510C + SHARP:BS2F7HZ7395 4 * LME2510C + LG TDQY-P001F 5 * LME2510C + BS2F7HZ0194 6 * LME2510 + LG TDQY-P001F 7 * LME2510 + BS2F7HZ0194 8 * 9 * MVB7395 (LME2510C+SHARP:BS2F7HZ7395) 10 * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V) 11 * 12 * MV001F (LME2510+LGTDQY-P001F) 13 * LG TDQY - P001F =(TDA8263 + TDA10086H) 14 * 15 * MVB0001F (LME2510C+LGTDQT-P001F) 16 * 17 * MV0194 (LME2510+SHARP:BS2F7HZ0194) 18 * SHARP:BS2F7HZ0194 = (STV0299+IX2410) 19 * 20 * MVB0194 (LME2510C+SHARP0194) 21 * 22 * LME2510C + M88RS2000 23 * 24 * For firmware see Documentation/media/dvb-drivers/lmedm04.rst 25 * 26 * I2C addresses: 27 * 0xd0 - STV0288 - Demodulator 28 * 0xc0 - Sharp IX2505V - Tuner 29 * -- 30 * 0x1c - TDA10086 - Demodulator 31 * 0xc0 - TDA8263 - Tuner 32 * -- 33 * 0xd0 - STV0299 - Demodulator 34 * 0xc0 - IX2410 - Tuner 35 * 36 * 37 * VID = 3344 PID LME2510=1122 LME2510C=1120 38 * 39 * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com) 40 * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd. 41 * 42 * This program is free software; you can redistribute it and/or modify 43 * it under the terms of the GNU General Public License Version 2, as 44 * published by the Free Software Foundation. 45 * 46 * This program is distributed in the hope that it will be useful, 47 * but WITHOUT ANY WARRANTY; without even the implied warranty of 48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 49 * GNU General Public License for more details. 50 * 51 * 52 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information 53 * 54 * Known Issues : 55 * LME2510: Non Intel USB chipsets fail to maintain High Speed on 56 * Boot or Hot Plug. 57 * 58 * QQbox suffers from noise on LNB voltage. 59 * 60 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system 61 * with other tuners. After a cold reset streaming will not start. 62 * 63 * M88RS2000 suffers from loss of lock. 64 */ 65 #define DVB_USB_LOG_PREFIX "LME2510(C)" 66 #include <linux/usb.h> 67 #include <linux/usb/input.h> 68 #include <media/rc-core.h> 69 70 #include "dvb_usb.h" 71 #include "lmedm04.h" 72 #include "tda826x.h" 73 #include "tda10086.h" 74 #include "stv0288.h" 75 #include "ix2505v.h" 76 #include "stv0299.h" 77 #include "dvb-pll.h" 78 #include "z0194a.h" 79 #include "m88rs2000.h" 80 #include "ts2020.h" 81 82 83 #define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw"; 84 #define LME2510_C_LG "dvb-usb-lme2510c-lg.fw"; 85 #define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw"; 86 #define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw"; 87 #define LME2510_LG "dvb-usb-lme2510-lg.fw"; 88 #define LME2510_S0194 "dvb-usb-lme2510-s0194.fw"; 89 90 /* debug */ 91 static int dvb_usb_lme2510_debug; 92 #define lme_debug(var, level, args...) do { \ 93 if ((var >= level)) \ 94 pr_debug(DVB_USB_LOG_PREFIX": " args); \ 95 } while (0) 96 #define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args) 97 #define debug_data_snipet(level, name, p) \ 98 deb_info(level, name" (%8phN)", p); 99 #define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args) 100 101 module_param_named(debug, dvb_usb_lme2510_debug, int, 0644); 102 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."); 103 104 static int dvb_usb_lme2510_firmware; 105 module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644); 106 MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG"); 107 108 static int pid_filter; 109 module_param_named(pid, pid_filter, int, 0644); 110 MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on"); 111 112 113 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 114 115 #define TUNER_DEFAULT 0x0 116 #define TUNER_LG 0x1 117 #define TUNER_S7395 0x2 118 #define TUNER_S0194 0x3 119 #define TUNER_RS2000 0x4 120 121 struct lme2510_state { 122 unsigned long int_urb_due; 123 enum fe_status lock_status; 124 u8 id; 125 u8 tuner_config; 126 u8 signal_level; 127 u8 signal_sn; 128 u8 time_key; 129 u8 i2c_talk_onoff; 130 u8 i2c_gate; 131 u8 i2c_tuner_gate_w; 132 u8 i2c_tuner_gate_r; 133 u8 i2c_tuner_addr; 134 u8 stream_on; 135 u8 pid_size; 136 u8 pid_off; 137 u8 int_buffer[128]; 138 struct urb *lme_urb; 139 u8 usb_buffer[64]; 140 /* Frontend original calls */ 141 int (*fe_read_status)(struct dvb_frontend *, enum fe_status *); 142 int (*fe_read_signal_strength)(struct dvb_frontend *, u16 *); 143 int (*fe_read_snr)(struct dvb_frontend *, u16 *); 144 int (*fe_read_ber)(struct dvb_frontend *, u32 *); 145 int (*fe_read_ucblocks)(struct dvb_frontend *, u32 *); 146 int (*fe_set_voltage)(struct dvb_frontend *, enum fe_sec_voltage); 147 u8 dvb_usb_lme2510_firmware; 148 }; 149 150 static int lme2510_usb_talk(struct dvb_usb_device *d, 151 u8 *wbuf, int wlen, u8 *rbuf, int rlen) 152 { 153 struct lme2510_state *st = d->priv; 154 int ret = 0; 155 156 if (max(wlen, rlen) > sizeof(st->usb_buffer)) 157 return -EINVAL; 158 159 ret = mutex_lock_interruptible(&d->usb_mutex); 160 if (ret < 0) 161 return -EAGAIN; 162 163 memcpy(st->usb_buffer, wbuf, wlen); 164 165 ret = dvb_usbv2_generic_rw_locked(d, st->usb_buffer, wlen, 166 st->usb_buffer, rlen); 167 168 if (rlen) 169 memcpy(rbuf, st->usb_buffer, rlen); 170 171 mutex_unlock(&d->usb_mutex); 172 173 return ret; 174 } 175 176 static int lme2510_stream_restart(struct dvb_usb_device *d) 177 { 178 struct lme2510_state *st = d->priv; 179 u8 all_pids[] = LME_ALL_PIDS; 180 u8 stream_on[] = LME_ST_ON_W; 181 u8 rbuff[1]; 182 if (st->pid_off) 183 lme2510_usb_talk(d, all_pids, sizeof(all_pids), 184 rbuff, sizeof(rbuff)); 185 /*Restart Stream Command*/ 186 return lme2510_usb_talk(d, stream_on, sizeof(stream_on), 187 rbuff, sizeof(rbuff)); 188 } 189 190 static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out) 191 { 192 struct lme2510_state *st = d->priv; 193 static u8 pid_buff[] = LME_ZERO_PID; 194 static u8 rbuf[1]; 195 u8 pid_no = index * 2; 196 u8 pid_len = pid_no + 2; 197 int ret = 0; 198 deb_info(1, "PID Setting Pid %04x", pid_out); 199 200 if (st->pid_size == 0) 201 ret |= lme2510_stream_restart(d); 202 203 pid_buff[2] = pid_no; 204 pid_buff[3] = (u8)pid_out & 0xff; 205 pid_buff[4] = pid_no + 1; 206 pid_buff[5] = (u8)(pid_out >> 8); 207 208 if (pid_len > st->pid_size) 209 st->pid_size = pid_len; 210 pid_buff[7] = 0x80 + st->pid_size; 211 212 ret |= lme2510_usb_talk(d, pid_buff , 213 sizeof(pid_buff) , rbuf, sizeof(rbuf)); 214 215 if (st->stream_on) 216 ret |= lme2510_stream_restart(d); 217 218 return ret; 219 } 220 221 /* Convert range from 0x00-0xff to 0x0000-0xffff */ 222 #define reg_to_16bits(x) ((x) | ((x) << 8)) 223 224 static void lme2510_update_stats(struct dvb_usb_adapter *adap) 225 { 226 struct lme2510_state *st = adap_to_priv(adap); 227 struct dvb_frontend *fe = adap->fe[0]; 228 struct dtv_frontend_properties *c; 229 u32 s_tmp = 0, c_tmp = 0; 230 231 if (!fe) 232 return; 233 234 c = &fe->dtv_property_cache; 235 236 c->block_count.len = 1; 237 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 238 c->block_error.len = 1; 239 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 240 c->post_bit_count.len = 1; 241 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 242 c->post_bit_error.len = 1; 243 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 244 245 if (st->i2c_talk_onoff) { 246 c->strength.len = 1; 247 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 248 c->cnr.len = 1; 249 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 250 return; 251 } 252 253 switch (st->tuner_config) { 254 case TUNER_LG: 255 s_tmp = reg_to_16bits(0xff - st->signal_level); 256 c_tmp = reg_to_16bits(0xff - st->signal_sn); 257 break; 258 case TUNER_S7395: 259 case TUNER_S0194: 260 s_tmp = 0xffff - (((st->signal_level * 2) << 8) * 5 / 4); 261 c_tmp = reg_to_16bits((0xff - st->signal_sn - 0xa1) * 3); 262 break; 263 case TUNER_RS2000: 264 s_tmp = reg_to_16bits(st->signal_level); 265 c_tmp = reg_to_16bits(st->signal_sn); 266 } 267 268 c->strength.len = 1; 269 c->strength.stat[0].scale = FE_SCALE_RELATIVE; 270 c->strength.stat[0].uvalue = (u64)s_tmp; 271 272 c->cnr.len = 1; 273 c->cnr.stat[0].scale = FE_SCALE_RELATIVE; 274 c->cnr.stat[0].uvalue = (u64)c_tmp; 275 } 276 277 static void lme2510_int_response(struct urb *lme_urb) 278 { 279 struct dvb_usb_adapter *adap = lme_urb->context; 280 struct lme2510_state *st = adap_to_priv(adap); 281 u8 *ibuf, *rbuf; 282 int i = 0, offset; 283 u32 key; 284 u8 signal_lock = 0; 285 286 switch (lme_urb->status) { 287 case 0: 288 case -ETIMEDOUT: 289 break; 290 case -ECONNRESET: 291 case -ENOENT: 292 case -ESHUTDOWN: 293 return; 294 default: 295 info("Error %x", lme_urb->status); 296 break; 297 } 298 299 rbuf = (u8 *) lme_urb->transfer_buffer; 300 301 offset = ((lme_urb->actual_length/8) > 4) 302 ? 4 : (lme_urb->actual_length/8) ; 303 304 for (i = 0; i < offset; ++i) { 305 ibuf = (u8 *)&rbuf[i*8]; 306 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x", 307 offset, i, ibuf[0], ibuf[1]); 308 309 switch (ibuf[0]) { 310 case 0xaa: 311 debug_data_snipet(1, "INT Remote data snippet", ibuf); 312 if (!adap_to_d(adap)->rc_dev) 313 break; 314 315 key = RC_SCANCODE_NEC32(ibuf[2] << 24 | 316 ibuf[3] << 16 | 317 ibuf[4] << 8 | 318 ibuf[5]); 319 320 deb_info(1, "INT Key = 0x%08x", key); 321 rc_keydown(adap_to_d(adap)->rc_dev, RC_PROTO_NEC32, key, 322 0); 323 break; 324 case 0xbb: 325 switch (st->tuner_config) { 326 case TUNER_LG: 327 signal_lock = ibuf[2] & BIT(5); 328 st->signal_level = ibuf[4]; 329 st->signal_sn = ibuf[3]; 330 st->time_key = ibuf[7]; 331 break; 332 case TUNER_S7395: 333 case TUNER_S0194: 334 /* Tweak for earlier firmware*/ 335 if (ibuf[1] == 0x03) { 336 signal_lock = ibuf[2] & BIT(4); 337 st->signal_level = ibuf[3]; 338 st->signal_sn = ibuf[4]; 339 } else { 340 st->signal_level = ibuf[4]; 341 st->signal_sn = ibuf[5]; 342 } 343 break; 344 case TUNER_RS2000: 345 signal_lock = ibuf[2] & 0xee; 346 st->signal_level = ibuf[5]; 347 st->signal_sn = ibuf[4]; 348 st->time_key = ibuf[7]; 349 default: 350 break; 351 } 352 353 /* Interrupt will also throw just BIT 0 as lock */ 354 signal_lock |= ibuf[2] & BIT(0); 355 356 if (!signal_lock) 357 st->lock_status &= ~FE_HAS_LOCK; 358 359 lme2510_update_stats(adap); 360 361 debug_data_snipet(5, "INT Remote data snippet in", ibuf); 362 break; 363 case 0xcc: 364 debug_data_snipet(1, "INT Control data snippet", ibuf); 365 break; 366 default: 367 debug_data_snipet(1, "INT Unknown data snippet", ibuf); 368 break; 369 } 370 } 371 372 usb_submit_urb(lme_urb, GFP_ATOMIC); 373 374 /* Interrupt urb is due every 48 msecs while streaming the buffer 375 * stores up to 4 periods if missed. Allow 200 msec for next interrupt. 376 */ 377 st->int_urb_due = jiffies + msecs_to_jiffies(200); 378 } 379 380 static int lme2510_int_read(struct dvb_usb_adapter *adap) 381 { 382 struct dvb_usb_device *d = adap_to_d(adap); 383 struct lme2510_state *lme_int = adap_to_priv(adap); 384 struct usb_host_endpoint *ep; 385 386 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC); 387 388 if (lme_int->lme_urb == NULL) 389 return -ENOMEM; 390 391 usb_fill_int_urb(lme_int->lme_urb, 392 d->udev, 393 usb_rcvintpipe(d->udev, 0xa), 394 lme_int->int_buffer, 395 sizeof(lme_int->int_buffer), 396 lme2510_int_response, 397 adap, 398 8); 399 400 /* Quirk of pipe reporting PIPE_BULK but behaves as interrupt */ 401 ep = usb_pipe_endpoint(d->udev, lme_int->lme_urb->pipe); 402 403 if (usb_endpoint_type(&ep->desc) == USB_ENDPOINT_XFER_BULK) 404 lme_int->lme_urb->pipe = usb_rcvbulkpipe(d->udev, 0xa), 405 406 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC); 407 info("INT Interrupt Service Started"); 408 409 return 0; 410 } 411 412 static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) 413 { 414 struct dvb_usb_device *d = adap_to_d(adap); 415 struct lme2510_state *st = adap_to_priv(adap); 416 static u8 clear_pid_reg[] = LME_ALL_PIDS; 417 static u8 rbuf[1]; 418 int ret = 0; 419 420 deb_info(1, "PID Clearing Filter"); 421 422 mutex_lock(&d->i2c_mutex); 423 424 if (!onoff) { 425 ret |= lme2510_usb_talk(d, clear_pid_reg, 426 sizeof(clear_pid_reg), rbuf, sizeof(rbuf)); 427 st->pid_off = true; 428 } else 429 st->pid_off = false; 430 431 st->pid_size = 0; 432 433 mutex_unlock(&d->i2c_mutex); 434 435 return 0; 436 } 437 438 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, 439 int onoff) 440 { 441 struct dvb_usb_device *d = adap_to_d(adap); 442 int ret = 0; 443 444 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__, 445 pid, index, onoff); 446 447 if (onoff) { 448 mutex_lock(&d->i2c_mutex); 449 ret |= lme2510_enable_pid(d, index, pid); 450 mutex_unlock(&d->i2c_mutex); 451 } 452 453 454 return ret; 455 } 456 457 458 static int lme2510_return_status(struct dvb_usb_device *d) 459 { 460 int ret; 461 u8 *data; 462 463 data = kzalloc(6, GFP_KERNEL); 464 if (!data) 465 return -ENOMEM; 466 467 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), 468 0x06, 0x80, 0x0302, 0x00, 469 data, 0x6, 200); 470 if (ret != 6) 471 ret = -EINVAL; 472 else 473 ret = data[2]; 474 475 info("Firmware Status: %6ph", data); 476 477 kfree(data); 478 return ret; 479 } 480 481 static int lme2510_msg(struct dvb_usb_device *d, 482 u8 *wbuf, int wlen, u8 *rbuf, int rlen) 483 { 484 struct lme2510_state *st = d->priv; 485 486 st->i2c_talk_onoff = 1; 487 488 return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen); 489 } 490 491 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 492 int num) 493 { 494 struct dvb_usb_device *d = i2c_get_adapdata(adap); 495 struct lme2510_state *st = d->priv; 496 static u8 obuf[64], ibuf[64]; 497 int i, read, read_o; 498 u16 len; 499 u8 gate = st->i2c_gate; 500 501 mutex_lock(&d->i2c_mutex); 502 503 if (gate == 0) 504 gate = 5; 505 506 for (i = 0; i < num; i++) { 507 read_o = msg[i].flags & I2C_M_RD; 508 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD; 509 read |= read_o; 510 gate = (msg[i].addr == st->i2c_tuner_addr) 511 ? (read) ? st->i2c_tuner_gate_r 512 : st->i2c_tuner_gate_w 513 : st->i2c_gate; 514 obuf[0] = gate | (read << 7); 515 516 if (gate == 5) 517 obuf[1] = (read) ? 2 : msg[i].len + 1; 518 else 519 obuf[1] = msg[i].len + read + 1; 520 521 obuf[2] = msg[i].addr << 1; 522 523 if (read) { 524 if (read_o) 525 len = 3; 526 else { 527 memcpy(&obuf[3], msg[i].buf, msg[i].len); 528 obuf[msg[i].len+3] = msg[i+1].len; 529 len = msg[i].len+4; 530 } 531 } else { 532 memcpy(&obuf[3], msg[i].buf, msg[i].len); 533 len = msg[i].len+3; 534 } 535 536 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) { 537 deb_info(1, "i2c transfer failed."); 538 mutex_unlock(&d->i2c_mutex); 539 return -EAGAIN; 540 } 541 542 if (read) { 543 if (read_o) 544 memcpy(msg[i].buf, &ibuf[1], msg[i].len); 545 else { 546 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len); 547 i++; 548 } 549 } 550 } 551 552 mutex_unlock(&d->i2c_mutex); 553 return i; 554 } 555 556 static u32 lme2510_i2c_func(struct i2c_adapter *adapter) 557 { 558 return I2C_FUNC_I2C; 559 } 560 561 static struct i2c_algorithm lme2510_i2c_algo = { 562 .master_xfer = lme2510_i2c_xfer, 563 .functionality = lme2510_i2c_func, 564 }; 565 566 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff) 567 { 568 struct dvb_usb_adapter *adap = fe_to_adap(fe); 569 struct dvb_usb_device *d = adap_to_d(adap); 570 struct lme2510_state *st = adap_to_priv(adap); 571 static u8 clear_reg_3[] = LME_ALL_PIDS; 572 static u8 rbuf[1]; 573 int ret = 0, rlen = sizeof(rbuf); 574 575 deb_info(1, "STM (%02x)", onoff); 576 577 /* Streaming is started by FE_HAS_LOCK */ 578 if (onoff == 1) 579 st->stream_on = 1; 580 else { 581 deb_info(1, "STM Steam Off"); 582 /* mutex is here only to avoid collision with I2C */ 583 mutex_lock(&d->i2c_mutex); 584 585 ret = lme2510_usb_talk(d, clear_reg_3, 586 sizeof(clear_reg_3), rbuf, rlen); 587 st->stream_on = 0; 588 st->i2c_talk_onoff = 1; 589 590 mutex_unlock(&d->i2c_mutex); 591 } 592 593 return (ret < 0) ? -ENODEV : 0; 594 } 595 596 static u8 check_sum(u8 *p, u8 len) 597 { 598 u8 sum = 0; 599 while (len--) 600 sum += *p++; 601 return sum; 602 } 603 604 static int lme2510_download_firmware(struct dvb_usb_device *d, 605 const struct firmware *fw) 606 { 607 int ret = 0; 608 u8 *data; 609 u16 j, wlen, len_in, start, end; 610 u8 packet_size, dlen, i; 611 u8 *fw_data; 612 613 packet_size = 0x31; 614 len_in = 1; 615 616 data = kzalloc(128, GFP_KERNEL); 617 if (!data) { 618 info("FRM Could not start Firmware Download"\ 619 "(Buffer allocation failed)"); 620 return -ENOMEM; 621 } 622 623 info("FRM Starting Firmware Download"); 624 625 for (i = 1; i < 3; i++) { 626 start = (i == 1) ? 0 : 512; 627 end = (i == 1) ? 512 : fw->size; 628 for (j = start; j < end; j += (packet_size+1)) { 629 fw_data = (u8 *)(fw->data + j); 630 if ((end - j) > packet_size) { 631 data[0] = i; 632 dlen = packet_size; 633 } else { 634 data[0] = i | 0x80; 635 dlen = (u8)(end - j)-1; 636 } 637 data[1] = dlen; 638 memcpy(&data[2], fw_data, dlen+1); 639 wlen = (u8) dlen + 4; 640 data[wlen-1] = check_sum(fw_data, dlen+1); 641 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3], 642 data[dlen+2], data[dlen+3]); 643 lme2510_usb_talk(d, data, wlen, data, len_in); 644 ret |= (data[0] == 0x88) ? 0 : -1; 645 } 646 } 647 648 data[0] = 0x8a; 649 len_in = 1; 650 msleep(2000); 651 lme2510_usb_talk(d, data, len_in, data, len_in); 652 msleep(400); 653 654 if (ret < 0) 655 info("FRM Firmware Download Failed (%04x)" , ret); 656 else 657 info("FRM Firmware Download Completed - Resetting Device"); 658 659 kfree(data); 660 return RECONNECTS_USB; 661 } 662 663 static void lme_coldreset(struct dvb_usb_device *d) 664 { 665 u8 data[1] = {0}; 666 data[0] = 0x0a; 667 info("FRM Firmware Cold Reset"); 668 669 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data)); 670 671 return; 672 } 673 674 static const char fw_c_s7395[] = LME2510_C_S7395; 675 static const char fw_c_lg[] = LME2510_C_LG; 676 static const char fw_c_s0194[] = LME2510_C_S0194; 677 static const char fw_c_rs2000[] = LME2510_C_RS2000; 678 static const char fw_lg[] = LME2510_LG; 679 static const char fw_s0194[] = LME2510_S0194; 680 681 static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold) 682 { 683 struct lme2510_state *st = d->priv; 684 struct usb_device *udev = d->udev; 685 const struct firmware *fw = NULL; 686 const char *fw_lme; 687 int ret = 0; 688 689 cold = (cold > 0) ? (cold & 1) : 0; 690 691 switch (le16_to_cpu(udev->descriptor.idProduct)) { 692 case 0x1122: 693 switch (st->dvb_usb_lme2510_firmware) { 694 default: 695 case TUNER_S0194: 696 fw_lme = fw_s0194; 697 ret = request_firmware(&fw, fw_lme, &udev->dev); 698 if (ret == 0) { 699 st->dvb_usb_lme2510_firmware = TUNER_S0194; 700 cold = 0; 701 break; 702 } 703 /* fall through */ 704 case TUNER_LG: 705 fw_lme = fw_lg; 706 ret = request_firmware(&fw, fw_lme, &udev->dev); 707 if (ret == 0) { 708 st->dvb_usb_lme2510_firmware = TUNER_LG; 709 break; 710 } 711 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT; 712 break; 713 } 714 break; 715 case 0x1120: 716 switch (st->dvb_usb_lme2510_firmware) { 717 default: 718 case TUNER_S7395: 719 fw_lme = fw_c_s7395; 720 ret = request_firmware(&fw, fw_lme, &udev->dev); 721 if (ret == 0) { 722 st->dvb_usb_lme2510_firmware = TUNER_S7395; 723 cold = 0; 724 break; 725 } 726 /* fall through */ 727 case TUNER_LG: 728 fw_lme = fw_c_lg; 729 ret = request_firmware(&fw, fw_lme, &udev->dev); 730 if (ret == 0) { 731 st->dvb_usb_lme2510_firmware = TUNER_LG; 732 break; 733 } 734 /* fall through */ 735 case TUNER_S0194: 736 fw_lme = fw_c_s0194; 737 ret = request_firmware(&fw, fw_lme, &udev->dev); 738 if (ret == 0) { 739 st->dvb_usb_lme2510_firmware = TUNER_S0194; 740 break; 741 } 742 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT; 743 cold = 0; 744 break; 745 } 746 break; 747 case 0x22f0: 748 fw_lme = fw_c_rs2000; 749 st->dvb_usb_lme2510_firmware = TUNER_RS2000; 750 break; 751 default: 752 fw_lme = fw_c_s7395; 753 } 754 755 release_firmware(fw); 756 757 if (cold) { 758 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware; 759 info("FRM Changing to %s firmware", fw_lme); 760 lme_coldreset(d); 761 return NULL; 762 } 763 764 return fw_lme; 765 } 766 767 static int lme2510_kill_urb(struct usb_data_stream *stream) 768 { 769 int i; 770 771 for (i = 0; i < stream->urbs_submitted; i++) { 772 deb_info(3, "killing URB no. %d.", i); 773 /* stop the URB */ 774 usb_kill_urb(stream->urb_list[i]); 775 } 776 stream->urbs_submitted = 0; 777 778 return 0; 779 } 780 781 static struct tda10086_config tda10086_config = { 782 .demod_address = 0x0e, 783 .invert = 0, 784 .diseqc_tone = 1, 785 .xtal_freq = TDA10086_XTAL_16M, 786 }; 787 788 static struct stv0288_config lme_config = { 789 .demod_address = 0x68, 790 .min_delay_ms = 15, 791 .inittab = s7395_inittab, 792 }; 793 794 static struct ix2505v_config lme_tuner = { 795 .tuner_address = 0x60, 796 .min_delay_ms = 100, 797 .tuner_gain = 0x0, 798 .tuner_chargepump = 0x3, 799 }; 800 801 static struct stv0299_config sharp_z0194_config = { 802 .demod_address = 0x68, 803 .inittab = sharp_z0194a_inittab, 804 .mclk = 88000000UL, 805 .invert = 0, 806 .skip_reinit = 0, 807 .lock_output = STV0299_LOCKOUTPUT_1, 808 .volt13_op0_op1 = STV0299_VOLT13_OP1, 809 .min_delay_ms = 100, 810 .set_symbol_rate = sharp_z0194a_set_symbol_rate, 811 }; 812 813 static struct m88rs2000_config m88rs2000_config = { 814 .demod_addr = 0x68 815 }; 816 817 static struct ts2020_config ts2020_config = { 818 .tuner_address = 0x60, 819 .clk_out_div = 7, 820 .dont_poll = true 821 }; 822 823 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe, 824 enum fe_sec_voltage voltage) 825 { 826 struct dvb_usb_device *d = fe_to_d(fe); 827 struct lme2510_state *st = fe_to_priv(fe); 828 static u8 voltage_low[] = LME_VOLTAGE_L; 829 static u8 voltage_high[] = LME_VOLTAGE_H; 830 static u8 rbuf[1]; 831 int ret = 0, len = 3, rlen = 1; 832 833 mutex_lock(&d->i2c_mutex); 834 835 switch (voltage) { 836 case SEC_VOLTAGE_18: 837 ret |= lme2510_usb_talk(d, 838 voltage_high, len, rbuf, rlen); 839 break; 840 841 case SEC_VOLTAGE_OFF: 842 case SEC_VOLTAGE_13: 843 default: 844 ret |= lme2510_usb_talk(d, 845 voltage_low, len, rbuf, rlen); 846 break; 847 } 848 849 mutex_unlock(&d->i2c_mutex); 850 851 if (st->tuner_config == TUNER_RS2000) 852 if (st->fe_set_voltage) 853 st->fe_set_voltage(fe, voltage); 854 855 856 return (ret < 0) ? -ENODEV : 0; 857 } 858 859 static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status) 860 { 861 struct dvb_usb_device *d = fe_to_d(fe); 862 struct lme2510_state *st = d->priv; 863 int ret = 0; 864 865 if (st->i2c_talk_onoff) { 866 if (st->fe_read_status) { 867 ret = st->fe_read_status(fe, status); 868 if (ret < 0) 869 return ret; 870 } 871 872 st->lock_status = *status; 873 874 if (*status & FE_HAS_LOCK && st->stream_on) { 875 mutex_lock(&d->i2c_mutex); 876 877 st->i2c_talk_onoff = 0; 878 ret = lme2510_stream_restart(d); 879 880 mutex_unlock(&d->i2c_mutex); 881 } 882 883 return ret; 884 } 885 886 /* Timeout of interrupt reached on RS2000 */ 887 if (st->tuner_config == TUNER_RS2000 && 888 time_after(jiffies, st->int_urb_due)) 889 st->lock_status &= ~FE_HAS_LOCK; 890 891 *status = st->lock_status; 892 893 if (!(*status & FE_HAS_LOCK)) { 894 struct dvb_usb_adapter *adap = fe_to_adap(fe); 895 896 st->i2c_talk_onoff = 1; 897 898 lme2510_update_stats(adap); 899 } 900 901 return ret; 902 } 903 904 static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength) 905 { 906 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 907 struct lme2510_state *st = fe_to_priv(fe); 908 909 if (st->fe_read_signal_strength && !st->stream_on) 910 return st->fe_read_signal_strength(fe, strength); 911 912 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE) 913 *strength = (u16)c->strength.stat[0].uvalue; 914 else 915 *strength = 0; 916 917 return 0; 918 } 919 920 static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr) 921 { 922 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 923 struct lme2510_state *st = fe_to_priv(fe); 924 925 if (st->fe_read_snr && !st->stream_on) 926 return st->fe_read_snr(fe, snr); 927 928 if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE) 929 *snr = (u16)c->cnr.stat[0].uvalue; 930 else 931 *snr = 0; 932 933 return 0; 934 } 935 936 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber) 937 { 938 struct lme2510_state *st = fe_to_priv(fe); 939 940 if (st->fe_read_ber && !st->stream_on) 941 return st->fe_read_ber(fe, ber); 942 943 *ber = 0; 944 945 return 0; 946 } 947 948 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 949 { 950 struct lme2510_state *st = fe_to_priv(fe); 951 952 if (st->fe_read_ucblocks && !st->stream_on) 953 return st->fe_read_ucblocks(fe, ucblocks); 954 955 *ucblocks = 0; 956 957 return 0; 958 } 959 960 static int lme_name(struct dvb_usb_adapter *adap) 961 { 962 struct dvb_usb_device *d = adap_to_d(adap); 963 struct lme2510_state *st = adap_to_priv(adap); 964 const char *desc = d->name; 965 static const char * const fe_name[] = { 966 "", " LG TDQY-P001F", " SHARP:BS2F7HZ7395", 967 " SHARP:BS2F7HZ0194", " RS2000"}; 968 char *name = adap->fe[0]->ops.info.name; 969 970 strscpy(name, desc, 128); 971 strlcat(name, fe_name[st->tuner_config], 128); 972 973 return 0; 974 } 975 976 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap) 977 { 978 struct dvb_usb_device *d = adap_to_d(adap); 979 struct lme2510_state *st = d->priv; 980 int ret = 0; 981 982 st->i2c_talk_onoff = 1; 983 switch (le16_to_cpu(d->udev->descriptor.idProduct)) { 984 case 0x1122: 985 case 0x1120: 986 st->i2c_gate = 4; 987 adap->fe[0] = dvb_attach(tda10086_attach, 988 &tda10086_config, &d->i2c_adap); 989 if (adap->fe[0]) { 990 info("TUN Found Frontend TDA10086"); 991 st->i2c_tuner_gate_w = 4; 992 st->i2c_tuner_gate_r = 4; 993 st->i2c_tuner_addr = 0x60; 994 st->tuner_config = TUNER_LG; 995 if (st->dvb_usb_lme2510_firmware != TUNER_LG) { 996 st->dvb_usb_lme2510_firmware = TUNER_LG; 997 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 998 } 999 break; 1000 } 1001 1002 st->i2c_gate = 4; 1003 adap->fe[0] = dvb_attach(stv0299_attach, 1004 &sharp_z0194_config, &d->i2c_adap); 1005 if (adap->fe[0]) { 1006 info("FE Found Stv0299"); 1007 st->i2c_tuner_gate_w = 4; 1008 st->i2c_tuner_gate_r = 5; 1009 st->i2c_tuner_addr = 0x60; 1010 st->tuner_config = TUNER_S0194; 1011 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) { 1012 st->dvb_usb_lme2510_firmware = TUNER_S0194; 1013 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 1014 } 1015 break; 1016 } 1017 1018 st->i2c_gate = 5; 1019 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config, 1020 &d->i2c_adap); 1021 1022 if (adap->fe[0]) { 1023 info("FE Found Stv0288"); 1024 st->i2c_tuner_gate_w = 4; 1025 st->i2c_tuner_gate_r = 5; 1026 st->i2c_tuner_addr = 0x60; 1027 st->tuner_config = TUNER_S7395; 1028 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) { 1029 st->dvb_usb_lme2510_firmware = TUNER_S7395; 1030 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 1031 } 1032 break; 1033 } 1034 /* fall through */ 1035 case 0x22f0: 1036 st->i2c_gate = 5; 1037 adap->fe[0] = dvb_attach(m88rs2000_attach, 1038 &m88rs2000_config, &d->i2c_adap); 1039 1040 if (adap->fe[0]) { 1041 info("FE Found M88RS2000"); 1042 st->i2c_tuner_gate_w = 5; 1043 st->i2c_tuner_gate_r = 5; 1044 st->i2c_tuner_addr = 0x60; 1045 st->tuner_config = TUNER_RS2000; 1046 st->fe_set_voltage = 1047 adap->fe[0]->ops.set_voltage; 1048 } 1049 break; 1050 } 1051 1052 if (adap->fe[0] == NULL) { 1053 info("DM04/QQBOX Not Powered up or not Supported"); 1054 return -ENODEV; 1055 } 1056 1057 if (ret) { 1058 if (adap->fe[0]) { 1059 dvb_frontend_detach(adap->fe[0]); 1060 adap->fe[0] = NULL; 1061 } 1062 d->rc_map = NULL; 1063 return -ENODEV; 1064 } 1065 1066 st->fe_read_status = adap->fe[0]->ops.read_status; 1067 st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength; 1068 st->fe_read_snr = adap->fe[0]->ops.read_snr; 1069 st->fe_read_ber = adap->fe[0]->ops.read_ber; 1070 st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks; 1071 1072 adap->fe[0]->ops.read_status = dm04_read_status; 1073 adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength; 1074 adap->fe[0]->ops.read_snr = dm04_read_snr; 1075 adap->fe[0]->ops.read_ber = dm04_read_ber; 1076 adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks; 1077 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage; 1078 1079 ret = lme_name(adap); 1080 return ret; 1081 } 1082 1083 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap) 1084 { 1085 struct dvb_usb_device *d = adap_to_d(adap); 1086 struct lme2510_state *st = adap_to_priv(adap); 1087 static const char * const tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"}; 1088 int ret = 0; 1089 1090 switch (st->tuner_config) { 1091 case TUNER_LG: 1092 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60, 1093 &d->i2c_adap, 1)) 1094 ret = st->tuner_config; 1095 break; 1096 case TUNER_S7395: 1097 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner, 1098 &d->i2c_adap)) 1099 ret = st->tuner_config; 1100 break; 1101 case TUNER_S0194: 1102 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60, 1103 &d->i2c_adap, DVB_PLL_OPERA1)) 1104 ret = st->tuner_config; 1105 break; 1106 case TUNER_RS2000: 1107 if (dvb_attach(ts2020_attach, adap->fe[0], 1108 &ts2020_config, &d->i2c_adap)) 1109 ret = st->tuner_config; 1110 break; 1111 default: 1112 break; 1113 } 1114 1115 if (ret) { 1116 info("TUN Found %s tuner", tun_msg[ret]); 1117 } else { 1118 info("TUN No tuner found"); 1119 return -ENODEV; 1120 } 1121 1122 /* Start the Interrupt*/ 1123 ret = lme2510_int_read(adap); 1124 if (ret < 0) { 1125 info("INT Unable to start Interrupt Service"); 1126 return -ENODEV; 1127 } 1128 1129 return ret; 1130 } 1131 1132 static int lme2510_powerup(struct dvb_usb_device *d, int onoff) 1133 { 1134 struct lme2510_state *st = d->priv; 1135 static u8 lnb_on[] = LNB_ON; 1136 static u8 lnb_off[] = LNB_OFF; 1137 static u8 rbuf[1]; 1138 int ret = 0, len = 3, rlen = 1; 1139 1140 mutex_lock(&d->i2c_mutex); 1141 1142 ret = lme2510_usb_talk(d, onoff ? lnb_on : lnb_off, len, rbuf, rlen); 1143 1144 st->i2c_talk_onoff = 1; 1145 1146 mutex_unlock(&d->i2c_mutex); 1147 1148 return ret; 1149 } 1150 1151 static int lme2510_get_adapter_count(struct dvb_usb_device *d) 1152 { 1153 return 1; 1154 } 1155 1156 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name) 1157 { 1158 struct lme2510_state *st = d->priv; 1159 int status; 1160 1161 usb_reset_configuration(d->udev); 1162 1163 usb_set_interface(d->udev, 1164 d->props->bInterfaceNumber, 1); 1165 1166 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware; 1167 1168 status = lme2510_return_status(d); 1169 if (status == 0x44) { 1170 *name = lme_firmware_switch(d, 0); 1171 return COLD; 1172 } 1173 1174 if (status != 0x47) 1175 return -EINVAL; 1176 1177 return WARM; 1178 } 1179 1180 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type, 1181 struct usb_data_stream_properties *stream) 1182 { 1183 struct dvb_usb_adapter *adap = fe_to_adap(fe); 1184 struct dvb_usb_device *d; 1185 1186 if (adap == NULL) 1187 return 0; 1188 1189 d = adap_to_d(adap); 1190 1191 /* Turn PID filter on the fly by module option */ 1192 if (pid_filter == 2) { 1193 adap->pid_filtering = true; 1194 adap->max_feed_count = 15; 1195 } 1196 1197 if (!(le16_to_cpu(d->udev->descriptor.idProduct) 1198 == 0x1122)) 1199 stream->endpoint = 0x8; 1200 1201 return 0; 1202 } 1203 1204 static int lme2510_get_rc_config(struct dvb_usb_device *d, 1205 struct dvb_usb_rc *rc) 1206 { 1207 rc->allowed_protos = RC_PROTO_BIT_NEC32; 1208 return 0; 1209 } 1210 1211 static void lme2510_exit(struct dvb_usb_device *d) 1212 { 1213 struct lme2510_state *st = d->priv; 1214 struct dvb_usb_adapter *adap = &d->adapter[0]; 1215 1216 if (adap != NULL) { 1217 lme2510_kill_urb(&adap->stream); 1218 } 1219 1220 if (st->lme_urb) { 1221 usb_kill_urb(st->lme_urb); 1222 usb_free_urb(st->lme_urb); 1223 info("Interrupt Service Stopped"); 1224 } 1225 } 1226 1227 static struct dvb_usb_device_properties lme2510_props = { 1228 .driver_name = KBUILD_MODNAME, 1229 .owner = THIS_MODULE, 1230 .bInterfaceNumber = 0, 1231 .adapter_nr = adapter_nr, 1232 .size_of_priv = sizeof(struct lme2510_state), 1233 .generic_bulk_ctrl_endpoint = 0x01, 1234 .generic_bulk_ctrl_endpoint_response = 0x01, 1235 1236 .download_firmware = lme2510_download_firmware, 1237 1238 .power_ctrl = lme2510_powerup, 1239 .identify_state = lme2510_identify_state, 1240 .i2c_algo = &lme2510_i2c_algo, 1241 1242 .frontend_attach = dm04_lme2510_frontend_attach, 1243 .tuner_attach = dm04_lme2510_tuner, 1244 .get_stream_config = lme2510_get_stream_config, 1245 .get_adapter_count = lme2510_get_adapter_count, 1246 .streaming_ctrl = lme2510_streaming_ctrl, 1247 1248 .get_rc_config = lme2510_get_rc_config, 1249 1250 .exit = lme2510_exit, 1251 .adapter = { 1252 { 1253 .caps = DVB_USB_ADAP_HAS_PID_FILTER| 1254 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1255 .pid_filter_count = 15, 1256 .pid_filter = lme2510_pid_filter, 1257 .pid_filter_ctrl = lme2510_pid_filter_ctrl, 1258 .stream = 1259 DVB_USB_STREAM_BULK(0x86, 10, 4096), 1260 }, 1261 { 1262 } 1263 }, 1264 }; 1265 1266 static const struct usb_device_id lme2510_id_table[] = { 1267 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props, 1268 "DM04_LME2510_DVB-S", RC_MAP_LME2510) }, 1269 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props, 1270 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) }, 1271 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props, 1272 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) }, 1273 {} /* Terminating entry */ 1274 }; 1275 1276 MODULE_DEVICE_TABLE(usb, lme2510_id_table); 1277 1278 static struct usb_driver lme2510_driver = { 1279 .name = KBUILD_MODNAME, 1280 .probe = dvb_usbv2_probe, 1281 .disconnect = dvb_usbv2_disconnect, 1282 .id_table = lme2510_id_table, 1283 .no_dynamic_id = 1, 1284 .soft_unbind = 1, 1285 }; 1286 1287 module_usb_driver(lme2510_driver); 1288 1289 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>"); 1290 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0"); 1291 MODULE_VERSION("2.07"); 1292 MODULE_LICENSE("GPL"); 1293 MODULE_FIRMWARE(LME2510_C_S7395); 1294 MODULE_FIRMWARE(LME2510_C_LG); 1295 MODULE_FIRMWARE(LME2510_C_S0194); 1296 MODULE_FIRMWARE(LME2510_C_RS2000); 1297 MODULE_FIRMWARE(LME2510_LG); 1298 MODULE_FIRMWARE(LME2510_S0194); 1299 1300