]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/gspca/stk014.c
V4L/DVB (8201): gspca: v4l2_pix_format in each subdriver.
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / gspca / stk014.c
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
26 #define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 5)
27 static const char version[] = "2.1.5";
28
29 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
30 MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
31 MODULE_LICENSE("GPL");
32
33 /* specific webcam descriptor */
34 struct sd {
35 struct gspca_dev gspca_dev; /* !! must be the first item */
36
37 unsigned char brightness;
38 unsigned char contrast;
39 unsigned char colors;
40 unsigned char lightfreq;
41 };
42
43 /* global parameters */
44 static int sd_quant = 7; /* <= 4 KO - 7: good (enough!) */
45
46 /* V4L2 controls supported by the driver */
47 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
48 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
49 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
50 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
51 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
52 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
53 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
54 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
55
56 static 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
115 static 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 -- */
129 static int reg_r(struct gspca_dev *gspca_dev,
130 __u16 index, __u8 *buf)
131 {
132 struct usb_device *dev = gspca_dev->dev;
133 int ret;
134
135 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
136 0x00,
137 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
138 0x00,
139 index,
140 buf, 1,
141 500);
142 if (ret < 0)
143 PDEBUG(D_ERR, "reg_r err %d", ret);
144 return ret;
145 }
146
147 /* -- write a register -- */
148 static int reg_w(struct gspca_dev *gspca_dev,
149 __u16 index, __u16 value)
150 {
151 struct usb_device *dev = gspca_dev->dev;
152 int ret;
153
154 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
155 0x01,
156 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
157 value,
158 index,
159 NULL,
160 0,
161 500);
162 if (ret < 0)
163 PDEBUG(D_ERR, "reg_w err %d", ret);
164 return ret;
165 }
166
167 /* -- get a value -- */
168 static int rcv_val(struct gspca_dev *gspca_dev,
169 int ads,
170 int len)
171 {
172 struct usb_device *dev = gspca_dev->dev;
173 int alen, ret;
174 unsigned char bulk_buf[4];
175
176 reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
177 reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
178 reg_w(gspca_dev, 0x636, ads & 0xff);
179 reg_w(gspca_dev, 0x637, 0);
180 reg_w(gspca_dev, 0x638, len & 0xff);
181 reg_w(gspca_dev, 0x639, len >> 8);
182 reg_w(gspca_dev, 0x63a, 0);
183 reg_w(gspca_dev, 0x63b, 0);
184 reg_w(gspca_dev, 0x630, 5);
185 if (len > sizeof bulk_buf)
186 return -1;
187 ret = usb_bulk_msg(dev,
188 usb_rcvbulkpipe(dev, 5),
189 bulk_buf,
190 len,
191 &alen,
192 500); /* timeout in milliseconds */
193 return ret;
194 }
195
196 /* -- send a value -- */
197 static int snd_val(struct gspca_dev *gspca_dev,
198 int ads,
199 unsigned int val)
200 {
201 struct usb_device *dev = gspca_dev->dev;
202 int alen, ret;
203 __u8 value, seq;
204 unsigned char bulk_buf[4];
205
206 if (ads == 0x003f08) {
207 ret = reg_r(gspca_dev, 0x0704, &value);
208 if (ret < 0)
209 goto ko;
210 ret = reg_r(gspca_dev, 0x0705, &seq);
211 if (ret < 0)
212 goto ko;
213 ret = reg_r(gspca_dev, 0x0650, &value);
214 if (ret < 0)
215 goto ko;
216 reg_w(gspca_dev, 0x654, seq);
217 } else
218 reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
219 reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
220 reg_w(gspca_dev, 0x656, ads & 0xff);
221 reg_w(gspca_dev, 0x657, 0);
222 reg_w(gspca_dev, 0x658, 0x04); /* size */
223 reg_w(gspca_dev, 0x659, 0);
224 reg_w(gspca_dev, 0x65a, 0);
225 reg_w(gspca_dev, 0x65b, 0);
226 reg_w(gspca_dev, 0x650, 5);
227 bulk_buf[0] = (val >> 24) & 0xff;
228 bulk_buf[1] = (val >> 16) & 0xff;
229 bulk_buf[2] = (val >> 8) & 0xff;
230 bulk_buf[3] = val & 0xff;
231 ret = usb_bulk_msg(dev,
232 usb_sndbulkpipe(dev, 6),
233 bulk_buf,
234 4,
235 &alen,
236 500); /* timeout in milliseconds */
237 if (ret < 0)
238 goto ko;
239 if (ads == 0x003f08) {
240 seq += 4;
241 seq &= 0x3f;
242 reg_w(gspca_dev, 0x705, seq);
243 }
244 return ret;
245 ko:
246 PDEBUG(D_ERR, "snd_val err %d", ret);
247 return ret;
248 }
249
250 /* set a camera parameter */
251 static int set_par(struct gspca_dev *gspca_dev,
252 int parval)
253 {
254 return snd_val(gspca_dev, 0x003f08, parval);
255 }
256
257 static void setbrightness(struct gspca_dev *gspca_dev)
258 {
259 struct sd *sd = (struct sd *) gspca_dev;
260 int parval;
261
262 parval = 0x06000000 /* whiteness */
263 + (sd->brightness << 16);
264 set_par(gspca_dev, parval);
265 }
266
267 static void setcontrast(struct gspca_dev *gspca_dev)
268 {
269 struct sd *sd = (struct sd *) gspca_dev;
270 int parval;
271
272 parval = 0x07000000 /* contrast */
273 + (sd->contrast << 16);
274 set_par(gspca_dev, parval);
275 }
276
277 static void setcolors(struct gspca_dev *gspca_dev)
278 {
279 struct sd *sd = (struct sd *) gspca_dev;
280 int parval;
281
282 parval = 0x08000000 /* saturation */
283 + (sd->colors << 16);
284 set_par(gspca_dev, parval);
285 }
286
287 static void setfreq(struct gspca_dev *gspca_dev)
288 {
289 struct sd *sd = (struct sd *) gspca_dev;
290
291 set_par(gspca_dev, sd->lightfreq == 1
292 ? 0x33640000 /* 50 Hz */
293 : 0x33780000); /* 60 Hz */
294 }
295
296 /* this function is called at probe time */
297 static int sd_config(struct gspca_dev *gspca_dev,
298 const struct usb_device_id *id)
299 {
300 struct sd *sd = (struct sd *) gspca_dev;
301 struct cam *cam = &gspca_dev->cam;
302
303 cam->dev_name = (char *) id->driver_info;
304 cam->epaddr = 0x02;
305 gspca_dev->cam.cam_mode = vga_mode;
306 gspca_dev->cam.nmodes = sizeof vga_mode / sizeof vga_mode[0];
307 sd->brightness = BRIGHTNESS_DEF;
308 sd->contrast = CONTRAST_DEF;
309 sd->colors = COLOR_DEF;
310 sd->lightfreq = FREQ_DEF;
311 return 0;
312 }
313
314 /* this function is called at open time */
315 static int sd_open(struct gspca_dev *gspca_dev)
316 {
317 __u8 value;
318 int ret;
319
320 /* check if the device responds */
321 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
322 ret = reg_r(gspca_dev, 0x0740, &value);
323 if (ret < 0)
324 return ret;
325 if (value != 0xff) {
326 PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", value);
327 return -1;
328 }
329 return 0;
330 }
331
332 /* -- start the camera -- */
333 static void sd_start(struct gspca_dev *gspca_dev)
334 {
335 __u8 dum;
336 int ret, value;
337
338 /* work on alternate 1 */
339 usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
340
341 set_par(gspca_dev, 0x10000000);
342 set_par(gspca_dev, 0x00000000);
343 set_par(gspca_dev, 0x8002e001);
344 set_par(gspca_dev, 0x14000000);
345 if (gspca_dev->width > 320)
346 value = 0x8002e001; /* 640x480 */
347 else
348 value = 0x4001f000; /* 320x240 */
349 set_par(gspca_dev, value);
350 ret = usb_set_interface(gspca_dev->dev,
351 gspca_dev->iface,
352 gspca_dev->alt);
353 if (ret < 0) {
354 PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
355 gspca_dev->iface, gspca_dev->alt);
356 goto out;
357 }
358 ret = reg_r(gspca_dev, 0x0630, &dum);
359 if (ret < 0)
360 goto out;
361 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
362 ret = reg_r(gspca_dev, 0x0650, &dum);
363 if (ret < 0)
364 goto out;
365 snd_val(gspca_dev, 0x000020, 0xffffffff);
366 reg_w(gspca_dev, 0x0620, 0);
367 reg_w(gspca_dev, 0x0630, 0);
368 reg_w(gspca_dev, 0x0640, 0);
369 reg_w(gspca_dev, 0x0650, 0);
370 reg_w(gspca_dev, 0x0660, 0);
371 setbrightness(gspca_dev); /* whiteness */
372 setcontrast(gspca_dev); /* contrast */
373 setcolors(gspca_dev); /* saturation */
374 set_par(gspca_dev, 0x09800000); /* Red ? */
375 set_par(gspca_dev, 0x0a800000); /* Green ? */
376 set_par(gspca_dev, 0x0b800000); /* Blue ? */
377 set_par(gspca_dev, 0x0d030000); /* Gamma ? */
378 setfreq(gspca_dev); /* light frequency */
379
380 /* start the video flow */
381 set_par(gspca_dev, 0x01000000);
382 set_par(gspca_dev, 0x01000000);
383 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
384 return;
385 out:
386 PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret);
387 }
388
389 static void sd_stopN(struct gspca_dev *gspca_dev)
390 {
391 struct usb_device *dev = gspca_dev->dev;
392 __u8 value;
393
394 set_par(gspca_dev, 0x02000000);
395 set_par(gspca_dev, 0x02000000);
396 usb_set_interface(dev, gspca_dev->iface, 1);
397 reg_r(gspca_dev, 0x0630, &value);
398 rcv_val(gspca_dev, 0x000020, 4); /* << (value ff ff ff ff) */
399 reg_r(gspca_dev, 0x0650, &value);
400 snd_val(gspca_dev, 0x000020, 0xffffffff);
401 reg_w(gspca_dev, 0x0620, 0);
402 reg_w(gspca_dev, 0x0630, 0);
403 reg_w(gspca_dev, 0x0640, 0);
404 reg_w(gspca_dev, 0x0650, 0);
405 reg_w(gspca_dev, 0x0660, 0);
406 PDEBUG(D_STREAM, "camera stopped");
407 }
408
409 static void sd_stop0(struct gspca_dev *gspca_dev)
410 {
411 }
412
413 static void sd_close(struct gspca_dev *gspca_dev)
414 {
415 }
416
417 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
418 struct gspca_frame *frame, /* target */
419 __u8 *data, /* isoc packet */
420 int len) /* iso packet length */
421 {
422 static unsigned char ffd9[] = {0xff, 0xd9};
423
424 /* a frame starts with:
425 * - 0xff 0xfe
426 * - 0x08 0x00 - length (little endian ?!)
427 * - 4 bytes = size of whole frame (BE - including header)
428 * - 0x00 0x0c
429 * - 0xff 0xd8
430 * - .. JPEG image with escape sequences (ff 00)
431 * (without ending - ff d9)
432 */
433 if (data[0] == 0xff && data[1] == 0xfe) {
434 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
435 ffd9, 2);
436
437 /* put the JPEG 411 header */
438 jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
439
440 /* beginning of the frame */
441 #define STKHDRSZ 12
442 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
443 data + STKHDRSZ, len - STKHDRSZ);
444 #undef STKHDRSZ
445 return;
446 }
447 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
448 }
449
450 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
451 {
452 struct sd *sd = (struct sd *) gspca_dev;
453
454 sd->brightness = val;
455 if (gspca_dev->streaming)
456 setbrightness(gspca_dev);
457 return 0;
458 }
459
460 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
461 {
462 struct sd *sd = (struct sd *) gspca_dev;
463
464 *val = sd->brightness;
465 return 0;
466 }
467
468 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
469 {
470 struct sd *sd = (struct sd *) gspca_dev;
471
472 sd->contrast = val;
473 if (gspca_dev->streaming)
474 setcontrast(gspca_dev);
475 return 0;
476 }
477
478 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
479 {
480 struct sd *sd = (struct sd *) gspca_dev;
481
482 *val = sd->contrast;
483 return 0;
484 }
485
486 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
487 {
488 struct sd *sd = (struct sd *) gspca_dev;
489
490 sd->colors = val;
491 if (gspca_dev->streaming)
492 setcolors(gspca_dev);
493 return 0;
494 }
495
496 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
497 {
498 struct sd *sd = (struct sd *) gspca_dev;
499
500 *val = sd->colors;
501 return 0;
502 }
503
504 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
505 {
506 struct sd *sd = (struct sd *) gspca_dev;
507
508 sd->lightfreq = val;
509 if (gspca_dev->streaming)
510 setfreq(gspca_dev);
511 return 0;
512 }
513
514 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
515 {
516 struct sd *sd = (struct sd *) gspca_dev;
517
518 *val = sd->lightfreq;
519 return 0;
520 }
521
522 static int sd_querymenu(struct gspca_dev *gspca_dev,
523 struct v4l2_querymenu *menu)
524 {
525 switch (menu->id) {
526 case V4L2_CID_POWER_LINE_FREQUENCY:
527 switch (menu->index) {
528 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
529 strcpy((char *) menu->name, "50 Hz");
530 return 0;
531 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
532 strcpy((char *) menu->name, "60 Hz");
533 return 0;
534 }
535 break;
536 }
537 return -EINVAL;
538 }
539
540 /* sub-driver description */
541 static struct sd_desc sd_desc = {
542 .name = MODULE_NAME,
543 .ctrls = sd_ctrls,
544 .nctrls = sizeof sd_ctrls / sizeof sd_ctrls[0],
545 .config = sd_config,
546 .open = sd_open,
547 .start = sd_start,
548 .stopN = sd_stopN,
549 .stop0 = sd_stop0,
550 .close = sd_close,
551 .pkt_scan = sd_pkt_scan,
552 .querymenu = sd_querymenu,
553 };
554
555 /* -- module initialisation -- */
556 #define DVNM(name) .driver_info = (kernel_ulong_t) name
557 static __devinitdata struct usb_device_id device_table[] = {
558 {USB_DEVICE(0x05e1, 0x0893), DVNM("Syntek DV4000")},
559 {}
560 };
561 MODULE_DEVICE_TABLE(usb, device_table);
562
563 /* -- device connect -- */
564 static int sd_probe(struct usb_interface *intf,
565 const struct usb_device_id *id)
566 {
567 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
568 THIS_MODULE);
569 }
570
571 static struct usb_driver sd_driver = {
572 .name = MODULE_NAME,
573 .id_table = device_table,
574 .probe = sd_probe,
575 .disconnect = gspca_disconnect,
576 };
577
578 /* -- module insert / remove -- */
579 static int __init sd_mod_init(void)
580 {
581 if (usb_register(&sd_driver) < 0)
582 return -1;
583 info("v%s registered", version);
584 return 0;
585 }
586 static void __exit sd_mod_exit(void)
587 {
588 usb_deregister(&sd_driver);
589 info("deregistered");
590 }
591
592 module_init(sd_mod_init);
593 module_exit(sd_mod_exit);
594
595 module_param_named(quant, sd_quant, int, 0644);
596 MODULE_PARM_DESC(quant, "Quantization index (0..8)");