ov534.c 35 KB

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