blob: ca342e4c61fcdc5bf59108a5a2bb38ebbbea81b0 [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 * Driver for the Auvitek USB bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
Steven Toth265a6512008-04-18 21:34:00 -03005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Steven Toth265a6512008-04-18 21:34:00 -030024#include <linux/videodev2.h>
25#include <media/v4l2-common.h>
26#include <linux/mutex.h>
27
28#include "au0828.h"
29
Steven Tothbc3c6132008-04-18 21:39:11 -030030/*
31 * 1 = General debug messages
32 * 2 = USB handling
33 * 4 = I2C related
34 * 8 = Bridge related
35 */
Adrian Bunkb33d24c2008-04-25 19:06:03 -030036int au0828_debug;
37module_param_named(debug, au0828_debug, int, 0644);
Steven Toth265a6512008-04-18 21:34:00 -030038MODULE_PARM_DESC(debug, "enable debug messages");
39
Devin Heitmuellerd6a9a432009-05-27 23:46:17 -030040static unsigned int disable_usb_speed_check;
41module_param(disable_usb_speed_check, int, 0444);
42MODULE_PARM_DESC(disable_usb_speed_check,
43 "override min bandwidth requirement of 480M bps");
44
Steven Toth265a6512008-04-18 21:34:00 -030045#define _AU0828_BULKPIPE 0x03
46#define _BULKPIPESIZE 0xffff
47
48static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
49 u16 index, unsigned char *cp, u16 size);
50static 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
57u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
58{
59 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
Devin Heitmuellerb80f7702009-03-11 03:00:57 -030060 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]);
Steven Toth265a6512008-04-18 21:34:00 -030061 return dev->ctrlmsg[0];
62}
63
64u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
65{
Devin Heitmuellerb80f7702009-03-11 03:00:57 -030066 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030067 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
68 dev->ctrlmsg, 0);
Steven Toth265a6512008-04-18 21:34:00 -030069}
70
71static void cmd_msg_dump(struct au0828_dev *dev)
72{
73 int i;
74
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030075 for (i = 0; i < sizeof(dev->ctrlmsg); i += 16)
76 dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x "
77 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -030078 __func__,
Steven Toth265a6512008-04-18 21:34:00 -030079 dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
80 dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
81 dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
82 dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
83 dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
84 dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
85 dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
86 dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
87}
88
89static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
90 u16 index, unsigned char *cp, u16 size)
91{
92 int status = -ENODEV;
93 mutex_lock(&dev->mutex);
94 if (dev->usbdev) {
95
96 /* cp must be memory that has been allocated by kmalloc */
97 status = usb_control_msg(dev->usbdev,
98 usb_sndctrlpipe(dev->usbdev, 0),
99 request,
Steven Totha8eb9122008-10-16 20:19:41 -0300100 USB_DIR_OUT | USB_TYPE_VENDOR |
101 USB_RECIP_DEVICE,
Steven Toth265a6512008-04-18 21:34:00 -0300102 value, index,
103 cp, size, 1000);
104
105 status = min(status, 0);
106
107 if (status < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300108 printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300109 __func__, status);
Steven Toth265a6512008-04-18 21:34:00 -0300110 }
111
112 }
113 mutex_unlock(&dev->mutex);
114 return status;
115}
116
117static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
118 u16 index, unsigned char *cp, u16 size)
119{
120 int status = -ENODEV;
121 mutex_lock(&dev->mutex);
122 if (dev->usbdev) {
123
124 memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
125
126 /* cp must be memory that has been allocated by kmalloc */
127 status = usb_control_msg(dev->usbdev,
128 usb_rcvctrlpipe(dev->usbdev, 0),
129 request,
130 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 value, index,
132 cp, size, 1000);
133
134 status = min(status, 0);
135
136 if (status < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300137 printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300138 __func__, status);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300139 } else
Steven Tothbc3c6132008-04-18 21:39:11 -0300140 cmd_msg_dump(dev);
Steven Toth265a6512008-04-18 21:34:00 -0300141 }
142 mutex_unlock(&dev->mutex);
143 return status;
144}
Steven Totha9c36aa2008-04-17 21:41:28 -0300145
Steven Toth265a6512008-04-18 21:34:00 -0300146static void au0828_usb_disconnect(struct usb_interface *interface)
147{
148 struct au0828_dev *dev = usb_get_intfdata(interface);
149
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300150 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300151
152 /* Digital TV */
153 au0828_dvb_unregister(dev);
154
Devin Heitmueller220be772009-03-11 03:01:01 -0300155 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300156 au0828_analog_unregister(dev);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300157
Steven Toth265a6512008-04-18 21:34:00 -0300158 /* I2C */
159 au0828_i2c_unregister(dev);
160
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300161 v4l2_device_unregister(&dev->v4l2_dev);
162
Steven Toth265a6512008-04-18 21:34:00 -0300163 usb_set_intfdata(interface, NULL);
164
165 mutex_lock(&dev->mutex);
166 dev->usbdev = NULL;
167 mutex_unlock(&dev->mutex);
168
169 kfree(dev);
170
171}
172
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300173static int au0828_usb_probe(struct usb_interface *interface,
Steven Toth265a6512008-04-18 21:34:00 -0300174 const struct usb_device_id *id)
175{
Janne Grunau4bb68512009-04-01 08:48:29 -0300176 int ifnum, retval;
Steven Toth265a6512008-04-18 21:34:00 -0300177 struct au0828_dev *dev;
178 struct usb_device *usbdev = interface_to_usbdev(interface);
179
180 ifnum = interface->altsetting->desc.bInterfaceNumber;
181
182 if (ifnum != 0)
183 return -ENODEV;
184
Steven Totha9c36aa2008-04-17 21:41:28 -0300185 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
Steven Toth265a6512008-04-18 21:34:00 -0300186 le16_to_cpu(usbdev->descriptor.idVendor),
187 le16_to_cpu(usbdev->descriptor.idProduct),
188 ifnum);
189
Devin Heitmuelleree3436b2009-05-27 23:25:36 -0300190 /*
191 * Make sure we have 480 Mbps of bandwidth, otherwise things like
192 * video stream wouldn't likely work, since 12 Mbps is generally
193 * not enough even for most Digital TV streams.
194 */
Devin Heitmuellerd6a9a432009-05-27 23:46:17 -0300195 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
Devin Heitmuelleree3436b2009-05-27 23:25:36 -0300196 printk(KERN_ERR "au0828: Device initialization failed.\n");
197 printk(KERN_ERR "au0828: Device must be connected to a "
198 "high-speed USB 2.0 port.\n");
199 return -ENODEV;
200 }
201
Steven Toth265a6512008-04-18 21:34:00 -0300202 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
203 if (dev == NULL) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300204 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300205 return -ENOMEM;
206 }
207
208 mutex_init(&dev->mutex);
209 mutex_init(&dev->dvb.lock);
210 dev->usbdev = usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300211 dev->boardnr = id->driver_info;
Steven Toth265a6512008-04-18 21:34:00 -0300212
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300213 /* Create the v4l2_device */
Janne Grunaua4124aa2009-04-01 08:47:35 -0300214 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300215 if (retval) {
216 printk(KERN_ERR "%s() v4l2_device_register failed\n",
217 __func__);
218 kfree(dev);
219 return -EIO;
220 }
221
Steven Toth265a6512008-04-18 21:34:00 -0300222 /* Power Up the bridge */
223 au0828_write(dev, REG_600, 1 << 4);
224
225 /* Bring up the GPIO's and supporting devices */
226 au0828_gpio_setup(dev);
227
228 /* I2C */
229 au0828_i2c_register(dev);
230
Steven Toth28930fa2008-03-29 19:53:07 -0300231 /* Setup */
232 au0828_card_setup(dev);
233
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300234 /* Analog TV */
Devin Heitmueller220be772009-03-11 03:01:01 -0300235 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
Devin Heitmuellerfc4ce6c2009-03-11 03:01:00 -0300236 au0828_analog_register(dev, interface);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300237
Steven Toth265a6512008-04-18 21:34:00 -0300238 /* Digital TV */
239 au0828_dvb_register(dev);
240
Devin Heitmuellerfe78a492009-04-28 13:14:07 -0300241 /* Store the pointer to the au0828_dev so it can be accessed in
242 au0828_usb_disconnect */
243 usb_set_intfdata(interface, dev);
244
Steven Tothbc3c6132008-04-18 21:39:11 -0300245 printk(KERN_INFO "Registered device AU0828 [%s]\n",
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300246 dev->board.name == NULL ? "Unset" : dev->board.name);
Steven Toth265a6512008-04-18 21:34:00 -0300247
248 return 0;
249}
250
251static struct usb_driver au0828_usb_driver = {
252 .name = DRIVER_NAME,
253 .probe = au0828_usb_probe,
254 .disconnect = au0828_usb_disconnect,
255 .id_table = au0828_usb_id_table,
256};
257
258static int __init au0828_init(void)
259{
260 int ret;
261
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300262 if (au0828_debug & 1)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300263 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300264
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300265 if (au0828_debug & 2)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300266 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300267
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300268 if (au0828_debug & 4)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300269 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300270
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300271 if (au0828_debug & 8)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300272 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
273 __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300274
275 printk(KERN_INFO "au0828 driver loaded\n");
Steven Toth265a6512008-04-18 21:34:00 -0300276
277 ret = usb_register(&au0828_usb_driver);
278 if (ret)
Steven Tothbc3c6132008-04-18 21:39:11 -0300279 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
Steven Toth265a6512008-04-18 21:34:00 -0300280
281 return ret;
282}
283
284static void __exit au0828_exit(void)
285{
286 usb_deregister(&au0828_usb_driver);
287}
288
289module_init(au0828_init);
290module_exit(au0828_exit);
291
292MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
Steven Toth6d897612008-09-03 17:12:12 -0300293MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Toth265a6512008-04-18 21:34:00 -0300294MODULE_LICENSE("GPL");