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 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 39 | #define _AU0828_BULKPIPE 0x03 |
| 40 | #define _BULKPIPESIZE 0xffff |
| 41 | |
| 42 | static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 43 | u16 index, unsigned char *cp, u16 size); |
| 44 | static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 45 | u16 index, unsigned char *cp, u16 size); |
| 46 | |
| 47 | /* USB Direction */ |
| 48 | #define CMD_REQUEST_IN 0x00 |
| 49 | #define CMD_REQUEST_OUT 0x01 |
| 50 | |
| 51 | u32 au0828_readreg(struct au0828_dev *dev, u16 reg) |
| 52 | { |
| 53 | recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1); |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 54 | dprintk(8, "%s(0x%x) = 0x%x\n", __func__, reg, dev->ctrlmsg[0]); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 55 | return dev->ctrlmsg[0]; |
| 56 | } |
| 57 | |
| 58 | u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val) |
| 59 | { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 60 | dprintk(8, "%s(0x%x, 0x%x)\n", __func__, reg, val); |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 61 | return send_control_msg(dev, CMD_REQUEST_OUT, val, reg, |
| 62 | dev->ctrlmsg, 0); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void cmd_msg_dump(struct au0828_dev *dev) |
| 66 | { |
| 67 | int i; |
| 68 | |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 69 | for (i = 0; i < sizeof(dev->ctrlmsg); i += 16) |
| 70 | dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x " |
| 71 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 72 | __func__, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 73 | dev->ctrlmsg[i+0], dev->ctrlmsg[i+1], |
| 74 | dev->ctrlmsg[i+2], dev->ctrlmsg[i+3], |
| 75 | dev->ctrlmsg[i+4], dev->ctrlmsg[i+5], |
| 76 | dev->ctrlmsg[i+6], dev->ctrlmsg[i+7], |
| 77 | dev->ctrlmsg[i+8], dev->ctrlmsg[i+9], |
| 78 | dev->ctrlmsg[i+10], dev->ctrlmsg[i+11], |
| 79 | dev->ctrlmsg[i+12], dev->ctrlmsg[i+13], |
| 80 | dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]); |
| 81 | } |
| 82 | |
| 83 | static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 84 | u16 index, unsigned char *cp, u16 size) |
| 85 | { |
| 86 | int status = -ENODEV; |
| 87 | mutex_lock(&dev->mutex); |
| 88 | if (dev->usbdev) { |
| 89 | |
| 90 | /* cp must be memory that has been allocated by kmalloc */ |
| 91 | status = usb_control_msg(dev->usbdev, |
| 92 | usb_sndctrlpipe(dev->usbdev, 0), |
| 93 | request, |
Steven Toth | a8eb912 | 2008-10-16 20:19:41 -0300 | [diff] [blame^] | 94 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 95 | USB_RECIP_DEVICE, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 96 | value, index, |
| 97 | cp, size, 1000); |
| 98 | |
| 99 | status = min(status, 0); |
| 100 | |
| 101 | if (status < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 102 | printk(KERN_ERR "%s() Failed sending control message, error %d.\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 103 | __func__, status); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } |
| 107 | mutex_unlock(&dev->mutex); |
| 108 | return status; |
| 109 | } |
| 110 | |
| 111 | static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value, |
| 112 | u16 index, unsigned char *cp, u16 size) |
| 113 | { |
| 114 | int status = -ENODEV; |
| 115 | mutex_lock(&dev->mutex); |
| 116 | if (dev->usbdev) { |
| 117 | |
| 118 | memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg)); |
| 119 | |
| 120 | /* cp must be memory that has been allocated by kmalloc */ |
| 121 | status = usb_control_msg(dev->usbdev, |
| 122 | usb_rcvctrlpipe(dev->usbdev, 0), |
| 123 | request, |
| 124 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 125 | value, index, |
| 126 | cp, size, 1000); |
| 127 | |
| 128 | status = min(status, 0); |
| 129 | |
| 130 | if (status < 0) { |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 131 | printk(KERN_ERR "%s() Failed receiving control message, error %d.\n", |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 132 | __func__, status); |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 133 | } else |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 134 | cmd_msg_dump(dev); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 135 | } |
| 136 | mutex_unlock(&dev->mutex); |
| 137 | return status; |
| 138 | } |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 139 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 140 | static void au0828_usb_disconnect(struct usb_interface *interface) |
| 141 | { |
| 142 | struct au0828_dev *dev = usb_get_intfdata(interface); |
| 143 | |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 144 | dprintk(1, "%s()\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 145 | |
| 146 | /* Digital TV */ |
| 147 | au0828_dvb_unregister(dev); |
| 148 | |
| 149 | /* I2C */ |
| 150 | au0828_i2c_unregister(dev); |
| 151 | |
| 152 | usb_set_intfdata(interface, NULL); |
| 153 | |
| 154 | mutex_lock(&dev->mutex); |
| 155 | dev->usbdev = NULL; |
| 156 | mutex_unlock(&dev->mutex); |
| 157 | |
| 158 | kfree(dev); |
| 159 | |
| 160 | } |
| 161 | |
Mauro Carvalho Chehab | 18d73c5 | 2008-04-18 22:12:52 -0300 | [diff] [blame] | 162 | static int au0828_usb_probe(struct usb_interface *interface, |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 163 | const struct usb_device_id *id) |
| 164 | { |
| 165 | int ifnum; |
| 166 | struct au0828_dev *dev; |
| 167 | struct usb_device *usbdev = interface_to_usbdev(interface); |
| 168 | |
| 169 | ifnum = interface->altsetting->desc.bInterfaceNumber; |
| 170 | |
| 171 | if (ifnum != 0) |
| 172 | return -ENODEV; |
| 173 | |
Steven Toth | a9c36aa | 2008-04-17 21:41:28 -0300 | [diff] [blame] | 174 | 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] | 175 | le16_to_cpu(usbdev->descriptor.idVendor), |
| 176 | le16_to_cpu(usbdev->descriptor.idProduct), |
| 177 | ifnum); |
| 178 | |
| 179 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 180 | if (dev == NULL) { |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 181 | printk(KERN_ERR "%s() Unable to allocate memory\n", __func__); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 182 | return -ENOMEM; |
| 183 | } |
| 184 | |
| 185 | mutex_init(&dev->mutex); |
| 186 | mutex_init(&dev->dvb.lock); |
| 187 | dev->usbdev = usbdev; |
| 188 | dev->board = id->driver_info; |
| 189 | |
| 190 | usb_set_intfdata(interface, dev); |
| 191 | |
| 192 | /* Power Up the bridge */ |
| 193 | au0828_write(dev, REG_600, 1 << 4); |
| 194 | |
| 195 | /* Bring up the GPIO's and supporting devices */ |
| 196 | au0828_gpio_setup(dev); |
| 197 | |
| 198 | /* I2C */ |
| 199 | au0828_i2c_register(dev); |
| 200 | |
Steven Toth | 28930fa | 2008-03-29 19:53:07 -0300 | [diff] [blame] | 201 | /* Setup */ |
| 202 | au0828_card_setup(dev); |
| 203 | |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 204 | /* Digital TV */ |
| 205 | au0828_dvb_register(dev); |
| 206 | |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 207 | printk(KERN_INFO "Registered device AU0828 [%s]\n", |
Steven Toth | 9c26de55 | 2008-04-02 01:10:40 -0300 | [diff] [blame] | 208 | au0828_boards[dev->board].name == NULL ? "Unset" : |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 209 | au0828_boards[dev->board].name); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static struct usb_driver au0828_usb_driver = { |
| 215 | .name = DRIVER_NAME, |
| 216 | .probe = au0828_usb_probe, |
| 217 | .disconnect = au0828_usb_disconnect, |
| 218 | .id_table = au0828_usb_id_table, |
| 219 | }; |
| 220 | |
| 221 | static int __init au0828_init(void) |
| 222 | { |
| 223 | int ret; |
| 224 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 225 | if (au0828_debug & 1) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 226 | printk(KERN_INFO "%s() Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 227 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 228 | if (au0828_debug & 2) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 229 | printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 230 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 231 | if (au0828_debug & 4) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 232 | printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 233 | |
Adrian Bunk | b33d24c | 2008-04-25 19:06:03 -0300 | [diff] [blame] | 234 | if (au0828_debug & 8) |
Michael Krufky | f07e8e4 | 2008-04-18 21:42:30 -0300 | [diff] [blame] | 235 | printk(KERN_INFO "%s() Bridge Debugging is enabled\n", |
| 236 | __func__); |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 237 | |
| 238 | printk(KERN_INFO "au0828 driver loaded\n"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 239 | |
| 240 | ret = usb_register(&au0828_usb_driver); |
| 241 | if (ret) |
Steven Toth | bc3c613 | 2008-04-18 21:39:11 -0300 | [diff] [blame] | 242 | printk(KERN_ERR "usb_register failed, error = %d\n", ret); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static void __exit au0828_exit(void) |
| 248 | { |
| 249 | usb_deregister(&au0828_usb_driver); |
| 250 | } |
| 251 | |
| 252 | module_init(au0828_init); |
| 253 | module_exit(au0828_exit); |
| 254 | |
| 255 | MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products"); |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 256 | MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); |
Steven Toth | 265a651 | 2008-04-18 21:34:00 -0300 | [diff] [blame] | 257 | MODULE_LICENSE("GPL"); |