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