Loading...
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
42static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
43static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
44
45static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
46 .g_volatile_ctrl = pwc_g_volatile_ctrl,
47 .s_ctrl = pwc_s_ctrl,
48};
49
50enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
51enum { custom_autocontour, custom_contour, custom_noise_reduction,
52 custom_awb_speed, custom_awb_delay,
53 custom_save_user, custom_restore_user, custom_restore_factory };
54
55const char * const pwc_auto_whitebal_qmenu[] = {
56 "Indoor (Incandescant Lighting) Mode",
57 "Outdoor (Sunlight) Mode",
58 "Indoor (Fluorescent Lighting) Mode",
59 "Manual Mode",
60 "Auto Mode",
61 NULL
62};
63
64static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
65 .ops = &pwc_ctrl_ops,
66 .id = V4L2_CID_AUTO_WHITE_BALANCE,
67 .type = V4L2_CTRL_TYPE_MENU,
68 .max = awb_auto,
69 .qmenu = pwc_auto_whitebal_qmenu,
70};
71
72static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
73 .ops = &pwc_ctrl_ops,
74 .id = PWC_CID_CUSTOM(autocontour),
75 .type = V4L2_CTRL_TYPE_BOOLEAN,
76 .name = "Auto contour",
77 .min = 0,
78 .max = 1,
79 .step = 1,
80};
81
82static const struct v4l2_ctrl_config pwc_contour_cfg = {
83 .ops = &pwc_ctrl_ops,
84 .id = PWC_CID_CUSTOM(contour),
85 .type = V4L2_CTRL_TYPE_INTEGER,
86 .name = "Contour",
87 .flags = V4L2_CTRL_FLAG_SLIDER,
88 .min = 0,
89 .max = 63,
90 .step = 1,
91};
92
93static const struct v4l2_ctrl_config pwc_backlight_cfg = {
94 .ops = &pwc_ctrl_ops,
95 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
96 .type = V4L2_CTRL_TYPE_BOOLEAN,
97 .min = 0,
98 .max = 1,
99 .step = 1,
100};
101
102static const struct v4l2_ctrl_config pwc_flicker_cfg = {
103 .ops = &pwc_ctrl_ops,
104 .id = V4L2_CID_BAND_STOP_FILTER,
105 .type = V4L2_CTRL_TYPE_BOOLEAN,
106 .min = 0,
107 .max = 1,
108 .step = 1,
109};
110
111static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
112 .ops = &pwc_ctrl_ops,
113 .id = PWC_CID_CUSTOM(noise_reduction),
114 .type = V4L2_CTRL_TYPE_INTEGER,
115 .name = "Dynamic Noise Reduction",
116 .min = 0,
117 .max = 3,
118 .step = 1,
119};
120
121static const struct v4l2_ctrl_config pwc_save_user_cfg = {
122 .ops = &pwc_ctrl_ops,
123 .id = PWC_CID_CUSTOM(save_user),
124 .type = V4L2_CTRL_TYPE_BUTTON,
125 .name = "Save User Settings",
126};
127
128static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
129 .ops = &pwc_ctrl_ops,
130 .id = PWC_CID_CUSTOM(restore_user),
131 .type = V4L2_CTRL_TYPE_BUTTON,
132 .name = "Restore User Settings",
133};
134
135static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
136 .ops = &pwc_ctrl_ops,
137 .id = PWC_CID_CUSTOM(restore_factory),
138 .type = V4L2_CTRL_TYPE_BUTTON,
139 .name = "Restore Factory Settings",
140};
141
142static const struct v4l2_ctrl_config pwc_awb_speed_cfg = {
143 .ops = &pwc_ctrl_ops,
144 .id = PWC_CID_CUSTOM(awb_speed),
145 .type = V4L2_CTRL_TYPE_INTEGER,
146 .name = "Auto White Balance Speed",
147 .min = 1,
148 .max = 32,
149 .step = 1,
150};
151
152static const struct v4l2_ctrl_config pwc_awb_delay_cfg = {
153 .ops = &pwc_ctrl_ops,
154 .id = PWC_CID_CUSTOM(awb_delay),
155 .type = V4L2_CTRL_TYPE_INTEGER,
156 .name = "Auto White Balance Delay",
157 .min = 0,
158 .max = 63,
159 .step = 1,
160};
161
162int pwc_init_controls(struct pwc_device *pdev)
163{
164 struct v4l2_ctrl_handler *hdl;
165 struct v4l2_ctrl_config cfg;
166 int r, def;
167
168 hdl = &pdev->ctrl_handler;
169 r = v4l2_ctrl_handler_init(hdl, 20);
170 if (r)
171 return r;
172
173 /* Brightness, contrast, saturation, gamma */
174 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
175 if (r || def > 127)
176 def = 63;
177 pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
178 V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
179
180 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
181 if (r || def > 63)
182 def = 31;
183 pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
184 V4L2_CID_CONTRAST, 0, 63, 1, def);
185
186 if (pdev->type >= 675) {
187 if (pdev->type < 730)
188 pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
189 else
190 pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
191 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
192 &def);
193 if (r || def < -100 || def > 100)
194 def = 0;
195 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
196 V4L2_CID_SATURATION, -100, 100, 1, def);
197 }
198
199 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
200 if (r || def > 31)
201 def = 15;
202 pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
203 V4L2_CID_GAMMA, 0, 31, 1, def);
204
205 /* auto white balance, red gain, blue gain */
206 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
207 if (r || def > awb_auto)
208 def = awb_auto;
209 cfg = pwc_auto_white_balance_cfg;
210 cfg.name = v4l2_ctrl_get_name(cfg.id);
211 cfg.def = def;
212 pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
213 /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
214 if (!pdev->auto_white_balance)
215 return hdl->error;
216
217 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
218 PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
219 if (r)
220 def = 127;
221 pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
222 V4L2_CID_RED_BALANCE, 0, 255, 1, def);
223
224 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
225 PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
226 if (r)
227 def = 127;
228 pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
229 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
230
231 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true);
232
233 /* autogain, gain */
234 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
235 if (r || (def != 0 && def != 0xff))
236 def = 0;
237 /* Note a register value if 0 means auto gain is on */
238 pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
239 V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
240 if (!pdev->autogain)
241 return hdl->error;
242
243 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
244 if (r || def > 63)
245 def = 31;
246 pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
247 V4L2_CID_GAIN, 0, 63, 1, def);
248
249 /* auto exposure, exposure */
250 if (DEVICE_USE_CODEC2(pdev->type)) {
251 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
252 &def);
253 if (r || (def != 0 && def != 0xff))
254 def = 0;
255 /*
256 * def = 0 auto, def = ff manual
257 * menu idx 0 = auto, idx 1 = manual
258 */
259 pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
260 &pwc_ctrl_ops,
261 V4L2_CID_EXPOSURE_AUTO,
262 1, 0, def != 0);
263 if (!pdev->exposure_auto)
264 return hdl->error;
265
266 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
267 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
268 READ_SHUTTER_FORMATTER, &def);
269 if (r || def > 655)
270 def = 655;
271 pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
272 V4L2_CID_EXPOSURE, 0, 655, 1, def);
273 /* CODEC2: separate auto gain & auto exposure */
274 v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
275 v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
276 V4L2_EXPOSURE_MANUAL, true);
277 } else if (DEVICE_USE_CODEC3(pdev->type)) {
278 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
279 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
280 READ_SHUTTER_FORMATTER, &def);
281 if (r || def > 255)
282 def = 255;
283 pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
284 V4L2_CID_EXPOSURE, 0, 255, 1, def);
285 /* CODEC3: both gain and exposure controlled by autogain */
286 pdev->autogain_expo_cluster[0] = pdev->autogain;
287 pdev->autogain_expo_cluster[1] = pdev->gain;
288 pdev->autogain_expo_cluster[2] = pdev->exposure;
289 v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
290 0, true);
291 }
292
293 /* color / bw setting */
294 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
295 &def);
296 if (r || (def != 0 && def != 0xff))
297 def = 0xff;
298 /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
299 pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
300 V4L2_CID_COLORFX, 1, 0, def == 0);
301
302 /* autocontour, contour */
303 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
304 if (r || (def != 0 && def != 0xff))
305 def = 0;
306 cfg = pwc_autocontour_cfg;
307 cfg.def = def == 0;
308 pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
309 if (!pdev->autocontour)
310 return hdl->error;
311
312 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
313 if (r || def > 63)
314 def = 31;
315 cfg = pwc_contour_cfg;
316 cfg.def = def;
317 pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
318
319 v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
320
321 /* backlight */
322 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
323 BACK_LIGHT_COMPENSATION_FORMATTER, &def);
324 if (r || (def != 0 && def != 0xff))
325 def = 0;
326 cfg = pwc_backlight_cfg;
327 cfg.name = v4l2_ctrl_get_name(cfg.id);
328 cfg.def = def == 0;
329 pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
330
331 /* flikker rediction */
332 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
333 FLICKERLESS_MODE_FORMATTER, &def);
334 if (r || (def != 0 && def != 0xff))
335 def = 0;
336 cfg = pwc_flicker_cfg;
337 cfg.name = v4l2_ctrl_get_name(cfg.id);
338 cfg.def = def == 0;
339 pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
340
341 /* Dynamic noise reduction */
342 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
343 DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
344 if (r || def > 3)
345 def = 2;
346 cfg = pwc_noise_reduction_cfg;
347 cfg.def = def;
348 pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
349
350 /* Save / Restore User / Factory Settings */
351 pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
352 pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
353 NULL);
354 if (pdev->restore_user)
355 pdev->restore_user->flags |= V4L2_CTRL_FLAG_UPDATE;
356 pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
357 &pwc_restore_factory_cfg,
358 NULL);
359 if (pdev->restore_factory)
360 pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE;
361
362 /* Auto White Balance speed & delay */
363 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
364 AWB_CONTROL_SPEED_FORMATTER, &def);
365 if (r || def < 1 || def > 32)
366 def = 1;
367 cfg = pwc_awb_speed_cfg;
368 cfg.def = def;
369 pdev->awb_speed = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
370
371 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
372 AWB_CONTROL_DELAY_FORMATTER, &def);
373 if (r || def > 63)
374 def = 0;
375 cfg = pwc_awb_delay_cfg;
376 cfg.def = def;
377 pdev->awb_delay = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
378
379 if (!(pdev->features & FEATURE_MOTOR_PANTILT))
380 return hdl->error;
381
382 /* Motor pan / tilt / reset */
383 pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
384 V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0);
385 if (!pdev->motor_pan)
386 return hdl->error;
387 pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
388 V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0);
389 pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
390 V4L2_CID_PAN_RESET, 0, 0, 0, 0);
391 pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
392 V4L2_CID_TILT_RESET, 0, 0, 0, 0);
393 v4l2_ctrl_cluster(4, &pdev->motor_pan);
394
395 return hdl->error;
396}
397
398static void pwc_vidioc_fill_fmt(struct v4l2_format *f,
399 int width, int height, u32 pixfmt)
400{
401 memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
402 f->fmt.pix.width = width;
403 f->fmt.pix.height = height;
404 f->fmt.pix.field = V4L2_FIELD_NONE;
405 f->fmt.pix.pixelformat = pixfmt;
406 f->fmt.pix.bytesperline = f->fmt.pix.width;
407 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width * 3 / 2;
408 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
409 "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
410 f->fmt.pix.width,
411 f->fmt.pix.height,
412 f->fmt.pix.bytesperline,
413 f->fmt.pix.sizeimage,
414 (f->fmt.pix.pixelformat)&255,
415 (f->fmt.pix.pixelformat>>8)&255,
416 (f->fmt.pix.pixelformat>>16)&255,
417 (f->fmt.pix.pixelformat>>24)&255);
418}
419
420/* ioctl(VIDIOC_TRY_FMT) */
421static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
422{
423 int size;
424
425 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
426 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
427 return -EINVAL;
428 }
429
430 switch (f->fmt.pix.pixelformat) {
431 case V4L2_PIX_FMT_YUV420:
432 break;
433 case V4L2_PIX_FMT_PWC1:
434 if (DEVICE_USE_CODEC23(pdev->type)) {
435 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
436 return -EINVAL;
437 }
438 break;
439 case V4L2_PIX_FMT_PWC2:
440 if (DEVICE_USE_CODEC1(pdev->type)) {
441 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
442 return -EINVAL;
443 }
444 break;
445 default:
446 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
447 return -EINVAL;
448
449 }
450
451 size = pwc_get_size(pdev, f->fmt.pix.width, f->fmt.pix.height);
452 pwc_vidioc_fill_fmt(f,
453 pwc_image_sizes[size][0],
454 pwc_image_sizes[size][1],
455 f->fmt.pix.pixelformat);
456
457 return 0;
458}
459
460/* ioctl(VIDIOC_SET_FMT) */
461
462static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
463{
464 struct pwc_device *pdev = video_drvdata(file);
465 int ret, pixelformat, compression = 0;
466
467 ret = pwc_vidioc_try_fmt(pdev, f);
468 if (ret < 0)
469 return ret;
470
471 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
472 return -ERESTARTSYS;
473
474 ret = pwc_test_n_set_capt_file(pdev, file);
475 if (ret)
476 goto leave;
477
478 if (pdev->vb_queue.streaming) {
479 ret = -EBUSY;
480 goto leave;
481 }
482
483 pixelformat = f->fmt.pix.pixelformat;
484
485 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
486 "format=%c%c%c%c\n",
487 f->fmt.pix.width, f->fmt.pix.height, pdev->vframes,
488 (pixelformat)&255,
489 (pixelformat>>8)&255,
490 (pixelformat>>16)&255,
491 (pixelformat>>24)&255);
492
493 ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height,
494 pixelformat, 30, &compression, 0);
495
496 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
497
498 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
499leave:
500 mutex_unlock(&pdev->vb_queue_lock);
501 return ret;
502}
503
504static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
505{
506 struct pwc_device *pdev = video_drvdata(file);
507
508 strcpy(cap->driver, PWC_NAME);
509 strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
510 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
511 cap->capabilities =
512 V4L2_CAP_VIDEO_CAPTURE |
513 V4L2_CAP_STREAMING |
514 V4L2_CAP_READWRITE;
515 return 0;
516}
517
518static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
519{
520 if (i->index) /* Only one INPUT is supported */
521 return -EINVAL;
522
523 strcpy(i->name, "usb");
524 return 0;
525}
526
527static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
528{
529 *i = 0;
530 return 0;
531}
532
533static int pwc_s_input(struct file *file, void *fh, unsigned int i)
534{
535 return i ? -EINVAL : 0;
536}
537
538static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
539{
540 struct pwc_device *pdev =
541 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
542 int ret = 0;
543
544 switch (ctrl->id) {
545 case V4L2_CID_AUTO_WHITE_BALANCE:
546 if (pdev->color_bal_valid &&
547 (pdev->auto_white_balance->val != awb_auto ||
548 time_before(jiffies,
549 pdev->last_color_bal_update + HZ / 4))) {
550 pdev->red_balance->val = pdev->last_red_balance;
551 pdev->blue_balance->val = pdev->last_blue_balance;
552 break;
553 }
554 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
555 READ_RED_GAIN_FORMATTER,
556 &pdev->red_balance->val);
557 if (ret)
558 break;
559 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
560 READ_BLUE_GAIN_FORMATTER,
561 &pdev->blue_balance->val);
562 if (ret)
563 break;
564 pdev->last_red_balance = pdev->red_balance->val;
565 pdev->last_blue_balance = pdev->blue_balance->val;
566 pdev->last_color_bal_update = jiffies;
567 pdev->color_bal_valid = true;
568 break;
569 case V4L2_CID_AUTOGAIN:
570 if (pdev->gain_valid && time_before(jiffies,
571 pdev->last_gain_update + HZ / 4)) {
572 pdev->gain->val = pdev->last_gain;
573 break;
574 }
575 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
576 READ_AGC_FORMATTER, &pdev->gain->val);
577 if (ret)
578 break;
579 pdev->last_gain = pdev->gain->val;
580 pdev->last_gain_update = jiffies;
581 pdev->gain_valid = true;
582 if (!DEVICE_USE_CODEC3(pdev->type))
583 break;
584 /* Fall through for CODEC3 where autogain also controls expo */
585 case V4L2_CID_EXPOSURE_AUTO:
586 if (pdev->exposure_valid && time_before(jiffies,
587 pdev->last_exposure_update + HZ / 4)) {
588 pdev->exposure->val = pdev->last_exposure;
589 break;
590 }
591 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
592 READ_SHUTTER_FORMATTER,
593 &pdev->exposure->val);
594 if (ret)
595 break;
596 pdev->last_exposure = pdev->exposure->val;
597 pdev->last_exposure_update = jiffies;
598 pdev->exposure_valid = true;
599 break;
600 default:
601 ret = -EINVAL;
602 }
603
604 if (ret)
605 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
606
607 return ret;
608}
609
610static int pwc_set_awb(struct pwc_device *pdev)
611{
612 int ret;
613
614 if (pdev->auto_white_balance->is_new) {
615 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
616 WB_MODE_FORMATTER,
617 pdev->auto_white_balance->val);
618 if (ret)
619 return ret;
620
621 if (pdev->auto_white_balance->val != awb_manual)
622 pdev->color_bal_valid = false; /* Force cache update */
623
624 /*
625 * If this is a preset, update our red / blue balance values
626 * so that events get generated for the new preset values
627 */
628 if (pdev->auto_white_balance->val == awb_indoor ||
629 pdev->auto_white_balance->val == awb_outdoor ||
630 pdev->auto_white_balance->val == awb_fl)
631 pwc_g_volatile_ctrl(pdev->auto_white_balance);
632 }
633 if (pdev->auto_white_balance->val != awb_manual)
634 return 0;
635
636 if (pdev->red_balance->is_new) {
637 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
638 PRESET_MANUAL_RED_GAIN_FORMATTER,
639 pdev->red_balance->val);
640 if (ret)
641 return ret;
642 }
643
644 if (pdev->blue_balance->is_new) {
645 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
646 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
647 pdev->blue_balance->val);
648 if (ret)
649 return ret;
650 }
651 return 0;
652}
653
654/* For CODEC2 models which have separate autogain and auto exposure */
655static int pwc_set_autogain(struct pwc_device *pdev)
656{
657 int ret;
658
659 if (pdev->autogain->is_new) {
660 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
661 AGC_MODE_FORMATTER,
662 pdev->autogain->val ? 0 : 0xff);
663 if (ret)
664 return ret;
665
666 if (pdev->autogain->val)
667 pdev->gain_valid = false; /* Force cache update */
668 }
669
670 if (pdev->autogain->val)
671 return 0;
672
673 if (pdev->gain->is_new) {
674 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
675 PRESET_AGC_FORMATTER,
676 pdev->gain->val);
677 if (ret)
678 return ret;
679 }
680 return 0;
681}
682
683/* For CODEC2 models which have separate autogain and auto exposure */
684static int pwc_set_exposure_auto(struct pwc_device *pdev)
685{
686 int ret;
687 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
688
689 if (pdev->exposure_auto->is_new) {
690 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
691 SHUTTER_MODE_FORMATTER,
692 is_auto ? 0 : 0xff);
693 if (ret)
694 return ret;
695
696 if (is_auto)
697 pdev->exposure_valid = false; /* Force cache update */
698 }
699
700 if (is_auto)
701 return 0;
702
703 if (pdev->exposure->is_new) {
704 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
705 PRESET_SHUTTER_FORMATTER,
706 pdev->exposure->val);
707 if (ret)
708 return ret;
709 }
710 return 0;
711}
712
713/* For CODEC3 models which have autogain controlling both gain and exposure */
714static int pwc_set_autogain_expo(struct pwc_device *pdev)
715{
716 int ret;
717
718 if (pdev->autogain->is_new) {
719 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
720 AGC_MODE_FORMATTER,
721 pdev->autogain->val ? 0 : 0xff);
722 if (ret)
723 return ret;
724
725 if (pdev->autogain->val) {
726 pdev->gain_valid = false; /* Force cache update */
727 pdev->exposure_valid = false; /* Force cache update */
728 }
729 }
730
731 if (pdev->autogain->val)
732 return 0;
733
734 if (pdev->gain->is_new) {
735 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
736 PRESET_AGC_FORMATTER,
737 pdev->gain->val);
738 if (ret)
739 return ret;
740 }
741
742 if (pdev->exposure->is_new) {
743 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
744 PRESET_SHUTTER_FORMATTER,
745 pdev->exposure->val);
746 if (ret)
747 return ret;
748 }
749 return 0;
750}
751
752static int pwc_set_motor(struct pwc_device *pdev)
753{
754 int ret;
755
756 pdev->ctrl_buf[0] = 0;
757 if (pdev->motor_pan_reset->is_new)
758 pdev->ctrl_buf[0] |= 0x01;
759 if (pdev->motor_tilt_reset->is_new)
760 pdev->ctrl_buf[0] |= 0x02;
761 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
762 ret = send_control_msg(pdev, SET_MPT_CTL,
763 PT_RESET_CONTROL_FORMATTER,
764 pdev->ctrl_buf, 1);
765 if (ret < 0)
766 return ret;
767 }
768
769 memset(pdev->ctrl_buf, 0, 4);
770 if (pdev->motor_pan->is_new) {
771 pdev->ctrl_buf[0] = pdev->motor_pan->val & 0xFF;
772 pdev->ctrl_buf[1] = (pdev->motor_pan->val >> 8);
773 }
774 if (pdev->motor_tilt->is_new) {
775 pdev->ctrl_buf[2] = pdev->motor_tilt->val & 0xFF;
776 pdev->ctrl_buf[3] = (pdev->motor_tilt->val >> 8);
777 }
778 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
779 ret = send_control_msg(pdev, SET_MPT_CTL,
780 PT_RELATIVE_CONTROL_FORMATTER,
781 pdev->ctrl_buf, 4);
782 if (ret < 0)
783 return ret;
784 }
785
786 return 0;
787}
788
789static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
790{
791 struct pwc_device *pdev =
792 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
793 int ret = 0;
794
795 switch (ctrl->id) {
796 case V4L2_CID_BRIGHTNESS:
797 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
798 BRIGHTNESS_FORMATTER, ctrl->val);
799 break;
800 case V4L2_CID_CONTRAST:
801 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
802 CONTRAST_FORMATTER, ctrl->val);
803 break;
804 case V4L2_CID_SATURATION:
805 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
806 pdev->saturation_fmt, ctrl->val);
807 break;
808 case V4L2_CID_GAMMA:
809 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
810 GAMMA_FORMATTER, ctrl->val);
811 break;
812 case V4L2_CID_AUTO_WHITE_BALANCE:
813 ret = pwc_set_awb(pdev);
814 break;
815 case V4L2_CID_AUTOGAIN:
816 if (DEVICE_USE_CODEC2(pdev->type))
817 ret = pwc_set_autogain(pdev);
818 else if (DEVICE_USE_CODEC3(pdev->type))
819 ret = pwc_set_autogain_expo(pdev);
820 else
821 ret = -EINVAL;
822 break;
823 case V4L2_CID_EXPOSURE_AUTO:
824 if (DEVICE_USE_CODEC2(pdev->type))
825 ret = pwc_set_exposure_auto(pdev);
826 else
827 ret = -EINVAL;
828 break;
829 case V4L2_CID_COLORFX:
830 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
831 COLOUR_MODE_FORMATTER,
832 ctrl->val ? 0 : 0xff);
833 break;
834 case PWC_CID_CUSTOM(autocontour):
835 if (pdev->autocontour->is_new) {
836 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
837 AUTO_CONTOUR_FORMATTER,
838 pdev->autocontour->val ? 0 : 0xff);
839 }
840 if (ret == 0 && pdev->contour->is_new) {
841 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
842 PRESET_CONTOUR_FORMATTER,
843 pdev->contour->val);
844 }
845 break;
846 case V4L2_CID_BACKLIGHT_COMPENSATION:
847 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
848 BACK_LIGHT_COMPENSATION_FORMATTER,
849 ctrl->val ? 0 : 0xff);
850 break;
851 case V4L2_CID_BAND_STOP_FILTER:
852 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
853 FLICKERLESS_MODE_FORMATTER,
854 ctrl->val ? 0 : 0xff);
855 break;
856 case PWC_CID_CUSTOM(noise_reduction):
857 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
858 DYNAMIC_NOISE_CONTROL_FORMATTER,
859 ctrl->val);
860 break;
861 case PWC_CID_CUSTOM(save_user):
862 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
863 break;
864 case PWC_CID_CUSTOM(restore_user):
865 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
866 break;
867 case PWC_CID_CUSTOM(restore_factory):
868 ret = pwc_button_ctrl(pdev,
869 RESTORE_FACTORY_DEFAULTS_FORMATTER);
870 break;
871 case PWC_CID_CUSTOM(awb_speed):
872 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
873 AWB_CONTROL_SPEED_FORMATTER,
874 ctrl->val);
875 break;
876 case PWC_CID_CUSTOM(awb_delay):
877 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
878 AWB_CONTROL_DELAY_FORMATTER,
879 ctrl->val);
880 break;
881 case V4L2_CID_PAN_RELATIVE:
882 ret = pwc_set_motor(pdev);
883 break;
884 default:
885 ret = -EINVAL;
886 }
887
888 if (ret)
889 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
890
891 return ret;
892}
893
894static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
895{
896 struct pwc_device *pdev = video_drvdata(file);
897
898 /* We only support two format: the raw format, and YUV */
899 switch (f->index) {
900 case 0:
901 /* RAW format */
902 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
903 f->flags = V4L2_FMT_FLAG_COMPRESSED;
904 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
905 break;
906 case 1:
907 f->pixelformat = V4L2_PIX_FMT_YUV420;
908 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
909 break;
910 default:
911 return -EINVAL;
912 }
913 return 0;
914}
915
916static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
917{
918 struct pwc_device *pdev = video_drvdata(file);
919
920 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
921 return -EINVAL;
922
923 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
924 pdev->width, pdev->height);
925 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
926 return 0;
927}
928
929static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
930{
931 struct pwc_device *pdev = video_drvdata(file);
932
933 return pwc_vidioc_try_fmt(pdev, f);
934}
935
936static int pwc_reqbufs(struct file *file, void *fh,
937 struct v4l2_requestbuffers *rb)
938{
939 struct pwc_device *pdev = video_drvdata(file);
940 int ret;
941
942 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
943 return -ERESTARTSYS;
944
945 ret = pwc_test_n_set_capt_file(pdev, file);
946 if (ret == 0)
947 ret = vb2_reqbufs(&pdev->vb_queue, rb);
948
949 mutex_unlock(&pdev->vb_queue_lock);
950 return ret;
951}
952
953static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
954{
955 struct pwc_device *pdev = video_drvdata(file);
956 int ret;
957
958 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
959 return -ERESTARTSYS;
960
961 ret = pwc_test_n_set_capt_file(pdev, file);
962 if (ret == 0)
963 ret = vb2_querybuf(&pdev->vb_queue, buf);
964
965 mutex_unlock(&pdev->vb_queue_lock);
966 return ret;
967}
968
969static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
970{
971 struct pwc_device *pdev = video_drvdata(file);
972 int ret;
973
974 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
975 return -ERESTARTSYS;
976
977 ret = pwc_test_n_set_capt_file(pdev, file);
978 if (ret == 0)
979 ret = vb2_qbuf(&pdev->vb_queue, buf);
980
981 mutex_unlock(&pdev->vb_queue_lock);
982 return ret;
983}
984
985static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
986{
987 struct pwc_device *pdev = video_drvdata(file);
988 int ret;
989
990 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
991 return -ERESTARTSYS;
992
993 ret = pwc_test_n_set_capt_file(pdev, file);
994 if (ret == 0)
995 ret = vb2_dqbuf(&pdev->vb_queue, buf,
996 file->f_flags & O_NONBLOCK);
997
998 mutex_unlock(&pdev->vb_queue_lock);
999 return ret;
1000}
1001
1002static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1003{
1004 struct pwc_device *pdev = video_drvdata(file);
1005 int ret;
1006
1007 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
1008 return -ERESTARTSYS;
1009
1010 ret = pwc_test_n_set_capt_file(pdev, file);
1011 if (ret == 0)
1012 ret = vb2_streamon(&pdev->vb_queue, i);
1013
1014 mutex_unlock(&pdev->vb_queue_lock);
1015 return ret;
1016}
1017
1018static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1019{
1020 struct pwc_device *pdev = video_drvdata(file);
1021 int ret;
1022
1023 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
1024 return -ERESTARTSYS;
1025
1026 ret = pwc_test_n_set_capt_file(pdev, file);
1027 if (ret == 0)
1028 ret = vb2_streamoff(&pdev->vb_queue, i);
1029
1030 mutex_unlock(&pdev->vb_queue_lock);
1031 return ret;
1032}
1033
1034static int pwc_enum_framesizes(struct file *file, void *fh,
1035 struct v4l2_frmsizeenum *fsize)
1036{
1037 struct pwc_device *pdev = video_drvdata(file);
1038 unsigned int i = 0, index = fsize->index;
1039
1040 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 ||
1041 (fsize->pixel_format == V4L2_PIX_FMT_PWC1 &&
1042 DEVICE_USE_CODEC1(pdev->type)) ||
1043 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 &&
1044 DEVICE_USE_CODEC23(pdev->type))) {
1045 for (i = 0; i < PSZ_MAX; i++) {
1046 if (!(pdev->image_mask & (1UL << i)))
1047 continue;
1048 if (!index--) {
1049 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1050 fsize->discrete.width = pwc_image_sizes[i][0];
1051 fsize->discrete.height = pwc_image_sizes[i][1];
1052 return 0;
1053 }
1054 }
1055 }
1056 return -EINVAL;
1057}
1058
1059static int pwc_enum_frameintervals(struct file *file, void *fh,
1060 struct v4l2_frmivalenum *fival)
1061{
1062 struct pwc_device *pdev = video_drvdata(file);
1063 int size = -1;
1064 unsigned int i;
1065
1066 for (i = 0; i < PSZ_MAX; i++) {
1067 if (pwc_image_sizes[i][0] == fival->width &&
1068 pwc_image_sizes[i][1] == fival->height) {
1069 size = i;
1070 break;
1071 }
1072 }
1073
1074 /* TODO: Support raw format */
1075 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1076 return -EINVAL;
1077
1078 i = pwc_get_fps(pdev, fival->index, size);
1079 if (!i)
1080 return -EINVAL;
1081
1082 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1083 fival->discrete.numerator = 1;
1084 fival->discrete.denominator = i;
1085
1086 return 0;
1087}
1088
1089static int pwc_g_parm(struct file *file, void *fh,
1090 struct v4l2_streamparm *parm)
1091{
1092 struct pwc_device *pdev = video_drvdata(file);
1093
1094 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1095 return -EINVAL;
1096
1097 memset(parm, 0, sizeof(*parm));
1098
1099 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1100 parm->parm.capture.readbuffers = MIN_FRAMES;
1101 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1102 parm->parm.capture.timeperframe.denominator = pdev->vframes;
1103 parm->parm.capture.timeperframe.numerator = 1;
1104
1105 return 0;
1106}
1107
1108static int pwc_s_parm(struct file *file, void *fh,
1109 struct v4l2_streamparm *parm)
1110{
1111 struct pwc_device *pdev = video_drvdata(file);
1112 int compression = 0;
1113 int ret, fps;
1114
1115 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1116 parm->parm.capture.timeperframe.numerator == 0)
1117 return -EINVAL;
1118
1119 fps = parm->parm.capture.timeperframe.denominator /
1120 parm->parm.capture.timeperframe.numerator;
1121
1122 if (mutex_lock_interruptible(&pdev->vb_queue_lock))
1123 return -ERESTARTSYS;
1124
1125 ret = pwc_test_n_set_capt_file(pdev, file);
1126 if (ret)
1127 goto leave;
1128
1129 if (pdev->vb_queue.streaming) {
1130 ret = -EBUSY;
1131 goto leave;
1132 }
1133
1134 ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
1135 fps, &compression, 0);
1136
1137 pwc_g_parm(file, fh, parm);
1138
1139leave:
1140 mutex_unlock(&pdev->vb_queue_lock);
1141 return ret;
1142}
1143
1144const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1145 .vidioc_querycap = pwc_querycap,
1146 .vidioc_enum_input = pwc_enum_input,
1147 .vidioc_g_input = pwc_g_input,
1148 .vidioc_s_input = pwc_s_input,
1149 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
1150 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
1151 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
1152 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
1153 .vidioc_reqbufs = pwc_reqbufs,
1154 .vidioc_querybuf = pwc_querybuf,
1155 .vidioc_qbuf = pwc_qbuf,
1156 .vidioc_dqbuf = pwc_dqbuf,
1157 .vidioc_streamon = pwc_streamon,
1158 .vidioc_streamoff = pwc_streamoff,
1159 .vidioc_log_status = v4l2_ctrl_log_status,
1160 .vidioc_enum_framesizes = pwc_enum_framesizes,
1161 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
1162 .vidioc_g_parm = pwc_g_parm,
1163 .vidioc_s_parm = pwc_s_parm,
1164 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1165 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1166};