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.
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.
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>
31 /* Due to enum tuner_pad_index */
32 #include <media/tuner.h>
35 * 1 = General debug messages
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");
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");
51 #define _AU0828_BULKPIPE 0x03
52 #define _BULKPIPESIZE 0xffff
54 static int send_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
56 static int recv_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
57 u16 index
, unsigned char *cp
, u16 size
);
60 #define CMD_REQUEST_IN 0x00
61 #define CMD_REQUEST_OUT 0x01
63 u32
au0828_readreg(struct au0828_dev
*dev
, u16 reg
)
67 recv_control_msg(dev
, CMD_REQUEST_IN
, 0, reg
, &result
, 1);
68 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__
, reg
, result
);
73 u32
au0828_writereg(struct au0828_dev
*dev
, u16 reg
, u32 val
)
75 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__
, reg
, val
);
76 return send_control_msg(dev
, CMD_REQUEST_OUT
, val
, reg
);
79 static int send_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
86 /* cp must be memory that has been allocated by kmalloc */
87 status
= usb_control_msg(dev
->usbdev
,
88 usb_sndctrlpipe(dev
->usbdev
, 0),
90 USB_DIR_OUT
| USB_TYPE_VENDOR
|
92 value
, index
, NULL
, 0, 1000);
94 status
= min(status
, 0);
97 pr_err("%s() Failed sending control message, error %d.\n",
106 static int recv_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
107 u16 index
, unsigned char *cp
, u16 size
)
109 int status
= -ENODEV
;
110 mutex_lock(&dev
->mutex
);
112 status
= usb_control_msg(dev
->usbdev
,
113 usb_rcvctrlpipe(dev
->usbdev
, 0),
115 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
117 dev
->ctrlmsg
, size
, 1000);
119 status
= min(status
, 0);
122 pr_err("%s() Failed receiving control message, error %d.\n",
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
);
130 mutex_unlock(&dev
->mutex
);
134 static void au0828_unregister_media_device(struct au0828_dev
*dev
)
137 #ifdef CONFIG_MEDIA_CONTROLLER
138 if (dev
->media_dev
&&
139 media_devnode_is_registered(&dev
->media_dev
->devnode
)) {
140 /* clear enable_source, disable_source */
141 dev
->media_dev
->source_priv
= NULL
;
142 dev
->media_dev
->enable_source
= NULL
;
143 dev
->media_dev
->disable_source
= NULL
;
145 media_device_unregister(dev
->media_dev
);
146 media_device_cleanup(dev
->media_dev
);
147 kfree(dev
->media_dev
);
148 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
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
205 /* check if media device is already initialized */
207 media_device_usb_init(mdev
, udev
, udev
->product
);
209 dev
->media_dev
= mdev
;
214 #ifdef CONFIG_MEDIA_CONTROLLER
215 static void au0828_media_graph_notify(struct media_entity
*new,
218 struct au0828_dev
*dev
= (struct au0828_dev
*) notify_data
;
220 struct media_entity
*entity
, *mixer
= NULL
, *decoder
= NULL
;
224 * Called during au0828 probe time to connect
225 * entites that were created prior to registering
226 * the notify handler. Find mixer and decoder.
228 media_device_for_each_entity(entity
, dev
->media_dev
) {
229 if (entity
->function
== MEDIA_ENT_F_AUDIO_MIXER
)
231 else if (entity
->function
== MEDIA_ENT_F_ATV_DECODER
)
237 switch (new->function
) {
238 case MEDIA_ENT_F_AUDIO_MIXER
:
241 decoder
= dev
->decoder
;
243 case MEDIA_ENT_F_ATV_DECODER
:
244 /* In case, Mixer is added first, find mixer and create link */
245 media_device_for_each_entity(entity
, dev
->media_dev
) {
246 if (entity
->function
== MEDIA_ENT_F_AUDIO_MIXER
)
256 if (decoder
&& mixer
) {
257 ret
= media_create_pad_link(decoder
,
260 MEDIA_LNK_FL_ENABLED
);
262 dev_err(&dev
->usbdev
->dev
,
263 "Mixer Pad Link Create Error: %d\n", ret
);
267 static int au0828_enable_source(struct media_entity
*entity
,
268 struct media_pipeline
*pipe
)
270 struct media_entity
*source
, *find_source
;
271 struct media_entity
*sink
;
272 struct media_link
*link
, *found_link
= NULL
;
274 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
275 struct au0828_dev
*dev
;
280 mutex_lock(&mdev
->graph_mutex
);
282 dev
= mdev
->source_priv
;
285 * For Audio and V4L2 entity, find the link to which decoder
286 * is the sink. Look for an active link between decoder and
287 * source (tuner/s-video/Composite), if one exists, nothing
288 * to do. If not, look for any active links between source
289 * and any other entity. If one exists, source is busy. If
290 * source is free, setup link and start pipeline from source.
291 * For DVB FE entity, the source for the link is the tuner.
292 * Check if tuner is available and setup link and start
295 if (entity
->function
== MEDIA_ENT_F_DTV_DEMOD
) {
297 find_source
= dev
->tuner
;
299 /* Analog isn't configured or register failed */
308 * Default input is tuner and default input_type
309 * is AU0828_VMUX_TELEVISION.
311 * There is a problem when s_input is called to
312 * change the default input. s_input will try to
313 * enable_source before attempting to change the
314 * input on the device, and will end up enabling
315 * default source which is tuner.
317 * Additional logic is necessary in au0828
318 * to detect that the input has changed and
319 * enable the right source.
322 if (dev
->input_type
== AU0828_VMUX_TELEVISION
)
323 find_source
= dev
->tuner
;
324 else if (dev
->input_type
== AU0828_VMUX_SVIDEO
||
325 dev
->input_type
== AU0828_VMUX_COMPOSITE
)
326 find_source
= &dev
->input_ent
[dev
->input_type
];
328 /* unknown input - let user select input */
334 /* Is an active link between sink and source */
335 if (dev
->active_link
) {
337 * If DVB is using the tuner and calling entity is
338 * audio/video, the following check will be false,
339 * since sink is different. Result is Busy.
341 if (dev
->active_link
->sink
->entity
== sink
&&
342 dev
->active_link
->source
->entity
== find_source
) {
344 * Either ALSA or Video own tuner. sink is
345 * the same for both. Prevent Video stepping
346 * on ALSA when ALSA owns the source.
348 if (dev
->active_link_owner
!= entity
&&
349 dev
->active_link_owner
->function
==
350 MEDIA_ENT_F_AUDIO_CAPTURE
) {
351 pr_debug("ALSA has the tuner\n");
363 list_for_each_entry(link
, &sink
->links
, list
) {
364 /* Check sink, and source */
365 if (link
->sink
->entity
== sink
&&
366 link
->source
->entity
== find_source
) {
377 /* activate link between source and sink and start pipeline */
378 source
= found_link
->source
->entity
;
379 ret
= __media_entity_setup_link(found_link
, MEDIA_LNK_FL_ENABLED
);
381 pr_err("Activate tuner link %s->%s. Error %d\n",
382 source
->name
, sink
->name
, ret
);
386 ret
= __media_entity_pipeline_start(entity
, pipe
);
388 pr_err("Start Pipeline: %s->%s Error %d\n",
389 source
->name
, entity
->name
, ret
);
390 ret
= __media_entity_setup_link(found_link
, 0);
391 pr_err("Deactivate link Error %d\n", ret
);
395 * save active link and active link owner to avoid audio
396 * deactivating video owned link from disable_source and
399 dev
->active_link
= found_link
;
400 dev
->active_link_owner
= entity
;
401 dev
->active_source
= source
;
402 dev
->active_sink
= sink
;
404 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
405 dev
->active_source
->name
, dev
->active_sink
->name
,
406 dev
->active_link_owner
->name
, ret
);
408 mutex_unlock(&mdev
->graph_mutex
);
409 pr_debug("au0828_enable_source() end %s %d %d\n",
410 entity
->name
, entity
->function
, ret
);
414 static void au0828_disable_source(struct media_entity
*entity
)
417 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
418 struct au0828_dev
*dev
;
423 mutex_lock(&mdev
->graph_mutex
);
424 dev
= mdev
->source_priv
;
426 if (!dev
->active_link
) {
431 /* link is active - stop pipeline from source (tuner) */
432 if (dev
->active_link
->sink
->entity
== dev
->active_sink
&&
433 dev
->active_link
->source
->entity
== dev
->active_source
) {
435 * prevent video from deactivating link when audio
436 * has active pipeline
438 if (dev
->active_link_owner
!= entity
)
440 __media_entity_pipeline_stop(entity
);
441 ret
= __media_entity_setup_link(dev
->active_link
, 0);
443 pr_err("Deactivate link Error %d\n", ret
);
445 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
446 dev
->active_source
->name
, dev
->active_sink
->name
,
447 dev
->active_link_owner
->name
, ret
);
449 dev
->active_link
= NULL
;
450 dev
->active_link_owner
= NULL
;
451 dev
->active_source
= NULL
;
452 dev
->active_sink
= NULL
;
456 mutex_unlock(&mdev
->graph_mutex
);
460 static int au0828_media_device_register(struct au0828_dev
*dev
,
461 struct usb_device
*udev
)
463 #ifdef CONFIG_MEDIA_CONTROLLER
465 struct media_entity
*entity
, *demod
= NULL
;
466 struct media_link
*link
;
471 if (!media_devnode_is_registered(&dev
->media_dev
->devnode
)) {
473 /* register media device */
474 ret
= media_device_register(dev
->media_dev
);
477 "Media Device Register Error: %d\n", ret
);
482 * Call au0828_media_graph_notify() to connect
483 * audio graph to our graph. In this case, audio
484 * driver registered the device and there is no
485 * entity_notify to be called when new entities
486 * are added. Invoke it now.
488 au0828_media_graph_notify(NULL
, (void *) dev
);
492 * Find tuner, decoder and demod.
494 * The tuner and decoder should be cached, as they'll be used by
495 * au0828_enable_source.
497 * It also needs to disable the link between tuner and
498 * decoder/demod, to avoid disable step when tuner is requested
499 * by video or audio. Note that this step can't be done until dvb
500 * graph is created during dvb register.
502 media_device_for_each_entity(entity
, dev
->media_dev
) {
503 switch (entity
->function
) {
504 case MEDIA_ENT_F_TUNER
:
507 case MEDIA_ENT_F_ATV_DECODER
:
508 dev
->decoder
= entity
;
510 case MEDIA_ENT_F_DTV_DEMOD
:
516 /* Disable link between tuner->demod and/or tuner->decoder */
518 list_for_each_entry(link
, &dev
->tuner
->links
, list
) {
519 if (demod
&& link
->sink
->entity
== demod
)
520 media_entity_setup_link(link
, 0);
521 if (dev
->decoder
&& link
->sink
->entity
== dev
->decoder
)
522 media_entity_setup_link(link
, 0);
526 /* register entity_notify callback */
527 dev
->entity_notify
.notify_data
= (void *) dev
;
528 dev
->entity_notify
.notify
= (void *) au0828_media_graph_notify
;
529 ret
= media_device_register_entity_notify(dev
->media_dev
,
530 &dev
->entity_notify
);
533 "Media Device register entity_notify Error: %d\n",
537 /* set enable_source */
538 dev
->media_dev
->source_priv
= (void *) dev
;
539 dev
->media_dev
->enable_source
= au0828_enable_source
;
540 dev
->media_dev
->disable_source
= au0828_disable_source
;
545 static int au0828_usb_probe(struct usb_interface
*interface
,
546 const struct usb_device_id
*id
)
551 struct au0828_dev
*dev
;
552 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
554 ifnum
= interface
->altsetting
->desc
.bInterfaceNumber
;
559 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__
,
560 le16_to_cpu(usbdev
->descriptor
.idVendor
),
561 le16_to_cpu(usbdev
->descriptor
.idProduct
),
565 * Make sure we have 480 Mbps of bandwidth, otherwise things like
566 * video stream wouldn't likely work, since 12 Mbps is generally
567 * not enough even for most Digital TV streams.
569 if (usbdev
->speed
!= USB_SPEED_HIGH
&& disable_usb_speed_check
== 0) {
570 pr_err("au0828: Device initialization failed.\n");
571 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
575 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
577 pr_err("%s() Unable to allocate memory\n", __func__
);
581 mutex_init(&dev
->lock
);
582 mutex_lock(&dev
->lock
);
583 mutex_init(&dev
->mutex
);
584 mutex_init(&dev
->dvb
.lock
);
585 dev
->usbdev
= usbdev
;
586 dev
->boardnr
= id
->driver_info
;
587 dev
->board
= au0828_boards
[dev
->boardnr
];
589 /* Initialize the media controller */
590 retval
= au0828_media_device_init(dev
, usbdev
);
592 pr_err("%s() au0828_media_device_init failed\n",
594 mutex_unlock(&dev
->lock
);
599 retval
= au0828_v4l2_device_register(interface
, dev
);
601 au0828_usb_v4l2_media_release(dev
);
602 mutex_unlock(&dev
->lock
);
607 /* Power Up the bridge */
608 au0828_write(dev
, REG_600
, 1 << 4);
610 /* Bring up the GPIO's and supporting devices */
611 au0828_gpio_setup(dev
);
614 au0828_i2c_register(dev
);
617 au0828_card_setup(dev
);
620 retval
= au0828_analog_register(dev
, interface
);
622 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
628 retval
= au0828_dvb_register(dev
);
630 pr_err("%s() au0282_dev_register failed\n",
633 /* Remote controller */
634 au0828_rc_register(dev
);
637 * Store the pointer to the au0828_dev so it can be accessed in
638 * au0828_usb_disconnect
640 usb_set_intfdata(interface
, dev
);
642 pr_info("Registered device AU0828 [%s]\n",
643 dev
->board
.name
== NULL
? "Unset" : dev
->board
.name
);
645 mutex_unlock(&dev
->lock
);
647 retval
= au0828_media_device_register(dev
, usbdev
);
651 au0828_usb_disconnect(interface
);
656 static int au0828_suspend(struct usb_interface
*interface
,
657 pm_message_t message
)
659 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
664 pr_info("Suspend\n");
666 au0828_rc_suspend(dev
);
667 au0828_v4l2_suspend(dev
);
668 au0828_dvb_suspend(dev
);
670 /* FIXME: should suspend also ATV/DTV */
675 static int au0828_resume(struct usb_interface
*interface
)
677 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
683 /* Power Up the bridge */
684 au0828_write(dev
, REG_600
, 1 << 4);
686 /* Bring up the GPIO's and supporting devices */
687 au0828_gpio_setup(dev
);
689 au0828_rc_resume(dev
);
690 au0828_v4l2_resume(dev
);
691 au0828_dvb_resume(dev
);
693 /* FIXME: should resume also ATV/DTV */
698 static struct usb_driver au0828_usb_driver
= {
699 .name
= KBUILD_MODNAME
,
700 .probe
= au0828_usb_probe
,
701 .disconnect
= au0828_usb_disconnect
,
702 .id_table
= au0828_usb_id_table
,
703 .suspend
= au0828_suspend
,
704 .resume
= au0828_resume
,
705 .reset_resume
= au0828_resume
,
708 static int __init
au0828_init(void)
712 if (au0828_debug
& 1)
713 pr_info("%s() Debugging is enabled\n", __func__
);
715 if (au0828_debug
& 2)
716 pr_info("%s() USB Debugging is enabled\n", __func__
);
718 if (au0828_debug
& 4)
719 pr_info("%s() I2C Debugging is enabled\n", __func__
);
721 if (au0828_debug
& 8)
722 pr_info("%s() Bridge Debugging is enabled\n",
725 if (au0828_debug
& 16)
726 pr_info("%s() IR Debugging is enabled\n",
729 pr_info("au0828 driver loaded\n");
731 ret
= usb_register(&au0828_usb_driver
);
733 pr_err("usb_register failed, error = %d\n", ret
);
738 static void __exit
au0828_exit(void)
740 usb_deregister(&au0828_usb_driver
);
743 module_init(au0828_init
);
744 module_exit(au0828_exit
);
746 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
747 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
748 MODULE_LICENSE("GPL");
749 MODULE_VERSION("0.0.3");