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, 4)
27 static const char version[] = "2.1.4";
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 cam_mode vga_mode[] = {
116 {V4L2_PIX_FMT_JPEG, 320, 240},
117 {V4L2_PIX_FMT_JPEG, 640, 480},
120 /* -- read a register -- */
121 static int reg_r(struct gspca_dev *gspca_dev,
122 __u16 index, __u8 *buf)
124 struct usb_device *dev = gspca_dev->dev;
127 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
129 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
135 PDEBUG(D_ERR, "reg_r err %d", ret);
139 /* -- write a register -- */
140 static int reg_w(struct gspca_dev *gspca_dev,
141 __u16 index, __u16 value)
143 struct usb_device *dev = gspca_dev->dev;
146 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
155 PDEBUG(D_ERR, "reg_w err %d", ret);
159 /* -- get a value -- */
160 static int rcv_val(struct gspca_dev *gspca_dev,
164 struct usb_device *dev = gspca_dev->dev;
166 unsigned char bulk_buf[4];
168 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
169 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
170 reg_w(gspca_dev, 0x636, ads & 0xff);
171 reg_w(gspca_dev, 0x637, 0);
172 reg_w(gspca_dev, 0x638, len & 0xff);
173 reg_w(gspca_dev, 0x639, len >> 8);
174 reg_w(gspca_dev, 0x63a, 0);
175 reg_w(gspca_dev, 0x63b, 0);
176 reg_w(gspca_dev, 0x630, 5);
177 if (len > sizeof bulk_buf)
179 ret = usb_bulk_msg(dev,
180 usb_rcvbulkpipe(dev, 5),
184 500); /* timeout in milliseconds */
188 /* -- send a value -- */
189 static int snd_val(struct gspca_dev *gspca_dev,
193 struct usb_device *dev = gspca_dev->dev;
196 unsigned char bulk_buf[4];
198 if (ads == 0x003f08) {
199 ret = reg_r(gspca_dev, 0x0704, &value);
202 ret = reg_r(gspca_dev, 0x0705, &seq);
205 ret = reg_r(gspca_dev, 0x0650, &value);
208 reg_w(gspca_dev, 0x654, seq);
210 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
211 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
212 reg_w(gspca_dev, 0x656, ads & 0xff);
213 reg_w(gspca_dev, 0x657, 0);
214 reg_w(gspca_dev, 0x658, 0x04); /* size */
215 reg_w(gspca_dev, 0x659, 0);
216 reg_w(gspca_dev, 0x65a, 0);
217 reg_w(gspca_dev, 0x65b, 0);
218 reg_w(gspca_dev, 0x650, 5);
219 bulk_buf[0] = (val >> 24) & 0xff;
220 bulk_buf[1] = (val >> 16) & 0xff;
221 bulk_buf[2] = (val >> 8) & 0xff;
222 bulk_buf[3] = val & 0xff;
223 ret = usb_bulk_msg(dev,
224 usb_sndbulkpipe(dev, 6),
228 500); /* timeout in milliseconds */
231 if (ads == 0x003f08) {
234 reg_w(gspca_dev, 0x705, seq);
238 PDEBUG(D_ERR, "snd_val err %d", ret);
242 /* set a camera parameter */
243 static int set_par(struct gspca_dev *gspca_dev,
246 return snd_val(gspca_dev, 0x003f08, parval);
249 static void setbrightness(struct gspca_dev *gspca_dev)
251 struct sd *sd = (struct sd *) gspca_dev;
254 parval = 0x06000000 /* whiteness */
255 + (sd->brightness << 16);
256 set_par(gspca_dev, parval);
259 static void setcontrast(struct gspca_dev *gspca_dev)
261 struct sd *sd = (struct sd *) gspca_dev;
264 parval = 0x07000000 /* contrast */
265 + (sd->contrast << 16);
266 set_par(gspca_dev, parval);
269 static void setcolors(struct gspca_dev *gspca_dev)
271 struct sd *sd = (struct sd *) gspca_dev;
274 parval = 0x08000000 /* saturation */
275 + (sd->colors << 16);
276 set_par(gspca_dev, parval);
279 static void setfreq(struct gspca_dev *gspca_dev)
281 struct sd *sd = (struct sd *) gspca_dev;
283 set_par(gspca_dev, sd->lightfreq == 1
284 ? 0x33640000 /* 50 Hz */
285 : 0x33780000); /* 60 Hz */
288 /* this function is called at probe time */
289 static int sd_config(struct gspca_dev *gspca_dev,
290 const struct usb_device_id *id)
292 struct sd *sd = (struct sd *) gspca_dev;
293 struct cam *cam = &gspca_dev->cam;
295 cam->dev_name = (char *) id->driver_info;
297 gspca_dev->cam.cam_mode = vga_mode;
298 gspca_dev->cam.nmodes = sizeof vga_mode / sizeof vga_mode[0];
299 sd->brightness = BRIGHTNESS_DEF;
300 sd->contrast = CONTRAST_DEF;
301 sd->colors = COLOR_DEF;
302 sd->lightfreq = FREQ_DEF;
306 /* this function is called at open time */
307 static int sd_open(struct gspca_dev *gspca_dev)
312 /* check if the device responds */
313 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
314 ret = reg_r(gspca_dev, 0x0740, &value);
318 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", value);
324 /* -- start the camera -- */
325 static void sd_start(struct gspca_dev *gspca_dev)
330 /* work on alternate 1 */
331 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
333 set_par(gspca_dev, 0x10000000);
334 set_par(gspca_dev, 0x00000000);
335 set_par(gspca_dev, 0x8002e001);
336 set_par(gspca_dev, 0x14000000);
337 if (gspca_dev->width > 320)
338 value = 0x8002e001; /* 640x480 */
340 value = 0x4001f000; /* 320x240 */
341 set_par(gspca_dev, value);
342 ret = usb_set_interface(gspca_dev->dev,
346 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
347 gspca_dev->iface, gspca_dev->alt);
350 ret = reg_r(gspca_dev, 0x0630, &dum);
353 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
354 ret = reg_r(gspca_dev, 0x0650, &dum);
357 snd_val(gspca_dev, 0x000020, 0xffffffff);
358 reg_w(gspca_dev, 0x0620, 0);
359 reg_w(gspca_dev, 0x0630, 0);
360 reg_w(gspca_dev, 0x0640, 0);
361 reg_w(gspca_dev, 0x0650, 0);
362 reg_w(gspca_dev, 0x0660, 0);
363 setbrightness(gspca_dev); /* whiteness */
364 setcontrast(gspca_dev); /* contrast */
365 setcolors(gspca_dev); /* saturation */
366 set_par(gspca_dev, 0x09800000); /* Red ? */
367 set_par(gspca_dev, 0x0a800000); /* Green ? */
368 set_par(gspca_dev, 0x0b800000); /* Blue ? */
369 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
370 setfreq(gspca_dev); /* light frequency */
372 /* start the video flow */
373 set_par(gspca_dev, 0x01000000);
374 set_par(gspca_dev, 0x01000000);
375 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
378 PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
381 static void sd_stopN(struct gspca_dev *gspca_dev)
383 struct usb_device *dev = gspca_dev->dev;
386 set_par(gspca_dev, 0x02000000);
387 set_par(gspca_dev, 0x02000000);
388 usb_set_interface(dev, gspca_dev->iface, 1);
389 reg_r(gspca_dev, 0x0630, &value);
390 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
391 reg_r(gspca_dev, 0x0650, &value);
392 snd_val(gspca_dev, 0x000020, 0xffffffff);
393 reg_w(gspca_dev, 0x0620, 0);
394 reg_w(gspca_dev, 0x0630, 0);
395 reg_w(gspca_dev, 0x0640, 0);
396 reg_w(gspca_dev, 0x0650, 0);
397 reg_w(gspca_dev, 0x0660, 0);
398 PDEBUG(D_STREAM, "camera stopped");
401 static void sd_stop0(struct gspca_dev *gspca_dev)
405 static void sd_close(struct gspca_dev *gspca_dev)
409 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
410 struct gspca_frame *frame, /* target */
411 __u8 *data, /* isoc packet */
412 int len) /* iso packet length */
414 static unsigned char ffd9[] = {0xff, 0xd9};
416 /* a frame starts with:
418 * - 0x08 0x00 - length (little endian ?!)
419 * - 4 bytes = size of whole frame (BE - including header)
422 * - .. JPEG image with escape sequences (ff 00)
423 * (without ending - ff d9)
425 if (data[0] == 0xff && data[1] == 0xfe) {
426 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
429 /* put the JPEG 411 header */
430 jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
432 /* beginning of the frame */
434 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
435 data + STKHDRSZ, len - STKHDRSZ);
439 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
442 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
444 struct sd *sd = (struct sd *) gspca_dev;
446 sd->brightness = val;
447 if (gspca_dev->streaming)
448 setbrightness(gspca_dev);
452 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
454 struct sd *sd = (struct sd *) gspca_dev;
456 *val = sd->brightness;
460 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
462 struct sd *sd = (struct sd *) gspca_dev;
465 if (gspca_dev->streaming)
466 setcontrast(gspca_dev);
470 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
472 struct sd *sd = (struct sd *) gspca_dev;
478 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
480 struct sd *sd = (struct sd *) gspca_dev;
483 if (gspca_dev->streaming)
484 setcolors(gspca_dev);
488 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
490 struct sd *sd = (struct sd *) gspca_dev;
496 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
498 struct sd *sd = (struct sd *) gspca_dev;
501 if (gspca_dev->streaming)
506 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
508 struct sd *sd = (struct sd *) gspca_dev;
510 *val = sd->lightfreq;
514 static int sd_querymenu(struct gspca_dev *gspca_dev,
515 struct v4l2_querymenu *menu)
518 case V4L2_CID_POWER_LINE_FREQUENCY:
519 switch (menu->index) {
520 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
521 strcpy((char *) menu->name, "50 Hz");
523 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
524 strcpy((char *) menu->name, "60 Hz");
532 /* sub-driver description */
533 static struct sd_desc sd_desc = {
536 .nctrls = sizeof sd_ctrls / sizeof sd_ctrls[0],
543 .pkt_scan = sd_pkt_scan,
544 .querymenu = sd_querymenu,
547 /* -- module initialisation -- */
548 #define DVNM(name) .driver_info = (kernel_ulong_t) name
549 static __devinitdata struct usb_device_id device_table[] = {
550 {USB_DEVICE(0x05e1, 0x0893), DVNM("Syntek DV4000")},
553 MODULE_DEVICE_TABLE(usb, device_table);
555 /* -- device connect -- */
556 static int sd_probe(struct usb_interface *intf,
557 const struct usb_device_id *id)
559 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
563 static struct usb_driver sd_driver = {
565 .id_table = device_table,
567 .disconnect = gspca_disconnect,
570 /* -- module insert / remove -- */
571 static int __init sd_mod_init(void)
573 if (usb_register(&sd_driver) < 0)
575 info("v%s registered", version);
578 static void __exit sd_mod_exit(void)
580 usb_deregister(&sd_driver);
581 info("deregistered");
584 module_init(sd_mod_init);
585 module_exit(sd_mod_exit);
587 module_param_named(quant, sd_quant, int, 0644);
588 MODULE_PARM_DESC(quant, "Quantization index (0..8)");