blob: b31772c6ed32b1b9adadeab7fef840076fefa1b2 [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 exposure; /* rev12a only */
Hans de Goede0fc23d22008-09-04 16:22:57 -030036#define EXPOSURE_MIN 1
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -030037#define EXPOSURE_DEF 200
Hans de Goede0fc23d22008-09-04 16:22:57 -030038#define EXPOSURE_MAX (4095 - 900) /* see set_exposure */
Jean-Francois Moine7879d452008-09-03 16:47:32 -030039
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -030040 __u8 contrast; /* rev72a only */
41#define CONTRAST_MIN 0x00
42#define CONTRAST_DEF 0x20
43#define CONTRAST_MAX 0x3f
44
Jean-Francois Moine7879d452008-09-03 16:47:32 -030045 __u8 brightness; /* rev72a only */
46#define BRIGHTNESS_MIN 0
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -030047#define BRIGHTNESS_DEF 0x20
48#define BRIGHTNESS_MAX 0x3f
Jean-Francois Moine7879d452008-09-03 16:47:32 -030049
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -030050 __u8 white;
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 */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300221/* from ms-win */
222 {0x0035, 0x8651}, /* R gain for white balance */
223 {0x0040, 0x8652}, /* Gr gain for white balance */
224 {0x005f, 0x8653}, /* B gain for white balance */
225 {0x0040, 0x8654}, /* Gb gain for white balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300226 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
227
228 {0x0011, 0x8802},
229 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
230 {0x0081, 0x8702}, /* Master clock output enable */
231
232 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
233 /* Originally was 0x0010 (352x288 compression) */
234
235 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
236 {0x0003, 0x865c}, /* Vertical offset for valid lines */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300237 /***************/ /* sensor active */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300238 {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
239 {0x0021, 0x8805},
240 {0x0001, 0x8800},
241 {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
242 {0x0065, 0x8805},
243 {0x0001, 0x8800},
244 {0x0005, 0x8801}, /* 0x05 <- 0x2f */
245 {0x002f, 0x8805},
246 {0x0000, 0x8800},
247 {0x0006, 0x8801}, /* 0x06 <- 0 */
248 {0x0000, 0x8805},
249 {0x0000, 0x8800},
250 {0x000a, 0x8801}, /* 0x0a <- 2 */
251 {0x0002, 0x8805},
252 {0x0000, 0x8800},
253 {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
254 {0x0061, 0x8805},
255 {0x0010, 0x8800},
256 {0x0035, 0x8801}, /* 0x35 <-0x14 */
257 {0x0014, 0x8805},
258 {0x0000, 0x8800},
259 {0x0030, 0x8112}, /* ISO and drop packet enable */
260 {0x0000, 0x8112}, /* Some kind of reset ???? */
261 {0x0009, 0x8118}, /* Enable sensor and set standby */
262 {0x0000, 0x8114}, /* Software GPIO output data */
263 {0x0000, 0x8114}, /* Software GPIO output data */
264 {0x0001, 0x8114}, /* Software GPIO output data */
265 {0x0000, 0x8112}, /* Some kind of reset ??? */
266 {0x0003, 0x8701},
267 {0x0001, 0x8703},
268 {0x0011, 0x8118},
269 {0x0001, 0x8118},
270 /***************/
271 {0x0092, 0x8804},
272 {0x0010, 0x8802},
273 {0x000d, 0x8805},
274 {0x0001, 0x8801},
275 {0x0000, 0x8800},
276 {0x0018, 0x8805},
277 {0x0002, 0x8801},
278 {0x0000, 0x8800},
279 {0x0065, 0x8805},
280 {0x0004, 0x8801},
281 {0x0001, 0x8800},
282 {0x0021, 0x8805},
283 {0x0005, 0x8801},
284 {0x0000, 0x8800},
285 {0x00aa, 0x8805},
286 {0x0007, 0x8801}, /* mode 0xaa */
287 {0x0000, 0x8800},
288 {0x0004, 0x8805},
289 {0x0020, 0x8801},
290 {0x0015, 0x8800}, /* mode 0x0415 */
291 {0x0002, 0x8805},
292 {0x0039, 0x8801},
293 {0x0000, 0x8800},
294 {0x0010, 0x8805},
295 {0x0035, 0x8801},
296 {0x0000, 0x8800},
297 {0x0049, 0x8805},
298 {0x0009, 0x8801},
299 {0x0010, 0x8800},
300 {0x000b, 0x8805},
301 {0x0028, 0x8801},
302 {0x0000, 0x8800},
303 {0x000f, 0x8805},
304 {0x003b, 0x8801},
305 {0x0000, 0x8800},
306 {0x0000, 0x8805},
307 {0x003c, 0x8801},
308 {0x0000, 0x8800},
309 {0x0002, 0x8502},
310 {0x0039, 0x8801},
311 {0x0000, 0x8805},
312 {0x0000, 0x8800},
313
314 {0x0087, 0x8700}, /* overwrite by start */
315 {0x0081, 0x8702},
316 {0x0000, 0x8500},
317/* {0x0010, 0x8500}, -- Previous line was this */
318 {0x0002, 0x865b},
319 {0x0003, 0x865c},
320 /***************/
321 {0x0003, 0x8801}, /* 0x121-> 289 */
322 {0x0021, 0x8805},
323 {0x0001, 0x8800},
324 {0x0004, 0x8801}, /* 0x165 -> 357 */
325 {0x0065, 0x8805},
326 {0x0001, 0x8800},
327 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
328 {0x002f, 0x8805},
329 {0x0000, 0x8800},
330 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
331 {0x0000, 0x8805},
332 {0x0000, 0x8800},
333 {0x000a, 0x8801}, /* 0x01 //0x02 */
334 {0x0001, 0x8805},
335 {0x0000, 0x8800},
336 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
337 * 0001 0 | 000 0110 0001 */
338 {0x0061, 0x8805}, /* 61 31 */
339 {0x0008, 0x8800}, /* 08 */
340 {0x0035, 0x8801}, /* 0x14 - set gain general */
341 {0x001f, 0x8805}, /* 0x14 */
342 {0x0000, 0x8800},
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300343 {0x000e, 0x8112}, /* white balance - was 30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300344 {}
345};
346
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300347/******************** QC Express etch2 stuff ********************/
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300348static const __u16 Pb100_1map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300349 /* reg, value */
350 {0x8320, 0x3304},
351
352 {0x8303, 0x0125}, /* image area */
353 {0x8304, 0x0169},
354 {0x8328, 0x000b},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300355 {0x833c, 0x0001}, /*fixme: win:07*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300356
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300357 {0x832f, 0x1904}, /*fixme: was 0419*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300358 {0x8307, 0x00aa},
359 {0x8301, 0x0003},
360 {0x8302, 0x000e},
361 {}
362};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300363static const __u16 Pb100_2map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300364 /* reg, value */
365 {0x8339, 0x0000},
366 {0x8307, 0x00aa},
367 {}
368};
369
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300370static const __u16 spca561_161rev12A_data1[][2] = {
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300371 {0x29, 0x8118}, /* white balance - was 21 */
372 {0x08, 0x8114}, /* white balance - was 01 */
373 {0x0e, 0x8112}, /* white balance - was 00 */
374 {0x00, 0x8102}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300375 {0x92, 0x8804},
376 {0x04, 0x8802}, /* windows uses 08 */
377 {}
378};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300379static const __u16 spca561_161rev12A_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300380 {0x21, 0x8118},
381 {0x10, 0x8500},
382 {0x07, 0x8601},
383 {0x07, 0x8602},
384 {0x04, 0x8501},
385 {0x21, 0x8118},
386
387 {0x07, 0x8201}, /* windows uses 02 */
388 {0x08, 0x8200},
389 {0x01, 0x8200},
390
391 {0x00, 0x8114},
392 {0x01, 0x8114}, /* windows uses 00 */
393
394 {0x90, 0x8604},
395 {0x00, 0x8605},
396 {0xb0, 0x8603},
397
398 /* sensor gains */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300399 {0x07, 0x8601}, /* white balance - new */
400 {0x07, 0x8602}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300401 {0x00, 0x8610}, /* *red */
402 {0x00, 0x8611}, /* 3f *green */
403 {0x00, 0x8612}, /* green *blue */
404 {0x00, 0x8613}, /* blue *green */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300405 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
406 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
407 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
408 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300409
410 {0x0c, 0x8620}, /* 0c */
411 {0xc8, 0x8631}, /* c8 */
412 {0xc8, 0x8634}, /* c8 */
413 {0x23, 0x8635}, /* 23 */
414 {0x1f, 0x8636}, /* 1f */
415 {0xdd, 0x8637}, /* dd */
416 {0xe1, 0x8638}, /* e1 */
417 {0x1d, 0x8639}, /* 1d */
418 {0x21, 0x863a}, /* 21 */
419 {0xe3, 0x863b}, /* e3 */
420 {0xdf, 0x863c}, /* df */
421 {0xf0, 0x8505},
422 {0x32, 0x850a},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300423/* {0x99, 0x8700}, * - white balance - new (removed) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300424 {}
425};
426
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300427static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300428{
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300429 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300430
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300431 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
432 0, /* request */
433 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
434 value, index, NULL, 0, 500);
435 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
436 if (ret < 0)
437 PDEBUG(D_ERR, "reg write: error %d", ret);
438}
439
440static void write_vector(struct gspca_dev *gspca_dev,
441 const __u16 data[][2])
442{
443 struct usb_device *dev = gspca_dev->dev;
444 int i;
445
446 i = 0;
447 while (data[i][1] != 0) {
448 reg_w_val(dev, data[i][1], data[i][0]);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300449 i++;
450 }
451}
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300452
453/* read 'len' bytes to gspca_dev->usb_buf */
454static void reg_r(struct gspca_dev *gspca_dev,
455 __u16 index, __u16 length)
456{
457 usb_control_msg(gspca_dev->dev,
458 usb_rcvctrlpipe(gspca_dev->dev, 0),
459 0, /* request */
460 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
461 0, /* value */
462 index, gspca_dev->usb_buf, length, 500);
463}
464
465/* write 'len' bytes from gspca_dev->usb_buf */
466static void reg_w_buf(struct gspca_dev *gspca_dev,
467 __u16 index, __u16 len)
468{
469 usb_control_msg(gspca_dev->dev,
470 usb_sndctrlpipe(gspca_dev->dev, 0),
471 0, /* request */
472 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
473 0, /* value */
474 index, gspca_dev->usb_buf, len, 500);
475}
476
477static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
478{
479 int retry = 60;
480
481 reg_w_val(gspca_dev->dev, 0x8801, reg);
482 reg_w_val(gspca_dev->dev, 0x8805, value);
483 reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
484 do {
485 reg_r(gspca_dev, 0x8803, 1);
486 if (!gspca_dev->usb_buf[0])
487 return;
488 } while (--retry);
489}
490
491static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
492{
493 int retry = 60;
494 __u8 value;
495
496 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
497 reg_w_val(gspca_dev->dev, 0x8801, reg);
498 reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
499 do {
500 reg_r(gspca_dev, 0x8803, 1);
501 if (!gspca_dev->usb_buf[0]) {
502 reg_r(gspca_dev, 0x8800, 1);
503 value = gspca_dev->usb_buf[0];
504 reg_r(gspca_dev, 0x8805, 1);
505 return ((int) value << 8) | gspca_dev->usb_buf[0];
506 }
507 } while (--retry);
508 return -1;
509}
510
511static void sensor_mapwrite(struct gspca_dev *gspca_dev,
512 const __u16 (*sensormap)[2])
513{
514 while ((*sensormap)[0]) {
515 gspca_dev->usb_buf[0] = (*sensormap)[1];
516 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
517 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
518 sensormap++;
519 }
520}
521
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300522static void init_161rev12A(struct gspca_dev *gspca_dev)
523{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300524 write_vector(gspca_dev, spca561_161rev12A_data1);
525 sensor_mapwrite(gspca_dev, Pb100_1map8300);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300526/*fixme: should be in sd_start*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300527 write_vector(gspca_dev, spca561_161rev12A_data2);
528 sensor_mapwrite(gspca_dev, Pb100_2map8300);
529}
530
531/* this function is called at probe time */
532static int sd_config(struct gspca_dev *gspca_dev,
533 const struct usb_device_id *id)
534{
535 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300536 struct cam *cam;
537 __u16 vendor, product;
538 __u8 data1, data2;
539
540 /* Read frm global register the USB product and vendor IDs, just to
541 * prove that we can communicate with the device. This works, which
542 * confirms at we are communicating properly and that the device
543 * is a 561. */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300544 reg_r(gspca_dev, 0x8104, 1);
545 data1 = gspca_dev->usb_buf[0];
546 reg_r(gspca_dev, 0x8105, 1);
547 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300548 vendor = (data2 << 8) | data1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300549 reg_r(gspca_dev, 0x8106, 1);
550 data1 = gspca_dev->usb_buf[0];
551 reg_r(gspca_dev, 0x8107, 1);
552 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300553 product = (data2 << 8) | data1;
554 if (vendor != id->idVendor || product != id->idProduct) {
555 PDEBUG(D_PROBE, "Bad vendor / product from device");
556 return -EINVAL;
557 }
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300558
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300559 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300560 cam->epaddr = 0x01;
561 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300562
563 sd->chip_revision = id->driver_info;
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300564 if (sd->chip_revision == Rev012A) {
565 cam->cam_mode = sif_012a_mode;
566 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
567 } else {
568 cam->cam_mode = sif_072a_mode;
569 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
570 }
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300571 sd->brightness = BRIGHTNESS_DEF;
572 sd->contrast = CONTRAST_DEF;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300573 sd->white = WHITE_DEF;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300574 sd->exposure = EXPOSURE_DEF;
575 sd->autogain = AUTOGAIN_DEF;
576 sd->gain = GAIN_DEF;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300577 sd->expo12a = EXPO12A_DEF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300578 return 0;
579}
580
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300581/* this function is called at probe and resume time */
582static int sd_init_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300583{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300584 PDEBUG(D_STREAM, "Chip revision: 012a");
585 init_161rev12A(gspca_dev);
586 return 0;
587}
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300588static int sd_init_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300589{
590 PDEBUG(D_STREAM, "Chip revision: 072a");
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300591 write_vector(gspca_dev, rev72a_init_data);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300592 return 0;
593}
594
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300595/* rev 72a only */
596static void setbrightness(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300597{
598 struct sd *sd = (struct sd *) gspca_dev;
599 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300600 __u8 value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300601
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300602 value = sd->brightness;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300603
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300604 /* offsets for white balance */
605 reg_w_val(dev, 0x8611, value); /* R */
606 reg_w_val(dev, 0x8612, value); /* Gr */
607 reg_w_val(dev, 0x8613, value); /* B */
608 reg_w_val(dev, 0x8614, value); /* Gb */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300609}
610
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300611static void setwhite(struct gspca_dev *gspca_dev)
612{
613 struct sd *sd = (struct sd *) gspca_dev;
614 __u16 white;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300615 __u8 blue, red;
616 __u16 reg;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300617
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300618 /* try to emulate MS-win as possible */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300619 white = sd->white;
620 red = 0x20 + white * 3 / 8;
621 blue = 0x90 - white * 5 / 8;
622 if (sd->chip_revision == Rev012A) {
623 reg = 0x8614;
624 } else {
625 reg = 0x8651;
626 red += sd->contrast - 0x20;
627 blue += sd->contrast - 0x20;
628 }
629 reg_w_val(gspca_dev->dev, reg, red);
630 reg_w_val(gspca_dev->dev, reg + 2, blue);
631}
632
633static void setcontrast(struct gspca_dev *gspca_dev)
634{
635 struct sd *sd = (struct sd *) gspca_dev;
636 struct usb_device *dev = gspca_dev->dev;
637 __u8 value;
638
639 if (sd->chip_revision != Rev072A)
640 return;
641 value = sd->contrast + 0x20;
642
643 /* gains for white balance */
644 setwhite(gspca_dev);
645/* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
646 reg_w_val(dev, 0x8652, value); /* Gr */
647/* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
648 reg_w_val(dev, 0x8654, value); /* Gb */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300649}
650
651/* rev 12a only */
652static void setexposure(struct gspca_dev *gspca_dev)
653{
654 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300655 int expo;
Hans de Goede0fc23d22008-09-04 16:22:57 -0300656 int clock_divider;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300657
Hans de Goede0fc23d22008-09-04 16:22:57 -0300658 /* Register 0x8309 controls exposure for the spca561,
659 the basic exposure setting goes from 1-2047, where 1 is completely
660 dark and 2047 is very bright. It not only influences exposure but
661 also the framerate (to allow for longer exposure) from 1 - 300 it
662 only raises the exposure time then from 300 - 600 it halves the
663 framerate to be able to further raise the exposure time and for every
664 300 more it halves the framerate again. This allows for a maximum
665 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
666 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
667 configure a divider for the base framerate which us used at the
668 exposure setting of 1-300. These bits configure the base framerate
669 according to the following formula: fps = 60 / (value + 2) */
670 if (sd->exposure < 2048) {
671 expo = sd->exposure;
672 clock_divider = 0;
673 } else {
674 /* Add 900 to make the 0 setting of the second part of the
675 exposure equal to the 2047 setting of the first part. */
676 expo = (sd->exposure - 2048) + 900;
677 clock_divider = 3;
678 }
679 expo |= clock_divider << 11;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300680 gspca_dev->usb_buf[0] = expo;
681 gspca_dev->usb_buf[1] = expo >> 8;
682 reg_w_buf(gspca_dev, 0x8309, 2);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300683}
684
685/* rev 12a only */
686static void setgain(struct gspca_dev *gspca_dev)
687{
688 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300689
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300690 gspca_dev->usb_buf[0] = sd->gain;
691 gspca_dev->usb_buf[1] = 0;
692 reg_w_buf(gspca_dev, 0x8335, 2);
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300693}
694
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300695static void setautogain(struct gspca_dev *gspca_dev)
696{
697 struct sd *sd = (struct sd *) gspca_dev;
698
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300699 if (sd->autogain)
700 sd->ag_cnt = AG_CNT_START;
701 else
702 sd->ag_cnt = -1;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300703}
704
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300705static int sd_start_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300706{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300707 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300708 int mode;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300709 static const __u8 Reg8391[8] =
710 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300711
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300712 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300713 if (mode <= 1) {
714 /* Use compression on 320x240 and above */
715 reg_w_val(dev, 0x8500, 0x10 | mode);
716 } else {
717 /* I couldn't get the compression to work below 320x240
718 * Fortunately at these resolutions the bandwidth
719 * is sufficient to push raw frames at ~20fps */
720 reg_w_val(dev, 0x8500, mode);
721 } /* -- qq@kuku.eu.org */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300722
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300723 gspca_dev->usb_buf[0] = 0xaa;
724 gspca_dev->usb_buf[1] = 0x00;
725 reg_w_buf(gspca_dev, 0x8307, 2);
726 /* clock - lower 0x8X values lead to fps > 30 */
727 reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300728 /* 0x8f 0x85 0x27 clock */
729 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
730 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300731 memcpy(gspca_dev->usb_buf, Reg8391, 8);
732 reg_w_buf(gspca_dev, 0x8391, 8);
733 reg_w_buf(gspca_dev, 0x8390, 8);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300734 setwhite(gspca_dev);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300735 setautogain(gspca_dev);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300736/* setgain(gspca_dev); */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300737 setexposure(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300738 return 0;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300739}
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300740static int sd_start_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300741{
742 struct usb_device *dev = gspca_dev->dev;
743 int Clck;
744 int mode;
745
746 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
747 switch (mode) {
748 default:
749/* case 0:
750 case 1: */
751 Clck = 0x25;
752 break;
753 case 2:
754 Clck = 0x22;
755 break;
756 case 3:
757 Clck = 0x21;
758 break;
759 }
760 reg_w_val(dev, 0x8500, mode); /* mode */
761 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
762 reg_w_val(dev, 0x8112, 0x10 | 0x20);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300763 setcontrast(gspca_dev);
764/* setbrightness(gspca_dev); * fixme: bad values */
765 setwhite(gspca_dev);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300766 setautogain(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300767 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300768}
769
770static void sd_stopN(struct gspca_dev *gspca_dev)
771{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300772 struct sd *sd = (struct sd *) gspca_dev;
773
774 if (sd->chip_revision == Rev012A) {
775 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
776 } else {
777 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
778/* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
779 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300780}
781
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300782/* called on streamoff with alt 0 and on disconnect */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300783static void sd_stop0(struct gspca_dev *gspca_dev)
784{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300785 struct sd *sd = (struct sd *) gspca_dev;
786
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300787 if (!gspca_dev->present)
788 return;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300789 if (sd->chip_revision == Rev012A) {
790 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
791 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
792 }
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300793/* reg_w_val(gspca_dev->dev, 0x8114, 0); */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300794}
795
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300796static void do_autogain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300797{
798 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300799 int expotimes;
800 int pixelclk;
801 int gainG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300802 __u8 R, Gr, Gb, B;
803 int y;
804 __u8 luma_mean = 110;
805 __u8 luma_delta = 20;
806 __u8 spring = 4;
807
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300808 if (sd->ag_cnt < 0)
809 return;
810 if (--sd->ag_cnt >= 0)
811 return;
812 sd->ag_cnt = AG_CNT_START;
813
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300814 switch (sd->chip_revision) {
815 case Rev072A:
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300816 reg_r(gspca_dev, 0x8621, 1);
817 Gr = gspca_dev->usb_buf[0];
818 reg_r(gspca_dev, 0x8622, 1);
819 R = gspca_dev->usb_buf[0];
820 reg_r(gspca_dev, 0x8623, 1);
821 B = gspca_dev->usb_buf[0];
822 reg_r(gspca_dev, 0x8624, 1);
823 Gb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300824 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
825 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
826 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
827 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
828
829 if (y < luma_mean - luma_delta ||
830 y > luma_mean + luma_delta) {
831 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
832 pixelclk = 0x0800;
833 expotimes = expotimes & 0x07ff;
834 /* PDEBUG(D_PACK,
835 "Exposition Times 0x%03X Clock 0x%04X ",
836 expotimes,pixelclk); */
837 gainG = i2c_read(gspca_dev, 0x35, 0x10);
838 /* PDEBUG(D_PACK,
839 "reading Gain register %d", gainG); */
840
841 expotimes += (luma_mean - y) >> spring;
842 gainG += (luma_mean - y) / 50;
843 /* PDEBUG(D_PACK,
844 "compute expotimes %d gain %d",
845 expotimes,gainG); */
846
847 if (gainG > 0x3f)
848 gainG = 0x3f;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300849 else if (gainG < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300850 gainG = 3;
851 i2c_write(gspca_dev, gainG, 0x35);
852
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300853 if (expotimes > 0x0256)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300854 expotimes = 0x0256;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300855 else if (expotimes < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300856 expotimes = 3;
857 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
858 }
859 break;
860 case Rev012A:
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300861 reg_r(gspca_dev, 0x8330, 2);
862 if (gspca_dev->usb_buf[1] > 0x08) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300863 gspca_dev->usb_buf[0] = ++sd->expo12a;
864 gspca_dev->usb_buf[1] = 0;
865 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300866 } else if (gspca_dev->usb_buf[1] < 0x02) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300867 gspca_dev->usb_buf[0] = --sd->expo12a;
868 gspca_dev->usb_buf[1] = 0;
869 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300870 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300871 break;
872 }
873}
874
875static void sd_pkt_scan(struct gspca_dev *gspca_dev,
876 struct gspca_frame *frame, /* target */
877 __u8 *data, /* isoc packet */
878 int len) /* iso packet length */
879{
Hans de Goede0fc23d22008-09-04 16:22:57 -0300880 struct sd *sd = (struct sd *) gspca_dev;
881
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300882 switch (data[0]) { /* sequence number */
883 case 0: /* start of frame */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300884 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
885 data, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300886 data += SPCA561_OFFSET_DATA;
887 len -= SPCA561_OFFSET_DATA;
888 if (data[1] & 0x10) {
889 /* compressed bayer */
890 gspca_frame_add(gspca_dev, FIRST_PACKET,
891 frame, data, len);
892 } else {
Hans de Goede54ab92c2008-07-03 11:20:58 -0300893 /* raw bayer (with a header, which we skip) */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300894 if (sd->chip_revision == Rev012A) {
895 data += 20;
896 len -= 20;
897 } else {
898 data += 16;
899 len -= 16;
900 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300901 gspca_frame_add(gspca_dev, FIRST_PACKET,
902 frame, data, len);
903 }
904 return;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300905 case 0xff: /* drop (empty mpackets) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300906 return;
907 }
908 data++;
909 len--;
910 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
911}
912
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300913/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300914static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
915{
916 struct sd *sd = (struct sd *) gspca_dev;
917
918 sd->brightness = val;
919 if (gspca_dev->streaming)
920 setbrightness(gspca_dev);
921 return 0;
922}
923
924static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
925{
926 struct sd *sd = (struct sd *) gspca_dev;
927
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300928 *val = sd->brightness;
929 return 0;
930}
931
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300932/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300933static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
934{
935 struct sd *sd = (struct sd *) gspca_dev;
936
937 sd->contrast = val;
938 if (gspca_dev->streaming)
939 setcontrast(gspca_dev);
940 return 0;
941}
942
943static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
944{
945 struct sd *sd = (struct sd *) gspca_dev;
946
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300947 *val = sd->contrast;
948 return 0;
949}
950
951static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
952{
953 struct sd *sd = (struct sd *) gspca_dev;
954
955 sd->autogain = val;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300956 if (gspca_dev->streaming)
957 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300958 return 0;
959}
960
961static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
962{
963 struct sd *sd = (struct sd *) gspca_dev;
964
965 *val = sd->autogain;
966 return 0;
967}
968
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300969static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
970{
971 struct sd *sd = (struct sd *) gspca_dev;
972
973 sd->white = val;
974 if (gspca_dev->streaming)
975 setwhite(gspca_dev);
976 return 0;
977}
978
979static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
980{
981 struct sd *sd = (struct sd *) gspca_dev;
982
983 *val = sd->white;
984 return 0;
985}
986
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300987/* rev12a only */
988static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
989{
990 struct sd *sd = (struct sd *) gspca_dev;
991
992 sd->exposure = val;
993 if (gspca_dev->streaming)
994 setexposure(gspca_dev);
995 return 0;
996}
997
998static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
999{
1000 struct sd *sd = (struct sd *) gspca_dev;
1001
1002 *val = sd->exposure;
1003 return 0;
1004}
1005
1006/* rev12a only */
1007static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1008{
1009 struct sd *sd = (struct sd *) gspca_dev;
1010
1011 sd->gain = val;
1012 if (gspca_dev->streaming)
1013 setgain(gspca_dev);
1014 return 0;
1015}
1016
1017static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1018{
1019 struct sd *sd = (struct sd *) gspca_dev;
1020
1021 *val = sd->gain;
1022 return 0;
1023}
1024
1025/* control tables */
1026static struct ctrl sd_ctrls_12a[] = {
1027 {
1028 {
1029 .id = V4L2_CID_DO_WHITE_BALANCE,
1030 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab695ebd12008-10-04 14:18:37 -03001031 .name = "White Balance",
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001032 .minimum = WHITE_MIN,
1033 .maximum = WHITE_MAX,
1034 .step = 1,
1035 .default_value = WHITE_DEF,
1036 },
1037 .set = sd_setwhite,
1038 .get = sd_getwhite,
1039 },
1040 {
1041 {
1042 .id = V4L2_CID_EXPOSURE,
1043 .type = V4L2_CTRL_TYPE_INTEGER,
1044 .name = "Exposure",
1045 .minimum = EXPOSURE_MIN,
1046 .maximum = EXPOSURE_MAX,
1047 .step = 1,
1048 .default_value = EXPOSURE_DEF,
1049 },
1050 .set = sd_setexposure,
1051 .get = sd_getexposure,
1052 },
1053 {
1054 {
1055 .id = V4L2_CID_AUTOGAIN,
1056 .type = V4L2_CTRL_TYPE_BOOLEAN,
1057 .name = "Auto Gain",
1058 .minimum = AUTOGAIN_MIN,
1059 .maximum = AUTOGAIN_MAX,
1060 .step = 1,
1061 .default_value = AUTOGAIN_DEF,
1062 },
1063 .set = sd_setautogain,
1064 .get = sd_getautogain,
1065 },
1066 {
1067 {
1068 .id = V4L2_CID_GAIN,
1069 .type = V4L2_CTRL_TYPE_INTEGER,
1070 .name = "Gain",
1071 .minimum = GAIN_MIN,
1072 .maximum = GAIN_MAX,
1073 .step = 1,
1074 .default_value = GAIN_DEF,
1075 },
1076 .set = sd_setgain,
1077 .get = sd_getgain,
1078 },
1079};
1080
1081static struct ctrl sd_ctrls_72a[] = {
1082 {
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -03001083 {
1084 .id = V4L2_CID_DO_WHITE_BALANCE,
1085 .type = V4L2_CTRL_TYPE_INTEGER,
1086 .name = "White Balance",
1087 .minimum = WHITE_MIN,
1088 .maximum = WHITE_MAX,
1089 .step = 1,
1090 .default_value = WHITE_DEF,
1091 },
1092 .set = sd_setwhite,
1093 .get = sd_getwhite,
1094 },
1095 {
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001096 {
1097 .id = V4L2_CID_BRIGHTNESS,
1098 .type = V4L2_CTRL_TYPE_INTEGER,
1099 .name = "Brightness",
1100 .minimum = BRIGHTNESS_MIN,
1101 .maximum = BRIGHTNESS_MAX,
1102 .step = 1,
1103 .default_value = BRIGHTNESS_DEF,
1104 },
1105 .set = sd_setbrightness,
1106 .get = sd_getbrightness,
1107 },
1108 {
1109 {
1110 .id = V4L2_CID_CONTRAST,
1111 .type = V4L2_CTRL_TYPE_INTEGER,
1112 .name = "Contrast",
1113 .minimum = CONTRAST_MIN,
1114 .maximum = CONTRAST_MAX,
1115 .step = 1,
1116 .default_value = CONTRAST_DEF,
1117 },
1118 .set = sd_setcontrast,
1119 .get = sd_getcontrast,
1120 },
1121 {
1122 {
1123 .id = V4L2_CID_AUTOGAIN,
1124 .type = V4L2_CTRL_TYPE_BOOLEAN,
1125 .name = "Auto Gain",
1126 .minimum = AUTOGAIN_MIN,
1127 .maximum = AUTOGAIN_MAX,
1128 .step = 1,
1129 .default_value = AUTOGAIN_DEF,
1130 },
1131 .set = sd_setautogain,
1132 .get = sd_getautogain,
1133 },
1134};
1135
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001136/* sub-driver description */
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001137static const struct sd_desc sd_desc_12a = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001138 .name = MODULE_NAME,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001139 .ctrls = sd_ctrls_12a,
1140 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001141 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001142 .init = sd_init_12a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001143 .start = sd_start_12a,
1144 .stopN = sd_stopN,
1145 .stop0 = sd_stop0,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001146 .pkt_scan = sd_pkt_scan,
1147/* .dq_callback = do_autogain, * fixme */
1148};
1149static const struct sd_desc sd_desc_72a = {
1150 .name = MODULE_NAME,
1151 .ctrls = sd_ctrls_72a,
1152 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1153 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001154 .init = sd_init_72a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001155 .start = sd_start_72a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001156 .stopN = sd_stopN,
1157 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001158 .pkt_scan = sd_pkt_scan,
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -03001159 .dq_callback = do_autogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001160};
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001161static const struct sd_desc *sd_desc[2] = {
1162 &sd_desc_12a,
1163 &sd_desc_72a
1164};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001165
1166/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001167static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine87581aa2008-07-26 14:30:01 -03001168 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1169 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1170 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1171 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1172 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1173 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1174 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1175 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1176 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1177 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1178 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1179 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1180 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1181 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1182 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001183 {}
1184};
1185
1186MODULE_DEVICE_TABLE(usb, device_table);
1187
1188/* -- device connect -- */
1189static int sd_probe(struct usb_interface *intf,
1190 const struct usb_device_id *id)
1191{
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001192 return gspca_dev_probe(intf, id,
1193 sd_desc[id->driver_info],
1194 sizeof(struct sd),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001195 THIS_MODULE);
1196}
1197
1198static struct usb_driver sd_driver = {
1199 .name = MODULE_NAME,
1200 .id_table = device_table,
1201 .probe = sd_probe,
1202 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001203#ifdef CONFIG_PM
1204 .suspend = gspca_suspend,
1205 .resume = gspca_resume,
1206#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001207};
1208
1209/* -- module insert / remove -- */
1210static int __init sd_mod_init(void)
1211{
1212 if (usb_register(&sd_driver) < 0)
1213 return -1;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001214 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001215 return 0;
1216}
1217static void __exit sd_mod_exit(void)
1218{
1219 usb_deregister(&sd_driver);
1220 PDEBUG(D_PROBE, "deregistered");
1221}
1222
1223module_init(sd_mod_init);
1224module_exit(sd_mod_exit);