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