4894ac19b0328d67b05ea2954fd2d88bdb3eb8c0
[linux-2.6-microblaze.git] / drivers / media / usb / gspca / pac7302.c
1 /*
2  * Pixart PAC7302 driver
3  *
4  * Copyright (C) 2008-2012 Jean-Francois Moine <http://moinejf.free.fr>
5  * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6  *
7  * Separated from Pixart PAC7311 library by Márton Németh
8  * Camera button input handling by Márton Németh <nm127@freemail.hu>
9  * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  */
25
26 /*
27  * Some documentation about various registers as determined by trial and error.
28  *
29  * Register page 0:
30  *
31  * Address      Description
32  * 0x01         Red balance control
33  * 0x02         Green balance control
34  * 0x03         Blue balance control
35  *                   The Windows driver uses a quadratic approach to map
36  *                   the settable values (0-200) on register values:
37  *                   min=0x20, default=0x40, max=0x80
38  * 0x0f-0x20    Color and saturation control
39  * 0xa2-0xab    Brightness, contrast and gamma control
40  * 0xb6         Sharpness control (bits 0-4)
41  *
42  * Register page 1:
43  *
44  * Address      Description
45  * 0x78         Global control, bit 6 controls the LED (inverted)
46  * 0x80         Compression balance, 2 interesting settings:
47  *              0x0f Default
48  *              0x50 Values >= this switch the camera to a lower compression,
49  *                   using the same table for both luminance and chrominance.
50  *                   This gives a sharper picture. Only usable when running
51  *                   at < 15 fps! Note currently the driver does not use this
52  *                   as the quality gain is small and the generated JPG-s are
53  *                   only understood by v4l-utils >= 0.8.9
54  *
55  * Register page 3:
56  *
57  * Address      Description
58  * 0x02         Clock divider 3-63, fps = 90 / val. Must be a multiple of 3 on
59  *              the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
60  * 0x03         Variable framerate ctrl reg2==3: 0 -> ~30 fps, 255 -> ~22fps
61  * 0x04         Another var framerate ctrl reg2==3, reg3==0: 0 -> ~30 fps,
62  *              63 -> ~27 fps, the 2 msb's must always be 1 !!
63  * 0x05         Another var framerate ctrl reg2==3, reg3==0, reg4==0xc0:
64  *              1 -> ~30 fps, 2 -> ~20 fps
65  * 0x0e         Exposure bits 0-7, 0-448, 0 = use full frame time
66  * 0x0f         Exposure bit 8, 0-448, 448 = no exposure at all
67  * 0x10         Gain 0-31
68  * 0x12         Another gain 0-31, unlike 0x10 this one seems to start with an
69  *              amplification value of 1 rather then 0 at its lowest setting
70  * 0x21         Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
71  * 0x80         Another framerate control, best left at 1, moving it from 1 to
72  *              2 causes the framerate to become 3/4th of what it was, and
73  *              also seems to cause pixel averaging, resulting in an effective
74  *              resolution of 320x240 and thus a much blockier image
75  *
76  * The registers are accessed in the following functions:
77  *
78  * Page | Register   | Function
79  * -----+------------+---------------------------------------------------
80  *  0   | 0x0f..0x20 | setcolors()
81  *  0   | 0xa2..0xab | setbrightcont()
82  *  0   | 0xb6       | setsharpness()
83  *  0   | 0xc5       | setredbalance()
84  *  0   | 0xc6       | setwhitebalance()
85  *  0   | 0xc7       | setbluebalance()
86  *  0   | 0xdc       | setbrightcont(), setcolors()
87  *  3   | 0x02       | setexposure()
88  *  3   | 0x10, 0x12 | setgain()
89  *  3   | 0x11       | setcolors(), setgain(), setexposure(), sethvflip()
90  *  3   | 0x21       | sethvflip()
91  */
92
93 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
94
95 #include <linux/input.h>
96 #include <media/v4l2-chip-ident.h>
97 #include "gspca.h"
98 /* Include pac common sof detection functions */
99 #include "pac_common.h"
100
101 #define PAC7302_GAIN_DEFAULT      15
102 #define PAC7302_GAIN_KNEE         42
103 #define PAC7302_EXPOSURE_DEFAULT  66 /* 33 ms / 30 fps */
104 #define PAC7302_EXPOSURE_KNEE    133 /* 66 ms / 15 fps */
105
106 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, "
107                 "Thomas Kaiser thomas@kaiser-linux.li");
108 MODULE_DESCRIPTION("Pixart PAC7302");
109 MODULE_LICENSE("GPL");
110
111 struct sd {
112         struct gspca_dev gspca_dev;             /* !! must be the first item */
113
114         struct { /* brightness / contrast cluster */
115                 struct v4l2_ctrl *brightness;
116                 struct v4l2_ctrl *contrast;
117         };
118         struct v4l2_ctrl *saturation;
119         struct v4l2_ctrl *white_balance;
120         struct v4l2_ctrl *red_balance;
121         struct v4l2_ctrl *blue_balance;
122         struct { /* flip cluster */
123                 struct v4l2_ctrl *hflip;
124                 struct v4l2_ctrl *vflip;
125         };
126         struct v4l2_ctrl *sharpness;
127         u8 flags;
128 #define FL_HFLIP 0x01           /* mirrored by default */
129 #define FL_VFLIP 0x02           /* vertical flipped by default */
130
131         u8 sof_read;
132         s8 autogain_ignore_frames;
133
134         atomic_t avg_lum;
135 };
136
137 static const struct v4l2_pix_format vga_mode[] = {
138         {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
139                 .bytesperline = 640,
140                 .sizeimage = 640 * 480 * 3 / 8 + 590,
141                 .colorspace = V4L2_COLORSPACE_JPEG,
142         },
143 };
144
145 #define LOAD_PAGE3              255
146 #define END_OF_SEQUENCE         0
147
148 static const u8 init_7302[] = {
149 /*      index,value */
150         0xff, 0x01,             /* page 1 */
151         0x78, 0x00,             /* deactivate */
152         0xff, 0x01,
153         0x78, 0x40,             /* led off */
154 };
155 static const u8 start_7302[] = {
156 /*      index, len, [value]* */
157         0xff, 1,        0x00,           /* page 0 */
158         0x00, 12,       0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
159                         0x00, 0x00, 0x00, 0x00,
160         0x0d, 24,       0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
161                         0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
162                         0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
163         0x26, 2,        0xaa, 0xaa,
164         0x2e, 1,        0x31,
165         0x38, 1,        0x01,
166         0x3a, 3,        0x14, 0xff, 0x5a,
167         0x43, 11,       0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
168                         0x00, 0x54, 0x11,
169         0x55, 1,        0x00,
170         0x62, 4,        0x10, 0x1e, 0x1e, 0x18,
171         0x6b, 1,        0x00,
172         0x6e, 3,        0x08, 0x06, 0x00,
173         0x72, 3,        0x00, 0xff, 0x00,
174         0x7d, 23,       0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
175                         0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
176                         0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
177         0xa2, 10,       0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
178                         0xd2, 0xeb,
179         0xaf, 1,        0x02,
180         0xb5, 2,        0x08, 0x08,
181         0xb8, 2,        0x08, 0x88,
182         0xc4, 4,        0xae, 0x01, 0x04, 0x01,
183         0xcc, 1,        0x00,
184         0xd1, 11,       0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
185                         0xc1, 0xd7, 0xec,
186         0xdc, 1,        0x01,
187         0xff, 1,        0x01,           /* page 1 */
188         0x12, 3,        0x02, 0x00, 0x01,
189         0x3e, 2,        0x00, 0x00,
190         0x76, 5,        0x01, 0x20, 0x40, 0x00, 0xf2,
191         0x7c, 1,        0x00,
192         0x7f, 10,       0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
193                         0x02, 0x00,
194         0x96, 5,        0x01, 0x10, 0x04, 0x01, 0x04,
195         0xc8, 14,       0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
196                         0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
197         0xd8, 1,        0x01,
198         0xdb, 2,        0x00, 0x01,
199         0xde, 7,        0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
200         0xe6, 4,        0x00, 0x00, 0x00, 0x01,
201         0xeb, 1,        0x00,
202         0xff, 1,        0x02,           /* page 2 */
203         0x22, 1,        0x00,
204         0xff, 1,        0x03,           /* page 3 */
205         0, LOAD_PAGE3,                  /* load the page 3 */
206         0x11, 1,        0x01,
207         0xff, 1,        0x02,           /* page 2 */
208         0x13, 1,        0x00,
209         0x22, 4,        0x1f, 0xa4, 0xf0, 0x96,
210         0x27, 2,        0x14, 0x0c,
211         0x2a, 5,        0xc8, 0x00, 0x18, 0x12, 0x22,
212         0x64, 8,        0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
213         0x6e, 1,        0x08,
214         0xff, 1,        0x01,           /* page 1 */
215         0x78, 1,        0x00,
216         0, END_OF_SEQUENCE              /* end of sequence */
217 };
218
219 #define SKIP            0xaa
220 /* page 3 - the value SKIP says skip the index - see reg_w_page() */
221 static const u8 page3_7302[] = {
222         0x90, 0x40, 0x03, 0x00, 0xc0, 0x01, 0x14, 0x16,
223         0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
224         0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225         0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
226         0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
227         0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
228         0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
229         0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230         0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
231         SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
232         0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233         0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
234         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235         0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
236         0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
237         0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
238         0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
239         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240         0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
241         0x00
242 };
243
244 static void reg_w_buf(struct gspca_dev *gspca_dev,
245                 u8 index,
246                   const u8 *buffer, int len)
247 {
248         int ret;
249
250         if (gspca_dev->usb_err < 0)
251                 return;
252         memcpy(gspca_dev->usb_buf, buffer, len);
253         ret = usb_control_msg(gspca_dev->dev,
254                         usb_sndctrlpipe(gspca_dev->dev, 0),
255                         0,              /* request */
256                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
257                         0,              /* value */
258                         index, gspca_dev->usb_buf, len,
259                         500);
260         if (ret < 0) {
261                 pr_err("reg_w_buf failed i: %02x error %d\n",
262                        index, ret);
263                 gspca_dev->usb_err = ret;
264         }
265 }
266
267
268 static void reg_w(struct gspca_dev *gspca_dev,
269                 u8 index,
270                 u8 value)
271 {
272         int ret;
273
274         if (gspca_dev->usb_err < 0)
275                 return;
276         gspca_dev->usb_buf[0] = value;
277         ret = usb_control_msg(gspca_dev->dev,
278                         usb_sndctrlpipe(gspca_dev->dev, 0),
279                         0,                      /* request */
280                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
281                         0, index, gspca_dev->usb_buf, 1,
282                         500);
283         if (ret < 0) {
284                 pr_err("reg_w() failed i: %02x v: %02x error %d\n",
285                        index, value, ret);
286                 gspca_dev->usb_err = ret;
287         }
288 }
289
290 static void reg_w_seq(struct gspca_dev *gspca_dev,
291                 const u8 *seq, int len)
292 {
293         while (--len >= 0) {
294                 reg_w(gspca_dev, seq[0], seq[1]);
295                 seq += 2;
296         }
297 }
298
299 /* load the beginning of a page */
300 static void reg_w_page(struct gspca_dev *gspca_dev,
301                         const u8 *page, int len)
302 {
303         int index;
304         int ret = 0;
305
306         if (gspca_dev->usb_err < 0)
307                 return;
308         for (index = 0; index < len; index++) {
309                 if (page[index] == SKIP)                /* skip this index */
310                         continue;
311                 gspca_dev->usb_buf[0] = page[index];
312                 ret = usb_control_msg(gspca_dev->dev,
313                                 usb_sndctrlpipe(gspca_dev->dev, 0),
314                                 0,                      /* request */
315                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
316                                 0, index, gspca_dev->usb_buf, 1,
317                                 500);
318                 if (ret < 0) {
319                         pr_err("reg_w_page() failed i: %02x v: %02x error %d\n",
320                                index, page[index], ret);
321                         gspca_dev->usb_err = ret;
322                         break;
323                 }
324         }
325 }
326
327 /* output a variable sequence */
328 static void reg_w_var(struct gspca_dev *gspca_dev,
329                         const u8 *seq,
330                         const u8 *page3, unsigned int page3_len)
331 {
332         int index, len;
333
334         for (;;) {
335                 index = *seq++;
336                 len = *seq++;
337                 switch (len) {
338                 case END_OF_SEQUENCE:
339                         return;
340                 case LOAD_PAGE3:
341                         reg_w_page(gspca_dev, page3, page3_len);
342                         break;
343                 default:
344 #ifdef GSPCA_DEBUG
345                         if (len > USB_BUF_SZ) {
346                                 PDEBUG(D_ERR|D_STREAM,
347                                         "Incorrect variable sequence");
348                                 return;
349                         }
350 #endif
351                         while (len > 0) {
352                                 if (len < 8) {
353                                         reg_w_buf(gspca_dev,
354                                                 index, seq, len);
355                                         seq += len;
356                                         break;
357                                 }
358                                 reg_w_buf(gspca_dev, index, seq, 8);
359                                 seq += 8;
360                                 index += 8;
361                                 len -= 8;
362                         }
363                 }
364         }
365         /* not reached */
366 }
367
368 /* this function is called at probe time for pac7302 */
369 static int sd_config(struct gspca_dev *gspca_dev,
370                         const struct usb_device_id *id)
371 {
372         struct sd *sd = (struct sd *) gspca_dev;
373         struct cam *cam;
374
375         cam = &gspca_dev->cam;
376
377         cam->cam_mode = vga_mode;       /* only 640x480 */
378         cam->nmodes = ARRAY_SIZE(vga_mode);
379
380         sd->flags = id->driver_info;
381         return 0;
382 }
383
384 static void setbrightcont(struct gspca_dev *gspca_dev)
385 {
386         struct sd *sd = (struct sd *) gspca_dev;
387         int i, v;
388         static const u8 max[10] =
389                 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
390                  0xd4, 0xec};
391         static const u8 delta[10] =
392                 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
393                  0x11, 0x0b};
394
395         reg_w(gspca_dev, 0xff, 0x00);           /* page 0 */
396         for (i = 0; i < 10; i++) {
397                 v = max[i];
398                 v += (sd->brightness->val - sd->brightness->maximum)
399                         * 150 / sd->brightness->maximum; /* 200 ? */
400                 v -= delta[i] * sd->contrast->val / sd->contrast->maximum;
401                 if (v < 0)
402                         v = 0;
403                 else if (v > 0xff)
404                         v = 0xff;
405                 reg_w(gspca_dev, 0xa2 + i, v);
406         }
407         reg_w(gspca_dev, 0xdc, 0x01);
408 }
409
410 static void setcolors(struct gspca_dev *gspca_dev)
411 {
412         struct sd *sd = (struct sd *) gspca_dev;
413         int i, v;
414         static const int a[9] =
415                 {217, -212, 0, -101, 170, -67, -38, -315, 355};
416         static const int b[9] =
417                 {19, 106, 0, 19, 106, 1, 19, 106, 1};
418
419         reg_w(gspca_dev, 0xff, 0x03);                   /* page 3 */
420         reg_w(gspca_dev, 0x11, 0x01);
421         reg_w(gspca_dev, 0xff, 0x00);                   /* page 0 */
422         for (i = 0; i < 9; i++) {
423                 v = a[i] * sd->saturation->val / sd->saturation->maximum;
424                 v += b[i];
425                 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
426                 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
427         }
428         reg_w(gspca_dev, 0xdc, 0x01);
429 }
430
431 static void setwhitebalance(struct gspca_dev *gspca_dev)
432 {
433         struct sd *sd = (struct sd *) gspca_dev;
434
435         reg_w(gspca_dev, 0xff, 0x00);           /* page 0 */
436         reg_w(gspca_dev, 0xc6, sd->white_balance->val);
437
438         reg_w(gspca_dev, 0xdc, 0x01);
439 }
440
441 static void setredbalance(struct gspca_dev *gspca_dev)
442 {
443         struct sd *sd = (struct sd *) gspca_dev;
444
445         reg_w(gspca_dev, 0xff, 0x00);           /* page 0 */
446         reg_w(gspca_dev, 0xc5, sd->red_balance->val);
447
448         reg_w(gspca_dev, 0xdc, 0x01);
449 }
450
451 static void setbluebalance(struct gspca_dev *gspca_dev)
452 {
453         struct sd *sd = (struct sd *) gspca_dev;
454
455         reg_w(gspca_dev, 0xff, 0x00);                   /* page 0 */
456         reg_w(gspca_dev, 0xc7, sd->blue_balance->val);
457
458         reg_w(gspca_dev, 0xdc, 0x01);
459 }
460
461 static void setgain(struct gspca_dev *gspca_dev)
462 {
463         u8 reg10, reg12;
464
465         if (gspca_dev->gain->val < 32) {
466                 reg10 = gspca_dev->gain->val;
467                 reg12 = 0;
468         } else {
469                 reg10 = 31;
470                 reg12 = gspca_dev->gain->val - 31;
471         }
472
473         reg_w(gspca_dev, 0xff, 0x03);                   /* page 3 */
474         reg_w(gspca_dev, 0x10, reg10);
475         reg_w(gspca_dev, 0x12, reg12);
476
477         /* load registers to sensor (Bit 0, auto clear) */
478         reg_w(gspca_dev, 0x11, 0x01);
479 }
480
481 static void setexposure(struct gspca_dev *gspca_dev)
482 {
483         u8 clockdiv;
484         u16 exposure;
485
486         /*
487          * Register 2 of frame 3 contains the clock divider configuring the
488          * no fps according to the formula: 90 / reg. sd->exposure is the
489          * desired exposure time in 0.5 ms.
490          */
491         clockdiv = (90 * gspca_dev->exposure->val + 1999) / 2000;
492
493         /*
494          * Note clockdiv = 3 also works, but when running at 30 fps, depending
495          * on the scene being recorded, the camera switches to another
496          * quantization table for certain JPEG blocks, and we don't know how
497          * to decompress these blocks. So we cap the framerate at 15 fps.
498          */
499         if (clockdiv < 6)
500                 clockdiv = 6;
501         else if (clockdiv > 63)
502                 clockdiv = 63;
503
504         /*
505          * Register 2 MUST be a multiple of 3, except when between 6 and 12?
506          * Always round up, otherwise we cannot get the desired frametime
507          * using the partial frame time exposure control.
508          */
509         if (clockdiv < 6 || clockdiv > 12)
510                 clockdiv = ((clockdiv + 2) / 3) * 3;
511
512         /*
513          * frame exposure time in ms = 1000 * clockdiv / 90    ->
514          * exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90)
515          */
516         exposure = (gspca_dev->exposure->val * 45 * 448) / (1000 * clockdiv);
517         /* 0 = use full frametime, 448 = no exposure, reverse it */
518         exposure = 448 - exposure;
519
520         reg_w(gspca_dev, 0xff, 0x03);                   /* page 3 */
521         reg_w(gspca_dev, 0x02, clockdiv);
522         reg_w(gspca_dev, 0x0e, exposure & 0xff);
523         reg_w(gspca_dev, 0x0f, exposure >> 8);
524
525         /* load registers to sensor (Bit 0, auto clear) */
526         reg_w(gspca_dev, 0x11, 0x01);
527 }
528
529 static void sethvflip(struct gspca_dev *gspca_dev)
530 {
531         struct sd *sd = (struct sd *) gspca_dev;
532         u8 data, hflip, vflip;
533
534         hflip = sd->hflip->val;
535         if (sd->flags & FL_HFLIP)
536                 hflip = !hflip;
537         vflip = sd->vflip->val;
538         if (sd->flags & FL_VFLIP)
539                 vflip = !vflip;
540
541         reg_w(gspca_dev, 0xff, 0x03);                   /* page 3 */
542         data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
543         reg_w(gspca_dev, 0x21, data);
544
545         /* load registers to sensor (Bit 0, auto clear) */
546         reg_w(gspca_dev, 0x11, 0x01);
547 }
548
549 static void setsharpness(struct gspca_dev *gspca_dev)
550 {
551         struct sd *sd = (struct sd *) gspca_dev;
552
553         reg_w(gspca_dev, 0xff, 0x00);           /* page 0 */
554         reg_w(gspca_dev, 0xb6, sd->sharpness->val);
555
556         reg_w(gspca_dev, 0xdc, 0x01);
557 }
558
559 /* this function is called at probe and resume time for pac7302 */
560 static int sd_init(struct gspca_dev *gspca_dev)
561 {
562         reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
563         return gspca_dev->usb_err;
564 }
565
566 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
567 {
568         struct gspca_dev *gspca_dev =
569                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
570         struct sd *sd = (struct sd *)gspca_dev;
571
572         gspca_dev->usb_err = 0;
573
574         if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
575                 /* when switching to autogain set defaults to make sure
576                    we are on a valid point of the autogain gain /
577                    exposure knee graph, and give this change time to
578                    take effect before doing autogain. */
579                 gspca_dev->exposure->val    = PAC7302_EXPOSURE_DEFAULT;
580                 gspca_dev->gain->val        = PAC7302_GAIN_DEFAULT;
581                 sd->autogain_ignore_frames  = PAC_AUTOGAIN_IGNORE_FRAMES;
582         }
583
584         if (!gspca_dev->streaming)
585                 return 0;
586
587         switch (ctrl->id) {
588         case V4L2_CID_BRIGHTNESS:
589                 setbrightcont(gspca_dev);
590                 break;
591         case V4L2_CID_SATURATION:
592                 setcolors(gspca_dev);
593                 break;
594         case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
595                 setwhitebalance(gspca_dev);
596                 break;
597         case V4L2_CID_RED_BALANCE:
598                 setredbalance(gspca_dev);
599                 break;
600         case V4L2_CID_BLUE_BALANCE:
601                 setbluebalance(gspca_dev);
602                 break;
603         case V4L2_CID_AUTOGAIN:
604                 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
605                         setexposure(gspca_dev);
606                 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
607                         setgain(gspca_dev);
608                 break;
609         case V4L2_CID_HFLIP:
610                 sethvflip(gspca_dev);
611                 break;
612         case V4L2_CID_SHARPNESS:
613                 setsharpness(gspca_dev);
614                 break;
615         default:
616                 return -EINVAL;
617         }
618         return gspca_dev->usb_err;
619 }
620
621 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
622         .s_ctrl = sd_s_ctrl,
623 };
624
625 /* this function is called at probe time */
626 static int sd_init_controls(struct gspca_dev *gspca_dev)
627 {
628         struct sd *sd = (struct sd *) gspca_dev;
629         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
630
631         gspca_dev->vdev.ctrl_handler = hdl;
632         v4l2_ctrl_handler_init(hdl, 12);
633
634         sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
635                                         V4L2_CID_BRIGHTNESS, 0, 32, 1, 16);
636         sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
637                                         V4L2_CID_CONTRAST, 0, 255, 1, 127);
638
639         sd->saturation = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
640                                         V4L2_CID_SATURATION, 0, 255, 1, 127);
641         sd->white_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
642                                         V4L2_CID_WHITE_BALANCE_TEMPERATURE,
643                                         0, 255, 1, 55);
644         sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
645                                         V4L2_CID_RED_BALANCE, 0, 3, 1, 1);
646         sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
647                                         V4L2_CID_BLUE_BALANCE, 0, 3, 1, 1);
648
649         gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
650                                         V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
651         gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
652                                         V4L2_CID_EXPOSURE, 0, 1023, 1,
653                                         PAC7302_EXPOSURE_DEFAULT);
654         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
655                                         V4L2_CID_GAIN, 0, 62, 1,
656                                         PAC7302_GAIN_DEFAULT);
657
658         sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
659                 V4L2_CID_HFLIP, 0, 1, 1, 0);
660         sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
661                 V4L2_CID_VFLIP, 0, 1, 1, 0);
662
663         sd->sharpness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
664                                         V4L2_CID_SHARPNESS, 0, 15, 1, 8);
665
666         if (hdl->error) {
667                 pr_err("Could not initialize controls\n");
668                 return hdl->error;
669         }
670
671         v4l2_ctrl_cluster(2, &sd->brightness);
672         v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
673         v4l2_ctrl_cluster(2, &sd->hflip);
674         return 0;
675 }
676
677 /* -- start the camera -- */
678 static int sd_start(struct gspca_dev *gspca_dev)
679 {
680         struct sd *sd = (struct sd *) gspca_dev;
681
682         reg_w_var(gspca_dev, start_7302,
683                 page3_7302, sizeof(page3_7302));
684
685         sd->sof_read = 0;
686         sd->autogain_ignore_frames = 0;
687         atomic_set(&sd->avg_lum, 270 + sd->brightness->val);
688
689         /* start stream */
690         reg_w(gspca_dev, 0xff, 0x01);
691         reg_w(gspca_dev, 0x78, 0x01);
692
693         return gspca_dev->usb_err;
694 }
695
696 static void sd_stopN(struct gspca_dev *gspca_dev)
697 {
698
699         /* stop stream */
700         reg_w(gspca_dev, 0xff, 0x01);
701         reg_w(gspca_dev, 0x78, 0x00);
702 }
703
704 /* called on streamoff with alt 0 and on disconnect for pac7302 */
705 static void sd_stop0(struct gspca_dev *gspca_dev)
706 {
707         if (!gspca_dev->present)
708                 return;
709         reg_w(gspca_dev, 0xff, 0x01);
710         reg_w(gspca_dev, 0x78, 0x40);
711 }
712
713 static void do_autogain(struct gspca_dev *gspca_dev)
714 {
715         struct sd *sd = (struct sd *) gspca_dev;
716         int avg_lum = atomic_read(&sd->avg_lum);
717         int desired_lum;
718         const int deadzone = 30;
719
720         if (sd->autogain_ignore_frames < 0)
721                 return;
722
723         if (sd->autogain_ignore_frames > 0) {
724                 sd->autogain_ignore_frames--;
725         } else {
726                 desired_lum = 270 + sd->brightness->val;
727
728                 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_lum,
729                                         deadzone, PAC7302_GAIN_KNEE,
730                                         PAC7302_EXPOSURE_KNEE))
731                         sd->autogain_ignore_frames =
732                                                 PAC_AUTOGAIN_IGNORE_FRAMES;
733         }
734 }
735
736 /* JPEG header */
737 static const u8 jpeg_header[] = {
738         0xff, 0xd8,     /* SOI: Start of Image */
739
740         0xff, 0xc0,     /* SOF0: Start of Frame (Baseline DCT) */
741         0x00, 0x11,     /* length = 17 bytes (including this length field) */
742         0x08,           /* Precision: 8 */
743         0x02, 0x80,     /* height = 640 (image rotated) */
744         0x01, 0xe0,     /* width = 480 */
745         0x03,           /* Number of image components: 3 */
746         0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
747         0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
748         0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
749
750         0xff, 0xda,     /* SOS: Start Of Scan */
751         0x00, 0x0c,     /* length = 12 bytes (including this length field) */
752         0x03,           /* number of components: 3 */
753         0x01, 0x00,     /* selector 1, table 0x00 */
754         0x02, 0x11,     /* selector 2, table 0x11 */
755         0x03, 0x11,     /* selector 3, table 0x11 */
756         0x00, 0x3f,     /* Spectral selection: 0 .. 63 */
757         0x00            /* Successive approximation: 0 */
758 };
759
760 /* this function is run at interrupt level */
761 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
762                         u8 *data,                       /* isoc packet */
763                         int len)                        /* iso packet length */
764 {
765         struct sd *sd = (struct sd *) gspca_dev;
766         u8 *image;
767         u8 *sof;
768
769         sof = pac_find_sof(&sd->sof_read, data, len);
770         if (sof) {
771                 int n, lum_offset, footer_length;
772
773                 /*
774                  * 6 bytes after the FF D9 EOF marker a number of lumination
775                  * bytes are send corresponding to different parts of the
776                  * image, the 14th and 15th byte after the EOF seem to
777                  * correspond to the center of the image.
778                  */
779                 lum_offset = 61 + sizeof pac_sof_marker;
780                 footer_length = 74;
781
782                 /* Finish decoding current frame */
783                 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
784                 if (n < 0) {
785                         gspca_dev->image_len += n;
786                         n = 0;
787                 } else {
788                         gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
789                 }
790
791                 image = gspca_dev->image;
792                 if (image != NULL
793                  && image[gspca_dev->image_len - 2] == 0xff
794                  && image[gspca_dev->image_len - 1] == 0xd9)
795                         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
796
797                 n = sof - data;
798                 len -= n;
799                 data = sof;
800
801                 /* Get average lumination */
802                 if (gspca_dev->last_packet_type == LAST_PACKET &&
803                                 n >= lum_offset)
804                         atomic_set(&sd->avg_lum, data[-lum_offset] +
805                                                 data[-lum_offset + 1]);
806
807                 /* Start the new frame with the jpeg header */
808                 /* The PAC7302 has the image rotated 90 degrees */
809                 gspca_frame_add(gspca_dev, FIRST_PACKET,
810                                 jpeg_header, sizeof jpeg_header);
811         }
812         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
813 }
814
815 #ifdef CONFIG_VIDEO_ADV_DEBUG
816 static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
817                         struct v4l2_dbg_register *reg)
818 {
819         u8 index;
820         u8 value;
821
822         /*
823          * reg->reg: bit0..15: reserved for register index (wIndex is 16bit
824          *                     long on the USB bus)
825          */
826         if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
827             reg->match.addr == 0 &&
828             (reg->reg < 0x000000ff) &&
829             (reg->val <= 0x000000ff)
830         ) {
831                 /* Currently writing to page 0 is only supported. */
832                 /* reg_w() only supports 8bit index */
833                 index = reg->reg;
834                 value = reg->val;
835
836                 /*
837                  * Note that there shall be no access to other page
838                  * by any other function between the page switch and
839                  * the actual register write.
840                  */
841                 reg_w(gspca_dev, 0xff, 0x00);           /* page 0 */
842                 reg_w(gspca_dev, index, value);
843
844                 reg_w(gspca_dev, 0xdc, 0x01);
845         }
846         return gspca_dev->usb_err;
847 }
848
849 static int sd_chip_ident(struct gspca_dev *gspca_dev,
850                         struct v4l2_dbg_chip_ident *chip)
851 {
852         int ret = -EINVAL;
853
854         if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
855             chip->match.addr == 0) {
856                 chip->revision = 0;
857                 chip->ident = V4L2_IDENT_UNKNOWN;
858                 ret = 0;
859         }
860         return ret;
861 }
862 #endif
863
864 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
865 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
866                         u8 *data,               /* interrupt packet data */
867                         int len)                /* interrput packet length */
868 {
869         int ret = -EINVAL;
870         u8 data0, data1;
871
872         if (len == 2) {
873                 data0 = data[0];
874                 data1 = data[1];
875                 if ((data0 == 0x00 && data1 == 0x11) ||
876                     (data0 == 0x22 && data1 == 0x33) ||
877                     (data0 == 0x44 && data1 == 0x55) ||
878                     (data0 == 0x66 && data1 == 0x77) ||
879                     (data0 == 0x88 && data1 == 0x99) ||
880                     (data0 == 0xaa && data1 == 0xbb) ||
881                     (data0 == 0xcc && data1 == 0xdd) ||
882                     (data0 == 0xee && data1 == 0xff)) {
883                         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
884                         input_sync(gspca_dev->input_dev);
885                         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
886                         input_sync(gspca_dev->input_dev);
887                         ret = 0;
888                 }
889         }
890
891         return ret;
892 }
893 #endif
894
895 /* sub-driver description for pac7302 */
896 static const struct sd_desc sd_desc = {
897         .name = KBUILD_MODNAME,
898         .config = sd_config,
899         .init = sd_init,
900         .init_controls = sd_init_controls,
901         .start = sd_start,
902         .stopN = sd_stopN,
903         .stop0 = sd_stop0,
904         .pkt_scan = sd_pkt_scan,
905         .dq_callback = do_autogain,
906 #ifdef CONFIG_VIDEO_ADV_DEBUG
907         .set_register = sd_dbg_s_register,
908         .get_chip_ident = sd_chip_ident,
909 #endif
910 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
911         .int_pkt_scan = sd_int_pkt_scan,
912 #endif
913 };
914
915 /* -- module initialisation -- */
916 static const struct usb_device_id device_table[] = {
917         {USB_DEVICE(0x06f8, 0x3009)},
918         {USB_DEVICE(0x06f8, 0x301b)},
919         {USB_DEVICE(0x093a, 0x2620)},
920         {USB_DEVICE(0x093a, 0x2621)},
921         {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
922         {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
923         {USB_DEVICE(0x093a, 0x2625)},
924         {USB_DEVICE(0x093a, 0x2626)},
925         {USB_DEVICE(0x093a, 0x2627), .driver_info = FL_VFLIP},
926         {USB_DEVICE(0x093a, 0x2628)},
927         {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
928         {USB_DEVICE(0x093a, 0x262a)},
929         {USB_DEVICE(0x093a, 0x262c)},
930         {USB_DEVICE(0x145f, 0x013c)},
931         {USB_DEVICE(0x1ae7, 0x2001)}, /* SpeedLink Snappy Mic SL-6825-SBK */
932         {}
933 };
934 MODULE_DEVICE_TABLE(usb, device_table);
935
936 /* -- device connect -- */
937 static int sd_probe(struct usb_interface *intf,
938                         const struct usb_device_id *id)
939 {
940         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
941                                 THIS_MODULE);
942 }
943
944 static struct usb_driver sd_driver = {
945         .name = KBUILD_MODNAME,
946         .id_table = device_table,
947         .probe = sd_probe,
948         .disconnect = gspca_disconnect,
949 #ifdef CONFIG_PM
950         .suspend = gspca_suspend,
951         .resume = gspca_resume,
952         .reset_resume = gspca_resume,
953 #endif
954 };
955
956 module_usb_driver(sd_driver);