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