2 * Driver for the Auvitek USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
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.
22 #include <linux/module.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <linux/mutex.h>
29 static unsigned int debug;
30 module_param(debug, int, 0644);
31 MODULE_PARM_DESC(debug, "enable debug messages");
33 #define _err(fmt, arg...)\
35 printk(KERN_ERR DRIVER_NAME "/0: " fmt, ## arg);\
38 #define _info(fmt, arg...)\
40 printk(KERN_INFO DRIVER_NAME "/0: " fmt, ## arg);\
43 #define _dbg(level, fmt, arg...)\
46 printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
49 #define _AU0828_BULKPIPE 0x03
50 #define _BULKPIPESIZE 0xffff
52 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
53 u16 index, unsigned char *cp, u16 size);
54 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
55 u16 index, unsigned char *cp, u16 size);
58 #define CMD_REQUEST_IN 0x00
59 #define CMD_REQUEST_OUT 0x01
61 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
63 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
64 _dbg(3,"%s(0x%x) = 0x%x\n", __FUNCTION__, reg, dev->ctrlmsg[0]);
65 return dev->ctrlmsg[0];
68 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
70 _dbg(3,"%s(0x%x, 0x%x)\n", __FUNCTION__, reg, val);
71 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg, dev->ctrlmsg, 0);
74 static void cmd_msg_dump(struct au0828_dev *dev)
78 for (i = 0;i < sizeof(dev->ctrlmsg); i+=16)
79 _dbg(1,"%s() %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
80 "%02x %02x %02x %02x %02x %02x\n",
82 dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
83 dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
84 dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
85 dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
86 dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
87 dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
88 dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
89 dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
92 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
93 u16 index, unsigned char *cp, u16 size)
96 mutex_lock(&dev->mutex);
99 /* cp must be memory that has been allocated by kmalloc */
100 status = usb_control_msg(dev->usbdev,
101 usb_sndctrlpipe(dev->usbdev, 0),
103 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107 status = min(status, 0);
110 _err("%s() Failed sending control message, error %d.\n",
116 mutex_unlock(&dev->mutex);
120 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
121 u16 index, unsigned char *cp, u16 size)
123 int status = -ENODEV;
124 mutex_lock(&dev->mutex);
127 memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
129 /* cp must be memory that has been allocated by kmalloc */
130 status = usb_control_msg(dev->usbdev,
131 usb_rcvctrlpipe(dev->usbdev, 0),
133 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
137 status = min(status, 0);
140 _err("%s() Failed receiving ctrl msg, error %d.\n",
148 mutex_unlock(&dev->mutex);
151 static void au0828_usb_disconnect(struct usb_interface *interface)
153 struct au0828_dev *dev = usb_get_intfdata(interface);
155 _dbg(1,"%s()\n", __FUNCTION__);
158 au0828_dvb_unregister(dev);
161 au0828_i2c_unregister(dev);
163 usb_set_intfdata(interface, NULL);
165 mutex_lock(&dev->mutex);
167 mutex_unlock(&dev->mutex);
173 static int au0828_usb_probe (struct usb_interface *interface,
174 const struct usb_device_id *id)
177 struct au0828_dev *dev;
178 struct usb_device *usbdev = interface_to_usbdev(interface);
180 ifnum = interface->altsetting->desc.bInterfaceNumber;
185 _dbg(1,"%s() vendor id 0x%x device id 0x%x ifnum:%d\n",
187 le16_to_cpu(usbdev->descriptor.idVendor),
188 le16_to_cpu(usbdev->descriptor.idProduct),
191 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
193 _err("Unable to allocate memory\n");
197 mutex_init(&dev->mutex);
198 mutex_init(&dev->dvb.lock);
199 dev->usbdev = usbdev;
200 dev->board = id->driver_info;
202 usb_set_intfdata(interface, dev);
204 /* Power Up the bridge */
205 au0828_write(dev, REG_600, 1 << 4);
207 /* Bring up the GPIO's and supporting devices */
208 au0828_gpio_setup(dev);
211 au0828_i2c_register(dev);
214 au0828_card_setup(dev);
217 au0828_dvb_register(dev);
219 _info("Registered device AU0828 [%s]\n",
220 au0828_boards[dev->board].name == NULL ? "Unset" :
221 au0828_boards[dev->board].name);
226 static struct usb_driver au0828_usb_driver = {
228 .probe = au0828_usb_probe,
229 .disconnect = au0828_usb_disconnect,
230 .id_table = au0828_usb_id_table,
233 static int __init au0828_init(void)
237 _info("au0828 driver loaded\n");
239 ret = usb_register(&au0828_usb_driver);
241 _err("usb_register failed, error = %d\n", ret);
246 static void __exit au0828_exit(void)
248 usb_deregister(&au0828_usb_driver);
251 module_init(au0828_init);
252 module_exit(au0828_exit);
254 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
255 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
256 MODULE_LICENSE("GPL");