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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Some documentation about various registers as determined by trial and error.
25 When the register addresses differ between the 7202 and the 7311 the 2
26 different addresses are written as 7302addr/7311addr, when one of the 2
27 addresses is a - sign that register description is not valid for the
33 -/0x08 Unknown compressor related, must always be 8 except when not
34 in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
35 -/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
36 bits 345 seem to toggle per color gains on/off (inverted)
37 0x78 Global control, bit 6 controls the LED (inverted)
38 -/0x80 JPEG compression ratio ? Best not touched
43 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
44 the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
45 -/0x0f Master gain 1-245, low value = high gain
46 0x10/- Master gain 0-31
47 -/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
48 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
49 -/0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
50 completely disable the analog amplification block. Set to 0x68
51 for max gain, 0x14 for minimal gain.
53 The registers are accessed in the following functions:
55 Page | Register | Function
56 -----+------------+---------------------------------------------------
57 0 | 0x0f..0x20 | setcolors()
58 0 | 0xa2..0xab | setbrightcont()
59 0 | 0xc5 | setredbalance()
60 0 | 0xc6 | setwhitebalance()
61 0 | 0xc7 | setbluebalance()
62 0 | 0xdc | setbrightcont(), setcolors()
63 3 | 0x02 | setexposure()
65 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
66 3 | 0x21 | sethvflip()
69 #define MODULE_NAME "pac7302"
71 #include <media/v4l2-chip-ident.h>
74 MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
75 MODULE_DESCRIPTION("Pixart PAC7302");
76 MODULE_LICENSE("GPL");
78 /* specific webcam descriptor for pac7302 */
80 struct gspca_dev gspca_dev
; /* !! must be the first item */
82 unsigned char brightness
;
83 unsigned char contrast
;
85 unsigned char white_balance
;
86 unsigned char red_balance
;
87 unsigned char blue_balance
;
89 unsigned char exposure
;
90 unsigned char autogain
;
95 u8 autogain_ignore_frames
;
100 /* V4L2 controls supported by the driver */
101 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
);
102 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
);
103 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
);
104 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
);
105 static int sd_setcolors(struct gspca_dev
*gspca_dev
, __s32 val
);
106 static int sd_getcolors(struct gspca_dev
*gspca_dev
, __s32
*val
);
107 static int sd_setwhitebalance(struct gspca_dev
*gspca_dev
, __s32 val
);
108 static int sd_getwhitebalance(struct gspca_dev
*gspca_dev
, __s32
*val
);
109 static int sd_setredbalance(struct gspca_dev
*gspca_dev
, __s32 val
);
110 static int sd_getredbalance(struct gspca_dev
*gspca_dev
, __s32
*val
);
111 static int sd_setbluebalance(struct gspca_dev
*gspca_dev
, __s32 val
);
112 static int sd_getbluebalance(struct gspca_dev
*gspca_dev
, __s32
*val
);
113 static int sd_setautogain(struct gspca_dev
*gspca_dev
, __s32 val
);
114 static int sd_getautogain(struct gspca_dev
*gspca_dev
, __s32
*val
);
115 static int sd_sethflip(struct gspca_dev
*gspca_dev
, __s32 val
);
116 static int sd_gethflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
117 static int sd_setvflip(struct gspca_dev
*gspca_dev
, __s32 val
);
118 static int sd_getvflip(struct gspca_dev
*gspca_dev
, __s32
*val
);
119 static int sd_setgain(struct gspca_dev
*gspca_dev
, __s32 val
);
120 static int sd_getgain(struct gspca_dev
*gspca_dev
, __s32
*val
);
121 static int sd_setexposure(struct gspca_dev
*gspca_dev
, __s32 val
);
122 static int sd_getexposure(struct gspca_dev
*gspca_dev
, __s32
*val
);
124 static struct ctrl sd_ctrls
[] = {
125 /* This control is pac7302 only */
128 .id
= V4L2_CID_BRIGHTNESS
,
129 .type
= V4L2_CTRL_TYPE_INTEGER
,
130 .name
= "Brightness",
132 #define BRIGHTNESS_MAX 0x20
133 .maximum
= BRIGHTNESS_MAX
,
135 #define BRIGHTNESS_DEF 0x10
136 .default_value
= BRIGHTNESS_DEF
,
138 .set
= sd_setbrightness
,
139 .get
= sd_getbrightness
,
141 /* This control is for both the 7302 and the 7311 */
144 .id
= V4L2_CID_CONTRAST
,
145 .type
= V4L2_CTRL_TYPE_INTEGER
,
148 #define CONTRAST_MAX 255
149 .maximum
= CONTRAST_MAX
,
151 #define CONTRAST_DEF 127
152 .default_value
= CONTRAST_DEF
,
154 .set
= sd_setcontrast
,
155 .get
= sd_getcontrast
,
157 /* This control is pac7302 only */
160 .id
= V4L2_CID_SATURATION
,
161 .type
= V4L2_CTRL_TYPE_INTEGER
,
162 .name
= "Saturation",
164 #define COLOR_MAX 255
165 .maximum
= COLOR_MAX
,
167 #define COLOR_DEF 127
168 .default_value
= COLOR_DEF
,
175 .id
= V4L2_CID_WHITE_BALANCE_TEMPERATURE
,
176 .type
= V4L2_CTRL_TYPE_INTEGER
,
177 .name
= "White Balance",
181 #define WHITEBALANCE_DEF 4
182 .default_value
= WHITEBALANCE_DEF
,
184 .set
= sd_setwhitebalance
,
185 .get
= sd_getwhitebalance
,
189 .id
= V4L2_CID_RED_BALANCE
,
190 .type
= V4L2_CTRL_TYPE_INTEGER
,
195 #define REDBALANCE_DEF 1
196 .default_value
= REDBALANCE_DEF
,
198 .set
= sd_setredbalance
,
199 .get
= sd_getredbalance
,
203 .id
= V4L2_CID_BLUE_BALANCE
,
204 .type
= V4L2_CTRL_TYPE_INTEGER
,
209 #define BLUEBALANCE_DEF 1
210 .default_value
= BLUEBALANCE_DEF
,
212 .set
= sd_setbluebalance
,
213 .get
= sd_getbluebalance
,
215 /* All controls below are for both the 7302 and the 7311 */
219 .type
= V4L2_CTRL_TYPE_INTEGER
,
226 #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
227 .default_value
= GAIN_DEF
,
234 .id
= V4L2_CID_EXPOSURE
,
235 .type
= V4L2_CTRL_TYPE_INTEGER
,
238 #define EXPOSURE_MAX 255
239 .maximum
= EXPOSURE_MAX
,
241 #define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
242 #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
243 .default_value
= EXPOSURE_DEF
,
245 .set
= sd_setexposure
,
246 .get
= sd_getexposure
,
250 .id
= V4L2_CID_AUTOGAIN
,
251 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
256 #define AUTOGAIN_DEF 1
257 .default_value
= AUTOGAIN_DEF
,
259 .set
= sd_setautogain
,
260 .get
= sd_getautogain
,
264 .id
= V4L2_CID_HFLIP
,
265 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
271 .default_value
= HFLIP_DEF
,
278 .id
= V4L2_CID_VFLIP
,
279 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
285 .default_value
= VFLIP_DEF
,
292 static const struct v4l2_pix_format vga_mode
[] = {
293 {640, 480, V4L2_PIX_FMT_PJPG
, V4L2_FIELD_NONE
,
295 .sizeimage
= 640 * 480 * 3 / 8 + 590,
296 .colorspace
= V4L2_COLORSPACE_JPEG
,
300 #define LOAD_PAGE3 255
301 #define LOAD_PAGE4 254
302 #define END_OF_SEQUENCE 0
305 static const __u8 init_7302
[] = {
307 0xff, 0x01, /* page 1 */
308 0x78, 0x00, /* deactivate */
310 0x78, 0x40, /* led off */
312 static const __u8 start_7302
[] = {
313 /* index, len, [value]* */
314 0xff, 1, 0x00, /* page 0 */
315 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
316 0x00, 0x00, 0x00, 0x00,
317 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
318 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
319 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
323 0x3a, 3, 0x14, 0xff, 0x5a,
324 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
327 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
329 0x6e, 3, 0x08, 0x06, 0x00,
330 0x72, 3, 0x00, 0xff, 0x00,
331 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
332 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
333 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
334 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
339 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
341 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
344 0xff, 1, 0x01, /* page 1 */
345 0x12, 3, 0x02, 0x00, 0x01,
347 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
349 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
351 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
352 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
353 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
356 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
357 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
359 0xff, 1, 0x02, /* page 2 */
361 0xff, 1, 0x03, /* page 3 */
362 0, LOAD_PAGE3
, /* load the page 3 */
364 0xff, 1, 0x02, /* page 2 */
366 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
368 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
369 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
371 0xff, 1, 0x01, /* page 1 */
373 0, END_OF_SEQUENCE
/* end of sequence */
377 /* page 3 - the value SKIP says skip the index - see reg_w_page() */
378 static const __u8 page3_7302
[] = {
379 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
380 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
381 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
383 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
384 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
385 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
386 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
388 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
389 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
391 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
393 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
394 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
395 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
401 static int reg_w_buf(struct gspca_dev
*gspca_dev
,
403 const char *buffer
, int len
)
407 memcpy(gspca_dev
->usb_buf
, buffer
, len
);
408 ret
= usb_control_msg(gspca_dev
->dev
,
409 usb_sndctrlpipe(gspca_dev
->dev
, 0),
411 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
413 index
, gspca_dev
->usb_buf
, len
,
416 PDEBUG(D_ERR
, "reg_w_buf(): "
417 "Failed to write registers to index 0x%x, error %i",
423 static int reg_w(struct gspca_dev
*gspca_dev
,
429 gspca_dev
->usb_buf
[0] = value
;
430 ret
= usb_control_msg(gspca_dev
->dev
,
431 usb_sndctrlpipe(gspca_dev
->dev
, 0),
433 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
434 0, index
, gspca_dev
->usb_buf
, 1,
437 PDEBUG(D_ERR
, "reg_w(): "
438 "Failed to write register to index 0x%x, value 0x%x, error %i",
443 static int reg_w_seq(struct gspca_dev
*gspca_dev
,
444 const __u8
*seq
, int len
)
449 ret
= reg_w(gspca_dev
, seq
[0], seq
[1]);
455 /* load the beginning of a page */
456 static int reg_w_page(struct gspca_dev
*gspca_dev
,
457 const __u8
*page
, int len
)
462 for (index
= 0; index
< len
; index
++) {
463 if (page
[index
] == SKIP
) /* skip this index */
465 gspca_dev
->usb_buf
[0] = page
[index
];
466 ret
= usb_control_msg(gspca_dev
->dev
,
467 usb_sndctrlpipe(gspca_dev
->dev
, 0),
469 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
470 0, index
, gspca_dev
->usb_buf
, 1,
473 PDEBUG(D_ERR
, "reg_w_page(): "
474 "Failed to write register to index 0x%x, "
475 "value 0x%x, error %i",
476 index
, page
[index
], ret
);
483 /* output a variable sequence */
484 static int reg_w_var(struct gspca_dev
*gspca_dev
,
486 const __u8
*page3
, unsigned int page3_len
,
487 const __u8
*page4
, unsigned int page4_len
)
496 case END_OF_SEQUENCE
:
499 ret
= reg_w_page(gspca_dev
, page4
, page4_len
);
502 ret
= reg_w_page(gspca_dev
, page3
, page3_len
);
505 if (len
> USB_BUF_SZ
) {
506 PDEBUG(D_ERR
|D_STREAM
,
507 "Incorrect variable sequence");
512 ret
= reg_w_buf(gspca_dev
,
519 ret
= reg_w_buf(gspca_dev
, index
, seq
, 8);
531 /* this function is called at probe time for pac7302 */
532 static int sd_config(struct gspca_dev
*gspca_dev
,
533 const struct usb_device_id
*id
)
535 struct sd
*sd
= (struct sd
*) gspca_dev
;
538 cam
= &gspca_dev
->cam
;
540 PDEBUG(D_CONF
, "Find Sensor PAC7302");
541 cam
->cam_mode
= vga_mode
; /* only 640x480 */
542 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
544 sd
->brightness
= BRIGHTNESS_DEF
;
545 sd
->contrast
= CONTRAST_DEF
;
546 sd
->colors
= COLOR_DEF
;
547 sd
->white_balance
= WHITEBALANCE_DEF
;
548 sd
->red_balance
= REDBALANCE_DEF
;
549 sd
->blue_balance
= BLUEBALANCE_DEF
;
551 sd
->exposure
= EXPOSURE_DEF
;
552 sd
->autogain
= AUTOGAIN_DEF
;
553 sd
->hflip
= HFLIP_DEF
;
554 sd
->vflip
= VFLIP_DEF
;
558 /* This function is used by pac7302 only */
559 static int setbrightcont(struct gspca_dev
*gspca_dev
)
561 struct sd
*sd
= (struct sd
*) gspca_dev
;
564 static const __u8 max
[10] =
565 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
567 static const __u8 delta
[10] =
568 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
571 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
572 for (i
= 0; i
< 10; i
++) {
574 v
+= (sd
->brightness
- BRIGHTNESS_MAX
)
575 * 150 / BRIGHTNESS_MAX
; /* 200 ? */
576 v
-= delta
[i
] * sd
->contrast
/ CONTRAST_MAX
;
582 ret
= reg_w(gspca_dev
, 0xa2 + i
, v
);
585 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
589 /* This function is used by pac7302 only */
590 static int setcolors(struct gspca_dev
*gspca_dev
)
592 struct sd
*sd
= (struct sd
*) gspca_dev
;
595 static const int a
[9] =
596 {217, -212, 0, -101, 170, -67, -38, -315, 355};
597 static const int b
[9] =
598 {19, 106, 0, 19, 106, 1, 19, 106, 1};
600 ret
= reg_w(gspca_dev
, 0xff, 0x03); /* page 3 */
602 ret
= reg_w(gspca_dev
, 0x11, 0x01);
604 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
605 for (i
= 0; i
< 9; i
++) {
606 v
= a
[i
] * sd
->colors
/ COLOR_MAX
+ b
[i
];
608 ret
= reg_w(gspca_dev
, 0x0f + 2 * i
, (v
>> 8) & 0x07);
610 ret
= reg_w(gspca_dev
, 0x0f + 2 * i
+ 1, v
);
613 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
614 PDEBUG(D_CONF
|D_STREAM
, "color: %i", sd
->colors
);
618 static int setwhitebalance(struct gspca_dev
*gspca_dev
)
620 struct sd
*sd
= (struct sd
*) gspca_dev
;
623 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
625 ret
= reg_w(gspca_dev
, 0xc6, sd
->white_balance
);
628 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
629 PDEBUG(D_CONF
|D_STREAM
, "white_balance: %i", sd
->white_balance
);
633 static int setredbalance(struct gspca_dev
*gspca_dev
)
635 struct sd
*sd
= (struct sd
*) gspca_dev
;
638 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
640 ret
= reg_w(gspca_dev
, 0xc5, sd
->red_balance
);
643 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
644 PDEBUG(D_CONF
|D_STREAM
, "red_balance: %i", sd
->red_balance
);
648 static int setbluebalance(struct gspca_dev
*gspca_dev
)
650 struct sd
*sd
= (struct sd
*) gspca_dev
;
653 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
655 ret
= reg_w(gspca_dev
, 0xc7, sd
->blue_balance
);
658 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
659 PDEBUG(D_CONF
|D_STREAM
, "blue_balance: %i", sd
->blue_balance
);
663 static int setgain(struct gspca_dev
*gspca_dev
)
665 struct sd
*sd
= (struct sd
*) gspca_dev
;
668 ret
= reg_w(gspca_dev
, 0xff, 0x03); /* page 3 */
670 ret
= reg_w(gspca_dev
, 0x10, sd
->gain
>> 3);
672 /* load registers to sensor (Bit 0, auto clear) */
674 ret
= reg_w(gspca_dev
, 0x11, 0x01);
678 static int setexposure(struct gspca_dev
*gspca_dev
)
680 struct sd
*sd
= (struct sd
*) gspca_dev
;
684 /* register 2 of frame 3/4 contains the clock divider configuring the
685 no fps according to the formula: 60 / reg. sd->exposure is the
686 desired exposure time in ms. */
687 reg
= 120 * sd
->exposure
/ 1000;
693 /* On the pac7302 reg2 MUST be a multiple of 3, so round it to
694 the nearest multiple of 3, except when between 6 and 12? */
695 if (reg
< 6 || reg
> 12)
696 reg
= ((reg
+ 1) / 3) * 3;
697 ret
= reg_w(gspca_dev
, 0xff, 0x03); /* page 3 */
699 ret
= reg_w(gspca_dev
, 0x02, reg
);
701 /* load registers to sensor (Bit 0, auto clear) */
703 ret
= reg_w(gspca_dev
, 0x11, 0x01);
707 static int sethvflip(struct gspca_dev
*gspca_dev
)
709 struct sd
*sd
= (struct sd
*) gspca_dev
;
713 ret
= reg_w(gspca_dev
, 0xff, 0x03); /* page 3 */
714 data
= (sd
->hflip
? 0x08 : 0x00) | (sd
->vflip
? 0x04 : 0x00);
716 ret
= reg_w(gspca_dev
, 0x21, data
);
717 /* load registers to sensor (Bit 0, auto clear) */
719 ret
= reg_w(gspca_dev
, 0x11, 0x01);
723 /* this function is called at probe and resume time for pac7302 */
724 static int sd_init(struct gspca_dev
*gspca_dev
)
726 return reg_w_seq(gspca_dev
, init_7302
, sizeof(init_7302
)/2);
729 static int sd_start(struct gspca_dev
*gspca_dev
)
731 struct sd
*sd
= (struct sd
*) gspca_dev
;
736 ret
= reg_w_var(gspca_dev
, start_7302
,
737 page3_7302
, sizeof(page3_7302
),
740 ret
= setbrightcont(gspca_dev
);
742 ret
= setcolors(gspca_dev
);
744 ret
= setwhitebalance(gspca_dev
);
746 ret
= setredbalance(gspca_dev
);
748 ret
= setbluebalance(gspca_dev
);
750 ret
= setgain(gspca_dev
);
752 ret
= setexposure(gspca_dev
);
754 ret
= sethvflip(gspca_dev
);
756 /* only resolution 640x480 is supported for pac7302 */
759 sd
->autogain_ignore_frames
= 0;
760 atomic_set(&sd
->avg_lum
, -1);
764 ret
= reg_w(gspca_dev
, 0xff, 0x01);
766 ret
= reg_w(gspca_dev
, 0x78, 0x01);
771 static void sd_stopN(struct gspca_dev
*gspca_dev
)
776 ret
= reg_w(gspca_dev
, 0xff, 0x01);
778 ret
= reg_w(gspca_dev
, 0x78, 0x00);
781 /* called on streamoff with alt 0 and on disconnect for pac7302 */
782 static void sd_stop0(struct gspca_dev
*gspca_dev
)
786 if (!gspca_dev
->present
)
788 ret
= reg_w(gspca_dev
, 0xff, 0x01);
790 ret
= reg_w(gspca_dev
, 0x78, 0x40);
793 /* Include pac common sof detection functions */
794 #include "pac_common.h"
796 static void do_autogain(struct gspca_dev
*gspca_dev
)
798 struct sd
*sd
= (struct sd
*) gspca_dev
;
799 int avg_lum
= atomic_read(&sd
->avg_lum
);
800 int desired_lum
, deadzone
;
805 desired_lum
= 270 + sd
->brightness
* 4;
806 /* Hack hack, with the 7202 the first exposure step is
807 pretty large, so if we're about to make the first
808 exposure increase make the deadzone large to avoid
810 if (desired_lum
> avg_lum
&& sd
->gain
== GAIN_DEF
&&
811 sd
->exposure
> EXPOSURE_DEF
&&
817 if (sd
->autogain_ignore_frames
> 0)
818 sd
->autogain_ignore_frames
--;
819 else if (gspca_auto_gain_n_exposure(gspca_dev
, avg_lum
, desired_lum
,
820 deadzone
, GAIN_KNEE
, EXPOSURE_KNEE
))
821 sd
->autogain_ignore_frames
= PAC_AUTOGAIN_IGNORE_FRAMES
;
824 /* JPEG header, part 1 */
825 static const unsigned char pac_jpeg_header1
[] = {
826 0xff, 0xd8, /* SOI: Start of Image */
828 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
829 0x00, 0x11, /* length = 17 bytes (including this length field) */
830 0x08 /* Precision: 8 */
831 /* 2 bytes is placed here: number of image lines */
832 /* 2 bytes is placed here: samples per line */
835 /* JPEG header, continued */
836 static const unsigned char pac_jpeg_header2
[] = {
837 0x03, /* Number of image components: 3 */
838 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
839 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
840 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
842 0xff, 0xda, /* SOS: Start Of Scan */
843 0x00, 0x0c, /* length = 12 bytes (including this length field) */
844 0x03, /* number of components: 3 */
845 0x01, 0x00, /* selector 1, table 0x00 */
846 0x02, 0x11, /* selector 2, table 0x11 */
847 0x03, 0x11, /* selector 3, table 0x11 */
848 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
849 0x00 /* Successive approximation: 0 */
852 static void pac_start_frame(struct gspca_dev
*gspca_dev
,
853 struct gspca_frame
*frame
,
854 __u16 lines
, __u16 samples_per_line
)
856 unsigned char tmpbuf
[4];
858 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
859 pac_jpeg_header1
, sizeof(pac_jpeg_header1
));
861 tmpbuf
[0] = lines
>> 8;
862 tmpbuf
[1] = lines
& 0xff;
863 tmpbuf
[2] = samples_per_line
>> 8;
864 tmpbuf
[3] = samples_per_line
& 0xff;
866 gspca_frame_add(gspca_dev
, INTER_PACKET
,
867 tmpbuf
, sizeof(tmpbuf
));
868 gspca_frame_add(gspca_dev
, INTER_PACKET
,
869 pac_jpeg_header2
, sizeof(pac_jpeg_header2
));
872 /* this function is run at interrupt level */
873 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
874 u8
*data
, /* isoc packet */
875 int len
) /* iso packet length */
877 struct sd
*sd
= (struct sd
*) gspca_dev
;
878 struct gspca_frame
*frame
;
881 sof
= pac_find_sof(&sd
->sof_read
, data
, len
);
883 int n
, lum_offset
, footer_length
;
885 frame
= gspca_get_i_frame(gspca_dev
);
887 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
891 /* 6 bytes after the FF D9 EOF marker a number of lumination
892 bytes are send corresponding to different parts of the
893 image, the 14th and 15th byte after the EOF seem to
894 correspond to the center of the image */
895 lum_offset
= 61 + sizeof pac_sof_marker
;
898 /* Finish decoding current frame */
899 n
= (sof
- data
) - (footer_length
+ sizeof pac_sof_marker
);
901 frame
->data_end
+= n
;
904 gspca_frame_add(gspca_dev
, INTER_PACKET
,
906 if (gspca_dev
->last_packet_type
!= DISCARD_PACKET
&&
907 frame
->data_end
[-2] == 0xff &&
908 frame
->data_end
[-1] == 0xd9)
909 gspca_frame_add(gspca_dev
, LAST_PACKET
,
916 /* Get average lumination */
917 if (gspca_dev
->last_packet_type
== LAST_PACKET
&&
919 atomic_set(&sd
->avg_lum
, data
[-lum_offset
] +
920 data
[-lum_offset
+ 1]);
922 atomic_set(&sd
->avg_lum
, -1);
924 /* Start the new frame with the jpeg header */
925 /* The PAC7302 has the image rotated 90 degrees */
926 pac_start_frame(gspca_dev
, frame
,
927 gspca_dev
->width
, gspca_dev
->height
);
929 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
932 static int sd_setbrightness(struct gspca_dev
*gspca_dev
, __s32 val
)
934 struct sd
*sd
= (struct sd
*) gspca_dev
;
936 sd
->brightness
= val
;
937 if (gspca_dev
->streaming
)
938 setbrightcont(gspca_dev
);
942 static int sd_getbrightness(struct gspca_dev
*gspca_dev
, __s32
*val
)
944 struct sd
*sd
= (struct sd
*) gspca_dev
;
946 *val
= sd
->brightness
;
950 static int sd_setcontrast(struct gspca_dev
*gspca_dev
, __s32 val
)
952 struct sd
*sd
= (struct sd
*) gspca_dev
;
955 if (gspca_dev
->streaming
) {
956 setbrightcont(gspca_dev
);
961 static int sd_getcontrast(struct gspca_dev
*gspca_dev
, __s32
*val
)
963 struct sd
*sd
= (struct sd
*) gspca_dev
;
969 static int sd_setcolors(struct gspca_dev
*gspca_dev
, __s32 val
)
971 struct sd
*sd
= (struct sd
*) gspca_dev
;
974 if (gspca_dev
->streaming
)
975 setcolors(gspca_dev
);
979 static int sd_getcolors(struct gspca_dev
*gspca_dev
, __s32
*val
)
981 struct sd
*sd
= (struct sd
*) gspca_dev
;
987 static int sd_setwhitebalance(struct gspca_dev
*gspca_dev
, __s32 val
)
989 struct sd
*sd
= (struct sd
*) gspca_dev
;
992 sd
->white_balance
= val
;
993 if (gspca_dev
->streaming
)
994 ret
= setwhitebalance(gspca_dev
);
1000 static int sd_getwhitebalance(struct gspca_dev
*gspca_dev
, __s32
*val
)
1002 struct sd
*sd
= (struct sd
*) gspca_dev
;
1004 *val
= sd
->white_balance
;
1008 static int sd_setredbalance(struct gspca_dev
*gspca_dev
, __s32 val
)
1010 struct sd
*sd
= (struct sd
*) gspca_dev
;
1013 sd
->red_balance
= val
;
1014 if (gspca_dev
->streaming
)
1015 ret
= setredbalance(gspca_dev
);
1021 static int sd_getredbalance(struct gspca_dev
*gspca_dev
, __s32
*val
)
1023 struct sd
*sd
= (struct sd
*) gspca_dev
;
1025 *val
= sd
->red_balance
;
1029 static int sd_setbluebalance(struct gspca_dev
*gspca_dev
, __s32 val
)
1031 struct sd
*sd
= (struct sd
*) gspca_dev
;
1034 sd
->blue_balance
= val
;
1035 if (gspca_dev
->streaming
)
1036 ret
= setbluebalance(gspca_dev
);
1042 static int sd_getbluebalance(struct gspca_dev
*gspca_dev
, __s32
*val
)
1044 struct sd
*sd
= (struct sd
*) gspca_dev
;
1046 *val
= sd
->blue_balance
;
1050 static int sd_setgain(struct gspca_dev
*gspca_dev
, __s32 val
)
1052 struct sd
*sd
= (struct sd
*) gspca_dev
;
1055 if (gspca_dev
->streaming
)
1060 static int sd_getgain(struct gspca_dev
*gspca_dev
, __s32
*val
)
1062 struct sd
*sd
= (struct sd
*) gspca_dev
;
1068 static int sd_setexposure(struct gspca_dev
*gspca_dev
, __s32 val
)
1070 struct sd
*sd
= (struct sd
*) gspca_dev
;
1073 if (gspca_dev
->streaming
)
1074 setexposure(gspca_dev
);
1078 static int sd_getexposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
1080 struct sd
*sd
= (struct sd
*) gspca_dev
;
1082 *val
= sd
->exposure
;
1086 static int sd_setautogain(struct gspca_dev
*gspca_dev
, __s32 val
)
1088 struct sd
*sd
= (struct sd
*) gspca_dev
;
1091 /* when switching to autogain set defaults to make sure
1092 we are on a valid point of the autogain gain /
1093 exposure knee graph, and give this change time to
1094 take effect before doing autogain. */
1096 sd
->exposure
= EXPOSURE_DEF
;
1097 sd
->gain
= GAIN_DEF
;
1098 if (gspca_dev
->streaming
) {
1099 sd
->autogain_ignore_frames
=
1100 PAC_AUTOGAIN_IGNORE_FRAMES
;
1101 setexposure(gspca_dev
);
1109 static int sd_getautogain(struct gspca_dev
*gspca_dev
, __s32
*val
)
1111 struct sd
*sd
= (struct sd
*) gspca_dev
;
1113 *val
= sd
->autogain
;
1117 static int sd_sethflip(struct gspca_dev
*gspca_dev
, __s32 val
)
1119 struct sd
*sd
= (struct sd
*) gspca_dev
;
1122 if (gspca_dev
->streaming
)
1123 sethvflip(gspca_dev
);
1127 static int sd_gethflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
1129 struct sd
*sd
= (struct sd
*) gspca_dev
;
1135 static int sd_setvflip(struct gspca_dev
*gspca_dev
, __s32 val
)
1137 struct sd
*sd
= (struct sd
*) gspca_dev
;
1140 if (gspca_dev
->streaming
)
1141 sethvflip(gspca_dev
);
1145 static int sd_getvflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
1147 struct sd
*sd
= (struct sd
*) gspca_dev
;
1153 #ifdef CONFIG_VIDEO_ADV_DEBUG
1154 static int sd_dbg_s_register(struct gspca_dev
*gspca_dev
,
1155 struct v4l2_dbg_register
*reg
)
1161 /* reg->reg: bit0..15: reserved for register index (wIndex is 16bit
1162 long on the USB bus)
1164 if (reg
->match
.type
== V4L2_CHIP_MATCH_HOST
&&
1165 reg
->match
.addr
== 0 &&
1166 (reg
->reg
< 0x000000ff) &&
1167 (reg
->val
<= 0x000000ff)
1169 /* Currently writing to page 0 is only supported. */
1170 /* reg_w() only supports 8bit index */
1171 index
= reg
->reg
& 0x000000ff;
1172 value
= reg
->val
& 0x000000ff;
1174 /* Note that there shall be no access to other page
1175 by any other function between the page swith and
1176 the actual register write */
1177 ret
= reg_w(gspca_dev
, 0xff, 0x00); /* page 0 */
1179 ret
= reg_w(gspca_dev
, index
, value
);
1182 ret
= reg_w(gspca_dev
, 0xdc, 0x01);
1187 static int sd_chip_ident(struct gspca_dev
*gspca_dev
,
1188 struct v4l2_dbg_chip_ident
*chip
)
1192 if (chip
->match
.type
== V4L2_CHIP_MATCH_HOST
&&
1193 chip
->match
.addr
== 0) {
1195 chip
->ident
= V4L2_IDENT_UNKNOWN
;
1202 /* sub-driver description for pac7302 */
1203 static struct sd_desc sd_desc
= {
1204 .name
= MODULE_NAME
,
1206 .nctrls
= ARRAY_SIZE(sd_ctrls
),
1207 .config
= sd_config
,
1212 .pkt_scan
= sd_pkt_scan
,
1213 .dq_callback
= do_autogain
,
1214 #ifdef CONFIG_VIDEO_ADV_DEBUG
1215 .set_register
= sd_dbg_s_register
,
1216 .get_chip_ident
= sd_chip_ident
,
1220 /* -- module initialisation -- */
1221 static __devinitdata
struct usb_device_id device_table
[] = {
1222 {USB_DEVICE(0x06f8, 0x3009)},
1223 {USB_DEVICE(0x093a, 0x2620)},
1224 {USB_DEVICE(0x093a, 0x2621)},
1225 {USB_DEVICE(0x093a, 0x2622)},
1226 {USB_DEVICE(0x093a, 0x2624)},
1227 {USB_DEVICE(0x093a, 0x2626)},
1228 {USB_DEVICE(0x093a, 0x2628)},
1229 {USB_DEVICE(0x093a, 0x2629)},
1230 {USB_DEVICE(0x093a, 0x262a)},
1231 {USB_DEVICE(0x093a, 0x262c)},
1234 MODULE_DEVICE_TABLE(usb
, device_table
);
1236 /* -- device connect -- */
1237 static int sd_probe(struct usb_interface
*intf
,
1238 const struct usb_device_id
*id
)
1240 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1244 static struct usb_driver sd_driver
= {
1245 .name
= MODULE_NAME
,
1246 .id_table
= device_table
,
1248 .disconnect
= gspca_disconnect
,
1250 .suspend
= gspca_suspend
,
1251 .resume
= gspca_resume
,
1255 /* -- module insert / remove -- */
1256 static int __init
sd_mod_init(void)
1259 ret
= usb_register(&sd_driver
);
1262 PDEBUG(D_PROBE
, "registered");
1265 static void __exit
sd_mod_exit(void)
1267 usb_deregister(&sd_driver
);
1268 PDEBUG(D_PROBE
, "deregistered");
1271 module_init(sd_mod_init
);
1272 module_exit(sd_mod_exit
);