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