Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cpia2_v4l.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Filename: cpia2_v4l.c
4  *
5  * Copyright 2001, STMicrolectronics, Inc.
6  * Contact: [email protected]
7  * Copyright 2001,2005, Scott J. Bertin <[email protected]>
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 <[email protected]>
30  ****************************************************************************/
31 
32 #define CPIA_VERSION "3.0.1"
33 
34 #include <linux/module.h>
35 #include <linux/time.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/videodev2.h>
40 #include <linux/stringify.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-event.h>
43 
44 #include "cpia2.h"
45 
46 static int video_nr = -1;
47 module_param(video_nr, int, 0);
48 MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
49 
50 static int buffer_size = 68 * 1024;
51 module_param(buffer_size, int, 0);
52 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
53 
54 static int num_buffers = 3;
55 module_param(num_buffers, int, 0);
56 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
57  __stringify(VIDEO_MAX_FRAME) ", default 3)");
58 
59 static int alternate = DEFAULT_ALT;
60 module_param(alternate, int, 0);
61 MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
62  __stringify(USBIF_ISO_6) ", default "
64 
65 static int flicker_mode;
66 module_param(flicker_mode, int, 0);
67 MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or "
68  __stringify(60) ", default 0)");
69 
70 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <[email protected]>");
71 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
73 MODULE_LICENSE("GPL");
75 
76 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
77 #define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
78 
79 /******************************************************************************
80  *
81  * cpia2_open
82  *
83  *****************************************************************************/
84 static int cpia2_open(struct file *file)
85 {
86  struct camera_data *cam = video_drvdata(file);
87  int retval;
88 
90  return -ERESTARTSYS;
91  retval = v4l2_fh_open(file);
92  if (retval)
93  goto open_unlock;
94 
95  if (v4l2_fh_is_singular_file(file)) {
96  if (cpia2_allocate_buffers(cam)) {
97  v4l2_fh_release(file);
98  retval = -ENOMEM;
99  goto open_unlock;
100  }
101 
102  /* reset the camera */
103  if (cpia2_reset_camera(cam) < 0) {
104  v4l2_fh_release(file);
105  retval = -EIO;
106  goto open_unlock;
107  }
108 
109  cam->APP_len = 0;
110  cam->COM_len = 0;
111  }
112 
114 open_unlock:
115  mutex_unlock(&cam->v4l2_lock);
116  return retval;
117 }
118 
119 /******************************************************************************
120  *
121  * cpia2_close
122  *
123  *****************************************************************************/
124 static int cpia2_close(struct file *file)
125 {
126  struct video_device *dev = video_devdata(file);
127  struct camera_data *cam = video_get_drvdata(dev);
128 
129  mutex_lock(&cam->v4l2_lock);
130  if (video_is_registered(&cam->vdev) && v4l2_fh_is_singular_file(file)) {
132 
133  /* save camera state for later open */
135 
136  cpia2_set_low_power(cam);
137  cpia2_free_buffers(cam);
138  }
139 
140  if (cam->stream_fh == file->private_data) {
141  cam->stream_fh = NULL;
142  cam->mmapped = 0;
143  }
144  mutex_unlock(&cam->v4l2_lock);
145  return v4l2_fh_release(file);
146 }
147 
148 /******************************************************************************
149  *
150  * cpia2_v4l_read
151  *
152  *****************************************************************************/
153 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
154  loff_t *off)
155 {
156  struct camera_data *cam = video_drvdata(file);
157  int noblock = file->f_flags&O_NONBLOCK;
158  ssize_t ret;
159 
160  if(!cam)
161  return -EINVAL;
162 
164  return -ERESTARTSYS;
165  ret = cpia2_read(cam, buf, count, noblock);
166  mutex_unlock(&cam->v4l2_lock);
167  return ret;
168 }
169 
170 
171 /******************************************************************************
172  *
173  * cpia2_v4l_poll
174  *
175  *****************************************************************************/
176 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
177 {
178  struct camera_data *cam = video_drvdata(filp);
179  unsigned int res;
180 
181  mutex_lock(&cam->v4l2_lock);
182  res = cpia2_poll(cam, filp, wait);
183  mutex_unlock(&cam->v4l2_lock);
184  return res;
185 }
186 
187 
188 static int sync(struct camera_data *cam, int frame_nr)
189 {
190  struct framebuf *frame = &cam->buffers[frame_nr];
191 
192  while (1) {
193  if (frame->status == FRAME_READY)
194  return 0;
195 
196  if (!cam->streaming) {
197  frame->status = FRAME_READY;
198  frame->length = 0;
199  return 0;
200  }
201 
202  mutex_unlock(&cam->v4l2_lock);
204  !cam->streaming ||
205  frame->status == FRAME_READY);
206  mutex_lock(&cam->v4l2_lock);
207  if (signal_pending(current))
208  return -ERESTARTSYS;
209  if (!video_is_registered(&cam->vdev))
210  return -ENOTTY;
211  }
212 }
213 
214 /******************************************************************************
215  *
216  * ioctl_querycap
217  *
218  * V4L2 device capabilities
219  *
220  *****************************************************************************/
221 
222 static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
223 {
224  struct camera_data *cam = video_drvdata(file);
225 
226  strcpy(vc->driver, "cpia2");
227 
228  if (cam->params.pnp_id.product == 0x151)
229  strcpy(vc->card, "QX5 Microscope");
230  else
231  strcpy(vc->card, "CPiA2 Camera");
232  switch (cam->params.pnp_id.device_type) {
233  case DEVICE_STV_672:
234  strcat(vc->card, " (672/");
235  break;
236  case DEVICE_STV_676:
237  strcat(vc->card, " (676/");
238  break;
239  default:
240  strcat(vc->card, " (XXX/");
241  break;
242  }
243  switch (cam->params.version.sensor_flags) {
245  strcat(vc->card, "404)");
246  break;
248  strcat(vc->card, "407)");
249  break;
251  strcat(vc->card, "409)");
252  break;
254  strcat(vc->card, "410)");
255  break;
257  strcat(vc->card, "500)");
258  break;
259  default:
260  strcat(vc->card, "XXX)");
261  break;
262  }
263 
264  if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
265  memset(vc->bus_info,0, sizeof(vc->bus_info));
266 
270  vc->capabilities = vc->device_caps |
272 
273  return 0;
274 }
275 
276 /******************************************************************************
277  *
278  * ioctl_input
279  *
280  * V4L2 input get/set/enumerate
281  *
282  *****************************************************************************/
283 
284 static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
285 {
286  if (i->index)
287  return -EINVAL;
288  strcpy(i->name, "Camera");
290  return 0;
291 }
292 
293 static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
294 {
295  *i = 0;
296  return 0;
297 }
298 
299 static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
300 {
301  return i ? -EINVAL : 0;
302 }
303 
304 /******************************************************************************
305  *
306  * ioctl_enum_fmt
307  *
308  * V4L2 format enumerate
309  *
310  *****************************************************************************/
311 
312 static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
313  struct v4l2_fmtdesc *f)
314 {
315  int index = f->index;
316 
317  if (index < 0 || index > 1)
318  return -EINVAL;
319 
320  memset(f, 0, sizeof(*f));
321  f->index = index;
324  switch(index) {
325  case 0:
326  strcpy(f->description, "MJPEG");
328  break;
329  case 1:
330  strcpy(f->description, "JPEG");
332  break;
333  default:
334  return -EINVAL;
335  }
336 
337  return 0;
338 }
339 
340 /******************************************************************************
341  *
342  * ioctl_try_fmt
343  *
344  * V4L2 format try
345  *
346  *****************************************************************************/
347 
348 static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
349  struct v4l2_format *f)
350 {
351  struct camera_data *cam = video_drvdata(file);
352 
353  if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
354  f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
355  return -EINVAL;
356 
357  f->fmt.pix.field = V4L2_FIELD_NONE;
358  f->fmt.pix.bytesperline = 0;
359  f->fmt.pix.sizeimage = cam->frame_size;
360  f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
361  f->fmt.pix.priv = 0;
362 
363  switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
364  case VIDEOSIZE_VGA:
365  f->fmt.pix.width = 640;
366  f->fmt.pix.height = 480;
367  break;
368  case VIDEOSIZE_CIF:
369  f->fmt.pix.width = 352;
370  f->fmt.pix.height = 288;
371  break;
372  case VIDEOSIZE_QVGA:
373  f->fmt.pix.width = 320;
374  f->fmt.pix.height = 240;
375  break;
376  case VIDEOSIZE_288_216:
377  f->fmt.pix.width = 288;
378  f->fmt.pix.height = 216;
379  break;
380  case VIDEOSIZE_256_192:
381  f->fmt.pix.width = 256;
382  f->fmt.pix.height = 192;
383  break;
384  case VIDEOSIZE_224_168:
385  f->fmt.pix.width = 224;
386  f->fmt.pix.height = 168;
387  break;
388  case VIDEOSIZE_192_144:
389  f->fmt.pix.width = 192;
390  f->fmt.pix.height = 144;
391  break;
392  case VIDEOSIZE_QCIF:
393  default:
394  f->fmt.pix.width = 176;
395  f->fmt.pix.height = 144;
396  break;
397  }
398 
399  return 0;
400 }
401 
402 /******************************************************************************
403  *
404  * ioctl_set_fmt
405  *
406  * V4L2 format set
407  *
408  *****************************************************************************/
409 
410 static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
411  struct v4l2_format *f)
412 {
413  struct camera_data *cam = video_drvdata(file);
414  int err, frame;
415 
416  err = cpia2_try_fmt_vid_cap(file, _fh, f);
417  if(err != 0)
418  return err;
419 
420  cam->pixelformat = f->fmt.pix.pixelformat;
421 
422  /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
423  * the missing Huffman table properly. */
424  cam->params.compression.inhibit_htables = 0;
425  /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
426 
427  /* we set the video window to something smaller or equal to what
428  * is requested by the user???
429  */
430  DBG("Requested width = %d, height = %d\n",
431  f->fmt.pix.width, f->fmt.pix.height);
432  if (f->fmt.pix.width != cam->width ||
433  f->fmt.pix.height != cam->height) {
434  cam->width = f->fmt.pix.width;
435  cam->height = f->fmt.pix.height;
436  cam->params.roi.width = f->fmt.pix.width;
437  cam->params.roi.height = f->fmt.pix.height;
438  cpia2_set_format(cam);
439  }
440 
441  for (frame = 0; frame < cam->num_frames; ++frame) {
442  if (cam->buffers[frame].status == FRAME_READING)
443  if ((err = sync(cam, frame)) < 0)
444  return err;
445 
446  cam->buffers[frame].status = FRAME_EMPTY;
447  }
448 
449  return 0;
450 }
451 
452 /******************************************************************************
453  *
454  * ioctl_get_fmt
455  *
456  * V4L2 format get
457  *
458  *****************************************************************************/
459 
460 static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
461  struct v4l2_format *f)
462 {
463  struct camera_data *cam = video_drvdata(file);
464 
465  f->fmt.pix.width = cam->width;
466  f->fmt.pix.height = cam->height;
467  f->fmt.pix.pixelformat = cam->pixelformat;
468  f->fmt.pix.field = V4L2_FIELD_NONE;
469  f->fmt.pix.bytesperline = 0;
470  f->fmt.pix.sizeimage = cam->frame_size;
471  f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
472  f->fmt.pix.priv = 0;
473 
474  return 0;
475 }
476 
477 /******************************************************************************
478  *
479  * ioctl_cropcap
480  *
481  * V4L2 query cropping capabilities
482  * NOTE: cropping is currently disabled
483  *
484  *****************************************************************************/
485 
486 static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
487 {
488  struct camera_data *cam = video_drvdata(file);
489 
491  return -EINVAL;
492 
493  c->bounds.left = 0;
494  c->bounds.top = 0;
495  c->bounds.width = cam->width;
496  c->bounds.height = cam->height;
497  c->defrect.left = 0;
498  c->defrect.top = 0;
499  c->defrect.width = cam->width;
500  c->defrect.height = cam->height;
501  c->pixelaspect.numerator = 1;
502  c->pixelaspect.denominator = 1;
503 
504  return 0;
505 }
506 
508  int value;
510 };
511 
512 static const struct framerate_info framerate_controls[] = {
513  { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } },
514  { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } },
515  { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } },
516  { CPIA2_VP_FRAMERATE_15, { 1, 15 } },
517  { CPIA2_VP_FRAMERATE_25, { 1, 25 } },
518  { CPIA2_VP_FRAMERATE_30, { 1, 30 } },
519 };
520 
521 static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
522 {
523  struct camera_data *cam = video_drvdata(file);
524  struct v4l2_captureparm *cap = &p->parm.capture;
525  int i;
526 
528  return -EINVAL;
529 
531  cap->readbuffers = cam->num_frames;
532  for (i = 0; i < ARRAY_SIZE(framerate_controls); i++)
533  if (cam->params.vp_params.frame_rate == framerate_controls[i].value) {
534  cap->timeperframe = framerate_controls[i].period;
535  break;
536  }
537  return 0;
538 }
539 
540 static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
541 {
542  struct camera_data *cam = video_drvdata(file);
543  struct v4l2_captureparm *cap = &p->parm.capture;
544  struct v4l2_fract tpf = cap->timeperframe;
545  int max = ARRAY_SIZE(framerate_controls) - 1;
546  int ret;
547  int i;
548 
549  ret = cpia2_g_parm(file, fh, p);
550  if (ret || !tpf.denominator || !tpf.numerator)
551  return ret;
552 
553  /* Maximum 15 fps for this model */
554  if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
555  cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
556  max -= 2;
557  for (i = 0; i <= max; i++) {
558  struct v4l2_fract f1 = tpf;
559  struct v4l2_fract f2 = framerate_controls[i].period;
560 
561  f1.numerator *= f2.denominator;
562  f2.numerator *= f1.denominator;
563  if (f1.numerator >= f2.numerator)
564  break;
565  }
566  if (i > max)
567  i = max;
568  cap->timeperframe = framerate_controls[i].period;
569  return cpia2_set_fps(cam, framerate_controls[i].value);
570 }
571 
572 static const struct {
575 } cpia2_framesizes[] = {
576  { 640, 480 },
577  { 352, 288 },
578  { 320, 240 },
579  { 288, 216 },
580  { 256, 192 },
581  { 224, 168 },
582  { 192, 144 },
583  { 176, 144 },
584 };
585 
586 static int cpia2_enum_framesizes(struct file *file, void *fh,
587  struct v4l2_frmsizeenum *fsize)
588 {
589 
590  if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG &&
592  return -EINVAL;
593  if (fsize->index >= ARRAY_SIZE(cpia2_framesizes))
594  return -EINVAL;
596  fsize->discrete.width = cpia2_framesizes[fsize->index].width;
597  fsize->discrete.height = cpia2_framesizes[fsize->index].height;
598 
599  return 0;
600 }
601 
602 static int cpia2_enum_frameintervals(struct file *file, void *fh,
603  struct v4l2_frmivalenum *fival)
604 {
605  struct camera_data *cam = video_drvdata(file);
606  int max = ARRAY_SIZE(framerate_controls) - 1;
607  int i;
608 
609  if (fival->pixel_format != V4L2_PIX_FMT_MJPEG &&
611  return -EINVAL;
612 
613  /* Maximum 15 fps for this model */
614  if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
615  cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
616  max -= 2;
617  if (fival->index > max)
618  return -EINVAL;
619  for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++)
620  if (fival->width == cpia2_framesizes[i].width &&
621  fival->height == cpia2_framesizes[i].height)
622  break;
623  if (i == ARRAY_SIZE(cpia2_framesizes))
624  return -EINVAL;
626  fival->discrete = framerate_controls[fival->index].period;
627  return 0;
628 }
629 
630 /******************************************************************************
631  *
632  * ioctl_s_ctrl
633  *
634  * V4L2 set the value of a control variable
635  *
636  *****************************************************************************/
637 
638 static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl)
639 {
640  struct camera_data *cam =
641  container_of(ctrl->handler, struct camera_data, hdl);
642  static const int flicker_table[] = {
644  FLICKER_50,
645  FLICKER_60,
646  };
647 
648  DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val);
649 
650  switch (ctrl->id) {
651  case V4L2_CID_BRIGHTNESS:
652  cpia2_set_brightness(cam, ctrl->val);
653  break;
654  case V4L2_CID_CONTRAST:
655  cpia2_set_contrast(cam, ctrl->val);
656  break;
657  case V4L2_CID_SATURATION:
658  cpia2_set_saturation(cam, ctrl->val);
659  break;
660  case V4L2_CID_HFLIP:
661  cpia2_set_property_mirror(cam, ctrl->val);
662  break;
663  case V4L2_CID_VFLIP:
664  cpia2_set_property_flip(cam, ctrl->val);
665  break;
667  return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]);
669  return cpia2_set_gpio(cam, (cam->top_light->val << 6) |
670  (cam->bottom_light->val << 7));
672  cam->params.compression.inhibit_htables =
673  !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT);
674  break;
676  cam->params.vc_params.quality = ctrl->val;
677  break;
678  case CPIA2_CID_USB_ALT:
679  cam->params.camera_state.stream_mode = ctrl->val;
680  break;
681  default:
682  return -EINVAL;
683  }
684 
685  return 0;
686 }
687 
688 /******************************************************************************
689  *
690  * ioctl_g_jpegcomp
691  *
692  * V4L2 get the JPEG compression parameters
693  *
694  *****************************************************************************/
695 
696 static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
697 {
698  struct camera_data *cam = video_drvdata(file);
699 
700  memset(parms, 0, sizeof(*parms));
701 
702  parms->quality = 80; // TODO: Can this be made meaningful?
703 
705  if(!cam->params.compression.inhibit_htables) {
707  }
708 
709  parms->APPn = cam->APPn;
710  parms->APP_len = cam->APP_len;
711  if(cam->APP_len > 0) {
712  memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
714  }
715 
716  parms->COM_len = cam->COM_len;
717  if(cam->COM_len > 0) {
718  memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
719  parms->jpeg_markers |= JPEG_MARKER_COM;
720  }
721 
722  DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
723  parms->APP_len, parms->COM_len);
724 
725  return 0;
726 }
727 
728 /******************************************************************************
729  *
730  * ioctl_s_jpegcomp
731  *
732  * V4L2 set the JPEG compression parameters
733  * NOTE: quality and some jpeg_markers are ignored.
734  *
735  *****************************************************************************/
736 
737 static int cpia2_s_jpegcomp(struct file *file, void *fh,
738  const struct v4l2_jpegcompression *parms)
739 {
740  struct camera_data *cam = video_drvdata(file);
741 
742  DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
743  parms->APP_len, parms->COM_len);
744 
745  cam->params.compression.inhibit_htables =
747 
748  if(parms->APP_len != 0) {
749  if(parms->APP_len > 0 &&
750  parms->APP_len <= sizeof(cam->APP_data) &&
751  parms->APPn >= 0 && parms->APPn <= 15) {
752  cam->APPn = parms->APPn;
753  cam->APP_len = parms->APP_len;
754  memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
755  } else {
756  LOG("Bad APPn Params n=%d len=%d\n",
757  parms->APPn, parms->APP_len);
758  return -EINVAL;
759  }
760  } else {
761  cam->APP_len = 0;
762  }
763 
764  if(parms->COM_len != 0) {
765  if(parms->COM_len > 0 &&
766  parms->COM_len <= sizeof(cam->COM_data)) {
767  cam->COM_len = parms->COM_len;
768  memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
769  } else {
770  LOG("Bad COM_len=%d\n", parms->COM_len);
771  return -EINVAL;
772  }
773  }
774 
775  return 0;
776 }
777 
778 /******************************************************************************
779  *
780  * ioctl_reqbufs
781  *
782  * V4L2 Initiate memory mapping.
783  * NOTE: The user's request is ignored. For now the buffers are fixed.
784  *
785  *****************************************************************************/
786 
787 static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
788 {
789  struct camera_data *cam = video_drvdata(file);
790 
791  if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
792  req->memory != V4L2_MEMORY_MMAP)
793  return -EINVAL;
794 
795  DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
796  req->count = cam->num_frames;
797  memset(&req->reserved, 0, sizeof(req->reserved));
798 
799  return 0;
800 }
801 
802 /******************************************************************************
803  *
804  * ioctl_querybuf
805  *
806  * V4L2 Query memory buffer status.
807  *
808  *****************************************************************************/
809 
810 static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
811 {
812  struct camera_data *cam = video_drvdata(file);
813 
814  if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
815  buf->index > cam->num_frames)
816  return -EINVAL;
817 
818  buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
819  buf->length = cam->frame_size;
820 
821  buf->memory = V4L2_MEMORY_MMAP;
822 
823  if(cam->mmapped)
825  else
826  buf->flags = 0;
827 
828  switch (cam->buffers[buf->index].status) {
829  case FRAME_EMPTY:
830  case FRAME_ERROR:
831  case FRAME_READING:
832  buf->bytesused = 0;
834  break;
835  case FRAME_READY:
836  buf->bytesused = cam->buffers[buf->index].length;
837  buf->timestamp = cam->buffers[buf->index].timestamp;
838  buf->sequence = cam->buffers[buf->index].seq;
839  buf->flags = V4L2_BUF_FLAG_DONE;
840  break;
841  }
842 
843  DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
844  buf->index, buf->m.offset, buf->flags, buf->sequence,
845  buf->bytesused);
846 
847  return 0;
848 }
849 
850 /******************************************************************************
851  *
852  * ioctl_qbuf
853  *
854  * V4L2 User is freeing buffer
855  *
856  *****************************************************************************/
857 
858 static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
859 {
860  struct camera_data *cam = video_drvdata(file);
861 
862  if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
863  buf->memory != V4L2_MEMORY_MMAP ||
864  buf->index > cam->num_frames)
865  return -EINVAL;
866 
867  DBG("QBUF #%d\n", buf->index);
868 
869  if(cam->buffers[buf->index].status == FRAME_READY)
870  cam->buffers[buf->index].status = FRAME_EMPTY;
871 
872  return 0;
873 }
874 
875 /******************************************************************************
876  *
877  * find_earliest_filled_buffer
878  *
879  * Helper for ioctl_dqbuf. Find the next ready buffer.
880  *
881  *****************************************************************************/
882 
883 static int find_earliest_filled_buffer(struct camera_data *cam)
884 {
885  int i;
886  int found = -1;
887  for (i=0; i<cam->num_frames; i++) {
888  if(cam->buffers[i].status == FRAME_READY) {
889  if(found < 0) {
890  found = i;
891  } else {
892  /* find which buffer is earlier */
893  struct timeval *tv1, *tv2;
894  tv1 = &cam->buffers[i].timestamp;
895  tv2 = &cam->buffers[found].timestamp;
896  if(tv1->tv_sec < tv2->tv_sec ||
897  (tv1->tv_sec == tv2->tv_sec &&
898  tv1->tv_usec < tv2->tv_usec))
899  found = i;
900  }
901  }
902  }
903  return found;
904 }
905 
906 /******************************************************************************
907  *
908  * ioctl_dqbuf
909  *
910  * V4L2 User is asking for a filled buffer.
911  *
912  *****************************************************************************/
913 
914 static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
915 {
916  struct camera_data *cam = video_drvdata(file);
917  int frame;
918 
919  if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
920  buf->memory != V4L2_MEMORY_MMAP)
921  return -EINVAL;
922 
923  frame = find_earliest_filled_buffer(cam);
924 
926  return -EAGAIN;
927 
928  if(frame < 0) {
929  /* Wait for a frame to become available */
930  struct framebuf *cb=cam->curbuff;
931  mutex_unlock(&cam->v4l2_lock);
933  !video_is_registered(&cam->vdev) ||
934  (cb=cam->curbuff)->status == FRAME_READY);
935  mutex_lock(&cam->v4l2_lock);
936  if (signal_pending(current))
937  return -ERESTARTSYS;
938  if (!video_is_registered(&cam->vdev))
939  return -ENOTTY;
940  frame = cb->num;
941  }
942 
943 
944  buf->index = frame;
945  buf->bytesused = cam->buffers[buf->index].length;
947  buf->field = V4L2_FIELD_NONE;
948  buf->timestamp = cam->buffers[buf->index].timestamp;
949  buf->sequence = cam->buffers[buf->index].seq;
950  buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
951  buf->length = cam->frame_size;
952  buf->reserved2 = 0;
953  buf->reserved = 0;
954  memset(&buf->timecode, 0, sizeof(buf->timecode));
955 
956  DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
957  cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
958 
959  return 0;
960 }
961 
962 static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
963 {
964  struct camera_data *cam = video_drvdata(file);
965  int ret = -EINVAL;
966 
967  DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
968  if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
969  return -EINVAL;
970 
971  if (!cam->streaming) {
972  ret = cpia2_usb_stream_start(cam,
973  cam->params.camera_state.stream_mode);
974  if (!ret)
975  v4l2_ctrl_grab(cam->usb_alt, true);
976  }
977  return ret;
978 }
979 
980 static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
981 {
982  struct camera_data *cam = video_drvdata(file);
983  int ret = -EINVAL;
984 
985  DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
986  if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
987  return -EINVAL;
988 
989  if (cam->streaming) {
990  ret = cpia2_usb_stream_stop(cam);
991  if (!ret)
992  v4l2_ctrl_grab(cam->usb_alt, false);
993  }
994  return ret;
995 }
996 
997 /******************************************************************************
998  *
999  * cpia2_mmap
1000  *
1001  *****************************************************************************/
1002 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1003 {
1004  struct camera_data *cam = video_drvdata(file);
1005  int retval;
1006 
1008  return -ERESTARTSYS;
1009  retval = cpia2_remap_buffer(cam, area);
1010 
1011  if(!retval)
1012  cam->stream_fh = file->private_data;
1013  mutex_unlock(&cam->v4l2_lock);
1014  return retval;
1015 }
1016 
1017 /******************************************************************************
1018  *
1019  * reset_camera_struct_v4l
1020  *
1021  * Sets all values to the defaults
1022  *****************************************************************************/
1023 static void reset_camera_struct_v4l(struct camera_data *cam)
1024 {
1025  cam->width = cam->params.roi.width;
1026  cam->height = cam->params.roi.height;
1027 
1028  cam->frame_size = buffer_size;
1029  cam->num_frames = num_buffers;
1030 
1031  /* Flicker modes */
1032  cam->params.flicker_control.flicker_mode_req = flicker_mode;
1033 
1034  /* stream modes */
1035  cam->params.camera_state.stream_mode = alternate;
1036 
1038 }
1039 
1040 static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1041  .vidioc_querycap = cpia2_querycap,
1042  .vidioc_enum_input = cpia2_enum_input,
1043  .vidioc_g_input = cpia2_g_input,
1044  .vidioc_s_input = cpia2_s_input,
1045  .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1046  .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1047  .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1048  .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
1049  .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1050  .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1051  .vidioc_cropcap = cpia2_cropcap,
1052  .vidioc_reqbufs = cpia2_reqbufs,
1053  .vidioc_querybuf = cpia2_querybuf,
1054  .vidioc_qbuf = cpia2_qbuf,
1055  .vidioc_dqbuf = cpia2_dqbuf,
1056  .vidioc_streamon = cpia2_streamon,
1057  .vidioc_streamoff = cpia2_streamoff,
1058  .vidioc_s_parm = cpia2_s_parm,
1059  .vidioc_g_parm = cpia2_g_parm,
1060  .vidioc_enum_framesizes = cpia2_enum_framesizes,
1061  .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1062  .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1063  .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1064 };
1065 
1066 /***
1067  * The v4l video device structure initialized for this device
1068  ***/
1069 static const struct v4l2_file_operations cpia2_fops = {
1070  .owner = THIS_MODULE,
1071  .open = cpia2_open,
1072  .release = cpia2_close,
1073  .read = cpia2_v4l_read,
1074  .poll = cpia2_v4l_poll,
1075  .unlocked_ioctl = video_ioctl2,
1076  .mmap = cpia2_mmap,
1077 };
1078 
1079 static struct video_device cpia2_template = {
1080  /* I could not find any place for the old .initialize initializer?? */
1081  .name = "CPiA2 Camera",
1082  .fops = &cpia2_fops,
1083  .ioctl_ops = &cpia2_ioctl_ops,
1084  .release = video_device_release_empty,
1085 };
1086 
1088 {
1089  struct camera_data *cam =
1090  container_of(v4l2_dev, struct camera_data, v4l2_dev);
1091 
1092  v4l2_ctrl_handler_free(&cam->hdl);
1094  kfree(cam);
1095 }
1096 
1097 static const struct v4l2_ctrl_ops cpia2_ctrl_ops = {
1098  .s_ctrl = cpia2_s_ctrl,
1099 };
1100 
1101 /******************************************************************************
1102  *
1103  * cpia2_register_camera
1104  *
1105  *****************************************************************************/
1107 {
1108  struct v4l2_ctrl_handler *hdl = &cam->hdl;
1109  struct v4l2_ctrl_config cpia2_usb_alt = {
1110  .ops = &cpia2_ctrl_ops,
1111  .id = CPIA2_CID_USB_ALT,
1112  .name = "USB Alternate",
1113  .type = V4L2_CTRL_TYPE_INTEGER,
1114  .min = USBIF_ISO_1,
1115  .max = USBIF_ISO_6,
1116  .step = 1,
1117  };
1118  int ret;
1119 
1120  v4l2_ctrl_handler_init(hdl, 12);
1121  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1123  cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0,
1124  255, 1, DEFAULT_BRIGHTNESS);
1125  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1126  V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST);
1127  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1129  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1130  V4L2_CID_HFLIP, 0, 1, 1, 0);
1131  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1135  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1137  100, 1, 100);
1138  cpia2_usb_alt.def = alternate;
1139  cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL);
1140  /* VP5 Only */
1141  if (cam->params.pnp_id.device_type != DEVICE_STV_672)
1142  v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1143  V4L2_CID_VFLIP, 0, 1, 1, 0);
1144  /* Flicker control only valid for 672 */
1145  if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1146  v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops,
1149  /* Light control only valid for the QX5 Microscope */
1150  if (cam->params.pnp_id.product == 0x151) {
1151  cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1152  V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
1153  cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1154  V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
1155  v4l2_ctrl_cluster(2, &cam->top_light);
1156  }
1157 
1158  if (hdl->error) {
1159  ret = hdl->error;
1161  return ret;
1162  }
1163 
1164  cam->vdev = cpia2_template;
1165  video_set_drvdata(&cam->vdev, cam);
1166  cam->vdev.lock = &cam->v4l2_lock;
1167  cam->vdev.ctrl_handler = hdl;
1168  cam->vdev.v4l2_dev = &cam->v4l2_dev;
1169  set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
1170 
1171  reset_camera_struct_v4l(cam);
1172 
1173  /* register v4l device */
1174  if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1175  ERR("video_register_device failed\n");
1176  return -ENODEV;
1177  }
1178 
1179  return 0;
1180 }
1181 
1182 /******************************************************************************
1183  *
1184  * cpia2_unregister_camera
1185  *
1186  *****************************************************************************/
1188 {
1190 }
1191 
1192 /******************************************************************************
1193  *
1194  * check_parameters
1195  *
1196  * Make sure that all user-supplied parameters are sensible
1197  *****************************************************************************/
1198 static void __init check_parameters(void)
1199 {
1200  if(buffer_size < PAGE_SIZE) {
1201  buffer_size = PAGE_SIZE;
1202  LOG("buffer_size too small, setting to %d\n", buffer_size);
1203  } else if(buffer_size > 1024*1024) {
1204  /* arbitrary upper limiit */
1205  buffer_size = 1024*1024;
1206  LOG("buffer_size ridiculously large, setting to %d\n",
1207  buffer_size);
1208  } else {
1209  buffer_size += PAGE_SIZE-1;
1210  buffer_size &= ~(PAGE_SIZE-1);
1211  }
1212 
1213  if(num_buffers < 1) {
1214  num_buffers = 1;
1215  LOG("num_buffers too small, setting to %d\n", num_buffers);
1216  } else if(num_buffers > VIDEO_MAX_FRAME) {
1217  num_buffers = VIDEO_MAX_FRAME;
1218  LOG("num_buffers too large, setting to %d\n", num_buffers);
1219  }
1220 
1222  alternate = DEFAULT_ALT;
1223  LOG("alternate specified is invalid, using %d\n", alternate);
1224  }
1225 
1226  if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) {
1227  flicker_mode = 0;
1228  LOG("Flicker mode specified is invalid, using %d\n",
1229  flicker_mode);
1230  }
1231 
1232  DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1233  num_buffers, buffer_size, alternate);
1234 }
1235 
1236 /************ Module Stuff ***************/
1237 
1238 
1239 /******************************************************************************
1240  *
1241  * cpia2_init/module_init
1242  *
1243  *****************************************************************************/
1244 static int __init cpia2_init(void)
1245 {
1246  LOG("%s v%s\n",
1247  ABOUT, CPIA_VERSION);
1248  check_parameters();
1249  cpia2_usb_init();
1250  return 0;
1251 }
1252 
1253 
1254 /******************************************************************************
1255  *
1256  * cpia2_exit/module_exit
1257  *
1258  *****************************************************************************/
1259 static void __exit cpia2_exit(void)
1260 {
1262  schedule_timeout(2 * HZ);
1263 }
1264 
1265 module_init(cpia2_init);
1266 module_exit(cpia2_exit);