V4L/DVB: gspca - pac7302: Use usb_err to propagate USB errors
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / gspca / pac7302.c
blob4fa604a0457fcca309c4d401ed5cc8d13ddb346d
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>
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
12 * any later version.
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
28 matching IC.
30 Register page 1:
32 Address Description
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
40 Register page 3/4:
42 Address Description
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()
64 3 | 0x10 | setgain()
65 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
66 3 | 0x21 | sethvflip()
69 #define MODULE_NAME "pac7302"
71 #include <media/v4l2-chip-ident.h>
72 #include "gspca.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 */
79 struct sd {
80 struct gspca_dev gspca_dev; /* !! must be the first item */
82 unsigned char brightness;
83 unsigned char contrast;
84 unsigned char colors;
85 unsigned char white_balance;
86 unsigned char red_balance;
87 unsigned char blue_balance;
88 unsigned char gain;
89 unsigned char exposure;
90 unsigned char autogain;
91 __u8 hflip;
92 __u8 vflip;
93 u8 flags;
94 #define FL_HFLIP 0x01 /* mirrored by default */
95 #define FL_VFLIP 0x02 /* vertical flipped by default */
97 u8 sof_read;
98 u8 autogain_ignore_frames;
100 atomic_t avg_lum;
103 /* V4L2 controls supported by the driver */
104 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
105 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
106 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
107 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
108 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
109 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
110 static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val);
111 static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val);
112 static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val);
113 static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val);
114 static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val);
115 static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val);
116 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
117 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
118 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
119 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
120 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
121 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
122 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
123 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
124 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
125 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
127 static const struct ctrl sd_ctrls[] = {
128 /* This control is pac7302 only */
131 .id = V4L2_CID_BRIGHTNESS,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Brightness",
134 .minimum = 0,
135 #define BRIGHTNESS_MAX 0x20
136 .maximum = BRIGHTNESS_MAX,
137 .step = 1,
138 #define BRIGHTNESS_DEF 0x10
139 .default_value = BRIGHTNESS_DEF,
141 .set = sd_setbrightness,
142 .get = sd_getbrightness,
144 /* This control is for both the 7302 and the 7311 */
147 .id = V4L2_CID_CONTRAST,
148 .type = V4L2_CTRL_TYPE_INTEGER,
149 .name = "Contrast",
150 .minimum = 0,
151 #define CONTRAST_MAX 255
152 .maximum = CONTRAST_MAX,
153 .step = 1,
154 #define CONTRAST_DEF 127
155 .default_value = CONTRAST_DEF,
157 .set = sd_setcontrast,
158 .get = sd_getcontrast,
160 /* This control is pac7302 only */
163 .id = V4L2_CID_SATURATION,
164 .type = V4L2_CTRL_TYPE_INTEGER,
165 .name = "Saturation",
166 .minimum = 0,
167 #define COLOR_MAX 255
168 .maximum = COLOR_MAX,
169 .step = 1,
170 #define COLOR_DEF 127
171 .default_value = COLOR_DEF,
173 .set = sd_setcolors,
174 .get = sd_getcolors,
178 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
179 .type = V4L2_CTRL_TYPE_INTEGER,
180 .name = "White Balance",
181 .minimum = 0,
182 .maximum = 255,
183 .step = 1,
184 #define WHITEBALANCE_DEF 4
185 .default_value = WHITEBALANCE_DEF,
187 .set = sd_setwhitebalance,
188 .get = sd_getwhitebalance,
192 .id = V4L2_CID_RED_BALANCE,
193 .type = V4L2_CTRL_TYPE_INTEGER,
194 .name = "Red",
195 .minimum = 0,
196 .maximum = 3,
197 .step = 1,
198 #define REDBALANCE_DEF 1
199 .default_value = REDBALANCE_DEF,
201 .set = sd_setredbalance,
202 .get = sd_getredbalance,
206 .id = V4L2_CID_BLUE_BALANCE,
207 .type = V4L2_CTRL_TYPE_INTEGER,
208 .name = "Blue",
209 .minimum = 0,
210 .maximum = 3,
211 .step = 1,
212 #define BLUEBALANCE_DEF 1
213 .default_value = BLUEBALANCE_DEF,
215 .set = sd_setbluebalance,
216 .get = sd_getbluebalance,
218 /* All controls below are for both the 7302 and the 7311 */
221 .id = V4L2_CID_GAIN,
222 .type = V4L2_CTRL_TYPE_INTEGER,
223 .name = "Gain",
224 .minimum = 0,
225 #define GAIN_MAX 255
226 .maximum = GAIN_MAX,
227 .step = 1,
228 #define GAIN_DEF 127
229 #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
230 .default_value = GAIN_DEF,
232 .set = sd_setgain,
233 .get = sd_getgain,
237 .id = V4L2_CID_EXPOSURE,
238 .type = V4L2_CTRL_TYPE_INTEGER,
239 .name = "Exposure",
240 .minimum = 0,
241 #define EXPOSURE_MAX 255
242 .maximum = EXPOSURE_MAX,
243 .step = 1,
244 #define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
245 #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
246 .default_value = EXPOSURE_DEF,
248 .set = sd_setexposure,
249 .get = sd_getexposure,
253 .id = V4L2_CID_AUTOGAIN,
254 .type = V4L2_CTRL_TYPE_BOOLEAN,
255 .name = "Auto Gain",
256 .minimum = 0,
257 .maximum = 1,
258 .step = 1,
259 #define AUTOGAIN_DEF 1
260 .default_value = AUTOGAIN_DEF,
262 .set = sd_setautogain,
263 .get = sd_getautogain,
267 .id = V4L2_CID_HFLIP,
268 .type = V4L2_CTRL_TYPE_BOOLEAN,
269 .name = "Mirror",
270 .minimum = 0,
271 .maximum = 1,
272 .step = 1,
273 #define HFLIP_DEF 0
274 .default_value = HFLIP_DEF,
276 .set = sd_sethflip,
277 .get = sd_gethflip,
281 .id = V4L2_CID_VFLIP,
282 .type = V4L2_CTRL_TYPE_BOOLEAN,
283 .name = "Vflip",
284 .minimum = 0,
285 .maximum = 1,
286 .step = 1,
287 #define VFLIP_DEF 0
288 .default_value = VFLIP_DEF,
290 .set = sd_setvflip,
291 .get = sd_getvflip,
295 static const struct v4l2_pix_format vga_mode[] = {
296 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
297 .bytesperline = 640,
298 .sizeimage = 640 * 480 * 3 / 8 + 590,
299 .colorspace = V4L2_COLORSPACE_JPEG,
300 .priv = 0},
303 #define LOAD_PAGE3 255
304 #define END_OF_SEQUENCE 0
306 /* pac 7302 */
307 static const __u8 init_7302[] = {
308 /* index,value */
309 0xff, 0x01, /* page 1 */
310 0x78, 0x00, /* deactivate */
311 0xff, 0x01,
312 0x78, 0x40, /* led off */
314 static const __u8 start_7302[] = {
315 /* index, len, [value]* */
316 0xff, 1, 0x00, /* page 0 */
317 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
318 0x00, 0x00, 0x00, 0x00,
319 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
320 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
321 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
322 0x26, 2, 0xaa, 0xaa,
323 0x2e, 1, 0x31,
324 0x38, 1, 0x01,
325 0x3a, 3, 0x14, 0xff, 0x5a,
326 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
327 0x00, 0x54, 0x11,
328 0x55, 1, 0x00,
329 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
330 0x6b, 1, 0x00,
331 0x6e, 3, 0x08, 0x06, 0x00,
332 0x72, 3, 0x00, 0xff, 0x00,
333 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
334 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
335 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
336 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
337 0xd2, 0xeb,
338 0xaf, 1, 0x02,
339 0xb5, 2, 0x08, 0x08,
340 0xb8, 2, 0x08, 0x88,
341 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
342 0xcc, 1, 0x00,
343 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
344 0xc1, 0xd7, 0xec,
345 0xdc, 1, 0x01,
346 0xff, 1, 0x01, /* page 1 */
347 0x12, 3, 0x02, 0x00, 0x01,
348 0x3e, 2, 0x00, 0x00,
349 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
350 0x7c, 1, 0x00,
351 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
352 0x02, 0x00,
353 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
354 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
355 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
356 0xd8, 1, 0x01,
357 0xdb, 2, 0x00, 0x01,
358 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
359 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
360 0xeb, 1, 0x00,
361 0xff, 1, 0x02, /* page 2 */
362 0x22, 1, 0x00,
363 0xff, 1, 0x03, /* page 3 */
364 0, LOAD_PAGE3, /* load the page 3 */
365 0x11, 1, 0x01,
366 0xff, 1, 0x02, /* page 2 */
367 0x13, 1, 0x00,
368 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
369 0x27, 2, 0x14, 0x0c,
370 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
371 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
372 0x6e, 1, 0x08,
373 0xff, 1, 0x01, /* page 1 */
374 0x78, 1, 0x00,
375 0, END_OF_SEQUENCE /* end of sequence */
378 #define SKIP 0xaa
379 /* page 3 - the value SKIP says skip the index - see reg_w_page() */
380 static const __u8 page3_7302[] = {
381 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
382 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
383 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
384 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
385 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
386 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
387 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
388 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
390 SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
391 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
393 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
395 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
396 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
397 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
398 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
400 0x00
403 static void reg_w_buf(struct gspca_dev *gspca_dev,
404 __u8 index,
405 const char *buffer, int len)
407 int ret;
409 if (gspca_dev->usb_err < 0)
410 return;
411 memcpy(gspca_dev->usb_buf, buffer, len);
412 ret = usb_control_msg(gspca_dev->dev,
413 usb_sndctrlpipe(gspca_dev->dev, 0),
414 1, /* request */
415 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
416 0, /* value */
417 index, gspca_dev->usb_buf, len,
418 500);
419 if (ret < 0) {
420 PDEBUG(D_ERR, "reg_w_buf(): "
421 "Failed to write registers to index 0x%x, error %i",
422 index, ret);
423 gspca_dev->usb_err = ret;
428 static void reg_w(struct gspca_dev *gspca_dev,
429 __u8 index,
430 __u8 value)
432 int ret;
434 if (gspca_dev->usb_err < 0)
435 return;
436 gspca_dev->usb_buf[0] = value;
437 ret = usb_control_msg(gspca_dev->dev,
438 usb_sndctrlpipe(gspca_dev->dev, 0),
439 0, /* request */
440 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
441 0, index, gspca_dev->usb_buf, 1,
442 500);
443 if (ret < 0) {
444 PDEBUG(D_ERR, "reg_w(): "
445 "Failed to write register to index 0x%x, value 0x%x, error %i",
446 index, value, ret);
447 gspca_dev->usb_err = ret;
451 static void reg_w_seq(struct gspca_dev *gspca_dev,
452 const __u8 *seq, int len)
454 while (--len >= 0) {
455 reg_w(gspca_dev, seq[0], seq[1]);
456 seq += 2;
460 /* load the beginning of a page */
461 static void reg_w_page(struct gspca_dev *gspca_dev,
462 const __u8 *page, int len)
464 int index;
465 int ret = 0;
467 if (gspca_dev->usb_err < 0)
468 return;
469 for (index = 0; index < len; index++) {
470 if (page[index] == SKIP) /* skip this index */
471 continue;
472 gspca_dev->usb_buf[0] = page[index];
473 ret = usb_control_msg(gspca_dev->dev,
474 usb_sndctrlpipe(gspca_dev->dev, 0),
475 0, /* request */
476 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
477 0, index, gspca_dev->usb_buf, 1,
478 500);
479 if (ret < 0) {
480 PDEBUG(D_ERR, "reg_w_page(): "
481 "Failed to write register to index 0x%x, "
482 "value 0x%x, error %i",
483 index, page[index], ret);
484 gspca_dev->usb_err = ret;
485 break;
490 /* output a variable sequence */
491 static void reg_w_var(struct gspca_dev *gspca_dev,
492 const __u8 *seq,
493 const __u8 *page3, unsigned int page3_len)
495 int index, len;
497 for (;;) {
498 index = *seq++;
499 len = *seq++;
500 switch (len) {
501 case END_OF_SEQUENCE:
502 return;
503 case LOAD_PAGE3:
504 reg_w_page(gspca_dev, page3, page3_len);
505 break;
506 default:
507 if (len > USB_BUF_SZ) {
508 PDEBUG(D_ERR|D_STREAM,
509 "Incorrect variable sequence");
510 return;
512 while (len > 0) {
513 if (len < 8) {
514 reg_w_buf(gspca_dev,
515 index, seq, len);
516 seq += len;
517 break;
519 reg_w_buf(gspca_dev, index, seq, 8);
520 seq += 8;
521 index += 8;
522 len -= 8;
526 /* not reached */
529 /* this function is called at probe time for pac7302 */
530 static int sd_config(struct gspca_dev *gspca_dev,
531 const struct usb_device_id *id)
533 struct sd *sd = (struct sd *) gspca_dev;
534 struct cam *cam;
536 cam = &gspca_dev->cam;
538 PDEBUG(D_CONF, "Find Sensor PAC7302");
539 cam->cam_mode = vga_mode; /* only 640x480 */
540 cam->nmodes = ARRAY_SIZE(vga_mode);
542 sd->brightness = BRIGHTNESS_DEF;
543 sd->contrast = CONTRAST_DEF;
544 sd->colors = COLOR_DEF;
545 sd->white_balance = WHITEBALANCE_DEF;
546 sd->red_balance = REDBALANCE_DEF;
547 sd->blue_balance = BLUEBALANCE_DEF;
548 sd->gain = GAIN_DEF;
549 sd->exposure = EXPOSURE_DEF;
550 sd->autogain = AUTOGAIN_DEF;
551 sd->hflip = HFLIP_DEF;
552 sd->vflip = VFLIP_DEF;
553 sd->flags = id->driver_info;
554 return 0;
557 /* This function is used by pac7302 only */
558 static void setbrightcont(struct gspca_dev *gspca_dev)
560 struct sd *sd = (struct sd *) gspca_dev;
561 int i, v;
562 static const __u8 max[10] =
563 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
564 0xd4, 0xec};
565 static const __u8 delta[10] =
566 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
567 0x11, 0x0b};
569 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
570 for (i = 0; i < 10; i++) {
571 v = max[i];
572 v += (sd->brightness - BRIGHTNESS_MAX)
573 * 150 / BRIGHTNESS_MAX; /* 200 ? */
574 v -= delta[i] * sd->contrast / CONTRAST_MAX;
575 if (v < 0)
576 v = 0;
577 else if (v > 0xff)
578 v = 0xff;
579 reg_w(gspca_dev, 0xa2 + i, v);
581 reg_w(gspca_dev, 0xdc, 0x01);
584 /* This function is used by pac7302 only */
585 static void setcolors(struct gspca_dev *gspca_dev)
587 struct sd *sd = (struct sd *) gspca_dev;
588 int i, v;
589 static const int a[9] =
590 {217, -212, 0, -101, 170, -67, -38, -315, 355};
591 static const int b[9] =
592 {19, 106, 0, 19, 106, 1, 19, 106, 1};
594 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
595 reg_w(gspca_dev, 0x11, 0x01);
596 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
597 for (i = 0; i < 9; i++) {
598 v = a[i] * sd->colors / COLOR_MAX + b[i];
599 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
600 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
602 reg_w(gspca_dev, 0xdc, 0x01);
603 PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
606 static void setwhitebalance(struct gspca_dev *gspca_dev)
608 struct sd *sd = (struct sd *) gspca_dev;
610 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
611 reg_w(gspca_dev, 0xc6, sd->white_balance);
613 reg_w(gspca_dev, 0xdc, 0x01);
614 PDEBUG(D_CONF|D_STREAM, "white_balance: %i", sd->white_balance);
617 static void setredbalance(struct gspca_dev *gspca_dev)
619 struct sd *sd = (struct sd *) gspca_dev;
621 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
622 reg_w(gspca_dev, 0xc5, sd->red_balance);
624 reg_w(gspca_dev, 0xdc, 0x01);
625 PDEBUG(D_CONF|D_STREAM, "red_balance: %i", sd->red_balance);
628 static void setbluebalance(struct gspca_dev *gspca_dev)
630 struct sd *sd = (struct sd *) gspca_dev;
632 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
633 reg_w(gspca_dev, 0xc7, sd->blue_balance);
635 reg_w(gspca_dev, 0xdc, 0x01);
636 PDEBUG(D_CONF|D_STREAM, "blue_balance: %i", sd->blue_balance);
639 static void setgain(struct gspca_dev *gspca_dev)
641 struct sd *sd = (struct sd *) gspca_dev;
643 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
644 reg_w(gspca_dev, 0x10, sd->gain >> 3);
646 /* load registers to sensor (Bit 0, auto clear) */
647 reg_w(gspca_dev, 0x11, 0x01);
650 static void setexposure(struct gspca_dev *gspca_dev)
652 struct sd *sd = (struct sd *) gspca_dev;
653 __u8 reg;
655 /* register 2 of frame 3/4 contains the clock divider configuring the
656 no fps according to the formula: 60 / reg. sd->exposure is the
657 desired exposure time in ms. */
658 reg = 120 * sd->exposure / 1000;
659 if (reg < 2)
660 reg = 2;
661 else if (reg > 63)
662 reg = 63;
664 /* On the pac7302 reg2 MUST be a multiple of 3, so round it to
665 the nearest multiple of 3, except when between 6 and 12? */
666 if (reg < 6 || reg > 12)
667 reg = ((reg + 1) / 3) * 3;
668 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
669 reg_w(gspca_dev, 0x02, reg);
671 /* load registers to sensor (Bit 0, auto clear) */
672 reg_w(gspca_dev, 0x11, 0x01);
675 static void sethvflip(struct gspca_dev *gspca_dev)
677 struct sd *sd = (struct sd *) gspca_dev;
678 u8 data, hflip, vflip;
680 hflip = sd->hflip;
681 if (sd->flags & FL_HFLIP)
682 hflip = !hflip;
683 vflip = sd->vflip;
684 if (sd->flags & FL_VFLIP)
685 vflip = !vflip;
687 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
688 data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
689 reg_w(gspca_dev, 0x21, data);
691 /* load registers to sensor (Bit 0, auto clear) */
692 reg_w(gspca_dev, 0x11, 0x01);
695 /* this function is called at probe and resume time for pac7302 */
696 static int sd_init(struct gspca_dev *gspca_dev)
698 reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
699 return gspca_dev->usb_err;
702 static int sd_start(struct gspca_dev *gspca_dev)
704 struct sd *sd = (struct sd *) gspca_dev;
706 sd->sof_read = 0;
708 reg_w_var(gspca_dev, start_7302,
709 page3_7302, sizeof(page3_7302));
710 setbrightcont(gspca_dev);
711 setcolors(gspca_dev);
712 setwhitebalance(gspca_dev);
713 setredbalance(gspca_dev);
714 setbluebalance(gspca_dev);
715 setgain(gspca_dev);
716 setexposure(gspca_dev);
717 sethvflip(gspca_dev);
719 /* only resolution 640x480 is supported for pac7302 */
721 sd->sof_read = 0;
722 sd->autogain_ignore_frames = 0;
723 atomic_set(&sd->avg_lum, -1);
725 /* start stream */
726 reg_w(gspca_dev, 0xff, 0x01);
727 reg_w(gspca_dev, 0x78, 0x01);
729 return gspca_dev->usb_err;
732 static void sd_stopN(struct gspca_dev *gspca_dev)
735 /* stop stream */
736 reg_w(gspca_dev, 0xff, 0x01);
737 reg_w(gspca_dev, 0x78, 0x00);
740 /* called on streamoff with alt 0 and on disconnect for pac7302 */
741 static void sd_stop0(struct gspca_dev *gspca_dev)
743 if (!gspca_dev->present)
744 return;
745 reg_w(gspca_dev, 0xff, 0x01);
746 reg_w(gspca_dev, 0x78, 0x40);
749 /* Include pac common sof detection functions */
750 #include "pac_common.h"
752 static void do_autogain(struct gspca_dev *gspca_dev)
754 struct sd *sd = (struct sd *) gspca_dev;
755 int avg_lum = atomic_read(&sd->avg_lum);
756 int desired_lum, deadzone;
758 if (avg_lum == -1)
759 return;
761 desired_lum = 270 + sd->brightness * 4;
762 /* Hack hack, with the 7202 the first exposure step is
763 pretty large, so if we're about to make the first
764 exposure increase make the deadzone large to avoid
765 oscilating */
766 if (desired_lum > avg_lum && sd->gain == GAIN_DEF &&
767 sd->exposure > EXPOSURE_DEF &&
768 sd->exposure < 42)
769 deadzone = 90;
770 else
771 deadzone = 30;
773 if (sd->autogain_ignore_frames > 0)
774 sd->autogain_ignore_frames--;
775 else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
776 deadzone, GAIN_KNEE, EXPOSURE_KNEE))
777 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
780 /* JPEG header, part 1 */
781 static const unsigned char pac_jpeg_header1[] = {
782 0xff, 0xd8, /* SOI: Start of Image */
784 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
785 0x00, 0x11, /* length = 17 bytes (including this length field) */
786 0x08 /* Precision: 8 */
787 /* 2 bytes is placed here: number of image lines */
788 /* 2 bytes is placed here: samples per line */
791 /* JPEG header, continued */
792 static const unsigned char pac_jpeg_header2[] = {
793 0x03, /* Number of image components: 3 */
794 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
795 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
796 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
798 0xff, 0xda, /* SOS: Start Of Scan */
799 0x00, 0x0c, /* length = 12 bytes (including this length field) */
800 0x03, /* number of components: 3 */
801 0x01, 0x00, /* selector 1, table 0x00 */
802 0x02, 0x11, /* selector 2, table 0x11 */
803 0x03, 0x11, /* selector 3, table 0x11 */
804 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
805 0x00 /* Successive approximation: 0 */
808 static void pac_start_frame(struct gspca_dev *gspca_dev,
809 struct gspca_frame *frame,
810 __u16 lines, __u16 samples_per_line)
812 unsigned char tmpbuf[4];
814 gspca_frame_add(gspca_dev, FIRST_PACKET,
815 pac_jpeg_header1, sizeof(pac_jpeg_header1));
817 tmpbuf[0] = lines >> 8;
818 tmpbuf[1] = lines & 0xff;
819 tmpbuf[2] = samples_per_line >> 8;
820 tmpbuf[3] = samples_per_line & 0xff;
822 gspca_frame_add(gspca_dev, INTER_PACKET,
823 tmpbuf, sizeof(tmpbuf));
824 gspca_frame_add(gspca_dev, INTER_PACKET,
825 pac_jpeg_header2, sizeof(pac_jpeg_header2));
828 /* this function is run at interrupt level */
829 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
830 u8 *data, /* isoc packet */
831 int len) /* iso packet length */
833 struct sd *sd = (struct sd *) gspca_dev;
834 struct gspca_frame *frame;
835 unsigned char *sof;
837 sof = pac_find_sof(&sd->sof_read, data, len);
838 if (sof) {
839 int n, lum_offset, footer_length;
841 frame = gspca_get_i_frame(gspca_dev);
842 if (frame == NULL) {
843 gspca_dev->last_packet_type = DISCARD_PACKET;
844 return;
847 /* 6 bytes after the FF D9 EOF marker a number of lumination
848 bytes are send corresponding to different parts of the
849 image, the 14th and 15th byte after the EOF seem to
850 correspond to the center of the image */
851 lum_offset = 61 + sizeof pac_sof_marker;
852 footer_length = 74;
854 /* Finish decoding current frame */
855 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
856 if (n < 0) {
857 frame->data_end += n;
858 n = 0;
860 gspca_frame_add(gspca_dev, INTER_PACKET,
861 data, n);
862 if (gspca_dev->last_packet_type != DISCARD_PACKET &&
863 frame->data_end[-2] == 0xff &&
864 frame->data_end[-1] == 0xd9)
865 gspca_frame_add(gspca_dev, LAST_PACKET,
866 NULL, 0);
868 n = sof - data;
869 len -= n;
870 data = sof;
872 /* Get average lumination */
873 if (gspca_dev->last_packet_type == LAST_PACKET &&
874 n >= lum_offset)
875 atomic_set(&sd->avg_lum, data[-lum_offset] +
876 data[-lum_offset + 1]);
877 else
878 atomic_set(&sd->avg_lum, -1);
880 /* Start the new frame with the jpeg header */
881 /* The PAC7302 has the image rotated 90 degrees */
882 pac_start_frame(gspca_dev, frame,
883 gspca_dev->width, gspca_dev->height);
885 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
888 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
890 struct sd *sd = (struct sd *) gspca_dev;
892 sd->brightness = val;
893 if (gspca_dev->streaming)
894 setbrightcont(gspca_dev);
895 return gspca_dev->usb_err;
898 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
900 struct sd *sd = (struct sd *) gspca_dev;
902 *val = sd->brightness;
903 return 0;
906 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
908 struct sd *sd = (struct sd *) gspca_dev;
910 sd->contrast = val;
911 if (gspca_dev->streaming) {
912 setbrightcont(gspca_dev);
914 return gspca_dev->usb_err;
917 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
919 struct sd *sd = (struct sd *) gspca_dev;
921 *val = sd->contrast;
922 return 0;
925 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
927 struct sd *sd = (struct sd *) gspca_dev;
929 sd->colors = val;
930 if (gspca_dev->streaming)
931 setcolors(gspca_dev);
932 return gspca_dev->usb_err;
935 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
937 struct sd *sd = (struct sd *) gspca_dev;
939 *val = sd->colors;
940 return 0;
943 static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val)
945 struct sd *sd = (struct sd *) gspca_dev;
947 sd->white_balance = val;
948 if (gspca_dev->streaming)
949 setwhitebalance(gspca_dev);
950 return gspca_dev->usb_err;
953 static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val)
955 struct sd *sd = (struct sd *) gspca_dev;
957 *val = sd->white_balance;
958 return 0;
961 static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val)
963 struct sd *sd = (struct sd *) gspca_dev;
965 sd->red_balance = val;
966 if (gspca_dev->streaming)
967 setredbalance(gspca_dev);
968 return gspca_dev->usb_err;
971 static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val)
973 struct sd *sd = (struct sd *) gspca_dev;
975 *val = sd->red_balance;
976 return 0;
979 static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val)
981 struct sd *sd = (struct sd *) gspca_dev;
983 sd->blue_balance = val;
984 if (gspca_dev->streaming)
985 setbluebalance(gspca_dev);
986 return gspca_dev->usb_err;
989 static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val)
991 struct sd *sd = (struct sd *) gspca_dev;
993 *val = sd->blue_balance;
994 return 0;
997 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
999 struct sd *sd = (struct sd *) gspca_dev;
1001 sd->gain = val;
1002 if (gspca_dev->streaming)
1003 setgain(gspca_dev);
1004 return gspca_dev->usb_err;
1007 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1009 struct sd *sd = (struct sd *) gspca_dev;
1011 *val = sd->gain;
1012 return 0;
1015 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1017 struct sd *sd = (struct sd *) gspca_dev;
1019 sd->exposure = val;
1020 if (gspca_dev->streaming)
1021 setexposure(gspca_dev);
1022 return gspca_dev->usb_err;
1025 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1027 struct sd *sd = (struct sd *) gspca_dev;
1029 *val = sd->exposure;
1030 return 0;
1033 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1035 struct sd *sd = (struct sd *) gspca_dev;
1037 sd->autogain = val;
1038 /* when switching to autogain set defaults to make sure
1039 we are on a valid point of the autogain gain /
1040 exposure knee graph, and give this change time to
1041 take effect before doing autogain. */
1042 if (sd->autogain) {
1043 sd->exposure = EXPOSURE_DEF;
1044 sd->gain = GAIN_DEF;
1045 if (gspca_dev->streaming) {
1046 sd->autogain_ignore_frames =
1047 PAC_AUTOGAIN_IGNORE_FRAMES;
1048 setexposure(gspca_dev);
1049 setgain(gspca_dev);
1053 return gspca_dev->usb_err;
1056 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1058 struct sd *sd = (struct sd *) gspca_dev;
1060 *val = sd->autogain;
1061 return 0;
1064 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1066 struct sd *sd = (struct sd *) gspca_dev;
1068 sd->hflip = val;
1069 if (gspca_dev->streaming)
1070 sethvflip(gspca_dev);
1071 return gspca_dev->usb_err;
1074 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1076 struct sd *sd = (struct sd *) gspca_dev;
1078 *val = sd->hflip;
1079 return 0;
1082 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1084 struct sd *sd = (struct sd *) gspca_dev;
1086 sd->vflip = val;
1087 if (gspca_dev->streaming)
1088 sethvflip(gspca_dev);
1089 return gspca_dev->usb_err;
1092 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1094 struct sd *sd = (struct sd *) gspca_dev;
1096 *val = sd->vflip;
1097 return 0;
1100 #ifdef CONFIG_VIDEO_ADV_DEBUG
1101 static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
1102 struct v4l2_dbg_register *reg)
1104 __u8 index;
1105 __u8 value;
1107 /* reg->reg: bit0..15: reserved for register index (wIndex is 16bit
1108 long on the USB bus)
1110 if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
1111 reg->match.addr == 0 &&
1112 (reg->reg < 0x000000ff) &&
1113 (reg->val <= 0x000000ff)
1115 /* Currently writing to page 0 is only supported. */
1116 /* reg_w() only supports 8bit index */
1117 index = reg->reg & 0x000000ff;
1118 value = reg->val & 0x000000ff;
1120 /* Note that there shall be no access to other page
1121 by any other function between the page swith and
1122 the actual register write */
1123 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
1124 reg_w(gspca_dev, index, value);
1126 reg_w(gspca_dev, 0xdc, 0x01);
1128 return gspca_dev->usb_err;
1131 static int sd_chip_ident(struct gspca_dev *gspca_dev,
1132 struct v4l2_dbg_chip_ident *chip)
1134 int ret = -EINVAL;
1136 if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
1137 chip->match.addr == 0) {
1138 chip->revision = 0;
1139 chip->ident = V4L2_IDENT_UNKNOWN;
1140 ret = 0;
1142 return ret;
1144 #endif
1146 /* sub-driver description for pac7302 */
1147 static const struct sd_desc sd_desc = {
1148 .name = MODULE_NAME,
1149 .ctrls = sd_ctrls,
1150 .nctrls = ARRAY_SIZE(sd_ctrls),
1151 .config = sd_config,
1152 .init = sd_init,
1153 .start = sd_start,
1154 .stopN = sd_stopN,
1155 .stop0 = sd_stop0,
1156 .pkt_scan = sd_pkt_scan,
1157 .dq_callback = do_autogain,
1158 #ifdef CONFIG_VIDEO_ADV_DEBUG
1159 .set_register = sd_dbg_s_register,
1160 .get_chip_ident = sd_chip_ident,
1161 #endif
1164 /* -- module initialisation -- */
1165 static const struct usb_device_id device_table[] __devinitconst = {
1166 {USB_DEVICE(0x06f8, 0x3009)},
1167 {USB_DEVICE(0x093a, 0x2620)},
1168 {USB_DEVICE(0x093a, 0x2621)},
1169 {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
1170 {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
1171 {USB_DEVICE(0x093a, 0x2626)},
1172 {USB_DEVICE(0x093a, 0x2628)},
1173 {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
1174 {USB_DEVICE(0x093a, 0x262a)},
1175 {USB_DEVICE(0x093a, 0x262c)},
1178 MODULE_DEVICE_TABLE(usb, device_table);
1180 /* -- device connect -- */
1181 static int __devinit sd_probe(struct usb_interface *intf,
1182 const struct usb_device_id *id)
1184 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1185 THIS_MODULE);
1188 static struct usb_driver sd_driver = {
1189 .name = MODULE_NAME,
1190 .id_table = device_table,
1191 .probe = sd_probe,
1192 .disconnect = gspca_disconnect,
1193 #ifdef CONFIG_PM
1194 .suspend = gspca_suspend,
1195 .resume = gspca_resume,
1196 #endif
1199 /* -- module insert / remove -- */
1200 static int __init sd_mod_init(void)
1202 int ret;
1203 ret = usb_register(&sd_driver);
1204 if (ret < 0)
1205 return ret;
1206 PDEBUG(D_PROBE, "registered");
1207 return 0;
1209 static void __exit sd_mod_exit(void)
1211 usb_deregister(&sd_driver);
1212 PDEBUG(D_PROBE, "deregistered");
1215 module_init(sd_mod_init);
1216 module_exit(sd_mod_exit);