pac7302.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Pixart PAC7302 driver
  3. *
  4. * Copyright (C) 2008-2012 Jean-Francois Moine <http://moinejf.free.fr>
  5. * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
  6. *
  7. * Separated from Pixart PAC7311 library by Márton Németh
  8. * Camera button input handling by Márton Németh <nm127@freemail.hu>
  9. * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* Some documentation about various registers as determined by trial and error.
  26. Register page 1:
  27. Address Description
  28. 0x78 Global control, bit 6 controls the LED (inverted)
  29. Register page 3:
  30. Address Description
  31. 0x02 Clock divider 3-63, fps = 90 / val. Must be a multiple of 3 on
  32. the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
  33. 0x03 Variable framerate ctrl reg2==3: 0 -> ~30 fps, 255 -> ~22fps
  34. 0x04 Another var framerate ctrl reg2==3, reg3==0: 0 -> ~30 fps,
  35. 63 -> ~27 fps, the 2 msb's must always be 1 !!
  36. 0x05 Another var framerate ctrl reg2==3, reg3==0, reg4==0xc0:
  37. 1 -> ~30 fps, 2 -> ~20 fps
  38. 0x0e Exposure bits 0-7, 0-448, 0 = use full frame time
  39. 0x0f Exposure bit 8, 0-448, 448 = no exposure at all
  40. 0x10 Master gain 0-31
  41. 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
  42. The registers are accessed in the following functions:
  43. Page | Register | Function
  44. -----+------------+---------------------------------------------------
  45. 0 | 0x0f..0x20 | setcolors()
  46. 0 | 0xa2..0xab | setbrightcont()
  47. 0 | 0xc5 | setredbalance()
  48. 0 | 0xc6 | setwhitebalance()
  49. 0 | 0xc7 | setbluebalance()
  50. 0 | 0xdc | setbrightcont(), setcolors()
  51. 3 | 0x02 | setexposure()
  52. 3 | 0x10 | setgain()
  53. 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
  54. 3 | 0x21 | sethvflip()
  55. */
  56. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  57. #include <linux/input.h>
  58. #include <media/v4l2-chip-ident.h>
  59. #include "gspca.h"
  60. /* Include pac common sof detection functions */
  61. #include "pac_common.h"
  62. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, "
  63. "Thomas Kaiser thomas@kaiser-linux.li");
  64. MODULE_DESCRIPTION("Pixart PAC7302");
  65. MODULE_LICENSE("GPL");
  66. enum e_ctrl {
  67. BRIGHTNESS,
  68. CONTRAST,
  69. COLORS,
  70. WHITE_BALANCE,
  71. RED_BALANCE,
  72. BLUE_BALANCE,
  73. GAIN,
  74. AUTOGAIN,
  75. EXPOSURE,
  76. VFLIP,
  77. HFLIP,
  78. NCTRLS /* number of controls */
  79. };
  80. /* specific webcam descriptor for pac7302 */
  81. struct sd {
  82. struct gspca_dev gspca_dev; /* !! must be the first item */
  83. struct gspca_ctrl ctrls[NCTRLS];
  84. u8 flags;
  85. #define FL_HFLIP 0x01 /* mirrored by default */
  86. #define FL_VFLIP 0x02 /* vertical flipped by default */
  87. u8 sof_read;
  88. s8 autogain_ignore_frames;
  89. atomic_t avg_lum;
  90. };
  91. /* V4L2 controls supported by the driver */
  92. static void setbrightcont(struct gspca_dev *gspca_dev);
  93. static void setcolors(struct gspca_dev *gspca_dev);
  94. static void setwhitebalance(struct gspca_dev *gspca_dev);
  95. static void setredbalance(struct gspca_dev *gspca_dev);
  96. static void setbluebalance(struct gspca_dev *gspca_dev);
  97. static void setgain(struct gspca_dev *gspca_dev);
  98. static void setexposure(struct gspca_dev *gspca_dev);
  99. static void setautogain(struct gspca_dev *gspca_dev);
  100. static void sethvflip(struct gspca_dev *gspca_dev);
  101. static const struct ctrl sd_ctrls[] = {
  102. [BRIGHTNESS] = {
  103. {
  104. .id = V4L2_CID_BRIGHTNESS,
  105. .type = V4L2_CTRL_TYPE_INTEGER,
  106. .name = "Brightness",
  107. .minimum = 0,
  108. #define BRIGHTNESS_MAX 0x20
  109. .maximum = BRIGHTNESS_MAX,
  110. .step = 1,
  111. .default_value = 0x10,
  112. },
  113. .set_control = setbrightcont
  114. },
  115. [CONTRAST] = {
  116. {
  117. .id = V4L2_CID_CONTRAST,
  118. .type = V4L2_CTRL_TYPE_INTEGER,
  119. .name = "Contrast",
  120. .minimum = 0,
  121. #define CONTRAST_MAX 255
  122. .maximum = CONTRAST_MAX,
  123. .step = 1,
  124. .default_value = 127,
  125. },
  126. .set_control = setbrightcont
  127. },
  128. [COLORS] = {
  129. {
  130. .id = V4L2_CID_SATURATION,
  131. .type = V4L2_CTRL_TYPE_INTEGER,
  132. .name = "Saturation",
  133. .minimum = 0,
  134. #define COLOR_MAX 255
  135. .maximum = COLOR_MAX,
  136. .step = 1,
  137. .default_value = 127
  138. },
  139. .set_control = setcolors
  140. },
  141. [WHITE_BALANCE] = {
  142. {
  143. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  144. .type = V4L2_CTRL_TYPE_INTEGER,
  145. .name = "White Balance",
  146. .minimum = 0,
  147. .maximum = 255,
  148. .step = 1,
  149. .default_value = 4,
  150. },
  151. .set_control = setwhitebalance
  152. },
  153. [RED_BALANCE] = {
  154. {
  155. .id = V4L2_CID_RED_BALANCE,
  156. .type = V4L2_CTRL_TYPE_INTEGER,
  157. .name = "Red",
  158. .minimum = 0,
  159. .maximum = 3,
  160. .step = 1,
  161. .default_value = 1,
  162. },
  163. .set_control = setredbalance
  164. },
  165. [BLUE_BALANCE] = {
  166. {
  167. .id = V4L2_CID_BLUE_BALANCE,
  168. .type = V4L2_CTRL_TYPE_INTEGER,
  169. .name = "Blue",
  170. .minimum = 0,
  171. .maximum = 3,
  172. .step = 1,
  173. .default_value = 1,
  174. },
  175. .set_control = setbluebalance
  176. },
  177. [GAIN] = {
  178. {
  179. .id = V4L2_CID_GAIN,
  180. .type = V4L2_CTRL_TYPE_INTEGER,
  181. .name = "Gain",
  182. .minimum = 0,
  183. .maximum = 255,
  184. .step = 1,
  185. #define GAIN_DEF 127
  186. #define GAIN_KNEE 255 /* Gain seems to cause little noise on the pac73xx */
  187. .default_value = GAIN_DEF,
  188. },
  189. .set_control = setgain
  190. },
  191. [EXPOSURE] = {
  192. {
  193. .id = V4L2_CID_EXPOSURE,
  194. .type = V4L2_CTRL_TYPE_INTEGER,
  195. .name = "Exposure",
  196. .minimum = 0,
  197. .maximum = 1023,
  198. .step = 1,
  199. #define EXPOSURE_DEF 66 /* 33 ms / 30 fps */
  200. #define EXPOSURE_KNEE 133 /* 66 ms / 15 fps */
  201. .default_value = EXPOSURE_DEF,
  202. },
  203. .set_control = setexposure
  204. },
  205. [AUTOGAIN] = {
  206. {
  207. .id = V4L2_CID_AUTOGAIN,
  208. .type = V4L2_CTRL_TYPE_BOOLEAN,
  209. .name = "Auto Gain",
  210. .minimum = 0,
  211. .maximum = 1,
  212. .step = 1,
  213. #define AUTOGAIN_DEF 1
  214. .default_value = AUTOGAIN_DEF,
  215. },
  216. .set_control = setautogain,
  217. },
  218. [HFLIP] = {
  219. {
  220. .id = V4L2_CID_HFLIP,
  221. .type = V4L2_CTRL_TYPE_BOOLEAN,
  222. .name = "Mirror",
  223. .minimum = 0,
  224. .maximum = 1,
  225. .step = 1,
  226. .default_value = 0,
  227. },
  228. .set_control = sethvflip,
  229. },
  230. [VFLIP] = {
  231. {
  232. .id = V4L2_CID_VFLIP,
  233. .type = V4L2_CTRL_TYPE_BOOLEAN,
  234. .name = "Vflip",
  235. .minimum = 0,
  236. .maximum = 1,
  237. .step = 1,
  238. .default_value = 0,
  239. },
  240. .set_control = sethvflip
  241. },
  242. };
  243. static const struct v4l2_pix_format vga_mode[] = {
  244. {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
  245. .bytesperline = 640,
  246. .sizeimage = 640 * 480 * 3 / 8 + 590,
  247. .colorspace = V4L2_COLORSPACE_JPEG,
  248. },
  249. };
  250. #define LOAD_PAGE3 255
  251. #define END_OF_SEQUENCE 0
  252. /* pac 7302 */
  253. static const u8 init_7302[] = {
  254. /* index,value */
  255. 0xff, 0x01, /* page 1 */
  256. 0x78, 0x00, /* deactivate */
  257. 0xff, 0x01,
  258. 0x78, 0x40, /* led off */
  259. };
  260. static const u8 start_7302[] = {
  261. /* index, len, [value]* */
  262. 0xff, 1, 0x00, /* page 0 */
  263. 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
  264. 0x00, 0x00, 0x00, 0x00,
  265. 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
  266. 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
  267. 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
  268. 0x26, 2, 0xaa, 0xaa,
  269. 0x2e, 1, 0x31,
  270. 0x38, 1, 0x01,
  271. 0x3a, 3, 0x14, 0xff, 0x5a,
  272. 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
  273. 0x00, 0x54, 0x11,
  274. 0x55, 1, 0x00,
  275. 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
  276. 0x6b, 1, 0x00,
  277. 0x6e, 3, 0x08, 0x06, 0x00,
  278. 0x72, 3, 0x00, 0xff, 0x00,
  279. 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
  280. 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
  281. 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
  282. 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
  283. 0xd2, 0xeb,
  284. 0xaf, 1, 0x02,
  285. 0xb5, 2, 0x08, 0x08,
  286. 0xb8, 2, 0x08, 0x88,
  287. 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
  288. 0xcc, 1, 0x00,
  289. 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
  290. 0xc1, 0xd7, 0xec,
  291. 0xdc, 1, 0x01,
  292. 0xff, 1, 0x01, /* page 1 */
  293. 0x12, 3, 0x02, 0x00, 0x01,
  294. 0x3e, 2, 0x00, 0x00,
  295. 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
  296. 0x7c, 1, 0x00,
  297. 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
  298. 0x02, 0x00,
  299. 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
  300. 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
  301. 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
  302. 0xd8, 1, 0x01,
  303. 0xdb, 2, 0x00, 0x01,
  304. 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
  305. 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
  306. 0xeb, 1, 0x00,
  307. 0xff, 1, 0x02, /* page 2 */
  308. 0x22, 1, 0x00,
  309. 0xff, 1, 0x03, /* page 3 */
  310. 0, LOAD_PAGE3, /* load the page 3 */
  311. 0x11, 1, 0x01,
  312. 0xff, 1, 0x02, /* page 2 */
  313. 0x13, 1, 0x00,
  314. 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
  315. 0x27, 2, 0x14, 0x0c,
  316. 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
  317. 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
  318. 0x6e, 1, 0x08,
  319. 0xff, 1, 0x01, /* page 1 */
  320. 0x78, 1, 0x00,
  321. 0, END_OF_SEQUENCE /* end of sequence */
  322. };
  323. #define SKIP 0xaa
  324. /* page 3 - the value SKIP says skip the index - see reg_w_page() */
  325. static const u8 page3_7302[] = {
  326. 0x90, 0x40, 0x03, 0x00, 0xc0, 0x01, 0x14, 0x16,
  327. 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
  328. 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  329. 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
  330. 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
  331. 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
  332. 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
  333. 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  334. 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
  335. SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
  336. 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  337. 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
  338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  339. 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
  340. 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
  341. 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
  342. 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
  343. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  344. 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
  345. 0x00
  346. };
  347. static void reg_w_buf(struct gspca_dev *gspca_dev,
  348. u8 index,
  349. const u8 *buffer, int len)
  350. {
  351. int ret;
  352. if (gspca_dev->usb_err < 0)
  353. return;
  354. memcpy(gspca_dev->usb_buf, buffer, len);
  355. ret = usb_control_msg(gspca_dev->dev,
  356. usb_sndctrlpipe(gspca_dev->dev, 0),
  357. 0, /* request */
  358. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  359. 0, /* value */
  360. index, gspca_dev->usb_buf, len,
  361. 500);
  362. if (ret < 0) {
  363. pr_err("reg_w_buf failed i: %02x error %d\n",
  364. index, ret);
  365. gspca_dev->usb_err = ret;
  366. }
  367. }
  368. static void reg_w(struct gspca_dev *gspca_dev,
  369. u8 index,
  370. u8 value)
  371. {
  372. int ret;
  373. if (gspca_dev->usb_err < 0)
  374. return;
  375. gspca_dev->usb_buf[0] = value;
  376. ret = usb_control_msg(gspca_dev->dev,
  377. usb_sndctrlpipe(gspca_dev->dev, 0),
  378. 0, /* request */
  379. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  380. 0, index, gspca_dev->usb_buf, 1,
  381. 500);
  382. if (ret < 0) {
  383. pr_err("reg_w() failed i: %02x v: %02x error %d\n",
  384. index, value, ret);
  385. gspca_dev->usb_err = ret;
  386. }
  387. }
  388. static void reg_w_seq(struct gspca_dev *gspca_dev,
  389. const u8 *seq, int len)
  390. {
  391. while (--len >= 0) {
  392. reg_w(gspca_dev, seq[0], seq[1]);
  393. seq += 2;
  394. }
  395. }
  396. /* load the beginning of a page */
  397. static void reg_w_page(struct gspca_dev *gspca_dev,
  398. const u8 *page, int len)
  399. {
  400. int index;
  401. int ret = 0;
  402. if (gspca_dev->usb_err < 0)
  403. return;
  404. for (index = 0; index < len; index++) {
  405. if (page[index] == SKIP) /* skip this index */
  406. continue;
  407. gspca_dev->usb_buf[0] = page[index];
  408. ret = usb_control_msg(gspca_dev->dev,
  409. usb_sndctrlpipe(gspca_dev->dev, 0),
  410. 0, /* request */
  411. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  412. 0, index, gspca_dev->usb_buf, 1,
  413. 500);
  414. if (ret < 0) {
  415. pr_err("reg_w_page() failed i: %02x v: %02x error %d\n",
  416. index, page[index], ret);
  417. gspca_dev->usb_err = ret;
  418. break;
  419. }
  420. }
  421. }
  422. /* output a variable sequence */
  423. static void reg_w_var(struct gspca_dev *gspca_dev,
  424. const u8 *seq,
  425. const u8 *page3, unsigned int page3_len)
  426. {
  427. int index, len;
  428. for (;;) {
  429. index = *seq++;
  430. len = *seq++;
  431. switch (len) {
  432. case END_OF_SEQUENCE:
  433. return;
  434. case LOAD_PAGE3:
  435. reg_w_page(gspca_dev, page3, page3_len);
  436. break;
  437. default:
  438. #ifdef GSPCA_DEBUG
  439. if (len > USB_BUF_SZ) {
  440. PDEBUG(D_ERR|D_STREAM,
  441. "Incorrect variable sequence");
  442. return;
  443. }
  444. #endif
  445. while (len > 0) {
  446. if (len < 8) {
  447. reg_w_buf(gspca_dev,
  448. index, seq, len);
  449. seq += len;
  450. break;
  451. }
  452. reg_w_buf(gspca_dev, index, seq, 8);
  453. seq += 8;
  454. index += 8;
  455. len -= 8;
  456. }
  457. }
  458. }
  459. /* not reached */
  460. }
  461. /* this function is called at probe time for pac7302 */
  462. static int sd_config(struct gspca_dev *gspca_dev,
  463. const struct usb_device_id *id)
  464. {
  465. struct sd *sd = (struct sd *) gspca_dev;
  466. struct cam *cam;
  467. cam = &gspca_dev->cam;
  468. cam->cam_mode = vga_mode; /* only 640x480 */
  469. cam->nmodes = ARRAY_SIZE(vga_mode);
  470. gspca_dev->cam.ctrls = sd->ctrls;
  471. sd->flags = id->driver_info;
  472. return 0;
  473. }
  474. /* This function is used by pac7302 only */
  475. static void setbrightcont(struct gspca_dev *gspca_dev)
  476. {
  477. struct sd *sd = (struct sd *) gspca_dev;
  478. int i, v;
  479. static const u8 max[10] =
  480. {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
  481. 0xd4, 0xec};
  482. static const u8 delta[10] =
  483. {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
  484. 0x11, 0x0b};
  485. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  486. for (i = 0; i < 10; i++) {
  487. v = max[i];
  488. v += (sd->ctrls[BRIGHTNESS].val - BRIGHTNESS_MAX)
  489. * 150 / BRIGHTNESS_MAX; /* 200 ? */
  490. v -= delta[i] * sd->ctrls[CONTRAST].val / CONTRAST_MAX;
  491. if (v < 0)
  492. v = 0;
  493. else if (v > 0xff)
  494. v = 0xff;
  495. reg_w(gspca_dev, 0xa2 + i, v);
  496. }
  497. reg_w(gspca_dev, 0xdc, 0x01);
  498. }
  499. /* This function is used by pac7302 only */
  500. static void setcolors(struct gspca_dev *gspca_dev)
  501. {
  502. struct sd *sd = (struct sd *) gspca_dev;
  503. int i, v;
  504. static const int a[9] =
  505. {217, -212, 0, -101, 170, -67, -38, -315, 355};
  506. static const int b[9] =
  507. {19, 106, 0, 19, 106, 1, 19, 106, 1};
  508. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  509. reg_w(gspca_dev, 0x11, 0x01);
  510. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  511. for (i = 0; i < 9; i++) {
  512. v = a[i] * sd->ctrls[COLORS].val / COLOR_MAX + b[i];
  513. reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
  514. reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
  515. }
  516. reg_w(gspca_dev, 0xdc, 0x01);
  517. }
  518. static void setwhitebalance(struct gspca_dev *gspca_dev)
  519. {
  520. struct sd *sd = (struct sd *) gspca_dev;
  521. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  522. reg_w(gspca_dev, 0xc6, sd->ctrls[WHITE_BALANCE].val);
  523. reg_w(gspca_dev, 0xdc, 0x01);
  524. }
  525. static void setredbalance(struct gspca_dev *gspca_dev)
  526. {
  527. struct sd *sd = (struct sd *) gspca_dev;
  528. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  529. reg_w(gspca_dev, 0xc5, sd->ctrls[RED_BALANCE].val);
  530. reg_w(gspca_dev, 0xdc, 0x01);
  531. }
  532. static void setbluebalance(struct gspca_dev *gspca_dev)
  533. {
  534. struct sd *sd = (struct sd *) gspca_dev;
  535. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  536. reg_w(gspca_dev, 0xc7, sd->ctrls[BLUE_BALANCE].val);
  537. reg_w(gspca_dev, 0xdc, 0x01);
  538. }
  539. static void setgain(struct gspca_dev *gspca_dev)
  540. {
  541. struct sd *sd = (struct sd *) gspca_dev;
  542. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  543. reg_w(gspca_dev, 0x10, sd->ctrls[GAIN].val >> 3);
  544. /* load registers to sensor (Bit 0, auto clear) */
  545. reg_w(gspca_dev, 0x11, 0x01);
  546. }
  547. static void setexposure(struct gspca_dev *gspca_dev)
  548. {
  549. struct sd *sd = (struct sd *) gspca_dev;
  550. u8 clockdiv;
  551. u16 exposure;
  552. /* register 2 of frame 3 contains the clock divider configuring the
  553. no fps according to the formula: 90 / reg. sd->exposure is the
  554. desired exposure time in 0.5 ms. */
  555. clockdiv = (90 * sd->ctrls[EXPOSURE].val + 1999) / 2000;
  556. /* Note clockdiv = 3 also works, but when running at 30 fps, depending
  557. on the scene being recorded, the camera switches to another
  558. quantization table for certain JPEG blocks, and we don't know how
  559. to decompress these blocks. So we cap the framerate at 15 fps */
  560. if (clockdiv < 6)
  561. clockdiv = 6;
  562. else if (clockdiv > 63)
  563. clockdiv = 63;
  564. /* reg2 MUST be a multiple of 3, except when between 6 and 12?
  565. Always round up, otherwise we cannot get the desired frametime
  566. using the partial frame time exposure control */
  567. if (clockdiv < 6 || clockdiv > 12)
  568. clockdiv = ((clockdiv + 2) / 3) * 3;
  569. /* frame exposure time in ms = 1000 * clockdiv / 90 ->
  570. exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90) */
  571. exposure = (sd->ctrls[EXPOSURE].val * 45 * 448) / (1000 * clockdiv);
  572. /* 0 = use full frametime, 448 = no exposure, reverse it */
  573. exposure = 448 - exposure;
  574. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  575. reg_w(gspca_dev, 0x02, clockdiv);
  576. reg_w(gspca_dev, 0x0e, exposure & 0xff);
  577. reg_w(gspca_dev, 0x0f, exposure >> 8);
  578. /* load registers to sensor (Bit 0, auto clear) */
  579. reg_w(gspca_dev, 0x11, 0x01);
  580. }
  581. static void setautogain(struct gspca_dev *gspca_dev)
  582. {
  583. struct sd *sd = (struct sd *) gspca_dev;
  584. /* when switching to autogain set defaults to make sure
  585. we are on a valid point of the autogain gain /
  586. exposure knee graph, and give this change time to
  587. take effect before doing autogain. */
  588. if (sd->ctrls[AUTOGAIN].val) {
  589. sd->ctrls[EXPOSURE].val = EXPOSURE_DEF;
  590. sd->ctrls[GAIN].val = GAIN_DEF;
  591. sd->autogain_ignore_frames =
  592. PAC_AUTOGAIN_IGNORE_FRAMES;
  593. } else {
  594. sd->autogain_ignore_frames = -1;
  595. }
  596. setexposure(gspca_dev);
  597. setgain(gspca_dev);
  598. }
  599. static void sethvflip(struct gspca_dev *gspca_dev)
  600. {
  601. struct sd *sd = (struct sd *) gspca_dev;
  602. u8 data, hflip, vflip;
  603. hflip = sd->ctrls[HFLIP].val;
  604. if (sd->flags & FL_HFLIP)
  605. hflip = !hflip;
  606. vflip = sd->ctrls[VFLIP].val;
  607. if (sd->flags & FL_VFLIP)
  608. vflip = !vflip;
  609. reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
  610. data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
  611. reg_w(gspca_dev, 0x21, data);
  612. /* load registers to sensor (Bit 0, auto clear) */
  613. reg_w(gspca_dev, 0x11, 0x01);
  614. }
  615. /* this function is called at probe and resume time for pac7302 */
  616. static int sd_init(struct gspca_dev *gspca_dev)
  617. {
  618. reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
  619. return gspca_dev->usb_err;
  620. }
  621. static int sd_start(struct gspca_dev *gspca_dev)
  622. {
  623. struct sd *sd = (struct sd *) gspca_dev;
  624. reg_w_var(gspca_dev, start_7302,
  625. page3_7302, sizeof(page3_7302));
  626. setbrightcont(gspca_dev);
  627. setcolors(gspca_dev);
  628. setwhitebalance(gspca_dev);
  629. setredbalance(gspca_dev);
  630. setbluebalance(gspca_dev);
  631. setautogain(gspca_dev);
  632. sethvflip(gspca_dev);
  633. /* only resolution 640x480 is supported for pac7302 */
  634. sd->sof_read = 0;
  635. atomic_set(&sd->avg_lum, 270 + sd->ctrls[BRIGHTNESS].val);
  636. /* start stream */
  637. reg_w(gspca_dev, 0xff, 0x01);
  638. reg_w(gspca_dev, 0x78, 0x01);
  639. return gspca_dev->usb_err;
  640. }
  641. static void sd_stopN(struct gspca_dev *gspca_dev)
  642. {
  643. /* stop stream */
  644. reg_w(gspca_dev, 0xff, 0x01);
  645. reg_w(gspca_dev, 0x78, 0x00);
  646. }
  647. /* called on streamoff with alt 0 and on disconnect for pac7302 */
  648. static void sd_stop0(struct gspca_dev *gspca_dev)
  649. {
  650. if (!gspca_dev->present)
  651. return;
  652. reg_w(gspca_dev, 0xff, 0x01);
  653. reg_w(gspca_dev, 0x78, 0x40);
  654. }
  655. /* !! coarse_grained_expo_autogain is not used !! */
  656. #define exp_too_low_cnt flags
  657. #define exp_too_high_cnt sof_read
  658. #include "autogain_functions.h"
  659. static void do_autogain(struct gspca_dev *gspca_dev)
  660. {
  661. struct sd *sd = (struct sd *) gspca_dev;
  662. int avg_lum = atomic_read(&sd->avg_lum);
  663. int desired_lum;
  664. const int deadzone = 30;
  665. if (sd->autogain_ignore_frames < 0)
  666. return;
  667. if (sd->autogain_ignore_frames > 0) {
  668. sd->autogain_ignore_frames--;
  669. } else {
  670. desired_lum = 270 + sd->ctrls[BRIGHTNESS].val;
  671. auto_gain_n_exposure(gspca_dev, avg_lum, desired_lum,
  672. deadzone, GAIN_KNEE, EXPOSURE_KNEE);
  673. sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
  674. }
  675. }
  676. /* JPEG header */
  677. static const u8 jpeg_header[] = {
  678. 0xff, 0xd8, /* SOI: Start of Image */
  679. 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
  680. 0x00, 0x11, /* length = 17 bytes (including this length field) */
  681. 0x08, /* Precision: 8 */
  682. 0x02, 0x80, /* height = 640 (image rotated) */
  683. 0x01, 0xe0, /* width = 480 */
  684. 0x03, /* Number of image components: 3 */
  685. 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
  686. 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
  687. 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
  688. 0xff, 0xda, /* SOS: Start Of Scan */
  689. 0x00, 0x0c, /* length = 12 bytes (including this length field) */
  690. 0x03, /* number of components: 3 */
  691. 0x01, 0x00, /* selector 1, table 0x00 */
  692. 0x02, 0x11, /* selector 2, table 0x11 */
  693. 0x03, 0x11, /* selector 3, table 0x11 */
  694. 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
  695. 0x00 /* Successive approximation: 0 */
  696. };
  697. /* this function is run at interrupt level */
  698. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  699. u8 *data, /* isoc packet */
  700. int len) /* iso packet length */
  701. {
  702. struct sd *sd = (struct sd *) gspca_dev;
  703. u8 *image;
  704. u8 *sof;
  705. sof = pac_find_sof(&sd->sof_read, data, len);
  706. if (sof) {
  707. int n, lum_offset, footer_length;
  708. /* 6 bytes after the FF D9 EOF marker a number of lumination
  709. bytes are send corresponding to different parts of the
  710. image, the 14th and 15th byte after the EOF seem to
  711. correspond to the center of the image */
  712. lum_offset = 61 + sizeof pac_sof_marker;
  713. footer_length = 74;
  714. /* Finish decoding current frame */
  715. n = (sof - data) - (footer_length + sizeof pac_sof_marker);
  716. if (n < 0) {
  717. gspca_dev->image_len += n;
  718. n = 0;
  719. } else {
  720. gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
  721. }
  722. image = gspca_dev->image;
  723. if (image != NULL
  724. && image[gspca_dev->image_len - 2] == 0xff
  725. && image[gspca_dev->image_len - 1] == 0xd9)
  726. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  727. n = sof - data;
  728. len -= n;
  729. data = sof;
  730. /* Get average lumination */
  731. if (gspca_dev->last_packet_type == LAST_PACKET &&
  732. n >= lum_offset)
  733. atomic_set(&sd->avg_lum, data[-lum_offset] +
  734. data[-lum_offset + 1]);
  735. /* Start the new frame with the jpeg header */
  736. /* The PAC7302 has the image rotated 90 degrees */
  737. gspca_frame_add(gspca_dev, FIRST_PACKET,
  738. jpeg_header, sizeof jpeg_header);
  739. }
  740. gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
  741. }
  742. #ifdef CONFIG_VIDEO_ADV_DEBUG
  743. static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
  744. struct v4l2_dbg_register *reg)
  745. {
  746. u8 index;
  747. u8 value;
  748. /* reg->reg: bit0..15: reserved for register index (wIndex is 16bit
  749. long on the USB bus)
  750. */
  751. if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
  752. reg->match.addr == 0 &&
  753. (reg->reg < 0x000000ff) &&
  754. (reg->val <= 0x000000ff)
  755. ) {
  756. /* Currently writing to page 0 is only supported. */
  757. /* reg_w() only supports 8bit index */
  758. index = reg->reg;
  759. value = reg->val;
  760. /* Note that there shall be no access to other page
  761. by any other function between the page swith and
  762. the actual register write */
  763. reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
  764. reg_w(gspca_dev, index, value);
  765. reg_w(gspca_dev, 0xdc, 0x01);
  766. }
  767. return gspca_dev->usb_err;
  768. }
  769. static int sd_chip_ident(struct gspca_dev *gspca_dev,
  770. struct v4l2_dbg_chip_ident *chip)
  771. {
  772. int ret = -EINVAL;
  773. if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
  774. chip->match.addr == 0) {
  775. chip->revision = 0;
  776. chip->ident = V4L2_IDENT_UNKNOWN;
  777. ret = 0;
  778. }
  779. return ret;
  780. }
  781. #endif
  782. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  783. static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
  784. u8 *data, /* interrupt packet data */
  785. int len) /* interrput packet length */
  786. {
  787. int ret = -EINVAL;
  788. u8 data0, data1;
  789. if (len == 2) {
  790. data0 = data[0];
  791. data1 = data[1];
  792. if ((data0 == 0x00 && data1 == 0x11) ||
  793. (data0 == 0x22 && data1 == 0x33) ||
  794. (data0 == 0x44 && data1 == 0x55) ||
  795. (data0 == 0x66 && data1 == 0x77) ||
  796. (data0 == 0x88 && data1 == 0x99) ||
  797. (data0 == 0xaa && data1 == 0xbb) ||
  798. (data0 == 0xcc && data1 == 0xdd) ||
  799. (data0 == 0xee && data1 == 0xff)) {
  800. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
  801. input_sync(gspca_dev->input_dev);
  802. input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
  803. input_sync(gspca_dev->input_dev);
  804. ret = 0;
  805. }
  806. }
  807. return ret;
  808. }
  809. #endif
  810. /* sub-driver description for pac7302 */
  811. static const struct sd_desc sd_desc = {
  812. .name = KBUILD_MODNAME,
  813. .ctrls = sd_ctrls,
  814. .nctrls = ARRAY_SIZE(sd_ctrls),
  815. .config = sd_config,
  816. .init = sd_init,
  817. .start = sd_start,
  818. .stopN = sd_stopN,
  819. .stop0 = sd_stop0,
  820. .pkt_scan = sd_pkt_scan,
  821. .dq_callback = do_autogain,
  822. #ifdef CONFIG_VIDEO_ADV_DEBUG
  823. .set_register = sd_dbg_s_register,
  824. .get_chip_ident = sd_chip_ident,
  825. #endif
  826. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  827. .int_pkt_scan = sd_int_pkt_scan,
  828. #endif
  829. };
  830. /* -- module initialisation -- */
  831. static const struct usb_device_id device_table[] = {
  832. {USB_DEVICE(0x06f8, 0x3009)},
  833. {USB_DEVICE(0x06f8, 0x301b)},
  834. {USB_DEVICE(0x093a, 0x2620)},
  835. {USB_DEVICE(0x093a, 0x2621)},
  836. {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
  837. {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
  838. {USB_DEVICE(0x093a, 0x2625)},
  839. {USB_DEVICE(0x093a, 0x2626)},
  840. {USB_DEVICE(0x093a, 0x2628)},
  841. {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
  842. {USB_DEVICE(0x093a, 0x262a)},
  843. {USB_DEVICE(0x093a, 0x262c)},
  844. {USB_DEVICE(0x145f, 0x013c)},
  845. {USB_DEVICE(0x1ae7, 0x2001)}, /* SpeedLink Snappy Mic SL-6825-SBK */
  846. {}
  847. };
  848. MODULE_DEVICE_TABLE(usb, device_table);
  849. /* -- device connect -- */
  850. static int sd_probe(struct usb_interface *intf,
  851. const struct usb_device_id *id)
  852. {
  853. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  854. THIS_MODULE);
  855. }
  856. static struct usb_driver sd_driver = {
  857. .name = KBUILD_MODNAME,
  858. .id_table = device_table,
  859. .probe = sd_probe,
  860. .disconnect = gspca_disconnect,
  861. #ifdef CONFIG_PM
  862. .suspend = gspca_suspend,
  863. .resume = gspca_resume,
  864. #endif
  865. };
  866. module_usb_driver(sd_driver);