ov534.c 37 KB

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