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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #define MODULE_NAME "stk014"
24 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
25 MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
26 MODULE_LICENSE("GPL");
30 /* specific webcam descriptor */
32 struct gspca_dev gspca_dev
; /* !! must be the first item */
33 u8 jpeg_hdr
[JPEG_HDR_SZ
];
36 static const struct v4l2_pix_format vga_mode
[] = {
37 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
39 .sizeimage
= 320 * 240 * 3 / 8 + 590,
40 .colorspace
= V4L2_COLORSPACE_JPEG
,
42 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
44 .sizeimage
= 640 * 480 * 3 / 8 + 590,
45 .colorspace
= V4L2_COLORSPACE_JPEG
,
49 /* -- read a register -- */
50 static u8
reg_r(struct gspca_dev
*gspca_dev
,
53 struct usb_device
*dev
= gspca_dev
->dev
;
56 if (gspca_dev
->usb_err
< 0)
58 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
60 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
63 gspca_dev
->usb_buf
, 1,
66 pr_err("reg_r err %d\n", ret
);
67 gspca_dev
->usb_err
= ret
;
70 return gspca_dev
->usb_buf
[0];
73 /* -- write a register -- */
74 static void reg_w(struct gspca_dev
*gspca_dev
,
75 __u16 index
, __u16 value
)
77 struct usb_device
*dev
= gspca_dev
->dev
;
80 if (gspca_dev
->usb_err
< 0)
82 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
84 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
91 pr_err("reg_w err %d\n", ret
);
92 gspca_dev
->usb_err
= ret
;
96 /* -- get a bulk value (4 bytes) -- */
97 static void rcv_val(struct gspca_dev
*gspca_dev
,
100 struct usb_device
*dev
= gspca_dev
->dev
;
103 reg_w(gspca_dev
, 0x634, (ads
>> 16) & 0xff);
104 reg_w(gspca_dev
, 0x635, (ads
>> 8) & 0xff);
105 reg_w(gspca_dev
, 0x636, ads
& 0xff);
106 reg_w(gspca_dev
, 0x637, 0);
107 reg_w(gspca_dev
, 0x638, 4); /* len & 0xff */
108 reg_w(gspca_dev
, 0x639, 0); /* len >> 8 */
109 reg_w(gspca_dev
, 0x63a, 0);
110 reg_w(gspca_dev
, 0x63b, 0);
111 reg_w(gspca_dev
, 0x630, 5);
112 if (gspca_dev
->usb_err
< 0)
114 ret
= usb_bulk_msg(dev
,
115 usb_rcvbulkpipe(dev
, 0x05),
119 500); /* timeout in milliseconds */
121 pr_err("rcv_val err %d\n", ret
);
122 gspca_dev
->usb_err
= ret
;
126 /* -- send a bulk value -- */
127 static void snd_val(struct gspca_dev
*gspca_dev
,
131 struct usb_device
*dev
= gspca_dev
->dev
;
135 if (ads
== 0x003f08) {
136 reg_r(gspca_dev
, 0x0704);
137 seq
= reg_r(gspca_dev
, 0x0705);
138 reg_r(gspca_dev
, 0x0650);
139 reg_w(gspca_dev
, 0x654, seq
);
141 reg_w(gspca_dev
, 0x654, (ads
>> 16) & 0xff);
143 reg_w(gspca_dev
, 0x655, (ads
>> 8) & 0xff);
144 reg_w(gspca_dev
, 0x656, ads
& 0xff);
145 reg_w(gspca_dev
, 0x657, 0);
146 reg_w(gspca_dev
, 0x658, 0x04); /* size */
147 reg_w(gspca_dev
, 0x659, 0);
148 reg_w(gspca_dev
, 0x65a, 0);
149 reg_w(gspca_dev
, 0x65b, 0);
150 reg_w(gspca_dev
, 0x650, 5);
151 if (gspca_dev
->usb_err
< 0)
153 gspca_dev
->usb_buf
[0] = val
>> 24;
154 gspca_dev
->usb_buf
[1] = val
>> 16;
155 gspca_dev
->usb_buf
[2] = val
>> 8;
156 gspca_dev
->usb_buf
[3] = val
;
157 ret
= usb_bulk_msg(dev
,
158 usb_sndbulkpipe(dev
, 6),
162 500); /* timeout in milliseconds */
164 pr_err("snd_val err %d\n", ret
);
165 gspca_dev
->usb_err
= ret
;
167 if (ads
== 0x003f08) {
170 reg_w(gspca_dev
, 0x705, seq
);
175 /* set a camera parameter */
176 static void set_par(struct gspca_dev
*gspca_dev
,
179 snd_val(gspca_dev
, 0x003f08, parval
);
182 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
186 parval
= 0x06000000 /* whiteness */
188 set_par(gspca_dev
, parval
);
191 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
195 parval
= 0x07000000 /* contrast */
197 set_par(gspca_dev
, parval
);
200 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
)
204 parval
= 0x08000000 /* saturation */
206 set_par(gspca_dev
, parval
);
209 static void setlightfreq(struct gspca_dev
*gspca_dev
, s32 val
)
211 set_par(gspca_dev
, val
== 1
212 ? 0x33640000 /* 50 Hz */
213 : 0x33780000); /* 60 Hz */
216 /* this function is called at probe time */
217 static int sd_config(struct gspca_dev
*gspca_dev
,
218 const struct usb_device_id
*id
)
220 gspca_dev
->cam
.cam_mode
= vga_mode
;
221 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
225 /* this function is called at probe and resume time */
226 static int sd_init(struct gspca_dev
*gspca_dev
)
230 /* check if the device responds */
231 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
232 ret
= reg_r(gspca_dev
, 0x0740);
233 if (gspca_dev
->usb_err
>= 0) {
235 pr_err("init reg: 0x%02x\n", ret
);
236 gspca_dev
->usb_err
= -EIO
;
239 return gspca_dev
->usb_err
;
242 /* -- start the camera -- */
243 static int sd_start(struct gspca_dev
*gspca_dev
)
245 struct sd
*sd
= (struct sd
*) gspca_dev
;
248 /* create the JPEG header */
249 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
250 gspca_dev
->pixfmt
.width
,
251 0x22); /* JPEG 411 */
252 jpeg_set_qual(sd
->jpeg_hdr
, QUALITY
);
254 /* work on alternate 1 */
255 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
257 set_par(gspca_dev
, 0x10000000);
258 set_par(gspca_dev
, 0x00000000);
259 set_par(gspca_dev
, 0x8002e001);
260 set_par(gspca_dev
, 0x14000000);
261 if (gspca_dev
->pixfmt
.width
> 320)
262 value
= 0x8002e001; /* 640x480 */
264 value
= 0x4001f000; /* 320x240 */
265 set_par(gspca_dev
, value
);
266 ret
= usb_set_interface(gspca_dev
->dev
,
270 pr_err("set intf %d %d failed\n",
271 gspca_dev
->iface
, gspca_dev
->alt
);
272 gspca_dev
->usb_err
= ret
;
275 reg_r(gspca_dev
, 0x0630);
276 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
277 reg_r(gspca_dev
, 0x0650);
278 snd_val(gspca_dev
, 0x000020, 0xffffffff);
279 reg_w(gspca_dev
, 0x0620, 0);
280 reg_w(gspca_dev
, 0x0630, 0);
281 reg_w(gspca_dev
, 0x0640, 0);
282 reg_w(gspca_dev
, 0x0650, 0);
283 reg_w(gspca_dev
, 0x0660, 0);
284 set_par(gspca_dev
, 0x09800000); /* Red ? */
285 set_par(gspca_dev
, 0x0a800000); /* Green ? */
286 set_par(gspca_dev
, 0x0b800000); /* Blue ? */
287 set_par(gspca_dev
, 0x0d030000); /* Gamma ? */
289 /* start the video flow */
290 set_par(gspca_dev
, 0x01000000);
291 set_par(gspca_dev
, 0x01000000);
292 if (gspca_dev
->usb_err
>= 0)
293 gspca_dbg(gspca_dev
, D_STREAM
, "camera started alt: 0x%02x\n",
296 return gspca_dev
->usb_err
;
299 static void sd_stopN(struct gspca_dev
*gspca_dev
)
301 struct usb_device
*dev
= gspca_dev
->dev
;
303 set_par(gspca_dev
, 0x02000000);
304 set_par(gspca_dev
, 0x02000000);
305 usb_set_interface(dev
, gspca_dev
->iface
, 1);
306 reg_r(gspca_dev
, 0x0630);
307 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
308 reg_r(gspca_dev
, 0x0650);
309 snd_val(gspca_dev
, 0x000020, 0xffffffff);
310 reg_w(gspca_dev
, 0x0620, 0);
311 reg_w(gspca_dev
, 0x0630, 0);
312 reg_w(gspca_dev
, 0x0640, 0);
313 reg_w(gspca_dev
, 0x0650, 0);
314 reg_w(gspca_dev
, 0x0660, 0);
315 gspca_dbg(gspca_dev
, D_STREAM
, "camera stopped\n");
318 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
319 u8
*data
, /* isoc packet */
320 int len
) /* iso packet length */
322 struct sd
*sd
= (struct sd
*) gspca_dev
;
323 static unsigned char ffd9
[] = {0xff, 0xd9};
325 /* a frame starts with:
327 * - 0x08 0x00 - length (little endian ?!)
328 * - 4 bytes = size of whole frame (BE - including header)
331 * - .. JPEG image with escape sequences (ff 00)
332 * (without ending - ff d9)
334 if (data
[0] == 0xff && data
[1] == 0xfe) {
335 gspca_frame_add(gspca_dev
, LAST_PACKET
,
338 /* put the JPEG 411 header */
339 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
340 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
342 /* beginning of the frame */
347 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
350 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
352 struct gspca_dev
*gspca_dev
=
353 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
355 gspca_dev
->usb_err
= 0;
357 if (!gspca_dev
->streaming
)
361 case V4L2_CID_BRIGHTNESS
:
362 setbrightness(gspca_dev
, ctrl
->val
);
364 case V4L2_CID_CONTRAST
:
365 setcontrast(gspca_dev
, ctrl
->val
);
367 case V4L2_CID_SATURATION
:
368 setcolors(gspca_dev
, ctrl
->val
);
370 case V4L2_CID_POWER_LINE_FREQUENCY
:
371 setlightfreq(gspca_dev
, ctrl
->val
);
374 return gspca_dev
->usb_err
;
377 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
381 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
383 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
385 gspca_dev
->vdev
.ctrl_handler
= hdl
;
386 v4l2_ctrl_handler_init(hdl
, 4);
387 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
388 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
389 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
390 V4L2_CID_CONTRAST
, 0, 255, 1, 127);
391 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
392 V4L2_CID_SATURATION
, 0, 255, 1, 127);
393 v4l2_ctrl_new_std_menu(hdl
, &sd_ctrl_ops
,
394 V4L2_CID_POWER_LINE_FREQUENCY
,
395 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, 1,
396 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
);
399 pr_err("Could not initialize controls\n");
405 /* sub-driver description */
406 static const struct sd_desc sd_desc
= {
410 .init_controls
= sd_init_controls
,
413 .pkt_scan
= sd_pkt_scan
,
416 /* -- module initialisation -- */
417 static const struct usb_device_id device_table
[] = {
418 {USB_DEVICE(0x05e1, 0x0893)},
421 MODULE_DEVICE_TABLE(usb
, device_table
);
423 /* -- device connect -- */
424 static int sd_probe(struct usb_interface
*intf
,
425 const struct usb_device_id
*id
)
427 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
431 static struct usb_driver sd_driver
= {
433 .id_table
= device_table
,
435 .disconnect
= gspca_disconnect
,
437 .suspend
= gspca_suspend
,
438 .resume
= gspca_resume
,
439 .reset_resume
= gspca_resume
,
443 module_usb_driver(sd_driver
);