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