1 /*
2  * STK1160 driver
3  *
4  * Copyright (C) 2012 Ezequiel Garcia
5  * <elezegarcia--a.t--gmail.com>
6  *
7  * Based on Easycap driver by R.M. Thomas
8  *	Copyright (C) 2010 R.M. Thomas
9  *	<rmthomas--a.t--sciolus.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * TODO:
22  *
23  * 1. Support stream at lower speed: lower frame rate or lower frame size.
24  *
25  */
26 
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 
33 #include <linux/usb.h>
34 #include <linux/mm.h>
35 #include <linux/vmalloc.h>
36 #include <media/i2c/saa7115.h>
37 
38 #include "stk1160.h"
39 #include "stk1160-reg.h"
40 
41 static unsigned int input;
42 module_param(input, int, 0644);
43 MODULE_PARM_DESC(input, "Set default input");
44 
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Ezequiel Garcia");
47 MODULE_DESCRIPTION("STK1160 driver");
48 
49 /* Devices supported by this driver */
50 static const struct usb_device_id stk1160_id_table[] = {
51 	{ USB_DEVICE(0x05e1, 0x0408) },
52 	{ }
53 };
54 MODULE_DEVICE_TABLE(usb, stk1160_id_table);
55 
56 /* saa7113 I2C address */
57 static unsigned short saa7113_addrs[] = {
58 	0x4a >> 1,
59 	I2C_CLIENT_END
60 };
61 
62 /*
63  * Read/Write stk registers
64  */
65 int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value)
66 {
67 	int ret;
68 	int pipe = usb_rcvctrlpipe(dev->udev, 0);
69 	u8 *buf;
70 
71 	*value = 0;
72 
73 	buf = kmalloc(sizeof(u8), GFP_KERNEL);
74 	if (!buf)
75 		return -ENOMEM;
76 	ret = usb_control_msg(dev->udev, pipe, 0x00,
77 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78 			0x00, reg, buf, sizeof(u8), HZ);
79 	if (ret < 0) {
80 		stk1160_err("read failed on reg 0x%x (%d)\n",
81 			reg, ret);
82 		kfree(buf);
83 		return ret;
84 	}
85 
86 	*value = *buf;
87 	kfree(buf);
88 	return 0;
89 }
90 
91 int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value)
92 {
93 	int ret;
94 	int pipe = usb_sndctrlpipe(dev->udev, 0);
95 
96 	ret =  usb_control_msg(dev->udev, pipe, 0x01,
97 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
98 			value, reg, NULL, 0, HZ);
99 	if (ret < 0) {
100 		stk1160_err("write failed on reg 0x%x (%d)\n",
101 			reg, ret);
102 		return ret;
103 	}
104 
105 	return 0;
106 }
107 
108 void stk1160_select_input(struct stk1160 *dev)
109 {
110 	int route;
111 	static const u8 gctrl[] = {
112 		0x98, 0x90, 0x88, 0x80, 0x98
113 	};
114 
115 	if (dev->ctl_input == STK1160_SVIDEO_INPUT)
116 		route = SAA7115_SVIDEO3;
117 	else
118 		route = SAA7115_COMPOSITE0;
119 
120 	if (dev->ctl_input < ARRAY_SIZE(gctrl)) {
121 		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
122 				route, 0, 0);
123 		stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]);
124 	}
125 }
126 
127 /* TODO: We should break this into pieces */
128 static void stk1160_reg_reset(struct stk1160 *dev)
129 {
130 	int i;
131 
132 	static const struct regval ctl[] = {
133 		{STK1160_GCTRL+2, 0x0078},
134 
135 		{STK1160_RMCTL+1, 0x0000},
136 		{STK1160_RMCTL+3, 0x0002},
137 
138 		{STK1160_PLLSO,   0x0010},
139 		{STK1160_PLLSO+1, 0x0000},
140 		{STK1160_PLLSO+2, 0x0014},
141 		{STK1160_PLLSO+3, 0x000E},
142 
143 		{STK1160_PLLFD,   0x0046},
144 
145 		/* Timing generator setup */
146 		{STK1160_TIGEN,   0x0012},
147 		{STK1160_TICTL,   0x002D},
148 		{STK1160_TICTL+1, 0x0001},
149 		{STK1160_TICTL+2, 0x0000},
150 		{STK1160_TICTL+3, 0x0000},
151 		{STK1160_TIGEN,   0x0080},
152 
153 		{0xffff, 0xffff}
154 	};
155 
156 	for (i = 0; ctl[i].reg != 0xffff; i++)
157 		stk1160_write_reg(dev, ctl[i].reg, ctl[i].val);
158 }
159 
160 static void stk1160_release(struct v4l2_device *v4l2_dev)
161 {
162 	struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev);
163 
164 	stk1160_dbg("releasing all resources\n");
165 
166 	stk1160_i2c_unregister(dev);
167 
168 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
169 	v4l2_device_unregister(&dev->v4l2_dev);
170 	kfree(dev->alt_max_pkt_size);
171 	kfree(dev);
172 }
173 
174 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
175 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
176 
177 /*
178  * Scan usb interface and populate max_pkt_size array
179  * with information on each alternate setting.
180  * The array should be allocated by the caller.
181  */
182 static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
183 		unsigned int *max_pkt_size)
184 {
185 	int i, e, sizedescr, size, ifnum;
186 	const struct usb_endpoint_descriptor *desc;
187 
188 	bool has_video = false, has_audio = false;
189 	const char *speed;
190 
191 	ifnum = intf->altsetting[0].desc.bInterfaceNumber;
192 
193 	/* Get endpoints */
194 	for (i = 0; i < intf->num_altsetting; i++) {
195 
196 		for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
197 
198 			/* This isn't clear enough, at least to me */
199 			desc = &intf->altsetting[i].endpoint[e].desc;
200 			sizedescr = le16_to_cpu(desc->wMaxPacketSize);
201 			size = sizedescr & 0x7ff;
202 
203 			if (udev->speed == USB_SPEED_HIGH)
204 				size = size * hb_mult(sizedescr);
205 
206 			if (usb_endpoint_xfer_isoc(desc) &&
207 			    usb_endpoint_dir_in(desc)) {
208 				switch (desc->bEndpointAddress) {
209 				case STK1160_EP_AUDIO:
210 					has_audio = true;
211 					break;
212 				case STK1160_EP_VIDEO:
213 					has_video = true;
214 					max_pkt_size[i] = size;
215 					break;
216 				}
217 			}
218 		}
219 	}
220 
221 	/* Is this even possible? */
222 	if (!(has_audio || has_video)) {
223 		dev_err(&udev->dev, "no audio or video endpoints found\n");
224 		return -ENODEV;
225 	}
226 
227 	switch (udev->speed) {
228 	case USB_SPEED_LOW:
229 		speed = "1.5";
230 		break;
231 	case USB_SPEED_FULL:
232 		speed = "12";
233 		break;
234 	case USB_SPEED_HIGH:
235 		speed = "480";
236 		break;
237 	default:
238 		speed = "unknown";
239 	}
240 
241 	dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
242 		udev->manufacturer ? udev->manufacturer : "",
243 		udev->product ? udev->product : "",
244 		speed,
245 		le16_to_cpu(udev->descriptor.idVendor),
246 		le16_to_cpu(udev->descriptor.idProduct),
247 		ifnum,
248 		intf->altsetting->desc.bInterfaceNumber);
249 
250 	/* This should never happen, since we rejected audio interfaces */
251 	if (has_audio)
252 		dev_warn(&udev->dev, "audio interface %d found.\n\
253 				This is not implemented by this driver,\
254 				you should use snd-usb-audio instead\n", ifnum);
255 
256 	if (has_video)
257 		dev_info(&udev->dev, "video interface %d found\n",
258 				ifnum);
259 
260 	/*
261 	 * Make sure we have 480 Mbps of bandwidth, otherwise things like
262 	 * video stream wouldn't likely work, since 12 Mbps is generally
263 	 * not enough even for most streams.
264 	 */
265 	if (udev->speed != USB_SPEED_HIGH)
266 		dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
267 				You may not be able to stream video smoothly\n");
268 
269 	return 0;
270 }
271 
272 static int stk1160_probe(struct usb_interface *interface,
273 		const struct usb_device_id *id)
274 {
275 	int rc = 0;
276 
277 	unsigned int *alt_max_pkt_size;	/* array of wMaxPacketSize */
278 	struct usb_device *udev;
279 	struct stk1160 *dev;
280 
281 	udev = interface_to_usbdev(interface);
282 
283 	/*
284 	 * Since usb audio class is supported by snd-usb-audio,
285 	 * we reject audio interface.
286 	 */
287 	if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
288 		return -ENODEV;
289 
290 	/* Alloc an array for all possible max_pkt_size */
291 	alt_max_pkt_size = kmalloc(sizeof(alt_max_pkt_size[0]) *
292 			interface->num_altsetting, GFP_KERNEL);
293 	if (alt_max_pkt_size == NULL)
294 		return -ENOMEM;
295 
296 	/*
297 	 * Scan usb posibilities and populate alt_max_pkt_size array.
298 	 * Also, check if device speed is fast enough.
299 	 */
300 	rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
301 	if (rc < 0) {
302 		kfree(alt_max_pkt_size);
303 		return rc;
304 	}
305 
306 	dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL);
307 	if (dev == NULL) {
308 		kfree(alt_max_pkt_size);
309 		return -ENOMEM;
310 	}
311 
312 	dev->alt_max_pkt_size = alt_max_pkt_size;
313 	dev->udev = udev;
314 	dev->num_alt = interface->num_altsetting;
315 	dev->ctl_input = input;
316 
317 	/* We save struct device for debug purposes only */
318 	dev->dev = &interface->dev;
319 
320 	usb_set_intfdata(interface, dev);
321 
322 	/* initialize videobuf2 stuff */
323 	rc = stk1160_vb2_setup(dev);
324 	if (rc < 0)
325 		goto free_err;
326 
327 	/*
328 	 * There is no need to take any locks here in probe
329 	 * because we register the device node as the *last* thing.
330 	 */
331 	spin_lock_init(&dev->buf_lock);
332 	mutex_init(&dev->v4l_lock);
333 	mutex_init(&dev->vb_queue_lock);
334 
335 	rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
336 	if (rc) {
337 		stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
338 		goto free_err;
339 	}
340 
341 	/*
342 	 * We obtain a v4l2_dev but defer
343 	 * registration of video device node as the last thing.
344 	 * There is no need to set the name if we give a device struct
345 	 */
346 	dev->v4l2_dev.release = stk1160_release;
347 	dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
348 	rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
349 	if (rc) {
350 		stk1160_err("v4l2_device_register failed (%d)\n", rc);
351 		goto free_ctrl;
352 	}
353 
354 	rc = stk1160_i2c_register(dev);
355 	if (rc < 0)
356 		goto unreg_v4l2;
357 
358 	/*
359 	 * To the best of my knowledge stk1160 boards only have
360 	 * saa7113, but it doesn't hurt to support them all.
361 	 */
362 	dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
363 		"saa7115_auto", 0, saa7113_addrs);
364 
365 	/* i2c reset saa711x */
366 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
367 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
368 
369 	/* reset stk1160 to default values */
370 	stk1160_reg_reset(dev);
371 
372 	/* select default input */
373 	stk1160_select_input(dev);
374 
375 	stk1160_ac97_setup(dev);
376 
377 	rc = stk1160_video_register(dev);
378 	if (rc < 0)
379 		goto unreg_i2c;
380 
381 	return 0;
382 
383 unreg_i2c:
384 	stk1160_i2c_unregister(dev);
385 unreg_v4l2:
386 	v4l2_device_unregister(&dev->v4l2_dev);
387 free_ctrl:
388 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
389 free_err:
390 	kfree(alt_max_pkt_size);
391 	kfree(dev);
392 
393 	return rc;
394 }
395 
396 static void stk1160_disconnect(struct usb_interface *interface)
397 {
398 	struct stk1160 *dev;
399 
400 	dev = usb_get_intfdata(interface);
401 	usb_set_intfdata(interface, NULL);
402 
403 	/*
404 	 * Wait until all current v4l2 operation are finished
405 	 * then deallocate resources
406 	 */
407 	mutex_lock(&dev->vb_queue_lock);
408 	mutex_lock(&dev->v4l_lock);
409 
410 	/* Here is the only place where isoc get released */
411 	stk1160_uninit_isoc(dev);
412 
413 	stk1160_clear_queue(dev);
414 
415 	video_unregister_device(&dev->vdev);
416 	v4l2_device_disconnect(&dev->v4l2_dev);
417 
418 	/* This way current users can detect device is gone */
419 	dev->udev = NULL;
420 
421 	mutex_unlock(&dev->v4l_lock);
422 	mutex_unlock(&dev->vb_queue_lock);
423 
424 	/*
425 	 * This calls stk1160_release if it's the last reference.
426 	 * therwise, release is posponed until there are no users left.
427 	 */
428 	v4l2_device_put(&dev->v4l2_dev);
429 }
430 
431 static struct usb_driver stk1160_usb_driver = {
432 	.name = "stk1160",
433 	.id_table = stk1160_id_table,
434 	.probe = stk1160_probe,
435 	.disconnect = stk1160_disconnect,
436 };
437 
438 module_usb_driver(stk1160_usb_driver);
439