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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #define MODULE_NAME "stk014"
28 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29 MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
30 MODULE_LICENSE("GPL");
34 /* specific webcam descriptor */
36 struct gspca_dev gspca_dev
; /* !! must be the first item */
37 u8 jpeg_hdr
[JPEG_HDR_SZ
];
40 static const struct v4l2_pix_format vga_mode
[] = {
41 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
43 .sizeimage
= 320 * 240 * 3 / 8 + 590,
44 .colorspace
= V4L2_COLORSPACE_JPEG
,
46 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
48 .sizeimage
= 640 * 480 * 3 / 8 + 590,
49 .colorspace
= V4L2_COLORSPACE_JPEG
,
53 /* -- read a register -- */
54 static u8
reg_r(struct gspca_dev
*gspca_dev
,
57 struct usb_device
*dev
= gspca_dev
->dev
;
60 if (gspca_dev
->usb_err
< 0)
62 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
64 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
67 gspca_dev
->usb_buf
, 1,
70 pr_err("reg_r err %d\n", ret
);
71 gspca_dev
->usb_err
= ret
;
74 return gspca_dev
->usb_buf
[0];
77 /* -- write a register -- */
78 static void reg_w(struct gspca_dev
*gspca_dev
,
79 __u16 index
, __u16 value
)
81 struct usb_device
*dev
= gspca_dev
->dev
;
84 if (gspca_dev
->usb_err
< 0)
86 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
88 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
95 pr_err("reg_w err %d\n", ret
);
96 gspca_dev
->usb_err
= ret
;
100 /* -- get a bulk value (4 bytes) -- */
101 static void rcv_val(struct gspca_dev
*gspca_dev
,
104 struct usb_device
*dev
= gspca_dev
->dev
;
107 reg_w(gspca_dev
, 0x634, (ads
>> 16) & 0xff);
108 reg_w(gspca_dev
, 0x635, (ads
>> 8) & 0xff);
109 reg_w(gspca_dev
, 0x636, ads
& 0xff);
110 reg_w(gspca_dev
, 0x637, 0);
111 reg_w(gspca_dev
, 0x638, 4); /* len & 0xff */
112 reg_w(gspca_dev
, 0x639, 0); /* len >> 8 */
113 reg_w(gspca_dev
, 0x63a, 0);
114 reg_w(gspca_dev
, 0x63b, 0);
115 reg_w(gspca_dev
, 0x630, 5);
116 if (gspca_dev
->usb_err
< 0)
118 ret
= usb_bulk_msg(dev
,
119 usb_rcvbulkpipe(dev
, 0x05),
123 500); /* timeout in milliseconds */
125 pr_err("rcv_val err %d\n", ret
);
126 gspca_dev
->usb_err
= ret
;
130 /* -- send a bulk value -- */
131 static void snd_val(struct gspca_dev
*gspca_dev
,
135 struct usb_device
*dev
= gspca_dev
->dev
;
139 if (ads
== 0x003f08) {
140 reg_r(gspca_dev
, 0x0704);
141 seq
= reg_r(gspca_dev
, 0x0705);
142 reg_r(gspca_dev
, 0x0650);
143 reg_w(gspca_dev
, 0x654, seq
);
145 reg_w(gspca_dev
, 0x654, (ads
>> 16) & 0xff);
147 reg_w(gspca_dev
, 0x655, (ads
>> 8) & 0xff);
148 reg_w(gspca_dev
, 0x656, ads
& 0xff);
149 reg_w(gspca_dev
, 0x657, 0);
150 reg_w(gspca_dev
, 0x658, 0x04); /* size */
151 reg_w(gspca_dev
, 0x659, 0);
152 reg_w(gspca_dev
, 0x65a, 0);
153 reg_w(gspca_dev
, 0x65b, 0);
154 reg_w(gspca_dev
, 0x650, 5);
155 if (gspca_dev
->usb_err
< 0)
157 gspca_dev
->usb_buf
[0] = val
>> 24;
158 gspca_dev
->usb_buf
[1] = val
>> 16;
159 gspca_dev
->usb_buf
[2] = val
>> 8;
160 gspca_dev
->usb_buf
[3] = val
;
161 ret
= usb_bulk_msg(dev
,
162 usb_sndbulkpipe(dev
, 6),
166 500); /* timeout in milliseconds */
168 pr_err("snd_val err %d\n", ret
);
169 gspca_dev
->usb_err
= ret
;
171 if (ads
== 0x003f08) {
174 reg_w(gspca_dev
, 0x705, seq
);
179 /* set a camera parameter */
180 static void set_par(struct gspca_dev
*gspca_dev
,
183 snd_val(gspca_dev
, 0x003f08, parval
);
186 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
190 parval
= 0x06000000 /* whiteness */
192 set_par(gspca_dev
, parval
);
195 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
199 parval
= 0x07000000 /* contrast */
201 set_par(gspca_dev
, parval
);
204 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
)
208 parval
= 0x08000000 /* saturation */
210 set_par(gspca_dev
, parval
);
213 static void setlightfreq(struct gspca_dev
*gspca_dev
, s32 val
)
215 set_par(gspca_dev
, val
== 1
216 ? 0x33640000 /* 50 Hz */
217 : 0x33780000); /* 60 Hz */
220 /* this function is called at probe time */
221 static int sd_config(struct gspca_dev
*gspca_dev
,
222 const struct usb_device_id
*id
)
224 gspca_dev
->cam
.cam_mode
= vga_mode
;
225 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(vga_mode
);
229 /* this function is called at probe and resume time */
230 static int sd_init(struct gspca_dev
*gspca_dev
)
234 /* check if the device responds */
235 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
236 ret
= reg_r(gspca_dev
, 0x0740);
237 if (gspca_dev
->usb_err
>= 0) {
239 pr_err("init reg: 0x%02x\n", ret
);
240 gspca_dev
->usb_err
= -EIO
;
243 return gspca_dev
->usb_err
;
246 /* -- start the camera -- */
247 static int sd_start(struct gspca_dev
*gspca_dev
)
249 struct sd
*sd
= (struct sd
*) gspca_dev
;
252 /* create the JPEG header */
253 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
254 gspca_dev
->pixfmt
.width
,
255 0x22); /* JPEG 411 */
256 jpeg_set_qual(sd
->jpeg_hdr
, QUALITY
);
258 /* work on alternate 1 */
259 usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
261 set_par(gspca_dev
, 0x10000000);
262 set_par(gspca_dev
, 0x00000000);
263 set_par(gspca_dev
, 0x8002e001);
264 set_par(gspca_dev
, 0x14000000);
265 if (gspca_dev
->pixfmt
.width
> 320)
266 value
= 0x8002e001; /* 640x480 */
268 value
= 0x4001f000; /* 320x240 */
269 set_par(gspca_dev
, value
);
270 ret
= usb_set_interface(gspca_dev
->dev
,
274 pr_err("set intf %d %d failed\n",
275 gspca_dev
->iface
, gspca_dev
->alt
);
276 gspca_dev
->usb_err
= ret
;
279 reg_r(gspca_dev
, 0x0630);
280 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
281 reg_r(gspca_dev
, 0x0650);
282 snd_val(gspca_dev
, 0x000020, 0xffffffff);
283 reg_w(gspca_dev
, 0x0620, 0);
284 reg_w(gspca_dev
, 0x0630, 0);
285 reg_w(gspca_dev
, 0x0640, 0);
286 reg_w(gspca_dev
, 0x0650, 0);
287 reg_w(gspca_dev
, 0x0660, 0);
288 set_par(gspca_dev
, 0x09800000); /* Red ? */
289 set_par(gspca_dev
, 0x0a800000); /* Green ? */
290 set_par(gspca_dev
, 0x0b800000); /* Blue ? */
291 set_par(gspca_dev
, 0x0d030000); /* Gamma ? */
293 /* start the video flow */
294 set_par(gspca_dev
, 0x01000000);
295 set_par(gspca_dev
, 0x01000000);
296 if (gspca_dev
->usb_err
>= 0)
297 PDEBUG(D_STREAM
, "camera started alt: 0x%02x",
300 return gspca_dev
->usb_err
;
303 static void sd_stopN(struct gspca_dev
*gspca_dev
)
305 struct usb_device
*dev
= gspca_dev
->dev
;
307 set_par(gspca_dev
, 0x02000000);
308 set_par(gspca_dev
, 0x02000000);
309 usb_set_interface(dev
, gspca_dev
->iface
, 1);
310 reg_r(gspca_dev
, 0x0630);
311 rcv_val(gspca_dev
, 0x000020); /* << (value ff ff ff ff) */
312 reg_r(gspca_dev
, 0x0650);
313 snd_val(gspca_dev
, 0x000020, 0xffffffff);
314 reg_w(gspca_dev
, 0x0620, 0);
315 reg_w(gspca_dev
, 0x0630, 0);
316 reg_w(gspca_dev
, 0x0640, 0);
317 reg_w(gspca_dev
, 0x0650, 0);
318 reg_w(gspca_dev
, 0x0660, 0);
319 PDEBUG(D_STREAM
, "camera stopped");
322 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
323 u8
*data
, /* isoc packet */
324 int len
) /* iso packet length */
326 struct sd
*sd
= (struct sd
*) gspca_dev
;
327 static unsigned char ffd9
[] = {0xff, 0xd9};
329 /* a frame starts with:
331 * - 0x08 0x00 - length (little endian ?!)
332 * - 4 bytes = size of whole frame (BE - including header)
335 * - .. JPEG image with escape sequences (ff 00)
336 * (without ending - ff d9)
338 if (data
[0] == 0xff && data
[1] == 0xfe) {
339 gspca_frame_add(gspca_dev
, LAST_PACKET
,
342 /* put the JPEG 411 header */
343 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
344 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
346 /* beginning of the frame */
351 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
354 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
356 struct gspca_dev
*gspca_dev
=
357 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
359 gspca_dev
->usb_err
= 0;
361 if (!gspca_dev
->streaming
)
365 case V4L2_CID_BRIGHTNESS
:
366 setbrightness(gspca_dev
, ctrl
->val
);
368 case V4L2_CID_CONTRAST
:
369 setcontrast(gspca_dev
, ctrl
->val
);
371 case V4L2_CID_SATURATION
:
372 setcolors(gspca_dev
, ctrl
->val
);
374 case V4L2_CID_POWER_LINE_FREQUENCY
:
375 setlightfreq(gspca_dev
, ctrl
->val
);
378 return gspca_dev
->usb_err
;
381 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
385 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
387 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
389 gspca_dev
->vdev
.ctrl_handler
= hdl
;
390 v4l2_ctrl_handler_init(hdl
, 4);
391 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
392 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
393 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
394 V4L2_CID_CONTRAST
, 0, 255, 1, 127);
395 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
396 V4L2_CID_SATURATION
, 0, 255, 1, 127);
397 v4l2_ctrl_new_std_menu(hdl
, &sd_ctrl_ops
,
398 V4L2_CID_POWER_LINE_FREQUENCY
,
399 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, 1,
400 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
);
403 pr_err("Could not initialize controls\n");
409 /* sub-driver description */
410 static const struct sd_desc sd_desc
= {
414 .init_controls
= sd_init_controls
,
417 .pkt_scan
= sd_pkt_scan
,
420 /* -- module initialisation -- */
421 static const struct usb_device_id device_table
[] = {
422 {USB_DEVICE(0x05e1, 0x0893)},
425 MODULE_DEVICE_TABLE(usb
, device_table
);
427 /* -- device connect -- */
428 static int sd_probe(struct usb_interface
*intf
,
429 const struct usb_device_id
*id
)
431 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
435 static struct usb_driver sd_driver
= {
437 .id_table
= device_table
,
439 .disconnect
= gspca_disconnect
,
441 .suspend
= gspca_suspend
,
442 .resume
= gspca_resume
,
443 .reset_resume
= gspca_resume
,
447 module_usb_driver(sd_driver
);