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");
36 NCTRLS
/* number of controls */
39 /* specific webcam descriptor */
41 struct gspca_dev gspca_dev
; /* !! must be the first item */
43 struct gspca_ctrl ctrls
[NCTRLS
];
46 #define QUALITY_MIN 70
47 #define QUALITY_MAX 95
48 #define QUALITY_DEF 88
50 u8 jpeg_hdr
[JPEG_HDR_SZ
];
53 /* V4L2 controls supported by the driver */
54 static void setbrightness(struct gspca_dev
*gspca_dev
);
55 static void setcontrast(struct gspca_dev
*gspca_dev
);
56 static void setcolors(struct gspca_dev
*gspca_dev
);
57 static void setlightfreq(struct gspca_dev
*gspca_dev
);
59 static const struct ctrl sd_ctrls
[NCTRLS
] = {
62 .id
= V4L2_CID_BRIGHTNESS
,
63 .type
= V4L2_CTRL_TYPE_INTEGER
,
70 .set_control
= setbrightness
74 .id
= V4L2_CID_CONTRAST
,
75 .type
= V4L2_CTRL_TYPE_INTEGER
,
82 .set_control
= setcontrast
86 .id
= V4L2_CID_SATURATION
,
87 .type
= V4L2_CTRL_TYPE_INTEGER
,
94 .set_control
= setcolors
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 */
106 .set_control
= setlightfreq
110 static const struct v4l2_pix_format vga_mode
[] = {
111 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
113 .sizeimage
= 320 * 240 * 3 / 8 + 590,
114 .colorspace
= V4L2_COLORSPACE_JPEG
,
116 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
118 .sizeimage
= 640 * 480 * 3 / 8 + 590,
119 .colorspace
= V4L2_COLORSPACE_JPEG
,
123 /* -- read a register -- */
124 static u8
reg_r(struct gspca_dev
*gspca_dev
,
127 struct usb_device
*dev
= gspca_dev
->dev
;
130 if (gspca_dev
->usb_err
< 0)
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 err("reg_r err %d", ret
);
141 gspca_dev
->usb_err
= ret
;
144 return gspca_dev
->usb_buf
[0];
147 /* -- write a register -- */
148 static void reg_w(struct gspca_dev
*gspca_dev
,
149 __u16 index
, __u16 value
)
151 struct usb_device
*dev
= gspca_dev
->dev
;
154 if (gspca_dev
->usb_err
< 0)
156 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
158 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
165 err("reg_w err %d", ret
);
166 gspca_dev
->usb_err
= ret
;
170 /* -- get a bulk value (4 bytes) -- */
171 static void rcv_val(struct gspca_dev
*gspca_dev
,
174 struct usb_device
*dev
= gspca_dev
->dev
;
177 reg_w(gspca_dev
, 0x634, (ads
>> 16) & 0xff);
178 reg_w(gspca_dev
, 0x635, (ads
>> 8) & 0xff);
179 reg_w(gspca_dev
, 0x636, ads
& 0xff);
180 reg_w(gspca_dev
, 0x637, 0);
181 reg_w(gspca_dev
, 0x638, 4); /* len & 0xff */
182 reg_w(gspca_dev
, 0x639, 0); /* len >> 8 */
183 reg_w(gspca_dev
, 0x63a, 0);
184 reg_w(gspca_dev
, 0x63b, 0);
185 reg_w(gspca_dev
, 0x630, 5);
186 if (gspca_dev
->usb_err
< 0)
188 ret
= usb_bulk_msg(dev
,
189 usb_rcvbulkpipe(dev
, 0x05),
193 500); /* timeout in milliseconds */
195 err("rcv_val err %d", ret
);
196 gspca_dev
->usb_err
= ret
;
200 /* -- send a bulk value -- */
201 static void snd_val(struct gspca_dev
*gspca_dev
,
205 struct usb_device
*dev
= gspca_dev
->dev
;
209 if (ads
== 0x003f08) {
210 reg_r(gspca_dev
, 0x0704);
211 seq
= reg_r(gspca_dev
, 0x0705);
212 reg_r(gspca_dev
, 0x0650);
213 reg_w(gspca_dev
, 0x654, seq
);
215 reg_w(gspca_dev
, 0x654, (ads
>> 16) & 0xff);
217 reg_w(gspca_dev
, 0x655, (ads
>> 8) & 0xff);
218 reg_w(gspca_dev
, 0x656, ads
& 0xff);
219 reg_w(gspca_dev
, 0x657, 0);
220 reg_w(gspca_dev
, 0x658, 0x04); /* size */
221 reg_w(gspca_dev
, 0x659, 0);
222 reg_w(gspca_dev
, 0x65a, 0);
223 reg_w(gspca_dev
, 0x65b, 0);
224 reg_w(gspca_dev
, 0x650, 5);
225 if (gspca_dev
->usb_err
< 0)
227 gspca_dev
->usb_buf
[0] = val
>> 24;
228 gspca_dev
->usb_buf
[1] = val
>> 16;
229 gspca_dev
->usb_buf
[2] = val
>> 8;
230 gspca_dev
->usb_buf
[3] = val
;
231 ret
= usb_bulk_msg(dev
,
232 usb_sndbulkpipe(dev
, 6),
236 500); /* timeout in milliseconds */
238 err("snd_val err %d", ret
);
239 gspca_dev
->usb_err
= ret
;
241 if (ads
== 0x003f08) {
244 reg_w(gspca_dev
, 0x705, seq
);
249 /* set a camera parameter */
250 static void set_par(struct gspca_dev
*gspca_dev
,
253 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
->ctrls
[BRIGHTNESS
].val
<< 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
->ctrls
[CONTRAST
].val
<< 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
->ctrls
[COLORS
].val
<< 16);
283 set_par(gspca_dev
, parval
);
286 static void setlightfreq(struct gspca_dev
*gspca_dev
)
288 struct sd
*sd
= (struct sd
*) gspca_dev
;
290 set_par(gspca_dev
, sd
->ctrls
[LIGHTFREQ
].val
== 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 gspca_dev
->cam
.ctrls
= sd
->ctrls
;
304 sd
->quality
= QUALITY_DEF
;
308 /* this function is called at probe and resume time */
309 static int sd_init(struct gspca_dev
*gspca_dev
)
313 /* check if the device responds */
314 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
315 ret
= reg_r(gspca_dev
, 0x0740);
316 if (gspca_dev
->usb_err
>= 0) {
318 err("init reg: 0x%02x", ret
);
319 gspca_dev
->usb_err
= -EIO
;
322 return gspca_dev
->usb_err
;
325 /* -- start the camera -- */
326 static int sd_start(struct gspca_dev
*gspca_dev
)
328 struct sd
*sd
= (struct sd
*) gspca_dev
;
331 /* create the JPEG header */
332 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
333 0x22); /* JPEG 411 */
334 jpeg_set_qual(sd
->jpeg_hdr
, sd
->quality
);
336 /* work on alternate 1 */
337 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
339 set_par(gspca_dev
, 0x10000000);
340 set_par(gspca_dev
, 0x00000000);
341 set_par(gspca_dev
, 0x8002e001);
342 set_par(gspca_dev
, 0x14000000);
343 if (gspca_dev
->width
> 320)
344 value
= 0x8002e001; /* 640x480 */
346 value
= 0x4001f000; /* 320x240 */
347 set_par(gspca_dev
, value
);
348 ret
= usb_set_interface(gspca_dev
->dev
,
352 err("set intf %d %d failed",
353 gspca_dev
->iface
, gspca_dev
->alt
);
354 gspca_dev
->usb_err
= ret
;
357 reg_r(gspca_dev
, 0x0630);
358 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
359 reg_r(gspca_dev
, 0x0650);
360 snd_val(gspca_dev
, 0x000020, 0xffffffff);
361 reg_w(gspca_dev
, 0x0620, 0);
362 reg_w(gspca_dev
, 0x0630, 0);
363 reg_w(gspca_dev
, 0x0640, 0);
364 reg_w(gspca_dev
, 0x0650, 0);
365 reg_w(gspca_dev
, 0x0660, 0);
366 setbrightness(gspca_dev
); /* whiteness */
367 setcontrast(gspca_dev
); /* contrast */
368 setcolors(gspca_dev
); /* saturation */
369 set_par(gspca_dev
, 0x09800000); /* Red ? */
370 set_par(gspca_dev
, 0x0a800000); /* Green ? */
371 set_par(gspca_dev
, 0x0b800000); /* Blue ? */
372 set_par(gspca_dev
, 0x0d030000); /* Gamma ? */
373 setlightfreq(gspca_dev
);
375 /* start the video flow */
376 set_par(gspca_dev
, 0x01000000);
377 set_par(gspca_dev
, 0x01000000);
378 if (gspca_dev
->usb_err
>= 0)
379 PDEBUG(D_STREAM
, "camera started alt: 0x%02x",
382 return gspca_dev
->usb_err
;
385 static void sd_stopN(struct gspca_dev
*gspca_dev
)
387 struct usb_device
*dev
= gspca_dev
->dev
;
389 set_par(gspca_dev
, 0x02000000);
390 set_par(gspca_dev
, 0x02000000);
391 usb_set_interface(dev
, gspca_dev
->iface
, 1);
392 reg_r(gspca_dev
, 0x0630);
393 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
394 reg_r(gspca_dev
, 0x0650);
395 snd_val(gspca_dev
, 0x000020, 0xffffffff);
396 reg_w(gspca_dev
, 0x0620, 0);
397 reg_w(gspca_dev
, 0x0630, 0);
398 reg_w(gspca_dev
, 0x0640, 0);
399 reg_w(gspca_dev
, 0x0650, 0);
400 reg_w(gspca_dev
, 0x0660, 0);
401 PDEBUG(D_STREAM
, "camera stopped");
404 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
405 u8
*data
, /* isoc packet */
406 int len
) /* iso packet length */
408 struct sd
*sd
= (struct sd
*) gspca_dev
;
409 static unsigned char ffd9
[] = {0xff, 0xd9};
411 /* a frame starts with:
413 * - 0x08 0x00 - length (little endian ?!)
414 * - 4 bytes = size of whole frame (BE - including header)
417 * - .. JPEG image with escape sequences (ff 00)
418 * (without ending - ff d9)
420 if (data
[0] == 0xff && data
[1] == 0xfe) {
421 gspca_frame_add(gspca_dev
, LAST_PACKET
,
424 /* put the JPEG 411 header */
425 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
426 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
428 /* beginning of the frame */
433 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
436 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
437 struct v4l2_querymenu
*menu
)
439 static const char *freq_nm
[3] = {"NoFliker", "50 Hz", "60 Hz"};
442 case V4L2_CID_POWER_LINE_FREQUENCY
:
443 if ((unsigned) menu
->index
>= ARRAY_SIZE(freq_nm
))
445 strcpy((char *) menu
->name
, freq_nm
[menu
->index
]);
451 static int sd_set_jcomp(struct gspca_dev
*gspca_dev
,
452 struct v4l2_jpegcompression
*jcomp
)
454 struct sd
*sd
= (struct sd
*) gspca_dev
;
456 if (jcomp
->quality
< QUALITY_MIN
)
457 sd
->quality
= QUALITY_MIN
;
458 else if (jcomp
->quality
> QUALITY_MAX
)
459 sd
->quality
= QUALITY_MAX
;
461 sd
->quality
= jcomp
->quality
;
462 if (gspca_dev
->streaming
)
463 jpeg_set_qual(sd
->jpeg_hdr
, sd
->quality
);
464 return gspca_dev
->usb_err
;
467 static int sd_get_jcomp(struct gspca_dev
*gspca_dev
,
468 struct v4l2_jpegcompression
*jcomp
)
470 struct sd
*sd
= (struct sd
*) gspca_dev
;
472 memset(jcomp
, 0, sizeof *jcomp
);
473 jcomp
->quality
= sd
->quality
;
474 jcomp
->jpeg_markers
= V4L2_JPEG_MARKER_DHT
475 | V4L2_JPEG_MARKER_DQT
;
479 /* sub-driver description */
480 static const struct sd_desc sd_desc
= {
488 .pkt_scan
= sd_pkt_scan
,
489 .querymenu
= sd_querymenu
,
490 .get_jcomp
= sd_get_jcomp
,
491 .set_jcomp
= sd_set_jcomp
,
494 /* -- module initialisation -- */
495 static const struct usb_device_id device_table
[] = {
496 {USB_DEVICE(0x05e1, 0x0893)},
499 MODULE_DEVICE_TABLE(usb
, device_table
);
501 /* -- device connect -- */
502 static int sd_probe(struct usb_interface
*intf
,
503 const struct usb_device_id
*id
)
505 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
509 static struct usb_driver sd_driver
= {
511 .id_table
= device_table
,
513 .disconnect
= gspca_disconnect
,
515 .suspend
= gspca_suspend
,
516 .resume
= gspca_resume
,
520 /* -- module insert / remove -- */
521 static int __init
sd_mod_init(void)
523 return usb_register(&sd_driver
);
525 static void __exit
sd_mod_exit(void)
527 usb_deregister(&sd_driver
);
530 module_init(sd_mod_init
);
531 module_exit(sd_mod_exit
);