pac7302.c 26 KB

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