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 int reg_r(struct gspca_dev *gspca_dev,
132 struct usb_device *dev = gspca_dev->dev;
135 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
137 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
140 gspca_dev->usb_buf, 1,
143 PDEBUG(D_ERR, "reg_r err %d", ret);
146 return gspca_dev->usb_buf[0];
149 /* -- write a register -- */
150 static int reg_w(struct gspca_dev *gspca_dev,
151 __u16 index, __u16 value)
153 struct usb_device *dev = gspca_dev->dev;
156 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
158 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
165 PDEBUG(D_ERR, "reg_w err %d", ret);
169 /* -- get a bulk value (4 bytes) -- */
170 static int rcv_val(struct gspca_dev *gspca_dev,
173 struct usb_device *dev = gspca_dev->dev;
176 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
177 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
178 reg_w(gspca_dev, 0x636, ads & 0xff);
179 reg_w(gspca_dev, 0x637, 0);
180 reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
181 reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
182 reg_w(gspca_dev, 0x63a, 0);
183 reg_w(gspca_dev, 0x63b, 0);
184 reg_w(gspca_dev, 0x630, 5);
185 ret = usb_bulk_msg(dev,
186 usb_rcvbulkpipe(dev, 0x05),
190 500); /* timeout in milliseconds */
194 /* -- send a bulk value -- */
195 static int snd_val(struct gspca_dev *gspca_dev,
199 struct usb_device *dev = gspca_dev->dev;
203 if (ads == 0x003f08) {
204 ret = reg_r(gspca_dev, 0x0704);
207 ret = reg_r(gspca_dev, 0x0705);
210 seq = ret; /* keep the sequence number */
211 ret = reg_r(gspca_dev, 0x0650);
214 reg_w(gspca_dev, 0x654, seq);
216 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
218 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
219 reg_w(gspca_dev, 0x656, ads & 0xff);
220 reg_w(gspca_dev, 0x657, 0);
221 reg_w(gspca_dev, 0x658, 0x04); /* size */
222 reg_w(gspca_dev, 0x659, 0);
223 reg_w(gspca_dev, 0x65a, 0);
224 reg_w(gspca_dev, 0x65b, 0);
225 reg_w(gspca_dev, 0x650, 5);
226 gspca_dev->usb_buf[0] = val >> 24;
227 gspca_dev->usb_buf[1] = val >> 16;
228 gspca_dev->usb_buf[2] = val >> 8;
229 gspca_dev->usb_buf[3] = val;
230 ret = usb_bulk_msg(dev,
231 usb_sndbulkpipe(dev, 6),
235 500); /* timeout in milliseconds */
238 if (ads == 0x003f08) {
241 reg_w(gspca_dev, 0x705, seq);
245 PDEBUG(D_ERR, "snd_val err %d", ret);
249 /* set a camera parameter */
250 static int set_par(struct gspca_dev *gspca_dev,
253 return snd_val(gspca_dev, 0x003f08, parval);
256 static void setbrightness(struct gspca_dev *gspca_dev)
258 struct sd *sd = (struct sd *) gspca_dev;
261 parval = 0x06000000 /* whiteness */
262 + (sd->brightness << 16);
263 set_par(gspca_dev, parval);
266 static void setcontrast(struct gspca_dev *gspca_dev)
268 struct sd *sd = (struct sd *) gspca_dev;
271 parval = 0x07000000 /* contrast */
272 + (sd->contrast << 16);
273 set_par(gspca_dev, parval);
276 static void setcolors(struct gspca_dev *gspca_dev)
278 struct sd *sd = (struct sd *) gspca_dev;
281 parval = 0x08000000 /* saturation */
282 + (sd->colors << 16);
283 set_par(gspca_dev, parval);
286 static void setfreq(struct gspca_dev *gspca_dev)
288 struct sd *sd = (struct sd *) gspca_dev;
290 set_par(gspca_dev, sd->lightfreq == 1
291 ? 0x33640000 /* 50 Hz */
292 : 0x33780000); /* 60 Hz */
295 /* this function is called at probe time */
296 static int sd_config(struct gspca_dev *gspca_dev,
297 const struct usb_device_id *id)
299 struct sd *sd = (struct sd *) gspca_dev;
301 gspca_dev->cam.cam_mode = vga_mode;
302 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
303 sd->brightness = BRIGHTNESS_DEF;
304 sd->contrast = CONTRAST_DEF;
305 sd->colors = COLOR_DEF;
306 sd->lightfreq = FREQ_DEF;
307 sd->quality = QUALITY_DEF;
311 /* this function is called at probe and resume time */
312 static int sd_init(struct gspca_dev *gspca_dev)
316 /* check if the device responds */
317 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
318 ret = reg_r(gspca_dev, 0x0740);
322 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
328 /* -- start the camera -- */
329 static int sd_start(struct gspca_dev *gspca_dev)
331 struct sd *sd = (struct sd *) gspca_dev;
334 /* create the JPEG header */
335 sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
336 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
337 0x22); /* JPEG 411 */
338 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
340 /* work on alternate 1 */
341 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
343 set_par(gspca_dev, 0x10000000);
344 set_par(gspca_dev, 0x00000000);
345 set_par(gspca_dev, 0x8002e001);
346 set_par(gspca_dev, 0x14000000);
347 if (gspca_dev->width > 320)
348 value = 0x8002e001; /* 640x480 */
350 value = 0x4001f000; /* 320x240 */
351 set_par(gspca_dev, value);
352 ret = usb_set_interface(gspca_dev->dev,
356 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
357 gspca_dev->iface, gspca_dev->alt);
360 ret = reg_r(gspca_dev, 0x0630);
363 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
364 ret = reg_r(gspca_dev, 0x0650);
367 snd_val(gspca_dev, 0x000020, 0xffffffff);
368 reg_w(gspca_dev, 0x0620, 0);
369 reg_w(gspca_dev, 0x0630, 0);
370 reg_w(gspca_dev, 0x0640, 0);
371 reg_w(gspca_dev, 0x0650, 0);
372 reg_w(gspca_dev, 0x0660, 0);
373 setbrightness(gspca_dev); /* whiteness */
374 setcontrast(gspca_dev); /* contrast */
375 setcolors(gspca_dev); /* saturation */
376 set_par(gspca_dev, 0x09800000); /* Red ? */
377 set_par(gspca_dev, 0x0a800000); /* Green ? */
378 set_par(gspca_dev, 0x0b800000); /* Blue ? */
379 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
380 setfreq(gspca_dev); /* light frequency */
382 /* start the video flow */
383 set_par(gspca_dev, 0x01000000);
384 set_par(gspca_dev, 0x01000000);
385 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
388 PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
392 static void sd_stopN(struct gspca_dev *gspca_dev)
394 struct usb_device *dev = gspca_dev->dev;
396 set_par(gspca_dev, 0x02000000);
397 set_par(gspca_dev, 0x02000000);
398 usb_set_interface(dev, gspca_dev->iface, 1);
399 reg_r(gspca_dev, 0x0630);
400 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
401 reg_r(gspca_dev, 0x0650);
402 snd_val(gspca_dev, 0x000020, 0xffffffff);
403 reg_w(gspca_dev, 0x0620, 0);
404 reg_w(gspca_dev, 0x0630, 0);
405 reg_w(gspca_dev, 0x0640, 0);
406 reg_w(gspca_dev, 0x0650, 0);
407 reg_w(gspca_dev, 0x0660, 0);
408 PDEBUG(D_STREAM, "camera stopped");
411 static void sd_stop0(struct gspca_dev *gspca_dev)
413 struct sd *sd = (struct sd *) gspca_dev;
418 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
419 struct gspca_frame *frame, /* target */
420 __u8 *data, /* isoc packet */
421 int len) /* iso packet length */
423 struct sd *sd = (struct sd *) gspca_dev;
424 static unsigned char ffd9[] = {0xff, 0xd9};
426 /* a frame starts with:
428 * - 0x08 0x00 - length (little endian ?!)
429 * - 4 bytes = size of whole frame (BE - including header)
432 * - .. JPEG image with escape sequences (ff 00)
433 * (without ending - ff d9)
435 if (data[0] == 0xff && data[1] == 0xfe) {
436 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
439 /* put the JPEG 411 header */
440 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
441 sd->jpeg_hdr, JPEG_HDR_SZ);
443 /* beginning of the frame */
448 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
451 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
453 struct sd *sd = (struct sd *) gspca_dev;
455 sd->brightness = val;
456 if (gspca_dev->streaming)
457 setbrightness(gspca_dev);
461 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
463 struct sd *sd = (struct sd *) gspca_dev;
465 *val = sd->brightness;
469 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
471 struct sd *sd = (struct sd *) gspca_dev;
474 if (gspca_dev->streaming)
475 setcontrast(gspca_dev);
479 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
481 struct sd *sd = (struct sd *) gspca_dev;
487 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
489 struct sd *sd = (struct sd *) gspca_dev;
492 if (gspca_dev->streaming)
493 setcolors(gspca_dev);
497 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
499 struct sd *sd = (struct sd *) gspca_dev;
505 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
507 struct sd *sd = (struct sd *) gspca_dev;
510 if (gspca_dev->streaming)
515 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
517 struct sd *sd = (struct sd *) gspca_dev;
519 *val = sd->lightfreq;
523 static int sd_querymenu(struct gspca_dev *gspca_dev,
524 struct v4l2_querymenu *menu)
527 case V4L2_CID_POWER_LINE_FREQUENCY:
528 switch (menu->index) {
529 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
530 strcpy((char *) menu->name, "50 Hz");
532 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
533 strcpy((char *) menu->name, "60 Hz");
541 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
542 struct v4l2_jpegcompression *jcomp)
544 struct sd *sd = (struct sd *) gspca_dev;
546 if (jcomp->quality < QUALITY_MIN)
547 sd->quality = QUALITY_MIN;
548 else if (jcomp->quality > QUALITY_MAX)
549 sd->quality = QUALITY_MAX;
551 sd->quality = jcomp->quality;
552 if (gspca_dev->streaming)
553 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
557 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
558 struct v4l2_jpegcompression *jcomp)
560 struct sd *sd = (struct sd *) gspca_dev;
562 memset(jcomp, 0, sizeof *jcomp);
563 jcomp->quality = sd->quality;
564 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
565 | V4L2_JPEG_MARKER_DQT;
569 /* sub-driver description */
570 static const struct sd_desc sd_desc = {
573 .nctrls = ARRAY_SIZE(sd_ctrls),
579 .pkt_scan = sd_pkt_scan,
580 .querymenu = sd_querymenu,
581 .get_jcomp = sd_get_jcomp,
582 .set_jcomp = sd_set_jcomp,
585 /* -- module initialisation -- */
586 static const __devinitdata struct usb_device_id device_table[] = {
587 {USB_DEVICE(0x05e1, 0x0893)},
590 MODULE_DEVICE_TABLE(usb, device_table);
592 /* -- device connect -- */
593 static int sd_probe(struct usb_interface *intf,
594 const struct usb_device_id *id)
596 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
600 static struct usb_driver sd_driver = {
602 .id_table = device_table,
604 .disconnect = gspca_disconnect,
606 .suspend = gspca_suspend,
607 .resume = gspca_resume,
611 /* -- module insert / remove -- */
612 static int __init sd_mod_init(void)
615 ret = usb_register(&sd_driver);
621 static void __exit sd_mod_exit(void)
623 usb_deregister(&sd_driver);
624 info("deregistered");
627 module_init(sd_mod_init);
628 module_exit(sd_mod_exit);