blob: 834055b71e0418352d100a650d57bddeb7a407c8 [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) */
Hans de Goede4fba4712011-06-26 12:13:44 -0300287
288static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
Luc Saillard2b455db2006-04-24 10:29:46 -0300289{
Hans de Goede4fba4712011-06-26 12:13:44 -0300290 struct pwc_device *pdev = video_drvdata(file);
Luc Saillard2b455db2006-04-24 10:29:46 -0300291 int ret, fps, snapshot, compression, pixelformat;
292
Hans de Goedeb824bb42011-06-25 17:39:19 -0300293 if (!pdev->udev)
294 return -ENODEV;
295
Hans de Goede4fba4712011-06-26 12:13:44 -0300296 if (pdev->capt_file != NULL &&
297 pdev->capt_file != file)
298 return -EBUSY;
299
300 pdev->capt_file = file;
301
Luc Saillard2b455db2006-04-24 10:29:46 -0300302 ret = pwc_vidioc_try_fmt(pdev, f);
303 if (ret<0)
304 return ret;
305
306 pixelformat = f->fmt.pix.pixelformat;
307 compression = pdev->vcompression;
308 snapshot = 0;
309 fps = pdev->vframes;
310 if (f->fmt.pix.priv) {
311 compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
312 snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
313 fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
314 if (fps == 0)
315 fps = pdev->vframes;
316 }
317
Hans Verkuil479567c2010-09-12 17:05:11 -0300318 if (pixelformat != V4L2_PIX_FMT_YUV420 &&
319 pixelformat != V4L2_PIX_FMT_PWC1 &&
320 pixelformat != V4L2_PIX_FMT_PWC2)
321 return -EINVAL;
Luc Saillard2b455db2006-04-24 10:29:46 -0300322
Hans de Goede885fe182011-06-06 15:33:44 -0300323 if (vb2_is_streaming(&pdev->vb_queue))
Hans de Goede3751e282010-11-16 11:39:25 -0300324 return -EBUSY;
325
326 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
Luc Saillard2b455db2006-04-24 10:29:46 -0300327 "compression=%d snapshot=%d format=%c%c%c%c\n",
328 f->fmt.pix.width, f->fmt.pix.height, fps,
329 compression, snapshot,
330 (pixelformat)&255,
331 (pixelformat>>8)&255,
332 (pixelformat>>16)&255,
333 (pixelformat>>24)&255);
334
Hans de Goede3751e282010-11-16 11:39:25 -0300335 ret = pwc_set_video_mode(pdev,
Luc Saillard2b455db2006-04-24 10:29:46 -0300336 f->fmt.pix.width,
337 f->fmt.pix.height,
338 fps,
339 compression,
340 snapshot);
341
Hans de Goede3751e282010-11-16 11:39:25 -0300342 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
Luc Saillard2b455db2006-04-24 10:29:46 -0300343
344 if (ret)
345 return ret;
346
Hans Verkuil479567c2010-09-12 17:05:11 -0300347 pdev->pixfmt = pixelformat;
348
Luc Saillard2b455db2006-04-24 10:29:46 -0300349 pwc_vidioc_fill_fmt(pdev, f);
350
351 return 0;
352
353}
354
Hans Verkuilafa38522011-01-22 06:34:55 -0300355static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Luc Saillard2b455db2006-04-24 10:29:46 -0300356{
357 struct video_device *vdev = video_devdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300358 struct pwc_device *pdev = video_drvdata(file);
Luc Saillard2b455db2006-04-24 10:29:46 -0300359
Hans de Goedeb824bb42011-06-25 17:39:19 -0300360 if (!pdev->udev)
361 return -ENODEV;
362
Hans Verkuilafa38522011-01-22 06:34:55 -0300363 strcpy(cap->driver, PWC_NAME);
364 strlcpy(cap->card, vdev->name, sizeof(cap->card));
365 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
Hans Verkuilafa38522011-01-22 06:34:55 -0300366 cap->capabilities =
367 V4L2_CAP_VIDEO_CAPTURE |
368 V4L2_CAP_STREAMING |
369 V4L2_CAP_READWRITE;
Luc Saillard2b455db2006-04-24 10:29:46 -0300370 return 0;
371}
372
Hans Verkuilafa38522011-01-22 06:34:55 -0300373static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
374{
375 if (i->index) /* Only one INPUT is supported */
376 return -EINVAL;
377
378 strcpy(i->name, "usb");
379 return 0;
380}
381
382static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
383{
384 *i = 0;
385 return 0;
386}
387
388static int pwc_s_input(struct file *file, void *fh, unsigned int i)
389{
390 return i ? -EINVAL : 0;
391}
392
393static int pwc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
394{
Jean-Francois Moine65886872011-03-27 04:11:48 -0300395 int i, idx;
396 u32 id;
Hans Verkuilafa38522011-01-22 06:34:55 -0300397
Jean-Francois Moine65886872011-03-27 04:11:48 -0300398 id = c->id;
399 if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
400 id &= V4L2_CTRL_ID_MASK;
401 id++;
402 idx = -1;
403 for (i = 0; i < ARRAY_SIZE(pwc_controls); i++) {
404 if (pwc_controls[i].id < id)
405 continue;
406 if (idx >= 0
407 && pwc_controls[i].id > pwc_controls[idx].id)
408 continue;
409 idx = i;
410 }
411 if (idx < 0)
412 return -EINVAL;
413 memcpy(c, &pwc_controls[idx], sizeof pwc_controls[0]);
414 return 0;
415 }
Hans Verkuilafa38522011-01-22 06:34:55 -0300416 for (i = 0; i < sizeof(pwc_controls) / sizeof(struct v4l2_queryctrl); i++) {
417 if (pwc_controls[i].id == c->id) {
418 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
419 memcpy(c, &pwc_controls[i], sizeof(struct v4l2_queryctrl));
420 return 0;
421 }
422 }
423 return -EINVAL;
424}
425
426static int pwc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
427{
428 struct pwc_device *pdev = video_drvdata(file);
429 int ret;
430
Hans de Goedeb824bb42011-06-25 17:39:19 -0300431 if (!pdev->udev)
432 return -ENODEV;
433
Hans Verkuilafa38522011-01-22 06:34:55 -0300434 switch (c->id) {
435 case V4L2_CID_BRIGHTNESS:
436 c->value = pwc_get_brightness(pdev);
437 if (c->value < 0)
438 return -EINVAL;
439 return 0;
440 case V4L2_CID_CONTRAST:
441 c->value = pwc_get_contrast(pdev);
442 if (c->value < 0)
443 return -EINVAL;
444 return 0;
445 case V4L2_CID_SATURATION:
446 ret = pwc_get_saturation(pdev, &c->value);
447 if (ret < 0)
448 return -EINVAL;
449 return 0;
450 case V4L2_CID_GAMMA:
451 c->value = pwc_get_gamma(pdev);
452 if (c->value < 0)
453 return -EINVAL;
454 return 0;
455 case V4L2_CID_RED_BALANCE:
456 ret = pwc_get_red_gain(pdev, &c->value);
457 if (ret < 0)
458 return -EINVAL;
459 c->value >>= 8;
460 return 0;
461 case V4L2_CID_BLUE_BALANCE:
462 ret = pwc_get_blue_gain(pdev, &c->value);
463 if (ret < 0)
464 return -EINVAL;
465 c->value >>= 8;
466 return 0;
467 case V4L2_CID_AUTO_WHITE_BALANCE:
468 ret = pwc_get_awb(pdev);
469 if (ret < 0)
470 return -EINVAL;
471 c->value = (ret == PWC_WB_MANUAL) ? 0 : 1;
472 return 0;
473 case V4L2_CID_GAIN:
474 ret = pwc_get_agc(pdev, &c->value);
475 if (ret < 0)
476 return -EINVAL;
477 c->value >>= 8;
478 return 0;
479 case V4L2_CID_AUTOGAIN:
480 ret = pwc_get_agc(pdev, &c->value);
481 if (ret < 0)
482 return -EINVAL;
483 c->value = (c->value < 0) ? 1 : 0;
484 return 0;
485 case V4L2_CID_EXPOSURE:
486 ret = pwc_get_shutter_speed(pdev, &c->value);
487 if (ret < 0)
488 return -EINVAL;
489 return 0;
490 case V4L2_CID_PRIVATE_COLOUR_MODE:
491 ret = pwc_get_colour_mode(pdev, &c->value);
492 if (ret < 0)
493 return -EINVAL;
494 return 0;
495 case V4L2_CID_PRIVATE_AUTOCONTOUR:
496 ret = pwc_get_contour(pdev, &c->value);
497 if (ret < 0)
498 return -EINVAL;
499 c->value = (c->value == -1 ? 1 : 0);
500 return 0;
501 case V4L2_CID_PRIVATE_CONTOUR:
502 ret = pwc_get_contour(pdev, &c->value);
503 if (ret < 0)
504 return -EINVAL;
505 c->value >>= 10;
506 return 0;
507 case V4L2_CID_PRIVATE_BACKLIGHT:
508 ret = pwc_get_backlight(pdev, &c->value);
509 if (ret < 0)
510 return -EINVAL;
511 return 0;
512 case V4L2_CID_PRIVATE_FLICKERLESS:
513 ret = pwc_get_flicker(pdev, &c->value);
514 if (ret < 0)
515 return -EINVAL;
516 c->value = (c->value ? 1 : 0);
517 return 0;
518 case V4L2_CID_PRIVATE_NOISE_REDUCTION:
519 ret = pwc_get_dynamic_noise(pdev, &c->value);
520 if (ret < 0)
521 return -EINVAL;
522 return 0;
523
524 case V4L2_CID_PRIVATE_SAVE_USER:
525 case V4L2_CID_PRIVATE_RESTORE_USER:
526 case V4L2_CID_PRIVATE_RESTORE_FACTORY:
527 return -EINVAL;
528 }
529 return -EINVAL;
530}
531
532static int pwc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
533{
534 struct pwc_device *pdev = video_drvdata(file);
535 int ret;
536
Hans de Goedeb824bb42011-06-25 17:39:19 -0300537 if (!pdev->udev)
538 return -ENODEV;
539
Hans Verkuilafa38522011-01-22 06:34:55 -0300540 switch (c->id) {
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
650static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
651{
652 struct pwc_device *pdev = video_drvdata(file);
653
654 /* We only support two format: the raw format, and YUV */
655 switch (f->index) {
656 case 0:
657 /* RAW format */
658 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
659 f->flags = V4L2_FMT_FLAG_COMPRESSED;
660 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
661 break;
662 case 1:
663 f->pixelformat = V4L2_PIX_FMT_YUV420;
664 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
665 break;
666 default:
667 return -EINVAL;
668 }
669 return 0;
670}
671
672static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
673{
674 struct pwc_device *pdev = video_drvdata(file);
675
676 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
677 pdev->image.x, pdev->image.y);
678 pwc_vidioc_fill_fmt(pdev, f);
679 return 0;
680}
681
682static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
683{
684 struct pwc_device *pdev = video_drvdata(file);
685
686 return pwc_vidioc_try_fmt(pdev, f);
687}
688
Hans de Goede885fe182011-06-06 15:33:44 -0300689static int pwc_reqbufs(struct file *file, void *fh,
690 struct v4l2_requestbuffers *rb)
Hans Verkuilafa38522011-01-22 06:34:55 -0300691{
Hans de Goede885fe182011-06-06 15:33:44 -0300692 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300693
Hans de Goede4fba4712011-06-26 12:13:44 -0300694 if (pdev->capt_file != NULL &&
695 pdev->capt_file != file)
696 return -EBUSY;
697
698 pdev->capt_file = file;
699
Hans de Goede885fe182011-06-06 15:33:44 -0300700 return vb2_reqbufs(&pdev->vb_queue, rb);
Hans Verkuilafa38522011-01-22 06:34:55 -0300701}
702
703static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
704{
705 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300706
Hans de Goede885fe182011-06-06 15:33:44 -0300707 return vb2_querybuf(&pdev->vb_queue, buf);
Hans Verkuilafa38522011-01-22 06:34:55 -0300708}
709
710static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
711{
Hans de Goede885fe182011-06-06 15:33:44 -0300712 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300713
Hans de Goedeb824bb42011-06-25 17:39:19 -0300714 if (!pdev->udev)
715 return -ENODEV;
716
Hans de Goede4fba4712011-06-26 12:13:44 -0300717 if (pdev->capt_file != file)
718 return -EBUSY;
719
Hans de Goede885fe182011-06-06 15:33:44 -0300720 return vb2_qbuf(&pdev->vb_queue, buf);
Hans Verkuilafa38522011-01-22 06:34:55 -0300721}
722
723static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
724{
Hans Verkuilafa38522011-01-22 06:34:55 -0300725 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300726
Hans de Goedeb824bb42011-06-25 17:39:19 -0300727 if (!pdev->udev)
728 return -ENODEV;
729
Hans de Goede4fba4712011-06-26 12:13:44 -0300730 if (pdev->capt_file != file)
731 return -EBUSY;
732
Hans de Goede885fe182011-06-06 15:33:44 -0300733 return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
Hans Verkuilafa38522011-01-22 06:34:55 -0300734}
735
736static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
737{
738 struct pwc_device *pdev = video_drvdata(file);
739
Hans de Goedeb824bb42011-06-25 17:39:19 -0300740 if (!pdev->udev)
741 return -ENODEV;
742
Hans de Goede4fba4712011-06-26 12:13:44 -0300743 if (pdev->capt_file != file)
744 return -EBUSY;
745
Hans de Goede885fe182011-06-06 15:33:44 -0300746 return vb2_streamon(&pdev->vb_queue, i);
Hans Verkuilafa38522011-01-22 06:34:55 -0300747}
748
749static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
750{
751 struct pwc_device *pdev = video_drvdata(file);
752
Hans de Goedeb824bb42011-06-25 17:39:19 -0300753 if (!pdev->udev)
754 return -ENODEV;
755
Hans de Goede4fba4712011-06-26 12:13:44 -0300756 if (pdev->capt_file != file)
757 return -EBUSY;
758
Hans de Goede885fe182011-06-06 15:33:44 -0300759 return vb2_streamoff(&pdev->vb_queue, i);
Hans Verkuilafa38522011-01-22 06:34:55 -0300760}
761
762static int pwc_enum_framesizes(struct file *file, void *fh,
763 struct v4l2_frmsizeenum *fsize)
764{
765 struct pwc_device *pdev = video_drvdata(file);
766 unsigned int i = 0, index = fsize->index;
767
768 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
769 for (i = 0; i < PSZ_MAX; i++) {
770 if (pdev->image_mask & (1UL << i)) {
771 if (!index--) {
772 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
773 fsize->discrete.width = pwc_image_sizes[i].x;
774 fsize->discrete.height = pwc_image_sizes[i].y;
775 return 0;
776 }
777 }
778 }
779 } else if (fsize->index == 0 &&
780 ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
781 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
782
783 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
784 fsize->discrete.width = pdev->abs_max.x;
785 fsize->discrete.height = pdev->abs_max.y;
786 return 0;
787 }
788 return -EINVAL;
789}
790
791static int pwc_enum_frameintervals(struct file *file, void *fh,
792 struct v4l2_frmivalenum *fival)
793{
794 struct pwc_device *pdev = video_drvdata(file);
795 int size = -1;
796 unsigned int i;
797
798 for (i = 0; i < PSZ_MAX; i++) {
799 if (pwc_image_sizes[i].x == fival->width &&
800 pwc_image_sizes[i].y == fival->height) {
801 size = i;
802 break;
803 }
804 }
805
806 /* TODO: Support raw format */
807 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
808 return -EINVAL;
809
810 i = pwc_get_fps(pdev, fival->index, size);
811 if (!i)
812 return -EINVAL;
813
814 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
815 fival->discrete.numerator = 1;
816 fival->discrete.denominator = i;
817
818 return 0;
819}
820
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300821static long pwc_default(struct file *file, void *fh, bool valid_prio,
822 int cmd, void *arg)
Hans Verkuilafa38522011-01-22 06:34:55 -0300823{
824 struct pwc_device *pdev = video_drvdata(file);
825
826 return pwc_ioctl(pdev, cmd, arg);
827}
828
829const struct v4l2_ioctl_ops pwc_ioctl_ops = {
830 .vidioc_querycap = pwc_querycap,
831 .vidioc_enum_input = pwc_enum_input,
832 .vidioc_g_input = pwc_g_input,
833 .vidioc_s_input = pwc_s_input,
834 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
835 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
836 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
837 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
838 .vidioc_queryctrl = pwc_queryctrl,
839 .vidioc_g_ctrl = pwc_g_ctrl,
840 .vidioc_s_ctrl = pwc_s_ctrl,
841 .vidioc_reqbufs = pwc_reqbufs,
842 .vidioc_querybuf = pwc_querybuf,
843 .vidioc_qbuf = pwc_qbuf,
844 .vidioc_dqbuf = pwc_dqbuf,
845 .vidioc_streamon = pwc_streamon,
846 .vidioc_streamoff = pwc_streamoff,
847 .vidioc_enum_framesizes = pwc_enum_framesizes,
848 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
849 .vidioc_default = pwc_default,
850};
851
852
Luc Saillard2b455db2006-04-24 10:29:46 -0300853/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */