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 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 5)
27 static const char version[] = "2.1.5";
29 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
30 MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
31 MODULE_LICENSE("GPL");
33 /* specific webcam descriptor */
35 struct gspca_dev gspca_dev; /* !! must be the first item */
37 unsigned char brightness;
38 unsigned char contrast;
40 unsigned char lightfreq;
43 /* global parameters */
44 static int sd_quant = 7; /* <= 4 KO - 7: good (enough!) */
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 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,
130 __u16 index, __u8 *buf)
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,
143 PDEBUG(D_ERR, "reg_r err %d", ret);
147 /* -- write a register -- */
148 static int reg_w(struct gspca_dev *gspca_dev,
149 __u16 index, __u16 value)
151 struct usb_device *dev = gspca_dev->dev;
154 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
156 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
163 PDEBUG(D_ERR, "reg_w err %d", ret);
167 /* -- get a value -- */
168 static int rcv_val(struct gspca_dev *gspca_dev,
172 struct usb_device *dev = gspca_dev->dev;
174 unsigned char bulk_buf[4];
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, len & 0xff);
181 reg_w(gspca_dev, 0x639, len >> 8);
182 reg_w(gspca_dev, 0x63a, 0);
183 reg_w(gspca_dev, 0x63b, 0);
184 reg_w(gspca_dev, 0x630, 5);
185 if (len > sizeof bulk_buf)
187 ret = usb_bulk_msg(dev,
188 usb_rcvbulkpipe(dev, 5),
192 500); /* timeout in milliseconds */
196 /* -- send a value -- */
197 static int snd_val(struct gspca_dev *gspca_dev,
201 struct usb_device *dev = gspca_dev->dev;
204 unsigned char bulk_buf[4];
206 if (ads == 0x003f08) {
207 ret = reg_r(gspca_dev, 0x0704, &value);
210 ret = reg_r(gspca_dev, 0x0705, &seq);
213 ret = reg_r(gspca_dev, 0x0650, &value);
216 reg_w(gspca_dev, 0x654, seq);
218 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
219 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
220 reg_w(gspca_dev, 0x656, ads & 0xff);
221 reg_w(gspca_dev, 0x657, 0);
222 reg_w(gspca_dev, 0x658, 0x04); /* size */
223 reg_w(gspca_dev, 0x659, 0);
224 reg_w(gspca_dev, 0x65a, 0);
225 reg_w(gspca_dev, 0x65b, 0);
226 reg_w(gspca_dev, 0x650, 5);
227 bulk_buf[0] = (val >> 24) & 0xff;
228 bulk_buf[1] = (val >> 16) & 0xff;
229 bulk_buf[2] = (val >> 8) & 0xff;
230 bulk_buf[3] = val & 0xff;
231 ret = usb_bulk_msg(dev,
232 usb_sndbulkpipe(dev, 6),
236 500); /* timeout in milliseconds */
239 if (ads == 0x003f08) {
242 reg_w(gspca_dev, 0x705, seq);
246 PDEBUG(D_ERR, "snd_val err %d", ret);
250 /* set a camera parameter */
251 static int set_par(struct gspca_dev *gspca_dev,
254 return snd_val(gspca_dev, 0x003f08, parval);
257 static void setbrightness(struct gspca_dev *gspca_dev)
259 struct sd *sd = (struct sd *) gspca_dev;
262 parval = 0x06000000 /* whiteness */
263 + (sd->brightness << 16);
264 set_par(gspca_dev, parval);
267 static void setcontrast(struct gspca_dev *gspca_dev)
269 struct sd *sd = (struct sd *) gspca_dev;
272 parval = 0x07000000 /* contrast */
273 + (sd->contrast << 16);
274 set_par(gspca_dev, parval);
277 static void setcolors(struct gspca_dev *gspca_dev)
279 struct sd *sd = (struct sd *) gspca_dev;
282 parval = 0x08000000 /* saturation */
283 + (sd->colors << 16);
284 set_par(gspca_dev, parval);
287 static void setfreq(struct gspca_dev *gspca_dev)
289 struct sd *sd = (struct sd *) gspca_dev;
291 set_par(gspca_dev, sd->lightfreq == 1
292 ? 0x33640000 /* 50 Hz */
293 : 0x33780000); /* 60 Hz */
296 /* this function is called at probe time */
297 static int sd_config(struct gspca_dev *gspca_dev,
298 const struct usb_device_id *id)
300 struct sd *sd = (struct sd *) gspca_dev;
301 struct cam *cam = &gspca_dev->cam;
303 cam->dev_name = (char *) id->driver_info;
305 gspca_dev->cam.cam_mode = vga_mode;
306 gspca_dev->cam.nmodes = sizeof vga_mode / sizeof vga_mode[0];
307 sd->brightness = BRIGHTNESS_DEF;
308 sd->contrast = CONTRAST_DEF;
309 sd->colors = COLOR_DEF;
310 sd->lightfreq = FREQ_DEF;
314 /* this function is called at open time */
315 static int sd_open(struct gspca_dev *gspca_dev)
320 /* check if the device responds */
321 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
322 ret = reg_r(gspca_dev, 0x0740, &value);
326 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", value);
332 /* -- start the camera -- */
333 static void sd_start(struct gspca_dev *gspca_dev)
338 /* work on alternate 1 */
339 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
341 set_par(gspca_dev, 0x10000000);
342 set_par(gspca_dev, 0x00000000);
343 set_par(gspca_dev, 0x8002e001);
344 set_par(gspca_dev, 0x14000000);
345 if (gspca_dev->width > 320)
346 value = 0x8002e001; /* 640x480 */
348 value = 0x4001f000; /* 320x240 */
349 set_par(gspca_dev, value);
350 ret = usb_set_interface(gspca_dev->dev,
354 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
355 gspca_dev->iface, gspca_dev->alt);
358 ret = reg_r(gspca_dev, 0x0630, &dum);
361 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
362 ret = reg_r(gspca_dev, 0x0650, &dum);
365 snd_val(gspca_dev, 0x000020, 0xffffffff);
366 reg_w(gspca_dev, 0x0620, 0);
367 reg_w(gspca_dev, 0x0630, 0);
368 reg_w(gspca_dev, 0x0640, 0);
369 reg_w(gspca_dev, 0x0650, 0);
370 reg_w(gspca_dev, 0x0660, 0);
371 setbrightness(gspca_dev); /* whiteness */
372 setcontrast(gspca_dev); /* contrast */
373 setcolors(gspca_dev); /* saturation */
374 set_par(gspca_dev, 0x09800000); /* Red ? */
375 set_par(gspca_dev, 0x0a800000); /* Green ? */
376 set_par(gspca_dev, 0x0b800000); /* Blue ? */
377 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
378 setfreq(gspca_dev); /* light frequency */
380 /* start the video flow */
381 set_par(gspca_dev, 0x01000000);
382 set_par(gspca_dev, 0x01000000);
383 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
386 PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
389 static void sd_stopN(struct gspca_dev *gspca_dev)
391 struct usb_device *dev = gspca_dev->dev;
394 set_par(gspca_dev, 0x02000000);
395 set_par(gspca_dev, 0x02000000);
396 usb_set_interface(dev, gspca_dev->iface, 1);
397 reg_r(gspca_dev, 0x0630, &value);
398 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
399 reg_r(gspca_dev, 0x0650, &value);
400 snd_val(gspca_dev, 0x000020, 0xffffffff);
401 reg_w(gspca_dev, 0x0620, 0);
402 reg_w(gspca_dev, 0x0630, 0);
403 reg_w(gspca_dev, 0x0640, 0);
404 reg_w(gspca_dev, 0x0650, 0);
405 reg_w(gspca_dev, 0x0660, 0);
406 PDEBUG(D_STREAM, "camera stopped");
409 static void sd_stop0(struct gspca_dev *gspca_dev)
413 static void sd_close(struct gspca_dev *gspca_dev)
417 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
418 struct gspca_frame *frame, /* target */
419 __u8 *data, /* isoc packet */
420 int len) /* iso packet length */
422 static unsigned char ffd9[] = {0xff, 0xd9};
424 /* a frame starts with:
426 * - 0x08 0x00 - length (little endian ?!)
427 * - 4 bytes = size of whole frame (BE - including header)
430 * - .. JPEG image with escape sequences (ff 00)
431 * (without ending - ff d9)
433 if (data[0] == 0xff && data[1] == 0xfe) {
434 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
437 /* put the JPEG 411 header */
438 jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
440 /* beginning of the frame */
442 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
443 data + STKHDRSZ, len - STKHDRSZ);
447 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
450 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
452 struct sd *sd = (struct sd *) gspca_dev;
454 sd->brightness = val;
455 if (gspca_dev->streaming)
456 setbrightness(gspca_dev);
460 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
462 struct sd *sd = (struct sd *) gspca_dev;
464 *val = sd->brightness;
468 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
470 struct sd *sd = (struct sd *) gspca_dev;
473 if (gspca_dev->streaming)
474 setcontrast(gspca_dev);
478 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
480 struct sd *sd = (struct sd *) gspca_dev;
486 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
488 struct sd *sd = (struct sd *) gspca_dev;
491 if (gspca_dev->streaming)
492 setcolors(gspca_dev);
496 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
498 struct sd *sd = (struct sd *) gspca_dev;
504 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
506 struct sd *sd = (struct sd *) gspca_dev;
509 if (gspca_dev->streaming)
514 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
516 struct sd *sd = (struct sd *) gspca_dev;
518 *val = sd->lightfreq;
522 static int sd_querymenu(struct gspca_dev *gspca_dev,
523 struct v4l2_querymenu *menu)
526 case V4L2_CID_POWER_LINE_FREQUENCY:
527 switch (menu->index) {
528 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
529 strcpy((char *) menu->name, "50 Hz");
531 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
532 strcpy((char *) menu->name, "60 Hz");
540 /* sub-driver description */
541 static struct sd_desc sd_desc = {
544 .nctrls = sizeof sd_ctrls / sizeof sd_ctrls[0],
551 .pkt_scan = sd_pkt_scan,
552 .querymenu = sd_querymenu,
555 /* -- module initialisation -- */
556 #define DVNM(name) .driver_info = (kernel_ulong_t) name
557 static __devinitdata struct usb_device_id device_table[] = {
558 {USB_DEVICE(0x05e1, 0x0893), DVNM("Syntek DV4000")},
561 MODULE_DEVICE_TABLE(usb, device_table);
563 /* -- device connect -- */
564 static int sd_probe(struct usb_interface *intf,
565 const struct usb_device_id *id)
567 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
571 static struct usb_driver sd_driver = {
573 .id_table = device_table,
575 .disconnect = gspca_disconnect,
578 /* -- module insert / remove -- */
579 static int __init sd_mod_init(void)
581 if (usb_register(&sd_driver) < 0)
583 info("v%s registered", version);
586 static void __exit sd_mod_exit(void)
588 usb_deregister(&sd_driver);
589 info("deregistered");
592 module_init(sd_mod_init);
593 module_exit(sd_mod_exit);
595 module_param_named(quant, sd_quant, int, 0644);
596 MODULE_PARM_DESC(quant, "Quantization index (0..8)");