ov534.c 36 KB

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