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