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