blob: aa87e462a958aec8025fb99c60772dd97039e66a [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 Verkuilafa38522011-01-22 06:34:55 -0300344static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Luc Saillard2b455db2006-04-24 10:29:46 -0300345{
346 struct video_device *vdev = video_devdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300347 struct pwc_device *pdev = video_drvdata(file);
Luc Saillard2b455db2006-04-24 10:29:46 -0300348
Hans Verkuilafa38522011-01-22 06:34:55 -0300349 strcpy(cap->driver, PWC_NAME);
350 strlcpy(cap->card, vdev->name, sizeof(cap->card));
351 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
352 cap->version = PWC_VERSION_CODE;
353 cap->capabilities =
354 V4L2_CAP_VIDEO_CAPTURE |
355 V4L2_CAP_STREAMING |
356 V4L2_CAP_READWRITE;
Luc Saillard2b455db2006-04-24 10:29:46 -0300357 return 0;
358}
359
Hans Verkuilafa38522011-01-22 06:34:55 -0300360static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
361{
362 if (i->index) /* Only one INPUT is supported */
363 return -EINVAL;
364
365 strcpy(i->name, "usb");
366 return 0;
367}
368
369static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
370{
371 *i = 0;
372 return 0;
373}
374
375static int pwc_s_input(struct file *file, void *fh, unsigned int i)
376{
377 return i ? -EINVAL : 0;
378}
379
380static int pwc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
381{
382 int i;
383
384 for (i = 0; i < sizeof(pwc_controls) / sizeof(struct v4l2_queryctrl); i++) {
385 if (pwc_controls[i].id == c->id) {
386 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
387 memcpy(c, &pwc_controls[i], sizeof(struct v4l2_queryctrl));
388 return 0;
389 }
390 }
391 return -EINVAL;
392}
393
394static int pwc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
395{
396 struct pwc_device *pdev = video_drvdata(file);
397 int ret;
398
399 switch (c->id) {
400 case V4L2_CID_BRIGHTNESS:
401 c->value = pwc_get_brightness(pdev);
402 if (c->value < 0)
403 return -EINVAL;
404 return 0;
405 case V4L2_CID_CONTRAST:
406 c->value = pwc_get_contrast(pdev);
407 if (c->value < 0)
408 return -EINVAL;
409 return 0;
410 case V4L2_CID_SATURATION:
411 ret = pwc_get_saturation(pdev, &c->value);
412 if (ret < 0)
413 return -EINVAL;
414 return 0;
415 case V4L2_CID_GAMMA:
416 c->value = pwc_get_gamma(pdev);
417 if (c->value < 0)
418 return -EINVAL;
419 return 0;
420 case V4L2_CID_RED_BALANCE:
421 ret = pwc_get_red_gain(pdev, &c->value);
422 if (ret < 0)
423 return -EINVAL;
424 c->value >>= 8;
425 return 0;
426 case V4L2_CID_BLUE_BALANCE:
427 ret = pwc_get_blue_gain(pdev, &c->value);
428 if (ret < 0)
429 return -EINVAL;
430 c->value >>= 8;
431 return 0;
432 case V4L2_CID_AUTO_WHITE_BALANCE:
433 ret = pwc_get_awb(pdev);
434 if (ret < 0)
435 return -EINVAL;
436 c->value = (ret == PWC_WB_MANUAL) ? 0 : 1;
437 return 0;
438 case V4L2_CID_GAIN:
439 ret = pwc_get_agc(pdev, &c->value);
440 if (ret < 0)
441 return -EINVAL;
442 c->value >>= 8;
443 return 0;
444 case V4L2_CID_AUTOGAIN:
445 ret = pwc_get_agc(pdev, &c->value);
446 if (ret < 0)
447 return -EINVAL;
448 c->value = (c->value < 0) ? 1 : 0;
449 return 0;
450 case V4L2_CID_EXPOSURE:
451 ret = pwc_get_shutter_speed(pdev, &c->value);
452 if (ret < 0)
453 return -EINVAL;
454 return 0;
455 case V4L2_CID_PRIVATE_COLOUR_MODE:
456 ret = pwc_get_colour_mode(pdev, &c->value);
457 if (ret < 0)
458 return -EINVAL;
459 return 0;
460 case V4L2_CID_PRIVATE_AUTOCONTOUR:
461 ret = pwc_get_contour(pdev, &c->value);
462 if (ret < 0)
463 return -EINVAL;
464 c->value = (c->value == -1 ? 1 : 0);
465 return 0;
466 case V4L2_CID_PRIVATE_CONTOUR:
467 ret = pwc_get_contour(pdev, &c->value);
468 if (ret < 0)
469 return -EINVAL;
470 c->value >>= 10;
471 return 0;
472 case V4L2_CID_PRIVATE_BACKLIGHT:
473 ret = pwc_get_backlight(pdev, &c->value);
474 if (ret < 0)
475 return -EINVAL;
476 return 0;
477 case V4L2_CID_PRIVATE_FLICKERLESS:
478 ret = pwc_get_flicker(pdev, &c->value);
479 if (ret < 0)
480 return -EINVAL;
481 c->value = (c->value ? 1 : 0);
482 return 0;
483 case V4L2_CID_PRIVATE_NOISE_REDUCTION:
484 ret = pwc_get_dynamic_noise(pdev, &c->value);
485 if (ret < 0)
486 return -EINVAL;
487 return 0;
488
489 case V4L2_CID_PRIVATE_SAVE_USER:
490 case V4L2_CID_PRIVATE_RESTORE_USER:
491 case V4L2_CID_PRIVATE_RESTORE_FACTORY:
492 return -EINVAL;
493 }
494 return -EINVAL;
495}
496
497static int pwc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
498{
499 struct pwc_device *pdev = video_drvdata(file);
500 int ret;
501
502 switch (c->id) {
503 case V4L2_CID_BRIGHTNESS:
504 c->value <<= 9;
505 ret = pwc_set_brightness(pdev, c->value);
506 if (ret < 0)
507 return -EINVAL;
508 return 0;
509 case V4L2_CID_CONTRAST:
510 c->value <<= 10;
511 ret = pwc_set_contrast(pdev, c->value);
512 if (ret < 0)
513 return -EINVAL;
514 return 0;
515 case V4L2_CID_SATURATION:
516 ret = pwc_set_saturation(pdev, c->value);
517 if (ret < 0)
518 return -EINVAL;
519 return 0;
520 case V4L2_CID_GAMMA:
521 c->value <<= 11;
522 ret = pwc_set_gamma(pdev, c->value);
523 if (ret < 0)
524 return -EINVAL;
525 return 0;
526 case V4L2_CID_RED_BALANCE:
527 c->value <<= 8;
528 ret = pwc_set_red_gain(pdev, c->value);
529 if (ret < 0)
530 return -EINVAL;
531 return 0;
532 case V4L2_CID_BLUE_BALANCE:
533 c->value <<= 8;
534 ret = pwc_set_blue_gain(pdev, c->value);
535 if (ret < 0)
536 return -EINVAL;
537 return 0;
538 case V4L2_CID_AUTO_WHITE_BALANCE:
539 c->value = (c->value == 0) ? PWC_WB_MANUAL : PWC_WB_AUTO;
540 ret = pwc_set_awb(pdev, c->value);
541 if (ret < 0)
542 return -EINVAL;
543 return 0;
544 case V4L2_CID_EXPOSURE:
545 c->value <<= 8;
546 ret = pwc_set_shutter_speed(pdev, c->value ? 0 : 1, c->value);
547 if (ret < 0)
548 return -EINVAL;
549 return 0;
550 case V4L2_CID_AUTOGAIN:
551 /* autogain off means nothing without a gain */
552 if (c->value == 0)
553 return 0;
554 ret = pwc_set_agc(pdev, c->value, 0);
555 if (ret < 0)
556 return -EINVAL;
557 return 0;
558 case V4L2_CID_GAIN:
559 c->value <<= 8;
560 ret = pwc_set_agc(pdev, 0, c->value);
561 if (ret < 0)
562 return -EINVAL;
563 return 0;
564 case V4L2_CID_PRIVATE_SAVE_USER:
565 if (pwc_save_user(pdev))
566 return -EINVAL;
567 return 0;
568 case V4L2_CID_PRIVATE_RESTORE_USER:
569 if (pwc_restore_user(pdev))
570 return -EINVAL;
571 return 0;
572 case V4L2_CID_PRIVATE_RESTORE_FACTORY:
573 if (pwc_restore_factory(pdev))
574 return -EINVAL;
575 return 0;
576 case V4L2_CID_PRIVATE_COLOUR_MODE:
577 ret = pwc_set_colour_mode(pdev, c->value);
578 if (ret < 0)
579 return -EINVAL;
580 return 0;
581 case V4L2_CID_PRIVATE_AUTOCONTOUR:
582 c->value = (c->value == 1) ? -1 : 0;
583 ret = pwc_set_contour(pdev, c->value);
584 if (ret < 0)
585 return -EINVAL;
586 return 0;
587 case V4L2_CID_PRIVATE_CONTOUR:
588 c->value <<= 10;
589 ret = pwc_set_contour(pdev, c->value);
590 if (ret < 0)
591 return -EINVAL;
592 return 0;
593 case V4L2_CID_PRIVATE_BACKLIGHT:
594 ret = pwc_set_backlight(pdev, c->value);
595 if (ret < 0)
596 return -EINVAL;
597 return 0;
598 case V4L2_CID_PRIVATE_FLICKERLESS:
599 ret = pwc_set_flicker(pdev, c->value);
600 if (ret < 0)
601 return -EINVAL;
602 case V4L2_CID_PRIVATE_NOISE_REDUCTION:
603 ret = pwc_set_dynamic_noise(pdev, c->value);
604 if (ret < 0)
605 return -EINVAL;
606 return 0;
607
608 }
609 return -EINVAL;
610}
611
612static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
613{
614 struct pwc_device *pdev = video_drvdata(file);
615
616 /* We only support two format: the raw format, and YUV */
617 switch (f->index) {
618 case 0:
619 /* RAW format */
620 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
621 f->flags = V4L2_FMT_FLAG_COMPRESSED;
622 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
623 break;
624 case 1:
625 f->pixelformat = V4L2_PIX_FMT_YUV420;
626 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
627 break;
628 default:
629 return -EINVAL;
630 }
631 return 0;
632}
633
634static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
635{
636 struct pwc_device *pdev = video_drvdata(file);
637
638 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
639 pdev->image.x, pdev->image.y);
640 pwc_vidioc_fill_fmt(pdev, f);
641 return 0;
642}
643
644static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
645{
646 struct pwc_device *pdev = video_drvdata(file);
647
648 return pwc_vidioc_try_fmt(pdev, f);
649}
650
651static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
652{
653 struct pwc_device *pdev = video_drvdata(file);
654
655 return pwc_vidioc_set_fmt(pdev, f);
656}
657
658static int pwc_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
659{
660 int nbuffers;
661
662 PWC_DEBUG_IOCTL("ioctl(VIDIOC_REQBUFS) count=%d\n", rb->count);
663 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
664 return -EINVAL;
665 if (rb->memory != V4L2_MEMORY_MMAP)
666 return -EINVAL;
667
668 nbuffers = rb->count;
669 if (nbuffers < 2)
670 nbuffers = 2;
671 else if (nbuffers > pwc_mbufs)
672 nbuffers = pwc_mbufs;
673 /* Force to use our # of buffers */
674 rb->count = pwc_mbufs;
675 return 0;
676}
677
678static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
679{
680 struct pwc_device *pdev = video_drvdata(file);
681 int index;
682
683 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) index=%d\n", buf->index);
684 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
685 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) Bad type\n");
686 return -EINVAL;
687 }
688 index = buf->index;
689 if (index < 0 || index >= pwc_mbufs) {
690 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYBUF) Bad index %d\n", buf->index);
691 return -EINVAL;
692 }
693
694 buf->m.offset = index * pdev->len_per_image;
695 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
696 buf->bytesused = pdev->frame_size + sizeof(struct pwc_raw_frame);
697 else
698 buf->bytesused = pdev->view.size;
699 buf->field = V4L2_FIELD_NONE;
700 buf->memory = V4L2_MEMORY_MMAP;
701 /*buf->flags = V4L2_BUF_FLAG_MAPPED;*/
702 buf->length = pdev->len_per_image;
703
704 PWC_DEBUG_READ("VIDIOC_QUERYBUF: index=%d\n", buf->index);
705 PWC_DEBUG_READ("VIDIOC_QUERYBUF: m.offset=%d\n", buf->m.offset);
706 PWC_DEBUG_READ("VIDIOC_QUERYBUF: bytesused=%d\n", buf->bytesused);
707
708 return 0;
709}
710
711static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
712{
713 PWC_DEBUG_IOCTL("ioctl(VIDIOC_QBUF) index=%d\n", buf->index);
714 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
715 return -EINVAL;
716 if (buf->memory != V4L2_MEMORY_MMAP)
717 return -EINVAL;
718 if (buf->index >= pwc_mbufs)
719 return -EINVAL;
720
721 buf->flags |= V4L2_BUF_FLAG_QUEUED;
722 buf->flags &= ~V4L2_BUF_FLAG_DONE;
723
724 return 0;
725}
726
727static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
728{
729 DECLARE_WAITQUEUE(wait, current);
730 struct pwc_device *pdev = video_drvdata(file);
731 int ret;
732
733 PWC_DEBUG_IOCTL("ioctl(VIDIOC_DQBUF)\n");
734
735 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
736 return -EINVAL;
737
738 add_wait_queue(&pdev->frameq, &wait);
739 while (pdev->full_frames == NULL) {
740 if (pdev->error_status) {
741 remove_wait_queue(&pdev->frameq, &wait);
742 set_current_state(TASK_RUNNING);
743 return -pdev->error_status;
744 }
745
746 if (signal_pending(current)) {
747 remove_wait_queue(&pdev->frameq, &wait);
748 set_current_state(TASK_RUNNING);
749 return -ERESTARTSYS;
750 }
751 mutex_unlock(&pdev->modlock);
752 schedule();
753 set_current_state(TASK_INTERRUPTIBLE);
754 mutex_lock(&pdev->modlock);
755 }
756 remove_wait_queue(&pdev->frameq, &wait);
757 set_current_state(TASK_RUNNING);
758
759 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: frame ready.\n");
760 /* Decompress data in pdev->images[pdev->fill_image] */
761 ret = pwc_handle_frame(pdev);
762 if (ret)
763 return -EFAULT;
764 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: after pwc_handle_frame\n");
765
766 buf->index = pdev->fill_image;
767 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
768 buf->bytesused = pdev->frame_size + sizeof(struct pwc_raw_frame);
769 else
770 buf->bytesused = pdev->view.size;
771 buf->flags = V4L2_BUF_FLAG_MAPPED;
772 buf->field = V4L2_FIELD_NONE;
773 do_gettimeofday(&buf->timestamp);
774 buf->sequence = 0;
775 buf->memory = V4L2_MEMORY_MMAP;
776 buf->m.offset = pdev->fill_image * pdev->len_per_image;
777 buf->length = pdev->len_per_image;
778 pwc_next_image(pdev);
779
780 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: buf->index=%d\n", buf->index);
781 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: buf->length=%d\n", buf->length);
782 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: m.offset=%d\n", buf->m.offset);
783 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: bytesused=%d\n", buf->bytesused);
784 PWC_DEBUG_IOCTL("VIDIOC_DQBUF: leaving\n");
785 return 0;
786
787}
788
789static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
790{
791 struct pwc_device *pdev = video_drvdata(file);
792
793 return pwc_isoc_init(pdev);
794}
795
796static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
797{
798 struct pwc_device *pdev = video_drvdata(file);
799
800 pwc_isoc_cleanup(pdev);
801 return 0;
802}
803
804static int pwc_enum_framesizes(struct file *file, void *fh,
805 struct v4l2_frmsizeenum *fsize)
806{
807 struct pwc_device *pdev = video_drvdata(file);
808 unsigned int i = 0, index = fsize->index;
809
810 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
811 for (i = 0; i < PSZ_MAX; i++) {
812 if (pdev->image_mask & (1UL << i)) {
813 if (!index--) {
814 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
815 fsize->discrete.width = pwc_image_sizes[i].x;
816 fsize->discrete.height = pwc_image_sizes[i].y;
817 return 0;
818 }
819 }
820 }
821 } else if (fsize->index == 0 &&
822 ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
823 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
824
825 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
826 fsize->discrete.width = pdev->abs_max.x;
827 fsize->discrete.height = pdev->abs_max.y;
828 return 0;
829 }
830 return -EINVAL;
831}
832
833static int pwc_enum_frameintervals(struct file *file, void *fh,
834 struct v4l2_frmivalenum *fival)
835{
836 struct pwc_device *pdev = video_drvdata(file);
837 int size = -1;
838 unsigned int i;
839
840 for (i = 0; i < PSZ_MAX; i++) {
841 if (pwc_image_sizes[i].x == fival->width &&
842 pwc_image_sizes[i].y == fival->height) {
843 size = i;
844 break;
845 }
846 }
847
848 /* TODO: Support raw format */
849 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
850 return -EINVAL;
851
852 i = pwc_get_fps(pdev, fival->index, size);
853 if (!i)
854 return -EINVAL;
855
856 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
857 fival->discrete.numerator = 1;
858 fival->discrete.denominator = i;
859
860 return 0;
861}
862
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300863static long pwc_default(struct file *file, void *fh, bool valid_prio,
864 int cmd, void *arg)
Hans Verkuilafa38522011-01-22 06:34:55 -0300865{
866 struct pwc_device *pdev = video_drvdata(file);
867
868 return pwc_ioctl(pdev, cmd, arg);
869}
870
871const struct v4l2_ioctl_ops pwc_ioctl_ops = {
872 .vidioc_querycap = pwc_querycap,
873 .vidioc_enum_input = pwc_enum_input,
874 .vidioc_g_input = pwc_g_input,
875 .vidioc_s_input = pwc_s_input,
876 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
877 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
878 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
879 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
880 .vidioc_queryctrl = pwc_queryctrl,
881 .vidioc_g_ctrl = pwc_g_ctrl,
882 .vidioc_s_ctrl = pwc_s_ctrl,
883 .vidioc_reqbufs = pwc_reqbufs,
884 .vidioc_querybuf = pwc_querybuf,
885 .vidioc_qbuf = pwc_qbuf,
886 .vidioc_dqbuf = pwc_dqbuf,
887 .vidioc_streamon = pwc_streamon,
888 .vidioc_streamoff = pwc_streamoff,
889 .vidioc_enum_framesizes = pwc_enum_framesizes,
890 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
891 .vidioc_default = pwc_default,
892};
893
894
Luc Saillard2b455db2006-04-24 10:29:46 -0300895/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */