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_awb_speed
, custom_awb_delay
,
53 custom_save_user
, custom_restore_user
, custom_restore_factory
};
55 const char * const pwc_auto_whitebal_qmenu
[] = {
56 "Indoor (Incandescant Lighting) Mode",
57 "Outdoor (Sunlight) Mode",
58 "Indoor (Fluorescent Lighting) Mode",
64 static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg
= {
66 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
67 .type
= V4L2_CTRL_TYPE_MENU
,
69 .qmenu
= pwc_auto_whitebal_qmenu
,
72 static const struct v4l2_ctrl_config pwc_autocontour_cfg
= {
74 .id
= PWC_CID_CUSTOM(autocontour
),
75 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
76 .name
= "Auto contour",
82 static const struct v4l2_ctrl_config pwc_contour_cfg
= {
84 .id
= PWC_CID_CUSTOM(contour
),
85 .type
= V4L2_CTRL_TYPE_INTEGER
,
87 .flags
= V4L2_CTRL_FLAG_SLIDER
,
93 static const struct v4l2_ctrl_config pwc_backlight_cfg
= {
95 .id
= V4L2_CID_BACKLIGHT_COMPENSATION
,
96 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
102 static 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
,
111 static 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",
121 static 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",
128 static 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",
135 static 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",
142 static 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",
152 static 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",
162 int pwc_init_controls(struct pwc_device
*pdev
)
164 struct v4l2_ctrl_handler
*hdl
;
165 struct v4l2_ctrl_config cfg
;
168 hdl
= &pdev
->ctrl_handler
;
169 r
= v4l2_ctrl_handler_init(hdl
, 20);
173 /* Brightness, contrast, saturation, gamma */
174 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, BRIGHTNESS_FORMATTER
, &def
);
177 pdev
->brightness
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
178 V4L2_CID_BRIGHTNESS
, 0, 127, 1, def
);
180 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, CONTRAST_FORMATTER
, &def
);
183 pdev
->contrast
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
184 V4L2_CID_CONTRAST
, 0, 63, 1, def
);
186 if (pdev
->type
>= 675) {
187 if (pdev
->type
< 730)
188 pdev
->saturation_fmt
= SATURATION_MODE_FORMATTER2
;
190 pdev
->saturation_fmt
= SATURATION_MODE_FORMATTER1
;
191 r
= pwc_get_s8_ctrl(pdev
, GET_CHROM_CTL
, pdev
->saturation_fmt
,
193 if (r
|| def
< -100 || def
> 100)
195 pdev
->saturation
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
196 V4L2_CID_SATURATION
, -100, 100, 1, def
);
199 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, GAMMA_FORMATTER
, &def
);
202 pdev
->gamma
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
203 V4L2_CID_GAMMA
, 0, 31, 1, def
);
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
)
209 cfg
= pwc_auto_white_balance_cfg
;
210 cfg
.name
= v4l2_ctrl_get_name(cfg
.id
);
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
)
217 r
= pwc_get_u8_ctrl(pdev
, GET_CHROM_CTL
,
218 PRESET_MANUAL_RED_GAIN_FORMATTER
, &def
);
221 pdev
->red_balance
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
222 V4L2_CID_RED_BALANCE
, 0, 255, 1, def
);
224 r
= pwc_get_u8_ctrl(pdev
, GET_CHROM_CTL
,
225 PRESET_MANUAL_BLUE_GAIN_FORMATTER
, &def
);
228 pdev
->blue_balance
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
229 V4L2_CID_BLUE_BALANCE
, 0, 255, 1, def
);
231 v4l2_ctrl_auto_cluster(3, &pdev
->auto_white_balance
, awb_manual
, true);
234 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, AGC_MODE_FORMATTER
, &def
);
235 if (r
|| (def
!= 0 && def
!= 0xff))
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);
243 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, PRESET_AGC_FORMATTER
, &def
);
246 pdev
->gain
= v4l2_ctrl_new_std(hdl
, &pwc_ctrl_ops
,
247 V4L2_CID_GAIN
, 0, 63, 1, def
);
249 /* auto exposure, exposure */
250 if (DEVICE_USE_CODEC2(pdev
->type
)) {
251 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, SHUTTER_MODE_FORMATTER
,
253 if (r
|| (def
!= 0 && def
!= 0xff))
256 * def = 0 auto, def = ff manual
257 * menu idx 0 = auto, idx 1 = manual
259 pdev
->exposure_auto
= v4l2_ctrl_new_std_menu(hdl
,
261 V4L2_CID_EXPOSURE_AUTO
,
263 if (!pdev
->exposure_auto
)
266 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
267 r
= pwc_get_u16_ctrl(pdev
, GET_STATUS_CTL
,
268 READ_SHUTTER_FORMATTER
, &def
);
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
);
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
,
293 /* color / bw setting */
294 r
= pwc_get_u8_ctrl(pdev
, GET_CHROM_CTL
, COLOUR_MODE_FORMATTER
,
296 if (r
|| (def
!= 0 && 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);
302 /* autocontour, contour */
303 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, AUTO_CONTOUR_FORMATTER
, &def
);
304 if (r
|| (def
!= 0 && def
!= 0xff))
306 cfg
= pwc_autocontour_cfg
;
308 pdev
->autocontour
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
309 if (!pdev
->autocontour
)
312 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
, PRESET_CONTOUR_FORMATTER
, &def
);
315 cfg
= pwc_contour_cfg
;
317 pdev
->contour
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
319 v4l2_ctrl_auto_cluster(2, &pdev
->autocontour
, 0, false);
322 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
,
323 BACK_LIGHT_COMPENSATION_FORMATTER
, &def
);
324 if (r
|| (def
!= 0 && def
!= 0xff))
326 cfg
= pwc_backlight_cfg
;
327 cfg
.name
= v4l2_ctrl_get_name(cfg
.id
);
329 pdev
->backlight
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
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))
336 cfg
= pwc_flicker_cfg
;
337 cfg
.name
= v4l2_ctrl_get_name(cfg
.id
);
339 pdev
->flicker
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
341 /* Dynamic noise reduction */
342 r
= pwc_get_u8_ctrl(pdev
, GET_LUM_CTL
,
343 DYNAMIC_NOISE_CONTROL_FORMATTER
, &def
);
346 cfg
= pwc_noise_reduction_cfg
;
348 pdev
->noise_reduction
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
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
,
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
,
359 if (pdev
->restore_factory
)
360 pdev
->restore_factory
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
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)
367 cfg
= pwc_awb_speed_cfg
;
369 pdev
->awb_speed
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
371 r
= pwc_get_u8_ctrl(pdev
, GET_CHROM_CTL
,
372 AWB_CONTROL_DELAY_FORMATTER
, &def
);
375 cfg
= pwc_awb_delay_cfg
;
377 pdev
->awb_delay
= v4l2_ctrl_new_custom(hdl
, &cfg
, NULL
);
379 if (!(pdev
->features
& FEATURE_MOTOR_PANTILT
))
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
)
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
);
398 static void pwc_vidioc_fill_fmt(struct v4l2_format
*f
,
399 int width
, int height
, u32 pixfmt
)
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 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SRGB
;
409 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() "
410 "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
413 f
->fmt
.pix
.bytesperline
,
414 f
->fmt
.pix
.sizeimage
,
415 (f
->fmt
.pix
.pixelformat
)&255,
416 (f
->fmt
.pix
.pixelformat
>>8)&255,
417 (f
->fmt
.pix
.pixelformat
>>16)&255,
418 (f
->fmt
.pix
.pixelformat
>>24)&255);
421 /* ioctl(VIDIOC_TRY_FMT) */
422 static int pwc_vidioc_try_fmt(struct pwc_device
*pdev
, struct v4l2_format
*f
)
426 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
427 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
431 switch (f
->fmt
.pix
.pixelformat
) {
432 case V4L2_PIX_FMT_YUV420
:
434 case V4L2_PIX_FMT_PWC1
:
435 if (DEVICE_USE_CODEC23(pdev
->type
)) {
436 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
437 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV420
;
440 case V4L2_PIX_FMT_PWC2
:
441 if (DEVICE_USE_CODEC1(pdev
->type
)) {
442 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
443 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV420
;
447 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
448 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV420
;
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
);
460 /* ioctl(VIDIOC_SET_FMT) */
462 static int pwc_s_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
464 struct pwc_device
*pdev
= video_drvdata(file
);
465 int ret
, pixelformat
, compression
= 0;
467 ret
= pwc_vidioc_try_fmt(pdev
, f
);
471 if (vb2_is_busy(&pdev
->vb_queue
))
474 pixelformat
= f
->fmt
.pix
.pixelformat
;
476 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
478 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, pdev
->vframes
,
480 (pixelformat
>>8)&255,
481 (pixelformat
>>16)&255,
482 (pixelformat
>>24)&255);
484 ret
= pwc_set_video_mode(pdev
, f
->fmt
.pix
.width
, f
->fmt
.pix
.height
,
485 pixelformat
, 30, &compression
, 0);
487 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret
);
489 pwc_vidioc_fill_fmt(f
, pdev
->width
, pdev
->height
, pdev
->pixfmt
);
493 static int pwc_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*cap
)
495 struct pwc_device
*pdev
= video_drvdata(file
);
497 strcpy(cap
->driver
, PWC_NAME
);
498 strlcpy(cap
->card
, pdev
->vdev
.name
, sizeof(cap
->card
));
499 usb_make_path(pdev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
500 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
502 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
506 static int pwc_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*i
)
508 if (i
->index
) /* Only one INPUT is supported */
511 strlcpy(i
->name
, "Camera", sizeof(i
->name
));
512 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
516 static int pwc_g_input(struct file
*file
, void *fh
, unsigned int *i
)
522 static int pwc_s_input(struct file
*file
, void *fh
, unsigned int i
)
524 return i
? -EINVAL
: 0;
527 static int pwc_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
529 struct pwc_device
*pdev
=
530 container_of(ctrl
->handler
, struct pwc_device
, ctrl_handler
);
534 case V4L2_CID_AUTO_WHITE_BALANCE
:
535 if (pdev
->color_bal_valid
&&
536 (pdev
->auto_white_balance
->val
!= awb_auto
||
538 pdev
->last_color_bal_update
+ HZ
/ 4))) {
539 pdev
->red_balance
->val
= pdev
->last_red_balance
;
540 pdev
->blue_balance
->val
= pdev
->last_blue_balance
;
543 ret
= pwc_get_u8_ctrl(pdev
, GET_STATUS_CTL
,
544 READ_RED_GAIN_FORMATTER
,
545 &pdev
->red_balance
->val
);
548 ret
= pwc_get_u8_ctrl(pdev
, GET_STATUS_CTL
,
549 READ_BLUE_GAIN_FORMATTER
,
550 &pdev
->blue_balance
->val
);
553 pdev
->last_red_balance
= pdev
->red_balance
->val
;
554 pdev
->last_blue_balance
= pdev
->blue_balance
->val
;
555 pdev
->last_color_bal_update
= jiffies
;
556 pdev
->color_bal_valid
= true;
558 case V4L2_CID_AUTOGAIN
:
559 if (pdev
->gain_valid
&& time_before(jiffies
,
560 pdev
->last_gain_update
+ HZ
/ 4)) {
561 pdev
->gain
->val
= pdev
->last_gain
;
564 ret
= pwc_get_u8_ctrl(pdev
, GET_STATUS_CTL
,
565 READ_AGC_FORMATTER
, &pdev
->gain
->val
);
568 pdev
->last_gain
= pdev
->gain
->val
;
569 pdev
->last_gain_update
= jiffies
;
570 pdev
->gain_valid
= true;
571 if (!DEVICE_USE_CODEC3(pdev
->type
))
573 /* Fall through for CODEC3 where autogain also controls expo */
574 case V4L2_CID_EXPOSURE_AUTO
:
575 if (pdev
->exposure_valid
&& time_before(jiffies
,
576 pdev
->last_exposure_update
+ HZ
/ 4)) {
577 pdev
->exposure
->val
= pdev
->last_exposure
;
580 ret
= pwc_get_u16_ctrl(pdev
, GET_STATUS_CTL
,
581 READ_SHUTTER_FORMATTER
,
582 &pdev
->exposure
->val
);
585 pdev
->last_exposure
= pdev
->exposure
->val
;
586 pdev
->last_exposure_update
= jiffies
;
587 pdev
->exposure_valid
= true;
594 PWC_ERROR("g_ctrl %s error %d\n", ctrl
->name
, ret
);
599 static int pwc_set_awb(struct pwc_device
*pdev
)
603 if (pdev
->auto_white_balance
->is_new
) {
604 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
606 pdev
->auto_white_balance
->val
);
610 if (pdev
->auto_white_balance
->val
!= awb_manual
)
611 pdev
->color_bal_valid
= false; /* Force cache update */
614 * If this is a preset, update our red / blue balance values
615 * so that events get generated for the new preset values
617 if (pdev
->auto_white_balance
->val
== awb_indoor
||
618 pdev
->auto_white_balance
->val
== awb_outdoor
||
619 pdev
->auto_white_balance
->val
== awb_fl
)
620 pwc_g_volatile_ctrl(pdev
->auto_white_balance
);
622 if (pdev
->auto_white_balance
->val
!= awb_manual
)
625 if (pdev
->red_balance
->is_new
) {
626 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
627 PRESET_MANUAL_RED_GAIN_FORMATTER
,
628 pdev
->red_balance
->val
);
633 if (pdev
->blue_balance
->is_new
) {
634 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
635 PRESET_MANUAL_BLUE_GAIN_FORMATTER
,
636 pdev
->blue_balance
->val
);
643 /* For CODEC2 models which have separate autogain and auto exposure */
644 static int pwc_set_autogain(struct pwc_device
*pdev
)
648 if (pdev
->autogain
->is_new
) {
649 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
651 pdev
->autogain
->val
? 0 : 0xff);
655 if (pdev
->autogain
->val
)
656 pdev
->gain_valid
= false; /* Force cache update */
659 if (pdev
->autogain
->val
)
662 if (pdev
->gain
->is_new
) {
663 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
664 PRESET_AGC_FORMATTER
,
672 /* For CODEC2 models which have separate autogain and auto exposure */
673 static int pwc_set_exposure_auto(struct pwc_device
*pdev
)
676 int is_auto
= pdev
->exposure_auto
->val
== V4L2_EXPOSURE_AUTO
;
678 if (pdev
->exposure_auto
->is_new
) {
679 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
680 SHUTTER_MODE_FORMATTER
,
686 pdev
->exposure_valid
= false; /* Force cache update */
692 if (pdev
->exposure
->is_new
) {
693 ret
= pwc_set_u16_ctrl(pdev
, SET_LUM_CTL
,
694 PRESET_SHUTTER_FORMATTER
,
695 pdev
->exposure
->val
);
702 /* For CODEC3 models which have autogain controlling both gain and exposure */
703 static int pwc_set_autogain_expo(struct pwc_device
*pdev
)
707 if (pdev
->autogain
->is_new
) {
708 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
710 pdev
->autogain
->val
? 0 : 0xff);
714 if (pdev
->autogain
->val
) {
715 pdev
->gain_valid
= false; /* Force cache update */
716 pdev
->exposure_valid
= false; /* Force cache update */
720 if (pdev
->autogain
->val
)
723 if (pdev
->gain
->is_new
) {
724 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
725 PRESET_AGC_FORMATTER
,
731 if (pdev
->exposure
->is_new
) {
732 ret
= pwc_set_u16_ctrl(pdev
, SET_LUM_CTL
,
733 PRESET_SHUTTER_FORMATTER
,
734 pdev
->exposure
->val
);
741 static int pwc_set_motor(struct pwc_device
*pdev
)
745 pdev
->ctrl_buf
[0] = 0;
746 if (pdev
->motor_pan_reset
->is_new
)
747 pdev
->ctrl_buf
[0] |= 0x01;
748 if (pdev
->motor_tilt_reset
->is_new
)
749 pdev
->ctrl_buf
[0] |= 0x02;
750 if (pdev
->motor_pan_reset
->is_new
|| pdev
->motor_tilt_reset
->is_new
) {
751 ret
= send_control_msg(pdev
, SET_MPT_CTL
,
752 PT_RESET_CONTROL_FORMATTER
,
758 memset(pdev
->ctrl_buf
, 0, 4);
759 if (pdev
->motor_pan
->is_new
) {
760 pdev
->ctrl_buf
[0] = pdev
->motor_pan
->val
& 0xFF;
761 pdev
->ctrl_buf
[1] = (pdev
->motor_pan
->val
>> 8);
763 if (pdev
->motor_tilt
->is_new
) {
764 pdev
->ctrl_buf
[2] = pdev
->motor_tilt
->val
& 0xFF;
765 pdev
->ctrl_buf
[3] = (pdev
->motor_tilt
->val
>> 8);
767 if (pdev
->motor_pan
->is_new
|| pdev
->motor_tilt
->is_new
) {
768 ret
= send_control_msg(pdev
, SET_MPT_CTL
,
769 PT_RELATIVE_CONTROL_FORMATTER
,
778 static int pwc_s_ctrl(struct v4l2_ctrl
*ctrl
)
780 struct pwc_device
*pdev
=
781 container_of(ctrl
->handler
, struct pwc_device
, ctrl_handler
);
785 case V4L2_CID_BRIGHTNESS
:
786 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
787 BRIGHTNESS_FORMATTER
, ctrl
->val
);
789 case V4L2_CID_CONTRAST
:
790 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
791 CONTRAST_FORMATTER
, ctrl
->val
);
793 case V4L2_CID_SATURATION
:
794 ret
= pwc_set_s8_ctrl(pdev
, SET_CHROM_CTL
,
795 pdev
->saturation_fmt
, ctrl
->val
);
798 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
799 GAMMA_FORMATTER
, ctrl
->val
);
801 case V4L2_CID_AUTO_WHITE_BALANCE
:
802 ret
= pwc_set_awb(pdev
);
804 case V4L2_CID_AUTOGAIN
:
805 if (DEVICE_USE_CODEC2(pdev
->type
))
806 ret
= pwc_set_autogain(pdev
);
807 else if (DEVICE_USE_CODEC3(pdev
->type
))
808 ret
= pwc_set_autogain_expo(pdev
);
812 case V4L2_CID_EXPOSURE_AUTO
:
813 if (DEVICE_USE_CODEC2(pdev
->type
))
814 ret
= pwc_set_exposure_auto(pdev
);
818 case V4L2_CID_COLORFX
:
819 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
820 COLOUR_MODE_FORMATTER
,
821 ctrl
->val
? 0 : 0xff);
823 case PWC_CID_CUSTOM(autocontour
):
824 if (pdev
->autocontour
->is_new
) {
825 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
826 AUTO_CONTOUR_FORMATTER
,
827 pdev
->autocontour
->val
? 0 : 0xff);
829 if (ret
== 0 && pdev
->contour
->is_new
) {
830 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
831 PRESET_CONTOUR_FORMATTER
,
835 case V4L2_CID_BACKLIGHT_COMPENSATION
:
836 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
837 BACK_LIGHT_COMPENSATION_FORMATTER
,
838 ctrl
->val
? 0 : 0xff);
840 case V4L2_CID_BAND_STOP_FILTER
:
841 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
842 FLICKERLESS_MODE_FORMATTER
,
843 ctrl
->val
? 0 : 0xff);
845 case PWC_CID_CUSTOM(noise_reduction
):
846 ret
= pwc_set_u8_ctrl(pdev
, SET_LUM_CTL
,
847 DYNAMIC_NOISE_CONTROL_FORMATTER
,
850 case PWC_CID_CUSTOM(save_user
):
851 ret
= pwc_button_ctrl(pdev
, SAVE_USER_DEFAULTS_FORMATTER
);
853 case PWC_CID_CUSTOM(restore_user
):
854 ret
= pwc_button_ctrl(pdev
, RESTORE_USER_DEFAULTS_FORMATTER
);
856 case PWC_CID_CUSTOM(restore_factory
):
857 ret
= pwc_button_ctrl(pdev
,
858 RESTORE_FACTORY_DEFAULTS_FORMATTER
);
860 case PWC_CID_CUSTOM(awb_speed
):
861 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
862 AWB_CONTROL_SPEED_FORMATTER
,
865 case PWC_CID_CUSTOM(awb_delay
):
866 ret
= pwc_set_u8_ctrl(pdev
, SET_CHROM_CTL
,
867 AWB_CONTROL_DELAY_FORMATTER
,
870 case V4L2_CID_PAN_RELATIVE
:
871 ret
= pwc_set_motor(pdev
);
878 PWC_ERROR("s_ctrl %s error %d\n", ctrl
->name
, ret
);
883 static int pwc_enum_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_fmtdesc
*f
)
885 struct pwc_device
*pdev
= video_drvdata(file
);
887 /* We only support two format: the raw format, and YUV */
891 f
->pixelformat
= pdev
->type
<= 646 ? V4L2_PIX_FMT_PWC1
: V4L2_PIX_FMT_PWC2
;
892 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
893 strlcpy(f
->description
, "Raw Philips Webcam", sizeof(f
->description
));
896 f
->pixelformat
= V4L2_PIX_FMT_YUV420
;
897 strlcpy(f
->description
, "4:2:0, planar, Y-Cb-Cr", sizeof(f
->description
));
905 static int pwc_g_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
907 struct pwc_device
*pdev
= video_drvdata(file
);
909 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
912 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
913 pdev
->width
, pdev
->height
);
914 pwc_vidioc_fill_fmt(f
, pdev
->width
, pdev
->height
, pdev
->pixfmt
);
918 static int pwc_try_fmt_vid_cap(struct file
*file
, void *fh
, struct v4l2_format
*f
)
920 struct pwc_device
*pdev
= video_drvdata(file
);
922 return pwc_vidioc_try_fmt(pdev
, f
);
925 static int pwc_enum_framesizes(struct file
*file
, void *fh
,
926 struct v4l2_frmsizeenum
*fsize
)
928 struct pwc_device
*pdev
= video_drvdata(file
);
929 unsigned int i
= 0, index
= fsize
->index
;
931 if (fsize
->pixel_format
== V4L2_PIX_FMT_YUV420
||
932 (fsize
->pixel_format
== V4L2_PIX_FMT_PWC1
&&
933 DEVICE_USE_CODEC1(pdev
->type
)) ||
934 (fsize
->pixel_format
== V4L2_PIX_FMT_PWC2
&&
935 DEVICE_USE_CODEC23(pdev
->type
))) {
936 for (i
= 0; i
< PSZ_MAX
; i
++) {
937 if (!(pdev
->image_mask
& (1UL << i
)))
940 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
941 fsize
->discrete
.width
= pwc_image_sizes
[i
][0];
942 fsize
->discrete
.height
= pwc_image_sizes
[i
][1];
950 static int pwc_enum_frameintervals(struct file
*file
, void *fh
,
951 struct v4l2_frmivalenum
*fival
)
953 struct pwc_device
*pdev
= video_drvdata(file
);
957 for (i
= 0; i
< PSZ_MAX
; i
++) {
958 if (pwc_image_sizes
[i
][0] == fival
->width
&&
959 pwc_image_sizes
[i
][1] == fival
->height
) {
965 /* TODO: Support raw format */
966 if (size
< 0 || fival
->pixel_format
!= V4L2_PIX_FMT_YUV420
)
969 i
= pwc_get_fps(pdev
, fival
->index
, size
);
973 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
974 fival
->discrete
.numerator
= 1;
975 fival
->discrete
.denominator
= i
;
980 static int pwc_g_parm(struct file
*file
, void *fh
,
981 struct v4l2_streamparm
*parm
)
983 struct pwc_device
*pdev
= video_drvdata(file
);
985 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
988 memset(parm
, 0, sizeof(*parm
));
990 parm
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
991 parm
->parm
.capture
.readbuffers
= MIN_FRAMES
;
992 parm
->parm
.capture
.capability
|= V4L2_CAP_TIMEPERFRAME
;
993 parm
->parm
.capture
.timeperframe
.denominator
= pdev
->vframes
;
994 parm
->parm
.capture
.timeperframe
.numerator
= 1;
999 static int pwc_s_parm(struct file
*file
, void *fh
,
1000 struct v4l2_streamparm
*parm
)
1002 struct pwc_device
*pdev
= video_drvdata(file
);
1003 int compression
= 0;
1006 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1009 /* If timeperframe == 0, then reset the framerate to the nominal value.
1010 We pick a high framerate here, and let pwc_set_video_mode() figure
1011 out the best match. */
1012 if (parm
->parm
.capture
.timeperframe
.numerator
== 0 ||
1013 parm
->parm
.capture
.timeperframe
.denominator
== 0)
1016 fps
= parm
->parm
.capture
.timeperframe
.denominator
/
1017 parm
->parm
.capture
.timeperframe
.numerator
;
1019 if (vb2_is_busy(&pdev
->vb_queue
))
1022 ret
= pwc_set_video_mode(pdev
, pdev
->width
, pdev
->height
, pdev
->pixfmt
,
1023 fps
, &compression
, 0);
1025 pwc_g_parm(file
, fh
, parm
);
1030 const struct v4l2_ioctl_ops pwc_ioctl_ops
= {
1031 .vidioc_querycap
= pwc_querycap
,
1032 .vidioc_enum_input
= pwc_enum_input
,
1033 .vidioc_g_input
= pwc_g_input
,
1034 .vidioc_s_input
= pwc_s_input
,
1035 .vidioc_enum_fmt_vid_cap
= pwc_enum_fmt_vid_cap
,
1036 .vidioc_g_fmt_vid_cap
= pwc_g_fmt_vid_cap
,
1037 .vidioc_s_fmt_vid_cap
= pwc_s_fmt_vid_cap
,
1038 .vidioc_try_fmt_vid_cap
= pwc_try_fmt_vid_cap
,
1039 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1040 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1041 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1042 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1043 .vidioc_streamon
= vb2_ioctl_streamon
,
1044 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1045 .vidioc_log_status
= v4l2_ctrl_log_status
,
1046 .vidioc_enum_framesizes
= pwc_enum_framesizes
,
1047 .vidioc_enum_frameintervals
= pwc_enum_frameintervals
,
1048 .vidioc_g_parm
= pwc_g_parm
,
1049 .vidioc_s_parm
= pwc_s_parm
,
1050 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1051 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,