pac207.c 15 KB

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