pac7302.c 26 KB

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