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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#define MODULE_NAME "spca505"
25
26#include "gspca.h"
27
28MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
29MODULE_DESCRIPTION("GSPCA/SPCA505 USB Camera Driver");
30MODULE_LICENSE("GPL");
31
32/* specific webcam descriptor */
33struct sd {
34	struct gspca_dev gspca_dev;		/* !! must be the first item */
35
36	u8 subtype;
37#define IntelPCCameraPro 0
38#define Nxultra 1
39};
40
41static const struct v4l2_pix_format vga_mode[] = {
42	{160, 120, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
43		.bytesperline = 160,
44		.sizeimage = 160 * 120 * 3 / 2,
45		.colorspace = V4L2_COLORSPACE_SRGB,
46		.priv = 4},
47	{176, 144, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
48		.bytesperline = 176,
49		.sizeimage = 176 * 144 * 3 / 2,
50		.colorspace = V4L2_COLORSPACE_SRGB,
51		.priv = 3},
52	{320, 240, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
53		.bytesperline = 320,
54		.sizeimage = 320 * 240 * 3 / 2,
55		.colorspace = V4L2_COLORSPACE_SRGB,
56		.priv = 2},
57	{352, 288, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
58		.bytesperline = 352,
59		.sizeimage = 352 * 288 * 3 / 2,
60		.colorspace = V4L2_COLORSPACE_SRGB,
61		.priv = 1},
62	{640, 480, V4L2_PIX_FMT_SPCA505, V4L2_FIELD_NONE,
63		.bytesperline = 640,
64		.sizeimage = 640 * 480 * 3 / 2,
65		.colorspace = V4L2_COLORSPACE_SRGB,
66		.priv = 0},
67};
68
69#define SPCA50X_OFFSET_DATA 10
70
71#define SPCA50X_REG_USB 0x02	/* spca505 501 */
72
73#define SPCA50X_USB_CTRL 0x00	/* spca505 */
74#define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
75
76#define SPCA50X_REG_GLOBAL 0x03	/* spca505 */
77#define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
78#define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */
79
80#define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
81#define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
82#define SPCA50X_GMISC3_SAA7113RST 0x20	/* Not sure about this one spca505 */
83
84/* Image format and compression control */
85#define SPCA50X_REG_COMPRESS 0x04
86
87/*
88 * Data to initialize a SPCA505. Common to the CCD and external modes
89 */
90static const u8 spca505_init_data[][3] = {
91	/* bmRequest,value,index */
92	{SPCA50X_REG_GLOBAL, SPCA50X_GMISC3_SAA7113RST, SPCA50X_GLOBAL_MISC3},
93	/* Sensor reset */
94	{SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC3},
95	{SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC1},
96	/* Block USB reset */
97	{SPCA50X_REG_GLOBAL, SPCA50X_GMISC0_IDSEL, SPCA50X_GLOBAL_MISC0},
98
99	{0x05, 0x01, 0x10},
100					/* Maybe power down some stuff */
101	{0x05, 0x0f, 0x11},
102
103	/* Setup internal CCD  ? */
104	{0x06, 0x10, 0x08},
105	{0x06, 0x00, 0x09},
106	{0x06, 0x00, 0x0a},
107	{0x06, 0x00, 0x0b},
108	{0x06, 0x10, 0x0c},
109	{0x06, 0x00, 0x0d},
110	{0x06, 0x00, 0x0e},
111	{0x06, 0x00, 0x0f},
112	{0x06, 0x10, 0x10},
113	{0x06, 0x02, 0x11},
114	{0x06, 0x00, 0x12},
115	{0x06, 0x04, 0x13},
116	{0x06, 0x02, 0x14},
117	{0x06, 0x8a, 0x51},
118	{0x06, 0x40, 0x52},
119	{0x06, 0xb6, 0x53},
120	{0x06, 0x3d, 0x54},
121	{}
122};
123
124/*
125 * Data to initialize the camera using the internal CCD
126 */
127static const u8 spca505_open_data_ccd[][3] = {
128	/* bmRequest,value,index */
129	/* Internal CCD data set */
130	{0x03, 0x04, 0x01},
131	/* This could be a reset */
132	{0x03, 0x00, 0x01},
133
134	/* Setup compression and image registers. 0x6 and 0x7 seem to be
135	   related to H&V hold, and are resolution mode specific */
136		{0x04, 0x10, 0x01},
137		/* DIFF(0x50), was (0x10) */
138	{0x04, 0x00, 0x04},
139	{0x04, 0x00, 0x05},
140	{0x04, 0x20, 0x06},
141	{0x04, 0x20, 0x07},
142
143	{0x08, 0x0a, 0x00},
144	/* DIFF (0x4a), was (0xa) */
145
146	{0x05, 0x00, 0x10},
147	{0x05, 0x00, 0x11},
148	{0x05, 0x00, 0x00},
149	/* DIFF not written */
150	{0x05, 0x00, 0x01},
151	/* DIFF not written */
152	{0x05, 0x00, 0x02},
153	/* DIFF not written */
154	{0x05, 0x00, 0x03},
155	/* DIFF not written */
156	{0x05, 0x00, 0x04},
157	/* DIFF not written */
158		{0x05, 0x80, 0x05},
159		/* DIFF not written */
160		{0x05, 0xe0, 0x06},
161		/* DIFF not written */
162		{0x05, 0x20, 0x07},
163		/* DIFF not written */
164		{0x05, 0xa0, 0x08},
165		/* DIFF not written */
166		{0x05, 0x0, 0x12},
167		/* DIFF not written */
168	{0x05, 0x02, 0x0f},
169	/* DIFF not written */
170		{0x05, 0x10, 0x46},
171		/* DIFF not written */
172		{0x05, 0x8, 0x4a},
173		/* DIFF not written */
174
175	{0x03, 0x08, 0x03},
176	/* DIFF (0x3,0x28,0x3) */
177	{0x03, 0x08, 0x01},
178	{0x03, 0x0c, 0x03},
179	/* DIFF not written */
180		{0x03, 0x21, 0x00},
181		/* DIFF (0x39) */
182
183/* Extra block copied from init to hopefully ensure CCD is in a sane state */
184	{0x06, 0x10, 0x08},
185	{0x06, 0x00, 0x09},
186	{0x06, 0x00, 0x0a},
187	{0x06, 0x00, 0x0b},
188	{0x06, 0x10, 0x0c},
189	{0x06, 0x00, 0x0d},
190	{0x06, 0x00, 0x0e},
191	{0x06, 0x00, 0x0f},
192	{0x06, 0x10, 0x10},
193	{0x06, 0x02, 0x11},
194	{0x06, 0x00, 0x12},
195	{0x06, 0x04, 0x13},
196	{0x06, 0x02, 0x14},
197	{0x06, 0x8a, 0x51},
198	{0x06, 0x40, 0x52},
199	{0x06, 0xb6, 0x53},
200	{0x06, 0x3d, 0x54},
201	/* End of extra block */
202
203		{0x06, 0x3f, 0x1},
204		/* Block skipped */
205	{0x06, 0x10, 0x02},
206	{0x06, 0x64, 0x07},
207	{0x06, 0x10, 0x08},
208	{0x06, 0x00, 0x09},
209	{0x06, 0x00, 0x0a},
210	{0x06, 0x00, 0x0b},
211	{0x06, 0x10, 0x0c},
212	{0x06, 0x00, 0x0d},
213	{0x06, 0x00, 0x0e},
214	{0x06, 0x00, 0x0f},
215	{0x06, 0x10, 0x10},
216	{0x06, 0x02, 0x11},
217	{0x06, 0x00, 0x12},
218	{0x06, 0x04, 0x13},
219	{0x06, 0x02, 0x14},
220	{0x06, 0x8a, 0x51},
221	{0x06, 0x40, 0x52},
222	{0x06, 0xb6, 0x53},
223	{0x06, 0x3d, 0x54},
224	{0x06, 0x60, 0x57},
225	{0x06, 0x20, 0x58},
226	{0x06, 0x15, 0x59},
227	{0x06, 0x05, 0x5a},
228
229	{0x05, 0x01, 0xc0},
230	{0x05, 0x10, 0xcb},
231		{0x05, 0x80, 0xc1},
232		/* */
233		{0x05, 0x0, 0xc2},
234		/* 4 was 0 */
235	{0x05, 0x00, 0xca},
236		{0x05, 0x80, 0xc1},
237		/*  */
238	{0x05, 0x04, 0xc2},
239	{0x05, 0x00, 0xca},
240		{0x05, 0x0, 0xc1},
241		/*  */
242	{0x05, 0x00, 0xc2},
243	{0x05, 0x00, 0xca},
244		{0x05, 0x40, 0xc1},
245		/* */
246	{0x05, 0x17, 0xc2},
247	{0x05, 0x00, 0xca},
248		{0x05, 0x80, 0xc1},
249		/* */
250	{0x05, 0x06, 0xc2},
251	{0x05, 0x00, 0xca},
252		{0x05, 0x80, 0xc1},
253		/* */
254	{0x05, 0x04, 0xc2},
255	{0x05, 0x00, 0xca},
256
257	{0x03, 0x4c, 0x3},
258	{0x03, 0x18, 0x1},
259
260	{0x06, 0x70, 0x51},
261	{0x06, 0xbe, 0x53},
262	{0x06, 0x71, 0x57},
263	{0x06, 0x20, 0x58},
264	{0x06, 0x05, 0x59},
265	{0x06, 0x15, 0x5a},
266
267	{0x04, 0x00, 0x08},
268	/* Compress = OFF (0x1 to turn on) */
269	{0x04, 0x12, 0x09},
270	{0x04, 0x21, 0x0a},
271	{0x04, 0x10, 0x0b},
272	{0x04, 0x21, 0x0c},
273	{0x04, 0x05, 0x00},
274	/* was 5 (Image Type ? ) */
275	{0x04, 0x00, 0x01},
276
277	{0x06, 0x3f, 0x01},
278
279	{0x04, 0x00, 0x04},
280	{0x04, 0x00, 0x05},
281	{0x04, 0x40, 0x06},
282	{0x04, 0x40, 0x07},
283
284	{0x06, 0x1c, 0x17},
285	{0x06, 0xe2, 0x19},
286	{0x06, 0x1c, 0x1b},
287	{0x06, 0xe2, 0x1d},
288	{0x06, 0xaa, 0x1f},
289	{0x06, 0x70, 0x20},
290
291	{0x05, 0x01, 0x10},
292	{0x05, 0x00, 0x11},
293	{0x05, 0x01, 0x00},
294	{0x05, 0x05, 0x01},
295		{0x05, 0x00, 0xc1},
296		/* */
297	{0x05, 0x00, 0xc2},
298	{0x05, 0x00, 0xca},
299
300	{0x06, 0x70, 0x51},
301	{0x06, 0xbe, 0x53},
302	{}
303};
304
305/*
306 * Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
307 * SPCA505b chip based cameras initialization data
308 */
309/* jfm */
310#define initial_brightness 0x7f	/* 0x0(white)-0xff(black) */
311/* #define initial_brightness 0x0	//0x0(white)-0xff(black) */
312/*
313 * Data to initialize a SPCA505. Common to the CCD and external modes
314 */
315static const u8 spca505b_init_data[][3] = {
316/* start */
317	{0x02, 0x00, 0x00},		/* init */
318	{0x02, 0x00, 0x01},
319	{0x02, 0x00, 0x02},
320	{0x02, 0x00, 0x03},
321	{0x02, 0x00, 0x04},
322	{0x02, 0x00, 0x05},
323	{0x02, 0x00, 0x06},
324	{0x02, 0x00, 0x07},
325	{0x02, 0x00, 0x08},
326	{0x02, 0x00, 0x09},
327	{0x03, 0x00, 0x00},
328	{0x03, 0x00, 0x01},
329	{0x03, 0x00, 0x02},
330	{0x03, 0x00, 0x03},
331	{0x03, 0x00, 0x04},
332	{0x03, 0x00, 0x05},
333	{0x03, 0x00, 0x06},
334	{0x04, 0x00, 0x00},
335	{0x04, 0x00, 0x02},
336	{0x04, 0x00, 0x04},
337	{0x04, 0x00, 0x05},
338	{0x04, 0x00, 0x06},
339	{0x04, 0x00, 0x07},
340	{0x04, 0x00, 0x08},
341	{0x04, 0x00, 0x09},
342	{0x04, 0x00, 0x0a},
343	{0x04, 0x00, 0x0b},
344	{0x04, 0x00, 0x0c},
345	{0x07, 0x00, 0x00},
346	{0x07, 0x00, 0x03},
347	{0x08, 0x00, 0x00},
348	{0x08, 0x00, 0x01},
349	{0x08, 0x00, 0x02},
350	{0x06, 0x18, 0x08},
351	{0x06, 0xfc, 0x09},
352	{0x06, 0xfc, 0x0a},
353	{0x06, 0xfc, 0x0b},
354	{0x06, 0x18, 0x0c},
355	{0x06, 0xfc, 0x0d},
356	{0x06, 0xfc, 0x0e},
357	{0x06, 0xfc, 0x0f},
358	{0x06, 0x18, 0x10},
359	{0x06, 0xfe, 0x12},
360	{0x06, 0x00, 0x11},
361	{0x06, 0x00, 0x14},
362	{0x06, 0x00, 0x13},
363	{0x06, 0x28, 0x51},
364	{0x06, 0xff, 0x53},
365	{0x02, 0x00, 0x08},
366
367	{0x03, 0x00, 0x03},
368	{0x03, 0x10, 0x03},
369	{}
370};
371
372/*
373 * Data to initialize the camera using the internal CCD
374 */
375static const u8 spca505b_open_data_ccd[][3] = {
376
377/* {0x02,0x00,0x00}, */
378	{0x03, 0x04, 0x01},		/* rst */
379	{0x03, 0x00, 0x01},
380	{0x03, 0x00, 0x00},
381	{0x03, 0x21, 0x00},
382	{0x03, 0x00, 0x04},
383	{0x03, 0x00, 0x03},
384	{0x03, 0x18, 0x03},
385	{0x03, 0x08, 0x01},
386	{0x03, 0x1c, 0x03},
387	{0x03, 0x5c, 0x03},
388	{0x03, 0x5c, 0x03},
389	{0x03, 0x18, 0x01},
390
391/* same as 505 */
392	{0x04, 0x10, 0x01},
393	{0x04, 0x00, 0x04},
394	{0x04, 0x00, 0x05},
395	{0x04, 0x20, 0x06},
396	{0x04, 0x20, 0x07},
397
398	{0x08, 0x0a, 0x00},
399
400	{0x05, 0x00, 0x10},
401	{0x05, 0x00, 0x11},
402	{0x05, 0x00, 0x12},
403	{0x05, 0x6f, 0x00},
404	{0x05, initial_brightness >> 6, 0x00},
405	{0x05, (initial_brightness << 2) & 0xff, 0x01},
406	{0x05, 0x00, 0x02},
407	{0x05, 0x01, 0x03},
408	{0x05, 0x00, 0x04},
409	{0x05, 0x03, 0x05},
410	{0x05, 0xe0, 0x06},
411	{0x05, 0x20, 0x07},
412	{0x05, 0xa0, 0x08},
413	{0x05, 0x00, 0x12},
414	{0x05, 0x02, 0x0f},
415	{0x05, 0x80, 0x14},		/* max exposure off (0=on) */
416	{0x05, 0x01, 0xb0},
417	{0x05, 0x01, 0xbf},
418	{0x03, 0x02, 0x06},
419	{0x05, 0x10, 0x46},
420	{0x05, 0x08, 0x4a},
421
422	{0x06, 0x00, 0x01},
423	{0x06, 0x10, 0x02},
424	{0x06, 0x64, 0x07},
425	{0x06, 0x18, 0x08},
426	{0x06, 0xfc, 0x09},
427	{0x06, 0xfc, 0x0a},
428	{0x06, 0xfc, 0x0b},
429	{0x04, 0x00, 0x01},
430	{0x06, 0x18, 0x0c},
431	{0x06, 0xfc, 0x0d},
432	{0x06, 0xfc, 0x0e},
433	{0x06, 0xfc, 0x0f},
434	{0x06, 0x11, 0x10},		/* contrast */
435	{0x06, 0x00, 0x11},
436	{0x06, 0xfe, 0x12},
437	{0x06, 0x00, 0x13},
438	{0x06, 0x00, 0x14},
439	{0x06, 0x9d, 0x51},
440	{0x06, 0x40, 0x52},
441	{0x06, 0x7c, 0x53},
442	{0x06, 0x40, 0x54},
443	{0x06, 0x02, 0x57},
444	{0x06, 0x03, 0x58},
445	{0x06, 0x15, 0x59},
446	{0x06, 0x05, 0x5a},
447	{0x06, 0x03, 0x56},
448	{0x06, 0x02, 0x3f},
449	{0x06, 0x00, 0x40},
450	{0x06, 0x39, 0x41},
451	{0x06, 0x69, 0x42},
452	{0x06, 0x87, 0x43},
453	{0x06, 0x9e, 0x44},
454	{0x06, 0xb1, 0x45},
455	{0x06, 0xbf, 0x46},
456	{0x06, 0xcc, 0x47},
457	{0x06, 0xd5, 0x48},
458	{0x06, 0xdd, 0x49},
459	{0x06, 0xe3, 0x4a},
460	{0x06, 0xe8, 0x4b},
461	{0x06, 0xed, 0x4c},
462	{0x06, 0xf2, 0x4d},
463	{0x06, 0xf7, 0x4e},
464	{0x06, 0xfc, 0x4f},
465	{0x06, 0xff, 0x50},
466
467	{0x05, 0x01, 0xc0},
468	{0x05, 0x10, 0xcb},
469	{0x05, 0x40, 0xc1},
470	{0x05, 0x04, 0xc2},
471	{0x05, 0x00, 0xca},
472	{0x05, 0x40, 0xc1},
473	{0x05, 0x09, 0xc2},
474	{0x05, 0x00, 0xca},
475	{0x05, 0xc0, 0xc1},
476	{0x05, 0x09, 0xc2},
477	{0x05, 0x00, 0xca},
478	{0x05, 0x40, 0xc1},
479	{0x05, 0x59, 0xc2},
480	{0x05, 0x00, 0xca},
481	{0x04, 0x00, 0x01},
482	{0x05, 0x80, 0xc1},
483	{0x05, 0xec, 0xc2},
484	{0x05, 0x0, 0xca},
485
486	{0x06, 0x02, 0x57},
487	{0x06, 0x01, 0x58},
488	{0x06, 0x15, 0x59},
489	{0x06, 0x0a, 0x5a},
490	{0x06, 0x01, 0x57},
491	{0x06, 0x8a, 0x03},
492	{0x06, 0x0a, 0x6c},
493	{0x06, 0x30, 0x01},
494	{0x06, 0x20, 0x02},
495	{0x06, 0x00, 0x03},
496
497	{0x05, 0x8c, 0x25},
498
499	{0x06, 0x4d, 0x51},		/* maybe saturation (4d) */
500	{0x06, 0x84, 0x53},		/* making green (84) */
501	{0x06, 0x00, 0x57},		/* sharpness (1) */
502	{0x06, 0x18, 0x08},
503	{0x06, 0xfc, 0x09},
504	{0x06, 0xfc, 0x0a},
505	{0x06, 0xfc, 0x0b},
506	{0x06, 0x18, 0x0c},		/* maybe hue (18) */
507	{0x06, 0xfc, 0x0d},
508	{0x06, 0xfc, 0x0e},
509	{0x06, 0xfc, 0x0f},
510	{0x06, 0x18, 0x10},		/* maybe contrast (18) */
511
512	{0x05, 0x01, 0x02},
513
514	{0x04, 0x00, 0x08},		/* compression */
515	{0x04, 0x12, 0x09},
516	{0x04, 0x21, 0x0a},
517	{0x04, 0x10, 0x0b},
518	{0x04, 0x21, 0x0c},
519	{0x04, 0x1d, 0x00},		/* imagetype (1d) */
520	{0x04, 0x41, 0x01},		/* hardware snapcontrol */
521
522	{0x04, 0x00, 0x04},
523	{0x04, 0x00, 0x05},
524	{0x04, 0x10, 0x06},
525	{0x04, 0x10, 0x07},
526	{0x04, 0x40, 0x06},
527	{0x04, 0x40, 0x07},
528	{0x04, 0x00, 0x04},
529	{0x04, 0x00, 0x05},
530
531	{0x06, 0x1c, 0x17},
532	{0x06, 0xe2, 0x19},
533	{0x06, 0x1c, 0x1b},
534	{0x06, 0xe2, 0x1d},
535	{0x06, 0x5f, 0x1f},
536	{0x06, 0x32, 0x20},
537
538	{0x05, initial_brightness >> 6, 0x00},
539	{0x05, (initial_brightness << 2) & 0xff, 0x01},
540	{0x05, 0x06, 0xc1},
541	{0x05, 0x58, 0xc2},
542	{0x05, 0x00, 0xca},
543	{0x05, 0x00, 0x11},
544	{}
545};
546
547static int reg_write(struct gspca_dev *gspca_dev,
548		     u16 req, u16 index, u16 value)
549{
550	int ret;
551	struct usb_device *dev = gspca_dev->dev;
552
553	ret = usb_control_msg(dev,
554			usb_sndctrlpipe(dev, 0),
555			req,
556			USB_TYPE_VENDOR | USB_RECIP_DEVICE,
557			value, index, NULL, 0, 500);
558	PDEBUG(D_USBO, "reg write: 0x%02x,0x%02x:0x%02x, %d",
559		req, index, value, ret);
560	if (ret < 0)
561		pr_err("reg write: error %d\n", ret);
562	return ret;
563}
564
565/* returns: negative is error, pos or zero is data */
566static int reg_read(struct gspca_dev *gspca_dev,
567			u16 req,	/* bRequest */
568			u16 index)	/* wIndex */
569{
570	int ret;
571
572	ret = usb_control_msg(gspca_dev->dev,
573			usb_rcvctrlpipe(gspca_dev->dev, 0),
574			req,
575			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
576			0,			/* value */
577			index,
578			gspca_dev->usb_buf, 2,
579			500);			/* timeout */
580	if (ret < 0)
581		return ret;
582	return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
583}
584
585static int write_vector(struct gspca_dev *gspca_dev,
586			const u8 data[][3])
587{
588	int ret, i = 0;
589
590	while (data[i][0] != 0) {
591		ret = reg_write(gspca_dev, data[i][0], data[i][2],
592								data[i][1]);
593		if (ret < 0)
594			return ret;
595		i++;
596	}
597	return 0;
598}
599
600/* this function is called at probe time */
601static int sd_config(struct gspca_dev *gspca_dev,
602			const struct usb_device_id *id)
603{
604	struct sd *sd = (struct sd *) gspca_dev;
605	struct cam *cam;
606
607	cam = &gspca_dev->cam;
608	cam->cam_mode = vga_mode;
609	sd->subtype = id->driver_info;
610	if (sd->subtype != IntelPCCameraPro)
611		cam->nmodes = ARRAY_SIZE(vga_mode);
612	else			/* no 640x480 for IntelPCCameraPro */
613		cam->nmodes = ARRAY_SIZE(vga_mode) - 1;
614
615	return 0;
616}
617
618/* this function is called at probe and resume time */
619static int sd_init(struct gspca_dev *gspca_dev)
620{
621	struct sd *sd = (struct sd *) gspca_dev;
622
623	if (write_vector(gspca_dev,
624			 sd->subtype == Nxultra
625				? spca505b_init_data
626				: spca505_init_data))
627		return -EIO;
628	return 0;
629}
630
631static void setbrightness(struct gspca_dev *gspca_dev, s32 brightness)
632{
633	reg_write(gspca_dev, 0x05, 0x00, (255 - brightness) >> 6);
634	reg_write(gspca_dev, 0x05, 0x01, (255 - brightness) << 2);
635}
636
637static int sd_start(struct gspca_dev *gspca_dev)
638{
639	struct sd *sd = (struct sd *) gspca_dev;
640	int ret, mode;
641	static u8 mode_tb[][3] = {
642	/*	  r00   r06   r07	*/
643		{0x00, 0x10, 0x10},	/* 640x480 */
644		{0x01, 0x1a, 0x1a},	/* 352x288 */
645		{0x02, 0x1c, 0x1d},	/* 320x240 */
646		{0x04, 0x34, 0x34},	/* 176x144 */
647		{0x05, 0x40, 0x40}	/* 160x120 */
648	};
649
650	if (sd->subtype == Nxultra)
651		write_vector(gspca_dev, spca505b_open_data_ccd);
652	else
653		write_vector(gspca_dev, spca505_open_data_ccd);
654	ret = reg_read(gspca_dev, 0x06, 0x16);
655
656	if (ret < 0) {
657		PERR("register read failed err: %d", ret);
658		return ret;
659	}
660	if (ret != 0x0101) {
661		pr_err("After vector read returns 0x%04x should be 0x0101\n",
662		       ret);
663	}
664
665	ret = reg_write(gspca_dev, 0x06, 0x16, 0x0a);
666	if (ret < 0)
667		return ret;
668	reg_write(gspca_dev, 0x05, 0xc2, 0x12);
669
670	/* necessary because without it we can see stream
671	 * only once after loading module */
672	/* stopping usb registers Tomasz change */
673	reg_write(gspca_dev, 0x02, 0x00, 0x00);
674
675	mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
676	reg_write(gspca_dev, SPCA50X_REG_COMPRESS, 0x00, mode_tb[mode][0]);
677	reg_write(gspca_dev, SPCA50X_REG_COMPRESS, 0x06, mode_tb[mode][1]);
678	reg_write(gspca_dev, SPCA50X_REG_COMPRESS, 0x07, mode_tb[mode][2]);
679
680	return reg_write(gspca_dev, SPCA50X_REG_USB,
681			 SPCA50X_USB_CTRL,
682			 SPCA50X_CUSB_ENABLE);
683}
684
685static void sd_stopN(struct gspca_dev *gspca_dev)
686{
687	/* Disable ISO packet machine */
688	reg_write(gspca_dev, 0x02, 0x00, 0x00);
689}
690
691/* called on streamoff with alt 0 and on disconnect */
692static void sd_stop0(struct gspca_dev *gspca_dev)
693{
694	if (!gspca_dev->present)
695		return;
696
697	/* This maybe reset or power control */
698	reg_write(gspca_dev, 0x03, 0x03, 0x20);
699	reg_write(gspca_dev, 0x03, 0x01, 0x00);
700	reg_write(gspca_dev, 0x03, 0x00, 0x01);
701	reg_write(gspca_dev, 0x05, 0x10, 0x01);
702	reg_write(gspca_dev, 0x05, 0x11, 0x0f);
703}
704
705static void sd_pkt_scan(struct gspca_dev *gspca_dev,
706			u8 *data,			/* isoc packet */
707			int len)			/* iso packet length */
708{
709	switch (data[0]) {
710	case 0:				/* start of frame */
711		gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
712		data += SPCA50X_OFFSET_DATA;
713		len -= SPCA50X_OFFSET_DATA;
714		gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
715		break;
716	case 0xff:			/* drop */
717		break;
718	default:
719		data += 1;
720		len -= 1;
721		gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
722		break;
723	}
724}
725
726static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
727{
728	struct gspca_dev *gspca_dev =
729		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
730
731	gspca_dev->usb_err = 0;
732
733	if (!gspca_dev->streaming)
734		return 0;
735
736	switch (ctrl->id) {
737	case V4L2_CID_BRIGHTNESS:
738		setbrightness(gspca_dev, ctrl->val);
739		break;
740	}
741	return gspca_dev->usb_err;
742}
743
744static const struct v4l2_ctrl_ops sd_ctrl_ops = {
745	.s_ctrl = sd_s_ctrl,
746};
747
748static int sd_init_controls(struct gspca_dev *gspca_dev)
749{
750	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
751
752	gspca_dev->vdev.ctrl_handler = hdl;
753	v4l2_ctrl_handler_init(hdl, 5);
754	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
755			V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
756
757	if (hdl->error) {
758		pr_err("Could not initialize controls\n");
759		return hdl->error;
760	}
761	return 0;
762}
763
764/* sub-driver description */
765static const struct sd_desc sd_desc = {
766	.name = MODULE_NAME,
767	.config = sd_config,
768	.init_controls = sd_init_controls,
769	.init = sd_init,
770	.start = sd_start,
771	.stopN = sd_stopN,
772	.stop0 = sd_stop0,
773	.pkt_scan = sd_pkt_scan,
774};
775
776/* -- module initialisation -- */
777static const struct usb_device_id device_table[] = {
778	{USB_DEVICE(0x041e, 0x401d), .driver_info = Nxultra},
779	{USB_DEVICE(0x0733, 0x0430), .driver_info = IntelPCCameraPro},
780/*fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
781	{}
782};
783MODULE_DEVICE_TABLE(usb, device_table);
784
785/* -- device connect -- */
786static int sd_probe(struct usb_interface *intf,
787			const struct usb_device_id *id)
788{
789	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
790				THIS_MODULE);
791}
792
793static struct usb_driver sd_driver = {
794	.name = MODULE_NAME,
795	.id_table = device_table,
796	.probe = sd_probe,
797	.disconnect = gspca_disconnect,
798#ifdef CONFIG_PM
799	.suspend = gspca_suspend,
800	.resume = gspca_resume,
801	.reset_resume = gspca_resume,
802#endif
803};
804
805module_usb_driver(sd_driver);
806