V4L/DVB (8442): gspca: Remove the version from the subdrivers.
[linux-2.6] / drivers / media / video / gspca / tv8532.c
1 /*
2  * Quickcam cameras initialization data
3  *
4  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #define MODULE_NAME "tv8532"
22
23 #include "gspca.h"
24
25 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
26 MODULE_DESCRIPTION("TV8532 USB Camera Driver");
27 MODULE_LICENSE("GPL");
28
29 /* specific webcam descriptor */
30 struct sd {
31         struct gspca_dev gspca_dev;     /* !! must be the first item */
32
33         int buflen;                     /* current length of tmpbuf */
34         __u8 tmpbuf[352 * 288 + 10 * 288];      /* no protection... */
35         __u8 tmpbuf2[352 * 288];                /* no protection... */
36
37         unsigned short brightness;
38         unsigned short contrast;
39
40         char packet;
41         char synchro;
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
50 static struct ctrl sd_ctrls[] = {
51 #define SD_BRIGHTNESS 0
52         {
53          {
54           .id = V4L2_CID_BRIGHTNESS,
55           .type = V4L2_CTRL_TYPE_INTEGER,
56           .name = "Brightness",
57           .minimum = 1,
58           .maximum = 0x2ff,
59           .step = 1,
60           .default_value = 0x18f,
61           },
62          .set = sd_setbrightness,
63          .get = sd_getbrightness,
64          },
65 #define SD_CONTRAST 1
66         {
67          {
68           .id = V4L2_CID_CONTRAST,
69           .type = V4L2_CTRL_TYPE_INTEGER,
70           .name = "Contrast",
71           .minimum = 0,
72           .maximum = 0xffff,
73           .step = 1,
74           .default_value = 0x7fff,
75           },
76          .set = sd_setcontrast,
77          .get = sd_getcontrast,
78          },
79 };
80
81 static struct v4l2_pix_format sif_mode[] = {
82         {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
83                 .bytesperline = 176,
84                 .sizeimage = 176 * 144,
85                 .colorspace = V4L2_COLORSPACE_SRGB,
86                 .priv = 1},
87         {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
88                 .bytesperline = 352,
89                 .sizeimage = 352 * 288,
90                 .colorspace = V4L2_COLORSPACE_SRGB,
91                 .priv = 0},
92 };
93
94 /*
95  * Initialization data: this is the first set-up data written to the
96  * device (before the open data).
97  */
98 #define TESTCLK 0x10            /* reg 0x2c -> 0x12 //10 */
99 #define TESTCOMP 0x90           /* reg 0x28 -> 0x80 */
100 #define TESTLINE 0x81           /* reg 0x29 -> 0x81 */
101 #define QCIFLINE 0x41           /* reg 0x29 -> 0x81 */
102 #define TESTPTL 0x14            /* reg 0x2D -> 0x14 */
103 #define TESTPTH 0x01            /* reg 0x2E -> 0x01 */
104 #define TESTPTBL 0x12           /* reg 0x2F -> 0x0a */
105 #define TESTPTBH 0x01           /* reg 0x30 -> 0x01 */
106 #define ADWIDTHL 0xe8           /* reg 0x0c -> 0xe8 */
107 #define ADWIDTHH 0x03           /* reg 0x0d -> 0x03 */
108 #define ADHEIGHL 0x90           /* reg 0x0e -> 0x91 //93 */
109 #define ADHEIGHH 0x01           /* reg 0x0f -> 0x01 */
110 #define EXPOL 0x8f              /* reg 0x1c -> 0x8f */
111 #define EXPOH 0x01              /* reg 0x1d -> 0x01 */
112 #define ADCBEGINL 0x44          /* reg 0x10 -> 0x46 //47 */
113 #define ADCBEGINH 0x00          /* reg 0x11 -> 0x00 */
114 #define ADRBEGINL 0x0a          /* reg 0x14 -> 0x0b //0x0c */
115 #define ADRBEGINH 0x00          /* reg 0x15 -> 0x00 */
116 #define TV8532_CMD_UPDATE 0x84
117
118 #define TV8532_EEprom_Add 0x03
119 #define TV8532_EEprom_DataL 0x04
120 #define TV8532_EEprom_DataM 0x05
121 #define TV8532_EEprom_DataH 0x06
122 #define TV8532_EEprom_TableLength 0x07
123 #define TV8532_EEprom_Write 0x08
124 #define TV8532_PART_CTRL 0x00
125 #define TV8532_CTRL 0x01
126 #define TV8532_CMD_EEprom_Open 0x30
127 #define TV8532_CMD_EEprom_Close 0x29
128 #define TV8532_UDP_UPDATE 0x31
129 #define TV8532_GPIO 0x39
130 #define TV8532_GPIO_OE 0x3B
131 #define TV8532_REQ_RegWrite 0x02
132 #define TV8532_REQ_RegRead 0x03
133
134 #define TV8532_ADWIDTH_L 0x0C
135 #define TV8532_ADWIDTH_H 0x0D
136 #define TV8532_ADHEIGHT_L 0x0E
137 #define TV8532_ADHEIGHT_H 0x0F
138 #define TV8532_EXPOSURE 0x1C
139 #define TV8532_QUANT_COMP 0x28
140 #define TV8532_MODE_PACKET 0x29
141 #define TV8532_SETCLK 0x2C
142 #define TV8532_POINT_L 0x2D
143 #define TV8532_POINT_H 0x2E
144 #define TV8532_POINTB_L 0x2F
145 #define TV8532_POINTB_H 0x30
146 #define TV8532_BUDGET_L 0x2A
147 #define TV8532_BUDGET_H 0x2B
148 #define TV8532_VID_L 0x34
149 #define TV8532_VID_H 0x35
150 #define TV8532_PID_L 0x36
151 #define TV8532_PID_H 0x37
152 #define TV8532_DeviceID 0x83
153 #define TV8532_AD_SLOPE 0x91
154 #define TV8532_AD_BITCTRL 0x94
155 #define TV8532_AD_COLBEGIN_L 0x10
156 #define TV8532_AD_COLBEGIN_H 0x11
157 #define TV8532_AD_ROWBEGIN_L 0x14
158 #define TV8532_AD_ROWBEGIN_H 0x15
159
160 static const __u32 tv_8532_eeprom_data[] = {
161 /*      add             dataL      dataM        dataH */
162         0x00010001, 0x01018011, 0x02050014, 0x0305001c,
163         0x040d001e, 0x0505001f, 0x06050519, 0x0705011b,
164         0x0805091e, 0x090d892e, 0x0a05892f, 0x0b050dd9,
165         0x0c0509f1, 0
166 };
167
168 static int reg_r(struct gspca_dev *gspca_dev,
169                  __u16 index)
170 {
171         usb_control_msg(gspca_dev->dev,
172                         usb_rcvctrlpipe(gspca_dev->dev, 0),
173                         TV8532_REQ_RegRead,
174                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
175                         0,      /* value */
176                         index, gspca_dev->usb_buf, 1,
177                         500);
178         return gspca_dev->usb_buf[0];
179 }
180
181 /* write 1 byte */
182 static void reg_w_1(struct gspca_dev *gspca_dev,
183                   __u16 index, __u8 value)
184 {
185         gspca_dev->usb_buf[0] = value;
186         usb_control_msg(gspca_dev->dev,
187                         usb_sndctrlpipe(gspca_dev->dev, 0),
188                         TV8532_REQ_RegWrite,
189                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
190                         0,      /* value */
191                         index, gspca_dev->usb_buf, 1, 500);
192 }
193
194 /* write 2 bytes */
195 static void reg_w_2(struct gspca_dev *gspca_dev,
196                   __u16 index, __u8 val1, __u8 val2)
197 {
198         gspca_dev->usb_buf[0] = val1;
199         gspca_dev->usb_buf[1] = val2;
200         usb_control_msg(gspca_dev->dev,
201                         usb_sndctrlpipe(gspca_dev->dev, 0),
202                         TV8532_REQ_RegWrite,
203                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
204                         0,      /* value */
205                         index, gspca_dev->usb_buf, 2, 500);
206 }
207
208 static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
209 {
210         int i = 0;
211         __u8 reg, data0, data1, data2;
212
213         reg_w_1(gspca_dev, TV8532_GPIO, 0xb0);
214         reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Open);
215 /*      msleep(1); */
216         while (tv_8532_eeprom_data[i]) {
217                 reg = (tv_8532_eeprom_data[i] & 0xff000000) >> 24;
218                 reg_w_1(gspca_dev, TV8532_EEprom_Add, reg);
219                 /* msleep(1); */
220                 data0 = (tv_8532_eeprom_data[i] & 0x000000ff);
221                 reg_w_1(gspca_dev, TV8532_EEprom_DataL, data0);
222                 /* msleep(1); */
223                 data1 = (tv_8532_eeprom_data[i] & 0x0000ff00) >> 8;
224                 reg_w_1(gspca_dev, TV8532_EEprom_DataM, data1);
225                 /* msleep(1); */
226                 data2 = (tv_8532_eeprom_data[i] & 0x00ff0000) >> 16;
227                 reg_w_1(gspca_dev, TV8532_EEprom_DataH, data2);
228                 /* msleep(1); */
229                 reg_w_1(gspca_dev, TV8532_EEprom_Write, 0);
230                 /* msleep(10); */
231                 i++;
232         }
233         reg_w_1(gspca_dev, TV8532_EEprom_TableLength, i);
234 /*      msleep(1); */
235         reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Close);
236         msleep(10);
237 }
238
239 /* this function is called at probe time */
240 static int sd_config(struct gspca_dev *gspca_dev,
241                      const struct usb_device_id *id)
242 {
243         struct sd *sd = (struct sd *) gspca_dev;
244         struct cam *cam;
245
246         tv_8532WriteEEprom(gspca_dev);
247
248         cam = &gspca_dev->cam;
249         cam->dev_name = (char *) id->driver_info;
250         cam->epaddr = 1;
251         cam->cam_mode = sif_mode;
252         cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
253
254         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
255         sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
256         return 0;
257 }
258
259 static void tv_8532ReadRegisters(struct gspca_dev *gspca_dev)
260 {
261         __u8 data;
262
263         data = reg_r(gspca_dev, 0x0001);
264         PDEBUG(D_USBI, "register 0x01-> %x", data);
265         data = reg_r(gspca_dev, 0x0002);
266         PDEBUG(D_USBI, "register 0x02-> %x", data);
267         reg_r(gspca_dev, TV8532_ADWIDTH_L);
268         reg_r(gspca_dev, TV8532_ADWIDTH_H);
269         reg_r(gspca_dev, TV8532_QUANT_COMP);
270         reg_r(gspca_dev, TV8532_MODE_PACKET);
271         reg_r(gspca_dev, TV8532_SETCLK);
272         reg_r(gspca_dev, TV8532_POINT_L);
273         reg_r(gspca_dev, TV8532_POINT_H);
274         reg_r(gspca_dev, TV8532_POINTB_L);
275         reg_r(gspca_dev, TV8532_POINTB_H);
276         reg_r(gspca_dev, TV8532_BUDGET_L);
277         reg_r(gspca_dev, TV8532_BUDGET_H);
278         reg_r(gspca_dev, TV8532_VID_L);
279         reg_r(gspca_dev, TV8532_VID_H);
280         reg_r(gspca_dev, TV8532_PID_L);
281         reg_r(gspca_dev, TV8532_PID_H);
282         reg_r(gspca_dev, TV8532_DeviceID);
283         reg_r(gspca_dev, TV8532_AD_COLBEGIN_L);
284         reg_r(gspca_dev, TV8532_AD_COLBEGIN_H);
285         reg_r(gspca_dev, TV8532_AD_ROWBEGIN_L);
286         reg_r(gspca_dev, TV8532_AD_ROWBEGIN_H);
287 }
288
289 static void tv_8532_setReg(struct gspca_dev *gspca_dev)
290 {
291         reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
292                         ADCBEGINL);                     /* 0x10 */
293         reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
294                         ADCBEGINH);                     /* also digital gain */
295         reg_w_1(gspca_dev, TV8532_PART_CTRL,
296                         TV8532_CMD_UPDATE);             /* 0x00<-0x84 */
297
298         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0a);
299         /******************************************************/
300         reg_w_1(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL); /* 0e */
301         reg_w_1(gspca_dev, TV8532_ADHEIGHT_H, ADHEIGHH); /* 0f */
302         reg_w_2(gspca_dev, TV8532_EXPOSURE,
303                         EXPOL, EXPOH);                  /* 350d 0x014c; 1c */
304         reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
305                         ADCBEGINL);                     /* 0x10 */
306         reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
307                         ADCBEGINH);                     /* also digital gain */
308         reg_w_1(gspca_dev, TV8532_AD_ROWBEGIN_L,
309                         ADRBEGINL);                     /* 0x14 */
310
311         reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00);      /* 0x91 */
312         reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x02);    /* 0x94 */
313
314         reg_w_1(gspca_dev, TV8532_CTRL,
315                         TV8532_CMD_EEprom_Close);       /* 0x01 */
316
317         reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00);      /* 0x91 */
318         reg_w_1(gspca_dev, TV8532_PART_CTRL,
319                         TV8532_CMD_UPDATE);             /* 0x00<-0x84 */
320 }
321
322 static void tv_8532_PollReg(struct gspca_dev *gspca_dev)
323 {
324         int i;
325
326         /* strange polling from tgc */
327         for (i = 0; i < 10; i++) {
328                 reg_w_1(gspca_dev, TV8532_SETCLK,
329                         TESTCLK);               /* 0x48; //0x08; 0x2c */
330                 reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
331                 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01);    /* 0x31 */
332         }
333 }
334
335 /* this function is called at open time */
336 static int sd_open(struct gspca_dev *gspca_dev)
337 {
338         reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
339         reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
340         tv_8532ReadRegisters(gspca_dev);
341         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
342         reg_w_2(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL,
343                                 ADHEIGHH);      /* 401d 0x0169; 0e */
344         reg_w_2(gspca_dev, TV8532_EXPOSURE, EXPOL,
345                                 EXPOH);         /* 350d 0x014c; 1c */
346         reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
347         reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
348
349         /*******************************************************************/
350         reg_w_1(gspca_dev, TV8532_QUANT_COMP,
351                         TESTCOMP);      /* 0x72 compressed mode 0x28 */
352         reg_w_1(gspca_dev, TV8532_MODE_PACKET,
353                         TESTLINE);      /* 0x84; // CIF | 4 packet 0x29 */
354
355         /************************************************/
356         reg_w_1(gspca_dev, TV8532_SETCLK,
357                         TESTCLK);               /* 0x48; //0x08; 0x2c */
358         reg_w_1(gspca_dev, TV8532_POINT_L,
359                         TESTPTL);               /* 0x38; 0x2d */
360         reg_w_1(gspca_dev, TV8532_POINT_H,
361                         TESTPTH);               /* 0x04; 0x2e */
362         reg_w_1(gspca_dev, TV8532_POINTB_L,
363                         TESTPTBL);              /* 0x04; 0x2f */
364         reg_w_1(gspca_dev, TV8532_POINTB_H,
365                         TESTPTBH);              /* 0x04; 0x30 */
366         reg_w_1(gspca_dev, TV8532_PART_CTRL,
367                         TV8532_CMD_UPDATE);     /* 0x00<-0x84 */
368         /*************************************************/
369         reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01);    /* 0x31 */
370         msleep(200);
371         reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00);    /* 0x31 */
372         /*************************************************/
373         tv_8532_setReg(gspca_dev);
374         /*************************************************/
375         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
376         /*************************************************/
377         tv_8532_setReg(gspca_dev);
378         /*************************************************/
379         tv_8532_PollReg(gspca_dev);
380         return 0;
381 }
382
383 static void setbrightness(struct gspca_dev *gspca_dev)
384 {
385         struct sd *sd = (struct sd *) gspca_dev;
386         int brightness = sd->brightness;
387
388         reg_w_2(gspca_dev, TV8532_EXPOSURE,
389                 brightness >> 8, brightness);           /* 1c */
390         reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
391 }
392
393 /* -- start the camera -- */
394 static void sd_start(struct gspca_dev *gspca_dev)
395 {
396         reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
397         reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
398         tv_8532ReadRegisters(gspca_dev);
399         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
400         reg_w_2(gspca_dev, TV8532_ADHEIGHT_L,
401                 ADHEIGHL, ADHEIGHH);    /* 401d 0x0169; 0e */
402 /*      reg_w_2(gspca_dev, TV8532_EXPOSURE,
403                 EXPOL, EXPOH);           * 350d 0x014c; 1c */
404         setbrightness(gspca_dev);
405
406         reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
407         reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
408
409         /************************************************/
410         reg_w_1(gspca_dev, TV8532_QUANT_COMP,
411                         TESTCOMP);      /* 0x72 compressed mode 0x28 */
412         if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
413                 /* 176x144 */
414                 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
415                         QCIFLINE);      /* 0x84; // CIF | 4 packet 0x29 */
416         } else {
417                 /* 352x288 */
418                 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
419                         TESTLINE);      /* 0x84; // CIF | 4 packet 0x29 */
420         }
421         /************************************************/
422         reg_w_1(gspca_dev, TV8532_SETCLK,
423                         TESTCLK);               /* 0x48; //0x08; 0x2c */
424         reg_w_1(gspca_dev, TV8532_POINT_L,
425                         TESTPTL);               /* 0x38; 0x2d */
426         reg_w_1(gspca_dev, TV8532_POINT_H,
427                         TESTPTH);               /* 0x04; 0x2e */
428         reg_w_1(gspca_dev, TV8532_POINTB_L,
429                         TESTPTBL);              /* 0x04; 0x2f */
430         reg_w_1(gspca_dev, TV8532_POINTB_H,
431                         TESTPTBH);              /* 0x04; 0x30 */
432         reg_w_1(gspca_dev, TV8532_PART_CTRL,
433                         TV8532_CMD_UPDATE);     /* 0x00<-0x84 */
434         /************************************************/
435         reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01);    /* 0x31 */
436         msleep(200);
437         reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00);    /* 0x31 */
438         /************************************************/
439         tv_8532_setReg(gspca_dev);
440         /************************************************/
441         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
442         /************************************************/
443         tv_8532_setReg(gspca_dev);
444         /************************************************/
445         tv_8532_PollReg(gspca_dev);
446         reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00);    /* 0x31 */
447 }
448
449 static void sd_stopN(struct gspca_dev *gspca_dev)
450 {
451         reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
452 }
453
454 static void sd_stop0(struct gspca_dev *gspca_dev)
455 {
456 }
457
458 static void sd_close(struct gspca_dev *gspca_dev)
459 {
460 }
461
462 static void tv8532_preprocess(struct gspca_dev *gspca_dev)
463 {
464         struct sd *sd = (struct sd *) gspca_dev;
465 /* we should received a whole frame with header and EOL marker
466  * in gspca_dev->tmpbuf and return a GBRG pattern in gspca_dev->tmpbuf2
467  * sequence 2bytes header the Alternate pixels bayer GB 4 bytes
468  * Alternate pixels bayer RG 4 bytes EOL */
469         int width = gspca_dev->width;
470         int height = gspca_dev->height;
471         unsigned char *dst = sd->tmpbuf2;
472         unsigned char *data = sd->tmpbuf;
473         int i;
474
475         /* precompute where is the good bayer line */
476         if (((data[3] + data[width + 7]) >> 1)
477             + (data[4] >> 2)
478             + (data[width + 6] >> 1) >= ((data[2] + data[width + 6]) >> 1)
479             + (data[3] >> 2)
480             + (data[width + 5] >> 1))
481                 data += 3;
482         else
483                 data += 2;
484         for (i = 0; i < height / 2; i++) {
485                 memcpy(dst, data, width);
486                 data += width + 3;
487                 dst += width;
488                 memcpy(dst, data, width);
489                 data += width + 7;
490                 dst += width;
491         }
492 }
493
494 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
495                         struct gspca_frame *frame,      /* target */
496                         __u8 *data,                     /* isoc packet */
497                         int len)                        /* iso packet length */
498 {
499         struct sd *sd = (struct sd *) gspca_dev;
500
501         if (data[0] != 0x80) {
502                 sd->packet++;
503                 if (sd->buflen + len > sizeof sd->tmpbuf) {
504                         if (gspca_dev->last_packet_type != DISCARD_PACKET) {
505                                 PDEBUG(D_PACK, "buffer overflow");
506                                 gspca_dev->last_packet_type = DISCARD_PACKET;
507                         }
508                         return;
509                 }
510                 memcpy(&sd->tmpbuf[sd->buflen], data, len);
511                 sd->buflen += len;
512                 return;
513         }
514
515         /* here we detect 0x80 */
516         /* counter is limited so we need few header for a frame :) */
517
518         /* header 0x80 0x80 0x80 0x80 0x80 */
519         /* packet  00   63  127  145  00   */
520         /* sof     0     1   1    0    0   */
521
522         /* update sequence */
523         if (sd->packet == 63 || sd->packet == 127)
524                 sd->synchro = 1;
525
526         /* is there a frame start ? */
527         if (sd->packet >= (gspca_dev->height >> 1) - 1) {
528                 PDEBUG(D_PACK, "SOF > %d packet %d", sd->synchro,
529                        sd->packet);
530                 if (!sd->synchro) {     /* start of frame */
531                         if (gspca_dev->last_packet_type == FIRST_PACKET) {
532                                 tv8532_preprocess(gspca_dev);
533                                 frame = gspca_frame_add(gspca_dev,
534                                                         LAST_PACKET,
535                                                         frame, sd->tmpbuf2,
536                                                         gspca_dev->width *
537                                                             gspca_dev->width);
538                         }
539                         gspca_frame_add(gspca_dev, FIRST_PACKET,
540                                         frame, data, 0);
541                         memcpy(sd->tmpbuf, data, len);
542                         sd->buflen = len;
543                         sd->packet = 0;
544                         return;
545                 }
546                 if (gspca_dev->last_packet_type != DISCARD_PACKET) {
547                         PDEBUG(D_PACK,
548                                "Warning wrong TV8532 frame detection %d",
549                                sd->packet);
550                         gspca_dev->last_packet_type = DISCARD_PACKET;
551                 }
552                 return;
553         }
554
555         if (!sd->synchro) {
556                 /* Drop packet frame corrupt */
557                 PDEBUG(D_PACK, "DROP SOF %d packet %d",
558                        sd->synchro, sd->packet);
559                 sd->packet = 0;
560                 gspca_dev->last_packet_type = DISCARD_PACKET;
561                 return;
562         }
563         sd->synchro = 1;
564         sd->packet++;
565         memcpy(&sd->tmpbuf[sd->buflen], data, len);
566         sd->buflen += len;
567 }
568
569 static void setcontrast(struct gspca_dev *gspca_dev)
570 {
571 }
572
573 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
574 {
575         struct sd *sd = (struct sd *) gspca_dev;
576
577         sd->brightness = val;
578         if (gspca_dev->streaming)
579                 setbrightness(gspca_dev);
580         return 0;
581 }
582
583 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
584 {
585         struct sd *sd = (struct sd *) gspca_dev;
586
587         *val = sd->brightness;
588         return 0;
589 }
590
591 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
592 {
593         struct sd *sd = (struct sd *) gspca_dev;
594
595         sd->contrast = val;
596         if (gspca_dev->streaming)
597                 setcontrast(gspca_dev);
598         return 0;
599 }
600
601 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
602 {
603         struct sd *sd = (struct sd *) gspca_dev;
604
605         *val = sd->contrast;
606         return 0;
607 }
608
609 /* sub-driver description */
610 static const struct sd_desc sd_desc = {
611         .name = MODULE_NAME,
612         .ctrls = sd_ctrls,
613         .nctrls = ARRAY_SIZE(sd_ctrls),
614         .config = sd_config,
615         .open = sd_open,
616         .start = sd_start,
617         .stopN = sd_stopN,
618         .stop0 = sd_stop0,
619         .close = sd_close,
620         .pkt_scan = sd_pkt_scan,
621 };
622
623 /* -- module initialisation -- */
624 #define DVNM(name) .driver_info = (kernel_ulong_t) name
625 static const __devinitdata struct usb_device_id device_table[] = {
626         {USB_DEVICE(0x046d, 0x0920), DVNM("QC Express")},
627         {USB_DEVICE(0x046d, 0x0921), DVNM("Labtec Webcam")},
628         {USB_DEVICE(0x0545, 0x808b), DVNM("Veo Stingray")},
629         {USB_DEVICE(0x0545, 0x8333), DVNM("Veo Stingray")},
630         {USB_DEVICE(0x0923, 0x010f), DVNM("ICM532 cams")},
631         {}
632 };
633
634 MODULE_DEVICE_TABLE(usb, device_table);
635
636 /* -- device connect -- */
637 static int sd_probe(struct usb_interface *intf,
638                     const struct usb_device_id *id)
639 {
640         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
641                                THIS_MODULE);
642 }
643
644 static struct usb_driver sd_driver = {
645         .name = MODULE_NAME,
646         .id_table = device_table,
647         .probe = sd_probe,
648         .disconnect = gspca_disconnect,
649 };
650
651 /* -- module insert / remove -- */
652 static int __init sd_mod_init(void)
653 {
654         if (usb_register(&sd_driver) < 0)
655                 return -1;
656         PDEBUG(D_PROBE, "registered");
657         return 0;
658 }
659
660 static void __exit sd_mod_exit(void)
661 {
662         usb_deregister(&sd_driver);
663         PDEBUG(D_PROBE, "deregistered");
664 }
665
666 module_init(sd_mod_init);
667 module_exit(sd_mod_exit);