V4L/DVB (8157): gspca: all subdrivers
[linux-2.6.git] / drivers / media / video / gspca / pac207.c
1 /*
2  * Pixart PAC207BCA library
3  *
4  * Copyright (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
5  * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6  * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
7  *
8  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  */
25
26 #define MODULE_NAME "pac207"
27
28 #include "gspca.h"
29
30 #define DRIVER_VERSION_NUMBER   KERNEL_VERSION(2, 1, 0)
31 static const char version[] = "2.1.0";
32
33 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
34 MODULE_DESCRIPTION("Pixart PAC207");
35 MODULE_LICENSE("GPL");
36
37 #define PAC207_CTRL_TIMEOUT             100  /* ms */
38
39 #define PAC207_BRIGHTNESS_MIN           0
40 #define PAC207_BRIGHTNESS_MAX           255
41 #define PAC207_BRIGHTNESS_DEFAULT       4 /* power on default: 4 */
42
43 #define PAC207_EXPOSURE_MIN             4
44 #define PAC207_EXPOSURE_MAX             26
45 #define PAC207_EXPOSURE_DEFAULT         4 /* power on default: 3 ?? */
46 #define PAC207_EXPOSURE_KNEE            11 /* 4 = 30 fps, 11 = 8, 15 = 6 */
47
48 #define PAC207_GAIN_MIN                 0
49 #define PAC207_GAIN_MAX                 31
50 #define PAC207_GAIN_DEFAULT             9 /* power on default: 9 */
51 #define PAC207_GAIN_KNEE                20
52
53 #define PAC207_AUTOGAIN_DEADZONE        30
54 /* We calculating the autogain at the end of the transfer of a frame, at this
55    moment a frame with the old settings is being transmitted, and a frame is
56    being captured with the old settings. So if we adjust the autogain we must
57    ignore atleast the 2 next frames for the new settings to come into effect
58    before doing any other adjustments */
59 #define PAC207_AUTOGAIN_IGNORE_FRAMES   3
60
61 enum pac207_line_state {
62         LINE_HEADER1,
63         LINE_HEADER2,
64         LINE_UNCOMPRESSED,
65         LINE_COMPRESSED,
66 };
67
68 struct pac207_decoder_state {
69         /* generic state */
70         u16 line_read;
71         u16 line_marker;
72         u8 line_state;
73         u8 header_read;
74         /* compression state */
75         u16 processed_bytes;
76         u8 remaining_bits;
77         s8 no_remaining_bits;
78         u8 get_abs;
79         u8 discard_byte;
80         u8 line_decode_buf[352];
81 };
82
83 /* specific webcam descriptor */
84 struct sd {
85         struct gspca_dev gspca_dev;             /* !! must be the first item */
86
87         struct pac207_decoder_state decoder_state;
88
89         u8 mode;
90
91         u8 brightness;
92         u8 exposure;
93         u8 autogain;
94         u8 gain;
95
96         u8 sof_read;
97         u8 autogain_ignore_frames;
98
99         atomic_t avg_lum;
100 };
101
102 /* V4L2 controls supported by the driver */
103 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
104 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
105 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
106 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
107 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
108 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
109 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
110 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
111
112 static struct ctrl sd_ctrls[] = {
113 #define SD_BRIGHTNESS 0
114         {
115             {
116                 .id      = V4L2_CID_BRIGHTNESS,
117                 .type    = V4L2_CTRL_TYPE_INTEGER,
118                 .name    = "Brightness",
119                 .minimum = PAC207_BRIGHTNESS_MIN,
120                 .maximum = PAC207_BRIGHTNESS_MAX,
121                 .step = 1,
122                 .default_value = PAC207_BRIGHTNESS_DEFAULT,
123                 .flags = 0,
124             },
125             .set = sd_setbrightness,
126             .get = sd_getbrightness,
127         },
128 #define SD_EXPOSURE 1
129         {
130             {
131                 .id = V4L2_CID_EXPOSURE,
132                 .type = V4L2_CTRL_TYPE_INTEGER,
133                 .name = "exposure",
134                 .minimum = PAC207_EXPOSURE_MIN,
135                 .maximum = PAC207_EXPOSURE_MAX,
136                 .step = 1,
137                 .default_value = PAC207_EXPOSURE_DEFAULT,
138                 .flags = 0,
139             },
140             .set = sd_setexposure,
141             .get = sd_getexposure,
142         },
143 #define SD_AUTOGAIN 2
144         {
145             {
146                 .id       = V4L2_CID_AUTOGAIN,
147                 .type   = V4L2_CTRL_TYPE_BOOLEAN,
148                 .name   = "Auto Gain",
149                 .minimum = 0,
150                 .maximum = 1,
151                 .step   = 1,
152                 .default_value = 1,
153                 .flags = 0,
154             },
155             .set = sd_setautogain,
156             .get = sd_getautogain,
157         },
158 #define SD_GAIN 3
159         {
160             {
161                 .id = V4L2_CID_GAIN,
162                 .type = V4L2_CTRL_TYPE_INTEGER,
163                 .name = "gain",
164                 .minimum = PAC207_GAIN_MIN,
165                 .maximum = PAC207_GAIN_MAX,
166                 .step = 1,
167                 .default_value = PAC207_GAIN_DEFAULT,
168                 .flags = 0,
169             },
170             .set = sd_setgain,
171             .get = sd_getgain,
172         },
173 };
174
175 static struct cam_mode sif_mode[] = {
176         {V4L2_PIX_FMT_SBGGR8, 176, 144, 1},
177         {V4L2_PIX_FMT_SBGGR8, 352, 288, 0},
178 };
179
180 static const __u8 pac207_sensor_init[][8] = {
181         {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0xf0},
182         {0x00, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
183         {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
184         {0x00, 0x00, 0x32, 0x00, 0x96, 0x00, 0xa2, 0x02},
185         {0x32, 0x00, 0x96, 0x00, 0xA2, 0x02, 0xaf, 0x00},
186 };
187
188                         /* 48 reg_72 Rate Control end BalSize_4a =0x36 */
189 static const __u8 PacReg72[] = { 0x00, 0x00, 0x36, 0x00 };
190
191 static const unsigned char pac207_sof_marker[5] =
192                 { 0xff, 0xff, 0x00, 0xff, 0x96 };
193
194 int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
195         const u8 *buffer, u16 length)
196 {
197         struct usb_device *udev = gspca_dev->dev;
198         int err;
199         u8 kbuf[8];
200
201         memcpy(kbuf, buffer, length);
202
203         err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
204                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
205                         0x00, index, kbuf, length, PAC207_CTRL_TIMEOUT);
206         if (err < 0)
207                 PDEBUG(D_ERR,
208                         "Failed to write registers to index 0x%04X, error %d)",
209                         index, err);
210
211         return err;
212 }
213
214
215 int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
216 {
217         struct usb_device *udev = gspca_dev->dev;
218         int err;
219
220         err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00,
221                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
222                         value, index, NULL, 0, PAC207_CTRL_TIMEOUT);
223         if (err)
224                 PDEBUG(D_ERR, "Failed to write a register (index 0x%04X,"
225                         " value 0x%02X, error %d)", index, value, err);
226
227         return err;
228 }
229
230
231 int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
232 {
233         struct usb_device *udev = gspca_dev->dev;
234         u8 buff;
235         int res;
236
237         res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
238                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
239                         0x00, index, &buff, 1, PAC207_CTRL_TIMEOUT);
240         if (res < 0) {
241                 PDEBUG(D_ERR,
242                         "Failed to read a register (index 0x%04X, error %d)",
243                         index, res);
244                 return res;
245         }
246
247         return buff;
248 }
249
250
251 /* this function is called at probe time */
252 static int sd_config(struct gspca_dev *gspca_dev,
253                         const struct usb_device_id *id)
254 {
255         struct sd *sd = (struct sd *) gspca_dev;
256         struct cam *cam;
257         u8 idreg[2];
258
259         idreg[0] = pac207_read_reg(gspca_dev, 0x0000);
260         idreg[1] = pac207_read_reg(gspca_dev, 0x0001);
261         idreg[0] = ((idreg[0] & 0x0F) << 4) | ((idreg[1] & 0xf0) >> 4);
262         idreg[1] = idreg[1] & 0x0f;
263         PDEBUG(D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X",
264                 idreg[0], idreg[1]);
265
266         if (idreg[0] != 0x27) {
267                 PDEBUG(D_PROBE, "Error invalid sensor ID!");
268                 return -ENODEV;
269         }
270
271         pac207_write_reg(gspca_dev, 0x41, 0x00);
272                                 /* Bit_0=Image Format,
273                                  * Bit_1=LED,
274                                  * Bit_2=Compression test mode enable */
275         pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
276         pac207_write_reg(gspca_dev, 0x11, 0x30); /* Analog Bias */
277
278         PDEBUG(D_PROBE,
279                 "Pixart PAC207BCA Image Processor and Control Chip detected"
280                 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
281
282         cam = &gspca_dev->cam;
283         cam->dev_name = (char *) id->driver_info;
284         cam->epaddr = 0x05;
285         cam->cam_mode = sif_mode;
286         cam->nmodes = ARRAY_SIZE(sif_mode);
287         sd->brightness = PAC207_BRIGHTNESS_DEFAULT;
288         sd->exposure = PAC207_EXPOSURE_DEFAULT;
289         sd->gain = PAC207_GAIN_DEFAULT;
290
291         return 0;
292 }
293
294 /* this function is called at open time */
295 static int sd_open(struct gspca_dev *gspca_dev)
296 {
297         struct sd *sd = (struct sd *) gspca_dev;
298
299         sd->autogain = 1;
300         return 0;
301 }
302
303 /* -- start the camera -- */
304 static void sd_start(struct gspca_dev *gspca_dev)
305 {
306         struct sd *sd = (struct sd *) gspca_dev;
307         __u8 mode;
308
309         pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */
310         pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
311         pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
312         pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
313         pac207_write_regs(gspca_dev, 0x0040, pac207_sensor_init[3], 8);
314         pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[4], 8);
315         pac207_write_regs(gspca_dev, 0x0048, PacReg72, 4);
316
317         /* Compression Balance */
318         if (gspca_dev->width == 176)
319                 pac207_write_reg(gspca_dev, 0x4a, 0xff);
320         else
321                 pac207_write_reg(gspca_dev, 0x4a, 0x88);
322         pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
323         pac207_write_reg(gspca_dev, 0x08, sd->brightness);
324
325         /* PGA global gain (Bit 4-0) */
326         pac207_write_reg(gspca_dev, 0x0e, sd->gain);
327         pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */
328
329         mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */
330         if (gspca_dev->width == 176) {  /* 176x144 */
331                 mode |= 0x01;
332                 PDEBUG(D_STREAM, "pac207_start mode 176x144");
333         } else {                                /* 352x288 */
334                 PDEBUG(D_STREAM, "pac207_start mode 352x288");
335         }
336         pac207_write_reg(gspca_dev, 0x41, mode);
337
338         pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
339         pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
340         msleep(10);
341         pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */
342
343         sd->sof_read = 0;
344         sd->autogain_ignore_frames = 0;
345         atomic_set(&sd->avg_lum, -1);
346 }
347
348 static void sd_stopN(struct gspca_dev *gspca_dev)
349 {
350         pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */
351         pac207_write_reg(gspca_dev, 0x41, 0x00); /* Turn of LED */
352         pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
353 }
354
355 static void sd_stop0(struct gspca_dev *gspca_dev)
356 {
357 }
358
359 /* this function is called at close time */
360 static void sd_close(struct gspca_dev *gspca_dev)
361 {
362 }
363
364 /* -- convert pixart frames to Bayer -- */
365 /* Sonix decompressor struct B.S.(2004) */
366 static struct {
367         u8 is_abs;
368         u8 len;
369         s8 val;
370 } table[256];
371
372 void init_pixart_decoder(void)
373 {
374         int i, is_abs, val, len;
375
376         for (i = 0; i < 256; i++) {
377                 is_abs = 0;
378                 val = 0;
379                 len = 0;
380                 if ((i & 0xC0) == 0) {
381                         /* code 00 */
382                         val = 0;
383                         len = 2;
384                 } else if ((i & 0xC0) == 0x40) {
385                         /* code 01 */
386                         val = -5;
387                         len = 2;
388                 } else if ((i & 0xC0) == 0x80) {
389                         /* code 10 */
390                         val = 5;
391                         len = 2;
392                 } else if ((i & 0xF0) == 0xC0) {
393                         /* code 1100 */
394                         val = -10;
395                         len = 4;
396                 } else if ((i & 0xF0) == 0xD0) {
397                         /* code 1101 */
398                         val = 10;
399                         len = 4;
400                 } else if ((i & 0xF8) == 0xE0) {
401                         /* code 11100 */
402                         val = -15;
403                         len = 5;
404                 } else if ((i & 0xF8) == 0xE8) {
405                         /* code 11101 */
406                         val = 15;
407                         len = 5;
408                 } else if ((i & 0xFC) == 0xF0) {
409                         /* code 111100 */
410                         val = -20;
411                         len = 6;
412                 } else if ((i & 0xFC) == 0xF4) {
413                         /* code 111101 */
414                         val = 20;
415                         len = 6;
416                 } else if ((i & 0xF8) == 0xF8) {
417                         /* code 11111xxxxxx */
418                         is_abs = 1;
419                         val = 0;
420                         len = 5;
421                 }
422                 table[i].is_abs = is_abs;
423                 table[i].val = val;
424                 table[i].len = len;
425         }
426 }
427
428 /* auto gain and exposure algorithm based on the knee algorithm described here:
429  * <http://ytse.tricolour.net/docs/LowLightOptimization.html> */
430 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
431 {
432         struct sd *sd = (struct sd *) gspca_dev;
433         int i, steps, desired_avg_lum;
434         int orig_gain = sd->gain;
435         int orig_exposure = sd->exposure;
436         int avg_lum = atomic_read(&sd->avg_lum);
437
438         if (!sd->autogain || avg_lum == -1)
439                 return;
440
441         if (sd->autogain_ignore_frames > 0) {
442                 sd->autogain_ignore_frames--;
443                 return;
444         }
445
446         /* correct desired lumination for the configured brightness */
447         desired_avg_lum = 100 + sd->brightness / 2;
448
449         /* If we are of a multiple of deadzone, do multiple step to reach the
450            desired lumination fast (with the risc of a slight overshoot) */
451         steps = abs(desired_avg_lum - avg_lum) / PAC207_AUTOGAIN_DEADZONE;
452
453         for (i = 0; i < steps; i++) {
454                 if (avg_lum > desired_avg_lum) {
455                         if (sd->gain > PAC207_GAIN_KNEE) {
456                                 sd->gain--;
457                         } else if (sd->exposure > PAC207_EXPOSURE_KNEE) {
458                                 sd->exposure--;
459                         } else if (sd->gain > PAC207_GAIN_DEFAULT) {
460                                 sd->gain--;
461                         } else if (sd->exposure > PAC207_EXPOSURE_MIN) {
462                                 sd->exposure--;
463                         } else if (sd->gain > PAC207_GAIN_MIN) {
464                                 sd->gain--;
465                         } else
466                                 break;
467                 } else {
468                         if (sd->gain < PAC207_GAIN_DEFAULT) {
469                                 sd->gain++;
470                         } else if (sd->exposure < PAC207_EXPOSURE_KNEE) {
471                                 sd->exposure++;
472                         } else if (sd->gain < PAC207_GAIN_KNEE) {
473                                 sd->gain++;
474                         } else if (sd->exposure < PAC207_EXPOSURE_MAX) {
475                                 sd->exposure++;
476                         } else if (sd->gain < PAC207_GAIN_MAX) {
477                                 sd->gain++;
478                         } else
479                                 break;
480                 }
481         }
482
483         if (sd->exposure != orig_exposure || sd->gain != orig_gain) {
484                 if (sd->exposure != orig_exposure)
485                         pac207_write_reg(gspca_dev, 0x0002, sd->exposure);
486                 if (sd->gain != orig_gain)
487                         pac207_write_reg(gspca_dev, 0x000e, sd->gain);
488                 pac207_write_reg(gspca_dev, 0x13, 0x01); /* load reg to sen */
489                 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
490                 sd->autogain_ignore_frames = PAC207_AUTOGAIN_IGNORE_FRAMES;
491         }
492 }
493
494 static unsigned char *pac207_find_sof(struct gspca_dev *gspca_dev,
495         unsigned char *m, int len)
496 {
497         struct sd *sd = (struct sd *) gspca_dev;
498         int i;
499
500         /* Search for the SOF marker (fixed part) in the header */
501         for (i = 0; i < len; i++) {
502                 if (m[i] == pac207_sof_marker[sd->sof_read]) {
503                         sd->sof_read++;
504                         if (sd->sof_read == sizeof(pac207_sof_marker)) {
505                                 PDEBUG(D_STREAM,
506                                         "SOF found, bytes to analyze: %u."
507                                         " Frame starts at byte #%u",
508                                         len, i + 1);
509                                 sd->sof_read = 0;
510                                 return m + i + 1;
511                         }
512                 } else {
513                         sd->sof_read = 0;
514                 }
515         }
516
517         return NULL;
518 }
519
520 static int pac207_decompress_row(struct gspca_dev *gspca_dev,
521         struct gspca_frame *f, unsigned char *cdata, int len)
522 {
523         struct sd *sd = (struct sd *) gspca_dev;
524         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
525         unsigned char *outp = decoder_state->line_decode_buf +
526                                 decoder_state->line_read;
527         int val, bitlen, bitpos = -decoder_state->no_remaining_bits;
528         u8 code;
529
530         /* first two pixels are stored as raw 8-bit */
531         while (decoder_state->line_read < 2) {
532                 *outp++ = *cdata++;
533                 decoder_state->line_read++;
534                 len--;
535                 if (len == 0)
536                         return 0;
537         }
538
539         while (decoder_state->line_read < gspca_dev->width) {
540                 if (bitpos < 0) {
541                         code = decoder_state->remaining_bits << (8 + bitpos) |
542                                 cdata[0] >> -bitpos;
543                 } else {
544                         u8 *addr = cdata + bitpos / 8;
545                         code = addr[0] << (bitpos & 7) |
546                                 addr[1] >> (8 - (bitpos & 7));
547                 }
548
549                 bitlen = decoder_state->get_abs ?
550                                 6 : table[code].len;
551
552                 /* Stop decompressing if we're out of input data */
553                 if ((bitpos + bitlen) > (len * 8))
554                         break;
555
556                 if (decoder_state->get_abs) {
557                         *outp++ = code & 0xFC;
558                         decoder_state->line_read++;
559                         decoder_state->get_abs = 0;
560                 } else {
561                         if (table[code].is_abs) {
562                                 decoder_state->get_abs = 1;
563                         } else {
564                                 /* relative to left pixel */
565                                 val = outp[-2] +
566                                         table[code].val;
567                                 if (val > 0xff)
568                                         val = 0xff;
569                                 else if (val < 0)
570                                         val = 0;
571                                 *outp++ = val;
572                                 decoder_state->line_read++;
573                         }
574                 }
575                 bitpos += bitlen;
576         }
577
578         if (decoder_state->line_read == gspca_dev->width) {
579                 int compressed_line_len;
580
581                 gspca_frame_add(gspca_dev, INTER_PACKET, f,
582                                 decoder_state->line_decode_buf,
583                                 gspca_dev->width);
584
585                 /* completely decompressed line, round pos to nearest word */
586                 compressed_line_len = ((decoder_state->processed_bytes * 8 +
587                         bitpos + 15) / 16) * 2;
588
589                 len -= compressed_line_len - decoder_state->processed_bytes;
590                 if (len < 0) {
591                         decoder_state->discard_byte = 1;
592                         len = 0;
593                 }
594         } else {
595                 decoder_state->processed_bytes += len;
596                 decoder_state->remaining_bits = cdata[bitpos/8];
597                 decoder_state->no_remaining_bits = (8 - bitpos) & 7;
598                 len = 0;
599         }
600
601         return len;
602 }
603
604 static void pac207_decode_line_init(struct gspca_dev *gspca_dev)
605 {
606         struct sd *sd = (struct sd *) gspca_dev;
607         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
608
609         decoder_state->line_read = 0;
610         decoder_state->line_state = LINE_HEADER1;
611         decoder_state->processed_bytes = 0;
612         decoder_state->no_remaining_bits = 0;
613         decoder_state->get_abs = 0;
614 }
615
616 static void pac207_decode_frame_init(struct gspca_dev *gspca_dev)
617 {
618         struct sd *sd = (struct sd *) gspca_dev;
619         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
620
621         decoder_state->header_read = 0;
622         decoder_state->discard_byte = 0;
623
624         pac207_decode_line_init(gspca_dev);
625 }
626
627 static int pac207_decode_frame_data(struct gspca_dev *gspca_dev,
628         struct gspca_frame *f, unsigned char *data, int len)
629 {
630         struct sd *sd = (struct sd *) gspca_dev;
631         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
632         int needed = 0;
633
634         /* first 11 bytes after sof marker: frame header */
635         if (decoder_state->header_read < 11) {
636                 /* get average lumination from frame header (byte 5) */
637                 if (decoder_state->header_read < 5) {
638                         needed = 5 - decoder_state->header_read;
639                         if (len >= needed)
640                                 atomic_set(&sd->avg_lum, data[needed-1]);
641                 }
642                 /* skip the rest of the header */
643                 needed = 11 - decoder_state->header_read;
644                 if (len <= needed) {
645                         decoder_state->header_read += len;
646                         return 0;
647                 }
648                 data += needed;
649                 len -= needed;
650                 decoder_state->header_read = 11;
651         }
652
653         while (len) {
654                 if (decoder_state->discard_byte) {
655                         data++;
656                         len--;
657                         decoder_state->discard_byte = 0;
658                         continue;
659                 }
660
661                 switch (decoder_state->line_state) {
662                 case LINE_HEADER1:
663                         decoder_state->line_marker = data[0] << 8;
664                         decoder_state->line_state = LINE_HEADER2;
665                         needed = 1;
666                         break;
667                 case LINE_HEADER2:
668                         decoder_state->line_marker |= data[0];
669                         switch (decoder_state->line_marker) {
670                         case 0x0FF0:
671                                 decoder_state->line_state = LINE_UNCOMPRESSED;
672                                 break;
673                         case 0x1EE1:
674                                 decoder_state->line_state = LINE_COMPRESSED;
675                                 break;
676                         default:
677                                 PDEBUG(D_STREAM,
678                                         "Error unknown line-header %04X",
679                                         (int) decoder_state->line_marker);
680                                 gspca_dev->last_packet_type = DISCARD_PACKET;
681                                 return 0;
682                         }
683                         needed = 1;
684                         break;
685                 case LINE_UNCOMPRESSED:
686                         needed = gspca_dev->width - decoder_state->line_read;
687                         if (needed > len)
688                                 needed = len;
689                         gspca_frame_add(gspca_dev, INTER_PACKET, f, data,
690                                 needed);
691                         decoder_state->line_read += needed;
692                         break;
693                 case LINE_COMPRESSED:
694                         needed = len -
695                                 pac207_decompress_row(gspca_dev, f, data, len);
696                         break;
697                 }
698
699                 data += needed;
700                 len -= needed;
701
702                 if (decoder_state->line_read == gspca_dev->width) {
703                         if ((f->data_end - f->data) ==
704                                 (gspca_dev->width * gspca_dev->height)) {
705                                 /* eureka we've got a frame */
706                                 return 1;
707                         }
708                         pac207_decode_line_init(gspca_dev);
709                 }
710         }
711
712         return 0;
713 }
714
715 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
716                         struct gspca_frame *frame,
717                         unsigned char *data,
718                         int len)
719 {
720         unsigned char *sof;
721         int n;
722
723         sof = pac207_find_sof(gspca_dev, data, len);
724
725         if (sof) {
726                 /* finish decoding current frame */
727                 if (gspca_dev->last_packet_type == INTER_PACKET) {
728                         n = sof - data;
729                         if (n > sizeof(pac207_sof_marker))
730                                 n -= sizeof(pac207_sof_marker);
731                         else
732                                 n = 0;
733                         n = pac207_decode_frame_data(gspca_dev, frame,
734                                                         data, n);
735                         if (n)
736                                 frame = gspca_frame_add(gspca_dev,
737                                                 LAST_PACKET,
738                                                 frame,
739                                                 NULL,
740                                                 0);
741                         else
742                                 PDEBUG(D_STREAM, "Incomplete frame");
743                 }
744                 pac207_decode_frame_init(gspca_dev);
745                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
746                 len -= sof - data;
747                 data = sof;
748         }
749
750         if (gspca_dev->last_packet_type == DISCARD_PACKET)
751                 return;
752
753         n = pac207_decode_frame_data(gspca_dev, frame, data, len);
754         if (n)
755                 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
756                                         frame, NULL, 0);
757 }
758
759 static void setbrightness(struct gspca_dev *gspca_dev)
760 {
761         struct sd *sd = (struct sd *) gspca_dev;
762
763         pac207_write_reg(gspca_dev, 0x08, sd->brightness);
764         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
765         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
766 }
767
768 static void setexposure(struct gspca_dev *gspca_dev)
769 {
770         struct sd *sd = (struct sd *) gspca_dev;
771
772         pac207_write_reg(gspca_dev, 0x02, sd->exposure);
773         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
774         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
775 }
776
777 static void setgain(struct gspca_dev *gspca_dev)
778 {
779         struct sd *sd = (struct sd *) gspca_dev;
780
781         pac207_write_reg(gspca_dev, 0x0e, sd->gain);
782         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
783         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
784 }
785
786 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
787 {
788         struct sd *sd = (struct sd *) gspca_dev;
789
790         sd->brightness = val;
791         if (gspca_dev->streaming)
792                 setbrightness(gspca_dev);
793         return 0;
794 }
795
796 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
797 {
798         struct sd *sd = (struct sd *) gspca_dev;
799
800         *val = sd->brightness;
801         return 0;
802 }
803
804 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
805 {
806         struct sd *sd = (struct sd *) gspca_dev;
807
808         /* don't allow mucking with exposure when using autogain */
809         if (sd->autogain)
810                 return -EINVAL;
811
812         sd->exposure = val;
813         if (gspca_dev->streaming)
814                 setexposure(gspca_dev);
815         return 0;
816 }
817
818 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
819 {
820         struct sd *sd = (struct sd *) gspca_dev;
821
822         *val = sd->exposure;
823         return 0;
824 }
825
826 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
827 {
828         struct sd *sd = (struct sd *) gspca_dev;
829
830         /* don't allow mucking with gain when using autogain */
831         if (sd->autogain)
832                 return -EINVAL;
833
834         sd->gain = val;
835         if (gspca_dev->streaming)
836                 setgain(gspca_dev);
837         return 0;
838 }
839
840 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
841 {
842         struct sd *sd = (struct sd *) gspca_dev;
843
844         *val = sd->gain;
845         return 0;
846 }
847
848 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
849 {
850         struct sd *sd = (struct sd *) gspca_dev;
851
852         sd->autogain = val;
853         /* when switching to autogain set defaults to make sure
854            we are on a valid point of the autogain gain /
855            exposure knee graph, and give this change time to
856            take effect before doing autogain. */
857         if (sd->autogain) {
858                 sd->exposure = PAC207_EXPOSURE_DEFAULT;
859                 sd->gain = PAC207_GAIN_DEFAULT;
860                 if (gspca_dev->streaming) {
861                         sd->autogain_ignore_frames =
862                                 PAC207_AUTOGAIN_IGNORE_FRAMES;
863                         setexposure(gspca_dev);
864                         setgain(gspca_dev);
865                 }
866         }
867
868         return 0;
869 }
870
871 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
872 {
873         struct sd *sd = (struct sd *) gspca_dev;
874
875         *val = sd->autogain;
876         return 0;
877 }
878
879 /* sub-driver description */
880 static struct sd_desc sd_desc = {
881         .name = MODULE_NAME,
882         .ctrls = sd_ctrls,
883         .nctrls = ARRAY_SIZE(sd_ctrls),
884         .config = sd_config,
885         .open = sd_open,
886         .start = sd_start,
887         .stopN = sd_stopN,
888         .stop0 = sd_stop0,
889         .close = sd_close,
890         .dq_callback = pac207_do_auto_gain,
891         .pkt_scan = sd_pkt_scan,
892 };
893
894 /* -- module initialisation -- */
895 #define DVNM(name) .driver_info = (kernel_ulong_t) name
896 static __devinitdata struct usb_device_id device_table[] = {
897         {USB_DEVICE(0x041e, 0x4028), DVNM("Creative Webcam Vista Plus")},
898         {USB_DEVICE(0x093a, 0x2460), DVNM("Q-Tec Webcam 100")},
899         {USB_DEVICE(0x093a, 0x2463), DVNM("Philips spc200nc pac207")},
900         {USB_DEVICE(0x093a, 0x2464), DVNM("Labtec Webcam 1200")},
901         {USB_DEVICE(0x093a, 0x2468), DVNM("PAC207")},
902         {USB_DEVICE(0x093a, 0x2470), DVNM("Genius GF112")},
903         {USB_DEVICE(0x093a, 0x2471), DVNM("Genius VideoCam GE111")},
904         {USB_DEVICE(0x093a, 0x2472), DVNM("Genius VideoCam GE110")},
905         {USB_DEVICE(0x2001, 0xf115), DVNM("D-Link DSB-C120")},
906         {}
907 };
908 MODULE_DEVICE_TABLE(usb, device_table);
909
910 /* -- device connect -- */
911 static int sd_probe(struct usb_interface *intf,
912                         const struct usb_device_id *id)
913 {
914         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
915                                 THIS_MODULE);
916 }
917
918 static struct usb_driver sd_driver = {
919         .name = MODULE_NAME,
920         .id_table = device_table,
921         .probe = sd_probe,
922         .disconnect = gspca_disconnect,
923 };
924
925 /* -- module insert / remove -- */
926 static int __init sd_mod_init(void)
927 {
928         init_pixart_decoder();
929         if (usb_register(&sd_driver) < 0)
930                 return -1;
931         PDEBUG(D_PROBE, "v%s registered", version);
932         return 0;
933 }
934 static void __exit sd_mod_exit(void)
935 {
936         usb_deregister(&sd_driver);
937         PDEBUG(D_PROBE, "deregistered");
938 }
939
940 module_init(sd_mod_init);
941 module_exit(sd_mod_exit);