Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / drivers / media / video / gspca / pac207.c
blob81739a2f205e8e020ac789c984d60a143603e3ce
1 /*
2 * Pixart PAC207BCA library
4 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6 * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
8 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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.
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.
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
26 #define MODULE_NAME "pac207"
28 #include <linux/input.h>
29 #include "gspca.h"
31 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
32 MODULE_DESCRIPTION("Pixart PAC207");
33 MODULE_LICENSE("GPL");
35 #define PAC207_CTRL_TIMEOUT 100 /* ms */
37 #define PAC207_BRIGHTNESS_MIN 0
38 #define PAC207_BRIGHTNESS_MAX 255
39 #define PAC207_BRIGHTNESS_DEFAULT 46
41 #define PAC207_EXPOSURE_MIN 3
42 #define PAC207_EXPOSURE_MAX 26
43 #define PAC207_EXPOSURE_DEFAULT 5 /* power on default: 3 */
44 #define PAC207_EXPOSURE_KNEE 8 /* 4 = 30 fps, 11 = 8, 15 = 6 */
46 #define PAC207_GAIN_MIN 0
47 #define PAC207_GAIN_MAX 31
48 #define PAC207_GAIN_DEFAULT 9 /* power on default: 9 */
49 #define PAC207_GAIN_KNEE 31
51 #define PAC207_AUTOGAIN_DEADZONE 30
53 /* specific webcam descriptor */
54 struct sd {
55 struct gspca_dev gspca_dev; /* !! must be the first item */
57 u8 mode;
59 u8 brightness;
60 u8 exposure;
61 u8 autogain;
62 u8 gain;
64 u8 sof_read;
65 u8 header_read;
66 u8 autogain_ignore_frames;
68 atomic_t avg_lum;
71 /* V4L2 controls supported by the driver */
72 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
73 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
74 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
75 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
76 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
77 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
78 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
79 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
81 static const struct ctrl sd_ctrls[] = {
82 #define SD_BRIGHTNESS 0
85 .id = V4L2_CID_BRIGHTNESS,
86 .type = V4L2_CTRL_TYPE_INTEGER,
87 .name = "Brightness",
88 .minimum = PAC207_BRIGHTNESS_MIN,
89 .maximum = PAC207_BRIGHTNESS_MAX,
90 .step = 1,
91 .default_value = PAC207_BRIGHTNESS_DEFAULT,
92 .flags = 0,
94 .set = sd_setbrightness,
95 .get = sd_getbrightness,
97 #define SD_EXPOSURE 1
100 .id = V4L2_CID_EXPOSURE,
101 .type = V4L2_CTRL_TYPE_INTEGER,
102 .name = "Exposure",
103 .minimum = PAC207_EXPOSURE_MIN,
104 .maximum = PAC207_EXPOSURE_MAX,
105 .step = 1,
106 .default_value = PAC207_EXPOSURE_DEFAULT,
107 .flags = 0,
109 .set = sd_setexposure,
110 .get = sd_getexposure,
112 #define SD_AUTOGAIN 2
115 .id = V4L2_CID_AUTOGAIN,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 .name = "Auto Gain",
118 .minimum = 0,
119 .maximum = 1,
120 .step = 1,
121 #define AUTOGAIN_DEF 1
122 .default_value = AUTOGAIN_DEF,
123 .flags = 0,
125 .set = sd_setautogain,
126 .get = sd_getautogain,
128 #define SD_GAIN 3
131 .id = V4L2_CID_GAIN,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Gain",
134 .minimum = PAC207_GAIN_MIN,
135 .maximum = PAC207_GAIN_MAX,
136 .step = 1,
137 .default_value = PAC207_GAIN_DEFAULT,
138 .flags = 0,
140 .set = sd_setgain,
141 .get = sd_getgain,
145 static const struct v4l2_pix_format sif_mode[] = {
146 {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
147 .bytesperline = 176,
148 .sizeimage = (176 + 2) * 144,
149 /* uncompressed, add 2 bytes / line for line header */
150 .colorspace = V4L2_COLORSPACE_SRGB,
151 .priv = 1},
152 {352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
153 .bytesperline = 352,
154 /* compressed, but only when needed (not compressed
155 when the framerate is low) */
156 .sizeimage = (352 + 2) * 288,
157 .colorspace = V4L2_COLORSPACE_SRGB,
158 .priv = 0},
161 static const __u8 pac207_sensor_init[][8] = {
162 {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
163 {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
164 {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
165 {0x32, 0x00, 0x96, 0x00, 0xa2, 0x02, 0xaf, 0x00},
168 static int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
169 const u8 *buffer, u16 length)
171 struct usb_device *udev = gspca_dev->dev;
172 int err;
174 memcpy(gspca_dev->usb_buf, buffer, length);
176 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
177 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
178 0x00, index,
179 gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT);
180 if (err < 0)
181 err("Failed to write registers to index 0x%04X, error %d)",
182 index, err);
184 return err;
188 static int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
190 struct usb_device *udev = gspca_dev->dev;
191 int err;
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 err("Failed to write a register (index 0x%04X,"
198 " value 0x%02X, error %d)", index, value, err);
200 return err;
203 static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
205 struct usb_device *udev = gspca_dev->dev;
206 int res;
208 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
209 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
210 0x00, index,
211 gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT);
212 if (res < 0) {
213 err("Failed to read a register (index 0x%04X, error %d)",
214 index, res);
215 return res;
218 return gspca_dev->usb_buf[0];
221 /* this function is called at probe time */
222 static int sd_config(struct gspca_dev *gspca_dev,
223 const struct usb_device_id *id)
225 struct sd *sd = (struct sd *) gspca_dev;
226 struct cam *cam;
227 u8 idreg[2];
229 idreg[0] = pac207_read_reg(gspca_dev, 0x0000);
230 idreg[1] = pac207_read_reg(gspca_dev, 0x0001);
231 idreg[0] = ((idreg[0] & 0x0f) << 4) | ((idreg[1] & 0xf0) >> 4);
232 idreg[1] = idreg[1] & 0x0f;
233 PDEBUG(D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X",
234 idreg[0], idreg[1]);
236 if (idreg[0] != 0x27) {
237 PDEBUG(D_PROBE, "Error invalid sensor ID!");
238 return -ENODEV;
241 PDEBUG(D_PROBE,
242 "Pixart PAC207BCA Image Processor and Control Chip detected"
243 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
245 cam = &gspca_dev->cam;
246 cam->cam_mode = sif_mode;
247 cam->nmodes = ARRAY_SIZE(sif_mode);
248 sd->brightness = PAC207_BRIGHTNESS_DEFAULT;
249 sd->exposure = PAC207_EXPOSURE_DEFAULT;
250 sd->gain = PAC207_GAIN_DEFAULT;
251 sd->autogain = AUTOGAIN_DEF;
253 return 0;
256 /* this function is called at probe and resume time */
257 static int sd_init(struct gspca_dev *gspca_dev)
259 pac207_write_reg(gspca_dev, 0x41, 0x00);
260 /* Bit_0=Image Format,
261 * Bit_1=LED,
262 * Bit_2=Compression test mode enable */
263 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
265 return 0;
268 /* -- start the camera -- */
269 static int sd_start(struct gspca_dev *gspca_dev)
271 struct sd *sd = (struct sd *) gspca_dev;
272 __u8 mode;
274 pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */
275 pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
276 pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
277 pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
278 pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8);
280 /* Compression Balance */
281 if (gspca_dev->width == 176)
282 pac207_write_reg(gspca_dev, 0x4a, 0xff);
283 else
284 pac207_write_reg(gspca_dev, 0x4a, 0x30);
285 pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
286 pac207_write_reg(gspca_dev, 0x08, sd->brightness);
288 /* PGA global gain (Bit 4-0) */
289 pac207_write_reg(gspca_dev, 0x0e, sd->gain);
290 pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */
292 mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */
293 if (gspca_dev->width == 176) { /* 176x144 */
294 mode |= 0x01;
295 PDEBUG(D_STREAM, "pac207_start mode 176x144");
296 } else { /* 352x288 */
297 PDEBUG(D_STREAM, "pac207_start mode 352x288");
299 pac207_write_reg(gspca_dev, 0x41, mode);
301 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
302 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
303 msleep(10);
304 pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */
306 sd->sof_read = 0;
307 sd->autogain_ignore_frames = 0;
308 atomic_set(&sd->avg_lum, -1);
309 return 0;
312 static void sd_stopN(struct gspca_dev *gspca_dev)
314 pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */
315 pac207_write_reg(gspca_dev, 0x41, 0x00); /* Turn of LED */
316 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
319 /* Include pac common sof detection functions */
320 #include "pac_common.h"
322 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
324 struct sd *sd = (struct sd *) gspca_dev;
325 int avg_lum = atomic_read(&sd->avg_lum);
327 if (avg_lum == -1)
328 return;
330 if (sd->autogain_ignore_frames > 0)
331 sd->autogain_ignore_frames--;
332 else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
333 100, PAC207_AUTOGAIN_DEADZONE,
334 PAC207_GAIN_KNEE, PAC207_EXPOSURE_KNEE))
335 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
338 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
339 u8 *data,
340 int len)
342 struct sd *sd = (struct sd *) gspca_dev;
343 unsigned char *sof;
345 sof = pac_find_sof(&sd->sof_read, data, len);
346 if (sof) {
347 int n;
349 /* finish decoding current frame */
350 n = sof - data;
351 if (n > sizeof pac_sof_marker)
352 n -= sizeof pac_sof_marker;
353 else
354 n = 0;
355 gspca_frame_add(gspca_dev, LAST_PACKET,
356 data, n);
357 sd->header_read = 0;
358 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
359 len -= sof - data;
360 data = sof;
362 if (sd->header_read < 11) {
363 int needed;
365 /* get average lumination from frame header (byte 5) */
366 if (sd->header_read < 5) {
367 needed = 5 - sd->header_read;
368 if (len >= needed)
369 atomic_set(&sd->avg_lum, data[needed - 1]);
371 /* skip the rest of the header */
372 needed = 11 - sd->header_read;
373 if (len <= needed) {
374 sd->header_read += len;
375 return;
377 data += needed;
378 len -= needed;
379 sd->header_read = 11;
382 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
385 static void setbrightness(struct gspca_dev *gspca_dev)
387 struct sd *sd = (struct sd *) gspca_dev;
389 pac207_write_reg(gspca_dev, 0x08, sd->brightness);
390 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
391 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
394 static void setexposure(struct gspca_dev *gspca_dev)
396 struct sd *sd = (struct sd *) gspca_dev;
398 pac207_write_reg(gspca_dev, 0x02, sd->exposure);
399 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
400 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
403 static void setgain(struct gspca_dev *gspca_dev)
405 struct sd *sd = (struct sd *) gspca_dev;
407 pac207_write_reg(gspca_dev, 0x0e, sd->gain);
408 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
409 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
412 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
414 struct sd *sd = (struct sd *) gspca_dev;
416 sd->brightness = val;
417 if (gspca_dev->streaming)
418 setbrightness(gspca_dev);
419 return 0;
422 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
424 struct sd *sd = (struct sd *) gspca_dev;
426 *val = sd->brightness;
427 return 0;
430 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
432 struct sd *sd = (struct sd *) gspca_dev;
434 sd->exposure = val;
435 if (gspca_dev->streaming)
436 setexposure(gspca_dev);
437 return 0;
440 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
442 struct sd *sd = (struct sd *) gspca_dev;
444 *val = sd->exposure;
445 return 0;
448 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
450 struct sd *sd = (struct sd *) gspca_dev;
452 sd->gain = val;
453 if (gspca_dev->streaming)
454 setgain(gspca_dev);
455 return 0;
458 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
460 struct sd *sd = (struct sd *) gspca_dev;
462 *val = sd->gain;
463 return 0;
466 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
468 struct sd *sd = (struct sd *) gspca_dev;
470 sd->autogain = val;
471 /* when switching to autogain set defaults to make sure
472 we are on a valid point of the autogain gain /
473 exposure knee graph, and give this change time to
474 take effect before doing autogain. */
475 if (sd->autogain) {
476 sd->exposure = PAC207_EXPOSURE_DEFAULT;
477 sd->gain = PAC207_GAIN_DEFAULT;
478 if (gspca_dev->streaming) {
479 sd->autogain_ignore_frames =
480 PAC_AUTOGAIN_IGNORE_FRAMES;
481 setexposure(gspca_dev);
482 setgain(gspca_dev);
486 return 0;
489 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
491 struct sd *sd = (struct sd *) gspca_dev;
493 *val = sd->autogain;
494 return 0;
497 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
498 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
499 u8 *data, /* interrupt packet data */
500 int len) /* interrput packet length */
502 int ret = -EINVAL;
504 if (len == 2 && data[0] == 0x5a && data[1] == 0x5a) {
505 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
506 input_sync(gspca_dev->input_dev);
507 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
508 input_sync(gspca_dev->input_dev);
509 ret = 0;
512 return ret;
514 #endif
516 /* sub-driver description */
517 static const struct sd_desc sd_desc = {
518 .name = MODULE_NAME,
519 .ctrls = sd_ctrls,
520 .nctrls = ARRAY_SIZE(sd_ctrls),
521 .config = sd_config,
522 .init = sd_init,
523 .start = sd_start,
524 .stopN = sd_stopN,
525 .dq_callback = pac207_do_auto_gain,
526 .pkt_scan = sd_pkt_scan,
527 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
528 .int_pkt_scan = sd_int_pkt_scan,
529 #endif
532 /* -- module initialisation -- */
533 static const struct usb_device_id device_table[] = {
534 {USB_DEVICE(0x041e, 0x4028)},
535 {USB_DEVICE(0x093a, 0x2460)},
536 {USB_DEVICE(0x093a, 0x2461)},
537 {USB_DEVICE(0x093a, 0x2463)},
538 {USB_DEVICE(0x093a, 0x2464)},
539 {USB_DEVICE(0x093a, 0x2468)},
540 {USB_DEVICE(0x093a, 0x2470)},
541 {USB_DEVICE(0x093a, 0x2471)},
542 {USB_DEVICE(0x093a, 0x2472)},
543 {USB_DEVICE(0x093a, 0x2474)},
544 {USB_DEVICE(0x093a, 0x2476)},
545 {USB_DEVICE(0x145f, 0x013a)},
546 {USB_DEVICE(0x2001, 0xf115)},
549 MODULE_DEVICE_TABLE(usb, device_table);
551 /* -- device connect -- */
552 static int sd_probe(struct usb_interface *intf,
553 const struct usb_device_id *id)
555 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
556 THIS_MODULE);
559 static struct usb_driver sd_driver = {
560 .name = MODULE_NAME,
561 .id_table = device_table,
562 .probe = sd_probe,
563 .disconnect = gspca_disconnect,
564 #ifdef CONFIG_PM
565 .suspend = gspca_suspend,
566 .resume = gspca_resume,
567 #endif
570 /* -- module insert / remove -- */
571 static int __init sd_mod_init(void)
573 return usb_register(&sd_driver);
575 static void __exit sd_mod_exit(void)
577 usb_deregister(&sd_driver);
580 module_init(sd_mod_init);
581 module_exit(sd_mod_exit);