1 /****************************************************************************
2  *
3  *  Filename: cpia2_v4l.c
4  *
5  *  Copyright 2001, STMicrolectronics, Inc.
6  *      Contact:  steve.miller@st.com
7  *  Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
8  *
9  *  Description:
10  *     This is a USB driver for CPia2 based video cameras.
11  *     The infrastructure of this driver is based on the cpia usb driver by
12  *     Jochen Scharrlach and Johannes Erdfeldt.
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2 of the License, or
17  *  (at your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  *  Stripped of 2.4 stuff ready for main kernel submit by
29  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
30  ****************************************************************************/
31 
32 #define CPIA_VERSION "3.0.1"
33 
34 #include <linux/module.h>
35 #include <linux/time.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/videodev2.h>
40 #include <linux/stringify.h>
41 #include <media/v4l2-ioctl.h>
42 
43 #include "cpia2.h"
44 #include "cpia2dev.h"
45 
46 static int video_nr = -1;
47 module_param(video_nr, int, 0);
48 MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
49 
50 static int buffer_size = 68*1024;
51 module_param(buffer_size, int, 0);
52 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
53 
54 static int num_buffers = 3;
55 module_param(num_buffers, int, 0);
56 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
57 		 __stringify(VIDEO_MAX_FRAME) ", default 3)");
58 
59 static int alternate = DEFAULT_ALT;
60 module_param(alternate, int, 0);
61 MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
62 		 __stringify(USBIF_ISO_6) ", default "
63 		 __stringify(DEFAULT_ALT) ")");
64 
65 static int flicker_freq = 60;
66 module_param(flicker_freq, int, 0);
67 MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
68 		 __stringify(60) ", default "
69 		 __stringify(60) ")");
70 
71 static int flicker_mode = NEVER_FLICKER;
72 module_param(flicker_mode, int, 0);
73 MODULE_PARM_DESC(flicker_mode,
74 		 "Flicker supression (" __stringify(NEVER_FLICKER) "or"
75 		 __stringify(ANTI_FLICKER_ON) ", default "
76 		 __stringify(NEVER_FLICKER) ")");
77 
78 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
79 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
80 MODULE_SUPPORTED_DEVICE("video");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(CPIA_VERSION);
83 
84 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
85 
86 struct control_menu_info {
87 	int value;
88 	char name[32];
89 };
90 
91 static struct control_menu_info framerate_controls[] =
92 {
93 	{ CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
94 	{ CPIA2_VP_FRAMERATE_7_5,  "7.5 fps"  },
95 	{ CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
96 	{ CPIA2_VP_FRAMERATE_15,   "15 fps"   },
97 	{ CPIA2_VP_FRAMERATE_25,   "25 fps"   },
98 	{ CPIA2_VP_FRAMERATE_30,   "30 fps"   },
99 };
100 #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
101 
102 static struct control_menu_info flicker_controls[] =
103 {
104 	{ NEVER_FLICKER, "Off" },
105 	{ FLICKER_50,    "50 Hz" },
106 	{ FLICKER_60,    "60 Hz"  },
107 };
108 #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
109 
110 static struct control_menu_info lights_controls[] =
111 {
112 	{ 0,   "Off" },
113 	{ 64,  "Top" },
114 	{ 128, "Bottom"  },
115 	{ 192, "Both"  },
116 };
117 #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
118 #define GPIO_LIGHTS_MASK 192
119 
120 static struct v4l2_queryctrl controls[] = {
121 	{
122 		.id            = V4L2_CID_BRIGHTNESS,
123 		.type          = V4L2_CTRL_TYPE_INTEGER,
124 		.name          = "Brightness",
125 		.minimum       = 0,
126 		.maximum       = 255,
127 		.step          = 1,
128 		.default_value = DEFAULT_BRIGHTNESS,
129 	},
130 	{
131 		.id            = V4L2_CID_CONTRAST,
132 		.type          = V4L2_CTRL_TYPE_INTEGER,
133 		.name          = "Contrast",
134 		.minimum       = 0,
135 		.maximum       = 255,
136 		.step          = 1,
137 		.default_value = DEFAULT_CONTRAST,
138 	},
139 	{
140 		.id            = V4L2_CID_SATURATION,
141 		.type          = V4L2_CTRL_TYPE_INTEGER,
142 		.name          = "Saturation",
143 		.minimum       = 0,
144 		.maximum       = 255,
145 		.step          = 1,
146 		.default_value = DEFAULT_SATURATION,
147 	},
148 	{
149 		.id            = V4L2_CID_HFLIP,
150 		.type          = V4L2_CTRL_TYPE_BOOLEAN,
151 		.name          = "Mirror Horizontally",
152 		.minimum       = 0,
153 		.maximum       = 1,
154 		.step          = 1,
155 		.default_value = 0,
156 	},
157 	{
158 		.id            = V4L2_CID_VFLIP,
159 		.type          = V4L2_CTRL_TYPE_BOOLEAN,
160 		.name          = "Flip Vertically",
161 		.minimum       = 0,
162 		.maximum       = 1,
163 		.step          = 1,
164 		.default_value = 0,
165 	},
166 	{
167 		.id            = CPIA2_CID_TARGET_KB,
168 		.type          = V4L2_CTRL_TYPE_INTEGER,
169 		.name          = "Target KB",
170 		.minimum       = 0,
171 		.maximum       = 255,
172 		.step          = 1,
173 		.default_value = DEFAULT_TARGET_KB,
174 	},
175 	{
176 		.id            = CPIA2_CID_GPIO,
177 		.type          = V4L2_CTRL_TYPE_INTEGER,
178 		.name          = "GPIO",
179 		.minimum       = 0,
180 		.maximum       = 255,
181 		.step          = 1,
182 		.default_value = 0,
183 	},
184 	{
185 		.id            = CPIA2_CID_FLICKER_MODE,
186 		.type          = V4L2_CTRL_TYPE_MENU,
187 		.name          = "Flicker Reduction",
188 		.minimum       = 0,
189 		.maximum       = NUM_FLICKER_CONTROLS-1,
190 		.step          = 1,
191 		.default_value = 0,
192 	},
193 	{
194 		.id            = CPIA2_CID_FRAMERATE,
195 		.type          = V4L2_CTRL_TYPE_MENU,
196 		.name          = "Framerate",
197 		.minimum       = 0,
198 		.maximum       = NUM_FRAMERATE_CONTROLS-1,
199 		.step          = 1,
200 		.default_value = NUM_FRAMERATE_CONTROLS-1,
201 	},
202 	{
203 		.id            = CPIA2_CID_USB_ALT,
204 		.type          = V4L2_CTRL_TYPE_INTEGER,
205 		.name          = "USB Alternate",
206 		.minimum       = USBIF_ISO_1,
207 		.maximum       = USBIF_ISO_6,
208 		.step          = 1,
209 		.default_value = DEFAULT_ALT,
210 	},
211 	{
212 		.id            = CPIA2_CID_LIGHTS,
213 		.type          = V4L2_CTRL_TYPE_MENU,
214 		.name          = "Lights",
215 		.minimum       = 0,
216 		.maximum       = NUM_LIGHTS_CONTROLS-1,
217 		.step          = 1,
218 		.default_value = 0,
219 	},
220 	{
221 		.id            = CPIA2_CID_RESET_CAMERA,
222 		.type          = V4L2_CTRL_TYPE_BUTTON,
223 		.name          = "Reset Camera",
224 		.minimum       = 0,
225 		.maximum       = 0,
226 		.step          = 0,
227 		.default_value = 0,
228 	},
229 };
230 #define NUM_CONTROLS (ARRAY_SIZE(controls))
231 
232 
233 /******************************************************************************
234  *
235  *  cpia2_open
236  *
237  *****************************************************************************/
cpia2_open(struct file * file)238 static int cpia2_open(struct file *file)
239 {
240 	struct camera_data *cam = video_drvdata(file);
241 	struct cpia2_fh *fh;
242 
243 	if (!cam) {
244 		ERR("Internal error, camera_data not found!\n");
245 		return -ENODEV;
246 	}
247 
248 	if (!cam->present)
249 		return -ENODEV;
250 
251 	if (cam->open_count == 0) {
252 		if (cpia2_allocate_buffers(cam))
253 			return -ENOMEM;
254 
255 		/* reset the camera */
256 		if (cpia2_reset_camera(cam) < 0)
257 			return -EIO;
258 
259 		cam->APP_len = 0;
260 		cam->COM_len = 0;
261 	}
262 
263 	fh = kmalloc(sizeof(*fh), GFP_KERNEL);
264 	if (!fh)
265 		return -ENOMEM;
266 	file->private_data = fh;
267 	fh->prio = V4L2_PRIORITY_UNSET;
268 	v4l2_prio_open(&cam->prio, &fh->prio);
269 	fh->mmapped = 0;
270 
271 	++cam->open_count;
272 
273 	cpia2_dbg_dump_registers(cam);
274 	return 0;
275 }
276 
277 /******************************************************************************
278  *
279  *  cpia2_close
280  *
281  *****************************************************************************/
cpia2_close(struct file * file)282 static int cpia2_close(struct file *file)
283 {
284 	struct video_device *dev = video_devdata(file);
285 	struct camera_data *cam = video_get_drvdata(dev);
286 	struct cpia2_fh *fh = file->private_data;
287 
288 	if (cam->present &&
289 	    (cam->open_count == 1 || fh->prio == V4L2_PRIORITY_RECORD)) {
290 		cpia2_usb_stream_stop(cam);
291 
292 		if (cam->open_count == 1) {
293 			/* save camera state for later open */
294 			cpia2_save_camera_state(cam);
295 
296 			cpia2_set_low_power(cam);
297 			cpia2_free_buffers(cam);
298 		}
299 	}
300 
301 	if (fh->mmapped)
302 		cam->mmapped = 0;
303 	v4l2_prio_close(&cam->prio, fh->prio);
304 	file->private_data = NULL;
305 	kfree(fh);
306 
307 	if (--cam->open_count == 0) {
308 		cpia2_free_buffers(cam);
309 		if (!cam->present) {
310 			video_unregister_device(dev);
311 			kfree(cam);
312 			return 0;
313 		}
314 	}
315 
316 	return 0;
317 }
318 
319 /******************************************************************************
320  *
321  *  cpia2_v4l_read
322  *
323  *****************************************************************************/
cpia2_v4l_read(struct file * file,char __user * buf,size_t count,loff_t * off)324 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
325 			      loff_t *off)
326 {
327 	struct camera_data *cam = video_drvdata(file);
328 	int noblock = file->f_flags&O_NONBLOCK;
329 
330 	struct cpia2_fh *fh = file->private_data;
331 
332 	if(!cam)
333 		return -EINVAL;
334 
335 	/* Priority check */
336 	if(fh->prio != V4L2_PRIORITY_RECORD) {
337 		return -EBUSY;
338 	}
339 
340 	return cpia2_read(cam, buf, count, noblock);
341 }
342 
343 
344 /******************************************************************************
345  *
346  *  cpia2_v4l_poll
347  *
348  *****************************************************************************/
cpia2_v4l_poll(struct file * filp,struct poll_table_struct * wait)349 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
350 {
351 	struct camera_data *cam = video_drvdata(filp);
352 	struct cpia2_fh *fh = filp->private_data;
353 
354 	if(!cam)
355 		return POLLERR;
356 
357 	/* Priority check */
358 	if(fh->prio != V4L2_PRIORITY_RECORD) {
359 		return POLLERR;
360 	}
361 
362 	return cpia2_poll(cam, filp, wait);
363 }
364 
365 
sync(struct camera_data * cam,int frame_nr)366 static int sync(struct camera_data *cam, int frame_nr)
367 {
368 	struct framebuf *frame = &cam->buffers[frame_nr];
369 
370 	while (1) {
371 		if (frame->status == FRAME_READY)
372 			return 0;
373 
374 		if (!cam->streaming) {
375 			frame->status = FRAME_READY;
376 			frame->length = 0;
377 			return 0;
378 		}
379 
380 		mutex_unlock(&cam->v4l2_lock);
381 		wait_event_interruptible(cam->wq_stream,
382 					 !cam->streaming ||
383 					 frame->status == FRAME_READY);
384 		mutex_lock(&cam->v4l2_lock);
385 		if (signal_pending(current))
386 			return -ERESTARTSYS;
387 		if(!cam->present)
388 			return -ENOTTY;
389 	}
390 }
391 
392 /******************************************************************************
393  *
394  *  ioctl_set_gpio
395  *
396  *****************************************************************************/
397 
cpia2_default(struct file * file,void * fh,bool valid_prio,int cmd,void * arg)398 static long cpia2_default(struct file *file, void *fh, bool valid_prio,
399 			  int cmd, void *arg)
400 {
401 	struct camera_data *cam = video_drvdata(file);
402 	__u32 gpio_val;
403 
404 	if (cmd != CPIA2_CID_GPIO)
405 		return -EINVAL;
406 
407 	gpio_val = *(__u32*) arg;
408 
409 	if (gpio_val &~ 0xFFU)
410 		return -EINVAL;
411 
412 	return cpia2_set_gpio(cam, (unsigned char)gpio_val);
413 }
414 
415 /******************************************************************************
416  *
417  *  ioctl_querycap
418  *
419  *  V4L2 device capabilities
420  *
421  *****************************************************************************/
422 
cpia2_querycap(struct file * file,void * fh,struct v4l2_capability * vc)423 static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
424 {
425 	struct camera_data *cam = video_drvdata(file);
426 
427 	strcpy(vc->driver, "cpia2");
428 
429 	if (cam->params.pnp_id.product == 0x151)
430 		strcpy(vc->card, "QX5 Microscope");
431 	else
432 		strcpy(vc->card, "CPiA2 Camera");
433 	switch (cam->params.pnp_id.device_type) {
434 	case DEVICE_STV_672:
435 		strcat(vc->card, " (672/");
436 		break;
437 	case DEVICE_STV_676:
438 		strcat(vc->card, " (676/");
439 		break;
440 	default:
441 		strcat(vc->card, " (XXX/");
442 		break;
443 	}
444 	switch (cam->params.version.sensor_flags) {
445 	case CPIA2_VP_SENSOR_FLAGS_404:
446 		strcat(vc->card, "404)");
447 		break;
448 	case CPIA2_VP_SENSOR_FLAGS_407:
449 		strcat(vc->card, "407)");
450 		break;
451 	case CPIA2_VP_SENSOR_FLAGS_409:
452 		strcat(vc->card, "409)");
453 		break;
454 	case CPIA2_VP_SENSOR_FLAGS_410:
455 		strcat(vc->card, "410)");
456 		break;
457 	case CPIA2_VP_SENSOR_FLAGS_500:
458 		strcat(vc->card, "500)");
459 		break;
460 	default:
461 		strcat(vc->card, "XXX)");
462 		break;
463 	}
464 
465 	if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
466 		memset(vc->bus_info,0, sizeof(vc->bus_info));
467 
468 	vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
469 			   V4L2_CAP_READWRITE |
470 			   V4L2_CAP_STREAMING;
471 
472 	return 0;
473 }
474 
475 /******************************************************************************
476  *
477  *  ioctl_input
478  *
479  *  V4L2 input get/set/enumerate
480  *
481  *****************************************************************************/
482 
cpia2_enum_input(struct file * file,void * fh,struct v4l2_input * i)483 static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
484 {
485 	if (i->index)
486 		return -EINVAL;
487 	strcpy(i->name, "Camera");
488 	i->type = V4L2_INPUT_TYPE_CAMERA;
489 	return 0;
490 }
491 
cpia2_g_input(struct file * file,void * fh,unsigned int * i)492 static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
493 {
494 	*i = 0;
495 	return 0;
496 }
497 
cpia2_s_input(struct file * file,void * fh,unsigned int i)498 static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
499 {
500 	return i ? -EINVAL : 0;
501 }
502 
503 /******************************************************************************
504  *
505  *  ioctl_enum_fmt
506  *
507  *  V4L2 format enumerate
508  *
509  *****************************************************************************/
510 
cpia2_enum_fmt_vid_cap(struct file * file,void * fh,struct v4l2_fmtdesc * f)511 static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
512 					    struct v4l2_fmtdesc *f)
513 {
514 	int index = f->index;
515 
516 	if (index < 0 || index > 1)
517 	       return -EINVAL;
518 
519 	memset(f, 0, sizeof(*f));
520 	f->index = index;
521 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
522 	f->flags = V4L2_FMT_FLAG_COMPRESSED;
523 	switch(index) {
524 	case 0:
525 		strcpy(f->description, "MJPEG");
526 		f->pixelformat = V4L2_PIX_FMT_MJPEG;
527 		break;
528 	case 1:
529 		strcpy(f->description, "JPEG");
530 		f->pixelformat = V4L2_PIX_FMT_JPEG;
531 		break;
532 	default:
533 		return -EINVAL;
534 	}
535 
536 	return 0;
537 }
538 
539 /******************************************************************************
540  *
541  *  ioctl_try_fmt
542  *
543  *  V4L2 format try
544  *
545  *****************************************************************************/
546 
cpia2_try_fmt_vid_cap(struct file * file,void * fh,struct v4l2_format * f)547 static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
548 					  struct v4l2_format *f)
549 {
550 	struct camera_data *cam = video_drvdata(file);
551 
552 	if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
553 	    f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
554 	       return -EINVAL;
555 
556 	f->fmt.pix.field = V4L2_FIELD_NONE;
557 	f->fmt.pix.bytesperline = 0;
558 	f->fmt.pix.sizeimage = cam->frame_size;
559 	f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
560 	f->fmt.pix.priv = 0;
561 
562 	switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
563 	case VIDEOSIZE_VGA:
564 		f->fmt.pix.width = 640;
565 		f->fmt.pix.height = 480;
566 		break;
567 	case VIDEOSIZE_CIF:
568 		f->fmt.pix.width = 352;
569 		f->fmt.pix.height = 288;
570 		break;
571 	case VIDEOSIZE_QVGA:
572 		f->fmt.pix.width = 320;
573 		f->fmt.pix.height = 240;
574 		break;
575 	case VIDEOSIZE_288_216:
576 		f->fmt.pix.width = 288;
577 		f->fmt.pix.height = 216;
578 		break;
579 	case VIDEOSIZE_256_192:
580 		f->fmt.pix.width = 256;
581 		f->fmt.pix.height = 192;
582 		break;
583 	case VIDEOSIZE_224_168:
584 		f->fmt.pix.width = 224;
585 		f->fmt.pix.height = 168;
586 		break;
587 	case VIDEOSIZE_192_144:
588 		f->fmt.pix.width = 192;
589 		f->fmt.pix.height = 144;
590 		break;
591 	case VIDEOSIZE_QCIF:
592 	default:
593 		f->fmt.pix.width = 176;
594 		f->fmt.pix.height = 144;
595 		break;
596 	}
597 
598 	return 0;
599 }
600 
601 /******************************************************************************
602  *
603  *  ioctl_set_fmt
604  *
605  *  V4L2 format set
606  *
607  *****************************************************************************/
608 
cpia2_s_fmt_vid_cap(struct file * file,void * _fh,struct v4l2_format * f)609 static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
610 					struct v4l2_format *f)
611 {
612 	struct camera_data *cam = video_drvdata(file);
613 	struct cpia2_fh *fh = _fh;
614 	int err, frame;
615 
616 	err = v4l2_prio_check(&cam->prio, fh->prio);
617 	if (err)
618 		return err;
619 	err = cpia2_try_fmt_vid_cap(file, _fh, f);
620 	if(err != 0)
621 		return err;
622 
623 	/* Ensure that only this process can change the format. */
624 	err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
625 	if(err != 0) {
626 		return err;
627 	}
628 
629 	cam->pixelformat = f->fmt.pix.pixelformat;
630 
631 	/* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
632 	 * the missing Huffman table properly. */
633 	cam->params.compression.inhibit_htables = 0;
634 		/*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
635 
636 	/* we set the video window to something smaller or equal to what
637 	 * is requested by the user???
638 	 */
639 	DBG("Requested width = %d, height = %d\n",
640 	    f->fmt.pix.width, f->fmt.pix.height);
641 	if (f->fmt.pix.width != cam->width ||
642 	    f->fmt.pix.height != cam->height) {
643 		cam->width = f->fmt.pix.width;
644 		cam->height = f->fmt.pix.height;
645 		cam->params.roi.width = f->fmt.pix.width;
646 		cam->params.roi.height = f->fmt.pix.height;
647 		cpia2_set_format(cam);
648 	}
649 
650 	for (frame = 0; frame < cam->num_frames; ++frame) {
651 		if (cam->buffers[frame].status == FRAME_READING)
652 			if ((err = sync(cam, frame)) < 0)
653 				return err;
654 
655 		cam->buffers[frame].status = FRAME_EMPTY;
656 	}
657 
658 	return 0;
659 }
660 
661 /******************************************************************************
662  *
663  *  ioctl_get_fmt
664  *
665  *  V4L2 format get
666  *
667  *****************************************************************************/
668 
cpia2_g_fmt_vid_cap(struct file * file,void * fh,struct v4l2_format * f)669 static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
670 					struct v4l2_format *f)
671 {
672 	struct camera_data *cam = video_drvdata(file);
673 
674 	f->fmt.pix.width = cam->width;
675 	f->fmt.pix.height = cam->height;
676 	f->fmt.pix.pixelformat = cam->pixelformat;
677 	f->fmt.pix.field = V4L2_FIELD_NONE;
678 	f->fmt.pix.bytesperline = 0;
679 	f->fmt.pix.sizeimage = cam->frame_size;
680 	f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
681 	f->fmt.pix.priv = 0;
682 
683 	return 0;
684 }
685 
686 /******************************************************************************
687  *
688  *  ioctl_cropcap
689  *
690  *  V4L2 query cropping capabilities
691  *  NOTE: cropping is currently disabled
692  *
693  *****************************************************************************/
694 
cpia2_cropcap(struct file * file,void * fh,struct v4l2_cropcap * c)695 static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
696 {
697 	struct camera_data *cam = video_drvdata(file);
698 
699 	if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
700 	       return -EINVAL;
701 
702 	c->bounds.left = 0;
703 	c->bounds.top = 0;
704 	c->bounds.width = cam->width;
705 	c->bounds.height = cam->height;
706 	c->defrect.left = 0;
707 	c->defrect.top = 0;
708 	c->defrect.width = cam->width;
709 	c->defrect.height = cam->height;
710 	c->pixelaspect.numerator = 1;
711 	c->pixelaspect.denominator = 1;
712 
713 	return 0;
714 }
715 
716 /******************************************************************************
717  *
718  *  ioctl_queryctrl
719  *
720  *  V4L2 query possible control variables
721  *
722  *****************************************************************************/
723 
cpia2_queryctrl(struct file * file,void * fh,struct v4l2_queryctrl * c)724 static int cpia2_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
725 {
726 	struct camera_data *cam = video_drvdata(file);
727 	int i;
728 
729 	for(i=0; i<NUM_CONTROLS; ++i) {
730 		if(c->id == controls[i].id) {
731 			memcpy(c, controls+i, sizeof(*c));
732 			break;
733 		}
734 	}
735 
736 	if(i == NUM_CONTROLS)
737 		return -EINVAL;
738 
739 	/* Some devices have additional limitations */
740 	switch(c->id) {
741 	case V4L2_CID_BRIGHTNESS:
742 		/***
743 		 * Don't let the register be set to zero - bug in VP4
744 		 * flash of full brightness
745 		 ***/
746 		if (cam->params.pnp_id.device_type == DEVICE_STV_672)
747 			c->minimum = 1;
748 		break;
749 	case V4L2_CID_VFLIP:
750 		// VP5 Only
751 		if(cam->params.pnp_id.device_type == DEVICE_STV_672)
752 			c->flags |= V4L2_CTRL_FLAG_DISABLED;
753 		break;
754 	case CPIA2_CID_FRAMERATE:
755 		if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
756 		   cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
757 			// Maximum 15fps
758 			for(i=0; i<c->maximum; ++i) {
759 				if(framerate_controls[i].value ==
760 				   CPIA2_VP_FRAMERATE_15) {
761 					c->maximum = i;
762 					c->default_value = i;
763 				}
764 			}
765 		}
766 		break;
767 	case CPIA2_CID_FLICKER_MODE:
768 		// Flicker control only valid for 672.
769 		if(cam->params.pnp_id.device_type != DEVICE_STV_672)
770 			c->flags |= V4L2_CTRL_FLAG_DISABLED;
771 		break;
772 	case CPIA2_CID_LIGHTS:
773 		// Light control only valid for the QX5 Microscope.
774 		if(cam->params.pnp_id.product != 0x151)
775 			c->flags |= V4L2_CTRL_FLAG_DISABLED;
776 		break;
777 	default:
778 		break;
779 	}
780 
781 	return 0;
782 }
783 
784 /******************************************************************************
785  *
786  *  ioctl_querymenu
787  *
788  *  V4L2 query possible control variables
789  *
790  *****************************************************************************/
791 
cpia2_querymenu(struct file * file,void * fh,struct v4l2_querymenu * m)792 static int cpia2_querymenu(struct file *file, void *fh, struct v4l2_querymenu *m)
793 {
794 	struct camera_data *cam = video_drvdata(file);
795 
796 	switch(m->id) {
797 	case CPIA2_CID_FLICKER_MODE:
798 		if (m->index >= NUM_FLICKER_CONTROLS)
799 			return -EINVAL;
800 
801 		strcpy(m->name, flicker_controls[m->index].name);
802 		break;
803 	case CPIA2_CID_FRAMERATE:
804 	    {
805 		int maximum = NUM_FRAMERATE_CONTROLS - 1;
806 		if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
807 		   cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
808 			// Maximum 15fps
809 			int i;
810 			for(i=0; i<maximum; ++i) {
811 				if(framerate_controls[i].value ==
812 				   CPIA2_VP_FRAMERATE_15)
813 					maximum = i;
814 			}
815 		}
816 		if (m->index > maximum)
817 			return -EINVAL;
818 
819 		strcpy(m->name, framerate_controls[m->index].name);
820 		break;
821 	    }
822 	case CPIA2_CID_LIGHTS:
823 		if (m->index >= NUM_LIGHTS_CONTROLS)
824 			return -EINVAL;
825 
826 		strcpy(m->name, lights_controls[m->index].name);
827 		break;
828 	default:
829 		return -EINVAL;
830 	}
831 
832 	return 0;
833 }
834 
835 /******************************************************************************
836  *
837  *  ioctl_g_ctrl
838  *
839  *  V4L2 get the value of a control variable
840  *
841  *****************************************************************************/
842 
cpia2_g_ctrl(struct file * file,void * fh,struct v4l2_control * c)843 static int cpia2_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
844 {
845 	struct camera_data *cam = video_drvdata(file);
846 
847 	switch(c->id) {
848 	case V4L2_CID_BRIGHTNESS:
849 		cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
850 				 TRANSFER_READ, 0);
851 		c->value = cam->params.color_params.brightness;
852 		break;
853 	case V4L2_CID_CONTRAST:
854 		cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
855 				 TRANSFER_READ, 0);
856 		c->value = cam->params.color_params.contrast;
857 		break;
858 	case V4L2_CID_SATURATION:
859 		cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
860 				 TRANSFER_READ, 0);
861 		c->value = cam->params.color_params.saturation;
862 		break;
863 	case V4L2_CID_HFLIP:
864 		cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
865 				 TRANSFER_READ, 0);
866 		c->value = (cam->params.vp_params.user_effects &
867 			    CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
868 		break;
869 	case V4L2_CID_VFLIP:
870 		cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
871 				 TRANSFER_READ, 0);
872 		c->value = (cam->params.vp_params.user_effects &
873 			    CPIA2_VP_USER_EFFECTS_FLIP) != 0;
874 		break;
875 	case CPIA2_CID_TARGET_KB:
876 		c->value = cam->params.vc_params.target_kb;
877 		break;
878 	case CPIA2_CID_GPIO:
879 		cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
880 				 TRANSFER_READ, 0);
881 		c->value = cam->params.vp_params.gpio_data;
882 		break;
883 	case CPIA2_CID_FLICKER_MODE:
884 	{
885 		int i, mode;
886 		cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
887 				 TRANSFER_READ, 0);
888 		if(cam->params.flicker_control.cam_register &
889 		   CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
890 			mode = NEVER_FLICKER;
891 		} else {
892 		    if(cam->params.flicker_control.cam_register &
893 		       CPIA2_VP_FLICKER_MODES_50HZ) {
894 			mode = FLICKER_50;
895 		    } else {
896 			mode = FLICKER_60;
897 		    }
898 		}
899 		for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
900 			if(flicker_controls[i].value == mode) {
901 				c->value = i;
902 				break;
903 			}
904 		}
905 		if(i == NUM_FLICKER_CONTROLS)
906 			return -EINVAL;
907 		break;
908 	}
909 	case CPIA2_CID_FRAMERATE:
910 	{
911 		int maximum = NUM_FRAMERATE_CONTROLS - 1;
912 		int i;
913 		for(i=0; i<= maximum; i++) {
914 			if(cam->params.vp_params.frame_rate ==
915 			   framerate_controls[i].value)
916 				break;
917 		}
918 		if(i > maximum)
919 			return -EINVAL;
920 		c->value = i;
921 		break;
922 	}
923 	case CPIA2_CID_USB_ALT:
924 		c->value = cam->params.camera_state.stream_mode;
925 		break;
926 	case CPIA2_CID_LIGHTS:
927 	{
928 		int i;
929 		cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
930 				 TRANSFER_READ, 0);
931 		for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
932 			if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
933 			   lights_controls[i].value) {
934 				break;
935 			}
936 		}
937 		if(i == NUM_LIGHTS_CONTROLS)
938 			return -EINVAL;
939 		c->value = i;
940 		break;
941 	}
942 	case CPIA2_CID_RESET_CAMERA:
943 		return -EINVAL;
944 	default:
945 		return -EINVAL;
946 	}
947 
948 	DBG("Get control id:%d, value:%d\n", c->id, c->value);
949 
950 	return 0;
951 }
952 
953 /******************************************************************************
954  *
955  *  ioctl_s_ctrl
956  *
957  *  V4L2 set the value of a control variable
958  *
959  *****************************************************************************/
960 
cpia2_s_ctrl(struct file * file,void * fh,struct v4l2_control * c)961 static int cpia2_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
962 {
963 	struct camera_data *cam = video_drvdata(file);
964 	int i;
965 	int retval = 0;
966 
967 	DBG("Set control id:%d, value:%d\n", c->id, c->value);
968 
969 	/* Check that the value is in range */
970 	for(i=0; i<NUM_CONTROLS; i++) {
971 		if(c->id == controls[i].id) {
972 			if(c->value < controls[i].minimum ||
973 			   c->value > controls[i].maximum) {
974 				return -EINVAL;
975 			}
976 			break;
977 		}
978 	}
979 	if(i == NUM_CONTROLS)
980 		return -EINVAL;
981 
982 	switch(c->id) {
983 	case V4L2_CID_BRIGHTNESS:
984 		cpia2_set_brightness(cam, c->value);
985 		break;
986 	case V4L2_CID_CONTRAST:
987 		cpia2_set_contrast(cam, c->value);
988 		break;
989 	case V4L2_CID_SATURATION:
990 		cpia2_set_saturation(cam, c->value);
991 		break;
992 	case V4L2_CID_HFLIP:
993 		cpia2_set_property_mirror(cam, c->value);
994 		break;
995 	case V4L2_CID_VFLIP:
996 		cpia2_set_property_flip(cam, c->value);
997 		break;
998 	case CPIA2_CID_TARGET_KB:
999 		retval = cpia2_set_target_kb(cam, c->value);
1000 		break;
1001 	case CPIA2_CID_GPIO:
1002 		retval = cpia2_set_gpio(cam, c->value);
1003 		break;
1004 	case CPIA2_CID_FLICKER_MODE:
1005 		retval = cpia2_set_flicker_mode(cam,
1006 					      flicker_controls[c->value].value);
1007 		break;
1008 	case CPIA2_CID_FRAMERATE:
1009 		retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1010 		break;
1011 	case CPIA2_CID_USB_ALT:
1012 		retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1013 		break;
1014 	case CPIA2_CID_LIGHTS:
1015 		retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1016 		break;
1017 	case CPIA2_CID_RESET_CAMERA:
1018 		cpia2_usb_stream_pause(cam);
1019 		cpia2_reset_camera(cam);
1020 		cpia2_usb_stream_resume(cam);
1021 		break;
1022 	default:
1023 		retval = -EINVAL;
1024 	}
1025 
1026 	return retval;
1027 }
1028 
1029 /******************************************************************************
1030  *
1031  *  ioctl_g_jpegcomp
1032  *
1033  *  V4L2 get the JPEG compression parameters
1034  *
1035  *****************************************************************************/
1036 
cpia2_g_jpegcomp(struct file * file,void * fh,struct v4l2_jpegcompression * parms)1037 static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
1038 {
1039 	struct camera_data *cam = video_drvdata(file);
1040 
1041 	memset(parms, 0, sizeof(*parms));
1042 
1043 	parms->quality = 80; // TODO: Can this be made meaningful?
1044 
1045 	parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1046 	if(!cam->params.compression.inhibit_htables) {
1047 		parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1048 	}
1049 
1050 	parms->APPn = cam->APPn;
1051 	parms->APP_len = cam->APP_len;
1052 	if(cam->APP_len > 0) {
1053 		memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1054 		parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1055 	}
1056 
1057 	parms->COM_len = cam->COM_len;
1058 	if(cam->COM_len > 0) {
1059 		memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1060 		parms->jpeg_markers |= JPEG_MARKER_COM;
1061 	}
1062 
1063 	DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1064 	    parms->APP_len, parms->COM_len);
1065 
1066 	return 0;
1067 }
1068 
1069 /******************************************************************************
1070  *
1071  *  ioctl_s_jpegcomp
1072  *
1073  *  V4L2 set the JPEG compression parameters
1074  *  NOTE: quality and some jpeg_markers are ignored.
1075  *
1076  *****************************************************************************/
1077 
cpia2_s_jpegcomp(struct file * file,void * fh,struct v4l2_jpegcompression * parms)1078 static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
1079 {
1080 	struct camera_data *cam = video_drvdata(file);
1081 
1082 	DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1083 	    parms->APP_len, parms->COM_len);
1084 
1085 	cam->params.compression.inhibit_htables =
1086 		!(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1087 
1088 	if(parms->APP_len != 0) {
1089 		if(parms->APP_len > 0 &&
1090 		   parms->APP_len <= sizeof(cam->APP_data) &&
1091 		   parms->APPn >= 0 && parms->APPn <= 15) {
1092 			cam->APPn = parms->APPn;
1093 			cam->APP_len = parms->APP_len;
1094 			memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1095 		} else {
1096 			LOG("Bad APPn Params n=%d len=%d\n",
1097 			    parms->APPn, parms->APP_len);
1098 			return -EINVAL;
1099 		}
1100 	} else {
1101 		cam->APP_len = 0;
1102 	}
1103 
1104 	if(parms->COM_len != 0) {
1105 		if(parms->COM_len > 0 &&
1106 		   parms->COM_len <= sizeof(cam->COM_data)) {
1107 			cam->COM_len = parms->COM_len;
1108 			memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1109 		} else {
1110 			LOG("Bad COM_len=%d\n", parms->COM_len);
1111 			return -EINVAL;
1112 		}
1113 	}
1114 
1115 	return 0;
1116 }
1117 
1118 /******************************************************************************
1119  *
1120  *  ioctl_reqbufs
1121  *
1122  *  V4L2 Initiate memory mapping.
1123  *  NOTE: The user's request is ignored. For now the buffers are fixed.
1124  *
1125  *****************************************************************************/
1126 
cpia2_reqbufs(struct file * file,void * fh,struct v4l2_requestbuffers * req)1127 static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
1128 {
1129 	struct camera_data *cam = video_drvdata(file);
1130 
1131 	if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1132 	   req->memory != V4L2_MEMORY_MMAP)
1133 		return -EINVAL;
1134 
1135 	DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1136 	req->count = cam->num_frames;
1137 	memset(&req->reserved, 0, sizeof(req->reserved));
1138 
1139 	return 0;
1140 }
1141 
1142 /******************************************************************************
1143  *
1144  *  ioctl_querybuf
1145  *
1146  *  V4L2 Query memory buffer status.
1147  *
1148  *****************************************************************************/
1149 
cpia2_querybuf(struct file * file,void * fh,struct v4l2_buffer * buf)1150 static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1151 {
1152 	struct camera_data *cam = video_drvdata(file);
1153 
1154 	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1155 	   buf->index > cam->num_frames)
1156 		return -EINVAL;
1157 
1158 	buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1159 	buf->length = cam->frame_size;
1160 
1161 	buf->memory = V4L2_MEMORY_MMAP;
1162 
1163 	if(cam->mmapped)
1164 		buf->flags = V4L2_BUF_FLAG_MAPPED;
1165 	else
1166 		buf->flags = 0;
1167 
1168 	switch (cam->buffers[buf->index].status) {
1169 	case FRAME_EMPTY:
1170 	case FRAME_ERROR:
1171 	case FRAME_READING:
1172 		buf->bytesused = 0;
1173 		buf->flags = V4L2_BUF_FLAG_QUEUED;
1174 		break;
1175 	case FRAME_READY:
1176 		buf->bytesused = cam->buffers[buf->index].length;
1177 		buf->timestamp = cam->buffers[buf->index].timestamp;
1178 		buf->sequence = cam->buffers[buf->index].seq;
1179 		buf->flags = V4L2_BUF_FLAG_DONE;
1180 		break;
1181 	}
1182 
1183 	DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1184 	     buf->index, buf->m.offset, buf->flags, buf->sequence,
1185 	     buf->bytesused);
1186 
1187 	return 0;
1188 }
1189 
1190 /******************************************************************************
1191  *
1192  *  ioctl_qbuf
1193  *
1194  *  V4L2 User is freeing buffer
1195  *
1196  *****************************************************************************/
1197 
cpia2_qbuf(struct file * file,void * fh,struct v4l2_buffer * buf)1198 static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1199 {
1200 	struct camera_data *cam = video_drvdata(file);
1201 
1202 	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1203 	   buf->memory != V4L2_MEMORY_MMAP ||
1204 	   buf->index > cam->num_frames)
1205 		return -EINVAL;
1206 
1207 	DBG("QBUF #%d\n", buf->index);
1208 
1209 	if(cam->buffers[buf->index].status == FRAME_READY)
1210 		cam->buffers[buf->index].status = FRAME_EMPTY;
1211 
1212 	return 0;
1213 }
1214 
1215 /******************************************************************************
1216  *
1217  *  find_earliest_filled_buffer
1218  *
1219  *  Helper for ioctl_dqbuf. Find the next ready buffer.
1220  *
1221  *****************************************************************************/
1222 
find_earliest_filled_buffer(struct camera_data * cam)1223 static int find_earliest_filled_buffer(struct camera_data *cam)
1224 {
1225 	int i;
1226 	int found = -1;
1227 	for (i=0; i<cam->num_frames; i++) {
1228 		if(cam->buffers[i].status == FRAME_READY) {
1229 			if(found < 0) {
1230 				found = i;
1231 			} else {
1232 				/* find which buffer is earlier */
1233 				struct timeval *tv1, *tv2;
1234 				tv1 = &cam->buffers[i].timestamp;
1235 				tv2 = &cam->buffers[found].timestamp;
1236 				if(tv1->tv_sec < tv2->tv_sec ||
1237 				   (tv1->tv_sec == tv2->tv_sec &&
1238 				    tv1->tv_usec < tv2->tv_usec))
1239 					found = i;
1240 			}
1241 		}
1242 	}
1243 	return found;
1244 }
1245 
1246 /******************************************************************************
1247  *
1248  *  ioctl_dqbuf
1249  *
1250  *  V4L2 User is asking for a filled buffer.
1251  *
1252  *****************************************************************************/
1253 
cpia2_dqbuf(struct file * file,void * fh,struct v4l2_buffer * buf)1254 static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1255 {
1256 	struct camera_data *cam = video_drvdata(file);
1257 	int frame;
1258 
1259 	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1260 	   buf->memory != V4L2_MEMORY_MMAP)
1261 		return -EINVAL;
1262 
1263 	frame = find_earliest_filled_buffer(cam);
1264 
1265 	if(frame < 0 && file->f_flags&O_NONBLOCK)
1266 		return -EAGAIN;
1267 
1268 	if(frame < 0) {
1269 		/* Wait for a frame to become available */
1270 		struct framebuf *cb=cam->curbuff;
1271 		mutex_unlock(&cam->v4l2_lock);
1272 		wait_event_interruptible(cam->wq_stream,
1273 					 !cam->present ||
1274 					 (cb=cam->curbuff)->status == FRAME_READY);
1275 		mutex_lock(&cam->v4l2_lock);
1276 		if (signal_pending(current))
1277 			return -ERESTARTSYS;
1278 		if(!cam->present)
1279 			return -ENOTTY;
1280 		frame = cb->num;
1281 	}
1282 
1283 
1284 	buf->index = frame;
1285 	buf->bytesused = cam->buffers[buf->index].length;
1286 	buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1287 	buf->field = V4L2_FIELD_NONE;
1288 	buf->timestamp = cam->buffers[buf->index].timestamp;
1289 	buf->sequence = cam->buffers[buf->index].seq;
1290 	buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1291 	buf->length = cam->frame_size;
1292 	buf->input = 0;
1293 	buf->reserved = 0;
1294 	memset(&buf->timecode, 0, sizeof(buf->timecode));
1295 
1296 	DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1297 	    cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1298 
1299 	return 0;
1300 }
1301 
cpia2_g_priority(struct file * file,void * _fh,enum v4l2_priority * p)1302 static int cpia2_g_priority(struct file *file, void *_fh, enum v4l2_priority *p)
1303 {
1304 	struct cpia2_fh *fh = _fh;
1305 
1306 	*p = fh->prio;
1307 	return 0;
1308 }
1309 
cpia2_s_priority(struct file * file,void * _fh,enum v4l2_priority prio)1310 static int cpia2_s_priority(struct file *file, void *_fh, enum v4l2_priority prio)
1311 {
1312 	struct camera_data *cam = video_drvdata(file);
1313 	struct cpia2_fh *fh = _fh;
1314 
1315 	if (cam->streaming && prio != fh->prio &&
1316 			fh->prio == V4L2_PRIORITY_RECORD)
1317 		/* Can't drop record priority while streaming */
1318 		return -EBUSY;
1319 
1320 	if (prio == V4L2_PRIORITY_RECORD && prio != fh->prio &&
1321 			v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD)
1322 		/* Only one program can record at a time */
1323 		return -EBUSY;
1324 	return v4l2_prio_change(&cam->prio, &fh->prio, prio);
1325 }
1326 
cpia2_streamon(struct file * file,void * fh,enum v4l2_buf_type type)1327 static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
1328 {
1329 	struct camera_data *cam = video_drvdata(file);
1330 
1331 	DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1332 	if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1333 		return -EINVAL;
1334 
1335 	if (!cam->streaming)
1336 		return cpia2_usb_stream_start(cam,
1337 				cam->params.camera_state.stream_mode);
1338 	return -EINVAL;
1339 }
1340 
cpia2_streamoff(struct file * file,void * fh,enum v4l2_buf_type type)1341 static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1342 {
1343 	struct camera_data *cam = video_drvdata(file);
1344 
1345 	DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1346 	if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1347 		return -EINVAL;
1348 
1349 	if (cam->streaming)
1350 		return cpia2_usb_stream_stop(cam);
1351 	return -EINVAL;
1352 }
1353 
1354 /******************************************************************************
1355  *
1356  *  cpia2_mmap
1357  *
1358  *****************************************************************************/
cpia2_mmap(struct file * file,struct vm_area_struct * area)1359 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1360 {
1361 	struct camera_data *cam = video_drvdata(file);
1362 	int retval;
1363 
1364 	/* Priority check */
1365 	struct cpia2_fh *fh = file->private_data;
1366 	if(fh->prio != V4L2_PRIORITY_RECORD) {
1367 		return -EBUSY;
1368 	}
1369 
1370 	retval = cpia2_remap_buffer(cam, area);
1371 
1372 	if(!retval)
1373 		fh->mmapped = 1;
1374 	return retval;
1375 }
1376 
1377 /******************************************************************************
1378  *
1379  *  reset_camera_struct_v4l
1380  *
1381  *  Sets all values to the defaults
1382  *****************************************************************************/
reset_camera_struct_v4l(struct camera_data * cam)1383 static void reset_camera_struct_v4l(struct camera_data *cam)
1384 {
1385 	cam->width = cam->params.roi.width;
1386 	cam->height = cam->params.roi.height;
1387 
1388 	cam->frame_size = buffer_size;
1389 	cam->num_frames = num_buffers;
1390 
1391 	/* FlickerModes */
1392 	cam->params.flicker_control.flicker_mode_req = flicker_mode;
1393 	cam->params.flicker_control.mains_frequency = flicker_freq;
1394 
1395 	/* streamMode */
1396 	cam->params.camera_state.stream_mode = alternate;
1397 
1398 	cam->pixelformat = V4L2_PIX_FMT_JPEG;
1399 	v4l2_prio_init(&cam->prio);
1400 }
1401 
1402 static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1403 	.vidioc_querycap		    = cpia2_querycap,
1404 	.vidioc_enum_input		    = cpia2_enum_input,
1405 	.vidioc_g_input			    = cpia2_g_input,
1406 	.vidioc_s_input			    = cpia2_s_input,
1407 	.vidioc_enum_fmt_vid_cap	    = cpia2_enum_fmt_vid_cap,
1408 	.vidioc_g_fmt_vid_cap		    = cpia2_g_fmt_vid_cap,
1409 	.vidioc_s_fmt_vid_cap		    = cpia2_s_fmt_vid_cap,
1410 	.vidioc_try_fmt_vid_cap		    = cpia2_try_fmt_vid_cap,
1411 	.vidioc_queryctrl		    = cpia2_queryctrl,
1412 	.vidioc_querymenu		    = cpia2_querymenu,
1413 	.vidioc_g_ctrl			    = cpia2_g_ctrl,
1414 	.vidioc_s_ctrl			    = cpia2_s_ctrl,
1415 	.vidioc_g_jpegcomp		    = cpia2_g_jpegcomp,
1416 	.vidioc_s_jpegcomp		    = cpia2_s_jpegcomp,
1417 	.vidioc_cropcap			    = cpia2_cropcap,
1418 	.vidioc_reqbufs			    = cpia2_reqbufs,
1419 	.vidioc_querybuf		    = cpia2_querybuf,
1420 	.vidioc_qbuf			    = cpia2_qbuf,
1421 	.vidioc_dqbuf			    = cpia2_dqbuf,
1422 	.vidioc_streamon		    = cpia2_streamon,
1423 	.vidioc_streamoff		    = cpia2_streamoff,
1424 	.vidioc_g_priority		    = cpia2_g_priority,
1425 	.vidioc_s_priority		    = cpia2_s_priority,
1426 	.vidioc_default			    = cpia2_default,
1427 };
1428 
1429 /***
1430  * The v4l video device structure initialized for this device
1431  ***/
1432 static const struct v4l2_file_operations cpia2_fops = {
1433 	.owner		= THIS_MODULE,
1434 	.open		= cpia2_open,
1435 	.release	= cpia2_close,
1436 	.read		= cpia2_v4l_read,
1437 	.poll		= cpia2_v4l_poll,
1438 	.unlocked_ioctl	= video_ioctl2,
1439 	.mmap		= cpia2_mmap,
1440 };
1441 
1442 static struct video_device cpia2_template = {
1443 	/* I could not find any place for the old .initialize initializer?? */
1444 	.name =		"CPiA2 Camera",
1445 	.fops =		&cpia2_fops,
1446 	.ioctl_ops =	&cpia2_ioctl_ops,
1447 	.release =	video_device_release,
1448 };
1449 
1450 /******************************************************************************
1451  *
1452  *  cpia2_register_camera
1453  *
1454  *****************************************************************************/
cpia2_register_camera(struct camera_data * cam)1455 int cpia2_register_camera(struct camera_data *cam)
1456 {
1457 	cam->vdev = video_device_alloc();
1458 	if(!cam->vdev)
1459 		return -ENOMEM;
1460 
1461 	memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1462 	video_set_drvdata(cam->vdev, cam);
1463 	cam->vdev->lock = &cam->v4l2_lock;
1464 
1465 	reset_camera_struct_v4l(cam);
1466 
1467 	/* register v4l device */
1468 	if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1469 		ERR("video_register_device failed\n");
1470 		video_device_release(cam->vdev);
1471 		return -ENODEV;
1472 	}
1473 
1474 	return 0;
1475 }
1476 
1477 /******************************************************************************
1478  *
1479  *  cpia2_unregister_camera
1480  *
1481  *****************************************************************************/
cpia2_unregister_camera(struct camera_data * cam)1482 void cpia2_unregister_camera(struct camera_data *cam)
1483 {
1484 	if (!cam->open_count) {
1485 		video_unregister_device(cam->vdev);
1486 	} else {
1487 		LOG("%s removed while open, deferring "
1488 		    "video_unregister_device\n",
1489 		    video_device_node_name(cam->vdev));
1490 	}
1491 }
1492 
1493 /******************************************************************************
1494  *
1495  *  check_parameters
1496  *
1497  *  Make sure that all user-supplied parameters are sensible
1498  *****************************************************************************/
check_parameters(void)1499 static void __init check_parameters(void)
1500 {
1501 	if(buffer_size < PAGE_SIZE) {
1502 		buffer_size = PAGE_SIZE;
1503 		LOG("buffer_size too small, setting to %d\n", buffer_size);
1504 	} else if(buffer_size > 1024*1024) {
1505 		/* arbitrary upper limiit */
1506 		buffer_size = 1024*1024;
1507 		LOG("buffer_size ridiculously large, setting to %d\n",
1508 		    buffer_size);
1509 	} else {
1510 		buffer_size += PAGE_SIZE-1;
1511 		buffer_size &= ~(PAGE_SIZE-1);
1512 	}
1513 
1514 	if(num_buffers < 1) {
1515 		num_buffers = 1;
1516 		LOG("num_buffers too small, setting to %d\n", num_buffers);
1517 	} else if(num_buffers > VIDEO_MAX_FRAME) {
1518 		num_buffers = VIDEO_MAX_FRAME;
1519 		LOG("num_buffers too large, setting to %d\n", num_buffers);
1520 	}
1521 
1522 	if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1523 		alternate = DEFAULT_ALT;
1524 		LOG("alternate specified is invalid, using %d\n", alternate);
1525 	}
1526 
1527 	if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1528 		flicker_mode = NEVER_FLICKER;
1529 		LOG("Flicker mode specified is invalid, using %d\n",
1530 		    flicker_mode);
1531 	}
1532 
1533 	if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1534 		flicker_freq = FLICKER_60;
1535 		LOG("Flicker mode specified is invalid, using %d\n",
1536 		    flicker_freq);
1537 	}
1538 
1539 	if(video_nr < -1 || video_nr > 64) {
1540 		video_nr = -1;
1541 		LOG("invalid video_nr specified, must be -1 to 64\n");
1542 	}
1543 
1544 	DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1545 	    num_buffers, buffer_size, alternate);
1546 }
1547 
1548 /************   Module Stuff ***************/
1549 
1550 
1551 /******************************************************************************
1552  *
1553  * cpia2_init/module_init
1554  *
1555  *****************************************************************************/
cpia2_init(void)1556 static int __init cpia2_init(void)
1557 {
1558 	LOG("%s v%s\n",
1559 	    ABOUT, CPIA_VERSION);
1560 	check_parameters();
1561 	cpia2_usb_init();
1562 	return 0;
1563 }
1564 
1565 
1566 /******************************************************************************
1567  *
1568  * cpia2_exit/module_exit
1569  *
1570  *****************************************************************************/
cpia2_exit(void)1571 static void __exit cpia2_exit(void)
1572 {
1573 	cpia2_usb_cleanup();
1574 	schedule_timeout(2 * HZ);
1575 }
1576 
1577 module_init(cpia2_init);
1578 module_exit(cpia2_exit);
1579