Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / gspca / stk014.c
blob0fb534210a2c7f4bfe25ba3ca3737df7314feae1
1 /*
2 * Syntek DV4000 (STK014) subdriver
4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define MODULE_NAME "stk014"
23 #include "gspca.h"
24 #include "jpeg.h"
26 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
27 MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
28 MODULE_LICENSE("GPL");
30 /* specific webcam descriptor */
31 struct sd {
32 struct gspca_dev gspca_dev; /* !! must be the first item */
34 unsigned char brightness;
35 unsigned char contrast;
36 unsigned char colors;
37 unsigned char lightfreq;
38 u8 quality;
39 #define QUALITY_MIN 60
40 #define QUALITY_MAX 95
41 #define QUALITY_DEF 80
43 u8 *jpeg_hdr;
46 /* V4L2 controls supported by the driver */
47 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
48 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
49 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
50 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
51 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
52 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
53 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
54 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
56 static const struct ctrl sd_ctrls[] = {
59 .id = V4L2_CID_BRIGHTNESS,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Brightness",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 1,
65 #define BRIGHTNESS_DEF 127
66 .default_value = BRIGHTNESS_DEF,
68 .set = sd_setbrightness,
69 .get = sd_getbrightness,
73 .id = V4L2_CID_CONTRAST,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 .name = "Contrast",
76 .minimum = 0,
77 .maximum = 255,
78 .step = 1,
79 #define CONTRAST_DEF 127
80 .default_value = CONTRAST_DEF,
82 .set = sd_setcontrast,
83 .get = sd_getcontrast,
87 .id = V4L2_CID_SATURATION,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Color",
90 .minimum = 0,
91 .maximum = 255,
92 .step = 1,
93 #define COLOR_DEF 127
94 .default_value = COLOR_DEF,
96 .set = sd_setcolors,
97 .get = sd_getcolors,
101 .id = V4L2_CID_POWER_LINE_FREQUENCY,
102 .type = V4L2_CTRL_TYPE_MENU,
103 .name = "Light frequency filter",
104 .minimum = 1,
105 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
106 .step = 1,
107 #define FREQ_DEF 1
108 .default_value = FREQ_DEF,
110 .set = sd_setfreq,
111 .get = sd_getfreq,
115 static const struct v4l2_pix_format vga_mode[] = {
116 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
117 .bytesperline = 320,
118 .sizeimage = 320 * 240 * 3 / 8 + 590,
119 .colorspace = V4L2_COLORSPACE_JPEG,
120 .priv = 1},
121 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
122 .bytesperline = 640,
123 .sizeimage = 640 * 480 * 3 / 8 + 590,
124 .colorspace = V4L2_COLORSPACE_JPEG,
125 .priv = 0},
128 /* -- read a register -- */
129 static u8 reg_r(struct gspca_dev *gspca_dev,
130 __u16 index)
132 struct usb_device *dev = gspca_dev->dev;
133 int ret;
135 if (gspca_dev->usb_err < 0)
136 return 0;
137 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
138 0x00,
139 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
140 0x00,
141 index,
142 gspca_dev->usb_buf, 1,
143 500);
144 if (ret < 0) {
145 PDEBUG(D_ERR, "reg_r err %d", ret);
146 gspca_dev->usb_err = ret;
147 return 0;
149 return gspca_dev->usb_buf[0];
152 /* -- write a register -- */
153 static void reg_w(struct gspca_dev *gspca_dev,
154 __u16 index, __u16 value)
156 struct usb_device *dev = gspca_dev->dev;
157 int ret;
159 if (gspca_dev->usb_err < 0)
160 return;
161 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
162 0x01,
163 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
164 value,
165 index,
166 NULL,
168 500);
169 if (ret < 0) {
170 PDEBUG(D_ERR, "reg_w err %d", ret);
171 gspca_dev->usb_err = ret;
175 /* -- get a bulk value (4 bytes) -- */
176 static void rcv_val(struct gspca_dev *gspca_dev,
177 int ads)
179 struct usb_device *dev = gspca_dev->dev;
180 int alen, ret;
182 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
183 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
184 reg_w(gspca_dev, 0x636, ads & 0xff);
185 reg_w(gspca_dev, 0x637, 0);
186 reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
187 reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
188 reg_w(gspca_dev, 0x63a, 0);
189 reg_w(gspca_dev, 0x63b, 0);
190 reg_w(gspca_dev, 0x630, 5);
191 if (gspca_dev->usb_err < 0)
192 return;
193 ret = usb_bulk_msg(dev,
194 usb_rcvbulkpipe(dev, 0x05),
195 gspca_dev->usb_buf,
196 4, /* length */
197 &alen,
198 500); /* timeout in milliseconds */
199 if (ret < 0) {
200 PDEBUG(D_ERR, "rcv_val err %d", ret);
201 gspca_dev->usb_err = ret;
205 /* -- send a bulk value -- */
206 static void snd_val(struct gspca_dev *gspca_dev,
207 int ads,
208 unsigned int val)
210 struct usb_device *dev = gspca_dev->dev;
211 int alen, ret;
212 __u8 seq = 0;
214 if (ads == 0x003f08) {
215 reg_r(gspca_dev, 0x0704);
216 seq = reg_r(gspca_dev, 0x0705);
217 reg_r(gspca_dev, 0x0650);
218 reg_w(gspca_dev, 0x654, seq);
219 } else {
220 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
222 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
223 reg_w(gspca_dev, 0x656, ads & 0xff);
224 reg_w(gspca_dev, 0x657, 0);
225 reg_w(gspca_dev, 0x658, 0x04); /* size */
226 reg_w(gspca_dev, 0x659, 0);
227 reg_w(gspca_dev, 0x65a, 0);
228 reg_w(gspca_dev, 0x65b, 0);
229 reg_w(gspca_dev, 0x650, 5);
230 if (gspca_dev->usb_err < 0)
231 return;
232 gspca_dev->usb_buf[0] = val >> 24;
233 gspca_dev->usb_buf[1] = val >> 16;
234 gspca_dev->usb_buf[2] = val >> 8;
235 gspca_dev->usb_buf[3] = val;
236 ret = usb_bulk_msg(dev,
237 usb_sndbulkpipe(dev, 6),
238 gspca_dev->usb_buf,
240 &alen,
241 500); /* timeout in milliseconds */
242 if (ret < 0) {
243 PDEBUG(D_ERR, "snd_val err %d", ret);
244 gspca_dev->usb_err = ret;
245 } else {
246 if (ads == 0x003f08) {
247 seq += 4;
248 seq &= 0x3f;
249 reg_w(gspca_dev, 0x705, seq);
254 /* set a camera parameter */
255 static void set_par(struct gspca_dev *gspca_dev,
256 int parval)
258 snd_val(gspca_dev, 0x003f08, parval);
261 static void setbrightness(struct gspca_dev *gspca_dev)
263 struct sd *sd = (struct sd *) gspca_dev;
264 int parval;
266 parval = 0x06000000 /* whiteness */
267 + (sd->brightness << 16);
268 set_par(gspca_dev, parval);
271 static void setcontrast(struct gspca_dev *gspca_dev)
273 struct sd *sd = (struct sd *) gspca_dev;
274 int parval;
276 parval = 0x07000000 /* contrast */
277 + (sd->contrast << 16);
278 set_par(gspca_dev, parval);
281 static void setcolors(struct gspca_dev *gspca_dev)
283 struct sd *sd = (struct sd *) gspca_dev;
284 int parval;
286 parval = 0x08000000 /* saturation */
287 + (sd->colors << 16);
288 set_par(gspca_dev, parval);
291 static void setfreq(struct gspca_dev *gspca_dev)
293 struct sd *sd = (struct sd *) gspca_dev;
295 set_par(gspca_dev, sd->lightfreq == 1
296 ? 0x33640000 /* 50 Hz */
297 : 0x33780000); /* 60 Hz */
300 /* this function is called at probe time */
301 static int sd_config(struct gspca_dev *gspca_dev,
302 const struct usb_device_id *id)
304 struct sd *sd = (struct sd *) gspca_dev;
306 gspca_dev->cam.cam_mode = vga_mode;
307 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
308 sd->brightness = BRIGHTNESS_DEF;
309 sd->contrast = CONTRAST_DEF;
310 sd->colors = COLOR_DEF;
311 sd->lightfreq = FREQ_DEF;
312 sd->quality = QUALITY_DEF;
313 return 0;
316 /* this function is called at probe and resume time */
317 static int sd_init(struct gspca_dev *gspca_dev)
319 u8 ret;
321 /* check if the device responds */
322 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
323 ret = reg_r(gspca_dev, 0x0740);
324 if (gspca_dev->usb_err >= 0) {
325 if (ret != 0xff) {
326 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
327 gspca_dev->usb_err = -EIO;
330 return gspca_dev->usb_err;
333 /* -- start the camera -- */
334 static int sd_start(struct gspca_dev *gspca_dev)
336 struct sd *sd = (struct sd *) gspca_dev;
337 int ret, value;
339 /* create the JPEG header */
340 sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
341 if (!sd->jpeg_hdr)
342 return -ENOMEM;
343 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
344 0x22); /* JPEG 411 */
345 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
347 /* work on alternate 1 */
348 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
350 set_par(gspca_dev, 0x10000000);
351 set_par(gspca_dev, 0x00000000);
352 set_par(gspca_dev, 0x8002e001);
353 set_par(gspca_dev, 0x14000000);
354 if (gspca_dev->width > 320)
355 value = 0x8002e001; /* 640x480 */
356 else
357 value = 0x4001f000; /* 320x240 */
358 set_par(gspca_dev, value);
359 ret = usb_set_interface(gspca_dev->dev,
360 gspca_dev->iface,
361 gspca_dev->alt);
362 if (ret < 0) {
363 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
364 gspca_dev->iface, gspca_dev->alt);
365 gspca_dev->usb_err = ret;
366 goto out;
368 reg_r(gspca_dev, 0x0630);
369 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
370 reg_r(gspca_dev, 0x0650);
371 snd_val(gspca_dev, 0x000020, 0xffffffff);
372 reg_w(gspca_dev, 0x0620, 0);
373 reg_w(gspca_dev, 0x0630, 0);
374 reg_w(gspca_dev, 0x0640, 0);
375 reg_w(gspca_dev, 0x0650, 0);
376 reg_w(gspca_dev, 0x0660, 0);
377 setbrightness(gspca_dev); /* whiteness */
378 setcontrast(gspca_dev); /* contrast */
379 setcolors(gspca_dev); /* saturation */
380 set_par(gspca_dev, 0x09800000); /* Red ? */
381 set_par(gspca_dev, 0x0a800000); /* Green ? */
382 set_par(gspca_dev, 0x0b800000); /* Blue ? */
383 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
384 setfreq(gspca_dev); /* light frequency */
386 /* start the video flow */
387 set_par(gspca_dev, 0x01000000);
388 set_par(gspca_dev, 0x01000000);
389 if (gspca_dev->usb_err >= 0)
390 PDEBUG(D_STREAM, "camera started alt: 0x%02x",
391 gspca_dev->alt);
392 out:
393 return gspca_dev->usb_err;
396 static void sd_stopN(struct gspca_dev *gspca_dev)
398 struct usb_device *dev = gspca_dev->dev;
400 set_par(gspca_dev, 0x02000000);
401 set_par(gspca_dev, 0x02000000);
402 usb_set_interface(dev, gspca_dev->iface, 1);
403 reg_r(gspca_dev, 0x0630);
404 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
405 reg_r(gspca_dev, 0x0650);
406 snd_val(gspca_dev, 0x000020, 0xffffffff);
407 reg_w(gspca_dev, 0x0620, 0);
408 reg_w(gspca_dev, 0x0630, 0);
409 reg_w(gspca_dev, 0x0640, 0);
410 reg_w(gspca_dev, 0x0650, 0);
411 reg_w(gspca_dev, 0x0660, 0);
412 PDEBUG(D_STREAM, "camera stopped");
415 static void sd_stop0(struct gspca_dev *gspca_dev)
417 struct sd *sd = (struct sd *) gspca_dev;
419 kfree(sd->jpeg_hdr);
422 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
423 u8 *data, /* isoc packet */
424 int len) /* iso packet length */
426 struct sd *sd = (struct sd *) gspca_dev;
427 static unsigned char ffd9[] = {0xff, 0xd9};
429 /* a frame starts with:
430 * - 0xff 0xfe
431 * - 0x08 0x00 - length (little endian ?!)
432 * - 4 bytes = size of whole frame (BE - including header)
433 * - 0x00 0x0c
434 * - 0xff 0xd8
435 * - .. JPEG image with escape sequences (ff 00)
436 * (without ending - ff d9)
438 if (data[0] == 0xff && data[1] == 0xfe) {
439 gspca_frame_add(gspca_dev, LAST_PACKET,
440 ffd9, 2);
442 /* put the JPEG 411 header */
443 gspca_frame_add(gspca_dev, FIRST_PACKET,
444 sd->jpeg_hdr, JPEG_HDR_SZ);
446 /* beginning of the frame */
447 #define STKHDRSZ 12
448 data += STKHDRSZ;
449 len -= STKHDRSZ;
451 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
454 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
456 struct sd *sd = (struct sd *) gspca_dev;
458 sd->brightness = val;
459 if (gspca_dev->streaming)
460 setbrightness(gspca_dev);
461 return gspca_dev->usb_err;
464 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
466 struct sd *sd = (struct sd *) gspca_dev;
468 *val = sd->brightness;
469 return 0;
472 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
474 struct sd *sd = (struct sd *) gspca_dev;
476 sd->contrast = val;
477 if (gspca_dev->streaming)
478 setcontrast(gspca_dev);
479 return gspca_dev->usb_err;
482 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
484 struct sd *sd = (struct sd *) gspca_dev;
486 *val = sd->contrast;
487 return 0;
490 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
492 struct sd *sd = (struct sd *) gspca_dev;
494 sd->colors = val;
495 if (gspca_dev->streaming)
496 setcolors(gspca_dev);
497 return gspca_dev->usb_err;
500 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
502 struct sd *sd = (struct sd *) gspca_dev;
504 *val = sd->colors;
505 return 0;
508 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
510 struct sd *sd = (struct sd *) gspca_dev;
512 sd->lightfreq = val;
513 if (gspca_dev->streaming)
514 setfreq(gspca_dev);
515 return gspca_dev->usb_err;
518 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
520 struct sd *sd = (struct sd *) gspca_dev;
522 *val = sd->lightfreq;
523 return 0;
526 static int sd_querymenu(struct gspca_dev *gspca_dev,
527 struct v4l2_querymenu *menu)
529 switch (menu->id) {
530 case V4L2_CID_POWER_LINE_FREQUENCY:
531 switch (menu->index) {
532 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
533 strcpy((char *) menu->name, "50 Hz");
534 return 0;
535 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
536 strcpy((char *) menu->name, "60 Hz");
537 return 0;
539 break;
541 return -EINVAL;
544 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
545 struct v4l2_jpegcompression *jcomp)
547 struct sd *sd = (struct sd *) gspca_dev;
549 if (jcomp->quality < QUALITY_MIN)
550 sd->quality = QUALITY_MIN;
551 else if (jcomp->quality > QUALITY_MAX)
552 sd->quality = QUALITY_MAX;
553 else
554 sd->quality = jcomp->quality;
555 if (gspca_dev->streaming)
556 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
557 return gspca_dev->usb_err;
560 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
561 struct v4l2_jpegcompression *jcomp)
563 struct sd *sd = (struct sd *) gspca_dev;
565 memset(jcomp, 0, sizeof *jcomp);
566 jcomp->quality = sd->quality;
567 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
568 | V4L2_JPEG_MARKER_DQT;
569 return 0;
572 /* sub-driver description */
573 static const struct sd_desc sd_desc = {
574 .name = MODULE_NAME,
575 .ctrls = sd_ctrls,
576 .nctrls = ARRAY_SIZE(sd_ctrls),
577 .config = sd_config,
578 .init = sd_init,
579 .start = sd_start,
580 .stopN = sd_stopN,
581 .stop0 = sd_stop0,
582 .pkt_scan = sd_pkt_scan,
583 .querymenu = sd_querymenu,
584 .get_jcomp = sd_get_jcomp,
585 .set_jcomp = sd_set_jcomp,
588 /* -- module initialisation -- */
589 static const __devinitdata struct usb_device_id device_table[] = {
590 {USB_DEVICE(0x05e1, 0x0893)},
593 MODULE_DEVICE_TABLE(usb, device_table);
595 /* -- device connect -- */
596 static int sd_probe(struct usb_interface *intf,
597 const struct usb_device_id *id)
599 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
600 THIS_MODULE);
603 static struct usb_driver sd_driver = {
604 .name = MODULE_NAME,
605 .id_table = device_table,
606 .probe = sd_probe,
607 .disconnect = gspca_disconnect,
608 #ifdef CONFIG_PM
609 .suspend = gspca_suspend,
610 .resume = gspca_resume,
611 #endif
614 /* -- module insert / remove -- */
615 static int __init sd_mod_init(void)
617 int ret;
618 ret = usb_register(&sd_driver);
619 if (ret < 0)
620 return ret;
621 info("registered");
622 return 0;
624 static void __exit sd_mod_exit(void)
626 usb_deregister(&sd_driver);
627 info("deregistered");
630 module_init(sd_mod_init);
631 module_exit(sd_mod_exit);