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