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