]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/gspca/ov534.c
edcd92557c87a6587cf99db68d4f76d70a43e660
[linux-2.6-omap-h63xx.git] / drivers / media / video / gspca / ov534.c
1 /*
2  * ov534/ov772x gspca driver
3  * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4  * Copyright (C) 2008 Jim Paris <jim@jtan.com>
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
42 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
43 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
44 MODULE_LICENSE("GPL");
45
46 /* specific webcam descriptor */
47 struct sd {
48         struct gspca_dev gspca_dev;     /* !! must be the first item */
49         __u32 last_fid;
50         __u32 last_pts;
51         int frame_rate;
52 };
53
54 /* V4L2 controls supported by the driver */
55 static struct ctrl sd_ctrls[] = {
56 };
57
58 static 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
66 static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
67 {
68         u8 data = val;
69         int ret;
70
71         PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
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
81 static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
82 {
83         u8 data;
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);
91         PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
92         if (ret < 0)
93                 PDEBUG(D_ERR, "read failed");
94         return data;
95 }
96
97 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
98  * (direction and output)? */
99 static void ov534_set_led(struct usb_device *udev, int status)
100 {
101         u8 data;
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
118 static int sccb_check_status(struct usb_device *udev)
119 {
120         u8 data;
121         int i;
122
123         for (i = 0; i < 5; i++) {
124                 data = ov534_reg_read(udev, OV534_REG_STATUS);
125
126                 switch (data) {
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
141 static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
142 {
143         PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
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
152 static const __u8 ov534_reg_initdata[][2] = {
153         { 0xe7, 0x3a },
154
155         { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
156
157         { 0xc2, 0x0c },
158         { 0x88, 0xf8 },
159         { 0xc3, 0x69 },
160         { 0x89, 0xff },
161         { 0x76, 0x03 },
162         { 0x92, 0x01 },
163         { 0x93, 0x18 },
164         { 0x94, 0x10 },
165         { 0x95, 0x10 },
166         { 0xe2, 0x00 },
167         { 0xe7, 0x3e },
168
169         { 0x96, 0x00 },
170
171         { 0x97, 0x20 },
172         { 0x97, 0x20 },
173         { 0x97, 0x20 },
174         { 0x97, 0x0a },
175         { 0x97, 0x3f },
176         { 0x97, 0x4a },
177         { 0x97, 0x20 },
178         { 0x97, 0x15 },
179         { 0x97, 0x0b },
180
181         { 0x8e, 0x40 },
182         { 0x1f, 0x81 },
183         { 0x34, 0x05 },
184         { 0xe3, 0x04 },
185         { 0x88, 0x00 },
186         { 0x89, 0x00 },
187         { 0x76, 0x00 },
188         { 0xe7, 0x2e },
189         { 0x31, 0xf9 },
190         { 0x25, 0x42 },
191         { 0x21, 0xf0 },
192
193         { 0x1c, 0x00 },
194         { 0x1d, 0x40 },
195         { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
196         { 0x1d, 0x00 }, /* payload size */
197         { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
198         { 0x1d, 0x58 }, /* frame size */
199         { 0x1d, 0x00 }, /* frame size */
200
201         { 0x1c, 0x0a },
202         { 0x1d, 0x08 }, /* turn on UVC header */
203         { 0x1d, 0x0e }, /* .. */
204
205         { 0x8d, 0x1c },
206         { 0x8e, 0x80 },
207         { 0xe5, 0x04 },
208
209         { 0xc0, 0x50 },
210         { 0xc1, 0x3c },
211         { 0xc2, 0x0c },
212 };
213
214 static const __u8 ov772x_reg_initdata[][2] = {
215         { 0x12, 0x80 },
216         { 0x11, 0x01 },
217
218         { 0x3d, 0x03 },
219         { 0x17, 0x26 },
220         { 0x18, 0xa0 },
221         { 0x19, 0x07 },
222         { 0x1a, 0xf0 },
223         { 0x32, 0x00 },
224         { 0x29, 0xa0 },
225         { 0x2c, 0xf0 },
226         { 0x65, 0x20 },
227         { 0x11, 0x01 },
228         { 0x42, 0x7f },
229         { 0x63, 0xe0 },
230         { 0x64, 0xff },
231         { 0x66, 0x00 },
232         { 0x13, 0xf0 },
233         { 0x0d, 0x41 },
234         { 0x0f, 0xc5 },
235         { 0x14, 0x11 },
236
237         { 0x22, 0x7f },
238         { 0x23, 0x03 },
239         { 0x24, 0x40 },
240         { 0x25, 0x30 },
241         { 0x26, 0xa1 },
242         { 0x2a, 0x00 },
243         { 0x2b, 0x00 },
244         { 0x6b, 0xaa },
245         { 0x13, 0xff },
246
247         { 0x90, 0x05 },
248         { 0x91, 0x01 },
249         { 0x92, 0x03 },
250         { 0x93, 0x00 },
251         { 0x94, 0x60 },
252         { 0x95, 0x3c },
253         { 0x96, 0x24 },
254         { 0x97, 0x1e },
255         { 0x98, 0x62 },
256         { 0x99, 0x80 },
257         { 0x9a, 0x1e },
258         { 0x9b, 0x08 },
259         { 0x9c, 0x20 },
260         { 0x9e, 0x81 },
261
262         { 0xa6, 0x04 },
263         { 0x7e, 0x0c },
264         { 0x7f, 0x16 },
265         { 0x80, 0x2a },
266         { 0x81, 0x4e },
267         { 0x82, 0x61 },
268         { 0x83, 0x6f },
269         { 0x84, 0x7b },
270         { 0x85, 0x86 },
271         { 0x86, 0x8e },
272         { 0x87, 0x97 },
273         { 0x88, 0xa4 },
274         { 0x89, 0xaf },
275         { 0x8a, 0xc5 },
276         { 0x8b, 0xd7 },
277         { 0x8c, 0xe8 },
278         { 0x8d, 0x20 },
279
280         { 0x0c, 0x90 },
281
282         { 0x2b, 0x00 },
283         { 0x22, 0x7f },
284         { 0x23, 0x03 },
285         { 0x11, 0x01 },
286         { 0x0c, 0xd0 },
287         { 0x64, 0xff },
288         { 0x0d, 0x41 },
289
290         { 0x14, 0x41 },
291         { 0x0e, 0xcd },
292         { 0xac, 0xbf },
293         { 0x8e, 0x00 },
294         { 0x0c, 0xd0 }
295 };
296
297 /* set framerate */
298 static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
299 {
300         struct sd *sd = (struct sd *) gspca_dev;
301         int fr = sd->frame_rate;
302
303         switch (fr) {
304         case 50:
305                 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
306                 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
307                 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
308                 break;
309         case 40:
310                 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
311                 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
312                 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
313                 break;
314 /*      case 30: */
315         default:
316                 fr = 30;
317                 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
318                 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
319                 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
320                 break;
321         case 15:
322                 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
323                 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
324                 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
325                 break;
326         }
327
328         sd->frame_rate = fr;
329         PDEBUG(D_PROBE, "frame_rate: %d", fr);
330 }
331
332 /* setup method */
333 static void ov534_setup(struct usb_device *udev)
334 {
335         int i;
336
337         /* Initialize bridge chip */
338         for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
339                 ov534_reg_write(udev, ov534_reg_initdata[i][0],
340                                 ov534_reg_initdata[i][1]);
341
342         ov534_set_led(udev, 1);
343
344         /* Initialize sensor */
345         for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
346                 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
347                                ov772x_reg_initdata[i][1]);
348
349         ov534_reg_write(udev, 0xe0, 0x09);
350         ov534_set_led(udev, 0);
351 }
352
353 /* this function is called at probe time */
354 static int sd_config(struct gspca_dev *gspca_dev,
355                      const struct usb_device_id *id)
356 {
357         struct cam *cam;
358
359         cam = &gspca_dev->cam;
360
361         cam->epaddr = 0x01;
362         cam->cam_mode = vga_mode;
363         cam->nmodes = ARRAY_SIZE(vga_mode);
364
365         cam->bulk_size = 16384;
366         cam->bulk_nurbs = 2;
367
368         return 0;
369 }
370
371 /* this function is called at probe and resume time */
372 static int sd_init(struct gspca_dev *gspca_dev)
373 {
374         ov534_setup(gspca_dev->dev);
375         ov534_set_frame_rate(gspca_dev);
376
377         return 0;
378 }
379
380 static int sd_start(struct gspca_dev *gspca_dev)
381 {
382         /* start streaming data */
383         ov534_set_led(gspca_dev->dev, 1);
384         ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
385
386         return 0;
387 }
388
389 static void sd_stopN(struct gspca_dev *gspca_dev)
390 {
391         /* stop streaming data */
392         ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
393         ov534_set_led(gspca_dev->dev, 0);
394 }
395
396 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
397 #define UVC_STREAM_EOH  (1 << 7)
398 #define UVC_STREAM_ERR  (1 << 6)
399 #define UVC_STREAM_STI  (1 << 5)
400 #define UVC_STREAM_RES  (1 << 4)
401 #define UVC_STREAM_SCR  (1 << 3)
402 #define UVC_STREAM_PTS  (1 << 2)
403 #define UVC_STREAM_EOF  (1 << 1)
404 #define UVC_STREAM_FID  (1 << 0)
405
406 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
407                         __u8 *data, int len)
408 {
409         struct sd *sd = (struct sd *) gspca_dev;
410         __u32 this_pts;
411         int this_fid;
412         int remaining_len = len;
413         __u8 *next_data = data;
414
415 scan_next:
416         if (remaining_len <= 0)
417                 return;
418
419         data = next_data;
420         len = min(remaining_len, 2048);
421         remaining_len -= len;
422         next_data += len;
423
424         /* Payloads are prefixed with a UVC-style header.  We
425            consider a frame to start when the FID toggles, or the PTS
426            changes.  A frame ends when EOF is set, and we've received
427            the correct number of bytes. */
428
429         /* Verify UVC header.  Header length is always 12 */
430         if (data[0] != 12 || len < 12) {
431                 PDEBUG(D_PACK, "bad header");
432                 goto discard;
433         }
434
435         /* Check errors */
436         if (data[1] & UVC_STREAM_ERR) {
437                 PDEBUG(D_PACK, "payload error");
438                 goto discard;
439         }
440
441         /* Extract PTS and FID */
442         if (!(data[1] & UVC_STREAM_PTS)) {
443                 PDEBUG(D_PACK, "PTS not present");
444                 goto discard;
445         }
446         this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
447         this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
448
449         /* If PTS or FID has changed, start a new frame. */
450         if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
451                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
452                 sd->last_pts = this_pts;
453                 sd->last_fid = this_fid;
454         }
455
456         /* Add the data from this payload */
457         gspca_frame_add(gspca_dev, INTER_PACKET, frame,
458                                 data + 12, len - 12);
459
460         /* If this packet is marked as EOF, end the frame */
461         if (data[1] & UVC_STREAM_EOF) {
462                 sd->last_pts = 0;
463
464                 if ((frame->data_end - frame->data) !=
465                     (gspca_dev->width * gspca_dev->height * 2)) {
466                         PDEBUG(D_PACK, "short frame");
467                         goto discard;
468                 }
469
470                 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
471         }
472
473         /* Done this payload */
474         goto scan_next;
475
476 discard:
477         /* Discard data until a new frame starts. */
478         gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
479         goto scan_next;
480 }
481
482 /* get stream parameters (framerate) */
483 int sd_get_streamparm(struct gspca_dev *gspca_dev,
484                       struct v4l2_streamparm *parm)
485 {
486         struct v4l2_captureparm *cp = &parm->parm.capture;
487         struct v4l2_fract *tpf = &cp->timeperframe;
488         struct sd *sd = (struct sd *) gspca_dev;
489
490         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
491                 return -EINVAL;
492
493         cp->capability |= V4L2_CAP_TIMEPERFRAME;
494         tpf->numerator = 1;
495         tpf->denominator = sd->frame_rate;
496
497         return 0;
498 }
499
500 /* set stream parameters (framerate) */
501 int sd_set_streamparm(struct gspca_dev *gspca_dev,
502                       struct v4l2_streamparm *parm)
503 {
504         struct v4l2_captureparm *cp = &parm->parm.capture;
505         struct v4l2_fract *tpf = &cp->timeperframe;
506         struct sd *sd = (struct sd *) gspca_dev;
507
508         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
509                 return -EINVAL;
510
511         /* Set requested framerate */
512         sd->frame_rate = tpf->denominator / tpf->numerator;
513         ov534_set_frame_rate(gspca_dev);
514
515         /* Return the actual framerate */
516         tpf->numerator = 1;
517         tpf->denominator = sd->frame_rate;
518
519         return 0;
520 }
521
522 /* sub-driver description */
523 static const struct sd_desc sd_desc = {
524         .name     = MODULE_NAME,
525         .ctrls    = sd_ctrls,
526         .nctrls   = ARRAY_SIZE(sd_ctrls),
527         .config   = sd_config,
528         .init     = sd_init,
529         .start    = sd_start,
530         .stopN    = sd_stopN,
531         .pkt_scan = sd_pkt_scan,
532         .get_streamparm = sd_get_streamparm,
533         .set_streamparm = sd_set_streamparm,
534 };
535
536 /* -- module initialisation -- */
537 static const __devinitdata struct usb_device_id device_table[] = {
538         {USB_DEVICE(0x06f8, 0x3002)},   /* Hercules Blog Webcam */
539         {USB_DEVICE(0x06f8, 0x3003)},   /* Hercules Dualpix HD Weblog */
540         {USB_DEVICE(0x1415, 0x2000)},   /* Sony HD Eye for PS3 (SLEH 00201) */
541         {}
542 };
543
544 MODULE_DEVICE_TABLE(usb, device_table);
545
546 /* -- device connect -- */
547 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
548 {
549         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
550                                THIS_MODULE);
551 }
552
553 static struct usb_driver sd_driver = {
554         .name       = MODULE_NAME,
555         .id_table   = device_table,
556         .probe      = sd_probe,
557         .disconnect = gspca_disconnect,
558 #ifdef CONFIG_PM
559         .suspend    = gspca_suspend,
560         .resume     = gspca_resume,
561 #endif
562 };
563
564 /* -- module insert / remove -- */
565 static int __init sd_mod_init(void)
566 {
567         if (usb_register(&sd_driver) < 0)
568                 return -1;
569         PDEBUG(D_PROBE, "registered");
570         return 0;
571 }
572
573 static void __exit sd_mod_exit(void)
574 {
575         usb_deregister(&sd_driver);
576         PDEBUG(D_PROBE, "deregistered");
577 }
578
579 module_init(sd_mod_init);
580 module_exit(sd_mod_exit);