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