• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
3  *
4  * Copyright (C) 2006 Nicolas VIVIEN
5  * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
6  *
7  * Some parts are inspired from cafe_ccic.c
8  * Copyright 2006-2007 Jonathan Corbet
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 
31 #include <linux/dmi.h>
32 #include <linux/usb.h>
33 #include <linux/mm.h>
34 #include <linux/vmalloc.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 
40 #include "stk-webcam.h"
41 
42 
43 static int hflip = -1;
44 module_param(hflip, int, 0444);
45 MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
46 
47 static int vflip = -1;
48 module_param(vflip, int, 0444);
49 MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
50 
51 static int debug;
52 module_param(debug, int, 0444);
53 MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
54 
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
57 MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
58 
59 /* Some cameras have audio interfaces, we aren't interested in those */
60 static struct usb_device_id stkwebcam_table[] = {
61 	{ USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
62 	{ USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
63 	{ }
64 };
65 MODULE_DEVICE_TABLE(usb, stkwebcam_table);
66 
67 /*
68  * The stk webcam laptop module is mounted upside down in some laptops :(
69  *
70  * Some background information (thanks to Hans de Goede for providing this):
71  *
72  * 1) Once upon a time the stkwebcam driver was written
73  *
74  * 2) The webcam in question was used mostly in Asus laptop models, including
75  * the laptop of the original author of the driver, and in these models, in
76  * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
77  * they mounted the webcam-module the wrong way up. So the hflip and vflip
78  * module options were given a default value of 1 (the correct value for
79  * upside down mounted models)
80  *
81  * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
82  * where the module was actually mounted the right way up, and thus showed
83  * upside down under Linux. So now I was facing the choice of 2 options:
84  *
85  * a) Add a not-upside-down list to stkwebcam, which overrules the default.
86  *
87  * b) Do it like all the other drivers do, and make the default right for
88  *    cams mounted the proper way and add an upside-down model list, with
89  *    models where we need to flip-by-default.
90  *
91  * Despite knowing that going b) would cause a period of pain where we were
92  * building the table I opted to go for option b), since a) is just too ugly,
93  * and worse different from how every other driver does it leading to
94  * confusion in the long run. This change was made in kernel 3.6.
95  *
96  * So for any user report about upside-down images since kernel 3.6 ask them
97  * to provide the output of 'sudo dmidecode' so the laptop can be added in
98  * the table below.
99  */
100 static const struct dmi_system_id stk_upside_down_dmi_table[] = {
101 	{
102 		.ident = "ASUS G1",
103 		.matches = {
104 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
105 			DMI_MATCH(DMI_PRODUCT_NAME, "G1")
106 		}
107 	}, {
108 		.ident = "ASUS F3JC",
109 		.matches = {
110 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
111 			DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
112 		}
113 	},
114 	{
115 		.ident = "T12Rg-H",
116 		.matches = {
117 			DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
118 			DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
119 		}
120 	},
121 	{}
122 };
123 
124 
125 /*
126  * Basic stuff
127  */
stk_camera_write_reg(struct stk_camera * dev,u16 index,u8 value)128 int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
129 {
130 	struct usb_device *udev = dev->udev;
131 	int ret;
132 
133 	ret =  usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
134 			0x01,
135 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
136 			value,
137 			index,
138 			NULL,
139 			0,
140 			500);
141 	if (ret < 0)
142 		return ret;
143 	else
144 		return 0;
145 }
146 
stk_camera_read_reg(struct stk_camera * dev,u16 index,int * value)147 int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
148 {
149 	struct usb_device *udev = dev->udev;
150 	int ret;
151 
152 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
153 			0x00,
154 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
155 			0x00,
156 			index,
157 			(u8 *) value,
158 			sizeof(u8),
159 			500);
160 	if (ret < 0)
161 		return ret;
162 	else
163 		return 0;
164 }
165 
stk_start_stream(struct stk_camera * dev)166 static int stk_start_stream(struct stk_camera *dev)
167 {
168 	int value;
169 	int i, ret;
170 	int value_116, value_117;
171 
172 	if (!is_present(dev))
173 		return -ENODEV;
174 	if (!is_memallocd(dev) || !is_initialised(dev)) {
175 		STK_ERROR("FIXME: Buffers are not allocated\n");
176 		return -EFAULT;
177 	}
178 	ret = usb_set_interface(dev->udev, 0, 5);
179 
180 	if (ret < 0)
181 		STK_ERROR("usb_set_interface failed !\n");
182 	if (stk_sensor_wakeup(dev))
183 		STK_ERROR("error awaking the sensor\n");
184 
185 	stk_camera_read_reg(dev, 0x0116, &value_116);
186 	stk_camera_read_reg(dev, 0x0117, &value_117);
187 
188 	stk_camera_write_reg(dev, 0x0116, 0x0000);
189 	stk_camera_write_reg(dev, 0x0117, 0x0000);
190 
191 	stk_camera_read_reg(dev, 0x0100, &value);
192 	stk_camera_write_reg(dev, 0x0100, value | 0x80);
193 
194 	stk_camera_write_reg(dev, 0x0116, value_116);
195 	stk_camera_write_reg(dev, 0x0117, value_117);
196 	for (i = 0; i < MAX_ISO_BUFS; i++) {
197 		if (dev->isobufs[i].urb) {
198 			ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
199 			atomic_inc(&dev->urbs_used);
200 			if (ret)
201 				return ret;
202 		}
203 	}
204 	set_streaming(dev);
205 	return 0;
206 }
207 
stk_stop_stream(struct stk_camera * dev)208 static int stk_stop_stream(struct stk_camera *dev)
209 {
210 	int value;
211 	int i;
212 	if (is_present(dev)) {
213 		stk_camera_read_reg(dev, 0x0100, &value);
214 		stk_camera_write_reg(dev, 0x0100, value & ~0x80);
215 		if (dev->isobufs != NULL) {
216 			for (i = 0; i < MAX_ISO_BUFS; i++) {
217 				if (dev->isobufs[i].urb)
218 					usb_kill_urb(dev->isobufs[i].urb);
219 			}
220 		}
221 		unset_streaming(dev);
222 
223 		if (usb_set_interface(dev->udev, 0, 0))
224 			STK_ERROR("usb_set_interface failed !\n");
225 		if (stk_sensor_sleep(dev))
226 			STK_ERROR("error suspending the sensor\n");
227 	}
228 	return 0;
229 }
230 
231 /*
232  * This seems to be the shortest init sequence we
233  * must do in order to find the sensor
234  * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
235  * is also reset. Maybe powers down it?
236  * Rest of values don't make a difference
237  */
238 
239 static struct regval stk1125_initvals[] = {
240 	/*TODO: What means this sequence? */
241 	{0x0000, 0x24},
242 	{0x0100, 0x21},
243 	{0x0002, 0x68},
244 	{0x0003, 0x80},
245 	{0x0005, 0x00},
246 	{0x0007, 0x03},
247 	{0x000d, 0x00},
248 	{0x000f, 0x02},
249 	{0x0300, 0x12},
250 	{0x0350, 0x41},
251 	{0x0351, 0x00},
252 	{0x0352, 0x00},
253 	{0x0353, 0x00},
254 	{0x0018, 0x10},
255 	{0x0019, 0x00},
256 	{0x001b, 0x0e},
257 	{0x001c, 0x46},
258 	{0x0300, 0x80},
259 	{0x001a, 0x04},
260 	{0x0110, 0x00},
261 	{0x0111, 0x00},
262 	{0x0112, 0x00},
263 	{0x0113, 0x00},
264 
265 	{0xffff, 0xff},
266 };
267 
268 
stk_initialise(struct stk_camera * dev)269 static int stk_initialise(struct stk_camera *dev)
270 {
271 	struct regval *rv;
272 	int ret;
273 	if (!is_present(dev))
274 		return -ENODEV;
275 	if (is_initialised(dev))
276 		return 0;
277 	rv = stk1125_initvals;
278 	while (rv->reg != 0xffff) {
279 		ret = stk_camera_write_reg(dev, rv->reg, rv->val);
280 		if (ret)
281 			return ret;
282 		rv++;
283 	}
284 	if (stk_sensor_init(dev) == 0) {
285 		set_initialised(dev);
286 		return 0;
287 	} else
288 		return -1;
289 }
290 
291 /* *********************************************** */
292 /*
293  * This function is called as an URB transfert is complete (Isochronous pipe).
294  * So, the traitement is done in interrupt time, so it has be fast, not crash,
295  * and not stall. Neat.
296  */
stk_isoc_handler(struct urb * urb)297 static void stk_isoc_handler(struct urb *urb)
298 {
299 	int i;
300 	int ret;
301 	int framelen;
302 	unsigned long flags;
303 
304 	unsigned char *fill = NULL;
305 	unsigned char *iso_buf = NULL;
306 
307 	struct stk_camera *dev;
308 	struct stk_sio_buffer *fb;
309 
310 	dev = (struct stk_camera *) urb->context;
311 
312 	if (dev == NULL) {
313 		STK_ERROR("isoc_handler called with NULL device !\n");
314 		return;
315 	}
316 
317 	if (urb->status == -ENOENT || urb->status == -ECONNRESET
318 		|| urb->status == -ESHUTDOWN) {
319 		atomic_dec(&dev->urbs_used);
320 		return;
321 	}
322 
323 	spin_lock_irqsave(&dev->spinlock, flags);
324 
325 	if (urb->status != -EINPROGRESS && urb->status != 0) {
326 		STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
327 		goto resubmit;
328 	}
329 
330 	if (list_empty(&dev->sio_avail)) {
331 		/*FIXME Stop streaming after a while */
332 		(void) (printk_ratelimit() &&
333 		STK_ERROR("isoc_handler without available buffer!\n"));
334 		goto resubmit;
335 	}
336 	fb = list_first_entry(&dev->sio_avail,
337 			struct stk_sio_buffer, list);
338 	fill = fb->buffer + fb->v4lbuf.bytesused;
339 
340 	for (i = 0; i < urb->number_of_packets; i++) {
341 		if (urb->iso_frame_desc[i].status != 0) {
342 			if (urb->iso_frame_desc[i].status != -EXDEV)
343 				STK_ERROR("Frame %d has error %d\n", i,
344 					urb->iso_frame_desc[i].status);
345 			continue;
346 		}
347 		framelen = urb->iso_frame_desc[i].actual_length;
348 		iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
349 
350 		if (framelen <= 4)
351 			continue; /* no data */
352 
353 		/*
354 		 * we found something informational from there
355 		 * the isoc frames have to type of headers
356 		 * type1: 00 xx 00 00 or 20 xx 00 00
357 		 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
358 		 * xx is a sequencer which has never been seen over 0x3f
359 		 * imho data written down looks like bayer, i see similarities
360 		 * after every 640 bytes
361 		 */
362 		if (*iso_buf & 0x80) {
363 			framelen -= 8;
364 			iso_buf += 8;
365 			/* This marks a new frame */
366 			if (fb->v4lbuf.bytesused != 0
367 				&& fb->v4lbuf.bytesused != dev->frame_size) {
368 				(void) (printk_ratelimit() &&
369 				STK_ERROR("frame %d, "
370 					"bytesused=%d, skipping\n",
371 					i, fb->v4lbuf.bytesused));
372 				fb->v4lbuf.bytesused = 0;
373 				fill = fb->buffer;
374 			} else if (fb->v4lbuf.bytesused == dev->frame_size) {
375 				if (list_is_singular(&dev->sio_avail)) {
376 					/* Always reuse the last buffer */
377 					fb->v4lbuf.bytesused = 0;
378 					fill = fb->buffer;
379 				} else {
380 					list_move_tail(dev->sio_avail.next,
381 						&dev->sio_full);
382 					wake_up(&dev->wait_frame);
383 					fb = list_first_entry(&dev->sio_avail,
384 						struct stk_sio_buffer, list);
385 					fb->v4lbuf.bytesused = 0;
386 					fill = fb->buffer;
387 				}
388 			}
389 		} else {
390 			framelen -= 4;
391 			iso_buf += 4;
392 		}
393 
394 		/* Our buffer is full !!! */
395 		if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
396 			(void) (printk_ratelimit() &&
397 			STK_ERROR("Frame buffer overflow, lost sync\n"));
398 			/*FIXME Do something here? */
399 			continue;
400 		}
401 		spin_unlock_irqrestore(&dev->spinlock, flags);
402 		memcpy(fill, iso_buf, framelen);
403 		spin_lock_irqsave(&dev->spinlock, flags);
404 		fill += framelen;
405 
406 		/* New size of our buffer */
407 		fb->v4lbuf.bytesused += framelen;
408 	}
409 
410 resubmit:
411 	spin_unlock_irqrestore(&dev->spinlock, flags);
412 	urb->dev = dev->udev;
413 	ret = usb_submit_urb(urb, GFP_ATOMIC);
414 	if (ret != 0) {
415 		STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
416 			ret);
417 	}
418 }
419 
420 /* -------------------------------------------- */
421 
stk_prepare_iso(struct stk_camera * dev)422 static int stk_prepare_iso(struct stk_camera *dev)
423 {
424 	void *kbuf;
425 	int i, j;
426 	struct urb *urb;
427 	struct usb_device *udev;
428 
429 	if (dev == NULL)
430 		return -ENXIO;
431 	udev = dev->udev;
432 
433 	if (dev->isobufs)
434 		STK_ERROR("isobufs already allocated. Bad\n");
435 	else
436 		dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
437 				       GFP_KERNEL);
438 	if (dev->isobufs == NULL) {
439 		STK_ERROR("Unable to allocate iso buffers\n");
440 		return -ENOMEM;
441 	}
442 	for (i = 0; i < MAX_ISO_BUFS; i++) {
443 		if (dev->isobufs[i].data == NULL) {
444 			kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
445 			if (kbuf == NULL) {
446 				STK_ERROR("Failed to allocate iso buffer %d\n",
447 					i);
448 				goto isobufs_out;
449 			}
450 			dev->isobufs[i].data = kbuf;
451 		} else
452 			STK_ERROR("isobuf data already allocated\n");
453 		if (dev->isobufs[i].urb == NULL) {
454 			urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
455 			if (urb == NULL) {
456 				STK_ERROR("Failed to allocate URB %d\n", i);
457 				goto isobufs_out;
458 			}
459 			dev->isobufs[i].urb = urb;
460 		} else {
461 			STK_ERROR("Killing URB\n");
462 			usb_kill_urb(dev->isobufs[i].urb);
463 			urb = dev->isobufs[i].urb;
464 		}
465 		urb->interval = 1;
466 		urb->dev = udev;
467 		urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
468 		urb->transfer_flags = URB_ISO_ASAP;
469 		urb->transfer_buffer = dev->isobufs[i].data;
470 		urb->transfer_buffer_length = ISO_BUFFER_SIZE;
471 		urb->complete = stk_isoc_handler;
472 		urb->context = dev;
473 		urb->start_frame = 0;
474 		urb->number_of_packets = ISO_FRAMES_PER_DESC;
475 
476 		for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
477 			urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
478 			urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
479 		}
480 	}
481 	set_memallocd(dev);
482 	return 0;
483 
484 isobufs_out:
485 	for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
486 		kfree(dev->isobufs[i].data);
487 	for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
488 		usb_free_urb(dev->isobufs[i].urb);
489 	kfree(dev->isobufs);
490 	dev->isobufs = NULL;
491 	return -ENOMEM;
492 }
493 
stk_clean_iso(struct stk_camera * dev)494 static void stk_clean_iso(struct stk_camera *dev)
495 {
496 	int i;
497 
498 	if (dev == NULL || dev->isobufs == NULL)
499 		return;
500 
501 	for (i = 0; i < MAX_ISO_BUFS; i++) {
502 		struct urb *urb;
503 
504 		urb = dev->isobufs[i].urb;
505 		if (urb) {
506 			if (atomic_read(&dev->urbs_used) && is_present(dev))
507 				usb_kill_urb(urb);
508 			usb_free_urb(urb);
509 		}
510 		kfree(dev->isobufs[i].data);
511 	}
512 	kfree(dev->isobufs);
513 	dev->isobufs = NULL;
514 	unset_memallocd(dev);
515 }
516 
stk_setup_siobuf(struct stk_camera * dev,int index)517 static int stk_setup_siobuf(struct stk_camera *dev, int index)
518 {
519 	struct stk_sio_buffer *buf = dev->sio_bufs + index;
520 	INIT_LIST_HEAD(&buf->list);
521 	buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
522 	buf->buffer = vmalloc_user(buf->v4lbuf.length);
523 	if (buf->buffer == NULL)
524 		return -ENOMEM;
525 	buf->mapcount = 0;
526 	buf->dev = dev;
527 	buf->v4lbuf.index = index;
528 	buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 	buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
530 	buf->v4lbuf.field = V4L2_FIELD_NONE;
531 	buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
532 	buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
533 	return 0;
534 }
535 
stk_free_sio_buffers(struct stk_camera * dev)536 static int stk_free_sio_buffers(struct stk_camera *dev)
537 {
538 	int i;
539 	int nbufs;
540 	unsigned long flags;
541 	if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
542 		return 0;
543 	/*
544 	* If any buffers are mapped, we cannot free them at all.
545 	*/
546 	for (i = 0; i < dev->n_sbufs; i++) {
547 		if (dev->sio_bufs[i].mapcount > 0)
548 			return -EBUSY;
549 	}
550 	/*
551 	* OK, let's do it.
552 	*/
553 	spin_lock_irqsave(&dev->spinlock, flags);
554 	INIT_LIST_HEAD(&dev->sio_avail);
555 	INIT_LIST_HEAD(&dev->sio_full);
556 	nbufs = dev->n_sbufs;
557 	dev->n_sbufs = 0;
558 	spin_unlock_irqrestore(&dev->spinlock, flags);
559 	for (i = 0; i < nbufs; i++)
560 		vfree(dev->sio_bufs[i].buffer);
561 	kfree(dev->sio_bufs);
562 	dev->sio_bufs = NULL;
563 	return 0;
564 }
565 
stk_prepare_sio_buffers(struct stk_camera * dev,unsigned n_sbufs)566 static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
567 {
568 	int i;
569 	if (dev->sio_bufs != NULL)
570 		STK_ERROR("sio_bufs already allocated\n");
571 	else {
572 		dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
573 				GFP_KERNEL);
574 		if (dev->sio_bufs == NULL)
575 			return -ENOMEM;
576 		for (i = 0; i < n_sbufs; i++) {
577 			if (stk_setup_siobuf(dev, i))
578 				return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
579 			dev->n_sbufs = i+1;
580 		}
581 	}
582 	return 0;
583 }
584 
stk_allocate_buffers(struct stk_camera * dev,unsigned n_sbufs)585 static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
586 {
587 	int err;
588 	err = stk_prepare_iso(dev);
589 	if (err) {
590 		stk_clean_iso(dev);
591 		return err;
592 	}
593 	err = stk_prepare_sio_buffers(dev, n_sbufs);
594 	if (err) {
595 		stk_free_sio_buffers(dev);
596 		return err;
597 	}
598 	return 0;
599 }
600 
stk_free_buffers(struct stk_camera * dev)601 static void stk_free_buffers(struct stk_camera *dev)
602 {
603 	stk_clean_iso(dev);
604 	stk_free_sio_buffers(dev);
605 }
606 /* -------------------------------------------- */
607 
608 /* v4l file operations */
609 
v4l_stk_open(struct file * fp)610 static int v4l_stk_open(struct file *fp)
611 {
612 	struct stk_camera *dev = video_drvdata(fp);
613 	int err;
614 
615 	if (dev == NULL || !is_present(dev))
616 		return -ENXIO;
617 
618 	if (mutex_lock_interruptible(&dev->lock))
619 		return -ERESTARTSYS;
620 	if (!dev->first_init)
621 		stk_camera_write_reg(dev, 0x0, 0x24);
622 	else
623 		dev->first_init = 0;
624 
625 	err = v4l2_fh_open(fp);
626 	if (!err)
627 		usb_autopm_get_interface(dev->interface);
628 	mutex_unlock(&dev->lock);
629 	return err;
630 }
631 
v4l_stk_release(struct file * fp)632 static int v4l_stk_release(struct file *fp)
633 {
634 	struct stk_camera *dev = video_drvdata(fp);
635 
636 	mutex_lock(&dev->lock);
637 	if (dev->owner == fp) {
638 		stk_stop_stream(dev);
639 		stk_free_buffers(dev);
640 		stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
641 		unset_initialised(dev);
642 		dev->owner = NULL;
643 	}
644 
645 	usb_autopm_put_interface(dev->interface);
646 	mutex_unlock(&dev->lock);
647 	return v4l2_fh_release(fp);
648 }
649 
stk_read(struct file * fp,char __user * buf,size_t count,loff_t * f_pos)650 static ssize_t stk_read(struct file *fp, char __user *buf,
651 		size_t count, loff_t *f_pos)
652 {
653 	int i;
654 	int ret;
655 	unsigned long flags;
656 	struct stk_sio_buffer *sbuf;
657 	struct stk_camera *dev = video_drvdata(fp);
658 
659 	if (!is_present(dev))
660 		return -EIO;
661 	if (dev->owner && (!dev->reading || dev->owner != fp))
662 		return -EBUSY;
663 	dev->owner = fp;
664 	if (!is_streaming(dev)) {
665 		if (stk_initialise(dev)
666 			|| stk_allocate_buffers(dev, 3)
667 			|| stk_start_stream(dev))
668 			return -ENOMEM;
669 		dev->reading = 1;
670 		spin_lock_irqsave(&dev->spinlock, flags);
671 		for (i = 0; i < dev->n_sbufs; i++) {
672 			list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
673 			dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
674 		}
675 		spin_unlock_irqrestore(&dev->spinlock, flags);
676 	}
677 	if (*f_pos == 0) {
678 		if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
679 			return -EWOULDBLOCK;
680 		ret = wait_event_interruptible(dev->wait_frame,
681 			!list_empty(&dev->sio_full) || !is_present(dev));
682 		if (ret)
683 			return ret;
684 		if (!is_present(dev))
685 			return -EIO;
686 	}
687 	if (count + *f_pos > dev->frame_size)
688 		count = dev->frame_size - *f_pos;
689 	spin_lock_irqsave(&dev->spinlock, flags);
690 	if (list_empty(&dev->sio_full)) {
691 		spin_unlock_irqrestore(&dev->spinlock, flags);
692 		STK_ERROR("BUG: No siobufs ready\n");
693 		return 0;
694 	}
695 	sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
696 	spin_unlock_irqrestore(&dev->spinlock, flags);
697 
698 	if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
699 		return -EFAULT;
700 
701 	*f_pos += count;
702 
703 	if (*f_pos >= dev->frame_size) {
704 		*f_pos = 0;
705 		spin_lock_irqsave(&dev->spinlock, flags);
706 		list_move_tail(&sbuf->list, &dev->sio_avail);
707 		spin_unlock_irqrestore(&dev->spinlock, flags);
708 	}
709 	return count;
710 }
711 
v4l_stk_read(struct file * fp,char __user * buf,size_t count,loff_t * f_pos)712 static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
713 		size_t count, loff_t *f_pos)
714 {
715 	struct stk_camera *dev = video_drvdata(fp);
716 	int ret;
717 
718 	if (mutex_lock_interruptible(&dev->lock))
719 		return -ERESTARTSYS;
720 	ret = stk_read(fp, buf, count, f_pos);
721 	mutex_unlock(&dev->lock);
722 	return ret;
723 }
724 
v4l_stk_poll(struct file * fp,poll_table * wait)725 static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
726 {
727 	struct stk_camera *dev = video_drvdata(fp);
728 	unsigned res = v4l2_ctrl_poll(fp, wait);
729 
730 	poll_wait(fp, &dev->wait_frame, wait);
731 
732 	if (!is_present(dev))
733 		return POLLERR;
734 
735 	if (!list_empty(&dev->sio_full))
736 		return res | POLLIN | POLLRDNORM;
737 
738 	return res;
739 }
740 
741 
stk_v4l_vm_open(struct vm_area_struct * vma)742 static void stk_v4l_vm_open(struct vm_area_struct *vma)
743 {
744 	struct stk_sio_buffer *sbuf = vma->vm_private_data;
745 	sbuf->mapcount++;
746 }
stk_v4l_vm_close(struct vm_area_struct * vma)747 static void stk_v4l_vm_close(struct vm_area_struct *vma)
748 {
749 	struct stk_sio_buffer *sbuf = vma->vm_private_data;
750 	sbuf->mapcount--;
751 	if (sbuf->mapcount == 0)
752 		sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
753 }
754 static const struct vm_operations_struct stk_v4l_vm_ops = {
755 	.open = stk_v4l_vm_open,
756 	.close = stk_v4l_vm_close
757 };
758 
v4l_stk_mmap(struct file * fp,struct vm_area_struct * vma)759 static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
760 {
761 	unsigned int i;
762 	int ret;
763 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
764 	struct stk_camera *dev = video_drvdata(fp);
765 	struct stk_sio_buffer *sbuf = NULL;
766 
767 	if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
768 		return -EINVAL;
769 
770 	for (i = 0; i < dev->n_sbufs; i++) {
771 		if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
772 			sbuf = dev->sio_bufs + i;
773 			break;
774 		}
775 	}
776 	if (sbuf == NULL)
777 		return -EINVAL;
778 	ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
779 	if (ret)
780 		return ret;
781 	vma->vm_flags |= VM_DONTEXPAND;
782 	vma->vm_private_data = sbuf;
783 	vma->vm_ops = &stk_v4l_vm_ops;
784 	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
785 	stk_v4l_vm_open(vma);
786 	return 0;
787 }
788 
789 /* v4l ioctl handlers */
790 
stk_vidioc_querycap(struct file * filp,void * priv,struct v4l2_capability * cap)791 static int stk_vidioc_querycap(struct file *filp,
792 		void *priv, struct v4l2_capability *cap)
793 {
794 	struct stk_camera *dev = video_drvdata(filp);
795 
796 	strcpy(cap->driver, "stk");
797 	strcpy(cap->card, "stk");
798 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
799 
800 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
801 		| V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
802 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
803 	return 0;
804 }
805 
stk_vidioc_enum_input(struct file * filp,void * priv,struct v4l2_input * input)806 static int stk_vidioc_enum_input(struct file *filp,
807 		void *priv, struct v4l2_input *input)
808 {
809 	if (input->index != 0)
810 		return -EINVAL;
811 
812 	strcpy(input->name, "Syntek USB Camera");
813 	input->type = V4L2_INPUT_TYPE_CAMERA;
814 	return 0;
815 }
816 
817 
stk_vidioc_g_input(struct file * filp,void * priv,unsigned int * i)818 static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
819 {
820 	*i = 0;
821 	return 0;
822 }
823 
stk_vidioc_s_input(struct file * filp,void * priv,unsigned int i)824 static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
825 {
826 	return i ? -EINVAL : 0;
827 }
828 
stk_s_ctrl(struct v4l2_ctrl * ctrl)829 static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
830 {
831 	struct stk_camera *dev =
832 		container_of(ctrl->handler, struct stk_camera, hdl);
833 
834 	switch (ctrl->id) {
835 	case V4L2_CID_BRIGHTNESS:
836 		return stk_sensor_set_brightness(dev, ctrl->val);
837 	case V4L2_CID_HFLIP:
838 		if (dmi_check_system(stk_upside_down_dmi_table))
839 			dev->vsettings.hflip = !ctrl->val;
840 		else
841 			dev->vsettings.hflip = ctrl->val;
842 		return 0;
843 	case V4L2_CID_VFLIP:
844 		if (dmi_check_system(stk_upside_down_dmi_table))
845 			dev->vsettings.vflip = !ctrl->val;
846 		else
847 			dev->vsettings.vflip = ctrl->val;
848 		return 0;
849 	default:
850 		return -EINVAL;
851 	}
852 	return 0;
853 }
854 
855 
stk_vidioc_enum_fmt_vid_cap(struct file * filp,void * priv,struct v4l2_fmtdesc * fmtd)856 static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
857 		void *priv, struct v4l2_fmtdesc *fmtd)
858 {
859 	switch (fmtd->index) {
860 	case 0:
861 		fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
862 		strcpy(fmtd->description, "r5g6b5");
863 		break;
864 	case 1:
865 		fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
866 		strcpy(fmtd->description, "r5g6b5BE");
867 		break;
868 	case 2:
869 		fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
870 		strcpy(fmtd->description, "yuv4:2:2");
871 		break;
872 	case 3:
873 		fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
874 		strcpy(fmtd->description, "Raw bayer");
875 		break;
876 	case 4:
877 		fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
878 		strcpy(fmtd->description, "yuv4:2:2");
879 		break;
880 	default:
881 		return -EINVAL;
882 	}
883 	return 0;
884 }
885 
886 static struct stk_size {
887 	unsigned w;
888 	unsigned h;
889 	enum stk_mode m;
890 } stk_sizes[] = {
891 	{ .w = 1280, .h = 1024, .m = MODE_SXGA, },
892 	{ .w = 640,  .h = 480,  .m = MODE_VGA,  },
893 	{ .w = 352,  .h = 288,  .m = MODE_CIF,  },
894 	{ .w = 320,  .h = 240,  .m = MODE_QVGA, },
895 	{ .w = 176,  .h = 144,  .m = MODE_QCIF, },
896 };
897 
stk_vidioc_g_fmt_vid_cap(struct file * filp,void * priv,struct v4l2_format * f)898 static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
899 		void *priv, struct v4l2_format *f)
900 {
901 	struct v4l2_pix_format *pix_format = &f->fmt.pix;
902 	struct stk_camera *dev = video_drvdata(filp);
903 	int i;
904 
905 	for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
906 			stk_sizes[i].m != dev->vsettings.mode; i++)
907 		;
908 	if (i == ARRAY_SIZE(stk_sizes)) {
909 		STK_ERROR("ERROR: mode invalid\n");
910 		return -EINVAL;
911 	}
912 	pix_format->width = stk_sizes[i].w;
913 	pix_format->height = stk_sizes[i].h;
914 	pix_format->field = V4L2_FIELD_NONE;
915 	pix_format->colorspace = V4L2_COLORSPACE_SRGB;
916 	pix_format->pixelformat = dev->vsettings.palette;
917 	if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
918 		pix_format->bytesperline = pix_format->width;
919 	else
920 		pix_format->bytesperline = 2 * pix_format->width;
921 	pix_format->sizeimage = pix_format->bytesperline
922 				* pix_format->height;
923 	return 0;
924 }
925 
stk_try_fmt_vid_cap(struct file * filp,struct v4l2_format * fmtd,int * idx)926 static int stk_try_fmt_vid_cap(struct file *filp,
927 		struct v4l2_format *fmtd, int *idx)
928 {
929 	int i;
930 	switch (fmtd->fmt.pix.pixelformat) {
931 	case V4L2_PIX_FMT_RGB565:
932 	case V4L2_PIX_FMT_RGB565X:
933 	case V4L2_PIX_FMT_UYVY:
934 	case V4L2_PIX_FMT_YUYV:
935 	case V4L2_PIX_FMT_SBGGR8:
936 		break;
937 	default:
938 		return -EINVAL;
939 	}
940 	for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
941 		if (fmtd->fmt.pix.width > stk_sizes[i].w)
942 			break;
943 	}
944 	if (i == ARRAY_SIZE(stk_sizes)
945 		|| (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
946 			< abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
947 		fmtd->fmt.pix.height = stk_sizes[i-1].h;
948 		fmtd->fmt.pix.width = stk_sizes[i-1].w;
949 		if (idx)
950 			*idx = i - 1;
951 	} else {
952 		fmtd->fmt.pix.height = stk_sizes[i].h;
953 		fmtd->fmt.pix.width = stk_sizes[i].w;
954 		if (idx)
955 			*idx = i;
956 	}
957 
958 	fmtd->fmt.pix.field = V4L2_FIELD_NONE;
959 	fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
960 	if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
961 		fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
962 	else
963 		fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
964 	fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
965 		* fmtd->fmt.pix.height;
966 	return 0;
967 }
968 
stk_vidioc_try_fmt_vid_cap(struct file * filp,void * priv,struct v4l2_format * fmtd)969 static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
970 		void *priv, struct v4l2_format *fmtd)
971 {
972 	return stk_try_fmt_vid_cap(filp, fmtd, NULL);
973 }
974 
stk_setup_format(struct stk_camera * dev)975 static int stk_setup_format(struct stk_camera *dev)
976 {
977 	int i = 0;
978 	int depth;
979 	if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
980 		depth = 1;
981 	else
982 		depth = 2;
983 	while (i < ARRAY_SIZE(stk_sizes) &&
984 			stk_sizes[i].m != dev->vsettings.mode)
985 		i++;
986 	if (i == ARRAY_SIZE(stk_sizes)) {
987 		STK_ERROR("Something is broken in %s\n", __func__);
988 		return -EFAULT;
989 	}
990 	/* This registers controls some timings, not sure of what. */
991 	stk_camera_write_reg(dev, 0x001b, 0x0e);
992 	if (dev->vsettings.mode == MODE_SXGA)
993 		stk_camera_write_reg(dev, 0x001c, 0x0e);
994 	else
995 		stk_camera_write_reg(dev, 0x001c, 0x46);
996 	/*
997 	 * Registers 0x0115 0x0114 are the size of each line (bytes),
998 	 * regs 0x0117 0x0116 are the heigth of the image.
999 	 */
1000 	stk_camera_write_reg(dev, 0x0115,
1001 		((stk_sizes[i].w * depth) >> 8) & 0xff);
1002 	stk_camera_write_reg(dev, 0x0114,
1003 		(stk_sizes[i].w * depth) & 0xff);
1004 	stk_camera_write_reg(dev, 0x0117,
1005 		(stk_sizes[i].h >> 8) & 0xff);
1006 	stk_camera_write_reg(dev, 0x0116,
1007 		stk_sizes[i].h & 0xff);
1008 	return stk_sensor_configure(dev);
1009 }
1010 
stk_vidioc_s_fmt_vid_cap(struct file * filp,void * priv,struct v4l2_format * fmtd)1011 static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
1012 		void *priv, struct v4l2_format *fmtd)
1013 {
1014 	int ret;
1015 	int idx;
1016 	struct stk_camera *dev = video_drvdata(filp);
1017 
1018 	if (dev == NULL)
1019 		return -ENODEV;
1020 	if (!is_present(dev))
1021 		return -ENODEV;
1022 	if (is_streaming(dev))
1023 		return -EBUSY;
1024 	if (dev->owner)
1025 		return -EBUSY;
1026 	ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
1027 	if (ret)
1028 		return ret;
1029 
1030 	dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1031 	stk_free_buffers(dev);
1032 	dev->frame_size = fmtd->fmt.pix.sizeimage;
1033 	dev->vsettings.mode = stk_sizes[idx].m;
1034 
1035 	stk_initialise(dev);
1036 	return stk_setup_format(dev);
1037 }
1038 
stk_vidioc_reqbufs(struct file * filp,void * priv,struct v4l2_requestbuffers * rb)1039 static int stk_vidioc_reqbufs(struct file *filp,
1040 		void *priv, struct v4l2_requestbuffers *rb)
1041 {
1042 	struct stk_camera *dev = video_drvdata(filp);
1043 
1044 	if (dev == NULL)
1045 		return -ENODEV;
1046 	if (rb->memory != V4L2_MEMORY_MMAP)
1047 		return -EINVAL;
1048 	if (is_streaming(dev)
1049 		|| (dev->owner && dev->owner != filp))
1050 		return -EBUSY;
1051 	stk_free_buffers(dev);
1052 	if (rb->count == 0) {
1053 		stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
1054 		unset_initialised(dev);
1055 		dev->owner = NULL;
1056 		return 0;
1057 	}
1058 	dev->owner = filp;
1059 
1060 	/*FIXME If they ask for zero, we must stop streaming and free */
1061 	if (rb->count < 3)
1062 		rb->count = 3;
1063 	/* Arbitrary limit */
1064 	else if (rb->count > 5)
1065 		rb->count = 5;
1066 
1067 	stk_allocate_buffers(dev, rb->count);
1068 	rb->count = dev->n_sbufs;
1069 	return 0;
1070 }
1071 
stk_vidioc_querybuf(struct file * filp,void * priv,struct v4l2_buffer * buf)1072 static int stk_vidioc_querybuf(struct file *filp,
1073 		void *priv, struct v4l2_buffer *buf)
1074 {
1075 	struct stk_camera *dev = video_drvdata(filp);
1076 	struct stk_sio_buffer *sbuf;
1077 
1078 	if (buf->index >= dev->n_sbufs)
1079 		return -EINVAL;
1080 	sbuf = dev->sio_bufs + buf->index;
1081 	*buf = sbuf->v4lbuf;
1082 	return 0;
1083 }
1084 
stk_vidioc_qbuf(struct file * filp,void * priv,struct v4l2_buffer * buf)1085 static int stk_vidioc_qbuf(struct file *filp,
1086 		void *priv, struct v4l2_buffer *buf)
1087 {
1088 	struct stk_camera *dev = video_drvdata(filp);
1089 	struct stk_sio_buffer *sbuf;
1090 	unsigned long flags;
1091 
1092 	if (buf->memory != V4L2_MEMORY_MMAP)
1093 		return -EINVAL;
1094 
1095 	if (buf->index >= dev->n_sbufs)
1096 		return -EINVAL;
1097 	sbuf = dev->sio_bufs + buf->index;
1098 	if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1099 		return 0;
1100 	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1101 	sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1102 	spin_lock_irqsave(&dev->spinlock, flags);
1103 	list_add_tail(&sbuf->list, &dev->sio_avail);
1104 	*buf = sbuf->v4lbuf;
1105 	spin_unlock_irqrestore(&dev->spinlock, flags);
1106 	return 0;
1107 }
1108 
stk_vidioc_dqbuf(struct file * filp,void * priv,struct v4l2_buffer * buf)1109 static int stk_vidioc_dqbuf(struct file *filp,
1110 		void *priv, struct v4l2_buffer *buf)
1111 {
1112 	struct stk_camera *dev = video_drvdata(filp);
1113 	struct stk_sio_buffer *sbuf;
1114 	unsigned long flags;
1115 	int ret;
1116 
1117 	if (!is_streaming(dev))
1118 		return -EINVAL;
1119 
1120 	if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1121 		return -EWOULDBLOCK;
1122 	ret = wait_event_interruptible(dev->wait_frame,
1123 		!list_empty(&dev->sio_full) || !is_present(dev));
1124 	if (ret)
1125 		return ret;
1126 	if (!is_present(dev))
1127 		return -EIO;
1128 
1129 	spin_lock_irqsave(&dev->spinlock, flags);
1130 	sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1131 	list_del_init(&sbuf->list);
1132 	spin_unlock_irqrestore(&dev->spinlock, flags);
1133 	sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1134 	sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1135 	sbuf->v4lbuf.sequence = ++dev->sequence;
1136 	v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
1137 
1138 	*buf = sbuf->v4lbuf;
1139 	return 0;
1140 }
1141 
stk_vidioc_streamon(struct file * filp,void * priv,enum v4l2_buf_type type)1142 static int stk_vidioc_streamon(struct file *filp,
1143 		void *priv, enum v4l2_buf_type type)
1144 {
1145 	struct stk_camera *dev = video_drvdata(filp);
1146 	if (is_streaming(dev))
1147 		return 0;
1148 	if (dev->sio_bufs == NULL)
1149 		return -EINVAL;
1150 	dev->sequence = 0;
1151 	return stk_start_stream(dev);
1152 }
1153 
stk_vidioc_streamoff(struct file * filp,void * priv,enum v4l2_buf_type type)1154 static int stk_vidioc_streamoff(struct file *filp,
1155 		void *priv, enum v4l2_buf_type type)
1156 {
1157 	struct stk_camera *dev = video_drvdata(filp);
1158 	unsigned long flags;
1159 	int i;
1160 	stk_stop_stream(dev);
1161 	spin_lock_irqsave(&dev->spinlock, flags);
1162 	INIT_LIST_HEAD(&dev->sio_avail);
1163 	INIT_LIST_HEAD(&dev->sio_full);
1164 	for (i = 0; i < dev->n_sbufs; i++) {
1165 		INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1166 		dev->sio_bufs[i].v4lbuf.flags = 0;
1167 	}
1168 	spin_unlock_irqrestore(&dev->spinlock, flags);
1169 	return 0;
1170 }
1171 
1172 
stk_vidioc_g_parm(struct file * filp,void * priv,struct v4l2_streamparm * sp)1173 static int stk_vidioc_g_parm(struct file *filp,
1174 		void *priv, struct v4l2_streamparm *sp)
1175 {
1176 	/*FIXME This is not correct */
1177 	sp->parm.capture.timeperframe.numerator = 1;
1178 	sp->parm.capture.timeperframe.denominator = 30;
1179 	sp->parm.capture.readbuffers = 2;
1180 	return 0;
1181 }
1182 
stk_vidioc_enum_framesizes(struct file * filp,void * priv,struct v4l2_frmsizeenum * frms)1183 static int stk_vidioc_enum_framesizes(struct file *filp,
1184 		void *priv, struct v4l2_frmsizeenum *frms)
1185 {
1186 	if (frms->index >= ARRAY_SIZE(stk_sizes))
1187 		return -EINVAL;
1188 	switch (frms->pixel_format) {
1189 	case V4L2_PIX_FMT_RGB565:
1190 	case V4L2_PIX_FMT_RGB565X:
1191 	case V4L2_PIX_FMT_UYVY:
1192 	case V4L2_PIX_FMT_YUYV:
1193 	case V4L2_PIX_FMT_SBGGR8:
1194 		frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1195 		frms->discrete.width = stk_sizes[frms->index].w;
1196 		frms->discrete.height = stk_sizes[frms->index].h;
1197 		return 0;
1198 	default: return -EINVAL;
1199 	}
1200 }
1201 
1202 static const struct v4l2_ctrl_ops stk_ctrl_ops = {
1203 	.s_ctrl = stk_s_ctrl,
1204 };
1205 
1206 static struct v4l2_file_operations v4l_stk_fops = {
1207 	.owner = THIS_MODULE,
1208 	.open = v4l_stk_open,
1209 	.release = v4l_stk_release,
1210 	.read = v4l_stk_read,
1211 	.poll = v4l_stk_poll,
1212 	.mmap = v4l_stk_mmap,
1213 	.unlocked_ioctl = video_ioctl2,
1214 };
1215 
1216 static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
1217 	.vidioc_querycap = stk_vidioc_querycap,
1218 	.vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1219 	.vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1220 	.vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1221 	.vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1222 	.vidioc_enum_input = stk_vidioc_enum_input,
1223 	.vidioc_s_input = stk_vidioc_s_input,
1224 	.vidioc_g_input = stk_vidioc_g_input,
1225 	.vidioc_reqbufs = stk_vidioc_reqbufs,
1226 	.vidioc_querybuf = stk_vidioc_querybuf,
1227 	.vidioc_qbuf = stk_vidioc_qbuf,
1228 	.vidioc_dqbuf = stk_vidioc_dqbuf,
1229 	.vidioc_streamon = stk_vidioc_streamon,
1230 	.vidioc_streamoff = stk_vidioc_streamoff,
1231 	.vidioc_g_parm = stk_vidioc_g_parm,
1232 	.vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1233 	.vidioc_log_status = v4l2_ctrl_log_status,
1234 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1235 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1236 };
1237 
stk_v4l_dev_release(struct video_device * vd)1238 static void stk_v4l_dev_release(struct video_device *vd)
1239 {
1240 	struct stk_camera *dev = vdev_to_camera(vd);
1241 
1242 	if (dev->sio_bufs != NULL || dev->isobufs != NULL)
1243 		STK_ERROR("We are leaking memory\n");
1244 	usb_put_intf(dev->interface);
1245 	kfree(dev);
1246 }
1247 
1248 static struct video_device stk_v4l_data = {
1249 	.name = "stkwebcam",
1250 	.fops = &v4l_stk_fops,
1251 	.ioctl_ops = &v4l_stk_ioctl_ops,
1252 	.release = stk_v4l_dev_release,
1253 };
1254 
1255 
stk_register_video_device(struct stk_camera * dev)1256 static int stk_register_video_device(struct stk_camera *dev)
1257 {
1258 	int err;
1259 
1260 	dev->vdev = stk_v4l_data;
1261 	dev->vdev.lock = &dev->lock;
1262 	dev->vdev.v4l2_dev = &dev->v4l2_dev;
1263 	video_set_drvdata(&dev->vdev, dev);
1264 	err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1265 	if (err)
1266 		STK_ERROR("v4l registration failed\n");
1267 	else
1268 		STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
1269 			 video_device_node_name(&dev->vdev));
1270 	return err;
1271 }
1272 
1273 
1274 /* USB Stuff */
1275 
stk_camera_probe(struct usb_interface * interface,const struct usb_device_id * id)1276 static int stk_camera_probe(struct usb_interface *interface,
1277 		const struct usb_device_id *id)
1278 {
1279 	struct v4l2_ctrl_handler *hdl;
1280 	int err = 0;
1281 	int i;
1282 
1283 	struct stk_camera *dev = NULL;
1284 	struct usb_device *udev = interface_to_usbdev(interface);
1285 	struct usb_host_interface *iface_desc;
1286 	struct usb_endpoint_descriptor *endpoint;
1287 
1288 	dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1289 	if (dev == NULL) {
1290 		STK_ERROR("Out of memory !\n");
1291 		return -ENOMEM;
1292 	}
1293 	err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
1294 	if (err < 0) {
1295 		dev_err(&udev->dev, "couldn't register v4l2_device\n");
1296 		kfree(dev);
1297 		return err;
1298 	}
1299 	hdl = &dev->hdl;
1300 	v4l2_ctrl_handler_init(hdl, 3);
1301 	v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1302 			  V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
1303 	v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1304 			  V4L2_CID_HFLIP, 0, 1, 1, 1);
1305 	v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
1306 			  V4L2_CID_VFLIP, 0, 1, 1, 1);
1307 	if (hdl->error) {
1308 		err = hdl->error;
1309 		dev_err(&udev->dev, "couldn't register control\n");
1310 		goto error;
1311 	}
1312 	dev->v4l2_dev.ctrl_handler = hdl;
1313 
1314 	spin_lock_init(&dev->spinlock);
1315 	mutex_init(&dev->lock);
1316 	init_waitqueue_head(&dev->wait_frame);
1317 	dev->first_init = 1; /* webcam LED management */
1318 
1319 	dev->udev = udev;
1320 	dev->interface = interface;
1321 	usb_get_intf(interface);
1322 
1323 	if (hflip != -1)
1324 		dev->vsettings.hflip = hflip;
1325 	else if (dmi_check_system(stk_upside_down_dmi_table))
1326 		dev->vsettings.hflip = 1;
1327 	else
1328 		dev->vsettings.hflip = 0;
1329 	if (vflip != -1)
1330 		dev->vsettings.vflip = vflip;
1331 	else if (dmi_check_system(stk_upside_down_dmi_table))
1332 		dev->vsettings.vflip = 1;
1333 	else
1334 		dev->vsettings.vflip = 0;
1335 	dev->n_sbufs = 0;
1336 	set_present(dev);
1337 
1338 	/* Set up the endpoint information
1339 	 * use only the first isoc-in endpoint
1340 	 * for the current alternate setting */
1341 	iface_desc = interface->cur_altsetting;
1342 
1343 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1344 		endpoint = &iface_desc->endpoint[i].desc;
1345 
1346 		if (!dev->isoc_ep
1347 			&& usb_endpoint_is_isoc_in(endpoint)) {
1348 			/* we found an isoc in endpoint */
1349 			dev->isoc_ep = usb_endpoint_num(endpoint);
1350 			break;
1351 		}
1352 	}
1353 	if (!dev->isoc_ep) {
1354 		STK_ERROR("Could not find isoc-in endpoint");
1355 		err = -ENODEV;
1356 		goto error_put;
1357 	}
1358 	dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1359 	dev->vsettings.mode = MODE_VGA;
1360 	dev->frame_size = 640 * 480 * 2;
1361 
1362 	INIT_LIST_HEAD(&dev->sio_avail);
1363 	INIT_LIST_HEAD(&dev->sio_full);
1364 
1365 	usb_set_intfdata(interface, dev);
1366 
1367 	err = stk_register_video_device(dev);
1368 	if (err)
1369 		goto error_put;
1370 
1371 	return 0;
1372 
1373 error_put:
1374 	usb_put_intf(interface);
1375 error:
1376 	v4l2_ctrl_handler_free(hdl);
1377 	v4l2_device_unregister(&dev->v4l2_dev);
1378 	kfree(dev);
1379 	return err;
1380 }
1381 
stk_camera_disconnect(struct usb_interface * interface)1382 static void stk_camera_disconnect(struct usb_interface *interface)
1383 {
1384 	struct stk_camera *dev = usb_get_intfdata(interface);
1385 
1386 	usb_set_intfdata(interface, NULL);
1387 	unset_present(dev);
1388 
1389 	wake_up_interruptible(&dev->wait_frame);
1390 
1391 	STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
1392 		 video_device_node_name(&dev->vdev));
1393 
1394 	video_unregister_device(&dev->vdev);
1395 	v4l2_ctrl_handler_free(&dev->hdl);
1396 	v4l2_device_unregister(&dev->v4l2_dev);
1397 }
1398 
1399 #ifdef CONFIG_PM
stk_camera_suspend(struct usb_interface * intf,pm_message_t message)1400 static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1401 {
1402 	struct stk_camera *dev = usb_get_intfdata(intf);
1403 	if (is_streaming(dev)) {
1404 		stk_stop_stream(dev);
1405 		/* yes, this is ugly */
1406 		set_streaming(dev);
1407 	}
1408 	return 0;
1409 }
1410 
stk_camera_resume(struct usb_interface * intf)1411 static int stk_camera_resume(struct usb_interface *intf)
1412 {
1413 	struct stk_camera *dev = usb_get_intfdata(intf);
1414 	if (!is_initialised(dev))
1415 		return 0;
1416 	unset_initialised(dev);
1417 	stk_initialise(dev);
1418 	stk_camera_write_reg(dev, 0x0, 0x49);
1419 	stk_setup_format(dev);
1420 	if (is_streaming(dev))
1421 		stk_start_stream(dev);
1422 	return 0;
1423 }
1424 #endif
1425 
1426 static struct usb_driver stk_camera_driver = {
1427 	.name = "stkwebcam",
1428 	.probe = stk_camera_probe,
1429 	.disconnect = stk_camera_disconnect,
1430 	.id_table = stkwebcam_table,
1431 #ifdef CONFIG_PM
1432 	.suspend = stk_camera_suspend,
1433 	.resume = stk_camera_resume,
1434 #endif
1435 };
1436 
1437 module_usb_driver(stk_camera_driver);
1438