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
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"
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 */
32 struct gspca_dev gspca_dev; /* !! must be the first item */
34 unsigned char brightness;
35 unsigned char contrast;
37 unsigned char lightfreq;
39 #define QUALITY_MIN 60
40 #define QUALITY_MAX 95
41 #define QUALITY_DEF 80
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 struct ctrl sd_ctrls[] = {
59 .id = V4L2_CID_BRIGHTNESS,
60 .type = V4L2_CTRL_TYPE_INTEGER,
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,
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,
94 .default_value = COLOR_DEF,
101 .id = V4L2_CID_POWER_LINE_FREQUENCY,
102 .type = V4L2_CTRL_TYPE_MENU,
103 .name = "Light frequency filter",
105 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
108 .default_value = FREQ_DEF,
115 static const struct v4l2_pix_format vga_mode[] = {
116 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
118 .sizeimage = 320 * 240 * 3 / 8 + 590,
119 .colorspace = V4L2_COLORSPACE_JPEG,
121 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
123 .sizeimage = 640 * 480 * 3 / 8 + 590,
124 .colorspace = V4L2_COLORSPACE_JPEG,
128 /* -- read a register -- */
129 static u8 reg_r(struct gspca_dev *gspca_dev,
132 struct usb_device *dev = gspca_dev->dev;
135 if (gspca_dev->usb_err < 0)
137 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
139 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
142 gspca_dev->usb_buf, 1,
145 PDEBUG(D_ERR, "reg_r err %d", ret);
146 gspca_dev->usb_err = ret;
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;
159 if (gspca_dev->usb_err < 0)
161 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
163 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
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,
179 struct usb_device *dev = gspca_dev->dev;
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)
193 ret = usb_bulk_msg(dev,
194 usb_rcvbulkpipe(dev, 0x05),
198 500); /* timeout in milliseconds */
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,
210 struct usb_device *dev = gspca_dev->dev;
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);
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)
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),
241 500); /* timeout in milliseconds */
243 PDEBUG(D_ERR, "snd_val err %d", ret);
244 gspca_dev->usb_err = ret;
246 if (ads == 0x003f08) {
249 reg_w(gspca_dev, 0x705, seq);
254 /* set a camera parameter */
255 static void set_par(struct gspca_dev *gspca_dev,
258 snd_val(gspca_dev, 0x003f08, parval);
261 static void setbrightness(struct gspca_dev *gspca_dev)
263 struct sd *sd = (struct sd *) gspca_dev;
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;
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;
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;
316 /* this function is called at probe and resume time */
317 static int sd_init(struct gspca_dev *gspca_dev)
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) {
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;
339 /* create the JPEG header */
340 sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
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 */
357 value = 0x4001f000; /* 320x240 */
358 set_par(gspca_dev, value);
359 ret = usb_set_interface(gspca_dev->dev,
363 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
364 gspca_dev->iface, gspca_dev->alt);
365 gspca_dev->usb_err = ret;
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",
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;
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:
431 * - 0x08 0x00 - length (little endian ?!)
432 * - 4 bytes = size of whole frame (BE - including header)
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,
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 */
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;
472 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
474 struct sd *sd = (struct sd *) gspca_dev;
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;
490 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
492 struct sd *sd = (struct sd *) gspca_dev;
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;
508 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
510 struct sd *sd = (struct sd *) gspca_dev;
513 if (gspca_dev->streaming)
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;
526 static int sd_querymenu(struct gspca_dev *gspca_dev,
527 struct v4l2_querymenu *menu)
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");
535 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
536 strcpy((char *) menu->name, "60 Hz");
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;
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;
572 /* sub-driver description */
573 static const struct sd_desc sd_desc = {
576 .nctrls = ARRAY_SIZE(sd_ctrls),
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),
603 static struct usb_driver sd_driver = {
605 .id_table = device_table,
607 .disconnect = gspca_disconnect,
609 .suspend = gspca_suspend,
610 .resume = gspca_resume,
614 /* -- module insert / remove -- */
615 static int __init sd_mod_init(void)
618 ret = usb_register(&sd_driver);
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);