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> 7 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. 13 14 15 */ 16 17 #include <linux/errno.h> 18 #include <linux/init.h> 19 #include <linux/mm.h> 20 #include <linux/module.h> 21 #include <linux/poll.h> 22 #include <linux/vmalloc.h> 23 #include <linux/jiffies.h> 24 #include <asm/io.h> 25 26 #include "pwc.h" 27 28 #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl) 29 30 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl); 31 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl); 32 33 static const struct v4l2_ctrl_ops pwc_ctrl_ops = { 34 .g_volatile_ctrl = pwc_g_volatile_ctrl, 35 .s_ctrl = pwc_s_ctrl, 36 }; 37 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 }; 42 43 static const char * const pwc_auto_whitebal_qmenu[] = { 44 "Indoor (Incandescant Lighting) Mode", 45 "Outdoor (Sunlight) Mode", 46 "Indoor (Fluorescent Lighting) Mode", 47 "Manual Mode", 48 "Auto Mode", 49 NULL 50 }; 51 52 static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = { 53 .ops = &pwc_ctrl_ops, 54 .id = V4L2_CID_AUTO_WHITE_BALANCE, 55 .type = V4L2_CTRL_TYPE_MENU, 56 .max = awb_auto, 57 .qmenu = pwc_auto_whitebal_qmenu, 58 }; 59 60 static const struct v4l2_ctrl_config pwc_autocontour_cfg = { 61 .ops = &pwc_ctrl_ops, 62 .id = PWC_CID_CUSTOM(autocontour), 63 .type = V4L2_CTRL_TYPE_BOOLEAN, 64 .name = "Auto contour", 65 .min = 0, 66 .max = 1, 67 .step = 1, 68 }; 69 70 static const struct v4l2_ctrl_config pwc_contour_cfg = { 71 .ops = &pwc_ctrl_ops, 72 .id = PWC_CID_CUSTOM(contour), 73 .type = V4L2_CTRL_TYPE_INTEGER, 74 .name = "Contour", 75 .flags = V4L2_CTRL_FLAG_SLIDER, 76 .min = 0, 77 .max = 63, 78 .step = 1, 79 }; 80 81 static const struct v4l2_ctrl_config pwc_backlight_cfg = { 82 .ops = &pwc_ctrl_ops, 83 .id = V4L2_CID_BACKLIGHT_COMPENSATION, 84 .type = V4L2_CTRL_TYPE_BOOLEAN, 85 .min = 0, 86 .max = 1, 87 .step = 1, 88 }; 89 90 static const struct v4l2_ctrl_config pwc_flicker_cfg = { 91 .ops = &pwc_ctrl_ops, 92 .id = V4L2_CID_BAND_STOP_FILTER, 93 .type = V4L2_CTRL_TYPE_BOOLEAN, 94 .min = 0, 95 .max = 1, 96 .step = 1, 97 }; 98 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", 104 .min = 0, 105 .max = 3, 106 .step = 1, 107 }; 108 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", 114 }; 115 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", 121 }; 122 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", 128 }; 129 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", 135 .min = 1, 136 .max = 32, 137 .step = 1, 138 }; 139 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", 145 .min = 0, 146 .max = 63, 147 .step = 1, 148 }; 149 150 int pwc_init_controls(struct pwc_device *pdev) 151 { 152 struct v4l2_ctrl_handler *hdl; 153 struct v4l2_ctrl_config cfg; 154 int r, def; 155 156 hdl = &pdev->ctrl_handler; 157 r = v4l2_ctrl_handler_init(hdl, 20); 158 if (r) 159 return r; 160 161 /* Brightness, contrast, saturation, gamma */ 162 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def); 163 if (r || def > 127) 164 def = 63; 165 pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 166 V4L2_CID_BRIGHTNESS, 0, 127, 1, def); 167 168 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def); 169 if (r || def > 63) 170 def = 31; 171 pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 172 V4L2_CID_CONTRAST, 0, 63, 1, def); 173 174 if (pdev->type >= 675) { 175 if (pdev->type < 730) 176 pdev->saturation_fmt = SATURATION_MODE_FORMATTER2; 177 else 178 pdev->saturation_fmt = SATURATION_MODE_FORMATTER1; 179 r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt, 180 &def); 181 if (r || def < -100 || def > 100) 182 def = 0; 183 pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 184 V4L2_CID_SATURATION, -100, 100, 1, def); 185 } 186 187 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def); 188 if (r || def > 31) 189 def = 15; 190 pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 191 V4L2_CID_GAMMA, 0, 31, 1, def); 192 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) 196 def = awb_auto; 197 cfg = pwc_auto_white_balance_cfg; 198 cfg.name = v4l2_ctrl_get_name(cfg.id); 199 cfg.def = def; 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) 203 return hdl->error; 204 205 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, 206 PRESET_MANUAL_RED_GAIN_FORMATTER, &def); 207 if (r) 208 def = 127; 209 pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 210 V4L2_CID_RED_BALANCE, 0, 255, 1, def); 211 212 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, 213 PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def); 214 if (r) 215 def = 127; 216 pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 217 V4L2_CID_BLUE_BALANCE, 0, 255, 1, def); 218 219 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true); 220 221 /* autogain, gain */ 222 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def); 223 if (r || (def != 0 && def != 0xff)) 224 def = 0; 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); 228 if (!pdev->autogain) 229 return hdl->error; 230 231 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def); 232 if (r || def > 63) 233 def = 31; 234 pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, 235 V4L2_CID_GAIN, 0, 63, 1, def); 236 237 /* auto exposure, exposure */ 238 if (DEVICE_USE_CODEC2(pdev->type)) { 239 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER, 240 &def); 241 if (r || (def != 0 && def != 0xff)) 242 def = 0; 243 /* 244 * def = 0 auto, def = ff manual 245 * menu idx 0 = auto, idx 1 = manual 246 */ 247 pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl, 248 &pwc_ctrl_ops, 249 V4L2_CID_EXPOSURE_AUTO, 250 1, 0, def != 0); 251 if (!pdev->exposure_auto) 252 return hdl->error; 253 254 /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */ 255 r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, 256 READ_SHUTTER_FORMATTER, &def); 257 if (r || def > 655) 258 def = 655; 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); 269 if (r || def > 255) 270 def = 255; 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, 278 0, true); 279 } 280 281 /* color / bw setting */ 282 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER, 283 &def); 284 if (r || (def != 0 && def != 0xff)) 285 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); 289 290 /* autocontour, contour */ 291 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def); 292 if (r || (def != 0 && def != 0xff)) 293 def = 0; 294 cfg = pwc_autocontour_cfg; 295 cfg.def = def == 0; 296 pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 297 if (!pdev->autocontour) 298 return hdl->error; 299 300 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def); 301 if (r || def > 63) 302 def = 31; 303 cfg = pwc_contour_cfg; 304 cfg.def = def; 305 pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 306 307 v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false); 308 309 /* backlight */ 310 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, 311 BACK_LIGHT_COMPENSATION_FORMATTER, &def); 312 if (r || (def != 0 && def != 0xff)) 313 def = 0; 314 cfg = pwc_backlight_cfg; 315 cfg.name = v4l2_ctrl_get_name(cfg.id); 316 cfg.def = def == 0; 317 pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 318 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)) 323 def = 0; 324 cfg = pwc_flicker_cfg; 325 cfg.name = v4l2_ctrl_get_name(cfg.id); 326 cfg.def = def == 0; 327 pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 328 329 /* Dynamic noise reduction */ 330 r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, 331 DYNAMIC_NOISE_CONTROL_FORMATTER, &def); 332 if (r || def > 3) 333 def = 2; 334 cfg = pwc_noise_reduction_cfg; 335 cfg.def = def; 336 pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 337 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, 341 NULL); 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, 346 NULL); 347 if (pdev->restore_factory) 348 pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE; 349 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) 354 def = 1; 355 cfg = pwc_awb_speed_cfg; 356 cfg.def = def; 357 pdev->awb_speed = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 358 359 r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, 360 AWB_CONTROL_DELAY_FORMATTER, &def); 361 if (r || def > 63) 362 def = 0; 363 cfg = pwc_awb_delay_cfg; 364 cfg.def = def; 365 pdev->awb_delay = v4l2_ctrl_new_custom(hdl, &cfg, NULL); 366 367 if (!(pdev->features & FEATURE_MOTOR_PANTILT)) 368 return hdl->error; 369 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) 374 return hdl->error; 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); 382 383 return hdl->error; 384 } 385 386 static void pwc_vidioc_fill_fmt(struct v4l2_format *f, 387 int width, int height, u32 pixfmt) 388 { 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", 398 f->fmt.pix.width, 399 f->fmt.pix.height, 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); 406 } 407 408 /* ioctl(VIDIOC_TRY_FMT) */ 409 static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f) 410 { 411 int size; 412 413 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { 414 PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n"); 415 return -EINVAL; 416 } 417 418 switch (f->fmt.pix.pixelformat) { 419 case V4L2_PIX_FMT_YUV420: 420 break; 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; 425 } 426 break; 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; 431 } 432 break; 433 default: 434 PWC_DEBUG_IOCTL("Unsupported pixel format\n"); 435 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; 436 } 437 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); 443 444 return 0; 445 } 446 447 /* ioctl(VIDIOC_SET_FMT) */ 448 449 static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) 450 { 451 struct pwc_device *pdev = video_drvdata(file); 452 int ret, pixelformat, compression = 0; 453 454 ret = pwc_vidioc_try_fmt(pdev, f); 455 if (ret < 0) 456 return ret; 457 458 if (vb2_is_busy(&pdev->vb_queue)) 459 return -EBUSY; 460 461 pixelformat = f->fmt.pix.pixelformat; 462 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, 465 (pixelformat)&255, 466 (pixelformat>>8)&255, 467 (pixelformat>>16)&255, 468 (pixelformat>>24)&255); 469 470 ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height, 471 pixelformat, 30, &compression, 0); 472 473 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret); 474 475 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt); 476 return ret; 477 } 478 479 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 480 { 481 struct pwc_device *pdev = video_drvdata(file); 482 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)); 486 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | 487 V4L2_CAP_READWRITE; 488 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 489 return 0; 490 } 491 492 static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i) 493 { 494 if (i->index) /* Only one INPUT is supported */ 495 return -EINVAL; 496 497 strscpy(i->name, "Camera", sizeof(i->name)); 498 i->type = V4L2_INPUT_TYPE_CAMERA; 499 return 0; 500 } 501 502 static int pwc_g_input(struct file *file, void *fh, unsigned int *i) 503 { 504 *i = 0; 505 return 0; 506 } 507 508 static int pwc_s_input(struct file *file, void *fh, unsigned int i) 509 { 510 return i ? -EINVAL : 0; 511 } 512 513 static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 514 { 515 struct pwc_device *pdev = 516 container_of(ctrl->handler, struct pwc_device, ctrl_handler); 517 int ret = 0; 518 519 switch (ctrl->id) { 520 case V4L2_CID_AUTO_WHITE_BALANCE: 521 if (pdev->color_bal_valid && 522 (pdev->auto_white_balance->val != awb_auto || 523 time_before(jiffies, 524 pdev->last_color_bal_update + HZ / 4))) { 525 pdev->red_balance->val = pdev->last_red_balance; 526 pdev->blue_balance->val = pdev->last_blue_balance; 527 break; 528 } 529 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, 530 READ_RED_GAIN_FORMATTER, 531 &pdev->red_balance->val); 532 if (ret) 533 break; 534 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, 535 READ_BLUE_GAIN_FORMATTER, 536 &pdev->blue_balance->val); 537 if (ret) 538 break; 539 pdev->last_red_balance = pdev->red_balance->val; 540 pdev->last_blue_balance = pdev->blue_balance->val; 541 pdev->last_color_bal_update = jiffies; 542 pdev->color_bal_valid = true; 543 break; 544 case V4L2_CID_AUTOGAIN: 545 if (pdev->gain_valid && time_before(jiffies, 546 pdev->last_gain_update + HZ / 4)) { 547 pdev->gain->val = pdev->last_gain; 548 break; 549 } 550 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, 551 READ_AGC_FORMATTER, &pdev->gain->val); 552 if (ret) 553 break; 554 pdev->last_gain = pdev->gain->val; 555 pdev->last_gain_update = jiffies; 556 pdev->gain_valid = true; 557 if (!DEVICE_USE_CODEC3(pdev->type)) 558 break; 559 /* For CODEC3 where autogain also controls expo */ 560 /* fall through */ 561 case V4L2_CID_EXPOSURE_AUTO: 562 if (pdev->exposure_valid && time_before(jiffies, 563 pdev->last_exposure_update + HZ / 4)) { 564 pdev->exposure->val = pdev->last_exposure; 565 break; 566 } 567 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, 568 READ_SHUTTER_FORMATTER, 569 &pdev->exposure->val); 570 if (ret) 571 break; 572 pdev->last_exposure = pdev->exposure->val; 573 pdev->last_exposure_update = jiffies; 574 pdev->exposure_valid = true; 575 break; 576 default: 577 ret = -EINVAL; 578 } 579 580 if (ret) 581 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret); 582 583 return ret; 584 } 585 586 static int pwc_set_awb(struct pwc_device *pdev) 587 { 588 int ret; 589 590 if (pdev->auto_white_balance->is_new) { 591 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 592 WB_MODE_FORMATTER, 593 pdev->auto_white_balance->val); 594 if (ret) 595 return ret; 596 597 if (pdev->auto_white_balance->val != awb_manual) 598 pdev->color_bal_valid = false; /* Force cache update */ 599 600 /* 601 * If this is a preset, update our red / blue balance values 602 * so that events get generated for the new preset values 603 */ 604 if (pdev->auto_white_balance->val == awb_indoor || 605 pdev->auto_white_balance->val == awb_outdoor || 606 pdev->auto_white_balance->val == awb_fl) 607 pwc_g_volatile_ctrl(pdev->auto_white_balance); 608 } 609 if (pdev->auto_white_balance->val != awb_manual) 610 return 0; 611 612 if (pdev->red_balance->is_new) { 613 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 614 PRESET_MANUAL_RED_GAIN_FORMATTER, 615 pdev->red_balance->val); 616 if (ret) 617 return ret; 618 } 619 620 if (pdev->blue_balance->is_new) { 621 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 622 PRESET_MANUAL_BLUE_GAIN_FORMATTER, 623 pdev->blue_balance->val); 624 if (ret) 625 return ret; 626 } 627 return 0; 628 } 629 630 /* For CODEC2 models which have separate autogain and auto exposure */ 631 static int pwc_set_autogain(struct pwc_device *pdev) 632 { 633 int ret; 634 635 if (pdev->autogain->is_new) { 636 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 637 AGC_MODE_FORMATTER, 638 pdev->autogain->val ? 0 : 0xff); 639 if (ret) 640 return ret; 641 642 if (pdev->autogain->val) 643 pdev->gain_valid = false; /* Force cache update */ 644 } 645 646 if (pdev->autogain->val) 647 return 0; 648 649 if (pdev->gain->is_new) { 650 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 651 PRESET_AGC_FORMATTER, 652 pdev->gain->val); 653 if (ret) 654 return ret; 655 } 656 return 0; 657 } 658 659 /* For CODEC2 models which have separate autogain and auto exposure */ 660 static int pwc_set_exposure_auto(struct pwc_device *pdev) 661 { 662 int ret; 663 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO; 664 665 if (pdev->exposure_auto->is_new) { 666 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 667 SHUTTER_MODE_FORMATTER, 668 is_auto ? 0 : 0xff); 669 if (ret) 670 return ret; 671 672 if (is_auto) 673 pdev->exposure_valid = false; /* Force cache update */ 674 } 675 676 if (is_auto) 677 return 0; 678 679 if (pdev->exposure->is_new) { 680 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, 681 PRESET_SHUTTER_FORMATTER, 682 pdev->exposure->val); 683 if (ret) 684 return ret; 685 } 686 return 0; 687 } 688 689 /* For CODEC3 models which have autogain controlling both gain and exposure */ 690 static int pwc_set_autogain_expo(struct pwc_device *pdev) 691 { 692 int ret; 693 694 if (pdev->autogain->is_new) { 695 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 696 AGC_MODE_FORMATTER, 697 pdev->autogain->val ? 0 : 0xff); 698 if (ret) 699 return ret; 700 701 if (pdev->autogain->val) { 702 pdev->gain_valid = false; /* Force cache update */ 703 pdev->exposure_valid = false; /* Force cache update */ 704 } 705 } 706 707 if (pdev->autogain->val) 708 return 0; 709 710 if (pdev->gain->is_new) { 711 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 712 PRESET_AGC_FORMATTER, 713 pdev->gain->val); 714 if (ret) 715 return ret; 716 } 717 718 if (pdev->exposure->is_new) { 719 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, 720 PRESET_SHUTTER_FORMATTER, 721 pdev->exposure->val); 722 if (ret) 723 return ret; 724 } 725 return 0; 726 } 727 728 static int pwc_set_motor(struct pwc_device *pdev) 729 { 730 int ret; 731 732 pdev->ctrl_buf[0] = 0; 733 if (pdev->motor_pan_reset->is_new) 734 pdev->ctrl_buf[0] |= 0x01; 735 if (pdev->motor_tilt_reset->is_new) 736 pdev->ctrl_buf[0] |= 0x02; 737 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) { 738 ret = send_control_msg(pdev, SET_MPT_CTL, 739 PT_RESET_CONTROL_FORMATTER, 740 pdev->ctrl_buf, 1); 741 if (ret < 0) 742 return ret; 743 } 744 745 memset(pdev->ctrl_buf, 0, 4); 746 if (pdev->motor_pan->is_new) { 747 pdev->ctrl_buf[0] = pdev->motor_pan->val & 0xFF; 748 pdev->ctrl_buf[1] = (pdev->motor_pan->val >> 8); 749 } 750 if (pdev->motor_tilt->is_new) { 751 pdev->ctrl_buf[2] = pdev->motor_tilt->val & 0xFF; 752 pdev->ctrl_buf[3] = (pdev->motor_tilt->val >> 8); 753 } 754 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) { 755 ret = send_control_msg(pdev, SET_MPT_CTL, 756 PT_RELATIVE_CONTROL_FORMATTER, 757 pdev->ctrl_buf, 4); 758 if (ret < 0) 759 return ret; 760 } 761 762 return 0; 763 } 764 765 static int pwc_s_ctrl(struct v4l2_ctrl *ctrl) 766 { 767 struct pwc_device *pdev = 768 container_of(ctrl->handler, struct pwc_device, ctrl_handler); 769 int ret = 0; 770 771 switch (ctrl->id) { 772 case V4L2_CID_BRIGHTNESS: 773 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 774 BRIGHTNESS_FORMATTER, ctrl->val); 775 break; 776 case V4L2_CID_CONTRAST: 777 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 778 CONTRAST_FORMATTER, ctrl->val); 779 break; 780 case V4L2_CID_SATURATION: 781 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL, 782 pdev->saturation_fmt, ctrl->val); 783 break; 784 case V4L2_CID_GAMMA: 785 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 786 GAMMA_FORMATTER, ctrl->val); 787 break; 788 case V4L2_CID_AUTO_WHITE_BALANCE: 789 ret = pwc_set_awb(pdev); 790 break; 791 case V4L2_CID_AUTOGAIN: 792 if (DEVICE_USE_CODEC2(pdev->type)) 793 ret = pwc_set_autogain(pdev); 794 else if (DEVICE_USE_CODEC3(pdev->type)) 795 ret = pwc_set_autogain_expo(pdev); 796 else 797 ret = -EINVAL; 798 break; 799 case V4L2_CID_EXPOSURE_AUTO: 800 if (DEVICE_USE_CODEC2(pdev->type)) 801 ret = pwc_set_exposure_auto(pdev); 802 else 803 ret = -EINVAL; 804 break; 805 case V4L2_CID_COLORFX: 806 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 807 COLOUR_MODE_FORMATTER, 808 ctrl->val ? 0 : 0xff); 809 break; 810 case PWC_CID_CUSTOM(autocontour): 811 if (pdev->autocontour->is_new) { 812 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 813 AUTO_CONTOUR_FORMATTER, 814 pdev->autocontour->val ? 0 : 0xff); 815 } 816 if (ret == 0 && pdev->contour->is_new) { 817 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 818 PRESET_CONTOUR_FORMATTER, 819 pdev->contour->val); 820 } 821 break; 822 case V4L2_CID_BACKLIGHT_COMPENSATION: 823 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 824 BACK_LIGHT_COMPENSATION_FORMATTER, 825 ctrl->val ? 0 : 0xff); 826 break; 827 case V4L2_CID_BAND_STOP_FILTER: 828 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 829 FLICKERLESS_MODE_FORMATTER, 830 ctrl->val ? 0 : 0xff); 831 break; 832 case PWC_CID_CUSTOM(noise_reduction): 833 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, 834 DYNAMIC_NOISE_CONTROL_FORMATTER, 835 ctrl->val); 836 break; 837 case PWC_CID_CUSTOM(save_user): 838 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER); 839 break; 840 case PWC_CID_CUSTOM(restore_user): 841 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER); 842 break; 843 case PWC_CID_CUSTOM(restore_factory): 844 ret = pwc_button_ctrl(pdev, 845 RESTORE_FACTORY_DEFAULTS_FORMATTER); 846 break; 847 case PWC_CID_CUSTOM(awb_speed): 848 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 849 AWB_CONTROL_SPEED_FORMATTER, 850 ctrl->val); 851 break; 852 case PWC_CID_CUSTOM(awb_delay): 853 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, 854 AWB_CONTROL_DELAY_FORMATTER, 855 ctrl->val); 856 break; 857 case V4L2_CID_PAN_RELATIVE: 858 ret = pwc_set_motor(pdev); 859 break; 860 default: 861 ret = -EINVAL; 862 } 863 864 if (ret) 865 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret); 866 867 return ret; 868 } 869 870 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f) 871 { 872 struct pwc_device *pdev = video_drvdata(file); 873 874 /* We only support two format: the raw format, and YUV */ 875 switch (f->index) { 876 case 0: 877 /* RAW format */ 878 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2; 879 f->flags = V4L2_FMT_FLAG_COMPRESSED; 880 strscpy(f->description, "Raw Philips Webcam", 881 sizeof(f->description)); 882 break; 883 case 1: 884 f->pixelformat = V4L2_PIX_FMT_YUV420; 885 strscpy(f->description, "4:2:0, planar, Y-Cb-Cr", 886 sizeof(f->description)); 887 break; 888 default: 889 return -EINVAL; 890 } 891 return 0; 892 } 893 894 static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) 895 { 896 struct pwc_device *pdev = video_drvdata(file); 897 898 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 899 return -EINVAL; 900 901 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n", 902 pdev->width, pdev->height); 903 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt); 904 return 0; 905 } 906 907 static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) 908 { 909 struct pwc_device *pdev = video_drvdata(file); 910 911 return pwc_vidioc_try_fmt(pdev, f); 912 } 913 914 static int pwc_enum_framesizes(struct file *file, void *fh, 915 struct v4l2_frmsizeenum *fsize) 916 { 917 struct pwc_device *pdev = video_drvdata(file); 918 unsigned int i = 0, index = fsize->index; 919 920 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 || 921 (fsize->pixel_format == V4L2_PIX_FMT_PWC1 && 922 DEVICE_USE_CODEC1(pdev->type)) || 923 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && 924 DEVICE_USE_CODEC23(pdev->type))) { 925 for (i = 0; i < PSZ_MAX; i++) { 926 if (!(pdev->image_mask & (1UL << i))) 927 continue; 928 if (!index--) { 929 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 930 fsize->discrete.width = pwc_image_sizes[i][0]; 931 fsize->discrete.height = pwc_image_sizes[i][1]; 932 return 0; 933 } 934 } 935 } 936 return -EINVAL; 937 } 938 939 static int pwc_enum_frameintervals(struct file *file, void *fh, 940 struct v4l2_frmivalenum *fival) 941 { 942 struct pwc_device *pdev = video_drvdata(file); 943 int size = -1; 944 unsigned int i; 945 946 for (i = 0; i < PSZ_MAX; i++) { 947 if (pwc_image_sizes[i][0] == fival->width && 948 pwc_image_sizes[i][1] == fival->height) { 949 size = i; 950 break; 951 } 952 } 953 954 /* TODO: Support raw format */ 955 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420) 956 return -EINVAL; 957 958 i = pwc_get_fps(pdev, fival->index, size); 959 if (!i) 960 return -EINVAL; 961 962 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 963 fival->discrete.numerator = 1; 964 fival->discrete.denominator = i; 965 966 return 0; 967 } 968 969 static int pwc_g_parm(struct file *file, void *fh, 970 struct v4l2_streamparm *parm) 971 { 972 struct pwc_device *pdev = video_drvdata(file); 973 974 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 975 return -EINVAL; 976 977 memset(parm, 0, sizeof(*parm)); 978 979 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 980 parm->parm.capture.readbuffers = MIN_FRAMES; 981 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME; 982 parm->parm.capture.timeperframe.denominator = pdev->vframes; 983 parm->parm.capture.timeperframe.numerator = 1; 984 985 return 0; 986 } 987 988 static int pwc_s_parm(struct file *file, void *fh, 989 struct v4l2_streamparm *parm) 990 { 991 struct pwc_device *pdev = video_drvdata(file); 992 int compression = 0; 993 int ret, fps; 994 995 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 996 return -EINVAL; 997 998 /* If timeperframe == 0, then reset the framerate to the nominal value. 999 We pick a high framerate here, and let pwc_set_video_mode() figure 1000 out the best match. */ 1001 if (parm->parm.capture.timeperframe.numerator == 0 || 1002 parm->parm.capture.timeperframe.denominator == 0) 1003 fps = 30; 1004 else 1005 fps = parm->parm.capture.timeperframe.denominator / 1006 parm->parm.capture.timeperframe.numerator; 1007 1008 if (vb2_is_busy(&pdev->vb_queue)) 1009 return -EBUSY; 1010 1011 ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt, 1012 fps, &compression, 0); 1013 1014 pwc_g_parm(file, fh, parm); 1015 1016 return ret; 1017 } 1018 1019 const struct v4l2_ioctl_ops pwc_ioctl_ops = { 1020 .vidioc_querycap = pwc_querycap, 1021 .vidioc_enum_input = pwc_enum_input, 1022 .vidioc_g_input = pwc_g_input, 1023 .vidioc_s_input = pwc_s_input, 1024 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap, 1025 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap, 1026 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap, 1027 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap, 1028 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1029 .vidioc_querybuf = vb2_ioctl_querybuf, 1030 .vidioc_qbuf = vb2_ioctl_qbuf, 1031 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1032 .vidioc_streamon = vb2_ioctl_streamon, 1033 .vidioc_streamoff = vb2_ioctl_streamoff, 1034 .vidioc_log_status = v4l2_ctrl_log_status, 1035 .vidioc_enum_framesizes = pwc_enum_framesizes, 1036 .vidioc_enum_frameintervals = pwc_enum_frameintervals, 1037 .vidioc_g_parm = pwc_g_parm, 1038 .vidioc_s_parm = pwc_s_parm, 1039 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1040 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1041 }; 1042