]> Pileus Git - ~andy/linux/blob - drivers/media/video/gspca/spca505.c
1bb23d03f048332f906a6f1be8a3f331a0f79fe6
[~andy/linux] / drivers / media / video / gspca / spca505.c
1 /*
2  * SPCA505 chip based cameras initialization data
3  *
4  * V4L2 by Jean-Francis 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
22 #define MODULE_NAME "spca505"
23
24 #include "gspca.h"
25
26 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
27 MODULE_DESCRIPTION("GSPCA/SPCA505 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         int buflen;
35         unsigned char tmpbuf[640 * 480 * 3 / 2]; /* YYUV per line */
36         unsigned char tmpbuf2[640 * 480 * 2];   /* YUYV */
37
38         unsigned char brightness;
39
40         char subtype;
41 #define IntelPCCameraPro 0
42 #define Nxultra 1
43 };
44
45 /* V4L2 controls supported by the driver */
46 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
47 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
48
49 static struct ctrl sd_ctrls[] = {
50 #define SD_BRIGHTNESS 0
51         {
52             {
53                 .id      = V4L2_CID_BRIGHTNESS,
54                 .type    = V4L2_CTRL_TYPE_INTEGER,
55                 .name    = "Brightness",
56                 .minimum = 0,
57                 .maximum = 255,
58                 .step    = 1,
59                 .default_value = 127,
60             },
61             .set = sd_setbrightness,
62             .get = sd_getbrightness,
63         },
64 };
65
66 static struct v4l2_pix_format vga_mode[] = {
67         {160, 120, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
68                 .bytesperline = 160 * 2,
69                 .sizeimage = 160 * 120 * 2,
70                 .colorspace = V4L2_COLORSPACE_SRGB,
71                 .priv = 5},
72         {176, 144, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
73                 .bytesperline = 176 * 2,
74                 .sizeimage = 176 * 144 * 2,
75                 .colorspace = V4L2_COLORSPACE_SRGB,
76                 .priv = 4},
77         {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
78                 .bytesperline = 320 * 2,
79                 .sizeimage = 320 * 240 * 2,
80                 .colorspace = V4L2_COLORSPACE_SRGB,
81                 .priv = 2},
82         {352, 288, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
83                 .bytesperline = 352 * 2,
84                 .sizeimage = 352 * 288 * 2,
85                 .colorspace = V4L2_COLORSPACE_SRGB,
86                 .priv = 1},
87         {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
88                 .bytesperline = 640 * 2,
89                 .sizeimage = 640 * 480 * 2,
90                 .colorspace = V4L2_COLORSPACE_SRGB,
91                 .priv = 0},
92 };
93
94 #define SPCA50X_OFFSET_DATA 10
95
96 #define SPCA50X_REG_USB 0x02    /* spca505 501 */
97
98 #define SPCA50X_USB_CTRL 0x00   /* spca505 */
99 #define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
100 #define SPCA50X_REG_GLOBAL 0x03 /* spca505 */
101 #define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
102 #define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
103
104 #define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
105 #define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
106 #define SPCA50X_GMISC3_SAA7113RST 0x20  /* Not sure about this one spca505 */
107
108 /*
109  * Data to initialize a SPCA505. Common to the CCD and external modes
110  */
111 static const __u16 spca505_init_data[][3] = {
112         /* line    bmRequest,value,index */
113         /* 1819 */
114         {SPCA50X_REG_GLOBAL, SPCA50X_GMISC3_SAA7113RST, SPCA50X_GLOBAL_MISC3},
115         /* Sensor reset */
116         /* 1822 */ {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC3},
117         /* 1825 */ {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC1},
118         /* Block USB reset */
119         /* 1828 */ {SPCA50X_REG_GLOBAL, SPCA50X_GMISC0_IDSEL,
120                 SPCA50X_GLOBAL_MISC0},
121
122         /* 1831 */ {0x5, 0x01, 0x10},
123                                         /* Maybe power down some stuff */
124         /* 1834 */ {0x5, 0x0f, 0x11},
125
126         /* Setup internal CCD  ? */
127         /* 1837 */ {0x6, 0x10, 0x08},
128         /* 1840 */ {0x6, 0x00, 0x09},
129         /* 1843 */ {0x6, 0x00, 0x0a},
130         /* 1846 */ {0x6, 0x00, 0x0b},
131         /* 1849 */ {0x6, 0x10, 0x0c},
132         /* 1852 */ {0x6, 0x00, 0x0d},
133         /* 1855 */ {0x6, 0x00, 0x0e},
134         /* 1858 */ {0x6, 0x00, 0x0f},
135         /* 1861 */ {0x6, 0x10, 0x10},
136         /* 1864 */ {0x6, 0x02, 0x11},
137         /* 1867 */ {0x6, 0x00, 0x12},
138         /* 1870 */ {0x6, 0x04, 0x13},
139         /* 1873 */ {0x6, 0x02, 0x14},
140         /* 1876 */ {0x6, 0x8a, 0x51},
141         /* 1879 */ {0x6, 0x40, 0x52},
142         /* 1882 */ {0x6, 0xb6, 0x53},
143         /* 1885 */ {0x6, 0x3d, 0x54},
144         {}
145 };
146
147 /*
148  * Data to initialize the camera using the internal CCD
149  */
150 static const __u16 spca505_open_data_ccd[][3] = {
151         /* line    bmRequest,value,index */
152         /* Internal CCD data set */
153         /* 1891 */ {0x3, 0x04, 0x01},
154         /* This could be a reset */
155         /* 1894 */ {0x3, 0x00, 0x01},
156
157         /* Setup compression and image registers. 0x6 and 0x7 seem to be
158            related to H&V hold, and are resolution mode specific */
159                 /* 1897 */ {0x4, 0x10, 0x01},
160                 /* DIFF(0x50), was (0x10) */
161         /* 1900 */ {0x4, 0x00, 0x04},
162         /* 1903 */ {0x4, 0x00, 0x05},
163         /* 1906 */ {0x4, 0x20, 0x06},
164         /* 1909 */ {0x4, 0x20, 0x07},
165
166         /* 1912 */ {0x8, 0x0a, 0x00},
167         /* DIFF (0x4a), was (0xa) */
168
169         /* 1915 */ {0x5, 0x00, 0x10},
170         /* 1918 */ {0x5, 0x00, 0x11},
171         /* 1921 */ {0x5, 0x00, 0x00},
172         /* DIFF not written */
173         /* 1924 */ {0x5, 0x00, 0x01},
174         /* DIFF not written */
175         /* 1927 */ {0x5, 0x00, 0x02},
176         /* DIFF not written */
177         /* 1930 */ {0x5, 0x00, 0x03},
178         /* DIFF not written */
179         /* 1933 */ {0x5, 0x00, 0x04},
180         /* DIFF not written */
181                 /* 1936 */ {0x5, 0x80, 0x05},
182                 /* DIFF not written */
183                 /* 1939 */ {0x5, 0xe0, 0x06},
184                 /* DIFF not written */
185                 /* 1942 */ {0x5, 0x20, 0x07},
186                 /* DIFF not written */
187                 /* 1945 */ {0x5, 0xa0, 0x08},
188                 /* DIFF not written */
189                 /* 1948 */ {0x5, 0x0, 0x12},
190                 /* DIFF not written */
191         /* 1951 */ {0x5, 0x02, 0x0f},
192         /* DIFF not written */
193                 /* 1954 */ {0x5, 0x10, 0x46},
194                 /* DIFF not written */
195                 /* 1957 */ {0x5, 0x8, 0x4a},
196                 /* DIFF not written */
197
198         /* 1960 */ {0x3, 0x08, 0x03},
199         /* DIFF (0x3,0x28,0x3) */
200         /* 1963 */ {0x3, 0x08, 0x01},
201         /* 1966 */ {0x3, 0x0c, 0x03},
202         /* DIFF not written */
203                 /* 1969 */ {0x3, 0x21, 0x00},
204                 /* DIFF (0x39) */
205
206 /* Extra block copied from init to hopefully ensure CCD is in a sane state */
207         /* 1837 */ {0x6, 0x10, 0x08},
208         /* 1840 */ {0x6, 0x00, 0x09},
209         /* 1843 */ {0x6, 0x00, 0x0a},
210         /* 1846 */ {0x6, 0x00, 0x0b},
211         /* 1849 */ {0x6, 0x10, 0x0c},
212         /* 1852 */ {0x6, 0x00, 0x0d},
213         /* 1855 */ {0x6, 0x00, 0x0e},
214         /* 1858 */ {0x6, 0x00, 0x0f},
215         /* 1861 */ {0x6, 0x10, 0x10},
216         /* 1864 */ {0x6, 0x02, 0x11},
217         /* 1867 */ {0x6, 0x00, 0x12},
218         /* 1870 */ {0x6, 0x04, 0x13},
219         /* 1873 */ {0x6, 0x02, 0x14},
220         /* 1876 */ {0x6, 0x8a, 0x51},
221         /* 1879 */ {0x6, 0x40, 0x52},
222         /* 1882 */ {0x6, 0xb6, 0x53},
223         /* 1885 */ {0x6, 0x3d, 0x54},
224         /* End of extra block */
225
226                 /* 1972 */ {0x6, 0x3f, 0x1},
227                 /* Block skipped */
228         /* 1975 */ {0x6, 0x10, 0x02},
229         /* 1978 */ {0x6, 0x64, 0x07},
230         /* 1981 */ {0x6, 0x10, 0x08},
231         /* 1984 */ {0x6, 0x00, 0x09},
232         /* 1987 */ {0x6, 0x00, 0x0a},
233         /* 1990 */ {0x6, 0x00, 0x0b},
234         /* 1993 */ {0x6, 0x10, 0x0c},
235         /* 1996 */ {0x6, 0x00, 0x0d},
236         /* 1999 */ {0x6, 0x00, 0x0e},
237         /* 2002 */ {0x6, 0x00, 0x0f},
238         /* 2005 */ {0x6, 0x10, 0x10},
239         /* 2008 */ {0x6, 0x02, 0x11},
240         /* 2011 */ {0x6, 0x00, 0x12},
241         /* 2014 */ {0x6, 0x04, 0x13},
242         /* 2017 */ {0x6, 0x02, 0x14},
243         /* 2020 */ {0x6, 0x8a, 0x51},
244         /* 2023 */ {0x6, 0x40, 0x52},
245         /* 2026 */ {0x6, 0xb6, 0x53},
246         /* 2029 */ {0x6, 0x3d, 0x54},
247         /* 2032 */ {0x6, 0x60, 0x57},
248         /* 2035 */ {0x6, 0x20, 0x58},
249         /* 2038 */ {0x6, 0x15, 0x59},
250         /* 2041 */ {0x6, 0x05, 0x5a},
251
252         /* 2044 */ {0x5, 0x01, 0xc0},
253         /* 2047 */ {0x5, 0x10, 0xcb},
254                 /* 2050 */ {0x5, 0x80, 0xc1},
255                 /* */
256                 /* 2053 */ {0x5, 0x0, 0xc2},
257                 /* 4 was 0 */
258         /* 2056 */ {0x5, 0x00, 0xca},
259                 /* 2059 */ {0x5, 0x80, 0xc1},
260                 /*  */
261         /* 2062 */ {0x5, 0x04, 0xc2},
262         /* 2065 */ {0x5, 0x00, 0xca},
263                 /* 2068 */ {0x5, 0x0, 0xc1},
264                 /*  */
265         /* 2071 */ {0x5, 0x00, 0xc2},
266         /* 2074 */ {0x5, 0x00, 0xca},
267                 /* 2077 */ {0x5, 0x40, 0xc1},
268                 /* */
269         /* 2080 */ {0x5, 0x17, 0xc2},
270         /* 2083 */ {0x5, 0x00, 0xca},
271                 /* 2086 */ {0x5, 0x80, 0xc1},
272                 /* */
273         /* 2089 */ {0x5, 0x06, 0xc2},
274         /* 2092 */ {0x5, 0x00, 0xca},
275                 /* 2095 */ {0x5, 0x80, 0xc1},
276                 /* */
277         /* 2098 */ {0x5, 0x04, 0xc2},
278         /* 2101 */ {0x5, 0x00, 0xca},
279
280         /* 2104 */ {0x3, 0x4c, 0x3},
281         /* 2107 */ {0x3, 0x18, 0x1},
282
283         /* 2110 */ {0x6, 0x70, 0x51},
284         /* 2113 */ {0x6, 0xbe, 0x53},
285         /* 2116 */ {0x6, 0x71, 0x57},
286         /* 2119 */ {0x6, 0x20, 0x58},
287         /* 2122 */ {0x6, 0x05, 0x59},
288         /* 2125 */ {0x6, 0x15, 0x5a},
289
290         /* 2128 */ {0x4, 0x00, 0x08},
291         /* Compress = OFF (0x1 to turn on) */
292         /* 2131 */ {0x4, 0x12, 0x09},
293         /* 2134 */ {0x4, 0x21, 0x0a},
294         /* 2137 */ {0x4, 0x10, 0x0b},
295         /* 2140 */ {0x4, 0x21, 0x0c},
296         /* 2143 */ {0x4, 0x05, 0x00},
297         /* was 5 (Image Type ? ) */
298         /* 2146 */ {0x4, 0x00, 0x01},
299
300         /* 2149 */ {0x6, 0x3f, 0x01},
301
302         /* 2152 */ {0x4, 0x00, 0x04},
303         /* 2155 */ {0x4, 0x00, 0x05},
304         /* 2158 */ {0x4, 0x40, 0x06},
305         /* 2161 */ {0x4, 0x40, 0x07},
306
307         /* 2164 */ {0x6, 0x1c, 0x17},
308         /* 2167 */ {0x6, 0xe2, 0x19},
309         /* 2170 */ {0x6, 0x1c, 0x1b},
310         /* 2173 */ {0x6, 0xe2, 0x1d},
311         /* 2176 */ {0x6, 0xaa, 0x1f},
312         /* 2179 */ {0x6, 0x70, 0x20},
313
314         /* 2182 */ {0x5, 0x01, 0x10},
315         /* 2185 */ {0x5, 0x00, 0x11},
316         /* 2188 */ {0x5, 0x01, 0x00},
317         /* 2191 */ {0x5, 0x05, 0x01},
318                 /* 2194 */ {0x5, 0x00, 0xc1},
319                 /* */
320         /* 2197 */ {0x5, 0x00, 0xc2},
321         /* 2200 */ {0x5, 0x00, 0xca},
322
323         /* 2203 */ {0x6, 0x70, 0x51},
324         /* 2206 */ {0x6, 0xbe, 0x53},
325         {}
326 };
327
328 /*
329    Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
330  * SPCA505b chip based cameras initialization data
331  *
332  */
333 /* jfm */
334 #define initial_brightness 0x7f /* 0x0(white)-0xff(black) */
335 /* #define initial_brightness 0x0       //0x0(white)-0xff(black) */
336 /*
337  * Data to initialize a SPCA505. Common to the CCD and external modes
338  */
339 static const __u16 spca505b_init_data[][3] = {
340 /* start */
341         {0x02, 0x00, 0x00},             /* init */
342         {0x02, 0x00, 0x01},
343         {0x02, 0x00, 0x02},
344         {0x02, 0x00, 0x03},
345         {0x02, 0x00, 0x04},
346         {0x02, 0x00, 0x05},
347         {0x02, 0x00, 0x06},
348         {0x02, 0x00, 0x07},
349         {0x02, 0x00, 0x08},
350         {0x02, 0x00, 0x09},
351         {0x03, 0x00, 0x00},
352         {0x03, 0x00, 0x01},
353         {0x03, 0x00, 0x02},
354         {0x03, 0x00, 0x03},
355         {0x03, 0x00, 0x04},
356         {0x03, 0x00, 0x05},
357         {0x03, 0x00, 0x06},
358         {0x04, 0x00, 0x00},
359         {0x04, 0x00, 0x02},
360         {0x04, 0x00, 0x04},
361         {0x04, 0x00, 0x05},
362         {0x04, 0x00, 0x06},
363         {0x04, 0x00, 0x07},
364         {0x04, 0x00, 0x08},
365         {0x04, 0x00, 0x09},
366         {0x04, 0x00, 0x0a},
367         {0x04, 0x00, 0x0b},
368         {0x04, 0x00, 0x0c},
369         {0x07, 0x00, 0x00},
370         {0x07, 0x00, 0x03},
371         {0x08, 0x00, 0x00},
372         {0x08, 0x00, 0x01},
373         {0x08, 0x00, 0x02},
374         {0x00, 0x01, 0x00},
375         {0x00, 0x01, 0x01},
376         {0x00, 0x01, 0x34},
377         {0x00, 0x01, 0x35},
378         {0x06, 0x18, 0x08},
379         {0x06, 0xfc, 0x09},
380         {0x06, 0xfc, 0x0a},
381         {0x06, 0xfc, 0x0b},
382         {0x06, 0x18, 0x0c},
383         {0x06, 0xfc, 0x0d},
384         {0x06, 0xfc, 0x0e},
385         {0x06, 0xfc, 0x0f},
386         {0x06, 0x18, 0x10},
387         {0x06, 0xfe, 0x12},
388         {0x06, 0x00, 0x11},
389         {0x06, 0x00, 0x14},
390         {0x06, 0x00, 0x13},
391         {0x06, 0x28, 0x51},
392         {0x06, 0xff, 0x53},
393         {0x02, 0x00, 0x08},
394
395         {0x03, 0x00, 0x03},
396         {0x03, 0x10, 0x03},
397         {}
398 };
399
400 /*
401  * Data to initialize the camera using the internal CCD
402  */
403 static const __u16 spca505b_open_data_ccd[][3] = {
404
405 /* {0x02,0x00,0x00}, */
406         {0x03, 0x04, 0x01},             /* rst */
407         {0x03, 0x00, 0x01},
408         {0x03, 0x00, 0x00},
409         {0x03, 0x21, 0x00},
410         {0x03, 0x00, 0x04},
411         {0x03, 0x00, 0x03},
412         {0x03, 0x18, 0x03},
413         {0x03, 0x08, 0x01},
414         {0x03, 0x1c, 0x03},
415         {0x03, 0x5c, 0x03},
416         {0x03, 0x5c, 0x03},
417         {0x03, 0x18, 0x01},
418
419 /* same as 505 */
420         {0x04, 0x10, 0x01},
421         {0x04, 0x00, 0x04},
422         {0x04, 0x00, 0x05},
423         {0x04, 0x20, 0x06},
424         {0x04, 0x20, 0x07},
425
426         {0x08, 0x0a, 0x00},
427
428         {0x05, 0x00, 0x10},
429         {0x05, 0x00, 0x11},
430         {0x05, 0x00, 0x12},
431         {0x05, 0x6f, 0x00},
432         {0x05, initial_brightness >> 6, 0x00},
433         {0x05, initial_brightness << 2, 0x01},
434         {0x05, 0x00, 0x02},
435         {0x05, 0x01, 0x03},
436         {0x05, 0x00, 0x04},
437         {0x05, 0x03, 0x05},
438         {0x05, 0xe0, 0x06},
439         {0x05, 0x20, 0x07},
440         {0x05, 0xa0, 0x08},
441         {0x05, 0x00, 0x12},
442         {0x05, 0x02, 0x0f},
443         {0x05, 128, 0x14},              /* max exposure off (0=on) */
444         {0x05, 0x01, 0xb0},
445         {0x05, 0x01, 0xbf},
446         {0x03, 0x02, 0x06},
447         {0x05, 0x10, 0x46},
448         {0x05, 0x08, 0x4a},
449
450         {0x06, 0x00, 0x01},
451         {0x06, 0x10, 0x02},
452         {0x06, 0x64, 0x07},
453         {0x06, 0x18, 0x08},
454         {0x06, 0xfc, 0x09},
455         {0x06, 0xfc, 0x0a},
456         {0x06, 0xfc, 0x0b},
457         {0x04, 0x00, 0x01},
458         {0x06, 0x18, 0x0c},
459         {0x06, 0xfc, 0x0d},
460         {0x06, 0xfc, 0x0e},
461         {0x06, 0xfc, 0x0f},
462         {0x06, 0x11, 0x10},             /* contrast */
463         {0x06, 0x00, 0x11},
464         {0x06, 0xfe, 0x12},
465         {0x06, 0x00, 0x13},
466         {0x06, 0x00, 0x14},
467         {0x06, 0x9d, 0x51},
468         {0x06, 0x40, 0x52},
469         {0x06, 0x7c, 0x53},
470         {0x06, 0x40, 0x54},
471         {0x06, 0x02, 0x57},
472         {0x06, 0x03, 0x58},
473         {0x06, 0x15, 0x59},
474         {0x06, 0x05, 0x5a},
475         {0x06, 0x03, 0x56},
476         {0x06, 0x02, 0x3f},
477         {0x06, 0x00, 0x40},
478         {0x06, 0x39, 0x41},
479         {0x06, 0x69, 0x42},
480         {0x06, 0x87, 0x43},
481         {0x06, 0x9e, 0x44},
482         {0x06, 0xb1, 0x45},
483         {0x06, 0xbf, 0x46},
484         {0x06, 0xcc, 0x47},
485         {0x06, 0xd5, 0x48},
486         {0x06, 0xdd, 0x49},
487         {0x06, 0xe3, 0x4a},
488         {0x06, 0xe8, 0x4b},
489         {0x06, 0xed, 0x4c},
490         {0x06, 0xf2, 0x4d},
491         {0x06, 0xf7, 0x4e},
492         {0x06, 0xfc, 0x4f},
493         {0x06, 0xff, 0x50},
494
495         {0x05, 0x01, 0xc0},
496         {0x05, 0x10, 0xcb},
497         {0x05, 0x40, 0xc1},
498         {0x05, 0x04, 0xc2},
499         {0x05, 0x00, 0xca},
500         {0x05, 0x40, 0xc1},
501         {0x05, 0x09, 0xc2},
502         {0x05, 0x00, 0xca},
503         {0x05, 0xc0, 0xc1},
504         {0x05, 0x09, 0xc2},
505         {0x05, 0x00, 0xca},
506         {0x05, 0x40, 0xc1},
507         {0x05, 0x59, 0xc2},
508         {0x05, 0x00, 0xca},
509         {0x04, 0x00, 0x01},
510         {0x05, 0x80, 0xc1},
511         {0x05, 0xec, 0xc2},
512         {0x05, 0x0, 0xca},
513
514         {0x06, 0x02, 0x57},
515         {0x06, 0x01, 0x58},
516         {0x06, 0x15, 0x59},
517         {0x06, 0x0a, 0x5a},
518         {0x06, 0x01, 0x57},
519         {0x06, 0x8a, 0x03},
520         {0x06, 0x0a, 0x6c},
521         {0x06, 0x30, 0x01},
522         {0x06, 0x20, 0x02},
523         {0x06, 0x00, 0x03},
524
525         {0x05, 0x8c, 0x25},
526
527         {0x06, 0x4d, 0x51},             /* maybe saturation (4d) */
528         {0x06, 0x84, 0x53},             /* making green (84) */
529         {0x06, 0x00, 0x57},             /* sharpness (1) */
530         {0x06, 0x18, 0x08},
531         {0x06, 0xfc, 0x09},
532         {0x06, 0xfc, 0x0a},
533         {0x06, 0xfc, 0x0b},
534         {0x06, 0x18, 0x0c},             /* maybe hue (18) */
535         {0x06, 0xfc, 0x0d},
536         {0x06, 0xfc, 0x0e},
537         {0x06, 0xfc, 0x0f},
538         {0x06, 0x18, 0x10},             /* maybe contrast (18) */
539
540         {0x05, 0x01, 0x02},
541
542         {0x04, 0x00, 0x08},             /* compression */
543         {0x04, 0x12, 0x09},
544         {0x04, 0x21, 0x0a},
545         {0x04, 0x10, 0x0b},
546         {0x04, 0x21, 0x0c},
547         {0x04, 0x1d, 0x00},             /* imagetype (1d) */
548         {0x04, 0x41, 0x01},             /* hardware snapcontrol */
549
550         {0x04, 0x00, 0x04},
551         {0x04, 0x00, 0x05},
552         {0x04, 0x10, 0x06},
553         {0x04, 0x10, 0x07},
554         {0x04, 0x40, 0x06},
555         {0x04, 0x40, 0x07},
556         {0x04, 0x00, 0x04},
557         {0x04, 0x00, 0x05},
558
559         {0x06, 0x1c, 0x17},
560         {0x06, 0xe2, 0x19},
561         {0x06, 0x1c, 0x1b},
562         {0x06, 0xe2, 0x1d},
563         {0x06, 0x5f, 0x1f},
564         {0x06, 0x32, 0x20},
565
566         {0x05, initial_brightness >> 6, 0x00},
567         {0x05, initial_brightness << 2, 0x01},
568         {0x05, 0x06, 0xc1},
569         {0x05, 0x58, 0xc2},
570         {0x05, 0x0, 0xca},
571         {0x05, 0x0, 0x11},
572         {}
573 };
574
575 static int reg_write(struct usb_device *dev,
576                      __u16 reg, __u16 index, __u16 value)
577 {
578         int ret;
579
580         ret = usb_control_msg(dev,
581                         usb_sndctrlpipe(dev, 0),
582                         reg,
583                         USB_TYPE_VENDOR | USB_RECIP_DEVICE,
584                         value, index, NULL, 0, 500);
585         PDEBUG(D_PACK, "reg write: 0x%02x,0x%02x:0x%02x, 0x%x",
586                 reg, index, value, ret);
587         if (ret < 0)
588                 PDEBUG(D_ERR, "reg write: error %d", ret);
589         return ret;
590 }
591
592 /* returns: negative is error, pos or zero is data */
593 static int reg_read(struct gspca_dev *gspca_dev,
594                         __u16 reg,      /* bRequest */
595                         __u16 index,    /* wIndex */
596                         __u16 length)   /* wLength (1 or 2 only) */
597 {
598         int ret;
599
600         gspca_dev->usb_buf[1] = 0;
601         ret = usb_control_msg(gspca_dev->dev,
602                         usb_rcvctrlpipe(gspca_dev->dev, 0),
603                         reg,
604                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
605                         (__u16) 0,              /* value */
606                         (__u16) index,
607                         gspca_dev->usb_buf, length,
608                         500);                   /* timeout */
609         if (ret < 0) {
610                 PDEBUG(D_ERR, "reg_read err %d", ret);
611                 return -1;
612         }
613         return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
614 }
615
616 static int write_vector(struct gspca_dev *gspca_dev,
617                         const __u16 data[][3])
618 {
619         struct usb_device *dev = gspca_dev->dev;
620         int ret, i = 0;
621
622         while (data[i][0] != 0 || data[i][1] != 0 || data[i][2] != 0) {
623                 ret = reg_write(dev, data[i][0], data[i][2], data[i][1]);
624                 if (ret < 0) {
625                         PDEBUG(D_ERR,
626                                 "Register write failed for 0x%x,0x%x,0x%x",
627                                 data[i][0], data[i][1], data[i][2]);
628                         return ret;
629                 }
630                 i++;
631         }
632         return 0;
633 }
634
635 /* this function is called at probe time */
636 static int sd_config(struct gspca_dev *gspca_dev,
637                         const struct usb_device_id *id)
638 {
639         struct sd *sd = (struct sd *) gspca_dev;
640         struct cam *cam;
641         __u16 vendor;
642         __u16 product;
643
644         vendor = id->idVendor;
645         product = id->idProduct;
646         switch (vendor) {
647         case 0x041e:            /* Creative cameras */
648 /*              switch (product) { */
649 /*              case 0x401d:     * here505b */
650                         sd->subtype = Nxultra;
651 /*                      break; */
652 /*              } */
653                 break;
654         case 0x0733:    /* Rebadged ViewQuest (Intel) and ViewQuest cameras */
655 /*              switch (product) { */
656 /*              case 0x0430: */
657 /*              fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
658                         sd->subtype = IntelPCCameraPro;
659 /*                      break; */
660 /*              } */
661                 break;
662         }
663
664         cam = &gspca_dev->cam;
665         cam->dev_name = (char *) id->driver_info;
666         cam->epaddr = 0x01;
667         cam->cam_mode = vga_mode;
668         if (sd->subtype != IntelPCCameraPro)
669                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
670         else                    /* no 640x480 for IntelPCCameraPro */
671                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0] - 1;
672         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
673
674         if (sd->subtype == Nxultra) {
675                 if (write_vector(gspca_dev, spca505b_init_data))
676                         return -EIO;
677         } else {
678                 if (write_vector(gspca_dev, spca505_init_data))
679                         return -EIO;
680         }
681         return 0;
682 }
683
684 /* this function is called at open time */
685 static int sd_open(struct gspca_dev *gspca_dev)
686 {
687         struct sd *sd = (struct sd *) gspca_dev;
688         int ret;
689
690         PDEBUG(D_STREAM, "Initializing SPCA505");
691         if (sd->subtype == Nxultra)
692                 write_vector(gspca_dev, spca505b_open_data_ccd);
693         else
694                 write_vector(gspca_dev, spca505_open_data_ccd);
695         ret = reg_read(gspca_dev, 6, 0x16, 2);
696
697         if (ret < 0) {
698                 PDEBUG(D_ERR|D_STREAM,
699                        "register read failed for after vector read err = %d",
700                        ret);
701                 return -EIO;
702         }
703         PDEBUG(D_STREAM,
704                 "After vector read returns : 0x%x should be 0x0101",
705                 ret & 0xffff);
706
707         ret = reg_write(gspca_dev->dev, 6, 0x16, 0x0a);
708         if (ret < 0) {
709                 PDEBUG(D_ERR, "register write failed for (6,0xa,0x16) err=%d",
710                        ret);
711                 return -EIO;
712         }
713         reg_write(gspca_dev->dev, 5, 0xc2, 18);
714         return 0;
715 }
716
717 static void sd_start(struct gspca_dev *gspca_dev)
718 {
719         struct usb_device *dev = gspca_dev->dev;
720         int ret;
721
722         /* necessary because without it we can see stream
723          * only once after loading module */
724         /* stopping usb registers Tomasz change */
725         reg_write(dev, 0x02, 0x0, 0x0);
726         switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
727         case 0:
728                 reg_write(dev, 0x04, 0x00, 0x00);
729                 reg_write(dev, 0x04, 0x06, 0x10);
730                 reg_write(dev, 0x04, 0x07, 0x10);
731                 break;
732         case 1:
733                 reg_write(dev, 0x04, 0x00, 0x01);
734                 reg_write(dev, 0x04, 0x06, 0x1a);
735                 reg_write(dev, 0x04, 0x07, 0x1a);
736                 break;
737         case 2:
738                 reg_write(dev, 0x04, 0x00, 0x02);
739                 reg_write(dev, 0x04, 0x06, 0x1c);
740                 reg_write(dev, 0x04, 0x07, 0x1d);
741                 break;
742         case 4:
743                 reg_write(dev, 0x04, 0x00, 0x04);
744                 reg_write(dev, 0x04, 0x06, 0x34);
745                 reg_write(dev, 0x04, 0x07, 0x34);
746                 break;
747         default:
748 /*      case 5: */
749                 reg_write(dev, 0x04, 0x00, 0x05);
750                 reg_write(dev, 0x04, 0x06, 0x40);
751                 reg_write(dev, 0x04, 0x07, 0x40);
752                 break;
753         }
754 /* Enable ISO packet machine - should we do this here or in ISOC init ? */
755         ret = reg_write(dev, SPCA50X_REG_USB,
756                          SPCA50X_USB_CTRL,
757                          SPCA50X_CUSB_ENABLE);
758
759 /*      reg_write(dev, 0x5, 0x0, 0x0); */
760 /*      reg_write(dev, 0x5, 0x0, 0x1); */
761 /*      reg_write(dev, 0x5, 0x11, 0x2); */
762 }
763
764 static void sd_stopN(struct gspca_dev *gspca_dev)
765 {
766         /* Disable ISO packet machine */
767         reg_write(gspca_dev->dev, 0x02, 0x00, 0x00);
768 }
769
770 static void sd_stop0(struct gspca_dev *gspca_dev)
771 {
772 }
773
774 /* this function is called at close time */
775 static void sd_close(struct gspca_dev *gspca_dev)
776 {
777         /* This maybe reset or power control */
778         reg_write(gspca_dev->dev, 0x03, 0x03, 0x20);
779         reg_write(gspca_dev->dev, 0x03, 0x01, 0x0);
780         reg_write(gspca_dev->dev, 0x03, 0x00, 0x1);
781         reg_write(gspca_dev->dev, 0x05, 0x10, 0x1);
782         reg_write(gspca_dev->dev, 0x05, 0x11, 0xf);
783 }
784
785 /* convert YYUV per line to YUYV (YUV 4:2:2) */
786 static void yyuv_decode(unsigned char *out,
787                         unsigned char *in,
788                         int width,
789                         int height)
790 {
791         unsigned char *Ui, *Vi, *yi, *yi1;
792         unsigned char *out1;
793         int i, j;
794
795         yi = in;
796         for (i = height / 2; --i >= 0; ) {
797                 out1 = out + width * 2;         /* next line */
798                 yi1 = yi + width;
799                 Ui = yi1 + width;
800                 Vi = Ui + width / 2;
801                 for (j = width / 2; --j >= 0; ) {
802                         *out++ = 128 + *yi++;
803                         *out++ = 128 + *Ui;
804                         *out++ = 128 + *yi++;
805                         *out++ = 128 + *Vi;
806
807                         *out1++ = 128 + *yi1++;
808                         *out1++ = 128 + *Ui++;
809                         *out1++ = 128 + *yi1++;
810                         *out1++ = 128 + *Vi++;
811                 }
812                 yi += width * 2;
813                 out = out1;
814         }
815 }
816
817 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
818                         struct gspca_frame *frame,      /* target */
819                         __u8 *data,                     /* isoc packet */
820                         int len)                        /* iso packet length */
821 {
822         struct sd *sd = (struct sd *) gspca_dev;
823
824         switch (data[0]) {
825         case 0:                         /* start of frame */
826                 if (gspca_dev->last_packet_type == FIRST_PACKET) {
827                         yyuv_decode(sd->tmpbuf2, sd->tmpbuf,
828                                         gspca_dev->width,
829                                         gspca_dev->height);
830                         frame = gspca_frame_add(gspca_dev,
831                                                 LAST_PACKET,
832                                                 frame,
833                                                 sd->tmpbuf2,
834                                                 gspca_dev->width
835                                                         * gspca_dev->height
836                                                         * 2);
837                 }
838                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
839                                 data, 0);
840                 data += SPCA50X_OFFSET_DATA;
841                 len -= SPCA50X_OFFSET_DATA;
842                 if (len > 0)
843                         memcpy(sd->tmpbuf, data, len);
844                 else
845                         len = 0;
846                 sd->buflen = len;
847                 return;
848         case 0xff:                      /* drop */
849 /*              gspca_dev->last_packet_type = DISCARD_PACKET; */
850                 return;
851         }
852         data += 1;
853         len -= 1;
854         memcpy(&sd->tmpbuf[sd->buflen], data, len);
855         sd->buflen += len;
856 }
857
858 static void setbrightness(struct gspca_dev *gspca_dev)
859 {
860         struct sd *sd = (struct sd *) gspca_dev;
861
862         __u8 brightness = sd->brightness;
863         reg_write(gspca_dev->dev, 5, 0x00, (255 - brightness) >> 6);
864         reg_write(gspca_dev->dev, 5, 0x01, (255 - brightness) << 2);
865
866 }
867 static void getbrightness(struct gspca_dev *gspca_dev)
868 {
869         struct sd *sd = (struct sd *) gspca_dev;
870
871         sd->brightness = 255
872                 - ((reg_read(gspca_dev, 5, 0x01, 1) >> 2)
873                         + (reg_read(gspca_dev, 5, 0x0, 1) << 6));
874 }
875
876 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
877 {
878         struct sd *sd = (struct sd *) gspca_dev;
879
880         sd->brightness = val;
881         if (gspca_dev->streaming)
882                 setbrightness(gspca_dev);
883         return 0;
884 }
885
886 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
887 {
888         struct sd *sd = (struct sd *) gspca_dev;
889
890         getbrightness(gspca_dev);
891         *val = sd->brightness;
892         return 0;
893 }
894
895 /* sub-driver description */
896 static const struct sd_desc sd_desc = {
897         .name = MODULE_NAME,
898         .ctrls = sd_ctrls,
899         .nctrls = ARRAY_SIZE(sd_ctrls),
900         .config = sd_config,
901         .open = sd_open,
902         .start = sd_start,
903         .stopN = sd_stopN,
904         .stop0 = sd_stop0,
905         .close = sd_close,
906         .pkt_scan = sd_pkt_scan,
907 };
908
909 /* -- module initialisation -- */
910 #define DVNM(name) .driver_info = (kernel_ulong_t) name
911 static const __devinitdata struct usb_device_id device_table[] = {
912         {USB_DEVICE(0x041e, 0x401d), DVNM("Creative Webcam NX ULTRA")},
913         {USB_DEVICE(0x0733, 0x0430), DVNM("Intel PC Camera Pro")},
914         {}
915 };
916 MODULE_DEVICE_TABLE(usb, device_table);
917
918 /* -- device connect -- */
919 static int sd_probe(struct usb_interface *intf,
920                         const struct usb_device_id *id)
921 {
922         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
923                                 THIS_MODULE);
924 }
925
926 static struct usb_driver sd_driver = {
927         .name = MODULE_NAME,
928         .id_table = device_table,
929         .probe = sd_probe,
930         .disconnect = gspca_disconnect,
931 };
932
933 /* -- module insert / remove -- */
934 static int __init sd_mod_init(void)
935 {
936         if (usb_register(&sd_driver) < 0)
937                 return -1;
938         PDEBUG(D_PROBE, "registered");
939         return 0;
940 }
941 static void __exit sd_mod_exit(void)
942 {
943         usb_deregister(&sd_driver);
944         PDEBUG(D_PROBE, "deregistered");
945 }
946
947 module_init(sd_mod_init);
948 module_exit(sd_mod_exit);