ov534.c 37 KB

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