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;
40 /* global parameters */
41 static int sd_quant = 7; /* <= 4 KO - 7: good (enough!) */
43 /* V4L2 controls supported by the driver */
44 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
45 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
46 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
47 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
48 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
49 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
50 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
51 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
53 static struct ctrl sd_ctrls[] = {
56 .id = V4L2_CID_BRIGHTNESS,
57 .type = V4L2_CTRL_TYPE_INTEGER,
62 #define BRIGHTNESS_DEF 127
63 .default_value = BRIGHTNESS_DEF,
65 .set = sd_setbrightness,
66 .get = sd_getbrightness,
70 .id = V4L2_CID_CONTRAST,
71 .type = V4L2_CTRL_TYPE_INTEGER,
76 #define CONTRAST_DEF 127
77 .default_value = CONTRAST_DEF,
79 .set = sd_setcontrast,
80 .get = sd_getcontrast,
84 .id = V4L2_CID_SATURATION,
85 .type = V4L2_CTRL_TYPE_INTEGER,
91 .default_value = COLOR_DEF,
98 .id = V4L2_CID_POWER_LINE_FREQUENCY,
99 .type = V4L2_CTRL_TYPE_MENU,
100 .name = "Light frequency filter",
102 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
105 .default_value = FREQ_DEF,
112 static struct v4l2_pix_format vga_mode[] = {
113 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
115 .sizeimage = 320 * 240 * 3 / 8 + 590,
116 .colorspace = V4L2_COLORSPACE_JPEG,
118 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
120 .sizeimage = 640 * 480 * 3 / 8 + 590,
121 .colorspace = V4L2_COLORSPACE_JPEG,
125 /* -- read a register -- */
126 static int reg_r(struct gspca_dev *gspca_dev,
129 struct usb_device *dev = gspca_dev->dev;
132 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
134 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
137 gspca_dev->usb_buf, 1,
140 PDEBUG(D_ERR, "reg_r err %d", ret);
143 return gspca_dev->usb_buf[0];
146 /* -- write a register -- */
147 static int reg_w(struct gspca_dev *gspca_dev,
148 __u16 index, __u16 value)
150 struct usb_device *dev = gspca_dev->dev;
153 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
155 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
162 PDEBUG(D_ERR, "reg_w err %d", ret);
166 /* -- get a bulk value (4 bytes) -- */
167 static int rcv_val(struct gspca_dev *gspca_dev,
170 struct usb_device *dev = gspca_dev->dev;
173 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
174 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
175 reg_w(gspca_dev, 0x636, ads & 0xff);
176 reg_w(gspca_dev, 0x637, 0);
177 reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
178 reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
179 reg_w(gspca_dev, 0x63a, 0);
180 reg_w(gspca_dev, 0x63b, 0);
181 reg_w(gspca_dev, 0x630, 5);
182 ret = usb_bulk_msg(dev,
183 usb_rcvbulkpipe(dev, 5),
187 500); /* timeout in milliseconds */
191 /* -- send a bulk value -- */
192 static int snd_val(struct gspca_dev *gspca_dev,
196 struct usb_device *dev = gspca_dev->dev;
200 if (ads == 0x003f08) {
201 ret = reg_r(gspca_dev, 0x0704);
204 ret = reg_r(gspca_dev, 0x0705);
207 seq = ret; /* keep the sequence number */
208 ret = reg_r(gspca_dev, 0x0650);
211 reg_w(gspca_dev, 0x654, seq);
213 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
215 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
216 reg_w(gspca_dev, 0x656, ads & 0xff);
217 reg_w(gspca_dev, 0x657, 0);
218 reg_w(gspca_dev, 0x658, 0x04); /* size */
219 reg_w(gspca_dev, 0x659, 0);
220 reg_w(gspca_dev, 0x65a, 0);
221 reg_w(gspca_dev, 0x65b, 0);
222 reg_w(gspca_dev, 0x650, 5);
223 gspca_dev->usb_buf[0] = val >> 24;
224 gspca_dev->usb_buf[1] = val >> 16;
225 gspca_dev->usb_buf[2] = val >> 8;
226 gspca_dev->usb_buf[3] = val;
227 ret = usb_bulk_msg(dev,
228 usb_sndbulkpipe(dev, 6),
232 500); /* timeout in milliseconds */
235 if (ads == 0x003f08) {
238 reg_w(gspca_dev, 0x705, seq);
242 PDEBUG(D_ERR, "snd_val err %d", ret);
246 /* set a camera parameter */
247 static int set_par(struct gspca_dev *gspca_dev,
250 return snd_val(gspca_dev, 0x003f08, parval);
253 static void setbrightness(struct gspca_dev *gspca_dev)
255 struct sd *sd = (struct sd *) gspca_dev;
258 parval = 0x06000000 /* whiteness */
259 + (sd->brightness << 16);
260 set_par(gspca_dev, parval);
263 static void setcontrast(struct gspca_dev *gspca_dev)
265 struct sd *sd = (struct sd *) gspca_dev;
268 parval = 0x07000000 /* contrast */
269 + (sd->contrast << 16);
270 set_par(gspca_dev, parval);
273 static void setcolors(struct gspca_dev *gspca_dev)
275 struct sd *sd = (struct sd *) gspca_dev;
278 parval = 0x08000000 /* saturation */
279 + (sd->colors << 16);
280 set_par(gspca_dev, parval);
283 static void setfreq(struct gspca_dev *gspca_dev)
285 struct sd *sd = (struct sd *) gspca_dev;
287 set_par(gspca_dev, sd->lightfreq == 1
288 ? 0x33640000 /* 50 Hz */
289 : 0x33780000); /* 60 Hz */
292 /* this function is called at probe time */
293 static int sd_config(struct gspca_dev *gspca_dev,
294 const struct usb_device_id *id)
296 struct sd *sd = (struct sd *) gspca_dev;
297 struct cam *cam = &gspca_dev->cam;
300 gspca_dev->cam.cam_mode = vga_mode;
301 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
302 sd->brightness = BRIGHTNESS_DEF;
303 sd->contrast = CONTRAST_DEF;
304 sd->colors = COLOR_DEF;
305 sd->lightfreq = FREQ_DEF;
309 /* this function is called at open time */
310 static int sd_open(struct gspca_dev *gspca_dev)
314 /* check if the device responds */
315 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
316 ret = reg_r(gspca_dev, 0x0740);
320 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
326 /* -- start the camera -- */
327 static void sd_start(struct gspca_dev *gspca_dev)
331 /* work on alternate 1 */
332 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
334 set_par(gspca_dev, 0x10000000);
335 set_par(gspca_dev, 0x00000000);
336 set_par(gspca_dev, 0x8002e001);
337 set_par(gspca_dev, 0x14000000);
338 if (gspca_dev->width > 320)
339 value = 0x8002e001; /* 640x480 */
341 value = 0x4001f000; /* 320x240 */
342 set_par(gspca_dev, value);
343 ret = usb_set_interface(gspca_dev->dev,
347 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
348 gspca_dev->iface, gspca_dev->alt);
351 ret = reg_r(gspca_dev, 0x0630);
354 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
355 ret = reg_r(gspca_dev, 0x0650);
358 snd_val(gspca_dev, 0x000020, 0xffffffff);
359 reg_w(gspca_dev, 0x0620, 0);
360 reg_w(gspca_dev, 0x0630, 0);
361 reg_w(gspca_dev, 0x0640, 0);
362 reg_w(gspca_dev, 0x0650, 0);
363 reg_w(gspca_dev, 0x0660, 0);
364 setbrightness(gspca_dev); /* whiteness */
365 setcontrast(gspca_dev); /* contrast */
366 setcolors(gspca_dev); /* saturation */
367 set_par(gspca_dev, 0x09800000); /* Red ? */
368 set_par(gspca_dev, 0x0a800000); /* Green ? */
369 set_par(gspca_dev, 0x0b800000); /* Blue ? */
370 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
371 setfreq(gspca_dev); /* light frequency */
373 /* start the video flow */
374 set_par(gspca_dev, 0x01000000);
375 set_par(gspca_dev, 0x01000000);
376 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
379 PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
382 static void sd_stopN(struct gspca_dev *gspca_dev)
384 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);
390 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
391 reg_r(gspca_dev, 0x0650);
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 const struct sd_desc sd_desc = {
536 .nctrls = ARRAY_SIZE(sd_ctrls),
543 .pkt_scan = sd_pkt_scan,
544 .querymenu = sd_querymenu,
547 /* -- module initialisation -- */
548 static const __devinitdata struct usb_device_id device_table[] = {
549 {USB_DEVICE(0x05e1, 0x0893)},
552 MODULE_DEVICE_TABLE(usb, device_table);
554 /* -- device connect -- */
555 static int sd_probe(struct usb_interface *intf,
556 const struct usb_device_id *id)
558 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
562 static struct usb_driver sd_driver = {
564 .id_table = device_table,
566 .disconnect = gspca_disconnect,
569 /* -- module insert / remove -- */
570 static int __init sd_mod_init(void)
572 if (usb_register(&sd_driver) < 0)
577 static void __exit sd_mod_exit(void)
579 usb_deregister(&sd_driver);
580 info("deregistered");
583 module_init(sd_mod_init);
584 module_exit(sd_mod_exit);
586 module_param_named(quant, sd_quant, int, 0644);
587 MODULE_PARM_DESC(quant, "Quantization index (0..8)");