ov534.c 38 KB

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