blob: 8a7e52c3b8f5cf7301ec46e7cc75a2de198e2c6d [file] [log] [blame]
Luc Saillard2b455db2006-04-24 10:29:46 -03001/* Linux driver for Philips webcam
2 USB and Video4Linux interface part.
3 (C) 1999-2004 Nemosoft Unv.
4 (C) 2004-2006 Luc Saillard (luc@saillard.org)
5
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26*/
27
28#include <linux/errno.h>
29#include <linux/init.h>
30#include <linux/mm.h>
31#include <linux/module.h>
32#include <linux/poll.h>
Luc Saillard2b455db2006-04-24 10:29:46 -030033#include <linux/vmalloc.h>
34#include <asm/io.h>
35
36#include "pwc.h"
37
38static struct v4l2_queryctrl pwc_controls[] = {
39 {
40 .id = V4L2_CID_BRIGHTNESS,
41 .type = V4L2_CTRL_TYPE_INTEGER,
42 .name = "Brightness",
43 .minimum = 0,
44 .maximum = 128,
45 .step = 1,
46 .default_value = 64,
47 },
48 {
49 .id = V4L2_CID_CONTRAST,
50 .type = V4L2_CTRL_TYPE_INTEGER,
51 .name = "Contrast",
52 .minimum = 0,
53 .maximum = 64,
54 .step = 1,
55 .default_value = 0,
56 },
57 {
58 .id = V4L2_CID_SATURATION,
59 .type = V4L2_CTRL_TYPE_INTEGER,
60 .name = "Saturation",
61 .minimum = -100,
62 .maximum = 100,
63 .step = 1,
64 .default_value = 0,
65 },
66 {
67 .id = V4L2_CID_GAMMA,
68 .type = V4L2_CTRL_TYPE_INTEGER,
69 .name = "Gamma",
70 .minimum = 0,
71 .maximum = 32,
72 .step = 1,
73 .default_value = 0,
74 },
75 {
76 .id = V4L2_CID_RED_BALANCE,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Red Gain",
79 .minimum = 0,
80 .maximum = 256,
81 .step = 1,
82 .default_value = 0,
83 },
84 {
85 .id = V4L2_CID_BLUE_BALANCE,
86 .type = V4L2_CTRL_TYPE_INTEGER,
87 .name = "Blue Gain",
88 .minimum = 0,
89 .maximum = 256,
90 .step = 1,
91 .default_value = 0,
92 },
93 {
94 .id = V4L2_CID_AUTO_WHITE_BALANCE,
95 .type = V4L2_CTRL_TYPE_BOOLEAN,
96 .name = "Auto White Balance",
97 .minimum = 0,
98 .maximum = 1,
99 .step = 1,
100 .default_value = 0,
101 },
102 {
103 .id = V4L2_CID_EXPOSURE,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Shutter Speed (Exposure)",
106 .minimum = 0,
107 .maximum = 256,
108 .step = 1,
109 .default_value = 200,
110 },
111 {
112 .id = V4L2_CID_AUTOGAIN,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Auto Gain Enabled",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 },
120 {
121 .id = V4L2_CID_GAIN,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 .name = "Gain Level",
124 .minimum = 0,
125 .maximum = 256,
126 .step = 1,
127 .default_value = 0,
128 },
Luc Saillard2b455db2006-04-24 10:29:46 -0300129 {
130 .id = V4L2_CID_PRIVATE_SAVE_USER,
131 .type = V4L2_CTRL_TYPE_BUTTON,
132 .name = "Save User Settings",
133 .minimum = 0,
134 .maximum = 0,
135 .step = 0,
136 .default_value = 0,
137 },
138 {
139 .id = V4L2_CID_PRIVATE_RESTORE_USER,
140 .type = V4L2_CTRL_TYPE_BUTTON,
141 .name = "Restore User Settings",
142 .minimum = 0,
143 .maximum = 0,
144 .step = 0,
145 .default_value = 0,
146 },
147 {
148 .id = V4L2_CID_PRIVATE_RESTORE_FACTORY,
149 .type = V4L2_CTRL_TYPE_BUTTON,
150 .name = "Restore Factory Settings",
151 .minimum = 0,
152 .maximum = 0,
153 .step = 0,
154 .default_value = 0,
155 },
156 {
157 .id = V4L2_CID_PRIVATE_COLOUR_MODE,
158 .type = V4L2_CTRL_TYPE_BOOLEAN,
159 .name = "Colour mode",
160 .minimum = 0,
161 .maximum = 1,
162 .step = 1,
163 .default_value = 0,
164 },
165 {
166 .id = V4L2_CID_PRIVATE_AUTOCONTOUR,
167 .type = V4L2_CTRL_TYPE_BOOLEAN,
168 .name = "Auto contour",
169 .minimum = 0,
170 .maximum = 1,
171 .step = 1,
172 .default_value = 0,
173 },
174 {
175 .id = V4L2_CID_PRIVATE_CONTOUR,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Contour",
178 .minimum = 0,
179 .maximum = 63,
180 .step = 1,
181 .default_value = 0,
182 },
183 {
184 .id = V4L2_CID_PRIVATE_BACKLIGHT,
185 .type = V4L2_CTRL_TYPE_BOOLEAN,
186 .name = "Backlight compensation",
187 .minimum = 0,
188 .maximum = 1,
189 .step = 1,
190 .default_value = 0,
191 },
192 {
193 .id = V4L2_CID_PRIVATE_FLICKERLESS,
194 .type = V4L2_CTRL_TYPE_BOOLEAN,
195 .name = "Flickerless",
196 .minimum = 0,
197 .maximum = 1,
198 .step = 1,
199 .default_value = 0,
200 },
201 {
202 .id = V4L2_CID_PRIVATE_NOISE_REDUCTION,
203 .type = V4L2_CTRL_TYPE_INTEGER,
204 .name = "Noise reduction",
205 .minimum = 0,
206 .maximum = 3,
207 .step = 1,
208 .default_value = 0,
209 },
Luc Saillard2b455db2006-04-24 10:29:46 -0300210};
211
Luc Saillard2b455db2006-04-24 10:29:46 -0300212
213static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
214{
215 memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
216 f->fmt.pix.width = pdev->view.x;
217 f->fmt.pix.height = pdev->view.y;
218 f->fmt.pix.field = V4L2_FIELD_NONE;
Hans Verkuil479567c2010-09-12 17:05:11 -0300219 if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300220 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
221 f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
222 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
223 } else {
224 /* vbandlength contains 4 lines ... */
225 f->fmt.pix.bytesperline = pdev->vbandlength/4;
226 f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
227 if (DEVICE_USE_CODEC1(pdev->type))
228 f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1;
229 else
230 f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
231 }
232 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
233 "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
234 f->fmt.pix.width,
235 f->fmt.pix.height,
236 f->fmt.pix.bytesperline,
237 f->fmt.pix.sizeimage,
238 (f->fmt.pix.pixelformat)&255,
239 (f->fmt.pix.pixelformat>>8)&255,
240 (f->fmt.pix.pixelformat>>16)&255,
241 (f->fmt.pix.pixelformat>>24)&255);
242}
243
244/* ioctl(VIDIOC_TRY_FMT) */
245static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
246{
247 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
248 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
249 return -EINVAL;
250 }
251
252 switch (f->fmt.pix.pixelformat) {
253 case V4L2_PIX_FMT_YUV420:
254 break;
255 case V4L2_PIX_FMT_PWC1:
256 if (DEVICE_USE_CODEC23(pdev->type)) {
257 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
258 return -EINVAL;
259 }
260 break;
261 case V4L2_PIX_FMT_PWC2:
262 if (DEVICE_USE_CODEC1(pdev->type)) {
263 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
264 return -EINVAL;
265 }
266 break;
267 default:
268 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
269 return -EINVAL;
270
271 }
272
273 if (f->fmt.pix.width > pdev->view_max.x)
274 f->fmt.pix.width = pdev->view_max.x;
275 else if (f->fmt.pix.width < pdev->view_min.x)
276 f->fmt.pix.width = pdev->view_min.x;
277
278 if (f->fmt.pix.height > pdev->view_max.y)
279 f->fmt.pix.height = pdev->view_max.y;
280 else if (f->fmt.pix.height < pdev->view_min.y)
281 f->fmt.pix.height = pdev->view_min.y;
282
283 return 0;
284}
285
286/* ioctl(VIDIOC_SET_FMT) */
287static int pwc_vidioc_set_fmt(struct pwc_device *pdev, struct v4l2_format *f)
288{
289 int ret, fps, snapshot, compression, pixelformat;
290
291 ret = pwc_vidioc_try_fmt(pdev, f);
292 if (ret<0)
293 return ret;
294
295 pixelformat = f->fmt.pix.pixelformat;
296 compression = pdev->vcompression;
297 snapshot = 0;
298 fps = pdev->vframes;
299 if (f->fmt.pix.priv) {
300 compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
301 snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
302 fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
303 if (fps == 0)
304 fps = pdev->vframes;
305 }
306
Hans Verkuil479567c2010-09-12 17:05:11 -0300307 if (pixelformat != V4L2_PIX_FMT_YUV420 &&
308 pixelformat != V4L2_PIX_FMT_PWC1 &&
309 pixelformat != V4L2_PIX_FMT_PWC2)
310 return -EINVAL;
Luc Saillard2b455db2006-04-24 10:29:46 -0300311
Hans de Goede3751e282010-11-16 11:39:25 -0300312 if (pdev->iso_init)
313 return -EBUSY;
314
315 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
Luc Saillard2b455db2006-04-24 10:29:46 -0300316 "compression=%d snapshot=%d format=%c%c%c%c\n",
317 f->fmt.pix.width, f->fmt.pix.height, fps,
318 compression, snapshot,
319 (pixelformat)&255,
320 (pixelformat>>8)&255,
321 (pixelformat>>16)&255,
322 (pixelformat>>24)&255);
323
Hans de Goede3751e282010-11-16 11:39:25 -0300324 ret = pwc_set_video_mode(pdev,
Luc Saillard2b455db2006-04-24 10:29:46 -0300325 f->fmt.pix.width,
326 f->fmt.pix.height,
327 fps,
328 compression,
329 snapshot);
330
Hans de Goede3751e282010-11-16 11:39:25 -0300331 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
Luc Saillard2b455db2006-04-24 10:29:46 -0300332
333 if (ret)
334 return ret;
335
Hans Verkuil479567c2010-09-12 17:05:11 -0300336 pdev->pixfmt = pixelformat;
337
Luc Saillard2b455db2006-04-24 10:29:46 -0300338 pwc_vidioc_fill_fmt(pdev, f);
339
340 return 0;
341
342}
343
Hans Verkuil069b7472008-12-30 07:04:34 -0300344long pwc_video_do_ioctl(struct file *file, unsigned int cmd, void *arg)
Luc Saillard2b455db2006-04-24 10:29:46 -0300345{
346 struct video_device *vdev = video_devdata(file);
347 struct pwc_device *pdev;
348 DECLARE_WAITQUEUE(wait, current);
349
350 if (vdev == NULL)
351 return -EFAULT;
Hans Verkuil601e9442008-08-23 07:24:07 -0300352 pdev = video_get_drvdata(vdev);
Luc Saillard2b455db2006-04-24 10:29:46 -0300353 if (pdev == NULL)
354 return -EFAULT;
355
Trent Piepho05ad3902007-01-30 23:26:01 -0300356#ifdef CONFIG_USB_PWC_DEBUG
Mauro Carvalho Chehab5e28e0092008-04-13 15:06:24 -0300357 if (PWC_DEBUG_LEVEL_IOCTL & pwc_trace) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300358 v4l_printk_ioctl(cmd);
Mauro Carvalho Chehab5e28e0092008-04-13 15:06:24 -0300359 printk("\n");
360 }
Luc Saillard2b455db2006-04-24 10:29:46 -0300361#endif
362
363
364 switch (cmd) {
Trent Piepho657de3c2006-06-20 00:30:57 -0300365 /* V4L2 Layer */
366 case VIDIOC_QUERYCAP:
367 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300368 struct v4l2_capability *cap = arg;
369
370 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCAP) This application "\
371 "try to use the v4l2 layer\n");
372 strcpy(cap->driver,PWC_NAME);
373 strlcpy(cap->card, vdev->name, sizeof(cap->card));
374 usb_make_path(pdev->udev,cap->bus_info,sizeof(cap->bus_info));
375 cap->version = PWC_VERSION_CODE;
376 cap->capabilities =
377 V4L2_CAP_VIDEO_CAPTURE |
378 V4L2_CAP_STREAMING |
379 V4L2_CAP_READWRITE;
380 return 0;
381 }
382
Trent Piepho657de3c2006-06-20 00:30:57 -0300383 case VIDIOC_ENUMINPUT:
384 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300385 struct v4l2_input *i = arg;
386
387 if ( i->index ) /* Only one INPUT is supported */
388 return -EINVAL;
389
390 memset(i, 0, sizeof(struct v4l2_input));
391 strcpy(i->name, "usb");
392 return 0;
393 }
394
Trent Piepho657de3c2006-06-20 00:30:57 -0300395 case VIDIOC_G_INPUT:
Luc Saillard2b455db2006-04-24 10:29:46 -0300396 {
397 int *i = arg;
398 *i = 0; /* Only one INPUT is supported */
399 return 0;
400 }
Trent Piepho657de3c2006-06-20 00:30:57 -0300401 case VIDIOC_S_INPUT:
402 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300403 int *i = arg;
404
405 if ( *i ) { /* Only one INPUT is supported */
406 PWC_DEBUG_IOCTL("Only one input source is"\
407 " supported with this webcam.\n");
408 return -EINVAL;
409 }
410 return 0;
411 }
412
413 /* TODO: */
Trent Piepho657de3c2006-06-20 00:30:57 -0300414 case VIDIOC_QUERYCTRL:
Luc Saillard2b455db2006-04-24 10:29:46 -0300415 {
416 struct v4l2_queryctrl *c = arg;
417 int i;
418
419 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) query id=%d\n", c->id);
420 for (i=0; i<sizeof(pwc_controls)/sizeof(struct v4l2_queryctrl); i++) {
421 if (pwc_controls[i].id == c->id) {
422 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
423 memcpy(c,&pwc_controls[i],sizeof(struct v4l2_queryctrl));
424 return 0;
425 }
426 }
427 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) not found\n");
428
429 return -EINVAL;
430 }
431 case VIDIOC_G_CTRL:
432 {
433 struct v4l2_control *c = arg;
434 int ret;
435
436 switch (c->id)
437 {
438 case V4L2_CID_BRIGHTNESS:
439 c->value = pwc_get_brightness(pdev);
440 if (c->value<0)
441 return -EINVAL;
442 return 0;
443 case V4L2_CID_CONTRAST:
444 c->value = pwc_get_contrast(pdev);
445 if (c->value<0)
446 return -EINVAL;
447 return 0;
448 case V4L2_CID_SATURATION:
449 ret = pwc_get_saturation(pdev, &c->value);
450 if (ret<0)
451 return -EINVAL;
452 return 0;
453 case V4L2_CID_GAMMA:
454 c->value = pwc_get_gamma(pdev);
455 if (c->value<0)
456 return -EINVAL;
457 return 0;
458 case V4L2_CID_RED_BALANCE:
459 ret = pwc_get_red_gain(pdev, &c->value);
460 if (ret<0)
461 return -EINVAL;
462 c->value >>= 8;
463 return 0;
464 case V4L2_CID_BLUE_BALANCE:
465 ret = pwc_get_blue_gain(pdev, &c->value);
466 if (ret<0)
467 return -EINVAL;
468 c->value >>= 8;
469 return 0;
470 case V4L2_CID_AUTO_WHITE_BALANCE:
471 ret = pwc_get_awb(pdev);
472 if (ret<0)
473 return -EINVAL;
474 c->value = (ret == PWC_WB_MANUAL)?0:1;
475 return 0;
476 case V4L2_CID_GAIN:
477 ret = pwc_get_agc(pdev, &c->value);
478 if (ret<0)
479 return -EINVAL;
480 c->value >>= 8;
481 return 0;
482 case V4L2_CID_AUTOGAIN:
483 ret = pwc_get_agc(pdev, &c->value);
484 if (ret<0)
485 return -EINVAL;
486 c->value = (c->value < 0)?1:0;
487 return 0;
488 case V4L2_CID_EXPOSURE:
489 ret = pwc_get_shutter_speed(pdev, &c->value);
490 if (ret<0)
491 return -EINVAL;
492 return 0;
493 case V4L2_CID_PRIVATE_COLOUR_MODE:
494 ret = pwc_get_colour_mode(pdev, &c->value);
495 if (ret < 0)
496 return -EINVAL;
497 return 0;
498 case V4L2_CID_PRIVATE_AUTOCONTOUR:
499 ret = pwc_get_contour(pdev, &c->value);
500 if (ret < 0)
501 return -EINVAL;
502 c->value=(c->value == -1?1:0);
503 return 0;
504 case V4L2_CID_PRIVATE_CONTOUR:
505 ret = pwc_get_contour(pdev, &c->value);
506 if (ret < 0)
507 return -EINVAL;
508 c->value >>= 10;
509 return 0;
510 case V4L2_CID_PRIVATE_BACKLIGHT:
511 ret = pwc_get_backlight(pdev, &c->value);
512 if (ret < 0)
513 return -EINVAL;
514 return 0;
515 case V4L2_CID_PRIVATE_FLICKERLESS:
516 ret = pwc_get_flicker(pdev, &c->value);
517 if (ret < 0)
518 return -EINVAL;
519 c->value=(c->value?1:0);
520 return 0;
521 case V4L2_CID_PRIVATE_NOISE_REDUCTION:
522 ret = pwc_get_dynamic_noise(pdev, &c->value);
523 if (ret < 0)
524 return -EINVAL;
525 return 0;
526
527 case V4L2_CID_PRIVATE_SAVE_USER:
528 case V4L2_CID_PRIVATE_RESTORE_USER:
529 case V4L2_CID_PRIVATE_RESTORE_FACTORY:
530 return -EINVAL;
531 }
532 return -EINVAL;
533 }
534 case VIDIOC_S_CTRL:
535 {
536 struct v4l2_control *c = arg;
537 int ret;
538
539 switch (c->id)
540 {
541 case V4L2_CID_BRIGHTNESS:
542 c->value <<= 9;
543 ret = pwc_set_brightness(pdev, c->value);
544 if (ret<0)
545 return -EINVAL;
546 return 0;
547 case V4L2_CID_CONTRAST:
548 c->value <<= 10;
549 ret = pwc_set_contrast(pdev, c->value);
550 if (ret<0)
551 return -EINVAL;
552 return 0;
553 case V4L2_CID_SATURATION:
554 ret = pwc_set_saturation(pdev, c->value);
555 if (ret<0)
556 return -EINVAL;
557 return 0;
558 case V4L2_CID_GAMMA:
559 c->value <<= 11;
560 ret = pwc_set_gamma(pdev, c->value);
561 if (ret<0)
562 return -EINVAL;
563 return 0;
564 case V4L2_CID_RED_BALANCE:
565 c->value <<= 8;
566 ret = pwc_set_red_gain(pdev, c->value);
567 if (ret<0)
568 return -EINVAL;
569 return 0;
570 case V4L2_CID_BLUE_BALANCE:
571 c->value <<= 8;
572 ret = pwc_set_blue_gain(pdev, c->value);
573 if (ret<0)
574 return -EINVAL;
575 return 0;
576 case V4L2_CID_AUTO_WHITE_BALANCE:
577 c->value = (c->value == 0)?PWC_WB_MANUAL:PWC_WB_AUTO;
578 ret = pwc_set_awb(pdev, c->value);
579 if (ret<0)
580 return -EINVAL;
581 return 0;
582 case V4L2_CID_EXPOSURE:
583 c->value <<= 8;
584 ret = pwc_set_shutter_speed(pdev, c->value?0:1, c->value);
585 if (ret<0)
586 return -EINVAL;
587 return 0;
588 case V4L2_CID_AUTOGAIN:
589 /* autogain off means nothing without a gain */
590 if (c->value == 0)
591 return 0;
592 ret = pwc_set_agc(pdev, c->value, 0);
593 if (ret<0)
594 return -EINVAL;
595 return 0;
596 case V4L2_CID_GAIN:
597 c->value <<= 8;
598 ret = pwc_set_agc(pdev, 0, c->value);
599 if (ret<0)
600 return -EINVAL;
601 return 0;
602 case V4L2_CID_PRIVATE_SAVE_USER:
603 if (pwc_save_user(pdev))
604 return -EINVAL;
605 return 0;
606 case V4L2_CID_PRIVATE_RESTORE_USER:
607 if (pwc_restore_user(pdev))
608 return -EINVAL;
609 return 0;
610 case V4L2_CID_PRIVATE_RESTORE_FACTORY:
611 if (pwc_restore_factory(pdev))
612 return -EINVAL;
613 return 0;
614 case V4L2_CID_PRIVATE_COLOUR_MODE:
615 ret = pwc_set_colour_mode(pdev, c->value);
616 if (ret < 0)
617 return -EINVAL;
618 return 0;
619 case V4L2_CID_PRIVATE_AUTOCONTOUR:
620 c->value=(c->value == 1)?-1:0;
621 ret = pwc_set_contour(pdev, c->value);
622 if (ret < 0)
623 return -EINVAL;
624 return 0;
625 case V4L2_CID_PRIVATE_CONTOUR:
626 c->value <<= 10;
627 ret = pwc_set_contour(pdev, c->value);
628 if (ret < 0)
629 return -EINVAL;
630 return 0;
631 case V4L2_CID_PRIVATE_BACKLIGHT:
632 ret = pwc_set_backlight(pdev, c->value);
633 if (ret < 0)
634 return -EINVAL;
635 return 0;
636 case V4L2_CID_PRIVATE_FLICKERLESS:
637 ret = pwc_set_flicker(pdev, c->value);
638 if (ret < 0)
639 return -EINVAL;
640 case V4L2_CID_PRIVATE_NOISE_REDUCTION:
641 ret = pwc_set_dynamic_noise(pdev, c->value);
642 if (ret < 0)
643 return -EINVAL;
644 return 0;
645
646 }
647 return -EINVAL;
648 }
649
650 case VIDIOC_ENUM_FMT:
651 {
Trent Piepho657de3c2006-06-20 00:30:57 -0300652 struct v4l2_fmtdesc *f = arg;
Luc Saillard2b455db2006-04-24 10:29:46 -0300653 int index;
654
655 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
656 return -EINVAL;
657
Trent Piepho657de3c2006-06-20 00:30:57 -0300658 /* We only support two format: the raw format, and YUV */
Luc Saillard2b455db2006-04-24 10:29:46 -0300659 index = f->index;
660 memset(f,0,sizeof(struct v4l2_fmtdesc));
661 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
662 f->index = index;
663 switch(index)
664 {
665 case 0:
666 /* RAW format */
Trent Piepho657de3c2006-06-20 00:30:57 -0300667 f->pixelformat = pdev->type<=646?V4L2_PIX_FMT_PWC1:V4L2_PIX_FMT_PWC2;
Luc Saillard2b455db2006-04-24 10:29:46 -0300668 f->flags = V4L2_FMT_FLAG_COMPRESSED;
Trent Piepho657de3c2006-06-20 00:30:57 -0300669 strlcpy(f->description,"Raw Philips Webcam",sizeof(f->description));
Luc Saillard2b455db2006-04-24 10:29:46 -0300670 break;
671 case 1:
Trent Piepho657de3c2006-06-20 00:30:57 -0300672 f->pixelformat = V4L2_PIX_FMT_YUV420;
673 strlcpy(f->description,"4:2:0, planar, Y-Cb-Cr",sizeof(f->description));
Luc Saillard2b455db2006-04-24 10:29:46 -0300674 break;
Trent Piepho657de3c2006-06-20 00:30:57 -0300675 default:
Luc Saillard2b455db2006-04-24 10:29:46 -0300676 return -EINVAL;
677 }
678 return 0;
679 }
680
Trent Piepho657de3c2006-06-20 00:30:57 -0300681 case VIDIOC_G_FMT:
Luc Saillard2b455db2006-04-24 10:29:46 -0300682 {
Trent Piepho657de3c2006-06-20 00:30:57 -0300683 struct v4l2_format *f = arg;
Luc Saillard2b455db2006-04-24 10:29:46 -0300684
685 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",pdev->image.x,pdev->image.y);
686 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
687 return -EINVAL;
688
689 pwc_vidioc_fill_fmt(pdev, f);
690
691 return 0;
692 }
693
694 case VIDIOC_TRY_FMT:
695 return pwc_vidioc_try_fmt(pdev, arg);
696
Trent Piepho657de3c2006-06-20 00:30:57 -0300697 case VIDIOC_S_FMT:
Luc Saillard2b455db2006-04-24 10:29:46 -0300698 return pwc_vidioc_set_fmt(pdev, arg);
699
700 case VIDIOC_G_STD:
701 {
702 v4l2_std_id *std = arg;
703 *std = V4L2_STD_UNKNOWN;
704 return 0;
705 }
706
707 case VIDIOC_S_STD:
708 {
709 v4l2_std_id *std = arg;
710 if (*std != V4L2_STD_UNKNOWN)
711 return -EINVAL;
712 return 0;
713 }
714
715 case VIDIOC_ENUMSTD:
716 {
717 struct v4l2_standard *std = arg;
718 if (std->index != 0)
719 return -EINVAL;
720 std->id = V4L2_STD_UNKNOWN;
Roel Kluinbd0eb122009-08-08 12:58:52 -0300721 strlcpy(std->name, "webcam", sizeof(std->name));
Luc Saillard2b455db2006-04-24 10:29:46 -0300722 return 0;
723 }
724
725 case VIDIOC_REQBUFS:
726 {
727 struct v4l2_requestbuffers *rb = arg;
728 int nbuffers;
729
730 PWC_DEBUG_IOCTL("ioctl(VIDIOC_REQBUFS) count=%d\n",rb->count);
731 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
732 return -EINVAL;
733 if (rb->memory != V4L2_MEMORY_MMAP)
734 return -EINVAL;
735
736 nbuffers = rb->count;
737 if (nbuffers < 2)
738 nbuffers = 2;
739 else if (nbuffers > pwc_mbufs)
740 nbuffers = pwc_mbufs;
741 /* Force to use our # of buffers */
742 rb->count = pwc_mbufs;
743 return 0;
744 }
745
746 case VIDIOC_QUERYBUF:
747 {
748 struct v4l2_buffer *buf = arg;
749 int index;
750
751 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) index=%d\n",buf->index);
752 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
753 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) Bad type\n");
754 return -EINVAL;
755 }
756 if (buf->memory != V4L2_MEMORY_MMAP) {
757 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) Bad memory type\n");
758 return -EINVAL;
759 }
760 index = buf->index;
761 if (index < 0 || index >= pwc_mbufs) {
762 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) Bad index %d\n", buf->index);
763 return -EINVAL;
764 }
765
766 memset(buf, 0, sizeof(struct v4l2_buffer));
767 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
768 buf->index = index;
769 buf->m.offset = index * pdev->len_per_image;
Hans Verkuil479567c2010-09-12 17:05:11 -0300770 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
Luc Saillard2b455db2006-04-24 10:29:46 -0300771 buf->bytesused = pdev->frame_size + sizeof(struct pwc_raw_frame);
772 else
773 buf->bytesused = pdev->view.size;
774 buf->field = V4L2_FIELD_NONE;
775 buf->memory = V4L2_MEMORY_MMAP;
776 //buf->flags = V4L2_BUF_FLAG_MAPPED;
777 buf->length = pdev->len_per_image;
778
779 PWC_DEBUG_READ("VIDIOC_QUERYBUF: index=%d\n",buf->index);
780 PWC_DEBUG_READ("VIDIOC_QUERYBUF: m.offset=%d\n",buf->m.offset);
781 PWC_DEBUG_READ("VIDIOC_QUERYBUF: bytesused=%d\n",buf->bytesused);
782
783 return 0;
784 }
785
786 case VIDIOC_QBUF:
787 {
788 struct v4l2_buffer *buf = arg;
789
790 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QBUF) index=%d\n",buf->index);
791 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
792 return -EINVAL;
793 if (buf->memory != V4L2_MEMORY_MMAP)
794 return -EINVAL;
Roel Kluin223ffe52009-05-02 16:38:47 -0300795 if (buf->index >= pwc_mbufs)
Luc Saillard2b455db2006-04-24 10:29:46 -0300796 return -EINVAL;
797
798 buf->flags |= V4L2_BUF_FLAG_QUEUED;
799 buf->flags &= ~V4L2_BUF_FLAG_DONE;
800
801 return 0;
802 }
803
804 case VIDIOC_DQBUF:
805 {
806 struct v4l2_buffer *buf = arg;
807 int ret;
808
809 PWC_DEBUG_IOCTL("ioctl(VIDIOC_DQBUF)\n");
810
811 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
812 return -EINVAL;
813
814 /* Add ourselves to the frame wait-queue.
815
816 FIXME: needs auditing for safety.
817 QUESTION: In what respect? I think that using the
818 frameq is safe now.
819 */
820 add_wait_queue(&pdev->frameq, &wait);
821 while (pdev->full_frames == NULL) {
822 if (pdev->error_status) {
823 remove_wait_queue(&pdev->frameq, &wait);
824 set_current_state(TASK_RUNNING);
825 return -pdev->error_status;
826 }
827
828 if (signal_pending(current)) {
829 remove_wait_queue(&pdev->frameq, &wait);
830 set_current_state(TASK_RUNNING);
831 return -ERESTARTSYS;
832 }
Hans Verkuilb577f962011-01-13 05:05:36 -0300833 mutex_unlock(&pdev->modlock);
Luc Saillard2b455db2006-04-24 10:29:46 -0300834 schedule();
835 set_current_state(TASK_INTERRUPTIBLE);
Hans Verkuilb577f962011-01-13 05:05:36 -0300836 mutex_lock(&pdev->modlock);
Luc Saillard2b455db2006-04-24 10:29:46 -0300837 }
838 remove_wait_queue(&pdev->frameq, &wait);
839 set_current_state(TASK_RUNNING);
840
841 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: frame ready.\n");
842 /* Decompress data in pdev->images[pdev->fill_image] */
843 ret = pwc_handle_frame(pdev);
844 if (ret)
845 return -EFAULT;
846 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: after pwc_handle_frame\n");
847
848 buf->index = pdev->fill_image;
Hans Verkuil479567c2010-09-12 17:05:11 -0300849 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
Luc Saillard2b455db2006-04-24 10:29:46 -0300850 buf->bytesused = pdev->frame_size + sizeof(struct pwc_raw_frame);
851 else
852 buf->bytesused = pdev->view.size;
853 buf->flags = V4L2_BUF_FLAG_MAPPED;
854 buf->field = V4L2_FIELD_NONE;
855 do_gettimeofday(&buf->timestamp);
856 buf->sequence = 0;
857 buf->memory = V4L2_MEMORY_MMAP;
858 buf->m.offset = pdev->fill_image * pdev->len_per_image;
Luc Saillard288bb0e2007-04-22 23:55:10 -0300859 buf->length = pdev->len_per_image;
Luc Saillard2b455db2006-04-24 10:29:46 -0300860 pwc_next_image(pdev);
861
862 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: buf->index=%d\n",buf->index);
863 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: buf->length=%d\n",buf->length);
864 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: m.offset=%d\n",buf->m.offset);
865 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: bytesused=%d\n",buf->bytesused);
866 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: leaving\n");
867 return 0;
868
869 }
870
871 case VIDIOC_STREAMON:
872 {
Hans de Goede3751e282010-11-16 11:39:25 -0300873 return pwc_isoc_init(pdev);
Luc Saillard2b455db2006-04-24 10:29:46 -0300874 }
875
876 case VIDIOC_STREAMOFF:
877 {
878 pwc_isoc_cleanup(pdev);
879 return 0;
880 }
881
Luc Saillard9ee6d782007-04-22 23:54:36 -0300882 case VIDIOC_ENUM_FRAMESIZES:
883 {
884 struct v4l2_frmsizeenum *fsize = arg;
885 unsigned int i = 0, index = fsize->index;
886
887 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
888 for (i = 0; i < PSZ_MAX; i++) {
889 if (pdev->image_mask & (1UL << i)) {
890 if (!index--) {
891 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
892 fsize->discrete.width = pwc_image_sizes[i].x;
893 fsize->discrete.height = pwc_image_sizes[i].y;
894 return 0;
895 }
896 }
897 }
898 } else if (fsize->index == 0 &&
899 ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
900 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
901
902 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
903 fsize->discrete.width = pdev->abs_max.x;
904 fsize->discrete.height = pdev->abs_max.y;
905 return 0;
906 }
907 return -EINVAL;
908 }
909
910 case VIDIOC_ENUM_FRAMEINTERVALS:
911 {
912 struct v4l2_frmivalenum *fival = arg;
913 int size = -1;
914 unsigned int i;
915
916 for (i = 0; i < PSZ_MAX; i++) {
917 if (pwc_image_sizes[i].x == fival->width &&
918 pwc_image_sizes[i].y == fival->height) {
919 size = i;
920 break;
921 }
922 }
923
924 /* TODO: Support raw format */
925 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420) {
926 return -EINVAL;
927 }
928
929 i = pwc_get_fps(pdev, fival->index, size);
930 if (!i)
931 return -EINVAL;
932
933 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
934 fival->discrete.numerator = 1;
935 fival->discrete.denominator = i;
936
937 return 0;
938 }
939
Luc Saillard2b455db2006-04-24 10:29:46 -0300940 default:
941 return pwc_ioctl(pdev, cmd, arg);
942 } /* ..switch */
943 return 0;
944}
945
946/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */