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