a3410fc8f5f9154d403997a4fcbc1260cf6325db
[safe/jmp/linux-2.6] / 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 static unsigned int debug;
30 module_param(debug, int, 0644);
31 MODULE_PARM_DESC(debug, "enable debug messages");
32
33 #define _err(fmt, arg...)\
34         do {\
35                 printk(KERN_ERR DRIVER_NAME "/0: " fmt, ## arg);\
36         } while (0)
37
38 #define _info(fmt, arg...)\
39         do {\
40                 printk(KERN_INFO DRIVER_NAME "/0: " fmt, ## arg);\
41         } while (0)
42
43 #define _dbg(level, fmt, arg...)\
44         do {\
45                 if (debug >= level) \
46                         printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
47         } while (0)
48
49 #define _AU0828_BULKPIPE 0x03
50 #define _BULKPIPESIZE 0xffff
51
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);
56
57 /* USB Direction */
58 #define CMD_REQUEST_IN          0x00
59 #define CMD_REQUEST_OUT         0x01
60
61 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
62 {
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];
66 }
67
68 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
69 {
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);
72 }
73
74 static void cmd_msg_dump(struct au0828_dev *dev)
75 {
76         int i;
77
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",
81                         __FUNCTION__,
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]);
90 }
91
92 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
93         u16 index, unsigned char *cp, u16 size)
94 {
95         int status = -ENODEV;
96         mutex_lock(&dev->mutex);
97         if (dev->usbdev) {
98
99                 /* cp must be memory that has been allocated by kmalloc */
100                 status = usb_control_msg(dev->usbdev,
101                                 usb_sndctrlpipe(dev->usbdev, 0),
102                                 request,
103                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
104                                 value, index,
105                                 cp, size, 1000);
106
107                 status = min(status, 0);
108
109                 if (status < 0) {
110                         _err("%s() Failed sending control message, error %d.\n",
111                                 __FUNCTION__,
112                                 status);
113                 }
114
115         }
116         mutex_unlock(&dev->mutex);
117         return status;
118 }
119
120 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
121         u16 index, unsigned char *cp, u16 size)
122 {
123         int status = -ENODEV;
124         mutex_lock(&dev->mutex);
125         if (dev->usbdev) {
126
127                 memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
128
129                 /* cp must be memory that has been allocated by kmalloc */
130                 status = usb_control_msg(dev->usbdev,
131                                 usb_rcvctrlpipe(dev->usbdev, 0),
132                                 request,
133                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
134                                 value, index,
135                                 cp, size, 1000);
136
137                 status = min(status, 0);
138
139                 if (status < 0) {
140                         _err("%s() Failed receiving ctrl msg, error %d.\n",
141                                 __FUNCTION__,
142                                 status);
143                 }
144                 else
145                         if (debug > 4)
146                                 cmd_msg_dump(dev);
147         }
148         mutex_unlock(&dev->mutex);
149         return status;
150 }
151 static void au0828_usb_disconnect(struct usb_interface *interface)
152 {
153         struct au0828_dev *dev = usb_get_intfdata(interface);
154
155         _dbg(1,"%s()\n", __FUNCTION__);
156
157         /* Digital TV */
158         au0828_dvb_unregister(dev);
159
160         /* I2C */
161         au0828_i2c_unregister(dev);
162
163         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
173 static int au0828_usb_probe (struct usb_interface *interface,
174         const struct usb_device_id *id)
175 {
176         int ifnum;
177         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
185         _dbg(1,"%s() vendor id 0x%x device id 0x%x ifnum:%d\n",
186                 __FUNCTION__,
187                 le16_to_cpu(usbdev->descriptor.idVendor),
188                 le16_to_cpu(usbdev->descriptor.idProduct),
189                 ifnum);
190
191         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
192         if (dev == NULL) {
193                 _err("Unable to allocate memory\n");
194                 return -ENOMEM;
195         }
196
197         mutex_init(&dev->mutex);
198         mutex_init(&dev->dvb.lock);
199         dev->usbdev = usbdev;
200         dev->board = id->driver_info;
201
202         usb_set_intfdata(interface, dev);
203
204         /* Power Up the bridge */
205         au0828_write(dev, REG_600, 1 << 4);
206
207         /* Bring up the GPIO's and supporting devices */
208         au0828_gpio_setup(dev);
209
210         /* I2C */
211         au0828_i2c_register(dev);
212
213         /* Setup */
214         au0828_card_setup(dev);
215
216         /* Digital TV */
217         au0828_dvb_register(dev);
218
219         _info("Registered device AU0828 [%s]\n",
220                 au0828_boards[dev->board].name == NULL ? "Unset" :
221                 au0828_boards[dev->board].name);
222
223         return 0;
224 }
225
226 static struct usb_driver au0828_usb_driver = {
227         .name           = DRIVER_NAME,
228         .probe          = au0828_usb_probe,
229         .disconnect     = au0828_usb_disconnect,
230         .id_table       = au0828_usb_id_table,
231 };
232
233 static int __init au0828_init(void)
234 {
235         int ret;
236
237         _info("au0828 driver loaded\n");
238
239         ret = usb_register(&au0828_usb_driver);
240         if (ret)
241                 _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");