pac7311.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. * Note setting vflip disabled leads to a much lower image quality,
  53. * so we always vflip, and tell userspace to flip it back
  54. * 0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
  55. * completely disable the analog amplification block. Set to 0x68
  56. * for max gain, 0x14 for minimal gain.
  57. */
  58. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  59. #define MODULE_NAME "pac7311"
  60. #include <linux/input.h>
  61. #include "gspca.h"
  62. /* Include pac common sof detection functions */
  63. #include "pac_common.h"
  64. #define PAC7311_GAIN_DEFAULT 122
  65. #define PAC7311_EXPOSURE_DEFAULT 3 /* 20 fps, avoid using high compr. */
  66. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  67. MODULE_DESCRIPTION("Pixart PAC7311");
  68. MODULE_LICENSE("GPL");
  69. struct sd {
  70. struct gspca_dev gspca_dev; /* !! must be the first item */
  71. struct v4l2_ctrl *contrast;
  72. struct v4l2_ctrl *hflip;
  73. u8 sof_read;
  74. u8 autogain_ignore_frames;
  75. atomic_t avg_lum;
  76. };
  77. static const struct v4l2_pix_format vga_mode[] = {
  78. {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  79. .bytesperline = 160,
  80. .sizeimage = 160 * 120 * 3 / 8 + 590,
  81. .colorspace = V4L2_COLORSPACE_JPEG,
  82. .priv = 2},
  83. {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  84. .bytesperline = 320,
  85. .sizeimage = 320 * 240 * 3 / 8 + 590,
  86. .colorspace = V4L2_COLORSPACE_JPEG,
  87. .priv = 1},
  88. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  89. .bytesperline = 640,
  90. .sizeimage = 640 * 480 * 3 / 8 + 590,
  91. .colorspace = V4L2_COLORSPACE_JPEG,
  92. .priv = 0},
  93. };
  94. #define LOAD_PAGE4 254
  95. #define END_OF_SEQUENCE 0
  96. static const __u8 init_7311[] = {
  97. 0xff, 0x01,
  98. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  99. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  100. 0x78, 0x44, /* Bit_0=start stream, Bit_6=LED */
  101. 0xff, 0x04,
  102. 0x27, 0x80,
  103. 0x28, 0xca,
  104. 0x29, 0x53,
  105. 0x2a, 0x0e,
  106. 0xff, 0x01,
  107. 0x3e, 0x20,
  108. };
  109. static const __u8 start_7311[] = {
  110. /* index, len, [value]* */
  111. 0xff, 1, 0x01, /* page 1 */
  112. 0x02, 43, 0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
  113. 0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
  114. 0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
  115. 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00,
  118. 0x3e, 42, 0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
  119. 0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
  120. 0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
  121. 0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
  122. 0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
  123. 0xd0, 0xff,
  124. 0x78, 6, 0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
  125. 0x7f, 18, 0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
  126. 0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
  127. 0x18, 0x20,
  128. 0x96, 3, 0x01, 0x08, 0x04,
  129. 0xa0, 4, 0x44, 0x44, 0x44, 0x04,
  130. 0xf0, 13, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
  131. 0x3f, 0x00, 0x0a, 0x01, 0x00,
  132. 0xff, 1, 0x04, /* page 4 */
  133. 0, LOAD_PAGE4, /* load the page 4 */
  134. 0x11, 1, 0x01,
  135. 0, END_OF_SEQUENCE /* end of sequence */
  136. };
  137. #define SKIP 0xaa
  138. /* page 4 - the value SKIP says skip the index - see reg_w_page() */
  139. static const __u8 page4_7311[] = {
  140. SKIP, SKIP, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
  141. 0x09, 0x00, SKIP, SKIP, 0x07, 0x00, 0x00, 0x62,
  142. 0x08, SKIP, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, SKIP,
  144. SKIP, 0x00, 0x08, SKIP, 0x03, SKIP, 0x00, 0x68,
  145. 0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
  146. 0x23, 0x28, 0x04, 0x11, 0x00, 0x00
  147. };
  148. static void reg_w_buf(struct gspca_dev *gspca_dev,
  149. __u8 index,
  150. const u8 *buffer, int len)
  151. {
  152. int ret;
  153. if (gspca_dev->usb_err < 0)
  154. return;
  155. memcpy(gspca_dev->usb_buf, buffer, len);
  156. ret = usb_control_msg(gspca_dev->dev,
  157. usb_sndctrlpipe(gspca_dev->dev, 0),
  158. 0, /* request */
  159. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  160. 0, /* value */
  161. index, gspca_dev->usb_buf, len,
  162. 500);
  163. if (ret < 0) {
  164. pr_err("reg_w_buf() failed index 0x%02x, error %d\n",
  165. index, ret);
  166. gspca_dev->usb_err = ret;
  167. }
  168. }
  169. static void reg_w(struct gspca_dev *gspca_dev,
  170. __u8 index,
  171. __u8 value)
  172. {
  173. int ret;
  174. if (gspca_dev->usb_err < 0)
  175. return;
  176. gspca_dev->usb_buf[0] = value;
  177. ret = usb_control_msg(gspca_dev->dev,
  178. usb_sndctrlpipe(gspca_dev->dev, 0),
  179. 0, /* request */
  180. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  181. 0, index, gspca_dev->usb_buf, 1,
  182. 500);
  183. if (ret < 0) {
  184. pr_err("reg_w() failed index 0x%02x, value 0x%02x, error %d\n",
  185. index, value, ret);
  186. gspca_dev->usb_err = ret;
  187. }
  188. }
  189. static void reg_w_seq(struct gspca_dev *gspca_dev,
  190. const __u8 *seq, int len)
  191. {
  192. while (--len >= 0) {
  193. reg_w(gspca_dev, seq[0], seq[1]);
  194. seq += 2;
  195. }
  196. }
  197. /* load the beginning of a page */
  198. static void reg_w_page(struct gspca_dev *gspca_dev,
  199. const __u8 *page, int len)
  200. {
  201. int index;
  202. int ret = 0;
  203. if (gspca_dev->usb_err < 0)
  204. return;
  205. for (index = 0; index < len; index++) {
  206. if (page[index] == SKIP) /* skip this index */
  207. continue;
  208. gspca_dev->usb_buf[0] = page[index];
  209. ret = usb_control_msg(gspca_dev->dev,
  210. usb_sndctrlpipe(gspca_dev->dev, 0),
  211. 0, /* request */
  212. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  213. 0, index, gspca_dev->usb_buf, 1,
  214. 500);
  215. if (ret < 0) {
  216. pr_err("reg_w_page() failed index 0x%02x, value 0x%02x, error %d\n",
  217. index, page[index], ret);
  218. gspca_dev->usb_err = ret;
  219. break;
  220. }
  221. }
  222. }
  223. /* output a variable sequence */
  224. static void reg_w_var(struct gspca_dev *gspca_dev,
  225. const __u8 *seq,
  226. const __u8 *page4, unsigned int page4_len)
  227. {
  228. int index, len;
  229. for (;;) {
  230. index = *seq++;
  231. len = *seq++;
  232. switch (len) {
  233. case END_OF_SEQUENCE:
  234. return;
  235. case LOAD_PAGE4:
  236. reg_w_page(gspca_dev, page4, page4_len);
  237. break;
  238. default:
  239. if (len > USB_BUF_SZ) {
  240. PDEBUG(D_ERR|D_STREAM,
  241. "Incorrect variable sequence");
  242. return;
  243. }
  244. while (len > 0) {
  245. if (len < 8) {
  246. reg_w_buf(gspca_dev,
  247. index, seq, len);
  248. seq += len;
  249. break;
  250. }
  251. reg_w_buf(gspca_dev, index, seq, 8);
  252. seq += 8;
  253. index += 8;
  254. len -= 8;
  255. }
  256. }
  257. }
  258. /* not reached */
  259. }
  260. /* this function is called at probe time for pac7311 */
  261. static int sd_config(struct gspca_dev *gspca_dev,
  262. const struct usb_device_id *id)
  263. {
  264. struct cam *cam = &gspca_dev->cam;
  265. cam->cam_mode = vga_mode;
  266. cam->nmodes = ARRAY_SIZE(vga_mode);
  267. cam->input_flags = V4L2_IN_ST_VFLIP;
  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, 1);
  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, 5);
  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. if (hdl->error) {
  387. pr_err("Could not initialize controls\n");
  388. return hdl->error;
  389. }
  390. v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
  391. return 0;
  392. }
  393. /* -- start the camera -- */
  394. static int sd_start(struct gspca_dev *gspca_dev)
  395. {
  396. struct sd *sd = (struct sd *) gspca_dev;
  397. sd->sof_read = 0;
  398. reg_w_var(gspca_dev, start_7311,
  399. page4_7311, sizeof(page4_7311));
  400. setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
  401. setgain(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->gain));
  402. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
  403. sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip), 1);
  404. /* set correct resolution */
  405. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  406. case 2: /* 160x120 */
  407. reg_w(gspca_dev, 0xff, 0x01);
  408. reg_w(gspca_dev, 0x17, 0x20);
  409. reg_w(gspca_dev, 0x87, 0x10);
  410. break;
  411. case 1: /* 320x240 */
  412. reg_w(gspca_dev, 0xff, 0x01);
  413. reg_w(gspca_dev, 0x17, 0x30);
  414. reg_w(gspca_dev, 0x87, 0x11);
  415. break;
  416. case 0: /* 640x480 */
  417. reg_w(gspca_dev, 0xff, 0x01);
  418. reg_w(gspca_dev, 0x17, 0x00);
  419. reg_w(gspca_dev, 0x87, 0x12);
  420. break;
  421. }
  422. sd->sof_read = 0;
  423. sd->autogain_ignore_frames = 0;
  424. atomic_set(&sd->avg_lum, -1);
  425. /* start stream */
  426. reg_w(gspca_dev, 0xff, 0x01);
  427. reg_w(gspca_dev, 0x78, 0x05);
  428. return gspca_dev->usb_err;
  429. }
  430. static void sd_stopN(struct gspca_dev *gspca_dev)
  431. {
  432. reg_w(gspca_dev, 0xff, 0x04);
  433. reg_w(gspca_dev, 0x27, 0x80);
  434. reg_w(gspca_dev, 0x28, 0xca);
  435. reg_w(gspca_dev, 0x29, 0x53);
  436. reg_w(gspca_dev, 0x2a, 0x0e);
  437. reg_w(gspca_dev, 0xff, 0x01);
  438. reg_w(gspca_dev, 0x3e, 0x20);
  439. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  440. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  441. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  442. }
  443. static void do_autogain(struct gspca_dev *gspca_dev)
  444. {
  445. struct sd *sd = (struct sd *) gspca_dev;
  446. int avg_lum = atomic_read(&sd->avg_lum);
  447. int desired_lum, deadzone;
  448. if (avg_lum == -1)
  449. return;
  450. desired_lum = 170;
  451. deadzone = 20;
  452. if (sd->autogain_ignore_frames > 0)
  453. sd->autogain_ignore_frames--;
  454. else if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
  455. desired_lum, deadzone))
  456. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  457. }
  458. /* JPEG header, part 1 */
  459. static const unsigned char pac_jpeg_header1[] = {
  460. 0xff, 0xd8, /* SOI: Start of Image */
  461. 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
  462. 0x00, 0x11, /* length = 17 bytes (including this length field) */
  463. 0x08 /* Precision: 8 */
  464. /* 2 bytes is placed here: number of image lines */
  465. /* 2 bytes is placed here: samples per line */
  466. };
  467. /* JPEG header, continued */
  468. static const unsigned char pac_jpeg_header2[] = {
  469. 0x03, /* Number of image components: 3 */
  470. 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
  471. 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
  472. 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
  473. 0xff, 0xda, /* SOS: Start Of Scan */
  474. 0x00, 0x0c, /* length = 12 bytes (including this length field) */
  475. 0x03, /* number of components: 3 */
  476. 0x01, 0x00, /* selector 1, table 0x00 */
  477. 0x02, 0x11, /* selector 2, table 0x11 */
  478. 0x03, 0x11, /* selector 3, table 0x11 */
  479. 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
  480. 0x00 /* Successive approximation: 0 */
  481. };
  482. static void pac_start_frame(struct gspca_dev *gspca_dev,
  483. __u16 lines, __u16 samples_per_line)
  484. {
  485. unsigned char tmpbuf[4];
  486. gspca_frame_add(gspca_dev, FIRST_PACKET,
  487. pac_jpeg_header1, sizeof(pac_jpeg_header1));
  488. tmpbuf[0] = lines >> 8;
  489. tmpbuf[1] = lines & 0xff;
  490. tmpbuf[2] = samples_per_line >> 8;
  491. tmpbuf[3] = samples_per_line & 0xff;
  492. gspca_frame_add(gspca_dev, INTER_PACKET,
  493. tmpbuf, sizeof(tmpbuf));
  494. gspca_frame_add(gspca_dev, INTER_PACKET,
  495. pac_jpeg_header2, sizeof(pac_jpeg_header2));
  496. }
  497. /* this function is run at interrupt level */
  498. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  499. u8 *data, /* isoc packet */
  500. int len) /* iso packet length */
  501. {
  502. struct sd *sd = (struct sd *) gspca_dev;
  503. u8 *image;
  504. unsigned char *sof;
  505. sof = pac_find_sof(&sd->sof_read, data, len);
  506. if (sof) {
  507. int n, lum_offset, footer_length;
  508. /*
  509. * 6 bytes after the FF D9 EOF marker a number of lumination
  510. * bytes are send corresponding to different parts of the
  511. * image, the 14th and 15th byte after the EOF seem to
  512. * correspond to the center of the image.
  513. */
  514. lum_offset = 24 + sizeof pac_sof_marker;
  515. footer_length = 26;
  516. /* Finish decoding current frame */
  517. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  518. if (n < 0) {
  519. gspca_dev->image_len += n;
  520. n = 0;
  521. } else {
  522. gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
  523. }
  524. image = gspca_dev->image;
  525. if (image != NULL
  526. && image[gspca_dev->image_len - 2] == 0xff
  527. && image[gspca_dev->image_len - 1] == 0xd9)
  528. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  529. n = sof - data;
  530. len -= n;
  531. data = sof;
  532. /* Get average lumination */
  533. if (gspca_dev->last_packet_type == LAST_PACKET &&
  534. n >= lum_offset)
  535. atomic_set(&sd->avg_lum, data[-lum_offset] +
  536. data[-lum_offset + 1]);
  537. else
  538. atomic_set(&sd->avg_lum, -1);
  539. /* Start the new frame with the jpeg header */
  540. pac_start_frame(gspca_dev,
  541. gspca_dev->height, gspca_dev->width);
  542. }
  543. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  544. }
  545. #if IS_ENABLED(CONFIG_INPUT)
  546. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  547. u8 *data, /* interrupt packet data */
  548. int len) /* interrupt packet length */
  549. {
  550. int ret = -EINVAL;
  551. u8 data0, data1;
  552. if (len == 2) {
  553. data0 = data[0];
  554. data1 = data[1];
  555. if ((data0 == 0x00 && data1 == 0x11) ||
  556. (data0 == 0x22 && data1 == 0x33) ||
  557. (data0 == 0x44 && data1 == 0x55) ||
  558. (data0 == 0x66 && data1 == 0x77) ||
  559. (data0 == 0x88 && data1 == 0x99) ||
  560. (data0 == 0xaa && data1 == 0xbb) ||
  561. (data0 == 0xcc && data1 == 0xdd) ||
  562. (data0 == 0xee && data1 == 0xff)) {
  563. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  564. input_sync(gspca_dev->input_dev);
  565. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  566. input_sync(gspca_dev->input_dev);
  567. ret = 0;
  568. }
  569. }
  570. return ret;
  571. }
  572. #endif
  573. static const struct sd_desc sd_desc = {
  574. .name = MODULE_NAME,
  575. .config = sd_config,
  576. .init = sd_init,
  577. .init_controls = sd_init_controls,
  578. .start = sd_start,
  579. .stopN = sd_stopN,
  580. .pkt_scan = sd_pkt_scan,
  581. .dq_callback = do_autogain,
  582. #if IS_ENABLED(CONFIG_INPUT)
  583. .int_pkt_scan = sd_int_pkt_scan,
  584. #endif
  585. };
  586. /* -- module initialisation -- */
  587. static const struct usb_device_id device_table[] = {
  588. {USB_DEVICE(0x093a, 0x2600)},
  589. {USB_DEVICE(0x093a, 0x2601)},
  590. {USB_DEVICE(0x093a, 0x2603)},
  591. {USB_DEVICE(0x093a, 0x2608)},
  592. {USB_DEVICE(0x093a, 0x260e)},
  593. {USB_DEVICE(0x093a, 0x260f)},
  594. {}
  595. };
  596. MODULE_DEVICE_TABLE(usb, device_table);
  597. /* -- device connect -- */
  598. static int sd_probe(struct usb_interface *intf,
  599. const struct usb_device_id *id)
  600. {
  601. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  602. THIS_MODULE);
  603. }
  604. static struct usb_driver sd_driver = {
  605. .name = MODULE_NAME,
  606. .id_table = device_table,
  607. .probe = sd_probe,
  608. .disconnect = gspca_disconnect,
  609. #ifdef CONFIG_PM
  610. .suspend = gspca_suspend,
  611. .resume = gspca_resume,
  612. .reset_resume = gspca_resume,
  613. #endif
  614. };
  615. module_usb_driver(sd_driver);