blob: e02c8f70f8140d78bacbb2928191af8eba9c4216 [file] [log] [blame]
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001/*
2 * ov534/ov772x gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4 *
5 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
6 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
7 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#define MODULE_NAME "ov534"
25
26#include "gspca.h"
27
28#define OV534_REG_ADDRESS 0xf1 /* ? */
29#define OV534_REG_SUBADDR 0xf2
30#define OV534_REG_WRITE 0xf3
31#define OV534_REG_READ 0xf4
32#define OV534_REG_OPERATION 0xf5
33#define OV534_REG_STATUS 0xf6
34
35#define OV534_OP_WRITE_3 0x37
36#define OV534_OP_WRITE_2 0x33
37#define OV534_OP_READ_2 0xf9
38
39#define CTRL_TIMEOUT 500
40
41MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
42MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
43MODULE_LICENSE("GPL");
44
45/* global parameters */
46static int frame_rate;
47
48/* specific webcam descriptor */
49struct sd {
50 struct gspca_dev gspca_dev; /* !! must be the first item */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030051};
52
53/* V4L2 controls supported by the driver */
54static struct ctrl sd_ctrls[] = {
55};
56
57static struct v4l2_pix_format vga_mode[] = {
58 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
59 .bytesperline = 640 * 2,
60 .sizeimage = 640 * 480 * 2,
61 .colorspace = V4L2_COLORSPACE_JPEG,
62 .priv = 0},
63};
64
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030065static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030066{
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030067 u8 data = val;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030068 int ret;
69
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030070 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030071 ret = usb_control_msg(udev,
72 usb_sndctrlpipe(udev, 0),
73 0x1,
74 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
75 0x0, reg, &data, 1, CTRL_TIMEOUT);
76 if (ret < 0)
77 PDEBUG(D_ERR, "write failed");
78}
79
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030080static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030081{
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030082 u8 data;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030083 int ret;
84
85 ret = usb_control_msg(udev,
86 usb_rcvctrlpipe(udev, 0),
87 0x1,
88 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 0x0, reg, &data, 1, CTRL_TIMEOUT);
Antonio Ospite9e1e7b02008-12-03 14:10:01 -030090 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030091 if (ret < 0)
92 PDEBUG(D_ERR, "read failed");
93 return data;
94}
95
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030096/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
97 * (direction and output)? */
98static void ov534_set_led(struct usb_device *udev, int status)
99{
Antonio Ospite9e1e7b02008-12-03 14:10:01 -0300100 u8 data;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300101
102 PDEBUG(D_CONF, "led status: %d", status);
103
104 data = ov534_reg_read(udev, 0x21);
105 data |= 0x80;
106 ov534_reg_write(udev, 0x21, data);
107
108 data = ov534_reg_read(udev, 0x23);
109 if (status)
110 data |= 0x80;
111 else
112 data &= ~(0x80);
113
114 ov534_reg_write(udev, 0x23, data);
115}
116
117static int sccb_check_status(struct usb_device *udev)
118{
Antonio Ospite9e1e7b02008-12-03 14:10:01 -0300119 u8 data;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300120 int i;
121
122 for (i = 0; i < 5; i++) {
123 data = ov534_reg_read(udev, OV534_REG_STATUS);
124
Antonio Ospite9e1e7b02008-12-03 14:10:01 -0300125 switch (data) {
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300126 case 0x00:
127 return 1;
128 case 0x04:
129 return 0;
130 case 0x03:
131 break;
132 default:
133 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
134 data, i + 1);
135 }
136 }
137 return 0;
138}
139
Antonio Ospite9e1e7b02008-12-03 14:10:01 -0300140static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300141{
Antonio Ospite9e1e7b02008-12-03 14:10:01 -0300142 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300143 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
144 ov534_reg_write(udev, OV534_REG_WRITE, val);
145 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
146
147 if (!sccb_check_status(udev))
148 PDEBUG(D_ERR, "sccb_reg_write failed");
149}
150
Jim Paris47dfd212008-12-04 04:28:27 -0300151static const __u8 ov534_reg_initdata[][2] = {
152 { 0xe7, 0x3a },
153
154 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
155
156 { 0xc2, 0x0c },
157 { 0x88, 0xf8 },
158 { 0xc3, 0x69 },
159 { 0x89, 0xff },
160 { 0x76, 0x03 },
161 { 0x92, 0x01 },
162 { 0x93, 0x18 },
163 { 0x94, 0x10 },
164 { 0x95, 0x10 },
165 { 0xe2, 0x00 },
166 { 0xe7, 0x3e },
167
Jim Paris47dfd212008-12-04 04:28:27 -0300168 { 0x96, 0x00 },
169
170 { 0x97, 0x20 },
171 { 0x97, 0x20 },
172 { 0x97, 0x20 },
173 { 0x97, 0x0a },
174 { 0x97, 0x3f },
175 { 0x97, 0x4a },
176 { 0x97, 0x20 },
177 { 0x97, 0x15 },
178 { 0x97, 0x0b },
179
180 { 0x8e, 0x40 },
181 { 0x1f, 0x81 },
182 { 0x34, 0x05 },
183 { 0xe3, 0x04 },
184 { 0x88, 0x00 },
185 { 0x89, 0x00 },
186 { 0x76, 0x00 },
187 { 0xe7, 0x2e },
188 { 0x31, 0xf9 },
189 { 0x25, 0x42 },
190 { 0x21, 0xf0 },
191
192 { 0x1c, 0x00 },
193 { 0x1d, 0x40 },
194 { 0x1d, 0x02 },
195 { 0x1d, 0x00 },
Jim Paris5ea9c4d2008-12-04 04:36:14 -0300196 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
197 { 0x1d, 0x58 }, /* frame size */
198 { 0x1d, 0x00 }, /* frame size */
Jim Paris47dfd212008-12-04 04:28:27 -0300199
200 { 0x8d, 0x1c },
201 { 0x8e, 0x80 },
202 { 0xe5, 0x04 },
203
204 { 0xc0, 0x50 },
205 { 0xc1, 0x3c },
206 { 0xc2, 0x0c },
207};
208
209static const __u8 ov772x_reg_initdata[][2] = {
210 { 0x12, 0x80 },
211 { 0x11, 0x01 },
212
213 { 0x3d, 0x03 },
214 { 0x17, 0x26 },
215 { 0x18, 0xa0 },
216 { 0x19, 0x07 },
217 { 0x1a, 0xf0 },
218 { 0x32, 0x00 },
219 { 0x29, 0xa0 },
220 { 0x2c, 0xf0 },
221 { 0x65, 0x20 },
222 { 0x11, 0x01 },
223 { 0x42, 0x7f },
224 { 0x63, 0xe0 },
225 { 0x64, 0xff },
226 { 0x66, 0x00 },
227 { 0x13, 0xf0 },
228 { 0x0d, 0x41 },
229 { 0x0f, 0xc5 },
230 { 0x14, 0x11 },
231
232 { 0x22, 0x7f },
233 { 0x23, 0x03 },
234 { 0x24, 0x40 },
235 { 0x25, 0x30 },
236 { 0x26, 0xa1 },
237 { 0x2a, 0x00 },
238 { 0x2b, 0x00 },
239 { 0x6b, 0xaa },
240 { 0x13, 0xff },
241
242 { 0x90, 0x05 },
243 { 0x91, 0x01 },
244 { 0x92, 0x03 },
245 { 0x93, 0x00 },
246 { 0x94, 0x60 },
247 { 0x95, 0x3c },
248 { 0x96, 0x24 },
249 { 0x97, 0x1e },
250 { 0x98, 0x62 },
251 { 0x99, 0x80 },
252 { 0x9a, 0x1e },
253 { 0x9b, 0x08 },
254 { 0x9c, 0x20 },
255 { 0x9e, 0x81 },
256
257 { 0xa6, 0x04 },
258 { 0x7e, 0x0c },
259 { 0x7f, 0x16 },
260 { 0x80, 0x2a },
261 { 0x81, 0x4e },
262 { 0x82, 0x61 },
263 { 0x83, 0x6f },
264 { 0x84, 0x7b },
265 { 0x85, 0x86 },
266 { 0x86, 0x8e },
267 { 0x87, 0x97 },
268 { 0x88, 0xa4 },
269 { 0x89, 0xaf },
270 { 0x8a, 0xc5 },
271 { 0x8b, 0xd7 },
272 { 0x8c, 0xe8 },
273 { 0x8d, 0x20 },
274
275 { 0x0c, 0x90 },
276
277 { 0x2b, 0x00 },
278 { 0x22, 0x7f },
279 { 0x23, 0x03 },
280 { 0x11, 0x01 },
281 { 0x0c, 0xd0 },
282 { 0x64, 0xff },
283 { 0x0d, 0x41 },
284
285 { 0x14, 0x41 },
286 { 0x0e, 0xcd },
287 { 0xac, 0xbf },
288 { 0x8e, 0x00 },
289 { 0x0c, 0xd0 }
290};
291
292
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300293/* setup method */
294static void ov534_setup(struct usb_device *udev)
295{
Jim Paris47dfd212008-12-04 04:28:27 -0300296 int i;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300297
Jim Paris47dfd212008-12-04 04:28:27 -0300298 /* Initialize bridge chip */
299 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
300 ov534_reg_write(udev, ov534_reg_initdata[i][0],
301 ov534_reg_initdata[i][1]);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300302
303 ov534_set_led(udev, 1);
304
Jim Paris47dfd212008-12-04 04:28:27 -0300305 /* Initialize sensor */
306 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
307 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
308 ov772x_reg_initdata[i][1]);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300309
310 ov534_reg_write(udev, 0xe0, 0x09);
311 ov534_set_led(udev, 0);
312}
313
314/* this function is called at probe time */
315static int sd_config(struct gspca_dev *gspca_dev,
316 const struct usb_device_id *id)
317{
318 struct cam *cam;
319
320 cam = &gspca_dev->cam;
321
322 cam->epaddr = 0x01;
323 cam->cam_mode = vga_mode;
324 cam->nmodes = ARRAY_SIZE(vga_mode);
325
Jim Parisfb139222008-12-04 04:52:40 -0300326 cam->bulk_size = 2048;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300327 cam->bulk_nurbs = 2;
328
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300329 return 0;
330}
331
332/* this function is called at probe and resume time */
333static int sd_init(struct gspca_dev *gspca_dev)
334{
Antonio Ospite3adba442008-12-03 14:01:54 -0300335 int fr;
336
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300337 ov534_setup(gspca_dev->dev);
338
Antonio Ospite3adba442008-12-03 14:01:54 -0300339 fr = frame_rate;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300340
Antonio Ospite3adba442008-12-03 14:01:54 -0300341 switch (fr) {
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300342 case 50:
343 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300344 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
Jim Paris47dfd212008-12-04 04:28:27 -0300345 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300346 break;
347 case 40:
348 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300349 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
Jim Paris47dfd212008-12-04 04:28:27 -0300350 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300351 break;
Antonio Ospite3adba442008-12-03 14:01:54 -0300352/* case 30: */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300353 default:
Antonio Ospite3adba442008-12-03 14:01:54 -0300354 fr = 30;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300355 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300356 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
Jim Paris47dfd212008-12-04 04:28:27 -0300357 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300358 break;
359 case 15:
360 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300361 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
Jim Paris47dfd212008-12-04 04:28:27 -0300362 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300363 break;
Antonio Ospite3adba442008-12-03 14:01:54 -0300364 }
365
366 PDEBUG(D_PROBE, "frame_rate: %d", fr);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300367
368 return 0;
369}
370
371static int sd_start(struct gspca_dev *gspca_dev)
372{
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300373 /* start streaming data */
374 ov534_set_led(gspca_dev->dev, 1);
375 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
376
377 return 0;
378}
379
380static void sd_stopN(struct gspca_dev *gspca_dev)
381{
382 /* stop streaming data */
383 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
384 ov534_set_led(gspca_dev->dev, 0);
385}
386
Jim Parisfb139222008-12-04 04:52:40 -0300387/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
388#define UVC_STREAM_EOH (1 << 7)
389#define UVC_STREAM_ERR (1 << 6)
390#define UVC_STREAM_STI (1 << 5)
391#define UVC_STREAM_RES (1 << 4)
392#define UVC_STREAM_SCR (1 << 3)
393#define UVC_STREAM_PTS (1 << 2)
394#define UVC_STREAM_EOF (1 << 1)
395#define UVC_STREAM_FID (1 << 0)
396
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300397static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
398 __u8 *data, int len)
399{
Jim Parisfb139222008-12-04 04:52:40 -0300400 static __u32 last_pts;
401 __u32 this_pts;
402 static int last_fid;
403 int this_fid;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300404
Jim Parisfb139222008-12-04 04:52:40 -0300405 /* Payloads are prefixed with a the UVC-style header. We
406 consider a frame to start when the FID toggles, or the PTS
407 changes. A frame ends when EOF is set, and we've received
408 the correct number of bytes. */
409
410 /* Verify UVC header. Header length is always 12 */
411 if (data[0] != 12 || len < 12) {
412 PDEBUG(D_PACK, "bad header");
413 goto discard;
414 }
415
416 /* Check errors */
417 if (data[1] & UVC_STREAM_ERR) {
418 PDEBUG(D_PACK, "payload error");
419 goto discard;
420 }
421
422 /* Extract PTS and FID */
423 if (!(data[1] & UVC_STREAM_PTS)) {
424 PDEBUG(D_PACK, "PTS not present");
425 goto discard;
426 }
427 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
428 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
429
430 /* If PTS or FID has changed, start a new frame. */
431 if (this_pts != last_pts || this_fid != last_fid) {
432 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
433 last_pts = this_pts;
434 last_fid = this_fid;
435 }
436
437 /* Add the data from this payload */
438 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
439 data + 12, len - 12);
440
441 /* If this packet is marked as EOF, end the frame */
442 if (data[1] & UVC_STREAM_EOF) {
443 last_pts = 0;
444
445 if ((frame->data_end - frame->data) !=
446 (gspca_dev->width * gspca_dev->height * 2)) {
447 PDEBUG(D_PACK, "short frame");
448 goto discard;
449 }
450
451 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
452 }
453
454 /* Done */
455 return;
456
457discard:
458 /* Discard data until a new frame starts. */
459 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300460}
461
462/* sub-driver description */
463static const struct sd_desc sd_desc = {
464 .name = MODULE_NAME,
465 .ctrls = sd_ctrls,
466 .nctrls = ARRAY_SIZE(sd_ctrls),
467 .config = sd_config,
468 .init = sd_init,
469 .start = sd_start,
470 .stopN = sd_stopN,
471 .pkt_scan = sd_pkt_scan,
472};
473
474/* -- module initialisation -- */
475static const __devinitdata struct usb_device_id device_table[] = {
476 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
477 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
478 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
479 {}
480};
481
482MODULE_DEVICE_TABLE(usb, device_table);
483
484/* -- device connect -- */
485static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
486{
487 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
488 THIS_MODULE);
489}
490
491static struct usb_driver sd_driver = {
492 .name = MODULE_NAME,
493 .id_table = device_table,
494 .probe = sd_probe,
495 .disconnect = gspca_disconnect,
496#ifdef CONFIG_PM
497 .suspend = gspca_suspend,
498 .resume = gspca_resume,
499#endif
500};
501
502/* -- module insert / remove -- */
503static int __init sd_mod_init(void)
504{
505 if (usb_register(&sd_driver) < 0)
506 return -1;
507 PDEBUG(D_PROBE, "registered");
508 return 0;
509}
510
511static void __exit sd_mod_exit(void)
512{
513 usb_deregister(&sd_driver);
514 PDEBUG(D_PROBE, "deregistered");
515}
516
517module_init(sd_mod_init);
518module_exit(sd_mod_exit);
519
520module_param(frame_rate, int, 0644);
521MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");