1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Linux driver for Philips webcam
3 USB and Video4Linux interface part.
4 (C) 1999-2004 Nemosoft Unv.
5 (C) 2004-2006 Luc Saillard (luc@saillard.org)
6 (C) 2011 Hans de Goede <hdegoede@redhat.com>
8 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
9 driver and thus may have bugs that are not present in the original version.
10 Please send bug reports and support requests to <luc@saillard.org>.
11 The decompression routines have been implemented by reverse-engineering the
12 Nemosoft binary pwcx module. Caveat emptor.
17 #include <linux/errno.h>
18 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/poll.h>
22 #include <linux/vmalloc.h>
23 #include <linux/jiffies.h>
28 #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
30 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
31 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
33 static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
34 .g_volatile_ctrl = pwc_g_volatile_ctrl,
38 enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
39 enum { custom_autocontour, custom_contour, custom_noise_reduction,
40 custom_awb_speed, custom_awb_delay,
41 custom_save_user, custom_restore_user, custom_restore_factory };
43 static const char * const pwc_auto_whitebal_qmenu[] = {
44 "Indoor (Incandescant Lighting) Mode",
45 "Outdoor (Sunlight) Mode",
46 "Indoor (Fluorescent Lighting) Mode",
52 static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
54 .id = V4L2_CID_AUTO_WHITE_BALANCE,
55 .type = V4L2_CTRL_TYPE_MENU,
57 .qmenu = pwc_auto_whitebal_qmenu,
60 static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
62 .id = PWC_CID_CUSTOM(autocontour),
63 .type = V4L2_CTRL_TYPE_BOOLEAN,
64 .name = "Auto contour",
70 static const struct v4l2_ctrl_config pwc_contour_cfg = {
72 .id = PWC_CID_CUSTOM(contour),
73 .type = V4L2_CTRL_TYPE_INTEGER,
75 .flags = V4L2_CTRL_FLAG_SLIDER,
81 static const struct v4l2_ctrl_config pwc_backlight_cfg = {
83 .id = V4L2_CID_BACKLIGHT_COMPENSATION,
84 .type = V4L2_CTRL_TYPE_BOOLEAN,
90 static const struct v4l2_ctrl_config pwc_flicker_cfg = {
92 .id = V4L2_CID_BAND_STOP_FILTER,
93 .type = V4L2_CTRL_TYPE_BOOLEAN,
99 static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
100 .ops = &pwc_ctrl_ops,
101 .id = PWC_CID_CUSTOM(noise_reduction),
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Dynamic Noise Reduction",
109 static const struct v4l2_ctrl_config pwc_save_user_cfg = {
110 .ops = &pwc_ctrl_ops,
111 .id = PWC_CID_CUSTOM(save_user),
112 .type = V4L2_CTRL_TYPE_BUTTON,
113 .name = "Save User Settings",
116 static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
117 .ops = &pwc_ctrl_ops,
118 .id = PWC_CID_CUSTOM(restore_user),
119 .type = V4L2_CTRL_TYPE_BUTTON,
120 .name = "Restore User Settings",
123 static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
124 .ops = &pwc_ctrl_ops,
125 .id = PWC_CID_CUSTOM(restore_factory),
126 .type = V4L2_CTRL_TYPE_BUTTON,
127 .name = "Restore Factory Settings",
130 static const struct v4l2_ctrl_config pwc_awb_speed_cfg = {
131 .ops = &pwc_ctrl_ops,
132 .id = PWC_CID_CUSTOM(awb_speed),
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Auto White Balance Speed",
140 static const struct v4l2_ctrl_config pwc_awb_delay_cfg = {
141 .ops = &pwc_ctrl_ops,
142 .id = PWC_CID_CUSTOM(awb_delay),
143 .type = V4L2_CTRL_TYPE_INTEGER,
144 .name = "Auto White Balance Delay",
150 int pwc_init_controls(struct pwc_device *pdev)
152 struct v4l2_ctrl_handler *hdl;
153 struct v4l2_ctrl_config cfg;
156 hdl = &pdev->ctrl_handler;
157 r = v4l2_ctrl_handler_init(hdl, 20);
161 /* Brightness, contrast, saturation, gamma */
162 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
165 pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
166 V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
168 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
171 pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
172 V4L2_CID_CONTRAST, 0, 63, 1, def);
174 if (pdev->type >= 675) {
175 if (pdev->type < 730)
176 pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
178 pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
179 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
181 if (r || def < -100 || def > 100)
183 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
184 V4L2_CID_SATURATION, -100, 100, 1, def);
187 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
190 pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
191 V4L2_CID_GAMMA, 0, 31, 1, def);
193 /* auto white balance, red gain, blue gain */
194 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
195 if (r || def > awb_auto)
197 cfg = pwc_auto_white_balance_cfg;
198 cfg.name = v4l2_ctrl_get_name(cfg.id);
200 pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
201 /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
202 if (!pdev->auto_white_balance)
205 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
206 PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
209 pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
210 V4L2_CID_RED_BALANCE, 0, 255, 1, def);
212 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
213 PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
216 pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
217 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
219 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true);
222 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
223 if (r || (def != 0 && def != 0xff))
225 /* Note a register value if 0 means auto gain is on */
226 pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
227 V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
231 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
234 pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
235 V4L2_CID_GAIN, 0, 63, 1, def);
237 /* auto exposure, exposure */
238 if (DEVICE_USE_CODEC2(pdev->type)) {
239 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
241 if (r || (def != 0 && def != 0xff))
244 * def = 0 auto, def = ff manual
245 * menu idx 0 = auto, idx 1 = manual
247 pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
249 V4L2_CID_EXPOSURE_AUTO,
251 if (!pdev->exposure_auto)
254 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
255 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
256 READ_SHUTTER_FORMATTER, &def);
259 pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
260 V4L2_CID_EXPOSURE, 0, 655, 1, def);
261 /* CODEC2: separate auto gain & auto exposure */
262 v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
263 v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
264 V4L2_EXPOSURE_MANUAL, true);
265 } else if (DEVICE_USE_CODEC3(pdev->type)) {
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, 255, 1, def);
273 /* CODEC3: both gain and exposure controlled by autogain */
274 pdev->autogain_expo_cluster[0] = pdev->autogain;
275 pdev->autogain_expo_cluster[1] = pdev->gain;
276 pdev->autogain_expo_cluster[2] = pdev->exposure;
277 v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
281 /* color / bw setting */
282 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
284 if (r || (def != 0 && def != 0xff))
286 /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
287 pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
288 V4L2_CID_COLORFX, 1, 0, def == 0);
290 /* autocontour, contour */
291 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
292 if (r || (def != 0 && def != 0xff))
294 cfg = pwc_autocontour_cfg;
296 pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
297 if (!pdev->autocontour)
300 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
303 cfg = pwc_contour_cfg;
305 pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
307 v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
310 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
311 BACK_LIGHT_COMPENSATION_FORMATTER, &def);
312 if (r || (def != 0 && def != 0xff))
314 cfg = pwc_backlight_cfg;
315 cfg.name = v4l2_ctrl_get_name(cfg.id);
317 pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
319 /* flikker rediction */
320 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
321 FLICKERLESS_MODE_FORMATTER, &def);
322 if (r || (def != 0 && def != 0xff))
324 cfg = pwc_flicker_cfg;
325 cfg.name = v4l2_ctrl_get_name(cfg.id);
327 pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
329 /* Dynamic noise reduction */
330 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
331 DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
334 cfg = pwc_noise_reduction_cfg;
336 pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
338 /* Save / Restore User / Factory Settings */
339 pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
340 pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
342 if (pdev->restore_user)
343 pdev->restore_user->flags |= V4L2_CTRL_FLAG_UPDATE;
344 pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
345 &pwc_restore_factory_cfg,
347 if (pdev->restore_factory)
348 pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE;
350 /* Auto White Balance speed & delay */
351 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
352 AWB_CONTROL_SPEED_FORMATTER, &def);
353 if (r || def < 1 || def > 32)
355 cfg = pwc_awb_speed_cfg;
357 pdev->awb_speed = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
359 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
360 AWB_CONTROL_DELAY_FORMATTER, &def);
363 cfg = pwc_awb_delay_cfg;
365 pdev->awb_delay = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
367 if (!(pdev->features & FEATURE_MOTOR_PANTILT))
370 /* Motor pan / tilt / reset */
371 pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
372 V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0);
373 if (!pdev->motor_pan)
375 pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
376 V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0);
377 pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
378 V4L2_CID_PAN_RESET, 0, 0, 0, 0);
379 pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
380 V4L2_CID_TILT_RESET, 0, 0, 0, 0);
381 v4l2_ctrl_cluster(4, &pdev->motor_pan);
386 static void pwc_vidioc_fill_fmt(struct v4l2_format *f,
387 int width, int height, u32 pixfmt)
389 memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
390 f->fmt.pix.width = width;
391 f->fmt.pix.height = height;
392 f->fmt.pix.field = V4L2_FIELD_NONE;
393 f->fmt.pix.pixelformat = pixfmt;
394 f->fmt.pix.bytesperline = f->fmt.pix.width;
395 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width * 3 / 2;
396 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
397 PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n",
400 f->fmt.pix.bytesperline,
401 f->fmt.pix.sizeimage,
402 (f->fmt.pix.pixelformat)&255,
403 (f->fmt.pix.pixelformat>>8)&255,
404 (f->fmt.pix.pixelformat>>16)&255,
405 (f->fmt.pix.pixelformat>>24)&255);
408 /* ioctl(VIDIOC_TRY_FMT) */
409 static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
413 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
414 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
418 switch (f->fmt.pix.pixelformat) {
419 case V4L2_PIX_FMT_YUV420:
421 case V4L2_PIX_FMT_PWC1:
422 if (DEVICE_USE_CODEC23(pdev->type)) {
423 PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n");
424 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
427 case V4L2_PIX_FMT_PWC2:
428 if (DEVICE_USE_CODEC1(pdev->type)) {
429 PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n");
430 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
434 PWC_DEBUG_IOCTL("Unsupported pixel format\n");
435 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
438 size = pwc_get_size(pdev, f->fmt.pix.width, f->fmt.pix.height);
439 pwc_vidioc_fill_fmt(f,
440 pwc_image_sizes[size][0],
441 pwc_image_sizes[size][1],
442 f->fmt.pix.pixelformat);
447 /* ioctl(VIDIOC_SET_FMT) */
449 static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
451 struct pwc_device *pdev = video_drvdata(file);
452 int ret, pixelformat, compression = 0;
454 ret = pwc_vidioc_try_fmt(pdev, f);
458 if (vb2_is_busy(&pdev->vb_queue))
461 pixelformat = f->fmt.pix.pixelformat;
463 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d format=%c%c%c%c\n",
464 f->fmt.pix.width, f->fmt.pix.height, pdev->vframes,
466 (pixelformat>>8)&255,
467 (pixelformat>>16)&255,
468 (pixelformat>>24)&255);
470 ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height,
471 pixelformat, 30, &compression, 0);
473 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
475 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
479 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
481 struct pwc_device *pdev = video_drvdata(file);
483 strscpy(cap->driver, PWC_NAME, sizeof(cap->driver));
484 strscpy(cap->card, pdev->vdev.name, sizeof(cap->card));
485 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
489 static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
491 if (i->index) /* Only one INPUT is supported */
494 strscpy(i->name, "Camera", sizeof(i->name));
495 i->type = V4L2_INPUT_TYPE_CAMERA;
499 static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
505 static int pwc_s_input(struct file *file, void *fh, unsigned int i)
507 return i ? -EINVAL : 0;
510 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
512 struct pwc_device *pdev =
513 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
517 case V4L2_CID_AUTO_WHITE_BALANCE:
518 if (pdev->color_bal_valid &&
519 (pdev->auto_white_balance->val != awb_auto ||
521 pdev->last_color_bal_update + HZ / 4))) {
522 pdev->red_balance->val = pdev->last_red_balance;
523 pdev->blue_balance->val = pdev->last_blue_balance;
526 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
527 READ_RED_GAIN_FORMATTER,
528 &pdev->red_balance->val);
531 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
532 READ_BLUE_GAIN_FORMATTER,
533 &pdev->blue_balance->val);
536 pdev->last_red_balance = pdev->red_balance->val;
537 pdev->last_blue_balance = pdev->blue_balance->val;
538 pdev->last_color_bal_update = jiffies;
539 pdev->color_bal_valid = true;
541 case V4L2_CID_AUTOGAIN:
542 if (pdev->gain_valid && time_before(jiffies,
543 pdev->last_gain_update + HZ / 4)) {
544 pdev->gain->val = pdev->last_gain;
547 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
548 READ_AGC_FORMATTER, &pdev->gain->val);
551 pdev->last_gain = pdev->gain->val;
552 pdev->last_gain_update = jiffies;
553 pdev->gain_valid = true;
554 if (!DEVICE_USE_CODEC3(pdev->type))
556 /* For CODEC3 where autogain also controls expo */
558 case V4L2_CID_EXPOSURE_AUTO:
559 if (pdev->exposure_valid && time_before(jiffies,
560 pdev->last_exposure_update + HZ / 4)) {
561 pdev->exposure->val = pdev->last_exposure;
564 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
565 READ_SHUTTER_FORMATTER,
566 &pdev->exposure->val);
569 pdev->last_exposure = pdev->exposure->val;
570 pdev->last_exposure_update = jiffies;
571 pdev->exposure_valid = true;
578 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
583 static int pwc_set_awb(struct pwc_device *pdev)
587 if (pdev->auto_white_balance->is_new) {
588 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
590 pdev->auto_white_balance->val);
594 if (pdev->auto_white_balance->val != awb_manual)
595 pdev->color_bal_valid = false; /* Force cache update */
598 * If this is a preset, update our red / blue balance values
599 * so that events get generated for the new preset values
601 if (pdev->auto_white_balance->val == awb_indoor ||
602 pdev->auto_white_balance->val == awb_outdoor ||
603 pdev->auto_white_balance->val == awb_fl)
604 pwc_g_volatile_ctrl(pdev->auto_white_balance);
606 if (pdev->auto_white_balance->val != awb_manual)
609 if (pdev->red_balance->is_new) {
610 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
611 PRESET_MANUAL_RED_GAIN_FORMATTER,
612 pdev->red_balance->val);
617 if (pdev->blue_balance->is_new) {
618 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
619 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
620 pdev->blue_balance->val);
627 /* For CODEC2 models which have separate autogain and auto exposure */
628 static int pwc_set_autogain(struct pwc_device *pdev)
632 if (pdev->autogain->is_new) {
633 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
635 pdev->autogain->val ? 0 : 0xff);
639 if (pdev->autogain->val)
640 pdev->gain_valid = false; /* Force cache update */
643 if (pdev->autogain->val)
646 if (pdev->gain->is_new) {
647 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
648 PRESET_AGC_FORMATTER,
656 /* For CODEC2 models which have separate autogain and auto exposure */
657 static int pwc_set_exposure_auto(struct pwc_device *pdev)
660 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
662 if (pdev->exposure_auto->is_new) {
663 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
664 SHUTTER_MODE_FORMATTER,
670 pdev->exposure_valid = false; /* Force cache update */
676 if (pdev->exposure->is_new) {
677 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
678 PRESET_SHUTTER_FORMATTER,
679 pdev->exposure->val);
686 /* For CODEC3 models which have autogain controlling both gain and exposure */
687 static int pwc_set_autogain_expo(struct pwc_device *pdev)
691 if (pdev->autogain->is_new) {
692 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
694 pdev->autogain->val ? 0 : 0xff);
698 if (pdev->autogain->val) {
699 pdev->gain_valid = false; /* Force cache update */
700 pdev->exposure_valid = false; /* Force cache update */
704 if (pdev->autogain->val)
707 if (pdev->gain->is_new) {
708 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
709 PRESET_AGC_FORMATTER,
715 if (pdev->exposure->is_new) {
716 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
717 PRESET_SHUTTER_FORMATTER,
718 pdev->exposure->val);
725 static int pwc_set_motor(struct pwc_device *pdev)
729 pdev->ctrl_buf[0] = 0;
730 if (pdev->motor_pan_reset->is_new)
731 pdev->ctrl_buf[0] |= 0x01;
732 if (pdev->motor_tilt_reset->is_new)
733 pdev->ctrl_buf[0] |= 0x02;
734 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
735 ret = send_control_msg(pdev, SET_MPT_CTL,
736 PT_RESET_CONTROL_FORMATTER,
742 memset(pdev->ctrl_buf, 0, 4);
743 if (pdev->motor_pan->is_new) {
744 pdev->ctrl_buf[0] = pdev->motor_pan->val & 0xFF;
745 pdev->ctrl_buf[1] = (pdev->motor_pan->val >> 8);
747 if (pdev->motor_tilt->is_new) {
748 pdev->ctrl_buf[2] = pdev->motor_tilt->val & 0xFF;
749 pdev->ctrl_buf[3] = (pdev->motor_tilt->val >> 8);
751 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
752 ret = send_control_msg(pdev, SET_MPT_CTL,
753 PT_RELATIVE_CONTROL_FORMATTER,
762 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
764 struct pwc_device *pdev =
765 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
769 case V4L2_CID_BRIGHTNESS:
770 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
771 BRIGHTNESS_FORMATTER, ctrl->val);
773 case V4L2_CID_CONTRAST:
774 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
775 CONTRAST_FORMATTER, ctrl->val);
777 case V4L2_CID_SATURATION:
778 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
779 pdev->saturation_fmt, ctrl->val);
782 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
783 GAMMA_FORMATTER, ctrl->val);
785 case V4L2_CID_AUTO_WHITE_BALANCE:
786 ret = pwc_set_awb(pdev);
788 case V4L2_CID_AUTOGAIN:
789 if (DEVICE_USE_CODEC2(pdev->type))
790 ret = pwc_set_autogain(pdev);
791 else if (DEVICE_USE_CODEC3(pdev->type))
792 ret = pwc_set_autogain_expo(pdev);
796 case V4L2_CID_EXPOSURE_AUTO:
797 if (DEVICE_USE_CODEC2(pdev->type))
798 ret = pwc_set_exposure_auto(pdev);
802 case V4L2_CID_COLORFX:
803 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
804 COLOUR_MODE_FORMATTER,
805 ctrl->val ? 0 : 0xff);
807 case PWC_CID_CUSTOM(autocontour):
808 if (pdev->autocontour->is_new) {
809 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
810 AUTO_CONTOUR_FORMATTER,
811 pdev->autocontour->val ? 0 : 0xff);
813 if (ret == 0 && pdev->contour->is_new) {
814 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
815 PRESET_CONTOUR_FORMATTER,
819 case V4L2_CID_BACKLIGHT_COMPENSATION:
820 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
821 BACK_LIGHT_COMPENSATION_FORMATTER,
822 ctrl->val ? 0 : 0xff);
824 case V4L2_CID_BAND_STOP_FILTER:
825 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
826 FLICKERLESS_MODE_FORMATTER,
827 ctrl->val ? 0 : 0xff);
829 case PWC_CID_CUSTOM(noise_reduction):
830 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
831 DYNAMIC_NOISE_CONTROL_FORMATTER,
834 case PWC_CID_CUSTOM(save_user):
835 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
837 case PWC_CID_CUSTOM(restore_user):
838 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
840 case PWC_CID_CUSTOM(restore_factory):
841 ret = pwc_button_ctrl(pdev,
842 RESTORE_FACTORY_DEFAULTS_FORMATTER);
844 case PWC_CID_CUSTOM(awb_speed):
845 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
846 AWB_CONTROL_SPEED_FORMATTER,
849 case PWC_CID_CUSTOM(awb_delay):
850 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
851 AWB_CONTROL_DELAY_FORMATTER,
854 case V4L2_CID_PAN_RELATIVE:
855 ret = pwc_set_motor(pdev);
862 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
867 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
869 struct pwc_device *pdev = video_drvdata(file);
871 /* We only support two format: the raw format, and YUV */
875 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
878 f->pixelformat = V4L2_PIX_FMT_YUV420;
886 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
888 struct pwc_device *pdev = video_drvdata(file);
890 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
893 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
894 pdev->width, pdev->height);
895 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
899 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
901 struct pwc_device *pdev = video_drvdata(file);
903 return pwc_vidioc_try_fmt(pdev, f);
906 static int pwc_enum_framesizes(struct file *file, void *fh,
907 struct v4l2_frmsizeenum *fsize)
909 struct pwc_device *pdev = video_drvdata(file);
910 unsigned int i = 0, index = fsize->index;
912 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 ||
913 (fsize->pixel_format == V4L2_PIX_FMT_PWC1 &&
914 DEVICE_USE_CODEC1(pdev->type)) ||
915 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 &&
916 DEVICE_USE_CODEC23(pdev->type))) {
917 for (i = 0; i < PSZ_MAX; i++) {
918 if (!(pdev->image_mask & (1UL << i)))
921 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
922 fsize->discrete.width = pwc_image_sizes[i][0];
923 fsize->discrete.height = pwc_image_sizes[i][1];
931 static int pwc_enum_frameintervals(struct file *file, void *fh,
932 struct v4l2_frmivalenum *fival)
934 struct pwc_device *pdev = video_drvdata(file);
938 for (i = 0; i < PSZ_MAX; i++) {
939 if (pwc_image_sizes[i][0] == fival->width &&
940 pwc_image_sizes[i][1] == fival->height) {
946 /* TODO: Support raw format */
947 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
950 i = pwc_get_fps(pdev, fival->index, size);
954 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
955 fival->discrete.numerator = 1;
956 fival->discrete.denominator = i;
961 static int pwc_g_parm(struct file *file, void *fh,
962 struct v4l2_streamparm *parm)
964 struct pwc_device *pdev = video_drvdata(file);
966 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
969 memset(parm, 0, sizeof(*parm));
971 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
972 parm->parm.capture.readbuffers = MIN_FRAMES;
973 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
974 parm->parm.capture.timeperframe.denominator = pdev->vframes;
975 parm->parm.capture.timeperframe.numerator = 1;
980 static int pwc_s_parm(struct file *file, void *fh,
981 struct v4l2_streamparm *parm)
983 struct pwc_device *pdev = video_drvdata(file);
987 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
990 /* If timeperframe == 0, then reset the framerate to the nominal value.
991 We pick a high framerate here, and let pwc_set_video_mode() figure
992 out the best match. */
993 if (parm->parm.capture.timeperframe.numerator == 0 ||
994 parm->parm.capture.timeperframe.denominator == 0)
997 fps = parm->parm.capture.timeperframe.denominator /
998 parm->parm.capture.timeperframe.numerator;
1000 if (vb2_is_busy(&pdev->vb_queue))
1003 ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
1004 fps, &compression, 0);
1006 pwc_g_parm(file, fh, parm);
1011 const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1012 .vidioc_querycap = pwc_querycap,
1013 .vidioc_enum_input = pwc_enum_input,
1014 .vidioc_g_input = pwc_g_input,
1015 .vidioc_s_input = pwc_s_input,
1016 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
1017 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
1018 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
1019 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
1020 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1021 .vidioc_querybuf = vb2_ioctl_querybuf,
1022 .vidioc_qbuf = vb2_ioctl_qbuf,
1023 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1024 .vidioc_streamon = vb2_ioctl_streamon,
1025 .vidioc_streamoff = vb2_ioctl_streamoff,
1026 .vidioc_log_status = v4l2_ctrl_log_status,
1027 .vidioc_enum_framesizes = pwc_enum_framesizes,
1028 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
1029 .vidioc_g_parm = pwc_g_parm,
1030 .vidioc_s_parm = pwc_s_parm,
1031 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1032 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,