1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Auvitek USB bridge
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-common.h>
15 #include <linux/mutex.h>
17 /* Due to enum tuner_pad_index */
18 #include <media/tuner.h>
21 * 1 = General debug messages
28 module_param_named(debug, au0828_debug, int, 0644);
29 MODULE_PARM_DESC(debug,
30 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
32 static unsigned int disable_usb_speed_check;
33 module_param(disable_usb_speed_check, int, 0444);
34 MODULE_PARM_DESC(disable_usb_speed_check,
35 "override min bandwidth requirement of 480M bps");
37 #define _AU0828_BULKPIPE 0x03
38 #define _BULKPIPESIZE 0xffff
40 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
42 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
43 u16 index, unsigned char *cp, u16 size);
46 #define CMD_REQUEST_IN 0x00
47 #define CMD_REQUEST_OUT 0x01
49 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
53 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
54 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
59 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
61 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
62 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
65 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
72 /* cp must be memory that has been allocated by kmalloc */
73 status = usb_control_msg(dev->usbdev,
74 usb_sndctrlpipe(dev->usbdev, 0),
76 USB_DIR_OUT | USB_TYPE_VENDOR |
78 value, index, NULL, 0, 1000);
80 status = min(status, 0);
83 pr_err("%s() Failed sending control message, error %d.\n",
92 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
93 u16 index, unsigned char *cp, u16 size)
96 mutex_lock(&dev->mutex);
98 status = usb_control_msg(dev->usbdev,
99 usb_rcvctrlpipe(dev->usbdev, 0),
101 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
103 dev->ctrlmsg, size, 1000);
105 status = min(status, 0);
108 pr_err("%s() Failed receiving control message, error %d.\n",
112 /* the host controller requires heap allocated memory, which
113 is why we didn't just pass "cp" into usb_control_msg */
114 memcpy(cp, dev->ctrlmsg, size);
116 mutex_unlock(&dev->mutex);
120 #ifdef CONFIG_MEDIA_CONTROLLER
121 static void au0828_media_graph_notify(struct media_entity *new,
125 static void au0828_unregister_media_device(struct au0828_dev *dev)
127 #ifdef CONFIG_MEDIA_CONTROLLER
128 struct media_device *mdev = dev->media_dev;
129 struct media_entity_notify *notify, *nextp;
131 if (!mdev || !media_devnode_is_registered(mdev->devnode))
134 /* Remove au0828 entity_notify callbacks */
135 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) {
136 if (notify->notify != au0828_media_graph_notify)
138 media_device_unregister_entity_notify(mdev, notify);
141 /* clear enable_source, disable_source */
142 mutex_lock(&mdev->graph_mutex);
143 dev->media_dev->source_priv = NULL;
144 dev->media_dev->enable_source = NULL;
145 dev->media_dev->disable_source = NULL;
146 mutex_unlock(&mdev->graph_mutex);
148 media_device_delete(dev->media_dev, KBUILD_MODNAME, THIS_MODULE);
149 dev->media_dev = NULL;
153 void au0828_usb_release(struct au0828_dev *dev)
155 au0828_unregister_media_device(dev);
158 au0828_i2c_unregister(dev);
163 static void au0828_usb_disconnect(struct usb_interface *interface)
165 struct au0828_dev *dev = usb_get_intfdata(interface);
167 dprintk(1, "%s()\n", __func__);
169 /* there is a small window after disconnect, before
170 dev->usbdev is NULL, for poll (e.g: IR) try to access
171 the device and fill the dmesg with error messages.
172 Set the status so poll routines can check and avoid
173 access after disconnect.
175 set_bit(DEV_DISCONNECTED, &dev->dev_state);
177 au0828_rc_unregister(dev);
179 au0828_dvb_unregister(dev);
181 usb_set_intfdata(interface, NULL);
182 mutex_lock(&dev->mutex);
184 mutex_unlock(&dev->mutex);
185 if (au0828_analog_unregister(dev)) {
187 * No need to call au0828_usb_release() if V4L2 is enabled,
188 * as this is already called via au0828_usb_v4l2_release()
192 au0828_usb_release(dev);
195 static int au0828_media_device_init(struct au0828_dev *dev,
196 struct usb_device *udev)
198 #ifdef CONFIG_MEDIA_CONTROLLER
199 struct media_device *mdev;
201 mdev = media_device_usb_allocate(udev, KBUILD_MODNAME, THIS_MODULE);
203 return PTR_ERR(mdev);
205 dev->media_dev = mdev;
210 #ifdef CONFIG_MEDIA_CONTROLLER
211 static void au0828_media_graph_notify(struct media_entity *new,
214 struct au0828_dev *dev = notify_data;
216 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
220 * Called during au0828 probe time to connect
221 * entities that were created prior to registering
222 * the notify handler. Find mixer and decoder.
224 media_device_for_each_entity(entity, dev->media_dev) {
225 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
227 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
233 switch (new->function) {
234 case MEDIA_ENT_F_AUDIO_MIXER:
237 decoder = dev->decoder;
239 case MEDIA_ENT_F_ATV_DECODER:
240 /* In case, Mixer is added first, find mixer and create link */
241 media_device_for_each_entity(entity, dev->media_dev) {
242 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
252 if (decoder && mixer) {
253 ret = media_get_pad_index(decoder, MEDIA_PAD_FL_SOURCE,
256 ret = media_create_pad_link(decoder, ret,
258 MEDIA_LNK_FL_ENABLED);
260 dev_err(&dev->usbdev->dev,
261 "Mixer Pad Link Create Error: %d\n", ret);
265 static bool au0828_is_link_shareable(struct media_entity *owner,
266 struct media_entity *entity)
268 bool shareable = false;
270 /* Tuner link can be shared by audio, video, and VBI */
271 switch (owner->function) {
272 case MEDIA_ENT_F_IO_V4L:
273 case MEDIA_ENT_F_AUDIO_CAPTURE:
274 case MEDIA_ENT_F_IO_VBI:
275 if (entity->function == MEDIA_ENT_F_IO_V4L ||
276 entity->function == MEDIA_ENT_F_AUDIO_CAPTURE ||
277 entity->function == MEDIA_ENT_F_IO_VBI)
280 case MEDIA_ENT_F_DTV_DEMOD:
287 /* Callers should hold graph_mutex */
288 static int au0828_enable_source(struct media_entity *entity,
289 struct media_pipeline *pipe)
291 struct media_entity *source, *find_source;
292 struct media_entity *sink;
293 struct media_link *link, *found_link = NULL;
295 struct media_device *mdev = entity->graph_obj.mdev;
296 struct au0828_dev *dev;
301 dev = mdev->source_priv;
304 * For Audio and V4L2 entity, find the link to which decoder
305 * is the sink. Look for an active link between decoder and
306 * source (tuner/s-video/Composite), if one exists, nothing
307 * to do. If not, look for any active links between source
308 * and any other entity. If one exists, source is busy. If
309 * source is free, setup link and start pipeline from source.
310 * For DVB FE entity, the source for the link is the tuner.
311 * Check if tuner is available and setup link and start
314 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
316 find_source = dev->tuner;
318 /* Analog isn't configured or register failed */
327 * Default input is tuner and default input_type
328 * is AU0828_VMUX_TELEVISION.
330 * There is a problem when s_input is called to
331 * change the default input. s_input will try to
332 * enable_source before attempting to change the
333 * input on the device, and will end up enabling
334 * default source which is tuner.
336 * Additional logic is necessary in au0828 to detect
337 * that the input has changed and enable the right
338 * source. au0828 handles this case in its s_input.
339 * It will disable the old source and enable the new
343 if (dev->input_type == AU0828_VMUX_TELEVISION)
344 find_source = dev->tuner;
345 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
346 dev->input_type == AU0828_VMUX_COMPOSITE)
347 find_source = &dev->input_ent[dev->input_type];
349 /* unknown input - let user select input */
355 /* Is there an active link between sink and source */
356 if (dev->active_link) {
357 if (dev->active_link_owner == entity) {
358 /* This check is necessary to handle multiple
359 * enable_source calls from v4l_ioctls during
360 * the course of video/vbi application run-time.
362 pr_debug("%s already owns the tuner\n", entity->name);
365 } else if (au0828_is_link_shareable(dev->active_link_owner,
367 /* Either ALSA or Video own tuner. Sink is the same
368 * for both. Allow sharing the active link between
369 * their common source (tuner) and sink (decoder).
370 * Starting pipeline between sharing entity and sink
371 * will fail with pipe mismatch, while owner has an
372 * active pipeline. Switch pipeline ownership from
373 * user to owner when owner disables the source.
375 dev->active_link_shared = true;
376 /* save the user info to use from disable */
377 dev->active_link_user = entity;
378 dev->active_link_user_pipe = pipe;
379 pr_debug("%s owns the tuner %s can share!\n",
380 dev->active_link_owner->name,
390 list_for_each_entry(link, &sink->links, list) {
391 /* Check sink, and source */
392 if (link->sink->entity == sink &&
393 link->source->entity == find_source) {
404 /* activate link between source and sink and start pipeline */
405 source = found_link->source->entity;
406 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
408 pr_err("Activate link from %s->%s. Error %d\n",
409 source->name, sink->name, ret);
413 ret = __media_pipeline_start(entity->pads, pipe);
415 pr_err("Start Pipeline: %s->%s Error %d\n",
416 source->name, entity->name, ret);
417 ret = __media_entity_setup_link(found_link, 0);
419 pr_err("Deactivate link Error %d\n", ret);
423 /* save link state to allow audio and video share the link
424 * and not disable the link while the other is using it.
425 * active_link_owner is used to deactivate the link.
427 dev->active_link = found_link;
428 dev->active_link_owner = entity;
429 dev->active_source = source;
430 dev->active_sink = sink;
432 pr_info("Enabled Source: %s->%s->%s Ret %d\n",
433 dev->active_source->name, dev->active_sink->name,
434 dev->active_link_owner->name, ret);
436 pr_debug("%s end: ent:%s fnc:%d ret %d\n",
437 __func__, entity->name, entity->function, ret);
441 /* Callers should hold graph_mutex */
442 static void au0828_disable_source(struct media_entity *entity)
445 struct media_device *mdev = entity->graph_obj.mdev;
446 struct au0828_dev *dev;
451 dev = mdev->source_priv;
453 if (!dev->active_link)
456 /* link is active - stop pipeline from source
457 * (tuner/s-video/Composite) to the entity
458 * When DVB/s-video/Composite owns tuner, it won't be in
461 if (dev->active_link->sink->entity == dev->active_sink &&
462 dev->active_link->source->entity == dev->active_source) {
464 * Prevent video from deactivating link when audio
465 * has active pipeline and vice versa. In addition
466 * handle the case when more than one video/vbi
467 * application is sharing the link.
469 bool owner_is_audio = false;
471 if (dev->active_link_owner->function ==
472 MEDIA_ENT_F_AUDIO_CAPTURE)
473 owner_is_audio = true;
475 if (dev->active_link_shared) {
476 pr_debug("Shared link owner %s user %s %d\n",
477 dev->active_link_owner->name,
478 entity->name, dev->users);
480 /* Handle video device users > 1
481 * When audio owns the shared link with
482 * more than one video users, avoid
483 * disabling the source and/or switching
484 * the owner until the last disable_source
485 * call from video _close(). Use dev->users to
486 * determine when to switch/disable.
488 if (dev->active_link_owner != entity) {
489 /* video device has users > 1 */
490 if (owner_is_audio && dev->users > 1)
493 dev->active_link_user = NULL;
494 dev->active_link_user_pipe = NULL;
495 dev->active_link_shared = false;
499 /* video owns the link and has users > 1 */
500 if (!owner_is_audio && dev->users > 1)
504 __media_pipeline_stop(dev->active_link_owner->pads);
505 pr_debug("Pipeline stop for %s\n",
506 dev->active_link_owner->name);
508 ret = __media_pipeline_start(
509 dev->active_link_user->pads,
510 dev->active_link_user_pipe);
512 pr_err("Start Pipeline: %s->%s %d\n",
513 dev->active_source->name,
514 dev->active_link_user->name,
516 goto deactivate_link;
518 /* link user is now the owner */
519 dev->active_link_owner = dev->active_link_user;
520 dev->active_link_user = NULL;
521 dev->active_link_user_pipe = NULL;
522 dev->active_link_shared = false;
524 pr_debug("Pipeline started for %s\n",
525 dev->active_link_owner->name);
527 } else if (!owner_is_audio && dev->users > 1)
528 /* video/vbi owns the link and has users > 1 */
531 if (dev->active_link_owner != entity)
535 __media_pipeline_stop(dev->active_link_owner->pads);
536 pr_debug("Pipeline stop for %s\n",
537 dev->active_link_owner->name);
540 ret = __media_entity_setup_link(dev->active_link, 0);
542 pr_err("Deactivate link Error %d\n", ret);
544 pr_info("Disabled Source: %s->%s->%s Ret %d\n",
545 dev->active_source->name, dev->active_sink->name,
546 dev->active_link_owner->name, ret);
548 dev->active_link = NULL;
549 dev->active_link_owner = NULL;
550 dev->active_source = NULL;
551 dev->active_sink = NULL;
552 dev->active_link_shared = false;
553 dev->active_link_user = NULL;
558 static int au0828_media_device_register(struct au0828_dev *dev,
559 struct usb_device *udev)
561 #ifdef CONFIG_MEDIA_CONTROLLER
563 struct media_entity *entity, *demod = NULL;
564 struct media_link *link;
569 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
571 /* register media device */
572 ret = media_device_register(dev->media_dev);
574 media_device_delete(dev->media_dev, KBUILD_MODNAME,
576 dev->media_dev = NULL;
578 "Media Device Register Error: %d\n", ret);
583 * Call au0828_media_graph_notify() to connect
584 * audio graph to our graph. In this case, audio
585 * driver registered the device and there is no
586 * entity_notify to be called when new entities
587 * are added. Invoke it now.
589 au0828_media_graph_notify(NULL, (void *) dev);
593 * Find tuner, decoder and demod.
595 * The tuner and decoder should be cached, as they'll be used by
596 * au0828_enable_source.
598 * It also needs to disable the link between tuner and
599 * decoder/demod, to avoid disable step when tuner is requested
600 * by video or audio. Note that this step can't be done until dvb
601 * graph is created during dvb register.
603 media_device_for_each_entity(entity, dev->media_dev) {
604 switch (entity->function) {
605 case MEDIA_ENT_F_TUNER:
608 case MEDIA_ENT_F_ATV_DECODER:
609 dev->decoder = entity;
611 case MEDIA_ENT_F_DTV_DEMOD:
617 /* Disable link between tuner->demod and/or tuner->decoder */
619 list_for_each_entry(link, &dev->tuner->links, list) {
620 if (demod && link->sink->entity == demod)
621 media_entity_setup_link(link, 0);
622 if (dev->decoder && link->sink->entity == dev->decoder)
623 media_entity_setup_link(link, 0);
627 /* register entity_notify callback */
628 dev->entity_notify.notify_data = (void *) dev;
629 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
630 media_device_register_entity_notify(dev->media_dev,
631 &dev->entity_notify);
633 /* set enable_source */
634 mutex_lock(&dev->media_dev->graph_mutex);
635 dev->media_dev->source_priv = (void *) dev;
636 dev->media_dev->enable_source = au0828_enable_source;
637 dev->media_dev->disable_source = au0828_disable_source;
638 mutex_unlock(&dev->media_dev->graph_mutex);
643 static int au0828_usb_probe(struct usb_interface *interface,
644 const struct usb_device_id *id)
649 struct au0828_dev *dev;
650 struct usb_device *usbdev = interface_to_usbdev(interface);
652 ifnum = interface->altsetting->desc.bInterfaceNumber;
657 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
658 le16_to_cpu(usbdev->descriptor.idVendor),
659 le16_to_cpu(usbdev->descriptor.idProduct),
663 * Make sure we have 480 Mbps of bandwidth, otherwise things like
664 * video stream wouldn't likely work, since 12 Mbps is generally
665 * not enough even for most Digital TV streams.
667 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
668 pr_err("au0828: Device initialization failed.\n");
669 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
673 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
675 pr_err("%s() Unable to allocate memory\n", __func__);
679 mutex_init(&dev->lock);
680 mutex_lock(&dev->lock);
681 mutex_init(&dev->mutex);
682 mutex_init(&dev->dvb.lock);
683 dev->usbdev = usbdev;
684 dev->boardnr = id->driver_info;
685 dev->board = au0828_boards[dev->boardnr];
687 /* Initialize the media controller */
688 retval = au0828_media_device_init(dev, usbdev);
690 pr_err("%s() au0828_media_device_init failed\n",
692 mutex_unlock(&dev->lock);
697 retval = au0828_v4l2_device_register(interface, dev);
699 au0828_usb_v4l2_media_release(dev);
700 mutex_unlock(&dev->lock);
705 /* Power Up the bridge */
706 au0828_write(dev, REG_600, 1 << 4);
708 /* Bring up the GPIO's and supporting devices */
709 au0828_gpio_setup(dev);
712 au0828_i2c_register(dev);
715 au0828_card_setup(dev);
718 * Store the pointer to the au0828_dev so it can be accessed in
719 * au0828_usb_disconnect
721 usb_set_intfdata(interface, dev);
724 retval = au0828_analog_register(dev, interface);
726 pr_err("%s() au0828_analog_register failed to register on V4L2\n",
728 mutex_unlock(&dev->lock);
733 retval = au0828_dvb_register(dev);
735 pr_err("%s() au0828_dvb_register failed\n",
738 /* Remote controller */
739 au0828_rc_register(dev);
741 pr_info("Registered device AU0828 [%s]\n",
742 dev->board.name == NULL ? "Unset" : dev->board.name);
744 mutex_unlock(&dev->lock);
746 retval = au0828_media_device_register(dev, usbdev);
750 au0828_usb_disconnect(interface);
755 static int au0828_suspend(struct usb_interface *interface,
756 pm_message_t message)
758 struct au0828_dev *dev = usb_get_intfdata(interface);
763 pr_info("Suspend\n");
765 au0828_rc_suspend(dev);
766 au0828_v4l2_suspend(dev);
767 au0828_dvb_suspend(dev);
769 /* FIXME: should suspend also ATV/DTV */
774 static int au0828_resume(struct usb_interface *interface)
776 struct au0828_dev *dev = usb_get_intfdata(interface);
782 /* Power Up the bridge */
783 au0828_write(dev, REG_600, 1 << 4);
785 /* Bring up the GPIO's and supporting devices */
786 au0828_gpio_setup(dev);
788 au0828_rc_resume(dev);
789 au0828_v4l2_resume(dev);
790 au0828_dvb_resume(dev);
792 /* FIXME: should resume also ATV/DTV */
797 static struct usb_driver au0828_usb_driver = {
798 .name = KBUILD_MODNAME,
799 .probe = au0828_usb_probe,
800 .disconnect = au0828_usb_disconnect,
801 .id_table = au0828_usb_id_table,
802 .suspend = au0828_suspend,
803 .resume = au0828_resume,
804 .reset_resume = au0828_resume,
807 static int __init au0828_init(void)
811 if (au0828_debug & 1)
812 pr_info("%s() Debugging is enabled\n", __func__);
814 if (au0828_debug & 2)
815 pr_info("%s() USB Debugging is enabled\n", __func__);
817 if (au0828_debug & 4)
818 pr_info("%s() I2C Debugging is enabled\n", __func__);
820 if (au0828_debug & 8)
821 pr_info("%s() Bridge Debugging is enabled\n",
824 if (au0828_debug & 16)
825 pr_info("%s() IR Debugging is enabled\n",
828 pr_info("au0828 driver loaded\n");
830 ret = usb_register(&au0828_usb_driver);
832 pr_err("usb_register failed, error = %d\n", ret);
837 static void __exit au0828_exit(void)
839 usb_deregister(&au0828_usb_driver);
842 module_init(au0828_init);
843 module_exit(au0828_exit);
845 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
847 MODULE_LICENSE("GPL");
848 MODULE_VERSION("0.0.3");