Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Auvitek USB bridge |
| 3 | * |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org> |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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 |
| 14 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/videodev2.h> |
| 24 | #include <media/v4l2-common.h> |
| 25 | #include <linux/mutex.h> |
| 26 | |
| 27 | #include "au0828.h" |
| 28 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 29 | /* |
| 30 | * 1 = General debug messages |
| 31 | * 2 = USB handling |
| 32 | * 4 = I2C related |
| 33 | * 8 = Bridge related |
| 34 | */ |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 35 | int au0828_debug; |
| 36 | module_param_named(debug, au0828_debug, int, 0644); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 37 | MODULE_PARM_DESC(debug, "enable debug messages"); |
| 38 | |
Devin Heitmueller | d6a9a43 | 2009-05-27 23:46:17 -0300 | [diff] [blame] | 39 | static unsigned int disable_usb_speed_check; |
| 40 | module_param(disable_usb_speed_check, int, 0444); |
| 41 | MODULE_PARM_DESC(disable_usb_speed_check, |
| 42 | "override min bandwidth requirement of 480M bps"); |
| 43 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 44 | #define _AU0828_BULKPIPE 0x03 |
| 45 | #define _BULKPIPESIZE 0xffff |
| 46 | |
| 47 | static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 48 | u16 index, unsigned char *cp, u16 size); |
| 49 | static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 50 | u16 index, unsigned char *cp, u16 size); |
| 51 | |
| 52 | /* USB Direction */ |
| 53 | #define CMD_REQUEST_IN 0x00 |
| 54 | #define CMD_REQUEST_OUT 0x01 |
| 55 | |
| 56 | u32 au0828_readreg(struct au0828_dev *dev, u16 reg) |
| 57 | { |
| 58 | recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1); |
Devin Heitmueller | b80f770 | 2009-03-11 03:00:57 -0300 | [diff] [blame] | 59 | dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 60 | return dev->ctrlmsg[0]; |
| 61 | } |
| 62 | |
| 63 | u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val) |
| 64 | { |
Devin Heitmueller | b80f770 | 2009-03-11 03:00:57 -0300 | [diff] [blame] | 65 | dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val); |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 66 | return send_control_msg(dev, CMD_REQUEST_OUT, val, reg, |
| 67 | dev->ctrlmsg, 0); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void cmd_msg_dump(struct au0828_dev *dev) |
| 71 | { |
| 72 | int i; |
| 73 | |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 74 | for (i = 0; i < sizeof(dev->ctrlmsg); i += 16) |
| 75 | dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x " |
| 76 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 77 | __func__, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 78 | dev->ctrlmsg[i+0], dev->ctrlmsg[i+1], |
| 79 | dev->ctrlmsg[i+2], dev->ctrlmsg[i+3], |
| 80 | dev->ctrlmsg[i+4], dev->ctrlmsg[i+5], |
| 81 | dev->ctrlmsg[i+6], dev->ctrlmsg[i+7], |
| 82 | dev->ctrlmsg[i+8], dev->ctrlmsg[i+9], |
| 83 | dev->ctrlmsg[i+10], dev->ctrlmsg[i+11], |
| 84 | dev->ctrlmsg[i+12], dev->ctrlmsg[i+13], |
| 85 | dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]); |
| 86 | } |
| 87 | |
| 88 | static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 89 | u16 index, unsigned char *cp, u16 size) |
| 90 | { |
| 91 | int status = -ENODEV; |
| 92 | mutex_lock(&dev->mutex); |
| 93 | if (dev->usbdev) { |
| 94 | |
| 95 | /* cp must be memory that has been allocated by kmalloc */ |
| 96 | status = usb_control_msg(dev->usbdev, |
| 97 | usb_sndctrlpipe(dev->usbdev, 0), |
| 98 | request, |
Steven Toth | a8eb912 | 2008-10-16 20:19:41 -0300 | [diff] [blame] | 99 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 100 | USB_RECIP_DEVICE, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 101 | value, index, |
| 102 | cp, size, 1000); |
| 103 | |
| 104 | status = min(status, 0); |
| 105 | |
| 106 | if (status < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 107 | printk(KERN_ERR "%s() Failed sending control message, error %d.\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 108 | __func__, status); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } |
| 112 | mutex_unlock(&dev->mutex); |
| 113 | return status; |
| 114 | } |
| 115 | |
| 116 | static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 117 | u16 index, unsigned char *cp, u16 size) |
| 118 | { |
| 119 | int status = -ENODEV; |
| 120 | mutex_lock(&dev->mutex); |
| 121 | if (dev->usbdev) { |
| 122 | |
| 123 | memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg)); |
| 124 | |
| 125 | /* cp must be memory that has been allocated by kmalloc */ |
| 126 | status = usb_control_msg(dev->usbdev, |
| 127 | usb_rcvctrlpipe(dev->usbdev, 0), |
| 128 | request, |
| 129 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 130 | value, index, |
| 131 | cp, size, 1000); |
| 132 | |
| 133 | status = min(status, 0); |
| 134 | |
| 135 | if (status < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 136 | printk(KERN_ERR "%s() Failed receiving control message, error %d.\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 137 | __func__, status); |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 138 | } else |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 139 | cmd_msg_dump(dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 140 | } |
| 141 | mutex_unlock(&dev->mutex); |
| 142 | return status; |
| 143 | } |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 144 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 145 | static void au0828_usb_disconnect(struct usb_interface *interface) |
| 146 | { |
| 147 | struct au0828_dev *dev = usb_get_intfdata(interface); |
| 148 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 149 | dprintk(1, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 150 | |
| 151 | /* Digital TV */ |
| 152 | au0828_dvb_unregister(dev); |
| 153 | |
Devin Heitmueller | 220be77 | 2009-03-11 03:01:01 -0300 | [diff] [blame] | 154 | if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 155 | au0828_analog_unregister(dev); |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 156 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 157 | /* I2C */ |
| 158 | au0828_i2c_unregister(dev); |
| 159 | |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 160 | v4l2_device_unregister(&dev->v4l2_dev); |
| 161 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 162 | usb_set_intfdata(interface, NULL); |
| 163 | |
| 164 | mutex_lock(&dev->mutex); |
| 165 | dev->usbdev = NULL; |
| 166 | mutex_unlock(&dev->mutex); |
| 167 | |
| 168 | kfree(dev); |
| 169 | |
| 170 | } |
| 171 | |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 172 | static int au0828_usb_probe(struct usb_interface *interface, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 173 | const struct usb_device_id *id) |
| 174 | { |
Janne Grunau | 4bb6851 | 2009-04-01 08:48:29 -0300 | [diff] [blame] | 175 | int ifnum, retval; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 176 | struct au0828_dev *dev; |
| 177 | struct usb_device *usbdev = interface_to_usbdev(interface); |
| 178 | |
| 179 | ifnum = interface->altsetting->desc.bInterfaceNumber; |
| 180 | |
| 181 | if (ifnum != 0) |
| 182 | return -ENODEV; |
| 183 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 184 | dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 185 | le16_to_cpu(usbdev->descriptor.idVendor), |
| 186 | le16_to_cpu(usbdev->descriptor.idProduct), |
| 187 | ifnum); |
| 188 | |
Devin Heitmueller | ee3436b | 2009-05-27 23:25:36 -0300 | [diff] [blame] | 189 | /* |
| 190 | * Make sure we have 480 Mbps of bandwidth, otherwise things like |
| 191 | * video stream wouldn't likely work, since 12 Mbps is generally |
| 192 | * not enough even for most Digital TV streams. |
| 193 | */ |
Devin Heitmueller | d6a9a43 | 2009-05-27 23:46:17 -0300 | [diff] [blame] | 194 | if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) { |
Devin Heitmueller | ee3436b | 2009-05-27 23:25:36 -0300 | [diff] [blame] | 195 | printk(KERN_ERR "au0828: Device initialization failed.\n"); |
| 196 | printk(KERN_ERR "au0828: Device must be connected to a " |
| 197 | "high-speed USB 2.0 port.\n"); |
| 198 | return -ENODEV; |
| 199 | } |
| 200 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 201 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 202 | if (dev == NULL) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 203 | printk(KERN_ERR "%s() Unable to allocate memory\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 204 | return -ENOMEM; |
| 205 | } |
| 206 | |
| 207 | mutex_init(&dev->mutex); |
| 208 | mutex_init(&dev->dvb.lock); |
| 209 | dev->usbdev = usbdev; |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 210 | dev->boardnr = id->driver_info; |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 211 | |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 212 | /* Create the v4l2_device */ |
Janne Grunau | a4124aa | 2009-04-01 08:47:35 -0300 | [diff] [blame] | 213 | retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev); |
Devin Heitmueller | b14667f | 2009-03-11 03:01:04 -0300 | [diff] [blame] | 214 | if (retval) { |
| 215 | printk(KERN_ERR "%s() v4l2_device_register failed\n", |
| 216 | __func__); |
| 217 | kfree(dev); |
| 218 | return -EIO; |
| 219 | } |
| 220 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 221 | /* Power Up the bridge */ |
| 222 | au0828_write(dev, REG_600, 1 << 4); |
| 223 | |
| 224 | /* Bring up the GPIO's and supporting devices */ |
| 225 | au0828_gpio_setup(dev); |
| 226 | |
| 227 | /* I2C */ |
| 228 | au0828_i2c_register(dev); |
| 229 | |
Steven Toth | 28930fa | 2008-03-29 19:53:07 -0300 | [diff] [blame] | 230 | /* Setup */ |
| 231 | au0828_card_setup(dev); |
| 232 | |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 233 | /* Analog TV */ |
Devin Heitmueller | 220be77 | 2009-03-11 03:01:01 -0300 | [diff] [blame] | 234 | if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) |
Devin Heitmueller | fc4ce6c | 2009-03-11 03:01:00 -0300 | [diff] [blame] | 235 | au0828_analog_register(dev, interface); |
Devin Heitmueller | 8b2f079 | 2009-03-11 03:00:40 -0300 | [diff] [blame] | 236 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 237 | /* Digital TV */ |
| 238 | au0828_dvb_register(dev); |
| 239 | |
Devin Heitmueller | fe78a49 | 2009-04-28 13:14:07 -0300 | [diff] [blame] | 240 | /* Store the pointer to the au0828_dev so it can be accessed in |
| 241 | au0828_usb_disconnect */ |
| 242 | usb_set_intfdata(interface, dev); |
| 243 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 244 | printk(KERN_INFO "Registered device AU0828 [%s]\n", |
Devin Heitmueller | f1add5b | 2009-03-11 03:00:47 -0300 | [diff] [blame] | 245 | dev->board.name == NULL ? "Unset" : dev->board.name); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static struct usb_driver au0828_usb_driver = { |
| 251 | .name = DRIVER_NAME, |
| 252 | .probe = au0828_usb_probe, |
| 253 | .disconnect = au0828_usb_disconnect, |
| 254 | .id_table = au0828_usb_id_table, |
| 255 | }; |
| 256 | |
| 257 | static int __init au0828_init(void) |
| 258 | { |
| 259 | int ret; |
| 260 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 261 | if (au0828_debug & 1) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 262 | printk(KERN_INFO "%s() Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 263 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 264 | if (au0828_debug & 2) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 265 | printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 266 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 267 | if (au0828_debug & 4) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 268 | printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 269 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 270 | if (au0828_debug & 8) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 271 | printk(KERN_INFO "%s() Bridge Debugging is enabled\n", |
| 272 | __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 273 | |
| 274 | printk(KERN_INFO "au0828 driver loaded\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 275 | |
| 276 | ret = usb_register(&au0828_usb_driver); |
| 277 | if (ret) |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 278 | printk(KERN_ERR "usb_register failed, error = %d\n", ret); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 279 | |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static void __exit au0828_exit(void) |
| 284 | { |
| 285 | usb_deregister(&au0828_usb_driver); |
| 286 | } |
| 287 | |
| 288 | module_init(au0828_init); |
| 289 | module_exit(au0828_exit); |
| 290 | |
| 291 | MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products"); |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 292 | MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 293 | MODULE_LICENSE("GPL"); |