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