V4L/DVB: gspca - ov534: Allow enumerating supported framerates
[deliverable/linux.git] / drivers / media / video / gspca / ov534.c
1 /*
2 * ov534-ov772x gspca driver
3 *
4 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
5 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
6 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
7 *
8 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
9 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
10 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
11 *
12 * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
13 * PS3 Eye camera, brightness, contrast, hue, AWB control added
14 * by Max Thrun <bear24rw@gmail.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31 #define MODULE_NAME "ov534"
32
33 #include "gspca.h"
34
35 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
36 #define OV534_REG_SUBADDR 0xf2
37 #define OV534_REG_WRITE 0xf3
38 #define OV534_REG_READ 0xf4
39 #define OV534_REG_OPERATION 0xf5
40 #define OV534_REG_STATUS 0xf6
41
42 #define OV534_OP_WRITE_3 0x37
43 #define OV534_OP_WRITE_2 0x33
44 #define OV534_OP_READ_2 0xf9
45
46 #define CTRL_TIMEOUT 500
47
48 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
49 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
50 MODULE_LICENSE("GPL");
51
52 /* specific webcam descriptor */
53 struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
55 __u32 last_pts;
56 u16 last_fid;
57 u8 frame_rate;
58
59 u8 brightness;
60 u8 contrast;
61 u8 gain;
62 u8 exposure;
63 u8 redblc;
64 u8 blueblc;
65 u8 hue;
66 u8 autogain;
67 u8 awb;
68 s8 sharpness;
69 u8 hflip;
70 u8 vflip;
71
72 };
73
74 /* V4L2 controls supported by the driver */
75 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
87 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
88 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
89 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
93 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
94 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
95 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
96 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
97 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
98 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
99
100 static const struct ctrl sd_ctrls[] = {
101 { /* 0 */
102 {
103 .id = V4L2_CID_BRIGHTNESS,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Brightness",
106 .minimum = 0,
107 .maximum = 255,
108 .step = 1,
109 #define BRIGHTNESS_DEF 20
110 .default_value = BRIGHTNESS_DEF,
111 },
112 .set = sd_setbrightness,
113 .get = sd_getbrightness,
114 },
115 { /* 1 */
116 {
117 .id = V4L2_CID_CONTRAST,
118 .type = V4L2_CTRL_TYPE_INTEGER,
119 .name = "Contrast",
120 .minimum = 0,
121 .maximum = 255,
122 .step = 1,
123 #define CONTRAST_DEF 37
124 .default_value = CONTRAST_DEF,
125 },
126 .set = sd_setcontrast,
127 .get = sd_getcontrast,
128 },
129 { /* 2 */
130 {
131 .id = V4L2_CID_GAIN,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Main Gain",
134 .minimum = 0,
135 .maximum = 63,
136 .step = 1,
137 #define GAIN_DEF 20
138 .default_value = GAIN_DEF,
139 },
140 .set = sd_setgain,
141 .get = sd_getgain,
142 },
143 { /* 3 */
144 {
145 .id = V4L2_CID_EXPOSURE,
146 .type = V4L2_CTRL_TYPE_INTEGER,
147 .name = "Exposure",
148 .minimum = 0,
149 .maximum = 255,
150 .step = 1,
151 #define EXPO_DEF 120
152 .default_value = EXPO_DEF,
153 },
154 .set = sd_setexposure,
155 .get = sd_getexposure,
156 },
157 { /* 4 */
158 {
159 .id = V4L2_CID_RED_BALANCE,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "Red Balance",
162 .minimum = 0,
163 .maximum = 255,
164 .step = 1,
165 #define RED_BALANCE_DEF 128
166 .default_value = RED_BALANCE_DEF,
167 },
168 .set = sd_setredblc,
169 .get = sd_getredblc,
170 },
171 { /* 5 */
172 {
173 .id = V4L2_CID_BLUE_BALANCE,
174 .type = V4L2_CTRL_TYPE_INTEGER,
175 .name = "Blue Balance",
176 .minimum = 0,
177 .maximum = 255,
178 .step = 1,
179 #define BLUE_BALANCE_DEF 128
180 .default_value = BLUE_BALANCE_DEF,
181 },
182 .set = sd_setblueblc,
183 .get = sd_getblueblc,
184 },
185 { /* 6 */
186 {
187 .id = V4L2_CID_HUE,
188 .type = V4L2_CTRL_TYPE_INTEGER,
189 .name = "Hue",
190 .minimum = 0,
191 .maximum = 255,
192 .step = 1,
193 #define HUE_DEF 143
194 .default_value = HUE_DEF,
195 },
196 .set = sd_sethue,
197 .get = sd_gethue,
198 },
199 { /* 7 */
200 {
201 .id = V4L2_CID_AUTOGAIN,
202 .type = V4L2_CTRL_TYPE_BOOLEAN,
203 .name = "Autogain",
204 .minimum = 0,
205 .maximum = 1,
206 .step = 1,
207 #define AUTOGAIN_DEF 0
208 .default_value = AUTOGAIN_DEF,
209 },
210 .set = sd_setautogain,
211 .get = sd_getautogain,
212 },
213 #define AWB_IDX 8
214 { /* 8 */
215 {
216 .id = V4L2_CID_AUTO_WHITE_BALANCE,
217 .type = V4L2_CTRL_TYPE_BOOLEAN,
218 .name = "Auto White Balance",
219 .minimum = 0,
220 .maximum = 1,
221 .step = 1,
222 #define AWB_DEF 0
223 .default_value = AWB_DEF,
224 },
225 .set = sd_setawb,
226 .get = sd_getawb,
227 },
228 { /* 9 */
229 {
230 .id = V4L2_CID_SHARPNESS,
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 .name = "Sharpness",
233 .minimum = 0,
234 .maximum = 63,
235 .step = 1,
236 #define SHARPNESS_DEF 0
237 .default_value = SHARPNESS_DEF,
238 },
239 .set = sd_setsharpness,
240 .get = sd_getsharpness,
241 },
242 { /* 10 */
243 {
244 .id = V4L2_CID_HFLIP,
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
246 .name = "HFlip",
247 .minimum = 0,
248 .maximum = 1,
249 .step = 1,
250 #define HFLIP_DEF 0
251 .default_value = HFLIP_DEF,
252 },
253 .set = sd_sethflip,
254 .get = sd_gethflip,
255 },
256 { /* 11 */
257 {
258 .id = V4L2_CID_VFLIP,
259 .type = V4L2_CTRL_TYPE_BOOLEAN,
260 .name = "VFlip",
261 .minimum = 0,
262 .maximum = 1,
263 .step = 1,
264 #define VFLIP_DEF 0
265 .default_value = VFLIP_DEF,
266 },
267 .set = sd_setvflip,
268 .get = sd_getvflip,
269 },
270 };
271
272 static const struct v4l2_pix_format ov772x_mode[] = {
273 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
274 .bytesperline = 320 * 2,
275 .sizeimage = 320 * 240 * 2,
276 .colorspace = V4L2_COLORSPACE_SRGB,
277 .priv = 1},
278 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
279 .bytesperline = 640 * 2,
280 .sizeimage = 640 * 480 * 2,
281 .colorspace = V4L2_COLORSPACE_SRGB,
282 .priv = 0},
283 };
284
285 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
286 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
287
288 static const struct framerates ov772x_framerates[] = {
289 { /* 320x240 */
290 .rates = qvga_rates,
291 .nrates = ARRAY_SIZE(qvga_rates),
292 },
293 { /* 640x480 */
294 .rates = vga_rates,
295 .nrates = ARRAY_SIZE(vga_rates),
296 },
297 };
298
299 static const u8 bridge_init[][2] = {
300 { 0xc2, 0x0c },
301 { 0x88, 0xf8 },
302 { 0xc3, 0x69 },
303 { 0x89, 0xff },
304 { 0x76, 0x03 },
305 { 0x92, 0x01 },
306 { 0x93, 0x18 },
307 { 0x94, 0x10 },
308 { 0x95, 0x10 },
309 { 0xe2, 0x00 },
310 { 0xe7, 0x3e },
311
312 { 0x96, 0x00 },
313
314 { 0x97, 0x20 },
315 { 0x97, 0x20 },
316 { 0x97, 0x20 },
317 { 0x97, 0x0a },
318 { 0x97, 0x3f },
319 { 0x97, 0x4a },
320 { 0x97, 0x20 },
321 { 0x97, 0x15 },
322 { 0x97, 0x0b },
323
324 { 0x8e, 0x40 },
325 { 0x1f, 0x81 },
326 { 0x34, 0x05 },
327 { 0xe3, 0x04 },
328 { 0x88, 0x00 },
329 { 0x89, 0x00 },
330 { 0x76, 0x00 },
331 { 0xe7, 0x2e },
332 { 0x31, 0xf9 },
333 { 0x25, 0x42 },
334 { 0x21, 0xf0 },
335
336 { 0x1c, 0x00 },
337 { 0x1d, 0x40 },
338 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
339 { 0x1d, 0x00 }, /* payload size */
340
341 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
342 { 0x1d, 0x58 }, /* frame size */
343 { 0x1d, 0x00 }, /* frame size */
344
345 { 0x1c, 0x0a },
346 { 0x1d, 0x08 }, /* turn on UVC header */
347 { 0x1d, 0x0e }, /* .. */
348
349 { 0x8d, 0x1c },
350 { 0x8e, 0x80 },
351 { 0xe5, 0x04 },
352
353 { 0xc0, 0x50 },
354 { 0xc1, 0x3c },
355 { 0xc2, 0x0c },
356 };
357 static const u8 sensor_init[][2] = {
358 { 0x12, 0x80 },
359 { 0x11, 0x01 },
360 /*fixme: better have a delay?*/
361 { 0x11, 0x01 },
362 { 0x11, 0x01 },
363 { 0x11, 0x01 },
364 { 0x11, 0x01 },
365 { 0x11, 0x01 },
366 { 0x11, 0x01 },
367 { 0x11, 0x01 },
368 { 0x11, 0x01 },
369 { 0x11, 0x01 },
370 { 0x11, 0x01 },
371
372 { 0x3d, 0x03 },
373 { 0x17, 0x26 },
374 { 0x18, 0xa0 },
375 { 0x19, 0x07 },
376 { 0x1a, 0xf0 },
377 { 0x32, 0x00 },
378 { 0x29, 0xa0 },
379 { 0x2c, 0xf0 },
380 { 0x65, 0x20 },
381 { 0x11, 0x01 },
382 { 0x42, 0x7f },
383 { 0x63, 0xaa }, /* AWB - was e0 */
384 { 0x64, 0xff },
385 { 0x66, 0x00 },
386 { 0x13, 0xf0 }, /* com8 */
387 { 0x0d, 0x41 },
388 { 0x0f, 0xc5 },
389 { 0x14, 0x11 },
390
391 { 0x22, 0x7f },
392 { 0x23, 0x03 },
393 { 0x24, 0x40 },
394 { 0x25, 0x30 },
395 { 0x26, 0xa1 },
396 { 0x2a, 0x00 },
397 { 0x2b, 0x00 },
398 { 0x6b, 0xaa },
399 { 0x13, 0xff }, /* AWB */
400
401 { 0x90, 0x05 },
402 { 0x91, 0x01 },
403 { 0x92, 0x03 },
404 { 0x93, 0x00 },
405 { 0x94, 0x60 },
406 { 0x95, 0x3c },
407 { 0x96, 0x24 },
408 { 0x97, 0x1e },
409 { 0x98, 0x62 },
410 { 0x99, 0x80 },
411 { 0x9a, 0x1e },
412 { 0x9b, 0x08 },
413 { 0x9c, 0x20 },
414 { 0x9e, 0x81 },
415
416 { 0xa6, 0x04 },
417 { 0x7e, 0x0c },
418 { 0x7f, 0x16 },
419 { 0x80, 0x2a },
420 { 0x81, 0x4e },
421 { 0x82, 0x61 },
422 { 0x83, 0x6f },
423 { 0x84, 0x7b },
424 { 0x85, 0x86 },
425 { 0x86, 0x8e },
426 { 0x87, 0x97 },
427 { 0x88, 0xa4 },
428 { 0x89, 0xaf },
429 { 0x8a, 0xc5 },
430 { 0x8b, 0xd7 },
431 { 0x8c, 0xe8 },
432 { 0x8d, 0x20 },
433
434 { 0x0c, 0x90 },
435
436 { 0x2b, 0x00 },
437 { 0x22, 0x7f },
438 { 0x23, 0x03 },
439 { 0x11, 0x01 },
440 { 0x0c, 0xd0 },
441 { 0x64, 0xff },
442 { 0x0d, 0x41 },
443
444 { 0x14, 0x41 },
445 { 0x0e, 0xcd },
446 { 0xac, 0xbf },
447 { 0x8e, 0x00 }, /* De-noise threshold */
448 { 0x0c, 0xd0 }
449 };
450 static const u8 bridge_start_vga[][2] = {
451 {0x1c, 0x00},
452 {0x1d, 0x40},
453 {0x1d, 0x02},
454 {0x1d, 0x00},
455 {0x1d, 0x02},
456 {0x1d, 0x58},
457 {0x1d, 0x00},
458 {0xc0, 0x50},
459 {0xc1, 0x3c},
460 };
461 static const u8 sensor_start_vga[][2] = {
462 {0x12, 0x00},
463 {0x17, 0x26},
464 {0x18, 0xa0},
465 {0x19, 0x07},
466 {0x1a, 0xf0},
467 {0x29, 0xa0},
468 {0x2c, 0xf0},
469 {0x65, 0x20},
470 };
471 static const u8 bridge_start_qvga[][2] = {
472 {0x1c, 0x00},
473 {0x1d, 0x40},
474 {0x1d, 0x02},
475 {0x1d, 0x00},
476 {0x1d, 0x01},
477 {0x1d, 0x4b},
478 {0x1d, 0x00},
479 {0xc0, 0x28},
480 {0xc1, 0x1e},
481 };
482 static const u8 sensor_start_qvga[][2] = {
483 {0x12, 0x40},
484 {0x17, 0x3f},
485 {0x18, 0x50},
486 {0x19, 0x03},
487 {0x1a, 0x78},
488 {0x29, 0x50},
489 {0x2c, 0x78},
490 {0x65, 0x2f},
491 };
492
493 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
494 {
495 struct usb_device *udev = gspca_dev->dev;
496 int ret;
497
498 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
499 gspca_dev->usb_buf[0] = val;
500 ret = usb_control_msg(udev,
501 usb_sndctrlpipe(udev, 0),
502 0x01,
503 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
504 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
505 if (ret < 0)
506 PDEBUG(D_ERR, "write failed");
507 }
508
509 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
510 {
511 struct usb_device *udev = gspca_dev->dev;
512 int ret;
513
514 ret = usb_control_msg(udev,
515 usb_rcvctrlpipe(udev, 0),
516 0x01,
517 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
518 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
519 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
520 if (ret < 0)
521 PDEBUG(D_ERR, "read failed");
522 return gspca_dev->usb_buf[0];
523 }
524
525 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
526 * (direction and output)? */
527 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
528 {
529 u8 data;
530
531 PDEBUG(D_CONF, "led status: %d", status);
532
533 data = ov534_reg_read(gspca_dev, 0x21);
534 data |= 0x80;
535 ov534_reg_write(gspca_dev, 0x21, data);
536
537 data = ov534_reg_read(gspca_dev, 0x23);
538 if (status)
539 data |= 0x80;
540 else
541 data &= ~0x80;
542
543 ov534_reg_write(gspca_dev, 0x23, data);
544
545 if (!status) {
546 data = ov534_reg_read(gspca_dev, 0x21);
547 data &= ~0x80;
548 ov534_reg_write(gspca_dev, 0x21, data);
549 }
550 }
551
552 static int sccb_check_status(struct gspca_dev *gspca_dev)
553 {
554 u8 data;
555 int i;
556
557 for (i = 0; i < 5; i++) {
558 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
559
560 switch (data) {
561 case 0x00:
562 return 1;
563 case 0x04:
564 return 0;
565 case 0x03:
566 break;
567 default:
568 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
569 data, i + 1);
570 }
571 }
572 return 0;
573 }
574
575 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
576 {
577 PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
578 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
579 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
580 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
581
582 if (!sccb_check_status(gspca_dev))
583 PDEBUG(D_ERR, "sccb_reg_write failed");
584 }
585
586 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
587 {
588 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
589 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
590 if (!sccb_check_status(gspca_dev))
591 PDEBUG(D_ERR, "sccb_reg_read failed 1");
592
593 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
594 if (!sccb_check_status(gspca_dev))
595 PDEBUG(D_ERR, "sccb_reg_read failed 2");
596
597 return ov534_reg_read(gspca_dev, OV534_REG_READ);
598 }
599
600 /* output a bridge sequence (reg - val) */
601 static void reg_w_array(struct gspca_dev *gspca_dev,
602 const u8 (*data)[2], int len)
603 {
604 while (--len >= 0) {
605 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
606 data++;
607 }
608 }
609
610 /* output a sensor sequence (reg - val) */
611 static void sccb_w_array(struct gspca_dev *gspca_dev,
612 const u8 (*data)[2], int len)
613 {
614 while (--len >= 0) {
615 if ((*data)[0] != 0xff) {
616 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
617 } else {
618 sccb_reg_read(gspca_dev, (*data)[1]);
619 sccb_reg_write(gspca_dev, 0xff, 0x00);
620 }
621 data++;
622 }
623 }
624
625 /* ov772x specific controls */
626 static void set_frame_rate(struct gspca_dev *gspca_dev)
627 {
628 struct sd *sd = (struct sd *) gspca_dev;
629 int i;
630 struct rate_s {
631 u8 fps;
632 u8 r11;
633 u8 r0d;
634 u8 re5;
635 };
636 const struct rate_s *r;
637 static const struct rate_s rate_0[] = { /* 640x480 */
638 {60, 0x01, 0xc1, 0x04},
639 {50, 0x01, 0x41, 0x02},
640 {40, 0x02, 0xc1, 0x04},
641 {30, 0x04, 0x81, 0x02},
642 {15, 0x03, 0x41, 0x04},
643 };
644 static const struct rate_s rate_1[] = { /* 320x240 */
645 {125, 0x02, 0x81, 0x02},
646 {100, 0x02, 0xc1, 0x04},
647 {75, 0x03, 0xc1, 0x04},
648 {60, 0x04, 0xc1, 0x04},
649 {50, 0x02, 0x41, 0x04},
650 {40, 0x03, 0x41, 0x04},
651 {30, 0x04, 0x41, 0x04},
652 };
653
654 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
655 r = rate_0;
656 i = ARRAY_SIZE(rate_0);
657 } else {
658 r = rate_1;
659 i = ARRAY_SIZE(rate_1);
660 }
661 while (--i > 0) {
662 if (sd->frame_rate >= r->fps)
663 break;
664 r++;
665 }
666
667 sccb_reg_write(gspca_dev, 0x11, r->r11);
668 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
669 ov534_reg_write(gspca_dev, 0xe5, r->re5);
670
671 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
672 }
673
674 static void setbrightness(struct gspca_dev *gspca_dev)
675 {
676 struct sd *sd = (struct sd *) gspca_dev;
677
678 sccb_reg_write(gspca_dev, 0x9B, sd->brightness);
679 }
680
681 static void setcontrast(struct gspca_dev *gspca_dev)
682 {
683 struct sd *sd = (struct sd *) gspca_dev;
684
685 sccb_reg_write(gspca_dev, 0x9C, sd->contrast);
686 }
687
688 static void setgain(struct gspca_dev *gspca_dev)
689 {
690 struct sd *sd = (struct sd *) gspca_dev;
691 u8 val;
692
693 val = sd->gain;
694 switch (val & 0x30) {
695 case 0x00:
696 val &= 0x0f;
697 break;
698 case 0x10:
699 val &= 0x0f;
700 val |= 0x30;
701 break;
702 case 0x20:
703 val &= 0x0f;
704 val |= 0x70;
705 break;
706 default:
707 /* case 0x30: */
708 val &= 0x0f;
709 val |= 0xf0;
710 break;
711 }
712 sccb_reg_write(gspca_dev, 0x00, val);
713 }
714
715 static void setexposure(struct gspca_dev *gspca_dev)
716 {
717 struct sd *sd = (struct sd *) gspca_dev;
718 u8 val;
719
720 val = sd->exposure;
721 sccb_reg_write(gspca_dev, 0x08, val >> 7);
722 sccb_reg_write(gspca_dev, 0x10, val << 1);
723 }
724
725 static void setredblc(struct gspca_dev *gspca_dev)
726 {
727 struct sd *sd = (struct sd *) gspca_dev;
728
729 sccb_reg_write(gspca_dev, 0x43, sd->redblc);
730 }
731
732 static void setblueblc(struct gspca_dev *gspca_dev)
733 {
734 struct sd *sd = (struct sd *) gspca_dev;
735
736 sccb_reg_write(gspca_dev, 0x42, sd->blueblc);
737 }
738
739 static void sethue(struct gspca_dev *gspca_dev)
740 {
741 struct sd *sd = (struct sd *) gspca_dev;
742
743 sccb_reg_write(gspca_dev, 0x01, sd->hue);
744 }
745
746 static void setautogain(struct gspca_dev *gspca_dev)
747 {
748 struct sd *sd = (struct sd *) gspca_dev;
749
750 if (sd->autogain) {
751 sccb_reg_write(gspca_dev, 0x13, 0xf7); /* AGC,AEC,AWB ON */
752 sccb_reg_write(gspca_dev, 0x64,
753 sccb_reg_read(gspca_dev, 0x64) | 0x03);
754 } else {
755 sccb_reg_write(gspca_dev, 0x13, 0xf0); /* AGC,AEC,AWB OFF */
756 sccb_reg_write(gspca_dev, 0x64,
757 sccb_reg_read(gspca_dev, 0x64) & 0xfc);
758 }
759 }
760
761 static void setawb(struct gspca_dev *gspca_dev)
762 {
763 struct sd *sd = (struct sd *) gspca_dev;
764
765 if (sd->awb)
766 sccb_reg_write(gspca_dev, 0x63, 0xe0); /* AWB on */
767 else
768 sccb_reg_write(gspca_dev, 0x63, 0xaa); /* AWB off */
769 }
770
771 static void setsharpness(struct gspca_dev *gspca_dev)
772 {
773 struct sd *sd = (struct sd *) gspca_dev;
774 u8 val;
775
776 val = sd->sharpness;
777 sccb_reg_write(gspca_dev, 0x91, val); /* vga noise */
778 sccb_reg_write(gspca_dev, 0x8e, val); /* qvga noise */
779 }
780
781 static void sethflip(struct gspca_dev *gspca_dev)
782 {
783 struct sd *sd = (struct sd *) gspca_dev;
784
785 if (sd->hflip == 0)
786 sccb_reg_write(gspca_dev, 0x0c,
787 sccb_reg_read(gspca_dev, 0x0c) | 0x40);
788 else
789 sccb_reg_write(gspca_dev, 0x0c,
790 sccb_reg_read(gspca_dev, 0x0c) & 0xbf);
791 }
792
793 static void setvflip(struct gspca_dev *gspca_dev)
794 {
795 struct sd *sd = (struct sd *) gspca_dev;
796
797 if (sd->vflip == 0)
798 sccb_reg_write(gspca_dev, 0x0c,
799 sccb_reg_read(gspca_dev, 0x0c) | 0x80);
800 else
801 sccb_reg_write(gspca_dev, 0x0c,
802 sccb_reg_read(gspca_dev, 0x0c) & 0x7f);
803 }
804
805 /* this function is called at probe time */
806 static int sd_config(struct gspca_dev *gspca_dev,
807 const struct usb_device_id *id)
808 {
809 struct sd *sd = (struct sd *) gspca_dev;
810 struct cam *cam;
811
812 cam = &gspca_dev->cam;
813
814 cam->cam_mode = ov772x_mode;
815 cam->nmodes = ARRAY_SIZE(ov772x_mode);
816 cam->mode_framerates = ov772x_framerates;
817
818 cam->bulk = 1;
819 cam->bulk_size = 16384;
820 cam->bulk_nurbs = 2;
821
822 sd->frame_rate = 30;
823
824 sd->brightness = BRIGHTNESS_DEF;
825 sd->contrast = CONTRAST_DEF;
826 sd->gain = GAIN_DEF;
827 sd->exposure = EXPO_DEF;
828 sd->redblc = RED_BALANCE_DEF;
829 sd->blueblc = BLUE_BALANCE_DEF;
830 sd->hue = HUE_DEF;
831 #if AUTOGAIN_DEF != 0
832 sd->autogain = AUTOGAIN_DEF;
833 #else
834 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
835 #endif
836 #if AWB_DEF != 0
837 sd->awb = AWB_DEF
838 #endif
839 #if SHARPNESS_DEF != 0
840 sd->sharpness = SHARPNESS_DEF;
841 #endif
842 #if HFLIP_DEF != 0
843 sd->hflip = HFLIP_DEF;
844 #endif
845 #if VFLIP_DEF != 0
846 sd->vflip = VFLIP_DEF;
847 #endif
848
849 return 0;
850 }
851
852 /* this function is called at probe and resume time */
853 static int sd_init(struct gspca_dev *gspca_dev)
854 {
855 u16 sensor_id;
856
857 /* reset bridge */
858 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
859 ov534_reg_write(gspca_dev, 0xe0, 0x08);
860 msleep(100);
861
862 /* initialize the sensor address */
863 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
864
865 /* reset sensor */
866 sccb_reg_write(gspca_dev, 0x12, 0x80);
867 msleep(10);
868
869 /* probe the sensor */
870 sccb_reg_read(gspca_dev, 0x0a);
871 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
872 sccb_reg_read(gspca_dev, 0x0b);
873 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
874 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
875
876 /* initialize */
877 reg_w_array(gspca_dev, bridge_init,
878 ARRAY_SIZE(bridge_init));
879 ov534_set_led(gspca_dev, 1);
880 sccb_w_array(gspca_dev, sensor_init,
881 ARRAY_SIZE(sensor_init));
882 ov534_reg_write(gspca_dev, 0xe0, 0x09);
883 ov534_set_led(gspca_dev, 0);
884 set_frame_rate(gspca_dev);
885
886 return 0;
887 }
888
889 static int sd_start(struct gspca_dev *gspca_dev)
890 {
891 int mode;
892
893 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
894 if (mode != 0) { /* 320x240 */
895 reg_w_array(gspca_dev, bridge_start_qvga,
896 ARRAY_SIZE(bridge_start_qvga));
897 sccb_w_array(gspca_dev, sensor_start_qvga,
898 ARRAY_SIZE(sensor_start_qvga));
899 } else { /* 640x480 */
900 reg_w_array(gspca_dev, bridge_start_vga,
901 ARRAY_SIZE(bridge_start_vga));
902 sccb_w_array(gspca_dev, sensor_start_vga,
903 ARRAY_SIZE(sensor_start_vga));
904 }
905 set_frame_rate(gspca_dev);
906
907 setautogain(gspca_dev);
908 setawb(gspca_dev);
909 setgain(gspca_dev);
910 setredblc(gspca_dev);
911 setblueblc(gspca_dev);
912 sethue(gspca_dev);
913 setexposure(gspca_dev);
914 setbrightness(gspca_dev);
915 setcontrast(gspca_dev);
916 setsharpness(gspca_dev);
917 setvflip(gspca_dev);
918 sethflip(gspca_dev);
919
920 ov534_set_led(gspca_dev, 1);
921 ov534_reg_write(gspca_dev, 0xe0, 0x00);
922 return 0;
923 }
924
925 static void sd_stopN(struct gspca_dev *gspca_dev)
926 {
927 ov534_reg_write(gspca_dev, 0xe0, 0x09);
928 ov534_set_led(gspca_dev, 0);
929 }
930
931 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
932 #define UVC_STREAM_EOH (1 << 7)
933 #define UVC_STREAM_ERR (1 << 6)
934 #define UVC_STREAM_STI (1 << 5)
935 #define UVC_STREAM_RES (1 << 4)
936 #define UVC_STREAM_SCR (1 << 3)
937 #define UVC_STREAM_PTS (1 << 2)
938 #define UVC_STREAM_EOF (1 << 1)
939 #define UVC_STREAM_FID (1 << 0)
940
941 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
942 u8 *data, int len)
943 {
944 struct sd *sd = (struct sd *) gspca_dev;
945 __u32 this_pts;
946 u16 this_fid;
947 int remaining_len = len;
948
949 do {
950 len = min(remaining_len, 2048);
951
952 /* Payloads are prefixed with a UVC-style header. We
953 consider a frame to start when the FID toggles, or the PTS
954 changes. A frame ends when EOF is set, and we've received
955 the correct number of bytes. */
956
957 /* Verify UVC header. Header length is always 12 */
958 if (data[0] != 12 || len < 12) {
959 PDEBUG(D_PACK, "bad header");
960 goto discard;
961 }
962
963 /* Check errors */
964 if (data[1] & UVC_STREAM_ERR) {
965 PDEBUG(D_PACK, "payload error");
966 goto discard;
967 }
968
969 /* Extract PTS and FID */
970 if (!(data[1] & UVC_STREAM_PTS)) {
971 PDEBUG(D_PACK, "PTS not present");
972 goto discard;
973 }
974 this_pts = (data[5] << 24) | (data[4] << 16)
975 | (data[3] << 8) | data[2];
976 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
977
978 /* If PTS or FID has changed, start a new frame. */
979 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
980 if (gspca_dev->last_packet_type == INTER_PACKET)
981 gspca_frame_add(gspca_dev, LAST_PACKET,
982 NULL, 0);
983 sd->last_pts = this_pts;
984 sd->last_fid = this_fid;
985 gspca_frame_add(gspca_dev, FIRST_PACKET,
986 data + 12, len - 12);
987 /* If this packet is marked as EOF, end the frame */
988 } else if (data[1] & UVC_STREAM_EOF) {
989 struct gspca_frame *frame;
990
991 sd->last_pts = 0;
992 frame = gspca_get_i_frame(gspca_dev);
993 if (frame == NULL)
994 goto discard;
995 if (frame->data_end - frame->data !=
996 gspca_dev->width * gspca_dev->height * 2) {
997 PDEBUG(D_PACK, "short frame");
998 goto discard;
999 }
1000 gspca_frame_add(gspca_dev, LAST_PACKET,
1001 data + 12, len - 12);
1002 } else {
1003
1004 /* Add the data from this payload */
1005 gspca_frame_add(gspca_dev, INTER_PACKET,
1006 data + 12, len - 12);
1007 }
1008
1009 /* Done this payload */
1010 goto scan_next;
1011
1012 discard:
1013 /* Discard data until a new frame starts. */
1014 gspca_dev->last_packet_type = DISCARD_PACKET;
1015
1016 scan_next:
1017 remaining_len -= len;
1018 data += len;
1019 } while (remaining_len > 0);
1020 }
1021
1022 /* controls */
1023 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1024 {
1025 struct sd *sd = (struct sd *) gspca_dev;
1026
1027 sd->gain = val;
1028 if (gspca_dev->streaming)
1029 setgain(gspca_dev);
1030 return 0;
1031 }
1032
1033 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1034 {
1035 struct sd *sd = (struct sd *) gspca_dev;
1036
1037 *val = sd->gain;
1038 return 0;
1039 }
1040
1041 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1042 {
1043 struct sd *sd = (struct sd *) gspca_dev;
1044
1045 sd->exposure = val;
1046 if (gspca_dev->streaming)
1047 setexposure(gspca_dev);
1048 return 0;
1049 }
1050
1051 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1052 {
1053 struct sd *sd = (struct sd *) gspca_dev;
1054
1055 *val = sd->exposure;
1056 return 0;
1057 }
1058
1059 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1060 {
1061 struct sd *sd = (struct sd *) gspca_dev;
1062
1063 sd->brightness = val;
1064 if (gspca_dev->streaming)
1065 setbrightness(gspca_dev);
1066 return 0;
1067 }
1068
1069 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1070 {
1071 struct sd *sd = (struct sd *) gspca_dev;
1072
1073 *val = sd->brightness;
1074 return 0;
1075 }
1076
1077 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1078 {
1079 struct sd *sd = (struct sd *) gspca_dev;
1080
1081 sd->contrast = val;
1082 if (gspca_dev->streaming)
1083 setcontrast(gspca_dev);
1084 return 0;
1085 }
1086
1087 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1088 {
1089 struct sd *sd = (struct sd *) gspca_dev;
1090
1091 *val = sd->contrast;
1092 return 0;
1093 }
1094
1095 static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val)
1096 {
1097 struct sd *sd = (struct sd *) gspca_dev;
1098
1099 sd->redblc = val;
1100 if (gspca_dev->streaming)
1101 setredblc(gspca_dev);
1102 return 0;
1103 }
1104
1105 static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val)
1106 {
1107 struct sd *sd = (struct sd *) gspca_dev;
1108
1109 *val = sd->redblc;
1110 return 0;
1111 }
1112
1113 static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val)
1114 {
1115 struct sd *sd = (struct sd *) gspca_dev;
1116
1117 sd->blueblc = val;
1118 if (gspca_dev->streaming)
1119 setblueblc(gspca_dev);
1120 return 0;
1121 }
1122
1123 static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val)
1124 {
1125 struct sd *sd = (struct sd *) gspca_dev;
1126
1127 *val = sd->blueblc;
1128 return 0;
1129 }
1130
1131 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
1132 {
1133 struct sd *sd = (struct sd *) gspca_dev;
1134
1135 sd->hue = val;
1136 if (gspca_dev->streaming)
1137 sethue(gspca_dev);
1138 return 0;
1139 }
1140
1141 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
1142 {
1143 struct sd *sd = (struct sd *) gspca_dev;
1144
1145 *val = sd->hue;
1146 return 0;
1147 }
1148
1149 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1150 {
1151 struct sd *sd = (struct sd *) gspca_dev;
1152
1153 sd->autogain = val;
1154
1155 if (gspca_dev->streaming) {
1156
1157 /* the auto white balance control works only
1158 * when auto gain is set */
1159 if (val)
1160 gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
1161 else
1162 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
1163 setautogain(gspca_dev);
1164 }
1165 return 0;
1166 }
1167
1168 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1169 {
1170 struct sd *sd = (struct sd *) gspca_dev;
1171
1172 *val = sd->autogain;
1173 return 0;
1174 }
1175
1176 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
1177 {
1178 struct sd *sd = (struct sd *) gspca_dev;
1179
1180 sd->awb = val;
1181 if (gspca_dev->streaming)
1182 setawb(gspca_dev);
1183 return 0;
1184 }
1185
1186 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
1187 {
1188 struct sd *sd = (struct sd *) gspca_dev;
1189
1190 *val = sd->awb;
1191 return 0;
1192 }
1193
1194 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
1195 {
1196 struct sd *sd = (struct sd *) gspca_dev;
1197
1198 sd->sharpness = val;
1199 if (gspca_dev->streaming)
1200 setsharpness(gspca_dev);
1201 return 0;
1202 }
1203
1204 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
1205 {
1206 struct sd *sd = (struct sd *) gspca_dev;
1207
1208 *val = sd->sharpness;
1209 return 0;
1210 }
1211
1212 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1213 {
1214 struct sd *sd = (struct sd *) gspca_dev;
1215
1216 sd->hflip = val;
1217 if (gspca_dev->streaming)
1218 sethflip(gspca_dev);
1219 return 0;
1220 }
1221
1222 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1223 {
1224 struct sd *sd = (struct sd *) gspca_dev;
1225
1226 *val = sd->hflip;
1227 return 0;
1228 }
1229
1230 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1231 {
1232 struct sd *sd = (struct sd *) gspca_dev;
1233
1234 sd->vflip = val;
1235 if (gspca_dev->streaming)
1236 setvflip(gspca_dev);
1237 return 0;
1238 }
1239
1240 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1241 {
1242 struct sd *sd = (struct sd *) gspca_dev;
1243
1244 *val = sd->vflip;
1245 return 0;
1246 }
1247
1248 /* get stream parameters (framerate) */
1249 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
1250 struct v4l2_streamparm *parm)
1251 {
1252 struct v4l2_captureparm *cp = &parm->parm.capture;
1253 struct v4l2_fract *tpf = &cp->timeperframe;
1254 struct sd *sd = (struct sd *) gspca_dev;
1255
1256 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1257 return -EINVAL;
1258
1259 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1260 tpf->numerator = 1;
1261 tpf->denominator = sd->frame_rate;
1262
1263 return 0;
1264 }
1265
1266 /* set stream parameters (framerate) */
1267 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
1268 struct v4l2_streamparm *parm)
1269 {
1270 struct v4l2_captureparm *cp = &parm->parm.capture;
1271 struct v4l2_fract *tpf = &cp->timeperframe;
1272 struct sd *sd = (struct sd *) gspca_dev;
1273
1274 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1275 return -EINVAL;
1276
1277 /* Set requested framerate */
1278 sd->frame_rate = tpf->denominator / tpf->numerator;
1279 if (gspca_dev->streaming)
1280 set_frame_rate(gspca_dev);
1281
1282 /* Return the actual framerate */
1283 tpf->numerator = 1;
1284 tpf->denominator = sd->frame_rate;
1285
1286 return 0;
1287 }
1288
1289 /* sub-driver description */
1290 static const struct sd_desc sd_desc = {
1291 .name = MODULE_NAME,
1292 .ctrls = sd_ctrls,
1293 .nctrls = ARRAY_SIZE(sd_ctrls),
1294 .config = sd_config,
1295 .init = sd_init,
1296 .start = sd_start,
1297 .stopN = sd_stopN,
1298 .pkt_scan = sd_pkt_scan,
1299 .get_streamparm = sd_get_streamparm,
1300 .set_streamparm = sd_set_streamparm,
1301 };
1302
1303 /* -- module initialisation -- */
1304 static const __devinitdata struct usb_device_id device_table[] = {
1305 {USB_DEVICE(0x1415, 0x2000)},
1306 {}
1307 };
1308
1309 MODULE_DEVICE_TABLE(usb, device_table);
1310
1311 /* -- device connect -- */
1312 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1313 {
1314 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1315 THIS_MODULE);
1316 }
1317
1318 static struct usb_driver sd_driver = {
1319 .name = MODULE_NAME,
1320 .id_table = device_table,
1321 .probe = sd_probe,
1322 .disconnect = gspca_disconnect,
1323 #ifdef CONFIG_PM
1324 .suspend = gspca_suspend,
1325 .resume = gspca_resume,
1326 #endif
1327 };
1328
1329 /* -- module insert / remove -- */
1330 static int __init sd_mod_init(void)
1331 {
1332 int ret;
1333
1334 ret = usb_register(&sd_driver);
1335 if (ret < 0)
1336 return ret;
1337 PDEBUG(D_PROBE, "registered");
1338 return 0;
1339 }
1340
1341 static void __exit sd_mod_exit(void)
1342 {
1343 usb_deregister(&sd_driver);
1344 PDEBUG(D_PROBE, "deregistered");
1345 }
1346
1347 module_init(sd_mod_init);
1348 module_exit(sd_mod_exit);
This page took 0.101389 seconds and 5 git commands to generate.