986dd5d6187a8a3ee4f5794caaac78e424ec9504
[pandora-kernel.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    (C) 2011 Hans de Goede <hdegoede@redhat.com>
6
7    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8    driver and thus may have bugs that are not present in the original version.
9    Please send bug reports and support requests to <luc@saillard.org>.
10    The decompression routines have been implemented by reverse-engineering the
11    Nemosoft binary pwcx module. Caveat emptor.
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 */
28
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/poll.h>
34 #include <linux/vmalloc.h>
35 #include <linux/jiffies.h>
36 #include <asm/io.h>
37
38 #include "pwc.h"
39
40 #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
41
42 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
43 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
44
45 static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
46         .g_volatile_ctrl = pwc_g_volatile_ctrl,
47         .s_ctrl = pwc_s_ctrl,
48 };
49
50 enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
51 enum { custom_autocontour, custom_contour, custom_noise_reduction,
52         custom_save_user, custom_restore_user, custom_restore_factory };
53
54 const char * const pwc_auto_whitebal_qmenu[] = {
55         "Indoor (Incandescant Lighting) Mode",
56         "Outdoor (Sunlight) Mode",
57         "Indoor (Fluorescent Lighting) Mode",
58         "Manual Mode",
59         "Auto Mode",
60         NULL
61 };
62
63 static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
64         .ops    = &pwc_ctrl_ops,
65         .id     = V4L2_CID_AUTO_WHITE_BALANCE,
66         .type   = V4L2_CTRL_TYPE_MENU,
67         .max    = awb_auto,
68         .qmenu  = pwc_auto_whitebal_qmenu,
69 };
70
71 static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
72         .ops    = &pwc_ctrl_ops,
73         .id     = PWC_CID_CUSTOM(autocontour),
74         .type   = V4L2_CTRL_TYPE_BOOLEAN,
75         .name   = "Auto contour",
76         .min    = 0,
77         .max    = 1,
78         .step   = 1,
79 };
80
81 static const struct v4l2_ctrl_config pwc_contour_cfg = {
82         .ops    = &pwc_ctrl_ops,
83         .id     = PWC_CID_CUSTOM(contour),
84         .type   = V4L2_CTRL_TYPE_INTEGER,
85         .name   = "Contour",
86         .min    = 0,
87         .max    = 63,
88         .step   = 1,
89 };
90
91 static const struct v4l2_ctrl_config pwc_backlight_cfg = {
92         .ops    = &pwc_ctrl_ops,
93         .id     = V4L2_CID_BACKLIGHT_COMPENSATION,
94         .type   = V4L2_CTRL_TYPE_BOOLEAN,
95         .min    = 0,
96         .max    = 1,
97         .step   = 1,
98 };
99
100 static const struct v4l2_ctrl_config pwc_flicker_cfg = {
101         .ops    = &pwc_ctrl_ops,
102         .id     = V4L2_CID_BAND_STOP_FILTER,
103         .type   = V4L2_CTRL_TYPE_BOOLEAN,
104         .min    = 0,
105         .max    = 1,
106         .step   = 1,
107 };
108
109 static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
110         .ops    = &pwc_ctrl_ops,
111         .id     = PWC_CID_CUSTOM(noise_reduction),
112         .type   = V4L2_CTRL_TYPE_INTEGER,
113         .name   = "Dynamic Noise Reduction",
114         .min    = 0,
115         .max    = 3,
116         .step   = 1,
117 };
118
119 static const struct v4l2_ctrl_config pwc_save_user_cfg = {
120         .ops    = &pwc_ctrl_ops,
121         .id     = PWC_CID_CUSTOM(save_user),
122         .type   = V4L2_CTRL_TYPE_BUTTON,
123         .name    = "Save User Settings",
124 };
125
126 static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
127         .ops    = &pwc_ctrl_ops,
128         .id     = PWC_CID_CUSTOM(restore_user),
129         .type   = V4L2_CTRL_TYPE_BUTTON,
130         .name    = "Restore User Settings",
131 };
132
133 static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
134         .ops    = &pwc_ctrl_ops,
135         .id     = PWC_CID_CUSTOM(restore_factory),
136         .type   = V4L2_CTRL_TYPE_BUTTON,
137         .name    = "Restore Factory Settings",
138 };
139
140 int pwc_init_controls(struct pwc_device *pdev)
141 {
142         struct v4l2_ctrl_handler *hdl;
143         struct v4l2_ctrl_config cfg;
144         int r, def;
145
146         hdl = &pdev->ctrl_handler;
147         r = v4l2_ctrl_handler_init(hdl, 20);
148         if (r)
149                 return r;
150
151         /* Brightness, contrast, saturation, gamma */
152         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
153         if (r || def > 127)
154                 def = 63;
155         pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
156                                 V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
157
158         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
159         if (r || def > 63)
160                 def = 31;
161         pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
162                                 V4L2_CID_CONTRAST, 0, 63, 1, def);
163
164         if (pdev->type >= 675) {
165                 if (pdev->type < 730)
166                         pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
167                 else
168                         pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
169                 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
170                                     &def);
171                 if (r || def < -100 || def > 100)
172                         def = 0;
173                 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
174                                       V4L2_CID_SATURATION, -100, 100, 1, def);
175         }
176
177         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
178         if (r || def > 31)
179                 def = 15;
180         pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
181                                 V4L2_CID_GAMMA, 0, 31, 1, def);
182
183         /* auto white balance, red gain, blue gain */
184         r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
185         if (r || def > awb_auto)
186                 def = awb_auto;
187         cfg = pwc_auto_white_balance_cfg;
188         cfg.name = v4l2_ctrl_get_name(cfg.id);
189         cfg.def = def;
190         pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
191         /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
192         if (!pdev->auto_white_balance)
193                 return hdl->error;
194
195         r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
196                             PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
197         if (r)
198                 def = 127;
199         pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
200                                 V4L2_CID_RED_BALANCE, 0, 255, 1, def);
201
202         r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
203                             PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
204         if (r)
205                 def = 127;
206         pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
207                                 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
208
209         v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual,
210                                pdev->auto_white_balance->cur.val == awb_auto);
211
212         /* autogain, gain */
213         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
214         if (r || (def != 0 && def != 0xff))
215                 def = 0;
216         /* Note a register value if 0 means auto gain is on */
217         pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
218                                 V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
219         if (!pdev->autogain)
220                 return hdl->error;
221
222         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
223         if (r || def > 63)
224                 def = 31;
225         pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
226                                 V4L2_CID_GAIN, 0, 63, 1, def);
227
228         /* auto exposure, exposure */
229         if (DEVICE_USE_CODEC2(pdev->type)) {
230                 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
231                                     &def);
232                 if (r || (def != 0 && def != 0xff))
233                         def = 0;
234                 /*
235                  * def = 0 auto, def = ff manual
236                  * menu idx 0 = auto, idx 1 = manual
237                  */
238                 pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
239                                         &pwc_ctrl_ops,
240                                         V4L2_CID_EXPOSURE_AUTO,
241                                         1, 0, def != 0);
242                 if (!pdev->exposure_auto)
243                         return hdl->error;
244
245                 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
246                 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
247                                      READ_SHUTTER_FORMATTER, &def);
248                 if (r || def > 655)
249                         def = 655;
250                 pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
251                                         V4L2_CID_EXPOSURE, 0, 655, 1, def);
252                 /* CODEC2: separate auto gain & auto exposure */
253                 v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
254                 v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
255                                        V4L2_EXPOSURE_MANUAL, true);
256         } else if (DEVICE_USE_CODEC3(pdev->type)) {
257                 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
258                 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
259                                      READ_SHUTTER_FORMATTER, &def);
260                 if (r || def > 255)
261                         def = 255;
262                 pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
263                                         V4L2_CID_EXPOSURE, 0, 255, 1, def);
264                 /* CODEC3: both gain and exposure controlled by autogain */
265                 pdev->autogain_expo_cluster[0] = pdev->autogain;
266                 pdev->autogain_expo_cluster[1] = pdev->gain;
267                 pdev->autogain_expo_cluster[2] = pdev->exposure;
268                 v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
269                                        0, true);
270         }
271
272         /* color / bw setting */
273         r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
274                          &def);
275         if (r || (def != 0 && def != 0xff))
276                 def = 0xff;
277         /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
278         pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
279                                 V4L2_CID_COLORFX, 1, 0, def == 0);
280
281         /* autocontour, contour */
282         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
283         if (r || (def != 0 && def != 0xff))
284                 def = 0;
285         cfg = pwc_autocontour_cfg;
286         cfg.def = def == 0;
287         pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
288         if (!pdev->autocontour)
289                 return hdl->error;
290
291         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
292         if (r || def > 63)
293                 def = 31;
294         cfg = pwc_contour_cfg;
295         cfg.def = def;
296         pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
297
298         v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
299
300         /* backlight */
301         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
302                             BACK_LIGHT_COMPENSATION_FORMATTER, &def);
303         if (r || (def != 0 && def != 0xff))
304                 def = 0;
305         cfg = pwc_backlight_cfg;
306         cfg.name = v4l2_ctrl_get_name(cfg.id);
307         cfg.def = def == 0;
308         pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
309
310         /* flikker rediction */
311         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
312                             FLICKERLESS_MODE_FORMATTER, &def);
313         if (r || (def != 0 && def != 0xff))
314                 def = 0;
315         cfg = pwc_flicker_cfg;
316         cfg.name = v4l2_ctrl_get_name(cfg.id);
317         cfg.def = def == 0;
318         pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
319
320         /* Dynamic noise reduction */
321         r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
322                             DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
323         if (r || def > 3)
324                 def = 2;
325         cfg = pwc_noise_reduction_cfg;
326         cfg.def = def;
327         pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
328
329         /* Save / Restore User / Factory Settings */
330         pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
331         pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
332                                                   NULL);
333         if (pdev->restore_user)
334                 pdev->restore_user->flags = V4L2_CTRL_FLAG_UPDATE;
335         pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
336                                                      &pwc_restore_factory_cfg,
337                                                      NULL);
338         if (pdev->restore_factory)
339                 pdev->restore_factory->flags = V4L2_CTRL_FLAG_UPDATE;
340
341         return hdl->error;
342 }
343
344 static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
345 {
346         memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
347         f->fmt.pix.width        = pdev->view.x;
348         f->fmt.pix.height       = pdev->view.y;
349         f->fmt.pix.field        = V4L2_FIELD_NONE;
350         if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
351                 f->fmt.pix.pixelformat  = V4L2_PIX_FMT_YUV420;
352                 f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
353                 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
354         } else {
355                 /* vbandlength contains 4 lines ...  */
356                 f->fmt.pix.bytesperline = pdev->vbandlength/4;
357                 f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
358                 if (DEVICE_USE_CODEC1(pdev->type))
359                         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_PWC1;
360                 else
361                         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_PWC2;
362         }
363         PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
364                         "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
365                         f->fmt.pix.width,
366                         f->fmt.pix.height,
367                         f->fmt.pix.bytesperline,
368                         f->fmt.pix.sizeimage,
369                         (f->fmt.pix.pixelformat)&255,
370                         (f->fmt.pix.pixelformat>>8)&255,
371                         (f->fmt.pix.pixelformat>>16)&255,
372                         (f->fmt.pix.pixelformat>>24)&255);
373 }
374
375 /* ioctl(VIDIOC_TRY_FMT) */
376 static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
377 {
378         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
379                 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
380                 return -EINVAL;
381         }
382
383         switch (f->fmt.pix.pixelformat) {
384                 case V4L2_PIX_FMT_YUV420:
385                         break;
386                 case V4L2_PIX_FMT_PWC1:
387                         if (DEVICE_USE_CODEC23(pdev->type)) {
388                                 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
389                                 return -EINVAL;
390                         }
391                         break;
392                 case V4L2_PIX_FMT_PWC2:
393                         if (DEVICE_USE_CODEC1(pdev->type)) {
394                                 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
395                                 return -EINVAL;
396                         }
397                         break;
398                 default:
399                         PWC_DEBUG_IOCTL("Unsupported pixel format\n");
400                         return -EINVAL;
401
402         }
403
404         if (f->fmt.pix.width > pdev->view_max.x)
405                 f->fmt.pix.width = pdev->view_max.x;
406         else if (f->fmt.pix.width < pdev->view_min.x)
407                 f->fmt.pix.width = pdev->view_min.x;
408
409         if (f->fmt.pix.height > pdev->view_max.y)
410                 f->fmt.pix.height = pdev->view_max.y;
411         else if (f->fmt.pix.height < pdev->view_min.y)
412                 f->fmt.pix.height = pdev->view_min.y;
413
414         return 0;
415 }
416
417 /* ioctl(VIDIOC_SET_FMT) */
418
419 static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
420 {
421         struct pwc_device *pdev = video_drvdata(file);
422         int ret, fps, snapshot, compression, pixelformat;
423
424         if (!pdev->udev)
425                 return -ENODEV;
426
427         if (pdev->capt_file != NULL &&
428             pdev->capt_file != file)
429                 return -EBUSY;
430
431         pdev->capt_file = file;
432
433         ret = pwc_vidioc_try_fmt(pdev, f);
434         if (ret<0)
435                 return ret;
436
437         pixelformat = f->fmt.pix.pixelformat;
438         compression = pdev->vcompression;
439         snapshot = 0;
440         fps = pdev->vframes;
441         if (f->fmt.pix.priv) {
442                 compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
443                 snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
444                 fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
445                 if (fps == 0)
446                         fps = pdev->vframes;
447         }
448
449         if (pixelformat != V4L2_PIX_FMT_YUV420 &&
450             pixelformat != V4L2_PIX_FMT_PWC1 &&
451             pixelformat != V4L2_PIX_FMT_PWC2)
452                 return -EINVAL;
453
454         if (vb2_is_streaming(&pdev->vb_queue))
455                 return -EBUSY;
456
457         PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
458                         "compression=%d snapshot=%d format=%c%c%c%c\n",
459                         f->fmt.pix.width, f->fmt.pix.height, fps,
460                         compression, snapshot,
461                         (pixelformat)&255,
462                         (pixelformat>>8)&255,
463                         (pixelformat>>16)&255,
464                         (pixelformat>>24)&255);
465
466         ret = pwc_set_video_mode(pdev,
467                                  f->fmt.pix.width,
468                                  f->fmt.pix.height,
469                                  fps,
470                                  compression,
471                                  snapshot);
472
473         PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
474
475         if (ret)
476                 return ret;
477
478         pdev->pixfmt = pixelformat;
479
480         pwc_vidioc_fill_fmt(pdev, f);
481
482         return 0;
483
484 }
485
486 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
487 {
488         struct pwc_device *pdev = video_drvdata(file);
489
490         if (!pdev->udev)
491                 return -ENODEV;
492
493         strcpy(cap->driver, PWC_NAME);
494         strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
495         usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
496         cap->capabilities =
497                 V4L2_CAP_VIDEO_CAPTURE  |
498                 V4L2_CAP_STREAMING      |
499                 V4L2_CAP_READWRITE;
500         return 0;
501 }
502
503 static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
504 {
505         if (i->index)   /* Only one INPUT is supported */
506                 return -EINVAL;
507
508         strcpy(i->name, "usb");
509         return 0;
510 }
511
512 static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
513 {
514         *i = 0;
515         return 0;
516 }
517
518 static int pwc_s_input(struct file *file, void *fh, unsigned int i)
519 {
520         return i ? -EINVAL : 0;
521 }
522
523 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
524 {
525         struct pwc_device *pdev =
526                 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
527         int ret = 0;
528
529         if (!pdev->udev)
530                 return -ENODEV;
531
532         switch (ctrl->id) {
533         case V4L2_CID_AUTO_WHITE_BALANCE:
534                 if (pdev->color_bal_valid && time_before(jiffies,
535                                 pdev->last_color_bal_update + HZ / 4)) {
536                         pdev->red_balance->val  = pdev->last_red_balance;
537                         pdev->blue_balance->val = pdev->last_blue_balance;
538                         break;
539                 }
540                 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
541                                       READ_RED_GAIN_FORMATTER,
542                                       &pdev->red_balance->val);
543                 if (ret)
544                         break;
545                 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
546                                       READ_BLUE_GAIN_FORMATTER,
547                                       &pdev->blue_balance->val);
548                 if (ret)
549                         break;
550                 pdev->last_red_balance  = pdev->red_balance->val;
551                 pdev->last_blue_balance = pdev->blue_balance->val;
552                 pdev->last_color_bal_update = jiffies;
553                 pdev->color_bal_valid = true;
554                 break;
555         case V4L2_CID_AUTOGAIN:
556                 if (pdev->gain_valid && time_before(jiffies,
557                                 pdev->last_gain_update + HZ / 4)) {
558                         pdev->gain->val = pdev->last_gain;
559                         break;
560                 }
561                 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
562                                       READ_AGC_FORMATTER, &pdev->gain->val);
563                 if (ret)
564                         break;
565                 pdev->last_gain = pdev->gain->val;
566                 pdev->last_gain_update = jiffies;
567                 pdev->gain_valid = true;
568                 if (!DEVICE_USE_CODEC3(pdev->type))
569                         break;
570                 /* Fall through for CODEC3 where autogain also controls expo */
571         case V4L2_CID_EXPOSURE_AUTO:
572                 if (pdev->exposure_valid && time_before(jiffies,
573                                 pdev->last_exposure_update + HZ / 4)) {
574                         pdev->exposure->val = pdev->last_exposure;
575                         break;
576                 }
577                 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
578                                        READ_SHUTTER_FORMATTER,
579                                        &pdev->exposure->val);
580                 if (ret)
581                         break;
582                 pdev->last_exposure = pdev->exposure->val;
583                 pdev->last_exposure_update = jiffies;
584                 pdev->exposure_valid = true;
585                 break;
586         default:
587                 ret = -EINVAL;
588         }
589
590         if (ret)
591                 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
592
593         return ret;
594 }
595
596 static int pwc_set_awb(struct pwc_device *pdev)
597 {
598         int ret = 0;
599
600         if (pdev->auto_white_balance->is_new) {
601                 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
602                                       WB_MODE_FORMATTER,
603                                       pdev->auto_white_balance->val);
604                 if (ret)
605                         return ret;
606
607                 /* Update val when coming from auto or going to a preset */
608                 if (pdev->red_balance->is_volatile ||
609                     pdev->auto_white_balance->val == awb_indoor ||
610                     pdev->auto_white_balance->val == awb_outdoor ||
611                     pdev->auto_white_balance->val == awb_fl) {
612                         if (!pdev->red_balance->is_new)
613                                 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
614                                         READ_RED_GAIN_FORMATTER,
615                                         &pdev->red_balance->val);
616                         if (!pdev->blue_balance->is_new)
617                                 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
618                                         READ_BLUE_GAIN_FORMATTER,
619                                         &pdev->blue_balance->val);
620                 }
621                 if (pdev->auto_white_balance->val == awb_auto) {
622                         pdev->red_balance->is_volatile = true;
623                         pdev->blue_balance->is_volatile = true;
624                         pdev->color_bal_valid = false; /* Force cache update */
625                 } else {
626                         pdev->red_balance->is_volatile = false;
627                         pdev->blue_balance->is_volatile = false;
628                 }
629         }
630
631         if (ret == 0 && pdev->red_balance->is_new) {
632                 if (pdev->auto_white_balance->val != awb_manual)
633                         return -EBUSY;
634                 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
635                                       PRESET_MANUAL_RED_GAIN_FORMATTER,
636                                       pdev->red_balance->val);
637         }
638
639         if (ret == 0 && pdev->blue_balance->is_new) {
640                 if (pdev->auto_white_balance->val != awb_manual)
641                         return -EBUSY;
642                 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
643                                       PRESET_MANUAL_BLUE_GAIN_FORMATTER,
644                                       pdev->blue_balance->val);
645         }
646         return ret;
647 }
648
649 /* For CODEC2 models which have separate autogain and auto exposure */
650 static int pwc_set_autogain(struct pwc_device *pdev)
651 {
652         int ret = 0;
653
654         if (pdev->autogain->is_new) {
655                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
656                                       AGC_MODE_FORMATTER,
657                                       pdev->autogain->val ? 0 : 0xff);
658                 if (ret)
659                         return ret;
660                 if (pdev->autogain->val)
661                         pdev->gain_valid = false; /* Force cache update */
662                 else if (!pdev->gain->is_new)
663                         pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
664                                         READ_AGC_FORMATTER,
665                                         &pdev->gain->val);
666         }
667         if (ret == 0 && pdev->gain->is_new) {
668                 if (pdev->autogain->val)
669                         return -EBUSY;
670                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
671                                       PRESET_AGC_FORMATTER,
672                                       pdev->gain->val);
673         }
674         return ret;
675 }
676
677 /* For CODEC2 models which have separate autogain and auto exposure */
678 static int pwc_set_exposure_auto(struct pwc_device *pdev)
679 {
680         int ret = 0;
681         int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
682
683         if (pdev->exposure_auto->is_new) {
684                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
685                                       SHUTTER_MODE_FORMATTER,
686                                       is_auto ? 0 : 0xff);
687                 if (ret)
688                         return ret;
689                 if (is_auto)
690                         pdev->exposure_valid = false; /* Force cache update */
691                 else if (!pdev->exposure->is_new)
692                         pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
693                                          READ_SHUTTER_FORMATTER,
694                                          &pdev->exposure->val);
695         }
696         if (ret == 0 && pdev->exposure->is_new) {
697                 if (is_auto)
698                         return -EBUSY;
699                 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
700                                        PRESET_SHUTTER_FORMATTER,
701                                        pdev->exposure->val);
702         }
703         return ret;
704 }
705
706 /* For CODEC3 models which have autogain controlling both gain and exposure */
707 static int pwc_set_autogain_expo(struct pwc_device *pdev)
708 {
709         int ret = 0;
710
711         if (pdev->autogain->is_new) {
712                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
713                                       AGC_MODE_FORMATTER,
714                                       pdev->autogain->val ? 0 : 0xff);
715                 if (ret)
716                         return ret;
717                 if (pdev->autogain->val) {
718                         pdev->gain_valid     = false; /* Force cache update */
719                         pdev->exposure_valid = false; /* Force cache update */
720                 } else {
721                         if (!pdev->gain->is_new)
722                                 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
723                                                 READ_AGC_FORMATTER,
724                                                 &pdev->gain->val);
725                         if (!pdev->exposure->is_new)
726                                 pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
727                                                  READ_SHUTTER_FORMATTER,
728                                                  &pdev->exposure->val);
729                 }
730         }
731         if (ret == 0 && pdev->gain->is_new) {
732                 if (pdev->autogain->val)
733                         return -EBUSY;
734                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
735                                       PRESET_AGC_FORMATTER,
736                                       pdev->gain->val);
737         }
738         if (ret == 0 && pdev->exposure->is_new) {
739                 if (pdev->autogain->val)
740                         return -EBUSY;
741                 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
742                                        PRESET_SHUTTER_FORMATTER,
743                                        pdev->exposure->val);
744         }
745         return ret;
746 }
747
748 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
749 {
750         struct pwc_device *pdev =
751                 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
752         int ret = 0;
753
754         if (!pdev->udev)
755                 return -ENODEV;
756
757         switch (ctrl->id) {
758         case V4L2_CID_BRIGHTNESS:
759                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
760                                       BRIGHTNESS_FORMATTER, ctrl->val);
761                 break;
762         case V4L2_CID_CONTRAST:
763                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
764                                       CONTRAST_FORMATTER, ctrl->val);
765                 break;
766         case V4L2_CID_SATURATION:
767                 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
768                                       pdev->saturation_fmt, ctrl->val);
769                 break;
770         case V4L2_CID_GAMMA:
771                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
772                                       GAMMA_FORMATTER, ctrl->val);
773                 break;
774         case V4L2_CID_AUTO_WHITE_BALANCE:
775                 ret = pwc_set_awb(pdev);
776                 break;
777         case V4L2_CID_AUTOGAIN:
778                 if (DEVICE_USE_CODEC2(pdev->type))
779                         ret = pwc_set_autogain(pdev);
780                 else if (DEVICE_USE_CODEC3(pdev->type))
781                         ret = pwc_set_autogain_expo(pdev);
782                 else
783                         ret = -EINVAL;
784                 break;
785         case V4L2_CID_EXPOSURE_AUTO:
786                 if (DEVICE_USE_CODEC2(pdev->type))
787                         ret = pwc_set_exposure_auto(pdev);
788                 else
789                         ret = -EINVAL;
790                 break;
791         case V4L2_CID_COLORFX:
792                 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
793                                       COLOUR_MODE_FORMATTER,
794                                       ctrl->val ? 0 : 0xff);
795                 break;
796         case PWC_CID_CUSTOM(autocontour):
797                 if (pdev->autocontour->is_new) {
798                         ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
799                                         AUTO_CONTOUR_FORMATTER,
800                                         pdev->autocontour->val ? 0 : 0xff);
801                 }
802                 if (ret == 0 && pdev->contour->is_new) {
803                         if (pdev->autocontour->val) {
804                                 ret = -EBUSY;
805                                 break;
806                         }
807                         ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
808                                               PRESET_CONTOUR_FORMATTER,
809                                               pdev->contour->val);
810                 }
811                 break;
812         case V4L2_CID_BACKLIGHT_COMPENSATION:
813                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
814                                       BACK_LIGHT_COMPENSATION_FORMATTER,
815                                       ctrl->val ? 0 : 0xff);
816                 break;
817         case V4L2_CID_BAND_STOP_FILTER:
818                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
819                                       FLICKERLESS_MODE_FORMATTER,
820                                       ctrl->val ? 0 : 0xff);
821                 break;
822         case PWC_CID_CUSTOM(noise_reduction):
823                 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
824                                       DYNAMIC_NOISE_CONTROL_FORMATTER,
825                                       ctrl->val);
826                 break;
827         case PWC_CID_CUSTOM(save_user):
828                 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
829                 break;
830         case PWC_CID_CUSTOM(restore_user):
831                 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
832                 break;
833         case PWC_CID_CUSTOM(restore_factory):
834                 ret = pwc_button_ctrl(pdev,
835                                       RESTORE_FACTORY_DEFAULTS_FORMATTER);
836                 break;
837         default:
838                 ret = -EINVAL;
839         }
840
841         if (ret)
842                 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
843
844         return ret;
845 }
846
847 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
848 {
849         struct pwc_device *pdev = video_drvdata(file);
850
851         /* We only support two format: the raw format, and YUV */
852         switch (f->index) {
853         case 0:
854                 /* RAW format */
855                 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
856                 f->flags = V4L2_FMT_FLAG_COMPRESSED;
857                 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
858                 break;
859         case 1:
860                 f->pixelformat = V4L2_PIX_FMT_YUV420;
861                 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
862                 break;
863         default:
864                 return -EINVAL;
865         }
866         return 0;
867 }
868
869 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
870 {
871         struct pwc_device *pdev = video_drvdata(file);
872
873         PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
874                         pdev->image.x, pdev->image.y);
875         pwc_vidioc_fill_fmt(pdev, f);
876         return 0;
877 }
878
879 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
880 {
881         struct pwc_device *pdev = video_drvdata(file);
882
883         return pwc_vidioc_try_fmt(pdev, f);
884 }
885
886 static int pwc_reqbufs(struct file *file, void *fh,
887                        struct v4l2_requestbuffers *rb)
888 {
889         struct pwc_device *pdev = video_drvdata(file);
890
891         if (pdev->capt_file != NULL &&
892             pdev->capt_file != file)
893                 return -EBUSY;
894
895         pdev->capt_file = file;
896
897         return vb2_reqbufs(&pdev->vb_queue, rb);
898 }
899
900 static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
901 {
902         struct pwc_device *pdev = video_drvdata(file);
903
904         return vb2_querybuf(&pdev->vb_queue, buf);
905 }
906
907 static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
908 {
909         struct pwc_device *pdev = video_drvdata(file);
910
911         if (!pdev->udev)
912                 return -ENODEV;
913
914         if (pdev->capt_file != file)
915                 return -EBUSY;
916
917         return vb2_qbuf(&pdev->vb_queue, buf);
918 }
919
920 static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
921 {
922         struct pwc_device *pdev = video_drvdata(file);
923
924         if (!pdev->udev)
925                 return -ENODEV;
926
927         if (pdev->capt_file != file)
928                 return -EBUSY;
929
930         return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
931 }
932
933 static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
934 {
935         struct pwc_device *pdev = video_drvdata(file);
936
937         if (!pdev->udev)
938                 return -ENODEV;
939
940         if (pdev->capt_file != file)
941                 return -EBUSY;
942
943         return vb2_streamon(&pdev->vb_queue, i);
944 }
945
946 static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
947 {
948         struct pwc_device *pdev = video_drvdata(file);
949
950         if (!pdev->udev)
951                 return -ENODEV;
952
953         if (pdev->capt_file != file)
954                 return -EBUSY;
955
956         return vb2_streamoff(&pdev->vb_queue, i);
957 }
958
959 static int pwc_enum_framesizes(struct file *file, void *fh,
960                                          struct v4l2_frmsizeenum *fsize)
961 {
962         struct pwc_device *pdev = video_drvdata(file);
963         unsigned int i = 0, index = fsize->index;
964
965         if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
966                 for (i = 0; i < PSZ_MAX; i++) {
967                         if (pdev->image_mask & (1UL << i)) {
968                                 if (!index--) {
969                                         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
970                                         fsize->discrete.width = pwc_image_sizes[i].x;
971                                         fsize->discrete.height = pwc_image_sizes[i].y;
972                                         return 0;
973                                 }
974                         }
975                 }
976         } else if (fsize->index == 0 &&
977                         ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
978                          (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
979
980                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
981                 fsize->discrete.width = pdev->abs_max.x;
982                 fsize->discrete.height = pdev->abs_max.y;
983                 return 0;
984         }
985         return -EINVAL;
986 }
987
988 static int pwc_enum_frameintervals(struct file *file, void *fh,
989                                            struct v4l2_frmivalenum *fival)
990 {
991         struct pwc_device *pdev = video_drvdata(file);
992         int size = -1;
993         unsigned int i;
994
995         for (i = 0; i < PSZ_MAX; i++) {
996                 if (pwc_image_sizes[i].x == fival->width &&
997                                 pwc_image_sizes[i].y == fival->height) {
998                         size = i;
999                         break;
1000                 }
1001         }
1002
1003         /* TODO: Support raw format */
1004         if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1005                 return -EINVAL;
1006
1007         i = pwc_get_fps(pdev, fival->index, size);
1008         if (!i)
1009                 return -EINVAL;
1010
1011         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1012         fival->discrete.numerator = 1;
1013         fival->discrete.denominator = i;
1014
1015         return 0;
1016 }
1017
1018 static long pwc_default(struct file *file, void *fh, bool valid_prio,
1019                         int cmd, void *arg)
1020 {
1021         struct pwc_device *pdev = video_drvdata(file);
1022
1023         return pwc_ioctl(pdev, cmd, arg);
1024 }
1025
1026 const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1027         .vidioc_querycap                    = pwc_querycap,
1028         .vidioc_enum_input                  = pwc_enum_input,
1029         .vidioc_g_input                     = pwc_g_input,
1030         .vidioc_s_input                     = pwc_s_input,
1031         .vidioc_enum_fmt_vid_cap            = pwc_enum_fmt_vid_cap,
1032         .vidioc_g_fmt_vid_cap               = pwc_g_fmt_vid_cap,
1033         .vidioc_s_fmt_vid_cap               = pwc_s_fmt_vid_cap,
1034         .vidioc_try_fmt_vid_cap             = pwc_try_fmt_vid_cap,
1035         .vidioc_reqbufs                     = pwc_reqbufs,
1036         .vidioc_querybuf                    = pwc_querybuf,
1037         .vidioc_qbuf                        = pwc_qbuf,
1038         .vidioc_dqbuf                       = pwc_dqbuf,
1039         .vidioc_streamon                    = pwc_streamon,
1040         .vidioc_streamoff                   = pwc_streamoff,
1041         .vidioc_enum_framesizes             = pwc_enum_framesizes,
1042         .vidioc_enum_frameintervals         = pwc_enum_frameintervals,
1043         .vidioc_default             = pwc_default,
1044 };
1045
1046
1047 /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */