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 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 #include "au0828.h" 23 #include "au8522.h" 24 25 #include <linux/module.h> 26 #include <linux/slab.h> 27 #include <linux/videodev2.h> 28 #include <media/v4l2-common.h> 29 #include <linux/mutex.h> 30 31 /* Due to enum tuner_pad_index */ 32 #include <media/tuner.h> 33 34 /* 35 * 1 = General debug messages 36 * 2 = USB handling 37 * 4 = I2C related 38 * 8 = Bridge related 39 * 16 = IR related 40 */ 41 int au0828_debug; 42 module_param_named(debug, au0828_debug, int, 0644); 43 MODULE_PARM_DESC(debug, 44 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR"); 45 46 static unsigned int disable_usb_speed_check; 47 module_param(disable_usb_speed_check, int, 0444); 48 MODULE_PARM_DESC(disable_usb_speed_check, 49 "override min bandwidth requirement of 480M bps"); 50 51 #define _AU0828_BULKPIPE 0x03 52 #define _BULKPIPESIZE 0xffff 53 54 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, 55 u16 index); 56 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, 57 u16 index, unsigned char *cp, u16 size); 58 59 /* USB Direction */ 60 #define CMD_REQUEST_IN 0x00 61 #define CMD_REQUEST_OUT 0x01 62 63 u32 au0828_readreg(struct au0828_dev *dev, u16 reg) 64 { 65 u8 result = 0; 66 67 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1); 68 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result); 69 70 return result; 71 } 72 73 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val) 74 { 75 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val); 76 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg); 77 } 78 79 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, 80 u16 index) 81 { 82 int status = -ENODEV; 83 84 if (dev->usbdev) { 85 86 /* cp must be memory that has been allocated by kmalloc */ 87 status = usb_control_msg(dev->usbdev, 88 usb_sndctrlpipe(dev->usbdev, 0), 89 request, 90 USB_DIR_OUT | USB_TYPE_VENDOR | 91 USB_RECIP_DEVICE, 92 value, index, NULL, 0, 1000); 93 94 status = min(status, 0); 95 96 if (status < 0) { 97 pr_err("%s() Failed sending control message, error %d.\n", 98 __func__, status); 99 } 100 101 } 102 103 return status; 104 } 105 106 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, 107 u16 index, unsigned char *cp, u16 size) 108 { 109 int status = -ENODEV; 110 mutex_lock(&dev->mutex); 111 if (dev->usbdev) { 112 status = usb_control_msg(dev->usbdev, 113 usb_rcvctrlpipe(dev->usbdev, 0), 114 request, 115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 116 value, index, 117 dev->ctrlmsg, size, 1000); 118 119 status = min(status, 0); 120 121 if (status < 0) { 122 pr_err("%s() Failed receiving control message, error %d.\n", 123 __func__, status); 124 } 125 126 /* the host controller requires heap allocated memory, which 127 is why we didn't just pass "cp" into usb_control_msg */ 128 memcpy(cp, dev->ctrlmsg, size); 129 } 130 mutex_unlock(&dev->mutex); 131 return status; 132 } 133 134 #ifdef CONFIG_MEDIA_CONTROLLER 135 static void au0828_media_graph_notify(struct media_entity *new, 136 void *notify_data); 137 #endif 138 139 static void au0828_unregister_media_device(struct au0828_dev *dev) 140 { 141 #ifdef CONFIG_MEDIA_CONTROLLER 142 struct media_device *mdev = dev->media_dev; 143 struct media_entity_notify *notify, *nextp; 144 145 if (!mdev || !media_devnode_is_registered(mdev->devnode)) 146 return; 147 148 /* Remove au0828 entity_notify callbacks */ 149 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) { 150 if (notify->notify != au0828_media_graph_notify) 151 continue; 152 media_device_unregister_entity_notify(mdev, notify); 153 } 154 155 /* clear enable_source, disable_source */ 156 dev->media_dev->source_priv = NULL; 157 dev->media_dev->enable_source = NULL; 158 dev->media_dev->disable_source = NULL; 159 160 media_device_unregister(dev->media_dev); 161 media_device_cleanup(dev->media_dev); 162 kfree(dev->media_dev); 163 dev->media_dev = NULL; 164 #endif 165 } 166 167 void au0828_usb_release(struct au0828_dev *dev) 168 { 169 au0828_unregister_media_device(dev); 170 171 /* I2C */ 172 au0828_i2c_unregister(dev); 173 174 kfree(dev); 175 } 176 177 static void au0828_usb_disconnect(struct usb_interface *interface) 178 { 179 struct au0828_dev *dev = usb_get_intfdata(interface); 180 181 dprintk(1, "%s()\n", __func__); 182 183 /* there is a small window after disconnect, before 184 dev->usbdev is NULL, for poll (e.g: IR) try to access 185 the device and fill the dmesg with error messages. 186 Set the status so poll routines can check and avoid 187 access after disconnect. 188 */ 189 set_bit(DEV_DISCONNECTED, &dev->dev_state); 190 191 au0828_rc_unregister(dev); 192 /* Digital TV */ 193 au0828_dvb_unregister(dev); 194 195 usb_set_intfdata(interface, NULL); 196 mutex_lock(&dev->mutex); 197 dev->usbdev = NULL; 198 mutex_unlock(&dev->mutex); 199 if (au0828_analog_unregister(dev)) { 200 /* 201 * No need to call au0828_usb_release() if V4L2 is enabled, 202 * as this is already called via au0828_usb_v4l2_release() 203 */ 204 return; 205 } 206 au0828_usb_release(dev); 207 } 208 209 static int au0828_media_device_init(struct au0828_dev *dev, 210 struct usb_device *udev) 211 { 212 #ifdef CONFIG_MEDIA_CONTROLLER 213 struct media_device *mdev; 214 215 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); 216 if (!mdev) 217 return -ENOMEM; 218 219 /* check if media device is already initialized */ 220 if (!mdev->dev) 221 media_device_usb_init(mdev, udev, udev->product); 222 223 dev->media_dev = mdev; 224 #endif 225 return 0; 226 } 227 228 #ifdef CONFIG_MEDIA_CONTROLLER 229 static void au0828_media_graph_notify(struct media_entity *new, 230 void *notify_data) 231 { 232 struct au0828_dev *dev = (struct au0828_dev *) notify_data; 233 int ret; 234 struct media_entity *entity, *mixer = NULL, *decoder = NULL; 235 236 if (!new) { 237 /* 238 * Called during au0828 probe time to connect 239 * entites that were created prior to registering 240 * the notify handler. Find mixer and decoder. 241 */ 242 media_device_for_each_entity(entity, dev->media_dev) { 243 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER) 244 mixer = entity; 245 else if (entity->function == MEDIA_ENT_F_ATV_DECODER) 246 decoder = entity; 247 } 248 goto create_link; 249 } 250 251 switch (new->function) { 252 case MEDIA_ENT_F_AUDIO_MIXER: 253 mixer = new; 254 if (dev->decoder) 255 decoder = dev->decoder; 256 break; 257 case MEDIA_ENT_F_ATV_DECODER: 258 /* In case, Mixer is added first, find mixer and create link */ 259 media_device_for_each_entity(entity, dev->media_dev) { 260 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER) 261 mixer = entity; 262 } 263 decoder = new; 264 break; 265 default: 266 break; 267 } 268 269 create_link: 270 if (decoder && mixer) { 271 ret = media_create_pad_link(decoder, 272 DEMOD_PAD_AUDIO_OUT, 273 mixer, 0, 274 MEDIA_LNK_FL_ENABLED); 275 if (ret) 276 dev_err(&dev->usbdev->dev, 277 "Mixer Pad Link Create Error: %d\n", ret); 278 } 279 } 280 281 static int au0828_enable_source(struct media_entity *entity, 282 struct media_pipeline *pipe) 283 { 284 struct media_entity *source, *find_source; 285 struct media_entity *sink; 286 struct media_link *link, *found_link = NULL; 287 int ret = 0; 288 struct media_device *mdev = entity->graph_obj.mdev; 289 struct au0828_dev *dev; 290 291 if (!mdev) 292 return -ENODEV; 293 294 mutex_lock(&mdev->graph_mutex); 295 296 dev = mdev->source_priv; 297 298 /* 299 * For Audio and V4L2 entity, find the link to which decoder 300 * is the sink. Look for an active link between decoder and 301 * source (tuner/s-video/Composite), if one exists, nothing 302 * to do. If not, look for any active links between source 303 * and any other entity. If one exists, source is busy. If 304 * source is free, setup link and start pipeline from source. 305 * For DVB FE entity, the source for the link is the tuner. 306 * Check if tuner is available and setup link and start 307 * pipeline. 308 */ 309 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) { 310 sink = entity; 311 find_source = dev->tuner; 312 } else { 313 /* Analog isn't configured or register failed */ 314 if (!dev->decoder) { 315 ret = -ENODEV; 316 goto end; 317 } 318 319 sink = dev->decoder; 320 321 /* 322 * Default input is tuner and default input_type 323 * is AU0828_VMUX_TELEVISION. 324 * FIXME: 325 * There is a problem when s_input is called to 326 * change the default input. s_input will try to 327 * enable_source before attempting to change the 328 * input on the device, and will end up enabling 329 * default source which is tuner. 330 * 331 * Additional logic is necessary in au0828 332 * to detect that the input has changed and 333 * enable the right source. 334 */ 335 336 if (dev->input_type == AU0828_VMUX_TELEVISION) 337 find_source = dev->tuner; 338 else if (dev->input_type == AU0828_VMUX_SVIDEO || 339 dev->input_type == AU0828_VMUX_COMPOSITE) 340 find_source = &dev->input_ent[dev->input_type]; 341 else { 342 /* unknown input - let user select input */ 343 ret = 0; 344 goto end; 345 } 346 } 347 348 /* Is an active link between sink and source */ 349 if (dev->active_link) { 350 /* 351 * If DVB is using the tuner and calling entity is 352 * audio/video, the following check will be false, 353 * since sink is different. Result is Busy. 354 */ 355 if (dev->active_link->sink->entity == sink && 356 dev->active_link->source->entity == find_source) { 357 /* 358 * Either ALSA or Video own tuner. sink is 359 * the same for both. Prevent Video stepping 360 * on ALSA when ALSA owns the source. 361 */ 362 if (dev->active_link_owner != entity && 363 dev->active_link_owner->function == 364 MEDIA_ENT_F_AUDIO_CAPTURE) { 365 pr_debug("ALSA has the tuner\n"); 366 ret = -EBUSY; 367 goto end; 368 } 369 ret = 0; 370 goto end; 371 } else { 372 ret = -EBUSY; 373 goto end; 374 } 375 } 376 377 list_for_each_entry(link, &sink->links, list) { 378 /* Check sink, and source */ 379 if (link->sink->entity == sink && 380 link->source->entity == find_source) { 381 found_link = link; 382 break; 383 } 384 } 385 386 if (!found_link) { 387 ret = -ENODEV; 388 goto end; 389 } 390 391 /* activate link between source and sink and start pipeline */ 392 source = found_link->source->entity; 393 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED); 394 if (ret) { 395 pr_err("Activate tuner link %s->%s. Error %d\n", 396 source->name, sink->name, ret); 397 goto end; 398 } 399 400 ret = __media_entity_pipeline_start(entity, pipe); 401 if (ret) { 402 pr_err("Start Pipeline: %s->%s Error %d\n", 403 source->name, entity->name, ret); 404 ret = __media_entity_setup_link(found_link, 0); 405 pr_err("Deactivate link Error %d\n", ret); 406 goto end; 407 } 408 /* 409 * save active link and active link owner to avoid audio 410 * deactivating video owned link from disable_source and 411 * vice versa 412 */ 413 dev->active_link = found_link; 414 dev->active_link_owner = entity; 415 dev->active_source = source; 416 dev->active_sink = sink; 417 418 pr_debug("Enabled Source: %s->%s->%s Ret %d\n", 419 dev->active_source->name, dev->active_sink->name, 420 dev->active_link_owner->name, ret); 421 end: 422 mutex_unlock(&mdev->graph_mutex); 423 pr_debug("au0828_enable_source() end %s %d %d\n", 424 entity->name, entity->function, ret); 425 return ret; 426 } 427 428 static void au0828_disable_source(struct media_entity *entity) 429 { 430 int ret = 0; 431 struct media_device *mdev = entity->graph_obj.mdev; 432 struct au0828_dev *dev; 433 434 if (!mdev) 435 return; 436 437 mutex_lock(&mdev->graph_mutex); 438 dev = mdev->source_priv; 439 440 if (!dev->active_link) { 441 ret = -ENODEV; 442 goto end; 443 } 444 445 /* link is active - stop pipeline from source (tuner) */ 446 if (dev->active_link->sink->entity == dev->active_sink && 447 dev->active_link->source->entity == dev->active_source) { 448 /* 449 * prevent video from deactivating link when audio 450 * has active pipeline 451 */ 452 if (dev->active_link_owner != entity) 453 goto end; 454 __media_entity_pipeline_stop(entity); 455 ret = __media_entity_setup_link(dev->active_link, 0); 456 if (ret) 457 pr_err("Deactivate link Error %d\n", ret); 458 459 pr_debug("Disabled Source: %s->%s->%s Ret %d\n", 460 dev->active_source->name, dev->active_sink->name, 461 dev->active_link_owner->name, ret); 462 463 dev->active_link = NULL; 464 dev->active_link_owner = NULL; 465 dev->active_source = NULL; 466 dev->active_sink = NULL; 467 } 468 469 end: 470 mutex_unlock(&mdev->graph_mutex); 471 } 472 #endif 473 474 static int au0828_media_device_register(struct au0828_dev *dev, 475 struct usb_device *udev) 476 { 477 #ifdef CONFIG_MEDIA_CONTROLLER 478 int ret; 479 struct media_entity *entity, *demod = NULL; 480 struct media_link *link; 481 482 if (!dev->media_dev) 483 return 0; 484 485 if (!media_devnode_is_registered(dev->media_dev->devnode)) { 486 487 /* register media device */ 488 ret = media_device_register(dev->media_dev); 489 if (ret) { 490 dev_err(&udev->dev, 491 "Media Device Register Error: %d\n", ret); 492 return ret; 493 } 494 } else { 495 /* 496 * Call au0828_media_graph_notify() to connect 497 * audio graph to our graph. In this case, audio 498 * driver registered the device and there is no 499 * entity_notify to be called when new entities 500 * are added. Invoke it now. 501 */ 502 au0828_media_graph_notify(NULL, (void *) dev); 503 } 504 505 /* 506 * Find tuner, decoder and demod. 507 * 508 * The tuner and decoder should be cached, as they'll be used by 509 * au0828_enable_source. 510 * 511 * It also needs to disable the link between tuner and 512 * decoder/demod, to avoid disable step when tuner is requested 513 * by video or audio. Note that this step can't be done until dvb 514 * graph is created during dvb register. 515 */ 516 media_device_for_each_entity(entity, dev->media_dev) { 517 switch (entity->function) { 518 case MEDIA_ENT_F_TUNER: 519 dev->tuner = entity; 520 break; 521 case MEDIA_ENT_F_ATV_DECODER: 522 dev->decoder = entity; 523 break; 524 case MEDIA_ENT_F_DTV_DEMOD: 525 demod = entity; 526 break; 527 } 528 } 529 530 /* Disable link between tuner->demod and/or tuner->decoder */ 531 if (dev->tuner) { 532 list_for_each_entry(link, &dev->tuner->links, list) { 533 if (demod && link->sink->entity == demod) 534 media_entity_setup_link(link, 0); 535 if (dev->decoder && link->sink->entity == dev->decoder) 536 media_entity_setup_link(link, 0); 537 } 538 } 539 540 /* register entity_notify callback */ 541 dev->entity_notify.notify_data = (void *) dev; 542 dev->entity_notify.notify = (void *) au0828_media_graph_notify; 543 ret = media_device_register_entity_notify(dev->media_dev, 544 &dev->entity_notify); 545 if (ret) { 546 dev_err(&udev->dev, 547 "Media Device register entity_notify Error: %d\n", 548 ret); 549 return ret; 550 } 551 /* set enable_source */ 552 dev->media_dev->source_priv = (void *) dev; 553 dev->media_dev->enable_source = au0828_enable_source; 554 dev->media_dev->disable_source = au0828_disable_source; 555 #endif 556 return 0; 557 } 558 559 static int au0828_usb_probe(struct usb_interface *interface, 560 const struct usb_device_id *id) 561 { 562 int ifnum; 563 int retval = 0; 564 565 struct au0828_dev *dev; 566 struct usb_device *usbdev = interface_to_usbdev(interface); 567 568 ifnum = interface->altsetting->desc.bInterfaceNumber; 569 570 if (ifnum != 0) 571 return -ENODEV; 572 573 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__, 574 le16_to_cpu(usbdev->descriptor.idVendor), 575 le16_to_cpu(usbdev->descriptor.idProduct), 576 ifnum); 577 578 /* 579 * Make sure we have 480 Mbps of bandwidth, otherwise things like 580 * video stream wouldn't likely work, since 12 Mbps is generally 581 * not enough even for most Digital TV streams. 582 */ 583 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) { 584 pr_err("au0828: Device initialization failed.\n"); 585 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n"); 586 return -ENODEV; 587 } 588 589 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 590 if (dev == NULL) { 591 pr_err("%s() Unable to allocate memory\n", __func__); 592 return -ENOMEM; 593 } 594 595 mutex_init(&dev->lock); 596 mutex_lock(&dev->lock); 597 mutex_init(&dev->mutex); 598 mutex_init(&dev->dvb.lock); 599 dev->usbdev = usbdev; 600 dev->boardnr = id->driver_info; 601 dev->board = au0828_boards[dev->boardnr]; 602 603 /* Initialize the media controller */ 604 retval = au0828_media_device_init(dev, usbdev); 605 if (retval) { 606 pr_err("%s() au0828_media_device_init failed\n", 607 __func__); 608 mutex_unlock(&dev->lock); 609 kfree(dev); 610 return retval; 611 } 612 613 retval = au0828_v4l2_device_register(interface, dev); 614 if (retval) { 615 au0828_usb_v4l2_media_release(dev); 616 mutex_unlock(&dev->lock); 617 kfree(dev); 618 return retval; 619 } 620 621 /* Power Up the bridge */ 622 au0828_write(dev, REG_600, 1 << 4); 623 624 /* Bring up the GPIO's and supporting devices */ 625 au0828_gpio_setup(dev); 626 627 /* I2C */ 628 au0828_i2c_register(dev); 629 630 /* Setup */ 631 au0828_card_setup(dev); 632 633 /* Analog TV */ 634 retval = au0828_analog_register(dev, interface); 635 if (retval) { 636 pr_err("%s() au0282_dev_register failed to register on V4L2\n", 637 __func__); 638 goto done; 639 } 640 641 /* Digital TV */ 642 retval = au0828_dvb_register(dev); 643 if (retval) 644 pr_err("%s() au0282_dev_register failed\n", 645 __func__); 646 647 /* Remote controller */ 648 au0828_rc_register(dev); 649 650 /* 651 * Store the pointer to the au0828_dev so it can be accessed in 652 * au0828_usb_disconnect 653 */ 654 usb_set_intfdata(interface, dev); 655 656 pr_info("Registered device AU0828 [%s]\n", 657 dev->board.name == NULL ? "Unset" : dev->board.name); 658 659 mutex_unlock(&dev->lock); 660 661 retval = au0828_media_device_register(dev, usbdev); 662 663 done: 664 if (retval < 0) 665 au0828_usb_disconnect(interface); 666 667 return retval; 668 } 669 670 static int au0828_suspend(struct usb_interface *interface, 671 pm_message_t message) 672 { 673 struct au0828_dev *dev = usb_get_intfdata(interface); 674 675 if (!dev) 676 return 0; 677 678 pr_info("Suspend\n"); 679 680 au0828_rc_suspend(dev); 681 au0828_v4l2_suspend(dev); 682 au0828_dvb_suspend(dev); 683 684 /* FIXME: should suspend also ATV/DTV */ 685 686 return 0; 687 } 688 689 static int au0828_resume(struct usb_interface *interface) 690 { 691 struct au0828_dev *dev = usb_get_intfdata(interface); 692 if (!dev) 693 return 0; 694 695 pr_info("Resume\n"); 696 697 /* Power Up the bridge */ 698 au0828_write(dev, REG_600, 1 << 4); 699 700 /* Bring up the GPIO's and supporting devices */ 701 au0828_gpio_setup(dev); 702 703 au0828_rc_resume(dev); 704 au0828_v4l2_resume(dev); 705 au0828_dvb_resume(dev); 706 707 /* FIXME: should resume also ATV/DTV */ 708 709 return 0; 710 } 711 712 static struct usb_driver au0828_usb_driver = { 713 .name = KBUILD_MODNAME, 714 .probe = au0828_usb_probe, 715 .disconnect = au0828_usb_disconnect, 716 .id_table = au0828_usb_id_table, 717 .suspend = au0828_suspend, 718 .resume = au0828_resume, 719 .reset_resume = au0828_resume, 720 }; 721 722 static int __init au0828_init(void) 723 { 724 int ret; 725 726 if (au0828_debug & 1) 727 pr_info("%s() Debugging is enabled\n", __func__); 728 729 if (au0828_debug & 2) 730 pr_info("%s() USB Debugging is enabled\n", __func__); 731 732 if (au0828_debug & 4) 733 pr_info("%s() I2C Debugging is enabled\n", __func__); 734 735 if (au0828_debug & 8) 736 pr_info("%s() Bridge Debugging is enabled\n", 737 __func__); 738 739 if (au0828_debug & 16) 740 pr_info("%s() IR Debugging is enabled\n", 741 __func__); 742 743 pr_info("au0828 driver loaded\n"); 744 745 ret = usb_register(&au0828_usb_driver); 746 if (ret) 747 pr_err("usb_register failed, error = %d\n", ret); 748 749 return ret; 750 } 751 752 static void __exit au0828_exit(void) 753 { 754 usb_deregister(&au0828_usb_driver); 755 } 756 757 module_init(au0828_init); 758 module_exit(au0828_exit); 759 760 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products"); 761 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); 762 MODULE_LICENSE("GPL"); 763 MODULE_VERSION("0.0.3"); 764