pac7311.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. PERR("Incorrect variable sequence");
  241. return;
  242. }
  243. while (len > 0) {
  244. if (len < 8) {
  245. reg_w_buf(gspca_dev,
  246. index, seq, len);
  247. seq += len;
  248. break;
  249. }
  250. reg_w_buf(gspca_dev, index, seq, 8);
  251. seq += 8;
  252. index += 8;
  253. len -= 8;
  254. }
  255. }
  256. }
  257. /* not reached */
  258. }
  259. /* this function is called at probe time for pac7311 */
  260. static int sd_config(struct gspca_dev *gspca_dev,
  261. const struct usb_device_id *id)
  262. {
  263. struct cam *cam = &gspca_dev->cam;
  264. cam->cam_mode = vga_mode;
  265. cam->nmodes = ARRAY_SIZE(vga_mode);
  266. cam->input_flags = V4L2_IN_ST_VFLIP;
  267. return 0;
  268. }
  269. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  270. {
  271. reg_w(gspca_dev, 0xff, 0x04);
  272. reg_w(gspca_dev, 0x10, val);
  273. /* load registers to sensor (Bit 0, auto clear) */
  274. reg_w(gspca_dev, 0x11, 0x01);
  275. }
  276. static void setgain(struct gspca_dev *gspca_dev, s32 val)
  277. {
  278. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  279. reg_w(gspca_dev, 0x0e, 0x00);
  280. reg_w(gspca_dev, 0x0f, gspca_dev->gain->maximum - val + 1);
  281. /* load registers to sensor (Bit 0, auto clear) */
  282. reg_w(gspca_dev, 0x11, 0x01);
  283. }
  284. static void setexposure(struct gspca_dev *gspca_dev, s32 val)
  285. {
  286. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  287. reg_w(gspca_dev, 0x02, val);
  288. /* load registers to sensor (Bit 0, auto clear) */
  289. reg_w(gspca_dev, 0x11, 0x01);
  290. /*
  291. * Page 1 register 8 must always be 0x08 except when not in
  292. * 640x480 mode and page 4 reg 2 <= 3 then it must be 9
  293. */
  294. reg_w(gspca_dev, 0xff, 0x01);
  295. if (gspca_dev->pixfmt.width != 640 && val <= 3)
  296. reg_w(gspca_dev, 0x08, 0x09);
  297. else
  298. reg_w(gspca_dev, 0x08, 0x08);
  299. /*
  300. * Page1 register 80 sets the compression balance, normally we
  301. * want / use 0x1c, but for 640x480@30fps we must allow the
  302. * camera to use higher compression or we may run out of
  303. * bandwidth.
  304. */
  305. if (gspca_dev->pixfmt.width == 640 && val == 2)
  306. reg_w(gspca_dev, 0x80, 0x01);
  307. else
  308. reg_w(gspca_dev, 0x80, 0x1c);
  309. /* load registers to sensor (Bit 0, auto clear) */
  310. reg_w(gspca_dev, 0x11, 0x01);
  311. }
  312. static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
  313. {
  314. __u8 data;
  315. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  316. data = (hflip ? 0x04 : 0x00) |
  317. (vflip ? 0x08 : 0x00);
  318. reg_w(gspca_dev, 0x21, data);
  319. /* load registers to sensor (Bit 0, auto clear) */
  320. reg_w(gspca_dev, 0x11, 0x01);
  321. }
  322. /* this function is called at probe and resume time for pac7311 */
  323. static int sd_init(struct gspca_dev *gspca_dev)
  324. {
  325. reg_w_seq(gspca_dev, init_7311, sizeof(init_7311)/2);
  326. return gspca_dev->usb_err;
  327. }
  328. static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
  329. {
  330. struct gspca_dev *gspca_dev =
  331. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  332. struct sd *sd = (struct sd *)gspca_dev;
  333. gspca_dev->usb_err = 0;
  334. if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
  335. /* when switching to autogain set defaults to make sure
  336. we are on a valid point of the autogain gain /
  337. exposure knee graph, and give this change time to
  338. take effect before doing autogain. */
  339. gspca_dev->exposure->val = PAC7311_EXPOSURE_DEFAULT;
  340. gspca_dev->gain->val = PAC7311_GAIN_DEFAULT;
  341. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  342. }
  343. if (!gspca_dev->streaming)
  344. return 0;
  345. switch (ctrl->id) {
  346. case V4L2_CID_CONTRAST:
  347. setcontrast(gspca_dev, ctrl->val);
  348. break;
  349. case V4L2_CID_AUTOGAIN:
  350. if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
  351. setexposure(gspca_dev, gspca_dev->exposure->val);
  352. if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
  353. setgain(gspca_dev, gspca_dev->gain->val);
  354. break;
  355. case V4L2_CID_HFLIP:
  356. sethvflip(gspca_dev, sd->hflip->val, 1);
  357. break;
  358. default:
  359. return -EINVAL;
  360. }
  361. return gspca_dev->usb_err;
  362. }
  363. static const struct v4l2_ctrl_ops sd_ctrl_ops = {
  364. .s_ctrl = sd_s_ctrl,
  365. };
  366. /* this function is called at probe time */
  367. static int sd_init_controls(struct gspca_dev *gspca_dev)
  368. {
  369. struct sd *sd = (struct sd *) gspca_dev;
  370. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  371. gspca_dev->vdev.ctrl_handler = hdl;
  372. v4l2_ctrl_handler_init(hdl, 5);
  373. sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  374. V4L2_CID_CONTRAST, 0, 15, 1, 7);
  375. gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  376. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  377. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  378. V4L2_CID_EXPOSURE, 2, 63, 1,
  379. PAC7311_EXPOSURE_DEFAULT);
  380. gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  381. V4L2_CID_GAIN, 0, 244, 1,
  382. PAC7311_GAIN_DEFAULT);
  383. sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
  384. V4L2_CID_HFLIP, 0, 1, 1, 0);
  385. if (hdl->error) {
  386. pr_err("Could not initialize controls\n");
  387. return hdl->error;
  388. }
  389. v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
  390. return 0;
  391. }
  392. /* -- start the camera -- */
  393. static int sd_start(struct gspca_dev *gspca_dev)
  394. {
  395. struct sd *sd = (struct sd *) gspca_dev;
  396. sd->sof_read = 0;
  397. reg_w_var(gspca_dev, start_7311,
  398. page4_7311, sizeof(page4_7311));
  399. setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
  400. setgain(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->gain));
  401. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(gspca_dev->exposure));
  402. sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip), 1);
  403. /* set correct resolution */
  404. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  405. case 2: /* 160x120 */
  406. reg_w(gspca_dev, 0xff, 0x01);
  407. reg_w(gspca_dev, 0x17, 0x20);
  408. reg_w(gspca_dev, 0x87, 0x10);
  409. break;
  410. case 1: /* 320x240 */
  411. reg_w(gspca_dev, 0xff, 0x01);
  412. reg_w(gspca_dev, 0x17, 0x30);
  413. reg_w(gspca_dev, 0x87, 0x11);
  414. break;
  415. case 0: /* 640x480 */
  416. reg_w(gspca_dev, 0xff, 0x01);
  417. reg_w(gspca_dev, 0x17, 0x00);
  418. reg_w(gspca_dev, 0x87, 0x12);
  419. break;
  420. }
  421. sd->sof_read = 0;
  422. sd->autogain_ignore_frames = 0;
  423. atomic_set(&sd->avg_lum, -1);
  424. /* start stream */
  425. reg_w(gspca_dev, 0xff, 0x01);
  426. reg_w(gspca_dev, 0x78, 0x05);
  427. return gspca_dev->usb_err;
  428. }
  429. static void sd_stopN(struct gspca_dev *gspca_dev)
  430. {
  431. reg_w(gspca_dev, 0xff, 0x04);
  432. reg_w(gspca_dev, 0x27, 0x80);
  433. reg_w(gspca_dev, 0x28, 0xca);
  434. reg_w(gspca_dev, 0x29, 0x53);
  435. reg_w(gspca_dev, 0x2a, 0x0e);
  436. reg_w(gspca_dev, 0xff, 0x01);
  437. reg_w(gspca_dev, 0x3e, 0x20);
  438. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  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. }
  442. static void do_autogain(struct gspca_dev *gspca_dev)
  443. {
  444. struct sd *sd = (struct sd *) gspca_dev;
  445. int avg_lum = atomic_read(&sd->avg_lum);
  446. int desired_lum, deadzone;
  447. if (avg_lum == -1)
  448. return;
  449. desired_lum = 170;
  450. deadzone = 20;
  451. if (sd->autogain_ignore_frames > 0)
  452. sd->autogain_ignore_frames--;
  453. else if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
  454. desired_lum, deadzone))
  455. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  456. }
  457. /* JPEG header, part 1 */
  458. static const unsigned char pac_jpeg_header1[] = {
  459. 0xff, 0xd8, /* SOI: Start of Image */
  460. 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
  461. 0x00, 0x11, /* length = 17 bytes (including this length field) */
  462. 0x08 /* Precision: 8 */
  463. /* 2 bytes is placed here: number of image lines */
  464. /* 2 bytes is placed here: samples per line */
  465. };
  466. /* JPEG header, continued */
  467. static const unsigned char pac_jpeg_header2[] = {
  468. 0x03, /* Number of image components: 3 */
  469. 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
  470. 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
  471. 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
  472. 0xff, 0xda, /* SOS: Start Of Scan */
  473. 0x00, 0x0c, /* length = 12 bytes (including this length field) */
  474. 0x03, /* number of components: 3 */
  475. 0x01, 0x00, /* selector 1, table 0x00 */
  476. 0x02, 0x11, /* selector 2, table 0x11 */
  477. 0x03, 0x11, /* selector 3, table 0x11 */
  478. 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
  479. 0x00 /* Successive approximation: 0 */
  480. };
  481. static void pac_start_frame(struct gspca_dev *gspca_dev,
  482. __u16 lines, __u16 samples_per_line)
  483. {
  484. unsigned char tmpbuf[4];
  485. gspca_frame_add(gspca_dev, FIRST_PACKET,
  486. pac_jpeg_header1, sizeof(pac_jpeg_header1));
  487. tmpbuf[0] = lines >> 8;
  488. tmpbuf[1] = lines & 0xff;
  489. tmpbuf[2] = samples_per_line >> 8;
  490. tmpbuf[3] = samples_per_line & 0xff;
  491. gspca_frame_add(gspca_dev, INTER_PACKET,
  492. tmpbuf, sizeof(tmpbuf));
  493. gspca_frame_add(gspca_dev, INTER_PACKET,
  494. pac_jpeg_header2, sizeof(pac_jpeg_header2));
  495. }
  496. /* this function is run at interrupt level */
  497. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  498. u8 *data, /* isoc packet */
  499. int len) /* iso packet length */
  500. {
  501. struct sd *sd = (struct sd *) gspca_dev;
  502. u8 *image;
  503. unsigned char *sof;
  504. sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
  505. if (sof) {
  506. int n, lum_offset, footer_length;
  507. /*
  508. * 6 bytes after the FF D9 EOF marker a number of lumination
  509. * bytes are send corresponding to different parts of the
  510. * image, the 14th and 15th byte after the EOF seem to
  511. * correspond to the center of the image.
  512. */
  513. lum_offset = 24 + sizeof pac_sof_marker;
  514. footer_length = 26;
  515. /* Finish decoding current frame */
  516. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  517. if (n < 0) {
  518. gspca_dev->image_len += n;
  519. n = 0;
  520. } else {
  521. gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
  522. }
  523. image = gspca_dev->image;
  524. if (image != NULL
  525. && image[gspca_dev->image_len - 2] == 0xff
  526. && image[gspca_dev->image_len - 1] == 0xd9)
  527. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  528. n = sof - data;
  529. len -= n;
  530. data = sof;
  531. /* Get average lumination */
  532. if (gspca_dev->last_packet_type == LAST_PACKET &&
  533. n >= lum_offset)
  534. atomic_set(&sd->avg_lum, data[-lum_offset] +
  535. data[-lum_offset + 1]);
  536. else
  537. atomic_set(&sd->avg_lum, -1);
  538. /* Start the new frame with the jpeg header */
  539. pac_start_frame(gspca_dev,
  540. gspca_dev->pixfmt.height, gspca_dev->pixfmt.width);
  541. }
  542. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  543. }
  544. #if IS_ENABLED(CONFIG_INPUT)
  545. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  546. u8 *data, /* interrupt packet data */
  547. int len) /* interrupt packet length */
  548. {
  549. int ret = -EINVAL;
  550. u8 data0, data1;
  551. if (len == 2) {
  552. data0 = data[0];
  553. data1 = data[1];
  554. if ((data0 == 0x00 && data1 == 0x11) ||
  555. (data0 == 0x22 && data1 == 0x33) ||
  556. (data0 == 0x44 && data1 == 0x55) ||
  557. (data0 == 0x66 && data1 == 0x77) ||
  558. (data0 == 0x88 && data1 == 0x99) ||
  559. (data0 == 0xaa && data1 == 0xbb) ||
  560. (data0 == 0xcc && data1 == 0xdd) ||
  561. (data0 == 0xee && data1 == 0xff)) {
  562. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  563. input_sync(gspca_dev->input_dev);
  564. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  565. input_sync(gspca_dev->input_dev);
  566. ret = 0;
  567. }
  568. }
  569. return ret;
  570. }
  571. #endif
  572. static const struct sd_desc sd_desc = {
  573. .name = MODULE_NAME,
  574. .config = sd_config,
  575. .init = sd_init,
  576. .init_controls = sd_init_controls,
  577. .start = sd_start,
  578. .stopN = sd_stopN,
  579. .pkt_scan = sd_pkt_scan,
  580. .dq_callback = do_autogain,
  581. #if IS_ENABLED(CONFIG_INPUT)
  582. .int_pkt_scan = sd_int_pkt_scan,
  583. #endif
  584. };
  585. /* -- module initialisation -- */
  586. static const struct usb_device_id device_table[] = {
  587. {USB_DEVICE(0x093a, 0x2600)},
  588. {USB_DEVICE(0x093a, 0x2601)},
  589. {USB_DEVICE(0x093a, 0x2603)},
  590. {USB_DEVICE(0x093a, 0x2608)},
  591. {USB_DEVICE(0x093a, 0x260e)},
  592. {USB_DEVICE(0x093a, 0x260f)},
  593. {}
  594. };
  595. MODULE_DEVICE_TABLE(usb, device_table);
  596. /* -- device connect -- */
  597. static int sd_probe(struct usb_interface *intf,
  598. const struct usb_device_id *id)
  599. {
  600. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  601. THIS_MODULE);
  602. }
  603. static struct usb_driver sd_driver = {
  604. .name = MODULE_NAME,
  605. .id_table = device_table,
  606. .probe = sd_probe,
  607. .disconnect = gspca_disconnect,
  608. #ifdef CONFIG_PM
  609. .suspend = gspca_suspend,
  610. .resume = gspca_resume,
  611. .reset_resume = gspca_resume,
  612. #endif
  613. };
  614. module_usb_driver(sd_driver);