pac7311.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. When the register addresses differ between the 7202 and the 7311 the 2
  23. different addresses are written as 7302addr/7311addr, when one of the 2
  24. addresses is a - sign that register description is not valid for the
  25. matching IC.
  26. Register page 1:
  27. Address Description
  28. -/0x08 Unknown compressor related, must always be 8 except when not
  29. in 640x480 resolution and page 4 reg 2 <= 3 then set it to 9 !
  30. -/0x1b Auto white balance related, bit 0 is AWB enable (inverted)
  31. bits 345 seem to toggle per color gains on/off (inverted)
  32. 0x78 Global control, bit 6 controls the LED (inverted)
  33. -/0x80 JPEG compression ratio ? Best not touched
  34. Register page 3/4:
  35. Address Description
  36. 0x02 Clock divider 2-63, fps =~ 60 / val. Must be a multiple of 3 on
  37. the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
  38. -/0x0f Master gain 1-245, low value = high gain
  39. 0x10/- Master gain 0-31
  40. -/0x10 Another gain 0-15, limited influence (1-2x gain I guess)
  41. 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
  42. -/0x27 Seems to toggle various gains on / off, Setting bit 7 seems to
  43. completely disable the analog amplification block. Set to 0x68
  44. for max gain, 0x14 for minimal gain.
  45. */
  46. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  47. #define MODULE_NAME "pac7311"
  48. #include <linux/input.h>
  49. #include "gspca.h"
  50. MODULE_AUTHOR("Thomas Kaiser thomas@kaiser-linux.li");
  51. MODULE_DESCRIPTION("Pixart PAC7311");
  52. MODULE_LICENSE("GPL");
  53. /* specific webcam descriptor for pac7311 */
  54. struct sd {
  55. struct gspca_dev gspca_dev; /* !! must be the first item */
  56. unsigned char contrast;
  57. unsigned char gain;
  58. unsigned char exposure;
  59. unsigned char autogain;
  60. __u8 hflip;
  61. __u8 vflip;
  62. u8 sof_read;
  63. u8 autogain_ignore_frames;
  64. atomic_t avg_lum;
  65. };
  66. /* V4L2 controls supported by the driver */
  67. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  68. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  69. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  70. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  71. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  72. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  73. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  74. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  75. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  76. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  77. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  78. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  79. static const struct ctrl sd_ctrls[] = {
  80. /* This control is for both the 7302 and the 7311 */
  81. {
  82. {
  83. .id = V4L2_CID_CONTRAST,
  84. .type = V4L2_CTRL_TYPE_INTEGER,
  85. .name = "Contrast",
  86. .minimum = 0,
  87. #define CONTRAST_MAX 255
  88. .maximum = CONTRAST_MAX,
  89. .step = 1,
  90. #define CONTRAST_DEF 127
  91. .default_value = CONTRAST_DEF,
  92. },
  93. .set = sd_setcontrast,
  94. .get = sd_getcontrast,
  95. },
  96. /* All controls below are for both the 7302 and the 7311 */
  97. {
  98. {
  99. .id = V4L2_CID_GAIN,
  100. .type = V4L2_CTRL_TYPE_INTEGER,
  101. .name = "Gain",
  102. .minimum = 0,
  103. #define GAIN_MAX 255
  104. .maximum = GAIN_MAX,
  105. .step = 1,
  106. #define GAIN_DEF 127
  107. #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
  108. .default_value = GAIN_DEF,
  109. },
  110. .set = sd_setgain,
  111. .get = sd_getgain,
  112. },
  113. {
  114. {
  115. .id = V4L2_CID_EXPOSURE,
  116. .type = V4L2_CTRL_TYPE_INTEGER,
  117. .name = "Exposure",
  118. .minimum = 0,
  119. #define EXPOSURE_MAX 255
  120. .maximum = EXPOSURE_MAX,
  121. .step = 1,
  122. #define EXPOSURE_DEF 16 /* 32 ms / 30 fps */
  123. #define EXPOSURE_KNEE 50 /* 100 ms / 10 fps */
  124. .default_value = EXPOSURE_DEF,
  125. },
  126. .set = sd_setexposure,
  127. .get = sd_getexposure,
  128. },
  129. {
  130. {
  131. .id = V4L2_CID_AUTOGAIN,
  132. .type = V4L2_CTRL_TYPE_BOOLEAN,
  133. .name = "Auto Gain",
  134. .minimum = 0,
  135. .maximum = 1,
  136. .step = 1,
  137. #define AUTOGAIN_DEF 1
  138. .default_value = AUTOGAIN_DEF,
  139. },
  140. .set = sd_setautogain,
  141. .get = sd_getautogain,
  142. },
  143. {
  144. {
  145. .id = V4L2_CID_HFLIP,
  146. .type = V4L2_CTRL_TYPE_BOOLEAN,
  147. .name = "Mirror",
  148. .minimum = 0,
  149. .maximum = 1,
  150. .step = 1,
  151. #define HFLIP_DEF 0
  152. .default_value = HFLIP_DEF,
  153. },
  154. .set = sd_sethflip,
  155. .get = sd_gethflip,
  156. },
  157. {
  158. {
  159. .id = V4L2_CID_VFLIP,
  160. .type = V4L2_CTRL_TYPE_BOOLEAN,
  161. .name = "Vflip",
  162. .minimum = 0,
  163. .maximum = 1,
  164. .step = 1,
  165. #define VFLIP_DEF 0
  166. .default_value = VFLIP_DEF,
  167. },
  168. .set = sd_setvflip,
  169. .get = sd_getvflip,
  170. },
  171. };
  172. static const struct v4l2_pix_format vga_mode[] = {
  173. {160, 120, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  174. .bytesperline = 160,
  175. .sizeimage = 160 * 120 * 3 / 8 + 590,
  176. .colorspace = V4L2_COLORSPACE_JPEG,
  177. .priv = 2},
  178. {320, 240, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  179. .bytesperline = 320,
  180. .sizeimage = 320 * 240 * 3 / 8 + 590,
  181. .colorspace = V4L2_COLORSPACE_JPEG,
  182. .priv = 1},
  183. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  184. .bytesperline = 640,
  185. .sizeimage = 640 * 480 * 3 / 8 + 590,
  186. .colorspace = V4L2_COLORSPACE_JPEG,
  187. .priv = 0},
  188. };
  189. #define LOAD_PAGE4 254
  190. #define END_OF_SEQUENCE 0
  191. /* pac 7311 */
  192. static const __u8 init_7311[] = {
  193. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  194. 0x78, 0x40, /* Bit_0=start stream, Bit_6=LED */
  195. 0x78, 0x44, /* Bit_0=start stream, Bit_6=LED */
  196. 0xff, 0x04,
  197. 0x27, 0x80,
  198. 0x28, 0xca,
  199. 0x29, 0x53,
  200. 0x2a, 0x0e,
  201. 0xff, 0x01,
  202. 0x3e, 0x20,
  203. };
  204. static const __u8 start_7311[] = {
  205. /* index, len, [value]* */
  206. 0xff, 1, 0x01, /* page 1 */
  207. 0x02, 43, 0x48, 0x0a, 0x40, 0x08, 0x00, 0x00, 0x08, 0x00,
  208. 0x06, 0xff, 0x11, 0xff, 0x5a, 0x30, 0x90, 0x4c,
  209. 0x00, 0x07, 0x00, 0x0a, 0x10, 0x00, 0xa0, 0x10,
  210. 0x02, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x00,
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00,
  213. 0x3e, 42, 0x00, 0x00, 0x78, 0x52, 0x4a, 0x52, 0x78, 0x6e,
  214. 0x48, 0x46, 0x48, 0x6e, 0x5f, 0x49, 0x42, 0x49,
  215. 0x5f, 0x5f, 0x49, 0x42, 0x49, 0x5f, 0x6e, 0x48,
  216. 0x46, 0x48, 0x6e, 0x78, 0x52, 0x4a, 0x52, 0x78,
  217. 0x00, 0x00, 0x09, 0x1b, 0x34, 0x49, 0x5c, 0x9b,
  218. 0xd0, 0xff,
  219. 0x78, 6, 0x44, 0x00, 0xf2, 0x01, 0x01, 0x80,
  220. 0x7f, 18, 0x2a, 0x1c, 0x00, 0xc8, 0x02, 0x58, 0x03, 0x84,
  221. 0x12, 0x00, 0x1a, 0x04, 0x08, 0x0c, 0x10, 0x14,
  222. 0x18, 0x20,
  223. 0x96, 3, 0x01, 0x08, 0x04,
  224. 0xa0, 4, 0x44, 0x44, 0x44, 0x04,
  225. 0xf0, 13, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x20, 0x00,
  226. 0x3f, 0x00, 0x0a, 0x01, 0x00,
  227. 0xff, 1, 0x04, /* page 4 */
  228. 0, LOAD_PAGE4, /* load the page 4 */
  229. 0x11, 1, 0x01,
  230. 0, END_OF_SEQUENCE /* end of sequence */
  231. };
  232. #define SKIP 0xaa
  233. /* page 4 - the value SKIP says skip the index - see reg_w_page() */
  234. static const __u8 page4_7311[] = {
  235. SKIP, SKIP, 0x04, 0x54, 0x07, 0x2b, 0x09, 0x0f,
  236. 0x09, 0x00, SKIP, SKIP, 0x07, 0x00, 0x00, 0x62,
  237. 0x08, SKIP, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  238. 0x00, 0x00, 0x00, 0x03, 0xa0, 0x01, 0xf4, SKIP,
  239. SKIP, 0x00, 0x08, SKIP, 0x03, SKIP, 0x00, 0x68,
  240. 0xca, 0x10, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00,
  241. 0x23, 0x28, 0x04, 0x11, 0x00, 0x00
  242. };
  243. static void reg_w_buf(struct gspca_dev *gspca_dev,
  244. __u8 index,
  245. const u8 *buffer, int len)
  246. {
  247. int ret;
  248. if (gspca_dev->usb_err < 0)
  249. return;
  250. memcpy(gspca_dev->usb_buf, buffer, len);
  251. ret = usb_control_msg(gspca_dev->dev,
  252. usb_sndctrlpipe(gspca_dev->dev, 0),
  253. 0, /* request */
  254. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  255. 0, /* value */
  256. index, gspca_dev->usb_buf, len,
  257. 500);
  258. if (ret < 0) {
  259. pr_err("reg_w_buf() failed index 0x%02x, error %d\n",
  260. index, ret);
  261. gspca_dev->usb_err = ret;
  262. }
  263. }
  264. static void reg_w(struct gspca_dev *gspca_dev,
  265. __u8 index,
  266. __u8 value)
  267. {
  268. int ret;
  269. if (gspca_dev->usb_err < 0)
  270. return;
  271. gspca_dev->usb_buf[0] = value;
  272. ret = usb_control_msg(gspca_dev->dev,
  273. usb_sndctrlpipe(gspca_dev->dev, 0),
  274. 0, /* request */
  275. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  276. 0, index, gspca_dev->usb_buf, 1,
  277. 500);
  278. if (ret < 0) {
  279. pr_err("reg_w() failed index 0x%02x, value 0x%02x, error %d\n",
  280. index, value, ret);
  281. gspca_dev->usb_err = ret;
  282. }
  283. }
  284. static void reg_w_seq(struct gspca_dev *gspca_dev,
  285. const __u8 *seq, int len)
  286. {
  287. while (--len >= 0) {
  288. reg_w(gspca_dev, seq[0], seq[1]);
  289. seq += 2;
  290. }
  291. }
  292. /* load the beginning of a page */
  293. static void reg_w_page(struct gspca_dev *gspca_dev,
  294. const __u8 *page, int len)
  295. {
  296. int index;
  297. int ret = 0;
  298. if (gspca_dev->usb_err < 0)
  299. return;
  300. for (index = 0; index < len; index++) {
  301. if (page[index] == SKIP) /* skip this index */
  302. continue;
  303. gspca_dev->usb_buf[0] = page[index];
  304. ret = usb_control_msg(gspca_dev->dev,
  305. usb_sndctrlpipe(gspca_dev->dev, 0),
  306. 0, /* request */
  307. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  308. 0, index, gspca_dev->usb_buf, 1,
  309. 500);
  310. if (ret < 0) {
  311. pr_err("reg_w_page() failed index 0x%02x, value 0x%02x, error %d\n",
  312. index, page[index], ret);
  313. gspca_dev->usb_err = ret;
  314. break;
  315. }
  316. }
  317. }
  318. /* output a variable sequence */
  319. static void reg_w_var(struct gspca_dev *gspca_dev,
  320. const __u8 *seq,
  321. const __u8 *page4, unsigned int page4_len)
  322. {
  323. int index, len;
  324. for (;;) {
  325. index = *seq++;
  326. len = *seq++;
  327. switch (len) {
  328. case END_OF_SEQUENCE:
  329. return;
  330. case LOAD_PAGE4:
  331. reg_w_page(gspca_dev, page4, page4_len);
  332. break;
  333. default:
  334. if (len > USB_BUF_SZ) {
  335. PDEBUG(D_ERR|D_STREAM,
  336. "Incorrect variable sequence");
  337. return;
  338. }
  339. while (len > 0) {
  340. if (len < 8) {
  341. reg_w_buf(gspca_dev,
  342. index, seq, len);
  343. seq += len;
  344. break;
  345. }
  346. reg_w_buf(gspca_dev, index, seq, 8);
  347. seq += 8;
  348. index += 8;
  349. len -= 8;
  350. }
  351. }
  352. }
  353. /* not reached */
  354. }
  355. /* this function is called at probe time for pac7311 */
  356. static int sd_config(struct gspca_dev *gspca_dev,
  357. const struct usb_device_id *id)
  358. {
  359. struct sd *sd = (struct sd *) gspca_dev;
  360. struct cam *cam;
  361. cam = &gspca_dev->cam;
  362. PDEBUG(D_CONF, "Find Sensor PAC7311");
  363. cam->cam_mode = vga_mode;
  364. cam->nmodes = ARRAY_SIZE(vga_mode);
  365. sd->contrast = CONTRAST_DEF;
  366. sd->gain = GAIN_DEF;
  367. sd->exposure = EXPOSURE_DEF;
  368. sd->autogain = AUTOGAIN_DEF;
  369. sd->hflip = HFLIP_DEF;
  370. sd->vflip = VFLIP_DEF;
  371. return 0;
  372. }
  373. /* This function is used by pac7311 only */
  374. static void setcontrast(struct gspca_dev *gspca_dev)
  375. {
  376. struct sd *sd = (struct sd *) gspca_dev;
  377. reg_w(gspca_dev, 0xff, 0x04);
  378. reg_w(gspca_dev, 0x10, sd->contrast >> 4);
  379. /* load registers to sensor (Bit 0, auto clear) */
  380. reg_w(gspca_dev, 0x11, 0x01);
  381. }
  382. static void setgain(struct gspca_dev *gspca_dev)
  383. {
  384. struct sd *sd = (struct sd *) gspca_dev;
  385. int gain = GAIN_MAX - sd->gain;
  386. if (gain < 1)
  387. gain = 1;
  388. else if (gain > 245)
  389. gain = 245;
  390. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  391. reg_w(gspca_dev, 0x0e, 0x00);
  392. reg_w(gspca_dev, 0x0f, gain);
  393. /* load registers to sensor (Bit 0, auto clear) */
  394. reg_w(gspca_dev, 0x11, 0x01);
  395. }
  396. static void setexposure(struct gspca_dev *gspca_dev)
  397. {
  398. struct sd *sd = (struct sd *) gspca_dev;
  399. __u8 reg;
  400. /* register 2 of frame 3/4 contains the clock divider configuring the
  401. no fps according to the formula: 60 / reg. sd->exposure is the
  402. desired exposure time in ms. */
  403. reg = 120 * sd->exposure / 1000;
  404. if (reg < 2)
  405. reg = 2;
  406. else if (reg > 63)
  407. reg = 63;
  408. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  409. reg_w(gspca_dev, 0x02, reg);
  410. /* Page 1 register 8 must always be 0x08 except when not in
  411. 640x480 mode and Page3/4 reg 2 <= 3 then it must be 9 */
  412. reg_w(gspca_dev, 0xff, 0x01);
  413. if (gspca_dev->cam.cam_mode[(int)gspca_dev->curr_mode].priv &&
  414. reg <= 3) {
  415. reg_w(gspca_dev, 0x08, 0x09);
  416. } else {
  417. reg_w(gspca_dev, 0x08, 0x08);
  418. }
  419. /* load registers to sensor (Bit 0, auto clear) */
  420. reg_w(gspca_dev, 0x11, 0x01);
  421. }
  422. static void sethvflip(struct gspca_dev *gspca_dev)
  423. {
  424. struct sd *sd = (struct sd *) gspca_dev;
  425. __u8 data;
  426. reg_w(gspca_dev, 0xff, 0x04); /* page 4 */
  427. data = (sd->hflip ? 0x04 : 0x00) | (sd->vflip ? 0x08 : 0x00);
  428. reg_w(gspca_dev, 0x21, data);
  429. /* load registers to sensor (Bit 0, auto clear) */
  430. reg_w(gspca_dev, 0x11, 0x01);
  431. }
  432. /* this function is called at probe and resume time for pac7311 */
  433. static int sd_init(struct gspca_dev *gspca_dev)
  434. {
  435. reg_w_seq(gspca_dev, init_7311, sizeof(init_7311)/2);
  436. return gspca_dev->usb_err;
  437. }
  438. static int sd_start(struct gspca_dev *gspca_dev)
  439. {
  440. struct sd *sd = (struct sd *) gspca_dev;
  441. sd->sof_read = 0;
  442. reg_w_var(gspca_dev, start_7311,
  443. page4_7311, sizeof(page4_7311));
  444. setcontrast(gspca_dev);
  445. setgain(gspca_dev);
  446. setexposure(gspca_dev);
  447. sethvflip(gspca_dev);
  448. /* set correct resolution */
  449. switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
  450. case 2: /* 160x120 pac7311 */
  451. reg_w(gspca_dev, 0xff, 0x01);
  452. reg_w(gspca_dev, 0x17, 0x20);
  453. reg_w(gspca_dev, 0x87, 0x10);
  454. break;
  455. case 1: /* 320x240 pac7311 */
  456. reg_w(gspca_dev, 0xff, 0x01);
  457. reg_w(gspca_dev, 0x17, 0x30);
  458. reg_w(gspca_dev, 0x87, 0x11);
  459. break;
  460. case 0: /* 640x480 */
  461. reg_w(gspca_dev, 0xff, 0x01);
  462. reg_w(gspca_dev, 0x17, 0x00);
  463. reg_w(gspca_dev, 0x87, 0x12);
  464. break;
  465. }
  466. sd->sof_read = 0;
  467. sd->autogain_ignore_frames = 0;
  468. atomic_set(&sd->avg_lum, -1);
  469. /* start stream */
  470. reg_w(gspca_dev, 0xff, 0x01);
  471. reg_w(gspca_dev, 0x78, 0x05);
  472. return gspca_dev->usb_err;
  473. }
  474. static void sd_stopN(struct gspca_dev *gspca_dev)
  475. {
  476. reg_w(gspca_dev, 0xff, 0x04);
  477. reg_w(gspca_dev, 0x27, 0x80);
  478. reg_w(gspca_dev, 0x28, 0xca);
  479. reg_w(gspca_dev, 0x29, 0x53);
  480. reg_w(gspca_dev, 0x2a, 0x0e);
  481. reg_w(gspca_dev, 0xff, 0x01);
  482. reg_w(gspca_dev, 0x3e, 0x20);
  483. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  484. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  485. reg_w(gspca_dev, 0x78, 0x44); /* Bit_0=start stream, Bit_6=LED */
  486. }
  487. /* called on streamoff with alt 0 and on disconnect for 7311 */
  488. static void sd_stop0(struct gspca_dev *gspca_dev)
  489. {
  490. }
  491. /* Include pac common sof detection functions */
  492. #include "pac_common.h"
  493. static void do_autogain(struct gspca_dev *gspca_dev)
  494. {
  495. struct sd *sd = (struct sd *) gspca_dev;
  496. int avg_lum = atomic_read(&sd->avg_lum);
  497. int desired_lum, deadzone;
  498. if (avg_lum == -1)
  499. return;
  500. desired_lum = 200;
  501. deadzone = 20;
  502. if (sd->autogain_ignore_frames > 0)
  503. sd->autogain_ignore_frames--;
  504. else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
  505. deadzone, GAIN_KNEE, EXPOSURE_KNEE))
  506. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  507. }
  508. /* JPEG header, part 1 */
  509. static const unsigned char pac_jpeg_header1[] = {
  510. 0xff, 0xd8, /* SOI: Start of Image */
  511. 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
  512. 0x00, 0x11, /* length = 17 bytes (including this length field) */
  513. 0x08 /* Precision: 8 */
  514. /* 2 bytes is placed here: number of image lines */
  515. /* 2 bytes is placed here: samples per line */
  516. };
  517. /* JPEG header, continued */
  518. static const unsigned char pac_jpeg_header2[] = {
  519. 0x03, /* Number of image components: 3 */
  520. 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
  521. 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
  522. 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
  523. 0xff, 0xda, /* SOS: Start Of Scan */
  524. 0x00, 0x0c, /* length = 12 bytes (including this length field) */
  525. 0x03, /* number of components: 3 */
  526. 0x01, 0x00, /* selector 1, table 0x00 */
  527. 0x02, 0x11, /* selector 2, table 0x11 */
  528. 0x03, 0x11, /* selector 3, table 0x11 */
  529. 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
  530. 0x00 /* Successive approximation: 0 */
  531. };
  532. static void pac_start_frame(struct gspca_dev *gspca_dev,
  533. __u16 lines, __u16 samples_per_line)
  534. {
  535. unsigned char tmpbuf[4];
  536. gspca_frame_add(gspca_dev, FIRST_PACKET,
  537. pac_jpeg_header1, sizeof(pac_jpeg_header1));
  538. tmpbuf[0] = lines >> 8;
  539. tmpbuf[1] = lines & 0xff;
  540. tmpbuf[2] = samples_per_line >> 8;
  541. tmpbuf[3] = samples_per_line & 0xff;
  542. gspca_frame_add(gspca_dev, INTER_PACKET,
  543. tmpbuf, sizeof(tmpbuf));
  544. gspca_frame_add(gspca_dev, INTER_PACKET,
  545. pac_jpeg_header2, sizeof(pac_jpeg_header2));
  546. }
  547. /* this function is run at interrupt level */
  548. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  549. u8 *data, /* isoc packet */
  550. int len) /* iso packet length */
  551. {
  552. struct sd *sd = (struct sd *) gspca_dev;
  553. u8 *image;
  554. unsigned char *sof;
  555. sof = pac_find_sof(&sd->sof_read, data, len);
  556. if (sof) {
  557. int n, lum_offset, footer_length;
  558. /* 6 bytes after the FF D9 EOF marker a number of lumination
  559. bytes are send corresponding to different parts of the
  560. image, the 14th and 15th byte after the EOF seem to
  561. correspond to the center of the image */
  562. lum_offset = 24 + sizeof pac_sof_marker;
  563. footer_length = 26;
  564. /* Finish decoding current frame */
  565. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  566. if (n < 0) {
  567. gspca_dev->image_len += n;
  568. n = 0;
  569. } else {
  570. gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
  571. }
  572. image = gspca_dev->image;
  573. if (image != NULL
  574. && image[gspca_dev->image_len - 2] == 0xff
  575. && image[gspca_dev->image_len - 1] == 0xd9)
  576. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  577. n = sof - data;
  578. len -= n;
  579. data = sof;
  580. /* Get average lumination */
  581. if (gspca_dev->last_packet_type == LAST_PACKET &&
  582. n >= lum_offset)
  583. atomic_set(&sd->avg_lum, data[-lum_offset] +
  584. data[-lum_offset + 1]);
  585. else
  586. atomic_set(&sd->avg_lum, -1);
  587. /* Start the new frame with the jpeg header */
  588. pac_start_frame(gspca_dev,
  589. gspca_dev->height, gspca_dev->width);
  590. }
  591. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  592. }
  593. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  594. {
  595. struct sd *sd = (struct sd *) gspca_dev;
  596. sd->contrast = val;
  597. if (gspca_dev->streaming)
  598. setcontrast(gspca_dev);
  599. return gspca_dev->usb_err;
  600. }
  601. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  602. {
  603. struct sd *sd = (struct sd *) gspca_dev;
  604. *val = sd->contrast;
  605. return 0;
  606. }
  607. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  608. {
  609. struct sd *sd = (struct sd *) gspca_dev;
  610. sd->gain = val;
  611. if (gspca_dev->streaming)
  612. setgain(gspca_dev);
  613. return gspca_dev->usb_err;
  614. }
  615. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  616. {
  617. struct sd *sd = (struct sd *) gspca_dev;
  618. *val = sd->gain;
  619. return 0;
  620. }
  621. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  622. {
  623. struct sd *sd = (struct sd *) gspca_dev;
  624. sd->exposure = val;
  625. if (gspca_dev->streaming)
  626. setexposure(gspca_dev);
  627. return gspca_dev->usb_err;
  628. }
  629. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  630. {
  631. struct sd *sd = (struct sd *) gspca_dev;
  632. *val = sd->exposure;
  633. return 0;
  634. }
  635. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  636. {
  637. struct sd *sd = (struct sd *) gspca_dev;
  638. sd->autogain = val;
  639. /* when switching to autogain set defaults to make sure
  640. we are on a valid point of the autogain gain /
  641. exposure knee graph, and give this change time to
  642. take effect before doing autogain. */
  643. if (sd->autogain) {
  644. sd->exposure = EXPOSURE_DEF;
  645. sd->gain = GAIN_DEF;
  646. if (gspca_dev->streaming) {
  647. sd->autogain_ignore_frames =
  648. PAC_AUTOGAIN_IGNORE_FRAMES;
  649. setexposure(gspca_dev);
  650. setgain(gspca_dev);
  651. }
  652. }
  653. return gspca_dev->usb_err;
  654. }
  655. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  656. {
  657. struct sd *sd = (struct sd *) gspca_dev;
  658. *val = sd->autogain;
  659. return 0;
  660. }
  661. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  662. {
  663. struct sd *sd = (struct sd *) gspca_dev;
  664. sd->hflip = val;
  665. if (gspca_dev->streaming)
  666. sethvflip(gspca_dev);
  667. return gspca_dev->usb_err;
  668. }
  669. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  670. {
  671. struct sd *sd = (struct sd *) gspca_dev;
  672. *val = sd->hflip;
  673. return 0;
  674. }
  675. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  676. {
  677. struct sd *sd = (struct sd *) gspca_dev;
  678. sd->vflip = val;
  679. if (gspca_dev->streaming)
  680. sethvflip(gspca_dev);
  681. return gspca_dev->usb_err;
  682. }
  683. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  684. {
  685. struct sd *sd = (struct sd *) gspca_dev;
  686. *val = sd->vflip;
  687. return 0;
  688. }
  689. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  690. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  691. u8 *data, /* interrupt packet data */
  692. int len) /* interrupt packet length */
  693. {
  694. int ret = -EINVAL;
  695. u8 data0, data1;
  696. if (len == 2) {
  697. data0 = data[0];
  698. data1 = data[1];
  699. if ((data0 == 0x00 && data1 == 0x11) ||
  700. (data0 == 0x22 && data1 == 0x33) ||
  701. (data0 == 0x44 && data1 == 0x55) ||
  702. (data0 == 0x66 && data1 == 0x77) ||
  703. (data0 == 0x88 && data1 == 0x99) ||
  704. (data0 == 0xaa && data1 == 0xbb) ||
  705. (data0 == 0xcc && data1 == 0xdd) ||
  706. (data0 == 0xee && data1 == 0xff)) {
  707. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  708. input_sync(gspca_dev->input_dev);
  709. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  710. input_sync(gspca_dev->input_dev);
  711. ret = 0;
  712. }
  713. }
  714. return ret;
  715. }
  716. #endif
  717. /* sub-driver description for pac7311 */
  718. static const struct sd_desc sd_desc = {
  719. .name = MODULE_NAME,
  720. .ctrls = sd_ctrls,
  721. .nctrls = ARRAY_SIZE(sd_ctrls),
  722. .config = sd_config,
  723. .init = sd_init,
  724. .start = sd_start,
  725. .stopN = sd_stopN,
  726. .stop0 = sd_stop0,
  727. .pkt_scan = sd_pkt_scan,
  728. .dq_callback = do_autogain,
  729. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  730. .int_pkt_scan = sd_int_pkt_scan,
  731. #endif
  732. };
  733. /* -- module initialisation -- */
  734. static const struct usb_device_id device_table[] = {
  735. {USB_DEVICE(0x093a, 0x2600)},
  736. {USB_DEVICE(0x093a, 0x2601)},
  737. {USB_DEVICE(0x093a, 0x2603)},
  738. {USB_DEVICE(0x093a, 0x2608)},
  739. {USB_DEVICE(0x093a, 0x260e)},
  740. {USB_DEVICE(0x093a, 0x260f)},
  741. {}
  742. };
  743. MODULE_DEVICE_TABLE(usb, device_table);
  744. /* -- device connect -- */
  745. static int sd_probe(struct usb_interface *intf,
  746. const struct usb_device_id *id)
  747. {
  748. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  749. THIS_MODULE);
  750. }
  751. static struct usb_driver sd_driver = {
  752. .name = MODULE_NAME,
  753. .id_table = device_table,
  754. .probe = sd_probe,
  755. .disconnect = gspca_disconnect,
  756. #ifdef CONFIG_PM
  757. .suspend = gspca_suspend,
  758. .resume = gspca_resume,
  759. #endif
  760. };
  761. module_usb_driver(sd_driver);