2 * Driver for the Auvitek USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
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.
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
15 * GNU General Public License for more details.
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>
27 /* Due to enum tuner_pad_index */
28 #include <media/tuner.h>
31 * 1 = General debug messages
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");
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");
47 #define _AU0828_BULKPIPE 0x03
48 #define _BULKPIPESIZE 0xffff
50 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
52 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
53 u16 index, unsigned char *cp, u16 size);
56 #define CMD_REQUEST_IN 0x00
57 #define CMD_REQUEST_OUT 0x01
59 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
63 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
64 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
69 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
71 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
72 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
75 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
82 /* cp must be memory that has been allocated by kmalloc */
83 status = usb_control_msg(dev->usbdev,
84 usb_sndctrlpipe(dev->usbdev, 0),
86 USB_DIR_OUT | USB_TYPE_VENDOR |
88 value, index, NULL, 0, 1000);
90 status = min(status, 0);
93 pr_err("%s() Failed sending control message, error %d.\n",
102 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
103 u16 index, unsigned char *cp, u16 size)
105 int status = -ENODEV;
106 mutex_lock(&dev->mutex);
108 status = usb_control_msg(dev->usbdev,
109 usb_rcvctrlpipe(dev->usbdev, 0),
111 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113 dev->ctrlmsg, size, 1000);
115 status = min(status, 0);
118 pr_err("%s() Failed receiving control message, error %d.\n",
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);
126 mutex_unlock(&dev->mutex);
130 #ifdef CONFIG_MEDIA_CONTROLLER
131 static void au0828_media_graph_notify(struct media_entity *new,
135 static void au0828_unregister_media_device(struct au0828_dev *dev)
137 #ifdef CONFIG_MEDIA_CONTROLLER
138 struct media_device *mdev = dev->media_dev;
139 struct media_entity_notify *notify, *nextp;
141 if (!mdev || !media_devnode_is_registered(mdev->devnode))
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)
148 media_device_unregister_entity_notify(mdev, notify);
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);
158 media_device_delete(dev->media_dev, KBUILD_MODNAME, THIS_MODULE);
159 dev->media_dev = NULL;
163 void au0828_usb_release(struct au0828_dev *dev)
165 au0828_unregister_media_device(dev);
168 au0828_i2c_unregister(dev);
173 static void au0828_usb_disconnect(struct usb_interface *interface)
175 struct au0828_dev *dev = usb_get_intfdata(interface);
177 dprintk(1, "%s()\n", __func__);
179 /* there is a small window after disconnect, before
180 dev->usbdev is NULL, for poll (e.g: IR) try to access
181 the device and fill the dmesg with error messages.
182 Set the status so poll routines can check and avoid
183 access after disconnect.
185 set_bit(DEV_DISCONNECTED, &dev->dev_state);
187 au0828_rc_unregister(dev);
189 au0828_dvb_unregister(dev);
191 usb_set_intfdata(interface, NULL);
192 mutex_lock(&dev->mutex);
194 mutex_unlock(&dev->mutex);
195 if (au0828_analog_unregister(dev)) {
197 * No need to call au0828_usb_release() if V4L2 is enabled,
198 * as this is already called via au0828_usb_v4l2_release()
202 au0828_usb_release(dev);
205 static int au0828_media_device_init(struct au0828_dev *dev,
206 struct usb_device *udev)
208 #ifdef CONFIG_MEDIA_CONTROLLER
209 struct media_device *mdev;
211 mdev = media_device_usb_allocate(udev, KBUILD_MODNAME, THIS_MODULE);
215 dev->media_dev = mdev;
220 #ifdef CONFIG_MEDIA_CONTROLLER
221 static void au0828_media_graph_notify(struct media_entity *new,
224 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
226 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
230 * Called during au0828 probe time to connect
231 * entities that were created prior to registering
232 * the notify handler. Find mixer and decoder.
234 media_device_for_each_entity(entity, dev->media_dev) {
235 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
237 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
243 switch (new->function) {
244 case MEDIA_ENT_F_AUDIO_MIXER:
247 decoder = dev->decoder;
249 case MEDIA_ENT_F_ATV_DECODER:
250 /* In case, Mixer is added first, find mixer and create link */
251 media_device_for_each_entity(entity, dev->media_dev) {
252 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
262 if (decoder && mixer) {
263 ret = media_get_pad_index(decoder, false,
266 ret = media_create_pad_link(decoder, ret,
268 MEDIA_LNK_FL_ENABLED);
270 dev_err(&dev->usbdev->dev,
271 "Mixer Pad Link Create Error: %d\n", ret);
275 static bool au0828_is_link_shareable(struct media_entity *owner,
276 struct media_entity *entity)
278 bool shareable = false;
280 /* Tuner link can be shared by audio, video, and VBI */
281 switch (owner->function) {
282 case MEDIA_ENT_F_IO_V4L:
283 case MEDIA_ENT_F_AUDIO_CAPTURE:
284 case MEDIA_ENT_F_IO_VBI:
285 if (entity->function == MEDIA_ENT_F_IO_V4L ||
286 entity->function == MEDIA_ENT_F_AUDIO_CAPTURE ||
287 entity->function == MEDIA_ENT_F_IO_VBI)
290 case MEDIA_ENT_F_DTV_DEMOD:
297 /* Callers should hold graph_mutex */
298 static int au0828_enable_source(struct media_entity *entity,
299 struct media_pipeline *pipe)
301 struct media_entity *source, *find_source;
302 struct media_entity *sink;
303 struct media_link *link, *found_link = NULL;
305 struct media_device *mdev = entity->graph_obj.mdev;
306 struct au0828_dev *dev;
311 dev = mdev->source_priv;
314 * For Audio and V4L2 entity, find the link to which decoder
315 * is the sink. Look for an active link between decoder and
316 * source (tuner/s-video/Composite), if one exists, nothing
317 * to do. If not, look for any active links between source
318 * and any other entity. If one exists, source is busy. If
319 * source is free, setup link and start pipeline from source.
320 * For DVB FE entity, the source for the link is the tuner.
321 * Check if tuner is available and setup link and start
324 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
326 find_source = dev->tuner;
328 /* Analog isn't configured or register failed */
337 * Default input is tuner and default input_type
338 * is AU0828_VMUX_TELEVISION.
340 * There is a problem when s_input is called to
341 * change the default input. s_input will try to
342 * enable_source before attempting to change the
343 * input on the device, and will end up enabling
344 * default source which is tuner.
346 * Additional logic is necessary in au0828 to detect
347 * that the input has changed and enable the right
348 * source. au0828 handles this case in its s_input.
349 * It will disable the old source and enable the new
353 if (dev->input_type == AU0828_VMUX_TELEVISION)
354 find_source = dev->tuner;
355 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
356 dev->input_type == AU0828_VMUX_COMPOSITE)
357 find_source = &dev->input_ent[dev->input_type];
359 /* unknown input - let user select input */
365 /* Is there an active link between sink and source */
366 if (dev->active_link) {
367 if (dev->active_link_owner == entity) {
368 /* This check is necessary to handle multiple
369 * enable_source calls from v4l_ioctls during
370 * the course of video/vbi application run-time.
372 pr_debug("%s already owns the tuner\n", entity->name);
375 } else if (au0828_is_link_shareable(dev->active_link_owner,
377 /* Either ALSA or Video own tuner. Sink is the same
378 * for both. Allow sharing the active link between
379 * their common source (tuner) and sink (decoder).
380 * Starting pipeline between sharing entity and sink
381 * will fail with pipe mismatch, while owner has an
382 * active pipeline. Switch pipeline ownership from
383 * user to owner when owner disables the source.
385 dev->active_link_shared = true;
386 /* save the user info to use from disable */
387 dev->active_link_user = entity;
388 dev->active_link_user_pipe = pipe;
389 pr_debug("%s owns the tuner %s can share!\n",
390 dev->active_link_owner->name,
400 list_for_each_entry(link, &sink->links, list) {
401 /* Check sink, and source */
402 if (link->sink->entity == sink &&
403 link->source->entity == find_source) {
414 /* activate link between source and sink and start pipeline */
415 source = found_link->source->entity;
416 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
418 pr_err("Activate link from %s->%s. Error %d\n",
419 source->name, sink->name, ret);
423 ret = __media_pipeline_start(entity, pipe);
425 pr_err("Start Pipeline: %s->%s Error %d\n",
426 source->name, entity->name, ret);
427 ret = __media_entity_setup_link(found_link, 0);
429 pr_err("Deactivate link Error %d\n", ret);
433 /* save link state to allow audio and video share the link
434 * and not disable the link while the other is using it.
435 * active_link_owner is used to deactivate the link.
437 dev->active_link = found_link;
438 dev->active_link_owner = entity;
439 dev->active_source = source;
440 dev->active_sink = sink;
442 pr_info("Enabled Source: %s->%s->%s Ret %d\n",
443 dev->active_source->name, dev->active_sink->name,
444 dev->active_link_owner->name, ret);
446 pr_debug("%s end: ent:%s fnc:%d ret %d\n",
447 __func__, entity->name, entity->function, ret);
451 /* Callers should hold graph_mutex */
452 static void au0828_disable_source(struct media_entity *entity)
455 struct media_device *mdev = entity->graph_obj.mdev;
456 struct au0828_dev *dev;
461 dev = mdev->source_priv;
463 if (!dev->active_link)
466 /* link is active - stop pipeline from source
467 * (tuner/s-video/Composite) to the entity
468 * When DVB/s-video/Composite owns tuner, it won't be in
471 if (dev->active_link->sink->entity == dev->active_sink &&
472 dev->active_link->source->entity == dev->active_source) {
474 * Prevent video from deactivating link when audio
475 * has active pipeline and vice versa. In addition
476 * handle the case when more than one video/vbi
477 * application is sharing the link.
479 bool owner_is_audio = false;
481 if (dev->active_link_owner->function ==
482 MEDIA_ENT_F_AUDIO_CAPTURE)
483 owner_is_audio = true;
485 if (dev->active_link_shared) {
486 pr_debug("Shared link owner %s user %s %d\n",
487 dev->active_link_owner->name,
488 entity->name, dev->users);
490 /* Handle video device users > 1
491 * When audio owns the shared link with
492 * more than one video users, avoid
493 * disabling the source and/or switching
494 * the owner until the last disable_source
495 * call from video _close(). Use dev->users to
496 * determine when to switch/disable.
498 if (dev->active_link_owner != entity) {
499 /* video device has users > 1 */
500 if (owner_is_audio && dev->users > 1)
503 dev->active_link_user = NULL;
504 dev->active_link_user_pipe = NULL;
505 dev->active_link_shared = false;
509 /* video owns the link and has users > 1 */
510 if (!owner_is_audio && dev->users > 1)
514 __media_pipeline_stop(dev->active_link_owner);
515 pr_debug("Pipeline stop for %s\n",
516 dev->active_link_owner->name);
518 ret = __media_pipeline_start(
519 dev->active_link_user,
520 dev->active_link_user_pipe);
522 pr_err("Start Pipeline: %s->%s %d\n",
523 dev->active_source->name,
524 dev->active_link_user->name,
526 goto deactivate_link;
528 /* link user is now the owner */
529 dev->active_link_owner = dev->active_link_user;
530 dev->active_link_user = NULL;
531 dev->active_link_user_pipe = NULL;
532 dev->active_link_shared = false;
534 pr_debug("Pipeline started for %s\n",
535 dev->active_link_owner->name);
537 } else if (!owner_is_audio && dev->users > 1)
538 /* video/vbi owns the link and has users > 1 */
541 if (dev->active_link_owner != entity)
545 __media_pipeline_stop(dev->active_link_owner);
546 pr_debug("Pipeline stop for %s\n",
547 dev->active_link_owner->name);
550 ret = __media_entity_setup_link(dev->active_link, 0);
552 pr_err("Deactivate link Error %d\n", ret);
554 pr_info("Disabled Source: %s->%s->%s Ret %d\n",
555 dev->active_source->name, dev->active_sink->name,
556 dev->active_link_owner->name, ret);
558 dev->active_link = NULL;
559 dev->active_link_owner = NULL;
560 dev->active_source = NULL;
561 dev->active_sink = NULL;
562 dev->active_link_shared = false;
563 dev->active_link_user = NULL;
568 static int au0828_media_device_register(struct au0828_dev *dev,
569 struct usb_device *udev)
571 #ifdef CONFIG_MEDIA_CONTROLLER
573 struct media_entity *entity, *demod = NULL;
574 struct media_link *link;
579 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
581 /* register media device */
582 ret = media_device_register(dev->media_dev);
584 media_device_delete(dev->media_dev, KBUILD_MODNAME,
586 dev->media_dev = NULL;
588 "Media Device Register Error: %d\n", ret);
593 * Call au0828_media_graph_notify() to connect
594 * audio graph to our graph. In this case, audio
595 * driver registered the device and there is no
596 * entity_notify to be called when new entities
597 * are added. Invoke it now.
599 au0828_media_graph_notify(NULL, (void *) dev);
603 * Find tuner, decoder and demod.
605 * The tuner and decoder should be cached, as they'll be used by
606 * au0828_enable_source.
608 * It also needs to disable the link between tuner and
609 * decoder/demod, to avoid disable step when tuner is requested
610 * by video or audio. Note that this step can't be done until dvb
611 * graph is created during dvb register.
613 media_device_for_each_entity(entity, dev->media_dev) {
614 switch (entity->function) {
615 case MEDIA_ENT_F_TUNER:
618 case MEDIA_ENT_F_ATV_DECODER:
619 dev->decoder = entity;
621 case MEDIA_ENT_F_DTV_DEMOD:
627 /* Disable link between tuner->demod and/or tuner->decoder */
629 list_for_each_entry(link, &dev->tuner->links, list) {
630 if (demod && link->sink->entity == demod)
631 media_entity_setup_link(link, 0);
632 if (dev->decoder && link->sink->entity == dev->decoder)
633 media_entity_setup_link(link, 0);
637 /* register entity_notify callback */
638 dev->entity_notify.notify_data = (void *) dev;
639 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
640 ret = media_device_register_entity_notify(dev->media_dev,
641 &dev->entity_notify);
644 "Media Device register entity_notify Error: %d\n",
648 /* set enable_source */
649 mutex_lock(&dev->media_dev->graph_mutex);
650 dev->media_dev->source_priv = (void *) dev;
651 dev->media_dev->enable_source = au0828_enable_source;
652 dev->media_dev->disable_source = au0828_disable_source;
653 mutex_unlock(&dev->media_dev->graph_mutex);
658 static int au0828_usb_probe(struct usb_interface *interface,
659 const struct usb_device_id *id)
664 struct au0828_dev *dev;
665 struct usb_device *usbdev = interface_to_usbdev(interface);
667 ifnum = interface->altsetting->desc.bInterfaceNumber;
672 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
673 le16_to_cpu(usbdev->descriptor.idVendor),
674 le16_to_cpu(usbdev->descriptor.idProduct),
678 * Make sure we have 480 Mbps of bandwidth, otherwise things like
679 * video stream wouldn't likely work, since 12 Mbps is generally
680 * not enough even for most Digital TV streams.
682 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
683 pr_err("au0828: Device initialization failed.\n");
684 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
688 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
690 pr_err("%s() Unable to allocate memory\n", __func__);
694 mutex_init(&dev->lock);
695 mutex_lock(&dev->lock);
696 mutex_init(&dev->mutex);
697 mutex_init(&dev->dvb.lock);
698 dev->usbdev = usbdev;
699 dev->boardnr = id->driver_info;
700 dev->board = au0828_boards[dev->boardnr];
702 /* Initialize the media controller */
703 retval = au0828_media_device_init(dev, usbdev);
705 pr_err("%s() au0828_media_device_init failed\n",
707 mutex_unlock(&dev->lock);
712 retval = au0828_v4l2_device_register(interface, dev);
714 au0828_usb_v4l2_media_release(dev);
715 mutex_unlock(&dev->lock);
720 /* Power Up the bridge */
721 au0828_write(dev, REG_600, 1 << 4);
723 /* Bring up the GPIO's and supporting devices */
724 au0828_gpio_setup(dev);
727 au0828_i2c_register(dev);
730 au0828_card_setup(dev);
733 retval = au0828_analog_register(dev, interface);
735 pr_err("%s() au0828_analog_register failed to register on V4L2\n",
737 mutex_unlock(&dev->lock);
742 retval = au0828_dvb_register(dev);
744 pr_err("%s() au0828_dvb_register failed\n",
747 /* Remote controller */
748 au0828_rc_register(dev);
751 * Store the pointer to the au0828_dev so it can be accessed in
752 * au0828_usb_disconnect
754 usb_set_intfdata(interface, dev);
756 pr_info("Registered device AU0828 [%s]\n",
757 dev->board.name == NULL ? "Unset" : dev->board.name);
759 mutex_unlock(&dev->lock);
761 retval = au0828_media_device_register(dev, usbdev);
765 au0828_usb_disconnect(interface);
770 static int au0828_suspend(struct usb_interface *interface,
771 pm_message_t message)
773 struct au0828_dev *dev = usb_get_intfdata(interface);
778 pr_info("Suspend\n");
780 au0828_rc_suspend(dev);
781 au0828_v4l2_suspend(dev);
782 au0828_dvb_suspend(dev);
784 /* FIXME: should suspend also ATV/DTV */
789 static int au0828_resume(struct usb_interface *interface)
791 struct au0828_dev *dev = usb_get_intfdata(interface);
797 /* Power Up the bridge */
798 au0828_write(dev, REG_600, 1 << 4);
800 /* Bring up the GPIO's and supporting devices */
801 au0828_gpio_setup(dev);
803 au0828_rc_resume(dev);
804 au0828_v4l2_resume(dev);
805 au0828_dvb_resume(dev);
807 /* FIXME: should resume also ATV/DTV */
812 static struct usb_driver au0828_usb_driver = {
813 .name = KBUILD_MODNAME,
814 .probe = au0828_usb_probe,
815 .disconnect = au0828_usb_disconnect,
816 .id_table = au0828_usb_id_table,
817 .suspend = au0828_suspend,
818 .resume = au0828_resume,
819 .reset_resume = au0828_resume,
822 static int __init au0828_init(void)
826 if (au0828_debug & 1)
827 pr_info("%s() Debugging is enabled\n", __func__);
829 if (au0828_debug & 2)
830 pr_info("%s() USB Debugging is enabled\n", __func__);
832 if (au0828_debug & 4)
833 pr_info("%s() I2C Debugging is enabled\n", __func__);
835 if (au0828_debug & 8)
836 pr_info("%s() Bridge Debugging is enabled\n",
839 if (au0828_debug & 16)
840 pr_info("%s() IR Debugging is enabled\n",
843 pr_info("au0828 driver loaded\n");
845 ret = usb_register(&au0828_usb_driver);
847 pr_err("usb_register failed, error = %d\n", ret);
852 static void __exit au0828_exit(void)
854 usb_deregister(&au0828_usb_driver);
857 module_init(au0828_init);
858 module_exit(au0828_exit);
860 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
861 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
862 MODULE_LICENSE("GPL");
863 MODULE_VERSION("0.0.3");