blob: d2f689e366af78da7c48e8fb803c8c1d26d6a797 [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
2 * Sunplus spca561 subdriver
3 *
4 * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
5 *
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#define MODULE_NAME "spca561"
24
25#include "gspca.h"
26
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030027MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
29MODULE_LICENSE("GPL");
30
31/* specific webcam descriptor */
32struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */
34
Jean-Francois Moine7879d452008-09-03 16:47:32 -030035 __u16 contrast; /* rev72a only */
36#define CONTRAST_MIN 0x0000
37#define CONTRAST_DEF 0x2000
38#define CONTRAST_MAX 0x3fff
39
40 __u16 exposure; /* rev12a only */
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -030041#define EXPOSURE_MIN 0
42#define EXPOSURE_DEF 200
43#define EXPOSURE_MAX 762
Jean-Francois Moine7879d452008-09-03 16:47:32 -030044
45 __u8 brightness; /* rev72a only */
46#define BRIGHTNESS_MIN 0
47#define BRIGHTNESS_DEF 32
48#define BRIGHTNESS_MAX 63
49
50 __u8 white; /* rev12a only */
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -030051#define WHITE_MIN 1
Jean-Francois Moine7879d452008-09-03 16:47:32 -030052#define WHITE_DEF 0x40
53#define WHITE_MAX 0x7f
54
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030055 __u8 autogain;
Jean-Francois Moine7879d452008-09-03 16:47:32 -030056#define AUTOGAIN_MIN 0
57#define AUTOGAIN_DEF 1
58#define AUTOGAIN_MAX 1
59
60 __u8 gain; /* rev12a only */
61#define GAIN_MIN 0x0
62#define GAIN_DEF 0x24
63#define GAIN_MAX 0x24
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030064
Jean-Francois Moined698dc62008-09-03 16:47:51 -030065#define EXPO12A_DEF 3
66 __u8 expo12a; /* expo/gain? for rev 12a */
67
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030068 __u8 chip_revision;
Jean-Francois Moine7879d452008-09-03 16:47:32 -030069#define Rev012A 0
70#define Rev072A 1
71
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030072 signed char ag_cnt;
73#define AG_CNT_START 13
74};
75
Jean-Francois Moineb77c0042008-09-04 16:22:56 -030076static struct v4l2_pix_format sif_012a_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -030077 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
78 .bytesperline = 160,
79 .sizeimage = 160 * 120,
80 .colorspace = V4L2_COLORSPACE_SRGB,
81 .priv = 3},
82 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
83 .bytesperline = 176,
84 .sizeimage = 176 * 144,
85 .colorspace = V4L2_COLORSPACE_SRGB,
86 .priv = 2},
87 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
88 .bytesperline = 320,
89 .sizeimage = 320 * 240 * 4 / 8,
90 .colorspace = V4L2_COLORSPACE_SRGB,
91 .priv = 1},
92 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
93 .bytesperline = 352,
94 .sizeimage = 352 * 288 * 4 / 8,
95 .colorspace = V4L2_COLORSPACE_SRGB,
96 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030097};
98
Jean-Francois Moineb77c0042008-09-04 16:22:56 -030099static struct v4l2_pix_format sif_072a_mode[] = {
100 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
101 .bytesperline = 160,
102 .sizeimage = 160 * 120,
103 .colorspace = V4L2_COLORSPACE_SRGB,
104 .priv = 3},
105 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
106 .bytesperline = 176,
107 .sizeimage = 176 * 144,
108 .colorspace = V4L2_COLORSPACE_SRGB,
109 .priv = 2},
110 {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
111 .bytesperline = 320,
112 .sizeimage = 320 * 240,
113 .colorspace = V4L2_COLORSPACE_SRGB,
114 .priv = 1},
115 {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
116 .bytesperline = 352,
117 .sizeimage = 352 * 288,
118 .colorspace = V4L2_COLORSPACE_SRGB,
119 .priv = 0},
120};
121
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300122/*
123 * Initialization data
124 * I'm not very sure how to split initialization from open data
125 * chunks. For now, we'll consider everything as initialization
126 */
127/* Frame packet header offsets for the spca561 */
128#define SPCA561_OFFSET_SNAP 1
129#define SPCA561_OFFSET_TYPE 2
130#define SPCA561_OFFSET_COMPRESS 3
131#define SPCA561_OFFSET_FRAMSEQ 4
132#define SPCA561_OFFSET_GPIO 5
133#define SPCA561_OFFSET_USBBUFF 6
134#define SPCA561_OFFSET_WIN2GRAVE 7
135#define SPCA561_OFFSET_WIN2RAVE 8
136#define SPCA561_OFFSET_WIN2BAVE 9
137#define SPCA561_OFFSET_WIN2GBAVE 10
138#define SPCA561_OFFSET_WIN1GRAVE 11
139#define SPCA561_OFFSET_WIN1RAVE 12
140#define SPCA561_OFFSET_WIN1BAVE 13
141#define SPCA561_OFFSET_WIN1GBAVE 14
142#define SPCA561_OFFSET_FREQ 15
143#define SPCA561_OFFSET_VSYNC 16
144#define SPCA561_OFFSET_DATA 1
145#define SPCA561_INDEX_I2C_BASE 0x8800
146#define SPCA561_SNAPBIT 0x20
147#define SPCA561_SNAPCTRL 0x40
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300148
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300149static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300150{
151 int ret;
152
153 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
154 0, /* request */
155 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
156 value, index, NULL, 0, 500);
157 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
158 if (ret < 0)
159 PDEBUG(D_ERR, "reg write: error %d", ret);
160}
161
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300162static void write_vector(struct gspca_dev *gspca_dev,
163 const __u16 data[][2])
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300164{
165 struct usb_device *dev = gspca_dev->dev;
166 int i;
167
168 i = 0;
169 while (data[i][1] != 0) {
170 reg_w_val(dev, data[i][1], data[i][0]);
171 i++;
172 }
173}
174
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300175/* read 'len' bytes to gspca_dev->usb_buf */
176static void reg_r(struct gspca_dev *gspca_dev,
177 __u16 index, __u16 length)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300178{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300179 usb_control_msg(gspca_dev->dev,
180 usb_rcvctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300181 0, /* request */
182 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
183 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300184 index, gspca_dev->usb_buf, length, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300185}
186
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300187static void reg_w_buf(struct gspca_dev *gspca_dev,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300188 __u16 index, const __u8 *buffer, __u16 len)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300189{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300190 memcpy(gspca_dev->usb_buf, buffer, len);
191 usb_control_msg(gspca_dev->dev,
192 usb_sndctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300193 0, /* request */
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
195 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300196 index, gspca_dev->usb_buf, len, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300197}
198
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300199static void i2c_write(struct gspca_dev *gspca_dev, __u16 valeur, __u16 reg)
200{
201 int retry = 60;
202 __u8 DataLow;
203 __u8 DataHight;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300204
205 DataLow = valeur;
206 DataHight = valeur >> 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300207 reg_w_val(gspca_dev->dev, 0x8801, reg);
208 reg_w_val(gspca_dev->dev, 0x8805, DataLow);
209 reg_w_val(gspca_dev->dev, 0x8800, DataHight);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300210 while (retry--) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300211 reg_r(gspca_dev, 0x8803, 1);
212 if (!gspca_dev->usb_buf[0])
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300213 break;
214 }
215}
216
217static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
218{
219 int retry = 60;
220 __u8 value;
221 __u8 vallsb;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300222
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300223 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
224 reg_w_val(gspca_dev->dev, 0x8801, reg);
225 reg_w_val(gspca_dev->dev, 0x8802, (mode | 0x01));
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300226 while (retry--) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300227 reg_r(gspca_dev, 0x8803, 1);
228 if (!gspca_dev->usb_buf)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300229 break;
230 }
231 if (retry == 0)
232 return -1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300233 reg_r(gspca_dev, 0x8800, 1);
234 value = gspca_dev->usb_buf[0];
235 reg_r(gspca_dev, 0x8805, 1);
236 vallsb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300237 return ((int) value << 8) | vallsb;
238}
239
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300240static const __u16 spca561_init_data[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300241 {0x0000, 0x8114}, /* Software GPIO output data */
242 {0x0001, 0x8114}, /* Software GPIO output data */
243 {0x0000, 0x8112}, /* Some kind of reset */
244 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
245 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
246 {0x0011, 0x8118}, /* Enable and conf sensor */
247 {0x0001, 0x8118}, /* Conf sensor */
248 {0x0092, 0x8804}, /* I know nothing about these */
249 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
250 /***************/
251 {0x000d, 0x8805}, /* sensor default setting */
252 {0x0001, 0x8801}, /* 1 <- 0x0d */
253 {0x0000, 0x8800},
254 {0x0018, 0x8805},
255 {0x0002, 0x8801}, /* 2 <- 0x18 */
256 {0x0000, 0x8800},
257 {0x0065, 0x8805},
258 {0x0004, 0x8801}, /* 4 <- 0x01 0x65 */
259 {0x0001, 0x8800},
260 {0x0021, 0x8805},
261 {0x0005, 0x8801}, /* 5 <- 0x21 */
262 {0x0000, 0x8800},
263 {0x00aa, 0x8805},
264 {0x0007, 0x8801}, /* 7 <- 0xaa */
265 {0x0000, 0x8800},
266 {0x0004, 0x8805},
267 {0x0020, 0x8801}, /* 0x20 <- 0x15 0x04 */
268 {0x0015, 0x8800},
269 {0x0002, 0x8805},
270 {0x0039, 0x8801}, /* 0x39 <- 0x02 */
271 {0x0000, 0x8800},
272 {0x0010, 0x8805},
273 {0x0035, 0x8801}, /* 0x35 <- 0x10 */
274 {0x0000, 0x8800},
275 {0x0049, 0x8805},
276 {0x0009, 0x8801}, /* 0x09 <- 0x10 0x49 */
277 {0x0010, 0x8800},
278 {0x000b, 0x8805},
279 {0x0028, 0x8801}, /* 0x28 <- 0x0b */
280 {0x0000, 0x8800},
281 {0x000f, 0x8805},
282 {0x003b, 0x8801}, /* 0x3b <- 0x0f */
283 {0x0000, 0x8800},
284 {0x0000, 0x8805},
285 {0x003c, 0x8801}, /* 0x3c <- 0x00 */
286 {0x0000, 0x8800},
287 /***************/
288 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
289 {0x0000, 0x8602}, /* Optical black level for user setting */
290 {0x0060, 0x8604}, /* Optical black horizontal offset */
291 {0x0002, 0x8605}, /* Optical black vertical offset */
292 {0x0000, 0x8603}, /* Non-automatic optical black level */
293 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
294 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
295 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
296 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
297 {0x00e0, 0x8406}, /* Memory buffer threshold */
298 {0x0000, 0x8660}, /* Compensation memory stuff */
299 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
300 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
301 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
302 {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
303 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
304 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
305 {0x0010, 0x8660}, /* Compensation memory stuff */
306 {0x0018, 0x8660}, /* Compensation memory stuff */
307
308 {0x0004, 0x8611}, /* R offset for white balance */
309 {0x0004, 0x8612}, /* Gr offset for white balance */
310 {0x0007, 0x8613}, /* B offset for white balance */
311 {0x0000, 0x8614}, /* Gb offset for white balance */
312 {0x008c, 0x8651}, /* R gain for white balance */
313 {0x008c, 0x8652}, /* Gr gain for white balance */
314 {0x00b5, 0x8653}, /* B gain for white balance */
315 {0x008c, 0x8654}, /* Gb gain for white balance */
316 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
317
318 {0x0011, 0x8802},
319 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
320 {0x0081, 0x8702}, /* Master clock output enable */
321
322 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
323 /* Originally was 0x0010 (352x288 compression) */
324
325 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
326 {0x0003, 0x865c}, /* Vertical offset for valid lines */
327 /***************//* sensor active */
328 {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
329 {0x0021, 0x8805},
330 {0x0001, 0x8800},
331 {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
332 {0x0065, 0x8805},
333 {0x0001, 0x8800},
334 {0x0005, 0x8801}, /* 0x05 <- 0x2f */
335 {0x002f, 0x8805},
336 {0x0000, 0x8800},
337 {0x0006, 0x8801}, /* 0x06 <- 0 */
338 {0x0000, 0x8805},
339 {0x0000, 0x8800},
340 {0x000a, 0x8801}, /* 0x0a <- 2 */
341 {0x0002, 0x8805},
342 {0x0000, 0x8800},
343 {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
344 {0x0061, 0x8805},
345 {0x0010, 0x8800},
346 {0x0035, 0x8801}, /* 0x35 <-0x14 */
347 {0x0014, 0x8805},
348 {0x0000, 0x8800},
349 {0x0030, 0x8112}, /* ISO and drop packet enable */
350 {0x0000, 0x8112}, /* Some kind of reset ???? */
351 {0x0009, 0x8118}, /* Enable sensor and set standby */
352 {0x0000, 0x8114}, /* Software GPIO output data */
353 {0x0000, 0x8114}, /* Software GPIO output data */
354 {0x0001, 0x8114}, /* Software GPIO output data */
355 {0x0000, 0x8112}, /* Some kind of reset ??? */
356 {0x0003, 0x8701},
357 {0x0001, 0x8703},
358 {0x0011, 0x8118},
359 {0x0001, 0x8118},
360 /***************/
361 {0x0092, 0x8804},
362 {0x0010, 0x8802},
363 {0x000d, 0x8805},
364 {0x0001, 0x8801},
365 {0x0000, 0x8800},
366 {0x0018, 0x8805},
367 {0x0002, 0x8801},
368 {0x0000, 0x8800},
369 {0x0065, 0x8805},
370 {0x0004, 0x8801},
371 {0x0001, 0x8800},
372 {0x0021, 0x8805},
373 {0x0005, 0x8801},
374 {0x0000, 0x8800},
375 {0x00aa, 0x8805},
376 {0x0007, 0x8801}, /* mode 0xaa */
377 {0x0000, 0x8800},
378 {0x0004, 0x8805},
379 {0x0020, 0x8801},
380 {0x0015, 0x8800}, /* mode 0x0415 */
381 {0x0002, 0x8805},
382 {0x0039, 0x8801},
383 {0x0000, 0x8800},
384 {0x0010, 0x8805},
385 {0x0035, 0x8801},
386 {0x0000, 0x8800},
387 {0x0049, 0x8805},
388 {0x0009, 0x8801},
389 {0x0010, 0x8800},
390 {0x000b, 0x8805},
391 {0x0028, 0x8801},
392 {0x0000, 0x8800},
393 {0x000f, 0x8805},
394 {0x003b, 0x8801},
395 {0x0000, 0x8800},
396 {0x0000, 0x8805},
397 {0x003c, 0x8801},
398 {0x0000, 0x8800},
399 {0x0002, 0x8502},
400 {0x0039, 0x8801},
401 {0x0000, 0x8805},
402 {0x0000, 0x8800},
403
404 {0x0087, 0x8700}, /* overwrite by start */
405 {0x0081, 0x8702},
406 {0x0000, 0x8500},
407/* {0x0010, 0x8500}, -- Previous line was this */
408 {0x0002, 0x865b},
409 {0x0003, 0x865c},
410 /***************/
411 {0x0003, 0x8801}, /* 0x121-> 289 */
412 {0x0021, 0x8805},
413 {0x0001, 0x8800},
414 {0x0004, 0x8801}, /* 0x165 -> 357 */
415 {0x0065, 0x8805},
416 {0x0001, 0x8800},
417 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
418 {0x002f, 0x8805},
419 {0x0000, 0x8800},
420 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
421 {0x0000, 0x8805},
422 {0x0000, 0x8800},
423 {0x000a, 0x8801}, /* 0x01 //0x02 */
424 {0x0001, 0x8805},
425 {0x0000, 0x8800},
426 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
427 * 0001 0 | 000 0110 0001 */
428 {0x0061, 0x8805}, /* 61 31 */
429 {0x0008, 0x8800}, /* 08 */
430 {0x0035, 0x8801}, /* 0x14 - set gain general */
431 {0x001f, 0x8805}, /* 0x14 */
432 {0x0000, 0x8800},
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300433 {0x000e, 0x8112}, /* white balance - was 30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300434 {}
435};
436
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300437
438/******************** QC Express etch2 stuff ********************/
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300439static const __u16 Pb100_1map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300440 /* reg, value */
441 {0x8320, 0x3304},
442
443 {0x8303, 0x0125}, /* image area */
444 {0x8304, 0x0169},
445 {0x8328, 0x000b},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300446 {0x833c, 0x0001}, /*fixme: win:07*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300447
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300448 {0x832f, 0x1904}, /*fixme: was 0419*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300449 {0x8307, 0x00aa},
450 {0x8301, 0x0003},
451 {0x8302, 0x000e},
452 {}
453};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300454static const __u16 Pb100_2map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300455 /* reg, value */
456 {0x8339, 0x0000},
457 {0x8307, 0x00aa},
458 {}
459};
460
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300461static const __u16 spca561_161rev12A_data1[][2] = {
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300462 {0x29, 0x8118}, /* white balance - was 21 */
463 {0x08, 0x8114}, /* white balance - was 01 */
464 {0x0e, 0x8112}, /* white balance - was 00 */
465 {0x00, 0x8102}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300466 {0x92, 0x8804},
467 {0x04, 0x8802}, /* windows uses 08 */
468 {}
469};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300470static const __u16 spca561_161rev12A_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300471 {0x21, 0x8118},
472 {0x10, 0x8500},
473 {0x07, 0x8601},
474 {0x07, 0x8602},
475 {0x04, 0x8501},
476 {0x21, 0x8118},
477
478 {0x07, 0x8201}, /* windows uses 02 */
479 {0x08, 0x8200},
480 {0x01, 0x8200},
481
482 {0x00, 0x8114},
483 {0x01, 0x8114}, /* windows uses 00 */
484
485 {0x90, 0x8604},
486 {0x00, 0x8605},
487 {0xb0, 0x8603},
488
489 /* sensor gains */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300490 {0x07, 0x8601}, /* white balance - new */
491 {0x07, 0x8602}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300492 {0x00, 0x8610}, /* *red */
493 {0x00, 0x8611}, /* 3f *green */
494 {0x00, 0x8612}, /* green *blue */
495 {0x00, 0x8613}, /* blue *green */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300496 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
497 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
498 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
499 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300500
501 {0x0c, 0x8620}, /* 0c */
502 {0xc8, 0x8631}, /* c8 */
503 {0xc8, 0x8634}, /* c8 */
504 {0x23, 0x8635}, /* 23 */
505 {0x1f, 0x8636}, /* 1f */
506 {0xdd, 0x8637}, /* dd */
507 {0xe1, 0x8638}, /* e1 */
508 {0x1d, 0x8639}, /* 1d */
509 {0x21, 0x863a}, /* 21 */
510 {0xe3, 0x863b}, /* e3 */
511 {0xdf, 0x863c}, /* df */
512 {0xf0, 0x8505},
513 {0x32, 0x850a},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300514/* {0x99, 0x8700}, * - white balance - new (removed) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300515 {}
516};
517
518static void sensor_mapwrite(struct gspca_dev *gspca_dev,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300519 const __u16 sensormap[][2])
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300520{
521 int i = 0;
522 __u8 usbval[2];
523
524 while (sensormap[i][0]) {
525 usbval[0] = sensormap[i][1];
526 usbval[1] = sensormap[i][1] >> 8;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300527 reg_w_buf(gspca_dev, sensormap[i][0], usbval, 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300528 i++;
529 }
530}
531static void init_161rev12A(struct gspca_dev *gspca_dev)
532{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300533/* sensor_reset(gspca_dev); (not in win) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300534 write_vector(gspca_dev, spca561_161rev12A_data1);
535 sensor_mapwrite(gspca_dev, Pb100_1map8300);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300536/*fixme: should be in sd_start*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300537 write_vector(gspca_dev, spca561_161rev12A_data2);
538 sensor_mapwrite(gspca_dev, Pb100_2map8300);
539}
540
541/* this function is called at probe time */
542static int sd_config(struct gspca_dev *gspca_dev,
543 const struct usb_device_id *id)
544{
545 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300546 struct cam *cam;
547 __u16 vendor, product;
548 __u8 data1, data2;
549
550 /* Read frm global register the USB product and vendor IDs, just to
551 * prove that we can communicate with the device. This works, which
552 * confirms at we are communicating properly and that the device
553 * is a 561. */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300554 reg_r(gspca_dev, 0x8104, 1);
555 data1 = gspca_dev->usb_buf[0];
556 reg_r(gspca_dev, 0x8105, 1);
557 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300558 vendor = (data2 << 8) | data1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300559 reg_r(gspca_dev, 0x8106, 1);
560 data1 = gspca_dev->usb_buf[0];
561 reg_r(gspca_dev, 0x8107, 1);
562 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300563 product = (data2 << 8) | data1;
564 if (vendor != id->idVendor || product != id->idProduct) {
565 PDEBUG(D_PROBE, "Bad vendor / product from device");
566 return -EINVAL;
567 }
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300568
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300569 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300570 cam->epaddr = 0x01;
571 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300572
573 sd->chip_revision = id->driver_info;
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300574 if (sd->chip_revision == Rev012A) {
575 cam->cam_mode = sif_012a_mode;
576 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
577 } else {
578 cam->cam_mode = sif_072a_mode;
579 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
580 }
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300581 sd->brightness = BRIGHTNESS_DEF;
582 sd->contrast = CONTRAST_DEF;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300583 sd->white = WHITE_DEF;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300584 sd->exposure = EXPOSURE_DEF;
585 sd->autogain = AUTOGAIN_DEF;
586 sd->gain = GAIN_DEF;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300587 sd->expo12a = EXPO12A_DEF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300588 return 0;
589}
590
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300591/* this function is called at probe and resume time */
592static int sd_init_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300593{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300594 PDEBUG(D_STREAM, "Chip revision: 012a");
595 init_161rev12A(gspca_dev);
596 return 0;
597}
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300598static int sd_init_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300599{
600 PDEBUG(D_STREAM, "Chip revision: 072a");
601 write_vector(gspca_dev, spca561_init_data);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300602 return 0;
603}
604
605static void setcontrast(struct gspca_dev *gspca_dev)
606{
607 struct sd *sd = (struct sd *) gspca_dev;
608 struct usb_device *dev = gspca_dev->dev;
609 __u8 lowb;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300610
611 switch (sd->chip_revision) {
612 case Rev072A:
613 lowb = sd->contrast >> 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300614 reg_w_val(dev, 0x8651, lowb);
615 reg_w_val(dev, 0x8652, lowb);
616 reg_w_val(dev, 0x8653, lowb);
617 reg_w_val(dev, 0x8654, lowb);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300618 break;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300619 default: {
620/* case Rev012A: { */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300621 static const __u8 Reg8391[] =
622 { 0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00 };
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300623
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300624 reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);
625 reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300626 break;
627 }
628 }
629}
630
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300631/* rev12a only */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300632static void setwhite(struct gspca_dev *gspca_dev)
633{
634 struct sd *sd = (struct sd *) gspca_dev;
635 __u16 white;
636 __u8 reg8614, reg8616;
637
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300638 white = sd->white;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300639 /* try to emulate MS-win as possible */
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300640 reg8616 = 0x90 - white * 5 / 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300641 reg_w_val(gspca_dev->dev, 0x8616, reg8616);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300642 reg8614 = 0x20 + white * 3 / 8;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300643 reg_w_val(gspca_dev->dev, 0x8614, reg8614);
644}
645
646/* rev 12a only */
647static void setexposure(struct gspca_dev *gspca_dev)
648{
649 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300650 int expo;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300651 __u8 data[2];
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300652
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300653 expo = sd->exposure + 0x20a8; /* from test */
654 data[0] = expo;
655 data[1] = expo >> 8;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300656 reg_w_buf(gspca_dev, 0x8309, data, 2);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300657}
658
659/* rev 12a only */
660static void setgain(struct gspca_dev *gspca_dev)
661{
662 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300663 __u8 data[2];
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300664
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300665 data[0] = sd->gain;
666 data[1] = 0;
667 reg_w_buf(gspca_dev, 0x8335, data, 2);
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300668}
669
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300670static void setautogain(struct gspca_dev *gspca_dev)
671{
672 struct sd *sd = (struct sd *) gspca_dev;
673
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300674 if (sd->autogain)
675 sd->ag_cnt = AG_CNT_START;
676 else
677 sd->ag_cnt = -1;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300678}
679
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300680static void sd_start_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300681{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300682 struct usb_device *dev = gspca_dev->dev;
683 int Clck;
684 __u8 Reg8307[] = { 0xaa, 0x00 };
685 int mode;
686
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300687 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300688 switch (mode) {
689 case 0:
690 case 1:
691 Clck = 0x8a;
692 break;
693 case 2:
694 Clck = 0x85;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300695 break;
696 default:
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300697 Clck = 0x83;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300698 break;
699 }
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 */
709 reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);
710 reg_w_val(gspca_dev->dev, 0x8700, Clck);
711 /* 0x8f 0x85 0x27 clock */
712 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
713 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
714 setcontrast(gspca_dev);
715 setwhite(gspca_dev);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300716 setautogain(gspca_dev);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300717}
718static void sd_start_72a(struct gspca_dev *gspca_dev)
719{
720 struct usb_device *dev = gspca_dev->dev;
721 int Clck;
722 int mode;
723
724 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
725 switch (mode) {
726 default:
727/* case 0:
728 case 1: */
729 Clck = 0x25;
730 break;
731 case 2:
732 Clck = 0x22;
733 break;
734 case 3:
735 Clck = 0x21;
736 break;
737 }
738 reg_w_val(dev, 0x8500, mode); /* mode */
739 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
740 reg_w_val(dev, 0x8112, 0x10 | 0x20);
741 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300742}
743
744static void sd_stopN(struct gspca_dev *gspca_dev)
745{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300746 struct sd *sd = (struct sd *) gspca_dev;
747
748 if (sd->chip_revision == Rev012A) {
749 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
750 } else {
751 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
752/* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
753 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300754}
755
756static void sd_stop0(struct gspca_dev *gspca_dev)
757{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300758 struct sd *sd = (struct sd *) gspca_dev;
759
760 if (sd->chip_revision == Rev012A) {
761 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
762 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
763 }
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300764/* reg_w_val(gspca_dev->dev, 0x8114, 0); */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300765}
766
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300767static void do_autogain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300768{
769 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300770 int expotimes;
771 int pixelclk;
772 int gainG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300773 __u8 R, Gr, Gb, B;
774 int y;
775 __u8 luma_mean = 110;
776 __u8 luma_delta = 20;
777 __u8 spring = 4;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300778 __u8 reg8339[2];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300779
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300780 if (sd->ag_cnt < 0)
781 return;
782 if (--sd->ag_cnt >= 0)
783 return;
784 sd->ag_cnt = AG_CNT_START;
785
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300786 switch (sd->chip_revision) {
787 case Rev072A:
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300788 reg_r(gspca_dev, 0x8621, 1);
789 Gr = gspca_dev->usb_buf[0];
790 reg_r(gspca_dev, 0x8622, 1);
791 R = gspca_dev->usb_buf[0];
792 reg_r(gspca_dev, 0x8623, 1);
793 B = gspca_dev->usb_buf[0];
794 reg_r(gspca_dev, 0x8624, 1);
795 Gb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300796 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
797 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
798 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
799 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
800
801 if (y < luma_mean - luma_delta ||
802 y > luma_mean + luma_delta) {
803 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
804 pixelclk = 0x0800;
805 expotimes = expotimes & 0x07ff;
806 /* PDEBUG(D_PACK,
807 "Exposition Times 0x%03X Clock 0x%04X ",
808 expotimes,pixelclk); */
809 gainG = i2c_read(gspca_dev, 0x35, 0x10);
810 /* PDEBUG(D_PACK,
811 "reading Gain register %d", gainG); */
812
813 expotimes += (luma_mean - y) >> spring;
814 gainG += (luma_mean - y) / 50;
815 /* PDEBUG(D_PACK,
816 "compute expotimes %d gain %d",
817 expotimes,gainG); */
818
819 if (gainG > 0x3f)
820 gainG = 0x3f;
821 else if (gainG < 4)
822 gainG = 3;
823 i2c_write(gspca_dev, gainG, 0x35);
824
825 if (expotimes >= 0x0256)
826 expotimes = 0x0256;
827 else if (expotimes < 4)
828 expotimes = 3;
829 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
830 }
831 break;
832 case Rev012A:
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300833 reg_r(gspca_dev, 0x8330, 2);
834 if (gspca_dev->usb_buf[1] > 0x08) {
835 reg8339[0] = ++sd->expo12a;
836 reg8339[1] = 0;
837 reg_w_buf(gspca_dev, 0x8339, reg8339, 2);
838 } else if (gspca_dev->usb_buf[1] < 0x02) {
839 reg8339[0] = --sd->expo12a;
840 reg8339[1] = 0;
841 reg_w_buf(gspca_dev, 0x8339, reg8339, 2);
842 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300843 break;
844 }
845}
846
847static void sd_pkt_scan(struct gspca_dev *gspca_dev,
848 struct gspca_frame *frame, /* target */
849 __u8 *data, /* isoc packet */
850 int len) /* iso packet length */
851{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300852 switch (data[0]) {
853 case 0: /* start of frame */
854 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
855 data, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300856 data += SPCA561_OFFSET_DATA;
857 len -= SPCA561_OFFSET_DATA;
858 if (data[1] & 0x10) {
859 /* compressed bayer */
860 gspca_frame_add(gspca_dev, FIRST_PACKET,
861 frame, data, len);
862 } else {
Hans de Goede54ab92c2008-07-03 11:20:58 -0300863 /* raw bayer (with a header, which we skip) */
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300864/*fixme: is this specific to the rev012a? */
865 data += 16;
866 len -= 16;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300867 gspca_frame_add(gspca_dev, FIRST_PACKET,
868 frame, data, len);
869 }
870 return;
871 case 0xff: /* drop */
872/* gspca_dev->last_packet_type = DISCARD_PACKET; */
873 return;
874 }
875 data++;
876 len--;
877 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
878}
879
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300880/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300881static void setbrightness(struct gspca_dev *gspca_dev)
882{
883 struct sd *sd = (struct sd *) gspca_dev;
884 __u8 value;
885
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300886 value = sd->brightness;
887 reg_w_val(gspca_dev->dev, 0x8611, value);
888 reg_w_val(gspca_dev->dev, 0x8612, value);
889 reg_w_val(gspca_dev->dev, 0x8613, value);
890 reg_w_val(gspca_dev->dev, 0x8614, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300891}
892
893static void getbrightness(struct gspca_dev *gspca_dev)
894{
895 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300896 __u16 tot;
897
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300898 tot = 0;
899 reg_r(gspca_dev, 0x8611, 1);
900 tot += gspca_dev->usb_buf[0];
901 reg_r(gspca_dev, 0x8612, 1);
902 tot += gspca_dev->usb_buf[0];
903 reg_r(gspca_dev, 0x8613, 1);
904 tot += gspca_dev->usb_buf[0];
905 reg_r(gspca_dev, 0x8614, 1);
906 tot += gspca_dev->usb_buf[0];
907 sd->brightness = tot >> 2;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300908}
909
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300910/* rev72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300911static void getcontrast(struct gspca_dev *gspca_dev)
912{
913 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300914 __u16 tot;
915
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300916 tot = 0;
917 reg_r(gspca_dev, 0x8651, 1);
918 tot += gspca_dev->usb_buf[0];
919 reg_r(gspca_dev, 0x8652, 1);
920 tot += gspca_dev->usb_buf[0];
921 reg_r(gspca_dev, 0x8653, 1);
922 tot += gspca_dev->usb_buf[0];
923 reg_r(gspca_dev, 0x8654, 1);
924 tot += gspca_dev->usb_buf[0];
925 sd->contrast = tot << 6;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300926 PDEBUG(D_CONF, "get contrast %d", sd->contrast);
927}
928
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300929/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300930static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
931{
932 struct sd *sd = (struct sd *) gspca_dev;
933
934 sd->brightness = val;
935 if (gspca_dev->streaming)
936 setbrightness(gspca_dev);
937 return 0;
938}
939
940static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
941{
942 struct sd *sd = (struct sd *) gspca_dev;
943
944 getbrightness(gspca_dev);
945 *val = sd->brightness;
946 return 0;
947}
948
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300949/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300950static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
951{
952 struct sd *sd = (struct sd *) gspca_dev;
953
954 sd->contrast = val;
955 if (gspca_dev->streaming)
956 setcontrast(gspca_dev);
957 return 0;
958}
959
960static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
961{
962 struct sd *sd = (struct sd *) gspca_dev;
963
964 getcontrast(gspca_dev);
965 *val = sd->contrast;
966 return 0;
967}
968
969static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
970{
971 struct sd *sd = (struct sd *) gspca_dev;
972
973 sd->autogain = val;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300974 if (gspca_dev->streaming)
975 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300976 return 0;
977}
978
979static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
980{
981 struct sd *sd = (struct sd *) gspca_dev;
982
983 *val = sd->autogain;
984 return 0;
985}
986
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300987/* rev12a only */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300988static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
989{
990 struct sd *sd = (struct sd *) gspca_dev;
991
992 sd->white = val;
993 if (gspca_dev->streaming)
994 setwhite(gspca_dev);
995 return 0;
996}
997
998static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
999{
1000 struct sd *sd = (struct sd *) gspca_dev;
1001
1002 *val = sd->white;
1003 return 0;
1004}
1005
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001006/* rev12a only */
1007static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1008{
1009 struct sd *sd = (struct sd *) gspca_dev;
1010
1011 sd->exposure = val;
1012 if (gspca_dev->streaming)
1013 setexposure(gspca_dev);
1014 return 0;
1015}
1016
1017static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1018{
1019 struct sd *sd = (struct sd *) gspca_dev;
1020
1021 *val = sd->exposure;
1022 return 0;
1023}
1024
1025/* rev12a only */
1026static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1027{
1028 struct sd *sd = (struct sd *) gspca_dev;
1029
1030 sd->gain = val;
1031 if (gspca_dev->streaming)
1032 setgain(gspca_dev);
1033 return 0;
1034}
1035
1036static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1037{
1038 struct sd *sd = (struct sd *) gspca_dev;
1039
1040 *val = sd->gain;
1041 return 0;
1042}
1043
1044/* control tables */
1045static struct ctrl sd_ctrls_12a[] = {
1046 {
1047 {
1048 .id = V4L2_CID_DO_WHITE_BALANCE,
1049 .type = V4L2_CTRL_TYPE_INTEGER,
1050 .name = "While Balance",
1051 .minimum = WHITE_MIN,
1052 .maximum = WHITE_MAX,
1053 .step = 1,
1054 .default_value = WHITE_DEF,
1055 },
1056 .set = sd_setwhite,
1057 .get = sd_getwhite,
1058 },
1059 {
1060 {
1061 .id = V4L2_CID_EXPOSURE,
1062 .type = V4L2_CTRL_TYPE_INTEGER,
1063 .name = "Exposure",
1064 .minimum = EXPOSURE_MIN,
1065 .maximum = EXPOSURE_MAX,
1066 .step = 1,
1067 .default_value = EXPOSURE_DEF,
1068 },
1069 .set = sd_setexposure,
1070 .get = sd_getexposure,
1071 },
1072 {
1073 {
1074 .id = V4L2_CID_AUTOGAIN,
1075 .type = V4L2_CTRL_TYPE_BOOLEAN,
1076 .name = "Auto Gain",
1077 .minimum = AUTOGAIN_MIN,
1078 .maximum = AUTOGAIN_MAX,
1079 .step = 1,
1080 .default_value = AUTOGAIN_DEF,
1081 },
1082 .set = sd_setautogain,
1083 .get = sd_getautogain,
1084 },
1085 {
1086 {
1087 .id = V4L2_CID_GAIN,
1088 .type = V4L2_CTRL_TYPE_INTEGER,
1089 .name = "Gain",
1090 .minimum = GAIN_MIN,
1091 .maximum = GAIN_MAX,
1092 .step = 1,
1093 .default_value = GAIN_DEF,
1094 },
1095 .set = sd_setgain,
1096 .get = sd_getgain,
1097 },
1098};
1099
1100static struct ctrl sd_ctrls_72a[] = {
1101 {
1102 {
1103 .id = V4L2_CID_BRIGHTNESS,
1104 .type = V4L2_CTRL_TYPE_INTEGER,
1105 .name = "Brightness",
1106 .minimum = BRIGHTNESS_MIN,
1107 .maximum = BRIGHTNESS_MAX,
1108 .step = 1,
1109 .default_value = BRIGHTNESS_DEF,
1110 },
1111 .set = sd_setbrightness,
1112 .get = sd_getbrightness,
1113 },
1114 {
1115 {
1116 .id = V4L2_CID_CONTRAST,
1117 .type = V4L2_CTRL_TYPE_INTEGER,
1118 .name = "Contrast",
1119 .minimum = CONTRAST_MIN,
1120 .maximum = CONTRAST_MAX,
1121 .step = 1,
1122 .default_value = CONTRAST_DEF,
1123 },
1124 .set = sd_setcontrast,
1125 .get = sd_getcontrast,
1126 },
1127 {
1128 {
1129 .id = V4L2_CID_AUTOGAIN,
1130 .type = V4L2_CTRL_TYPE_BOOLEAN,
1131 .name = "Auto Gain",
1132 .minimum = AUTOGAIN_MIN,
1133 .maximum = AUTOGAIN_MAX,
1134 .step = 1,
1135 .default_value = AUTOGAIN_DEF,
1136 },
1137 .set = sd_setautogain,
1138 .get = sd_getautogain,
1139 },
1140};
1141
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001142/* sub-driver description */
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001143static const struct sd_desc sd_desc_12a = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001144 .name = MODULE_NAME,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001145 .ctrls = sd_ctrls_12a,
1146 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001147 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001148 .init = sd_init_12a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001149 .start = sd_start_12a,
1150 .stopN = sd_stopN,
1151 .stop0 = sd_stop0,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001152 .pkt_scan = sd_pkt_scan,
1153/* .dq_callback = do_autogain, * fixme */
1154};
1155static const struct sd_desc sd_desc_72a = {
1156 .name = MODULE_NAME,
1157 .ctrls = sd_ctrls_72a,
1158 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1159 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001160 .init = sd_init_72a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001161 .start = sd_start_72a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001162 .stopN = sd_stopN,
1163 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001164 .pkt_scan = sd_pkt_scan,
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -03001165 .dq_callback = do_autogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001166};
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001167static const struct sd_desc *sd_desc[2] = {
1168 &sd_desc_12a,
1169 &sd_desc_72a
1170};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001171
1172/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001173static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine87581aa2008-07-26 14:30:01 -03001174 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1175 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1176 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1177 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1178 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1179 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1180 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1181 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1182 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1183 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1184 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1185 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1186 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1187 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1188 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001189 {}
1190};
1191
1192MODULE_DEVICE_TABLE(usb, device_table);
1193
1194/* -- device connect -- */
1195static int sd_probe(struct usb_interface *intf,
1196 const struct usb_device_id *id)
1197{
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001198 return gspca_dev_probe(intf, id,
1199 sd_desc[id->driver_info],
1200 sizeof(struct sd),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001201 THIS_MODULE);
1202}
1203
1204static struct usb_driver sd_driver = {
1205 .name = MODULE_NAME,
1206 .id_table = device_table,
1207 .probe = sd_probe,
1208 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001209#ifdef CONFIG_PM
1210 .suspend = gspca_suspend,
1211 .resume = gspca_resume,
1212#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001213};
1214
1215/* -- module insert / remove -- */
1216static int __init sd_mod_init(void)
1217{
1218 if (usb_register(&sd_driver) < 0)
1219 return -1;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001220 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001221 return 0;
1222}
1223static void __exit sd_mod_exit(void)
1224{
1225 usb_deregister(&sd_driver);
1226 PDEBUG(D_PROBE, "deregistered");
1227}
1228
1229module_init(sd_mod_init);
1230module_exit(sd_mod_exit);