1 /* 2 * Driver for the Auvitek USB bridge 3 * 4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * 15 * GNU General Public License for more details. 16 */ 17 18 #include "au0828.h" 19 20 #include <linux/module.h> 21 #include <linux/slab.h> 22 #include <linux/init.h> 23 #include <linux/device.h> 24 #include <media/v4l2-common.h> 25 #include <media/tuner.h> 26 27 #include "au8522.h" 28 #include "xc5000.h" 29 #include "mxl5007t.h" 30 #include "tda18271.h" 31 32 static int preallocate_big_buffers; 33 module_param_named(preallocate_big_buffers, preallocate_big_buffers, int, 0644); 34 MODULE_PARM_DESC(preallocate_big_buffers, "Preallocate the larger transfer buffers at module load time"); 35 36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 37 38 #define _AU0828_BULKPIPE 0x83 39 #define _BULKPIPESIZE 0xe522 40 41 static u8 hauppauge_hvr950q_led_states[] = { 42 0x00, /* off */ 43 0x02, /* yellow */ 44 0x04, /* green */ 45 }; 46 47 static struct au8522_led_config hauppauge_hvr950q_led_cfg = { 48 .gpio_output = 0x00e0, 49 .gpio_output_enable = 0x6006, 50 .gpio_output_disable = 0x0660, 51 52 .gpio_leds = 0x00e2, 53 .led_states = hauppauge_hvr950q_led_states, 54 .num_led_states = sizeof(hauppauge_hvr950q_led_states), 55 56 .vsb8_strong = 20 /* dB */ * 10, 57 .qam64_strong = 25 /* dB */ * 10, 58 .qam256_strong = 32 /* dB */ * 10, 59 }; 60 61 static struct au8522_config hauppauge_hvr950q_config = { 62 .demod_address = 0x8e >> 1, 63 .status_mode = AU8522_DEMODLOCKING, 64 .qam_if = AU8522_IF_6MHZ, 65 .vsb_if = AU8522_IF_6MHZ, 66 .led_cfg = &hauppauge_hvr950q_led_cfg, 67 }; 68 69 static struct au8522_config fusionhdtv7usb_config = { 70 .demod_address = 0x8e >> 1, 71 .status_mode = AU8522_DEMODLOCKING, 72 .qam_if = AU8522_IF_6MHZ, 73 .vsb_if = AU8522_IF_6MHZ, 74 }; 75 76 static struct au8522_config hauppauge_woodbury_config = { 77 .demod_address = 0x8e >> 1, 78 .status_mode = AU8522_DEMODLOCKING, 79 .qam_if = AU8522_IF_4MHZ, 80 .vsb_if = AU8522_IF_3_25MHZ, 81 }; 82 83 static struct xc5000_config hauppauge_xc5000a_config = { 84 .i2c_address = 0x61, 85 .if_khz = 6000, 86 .chip_id = XC5000A, 87 .output_amp = 0x8f, 88 }; 89 90 static struct xc5000_config hauppauge_xc5000c_config = { 91 .i2c_address = 0x61, 92 .if_khz = 6000, 93 .chip_id = XC5000C, 94 .output_amp = 0x8f, 95 }; 96 97 static struct mxl5007t_config mxl5007t_hvr950q_config = { 98 .xtal_freq_hz = MxL_XTAL_24_MHZ, 99 .if_freq_hz = MxL_IF_6_MHZ, 100 }; 101 102 static struct tda18271_config hauppauge_woodbury_tunerconfig = { 103 .gate = TDA18271_GATE_DIGITAL, 104 }; 105 106 static void au0828_restart_dvb_streaming(struct work_struct *work); 107 108 /*-------------------------------------------------------------------*/ 109 static void urb_completion(struct urb *purb) 110 { 111 struct au0828_dev *dev = purb->context; 112 int ptype = usb_pipetype(purb->pipe); 113 unsigned char *ptr; 114 115 dprintk(2, "%s: %d\n", __func__, purb->actual_length); 116 117 if (!dev) { 118 dprintk(2, "%s: no dev!\n", __func__); 119 return; 120 } 121 122 if (!dev->urb_streaming) { 123 dprintk(2, "%s: not streaming!\n", __func__); 124 return; 125 } 126 127 if (ptype != PIPE_BULK) { 128 pr_err("%s: Unsupported URB type %d\n", 129 __func__, ptype); 130 return; 131 } 132 133 /* See if the stream is corrupted (to work around a hardware 134 bug where the stream gets misaligned */ 135 ptr = purb->transfer_buffer; 136 if (purb->actual_length > 0 && ptr[0] != 0x47) { 137 dprintk(1, "Need to restart streaming %02x len=%d!\n", 138 ptr[0], purb->actual_length); 139 schedule_work(&dev->restart_streaming); 140 return; 141 } 142 143 /* Feed the transport payload into the kernel demux */ 144 dvb_dmx_swfilter_packets(&dev->dvb.demux, 145 purb->transfer_buffer, purb->actual_length / 188); 146 147 /* Clean the buffer before we requeue */ 148 memset(purb->transfer_buffer, 0, URB_BUFSIZE); 149 150 /* Requeue URB */ 151 usb_submit_urb(purb, GFP_ATOMIC); 152 } 153 154 static int stop_urb_transfer(struct au0828_dev *dev) 155 { 156 int i; 157 158 dprintk(2, "%s()\n", __func__); 159 160 if (!dev->urb_streaming) 161 return 0; 162 163 dev->urb_streaming = false; 164 for (i = 0; i < URB_COUNT; i++) { 165 if (dev->urbs[i]) { 166 usb_kill_urb(dev->urbs[i]); 167 if (!preallocate_big_buffers) 168 kfree(dev->urbs[i]->transfer_buffer); 169 170 usb_free_urb(dev->urbs[i]); 171 } 172 } 173 174 return 0; 175 } 176 177 static int start_urb_transfer(struct au0828_dev *dev) 178 { 179 struct urb *purb; 180 int i, ret; 181 182 dprintk(2, "%s()\n", __func__); 183 184 if (dev->urb_streaming) { 185 dprintk(2, "%s: bulk xfer already running!\n", __func__); 186 return 0; 187 } 188 189 for (i = 0; i < URB_COUNT; i++) { 190 191 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); 192 if (!dev->urbs[i]) 193 return -ENOMEM; 194 195 purb = dev->urbs[i]; 196 197 if (preallocate_big_buffers) 198 purb->transfer_buffer = dev->dig_transfer_buffer[i]; 199 else 200 purb->transfer_buffer = kzalloc(URB_BUFSIZE, 201 GFP_KERNEL); 202 203 if (!purb->transfer_buffer) { 204 usb_free_urb(purb); 205 dev->urbs[i] = NULL; 206 ret = -ENOMEM; 207 pr_err("%s: failed big buffer allocation, err = %d\n", 208 __func__, ret); 209 return ret; 210 } 211 212 purb->status = -EINPROGRESS; 213 usb_fill_bulk_urb(purb, 214 dev->usbdev, 215 usb_rcvbulkpipe(dev->usbdev, 216 _AU0828_BULKPIPE), 217 purb->transfer_buffer, 218 URB_BUFSIZE, 219 urb_completion, 220 dev); 221 222 } 223 224 for (i = 0; i < URB_COUNT; i++) { 225 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC); 226 if (ret != 0) { 227 stop_urb_transfer(dev); 228 pr_err("%s: failed urb submission, err = %d\n", 229 __func__, ret); 230 return ret; 231 } 232 } 233 234 dev->urb_streaming = true; 235 return 0; 236 } 237 238 static void au0828_start_transport(struct au0828_dev *dev) 239 { 240 au0828_write(dev, 0x608, 0x90); 241 au0828_write(dev, 0x609, 0x72); 242 au0828_write(dev, 0x60a, 0x71); 243 au0828_write(dev, 0x60b, 0x01); 244 245 } 246 247 static void au0828_stop_transport(struct au0828_dev *dev, int full_stop) 248 { 249 if (full_stop) { 250 au0828_write(dev, 0x608, 0x00); 251 au0828_write(dev, 0x609, 0x00); 252 au0828_write(dev, 0x60a, 0x00); 253 } 254 au0828_write(dev, 0x60b, 0x00); 255 } 256 257 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed) 258 { 259 struct dvb_demux *demux = feed->demux; 260 struct au0828_dev *dev = (struct au0828_dev *) demux->priv; 261 struct au0828_dvb *dvb = &dev->dvb; 262 int ret = 0; 263 264 dprintk(1, "%s()\n", __func__); 265 266 if (!demux->dmx.frontend) 267 return -EINVAL; 268 269 if (dvb->frontend) { 270 mutex_lock(&dvb->lock); 271 dvb->start_count++; 272 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__, 273 dvb->start_count, dvb->stop_count); 274 if (dvb->feeding++ == 0) { 275 /* Start transport */ 276 au0828_start_transport(dev); 277 ret = start_urb_transfer(dev); 278 if (ret < 0) { 279 au0828_stop_transport(dev, 0); 280 dvb->feeding--; /* We ran out of memory... */ 281 } 282 } 283 mutex_unlock(&dvb->lock); 284 } 285 286 return ret; 287 } 288 289 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed) 290 { 291 struct dvb_demux *demux = feed->demux; 292 struct au0828_dev *dev = (struct au0828_dev *) demux->priv; 293 struct au0828_dvb *dvb = &dev->dvb; 294 int ret = 0; 295 296 dprintk(1, "%s()\n", __func__); 297 298 if (dvb->frontend) { 299 cancel_work_sync(&dev->restart_streaming); 300 301 mutex_lock(&dvb->lock); 302 dvb->stop_count++; 303 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__, 304 dvb->start_count, dvb->stop_count); 305 if (dvb->feeding > 0) { 306 dvb->feeding--; 307 if (dvb->feeding == 0) { 308 /* Stop transport */ 309 ret = stop_urb_transfer(dev); 310 au0828_stop_transport(dev, 0); 311 } 312 } 313 mutex_unlock(&dvb->lock); 314 } 315 316 return ret; 317 } 318 319 static void au0828_restart_dvb_streaming(struct work_struct *work) 320 { 321 struct au0828_dev *dev = container_of(work, struct au0828_dev, 322 restart_streaming); 323 struct au0828_dvb *dvb = &dev->dvb; 324 325 if (!dev->urb_streaming) 326 return; 327 328 dprintk(1, "Restarting streaming...!\n"); 329 330 mutex_lock(&dvb->lock); 331 332 /* Stop transport */ 333 stop_urb_transfer(dev); 334 au0828_stop_transport(dev, 1); 335 336 /* Start transport */ 337 au0828_start_transport(dev); 338 start_urb_transfer(dev); 339 340 mutex_unlock(&dvb->lock); 341 } 342 343 static int au0828_set_frontend(struct dvb_frontend *fe) 344 { 345 struct au0828_dev *dev = fe->dvb->priv; 346 struct au0828_dvb *dvb = &dev->dvb; 347 int ret, was_streaming; 348 349 mutex_lock(&dvb->lock); 350 was_streaming = dev->urb_streaming; 351 if (was_streaming) { 352 au0828_stop_transport(dev, 1); 353 354 /* 355 * We can't hold a mutex here, as the restart_streaming 356 * kthread may also hold it. 357 */ 358 mutex_unlock(&dvb->lock); 359 cancel_work_sync(&dev->restart_streaming); 360 mutex_lock(&dvb->lock); 361 362 stop_urb_transfer(dev); 363 } 364 mutex_unlock(&dvb->lock); 365 366 ret = dvb->set_frontend(fe); 367 368 if (was_streaming) { 369 mutex_lock(&dvb->lock); 370 au0828_start_transport(dev); 371 start_urb_transfer(dev); 372 mutex_unlock(&dvb->lock); 373 } 374 375 return ret; 376 } 377 378 static int dvb_register(struct au0828_dev *dev) 379 { 380 struct au0828_dvb *dvb = &dev->dvb; 381 int result; 382 383 dprintk(1, "%s()\n", __func__); 384 385 if (preallocate_big_buffers) { 386 int i; 387 for (i = 0; i < URB_COUNT; i++) { 388 dev->dig_transfer_buffer[i] = kzalloc(URB_BUFSIZE, 389 GFP_KERNEL); 390 391 if (!dev->dig_transfer_buffer[i]) { 392 result = -ENOMEM; 393 394 pr_err("failed buffer allocation (errno = %d)\n", 395 result); 396 goto fail_adapter; 397 } 398 } 399 } 400 401 INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming); 402 403 /* register adapter */ 404 result = dvb_register_adapter(&dvb->adapter, 405 KBUILD_MODNAME, THIS_MODULE, 406 &dev->usbdev->dev, adapter_nr); 407 if (result < 0) { 408 pr_err("dvb_register_adapter failed (errno = %d)\n", 409 result); 410 goto fail_adapter; 411 } 412 413 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 414 dvb->adapter.mdev = dev->media_dev; 415 #endif 416 417 dvb->adapter.priv = dev; 418 419 /* register frontend */ 420 result = dvb_register_frontend(&dvb->adapter, dvb->frontend); 421 if (result < 0) { 422 pr_err("dvb_register_frontend failed (errno = %d)\n", 423 result); 424 goto fail_frontend; 425 } 426 427 /* Hook dvb frontend */ 428 dvb->set_frontend = dvb->frontend->ops.set_frontend; 429 dvb->frontend->ops.set_frontend = au0828_set_frontend; 430 431 /* register demux stuff */ 432 dvb->demux.dmx.capabilities = 433 DMX_TS_FILTERING | DMX_SECTION_FILTERING | 434 DMX_MEMORY_BASED_FILTERING; 435 dvb->demux.priv = dev; 436 dvb->demux.filternum = 256; 437 dvb->demux.feednum = 256; 438 dvb->demux.start_feed = au0828_dvb_start_feed; 439 dvb->demux.stop_feed = au0828_dvb_stop_feed; 440 result = dvb_dmx_init(&dvb->demux); 441 if (result < 0) { 442 pr_err("dvb_dmx_init failed (errno = %d)\n", result); 443 goto fail_dmx; 444 } 445 446 dvb->dmxdev.filternum = 256; 447 dvb->dmxdev.demux = &dvb->demux.dmx; 448 dvb->dmxdev.capabilities = 0; 449 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter); 450 if (result < 0) { 451 pr_err("dvb_dmxdev_init failed (errno = %d)\n", result); 452 goto fail_dmxdev; 453 } 454 455 dvb->fe_hw.source = DMX_FRONTEND_0; 456 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw); 457 if (result < 0) { 458 pr_err("add_frontend failed (DMX_FRONTEND_0, errno = %d)\n", 459 result); 460 goto fail_fe_hw; 461 } 462 463 dvb->fe_mem.source = DMX_MEMORY_FE; 464 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem); 465 if (result < 0) { 466 pr_err("add_frontend failed (DMX_MEMORY_FE, errno = %d)\n", 467 result); 468 goto fail_fe_mem; 469 } 470 471 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw); 472 if (result < 0) { 473 pr_err("connect_frontend failed (errno = %d)\n", result); 474 goto fail_fe_conn; 475 } 476 477 /* register network adapter */ 478 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx); 479 480 dvb->start_count = 0; 481 dvb->stop_count = 0; 482 483 result = dvb_create_media_graph(&dvb->adapter, false); 484 if (result < 0) 485 goto fail_create_graph; 486 487 return 0; 488 489 fail_create_graph: 490 dvb_net_release(&dvb->net); 491 fail_fe_conn: 492 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); 493 fail_fe_mem: 494 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); 495 fail_fe_hw: 496 dvb_dmxdev_release(&dvb->dmxdev); 497 fail_dmxdev: 498 dvb_dmx_release(&dvb->demux); 499 fail_dmx: 500 dvb_unregister_frontend(dvb->frontend); 501 fail_frontend: 502 dvb_frontend_detach(dvb->frontend); 503 dvb_unregister_adapter(&dvb->adapter); 504 fail_adapter: 505 506 if (preallocate_big_buffers) { 507 int i; 508 for (i = 0; i < URB_COUNT; i++) 509 kfree(dev->dig_transfer_buffer[i]); 510 } 511 512 return result; 513 } 514 515 void au0828_dvb_unregister(struct au0828_dev *dev) 516 { 517 struct au0828_dvb *dvb = &dev->dvb; 518 519 dprintk(1, "%s()\n", __func__); 520 521 if (dvb->frontend == NULL) 522 return; 523 524 cancel_work_sync(&dev->restart_streaming); 525 526 dvb_net_release(&dvb->net); 527 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); 528 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); 529 dvb_dmxdev_release(&dvb->dmxdev); 530 dvb_dmx_release(&dvb->demux); 531 dvb_unregister_frontend(dvb->frontend); 532 dvb_frontend_detach(dvb->frontend); 533 dvb_unregister_adapter(&dvb->adapter); 534 535 if (preallocate_big_buffers) { 536 int i; 537 for (i = 0; i < URB_COUNT; i++) 538 kfree(dev->dig_transfer_buffer[i]); 539 } 540 dvb->frontend = NULL; 541 } 542 543 /* All the DVB attach calls go here, this function get's modified 544 * for each new card. No other function in this file needs 545 * to change. 546 */ 547 int au0828_dvb_register(struct au0828_dev *dev) 548 { 549 struct au0828_dvb *dvb = &dev->dvb; 550 int ret; 551 552 dprintk(1, "%s()\n", __func__); 553 554 /* init frontend */ 555 switch (dev->boardnr) { 556 case AU0828_BOARD_HAUPPAUGE_HVR850: 557 case AU0828_BOARD_HAUPPAUGE_HVR950Q: 558 dvb->frontend = dvb_attach(au8522_attach, 559 &hauppauge_hvr950q_config, 560 &dev->i2c_adap); 561 if (dvb->frontend != NULL) 562 switch (dev->board.tuner_type) { 563 default: 564 case TUNER_XC5000: 565 dvb_attach(xc5000_attach, dvb->frontend, 566 &dev->i2c_adap, 567 &hauppauge_xc5000a_config); 568 break; 569 case TUNER_XC5000C: 570 dvb_attach(xc5000_attach, dvb->frontend, 571 &dev->i2c_adap, 572 &hauppauge_xc5000c_config); 573 break; 574 } 575 break; 576 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL: 577 dvb->frontend = dvb_attach(au8522_attach, 578 &hauppauge_hvr950q_config, 579 &dev->i2c_adap); 580 if (dvb->frontend != NULL) 581 dvb_attach(mxl5007t_attach, dvb->frontend, 582 &dev->i2c_adap, 0x60, 583 &mxl5007t_hvr950q_config); 584 break; 585 case AU0828_BOARD_HAUPPAUGE_WOODBURY: 586 dvb->frontend = dvb_attach(au8522_attach, 587 &hauppauge_woodbury_config, 588 &dev->i2c_adap); 589 if (dvb->frontend != NULL) 590 dvb_attach(tda18271_attach, dvb->frontend, 591 0x60, &dev->i2c_adap, 592 &hauppauge_woodbury_tunerconfig); 593 break; 594 case AU0828_BOARD_DVICO_FUSIONHDTV7: 595 dvb->frontend = dvb_attach(au8522_attach, 596 &fusionhdtv7usb_config, 597 &dev->i2c_adap); 598 if (dvb->frontend != NULL) { 599 dvb_attach(xc5000_attach, dvb->frontend, 600 &dev->i2c_adap, 601 &hauppauge_xc5000a_config); 602 } 603 break; 604 default: 605 pr_warn("The frontend of your DVB/ATSC card isn't supported yet\n"); 606 break; 607 } 608 if (NULL == dvb->frontend) { 609 pr_err("%s() Frontend initialization failed\n", 610 __func__); 611 return -1; 612 } 613 /* define general-purpose callback pointer */ 614 dvb->frontend->callback = au0828_tuner_callback; 615 616 /* register everything */ 617 ret = dvb_register(dev); 618 if (ret < 0) { 619 if (dvb->frontend->ops.release) 620 dvb->frontend->ops.release(dvb->frontend); 621 dvb->frontend = NULL; 622 return ret; 623 } 624 625 return 0; 626 } 627 628 void au0828_dvb_suspend(struct au0828_dev *dev) 629 { 630 struct au0828_dvb *dvb = &dev->dvb; 631 int rc; 632 633 if (dvb->frontend) { 634 if (dev->urb_streaming) { 635 cancel_work_sync(&dev->restart_streaming); 636 /* Stop transport */ 637 mutex_lock(&dvb->lock); 638 stop_urb_transfer(dev); 639 au0828_stop_transport(dev, 1); 640 mutex_unlock(&dvb->lock); 641 dev->need_urb_start = true; 642 } 643 /* suspend frontend - does tuner and fe to sleep */ 644 rc = dvb_frontend_suspend(dvb->frontend); 645 pr_info("au0828_dvb_suspend(): Suspending DVB fe %d\n", rc); 646 } 647 } 648 649 void au0828_dvb_resume(struct au0828_dev *dev) 650 { 651 struct au0828_dvb *dvb = &dev->dvb; 652 int rc; 653 654 if (dvb->frontend) { 655 /* resume frontend - does fe and tuner init */ 656 rc = dvb_frontend_resume(dvb->frontend); 657 pr_info("au0828_dvb_resume(): Resuming DVB fe %d\n", rc); 658 if (dev->need_urb_start) { 659 /* Start transport */ 660 mutex_lock(&dvb->lock); 661 au0828_start_transport(dev); 662 start_urb_transfer(dev); 663 mutex_unlock(&dvb->lock); 664 } 665 } 666 } 667