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