pac7302.c 26 KB

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