/drivers/media/video/gspca/stk014.c
C | 631 lines | 513 code | 70 blank | 48 comment | 37 complexity | 9c196df090fafb70f0d12eecddd9a983 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
1/*
2 * Syntek DV4000 (STK014) subdriver
3 *
4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
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
19 */
20
21#define MODULE_NAME "stk014"
22
23#include "gspca.h"
24#include "jpeg.h"
25
26MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
27MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
28MODULE_LICENSE("GPL");
29
30/* specific webcam descriptor */
31struct sd {
32 struct gspca_dev gspca_dev; /* !! must be the first item */
33
34 unsigned char brightness;
35 unsigned char contrast;
36 unsigned char colors;
37 unsigned char lightfreq;
38 u8 quality;
39#define QUALITY_MIN 60
40#define QUALITY_MAX 95
41#define QUALITY_DEF 80
42
43 u8 *jpeg_hdr;
44};
45
46/* V4L2 controls supported by the driver */
47static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
48static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
49static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
50static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
51static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
52static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
53static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
54static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
55
56static const struct ctrl sd_ctrls[] = {
57 {
58 {
59 .id = V4L2_CID_BRIGHTNESS,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Brightness",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 1,
65#define BRIGHTNESS_DEF 127
66 .default_value = BRIGHTNESS_DEF,
67 },
68 .set = sd_setbrightness,
69 .get = sd_getbrightness,
70 },
71 {
72 {
73 .id = V4L2_CID_CONTRAST,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 .name = "Contrast",
76 .minimum = 0,
77 .maximum = 255,
78 .step = 1,
79#define CONTRAST_DEF 127
80 .default_value = CONTRAST_DEF,
81 },
82 .set = sd_setcontrast,
83 .get = sd_getcontrast,
84 },
85 {
86 {
87 .id = V4L2_CID_SATURATION,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Color",
90 .minimum = 0,
91 .maximum = 255,
92 .step = 1,
93#define COLOR_DEF 127
94 .default_value = COLOR_DEF,
95 },
96 .set = sd_setcolors,
97 .get = sd_getcolors,
98 },
99 {
100 {
101 .id = V4L2_CID_POWER_LINE_FREQUENCY,
102 .type = V4L2_CTRL_TYPE_MENU,
103 .name = "Light frequency filter",
104 .minimum = 1,
105 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
106 .step = 1,
107#define FREQ_DEF 1
108 .default_value = FREQ_DEF,
109 },
110 .set = sd_setfreq,
111 .get = sd_getfreq,
112 },
113};
114
115static const struct v4l2_pix_format vga_mode[] = {
116 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
117 .bytesperline = 320,
118 .sizeimage = 320 * 240 * 3 / 8 + 590,
119 .colorspace = V4L2_COLORSPACE_JPEG,
120 .priv = 1},
121 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
122 .bytesperline = 640,
123 .sizeimage = 640 * 480 * 3 / 8 + 590,
124 .colorspace = V4L2_COLORSPACE_JPEG,
125 .priv = 0},
126};
127
128/* -- read a register -- */
129static u8 reg_r(struct gspca_dev *gspca_dev,
130 __u16 index)
131{
132 struct usb_device *dev = gspca_dev->dev;
133 int ret;
134
135 if (gspca_dev->usb_err < 0)
136 return 0;
137 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
138 0x00,
139 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
140 0x00,
141 index,
142 gspca_dev->usb_buf, 1,
143 500);
144 if (ret < 0) {
145 PDEBUG(D_ERR, "reg_r err %d", ret);
146 gspca_dev->usb_err = ret;
147 return 0;
148 }
149 return gspca_dev->usb_buf[0];
150}
151
152/* -- write a register -- */
153static void reg_w(struct gspca_dev *gspca_dev,
154 __u16 index, __u16 value)
155{
156 struct usb_device *dev = gspca_dev->dev;
157 int ret;
158
159 if (gspca_dev->usb_err < 0)
160 return;
161 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
162 0x01,
163 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
164 value,
165 index,
166 NULL,
167 0,
168 500);
169 if (ret < 0) {
170 PDEBUG(D_ERR, "reg_w err %d", ret);
171 gspca_dev->usb_err = ret;
172 }
173}
174
175/* -- get a bulk value (4 bytes) -- */
176static void rcv_val(struct gspca_dev *gspca_dev,
177 int ads)
178{
179 struct usb_device *dev = gspca_dev->dev;
180 int alen, ret;
181
182 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
183 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
184 reg_w(gspca_dev, 0x636, ads & 0xff);
185 reg_w(gspca_dev, 0x637, 0);
186 reg_w(gspca_dev, 0x638, 4); /* len & 0xff */
187 reg_w(gspca_dev, 0x639, 0); /* len >> 8 */
188 reg_w(gspca_dev, 0x63a, 0);
189 reg_w(gspca_dev, 0x63b, 0);
190 reg_w(gspca_dev, 0x630, 5);
191 if (gspca_dev->usb_err < 0)
192 return;
193 ret = usb_bulk_msg(dev,
194 usb_rcvbulkpipe(dev, 0x05),
195 gspca_dev->usb_buf,
196 4, /* length */
197 &alen,
198 500); /* timeout in milliseconds */
199 if (ret < 0) {
200 PDEBUG(D_ERR, "rcv_val err %d", ret);
201 gspca_dev->usb_err = ret;
202 }
203}
204
205/* -- send a bulk value -- */
206static void snd_val(struct gspca_dev *gspca_dev,
207 int ads,
208 unsigned int val)
209{
210 struct usb_device *dev = gspca_dev->dev;
211 int alen, ret;
212 __u8 seq = 0;
213
214 if (ads == 0x003f08) {
215 reg_r(gspca_dev, 0x0704);
216 seq = reg_r(gspca_dev, 0x0705);
217 reg_r(gspca_dev, 0x0650);
218 reg_w(gspca_dev, 0x654, seq);
219 } else {
220 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
221 }
222 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
223 reg_w(gspca_dev, 0x656, ads & 0xff);
224 reg_w(gspca_dev, 0x657, 0);
225 reg_w(gspca_dev, 0x658, 0x04); /* size */
226 reg_w(gspca_dev, 0x659, 0);
227 reg_w(gspca_dev, 0x65a, 0);
228 reg_w(gspca_dev, 0x65b, 0);
229 reg_w(gspca_dev, 0x650, 5);
230 if (gspca_dev->usb_err < 0)
231 return;
232 gspca_dev->usb_buf[0] = val >> 24;
233 gspca_dev->usb_buf[1] = val >> 16;
234 gspca_dev->usb_buf[2] = val >> 8;
235 gspca_dev->usb_buf[3] = val;
236 ret = usb_bulk_msg(dev,
237 usb_sndbulkpipe(dev, 6),
238 gspca_dev->usb_buf,
239 4,
240 &alen,
241 500); /* timeout in milliseconds */
242 if (ret < 0) {
243 PDEBUG(D_ERR, "snd_val err %d", ret);
244 gspca_dev->usb_err = ret;
245 } else {
246 if (ads == 0x003f08) {
247 seq += 4;
248 seq &= 0x3f;
249 reg_w(gspca_dev, 0x705, seq);
250 }
251 }
252}
253
254/* set a camera parameter */
255static void set_par(struct gspca_dev *gspca_dev,
256 int parval)
257{
258 snd_val(gspca_dev, 0x003f08, parval);
259}
260
261static void setbrightness(struct gspca_dev *gspca_dev)
262{
263 struct sd *sd = (struct sd *) gspca_dev;
264 int parval;
265
266 parval = 0x06000000 /* whiteness */
267 + (sd->brightness << 16);
268 set_par(gspca_dev, parval);
269}
270
271static void setcontrast(struct gspca_dev *gspca_dev)
272{
273 struct sd *sd = (struct sd *) gspca_dev;
274 int parval;
275
276 parval = 0x07000000 /* contrast */
277 + (sd->contrast << 16);
278 set_par(gspca_dev, parval);
279}
280
281static void setcolors(struct gspca_dev *gspca_dev)
282{
283 struct sd *sd = (struct sd *) gspca_dev;
284 int parval;
285
286 parval = 0x08000000 /* saturation */
287 + (sd->colors << 16);
288 set_par(gspca_dev, parval);
289}
290
291static void setfreq(struct gspca_dev *gspca_dev)
292{
293 struct sd *sd = (struct sd *) gspca_dev;
294
295 set_par(gspca_dev, sd->lightfreq == 1
296 ? 0x33640000 /* 50 Hz */
297 : 0x33780000); /* 60 Hz */
298}
299
300/* this function is called at probe time */
301static int sd_config(struct gspca_dev *gspca_dev,
302 const struct usb_device_id *id)
303{
304 struct sd *sd = (struct sd *) gspca_dev;
305
306 gspca_dev->cam.cam_mode = vga_mode;
307 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
308 sd->brightness = BRIGHTNESS_DEF;
309 sd->contrast = CONTRAST_DEF;
310 sd->colors = COLOR_DEF;
311 sd->lightfreq = FREQ_DEF;
312 sd->quality = QUALITY_DEF;
313 return 0;
314}
315
316/* this function is called at probe and resume time */
317static int sd_init(struct gspca_dev *gspca_dev)
318{
319 u8 ret;
320
321 /* check if the device responds */
322 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
323 ret = reg_r(gspca_dev, 0x0740);
324 if (gspca_dev->usb_err >= 0) {
325 if (ret != 0xff) {
326 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
327 gspca_dev->usb_err = -EIO;
328 }
329 }
330 return gspca_dev->usb_err;
331}
332
333/* -- start the camera -- */
334static int sd_start(struct gspca_dev *gspca_dev)
335{
336 struct sd *sd = (struct sd *) gspca_dev;
337 int ret, value;
338
339 /* create the JPEG header */
340 sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
341 if (!sd->jpeg_hdr)
342 return -ENOMEM;
343 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
344 0x22); /* JPEG 411 */
345 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
346
347 /* work on alternate 1 */
348 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
349
350 set_par(gspca_dev, 0x10000000);
351 set_par(gspca_dev, 0x00000000);
352 set_par(gspca_dev, 0x8002e001);
353 set_par(gspca_dev, 0x14000000);
354 if (gspca_dev->width > 320)
355 value = 0x8002e001; /* 640x480 */
356 else
357 value = 0x4001f000; /* 320x240 */
358 set_par(gspca_dev, value);
359 ret = usb_set_interface(gspca_dev->dev,
360 gspca_dev->iface,
361 gspca_dev->alt);
362 if (ret < 0) {
363 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
364 gspca_dev->iface, gspca_dev->alt);
365 gspca_dev->usb_err = ret;
366 goto out;
367 }
368 reg_r(gspca_dev, 0x0630);
369 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
370 reg_r(gspca_dev, 0x0650);
371 snd_val(gspca_dev, 0x000020, 0xffffffff);
372 reg_w(gspca_dev, 0x0620, 0);
373 reg_w(gspca_dev, 0x0630, 0);
374 reg_w(gspca_dev, 0x0640, 0);
375 reg_w(gspca_dev, 0x0650, 0);
376 reg_w(gspca_dev, 0x0660, 0);
377 setbrightness(gspca_dev); /* whiteness */
378 setcontrast(gspca_dev); /* contrast */
379 setcolors(gspca_dev); /* saturation */
380 set_par(gspca_dev, 0x09800000); /* Red ? */
381 set_par(gspca_dev, 0x0a800000); /* Green ? */
382 set_par(gspca_dev, 0x0b800000); /* Blue ? */
383 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
384 setfreq(gspca_dev); /* light frequency */
385
386 /* start the video flow */
387 set_par(gspca_dev, 0x01000000);
388 set_par(gspca_dev, 0x01000000);
389 if (gspca_dev->usb_err >= 0)
390 PDEBUG(D_STREAM, "camera started alt: 0x%02x",
391 gspca_dev->alt);
392out:
393 return gspca_dev->usb_err;
394}
395
396static void sd_stopN(struct gspca_dev *gspca_dev)
397{
398 struct usb_device *dev = gspca_dev->dev;
399
400 set_par(gspca_dev, 0x02000000);
401 set_par(gspca_dev, 0x02000000);
402 usb_set_interface(dev, gspca_dev->iface, 1);
403 reg_r(gspca_dev, 0x0630);
404 rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */
405 reg_r(gspca_dev, 0x0650);
406 snd_val(gspca_dev, 0x000020, 0xffffffff);
407 reg_w(gspca_dev, 0x0620, 0);
408 reg_w(gspca_dev, 0x0630, 0);
409 reg_w(gspca_dev, 0x0640, 0);
410 reg_w(gspca_dev, 0x0650, 0);
411 reg_w(gspca_dev, 0x0660, 0);
412 PDEBUG(D_STREAM, "camera stopped");
413}
414
415static void sd_stop0(struct gspca_dev *gspca_dev)
416{
417 struct sd *sd = (struct sd *) gspca_dev;
418
419 kfree(sd->jpeg_hdr);
420}
421
422static void sd_pkt_scan(struct gspca_dev *gspca_dev,
423 u8 *data, /* isoc packet */
424 int len) /* iso packet length */
425{
426 struct sd *sd = (struct sd *) gspca_dev;
427 static unsigned char ffd9[] = {0xff, 0xd9};
428
429 /* a frame starts with:
430 * - 0xff 0xfe
431 * - 0x08 0x00 - length (little endian ?!)
432 * - 4 bytes = size of whole frame (BE - including header)
433 * - 0x00 0x0c
434 * - 0xff 0xd8
435 * - .. JPEG image with escape sequences (ff 00)
436 * (without ending - ff d9)
437 */
438 if (data[0] == 0xff && data[1] == 0xfe) {
439 gspca_frame_add(gspca_dev, LAST_PACKET,
440 ffd9, 2);
441
442 /* put the JPEG 411 header */
443 gspca_frame_add(gspca_dev, FIRST_PACKET,
444 sd->jpeg_hdr, JPEG_HDR_SZ);
445
446 /* beginning of the frame */
447#define STKHDRSZ 12
448 data += STKHDRSZ;
449 len -= STKHDRSZ;
450 }
451 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
452}
453
454static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
455{
456 struct sd *sd = (struct sd *) gspca_dev;
457
458 sd->brightness = val;
459 if (gspca_dev->streaming)
460 setbrightness(gspca_dev);
461 return gspca_dev->usb_err;
462}
463
464static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
465{
466 struct sd *sd = (struct sd *) gspca_dev;
467
468 *val = sd->brightness;
469 return 0;
470}
471
472static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
473{
474 struct sd *sd = (struct sd *) gspca_dev;
475
476 sd->contrast = val;
477 if (gspca_dev->streaming)
478 setcontrast(gspca_dev);
479 return gspca_dev->usb_err;
480}
481
482static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
483{
484 struct sd *sd = (struct sd *) gspca_dev;
485
486 *val = sd->contrast;
487 return 0;
488}
489
490static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
491{
492 struct sd *sd = (struct sd *) gspca_dev;
493
494 sd->colors = val;
495 if (gspca_dev->streaming)
496 setcolors(gspca_dev);
497 return gspca_dev->usb_err;
498}
499
500static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
501{
502 struct sd *sd = (struct sd *) gspca_dev;
503
504 *val = sd->colors;
505 return 0;
506}
507
508static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
509{
510 struct sd *sd = (struct sd *) gspca_dev;
511
512 sd->lightfreq = val;
513 if (gspca_dev->streaming)
514 setfreq(gspca_dev);
515 return gspca_dev->usb_err;
516}
517
518static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
519{
520 struct sd *sd = (struct sd *) gspca_dev;
521
522 *val = sd->lightfreq;
523 return 0;
524}
525
526static int sd_querymenu(struct gspca_dev *gspca_dev,
527 struct v4l2_querymenu *menu)
528{
529 switch (menu->id) {
530 case V4L2_CID_POWER_LINE_FREQUENCY:
531 switch (menu->index) {
532 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
533 strcpy((char *) menu->name, "50 Hz");
534 return 0;
535 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
536 strcpy((char *) menu->name, "60 Hz");
537 return 0;
538 }
539 break;
540 }
541 return -EINVAL;
542}
543
544static int sd_set_jcomp(struct gspca_dev *gspca_dev,
545 struct v4l2_jpegcompression *jcomp)
546{
547 struct sd *sd = (struct sd *) gspca_dev;
548
549 if (jcomp->quality < QUALITY_MIN)
550 sd->quality = QUALITY_MIN;
551 else if (jcomp->quality > QUALITY_MAX)
552 sd->quality = QUALITY_MAX;
553 else
554 sd->quality = jcomp->quality;
555 if (gspca_dev->streaming)
556 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
557 return gspca_dev->usb_err;
558}
559
560static int sd_get_jcomp(struct gspca_dev *gspca_dev,
561 struct v4l2_jpegcompression *jcomp)
562{
563 struct sd *sd = (struct sd *) gspca_dev;
564
565 memset(jcomp, 0, sizeof *jcomp);
566 jcomp->quality = sd->quality;
567 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
568 | V4L2_JPEG_MARKER_DQT;
569 return 0;
570}
571
572/* sub-driver description */
573static const struct sd_desc sd_desc = {
574 .name = MODULE_NAME,
575 .ctrls = sd_ctrls,
576 .nctrls = ARRAY_SIZE(sd_ctrls),
577 .config = sd_config,
578 .init = sd_init,
579 .start = sd_start,
580 .stopN = sd_stopN,
581 .stop0 = sd_stop0,
582 .pkt_scan = sd_pkt_scan,
583 .querymenu = sd_querymenu,
584 .get_jcomp = sd_get_jcomp,
585 .set_jcomp = sd_set_jcomp,
586};
587
588/* -- module initialisation -- */
589static const __devinitdata struct usb_device_id device_table[] = {
590 {USB_DEVICE(0x05e1, 0x0893)},
591 {}
592};
593MODULE_DEVICE_TABLE(usb, device_table);
594
595/* -- device connect -- */
596static int sd_probe(struct usb_interface *intf,
597 const struct usb_device_id *id)
598{
599 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
600 THIS_MODULE);
601}
602
603static struct usb_driver sd_driver = {
604 .name = MODULE_NAME,
605 .id_table = device_table,
606 .probe = sd_probe,
607 .disconnect = gspca_disconnect,
608#ifdef CONFIG_PM
609 .suspend = gspca_suspend,
610 .resume = gspca_resume,
611#endif
612};
613
614/* -- module insert / remove -- */
615static int __init sd_mod_init(void)
616{
617 int ret;
618 ret = usb_register(&sd_driver);
619 if (ret < 0)
620 return ret;
621 info("registered");
622 return 0;
623}
624static void __exit sd_mod_exit(void)
625{
626 usb_deregister(&sd_driver);
627 info("deregistered");
628}
629
630module_init(sd_mod_init);
631module_exit(sd_mod_exit);