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_unregister(dev
->media_dev
);
159 media_device_cleanup(dev
->media_dev
);
160 kfree(dev
->media_dev
);
161 dev
->media_dev
= NULL
;
165 void au0828_usb_release(struct au0828_dev
*dev
)
167 au0828_unregister_media_device(dev
);
170 au0828_i2c_unregister(dev
);
175 static void au0828_usb_disconnect(struct usb_interface
*interface
)
177 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
179 dprintk(1, "%s()\n", __func__
);
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.
187 set_bit(DEV_DISCONNECTED
, &dev
->dev_state
);
189 au0828_rc_unregister(dev
);
191 au0828_dvb_unregister(dev
);
193 usb_set_intfdata(interface
, NULL
);
194 mutex_lock(&dev
->mutex
);
196 mutex_unlock(&dev
->mutex
);
197 if (au0828_analog_unregister(dev
)) {
199 * No need to call au0828_usb_release() if V4L2 is enabled,
200 * as this is already called via au0828_usb_v4l2_release()
204 au0828_usb_release(dev
);
207 static int au0828_media_device_init(struct au0828_dev
*dev
,
208 struct usb_device
*udev
)
210 #ifdef CONFIG_MEDIA_CONTROLLER
211 struct media_device
*mdev
;
213 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
217 /* check if media device is already initialized */
219 media_device_usb_init(mdev
, udev
, udev
->product
);
221 dev
->media_dev
= mdev
;
226 #ifdef CONFIG_MEDIA_CONTROLLER
227 static void au0828_media_graph_notify(struct media_entity
*new,
230 struct au0828_dev
*dev
= (struct au0828_dev
*) notify_data
;
232 struct media_entity
*entity
, *mixer
= NULL
, *decoder
= NULL
;
236 * Called during au0828 probe time to connect
237 * entites that were created prior to registering
238 * the notify handler. Find mixer and decoder.
240 media_device_for_each_entity(entity
, dev
->media_dev
) {
241 if (entity
->function
== MEDIA_ENT_F_AUDIO_MIXER
)
243 else if (entity
->function
== MEDIA_ENT_F_ATV_DECODER
)
249 switch (new->function
) {
250 case MEDIA_ENT_F_AUDIO_MIXER
:
253 decoder
= dev
->decoder
;
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
)
268 if (decoder
&& mixer
) {
269 ret
= media_create_pad_link(decoder
,
272 MEDIA_LNK_FL_ENABLED
);
274 dev_err(&dev
->usbdev
->dev
,
275 "Mixer Pad Link Create Error: %d\n", ret
);
279 /* Callers should hold graph_mutex */
280 static int au0828_enable_source(struct media_entity
*entity
,
281 struct media_pipeline
*pipe
)
283 struct media_entity
*source
, *find_source
;
284 struct media_entity
*sink
;
285 struct media_link
*link
, *found_link
= NULL
;
287 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
288 struct au0828_dev
*dev
;
293 dev
= mdev
->source_priv
;
296 * For Audio and V4L2 entity, find the link to which decoder
297 * is the sink. Look for an active link between decoder and
298 * source (tuner/s-video/Composite), if one exists, nothing
299 * to do. If not, look for any active links between source
300 * and any other entity. If one exists, source is busy. If
301 * source is free, setup link and start pipeline from source.
302 * For DVB FE entity, the source for the link is the tuner.
303 * Check if tuner is available and setup link and start
306 if (entity
->function
== MEDIA_ENT_F_DTV_DEMOD
) {
308 find_source
= dev
->tuner
;
310 /* Analog isn't configured or register failed */
319 * Default input is tuner and default input_type
320 * is AU0828_VMUX_TELEVISION.
322 * There is a problem when s_input is called to
323 * change the default input. s_input will try to
324 * enable_source before attempting to change the
325 * input on the device, and will end up enabling
326 * default source which is tuner.
328 * Additional logic is necessary in au0828
329 * to detect that the input has changed and
330 * enable the right source.
333 if (dev
->input_type
== AU0828_VMUX_TELEVISION
)
334 find_source
= dev
->tuner
;
335 else if (dev
->input_type
== AU0828_VMUX_SVIDEO
||
336 dev
->input_type
== AU0828_VMUX_COMPOSITE
)
337 find_source
= &dev
->input_ent
[dev
->input_type
];
339 /* unknown input - let user select input */
345 /* Is an active link between sink and source */
346 if (dev
->active_link
) {
348 * If DVB is using the tuner and calling entity is
349 * audio/video, the following check will be false,
350 * since sink is different. Result is Busy.
352 if (dev
->active_link
->sink
->entity
== sink
&&
353 dev
->active_link
->source
->entity
== find_source
) {
355 * Either ALSA or Video own tuner. sink is
356 * the same for both. Prevent Video stepping
357 * on ALSA when ALSA owns the source.
359 if (dev
->active_link_owner
!= entity
&&
360 dev
->active_link_owner
->function
==
361 MEDIA_ENT_F_AUDIO_CAPTURE
) {
362 pr_debug("ALSA has the tuner\n");
374 list_for_each_entry(link
, &sink
->links
, list
) {
375 /* Check sink, and source */
376 if (link
->sink
->entity
== sink
&&
377 link
->source
->entity
== find_source
) {
388 /* activate link between source and sink and start pipeline */
389 source
= found_link
->source
->entity
;
390 ret
= __media_entity_setup_link(found_link
, MEDIA_LNK_FL_ENABLED
);
392 pr_err("Activate tuner link %s->%s. Error %d\n",
393 source
->name
, sink
->name
, ret
);
397 ret
= __media_pipeline_start(entity
, pipe
);
399 pr_err("Start Pipeline: %s->%s Error %d\n",
400 source
->name
, entity
->name
, ret
);
401 ret
= __media_entity_setup_link(found_link
, 0);
402 pr_err("Deactivate link Error %d\n", ret
);
406 * save active link and active link owner to avoid audio
407 * deactivating video owned link from disable_source and
410 dev
->active_link
= found_link
;
411 dev
->active_link_owner
= entity
;
412 dev
->active_source
= source
;
413 dev
->active_sink
= sink
;
415 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
416 dev
->active_source
->name
, dev
->active_sink
->name
,
417 dev
->active_link_owner
->name
, ret
);
419 pr_debug("au0828_enable_source() end %s %d %d\n",
420 entity
->name
, entity
->function
, ret
);
424 /* Callers should hold graph_mutex */
425 static void au0828_disable_source(struct media_entity
*entity
)
428 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
429 struct au0828_dev
*dev
;
434 dev
= mdev
->source_priv
;
436 if (!dev
->active_link
)
439 /* link is active - stop pipeline from source (tuner) */
440 if (dev
->active_link
->sink
->entity
== dev
->active_sink
&&
441 dev
->active_link
->source
->entity
== dev
->active_source
) {
443 * prevent video from deactivating link when audio
444 * has active pipeline
446 if (dev
->active_link_owner
!= entity
)
448 __media_pipeline_stop(entity
);
449 ret
= __media_entity_setup_link(dev
->active_link
, 0);
451 pr_err("Deactivate link Error %d\n", ret
);
453 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
454 dev
->active_source
->name
, dev
->active_sink
->name
,
455 dev
->active_link_owner
->name
, ret
);
457 dev
->active_link
= NULL
;
458 dev
->active_link_owner
= NULL
;
459 dev
->active_source
= NULL
;
460 dev
->active_sink
= NULL
;
465 static int au0828_media_device_register(struct au0828_dev
*dev
,
466 struct usb_device
*udev
)
468 #ifdef CONFIG_MEDIA_CONTROLLER
470 struct media_entity
*entity
, *demod
= NULL
;
471 struct media_link
*link
;
476 if (!media_devnode_is_registered(dev
->media_dev
->devnode
)) {
478 /* register media device */
479 ret
= media_device_register(dev
->media_dev
);
482 "Media Device Register Error: %d\n", ret
);
487 * Call au0828_media_graph_notify() to connect
488 * audio graph to our graph. In this case, audio
489 * driver registered the device and there is no
490 * entity_notify to be called when new entities
491 * are added. Invoke it now.
493 au0828_media_graph_notify(NULL
, (void *) dev
);
497 * Find tuner, decoder and demod.
499 * The tuner and decoder should be cached, as they'll be used by
500 * au0828_enable_source.
502 * It also needs to disable the link between tuner and
503 * decoder/demod, to avoid disable step when tuner is requested
504 * by video or audio. Note that this step can't be done until dvb
505 * graph is created during dvb register.
507 media_device_for_each_entity(entity
, dev
->media_dev
) {
508 switch (entity
->function
) {
509 case MEDIA_ENT_F_TUNER
:
512 case MEDIA_ENT_F_ATV_DECODER
:
513 dev
->decoder
= entity
;
515 case MEDIA_ENT_F_DTV_DEMOD
:
521 /* Disable link between tuner->demod and/or tuner->decoder */
523 list_for_each_entry(link
, &dev
->tuner
->links
, list
) {
524 if (demod
&& link
->sink
->entity
== demod
)
525 media_entity_setup_link(link
, 0);
526 if (dev
->decoder
&& link
->sink
->entity
== dev
->decoder
)
527 media_entity_setup_link(link
, 0);
531 /* register entity_notify callback */
532 dev
->entity_notify
.notify_data
= (void *) dev
;
533 dev
->entity_notify
.notify
= (void *) au0828_media_graph_notify
;
534 ret
= media_device_register_entity_notify(dev
->media_dev
,
535 &dev
->entity_notify
);
538 "Media Device register entity_notify Error: %d\n",
542 /* set enable_source */
543 mutex_lock(&dev
->media_dev
->graph_mutex
);
544 dev
->media_dev
->source_priv
= (void *) dev
;
545 dev
->media_dev
->enable_source
= au0828_enable_source
;
546 dev
->media_dev
->disable_source
= au0828_disable_source
;
547 mutex_unlock(&dev
->media_dev
->graph_mutex
);
552 static int au0828_usb_probe(struct usb_interface
*interface
,
553 const struct usb_device_id
*id
)
558 struct au0828_dev
*dev
;
559 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
561 ifnum
= interface
->altsetting
->desc
.bInterfaceNumber
;
566 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__
,
567 le16_to_cpu(usbdev
->descriptor
.idVendor
),
568 le16_to_cpu(usbdev
->descriptor
.idProduct
),
572 * Make sure we have 480 Mbps of bandwidth, otherwise things like
573 * video stream wouldn't likely work, since 12 Mbps is generally
574 * not enough even for most Digital TV streams.
576 if (usbdev
->speed
!= USB_SPEED_HIGH
&& disable_usb_speed_check
== 0) {
577 pr_err("au0828: Device initialization failed.\n");
578 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
582 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
584 pr_err("%s() Unable to allocate memory\n", __func__
);
588 mutex_init(&dev
->lock
);
589 mutex_lock(&dev
->lock
);
590 mutex_init(&dev
->mutex
);
591 mutex_init(&dev
->dvb
.lock
);
592 dev
->usbdev
= usbdev
;
593 dev
->boardnr
= id
->driver_info
;
594 dev
->board
= au0828_boards
[dev
->boardnr
];
596 /* Initialize the media controller */
597 retval
= au0828_media_device_init(dev
, usbdev
);
599 pr_err("%s() au0828_media_device_init failed\n",
601 mutex_unlock(&dev
->lock
);
606 retval
= au0828_v4l2_device_register(interface
, dev
);
608 au0828_usb_v4l2_media_release(dev
);
609 mutex_unlock(&dev
->lock
);
614 /* Power Up the bridge */
615 au0828_write(dev
, REG_600
, 1 << 4);
617 /* Bring up the GPIO's and supporting devices */
618 au0828_gpio_setup(dev
);
621 au0828_i2c_register(dev
);
624 au0828_card_setup(dev
);
627 retval
= au0828_analog_register(dev
, interface
);
629 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
631 mutex_unlock(&dev
->lock
);
637 retval
= au0828_dvb_register(dev
);
639 pr_err("%s() au0282_dev_register failed\n",
642 /* Remote controller */
643 au0828_rc_register(dev
);
646 * Store the pointer to the au0828_dev so it can be accessed in
647 * au0828_usb_disconnect
649 usb_set_intfdata(interface
, dev
);
651 pr_info("Registered device AU0828 [%s]\n",
652 dev
->board
.name
== NULL
? "Unset" : dev
->board
.name
);
654 mutex_unlock(&dev
->lock
);
656 retval
= au0828_media_device_register(dev
, usbdev
);
660 au0828_usb_disconnect(interface
);
665 static int au0828_suspend(struct usb_interface
*interface
,
666 pm_message_t message
)
668 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
673 pr_info("Suspend\n");
675 au0828_rc_suspend(dev
);
676 au0828_v4l2_suspend(dev
);
677 au0828_dvb_suspend(dev
);
679 /* FIXME: should suspend also ATV/DTV */
684 static int au0828_resume(struct usb_interface
*interface
)
686 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
692 /* Power Up the bridge */
693 au0828_write(dev
, REG_600
, 1 << 4);
695 /* Bring up the GPIO's and supporting devices */
696 au0828_gpio_setup(dev
);
698 au0828_rc_resume(dev
);
699 au0828_v4l2_resume(dev
);
700 au0828_dvb_resume(dev
);
702 /* FIXME: should resume also ATV/DTV */
707 static struct usb_driver au0828_usb_driver
= {
708 .name
= KBUILD_MODNAME
,
709 .probe
= au0828_usb_probe
,
710 .disconnect
= au0828_usb_disconnect
,
711 .id_table
= au0828_usb_id_table
,
712 .suspend
= au0828_suspend
,
713 .resume
= au0828_resume
,
714 .reset_resume
= au0828_resume
,
717 static int __init
au0828_init(void)
721 if (au0828_debug
& 1)
722 pr_info("%s() Debugging is enabled\n", __func__
);
724 if (au0828_debug
& 2)
725 pr_info("%s() USB Debugging is enabled\n", __func__
);
727 if (au0828_debug
& 4)
728 pr_info("%s() I2C Debugging is enabled\n", __func__
);
730 if (au0828_debug
& 8)
731 pr_info("%s() Bridge Debugging is enabled\n",
734 if (au0828_debug
& 16)
735 pr_info("%s() IR Debugging is enabled\n",
738 pr_info("au0828 driver loaded\n");
740 ret
= usb_register(&au0828_usb_driver
);
742 pr_err("usb_register failed, error = %d\n", ret
);
747 static void __exit
au0828_exit(void)
749 usb_deregister(&au0828_usb_driver
);
752 module_init(au0828_init
);
753 module_exit(au0828_exit
);
755 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
756 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
757 MODULE_LICENSE("GPL");
758 MODULE_VERSION("0.0.3");