[media] pwc: Replace control code with v4l2-ctrls framework
authorHans de Goede <hdegoede@redhat.com>
Sun, 26 Jun 2011 15:52:01 +0000 (12:52 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Wed, 27 Jul 2011 20:53:45 +0000 (17:53 -0300)
Also remove all the converting from native range to 0-65535 and back
that was going on. This is no longer needed now that we no longer support
v4l1.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
drivers/media/video/pwc/pwc-ctrl.c
drivers/media/video/pwc/pwc-if.c
drivers/media/video/pwc/pwc-v4l.c
drivers/media/video/pwc/pwc.h

index 9d800c66807392083c498f1da911b367e9e55c82..8e0cc537e1e463605caaec42a2e277869446a5c8 100644 (file)
@@ -3,6 +3,7 @@
    video modes.
    (C) 1999-2003 Nemosoft Unv.
    (C) 2004-2006 Luc Saillard (luc@saillard.org)
+   (C) 2011 Hans de Goede <hdegoede@redhat.com>
 
    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
    driver and thus may have bugs that are not present in the original version.
 #include "pwc-dec1.h"
 #include "pwc-dec23.h"
 
-/* Request types: video */
-#define SET_LUM_CTL                    0x01
-#define GET_LUM_CTL                    0x02
-#define SET_CHROM_CTL                  0x03
-#define GET_CHROM_CTL                  0x04
-#define SET_STATUS_CTL                 0x05
-#define GET_STATUS_CTL                 0x06
-#define SET_EP_STREAM_CTL              0x07
-#define GET_EP_STREAM_CTL              0x08
-#define GET_XX_CTL                     0x09
-#define SET_XX_CTL                     0x0A
-#define GET_XY_CTL                     0x0B
-#define SET_XY_CTL                     0x0C
-#define SET_MPT_CTL                    0x0D
-#define GET_MPT_CTL                    0x0E
-
-/* Selectors for the Luminance controls [GS]ET_LUM_CTL */
-#define AGC_MODE_FORMATTER                     0x2000
-#define PRESET_AGC_FORMATTER                   0x2100
-#define SHUTTER_MODE_FORMATTER                 0x2200
-#define PRESET_SHUTTER_FORMATTER               0x2300
-#define PRESET_CONTOUR_FORMATTER               0x2400
-#define AUTO_CONTOUR_FORMATTER                 0x2500
-#define BACK_LIGHT_COMPENSATION_FORMATTER      0x2600
-#define CONTRAST_FORMATTER                     0x2700
-#define DYNAMIC_NOISE_CONTROL_FORMATTER                0x2800
-#define FLICKERLESS_MODE_FORMATTER             0x2900
-#define AE_CONTROL_SPEED                       0x2A00
-#define BRIGHTNESS_FORMATTER                   0x2B00
-#define GAMMA_FORMATTER                                0x2C00
-
-/* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
-#define WB_MODE_FORMATTER                      0x1000
-#define AWB_CONTROL_SPEED_FORMATTER            0x1100
-#define AWB_CONTROL_DELAY_FORMATTER            0x1200
-#define PRESET_MANUAL_RED_GAIN_FORMATTER       0x1300
-#define PRESET_MANUAL_BLUE_GAIN_FORMATTER      0x1400
-#define COLOUR_MODE_FORMATTER                  0x1500
-#define SATURATION_MODE_FORMATTER1             0x1600
-#define SATURATION_MODE_FORMATTER2             0x1700
-
-/* Selectors for the Status controls [GS]ET_STATUS_CTL */
-#define SAVE_USER_DEFAULTS_FORMATTER           0x0200
-#define RESTORE_USER_DEFAULTS_FORMATTER                0x0300
-#define RESTORE_FACTORY_DEFAULTS_FORMATTER     0x0400
-#define READ_AGC_FORMATTER                     0x0500
-#define READ_SHUTTER_FORMATTER                 0x0600
-#define READ_RED_GAIN_FORMATTER                        0x0700
-#define READ_BLUE_GAIN_FORMATTER               0x0800
+/* Selectors for status controls used only in this file */
 #define GET_STATUS_B00                         0x0B00
 #define SENSOR_TYPE_FORMATTER1                 0x0C00
 #define GET_STATUS_3000                                0x3000
 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
 #define VIDEO_OUTPUT_CONTROL_FORMATTER         0x0100
 
-/* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
-#define PT_RELATIVE_CONTROL_FORMATTER          0x01
-#define PT_RESET_CONTROL_FORMATTER             0x02
-#define PT_STATUS_FORMATTER                    0x03
-
 static const char *size2name[PSZ_MAX] =
 {
        "subQCIF",
@@ -160,7 +108,7 @@ static void pwc_set_image_buffer_size(struct pwc_device *pdev);
 /****************************************************************************/
 
 static int _send_control_msg(struct pwc_device *pdev,
-       u8 request, u16 value, int index, void *buf, int buflen, int timeout)
+       u8 request, u16 value, int index, void *buf, int buflen)
 {
        int rc;
        void *kbuf = NULL;
@@ -177,7 +125,7 @@ static int _send_control_msg(struct pwc_device *pdev,
                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
                value,
                index,
-               kbuf, buflen, timeout);
+               kbuf, buflen, USB_CTRL_SET_TIMEOUT);
 
        kfree(kbuf);
        return rc;
@@ -197,9 +145,13 @@ static int recv_control_msg(struct pwc_device *pdev,
                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
                value,
                pdev->vcinterface,
-               kbuf, buflen, 500);
+               kbuf, buflen, USB_CTRL_GET_TIMEOUT);
        memcpy(buf, kbuf, buflen);
        kfree(kbuf);
+
+       if (rc < 0)
+               PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
+                         rc, request, value);
        return rc;
 }
 
@@ -210,18 +162,16 @@ static inline int send_video_command(struct pwc_device *pdev,
                SET_EP_STREAM_CTL,
                VIDEO_OUTPUT_CONTROL_FORMATTER,
                index,
-               buf, buflen, 1000);
+               buf, buflen);
 }
 
 static inline int send_control_msg(struct pwc_device *pdev,
        u8 request, u16 value, void *buf, int buflen)
 {
        return _send_control_msg(pdev,
-               request, value, pdev->vcinterface, buf, buflen, 500);
+               request, value, pdev->vcinterface, buf, buflen);
 }
 
-
-
 static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
 {
        unsigned char buf[3];
@@ -549,246 +499,78 @@ static void pwc_set_image_buffer_size(struct pwc_device *pdev)
        pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
 }
 
-/* BRIGHTNESS */
-int pwc_get_brightness(struct pwc_device *pdev)
+int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
 {
-       char buf;
        int ret;
+       u8 buf;
 
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, BRIGHTNESS_FORMATTER, &buf, sizeof(buf));
+       ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
        if (ret < 0)
                return ret;
-       return buf;
-}
-
-int pwc_set_brightness(struct pwc_device *pdev, int value)
-{
-       char buf;
 
-       if (value < 0)
-               value = 0;
-       if (value > 0xffff)
-               value = 0xffff;
-       buf = (value >> 9) & 0x7f;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, BRIGHTNESS_FORMATTER, &buf, sizeof(buf));
+       *data = buf;
+       return 0;
 }
 
-/* CONTRAST */
-
-int pwc_get_contrast(struct pwc_device *pdev)
+int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
 {
-       char buf;
        int ret;
 
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, CONTRAST_FORMATTER, &buf, sizeof(buf));
+       ret = send_control_msg(pdev, request, value, &data, sizeof(data));
        if (ret < 0)
                return ret;
-       return buf;
-}
 
-int pwc_set_contrast(struct pwc_device *pdev, int value)
-{
-       char buf;
-
-       if (value < 0)
-               value = 0;
-       if (value > 0xffff)
-               value = 0xffff;
-       buf = (value >> 10) & 0x3f;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, CONTRAST_FORMATTER, &buf, sizeof(buf));
+       return 0;
 }
 
-/* GAMMA */
-
-int pwc_get_gamma(struct pwc_device *pdev)
+int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
 {
-       char buf;
        int ret;
+       s8 buf;
 
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, GAMMA_FORMATTER, &buf, sizeof(buf));
+       ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
        if (ret < 0)
                return ret;
