]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/cpia2/cpia2_v4l.c
[media] saa7146: Convert from .ioctl to .unlocked_ioctl
[mirror_ubuntu-artful-kernel.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
d9b01449 29 * Alan Cox <alan@lxorguk.ukuu.org.uk>
ab33d507
AC
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>
873ecd8f 40#include <linux/videodev2.h>
cb63c2aa 41#include <linux/stringify.h>
35ea11ff 42#include <media/v4l2-ioctl.h>
ab33d507
AC
43
44#include "cpia2.h"
45#include "cpia2dev.h"
46
ab33d507
AC
47static int video_nr = -1;
48module_param(video_nr, int, 0);
49MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
50
51static int buffer_size = 68*1024;
52module_param(buffer_size, int, 0);
53MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
54
55static int num_buffers = 3;
56module_param(num_buffers, int, 0);
57MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
cb63c2aa 58 __stringify(VIDEO_MAX_FRAME) ", default 3)");
ab33d507
AC
59
60static int alternate = DEFAULT_ALT;
61module_param(alternate, int, 0);
cb63c2aa
TF
62MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
63 __stringify(USBIF_ISO_6) ", default "
64 __stringify(DEFAULT_ALT) ")");
ab33d507
AC
65
66static int flicker_freq = 60;
67module_param(flicker_freq, int, 0);
cb63c2aa
TF
68MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
69 __stringify(60) ", default "
70 __stringify(60) ")");
ab33d507
AC
71
72static int flicker_mode = NEVER_FLICKER;
73module_param(flicker_mode, int, 0);
74MODULE_PARM_DESC(flicker_mode,
cb63c2aa
TF
75 "Flicker supression (" __stringify(NEVER_FLICKER) "or"
76 __stringify(ANTI_FLICKER_ON) ", default "
77 __stringify(NEVER_FLICKER) ")");
ab33d507
AC
78
79MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
80MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
81MODULE_SUPPORTED_DEVICE("video");
82MODULE_LICENSE("GPL");
83
84#define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
85
ab33d507
AC
86struct control_menu_info {
87 int value;
88 char name[32];
89};
90
91static 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};
0c71bf1c 100#define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
ab33d507
AC
101
102static struct control_menu_info flicker_controls[] =
103{
104 { NEVER_FLICKER, "Off" },
105 { FLICKER_50, "50 Hz" },
106 { FLICKER_60, "60 Hz" },
107};
0c71bf1c 108#define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
ab33d507
AC
109
110static struct control_menu_info lights_controls[] =
111{
112 { 0, "Off" },
113 { 64, "Top" },
114 { 128, "Bottom" },
115 { 192, "Both" },
116};
0c71bf1c 117#define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
ab33d507
AC
118#define GPIO_LIGHTS_MASK 192
119
120static 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};
0c71bf1c 230#define NUM_CONTROLS (ARRAY_SIZE(controls))
ab33d507
AC
231
232
233/******************************************************************************
234 *
235 * cpia2_open
236 *
237 *****************************************************************************/
bec43661 238static int cpia2_open(struct file *file)
ab33d507 239{
c170ecf4 240 struct camera_data *cam = video_drvdata(file);
ab33d507
AC
241 int retval = 0;
242
243 if (!cam) {
244 ERR("Internal error, camera_data not found!\n");
245 return -ENODEV;
246 }
247
2ae15191 248 if(mutex_lock_interruptible(&cam->busy_lock))
ab33d507
AC
249 return -ERESTARTSYS;
250
251 if(!cam->present) {
252 retval = -ENODEV;
253 goto err_return;
254 }
255
256 if (cam->open_count > 0) {
257 goto skip_init;
258 }
259
260 if (cpia2_allocate_buffers(cam)) {
261 retval = -ENOMEM;
262 goto err_return;
263 }
264
265 /* reset the camera */
266 if (cpia2_reset_camera(cam) < 0) {
267 retval = -EIO;
268 goto err_return;
269 }
270
271 cam->APP_len = 0;
272 cam->COM_len = 0;
273
274skip_init:
275 {
276 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
277 if(!fh) {
278 retval = -ENOMEM;
279 goto err_return;
280 }
281 file->private_data = fh;
282 fh->prio = V4L2_PRIORITY_UNSET;
283 v4l2_prio_open(&cam->prio, &fh->prio);
284 fh->mmapped = 0;
285 }
286
287 ++cam->open_count;
288
289 cpia2_dbg_dump_registers(cam);
290
291err_return:
2ae15191 292 mutex_unlock(&cam->busy_lock);
ab33d507
AC
293 return retval;
294}
295
296/******************************************************************************
297 *
298 * cpia2_close
299 *
300 *****************************************************************************/
bec43661 301static int cpia2_close(struct file *file)
ab33d507
AC
302{
303 struct video_device *dev = video_devdata(file);
304 struct camera_data *cam = video_get_drvdata(dev);
305 struct cpia2_fh *fh = file->private_data;
306
2ae15191 307 mutex_lock(&cam->busy_lock);
ab33d507
AC
308
309 if (cam->present &&
310 (cam->open_count == 1
311 || fh->prio == V4L2_PRIORITY_RECORD
312 )) {
313 cpia2_usb_stream_stop(cam);
314
315 if(cam->open_count == 1) {
316 /* save camera state for later open */
317 cpia2_save_camera_state(cam);
318
319 cpia2_set_low_power(cam);
320 cpia2_free_buffers(cam);
321 }
322 }
323
324 {
325 if(fh->mmapped)
326 cam->mmapped = 0;
ffb4877b 327 v4l2_prio_close(&cam->prio, fh->prio);
ab33d507
AC
328 file->private_data = NULL;
329 kfree(fh);
330 }
331
332 if (--cam->open_count == 0) {
333 cpia2_free_buffers(cam);
334 if (!cam->present) {
335 video_unregister_device(dev);
bafefc0c 336 mutex_unlock(&cam->busy_lock);
ab33d507 337 kfree(cam);
bafefc0c 338 return 0;
ab33d507
AC
339 }
340 }
341
2ae15191 342 mutex_unlock(&cam->busy_lock);
ab33d507
AC
343
344 return 0;
345}
346
347/******************************************************************************
348 *
349 * cpia2_v4l_read
350 *
351 *****************************************************************************/
352static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
353 loff_t *off)
354{
c170ecf4 355 struct camera_data *cam = video_drvdata(file);
ab33d507
AC
356 int noblock = file->f_flags&O_NONBLOCK;
357
358 struct cpia2_fh *fh = file->private_data;
359
360 if(!cam)
361 return -EINVAL;
362
363 /* Priority check */
364 if(fh->prio != V4L2_PRIORITY_RECORD) {
365 return -EBUSY;
366 }
367
368 return cpia2_read(cam, buf, count, noblock);
369}
370
371
372/******************************************************************************
373 *
374 * cpia2_v4l_poll
375 *
376 *****************************************************************************/
377static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
378{
c170ecf4 379 struct camera_data *cam = video_drvdata(filp);
ab33d507
AC
380 struct cpia2_fh *fh = filp->private_data;
381
382 if(!cam)
383 return POLLERR;
384
385 /* Priority check */
386 if(fh->prio != V4L2_PRIORITY_RECORD) {
387 return POLLERR;
388 }
389
390 return cpia2_poll(cam, filp, wait);
391}
392
393
ab33d507
AC
394static int sync(struct camera_data *cam, int frame_nr)
395{
396 struct framebuf *frame = &cam->buffers[frame_nr];
397
398 while (1) {
399 if (frame->status == FRAME_READY)
400 return 0;
401
402 if (!cam->streaming) {
403 frame->status = FRAME_READY;
404 frame->length = 0;
405 return 0;
406 }
407
2ae15191 408 mutex_unlock(&cam->busy_lock);
ab33d507
AC
409 wait_event_interruptible(cam->wq_stream,
410 !cam->streaming ||
411 frame->status == FRAME_READY);
2ae15191 412 mutex_lock(&cam->busy_lock);
ab33d507
AC
413 if (signal_pending(current))
414 return -ERESTARTSYS;
415 if(!cam->present)
416 return -ENOTTY;
417 }
418}
419
ab33d507
AC
420/******************************************************************************
421 *
422 * ioctl_set_gpio
423 *
424 *****************************************************************************/
425
426static int ioctl_set_gpio(void *arg, struct camera_data *cam)
427{
428 __u32 gpio_val;
429
430 gpio_val = *(__u32*) arg;
431
432 if (gpio_val &~ 0xFFU)
433 return -EINVAL;
434
435 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
436}
437
438/******************************************************************************
439 *
440 * ioctl_querycap
441 *
442 * V4L2 device capabilities
443 *
444 *****************************************************************************/
445
446static int ioctl_querycap(void *arg, struct camera_data *cam)
447{
448 struct v4l2_capability *vc = arg;
449
450 memset(vc, 0, sizeof(*vc));
451 strcpy(vc->driver, "cpia2");
452
453 if (cam->params.pnp_id.product == 0x151)
454 strcpy(vc->card, "QX5 Microscope");
455 else
456 strcpy(vc->card, "CPiA2 Camera");
457 switch (cam->params.pnp_id.device_type) {
458 case DEVICE_STV_672:
459 strcat(vc->card, " (672/");
460 break;
461 case DEVICE_STV_676:
462 strcat(vc->card, " (676/");
463 break;
464 default:
465 strcat(vc->card, " (???/");
466 break;
467 }
468 switch (cam->params.version.sensor_flags) {
469 case CPIA2_VP_SENSOR_FLAGS_404:
470 strcat(vc->card, "404)");
471 break;
472 case CPIA2_VP_SENSOR_FLAGS_407:
473 strcat(vc->card, "407)");
474 break;
475 case CPIA2_VP_SENSOR_FLAGS_409:
476 strcat(vc->card, "409)");
477 break;
478 case CPIA2_VP_SENSOR_FLAGS_410:
479 strcat(vc->card, "410)");
480 break;
481 case CPIA2_VP_SENSOR_FLAGS_500:
482 strcat(vc->card, "500)");
483 break;
484 default:
485 strcat(vc->card, "???)");
486 break;
487 }
488
489 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
490 memset(vc->bus_info,0, sizeof(vc->bus_info));
491
492 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
493 CPIA2_PATCH_VER);
494
495 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
496 V4L2_CAP_READWRITE |
497 V4L2_CAP_STREAMING;
498
499 return 0;
500}
501
502/******************************************************************************
503 *
504 * ioctl_input
505 *
506 * V4L2 input get/set/enumerate
507 *
508 *****************************************************************************/
509
510static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
511{
512 struct v4l2_input *i = arg;
513
514 if(ioclt_nr != VIDIOC_G_INPUT) {
515 if (i->index != 0)
516 return -EINVAL;
517 }
518
519 memset(i, 0, sizeof(*i));
520 strcpy(i->name, "Camera");
521 i->type = V4L2_INPUT_TYPE_CAMERA;
522
523 return 0;
524}
525
526/******************************************************************************
527 *
528 * ioctl_enum_fmt
529 *
530 * V4L2 format enumerate
531 *
532 *****************************************************************************/
533
534static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
535{
536 struct v4l2_fmtdesc *f = arg;
537 int index = f->index;
538
539 if (index < 0 || index > 1)
540 return -EINVAL;
541
542 memset(f, 0, sizeof(*f));
543 f->index = index;
544 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
545 f->flags = V4L2_FMT_FLAG_COMPRESSED;
546 switch(index) {
547 case 0:
548 strcpy(f->description, "MJPEG");
549 f->pixelformat = V4L2_PIX_FMT_MJPEG;
550 break;
551 case 1:
552 strcpy(f->description, "JPEG");
553 f->pixelformat = V4L2_PIX_FMT_JPEG;
554 break;
555 default:
556 return -EINVAL;
557 }
558
559 return 0;
560}
561
562/******************************************************************************
563 *
564 * ioctl_try_fmt
565 *
566 * V4L2 format try
567 *
568 *****************************************************************************/
569
570static int ioctl_try_fmt(void *arg,struct camera_data *cam)
571{
572 struct v4l2_format *f = arg;
573
574 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
575 return -EINVAL;
576
577 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
578 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
579 return -EINVAL;
580
581 f->fmt.pix.field = V4L2_FIELD_NONE;
582 f->fmt.pix.bytesperline = 0;
583 f->fmt.pix.sizeimage = cam->frame_size;
584 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
585 f->fmt.pix.priv = 0;
586
587 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
588 case VIDEOSIZE_VGA:
589 f->fmt.pix.width = 640;
590 f->fmt.pix.height = 480;
591 break;
592 case VIDEOSIZE_CIF:
593 f->fmt.pix.width = 352;
594 f->fmt.pix.height = 288;
595 break;
596 case VIDEOSIZE_QVGA:
597 f->fmt.pix.width = 320;
598 f->fmt.pix.height = 240;
599 break;
600 case VIDEOSIZE_288_216:
601 f->fmt.pix.width = 288;
602 f->fmt.pix.height = 216;
603 break;
604 case VIDEOSIZE_256_192:
605 f->fmt.pix.width = 256;
606 f->fmt.pix.height = 192;
607 break;
608 case VIDEOSIZE_224_168:
609 f->fmt.pix.width = 224;
610 f->fmt.pix.height = 168;
611 break;
612 case VIDEOSIZE_192_144:
613 f->fmt.pix.width = 192;
614 f->fmt.pix.height = 144;
615 break;
616 case VIDEOSIZE_QCIF:
617 default:
618 f->fmt.pix.width = 176;
619 f->fmt.pix.height = 144;
620 break;
621 }
622
623 return 0;
624}
625
626/******************************************************************************
627 *
628 * ioctl_set_fmt
629 *
630 * V4L2 format set
631 *
632 *****************************************************************************/
633
634static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
635{
636 struct v4l2_format *f = arg;
637 int err, frame;
638
639 err = ioctl_try_fmt(arg, cam);
640 if(err != 0)
641 return err;
642
643 /* Ensure that only this process can change the format. */
644 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
645 if(err != 0) {
646 return err;
647 }
648
649 cam->pixelformat = f->fmt.pix.pixelformat;
650
651 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
652 * the missing Huffman table properly. */
653 cam->params.compression.inhibit_htables = 0;
654 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
655
656 /* we set the video window to something smaller or equal to what
657 * is requested by the user???
658 */
659 DBG("Requested width = %d, height = %d\n",
660 f->fmt.pix.width, f->fmt.pix.height);
873ecd8f
HV
661 if (f->fmt.pix.width != cam->width ||
662 f->fmt.pix.height != cam->height) {
663 cam->width = f->fmt.pix.width;
664 cam->height = f->fmt.pix.height;
ab33d507
AC
665 cam->params.roi.width = f->fmt.pix.width;
666 cam->params.roi.height = f->fmt.pix.height;
667 cpia2_set_format(cam);
668 }
669
670 for (frame = 0; frame < cam->num_frames; ++frame) {
671 if (cam->buffers[frame].status == FRAME_READING)
672 if ((err = sync(cam, frame)) < 0)
673 return err;
674
675 cam->buffers[frame].status = FRAME_EMPTY;
676 }
677
678 return 0;
679}
680
681/******************************************************************************
682 *
683 * ioctl_get_fmt
684 *
685 * V4L2 format get
686 *
687 *****************************************************************************/
688
689static int ioctl_get_fmt(void *arg,struct camera_data *cam)
690{
691 struct v4l2_format *f = arg;
692
693 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 return -EINVAL;
695
873ecd8f
HV
696 f->fmt.pix.width = cam->width;
697 f->fmt.pix.height = cam->height;
ab33d507
AC
698 f->fmt.pix.pixelformat = cam->pixelformat;
699 f->fmt.pix.field = V4L2_FIELD_NONE;
700 f->fmt.pix.bytesperline = 0;
701 f->fmt.pix.sizeimage = cam->frame_size;
702 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
703 f->fmt.pix.priv = 0;
704
705 return 0;
706}
707
708/******************************************************************************
709 *
710 * ioctl_cropcap
711 *
712 * V4L2 query cropping capabilities
713 * NOTE: cropping is currently disabled
714 *
715 *****************************************************************************/
716
717static int ioctl_cropcap(void *arg,struct camera_data *cam)
718{
719 struct v4l2_cropcap *c = arg;
720
721 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
722 return -EINVAL;
723
724 c->bounds.left = 0;
725 c->bounds.top = 0;
873ecd8f
HV
726 c->bounds.width = cam->width;
727 c->bounds.height = cam->height;
ab33d507
AC
728 c->defrect.left = 0;
729 c->defrect.top = 0;
873ecd8f
HV
730 c->defrect.width = cam->width;
731 c->defrect.height = cam->height;
ab33d507
AC
732 c->pixelaspect.numerator = 1;
733 c->pixelaspect.denominator = 1;
734
735 return 0;
736}
737
738/******************************************************************************
739 *
740 * ioctl_queryctrl
741 *
742 * V4L2 query possible control variables
743 *
744 *****************************************************************************/
745
746static int ioctl_queryctrl(void *arg,struct camera_data *cam)
747{
748 struct v4l2_queryctrl *c = arg;
749 int i;
750
751 for(i=0; i<NUM_CONTROLS; ++i) {
752 if(c->id == controls[i].id) {
753 memcpy(c, controls+i, sizeof(*c));
754 break;
755 }
756 }
757
758 if(i == NUM_CONTROLS)
759 return -EINVAL;
760
761 /* Some devices have additional limitations */
762 switch(c->id) {
763 case V4L2_CID_BRIGHTNESS:
764 /***
765 * Don't let the register be set to zero - bug in VP4
766 * flash of full brightness
767 ***/
768 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
769 c->minimum = 1;
770 break;
771 case V4L2_CID_VFLIP:
772 // VP5 Only
773 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
774 c->flags |= V4L2_CTRL_FLAG_DISABLED;
775 break;
776 case CPIA2_CID_FRAMERATE:
777 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
778 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
779 // Maximum 15fps
ab33d507
AC
780 for(i=0; i<c->maximum; ++i) {
781 if(framerate_controls[i].value ==
782 CPIA2_VP_FRAMERATE_15) {
783 c->maximum = i;
784 c->default_value = i;
785 }
786 }
787 }
788 break;
789 case CPIA2_CID_FLICKER_MODE:
790 // Flicker control only valid for 672.
791 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
792 c->flags |= V4L2_CTRL_FLAG_DISABLED;
793 break;
794 case CPIA2_CID_LIGHTS:
795 // Light control only valid for the QX5 Microscope.
796 if(cam->params.pnp_id.product != 0x151)
797 c->flags |= V4L2_CTRL_FLAG_DISABLED;
798 break;
799 default:
800 break;
801 }
802
803 return 0;
804}
805
806/******************************************************************************
807 *
808 * ioctl_querymenu
809 *
810 * V4L2 query possible control variables
811 *
812 *****************************************************************************/
813
814static int ioctl_querymenu(void *arg,struct camera_data *cam)
815{
816 struct v4l2_querymenu *m = arg;
817
818 memset(m->name, 0, sizeof(m->name));
819 m->reserved = 0;
820
821 switch(m->id) {
822 case CPIA2_CID_FLICKER_MODE:
223ffe5f 823 if (m->index >= NUM_FLICKER_CONTROLS)
ab33d507
AC
824 return -EINVAL;
825
826 strcpy(m->name, flicker_controls[m->index].name);
827 break;
828 case CPIA2_CID_FRAMERATE:
829 {
830 int maximum = NUM_FRAMERATE_CONTROLS - 1;
831 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
832 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
833 // Maximum 15fps
834 int i;
835 for(i=0; i<maximum; ++i) {
836 if(framerate_controls[i].value ==
837 CPIA2_VP_FRAMERATE_15)
838 maximum = i;
839 }
840 }
223ffe5f 841 if (m->index > maximum)
ab33d507
AC
842 return -EINVAL;
843
844 strcpy(m->name, framerate_controls[m->index].name);
845 break;
846 }
847 case CPIA2_CID_LIGHTS:
223ffe5f 848 if (m->index >= NUM_LIGHTS_CONTROLS)
ab33d507
AC
849 return -EINVAL;
850
851 strcpy(m->name, lights_controls[m->index].name);
852 break;
853 default:
854 return -EINVAL;
855 }
856
857 return 0;
858}
859
860/******************************************************************************
861 *
862 * ioctl_g_ctrl
863 *
864 * V4L2 get the value of a control variable
865 *
866 *****************************************************************************/
867
868static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
869{
870 struct v4l2_control *c = arg;
871
872 switch(c->id) {
873 case V4L2_CID_BRIGHTNESS:
874 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
875 TRANSFER_READ, 0);
876 c->value = cam->params.color_params.brightness;
877 break;
878 case V4L2_CID_CONTRAST:
879 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
880 TRANSFER_READ, 0);
881 c->value = cam->params.color_params.contrast;
882 break;
883 case V4L2_CID_SATURATION:
884 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
885 TRANSFER_READ, 0);
886 c->value = cam->params.color_params.saturation;
887 break;
888 case V4L2_CID_HFLIP:
889 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
890 TRANSFER_READ, 0);
891 c->value = (cam->params.vp_params.user_effects &
892 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
893 break;
894 case V4L2_CID_VFLIP:
895 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
896 TRANSFER_READ, 0);
897 c->value = (cam->params.vp_params.user_effects &
898 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
899 break;
900 case CPIA2_CID_TARGET_KB:
901 c->value = cam->params.vc_params.target_kb;
902 break;
903 case CPIA2_CID_GPIO:
904 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
905 TRANSFER_READ, 0);
906 c->value = cam->params.vp_params.gpio_data;
907 break;
908 case CPIA2_CID_FLICKER_MODE:
909 {
910 int i, mode;
911 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
912 TRANSFER_READ, 0);
913 if(cam->params.flicker_control.cam_register &
914 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
915 mode = NEVER_FLICKER;
916 } else {
917 if(cam->params.flicker_control.cam_register &
918 CPIA2_VP_FLICKER_MODES_50HZ) {
657de3cd 919 mode = FLICKER_50;
ab33d507 920 } else {
657de3cd 921 mode = FLICKER_60;
ab33d507
AC
922 }
923 }
924 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
925 if(flicker_controls[i].value == mode) {
926 c->value = i;
927 break;
928 }
929 }
930 if(i == NUM_FLICKER_CONTROLS)
931 return -EINVAL;
932 break;
933 }
934 case CPIA2_CID_FRAMERATE:
935 {
936 int maximum = NUM_FRAMERATE_CONTROLS - 1;
937 int i;
938 for(i=0; i<= maximum; i++) {
939 if(cam->params.vp_params.frame_rate ==
940 framerate_controls[i].value)
941 break;
942 }
943 if(i > maximum)
944 return -EINVAL;
945 c->value = i;
946 break;
947 }
948 case CPIA2_CID_USB_ALT:
949 c->value = cam->params.camera_state.stream_mode;
950 break;
951 case CPIA2_CID_LIGHTS:
952 {
953 int i;
954 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
955 TRANSFER_READ, 0);
956 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
957 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
958 lights_controls[i].value) {
959 break;
960 }
961 }
962 if(i == NUM_LIGHTS_CONTROLS)
963 return -EINVAL;
964 c->value = i;
965 break;
966 }
967 case CPIA2_CID_RESET_CAMERA:
968 return -EINVAL;
969 default:
970 return -EINVAL;
971 }
972
973 DBG("Get control id:%d, value:%d\n", c->id, c->value);
974
975 return 0;
976}
977
978/******************************************************************************
979 *
980 * ioctl_s_ctrl
981 *
982 * V4L2 set the value of a control variable
983 *
984 *****************************************************************************/
985
986static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
987{
988 struct v4l2_control *c = arg;
989 int i;
990 int retval = 0;
991
992 DBG("Set control id:%d, value:%d\n", c->id, c->value);
993
994 /* Check that the value is in range */
995 for(i=0; i<NUM_CONTROLS; i++) {
996 if(c->id == controls[i].id) {
997 if(c->value < controls[i].minimum ||
998 c->value > controls[i].maximum) {
999 return -EINVAL;
1000 }
1001 break;
1002 }
1003 }
1004 if(i == NUM_CONTROLS)
1005 return -EINVAL;
1006
1007 switch(c->id) {
1008 case V4L2_CID_BRIGHTNESS:
1009 cpia2_set_brightness(cam, c->value);
1010 break;
1011 case V4L2_CID_CONTRAST:
1012 cpia2_set_contrast(cam, c->value);
1013 break;
1014 case V4L2_CID_SATURATION:
1015 cpia2_set_saturation(cam, c->value);
1016 break;
1017 case V4L2_CID_HFLIP:
1018 cpia2_set_property_mirror(cam, c->value);
1019 break;
1020 case V4L2_CID_VFLIP:
1021 cpia2_set_property_flip(cam, c->value);
1022 break;
1023 case CPIA2_CID_TARGET_KB:
1024 retval = cpia2_set_target_kb(cam, c->value);
1025 break;
1026 case CPIA2_CID_GPIO:
1027 retval = cpia2_set_gpio(cam, c->value);
1028 break;
1029 case CPIA2_CID_FLICKER_MODE:
1030 retval = cpia2_set_flicker_mode(cam,
1031 flicker_controls[c->value].value);
1032 break;
1033 case CPIA2_CID_FRAMERATE:
1034 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1035 break;
1036 case CPIA2_CID_USB_ALT:
1037 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1038 break;
1039 case CPIA2_CID_LIGHTS:
1040 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1041 break;
1042 case CPIA2_CID_RESET_CAMERA:
1043 cpia2_usb_stream_pause(cam);
1044 cpia2_reset_camera(cam);
1045 cpia2_usb_stream_resume(cam);
1046 break;
1047 default:
1048 retval = -EINVAL;
1049 }
1050
1051 return retval;
1052}
1053
1054/******************************************************************************
1055 *
1056 * ioctl_g_jpegcomp
1057 *
1058 * V4L2 get the JPEG compression parameters
1059 *
1060 *****************************************************************************/
1061
1062static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1063{
1064 struct v4l2_jpegcompression *parms = arg;
1065
1066 memset(parms, 0, sizeof(*parms));
1067
1068 parms->quality = 80; // TODO: Can this be made meaningful?
1069
1070 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1071 if(!cam->params.compression.inhibit_htables) {
1072 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1073 }
1074
1075 parms->APPn = cam->APPn;
1076 parms->APP_len = cam->APP_len;
1077 if(cam->APP_len > 0) {
1078 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1079 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1080 }
1081
1082 parms->COM_len = cam->COM_len;
1083 if(cam->COM_len > 0) {
1084 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1085 parms->jpeg_markers |= JPEG_MARKER_COM;
1086 }
1087
1088 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1089 parms->APP_len, parms->COM_len);
1090
1091 return 0;
1092}
1093
1094/******************************************************************************
1095 *
1096 * ioctl_s_jpegcomp
1097 *
1098 * V4L2 set the JPEG compression parameters
1099 * NOTE: quality and some jpeg_markers are ignored.
1100 *
1101 *****************************************************************************/
1102
1103static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1104{
1105 struct v4l2_jpegcompression *parms = arg;
1106
1107 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1108 parms->APP_len, parms->COM_len);
1109
1110 cam->params.compression.inhibit_htables =
1111 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1112
1113 if(parms->APP_len != 0) {
1114 if(parms->APP_len > 0 &&
1115 parms->APP_len <= sizeof(cam->APP_data) &&
1116 parms->APPn >= 0 && parms->APPn <= 15) {
1117 cam->APPn = parms->APPn;
1118 cam->APP_len = parms->APP_len;
1119 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1120 } else {
1121 LOG("Bad APPn Params n=%d len=%d\n",
1122 parms->APPn, parms->APP_len);
1123 return -EINVAL;
1124 }
1125 } else {
1126 cam->APP_len = 0;
1127 }
1128
1129 if(parms->COM_len != 0) {
1130 if(parms->COM_len > 0 &&
1131 parms->COM_len <= sizeof(cam->COM_data)) {
1132 cam->COM_len = parms->COM_len;
1133 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1134 } else {
1135 LOG("Bad COM_len=%d\n", parms->COM_len);
1136 return -EINVAL;
1137 }
1138 }
1139
1140 return 0;
1141}
1142
1143/******************************************************************************
1144 *
1145 * ioctl_reqbufs
1146 *
1147 * V4L2 Initiate memory mapping.
1148 * NOTE: The user's request is ignored. For now the buffers are fixed.
1149 *
1150 *****************************************************************************/
1151
1152static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1153{
1154 struct v4l2_requestbuffers *req = arg;
1155
1156 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1157 req->memory != V4L2_MEMORY_MMAP)
1158 return -EINVAL;
1159
1160 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1161 req->count = cam->num_frames;
1162 memset(&req->reserved, 0, sizeof(req->reserved));
1163
1164 return 0;
1165}
1166
1167/******************************************************************************
1168 *
1169 * ioctl_querybuf
1170 *
1171 * V4L2 Query memory buffer status.
1172 *
1173 *****************************************************************************/
1174
1175static int ioctl_querybuf(void *arg,struct camera_data *cam)
1176{
1177 struct v4l2_buffer *buf = arg;
1178
1179 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1180 buf->index > cam->num_frames)
1181 return -EINVAL;
1182
1183 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1184 buf->length = cam->frame_size;
1185
1186 buf->memory = V4L2_MEMORY_MMAP;
1187
1188 if(cam->mmapped)
1189 buf->flags = V4L2_BUF_FLAG_MAPPED;
1190 else
1191 buf->flags = 0;
1192
1193 switch (cam->buffers[buf->index].status) {
1194 case FRAME_EMPTY:
1195 case FRAME_ERROR:
1196 case FRAME_READING:
1197 buf->bytesused = 0;
1198 buf->flags = V4L2_BUF_FLAG_QUEUED;
1199 break;
1200 case FRAME_READY:
1201 buf->bytesused = cam->buffers[buf->index].length;
1202 buf->timestamp = cam->buffers[buf->index].timestamp;
1203 buf->sequence = cam->buffers[buf->index].seq;
1204 buf->flags = V4L2_BUF_FLAG_DONE;
1205 break;
1206 }
1207
1208 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1209 buf->index, buf->m.offset, buf->flags, buf->sequence,
1210 buf->bytesused);
1211
1212 return 0;
1213}
1214
1215/******************************************************************************
1216 *
1217 * ioctl_qbuf
1218 *
1219 * V4L2 User is freeing buffer
1220 *
1221 *****************************************************************************/
1222
1223static int ioctl_qbuf(void *arg,struct camera_data *cam)
1224{
1225 struct v4l2_buffer *buf = arg;
1226
1227 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1228 buf->memory != V4L2_MEMORY_MMAP ||
1229 buf->index > cam->num_frames)
1230 return -EINVAL;
1231
1232 DBG("QBUF #%d\n", buf->index);
1233
1234 if(cam->buffers[buf->index].status == FRAME_READY)
1235 cam->buffers[buf->index].status = FRAME_EMPTY;
1236
1237 return 0;
1238}
1239
1240/******************************************************************************
1241 *
1242 * find_earliest_filled_buffer
1243 *
1244 * Helper for ioctl_dqbuf. Find the next ready buffer.
1245 *
1246 *****************************************************************************/
1247
1248static int find_earliest_filled_buffer(struct camera_data *cam)
1249{
1250 int i;
1251 int found = -1;
1252 for (i=0; i<cam->num_frames; i++) {
1253 if(cam->buffers[i].status == FRAME_READY) {
1254 if(found < 0) {
1255 found = i;
1256 } else {
1257 /* find which buffer is earlier */
1258 struct timeval *tv1, *tv2;
1259 tv1 = &cam->buffers[i].timestamp;
1260 tv2 = &cam->buffers[found].timestamp;
1261 if(tv1->tv_sec < tv2->tv_sec ||
1262 (tv1->tv_sec == tv2->tv_sec &&
1263 tv1->tv_usec < tv2->tv_usec))
1264 found = i;
1265 }
1266 }
1267 }
1268 return found;
1269}
1270
1271/******************************************************************************
1272 *
1273 * ioctl_dqbuf
1274 *
1275 * V4L2 User is asking for a filled buffer.
1276 *
1277 *****************************************************************************/
1278
1279static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1280{
1281 struct v4l2_buffer *buf = arg;
1282 int frame;
1283
1284 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1285 buf->memory != V4L2_MEMORY_MMAP)
1286 return -EINVAL;
1287
1288 frame = find_earliest_filled_buffer(cam);
1289
1290 if(frame < 0 && file->f_flags&O_NONBLOCK)
1291 return -EAGAIN;
1292
1293 if(frame < 0) {
1294 /* Wait for a frame to become available */
1295 struct framebuf *cb=cam->curbuff;
2ae15191 1296 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1297 wait_event_interruptible(cam->wq_stream,
1298 !cam->present ||
1299 (cb=cam->curbuff)->status == FRAME_READY);
2ae15191 1300 mutex_lock(&cam->busy_lock);
ab33d507
AC
1301 if (signal_pending(current))
1302 return -ERESTARTSYS;
1303 if(!cam->present)
1304 return -ENOTTY;
1305 frame = cb->num;
1306 }
1307
1308
1309 buf->index = frame;
1310 buf->bytesused = cam->buffers[buf->index].length;
1311 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1312 buf->field = V4L2_FIELD_NONE;
1313 buf->timestamp = cam->buffers[buf->index].timestamp;
1314 buf->sequence = cam->buffers[buf->index].seq;
1315 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1316 buf->length = cam->frame_size;
1317 buf->input = 0;
1318 buf->reserved = 0;
1319 memset(&buf->timecode, 0, sizeof(buf->timecode));
1320
1321 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1322 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1323
1324 return 0;
1325}
1326
1327/******************************************************************************
1328 *
1329 * cpia2_ioctl
1330 *
1331 *****************************************************************************/
069b7479 1332static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
ab33d507 1333{
c170ecf4 1334 struct camera_data *cam = video_drvdata(file);
069b7479 1335 long retval = 0;
ab33d507
AC
1336
1337 if (!cam)
1338 return -ENOTTY;
1339
1340 /* make this _really_ smp-safe */
2ae15191 1341 if (mutex_lock_interruptible(&cam->busy_lock))
ab33d507
AC
1342 return -ERESTARTSYS;
1343
1344 if (!cam->present) {
2ae15191 1345 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1346 return -ENODEV;
1347 }
1348
1349 /* Priority check */
f473bf76 1350 switch (cmd) {
ab33d507
AC
1351 case VIDIOC_S_FMT:
1352 {
1353 struct cpia2_fh *fh = file->private_data;
ffb4877b 1354 retval = v4l2_prio_check(&cam->prio, fh->prio);
ab33d507 1355 if(retval) {
2ae15191 1356 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1357 return retval;
1358 }
1359 break;
1360 }
ab33d507
AC
1361 default:
1362 break;
1363 }
1364
f473bf76 1365 switch (cmd) {
ab33d507
AC
1366 /* CPIA2 extension to Video4Linux API */
1367 case CPIA2_IOC_SET_GPIO:
1368 retval = ioctl_set_gpio(arg, cam);
1369 break;
1370 case VIDIOC_QUERYCAP:
1371 retval = ioctl_querycap(arg,cam);
1372 break;
1373
1374 case VIDIOC_ENUMINPUT:
1375 case VIDIOC_G_INPUT:
1376 case VIDIOC_S_INPUT:
f473bf76 1377 retval = ioctl_input(cmd, arg, cam);
ab33d507
AC
1378 break;
1379
1380 case VIDIOC_ENUM_FMT:
1381 retval = ioctl_enum_fmt(arg,cam);
1382 break;
1383 case VIDIOC_TRY_FMT:
1384 retval = ioctl_try_fmt(arg,cam);
1385 break;
1386 case VIDIOC_G_FMT:
1387 retval = ioctl_get_fmt(arg,cam);
1388 break;
1389 case VIDIOC_S_FMT:
1390 retval = ioctl_set_fmt(arg,cam,file->private_data);
1391 break;
1392
1393 case VIDIOC_CROPCAP:
1394 retval = ioctl_cropcap(arg,cam);
1395 break;
1396 case VIDIOC_G_CROP:
1397 case VIDIOC_S_CROP:
1398 // TODO: I think cropping can be implemented - SJB
1399 retval = -EINVAL;
1400 break;
1401
1402 case VIDIOC_QUERYCTRL:
1403 retval = ioctl_queryctrl(arg,cam);
1404 break;
1405 case VIDIOC_QUERYMENU:
1406 retval = ioctl_querymenu(arg,cam);
1407 break;
1408 case VIDIOC_G_CTRL:
1409 retval = ioctl_g_ctrl(arg,cam);
1410 break;
1411 case VIDIOC_S_CTRL:
1412 retval = ioctl_s_ctrl(arg,cam);
1413 break;
1414
1415 case VIDIOC_G_JPEGCOMP:
1416 retval = ioctl_g_jpegcomp(arg,cam);
1417 break;
1418 case VIDIOC_S_JPEGCOMP:
1419 retval = ioctl_s_jpegcomp(arg,cam);
1420 break;
1421
1422 case VIDIOC_G_PRIORITY:
1423 {
1424 struct cpia2_fh *fh = file->private_data;
1425 *(enum v4l2_priority*)arg = fh->prio;
1426 break;
1427 }
1428 case VIDIOC_S_PRIORITY:
1429 {
1430 struct cpia2_fh *fh = file->private_data;
1431 enum v4l2_priority prio;
1432 prio = *(enum v4l2_priority*)arg;
1433 if(cam->streaming &&
1434 prio != fh->prio &&
1435 fh->prio == V4L2_PRIORITY_RECORD) {
1436 /* Can't drop record priority while streaming */
1437 retval = -EBUSY;
1438 } else if(prio == V4L2_PRIORITY_RECORD &&
1439 prio != fh->prio &&
1440 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1441 /* Only one program can record at a time */
1442 retval = -EBUSY;
1443 } else {
1444 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1445 }
1446 break;
1447 }
1448
1449 case VIDIOC_REQBUFS:
1450 retval = ioctl_reqbufs(arg,cam);
1451 break;
1452 case VIDIOC_QUERYBUF:
1453 retval = ioctl_querybuf(arg,cam);
1454 break;
1455 case VIDIOC_QBUF:
1456 retval = ioctl_qbuf(arg,cam);
1457 break;
1458 case VIDIOC_DQBUF:
1459 retval = ioctl_dqbuf(arg,cam,file);
1460 break;
1461 case VIDIOC_STREAMON:
1462 {
1463 int type;
1464 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1465 type = *(int*)arg;
1466 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1467 retval = -EINVAL;
1468
1469 if(!cam->streaming) {
1470 retval = cpia2_usb_stream_start(cam,
1471 cam->params.camera_state.stream_mode);
1472 } else {
1473 retval = -EINVAL;
1474 }
1475
1476 break;
1477 }
1478 case VIDIOC_STREAMOFF:
1479 {
1480 int type;
1481 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1482 type = *(int*)arg;
1483 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1484 retval = -EINVAL;
1485
1486 if(cam->streaming) {
1487 retval = cpia2_usb_stream_stop(cam);
1488 } else {
1489 retval = -EINVAL;
1490 }
1491
1492 break;
1493 }
1494
1495 case VIDIOC_ENUMOUTPUT:
1496 case VIDIOC_G_OUTPUT:
1497 case VIDIOC_S_OUTPUT:
1498 case VIDIOC_G_MODULATOR:
1499 case VIDIOC_S_MODULATOR:
1500
1501 case VIDIOC_ENUMAUDIO:
1502 case VIDIOC_G_AUDIO:
1503 case VIDIOC_S_AUDIO:
1504
1505 case VIDIOC_ENUMAUDOUT:
1506 case VIDIOC_G_AUDOUT:
1507 case VIDIOC_S_AUDOUT:
1508
1509 case VIDIOC_ENUMSTD:
1510 case VIDIOC_QUERYSTD:
1511 case VIDIOC_G_STD:
1512 case VIDIOC_S_STD:
1513
1514 case VIDIOC_G_TUNER:
1515 case VIDIOC_S_TUNER:
1516 case VIDIOC_G_FREQUENCY:
1517 case VIDIOC_S_FREQUENCY:
1518
1519 case VIDIOC_OVERLAY:
1520 case VIDIOC_G_FBUF:
1521 case VIDIOC_S_FBUF:
1522
1523 case VIDIOC_G_PARM:
1524 case VIDIOC_S_PARM:
1525 retval = -EINVAL;
1526 break;
1527 default:
1528 retval = -ENOIOCTLCMD;
1529 break;
1530 }
1531
2ae15191 1532 mutex_unlock(&cam->busy_lock);
ab33d507
AC
1533 return retval;
1534}
1535
069b7479 1536static long cpia2_ioctl(struct file *file,
f473bf76 1537 unsigned int cmd, unsigned long arg)
ab33d507 1538{
f473bf76 1539 return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
ab33d507
AC
1540}
1541
1542/******************************************************************************
1543 *
1544 * cpia2_mmap
1545 *
1546 *****************************************************************************/
1547static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1548{
c170ecf4 1549 struct camera_data *cam = video_drvdata(file);
ab33d507 1550 int retval;
ab33d507
AC
1551
1552 /* Priority check */
1553 struct cpia2_fh *fh = file->private_data;
1554 if(fh->prio != V4L2_PRIORITY_RECORD) {
1555 return -EBUSY;
1556 }
1557
1558 retval = cpia2_remap_buffer(cam, area);
1559
1560 if(!retval)
1561 fh->mmapped = 1;
1562 return retval;
1563}
1564
1565/******************************************************************************
1566 *
1567 * reset_camera_struct_v4l
1568 *
1569 * Sets all values to the defaults
1570 *****************************************************************************/
1571static void reset_camera_struct_v4l(struct camera_data *cam)
1572{
873ecd8f
HV
1573 cam->width = cam->params.roi.width;
1574 cam->height = cam->params.roi.height;
ab33d507
AC
1575
1576 cam->frame_size = buffer_size;
1577 cam->num_frames = num_buffers;
1578
1579 /* FlickerModes */
1580 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1581 cam->params.flicker_control.mains_frequency = flicker_freq;
1582
1583 /* streamMode */
1584 cam->params.camera_state.stream_mode = alternate;
1585
1586 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1587 v4l2_prio_init(&cam->prio);
ab33d507
AC
1588}
1589
1590/***
1591 * The v4l video device structure initialized for this device
1592 ***/
873ecd8f 1593static const struct v4l2_file_operations cpia2_fops = {
2ae15191
AC
1594 .owner = THIS_MODULE,
1595 .open = cpia2_open,
1596 .release = cpia2_close,
1597 .read = cpia2_v4l_read,
1598 .poll = cpia2_v4l_poll,
1599 .ioctl = cpia2_ioctl,
2ae15191 1600 .mmap = cpia2_mmap,
ab33d507
AC
1601};
1602
1603static struct video_device cpia2_template = {
1604 /* I could not find any place for the old .initialize initializer?? */
873ecd8f
HV
1605 .name = "CPiA2 Camera",
1606 .fops = &cpia2_fops,
1607 .release = video_device_release,
ab33d507
AC
1608};
1609
1610/******************************************************************************
1611 *
1612 * cpia2_register_camera
1613 *
1614 *****************************************************************************/
1615int cpia2_register_camera(struct camera_data *cam)
1616{
1617 cam->vdev = video_device_alloc();
1618 if(!cam->vdev)
1619 return -ENOMEM;
1620
1621 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1622 video_set_drvdata(cam->vdev, cam);
1623
1624 reset_camera_struct_v4l(cam);
1625
1626 /* register v4l device */
dc60de33 1627 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
ab33d507
AC
1628 ERR("video_register_device failed\n");
1629 video_device_release(cam->vdev);
1630 return -ENODEV;
1631 }
1632
1633 return 0;
1634}
1635
1636/******************************************************************************
1637 *
1638 * cpia2_unregister_camera
1639 *
1640 *****************************************************************************/
1641void cpia2_unregister_camera(struct camera_data *cam)
1642{
1643 if (!cam->open_count) {
1644 video_unregister_device(cam->vdev);
1645 } else {
38c7c036
LP
1646 LOG("%s removed while open, deferring "
1647 "video_unregister_device\n",
1648 video_device_node_name(cam->vdev));
ab33d507
AC
1649 }
1650}
1651
1652/******************************************************************************
1653 *
1654 * check_parameters
1655 *
1656 * Make sure that all user-supplied parameters are sensible
1657 *****************************************************************************/
1658static void __init check_parameters(void)
1659{
1660 if(buffer_size < PAGE_SIZE) {
1661 buffer_size = PAGE_SIZE;
1662 LOG("buffer_size too small, setting to %d\n", buffer_size);
1663 } else if(buffer_size > 1024*1024) {
1664 /* arbitrary upper limiit */
1665 buffer_size = 1024*1024;
1666 LOG("buffer_size ridiculously large, setting to %d\n",
1667 buffer_size);
1668 } else {
1669 buffer_size += PAGE_SIZE-1;
1670 buffer_size &= ~(PAGE_SIZE-1);
1671 }
1672
1673 if(num_buffers < 1) {
1674 num_buffers = 1;
1675 LOG("num_buffers too small, setting to %d\n", num_buffers);
1676 } else if(num_buffers > VIDEO_MAX_FRAME) {
1677 num_buffers = VIDEO_MAX_FRAME;
1678 LOG("num_buffers too large, setting to %d\n", num_buffers);
1679 }
1680
1681 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1682 alternate = DEFAULT_ALT;
1683 LOG("alternate specified is invalid, using %d\n", alternate);
1684 }
1685
1686 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1687 flicker_mode = NEVER_FLICKER;
1688 LOG("Flicker mode specified is invalid, using %d\n",
1689 flicker_mode);
1690 }
1691
1692 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1693 flicker_freq = FLICKER_60;
1694 LOG("Flicker mode specified is invalid, using %d\n",
1695 flicker_freq);
1696 }
1697
1698 if(video_nr < -1 || video_nr > 64) {
1699 video_nr = -1;
1700 LOG("invalid video_nr specified, must be -1 to 64\n");
1701 }
1702
1703 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1704 num_buffers, buffer_size, alternate);
1705}
1706
1707/************ Module Stuff ***************/
1708
1709
1710/******************************************************************************
1711 *
1712 * cpia2_init/module_init
1713 *
1714 *****************************************************************************/
8cbe84f3 1715static int __init cpia2_init(void)
ab33d507
AC
1716{
1717 LOG("%s v%d.%d.%d\n",
1718 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
1719 check_parameters();
1720 cpia2_usb_init();
1721 return 0;
1722}
1723
1724
1725/******************************************************************************
1726 *
1727 * cpia2_exit/module_exit
1728 *
1729 *****************************************************************************/
8cbe84f3 1730static void __exit cpia2_exit(void)
ab33d507
AC
1731{
1732 cpia2_usb_cleanup();
1733 schedule_timeout(2 * HZ);
1734}
1735
ab33d507
AC
1736module_init(cpia2_init);
1737module_exit(cpia2_exit);
1738