bridge: don't allow setting hello time to zero
[pandora-kernel.git] / drivers / media / video / au0828 / au0828-core.c
1 /*
2  *  Driver for the Auvitek USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
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
29 /*
30  * 1 = General debug messages
31  * 2 = USB handling
32  * 4 = I2C related
33  * 8 = Bridge related
34  */
35 int au0828_debug;
36 module_param_named(debug, au0828_debug, int, 0644);
37 MODULE_PARM_DESC(debug, "enable debug messages");
38
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);
54         dprintk(8, "%s(0x%x) = 0x%x\n", __func__, reg, dev->ctrlmsg[0]);
55         return dev->ctrlmsg[0];
56 }
57
58 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
59 {
60         dprintk(8, "%s(0x%x, 0x%x)\n", __func__, reg, val);
61         return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
62                                 dev->ctrlmsg, 0);
63 }
64
65 static void cmd_msg_dump(struct au0828_dev *dev)
66 {
67         int i;
68
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",
72                         __func__,
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,
94                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
95                                 value, index,
96                                 cp, size, 1000);
97
98                 status = min(status, 0);
99
100                 if (status < 0) {
101                         printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
102                                 __func__, status);
103                 }
104
105         }
106         mutex_unlock(&dev->mutex);
107         return status;
108 }
109
110 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
111         u16 index, unsigned char *cp, u16 size)
112 {
113         int status = -ENODEV;
114         mutex_lock(&dev->mutex);
115         if (dev->usbdev) {
116
117                 memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
118
119                 /* cp must be memory that has been allocated by kmalloc */
120                 status = usb_control_msg(dev->usbdev,
121                                 usb_rcvctrlpipe(dev->usbdev, 0),
122                                 request,
123                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124                                 value, index,
125                                 cp, size, 1000);
126
127                 status = min(status, 0);
128
129                 if (status < 0) {
130                         printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
131                                 __func__, status);
132                 } else
133                         cmd_msg_dump(dev);
134         }
135         mutex_unlock(&dev->mutex);
136         return status;
137 }
138
139 static void au0828_usb_disconnect(struct usb_interface *interface)
140 {
141         struct au0828_dev *dev = usb_get_intfdata(interface);
142
143         dprintk(1, "%s()\n", __func__);
144
145         /* Digital TV */
146         au0828_dvb_unregister(dev);
147
148         /* I2C */
149         au0828_i2c_unregister(dev);
150
151         usb_set_intfdata(interface, NULL);
152
153         mutex_lock(&dev->mutex);
154         dev->usbdev = NULL;
155         mutex_unlock(&dev->mutex);
156
157         kfree(dev);
158
159 }
160
161 static int au0828_usb_probe(struct usb_interface *interface,
162         const struct usb_device_id *id)
163 {
164         int ifnum;
165         struct au0828_dev *dev;
166         struct usb_device *usbdev = interface_to_usbdev(interface);
167
168         ifnum = interface->altsetting->desc.bInterfaceNumber;
169
170         if (ifnum != 0)
171                 return -ENODEV;
172
173         dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
174                 le16_to_cpu(usbdev->descriptor.idVendor),
175                 le16_to_cpu(usbdev->descriptor.idProduct),
176                 ifnum);
177
178         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
179         if (dev == NULL) {
180                 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
181                 return -ENOMEM;
182         }
183
184         mutex_init(&dev->mutex);
185         mutex_init(&dev->dvb.lock);
186         dev->usbdev = usbdev;
187         dev->board = id->driver_info;
188
189         usb_set_intfdata(interface, dev);
190
191         /* Power Up the bridge */
192         au0828_write(dev, REG_600, 1 << 4);
193
194         /* Bring up the GPIO's and supporting devices */
195         au0828_gpio_setup(dev);
196
197         /* I2C */
198         au0828_i2c_register(dev);
199
200         /* Setup */
201         au0828_card_setup(dev);
202
203         /* Digital TV */
204         au0828_dvb_register(dev);
205
206         printk(KERN_INFO "Registered device AU0828 [%s]\n",
207                 au0828_boards[dev->board].name == NULL ? "Unset" :
208                 au0828_boards[dev->board].name);
209
210         return 0;
211 }
212
213 static struct usb_driver au0828_usb_driver = {
214         .name           = DRIVER_NAME,
215         .probe          = au0828_usb_probe,
216         .disconnect     = au0828_usb_disconnect,
217         .id_table       = au0828_usb_id_table,
218 };
219
220 static int __init au0828_init(void)
221 {
222         int ret;
223
224         if (au0828_debug & 1)
225                 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
226
227         if (au0828_debug & 2)
228                 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
229
230         if (au0828_debug & 4)
231                 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
232
233         if (au0828_debug & 8)
234                 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
235                        __func__);
236
237         printk(KERN_INFO "au0828 driver loaded\n");
238
239         ret = usb_register(&au0828_usb_driver);
240         if (ret)
241                 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
242
243         return ret;
244 }
245
246 static void __exit au0828_exit(void)
247 {
248         usb_deregister(&au0828_usb_driver);
249 }
250
251 module_init(au0828_init);
252 module_exit(au0828_exit);
253
254 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
255 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
256 MODULE_LICENSE("GPL");