ov534.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. /*
  2. * ov534-ov7xxx gspca driver
  3. *
  4. * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
  5. * Copyright (C) 2008 Jim Paris <jim@jtan.com>
  6. * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
  7. *
  8. * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
  9. * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
  10. * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
  11. *
  12. * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
  13. * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
  14. * added by Max Thrun <bear24rw@gmail.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #define MODULE_NAME "ov534"
  32. #include "gspca.h"
  33. #include <linux/fixp-arith.h>
  34. #define OV534_REG_ADDRESS 0xf1 /* sensor address */
  35. #define OV534_REG_SUBADDR 0xf2
  36. #define OV534_REG_WRITE 0xf3
  37. #define OV534_REG_READ 0xf4
  38. #define OV534_REG_OPERATION 0xf5
  39. #define OV534_REG_STATUS 0xf6
  40. #define OV534_OP_WRITE_3 0x37
  41. #define OV534_OP_WRITE_2 0x33
  42. #define OV534_OP_READ_2 0xf9
  43. #define CTRL_TIMEOUT 500
  44. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  45. MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
  46. MODULE_LICENSE("GPL");
  47. /* controls */
  48. enum e_ctrl {
  49. HUE,
  50. SATURATION,
  51. BRIGHTNESS,
  52. CONTRAST,
  53. GAIN,
  54. EXPOSURE,
  55. AGC,
  56. AWB,
  57. AEC,
  58. SHARPNESS,
  59. HFLIP,
  60. VFLIP,
  61. LIGHTFREQ,
  62. NCTRLS /* number of controls */
  63. };
  64. /* specific webcam descriptor */
  65. struct sd {
  66. struct gspca_dev gspca_dev; /* !! must be the first item */
  67. struct gspca_ctrl ctrls[NCTRLS];
  68. __u32 last_pts;
  69. u16 last_fid;
  70. u8 frame_rate;
  71. u8 sensor;
  72. };
  73. enum sensors {
  74. SENSOR_OV767x,
  75. SENSOR_OV772x,
  76. NSENSORS
  77. };
  78. /* V4L2 controls supported by the driver */
  79. static void sethue(struct gspca_dev *gspca_dev);
  80. static void setsaturation(struct gspca_dev *gspca_dev);
  81. static void setbrightness(struct gspca_dev *gspca_dev);
  82. static void setcontrast(struct gspca_dev *gspca_dev);
  83. static void setgain(struct gspca_dev *gspca_dev);
  84. static void setexposure(struct gspca_dev *gspca_dev);
  85. static void setagc(struct gspca_dev *gspca_dev);
  86. static void setawb(struct gspca_dev *gspca_dev);
  87. static void setaec(struct gspca_dev *gspca_dev);
  88. static void setsharpness(struct gspca_dev *gspca_dev);
  89. static void sethvflip(struct gspca_dev *gspca_dev);
  90. static void setlightfreq(struct gspca_dev *gspca_dev);
  91. static int sd_start(struct gspca_dev *gspca_dev);
  92. static void sd_stopN(struct gspca_dev *gspca_dev);
  93. static const struct ctrl sd_ctrls[] = {
  94. [HUE] = {
  95. {
  96. .id = V4L2_CID_HUE,
  97. .type = V4L2_CTRL_TYPE_INTEGER,
  98. .name = "Hue",
  99. .minimum = -90,
  100. .maximum = 90,
  101. .step = 1,
  102. .default_value = 0,
  103. },
  104. .set_control = sethue
  105. },
  106. [SATURATION] = {
  107. {
  108. .id = V4L2_CID_SATURATION,
  109. .type = V4L2_CTRL_TYPE_INTEGER,
  110. .name = "Saturation",
  111. .minimum = 0,
  112. .maximum = 255,
  113. .step = 1,
  114. .default_value = 64,
  115. },
  116. .set_control = setsaturation
  117. },
  118. [BRIGHTNESS] = {
  119. {
  120. .id = V4L2_CID_BRIGHTNESS,
  121. .type = V4L2_CTRL_TYPE_INTEGER,
  122. .name = "Brightness",
  123. .minimum = 0,
  124. .maximum = 255,
  125. .step = 1,
  126. .default_value = 0,
  127. },
  128. .set_control = setbrightness
  129. },
  130. [CONTRAST] = {
  131. {
  132. .id = V4L2_CID_CONTRAST,
  133. .type = V4L2_CTRL_TYPE_INTEGER,
  134. .name = "Contrast",
  135. .minimum = 0,
  136. .maximum = 255,
  137. .step = 1,
  138. .default_value = 32,
  139. },
  140. .set_control = setcontrast
  141. },
  142. [GAIN] = {
  143. {
  144. .id = V4L2_CID_GAIN,
  145. .type = V4L2_CTRL_TYPE_INTEGER,
  146. .name = "Main Gain",
  147. .minimum = 0,
  148. .maximum = 63,
  149. .step = 1,
  150. .default_value = 20,
  151. },
  152. .set_control = setgain
  153. },
  154. [EXPOSURE] = {
  155. {
  156. .id = V4L2_CID_EXPOSURE,
  157. .type = V4L2_CTRL_TYPE_INTEGER,
  158. .name = "Exposure",
  159. .minimum = 0,
  160. .maximum = 255,
  161. .step = 1,
  162. .default_value = 120,
  163. },
  164. .set_control = setexposure
  165. },
  166. [AGC] = {
  167. {
  168. .id = V4L2_CID_AUTOGAIN,
  169. .type = V4L2_CTRL_TYPE_BOOLEAN,
  170. .name = "Auto Gain",
  171. .minimum = 0,
  172. .maximum = 1,
  173. .step = 1,
  174. .default_value = 1,
  175. },
  176. .set_control = setagc
  177. },
  178. [AWB] = {
  179. {
  180. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  181. .type = V4L2_CTRL_TYPE_BOOLEAN,
  182. .name = "Auto White Balance",
  183. .minimum = 0,
  184. .maximum = 1,
  185. .step = 1,
  186. .default_value = 1,
  187. },
  188. .set_control = setawb
  189. },
  190. [AEC] = {
  191. {
  192. .id = V4L2_CID_EXPOSURE_AUTO,
  193. .type = V4L2_CTRL_TYPE_BOOLEAN,
  194. .name = "Auto Exposure",
  195. .minimum = 0,
  196. .maximum = 1,
  197. .step = 1,
  198. .default_value = 1,
  199. },
  200. .set_control = setaec
  201. },
  202. [SHARPNESS] = {
  203. {
  204. .id = V4L2_CID_SHARPNESS,
  205. .type = V4L2_CTRL_TYPE_INTEGER,
  206. .name = "Sharpness",
  207. .minimum = 0,
  208. .maximum = 63,
  209. .step = 1,
  210. .default_value = 0,
  211. },
  212. .set_control = setsharpness
  213. },
  214. [HFLIP] = {
  215. {
  216. .id = V4L2_CID_HFLIP,
  217. .type = V4L2_CTRL_TYPE_BOOLEAN,
  218. .name = "HFlip",
  219. .minimum = 0,
  220. .maximum = 1,
  221. .step = 1,
  222. .default_value = 0,
  223. },
  224. .set_control = sethvflip
  225. },
  226. [VFLIP] = {
  227. {
  228. .id = V4L2_CID_VFLIP,
  229. .type = V4L2_CTRL_TYPE_BOOLEAN,
  230. .name = "VFlip",
  231. .minimum = 0,
  232. .maximum = 1,
  233. .step = 1,
  234. .default_value = 0,
  235. },
  236. .set_control = sethvflip
  237. },
  238. [LIGHTFREQ] = {
  239. {
  240. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  241. .type = V4L2_CTRL_TYPE_MENU,
  242. .name = "Light Frequency Filter",
  243. .minimum = 0,
  244. .maximum = 1,
  245. .step = 1,
  246. .default_value = 0,
  247. },
  248. .set_control = setlightfreq
  249. },
  250. };
  251. static const struct v4l2_pix_format ov772x_mode[] = {
  252. {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  253. .bytesperline = 320 * 2,
  254. .sizeimage = 320 * 240 * 2,
  255. .colorspace = V4L2_COLORSPACE_SRGB,
  256. .priv = 1},
  257. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  258. .bytesperline = 640 * 2,
  259. .sizeimage = 640 * 480 * 2,
  260. .colorspace = V4L2_COLORSPACE_SRGB,
  261. .priv = 0},
  262. };
  263. static const struct v4l2_pix_format ov767x_mode[] = {
  264. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  265. .bytesperline = 320,
  266. .sizeimage = 320 * 240 * 3 / 8 + 590,
  267. .colorspace = V4L2_COLORSPACE_JPEG},
  268. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  269. .bytesperline = 640,
  270. .sizeimage = 640 * 480 * 3 / 8 + 590,
  271. .colorspace = V4L2_COLORSPACE_JPEG},
  272. };
  273. static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
  274. static const u8 vga_rates[] = {60, 50, 40, 30, 15};
  275. static const struct framerates ov772x_framerates[] = {
  276. { /* 320x240 */
  277. .rates = qvga_rates,
  278. .nrates = ARRAY_SIZE(qvga_rates),
  279. },
  280. { /* 640x480 */
  281. .rates = vga_rates,
  282. .nrates = ARRAY_SIZE(vga_rates),
  283. },
  284. };
  285. struct reg_array {
  286. const u8 (*val)[2];
  287. int len;
  288. };
  289. static const u8 bridge_init_767x[][2] = {
  290. /* comments from the ms-win file apollo7670.set */
  291. /* str1 */
  292. {0xf1, 0x42},
  293. {0x88, 0xf8},
  294. {0x89, 0xff},
  295. {0x76, 0x03},
  296. {0x92, 0x03},
  297. {0x95, 0x10},
  298. {0xe2, 0x00},
  299. {0xe7, 0x3e},
  300. {0x8d, 0x1c},
  301. {0x8e, 0x00},
  302. {0x8f, 0x00},
  303. {0x1f, 0x00},
  304. {0xc3, 0xf9},
  305. {0x89, 0xff},
  306. {0x88, 0xf8},
  307. {0x76, 0x03},
  308. {0x92, 0x01},
  309. {0x93, 0x18},
  310. {0x1c, 0x00},
  311. {0x1d, 0x48},
  312. {0x1d, 0x00},
  313. {0x1d, 0xff},
  314. {0x1d, 0x02},
  315. {0x1d, 0x58},
  316. {0x1d, 0x00},
  317. {0x1c, 0x0a},
  318. {0x1d, 0x0a},
  319. {0x1d, 0x0e},
  320. {0xc0, 0x50}, /* HSize 640 */
  321. {0xc1, 0x3c}, /* VSize 480 */
  322. {0x34, 0x05}, /* enable Audio Suspend mode */
  323. {0xc2, 0x0c}, /* Input YUV */
  324. {0xc3, 0xf9}, /* enable PRE */
  325. {0x34, 0x05}, /* enable Audio Suspend mode */
  326. {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
  327. {0x31, 0xf9}, /* enable 1.8V Suspend */
  328. {0x35, 0x02}, /* turn on JPEG */
  329. {0xd9, 0x10},
  330. {0x25, 0x42}, /* GPIO[8]:Input */
  331. {0x94, 0x11}, /* If the default setting is loaded when
  332. * system boots up, this flag is closed here */
  333. };
  334. static const u8 sensor_init_767x[][2] = {
  335. {0x12, 0x80},
  336. {0x11, 0x03},
  337. {0x3a, 0x04},
  338. {0x12, 0x00},
  339. {0x17, 0x13},
  340. {0x18, 0x01},
  341. {0x32, 0xb6},
  342. {0x19, 0x02},
  343. {0x1a, 0x7a},
  344. {0x03, 0x0a},
  345. {0x0c, 0x00},
  346. {0x3e, 0x00},
  347. {0x70, 0x3a},
  348. {0x71, 0x35},
  349. {0x72, 0x11},
  350. {0x73, 0xf0},
  351. {0xa2, 0x02},
  352. {0x7a, 0x2a}, /* set Gamma=1.6 below */
  353. {0x7b, 0x12},
  354. {0x7c, 0x1d},
  355. {0x7d, 0x2d},
  356. {0x7e, 0x45},
  357. {0x7f, 0x50},
  358. {0x80, 0x59},
  359. {0x81, 0x62},
  360. {0x82, 0x6b},
  361. {0x83, 0x73},
  362. {0x84, 0x7b},
  363. {0x85, 0x8a},
  364. {0x86, 0x98},
  365. {0x87, 0xb2},
  366. {0x88, 0xca},
  367. {0x89, 0xe0},
  368. {0x13, 0xe0},
  369. {0x00, 0x00},
  370. {0x10, 0x00},
  371. {0x0d, 0x40},
  372. {0x14, 0x38}, /* gain max 16x */
  373. {0xa5, 0x05},
  374. {0xab, 0x07},
  375. {0x24, 0x95},
  376. {0x25, 0x33},
  377. {0x26, 0xe3},
  378. {0x9f, 0x78},
  379. {0xa0, 0x68},
  380. {0xa1, 0x03},
  381. {0xa6, 0xd8},
  382. {0xa7, 0xd8},
  383. {0xa8, 0xf0},
  384. {0xa9, 0x90},
  385. {0xaa, 0x94},
  386. {0x13, 0xe5},
  387. {0x0e, 0x61},
  388. {0x0f, 0x4b},
  389. {0x16, 0x02},
  390. {0x21, 0x02},
  391. {0x22, 0x91},
  392. {0x29, 0x07},
  393. {0x33, 0x0b},
  394. {0x35, 0x0b},
  395. {0x37, 0x1d},
  396. {0x38, 0x71},
  397. {0x39, 0x2a},
  398. {0x3c, 0x78},
  399. {0x4d, 0x40},
  400. {0x4e, 0x20},
  401. {0x69, 0x00},
  402. {0x6b, 0x4a},
  403. {0x74, 0x10},
  404. {0x8d, 0x4f},
  405. {0x8e, 0x00},
  406. {0x8f, 0x00},
  407. {0x90, 0x00},
  408. {0x91, 0x00},
  409. {0x96, 0x00},
  410. {0x9a, 0x80},
  411. {0xb0, 0x84},
  412. {0xb1, 0x0c},
  413. {0xb2, 0x0e},
  414. {0xb3, 0x82},
  415. {0xb8, 0x0a},
  416. {0x43, 0x0a},
  417. {0x44, 0xf0},
  418. {0x45, 0x34},
  419. {0x46, 0x58},
  420. {0x47, 0x28},
  421. {0x48, 0x3a},
  422. {0x59, 0x88},
  423. {0x5a, 0x88},
  424. {0x5b, 0x44},
  425. {0x5c, 0x67},
  426. {0x5d, 0x49},
  427. {0x5e, 0x0e},
  428. {0x6c, 0x0a},
  429. {0x6d, 0x55},
  430. {0x6e, 0x11},
  431. {0x6f, 0x9f},
  432. {0x6a, 0x40},
  433. {0x01, 0x40},
  434. {0x02, 0x40},
  435. {0x13, 0xe7},
  436. {0x4f, 0x80},
  437. {0x50, 0x80},
  438. {0x51, 0x00},
  439. {0x52, 0x22},
  440. {0x53, 0x5e},
  441. {0x54, 0x80},
  442. {0x58, 0x9e},
  443. {0x41, 0x08},
  444. {0x3f, 0x00},
  445. {0x75, 0x04},
  446. {0x76, 0xe1},
  447. {0x4c, 0x00},
  448. {0x77, 0x01},
  449. {0x3d, 0xc2},
  450. {0x4b, 0x09},
  451. {0xc9, 0x60},
  452. {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
  453. {0x56, 0x40},
  454. {0x34, 0x11},
  455. {0x3b, 0xc2},
  456. {0xa4, 0x8a}, /* Night mode trigger point */
  457. {0x96, 0x00},
  458. {0x97, 0x30},
  459. {0x98, 0x20},
  460. {0x99, 0x20},
  461. {0x9a, 0x84},
  462. {0x9b, 0x29},
  463. {0x9c, 0x03},
  464. {0x9d, 0x4c},
  465. {0x9e, 0x3f},
  466. {0x78, 0x04},
  467. {0x79, 0x01},
  468. {0xc8, 0xf0},
  469. {0x79, 0x0f},
  470. {0xc8, 0x00},
  471. {0x79, 0x10},
  472. {0xc8, 0x7e},
  473. {0x79, 0x0a},
  474. {0xc8, 0x80},
  475. {0x79, 0x0b},
  476. {0xc8, 0x01},
  477. {0x79, 0x0c},
  478. {0xc8, 0x0f},
  479. {0x79, 0x0d},
  480. {0xc8, 0x20},
  481. {0x79, 0x09},
  482. {0xc8, 0x80},
  483. {0x79, 0x02},
  484. {0xc8, 0xc0},
  485. {0x79, 0x03},
  486. {0xc8, 0x20},
  487. {0x79, 0x26},
  488. };
  489. static const u8 bridge_start_vga_767x[][2] = {
  490. /* str59 JPG */
  491. {0x94, 0xaa},
  492. {0xf1, 0x42},
  493. {0xe5, 0x04},
  494. {0xc0, 0x50},
  495. {0xc1, 0x3c},
  496. {0xc2, 0x0c},
  497. {0x35, 0x02}, /* turn on JPEG */
  498. {0xd9, 0x10},
  499. {0xda, 0x00}, /* for higher clock rate(30fps) */
  500. {0x34, 0x05}, /* enable Audio Suspend mode */
  501. {0xc3, 0xf9}, /* enable PRE */
  502. {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
  503. {0x8d, 0x1c}, /* output YUV */
  504. /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
  505. {0x50, 0x00}, /* H/V divider=0 */
  506. {0x51, 0xa0}, /* input H=640/4 */
  507. {0x52, 0x3c}, /* input V=480/4 */
  508. {0x53, 0x00}, /* offset X=0 */
  509. {0x54, 0x00}, /* offset Y=0 */
  510. {0x55, 0x00}, /* H/V size[8]=0 */
  511. {0x57, 0x00}, /* H-size[9]=0 */
  512. {0x5c, 0x00}, /* output size[9:8]=0 */
  513. {0x5a, 0xa0}, /* output H=640/4 */
  514. {0x5b, 0x78}, /* output V=480/4 */
  515. {0x1c, 0x0a},
  516. {0x1d, 0x0a},
  517. {0x94, 0x11},
  518. };
  519. static const u8 sensor_start_vga_767x[][2] = {
  520. {0x11, 0x01},
  521. {0x1e, 0x04},
  522. {0x19, 0x02},
  523. {0x1a, 0x7a},
  524. };
  525. static const u8 bridge_start_qvga_767x[][2] = {
  526. /* str86 JPG */
  527. {0x94, 0xaa},
  528. {0xf1, 0x42},
  529. {0xe5, 0x04},
  530. {0xc0, 0x80},
  531. {0xc1, 0x60},
  532. {0xc2, 0x0c},
  533. {0x35, 0x02}, /* turn on JPEG */
  534. {0xd9, 0x10},
  535. {0xc0, 0x50}, /* CIF HSize 640 */
  536. {0xc1, 0x3c}, /* CIF VSize 480 */
  537. {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
  538. {0x8d, 0x1c}, /* output YUV */
  539. {0x34, 0x05}, /* enable Audio Suspend mode */
  540. {0xc2, 0x4c}, /* output YUV and Enable DCW */
  541. {0xc3, 0xf9}, /* enable PRE */
  542. {0x1c, 0x00}, /* indirect addressing */
  543. {0x1d, 0x48}, /* output YUV422 */
  544. {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
  545. {0x51, 0xa0}, /* DCW input H=640/4 */
  546. {0x52, 0x78}, /* DCW input V=480/4 */
  547. {0x53, 0x00}, /* offset X=0 */
  548. {0x54, 0x00}, /* offset Y=0 */
  549. {0x55, 0x00}, /* H/V size[8]=0 */
  550. {0x57, 0x00}, /* H-size[9]=0 */
  551. {0x5c, 0x00}, /* DCW output size[9:8]=0 */
  552. {0x5a, 0x50}, /* DCW output H=320/4 */
  553. {0x5b, 0x3c}, /* DCW output V=240/4 */
  554. {0x1c, 0x0a},
  555. {0x1d, 0x0a},
  556. {0x94, 0x11},
  557. };
  558. static const u8 sensor_start_qvga_767x[][2] = {
  559. {0x11, 0x01},
  560. {0x1e, 0x04},
  561. {0x19, 0x02},
  562. {0x1a, 0x7a},
  563. };
  564. static const u8 bridge_init_772x[][2] = {
  565. { 0xc2, 0x0c },
  566. { 0x88, 0xf8 },
  567. { 0xc3, 0x69 },
  568. { 0x89, 0xff },
  569. { 0x76, 0x03 },
  570. { 0x92, 0x01 },
  571. { 0x93, 0x18 },
  572. { 0x94, 0x10 },
  573. { 0x95, 0x10 },
  574. { 0xe2, 0x00 },
  575. { 0xe7, 0x3e },
  576. { 0x96, 0x00 },
  577. { 0x97, 0x20 },
  578. { 0x97, 0x20 },
  579. { 0x97, 0x20 },
  580. { 0x97, 0x0a },
  581. { 0x97, 0x3f },
  582. { 0x97, 0x4a },
  583. { 0x97, 0x20 },
  584. { 0x97, 0x15 },
  585. { 0x97, 0x0b },
  586. { 0x8e, 0x40 },
  587. { 0x1f, 0x81 },
  588. { 0x34, 0x05 },
  589. { 0xe3, 0x04 },
  590. { 0x88, 0x00 },
  591. { 0x89, 0x00 },
  592. { 0x76, 0x00 },
  593. { 0xe7, 0x2e },
  594. { 0x31, 0xf9 },
  595. { 0x25, 0x42 },
  596. { 0x21, 0xf0 },
  597. { 0x1c, 0x00 },
  598. { 0x1d, 0x40 },
  599. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  600. { 0x1d, 0x00 }, /* payload size */
  601. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  602. { 0x1d, 0x58 }, /* frame size */
  603. { 0x1d, 0x00 }, /* frame size */
  604. { 0x1c, 0x0a },
  605. { 0x1d, 0x08 }, /* turn on UVC header */
  606. { 0x1d, 0x0e }, /* .. */
  607. { 0x8d, 0x1c },
  608. { 0x8e, 0x80 },
  609. { 0xe5, 0x04 },
  610. { 0xc0, 0x50 },
  611. { 0xc1, 0x3c },
  612. { 0xc2, 0x0c },
  613. };
  614. static const u8 sensor_init_772x[][2] = {
  615. { 0x12, 0x80 },
  616. { 0x11, 0x01 },
  617. /*fixme: better have a delay?*/
  618. { 0x11, 0x01 },
  619. { 0x11, 0x01 },
  620. { 0x11, 0x01 },
  621. { 0x11, 0x01 },
  622. { 0x11, 0x01 },
  623. { 0x11, 0x01 },
  624. { 0x11, 0x01 },
  625. { 0x11, 0x01 },
  626. { 0x11, 0x01 },
  627. { 0x11, 0x01 },
  628. { 0x3d, 0x03 },
  629. { 0x17, 0x26 },
  630. { 0x18, 0xa0 },
  631. { 0x19, 0x07 },
  632. { 0x1a, 0xf0 },
  633. { 0x32, 0x00 },
  634. { 0x29, 0xa0 },
  635. { 0x2c, 0xf0 },
  636. { 0x65, 0x20 },
  637. { 0x11, 0x01 },
  638. { 0x42, 0x7f },
  639. { 0x63, 0xaa }, /* AWB - was e0 */
  640. { 0x64, 0xff },
  641. { 0x66, 0x00 },
  642. { 0x13, 0xf0 }, /* com8 */
  643. { 0x0d, 0x41 },
  644. { 0x0f, 0xc5 },
  645. { 0x14, 0x11 },
  646. { 0x22, 0x7f },
  647. { 0x23, 0x03 },
  648. { 0x24, 0x40 },
  649. { 0x25, 0x30 },
  650. { 0x26, 0xa1 },
  651. { 0x2a, 0x00 },
  652. { 0x2b, 0x00 },
  653. { 0x6b, 0xaa },
  654. { 0x13, 0xff }, /* AWB */
  655. { 0x90, 0x05 },
  656. { 0x91, 0x01 },
  657. { 0x92, 0x03 },
  658. { 0x93, 0x00 },
  659. { 0x94, 0x60 },
  660. { 0x95, 0x3c },
  661. { 0x96, 0x24 },
  662. { 0x97, 0x1e },
  663. { 0x98, 0x62 },
  664. { 0x99, 0x80 },
  665. { 0x9a, 0x1e },
  666. { 0x9b, 0x08 },
  667. { 0x9c, 0x20 },
  668. { 0x9e, 0x81 },
  669. { 0xa6, 0x07 },
  670. { 0x7e, 0x0c },
  671. { 0x7f, 0x16 },
  672. { 0x80, 0x2a },
  673. { 0x81, 0x4e },
  674. { 0x82, 0x61 },
  675. { 0x83, 0x6f },
  676. { 0x84, 0x7b },
  677. { 0x85, 0x86 },
  678. { 0x86, 0x8e },
  679. { 0x87, 0x97 },
  680. { 0x88, 0xa4 },
  681. { 0x89, 0xaf },
  682. { 0x8a, 0xc5 },
  683. { 0x8b, 0xd7 },
  684. { 0x8c, 0xe8 },
  685. { 0x8d, 0x20 },
  686. { 0x0c, 0x90 },
  687. { 0x2b, 0x00 },
  688. { 0x22, 0x7f },
  689. { 0x23, 0x03 },
  690. { 0x11, 0x01 },
  691. { 0x0c, 0xd0 },
  692. { 0x64, 0xff },
  693. { 0x0d, 0x41 },
  694. { 0x14, 0x41 },
  695. { 0x0e, 0xcd },
  696. { 0xac, 0xbf },
  697. { 0x8e, 0x00 }, /* De-noise threshold */
  698. { 0x0c, 0xd0 }
  699. };
  700. static const u8 bridge_start_vga_772x[][2] = {
  701. {0x1c, 0x00},
  702. {0x1d, 0x40},
  703. {0x1d, 0x02},
  704. {0x1d, 0x00},
  705. {0x1d, 0x02},
  706. {0x1d, 0x58},
  707. {0x1d, 0x00},
  708. {0xc0, 0x50},
  709. {0xc1, 0x3c},
  710. };
  711. static const u8 sensor_start_vga_772x[][2] = {
  712. {0x12, 0x00},
  713. {0x17, 0x26},
  714. {0x18, 0xa0},
  715. {0x19, 0x07},
  716. {0x1a, 0xf0},
  717. {0x29, 0xa0},
  718. {0x2c, 0xf0},
  719. {0x65, 0x20},
  720. };
  721. static const u8 bridge_start_qvga_772x[][2] = {
  722. {0x1c, 0x00},
  723. {0x1d, 0x40},
  724. {0x1d, 0x02},
  725. {0x1d, 0x00},
  726. {0x1d, 0x01},
  727. {0x1d, 0x4b},
  728. {0x1d, 0x00},
  729. {0xc0, 0x28},
  730. {0xc1, 0x1e},
  731. };
  732. static const u8 sensor_start_qvga_772x[][2] = {
  733. {0x12, 0x40},
  734. {0x17, 0x3f},
  735. {0x18, 0x50},
  736. {0x19, 0x03},
  737. {0x1a, 0x78},
  738. {0x29, 0x50},
  739. {0x2c, 0x78},
  740. {0x65, 0x2f},
  741. };
  742. static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
  743. {
  744. struct usb_device *udev = gspca_dev->dev;
  745. int ret;
  746. if (gspca_dev->usb_err < 0)
  747. return;
  748. PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
  749. gspca_dev->usb_buf[0] = val;
  750. ret = usb_control_msg(udev,
  751. usb_sndctrlpipe(udev, 0),
  752. 0x01,
  753. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  754. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  755. if (ret < 0) {
  756. pr_err("write failed %d\n", ret);
  757. gspca_dev->usb_err = ret;
  758. }
  759. }
  760. static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  761. {
  762. struct usb_device *udev = gspca_dev->dev;
  763. int ret;
  764. if (gspca_dev->usb_err < 0)
  765. return 0;
  766. ret = usb_control_msg(udev,
  767. usb_rcvctrlpipe(udev, 0),
  768. 0x01,
  769. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  770. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  771. PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
  772. if (ret < 0) {
  773. pr_err("read failed %d\n", ret);
  774. gspca_dev->usb_err = ret;
  775. }
  776. return gspca_dev->usb_buf[0];
  777. }
  778. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  779. * (direction and output)? */
  780. static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
  781. {
  782. u8 data;
  783. PDEBUG(D_CONF, "led status: %d", status);
  784. data = ov534_reg_read(gspca_dev, 0x21);
  785. data |= 0x80;
  786. ov534_reg_write(gspca_dev, 0x21, data);
  787. data = ov534_reg_read(gspca_dev, 0x23);
  788. if (status)
  789. data |= 0x80;
  790. else
  791. data &= ~0x80;
  792. ov534_reg_write(gspca_dev, 0x23, data);
  793. if (!status) {
  794. data = ov534_reg_read(gspca_dev, 0x21);
  795. data &= ~0x80;
  796. ov534_reg_write(gspca_dev, 0x21, data);
  797. }
  798. }
  799. static int sccb_check_status(struct gspca_dev *gspca_dev)
  800. {
  801. u8 data;
  802. int i;
  803. for (i = 0; i < 5; i++) {
  804. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  805. switch (data) {
  806. case 0x00:
  807. return 1;
  808. case 0x04:
  809. return 0;
  810. case 0x03:
  811. break;
  812. default:
  813. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
  814. data, i + 1);
  815. }
  816. }
  817. return 0;
  818. }
  819. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  820. {
  821. PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
  822. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  823. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  824. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  825. if (!sccb_check_status(gspca_dev)) {
  826. pr_err("sccb_reg_write failed\n");
  827. gspca_dev->usb_err = -EIO;
  828. }
  829. }
  830. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  831. {
  832. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  833. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  834. if (!sccb_check_status(gspca_dev))
  835. pr_err("sccb_reg_read failed 1\n");
  836. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  837. if (!sccb_check_status(gspca_dev))
  838. pr_err("sccb_reg_read failed 2\n");
  839. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  840. }
  841. /* output a bridge sequence (reg - val) */
  842. static void reg_w_array(struct gspca_dev *gspca_dev,
  843. const u8 (*data)[2], int len)
  844. {
  845. while (--len >= 0) {
  846. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  847. data++;
  848. }
  849. }
  850. /* output a sensor sequence (reg - val) */
  851. static void sccb_w_array(struct gspca_dev *gspca_dev,
  852. const u8 (*data)[2], int len)
  853. {
  854. while (--len >= 0) {
  855. if ((*data)[0] != 0xff) {
  856. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  857. } else {
  858. sccb_reg_read(gspca_dev, (*data)[1]);
  859. sccb_reg_write(gspca_dev, 0xff, 0x00);
  860. }
  861. data++;
  862. }
  863. }
  864. /* ov772x specific controls */
  865. static void set_frame_rate(struct gspca_dev *gspca_dev)
  866. {
  867. struct sd *sd = (struct sd *) gspca_dev;
  868. int i;
  869. struct rate_s {
  870. u8 fps;
  871. u8 r11;
  872. u8 r0d;
  873. u8 re5;
  874. };
  875. const struct rate_s *r;
  876. static const struct rate_s rate_0[] = { /* 640x480 */
  877. {60, 0x01, 0xc1, 0x04},
  878. {50, 0x01, 0x41, 0x02},
  879. {40, 0x02, 0xc1, 0x04},
  880. {30, 0x04, 0x81, 0x02},
  881. {15, 0x03, 0x41, 0x04},
  882. };
  883. static const struct rate_s rate_1[] = { /* 320x240 */
  884. {125, 0x02, 0x81, 0x02},
  885. {100, 0x02, 0xc1, 0x04},
  886. {75, 0x03, 0xc1, 0x04},
  887. {60, 0x04, 0xc1, 0x04},
  888. {50, 0x02, 0x41, 0x04},
  889. {40, 0x03, 0x41, 0x04},
  890. {30, 0x04, 0x41, 0x04},
  891. };
  892. if (sd->sensor != SENSOR_OV772x)
  893. return;
  894. if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
  895. r = rate_0;
  896. i = ARRAY_SIZE(rate_0);
  897. } else {
  898. r = rate_1;
  899. i = ARRAY_SIZE(rate_1);
  900. }
  901. while (--i > 0) {
  902. if (sd->frame_rate >= r->fps)
  903. break;
  904. r++;
  905. }
  906. sccb_reg_write(gspca_dev, 0x11, r->r11);
  907. sccb_reg_write(gspca_dev, 0x0d, r->r0d);
  908. ov534_reg_write(gspca_dev, 0xe5, r->re5);
  909. PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
  910. }
  911. static void sethue(struct gspca_dev *gspca_dev)
  912. {
  913. struct sd *sd = (struct sd *) gspca_dev;
  914. int val;
  915. val = sd->ctrls[HUE].val;
  916. if (sd->sensor == SENSOR_OV767x) {
  917. /* TBD */
  918. } else {
  919. s16 huesin;
  920. s16 huecos;
  921. /* fixp_sin and fixp_cos accept only positive values, while
  922. * our val is between -90 and 90
  923. */
  924. val += 360;
  925. /* According to the datasheet the registers expect HUESIN and
  926. * HUECOS to be the result of the trigonometric functions,
  927. * scaled by 0x80.
  928. *
  929. * The 0x100 here represents the maximun absolute value
  930. * returned byt fixp_sin and fixp_cos, so the scaling will
  931. * consider the result like in the interval [-1.0, 1.0].
  932. */
  933. huesin = fixp_sin(val) * 0x80 / 0x100;
  934. huecos = fixp_cos(val) * 0x80 / 0x100;
  935. if (huesin < 0) {
  936. sccb_reg_write(gspca_dev, 0xab,
  937. sccb_reg_read(gspca_dev, 0xab) | 0x2);
  938. huesin = -huesin;
  939. } else {
  940. sccb_reg_write(gspca_dev, 0xab,
  941. sccb_reg_read(gspca_dev, 0xab) & ~0x2);
  942. }
  943. sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
  944. sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
  945. }
  946. }
  947. static void setsaturation(struct gspca_dev *gspca_dev)
  948. {
  949. struct sd *sd = (struct sd *) gspca_dev;
  950. int val;
  951. val = sd->ctrls[SATURATION].val;
  952. if (sd->sensor == SENSOR_OV767x) {
  953. int i;
  954. static u8 color_tb[][6] = {
  955. {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
  956. {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
  957. {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
  958. {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
  959. {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
  960. {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
  961. {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
  962. };
  963. for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
  964. sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
  965. } else {
  966. sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
  967. sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
  968. }
  969. }
  970. static void setbrightness(struct gspca_dev *gspca_dev)
  971. {
  972. struct sd *sd = (struct sd *) gspca_dev;
  973. int val;
  974. val = sd->ctrls[BRIGHTNESS].val;
  975. if (sd->sensor == SENSOR_OV767x) {
  976. if (val < 0)
  977. val = 0x80 - val;
  978. sccb_reg_write(gspca_dev, 0x55, val); /* bright */
  979. } else {
  980. sccb_reg_write(gspca_dev, 0x9b, val);
  981. }
  982. }
  983. static void setcontrast(struct gspca_dev *gspca_dev)
  984. {
  985. struct sd *sd = (struct sd *) gspca_dev;
  986. u8 val;
  987. val = sd->ctrls[CONTRAST].val;
  988. if (sd->sensor == SENSOR_OV767x)
  989. sccb_reg_write(gspca_dev, 0x56, val); /* contras */
  990. else
  991. sccb_reg_write(gspca_dev, 0x9c, val);
  992. }
  993. static void setgain(struct gspca_dev *gspca_dev)
  994. {
  995. struct sd *sd = (struct sd *) gspca_dev;
  996. u8 val;
  997. if (sd->ctrls[AGC].val)
  998. return;
  999. val = sd->ctrls[GAIN].val;
  1000. switch (val & 0x30) {
  1001. case 0x00:
  1002. val &= 0x0f;
  1003. break;
  1004. case 0x10:
  1005. val &= 0x0f;
  1006. val |= 0x30;
  1007. break;
  1008. case 0x20:
  1009. val &= 0x0f;
  1010. val |= 0x70;
  1011. break;
  1012. default:
  1013. /* case 0x30: */
  1014. val &= 0x0f;
  1015. val |= 0xf0;
  1016. break;
  1017. }
  1018. sccb_reg_write(gspca_dev, 0x00, val);
  1019. }
  1020. static void setexposure(struct gspca_dev *gspca_dev)
  1021. {
  1022. struct sd *sd = (struct sd *) gspca_dev;
  1023. u8 val;
  1024. if (sd->ctrls[AEC].val)
  1025. return;
  1026. val = sd->ctrls[EXPOSURE].val;
  1027. if (sd->sensor == SENSOR_OV767x) {
  1028. /* set only aec[9:2] */
  1029. sccb_reg_write(gspca_dev, 0x10, val); /* aech */
  1030. } else {
  1031. /* 'val' is one byte and represents half of the exposure value
  1032. * we are going to set into registers, a two bytes value:
  1033. *
  1034. * MSB: ((u16) val << 1) >> 8 == val >> 7
  1035. * LSB: ((u16) val << 1) & 0xff == val << 1
  1036. */
  1037. sccb_reg_write(gspca_dev, 0x08, val >> 7);
  1038. sccb_reg_write(gspca_dev, 0x10, val << 1);
  1039. }
  1040. }
  1041. static void setagc(struct gspca_dev *gspca_dev)
  1042. {
  1043. struct sd *sd = (struct sd *) gspca_dev;
  1044. if (sd->ctrls[AGC].val) {
  1045. sccb_reg_write(gspca_dev, 0x13,
  1046. sccb_reg_read(gspca_dev, 0x13) | 0x04);
  1047. sccb_reg_write(gspca_dev, 0x64,
  1048. sccb_reg_read(gspca_dev, 0x64) | 0x03);
  1049. } else {
  1050. sccb_reg_write(gspca_dev, 0x13,
  1051. sccb_reg_read(gspca_dev, 0x13) & ~0x04);
  1052. sccb_reg_write(gspca_dev, 0x64,
  1053. sccb_reg_read(gspca_dev, 0x64) & ~0x03);
  1054. setgain(gspca_dev);
  1055. }
  1056. }
  1057. static void setawb(struct gspca_dev *gspca_dev)
  1058. {
  1059. struct sd *sd = (struct sd *) gspca_dev;
  1060. if (sd->ctrls[AWB].val) {
  1061. sccb_reg_write(gspca_dev, 0x13,
  1062. sccb_reg_read(gspca_dev, 0x13) | 0x02);
  1063. if (sd->sensor == SENSOR_OV772x)
  1064. sccb_reg_write(gspca_dev, 0x63,
  1065. sccb_reg_read(gspca_dev, 0x63) | 0xc0);
  1066. } else {
  1067. sccb_reg_write(gspca_dev, 0x13,
  1068. sccb_reg_read(gspca_dev, 0x13) & ~0x02);
  1069. if (sd->sensor == SENSOR_OV772x)
  1070. sccb_reg_write(gspca_dev, 0x63,
  1071. sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
  1072. }
  1073. }
  1074. static void setaec(struct gspca_dev *gspca_dev)
  1075. {
  1076. struct sd *sd = (struct sd *) gspca_dev;
  1077. u8 data;
  1078. data = sd->sensor == SENSOR_OV767x ?
  1079. 0x05 : /* agc + aec */
  1080. 0x01; /* agc */
  1081. if (sd->ctrls[AEC].val)
  1082. sccb_reg_write(gspca_dev, 0x13,
  1083. sccb_reg_read(gspca_dev, 0x13) | data);
  1084. else {
  1085. sccb_reg_write(gspca_dev, 0x13,
  1086. sccb_reg_read(gspca_dev, 0x13) & ~data);
  1087. if (sd->sensor == SENSOR_OV767x)
  1088. sd->ctrls[EXPOSURE].val =
  1089. sccb_reg_read(gspca_dev, 10); /* aech */
  1090. else
  1091. setexposure(gspca_dev);
  1092. }
  1093. }
  1094. static void setsharpness(struct gspca_dev *gspca_dev)
  1095. {
  1096. struct sd *sd = (struct sd *) gspca_dev;
  1097. u8 val;
  1098. val = sd->ctrls[SHARPNESS].val;
  1099. sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
  1100. sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
  1101. }
  1102. static void sethvflip(struct gspca_dev *gspca_dev)
  1103. {
  1104. struct sd *sd = (struct sd *) gspca_dev;
  1105. u8 val;
  1106. if (sd->sensor == SENSOR_OV767x) {
  1107. val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
  1108. val &= ~0x30;
  1109. if (sd->ctrls[HFLIP].val)
  1110. val |= 0x20;
  1111. if (sd->ctrls[VFLIP].val)
  1112. val |= 0x10;
  1113. sccb_reg_write(gspca_dev, 0x1e, val);
  1114. } else {
  1115. val = sccb_reg_read(gspca_dev, 0x0c);
  1116. val &= ~0xc0;
  1117. if (sd->ctrls[HFLIP].val == 0)
  1118. val |= 0x40;
  1119. if (sd->ctrls[VFLIP].val == 0)
  1120. val |= 0x80;
  1121. sccb_reg_write(gspca_dev, 0x0c, val);
  1122. }
  1123. }
  1124. static void setlightfreq(struct gspca_dev *gspca_dev)
  1125. {
  1126. struct sd *sd = (struct sd *) gspca_dev;
  1127. u8 val;
  1128. val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
  1129. if (sd->sensor == SENSOR_OV767x) {
  1130. sccb_reg_write(gspca_dev, 0x2a, 0x00);
  1131. if (val)
  1132. val = 0x9d; /* insert dummy to 25fps for 50Hz */
  1133. }
  1134. sccb_reg_write(gspca_dev, 0x2b, val);
  1135. }
  1136. /* this function is called at probe time */
  1137. static int sd_config(struct gspca_dev *gspca_dev,
  1138. const struct usb_device_id *id)
  1139. {
  1140. struct sd *sd = (struct sd *) gspca_dev;
  1141. struct cam *cam;
  1142. cam = &gspca_dev->cam;
  1143. cam->ctrls = sd->ctrls;
  1144. cam->cam_mode = ov772x_mode;
  1145. cam->nmodes = ARRAY_SIZE(ov772x_mode);
  1146. sd->frame_rate = 30;
  1147. return 0;
  1148. }
  1149. /* this function is called at probe and resume time */
  1150. static int sd_init(struct gspca_dev *gspca_dev)
  1151. {
  1152. struct sd *sd = (struct sd *) gspca_dev;
  1153. u16 sensor_id;
  1154. static const struct reg_array bridge_init[NSENSORS] = {
  1155. [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
  1156. [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
  1157. };
  1158. static const struct reg_array sensor_init[NSENSORS] = {
  1159. [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
  1160. [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
  1161. };
  1162. /* reset bridge */
  1163. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  1164. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  1165. msleep(100);
  1166. /* initialize the sensor address */
  1167. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
  1168. /* reset sensor */
  1169. sccb_reg_write(gspca_dev, 0x12, 0x80);
  1170. msleep(10);
  1171. /* probe the sensor */
  1172. sccb_reg_read(gspca_dev, 0x0a);
  1173. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  1174. sccb_reg_read(gspca_dev, 0x0b);
  1175. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  1176. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  1177. if ((sensor_id & 0xfff0) == 0x7670) {
  1178. sd->sensor = SENSOR_OV767x;
  1179. gspca_dev->ctrl_dis = (1 << HUE) |
  1180. (1 << GAIN) |
  1181. (1 << AGC) |
  1182. (1 << SHARPNESS); /* auto */
  1183. sd->ctrls[SATURATION].min = 0,
  1184. sd->ctrls[SATURATION].max = 6,
  1185. sd->ctrls[SATURATION].def = 3,
  1186. sd->ctrls[BRIGHTNESS].min = -127;
  1187. sd->ctrls[BRIGHTNESS].max = 127;
  1188. sd->ctrls[BRIGHTNESS].def = 0;
  1189. sd->ctrls[CONTRAST].max = 0x80;
  1190. sd->ctrls[CONTRAST].def = 0x40;
  1191. sd->ctrls[EXPOSURE].min = 0x08;
  1192. sd->ctrls[EXPOSURE].max = 0x60;
  1193. sd->ctrls[EXPOSURE].def = 0x13;
  1194. sd->ctrls[SHARPNESS].max = 9;
  1195. sd->ctrls[SHARPNESS].def = 4;
  1196. sd->ctrls[HFLIP].def = 1;
  1197. gspca_dev->cam.cam_mode = ov767x_mode;
  1198. gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
  1199. } else {
  1200. sd->sensor = SENSOR_OV772x;
  1201. gspca_dev->cam.bulk = 1;
  1202. gspca_dev->cam.bulk_size = 16384;
  1203. gspca_dev->cam.bulk_nurbs = 2;
  1204. gspca_dev->cam.mode_framerates = ov772x_framerates;
  1205. }
  1206. /* initialize */
  1207. reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
  1208. bridge_init[sd->sensor].len);
  1209. ov534_set_led(gspca_dev, 1);
  1210. sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
  1211. sensor_init[sd->sensor].len);
  1212. if (sd->sensor == SENSOR_OV767x)
  1213. sd_start(gspca_dev);
  1214. sd_stopN(gspca_dev);
  1215. /* set_frame_rate(gspca_dev); */
  1216. return gspca_dev->usb_err;
  1217. }
  1218. static int sd_start(struct gspca_dev *gspca_dev)
  1219. {
  1220. struct sd *sd = (struct sd *) gspca_dev;
  1221. int mode;
  1222. static const struct reg_array bridge_start[NSENSORS][2] = {
  1223. [SENSOR_OV767x] = {{bridge_start_qvga_767x,
  1224. ARRAY_SIZE(bridge_start_qvga_767x)},
  1225. {bridge_start_vga_767x,
  1226. ARRAY_SIZE(bridge_start_vga_767x)}},
  1227. [SENSOR_OV772x] = {{bridge_start_qvga_772x,
  1228. ARRAY_SIZE(bridge_start_qvga_772x)},
  1229. {bridge_start_vga_772x,
  1230. ARRAY_SIZE(bridge_start_vga_772x)}},
  1231. };
  1232. static const struct reg_array sensor_start[NSENSORS][2] = {
  1233. [SENSOR_OV767x] = {{sensor_start_qvga_767x,
  1234. ARRAY_SIZE(sensor_start_qvga_767x)},
  1235. {sensor_start_vga_767x,
  1236. ARRAY_SIZE(sensor_start_vga_767x)}},
  1237. [SENSOR_OV772x] = {{sensor_start_qvga_772x,
  1238. ARRAY_SIZE(sensor_start_qvga_772x)},
  1239. {sensor_start_vga_772x,
  1240. ARRAY_SIZE(sensor_start_vga_772x)}},
  1241. };
  1242. /* (from ms-win trace) */
  1243. if (sd->sensor == SENSOR_OV767x)
  1244. sccb_reg_write(gspca_dev, 0x1e, 0x04);
  1245. /* black sun enable ? */
  1246. mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
  1247. reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
  1248. bridge_start[sd->sensor][mode].len);
  1249. sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
  1250. sensor_start[sd->sensor][mode].len);
  1251. set_frame_rate(gspca_dev);
  1252. if (!(gspca_dev->ctrl_dis & (1 << HUE)))
  1253. sethue(gspca_dev);
  1254. setsaturation(gspca_dev);
  1255. if (!(gspca_dev->ctrl_dis & (1 << AGC)))
  1256. setagc(gspca_dev);
  1257. setawb(gspca_dev);
  1258. setaec(gspca_dev);
  1259. if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
  1260. setgain(gspca_dev);
  1261. setexposure(gspca_dev);
  1262. setbrightness(gspca_dev);
  1263. setcontrast(gspca_dev);
  1264. if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
  1265. setsharpness(gspca_dev);
  1266. sethvflip(gspca_dev);
  1267. setlightfreq(gspca_dev);
  1268. ov534_set_led(gspca_dev, 1);
  1269. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  1270. return gspca_dev->usb_err;
  1271. }
  1272. static void sd_stopN(struct gspca_dev *gspca_dev)
  1273. {
  1274. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  1275. ov534_set_led(gspca_dev, 0);
  1276. }
  1277. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  1278. #define UVC_STREAM_EOH (1 << 7)
  1279. #define UVC_STREAM_ERR (1 << 6)
  1280. #define UVC_STREAM_STI (1 << 5)
  1281. #define UVC_STREAM_RES (1 << 4)
  1282. #define UVC_STREAM_SCR (1 << 3)
  1283. #define UVC_STREAM_PTS (1 << 2)
  1284. #define UVC_STREAM_EOF (1 << 1)
  1285. #define UVC_STREAM_FID (1 << 0)
  1286. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1287. u8 *data, int len)
  1288. {
  1289. struct sd *sd = (struct sd *) gspca_dev;
  1290. __u32 this_pts;
  1291. u16 this_fid;
  1292. int remaining_len = len;
  1293. int payload_len;
  1294. payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
  1295. do {
  1296. len = min(remaining_len, payload_len);
  1297. /* Payloads are prefixed with a UVC-style header. We
  1298. consider a frame to start when the FID toggles, or the PTS
  1299. changes. A frame ends when EOF is set, and we've received
  1300. the correct number of bytes. */
  1301. /* Verify UVC header. Header length is always 12 */
  1302. if (data[0] != 12 || len < 12) {
  1303. PDEBUG(D_PACK, "bad header");
  1304. goto discard;
  1305. }
  1306. /* Check errors */
  1307. if (data[1] & UVC_STREAM_ERR) {
  1308. PDEBUG(D_PACK, "payload error");
  1309. goto discard;
  1310. }
  1311. /* Extract PTS and FID */
  1312. if (!(data[1] & UVC_STREAM_PTS)) {
  1313. PDEBUG(D_PACK, "PTS not present");
  1314. goto discard;
  1315. }
  1316. this_pts = (data[5] << 24) | (data[4] << 16)
  1317. | (data[3] << 8) | data[2];
  1318. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  1319. /* If PTS or FID has changed, start a new frame. */
  1320. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  1321. if (gspca_dev->last_packet_type == INTER_PACKET)
  1322. gspca_frame_add(gspca_dev, LAST_PACKET,
  1323. NULL, 0);
  1324. sd->last_pts = this_pts;
  1325. sd->last_fid = this_fid;
  1326. gspca_frame_add(gspca_dev, FIRST_PACKET,
  1327. data + 12, len - 12);
  1328. /* If this packet is marked as EOF, end the frame */
  1329. } else if (data[1] & UVC_STREAM_EOF) {
  1330. sd->last_pts = 0;
  1331. if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
  1332. && gspca_dev->image_len + len - 12 !=
  1333. gspca_dev->width * gspca_dev->height * 2) {
  1334. PDEBUG(D_PACK, "wrong sized frame");
  1335. goto discard;
  1336. }
  1337. gspca_frame_add(gspca_dev, LAST_PACKET,
  1338. data + 12, len - 12);
  1339. } else {
  1340. /* Add the data from this payload */
  1341. gspca_frame_add(gspca_dev, INTER_PACKET,
  1342. data + 12, len - 12);
  1343. }
  1344. /* Done this payload */
  1345. goto scan_next;
  1346. discard:
  1347. /* Discard data until a new frame starts. */
  1348. gspca_dev->last_packet_type = DISCARD_PACKET;
  1349. scan_next:
  1350. remaining_len -= len;
  1351. data += len;
  1352. } while (remaining_len > 0);
  1353. }
  1354. static int sd_querymenu(struct gspca_dev *gspca_dev,
  1355. struct v4l2_querymenu *menu)
  1356. {
  1357. switch (menu->id) {
  1358. case V4L2_CID_POWER_LINE_FREQUENCY:
  1359. switch (menu->index) {
  1360. case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
  1361. strcpy((char *) menu->name, "Disabled");
  1362. return 0;
  1363. case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
  1364. strcpy((char *) menu->name, "50 Hz");
  1365. return 0;
  1366. }
  1367. break;
  1368. }
  1369. return -EINVAL;
  1370. }
  1371. /* get stream parameters (framerate) */
  1372. static void sd_get_streamparm(struct gspca_dev *gspca_dev,
  1373. struct v4l2_streamparm *parm)
  1374. {
  1375. struct v4l2_captureparm *cp = &parm->parm.capture;
  1376. struct v4l2_fract *tpf = &cp->timeperframe;
  1377. struct sd *sd = (struct sd *) gspca_dev;
  1378. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  1379. tpf->numerator = 1;
  1380. tpf->denominator = sd->frame_rate;
  1381. }
  1382. /* set stream parameters (framerate) */
  1383. static void sd_set_streamparm(struct gspca_dev *gspca_dev,
  1384. struct v4l2_streamparm *parm)
  1385. {
  1386. struct v4l2_captureparm *cp = &parm->parm.capture;
  1387. struct v4l2_fract *tpf = &cp->timeperframe;
  1388. struct sd *sd = (struct sd *) gspca_dev;
  1389. /* Set requested framerate */
  1390. sd->frame_rate = tpf->denominator / tpf->numerator;
  1391. if (gspca_dev->streaming)
  1392. set_frame_rate(gspca_dev);
  1393. /* Return the actual framerate */
  1394. tpf->numerator = 1;
  1395. tpf->denominator = sd->frame_rate;
  1396. }
  1397. /* sub-driver description */
  1398. static const struct sd_desc sd_desc = {
  1399. .name = MODULE_NAME,
  1400. .ctrls = sd_ctrls,
  1401. .nctrls = ARRAY_SIZE(sd_ctrls),
  1402. .config = sd_config,
  1403. .init = sd_init,
  1404. .start = sd_start,
  1405. .stopN = sd_stopN,
  1406. .pkt_scan = sd_pkt_scan,
  1407. .querymenu = sd_querymenu,
  1408. .get_streamparm = sd_get_streamparm,
  1409. .set_streamparm = sd_set_streamparm,
  1410. };
  1411. /* -- module initialisation -- */
  1412. static const struct usb_device_id device_table[] = {
  1413. {USB_DEVICE(0x1415, 0x2000)},
  1414. {USB_DEVICE(0x06f8, 0x3002)},
  1415. {}
  1416. };
  1417. MODULE_DEVICE_TABLE(usb, device_table);
  1418. /* -- device connect -- */
  1419. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1420. {
  1421. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1422. THIS_MODULE);
  1423. }
  1424. static struct usb_driver sd_driver = {
  1425. .name = MODULE_NAME,
  1426. .id_table = device_table,
  1427. .probe = sd_probe,
  1428. .disconnect = gspca_disconnect,
  1429. #ifdef CONFIG_PM
  1430. .suspend = gspca_suspend,
  1431. .resume = gspca_resume,
  1432. #endif
  1433. };
  1434. module_usb_driver(sd_driver);