ov534.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. * ov534 gspca driver
  3. * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
  4. * Copyright (C) 2008 Jim Paris <jim@jtan.com>
  5. * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
  6. *
  7. * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
  8. * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
  9. * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #define MODULE_NAME "ov534"
  26. #include "gspca.h"
  27. #define OV534_REG_ADDRESS 0xf1 /* sensor address */
  28. #define OV534_REG_SUBADDR 0xf2
  29. #define OV534_REG_WRITE 0xf3
  30. #define OV534_REG_READ 0xf4
  31. #define OV534_REG_OPERATION 0xf5
  32. #define OV534_REG_STATUS 0xf6
  33. #define OV534_OP_WRITE_3 0x37
  34. #define OV534_OP_WRITE_2 0x33
  35. #define OV534_OP_READ_2 0xf9
  36. #define CTRL_TIMEOUT 500
  37. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  38. MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
  39. MODULE_LICENSE("GPL");
  40. /* specific webcam descriptor */
  41. struct sd {
  42. struct gspca_dev gspca_dev; /* !! must be the first item */
  43. __u32 last_pts;
  44. u16 last_fid;
  45. u8 frame_rate;
  46. u8 sensor;
  47. #define SENSOR_OV772X 0
  48. #define SENSOR_OV965X 1
  49. };
  50. /* V4L2 controls supported by the driver */
  51. static struct ctrl sd_ctrls[] = {
  52. };
  53. static const struct v4l2_pix_format vga_yuyv_mode[] = {
  54. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  55. .bytesperline = 640 * 2,
  56. .sizeimage = 640 * 480 * 2,
  57. .colorspace = V4L2_COLORSPACE_SRGB,
  58. .priv = 0},
  59. };
  60. static const struct v4l2_pix_format vga_jpeg_mode[] = {
  61. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  62. .bytesperline = 320,
  63. .sizeimage = 320 * 240 * 3 / 8 + 590,
  64. .colorspace = V4L2_COLORSPACE_JPEG,
  65. .priv = 1},
  66. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  67. .bytesperline = 640,
  68. .sizeimage = 640 * 480 * 3 / 8 + 590,
  69. .colorspace = V4L2_COLORSPACE_JPEG,
  70. .priv = 0},
  71. };
  72. static const u8 bridge_init_ov722x[][2] = {
  73. { 0xc2, 0x0c },
  74. { 0x88, 0xf8 },
  75. { 0xc3, 0x69 },
  76. { 0x89, 0xff },
  77. { 0x76, 0x03 },
  78. { 0x92, 0x01 },
  79. { 0x93, 0x18 },
  80. { 0x94, 0x10 },
  81. { 0x95, 0x10 },
  82. { 0xe2, 0x00 },
  83. { 0xe7, 0x3e },
  84. { 0x96, 0x00 },
  85. { 0x97, 0x20 },
  86. { 0x97, 0x20 },
  87. { 0x97, 0x20 },
  88. { 0x97, 0x0a },
  89. { 0x97, 0x3f },
  90. { 0x97, 0x4a },
  91. { 0x97, 0x20 },
  92. { 0x97, 0x15 },
  93. { 0x97, 0x0b },
  94. { 0x8e, 0x40 },
  95. { 0x1f, 0x81 },
  96. { 0x34, 0x05 },
  97. { 0xe3, 0x04 },
  98. { 0x88, 0x00 },
  99. { 0x89, 0x00 },
  100. { 0x76, 0x00 },
  101. { 0xe7, 0x2e },
  102. { 0x31, 0xf9 },
  103. { 0x25, 0x42 },
  104. { 0x21, 0xf0 },
  105. { 0x1c, 0x00 },
  106. { 0x1d, 0x40 },
  107. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  108. { 0x1d, 0x00 }, /* payload size */
  109. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  110. { 0x1d, 0x58 }, /* frame size */
  111. { 0x1d, 0x00 }, /* frame size */
  112. { 0x1c, 0x0a },
  113. { 0x1d, 0x08 }, /* turn on UVC header */
  114. { 0x1d, 0x0e }, /* .. */
  115. { 0x8d, 0x1c },
  116. { 0x8e, 0x80 },
  117. { 0xe5, 0x04 },
  118. { 0xc0, 0x50 },
  119. { 0xc1, 0x3c },
  120. { 0xc2, 0x0c },
  121. };
  122. static const u8 sensor_init_ov722x[][2] = {
  123. { 0x12, 0x80 },
  124. { 0x11, 0x01 },
  125. { 0x3d, 0x03 },
  126. { 0x17, 0x26 },
  127. { 0x18, 0xa0 },
  128. { 0x19, 0x07 },
  129. { 0x1a, 0xf0 },
  130. { 0x32, 0x00 },
  131. { 0x29, 0xa0 },
  132. { 0x2c, 0xf0 },
  133. { 0x65, 0x20 },
  134. { 0x11, 0x01 },
  135. { 0x42, 0x7f },
  136. { 0x63, 0xe0 },
  137. { 0x64, 0xff },
  138. { 0x66, 0x00 },
  139. { 0x13, 0xf0 },
  140. { 0x0d, 0x41 },
  141. { 0x0f, 0xc5 },
  142. { 0x14, 0x11 },
  143. { 0x22, 0x7f },
  144. { 0x23, 0x03 },
  145. { 0x24, 0x40 },
  146. { 0x25, 0x30 },
  147. { 0x26, 0xa1 },
  148. { 0x2a, 0x00 },
  149. { 0x2b, 0x00 },
  150. { 0x6b, 0xaa },
  151. { 0x13, 0xff },
  152. { 0x90, 0x05 },
  153. { 0x91, 0x01 },
  154. { 0x92, 0x03 },
  155. { 0x93, 0x00 },
  156. { 0x94, 0x60 },
  157. { 0x95, 0x3c },
  158. { 0x96, 0x24 },
  159. { 0x97, 0x1e },
  160. { 0x98, 0x62 },
  161. { 0x99, 0x80 },
  162. { 0x9a, 0x1e },
  163. { 0x9b, 0x08 },
  164. { 0x9c, 0x20 },
  165. { 0x9e, 0x81 },
  166. { 0xa6, 0x04 },
  167. { 0x7e, 0x0c },
  168. { 0x7f, 0x16 },
  169. { 0x80, 0x2a },
  170. { 0x81, 0x4e },
  171. { 0x82, 0x61 },
  172. { 0x83, 0x6f },
  173. { 0x84, 0x7b },
  174. { 0x85, 0x86 },
  175. { 0x86, 0x8e },
  176. { 0x87, 0x97 },
  177. { 0x88, 0xa4 },
  178. { 0x89, 0xaf },
  179. { 0x8a, 0xc5 },
  180. { 0x8b, 0xd7 },
  181. { 0x8c, 0xe8 },
  182. { 0x8d, 0x20 },
  183. { 0x0c, 0x90 },
  184. { 0x2b, 0x00 },
  185. { 0x22, 0x7f },
  186. { 0x23, 0x03 },
  187. { 0x11, 0x01 },
  188. { 0x0c, 0xd0 },
  189. { 0x64, 0xff },
  190. { 0x0d, 0x41 },
  191. { 0x14, 0x41 },
  192. { 0x0e, 0xcd },
  193. { 0xac, 0xbf },
  194. { 0x8e, 0x00 },
  195. { 0x0c, 0xd0 }
  196. };
  197. static const u8 bridge_init_ov965x[][2] = {
  198. {0x88, 0xf8},
  199. {0x89, 0xff},
  200. {0x76, 0x03},
  201. {0x92, 0x03},
  202. {0x95, 0x10},
  203. {0xe2, 0x00},
  204. {0xe7, 0x3e},
  205. {0x8d, 0x1c},
  206. {0x8e, 0x00},
  207. {0x8f, 0x00},
  208. {0x1f, 0x00},
  209. {0xc3, 0xf9},
  210. {0x89, 0xff},
  211. {0x88, 0xf8},
  212. {0x76, 0x03},
  213. {0x92, 0x01},
  214. {0x93, 0x18},
  215. {0x1c, 0x0a},
  216. {0x1d, 0x48},
  217. {0xc0, 0x50},
  218. {0xc1, 0x3c},
  219. {0x34, 0x05},
  220. {0xc2, 0x0c},
  221. {0xc3, 0xf9},
  222. {0x34, 0x05},
  223. {0xe7, 0x2e},
  224. {0x31, 0xf9},
  225. {0x35, 0x02},
  226. {0xd9, 0x10},
  227. {0x25, 0x42},
  228. {0x94, 0x11},
  229. };
  230. static const u8 sensor_init_ov965x[][2] = {
  231. {0x12, 0x80}, /* com7 - SSCB reset */
  232. {0x00, 0x00}, /* gain */
  233. {0x01, 0x80}, /* blue */
  234. {0x02, 0x80}, /* red */
  235. {0x03, 0x1b}, /* vref */
  236. {0x04, 0x03}, /* com1 - exposure low bits */
  237. {0x0b, 0x57}, /* ver */
  238. {0x0e, 0x61}, /* com5 */
  239. {0x0f, 0x42}, /* com6 */
  240. {0x11, 0x00}, /* clkrc */
  241. {0x12, 0x02}, /* com7 - 15fps VGA YUYV */
  242. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  243. {0x14, 0x28}, /* com9 */
  244. {0x16, 0x24}, /* reg16 */
  245. {0x17, 0x1d}, /* hstart*/
  246. {0x18, 0xbd}, /* hstop */
  247. {0x19, 0x01}, /* vstrt */
  248. {0x1a, 0x81}, /* vstop*/
  249. {0x1e, 0x04}, /* mvfp */
  250. {0x24, 0x3c}, /* aew */
  251. {0x25, 0x36}, /* aeb */
  252. {0x26, 0x71}, /* vpt */
  253. {0x27, 0x08}, /* bbias */
  254. {0x28, 0x08}, /* gbbias */
  255. {0x29, 0x15}, /* gr com */
  256. {0x2a, 0x00}, /* exhch */
  257. {0x2b, 0x00}, /* exhcl */
  258. {0x2c, 0x08}, /* rbias */
  259. {0x32, 0xff}, /* href */
  260. {0x33, 0x00}, /* chlf */
  261. {0x34, 0x3f}, /* aref1 */
  262. {0x35, 0x00}, /* aref2 */
  263. {0x36, 0xf8}, /* aref3 */
  264. {0x38, 0x72}, /* adc2 */
  265. {0x39, 0x57}, /* aref4 */
  266. {0x3a, 0x80}, /* tslb - yuyv */
  267. {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
  268. {0x3d, 0x99}, /* com13 */
  269. {0x3f, 0xc1}, /* edge */
  270. {0x40, 0xc0}, /* com15 */
  271. {0x41, 0x40}, /* com16 */
  272. {0x42, 0xc0}, /* com17 */
  273. {0x43, 0x0a}, /* rsvd */
  274. {0x44, 0xf0},
  275. {0x45, 0x46},
  276. {0x46, 0x62},
  277. {0x47, 0x2a},
  278. {0x48, 0x3c},
  279. {0x4a, 0xfc},
  280. {0x4b, 0xfc},
  281. {0x4c, 0x7f},
  282. {0x4d, 0x7f},
  283. {0x4e, 0x7f},
  284. {0x4f, 0x98}, /* matrix */
  285. {0x50, 0x98},
  286. {0x51, 0x00},
  287. {0x52, 0x28},
  288. {0x53, 0x70},
  289. {0x54, 0x98},
  290. {0x58, 0x1a}, /* matrix coef sign */
  291. {0x59, 0x85}, /* AWB control */
  292. {0x5a, 0xa9},
  293. {0x5b, 0x64},
  294. {0x5c, 0x84},
  295. {0x5d, 0x53},
  296. {0x5e, 0x0e},
  297. {0x5f, 0xf0}, /* AWB blue limit */
  298. {0x60, 0xf0}, /* AWB red limit */
  299. {0x61, 0xf0}, /* AWB green limit */
  300. {0x62, 0x00}, /* lcc1 */
  301. {0x63, 0x00}, /* lcc2 */
  302. {0x64, 0x02}, /* lcc3 */
  303. {0x65, 0x16}, /* lcc4 */
  304. {0x66, 0x01}, /* lcc5 */
  305. {0x69, 0x02}, /* hv */
  306. {0x6b, 0x5a}, /* dbvl */
  307. {0x6c, 0x04},
  308. {0x6d, 0x55},
  309. {0x6e, 0x00},
  310. {0x6f, 0x9d},
  311. {0x70, 0x21}, /* dnsth */
  312. {0x71, 0x78},
  313. {0x72, 0x00}, /* poidx */
  314. {0x73, 0x01}, /* pckdv */
  315. {0x74, 0x3a}, /* xindx */
  316. {0x75, 0x35}, /* yindx */
  317. {0x76, 0x01},
  318. {0x77, 0x02},
  319. {0x7a, 0x12}, /* gamma curve */
  320. {0x7b, 0x08},
  321. {0x7c, 0x16},
  322. {0x7d, 0x30},
  323. {0x7e, 0x5e},
  324. {0x7f, 0x72},
  325. {0x80, 0x82},
  326. {0x81, 0x8e},
  327. {0x82, 0x9a},
  328. {0x83, 0xa4},
  329. {0x84, 0xac},
  330. {0x85, 0xb8},
  331. {0x86, 0xc3},
  332. {0x87, 0xd6},
  333. {0x88, 0xe6},
  334. {0x89, 0xf2},
  335. {0x8a, 0x03},
  336. {0x8c, 0x89}, /* com19 */
  337. {0x14, 0x28}, /* com9 */
  338. {0x90, 0x7d},
  339. {0x91, 0x7b},
  340. {0x9d, 0x03}, /* lcc6 */
  341. {0x9e, 0x04}, /* lcc7 */
  342. {0x9f, 0x7a},
  343. {0xa0, 0x79},
  344. {0xa1, 0x40}, /* aechm */
  345. {0xa4, 0x50}, /* com21 */
  346. {0xa5, 0x68}, /* com26 */
  347. {0xa6, 0x4a}, /* AWB green */
  348. {0xa8, 0xc1}, /* refa8 */
  349. {0xa9, 0xef}, /* refa9 */
  350. {0xaa, 0x92},
  351. {0xab, 0x04},
  352. {0xac, 0x80}, /* black level control */
  353. {0xad, 0x80},
  354. {0xae, 0x80},
  355. {0xaf, 0x80},
  356. {0xb2, 0xf2},
  357. {0xb3, 0x20},
  358. {0xb4, 0x20}, /* ctrlb4 */
  359. {0xb5, 0x00},
  360. {0xb6, 0xaf},
  361. {0xbb, 0xae},
  362. {0xbc, 0x7f}, /* ADC channel offsets */
  363. {0xdb, 0x7f},
  364. {0xbe, 0x7f},
  365. {0xbf, 0x7f},
  366. {0xc0, 0xe2},
  367. {0xc1, 0xc0},
  368. {0xc2, 0x01},
  369. {0xc3, 0x4e},
  370. {0xc6, 0x85},
  371. {0xc7, 0x80}, /* com24 */
  372. {0xc9, 0xe0},
  373. {0xca, 0xe8},
  374. {0xcb, 0xf0},
  375. {0xcc, 0xd8},
  376. {0xcd, 0xf1},
  377. {0x4f, 0x98},
  378. {0x50, 0x98},
  379. {0x51, 0x00},
  380. {0x52, 0x28},
  381. {0x53, 0x70},
  382. {0x54, 0x98},
  383. {0x58, 0x1a},
  384. {0xff, 0x41}, /* read 41, write ff 00 */
  385. {0x41, 0x40}, /* com16 */
  386. {0xc5, 0x03}, /* 60 Hz banding filter */
  387. {0x6a, 0x02}, /* 50 Hz banding filter */
  388. {0x12, 0x62}, /* com7 - 30fps VGA YUV */
  389. {0x36, 0xfa}, /* aref3 */
  390. {0x69, 0x0a}, /* hv */
  391. {0x8c, 0x89}, /* com22 */
  392. {0x14, 0x28}, /* com9 */
  393. {0x3e, 0x0c},
  394. {0x41, 0x40}, /* com16 */
  395. {0x72, 0x00},
  396. {0x73, 0x00},
  397. {0x74, 0x3a},
  398. {0x75, 0x35},
  399. {0x76, 0x01},
  400. {0xc7, 0x80},
  401. {0x03, 0x12}, /* vref */
  402. {0x17, 0x16}, /* hstart */
  403. {0x18, 0x02}, /* hstop */
  404. {0x19, 0x01}, /* vstrt */
  405. {0x1a, 0x3d}, /* vstop */
  406. {0x32, 0xff}, /* href */
  407. {0xc0, 0xaa},
  408. };
  409. static const u8 bridge_init_ov965x_2[][2] = {
  410. {0x94, 0xaa},
  411. {0xf1, 0x60},
  412. {0xe5, 0x04},
  413. {0xc0, 0x50},
  414. {0xc1, 0x3c},
  415. {0x8c, 0x00},
  416. {0x8d, 0x1c},
  417. {0x34, 0x05},
  418. {0xc2, 0x0c},
  419. {0xc3, 0xf9},
  420. {0xda, 0x01},
  421. {0x50, 0x00},
  422. {0x51, 0xa0},
  423. {0x52, 0x3c},
  424. {0x53, 0x00},
  425. {0x54, 0x00},
  426. {0x55, 0x00}, /* brightness */
  427. {0x57, 0x00}, /* contrast 2 */
  428. {0x5c, 0x00},
  429. {0x5a, 0xa0},
  430. {0x5b, 0x78},
  431. {0x35, 0x02},
  432. {0xd9, 0x10},
  433. {0x94, 0x11},
  434. };
  435. static const u8 sensor_init_ov965x_2[][2] = {
  436. {0x3b, 0xc4},
  437. {0x1e, 0x04}, /* mvfp */
  438. {0x13, 0xe0}, /* com8 */
  439. {0x00, 0x00}, /* gain */
  440. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  441. {0x11, 0x03}, /* clkrc */
  442. {0x6b, 0x5a}, /* dblv */
  443. {0x6a, 0x05},
  444. {0xc5, 0x07},
  445. {0xa2, 0x4b},
  446. {0xa3, 0x3e},
  447. {0x2d, 0x00},
  448. {0xff, 0x42}, /* read 42, write ff 00 */
  449. {0x42, 0xc0},
  450. {0x2d, 0x00},
  451. {0xff, 0x42}, /* read 42, write ff 00 */
  452. {0x42, 0xc1},
  453. {0x3f, 0x01},
  454. {0xff, 0x42}, /* read 42, write ff 00 */
  455. {0x42, 0xc1},
  456. {0x4f, 0x98},
  457. {0x50, 0x98},
  458. {0x51, 0x00},
  459. {0x52, 0x28},
  460. {0x53, 0x70},
  461. {0x54, 0x98},
  462. {0x58, 0x1a},
  463. {0xff, 0x41}, /* read 41, write ff 00 */
  464. {0x41, 0x40}, /* com16 */
  465. {0x56, 0x40},
  466. {0x55, 0x8f},
  467. {0x10, 0x25}, /* aech - exposure high bits */
  468. {0xff, 0x13}, /* read 13, write ff 00 */
  469. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  470. };
  471. static const u8 sensor_start_ov965x[][2] = {
  472. {0x12, 0x62}, /* com7 - 30fps VGA YUV */
  473. {0x36, 0xfa}, /* aref3 */
  474. {0x69, 0x0a}, /* hv */
  475. {0x8c, 0x89}, /* com22 */
  476. {0x14, 0x28}, /* com9 */
  477. {0x3e, 0x0c}, /* com14 */
  478. {0x41, 0x40}, /* com16 */
  479. {0x72, 0x00},
  480. {0x73, 0x00},
  481. {0x74, 0x3a},
  482. {0x75, 0x35},
  483. {0x76, 0x01},
  484. {0xc7, 0x80}, /* com24 */
  485. {0x03, 0x12}, /* vref */
  486. {0x17, 0x16}, /* hstart */
  487. {0x18, 0x02}, /* hstop */
  488. {0x19, 0x01}, /* vstrt */
  489. {0x1a, 0x3d}, /* vstop */
  490. {0x32, 0xff}, /* href */
  491. {0xc0, 0xaa},
  492. {}
  493. };
  494. static const u8 bridge_start_ov965x[][2] = {
  495. {0x94, 0xaa},
  496. {0xf1, 0x60},
  497. {0xe5, 0x04},
  498. {0xc0, 0x50},
  499. {0xc1, 0x3c},
  500. {0x8c, 0x00},
  501. {0x8d, 0x1c},
  502. {0x34, 0x05},
  503. {}
  504. };
  505. static const u8 bridge_start_ov965x_vga[][2] = {
  506. {0xc2, 0x0c},
  507. {0xc3, 0xf9},
  508. {0xda, 0x01},
  509. {0x50, 0x00},
  510. {0x51, 0xa0},
  511. {0x52, 0x3c},
  512. {0x53, 0x00},
  513. {0x54, 0x00},
  514. {0x55, 0x00},
  515. {0x57, 0x00},
  516. {0x5c, 0x00},
  517. {0x5a, 0xa0},
  518. {0x5b, 0x78},
  519. {0x35, 0x02},
  520. {0xd9, 0x10},
  521. {0x94, 0x11},
  522. {}
  523. };
  524. static const u8 bridge_start_ov965x_cif[][2] = {
  525. {0xc2, 0x4c},
  526. {0xc3, 0xf9},
  527. {0xda, 0x00},
  528. {0x50, 0x00},
  529. {0x51, 0xa0},
  530. {0x52, 0x78},
  531. {0x53, 0x00},
  532. {0x54, 0x00},
  533. {0x55, 0x00},
  534. {0x57, 0x00},
  535. {0x5c, 0x00},
  536. {0x5a, 0x50},
  537. {0x5b, 0x3c},
  538. {0x35, 0x02},
  539. {0xd9, 0x10},
  540. {0x94, 0x11},
  541. {}
  542. };
  543. static const u8 sensor_start_ov965x_vga[][2] = {
  544. {0x3b, 0xc4}, /* com11 - night mode 1/4 frame rate */
  545. {0x1e, 0x04}, /* mvfp */
  546. {0x13, 0xe0}, /* com8 */
  547. {0x00, 0x00},
  548. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  549. {0x11, 0x03}, /* clkrc */
  550. {0x6b, 0x5a}, /* dblv */
  551. {0x6a, 0x05}, /* 50 Hz banding filter */
  552. {0xc5, 0x07}, /* 60 Hz banding filter */
  553. {0xa2, 0x4b}, /* bd50 */
  554. {0xa3, 0x3e}, /* bd60 */
  555. {0x2d, 0x00}, /* advfl */
  556. {}
  557. };
  558. static const u8 sensor_start_ov965x_cif[][2] = {
  559. {0x3b, 0xe4}, /* com11 - night mode 1/4 frame rate */
  560. {0x1e, 0x04}, /* mvfp */
  561. {0x13, 0xe0}, /* com8 */
  562. {0x00, 0x00},
  563. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  564. {0x11, 0x01}, /* clkrc */
  565. {0x6b, 0x5a}, /* dblv */
  566. {0x6a, 0x02}, /* 50 Hz banding filter */
  567. {0xc5, 0x03}, /* 60 Hz banding filter */
  568. {0xa2, 0x96}, /* bd50 */
  569. {0xa3, 0x7d}, /* bd60 */
  570. {0xff, 0x13}, /* read 13, write ff 00 */
  571. {0x13, 0xe7},
  572. {0x3a, 0x80}, /* tslb - yuyv */
  573. {}
  574. };
  575. static const u8 sensor_start_ov965x_2[][2] = {
  576. {0xff, 0x42}, /* read 42, write ff 00 */
  577. {0x42, 0xc1}, /* com17 - 50 Hz filter */
  578. {}
  579. };
  580. static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
  581. {
  582. struct usb_device *udev = gspca_dev->dev;
  583. int ret;
  584. PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
  585. gspca_dev->usb_buf[0] = val;
  586. ret = usb_control_msg(udev,
  587. usb_sndctrlpipe(udev, 0),
  588. 0x01,
  589. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  590. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  591. if (ret < 0)
  592. PDEBUG(D_ERR, "write failed");
  593. }
  594. static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  595. {
  596. struct usb_device *udev = gspca_dev->dev;
  597. int ret;
  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, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
  604. if (ret < 0)
  605. PDEBUG(D_ERR, "read failed");
  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. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  635. switch (data) {
  636. case 0x00:
  637. return 1;
  638. case 0x04:
  639. return 0;
  640. case 0x03:
  641. break;
  642. default:
  643. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
  644. data, i + 1);
  645. }
  646. }
  647. return 0;
  648. }
  649. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  650. {
  651. PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
  652. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  653. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  654. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  655. if (!sccb_check_status(gspca_dev))
  656. PDEBUG(D_ERR, "sccb_reg_write failed");
  657. }
  658. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  659. {
  660. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  661. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  662. if (!sccb_check_status(gspca_dev))
  663. PDEBUG(D_ERR, "sccb_reg_read failed 1");
  664. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  665. if (!sccb_check_status(gspca_dev))
  666. PDEBUG(D_ERR, "sccb_reg_read failed 2");
  667. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  668. }
  669. /* output a bridge sequence (reg - val) */
  670. static void reg_w_array(struct gspca_dev *gspca_dev,
  671. const u8 (*data)[2], int len)
  672. {
  673. while (--len >= 0) {
  674. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  675. data++;
  676. }
  677. }
  678. /* output a sensor sequence (reg - val) */
  679. static void sccb_w_array(struct gspca_dev *gspca_dev,
  680. const u8 (*data)[2], int len)
  681. {
  682. while (--len >= 0) {
  683. if ((*data)[0] != 0xff) {
  684. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  685. } else {
  686. sccb_reg_read(gspca_dev, (*data)[1]);
  687. sccb_reg_write(gspca_dev, 0xff, 0x00);
  688. }
  689. data++;
  690. }
  691. }
  692. /* set framerate */
  693. static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
  694. {
  695. struct sd *sd = (struct sd *) gspca_dev;
  696. int fr = sd->frame_rate;
  697. switch (fr) {
  698. case 50:
  699. sccb_reg_write(gspca_dev, 0x11, 0x01);
  700. sccb_reg_write(gspca_dev, 0x0d, 0x41);
  701. ov534_reg_write(gspca_dev, 0xe5, 0x02);
  702. break;
  703. case 40:
  704. sccb_reg_write(gspca_dev, 0x11, 0x02);
  705. sccb_reg_write(gspca_dev, 0x0d, 0xc1);
  706. ov534_reg_write(gspca_dev, 0xe5, 0x04);
  707. break;
  708. /* case 30: */
  709. default:
  710. fr = 30;
  711. sccb_reg_write(gspca_dev, 0x11, 0x04);
  712. sccb_reg_write(gspca_dev, 0x0d, 0x81);
  713. ov534_reg_write(gspca_dev, 0xe5, 0x02);
  714. break;
  715. case 15:
  716. sccb_reg_write(gspca_dev, 0x11, 0x03);
  717. sccb_reg_write(gspca_dev, 0x0d, 0x41);
  718. ov534_reg_write(gspca_dev, 0xe5, 0x04);
  719. break;
  720. }
  721. sd->frame_rate = fr;
  722. PDEBUG(D_PROBE, "frame_rate: %d", fr);
  723. }
  724. /* this function is called at probe time */
  725. static int sd_config(struct gspca_dev *gspca_dev,
  726. const struct usb_device_id *id)
  727. {
  728. struct sd *sd = (struct sd *) gspca_dev;
  729. struct cam *cam;
  730. sd->sensor = id->driver_info;
  731. cam = &gspca_dev->cam;
  732. if (sd->sensor == SENSOR_OV772X) {
  733. cam->cam_mode = vga_yuyv_mode;
  734. cam->nmodes = ARRAY_SIZE(vga_yuyv_mode);
  735. cam->bulk = 1;
  736. cam->bulk_size = 16384;
  737. cam->bulk_nurbs = 2;
  738. } else { /* ov965x */
  739. cam->cam_mode = vga_jpeg_mode;
  740. cam->nmodes = ARRAY_SIZE(vga_jpeg_mode);
  741. }
  742. return 0;
  743. }
  744. /* this function is called at probe and resume time */
  745. static int sd_init(struct gspca_dev *gspca_dev)
  746. {
  747. struct sd *sd = (struct sd *) gspca_dev;
  748. u16 sensor_id;
  749. static const u8 sensor_addr[2] = {
  750. 0x42, /* 0 SENSOR_OV772X */
  751. 0x60, /* 1 SENSOR_OV965X */
  752. };
  753. /* reset bridge */
  754. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  755. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  756. msleep(100);
  757. /* initialize the sensor address */
  758. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
  759. sensor_addr[sd->sensor]);
  760. /* reset sensor */
  761. sccb_reg_write(gspca_dev, 0x12, 0x80);
  762. msleep(10);
  763. /* probe the sensor */
  764. sccb_reg_read(gspca_dev, 0x0a);
  765. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  766. sccb_reg_read(gspca_dev, 0x0b);
  767. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  768. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  769. /* initialize */
  770. switch (sd->sensor) {
  771. case SENSOR_OV772X:
  772. reg_w_array(gspca_dev, bridge_init_ov722x,
  773. ARRAY_SIZE(bridge_init_ov722x));
  774. ov534_set_led(gspca_dev, 1);
  775. sccb_w_array(gspca_dev, sensor_init_ov722x,
  776. ARRAY_SIZE(sensor_init_ov722x));
  777. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  778. ov534_set_led(gspca_dev, 0);
  779. ov534_set_frame_rate(gspca_dev);
  780. break;
  781. default:
  782. /* case SENSOR_OV965X: */
  783. reg_w_array(gspca_dev, bridge_init_ov965x,
  784. ARRAY_SIZE(bridge_init_ov965x));
  785. sccb_w_array(gspca_dev, sensor_init_ov965x,
  786. ARRAY_SIZE(sensor_init_ov965x));
  787. reg_w_array(gspca_dev, bridge_init_ov965x_2,
  788. ARRAY_SIZE(bridge_init_ov965x_2));
  789. sccb_w_array(gspca_dev, sensor_init_ov965x_2,
  790. ARRAY_SIZE(sensor_init_ov965x_2));
  791. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  792. ov534_reg_write(gspca_dev, 0xe0, 0x01);
  793. ov534_set_led(gspca_dev, 0);
  794. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  795. }
  796. return 0;
  797. }
  798. static int sd_start(struct gspca_dev *gspca_dev)
  799. {
  800. struct sd *sd = (struct sd *) gspca_dev;
  801. int mode;
  802. switch (sd->sensor) {
  803. case SENSOR_OV772X:
  804. ov534_set_led(gspca_dev, 1);
  805. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  806. break;
  807. default:
  808. /* case SENSOR_OV965X: */
  809. sccb_w_array(gspca_dev, sensor_start_ov965x,
  810. ARRAY_SIZE(sensor_start_ov965x));
  811. reg_w_array(gspca_dev, bridge_start_ov965x,
  812. ARRAY_SIZE(bridge_start_ov965x));
  813. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  814. if (mode != 0) { /* 320x240 */
  815. reg_w_array(gspca_dev, bridge_start_ov965x_cif,
  816. ARRAY_SIZE(bridge_start_ov965x_cif));
  817. sccb_w_array(gspca_dev, sensor_start_ov965x_cif,
  818. ARRAY_SIZE(sensor_start_ov965x_cif));
  819. } else { /* 640x480 */
  820. reg_w_array(gspca_dev, bridge_start_ov965x_vga,
  821. ARRAY_SIZE(bridge_start_ov965x_vga));
  822. sccb_w_array(gspca_dev, sensor_start_ov965x_vga,
  823. ARRAY_SIZE(sensor_start_ov965x_vga));
  824. }
  825. sccb_w_array(gspca_dev, sensor_start_ov965x_2,
  826. ARRAY_SIZE(sensor_start_ov965x_2));
  827. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  828. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  829. ov534_set_led(gspca_dev, 1);
  830. }
  831. return 0;
  832. }
  833. static void sd_stopN(struct gspca_dev *gspca_dev)
  834. {
  835. struct sd *sd = (struct sd *) gspca_dev;
  836. switch (sd->sensor) {
  837. case SENSOR_OV772X:
  838. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  839. ov534_set_led(gspca_dev, 0);
  840. break;
  841. default:
  842. /* case SENSOR_OV965X: */
  843. ov534_reg_write(gspca_dev, 0xe0, 0x01);
  844. ov534_set_led(gspca_dev, 0);
  845. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  846. break;
  847. }
  848. }
  849. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  850. #define UVC_STREAM_EOH (1 << 7)
  851. #define UVC_STREAM_ERR (1 << 6)
  852. #define UVC_STREAM_STI (1 << 5)
  853. #define UVC_STREAM_RES (1 << 4)
  854. #define UVC_STREAM_SCR (1 << 3)
  855. #define UVC_STREAM_PTS (1 << 2)
  856. #define UVC_STREAM_EOF (1 << 1)
  857. #define UVC_STREAM_FID (1 << 0)
  858. static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
  859. __u8 *data, int len)
  860. {
  861. struct sd *sd = (struct sd *) gspca_dev;
  862. __u32 this_pts;
  863. u16 this_fid;
  864. int remaining_len = len;
  865. int payload_len;
  866. payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
  867. do {
  868. len = min(remaining_len, payload_len);
  869. /* Payloads are prefixed with a UVC-style header. We
  870. consider a frame to start when the FID toggles, or the PTS
  871. changes. A frame ends when EOF is set, and we've received
  872. the correct number of bytes. */
  873. /* Verify UVC header. Header length is always 12 */
  874. if (data[0] != 12 || len < 12) {
  875. PDEBUG(D_PACK, "bad header");
  876. goto discard;
  877. }
  878. /* Check errors */
  879. if (data[1] & UVC_STREAM_ERR) {
  880. PDEBUG(D_PACK, "payload error");
  881. goto discard;
  882. }
  883. /* Extract PTS and FID */
  884. if (!(data[1] & UVC_STREAM_PTS)) {
  885. PDEBUG(D_PACK, "PTS not present");
  886. goto discard;
  887. }
  888. this_pts = (data[5] << 24) | (data[4] << 16)
  889. | (data[3] << 8) | data[2];
  890. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  891. /* If PTS or FID has changed, start a new frame. */
  892. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  893. if (gspca_dev->last_packet_type == INTER_PACKET)
  894. frame = gspca_frame_add(gspca_dev,
  895. LAST_PACKET, frame,
  896. NULL, 0);
  897. sd->last_pts = this_pts;
  898. sd->last_fid = this_fid;
  899. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  900. data + 12, len - 12);
  901. /* If this packet is marked as EOF, end the frame */
  902. } else if (data[1] & UVC_STREAM_EOF) {
  903. sd->last_pts = 0;
  904. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  905. data + 12, len - 12);
  906. } else {
  907. /* Add the data from this payload */
  908. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  909. data + 12, len - 12);
  910. }
  911. /* Done this payload */
  912. goto scan_next;
  913. discard:
  914. /* Discard data until a new frame starts. */
  915. gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
  916. scan_next:
  917. remaining_len -= len;
  918. data += len;
  919. } while (remaining_len > 0);
  920. }
  921. /* get stream parameters (framerate) */
  922. static int sd_get_streamparm(struct gspca_dev *gspca_dev,
  923. struct v4l2_streamparm *parm)
  924. {
  925. struct v4l2_captureparm *cp = &parm->parm.capture;
  926. struct v4l2_fract *tpf = &cp->timeperframe;
  927. struct sd *sd = (struct sd *) gspca_dev;
  928. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  929. return -EINVAL;
  930. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  931. tpf->numerator = 1;
  932. tpf->denominator = sd->frame_rate;
  933. return 0;
  934. }
  935. /* set stream parameters (framerate) */
  936. static int sd_set_streamparm(struct gspca_dev *gspca_dev,
  937. struct v4l2_streamparm *parm)
  938. {
  939. struct v4l2_captureparm *cp = &parm->parm.capture;
  940. struct v4l2_fract *tpf = &cp->timeperframe;
  941. struct sd *sd = (struct sd *) gspca_dev;
  942. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  943. return -EINVAL;
  944. /* Set requested framerate */
  945. sd->frame_rate = tpf->denominator / tpf->numerator;
  946. ov534_set_frame_rate(gspca_dev);
  947. /* Return the actual framerate */
  948. tpf->numerator = 1;
  949. tpf->denominator = sd->frame_rate;
  950. return 0;
  951. }
  952. /* sub-driver description */
  953. static const struct sd_desc sd_desc = {
  954. .name = MODULE_NAME,
  955. .ctrls = sd_ctrls,
  956. .nctrls = ARRAY_SIZE(sd_ctrls),
  957. .config = sd_config,
  958. .init = sd_init,
  959. .start = sd_start,
  960. .stopN = sd_stopN,
  961. .pkt_scan = sd_pkt_scan,
  962. .get_streamparm = sd_get_streamparm,
  963. .set_streamparm = sd_set_streamparm,
  964. };
  965. /* -- module initialisation -- */
  966. static const __devinitdata struct usb_device_id device_table[] = {
  967. {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
  968. {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
  969. {}
  970. };
  971. MODULE_DEVICE_TABLE(usb, device_table);
  972. /* -- device connect -- */
  973. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  974. {
  975. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  976. THIS_MODULE);
  977. }
  978. static struct usb_driver sd_driver = {
  979. .name = MODULE_NAME,
  980. .id_table = device_table,
  981. .probe = sd_probe,
  982. .disconnect = gspca_disconnect,
  983. #ifdef CONFIG_PM
  984. .suspend = gspca_suspend,
  985. .resume = gspca_resume,
  986. #endif
  987. };
  988. /* -- module insert / remove -- */
  989. static int __init sd_mod_init(void)
  990. {
  991. int ret;
  992. ret = usb_register(&sd_driver);
  993. if (ret < 0)
  994. return ret;
  995. PDEBUG(D_PROBE, "registered");
  996. return 0;
  997. }
  998. static void __exit sd_mod_exit(void)
  999. {
  1000. usb_deregister(&sd_driver);
  1001. PDEBUG(D_PROBE, "deregistered");
  1002. }
  1003. module_init(sd_mod_init);
  1004. module_exit(sd_mod_exit);