V4L/DVB: gspca - pac7302: Add support for camera button.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / gspca / pac7302.c
blob5bf51786b443c6bff50b834c8d58c79e2ea393ec
1 /*
2 * Pixart PAC7302 library
3 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
5 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7 * Separated from Pixart PAC7311 library by Márton Németh <nm127@freemail.hu>
8 * Camera button input handling by Márton Németh <nm127@freemail.hu>
9 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* Some documentation about various registers as determined by trial and error.
27 When the register addresses differ between the 7202 and the 7311 the 2
28 different addresses are written as 7302addr/7311addr, when one of the 2
29 addresses is a - sign that register description is not valid for the
30 matching IC.
32 Register page 1:
34 Address Description
35 -/0x08 Unknown compressor related, must always be 8 except when not
36 in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
37 -/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
38 bits 345 seem to toggle per color gains on/off (inverted)
39 0x78 Global control, bit 6 controls the LED (inverted)
40 -/0x80 JPEG compression ratio ? Best not touched
42 Register page 3/4:
44 Address Description
45 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
46 the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
47 -/0x0f Master gain 1-245, low value = high gain
48 0x10/- Master gain 0-31
49 -/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
50 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
51 -/0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
52 completely disable the analog amplification block. Set to 0x68
53 for max gain, 0x14 for minimal gain.
55 The registers are accessed in the following functions:
57 Page | Register | Function
58 -----+------------+---------------------------------------------------
59 0 | 0x0f..0x20 | setcolors()
60 0 | 0xa2..0xab | setbrightcont()
61 0 | 0xc5 | setredbalance()
62 0 | 0xc6 | setwhitebalance()
63 0 | 0xc7 | setbluebalance()
64 0 | 0xdc | setbrightcont(), setcolors()
65 3 | 0x02 | setexposure()
66 3 | 0x10 | setgain()
67 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
68 3 | 0x21 | sethvflip()
71 #define MODULE_NAME "pac7302"
73 #include <linux/input.h>
74 #include <media/v4l2-chip-ident.h>
75 #include "gspca.h"
77 MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
78 MODULE_DESCRIPTION("Pixart PAC7302");
79 MODULE_LICENSE("GPL");
81 /* specific webcam descriptor for pac7302 */
82 struct sd {
83 struct gspca_dev gspca_dev; /* !! must be the first item */
85 unsigned char brightness;
86 unsigned char contrast;
87 unsigned char colors;
88 unsigned char white_balance;
89 unsigned char red_balance;
90 unsigned char blue_balance;
91 unsigned char gain;
92 unsigned char exposure;
93 unsigned char autogain;
94 __u8 hflip;
95 __u8 vflip;
96 u8 flags;
97 #define FL_HFLIP 0x01 /* mirrored by default */
98 #define FL_VFLIP 0x02 /* vertical flipped by default */
100 u8 sof_read;
101 u8 autogain_ignore_frames;
103 atomic_t avg_lum;
106 /* V4L2 controls supported by the driver */
107 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
108 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
109 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
110 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
111 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
112 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
113 static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val);
114 static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val);
115 static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val);
116 static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val);
117 static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val);
118 static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val);
119 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
120 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
121 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
122 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
123 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
124 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
125 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
126 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
127 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
128 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
130 static const struct ctrl sd_ctrls[] = {
131 /* This control is pac7302 only */
134 .id = V4L2_CID_BRIGHTNESS,
135 .type = V4L2_CTRL_TYPE_INTEGER,
136 .name = "Brightness",
137 .minimum = 0,
138 #define BRIGHTNESS_MAX 0x20
139 .maximum = BRIGHTNESS_MAX,
140 .step = 1,
141 #define BRIGHTNESS_DEF 0x10
142 .default_value = BRIGHTNESS_DEF,
144 .set = sd_setbrightness,
145 .get = sd_getbrightness,
147 /* This control is for both the 7302 and the 7311 */
150 .id = V4L2_CID_CONTRAST,
151 .type = V4L2_CTRL_TYPE_INTEGER,
152 .name = "Contrast",
153 .minimum = 0,
154 #define CONTRAST_MAX 255
155 .maximum = CONTRAST_MAX,
156 .step = 1,
157 #define CONTRAST_DEF 127
158 .default_value = CONTRAST_DEF,
160 .set = sd_setcontrast,
161 .get = sd_getcontrast,
163 /* This control is pac7302 only */
166 .id = V4L2_CID_SATURATION,
167 .type = V4L2_CTRL_TYPE_INTEGER,
168 .name = "Saturation",
169 .minimum = 0,
170 #define COLOR_MAX 255
171 .maximum = COLOR_MAX,
172 .step = 1,
173 #define COLOR_DEF 127
174 .default_value = COLOR_DEF,
176 .set = sd_setcolors,
177 .get = sd_getcolors,
181 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
182 .type = V4L2_CTRL_TYPE_INTEGER,
183 .name = "White Balance",
184 .minimum = 0,
185 .maximum = 255,
186 .step = 1,
187 #define WHITEBALANCE_DEF 4
188 .default_value = WHITEBALANCE_DEF,
190 .set = sd_setwhitebalance,
191 .get = sd_getwhitebalance,
195 .id = V4L2_CID_RED_BALANCE,
196 .type = V4L2_CTRL_TYPE_INTEGER,
197 .name = "Red",
198 .minimum = 0,
199 .maximum = 3,
200 .step = 1,
201 #define REDBALANCE_DEF 1
202 .default_value = REDBALANCE_DEF,
204 .set = sd_setredbalance,
205 .get = sd_getredbalance,
209 .id = V4L2_CID_BLUE_BALANCE,
210 .type = V4L2_CTRL_TYPE_INTEGER,
211 .name = "Blue",
212 .minimum = 0,
213 .maximum = 3,
214 .step = 1,
215 #define BLUEBALANCE_DEF 1
216 .default_value = BLUEBALANCE_DEF,
218 .set = sd_setbluebalance,
219 .get = sd_getbluebalance,
221 /* All controls below are for both the 7302 and the 7311 */
224 .id = V4L2_CID_GAIN,
225 .type = V4L2_CTRL_TYPE_INTEGER,
226 .name = "Gain",
227 .minimum = 0,
228 #define GAIN_MAX 255
229 .maximum = GAIN_MAX,
230 .step = 1,
231 #define GAIN_DEF 127
232 #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
233 .default_value = GAIN_DEF,
235 .set = sd_setgain,
236 .get = sd_getgain,
240 .id = V4L2_CID_EXPOSURE,
241 .type = V4L2_CTRL_TYPE_INTEGER,
242 .name = "Exposure",
243 .minimum = 0,
244 #define EXPOSURE_MAX 255
245 .maximum = EXPOSURE_MAX,
246 .step = 1,
247 #define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
248 #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
249 .default_value = EXPOSURE_DEF,
251 .set = sd_setexposure,
252 .get = sd_getexposure,
256 .id = V4L2_CID_AUTOGAIN,
257 .type = V4L2_CTRL_TYPE_BOOLEAN,
258 .name = "Auto Gain",
259 .minimum = 0,
260 .maximum = 1,
261 .step = 1,
262 #define AUTOGAIN_DEF 1
263 .default_value = AUTOGAIN_DEF,
265 .set = sd_setautogain,
266 .get = sd_getautogain,
270 .id = V4L2_CID_HFLIP,
271 .type = V4L2_CTRL_TYPE_BOOLEAN,
272 .name = "Mirror",
273 .minimum = 0,
274 .maximum = 1,
275 .step = 1,
276 #define HFLIP_DEF 0
277 .default_value = HFLIP_DEF,
279 .set = sd_sethflip,
280 .get = sd_gethflip,
284 .id = V4L2_CID_VFLIP,
285 .type = V4L2_CTRL_TYPE_BOOLEAN,
286 .name = "Vflip",
287 .minimum = 0,
288 .maximum = 1,
289 .step = 1,
290 #define VFLIP_DEF 0
291 .default_value = VFLIP_DEF,
293 .set = sd_setvflip,
294 .get = sd_getvflip,
298 static const struct v4l2_pix_format vga_mode[] = {
299 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
300 .bytesperline = 640,
301 .sizeimage = 640 * 480 * 3 / 8 + 590,
302 .colorspace = V4L2_COLORSPACE_JPEG,
303 .priv = 0},
306 #define LOAD_PAGE3 255
307 #define END_OF_SEQUENCE 0
309 /* pac 7302 */
310 static const __u8 init_7302[] = {
311 /* index,value */
312 0xff, 0x01, /* page 1 */
313 0x78, 0x00, /* deactivate */
314 0xff, 0x01,
315 0x78, 0x40, /* led off */
317 static const __u8 start_7302[] = {
318 /* index, len, [value]* */
319 0xff, 1, 0x00, /* page 0 */
320 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
321 0x00, 0x00, 0x00, 0x00,
322 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
323 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
324 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
325 0x26, 2, 0xaa, 0xaa,
326 0x2e, 1, 0x31,
327 0x38, 1, 0x01,
328 0x3a, 3, 0x14, 0xff, 0x5a,
329 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
330 0x00, 0x54, 0x11,
331 0x55, 1, 0x00,
332 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
333 0x6b, 1, 0x00,
334 0x6e, 3, 0x08, 0x06, 0x00,
335 0x72, 3, 0x00, 0xff, 0x00,
336 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
337 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
338 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
339 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
340 0xd2, 0xeb,
341 0xaf, 1, 0x02,
342 0xb5, 2, 0x08, 0x08,
343 0xb8, 2, 0x08, 0x88,
344 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
345 0xcc, 1, 0x00,
346 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
347 0xc1, 0xd7, 0xec,
348 0xdc, 1, 0x01,
349 0xff, 1, 0x01, /* page 1 */
350 0x12, 3, 0x02, 0x00, 0x01,
351 0x3e, 2, 0x00, 0x00,
352 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
353 0x7c, 1, 0x00,
354 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
355 0x02, 0x00,
356 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
357 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
358 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
359 0xd8, 1, 0x01,
360 0xdb, 2, 0x00, 0x01,
361 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
362 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
363 0xeb, 1, 0x00,
364 0xff, 1, 0x02, /* page 2 */
365 0x22, 1, 0x00,
366 0xff, 1, 0x03, /* page 3 */
367 0, LOAD_PAGE3, /* load the page 3 */
368 0x11, 1, 0x01,
369 0xff, 1, 0x02, /* page 2 */
370 0x13, 1, 0x00,
371 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
372 0x27, 2, 0x14, 0x0c,
373 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
374 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
375 0x6e, 1, 0x08,
376 0xff, 1, 0x01, /* page 1 */
377 0x78, 1, 0x00,
378 0, END_OF_SEQUENCE /* end of sequence */
381 #define SKIP 0xaa
382 /* page 3 - the value SKIP says skip the index - see reg_w_page() */
383 static const __u8 page3_7302[] = {
384 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
385 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
386 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
388 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
389 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
390 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
391 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
393 SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
394 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
398 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
399 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
400 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
401 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
403 0x00
406 static void reg_w_buf(struct gspca_dev *gspca_dev,
407 __u8 index,
408 const char *buffer, int len)
410 int ret;
412 if (gspca_dev->usb_err < 0)
413 return;
414 memcpy(gspca_dev->usb_buf, buffer, len);
415 ret = usb_control_msg(gspca_dev->dev,
416 usb_sndctrlpipe(gspca_dev->dev, 0),
417 1, /* request */
418 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
419 0, /* value */
420 index, gspca_dev->usb_buf, len,
421 500);
422 if (ret < 0) {
423 PDEBUG(D_ERR, "reg_w_buf(): "
424 "Failed to write registers to index 0x%x, error %i",
425 index, ret);
426 gspca_dev->usb_err = ret;
431 static void reg_w(struct gspca_dev *gspca_dev,
432 __u8 index,
433 __u8 value)
435 int ret;
437 if (gspca_dev->usb_err < 0)
438 return;
439 gspca_dev->usb_buf[0] = value;
440 ret = usb_control_msg(gspca_dev->dev,
441 usb_sndctrlpipe(gspca_dev->dev, 0),
442 0, /* request */
443 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
444 0, index, gspca_dev->usb_buf, 1,
445 500);
446 if (ret < 0) {
447 PDEBUG(D_ERR, "reg_w(): "
448 "Failed to write register to index 0x%x, value 0x%x, error %i",
449 index, value, ret);
450 gspca_dev->usb_err = ret;
454 static void reg_w_seq(struct gspca_dev *gspca_dev,
455 const __u8 *seq, int len)
457 while (--len >= 0) {
458 reg_w(gspca_dev, seq[0], seq[1]);
459 seq += 2;
463 /* load the beginning of a page */
464 static void reg_w_page(struct gspca_dev *gspca_dev,
465 const __u8 *page, int len)
467 int index;
468 int ret = 0;
470 if (gspca_dev->usb_err < 0)
471 return;
472 for (index = 0; index < len; index++) {
473 if (page[index] == SKIP) /* skip this index */
474 continue;
475 gspca_dev->usb_buf[0] = page[index];
476 ret = usb_control_msg(gspca_dev->dev,
477 usb_sndctrlpipe(gspca_dev->dev, 0),
478 0, /* request */
479 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
480 0, index, gspca_dev->usb_buf, 1,
481 500);
482 if (ret < 0) {
483 PDEBUG(D_ERR, "reg_w_page(): "
484 "Failed to write register to index 0x%x, "
485 "value 0x%x, error %i",
486 index, page[index], ret);
487 gspca_dev->usb_err = ret;
488 break;
493 /* output a variable sequence */
494 static void reg_w_var(struct gspca_dev *gspca_dev,
495 const __u8 *seq,
496 const __u8 *page3, unsigned int page3_len)
498 int index, len;
500 for (;;) {
501 index = *seq++;
502 len = *seq++;
503 switch (len) {
504 case END_OF_SEQUENCE:
505 return;
506 case LOAD_PAGE3:
507 reg_w_page(gspca_dev, page3, page3_len);
508 break;
509 default:
510 if (len > USB_BUF_SZ) {
511 PDEBUG(D_ERR|D_STREAM,
512 "Incorrect variable sequence");
513 return;
515 while (len > 0) {
516 if (len < 8) {
517 reg_w_buf(gspca_dev,
518 index, seq, len);
519 seq += len;
520 break;
522 reg_w_buf(gspca_dev, index, seq, 8);
523 seq += 8;
524 index += 8;
525 len -= 8;
529 /* not reached */
532 /* this function is called at probe time for pac7302 */
533 static int sd_config(struct gspca_dev *gspca_dev,
534 const struct usb_device_id *id)
536 struct sd *sd = (struct sd *) gspca_dev;
537 struct cam *cam;
539 cam = &gspca_dev->cam;
541 PDEBUG(D_CONF, "Find Sensor PAC7302");
542 cam->cam_mode = vga_mode; /* only 640x480 */
543 cam->nmodes = ARRAY_SIZE(vga_mode);
545 sd->brightness = BRIGHTNESS_DEF;
546 sd->contrast = CONTRAST_DEF;
547 sd->colors = COLOR_DEF;
548 sd->white_balance = WHITEBALANCE_DEF;
549 sd->red_balance = REDBALANCE_DEF;
550 sd->blue_balance = BLUEBALANCE_DEF;
551 sd->gain = GAIN_DEF;
552 sd->exposure = EXPOSURE_DEF;
553 sd->autogain = AUTOGAIN_DEF;
554 sd->hflip = HFLIP_DEF;
555 sd->vflip = VFLIP_DEF;
556 sd->flags = id->driver_info;
557 return 0;
560 /* This function is used by pac7302 only */
561 static void setbrightcont(struct gspca_dev *gspca_dev)
563 struct sd *sd = (struct sd *) gspca_dev;
564 int i, v;
565 static const __u8 max[10] =
566 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
567 0xd4, 0xec};
568 static const __u8 delta[10] =
569 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
570 0x11, 0x0b};
572 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
573 for (i = 0; i < 10; i++) {
574 v = max[i];
575 v += (sd->brightness - BRIGHTNESS_MAX)
576 * 150 / BRIGHTNESS_MAX; /* 200 ? */
577 v -= delta[i] * sd->contrast / CONTRAST_MAX;
578 if (v < 0)
579 v = 0;
580 else if (v > 0xff)
581 v = 0xff;
582 reg_w(gspca_dev, 0xa2 + i, v);
584 reg_w(gspca_dev, 0xdc, 0x01);
587 /* This function is used by pac7302 only */
588 static void setcolors(struct gspca_dev *gspca_dev)
590 struct sd *sd = (struct sd *) gspca_dev;
591 int i, v;
592 static const int a[9] =
593 {217, -212, 0, -101, 170, -67, -38, -315, 355};
594 static const int b[9] =
595 {19, 106, 0, 19, 106, 1, 19, 106, 1};
597 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
598 reg_w(gspca_dev, 0x11, 0x01);
599 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
600 for (i = 0; i < 9; i++) {
601 v = a[i] * sd->colors / COLOR_MAX + b[i];
602 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
603 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
605 reg_w(gspca_dev, 0xdc, 0x01);
606 PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
609 static void setwhitebalance(struct gspca_dev *gspca_dev)
611 struct sd *sd = (struct sd *) gspca_dev;
613 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
614 reg_w(gspca_dev, 0xc6, sd->white_balance);
616 reg_w(gspca_dev, 0xdc, 0x01);
617 PDEBUG(D_CONF|D_STREAM, "white_balance: %i", sd->white_balance);
620 static void setredbalance(struct gspca_dev *gspca_dev)
622 struct sd *sd = (struct sd *) gspca_dev;
624 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
625 reg_w(gspca_dev, 0xc5, sd->red_balance);
627 reg_w(gspca_dev, 0xdc, 0x01);
628 PDEBUG(D_CONF|D_STREAM, "red_balance: %i", sd->red_balance);
631 static void setbluebalance(struct gspca_dev *gspca_dev)
633 struct sd *sd = (struct sd *) gspca_dev;
635 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
636 reg_w(gspca_dev, 0xc7, sd->blue_balance);
638 reg_w(gspca_dev, 0xdc, 0x01);
639 PDEBUG(D_CONF|D_STREAM, "blue_balance: %i", sd->blue_balance);
642 static void setgain(struct gspca_dev *gspca_dev)
644 struct sd *sd = (struct sd *) gspca_dev;
646 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
647 reg_w(gspca_dev, 0x10, sd->gain >> 3);
649 /* load registers to sensor (Bit 0, auto clear) */
650 reg_w(gspca_dev, 0x11, 0x01);
653 static void setexposure(struct gspca_dev *gspca_dev)
655 struct sd *sd = (struct sd *) gspca_dev;
656 __u8 reg;
658 /* register 2 of frame 3/4 contains the clock divider configuring the
659 no fps according to the formula: 60 / reg. sd->exposure is the
660 desired exposure time in ms. */
661 reg = 120 * sd->exposure / 1000;
662 if (reg < 2)
663 reg = 2;
664 else if (reg > 63)
665 reg = 63;
667 /* On the pac7302 reg2 MUST be a multiple of 3, so round it to
668 the nearest multiple of 3, except when between 6 and 12? */
669 if (reg < 6 || reg > 12)
670 reg = ((reg + 1) / 3) * 3;
671 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
672 reg_w(gspca_dev, 0x02, reg);
674 /* load registers to sensor (Bit 0, auto clear) */
675 reg_w(gspca_dev, 0x11, 0x01);
678 static void sethvflip(struct gspca_dev *gspca_dev)
680 struct sd *sd = (struct sd *) gspca_dev;
681 u8 data, hflip, vflip;
683 hflip = sd->hflip;
684 if (sd->flags & FL_HFLIP)
685 hflip = !hflip;
686 vflip = sd->vflip;
687 if (sd->flags & FL_VFLIP)
688 vflip = !vflip;
690 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
691 data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
692 reg_w(gspca_dev, 0x21, data);
694 /* load registers to sensor (Bit 0, auto clear) */
695 reg_w(gspca_dev, 0x11, 0x01);
698 /* this function is called at probe and resume time for pac7302 */
699 static int sd_init(struct gspca_dev *gspca_dev)
701 reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
702 return gspca_dev->usb_err;
705 static int sd_start(struct gspca_dev *gspca_dev)
707 struct sd *sd = (struct sd *) gspca_dev;
709 sd->sof_read = 0;
711 reg_w_var(gspca_dev, start_7302,
712 page3_7302, sizeof(page3_7302));
713 setbrightcont(gspca_dev);
714 setcolors(gspca_dev);
715 setwhitebalance(gspca_dev);
716 setredbalance(gspca_dev);
717 setbluebalance(gspca_dev);
718 setgain(gspca_dev);
719 setexposure(gspca_dev);
720 sethvflip(gspca_dev);
722 /* only resolution 640x480 is supported for pac7302 */
724 sd->sof_read = 0;
725 sd->autogain_ignore_frames = 0;
726 atomic_set(&sd->avg_lum, -1);
728 /* start stream */
729 reg_w(gspca_dev, 0xff, 0x01);
730 reg_w(gspca_dev, 0x78, 0x01);
732 return gspca_dev->usb_err;
735 static void sd_stopN(struct gspca_dev *gspca_dev)
738 /* stop stream */
739 reg_w(gspca_dev, 0xff, 0x01);
740 reg_w(gspca_dev, 0x78, 0x00);
743 /* called on streamoff with alt 0 and on disconnect for pac7302 */
744 static void sd_stop0(struct gspca_dev *gspca_dev)
746 if (!gspca_dev->present)
747 return;
748 reg_w(gspca_dev, 0xff, 0x01);
749 reg_w(gspca_dev, 0x78, 0x40);
752 /* Include pac common sof detection functions */
753 #include "pac_common.h"
755 static void do_autogain(struct gspca_dev *gspca_dev)
757 struct sd *sd = (struct sd *) gspca_dev;
758 int avg_lum = atomic_read(&sd->avg_lum);
759 int desired_lum, deadzone;
761 if (avg_lum == -1)
762 return;
764 desired_lum = 270 + sd->brightness * 4;
765 /* Hack hack, with the 7202 the first exposure step is
766 pretty large, so if we're about to make the first
767 exposure increase make the deadzone large to avoid
768 oscilating */
769 if (desired_lum > avg_lum && sd->gain == GAIN_DEF &&
770 sd->exposure > EXPOSURE_DEF &&
771 sd->exposure < 42)
772 deadzone = 90;
773 else
774 deadzone = 30;
776 if (sd->autogain_ignore_frames > 0)
777 sd->autogain_ignore_frames--;
778 else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
779 deadzone, GAIN_KNEE, EXPOSURE_KNEE))
780 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
783 /* JPEG header, part 1 */
784 static const unsigned char pac_jpeg_header1[] = {
785 0xff, 0xd8, /* SOI: Start of Image */
787 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
788 0x00, 0x11, /* length = 17 bytes (including this length field) */
789 0x08 /* Precision: 8 */
790 /* 2 bytes is placed here: number of image lines */
791 /* 2 bytes is placed here: samples per line */
794 /* JPEG header, continued */
795 static const unsigned char pac_jpeg_header2[] = {
796 0x03, /* Number of image components: 3 */
797 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
798 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
799 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
801 0xff, 0xda, /* SOS: Start Of Scan */
802 0x00, 0x0c, /* length = 12 bytes (including this length field) */
803 0x03, /* number of components: 3 */
804 0x01, 0x00, /* selector 1, table 0x00 */
805 0x02, 0x11, /* selector 2, table 0x11 */
806 0x03, 0x11, /* selector 3, table 0x11 */
807 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
808 0x00 /* Successive approximation: 0 */
811 static void pac_start_frame(struct gspca_dev *gspca_dev,
812 struct gspca_frame *frame,
813 __u16 lines, __u16 samples_per_line)
815 unsigned char tmpbuf[4];
817 gspca_frame_add(gspca_dev, FIRST_PACKET,
818 pac_jpeg_header1, sizeof(pac_jpeg_header1));
820 tmpbuf[0] = lines >> 8;
821 tmpbuf[1] = lines & 0xff;
822 tmpbuf[2] = samples_per_line >> 8;
823 tmpbuf[3] = samples_per_line & 0xff;
825 gspca_frame_add(gspca_dev, INTER_PACKET,
826 tmpbuf, sizeof(tmpbuf));
827 gspca_frame_add(gspca_dev, INTER_PACKET,
828 pac_jpeg_header2, sizeof(pac_jpeg_header2));
831 /* this function is run at interrupt level */
832 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
833 u8 *data, /* isoc packet */
834 int len) /* iso packet length */
836 struct sd *sd = (struct sd *) gspca_dev;
837 struct gspca_frame *frame;
838 unsigned char *sof;
840 sof = pac_find_sof(&sd->sof_read, data, len);
841 if (sof) {
842 int n, lum_offset, footer_length;
844 frame = gspca_get_i_frame(gspca_dev);
845 if (frame == NULL) {
846 gspca_dev->last_packet_type = DISCARD_PACKET;
847 return;
850 /* 6 bytes after the FF D9 EOF marker a number of lumination
851 bytes are send corresponding to different parts of the
852 image, the 14th and 15th byte after the EOF seem to
853 correspond to the center of the image */
854 lum_offset = 61 + sizeof pac_sof_marker;
855 footer_length = 74;
857 /* Finish decoding current frame */
858 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
859 if (n < 0) {
860 frame->data_end += n;
861 n = 0;
863 gspca_frame_add(gspca_dev, INTER_PACKET,
864 data, n);
865 if (gspca_dev->last_packet_type != DISCARD_PACKET &&
866 frame->data_end[-2] == 0xff &&
867 frame->data_end[-1] == 0xd9)
868 gspca_frame_add(gspca_dev, LAST_PACKET,
869 NULL, 0);
871 n = sof - data;
872 len -= n;
873 data = sof;
875 /* Get average lumination */
876 if (gspca_dev->last_packet_type == LAST_PACKET &&
877 n >= lum_offset)
878 atomic_set(&sd->avg_lum, data[-lum_offset] +
879 data[-lum_offset + 1]);
880 else
881 atomic_set(&sd->avg_lum, -1);
883 /* Start the new frame with the jpeg header */
884 /* The PAC7302 has the image rotated 90 degrees */
885 pac_start_frame(gspca_dev, frame,
886 gspca_dev->width, gspca_dev->height);
888 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
891 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
893 struct sd *sd = (struct sd *) gspca_dev;
895 sd->brightness = val;
896 if (gspca_dev->streaming)
897 setbrightcont(gspca_dev);
898 return gspca_dev->usb_err;
901 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
903 struct sd *sd = (struct sd *) gspca_dev;
905 *val = sd->brightness;
906 return 0;
909 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
911 struct sd *sd = (struct sd *) gspca_dev;
913 sd->contrast = val;
914 if (gspca_dev->streaming) {
915 setbrightcont(gspca_dev);
917 return gspca_dev->usb_err;
920 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
922 struct sd *sd = (struct sd *) gspca_dev;
924 *val = sd->contrast;
925 return 0;
928 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
930 struct sd *sd = (struct sd *) gspca_dev;
932 sd->colors = val;
933 if (gspca_dev->streaming)
934 setcolors(gspca_dev);
935 return gspca_dev->usb_err;
938 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
940 struct sd *sd = (struct sd *) gspca_dev;
942 *val = sd->colors;
943 return 0;
946 static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val)
948 struct sd *sd = (struct sd *) gspca_dev;
950 sd->white_balance = val;
951 if (gspca_dev->streaming)
952 setwhitebalance(gspca_dev);
953 return gspca_dev->usb_err;
956 static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val)
958 struct sd *sd = (struct sd *) gspca_dev;
960 *val = sd->white_balance;
961 return 0;
964 static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val)
966 struct sd *sd = (struct sd *) gspca_dev;
968 sd->red_balance = val;
969 if (gspca_dev->streaming)
970 setredbalance(gspca_dev);
971 return gspca_dev->usb_err;
974 static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val)
976 struct sd *sd = (struct sd *) gspca_dev;
978 *val = sd->red_balance;
979 return 0;
982 static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val)
984 struct sd *sd = (struct sd *) gspca_dev;
986 sd->blue_balance = val;
987 if (gspca_dev->streaming)
988 setbluebalance(gspca_dev);
989 return gspca_dev->usb_err;
992 static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val)
994 struct sd *sd = (struct sd *) gspca_dev;
996 *val = sd->blue_balance;
997 return 0;
1000 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1002 struct sd *sd = (struct sd *) gspca_dev;
1004 sd->gain = val;
1005 if (gspca_dev->streaming)
1006 setgain(gspca_dev);
1007 return gspca_dev->usb_err;
1010 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1012 struct sd *sd = (struct sd *) gspca_dev;
1014 *val = sd->gain;
1015 return 0;
1018 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1020 struct sd *sd = (struct sd *) gspca_dev;
1022 sd->exposure = val;
1023 if (gspca_dev->streaming)
1024 setexposure(gspca_dev);
1025 return gspca_dev->usb_err;
1028 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1030 struct sd *sd = (struct sd *) gspca_dev;
1032 *val = sd->exposure;
1033 return 0;
1036 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1038 struct sd *sd = (struct sd *) gspca_dev;
1040 sd->autogain = val;
1041 /* when switching to autogain set defaults to make sure
1042 we are on a valid point of the autogain gain /
1043 exposure knee graph, and give this change time to
1044 take effect before doing autogain. */
1045 if (sd->autogain) {
1046 sd->exposure = EXPOSURE_DEF;
1047 sd->gain = GAIN_DEF;
1048 if (gspca_dev->streaming) {
1049 sd->autogain_ignore_frames =
1050 PAC_AUTOGAIN_IGNORE_FRAMES;
1051 setexposure(gspca_dev);
1052 setgain(gspca_dev);
1056 return gspca_dev->usb_err;
1059 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1061 struct sd *sd = (struct sd *) gspca_dev;
1063 *val = sd->autogain;
1064 return 0;
1067 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1069 struct sd *sd = (struct sd *) gspca_dev;
1071 sd->hflip = val;
1072 if (gspca_dev->streaming)
1073 sethvflip(gspca_dev);
1074 return gspca_dev->usb_err;
1077 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1079 struct sd *sd = (struct sd *) gspca_dev;
1081 *val = sd->hflip;
1082 return 0;
1085 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1087 struct sd *sd = (struct sd *) gspca_dev;
1089 sd->vflip = val;
1090 if (gspca_dev->streaming)
1091 sethvflip(gspca_dev);
1092 return gspca_dev->usb_err;
1095 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1097 struct sd *sd = (struct sd *) gspca_dev;
1099 *val = sd->vflip;
1100 return 0;
1103 #ifdef CONFIG_VIDEO_ADV_DEBUG
1104 static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
1105 struct v4l2_dbg_register *reg)
1107 __u8 index;
1108 __u8 value;
1110 /* reg->reg: bit0..15: reserved for register index (wIndex is 16bit
1111 long on the USB bus)
1113 if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
1114 reg->match.addr == 0 &&
1115 (reg->reg < 0x000000ff) &&
1116 (reg->val <= 0x000000ff)
1118 /* Currently writing to page 0 is only supported. */
1119 /* reg_w() only supports 8bit index */
1120 index = reg->reg & 0x000000ff;
1121 value = reg->val & 0x000000ff;
1123 /* Note that there shall be no access to other page
1124 by any other function between the page swith and
1125 the actual register write */
1126 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
1127 reg_w(gspca_dev, index, value);
1129 reg_w(gspca_dev, 0xdc, 0x01);
1131 return gspca_dev->usb_err;
1134 static int sd_chip_ident(struct gspca_dev *gspca_dev,
1135 struct v4l2_dbg_chip_ident *chip)
1137 int ret = -EINVAL;
1139 if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
1140 chip->match.addr == 0) {
1141 chip->revision = 0;
1142 chip->ident = V4L2_IDENT_UNKNOWN;
1143 ret = 0;
1145 return ret;
1147 #endif
1149 #ifdef CONFIG_INPUT
1150 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1151 u8 *data, /* interrupt packet data */
1152 int len) /* interrput packet length */
1154 int ret = -EINVAL;
1155 u8 data0, data1;
1157 if (len == 2) {
1158 data0 = data[0];
1159 data1 = data[1];
1160 if ((data0 == 0x00 && data1 == 0x11) ||
1161 (data0 == 0x22 && data1 == 0x33) ||
1162 (data0 == 0x44 && data1 == 0x55) ||
1163 (data0 == 0x66 && data1 == 0x77) ||
1164 (data0 == 0x88 && data1 == 0x99) ||
1165 (data0 == 0xaa && data1 == 0xbb) ||
1166 (data0 == 0xcc && data1 == 0xdd) ||
1167 (data0 == 0xee && data1 == 0xff)) {
1168 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1169 input_sync(gspca_dev->input_dev);
1170 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1171 input_sync(gspca_dev->input_dev);
1172 ret = 0;
1176 return ret;
1178 #endif
1180 /* sub-driver description for pac7302 */
1181 static const struct sd_desc sd_desc = {
1182 .name = MODULE_NAME,
1183 .ctrls = sd_ctrls,
1184 .nctrls = ARRAY_SIZE(sd_ctrls),
1185 .config = sd_config,
1186 .init = sd_init,
1187 .start = sd_start,
1188 .stopN = sd_stopN,
1189 .stop0 = sd_stop0,
1190 .pkt_scan = sd_pkt_scan,
1191 .dq_callback = do_autogain,
1192 #ifdef CONFIG_VIDEO_ADV_DEBUG
1193 .set_register = sd_dbg_s_register,
1194 .get_chip_ident = sd_chip_ident,
1195 #endif
1196 #ifdef CONFIG_INPUT
1197 .int_pkt_scan = sd_int_pkt_scan,
1198 #endif
1201 /* -- module initialisation -- */
1202 static const struct usb_device_id device_table[] __devinitconst = {
1203 {USB_DEVICE(0x06f8, 0x3009)},
1204 {USB_DEVICE(0x093a, 0x2620)},
1205 {USB_DEVICE(0x093a, 0x2621)},
1206 {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
1207 {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
1208 {USB_DEVICE(0x093a, 0x2626)},
1209 {USB_DEVICE(0x093a, 0x2628)},
1210 {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
1211 {USB_DEVICE(0x093a, 0x262a)},
1212 {USB_DEVICE(0x093a, 0x262c)},
1215 MODULE_DEVICE_TABLE(usb, device_table);
1217 /* -- device connect -- */
1218 static int __devinit sd_probe(struct usb_interface *intf,
1219 const struct usb_device_id *id)
1221 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1222 THIS_MODULE);
1225 static struct usb_driver sd_driver = {
1226 .name = MODULE_NAME,
1227 .id_table = device_table,
1228 .probe = sd_probe,
1229 .disconnect = gspca_disconnect,
1230 #ifdef CONFIG_PM
1231 .suspend = gspca_suspend,
1232 .resume = gspca_resume,
1233 #endif
1236 /* -- module insert / remove -- */
1237 static int __init sd_mod_init(void)
1239 int ret;
1240 ret = usb_register(&sd_driver);
1241 if (ret < 0)
1242 return ret;
1243 PDEBUG(D_PROBE, "registered");
1244 return 0;
1246 static void __exit sd_mod_exit(void)
1248 usb_deregister(&sd_driver);
1249 PDEBUG(D_PROBE, "deregistered");
1252 module_init(sd_mod_init);
1253 module_exit(sd_mod_exit);