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