[media] gspca: always call v4l2_ctrl_handler_setup after start
[deliverable/linux.git] / drivers / media / video / gspca / stk014.c
CommitLineData
63eb9546
JFM
1/*
2 * Syntek DV4000 (STK014) subdriver
3 *
d43fa32f 4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
63eb9546
JFM
5 *
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
9 * any later version.
10 *
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.
15 *
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
63eb9546
JFM
19 */
20
133a9fe9
JP
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
63eb9546
JFM
23#define MODULE_NAME "stk014"
24
25#include "gspca.h"
26#include "jpeg.h"
27
63eb9546
JFM
28MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
30MODULE_LICENSE("GPL");
31
32/* specific webcam descriptor */
33struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */
35
74dec797 36 struct v4l2_ctrl *jpegqual;
bf48cc41 37#define QUALITY_MIN 70
77ac0baf 38#define QUALITY_MAX 95
bf48cc41 39#define QUALITY_DEF 88
71cb2764 40
9a731a32 41 u8 jpeg_hdr[JPEG_HDR_SZ];
63eb9546
JFM
42};
43
cc611b8a 44static const struct v4l2_pix_format vga_mode[] = {
c2446b3e
JFM
45 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
46 .bytesperline = 320,
47 .sizeimage = 320 * 240 * 3 / 8 + 590,
48 .colorspace = V4L2_COLORSPACE_JPEG,
49 .priv = 1},
50 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51 .bytesperline = 640,
52 .sizeimage = 640 * 480 * 3 / 8 + 590,
53 .colorspace = V4L2_COLORSPACE_JPEG,
54 .priv = 0},
63eb9546
JFM
55};
56
57/* -- read a register -- */
ecb77686 58static u8 reg_r(struct gspca_dev *gspca_dev,
739570bb 59 __u16 index)
63eb9546 60{
63eb9546 61 struct usb_device *dev = gspca_dev->dev;
6a7eba24 62 int ret;
63eb9546 63
ecb77686
JFM
64 if (gspca_dev->usb_err < 0)
65 return 0;
63eb9546
JFM
66 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
67 0x00,
68 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
69 0x00,
70 index,
739570bb 71 gspca_dev->usb_buf, 1,
63eb9546 72 500);
739570bb 73 if (ret < 0) {
133a9fe9 74 pr_err("reg_r err %d\n", ret);
ecb77686
JFM
75 gspca_dev->usb_err = ret;
76 return 0;
739570bb
JFM
77 }
78 return gspca_dev->usb_buf[0];
63eb9546
JFM
79}
80
81/* -- write a register -- */
ecb77686 82static void reg_w(struct gspca_dev *gspca_dev,
63eb9546
JFM
83 __u16 index, __u16 value)
84{
85 struct usb_device *dev = gspca_dev->dev;
86 int ret;
87
ecb77686
JFM
88 if (gspca_dev->usb_err < 0)
89 return;
63eb9546
JFM
90 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
91 0x01,
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
93 value,
94 index,
95 NULL,
96 0,
97 500);
ecb77686 98 if (ret < 0) {
133a9fe9 99 pr_err("reg_w err %d\n", ret);
ecb77686
JFM
100 gspca_dev->usb_err = ret;
101 }
63eb9546
JFM
102}
103
739570bb 104/* -- get a bulk value (4 bytes) -- */
ecb77686 105static void rcv_val(struct gspca_dev *gspca_dev,
739570bb 106 int ads)
63eb9546
JFM
107{
108 struct usb_device *dev = gspca_dev->dev;
109 int alen, ret;
63eb9546 110
6a7eba24
JFM
111 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
112 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
113 reg_w(gspca_dev, 0x636, ads & 0xff);
114 reg_w(gspca_dev, 0x637, 0);
739570bb
JFM
115 reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
116 reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
6a7eba24
JFM
117 reg_w(gspca_dev, 0x63a, 0);
118 reg_w(gspca_dev, 0x63b, 0);
119 reg_w(gspca_dev, 0x630, 5);
ecb77686
JFM
120 if (gspca_dev->usb_err < 0)
121 return;
63eb9546 122 ret = usb_bulk_msg(dev,
50e06dee 123 usb_rcvbulkpipe(dev, 0x05),
739570bb
JFM
124 gspca_dev->usb_buf,
125 4, /* length */
63eb9546 126 &alen,
739570bb 127 500); /* timeout in milliseconds */
ecb77686 128 if (ret < 0) {
133a9fe9 129 pr_err("rcv_val err %d\n", ret);
ecb77686
JFM
130 gspca_dev->usb_err = ret;
131 }
63eb9546
JFM
132}
133
739570bb 134/* -- send a bulk value -- */
ecb77686 135static void snd_val(struct gspca_dev *gspca_dev,
63eb9546
JFM
136 int ads,
137 unsigned int val)
138{
139 struct usb_device *dev = gspca_dev->dev;
140 int alen, ret;
739570bb 141 __u8 seq = 0;
63eb9546
JFM
142
143 if (ads == 0x003f08) {
ecb77686
JFM
144 reg_r(gspca_dev, 0x0704);
145 seq = reg_r(gspca_dev, 0x0705);
146 reg_r(gspca_dev, 0x0650);
6a7eba24 147 reg_w(gspca_dev, 0x654, seq);
739570bb 148 } else {
6a7eba24 149 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
739570bb 150 }
6a7eba24
JFM
151 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
152 reg_w(gspca_dev, 0x656, ads & 0xff);
153 reg_w(gspca_dev, 0x657, 0);
154 reg_w(gspca_dev, 0x658, 0x04); /* size */
155 reg_w(gspca_dev, 0x659, 0);
156 reg_w(gspca_dev, 0x65a, 0);
157 reg_w(gspca_dev, 0x65b, 0);
158 reg_w(gspca_dev, 0x650, 5);
ecb77686
JFM
159 if (gspca_dev->usb_err < 0)
160 return;
739570bb
JFM
161 gspca_dev->usb_buf[0] = val >> 24;
162 gspca_dev->usb_buf[1] = val >> 16;
163 gspca_dev->usb_buf[2] = val >> 8;
164 gspca_dev->usb_buf[3] = val;
63eb9546
JFM
165 ret = usb_bulk_msg(dev,
166 usb_sndbulkpipe(dev, 6),
739570bb 167 gspca_dev->usb_buf,
63eb9546
JFM
168 4,
169 &alen,
170 500); /* timeout in milliseconds */
ecb77686 171 if (ret < 0) {
133a9fe9 172 pr_err("snd_val err %d\n", ret);
ecb77686
JFM
173 gspca_dev->usb_err = ret;
174 } else {
175 if (ads == 0x003f08) {
176 seq += 4;
177 seq &= 0x3f;
178 reg_w(gspca_dev, 0x705, seq);
179 }
63eb9546 180 }
63eb9546
JFM
181}
182
183/* set a camera parameter */
ecb77686 184static void set_par(struct gspca_dev *gspca_dev,
63eb9546
JFM
185 int parval)
186{
ecb77686 187 snd_val(gspca_dev, 0x003f08, parval);
63eb9546
JFM
188}
189
74dec797 190static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
63eb9546 191{
63eb9546
JFM
192 int parval;
193
63eb9546 194 parval = 0x06000000 /* whiteness */
74dec797 195 + (val << 16);
63eb9546
JFM
196 set_par(gspca_dev, parval);
197}
198
74dec797 199static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
63eb9546 200{
63eb9546
JFM
201 int parval;
202
63eb9546 203 parval = 0x07000000 /* contrast */
74dec797 204 + (val << 16);
63eb9546
JFM
205 set_par(gspca_dev, parval);
206}
207
74dec797 208static void setcolors(struct gspca_dev *gspca_dev, s32 val)
63eb9546 209{
63eb9546
JFM
210 int parval;
211
63eb9546 212 parval = 0x08000000 /* saturation */
74dec797 213 + (val << 16);
63eb9546
JFM
214 set_par(gspca_dev, parval);
215}
216
74dec797 217static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
6a7eba24 218{
74dec797 219 set_par(gspca_dev, val == 1
6a7eba24
JFM
220 ? 0x33640000 /* 50 Hz */
221 : 0x33780000); /* 60 Hz */
222}
223
63eb9546
JFM
224/* this function is called at probe time */
225static int sd_config(struct gspca_dev *gspca_dev,
226 const struct usb_device_id *id)
227{
63eb9546 228 gspca_dev->cam.cam_mode = vga_mode;
739570bb 229 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
63eb9546
JFM
230 return 0;
231}
232
012d6b02
JFM
233/* this function is called at probe and resume time */
234static int sd_init(struct gspca_dev *gspca_dev)
63eb9546 235{
ecb77686 236 u8 ret;
63eb9546
JFM
237
238 /* check if the device responds */
239 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
739570bb 240 ret = reg_r(gspca_dev, 0x0740);
ecb77686
JFM
241 if (gspca_dev->usb_err >= 0) {
242 if (ret != 0xff) {
133a9fe9 243 pr_err("init reg: 0x%02x\n", ret);
ecb77686
JFM
244 gspca_dev->usb_err = -EIO;
245 }
63eb9546 246 }
ecb77686 247 return gspca_dev->usb_err;
63eb9546
JFM
248}
249
250/* -- start the camera -- */
72ab97ce 251static int sd_start(struct gspca_dev *gspca_dev)
63eb9546 252{
71cb2764 253 struct sd *sd = (struct sd *) gspca_dev;
63eb9546
JFM
254 int ret, value;
255
71cb2764 256 /* create the JPEG header */
71cb2764
JFM
257 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
258 0x22); /* JPEG 411 */
71cb2764 259
63eb9546
JFM
260 /* work on alternate 1 */
261 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
262
263 set_par(gspca_dev, 0x10000000);
264 set_par(gspca_dev, 0x00000000);
265 set_par(gspca_dev, 0x8002e001);
266 set_par(gspca_dev, 0x14000000);
267 if (gspca_dev->width > 320)
268 value = 0x8002e001; /* 640x480 */
269 else
270 value = 0x4001f000; /* 320x240 */
271 set_par(gspca_dev, value);
272 ret = usb_set_interface(gspca_dev->dev,
273 gspca_dev->iface,
274 gspca_dev->alt);
6a7eba24 275 if (ret < 0) {
133a9fe9
JP
276 pr_err("set intf %d %d failed\n",
277 gspca_dev->iface, gspca_dev->alt);
ecb77686 278 gspca_dev->usb_err = ret;
63eb9546 279 goto out;
6a7eba24 280 }
ecb77686 281 reg_r(gspca_dev, 0x0630);
739570bb 282 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
ecb77686 283 reg_r(gspca_dev, 0x0650);
63eb9546 284 snd_val(gspca_dev, 0x000020, 0xffffffff);
6a7eba24
JFM
285 reg_w(gspca_dev, 0x0620, 0);
286 reg_w(gspca_dev, 0x0630, 0);
287 reg_w(gspca_dev, 0x0640, 0);
288 reg_w(gspca_dev, 0x0650, 0);
289 reg_w(gspca_dev, 0x0660, 0);
63eb9546
JFM
290 set_par(gspca_dev, 0x09800000); /* Red ? */
291 set_par(gspca_dev, 0x0a800000); /* Green ? */
292 set_par(gspca_dev, 0x0b800000); /* Blue ? */
293 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
63eb9546
JFM
294
295 /* start the video flow */
296 set_par(gspca_dev, 0x01000000);
297 set_par(gspca_dev, 0x01000000);
ecb77686
JFM
298 if (gspca_dev->usb_err >= 0)
299 PDEBUG(D_STREAM, "camera started alt: 0x%02x",
300 gspca_dev->alt);
63eb9546 301out:
ecb77686 302 return gspca_dev->usb_err;
63eb9546
JFM
303}
304
305static void sd_stopN(struct gspca_dev *gspca_dev)
306{
307 struct usb_device *dev = gspca_dev->dev;
63eb9546
JFM
308
309 set_par(gspca_dev, 0x02000000);
310 set_par(gspca_dev, 0x02000000);
311 usb_set_interface(dev, gspca_dev->iface, 1);
739570bb
JFM
312 reg_r(gspca_dev, 0x0630);
313 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
314 reg_r(gspca_dev, 0x0650);
63eb9546 315 snd_val(gspca_dev, 0x000020, 0xffffffff);
6a7eba24
JFM
316 reg_w(gspca_dev, 0x0620, 0);
317 reg_w(gspca_dev, 0x0630, 0);
318 reg_w(gspca_dev, 0x0640, 0);
319 reg_w(gspca_dev, 0x0650, 0);
320 reg_w(gspca_dev, 0x0660, 0);
63eb9546
JFM
321 PDEBUG(D_STREAM, "camera stopped");
322}
323
63eb9546 324static void sd_pkt_scan(struct gspca_dev *gspca_dev,
76dd272b 325 u8 *data, /* isoc packet */
63eb9546
JFM
326 int len) /* iso packet length */
327{
71cb2764 328 struct sd *sd = (struct sd *) gspca_dev;
4aa0d037 329 static unsigned char ffd9[] = {0xff, 0xd9};
63eb9546
JFM
330
331 /* a frame starts with:
332 * - 0xff 0xfe
d43fa32f
JFM
333 * - 0x08 0x00 - length (little endian ?!)
334 * - 4 bytes = size of whole frame (BE - including header)
63eb9546
JFM
335 * - 0x00 0x0c
336 * - 0xff 0xd8
337 * - .. JPEG image with escape sequences (ff 00)
d43fa32f 338 * (without ending - ff d9)
63eb9546
JFM
339 */
340 if (data[0] == 0xff && data[1] == 0xfe) {
76dd272b
JFM
341 gspca_frame_add(gspca_dev, LAST_PACKET,
342 ffd9, 2);
63eb9546 343
d43fa32f 344 /* put the JPEG 411 header */
76dd272b 345 gspca_frame_add(gspca_dev, FIRST_PACKET,
71cb2764 346 sd->jpeg_hdr, JPEG_HDR_SZ);
63eb9546
JFM
347
348 /* beginning of the frame */
349#define STKHDRSZ 12
8843765e
JFM
350 data += STKHDRSZ;
351 len -= STKHDRSZ;
63eb9546 352 }
76dd272b 353 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
63eb9546
JFM
354}
355
77ac0baf
JFM
356static int sd_set_jcomp(struct gspca_dev *gspca_dev,
357 struct v4l2_jpegcompression *jcomp)
358{
359 struct sd *sd = (struct sd *) gspca_dev;
360
74dec797 361 v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
ecb77686 362 return gspca_dev->usb_err;
77ac0baf
JFM
363}
364
365static int sd_get_jcomp(struct gspca_dev *gspca_dev,
366 struct v4l2_jpegcompression *jcomp)
367{
368 struct sd *sd = (struct sd *) gspca_dev;
369
370 memset(jcomp, 0, sizeof *jcomp);
74dec797 371 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
77ac0baf
JFM
372 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
373 | V4L2_JPEG_MARKER_DQT;
374 return 0;
375}
376
74dec797
HV
377static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
378{
379 struct gspca_dev *gspca_dev =
380 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
381 struct sd *sd = (struct sd *)gspca_dev;
382
383 gspca_dev->usb_err = 0;
384
385 if (!gspca_dev->streaming)
386 return 0;
387
388 switch (ctrl->id) {
389 case V4L2_CID_BRIGHTNESS:
390 setbrightness(gspca_dev, ctrl->val);
391 break;
392 case V4L2_CID_CONTRAST:
393 setcontrast(gspca_dev, ctrl->val);
394 break;
395 case V4L2_CID_SATURATION:
396 setcolors(gspca_dev, ctrl->val);
397 break;
398 case V4L2_CID_POWER_LINE_FREQUENCY:
399 setlightfreq(gspca_dev, ctrl->val);
400 break;
401 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
402 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
403 break;
404 }
405 return gspca_dev->usb_err;
406}
407
408static const struct v4l2_ctrl_ops sd_ctrl_ops = {
409 .s_ctrl = sd_s_ctrl,
410};
411
412static int sd_init_controls(struct gspca_dev *gspca_dev)
413{
414 struct sd *sd = (struct sd *)gspca_dev;
415 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
416
417 gspca_dev->vdev.ctrl_handler = hdl;
418 v4l2_ctrl_handler_init(hdl, 5);
419 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
420 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
421 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
422 V4L2_CID_CONTRAST, 0, 255, 1, 127);
423 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
424 V4L2_CID_SATURATION, 0, 255, 1, 127);
425 v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
426 V4L2_CID_POWER_LINE_FREQUENCY,
427 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
428 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
429 sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
430 V4L2_CID_JPEG_COMPRESSION_QUALITY,
431 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
432
433 if (hdl->error) {
434 pr_err("Could not initialize controls\n");
435 return hdl->error;
436 }
437 return 0;
438}
439
63eb9546 440/* sub-driver description */
739570bb 441static const struct sd_desc sd_desc = {
63eb9546 442 .name = MODULE_NAME,
63eb9546 443 .config = sd_config,
012d6b02 444 .init = sd_init,
74dec797 445 .init_controls = sd_init_controls,
63eb9546
JFM
446 .start = sd_start,
447 .stopN = sd_stopN,
63eb9546 448 .pkt_scan = sd_pkt_scan,
77ac0baf
JFM
449 .get_jcomp = sd_get_jcomp,
450 .set_jcomp = sd_set_jcomp,
63eb9546
JFM
451};
452
453/* -- module initialisation -- */
95c967c1 454static const struct usb_device_id device_table[] = {
9d64fdb1 455 {USB_DEVICE(0x05e1, 0x0893)},
63eb9546
JFM
456 {}
457};
458MODULE_DEVICE_TABLE(usb, device_table);
459
460/* -- device connect -- */
461static int sd_probe(struct usb_interface *intf,
462 const struct usb_device_id *id)
463{
d43fa32f
JFM
464 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
465 THIS_MODULE);
63eb9546
JFM
466}
467
468static struct usb_driver sd_driver = {
469 .name = MODULE_NAME,
470 .id_table = device_table,
471 .probe = sd_probe,
472 .disconnect = gspca_disconnect,
6a709749
JFM
473#ifdef CONFIG_PM
474 .suspend = gspca_suspend,
475 .resume = gspca_resume,
476#endif
63eb9546
JFM
477};
478
ecb3b2b3 479module_usb_driver(sd_driver);
This page took 0.391208 seconds and 5 git commands to generate.