blob: 19f0cd892dba88c937085b489a30e8f8a97bc7d7 [file] [log] [blame]
Marton Nemeth1408b842009-11-02 08:13:21 -03001/*
2 * Pixart PAC7302 library
3 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
4 *
5 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
6 *
Jean-Francois Moinecc2f82c2010-01-28 16:35:40 -03007 * Separated from Pixart PAC7311 library by Márton Németh
Márton Némethaed6f1b2010-01-28 16:33:38 -03008 * Camera button input handling by Márton Németh <nm127@freemail.hu>
9 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
Marton Nemeth1408b842009-11-02 08:13:21 -030010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26/* Some documentation about various registers as determined by trial and error.
27 When the register addresses differ between the 7202 and the 7311 the 2
28 different addresses are written as 7302addr/7311addr, when one of the 2
29 addresses is a - sign that register description is not valid for the
30 matching IC.
31
32 Register page 1:
33
34 Address Description
35 -/0x08 Unknown compressor related, must always be 8 except when not
36 in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
37 -/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
38 bits 345 seem to toggle per color gains on/off (inverted)
39 0x78 Global control, bit 6 controls the LED (inverted)
40 -/0x80 JPEG compression ratio ? Best not touched
41
42 Register page 3/4:
43
44 Address Description
45 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
46 the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
47 -/0x0f Master gain 1-245, low value = high gain
48 0x10/- Master gain 0-31
49 -/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
50 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
51 -/0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
52 completely disable the analog amplification block. Set to 0x68
53 for max gain, 0x14 for minimal gain.
Márton Németh265a8092009-11-07 15:15:56 -030054
55 The registers are accessed in the following functions:
56
57 Page | Register | Function
58 -----+------------+---------------------------------------------------
59 0 | 0x0f..0x20 | setcolors()
60 0 | 0xa2..0xab | setbrightcont()
61 0 | 0xc5 | setredbalance()
Marton Nemeth23fbee62009-11-08 04:35:12 -030062 0 | 0xc6 | setwhitebalance()
Márton Németh265a8092009-11-07 15:15:56 -030063 0 | 0xc7 | setbluebalance()
64 0 | 0xdc | setbrightcont(), setcolors()
65 3 | 0x02 | setexposure()
66 3 | 0x10 | setgain()
67 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
68 3 | 0x21 | sethvflip()
Marton Nemeth1408b842009-11-02 08:13:21 -030069*/
70
71#define MODULE_NAME "pac7302"
72
Márton Némethaed6f1b2010-01-28 16:33:38 -030073#include <linux/input.h>
Márton Németh6763cc02009-11-09 07:10:46 -030074#include <media/v4l2-chip-ident.h>
Marton Nemeth1408b842009-11-02 08:13:21 -030075#include "gspca.h"
76
77MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
78MODULE_DESCRIPTION("Pixart PAC7302");
79MODULE_LICENSE("GPL");
80
81/* specific webcam descriptor for pac7302 */
82struct sd {
83 struct gspca_dev gspca_dev; /* !! must be the first item */
84
85 unsigned char brightness;
86 unsigned char contrast;
87 unsigned char colors;
Marton Nemeth23fbee62009-11-08 04:35:12 -030088 unsigned char white_balance;
Márton Németh265a8092009-11-07 15:15:56 -030089 unsigned char red_balance;
90 unsigned char blue_balance;
Marton Nemeth1408b842009-11-02 08:13:21 -030091 unsigned char gain;
92 unsigned char exposure;
93 unsigned char autogain;
94 __u8 hflip;
95 __u8 vflip;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -030096 u8 flags;
97#define FL_HFLIP 0x01 /* mirrored by default */
98#define FL_VFLIP 0x02 /* vertical flipped by default */
Marton Nemeth1408b842009-11-02 08:13:21 -030099
100 u8 sof_read;
101 u8 autogain_ignore_frames;
102
103 atomic_t avg_lum;
104};
105
106/* V4L2 controls supported by the driver */
107static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
108static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
109static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
110static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
111static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
112static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300113static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val);
114static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val);
Márton Németh265a8092009-11-07 15:15:56 -0300115static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val);
116static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val);
117static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val);
118static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val);
Marton Nemeth1408b842009-11-02 08:13:21 -0300119static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
120static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
121static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
122static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
123static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
124static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
125static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
126static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
127static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
128static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
129
Marton Nemeth7e64dc42009-12-30 09:12:41 -0300130static const struct ctrl sd_ctrls[] = {
Marton Nemeth1408b842009-11-02 08:13:21 -0300131/* This control is pac7302 only */
132 {
133 {
134 .id = V4L2_CID_BRIGHTNESS,
135 .type = V4L2_CTRL_TYPE_INTEGER,
136 .name = "Brightness",
137 .minimum = 0,
138#define BRIGHTNESS_MAX 0x20
139 .maximum = BRIGHTNESS_MAX,
140 .step = 1,
141#define BRIGHTNESS_DEF 0x10
142 .default_value = BRIGHTNESS_DEF,
143 },
144 .set = sd_setbrightness,
145 .get = sd_getbrightness,
146 },
147/* This control is for both the 7302 and the 7311 */
148 {
149 {
150 .id = V4L2_CID_CONTRAST,
151 .type = V4L2_CTRL_TYPE_INTEGER,
152 .name = "Contrast",
153 .minimum = 0,
154#define CONTRAST_MAX 255
155 .maximum = CONTRAST_MAX,
156 .step = 1,
157#define CONTRAST_DEF 127
158 .default_value = CONTRAST_DEF,
159 },
160 .set = sd_setcontrast,
161 .get = sd_getcontrast,
162 },
163/* This control is pac7302 only */
164 {
165 {
166 .id = V4L2_CID_SATURATION,
167 .type = V4L2_CTRL_TYPE_INTEGER,
168 .name = "Saturation",
169 .minimum = 0,
170#define COLOR_MAX 255
171 .maximum = COLOR_MAX,
172 .step = 1,
173#define COLOR_DEF 127
174 .default_value = COLOR_DEF,
175 },
176 .set = sd_setcolors,
177 .get = sd_getcolors,
178 },
Márton Németh265a8092009-11-07 15:15:56 -0300179 {
180 {
Marton Nemeth23fbee62009-11-08 04:35:12 -0300181 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
182 .type = V4L2_CTRL_TYPE_INTEGER,
183 .name = "White Balance",
184 .minimum = 0,
185 .maximum = 255,
186 .step = 1,
187#define WHITEBALANCE_DEF 4
188 .default_value = WHITEBALANCE_DEF,
189 },
190 .set = sd_setwhitebalance,
191 .get = sd_getwhitebalance,
192 },
193 {
194 {
Márton Németh265a8092009-11-07 15:15:56 -0300195 .id = V4L2_CID_RED_BALANCE,
196 .type = V4L2_CTRL_TYPE_INTEGER,
197 .name = "Red",
198 .minimum = 0,
199 .maximum = 3,
200 .step = 1,
201#define REDBALANCE_DEF 1
202 .default_value = REDBALANCE_DEF,
203 },
204 .set = sd_setredbalance,
205 .get = sd_getredbalance,
206 },
207 {
208 {
209 .id = V4L2_CID_BLUE_BALANCE,
210 .type = V4L2_CTRL_TYPE_INTEGER,
211 .name = "Blue",
212 .minimum = 0,
213 .maximum = 3,
214 .step = 1,
215#define BLUEBALANCE_DEF 1
216 .default_value = BLUEBALANCE_DEF,
217 },
218 .set = sd_setbluebalance,
219 .get = sd_getbluebalance,
220 },
Marton Nemeth1408b842009-11-02 08:13:21 -0300221/* All controls below are for both the 7302 and the 7311 */
222 {
223 {
224 .id = V4L2_CID_GAIN,
225 .type = V4L2_CTRL_TYPE_INTEGER,
226 .name = "Gain",
227 .minimum = 0,
228#define GAIN_MAX 255
229 .maximum = GAIN_MAX,
230 .step = 1,
231#define GAIN_DEF 127
232#define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
233 .default_value = GAIN_DEF,
234 },
235 .set = sd_setgain,
236 .get = sd_getgain,
237 },
238 {
239 {
240 .id = V4L2_CID_EXPOSURE,
241 .type = V4L2_CTRL_TYPE_INTEGER,
242 .name = "Exposure",
243 .minimum = 0,
244#define EXPOSURE_MAX 255
245 .maximum = EXPOSURE_MAX,
246 .step = 1,
247#define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
248#define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
249 .default_value = EXPOSURE_DEF,
250 },
251 .set = sd_setexposure,
252 .get = sd_getexposure,
253 },
254 {
255 {
256 .id = V4L2_CID_AUTOGAIN,
257 .type = V4L2_CTRL_TYPE_BOOLEAN,
258 .name = "Auto Gain",
259 .minimum = 0,
260 .maximum = 1,
261 .step = 1,
262#define AUTOGAIN_DEF 1
263 .default_value = AUTOGAIN_DEF,
264 },
265 .set = sd_setautogain,
266 .get = sd_getautogain,
267 },
268 {
269 {
270 .id = V4L2_CID_HFLIP,
271 .type = V4L2_CTRL_TYPE_BOOLEAN,
272 .name = "Mirror",
273 .minimum = 0,
274 .maximum = 1,
275 .step = 1,
276#define HFLIP_DEF 0
277 .default_value = HFLIP_DEF,
278 },
279 .set = sd_sethflip,
280 .get = sd_gethflip,
281 },
282 {
283 {
284 .id = V4L2_CID_VFLIP,
285 .type = V4L2_CTRL_TYPE_BOOLEAN,
286 .name = "Vflip",
287 .minimum = 0,
288 .maximum = 1,
289 .step = 1,
290#define VFLIP_DEF 0
291 .default_value = VFLIP_DEF,
292 },
293 .set = sd_setvflip,
294 .get = sd_getvflip,
295 },
296};
297
298static const struct v4l2_pix_format vga_mode[] = {
299 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
300 .bytesperline = 640,
301 .sizeimage = 640 * 480 * 3 / 8 + 590,
302 .colorspace = V4L2_COLORSPACE_JPEG,
303 .priv = 0},
304};
305
306#define LOAD_PAGE3 255
Marton Nemeth1408b842009-11-02 08:13:21 -0300307#define END_OF_SEQUENCE 0
308
309/* pac 7302 */
310static const __u8 init_7302[] = {
311/* index,value */
312 0xff, 0x01, /* page 1 */
313 0x78, 0x00, /* deactivate */
314 0xff, 0x01,
315 0x78, 0x40, /* led off */
316};
317static const __u8 start_7302[] = {
318/* index, len, [value]* */
319 0xff, 1, 0x00, /* page 0 */
320 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
321 0x00, 0x00, 0x00, 0x00,
322 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
323 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
324 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
325 0x26, 2, 0xaa, 0xaa,
326 0x2e, 1, 0x31,
327 0x38, 1, 0x01,
328 0x3a, 3, 0x14, 0xff, 0x5a,
329 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
330 0x00, 0x54, 0x11,
331 0x55, 1, 0x00,
332 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
333 0x6b, 1, 0x00,
334 0x6e, 3, 0x08, 0x06, 0x00,
335 0x72, 3, 0x00, 0xff, 0x00,
336 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
337 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
338 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
339 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
340 0xd2, 0xeb,
341 0xaf, 1, 0x02,
342 0xb5, 2, 0x08, 0x08,
343 0xb8, 2, 0x08, 0x88,
344 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
345 0xcc, 1, 0x00,
346 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
347 0xc1, 0xd7, 0xec,
348 0xdc, 1, 0x01,
349 0xff, 1, 0x01, /* page 1 */
350 0x12, 3, 0x02, 0x00, 0x01,
351 0x3e, 2, 0x00, 0x00,
352 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
353 0x7c, 1, 0x00,
354 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
355 0x02, 0x00,
356 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
357 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
358 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
359 0xd8, 1, 0x01,
360 0xdb, 2, 0x00, 0x01,
361 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
362 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
363 0xeb, 1, 0x00,
364 0xff, 1, 0x02, /* page 2 */
365 0x22, 1, 0x00,
366 0xff, 1, 0x03, /* page 3 */
367 0, LOAD_PAGE3, /* load the page 3 */
368 0x11, 1, 0x01,
369 0xff, 1, 0x02, /* page 2 */
370 0x13, 1, 0x00,
371 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
372 0x27, 2, 0x14, 0x0c,
373 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
374 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
375 0x6e, 1, 0x08,
376 0xff, 1, 0x01, /* page 1 */
377 0x78, 1, 0x00,
378 0, END_OF_SEQUENCE /* end of sequence */
379};
380
381#define SKIP 0xaa
382/* page 3 - the value SKIP says skip the index - see reg_w_page() */
383static const __u8 page3_7302[] = {
384 0x90, 0x40, 0x03, 0x50, 0xc2, 0x01, 0x14, 0x16,
385 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
386 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
388 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
389 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
390 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
391 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
Jean-Francois Moinecdf955c2010-01-11 15:06:12 -0300393 SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
Marton Nemeth1408b842009-11-02 08:13:21 -0300394 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
398 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
399 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
400 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
401 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
402 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
403 0x00
404};
405
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300406static void reg_w_buf(struct gspca_dev *gspca_dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300407 __u8 index,
408 const char *buffer, int len)
409{
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300410 int ret;
411
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300412 if (gspca_dev->usb_err < 0)
413 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300414 memcpy(gspca_dev->usb_buf, buffer, len);
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300415 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300416 usb_sndctrlpipe(gspca_dev->dev, 0),
417 1, /* request */
418 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
419 0, /* value */
420 index, gspca_dev->usb_buf, len,
421 500);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300422 if (ret < 0) {
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300423 PDEBUG(D_ERR, "reg_w_buf(): "
424 "Failed to write registers to index 0x%x, error %i",
425 index, ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300426 gspca_dev->usb_err = ret;
427 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300428}
429
430
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300431static void reg_w(struct gspca_dev *gspca_dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300432 __u8 index,
433 __u8 value)
434{
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300435 int ret;
436
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300437 if (gspca_dev->usb_err < 0)
438 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300439 gspca_dev->usb_buf[0] = value;
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300440 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300441 usb_sndctrlpipe(gspca_dev->dev, 0),
442 0, /* request */
443 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
444 0, index, gspca_dev->usb_buf, 1,
445 500);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300446 if (ret < 0) {
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300447 PDEBUG(D_ERR, "reg_w(): "
448 "Failed to write register to index 0x%x, value 0x%x, error %i",
449 index, value, ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300450 gspca_dev->usb_err = ret;
451 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300452}
453
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300454static void reg_w_seq(struct gspca_dev *gspca_dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300455 const __u8 *seq, int len)
456{
457 while (--len >= 0) {
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300458 reg_w(gspca_dev, seq[0], seq[1]);
Marton Nemeth1408b842009-11-02 08:13:21 -0300459 seq += 2;
460 }
461}
462
463/* load the beginning of a page */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300464static void reg_w_page(struct gspca_dev *gspca_dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300465 const __u8 *page, int len)
466{
467 int index;
Márton Némethb1784b32009-11-07 05:52:02 -0300468 int ret = 0;
Marton Nemeth1408b842009-11-02 08:13:21 -0300469
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300470 if (gspca_dev->usb_err < 0)
471 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300472 for (index = 0; index < len; index++) {
473 if (page[index] == SKIP) /* skip this index */
474 continue;
475 gspca_dev->usb_buf[0] = page[index];
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300476 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300477 usb_sndctrlpipe(gspca_dev->dev, 0),
478 0, /* request */
479 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
480 0, index, gspca_dev->usb_buf, 1,
481 500);
Márton Némethb1784b32009-11-07 05:52:02 -0300482 if (ret < 0) {
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300483 PDEBUG(D_ERR, "reg_w_page(): "
484 "Failed to write register to index 0x%x, "
485 "value 0x%x, error %i",
486 index, page[index], ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300487 gspca_dev->usb_err = ret;
Márton Némethb1784b32009-11-07 05:52:02 -0300488 break;
489 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300490 }
491}
492
493/* output a variable sequence */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300494static void reg_w_var(struct gspca_dev *gspca_dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300495 const __u8 *seq,
Jean-Francois Moine23a5de22010-01-13 08:30:30 -0300496 const __u8 *page3, unsigned int page3_len)
Marton Nemeth1408b842009-11-02 08:13:21 -0300497{
498 int index, len;
499
500 for (;;) {
501 index = *seq++;
502 len = *seq++;
503 switch (len) {
504 case END_OF_SEQUENCE:
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300505 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300506 case LOAD_PAGE3:
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300507 reg_w_page(gspca_dev, page3, page3_len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300508 break;
509 default:
510 if (len > USB_BUF_SZ) {
511 PDEBUG(D_ERR|D_STREAM,
512 "Incorrect variable sequence");
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300513 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300514 }
515 while (len > 0) {
516 if (len < 8) {
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300517 reg_w_buf(gspca_dev,
Márton Némethb1784b32009-11-07 05:52:02 -0300518 index, seq, len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300519 seq += len;
520 break;
521 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300522 reg_w_buf(gspca_dev, index, seq, 8);
Marton Nemeth1408b842009-11-02 08:13:21 -0300523 seq += 8;
524 index += 8;
525 len -= 8;
526 }
527 }
528 }
529 /* not reached */
530}
531
532/* this function is called at probe time for pac7302 */
533static int sd_config(struct gspca_dev *gspca_dev,
534 const struct usb_device_id *id)
535{
536 struct sd *sd = (struct sd *) gspca_dev;
537 struct cam *cam;
538
539 cam = &gspca_dev->cam;
540
541 PDEBUG(D_CONF, "Find Sensor PAC7302");
542 cam->cam_mode = vga_mode; /* only 640x480 */
543 cam->nmodes = ARRAY_SIZE(vga_mode);
544
545 sd->brightness = BRIGHTNESS_DEF;
546 sd->contrast = CONTRAST_DEF;
547 sd->colors = COLOR_DEF;
Marton Nemeth23fbee62009-11-08 04:35:12 -0300548 sd->white_balance = WHITEBALANCE_DEF;
Márton Németh265a8092009-11-07 15:15:56 -0300549 sd->red_balance = REDBALANCE_DEF;
550 sd->blue_balance = BLUEBALANCE_DEF;
Marton Nemeth1408b842009-11-02 08:13:21 -0300551 sd->gain = GAIN_DEF;
552 sd->exposure = EXPOSURE_DEF;
553 sd->autogain = AUTOGAIN_DEF;
554 sd->hflip = HFLIP_DEF;
555 sd->vflip = VFLIP_DEF;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300556 sd->flags = id->driver_info;
Marton Nemeth1408b842009-11-02 08:13:21 -0300557 return 0;
558}
559
560/* This function is used by pac7302 only */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300561static void setbrightcont(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300562{
563 struct sd *sd = (struct sd *) gspca_dev;
564 int i, v;
565 static const __u8 max[10] =
566 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
567 0xd4, 0xec};
568 static const __u8 delta[10] =
569 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
570 0x11, 0x0b};
571
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300572 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300573 for (i = 0; i < 10; i++) {
574 v = max[i];
575 v += (sd->brightness - BRIGHTNESS_MAX)
576 * 150 / BRIGHTNESS_MAX; /* 200 ? */
577 v -= delta[i] * sd->contrast / CONTRAST_MAX;
578 if (v < 0)
579 v = 0;
580 else if (v > 0xff)
581 v = 0xff;
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300582 reg_w(gspca_dev, 0xa2 + i, v);
Marton Nemeth1408b842009-11-02 08:13:21 -0300583 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300584 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300585}
586
587/* This function is used by pac7302 only */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300588static void setcolors(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300589{
590 struct sd *sd = (struct sd *) gspca_dev;
591 int i, v;
592 static const int a[9] =
593 {217, -212, 0, -101, 170, -67, -38, -315, 355};
594 static const int b[9] =
595 {19, 106, 0, 19, 106, 1, 19, 106, 1};
596
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300597 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
598 reg_w(gspca_dev, 0x11, 0x01);
599 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300600 for (i = 0; i < 9; i++) {
601 v = a[i] * sd->colors / COLOR_MAX + b[i];
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300602 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
603 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
Marton Nemeth1408b842009-11-02 08:13:21 -0300604 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300605 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300606 PDEBUG(D_CONF|D_STREAM, "color: %i", sd->colors);
607}
608
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300609static void setwhitebalance(struct gspca_dev *gspca_dev)
Marton Nemeth23fbee62009-11-08 04:35:12 -0300610{
611 struct sd *sd = (struct sd *) gspca_dev;
Marton Nemeth23fbee62009-11-08 04:35:12 -0300612
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300613 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
614 reg_w(gspca_dev, 0xc6, sd->white_balance);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300615
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300616 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300617 PDEBUG(D_CONF|D_STREAM, "white_balance: %i", sd->white_balance);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300618}
619
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300620static void setredbalance(struct gspca_dev *gspca_dev)
Márton Németh265a8092009-11-07 15:15:56 -0300621{
622 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300623
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300624 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
625 reg_w(gspca_dev, 0xc5, sd->red_balance);
Márton Németh265a8092009-11-07 15:15:56 -0300626
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300627 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh265a8092009-11-07 15:15:56 -0300628 PDEBUG(D_CONF|D_STREAM, "red_balance: %i", sd->red_balance);
Márton Németh265a8092009-11-07 15:15:56 -0300629}
630
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300631static void setbluebalance(struct gspca_dev *gspca_dev)
Márton Németh265a8092009-11-07 15:15:56 -0300632{
633 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300634
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300635 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
636 reg_w(gspca_dev, 0xc7, sd->blue_balance);
Márton Németh265a8092009-11-07 15:15:56 -0300637
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300638 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh265a8092009-11-07 15:15:56 -0300639 PDEBUG(D_CONF|D_STREAM, "blue_balance: %i", sd->blue_balance);
Márton Németh265a8092009-11-07 15:15:56 -0300640}
641
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300642static void setgain(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300643{
644 struct sd *sd = (struct sd *) gspca_dev;
645
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300646 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
647 reg_w(gspca_dev, 0x10, sd->gain >> 3);
Marton Nemeth1408b842009-11-02 08:13:21 -0300648
649 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300650 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300651}
652
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300653static void setexposure(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300654{
655 struct sd *sd = (struct sd *) gspca_dev;
656 __u8 reg;
657
658 /* register 2 of frame 3/4 contains the clock divider configuring the
659 no fps according to the formula: 60 / reg. sd->exposure is the
660 desired exposure time in ms. */
661 reg = 120 * sd->exposure / 1000;
662 if (reg < 2)
663 reg = 2;
664 else if (reg > 63)
665 reg = 63;
666
667 /* On the pac7302 reg2 MUST be a multiple of 3, so round it to
668 the nearest multiple of 3, except when between 6 and 12? */
669 if (reg < 6 || reg > 12)
670 reg = ((reg + 1) / 3) * 3;
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300671 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
672 reg_w(gspca_dev, 0x02, reg);
Marton Nemeth1408b842009-11-02 08:13:21 -0300673
674 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300675 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300676}
677
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300678static void sethvflip(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300679{
680 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300681 u8 data, hflip, vflip;
682
683 hflip = sd->hflip;
684 if (sd->flags & FL_HFLIP)
685 hflip = !hflip;
686 vflip = sd->vflip;
687 if (sd->flags & FL_VFLIP)
688 vflip = !vflip;
Marton Nemeth1408b842009-11-02 08:13:21 -0300689
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300690 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300691 data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300692 reg_w(gspca_dev, 0x21, data);
693
Marton Nemeth1408b842009-11-02 08:13:21 -0300694 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300695 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300696}
697
698/* this function is called at probe and resume time for pac7302 */
699static int sd_init(struct gspca_dev *gspca_dev)
700{
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300701 reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
702 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300703}
704
705static int sd_start(struct gspca_dev *gspca_dev)
706{
707 struct sd *sd = (struct sd *) gspca_dev;
708
709 sd->sof_read = 0;
710
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300711 reg_w_var(gspca_dev, start_7302,
Jean-Francois Moine23a5de22010-01-13 08:30:30 -0300712 page3_7302, sizeof(page3_7302));
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300713 setbrightcont(gspca_dev);
714 setcolors(gspca_dev);
715 setwhitebalance(gspca_dev);
716 setredbalance(gspca_dev);
717 setbluebalance(gspca_dev);
718 setgain(gspca_dev);
719 setexposure(gspca_dev);
720 sethvflip(gspca_dev);
Marton Nemeth1408b842009-11-02 08:13:21 -0300721
722 /* only resolution 640x480 is supported for pac7302 */
723
724 sd->sof_read = 0;
725 sd->autogain_ignore_frames = 0;
726 atomic_set(&sd->avg_lum, -1);
727
728 /* start stream */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300729 reg_w(gspca_dev, 0xff, 0x01);
730 reg_w(gspca_dev, 0x78, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300731
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300732 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300733}
734
735static void sd_stopN(struct gspca_dev *gspca_dev)
736{
Márton Némethb1784b32009-11-07 05:52:02 -0300737
Márton Németh67c98f72009-11-07 05:45:33 -0300738 /* stop stream */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300739 reg_w(gspca_dev, 0xff, 0x01);
740 reg_w(gspca_dev, 0x78, 0x00);
Marton Nemeth1408b842009-11-02 08:13:21 -0300741}
742
743/* called on streamoff with alt 0 and on disconnect for pac7302 */
744static void sd_stop0(struct gspca_dev *gspca_dev)
745{
746 if (!gspca_dev->present)
747 return;
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300748 reg_w(gspca_dev, 0xff, 0x01);
749 reg_w(gspca_dev, 0x78, 0x40);
Marton Nemeth1408b842009-11-02 08:13:21 -0300750}
751
752/* Include pac common sof detection functions */
753#include "pac_common.h"
754
755static void do_autogain(struct gspca_dev *gspca_dev)
756{
757 struct sd *sd = (struct sd *) gspca_dev;
758 int avg_lum = atomic_read(&sd->avg_lum);
759 int desired_lum, deadzone;
760
761 if (avg_lum == -1)
762 return;
763
764 desired_lum = 270 + sd->brightness * 4;
765 /* Hack hack, with the 7202 the first exposure step is
766 pretty large, so if we're about to make the first
767 exposure increase make the deadzone large to avoid
768 oscilating */
769 if (desired_lum > avg_lum && sd->gain == GAIN_DEF &&
770 sd->exposure > EXPOSURE_DEF &&
771 sd->exposure < 42)
772 deadzone = 90;
773 else
774 deadzone = 30;
775
776 if (sd->autogain_ignore_frames > 0)
777 sd->autogain_ignore_frames--;
778 else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
779 deadzone, GAIN_KNEE, EXPOSURE_KNEE))
780 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
781}
782
783/* JPEG header, part 1 */
784static const unsigned char pac_jpeg_header1[] = {
785 0xff, 0xd8, /* SOI: Start of Image */
786
787 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
788 0x00, 0x11, /* length = 17 bytes (including this length field) */
789 0x08 /* Precision: 8 */
790 /* 2 bytes is placed here: number of image lines */
791 /* 2 bytes is placed here: samples per line */
792};
793
794/* JPEG header, continued */
795static const unsigned char pac_jpeg_header2[] = {
796 0x03, /* Number of image components: 3 */
797 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
798 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
799 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
800
801 0xff, 0xda, /* SOS: Start Of Scan */
802 0x00, 0x0c, /* length = 12 bytes (including this length field) */
803 0x03, /* number of components: 3 */
804 0x01, 0x00, /* selector 1, table 0x00 */
805 0x02, 0x11, /* selector 2, table 0x11 */
806 0x03, 0x11, /* selector 3, table 0x11 */
807 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
808 0x00 /* Successive approximation: 0 */
809};
810
811static void pac_start_frame(struct gspca_dev *gspca_dev,
812 struct gspca_frame *frame,
813 __u16 lines, __u16 samples_per_line)
814{
815 unsigned char tmpbuf[4];
816
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300817 gspca_frame_add(gspca_dev, FIRST_PACKET,
Marton Nemeth1408b842009-11-02 08:13:21 -0300818 pac_jpeg_header1, sizeof(pac_jpeg_header1));
819
820 tmpbuf[0] = lines >> 8;
821 tmpbuf[1] = lines & 0xff;
822 tmpbuf[2] = samples_per_line >> 8;
823 tmpbuf[3] = samples_per_line & 0xff;
824
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300825 gspca_frame_add(gspca_dev, INTER_PACKET,
Marton Nemeth1408b842009-11-02 08:13:21 -0300826 tmpbuf, sizeof(tmpbuf));
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300827 gspca_frame_add(gspca_dev, INTER_PACKET,
Marton Nemeth1408b842009-11-02 08:13:21 -0300828 pac_jpeg_header2, sizeof(pac_jpeg_header2));
829}
830
831/* this function is run at interrupt level */
832static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300833 u8 *data, /* isoc packet */
Marton Nemeth1408b842009-11-02 08:13:21 -0300834 int len) /* iso packet length */
835{
836 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300837 struct gspca_frame *frame;
Marton Nemeth1408b842009-11-02 08:13:21 -0300838 unsigned char *sof;
839
840 sof = pac_find_sof(&sd->sof_read, data, len);
841 if (sof) {
842 int n, lum_offset, footer_length;
843
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300844 frame = gspca_get_i_frame(gspca_dev);
845 if (frame == NULL) {
846 gspca_dev->last_packet_type = DISCARD_PACKET;
847 return;
848 }
849
Marton Nemeth1408b842009-11-02 08:13:21 -0300850 /* 6 bytes after the FF D9 EOF marker a number of lumination
851 bytes are send corresponding to different parts of the
852 image, the 14th and 15th byte after the EOF seem to
853 correspond to the center of the image */
854 lum_offset = 61 + sizeof pac_sof_marker;
855 footer_length = 74;
856
857 /* Finish decoding current frame */
858 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
859 if (n < 0) {
860 frame->data_end += n;
861 n = 0;
862 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300863 gspca_frame_add(gspca_dev, INTER_PACKET,
Marton Nemeth1408b842009-11-02 08:13:21 -0300864 data, n);
865 if (gspca_dev->last_packet_type != DISCARD_PACKET &&
866 frame->data_end[-2] == 0xff &&
867 frame->data_end[-1] == 0xd9)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300868 gspca_frame_add(gspca_dev, LAST_PACKET,
Marton Nemeth1408b842009-11-02 08:13:21 -0300869 NULL, 0);
870
871 n = sof - data;
872 len -= n;
873 data = sof;
874
875 /* Get average lumination */
876 if (gspca_dev->last_packet_type == LAST_PACKET &&
877 n >= lum_offset)
878 atomic_set(&sd->avg_lum, data[-lum_offset] +
879 data[-lum_offset + 1]);
880 else
881 atomic_set(&sd->avg_lum, -1);
882
883 /* Start the new frame with the jpeg header */
884 /* The PAC7302 has the image rotated 90 degrees */
885 pac_start_frame(gspca_dev, frame,
886 gspca_dev->width, gspca_dev->height);
887 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300888 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300889}
890
891static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
892{
893 struct sd *sd = (struct sd *) gspca_dev;
894
895 sd->brightness = val;
896 if (gspca_dev->streaming)
897 setbrightcont(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300898 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300899}
900
901static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
902{
903 struct sd *sd = (struct sd *) gspca_dev;
904
905 *val = sd->brightness;
906 return 0;
907}
908
909static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
910{
911 struct sd *sd = (struct sd *) gspca_dev;
912
913 sd->contrast = val;
914 if (gspca_dev->streaming) {
915 setbrightcont(gspca_dev);
916 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300917 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300918}
919
920static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
921{
922 struct sd *sd = (struct sd *) gspca_dev;
923
924 *val = sd->contrast;
925 return 0;
926}
927
928static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
929{
930 struct sd *sd = (struct sd *) gspca_dev;
931
932 sd->colors = val;
933 if (gspca_dev->streaming)
934 setcolors(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300935 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300936}
937
938static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
939{
940 struct sd *sd = (struct sd *) gspca_dev;
941
942 *val = sd->colors;
943 return 0;
944}
945
Marton Nemeth23fbee62009-11-08 04:35:12 -0300946static int sd_setwhitebalance(struct gspca_dev *gspca_dev, __s32 val)
947{
948 struct sd *sd = (struct sd *) gspca_dev;
Marton Nemeth23fbee62009-11-08 04:35:12 -0300949
950 sd->white_balance = val;
951 if (gspca_dev->streaming)
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300952 setwhitebalance(gspca_dev);
953 return gspca_dev->usb_err;
Marton Nemeth23fbee62009-11-08 04:35:12 -0300954}
955
956static int sd_getwhitebalance(struct gspca_dev *gspca_dev, __s32 *val)
957{
958 struct sd *sd = (struct sd *) gspca_dev;
959
960 *val = sd->white_balance;
961 return 0;
962}
963
Márton Németh265a8092009-11-07 15:15:56 -0300964static int sd_setredbalance(struct gspca_dev *gspca_dev, __s32 val)
965{
966 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300967
968 sd->red_balance = val;
969 if (gspca_dev->streaming)
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300970 setredbalance(gspca_dev);
971 return gspca_dev->usb_err;
Márton Németh265a8092009-11-07 15:15:56 -0300972}
973
974static int sd_getredbalance(struct gspca_dev *gspca_dev, __s32 *val)
975{
976 struct sd *sd = (struct sd *) gspca_dev;
977
978 *val = sd->red_balance;
979 return 0;
980}
981
982static int sd_setbluebalance(struct gspca_dev *gspca_dev, __s32 val)
983{
984 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300985
986 sd->blue_balance = val;
987 if (gspca_dev->streaming)
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300988 setbluebalance(gspca_dev);
989 return gspca_dev->usb_err;
Márton Németh265a8092009-11-07 15:15:56 -0300990}
991
992static int sd_getbluebalance(struct gspca_dev *gspca_dev, __s32 *val)
993{
994 struct sd *sd = (struct sd *) gspca_dev;
995
996 *val = sd->blue_balance;
997 return 0;
998}
999
Marton Nemeth1408b842009-11-02 08:13:21 -03001000static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1001{
1002 struct sd *sd = (struct sd *) gspca_dev;
1003
1004 sd->gain = val;
1005 if (gspca_dev->streaming)
1006 setgain(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001007 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -03001008}
1009
1010static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1011{
1012 struct sd *sd = (struct sd *) gspca_dev;
1013
1014 *val = sd->gain;
1015 return 0;
1016}
1017
1018static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1019{
1020 struct sd *sd = (struct sd *) gspca_dev;
1021
1022 sd->exposure = val;
1023 if (gspca_dev->streaming)
1024 setexposure(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001025 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -03001026}
1027
1028static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1029{
1030 struct sd *sd = (struct sd *) gspca_dev;
1031
1032 *val = sd->exposure;
1033 return 0;
1034}
1035
1036static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1037{
1038 struct sd *sd = (struct sd *) gspca_dev;
1039
1040 sd->autogain = val;
1041 /* when switching to autogain set defaults to make sure
1042 we are on a valid point of the autogain gain /
1043 exposure knee graph, and give this change time to
1044 take effect before doing autogain. */
1045 if (sd->autogain) {
1046 sd->exposure = EXPOSURE_DEF;
1047 sd->gain = GAIN_DEF;
1048 if (gspca_dev->streaming) {
1049 sd->autogain_ignore_frames =
1050 PAC_AUTOGAIN_IGNORE_FRAMES;
1051 setexposure(gspca_dev);
1052 setgain(gspca_dev);
1053 }
1054 }
1055
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001056 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -03001057}
1058
1059static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1060{
1061 struct sd *sd = (struct sd *) gspca_dev;
1062
1063 *val = sd->autogain;
1064 return 0;
1065}
1066
1067static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1068{
1069 struct sd *sd = (struct sd *) gspca_dev;
1070
1071 sd->hflip = val;
1072 if (gspca_dev->streaming)
1073 sethvflip(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001074 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -03001075}
1076
1077static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1078{
1079 struct sd *sd = (struct sd *) gspca_dev;
1080
1081 *val = sd->hflip;
1082 return 0;
1083}
1084
1085static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1086{
1087 struct sd *sd = (struct sd *) gspca_dev;
1088
1089 sd->vflip = val;
1090 if (gspca_dev->streaming)
1091 sethvflip(gspca_dev);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001092 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -03001093}
1094
1095static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1096{
1097 struct sd *sd = (struct sd *) gspca_dev;
1098
1099 *val = sd->vflip;
1100 return 0;
1101}
1102
Márton Németh6763cc02009-11-09 07:10:46 -03001103#ifdef CONFIG_VIDEO_ADV_DEBUG
1104static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
1105 struct v4l2_dbg_register *reg)
1106{
Márton Németh6763cc02009-11-09 07:10:46 -03001107 __u8 index;
1108 __u8 value;
1109
1110 /* reg->reg: bit0..15: reserved for register index (wIndex is 16bit
1111 long on the USB bus)
1112 */
1113 if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
1114 reg->match.addr == 0 &&
1115 (reg->reg < 0x000000ff) &&
1116 (reg->val <= 0x000000ff)
1117 ) {
1118 /* Currently writing to page 0 is only supported. */
1119 /* reg_w() only supports 8bit index */
1120 index = reg->reg & 0x000000ff;
1121 value = reg->val & 0x000000ff;
1122
1123 /* Note that there shall be no access to other page
1124 by any other function between the page swith and
1125 the actual register write */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001126 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
1127 reg_w(gspca_dev, index, value);
Márton Németh6763cc02009-11-09 07:10:46 -03001128
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001129 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh6763cc02009-11-09 07:10:46 -03001130 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -03001131 return gspca_dev->usb_err;
Márton Németh6763cc02009-11-09 07:10:46 -03001132}
1133
1134static int sd_chip_ident(struct gspca_dev *gspca_dev,
1135 struct v4l2_dbg_chip_ident *chip)
1136{
1137 int ret = -EINVAL;
1138
1139 if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
1140 chip->match.addr == 0) {
1141 chip->revision = 0;
1142 chip->ident = V4L2_IDENT_UNKNOWN;
1143 ret = 0;
1144 }
1145 return ret;
1146}
1147#endif
1148
Márton Némethaed6f1b2010-01-28 16:33:38 -03001149#ifdef CONFIG_INPUT
1150static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1151 u8 *data, /* interrupt packet data */
1152 int len) /* interrput packet length */
1153{
1154 int ret = -EINVAL;
1155 u8 data0, data1;
1156
1157 if (len == 2) {
1158 data0 = data[0];
1159 data1 = data[1];
1160 if ((data0 == 0x00 && data1 == 0x11) ||
1161 (data0 == 0x22 && data1 == 0x33) ||
1162 (data0 == 0x44 && data1 == 0x55) ||
1163 (data0 == 0x66 && data1 == 0x77) ||
1164 (data0 == 0x88 && data1 == 0x99) ||
1165 (data0 == 0xaa && data1 == 0xbb) ||
1166 (data0 == 0xcc && data1 == 0xdd) ||
1167 (data0 == 0xee && data1 == 0xff)) {
1168 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1169 input_sync(gspca_dev->input_dev);
1170 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1171 input_sync(gspca_dev->input_dev);
1172 ret = 0;
1173 }
1174 }
1175
1176 return ret;
1177}
1178#endif
1179
Marton Nemeth1408b842009-11-02 08:13:21 -03001180/* sub-driver description for pac7302 */
Márton Némethaabcdfb2010-01-05 12:39:02 -03001181static const struct sd_desc sd_desc = {
Marton Nemeth1408b842009-11-02 08:13:21 -03001182 .name = MODULE_NAME,
1183 .ctrls = sd_ctrls,
1184 .nctrls = ARRAY_SIZE(sd_ctrls),
1185 .config = sd_config,
1186 .init = sd_init,
1187 .start = sd_start,
1188 .stopN = sd_stopN,
1189 .stop0 = sd_stop0,
1190 .pkt_scan = sd_pkt_scan,
1191 .dq_callback = do_autogain,
Márton Németh6763cc02009-11-09 07:10:46 -03001192#ifdef CONFIG_VIDEO_ADV_DEBUG
1193 .set_register = sd_dbg_s_register,
1194 .get_chip_ident = sd_chip_ident,
1195#endif
Márton Némethaed6f1b2010-01-28 16:33:38 -03001196#ifdef CONFIG_INPUT
1197 .int_pkt_scan = sd_int_pkt_scan,
1198#endif
Marton Nemeth1408b842009-11-02 08:13:21 -03001199};
1200
1201/* -- module initialisation -- */
Márton Németh37b372e2009-12-10 11:31:09 -03001202static const struct usb_device_id device_table[] __devinitconst = {
Marton Nemeth1408b842009-11-02 08:13:21 -03001203 {USB_DEVICE(0x06f8, 0x3009)},
1204 {USB_DEVICE(0x093a, 0x2620)},
1205 {USB_DEVICE(0x093a, 0x2621)},
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -03001206 {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
1207 {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
Marton Nemeth1408b842009-11-02 08:13:21 -03001208 {USB_DEVICE(0x093a, 0x2626)},
1209 {USB_DEVICE(0x093a, 0x2628)},
Jean-Francois Moinec4322bf2009-12-02 07:04:35 -03001210 {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
Marton Nemeth1408b842009-11-02 08:13:21 -03001211 {USB_DEVICE(0x093a, 0x262a)},
1212 {USB_DEVICE(0x093a, 0x262c)},
1213 {}
1214};
1215MODULE_DEVICE_TABLE(usb, device_table);
1216
1217/* -- device connect -- */
Márton Németh37b372e2009-12-10 11:31:09 -03001218static int __devinit sd_probe(struct usb_interface *intf,
Marton Nemeth1408b842009-11-02 08:13:21 -03001219 const struct usb_device_id *id)
1220{
1221 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1222 THIS_MODULE);
1223}
1224
1225static struct usb_driver sd_driver = {
1226 .name = MODULE_NAME,
1227 .id_table = device_table,
1228 .probe = sd_probe,
1229 .disconnect = gspca_disconnect,
1230#ifdef CONFIG_PM
1231 .suspend = gspca_suspend,
1232 .resume = gspca_resume,
1233#endif
1234};
1235
1236/* -- module insert / remove -- */
1237static int __init sd_mod_init(void)
1238{
1239 int ret;
1240 ret = usb_register(&sd_driver);
1241 if (ret < 0)
1242 return ret;
1243 PDEBUG(D_PROBE, "registered");
1244 return 0;
1245}
1246static void __exit sd_mod_exit(void)
1247{
1248 usb_deregister(&sd_driver);
1249 PDEBUG(D_PROBE, "deregistered");
1250}
1251
1252module_init(sd_mod_init);
1253module_exit(sd_mod_exit);