2 * Pixart PAC207BCA library
4 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
6 * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
8 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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.
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.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #define MODULE_NAME "pac207"
26 #include <linux/input.h>
28 /* Include pac common sof detection functions */
29 #include "pac_common.h"
31 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
32 MODULE_DESCRIPTION("Pixart PAC207");
33 MODULE_LICENSE("GPL");
35 #define PAC207_CTRL_TIMEOUT 100 /* ms */
37 #define PAC207_BRIGHTNESS_MIN 0
38 #define PAC207_BRIGHTNESS_MAX 255
39 #define PAC207_BRIGHTNESS_DEFAULT 46
40 #define PAC207_BRIGHTNESS_REG 0x08
42 #define PAC207_EXPOSURE_MIN 3
43 #define PAC207_EXPOSURE_MAX 90 /* 1 sec expo time / 1 fps */
44 #define PAC207_EXPOSURE_DEFAULT 5 /* power on default: 3 */
45 #define PAC207_EXPOSURE_REG 0x02
47 #define PAC207_GAIN_MIN 0
48 #define PAC207_GAIN_MAX 31
49 #define PAC207_GAIN_DEFAULT 7 /* power on default: 9 */
50 #define PAC207_GAIN_REG 0x0e
52 #define PAC207_AUTOGAIN_DEADZONE 30
54 /* global parameters */
55 static int led_invert
;
56 module_param(led_invert
, int, 0644);
57 MODULE_PARM_DESC(led_invert
, "Invert led");
59 /* specific webcam descriptor */
61 struct gspca_dev gspca_dev
; /* !! must be the first item */
63 struct v4l2_ctrl
*brightness
;
68 u8 autogain_ignore_frames
;
73 static const struct v4l2_pix_format sif_mode
[] = {
74 {176, 144, V4L2_PIX_FMT_PAC207
, V4L2_FIELD_NONE
,
76 .sizeimage
= (176 + 2) * 144,
77 /* uncompressed, add 2 bytes / line for line header */
78 .colorspace
= V4L2_COLORSPACE_SRGB
,
80 {352, 288, V4L2_PIX_FMT_PAC207
, V4L2_FIELD_NONE
,
82 /* compressed, but only when needed (not compressed
83 when the framerate is low) */
84 .sizeimage
= (352 + 2) * 288,
85 .colorspace
= V4L2_COLORSPACE_SRGB
,
89 static const __u8 pac207_sensor_init
[][8] = {
90 {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
91 {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
92 {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
93 {0x32, 0x00, 0x96, 0x00, 0xa2, 0x02, 0xaf, 0x00},
96 static void pac207_write_regs(struct gspca_dev
*gspca_dev
, u16 index
,
97 const u8
*buffer
, u16 length
)
99 struct usb_device
*udev
= gspca_dev
->dev
;
102 if (gspca_dev
->usb_err
< 0)
105 memcpy(gspca_dev
->usb_buf
, buffer
, length
);
107 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x01,
108 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
110 gspca_dev
->usb_buf
, length
, PAC207_CTRL_TIMEOUT
);
112 pr_err("Failed to write registers to index 0x%04X, error %d\n",
114 gspca_dev
->usb_err
= err
;
118 static void pac207_write_reg(struct gspca_dev
*gspca_dev
, u16 index
, u16 value
)
120 struct usb_device
*udev
= gspca_dev
->dev
;
123 if (gspca_dev
->usb_err
< 0)
126 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), 0x00,
127 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
128 value
, index
, NULL
, 0, PAC207_CTRL_TIMEOUT
);
130 pr_err("Failed to write a register (index 0x%04X, value 0x%02X, error %d)\n",
132 gspca_dev
->usb_err
= err
;
136 static int pac207_read_reg(struct gspca_dev
*gspca_dev
, u16 index
)
138 struct usb_device
*udev
= gspca_dev
->dev
;
141 if (gspca_dev
->usb_err
< 0)
144 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), 0x00,
145 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
147 gspca_dev
->usb_buf
, 1, PAC207_CTRL_TIMEOUT
);
149 pr_err("Failed to read a register (index 0x%04X, error %d)\n",
151 gspca_dev
->usb_err
= res
;
155 return gspca_dev
->usb_buf
[0];
158 /* this function is called at probe time */
159 static int sd_config(struct gspca_dev
*gspca_dev
,
160 const struct usb_device_id
*id
)
165 idreg
[0] = pac207_read_reg(gspca_dev
, 0x0000);
166 idreg
[1] = pac207_read_reg(gspca_dev
, 0x0001);
167 idreg
[0] = ((idreg
[0] & 0x0f) << 4) | ((idreg
[1] & 0xf0) >> 4);
168 idreg
[1] = idreg
[1] & 0x0f;
169 gspca_dbg(gspca_dev
, D_PROBE
, "Pixart Sensor ID 0x%02X Chips ID 0x%02X\n",
172 if (idreg
[0] != 0x27) {
173 gspca_dbg(gspca_dev
, D_PROBE
, "Error invalid sensor ID!\n");
177 gspca_dbg(gspca_dev
, D_PROBE
,
178 "Pixart PAC207BCA Image Processor and Control Chip detected (vid/pid 0x%04X:0x%04X)\n",
179 id
->idVendor
, id
->idProduct
);
181 cam
= &gspca_dev
->cam
;
182 cam
->cam_mode
= sif_mode
;
183 cam
->nmodes
= ARRAY_SIZE(sif_mode
);
188 /* this function is called at probe and resume time */
189 static int sd_init(struct gspca_dev
*gspca_dev
)
193 /* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
198 pac207_write_reg(gspca_dev
, 0x41, mode
);
199 pac207_write_reg(gspca_dev
, 0x0f, 0x00); /* Power Control */
201 return gspca_dev
->usb_err
;
204 static void setcontrol(struct gspca_dev
*gspca_dev
, u16 reg
, u16 val
)
206 pac207_write_reg(gspca_dev
, reg
, val
);
207 pac207_write_reg(gspca_dev
, 0x13, 0x01); /* Bit 0, auto clear */
208 pac207_write_reg(gspca_dev
, 0x1c, 0x01); /* not documented */
211 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
213 struct gspca_dev
*gspca_dev
=
214 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
215 struct sd
*sd
= (struct sd
*)gspca_dev
;
217 gspca_dev
->usb_err
= 0;
219 if (ctrl
->id
== V4L2_CID_AUTOGAIN
&& ctrl
->is_new
&& ctrl
->val
) {
220 /* when switching to autogain set defaults to make sure
221 we are on a valid point of the autogain gain /
222 exposure knee graph, and give this change time to
223 take effect before doing autogain. */
224 gspca_dev
->exposure
->val
= PAC207_EXPOSURE_DEFAULT
;
225 gspca_dev
->gain
->val
= PAC207_GAIN_DEFAULT
;
226 sd
->autogain_ignore_frames
= PAC_AUTOGAIN_IGNORE_FRAMES
;
229 if (!gspca_dev
->streaming
)
233 case V4L2_CID_BRIGHTNESS
:
234 setcontrol(gspca_dev
, PAC207_BRIGHTNESS_REG
, ctrl
->val
);
236 case V4L2_CID_AUTOGAIN
:
237 if (gspca_dev
->exposure
->is_new
|| (ctrl
->is_new
&& ctrl
->val
))
238 setcontrol(gspca_dev
, PAC207_EXPOSURE_REG
,
239 gspca_dev
->exposure
->val
);
240 if (gspca_dev
->gain
->is_new
|| (ctrl
->is_new
&& ctrl
->val
))
241 setcontrol(gspca_dev
, PAC207_GAIN_REG
,
242 gspca_dev
->gain
->val
);
247 return gspca_dev
->usb_err
;
250 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
254 /* this function is called at probe time */
255 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
257 struct sd
*sd
= (struct sd
*) gspca_dev
;
258 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
260 gspca_dev
->vdev
.ctrl_handler
= hdl
;
261 v4l2_ctrl_handler_init(hdl
, 4);
263 sd
->brightness
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
265 PAC207_BRIGHTNESS_MIN
, PAC207_BRIGHTNESS_MAX
,
266 1, PAC207_BRIGHTNESS_DEFAULT
);
267 gspca_dev
->autogain
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
268 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
269 gspca_dev
->exposure
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
271 PAC207_EXPOSURE_MIN
, PAC207_EXPOSURE_MAX
,
272 1, PAC207_EXPOSURE_DEFAULT
);
273 gspca_dev
->gain
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
275 PAC207_GAIN_MIN
, PAC207_GAIN_MAX
,
276 1, PAC207_GAIN_DEFAULT
);
278 pr_err("Could not initialize controls\n");
281 v4l2_ctrl_auto_cluster(3, &gspca_dev
->autogain
, 0, false);
285 /* -- start the camera -- */
286 static int sd_start(struct gspca_dev
*gspca_dev
)
288 struct sd
*sd
= (struct sd
*) gspca_dev
;
291 pac207_write_reg(gspca_dev
, 0x0f, 0x10); /* Power control (Bit 6-0) */
292 pac207_write_regs(gspca_dev
, 0x0002, pac207_sensor_init
[0], 8);
293 pac207_write_regs(gspca_dev
, 0x000a, pac207_sensor_init
[1], 8);
294 pac207_write_regs(gspca_dev
, 0x0012, pac207_sensor_init
[2], 8);
295 pac207_write_regs(gspca_dev
, 0x0042, pac207_sensor_init
[3], 8);
297 /* Compression Balance */
298 if (gspca_dev
->pixfmt
.width
== 176)
299 pac207_write_reg(gspca_dev
, 0x4a, 0xff);
301 pac207_write_reg(gspca_dev
, 0x4a, 0x30);
302 pac207_write_reg(gspca_dev
, 0x4b, 0x00); /* Sram test value */
303 pac207_write_reg(gspca_dev
, 0x08, v4l2_ctrl_g_ctrl(sd
->brightness
));
305 /* PGA global gain (Bit 4-0) */
306 pac207_write_reg(gspca_dev
, 0x0e,
307 v4l2_ctrl_g_ctrl(gspca_dev
->gain
));
308 pac207_write_reg(gspca_dev
, 0x02,
309 v4l2_ctrl_g_ctrl(gspca_dev
->exposure
)); /* PXCK = 12MHz /n */
311 /* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
316 if (gspca_dev
->pixfmt
.width
== 176) { /* 176x144 */
318 gspca_dbg(gspca_dev
, D_STREAM
, "pac207_start mode 176x144\n");
319 } else { /* 352x288 */
320 gspca_dbg(gspca_dev
, D_STREAM
, "pac207_start mode 352x288\n");
322 pac207_write_reg(gspca_dev
, 0x41, mode
);
324 pac207_write_reg(gspca_dev
, 0x13, 0x01); /* Bit 0, auto clear */
325 pac207_write_reg(gspca_dev
, 0x1c, 0x01); /* not documented */
327 pac207_write_reg(gspca_dev
, 0x40, 0x01); /* Start ISO pipe */
330 sd
->autogain_ignore_frames
= 0;
331 atomic_set(&sd
->avg_lum
, -1);
332 return gspca_dev
->usb_err
;
335 static void sd_stopN(struct gspca_dev
*gspca_dev
)
339 /* mode: Image Format (Bit 0), LED (1), Compr. test mode (2) */
344 pac207_write_reg(gspca_dev
, 0x40, 0x00); /* Stop ISO pipe */
345 pac207_write_reg(gspca_dev
, 0x41, mode
); /* Turn off LED */
346 pac207_write_reg(gspca_dev
, 0x0f, 0x00); /* Power Control */
350 static void pac207_do_auto_gain(struct gspca_dev
*gspca_dev
)
352 struct sd
*sd
= (struct sd
*) gspca_dev
;
353 int avg_lum
= atomic_read(&sd
->avg_lum
);
358 if (sd
->autogain_ignore_frames
> 0)
359 sd
->autogain_ignore_frames
--;
360 else if (gspca_coarse_grained_expo_autogain(gspca_dev
, avg_lum
,
361 90, PAC207_AUTOGAIN_DEADZONE
))
362 sd
->autogain_ignore_frames
= PAC_AUTOGAIN_IGNORE_FRAMES
;
365 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
369 struct sd
*sd
= (struct sd
*) gspca_dev
;
372 sof
= pac_find_sof(gspca_dev
, &sd
->sof_read
, data
, len
);
376 /* finish decoding current frame */
378 if (n
> sizeof pac_sof_marker
)
379 n
-= sizeof pac_sof_marker
;
382 gspca_frame_add(gspca_dev
, LAST_PACKET
,
385 gspca_frame_add(gspca_dev
, FIRST_PACKET
, NULL
, 0);
389 if (sd
->header_read
< 11) {
392 /* get average lumination from frame header (byte 5) */
393 if (sd
->header_read
< 5) {
394 needed
= 5 - sd
->header_read
;
396 atomic_set(&sd
->avg_lum
, data
[needed
- 1]);
398 /* skip the rest of the header */
399 needed
= 11 - sd
->header_read
;
401 sd
->header_read
+= len
;
406 sd
->header_read
= 11;
409 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
412 #if IS_ENABLED(CONFIG_INPUT)
413 static int sd_int_pkt_scan(struct gspca_dev
*gspca_dev
,
414 u8
*data
, /* interrupt packet data */
415 int len
) /* interrupt packet length */
419 if (len
== 2 && data
[0] == 0x5a && data
[1] == 0x5a) {
420 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 1);
421 input_sync(gspca_dev
->input_dev
);
422 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
423 input_sync(gspca_dev
->input_dev
);
431 /* sub-driver description */
432 static const struct sd_desc sd_desc
= {
436 .init_controls
= sd_init_controls
,
439 .dq_callback
= pac207_do_auto_gain
,
440 .pkt_scan
= sd_pkt_scan
,
441 #if IS_ENABLED(CONFIG_INPUT)
442 .int_pkt_scan
= sd_int_pkt_scan
,
446 /* -- module initialisation -- */
447 static const struct usb_device_id device_table
[] = {
448 {USB_DEVICE(0x041e, 0x4028)},
449 {USB_DEVICE(0x093a, 0x2460)},
450 {USB_DEVICE(0x093a, 0x2461)},
451 {USB_DEVICE(0x093a, 0x2463)},
452 {USB_DEVICE(0x093a, 0x2464)},
453 {USB_DEVICE(0x093a, 0x2468)},
454 {USB_DEVICE(0x093a, 0x2470)},
455 {USB_DEVICE(0x093a, 0x2471)},
456 {USB_DEVICE(0x093a, 0x2472)},
457 {USB_DEVICE(0x093a, 0x2474)},
458 {USB_DEVICE(0x093a, 0x2476)},
459 {USB_DEVICE(0x145f, 0x013a)},
460 {USB_DEVICE(0x2001, 0xf115)},
463 MODULE_DEVICE_TABLE(usb
, device_table
);
465 /* -- device connect -- */
466 static int sd_probe(struct usb_interface
*intf
,
467 const struct usb_device_id
*id
)
469 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
473 static struct usb_driver sd_driver
= {
475 .id_table
= device_table
,
477 .disconnect
= gspca_disconnect
,
479 .suspend
= gspca_suspend
,
480 .resume
= gspca_resume
,
481 .reset_resume
= gspca_resume
,
485 module_usb_driver(sd_driver
);