blob: c99fe0f904fe645c5379812e5e4490d75f77bb8f [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
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300144#define SPCA561_INDEX_I2C_BASE 0x8800
145#define SPCA561_SNAPBIT 0x20
146#define SPCA561_SNAPCTRL 0x40
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300147
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300148static const u16 rev72a_reset[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300149 {0x0000, 0x8114}, /* Software GPIO output data */
150 {0x0001, 0x8114}, /* Software GPIO output data */
151 {0x0000, 0x8112}, /* Some kind of reset */
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300152 {}
153};
154static const __u16 rev72a_init_data1[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300155 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
156 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
157 {0x0011, 0x8118}, /* Enable and conf sensor */
158 {0x0001, 0x8118}, /* Conf sensor */
159 {0x0092, 0x8804}, /* I know nothing about these */
160 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300161 {}
162};
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300163static const u16 rev72a_init_sensor1[][2] = {
164 {0x0001, 0x000d},
165 {0x0002, 0x0018},
166 {0x0004, 0x0165},
167 {0x0005, 0x0021},
168 {0x0007, 0x00aa},
169 {0x0020, 0x1504},
170 {0x0039, 0x0002},
171 {0x0035, 0x0010},
172 {0x0009, 0x1049},
173 {0x0028, 0x000b},
174 {0x003b, 0x000f},
175 {0x003c, 0x0000},
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300176 {}
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 */
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300193/* from ms-win */
194 {0x0000, 0x8611}, /* R offset for white balance */
195 {0x00fd, 0x8612}, /* Gr offset for white balance */
196 {0x0003, 0x8613}, /* B offset for white balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300197 {0x0000, 0x8614}, /* Gb offset for white balance */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300198/* from ms-win */
199 {0x0035, 0x8651}, /* R gain for white balance */
200 {0x0040, 0x8652}, /* Gr gain for white balance */
201 {0x005f, 0x8653}, /* B gain for white balance */
202 {0x0040, 0x8654}, /* Gb gain for white balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300203 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300204 {0x0011, 0x8802},
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300205
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300206 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
207 {0x0081, 0x8702}, /* Master clock output enable */
208
209 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
210 /* Originally was 0x0010 (352x288 compression) */
211
212 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
213 {0x0003, 0x865c}, /* Vertical offset for valid lines */
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300214 {}
215};
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300216static const u16 rev72a_init_sensor2[][2] = {
217 {0x0003, 0x0121},
218 {0x0004, 0x0165},
219 {0x0005, 0x002f}, /* blanking control column */
220 {0x0006, 0x0000}, /* blanking mode row*/
221 {0x000a, 0x0002},
222 {0x0009, 0x1061}, /* setexposure times && pixel clock
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300223 * 0001 0 | 000 0110 0001 */
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300224 {0x0035, 0x0014},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300225 {}
226};
227
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300228/******************** QC Express etch2 stuff ********************/
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300229static const __u16 Pb100_1map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300230 /* reg, value */
231 {0x8320, 0x3304},
232
233 {0x8303, 0x0125}, /* image area */
234 {0x8304, 0x0169},
235 {0x8328, 0x000b},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300236 {0x833c, 0x0001}, /*fixme: win:07*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300237
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300238 {0x832f, 0x1904}, /*fixme: was 0419*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300239 {0x8307, 0x00aa},
240 {0x8301, 0x0003},
241 {0x8302, 0x000e},
242 {}
243};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300244static const __u16 Pb100_2map8300[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300245 /* reg, value */
246 {0x8339, 0x0000},
247 {0x8307, 0x00aa},
248 {}
249};
250
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300251static const __u16 spca561_161rev12A_data1[][2] = {
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300252 {0x29, 0x8118}, /* white balance - was 21 */
253 {0x08, 0x8114}, /* white balance - was 01 */
254 {0x0e, 0x8112}, /* white balance - was 00 */
255 {0x00, 0x8102}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300256 {0x92, 0x8804},
257 {0x04, 0x8802}, /* windows uses 08 */
258 {}
259};
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300260static const __u16 spca561_161rev12A_data2[][2] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300261 {0x21, 0x8118},
262 {0x10, 0x8500},
263 {0x07, 0x8601},
264 {0x07, 0x8602},
265 {0x04, 0x8501},
266 {0x21, 0x8118},
267
268 {0x07, 0x8201}, /* windows uses 02 */
269 {0x08, 0x8200},
270 {0x01, 0x8200},
271
272 {0x00, 0x8114},
273 {0x01, 0x8114}, /* windows uses 00 */
274
275 {0x90, 0x8604},
276 {0x00, 0x8605},
277 {0xb0, 0x8603},
278
279 /* sensor gains */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300280 {0x07, 0x8601}, /* white balance - new */
281 {0x07, 0x8602}, /* white balance - new */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300282 {0x00, 0x8610}, /* *red */
283 {0x00, 0x8611}, /* 3f *green */
284 {0x00, 0x8612}, /* green *blue */
285 {0x00, 0x8613}, /* blue *green */
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300286 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
287 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
288 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
289 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300290
291 {0x0c, 0x8620}, /* 0c */
292 {0xc8, 0x8631}, /* c8 */
293 {0xc8, 0x8634}, /* c8 */
294 {0x23, 0x8635}, /* 23 */
295 {0x1f, 0x8636}, /* 1f */
296 {0xdd, 0x8637}, /* dd */
297 {0xe1, 0x8638}, /* e1 */
298 {0x1d, 0x8639}, /* 1d */
299 {0x21, 0x863a}, /* 21 */
300 {0xe3, 0x863b}, /* e3 */
301 {0xdf, 0x863c}, /* df */
302 {0xf0, 0x8505},
303 {0x32, 0x850a},
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300304/* {0x99, 0x8700}, * - white balance - new (removed) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300305 {}
306};
307
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300308static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300309{
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300310 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300311
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300312 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
313 0, /* request */
314 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
315 value, index, NULL, 0, 500);
316 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
317 if (ret < 0)
318 PDEBUG(D_ERR, "reg write: error %d", ret);
319}
320
321static void write_vector(struct gspca_dev *gspca_dev,
322 const __u16 data[][2])
323{
324 struct usb_device *dev = gspca_dev->dev;
325 int i;
326
327 i = 0;
328 while (data[i][1] != 0) {
329 reg_w_val(dev, data[i][1], data[i][0]);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300330 i++;
331 }
332}
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300333
334/* read 'len' bytes to gspca_dev->usb_buf */
335static void reg_r(struct gspca_dev *gspca_dev,
336 __u16 index, __u16 length)
337{
338 usb_control_msg(gspca_dev->dev,
339 usb_rcvctrlpipe(gspca_dev->dev, 0),
340 0, /* request */
341 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
342 0, /* value */
343 index, gspca_dev->usb_buf, length, 500);
344}
345
346/* write 'len' bytes from gspca_dev->usb_buf */
347static void reg_w_buf(struct gspca_dev *gspca_dev,
348 __u16 index, __u16 len)
349{
350 usb_control_msg(gspca_dev->dev,
351 usb_sndctrlpipe(gspca_dev->dev, 0),
352 0, /* request */
353 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
354 0, /* value */
355 index, gspca_dev->usb_buf, len, 500);
356}
357
358static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
359{
360 int retry = 60;
361
362 reg_w_val(gspca_dev->dev, 0x8801, reg);
363 reg_w_val(gspca_dev->dev, 0x8805, value);
364 reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
365 do {
366 reg_r(gspca_dev, 0x8803, 1);
367 if (!gspca_dev->usb_buf[0])
368 return;
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300369 msleep(10);
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300370 } while (--retry);
371}
372
373static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
374{
375 int retry = 60;
376 __u8 value;
377
378 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
379 reg_w_val(gspca_dev->dev, 0x8801, reg);
380 reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
381 do {
382 reg_r(gspca_dev, 0x8803, 1);
383 if (!gspca_dev->usb_buf[0]) {
384 reg_r(gspca_dev, 0x8800, 1);
385 value = gspca_dev->usb_buf[0];
386 reg_r(gspca_dev, 0x8805, 1);
387 return ((int) value << 8) | gspca_dev->usb_buf[0];
388 }
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300389 msleep(10);
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300390 } while (--retry);
391 return -1;
392}
393
394static void sensor_mapwrite(struct gspca_dev *gspca_dev,
395 const __u16 (*sensormap)[2])
396{
397 while ((*sensormap)[0]) {
398 gspca_dev->usb_buf[0] = (*sensormap)[1];
399 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
400 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
401 sensormap++;
402 }
403}
404
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300405static void write_sensor_72a(struct gspca_dev *gspca_dev,
406 const __u16 (*sensor)[2])
407{
408 while ((*sensor)[0]) {
409 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
410 sensor++;
411 }
412}
413
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300414static void init_161rev12A(struct gspca_dev *gspca_dev)
415{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300416 write_vector(gspca_dev, spca561_161rev12A_data1);
417 sensor_mapwrite(gspca_dev, Pb100_1map8300);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300418/*fixme: should be in sd_start*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300419 write_vector(gspca_dev, spca561_161rev12A_data2);
420 sensor_mapwrite(gspca_dev, Pb100_2map8300);
421}
422
423/* this function is called at probe time */
424static int sd_config(struct gspca_dev *gspca_dev,
425 const struct usb_device_id *id)
426{
427 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300428 struct cam *cam;
429 __u16 vendor, product;
430 __u8 data1, data2;
431
432 /* Read frm global register the USB product and vendor IDs, just to
433 * prove that we can communicate with the device. This works, which
434 * confirms at we are communicating properly and that the device
435 * is a 561. */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300436 reg_r(gspca_dev, 0x8104, 1);
437 data1 = gspca_dev->usb_buf[0];
438 reg_r(gspca_dev, 0x8105, 1);
439 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300440 vendor = (data2 << 8) | data1;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300441 reg_r(gspca_dev, 0x8106, 1);
442 data1 = gspca_dev->usb_buf[0];
443 reg_r(gspca_dev, 0x8107, 1);
444 data2 = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300445 product = (data2 << 8) | data1;
446 if (vendor != id->idVendor || product != id->idProduct) {
447 PDEBUG(D_PROBE, "Bad vendor / product from device");
448 return -EINVAL;
449 }
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300450
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300451 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300452 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300453
454 sd->chip_revision = id->driver_info;
Jean-Francois Moineb77c0042008-09-04 16:22:56 -0300455 if (sd->chip_revision == Rev012A) {
456 cam->cam_mode = sif_012a_mode;
457 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
458 } else {
459 cam->cam_mode = sif_072a_mode;
460 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
461 }
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300462 sd->brightness = BRIGHTNESS_DEF;
463 sd->contrast = CONTRAST_DEF;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300464 sd->white = WHITE_DEF;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300465 sd->exposure = EXPOSURE_DEF;
466 sd->autogain = AUTOGAIN_DEF;
467 sd->gain = GAIN_DEF;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300468 sd->expo12a = EXPO12A_DEF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300469 return 0;
470}
471
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300472/* this function is called at probe and resume time */
473static int sd_init_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300474{
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300475 PDEBUG(D_STREAM, "Chip revision: 012a");
476 init_161rev12A(gspca_dev);
477 return 0;
478}
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300479static int sd_init_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300480{
481 PDEBUG(D_STREAM, "Chip revision: 072a");
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300482 write_vector(gspca_dev, rev72a_reset);
483 msleep(200);
Jean-Francois Moinef8a04a62008-12-19 07:21:26 -0300484 write_vector(gspca_dev, rev72a_init_data1);
485 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
486 write_vector(gspca_dev, rev72a_init_data2);
487 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300488 reg_w_val(gspca_dev->dev, 0x8112, 0x30);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300489 return 0;
490}
491
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300492/* rev 72a only */
493static void setbrightness(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300494{
495 struct sd *sd = (struct sd *) gspca_dev;
496 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300497 __u8 value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300498
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300499 value = sd->brightness;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300500
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300501 /* offsets for white balance */
502 reg_w_val(dev, 0x8611, value); /* R */
503 reg_w_val(dev, 0x8612, value); /* Gr */
504 reg_w_val(dev, 0x8613, value); /* B */
505 reg_w_val(dev, 0x8614, value); /* Gb */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300506}
507
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300508static void setwhite(struct gspca_dev *gspca_dev)
509{
510 struct sd *sd = (struct sd *) gspca_dev;
511 __u16 white;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300512 __u8 blue, red;
513 __u16 reg;
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300514
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300515 /* try to emulate MS-win as possible */
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300516 white = sd->white;
517 red = 0x20 + white * 3 / 8;
518 blue = 0x90 - white * 5 / 8;
519 if (sd->chip_revision == Rev012A) {
520 reg = 0x8614;
521 } else {
522 reg = 0x8651;
523 red += sd->contrast - 0x20;
524 blue += sd->contrast - 0x20;
525 }
526 reg_w_val(gspca_dev->dev, reg, red);
527 reg_w_val(gspca_dev->dev, reg + 2, blue);
528}
529
530static void setcontrast(struct gspca_dev *gspca_dev)
531{
532 struct sd *sd = (struct sd *) gspca_dev;
533 struct usb_device *dev = gspca_dev->dev;
534 __u8 value;
535
536 if (sd->chip_revision != Rev072A)
537 return;
538 value = sd->contrast + 0x20;
539
540 /* gains for white balance */
541 setwhite(gspca_dev);
542/* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
543 reg_w_val(dev, 0x8652, value); /* Gr */
544/* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
545 reg_w_val(dev, 0x8654, value); /* Gb */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300546}
547
548/* rev 12a only */
549static void setexposure(struct gspca_dev *gspca_dev)
550{
551 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine3a1ea702008-09-03 16:47:55 -0300552 int expo;
Hans de Goede0fc23d22008-09-04 16:22:57 -0300553 int clock_divider;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300554
Hans de Goede0fc23d22008-09-04 16:22:57 -0300555 /* Register 0x8309 controls exposure for the spca561,
556 the basic exposure setting goes from 1-2047, where 1 is completely
557 dark and 2047 is very bright. It not only influences exposure but
558 also the framerate (to allow for longer exposure) from 1 - 300 it
559 only raises the exposure time then from 300 - 600 it halves the
560 framerate to be able to further raise the exposure time and for every
561 300 more it halves the framerate again. This allows for a maximum
562 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
563 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
564 configure a divider for the base framerate which us used at the
565 exposure setting of 1-300. These bits configure the base framerate
566 according to the following formula: fps = 60 / (value + 2) */
567 if (sd->exposure < 2048) {
568 expo = sd->exposure;
569 clock_divider = 0;
570 } else {
571 /* Add 900 to make the 0 setting of the second part of the
572 exposure equal to the 2047 setting of the first part. */
573 expo = (sd->exposure - 2048) + 900;
574 clock_divider = 3;
575 }
576 expo |= clock_divider << 11;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300577 gspca_dev->usb_buf[0] = expo;
578 gspca_dev->usb_buf[1] = expo >> 8;
579 reg_w_buf(gspca_dev, 0x8309, 2);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300580}
581
582/* rev 12a only */
583static void setgain(struct gspca_dev *gspca_dev)
584{
585 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300586
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300587 gspca_dev->usb_buf[0] = sd->gain;
588 gspca_dev->usb_buf[1] = 0;
589 reg_w_buf(gspca_dev, 0x8335, 2);
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300590}
591
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300592static void setautogain(struct gspca_dev *gspca_dev)
593{
594 struct sd *sd = (struct sd *) gspca_dev;
595
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300596 if (sd->autogain)
597 sd->ag_cnt = AG_CNT_START;
598 else
599 sd->ag_cnt = -1;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300600}
601
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300602static int sd_start_12a(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300603{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300604 struct usb_device *dev = gspca_dev->dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300605 int mode;
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300606 static const __u8 Reg8391[8] =
607 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300608
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300609 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300610 if (mode <= 1) {
611 /* Use compression on 320x240 and above */
612 reg_w_val(dev, 0x8500, 0x10 | mode);
613 } else {
614 /* I couldn't get the compression to work below 320x240
615 * Fortunately at these resolutions the bandwidth
616 * is sufficient to push raw frames at ~20fps */
617 reg_w_val(dev, 0x8500, mode);
618 } /* -- qq@kuku.eu.org */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300619
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300620 gspca_dev->usb_buf[0] = 0xaa;
621 gspca_dev->usb_buf[1] = 0x00;
622 reg_w_buf(gspca_dev, 0x8307, 2);
623 /* clock - lower 0x8X values lead to fps > 30 */
624 reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300625 /* 0x8f 0x85 0x27 clock */
626 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
627 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300628 memcpy(gspca_dev->usb_buf, Reg8391, 8);
629 reg_w_buf(gspca_dev, 0x8391, 8);
630 reg_w_buf(gspca_dev, 0x8390, 8);
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300631 setwhite(gspca_dev);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300632 setautogain(gspca_dev);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300633/* setgain(gspca_dev); */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300634 setexposure(gspca_dev);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300635 return 0;
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300636}
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300637static int sd_start_72a(struct gspca_dev *gspca_dev)
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300638{
639 struct usb_device *dev = gspca_dev->dev;
640 int Clck;
641 int mode;
642
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300643 write_vector(gspca_dev, rev72a_reset);
644 msleep(200);
645 write_vector(gspca_dev, rev72a_init_data1);
646 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
647
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300648 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
649 switch (mode) {
650 default:
651/* case 0:
652 case 1: */
653 Clck = 0x25;
654 break;
655 case 2:
656 Clck = 0x22;
657 break;
658 case 3:
659 Clck = 0x21;
660 break;
661 }
662 reg_w_val(dev, 0x8500, mode); /* mode */
663 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300664 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300665 setcontrast(gspca_dev);
666/* setbrightness(gspca_dev); * fixme: bad values */
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300667 setautogain(gspca_dev);
Jean-Francois Moine0dbc2c12009-01-16 16:24:28 -0300668 reg_w_val(dev, 0x8112, 0x10 | 0x20);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300669 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300670}
671
672static void sd_stopN(struct gspca_dev *gspca_dev)
673{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300674 struct sd *sd = (struct sd *) gspca_dev;
675
676 if (sd->chip_revision == Rev012A) {
677 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
678 } else {
679 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
680/* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
681 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300682}
683
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300684/* called on streamoff with alt 0 and on disconnect */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300685static void sd_stop0(struct gspca_dev *gspca_dev)
686{
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300687 struct sd *sd = (struct sd *) gspca_dev;
688
Jean-Francois Moine98522a72008-11-18 06:33:08 -0300689 if (!gspca_dev->present)
690 return;
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300691 if (sd->chip_revision == Rev012A) {
692 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
693 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
694 }
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300695/* reg_w_val(gspca_dev->dev, 0x8114, 0); */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300696}
697
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300698static void do_autogain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300699{
700 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300701 int expotimes;
702 int pixelclk;
703 int gainG;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300704 __u8 R, Gr, Gb, B;
705 int y;
706 __u8 luma_mean = 110;
707 __u8 luma_delta = 20;
708 __u8 spring = 4;
709
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300710 if (sd->ag_cnt < 0)
711 return;
712 if (--sd->ag_cnt >= 0)
713 return;
714 sd->ag_cnt = AG_CNT_START;
715
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300716 switch (sd->chip_revision) {
717 case Rev072A:
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300718 reg_r(gspca_dev, 0x8621, 1);
719 Gr = gspca_dev->usb_buf[0];
720 reg_r(gspca_dev, 0x8622, 1);
721 R = gspca_dev->usb_buf[0];
722 reg_r(gspca_dev, 0x8623, 1);
723 B = gspca_dev->usb_buf[0];
724 reg_r(gspca_dev, 0x8624, 1);
725 Gb = gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300726 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
727 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
728 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
729 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
730
731 if (y < luma_mean - luma_delta ||
732 y > luma_mean + luma_delta) {
733 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
734 pixelclk = 0x0800;
735 expotimes = expotimes & 0x07ff;
736 /* PDEBUG(D_PACK,
737 "Exposition Times 0x%03X Clock 0x%04X ",
738 expotimes,pixelclk); */
739 gainG = i2c_read(gspca_dev, 0x35, 0x10);
740 /* PDEBUG(D_PACK,
741 "reading Gain register %d", gainG); */
742
743 expotimes += (luma_mean - y) >> spring;
744 gainG += (luma_mean - y) / 50;
745 /* PDEBUG(D_PACK,
746 "compute expotimes %d gain %d",
747 expotimes,gainG); */
748
749 if (gainG > 0x3f)
750 gainG = 0x3f;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300751 else if (gainG < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300752 gainG = 3;
753 i2c_write(gspca_dev, gainG, 0x35);
754
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300755 if (expotimes > 0x0256)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300756 expotimes = 0x0256;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300757 else if (expotimes < 3)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300758 expotimes = 3;
759 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
760 }
761 break;
762 case Rev012A:
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300763 reg_r(gspca_dev, 0x8330, 2);
764 if (gspca_dev->usb_buf[1] > 0x08) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300765 gspca_dev->usb_buf[0] = ++sd->expo12a;
766 gspca_dev->usb_buf[1] = 0;
767 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300768 } else if (gspca_dev->usb_buf[1] < 0x02) {
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300769 gspca_dev->usb_buf[0] = --sd->expo12a;
770 gspca_dev->usb_buf[1] = 0;
771 reg_w_buf(gspca_dev, 0x8339, 2);
Jean-Francois Moined698dc62008-09-03 16:47:51 -0300772 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300773 break;
774 }
775}
776
777static void sd_pkt_scan(struct gspca_dev *gspca_dev,
778 struct gspca_frame *frame, /* target */
779 __u8 *data, /* isoc packet */
780 int len) /* iso packet length */
781{
Hans de Goede0fc23d22008-09-04 16:22:57 -0300782 struct sd *sd = (struct sd *) gspca_dev;
783
Jean-Francois Moine576ed7b2009-01-16 08:57:28 -0300784 len--;
785 switch (*data++) { /* sequence number */
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300786 case 0: /* start of frame */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300787 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
788 data, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300789 if (data[1] & 0x10) {
790 /* compressed bayer */
791 gspca_frame_add(gspca_dev, FIRST_PACKET,
792 frame, data, len);
793 } else {
Hans de Goede54ab92c2008-07-03 11:20:58 -0300794 /* raw bayer (with a header, which we skip) */
Hans de Goede0fc23d22008-09-04 16:22:57 -0300795 if (sd->chip_revision == Rev012A) {
796 data += 20;
797 len -= 20;
798 } else {
799 data += 16;
800 len -= 16;
801 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300802 gspca_frame_add(gspca_dev, FIRST_PACKET,
803 frame, data, len);
804 }
805 return;
Jean-Francois Moine35dc1b42008-12-17 14:34:53 -0300806 case 0xff: /* drop (empty mpackets) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300807 return;
808 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300809 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
810}
811
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300812/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300813static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
814{
815 struct sd *sd = (struct sd *) gspca_dev;
816
817 sd->brightness = val;
818 if (gspca_dev->streaming)
819 setbrightness(gspca_dev);
820 return 0;
821}
822
823static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
824{
825 struct sd *sd = (struct sd *) gspca_dev;
826
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300827 *val = sd->brightness;
828 return 0;
829}
830
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300831/* rev 72a only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300832static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
833{
834 struct sd *sd = (struct sd *) gspca_dev;
835
836 sd->contrast = val;
837 if (gspca_dev->streaming)
838 setcontrast(gspca_dev);
839 return 0;
840}
841
842static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
843{
844 struct sd *sd = (struct sd *) gspca_dev;
845
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300846 *val = sd->contrast;
847 return 0;
848}
849
850static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
851{
852 struct sd *sd = (struct sd *) gspca_dev;
853
854 sd->autogain = val;
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -0300855 if (gspca_dev->streaming)
856 setautogain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300857 return 0;
858}
859
860static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
861{
862 struct sd *sd = (struct sd *) gspca_dev;
863
864 *val = sd->autogain;
865 return 0;
866}
867
Jean-Francois Moine6c9d3c52008-09-03 16:47:30 -0300868static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
869{
870 struct sd *sd = (struct sd *) gspca_dev;
871
872 sd->white = val;
873 if (gspca_dev->streaming)
874 setwhite(gspca_dev);
875 return 0;
876}
877
878static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
879{
880 struct sd *sd = (struct sd *) gspca_dev;
881
882 *val = sd->white;
883 return 0;
884}
885
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300886/* rev12a only */
887static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
888{
889 struct sd *sd = (struct sd *) gspca_dev;
890
891 sd->exposure = val;
892 if (gspca_dev->streaming)
893 setexposure(gspca_dev);
894 return 0;
895}
896
897static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
898{
899 struct sd *sd = (struct sd *) gspca_dev;
900
901 *val = sd->exposure;
902 return 0;
903}
904
905/* rev12a only */
906static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
907{
908 struct sd *sd = (struct sd *) gspca_dev;
909
910 sd->gain = val;
911 if (gspca_dev->streaming)
912 setgain(gspca_dev);
913 return 0;
914}
915
916static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
917{
918 struct sd *sd = (struct sd *) gspca_dev;
919
920 *val = sd->gain;
921 return 0;
922}
923
924/* control tables */
925static struct ctrl sd_ctrls_12a[] = {
926 {
927 {
928 .id = V4L2_CID_DO_WHITE_BALANCE,
929 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab695ebd12008-10-04 14:18:37 -0300930 .name = "White Balance",
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300931 .minimum = WHITE_MIN,
932 .maximum = WHITE_MAX,
933 .step = 1,
934 .default_value = WHITE_DEF,
935 },
936 .set = sd_setwhite,
937 .get = sd_getwhite,
938 },
939 {
940 {
941 .id = V4L2_CID_EXPOSURE,
942 .type = V4L2_CTRL_TYPE_INTEGER,
943 .name = "Exposure",
944 .minimum = EXPOSURE_MIN,
945 .maximum = EXPOSURE_MAX,
946 .step = 1,
947 .default_value = EXPOSURE_DEF,
948 },
949 .set = sd_setexposure,
950 .get = sd_getexposure,
951 },
952 {
953 {
954 .id = V4L2_CID_AUTOGAIN,
955 .type = V4L2_CTRL_TYPE_BOOLEAN,
956 .name = "Auto Gain",
957 .minimum = AUTOGAIN_MIN,
958 .maximum = AUTOGAIN_MAX,
959 .step = 1,
960 .default_value = AUTOGAIN_DEF,
961 },
962 .set = sd_setautogain,
963 .get = sd_getautogain,
964 },
965 {
966 {
967 .id = V4L2_CID_GAIN,
968 .type = V4L2_CTRL_TYPE_INTEGER,
969 .name = "Gain",
970 .minimum = GAIN_MIN,
971 .maximum = GAIN_MAX,
972 .step = 1,
973 .default_value = GAIN_DEF,
974 },
975 .set = sd_setgain,
976 .get = sd_getgain,
977 },
978};
979
980static struct ctrl sd_ctrls_72a[] = {
981 {
Jean-Francois Moine5b7ed282008-12-18 12:38:15 -0300982 {
983 .id = V4L2_CID_DO_WHITE_BALANCE,
984 .type = V4L2_CTRL_TYPE_INTEGER,
985 .name = "White Balance",
986 .minimum = WHITE_MIN,
987 .maximum = WHITE_MAX,
988 .step = 1,
989 .default_value = WHITE_DEF,
990 },
991 .set = sd_setwhite,
992 .get = sd_getwhite,
993 },
994 {
Jean-Francois Moine7879d452008-09-03 16:47:32 -0300995 {
996 .id = V4L2_CID_BRIGHTNESS,
997 .type = V4L2_CTRL_TYPE_INTEGER,
998 .name = "Brightness",
999 .minimum = BRIGHTNESS_MIN,
1000 .maximum = BRIGHTNESS_MAX,
1001 .step = 1,
1002 .default_value = BRIGHTNESS_DEF,
1003 },
1004 .set = sd_setbrightness,
1005 .get = sd_getbrightness,
1006 },
1007 {
1008 {
1009 .id = V4L2_CID_CONTRAST,
1010 .type = V4L2_CTRL_TYPE_INTEGER,
1011 .name = "Contrast",
1012 .minimum = CONTRAST_MIN,
1013 .maximum = CONTRAST_MAX,
1014 .step = 1,
1015 .default_value = CONTRAST_DEF,
1016 },
1017 .set = sd_setcontrast,
1018 .get = sd_getcontrast,
1019 },
1020 {
1021 {
1022 .id = V4L2_CID_AUTOGAIN,
1023 .type = V4L2_CTRL_TYPE_BOOLEAN,
1024 .name = "Auto Gain",
1025 .minimum = AUTOGAIN_MIN,
1026 .maximum = AUTOGAIN_MAX,
1027 .step = 1,
1028 .default_value = AUTOGAIN_DEF,
1029 },
1030 .set = sd_setautogain,
1031 .get = sd_getautogain,
1032 },
1033};
1034
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001035/* sub-driver description */
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001036static const struct sd_desc sd_desc_12a = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001037 .name = MODULE_NAME,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001038 .ctrls = sd_ctrls_12a,
1039 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001040 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001041 .init = sd_init_12a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001042 .start = sd_start_12a,
1043 .stopN = sd_stopN,
1044 .stop0 = sd_stop0,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001045 .pkt_scan = sd_pkt_scan,
1046/* .dq_callback = do_autogain, * fixme */
1047};
1048static const struct sd_desc sd_desc_72a = {
1049 .name = MODULE_NAME,
1050 .ctrls = sd_ctrls_72a,
1051 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1052 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001053 .init = sd_init_72a,
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001054 .start = sd_start_72a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001055 .stopN = sd_stopN,
1056 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001057 .pkt_scan = sd_pkt_scan,
Jean-Francois Moinecebf3b62008-08-03 07:52:53 -03001058 .dq_callback = do_autogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001059};
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001060static const struct sd_desc *sd_desc[2] = {
1061 &sd_desc_12a,
1062 &sd_desc_72a
1063};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001064
1065/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001066static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine87581aa2008-07-26 14:30:01 -03001067 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1068 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1069 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1070 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1071 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1072 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1073 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1074 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1075 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1076 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1077 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1078 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1079 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1080 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1081 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001082 {}
1083};
1084
1085MODULE_DEVICE_TABLE(usb, device_table);
1086
1087/* -- device connect -- */
1088static int sd_probe(struct usb_interface *intf,
1089 const struct usb_device_id *id)
1090{
Jean-Francois Moine7879d452008-09-03 16:47:32 -03001091 return gspca_dev_probe(intf, id,
1092 sd_desc[id->driver_info],
1093 sizeof(struct sd),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001094 THIS_MODULE);
1095}
1096
1097static struct usb_driver sd_driver = {
1098 .name = MODULE_NAME,
1099 .id_table = device_table,
1100 .probe = sd_probe,
1101 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001102#ifdef CONFIG_PM
1103 .suspend = gspca_suspend,
1104 .resume = gspca_resume,
1105#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001106};
1107
1108/* -- module insert / remove -- */
1109static int __init sd_mod_init(void)
1110{
Alexey Klimovf69e9522009-01-01 13:02:07 -03001111 int ret;
1112 ret = usb_register(&sd_driver);
1113 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -03001114 return ret;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001115 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001116 return 0;
1117}
1118static void __exit sd_mod_exit(void)
1119{
1120 usb_deregister(&sd_driver);
1121 PDEBUG(D_PROBE, "deregistered");
1122}
1123
1124module_init(sd_mod_init);
1125module_exit(sd_mod_exit);