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