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/admin-guide/media/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/driver-api/media/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; 490 491 mutex_lock(&d->i2c_mutex); 492 493 for (i = 0; i < num; i++) { 494 read_o = msg[i].flags & I2C_M_RD; 495 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD; 496 read |= read_o; 497 gate = (msg[i].addr == st->i2c_tuner_addr) 498 ? (read) ? st->i2c_tuner_gate_r 499 : st->i2c_tuner_gate_w 500 : st->i2c_gate; 501 obuf[0] = gate | (read << 7); 502 503 if (gate == 5) 504 obuf[1] = (read) ? 2 : msg[i].len + 1; 505 else 506 obuf[1] = msg[i].len + read + 1; 507 508 obuf[2] = msg[i].addr << 1; 509 510 if (read) { 511 if (read_o) 512 len = 3; 513 else { 514 memcpy(&obuf[3], msg[i].buf, msg[i].len); 515 obuf[msg[i].len+3] = msg[i+1].len; 516 len = msg[i].len+4; 517 } 518 } else { 519 memcpy(&obuf[3], msg[i].buf, msg[i].len); 520 len = msg[i].len+3; 521 } 522 523 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) { 524 deb_info(1, "i2c transfer failed."); 525 mutex_unlock(&d->i2c_mutex); 526 return -EAGAIN; 527 } 528 529 if (read) { 530 if (read_o) 531 memcpy(msg[i].buf, &ibuf[1], msg[i].len); 532 else { 533 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len); 534 i++; 535 } 536 } 537 } 538 539 mutex_unlock(&d->i2c_mutex); 540 return i; 541 } 542 543 static u32 lme2510_i2c_func(struct i2c_adapter *adapter) 544 { 545 return I2C_FUNC_I2C; 546 } 547 548 static struct i2c_algorithm lme2510_i2c_algo = { 549 .master_xfer = lme2510_i2c_xfer, 550 .functionality = lme2510_i2c_func, 551 }; 552 553 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff) 554 { 555 struct dvb_usb_adapter *adap = fe_to_adap(fe); 556 struct dvb_usb_device *d = adap_to_d(adap); 557 struct lme2510_state *st = adap_to_priv(adap); 558 static u8 clear_reg_3[] = LME_ALL_PIDS; 559 static u8 rbuf[1]; 560 int ret = 0, rlen = sizeof(rbuf); 561 562 deb_info(1, "STM (%02x)", onoff); 563 564 /* Streaming is started by FE_HAS_LOCK */ 565 if (onoff == 1) 566 st->stream_on = 1; 567 else { 568 deb_info(1, "STM Steam Off"); 569 /* mutex is here only to avoid collision with I2C */ 570 mutex_lock(&d->i2c_mutex); 571 572 ret = lme2510_usb_talk(d, clear_reg_3, 573 sizeof(clear_reg_3), rbuf, rlen); 574 st->stream_on = 0; 575 st->i2c_talk_onoff = 1; 576 577 mutex_unlock(&d->i2c_mutex); 578 } 579 580 return (ret < 0) ? -ENODEV : 0; 581 } 582 583 static u8 check_sum(u8 *p, u8 len) 584 { 585 u8 sum = 0; 586 while (len--) 587 sum += *p++; 588 return sum; 589 } 590 591 static int lme2510_download_firmware(struct dvb_usb_device *d, 592 const struct firmware *fw) 593 { 594 int ret = 0; 595 u8 *data; 596 u16 j, wlen, len_in, start, end; 597 u8 packet_size, dlen, i; 598 u8 *fw_data; 599 600 packet_size = 0x31; 601 len_in = 1; 602 603 data = kzalloc(128, GFP_KERNEL); 604 if (!data) { 605 info("FRM Could not start Firmware Download"\ 606 "(Buffer allocation failed)"); 607 return -ENOMEM; 608 } 609 610 info("FRM Starting Firmware Download"); 611 612 for (i = 1; i < 3; i++) { 613 start = (i == 1) ? 0 : 512; 614 end = (i == 1) ? 512 : fw->size; 615 for (j = start; j < end; j += (packet_size+1)) { 616 fw_data = (u8 *)(fw->data + j); 617 if ((end - j) > packet_size) { 618 data[0] = i; 619 dlen = packet_size; 620 } else { 621 data[0] = i | 0x80; 622 dlen = (u8)(end - j)-1; 623 } 624 data[1] = dlen; 625 memcpy(&data[2], fw_data, dlen+1); 626 wlen = (u8) dlen + 4; 627 data[wlen-1] = check_sum(fw_data, dlen+1); 628 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3], 629 data[dlen+2], data[dlen+3]); 630 lme2510_usb_talk(d, data, wlen, data, len_in); 631 ret |= (data[0] == 0x88) ? 0 : -1; 632 } 633 } 634 635 data[0] = 0x8a; 636 len_in = 1; 637 msleep(2000); 638 lme2510_usb_talk(d, data, len_in, data, len_in); 639 msleep(400); 640 641 if (ret < 0) 642 info("FRM Firmware Download Failed (%04x)" , ret); 643 else 644 info("FRM Firmware Download Completed - Resetting Device"); 645 646 kfree(data); 647 return RECONNECTS_USB; 648 } 649 650 static void lme_coldreset(struct dvb_usb_device *d) 651 { 652 u8 data[1] = {0}; 653 data[0] = 0x0a; 654 info("FRM Firmware Cold Reset"); 655 656 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data)); 657 658 return; 659 } 660 661 static const char fw_c_s7395[] = LME2510_C_S7395; 662 static const char fw_c_lg[] = LME2510_C_LG; 663 static const char fw_c_s0194[] = LME2510_C_S0194; 664 static const char fw_c_rs2000[] = LME2510_C_RS2000; 665 static const char fw_lg[] = LME2510_LG; 666 static const char fw_s0194[] = LME2510_S0194; 667 668 static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold) 669 { 670 struct lme2510_state *st = d->priv; 671 struct usb_device *udev = d->udev; 672 const struct firmware *fw = NULL; 673 const char *fw_lme; 674 int ret = 0; 675 676 cold = (cold > 0) ? (cold & 1) : 0; 677 678 switch (le16_to_cpu(udev->descriptor.idProduct)) { 679 case 0x1122: 680 switch (st->dvb_usb_lme2510_firmware) { 681 default: 682 case TUNER_S0194: 683 fw_lme = fw_s0194; 684 ret = request_firmware(&fw, fw_lme, &udev->dev); 685 if (ret == 0) { 686 st->dvb_usb_lme2510_firmware = TUNER_S0194; 687 cold = 0; 688 break; 689 } 690 /* fall through */ 691 case TUNER_LG: 692 fw_lme = fw_lg; 693 ret = request_firmware(&fw, fw_lme, &udev->dev); 694 if (ret == 0) { 695 st->dvb_usb_lme2510_firmware = TUNER_LG; 696 break; 697 } 698 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT; 699 break; 700 } 701 break; 702 case 0x1120: 703 switch (st->dvb_usb_lme2510_firmware) { 704 default: 705 case TUNER_S7395: 706 fw_lme = fw_c_s7395; 707 ret = request_firmware(&fw, fw_lme, &udev->dev); 708 if (ret == 0) { 709 st->dvb_usb_lme2510_firmware = TUNER_S7395; 710 cold = 0; 711 break; 712 } 713 /* fall through */ 714 case TUNER_LG: 715 fw_lme = fw_c_lg; 716 ret = request_firmware(&fw, fw_lme, &udev->dev); 717 if (ret == 0) { 718 st->dvb_usb_lme2510_firmware = TUNER_LG; 719 break; 720 } 721 /* fall through */ 722 case TUNER_S0194: 723 fw_lme = fw_c_s0194; 724 ret = request_firmware(&fw, fw_lme, &udev->dev); 725 if (ret == 0) { 726 st->dvb_usb_lme2510_firmware = TUNER_S0194; 727 break; 728 } 729 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT; 730 cold = 0; 731 break; 732 } 733 break; 734 case 0x22f0: 735 fw_lme = fw_c_rs2000; 736 st->dvb_usb_lme2510_firmware = TUNER_RS2000; 737 break; 738 default: 739 fw_lme = fw_c_s7395; 740 } 741 742 release_firmware(fw); 743 744 if (cold) { 745 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware; 746 info("FRM Changing to %s firmware", fw_lme); 747 lme_coldreset(d); 748 return NULL; 749 } 750 751 return fw_lme; 752 } 753 754 static int lme2510_kill_urb(struct usb_data_stream *stream) 755 { 756 int i; 757 758 for (i = 0; i < stream->urbs_submitted; i++) { 759 deb_info(3, "killing URB no. %d.", i); 760 /* stop the URB */ 761 usb_kill_urb(stream->urb_list[i]); 762 } 763 stream->urbs_submitted = 0; 764 765 return 0; 766 } 767 768 static struct tda10086_config tda10086_config = { 769 .demod_address = 0x0e, 770 .invert = 0, 771 .diseqc_tone = 1, 772 .xtal_freq = TDA10086_XTAL_16M, 773 }; 774 775 static struct stv0288_config lme_config = { 776 .demod_address = 0x68, 777 .min_delay_ms = 15, 778 .inittab = s7395_inittab, 779 }; 780 781 static struct ix2505v_config lme_tuner = { 782 .tuner_address = 0x60, 783 .min_delay_ms = 100, 784 .tuner_gain = 0x0, 785 .tuner_chargepump = 0x3, 786 }; 787 788 static struct stv0299_config sharp_z0194_config = { 789 .demod_address = 0x68, 790 .inittab = sharp_z0194a_inittab, 791 .mclk = 88000000UL, 792 .invert = 0, 793 .skip_reinit = 0, 794 .lock_output = STV0299_LOCKOUTPUT_1, 795 .volt13_op0_op1 = STV0299_VOLT13_OP1, 796 .min_delay_ms = 100, 797 .set_symbol_rate = sharp_z0194a_set_symbol_rate, 798 }; 799 800 static struct m88rs2000_config m88rs2000_config = { 801 .demod_addr = 0x68 802 }; 803 804 static struct ts2020_config ts2020_config = { 805 .tuner_address = 0x60, 806 .clk_out_div = 7, 807 .dont_poll = true 808 }; 809 810 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe, 811 enum fe_sec_voltage voltage) 812 { 813 struct dvb_usb_device *d = fe_to_d(fe); 814 struct lme2510_state *st = fe_to_priv(fe); 815 static u8 voltage_low[] = LME_VOLTAGE_L; 816 static u8 voltage_high[] = LME_VOLTAGE_H; 817 static u8 rbuf[1]; 818 int ret = 0, len = 3, rlen = 1; 819 820 mutex_lock(&d->i2c_mutex); 821 822 switch (voltage) { 823 case SEC_VOLTAGE_18: 824 ret |= lme2510_usb_talk(d, 825 voltage_high, len, rbuf, rlen); 826 break; 827 828 case SEC_VOLTAGE_OFF: 829 case SEC_VOLTAGE_13: 830 default: 831 ret |= lme2510_usb_talk(d, 832 voltage_low, len, rbuf, rlen); 833 break; 834 } 835 836 mutex_unlock(&d->i2c_mutex); 837 838 if (st->tuner_config == TUNER_RS2000) 839 if (st->fe_set_voltage) 840 st->fe_set_voltage(fe, voltage); 841 842 843 return (ret < 0) ? -ENODEV : 0; 844 } 845 846 static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status) 847 { 848 struct dvb_usb_device *d = fe_to_d(fe); 849 struct lme2510_state *st = d->priv; 850 int ret = 0; 851 852 if (st->i2c_talk_onoff) { 853 if (st->fe_read_status) { 854 ret = st->fe_read_status(fe, status); 855 if (ret < 0) 856 return ret; 857 } 858 859 st->lock_status = *status; 860 861 if (*status & FE_HAS_LOCK && st->stream_on) { 862 mutex_lock(&d->i2c_mutex); 863 864 st->i2c_talk_onoff = 0; 865 ret = lme2510_stream_restart(d); 866 867 mutex_unlock(&d->i2c_mutex); 868 } 869 870 return ret; 871 } 872 873 /* Timeout of interrupt reached on RS2000 */ 874 if (st->tuner_config == TUNER_RS2000 && 875 time_after(jiffies, st->int_urb_due)) 876 st->lock_status &= ~FE_HAS_LOCK; 877 878 *status = st->lock_status; 879 880 if (!(*status & FE_HAS_LOCK)) { 881 struct dvb_usb_adapter *adap = fe_to_adap(fe); 882 883 st->i2c_talk_onoff = 1; 884 885 lme2510_update_stats(adap); 886 } 887 888 return ret; 889 } 890 891 static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength) 892 { 893 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 894 struct lme2510_state *st = fe_to_priv(fe); 895 896 if (st->fe_read_signal_strength && !st->stream_on) 897 return st->fe_read_signal_strength(fe, strength); 898 899 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE) 900 *strength = (u16)c->strength.stat[0].uvalue; 901 else 902 *strength = 0; 903 904 return 0; 905 } 906 907 static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr) 908 { 909 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 910 struct lme2510_state *st = fe_to_priv(fe); 911 912 if (st->fe_read_snr && !st->stream_on) 913 return st->fe_read_snr(fe, snr); 914 915 if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE) 916 *snr = (u16)c->cnr.stat[0].uvalue; 917 else 918 *snr = 0; 919 920 return 0; 921 } 922 923 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber) 924 { 925 struct lme2510_state *st = fe_to_priv(fe); 926 927 if (st->fe_read_ber && !st->stream_on) 928 return st->fe_read_ber(fe, ber); 929 930 *ber = 0; 931 932 return 0; 933 } 934 935 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 936 { 937 struct lme2510_state *st = fe_to_priv(fe); 938 939 if (st->fe_read_ucblocks && !st->stream_on) 940 return st->fe_read_ucblocks(fe, ucblocks); 941 942 *ucblocks = 0; 943 944 return 0; 945 } 946 947 static int lme_name(struct dvb_usb_adapter *adap) 948 { 949 struct dvb_usb_device *d = adap_to_d(adap); 950 struct lme2510_state *st = adap_to_priv(adap); 951 const char *desc = d->name; 952 static const char * const fe_name[] = { 953 "", " LG TDQY-P001F", " SHARP:BS2F7HZ7395", 954 " SHARP:BS2F7HZ0194", " RS2000"}; 955 char *name = adap->fe[0]->ops.info.name; 956 957 strscpy(name, desc, 128); 958 strlcat(name, fe_name[st->tuner_config], 128); 959 960 return 0; 961 } 962 963 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap) 964 { 965 struct dvb_usb_device *d = adap_to_d(adap); 966 struct lme2510_state *st = d->priv; 967 int ret = 0; 968 969 st->i2c_talk_onoff = 1; 970 switch (le16_to_cpu(d->udev->descriptor.idProduct)) { 971 case 0x1122: 972 case 0x1120: 973 st->i2c_gate = 4; 974 adap->fe[0] = dvb_attach(tda10086_attach, 975 &tda10086_config, &d->i2c_adap); 976 if (adap->fe[0]) { 977 info("TUN Found Frontend TDA10086"); 978 st->i2c_tuner_gate_w = 4; 979 st->i2c_tuner_gate_r = 4; 980 st->i2c_tuner_addr = 0x60; 981 st->tuner_config = TUNER_LG; 982 if (st->dvb_usb_lme2510_firmware != TUNER_LG) { 983 st->dvb_usb_lme2510_firmware = TUNER_LG; 984 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 985 } 986 break; 987 } 988 989 st->i2c_gate = 4; 990 adap->fe[0] = dvb_attach(stv0299_attach, 991 &sharp_z0194_config, &d->i2c_adap); 992 if (adap->fe[0]) { 993 info("FE Found Stv0299"); 994 st->i2c_tuner_gate_w = 4; 995 st->i2c_tuner_gate_r = 5; 996 st->i2c_tuner_addr = 0x60; 997 st->tuner_config = TUNER_S0194; 998 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) { 999 st->dvb_usb_lme2510_firmware = TUNER_S0194; 1000 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 1001 } 1002 break; 1003 } 1004 1005 st->i2c_gate = 5; 1006 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config, 1007 &d->i2c_adap); 1008 1009 if (adap->fe[0]) { 1010 info("FE Found Stv0288"); 1011 st->i2c_tuner_gate_w = 4; 1012 st->i2c_tuner_gate_r = 5; 1013 st->i2c_tuner_addr = 0x60; 1014 st->tuner_config = TUNER_S7395; 1015 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) { 1016 st->dvb_usb_lme2510_firmware = TUNER_S7395; 1017 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV; 1018 } 1019 break; 1020 } 1021 /* fall through */ 1022 case 0x22f0: 1023 st->i2c_gate = 5; 1024 adap->fe[0] = dvb_attach(m88rs2000_attach, 1025 &m88rs2000_config, &d->i2c_adap); 1026 1027 if (adap->fe[0]) { 1028 info("FE Found M88RS2000"); 1029 st->i2c_tuner_gate_w = 5; 1030 st->i2c_tuner_gate_r = 5; 1031 st->i2c_tuner_addr = 0x60; 1032 st->tuner_config = TUNER_RS2000; 1033 st->fe_set_voltage = 1034 adap->fe[0]->ops.set_voltage; 1035 } 1036 break; 1037 } 1038 1039 if (adap->fe[0] == NULL) { 1040 info("DM04/QQBOX Not Powered up or not Supported"); 1041 return -ENODEV; 1042 } 1043 1044 if (ret) { 1045 if (adap->fe[0]) { 1046 dvb_frontend_detach(adap->fe[0]); 1047 adap->fe[0] = NULL; 1048 } 1049 d->rc_map = NULL; 1050 return -ENODEV; 1051 } 1052 1053 st->fe_read_status = adap->fe[0]->ops.read_status; 1054 st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength; 1055 st->fe_read_snr = adap->fe[0]->ops.read_snr; 1056 st->fe_read_ber = adap->fe[0]->ops.read_ber; 1057 st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks; 1058 1059 adap->fe[0]->ops.read_status = dm04_read_status; 1060 adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength; 1061 adap->fe[0]->ops.read_snr = dm04_read_snr; 1062 adap->fe[0]->ops.read_ber = dm04_read_ber; 1063 adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks; 1064 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage; 1065 1066 ret = lme_name(adap); 1067 return ret; 1068 } 1069 1070 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap) 1071 { 1072 struct dvb_usb_device *d = adap_to_d(adap); 1073 struct lme2510_state *st = adap_to_priv(adap); 1074 static const char * const tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"}; 1075 int ret = 0; 1076 1077 switch (st->tuner_config) { 1078 case TUNER_LG: 1079 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60, 1080 &d->i2c_adap, 1)) 1081 ret = st->tuner_config; 1082 break; 1083 case TUNER_S7395: 1084 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner, 1085 &d->i2c_adap)) 1086 ret = st->tuner_config; 1087 break; 1088 case TUNER_S0194: 1089 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60, 1090 &d->i2c_adap, DVB_PLL_OPERA1)) 1091 ret = st->tuner_config; 1092 break; 1093 case TUNER_RS2000: 1094 if (dvb_attach(ts2020_attach, adap->fe[0], 1095 &ts2020_config, &d->i2c_adap)) 1096 ret = st->tuner_config; 1097 break; 1098 default: 1099 break; 1100 } 1101 1102 if (ret) { 1103 info("TUN Found %s tuner", tun_msg[ret]); 1104 } else { 1105 info("TUN No tuner found"); 1106 return -ENODEV; 1107 } 1108 1109 /* Start the Interrupt*/ 1110 ret = lme2510_int_read(adap); 1111 if (ret < 0) { 1112 info("INT Unable to start Interrupt Service"); 1113 return -ENODEV; 1114 } 1115 1116 return ret; 1117 } 1118 1119 static int lme2510_powerup(struct dvb_usb_device *d, int onoff) 1120 { 1121 struct lme2510_state *st = d->priv; 1122 static u8 lnb_on[] = LNB_ON; 1123 static u8 lnb_off[] = LNB_OFF; 1124 static u8 rbuf[1]; 1125 int ret = 0, len = 3, rlen = 1; 1126 1127 mutex_lock(&d->i2c_mutex); 1128 1129 ret = lme2510_usb_talk(d, onoff ? lnb_on : lnb_off, len, rbuf, rlen); 1130 1131 st->i2c_talk_onoff = 1; 1132 1133 mutex_unlock(&d->i2c_mutex); 1134 1135 return ret; 1136 } 1137 1138 static int lme2510_get_adapter_count(struct dvb_usb_device *d) 1139 { 1140 return 1; 1141 } 1142 1143 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name) 1144 { 1145 struct lme2510_state *st = d->priv; 1146 int status; 1147 1148 usb_reset_configuration(d->udev); 1149 1150 usb_set_interface(d->udev, 1151 d->props->bInterfaceNumber, 1); 1152 1153 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware; 1154 1155 status = lme2510_return_status(d); 1156 if (status == 0x44) { 1157 *name = lme_firmware_switch(d, 0); 1158 return COLD; 1159 } 1160 1161 if (status != 0x47) 1162 return -EINVAL; 1163 1164 return WARM; 1165 } 1166 1167 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type, 1168 struct usb_data_stream_properties *stream) 1169 { 1170 struct dvb_usb_adapter *adap = fe_to_adap(fe); 1171 struct dvb_usb_device *d; 1172 1173 if (adap == NULL) 1174 return 0; 1175 1176 d = adap_to_d(adap); 1177 1178 /* Turn PID filter on the fly by module option */ 1179 if (pid_filter == 2) { 1180 adap->pid_filtering = true; 1181 adap->max_feed_count = 15; 1182 } 1183 1184 if (!(le16_to_cpu(d->udev->descriptor.idProduct) 1185 == 0x1122)) 1186 stream->endpoint = 0x8; 1187 1188 return 0; 1189 } 1190 1191 static int lme2510_get_rc_config(struct dvb_usb_device *d, 1192 struct dvb_usb_rc *rc) 1193 { 1194 rc->allowed_protos = RC_PROTO_BIT_NEC32; 1195 return 0; 1196 } 1197 1198 static void lme2510_exit(struct dvb_usb_device *d) 1199 { 1200 struct lme2510_state *st = d->priv; 1201 struct dvb_usb_adapter *adap = &d->adapter[0]; 1202 1203 if (adap != NULL) { 1204 lme2510_kill_urb(&adap->stream); 1205 } 1206 1207 if (st->lme_urb) { 1208 usb_kill_urb(st->lme_urb); 1209 usb_free_urb(st->lme_urb); 1210 info("Interrupt Service Stopped"); 1211 } 1212 } 1213 1214 static struct dvb_usb_device_properties lme2510_props = { 1215 .driver_name = KBUILD_MODNAME, 1216 .owner = THIS_MODULE, 1217 .bInterfaceNumber = 0, 1218 .adapter_nr = adapter_nr, 1219 .size_of_priv = sizeof(struct lme2510_state), 1220 .generic_bulk_ctrl_endpoint = 0x01, 1221 .generic_bulk_ctrl_endpoint_response = 0x01, 1222 1223 .download_firmware = lme2510_download_firmware, 1224 1225 .power_ctrl = lme2510_powerup, 1226 .identify_state = lme2510_identify_state, 1227 .i2c_algo = &lme2510_i2c_algo, 1228 1229 .frontend_attach = dm04_lme2510_frontend_attach, 1230 .tuner_attach = dm04_lme2510_tuner, 1231 .get_stream_config = lme2510_get_stream_config, 1232 .get_adapter_count = lme2510_get_adapter_count, 1233 .streaming_ctrl = lme2510_streaming_ctrl, 1234 1235 .get_rc_config = lme2510_get_rc_config, 1236 1237 .exit = lme2510_exit, 1238 .adapter = { 1239 { 1240 .caps = DVB_USB_ADAP_HAS_PID_FILTER| 1241 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1242 .pid_filter_count = 15, 1243 .pid_filter = lme2510_pid_filter, 1244 .pid_filter_ctrl = lme2510_pid_filter_ctrl, 1245 .stream = 1246 DVB_USB_STREAM_BULK(0x86, 10, 4096), 1247 }, 1248 { 1249 } 1250 }, 1251 }; 1252 1253 static const struct usb_device_id lme2510_id_table[] = { 1254 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props, 1255 "DM04_LME2510_DVB-S", RC_MAP_LME2510) }, 1256 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props, 1257 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) }, 1258 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props, 1259 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) }, 1260 {} /* Terminating entry */ 1261 }; 1262 1263 MODULE_DEVICE_TABLE(usb, lme2510_id_table); 1264 1265 static struct usb_driver lme2510_driver = { 1266 .name = KBUILD_MODNAME, 1267 .probe = dvb_usbv2_probe, 1268 .disconnect = dvb_usbv2_disconnect, 1269 .id_table = lme2510_id_table, 1270 .no_dynamic_id = 1, 1271 .soft_unbind = 1, 1272 }; 1273 1274 module_usb_driver(lme2510_driver); 1275 1276 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>"); 1277 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0"); 1278 MODULE_VERSION("2.07"); 1279 MODULE_LICENSE("GPL"); 1280 MODULE_FIRMWARE(LME2510_C_S7395); 1281 MODULE_FIRMWARE(LME2510_C_LG); 1282 MODULE_FIRMWARE(LME2510_C_S0194); 1283 MODULE_FIRMWARE(LME2510_C_RS2000); 1284 MODULE_FIRMWARE(LME2510_LG); 1285 MODULE_FIRMWARE(LME2510_S0194); 1286 1287