V4L/DVB (9994): gspca: t613: Bad loop in om6802 reset.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / gspca / ov534.c
CommitLineData
fbb4c6d2
AO
1/*
2 * ov534/ov772x gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
0f7a50b2 4 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
fbb4c6d2
AO
5 *
6 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
7 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
8 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#define MODULE_NAME "ov534"
26
27#include "gspca.h"
28
29#define OV534_REG_ADDRESS 0xf1 /* ? */
30#define OV534_REG_SUBADDR 0xf2
31#define OV534_REG_WRITE 0xf3
32#define OV534_REG_READ 0xf4
33#define OV534_REG_OPERATION 0xf5
34#define OV534_REG_STATUS 0xf6
35
36#define OV534_OP_WRITE_3 0x37
37#define OV534_OP_WRITE_2 0x33
38#define OV534_OP_READ_2 0xf9
39
40#define CTRL_TIMEOUT 500
41
42MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
43MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
44MODULE_LICENSE("GPL");
45
fbb4c6d2
AO
46/* specific webcam descriptor */
47struct sd {
48 struct gspca_dev gspca_dev; /* !! must be the first item */
8c252050
JFM
49 __u32 last_fid;
50 __u32 last_pts;
11d9f25d 51 int frame_rate;
fbb4c6d2
AO
52};
53
54/* V4L2 controls supported by the driver */
55static struct ctrl sd_ctrls[] = {
56};
57
58static struct v4l2_pix_format vga_mode[] = {
59 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
60 .bytesperline = 640 * 2,
61 .sizeimage = 640 * 480 * 2,
62 .colorspace = V4L2_COLORSPACE_JPEG,
63 .priv = 0},
64};
65
9e1e7b06 66static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
fbb4c6d2 67{
9e1e7b06 68 u8 data = val;
fbb4c6d2
AO
69 int ret;
70
9e1e7b06 71 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
fbb4c6d2
AO
72 ret = usb_control_msg(udev,
73 usb_sndctrlpipe(udev, 0),
74 0x1,
75 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
76 0x0, reg, &data, 1, CTRL_TIMEOUT);
77 if (ret < 0)
78 PDEBUG(D_ERR, "write failed");
79}
80
9e1e7b06 81static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
fbb4c6d2 82{
9e1e7b06 83 u8 data;
fbb4c6d2
AO
84 int ret;
85
86 ret = usb_control_msg(udev,
87 usb_rcvctrlpipe(udev, 0),
88 0x1,
89 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 0x0, reg, &data, 1, CTRL_TIMEOUT);
9e1e7b06 91 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
fbb4c6d2
AO
92 if (ret < 0)
93 PDEBUG(D_ERR, "read failed");
94 return data;
95}
96
fbb4c6d2
AO
97/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
98 * (direction and output)? */
99static void ov534_set_led(struct usb_device *udev, int status)
100{
9e1e7b06 101 u8 data;
fbb4c6d2
AO
102
103 PDEBUG(D_CONF, "led status: %d", status);
104
105 data = ov534_reg_read(udev, 0x21);
106 data |= 0x80;
107 ov534_reg_write(udev, 0x21, data);
108
109 data = ov534_reg_read(udev, 0x23);
110 if (status)
111 data |= 0x80;
112 else
113 data &= ~(0x80);
114
115 ov534_reg_write(udev, 0x23, data);
116}
117
118static int sccb_check_status(struct usb_device *udev)
119{
9e1e7b06 120 u8 data;
fbb4c6d2
AO
121 int i;
122
123 for (i = 0; i < 5; i++) {
124 data = ov534_reg_read(udev, OV534_REG_STATUS);
125
9e1e7b06 126 switch (data) {
fbb4c6d2
AO
127 case 0x00:
128 return 1;
129 case 0x04:
130 return 0;
131 case 0x03:
132 break;
133 default:
134 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
135 data, i + 1);
136 }
137 }
138 return 0;
139}
140
9e1e7b06 141static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
fbb4c6d2 142{
9e1e7b06 143 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
fbb4c6d2
AO
144 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
145 ov534_reg_write(udev, OV534_REG_WRITE, val);
146 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
147
148 if (!sccb_check_status(udev))
149 PDEBUG(D_ERR, "sccb_reg_write failed");
150}
151
1dc87b6e 152#ifdef GSPCA_DEBUG
e6e48378
AO
153static u8 sccb_reg_read(struct usb_device *udev, u16 reg)
154{
155 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
156 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
157 if (!sccb_check_status(udev))
158 PDEBUG(D_ERR, "sccb_reg_read failed 1");
159
160 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_READ_2);
161 if (!sccb_check_status(udev))
162 PDEBUG(D_ERR, "sccb_reg_read failed 2");
163
164 return ov534_reg_read(udev, OV534_REG_READ);
165}
1dc87b6e 166#endif
e6e48378 167
47dfd21f
JP
168static const __u8 ov534_reg_initdata[][2] = {
169 { 0xe7, 0x3a },
170
171 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
172
173 { 0xc2, 0x0c },
174 { 0x88, 0xf8 },
175 { 0xc3, 0x69 },
176 { 0x89, 0xff },
177 { 0x76, 0x03 },
178 { 0x92, 0x01 },
179 { 0x93, 0x18 },
180 { 0x94, 0x10 },
181 { 0x95, 0x10 },
182 { 0xe2, 0x00 },
183 { 0xe7, 0x3e },
184
47dfd21f
JP
185 { 0x96, 0x00 },
186
187 { 0x97, 0x20 },
188 { 0x97, 0x20 },
189 { 0x97, 0x20 },
190 { 0x97, 0x0a },
191 { 0x97, 0x3f },
192 { 0x97, 0x4a },
193 { 0x97, 0x20 },
194 { 0x97, 0x15 },
195 { 0x97, 0x0b },
196
197 { 0x8e, 0x40 },
198 { 0x1f, 0x81 },
199 { 0x34, 0x05 },
200 { 0xe3, 0x04 },
201 { 0x88, 0x00 },
202 { 0x89, 0x00 },
203 { 0x76, 0x00 },
204 { 0xe7, 0x2e },
205 { 0x31, 0xf9 },
206 { 0x25, 0x42 },
207 { 0x21, 0xf0 },
208
209 { 0x1c, 0x00 },
210 { 0x1d, 0x40 },
0f7a50b2
JP
211 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
212 { 0x1d, 0x00 }, /* payload size */
5ea9c4de
JP
213 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
214 { 0x1d, 0x58 }, /* frame size */
215 { 0x1d, 0x00 }, /* frame size */
47dfd21f 216
c06eb619
JP
217 { 0x1c, 0x0a },
218 { 0x1d, 0x08 }, /* turn on UVC header */
219 { 0x1d, 0x0e }, /* .. */
220
47dfd21f
JP
221 { 0x8d, 0x1c },
222 { 0x8e, 0x80 },
223 { 0xe5, 0x04 },
224
225 { 0xc0, 0x50 },
226 { 0xc1, 0x3c },
227 { 0xc2, 0x0c },
228};
fbb4c6d2 229
47dfd21f
JP
230static const __u8 ov772x_reg_initdata[][2] = {
231 { 0x12, 0x80 },
232 { 0x11, 0x01 },
233
234 { 0x3d, 0x03 },
235 { 0x17, 0x26 },
236 { 0x18, 0xa0 },
237 { 0x19, 0x07 },
238 { 0x1a, 0xf0 },
239 { 0x32, 0x00 },
240 { 0x29, 0xa0 },
241 { 0x2c, 0xf0 },
242 { 0x65, 0x20 },
243 { 0x11, 0x01 },
244 { 0x42, 0x7f },
245 { 0x63, 0xe0 },
246 { 0x64, 0xff },
247 { 0x66, 0x00 },
248 { 0x13, 0xf0 },
249 { 0x0d, 0x41 },
250 { 0x0f, 0xc5 },
251 { 0x14, 0x11 },
252
253 { 0x22, 0x7f },
254 { 0x23, 0x03 },
255 { 0x24, 0x40 },
256 { 0x25, 0x30 },
257 { 0x26, 0xa1 },
258 { 0x2a, 0x00 },
259 { 0x2b, 0x00 },
260 { 0x6b, 0xaa },
261 { 0x13, 0xff },
262
263 { 0x90, 0x05 },
264 { 0x91, 0x01 },
265 { 0x92, 0x03 },
266 { 0x93, 0x00 },
267 { 0x94, 0x60 },
268 { 0x95, 0x3c },
269 { 0x96, 0x24 },
270 { 0x97, 0x1e },
271 { 0x98, 0x62 },
272 { 0x99, 0x80 },
273 { 0x9a, 0x1e },
274 { 0x9b, 0x08 },
275 { 0x9c, 0x20 },
276 { 0x9e, 0x81 },
277
278 { 0xa6, 0x04 },
279 { 0x7e, 0x0c },
280 { 0x7f, 0x16 },
281 { 0x80, 0x2a },
282 { 0x81, 0x4e },
283 { 0x82, 0x61 },
284 { 0x83, 0x6f },
285 { 0x84, 0x7b },
286 { 0x85, 0x86 },
287 { 0x86, 0x8e },
288 { 0x87, 0x97 },
289 { 0x88, 0xa4 },
290 { 0x89, 0xaf },
291 { 0x8a, 0xc5 },
292 { 0x8b, 0xd7 },
293 { 0x8c, 0xe8 },
294 { 0x8d, 0x20 },
295
296 { 0x0c, 0x90 },
297
298 { 0x2b, 0x00 },
299 { 0x22, 0x7f },
300 { 0x23, 0x03 },
301 { 0x11, 0x01 },
302 { 0x0c, 0xd0 },
303 { 0x64, 0xff },
304 { 0x0d, 0x41 },
305
306 { 0x14, 0x41 },
307 { 0x0e, 0xcd },
308 { 0xac, 0xbf },
309 { 0x8e, 0x00 },
310 { 0x0c, 0xd0 }
311};
fbb4c6d2 312
11d9f25d
JP
313/* set framerate */
314static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
315{
316 struct sd *sd = (struct sd *) gspca_dev;
317 int fr = sd->frame_rate;
318
319 switch (fr) {
320 case 50:
321 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
322 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
323 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
324 break;
325 case 40:
326 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
327 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
328 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
329 break;
330/* case 30: */
331 default:
332 fr = 30;
333 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
334 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
335 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
336 break;
337 case 15:
338 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
339 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
340 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
341 break;
342 }
343
344 sd->frame_rate = fr;
345 PDEBUG(D_PROBE, "frame_rate: %d", fr);
346}
fbb4c6d2 347
47dfd21f
JP
348/* setup method */
349static void ov534_setup(struct usb_device *udev)
350{
351 int i;
fbb4c6d2 352
47dfd21f
JP
353 /* Initialize bridge chip */
354 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
355 ov534_reg_write(udev, ov534_reg_initdata[i][0],
356 ov534_reg_initdata[i][1]);
fbb4c6d2 357
e6e48378
AO
358 PDEBUG(D_PROBE, "sensor is ov%02x%02x",
359 sccb_reg_read(udev, 0x0a), sccb_reg_read(udev, 0x0b));
360
fbb4c6d2
AO
361 ov534_set_led(udev, 1);
362
47dfd21f
JP
363 /* Initialize sensor */
364 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
365 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
366 ov772x_reg_initdata[i][1]);
fbb4c6d2
AO
367
368 ov534_reg_write(udev, 0xe0, 0x09);
369 ov534_set_led(udev, 0);
370}
371
372/* this function is called at probe time */
373static int sd_config(struct gspca_dev *gspca_dev,
374 const struct usb_device_id *id)
375{
376 struct cam *cam;
377
378 cam = &gspca_dev->cam;
379
380 cam->epaddr = 0x01;
381 cam->cam_mode = vga_mode;
382 cam->nmodes = ARRAY_SIZE(vga_mode);
383
0f7a50b2 384 cam->bulk_size = 16384;
fbb4c6d2
AO
385 cam->bulk_nurbs = 2;
386
fbb4c6d2
AO
387 return 0;
388}
389
390/* this function is called at probe and resume time */
391static int sd_init(struct gspca_dev *gspca_dev)
392{
3adba442 393 ov534_setup(gspca_dev->dev);
11d9f25d 394 ov534_set_frame_rate(gspca_dev);
fbb4c6d2
AO
395
396 return 0;
397}
398
399static int sd_start(struct gspca_dev *gspca_dev)
400{
fbb4c6d2
AO
401 /* start streaming data */
402 ov534_set_led(gspca_dev->dev, 1);
403 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
404
405 return 0;
406}
407
408static void sd_stopN(struct gspca_dev *gspca_dev)
409{
410 /* stop streaming data */
411 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
412 ov534_set_led(gspca_dev->dev, 0);
413}
414
fb139224
JP
415/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
416#define UVC_STREAM_EOH (1 << 7)
417#define UVC_STREAM_ERR (1 << 6)
418#define UVC_STREAM_STI (1 << 5)
419#define UVC_STREAM_RES (1 << 4)
420#define UVC_STREAM_SCR (1 << 3)
421#define UVC_STREAM_PTS (1 << 2)
422#define UVC_STREAM_EOF (1 << 1)
423#define UVC_STREAM_FID (1 << 0)
424
fbb4c6d2
AO
425static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
426 __u8 *data, int len)
427{
8c252050 428 struct sd *sd = (struct sd *) gspca_dev;
fb139224 429 __u32 this_pts;
fb139224 430 int this_fid;
0f7a50b2
JP
431 int remaining_len = len;
432 __u8 *next_data = data;
433
434scan_next:
435 if (remaining_len <= 0)
436 return;
437
438 data = next_data;
439 len = min(remaining_len, 2048);
440 remaining_len -= len;
441 next_data += len;
fb139224 442
15e3209a 443 /* Payloads are prefixed with a UVC-style header. We
fb139224
JP
444 consider a frame to start when the FID toggles, or the PTS
445 changes. A frame ends when EOF is set, and we've received
446 the correct number of bytes. */
447
448 /* Verify UVC header. Header length is always 12 */
449 if (data[0] != 12 || len < 12) {
450 PDEBUG(D_PACK, "bad header");
451 goto discard;
452 }
453
454 /* Check errors */
455 if (data[1] & UVC_STREAM_ERR) {
456 PDEBUG(D_PACK, "payload error");
457 goto discard;
458 }
459
460 /* Extract PTS and FID */
461 if (!(data[1] & UVC_STREAM_PTS)) {
462 PDEBUG(D_PACK, "PTS not present");
463 goto discard;
464 }
465 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
466 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
467
468 /* If PTS or FID has changed, start a new frame. */
8c252050 469 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
fb139224 470 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
8c252050
JFM
471 sd->last_pts = this_pts;
472 sd->last_fid = this_fid;
fb139224
JP
473 }
474
475 /* Add the data from this payload */
476 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
477 data + 12, len - 12);
478
479 /* If this packet is marked as EOF, end the frame */
480 if (data[1] & UVC_STREAM_EOF) {
8c252050 481 sd->last_pts = 0;
fb139224
JP
482
483 if ((frame->data_end - frame->data) !=
484 (gspca_dev->width * gspca_dev->height * 2)) {
485 PDEBUG(D_PACK, "short frame");
486 goto discard;
487 }
488
489 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
490 }
491
0f7a50b2
JP
492 /* Done this payload */
493 goto scan_next;
fb139224
JP
494
495discard:
496 /* Discard data until a new frame starts. */
497 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
0f7a50b2 498 goto scan_next;
fbb4c6d2
AO
499}
500
11d9f25d
JP
501/* get stream parameters (framerate) */
502int sd_get_streamparm(struct gspca_dev *gspca_dev,
503 struct v4l2_streamparm *parm)
504{
505 struct v4l2_captureparm *cp = &parm->parm.capture;
506 struct v4l2_fract *tpf = &cp->timeperframe;
507 struct sd *sd = (struct sd *) gspca_dev;
508
509 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
510 return -EINVAL;
511
512 cp->capability |= V4L2_CAP_TIMEPERFRAME;
513 tpf->numerator = 1;
514 tpf->denominator = sd->frame_rate;
515
516 return 0;
517}
518
519/* set stream parameters (framerate) */
520int sd_set_streamparm(struct gspca_dev *gspca_dev,
521 struct v4l2_streamparm *parm)
522{
523 struct v4l2_captureparm *cp = &parm->parm.capture;
524 struct v4l2_fract *tpf = &cp->timeperframe;
525 struct sd *sd = (struct sd *) gspca_dev;
526
527 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
528 return -EINVAL;
529
530 /* Set requested framerate */
531 sd->frame_rate = tpf->denominator / tpf->numerator;
532 ov534_set_frame_rate(gspca_dev);
533
534 /* Return the actual framerate */
535 tpf->numerator = 1;
536 tpf->denominator = sd->frame_rate;
537
538 return 0;
539}
540
fbb4c6d2
AO
541/* sub-driver description */
542static const struct sd_desc sd_desc = {
543 .name = MODULE_NAME,
544 .ctrls = sd_ctrls,
545 .nctrls = ARRAY_SIZE(sd_ctrls),
546 .config = sd_config,
547 .init = sd_init,
548 .start = sd_start,
549 .stopN = sd_stopN,
550 .pkt_scan = sd_pkt_scan,
11d9f25d
JP
551 .get_streamparm = sd_get_streamparm,
552 .set_streamparm = sd_set_streamparm,
fbb4c6d2
AO
553};
554
555/* -- module initialisation -- */
556static const __devinitdata struct usb_device_id device_table[] = {
557 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
558 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
559 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
560 {}
561};
562
563MODULE_DEVICE_TABLE(usb, device_table);
564
565/* -- device connect -- */
566static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
567{
568 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
569 THIS_MODULE);
570}
571
572static struct usb_driver sd_driver = {
573 .name = MODULE_NAME,
574 .id_table = device_table,
575 .probe = sd_probe,
576 .disconnect = gspca_disconnect,
577#ifdef CONFIG_PM
578 .suspend = gspca_suspend,
579 .resume = gspca_resume,
580#endif
581};
582
583/* -- module insert / remove -- */
584static int __init sd_mod_init(void)
585{
586 if (usb_register(&sd_driver) < 0)
587 return -1;
588 PDEBUG(D_PROBE, "registered");
589 return 0;
590}
591
592static void __exit sd_mod_exit(void)
593{
594 usb_deregister(&sd_driver);
595 PDEBUG(D_PROBE, "deregistered");
596}
597
598module_init(sd_mod_init);
599module_exit(sd_mod_exit);