Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 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) |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 5 | (C) 2011 Hans de Goede <hdegoede@redhat.com> |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 6 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. |
| 22 | |
| 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 |
| 26 | |
| 27 | */ |
| 28 | |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/poll.h> |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 34 | #include <linux/vmalloc.h> |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 35 | #include <linux/jiffies.h> |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 36 | #include <asm/io.h> |
| 37 | |
| 38 | #include "pwc.h" |
| 39 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 40 | #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl) |
| 41 | |
| 42 | static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl); |
| 43 | static int pwc_s_ctrl(struct v4l2_ctrl *ctrl); |
| 44 | |
| 45 | static const struct v4l2_ctrl_ops pwc_ctrl_ops = { |
| 46 | .g_volatile_ctrl = pwc_g_volatile_ctrl, |
| 47 | .s_ctrl = pwc_s_ctrl, |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 48 | }; |
| 49 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 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 }; |
| 53 | |
| 54 | const char * const pwc_auto_whitebal_qmenu[] = { |
| 55 | "Indoor (Incandescant Lighting) Mode", |
| 56 | "Outdoor (Sunlight) Mode", |
| 57 | "Indoor (Fluorescent Lighting) Mode", |
| 58 | "Manual Mode", |
| 59 | "Auto Mode", |
| 60 | NULL |
| 61 | }; |
| 62 | |
| 63 | static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = { |
| 64 | .ops = &pwc_ctrl_ops, |
| 65 | .id = V4L2_CID_AUTO_WHITE_BALANCE, |
| 66 | .type = V4L2_CTRL_TYPE_MENU, |
| 67 | .max = awb_auto, |
| 68 | .qmenu = pwc_auto_whitebal_qmenu, |
| 69 | }; |
| 70 | |
| 71 | static const struct v4l2_ctrl_config pwc_autocontour_cfg = { |
| 72 | .ops = &pwc_ctrl_ops, |
| 73 | .id = PWC_CID_CUSTOM(autocontour), |
| 74 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 75 | .name = "Auto contour", |
| 76 | .min = 0, |
| 77 | .max = 1, |
| 78 | .step = 1, |
| 79 | }; |
| 80 | |
| 81 | static const struct v4l2_ctrl_config pwc_contour_cfg = { |
| 82 | .ops = &pwc_ctrl_ops, |
| 83 | .id = PWC_CID_CUSTOM(contour), |
| 84 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 85 | .name = "Contour", |
| 86 | .min = 0, |
| 87 | .max = 63, |
| 88 | .step = 1, |
| 89 | }; |
| 90 | |
| 91 | static const struct v4l2_ctrl_config pwc_backlight_cfg = { |
| 92 | .ops = &pwc_ctrl_ops, |
| 93 | .id = V4L2_CID_BACKLIGHT_COMPENSATION, |
| 94 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 95 | .min = 0, |
| 96 | .max = 1, |
| 97 | .step = 1, |
| 98 | }; |
| 99 | |
| 100 | static const struct v4l2_ctrl_config pwc_flicker_cfg = { |
| 101 | .ops = &pwc_ctrl_ops, |
| 102 | .id = V4L2_CID_BAND_STOP_FILTER, |
| 103 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 104 | .min = 0, |
| 105 | .max = 1, |
| 106 | .step = 1, |
| 107 | }; |
| 108 | |
| 109 | static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = { |
| 110 | .ops = &pwc_ctrl_ops, |
| 111 | .id = PWC_CID_CUSTOM(noise_reduction), |
| 112 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 113 | .name = "Dynamic Noise Reduction", |
| 114 | .min = 0, |
| 115 | .max = 3, |
| 116 | .step = 1, |
| 117 | }; |
| 118 | |
| 119 | static const struct v4l2_ctrl_config pwc_save_user_cfg = { |
| 120 | .ops = &pwc_ctrl_ops, |
| 121 | .id = PWC_CID_CUSTOM(save_user), |
| 122 | .type = V4L2_CTRL_TYPE_BUTTON, |
| 123 | .name = "Save User Settings", |
| 124 | }; |
| 125 | |
| 126 | static const struct v4l2_ctrl_config pwc_restore_user_cfg = { |
| 127 | .ops = &pwc_ctrl_ops, |
| 128 | .id = PWC_CID_CUSTOM(restore_user), |
| 129 | .type = V4L2_CTRL_TYPE_BUTTON, |
| 130 | .name = "Restore User Settings", |
| 131 | }; |
| 132 | |
| 133 | static const struct v4l2_ctrl_config pwc_restore_factory_cfg = { |
| 134 | .ops = &pwc_ctrl_ops, |
| 135 | .id = PWC_CID_CUSTOM(restore_factory), |
| 136 | .type = V4L2_CTRL_TYPE_BUTTON, |
| 137 | .name = "Restore Factory Settings", |
| 138 | }; |
| 139 | |
| 140 | int pwc_init_controls(struct pwc_device *pdev) |
| 141 | { |
| 142 | struct v4l2_ctrl_handler *hdl; |
| 143 | struct v4l2_ctrl_config cfg; |
| 144 | int r, def; |
| 145 | |
| 146 | hdl = &pdev->ctrl_handler; |
| 147 | r = v4l2_ctrl_handler_init(hdl, 20); |
| 148 | if (r) |
| 149 | return r; |
| 150 | |
| 151 | /* Brightness, contrast, saturation, gamma */ |
| 152 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def); |
| 153 | if (r || def > 127) |
| 154 | def = 63; |
| 155 | pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 156 | V4L2_CID_BRIGHTNESS, 0, 127, 1, def); |
| 157 | |
| 158 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def); |
| 159 | if (r || def > 63) |
| 160 | def = 31; |
| 161 | pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 162 | V4L2_CID_CONTRAST, 0, 63, 1, def); |
| 163 | |
| 164 | if (pdev->type >= 675) { |
| 165 | if (pdev->type < 730) |
| 166 | pdev->saturation_fmt = SATURATION_MODE_FORMATTER2; |
| 167 | else |
| 168 | pdev->saturation_fmt = SATURATION_MODE_FORMATTER1; |
| 169 | r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt, |
| 170 | &def); |
| 171 | if (r || def < -100 || def > 100) |
| 172 | def = 0; |
| 173 | pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 174 | V4L2_CID_SATURATION, -100, 100, 1, def); |
| 175 | } |
| 176 | |
| 177 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def); |
| 178 | if (r || def > 31) |
| 179 | def = 15; |
| 180 | pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 181 | V4L2_CID_GAMMA, 0, 31, 1, def); |
| 182 | |
| 183 | /* auto white balance, red gain, blue gain */ |
| 184 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def); |
| 185 | if (r || def > awb_auto) |
| 186 | def = awb_auto; |
| 187 | cfg = pwc_auto_white_balance_cfg; |
| 188 | cfg.name = v4l2_ctrl_get_name(cfg.id); |
| 189 | cfg.def = def; |
| 190 | pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 191 | /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */ |
| 192 | if (!pdev->auto_white_balance) |
| 193 | return hdl->error; |
| 194 | |
| 195 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, |
| 196 | PRESET_MANUAL_RED_GAIN_FORMATTER, &def); |
| 197 | if (r) |
| 198 | def = 127; |
| 199 | pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 200 | V4L2_CID_RED_BALANCE, 0, 255, 1, def); |
| 201 | |
| 202 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, |
| 203 | PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def); |
| 204 | if (r) |
| 205 | def = 127; |
| 206 | pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 207 | V4L2_CID_BLUE_BALANCE, 0, 255, 1, def); |
| 208 | |
| 209 | v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, |
| 210 | pdev->auto_white_balance->cur.val == awb_auto); |
| 211 | |
| 212 | /* autogain, gain */ |
| 213 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def); |
| 214 | if (r || (def != 0 && def != 0xff)) |
| 215 | def = 0; |
| 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); |
| 219 | if (!pdev->autogain) |
| 220 | return hdl->error; |
| 221 | |
| 222 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def); |
| 223 | if (r || def > 63) |
| 224 | def = 31; |
| 225 | pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, |
| 226 | V4L2_CID_GAIN, 0, 63, 1, def); |
| 227 | |
| 228 | /* auto exposure, exposure */ |
| 229 | if (DEVICE_USE_CODEC2(pdev->type)) { |
| 230 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER, |
| 231 | &def); |
| 232 | if (r || (def != 0 && def != 0xff)) |
| 233 | def = 0; |
| 234 | /* |
| 235 | * def = 0 auto, def = ff manual |
| 236 | * menu idx 0 = auto, idx 1 = manual |
| 237 | */ |
| 238 | pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl, |
| 239 | &pwc_ctrl_ops, |
| 240 | V4L2_CID_EXPOSURE_AUTO, |
| 241 | 1, 0, def != 0); |
| 242 | if (!pdev->exposure_auto) |
| 243 | return hdl->error; |
| 244 | |
| 245 | /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */ |
| 246 | r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, |
| 247 | READ_SHUTTER_FORMATTER, &def); |
| 248 | if (r || def > 655) |
| 249 | def = 655; |
| 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); |
| 260 | if (r || def > 255) |
| 261 | def = 255; |
| 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, |
| 269 | 0, true); |
| 270 | } |
| 271 | |
| 272 | /* color / bw setting */ |
| 273 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER, |
| 274 | &def); |
| 275 | if (r || (def != 0 && def != 0xff)) |
| 276 | 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); |
| 280 | |
| 281 | /* autocontour, contour */ |
| 282 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def); |
| 283 | if (r || (def != 0 && def != 0xff)) |
| 284 | def = 0; |
| 285 | cfg = pwc_autocontour_cfg; |
| 286 | cfg.def = def == 0; |
| 287 | pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 288 | if (!pdev->autocontour) |
| 289 | return hdl->error; |
| 290 | |
| 291 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def); |
| 292 | if (r || def > 63) |
| 293 | def = 31; |
| 294 | cfg = pwc_contour_cfg; |
| 295 | cfg.def = def; |
| 296 | pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 297 | |
| 298 | v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false); |
| 299 | |
| 300 | /* backlight */ |
| 301 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, |
| 302 | BACK_LIGHT_COMPENSATION_FORMATTER, &def); |
| 303 | if (r || (def != 0 && def != 0xff)) |
| 304 | def = 0; |
| 305 | cfg = pwc_backlight_cfg; |
| 306 | cfg.name = v4l2_ctrl_get_name(cfg.id); |
| 307 | cfg.def = def == 0; |
| 308 | pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 309 | |
| 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)) |
| 314 | def = 0; |
| 315 | cfg = pwc_flicker_cfg; |
| 316 | cfg.name = v4l2_ctrl_get_name(cfg.id); |
| 317 | cfg.def = def == 0; |
| 318 | pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 319 | |
| 320 | /* Dynamic noise reduction */ |
| 321 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, |
| 322 | DYNAMIC_NOISE_CONTROL_FORMATTER, &def); |
| 323 | if (r || def > 3) |
| 324 | def = 2; |
| 325 | cfg = pwc_noise_reduction_cfg; |
| 326 | cfg.def = def; |
| 327 | pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL); |
| 328 | |
| 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, |
| 332 | NULL); |
| 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, |
| 337 | NULL); |
| 338 | if (pdev->restore_factory) |
| 339 | pdev->restore_factory->flags = V4L2_CTRL_FLAG_UPDATE; |
| 340 | |
| 341 | return hdl->error; |
| 342 | } |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 343 | |
| 344 | static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f) |
| 345 | { |
| 346 | memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format)); |
| 347 | f->fmt.pix.width = pdev->view.x; |
| 348 | f->fmt.pix.height = pdev->view.y; |
| 349 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Hans Verkuil | 479567c | 2010-09-12 17:05:11 -0300 | [diff] [blame] | 350 | if (pdev->pixfmt == V4L2_PIX_FMT_YUV420) { |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 351 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; |
| 352 | f->fmt.pix.bytesperline = (f->fmt.pix.width * 3)/2; |
| 353 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 354 | } else { |
| 355 | /* vbandlength contains 4 lines ... */ |
| 356 | f->fmt.pix.bytesperline = pdev->vbandlength/4; |
| 357 | f->fmt.pix.sizeimage = pdev->frame_size + sizeof(struct pwc_raw_frame); |
| 358 | if (DEVICE_USE_CODEC1(pdev->type)) |
| 359 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC1; |
| 360 | else |
| 361 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_PWC2; |
| 362 | } |
| 363 | PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() " |
| 364 | "width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n", |
| 365 | f->fmt.pix.width, |
| 366 | f->fmt.pix.height, |
| 367 | f->fmt.pix.bytesperline, |
| 368 | f->fmt.pix.sizeimage, |
| 369 | (f->fmt.pix.pixelformat)&255, |
| 370 | (f->fmt.pix.pixelformat>>8)&255, |
| 371 | (f->fmt.pix.pixelformat>>16)&255, |
| 372 | (f->fmt.pix.pixelformat>>24)&255); |
| 373 | } |
| 374 | |
| 375 | /* ioctl(VIDIOC_TRY_FMT) */ |
| 376 | static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f) |
| 377 | { |
| 378 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 379 | PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n"); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | switch (f->fmt.pix.pixelformat) { |
| 384 | case V4L2_PIX_FMT_YUV420: |
| 385 | break; |
| 386 | case V4L2_PIX_FMT_PWC1: |
| 387 | if (DEVICE_USE_CODEC23(pdev->type)) { |
| 388 | PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n"); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | break; |
| 392 | case V4L2_PIX_FMT_PWC2: |
| 393 | if (DEVICE_USE_CODEC1(pdev->type)) { |
| 394 | PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n"); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | break; |
| 398 | default: |
| 399 | PWC_DEBUG_IOCTL("Unsupported pixel format\n"); |
| 400 | return -EINVAL; |
| 401 | |
| 402 | } |
| 403 | |
| 404 | if (f->fmt.pix.width > pdev->view_max.x) |
| 405 | f->fmt.pix.width = pdev->view_max.x; |
| 406 | else if (f->fmt.pix.width < pdev->view_min.x) |
| 407 | f->fmt.pix.width = pdev->view_min.x; |
| 408 | |
| 409 | if (f->fmt.pix.height > pdev->view_max.y) |
| 410 | f->fmt.pix.height = pdev->view_max.y; |
| 411 | else if (f->fmt.pix.height < pdev->view_min.y) |
| 412 | f->fmt.pix.height = pdev->view_min.y; |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* ioctl(VIDIOC_SET_FMT) */ |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 418 | |
| 419 | static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 420 | { |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 421 | struct pwc_device *pdev = video_drvdata(file); |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 422 | int ret, fps, snapshot, compression, pixelformat; |
| 423 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 424 | if (!pdev->udev) |
| 425 | return -ENODEV; |
| 426 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 427 | if (pdev->capt_file != NULL && |
| 428 | pdev->capt_file != file) |
| 429 | return -EBUSY; |
| 430 | |
| 431 | pdev->capt_file = file; |
| 432 | |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 433 | ret = pwc_vidioc_try_fmt(pdev, f); |
| 434 | if (ret<0) |
| 435 | return ret; |
| 436 | |
| 437 | pixelformat = f->fmt.pix.pixelformat; |
| 438 | compression = pdev->vcompression; |
| 439 | snapshot = 0; |
| 440 | fps = pdev->vframes; |
| 441 | if (f->fmt.pix.priv) { |
| 442 | compression = (f->fmt.pix.priv & PWC_QLT_MASK) >> PWC_QLT_SHIFT; |
| 443 | snapshot = !!(f->fmt.pix.priv & PWC_FPS_SNAPSHOT); |
| 444 | fps = (f->fmt.pix.priv & PWC_FPS_FRMASK) >> PWC_FPS_SHIFT; |
| 445 | if (fps == 0) |
| 446 | fps = pdev->vframes; |
| 447 | } |
| 448 | |
Hans Verkuil | 479567c | 2010-09-12 17:05:11 -0300 | [diff] [blame] | 449 | if (pixelformat != V4L2_PIX_FMT_YUV420 && |
| 450 | pixelformat != V4L2_PIX_FMT_PWC1 && |
| 451 | pixelformat != V4L2_PIX_FMT_PWC2) |
| 452 | return -EINVAL; |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 453 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 454 | if (vb2_is_streaming(&pdev->vb_queue)) |
Hans de Goede | 3751e28 | 2010-11-16 11:39:25 -0300 | [diff] [blame] | 455 | return -EBUSY; |
| 456 | |
| 457 | PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d " |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 458 | "compression=%d snapshot=%d format=%c%c%c%c\n", |
| 459 | f->fmt.pix.width, f->fmt.pix.height, fps, |
| 460 | compression, snapshot, |
| 461 | (pixelformat)&255, |
| 462 | (pixelformat>>8)&255, |
| 463 | (pixelformat>>16)&255, |
| 464 | (pixelformat>>24)&255); |
| 465 | |
Hans de Goede | 3751e28 | 2010-11-16 11:39:25 -0300 | [diff] [blame] | 466 | ret = pwc_set_video_mode(pdev, |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 467 | f->fmt.pix.width, |
| 468 | f->fmt.pix.height, |
| 469 | fps, |
| 470 | compression, |
| 471 | snapshot); |
| 472 | |
Hans de Goede | 3751e28 | 2010-11-16 11:39:25 -0300 | [diff] [blame] | 473 | PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret); |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 474 | |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | |
Hans Verkuil | 479567c | 2010-09-12 17:05:11 -0300 | [diff] [blame] | 478 | pdev->pixfmt = pixelformat; |
| 479 | |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 480 | pwc_vidioc_fill_fmt(pdev, f); |
| 481 | |
| 482 | return 0; |
| 483 | |
| 484 | } |
| 485 | |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 486 | static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 487 | { |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 488 | struct pwc_device *pdev = video_drvdata(file); |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 489 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 490 | if (!pdev->udev) |
| 491 | return -ENODEV; |
| 492 | |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 493 | strcpy(cap->driver, PWC_NAME); |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 494 | strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card)); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 495 | usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 496 | cap->capabilities = |
| 497 | V4L2_CAP_VIDEO_CAPTURE | |
| 498 | V4L2_CAP_STREAMING | |
| 499 | V4L2_CAP_READWRITE; |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 503 | static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i) |
| 504 | { |
| 505 | if (i->index) /* Only one INPUT is supported */ |
| 506 | return -EINVAL; |
| 507 | |
| 508 | strcpy(i->name, "usb"); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int pwc_g_input(struct file *file, void *fh, unsigned int *i) |
| 513 | { |
| 514 | *i = 0; |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int pwc_s_input(struct file *file, void *fh, unsigned int i) |
| 519 | { |
| 520 | return i ? -EINVAL : 0; |
| 521 | } |
| 522 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 523 | static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 524 | { |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 525 | struct pwc_device *pdev = |
| 526 | container_of(ctrl->handler, struct pwc_device, ctrl_handler); |
| 527 | int ret = 0; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 528 | |
Hans de Goede | c112713 | 2011-07-03 11:50:51 -0300 | [diff] [blame^] | 529 | /* |
| 530 | * Sometimes it can take quite long for the pwc to complete usb control |
| 531 | * transfers, so release the modlock to give streaming by another |
| 532 | * process / thread the chance to continue with a dqbuf. |
| 533 | */ |
| 534 | mutex_unlock(&pdev->modlock); |
| 535 | |
| 536 | /* |
| 537 | * Take the udev-lock to protect against the disconnect handler |
| 538 | * completing and setting dev->udev to NULL underneath us. Other code |
| 539 | * does not need to do this since it is protected by the modlock. |
| 540 | */ |
| 541 | mutex_lock(&pdev->udevlock); |
| 542 | |
| 543 | if (!pdev->udev) { |
| 544 | ret = -ENODEV; |
| 545 | goto leave; |
| 546 | } |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 547 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 548 | switch (ctrl->id) { |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 549 | case V4L2_CID_AUTO_WHITE_BALANCE: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 550 | if (pdev->color_bal_valid && time_before(jiffies, |
| 551 | pdev->last_color_bal_update + HZ / 4)) { |
| 552 | pdev->red_balance->val = pdev->last_red_balance; |
| 553 | pdev->blue_balance->val = pdev->last_blue_balance; |
| 554 | break; |
| 555 | } |
| 556 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 557 | READ_RED_GAIN_FORMATTER, |
| 558 | &pdev->red_balance->val); |
| 559 | if (ret) |
| 560 | break; |
| 561 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 562 | READ_BLUE_GAIN_FORMATTER, |
| 563 | &pdev->blue_balance->val); |
| 564 | if (ret) |
| 565 | break; |
| 566 | pdev->last_red_balance = pdev->red_balance->val; |
| 567 | pdev->last_blue_balance = pdev->blue_balance->val; |
| 568 | pdev->last_color_bal_update = jiffies; |
| 569 | pdev->color_bal_valid = true; |
| 570 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 571 | case V4L2_CID_AUTOGAIN: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 572 | if (pdev->gain_valid && time_before(jiffies, |
| 573 | pdev->last_gain_update + HZ / 4)) { |
| 574 | pdev->gain->val = pdev->last_gain; |
| 575 | break; |
| 576 | } |
| 577 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 578 | READ_AGC_FORMATTER, &pdev->gain->val); |
| 579 | if (ret) |
| 580 | break; |
| 581 | pdev->last_gain = pdev->gain->val; |
| 582 | pdev->last_gain_update = jiffies; |
| 583 | pdev->gain_valid = true; |
| 584 | if (!DEVICE_USE_CODEC3(pdev->type)) |
| 585 | break; |
| 586 | /* Fall through for CODEC3 where autogain also controls expo */ |
| 587 | case V4L2_CID_EXPOSURE_AUTO: |
| 588 | if (pdev->exposure_valid && time_before(jiffies, |
| 589 | pdev->last_exposure_update + HZ / 4)) { |
| 590 | pdev->exposure->val = pdev->last_exposure; |
| 591 | break; |
| 592 | } |
| 593 | ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, |
| 594 | READ_SHUTTER_FORMATTER, |
| 595 | &pdev->exposure->val); |
| 596 | if (ret) |
| 597 | break; |
| 598 | pdev->last_exposure = pdev->exposure->val; |
| 599 | pdev->last_exposure_update = jiffies; |
| 600 | pdev->exposure_valid = true; |
| 601 | break; |
| 602 | default: |
| 603 | ret = -EINVAL; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 604 | } |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 605 | |
| 606 | if (ret) |
| 607 | PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret); |
| 608 | |
Hans de Goede | c112713 | 2011-07-03 11:50:51 -0300 | [diff] [blame^] | 609 | leave: |
| 610 | mutex_unlock(&pdev->udevlock); |
| 611 | mutex_lock(&pdev->modlock); |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 612 | return ret; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 613 | } |
| 614 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 615 | static int pwc_set_awb(struct pwc_device *pdev) |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 616 | { |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 617 | int ret = 0; |
| 618 | |
| 619 | if (pdev->auto_white_balance->is_new) { |
| 620 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
| 621 | WB_MODE_FORMATTER, |
| 622 | pdev->auto_white_balance->val); |
| 623 | if (ret) |
| 624 | return ret; |
| 625 | |
| 626 | /* Update val when coming from auto or going to a preset */ |
| 627 | if (pdev->red_balance->is_volatile || |
| 628 | pdev->auto_white_balance->val == awb_indoor || |
| 629 | pdev->auto_white_balance->val == awb_outdoor || |
| 630 | pdev->auto_white_balance->val == awb_fl) { |
| 631 | if (!pdev->red_balance->is_new) |
| 632 | pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 633 | READ_RED_GAIN_FORMATTER, |
| 634 | &pdev->red_balance->val); |
| 635 | if (!pdev->blue_balance->is_new) |
| 636 | pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 637 | READ_BLUE_GAIN_FORMATTER, |
| 638 | &pdev->blue_balance->val); |
| 639 | } |
| 640 | if (pdev->auto_white_balance->val == awb_auto) { |
| 641 | pdev->red_balance->is_volatile = true; |
| 642 | pdev->blue_balance->is_volatile = true; |
| 643 | pdev->color_bal_valid = false; /* Force cache update */ |
| 644 | } else { |
| 645 | pdev->red_balance->is_volatile = false; |
| 646 | pdev->blue_balance->is_volatile = false; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if (ret == 0 && pdev->red_balance->is_new) { |
| 651 | if (pdev->auto_white_balance->val != awb_manual) |
| 652 | return -EBUSY; |
| 653 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
| 654 | PRESET_MANUAL_RED_GAIN_FORMATTER, |
| 655 | pdev->red_balance->val); |
| 656 | } |
| 657 | |
| 658 | if (ret == 0 && pdev->blue_balance->is_new) { |
| 659 | if (pdev->auto_white_balance->val != awb_manual) |
| 660 | return -EBUSY; |
| 661 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
| 662 | PRESET_MANUAL_BLUE_GAIN_FORMATTER, |
| 663 | pdev->blue_balance->val); |
| 664 | } |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | /* For CODEC2 models which have separate autogain and auto exposure */ |
| 669 | static int pwc_set_autogain(struct pwc_device *pdev) |
| 670 | { |
| 671 | int ret = 0; |
| 672 | |
| 673 | if (pdev->autogain->is_new) { |
| 674 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 675 | AGC_MODE_FORMATTER, |
| 676 | pdev->autogain->val ? 0 : 0xff); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | if (pdev->autogain->val) |
| 680 | pdev->gain_valid = false; /* Force cache update */ |
| 681 | else if (!pdev->gain->is_new) |
| 682 | pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 683 | READ_AGC_FORMATTER, |
| 684 | &pdev->gain->val); |
| 685 | } |
| 686 | if (ret == 0 && pdev->gain->is_new) { |
| 687 | if (pdev->autogain->val) |
| 688 | return -EBUSY; |
| 689 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 690 | PRESET_AGC_FORMATTER, |
| 691 | pdev->gain->val); |
| 692 | } |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | /* For CODEC2 models which have separate autogain and auto exposure */ |
| 697 | static int pwc_set_exposure_auto(struct pwc_device *pdev) |
| 698 | { |
| 699 | int ret = 0; |
| 700 | int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO; |
| 701 | |
| 702 | if (pdev->exposure_auto->is_new) { |
| 703 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 704 | SHUTTER_MODE_FORMATTER, |
| 705 | is_auto ? 0 : 0xff); |
| 706 | if (ret) |
| 707 | return ret; |
| 708 | if (is_auto) |
| 709 | pdev->exposure_valid = false; /* Force cache update */ |
| 710 | else if (!pdev->exposure->is_new) |
| 711 | pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, |
| 712 | READ_SHUTTER_FORMATTER, |
| 713 | &pdev->exposure->val); |
| 714 | } |
| 715 | if (ret == 0 && pdev->exposure->is_new) { |
| 716 | if (is_auto) |
| 717 | return -EBUSY; |
| 718 | ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, |
| 719 | PRESET_SHUTTER_FORMATTER, |
| 720 | pdev->exposure->val); |
| 721 | } |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | /* For CODEC3 models which have autogain controlling both gain and exposure */ |
| 726 | static int pwc_set_autogain_expo(struct pwc_device *pdev) |
| 727 | { |
| 728 | int ret = 0; |
| 729 | |
| 730 | if (pdev->autogain->is_new) { |
| 731 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 732 | AGC_MODE_FORMATTER, |
| 733 | pdev->autogain->val ? 0 : 0xff); |
| 734 | if (ret) |
| 735 | return ret; |
| 736 | if (pdev->autogain->val) { |
| 737 | pdev->gain_valid = false; /* Force cache update */ |
| 738 | pdev->exposure_valid = false; /* Force cache update */ |
| 739 | } else { |
| 740 | if (!pdev->gain->is_new) |
| 741 | pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
| 742 | READ_AGC_FORMATTER, |
| 743 | &pdev->gain->val); |
| 744 | if (!pdev->exposure->is_new) |
| 745 | pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, |
| 746 | READ_SHUTTER_FORMATTER, |
| 747 | &pdev->exposure->val); |
| 748 | } |
| 749 | } |
| 750 | if (ret == 0 && pdev->gain->is_new) { |
| 751 | if (pdev->autogain->val) |
| 752 | return -EBUSY; |
| 753 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 754 | PRESET_AGC_FORMATTER, |
| 755 | pdev->gain->val); |
| 756 | } |
| 757 | if (ret == 0 && pdev->exposure->is_new) { |
| 758 | if (pdev->autogain->val) |
| 759 | return -EBUSY; |
| 760 | ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, |
| 761 | PRESET_SHUTTER_FORMATTER, |
| 762 | pdev->exposure->val); |
| 763 | } |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static int pwc_s_ctrl(struct v4l2_ctrl *ctrl) |
| 768 | { |
| 769 | struct pwc_device *pdev = |
| 770 | container_of(ctrl->handler, struct pwc_device, ctrl_handler); |
| 771 | int ret = 0; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 772 | |
Hans de Goede | c112713 | 2011-07-03 11:50:51 -0300 | [diff] [blame^] | 773 | /* See the comments on locking in pwc_g_volatile_ctrl */ |
| 774 | mutex_unlock(&pdev->modlock); |
| 775 | mutex_lock(&pdev->udevlock); |
| 776 | |
| 777 | if (!pdev->udev) { |
| 778 | ret = -ENODEV; |
| 779 | goto leave; |
| 780 | } |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 781 | |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 782 | switch (ctrl->id) { |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 783 | case V4L2_CID_BRIGHTNESS: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 784 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 785 | BRIGHTNESS_FORMATTER, ctrl->val); |
| 786 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 787 | case V4L2_CID_CONTRAST: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 788 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 789 | CONTRAST_FORMATTER, ctrl->val); |
| 790 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 791 | case V4L2_CID_SATURATION: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 792 | ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL, |
| 793 | pdev->saturation_fmt, ctrl->val); |
| 794 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 795 | case V4L2_CID_GAMMA: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 796 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 797 | GAMMA_FORMATTER, ctrl->val); |
| 798 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 799 | case V4L2_CID_AUTO_WHITE_BALANCE: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 800 | ret = pwc_set_awb(pdev); |
| 801 | break; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 802 | case V4L2_CID_AUTOGAIN: |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 803 | if (DEVICE_USE_CODEC2(pdev->type)) |
| 804 | ret = pwc_set_autogain(pdev); |
| 805 | else if (DEVICE_USE_CODEC3(pdev->type)) |
| 806 | ret = pwc_set_autogain_expo(pdev); |
| 807 | else |
| 808 | ret = -EINVAL; |
| 809 | break; |
| 810 | case V4L2_CID_EXPOSURE_AUTO: |
| 811 | if (DEVICE_USE_CODEC2(pdev->type)) |
| 812 | ret = pwc_set_exposure_auto(pdev); |
| 813 | else |
| 814 | ret = -EINVAL; |
| 815 | break; |
| 816 | case V4L2_CID_COLORFX: |
| 817 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
| 818 | COLOUR_MODE_FORMATTER, |
| 819 | ctrl->val ? 0 : 0xff); |
| 820 | break; |
| 821 | case PWC_CID_CUSTOM(autocontour): |
| 822 | if (pdev->autocontour->is_new) { |
| 823 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 824 | AUTO_CONTOUR_FORMATTER, |
| 825 | pdev->autocontour->val ? 0 : 0xff); |
| 826 | } |
| 827 | if (ret == 0 && pdev->contour->is_new) { |
| 828 | if (pdev->autocontour->val) { |
| 829 | ret = -EBUSY; |
| 830 | break; |
| 831 | } |
| 832 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 833 | PRESET_CONTOUR_FORMATTER, |
| 834 | pdev->contour->val); |
| 835 | } |
| 836 | break; |
| 837 | case V4L2_CID_BACKLIGHT_COMPENSATION: |
| 838 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 839 | BACK_LIGHT_COMPENSATION_FORMATTER, |
| 840 | ctrl->val ? 0 : 0xff); |
| 841 | break; |
| 842 | case V4L2_CID_BAND_STOP_FILTER: |
| 843 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 844 | FLICKERLESS_MODE_FORMATTER, |
| 845 | ctrl->val ? 0 : 0xff); |
| 846 | break; |
| 847 | case PWC_CID_CUSTOM(noise_reduction): |
| 848 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
| 849 | DYNAMIC_NOISE_CONTROL_FORMATTER, |
| 850 | ctrl->val); |
| 851 | break; |
| 852 | case PWC_CID_CUSTOM(save_user): |
| 853 | ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER); |
| 854 | break; |
| 855 | case PWC_CID_CUSTOM(restore_user): |
| 856 | ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER); |
| 857 | break; |
| 858 | case PWC_CID_CUSTOM(restore_factory): |
| 859 | ret = pwc_button_ctrl(pdev, |
| 860 | RESTORE_FACTORY_DEFAULTS_FORMATTER); |
| 861 | break; |
| 862 | default: |
| 863 | ret = -EINVAL; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 864 | } |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 865 | |
| 866 | if (ret) |
| 867 | PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret); |
| 868 | |
Hans de Goede | c112713 | 2011-07-03 11:50:51 -0300 | [diff] [blame^] | 869 | leave: |
| 870 | mutex_unlock(&pdev->udevlock); |
| 871 | mutex_lock(&pdev->modlock); |
Hans de Goede | 6c9cac8 | 2011-06-26 12:52:01 -0300 | [diff] [blame] | 872 | return ret; |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f) |
| 876 | { |
| 877 | struct pwc_device *pdev = video_drvdata(file); |
| 878 | |
| 879 | /* We only support two format: the raw format, and YUV */ |
| 880 | switch (f->index) { |
| 881 | case 0: |
| 882 | /* RAW format */ |
| 883 | f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2; |
| 884 | f->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 885 | strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description)); |
| 886 | break; |
| 887 | case 1: |
| 888 | f->pixelformat = V4L2_PIX_FMT_YUV420; |
| 889 | strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description)); |
| 890 | break; |
| 891 | default: |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) |
| 898 | { |
| 899 | struct pwc_device *pdev = video_drvdata(file); |
| 900 | |
| 901 | PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n", |
| 902 | pdev->image.x, pdev->image.y); |
| 903 | pwc_vidioc_fill_fmt(pdev, f); |
| 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 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 914 | static int pwc_reqbufs(struct file *file, void *fh, |
| 915 | struct v4l2_requestbuffers *rb) |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 916 | { |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 917 | struct pwc_device *pdev = video_drvdata(file); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 918 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 919 | if (pdev->capt_file != NULL && |
| 920 | pdev->capt_file != file) |
| 921 | return -EBUSY; |
| 922 | |
| 923 | pdev->capt_file = file; |
| 924 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 925 | return vb2_reqbufs(&pdev->vb_queue, rb); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf) |
| 929 | { |
| 930 | struct pwc_device *pdev = video_drvdata(file); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 931 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 932 | return vb2_querybuf(&pdev->vb_queue, buf); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf) |
| 936 | { |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 937 | struct pwc_device *pdev = video_drvdata(file); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 938 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 939 | if (!pdev->udev) |
| 940 | return -ENODEV; |
| 941 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 942 | if (pdev->capt_file != file) |
| 943 | return -EBUSY; |
| 944 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 945 | return vb2_qbuf(&pdev->vb_queue, buf); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf) |
| 949 | { |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 950 | struct pwc_device *pdev = video_drvdata(file); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 951 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 952 | if (!pdev->udev) |
| 953 | return -ENODEV; |
| 954 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 955 | if (pdev->capt_file != file) |
| 956 | return -EBUSY; |
| 957 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 958 | return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i) |
| 962 | { |
| 963 | struct pwc_device *pdev = video_drvdata(file); |
| 964 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 965 | if (!pdev->udev) |
| 966 | return -ENODEV; |
| 967 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 968 | if (pdev->capt_file != file) |
| 969 | return -EBUSY; |
| 970 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 971 | return vb2_streamon(&pdev->vb_queue, i); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i) |
| 975 | { |
| 976 | struct pwc_device *pdev = video_drvdata(file); |
| 977 | |
Hans de Goede | b824bb4 | 2011-06-25 17:39:19 -0300 | [diff] [blame] | 978 | if (!pdev->udev) |
| 979 | return -ENODEV; |
| 980 | |
Hans de Goede | 4fba471 | 2011-06-26 12:13:44 -0300 | [diff] [blame] | 981 | if (pdev->capt_file != file) |
| 982 | return -EBUSY; |
| 983 | |
Hans de Goede | 885fe18 | 2011-06-06 15:33:44 -0300 | [diff] [blame] | 984 | return vb2_streamoff(&pdev->vb_queue, i); |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static int pwc_enum_framesizes(struct file *file, void *fh, |
| 988 | struct v4l2_frmsizeenum *fsize) |
| 989 | { |
| 990 | struct pwc_device *pdev = video_drvdata(file); |
| 991 | unsigned int i = 0, index = fsize->index; |
| 992 | |
| 993 | if (fsize->pixel_format == V4L2_PIX_FMT_YUV420) { |
| 994 | for (i = 0; i < PSZ_MAX; i++) { |
| 995 | if (pdev->image_mask & (1UL << i)) { |
| 996 | if (!index--) { |
| 997 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 998 | fsize->discrete.width = pwc_image_sizes[i].x; |
| 999 | fsize->discrete.height = pwc_image_sizes[i].y; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } else if (fsize->index == 0 && |
| 1005 | ((fsize->pixel_format == V4L2_PIX_FMT_PWC1 && DEVICE_USE_CODEC1(pdev->type)) || |
| 1006 | (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && DEVICE_USE_CODEC23(pdev->type)))) { |
| 1007 | |
| 1008 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1009 | fsize->discrete.width = pdev->abs_max.x; |
| 1010 | fsize->discrete.height = pdev->abs_max.y; |
| 1011 | return 0; |
| 1012 | } |
| 1013 | return -EINVAL; |
| 1014 | } |
| 1015 | |
| 1016 | static int pwc_enum_frameintervals(struct file *file, void *fh, |
| 1017 | struct v4l2_frmivalenum *fival) |
| 1018 | { |
| 1019 | struct pwc_device *pdev = video_drvdata(file); |
| 1020 | int size = -1; |
| 1021 | unsigned int i; |
| 1022 | |
| 1023 | for (i = 0; i < PSZ_MAX; i++) { |
| 1024 | if (pwc_image_sizes[i].x == fival->width && |
| 1025 | pwc_image_sizes[i].y == fival->height) { |
| 1026 | size = i; |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* TODO: Support raw format */ |
| 1032 | if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420) |
| 1033 | return -EINVAL; |
| 1034 | |
| 1035 | i = pwc_get_fps(pdev, fival->index, size); |
| 1036 | if (!i) |
| 1037 | return -EINVAL; |
| 1038 | |
| 1039 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 1040 | fival->discrete.numerator = 1; |
| 1041 | fival->discrete.denominator = i; |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Hans Verkuil | 99cd47bc | 2011-03-11 19:00:56 -0300 | [diff] [blame] | 1046 | static long pwc_default(struct file *file, void *fh, bool valid_prio, |
| 1047 | int cmd, void *arg) |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 1048 | { |
| 1049 | struct pwc_device *pdev = video_drvdata(file); |
| 1050 | |
| 1051 | return pwc_ioctl(pdev, cmd, arg); |
| 1052 | } |
| 1053 | |
| 1054 | const struct v4l2_ioctl_ops pwc_ioctl_ops = { |
| 1055 | .vidioc_querycap = pwc_querycap, |
| 1056 | .vidioc_enum_input = pwc_enum_input, |
| 1057 | .vidioc_g_input = pwc_g_input, |
| 1058 | .vidioc_s_input = pwc_s_input, |
| 1059 | .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap, |
| 1060 | .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap, |
| 1061 | .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap, |
| 1062 | .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap, |
Hans Verkuil | afa3852 | 2011-01-22 06:34:55 -0300 | [diff] [blame] | 1063 | .vidioc_reqbufs = pwc_reqbufs, |
| 1064 | .vidioc_querybuf = pwc_querybuf, |
| 1065 | .vidioc_qbuf = pwc_qbuf, |
| 1066 | .vidioc_dqbuf = pwc_dqbuf, |
| 1067 | .vidioc_streamon = pwc_streamon, |
| 1068 | .vidioc_streamoff = pwc_streamoff, |
| 1069 | .vidioc_enum_framesizes = pwc_enum_framesizes, |
| 1070 | .vidioc_enum_frameintervals = pwc_enum_frameintervals, |
| 1071 | .vidioc_default = pwc_default, |
| 1072 | }; |
| 1073 | |
| 1074 | |
Luc Saillard | 2b455db | 2006-04-24 10:29:46 -0300 | [diff] [blame] | 1075 | /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */ |