pac207.c 13 KB

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