pac7302.c 27 KB

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