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.
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-common.h>
28 #include <linux/mutex.h>
30 /* Due to enum tuner_pad_index */
31 #include <media/tuner.h>
34 * 1 = General debug messages
41 module_param_named(debug
, au0828_debug
, int, 0644);
42 MODULE_PARM_DESC(debug
,
43 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
45 static unsigned int disable_usb_speed_check
;
46 module_param(disable_usb_speed_check
, int, 0444);
47 MODULE_PARM_DESC(disable_usb_speed_check
,
48 "override min bandwidth requirement of 480M bps");
50 #define _AU0828_BULKPIPE 0x03
51 #define _BULKPIPESIZE 0xffff
53 static int send_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
55 static int recv_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
56 u16 index
, unsigned char *cp
, u16 size
);
59 #define CMD_REQUEST_IN 0x00
60 #define CMD_REQUEST_OUT 0x01
62 u32
au0828_readreg(struct au0828_dev
*dev
, u16 reg
)
66 recv_control_msg(dev
, CMD_REQUEST_IN
, 0, reg
, &result
, 1);
67 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__
, reg
, result
);
72 u32
au0828_writereg(struct au0828_dev
*dev
, u16 reg
, u32 val
)
74 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__
, reg
, val
);
75 return send_control_msg(dev
, CMD_REQUEST_OUT
, val
, reg
);
78 static int send_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
85 /* cp must be memory that has been allocated by kmalloc */
86 status
= usb_control_msg(dev
->usbdev
,
87 usb_sndctrlpipe(dev
->usbdev
, 0),
89 USB_DIR_OUT
| USB_TYPE_VENDOR
|
91 value
, index
, NULL
, 0, 1000);
93 status
= min(status
, 0);
96 pr_err("%s() Failed sending control message, error %d.\n",
105 static int recv_control_msg(struct au0828_dev
*dev
, u16 request
, u32 value
,
106 u16 index
, unsigned char *cp
, u16 size
)
108 int status
= -ENODEV
;
109 mutex_lock(&dev
->mutex
);
111 status
= usb_control_msg(dev
->usbdev
,
112 usb_rcvctrlpipe(dev
->usbdev
, 0),
114 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
116 dev
->ctrlmsg
, size
, 1000);
118 status
= min(status
, 0);
121 pr_err("%s() Failed receiving control message, error %d.\n",
125 /* the host controller requires heap allocated memory, which
126 is why we didn't just pass "cp" into usb_control_msg */
127 memcpy(cp
, dev
->ctrlmsg
, size
);
129 mutex_unlock(&dev
->mutex
);
133 static void au0828_unregister_media_device(struct au0828_dev
*dev
)
136 #ifdef CONFIG_MEDIA_CONTROLLER
137 if (dev
->media_dev
) {
138 media_device_unregister(dev
->media_dev
);
139 media_device_cleanup(dev
->media_dev
);
140 kfree(dev
->media_dev
);
141 dev
->media_dev
= NULL
;
146 static void au0828_usb_release(struct au0828_dev
*dev
)
148 au0828_unregister_media_device(dev
);
151 au0828_i2c_unregister(dev
);
156 #ifdef CONFIG_VIDEO_AU0828_V4L2
158 static void au0828_usb_v4l2_media_release(struct au0828_dev
*dev
)
160 #ifdef CONFIG_MEDIA_CONTROLLER
163 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
164 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
166 media_device_unregister_entity(&dev
->input_ent
[i
]);
171 static void au0828_usb_v4l2_release(struct v4l2_device
*v4l2_dev
)
173 struct au0828_dev
*dev
=
174 container_of(v4l2_dev
, struct au0828_dev
, v4l2_dev
);
176 v4l2_ctrl_handler_free(&dev
->v4l2_ctrl_hdl
);
177 v4l2_device_unregister(&dev
->v4l2_dev
);
178 au0828_usb_v4l2_media_release(dev
);
179 au0828_usb_release(dev
);
183 static void au0828_usb_disconnect(struct usb_interface
*interface
)
185 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
187 dprintk(1, "%s()\n", __func__
);
189 /* there is a small window after disconnect, before
190 dev->usbdev is NULL, for poll (e.g: IR) try to access
191 the device and fill the dmesg with error messages.
192 Set the status so poll routines can check and avoid
193 access after disconnect.
195 dev
->dev_state
= DEV_DISCONNECTED
;
197 au0828_rc_unregister(dev
);
199 au0828_dvb_unregister(dev
);
201 usb_set_intfdata(interface
, NULL
);
202 mutex_lock(&dev
->mutex
);
204 mutex_unlock(&dev
->mutex
);
205 #ifdef CONFIG_VIDEO_AU0828_V4L2
206 if (AUVI_INPUT(0).type
!= AU0828_VMUX_UNDEFINED
) {
207 au0828_analog_unregister(dev
);
208 v4l2_device_disconnect(&dev
->v4l2_dev
);
209 v4l2_device_put(&dev
->v4l2_dev
);
211 * No need to call au0828_usb_release() if V4L2 is enabled,
212 * as this is already called via au0828_usb_v4l2_release()
217 au0828_usb_release(dev
);
220 static int au0828_media_device_init(struct au0828_dev
*dev
,
221 struct usb_device
*udev
)
223 #ifdef CONFIG_MEDIA_CONTROLLER
224 struct media_device
*mdev
;
226 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
230 mdev
->dev
= &udev
->dev
;
232 if (!dev
->board
.name
)
233 strlcpy(mdev
->model
, "unknown au0828", sizeof(mdev
->model
));
235 strlcpy(mdev
->model
, dev
->board
.name
, sizeof(mdev
->model
));
237 strlcpy(mdev
->serial
, udev
->serial
, sizeof(mdev
->serial
));
238 strcpy(mdev
->bus_info
, udev
->devpath
);
239 mdev
->hw_revision
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
240 mdev
->driver_version
= LINUX_VERSION_CODE
;
242 media_device_init(mdev
);
244 dev
->media_dev
= mdev
;
250 static int au0828_create_media_graph(struct au0828_dev
*dev
)
252 #ifdef CONFIG_MEDIA_CONTROLLER
253 struct media_device
*mdev
= dev
->media_dev
;
254 struct media_entity
*entity
;
255 struct media_entity
*tuner
= NULL
, *decoder
= NULL
;
261 media_device_for_each_entity(entity
, mdev
) {
262 switch (entity
->function
) {
263 case MEDIA_ENT_F_TUNER
:
266 case MEDIA_ENT_F_ATV_DECODER
:
272 /* Analog setup, using tuner as a link */
274 /* Something bad happened! */
279 ret
= media_create_pad_link(tuner
, TUNER_PAD_IF_OUTPUT
,
281 MEDIA_LNK_FL_ENABLED
);
285 ret
= media_create_pad_link(decoder
, 1, &dev
->vdev
.entity
, 0,
286 MEDIA_LNK_FL_ENABLED
);
289 ret
= media_create_pad_link(decoder
, 2, &dev
->vbi_dev
.entity
, 0,
290 MEDIA_LNK_FL_ENABLED
);
294 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
295 struct media_entity
*ent
= &dev
->input_ent
[i
];
297 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
300 switch (AUVI_INPUT(i
).type
) {
301 case AU0828_VMUX_CABLE
:
302 case AU0828_VMUX_TELEVISION
:
303 case AU0828_VMUX_DVB
:
307 ret
= media_create_pad_link(ent
, 0, tuner
,
309 MEDIA_LNK_FL_ENABLED
);
313 case AU0828_VMUX_COMPOSITE
:
314 case AU0828_VMUX_SVIDEO
:
315 default: /* AU0828_VMUX_DEBUG */
316 /* FIXME: fix the decoder PAD */
317 ret
= media_create_pad_link(ent
, 0, decoder
, 0, 0);
327 static int au0828_usb_probe(struct usb_interface
*interface
,
328 const struct usb_device_id
*id
)
333 struct au0828_dev
*dev
;
334 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
336 ifnum
= interface
->altsetting
->desc
.bInterfaceNumber
;
341 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__
,
342 le16_to_cpu(usbdev
->descriptor
.idVendor
),
343 le16_to_cpu(usbdev
->descriptor
.idProduct
),
347 * Make sure we have 480 Mbps of bandwidth, otherwise things like
348 * video stream wouldn't likely work, since 12 Mbps is generally
349 * not enough even for most Digital TV streams.
351 if (usbdev
->speed
!= USB_SPEED_HIGH
&& disable_usb_speed_check
== 0) {
352 pr_err("au0828: Device initialization failed.\n");
353 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
357 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
359 pr_err("%s() Unable to allocate memory\n", __func__
);
363 mutex_init(&dev
->lock
);
364 mutex_lock(&dev
->lock
);
365 mutex_init(&dev
->mutex
);
366 mutex_init(&dev
->dvb
.lock
);
367 dev
->usbdev
= usbdev
;
368 dev
->boardnr
= id
->driver_info
;
369 dev
->board
= au0828_boards
[dev
->boardnr
];
371 /* Initialize the media controller */
372 retval
= au0828_media_device_init(dev
, usbdev
);
374 pr_err("%s() au0828_media_device_init failed\n",
376 mutex_unlock(&dev
->lock
);
381 #ifdef CONFIG_VIDEO_AU0828_V4L2
382 dev
->v4l2_dev
.release
= au0828_usb_v4l2_release
;
384 /* Create the v4l2_device */
385 #ifdef CONFIG_MEDIA_CONTROLLER
386 dev
->v4l2_dev
.mdev
= dev
->media_dev
;
388 retval
= v4l2_device_register(&interface
->dev
, &dev
->v4l2_dev
);
390 pr_err("%s() v4l2_device_register failed\n",
392 mutex_unlock(&dev
->lock
);
396 /* This control handler will inherit the controls from au8522 */
397 retval
= v4l2_ctrl_handler_init(&dev
->v4l2_ctrl_hdl
, 4);
399 pr_err("%s() v4l2_ctrl_handler_init failed\n",
401 mutex_unlock(&dev
->lock
);
405 dev
->v4l2_dev
.ctrl_handler
= &dev
->v4l2_ctrl_hdl
;
408 /* Power Up the bridge */
409 au0828_write(dev
, REG_600
, 1 << 4);
411 /* Bring up the GPIO's and supporting devices */
412 au0828_gpio_setup(dev
);
415 au0828_i2c_register(dev
);
418 au0828_card_setup(dev
);
420 #ifdef CONFIG_VIDEO_AU0828_V4L2
422 if (AUVI_INPUT(0).type
!= AU0828_VMUX_UNDEFINED
)
423 au0828_analog_register(dev
, interface
);
427 retval
= au0828_dvb_register(dev
);
429 pr_err("%s() au0282_dev_register failed\n",
432 /* Remote controller */
433 au0828_rc_register(dev
);
436 * Store the pointer to the au0828_dev so it can be accessed in
437 * au0828_usb_disconnect
439 usb_set_intfdata(interface
, dev
);
441 pr_info("Registered device AU0828 [%s]\n",
442 dev
->board
.name
== NULL
? "Unset" : dev
->board
.name
);
444 mutex_unlock(&dev
->lock
);
446 retval
= au0828_create_media_graph(dev
);
448 pr_err("%s() au0282_dev_register failed to create graph\n",
453 #ifdef CONFIG_MEDIA_CONTROLLER
454 retval
= media_device_register(dev
->media_dev
);
459 au0828_usb_disconnect(interface
);
464 static int au0828_suspend(struct usb_interface
*interface
,
465 pm_message_t message
)
467 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
472 pr_info("Suspend\n");
474 au0828_rc_suspend(dev
);
475 au0828_v4l2_suspend(dev
);
476 au0828_dvb_suspend(dev
);
478 /* FIXME: should suspend also ATV/DTV */
483 static int au0828_resume(struct usb_interface
*interface
)
485 struct au0828_dev
*dev
= usb_get_intfdata(interface
);
491 /* Power Up the bridge */
492 au0828_write(dev
, REG_600
, 1 << 4);
494 /* Bring up the GPIO's and supporting devices */
495 au0828_gpio_setup(dev
);
497 au0828_rc_resume(dev
);
498 au0828_v4l2_resume(dev
);
499 au0828_dvb_resume(dev
);
501 /* FIXME: should resume also ATV/DTV */
506 static struct usb_driver au0828_usb_driver
= {
507 .name
= KBUILD_MODNAME
,
508 .probe
= au0828_usb_probe
,
509 .disconnect
= au0828_usb_disconnect
,
510 .id_table
= au0828_usb_id_table
,
511 .suspend
= au0828_suspend
,
512 .resume
= au0828_resume
,
513 .reset_resume
= au0828_resume
,
516 static int __init
au0828_init(void)
520 if (au0828_debug
& 1)
521 pr_info("%s() Debugging is enabled\n", __func__
);
523 if (au0828_debug
& 2)
524 pr_info("%s() USB Debugging is enabled\n", __func__
);
526 if (au0828_debug
& 4)
527 pr_info("%s() I2C Debugging is enabled\n", __func__
);
529 if (au0828_debug
& 8)
530 pr_info("%s() Bridge Debugging is enabled\n",
533 if (au0828_debug
& 16)
534 pr_info("%s() IR Debugging is enabled\n",
537 pr_info("au0828 driver loaded\n");
539 ret
= usb_register(&au0828_usb_driver
);
541 pr_err("usb_register failed, error = %d\n", ret
);
546 static void __exit
au0828_exit(void)
548 usb_deregister(&au0828_usb_driver
);
551 module_init(au0828_init
);
552 module_exit(au0828_exit
);
554 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
555 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
556 MODULE_LICENSE("GPL");
557 MODULE_VERSION("0.0.3");