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