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