1 /* DVB USB compliant linux driver for GL861 USB2.0 devices. 2 * 3 * This program is free software; you can redistribute it and/or modify it 4 * under the terms of the GNU General Public License as published by the 5 * Free Software Foundation, version 2. 6 * 7 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information 8 */ 9 #include <linux/string.h> 10 11 #include "gl861.h" 12 13 #include "zl10353.h" 14 #include "qt1010.h" 15 #include "tc90522.h" 16 #include "dvb-pll.h" 17 18 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 19 20 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr, 21 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 22 { 23 u16 index; 24 u16 value = addr << (8 + 1); 25 int wo = (rbuf == NULL || rlen == 0); /* write-only */ 26 u8 req, type; 27 u8 *buf; 28 int ret; 29 30 if (wo) { 31 req = GL861_REQ_I2C_WRITE; 32 type = GL861_WRITE; 33 buf = kmemdup(wbuf, wlen, GFP_KERNEL); 34 } else { /* rw */ 35 req = GL861_REQ_I2C_READ; 36 type = GL861_READ; 37 buf = kmalloc(rlen, GFP_KERNEL); 38 } 39 if (!buf) 40 return -ENOMEM; 41 42 switch (wlen) { 43 case 1: 44 index = wbuf[0]; 45 break; 46 case 2: 47 index = wbuf[0]; 48 value = value + wbuf[1]; 49 break; 50 default: 51 dev_err(&d->udev->dev, "%s: wlen=%d, aborting\n", 52 KBUILD_MODNAME, wlen); 53 kfree(buf); 54 return -EINVAL; 55 } 56 57 usleep_range(1000, 2000); /* avoid I2C errors */ 58 59 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type, 60 value, index, buf, rlen, 2000); 61 62 if (!wo && ret > 0) 63 memcpy(rbuf, buf, rlen); 64 65 kfree(buf); 66 return ret; 67 } 68 69 /* I2C */ 70 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 71 int num) 72 { 73 struct dvb_usb_device *d = i2c_get_adapdata(adap); 74 int i; 75 76 if (num > 2) 77 return -EINVAL; 78 79 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 80 return -EAGAIN; 81 82 for (i = 0; i < num; i++) { 83 /* write/read request */ 84 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 85 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf, 86 msg[i].len, msg[i+1].buf, msg[i+1].len) < 0) 87 break; 88 i++; 89 } else 90 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf, 91 msg[i].len, NULL, 0) < 0) 92 break; 93 } 94 95 mutex_unlock(&d->i2c_mutex); 96 return i; 97 } 98 99 static u32 gl861_i2c_func(struct i2c_adapter *adapter) 100 { 101 return I2C_FUNC_I2C; 102 } 103 104 static struct i2c_algorithm gl861_i2c_algo = { 105 .master_xfer = gl861_i2c_xfer, 106 .functionality = gl861_i2c_func, 107 }; 108 109 /* Callbacks for DVB USB */ 110 static struct zl10353_config gl861_zl10353_config = { 111 .demod_address = 0x0f, 112 .no_tuner = 1, 113 .parallel_ts = 1, 114 }; 115 116 static int gl861_frontend_attach(struct dvb_usb_adapter *adap) 117 { 118 119 adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config, 120 &adap_to_d(adap)->i2c_adap); 121 if (adap->fe[0] == NULL) 122 return -EIO; 123 124 return 0; 125 } 126 127 static struct qt1010_config gl861_qt1010_config = { 128 .i2c_address = 0x62 129 }; 130 131 static int gl861_tuner_attach(struct dvb_usb_adapter *adap) 132 { 133 return dvb_attach(qt1010_attach, 134 adap->fe[0], &adap_to_d(adap)->i2c_adap, 135 &gl861_qt1010_config) == NULL ? -ENODEV : 0; 136 } 137 138 static int gl861_init(struct dvb_usb_device *d) 139 { 140 /* 141 * There is 2 interfaces. Interface 0 is for TV and interface 1 is 142 * for HID remote controller. Interface 0 has 2 alternate settings. 143 * For some reason we need to set interface explicitly, defaulted 144 * as alternate setting 1? 145 */ 146 return usb_set_interface(d->udev, 0, 0); 147 } 148 149 /* DVB USB Driver stuff */ 150 static struct dvb_usb_device_properties gl861_props = { 151 .driver_name = KBUILD_MODNAME, 152 .owner = THIS_MODULE, 153 .adapter_nr = adapter_nr, 154 155 .i2c_algo = &gl861_i2c_algo, 156 .frontend_attach = gl861_frontend_attach, 157 .tuner_attach = gl861_tuner_attach, 158 .init = gl861_init, 159 160 .num_adapters = 1, 161 .adapter = { 162 { 163 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512), 164 } 165 } 166 }; 167 168 169 /* 170 * For Friio 171 */ 172 173 struct friio_priv { 174 struct i2c_adapter *demod_sub_i2c; 175 struct i2c_client *i2c_client_demod; 176 struct i2c_client *i2c_client_tuner; 177 struct i2c_adapter tuner_adap; 178 }; 179 180 struct friio_config { 181 struct i2c_board_info demod_info; 182 struct tc90522_config demod_cfg; 183 184 struct i2c_board_info tuner_info; 185 struct dvb_pll_config tuner_cfg; 186 }; 187 188 static const struct friio_config friio_config = { 189 .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), }, 190 .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), }, 191 }; 192 193 /* For another type of I2C: 194 * message sent by a USB control-read/write transaction with data stage. 195 * Used in init/config of Friio. 196 */ 197 static int 198 gl861_i2c_write_ex(struct dvb_usb_device *d, u8 addr, u8 *wbuf, u16 wlen) 199 { 200 u8 *buf; 201 int ret; 202 203 buf = kmemdup(wbuf, wlen, GFP_KERNEL); 204 if (!buf) 205 return -ENOMEM; 206 207 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0), 208 GL861_REQ_I2C_RAW, GL861_WRITE, 209 addr << (8 + 1), 0x0100, buf, wlen, 2000); 210 kfree(buf); 211 return ret; 212 } 213 214 static int 215 gl861_i2c_read_ex(struct dvb_usb_device *d, u8 addr, u8 *rbuf, u16 rlen) 216 { 217 u8 *buf; 218 int ret; 219 220 buf = kmalloc(rlen, GFP_KERNEL); 221 if (!buf) 222 return -ENOMEM; 223 224 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), 225 GL861_REQ_I2C_READ, GL861_READ, 226 addr << (8 + 1), 0x0100, buf, rlen, 2000); 227 if (ret > 0 && rlen > 0) 228 memcpy(buf, rbuf, rlen); 229 kfree(buf); 230 return ret; 231 } 232 233 /* For I2C transactions to the tuner of Friio (dvb_pll). 234 * 235 * Friio uses irregular USB encapsulation for tuner i2c transactions: 236 * write transacions are encapsulated with a different USB 'request' value. 237 * 238 * Although all transactions are sent via the demod(tc90522) 239 * and the demod provides an i2c adapter for them, it cannot be used in Friio 240 * since it assumes using the same parent adapter with the demod, 241 * which does not use the request value and uses same one for both read/write. 242 * So we define a dedicated i2c adapter here. 243 */ 244 245 static int 246 friio_i2c_tuner_read(struct dvb_usb_device *d, struct i2c_msg *msg) 247 { 248 struct friio_priv *priv; 249 u8 addr; 250 251 priv = d_to_priv(d); 252 addr = priv->i2c_client_demod->addr; 253 return gl861_i2c_read_ex(d, addr, msg->buf, msg->len); 254 } 255 256 static int 257 friio_i2c_tuner_write(struct dvb_usb_device *d, struct i2c_msg *msg) 258 { 259 u8 *buf; 260 int ret; 261 struct friio_priv *priv; 262 263 priv = d_to_priv(d); 264 265 if (msg->len < 1) 266 return -EINVAL; 267 268 buf = kmalloc(msg->len + 1, GFP_KERNEL); 269 if (!buf) 270 return -ENOMEM; 271 buf[0] = msg->addr << 1; 272 memcpy(buf + 1, msg->buf, msg->len); 273 274 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0), 275 GL861_REQ_I2C_RAW, GL861_WRITE, 276 priv->i2c_client_demod->addr << (8 + 1), 277 0xFE, buf, msg->len + 1, 2000); 278 kfree(buf); 279 return ret; 280 } 281 282 static int friio_tuner_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 283 int num) 284 { 285 struct dvb_usb_device *d = i2c_get_adapdata(adap); 286 int i; 287 288 if (num > 2) 289 return -EINVAL; 290 291 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 292 return -EAGAIN; 293 294 for (i = 0; i < num; i++) { 295 int ret; 296 297 if (msg[i].flags & I2C_M_RD) 298 ret = friio_i2c_tuner_read(d, &msg[i]); 299 else 300 ret = friio_i2c_tuner_write(d, &msg[i]); 301 302 if (ret < 0) 303 break; 304 305 usleep_range(1000, 2000); /* avoid I2C errors */ 306 } 307 308 mutex_unlock(&d->i2c_mutex); 309 return i; 310 } 311 312 static struct i2c_algorithm friio_tuner_i2c_algo = { 313 .master_xfer = friio_tuner_i2c_xfer, 314 .functionality = gl861_i2c_func, 315 }; 316 317 /* GPIO control in Friio */ 318 319 #define FRIIO_CTL_LNB (1 << 0) 320 #define FRIIO_CTL_STROBE (1 << 1) 321 #define FRIIO_CTL_CLK (1 << 2) 322 #define FRIIO_CTL_LED (1 << 3) 323 324 #define FRIIO_LED_RUNNING 0x6400ff64 325 #define FRIIO_LED_STOPPED 0x96ff00ff 326 327 /* control PIC16F676 attached to Friio */ 328 static int friio_ext_ctl(struct dvb_usb_device *d, 329 u32 sat_color, int power_on) 330 { 331 int i, ret; 332 struct i2c_msg msg; 333 u8 *buf; 334 u32 mask; 335 u8 power = (power_on) ? FRIIO_CTL_LNB : 0; 336 337 buf = kmalloc(2, GFP_KERNEL); 338 if (!buf) 339 return -ENOMEM; 340 341 msg.addr = 0x00; 342 msg.flags = 0; 343 msg.len = 2; 344 msg.buf = buf; 345 buf[0] = 0x00; 346 347 /* send 2bit header (&B10) */ 348 buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE; 349 ret = i2c_transfer(&d->i2c_adap, &msg, 1); 350 buf[1] |= FRIIO_CTL_CLK; 351 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 352 353 buf[1] = power | FRIIO_CTL_STROBE; 354 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 355 buf[1] |= FRIIO_CTL_CLK; 356 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 357 358 /* send 32bit(satur, R, G, B) data in serial */ 359 mask = 1 << 31; 360 for (i = 0; i < 32; i++) { 361 buf[1] = power | FRIIO_CTL_STROBE; 362 if (sat_color & mask) 363 buf[1] |= FRIIO_CTL_LED; 364 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 365 buf[1] |= FRIIO_CTL_CLK; 366 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 367 mask >>= 1; 368 } 369 370 /* set the strobe off */ 371 buf[1] = power; 372 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 373 buf[1] |= FRIIO_CTL_CLK; 374 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 375 376 kfree(buf); 377 return (ret == 70) ? 0 : -EREMOTEIO; 378 } 379 380 /* init/config of gl861 for Friio */ 381 /* NOTE: 382 * This function cannot be moved to friio_init()/dvb_usbv2_init(), 383 * because the init defined here must be done before any activities like I2C, 384 * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(), 385 * where I2C communication is used. 386 * Thus this function is set to be called from _power_ctl(). 387 * 388 * Since it will be called on the early init stage 389 * where the i2c adapter is not initialized yet, 390 * we cannot use i2c_transfer() here. 391 */ 392 static int friio_reset(struct dvb_usb_device *d) 393 { 394 int i, ret; 395 u8 wbuf[2], rbuf[2]; 396 397 static const u8 friio_init_cmds[][2] = { 398 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff}, 399 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00}, 400 {0x39, 0x00}, {0x36, 0x00}, 401 }; 402 403 ret = usb_set_interface(d->udev, 0, 0); 404 if (ret < 0) 405 return ret; 406 407 wbuf[0] = 0x11; 408 wbuf[1] = 0x02; 409 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0); 410 if (ret < 0) 411 return ret; 412 usleep_range(2000, 3000); 413 414 wbuf[0] = 0x11; 415 wbuf[1] = 0x00; 416 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0); 417 if (ret < 0) 418 return ret; 419 420 /* 421 * Check if the dev is really a Friio White, since it might be 422 * another device, Friio Black, with the same VID/PID. 423 */ 424 425 usleep_range(1000, 2000); 426 wbuf[0] = 0x03; 427 wbuf[1] = 0x80; 428 ret = gl861_i2c_write_ex(d, 0x09, wbuf, 2); 429 if (ret < 0) 430 return ret; 431 432 usleep_range(2000, 3000); 433 ret = gl861_i2c_read_ex(d, 0x09, rbuf, 2); 434 if (ret < 0) 435 return ret; 436 if (rbuf[0] != 0xff || rbuf[1] != 0xff) 437 return -ENODEV; 438 439 440 usleep_range(1000, 2000); 441 ret = gl861_i2c_write_ex(d, 0x48, wbuf, 2); 442 if (ret < 0) 443 return ret; 444 445 usleep_range(2000, 3000); 446 ret = gl861_i2c_read_ex(d, 0x48, rbuf, 2); 447 if (ret < 0) 448 return ret; 449 if (rbuf[0] != 0xff || rbuf[1] != 0xff) 450 return -ENODEV; 451 452 wbuf[0] = 0x30; 453 wbuf[1] = 0x04; 454 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0); 455 if (ret < 0) 456 return ret; 457 458 wbuf[0] = 0x00; 459 wbuf[1] = 0x01; 460 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0); 461 if (ret < 0) 462 return ret; 463 464 wbuf[0] = 0x06; 465 wbuf[1] = 0x0f; 466 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0); 467 if (ret < 0) 468 return ret; 469 470 for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) { 471 ret = gl861_i2c_msg(d, 0x00, (u8 *)friio_init_cmds[i], 2, 472 NULL, 0); 473 if (ret < 0) 474 return ret; 475 } 476 return 0; 477 } 478 479 /* 480 * DVB callbacks for Friio 481 */ 482 483 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff) 484 { 485 return onoff ? friio_reset(d) : 0; 486 } 487 488 static int friio_frontend_attach(struct dvb_usb_adapter *adap) 489 { 490 const struct i2c_board_info *info; 491 struct dvb_usb_device *d; 492 struct tc90522_config cfg; 493 struct i2c_client *cl; 494 struct friio_priv *priv; 495 496 info = &friio_config.demod_info; 497 d = adap_to_d(adap); 498 cl = dvb_module_probe("tc90522", info->type, 499 &d->i2c_adap, info->addr, &cfg); 500 if (!cl) 501 return -ENODEV; 502 adap->fe[0] = cfg.fe; 503 504 /* ignore cfg.tuner_i2c and create new one */ 505 priv = adap_to_priv(adap); 506 priv->i2c_client_demod = cl; 507 priv->tuner_adap.algo = &friio_tuner_i2c_algo; 508 priv->tuner_adap.dev.parent = &d->udev->dev; 509 strscpy(priv->tuner_adap.name, d->name, sizeof(priv->tuner_adap.name)); 510 strlcat(priv->tuner_adap.name, "-tuner", sizeof(priv->tuner_adap.name)); 511 priv->demod_sub_i2c = &priv->tuner_adap; 512 i2c_set_adapdata(&priv->tuner_adap, d); 513 514 return i2c_add_adapter(&priv->tuner_adap); 515 } 516 517 static int friio_frontend_detach(struct dvb_usb_adapter *adap) 518 { 519 struct friio_priv *priv; 520 521 priv = adap_to_priv(adap); 522 i2c_del_adapter(&priv->tuner_adap); 523 dvb_module_release(priv->i2c_client_demod); 524 return 0; 525 } 526 527 static int friio_tuner_attach(struct dvb_usb_adapter *adap) 528 { 529 const struct i2c_board_info *info; 530 struct dvb_pll_config cfg; 531 struct i2c_client *cl; 532 struct friio_priv *priv; 533 534 priv = adap_to_priv(adap); 535 info = &friio_config.tuner_info; 536 cfg = friio_config.tuner_cfg; 537 cfg.fe = adap->fe[0]; 538 539 cl = dvb_module_probe("dvb_pll", info->type, 540 priv->demod_sub_i2c, info->addr, &cfg); 541 if (!cl) 542 return -ENODEV; 543 priv->i2c_client_tuner = cl; 544 return 0; 545 } 546 547 static int friio_tuner_detach(struct dvb_usb_adapter *adap) 548 { 549 struct friio_priv *priv; 550 551 priv = adap_to_priv(adap); 552 dvb_module_release(priv->i2c_client_tuner); 553 return 0; 554 } 555 556 static int friio_init(struct dvb_usb_device *d) 557 { 558 int i; 559 int ret; 560 struct friio_priv *priv; 561 562 static const u8 demod_init[][2] = { 563 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40}, 564 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f}, 565 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20}, 566 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28}, 567 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78}, 568 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68}, 569 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00}, 570 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f}, 571 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c}, 572 }; 573 574 /* power on LNA? */ 575 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true); 576 if (ret < 0) 577 return ret; 578 msleep(20); 579 580 /* init/config demod */ 581 priv = d_to_priv(d); 582 for (i = 0; i < ARRAY_SIZE(demod_init); i++) { 583 int ret; 584 585 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2); 586 if (ret < 0) 587 return ret; 588 } 589 msleep(100); 590 return 0; 591 } 592 593 static void friio_exit(struct dvb_usb_device *d) 594 { 595 friio_ext_ctl(d, FRIIO_LED_STOPPED, false); 596 } 597 598 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff) 599 { 600 u32 led_color; 601 602 led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED; 603 return friio_ext_ctl(fe_to_d(fe), led_color, true); 604 } 605 606 607 static struct dvb_usb_device_properties friio_props = { 608 .driver_name = KBUILD_MODNAME, 609 .owner = THIS_MODULE, 610 .adapter_nr = adapter_nr, 611 612 .size_of_priv = sizeof(struct friio_priv), 613 614 .i2c_algo = &gl861_i2c_algo, 615 .power_ctrl = friio_power_ctrl, 616 .frontend_attach = friio_frontend_attach, 617 .frontend_detach = friio_frontend_detach, 618 .tuner_attach = friio_tuner_attach, 619 .tuner_detach = friio_tuner_detach, 620 .init = friio_init, 621 .exit = friio_exit, 622 .streaming_ctrl = friio_streaming_ctrl, 623 624 .num_adapters = 1, 625 .adapter = { 626 { 627 .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384), 628 } 629 } 630 }; 631 632 static const struct usb_device_id gl861_id_table[] = { 633 { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801, 634 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) }, 635 { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU, 636 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) }, 637 { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE, 638 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) }, 639 { } 640 }; 641 MODULE_DEVICE_TABLE(usb, gl861_id_table); 642 643 static struct usb_driver gl861_usb_driver = { 644 .name = KBUILD_MODNAME, 645 .id_table = gl861_id_table, 646 .probe = dvb_usbv2_probe, 647 .disconnect = dvb_usbv2_disconnect, 648 .suspend = dvb_usbv2_suspend, 649 .resume = dvb_usbv2_resume, 650 .reset_resume = dvb_usbv2_reset_resume, 651 .no_dynamic_id = 1, 652 .soft_unbind = 1, 653 }; 654 655 module_usb_driver(gl861_usb_driver); 656 657 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>"); 658 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861"); 659 MODULE_VERSION("0.1"); 660 MODULE_LICENSE("GPL"); 661