ov534.c 35 KB

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