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,
86 .flags = V4L2_CTRL_FLAG_SLIDER,
92 static const struct v4l2_ctrl_config pwc_backlight_cfg = {
94 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
95 .type = V4L2_CTRL_TYPE_BOOLEAN,
101 static const struct v4l2_ctrl_config pwc_flicker_cfg = {
102 .ops = &pwc_ctrl_ops,
103 .id = V4L2_CID_BAND_STOP_FILTER,
104 .type = V4L2_CTRL_TYPE_BOOLEAN,
110 static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
111 .ops = &pwc_ctrl_ops,
112 .id = PWC_CID_CUSTOM(noise_reduction),
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Dynamic Noise Reduction",
120 static const struct v4l2_ctrl_config pwc_save_user_cfg = {
121 .ops = &pwc_ctrl_ops,
122 .id = PWC_CID_CUSTOM(save_user),
123 .type = V4L2_CTRL_TYPE_BUTTON,
124 .name = "Save User Settings",
127 static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
128 .ops = &pwc_ctrl_ops,
129 .id = PWC_CID_CUSTOM(restore_user),
130 .type = V4L2_CTRL_TYPE_BUTTON,
131 .name = "Restore User Settings",
134 static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
135 .ops = &pwc_ctrl_ops,
136 .id = PWC_CID_CUSTOM(restore_factory),
137 .type = V4L2_CTRL_TYPE_BUTTON,
138 .name = "Restore Factory Settings",
141 int pwc_init_controls(struct pwc_device *pdev)
143 struct v4l2_ctrl_handler *hdl;
144 struct v4l2_ctrl_config cfg;
147 hdl = &pdev->ctrl_handler;
148 r = v4l2_ctrl_handler_init(hdl, 20);
152 /* Brightness, contrast, saturation, gamma */
153 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
156 pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
157 V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
159 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
162 pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
163 V4L2_CID_CONTRAST, 0, 63, 1, def);
165 if (pdev->type >= 675) {
166 if (pdev->type < 730)
167 pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
169 pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
170 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
172 if (r || def < -100 || def > 100)
174 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
175 V4L2_CID_SATURATION, -100, 100, 1, def);
178 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
181 pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
182 V4L2_CID_GAMMA, 0, 31, 1, def);
184 /* auto white balance, red gain, blue gain */
185 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
186 if (r || def > awb_auto)
188 cfg = pwc_auto_white_balance_cfg;
189 cfg.name = v4l2_ctrl_get_name(cfg.id);
191 pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
192 /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
193 if (!pdev->auto_white_balance)
196 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
197 PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
200 pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
201 V4L2_CID_RED_BALANCE, 0, 255, 1, def);
203 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
204 PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
207 pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
208 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
210 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true);
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 &&
567 (pdev->auto_white_balance->val != awb_auto ||
569 pdev->last_color_bal_update + HZ / 4))) {
570 pdev->red_balance->val = pdev->last_red_balance;
571 pdev->blue_balance->val = pdev->last_blue_balance;
574 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
575 READ_RED_GAIN_FORMATTER,
576 &pdev->red_balance->val);
579 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
580 READ_BLUE_GAIN_FORMATTER,
581 &pdev->blue_balance->val);
584 pdev->last_red_balance = pdev->red_balance->val;
585 pdev->last_blue_balance = pdev->blue_balance->val;
586 pdev->last_color_bal_update = jiffies;
587 pdev->color_bal_valid = true;
589 case V4L2_CID_AUTOGAIN:
590 if (pdev->gain_valid && time_before(jiffies,
591 pdev->last_gain_update + HZ / 4)) {
592 pdev->gain->val = pdev->last_gain;
595 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
596 READ_AGC_FORMATTER, &pdev->gain->val);
599 pdev->last_gain = pdev->gain->val;
600 pdev->last_gain_update = jiffies;
601 pdev->gain_valid = true;
602 if (!DEVICE_USE_CODEC3(pdev->type))
604 /* Fall through for CODEC3 where autogain also controls expo */
605 case V4L2_CID_EXPOSURE_AUTO:
606 if (pdev->exposure_valid && time_before(jiffies,
607 pdev->last_exposure_update + HZ / 4)) {
608 pdev->exposure->val = pdev->last_exposure;
611 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
612 READ_SHUTTER_FORMATTER,
613 &pdev->exposure->val);
616 pdev->last_exposure = pdev->exposure->val;
617 pdev->last_exposure_update = jiffies;
618 pdev->exposure_valid = true;
625 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
628 mutex_unlock(&pdev->udevlock);
629 mutex_lock(&pdev->modlock);
633 static int pwc_set_awb(struct pwc_device *pdev)
637 if (pdev->auto_white_balance->is_new) {
638 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
640 pdev->auto_white_balance->val);
644 if (pdev->auto_white_balance->val != awb_manual)
645 pdev->color_bal_valid = false; /* Force cache update */
647 if (pdev->auto_white_balance->val != awb_manual)
650 if (pdev->red_balance->is_new) {
651 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
652 PRESET_MANUAL_RED_GAIN_FORMATTER,
653 pdev->red_balance->val);
658 if (pdev->blue_balance->is_new) {
659 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
660 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
661 pdev->blue_balance->val);
668 /* For CODEC2 models which have separate autogain and auto exposure */
669 static int pwc_set_autogain(struct pwc_device *pdev)
673 if (pdev->autogain->is_new) {
674 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
676 pdev->autogain->val ? 0 : 0xff);
680 if (pdev->autogain->val)
681 pdev->gain_valid = false; /* Force cache update */
684 if (pdev->autogain->val)
687 if (pdev->gain->is_new) {
688 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
689 PRESET_AGC_FORMATTER,
697 /* For CODEC2 models which have separate autogain and auto exposure */
698 static int pwc_set_exposure_auto(struct pwc_device *pdev)
701 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
703 if (pdev->exposure_auto->is_new) {
704 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
705 SHUTTER_MODE_FORMATTER,
711 pdev->exposure_valid = false; /* Force cache update */
717 if (pdev->exposure->is_new) {
718 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
719 PRESET_SHUTTER_FORMATTER,
720 pdev->exposure->val);
727 /* For CODEC3 models which have autogain controlling both gain and exposure */
728 static int pwc_set_autogain_expo(struct pwc_device *pdev)
732 if (pdev->autogain->is_new) {
733 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
735 pdev->autogain->val ? 0 : 0xff);
739 if (pdev->autogain->val) {
740 pdev->gain_valid = false; /* Force cache update */
741 pdev->exposure_valid = false; /* Force cache update */
745 if (pdev->autogain->val)
748 if (pdev->gain->is_new) {
749 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
750 PRESET_AGC_FORMATTER,
756 if (pdev->exposure->is_new) {
757 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
758 PRESET_SHUTTER_FORMATTER,
759 pdev->exposure->val);
766 static int pwc_set_motor(struct pwc_device *pdev)
772 if (pdev->motor_pan_reset->is_new)
774 if (pdev->motor_tilt_reset->is_new)
776 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
777 ret = send_control_msg(pdev, SET_MPT_CTL,
778 PT_RESET_CONTROL_FORMATTER, buf, 1);
783 memset(buf, 0, sizeof(buf));
784 if (pdev->motor_pan->is_new) {
785 buf[0] = pdev->motor_pan->val & 0xFF;
786 buf[1] = (pdev->motor_pan->val >> 8);
788 if (pdev->motor_tilt->is_new) {
789 buf[2] = pdev->motor_tilt->val & 0xFF;
790 buf[3] = (pdev->motor_tilt->val >> 8);
792 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
793 ret = send_control_msg(pdev, SET_MPT_CTL,
794 PT_RELATIVE_CONTROL_FORMATTER,
803 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
805 struct pwc_device *pdev =
806 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
809 /* See the comments on locking in pwc_g_volatile_ctrl */
810 mutex_unlock(&pdev->modlock);
811 mutex_lock(&pdev->udevlock);
819 case V4L2_CID_BRIGHTNESS:
820 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
821 BRIGHTNESS_FORMATTER, ctrl->val);
823 case V4L2_CID_CONTRAST:
824 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
825 CONTRAST_FORMATTER, ctrl->val);
827 case V4L2_CID_SATURATION:
828 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
829 pdev->saturation_fmt, ctrl->val);
832 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
833 GAMMA_FORMATTER, ctrl->val);
835 case V4L2_CID_AUTO_WHITE_BALANCE:
836 ret = pwc_set_awb(pdev);
838 case V4L2_CID_AUTOGAIN:
839 if (DEVICE_USE_CODEC2(pdev->type))
840 ret = pwc_set_autogain(pdev);
841 else if (DEVICE_USE_CODEC3(pdev->type))
842 ret = pwc_set_autogain_expo(pdev);
846 case V4L2_CID_EXPOSURE_AUTO:
847 if (DEVICE_USE_CODEC2(pdev->type))
848 ret = pwc_set_exposure_auto(pdev);
852 case V4L2_CID_COLORFX:
853 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
854 COLOUR_MODE_FORMATTER,
855 ctrl->val ? 0 : 0xff);
857 case PWC_CID_CUSTOM(autocontour):
858 if (pdev->autocontour->is_new) {
859 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
860 AUTO_CONTOUR_FORMATTER,
861 pdev->autocontour->val ? 0 : 0xff);
863 if (ret == 0 && pdev->contour->is_new) {
864 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
865 PRESET_CONTOUR_FORMATTER,
869 case V4L2_CID_BACKLIGHT_COMPENSATION:
870 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
871 BACK_LIGHT_COMPENSATION_FORMATTER,
872 ctrl->val ? 0 : 0xff);
874 case V4L2_CID_BAND_STOP_FILTER:
875 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
876 FLICKERLESS_MODE_FORMATTER,
877 ctrl->val ? 0 : 0xff);
879 case PWC_CID_CUSTOM(noise_reduction):
880 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
881 DYNAMIC_NOISE_CONTROL_FORMATTER,
884 case PWC_CID_CUSTOM(save_user):
885 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
887 case PWC_CID_CUSTOM(restore_user):
888 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
890 case PWC_CID_CUSTOM(restore_factory):
891 ret = pwc_button_ctrl(pdev,
892 RESTORE_FACTORY_DEFAULTS_FORMATTER);
894 case V4L2_CID_PAN_RELATIVE:
895 ret = pwc_set_motor(pdev);
902 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
905 mutex_unlock(&pdev->udevlock);
906 mutex_lock(&pdev->modlock);
910 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
912 struct pwc_device *pdev = video_drvdata(file);
914 /* We only support two format: the raw format, and YUV */
918 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
919 f->flags = V4L2_FMT_FLAG_COMPRESSED;
920 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
923 f->pixelformat = V4L2_PIX_FMT_YUV420;
924 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
932 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
934 struct pwc_device *pdev = video_drvdata(file);
936 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
937 pdev->image.x, pdev->image.y);
938 pwc_vidioc_fill_fmt(pdev, f);
942 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
944 struct pwc_device *pdev = video_drvdata(file);
946 return pwc_vidioc_try_fmt(pdev, f);
949 static int pwc_reqbufs(struct file *file, void *fh,
950 struct v4l2_requestbuffers *rb)
952 struct pwc_device *pdev = video_drvdata(file);
954 if (pdev->capt_file != NULL &&
955 pdev->capt_file != file)
958 pdev->capt_file = file;
960 return vb2_reqbufs(&pdev->vb_queue, rb);
963 static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
965 struct pwc_device *pdev = video_drvdata(file);
967 return vb2_querybuf(&pdev->vb_queue, buf);
970 static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
972 struct pwc_device *pdev = video_drvdata(file);
977 if (pdev->capt_file != file)
980 return vb2_qbuf(&pdev->vb_queue, buf);
983 static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
985 struct pwc_device *pdev = video_drvdata(file);
990 if (pdev->capt_file != file)
993 return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
996 static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
998 struct pwc_device *pdev = video_drvdata(file);
1003 if (pdev->capt_file != file)
1006 return vb2_streamon(&pdev->vb_queue, i);
1009 static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1011 struct pwc_device *pdev = video_drvdata(file);
1016 if (pdev->capt_file != file)
1019 return vb2_streamoff(&pdev->vb_queue, i);
1022 static int pwc_enum_framesizes(struct file *file, void *fh,
1023 struct v4l2_frmsizeenum *fsize)
1025 struct pwc_device *pdev = video_drvdata(file);
1026 unsigned int i = 0, index = fsize->index;
1028 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) {
1029 for (i = 0; i < PSZ_MAX; i++) {
1030 if (pdev->image_mask & (1UL << i)) {
1032 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1033 fsize->discrete.width = pwc_image_sizes[i].x;
1034 fsize->discrete.height = pwc_image_sizes[i].y;
1039 } else if (fsize->index == 0 &&
1040 ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) ||
1041 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) {
1043 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1044 fsize->discrete.width = pdev->abs_max.x;
1045 fsize->discrete.height = pdev->abs_max.y;
1051 static int pwc_enum_frameintervals(struct file *file, void *fh,
1052 struct v4l2_frmivalenum *fival)
1054 struct pwc_device *pdev = video_drvdata(file);
1058 for (i = 0; i < PSZ_MAX; i++) {
1059 if (pwc_image_sizes[i].x == fival->width &&
1060 pwc_image_sizes[i].y == fival->height) {
1066 /* TODO: Support raw format */
1067 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1070 i = pwc_get_fps(pdev, fival->index, size);
1074 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1075 fival->discrete.numerator = 1;
1076 fival->discrete.denominator = i;
1081 static int pwc_log_status(struct file *file, void *priv)
1083 struct pwc_device *pdev = video_drvdata(file);
1085 v4l2_ctrl_handler_log_status(&pdev->ctrl_handler, PWC_NAME);
1089 static long pwc_default(struct file *file, void *fh, bool valid_prio,
1092 struct pwc_device *pdev = video_drvdata(file);
1094 return pwc_ioctl(pdev, cmd, arg);
1097 const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1098 .vidioc_querycap = pwc_querycap,
1099 .vidioc_enum_input = pwc_enum_input,
1100 .vidioc_g_input = pwc_g_input,
1101 .vidioc_s_input = pwc_s_input,
1102 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
1103 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
1104 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
1105 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
1106 .vidioc_reqbufs = pwc_reqbufs,
1107 .vidioc_querybuf = pwc_querybuf,
1108 .vidioc_qbuf = pwc_qbuf,
1109 .vidioc_dqbuf = pwc_dqbuf,
1110 .vidioc_streamon = pwc_streamon,
1111 .vidioc_streamoff = pwc_streamoff,
1112 .vidioc_log_status = pwc_log_status,
1113 .vidioc_enum_framesizes = pwc_enum_framesizes,
1114 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
1115 .vidioc_default = pwc_default,
1119 /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */