pac7311.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Pixart PAC7311 library
  3. * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
  4. *
  5. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* Some documentation about various registers as determined by trial and error.
  22. *
  23. * Register page 1:
  24. *
  25. * Address Description
  26. * 0x08 Unknown compressor related, must always be 8 except when not
  27. * in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
  28. * 0x1b Auto white balance related, bit 0 is AWB enable (inverted)
  29. * bits 345 seem to toggle per color gains on/off (inverted)
  30. * 0x78 Global control, bit 6 controls the LED (inverted)
  31. * 0x80 Compression balance, interesting settings:
  32. * 0x01 Use this to allow the camera to switch to higher compr.
  33. * on the fly. Needed to stay within bandwidth @ 640x480@30
  34. * 0x1c From usb captures under Windows for 640x480
  35. * 0x2a Values >= this switch the camera to a lower compression,
  36. * using the same table for both luminance and chrominance.
  37. * This gives a sharper picture. Usable only at 640x480@ <
  38. * 15 fps or 320x240 / 160x120. Note currently the driver
  39. * does not use this as the quality gain is small and the
  40. * generated JPG-s are only understood by v4l-utils >= 0.8.9
  41. * 0x3f From usb captures under Windows for 320x240
  42. * 0x69 From usb captures under Windows for 160x120
  43. *
  44. * Register page 4:
  45. *
  46. * Address Description
  47. * 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
  48. * the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
  49. * 0x0f Master gain 1-245, low value = high gain
  50. * 0x10 Another gain 0-15, limited influence (1-2x gain I guess)
  51. * 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
  52. * 0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
  53. * completely disable the analog amplification block. Set to 0x68
  54. * for max gain, 0x14 for minimal gain.
  55. */
  56. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  57. #define MODULE_NAME "pac7311"
  58. #include <linux/input.h>
  59. #include "gspca.h"
  60. /* Include pac common sof detection functions */
  61. #include "pac_common.h"
  62. #define PAC7311_GAIN_DEFAULT 122
  63. #define PAC7311_EXPOSURE_DEFAULT 3 /* 20 fps, avoid using high compr. */
  64. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  65. MODULE_DESCRIPTION("Pixart PAC7311");
  66. MODULE_LICENSE("GPL");
  67. struct sd {
  68. struct gspca_dev gspca_dev; /* !! must be the first item */
  69. struct v4l2_ctrl *contrast;
  70. struct { /* flip cluster */
  71. struct v4l2_ctrl *hflip;
  72. struct v4l2_ctrl *vflip;
  73. };
  74. u8 sof_read;
  75. u8 autogain_ignore_frames;
  76. atomic_t avg_lum;
  77. };
  78. static const struct v4l2_pix_format vga_mode[] = {
  79. {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  80. .bytesperline = 160,
  81. .sizeimage = 160 * 120 * 3 / 8 + 590,
  82. .colorspace = V4L2_COLORSPACE_JPEG,
  83. .priv = 2},
  84. {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  85. .bytesperline = 320,
  86. .sizeimage = 320 * 240 * 3 / 8 + 590,
  87. .colorspace = V4L2_COLORSPACE_JPEG,
  88. .priv = 1},
  89. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  90. .bytesperline = 640,
  91. .sizeimage = 640 * 480 * 3 / 8 + 590,
  92. .colorspace = V4L2_COLORSPACE_JPEG,
  93. .priv = 0},
  94. };
  95. #define LOAD_PAGE4 254
  96. #define END_OF_SEQUENCE 0
  97. static const __u8 init_7311[] = {
  98. 0xff, 0x01,
  99. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  100. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  101. 0x78, 0x44, /* Bit_0=start stream, Bit_6=LED */
  102. 0xff, 0x04,
  103. 0x27, 0x80,
  104. 0x28, 0xca,
  105. 0x29, 0x53,
  106. 0x2a, 0x0e,
  107. 0xff, 0x01,
  108. 0x3e, 0x20,
  109. };
  110. static const __u8 start_7311[] = {
  111. /* index, len, [value]* */
  112. 0xff, 1, 0x01, /* page 1 */
  113. 0x02, 43, 0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
  114. 0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
  115. 0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
  116. 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00,
  119. 0x3e, 42, 0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
  120. 0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
  121. 0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
  122. 0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
  123. 0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
  124. 0xd0, 0xff,
  125. 0x78, 6, 0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
  126. 0x7f, 18, 0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
  127. 0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
  128. 0x18, 0x20,
  129. 0x96, 3, 0x01, 0x08, 0x04,
  130. 0xa0, 4, 0x44, 0x44, 0x44, 0x04,
  131. 0xf0, 13, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
  132. 0x3f, 0x00, 0x0a, 0x01, 0x00,
  133. 0xff, 1, 0x04, /* page 4 */
  134. 0, LOAD_PAGE4, /* load the page 4 */
  135. 0x11, 1, 0x01,
  136. 0, END_OF_SEQUENCE /* end of sequence */
  137. };
  138. #define SKIP 0xaa
  139. /* page 4 - the value SKIP says skip the index - see reg_w_page() */
  140. static const __u8 page4_7311[] = {
  141. SKIP, SKIP, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
  142. 0x09, 0x00, SKIP, SKIP, 0x07, 0x00, 0x00, 0x62,
  143. 0x08, SKIP, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, SKIP,
  145. SKIP, 0x00, 0x08, SKIP, 0x03, SKIP, 0x00, 0x68,
  146. 0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
  147. 0x23, 0x28, 0x04, 0x11, 0x00, 0x00
  148. };
  149. static void reg_w_buf(struct gspca_dev *gspca_dev,
  150. __u8 index,
  151. const u8 *buffer, int len)
  152. {
  153. int ret;
  154. if (gspca_dev->usb_err < 0)
  155. return;
  156. memcpy(gspca_dev->usb_buf, buffer, len);
  157. ret = usb_control_msg(gspca_dev->dev,
  158. usb_sndctrlpipe(gspca_dev->dev, 0),
  159. 0, /* request */
  160. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  161. 0, /* value */
  162. index, gspca_dev->usb_buf, len,
  163. 500);
  164. if (ret < 0) {
  165. pr_err("reg_w_buf() failed index 0x%02x, error %d\n",
  166. index, ret);
  167. gspca_dev->usb_err = ret;
  168. }
  169. }
  170. static void reg_w(struct gspca_dev *gspca_dev,
  171. __u8 index,
  172. __u8 value)
  173. {
  174. int ret;
  175. if (gspca_dev->usb_err < 0)
  176. return;
  177. gspca_dev->usb_buf[0] = value;
  178. ret = usb_control_msg(gspca_dev->dev,
  179. usb_sndctrlpipe(gspca_dev->dev, 0),
  180. 0, /* request */
  181. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  182. 0, index, gspca_dev->usb_buf, 1,
  183. 500);
  184. if (ret < 0) {
  185. pr_err("reg_w() failed index 0x%02x, value 0x%02x, error %d\n",
  186. index, value, ret);
  187. gspca_dev->usb_err = ret;
  188. }
  189. }
  190. static void reg_w_seq(struct gspca_dev *gspca_dev,
  191. const __u8 *seq, int len)
  192. {
  193. while (--len >= 0) {
  194. reg_w(gspca_dev, seq[0], seq[1]);
  195. seq += 2;
  196. }
  197. }
  198. /* load the beginning of a page */
  199. static void reg_w_page(struct gspca_dev *gspca_dev,
  200. const __u8 *page, int len)
  201. {
  202. int index;
  203. int ret = 0;
  204. if (gspca_dev->usb_err < 0)
  205. return;
  206. for (index = 0; index < len; index++) {
  207. if (page[index] == SKIP) /* skip this index */
  208. continue;
  209. gspca_dev->usb_buf[0] = page[index];
  210. ret = usb_control_msg(gspca_dev->dev,
  211. usb_sndctrlpipe(gspca_dev->dev, 0),
  212. 0, /* request */
  213. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  214. 0, index, gspca_dev->usb_buf, 1,
  215. 500);
  216. if (ret < 0) {
  217. pr_err("reg_w_page() failed index 0x%02x, value 0x%02x, error %d\n",
  218. index, page[index], ret);
  219. gspca_dev->usb_err = ret;
  220. break;
  221. }
  222. }
  223. }
  224. /* output a variable sequence */
  225. static void reg_w_var(struct gspca_dev *gspca_dev,
  226. const __u8 *seq,
  227. const __u8 *page4, unsigned int page4_len)
  228. {
  229. int index, len;
  230. for (;;) {
  231. index = *seq++;
  232. len = *seq++;
  233. switch (len) {
  234. case END_OF_SEQUENCE:
  235. return;
  236. case LOAD_PAGE4:
  237. reg_w_page(gspca_dev, page4, page4_len);
  238. break;
  239. default:
  240. if (len > USB_BUF_SZ) {
  241. PDEBUG(D_ERR|D_STREAM,
  242. "Incorrect variable sequence");
  243. return;
  244. }
  245. while (len > 0) {
  246. if (len < 8) {
  247. reg_w_buf(gspca_dev,
  248. index, seq, len);
  249. seq += len;
  250. break;
  251. }
  252. reg_w_buf(gspca_dev, index, seq, 8);
  253. seq += 8;
  254. index += 8;
  255. len -= 8;
  256. }
  257. }
  258. }
  259. /* not reached */
  260. }
  261. /* this function is called at probe time for pac7311 */
  262. static int sd_config(struct gspca_dev *gspca_dev,
  263. const struct usb_device_id *id)
  264. {
  265. struct cam *cam = &gspca_dev->cam;
  266. cam->cam_mode = vga_mode;
  267. cam->nmodes = ARRAY_SIZE(vga_mode);
  268. return 0;
  269. }
  270. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  271. {
  272. reg_w(gspca_dev, 0xff, 0x04);
  273. reg_w(gspca_dev, 0x10, val);
  274. /* load registers to sensor (Bit 0, auto clear) */
  275. reg_w(gspca_dev, 0x11, 0x01);
  276. }
  277. static void setgain(struct gspca_dev *gspca_dev, s32 val)
  278. {
  279. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  280. reg_w(gspca_dev, 0x0e, 0x00);
  281. reg_w(gspca_dev, 0x0f, gspca_dev->gain->maximum - val + 1);
  282. /* load registers to sensor (Bit 0, auto clear) */
  283. reg_w(gspca_dev, 0x11, 0x01);
  284. }
  285. static void setexposure(struct gspca_dev *gspca_dev, s32 val)
  286. {
  287. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  288. reg_w(gspca_dev, 0x02, val);
  289. /* load registers to sensor (Bit 0, auto clear) */
  290. reg_w(gspca_dev, 0x11, 0x01);
  291. /*
  292. * Page 1 register 8 must always be 0x08 except when not in
  293. * 640x480 mode and page 4 reg 2 <= 3 then it must be 9
  294. */
  295. reg_w(gspca_dev, 0xff, 0x01);
  296. if (gspca_dev->width != 640 && val <= 3)
  297. reg_w(gspca_dev, 0x08, 0x09);
  298. else
  299. reg_w(gspca_dev, 0x08, 0x08);
  300. /*
  301. * Page1 register 80 sets the compression balance, normally we
  302. * want / use 0x1c, but for 640x480@30fps we must allow the
  303. * camera to use higher compression or we may run out of
  304. * bandwidth.
  305. */
  306. if (gspca_dev->width == 640 && val == 2)
  307. reg_w(gspca_dev, 0x80, 0x01);
  308. else
  309. reg_w(gspca_dev, 0x80, 0x1c);
  310. /* load registers to sensor (Bit 0, auto clear) */
  311. reg_w(gspca_dev, 0x11, 0x01);
  312. }
  313. static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
  314. {
  315. __u8 data;
  316. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  317. data = (hflip ? 0x04 : 0x00) |
  318. (vflip ? 0x08 : 0x00);
  319. reg_w(gspca_dev, 0x21, data);
  320. /* load registers to sensor (Bit 0, auto clear) */
  321. reg_w(gspca_dev, 0x11, 0x01);
  322. }
  323. /* this function is called at probe and resume time for pac7311 */
  324. static int sd_init(struct gspca_dev *gspca_dev)
  325. {
  326. reg_w_seq(gspca_dev, init_7311, sizeof(init_7311)/2);
  327. return gspca_dev->usb_err;
  328. }
  329. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  330. {
  331. struct gspca_dev *gspca_dev =
  332. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  333. struct sd *sd = (struct sd *)gspca_dev;
  334. gspca_dev->usb_err = 0;
  335. if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
  336. /* when switching to autogain set defaults to make sure
  337. we are on a valid point of the autogain gain /
  338. exposure knee graph, and give this change time to
  339. take effect before doing autogain. */
  340. gspca_dev->exposure->val = PAC7311_EXPOSURE_DEFAULT;
  341. gspca_dev->gain->val = PAC7311_GAIN_DEFAULT;
  342. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  343. }
  344. if (!gspca_dev->streaming)
  345. return 0;
  346. switch (ctrl->id) {
  347. case V4L2_CID_CONTRAST:
  348. setcontrast(gspca_dev, ctrl->val);
  349. break;
  350. case V4L2_CID_AUTOGAIN:
  351. if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
  352. setexposure(gspca_dev, gspca_dev->exposure->val);
  353. if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
  354. setgain(gspca_dev, gspca_dev->gain->val);
  355. break;
  356. case V4L2_CID_HFLIP:
  357. sethvflip(gspca_dev, sd->hflip->val, sd->vflip->val);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. return gspca_dev->usb_err;
  363. }
  364. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  365. .s_ctrl = sd_s_ctrl,
  366. };
  367. /* this function is called at probe time */
  368. static int sd_init_controls(struct gspca_dev *gspca_dev)
  369. {
  370. struct sd *sd = (struct sd *) gspca_dev;
  371. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  372. gspca_dev->vdev.ctrl_handler = hdl;
  373. v4l2_ctrl_handler_init(hdl, 4);
  374. sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  375. V4L2_CID_CONTRAST, 0, 15, 1, 7);
  376. gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  377. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  378. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  379. V4L2_CID_EXPOSURE, 2, 63, 1,
  380. PAC7311_EXPOSURE_DEFAULT);
  381. gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  382. V4L2_CID_GAIN, 0, 244, 1,
  383. PAC7311_GAIN_DEFAULT);
  384. sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  385. V4L2_CID_HFLIP, 0, 1, 1, 0);
  386. sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  387. V4L2_CID_VFLIP, 0, 1, 1, 0);
  388. if (hdl->error) {
  389. pr_err("Could not initialize controls\n");
  390. return hdl->error;
  391. }
  392. v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
  393. v4l2_ctrl_cluster(2, &sd->hflip);
  394. return 0;
  395. }
  396. /* -- start the camera -- */
  397. static int sd_start(struct gspca_dev *gspca_dev)
  398. {
  399. struct sd *sd = (struct sd *) gspca_dev;
  400. sd->sof_read = 0;
  401. reg_w_var(gspca_dev, start_7311,
  402. page4_7311, sizeof(page4_7311));
  403. setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
  404. setgain(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->gain));
  405. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
  406. sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
  407. v4l2_ctrl_g_ctrl(sd->vflip));
  408. /* set correct resolution */
  409. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  410. case 2: /* 160x120 */
  411. reg_w(gspca_dev, 0xff, 0x01);
  412. reg_w(gspca_dev, 0x17, 0x20);
  413. reg_w(gspca_dev, 0x87, 0x10);
  414. break;
  415. case 1: /* 320x240 */
  416. reg_w(gspca_dev, 0xff, 0x01);
  417. reg_w(gspca_dev, 0x17, 0x30);
  418. reg_w(gspca_dev, 0x87, 0x11);
  419. break;
  420. case 0: /* 640x480 */
  421. reg_w(gspca_dev, 0xff, 0x01);
  422. reg_w(gspca_dev, 0x17, 0x00);
  423. reg_w(gspca_dev, 0x87, 0x12);
  424. break;
  425. }
  426. sd->sof_read = 0;
  427. sd->autogain_ignore_frames = 0;
  428. atomic_set(&sd->avg_lum, -1);
  429. /* start stream */
  430. reg_w(gspca_dev, 0xff, 0x01);
  431. reg_w(gspca_dev, 0x78, 0x05);
  432. return gspca_dev->usb_err;
  433. }
  434. static void sd_stopN(struct gspca_dev *gspca_dev)
  435. {
  436. reg_w(gspca_dev, 0xff, 0x04);
  437. reg_w(gspca_dev, 0x27, 0x80);
  438. reg_w(gspca_dev, 0x28, 0xca);
  439. reg_w(gspca_dev, 0x29, 0x53);
  440. reg_w(gspca_dev, 0x2a, 0x0e);
  441. reg_w(gspca_dev, 0xff, 0x01);
  442. reg_w(gspca_dev, 0x3e, 0x20);
  443. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  444. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  445. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  446. }
  447. static void do_autogain(struct gspca_dev *gspca_dev)
  448. {
  449. struct sd *sd = (struct sd *) gspca_dev;
  450. int avg_lum = atomic_read(&sd->avg_lum);
  451. int desired_lum, deadzone;
  452. if (avg_lum == -1)
  453. return;
  454. desired_lum = 200;
  455. deadzone = 20;
  456. if (sd->autogain_ignore_frames > 0)
  457. sd->autogain_ignore_frames--;
  458. else if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
  459. desired_lum, deadzone))
  460. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  461. }
  462. /* JPEG header, part 1 */
  463. static const unsigned char pac_jpeg_header1[] = {
  464. 0xff, 0xd8, /* SOI: Start of Image */
  465. 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
  466. 0x00, 0x11, /* length = 17 bytes (including this length field) */
  467. 0x08 /* Precision: 8 */
  468. /* 2 bytes is placed here: number of image lines */
  469. /* 2 bytes is placed here: samples per line */
  470. };
  471. /* JPEG header, continued */
  472. static const unsigned char pac_jpeg_header2[] = {
  473. 0x03, /* Number of image components: 3 */
  474. 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
  475. 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
  476. 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
  477. 0xff, 0xda, /* SOS: Start Of Scan */
  478. 0x00, 0x0c, /* length = 12 bytes (including this length field) */
  479. 0x03, /* number of components: 3 */
  480. 0x01, 0x00, /* selector 1, table 0x00 */
  481. 0x02, 0x11, /* selector 2, table 0x11 */
  482. 0x03, 0x11, /* selector 3, table 0x11 */
  483. 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
  484. 0x00 /* Successive approximation: 0 */
  485. };
  486. static void pac_start_frame(struct gspca_dev *gspca_dev,
  487. __u16 lines, __u16 samples_per_line)
  488. {
  489. unsigned char tmpbuf[4];
  490. gspca_frame_add(gspca_dev, FIRST_PACKET,
  491. pac_jpeg_header1, sizeof(pac_jpeg_header1));
  492. tmpbuf[0] = lines >> 8;
  493. tmpbuf[1] = lines & 0xff;
  494. tmpbuf[2] = samples_per_line >> 8;
  495. tmpbuf[3] = samples_per_line & 0xff;
  496. gspca_frame_add(gspca_dev, INTER_PACKET,
  497. tmpbuf, sizeof(tmpbuf));
  498. gspca_frame_add(gspca_dev, INTER_PACKET,
  499. pac_jpeg_header2, sizeof(pac_jpeg_header2));
  500. }
  501. /* this function is run at interrupt level */
  502. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  503. u8 *data, /* isoc packet */
  504. int len) /* iso packet length */
  505. {
  506. struct sd *sd = (struct sd *) gspca_dev;
  507. u8 *image;
  508. unsigned char *sof;
  509. sof = pac_find_sof(&sd->sof_read, data, len);
  510. if (sof) {
  511. int n, lum_offset, footer_length;
  512. /*
  513. * 6 bytes after the FF D9 EOF marker a number of lumination
  514. * bytes are send corresponding to different parts of the
  515. * image, the 14th and 15th byte after the EOF seem to
  516. * correspond to the center of the image.
  517. */
  518. lum_offset = 24 + sizeof pac_sof_marker;
  519. footer_length = 26;
  520. /* Finish decoding current frame */
  521. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  522. if (n < 0) {
  523. gspca_dev->image_len += n;
  524. n = 0;
  525. } else {
  526. gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
  527. }
  528. image = gspca_dev->image;
  529. if (image != NULL
  530. && image[gspca_dev->image_len - 2] == 0xff
  531. && image[gspca_dev->image_len - 1] == 0xd9)
  532. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  533. n = sof - data;
  534. len -= n;
  535. data = sof;
  536. /* Get average lumination */
  537. if (gspca_dev->last_packet_type == LAST_PACKET &&
  538. n >= lum_offset)
  539. atomic_set(&sd->avg_lum, data[-lum_offset] +
  540. data[-lum_offset + 1]);
  541. else
  542. atomic_set(&sd->avg_lum, -1);
  543. /* Start the new frame with the jpeg header */
  544. pac_start_frame(gspca_dev,
  545. gspca_dev->height, gspca_dev->width);
  546. }
  547. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  548. }
  549. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  550. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  551. u8 *data, /* interrupt packet data */
  552. int len) /* interrupt packet length */
  553. {
  554. int ret = -EINVAL;
  555. u8 data0, data1;
  556. if (len == 2) {
  557. data0 = data[0];
  558. data1 = data[1];
  559. if ((data0 == 0x00 && data1 == 0x11) ||
  560. (data0 == 0x22 && data1 == 0x33) ||
  561. (data0 == 0x44 && data1 == 0x55) ||
  562. (data0 == 0x66 && data1 == 0x77) ||
  563. (data0 == 0x88 && data1 == 0x99) ||
  564. (data0 == 0xaa && data1 == 0xbb) ||
  565. (data0 == 0xcc && data1 == 0xdd) ||
  566. (data0 == 0xee && data1 == 0xff)) {
  567. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  568. input_sync(gspca_dev->input_dev);
  569. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  570. input_sync(gspca_dev->input_dev);
  571. ret = 0;
  572. }
  573. }
  574. return ret;
  575. }
  576. #endif
  577. static const struct sd_desc sd_desc = {
  578. .name = MODULE_NAME,
  579. .config = sd_config,
  580. .init = sd_init,
  581. .init_controls = sd_init_controls,
  582. .start = sd_start,
  583. .stopN = sd_stopN,
  584. .pkt_scan = sd_pkt_scan,
  585. .dq_callback = do_autogain,
  586. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  587. .int_pkt_scan = sd_int_pkt_scan,
  588. #endif
  589. };
  590. /* -- module initialisation -- */
  591. static const struct usb_device_id device_table[] = {
  592. {USB_DEVICE(0x093a, 0x2600)},
  593. {USB_DEVICE(0x093a, 0x2601)},
  594. {USB_DEVICE(0x093a, 0x2603)},
  595. {USB_DEVICE(0x093a, 0x2608)},
  596. {USB_DEVICE(0x093a, 0x260e)},
  597. {USB_DEVICE(0x093a, 0x260f)},
  598. {}
  599. };
  600. MODULE_DEVICE_TABLE(usb, device_table);
  601. /* -- device connect -- */
  602. static int sd_probe(struct usb_interface *intf,
  603. const struct usb_device_id *id)
  604. {
  605. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  606. THIS_MODULE);
  607. }
  608. static struct usb_driver sd_driver = {
  609. .name = MODULE_NAME,
  610. .id_table = device_table,
  611. .probe = sd_probe,
  612. .disconnect = gspca_disconnect,
  613. #ifdef CONFIG_PM
  614. .suspend = gspca_suspend,
  615. .resume = gspca_resume,
  616. #endif
  617. };
  618. module_usb_driver(sd_driver);