ov534.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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_mode[] = {
  54. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  55. .bytesperline = 640 * 2,
  56. .sizeimage = 640 * 480 * 2,
  57. .colorspace = V4L2_COLORSPACE_JPEG,
  58. .priv = 0},
  59. };
  60. static const u8 bridge_init_ov722x[][2] = {
  61. { 0xc2, 0x0c },
  62. { 0x88, 0xf8 },
  63. { 0xc3, 0x69 },
  64. { 0x89, 0xff },
  65. { 0x76, 0x03 },
  66. { 0x92, 0x01 },
  67. { 0x93, 0x18 },
  68. { 0x94, 0x10 },
  69. { 0x95, 0x10 },
  70. { 0xe2, 0x00 },
  71. { 0xe7, 0x3e },
  72. { 0x96, 0x00 },
  73. { 0x97, 0x20 },
  74. { 0x97, 0x20 },
  75. { 0x97, 0x20 },
  76. { 0x97, 0x0a },
  77. { 0x97, 0x3f },
  78. { 0x97, 0x4a },
  79. { 0x97, 0x20 },
  80. { 0x97, 0x15 },
  81. { 0x97, 0x0b },
  82. { 0x8e, 0x40 },
  83. { 0x1f, 0x81 },
  84. { 0x34, 0x05 },
  85. { 0xe3, 0x04 },
  86. { 0x88, 0x00 },
  87. { 0x89, 0x00 },
  88. { 0x76, 0x00 },
  89. { 0xe7, 0x2e },
  90. { 0x31, 0xf9 },
  91. { 0x25, 0x42 },
  92. { 0x21, 0xf0 },
  93. { 0x1c, 0x00 },
  94. { 0x1d, 0x40 },
  95. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  96. { 0x1d, 0x00 }, /* payload size */
  97. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  98. { 0x1d, 0x58 }, /* frame size */
  99. { 0x1d, 0x00 }, /* frame size */
  100. { 0x1c, 0x0a },
  101. { 0x1d, 0x08 }, /* turn on UVC header */
  102. { 0x1d, 0x0e }, /* .. */
  103. { 0x8d, 0x1c },
  104. { 0x8e, 0x80 },
  105. { 0xe5, 0x04 },
  106. { 0xc0, 0x50 },
  107. { 0xc1, 0x3c },
  108. { 0xc2, 0x0c },
  109. };
  110. static const u8 sensor_init_ov722x[][2] = {
  111. { 0x12, 0x80 },
  112. { 0x11, 0x01 },
  113. { 0x3d, 0x03 },
  114. { 0x17, 0x26 },
  115. { 0x18, 0xa0 },
  116. { 0x19, 0x07 },
  117. { 0x1a, 0xf0 },
  118. { 0x32, 0x00 },
  119. { 0x29, 0xa0 },
  120. { 0x2c, 0xf0 },
  121. { 0x65, 0x20 },
  122. { 0x11, 0x01 },
  123. { 0x42, 0x7f },
  124. { 0x63, 0xe0 },
  125. { 0x64, 0xff },
  126. { 0x66, 0x00 },
  127. { 0x13, 0xf0 },
  128. { 0x0d, 0x41 },
  129. { 0x0f, 0xc5 },
  130. { 0x14, 0x11 },
  131. { 0x22, 0x7f },
  132. { 0x23, 0x03 },
  133. { 0x24, 0x40 },
  134. { 0x25, 0x30 },
  135. { 0x26, 0xa1 },
  136. { 0x2a, 0x00 },
  137. { 0x2b, 0x00 },
  138. { 0x6b, 0xaa },
  139. { 0x13, 0xff },
  140. { 0x90, 0x05 },
  141. { 0x91, 0x01 },
  142. { 0x92, 0x03 },
  143. { 0x93, 0x00 },
  144. { 0x94, 0x60 },
  145. { 0x95, 0x3c },
  146. { 0x96, 0x24 },
  147. { 0x97, 0x1e },
  148. { 0x98, 0x62 },
  149. { 0x99, 0x80 },
  150. { 0x9a, 0x1e },
  151. { 0x9b, 0x08 },
  152. { 0x9c, 0x20 },
  153. { 0x9e, 0x81 },
  154. { 0xa6, 0x04 },
  155. { 0x7e, 0x0c },
  156. { 0x7f, 0x16 },
  157. { 0x80, 0x2a },
  158. { 0x81, 0x4e },
  159. { 0x82, 0x61 },
  160. { 0x83, 0x6f },
  161. { 0x84, 0x7b },
  162. { 0x85, 0x86 },
  163. { 0x86, 0x8e },
  164. { 0x87, 0x97 },
  165. { 0x88, 0xa4 },
  166. { 0x89, 0xaf },
  167. { 0x8a, 0xc5 },
  168. { 0x8b, 0xd7 },
  169. { 0x8c, 0xe8 },
  170. { 0x8d, 0x20 },
  171. { 0x0c, 0x90 },
  172. { 0x2b, 0x00 },
  173. { 0x22, 0x7f },
  174. { 0x23, 0x03 },
  175. { 0x11, 0x01 },
  176. { 0x0c, 0xd0 },
  177. { 0x64, 0xff },
  178. { 0x0d, 0x41 },
  179. { 0x14, 0x41 },
  180. { 0x0e, 0xcd },
  181. { 0xac, 0xbf },
  182. { 0x8e, 0x00 },
  183. { 0x0c, 0xd0 }
  184. };
  185. static const u8 bridge_init_ov965x[][2] = {
  186. {0x88, 0xf8},
  187. {0x89, 0xff},
  188. {0x76, 0x03},
  189. {0x92, 0x03},
  190. {0x95, 0x10},
  191. {0xe2, 0x00},
  192. {0xe7, 0x3e},
  193. {0x8d, 0x1c},
  194. {0x8e, 0x00},
  195. {0x8f, 0x00},
  196. {0x1f, 0x00},
  197. {0xc3, 0xf9},
  198. {0x89, 0xff},
  199. {0x88, 0xf8},
  200. {0x76, 0x03},
  201. {0x92, 0x01},
  202. {0x93, 0x18},
  203. {0x1c, 0x0a},
  204. {0x1d, 0x48},
  205. {0xc0, 0x50},
  206. {0xc1, 0x3c},
  207. {0x34, 0x05},
  208. {0xc2, 0x0c},
  209. {0xc3, 0xf9},
  210. {0x34, 0x05},
  211. {0xe7, 0x2e},
  212. {0x31, 0xf9},
  213. {0x35, 0x02},
  214. {0xd9, 0x10},
  215. {0x25, 0x42},
  216. {0x94, 0x11},
  217. };
  218. static const u8 sensor_init_ov965x[][2] = {
  219. {0x12, 0x80}, /* com7 - reset */
  220. {0x00, 0x00}, /* gain */
  221. {0x01, 0x80}, /* blue */
  222. {0x02, 0x80}, /* red */
  223. {0x03, 0x1b}, /* vref */
  224. {0x04, 0x03}, /* com1 - exposure low bits */
  225. {0x0b, 0x57}, /* ver */
  226. {0x0e, 0x61}, /* com5 */
  227. {0x0f, 0x42}, /* com6 */
  228. {0x11, 0x00}, /* clkrc */
  229. {0x12, 0x02}, /* com7 */
  230. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  231. {0x14, 0x28}, /* com9 */
  232. {0x16, 0x24}, /* rsvd16 */
  233. {0x17, 0x1d}, /* hstart*/
  234. {0x18, 0xbd}, /* hstop */
  235. {0x19, 0x01}, /* vstrt */
  236. {0x1a, 0x81}, /* vstop*/
  237. {0x1e, 0x04}, /* mvfp */
  238. {0x24, 0x3c}, /* aew */
  239. {0x25, 0x36}, /* aeb */
  240. {0x26, 0x71}, /* vpt */
  241. {0x27, 0x08}, /* bbias */
  242. {0x28, 0x08}, /* gbbias */
  243. {0x29, 0x15}, /* gr com */
  244. {0x2a, 0x00},
  245. {0x2b, 0x00},
  246. {0x2c, 0x08}, /* rbias */
  247. {0x32, 0xff}, /* href */
  248. {0x33, 0x00}, /* chlf */
  249. {0x34, 0x3f}, /* arblm */
  250. {0x35, 0x00}, /* rsvd35 */
  251. {0x36, 0xf8}, /* rsvd36 */
  252. {0x38, 0x72}, /* acom38 */
  253. {0x39, 0x57}, /* ofon */
  254. {0x3a, 0x80}, /* tslb */
  255. {0x3b, 0xc4},
  256. {0x3d, 0x99}, /* com13 */
  257. {0x3f, 0xc1},
  258. {0x40, 0xc0}, /* com15 */
  259. {0x41, 0x40}, /* com16 */
  260. {0x42, 0xc0},
  261. {0x43, 0x0a},
  262. {0x44, 0xf0},
  263. {0x45, 0x46},
  264. {0x46, 0x62},
  265. {0x47, 0x2a},
  266. {0x48, 0x3c},
  267. {0x4a, 0xfc},
  268. {0x4b, 0xfc},
  269. {0x4c, 0x7f},
  270. {0x4d, 0x7f},
  271. {0x4e, 0x7f},
  272. {0x4f, 0x98},
  273. {0x50, 0x98},
  274. {0x51, 0x00},
  275. {0x52, 0x28},
  276. {0x53, 0x70},
  277. {0x54, 0x98},
  278. {0x58, 0x1a},
  279. {0x59, 0x85},
  280. {0x5a, 0xa9},
  281. {0x5b, 0x64},
  282. {0x5c, 0x84},
  283. {0x5d, 0x53},
  284. {0x5e, 0x0e},
  285. {0x5f, 0xf0},
  286. {0x60, 0xf0},
  287. {0x61, 0xf0},
  288. {0x62, 0x00}, /* lcc1 */
  289. {0x63, 0x00}, /* lcc2 */
  290. {0x64, 0x02}, /* lcc3 */
  291. {0x65, 0x16}, /* lcc4 */
  292. {0x66, 0x01}, /* lcc5 */
  293. {0x69, 0x02}, /* hv */
  294. {0x6b, 0x5a}, /* dbvl */
  295. {0x6c, 0x04},
  296. {0x6d, 0x55},
  297. {0x6e, 0x00},
  298. {0x6f, 0x9d},
  299. {0x70, 0x21},
  300. {0x71, 0x78},
  301. {0x72, 0x00},
  302. {0x73, 0x01},
  303. {0x74, 0x3a},
  304. {0x75, 0x35},
  305. {0x76, 0x01},
  306. {0x77, 0x02},
  307. {0x7a, 0x12},
  308. {0x7b, 0x08},
  309. {0x7c, 0x16},
  310. {0x7d, 0x30},
  311. {0x7e, 0x5e},
  312. {0x7f, 0x72},
  313. {0x80, 0x82},
  314. {0x81, 0x8e},
  315. {0x82, 0x9a},
  316. {0x83, 0xa4},
  317. {0x84, 0xac},
  318. {0x85, 0xb8},
  319. {0x86, 0xc3},
  320. {0x87, 0xd6},
  321. {0x88, 0xe6},
  322. {0x89, 0xf2},
  323. {0x8a, 0x03},
  324. {0x8c, 0x89},
  325. {0x14, 0x28}, /* com9 */
  326. {0x90, 0x7d},
  327. {0x91, 0x7b},
  328. {0x9d, 0x03},
  329. {0x9e, 0x04},
  330. {0x9f, 0x7a},
  331. {0xa0, 0x79},
  332. {0xa1, 0x40}, /* aechm */
  333. {0xa4, 0x50},
  334. {0xa5, 0x68}, /* com26 */
  335. {0xa6, 0x4a},
  336. {0xa8, 0xc1}, /* acoma8 */
  337. {0xa9, 0xef}, /* acoma9 */
  338. {0xaa, 0x92},
  339. {0xab, 0x04},
  340. {0xac, 0x80},
  341. {0xad, 0x80},
  342. {0xae, 0x80},
  343. {0xaf, 0x80},
  344. {0xb2, 0xf2},
  345. {0xb3, 0x20},
  346. {0xb4, 0x20},
  347. {0xb5, 0x00},
  348. {0xb6, 0xaf},
  349. {0xbb, 0xae},
  350. {0xbc, 0x7f},
  351. {0xdb, 0x7f},
  352. {0xbe, 0x7f},
  353. {0xbf, 0x7f},
  354. {0xc0, 0xe2},
  355. {0xc1, 0xc0},
  356. {0xc2, 0x01},
  357. {0xc3, 0x4e},
  358. {0xc6, 0x85},
  359. {0xc7, 0x80},
  360. {0xc9, 0xe0},
  361. {0xca, 0xe8},
  362. {0xcb, 0xf0},
  363. {0xcc, 0xd8},
  364. {0xcd, 0xf1},
  365. {0x4f, 0x98},
  366. {0x50, 0x98},
  367. {0x51, 0x00},
  368. {0x52, 0x28},
  369. {0x53, 0x70},
  370. {0x54, 0x98},
  371. {0x58, 0x1a},
  372. {0xff, 0x41}, /* read 41, write ff 00 */
  373. {0x41, 0x40}, /* com16 */
  374. {0xc5, 0x03},
  375. {0x6a, 0x02},
  376. {0x12, 0x62}, /* com7 - VGA + CIF */
  377. {0x36, 0xfa}, /* rsvd36 */
  378. {0x69, 0x0a}, /* hv */
  379. {0x8c, 0x89}, /* com22 */
  380. {0x14, 0x28}, /* com9 */
  381. {0x3e, 0x0c},
  382. {0x41, 0x40}, /* com16 */
  383. {0x72, 0x00},
  384. {0x73, 0x00},
  385. {0x74, 0x3a},
  386. {0x75, 0x35},
  387. {0x76, 0x01},
  388. {0xc7, 0x80},
  389. {0x03, 0x12}, /* vref */
  390. {0x17, 0x16}, /* hstart */
  391. {0x18, 0x02}, /* hstop */
  392. {0x19, 0x01}, /* vstrt */
  393. {0x1a, 0x3d}, /* vstop */
  394. {0x32, 0xff}, /* href */
  395. {0xc0, 0xaa},
  396. };
  397. static const u8 bridge_init_ov965x_2[][2] = {
  398. {0x94, 0xaa},
  399. {0xf1, 0x60},
  400. {0xe5, 0x04},
  401. {0xc0, 0x50},
  402. {0xc1, 0x3c},
  403. {0x8c, 0x00},
  404. {0x8d, 0x1c},
  405. {0x34, 0x05},
  406. {0xc2, 0x0c},
  407. {0xc3, 0xf9},
  408. {0xda, 0x01},
  409. {0x50, 0x00},
  410. {0x51, 0xa0},
  411. {0x52, 0x3c},
  412. {0x53, 0x00},
  413. {0x54, 0x00},
  414. {0x55, 0x00},
  415. {0x57, 0x00},
  416. {0x5c, 0x00},
  417. {0x5a, 0xa0},
  418. {0x5b, 0x78},
  419. {0x35, 0x02},
  420. {0xd9, 0x10},
  421. {0x94, 0x11},
  422. };
  423. static const u8 sensor_init_ov965x_2[][2] = {
  424. {0x3b, 0xc4},
  425. {0x1e, 0x04}, /* mvfp */
  426. {0x13, 0xe0}, /* com8 */
  427. {0x00, 0x00}, /* gain */
  428. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  429. {0x11, 0x03}, /* clkrc */
  430. {0x6b, 0x5a}, /* dblv */
  431. {0x6a, 0x05},
  432. {0xc5, 0x07},
  433. {0xa2, 0x4b},
  434. {0xa3, 0x3e},
  435. {0x2d, 0x00},
  436. {0xff, 0x42}, /* read 42, write ff 00 */
  437. {0x42, 0xc0},
  438. {0x2d, 0x00},
  439. {0xff, 0x42}, /* read 42, write ff 00 */
  440. {0x42, 0xc1},
  441. {0x3f, 0x01},
  442. {0xff, 0x42}, /* read 42, write ff 00 */
  443. {0x42, 0xc1},
  444. {0x4f, 0x98},
  445. {0x50, 0x98},
  446. {0x51, 0x00},
  447. {0x52, 0x28},
  448. {0x53, 0x70},
  449. {0x54, 0x98},
  450. {0x58, 0x1a},
  451. {0xff, 0x41}, /* read 41, write ff 00 */
  452. {0x41, 0x40}, /* com16 */
  453. {0x56, 0x40},
  454. {0x55, 0x8f},
  455. {0x10, 0x25}, /* aech - exposure high bits */
  456. {0xff, 0x13}, /* read 13, write ff 00 */
  457. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  458. };
  459. static const u8 bridge_start_ov965x[][2] = {
  460. {0xc2, 0x4c},
  461. {0xc3, 0xf9},
  462. {0x50, 0x00},
  463. {0x51, 0xa0},
  464. {0x52, 0x78},
  465. {0x53, 0x00},
  466. {0x54, 0x00},
  467. {0x55, 0x00},
  468. {0x57, 0x00},
  469. {0x5c, 0x00},
  470. {0x5a, 0x28},
  471. {0x5b, 0x1e},
  472. {0x35, 0x00},
  473. {0xd9, 0x21},
  474. {0x94, 0x11},
  475. };
  476. static const u8 sensor_start_ov965x[][2] = {
  477. {0x3b, 0xe4},
  478. {0x1e, 0x04}, /* mvfp */
  479. {0x13, 0xe0}, /* com8 */
  480. {0x00, 0x00},
  481. {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
  482. {0x11, 0x01}, /* clkrc */
  483. {0x6b, 0x5a}, /* dblv */
  484. {0x6a, 0x02},
  485. {0xc5, 0x03},
  486. {0xa2, 0x96},
  487. {0xa3, 0x7d},
  488. {0xff, 0x13}, /* read 13, write ff 00 */
  489. {0x13, 0xe7},
  490. {0x3a, 0x80},
  491. {0xff, 0x42}, /* read 42, write ff 00 */
  492. {0x42, 0xc1},
  493. };
  494. static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
  495. {
  496. struct usb_device *udev = gspca_dev->dev;
  497. int ret;
  498. PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
  499. gspca_dev->usb_buf[0] = val;
  500. ret = usb_control_msg(udev,
  501. usb_sndctrlpipe(udev, 0),
  502. 0x01,
  503. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  504. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  505. if (ret < 0)
  506. PDEBUG(D_ERR, "write failed");
  507. }
  508. static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  509. {
  510. struct usb_device *udev = gspca_dev->dev;
  511. int ret;
  512. ret = usb_control_msg(udev,
  513. usb_rcvctrlpipe(udev, 0),
  514. 0x01,
  515. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  516. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  517. PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
  518. if (ret < 0)
  519. PDEBUG(D_ERR, "read failed");
  520. return gspca_dev->usb_buf[0];
  521. }
  522. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  523. * (direction and output)? */
  524. static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
  525. {
  526. u8 data;
  527. PDEBUG(D_CONF, "led status: %d", status);
  528. data = ov534_reg_read(gspca_dev, 0x21);
  529. data |= 0x80;
  530. ov534_reg_write(gspca_dev, 0x21, data);
  531. data = ov534_reg_read(gspca_dev, 0x23);
  532. if (status)
  533. data |= 0x80;
  534. else
  535. data &= ~0x80;
  536. ov534_reg_write(gspca_dev, 0x23, data);
  537. if (!status) {
  538. data = ov534_reg_read(gspca_dev, 0x21);
  539. data &= ~0x80;
  540. ov534_reg_write(gspca_dev, 0x21, data);
  541. }
  542. }
  543. static int sccb_check_status(struct gspca_dev *gspca_dev)
  544. {
  545. u8 data;
  546. int i;
  547. for (i = 0; i < 5; i++) {
  548. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  549. switch (data) {
  550. case 0x00:
  551. return 1;
  552. case 0x04:
  553. return 0;
  554. case 0x03:
  555. break;
  556. default:
  557. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
  558. data, i + 1);
  559. }
  560. }
  561. return 0;
  562. }
  563. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  564. {
  565. PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
  566. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  567. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  568. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  569. if (!sccb_check_status(gspca_dev))
  570. PDEBUG(D_ERR, "sccb_reg_write failed");
  571. }
  572. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  573. {
  574. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  575. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  576. if (!sccb_check_status(gspca_dev))
  577. PDEBUG(D_ERR, "sccb_reg_read failed 1");
  578. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  579. if (!sccb_check_status(gspca_dev))
  580. PDEBUG(D_ERR, "sccb_reg_read failed 2");
  581. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  582. }
  583. /* output a bridge sequence (reg - val) */
  584. static void reg_w_array(struct gspca_dev *gspca_dev,
  585. const u8 (*data)[2], int len)
  586. {
  587. while (--len >= 0) {
  588. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  589. data++;
  590. }
  591. }
  592. /* output a sensor sequence (reg - val) */
  593. static void sccb_w_array(struct gspca_dev *gspca_dev,
  594. const u8 (*data)[2], int len)
  595. {
  596. while (--len >= 0) {
  597. if ((*data)[0] != 0xff) {
  598. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  599. } else {
  600. sccb_reg_read(gspca_dev, (*data)[1]);
  601. sccb_reg_write(gspca_dev, 0xff, 0x00);
  602. }
  603. data++;
  604. }
  605. }
  606. /* set framerate */
  607. static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
  608. {
  609. struct sd *sd = (struct sd *) gspca_dev;
  610. int fr = sd->frame_rate;
  611. switch (fr) {
  612. case 50:
  613. sccb_reg_write(gspca_dev, 0x11, 0x01);
  614. sccb_reg_write(gspca_dev, 0x0d, 0x41);
  615. ov534_reg_write(gspca_dev, 0xe5, 0x02);
  616. break;
  617. case 40:
  618. sccb_reg_write(gspca_dev, 0x11, 0x02);
  619. sccb_reg_write(gspca_dev, 0x0d, 0xc1);
  620. ov534_reg_write(gspca_dev, 0xe5, 0x04);
  621. break;
  622. /* case 30: */
  623. default:
  624. fr = 30;
  625. sccb_reg_write(gspca_dev, 0x11, 0x04);
  626. sccb_reg_write(gspca_dev, 0x0d, 0x81);
  627. ov534_reg_write(gspca_dev, 0xe5, 0x02);
  628. break;
  629. case 15:
  630. sccb_reg_write(gspca_dev, 0x11, 0x03);
  631. sccb_reg_write(gspca_dev, 0x0d, 0x41);
  632. ov534_reg_write(gspca_dev, 0xe5, 0x04);
  633. break;
  634. }
  635. sd->frame_rate = fr;
  636. PDEBUG(D_PROBE, "frame_rate: %d", fr);
  637. }
  638. /* this function is called at probe time */
  639. static int sd_config(struct gspca_dev *gspca_dev,
  640. const struct usb_device_id *id)
  641. {
  642. struct sd *sd = (struct sd *) gspca_dev;
  643. struct cam *cam;
  644. sd->sensor = id->driver_info;
  645. cam = &gspca_dev->cam;
  646. cam->cam_mode = vga_mode;
  647. cam->nmodes = ARRAY_SIZE(vga_mode);
  648. cam->bulk_size = 16384;
  649. cam->bulk_nurbs = 2;
  650. return 0;
  651. }
  652. /* this function is called at probe and resume time */
  653. static int sd_init(struct gspca_dev *gspca_dev)
  654. {
  655. struct sd *sd = (struct sd *) gspca_dev;
  656. u16 sensor_id;
  657. static const u8 sensor_addr[2] = {
  658. 0x42, /* 0 SENSOR_OV772X */
  659. 0x60, /* 1 SENSOR_OV965X */
  660. };
  661. /* reset bridge */
  662. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  663. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  664. msleep(100);
  665. /* initialize the sensor address */
  666. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
  667. sensor_addr[sd->sensor]);
  668. /* reset sensor */
  669. sccb_reg_write(gspca_dev, 0x12, 0x80);
  670. msleep(10);
  671. /* probe the sensor */
  672. sccb_reg_read(gspca_dev, 0x0a);
  673. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  674. sccb_reg_read(gspca_dev, 0x0b);
  675. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  676. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  677. /* initialize */
  678. switch (sd->sensor) {
  679. case SENSOR_OV772X:
  680. reg_w_array(gspca_dev, bridge_init_ov722x,
  681. ARRAY_SIZE(bridge_init_ov722x));
  682. ov534_set_led(gspca_dev, 1);
  683. sccb_w_array(gspca_dev, sensor_init_ov722x,
  684. ARRAY_SIZE(sensor_init_ov722x));
  685. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  686. ov534_set_led(gspca_dev, 0);
  687. ov534_set_frame_rate(gspca_dev);
  688. break;
  689. default:
  690. /* case SENSOR_OV965X: */
  691. reg_w_array(gspca_dev, bridge_init_ov965x,
  692. ARRAY_SIZE(bridge_init_ov965x));
  693. sccb_w_array(gspca_dev, sensor_init_ov965x,
  694. ARRAY_SIZE(sensor_init_ov965x));
  695. reg_w_array(gspca_dev, bridge_init_ov965x_2,
  696. ARRAY_SIZE(bridge_init_ov965x_2));
  697. sccb_w_array(gspca_dev, sensor_init_ov965x_2,
  698. ARRAY_SIZE(sensor_init_ov965x_2));
  699. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  700. ov534_reg_write(gspca_dev, 0xe0, 0x01);
  701. ov534_set_led(gspca_dev, 0);
  702. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  703. }
  704. return 0;
  705. }
  706. static int sd_start(struct gspca_dev *gspca_dev)
  707. {
  708. struct sd *sd = (struct sd *) gspca_dev;
  709. switch (sd->sensor) {
  710. case SENSOR_OV772X:
  711. ov534_set_led(gspca_dev, 1);
  712. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  713. break;
  714. default:
  715. /* case SENSOR_OV965X: */
  716. reg_w_array(gspca_dev, bridge_start_ov965x,
  717. ARRAY_SIZE(bridge_start_ov965x));
  718. sccb_w_array(gspca_dev, sensor_start_ov965x,
  719. ARRAY_SIZE(sensor_start_ov965x));
  720. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  721. ov534_set_led(gspca_dev, 1);
  722. /*fixme: other sensor start omitted*/
  723. }
  724. return 0;
  725. }
  726. static void sd_stopN(struct gspca_dev *gspca_dev)
  727. {
  728. struct sd *sd = (struct sd *) gspca_dev;
  729. switch (sd->sensor) {
  730. case SENSOR_OV772X:
  731. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  732. ov534_set_led(gspca_dev, 0);
  733. break;
  734. default:
  735. /* case SENSOR_OV965X: */
  736. ov534_reg_write(gspca_dev, 0xe0, 0x01);
  737. ov534_set_led(gspca_dev, 0);
  738. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  739. break;
  740. }
  741. }
  742. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  743. #define UVC_STREAM_EOH (1 << 7)
  744. #define UVC_STREAM_ERR (1 << 6)
  745. #define UVC_STREAM_STI (1 << 5)
  746. #define UVC_STREAM_RES (1 << 4)
  747. #define UVC_STREAM_SCR (1 << 3)
  748. #define UVC_STREAM_PTS (1 << 2)
  749. #define UVC_STREAM_EOF (1 << 1)
  750. #define UVC_STREAM_FID (1 << 0)
  751. static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
  752. __u8 *data, int len)
  753. {
  754. struct sd *sd = (struct sd *) gspca_dev;
  755. __u32 this_pts;
  756. u16 this_fid;
  757. int remaining_len = len;
  758. do {
  759. len = min(remaining_len, 2040); /*fixme: was 2048*/
  760. /* Payloads are prefixed with a UVC-style header. We
  761. consider a frame to start when the FID toggles, or the PTS
  762. changes. A frame ends when EOF is set, and we've received
  763. the correct number of bytes. */
  764. /* Verify UVC header. Header length is always 12 */
  765. if (data[0] != 12 || len < 12) {
  766. PDEBUG(D_PACK, "bad header");
  767. goto discard;
  768. }
  769. /* Check errors */
  770. if (data[1] & UVC_STREAM_ERR) {
  771. PDEBUG(D_PACK, "payload error");
  772. goto discard;
  773. }
  774. /* Extract PTS and FID */
  775. if (!(data[1] & UVC_STREAM_PTS)) {
  776. PDEBUG(D_PACK, "PTS not present");
  777. goto discard;
  778. }
  779. this_pts = (data[5] << 24) | (data[4] << 16)
  780. | (data[3] << 8) | data[2];
  781. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  782. /* If PTS or FID has changed, start a new frame. */
  783. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  784. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  785. NULL, 0);
  786. sd->last_pts = this_pts;
  787. sd->last_fid = this_fid;
  788. }
  789. /* Add the data from this payload */
  790. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  791. data + 12, len - 12);
  792. /* If this packet is marked as EOF, end the frame */
  793. if (data[1] & UVC_STREAM_EOF) {
  794. sd->last_pts = 0;
  795. if (frame->data_end - frame->data !=
  796. gspca_dev->width * gspca_dev->height * 2) {
  797. PDEBUG(D_PACK, "short frame");
  798. goto discard;
  799. }
  800. frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  801. NULL, 0);
  802. }
  803. /* Done this payload */
  804. goto scan_next;
  805. discard:
  806. /* Discard data until a new frame starts. */
  807. gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
  808. scan_next:
  809. remaining_len -= len;
  810. data += len;
  811. } while (remaining_len > 0);
  812. }
  813. /* get stream parameters (framerate) */
  814. static int sd_get_streamparm(struct gspca_dev *gspca_dev,
  815. struct v4l2_streamparm *parm)
  816. {
  817. struct v4l2_captureparm *cp = &parm->parm.capture;
  818. struct v4l2_fract *tpf = &cp->timeperframe;
  819. struct sd *sd = (struct sd *) gspca_dev;
  820. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  821. return -EINVAL;
  822. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  823. tpf->numerator = 1;
  824. tpf->denominator = sd->frame_rate;
  825. return 0;
  826. }
  827. /* set stream parameters (framerate) */
  828. static int sd_set_streamparm(struct gspca_dev *gspca_dev,
  829. struct v4l2_streamparm *parm)
  830. {
  831. struct v4l2_captureparm *cp = &parm->parm.capture;
  832. struct v4l2_fract *tpf = &cp->timeperframe;
  833. struct sd *sd = (struct sd *) gspca_dev;
  834. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  835. return -EINVAL;
  836. /* Set requested framerate */
  837. sd->frame_rate = tpf->denominator / tpf->numerator;
  838. ov534_set_frame_rate(gspca_dev);
  839. /* Return the actual framerate */
  840. tpf->numerator = 1;
  841. tpf->denominator = sd->frame_rate;
  842. return 0;
  843. }
  844. /* sub-driver description */
  845. static const struct sd_desc sd_desc = {
  846. .name = MODULE_NAME,
  847. .ctrls = sd_ctrls,
  848. .nctrls = ARRAY_SIZE(sd_ctrls),
  849. .config = sd_config,
  850. .init = sd_init,
  851. .start = sd_start,
  852. .stopN = sd_stopN,
  853. .pkt_scan = sd_pkt_scan,
  854. .get_streamparm = sd_get_streamparm,
  855. .set_streamparm = sd_set_streamparm,
  856. };
  857. /* -- module initialisation -- */
  858. static const __devinitdata struct usb_device_id device_table[] = {
  859. {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
  860. {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
  861. {}
  862. };
  863. MODULE_DEVICE_TABLE(usb, device_table);
  864. /* -- device connect -- */
  865. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  866. {
  867. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  868. THIS_MODULE);
  869. }
  870. static struct usb_driver sd_driver = {
  871. .name = MODULE_NAME,
  872. .id_table = device_table,
  873. .probe = sd_probe,
  874. .disconnect = gspca_disconnect,
  875. #ifdef CONFIG_PM
  876. .suspend = gspca_suspend,
  877. .resume = gspca_resume,
  878. #endif
  879. };
  880. /* -- module insert / remove -- */
  881. static int __init sd_mod_init(void)
  882. {
  883. int ret;
  884. ret = usb_register(&sd_driver);
  885. if (ret < 0)
  886. return ret;
  887. PDEBUG(D_PROBE, "registered");
  888. return 0;
  889. }
  890. static void __exit sd_mod_exit(void)
  891. {
  892. usb_deregister(&sd_driver);
  893. PDEBUG(D_PROBE, "deregistered");
  894. }
  895. module_init(sd_mod_init);
  896. module_exit(sd_mod_exit);