blob: a62a7b739991806567231870dc143ceb3cac675a [file] [log] [blame]
Alan Coxab33d502006-02-27 00:09:05 -03001/****************************************************************************
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
Alan Coxd9b01442008-10-27 15:13:47 -030029 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Alan Coxab33d502006-02-27 00:09:05 -030030 ****************************************************************************/
31
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030032#define CPIA_VERSION "3.0.1"
Alan Coxab33d502006-02-27 00:09:05 -030033
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>
Hans Verkuil873ecd82010-09-19 06:00:54 -030039#include <linux/videodev2.h>
Thiago Farinacb63c2a2009-12-10 16:40:50 -030040#include <linux/stringify.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Hans Verkuil6c493f82012-04-29 08:44:44 -030042#include <media/v4l2-event.h>
Alan Coxab33d502006-02-27 00:09:05 -030043
44#include "cpia2.h"
Alan Coxab33d502006-02-27 00:09:05 -030045
Alan Coxab33d502006-02-27 00:09:05 -030046static int video_nr = -1;
47module_param(video_nr, int, 0);
Hans Verkuil6c493f82012-04-29 08:44:44 -030048MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
Alan Coxab33d502006-02-27 00:09:05 -030049
Hans Verkuil6c493f82012-04-29 08:44:44 -030050static int buffer_size = 68 * 1024;
Alan Coxab33d502006-02-27 00:09:05 -030051module_param(buffer_size, int, 0);
52MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
53
54static int num_buffers = 3;
55module_param(num_buffers, int, 0);
56MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
Thiago Farinacb63c2a2009-12-10 16:40:50 -030057 __stringify(VIDEO_MAX_FRAME) ", default 3)");
Alan Coxab33d502006-02-27 00:09:05 -030058
59static int alternate = DEFAULT_ALT;
60module_param(alternate, int, 0);
Thiago Farinacb63c2a2009-12-10 16:40:50 -030061MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
62 __stringify(USBIF_ISO_6) ", default "
63 __stringify(DEFAULT_ALT) ")");
Alan Coxab33d502006-02-27 00:09:05 -030064
Hans Verkuil6c493f82012-04-29 08:44:44 -030065static int flicker_mode;
Alan Coxab33d502006-02-27 00:09:05 -030066module_param(flicker_mode, int, 0);
Hans Verkuil6c493f82012-04-29 08:44:44 -030067MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or "
68 __stringify(60) ", default 0)");
Alan Coxab33d502006-02-27 00:09:05 -030069
70MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
71MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
72MODULE_SUPPORTED_DEVICE("video");
73MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030074MODULE_VERSION(CPIA_VERSION);
Alan Coxab33d502006-02-27 00:09:05 -030075
76#define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
Hans Verkuil6c493f82012-04-29 08:44:44 -030077#define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
Alan Coxab33d502006-02-27 00:09:05 -030078
79/******************************************************************************
80 *
81 * cpia2_open
82 *
83 *****************************************************************************/
Hans Verkuilbec43662008-12-30 06:58:20 -030084static int cpia2_open(struct file *file)
Alan Coxab33d502006-02-27 00:09:05 -030085{
Hans Verkuilc170ecf2008-08-23 08:32:09 -030086 struct camera_data *cam = video_drvdata(file);
Hans Verkuil6c493f82012-04-29 08:44:44 -030087 int retval = v4l2_fh_open(file);
Alan Coxab33d502006-02-27 00:09:05 -030088
Hans Verkuil6c493f82012-04-29 08:44:44 -030089 if (retval)
90 return retval;
Alan Coxab33d502006-02-27 00:09:05 -030091
Hans Verkuil6c493f82012-04-29 08:44:44 -030092 if (v4l2_fh_is_singular_file(file)) {
93 if (cpia2_allocate_buffers(cam)) {
94 v4l2_fh_release(file);
Hans Verkuild2db8fe2010-12-18 09:50:43 -030095 return -ENOMEM;
Hans Verkuil6c493f82012-04-29 08:44:44 -030096 }
Hans Verkuild2db8fe2010-12-18 09:50:43 -030097
98 /* reset the camera */
Hans Verkuil6c493f82012-04-29 08:44:44 -030099 if (cpia2_reset_camera(cam) < 0) {
100 v4l2_fh_release(file);
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300101 return -EIO;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300102 }
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300103
104 cam->APP_len = 0;
105 cam->COM_len = 0;
Alan Coxab33d502006-02-27 00:09:05 -0300106 }
107
Alan Coxab33d502006-02-27 00:09:05 -0300108 cpia2_dbg_dump_registers(cam);
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300109 return 0;
Alan Coxab33d502006-02-27 00:09:05 -0300110}
111
112/******************************************************************************
113 *
114 * cpia2_close
115 *
116 *****************************************************************************/
Hans Verkuilbec43662008-12-30 06:58:20 -0300117static int cpia2_close(struct file *file)
Alan Coxab33d502006-02-27 00:09:05 -0300118{
119 struct video_device *dev = video_devdata(file);
120 struct camera_data *cam = video_get_drvdata(dev);
Alan Coxab33d502006-02-27 00:09:05 -0300121
Hans Verkuil6c493f82012-04-29 08:44:44 -0300122 if (video_is_registered(&cam->vdev) && v4l2_fh_is_singular_file(file)) {
Alan Coxab33d502006-02-27 00:09:05 -0300123 cpia2_usb_stream_stop(cam);
124
Hans Verkuil6c493f82012-04-29 08:44:44 -0300125 /* save camera state for later open */
126 cpia2_save_camera_state(cam);
Alan Coxab33d502006-02-27 00:09:05 -0300127
Hans Verkuil6c493f82012-04-29 08:44:44 -0300128 cpia2_set_low_power(cam);
Alan Coxab33d502006-02-27 00:09:05 -0300129 cpia2_free_buffers(cam);
Alan Coxab33d502006-02-27 00:09:05 -0300130 }
131
Hans Verkuil6c493f82012-04-29 08:44:44 -0300132 if (cam->stream_fh == file->private_data) {
133 cam->stream_fh = NULL;
134 cam->mmapped = 0;
135 }
136 return v4l2_fh_release(file);
Alan Coxab33d502006-02-27 00:09:05 -0300137}
138
139/******************************************************************************
140 *
141 * cpia2_v4l_read
142 *
143 *****************************************************************************/
144static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
145 loff_t *off)
146{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300147 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300148 int noblock = file->f_flags&O_NONBLOCK;
149
Alan Coxab33d502006-02-27 00:09:05 -0300150 if(!cam)
151 return -EINVAL;
152
Alan Coxab33d502006-02-27 00:09:05 -0300153 return cpia2_read(cam, buf, count, noblock);
154}
155
156
157/******************************************************************************
158 *
159 * cpia2_v4l_poll
160 *
161 *****************************************************************************/
162static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
163{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300164 struct camera_data *cam = video_drvdata(filp);
Alan Coxab33d502006-02-27 00:09:05 -0300165
166 return cpia2_poll(cam, filp, wait);
167}
168
169
Alan Coxab33d502006-02-27 00:09:05 -0300170static int sync(struct camera_data *cam, int frame_nr)
171{
172 struct framebuf *frame = &cam->buffers[frame_nr];
173
174 while (1) {
175 if (frame->status == FRAME_READY)
176 return 0;
177
178 if (!cam->streaming) {
179 frame->status = FRAME_READY;
180 frame->length = 0;
181 return 0;
182 }
183
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300184 mutex_unlock(&cam->v4l2_lock);
Alan Coxab33d502006-02-27 00:09:05 -0300185 wait_event_interruptible(cam->wq_stream,
186 !cam->streaming ||
187 frame->status == FRAME_READY);
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300188 mutex_lock(&cam->v4l2_lock);
Alan Coxab33d502006-02-27 00:09:05 -0300189 if (signal_pending(current))
190 return -ERESTARTSYS;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300191 if (!video_is_registered(&cam->vdev))
Alan Coxab33d502006-02-27 00:09:05 -0300192 return -ENOTTY;
193 }
194}
195
196/******************************************************************************
197 *
Alan Coxab33d502006-02-27 00:09:05 -0300198 * ioctl_querycap
199 *
200 * V4L2 device capabilities
201 *
202 *****************************************************************************/
203
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300204static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
Alan Coxab33d502006-02-27 00:09:05 -0300205{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300206 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300207
Alan Coxab33d502006-02-27 00:09:05 -0300208 strcpy(vc->driver, "cpia2");
209
210 if (cam->params.pnp_id.product == 0x151)
211 strcpy(vc->card, "QX5 Microscope");
212 else
213 strcpy(vc->card, "CPiA2 Camera");
214 switch (cam->params.pnp_id.device_type) {
215 case DEVICE_STV_672:
216 strcat(vc->card, " (672/");
217 break;
218 case DEVICE_STV_676:
219 strcat(vc->card, " (676/");
220 break;
221 default:
Hans Petter Selaskyed82b9c2011-05-23 07:59:37 -0300222 strcat(vc->card, " (XXX/");
Alan Coxab33d502006-02-27 00:09:05 -0300223 break;
224 }
225 switch (cam->params.version.sensor_flags) {
226 case CPIA2_VP_SENSOR_FLAGS_404:
227 strcat(vc->card, "404)");
228 break;
229 case CPIA2_VP_SENSOR_FLAGS_407:
230 strcat(vc->card, "407)");
231 break;
232 case CPIA2_VP_SENSOR_FLAGS_409:
233 strcat(vc->card, "409)");
234 break;
235 case CPIA2_VP_SENSOR_FLAGS_410:
236 strcat(vc->card, "410)");
237 break;
238 case CPIA2_VP_SENSOR_FLAGS_500:
239 strcat(vc->card, "500)");
240 break;
241 default:
Hans Petter Selaskyed82b9c2011-05-23 07:59:37 -0300242 strcat(vc->card, "XXX)");
Alan Coxab33d502006-02-27 00:09:05 -0300243 break;
244 }
245
246 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
247 memset(vc->bus_info,0, sizeof(vc->bus_info));
248
Hans Verkuil6c493f82012-04-29 08:44:44 -0300249 vc->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Alan Coxab33d502006-02-27 00:09:05 -0300250 V4L2_CAP_READWRITE |
251 V4L2_CAP_STREAMING;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300252 vc->capabilities = vc->device_caps |
253 V4L2_CAP_DEVICE_CAPS;
Alan Coxab33d502006-02-27 00:09:05 -0300254
255 return 0;
256}
257
258/******************************************************************************
259 *
260 * ioctl_input
261 *
262 * V4L2 input get/set/enumerate
263 *
264 *****************************************************************************/
265
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300266static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
Alan Coxab33d502006-02-27 00:09:05 -0300267{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300268 if (i->index)
269 return -EINVAL;
Alan Coxab33d502006-02-27 00:09:05 -0300270 strcpy(i->name, "Camera");
271 i->type = V4L2_INPUT_TYPE_CAMERA;
Alan Coxab33d502006-02-27 00:09:05 -0300272 return 0;
273}
274
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300275static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
276{
277 *i = 0;
278 return 0;
279}
280
281static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
282{
283 return i ? -EINVAL : 0;
284}
285
Alan Coxab33d502006-02-27 00:09:05 -0300286/******************************************************************************
287 *
288 * ioctl_enum_fmt
289 *
290 * V4L2 format enumerate
291 *
292 *****************************************************************************/
293
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300294static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
295 struct v4l2_fmtdesc *f)
Alan Coxab33d502006-02-27 00:09:05 -0300296{
Alan Coxab33d502006-02-27 00:09:05 -0300297 int index = f->index;
298
299 if (index < 0 || index > 1)
300 return -EINVAL;
301
302 memset(f, 0, sizeof(*f));
303 f->index = index;
304 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
305 f->flags = V4L2_FMT_FLAG_COMPRESSED;
306 switch(index) {
307 case 0:
308 strcpy(f->description, "MJPEG");
309 f->pixelformat = V4L2_PIX_FMT_MJPEG;
310 break;
311 case 1:
312 strcpy(f->description, "JPEG");
313 f->pixelformat = V4L2_PIX_FMT_JPEG;
314 break;
315 default:
316 return -EINVAL;
317 }
318
319 return 0;
320}
321
322/******************************************************************************
323 *
324 * ioctl_try_fmt
325 *
326 * V4L2 format try
327 *
328 *****************************************************************************/
329
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300330static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
331 struct v4l2_format *f)
Alan Coxab33d502006-02-27 00:09:05 -0300332{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300333 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300334
335 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
336 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
337 return -EINVAL;
338
339 f->fmt.pix.field = V4L2_FIELD_NONE;
340 f->fmt.pix.bytesperline = 0;
341 f->fmt.pix.sizeimage = cam->frame_size;
342 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
343 f->fmt.pix.priv = 0;
344
345 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
346 case VIDEOSIZE_VGA:
347 f->fmt.pix.width = 640;
348 f->fmt.pix.height = 480;
349 break;
350 case VIDEOSIZE_CIF:
351 f->fmt.pix.width = 352;
352 f->fmt.pix.height = 288;
353 break;
354 case VIDEOSIZE_QVGA:
355 f->fmt.pix.width = 320;
356 f->fmt.pix.height = 240;
357 break;
358 case VIDEOSIZE_288_216:
359 f->fmt.pix.width = 288;
360 f->fmt.pix.height = 216;
361 break;
362 case VIDEOSIZE_256_192:
363 f->fmt.pix.width = 256;
364 f->fmt.pix.height = 192;
365 break;
366 case VIDEOSIZE_224_168:
367 f->fmt.pix.width = 224;
368 f->fmt.pix.height = 168;
369 break;
370 case VIDEOSIZE_192_144:
371 f->fmt.pix.width = 192;
372 f->fmt.pix.height = 144;
373 break;
374 case VIDEOSIZE_QCIF:
375 default:
376 f->fmt.pix.width = 176;
377 f->fmt.pix.height = 144;
378 break;
379 }
380
381 return 0;
382}
383
384/******************************************************************************
385 *
386 * ioctl_set_fmt
387 *
388 * V4L2 format set
389 *
390 *****************************************************************************/
391
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300392static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
393 struct v4l2_format *f)
Alan Coxab33d502006-02-27 00:09:05 -0300394{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300395 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300396 int err, frame;
397
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300398 err = cpia2_try_fmt_vid_cap(file, _fh, f);
Alan Coxab33d502006-02-27 00:09:05 -0300399 if(err != 0)
400 return err;
401
Alan Coxab33d502006-02-27 00:09:05 -0300402 cam->pixelformat = f->fmt.pix.pixelformat;
403
404 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
405 * the missing Huffman table properly. */
406 cam->params.compression.inhibit_htables = 0;
407 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
408
409 /* we set the video window to something smaller or equal to what
410 * is requested by the user???
411 */
412 DBG("Requested width = %d, height = %d\n",
413 f->fmt.pix.width, f->fmt.pix.height);
Hans Verkuil873ecd82010-09-19 06:00:54 -0300414 if (f->fmt.pix.width != cam->width ||
415 f->fmt.pix.height != cam->height) {
416 cam->width = f->fmt.pix.width;
417 cam->height = f->fmt.pix.height;
Alan Coxab33d502006-02-27 00:09:05 -0300418 cam->params.roi.width = f->fmt.pix.width;
419 cam->params.roi.height = f->fmt.pix.height;
420 cpia2_set_format(cam);
421 }
422
423 for (frame = 0; frame < cam->num_frames; ++frame) {
424 if (cam->buffers[frame].status == FRAME_READING)
425 if ((err = sync(cam, frame)) < 0)
426 return err;
427
428 cam->buffers[frame].status = FRAME_EMPTY;
429 }
430
431 return 0;
432}
433
434/******************************************************************************
435 *
436 * ioctl_get_fmt
437 *
438 * V4L2 format get
439 *
440 *****************************************************************************/
441
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300442static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
443 struct v4l2_format *f)
Alan Coxab33d502006-02-27 00:09:05 -0300444{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300445 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300446
Hans Verkuil873ecd82010-09-19 06:00:54 -0300447 f->fmt.pix.width = cam->width;
448 f->fmt.pix.height = cam->height;
Alan Coxab33d502006-02-27 00:09:05 -0300449 f->fmt.pix.pixelformat = cam->pixelformat;
450 f->fmt.pix.field = V4L2_FIELD_NONE;
451 f->fmt.pix.bytesperline = 0;
452 f->fmt.pix.sizeimage = cam->frame_size;
453 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
454 f->fmt.pix.priv = 0;
455
456 return 0;
457}
458
459/******************************************************************************
460 *
461 * ioctl_cropcap
462 *
463 * V4L2 query cropping capabilities
464 * NOTE: cropping is currently disabled
465 *
466 *****************************************************************************/
467
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300468static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
Alan Coxab33d502006-02-27 00:09:05 -0300469{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300470 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300471
472 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
473 return -EINVAL;
474
475 c->bounds.left = 0;
476 c->bounds.top = 0;
Hans Verkuil873ecd82010-09-19 06:00:54 -0300477 c->bounds.width = cam->width;
478 c->bounds.height = cam->height;
Alan Coxab33d502006-02-27 00:09:05 -0300479 c->defrect.left = 0;
480 c->defrect.top = 0;
Hans Verkuil873ecd82010-09-19 06:00:54 -0300481 c->defrect.width = cam->width;
482 c->defrect.height = cam->height;
Alan Coxab33d502006-02-27 00:09:05 -0300483 c->pixelaspect.numerator = 1;
484 c->pixelaspect.denominator = 1;
485
486 return 0;
487}
488
Hans Verkuil6c493f82012-04-29 08:44:44 -0300489struct framerate_info {
490 int value;
491 struct v4l2_fract period;
492};
Alan Coxab33d502006-02-27 00:09:05 -0300493
Hans Verkuil6c493f82012-04-29 08:44:44 -0300494static const struct framerate_info framerate_controls[] = {
495 { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } },
496 { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } },
497 { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } },
498 { CPIA2_VP_FRAMERATE_15, { 1, 15 } },
499 { CPIA2_VP_FRAMERATE_25, { 1, 25 } },
500 { CPIA2_VP_FRAMERATE_30, { 1, 30 } },
501};
502
503static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
Alan Coxab33d502006-02-27 00:09:05 -0300504{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300505 struct camera_data *cam = video_drvdata(file);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300506 struct v4l2_captureparm *cap = &p->parm.capture;
Alan Coxab33d502006-02-27 00:09:05 -0300507 int i;
508
Hans Verkuil6c493f82012-04-29 08:44:44 -0300509 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
510 return -EINVAL;
511
512 cap->capability = V4L2_CAP_TIMEPERFRAME;
513 cap->readbuffers = cam->num_frames;
514 for (i = 0; i < ARRAY_SIZE(framerate_controls); i++)
515 if (cam->params.vp_params.frame_rate == framerate_controls[i].value) {
516 cap->timeperframe = framerate_controls[i].period;
Alan Coxab33d502006-02-27 00:09:05 -0300517 break;
518 }
Hans Verkuil6c493f82012-04-29 08:44:44 -0300519 return 0;
520}
Alan Coxab33d502006-02-27 00:09:05 -0300521
Hans Verkuil6c493f82012-04-29 08:44:44 -0300522static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
523{
524 struct camera_data *cam = video_drvdata(file);
525 struct v4l2_captureparm *cap = &p->parm.capture;
526 struct v4l2_fract tpf = cap->timeperframe;
527 int max = ARRAY_SIZE(framerate_controls) - 1;
528 int ret;
529 int i;
530
531 ret = cpia2_g_parm(file, fh, p);
532 if (ret || !tpf.denominator || !tpf.numerator)
533 return ret;
534
535 /* Maximum 15 fps for this model */
536 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
537 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
538 max -= 2;
539 for (i = 0; i <= max; i++) {
540 struct v4l2_fract f1 = tpf;
541 struct v4l2_fract f2 = framerate_controls[i].period;
542
543 f1.numerator *= f2.denominator;
544 f2.numerator *= f1.denominator;
545 if (f1.numerator >= f2.numerator)
546 break;
547 }
548 if (i > max)
549 i = max;
550 cap->timeperframe = framerate_controls[i].period;
551 return cpia2_set_fps(cam, framerate_controls[i].value);
552}
553
554static const struct {
555 u32 width;
556 u32 height;
557} cpia2_framesizes[] = {
558 { 640, 480 },
559 { 352, 288 },
560 { 320, 240 },
561 { 288, 216 },
562 { 256, 192 },
563 { 224, 168 },
564 { 192, 144 },
565 { 176, 144 },
566};
567
568static int cpia2_enum_framesizes(struct file *file, void *fh,
569 struct v4l2_frmsizeenum *fsize)
570{
571
572 if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG &&
573 fsize->pixel_format != V4L2_PIX_FMT_JPEG)
Alan Coxab33d502006-02-27 00:09:05 -0300574 return -EINVAL;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300575 if (fsize->index >= ARRAY_SIZE(cpia2_framesizes))
576 return -EINVAL;
577 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
578 fsize->discrete.width = cpia2_framesizes[fsize->index].width;
579 fsize->discrete.height = cpia2_framesizes[fsize->index].height;
Alan Coxab33d502006-02-27 00:09:05 -0300580
581 return 0;
582}
583
Hans Verkuil6c493f82012-04-29 08:44:44 -0300584static int cpia2_enum_frameintervals(struct file *file, void *fh,
585 struct v4l2_frmivalenum *fival)
Alan Coxab33d502006-02-27 00:09:05 -0300586{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300587 struct camera_data *cam = video_drvdata(file);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300588 int max = ARRAY_SIZE(framerate_controls) - 1;
589 int i;
Alan Coxab33d502006-02-27 00:09:05 -0300590
Hans Verkuil6c493f82012-04-29 08:44:44 -0300591 if (fival->pixel_format != V4L2_PIX_FMT_MJPEG &&
592 fival->pixel_format != V4L2_PIX_FMT_JPEG)
Alan Coxab33d502006-02-27 00:09:05 -0300593 return -EINVAL;
Alan Coxab33d502006-02-27 00:09:05 -0300594
Hans Verkuil6c493f82012-04-29 08:44:44 -0300595 /* Maximum 15 fps for this model */
596 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
597 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
598 max -= 2;
599 if (fival->index > max)
Alan Coxab33d502006-02-27 00:09:05 -0300600 return -EINVAL;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300601 for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++)
602 if (fival->width == cpia2_framesizes[i].width &&
603 fival->height == cpia2_framesizes[i].height)
604 break;
605 if (i == ARRAY_SIZE(cpia2_framesizes))
Alan Coxab33d502006-02-27 00:09:05 -0300606 return -EINVAL;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300607 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
608 fival->discrete = framerate_controls[fival->index].period;
Alan Coxab33d502006-02-27 00:09:05 -0300609 return 0;
610}
611
612/******************************************************************************
613 *
614 * ioctl_s_ctrl
615 *
616 * V4L2 set the value of a control variable
617 *
618 *****************************************************************************/
619
Hans Verkuil6c493f82012-04-29 08:44:44 -0300620static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl)
Alan Coxab33d502006-02-27 00:09:05 -0300621{
Hans Verkuil6c493f82012-04-29 08:44:44 -0300622 struct camera_data *cam =
623 container_of(ctrl->handler, struct camera_data, hdl);
624 static const int flicker_table[] = {
625 NEVER_FLICKER,
626 FLICKER_50,
627 FLICKER_60,
628 };
Alan Coxab33d502006-02-27 00:09:05 -0300629
Hans Verkuil6c493f82012-04-29 08:44:44 -0300630 DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300631
Hans Verkuil6c493f82012-04-29 08:44:44 -0300632 switch (ctrl->id) {
Alan Coxab33d502006-02-27 00:09:05 -0300633 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300634 cpia2_set_brightness(cam, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300635 break;
636 case V4L2_CID_CONTRAST:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300637 cpia2_set_contrast(cam, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300638 break;
639 case V4L2_CID_SATURATION:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300640 cpia2_set_saturation(cam, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300641 break;
642 case V4L2_CID_HFLIP:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300643 cpia2_set_property_mirror(cam, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300644 break;
645 case V4L2_CID_VFLIP:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300646 cpia2_set_property_flip(cam, ctrl->val);
Alan Coxab33d502006-02-27 00:09:05 -0300647 break;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300648 case V4L2_CID_POWER_LINE_FREQUENCY:
649 return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]);
650 case V4L2_CID_ILLUMINATORS_1:
651 return cpia2_set_gpio(cam, (cam->top_light->val << 6) |
652 (cam->bottom_light->val << 7));
653 case V4L2_CID_JPEG_ACTIVE_MARKER:
654 cam->params.compression.inhibit_htables =
655 !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT);
Alan Coxab33d502006-02-27 00:09:05 -0300656 break;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300657 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
658 cam->params.vc_params.quality = ctrl->val;
Alan Coxab33d502006-02-27 00:09:05 -0300659 break;
660 case CPIA2_CID_USB_ALT:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300661 cam->params.camera_state.stream_mode = ctrl->val;
Alan Coxab33d502006-02-27 00:09:05 -0300662 break;
663 default:
Hans Verkuil6c493f82012-04-29 08:44:44 -0300664 return -EINVAL;
Alan Coxab33d502006-02-27 00:09:05 -0300665 }
666
Hans Verkuil6c493f82012-04-29 08:44:44 -0300667 return 0;
Alan Coxab33d502006-02-27 00:09:05 -0300668}
669
670/******************************************************************************
671 *
672 * ioctl_g_jpegcomp
673 *
674 * V4L2 get the JPEG compression parameters
675 *
676 *****************************************************************************/
677
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300678static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
Alan Coxab33d502006-02-27 00:09:05 -0300679{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300680 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300681
682 memset(parms, 0, sizeof(*parms));
683
684 parms->quality = 80; // TODO: Can this be made meaningful?
685
686 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
687 if(!cam->params.compression.inhibit_htables) {
688 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
689 }
690
691 parms->APPn = cam->APPn;
692 parms->APP_len = cam->APP_len;
693 if(cam->APP_len > 0) {
694 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
695 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
696 }
697
698 parms->COM_len = cam->COM_len;
699 if(cam->COM_len > 0) {
700 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
701 parms->jpeg_markers |= JPEG_MARKER_COM;
702 }
703
704 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
705 parms->APP_len, parms->COM_len);
706
707 return 0;
708}
709
710/******************************************************************************
711 *
712 * ioctl_s_jpegcomp
713 *
714 * V4L2 set the JPEG compression parameters
715 * NOTE: quality and some jpeg_markers are ignored.
716 *
717 *****************************************************************************/
718
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300719static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
Alan Coxab33d502006-02-27 00:09:05 -0300720{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300721 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300722
723 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
724 parms->APP_len, parms->COM_len);
725
726 cam->params.compression.inhibit_htables =
727 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300728 parms->jpeg_markers &= V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI |
729 V4L2_JPEG_MARKER_DHT;
Alan Coxab33d502006-02-27 00:09:05 -0300730
731 if(parms->APP_len != 0) {
732 if(parms->APP_len > 0 &&
733 parms->APP_len <= sizeof(cam->APP_data) &&
734 parms->APPn >= 0 && parms->APPn <= 15) {
735 cam->APPn = parms->APPn;
736 cam->APP_len = parms->APP_len;
737 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
738 } else {
739 LOG("Bad APPn Params n=%d len=%d\n",
740 parms->APPn, parms->APP_len);
741 return -EINVAL;
742 }
743 } else {
744 cam->APP_len = 0;
745 }
746
747 if(parms->COM_len != 0) {
748 if(parms->COM_len > 0 &&
749 parms->COM_len <= sizeof(cam->COM_data)) {
750 cam->COM_len = parms->COM_len;
751 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
752 } else {
753 LOG("Bad COM_len=%d\n", parms->COM_len);
754 return -EINVAL;
755 }
756 }
757
758 return 0;
759}
760
761/******************************************************************************
762 *
763 * ioctl_reqbufs
764 *
765 * V4L2 Initiate memory mapping.
766 * NOTE: The user's request is ignored. For now the buffers are fixed.
767 *
768 *****************************************************************************/
769
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300770static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
Alan Coxab33d502006-02-27 00:09:05 -0300771{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300772 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300773
774 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
775 req->memory != V4L2_MEMORY_MMAP)
776 return -EINVAL;
777
778 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
779 req->count = cam->num_frames;
780 memset(&req->reserved, 0, sizeof(req->reserved));
781
782 return 0;
783}
784
785/******************************************************************************
786 *
787 * ioctl_querybuf
788 *
789 * V4L2 Query memory buffer status.
790 *
791 *****************************************************************************/
792
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300793static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
Alan Coxab33d502006-02-27 00:09:05 -0300794{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300795 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300796
797 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
798 buf->index > cam->num_frames)
799 return -EINVAL;
800
801 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
802 buf->length = cam->frame_size;
803
804 buf->memory = V4L2_MEMORY_MMAP;
805
806 if(cam->mmapped)
807 buf->flags = V4L2_BUF_FLAG_MAPPED;
808 else
809 buf->flags = 0;
810
811 switch (cam->buffers[buf->index].status) {
812 case FRAME_EMPTY:
813 case FRAME_ERROR:
814 case FRAME_READING:
815 buf->bytesused = 0;
816 buf->flags = V4L2_BUF_FLAG_QUEUED;
817 break;
818 case FRAME_READY:
819 buf->bytesused = cam->buffers[buf->index].length;
820 buf->timestamp = cam->buffers[buf->index].timestamp;
821 buf->sequence = cam->buffers[buf->index].seq;
822 buf->flags = V4L2_BUF_FLAG_DONE;
823 break;
824 }
825
826 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
827 buf->index, buf->m.offset, buf->flags, buf->sequence,
828 buf->bytesused);
829
830 return 0;
831}
832
833/******************************************************************************
834 *
835 * ioctl_qbuf
836 *
837 * V4L2 User is freeing buffer
838 *
839 *****************************************************************************/
840
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300841static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
Alan Coxab33d502006-02-27 00:09:05 -0300842{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300843 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300844
845 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
846 buf->memory != V4L2_MEMORY_MMAP ||
847 buf->index > cam->num_frames)
848 return -EINVAL;
849
850 DBG("QBUF #%d\n", buf->index);
851
852 if(cam->buffers[buf->index].status == FRAME_READY)
853 cam->buffers[buf->index].status = FRAME_EMPTY;
854
855 return 0;
856}
857
858/******************************************************************************
859 *
860 * find_earliest_filled_buffer
861 *
862 * Helper for ioctl_dqbuf. Find the next ready buffer.
863 *
864 *****************************************************************************/
865
866static int find_earliest_filled_buffer(struct camera_data *cam)
867{
868 int i;
869 int found = -1;
870 for (i=0; i<cam->num_frames; i++) {
871 if(cam->buffers[i].status == FRAME_READY) {
872 if(found < 0) {
873 found = i;
874 } else {
875 /* find which buffer is earlier */
876 struct timeval *tv1, *tv2;
877 tv1 = &cam->buffers[i].timestamp;
878 tv2 = &cam->buffers[found].timestamp;
879 if(tv1->tv_sec < tv2->tv_sec ||
880 (tv1->tv_sec == tv2->tv_sec &&
881 tv1->tv_usec < tv2->tv_usec))
882 found = i;
883 }
884 }
885 }
886 return found;
887}
888
889/******************************************************************************
890 *
891 * ioctl_dqbuf
892 *
893 * V4L2 User is asking for a filled buffer.
894 *
895 *****************************************************************************/
896
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300897static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
Alan Coxab33d502006-02-27 00:09:05 -0300898{
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300899 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300900 int frame;
901
902 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
903 buf->memory != V4L2_MEMORY_MMAP)
904 return -EINVAL;
905
906 frame = find_earliest_filled_buffer(cam);
907
908 if(frame < 0 && file->f_flags&O_NONBLOCK)
909 return -EAGAIN;
910
911 if(frame < 0) {
912 /* Wait for a frame to become available */
913 struct framebuf *cb=cam->curbuff;
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300914 mutex_unlock(&cam->v4l2_lock);
Alan Coxab33d502006-02-27 00:09:05 -0300915 wait_event_interruptible(cam->wq_stream,
Hans Verkuil6c493f82012-04-29 08:44:44 -0300916 !video_is_registered(&cam->vdev) ||
Alan Coxab33d502006-02-27 00:09:05 -0300917 (cb=cam->curbuff)->status == FRAME_READY);
Hans Verkuild2db8fe2010-12-18 09:50:43 -0300918 mutex_lock(&cam->v4l2_lock);
Alan Coxab33d502006-02-27 00:09:05 -0300919 if (signal_pending(current))
920 return -ERESTARTSYS;
Hans Verkuil6c493f82012-04-29 08:44:44 -0300921 if (!video_is_registered(&cam->vdev))
Alan Coxab33d502006-02-27 00:09:05 -0300922 return -ENOTTY;
923 frame = cb->num;
924 }
925
926
927 buf->index = frame;
928 buf->bytesused = cam->buffers[buf->index].length;
929 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
930 buf->field = V4L2_FIELD_NONE;
931 buf->timestamp = cam->buffers[buf->index].timestamp;
932 buf->sequence = cam->buffers[buf->index].seq;
933 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
934 buf->length = cam->frame_size;
Sakari Ailus2b719d72012-05-02 09:40:03 -0300935 buf->reserved2 = 0;
Alan Coxab33d502006-02-27 00:09:05 -0300936 buf->reserved = 0;
937 memset(&buf->timecode, 0, sizeof(buf->timecode));
938
939 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
940 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
941
942 return 0;
943}
944
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300945static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
946{
947 struct camera_data *cam = video_drvdata(file);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300948 int ret = -EINVAL;
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300949
950 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
951 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
952 return -EINVAL;
953
Hans Verkuil6c493f82012-04-29 08:44:44 -0300954 if (!cam->streaming) {
955 ret = cpia2_usb_stream_start(cam,
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300956 cam->params.camera_state.stream_mode);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300957 if (!ret)
958 v4l2_ctrl_grab(cam->usb_alt, true);
959 }
960 return ret;
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300961}
962
963static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
964{
965 struct camera_data *cam = video_drvdata(file);
Hans Verkuil6c493f82012-04-29 08:44:44 -0300966 int ret = -EINVAL;
Hans Verkuilcbfb3da2011-01-22 06:36:02 -0300967
968 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
969 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
970 return -EINVAL;
971
Hans Verkuil6c493f82012-04-29 08:44:44 -0300972 if (cam->streaming) {
973 ret = cpia2_usb_stream_stop(cam);
974 if (!ret)
975 v4l2_ctrl_grab(cam->usb_alt, false);
976 }
977 return ret;
Alan Coxab33d502006-02-27 00:09:05 -0300978}
979
980/******************************************************************************
981 *
982 * cpia2_mmap
983 *
984 *****************************************************************************/
985static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
986{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300987 struct camera_data *cam = video_drvdata(file);
Alan Coxab33d502006-02-27 00:09:05 -0300988 int retval;
Alan Coxab33d502006-02-27 00:09:05 -0300989
Alan Coxab33d502006-02-27 00:09:05 -0300990 retval = cpia2_remap_buffer(cam, area);
991
992 if(!retval)
Hans Verkuil6c493f82012-04-29 08:44:44 -0300993 cam->stream_fh = file->private_data;
Alan Coxab33d502006-02-27 00:09:05 -0300994 return retval;
995}
996
997/******************************************************************************
998 *
999 * reset_camera_struct_v4l
1000 *
1001 * Sets all values to the defaults
1002 *****************************************************************************/
1003static void reset_camera_struct_v4l(struct camera_data *cam)
1004{
Hans Verkuil873ecd82010-09-19 06:00:54 -03001005 cam->width = cam->params.roi.width;
1006 cam->height = cam->params.roi.height;
Alan Coxab33d502006-02-27 00:09:05 -03001007
1008 cam->frame_size = buffer_size;
1009 cam->num_frames = num_buffers;
1010
Hans Verkuil6c493f82012-04-29 08:44:44 -03001011 /* Flicker modes */
Alan Coxab33d502006-02-27 00:09:05 -03001012 cam->params.flicker_control.flicker_mode_req = flicker_mode;
Alan Coxab33d502006-02-27 00:09:05 -03001013
Hans Verkuil6c493f82012-04-29 08:44:44 -03001014 /* stream modes */
Alan Coxab33d502006-02-27 00:09:05 -03001015 cam->params.camera_state.stream_mode = alternate;
1016
1017 cam->pixelformat = V4L2_PIX_FMT_JPEG;
Alan Coxab33d502006-02-27 00:09:05 -03001018}
1019
Hans Verkuilcbfb3da2011-01-22 06:36:02 -03001020static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1021 .vidioc_querycap = cpia2_querycap,
1022 .vidioc_enum_input = cpia2_enum_input,
1023 .vidioc_g_input = cpia2_g_input,
1024 .vidioc_s_input = cpia2_s_input,
1025 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1026 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1027 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1028 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
Hans Verkuilcbfb3da2011-01-22 06:36:02 -03001029 .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1030 .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1031 .vidioc_cropcap = cpia2_cropcap,
1032 .vidioc_reqbufs = cpia2_reqbufs,
1033 .vidioc_querybuf = cpia2_querybuf,
1034 .vidioc_qbuf = cpia2_qbuf,
1035 .vidioc_dqbuf = cpia2_dqbuf,
1036 .vidioc_streamon = cpia2_streamon,
1037 .vidioc_streamoff = cpia2_streamoff,
Hans Verkuil6c493f82012-04-29 08:44:44 -03001038 .vidioc_s_parm = cpia2_s_parm,
1039 .vidioc_g_parm = cpia2_g_parm,
1040 .vidioc_enum_framesizes = cpia2_enum_framesizes,
1041 .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1042 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1043 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilcbfb3da2011-01-22 06:36:02 -03001044};
1045
Alan Coxab33d502006-02-27 00:09:05 -03001046/***
1047 * The v4l video device structure initialized for this device
1048 ***/
Hans Verkuil873ecd82010-09-19 06:00:54 -03001049static const struct v4l2_file_operations cpia2_fops = {
Alan Cox2ae15192006-03-20 13:59:42 -03001050 .owner = THIS_MODULE,
1051 .open = cpia2_open,
1052 .release = cpia2_close,
1053 .read = cpia2_v4l_read,
1054 .poll = cpia2_v4l_poll,
Hans Verkuilcbfb3da2011-01-22 06:36:02 -03001055 .unlocked_ioctl = video_ioctl2,
Alan Cox2ae15192006-03-20 13:59:42 -03001056 .mmap = cpia2_mmap,
Alan Coxab33d502006-02-27 00:09:05 -03001057};
1058
1059static struct video_device cpia2_template = {
1060 /* I could not find any place for the old .initialize initializer?? */
Hans Verkuil873ecd82010-09-19 06:00:54 -03001061 .name = "CPiA2 Camera",
1062 .fops = &cpia2_fops,
Hans Verkuilcbfb3da2011-01-22 06:36:02 -03001063 .ioctl_ops = &cpia2_ioctl_ops,
Hans Verkuil6c493f82012-04-29 08:44:44 -03001064 .release = video_device_release_empty,
1065};
1066
1067void cpia2_camera_release(struct v4l2_device *v4l2_dev)
1068{
1069 struct camera_data *cam =
1070 container_of(v4l2_dev, struct camera_data, v4l2_dev);
1071
1072 v4l2_ctrl_handler_free(&cam->hdl);
1073 v4l2_device_unregister(&cam->v4l2_dev);
1074 kfree(cam);
1075}
1076
1077static const struct v4l2_ctrl_ops cpia2_ctrl_ops = {
1078 .s_ctrl = cpia2_s_ctrl,
Alan Coxab33d502006-02-27 00:09:05 -03001079};
1080
1081/******************************************************************************
1082 *
1083 * cpia2_register_camera
1084 *
1085 *****************************************************************************/
1086int cpia2_register_camera(struct camera_data *cam)
1087{
Hans Verkuil6c493f82012-04-29 08:44:44 -03001088 struct v4l2_ctrl_handler *hdl = &cam->hdl;
1089 struct v4l2_ctrl_config cpia2_usb_alt = {
1090 .ops = &cpia2_ctrl_ops,
1091 .id = CPIA2_CID_USB_ALT,
1092 .name = "USB Alternate",
1093 .type = V4L2_CTRL_TYPE_INTEGER,
1094 .min = USBIF_ISO_1,
1095 .max = USBIF_ISO_6,
1096 .step = 1,
1097 };
1098 int ret;
Alan Coxab33d502006-02-27 00:09:05 -03001099
Hans Verkuil6c493f82012-04-29 08:44:44 -03001100 v4l2_ctrl_handler_init(hdl, 12);
1101 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1102 V4L2_CID_BRIGHTNESS,
1103 cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0,
1104 255, 1, DEFAULT_BRIGHTNESS);
1105 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1106 V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST);
1107 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1108 V4L2_CID_SATURATION, 0, 255, 1, DEFAULT_SATURATION);
1109 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1110 V4L2_CID_HFLIP, 0, 1, 1, 0);
1111 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1112 V4L2_CID_JPEG_ACTIVE_MARKER, 0,
1113 V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
1114 V4L2_JPEG_ACTIVE_MARKER_DHT);
1115 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1116 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1,
1117 100, 1, 100);
1118 cpia2_usb_alt.def = alternate;
1119 cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL);
1120 /* VP5 Only */
1121 if (cam->params.pnp_id.device_type != DEVICE_STV_672)
1122 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1123 V4L2_CID_VFLIP, 0, 1, 1, 0);
1124 /* Flicker control only valid for 672 */
1125 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1126 v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops,
1127 V4L2_CID_POWER_LINE_FREQUENCY,
1128 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1129 /* Light control only valid for the QX5 Microscope */
1130 if (cam->params.pnp_id.product == 0x151) {
1131 cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1132 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
1133 cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1134 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
1135 v4l2_ctrl_cluster(2, &cam->top_light);
1136 }
1137
1138 if (hdl->error) {
1139 ret = hdl->error;
1140 v4l2_ctrl_handler_free(hdl);
1141 return ret;
1142 }
1143
1144 cam->vdev = cpia2_template;
1145 video_set_drvdata(&cam->vdev, cam);
1146 cam->vdev.lock = &cam->v4l2_lock;
1147 cam->vdev.ctrl_handler = hdl;
1148 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1149 set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
Hans Verkuil5126f252012-05-10 04:57:22 -03001150 /* Locking in file operations other than ioctl should be done
1151 by the driver, not the V4L2 core.
1152 This driver needs auditing so that this flag can be removed. */
1153 set_bit(V4L2_FL_LOCK_ALL_FOPS, &cam->vdev.flags);
Alan Coxab33d502006-02-27 00:09:05 -03001154
1155 reset_camera_struct_v4l(cam);
1156
1157 /* register v4l device */
Hans Verkuil6c493f82012-04-29 08:44:44 -03001158 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
Alan Coxab33d502006-02-27 00:09:05 -03001159 ERR("video_register_device failed\n");
Alan Coxab33d502006-02-27 00:09:05 -03001160 return -ENODEV;
1161 }
1162
1163 return 0;
1164}
1165
1166/******************************************************************************
1167 *
1168 * cpia2_unregister_camera
1169 *
1170 *****************************************************************************/
1171void cpia2_unregister_camera(struct camera_data *cam)
1172{
Hans Verkuil6c493f82012-04-29 08:44:44 -03001173 video_unregister_device(&cam->vdev);
Alan Coxab33d502006-02-27 00:09:05 -03001174}
1175
1176/******************************************************************************
1177 *
1178 * check_parameters
1179 *
1180 * Make sure that all user-supplied parameters are sensible
1181 *****************************************************************************/
1182static void __init check_parameters(void)
1183{
1184 if(buffer_size < PAGE_SIZE) {
1185 buffer_size = PAGE_SIZE;
1186 LOG("buffer_size too small, setting to %d\n", buffer_size);
1187 } else if(buffer_size > 1024*1024) {
1188 /* arbitrary upper limiit */
1189 buffer_size = 1024*1024;
1190 LOG("buffer_size ridiculously large, setting to %d\n",
1191 buffer_size);
1192 } else {
1193 buffer_size += PAGE_SIZE-1;
1194 buffer_size &= ~(PAGE_SIZE-1);
1195 }
1196
1197 if(num_buffers < 1) {
1198 num_buffers = 1;
1199 LOG("num_buffers too small, setting to %d\n", num_buffers);
1200 } else if(num_buffers > VIDEO_MAX_FRAME) {
1201 num_buffers = VIDEO_MAX_FRAME;
1202 LOG("num_buffers too large, setting to %d\n", num_buffers);
1203 }
1204
1205 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1206 alternate = DEFAULT_ALT;
1207 LOG("alternate specified is invalid, using %d\n", alternate);
1208 }
1209
Hans Verkuil6c493f82012-04-29 08:44:44 -03001210 if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) {
1211 flicker_mode = 0;
Alan Coxab33d502006-02-27 00:09:05 -03001212 LOG("Flicker mode specified is invalid, using %d\n",
1213 flicker_mode);
1214 }
1215
Alan Coxab33d502006-02-27 00:09:05 -03001216 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1217 num_buffers, buffer_size, alternate);
1218}
1219
1220/************ Module Stuff ***************/
1221
1222
1223/******************************************************************************
1224 *
1225 * cpia2_init/module_init
1226 *
1227 *****************************************************************************/
Adrian Bunk8cbe84f2006-02-28 04:40:51 -03001228static int __init cpia2_init(void)
Alan Coxab33d502006-02-27 00:09:05 -03001229{
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03001230 LOG("%s v%s\n",
1231 ABOUT, CPIA_VERSION);
Alan Coxab33d502006-02-27 00:09:05 -03001232 check_parameters();
1233 cpia2_usb_init();
1234 return 0;
1235}
1236
1237
1238/******************************************************************************
1239 *
1240 * cpia2_exit/module_exit
1241 *
1242 *****************************************************************************/
Adrian Bunk8cbe84f2006-02-28 04:40:51 -03001243static void __exit cpia2_exit(void)
Alan Coxab33d502006-02-27 00:09:05 -03001244{
1245 cpia2_usb_cleanup();
1246 schedule_timeout(2 * HZ);
1247}
1248
Alan Coxab33d502006-02-27 00:09:05 -03001249module_init(cpia2_init);
1250module_exit(cpia2_exit);