ov534.c 37 KB

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