xref: /openbmc/linux/drivers/media/usb/au0828/au0828-core.c (revision c43875f66140f5457f90fc5f6f6840c74b2762cd)
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 "au0828.h"
23 
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-common.h>
28 #include <linux/mutex.h>
29 
30 /* Due to enum tuner_pad_index */
31 #include <media/tuner.h>
32 
33 /*
34  * 1 = General debug messages
35  * 2 = USB handling
36  * 4 = I2C related
37  * 8 = Bridge related
38  * 16 = IR related
39  */
40 int au0828_debug;
41 module_param_named(debug, au0828_debug, int, 0644);
42 MODULE_PARM_DESC(debug,
43 		 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
44 
45 static unsigned int disable_usb_speed_check;
46 module_param(disable_usb_speed_check, int, 0444);
47 MODULE_PARM_DESC(disable_usb_speed_check,
48 		 "override min bandwidth requirement of 480M bps");
49 
50 #define _AU0828_BULKPIPE 0x03
51 #define _BULKPIPESIZE 0xffff
52 
53 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
54 			    u16 index);
55 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
56 	u16 index, unsigned char *cp, u16 size);
57 
58 /* USB Direction */
59 #define CMD_REQUEST_IN		0x00
60 #define CMD_REQUEST_OUT		0x01
61 
62 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
63 {
64 	u8 result = 0;
65 
66 	recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
67 	dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
68 
69 	return result;
70 }
71 
72 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
73 {
74 	dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
75 	return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
76 }
77 
78 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
79 	u16 index)
80 {
81 	int status = -ENODEV;
82 
83 	if (dev->usbdev) {
84 
85 		/* cp must be memory that has been allocated by kmalloc */
86 		status = usb_control_msg(dev->usbdev,
87 				usb_sndctrlpipe(dev->usbdev, 0),
88 				request,
89 				USB_DIR_OUT | USB_TYPE_VENDOR |
90 					USB_RECIP_DEVICE,
91 				value, index, NULL, 0, 1000);
92 
93 		status = min(status, 0);
94 
95 		if (status < 0) {
96 			pr_err("%s() Failed sending control message, error %d.\n",
97 				__func__, status);
98 		}
99 
100 	}
101 
102 	return status;
103 }
104 
105 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
106 	u16 index, unsigned char *cp, u16 size)
107 {
108 	int status = -ENODEV;
109 	mutex_lock(&dev->mutex);
110 	if (dev->usbdev) {
111 		status = usb_control_msg(dev->usbdev,
112 				usb_rcvctrlpipe(dev->usbdev, 0),
113 				request,
114 				USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
115 				value, index,
116 				dev->ctrlmsg, size, 1000);
117 
118 		status = min(status, 0);
119 
120 		if (status < 0) {
121 			pr_err("%s() Failed receiving control message, error %d.\n",
122 				__func__, status);
123 		}
124 
125 		/* the host controller requires heap allocated memory, which
126 		   is why we didn't just pass "cp" into usb_control_msg */
127 		memcpy(cp, dev->ctrlmsg, size);
128 	}
129 	mutex_unlock(&dev->mutex);
130 	return status;
131 }
132 
133 static void au0828_unregister_media_device(struct au0828_dev *dev)
134 {
135 
136 #ifdef CONFIG_MEDIA_CONTROLLER
137 	if (dev->media_dev) {
138 		media_device_unregister(dev->media_dev);
139 		media_device_cleanup(dev->media_dev);
140 		kfree(dev->media_dev);
141 		dev->media_dev = NULL;
142 	}
143 #endif
144 }
145 
146 void au0828_usb_release(struct au0828_dev *dev)
147 {
148 	au0828_unregister_media_device(dev);
149 
150 	/* I2C */
151 	au0828_i2c_unregister(dev);
152 
153 	kfree(dev);
154 }
155 
156 static void au0828_usb_disconnect(struct usb_interface *interface)
157 {
158 	struct au0828_dev *dev = usb_get_intfdata(interface);
159 
160 	dprintk(1, "%s()\n", __func__);
161 
162 	/* there is a small window after disconnect, before
163 	   dev->usbdev is NULL, for poll (e.g: IR) try to access
164 	   the device and fill the dmesg with error messages.
165 	   Set the status so poll routines can check and avoid
166 	   access after disconnect.
167 	*/
168 	dev->dev_state = DEV_DISCONNECTED;
169 
170 	au0828_rc_unregister(dev);
171 	/* Digital TV */
172 	au0828_dvb_unregister(dev);
173 
174 	usb_set_intfdata(interface, NULL);
175 	mutex_lock(&dev->mutex);
176 	dev->usbdev = NULL;
177 	mutex_unlock(&dev->mutex);
178 	if (au0828_analog_unregister(dev)) {
179 		/*
180 		 * No need to call au0828_usb_release() if V4L2 is enabled,
181 		 * as this is already called via au0828_usb_v4l2_release()
182 		 */
183 		return;
184 	}
185 	au0828_usb_release(dev);
186 }
187 
188 static int au0828_media_device_init(struct au0828_dev *dev,
189 				    struct usb_device *udev)
190 {
191 #ifdef CONFIG_MEDIA_CONTROLLER
192 	struct media_device *mdev;
193 
194 	if (!dev->board.name)
195 		mdev = v4l2_mc_usb_media_device_init(udev, "unknown au0828");
196 	else
197 		mdev = v4l2_mc_usb_media_device_init(udev, dev->board.name);
198 	if (!mdev)
199 		return -ENOMEM;
200 
201 	dev->media_dev = mdev;
202 #endif
203 	return 0;
204 }
205 
206 
207 static int au0828_usb_probe(struct usb_interface *interface,
208 	const struct usb_device_id *id)
209 {
210 	int ifnum;
211 	int retval = 0;
212 
213 	struct au0828_dev *dev;
214 	struct usb_device *usbdev = interface_to_usbdev(interface);
215 
216 	ifnum = interface->altsetting->desc.bInterfaceNumber;
217 
218 	if (ifnum != 0)
219 		return -ENODEV;
220 
221 	dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
222 		le16_to_cpu(usbdev->descriptor.idVendor),
223 		le16_to_cpu(usbdev->descriptor.idProduct),
224 		ifnum);
225 
226 	/*
227 	 * Make sure we have 480 Mbps of bandwidth, otherwise things like
228 	 * video stream wouldn't likely work, since 12 Mbps is generally
229 	 * not enough even for most Digital TV streams.
230 	 */
231 	if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
232 		pr_err("au0828: Device initialization failed.\n");
233 		pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
234 		return -ENODEV;
235 	}
236 
237 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
238 	if (dev == NULL) {
239 		pr_err("%s() Unable to allocate memory\n", __func__);
240 		return -ENOMEM;
241 	}
242 
243 	mutex_init(&dev->lock);
244 	mutex_lock(&dev->lock);
245 	mutex_init(&dev->mutex);
246 	mutex_init(&dev->dvb.lock);
247 	dev->usbdev = usbdev;
248 	dev->boardnr = id->driver_info;
249 	dev->board = au0828_boards[dev->boardnr];
250 
251 	/* Initialize the media controller */
252 	retval = au0828_media_device_init(dev, usbdev);
253 	if (retval) {
254 		pr_err("%s() au0828_media_device_init failed\n",
255 		       __func__);
256 		mutex_unlock(&dev->lock);
257 		kfree(dev);
258 		return retval;
259 	}
260 
261 	retval = au0828_v4l2_device_register(interface, dev);
262 	if (retval) {
263 		au0828_usb_v4l2_media_release(dev);
264 		mutex_unlock(&dev->lock);
265 		kfree(dev);
266 		return retval;
267 	}
268 
269 	/* Power Up the bridge */
270 	au0828_write(dev, REG_600, 1 << 4);
271 
272 	/* Bring up the GPIO's and supporting devices */
273 	au0828_gpio_setup(dev);
274 
275 	/* I2C */
276 	au0828_i2c_register(dev);
277 
278 	/* Setup */
279 	au0828_card_setup(dev);
280 
281 	/* Analog TV */
282 	retval = au0828_analog_register(dev, interface);
283 	if (retval) {
284 		pr_err("%s() au0282_dev_register failed to register on V4L2\n",
285 			__func__);
286 		goto done;
287 	}
288 
289 	/* Digital TV */
290 	retval = au0828_dvb_register(dev);
291 	if (retval)
292 		pr_err("%s() au0282_dev_register failed\n",
293 		       __func__);
294 
295 	/* Remote controller */
296 	au0828_rc_register(dev);
297 
298 	/*
299 	 * Store the pointer to the au0828_dev so it can be accessed in
300 	 * au0828_usb_disconnect
301 	 */
302 	usb_set_intfdata(interface, dev);
303 
304 	pr_info("Registered device AU0828 [%s]\n",
305 		dev->board.name == NULL ? "Unset" : dev->board.name);
306 
307 	mutex_unlock(&dev->lock);
308 
309 #ifdef CONFIG_MEDIA_CONTROLLER
310 	retval = media_device_register(dev->media_dev);
311 #endif
312 
313 done:
314 	if (retval < 0)
315 		au0828_usb_disconnect(interface);
316 
317 	return retval;
318 }
319 
320 static int au0828_suspend(struct usb_interface *interface,
321 				pm_message_t message)
322 {
323 	struct au0828_dev *dev = usb_get_intfdata(interface);
324 
325 	if (!dev)
326 		return 0;
327 
328 	pr_info("Suspend\n");
329 
330 	au0828_rc_suspend(dev);
331 	au0828_v4l2_suspend(dev);
332 	au0828_dvb_suspend(dev);
333 
334 	/* FIXME: should suspend also ATV/DTV */
335 
336 	return 0;
337 }
338 
339 static int au0828_resume(struct usb_interface *interface)
340 {
341 	struct au0828_dev *dev = usb_get_intfdata(interface);
342 	if (!dev)
343 		return 0;
344 
345 	pr_info("Resume\n");
346 
347 	/* Power Up the bridge */
348 	au0828_write(dev, REG_600, 1 << 4);
349 
350 	/* Bring up the GPIO's and supporting devices */
351 	au0828_gpio_setup(dev);
352 
353 	au0828_rc_resume(dev);
354 	au0828_v4l2_resume(dev);
355 	au0828_dvb_resume(dev);
356 
357 	/* FIXME: should resume also ATV/DTV */
358 
359 	return 0;
360 }
361 
362 static struct usb_driver au0828_usb_driver = {
363 	.name		= KBUILD_MODNAME,
364 	.probe		= au0828_usb_probe,
365 	.disconnect	= au0828_usb_disconnect,
366 	.id_table	= au0828_usb_id_table,
367 	.suspend	= au0828_suspend,
368 	.resume		= au0828_resume,
369 	.reset_resume	= au0828_resume,
370 };
371 
372 static int __init au0828_init(void)
373 {
374 	int ret;
375 
376 	if (au0828_debug & 1)
377 		pr_info("%s() Debugging is enabled\n", __func__);
378 
379 	if (au0828_debug & 2)
380 		pr_info("%s() USB Debugging is enabled\n", __func__);
381 
382 	if (au0828_debug & 4)
383 		pr_info("%s() I2C Debugging is enabled\n", __func__);
384 
385 	if (au0828_debug & 8)
386 		pr_info("%s() Bridge Debugging is enabled\n",
387 		       __func__);
388 
389 	if (au0828_debug & 16)
390 		pr_info("%s() IR Debugging is enabled\n",
391 		       __func__);
392 
393 	pr_info("au0828 driver loaded\n");
394 
395 	ret = usb_register(&au0828_usb_driver);
396 	if (ret)
397 		pr_err("usb_register failed, error = %d\n", ret);
398 
399 	return ret;
400 }
401 
402 static void __exit au0828_exit(void)
403 {
404 	usb_deregister(&au0828_usb_driver);
405 }
406 
407 module_init(au0828_init);
408 module_exit(au0828_exit);
409 
410 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
411 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
412 MODULE_LICENSE("GPL");
413 MODULE_VERSION("0.0.3");
414