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