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