]> err.no Git - linux-2.6/blob - drivers/media/video/gspca/spca505.c
V4L/DVB (8513): gspca: Set the specific per webcam information in driver_info.
[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 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
642         cam = &gspca_dev->cam;
643         cam->epaddr = 0x01;
644         cam->cam_mode = vga_mode;
645         sd->subtype = id->driver_info;
646         if (sd->subtype != IntelPCCameraPro)
647                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
648         else                    /* no 640x480 for IntelPCCameraPro */
649                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0] - 1;
650         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
651
652         if (sd->subtype == Nxultra) {
653                 if (write_vector(gspca_dev, spca505b_init_data))
654                         return -EIO;
655         } else {
656                 if (write_vector(gspca_dev, spca505_init_data))
657                         return -EIO;
658         }
659         return 0;
660 }
661
662 /* this function is called at open time */
663 static int sd_open(struct gspca_dev *gspca_dev)
664 {
665         struct sd *sd = (struct sd *) gspca_dev;
666         int ret;
667
668         PDEBUG(D_STREAM, "Initializing SPCA505");
669         if (sd->subtype == Nxultra)
670                 write_vector(gspca_dev, spca505b_open_data_ccd);
671         else
672                 write_vector(gspca_dev, spca505_open_data_ccd);
673         ret = reg_read(gspca_dev, 6, 0x16, 2);
674
675         if (ret < 0) {
676                 PDEBUG(D_ERR|D_STREAM,
677                        "register read failed for after vector read err = %d",
678                        ret);
679                 return -EIO;
680         }
681         PDEBUG(D_STREAM,
682                 "After vector read returns : 0x%x should be 0x0101",
683                 ret & 0xffff);
684
685         ret = reg_write(gspca_dev->dev, 6, 0x16, 0x0a);
686         if (ret < 0) {
687                 PDEBUG(D_ERR, "register write failed for (6,0xa,0x16) err=%d",
688                        ret);
689                 return -EIO;
690         }
691         reg_write(gspca_dev->dev, 5, 0xc2, 18);
692         return 0;
693 }
694
695 static void sd_start(struct gspca_dev *gspca_dev)
696 {
697         struct usb_device *dev = gspca_dev->dev;
698         int ret;
699
700         /* necessary because without it we can see stream
701          * only once after loading module */
702         /* stopping usb registers Tomasz change */
703         reg_write(dev, 0x02, 0x0, 0x0);
704         switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
705         case 0:
706                 reg_write(dev, 0x04, 0x00, 0x00);
707                 reg_write(dev, 0x04, 0x06, 0x10);
708                 reg_write(dev, 0x04, 0x07, 0x10);
709                 break;
710         case 1:
711                 reg_write(dev, 0x04, 0x00, 0x01);
712                 reg_write(dev, 0x04, 0x06, 0x1a);
713                 reg_write(dev, 0x04, 0x07, 0x1a);
714                 break;
715         case 2:
716                 reg_write(dev, 0x04, 0x00, 0x02);
717                 reg_write(dev, 0x04, 0x06, 0x1c);
718                 reg_write(dev, 0x04, 0x07, 0x1d);
719                 break;
720         case 4:
721                 reg_write(dev, 0x04, 0x00, 0x04);
722                 reg_write(dev, 0x04, 0x06, 0x34);
723                 reg_write(dev, 0x04, 0x07, 0x34);
724                 break;
725         default:
726 /*      case 5: */
727                 reg_write(dev, 0x04, 0x00, 0x05);
728                 reg_write(dev, 0x04, 0x06, 0x40);
729                 reg_write(dev, 0x04, 0x07, 0x40);
730                 break;
731         }
732 /* Enable ISO packet machine - should we do this here or in ISOC init ? */
733         ret = reg_write(dev, SPCA50X_REG_USB,
734                          SPCA50X_USB_CTRL,
735                          SPCA50X_CUSB_ENABLE);
736
737 /*      reg_write(dev, 0x5, 0x0, 0x0); */
738 /*      reg_write(dev, 0x5, 0x0, 0x1); */
739 /*      reg_write(dev, 0x5, 0x11, 0x2); */
740 }
741
742 static void sd_stopN(struct gspca_dev *gspca_dev)
743 {
744         /* Disable ISO packet machine */
745         reg_write(gspca_dev->dev, 0x02, 0x00, 0x00);
746 }
747
748 static void sd_stop0(struct gspca_dev *gspca_dev)
749 {
750 }
751
752 /* this function is called at close time */
753 static void sd_close(struct gspca_dev *gspca_dev)
754 {
755         /* This maybe reset or power control */
756         reg_write(gspca_dev->dev, 0x03, 0x03, 0x20);
757         reg_write(gspca_dev->dev, 0x03, 0x01, 0x0);
758         reg_write(gspca_dev->dev, 0x03, 0x00, 0x1);
759         reg_write(gspca_dev->dev, 0x05, 0x10, 0x1);
760         reg_write(gspca_dev->dev, 0x05, 0x11, 0xf);
761 }
762
763 /* convert YYUV per line to YUYV (YUV 4:2:2) */
764 static void yyuv_decode(unsigned char *out,
765                         unsigned char *in,
766                         int width,
767                         int height)
768 {
769         unsigned char *Ui, *Vi, *yi, *yi1;
770         unsigned char *out1;
771         int i, j;
772
773         yi = in;
774         for (i = height / 2; --i >= 0; ) {
775                 out1 = out + width * 2;         /* next line */
776                 yi1 = yi + width;
777                 Ui = yi1 + width;
778                 Vi = Ui + width / 2;
779                 for (j = width / 2; --j >= 0; ) {
780                         *out++ = 128 + *yi++;
781                         *out++ = 128 + *Ui;
782                         *out++ = 128 + *yi++;
783                         *out++ = 128 + *Vi;
784
785                         *out1++ = 128 + *yi1++;
786                         *out1++ = 128 + *Ui++;
787                         *out1++ = 128 + *yi1++;
788                         *out1++ = 128 + *Vi++;
789                 }
790                 yi += width * 2;
791                 out = out1;
792         }
793 }
794
795 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
796                         struct gspca_frame *frame,      /* target */
797                         __u8 *data,                     /* isoc packet */
798                         int len)                        /* iso packet length */
799 {
800         struct sd *sd = (struct sd *) gspca_dev;
801
802         switch (data[0]) {
803         case 0:                         /* start of frame */
804                 if (gspca_dev->last_packet_type == FIRST_PACKET) {
805                         yyuv_decode(sd->tmpbuf2, sd->tmpbuf,
806                                         gspca_dev->width,
807                                         gspca_dev->height);
808                         frame = gspca_frame_add(gspca_dev,
809                                                 LAST_PACKET,
810                                                 frame,
811                                                 sd->tmpbuf2,
812                                                 gspca_dev->width
813                                                         * gspca_dev->height
814                                                         * 2);
815                 }
816                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
817                                 data, 0);
818                 data += SPCA50X_OFFSET_DATA;
819                 len -= SPCA50X_OFFSET_DATA;
820                 if (len > 0)
821                         memcpy(sd->tmpbuf, data, len);
822                 else
823                         len = 0;
824                 sd->buflen = len;
825                 return;
826         case 0xff:                      /* drop */
827 /*              gspca_dev->last_packet_type = DISCARD_PACKET; */
828                 return;
829         }
830         data += 1;
831         len -= 1;
832         memcpy(&sd->tmpbuf[sd->buflen], data, len);
833         sd->buflen += len;
834 }
835
836 static void setbrightness(struct gspca_dev *gspca_dev)
837 {
838         struct sd *sd = (struct sd *) gspca_dev;
839
840         __u8 brightness = sd->brightness;
841         reg_write(gspca_dev->dev, 5, 0x00, (255 - brightness) >> 6);
842         reg_write(gspca_dev->dev, 5, 0x01, (255 - brightness) << 2);
843
844 }
845 static void getbrightness(struct gspca_dev *gspca_dev)
846 {
847         struct sd *sd = (struct sd *) gspca_dev;
848
849         sd->brightness = 255
850                 - ((reg_read(gspca_dev, 5, 0x01, 1) >> 2)
851                         + (reg_read(gspca_dev, 5, 0x0, 1) << 6));
852 }
853
854 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
855 {
856         struct sd *sd = (struct sd *) gspca_dev;
857
858         sd->brightness = val;
859         if (gspca_dev->streaming)
860                 setbrightness(gspca_dev);
861         return 0;
862 }
863
864 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
865 {
866         struct sd *sd = (struct sd *) gspca_dev;
867
868         getbrightness(gspca_dev);
869         *val = sd->brightness;
870         return 0;
871 }
872
873 /* sub-driver description */
874 static const struct sd_desc sd_desc = {
875         .name = MODULE_NAME,
876         .ctrls = sd_ctrls,
877         .nctrls = ARRAY_SIZE(sd_ctrls),
878         .config = sd_config,
879         .open = sd_open,
880         .start = sd_start,
881         .stopN = sd_stopN,
882         .stop0 = sd_stop0,
883         .close = sd_close,
884         .pkt_scan = sd_pkt_scan,
885 };
886
887 /* -- module initialisation -- */
888 static const __devinitdata struct usb_device_id device_table[] = {
889         {USB_DEVICE(0x041e, 0x401d), Nxultra},
890         {USB_DEVICE(0x0733, 0x0430), IntelPCCameraPro},
891 /*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
892         {}
893 };
894 MODULE_DEVICE_TABLE(usb, device_table);
895
896 /* -- device connect -- */
897 static int sd_probe(struct usb_interface *intf,
898                         const struct usb_device_id *id)
899 {
900         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
901                                 THIS_MODULE);
902 }
903
904 static struct usb_driver sd_driver = {
905         .name = MODULE_NAME,
906         .id_table = device_table,
907         .probe = sd_probe,
908         .disconnect = gspca_disconnect,
909 };
910
911 /* -- module insert / remove -- */
912 static int __init sd_mod_init(void)
913 {
914         if (usb_register(&sd_driver) < 0)
915                 return -1;
916         PDEBUG(D_PROBE, "registered");
917         return 0;
918 }
919 static void __exit sd_mod_exit(void)
920 {
921         usb_deregister(&sd_driver);
922         PDEBUG(D_PROBE, "deregistered");
923 }
924
925 module_init(sd_mod_init);
926 module_exit(sd_mod_exit);