blob: 173c0c43fd1f69dfabf0dd1c8084d4115565add7 [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 Moinecc611b82008-12-29 07:49:41 -030076static const 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 Moinecc611b82008-12-29 07:49:41 -030099static const struct v4l2_pix_format sif_072a_mode[] = {
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300100 {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 Moinef8a04a62008-12-19 07:21:26 -0300149static const __u16 rev72a_init_data1[][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 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300159 {0x000d, 0x8805}, /* sensor default setting */
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300160 {}
161};
162static const __u16 rev72a_init_sensor1[][2] = {
163 /* ms-win values */
164 {0x0001, 0x0018}, /* 0x01 <- 0x0d */
165 {0x0002, 0x0065}, /* 0x02 <- 0x18 */
166 {0x0004, 0x0121}, /* 0x04 <- 0x0165 */
167 {0x0005, 0x00aa}, /* 0x05 <- 0x21 */
168 {0x0007, 0x0004}, /* 0x07 <- 0xaa */
169 {0x0020, 0x1502}, /* 0x20 <- 0x1504 */
170 {0x0039, 0x0010}, /* 0x39 <- 0x02 */
171 {0x0035, 0x0049}, /* 0x35 <- 0x10 */
172 {0x0009, 0x100b}, /* 0x09 <- 0x1049 */
173 {0x0028, 0x000f}, /* 0x28 <- 0x0b */
174 {0x003b, 0x003c}, /* 0x3b <- 0x0f */
175 {0x003c, 0x0000}, /* 0x3c <- 0x00 */
176 {}
177};
178static const __u16 rev72a_init_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300179 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
180 {0x0000, 0x8602}, /* Optical black level for user setting */
181 {0x0060, 0x8604}, /* Optical black horizontal offset */
182 {0x0002, 0x8605}, /* Optical black vertical offset */
183 {0x0000, 0x8603}, /* Non-automatic optical black level */
184 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
185 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
186 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
187 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
188 {0x00e0, 0x8406}, /* Memory buffer threshold */
189 {0x0000, 0x8660}, /* Compensation memory stuff */
190 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
191 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
192 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
193 {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
194 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
195 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
196 {0x0010, 0x8660}, /* Compensation memory stuff */
197 {0x0018, 0x8660}, /* Compensation memory stuff */
198
199 {0x0004, 0x8611}, /* R offset for white balance */
200 {0x0004, 0x8612}, /* Gr offset for white balance */
201 {0x0007, 0x8613}, /* B offset for white balance */
202 {0x0000, 0x8614}, /* Gb offset for white balance */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300203/* from ms-win */
204 {0x0035, 0x8651}, /* R gain for white balance */
205 {0x0040, 0x8652}, /* Gr gain for white balance */
206 {0x005f, 0x8653}, /* B gain for white balance */
207 {0x0040, 0x8654}, /* Gb gain for white balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300208 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
209
210 {0x0011, 0x8802},
211 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
212 {0x0081, 0x8702}, /* Master clock output enable */
213
214 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
215 /* Originally was 0x0010 (352x288 compression) */
216
217 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
218 {0x0003, 0x865c}, /* Vertical offset for valid lines */
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300219 {}
220};
221static const __u16 rev72a_init_sensor2[][2] = {
222 /* ms-win values */
223 {0x0003, 0x0121}, /* 0x03 <- 0x01 0x21 //289 */
224 {0x0004, 0x0165}, /* 0x04 <- 0x01 0x65 //357 */
225 {0x0005, 0x002f}, /* 0x05 <- 0x2f */
226 {0x0006, 0x0000}, /* 0x06 <- 0 */
227 {0x000a, 0x0002}, /* 0x0a <- 2 */
228 {0x0009, 0x1061}, /* 0x09 <- 0x1061 */
229 {0x0035, 0x0014}, /* 0x35 <- 0x14 */
230 {}
231};
232static const __u16 rev72a_init_data3[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300233 {0x0030, 0x8112}, /* ISO and drop packet enable */
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300234/*fixme: should stop here*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300235 {0x0000, 0x8112}, /* Some kind of reset ???? */
236 {0x0009, 0x8118}, /* Enable sensor and set standby */
237 {0x0000, 0x8114}, /* Software GPIO output data */
238 {0x0000, 0x8114}, /* Software GPIO output data */
239 {0x0001, 0x8114}, /* Software GPIO output data */
240 {0x0000, 0x8112}, /* Some kind of reset ??? */
241 {0x0003, 0x8701},
242 {0x0001, 0x8703},
243 {0x0011, 0x8118},
244 {0x0001, 0x8118},
245 /***************/
246 {0x0092, 0x8804},
247 {0x0010, 0x8802},
248 {0x000d, 0x8805},
249 {0x0001, 0x8801},
250 {0x0000, 0x8800},
251 {0x0018, 0x8805},
252 {0x0002, 0x8801},
253 {0x0000, 0x8800},
254 {0x0065, 0x8805},
255 {0x0004, 0x8801},
256 {0x0001, 0x8800},
257 {0x0021, 0x8805},
258 {0x0005, 0x8801},
259 {0x0000, 0x8800},
260 {0x00aa, 0x8805},
261 {0x0007, 0x8801}, /* mode 0xaa */
262 {0x0000, 0x8800},
263 {0x0004, 0x8805},
264 {0x0020, 0x8801},
265 {0x0015, 0x8800}, /* mode 0x0415 */
266 {0x0002, 0x8805},
267 {0x0039, 0x8801},
268 {0x0000, 0x8800},
269 {0x0010, 0x8805},
270 {0x0035, 0x8801},
271 {0x0000, 0x8800},
272 {0x0049, 0x8805},
273 {0x0009, 0x8801},
274 {0x0010, 0x8800},
275 {0x000b, 0x8805},
276 {0x0028, 0x8801},
277 {0x0000, 0x8800},
278 {0x000f, 0x8805},
279 {0x003b, 0x8801},
280 {0x0000, 0x8800},
281 {0x0000, 0x8805},
282 {0x003c, 0x8801},
283 {0x0000, 0x8800},
284 {0x0002, 0x8502},
285 {0x0039, 0x8801},
286 {0x0000, 0x8805},
287 {0x0000, 0x8800},
288
289 {0x0087, 0x8700}, /* overwrite by start */
290 {0x0081, 0x8702},
291 {0x0000, 0x8500},
292/* {0x0010, 0x8500}, -- Previous line was this */
293 {0x0002, 0x865b},
294 {0x0003, 0x865c},
295 /***************/
296 {0x0003, 0x8801}, /* 0x121-> 289 */
297 {0x0021, 0x8805},
298 {0x0001, 0x8800},
299 {0x0004, 0x8801}, /* 0x165 -> 357 */
300 {0x0065, 0x8805},
301 {0x0001, 0x8800},
302 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
303 {0x002f, 0x8805},
304 {0x0000, 0x8800},
305 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
306 {0x0000, 0x8805},
307 {0x0000, 0x8800},
308 {0x000a, 0x8801}, /* 0x01 //0x02 */
309 {0x0001, 0x8805},
310 {0x0000, 0x8800},
311 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
312 * 0001 0 | 000 0110 0001 */
313 {0x0061, 0x8805}, /* 61 31 */
314 {0x0008, 0x8800}, /* 08 */
315 {0x0035, 0x8801}, /* 0x14 - set gain general */
316 {0x001f, 0x8805}, /* 0x14 */
317 {0x0000, 0x8800},
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300318 {0x000e, 0x8112}, /* white balance - was 30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300319 {}
320};
321
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300322/******************** QC Express etch2 stuff ********************/
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300323static const __u16 Pb100_1map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300324 /* reg, value */
325 {0x8320, 0x3304},
326
327 {0x8303, 0x0125}, /* image area */
328 {0x8304, 0x0169},
329 {0x8328, 0x000b},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300330 {0x833c, 0x0001}, /*fixme: win:07*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300331
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300332 {0x832f, 0x1904}, /*fixme: was 0419*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300333 {0x8307, 0x00aa},
334 {0x8301, 0x0003},
335 {0x8302, 0x000e},
336 {}
337};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300338static const __u16 Pb100_2map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300339 /* reg, value */
340 {0x8339, 0x0000},
341 {0x8307, 0x00aa},
342 {}
343};
344
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300345static const __u16 spca561_161rev12A_data1[][2] = {
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300346 {0x29, 0x8118}, /* white balance - was 21 */
347 {0x08, 0x8114}, /* white balance - was 01 */
348 {0x0e, 0x8112}, /* white balance - was 00 */
349 {0x00, 0x8102}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300350 {0x92, 0x8804},
351 {0x04, 0x8802}, /* windows uses 08 */
352 {}
353};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300354static const __u16 spca561_161rev12A_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300355 {0x21, 0x8118},
356 {0x10, 0x8500},
357 {0x07, 0x8601},
358 {0x07, 0x8602},
359 {0x04, 0x8501},
360 {0x21, 0x8118},
361
362 {0x07, 0x8201}, /* windows uses 02 */
363 {0x08, 0x8200},
364 {0x01, 0x8200},
365
366 {0x00, 0x8114},
367 {0x01, 0x8114}, /* windows uses 00 */
368
369 {0x90, 0x8604},
370 {0x00, 0x8605},
371 {0xb0, 0x8603},
372
373 /* sensor gains */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300374 {0x07, 0x8601}, /* white balance - new */
375 {0x07, 0x8602}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300376 {0x00, 0x8610}, /* *red */
377 {0x00, 0x8611}, /* 3f *green */
378 {0x00, 0x8612}, /* green *blue */
379 {0x00, 0x8613}, /* blue *green */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300380 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
381 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
382 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
383 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300384
385 {0x0c, 0x8620}, /* 0c */
386 {0xc8, 0x8631}, /* c8 */
387 {0xc8, 0x8634}, /* c8 */
388 {0x23, 0x8635}, /* 23 */
389 {0x1f, 0x8636}, /* 1f */
390 {0xdd, 0x8637}, /* dd */
391 {0xe1, 0x8638}, /* e1 */
392 {0x1d, 0x8639}, /* 1d */
393 {0x21, 0x863a}, /* 21 */
394 {0xe3, 0x863b}, /* e3 */
395 {0xdf, 0x863c}, /* df */
396 {0xf0, 0x8505},
397 {0x32, 0x850a},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300398/* {0x99, 0x8700}, * - white balance - new (removed) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300399 {}
400};
401
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300402static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300403{
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300404 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300405
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300406 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
407 0, /* request */
408 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
409 value, index, NULL, 0, 500);
410 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
411 if (ret < 0)
412 PDEBUG(D_ERR, "reg write: error %d", ret);
413}
414
415static void write_vector(struct gspca_dev *gspca_dev,
416 const __u16 data[][2])
417{
418 struct usb_device *dev = gspca_dev->dev;
419 int i;
420
421 i = 0;
422 while (data[i][1] != 0) {
423 reg_w_val(dev, data[i][1], data[i][0]);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300424 i++;
425 }
426}
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300427
428/* read 'len' bytes to gspca_dev->usb_buf */
429static void reg_r(struct gspca_dev *gspca_dev,
430 __u16 index, __u16 length)
431{
432 usb_control_msg(gspca_dev->dev,
433 usb_rcvctrlpipe(gspca_dev->dev, 0),
434 0, /* request */
435 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
436 0, /* value */
437 index, gspca_dev->usb_buf, length, 500);
438}
439
440/* write 'len' bytes from gspca_dev->usb_buf */
441static void reg_w_buf(struct gspca_dev *gspca_dev,
442 __u16 index, __u16 len)
443{
444 usb_control_msg(gspca_dev->dev,
445 usb_sndctrlpipe(gspca_dev->dev, 0),
446 0, /* request */
447 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
448 0, /* value */
449 index, gspca_dev->usb_buf, len, 500);
450}
451
452static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
453{
454 int retry = 60;
455
456 reg_w_val(gspca_dev->dev, 0x8801, reg);
457 reg_w_val(gspca_dev->dev, 0x8805, value);
458 reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
459 do {
460 reg_r(gspca_dev, 0x8803, 1);
461 if (!gspca_dev->usb_buf[0])
462 return;
463 } while (--retry);
464}
465
466static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
467{
468 int retry = 60;
469 __u8 value;
470
471 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
472 reg_w_val(gspca_dev->dev, 0x8801, reg);
473 reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
474 do {
475 reg_r(gspca_dev, 0x8803, 1);
476 if (!gspca_dev->usb_buf[0]) {
477 reg_r(gspca_dev, 0x8800, 1);
478 value = gspca_dev->usb_buf[0];
479 reg_r(gspca_dev, 0x8805, 1);
480 return ((int) value << 8) | gspca_dev->usb_buf[0];
481 }
482 } while (--retry);
483 return -1;
484}
485
486static void sensor_mapwrite(struct gspca_dev *gspca_dev,
487 const __u16 (*sensormap)[2])
488{
489 while ((*sensormap)[0]) {
490 gspca_dev->usb_buf[0] = (*sensormap)[1];
491 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
492 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
493 sensormap++;
494 }
495}
496
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300497static void write_sensor_72a(struct gspca_dev *gspca_dev,
498 const __u16 (*sensor)[2])
499{
500 while ((*sensor)[0]) {
501 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
502 sensor++;
503 }
504}
505
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300506static void init_161rev12A(struct gspca_dev *gspca_dev)
507{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300508 write_vector(gspca_dev, spca561_161rev12A_data1);
509 sensor_mapwrite(gspca_dev, Pb100_1map8300);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300510/*fixme: should be in sd_start*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300511 write_vector(gspca_dev, spca561_161rev12A_data2);
512 sensor_mapwrite(gspca_dev, Pb100_2map8300);
513}
514
515/* this function is called at probe time */
516static int sd_config(struct gspca_dev *gspca_dev,
517 const struct usb_device_id *id)
518{
519 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300520 struct cam *cam;
521 __u16 vendor, product;
522 __u8 data1, data2;
523
524 /* Read frm global register the USB product and vendor IDs, just to
525 * prove that we can communicate with the device. This works, which
526 * confirms at we are communicating properly and that the device
527 * is a 561. */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300528 reg_r(gspca_dev, 0x8104, 1);
529 data1 = gspca_dev->usb_buf[0];
530 reg_r(gspca_dev, 0x8105, 1);
531 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300532 vendor = (data2 << 8) | data1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300533 reg_r(gspca_dev, 0x8106, 1);
534 data1 = gspca_dev->usb_buf[0];
535 reg_r(gspca_dev, 0x8107, 1);
536 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300537 product = (data2 << 8) | data1;
538 if (vendor != id->idVendor || product != id->idProduct) {
539 PDEBUG(D_PROBE, "Bad vendor / product from device");
540 return -EINVAL;
541 }
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300542
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300543 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300544 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300545
546 sd->chip_revision = id->driver_info;
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300547 if (sd->chip_revision == Rev012A) {
548 cam->cam_mode = sif_012a_mode;
549 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
550 } else {
551 cam->cam_mode = sif_072a_mode;
552 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
553 }
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300554 sd->brightness = BRIGHTNESS_DEF;
555 sd->contrast = CONTRAST_DEF;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300556 sd->white = WHITE_DEF;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300557 sd->exposure = EXPOSURE_DEF;
558 sd->autogain = AUTOGAIN_DEF;
559 sd->gain = GAIN_DEF;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300560 sd->expo12a = EXPO12A_DEF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300561 return 0;
562}
563
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300564/* this function is called at probe and resume time */
565static int sd_init_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300566{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300567 PDEBUG(D_STREAM, "Chip revision: 012a");
568 init_161rev12A(gspca_dev);
569 return 0;
570}
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300571static int sd_init_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300572{
573 PDEBUG(D_STREAM, "Chip revision: 072a");
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300574 write_vector(gspca_dev, rev72a_init_data1);
575 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
576 write_vector(gspca_dev, rev72a_init_data2);
577 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
578 write_vector(gspca_dev, rev72a_init_data3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300579 return 0;
580}
581
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300582/* rev 72a only */
583static void setbrightness(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300584{
585 struct sd *sd = (struct sd *) gspca_dev;
586 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300587 __u8 value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300588
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300589 value = sd->brightness;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300590
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300591 /* offsets for white balance */
592 reg_w_val(dev, 0x8611, value); /* R */
593 reg_w_val(dev, 0x8612, value); /* Gr */
594 reg_w_val(dev, 0x8613, value); /* B */
595 reg_w_val(dev, 0x8614, value); /* Gb */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300596}
597
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300598static void setwhite(struct gspca_dev *gspca_dev)
599{
600 struct sd *sd = (struct sd *) gspca_dev;
601 __u16 white;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300602 __u8 blue, red;
603 __u16 reg;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300604
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300605 /* try to emulate MS-win as possible */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300606 white = sd->white;
607 red = 0x20 + white * 3 / 8;
608 blue = 0x90 - white * 5 / 8;
609 if (sd->chip_revision == Rev012A) {
610 reg = 0x8614;
611 } else {
612 reg = 0x8651;
613 red += sd->contrast - 0x20;
614 blue += sd->contrast - 0x20;
615 }
616 reg_w_val(gspca_dev->dev, reg, red);
617 reg_w_val(gspca_dev->dev, reg + 2, blue);
618}
619
620static void setcontrast(struct gspca_dev *gspca_dev)
621{
622 struct sd *sd = (struct sd *) gspca_dev;
623 struct usb_device *dev = gspca_dev->dev;
624 __u8 value;
625
626 if (sd->chip_revision != Rev072A)
627 return;
628 value = sd->contrast + 0x20;
629
630 /* gains for white balance */
631 setwhite(gspca_dev);
632/* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
633 reg_w_val(dev, 0x8652, value); /* Gr */
634/* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
635 reg_w_val(dev, 0x8654, value); /* Gb */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300636}
637
638/* rev 12a only */
639static void setexposure(struct gspca_dev *gspca_dev)
640{
641 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300642 int expo;
Hans de Goede0fc23d22008-09-04 16:22:57 -0300643 int clock_divider;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300644
Hans de Goede0fc23d22008-09-04 16:22:57 -0300645 /* Register 0x8309 controls exposure for the spca561,
646 the basic exposure setting goes from 1-2047, where 1 is completely
647 dark and 2047 is very bright. It not only influences exposure but
648 also the framerate (to allow for longer exposure) from 1 - 300 it
649 only raises the exposure time then from 300 - 600 it halves the
650 framerate to be able to further raise the exposure time and for every
651 300 more it halves the framerate again. This allows for a maximum
652 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
653 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
654 configure a divider for the base framerate which us used at the
655 exposure setting of 1-300. These bits configure the base framerate
656 according to the following formula: fps = 60 / (value + 2) */
657 if (sd->exposure < 2048) {
658 expo = sd->exposure;
659 clock_divider = 0;
660 } else {
661 /* Add 900 to make the 0 setting of the second part of the
662 exposure equal to the 2047 setting of the first part. */
663 expo = (sd->exposure - 2048) + 900;
664 clock_divider = 3;
665 }
666 expo |= clock_divider << 11;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300667 gspca_dev->usb_buf[0] = expo;
668 gspca_dev->usb_buf[1] = expo >> 8;
669 reg_w_buf(gspca_dev, 0x8309, 2);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300670}
671
672/* rev 12a only */
673static void setgain(struct gspca_dev *gspca_dev)
674{
675 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300676
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300677 gspca_dev->usb_buf[0] = sd->gain;
678 gspca_dev->usb_buf[1] = 0;
679 reg_w_buf(gspca_dev, 0x8335, 2);
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300680}
681
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300682static void setautogain(struct gspca_dev *gspca_dev)
683{
684 struct sd *sd = (struct sd *) gspca_dev;
685
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300686 if (sd->autogain)
687 sd->ag_cnt = AG_CNT_START;
688 else
689 sd->ag_cnt = -1;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300690}
691
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300692static int sd_start_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300693{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300694 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300695 int mode;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300696 static const __u8 Reg8391[8] =
697 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300698
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300699 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300700 if (mode <= 1) {
701 /* Use compression on 320x240 and above */
702 reg_w_val(dev, 0x8500, 0x10 | mode);
703 } else {
704 /* I couldn't get the compression to work below 320x240
705 * Fortunately at these resolutions the bandwidth
706 * is sufficient to push raw frames at ~20fps */
707 reg_w_val(dev, 0x8500, mode);
708 } /* -- qq@kuku.eu.org */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300709
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300710 gspca_dev->usb_buf[0] = 0xaa;
711 gspca_dev->usb_buf[1] = 0x00;
712 reg_w_buf(gspca_dev, 0x8307, 2);
713 /* clock - lower 0x8X values lead to fps > 30 */
714 reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300715 /* 0x8f 0x85 0x27 clock */
716 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
717 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300718 memcpy(gspca_dev->usb_buf, Reg8391, 8);
719 reg_w_buf(gspca_dev, 0x8391, 8);
720 reg_w_buf(gspca_dev, 0x8390, 8);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300721 setwhite(gspca_dev);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300722 setautogain(gspca_dev);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300723/* setgain(gspca_dev); */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300724 setexposure(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300725 return 0;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300726}
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300727static int sd_start_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300728{
729 struct usb_device *dev = gspca_dev->dev;
730 int Clck;
731 int mode;
732
733 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
734 switch (mode) {
735 default:
736/* case 0:
737 case 1: */
738 Clck = 0x25;
739 break;
740 case 2:
741 Clck = 0x22;
742 break;
743 case 3:
744 Clck = 0x21;
745 break;
746 }
747 reg_w_val(dev, 0x8500, mode); /* mode */
748 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
749 reg_w_val(dev, 0x8112, 0x10 | 0x20);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300750 setcontrast(gspca_dev);
751/* setbrightness(gspca_dev); * fixme: bad values */
752 setwhite(gspca_dev);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300753 setautogain(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300754 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300755}
756
757static void sd_stopN(struct gspca_dev *gspca_dev)
758{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300759 struct sd *sd = (struct sd *) gspca_dev;
760
761 if (sd->chip_revision == Rev012A) {
762 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
763 } else {
764 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
765/* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
766 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300767}
768
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300769/* called on streamoff with alt 0 and on disconnect */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300770static void sd_stop0(struct gspca_dev *gspca_dev)
771{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300772 struct sd *sd = (struct sd *) gspca_dev;
773
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300774 if (!gspca_dev->present)
775 return;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300776 if (sd->chip_revision == Rev012A) {
777 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
778 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
779 }
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300780/* reg_w_val(gspca_dev->dev, 0x8114, 0); */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300781}
782
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300783static void do_autogain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300784{
785 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300786 int expotimes;
787 int pixelclk;
788 int gainG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300789 __u8 R, Gr, Gb, B;
790 int y;
791 __u8 luma_mean = 110;
792 __u8 luma_delta = 20;
793 __u8 spring = 4;
794
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300795 if (sd->ag_cnt < 0)
796 return;
797 if (--sd->ag_cnt >= 0)
798 return;
799 sd->ag_cnt = AG_CNT_START;
800
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300801 switch (sd->chip_revision) {
802 case Rev072A:
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300803 reg_r(gspca_dev, 0x8621, 1);
804 Gr = gspca_dev->usb_buf[0];
805 reg_r(gspca_dev, 0x8622, 1);
806 R = gspca_dev->usb_buf[0];
807 reg_r(gspca_dev, 0x8623, 1);
808 B = gspca_dev->usb_buf[0];
809 reg_r(gspca_dev, 0x8624, 1);
810 Gb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300811 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
812 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
813 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
814 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
815
816 if (y < luma_mean - luma_delta ||
817 y > luma_mean + luma_delta) {
818 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
819 pixelclk = 0x0800;
820 expotimes = expotimes & 0x07ff;
821 /* PDEBUG(D_PACK,
822 "Exposition Times 0x%03X Clock 0x%04X ",
823 expotimes,pixelclk); */
824 gainG = i2c_read(gspca_dev, 0x35, 0x10);
825 /* PDEBUG(D_PACK,
826 "reading Gain register %d", gainG); */
827
828 expotimes += (luma_mean - y) >> spring;
829 gainG += (luma_mean - y) / 50;
830 /* PDEBUG(D_PACK,
831 "compute expotimes %d gain %d",
832 expotimes,gainG); */
833
834 if (gainG > 0x3f)
835 gainG = 0x3f;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300836 else if (gainG < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300837 gainG = 3;
838 i2c_write(gspca_dev, gainG, 0x35);
839
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300840 if (expotimes > 0x0256)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300841 expotimes = 0x0256;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300842 else if (expotimes < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300843 expotimes = 3;
844 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
845 }
846 break;
847 case Rev012A:
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300848 reg_r(gspca_dev, 0x8330, 2);
849 if (gspca_dev->usb_buf[1] > 0x08) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300850 gspca_dev->usb_buf[0] = ++sd->expo12a;
851 gspca_dev->usb_buf[1] = 0;
852 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300853 } else if (gspca_dev->usb_buf[1] < 0x02) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300854 gspca_dev->usb_buf[0] = --sd->expo12a;
855 gspca_dev->usb_buf[1] = 0;
856 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300857 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300858 break;
859 }
860}
861
862static void sd_pkt_scan(struct gspca_dev *gspca_dev,
863 struct gspca_frame *frame, /* target */
864 __u8 *data, /* isoc packet */
865 int len) /* iso packet length */
866{
Hans de Goede0fc23d22008-09-04 16:22:57 -0300867 struct sd *sd = (struct sd *) gspca_dev;
868
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300869 switch (data[0]) { /* sequence number */
870 case 0: /* start of frame */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300871 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
872 data, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300873 data += SPCA561_OFFSET_DATA;
874 len -= SPCA561_OFFSET_DATA;
875 if (data[1] & 0x10) {
876 /* compressed bayer */
877 gspca_frame_add(gspca_dev, FIRST_PACKET,
878 frame, data, len);
879 } else {
Hans de Goede54ab92c2008-07-03 11:20:58 -0300880 /* raw bayer (with a header, which we skip) */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300881 if (sd->chip_revision == Rev012A) {
882 data += 20;
883 len -= 20;
884 } else {
885 data += 16;
886 len -= 16;
887 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300888 gspca_frame_add(gspca_dev, FIRST_PACKET,
889 frame, data, len);
890 }
891 return;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300892 case 0xff: /* drop (empty mpackets) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300893 return;
894 }
895 data++;
896 len--;
897 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
898}
899
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300900/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300901static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
902{
903 struct sd *sd = (struct sd *) gspca_dev;
904
905 sd->brightness = val;
906 if (gspca_dev->streaming)
907 setbrightness(gspca_dev);
908 return 0;
909}
910
911static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
912{
913 struct sd *sd = (struct sd *) gspca_dev;
914
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300915 *val = sd->brightness;
916 return 0;
917}
918
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300919/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300920static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
921{
922 struct sd *sd = (struct sd *) gspca_dev;
923
924 sd->contrast = val;
925 if (gspca_dev->streaming)
926 setcontrast(gspca_dev);
927 return 0;
928}
929
930static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
931{
932 struct sd *sd = (struct sd *) gspca_dev;
933
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300934 *val = sd->contrast;
935 return 0;
936}
937
938static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
939{
940 struct sd *sd = (struct sd *) gspca_dev;
941
942 sd->autogain = val;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300943 if (gspca_dev->streaming)
944 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300945 return 0;
946}
947
948static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
949{
950 struct sd *sd = (struct sd *) gspca_dev;
951
952 *val = sd->autogain;
953 return 0;
954}
955
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300956static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
957{
958 struct sd *sd = (struct sd *) gspca_dev;
959
960 sd->white = val;
961 if (gspca_dev->streaming)
962 setwhite(gspca_dev);
963 return 0;
964}
965
966static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
967{
968 struct sd *sd = (struct sd *) gspca_dev;
969
970 *val = sd->white;
971 return 0;
972}
973
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300974/* rev12a only */
975static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
976{
977 struct sd *sd = (struct sd *) gspca_dev;
978
979 sd->exposure = val;
980 if (gspca_dev->streaming)
981 setexposure(gspca_dev);
982 return 0;
983}
984
985static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
986{
987 struct sd *sd = (struct sd *) gspca_dev;
988
989 *val = sd->exposure;
990 return 0;
991}
992
993/* rev12a only */
994static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
995{
996 struct sd *sd = (struct sd *) gspca_dev;
997
998 sd->gain = val;
999 if (gspca_dev->streaming)
1000 setgain(gspca_dev);
1001 return 0;
1002}
1003
1004static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1005{
1006 struct sd *sd = (struct sd *) gspca_dev;
1007
1008 *val = sd->gain;
1009 return 0;
1010}
1011
1012/* control tables */
1013static struct ctrl sd_ctrls_12a[] = {
1014 {
1015 {
1016 .id = V4L2_CID_DO_WHITE_BALANCE,
1017 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab695ebd12008-10-04 14:18:37 -03001018 .name = "White Balance",
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001019 .minimum = WHITE_MIN,
1020 .maximum = WHITE_MAX,
1021 .step = 1,
1022 .default_value = WHITE_DEF,
1023 },
1024 .set = sd_setwhite,
1025 .get = sd_getwhite,
1026 },
1027 {
1028 {
1029 .id = V4L2_CID_EXPOSURE,
1030 .type = V4L2_CTRL_TYPE_INTEGER,
1031 .name = "Exposure",
1032 .minimum = EXPOSURE_MIN,
1033 .maximum = EXPOSURE_MAX,
1034 .step = 1,
1035 .default_value = EXPOSURE_DEF,
1036 },
1037 .set = sd_setexposure,
1038 .get = sd_getexposure,
1039 },
1040 {
1041 {
1042 .id = V4L2_CID_AUTOGAIN,
1043 .type = V4L2_CTRL_TYPE_BOOLEAN,
1044 .name = "Auto Gain",
1045 .minimum = AUTOGAIN_MIN,
1046 .maximum = AUTOGAIN_MAX,
1047 .step = 1,
1048 .default_value = AUTOGAIN_DEF,
1049 },
1050 .set = sd_setautogain,
1051 .get = sd_getautogain,
1052 },
1053 {
1054 {
1055 .id = V4L2_CID_GAIN,
1056 .type = V4L2_CTRL_TYPE_INTEGER,
1057 .name = "Gain",
1058 .minimum = GAIN_MIN,
1059 .maximum = GAIN_MAX,
1060 .step = 1,
1061 .default_value = GAIN_DEF,
1062 },
1063 .set = sd_setgain,
1064 .get = sd_getgain,
1065 },
1066};
1067
1068static struct ctrl sd_ctrls_72a[] = {
1069 {
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -03001070 {
1071 .id = V4L2_CID_DO_WHITE_BALANCE,
1072 .type = V4L2_CTRL_TYPE_INTEGER,
1073 .name = "White Balance",
1074 .minimum = WHITE_MIN,
1075 .maximum = WHITE_MAX,
1076 .step = 1,
1077 .default_value = WHITE_DEF,
1078 },
1079 .set = sd_setwhite,
1080 .get = sd_getwhite,
1081 },
1082 {
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001083 {
1084 .id = V4L2_CID_BRIGHTNESS,
1085 .type = V4L2_CTRL_TYPE_INTEGER,
1086 .name = "Brightness",
1087 .minimum = BRIGHTNESS_MIN,
1088 .maximum = BRIGHTNESS_MAX,
1089 .step = 1,
1090 .default_value = BRIGHTNESS_DEF,
1091 },
1092 .set = sd_setbrightness,
1093 .get = sd_getbrightness,
1094 },
1095 {
1096 {
1097 .id = V4L2_CID_CONTRAST,
1098 .type = V4L2_CTRL_TYPE_INTEGER,
1099 .name = "Contrast",
1100 .minimum = CONTRAST_MIN,
1101 .maximum = CONTRAST_MAX,
1102 .step = 1,
1103 .default_value = CONTRAST_DEF,
1104 },
1105 .set = sd_setcontrast,
1106 .get = sd_getcontrast,
1107 },
1108 {
1109 {
1110 .id = V4L2_CID_AUTOGAIN,
1111 .type = V4L2_CTRL_TYPE_BOOLEAN,
1112 .name = "Auto Gain",
1113 .minimum = AUTOGAIN_MIN,
1114 .maximum = AUTOGAIN_MAX,
1115 .step = 1,
1116 .default_value = AUTOGAIN_DEF,
1117 },
1118 .set = sd_setautogain,
1119 .get = sd_getautogain,
1120 },
1121};
1122
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001123/* sub-driver description */
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001124static const struct sd_desc sd_desc_12a = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001125 .name = MODULE_NAME,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001126 .ctrls = sd_ctrls_12a,
1127 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001128 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001129 .init = sd_init_12a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001130 .start = sd_start_12a,
1131 .stopN = sd_stopN,
1132 .stop0 = sd_stop0,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001133 .pkt_scan = sd_pkt_scan,
1134/* .dq_callback = do_autogain, * fixme */
1135};
1136static const struct sd_desc sd_desc_72a = {
1137 .name = MODULE_NAME,
1138 .ctrls = sd_ctrls_72a,
1139 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1140 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001141 .init = sd_init_72a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001142 .start = sd_start_72a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001143 .stopN = sd_stopN,
1144 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001145 .pkt_scan = sd_pkt_scan,
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -03001146 .dq_callback = do_autogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001147};
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001148static const struct sd_desc *sd_desc[2] = {
1149 &sd_desc_12a,
1150 &sd_desc_72a
1151};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001152
1153/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001154static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine87581aa2008-07-26 14:30:01 -03001155 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1156 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1157 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1158 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1159 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1160 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1161 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1162 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1163 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1164 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1165 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1166 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1167 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1168 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1169 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001170 {}
1171};
1172
1173MODULE_DEVICE_TABLE(usb, device_table);
1174
1175/* -- device connect -- */
1176static int sd_probe(struct usb_interface *intf,
1177 const struct usb_device_id *id)
1178{
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001179 return gspca_dev_probe(intf, id,
1180 sd_desc[id->driver_info],
1181 sizeof(struct sd),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001182 THIS_MODULE);
1183}
1184
1185static struct usb_driver sd_driver = {
1186 .name = MODULE_NAME,
1187 .id_table = device_table,
1188 .probe = sd_probe,
1189 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001190#ifdef CONFIG_PM
1191 .suspend = gspca_suspend,
1192 .resume = gspca_resume,
1193#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001194};
1195
1196/* -- module insert / remove -- */
1197static int __init sd_mod_init(void)
1198{
Alexey Klimovf69e9522009-01-01 13:02:07 -03001199 int ret;
1200 ret = usb_register(&sd_driver);
1201 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -03001202 return ret;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001203 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001204 return 0;
1205}
1206static void __exit sd_mod_exit(void)
1207{
1208 usb_deregister(&sd_driver);
1209 PDEBUG(D_PROBE, "deregistered");
1210}
1211
1212module_init(sd_mod_init);
1213module_exit(sd_mod_exit);