2 * ov534/ov772x gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
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/
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
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.
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
24 #define MODULE_NAME "ov534"
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
35 #define OV534_OP_WRITE_3 0x37
36 #define OV534_OP_WRITE_2 0x33
37 #define OV534_OP_READ_2 0xf9
39 #define CTRL_TIMEOUT 500
41 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
42 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
43 MODULE_LICENSE("GPL");
45 /* global parameters */
46 static int frame_rate;
48 /* specific webcam descriptor */
50 struct gspca_dev gspca_dev; /* !! must be the first item */
53 /* V4L2 controls supported by the driver */
54 static struct ctrl sd_ctrls[] = {
57 static 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,
65 static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
70 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
71 ret = usb_control_msg(udev,
72 usb_sndctrlpipe(udev, 0),
74 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
75 0x0, reg, &data, 1, CTRL_TIMEOUT);
77 PDEBUG(D_ERR, "write failed");
80 static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
85 ret = usb_control_msg(udev,
86 usb_rcvctrlpipe(udev, 0),
88 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 0x0, reg, &data, 1, CTRL_TIMEOUT);
90 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
92 PDEBUG(D_ERR, "read failed");
96 static void ov534_reg_verify_write(struct usb_device *udev, u16 reg, u8 val)
100 ov534_reg_write(udev, reg, val);
101 data = ov534_reg_read(udev, reg);
103 PDEBUG(D_ERR | D_USBO,
104 "unexpected result from read: 0x%02x != 0x%02x", val,
109 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
110 * (direction and output)? */
111 static void ov534_set_led(struct usb_device *udev, int status)
115 PDEBUG(D_CONF, "led status: %d", status);
117 data = ov534_reg_read(udev, 0x21);
119 ov534_reg_write(udev, 0x21, data);
121 data = ov534_reg_read(udev, 0x23);
127 ov534_reg_write(udev, 0x23, data);
130 static int sccb_check_status(struct usb_device *udev)
135 for (i = 0; i < 5; i++) {
136 data = ov534_reg_read(udev, OV534_REG_STATUS);
146 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
153 static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
155 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
156 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
157 ov534_reg_write(udev, OV534_REG_WRITE, val);
158 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
160 if (!sccb_check_status(udev))
161 PDEBUG(D_ERR, "sccb_reg_write failed");
165 static void ov534_setup(struct usb_device *udev)
167 ov534_reg_verify_write(udev, 0xe7, 0x3a);
169 ov534_reg_write(udev, OV534_REG_ADDRESS, 0x60);
170 ov534_reg_write(udev, OV534_REG_ADDRESS, 0x60);
171 ov534_reg_write(udev, OV534_REG_ADDRESS, 0x60);
172 ov534_reg_write(udev, OV534_REG_ADDRESS, 0x42);
174 ov534_reg_verify_write(udev, 0xc2, 0x0c);
175 ov534_reg_verify_write(udev, 0x88, 0xf8);
176 ov534_reg_verify_write(udev, 0xc3, 0x69);
177 ov534_reg_verify_write(udev, 0x89, 0xff);
178 ov534_reg_verify_write(udev, 0x76, 0x03);
179 ov534_reg_verify_write(udev, 0x92, 0x01);
180 ov534_reg_verify_write(udev, 0x93, 0x18);
181 ov534_reg_verify_write(udev, 0x94, 0x10);
182 ov534_reg_verify_write(udev, 0x95, 0x10);
183 ov534_reg_verify_write(udev, 0xe2, 0x00);
184 ov534_reg_verify_write(udev, 0xe7, 0x3e);
186 ov534_reg_write(udev, 0x1c, 0x0a);
187 ov534_reg_write(udev, 0x1d, 0x22);
188 ov534_reg_write(udev, 0x1d, 0x06);
190 ov534_reg_verify_write(udev, 0x96, 0x00);
192 ov534_reg_write(udev, 0x97, 0x20);
193 ov534_reg_write(udev, 0x97, 0x20);
194 ov534_reg_write(udev, 0x97, 0x20);
195 ov534_reg_write(udev, 0x97, 0x0a);
196 ov534_reg_write(udev, 0x97, 0x3f);
197 ov534_reg_write(udev, 0x97, 0x4a);
198 ov534_reg_write(udev, 0x97, 0x20);
199 ov534_reg_write(udev, 0x97, 0x15);
200 ov534_reg_write(udev, 0x97, 0x0b);
202 ov534_reg_verify_write(udev, 0x8e, 0x40);
203 ov534_reg_verify_write(udev, 0x1f, 0x81);
204 ov534_reg_verify_write(udev, 0x34, 0x05);
205 ov534_reg_verify_write(udev, 0xe3, 0x04);
206 ov534_reg_verify_write(udev, 0x88, 0x00);
207 ov534_reg_verify_write(udev, 0x89, 0x00);
208 ov534_reg_verify_write(udev, 0x76, 0x00);
209 ov534_reg_verify_write(udev, 0xe7, 0x2e);
210 ov534_reg_verify_write(udev, 0x31, 0xf9);
211 ov534_reg_verify_write(udev, 0x25, 0x42);
212 ov534_reg_verify_write(udev, 0x21, 0xf0);
214 ov534_reg_write(udev, 0x1c, 0x00);
215 ov534_reg_write(udev, 0x1d, 0x40);
216 ov534_reg_write(udev, 0x1d, 0x02);
217 ov534_reg_write(udev, 0x1d, 0x00);
218 ov534_reg_write(udev, 0x1d, 0x02);
219 ov534_reg_write(udev, 0x1d, 0x57);
220 ov534_reg_write(udev, 0x1d, 0xff);
222 ov534_reg_verify_write(udev, 0x8d, 0x1c);
223 ov534_reg_verify_write(udev, 0x8e, 0x80);
224 ov534_reg_verify_write(udev, 0xe5, 0x04);
226 ov534_set_led(udev, 1);
228 sccb_reg_write(udev, 0x12, 0x80);
229 sccb_reg_write(udev, 0x11, 0x01);
230 sccb_reg_write(udev, 0x11, 0x01);
231 sccb_reg_write(udev, 0x11, 0x01);
232 sccb_reg_write(udev, 0x11, 0x01);
233 sccb_reg_write(udev, 0x11, 0x01);
234 sccb_reg_write(udev, 0x11, 0x01);
235 sccb_reg_write(udev, 0x11, 0x01);
236 sccb_reg_write(udev, 0x11, 0x01);
237 sccb_reg_write(udev, 0x11, 0x01);
238 sccb_reg_write(udev, 0x11, 0x01);
239 sccb_reg_write(udev, 0x11, 0x01);
241 ov534_set_led(udev, 0);
243 sccb_reg_write(udev, 0x3d, 0x03);
244 sccb_reg_write(udev, 0x17, 0x26);
245 sccb_reg_write(udev, 0x18, 0xa0);
246 sccb_reg_write(udev, 0x19, 0x07);
247 sccb_reg_write(udev, 0x1a, 0xf0);
248 sccb_reg_write(udev, 0x32, 0x00);
249 sccb_reg_write(udev, 0x29, 0xa0);
250 sccb_reg_write(udev, 0x2c, 0xf0);
251 sccb_reg_write(udev, 0x65, 0x20);
252 sccb_reg_write(udev, 0x11, 0x01);
253 sccb_reg_write(udev, 0x42, 0x7f);
254 sccb_reg_write(udev, 0x63, 0xe0);
255 sccb_reg_write(udev, 0x64, 0xff);
256 sccb_reg_write(udev, 0x66, 0x00);
257 sccb_reg_write(udev, 0x13, 0xf0);
258 sccb_reg_write(udev, 0x0d, 0x41);
259 sccb_reg_write(udev, 0x0f, 0xc5);
260 sccb_reg_write(udev, 0x14, 0x11);
262 ov534_set_led(udev, 1);
264 sccb_reg_write(udev, 0x22, 0x7f);
265 sccb_reg_write(udev, 0x23, 0x03);
266 sccb_reg_write(udev, 0x24, 0x40);
267 sccb_reg_write(udev, 0x25, 0x30);
268 sccb_reg_write(udev, 0x26, 0xa1);
269 sccb_reg_write(udev, 0x2a, 0x00);
270 sccb_reg_write(udev, 0x2b, 0x00);
271 sccb_reg_write(udev, 0x6b, 0xaa);
272 sccb_reg_write(udev, 0x13, 0xff);
274 ov534_set_led(udev, 0);
276 sccb_reg_write(udev, 0x90, 0x05);
277 sccb_reg_write(udev, 0x91, 0x01);
278 sccb_reg_write(udev, 0x92, 0x03);
279 sccb_reg_write(udev, 0x93, 0x00);
280 sccb_reg_write(udev, 0x94, 0x60);
281 sccb_reg_write(udev, 0x95, 0x3c);
282 sccb_reg_write(udev, 0x96, 0x24);
283 sccb_reg_write(udev, 0x97, 0x1e);
284 sccb_reg_write(udev, 0x98, 0x62);
285 sccb_reg_write(udev, 0x99, 0x80);
286 sccb_reg_write(udev, 0x9a, 0x1e);
287 sccb_reg_write(udev, 0x9b, 0x08);
288 sccb_reg_write(udev, 0x9c, 0x20);
289 sccb_reg_write(udev, 0x9e, 0x81);
291 ov534_set_led(udev, 1);
293 sccb_reg_write(udev, 0xa6, 0x04);
294 sccb_reg_write(udev, 0x7e, 0x0c);
295 sccb_reg_write(udev, 0x7f, 0x16);
296 sccb_reg_write(udev, 0x80, 0x2a);
297 sccb_reg_write(udev, 0x81, 0x4e);
298 sccb_reg_write(udev, 0x82, 0x61);
299 sccb_reg_write(udev, 0x83, 0x6f);
300 sccb_reg_write(udev, 0x84, 0x7b);
301 sccb_reg_write(udev, 0x85, 0x86);
302 sccb_reg_write(udev, 0x86, 0x8e);
303 sccb_reg_write(udev, 0x87, 0x97);
304 sccb_reg_write(udev, 0x88, 0xa4);
305 sccb_reg_write(udev, 0x89, 0xaf);
306 sccb_reg_write(udev, 0x8a, 0xc5);
307 sccb_reg_write(udev, 0x8b, 0xd7);
308 sccb_reg_write(udev, 0x8c, 0xe8);
309 sccb_reg_write(udev, 0x8d, 0x20);
311 sccb_reg_write(udev, 0x0c, 0x90);
313 ov534_reg_verify_write(udev, 0xc0, 0x50);
314 ov534_reg_verify_write(udev, 0xc1, 0x3c);
315 ov534_reg_verify_write(udev, 0xc2, 0x0c);
317 ov534_set_led(udev, 1);
319 sccb_reg_write(udev, 0x2b, 0x00);
320 sccb_reg_write(udev, 0x22, 0x7f);
321 sccb_reg_write(udev, 0x23, 0x03);
322 sccb_reg_write(udev, 0x11, 0x01);
323 sccb_reg_write(udev, 0x0c, 0xd0);
324 sccb_reg_write(udev, 0x64, 0xff);
325 sccb_reg_write(udev, 0x0d, 0x41);
327 sccb_reg_write(udev, 0x14, 0x41);
328 sccb_reg_write(udev, 0x0e, 0xcd);
329 sccb_reg_write(udev, 0xac, 0xbf);
330 sccb_reg_write(udev, 0x8e, 0x00);
331 sccb_reg_write(udev, 0x0c, 0xd0);
333 ov534_reg_write(udev, 0xe0, 0x09);
334 ov534_set_led(udev, 0);
337 /* this function is called at probe time */
338 static int sd_config(struct gspca_dev *gspca_dev,
339 const struct usb_device_id *id)
343 cam = &gspca_dev->cam;
346 cam->cam_mode = vga_mode;
347 cam->nmodes = ARRAY_SIZE(vga_mode);
349 cam->bulk_size = vga_mode[0].sizeimage;
352 PDEBUG(D_PROBE, "bulk_size = %d", cam->bulk_size);
357 /* this function is called at probe and resume time */
358 static int sd_init(struct gspca_dev *gspca_dev)
362 ov534_setup(gspca_dev->dev);
368 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
369 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
370 ov534_reg_verify_write(gspca_dev->dev, 0xe5, 0x02);
373 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
374 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
375 ov534_reg_verify_write(gspca_dev->dev, 0xe5, 0x04);
380 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
381 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
382 ov534_reg_verify_write(gspca_dev->dev, 0xe5, 0x02);
385 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
386 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
387 ov534_reg_verify_write(gspca_dev->dev, 0xe5, 0x04);
391 PDEBUG(D_PROBE, "frame_rate: %d", fr);
396 static int sd_start(struct gspca_dev *gspca_dev)
398 PDEBUG(D_PROBE, "width = %d, height = %d",
399 gspca_dev->width, gspca_dev->height);
401 gspca_dev->cam.bulk_size = gspca_dev->width * gspca_dev->height * 2;
403 /* start streaming data */
404 ov534_set_led(gspca_dev->dev, 1);
405 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
410 static void sd_stopN(struct gspca_dev *gspca_dev)
412 /* stop streaming data */
413 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
414 ov534_set_led(gspca_dev->dev, 0);
417 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
421 * The current camera setup doesn't stream the last pixel, so we set it
424 __u8 last_pixel[4] = { 0, 0, 0, 0 };
425 int framesize = gspca_dev->cam.bulk_size;
427 if (len == framesize - 4) {
429 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, len);
431 gspca_frame_add(gspca_dev, LAST_PACKET, frame, last_pixel,
434 PDEBUG(D_PACK, "packet len = %d, framesize = %d", len,
438 /* sub-driver description */
439 static const struct sd_desc sd_desc = {
442 .nctrls = ARRAY_SIZE(sd_ctrls),
447 .pkt_scan = sd_pkt_scan,
450 /* -- module initialisation -- */
451 static const __devinitdata struct usb_device_id device_table[] = {
452 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
453 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
454 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
458 MODULE_DEVICE_TABLE(usb, device_table);
460 /* -- device connect -- */
461 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
463 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
467 static struct usb_driver sd_driver = {
469 .id_table = device_table,
471 .disconnect = gspca_disconnect,
473 .suspend = gspca_suspend,
474 .resume = gspca_resume,
478 /* -- module insert / remove -- */
479 static int __init sd_mod_init(void)
481 if (usb_register(&sd_driver) < 0)
483 PDEBUG(D_PROBE, "registered");
487 static void __exit sd_mod_exit(void)
489 usb_deregister(&sd_driver);
490 PDEBUG(D_PROBE, "deregistered");
493 module_init(sd_mod_init);
494 module_exit(sd_mod_exit);
496 module_param(frame_rate, int, 0644);
497 MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");