blob: 680e88f613972089c8d1473cba7907b123835bdc [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>
23#include <linux/videodev2.h>
24#include <media/v4l2-common.h>
25#include <linux/mutex.h>
26
27#include "au0828.h"
28
Steven Tothbc3c6132008-04-18 21:39:11 -030029/*
30 * 1 = General debug messages
31 * 2 = USB handling
32 * 4 = I2C related
33 * 8 = Bridge related
34 */
Adrian Bunkb33d24c2008-04-25 19:06:03 -030035int au0828_debug;
36module_param_named(debug, au0828_debug, int, 0644);
Steven Toth265a6512008-04-18 21:34:00 -030037MODULE_PARM_DESC(debug, "enable debug messages");
38
Steven Toth265a6512008-04-18 21:34:00 -030039#define _AU0828_BULKPIPE 0x03
40#define _BULKPIPESIZE 0xffff
41
42static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
43 u16 index, unsigned char *cp, u16 size);
44static 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
51u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
52{
53 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
Michael Krufkyf07e8e42008-04-18 21:42:30 -030054 dprintk(8, "%s(0x%x) = 0x%x\n", __func__, reg, dev->ctrlmsg[0]);
Steven Toth265a6512008-04-18 21:34:00 -030055 return dev->ctrlmsg[0];
56}
57
58u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
59{
Michael Krufkyf07e8e42008-04-18 21:42:30 -030060 dprintk(8, "%s(0x%x, 0x%x)\n", __func__, reg, val);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030061 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
62 dev->ctrlmsg, 0);
Steven Toth265a6512008-04-18 21:34:00 -030063}
64
65static void cmd_msg_dump(struct au0828_dev *dev)
66{
67 int i;
68
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030069 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 Krufkyf07e8e42008-04-18 21:42:30 -030072 __func__,
Steven Toth265a6512008-04-18 21:34:00 -030073 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
83static 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 Totha8eb9122008-10-16 20:19:41 -030094 USB_DIR_OUT | USB_TYPE_VENDOR |
95 USB_RECIP_DEVICE,
Steven Toth265a6512008-04-18 21:34:00 -030096 value, index,
97 cp, size, 1000);
98
99 status = min(status, 0);
100
101 if (status < 0) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300102 printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300103 __func__, status);
Steven Toth265a6512008-04-18 21:34:00 -0300104 }
105
106 }
107 mutex_unlock(&dev->mutex);
108 return status;
109}
110
111static 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 Tothbc3c6132008-04-18 21:39:11 -0300131 printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300132 __func__, status);
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300133 } else
Steven Tothbc3c6132008-04-18 21:39:11 -0300134 cmd_msg_dump(dev);
Steven Toth265a6512008-04-18 21:34:00 -0300135 }
136 mutex_unlock(&dev->mutex);
137 return status;
138}
Steven Totha9c36aa2008-04-17 21:41:28 -0300139
Steven Toth265a6512008-04-18 21:34:00 -0300140static void au0828_usb_disconnect(struct usb_interface *interface)
141{
142 struct au0828_dev *dev = usb_get_intfdata(interface);
143
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300144 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300145
146 /* Digital TV */
147 au0828_dvb_unregister(dev);
148
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300149 au0828_analog_unregister(dev);
150
Steven Toth265a6512008-04-18 21:34:00 -0300151 /* I2C */
152 au0828_i2c_unregister(dev);
153
154 usb_set_intfdata(interface, NULL);
155
156 mutex_lock(&dev->mutex);
157 dev->usbdev = NULL;
158 mutex_unlock(&dev->mutex);
159
160 kfree(dev);
161
162}
163
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300164static int au0828_usb_probe(struct usb_interface *interface,
Steven Toth265a6512008-04-18 21:34:00 -0300165 const struct usb_device_id *id)
166{
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300167 int ifnum, i;
Steven Toth265a6512008-04-18 21:34:00 -0300168 struct au0828_dev *dev;
169 struct usb_device *usbdev = interface_to_usbdev(interface);
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300170 struct usb_host_interface *iface_desc;
171 struct usb_endpoint_descriptor *endpoint;
Steven Toth265a6512008-04-18 21:34:00 -0300172
173 ifnum = interface->altsetting->desc.bInterfaceNumber;
174
175 if (ifnum != 0)
176 return -ENODEV;
177
Steven Totha9c36aa2008-04-17 21:41:28 -0300178 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
Steven Toth265a6512008-04-18 21:34:00 -0300179 le16_to_cpu(usbdev->descriptor.idVendor),
180 le16_to_cpu(usbdev->descriptor.idProduct),
181 ifnum);
182
183 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
184 if (dev == NULL) {
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300185 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300186 return -ENOMEM;
187 }
188
189 mutex_init(&dev->mutex);
190 mutex_init(&dev->dvb.lock);
191 dev->usbdev = usbdev;
192 dev->board = id->driver_info;
193
194 usb_set_intfdata(interface, dev);
195
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300196 /* set au0828 usb interface0 to as5 */
197 usb_set_interface(usbdev,
198 interface->cur_altsetting->desc.bInterfaceNumber, 5);
199
200 /* Figure out which endpoint has the isoc interface */
201 iface_desc = interface->cur_altsetting;
202 for(i = 0; i < iface_desc->desc.bNumEndpoints; i++){
203 endpoint = &iface_desc->endpoint[i].desc;
204 if(((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
205 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)){
206
207 /* we find our isoc in endpoint */
208 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
209 dev->max_pkt_size = (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
210 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
211 }
212 }
213 if(!(dev->isoc_in_endpointaddr)) {
214 printk("Could not locate isoc endpoint\n");
215 kfree(dev);
216 return -ENODEV;
217 }
218
219
Steven Toth265a6512008-04-18 21:34:00 -0300220 /* Power Up the bridge */
221 au0828_write(dev, REG_600, 1 << 4);
222
223 /* Bring up the GPIO's and supporting devices */
224 au0828_gpio_setup(dev);
225
226 /* I2C */
227 au0828_i2c_register(dev);
228
Steven Toth28930fa2008-03-29 19:53:07 -0300229 /* Setup */
230 au0828_card_setup(dev);
231
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300232 /* Analog TV */
233 au0828_analog_register(dev);
234
Steven Toth265a6512008-04-18 21:34:00 -0300235 /* Digital TV */
236 au0828_dvb_register(dev);
237
Steven Tothbc3c6132008-04-18 21:39:11 -0300238 printk(KERN_INFO "Registered device AU0828 [%s]\n",
Steven Toth9c26de552008-04-02 01:10:40 -0300239 au0828_boards[dev->board].name == NULL ? "Unset" :
Steven Toth265a6512008-04-18 21:34:00 -0300240 au0828_boards[dev->board].name);
241
242 return 0;
243}
244
245static struct usb_driver au0828_usb_driver = {
246 .name = DRIVER_NAME,
247 .probe = au0828_usb_probe,
248 .disconnect = au0828_usb_disconnect,
249 .id_table = au0828_usb_id_table,
250};
251
252static int __init au0828_init(void)
253{
254 int ret;
255
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300256 if (au0828_debug & 1)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300257 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300258
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300259 if (au0828_debug & 2)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300260 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300261
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300262 if (au0828_debug & 4)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300263 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300264
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300265 if (au0828_debug & 8)
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300266 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
267 __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300268
269 printk(KERN_INFO "au0828 driver loaded\n");
Steven Toth265a6512008-04-18 21:34:00 -0300270
271 ret = usb_register(&au0828_usb_driver);
272 if (ret)
Steven Tothbc3c6132008-04-18 21:39:11 -0300273 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
Steven Toth265a6512008-04-18 21:34:00 -0300274
275 return ret;
276}
277
278static void __exit au0828_exit(void)
279{
280 usb_deregister(&au0828_usb_driver);
281}
282
283module_init(au0828_init);
284module_exit(au0828_exit);
285
286MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
Steven Toth6d897612008-09-03 17:12:12 -0300287MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Toth265a6512008-04-18 21:34:00 -0300288MODULE_LICENSE("GPL");