V4L/DVB (8195): gspca: Input buffer overwritten in spca561 + cleanup code.
[linux-2.6.git] / drivers / media / video / gspca / spca561.c
1 /*
2  * Sunplus spca561 subdriver
3  *
4  * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
5  *
6  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #define MODULE_NAME "spca561"
24
25 #include "gspca.h"
26
27 #define DRIVER_VERSION_NUMBER   KERNEL_VERSION(2, 1, 4)
28 static const char version[] = "2.1.4";
29
30 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
31 MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
32 MODULE_LICENSE("GPL");
33
34 /* specific webcam descriptor */
35 struct sd {
36         struct gspca_dev gspca_dev;     /* !! must be the first item */
37
38         unsigned short contrast;
39         __u8 brightness;
40         __u8 autogain;
41
42         __u8 chip_revision;
43         signed char ag_cnt;
44 #define AG_CNT_START 13
45 };
46
47 /* V4L2 controls supported by the driver */
48 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
49 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
50 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
51 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
52 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
53 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
54
55 static struct ctrl sd_ctrls[] = {
56 #define SD_BRIGHTNESS 0
57         {
58          {
59           .id = V4L2_CID_BRIGHTNESS,
60           .type = V4L2_CTRL_TYPE_INTEGER,
61           .name = "Brightness",
62           .minimum = 0,
63           .maximum = 63,
64           .step = 1,
65           .default_value = 32,
66           },
67          .set = sd_setbrightness,
68          .get = sd_getbrightness,
69          },
70 #define SD_CONTRAST 1
71         {
72          {
73           .id = V4L2_CID_CONTRAST,
74           .type = V4L2_CTRL_TYPE_INTEGER,
75           .name = "Contrast",
76           .minimum = 0,
77           .maximum = 0x3fff,
78           .step = 1,
79           .default_value = 0x2000,
80           },
81          .set = sd_setcontrast,
82          .get = sd_getcontrast,
83          },
84 #define SD_AUTOGAIN 2
85         {
86          {
87           .id = V4L2_CID_AUTOGAIN,
88           .type = V4L2_CTRL_TYPE_BOOLEAN,
89           .name = "Auto Gain",
90           .minimum = 0,
91           .maximum = 1,
92           .step = 1,
93           .default_value = 1,
94           },
95          .set = sd_setautogain,
96          .get = sd_getautogain,
97          },
98 };
99
100 static struct cam_mode sif_mode[] = {
101         {V4L2_PIX_FMT_SGBRG8, 160, 120, 3},
102         {V4L2_PIX_FMT_SGBRG8, 176, 144, 2},
103         {V4L2_PIX_FMT_SPCA561, 320, 240, 1},
104         {V4L2_PIX_FMT_SPCA561, 352, 288, 0},
105 };
106
107 /*
108  * Initialization data
109  * I'm not very sure how to split initialization from open data
110  * chunks. For now, we'll consider everything as initialization
111  */
112 /* Frame packet header offsets for the spca561 */
113 #define SPCA561_OFFSET_SNAP 1
114 #define SPCA561_OFFSET_TYPE 2
115 #define SPCA561_OFFSET_COMPRESS 3
116 #define SPCA561_OFFSET_FRAMSEQ   4
117 #define SPCA561_OFFSET_GPIO 5
118 #define SPCA561_OFFSET_USBBUFF 6
119 #define SPCA561_OFFSET_WIN2GRAVE 7
120 #define SPCA561_OFFSET_WIN2RAVE 8
121 #define SPCA561_OFFSET_WIN2BAVE 9
122 #define SPCA561_OFFSET_WIN2GBAVE 10
123 #define SPCA561_OFFSET_WIN1GRAVE 11
124 #define SPCA561_OFFSET_WIN1RAVE 12
125 #define SPCA561_OFFSET_WIN1BAVE 13
126 #define SPCA561_OFFSET_WIN1GBAVE 14
127 #define SPCA561_OFFSET_FREQ 15
128 #define SPCA561_OFFSET_VSYNC 16
129 #define SPCA561_OFFSET_DATA 1
130 #define SPCA561_INDEX_I2C_BASE 0x8800
131 #define SPCA561_SNAPBIT 0x20
132 #define SPCA561_SNAPCTRL 0x40
133 enum {
134         Rev072A = 0,
135         Rev012A,
136 };
137
138 static void reg_w_val(struct usb_device *dev, __u16 index, __u16 value)
139 {
140         int ret;
141
142         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
143                               0,                /* request */
144                               USB_TYPE_VENDOR | USB_RECIP_DEVICE,
145                               value, index, NULL, 0, 500);
146         PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
147         if (ret < 0)
148                 PDEBUG(D_ERR, "reg write: error %d", ret);
149 }
150
151 static void write_vector(struct gspca_dev *gspca_dev,
152                         const __u16 data[][2])
153 {
154         struct usb_device *dev = gspca_dev->dev;
155         int i;
156
157         i = 0;
158         while (data[i][1] != 0) {
159                 reg_w_val(dev, data[i][1], data[i][0]);
160                 i++;
161         }
162 }
163
164 static void reg_r(struct usb_device *dev,
165                   __u16 index, __u8 *buffer, __u16 length)
166 {
167         usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
168                         0,                      /* request */
169                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
170                         0,                      /* value */
171                         index, buffer, length, 500);
172 }
173
174 static void reg_w_buf(struct usb_device *dev,
175                       __u16 index, const __u8 *buffer, __u16 len)
176 {
177         __u8 tmpbuf[8];
178
179         memcpy(tmpbuf, buffer, len);
180         usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
181                         0,                      /* request */
182                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
183                         0,                      /* value */
184                         index, tmpbuf, len, 500);
185 }
186
187 static void i2c_init(struct gspca_dev *gspca_dev, __u8 mode)
188 {
189         reg_w_val(gspca_dev->dev, 0x92, 0x8804);
190         reg_w_val(gspca_dev->dev, mode, 0x8802);
191 }
192
193 static void i2c_write(struct gspca_dev *gspca_dev, __u16 valeur, __u16 reg)
194 {
195         int retry = 60;
196         __u8 DataLow;
197         __u8 DataHight;
198         __u8 Data;
199
200         DataLow = valeur;
201         DataHight = valeur >> 8;
202         reg_w_val(gspca_dev->dev, reg, 0x8801);
203         reg_w_val(gspca_dev->dev, DataLow, 0x8805);
204         reg_w_val(gspca_dev->dev, DataHight, 0x8800);
205         while (retry--) {
206                 reg_r(gspca_dev->dev, 0x8803, &Data, 1);
207                 if (!Data)
208                         break;
209         }
210 }
211
212 static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
213 {
214         int retry = 60;
215         __u8 value;
216         __u8 vallsb;
217         __u8 Data;
218
219         reg_w_val(gspca_dev->dev, 0x92, 0x8804);
220         reg_w_val(gspca_dev->dev, reg, 0x8801);
221         reg_w_val(gspca_dev->dev, (mode | 0x01), 0x8802);
222         while (retry--) {
223                 reg_r(gspca_dev->dev, 0x8803, &Data, 1);
224                 if (!Data)
225                         break;
226         }
227         if (retry == 0)
228                 return -1;
229         reg_r(gspca_dev->dev, 0x8800, &value, 1);
230         reg_r(gspca_dev->dev, 0x8805, &vallsb, 1);
231         return ((int) value << 8) | vallsb;
232 }
233
234 static const __u16 spca561_init_data[][2] = {
235         {0x0000, 0x8114},       /* Software GPIO output data */
236         {0x0001, 0x8114},       /* Software GPIO output data */
237         {0x0000, 0x8112},       /* Some kind of reset */
238         {0x0003, 0x8701},       /* PCLK clock delay adjustment */
239         {0x0001, 0x8703},       /* HSYNC from cmos inverted */
240         {0x0011, 0x8118},       /* Enable and conf sensor */
241         {0x0001, 0x8118},       /* Conf sensor */
242         {0x0092, 0x8804},       /* I know nothing about these */
243         {0x0010, 0x8802},       /* 0x88xx registers, so I won't */
244         /***************/
245         {0x000d, 0x8805},       /* sensor default setting */
246         {0x0001, 0x8801},       /* 1 <- 0x0d */
247         {0x0000, 0x8800},
248         {0x0018, 0x8805},
249         {0x0002, 0x8801},       /* 2 <- 0x18 */
250         {0x0000, 0x8800},
251         {0x0065, 0x8805},
252         {0x0004, 0x8801},       /* 4 <- 0x01 0x65 */
253         {0x0001, 0x8800},
254         {0x0021, 0x8805},
255         {0x0005, 0x8801},       /* 5 <- 0x21 */
256         {0x0000, 0x8800},
257         {0x00aa, 0x8805},
258         {0x0007, 0x8801},       /* 7 <- 0xaa */
259         {0x0000, 0x8800},
260         {0x0004, 0x8805},
261         {0x0020, 0x8801},       /* 0x20 <- 0x15 0x04 */
262         {0x0015, 0x8800},
263         {0x0002, 0x8805},
264         {0x0039, 0x8801},       /* 0x39 <- 0x02 */
265         {0x0000, 0x8800},
266         {0x0010, 0x8805},
267         {0x0035, 0x8801},       /* 0x35 <- 0x10 */
268         {0x0000, 0x8800},
269         {0x0049, 0x8805},
270         {0x0009, 0x8801},       /* 0x09 <- 0x10 0x49 */
271         {0x0010, 0x8800},
272         {0x000b, 0x8805},
273         {0x0028, 0x8801},       /* 0x28 <- 0x0b */
274         {0x0000, 0x8800},
275         {0x000f, 0x8805},
276         {0x003b, 0x8801},       /* 0x3b <- 0x0f */
277         {0x0000, 0x8800},
278         {0x0000, 0x8805},
279         {0x003c, 0x8801},       /* 0x3c <- 0x00 */
280         {0x0000, 0x8800},
281         /***************/
282         {0x0018, 0x8601},       /* Pixel/line selection for color separation */
283         {0x0000, 0x8602},       /* Optical black level for user setting */
284         {0x0060, 0x8604},       /* Optical black horizontal offset */
285         {0x0002, 0x8605},       /* Optical black vertical offset */
286         {0x0000, 0x8603},       /* Non-automatic optical black level */
287         {0x0002, 0x865b},       /* Horizontal offset for valid pixels */
288         {0x0000, 0x865f},       /* Vertical valid pixels window (x2) */
289         {0x00b0, 0x865d},       /* Horizontal valid pixels window (x2) */
290         {0x0090, 0x865e},       /* Vertical valid lines window (x2) */
291         {0x00e0, 0x8406},       /* Memory buffer threshold */
292         {0x0000, 0x8660},       /* Compensation memory stuff */
293         {0x0002, 0x8201},       /* Output address for r/w serial EEPROM */
294         {0x0008, 0x8200},       /* Clear valid bit for serial EEPROM */
295         {0x0001, 0x8200},       /* OprMode to be executed by hardware */
296         {0x0007, 0x8201},       /* Output address for r/w serial EEPROM */
297         {0x0008, 0x8200},       /* Clear valid bit for serial EEPROM */
298         {0x0001, 0x8200},       /* OprMode to be executed by hardware */
299         {0x0010, 0x8660},       /* Compensation memory stuff */
300         {0x0018, 0x8660},       /* Compensation memory stuff */
301
302         {0x0004, 0x8611},       /* R offset for white balance */
303         {0x0004, 0x8612},       /* Gr offset for white balance */
304         {0x0007, 0x8613},       /* B offset for white balance */
305         {0x0000, 0x8614},       /* Gb offset for white balance */
306         {0x008c, 0x8651},       /* R gain for white balance */
307         {0x008c, 0x8652},       /* Gr gain for white balance */
308         {0x00b5, 0x8653},       /* B gain for white balance */
309         {0x008c, 0x8654},       /* Gb gain for white balance */
310         {0x0002, 0x8502},       /* Maximum average bit rate stuff */
311
312         {0x0011, 0x8802},
313         {0x0087, 0x8700},       /* Set master clock (96Mhz????) */
314         {0x0081, 0x8702},       /* Master clock output enable */
315
316         {0x0000, 0x8500},       /* Set image type (352x288 no compression) */
317         /* Originally was 0x0010 (352x288 compression) */
318
319         {0x0002, 0x865b},       /* Horizontal offset for valid pixels */
320         {0x0003, 0x865c},       /* Vertical offset for valid lines */
321         /***************//* sensor active */
322         {0x0003, 0x8801},       /* 0x03 <- 0x01 0x21 //289 */
323         {0x0021, 0x8805},
324         {0x0001, 0x8800},
325         {0x0004, 0x8801},       /* 0x04 <- 0x01 0x65 //357 */
326         {0x0065, 0x8805},
327         {0x0001, 0x8800},
328         {0x0005, 0x8801},       /* 0x05 <- 0x2f */
329         {0x002f, 0x8805},
330         {0x0000, 0x8800},
331         {0x0006, 0x8801},       /* 0x06 <- 0 */
332         {0x0000, 0x8805},
333         {0x0000, 0x8800},
334         {0x000a, 0x8801},       /* 0x0a <- 2 */
335         {0x0002, 0x8805},
336         {0x0000, 0x8800},
337         {0x0009, 0x8801},       /* 0x09 <- 0x1061 */
338         {0x0061, 0x8805},
339         {0x0010, 0x8800},
340         {0x0035, 0x8801},       /* 0x35 <-0x14 */
341         {0x0014, 0x8805},
342         {0x0000, 0x8800},
343         {0x0030, 0x8112},       /* ISO and drop packet enable */
344         {0x0000, 0x8112},       /* Some kind of reset ???? */
345         {0x0009, 0x8118},       /* Enable sensor and set standby */
346         {0x0000, 0x8114},       /* Software GPIO output data */
347         {0x0000, 0x8114},       /* Software GPIO output data */
348         {0x0001, 0x8114},       /* Software GPIO output data */
349         {0x0000, 0x8112},       /* Some kind of reset ??? */
350         {0x0003, 0x8701},
351         {0x0001, 0x8703},
352         {0x0011, 0x8118},
353         {0x0001, 0x8118},
354         /***************/
355         {0x0092, 0x8804},
356         {0x0010, 0x8802},
357         {0x000d, 0x8805},
358         {0x0001, 0x8801},
359         {0x0000, 0x8800},
360         {0x0018, 0x8805},
361         {0x0002, 0x8801},
362         {0x0000, 0x8800},
363         {0x0065, 0x8805},
364         {0x0004, 0x8801},
365         {0x0001, 0x8800},
366         {0x0021, 0x8805},
367         {0x0005, 0x8801},
368         {0x0000, 0x8800},
369         {0x00aa, 0x8805},
370         {0x0007, 0x8801},       /* mode 0xaa */
371         {0x0000, 0x8800},
372         {0x0004, 0x8805},
373         {0x0020, 0x8801},
374         {0x0015, 0x8800},       /* mode 0x0415 */
375         {0x0002, 0x8805},
376         {0x0039, 0x8801},
377         {0x0000, 0x8800},
378         {0x0010, 0x8805},
379         {0x0035, 0x8801},
380         {0x0000, 0x8800},
381         {0x0049, 0x8805},
382         {0x0009, 0x8801},
383         {0x0010, 0x8800},
384         {0x000b, 0x8805},
385         {0x0028, 0x8801},
386         {0x0000, 0x8800},
387         {0x000f, 0x8805},
388         {0x003b, 0x8801},
389         {0x0000, 0x8800},
390         {0x0000, 0x8805},
391         {0x003c, 0x8801},
392         {0x0000, 0x8800},
393         {0x0002, 0x8502},
394         {0x0039, 0x8801},
395         {0x0000, 0x8805},
396         {0x0000, 0x8800},
397
398         {0x0087, 0x8700},       /* overwrite by start */
399         {0x0081, 0x8702},
400         {0x0000, 0x8500},
401 /*      {0x0010, 0x8500},  -- Previous line was this */
402         {0x0002, 0x865b},
403         {0x0003, 0x865c},
404         /***************/
405         {0x0003, 0x8801},       /* 0x121-> 289 */
406         {0x0021, 0x8805},
407         {0x0001, 0x8800},
408         {0x0004, 0x8801},       /* 0x165 -> 357 */
409         {0x0065, 0x8805},
410         {0x0001, 0x8800},
411         {0x0005, 0x8801},       /* 0x2f //blanking control colonne */
412         {0x002f, 0x8805},
413         {0x0000, 0x8800},
414         {0x0006, 0x8801},       /* 0x00 //blanking mode row */
415         {0x0000, 0x8805},
416         {0x0000, 0x8800},
417         {0x000a, 0x8801},       /* 0x01 //0x02 */
418         {0x0001, 0x8805},
419         {0x0000, 0x8800},
420         {0x0009, 0x8801},       /* 0x1061 - setexposure times && pixel clock
421                                  * 0001 0 | 000 0110 0001 */
422         {0x0061, 0x8805},       /* 61 31 */
423         {0x0008, 0x8800},       /* 08 */
424         {0x0035, 0x8801},       /* 0x14 - set gain general */
425         {0x001f, 0x8805},       /* 0x14 */
426         {0x0000, 0x8800},
427         {0x0030, 0x8112},
428         {}
429 };
430
431 static void sensor_reset(struct gspca_dev *gspca_dev)
432 {
433         reg_w_val(gspca_dev->dev, 0x8631, 0xc8);
434         reg_w_val(gspca_dev->dev, 0x8634, 0xc8);
435         reg_w_val(gspca_dev->dev, 0x8112, 0x00);
436         reg_w_val(gspca_dev->dev, 0x8114, 0x00);
437         reg_w_val(gspca_dev->dev, 0x8118, 0x21);
438         i2c_init(gspca_dev, 0x14);
439         i2c_write(gspca_dev, 1, 0x0d);
440         i2c_write(gspca_dev, 0, 0x0d);
441 }
442
443 /******************** QC Express etch2 stuff ********************/
444 static const __u16 Pb100_1map8300[][2] = {
445         /* reg, value */
446         {0x8320, 0x3304},
447
448         {0x8303, 0x0125},       /* image area */
449         {0x8304, 0x0169},
450         {0x8328, 0x000b},
451         {0x833c, 0x0001},
452
453         {0x832f, 0x0419},
454         {0x8307, 0x00aa},
455         {0x8301, 0x0003},
456         {0x8302, 0x000e},
457         {}
458 };
459 static const __u16 Pb100_2map8300[][2] = {
460         /* reg, value */
461         {0x8339, 0x0000},
462         {0x8307, 0x00aa},
463         {}
464 };
465
466 static const __u16 spca561_161rev12A_data1[][2] = {
467         {0x21, 0x8118},
468         {0x01, 0x8114},
469         {0x00, 0x8112},
470         {0x92, 0x8804},
471         {0x04, 0x8802},         /* windows uses 08 */
472         {}
473 };
474 static const __u16 spca561_161rev12A_data2[][2] = {
475         {0x21, 0x8118},
476         {0x10, 0x8500},
477         {0x07, 0x8601},
478         {0x07, 0x8602},
479         {0x04, 0x8501},
480         {0x21, 0x8118},
481
482         {0x07, 0x8201},         /* windows uses 02 */
483         {0x08, 0x8200},
484         {0x01, 0x8200},
485
486         {0x00, 0x8114},
487         {0x01, 0x8114},         /* windows uses 00 */
488
489         {0x90, 0x8604},
490         {0x00, 0x8605},
491         {0xb0, 0x8603},
492
493         /* sensor gains */
494         {0x00, 0x8610},         /* *red */
495         {0x00, 0x8611},         /* 3f   *green */
496         {0x00, 0x8612},         /* green *blue */
497         {0x00, 0x8613},         /* blue *green */
498         {0x35, 0x8614},         /* green *red */
499         {0x35, 0x8615},         /* 40   *green */
500         {0x35, 0x8616},         /* 7a   *blue */
501         {0x35, 0x8617},         /* 40   *green */
502
503         {0x0c, 0x8620},         /* 0c */
504         {0xc8, 0x8631},         /* c8 */
505         {0xc8, 0x8634},         /* c8 */
506         {0x23, 0x8635},         /* 23 */
507         {0x1f, 0x8636},         /* 1f */
508         {0xdd, 0x8637},         /* dd */
509         {0xe1, 0x8638},         /* e1 */
510         {0x1d, 0x8639},         /* 1d */
511         {0x21, 0x863a},         /* 21 */
512         {0xe3, 0x863b},         /* e3 */
513         {0xdf, 0x863c},         /* df */
514         {0xf0, 0x8505},
515         {0x32, 0x850a},
516         {}
517 };
518
519 static void sensor_mapwrite(struct gspca_dev *gspca_dev,
520                             const __u16 sensormap[][2])
521 {
522         int i = 0;
523         __u8 usbval[2];
524
525         while (sensormap[i][0]) {
526                 usbval[0] = sensormap[i][1];
527                 usbval[1] = sensormap[i][1] >> 8;
528                 reg_w_buf(gspca_dev->dev, sensormap[i][0], usbval, 2);
529                 i++;
530         }
531 }
532 static void init_161rev12A(struct gspca_dev *gspca_dev)
533 {
534         sensor_reset(gspca_dev);
535         write_vector(gspca_dev, spca561_161rev12A_data1);
536         sensor_mapwrite(gspca_dev, Pb100_1map8300);
537         write_vector(gspca_dev, spca561_161rev12A_data2);
538         sensor_mapwrite(gspca_dev, Pb100_2map8300);
539 }
540
541 /* this function is called at probe time */
542 static int sd_config(struct gspca_dev *gspca_dev,
543                      const struct usb_device_id *id)
544 {
545         struct sd *sd = (struct sd *) gspca_dev;
546         struct usb_device *dev = gspca_dev->dev;
547         struct cam *cam;
548         __u16 vendor, product;
549         __u8 data1, data2;
550
551         /* Read frm global register the USB product and vendor IDs, just to
552          * prove that we can communicate with the device.  This works, which
553          * confirms at we are communicating properly and that the device
554          * is a 561. */
555         reg_r(dev, 0x8104, &data1, 1);
556         reg_r(dev, 0x8105, &data2, 1);
557         vendor = (data2 << 8) | data1;
558         reg_r(dev, 0x8106, &data1, 1);
559         reg_r(dev, 0x8107, &data2, 1);
560         product = (data2 << 8) | data1;
561         if (vendor != id->idVendor || product != id->idProduct) {
562                 PDEBUG(D_PROBE, "Bad vendor / product from device");
563                 return -EINVAL;
564         }
565         switch (product) {
566         case 0x0928:
567         case 0x0929:
568         case 0x092a:
569         case 0x092b:
570         case 0x092c:
571         case 0x092d:
572         case 0x092e:
573         case 0x092f:
574         case 0x403b:
575                 sd->chip_revision = Rev012A;
576                 break;
577         default:
578 /*      case 0x0561:
579         case 0x0815:                    * ?? in spca508.c
580         case 0x401a:
581         case 0x7004:
582         case 0x7e50:
583         case 0xa001:
584         case 0xcdee: */
585                 sd->chip_revision = Rev072A;
586                 break;
587         }
588         cam = &gspca_dev->cam;
589         cam->dev_name = (char *) id->driver_info;
590         cam->epaddr = 0x01;
591         gspca_dev->nbalt = 7 + 1;       /* choose alternate 7 first */
592         cam->cam_mode = sif_mode;
593         cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
594         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
595         sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
596         sd->autogain = sd_ctrls[SD_AUTOGAIN].qctrl.default_value;
597         return 0;
598 }
599
600 /* this function is called at open time */
601 static int sd_open(struct gspca_dev *gspca_dev)
602 {
603         struct sd *sd = (struct sd *) gspca_dev;
604
605         switch (sd->chip_revision) {
606         case Rev072A:
607                 PDEBUG(D_STREAM, "Chip revision id: 072a");
608                 write_vector(gspca_dev, spca561_init_data);
609                 break;
610         default:
611 /*      case Rev012A: */
612                 PDEBUG(D_STREAM, "Chip revision id: 012a");
613                 init_161rev12A(gspca_dev);
614                 break;
615         }
616         return 0;
617 }
618
619 static void setcontrast(struct gspca_dev *gspca_dev)
620 {
621         struct sd *sd = (struct sd *) gspca_dev;
622         struct usb_device *dev = gspca_dev->dev;
623         __u8 lowb;
624         int expotimes;
625
626         switch (sd->chip_revision) {
627         case Rev072A:
628                 lowb = sd->contrast >> 8;
629                 reg_w_val(dev, lowb, 0x8651);
630                 reg_w_val(dev, lowb, 0x8652);
631                 reg_w_val(dev, lowb, 0x8653);
632                 reg_w_val(dev, lowb, 0x8654);
633                 break;
634         case Rev012A: {
635                 __u8 Reg8391[] =
636                         { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00 };
637
638                 /* Write camera sensor settings */
639                 expotimes = (sd->contrast >> 5) & 0x07ff;
640                 Reg8391[0] = expotimes & 0xff;  /* exposure */
641                 Reg8391[1] = 0x18 | (expotimes >> 8);
642                 Reg8391[2] = sd->brightness;    /* gain */
643                 reg_w_buf(dev, 0x8391, Reg8391, 8);
644                 reg_w_buf(dev, 0x8390, Reg8391, 8);
645                 break;
646             }
647         }
648 }
649
650 static void sd_start(struct gspca_dev *gspca_dev)
651 {
652         struct sd *sd = (struct sd *) gspca_dev;
653         struct usb_device *dev = gspca_dev->dev;
654         int Clck;
655         __u8 Reg8307[] = { 0xaa, 0x00 };
656         int mode;
657
658         mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].mode;
659         switch (sd->chip_revision) {
660         case Rev072A:
661                 switch (mode) {
662                 default:
663 /*              case 0:
664                 case 1: */
665                         Clck = 0x25;
666                         break;
667                 case 2:
668                         Clck = 0x22;
669                         break;
670                 case 3:
671                         Clck = 0x21;
672                         break;
673                 }
674                 reg_w_val(dev, 0x8500, mode);   /* mode */
675                 reg_w_val(dev, 0x8700, Clck);   /* 0x27 clock */
676                 reg_w_val(dev, 0x8112, 0x10 | 0x20);
677                 break;
678         default:
679 /*      case Rev012A: */
680                 switch (mode) {
681                 case 0:
682                 case 1:
683                         Clck = 0x8a;
684                         break;
685                 case 2:
686                         Clck = 0x85;
687                         break;
688                 default:
689                         Clck = 0x83;
690                         break;
691                 }
692                 if (mode <= 1) {
693                         /* Use compression on 320x240 and above */
694                         reg_w_val(dev, 0x8500, 0x10 | mode);
695                 } else {
696                         /* I couldn't get the compression to work below 320x240
697                          * Fortunately at these resolutions the bandwidth
698                          * is sufficient to push raw frames at ~20fps */
699                         reg_w_val(dev, 0x8500, mode);
700                 }               /* -- qq@kuku.eu.org */
701                 reg_w_buf(dev, 0x8307, Reg8307, 2);
702                 reg_w_val(dev, 0x8700, Clck);   /* 0x8f 0x85 0x27 clock */
703                 reg_w_val(dev, 0x8112, 0x1e | 0x20);
704                 reg_w_val(dev, 0x850b, 0x03);
705                 setcontrast(gspca_dev);
706                 break;
707         }
708 }
709
710 static void sd_stopN(struct gspca_dev *gspca_dev)
711 {
712         reg_w_val(gspca_dev->dev, 0x8112, 0x20);
713 }
714
715 static void sd_stop0(struct gspca_dev *gspca_dev)
716 {
717 }
718
719 /* this function is called at close time */
720 static void sd_close(struct gspca_dev *gspca_dev)
721 {
722         reg_w_val(gspca_dev->dev, 0x8114, 0);
723 }
724
725 static void setautogain(struct gspca_dev *gspca_dev)
726 {
727         struct sd *sd = (struct sd *) gspca_dev;
728         int expotimes = 0;
729         int pixelclk = 0;
730         int gainG = 0;
731         __u8 R, Gr, Gb, B;
732         int y;
733         __u8 luma_mean = 110;
734         __u8 luma_delta = 20;
735         __u8 spring = 4;
736
737         switch (sd->chip_revision) {
738         case Rev072A:
739                 reg_r(gspca_dev->dev, 0x8621, &Gr, 1);
740                 reg_r(gspca_dev->dev, 0x8622, &R, 1);
741                 reg_r(gspca_dev->dev, 0x8623, &B, 1);
742                 reg_r(gspca_dev->dev, 0x8624, &Gb, 1);
743                 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
744                 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
745                 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
746                 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
747
748                 if (y < luma_mean - luma_delta ||
749                     y > luma_mean + luma_delta) {
750                         expotimes = i2c_read(gspca_dev, 0x09, 0x10);
751                         pixelclk = 0x0800;
752                         expotimes = expotimes & 0x07ff;
753                         /* PDEBUG(D_PACK,
754                                 "Exposition Times 0x%03X Clock 0x%04X ",
755                                 expotimes,pixelclk); */
756                         gainG = i2c_read(gspca_dev, 0x35, 0x10);
757                         /* PDEBUG(D_PACK,
758                                 "reading Gain register %d", gainG); */
759
760                         expotimes += (luma_mean - y) >> spring;
761                         gainG += (luma_mean - y) / 50;
762                         /* PDEBUG(D_PACK,
763                                 "compute expotimes %d gain %d",
764                                 expotimes,gainG); */
765
766                         if (gainG > 0x3f)
767                                 gainG = 0x3f;
768                         else if (gainG < 4)
769                                 gainG = 3;
770                         i2c_write(gspca_dev, gainG, 0x35);
771
772                         if (expotimes >= 0x0256)
773                                 expotimes = 0x0256;
774                         else if (expotimes < 4)
775                                 expotimes = 3;
776                         i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
777                 }
778                 break;
779         case Rev012A:
780                 /* sensor registers is access and memory mapped to 0x8300 */
781                 /* readind all 0x83xx block the sensor */
782                 /*
783                  * The data from the header seem wrong where is the luma
784                  * and chroma mean value
785                  * at the moment set exposure in contrast set
786                  */
787                 break;
788         }
789 }
790
791 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
792                         struct gspca_frame *frame, /* target */
793                         __u8 *data,             /* isoc packet */
794                         int len)                /* iso packet length */
795 {
796         struct sd *sd = (struct sd *) gspca_dev;
797
798         switch (data[0]) {
799         case 0:         /* start of frame */
800                 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
801                                         data, 0);
802                 if (sd->ag_cnt >= 0) {
803                         if (--sd->ag_cnt < 0) {
804                                 sd->ag_cnt = AG_CNT_START;
805                                 setautogain(gspca_dev);
806                         }
807                 }
808                 data += SPCA561_OFFSET_DATA;
809                 len -= SPCA561_OFFSET_DATA;
810                 if (data[1] & 0x10) {
811                         /* compressed bayer */
812                         gspca_frame_add(gspca_dev, FIRST_PACKET,
813                                         frame, data, len);
814                 } else {
815                         /* raw bayer (with a header, which we skip) */
816                         data += 20;
817                         len -= 20;
818                         gspca_frame_add(gspca_dev, FIRST_PACKET,
819                                                 frame, data, len);
820                 }
821                 return;
822         case 0xff:              /* drop */
823 /*              gspca_dev->last_packet_type = DISCARD_PACKET; */
824                 return;
825         }
826         data++;
827         len--;
828         gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
829 }
830
831 static void setbrightness(struct gspca_dev *gspca_dev)
832 {
833         struct sd *sd = (struct sd *) gspca_dev;
834         __u8 value;
835
836         switch (sd->chip_revision) {
837         case Rev072A:
838                 value = sd->brightness;
839                 reg_w_val(gspca_dev->dev, value, 0x8611);
840                 reg_w_val(gspca_dev->dev, value, 0x8612);
841                 reg_w_val(gspca_dev->dev, value, 0x8613);
842                 reg_w_val(gspca_dev->dev, value, 0x8614);
843                 break;
844         default:
845 /*      case Rev012A: */
846                 setcontrast(gspca_dev);
847                 break;
848         }
849 }
850
851 static void getbrightness(struct gspca_dev *gspca_dev)
852 {
853         struct sd *sd = (struct sd *) gspca_dev;
854         __u8 value;
855         __u16 tot;
856
857         switch (sd->chip_revision) {
858         case Rev072A:
859                 tot = 0;
860                 reg_r(gspca_dev->dev, 0x8611, &value, 1);
861                 tot += value;
862                 reg_r(gspca_dev->dev, 0x8612, &value, 1);
863                 tot += value;
864                 reg_r(gspca_dev->dev, 0x8613, &value, 1);
865                 tot += value;
866                 reg_r(gspca_dev->dev, 0x8614, &value, 1);
867                 tot += value;
868                 sd->brightness = tot >> 2;
869                 break;
870         default:
871 /*      case Rev012A: */
872                 /* no way to read sensor settings */
873                 break;
874         }
875 }
876
877 static void getcontrast(struct gspca_dev *gspca_dev)
878 {
879         struct sd *sd = (struct sd *) gspca_dev;
880         __u8 value;
881         __u16 tot;
882
883         switch (sd->chip_revision) {
884         case Rev072A:
885                 tot = 0;
886                 reg_r(gspca_dev->dev, 0x8651, &value, 1);
887                 tot += value;
888                 reg_r(gspca_dev->dev, 0x8652, &value, 1);
889                 tot += value;
890                 reg_r(gspca_dev->dev, 0x8653, &value, 1);
891                 tot += value;
892                 reg_r(gspca_dev->dev, 0x8654, &value, 1);
893                 tot += value;
894                 sd->contrast = tot << 6;
895                 break;
896         default:
897 /*      case Rev012A: */
898                 /* no way to read sensor settings */
899                 break;
900         }
901         PDEBUG(D_CONF, "get contrast %d", sd->contrast);
902 }
903
904 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
905 {
906         struct sd *sd = (struct sd *) gspca_dev;
907
908         sd->brightness = val;
909         if (gspca_dev->streaming)
910                 setbrightness(gspca_dev);
911         return 0;
912 }
913
914 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
915 {
916         struct sd *sd = (struct sd *) gspca_dev;
917
918         getbrightness(gspca_dev);
919         *val = sd->brightness;
920         return 0;
921 }
922
923 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
924 {
925         struct sd *sd = (struct sd *) gspca_dev;
926
927         sd->contrast = val;
928         if (gspca_dev->streaming)
929                 setcontrast(gspca_dev);
930         return 0;
931 }
932
933 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
934 {
935         struct sd *sd = (struct sd *) gspca_dev;
936
937         getcontrast(gspca_dev);
938         *val = sd->contrast;
939         return 0;
940 }
941
942 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
943 {
944         struct sd *sd = (struct sd *) gspca_dev;
945
946         sd->autogain = val;
947         if (val)
948                 sd->ag_cnt = AG_CNT_START;
949         else
950                 sd->ag_cnt = -1;
951         return 0;
952 }
953
954 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
955 {
956         struct sd *sd = (struct sd *) gspca_dev;
957
958         *val = sd->autogain;
959         return 0;
960 }
961
962 /* sub-driver description */
963 static const struct sd_desc sd_desc = {
964         .name = MODULE_NAME,
965         .ctrls = sd_ctrls,
966         .nctrls = ARRAY_SIZE(sd_ctrls),
967         .config = sd_config,
968         .open = sd_open,
969         .start = sd_start,
970         .stopN = sd_stopN,
971         .stop0 = sd_stop0,
972         .close = sd_close,
973         .pkt_scan = sd_pkt_scan,
974 };
975
976 /* -- module initialisation -- */
977 #define DVNM(name) .driver_info = (kernel_ulong_t) name
978 static const __devinitdata struct usb_device_id device_table[] = {
979         {USB_DEVICE(0x041e, 0x401a), DVNM("Creative Webcam Vista (PD1100)")},
980         {USB_DEVICE(0x041e, 0x403b),  DVNM("Creative Webcam Vista (VF0010)")},
981         {USB_DEVICE(0x0458, 0x7004), DVNM("Genius VideoCAM Express V2")},
982         {USB_DEVICE(0x046d, 0x0928), DVNM("Logitech QC Express Etch2")},
983         {USB_DEVICE(0x046d, 0x0929), DVNM("Labtec Webcam Elch2")},
984         {USB_DEVICE(0x046d, 0x092a), DVNM("Logitech QC for Notebook")},
985         {USB_DEVICE(0x046d, 0x092b), DVNM("Labtec Webcam Plus")},
986         {USB_DEVICE(0x046d, 0x092c), DVNM("Logitech QC chat Elch2")},
987         {USB_DEVICE(0x046d, 0x092d), DVNM("Logitech QC Elch2")},
988         {USB_DEVICE(0x046d, 0x092e), DVNM("Logitech QC Elch2")},
989         {USB_DEVICE(0x046d, 0x092f), DVNM("Logitech QC Elch2")},
990         {USB_DEVICE(0x04fc, 0x0561), DVNM("Flexcam 100")},
991         {USB_DEVICE(0x060b, 0xa001), DVNM("Maxell Compact Pc PM3")},
992         {USB_DEVICE(0x10fd, 0x7e50), DVNM("FlyCam Usb 100")},
993         {USB_DEVICE(0xabcd, 0xcdee), DVNM("Petcam")},
994         {}
995 };
996
997 MODULE_DEVICE_TABLE(usb, device_table);
998
999 /* -- device connect -- */
1000 static int sd_probe(struct usb_interface *intf,
1001                     const struct usb_device_id *id)
1002 {
1003         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1004                                THIS_MODULE);
1005 }
1006
1007 static struct usb_driver sd_driver = {
1008         .name = MODULE_NAME,
1009         .id_table = device_table,
1010         .probe = sd_probe,
1011         .disconnect = gspca_disconnect,
1012 };
1013
1014 /* -- module insert / remove -- */
1015 static int __init sd_mod_init(void)
1016 {
1017         if (usb_register(&sd_driver) < 0)
1018                 return -1;
1019         PDEBUG(D_PROBE, "v%s registered", version);
1020         return 0;
1021 }
1022 static void __exit sd_mod_exit(void)
1023 {
1024         usb_deregister(&sd_driver);
1025         PDEBUG(D_PROBE, "deregistered");
1026 }
1027
1028 module_init(sd_mod_init);
1029 module_exit(sd_mod_exit);