d125763650386f63208254bad5ea13002f8252db
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / gspca / pac7311.c
1 /*
2  *              Pixart PAC7311 library
3  *              Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
4  *
5  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 /* Some documentation about various registers as determined by trial and error.
23
24    Register page 1:
25
26    Address      Description
27    0x08         Unknown compressor related, must always be 8 except when not
28                 in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
29    0x1b         Auto white balance related, bit 0 is AWB enable (inverted)
30                 bits 345 seem to toggle per color gains on/off (inverted)
31    0x78         Global control, bit 6 controls the LED (inverted)
32    0x80         JPEG compression ratio ? Best not touched
33
34    Register page 4:
35
36    Address      Description
37    0x02         Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
38                 the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
39    0x0f         Master gain 1-245, low value = high gain
40    0x10         Another gain 0-15, limited influence (1-2x gain I guess)
41    0x21         Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
42    0x27         Seems to toggle various gains on / off, Setting bit 7 seems to
43                 completely disable the analog amplification block. Set to 0x68
44                 for max gain, 0x14 for minimal gain.
45 */
46
47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48
49 #define MODULE_NAME "pac7311"
50
51 #include <linux/input.h>
52 #include "gspca.h"
53
54 MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
55 MODULE_DESCRIPTION("Pixart PAC7311");
56 MODULE_LICENSE("GPL");
57
58 struct sd {
59         struct gspca_dev gspca_dev;             /* !! must be the first item */
60
61         unsigned char contrast;
62         unsigned char gain;
63         unsigned char exposure;
64         unsigned char autogain;
65         __u8 hflip;
66         __u8 vflip;
67
68         u8 sof_read;
69         u8 autogain_ignore_frames;
70
71         atomic_t avg_lum;
72 };
73
74 /* V4L2 controls supported by the driver */
75 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
87
88 static const struct ctrl sd_ctrls[] = {
89         {
90             {
91                 .id      = V4L2_CID_CONTRAST,
92                 .type    = V4L2_CTRL_TYPE_INTEGER,
93                 .name    = "Contrast",
94                 .minimum = 0,
95 #define CONTRAST_MAX 255
96                 .maximum = CONTRAST_MAX,
97                 .step    = 1,
98 #define CONTRAST_DEF 127
99                 .default_value = CONTRAST_DEF,
100             },
101             .set = sd_setcontrast,
102             .get = sd_getcontrast,
103         },
104         {
105             {
106                 .id      = V4L2_CID_GAIN,
107                 .type    = V4L2_CTRL_TYPE_INTEGER,
108                 .name    = "Gain",
109                 .minimum = 0,
110 #define GAIN_MAX 255
111                 .maximum = GAIN_MAX,
112                 .step    = 1,
113 #define GAIN_DEF 127
114 #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
115                 .default_value = GAIN_DEF,
116             },
117             .set = sd_setgain,
118             .get = sd_getgain,
119         },
120         {
121             {
122                 .id      = V4L2_CID_EXPOSURE,
123                 .type    = V4L2_CTRL_TYPE_INTEGER,
124                 .name    = "Exposure",
125                 .minimum = 0,
126 #define EXPOSURE_MAX 255
127                 .maximum = EXPOSURE_MAX,
128                 .step    = 1,
129 #define EXPOSURE_DEF  16 /*  32 ms / 30 fps */
130 #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
131                 .default_value = EXPOSURE_DEF,
132             },
133             .set = sd_setexposure,
134             .get = sd_getexposure,
135         },
136         {
137             {
138                 .id      = V4L2_CID_AUTOGAIN,
139                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
140                 .name    = "Auto Gain",
141                 .minimum = 0,
142                 .maximum = 1,
143                 .step    = 1,
144 #define AUTOGAIN_DEF 1
145                 .default_value = AUTOGAIN_DEF,
146             },
147             .set = sd_setautogain,
148             .get = sd_getautogain,
149         },
150         {
151             {
152                 .id      = V4L2_CID_HFLIP,
153                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
154                 .name    = "Mirror",
155                 .minimum = 0,
156                 .maximum = 1,
157                 .step    = 1,
158 #define HFLIP_DEF 0
159                 .default_value = HFLIP_DEF,
160             },
161             .set = sd_sethflip,
162             .get = sd_gethflip,
163         },
164         {
165             {
166                 .id      = V4L2_CID_VFLIP,
167                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
168                 .name    = "Vflip",
169                 .minimum = 0,
170                 .maximum = 1,
171                 .step    = 1,
172 #define VFLIP_DEF 0
173                 .default_value = VFLIP_DEF,
174             },
175             .set = sd_setvflip,
176             .get = sd_getvflip,
177         },
178 };
179
180 static const struct v4l2_pix_format vga_mode[] = {
181         {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
182                 .bytesperline = 160,
183                 .sizeimage = 160 * 120 * 3 / 8 + 590,
184                 .colorspace = V4L2_COLORSPACE_JPEG,
185                 .priv = 2},
186         {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
187                 .bytesperline = 320,
188                 .sizeimage = 320 * 240 * 3 / 8 + 590,
189                 .colorspace = V4L2_COLORSPACE_JPEG,
190                 .priv = 1},
191         {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
192                 .bytesperline = 640,
193                 .sizeimage = 640 * 480 * 3 / 8 + 590,
194                 .colorspace = V4L2_COLORSPACE_JPEG,
195                 .priv = 0},
196 };
197
198 #define LOAD_PAGE4              254
199 #define END_OF_SEQUENCE         0
200
201 static const __u8 init_7311[] = {
202         0x78, 0x40,     /* Bit_0=start stream, Bit_6=LED */
203         0x78, 0x40,     /* Bit_0=start stream, Bit_6=LED */
204         0x78, 0x44,     /* Bit_0=start stream, Bit_6=LED */
205         0xff, 0x04,
206         0x27, 0x80,
207         0x28, 0xca,
208         0x29, 0x53,
209         0x2a, 0x0e,
210         0xff, 0x01,
211         0x3e, 0x20,
212 };
213
214 static const __u8 start_7311[] = {
215 /*      index, len, [value]* */
216         0xff, 1,        0x01,           /* page 1 */
217         0x02, 43,       0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
218                         0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
219                         0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
220                         0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
221                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222                         0x00, 0x00, 0x00,
223         0x3e, 42,       0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
224                         0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
225                         0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
226                         0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
227                         0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
228                         0xd0, 0xff,
229         0x78, 6,        0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
230         0x7f, 18,       0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
231                         0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
232                         0x18, 0x20,
233         0x96, 3,        0x01, 0x08, 0x04,
234         0xa0, 4,        0x44, 0x44, 0x44, 0x04,
235         0xf0, 13,       0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
236                         0x3f, 0x00, 0x0a, 0x01, 0x00,
237         0xff, 1,        0x04,           /* page 4 */
238         0, LOAD_PAGE4,                  /* load the page 4 */
239         0x11, 1,        0x01,
240         0, END_OF_SEQUENCE              /* end of sequence */
241 };
242
243 #define SKIP            0xaa
244 /* page 4 - the value SKIP says skip the index - see reg_w_page() */
245 static const __u8 page4_7311[] = {
246         SKIP, SKIP, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
247         0x09, 0x00, SKIP, SKIP, 0x07, 0x00, 0x00, 0x62,
248         0x08, SKIP, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
249         0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, SKIP,
250         SKIP, 0x00, 0x08, SKIP, 0x03, SKIP, 0x00, 0x68,
251         0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
252         0x23, 0x28, 0x04, 0x11, 0x00, 0x00
253 };
254
255 static void reg_w_buf(struct gspca_dev *gspca_dev,
256                   __u8 index,
257                   const u8 *buffer, int len)
258 {
259         int ret;
260
261         if (gspca_dev->usb_err < 0)
262                 return;
263         memcpy(gspca_dev->usb_buf, buffer, len);
264         ret = usb_control_msg(gspca_dev->dev,
265                         usb_sndctrlpipe(gspca_dev->dev, 0),
266                         0,              /* request */
267                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
268                         0,              /* value */
269                         index, gspca_dev->usb_buf, len,
270                         500);
271         if (ret < 0) {
272                 pr_err("reg_w_buf() failed index 0x%02x, error %d\n",
273                        index, ret);
274                 gspca_dev->usb_err = ret;
275         }
276 }
277
278
279 static void reg_w(struct gspca_dev *gspca_dev,
280                   __u8 index,
281                   __u8 value)
282 {
283         int ret;
284
285         if (gspca_dev->usb_err < 0)
286                 return;
287         gspca_dev->usb_buf[0] = value;
288         ret = usb_control_msg(gspca_dev->dev,
289                         usb_sndctrlpipe(gspca_dev->dev, 0),
290                         0,                      /* request */
291                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
292                         0, index, gspca_dev->usb_buf, 1,
293                         500);
294         if (ret < 0) {
295                 pr_err("reg_w() failed index 0x%02x, value 0x%02x, error %d\n",
296                        index, value, ret);
297                 gspca_dev->usb_err = ret;
298         }
299 }
300
301 static void reg_w_seq(struct gspca_dev *gspca_dev,
302                 const __u8 *seq, int len)
303 {
304         while (--len >= 0) {
305                 reg_w(gspca_dev, seq[0], seq[1]);
306                 seq += 2;
307         }
308 }
309
310 /* load the beginning of a page */
311 static void reg_w_page(struct gspca_dev *gspca_dev,
312                         const __u8 *page, int len)
313 {
314         int index;
315         int ret = 0;
316
317         if (gspca_dev->usb_err < 0)
318                 return;
319         for (index = 0; index < len; index++) {
320                 if (page[index] == SKIP)                /* skip this index */
321                         continue;
322                 gspca_dev->usb_buf[0] = page[index];
323                 ret = usb_control_msg(gspca_dev->dev,
324                                 usb_sndctrlpipe(gspca_dev->dev, 0),
325                                 0,                      /* request */
326                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
327                                 0, index, gspca_dev->usb_buf, 1,
328                                 500);
329                 if (ret < 0) {
330                         pr_err("reg_w_page() failed index 0x%02x, value 0x%02x, error %d\n",
331                                index, page[index], ret);
332                         gspca_dev->usb_err = ret;
333                         break;
334                 }
335         }
336 }
337
338 /* output a variable sequence */
339 static void reg_w_var(struct gspca_dev *gspca_dev,
340                         const __u8 *seq,
341                         const __u8 *page4, unsigned int page4_len)
342 {
343         int index, len;
344
345         for (;;) {
346                 index = *seq++;
347                 len = *seq++;
348                 switch (len) {
349                 case END_OF_SEQUENCE:
350                         return;
351                 case LOAD_PAGE4:
352                         reg_w_page(gspca_dev, page4, page4_len);
353                         break;
354                 default:
355                         if (len > USB_BUF_SZ) {
356                                 PDEBUG(D_ERR|D_STREAM,
357                                         "Incorrect variable sequence");
358                                 return;
359                         }
360                         while (len > 0) {
361                                 if (len < 8) {
362                                         reg_w_buf(gspca_dev,
363                                                 index, seq, len);
364                                         seq += len;
365                                         break;
366                                 }
367                                 reg_w_buf(gspca_dev, index, seq, 8);
368                                 seq += 8;
369                                 index += 8;
370                                 len -= 8;
371                         }
372                 }
373         }
374         /* not reached */
375 }
376
377 /* this function is called at probe time for pac7311 */
378 static int sd_config(struct gspca_dev *gspca_dev,
379                         const struct usb_device_id *id)
380 {
381         struct sd *sd = (struct sd *) gspca_dev;
382         struct cam *cam;
383
384         cam = &gspca_dev->cam;
385
386         cam->cam_mode = vga_mode;
387         cam->nmodes = ARRAY_SIZE(vga_mode);
388
389         sd->contrast = CONTRAST_DEF;
390         sd->gain = GAIN_DEF;
391         sd->exposure = EXPOSURE_DEF;
392         sd->autogain = AUTOGAIN_DEF;
393         sd->hflip = HFLIP_DEF;
394         sd->vflip = VFLIP_DEF;
395         return 0;
396 }
397
398 static void setcontrast(struct gspca_dev *gspca_dev)
399 {
400         struct sd *sd = (struct sd *) gspca_dev;
401
402         reg_w(gspca_dev, 0xff, 0x04);
403         reg_w(gspca_dev, 0x10, sd->contrast >> 4);
404         /* load registers to sensor (Bit 0, auto clear) */
405         reg_w(gspca_dev, 0x11, 0x01);
406 }
407
408 static void setgain(struct gspca_dev *gspca_dev)
409 {
410         struct sd *sd = (struct sd *) gspca_dev;
411         int gain = GAIN_MAX - sd->gain;
412
413         if (gain < 1)
414                 gain = 1;
415         else if (gain > 245)
416                 gain = 245;
417         reg_w(gspca_dev, 0xff, 0x04);                   /* page 4 */
418         reg_w(gspca_dev, 0x0e, 0x00);
419         reg_w(gspca_dev, 0x0f, gain);
420
421         /* load registers to sensor (Bit 0, auto clear) */
422         reg_w(gspca_dev, 0x11, 0x01);
423 }
424
425 static void setexposure(struct gspca_dev *gspca_dev)
426 {
427         struct sd *sd = (struct sd *) gspca_dev;
428         __u8 reg;
429
430         /* register 2 of page 4 contains the clock divider configuring the
431            no fps according to the formula: 60 / reg. sd->exposure is the
432            desired exposure time in ms. */
433         reg = 120 * sd->exposure / 1000;
434         if (reg < 2)
435                 reg = 2;
436         else if (reg > 63)
437                 reg = 63;
438
439         reg_w(gspca_dev, 0xff, 0x04);                   /* page 4 */
440         reg_w(gspca_dev, 0x02, reg);
441
442         /* load registers to sensor (Bit 0, auto clear) */
443         reg_w(gspca_dev, 0x11, 0x01);
444
445         /* Page 1 register 8 must always be 0x08 except when not in
446            640x480 mode and page 4 reg 2 <= 3 then it must be 9 */
447         reg_w(gspca_dev, 0xff, 0x01);
448         if (gspca_dev->cam.cam_mode[(int)gspca_dev->curr_mode].priv &&
449                         reg <= 3) {
450                 reg_w(gspca_dev, 0x08, 0x09);
451         } else {
452                 reg_w(gspca_dev, 0x08, 0x08);
453         }
454
455         /* load registers to sensor (Bit 0, auto clear) */
456         reg_w(gspca_dev, 0x11, 0x01);
457 }
458
459 static void sethvflip(struct gspca_dev *gspca_dev)
460 {
461         struct sd *sd = (struct sd *) gspca_dev;
462         __u8 data;
463
464         reg_w(gspca_dev, 0xff, 0x04);                   /* page 4 */
465         data = (sd->hflip ? 0x04 : 0x00) | (sd->vflip ? 0x08 : 0x00);
466         reg_w(gspca_dev, 0x21, data);
467
468         /* load registers to sensor (Bit 0, auto clear) */
469         reg_w(gspca_dev, 0x11, 0x01);
470 }
471
472 /* this function is called at probe and resume time for pac7311 */
473 static int sd_init(struct gspca_dev *gspca_dev)
474 {
475         reg_w_seq(gspca_dev, init_7311, sizeof(init_7311)/2);
476         return gspca_dev->usb_err;
477 }
478
479 static int sd_start(struct gspca_dev *gspca_dev)
480 {
481         struct sd *sd = (struct sd *) gspca_dev;
482
483         sd->sof_read = 0;
484
485         reg_w_var(gspca_dev, start_7311,
486                 page4_7311, sizeof(page4_7311));
487         setcontrast(gspca_dev);
488         setgain(gspca_dev);
489         setexposure(gspca_dev);
490         sethvflip(gspca_dev);
491
492         /* set correct resolution */
493         switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
494         case 2:                                 /* 160x120 */
495                 reg_w(gspca_dev, 0xff, 0x01);
496                 reg_w(gspca_dev, 0x17, 0x20);
497                 reg_w(gspca_dev, 0x87, 0x10);
498                 break;
499         case 1:                                 /* 320x240 */
500                 reg_w(gspca_dev, 0xff, 0x01);
501                 reg_w(gspca_dev, 0x17, 0x30);
502                 reg_w(gspca_dev, 0x87, 0x11);
503                 break;
504         case 0:                                 /* 640x480 */
505                 reg_w(gspca_dev, 0xff, 0x01);
506                 reg_w(gspca_dev, 0x17, 0x00);
507                 reg_w(gspca_dev, 0x87, 0x12);
508                 break;
509         }
510
511         sd->sof_read = 0;
512         sd->autogain_ignore_frames = 0;
513         atomic_set(&sd->avg_lum, -1);
514
515         /* start stream */
516         reg_w(gspca_dev, 0xff, 0x01);
517         reg_w(gspca_dev, 0x78, 0x05);
518
519         return gspca_dev->usb_err;
520 }
521
522 static void sd_stopN(struct gspca_dev *gspca_dev)
523 {
524         reg_w(gspca_dev, 0xff, 0x04);
525         reg_w(gspca_dev, 0x27, 0x80);
526         reg_w(gspca_dev, 0x28, 0xca);
527         reg_w(gspca_dev, 0x29, 0x53);
528         reg_w(gspca_dev, 0x2a, 0x0e);
529         reg_w(gspca_dev, 0xff, 0x01);
530         reg_w(gspca_dev, 0x3e, 0x20);
531         reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
532         reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
533         reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
534 }
535
536 /* Include pac common sof detection functions */
537 #include "pac_common.h"
538
539 static void do_autogain(struct gspca_dev *gspca_dev)
540 {
541         struct sd *sd = (struct sd *) gspca_dev;
542         int avg_lum = atomic_read(&sd->avg_lum);
543         int desired_lum, deadzone;
544
545         if (avg_lum == -1)
546                 return;
547
548         desired_lum = 200;
549         deadzone = 20;
550
551         if (sd->autogain_ignore_frames > 0)
552                 sd->autogain_ignore_frames--;
553         else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
554                         deadzone, GAIN_KNEE, EXPOSURE_KNEE))
555                 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
556 }
557
558 /* JPEG header, part 1 */
559 static const unsigned char pac_jpeg_header1[] = {
560   0xff, 0xd8,           /* SOI: Start of Image */
561
562   0xff, 0xc0,           /* SOF0: Start of Frame (Baseline DCT) */
563   0x00, 0x11,           /* length = 17 bytes (including this length field) */
564   0x08                  /* Precision: 8 */
565   /* 2 bytes is placed here: number of image lines */
566   /* 2 bytes is placed here: samples per line */
567 };
568
569 /* JPEG header, continued */
570 static const unsigned char pac_jpeg_header2[] = {
571   0x03,                 /* Number of image components: 3 */
572   0x01, 0x21, 0x00,     /* ID=1, Subsampling 1x1, Quantization table: 0 */
573   0x02, 0x11, 0x01,     /* ID=2, Subsampling 2x1, Quantization table: 1 */
574   0x03, 0x11, 0x01,     /* ID=3, Subsampling 2x1, Quantization table: 1 */
575
576   0xff, 0xda,           /* SOS: Start Of Scan */
577   0x00, 0x0c,           /* length = 12 bytes (including this length field) */
578   0x03,                 /* number of components: 3 */
579   0x01, 0x00,           /* selector 1, table 0x00 */
580   0x02, 0x11,           /* selector 2, table 0x11 */
581   0x03, 0x11,           /* selector 3, table 0x11 */
582   0x00, 0x3f,           /* Spectral selection: 0 .. 63 */
583   0x00                  /* Successive approximation: 0 */
584 };
585
586 static void pac_start_frame(struct gspca_dev *gspca_dev,
587                 __u16 lines, __u16 samples_per_line)
588 {
589         unsigned char tmpbuf[4];
590
591         gspca_frame_add(gspca_dev, FIRST_PACKET,
592                 pac_jpeg_header1, sizeof(pac_jpeg_header1));
593
594         tmpbuf[0] = lines >> 8;
595         tmpbuf[1] = lines & 0xff;
596         tmpbuf[2] = samples_per_line >> 8;
597         tmpbuf[3] = samples_per_line & 0xff;
598
599         gspca_frame_add(gspca_dev, INTER_PACKET,
600                 tmpbuf, sizeof(tmpbuf));
601         gspca_frame_add(gspca_dev, INTER_PACKET,
602                 pac_jpeg_header2, sizeof(pac_jpeg_header2));
603 }
604
605 /* this function is run at interrupt level */
606 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
607                         u8 *data,                       /* isoc packet */
608                         int len)                        /* iso packet length */
609 {
610         struct sd *sd = (struct sd *) gspca_dev;
611         u8 *image;
612         unsigned char *sof;
613
614         sof = pac_find_sof(&sd->sof_read, data, len);
615         if (sof) {
616                 int n, lum_offset, footer_length;
617
618                 /* 6 bytes after the FF D9 EOF marker a number of lumination
619                    bytes are send corresponding to different parts of the
620                    image, the 14th and 15th byte after the EOF seem to
621                    correspond to the center of the image */
622                 lum_offset = 24 + sizeof pac_sof_marker;
623                 footer_length = 26;
624
625                 /* Finish decoding current frame */
626                 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
627                 if (n < 0) {
628                         gspca_dev->image_len += n;
629                         n = 0;
630                 } else {
631                         gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
632                 }
633                 image = gspca_dev->image;
634                 if (image != NULL
635                  && image[gspca_dev->image_len - 2] == 0xff
636                  && image[gspca_dev->image_len - 1] == 0xd9)
637                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
638
639                 n = sof - data;
640                 len -= n;
641                 data = sof;
642
643                 /* Get average lumination */
644                 if (gspca_dev->last_packet_type == LAST_PACKET &&
645                                 n >= lum_offset)
646                         atomic_set(&sd->avg_lum, data[-lum_offset] +
647                                                 data[-lum_offset + 1]);
648                 else
649                         atomic_set(&sd->avg_lum, -1);
650
651                 /* Start the new frame with the jpeg header */
652                 pac_start_frame(gspca_dev,
653                         gspca_dev->height, gspca_dev->width);
654         }
655         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
656 }
657
658 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
659 {
660         struct sd *sd = (struct sd *) gspca_dev;
661
662         sd->contrast = val;
663         if (gspca_dev->streaming)
664                 setcontrast(gspca_dev);
665         return gspca_dev->usb_err;
666 }
667
668 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
669 {
670         struct sd *sd = (struct sd *) gspca_dev;
671
672         *val = sd->contrast;
673         return 0;
674 }
675
676 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
677 {
678         struct sd *sd = (struct sd *) gspca_dev;
679
680         sd->gain = val;
681         if (gspca_dev->streaming)
682                 setgain(gspca_dev);
683         return gspca_dev->usb_err;
684 }
685
686 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
687 {
688         struct sd *sd = (struct sd *) gspca_dev;
689
690         *val = sd->gain;
691         return 0;
692 }
693
694 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
695 {
696         struct sd *sd = (struct sd *) gspca_dev;
697
698         sd->exposure = val;
699         if (gspca_dev->streaming)
700                 setexposure(gspca_dev);
701         return gspca_dev->usb_err;
702 }
703
704 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
705 {
706         struct sd *sd = (struct sd *) gspca_dev;
707
708         *val = sd->exposure;
709         return 0;
710 }
711
712 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
713 {
714         struct sd *sd = (struct sd *) gspca_dev;
715
716         sd->autogain = val;
717         /* when switching to autogain set defaults to make sure
718            we are on a valid point of the autogain gain /
719            exposure knee graph, and give this change time to
720            take effect before doing autogain. */
721         if (sd->autogain) {
722                 sd->exposure = EXPOSURE_DEF;
723                 sd->gain = GAIN_DEF;
724                 if (gspca_dev->streaming) {
725                         sd->autogain_ignore_frames =
726                                 PAC_AUTOGAIN_IGNORE_FRAMES;
727                         setexposure(gspca_dev);
728                         setgain(gspca_dev);
729                 }
730         }
731
732         return gspca_dev->usb_err;
733 }
734
735 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
736 {
737         struct sd *sd = (struct sd *) gspca_dev;
738
739         *val = sd->autogain;
740         return 0;
741 }
742
743 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
744 {
745         struct sd *sd = (struct sd *) gspca_dev;
746
747         sd->hflip = val;
748         if (gspca_dev->streaming)
749                 sethvflip(gspca_dev);
750         return gspca_dev->usb_err;
751 }
752
753 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
754 {
755         struct sd *sd = (struct sd *) gspca_dev;
756
757         *val = sd->hflip;
758         return 0;
759 }
760
761 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
762 {
763         struct sd *sd = (struct sd *) gspca_dev;
764
765         sd->vflip = val;
766         if (gspca_dev->streaming)
767                 sethvflip(gspca_dev);
768         return gspca_dev->usb_err;
769 }
770
771 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
772 {
773         struct sd *sd = (struct sd *) gspca_dev;
774
775         *val = sd->vflip;
776         return 0;
777 }
778
779 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
780 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
781                         u8 *data,               /* interrupt packet data */
782                         int len)                /* interrupt packet length */
783 {
784         int ret = -EINVAL;
785         u8 data0, data1;
786
787         if (len == 2) {
788                 data0 = data[0];
789                 data1 = data[1];
790                 if ((data0 == 0x00 && data1 == 0x11) ||
791                     (data0 == 0x22 && data1 == 0x33) ||
792                     (data0 == 0x44 && data1 == 0x55) ||
793                     (data0 == 0x66 && data1 == 0x77) ||
794                     (data0 == 0x88 && data1 == 0x99) ||
795                     (data0 == 0xaa && data1 == 0xbb) ||
796                     (data0 == 0xcc && data1 == 0xdd) ||
797                     (data0 == 0xee && data1 == 0xff)) {
798                         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
799                         input_sync(gspca_dev->input_dev);
800                         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
801                         input_sync(gspca_dev->input_dev);
802                         ret = 0;
803                 }
804         }
805
806         return ret;
807 }
808 #endif
809
810 static const struct sd_desc sd_desc = {
811         .name = MODULE_NAME,
812         .ctrls = sd_ctrls,
813         .nctrls = ARRAY_SIZE(sd_ctrls),
814         .config = sd_config,
815         .init = sd_init,
816         .start = sd_start,
817         .stopN = sd_stopN,
818         .pkt_scan = sd_pkt_scan,
819         .dq_callback = do_autogain,
820 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
821         .int_pkt_scan = sd_int_pkt_scan,
822 #endif
823 };
824
825 /* -- module initialisation -- */
826 static const struct usb_device_id device_table[] = {
827         {USB_DEVICE(0x093a, 0x2600)},
828         {USB_DEVICE(0x093a, 0x2601)},
829         {USB_DEVICE(0x093a, 0x2603)},
830         {USB_DEVICE(0x093a, 0x2608)},
831         {USB_DEVICE(0x093a, 0x260e)},
832         {USB_DEVICE(0x093a, 0x260f)},
833         {}
834 };
835 MODULE_DEVICE_TABLE(usb, device_table);
836
837 /* -- device connect -- */
838 static int sd_probe(struct usb_interface *intf,
839                         const struct usb_device_id *id)
840 {
841         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
842                                 THIS_MODULE);
843 }
844
845 static struct usb_driver sd_driver = {
846         .name = MODULE_NAME,
847         .id_table = device_table,
848         .probe = sd_probe,
849         .disconnect = gspca_disconnect,
850 #ifdef CONFIG_PM
851         .suspend = gspca_suspend,
852         .resume = gspca_resume,
853 #endif
854 };
855
856 module_usb_driver(sd_driver);