-       return buf;
-}
-
-int pwc_set_gamma(struct pwc_device *pdev, int value)
-{
-       char buf;
-
-       if (value < 0)
-               value = 0;
-       if (value > 0xffff)
-               value = 0xffff;
-       buf = (value >> 11) & 0x1f;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, GAMMA_FORMATTER, &buf, sizeof(buf));
-}
-
-
-/* SATURATION */
-
-/* return a value between [-100 , 100] */
-int pwc_get_saturation(struct pwc_device *pdev, int *value)
-{
-       char buf;
-       int ret, saturation_register;
 
-       if (pdev->type < 675)
-               return -EINVAL;
-       if (pdev->type < 730)
-               saturation_register = SATURATION_MODE_FORMATTER2;
-       else
-               saturation_register = SATURATION_MODE_FORMATTER1;
-       ret = recv_control_msg(pdev,
-               GET_CHROM_CTL, saturation_register, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *value = (signed)buf;
+       *data = buf;
        return 0;
 }
 
-/* @param value saturation color between [-100 , 100] */
-int pwc_set_saturation(struct pwc_device *pdev, int value)
-{
-       char buf;
-       int saturation_register;
-
-       if (pdev->type < 675)
-               return -EINVAL;
-       if (value < -100)
-               value = -100;
-       if (value > 100)
-               value = 100;
-       if (pdev->type < 730)
-               saturation_register = SATURATION_MODE_FORMATTER2;
-       else
-               saturation_register = SATURATION_MODE_FORMATTER1;
-       return send_control_msg(pdev,
-               SET_CHROM_CTL, saturation_register, &buf, sizeof(buf));
-}
-
-/* AGC */
-
-int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
+int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
 {
-       char buf;
        int ret;
+       u8 buf[2];
 
-       if (mode)
-               buf = 0x0; /* auto */
-       else
-               buf = 0xff; /* fixed */
-
-       ret = send_control_msg(pdev,
-               SET_LUM_CTL, AGC_MODE_FORMATTER, &buf, sizeof(buf));
-
-       if (!mode && ret >= 0) {
-               if (value < 0)
-                       value = 0;
-               if (value > 0xffff)
-                       value = 0xffff;
-               buf = (value >> 10) & 0x3F;
-               ret = send_control_msg(pdev,
-                       SET_LUM_CTL, PRESET_AGC_FORMATTER, &buf, sizeof(buf));
-       }
+       ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
        if (ret < 0)
                return ret;
+
+       *data = (buf[1] << 8) | buf[0];
        return 0;
 }
 
-int pwc_get_agc(struct pwc_device *pdev, int *value)
+int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
 {
-       unsigned char buf;
        int ret;
+       u8 buf[2];
 
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, AGC_MODE_FORMATTER, &buf, sizeof(buf));
+       buf[0] = data & 0xff;
+       buf[1] = data >> 8;
+       ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
        if (ret < 0)
                return ret;
 
-       if (buf != 0) { /* fixed */
-               ret = recv_control_msg(pdev,
-                       GET_LUM_CTL, PRESET_AGC_FORMATTER, &buf, sizeof(buf));
-               if (ret < 0)
-                       return ret;
-               if (buf > 0x3F)
-                       buf = 0x3F;
-               *value = (buf << 10);
-       }
-       else { /* auto */
-               ret = recv_control_msg(pdev,
-                       GET_STATUS_CTL, READ_AGC_FORMATTER, &buf, sizeof(buf));
-               if (ret < 0)
-                       return ret;
-               /* Gah... this value ranges from 0x00 ... 0x9F */
-               if (buf > 0x9F)
-                       buf = 0x9F;
-               *value = -(48 + buf * 409);
-       }
-
        return 0;
 }
 
-int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
-{
-       char buf[2];
-       int speed, ret;
-
-
-       if (mode)
-               buf[0] = 0x0;   /* auto */
-       else
-               buf[0] = 0xff; /* fixed */
-
-       ret = send_control_msg(pdev,
-               SET_LUM_CTL, SHUTTER_MODE_FORMATTER, &buf, 1);
-
-       if (!mode && ret >= 0) {
-               if (value < 0)
-                       value = 0;
-               if (value > 0xffff)
-                       value = 0xffff;
-
-               if (DEVICE_USE_CODEC2(pdev->type)) {
-                       /* speed ranges from 0x0 to 0x290 (656) */
-                       speed = (value / 100);
-                       buf[1] = speed >> 8;
-                       buf[0] = speed & 0xff;
-               } else if (DEVICE_USE_CODEC3(pdev->type)) {
-                       /* speed seems to range from 0x0 to 0xff */
-                       buf[1] = 0;
-                       buf[0] = value >> 8;
-               }
-
-               ret = send_control_msg(pdev,
-                       SET_LUM_CTL, PRESET_SHUTTER_FORMATTER,
-                       &buf, sizeof(buf));
-       }
-       return ret;
-}
-
-/* This function is not exported to v4l1, so output values between 0 -> 256 */
-int pwc_get_shutter_speed(struct pwc_device *pdev, int *value)
+int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
 {
-       unsigned char buf[2];
        int ret;
 
-       ret = recv_control_msg(pdev,
-               GET_STATUS_CTL, READ_SHUTTER_FORMATTER, &buf, sizeof(buf));
+       ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
        if (ret < 0)
                return ret;
-       *value = buf[0] + (buf[1] << 8);
-       if (DEVICE_USE_CODEC2(pdev->type)) {
-               /* speed ranges from 0x0 to 0x290 (656) */
-               *value *= 256/656;
-       } else if (DEVICE_USE_CODEC3(pdev->type)) {
-               /* speed seems to range from 0x0 to 0xff */
-       }
+
        return 0;
 }
 
@@ -817,162 +599,6 @@ void pwc_camera_power(struct pwc_device *pdev, int power)
                          power ? "on" : "off", r);
 }
 
-/* private calls */
-int pwc_restore_user(struct pwc_device *pdev)
-{
-       return send_control_msg(pdev,
-               SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, NULL, 0);
-}
-
-int pwc_save_user(struct pwc_device *pdev)
-{
-       return send_control_msg(pdev,
-               SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, NULL, 0);
-}
-
-int pwc_restore_factory(struct pwc_device *pdev)
-{
-       return send_control_msg(pdev,
-               SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, NULL, 0);
-}
-
- /* ************************************************* */
- /* Patch by Alvarado: (not in the original version   */
-
- /*
-  * the camera recognizes modes from 0 to 4:
-  *
-  * 00: indoor (incandescant lighting)
-  * 01: outdoor (sunlight)
-  * 02: fluorescent lighting
-  * 03: manual
-  * 04: auto
-  */
-int pwc_set_awb(struct pwc_device *pdev, int mode)
-{
-       char buf;
-       int ret;
-
-       if (mode < 0)
-           mode = 0;
-
-       if (mode > 4)
-           mode = 4;
-
-       buf = mode & 0x07; /* just the lowest three bits */
-
-       ret = send_control_msg(pdev,
-               SET_CHROM_CTL, WB_MODE_FORMATTER, &buf, sizeof(buf));
-
-       if (ret < 0)
-               return ret;
-       return 0;
-}
-
-int pwc_get_awb(struct pwc_device *pdev)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_CHROM_CTL, WB_MODE_FORMATTER, &buf, sizeof(buf));
-
-       if (ret < 0)
-               return ret;
-       return buf;
-}
-
-int pwc_set_red_gain(struct pwc_device *pdev, int value)
-{
-       unsigned char buf;
-
-       if (value < 0)
-               value = 0;
-       if (value > 0xffff)
-               value = 0xffff;
-       /* only the msb is considered */
-       buf = value >> 8;
-       return send_control_msg(pdev,
-               SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER,
-               &buf, sizeof(buf));
-}
-
-int pwc_get_red_gain(struct pwc_device *pdev, int *value)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER,
-               &buf, sizeof(buf));
-       if (ret < 0)
-           return ret;
-       *value = buf << 8;
-       return 0;
-}
-
-
-int pwc_set_blue_gain(struct pwc_device *pdev, int value)
-{
-       unsigned char buf;
-
-       if (value < 0)
-               value = 0;
-       if (value > 0xffff)
-               value = 0xffff;
-       /* only the msb is considered */
-       buf = value >> 8;
-       return send_control_msg(pdev,
-               SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER,
-               &buf, sizeof(buf));
-}
-
-int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER,
-               &buf, sizeof(buf));
-       if (ret < 0)
-           return ret;
-       *value = buf << 8;
-       return 0;
-}
-
-
-/* The following two functions are different, since they only read the
-   internal red/blue gains, which may be different from the manual
-   gains set or read above.
- */
-static int pwc_read_red_gain(struct pwc_device *pdev, int *value)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *value = buf << 8;
-       return 0;
-}
-
-static int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *value = buf << 8;
-       return 0;
-}
-
-
 static int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
 {
        unsigned char buf;
@@ -1070,164 +696,6 @@ static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
        return 0;
 }
 
-int pwc_set_contour(struct pwc_device *pdev, int contour)
-{
-       unsigned char buf;
-       int ret;
-
-       if (contour < 0)
-               buf = 0xff; /* auto contour on */
-       else
-               buf = 0x0; /* auto contour off */
-       ret = send_control_msg(pdev,
-               SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-
-       if (contour < 0)
-               return 0;
-       if (contour > 0xffff)
-               contour = 0xffff;
-
-       buf = (contour >> 10); /* contour preset is [0..3f] */
-       ret = send_control_msg(pdev,
-               SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       return 0;
-}
-
-int pwc_get_contour(struct pwc_device *pdev, int *contour)
-{
-       unsigned char buf;
-       int ret;
-
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-
-       if (buf == 0) {
-               /* auto mode off, query current preset value */
-               ret = recv_control_msg(pdev,
-                       GET_LUM_CTL, PRESET_CONTOUR_FORMATTER,
-                       &buf, sizeof(buf));
-               if (ret < 0)
-                       return ret;
-               *contour = buf << 10;
-       }
-       else
-               *contour = -1;
-       return 0;
-}
-
-
-int pwc_set_backlight(struct pwc_device *pdev, int backlight)
-{
-       unsigned char buf;
-
-       if (backlight)
-               buf = 0xff;
-       else
-               buf = 0x0;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER,
-               &buf, sizeof(buf));
-}
-
-int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
-{
-       int ret;
-       unsigned char buf;
-
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER,
-               &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *backlight = !!buf;
-       return 0;
-}
-
-int pwc_set_colour_mode(struct pwc_device *pdev, int colour)
-{
-       unsigned char buf;
-
-       if (colour)
-               buf = 0xff;
-       else
-               buf = 0x0;
-       return send_control_msg(pdev,
-               SET_CHROM_CTL, COLOUR_MODE_FORMATTER, &buf, sizeof(buf));
-}
-
-int pwc_get_colour_mode(struct pwc_device *pdev, int *colour)
-{
-       int ret;
-       unsigned char buf;
-
-       ret = recv_control_msg(pdev,
-               GET_CHROM_CTL, COLOUR_MODE_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *colour = !!buf;
-       return 0;
-}
-
-
-int pwc_set_flicker(struct pwc_device *pdev, int flicker)
-{
-       unsigned char buf;
-
-       if (flicker)
-               buf = 0xff;
-       else
-               buf = 0x0;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, &buf, sizeof(buf));
-}
-
-int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
-{
-       int ret;
-       unsigned char buf;
-
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *flicker = !!buf;
-       return 0;
-}
-
-int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
-{
-       unsigned char buf;
-
-       if (noise < 0)
-               noise = 0;
-       if (noise > 3)
-               noise = 3;
-       buf = noise;
-       return send_control_msg(pdev,
-               SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER,
-               &buf, sizeof(buf));
-}
-
-int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
-{
-       int ret;
-       unsigned char buf;
-
-       ret = recv_control_msg(pdev,
-               GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER,
-               &buf, sizeof(buf));
-       if (ret < 0)
-               return ret;
-       *noise = buf;
-       return 0;
-}
-
 static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
 {
        unsigned char buf;
@@ -1357,37 +825,41 @@ int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
 /* copy local variable to arg */
 #define ARG_OUT(ARG_name) /* nothing */
 
+/*
+ * Our ctrls use native values, but the old custom pwc ioctl interface expects
+ * values from 0 - 65535, define 2 helper functions to scale things. */
+static int pwc_ioctl_g_ctrl(struct v4l2_ctrl *ctrl)
+{
+       return v4l2_ctrl_g_ctrl(ctrl) * 65535 / ctrl->maximum;
+}
+
+static int pwc_ioctl_s_ctrl(struct v4l2_ctrl *ctrl, int val)
+{
+       return v4l2_ctrl_s_ctrl(ctrl, val * ctrl->maximum / 65535);
+}
+
 long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
 {
        long ret = 0;
 
        switch(cmd) {
        case VIDIOCPWCRUSER:
-       {
-               if (pwc_restore_user(pdev))
-                       ret = -EINVAL;
+               ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
                break;
-       }
 
        case VIDIOCPWCSUSER:
-       {
-               if (pwc_save_user(pdev))
-                       ret = -EINVAL;
+               ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
                break;
-       }
 
        case VIDIOCPWCFACTORY:
-       {
-               if (pwc_restore_factory(pdev))
-                       ret = -EINVAL;
+               ret = pwc_button_ctrl(pdev, RESTORE_FACTORY_DEFAULTS_FORMATTER);
                break;
-       }
 
        case VIDIOCPWCSCQUAL:
        {
                ARG_DEF(int, qual)
 
-               if (pdev->iso_init) {
+               if (vb2_is_streaming(&pdev->vb_queue)) {
                        ret = -EBUSY;
                        break;
                }
@@ -1431,71 +903,59 @@ long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
        case VIDIOCPWCSAGC:
        {
                ARG_DEF(int, agc)
-
                ARG_IN(agc)
-               if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
-                       ret = -EINVAL;
+               ret = v4l2_ctrl_s_ctrl(pdev->autogain, ARGR(agc) < 0);
+               if (ret == 0 && ARGR(agc) >= 0)
+                       ret = pwc_ioctl_s_ctrl(pdev->gain, ARGR(agc));
                break;
        }
 
        case VIDIOCPWCGAGC:
        {
                ARG_DEF(int, agc)
-
-               if (pwc_get_agc(pdev, ARGA(agc)))
-                       ret = -EINVAL;
+               if (v4l2_ctrl_g_ctrl(pdev->autogain))
+                       ARGR(agc) = -1;
+               else
+                       ARGR(agc) = pwc_ioctl_g_ctrl(pdev->gain);
                ARG_OUT(agc)
                break;
        }
 
        case VIDIOCPWCSSHUTTER:
        {
-               ARG_DEF(int, shutter_speed)
-
-               ARG_IN(shutter_speed)
-               ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
+               ARG_DEF(int, shutter)
+               ARG_IN(shutter)
+               ret = v4l2_ctrl_s_ctrl(pdev->exposure_auto,
+                                      /* Menu idx 0 = auto, idx 1 = manual */
+                                      ARGR(shutter) >= 0);
+               if (ret == 0 && ARGR(shutter) >= 0)
+                       ret = pwc_ioctl_s_ctrl(pdev->exposure, ARGR(shutter));
                break;
        }
 
        case VIDIOCPWCSAWB:
        {
                ARG_DEF(struct pwc_whitebalance, wb)
-
                ARG_IN(wb)
-               ret = pwc_set_awb(pdev, ARGR(wb).mode);
-               if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
-                       pwc_set_red_gain(pdev, ARGR(wb).manual_red);
-                       pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
-               }
+               ret = v4l2_ctrl_s_ctrl(pdev->auto_white_balance,
+                                      ARGR(wb).mode);
+               if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
+                       ret = pwc_ioctl_s_ctrl(pdev->red_balance,
+                                              ARGR(wb).manual_red);
+               if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
+                       ret = pwc_ioctl_s_ctrl(pdev->blue_balance,
+                                              ARGR(wb).manual_blue);
                break;
        }
 
        case VIDIOCPWCGAWB:
        {
                ARG_DEF(struct pwc_whitebalance, wb)
-
-               memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
-               ARGR(wb).mode = pwc_get_awb(pdev);
-               if (ARGR(wb).mode < 0)
-                       ret = -EINVAL;
-               else {
-                       if (ARGR(wb).mode == PWC_WB_MANUAL) {
-                               ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
-                               if (ret < 0)
-                                       break;
-                               ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
-                               if (ret < 0)
-                                       break;
-                       }
-                       if (ARGR(wb).mode == PWC_WB_AUTO) {
-                               ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
-                               if (ret < 0)
-                                       break;
-                               ret = pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
-                               if (ret < 0)
-                                       break;
-                       }
-               }
+               ARGR(wb).mode = v4l2_ctrl_g_ctrl(pdev->auto_white_balance);
+               ARGR(wb).manual_red = ARGR(wb).read_red =
+                       pwc_ioctl_g_ctrl(pdev->red_balance);
+               ARGR(wb).manual_blue = ARGR(wb).read_blue =
+                       pwc_ioctl_g_ctrl(pdev->blue_balance);
                ARG_OUT(wb)
                break;
        }
@@ -1549,17 +1009,20 @@ long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
        case VIDIOCPWCSCONTOUR:
        {
                ARG_DEF(int, contour)
-
                ARG_IN(contour)
-               ret = pwc_set_contour(pdev, ARGR(contour));
+               ret = v4l2_ctrl_s_ctrl(pdev->autocontour, ARGR(contour) < 0);
+               if (ret == 0 && ARGR(contour) >= 0)
+                       ret = pwc_ioctl_s_ctrl(pdev->contour, ARGR(contour));
                break;
        }
 
        case VIDIOCPWCGCONTOUR:
        {
                ARG_DEF(int, contour)
-
-               ret = pwc_get_contour(pdev, ARGA(contour));
+               if (v4l2_ctrl_g_ctrl(pdev->autocontour))
+                       ARGR(contour) = -1;
+               else
+                       ARGR(contour) = pwc_ioctl_g_ctrl(pdev->contour);
                ARG_OUT(contour)
                break;
        }
@@ -1567,17 +1030,15 @@ long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
        case VIDIOCPWCSBACKLIGHT:
        {
                ARG_DEF(int, backlight)
-
                ARG_IN(backlight)
-               ret = pwc_set_backlight(pdev, ARGR(backlight));
+               ret = v4l2_ctrl_s_ctrl(pdev->backlight, ARGR(backlight));
                break;
        }
 
        case VIDIOCPWCGBACKLIGHT:
        {
                ARG_DEF(int, backlight)
-
-               ret = pwc_get_backlight(pdev, ARGA(backlight));
+               ARGR(backlight) = v4l2_ctrl_g_ctrl(pdev->backlight);
                ARG_OUT(backlight)
                break;
        }
@@ -1585,17 +1046,15 @@ long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
        case VIDIOCPWCSFLICKER:
        {
                ARG_DEF(int, flicker)
-
                ARG_IN(flicker)
-               ret = pwc_set_flicker(pdev, ARGR(flicker));
+               ret = v4l2_ctrl_s_ctrl(pdev->flicker, ARGR(flicker));
                break;
        }
 
        case VIDIOCPWCGFLICKER:
        {
                ARG_DEF(int, flicker)
-
-               ret = pwc_get_flicker(pdev, ARGA(flicker));
+               ARGR(flicker) = v4l2_ctrl_g_ctrl(pdev->flicker);
                ARG_OUT(flicker)
                break;
        }
@@ -1603,17 +1062,15 @@ long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
        case VIDIOCPWCSDYNNOISE:
        {
                ARG_DEF(int, dynnoise)
-
                ARG_IN(dynnoise)
-               ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
+               ret = v4l2_ctrl_s_ctrl(pdev->noise_reduction, ARGR(dynnoise));
                break;
        }
 
        case VIDIOCPWCGDYNNOISE:
        {
                ARG_DEF(int, dynnoise)
-
-               ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
+               ARGR(dynnoise) = v4l2_ctrl_g_ctrl(pdev->noise_reduction);
                ARG_OUT(dynnoise);
                break;
        }
index 4c1c056ffdf492bb2ac2172e0b9431d9be5b1db8..1d5a6db915fa2656f7d20c8dc4a6e50e5954012e 100644 (file)
@@ -679,6 +679,8 @@ static void pwc_video_release(struct video_device *vfd)
                pdev->decompress_data = NULL;
        }
 
+       v4l2_ctrl_handler_free(&pdev->ctrl_handler);
+
        kfree(pdev);
 }
 
@@ -1224,9 +1226,14 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
        if (rc)
                goto err_free_mem;
 
-       /* Initialize the webcam to sane values */
-       pwc_set_brightness(pdev, 0x7fff);
-       pwc_set_agc(pdev, 1, 0);
+       /* Register controls (and read default values from camera */
+       rc = pwc_init_controls(pdev);
+       if (rc) {
+               PWC_ERROR("Failed to register v4l2 controls (%d).\n", rc);
+               goto err_free_mem;
+       }
+
+       pdev->vdev.ctrl_handler = &pdev->ctrl_handler;
 
        /* And powerdown the camera until streaming starts */
        pwc_camera_power(pdev, 0);
@@ -1234,7 +1241,7 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
        rc = video_register_device(&pdev->vdev, VFL_TYPE_GRABBER, video_nr);
        if (rc < 0) {
                PWC_ERROR("Failed to register as video device (%d).\n", rc);
-               goto err_free_mem;
+               goto err_free_controls;
        }
        rc = pwc_create_sysfs_files(pdev);
        if (rc)
@@ -1277,7 +1284,10 @@ err_video_unreg:
        if (hint < MAX_DEV_HINTS)
                device_hint[hint].pdev = NULL;
        video_unregister_device(&pdev->vdev);
+err_free_controls:
+       v4l2_ctrl_handler_free(&pdev->ctrl_handler);
 err_free_mem:
+       usb_set_intfdata(intf, NULL);
        kfree(pdev);
        return rc;
 }
index 834055b71e0418352d100a650d57bddeb7a407c8..986dd5d6187a8a3ee4f5794caaac78e424ec9504 100644 (file)
@@ -2,6 +2,7 @@
    USB and Video4Linux interface part.
    (C) 1999-2004 Nemosoft Unv.
    (C) 2004-2006 Luc Saillard (luc@saillard.org)
+   (C) 2011 Hans de Goede <hdegoede@redhat.com>
 
    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
    driver and thus may have bugs that are not present in the original version.
 #include <linux/module.h>
 #include <linux/poll.h>
 #include <linux/vmalloc.h>
+#include <linux/jiffies.h>
 #include <asm/io.h>
 
 #include "pwc.h"
 
-static struct v4l2_queryctrl pwc_controls[] = {
-       {
-           .id      = V4L2_CID_BRIGHTNESS,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Brightness",
-           .minimum = 0,
-           .maximum = 128,
-           .step    = 1,
-           .default_value = 64,
-       },
-       {
-           .id      = V4L2_CID_CONTRAST,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Contrast",
-           .minimum = 0,
-           .maximum = 64,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_SATURATION,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Saturation",
-           .minimum = -100,
-           .maximum = 100,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_GAMMA,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Gamma",
-           .minimum = 0,
-           .maximum = 32,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_RED_BALANCE,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Red Gain",
-           .minimum = 0,
-           .maximum = 256,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_BLUE_BALANCE,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Blue Gain",
-           .minimum = 0,
-           .maximum = 256,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_AUTO_WHITE_BALANCE,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Auto White Balance",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_EXPOSURE,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Shutter Speed (Exposure)",
-           .minimum = 0,
-           .maximum = 256,
-           .step    = 1,
-           .default_value = 200,
-       },
-       {
-           .id      = V4L2_CID_AUTOGAIN,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Auto Gain Enabled",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 1,
-       },
-       {
-           .id      = V4L2_CID_GAIN,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Gain Level",
-           .minimum = 0,
-           .maximum = 256,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_SAVE_USER,
-           .type    = V4L2_CTRL_TYPE_BUTTON,
-           .name    = "Save User Settings",
-           .minimum = 0,
-           .maximum = 0,
-           .step    = 0,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_RESTORE_USER,
-           .type    = V4L2_CTRL_TYPE_BUTTON,
-           .name    = "Restore User Settings",
-           .minimum = 0,
-           .maximum = 0,
-           .step    = 0,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_RESTORE_FACTORY,
-           .type    = V4L2_CTRL_TYPE_BUTTON,
-           .name    = "Restore Factory Settings",
-           .minimum = 0,
-           .maximum = 0,
-           .step    = 0,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_COLOUR_MODE,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Colour mode",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_AUTOCONTOUR,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Auto contour",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_CONTOUR,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Contour",
-           .minimum = 0,
-           .maximum = 63,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_BACKLIGHT,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Backlight compensation",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-         .id      = V4L2_CID_PRIVATE_FLICKERLESS,
-           .type    = V4L2_CTRL_TYPE_BOOLEAN,
-           .name    = "Flickerless",
-           .minimum = 0,
-           .maximum = 1,
-           .step    = 1,
-           .default_value = 0,
-       },
-       {
-           .id      = V4L2_CID_PRIVATE_NOISE_REDUCTION,
-           .type    = V4L2_CTRL_TYPE_INTEGER,
-           .name    = "Noise reduction",
-           .minimum = 0,
-           .maximum = 3,
-           .step    = 1,
-           .default_value = 0,
-       },
+#define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
+
+static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
+static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
+
+static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
+       .g_volatile_ctrl = pwc_g_volatile_ctrl,
+       .s_ctrl = pwc_s_ctrl,
+};
+
+enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
+enum { custom_autocontour, custom_contour, custom_noise_reduction,
+       custom_save_user, custom_restore_user, custom_restore_factory };
+
+const char * const pwc_auto_whitebal_qmenu[] = {
+       "Indoor (Incandescant Lighting) Mode",
+       "Outdoor (Sunlight) Mode",
+       "Indoor (Fluorescent Lighting) Mode",
+       "Manual Mode",
+       "Auto Mode",
+       NULL
+};
+
+static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = V4L2_CID_AUTO_WHITE_BALANCE,
+       .type   = V4L2_CTRL_TYPE_MENU,
+       .max    = awb_auto,
+       .qmenu  = pwc_auto_whitebal_qmenu,
+};
+
+static const struct v4l2_ctrl_config pwc_autocontour_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(autocontour),
+       .type   = V4L2_CTRL_TYPE_BOOLEAN,
+       .name   = "Auto contour",
+       .min    = 0,
+       .max    = 1,
+       .step   = 1,
+};
+
+static const struct v4l2_ctrl_config pwc_contour_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(contour),
+       .type   = V4L2_CTRL_TYPE_INTEGER,
+       .name   = "Contour",
+       .min    = 0,
+       .max    = 63,
+       .step   = 1,
+};
+
+static const struct v4l2_ctrl_config pwc_backlight_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = V4L2_CID_BACKLIGHT_COMPENSATION,
+       .type   = V4L2_CTRL_TYPE_BOOLEAN,
+       .min    = 0,
+       .max    = 1,
+       .step   = 1,
 };
 
+static const struct v4l2_ctrl_config pwc_flicker_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = V4L2_CID_BAND_STOP_FILTER,
+       .type   = V4L2_CTRL_TYPE_BOOLEAN,
+       .min    = 0,
+       .max    = 1,
+       .step   = 1,
+};
+
+static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(noise_reduction),
+       .type   = V4L2_CTRL_TYPE_INTEGER,
+       .name   = "Dynamic Noise Reduction",
+       .min    = 0,
+       .max    = 3,
+       .step   = 1,
+};
+
+static const struct v4l2_ctrl_config pwc_save_user_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(save_user),
+       .type   = V4L2_CTRL_TYPE_BUTTON,
+       .name    = "Save User Settings",
+};
+
+static const struct v4l2_ctrl_config pwc_restore_user_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(restore_user),
+       .type   = V4L2_CTRL_TYPE_BUTTON,
+       .name    = "Restore User Settings",
+};
+
+static const struct v4l2_ctrl_config pwc_restore_factory_cfg = {
+       .ops    = &pwc_ctrl_ops,
+       .id     = PWC_CID_CUSTOM(restore_factory),
+       .type   = V4L2_CTRL_TYPE_BUTTON,
+       .name    = "Restore Factory Settings",
+};
+
+int pwc_init_controls(struct pwc_device *pdev)
+{
+       struct v4l2_ctrl_handler *hdl;
+       struct v4l2_ctrl_config cfg;
+       int r, def;
+
+       hdl = &pdev->ctrl_handler;
+       r = v4l2_ctrl_handler_init(hdl, 20);
+       if (r)
+               return r;
+
+       /* Brightness, contrast, saturation, gamma */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def);
+       if (r || def > 127)
+               def = 63;
+       pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_BRIGHTNESS, 0, 127, 1, def);
+
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def);
+       if (r || def > 63)
+               def = 31;
+       pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_CONTRAST, 0, 63, 1, def);
+
+       if (pdev->type >= 675) {
+               if (pdev->type < 730)
+                       pdev->saturation_fmt = SATURATION_MODE_FORMATTER2;
+               else
+                       pdev->saturation_fmt = SATURATION_MODE_FORMATTER1;
+               r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt,
+                                   &def);
+               if (r || def < -100 || def > 100)
+                       def = 0;
+               pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                                     V4L2_CID_SATURATION, -100, 100, 1, def);
+       }
+
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def);
+       if (r || def > 31)
+               def = 15;
+       pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_GAMMA, 0, 31, 1, def);
+
+       /* auto white balance, red gain, blue gain */
+       r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def);
+       if (r || def > awb_auto)
+               def = awb_auto;
+       cfg = pwc_auto_white_balance_cfg;
+       cfg.name = v4l2_ctrl_get_name(cfg.id);
+       cfg.def = def;
+       pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+       /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */
+       if (!pdev->auto_white_balance)
+               return hdl->error;
+
+       r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
+                           PRESET_MANUAL_RED_GAIN_FORMATTER, &def);
+       if (r)
+               def = 127;
+       pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_RED_BALANCE, 0, 255, 1, def);
+
+       r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL,
+                           PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def);
+       if (r)
+               def = 127;
+       pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_BLUE_BALANCE, 0, 255, 1, def);
+
+       v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual,
+                              pdev->auto_white_balance->cur.val == awb_auto);
+
+       /* autogain, gain */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def);
+       if (r || (def != 0 && def != 0xff))
+               def = 0;
+       /* Note a register value if 0 means auto gain is on */
+       pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0);
+       if (!pdev->autogain)
+               return hdl->error;
+
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def);
+       if (r || def > 63)
+               def = 31;
+       pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_GAIN, 0, 63, 1, def);
+
+       /* auto exposure, exposure */
+       if (DEVICE_USE_CODEC2(pdev->type)) {
+               r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER,
+                                   &def);
+               if (r || (def != 0 && def != 0xff))
+                       def = 0;
+               /*
+                * def = 0 auto, def = ff manual
+                * menu idx 0 = auto, idx 1 = manual
+                */
+               pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl,
+                                       &pwc_ctrl_ops,
+                                       V4L2_CID_EXPOSURE_AUTO,
+                                       1, 0, def != 0);
+               if (!pdev->exposure_auto)
+                       return hdl->error;
+
+               /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
+               r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
+                                    READ_SHUTTER_FORMATTER, &def);
+               if (r || def > 655)
+                       def = 655;
+               pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                                       V4L2_CID_EXPOSURE, 0, 655, 1, def);
+               /* CODEC2: separate auto gain & auto exposure */
+               v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true);
+               v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto,
+                                      V4L2_EXPOSURE_MANUAL, true);
+       } else if (DEVICE_USE_CODEC3(pdev->type)) {
+               /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */
+               r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
+                                    READ_SHUTTER_FORMATTER, &def);
+               if (r || def > 255)
+                       def = 255;
+               pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops,
+                                       V4L2_CID_EXPOSURE, 0, 255, 1, def);
+               /* CODEC3: both gain and exposure controlled by autogain */
+               pdev->autogain_expo_cluster[0] = pdev->autogain;
+               pdev->autogain_expo_cluster[1] = pdev->gain;
+               pdev->autogain_expo_cluster[2] = pdev->exposure;
+               v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster,
+                                      0, true);
+       }
+
+       /* color / bw setting */
+       r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER,
+                        &def);
+       if (r || (def != 0 && def != 0xff))
+               def = 0xff;
+       /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */
+       pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops,
+                               V4L2_CID_COLORFX, 1, 0, def == 0);
+
+       /* autocontour, contour */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def);
+       if (r || (def != 0 && def != 0xff))
+               def = 0;
+       cfg = pwc_autocontour_cfg;
+       cfg.def = def == 0;
+       pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+       if (!pdev->autocontour)
+               return hdl->error;
+
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def);
+       if (r || def > 63)
+               def = 31;
+       cfg = pwc_contour_cfg;
+       cfg.def = def;
+       pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+
+       v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false);
+
+       /* backlight */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
+                           BACK_LIGHT_COMPENSATION_FORMATTER, &def);
+       if (r || (def != 0 && def != 0xff))
+               def = 0;
+       cfg = pwc_backlight_cfg;
+       cfg.name = v4l2_ctrl_get_name(cfg.id);
+       cfg.def = def == 0;
+       pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+
+       /* flikker rediction */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
+                           FLICKERLESS_MODE_FORMATTER, &def);
+       if (r || (def != 0 && def != 0xff))
+               def = 0;
+       cfg = pwc_flicker_cfg;
+       cfg.name = v4l2_ctrl_get_name(cfg.id);
+       cfg.def = def == 0;
+       pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+
+       /* Dynamic noise reduction */
+       r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL,
+                           DYNAMIC_NOISE_CONTROL_FORMATTER, &def);
+       if (r || def > 3)
+               def = 2;
+       cfg = pwc_noise_reduction_cfg;
+       cfg.def = def;
+       pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL);
+
+       /* Save / Restore User / Factory Settings */
+       pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL);
+       pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg,
+                                                 NULL);
+       if (pdev->restore_user)
+               pdev->restore_user->flags = V4L2_CTRL_FLAG_UPDATE;
+       pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
+                                                    &pwc_restore_factory_cfg,
+                                                    NULL);
+       if (pdev->restore_factory)
+               pdev->restore_factory->flags = V4L2_CTRL_FLAG_UPDATE;
+
+       return hdl->error;
+}
 
 static void pwc_vidioc_fill_fmt(const struct pwc_device *pdev, struct v4l2_format *f)
 {
@@ -354,14 +485,13 @@ static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
 
 static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
 {
-       struct video_device *vdev = video_devdata(file);
        struct pwc_device *pdev = video_drvdata(file);
 
        if (!pdev->udev)
                return -ENODEV;
 
        strcpy(cap->driver, PWC_NAME);
-       strlcpy(cap->card, vdev->name, sizeof(cap->card));
+       strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
        usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
        cap->capabilities =
                V4L2_CAP_VIDEO_CAPTURE  |
@@ -390,261 +520,328 @@ static int pwc_s_input(struct file *file, void *fh, unsigned int i)
        return i ? -EINVAL : 0;
 }
 
-static int pwc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
+static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 {
-       int i, idx;
-       u32 id;
-
-       id = c->id;
-       if (id & V4L2_CTRL_FLAG_NEXT_CTRL) {
-               id &= V4L2_CTRL_ID_MASK;
-               id++;
-               idx = -1;
-               for (i = 0; i < ARRAY_SIZE(pwc_controls); i++) {
-                       if (pwc_controls[i].id < id)
-                               continue;
-                       if (idx >= 0
-                        && pwc_controls[i].id > pwc_controls[idx].id)
-                               continue;
-                       idx = i;
+       struct pwc_device *pdev =
+               container_of(ctrl->handler, struct pwc_device, ctrl_handler);
+       int ret = 0;
+
+       if (!pdev->udev)
+               return -ENODEV;
+
+       switch (ctrl->id) {
+       case V4L2_CID_AUTO_WHITE_BALANCE:
+               if (pdev->color_bal_valid && time_before(jiffies,
+                               pdev->last_color_bal_update + HZ / 4)) {
+                       pdev->red_balance->val  = pdev->last_red_balance;
+                       pdev->blue_balance->val = pdev->last_blue_balance;
+                       break;
                }
-               if (idx < 0)
-                       return -EINVAL;
-               memcpy(c, &pwc_controls[idx], sizeof pwc_controls[0]);
-               return 0;
-       }
-       for (i = 0; i < sizeof(pwc_controls) / sizeof(struct v4l2_queryctrl); i++) {
-               if (pwc_controls[i].id == c->id) {
-                       PWC_DEBUG_IOCTL("ioctl(VIDIOC_QUERYCTRL) found\n");
-                       memcpy(c, &pwc_controls[i], sizeof(struct v4l2_queryctrl));
-                       return 0;
+               ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                     READ_RED_GAIN_FORMATTER,
+                                     &pdev->red_balance->val);
+               if (ret)
+                       break;
+               ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                     READ_BLUE_GAIN_FORMATTER,
+                                     &pdev->blue_balance->val);
+               if (ret)
+                       break;
+               pdev->last_red_balance  = pdev->red_balance->val;
+               pdev->last_blue_balance = pdev->blue_balance->val;
+               pdev->last_color_bal_update = jiffies;
+               pdev->color_bal_valid = true;
+               break;
+       case V4L2_CID_AUTOGAIN:
+               if (pdev->gain_valid && time_before(jiffies,
+                               pdev->last_gain_update + HZ / 4)) {
+                       pdev->gain->val = pdev->last_gain;
+                       break;
                }
+               ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                     READ_AGC_FORMATTER, &pdev->gain->val);
+               if (ret)
+                       break;
+               pdev->last_gain = pdev->gain->val;
+               pdev->last_gain_update = jiffies;
+               pdev->gain_valid = true;
+               if (!DEVICE_USE_CODEC3(pdev->type))
+                       break;
+               /* Fall through for CODEC3 where autogain also controls expo */
+       case V4L2_CID_EXPOSURE_AUTO:
+               if (pdev->exposure_valid && time_before(jiffies,
+                               pdev->last_exposure_update + HZ / 4)) {
+                       pdev->exposure->val = pdev->last_exposure;
+                       break;
+               }
+               ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
+                                      READ_SHUTTER_FORMATTER,
+                                      &pdev->exposure->val);
+               if (ret)
+                       break;
+               pdev->last_exposure = pdev->exposure->val;
+               pdev->last_exposure_update = jiffies;
+               pdev->exposure_valid = true;
+               break;
+       default:
+               ret = -EINVAL;
        }
-       return -EINVAL;
+
+       if (ret)
+               PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
+
+       return ret;
 }
 
-static int pwc_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
+static int pwc_set_awb(struct pwc_device *pdev)
 {
-       struct pwc_device *pdev = video_drvdata(file);
-       int ret;
+       int ret = 0;
+
+       if (pdev->auto_white_balance->is_new) {
+               ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
+                                     WB_MODE_FORMATTER,
+                                     pdev->auto_white_balance->val);
+               if (ret)
+                       return ret;
+
+               /* Update val when coming from auto or going to a preset */
+               if (pdev->red_balance->is_volatile ||
+                   pdev->auto_white_balance->val == awb_indoor ||
+                   pdev->auto_white_balance->val == awb_outdoor ||
+                   pdev->auto_white_balance->val == awb_fl) {
+                       if (!pdev->red_balance->is_new)
+                               pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                       READ_RED_GAIN_FORMATTER,
+                                       &pdev->red_balance->val);
+                       if (!pdev->blue_balance->is_new)
+                               pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                       READ_BLUE_GAIN_FORMATTER,
+                                       &pdev->blue_balance->val);
+               }
+               if (pdev->auto_white_balance->val == awb_auto) {
+                       pdev->red_balance->is_volatile = true;
+                       pdev->blue_balance->is_volatile = true;
+                       pdev->color_bal_valid = false; /* Force cache update */
+               } else {
+                       pdev->red_balance->is_volatile = false;
+                       pdev->blue_balance->is_volatile = false;
+               }
+       }
 
-       if (!pdev->udev)
-               return -ENODEV;
+       if (ret == 0 && pdev->red_balance->is_new) {
+               if (pdev->auto_white_balance->val != awb_manual)
+                       return -EBUSY;
+               ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
+                                     PRESET_MANUAL_RED_GAIN_FORMATTER,
+                                     pdev->red_balance->val);
+       }
 
-       switch (c->id) {
-       case V4L2_CID_BRIGHTNESS:
-               c->value = pwc_get_brightness(pdev);
-               if (c->value < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_CONTRAST:
-               c->value = pwc_get_contrast(pdev);
-               if (c->value < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_SATURATION:
-               ret = pwc_get_saturation(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_GAMMA:
-               c->value = pwc_get_gamma(pdev);
-               if (c->value < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_RED_BALANCE:
-               ret = pwc_get_red_gain(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value >>= 8;
-               return 0;
-       case V4L2_CID_BLUE_BALANCE:
-               ret = pwc_get_blue_gain(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value >>= 8;
-               return 0;
-       case V4L2_CID_AUTO_WHITE_BALANCE:
-               ret = pwc_get_awb(pdev);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value = (ret == PWC_WB_MANUAL) ? 0 : 1;
-               return 0;
-       case V4L2_CID_GAIN:
-               ret = pwc_get_agc(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value >>= 8;
-               return 0;
-       case V4L2_CID_AUTOGAIN:
-               ret = pwc_get_agc(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value = (c->value < 0) ? 1 : 0;
-               return 0;
-       case V4L2_CID_EXPOSURE:
-               ret = pwc_get_shutter_speed(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_COLOUR_MODE:
-               ret = pwc_get_colour_mode(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_AUTOCONTOUR:
-               ret = pwc_get_contour(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value = (c->value == -1 ? 1 : 0);
-               return 0;
-       case V4L2_CID_PRIVATE_CONTOUR:
-               ret = pwc_get_contour(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value >>= 10;
-               return 0;
-       case V4L2_CID_PRIVATE_BACKLIGHT:
-               ret = pwc_get_backlight(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_FLICKERLESS:
-               ret = pwc_get_flicker(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               c->value = (c->value ? 1 : 0);
-               return 0;
-       case V4L2_CID_PRIVATE_NOISE_REDUCTION:
-               ret = pwc_get_dynamic_noise(pdev, &c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+       if (ret == 0 && pdev->blue_balance->is_new) {
+               if (pdev->auto_white_balance->val != awb_manual)
+                       return -EBUSY;
+               ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
+                                     PRESET_MANUAL_BLUE_GAIN_FORMATTER,
+                                     pdev->blue_balance->val);
+       }
+       return ret;
+}
 
-       case V4L2_CID_PRIVATE_SAVE_USER:
-       case V4L2_CID_PRIVATE_RESTORE_USER:
-       case V4L2_CID_PRIVATE_RESTORE_FACTORY:
-               return -EINVAL;
+/* For CODEC2 models which have separate autogain and auto exposure */
+static int pwc_set_autogain(struct pwc_device *pdev)
+{
+       int ret = 0;
+
+       if (pdev->autogain->is_new) {
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     AGC_MODE_FORMATTER,
+                                     pdev->autogain->val ? 0 : 0xff);
+               if (ret)
+                       return ret;
+               if (pdev->autogain->val)
+                       pdev->gain_valid = false; /* Force cache update */
+               else if (!pdev->gain->is_new)
+                       pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                       READ_AGC_FORMATTER,
+                                       &pdev->gain->val);
        }
-       return -EINVAL;
+       if (ret == 0 && pdev->gain->is_new) {
+               if (pdev->autogain->val)
+                       return -EBUSY;
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     PRESET_AGC_FORMATTER,
+                                     pdev->gain->val);
+       }
+       return ret;
 }
 
-static int pwc_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
+/* For CODEC2 models which have separate autogain and auto exposure */
+static int pwc_set_exposure_auto(struct pwc_device *pdev)
 {
-       struct pwc_device *pdev = video_drvdata(file);
-       int ret;
+       int ret = 0;
+       int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
+
+       if (pdev->exposure_auto->is_new) {
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     SHUTTER_MODE_FORMATTER,
+                                     is_auto ? 0 : 0xff);
+               if (ret)
+                       return ret;
+               if (is_auto)
+                       pdev->exposure_valid = false; /* Force cache update */
+               else if (!pdev->exposure->is_new)
+                       pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
+                                        READ_SHUTTER_FORMATTER,
+                                        &pdev->exposure->val);
+       }
+       if (ret == 0 && pdev->exposure->is_new) {
+               if (is_auto)
+                       return -EBUSY;
+               ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
+                                      PRESET_SHUTTER_FORMATTER,
+                                      pdev->exposure->val);
+       }
+       return ret;
+}
+
+/* For CODEC3 models which have autogain controlling both gain and exposure */
+static int pwc_set_autogain_expo(struct pwc_device *pdev)
+{
+       int ret = 0;
+
+       if (pdev->autogain->is_new) {
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     AGC_MODE_FORMATTER,
+                                     pdev->autogain->val ? 0 : 0xff);
+               if (ret)
+                       return ret;
+               if (pdev->autogain->val) {
+                       pdev->gain_valid     = false; /* Force cache update */
+                       pdev->exposure_valid = false; /* Force cache update */
+               } else {
+                       if (!pdev->gain->is_new)
+                               pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
+                                               READ_AGC_FORMATTER,
+                                               &pdev->gain->val);
+                       if (!pdev->exposure->is_new)
+                               pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
+                                                READ_SHUTTER_FORMATTER,
+                                                &pdev->exposure->val);
+               }
+       }
+       if (ret == 0 && pdev->gain->is_new) {
+               if (pdev->autogain->val)
+                       return -EBUSY;
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     PRESET_AGC_FORMATTER,
+                                     pdev->gain->val);
+       }
+       if (ret == 0 && pdev->exposure->is_new) {
+               if (pdev->autogain->val)
+                       return -EBUSY;
+               ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
+                                      PRESET_SHUTTER_FORMATTER,
+                                      pdev->exposure->val);
+       }
+       return ret;
+}
+
+static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct pwc_device *pdev =
+               container_of(ctrl->handler, struct pwc_device, ctrl_handler);
+       int ret = 0;
 
        if (!pdev->udev)
                return -ENODEV;
 
-       switch (c->id) {
+       switch (ctrl->id) {
        case V4L2_CID_BRIGHTNESS:
-               c->value <<= 9;
-               ret = pwc_set_brightness(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     BRIGHTNESS_FORMATTER, ctrl->val);
+               break;
        case V4L2_CID_CONTRAST:
-               c->value <<= 10;
-               ret = pwc_set_contrast(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     CONTRAST_FORMATTER, ctrl->val);
+               break;
        case V4L2_CID_SATURATION:
-               ret = pwc_set_saturation(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+               ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
+                                     pdev->saturation_fmt, ctrl->val);
+               break;
        case V4L2_CID_GAMMA:
-               c->value <<= 11;
-               ret = pwc_set_gamma(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_RED_BALANCE:
-               c->value <<= 8;
-               ret = pwc_set_red_gain(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_BLUE_BALANCE:
-               c->value <<= 8;
-               ret = pwc_set_blue_gain(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     GAMMA_FORMATTER, ctrl->val);
+               break;
        case V4L2_CID_AUTO_WHITE_BALANCE:
-               c->value = (c->value == 0) ? PWC_WB_MANUAL : PWC_WB_AUTO;
-               ret = pwc_set_awb(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_EXPOSURE:
-               c->value <<= 8;
-               ret = pwc_set_shutter_speed(pdev, c->value ? 0 : 1, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
+               ret = pwc_set_awb(pdev);
+               break;
        case V4L2_CID_AUTOGAIN:
-               /* autogain off means nothing without a gain */
-               if (c->value == 0)
-                       return 0;
-               ret = pwc_set_agc(pdev, c->value, 0);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_GAIN:
-               c->value <<= 8;
-               ret = pwc_set_agc(pdev, 0, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_SAVE_USER:
-               if (pwc_save_user(pdev))
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_RESTORE_USER:
-               if (pwc_restore_user(pdev))
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_RESTORE_FACTORY:
-               if (pwc_restore_factory(pdev))
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_COLOUR_MODE:
-               ret = pwc_set_colour_mode(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_AUTOCONTOUR:
-               c->value = (c->value == 1) ? -1 : 0;
-               ret = pwc_set_contour(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_CONTOUR:
-               c->value <<= 10;
-               ret = pwc_set_contour(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_BACKLIGHT:
-               ret = pwc_set_backlight(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-       case V4L2_CID_PRIVATE_FLICKERLESS:
-               ret = pwc_set_flicker(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-       case V4L2_CID_PRIVATE_NOISE_REDUCTION:
-               ret = pwc_set_dynamic_noise(pdev, c->value);
-               if (ret < 0)
-                       return -EINVAL;
-               return 0;
-
+               if (DEVICE_USE_CODEC2(pdev->type))
+                       ret = pwc_set_autogain(pdev);
+               else if (DEVICE_USE_CODEC3(pdev->type))
+                       ret = pwc_set_autogain_expo(pdev);
+               else
+                       ret = -EINVAL;
+               break;
+       case V4L2_CID_EXPOSURE_AUTO:
+               if (DEVICE_USE_CODEC2(pdev->type))
+                       ret = pwc_set_exposure_auto(pdev);
+               else
+                       ret = -EINVAL;
+               break;
+       case V4L2_CID_COLORFX:
+               ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
+                                     COLOUR_MODE_FORMATTER,
+                                     ctrl->val ? 0 : 0xff);
+               break;
+       case PWC_CID_CUSTOM(autocontour):
+               if (pdev->autocontour->is_new) {
+                       ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                       AUTO_CONTOUR_FORMATTER,
+                                       pdev->autocontour->val ? 0 : 0xff);
+               }
+               if (ret == 0 && pdev->contour->is_new) {
+                       if (pdev->autocontour->val) {
+                               ret = -EBUSY;
+                               break;
+                       }
+                       ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                             PRESET_CONTOUR_FORMATTER,
+                                             pdev->contour->val);
+               }
+               break;
+       case V4L2_CID_BACKLIGHT_COMPENSATION:
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     BACK_LIGHT_COMPENSATION_FORMATTER,
+                                     ctrl->val ? 0 : 0xff);
+               break;
+       case V4L2_CID_BAND_STOP_FILTER:
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     FLICKERLESS_MODE_FORMATTER,
+                                     ctrl->val ? 0 : 0xff);
+               break;
+       case PWC_CID_CUSTOM(noise_reduction):
+               ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
+                                     DYNAMIC_NOISE_CONTROL_FORMATTER,
+                                     ctrl->val);
+               break;
+       case PWC_CID_CUSTOM(save_user):
+               ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
+               break;
+       case PWC_CID_CUSTOM(restore_user):
+               ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
+               break;
+       case PWC_CID_CUSTOM(restore_factory):
+               ret = pwc_button_ctrl(pdev,
+                                     RESTORE_FACTORY_DEFAULTS_FORMATTER);
+               break;
+       default:
+               ret = -EINVAL;
        }
-       return -EINVAL;
+
+       if (ret)
+               PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
+
+       return ret;
 }
 
 static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
@@ -835,9 +1032,6 @@ const struct v4l2_ioctl_ops pwc_ioctl_ops = {
        .vidioc_g_fmt_vid_cap               = pwc_g_fmt_vid_cap,
        .vidioc_s_fmt_vid_cap               = pwc_s_fmt_vid_cap,
        .vidioc_try_fmt_vid_cap             = pwc_try_fmt_vid_cap,
-       .vidioc_queryctrl                   = pwc_queryctrl,
-       .vidioc_g_ctrl                      = pwc_g_ctrl,
-       .vidioc_s_ctrl                      = pwc_s_ctrl,
        .vidioc_reqbufs                     = pwc_reqbufs,
        .vidioc_querybuf                    = pwc_querybuf,
        .vidioc_qbuf                        = pwc_qbuf,
index 1cfb8b475a3f42ccff6206972f2d0ce9f1396ec5..601a549988b5e10d938cfaa30f8dcfbed8756eb9 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/videodev2.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-ioctl.h>
+#include <media/v4l2-ctrls.h>
 #include <media/videobuf2-vmalloc.h>
 #ifdef CONFIG_USB_PWC_INPUT_EVDEV
 #include <linux/input.h>
 #define DEVICE_USE_CODEC3(x) ((x)>=700)
 #define DEVICE_USE_CODEC23(x) ((x)>=675)
 
+/* Request types: video */
+#define SET_LUM_CTL                    0x01
+#define GET_LUM_CTL                    0x02
+#define SET_CHROM_CTL                  0x03
+#define GET_CHROM_CTL                  0x04
+#define SET_STATUS_CTL                 0x05
+#define GET_STATUS_CTL                 0x06
+#define SET_EP_STREAM_CTL              0x07
+#define GET_EP_STREAM_CTL              0x08
+#define GET_XX_CTL                     0x09
+#define SET_XX_CTL                     0x0A
+#define GET_XY_CTL                     0x0B
+#define SET_XY_CTL                     0x0C
+#define SET_MPT_CTL                    0x0D
+#define GET_MPT_CTL                    0x0E
+
+/* Selectors for the Luminance controls [GS]ET_LUM_CTL */
+#define AGC_MODE_FORMATTER                     0x2000
+#define PRESET_AGC_FORMATTER                   0x2100
+#define SHUTTER_MODE_FORMATTER                 0x2200
+#define PRESET_SHUTTER_FORMATTER               0x2300
+#define PRESET_CONTOUR_FORMATTER               0x2400
+#define AUTO_CONTOUR_FORMATTER                 0x2500
+#define BACK_LIGHT_COMPENSATION_FORMATTER      0x2600
+#define CONTRAST_FORMATTER                     0x2700
+#define DYNAMIC_NOISE_CONTROL_FORMATTER                0x2800
+#define FLICKERLESS_MODE_FORMATTER             0x2900
+#define AE_CONTROL_SPEED                       0x2A00
+#define BRIGHTNESS_FORMATTER                   0x2B00
+#define GAMMA_FORMATTER                                0x2C00
+
+/* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
+#define WB_MODE_FORMATTER                      0x1000
+#define AWB_CONTROL_SPEED_FORMATTER            0x1100
+#define AWB_CONTROL_DELAY_FORMATTER            0x1200
+#define PRESET_MANUAL_RED_GAIN_FORMATTER       0x1300
+#define PRESET_MANUAL_BLUE_GAIN_FORMATTER      0x1400
+#define COLOUR_MODE_FORMATTER                  0x1500
+#define SATURATION_MODE_FORMATTER1             0x1600
+#define SATURATION_MODE_FORMATTER2             0x1700
+
+/* Selectors for the Status controls [GS]ET_STATUS_CTL */
+#define SAVE_USER_DEFAULTS_FORMATTER           0x0200
+#define RESTORE_USER_DEFAULTS_FORMATTER                0x0300
+#define RESTORE_FACTORY_DEFAULTS_FORMATTER     0x0400
+#define READ_AGC_FORMATTER                     0x0500
+#define READ_SHUTTER_FORMATTER                 0x0600
+#define READ_RED_GAIN_FORMATTER                        0x0700
+#define READ_BLUE_GAIN_FORMATTER               0x0800
+
+/* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
+#define PT_RELATIVE_CONTROL_FORMATTER          0x01
+#define PT_RESET_CONTROL_FORMATTER             0x02
+#define PT_STATUS_FORMATTER                    0x03
+
 /* intermediate buffers with raw data from the USB cam */
 struct pwc_frame_buf
 {
@@ -220,6 +276,55 @@ struct pwc_device
        struct input_dev *button_dev;   /* webcam snapshot button input */
        char button_phys[64];
 #endif
+
+       /* controls */
+       struct v4l2_ctrl_handler        ctrl_handler;
+       u16                             saturation_fmt;
+       struct v4l2_ctrl                *brightness;
+       struct v4l2_ctrl                *contrast;
+       struct v4l2_ctrl                *saturation;
+       struct v4l2_ctrl                *gamma;
+       struct {
+               /* awb / red-blue balance cluster */
+               struct v4l2_ctrl        *auto_white_balance;
+               struct v4l2_ctrl        *red_balance;
+               struct v4l2_ctrl        *blue_balance;
+               /* usb ctrl transfers are slow, so we cache things */
+               int                     color_bal_valid;
+               unsigned long           last_color_bal_update; /* In jiffies */
+               s32                     last_red_balance;
+               s32                     last_blue_balance;
+       };
+       struct {
+               /* autogain / gain cluster */
+               struct v4l2_ctrl        *autogain;
+               struct v4l2_ctrl        *gain;
+               int                     gain_valid;
+               unsigned long           last_gain_update; /* In jiffies */
+               s32                     last_gain;
+       };
+       struct {
+               /* exposure_auto / exposure cluster */
+               struct v4l2_ctrl        *exposure_auto;
+               struct v4l2_ctrl        *exposure;
+               int                     exposure_valid;
+               unsigned long           last_exposure_update; /* In jiffies */
+               s32                     last_exposure;
+       };
+       struct v4l2_ctrl                *colorfx;
+       struct {
+               /* autocontour/contour cluster */
+               struct v4l2_ctrl        *autocontour;
+               struct v4l2_ctrl        *contour;
+       };
+       struct v4l2_ctrl                *backlight;
+       struct v4l2_ctrl                *flicker;
+       struct v4l2_ctrl                *noise_reduction;
+       struct v4l2_ctrl                *save_user;
+       struct v4l2_ctrl                *restore_user;
+       struct v4l2_ctrl                *restore_factory;
+       /* CODEC3 models have both gain and exposure controlled by autogain */
+       struct v4l2_ctrl                *autogain_expo_cluster[3];
 };
 
 /* Global variables */
@@ -238,47 +343,20 @@ void pwc_construct(struct pwc_device *pdev);
 /* Request a certain video mode. Returns < 0 if not possible */
 extern int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot);
 extern unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size);
-/* Calculate the number of bytes per image (not frame) */
 extern int pwc_mpt_reset(struct pwc_device *pdev, int flags);
 extern int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt);
-
-/* Various controls; should be obvious. Value 0..65535, or < 0 on error */
-extern int pwc_get_brightness(struct pwc_device *pdev);
-extern int pwc_set_brightness(struct pwc_device *pdev, int value);
-extern int pwc_get_contrast(struct pwc_device *pdev);
-extern int pwc_set_contrast(struct pwc_device *pdev, int value);
-extern int pwc_get_gamma(struct pwc_device *pdev);
-extern int pwc_set_gamma(struct pwc_device *pdev, int value);
-extern int pwc_get_saturation(struct pwc_device *pdev, int *value);
-extern int pwc_set_saturation(struct pwc_device *pdev, int value);
 extern int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value);
 extern int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor);
-extern int pwc_restore_user(struct pwc_device *pdev);
-extern int pwc_save_user(struct pwc_device *pdev);
-extern int pwc_restore_factory(struct pwc_device *pdev);
-
-/* exported for use by v4l2 controls */
-extern int pwc_get_red_gain(struct pwc_device *pdev, int *value);
-extern int pwc_set_red_gain(struct pwc_device *pdev, int value);
-extern int pwc_get_blue_gain(struct pwc_device *pdev, int *value);
-extern int pwc_set_blue_gain(struct pwc_device *pdev, int value);
-extern int pwc_get_awb(struct pwc_device *pdev);
-extern int pwc_set_awb(struct pwc_device *pdev, int mode);
-extern int pwc_set_agc(struct pwc_device *pdev, int mode, int value);
-extern int pwc_get_agc(struct pwc_device *pdev, int *value);
-extern int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value);
-extern int pwc_get_shutter_speed(struct pwc_device *pdev, int *value);
-
-extern int pwc_set_colour_mode(struct pwc_device *pdev, int colour);
-extern int pwc_get_colour_mode(struct pwc_device *pdev, int *colour);
-extern int pwc_set_contour(struct pwc_device *pdev, int contour);
-extern int pwc_get_contour(struct pwc_device *pdev, int *contour);
-extern int pwc_set_backlight(struct pwc_device *pdev, int backlight);
-extern int pwc_get_backlight(struct pwc_device *pdev, int *backlight);
-extern int pwc_set_flicker(struct pwc_device *pdev, int flicker);
-extern int pwc_get_flicker(struct pwc_device *pdev, int *flicker);
-extern int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise);
-extern int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise);
+
+/* Control get / set helpers */
+int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data);
+int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data);
+int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data);
+#define pwc_set_s8_ctrl pwc_set_u8_ctrl
+int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *dat);
+int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data);
+int pwc_button_ctrl(struct pwc_device *pdev, u16 value);
+int pwc_init_controls(struct pwc_device *pdev);
 
 /* Power down or up the camera; not supported by all models */
 extern void pwc_camera_power(struct pwc_device *pdev, int power);