V4L/DVB (8195): gspca: Input buffer overwritten in spca561 + cleanup code.
[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, 4)
31 static const char version[] = "2.1.4";
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,
522                                 __u8 *cdata,
523                                 int len)
524 {
525         struct sd *sd = (struct sd *) gspca_dev;
526         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
527         unsigned char *outp = decoder_state->line_decode_buf +
528                                 decoder_state->line_read;
529         int val, bitlen, bitpos = -decoder_state->no_remaining_bits;
530         u8 code;
531
532         /* first two pixels are stored as raw 8-bit */
533         while (decoder_state->line_read < 2) {
534                 *outp++ = *cdata++;
535                 decoder_state->line_read++;
536                 len--;
537                 if (len == 0)
538                         return 0;
539         }
540
541         while (decoder_state->line_read < gspca_dev->width) {
542                 if (bitpos < 0) {
543                         code = decoder_state->remaining_bits << (8 + bitpos) |
544                                 cdata[0] >> -bitpos;
545                 } else {
546                         u8 *addr = cdata + bitpos / 8;
547                         code = addr[0] << (bitpos & 7) |
548                                 addr[1] >> (8 - (bitpos & 7));
549                 }
550
551                 bitlen = decoder_state->get_abs ?
552                                 6 : table[code].len;
553
554                 /* Stop decompressing if we're out of input data */
555                 if ((bitpos + bitlen) > (len * 8))
556                         break;
557
558                 if (decoder_state->get_abs) {
559                         *outp++ = code & 0xFC;
560                         decoder_state->line_read++;
561                         decoder_state->get_abs = 0;
562                 } else {
563                         if (table[code].is_abs) {
564                                 decoder_state->get_abs = 1;
565                         } else {
566                                 /* relative to left pixel */
567                                 val = outp[-2] +
568                                         table[code].val;
569                                 if (val > 0xff)
570                                         val = 0xff;
571                                 else if (val < 0)
572                                         val = 0;
573                                 *outp++ = val;
574                                 decoder_state->line_read++;
575                         }
576                 }
577                 bitpos += bitlen;
578         }
579
580         if (decoder_state->line_read == gspca_dev->width) {
581                 int compressed_line_len;
582
583                 gspca_frame_add(gspca_dev, INTER_PACKET, f,
584                                 decoder_state->line_decode_buf,
585                                 gspca_dev->width);
586
587                 /* completely decompressed line, round pos to nearest word */
588                 compressed_line_len = ((decoder_state->processed_bytes * 8 +
589                         bitpos + 15) / 16) * 2;
590
591                 len -= compressed_line_len - decoder_state->processed_bytes;
592                 if (len < 0) {
593                         decoder_state->discard_byte = 1;
594                         len = 0;
595                 }
596         } else {
597                 decoder_state->processed_bytes += len;
598                 decoder_state->remaining_bits = cdata[bitpos/8];
599                 decoder_state->no_remaining_bits = (8 - bitpos) & 7;
600                 len = 0;
601         }
602
603         return len;
604 }
605
606 static void pac207_decode_line_init(struct gspca_dev *gspca_dev)
607 {
608         struct sd *sd = (struct sd *) gspca_dev;
609         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
610
611         decoder_state->line_read = 0;
612         decoder_state->line_state = LINE_HEADER1;
613         decoder_state->processed_bytes = 0;
614         decoder_state->no_remaining_bits = 0;
615         decoder_state->get_abs = 0;
616 }
617
618 static void pac207_decode_frame_init(struct gspca_dev *gspca_dev)
619 {
620         struct sd *sd = (struct sd *) gspca_dev;
621         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
622
623         decoder_state->header_read = 0;
624         decoder_state->discard_byte = 0;
625
626         pac207_decode_line_init(gspca_dev);
627 }
628
629 static int pac207_decode_frame_data(struct gspca_dev *gspca_dev,
630         struct gspca_frame *f, unsigned char *data, int len)
631 {
632         struct sd *sd = (struct sd *) gspca_dev;
633         struct pac207_decoder_state *decoder_state = &sd->decoder_state;
634         int needed = 0;
635
636         /* first 11 bytes after sof marker: frame header */
637         if (decoder_state->header_read < 11) {
638                 /* get average lumination from frame header (byte 5) */
639                 if (decoder_state->header_read < 5) {
640                         needed = 5 - decoder_state->header_read;
641                         if (len >= needed)
642                                 atomic_set(&sd->avg_lum, data[needed-1]);
643                 }
644                 /* skip the rest of the header */
645                 needed = 11 - decoder_state->header_read;
646                 if (len <= needed) {
647                         decoder_state->header_read += len;
648                         return 0;
649                 }
650                 data += needed;
651                 len -= needed;
652                 decoder_state->header_read = 11;
653         }
654
655         while (len) {
656                 if (decoder_state->discard_byte) {
657                         data++;
658                         len--;
659                         decoder_state->discard_byte = 0;
660                         continue;
661                 }
662
663                 switch (decoder_state->line_state) {
664                 case LINE_HEADER1:
665                         decoder_state->line_marker = data[0] << 8;
666                         decoder_state->line_state = LINE_HEADER2;
667                         needed = 1;
668                         break;
669                 case LINE_HEADER2:
670                         decoder_state->line_marker |= data[0];
671                         switch (decoder_state->line_marker) {
672                         case 0x0ff0:
673                                 decoder_state->line_state = LINE_UNCOMPRESSED;
674                                 break;
675                         case 0x1ee1:
676                                 decoder_state->line_state = LINE_COMPRESSED;
677                                 break;
678                         default:
679                                 PDEBUG(D_STREAM,
680                                         "Error unknown line-header %04X",
681                                         (int) decoder_state->line_marker);
682                                 gspca_dev->last_packet_type = DISCARD_PACKET;
683                                 return 0;
684                         }
685                         needed = 1;
686                         break;
687                 case LINE_UNCOMPRESSED:
688                         needed = gspca_dev->width - decoder_state->line_read;
689                         if (needed > len)
690                                 needed = len;
691                         gspca_frame_add(gspca_dev, INTER_PACKET, f, data,
692                                 needed);
693                         decoder_state->line_read += needed;
694                         break;
695                 case LINE_COMPRESSED:
696                         needed = len -
697                                 pac207_decompress_row(gspca_dev, f, data, len);
698                         break;
699                 }
700
701                 data += needed;
702                 len -= needed;
703
704                 if (decoder_state->line_read == gspca_dev->width) {
705                         if ((f->data_end - f->data) ==
706                                 (gspca_dev->width * gspca_dev->height)) {
707                                 /* eureka we've got a frame */
708                                 return 1;
709                         }
710                         pac207_decode_line_init(gspca_dev);
711                 }
712         }
713
714         return 0;
715 }
716
717 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
718                         struct gspca_frame *frame,
719                         __u8 *data,
720                         int len)
721 {
722         unsigned char *sof;
723         int n;
724
725         sof = pac207_find_sof(gspca_dev, data, len);
726
727         if (sof) {
728                 /* finish decoding current frame */
729                 if (gspca_dev->last_packet_type == INTER_PACKET) {
730                         n = sof - data;
731                         if (n > sizeof(pac207_sof_marker))
732                                 n -= sizeof(pac207_sof_marker);
733                         else
734                                 n = 0;
735                         n = pac207_decode_frame_data(gspca_dev, frame,
736                                                         data, n);
737                         if (n)
738                                 frame = gspca_frame_add(gspca_dev,
739                                                 LAST_PACKET,
740                                                 frame,
741                                                 NULL,
742                                                 0);
743                         else
744                                 PDEBUG(D_STREAM, "Incomplete frame");
745                 }
746                 pac207_decode_frame_init(gspca_dev);
747                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
748                 len -= sof - data;
749                 data = sof;
750         }
751
752         if (gspca_dev->last_packet_type == DISCARD_PACKET)
753                 return;
754
755         n = pac207_decode_frame_data(gspca_dev, frame, data, len);
756         if (n)
757                 gspca_frame_add(gspca_dev, LAST_PACKET,
758                                 frame, NULL, 0);
759 }
760
761 static void setbrightness(struct gspca_dev *gspca_dev)
762 {
763         struct sd *sd = (struct sd *) gspca_dev;
764
765         pac207_write_reg(gspca_dev, 0x08, sd->brightness);
766         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
767         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
768 }
769
770 static void setexposure(struct gspca_dev *gspca_dev)
771 {
772         struct sd *sd = (struct sd *) gspca_dev;
773
774         pac207_write_reg(gspca_dev, 0x02, sd->exposure);
775         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
776         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
777 }
778
779 static void setgain(struct gspca_dev *gspca_dev)
780 {
781         struct sd *sd = (struct sd *) gspca_dev;
782
783         pac207_write_reg(gspca_dev, 0x0e, sd->gain);
784         pac207_write_reg(gspca_dev, 0x13, 0x01);        /* Bit 0, auto clear */
785         pac207_write_reg(gspca_dev, 0x1c, 0x01);        /* not documented */
786 }
787
788 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
789 {
790         struct sd *sd = (struct sd *) gspca_dev;
791
792         sd->brightness = val;
793         if (gspca_dev->streaming)
794                 setbrightness(gspca_dev);
795         return 0;
796 }
797
798 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
799 {
800         struct sd *sd = (struct sd *) gspca_dev;
801
802         *val = sd->brightness;
803         return 0;
804 }
805
806 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
807 {
808         struct sd *sd = (struct sd *) gspca_dev;
809
810         /* don't allow mucking with exposure when using autogain */
811         if (sd->autogain)
812                 return -EINVAL;
813
814         sd->exposure = val;
815         if (gspca_dev->streaming)
816                 setexposure(gspca_dev);
817         return 0;
818 }
819
820 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
821 {
822         struct sd *sd = (struct sd *) gspca_dev;
823
824         *val = sd->exposure;
825         return 0;
826 }
827
828 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
829 {
830         struct sd *sd = (struct sd *) gspca_dev;
831
832         /* don't allow mucking with gain when using autogain */
833         if (sd->autogain)
834                 return -EINVAL;
835
836         sd->gain = val;
837         if (gspca_dev->streaming)
838                 setgain(gspca_dev);
839         return 0;
840 }
841
842 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
843 {
844         struct sd *sd = (struct sd *) gspca_dev;
845
846         *val = sd->gain;
847         return 0;
848 }
849
850 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
851 {
852         struct sd *sd = (struct sd *) gspca_dev;
853
854         sd->autogain = val;
855         /* when switching to autogain set defaults to make sure
856            we are on a valid point of the autogain gain /
857            exposure knee graph, and give this change time to
858            take effect before doing autogain. */
859         if (sd->autogain) {
860                 sd->exposure = PAC207_EXPOSURE_DEFAULT;
861                 sd->gain = PAC207_GAIN_DEFAULT;
862                 if (gspca_dev->streaming) {
863                         sd->autogain_ignore_frames =
864                                 PAC207_AUTOGAIN_IGNORE_FRAMES;
865                         setexposure(gspca_dev);
866                         setgain(gspca_dev);
867                 }
868         }
869
870         return 0;
871 }
872
873 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
874 {
875         struct sd *sd = (struct sd *) gspca_dev;
876
877         *val = sd->autogain;
878         return 0;
879 }
880
881 /* sub-driver description */
882 static const struct sd_desc sd_desc = {
883         .name = MODULE_NAME,
884         .ctrls = sd_ctrls,
885         .nctrls = ARRAY_SIZE(sd_ctrls),
886         .config = sd_config,
887         .open = sd_open,
888         .start = sd_start,
889         .stopN = sd_stopN,
890         .stop0 = sd_stop0,
891         .close = sd_close,
892         .dq_callback = pac207_do_auto_gain,
893         .pkt_scan = sd_pkt_scan,
894 };
895
896 /* -- module initialisation -- */
897 #define DVNM(name) .driver_info = (kernel_ulong_t) name
898 static const __devinitdata struct usb_device_id device_table[] = {
899         {USB_DEVICE(0x041e, 0x4028), DVNM("Creative Webcam Vista Plus")},
900         {USB_DEVICE(0x093a, 0x2460), DVNM("Q-Tec Webcam 100")},
901         {USB_DEVICE(0x093a, 0x2463), DVNM("Philips spc200nc pac207")},
902         {USB_DEVICE(0x093a, 0x2464), DVNM("Labtec Webcam 1200")},
903         {USB_DEVICE(0x093a, 0x2468), DVNM("PAC207")},
904         {USB_DEVICE(0x093a, 0x2470), DVNM("Genius GF112")},
905         {USB_DEVICE(0x093a, 0x2471), DVNM("Genius VideoCam GE111")},
906         {USB_DEVICE(0x093a, 0x2472), DVNM("Genius VideoCam GE110")},
907         {USB_DEVICE(0x2001, 0xf115), DVNM("D-Link DSB-C120")},
908         {}
909 };
910 MODULE_DEVICE_TABLE(usb, device_table);
911
912 /* -- device connect -- */
913 static int sd_probe(struct usb_interface *intf,
914                         const struct usb_device_id *id)
915 {
916         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
917                                 THIS_MODULE);
918 }
919
920 static struct usb_driver sd_driver = {
921         .name = MODULE_NAME,
922         .id_table = device_table,
923         .probe = sd_probe,
924         .disconnect = gspca_disconnect,
925 };
926
927 /* -- module insert / remove -- */
928 static int __init sd_mod_init(void)
929 {
930         init_pixart_decoder();
931         if (usb_register(&sd_driver) < 0)
932                 return -1;
933         PDEBUG(D_PROBE, "v%s registered", version);
934         return 0;
935 }
936 static void __exit sd_mod_exit(void)
937 {
938         usb_deregister(&sd_driver);
939         PDEBUG(D_PROBE, "deregistered");
940 }
941
942 module_init(sd_mod_init);
943 module_exit(sd_mod_exit);