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