]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/au0828/au0828-core.c
V4L/DVB (11923): em28xx: Don't let device work unless connected to a high speed USB...
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / 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>
23#include <linux/videodev2.h>
24#include <media/v4l2-common.h>
25#include <linux/mutex.h>
26
27#include "au0828.h"
28
bc3c613c
ST
29/*
30 * 1 = General debug messages
31 * 2 = USB handling
32 * 4 = I2C related
33 * 8 = Bridge related
34 */
b33d24c4
AB
35int au0828_debug;
36module_param_named(debug, au0828_debug, int, 0644);
265a6510
ST
37MODULE_PARM_DESC(debug, "enable debug messages");
38
265a6510
ST
39#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);
b80f770a 54 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]);
265a6510
ST
55 return dev->ctrlmsg[0];
56}
57
58u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
59{
b80f770a 60 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
18d73c58
MCC
61 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
62 dev->ctrlmsg, 0);
265a6510
ST
63}
64
65static void cmd_msg_dump(struct au0828_dev *dev)
66{
67 int i;
68
18d73c58
MCC
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",
f07e8e4b 72 __func__,
265a6510
ST
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
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,
a8eb912c
ST
94 USB_DIR_OUT | USB_TYPE_VENDOR |
95 USB_RECIP_DEVICE,
265a6510
ST
96 value, index,
97 cp, size, 1000);
98
99 status = min(status, 0);
100
101 if (status < 0) {
bc3c613c 102 printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
f07e8e4b 103 __func__, status);
265a6510
ST
104 }
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) {
bc3c613c 131 printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
f07e8e4b 132 __func__, status);
18d73c58 133 } else
bc3c613c 134 cmd_msg_dump(dev);
265a6510
ST
135 }
136 mutex_unlock(&dev->mutex);
137 return status;
138}
a9c36aad 139
265a6510
ST
140static void au0828_usb_disconnect(struct usb_interface *interface)
141{
142 struct au0828_dev *dev = usb_get_intfdata(interface);
143
f07e8e4b 144 dprintk(1, "%s()\n", __func__);
265a6510
ST
145
146 /* Digital TV */
147 au0828_dvb_unregister(dev);
148
220be77c 149 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
f1add5b5 150 au0828_analog_unregister(dev);
8b2f0795 151
265a6510
ST
152 /* I2C */
153 au0828_i2c_unregister(dev);
154
b14667f3
DH
155 v4l2_device_unregister(&dev->v4l2_dev);
156
265a6510
ST
157 usb_set_intfdata(interface, NULL);
158
159 mutex_lock(&dev->mutex);
160 dev->usbdev = NULL;
161 mutex_unlock(&dev->mutex);
162
163 kfree(dev);
164
165}
166
18d73c58 167static int au0828_usb_probe(struct usb_interface *interface,
265a6510
ST
168 const struct usb_device_id *id)
169{
4bb68510 170 int ifnum, retval;
265a6510
ST
171 struct au0828_dev *dev;
172 struct usb_device *usbdev = interface_to_usbdev(interface);
173
174 ifnum = interface->altsetting->desc.bInterfaceNumber;
175
176 if (ifnum != 0)
177 return -ENODEV;
178
a9c36aad 179 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
265a6510
ST
180 le16_to_cpu(usbdev->descriptor.idVendor),
181 le16_to_cpu(usbdev->descriptor.idProduct),
182 ifnum);
183
184 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
185 if (dev == NULL) {
f07e8e4b 186 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
265a6510
ST
187 return -ENOMEM;
188 }
189
190 mutex_init(&dev->mutex);
191 mutex_init(&dev->dvb.lock);
192 dev->usbdev = usbdev;
f1add5b5 193 dev->boardnr = id->driver_info;
265a6510 194
b14667f3 195 /* Create the v4l2_device */
a4124aa9 196 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
b14667f3
DH
197 if (retval) {
198 printk(KERN_ERR "%s() v4l2_device_register failed\n",
199 __func__);
200 kfree(dev);
201 return -EIO;
202 }
203
265a6510
ST
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
28930fa9
ST
213 /* Setup */
214 au0828_card_setup(dev);
215
8b2f0795 216 /* Analog TV */
220be77c 217 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
fc4ce6cd 218 au0828_analog_register(dev, interface);
8b2f0795 219
265a6510
ST
220 /* Digital TV */
221 au0828_dvb_register(dev);
222
fe78a49c
DH
223 /* Store the pointer to the au0828_dev so it can be accessed in
224 au0828_usb_disconnect */
225 usb_set_intfdata(interface, dev);
226
bc3c613c 227 printk(KERN_INFO "Registered device AU0828 [%s]\n",
f1add5b5 228 dev->board.name == NULL ? "Unset" : dev->board.name);
265a6510
ST
229
230 return 0;
231}
232
233static struct usb_driver au0828_usb_driver = {
234 .name = DRIVER_NAME,
235 .probe = au0828_usb_probe,
236 .disconnect = au0828_usb_disconnect,
237 .id_table = au0828_usb_id_table,
238};
239
240static int __init au0828_init(void)
241{
242 int ret;
243
b33d24c4 244 if (au0828_debug & 1)
f07e8e4b 245 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
bc3c613c 246
b33d24c4 247 if (au0828_debug & 2)
f07e8e4b 248 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
bc3c613c 249
b33d24c4 250 if (au0828_debug & 4)
f07e8e4b 251 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
bc3c613c 252
b33d24c4 253 if (au0828_debug & 8)
f07e8e4b
MK
254 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
255 __func__);
bc3c613c
ST
256
257 printk(KERN_INFO "au0828 driver loaded\n");
265a6510
ST
258
259 ret = usb_register(&au0828_usb_driver);
260 if (ret)
bc3c613c 261 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
265a6510
ST
262
263 return ret;
264}
265
266static void __exit au0828_exit(void)
267{
268 usb_deregister(&au0828_usb_driver);
269}
270
271module_init(au0828_init);
272module_exit(au0828_exit);
273
274MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
6d897616 275MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
265a6510 276MODULE_LICENSE("GPL");