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;
341 if (!(pdev->features & FEATURE_MOTOR_PANTILT))
344 /* Motor pan / tilt / reset */
345 pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
346 V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0);
347 if (!pdev->motor_pan)
349 pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
350 V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0);
351 pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
352 V4L2_CID_PAN_RESET, 0, 0, 0, 0);
353 pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
354 V4L2_CID_TILT_RESET, 0, 0, 0, 0);
355 v4l2_ctrl_cluster(4, &pdev->motor_pan);
360 static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
362 memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
363 f->fmt.pix.width = pdev->view.x;
364 f->fmt.pix.height = pdev->view.y;
365 f->fmt.pix.field = V4L2_FIELD_NONE;
366 if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
367 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
368 f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2;
369 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
371 /* vbandlength contains 4 lines ... */
372 f->fmt.pix.bytesperline = pdev->vbandlength/4;
373 f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame);
374 if (DEVICE_USE_CODEC1(pdev->type))
375 f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1;
377 f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2;
379 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
380 "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
383 f->fmt.pix.bytesperline,
384 f->fmt.pix.sizeimage,
385 (f->fmt.pix.pixelformat)&255,
386 (f->fmt.pix.pixelformat>>8)&255,
387 (f->fmt.pix.pixelformat>>16)&255,
388 (f->fmt.pix.pixelformat>>24)&255);
391 /* ioctl(VIDIOC_TRY_FMT) */
392 static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
394 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
395 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
399 switch (f->fmt.pix.pixelformat) {
400 case V4L2_PIX_FMT_YUV420:
402 case V4L2_PIX_FMT_PWC1:
403 if (DEVICE_USE_CODEC23(pdev->type)) {
404 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
408 case V4L2_PIX_FMT_PWC2:
409 if (DEVICE_USE_CODEC1(pdev->type)) {
410 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
415 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
420 if (f->fmt.pix.width > pdev->view_max.x)
421 f->fmt.pix.width = pdev->view_max.x;
422 else if (f->fmt.pix.width < pdev->view_min.x)
423 f->fmt.pix.width = pdev->view_min.x;
425 if (f->fmt.pix.height > pdev->view_max.y)
426 f->fmt.pix.height = pdev->view_max.y;
427 else if (f->fmt.pix.height < pdev->view_min.y)
428 f->fmt.pix.height = pdev->view_min.y;
433 /* ioctl(VIDIOC_SET_FMT) */
435 static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
437 struct pwc_device *pdev = video_drvdata(file);
438 int ret, fps, snapshot, compression, pixelformat;
443 if (pdev->capt_file != NULL &&
444 pdev->capt_file != file)
447 pdev->capt_file = file;
449 ret = pwc_vidioc_try_fmt(pdev, f);
453 pixelformat = f->fmt.pix.pixelformat;
454 compression = pdev->vcompression;
457 if (f->fmt.pix.priv) {
458 compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT;
459 snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT);
460 fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT;
465 if (pixelformat != V4L2_PIX_FMT_YUV420 &&
466 pixelformat != V4L2_PIX_FMT_PWC1 &&
467 pixelformat != V4L2_PIX_FMT_PWC2)
470 if (vb2_is_streaming(&pdev->vb_queue))
473 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
474 "compression=%d snapshot=%d format=%c%c%c%c\n",
475 f->fmt.pix.width, f->fmt.pix.height, fps,
476 compression, snapshot,
478 (pixelformat>>8)&255,
479 (pixelformat>>16)&255,
480 (pixelformat>>24)&255);
482 ret = pwc_set_video_mode(pdev,
489 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
494 pdev->pixfmt = pixelformat;
496 pwc_vidioc_fill_fmt(pdev, f);
502 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
504 struct pwc_device *pdev = video_drvdata(file);
509 strcpy(cap->driver, PWC_NAME);
510 strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
511 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
513 V4L2_CAP_VIDEO_CAPTURE |
519 static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
521 if (i->index) /* Only one INPUT is supported */
524 strcpy(i->name, "usb");
528 static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
534 static int pwc_s_input(struct file *file, void *fh, unsigned int i)
536 return i ? -EINVAL : 0;
539 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
541 struct pwc_device *pdev =
542 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
546 * Sometimes it can take quite long for the pwc to complete usb control
547 * transfers, so release the modlock to give streaming by another
548 * process / thread the chance to continue with a dqbuf.
550 mutex_unlock(&pdev->modlock);
553 * Take the udev-lock to protect against the disconnect handler
554 * completing and setting dev->udev to NULL underneath us. Other code
555 * does not need to do this since it is protected by the modlock.
557 mutex_lock(&pdev->udevlock);
565 case V4L2_CID_AUTO_WHITE_BALANCE:
566 if (pdev->color_bal_valid && time_before(jiffies,
567 pdev->last_color_bal_update + HZ / 4)) {
568 pdev->red_balance->val = pdev->last_red_balance;
569 pdev->blue_balance->val = pdev->last_blue_balance;
572 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
573 READ_RED_GAIN_FORMATTER,
574 &pdev->red_balance->val);
577 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
578 READ_BLUE_GAIN_FORMATTER,
579 &pdev->blue_balance->val);
582 pdev->last_red_balance = pdev->red_balance->val;
583 pdev->last_blue_balance = pdev->blue_balance->val;
584 pdev->last_color_bal_update = jiffies;
585 pdev->color_bal_valid = true;
587 case V4L2_CID_AUTOGAIN:
588 if (pdev->gain_valid && time_before(jiffies,
589 pdev->last_gain_update + HZ / 4)) {
590 pdev->gain->val = pdev->last_gain;
593 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
594 READ_AGC_FORMATTER, &pdev->gain->val);
597 pdev->last_gain = pdev->gain->val;
598 pdev->last_gain_update = jiffies;
599 pdev->gain_valid = true;
600 if (!DEVICE_USE_CODEC3(pdev->type))
602 /* Fall through for CODEC3 where autogain also controls expo */
603 case V4L2_CID_EXPOSURE_AUTO:
604 if (pdev->exposure_valid && time_before(jiffies,
605 pdev->last_exposure_update + HZ / 4)) {
606 pdev->exposure->val = pdev->last_exposure;
609 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
610 READ_SHUTTER_FORMATTER,
611 &pdev->exposure->val);
614 pdev->last_exposure = pdev->exposure->val;
615 pdev->last_exposure_update = jiffies;
616 pdev->exposure_valid = true;
623 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
626 mutex_unlock(&pdev->udevlock);
627 mutex_lock(&pdev->modlock);
631 static int pwc_set_awb(struct pwc_device *pdev)
635 if (pdev->auto_white_balance->is_new) {
636 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
638 pdev->auto_white_balance->val);
642 /* Update val when coming from auto or going to a preset */
643 if (pdev->red_balance->is_volatile ||
644 pdev->auto_white_balance->val == awb_indoor ||
645 pdev->auto_white_balance->val == awb_outdoor ||
646 pdev->auto_white_balance->val == awb_fl) {
647 if (!pdev->red_balance->is_new)
648 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
649 READ_RED_GAIN_FORMATTER,
650 &pdev->red_balance->val);
651 if (!pdev->blue_balance->is_new)
652 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
653 READ_BLUE_GAIN_FORMATTER,
654 &pdev->blue_balance->val);
656 if (pdev->auto_white_balance->val == awb_auto) {
657 pdev->red_balance->is_volatile = true;
658 pdev->blue_balance->is_volatile = true;
659 pdev->color_bal_valid = false; /* Force cache update */
661 pdev->red_balance->is_volatile = false;
662 pdev->blue_balance->is_volatile = false;
666 if (ret == 0 && pdev->red_balance->is_new) {
667 if (pdev->auto_white_balance->val != awb_manual)
669 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
670 PRESET_MANUAL_RED_GAIN_FORMATTER,
671 pdev->red_balance->val);
674 if (ret == 0 && pdev->blue_balance->is_new) {
675 if (pdev->auto_white_balance->val != awb_manual)
677 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
678 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
679 pdev->blue_balance->val);
684 /* For CODEC2 models which have separate autogain and auto exposure */
685 static int pwc_set_autogain(struct pwc_device *pdev)
689 if (pdev->autogain->is_new) {
690 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
692 pdev->autogain->val ? 0 : 0xff);
695 if (pdev->autogain->val)
696 pdev->gain_valid = false; /* Force cache update */
697 else if (!pdev->gain->is_new)
698 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
702 if (ret == 0 && pdev->gain->is_new) {
703 if (pdev->autogain->val)
705 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
706 PRESET_AGC_FORMATTER,
712 /* For CODEC2 models which have separate autogain and auto exposure */
713 static int pwc_set_exposure_auto(struct pwc_device *pdev)
716 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
718 if (pdev->exposure_auto->is_new) {
719 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
720 SHUTTER_MODE_FORMATTER,
725 pdev->exposure_valid = false; /* Force cache update */
726 else if (!pdev->exposure->is_new)
727 pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
728 READ_SHUTTER_FORMATTER,
729 &pdev->exposure->val);
731 if (ret == 0 && pdev->exposure->is_new) {
734 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
735 PRESET_SHUTTER_FORMATTER,
736 pdev->exposure->val);
741 /* For CODEC3 models which have autogain controlling both gain and exposure */
742 static int pwc_set_autogain_expo(struct pwc_device *pdev)
746 if (pdev->autogain->is_new) {
747 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
749 pdev->autogain->val ? 0 : 0xff);
752 if (pdev->autogain->val) {
753 pdev->gain_valid = false; /* Force cache update */
754 pdev->exposure_valid = false; /* Force cache update */
756 if (!pdev->gain->is_new)
757 pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
760 if (!pdev->exposure->is_new)
761 pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
762 READ_SHUTTER_FORMATTER,
763 &pdev->exposure->val);
766 if (ret == 0 && pdev->gain->is_new) {
767 if (pdev->autogain->val)
769 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
770 PRESET_AGC_FORMATTER,
773 if (ret == 0 && pdev->exposure->is_new) {
774 if (pdev->autogain->val)
776 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
777 PRESET_SHUTTER_FORMATTER,
778 pdev->exposure->val);
783 static int pwc_set_motor(struct pwc_device *pdev)
789 if (pdev->motor_pan_reset->is_new)
791 if (pdev->motor_tilt_reset->is_new)
793 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
794 ret = send_control_msg(pdev, SET_MPT_CTL,
795 PT_RESET_CONTROL_FORMATTER, buf, 1);
800 memset(buf, 0, sizeof(buf));
801 if (pdev->motor_pan->is_new) {
802 buf[0] = pdev->motor_pan->val & 0xFF;
803 buf[1] = (pdev->motor_pan->val >> 8);
805 if (pdev->motor_tilt->is_new) {
806 buf[2] = pdev->motor_tilt->val & 0xFF;
807 buf[3] = (pdev->motor_tilt->val >> 8);
809 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
810 ret = send_control_msg(pdev, SET_MPT_CTL,
811 PT_RELATIVE_CONTROL_FORMATTER,
820 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
822 struct pwc_device *pdev =
823 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
826 /* See the comments on locking in pwc_g_volatile_ctrl */
827 mutex_unlock(&pdev->modlock);
828 mutex_lock(&pdev->udevlock);
836 case V4L2_CID_BRIGHTNESS:
837 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
838 BRIGHTNESS_FORMATTER, ctrl->val);
840 case V4L2_CID_CONTRAST:
841 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
842 CONTRAST_FORMATTER, ctrl->val);
844 case V4L2_CID_SATURATION:
845 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
846 pdev->saturation_fmt, ctrl->val);
849 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
850 GAMMA_FORMATTER, ctrl->val);
852 case V4L2_CID_AUTO_WHITE_BALANCE:
853 ret = pwc_set_awb(pdev);
855 case V4L2_CID_AUTOGAIN:
856 if (DEVICE_USE_CODEC2(pdev->type))
857 ret = pwc_set_autogain(pdev);
858 else if (DEVICE_USE_CODEC3(pdev->type))
859 ret = pwc_set_autogain_expo(pdev);
863 case V4L2_CID_EXPOSURE_AUTO:
864 if (DEVICE_USE_CODEC2(pdev->type))
865 ret = pwc_set_exposure_auto(pdev);
869 case V4L2_CID_COLORFX:
870 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
871 COLOUR_MODE_FORMATTER,
872 ctrl->val ? 0 : 0xff);
874 case PWC_CID_CUSTOM(autocontour):
875 if (pdev->autocontour->is_new) {
876 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
877 AUTO_CONTOUR_FORMATTER,
878 pdev->autocontour->val ? 0 : 0xff);
880 if (ret == 0 && pdev->contour->is_new) {
881 if (pdev->autocontour->val) {
885 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
886 PRESET_CONTOUR_FORMATTER,
890 case V4L2_CID_BACKLIGHT_COMPENSATION:
891 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
892 BACK_LIGHT_COMPENSATION_FORMATTER,
893 ctrl->val ? 0 : 0xff);
895 case V4L2_CID_BAND_STOP_FILTER:
896 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
897 FLICKERLESS_MODE_FORMATTER,
898 ctrl->val ? 0 : 0xff);
900 case PWC_CID_CUSTOM(noise_reduction):
901 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
902 DYNAMIC_NOISE_CONTROL_FORMATTER,
905 case PWC_CID_CUSTOM(save_user):
906 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
908 case PWC_CID_CUSTOM(restore_user):
909 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
911 case PWC_CID_CUSTOM(restore_factory):
912 ret = pwc_button_ctrl(pdev,
913 RESTORE_FACTORY_DEFAULTS_FORMATTER);
915 case V4L2_CID_PAN_RELATIVE:
916 ret = pwc_set_motor(pdev);
923 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
926 mutex_unlock(&pdev->udevlock);
927 mutex_lock(&pdev->modlock);
931 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
933 struct pwc_device *pdev = video_drvdata(file);
935 /* We only support two format: the raw format, and YUV */
939 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
940 f->flags = V4L2_FMT_FLAG_COMPRESSED;
941 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
944 f->pixelformat = V4L2_PIX_FMT_YUV420;
945 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
953 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
955 struct pwc_device *pdev = video_drvdata(file);
957 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
958 pdev->image.x, pdev->image.y);
959 pwc_vidioc_fill_fmt(pdev, f);
963 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
965 struct pwc_device *pdev = video_drvdata(file);
967 return pwc_vidioc_try_fmt(pdev, f);
970 static int pwc_reqbufs(struct file *file, void *fh,
971 struct v4l2_requestbuffers *rb)
973 struct pwc_device *pdev = video_drvdata(file);
975 if (pdev->capt_file != NULL &&
976 pdev->capt_file != file)
979 pdev->capt_file = file;
981 return vb2_reqbufs(&pdev->vb_queue, rb);
984 static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
986 struct pwc_device *pdev = video_drvdata(file);
988 return vb2_querybuf(&pdev->vb_queue, buf);
991 static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
993 struct pwc_device *pdev = video_drvdata(file);
998 if (pdev->capt_file != file)
1001 return vb2_qbuf(&pdev->vb_queue, buf);
1004 static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1006 struct pwc_device *pdev = video_drvdata(file);
1011 if (pdev->capt_file != file)
1014 return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
1017 static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1019 struct pwc_device *pdev = video_drvdata(file);
1024 if (pdev->capt_file != file)
1027 return vb2_streamon(&pdev->vb_queue, i);
1030 static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1032 struct pwc_device *pdev = video_drvdata(file);
1037 if (pdev->capt_file != file)
1040 return vb2_streamoff(&pdev->vb_queue, i);
1043 static int pwc_enum_framesizes(struct file *file, void *fh,
1044 struct v4l2_frmsizeenum *fsize)
1046 struct pwc_device *pdev = video_drvdata(file);
1047 unsigned int i = 0, index = fsize->index;
1049 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
1050 for (i = 0; i < PSZ_MAX; i++) {
1051 if (pdev->image_mask & (1UL << i)) {
1053 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1054 fsize->discrete.width = pwc_image_sizes[i].x;
1055 fsize->discrete.height = pwc_image_sizes[i].y;
1060 } else if (fsize->index == 0 &&
1061 ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
1062 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
1064 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1065 fsize->discrete.width = pdev->abs_max.x;
1066 fsize->discrete.height = pdev->abs_max.y;
1072 static int pwc_enum_frameintervals(struct file *file, void *fh,
1073 struct v4l2_frmivalenum *fival)
1075 struct pwc_device *pdev = video_drvdata(file);
1079 for (i = 0; i < PSZ_MAX; i++) {
1080 if (pwc_image_sizes[i].x == fival->width &&
1081 pwc_image_sizes[i].y == fival->height) {
1087 /* TODO: Support raw format */
1088 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1091 i = pwc_get_fps(pdev, fival->index, size);
1095 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1096 fival->discrete.numerator = 1;
1097 fival->discrete.denominator = i;
1102 static long pwc_default(struct file *file, void *fh, bool valid_prio,
1105 struct pwc_device *pdev = video_drvdata(file);
1107 return pwc_ioctl(pdev, cmd, arg);
1110 const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1111 .vidioc_querycap = pwc_querycap,
1112 .vidioc_enum_input = pwc_enum_input,
1113 .vidioc_g_input = pwc_g_input,
1114 .vidioc_s_input = pwc_s_input,
1115 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
1116 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
1117 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
1118 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
1119 .vidioc_reqbufs = pwc_reqbufs,
1120 .vidioc_querybuf = pwc_querybuf,
1121 .vidioc_qbuf = pwc_qbuf,
1122 .vidioc_dqbuf = pwc_dqbuf,
1123 .vidioc_streamon = pwc_streamon,
1124 .vidioc_streamoff = pwc_streamoff,
1125 .vidioc_enum_framesizes = pwc_enum_framesizes,
1126 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
1127 .vidioc_default = pwc_default,
1131 /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */