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