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>
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.
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.
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.
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
29 #include <linux/errno.h>
30 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/poll.h>
34 #include <linux/vmalloc.h>
35 #include <linux/jiffies.h>
40 #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
42 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
43 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
45 static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
46 .g_volatile_ctrl = pwc_g_volatile_ctrl,
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 };
54 const char * const pwc_auto_whitebal_qmenu[] = {
55 "Indoor (Incandescant Lighting) Mode",
56 "Outdoor (Sunlight) Mode",
57 "Indoor (Fluorescent Lighting) Mode",
63 static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
65 .id = V4L2_CID_AUTO_WHITE_BALANCE,
66 .type = V4L2_CTRL_TYPE_MENU,
68 .qmenu = pwc_auto_whitebal_qmenu,
71 static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
73 .id = PWC_CID_CUSTOM(autocontour),
74 .type = V4L2_CTRL_TYPE_BOOLEAN,
75 .name = "Auto contour",
81 static const struct v4l2_ctrl_config pwc_contour_cfg = {
83 .id = PWC_CID_CUSTOM(contour),
84 .type = V4L2_CTRL_TYPE_INTEGER,
91 static const struct v4l2_ctrl_config pwc_backlight_cfg = {
93 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
94 .type = V4L2_CTRL_TYPE_BOOLEAN,
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,
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",
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",
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",
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",
140 int pwc_init_controls(struct pwc_device *pdev)
142 struct v4l2_ctrl_handler *hdl;
143 struct v4l2_ctrl_config cfg;
146 hdl = &pdev->ctrl_handler;
147 r = v4l2_ctrl_handler_init(hdl, 20);
151 /* Brightness, contrast, saturation, gamma */
152 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
155 pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
156 V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
158 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
161 pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
162 V4L2_CID_CONTRAST, 0, 63, 1, def);
164 if (pdev->type >= 675) {
165 if (pdev->type < 730)
166 pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
168 pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
169 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
171 if (r || def < -100 || def > 100)
173 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
174 V4L2_CID_SATURATION, -100, 100, 1, def);
177 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
180 pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
181 V4L2_CID_GAMMA, 0, 31, 1, def);
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)
187 cfg = pwc_auto_white_balance_cfg;
188 cfg.name = v4l2_ctrl_get_name(cfg.id);
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)
195 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
196 PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
199 pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
200 V4L2_CID_RED_BALANCE, 0, 255, 1, def);
202 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
203 PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
206 pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
207 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
209 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual,
210 pdev->auto_white_balance->cur.val == awb_auto);
213 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
214 if (r || (def != 0 && def != 0xff))
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);
222 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
225 pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
226 V4L2_CID_GAIN, 0, 63, 1, def);
228 /* auto exposure, exposure */
229 if (DEVICE_USE_CODEC2(pdev->type)) {
230 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
232 if (r || (def != 0 && def != 0xff))
235 * def = 0 auto, def = ff manual
236 * menu idx 0 = auto, idx 1 = manual
238 pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
240 V4L2_CID_EXPOSURE_AUTO,
242 if (!pdev->exposure_auto)
245 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
246 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
247 READ_SHUTTER_FORMATTER, &def);
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);
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,
272 /* color / bw setting */
273 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
275 if (r || (def != 0 && 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);
281 /* autocontour, contour */
282 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
283 if (r || (def != 0 && def != 0xff))
285 cfg = pwc_autocontour_cfg;
287 pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
288 if (!pdev->autocontour)
291 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
294 cfg = pwc_contour_cfg;
296 pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
298 v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
301 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
302 BACK_LIGHT_COMPENSATION_FORMATTER, &def);
303 if (r || (def != 0 && def != 0xff))
305 cfg = pwc_backlight_cfg;
306 cfg.name = v4l2_ctrl_get_name(cfg.id);
308 pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
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))
315 cfg = pwc_flicker_cfg;
316 cfg.name = v4l2_ctrl_get_name(cfg.id);
318 pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
320 /* Dynamic noise reduction */
321 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
322 DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
325 cfg = pwc_noise_reduction_cfg;
327 pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
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,
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,
338 if (pdev->restore_factory)
339 pdev->restore_factory->flags = V4L2_CTRL_FLAG_UPDATE;
344 static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
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;
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;
361 f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
363 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
364 "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
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);
375 /* ioctl(VIDIOC_TRY_FMT) */
376 static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
378 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
379 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
383 switch (f->fmt.pix.pixelformat) {
384 case V4L2_PIX_FMT_YUV420:
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");
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");
399 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
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;
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;
417 /* ioctl(VIDIOC_SET_FMT) */
419 static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
421 struct pwc_device *pdev = video_drvdata(file);
422 int ret, fps, snapshot, compression, pixelformat;
427 if (pdev->capt_file != NULL &&
428 pdev->capt_file != file)
431 pdev->capt_file = file;
433 ret = pwc_vidioc_try_fmt(pdev, f);
437 pixelformat = f->fmt.pix.pixelformat;
438 compression = pdev->vcompression;
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;
449 if (pixelformat != V4L2_PIX_FMT_YUV420 &&
450 pixelformat != V4L2_PIX_FMT_PWC1 &&
451 pixelformat != V4L2_PIX_FMT_PWC2)
454 if (vb2_is_streaming(&pdev->vb_queue))
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,
462 (pixelformat>>8)&255,
463 (pixelformat>>16)&255,
464 (pixelformat>>24)&255);
466 ret = pwc_set_video_mode(pdev,
473 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
478 pdev->pixfmt = pixelformat;
480 pwc_vidioc_fill_fmt(pdev, f);
486 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
488 struct pwc_device *pdev = video_drvdata(file);
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));
497 V4L2_CAP_VIDEO_CAPTURE |
503 static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
505 if (i->index) /* Only one INPUT is supported */
508 strcpy(i->name, "usb");
512 static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
518 static int pwc_s_input(struct file *file, void *fh, unsigned int i)
520 return i ? -EINVAL : 0;
523 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
525 struct pwc_device *pdev =
526 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
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;
540 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
541 READ_RED_GAIN_FORMATTER,
542 &pdev->red_balance->val);
545 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
546 READ_BLUE_GAIN_FORMATTER,
547 &pdev->blue_balance->val);
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;
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;
561 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
562 READ_AGC_FORMATTER, &pdev->gain->val);
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))
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;
577 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
578 READ_SHUTTER_FORMATTER,
579 &pdev->exposure->val);
582 pdev->last_exposure = pdev->exposure->val;
583 pdev->last_exposure_update = jiffies;
584 pdev->exposure_valid = true;
591 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
596 static int pwc_set_awb(struct pwc_device *pdev)
600 if (pdev->auto_white_balance->is_new) {
601 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
603 pdev->auto_white_balance->val);
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);
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 */
626 pdev->red_balance->is_volatile = false;
627 pdev->blue_balance->is_volatile = false;
631 if (ret == 0 && pdev->red_balance->is_new) {
632 if (pdev->auto_white_balance->val != awb_manual)
634 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
635 PRESET_MANUAL_RED_GAIN_FORMATTER,
636 pdev->red_balance->val);
639 if (ret == 0 && pdev->blue_balance->is_new) {
640 if (pdev->auto_white_balance->val != awb_manual)
642 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
643 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
644 pdev->blue_balance->val);
649 /* For CODEC2 models which have separate autogain and auto exposure */
650 static int pwc_set_autogain(struct pwc_device *pdev)
654 if (pdev->autogain->is_new) {
655 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
657 pdev->autogain->val ? 0 : 0xff);
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,
667 if (ret == 0 && pdev->gain->is_new) {
668 if (pdev->autogain->val)
670 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
671 PRESET_AGC_FORMATTER,
677 /* For CODEC2 models which have separate autogain and auto exposure */
678 static int pwc_set_exposure_auto(struct pwc_device *pdev)
681 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
683 if (pdev->exposure_auto->is_new) {
684 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
685 SHUTTER_MODE_FORMATTER,
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);
696 if (ret == 0 && pdev->exposure->is_new) {
699 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
700 PRESET_SHUTTER_FORMATTER,
701 pdev->exposure->val);
706 /* For CODEC3 models which have autogain controlling both gain and exposure */
707 static int pwc_set_autogain_expo(struct pwc_device *pdev)
711 if (pdev->autogain->is_new) {
712 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
714 pdev->autogain->val ? 0 : 0xff);
717 if (pdev->autogain->val) {
718 pdev->gain_valid = false; /* Force cache update */
719 pdev->exposure_valid = false; /* Force cache update */
721 if (!pdev->gain->is_new)
722 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
725 if (!pdev->exposure->is_new)
726 pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
727 READ_SHUTTER_FORMATTER,
728 &pdev->exposure->val);
731 if (ret == 0 && pdev->gain->is_new) {
732 if (pdev->autogain->val)
734 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
735 PRESET_AGC_FORMATTER,
738 if (ret == 0 && pdev->exposure->is_new) {
739 if (pdev->autogain->val)
741 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
742 PRESET_SHUTTER_FORMATTER,
743 pdev->exposure->val);
748 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
750 struct pwc_device *pdev =
751 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
758 case V4L2_CID_BRIGHTNESS:
759 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
760 BRIGHTNESS_FORMATTER, ctrl->val);
762 case V4L2_CID_CONTRAST:
763 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
764 CONTRAST_FORMATTER, ctrl->val);
766 case V4L2_CID_SATURATION:
767 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
768 pdev->saturation_fmt, ctrl->val);
771 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
772 GAMMA_FORMATTER, ctrl->val);
774 case V4L2_CID_AUTO_WHITE_BALANCE:
775 ret = pwc_set_awb(pdev);
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);
785 case V4L2_CID_EXPOSURE_AUTO:
786 if (DEVICE_USE_CODEC2(pdev->type))
787 ret = pwc_set_exposure_auto(pdev);
791 case V4L2_CID_COLORFX:
792 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
793 COLOUR_MODE_FORMATTER,
794 ctrl->val ? 0 : 0xff);
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);
802 if (ret == 0 && pdev->contour->is_new) {
803 if (pdev->autocontour->val) {
807 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
808 PRESET_CONTOUR_FORMATTER,
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);
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);
822 case PWC_CID_CUSTOM(noise_reduction):
823 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
824 DYNAMIC_NOISE_CONTROL_FORMATTER,
827 case PWC_CID_CUSTOM(save_user):
828 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
830 case PWC_CID_CUSTOM(restore_user):
831 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
833 case PWC_CID_CUSTOM(restore_factory):
834 ret = pwc_button_ctrl(pdev,
835 RESTORE_FACTORY_DEFAULTS_FORMATTER);
842 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
847 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
849 struct pwc_device *pdev = video_drvdata(file);
851 /* We only support two format: the raw format, and YUV */
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));
860 f->pixelformat = V4L2_PIX_FMT_YUV420;
861 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
869 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
871 struct pwc_device *pdev = video_drvdata(file);
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);
879 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
881 struct pwc_device *pdev = video_drvdata(file);
883 return pwc_vidioc_try_fmt(pdev, f);
886 static int pwc_reqbufs(struct file *file, void *fh,
887 struct v4l2_requestbuffers *rb)
889 struct pwc_device *pdev = video_drvdata(file);
891 if (pdev->capt_file != NULL &&
892 pdev->capt_file != file)
895 pdev->capt_file = file;
897 return vb2_reqbufs(&pdev->vb_queue, rb);
900 static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
902 struct pwc_device *pdev = video_drvdata(file);
904 return vb2_querybuf(&pdev->vb_queue, buf);
907 static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
909 struct pwc_device *pdev = video_drvdata(file);
914 if (pdev->capt_file != file)
917 return vb2_qbuf(&pdev->vb_queue, buf);
920 static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
922 struct pwc_device *pdev = video_drvdata(file);
927 if (pdev->capt_file != file)
930 return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
933 static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
935 struct pwc_device *pdev = video_drvdata(file);
940 if (pdev->capt_file != file)
943 return vb2_streamon(&pdev->vb_queue, i);
946 static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
948 struct pwc_device *pdev = video_drvdata(file);
953 if (pdev->capt_file != file)
956 return vb2_streamoff(&pdev->vb_queue, i);
959 static int pwc_enum_framesizes(struct file *file, void *fh,
960 struct v4l2_frmsizeenum *fsize)
962 struct pwc_device *pdev = video_drvdata(file);
963 unsigned int i = 0, index = fsize->index;
965 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
966 for (i = 0; i < PSZ_MAX; i++) {
967 if (pdev->image_mask & (1UL << i)) {
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;
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)))) {
980 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
981 fsize->discrete.width = pdev->abs_max.x;
982 fsize->discrete.height = pdev->abs_max.y;
988 static int pwc_enum_frameintervals(struct file *file, void *fh,
989 struct v4l2_frmivalenum *fival)
991 struct pwc_device *pdev = video_drvdata(file);
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) {
1003 /* TODO: Support raw format */
1004 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1007 i = pwc_get_fps(pdev, fival->index, size);
1011 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1012 fival->discrete.numerator = 1;
1013 fival->discrete.denominator = i;
1018 static long pwc_default(struct file *file, void *fh, bool valid_prio,
1021 struct pwc_device *pdev = video_drvdata(file);
1023 return pwc_ioctl(pdev, cmd, arg);
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,
1047 /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */