Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Pixart PAC207BCA library |
| 3 | * |
Hans de Goede | dc1fe15 | 2008-12-14 09:37:02 -0300 | [diff] [blame] | 4 | * Copyright (C) 2008 Hans de Goede <hdgoede@redhat.com> |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 5 | * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li |
| 6 | * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr |
| 7 | * |
| 8 | * V4L2 by Jean-Francois Moine <http://moinejf.free.fr> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #define MODULE_NAME "pac207" |
| 27 | |
| 28 | #include "gspca.h" |
| 29 | |
Hans de Goede | dc1fe15 | 2008-12-14 09:37:02 -0300 | [diff] [blame] | 30 | MODULE_AUTHOR("Hans de Goede <hdgoede@redhat.com>"); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 31 | MODULE_DESCRIPTION("Pixart PAC207"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | #define PAC207_CTRL_TIMEOUT 100 /* ms */ |
| 35 | |
| 36 | #define PAC207_BRIGHTNESS_MIN 0 |
| 37 | #define PAC207_BRIGHTNESS_MAX 255 |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 38 | #define PAC207_BRIGHTNESS_DEFAULT 46 |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 39 | |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 40 | #define PAC207_EXPOSURE_MIN 3 |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 41 | #define PAC207_EXPOSURE_MAX 26 |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 42 | #define PAC207_EXPOSURE_DEFAULT 5 /* power on default: 3 */ |
| 43 | #define PAC207_EXPOSURE_KNEE 8 /* 4 = 30 fps, 11 = 8, 15 = 6 */ |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 44 | |
| 45 | #define PAC207_GAIN_MIN 0 |
| 46 | #define PAC207_GAIN_MAX 31 |
| 47 | #define PAC207_GAIN_DEFAULT 9 /* power on default: 9 */ |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 48 | #define PAC207_GAIN_KNEE 31 |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 49 | |
| 50 | #define PAC207_AUTOGAIN_DEADZONE 30 |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 51 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 52 | /* specific webcam descriptor */ |
| 53 | struct sd { |
| 54 | struct gspca_dev gspca_dev; /* !! must be the first item */ |
| 55 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 56 | u8 mode; |
| 57 | |
| 58 | u8 brightness; |
| 59 | u8 exposure; |
| 60 | u8 autogain; |
| 61 | u8 gain; |
| 62 | |
| 63 | u8 sof_read; |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 64 | u8 header_read; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 65 | u8 autogain_ignore_frames; |
| 66 | |
| 67 | atomic_t avg_lum; |
| 68 | }; |
| 69 | |
| 70 | /* V4L2 controls supported by the driver */ |
| 71 | static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val); |
| 72 | static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val); |
| 73 | static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val); |
| 74 | static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val); |
| 75 | static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val); |
| 76 | static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val); |
| 77 | static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val); |
| 78 | static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val); |
| 79 | |
| 80 | static struct ctrl sd_ctrls[] = { |
| 81 | #define SD_BRIGHTNESS 0 |
| 82 | { |
| 83 | { |
| 84 | .id = V4L2_CID_BRIGHTNESS, |
| 85 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 86 | .name = "Brightness", |
| 87 | .minimum = PAC207_BRIGHTNESS_MIN, |
| 88 | .maximum = PAC207_BRIGHTNESS_MAX, |
| 89 | .step = 1, |
| 90 | .default_value = PAC207_BRIGHTNESS_DEFAULT, |
| 91 | .flags = 0, |
| 92 | }, |
| 93 | .set = sd_setbrightness, |
| 94 | .get = sd_getbrightness, |
| 95 | }, |
| 96 | #define SD_EXPOSURE 1 |
| 97 | { |
| 98 | { |
| 99 | .id = V4L2_CID_EXPOSURE, |
| 100 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 101 | .name = "exposure", |
| 102 | .minimum = PAC207_EXPOSURE_MIN, |
| 103 | .maximum = PAC207_EXPOSURE_MAX, |
| 104 | .step = 1, |
| 105 | .default_value = PAC207_EXPOSURE_DEFAULT, |
| 106 | .flags = 0, |
| 107 | }, |
| 108 | .set = sd_setexposure, |
| 109 | .get = sd_getexposure, |
| 110 | }, |
| 111 | #define SD_AUTOGAIN 2 |
| 112 | { |
| 113 | { |
| 114 | .id = V4L2_CID_AUTOGAIN, |
| 115 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 116 | .name = "Auto Gain", |
| 117 | .minimum = 0, |
| 118 | .maximum = 1, |
| 119 | .step = 1, |
Jean-Francois Moine | 66e4124 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 120 | #define AUTOGAIN_DEF 1 |
| 121 | .default_value = AUTOGAIN_DEF, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 122 | .flags = 0, |
| 123 | }, |
| 124 | .set = sd_setautogain, |
| 125 | .get = sd_getautogain, |
| 126 | }, |
| 127 | #define SD_GAIN 3 |
| 128 | { |
| 129 | { |
| 130 | .id = V4L2_CID_GAIN, |
| 131 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 132 | .name = "gain", |
| 133 | .minimum = PAC207_GAIN_MIN, |
| 134 | .maximum = PAC207_GAIN_MAX, |
| 135 | .step = 1, |
| 136 | .default_value = PAC207_GAIN_DEFAULT, |
| 137 | .flags = 0, |
| 138 | }, |
| 139 | .set = sd_setgain, |
| 140 | .get = sd_getgain, |
| 141 | }, |
| 142 | }; |
| 143 | |
Jean-Francois Moine | cc611b8 | 2008-12-29 07:49:41 -0300 | [diff] [blame] | 144 | static const struct v4l2_pix_format sif_mode[] = { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 145 | {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE, |
| 146 | .bytesperline = 176, |
| 147 | .sizeimage = (176 + 2) * 144, |
| 148 | /* uncompressed, add 2 bytes / line for line header */ |
| 149 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 150 | .priv = 1}, |
| 151 | {352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE, |
| 152 | .bytesperline = 352, |
Hans de Goede | 80544d3c | 2008-07-06 06:40:55 -0300 | [diff] [blame] | 153 | /* compressed, but only when needed (not compressed |
| 154 | when the framerate is low) */ |
| 155 | .sizeimage = (352 + 2) * 288, |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 156 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 157 | .priv = 0}, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static const __u8 pac207_sensor_init[][8] = { |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 161 | {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84}, |
| 162 | {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30}, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 163 | {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00}, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 164 | {0x32, 0x00, 0x96, 0x00, 0xA2, 0x02, 0xaf, 0x00}, |
| 165 | }; |
| 166 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 167 | static int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 168 | const u8 *buffer, u16 length) |
| 169 | { |
| 170 | struct usb_device *udev = gspca_dev->dev; |
| 171 | int err; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 172 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 173 | memcpy(gspca_dev->usb_buf, buffer, length); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 174 | |
| 175 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01, |
| 176 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 177 | 0x00, index, |
| 178 | gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 179 | if (err < 0) |
| 180 | PDEBUG(D_ERR, |
| 181 | "Failed to write registers to index 0x%04X, error %d)", |
| 182 | index, err); |
| 183 | |
| 184 | return err; |
| 185 | } |
| 186 | |
| 187 | |
Adrian Bunk | 903e10a | 2008-07-22 02:35:05 -0300 | [diff] [blame] | 188 | static int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 189 | { |
| 190 | struct usb_device *udev = gspca_dev->dev; |
| 191 | int err; |
| 192 | |
| 193 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, |
| 194 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 195 | value, index, NULL, 0, PAC207_CTRL_TIMEOUT); |
| 196 | if (err) |
| 197 | PDEBUG(D_ERR, "Failed to write a register (index 0x%04X," |
| 198 | " value 0x%02X, error %d)", index, value, err); |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | |
Adrian Bunk | 903e10a | 2008-07-22 02:35:05 -0300 | [diff] [blame] | 203 | static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 204 | { |
| 205 | struct usb_device *udev = gspca_dev->dev; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 206 | int res; |
| 207 | |
| 208 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, |
| 209 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 210 | 0x00, index, |
| 211 | gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 212 | if (res < 0) { |
| 213 | PDEBUG(D_ERR, |
| 214 | "Failed to read a register (index 0x%04X, error %d)", |
| 215 | index, res); |
| 216 | return res; |
| 217 | } |
| 218 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 219 | return gspca_dev->usb_buf[0]; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 220 | } |
| 221 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 222 | /* this function is called at probe time */ |
| 223 | static int sd_config(struct gspca_dev *gspca_dev, |
| 224 | const struct usb_device_id *id) |
| 225 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 226 | struct sd *sd = (struct sd *) gspca_dev; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 227 | struct cam *cam; |
| 228 | u8 idreg[2]; |
| 229 | |
| 230 | idreg[0] = pac207_read_reg(gspca_dev, 0x0000); |
| 231 | idreg[1] = pac207_read_reg(gspca_dev, 0x0001); |
| 232 | idreg[0] = ((idreg[0] & 0x0F) << 4) | ((idreg[1] & 0xf0) >> 4); |
| 233 | idreg[1] = idreg[1] & 0x0f; |
| 234 | PDEBUG(D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X", |
| 235 | idreg[0], idreg[1]); |
| 236 | |
| 237 | if (idreg[0] != 0x27) { |
| 238 | PDEBUG(D_PROBE, "Error invalid sensor ID!"); |
| 239 | return -ENODEV; |
| 240 | } |
| 241 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 242 | PDEBUG(D_PROBE, |
| 243 | "Pixart PAC207BCA Image Processor and Control Chip detected" |
| 244 | " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); |
| 245 | |
| 246 | cam = &gspca_dev->cam; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 247 | cam->cam_mode = sif_mode; |
| 248 | cam->nmodes = ARRAY_SIZE(sif_mode); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 249 | sd->brightness = PAC207_BRIGHTNESS_DEFAULT; |
| 250 | sd->exposure = PAC207_EXPOSURE_DEFAULT; |
| 251 | sd->gain = PAC207_GAIN_DEFAULT; |
Jean-Francois Moine | 66e4124 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 252 | sd->autogain = AUTOGAIN_DEF; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 257 | /* this function is called at probe and resume time */ |
| 258 | static int sd_init(struct gspca_dev *gspca_dev) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 259 | { |
Jean-Francois Moine | 66e4124 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 260 | pac207_write_reg(gspca_dev, 0x41, 0x00); |
| 261 | /* Bit_0=Image Format, |
| 262 | * Bit_1=LED, |
| 263 | * Bit_2=Compression test mode enable */ |
| 264 | pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */ |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 265 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* -- start the camera -- */ |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 270 | static int sd_start(struct gspca_dev *gspca_dev) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 271 | { |
| 272 | struct sd *sd = (struct sd *) gspca_dev; |
| 273 | __u8 mode; |
| 274 | |
| 275 | pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */ |
| 276 | pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8); |
| 277 | pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8); |
| 278 | pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8); |
Hans de Goede | c529e55 | 2009-07-29 08:02:50 -0300 | [diff] [blame] | 279 | pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 280 | |
| 281 | /* Compression Balance */ |
| 282 | if (gspca_dev->width == 176) |
| 283 | pac207_write_reg(gspca_dev, 0x4a, 0xff); |
| 284 | else |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 285 | pac207_write_reg(gspca_dev, 0x4a, 0x30); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 286 | pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */ |
| 287 | pac207_write_reg(gspca_dev, 0x08, sd->brightness); |
| 288 | |
| 289 | /* PGA global gain (Bit 4-0) */ |
| 290 | pac207_write_reg(gspca_dev, 0x0e, sd->gain); |
| 291 | pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */ |
| 292 | |
| 293 | mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */ |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 294 | if (gspca_dev->width == 176) { /* 176x144 */ |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 295 | mode |= 0x01; |
| 296 | PDEBUG(D_STREAM, "pac207_start mode 176x144"); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 297 | } else { /* 352x288 */ |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 298 | PDEBUG(D_STREAM, "pac207_start mode 352x288"); |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 299 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 300 | pac207_write_reg(gspca_dev, 0x41, mode); |
| 301 | |
| 302 | pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */ |
| 303 | pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */ |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 304 | msleep(10); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 305 | pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */ |
| 306 | |
| 307 | sd->sof_read = 0; |
| 308 | sd->autogain_ignore_frames = 0; |
| 309 | atomic_set(&sd->avg_lum, -1); |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 310 | return 0; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void sd_stopN(struct gspca_dev *gspca_dev) |
| 314 | { |
| 315 | pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */ |
| 316 | pac207_write_reg(gspca_dev, 0x41, 0x00); /* Turn of LED */ |
| 317 | pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */ |
| 318 | } |
| 319 | |
Hans de Goede | 8a5b2e9 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 320 | /* Include pac common sof detection functions */ |
| 321 | #include "pac_common.h" |
| 322 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 323 | static void pac207_do_auto_gain(struct gspca_dev *gspca_dev) |
| 324 | { |
| 325 | struct sd *sd = (struct sd *) gspca_dev; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 326 | int avg_lum = atomic_read(&sd->avg_lum); |
| 327 | |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 328 | if (avg_lum == -1) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 329 | return; |
| 330 | |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 331 | if (sd->autogain_ignore_frames > 0) |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 332 | sd->autogain_ignore_frames--; |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 333 | else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, |
Hans de Goede | 0e4a909 | 2009-07-29 08:03:17 -0300 | [diff] [blame] | 334 | 100, PAC207_AUTOGAIN_DEADZONE, |
Hans de Goede | dcef323 | 2008-07-10 10:40:53 -0300 | [diff] [blame] | 335 | PAC207_GAIN_KNEE, PAC207_EXPOSURE_KNEE)) |
Hans de Goede | 8a5b2e9 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 336 | sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 337 | } |
| 338 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 339 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 340 | u8 *data, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 341 | int len) |
| 342 | { |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 343 | struct sd *sd = (struct sd *) gspca_dev; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 344 | unsigned char *sof; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 345 | |
Marton Nemeth | a6b69e4 | 2009-11-02 08:05:51 -0300 | [diff] [blame] | 346 | sof = pac_find_sof(&sd->sof_read, data, len); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 347 | if (sof) { |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 348 | int n; |
| 349 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 350 | /* finish decoding current frame */ |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 351 | n = sof - data; |
Hans de Goede | 327c4ab | 2008-09-03 17:12:14 -0300 | [diff] [blame] | 352 | if (n > sizeof pac_sof_marker) |
| 353 | n -= sizeof pac_sof_marker; |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 354 | else |
| 355 | n = 0; |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 356 | gspca_frame_add(gspca_dev, LAST_PACKET, |
| 357 | data, n); |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 358 | sd->header_read = 0; |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 359 | gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 360 | len -= sof - data; |
| 361 | data = sof; |
| 362 | } |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 363 | if (sd->header_read < 11) { |
| 364 | int needed; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 365 | |
Hans de Goede | ab8f12c | 2008-07-04 18:29:32 -0300 | [diff] [blame] | 366 | /* get average lumination from frame header (byte 5) */ |
| 367 | if (sd->header_read < 5) { |
| 368 | needed = 5 - sd->header_read; |
| 369 | if (len >= needed) |
| 370 | atomic_set(&sd->avg_lum, data[needed - 1]); |
| 371 | } |
| 372 | /* skip the rest of the header */ |
| 373 | needed = 11 - sd->header_read; |
| 374 | if (len <= needed) { |
| 375 | sd->header_read += len; |
| 376 | return; |
| 377 | } |
| 378 | data += needed; |
| 379 | len -= needed; |
| 380 | sd->header_read = 11; |
| 381 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 382 | |
Jean-Francois Moine | 76dd272 | 2009-11-13 09:21:03 -0300 | [diff] [blame] | 383 | gspca_frame_add(gspca_dev, INTER_PACKET, data, len); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static void setbrightness(struct gspca_dev *gspca_dev) |
| 387 | { |
| 388 | struct sd *sd = (struct sd *) gspca_dev; |
| 389 | |
| 390 | pac207_write_reg(gspca_dev, 0x08, sd->brightness); |
| 391 | pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */ |
| 392 | pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */ |
| 393 | } |
| 394 | |
| 395 | static void setexposure(struct gspca_dev *gspca_dev) |
| 396 | { |
| 397 | struct sd *sd = (struct sd *) gspca_dev; |
| 398 | |
| 399 | pac207_write_reg(gspca_dev, 0x02, sd->exposure); |
| 400 | pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */ |
| 401 | pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */ |
| 402 | } |
| 403 | |
| 404 | static void setgain(struct gspca_dev *gspca_dev) |
| 405 | { |
| 406 | struct sd *sd = (struct sd *) gspca_dev; |
| 407 | |
| 408 | pac207_write_reg(gspca_dev, 0x0e, sd->gain); |
| 409 | pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */ |
| 410 | pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */ |
| 411 | } |
| 412 | |
| 413 | static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) |
| 414 | { |
| 415 | struct sd *sd = (struct sd *) gspca_dev; |
| 416 | |
| 417 | sd->brightness = val; |
| 418 | if (gspca_dev->streaming) |
| 419 | setbrightness(gspca_dev); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) |
| 424 | { |
| 425 | struct sd *sd = (struct sd *) gspca_dev; |
| 426 | |
| 427 | *val = sd->brightness; |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val) |
| 432 | { |
| 433 | struct sd *sd = (struct sd *) gspca_dev; |
| 434 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 435 | sd->exposure = val; |
| 436 | if (gspca_dev->streaming) |
| 437 | setexposure(gspca_dev); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val) |
| 442 | { |
| 443 | struct sd *sd = (struct sd *) gspca_dev; |
| 444 | |
| 445 | *val = sd->exposure; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val) |
| 450 | { |
| 451 | struct sd *sd = (struct sd *) gspca_dev; |
| 452 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 453 | sd->gain = val; |
| 454 | if (gspca_dev->streaming) |
| 455 | setgain(gspca_dev); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val) |
| 460 | { |
| 461 | struct sd *sd = (struct sd *) gspca_dev; |
| 462 | |
| 463 | *val = sd->gain; |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val) |
| 468 | { |
| 469 | struct sd *sd = (struct sd *) gspca_dev; |
| 470 | |
| 471 | sd->autogain = val; |
| 472 | /* when switching to autogain set defaults to make sure |
| 473 | we are on a valid point of the autogain gain / |
| 474 | exposure knee graph, and give this change time to |
| 475 | take effect before doing autogain. */ |
| 476 | if (sd->autogain) { |
| 477 | sd->exposure = PAC207_EXPOSURE_DEFAULT; |
| 478 | sd->gain = PAC207_GAIN_DEFAULT; |
| 479 | if (gspca_dev->streaming) { |
| 480 | sd->autogain_ignore_frames = |
Hans de Goede | 8a5b2e9 | 2008-09-03 17:12:17 -0300 | [diff] [blame] | 481 | PAC_AUTOGAIN_IGNORE_FRAMES; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 482 | setexposure(gspca_dev); |
| 483 | setgain(gspca_dev); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val) |
| 491 | { |
| 492 | struct sd *sd = (struct sd *) gspca_dev; |
| 493 | |
| 494 | *val = sd->autogain; |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /* sub-driver description */ |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 499 | static const struct sd_desc sd_desc = { |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 500 | .name = MODULE_NAME, |
| 501 | .ctrls = sd_ctrls, |
| 502 | .nctrls = ARRAY_SIZE(sd_ctrls), |
| 503 | .config = sd_config, |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 504 | .init = sd_init, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 505 | .start = sd_start, |
| 506 | .stopN = sd_stopN, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 507 | .dq_callback = pac207_do_auto_gain, |
| 508 | .pkt_scan = sd_pkt_scan, |
| 509 | }; |
| 510 | |
| 511 | /* -- module initialisation -- */ |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 512 | static const __devinitdata struct usb_device_id device_table[] = { |
Jean-Francois Moine | 9d64fdb | 2008-07-25 08:53:03 -0300 | [diff] [blame] | 513 | {USB_DEVICE(0x041e, 0x4028)}, |
| 514 | {USB_DEVICE(0x093a, 0x2460)}, |
Hans de Goede | 8794589 | 2008-12-20 14:30:58 -0300 | [diff] [blame] | 515 | {USB_DEVICE(0x093a, 0x2461)}, |
Jean-Francois Moine | 9d64fdb | 2008-07-25 08:53:03 -0300 | [diff] [blame] | 516 | {USB_DEVICE(0x093a, 0x2463)}, |
| 517 | {USB_DEVICE(0x093a, 0x2464)}, |
| 518 | {USB_DEVICE(0x093a, 0x2468)}, |
| 519 | {USB_DEVICE(0x093a, 0x2470)}, |
| 520 | {USB_DEVICE(0x093a, 0x2471)}, |
| 521 | {USB_DEVICE(0x093a, 0x2472)}, |
Lierdakil | db91235 | 2009-01-25 14:37:26 -0300 | [diff] [blame] | 522 | {USB_DEVICE(0x093a, 0x2474)}, |
Jean-Francois Moine | d654dca | 2008-09-06 04:18:48 -0300 | [diff] [blame] | 523 | {USB_DEVICE(0x093a, 0x2476)}, |
Jean-Francois Moine | 9171187 | 2008-10-28 08:00:23 -0300 | [diff] [blame] | 524 | {USB_DEVICE(0x145f, 0x013a)}, |
Jean-Francois Moine | 9d64fdb | 2008-07-25 08:53:03 -0300 | [diff] [blame] | 525 | {USB_DEVICE(0x2001, 0xf115)}, |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 526 | {} |
| 527 | }; |
| 528 | MODULE_DEVICE_TABLE(usb, device_table); |
| 529 | |
| 530 | /* -- device connect -- */ |
| 531 | static int sd_probe(struct usb_interface *intf, |
| 532 | const struct usb_device_id *id) |
| 533 | { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 534 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), |
| 535 | THIS_MODULE); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static struct usb_driver sd_driver = { |
| 539 | .name = MODULE_NAME, |
| 540 | .id_table = device_table, |
| 541 | .probe = sd_probe, |
| 542 | .disconnect = gspca_disconnect, |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 543 | #ifdef CONFIG_PM |
| 544 | .suspend = gspca_suspend, |
| 545 | .resume = gspca_resume, |
| 546 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | /* -- module insert / remove -- */ |
| 550 | static int __init sd_mod_init(void) |
| 551 | { |
Alexey Klimov | f69e952 | 2009-01-01 13:02:07 -0300 | [diff] [blame] | 552 | int ret; |
| 553 | ret = usb_register(&sd_driver); |
| 554 | if (ret < 0) |
Alexey Klimov | e6b1484 | 2009-01-01 13:04:58 -0300 | [diff] [blame] | 555 | return ret; |
Adrian Bunk | 903e10a | 2008-07-22 02:35:05 -0300 | [diff] [blame] | 556 | PDEBUG(D_PROBE, "registered"); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | static void __exit sd_mod_exit(void) |
| 560 | { |
| 561 | usb_deregister(&sd_driver); |
| 562 | PDEBUG(D_PROBE, "deregistered"); |
| 563 | } |
| 564 | |
| 565 | module_init(sd_mod_init); |
| 566 | module_exit(sd_mod_exit); |