disable some mediatekl custom warnings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / usb / au0828 / au0828-core.c
CommitLineData
265a6510
ST
1/*
2 * Driver for the Auvitek USB bridge
3 *
6d897616 4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
265a6510
ST
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>
5a0e3ad6 23#include <linux/slab.h>
265a6510
ST
24#include <linux/videodev2.h>
25#include <media/v4l2-common.h>
26#include <linux/mutex.h>
27
28#include "au0828.h"
29
bc3c613c
ST
30/*
31 * 1 = General debug messages
32 * 2 = USB handling
33 * 4 = I2C related
34 * 8 = Bridge related
35 */
b33d24c4
AB
36int au0828_debug;
37module_param_named(debug, au0828_debug, int, 0644);
265a6510
ST
38MODULE_PARM_DESC(debug, "enable debug messages");
39
d6a9a430
DH
40static 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
265a6510
ST
45#define _AU0828_BULKPIPE 0x03
46#define _BULKPIPESIZE 0xffff
47
48static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
8ff63de6 49 u16 index);
265a6510
ST
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{
77fc2863
DH
59 u8 result = 0;
60
61 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
62 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
63
64 return result;
265a6510
ST
65}
66
67u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
68{
b80f770a 69 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
8ff63de6 70 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
265a6510
ST
71}
72
265a6510 73static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
8ff63de6 74 u16 index)
265a6510
ST
75{
76 int status = -ENODEV;
8ff63de6 77
265a6510
ST
78 if (dev->usbdev) {
79
80 /* cp must be memory that has been allocated by kmalloc */
81 status = usb_control_msg(dev->usbdev,
82 usb_sndctrlpipe(dev->usbdev, 0),
83 request,
a8eb912c
ST
84 USB_DIR_OUT | USB_TYPE_VENDOR |
85 USB_RECIP_DEVICE,
8ff63de6 86 value, index, NULL, 0, 1000);
265a6510
ST
87
88 status = min(status, 0);
89
90 if (status < 0) {
bc3c613c 91 printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
f07e8e4b 92 __func__, status);
265a6510
ST
93 }
94
95 }
8ff63de6 96
265a6510
ST
97 return status;
98}
99
100static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
101 u16 index, unsigned char *cp, u16 size)
102{
103 int status = -ENODEV;
104 mutex_lock(&dev->mutex);
105 if (dev->usbdev) {
265a6510
ST
106 status = usb_control_msg(dev->usbdev,
107 usb_rcvctrlpipe(dev->usbdev, 0),
108 request,
109 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
110 value, index,
77fc2863 111 dev->ctrlmsg, size, 1000);
265a6510
ST
112
113 status = min(status, 0);
114
115 if (status < 0) {
bc3c613c 116 printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
f07e8e4b 117 __func__, status);
77fc2863
DH
118 }
119
120 /* the host controller requires heap allocated memory, which
121 is why we didn't just pass "cp" into usb_control_msg */
122 memcpy(cp, dev->ctrlmsg, size);
265a6510
ST
123 }
124 mutex_unlock(&dev->mutex);
125 return status;
126}
a9c36aad 127
823beb7e 128static void au0828_usb_release(struct au0828_dev *dev)
265a6510 129{
265a6510
ST
130 /* I2C */
131 au0828_i2c_unregister(dev);
132
823beb7e
HV
133 kfree(dev);
134}
135
8a4e7866 136#ifdef CONFIG_VIDEO_AU0828_V4L2
823beb7e
HV
137static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
138{
139 struct au0828_dev *dev =
140 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
141
e8c26f45 142 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
b14667f3 143 v4l2_device_unregister(&dev->v4l2_dev);
823beb7e
HV
144 au0828_usb_release(dev);
145}
8a4e7866 146#endif
b14667f3 147
823beb7e
HV
148static void au0828_usb_disconnect(struct usb_interface *interface)
149{
150 struct au0828_dev *dev = usb_get_intfdata(interface);
151
152 dprintk(1, "%s()\n", __func__);
153
154 /* Digital TV */
155 au0828_dvb_unregister(dev);
265a6510 156
823beb7e 157 usb_set_intfdata(interface, NULL);
265a6510
ST
158 mutex_lock(&dev->mutex);
159 dev->usbdev = NULL;
160 mutex_unlock(&dev->mutex);
823beb7e
HV
161#ifdef CONFIG_VIDEO_AU0828_V4L2
162 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) {
163 au0828_analog_unregister(dev);
164 v4l2_device_disconnect(&dev->v4l2_dev);
165 v4l2_device_put(&dev->v4l2_dev);
166 return;
167 }
168#endif
169 au0828_usb_release(dev);
265a6510
ST
170}
171
18d73c58 172static int au0828_usb_probe(struct usb_interface *interface,
265a6510
ST
173 const struct usb_device_id *id)
174{
8a4e7866
MK
175 int ifnum;
176#ifdef CONFIG_VIDEO_AU0828_V4L2
177 int retval;
178#endif
265a6510
ST
179 struct au0828_dev *dev;
180 struct usb_device *usbdev = interface_to_usbdev(interface);
181
182 ifnum = interface->altsetting->desc.bInterfaceNumber;
183
184 if (ifnum != 0)
185 return -ENODEV;
186
a9c36aad 187 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
265a6510
ST
188 le16_to_cpu(usbdev->descriptor.idVendor),
189 le16_to_cpu(usbdev->descriptor.idProduct),
190 ifnum);
191
ee3436b8
DH
192 /*
193 * Make sure we have 480 Mbps of bandwidth, otherwise things like
194 * video stream wouldn't likely work, since 12 Mbps is generally
195 * not enough even for most Digital TV streams.
196 */
d6a9a430 197 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
ee3436b8
DH
198 printk(KERN_ERR "au0828: Device initialization failed.\n");
199 printk(KERN_ERR "au0828: Device must be connected to a "
200 "high-speed USB 2.0 port.\n");
201 return -ENODEV;
202 }
203
265a6510
ST
204 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
205 if (dev == NULL) {
f07e8e4b 206 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
265a6510
ST
207 return -ENOMEM;
208 }
209
549ee4df
DH
210 mutex_init(&dev->lock);
211 mutex_lock(&dev->lock);
265a6510
ST
212 mutex_init(&dev->mutex);
213 mutex_init(&dev->dvb.lock);
214 dev->usbdev = usbdev;
f1add5b5 215 dev->boardnr = id->driver_info;
265a6510 216
8a4e7866 217#ifdef CONFIG_VIDEO_AU0828_V4L2
823beb7e
HV
218 dev->v4l2_dev.release = au0828_usb_v4l2_release;
219
b14667f3 220 /* Create the v4l2_device */
a4124aa9 221 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
b14667f3 222 if (retval) {
e8c26f45 223 pr_err("%s() v4l2_device_register failed\n",
b14667f3 224 __func__);
549ee4df 225 mutex_unlock(&dev->lock);
b14667f3 226 kfree(dev);
e8c26f45 227 return retval;
b14667f3 228 }
e8c26f45
HV
229 /* This control handler will inherit the controls from au8522 */
230 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
231 if (retval) {
232 pr_err("%s() v4l2_ctrl_handler_init failed\n",
233 __func__);
234 mutex_unlock(&dev->lock);
235 kfree(dev);
236 return retval;
237 }
238 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
8a4e7866 239#endif
b14667f3 240
265a6510
ST
241 /* Power Up the bridge */
242 au0828_write(dev, REG_600, 1 << 4);
243
244 /* Bring up the GPIO's and supporting devices */
245 au0828_gpio_setup(dev);
246
247 /* I2C */
248 au0828_i2c_register(dev);
249
28930fa9
ST
250 /* Setup */
251 au0828_card_setup(dev);
252
8a4e7866 253#ifdef CONFIG_VIDEO_AU0828_V4L2
8b2f0795 254 /* Analog TV */
220be77c 255 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
fc4ce6cd 256 au0828_analog_register(dev, interface);
8a4e7866 257#endif
8b2f0795 258
265a6510
ST
259 /* Digital TV */
260 au0828_dvb_register(dev);
261
fe78a49c
DH
262 /* Store the pointer to the au0828_dev so it can be accessed in
263 au0828_usb_disconnect */
264 usb_set_intfdata(interface, dev);
265
bc3c613c 266 printk(KERN_INFO "Registered device AU0828 [%s]\n",
f1add5b5 267 dev->board.name == NULL ? "Unset" : dev->board.name);
265a6510 268
549ee4df
DH
269 mutex_unlock(&dev->lock);
270
265a6510
ST
271 return 0;
272}
273
274static struct usb_driver au0828_usb_driver = {
275 .name = DRIVER_NAME,
276 .probe = au0828_usb_probe,
277 .disconnect = au0828_usb_disconnect,
278 .id_table = au0828_usb_id_table,
279};
280
281static int __init au0828_init(void)
282{
283 int ret;
284
b33d24c4 285 if (au0828_debug & 1)
f07e8e4b 286 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
bc3c613c 287
b33d24c4 288 if (au0828_debug & 2)
f07e8e4b 289 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
bc3c613c 290
b33d24c4 291 if (au0828_debug & 4)
f07e8e4b 292 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
bc3c613c 293
b33d24c4 294 if (au0828_debug & 8)
f07e8e4b
MK
295 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
296 __func__);
bc3c613c
ST
297
298 printk(KERN_INFO "au0828 driver loaded\n");
265a6510
ST
299
300 ret = usb_register(&au0828_usb_driver);
301 if (ret)
bc3c613c 302 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
265a6510
ST
303
304 return ret;
305}
306
307static void __exit au0828_exit(void)
308{
309 usb_deregister(&au0828_usb_driver);
310}
311
312module_init(au0828_init);
313module_exit(au0828_exit);
314
315MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
6d897616 316MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
265a6510 317MODULE_LICENSE("GPL");
1990d50b 318MODULE_VERSION("0.0.2");