blob: ee24629f0f5c5c6b70fc87dbf4aecb50c5867696 [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
2 * Sunplus spca561 subdriver
3 *
4 * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
5 *
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#define MODULE_NAME "spca561"
24
25#include "gspca.h"
26
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030027MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
29MODULE_LICENSE("GPL");
30
31/* specific webcam descriptor */
32struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */
34
Jean-Francois Moine7879d452008-09-03 16:47:32 -030035 __u16 contrast; /* rev72a only */
36#define CONTRAST_MIN 0x0000
37#define CONTRAST_DEF 0x2000
38#define CONTRAST_MAX 0x3fff
39
40 __u16 exposure; /* rev12a only */
Hans de Goede0fc23d22008-09-04 16:22:57 -030041#define EXPOSURE_MIN 1
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -030042#define EXPOSURE_DEF 200
Hans de Goede0fc23d22008-09-04 16:22:57 -030043#define EXPOSURE_MAX (4095 - 900) /* see set_exposure */
Jean-Francois Moine7879d452008-09-03 16:47:32 -030044
45 __u8 brightness; /* rev72a only */
46#define BRIGHTNESS_MIN 0
47#define BRIGHTNESS_DEF 32
48#define BRIGHTNESS_MAX 63
49
50 __u8 white; /* rev12a only */
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -030051#define WHITE_MIN 1
Jean-Francois Moine7879d452008-09-03 16:47:32 -030052#define WHITE_DEF 0x40
53#define WHITE_MAX 0x7f
54
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030055 __u8 autogain;
Jean-Francois Moine7879d452008-09-03 16:47:32 -030056#define AUTOGAIN_MIN 0
57#define AUTOGAIN_DEF 1
58#define AUTOGAIN_MAX 1
59
60 __u8 gain; /* rev12a only */
61#define GAIN_MIN 0x0
62#define GAIN_DEF 0x24
63#define GAIN_MAX 0x24
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030064
Jean-Francois Moined698dc62008-09-03 16:47:51 -030065#define EXPO12A_DEF 3
66 __u8 expo12a; /* expo/gain? for rev 12a */
67
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030068 __u8 chip_revision;
Jean-Francois Moine7879d452008-09-03 16:47:32 -030069#define Rev012A 0
70#define Rev072A 1
71
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030072 signed char ag_cnt;
73#define AG_CNT_START 13
74};
75
Jean-Francois Moineb77c0042008-09-04 16:22:56 -030076static struct v4l2_pix_format sif_012a_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -030077 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
78 .bytesperline = 160,
79 .sizeimage = 160 * 120,
80 .colorspace = V4L2_COLORSPACE_SRGB,
81 .priv = 3},
82 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
83 .bytesperline = 176,
84 .sizeimage = 176 * 144,
85 .colorspace = V4L2_COLORSPACE_SRGB,
86 .priv = 2},
87 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
88 .bytesperline = 320,
89 .sizeimage = 320 * 240 * 4 / 8,
90 .colorspace = V4L2_COLORSPACE_SRGB,
91 .priv = 1},
92 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
93 .bytesperline = 352,
94 .sizeimage = 352 * 288 * 4 / 8,
95 .colorspace = V4L2_COLORSPACE_SRGB,
96 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030097};
98
Jean-Francois Moineb77c0042008-09-04 16:22:56 -030099static struct v4l2_pix_format sif_072a_mode[] = {
100 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
101 .bytesperline = 160,
102 .sizeimage = 160 * 120,
103 .colorspace = V4L2_COLORSPACE_SRGB,
104 .priv = 3},
105 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
106 .bytesperline = 176,
107 .sizeimage = 176 * 144,
108 .colorspace = V4L2_COLORSPACE_SRGB,
109 .priv = 2},
110 {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
111 .bytesperline = 320,
112 .sizeimage = 320 * 240,
113 .colorspace = V4L2_COLORSPACE_SRGB,
114 .priv = 1},
115 {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
116 .bytesperline = 352,
117 .sizeimage = 352 * 288,
118 .colorspace = V4L2_COLORSPACE_SRGB,
119 .priv = 0},
120};
121
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300122/*
123 * Initialization data
124 * I'm not very sure how to split initialization from open data
125 * chunks. For now, we'll consider everything as initialization
126 */
127/* Frame packet header offsets for the spca561 */
128#define SPCA561_OFFSET_SNAP 1
129#define SPCA561_OFFSET_TYPE 2
130#define SPCA561_OFFSET_COMPRESS 3
131#define SPCA561_OFFSET_FRAMSEQ 4
132#define SPCA561_OFFSET_GPIO 5
133#define SPCA561_OFFSET_USBBUFF 6
134#define SPCA561_OFFSET_WIN2GRAVE 7
135#define SPCA561_OFFSET_WIN2RAVE 8
136#define SPCA561_OFFSET_WIN2BAVE 9
137#define SPCA561_OFFSET_WIN2GBAVE 10
138#define SPCA561_OFFSET_WIN1GRAVE 11
139#define SPCA561_OFFSET_WIN1RAVE 12
140#define SPCA561_OFFSET_WIN1BAVE 13
141#define SPCA561_OFFSET_WIN1GBAVE 14
142#define SPCA561_OFFSET_FREQ 15
143#define SPCA561_OFFSET_VSYNC 16
144#define SPCA561_OFFSET_DATA 1
145#define SPCA561_INDEX_I2C_BASE 0x8800
146#define SPCA561_SNAPBIT 0x20
147#define SPCA561_SNAPCTRL 0x40
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300148
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300149static const __u16 rev72a_init_data[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300150 {0x0000, 0x8114}, /* Software GPIO output data */
151 {0x0001, 0x8114}, /* Software GPIO output data */
152 {0x0000, 0x8112}, /* Some kind of reset */
153 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
154 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
155 {0x0011, 0x8118}, /* Enable and conf sensor */
156 {0x0001, 0x8118}, /* Conf sensor */
157 {0x0092, 0x8804}, /* I know nothing about these */
158 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
159 /***************/
160 {0x000d, 0x8805}, /* sensor default setting */
161 {0x0001, 0x8801}, /* 1 <- 0x0d */
162 {0x0000, 0x8800},
163 {0x0018, 0x8805},
164 {0x0002, 0x8801}, /* 2 <- 0x18 */
165 {0x0000, 0x8800},
166 {0x0065, 0x8805},
167 {0x0004, 0x8801}, /* 4 <- 0x01 0x65 */
168 {0x0001, 0x8800},
169 {0x0021, 0x8805},
170 {0x0005, 0x8801}, /* 5 <- 0x21 */
171 {0x0000, 0x8800},
172 {0x00aa, 0x8805},
173 {0x0007, 0x8801}, /* 7 <- 0xaa */
174 {0x0000, 0x8800},
175 {0x0004, 0x8805},
176 {0x0020, 0x8801}, /* 0x20 <- 0x15 0x04 */
177 {0x0015, 0x8800},
178 {0x0002, 0x8805},
179 {0x0039, 0x8801}, /* 0x39 <- 0x02 */
180 {0x0000, 0x8800},
181 {0x0010, 0x8805},
182 {0x0035, 0x8801}, /* 0x35 <- 0x10 */
183 {0x0000, 0x8800},
184 {0x0049, 0x8805},
185 {0x0009, 0x8801}, /* 0x09 <- 0x10 0x49 */
186 {0x0010, 0x8800},
187 {0x000b, 0x8805},
188 {0x0028, 0x8801}, /* 0x28 <- 0x0b */
189 {0x0000, 0x8800},
190 {0x000f, 0x8805},
191 {0x003b, 0x8801}, /* 0x3b <- 0x0f */
192 {0x0000, 0x8800},
193 {0x0000, 0x8805},
194 {0x003c, 0x8801}, /* 0x3c <- 0x00 */
195 {0x0000, 0x8800},
196 /***************/
197 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
198 {0x0000, 0x8602}, /* Optical black level for user setting */
199 {0x0060, 0x8604}, /* Optical black horizontal offset */
200 {0x0002, 0x8605}, /* Optical black vertical offset */
201 {0x0000, 0x8603}, /* Non-automatic optical black level */
202 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
203 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
204 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
205 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
206 {0x00e0, 0x8406}, /* Memory buffer threshold */
207 {0x0000, 0x8660}, /* Compensation memory stuff */
208 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
209 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
210 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
211 {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
212 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
213 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
214 {0x0010, 0x8660}, /* Compensation memory stuff */
215 {0x0018, 0x8660}, /* Compensation memory stuff */
216
217 {0x0004, 0x8611}, /* R offset for white balance */
218 {0x0004, 0x8612}, /* Gr offset for white balance */
219 {0x0007, 0x8613}, /* B offset for white balance */
220 {0x0000, 0x8614}, /* Gb offset for white balance */
221 {0x008c, 0x8651}, /* R gain for white balance */
222 {0x008c, 0x8652}, /* Gr gain for white balance */
223 {0x00b5, 0x8653}, /* B gain for white balance */
224 {0x008c, 0x8654}, /* Gb gain for white balance */
225 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
226
227 {0x0011, 0x8802},
228 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
229 {0x0081, 0x8702}, /* Master clock output enable */
230
231 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
232 /* Originally was 0x0010 (352x288 compression) */
233
234 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
235 {0x0003, 0x865c}, /* Vertical offset for valid lines */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300236 /***************/ /* sensor active */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300237 {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
238 {0x0021, 0x8805},
239 {0x0001, 0x8800},
240 {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
241 {0x0065, 0x8805},
242 {0x0001, 0x8800},
243 {0x0005, 0x8801}, /* 0x05 <- 0x2f */
244 {0x002f, 0x8805},
245 {0x0000, 0x8800},
246 {0x0006, 0x8801}, /* 0x06 <- 0 */
247 {0x0000, 0x8805},
248 {0x0000, 0x8800},
249 {0x000a, 0x8801}, /* 0x0a <- 2 */
250 {0x0002, 0x8805},
251 {0x0000, 0x8800},
252 {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
253 {0x0061, 0x8805},
254 {0x0010, 0x8800},
255 {0x0035, 0x8801}, /* 0x35 <-0x14 */
256 {0x0014, 0x8805},
257 {0x0000, 0x8800},
258 {0x0030, 0x8112}, /* ISO and drop packet enable */
259 {0x0000, 0x8112}, /* Some kind of reset ???? */
260 {0x0009, 0x8118}, /* Enable sensor and set standby */
261 {0x0000, 0x8114}, /* Software GPIO output data */
262 {0x0000, 0x8114}, /* Software GPIO output data */
263 {0x0001, 0x8114}, /* Software GPIO output data */
264 {0x0000, 0x8112}, /* Some kind of reset ??? */
265 {0x0003, 0x8701},
266 {0x0001, 0x8703},
267 {0x0011, 0x8118},
268 {0x0001, 0x8118},
269 /***************/
270 {0x0092, 0x8804},
271 {0x0010, 0x8802},
272 {0x000d, 0x8805},
273 {0x0001, 0x8801},
274 {0x0000, 0x8800},
275 {0x0018, 0x8805},
276 {0x0002, 0x8801},
277 {0x0000, 0x8800},
278 {0x0065, 0x8805},
279 {0x0004, 0x8801},
280 {0x0001, 0x8800},
281 {0x0021, 0x8805},
282 {0x0005, 0x8801},
283 {0x0000, 0x8800},
284 {0x00aa, 0x8805},
285 {0x0007, 0x8801}, /* mode 0xaa */
286 {0x0000, 0x8800},
287 {0x0004, 0x8805},
288 {0x0020, 0x8801},
289 {0x0015, 0x8800}, /* mode 0x0415 */
290 {0x0002, 0x8805},
291 {0x0039, 0x8801},
292 {0x0000, 0x8800},
293 {0x0010, 0x8805},
294 {0x0035, 0x8801},
295 {0x0000, 0x8800},
296 {0x0049, 0x8805},
297 {0x0009, 0x8801},
298 {0x0010, 0x8800},
299 {0x000b, 0x8805},
300 {0x0028, 0x8801},
301 {0x0000, 0x8800},
302 {0x000f, 0x8805},
303 {0x003b, 0x8801},
304 {0x0000, 0x8800},
305 {0x0000, 0x8805},
306 {0x003c, 0x8801},
307 {0x0000, 0x8800},
308 {0x0002, 0x8502},
309 {0x0039, 0x8801},
310 {0x0000, 0x8805},
311 {0x0000, 0x8800},
312
313 {0x0087, 0x8700}, /* overwrite by start */
314 {0x0081, 0x8702},
315 {0x0000, 0x8500},
316/* {0x0010, 0x8500}, -- Previous line was this */
317 {0x0002, 0x865b},
318 {0x0003, 0x865c},
319 /***************/
320 {0x0003, 0x8801}, /* 0x121-> 289 */
321 {0x0021, 0x8805},
322 {0x0001, 0x8800},
323 {0x0004, 0x8801}, /* 0x165 -> 357 */
324 {0x0065, 0x8805},
325 {0x0001, 0x8800},
326 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
327 {0x002f, 0x8805},
328 {0x0000, 0x8800},
329 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
330 {0x0000, 0x8805},
331 {0x0000, 0x8800},
332 {0x000a, 0x8801}, /* 0x01 //0x02 */
333 {0x0001, 0x8805},
334 {0x0000, 0x8800},
335 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
336 * 0001 0 | 000 0110 0001 */
337 {0x0061, 0x8805}, /* 61 31 */
338 {0x0008, 0x8800}, /* 08 */
339 {0x0035, 0x8801}, /* 0x14 - set gain general */
340 {0x001f, 0x8805}, /* 0x14 */
341 {0x0000, 0x8800},
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300342 {0x000e, 0x8112}, /* white balance - was 30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300343 {}
344};
345
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300346/******************** QC Express etch2 stuff ********************/
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300347static const __u16 Pb100_1map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300348 /* reg, value */
349 {0x8320, 0x3304},
350
351 {0x8303, 0x0125}, /* image area */
352 {0x8304, 0x0169},
353 {0x8328, 0x000b},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300354 {0x833c, 0x0001}, /*fixme: win:07*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300355
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300356 {0x832f, 0x1904}, /*fixme: was 0419*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300357 {0x8307, 0x00aa},
358 {0x8301, 0x0003},
359 {0x8302, 0x000e},
360 {}
361};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300362static const __u16 Pb100_2map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300363 /* reg, value */
364 {0x8339, 0x0000},
365 {0x8307, 0x00aa},
366 {}
367};
368
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300369static const __u16 spca561_161rev12A_data1[][2] = {
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300370 {0x29, 0x8118}, /* white balance - was 21 */
371 {0x08, 0x8114}, /* white balance - was 01 */
372 {0x0e, 0x8112}, /* white balance - was 00 */
373 {0x00, 0x8102}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300374 {0x92, 0x8804},
375 {0x04, 0x8802}, /* windows uses 08 */
376 {}
377};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300378static const __u16 spca561_161rev12A_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300379 {0x21, 0x8118},
380 {0x10, 0x8500},
381 {0x07, 0x8601},
382 {0x07, 0x8602},
383 {0x04, 0x8501},
384 {0x21, 0x8118},
385
386 {0x07, 0x8201}, /* windows uses 02 */
387 {0x08, 0x8200},
388 {0x01, 0x8200},
389
390 {0x00, 0x8114},
391 {0x01, 0x8114}, /* windows uses 00 */
392
393 {0x90, 0x8604},
394 {0x00, 0x8605},
395 {0xb0, 0x8603},
396
397 /* sensor gains */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300398 {0x07, 0x8601}, /* white balance - new */
399 {0x07, 0x8602}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300400 {0x00, 0x8610}, /* *red */
401 {0x00, 0x8611}, /* 3f *green */
402 {0x00, 0x8612}, /* green *blue */
403 {0x00, 0x8613}, /* blue *green */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300404 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
405 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
406 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
407 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300408
409 {0x0c, 0x8620}, /* 0c */
410 {0xc8, 0x8631}, /* c8 */
411 {0xc8, 0x8634}, /* c8 */
412 {0x23, 0x8635}, /* 23 */
413 {0x1f, 0x8636}, /* 1f */
414 {0xdd, 0x8637}, /* dd */
415 {0xe1, 0x8638}, /* e1 */
416 {0x1d, 0x8639}, /* 1d */
417 {0x21, 0x863a}, /* 21 */
418 {0xe3, 0x863b}, /* e3 */
419 {0xdf, 0x863c}, /* df */
420 {0xf0, 0x8505},
421 {0x32, 0x850a},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300422/* {0x99, 0x8700}, * - white balance - new (removed) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300423 {}
424};
425
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300426static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300427{
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300428 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300429
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300430 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
431 0, /* request */
432 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
433 value, index, NULL, 0, 500);
434 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
435 if (ret < 0)
436 PDEBUG(D_ERR, "reg write: error %d", ret);
437}
438
439static void write_vector(struct gspca_dev *gspca_dev,
440 const __u16 data[][2])
441{
442 struct usb_device *dev = gspca_dev->dev;
443 int i;
444
445 i = 0;
446 while (data[i][1] != 0) {
447 reg_w_val(dev, data[i][1], data[i][0]);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300448 i++;
449 }
450}
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300451
452/* read 'len' bytes to gspca_dev->usb_buf */
453static void reg_r(struct gspca_dev *gspca_dev,
454 __u16 index, __u16 length)
455{
456 usb_control_msg(gspca_dev->dev,
457 usb_rcvctrlpipe(gspca_dev->dev, 0),
458 0, /* request */
459 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
460 0, /* value */
461 index, gspca_dev->usb_buf, length, 500);
462}
463
464/* write 'len' bytes from gspca_dev->usb_buf */
465static void reg_w_buf(struct gspca_dev *gspca_dev,
466 __u16 index, __u16 len)
467{
468 usb_control_msg(gspca_dev->dev,
469 usb_sndctrlpipe(gspca_dev->dev, 0),
470 0, /* request */
471 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
472 0, /* value */
473 index, gspca_dev->usb_buf, len, 500);
474}
475
476static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
477{
478 int retry = 60;
479
480 reg_w_val(gspca_dev->dev, 0x8801, reg);
481 reg_w_val(gspca_dev->dev, 0x8805, value);
482 reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
483 do {
484 reg_r(gspca_dev, 0x8803, 1);
485 if (!gspca_dev->usb_buf[0])
486 return;
487 } while (--retry);
488}
489
490static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
491{
492 int retry = 60;
493 __u8 value;
494
495 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
496 reg_w_val(gspca_dev->dev, 0x8801, reg);
497 reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
498 do {
499 reg_r(gspca_dev, 0x8803, 1);
500 if (!gspca_dev->usb_buf[0]) {
501 reg_r(gspca_dev, 0x8800, 1);
502 value = gspca_dev->usb_buf[0];
503 reg_r(gspca_dev, 0x8805, 1);
504 return ((int) value << 8) | gspca_dev->usb_buf[0];
505 }
506 } while (--retry);
507 return -1;
508}
509
510static void sensor_mapwrite(struct gspca_dev *gspca_dev,
511 const __u16 (*sensormap)[2])
512{
513 while ((*sensormap)[0]) {
514 gspca_dev->usb_buf[0] = (*sensormap)[1];
515 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
516 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
517 sensormap++;
518 }
519}
520
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300521static void init_161rev12A(struct gspca_dev *gspca_dev)
522{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300523 write_vector(gspca_dev, spca561_161rev12A_data1);
524 sensor_mapwrite(gspca_dev, Pb100_1map8300);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300525/*fixme: should be in sd_start*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300526 write_vector(gspca_dev, spca561_161rev12A_data2);
527 sensor_mapwrite(gspca_dev, Pb100_2map8300);
528}
529
530/* this function is called at probe time */
531static int sd_config(struct gspca_dev *gspca_dev,
532 const struct usb_device_id *id)
533{
534 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300535 struct cam *cam;
536 __u16 vendor, product;
537 __u8 data1, data2;
538
539 /* Read frm global register the USB product and vendor IDs, just to
540 * prove that we can communicate with the device. This works, which
541 * confirms at we are communicating properly and that the device
542 * is a 561. */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300543 reg_r(gspca_dev, 0x8104, 1);
544 data1 = gspca_dev->usb_buf[0];
545 reg_r(gspca_dev, 0x8105, 1);
546 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300547 vendor = (data2 << 8) | data1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300548 reg_r(gspca_dev, 0x8106, 1);
549 data1 = gspca_dev->usb_buf[0];
550 reg_r(gspca_dev, 0x8107, 1);
551 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300552 product = (data2 << 8) | data1;
553 if (vendor != id->idVendor || product != id->idProduct) {
554 PDEBUG(D_PROBE, "Bad vendor / product from device");
555 return -EINVAL;
556 }
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300557
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300558 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300559 cam->epaddr = 0x01;
560 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300561
562 sd->chip_revision = id->driver_info;
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300563 if (sd->chip_revision == Rev012A) {
564 cam->cam_mode = sif_012a_mode;
565 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
566 } else {
567 cam->cam_mode = sif_072a_mode;
568 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
569 }
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300570 sd->brightness = BRIGHTNESS_DEF;
571 sd->contrast = CONTRAST_DEF;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300572 sd->white = WHITE_DEF;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300573 sd->exposure = EXPOSURE_DEF;
574 sd->autogain = AUTOGAIN_DEF;
575 sd->gain = GAIN_DEF;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300576 sd->expo12a = EXPO12A_DEF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300577 return 0;
578}
579
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300580/* this function is called at probe and resume time */
581static int sd_init_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300582{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300583 PDEBUG(D_STREAM, "Chip revision: 012a");
584 init_161rev12A(gspca_dev);
585 return 0;
586}
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300587static int sd_init_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300588{
589 PDEBUG(D_STREAM, "Chip revision: 072a");
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300590 write_vector(gspca_dev, rev72a_init_data);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300591 return 0;
592}
593
594static void setcontrast(struct gspca_dev *gspca_dev)
595{
596 struct sd *sd = (struct sd *) gspca_dev;
597 struct usb_device *dev = gspca_dev->dev;
598 __u8 lowb;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300599
600 switch (sd->chip_revision) {
601 case Rev072A:
602 lowb = sd->contrast >> 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300603 reg_w_val(dev, 0x8651, lowb);
604 reg_w_val(dev, 0x8652, lowb);
605 reg_w_val(dev, 0x8653, lowb);
606 reg_w_val(dev, 0x8654, lowb);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300607 break;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300608 default: {
609/* case Rev012A: { */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300610 static const __u8 Reg8391[] =
611 { 0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00 };
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300612
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300613 memcpy(gspca_dev->usb_buf, Reg8391, 8);
614 reg_w_buf(gspca_dev, 0x8391, 8);
615 reg_w_buf(gspca_dev, 0x8390, 8);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300616 break;
617 }
618 }
619}
620
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300621/* rev12a only */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300622static void setwhite(struct gspca_dev *gspca_dev)
623{
624 struct sd *sd = (struct sd *) gspca_dev;
625 __u16 white;
626 __u8 reg8614, reg8616;
627
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300628 white = sd->white;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300629 /* try to emulate MS-win as possible */
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300630 reg8616 = 0x90 - white * 5 / 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300631 reg_w_val(gspca_dev->dev, 0x8616, reg8616);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300632 reg8614 = 0x20 + white * 3 / 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300633 reg_w_val(gspca_dev->dev, 0x8614, reg8614);
634}
635
636/* rev 12a only */
637static void setexposure(struct gspca_dev *gspca_dev)
638{
639 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300640 int expo;
Hans de Goede0fc23d22008-09-04 16:22:57 -0300641 int clock_divider;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300642
Hans de Goede0fc23d22008-09-04 16:22:57 -0300643 /* Register 0x8309 controls exposure for the spca561,
644 the basic exposure setting goes from 1-2047, where 1 is completely
645 dark and 2047 is very bright. It not only influences exposure but
646 also the framerate (to allow for longer exposure) from 1 - 300 it
647 only raises the exposure time then from 300 - 600 it halves the
648 framerate to be able to further raise the exposure time and for every
649 300 more it halves the framerate again. This allows for a maximum
650 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
651 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
652 configure a divider for the base framerate which us used at the
653 exposure setting of 1-300. These bits configure the base framerate
654 according to the following formula: fps = 60 / (value + 2) */
655 if (sd->exposure < 2048) {
656 expo = sd->exposure;
657 clock_divider = 0;
658 } else {
659 /* Add 900 to make the 0 setting of the second part of the
660 exposure equal to the 2047 setting of the first part. */
661 expo = (sd->exposure - 2048) + 900;
662 clock_divider = 3;
663 }
664 expo |= clock_divider << 11;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300665 gspca_dev->usb_buf[0] = expo;
666 gspca_dev->usb_buf[1] = expo >> 8;
667 reg_w_buf(gspca_dev, 0x8309, 2);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300668}
669
670/* rev 12a only */
671static void setgain(struct gspca_dev *gspca_dev)
672{
673 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300674
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300675 gspca_dev->usb_buf[0] = sd->gain;
676 gspca_dev->usb_buf[1] = 0;
677 reg_w_buf(gspca_dev, 0x8335, 2);
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300678}
679
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300680static void setautogain(struct gspca_dev *gspca_dev)
681{
682 struct sd *sd = (struct sd *) gspca_dev;
683
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300684 if (sd->autogain)
685 sd->ag_cnt = AG_CNT_START;
686 else
687 sd->ag_cnt = -1;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300688}
689
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300690static int sd_start_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300691{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300692 struct usb_device *dev = gspca_dev->dev;
Hans de Goede0fc23d22008-09-04 16:22:57 -0300693 int Clck = 0x8a; /* lower 0x8X values lead to fps > 30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300694 __u8 Reg8307[] = { 0xaa, 0x00 };
695 int mode;
696
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300697 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300698 if (mode <= 1) {
699 /* Use compression on 320x240 and above */
700 reg_w_val(dev, 0x8500, 0x10 | mode);
701 } else {
702 /* I couldn't get the compression to work below 320x240
703 * Fortunately at these resolutions the bandwidth
704 * is sufficient to push raw frames at ~20fps */
705 reg_w_val(dev, 0x8500, mode);
706 } /* -- qq@kuku.eu.org */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300707
708 memcpy(gspca_dev->usb_buf, Reg8307, sizeof Reg8307);
709 reg_w_buf(gspca_dev, 0x8307, sizeof Reg8307);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300710 reg_w_val(gspca_dev->dev, 0x8700, Clck);
711 /* 0x8f 0x85 0x27 clock */
712 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
713 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
714 setcontrast(gspca_dev);
715 setwhite(gspca_dev);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300716 setautogain(gspca_dev);
Hans de Goede0fc23d22008-09-04 16:22:57 -0300717 setexposure(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300718 return 0;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300719}
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300720static int sd_start_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300721{
722 struct usb_device *dev = gspca_dev->dev;
723 int Clck;
724 int mode;
725
726 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
727 switch (mode) {
728 default:
729/* case 0:
730 case 1: */
731 Clck = 0x25;
732 break;
733 case 2:
734 Clck = 0x22;
735 break;
736 case 3:
737 Clck = 0x21;
738 break;
739 }
740 reg_w_val(dev, 0x8500, mode); /* mode */
741 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
742 reg_w_val(dev, 0x8112, 0x10 | 0x20);
743 setautogain(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300744 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300745}
746
747static void sd_stopN(struct gspca_dev *gspca_dev)
748{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300749 struct sd *sd = (struct sd *) gspca_dev;
750
751 if (sd->chip_revision == Rev012A) {
752 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
753 } else {
754 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
755/* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
756 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300757}
758
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300759/* called on streamoff with alt 0 and on disconnect */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300760static void sd_stop0(struct gspca_dev *gspca_dev)
761{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300762 struct sd *sd = (struct sd *) gspca_dev;
763
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300764 if (!gspca_dev->present)
765 return;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300766 if (sd->chip_revision == Rev012A) {
767 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
768 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
769 }
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300770/* reg_w_val(gspca_dev->dev, 0x8114, 0); */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300771}
772
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300773static void do_autogain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300774{
775 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300776 int expotimes;
777 int pixelclk;
778 int gainG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300779 __u8 R, Gr, Gb, B;
780 int y;
781 __u8 luma_mean = 110;
782 __u8 luma_delta = 20;
783 __u8 spring = 4;
784
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300785 if (sd->ag_cnt < 0)
786 return;
787 if (--sd->ag_cnt >= 0)
788 return;
789 sd->ag_cnt = AG_CNT_START;
790
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300791 switch (sd->chip_revision) {
792 case Rev072A:
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300793 reg_r(gspca_dev, 0x8621, 1);
794 Gr = gspca_dev->usb_buf[0];
795 reg_r(gspca_dev, 0x8622, 1);
796 R = gspca_dev->usb_buf[0];
797 reg_r(gspca_dev, 0x8623, 1);
798 B = gspca_dev->usb_buf[0];
799 reg_r(gspca_dev, 0x8624, 1);
800 Gb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300801 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
802 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
803 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
804 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
805
806 if (y < luma_mean - luma_delta ||
807 y > luma_mean + luma_delta) {
808 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
809 pixelclk = 0x0800;
810 expotimes = expotimes & 0x07ff;
811 /* PDEBUG(D_PACK,
812 "Exposition Times 0x%03X Clock 0x%04X ",
813 expotimes,pixelclk); */
814 gainG = i2c_read(gspca_dev, 0x35, 0x10);
815 /* PDEBUG(D_PACK,
816 "reading Gain register %d", gainG); */
817
818 expotimes += (luma_mean - y) >> spring;
819 gainG += (luma_mean - y) / 50;
820 /* PDEBUG(D_PACK,
821 "compute expotimes %d gain %d",
822 expotimes,gainG); */
823
824 if (gainG > 0x3f)
825 gainG = 0x3f;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300826 else if (gainG < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300827 gainG = 3;
828 i2c_write(gspca_dev, gainG, 0x35);
829
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300830 if (expotimes > 0x0256)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300831 expotimes = 0x0256;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300832 else if (expotimes < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300833 expotimes = 3;
834 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
835 }
836 break;
837 case Rev012A:
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300838 reg_r(gspca_dev, 0x8330, 2);
839 if (gspca_dev->usb_buf[1] > 0x08) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300840 gspca_dev->usb_buf[0] = ++sd->expo12a;
841 gspca_dev->usb_buf[1] = 0;
842 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300843 } else if (gspca_dev->usb_buf[1] < 0x02) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300844 gspca_dev->usb_buf[0] = --sd->expo12a;
845 gspca_dev->usb_buf[1] = 0;
846 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300847 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300848 break;
849 }
850}
851
852static void sd_pkt_scan(struct gspca_dev *gspca_dev,
853 struct gspca_frame *frame, /* target */
854 __u8 *data, /* isoc packet */
855 int len) /* iso packet length */
856{
Hans de Goede0fc23d22008-09-04 16:22:57 -0300857 struct sd *sd = (struct sd *) gspca_dev;
858
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300859 switch (data[0]) { /* sequence number */
860 case 0: /* start of frame */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300861 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
862 data, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300863 data += SPCA561_OFFSET_DATA;
864 len -= SPCA561_OFFSET_DATA;
865 if (data[1] & 0x10) {
866 /* compressed bayer */
867 gspca_frame_add(gspca_dev, FIRST_PACKET,
868 frame, data, len);
869 } else {
Hans de Goede54ab92c2008-07-03 11:20:58 -0300870 /* raw bayer (with a header, which we skip) */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300871 if (sd->chip_revision == Rev012A) {
872 data += 20;
873 len -= 20;
874 } else {
875 data += 16;
876 len -= 16;
877 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300878 gspca_frame_add(gspca_dev, FIRST_PACKET,
879 frame, data, len);
880 }
881 return;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300882 case 0xff: /* drop (empty mpackets) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300883 return;
884 }
885 data++;
886 len--;
887 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
888}
889
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300890/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300891static void setbrightness(struct gspca_dev *gspca_dev)
892{
893 struct sd *sd = (struct sd *) gspca_dev;
894 __u8 value;
895
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300896 value = sd->brightness;
897 reg_w_val(gspca_dev->dev, 0x8611, value);
898 reg_w_val(gspca_dev->dev, 0x8612, value);
899 reg_w_val(gspca_dev->dev, 0x8613, value);
900 reg_w_val(gspca_dev->dev, 0x8614, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300901}
902
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300903/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300904static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
905{
906 struct sd *sd = (struct sd *) gspca_dev;
907
908 sd->brightness = val;
909 if (gspca_dev->streaming)
910 setbrightness(gspca_dev);
911 return 0;
912}
913
914static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
915{
916 struct sd *sd = (struct sd *) gspca_dev;
917
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300918 *val = sd->brightness;
919 return 0;
920}
921
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300922/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300923static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
924{
925 struct sd *sd = (struct sd *) gspca_dev;
926
927 sd->contrast = val;
928 if (gspca_dev->streaming)
929 setcontrast(gspca_dev);
930 return 0;
931}
932
933static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
934{
935 struct sd *sd = (struct sd *) gspca_dev;
936
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300937 *val = sd->contrast;
938 return 0;
939}
940
941static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
942{
943 struct sd *sd = (struct sd *) gspca_dev;
944
945 sd->autogain = val;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300946 if (gspca_dev->streaming)
947 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300948 return 0;
949}
950
951static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
952{
953 struct sd *sd = (struct sd *) gspca_dev;
954
955 *val = sd->autogain;
956 return 0;
957}
958
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300959/* rev12a only */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300960static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
961{
962 struct sd *sd = (struct sd *) gspca_dev;
963
964 sd->white = val;
965 if (gspca_dev->streaming)
966 setwhite(gspca_dev);
967 return 0;
968}
969
970static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
971{
972 struct sd *sd = (struct sd *) gspca_dev;
973
974 *val = sd->white;
975 return 0;
976}
977
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300978/* rev12a only */
979static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
980{
981 struct sd *sd = (struct sd *) gspca_dev;
982
983 sd->exposure = val;
984 if (gspca_dev->streaming)
985 setexposure(gspca_dev);
986 return 0;
987}
988
989static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
990{
991 struct sd *sd = (struct sd *) gspca_dev;
992
993 *val = sd->exposure;
994 return 0;
995}
996
997/* rev12a only */
998static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
999{
1000 struct sd *sd = (struct sd *) gspca_dev;
1001
1002 sd->gain = val;
1003 if (gspca_dev->streaming)
1004 setgain(gspca_dev);
1005 return 0;
1006}
1007
1008static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1009{
1010 struct sd *sd = (struct sd *) gspca_dev;
1011
1012 *val = sd->gain;
1013 return 0;
1014}
1015
1016/* control tables */
1017static struct ctrl sd_ctrls_12a[] = {
1018 {
1019 {
1020 .id = V4L2_CID_DO_WHITE_BALANCE,
1021 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab695ebd12008-10-04 14:18:37 -03001022 .name = "White Balance",
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001023 .minimum = WHITE_MIN,
1024 .maximum = WHITE_MAX,
1025 .step = 1,
1026 .default_value = WHITE_DEF,
1027 },
1028 .set = sd_setwhite,
1029 .get = sd_getwhite,
1030 },
1031 {
1032 {
1033 .id = V4L2_CID_EXPOSURE,
1034 .type = V4L2_CTRL_TYPE_INTEGER,
1035 .name = "Exposure",
1036 .minimum = EXPOSURE_MIN,
1037 .maximum = EXPOSURE_MAX,
1038 .step = 1,
1039 .default_value = EXPOSURE_DEF,
1040 },
1041 .set = sd_setexposure,
1042 .get = sd_getexposure,
1043 },
1044 {
1045 {
1046 .id = V4L2_CID_AUTOGAIN,
1047 .type = V4L2_CTRL_TYPE_BOOLEAN,
1048 .name = "Auto Gain",
1049 .minimum = AUTOGAIN_MIN,
1050 .maximum = AUTOGAIN_MAX,
1051 .step = 1,
1052 .default_value = AUTOGAIN_DEF,
1053 },
1054 .set = sd_setautogain,
1055 .get = sd_getautogain,
1056 },
1057 {
1058 {
1059 .id = V4L2_CID_GAIN,
1060 .type = V4L2_CTRL_TYPE_INTEGER,
1061 .name = "Gain",
1062 .minimum = GAIN_MIN,
1063 .maximum = GAIN_MAX,
1064 .step = 1,
1065 .default_value = GAIN_DEF,
1066 },
1067 .set = sd_setgain,
1068 .get = sd_getgain,
1069 },
1070};
1071
1072static struct ctrl sd_ctrls_72a[] = {
1073 {
1074 {
1075 .id = V4L2_CID_BRIGHTNESS,
1076 .type = V4L2_CTRL_TYPE_INTEGER,
1077 .name = "Brightness",
1078 .minimum = BRIGHTNESS_MIN,
1079 .maximum = BRIGHTNESS_MAX,
1080 .step = 1,
1081 .default_value = BRIGHTNESS_DEF,
1082 },
1083 .set = sd_setbrightness,
1084 .get = sd_getbrightness,
1085 },
1086 {
1087 {
1088 .id = V4L2_CID_CONTRAST,
1089 .type = V4L2_CTRL_TYPE_INTEGER,
1090 .name = "Contrast",
1091 .minimum = CONTRAST_MIN,
1092 .maximum = CONTRAST_MAX,
1093 .step = 1,
1094 .default_value = CONTRAST_DEF,
1095 },
1096 .set = sd_setcontrast,
1097 .get = sd_getcontrast,
1098 },
1099 {
1100 {
1101 .id = V4L2_CID_AUTOGAIN,
1102 .type = V4L2_CTRL_TYPE_BOOLEAN,
1103 .name = "Auto Gain",
1104 .minimum = AUTOGAIN_MIN,
1105 .maximum = AUTOGAIN_MAX,
1106 .step = 1,
1107 .default_value = AUTOGAIN_DEF,
1108 },
1109 .set = sd_setautogain,
1110 .get = sd_getautogain,
1111 },
1112};
1113
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001114/* sub-driver description */
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001115static const struct sd_desc sd_desc_12a = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001116 .name = MODULE_NAME,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001117 .ctrls = sd_ctrls_12a,
1118 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001119 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001120 .init = sd_init_12a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001121 .start = sd_start_12a,
1122 .stopN = sd_stopN,
1123 .stop0 = sd_stop0,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001124 .pkt_scan = sd_pkt_scan,
1125/* .dq_callback = do_autogain, * fixme */
1126};
1127static const struct sd_desc sd_desc_72a = {
1128 .name = MODULE_NAME,
1129 .ctrls = sd_ctrls_72a,
1130 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1131 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001132 .init = sd_init_72a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001133 .start = sd_start_72a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001134 .stopN = sd_stopN,
1135 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001136 .pkt_scan = sd_pkt_scan,
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -03001137 .dq_callback = do_autogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001138};
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001139static const struct sd_desc *sd_desc[2] = {
1140 &sd_desc_12a,
1141 &sd_desc_72a
1142};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001143
1144/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001145static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine87581aa2008-07-26 14:30:01 -03001146 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1147 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1148 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1149 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1150 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1151 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1152 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1153 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1154 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1155 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1156 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1157 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1158 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1159 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1160 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001161 {}
1162};
1163
1164MODULE_DEVICE_TABLE(usb, device_table);
1165
1166/* -- device connect -- */
1167static int sd_probe(struct usb_interface *intf,
1168 const struct usb_device_id *id)
1169{
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001170 return gspca_dev_probe(intf, id,
1171 sd_desc[id->driver_info],
1172 sizeof(struct sd),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001173 THIS_MODULE);
1174}
1175
1176static struct usb_driver sd_driver = {
1177 .name = MODULE_NAME,
1178 .id_table = device_table,
1179 .probe = sd_probe,
1180 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001181#ifdef CONFIG_PM
1182 .suspend = gspca_suspend,
1183 .resume = gspca_resume,
1184#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001185};
1186
1187/* -- module insert / remove -- */
1188static int __init sd_mod_init(void)
1189{
1190 if (usb_register(&sd_driver) < 0)
1191 return -1;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001192 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001193 return 0;
1194}
1195static void __exit sd_mod_exit(void)
1196{
1197 usb_deregister(&sd_driver);
1198 PDEBUG(D_PROBE, "deregistered");
1199}
1200
1201module_init(sd_mod_init);
1202module_exit(sd_mod_exit);