]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/gspca/pac207.c
[media] gspca_pac207: Convert to the control framework
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / gspca / pac207.c
CommitLineData
e2997a72
HG
1/*
2 * Pixart PAC207BCA library
3 *
1fddcf0e 4 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
e2997a72
HG
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6 * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
7 *
8 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
be612514
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
e2997a72
HG
28#define MODULE_NAME "pac207"
29
937a6f54 30#include <linux/input.h>
e2997a72 31#include "gspca.h"
cd92c1a6
HG
32/* Include pac common sof detection functions */
33#include "pac_common.h"
e2997a72 34
1fddcf0e 35MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
e2997a72
HG
36MODULE_DESCRIPTION("Pixart PAC207");
37MODULE_LICENSE("GPL");
38
39#define PAC207_CTRL_TIMEOUT 100 /* ms */
40
41#define PAC207_BRIGHTNESS_MIN 0
42#define PAC207_BRIGHTNESS_MAX 255
0e4a9099 43#define PAC207_BRIGHTNESS_DEFAULT 46
cd92c1a6 44#define PAC207_BRIGHTNESS_REG 0x08
0e4a9099
HG
45
46#define PAC207_EXPOSURE_MIN 3
69c00399 47#define PAC207_EXPOSURE_MAX 90 /* 1 sec expo time / 1 fps */
0e4a9099 48#define PAC207_EXPOSURE_DEFAULT 5 /* power on default: 3 */
69c00399 49#define PAC207_EXPOSURE_KNEE 9 /* fps: 90 / exposure -> 9: 10 fps */
cd92c1a6 50#define PAC207_EXPOSURE_REG 0x02
e2997a72
HG
51
52#define PAC207_GAIN_MIN 0
53#define PAC207_GAIN_MAX 31
69c00399
HG
54#define PAC207_GAIN_DEFAULT 7 /* power on default: 9 */
55#define PAC207_GAIN_KNEE 15
cd92c1a6 56#define PAC207_GAIN_REG 0x0e
e2997a72
HG
57
58#define PAC207_AUTOGAIN_DEADZONE 30
e2997a72 59
e2997a72
HG
60/* specific webcam descriptor */
61struct sd {
62 struct gspca_dev gspca_dev; /* !! must be the first item */
63
cd92c1a6 64 struct v4l2_ctrl *brightness;
e2997a72 65
cd92c1a6 66 u8 mode;
e2997a72 67 u8 sof_read;
ab8f12cf 68 u8 header_read;
e2997a72
HG
69 u8 autogain_ignore_frames;
70
71 atomic_t avg_lum;
72};
73
cc611b8a 74static const struct v4l2_pix_format sif_mode[] = {
c2446b3e
JFM
75 {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
76 .bytesperline = 176,
77 .sizeimage = (176 + 2) * 144,
78 /* uncompressed, add 2 bytes / line for line header */
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .priv = 1},
81 {352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
82 .bytesperline = 352,
80544d3c
HG
83 /* compressed, but only when needed (not compressed
84 when the framerate is low) */
85 .sizeimage = (352 + 2) * 288,
c2446b3e
JFM
86 .colorspace = V4L2_COLORSPACE_SRGB,
87 .priv = 0},
e2997a72
HG
88};
89
90static const __u8 pac207_sensor_init[][8] = {
0e4a9099
HG
91 {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
92 {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
e2997a72 93 {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
1d00d6c1 94 {0x32, 0x00, 0x96, 0x00, 0xa2, 0x02, 0xaf, 0x00},
e2997a72
HG
95};
96
cb0988cb 97static void pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
e2997a72
HG
98 const u8 *buffer, u16 length)
99{
100 struct usb_device *udev = gspca_dev->dev;
101 int err;
e2997a72 102
cb0988cb
HG
103 if (gspca_dev->usb_err < 0)
104 return;
105
739570bb 106 memcpy(gspca_dev->usb_buf, buffer, length);
e2997a72
HG
107
108 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
109 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
739570bb
JFM
110 0x00, index,
111 gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT);
cb0988cb 112 if (err < 0) {
be612514
JP
113 pr_err("Failed to write registers to index 0x%04X, error %d\n",
114 index, err);
cb0988cb
HG
115 gspca_dev->usb_err = err;
116 }
e2997a72
HG
117}
118
cb0988cb 119static void pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
e2997a72
HG
120{
121 struct usb_device *udev = gspca_dev->dev;
122 int err;
123
cb0988cb
HG
124 if (gspca_dev->usb_err < 0)
125 return;
126
e2997a72
HG
127 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00,
128 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
129 value, index, NULL, 0, PAC207_CTRL_TIMEOUT);
cb0988cb 130 if (err) {
be612514
JP
131 pr_err("Failed to write a register (index 0x%04X, value 0x%02X, error %d)\n",
132 index, value, err);
cb0988cb
HG
133 gspca_dev->usb_err = err;
134 }
e2997a72
HG
135}
136
903e10aa 137static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
e2997a72
HG
138{
139 struct usb_device *udev = gspca_dev->dev;
e2997a72
HG
140 int res;
141
cb0988cb
HG
142 if (gspca_dev->usb_err < 0)
143 return 0;
144
e2997a72
HG
145 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
146 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
739570bb
JFM
147 0x00, index,
148 gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT);
e2997a72 149 if (res < 0) {
be612514
JP
150 pr_err("Failed to read a register (index 0x%04X, error %d)\n",
151 index, res);
cb0988cb
HG
152 gspca_dev->usb_err = res;
153 return 0;
e2997a72
HG
154 }
155
739570bb 156 return gspca_dev->usb_buf[0];
e2997a72
HG
157}
158
e2997a72
HG
159/* this function is called at probe time */
160static int sd_config(struct gspca_dev *gspca_dev,
161 const struct usb_device_id *id)
162{
163 struct cam *cam;
164 u8 idreg[2];
165
166 idreg[0] = pac207_read_reg(gspca_dev, 0x0000);
167 idreg[1] = pac207_read_reg(gspca_dev, 0x0001);
1d00d6c1 168 idreg[0] = ((idreg[0] & 0x0f) << 4) | ((idreg[1] & 0xf0) >> 4);
e2997a72
HG
169 idreg[1] = idreg[1] & 0x0f;
170 PDEBUG(D_PROBE, "Pixart Sensor ID 0x%02X Chips ID 0x%02X",
171 idreg[0], idreg[1]);
172
173 if (idreg[0] != 0x27) {
174 PDEBUG(D_PROBE, "Error invalid sensor ID!");
175 return -ENODEV;
176 }
177
e2997a72
HG
178 PDEBUG(D_PROBE,
179 "Pixart PAC207BCA Image Processor and Control Chip detected"
180 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
181
182 cam = &gspca_dev->cam;
e2997a72
HG
183 cam->cam_mode = sif_mode;
184 cam->nmodes = ARRAY_SIZE(sif_mode);
185
186 return 0;
187}
188
012d6b02
JFM
189/* this function is called at probe and resume time */
190static int sd_init(struct gspca_dev *gspca_dev)
e2997a72 191{
66e4124f
JFM
192 pac207_write_reg(gspca_dev, 0x41, 0x00);
193 /* Bit_0=Image Format,
194 * Bit_1=LED,
195 * Bit_2=Compression test mode enable */
196 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
e2997a72 197
cb0988cb 198 return gspca_dev->usb_err;
e2997a72
HG
199}
200
cd92c1a6
HG
201static void setcontrol(struct gspca_dev *gspca_dev, u16 reg, u16 val)
202{
203 pac207_write_reg(gspca_dev, reg, val);
204 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
205 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
206}
207
208static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
209{
210 struct gspca_dev *gspca_dev =
211 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
212 struct sd *sd = (struct sd *)gspca_dev;
213
214 gspca_dev->usb_err = 0;
215
216 if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
217 /* when switching to autogain set defaults to make sure
218 we are on a valid point of the autogain gain /
219 exposure knee graph, and give this change time to
220 take effect before doing autogain. */
221 gspca_dev->exposure->val = PAC207_EXPOSURE_DEFAULT;
222 gspca_dev->gain->val = PAC207_GAIN_DEFAULT;
223 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
224 }
225
226 if (!gspca_dev->streaming)
227 return 0;
228
229 switch (ctrl->id) {
230 case V4L2_CID_BRIGHTNESS:
231 setcontrol(gspca_dev, PAC207_BRIGHTNESS_REG, ctrl->val);
232 break;
233 case V4L2_CID_AUTOGAIN:
234 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
235 setcontrol(gspca_dev, PAC207_EXPOSURE_REG,
236 gspca_dev->exposure->val);
237 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
238 setcontrol(gspca_dev, PAC207_GAIN_REG,
239 gspca_dev->gain->val);
240 break;
241 default:
242 return -EINVAL;
243 }
244 return gspca_dev->usb_err;
245}
246
247static const struct v4l2_ctrl_ops sd_ctrl_ops = {
248 .s_ctrl = sd_s_ctrl,
249};
250
251/* this function is called at probe time */
252static int sd_init_controls(struct gspca_dev *gspca_dev)
253{
254 struct sd *sd = (struct sd *) gspca_dev;
255 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
256
257 gspca_dev->vdev.ctrl_handler = hdl;
258 v4l2_ctrl_handler_init(hdl, 4);
259
260 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
261 V4L2_CID_BRIGHTNESS,
262 PAC207_BRIGHTNESS_MIN, PAC207_BRIGHTNESS_MAX,
263 1, PAC207_BRIGHTNESS_DEFAULT);
264 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
265 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
266 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
267 V4L2_CID_EXPOSURE,
268 PAC207_EXPOSURE_MIN, PAC207_EXPOSURE_MAX,
269 1, PAC207_EXPOSURE_DEFAULT);
270 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
271 V4L2_CID_GAIN,
272 PAC207_GAIN_MIN, PAC207_GAIN_MAX,
273 1, PAC207_GAIN_DEFAULT);
274 if (hdl->error) {
275 pr_err("Could not initialize controls\n");
276 return hdl->error;
277 }
278 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
279 return 0;
280}
281
e2997a72 282/* -- start the camera -- */
72ab97ce 283static int sd_start(struct gspca_dev *gspca_dev)
e2997a72
HG
284{
285 struct sd *sd = (struct sd *) gspca_dev;
286 __u8 mode;
287
288 pac207_write_reg(gspca_dev, 0x0f, 0x10); /* Power control (Bit 6-0) */
289 pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
290 pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
291 pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
c529e556 292 pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8);
e2997a72
HG
293
294 /* Compression Balance */
295 if (gspca_dev->width == 176)
296 pac207_write_reg(gspca_dev, 0x4a, 0xff);
297 else
0e4a9099 298 pac207_write_reg(gspca_dev, 0x4a, 0x30);
e2997a72 299 pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
cd92c1a6 300 pac207_write_reg(gspca_dev, 0x08, v4l2_ctrl_g_ctrl(sd->brightness));
e2997a72
HG
301
302 /* PGA global gain (Bit 4-0) */
cd92c1a6
HG
303 pac207_write_reg(gspca_dev, 0x0e,
304 v4l2_ctrl_g_ctrl(gspca_dev->gain));
305 pac207_write_reg(gspca_dev, 0x02,
306 v4l2_ctrl_g_ctrl(gspca_dev->exposure)); /* PXCK = 12MHz /n */
e2997a72
HG
307
308 mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */
d43fa32f 309 if (gspca_dev->width == 176) { /* 176x144 */
e2997a72
HG
310 mode |= 0x01;
311 PDEBUG(D_STREAM, "pac207_start mode 176x144");
d43fa32f 312 } else { /* 352x288 */
e2997a72 313 PDEBUG(D_STREAM, "pac207_start mode 352x288");
d43fa32f 314 }
e2997a72
HG
315 pac207_write_reg(gspca_dev, 0x41, mode);
316
317 pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
318 pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
6a7eba24 319 msleep(10);
e2997a72
HG
320 pac207_write_reg(gspca_dev, 0x40, 0x01); /* Start ISO pipe */
321
322 sd->sof_read = 0;
323 sd->autogain_ignore_frames = 0;
324 atomic_set(&sd->avg_lum, -1);
cb0988cb 325 return gspca_dev->usb_err;
e2997a72
HG
326}
327
328static void sd_stopN(struct gspca_dev *gspca_dev)
329{
330 pac207_write_reg(gspca_dev, 0x40, 0x00); /* Stop ISO pipe */
331 pac207_write_reg(gspca_dev, 0x41, 0x00); /* Turn of LED */
332 pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
333}
334
8a5b2e90 335
e2997a72
HG
336static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
337{
338 struct sd *sd = (struct sd *) gspca_dev;
e2997a72
HG
339 int avg_lum = atomic_read(&sd->avg_lum);
340
dcef3237 341 if (avg_lum == -1)
e2997a72
HG
342 return;
343
dcef3237 344 if (sd->autogain_ignore_frames > 0)
e2997a72 345 sd->autogain_ignore_frames--;
cd92c1a6 346 else if (gspca_expo_autogain(gspca_dev, avg_lum,
69c00399 347 90, PAC207_AUTOGAIN_DEADZONE,
dcef3237 348 PAC207_GAIN_KNEE, PAC207_EXPOSURE_KNEE))
8a5b2e90 349 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
e2997a72
HG
350}
351
e2997a72 352static void sd_pkt_scan(struct gspca_dev *gspca_dev,
76dd272b 353 u8 *data,
e2997a72
HG
354 int len)
355{
ab8f12cf 356 struct sd *sd = (struct sd *) gspca_dev;
e2997a72 357 unsigned char *sof;
e2997a72 358
a6b69e40 359 sof = pac_find_sof(&sd->sof_read, data, len);
e2997a72 360 if (sof) {
ab8f12cf
HG
361 int n;
362
e2997a72 363 /* finish decoding current frame */
ab8f12cf 364 n = sof - data;
327c4abf
HG
365 if (n > sizeof pac_sof_marker)
366 n -= sizeof pac_sof_marker;
ab8f12cf
HG
367 else
368 n = 0;
76dd272b
JFM
369 gspca_frame_add(gspca_dev, LAST_PACKET,
370 data, n);
ab8f12cf 371 sd->header_read = 0;
76dd272b 372 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
e2997a72
HG
373 len -= sof - data;
374 data = sof;
375 }
ab8f12cf
HG
376 if (sd->header_read < 11) {
377 int needed;
e2997a72 378
ab8f12cf
HG
379 /* get average lumination from frame header (byte 5) */
380 if (sd->header_read < 5) {
381 needed = 5 - sd->header_read;
382 if (len >= needed)
383 atomic_set(&sd->avg_lum, data[needed - 1]);
384 }
385 /* skip the rest of the header */
386 needed = 11 - sd->header_read;
387 if (len <= needed) {
388 sd->header_read += len;
389 return;
390 }
391 data += needed;
392 len -= needed;
393 sd->header_read = 11;
394 }
e2997a72 395
76dd272b 396 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
e2997a72
HG
397}
398
2856643e 399#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
937a6f54
HG
400static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
401 u8 *data, /* interrupt packet data */
402 int len) /* interrput packet length */
403{
404 int ret = -EINVAL;
405
406 if (len == 2 && data[0] == 0x5a && data[1] == 0x5a) {
407 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
408 input_sync(gspca_dev->input_dev);
409 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
410 input_sync(gspca_dev->input_dev);
411 ret = 0;
412 }
413
414 return ret;
415}
416#endif
417
e2997a72 418/* sub-driver description */
a5ae2062 419static const struct sd_desc sd_desc = {
e2997a72 420 .name = MODULE_NAME,
e2997a72 421 .config = sd_config,
012d6b02 422 .init = sd_init,
cd92c1a6 423 .init_controls = sd_init_controls,
e2997a72
HG
424 .start = sd_start,
425 .stopN = sd_stopN,
e2997a72
HG
426 .dq_callback = pac207_do_auto_gain,
427 .pkt_scan = sd_pkt_scan,
2856643e 428#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
937a6f54
HG
429 .int_pkt_scan = sd_int_pkt_scan,
430#endif
e2997a72
HG
431};
432
433/* -- module initialisation -- */
95c967c1 434static const struct usb_device_id device_table[] = {
9d64fdb1
JFM
435 {USB_DEVICE(0x041e, 0x4028)},
436 {USB_DEVICE(0x093a, 0x2460)},
87945895 437 {USB_DEVICE(0x093a, 0x2461)},
9d64fdb1
JFM
438 {USB_DEVICE(0x093a, 0x2463)},
439 {USB_DEVICE(0x093a, 0x2464)},
440 {USB_DEVICE(0x093a, 0x2468)},
441 {USB_DEVICE(0x093a, 0x2470)},
442 {USB_DEVICE(0x093a, 0x2471)},
443 {USB_DEVICE(0x093a, 0x2472)},
db91235e 444 {USB_DEVICE(0x093a, 0x2474)},
d654dca7 445 {USB_DEVICE(0x093a, 0x2476)},
91711874 446 {USB_DEVICE(0x145f, 0x013a)},
9d64fdb1 447 {USB_DEVICE(0x2001, 0xf115)},
e2997a72
HG
448 {}
449};
450MODULE_DEVICE_TABLE(usb, device_table);
451
452/* -- device connect -- */
453static int sd_probe(struct usb_interface *intf,
454 const struct usb_device_id *id)
455{
d43fa32f
JFM
456 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
457 THIS_MODULE);
e2997a72
HG
458}
459
460static struct usb_driver sd_driver = {
461 .name = MODULE_NAME,
462 .id_table = device_table,
463 .probe = sd_probe,
464 .disconnect = gspca_disconnect,
6a709749
JFM
465#ifdef CONFIG_PM
466 .suspend = gspca_suspend,
467 .resume = gspca_resume,
468#endif
e2997a72
HG
469};
470
ecb3b2b3 471module_usb_driver(sd_driver);