Merge branch 'upstream-davem' of master.kernel.org:/pub/scm/linux/kernel/git/linville...
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / drivers / media / video / cpia2 / cpia2_v4l.c
CommitLineData
ab33d507
AC
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@redhat.com>
30 ****************************************************************************/
31
32#include <linux/version.h>
33
ab33d507
AC
34
35#include <linux/module.h>
36#include <linux/time.h>
37#include <linux/sched.h>
38#include <linux/slab.h>
39#include <linux/init.h>
40#include <linux/moduleparam.h>
41
42#include "cpia2.h"
43#include "cpia2dev.h"
44
45
46//#define _CPIA2_DEBUG_
47
48#define MAKE_STRING_1(x) #x
49#define MAKE_STRING(x) MAKE_STRING_1(x)
50
51static int video_nr = -1;
52module_param(video_nr, int, 0);
53MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
54
55static int buffer_size = 68*1024;
56module_param(buffer_size, int, 0);
57MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
58
59static int num_buffers = 3;
60module_param(num_buffers, int, 0);
61MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
62 MAKE_STRING(VIDEO_MAX_FRAME) ", default 3)");
63
64static int alternate = DEFAULT_ALT;
65module_param(alternate, int, 0);
66MODULE_PARM_DESC(alternate, "USB Alternate (" MAKE_STRING(USBIF_ISO_1) "-"
67 MAKE_STRING(USBIF_ISO_6) ", default "
68 MAKE_STRING(DEFAULT_ALT) ")");
69
70static int flicker_freq = 60;
71module_param(flicker_freq, int, 0);
72MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" MAKE_STRING(50) "or"
73 MAKE_STRING(60) ", default "
74 MAKE_STRING(60) ")");
75
76static int flicker_mode = NEVER_FLICKER;
77module_param(flicker_mode, int, 0);
78MODULE_PARM_DESC(flicker_mode,
79 "Flicker supression (" MAKE_STRING(NEVER_FLICKER) "or"
80 MAKE_STRING(ANTI_FLICKER_ON) ", default "
81 MAKE_STRING(NEVER_FLICKER) ")");
82
83MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
84MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
85MODULE_SUPPORTED_DEVICE("video");
86MODULE_LICENSE("GPL");
87
88#define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
89
90#ifndef VID_HARDWARE_CPIA2
91#error "VID_HARDWARE_CPIA2 should have been defined in linux/videodev.h"
92#endif
93
94struct control_menu_info {
95 int value;
96 char name[32];
97};
98
99static struct control_menu_info framerate_controls[] =
100{
101 { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
102 { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
103 { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
104 { CPIA2_VP_FRAMERATE_15, "15 fps" },
105 { CPIA2_VP_FRAMERATE_25, "25 fps" },
106 { CPIA2_VP_FRAMERATE_30, "30 fps" },
107};
0c71bf1c 108#define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
ab33d507
AC
109
110static struct control_menu_info flicker_controls[] =
111{
112 { NEVER_FLICKER, "Off" },
113 { FLICKER_50, "50 Hz" },
114 { FLICKER_60, "60 Hz" },
115};
0c71bf1c 116#define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
ab33d507
AC
117
118static struct control_menu_info lights_controls[] =
119{
120 { 0, "Off" },
121 { 64, "Top" },
122 { 128, "Bottom" },
123 { 192, "Both" },
124};
0c71bf1c 125#define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
ab33d507
AC
126#define GPIO_LIGHTS_MASK 192
127
128static struct v4l2_queryctrl controls[] = {
129 {
130 .id = V4L2_CID_BRIGHTNESS,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Brightness",
133 .minimum = 0,
134 .maximum = 255,
135 .step = 1,
136 .default_value = DEFAULT_BRIGHTNESS,
137 },
138 {
139 .id = V4L2_CID_CONTRAST,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "Contrast",
142 .minimum = 0,
143 .maximum = 255,
144 .step = 1,
145 .default_value = DEFAULT_CONTRAST,
146 },
147 {
148 .id = V4L2_CID_SATURATION,
149 .type = V4L2_CTRL_TYPE_INTEGER,
150 .name = "Saturation",
151 .minimum = 0,
152 .maximum = 255,
153 .step = 1,
154 .default_value = DEFAULT_SATURATION,
155 },
156 {
157 .id = V4L2_CID_HFLIP,
158 .type = V4L2_CTRL_TYPE_BOOLEAN,
159 .name = "Mirror Horizontally",
160 .minimum = 0,
161 .maximum = 1,
162 .step = 1,
163 .default_value = 0,
164 },
165 {
166 .id = V4L2_CID_VFLIP,
167 .type = V4L2_CTRL_TYPE_BOOLEAN,
168 .name = "Flip Vertically",
169 .minimum = 0,
170 .maximum = 1,
171 .step = 1,
172 .default_value = 0,
173 },
174 {
175 .id = CPIA2_CID_TARGET_KB,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Target KB",
178 .minimum = 0,
179 .maximum = 255,
180 .step = 1,
181 .default_value = DEFAULT_TARGET_KB,
182 },
183 {
184 .id = CPIA2_CID_GPIO,
185 .type = V4L2_CTRL_TYPE_INTEGER,
186 .name = "GPIO",
187 .minimum = 0,
188 .maximum = 255,
189 .step = 1,
190 .default_value = 0,
191 },
192 {
193 .id = CPIA2_CID_FLICKER_MODE,
194 .type = V4L2_CTRL_TYPE_MENU,
195 .name = "Flicker Reduction",
196 .minimum = 0,
197 .maximum = NUM_FLICKER_CONTROLS-1,
198 .step = 1,
199 .default_value = 0,
200 },
201 {
202 .id = CPIA2_CID_FRAMERATE,
203 .type = V4L2_CTRL_TYPE_MENU,
204 .name = "Framerate",
205 .minimum = 0,
206 .maximum = NUM_FRAMERATE_CONTROLS-1,
207 .step = 1,
208 .default_value = NUM_FRAMERATE_CONTROLS-1,
209 },
210 {
211 .id = CPIA2_CID_USB_ALT,
212 .type = V4L2_CTRL_TYPE_INTEGER,
213 .name = "USB Alternate",
214 .minimum = USBIF_ISO_1,
215 .maximum = USBIF_ISO_6,
216 .step = 1,
217 .default_value = DEFAULT_ALT,
218 },
219 {
220 .id = CPIA2_CID_LIGHTS,
221 .type = V4L2_CTRL_TYPE_MENU,
222 .name = "Lights",
223 .minimum = 0,
224 .maximum = NUM_LIGHTS_CONTROLS-1,
225 .step = 1,
226 .default_value = 0,
227 },
228 {
229 .id = CPIA2_CID_RESET_CAMERA,
230 .type = V4L2_CTRL_TYPE_BUTTON,
231 .name = "Reset Camera",
232 .minimum = 0,
233 .maximum = 0,
234 .step = 0,
235 .default_value = 0,
236 },
237};
0c71bf1c 238#define NUM_CONTROLS (ARRAY_SIZE(controls))
ab33d507
AC
239
240
241/******************************************************************************
242 *
243 * cpia2_open
244 *
245 *****************************************************************************/
246static int cpia2_open(struct inode *inode, struct file *file)
247{
248 struct video_device *dev = video_devdata(file);
249 struct camera_data *cam = video_get_drvdata(dev);
250 int retval = 0;
251
252 if (!cam) {
253 ERR("Internal error, camera_data not found!\n");
254 return -ENODEV;
255 }
256
2ae15191 257 if(mutex_lock_interruptible(&cam->busy_lock))
ab33d507
AC
258 return -ERESTARTSYS;
259
260 if(!cam->present) {
261 retval = -ENODEV;
262 goto err_return;
263 }
264
265 if (cam->open_count > 0) {
266 goto skip_init;
267 }
268
269 if (cpia2_allocate_buffers(cam)) {
270 retval = -ENOMEM;
271 goto err_return;
272 }
273
274 /* reset the camera */
275 if (cpia2_reset_camera(cam) < 0) {
276 retval = -EIO;
277 goto err_return;
278 }
279
280 cam->APP_len = 0;
281 cam->COM_len = 0;
282
283skip_init:
284 {
285 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
286 if(!fh) {
287 retval = -ENOMEM;
288 goto err_return;
289 }
290 file->private_data = fh;
291 fh->prio = V4L2_PRIORITY_UNSET;
292 v4l2_prio_open(&cam->prio, &fh->prio);
293 fh->mmapped = 0;
294 }
295
296 ++cam->open_count;
297
298 cpia2_dbg_dump_registers(cam);
299
300err_return:
2ae15191 301 mutex_unlock(&cam->busy_lock);
ab33d507
AC
302 return retval;
303}
304
305/******************************************************************************
306 *
307 * cpia2_close
308 *
309 *****************************************************************************/
310static int cpia2_close(struct inode *inode, struct file *file)
311{
312 struct video_device *dev = video_devdata(file);
313 struct camera_data *cam = video_get_drvdata(dev);
314 struct cpia2_fh *fh = file->private_data;
315
2ae15191 316 mutex_lock(&cam->busy_lock);
ab33d507
AC
317
318 if (cam->present &&
319 (cam->open_count == 1
320 || fh->prio == V4L2_PRIORITY_RECORD
321 )) {
322 cpia2_usb_stream_stop(cam);
323
324 if(cam->open_count == 1) {
325 /* save camera state for later open */
326 cpia2_save_camera_state(cam);
327
328 cpia2_set_low_power(cam);
329 cpia2_free_buffers(cam);
330 }
331 }
332
333 {
334 if(fh->mmapped)
335 cam->mmapped = 0;
336 v4l2_prio_close(&cam->prio,&fh->prio);
337 file->private_data = NULL;
338 kfree(fh);
339 }
340
341 if (--cam->open_count == 0) {
342 cpia2_free_buffers(cam);
343 if (!cam->present) {
344 video_unregister_device(dev);
bafefc0c 345 mutex_unlock(&cam->busy_lock);
ab33d507 346 kfree(cam);
bafefc0c 347 return 0;
ab33d507
AC
348 }
349 }
350
2ae15191 351 mutex_unlock(&cam->busy_lock);
ab33d507
AC
352
353 return 0;
354}
355
356/******************************************************************************
357 *
358 * cpia2_v4l_read
359 *
360 *****************************************************************************/
361static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
362 loff_t *off)
363{
364 struct video_device *dev = video_devdata(file);
365 struct camera_data *cam = video_get_drvdata(dev);
366 int noblock = file->f_flags&O_NONBLOCK;
367
368 struct cpia2_fh *fh = file->private_data;
369
370 if(!cam)
371 return -EINVAL;
372
373 /* Priority check */
374 if(fh->prio != V4L2_PRIORITY_RECORD) {
375 return -EBUSY;
376 }
377
378 return cpia2_read(cam, buf, count, noblock);
379}
380
381
382/******************************************************************************
383 *
384 * cpia2_v4l_poll
385 *
386 *****************************************************************************/
387static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
388{
389 struct video_device *dev = video_devdata(filp);
390 struct camera_data *cam = video_get_drvdata(dev);
391
392 struct cpia2_fh *fh = filp->private_data;
393
394 if(!cam)
395 return POLLERR;
396
397 /* Priority check */
398 if(fh->prio != V4L2_PRIORITY_RECORD) {
399 return POLLERR;
400 }
401
402 return cpia2_poll(cam, filp, wait);
403}
404
405
406/******************************************************************************
407 *
408 * ioctl_cap_query
409 *
410 *****************************************************************************/
411static int ioctl_cap_query(void *arg, struct camera_data *cam)
412{
413 struct video_capability *vc;
414 int retval = 0;
415 vc = arg;
416
417 if (cam->params.pnp_id.product == 0x151)
418 strcpy(vc->name, "QX5 Microscope");
419 else
420 strcpy(vc->name, "CPiA2 Camera");
421
422 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
423 vc->channels = 1;
424 vc->audios = 0;
425 vc->minwidth = 176; /* VIDEOSIZE_QCIF */
426 vc->minheight = 144;
427 switch (cam->params.version.sensor_flags) {
428 case CPIA2_VP_SENSOR_FLAGS_500:
429 vc->maxwidth = STV_IMAGE_VGA_COLS;
430 vc->maxheight = STV_IMAGE_VGA_ROWS;
431 break;
432 case CPIA2_VP_SENSOR_FLAGS_410:
433 vc->maxwidth = STV_IMAGE_CIF_COLS;
434 vc->maxheight = STV_IMAGE_CIF_ROWS;
435 break;
436 default:
437 return -EINVAL;
438 }
439
440 return retval;
441}
442
443/******************************************************************************
444 *
445 * ioctl_get_channel
446 *
447 *****************************************************************************/
448static int ioctl_get_channel(void *arg)
449{
450 int retval = 0;
451 struct video_channel *v;
452 v = arg;
453
454 if (v->channel != 0)
455 return -EINVAL;
456
457 v->channel = 0;
458 strcpy(v->name, "Camera");
459 v->tuners = 0;
460 v->flags = 0;
461 v->type = VIDEO_TYPE_CAMERA;
462 v->norm = 0;
463
464 return retval;
465}
466
467/******************************************************************************
468 *
469 * ioctl_set_channel
470 *
471 *****************************************************************************/
472static int ioctl_set_channel(void *arg)
473{
474 struct video_channel *v;
475 int retval = 0;
476 v = arg;
477
478 if (retval == 0 && v->channel != 0)
479 retval = -EINVAL;
480
481 return retval;
482}
483
484/******************************************************************************
485 *
486 * ioctl_set_image_prop
487 *
488 *****************************************************************************/
489static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
490{
491 struct video_picture *vp;
492 int retval = 0;
493 vp = arg;
494
495 /* brightness, color, contrast need no check 0-65535 */
496 memcpy(&cam->vp, vp, sizeof(*vp));
497
498 /* update cam->params.colorParams */
499 cam->params.color_params.brightness = vp->brightness / 256;
500 cam->params.color_params.saturation = vp->colour / 256;
501 cam->params.color_params.contrast = vp->contrast / 256;
502
503 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
504 cam->params.color_params.brightness,
505 cam->params.color_params.saturation,
506 cam->params.color_params.contrast);
507
508 cpia2_set_color_params(cam);
509
510 return retval;
511}
512
513static int sync(struct camera_data *cam, int frame_nr)
514{
515 struct framebuf *frame = &cam->buffers[frame_nr];
516
517 while (1) {
518 if (frame->status == FRAME_READY)
519 return 0;
520
521 if (!cam->streaming) {
522 frame->status = FRAME_READY;
523 frame->length = 0;
524 return 0;
525 }
526
2ae15191 527 mutex_unlock(&cam->busy_lock);
ab33d507
AC
528 wait_event_interruptible(cam->wq_stream,
529 !cam->streaming ||
530 frame->status == FRAME_READY);
2ae15191 531 mutex_lock(&cam->busy_lock);
ab33d507
AC
532 if (signal_pending(current))
533 return -ERESTARTSYS;
534 if(!cam->present)
535 return -ENOTTY;
536 }
537}
538
539/******************************************************************************
540 *
541 * ioctl_set_window_size
542 *
543 *****************************************************************************/
544static int ioctl_set_window_size(void *arg, struct camera_data *cam,
545 struct cpia2_fh *fh)
546{
547 /* copy_from_user, check validity, copy to internal structure */
548 struct video_window *vw;
549 int frame, err;
550 vw = arg;
551
552 if (vw->clipcount != 0) /* clipping not supported */
553 return -EINVAL;
554
555 if (vw->clips != NULL) /* clipping not supported */
556 return -EINVAL;
557
558 /* Ensure that only this process can change the format. */
559 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
560 if(err != 0)
561 return err;
562
563 cam->pixelformat = V4L2_PIX_FMT_JPEG;
564
565 /* Be sure to supply the Huffman tables, this isn't MJPEG */
566 cam->params.compression.inhibit_htables = 0;
567
568 /* we set the video window to something smaller or equal to what
569 * is requested by the user???
570 */
571 DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
572 if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
573 cam->vw.width = vw->width;
574 cam->vw.height = vw->height;
575 cam->params.roi.width = vw->width;
576 cam->params.roi.height = vw->height;
577 cpia2_set_format(cam);
578 }
579
580 for (frame = 0; frame < cam->num_frames; ++frame) {
581 if (cam->buffers[frame].status == FRAME_READING)
582 if ((err = sync(cam, frame)) < 0)
583 return err;
584
585 cam->buffers[frame].status = FRAME_EMPTY;
586 }
587
588 return 0;
589}
590
591/******************************************************************************
592 *
593 * ioctl_get_mbuf
594 *
595 *****************************************************************************/
596static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
597{
598 struct video_mbuf *vm;
599 int i;
600 vm = arg;
601
602 memset(vm, 0, sizeof(*vm));
603 vm->size = cam->frame_size*cam->num_frames;
604 vm->frames = cam->num_frames;
605 for (i = 0; i < cam->num_frames; i++)
606 vm->offsets[i] = cam->frame_size * i;
607
608 return 0;
609}
610
611/******************************************************************************
612 *
613 * ioctl_mcapture
614 *
615 *****************************************************************************/
616static int ioctl_mcapture(void *arg, struct camera_data *cam,
617 struct cpia2_fh *fh)
618{
619 struct video_mmap *vm;
620 int video_size, err;
621 vm = arg;
622
623 if (vm->frame < 0 || vm->frame >= cam->num_frames)
624 return -EINVAL;
625
626 /* set video size */
627 video_size = cpia2_match_video_size(vm->width, vm->height);
628 if (cam->video_size < 0) {
629 return -EINVAL;
630 }
631
632 /* Ensure that only this process can change the format. */
633 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
634 if(err != 0)
635 return err;
636
637 if (video_size != cam->video_size) {
638 cam->video_size = video_size;
639 cam->params.roi.width = vm->width;
640 cam->params.roi.height = vm->height;
641 cpia2_set_format(cam);
642 }
643
644 if (cam->buffers[vm->frame].status == FRAME_READING)
645 if ((err=sync(cam, vm->frame)) < 0)
646 return err;
647
648 cam->buffers[vm->frame].status = FRAME_EMPTY;
649
650 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
651}
652
653/******************************************************************************
654 *
655 * ioctl_sync
656 *
657 *****************************************************************************/
658static int ioctl_sync(void *arg, struct camera_data *cam)
659{
660 int frame;
661
662 frame = *(int*)arg;
663
664 if (frame < 0 || frame >= cam->num_frames)
665 return -EINVAL;
666
667 return sync(cam, frame);
668}
669
670
671/******************************************************************************
672 *
673 * ioctl_set_gpio
674 *
675 *****************************************************************************/
676
677static int ioctl_set_gpio(void *arg, struct camera_data *cam)
678{
679 __u32 gpio_val;
680
681 gpio_val = *(__u32*) arg;
682
683 if (gpio_val &~ 0xFFU)
684 return -EINVAL;
685
686 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
687}
688
689/******************************************************************************
690 *
691 * ioctl_querycap
692 *
693 * V4L2 device capabilities
694 *
695 *****************************************************************************/
696
697static int ioctl_querycap(void *arg, struct camera_data *cam)
698{
699 struct v4l2_capability *vc = arg;
700
701 memset(vc, 0, sizeof(*vc));
702 strcpy(vc->driver, "cpia2");
703
704 if (cam->params.pnp_id.product == 0x151)
705 strcpy(vc->card, "QX5 Microscope");
706 else
707 strcpy(vc->card, "CPiA2 Camera");
708 switch (cam->params.pnp_id.device_type) {
709 case DEVICE_STV_672:
710 strcat(vc->card, " (672/");
711 break;
712 case DEVICE_STV_676:
713 strcat(vc->card, " (676/");
714 break;
715 default:
716 strcat(vc->card, " (???/");
717 break;
718 }
719 switch (cam->params.version.sensor_flags) {
720 case CPIA2_VP_SENSOR_FLAGS_404:
721 strcat(vc->card, "404)");
722 break;
723 case CPIA2_VP_SENSOR_FLAGS_407:
724 strcat(vc->card, "407)");
725 break;
726 case CPIA2_VP_SENSOR_FLAGS_409:
727 strcat(vc->card, "409)");
728 break;
729 case CPIA2_VP_SENSOR_FLAGS_410:
730 strcat(vc->card, "410)");
731 break;
732 case CPIA2_VP_SENSOR_FLAGS_500:
733 strcat(vc->card, "500)");
734 break;
735 default:
736 strcat(vc->card, "???)");
737 break;
738 }
739
740 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
741 memset(vc->bus_info,0, sizeof(vc->bus_info));
742
743 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
744 CPIA2_PATCH_VER);
745
746 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
747 V4L2_CAP_READWRITE |
748 V4L2_CAP_STREAMING;
749
750 return 0;
751}
752
753/******************************************************************************
754 *
755 * ioctl_input
756 *
757 * V4L2 input get/set/enumerate
758 *
759 *****************************************************************************/
760
761static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
762{
763 struct v4l2_input *i = arg;
764
765 if(ioclt_nr != VIDIOC_G_INPUT) {
766 if (i->index != 0)
767 return -EINVAL;
768 }
769
770 memset(i, 0, sizeof(*i));
771 strcpy(i->name, "Camera");
772 i->type = V4L2_INPUT_TYPE_CAMERA;
773
774 return 0;
775}
776
777/******************************************************************************
778 *
779 * ioctl_enum_fmt
780 *
781 * V4L2 format enumerate
782 *
783 *****************************************************************************/
784
785static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
786{
787 struct v4l2_fmtdesc *f = arg;
788 int index = f->index;
789
790 if (index < 0 || index > 1)
791 return -EINVAL;
792
793 memset(f, 0, sizeof(*f));
794 f->index = index;
795 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
796 f->flags = V4L2_FMT_FLAG_COMPRESSED;
797 switch(index) {
798 case 0:
799 strcpy(f->description, "MJPEG");
800 f->pixelformat = V4L2_PIX_FMT_MJPEG;
801 break;
802 case 1:
803 strcpy(f->description, "JPEG");
804 f->pixelformat = V4L2_PIX_FMT_JPEG;
805 break;
806 default:
807 return -EINVAL;
808 }
809
810 return 0;
811}
812
813/******************************************************************************
814 *
815 * ioctl_try_fmt
816 *
817 * V4L2 format try
818 *
819 *****************************************************************************/
820
821static int ioctl_try_fmt(void *arg,struct camera_data *cam)
822{
823 struct v4l2_format *f = arg;
824
825 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
826 return -EINVAL;
827
828 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
829 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
830 return -EINVAL;
831
832 f->fmt.pix.field = V4L2_FIELD_NONE;
833 f->fmt.pix.bytesperline = 0;
834 f->fmt.pix.sizeimage = cam->frame_size;
835 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
836 f->fmt.pix.priv = 0;
837
838 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
839 case VIDEOSIZE_VGA:
840 f->fmt.pix.width = 640;
841 f->fmt.pix.height = 480;
842 break;
843 case VIDEOSIZE_CIF:
844 f->fmt.pix.width = 352;
845 f->fmt.pix.height = 288;
846 break;
847 case VIDEOSIZE_QVGA:
848 f->fmt.pix.width = 320;
849 f->fmt.pix.height = 240;
850 break;
851 case VIDEOSIZE_288_216:
852 f->fmt.pix.width = 288;
853 f->fmt.pix.height = 216;
854 break;
855 case VIDEOSIZE_256_192:
856 f->fmt.pix.width = 256;
857 f->fmt.pix.height = 192;
858 break;
859 case VIDEOSIZE_224_168:
860 f->fmt.pix.width = 224;
861 f->fmt.pix.height = 168;
862 break;
863 case VIDEOSIZE_192_144:
864 f->fmt.pix.width = 192;
865 f->fmt.pix.height = 144;
866 break;
867 case VIDEOSIZE_QCIF:
868 default:
869 f->fmt.pix.width = 176;
870 f->fmt.pix.height = 144;
871 break;
872 }
873
874 return 0;
875}
876
877/******************************************************************************
878 *
879 * ioctl_set_fmt
880 *
881 * V4L2 format set
882 *
883 *****************************************************************************/
884
885static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
886{
887 struct v4l2_format *f = arg;
888 int err, frame;
889
890 err = ioctl_try_fmt(arg, cam);
891 if(err != 0)
892 return err;
893
894 /* Ensure that only this process can change the format. */
895 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
896 if(err != 0) {
897 return err;
898 }
899
900 cam->pixelformat = f->fmt.pix.pixelformat;
901
902 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
903 * the missing Huffman table properly. */
904 cam->params.compression.inhibit_htables = 0;
905 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
906
907 /* we set the video window to something smaller or equal to what
908 * is requested by the user???
909 */
910 DBG("Requested width = %d, height = %d\n",
911 f->fmt.pix.width, f->fmt.pix.height);
912 if (f->fmt.pix.width != cam->vw.width ||
913 f->fmt.pix.height != cam->vw.height) {
914 cam->vw.width = f->fmt.pix.width;
915 cam->vw.height = f->fmt.pix.height;
916 cam->params.roi.width = f->fmt.pix.width;
917 cam->params.roi.height = f->fmt.pix.height;
918 cpia2_set_format(cam);
919 }
920
921 for (frame = 0; frame < cam->num_frames; ++frame) {
922 if (cam->buffers[frame].status == FRAME_READING)
923 if ((err = sync(cam, frame)) < 0)
924 return err;
925
926 cam->buffers[frame].status = FRAME_EMPTY;
927 }
928
929 return 0;
930}
931
932/******************************************************************************
933 *
934 * ioctl_get_fmt
935 *
936 * V4L2 format get
937 *
938 *****************************************************************************/
939
940static int ioctl_get_fmt(void *arg,struct camera_data *cam)
941{
942 struct v4l2_format *f = arg;
943
944 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945 return -EINVAL;
946
947 f->fmt.pix.width = cam->vw.width;
948 f->fmt.pix.height = cam->vw.height;
949 f->fmt.pix.pixelformat = cam->pixelformat;
950 f->fmt.pix.field = V4L2_FIELD_NONE;
951 f->fmt.pix.bytesperline = 0;
952 f->fmt.pix.sizeimage = cam->frame_size;
953 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
954 f->fmt.pix.priv = 0;
955
956 return 0;
957}
958
959/******************************************************************************
960 *
961 * ioctl_cropcap
962 *
963 * V4L2 query cropping capabilities
964 * NOTE: cropping is currently disabled
965 *
966 *****************************************************************************/
967
968static int ioctl_cropcap(void *arg,struct camera_data *cam)
969{
970 struct v4l2_cropcap *c = arg;
971
972 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
973 return -EINVAL;
974
975 c->bounds.left = 0;
976 c->bounds.top = 0;
977 c->bounds.width = cam->vw.width;
978 c->bounds.height = cam->vw.height;
979 c->defrect.left = 0;
980 c->defrect.top = 0;
981 c->defrect.width = cam->vw.width;
982 c->defrect.height = cam->vw.height;
983 c->pixelaspect.numerator = 1;
984 c->pixelaspect.denominator = 1;
985
986 return 0;
987}
988
989/******************************************************************************
990 *
991 * ioctl_queryctrl
992 *
993 * V4L2 query possible control variables
994 *
995 *****************************************************************************/
996
997static int ioctl_queryctrl(void *arg,struct camera_data *cam)
998{
999 struct v4l2_queryctrl *c = arg;
1000 int i;
1001
1002 for(i=0; i<NUM_CONTROLS; ++i) {
1003 if(c->id == controls[i].id) {
1004 memcpy(c, controls+i, sizeof(*c));
1005 break;
1006 }
1007 }
1008
1009 if(i == NUM_CONTROLS)
1010 return -EINVAL;
1011
1012 /* Some devices have additional limitations */
1013 switch(c->id) {
1014 case V4L2_CID_BRIGHTNESS:
1015 /***
1016 * Don't let the register be set to zero - bug in VP4
1017 * flash of full brightness
1018 ***/
1019 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1020 c->minimum = 1;
1021 break;
1022 case V4L2_CID_VFLIP:
1023 // VP5 Only
1024 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
1025 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1026 break;
1027 case CPIA2_CID_FRAMERATE:
1028 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1029 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1030 // Maximum 15fps
1031 int i;
1032 for(i=0; i<c->maximum; ++i) {
1033 if(framerate_controls[i].value ==
1034 CPIA2_VP_FRAMERATE_15) {
1035 c->maximum = i;
1036 c->default_value = i;
1037 }
1038 }
1039 }
1040 break;
1041 case CPIA2_CID_FLICKER_MODE:
1042 // Flicker control only valid for 672.
1043 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1044 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1045 break;
1046 case CPIA2_CID_LIGHTS:
1047 // Light control only valid for the QX5 Microscope.
1048 if(cam->params.pnp_id.product != 0x151)
1049 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1050 break;
1051 default:
1052 break;
1053 }
1054
1055 return 0;
1056}
1057
1058/******************************************************************************
1059 *
1060 * ioctl_querymenu
1061 *
1062 * V4L2 query possible control variables
1063 *
1064 *****************************************************************************/
1065
1066static int ioctl_querymenu(void *arg,struct camera_data *cam)
1067{
1068 struct v4l2_querymenu *m = arg;
1069
1070 memset(m->name, 0, sizeof(m->name));
1071 m->reserved = 0;
1072
1073 switch(m->id) {
1074 case CPIA2_CID_FLICKER_MODE:
1075 if(m->index < 0 || m->index >= NUM_FLICKER_CONTROLS)
1076 return -EINVAL;
1077
1078 strcpy(m->name, flicker_controls[m->index].name);
1079 break;
1080 case CPIA2_CID_FRAMERATE:
1081 {
1082 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1083 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1084 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1085 // Maximum 15fps
1086 int i;
1087 for(i=0; i<maximum; ++i) {
1088 if(framerate_controls[i].value ==
1089 CPIA2_VP_FRAMERATE_15)
1090 maximum = i;
1091 }
1092 }
1093 if(m->index < 0 || m->index > maximum)
1094 return -EINVAL;
1095
1096 strcpy(m->name, framerate_controls[m->index].name);
1097 break;
1098 }
1099 case CPIA2_CID_LIGHTS:
1100 if(m->index < 0 || m->index >= NUM_LIGHTS_CONTROLS)
1101 return -EINVAL;
1102
1103 strcpy(m->name, lights_controls[m->index].name);
1104 break;
1105 default:
1106 return -EINVAL;
1107 }
1108
1109 return 0;
1110}
1111
1112/******************************************************************************
1113 *
1114 * ioctl_g_ctrl
1115 *
1116 * V4L2 get the value of a control variable
1117 *
1118 *****************************************************************************/
1119
1120static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
1121{
1122 struct v4l2_control *c = arg;
1123
1124 switch(c->id) {
1125 case V4L2_CID_BRIGHTNESS:
1126 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
1127 TRANSFER_READ, 0);
1128 c->value = cam->params.color_params.brightness;
1129 break;
1130 case V4L2_CID_CONTRAST:
1131 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
1132 TRANSFER_READ, 0);
1133 c->value = cam->params.color_params.contrast;
1134 break;
1135 case V4L2_CID_SATURATION:
1136 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
1137 TRANSFER_READ, 0);
1138 c->value = cam->params.color_params.saturation;
1139 break;
1140 case V4L2_CID_HFLIP:
1141 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1142 TRANSFER_READ, 0);
1143 c->value = (cam->params.vp_params.user_effects &
1144 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
1145 break;
1146 case V4L2_CID_VFLIP:
1147 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1148 TRANSFER_READ, 0);
1149 c->value = (cam->params.vp_params.user_effects &
1150 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
1151 break;
1152 case CPIA2_CID_TARGET_KB:
1153 c->value = cam->params.vc_params.target_kb;
1154 break;
1155 case CPIA2_CID_GPIO:
1156 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1157 TRANSFER_READ, 0);
1158 c->value = cam->params.vp_params.gpio_data;
1159 break;
1160 case CPIA2_CID_FLICKER_MODE:
1161 {
1162 int i, mode;
1163 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1164 TRANSFER_READ, 0);
1165 if(cam->params.flicker_control.cam_register &
1166 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
1167 mode = NEVER_FLICKER;
1168 } else {
1169 if(cam->params.flicker_control.cam_register &
1170 CPIA2_VP_FLICKER_MODES_50HZ) {
657de3cd 1171 mode = FLICKER_50;
ab33d507 1172 } else {
657de3cd 1173 mode = FLICKER_60;
ab33d507
AC
1174 }
1175 }
1176 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
1177 if(flicker_controls[i].value == mode) {
1178 c->value = i;
1179 break;
1180 }
1181 }
1182 if(i == NUM_FLICKER_CONTROLS)
1183 return -EINVAL;
1184 break;
1185 }
1186 case CPIA2_CID_FRAMERATE:
1187 {
1188 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1189 int i;
1190 for(i=0; i<= maximum; i++) {
1191 if(cam->params.vp_params.frame_rate ==
1192 framerate_controls[i].value)
1193 break;
1194 }
1195 if(i > maximum)
1196 return -EINVAL;
1197 c->value = i;
1198 break;
1199 }
1200 case CPIA2_CID_USB_ALT:
1201 c->value = cam->params.camera_state.stream_mode;
1202 break;
1203 case CPIA2_CID_LIGHTS:
1204 {
1205 int i;
1206 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1207 TRANSFER_READ, 0);
1208 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
1209 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
1210 lights_controls[i].value) {
1211 break;
1212 }
1213 }
1214 if(i == NUM_LIGHTS_CONTROLS)
1215 return -EINVAL;
1216 c->value = i;
1217 break;
1218 }
1219 case CPIA2_CID_RESET_CAMERA:
1220 return -EINVAL;
1221 default:
1222 return -EINVAL;
1223 }
1224
1225 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1226
1227 return 0;
1228}
1229
1230/******************************************************************************
1231 *
1232 * ioctl_s_ctrl
1233 *
1234 * V4L2 set the value of a control variable
1235 *
1236 *****************************************************************************/
1237
1238static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1239{
1240 struct v4l2_control *c = arg;
1241 int i;
1242 int retval = 0;
1243
1244 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1245
1246 /* Check that the value is in range */
1247 for(i=0; i<NUM_CONTROLS; i++) {
1248 if(c->id == controls[i].id) {
1249 if(c->value < controls[i].minimum ||
1250 c->value > controls[i].maximum) {
1251 return -EINVAL;
1252 }
1253 break;
1254 }
1255 }
1256 if(i == NUM_CONTROLS)
1257 return -EINVAL;
1258
1259 switch(c->id) {
1260 case V4L2_CID_BRIGHTNESS:
1261 cpia2_set_brightness(cam, c->value);
1262 break;
1263 case V4L2_CID_CONTRAST:
1264 cpia2_set_contrast(cam, c->value);
1265 break;
1266 case V4L2_CID_SATURATION:
1267 cpia2_set_saturation(cam, c->value);
1268 break;
1269 case V4L2_CID_HFLIP:
1270 cpia2_set_property_mirror(cam, c->value);
1271 break;
1272 case V4L2_CID_VFLIP:
1273 cpia2_set_property_flip(cam, c->value);
1274 break;
1275 case CPIA2_CID_TARGET_KB:
1276 retval = cpia2_set_target_kb(cam, c->value);
1277 break;
1278 case CPIA2_CID_GPIO:
1279 retval = cpia2_set_gpio(cam, c->value);
1280 break;
1281 case CPIA2_CID_FLICKER_MODE:
1282 retval = cpia2_set_flicker_mode(cam,
1283 flicker_controls[c->value].value);
1284 break;
1285 case CPIA2_CID_FRAMERATE:
1286 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1287 break;
1288 case CPIA2_CID_USB_ALT:
1289 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1290 break;
1291 case CPIA2_CID_LIGHTS:
1292 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1293 break;
1294 case CPIA2_CID_RESET_CAMERA:
1295 cpia2_usb_stream_pause(cam);
1296 cpia2_reset_camera(cam);
1297 cpia2_usb_stream_resume(cam);
1298 break;
1299 default:
1300 retval = -EINVAL;
1301 }
1302
1303 return retval;
1304}
1305
1306/******************************************************************************
1307 *
1308 * ioctl_g_jpegcomp
1309 *
1310 * V4L2 get the JPEG compression parameters
1311 *
1312 *****************************************************************************/
1313
1314static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1315{
1316 struct v4l2_jpegcompression *parms = arg;
1317
1318 memset(parms, 0, sizeof(*parms));
1319
1320 parms->quality = 80; // TODO: Can this be made meaningful?
1321
1322 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1323 if(!cam->params.compression.inhibit_htables) {
1324 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1325 }
1326
1327 parms->APPn = cam->APPn;
1328 parms->APP_len = cam->APP_len;
1329 if(cam->APP_len > 0) {
1330 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1331 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1332 }
1333
1334 parms->COM_len = cam->COM_len;
1335 if(cam->COM_len > 0) {
1336 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1337 parms->jpeg_markers |= JPEG_MARKER_COM;
1338 }
1339
1340 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1341 parms->APP_len, parms->COM_len);
1342
1343 return 0;
1344}
1345
1346/******************************************************************************
1347 *
1348 * ioctl_s_jpegcomp
1349 *
1350 * V4L2 set the JPEG compression parameters
1351 * NOTE: quality and some jpeg_markers are ignored.
1352 *
1353 *****************************************************************************/
1354
1355static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1356{
1357 struct v4l2_jpegcompression *parms = arg;
1358
1359 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1360 parms->APP_len, parms->COM_len);
1361
1362 cam->params.compression.inhibit_htables =
1363 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1364
1365 if(parms->APP_len != 0) {
1366 if(parms->APP_len > 0 &&
1367 parms->APP_len <= sizeof(cam->APP_data) &&
1368 parms->APPn >= 0 && parms->APPn <= 15) {
1369 cam->APPn = parms->APPn;
1370 cam->APP_len = parms->APP_len;
1371 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1372 } else {
1373 LOG("Bad APPn Params n=%d len=%d\n",
1374 parms->APPn, parms->APP_len);
1375 return -EINVAL;
1376 }
1377 } else {
1378 cam->APP_len = 0;
1379 }
1380
1381 if(parms->COM_len != 0) {
1382 if(parms->COM_len > 0 &&
1383 parms->COM_len <= sizeof(cam->COM_data)) {
1384 cam->COM_len = parms->COM_len;
1385 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1386 } else {
1387 LOG("Bad COM_len=%d\n", parms->COM_len);
1388 return -EINVAL;
1389 }
1390 }
1391
1392 return 0;
1393}
1394
1395/******************************************************************************
1396 *
1397 * ioctl_reqbufs
1398 *
1399 * V4L2 Initiate memory mapping.
1400 * NOTE: The user's request is ignored. For now the buffers are fixed.
1401 *
1402 *****************************************************************************/
1403
1404static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1405{
1406 struct v4l2_requestbuffers *req = arg;
1407
1408 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1409 req->memory != V4L2_MEMORY_MMAP)
1410 return -EINVAL;
1411
1412 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1413 req->count = cam->num_frames;
1414 memset(&req->reserved, 0, sizeof(req->reserved));
1415
1416 return 0;
1417}
1418
1419/******************************************************************************
1420 *
1421 * ioctl_querybuf
1422 *
1423 * V4L2 Query memory buffer status.
1424 *
1425 *****************************************************************************/
1426
1427static int ioctl_querybuf(void *arg,struct camera_data *cam)
1428{
1429 struct v4l2_buffer *buf = arg;
1430
1431 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1432 buf->index > cam->num_frames)
1433 return -EINVAL;
1434
1435 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1436 buf->length = cam->frame_size;
1437
1438 buf->memory = V4L2_MEMORY_MMAP;
1439
1440 if(cam->mmapped)
1441 buf->flags = V4L2_BUF_FLAG_MAPPED;
1442 else
1443 buf->flags = 0;
1444
1445 switch (cam->buffers[buf->index].status) {
1446 case FRAME_EMPTY:
1447 case FRAME_ERROR:
1448 case FRAME_READING:
1449 buf->bytesused = 0;
1450 buf->flags = V4L2_BUF_FLAG_QUEUED;
1451 break;
1452 case FRAME_READY:
1453 buf->bytesused = cam->buffers[buf->index].length;
1454 buf->timestamp = cam->buffers[buf->index].timestamp;
1455 buf->sequence = cam->buffers[buf->index].seq;
1456 buf->flags = V4L2_BUF_FLAG_DONE;
1457 break;
1458 }
1459
1460 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1461 buf->index, buf->m.offset, buf->flags, buf->sequence,
1462 buf->bytesused);
1463
1464 return 0;
1465}
1466
1467/******************************************************************************
1468 *
1469 * ioctl_qbuf
1470 *
1471 * V4L2 User is freeing buffer
1472 *
1473 *****************************************************************************/
1474
1475static int ioctl_qbuf(void *arg,struct camera_data *cam)
1476{
1477 struct v4l2_buffer *buf = arg;
1478
1479 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1480 buf->memory != V4L2_MEMORY_MMAP ||
1481 buf->index > cam->num_frames)
1482 return -EINVAL;
1483
1484 DBG("QBUF #%d\n", buf->index);
1485
1486 if(cam->buffers[buf->index].status == FRAME_READY)
1487 cam->buffers[buf->index].status = FRAME_EMPTY;
1488
1489 return 0;
1490}
1491
1492/******************************************************************************
1493 *
1494 * find_earliest_filled_buffer
1495 *
1496 * Helper for ioctl_dqbuf. Find the next ready buffer.
1497 *
1498 *****************************************************************************/
1499
1500static int find_earliest_filled_buffer(struct camera_data *cam)
1501{
1502 int i;
1503 int found = -1;
1504 for (i=0; i<cam->num_frames; i++) {
1505 if(cam->buffers[i].status == FRAME_READY) {
1506 if(found < 0) {
1507 found = i;
1508 } else {
1509 /* find which buffer is earlier */
1510 struct timeval *tv1, *tv2;
1511 tv1 = &cam->buffers[i].timestamp;
1512 tv2 = &cam->buffers[found].timestamp;
1513 if(tv1->tv_sec < tv2->tv_sec ||
1514 (tv1->tv_sec == tv2->tv_sec &&
1515 tv1->tv_usec < tv2->tv_usec))
1516 found = i;
1517 }
1518 }
1519 }
1520 return found;
1521}
1522
1523/******************************************************************************
1524 *
1525 * ioctl_dqbuf
1526 *
1527 * V4L2 User is asking for a filled buffer.
1528 *
1529 *****************************************************************************/
1530
1531static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1532{
1533 struct v4l2_buffer *buf = arg;
1534 int frame;
1535
1536 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1537 buf->memory != V4L2_MEMORY_MMAP)
1538 return -EINVAL;
1539
1540 frame = find_earliest_filled_buffer(cam);
1541
1542 if(frame < 0 && file->f_flags&O_NONBLOCK)
1543 return -EAGAIN;
1544
1545 if(frame < 0) {
1546 /* Wait for a frame to become available */
1547 struct framebuf *cb=cam->curbuff;
2ae15191 1548 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1549 wait_event_interruptible(cam->wq_stream,
1550 !cam->present ||
1551 (cb=cam->curbuff)->status == FRAME_READY);
2ae15191 1552 mutex_lock(&cam->busy_lock);
ab33d507
AC
1553 if (signal_pending(current))
1554 return -ERESTARTSYS;
1555 if(!cam->present)
1556 return -ENOTTY;
1557 frame = cb->num;
1558 }
1559
1560
1561 buf->index = frame;
1562 buf->bytesused = cam->buffers[buf->index].length;
1563 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1564 buf->field = V4L2_FIELD_NONE;
1565 buf->timestamp = cam->buffers[buf->index].timestamp;
1566 buf->sequence = cam->buffers[buf->index].seq;
1567 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1568 buf->length = cam->frame_size;
1569 buf->input = 0;
1570 buf->reserved = 0;
1571 memset(&buf->timecode, 0, sizeof(buf->timecode));
1572
1573 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1574 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1575
1576 return 0;
1577}
1578
1579/******************************************************************************
1580 *
1581 * cpia2_ioctl
1582 *
1583 *****************************************************************************/
1584static int cpia2_do_ioctl(struct inode *inode, struct file *file,
1585 unsigned int ioctl_nr, void *arg)
1586{
1587 struct video_device *dev = video_devdata(file);
1588 struct camera_data *cam = video_get_drvdata(dev);
1589 int retval = 0;
1590
1591 if (!cam)
1592 return -ENOTTY;
1593
1594 /* make this _really_ smp-safe */
2ae15191 1595 if (mutex_lock_interruptible(&cam->busy_lock))
ab33d507
AC
1596 return -ERESTARTSYS;
1597
1598 if (!cam->present) {
2ae15191 1599 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1600 return -ENODEV;
1601 }
1602
1603 /* Priority check */
1604 switch (ioctl_nr) {
1605 case VIDIOCSWIN:
1606 case VIDIOCMCAPTURE:
1607 case VIDIOC_S_FMT:
1608 {
1609 struct cpia2_fh *fh = file->private_data;
1610 retval = v4l2_prio_check(&cam->prio, &fh->prio);
1611 if(retval) {
2ae15191 1612 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1613 return retval;
1614 }
1615 break;
1616 }
1617 case VIDIOCGMBUF:
1618 case VIDIOCSYNC:
1619 {
1620 struct cpia2_fh *fh = file->private_data;
1621 if(fh->prio != V4L2_PRIORITY_RECORD) {
2ae15191 1622 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1623 return -EBUSY;
1624 }
1625 break;
1626 }
1627 default:
1628 break;
1629 }
1630
1631 switch (ioctl_nr) {
be787ace 1632 case VIDIOCGCAP: /* query capabilities */
ab33d507
AC
1633 retval = ioctl_cap_query(arg, cam);
1634 break;
1635
1636 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
1637 retval = ioctl_get_channel(arg);
1638 break;
1639 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
1640 retval = ioctl_set_channel(arg);
1641 break;
1642 case VIDIOCGPICT: /* image properties */
1643 memcpy(arg, &cam->vp, sizeof(struct video_picture));
1644 break;
1645 case VIDIOCSPICT:
1646 retval = ioctl_set_image_prop(arg, cam);
1647 break;
1648 case VIDIOCGWIN: /* get/set capture window */
1649 memcpy(arg, &cam->vw, sizeof(struct video_window));
1650 break;
1651 case VIDIOCSWIN:
1652 retval = ioctl_set_window_size(arg, cam, file->private_data);
1653 break;
1654 case VIDIOCGMBUF: /* mmap interface */
1655 retval = ioctl_get_mbuf(arg, cam);
1656 break;
1657 case VIDIOCMCAPTURE:
1658 retval = ioctl_mcapture(arg, cam, file->private_data);
1659 break;
1660 case VIDIOCSYNC:
1661 retval = ioctl_sync(arg, cam);
1662 break;
1663 /* pointless to implement overlay with this camera */
1664 case VIDIOCCAPTURE:
1665 case VIDIOCGFBUF:
1666 case VIDIOCSFBUF:
1667 case VIDIOCKEY:
1668 retval = -EINVAL;
1669 break;
1670
1671 /* tuner interface - we have none */
1672 case VIDIOCGTUNER:
1673 case VIDIOCSTUNER:
1674 case VIDIOCGFREQ:
1675 case VIDIOCSFREQ:
1676 retval = -EINVAL;
1677 break;
1678
1679 /* audio interface - we have none */
1680 case VIDIOCGAUDIO:
1681 case VIDIOCSAUDIO:
1682 retval = -EINVAL;
1683 break;
1684
1685 /* CPIA2 extension to Video4Linux API */
1686 case CPIA2_IOC_SET_GPIO:
1687 retval = ioctl_set_gpio(arg, cam);
1688 break;
1689 case VIDIOC_QUERYCAP:
1690 retval = ioctl_querycap(arg,cam);
1691 break;
1692
1693 case VIDIOC_ENUMINPUT:
1694 case VIDIOC_G_INPUT:
1695 case VIDIOC_S_INPUT:
1696 retval = ioctl_input(ioctl_nr, arg,cam);
1697 break;
1698
1699 case VIDIOC_ENUM_FMT:
1700 retval = ioctl_enum_fmt(arg,cam);
1701 break;
1702 case VIDIOC_TRY_FMT:
1703 retval = ioctl_try_fmt(arg,cam);
1704 break;
1705 case VIDIOC_G_FMT:
1706 retval = ioctl_get_fmt(arg,cam);
1707 break;
1708 case VIDIOC_S_FMT:
1709 retval = ioctl_set_fmt(arg,cam,file->private_data);
1710 break;
1711
1712 case VIDIOC_CROPCAP:
1713 retval = ioctl_cropcap(arg,cam);
1714 break;
1715 case VIDIOC_G_CROP:
1716 case VIDIOC_S_CROP:
1717 // TODO: I think cropping can be implemented - SJB
1718 retval = -EINVAL;
1719 break;
1720
1721 case VIDIOC_QUERYCTRL:
1722 retval = ioctl_queryctrl(arg,cam);
1723 break;
1724 case VIDIOC_QUERYMENU:
1725 retval = ioctl_querymenu(arg,cam);
1726 break;
1727 case VIDIOC_G_CTRL:
1728 retval = ioctl_g_ctrl(arg,cam);
1729 break;
1730 case VIDIOC_S_CTRL:
1731 retval = ioctl_s_ctrl(arg,cam);
1732 break;
1733
1734 case VIDIOC_G_JPEGCOMP:
1735 retval = ioctl_g_jpegcomp(arg,cam);
1736 break;
1737 case VIDIOC_S_JPEGCOMP:
1738 retval = ioctl_s_jpegcomp(arg,cam);
1739 break;
1740
1741 case VIDIOC_G_PRIORITY:
1742 {
1743 struct cpia2_fh *fh = file->private_data;
1744 *(enum v4l2_priority*)arg = fh->prio;
1745 break;
1746 }
1747 case VIDIOC_S_PRIORITY:
1748 {
1749 struct cpia2_fh *fh = file->private_data;
1750 enum v4l2_priority prio;
1751 prio = *(enum v4l2_priority*)arg;
1752 if(cam->streaming &&
1753 prio != fh->prio &&
1754 fh->prio == V4L2_PRIORITY_RECORD) {
1755 /* Can't drop record priority while streaming */
1756 retval = -EBUSY;
1757 } else if(prio == V4L2_PRIORITY_RECORD &&
1758 prio != fh->prio &&
1759 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1760 /* Only one program can record at a time */
1761 retval = -EBUSY;
1762 } else {
1763 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1764 }
1765 break;
1766 }
1767
1768 case VIDIOC_REQBUFS:
1769 retval = ioctl_reqbufs(arg,cam);
1770 break;
1771 case VIDIOC_QUERYBUF:
1772 retval = ioctl_querybuf(arg,cam);
1773 break;
1774 case VIDIOC_QBUF:
1775 retval = ioctl_qbuf(arg,cam);
1776 break;
1777 case VIDIOC_DQBUF:
1778 retval = ioctl_dqbuf(arg,cam,file);
1779 break;
1780 case VIDIOC_STREAMON:
1781 {
1782 int type;
1783 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1784 type = *(int*)arg;
1785 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1786 retval = -EINVAL;
1787
1788 if(!cam->streaming) {
1789 retval = cpia2_usb_stream_start(cam,
1790 cam->params.camera_state.stream_mode);
1791 } else {
1792 retval = -EINVAL;
1793 }
1794
1795 break;
1796 }
1797 case VIDIOC_STREAMOFF:
1798 {
1799 int type;
1800 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1801 type = *(int*)arg;
1802 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1803 retval = -EINVAL;
1804
1805 if(cam->streaming) {
1806 retval = cpia2_usb_stream_stop(cam);
1807 } else {
1808 retval = -EINVAL;
1809 }
1810
1811 break;
1812 }
1813
1814 case VIDIOC_ENUMOUTPUT:
1815 case VIDIOC_G_OUTPUT:
1816 case VIDIOC_S_OUTPUT:
1817 case VIDIOC_G_MODULATOR:
1818 case VIDIOC_S_MODULATOR:
1819
1820 case VIDIOC_ENUMAUDIO:
1821 case VIDIOC_G_AUDIO:
1822 case VIDIOC_S_AUDIO:
1823
1824 case VIDIOC_ENUMAUDOUT:
1825 case VIDIOC_G_AUDOUT:
1826 case VIDIOC_S_AUDOUT:
1827
1828 case VIDIOC_ENUMSTD:
1829 case VIDIOC_QUERYSTD:
1830 case VIDIOC_G_STD:
1831 case VIDIOC_S_STD:
1832
1833 case VIDIOC_G_TUNER:
1834 case VIDIOC_S_TUNER:
1835 case VIDIOC_G_FREQUENCY:
1836 case VIDIOC_S_FREQUENCY:
1837
1838 case VIDIOC_OVERLAY:
1839 case VIDIOC_G_FBUF:
1840 case VIDIOC_S_FBUF:
1841
1842 case VIDIOC_G_PARM:
1843 case VIDIOC_S_PARM:
1844 retval = -EINVAL;
1845 break;
1846 default:
1847 retval = -ENOIOCTLCMD;
1848 break;
1849 }
1850
2ae15191 1851 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1852 return retval;
1853}
1854
1855static int cpia2_ioctl(struct inode *inode, struct file *file,
1856 unsigned int ioctl_nr, unsigned long iarg)
1857{
1858 return video_usercopy(inode, file, ioctl_nr, iarg, cpia2_do_ioctl);
1859}
1860
1861/******************************************************************************
1862 *
1863 * cpia2_mmap
1864 *
1865 *****************************************************************************/
1866static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1867{
1868 int retval;
1869 struct video_device *dev = video_devdata(file);
1870 struct camera_data *cam = video_get_drvdata(dev);
1871
1872 /* Priority check */
1873 struct cpia2_fh *fh = file->private_data;
1874 if(fh->prio != V4L2_PRIORITY_RECORD) {
1875 return -EBUSY;
1876 }
1877
1878 retval = cpia2_remap_buffer(cam, area);
1879
1880 if(!retval)
1881 fh->mmapped = 1;
1882 return retval;
1883}
1884
1885/******************************************************************************
1886 *
1887 * reset_camera_struct_v4l
1888 *
1889 * Sets all values to the defaults
1890 *****************************************************************************/
1891static void reset_camera_struct_v4l(struct camera_data *cam)
1892{
1893 /***
1894 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1895 * Ioctl. Here, just do the window and picture stucts.
1896 ***/
1897 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
1898 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
1899 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
1900 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
1901
1902 cam->vw.x = 0;
1903 cam->vw.y = 0;
1904 cam->vw.width = cam->params.roi.width;
1905 cam->vw.height = cam->params.roi.height;
1906 cam->vw.flags = 0;
1907 cam->vw.clipcount = 0;
1908
1909 cam->frame_size = buffer_size;
1910 cam->num_frames = num_buffers;
1911
1912 /* FlickerModes */
1913 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1914 cam->params.flicker_control.mains_frequency = flicker_freq;
1915
1916 /* streamMode */
1917 cam->params.camera_state.stream_mode = alternate;
1918
1919 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1920 v4l2_prio_init(&cam->prio);
1921 return;
1922}
1923
1924/***
1925 * The v4l video device structure initialized for this device
1926 ***/
fa027c2a 1927static const struct file_operations fops_template = {
2ae15191
AC
1928 .owner = THIS_MODULE,
1929 .open = cpia2_open,
1930 .release = cpia2_close,
1931 .read = cpia2_v4l_read,
1932 .poll = cpia2_v4l_poll,
1933 .ioctl = cpia2_ioctl,
1934 .llseek = no_llseek,
1935 .compat_ioctl = v4l_compat_ioctl32,
1936 .mmap = cpia2_mmap,
ab33d507
AC
1937};
1938
1939static struct video_device cpia2_template = {
1940 /* I could not find any place for the old .initialize initializer?? */
1941 .owner= THIS_MODULE,
1942 .name= "CPiA2 Camera",
1943 .type= VID_TYPE_CAPTURE,
1944 .type2 = V4L2_CAP_VIDEO_CAPTURE |
1945 V4L2_CAP_STREAMING,
1946 .hardware= VID_HARDWARE_CPIA2,
1947 .minor= -1,
1948 .fops= &fops_template,
1949 .release= video_device_release,
1950};
1951
1952/******************************************************************************
1953 *
1954 * cpia2_register_camera
1955 *
1956 *****************************************************************************/
1957int cpia2_register_camera(struct camera_data *cam)
1958{
1959 cam->vdev = video_device_alloc();
1960 if(!cam->vdev)
1961 return -ENOMEM;
1962
1963 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1964 video_set_drvdata(cam->vdev, cam);
1965
1966 reset_camera_struct_v4l(cam);
1967
1968 /* register v4l device */
1969 if (video_register_device
1970 (cam->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1971 ERR("video_register_device failed\n");
1972 video_device_release(cam->vdev);
1973 return -ENODEV;
1974 }
1975
1976 return 0;
1977}
1978
1979/******************************************************************************
1980 *
1981 * cpia2_unregister_camera
1982 *
1983 *****************************************************************************/
1984void cpia2_unregister_camera(struct camera_data *cam)
1985{
1986 if (!cam->open_count) {
1987 video_unregister_device(cam->vdev);
1988 } else {
1989 LOG("/dev/video%d removed while open, "
1990 "deferring video_unregister_device\n",
1991 cam->vdev->minor);
1992 }
1993}
1994
1995/******************************************************************************
1996 *
1997 * check_parameters
1998 *
1999 * Make sure that all user-supplied parameters are sensible
2000 *****************************************************************************/
2001static void __init check_parameters(void)
2002{
2003 if(buffer_size < PAGE_SIZE) {
2004 buffer_size = PAGE_SIZE;
2005 LOG("buffer_size too small, setting to %d\n", buffer_size);
2006 } else if(buffer_size > 1024*1024) {
2007 /* arbitrary upper limiit */
2008 buffer_size = 1024*1024;
2009 LOG("buffer_size ridiculously large, setting to %d\n",
2010 buffer_size);
2011 } else {
2012 buffer_size += PAGE_SIZE-1;
2013 buffer_size &= ~(PAGE_SIZE-1);
2014 }
2015
2016 if(num_buffers < 1) {
2017 num_buffers = 1;
2018 LOG("num_buffers too small, setting to %d\n", num_buffers);
2019 } else if(num_buffers > VIDEO_MAX_FRAME) {
2020 num_buffers = VIDEO_MAX_FRAME;
2021 LOG("num_buffers too large, setting to %d\n", num_buffers);
2022 }
2023
2024 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
2025 alternate = DEFAULT_ALT;
2026 LOG("alternate specified is invalid, using %d\n", alternate);
2027 }
2028
2029 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
2030 flicker_mode = NEVER_FLICKER;
2031 LOG("Flicker mode specified is invalid, using %d\n",
2032 flicker_mode);
2033 }
2034
2035 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
2036 flicker_freq = FLICKER_60;
2037 LOG("Flicker mode specified is invalid, using %d\n",
2038 flicker_freq);
2039 }
2040
2041 if(video_nr < -1 || video_nr > 64) {
2042 video_nr = -1;
2043 LOG("invalid video_nr specified, must be -1 to 64\n");
2044 }
2045
2046 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2047 num_buffers, buffer_size, alternate);
2048}
2049
2050/************ Module Stuff ***************/
2051
2052
2053/******************************************************************************
2054 *
2055 * cpia2_init/module_init
2056 *
2057 *****************************************************************************/
8cbe84f3 2058static int __init cpia2_init(void)
ab33d507
AC
2059{
2060 LOG("%s v%d.%d.%d\n",
2061 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
2062 check_parameters();
2063 cpia2_usb_init();
2064 return 0;
2065}
2066
2067
2068/******************************************************************************
2069 *
2070 * cpia2_exit/module_exit
2071 *
2072 *****************************************************************************/
8cbe84f3 2073static void __exit cpia2_exit(void)
ab33d507
AC
2074{
2075 cpia2_usb_cleanup();
2076 schedule_timeout(2 * HZ);
2077}
2078
ab33d507
AC
2079module_init(cpia2_init);
2080module_exit(cpia2_exit);
2081