ov534.c 36 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. PDEBUG(D_USBO, "SET 01 0000 %04x %02x", 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. PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
  601. if (ret < 0) {
  602. pr_err("read failed %d\n", ret);
  603. gspca_dev->usb_err = ret;
  604. }
  605. return gspca_dev->usb_buf[0];
  606. }
  607. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  608. * (direction and output)? */
  609. static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
  610. {
  611. u8 data;
  612. PDEBUG(D_CONF, "led status: %d", status);
  613. data = ov534_reg_read(gspca_dev, 0x21);
  614. data |= 0x80;
  615. ov534_reg_write(gspca_dev, 0x21, data);
  616. data = ov534_reg_read(gspca_dev, 0x23);
  617. if (status)
  618. data |= 0x80;
  619. else
  620. data &= ~0x80;
  621. ov534_reg_write(gspca_dev, 0x23, data);
  622. if (!status) {
  623. data = ov534_reg_read(gspca_dev, 0x21);
  624. data &= ~0x80;
  625. ov534_reg_write(gspca_dev, 0x21, data);
  626. }
  627. }
  628. static int sccb_check_status(struct gspca_dev *gspca_dev)
  629. {
  630. u8 data;
  631. int i;
  632. for (i = 0; i < 5; i++) {
  633. msleep(10);
  634. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  635. switch (data) {
  636. case 0x00:
  637. return 1;
  638. case 0x04:
  639. return 0;
  640. case 0x03:
  641. break;
  642. default:
  643. PERR("sccb status 0x%02x, attempt %d/5",
  644. data, i + 1);
  645. }
  646. }
  647. return 0;
  648. }
  649. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  650. {
  651. PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
  652. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  653. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  654. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  655. if (!sccb_check_status(gspca_dev)) {
  656. pr_err("sccb_reg_write failed\n");
  657. gspca_dev->usb_err = -EIO;
  658. }
  659. }
  660. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  661. {
  662. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  663. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  664. if (!sccb_check_status(gspca_dev))
  665. pr_err("sccb_reg_read failed 1\n");
  666. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  667. if (!sccb_check_status(gspca_dev))
  668. pr_err("sccb_reg_read failed 2\n");
  669. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  670. }
  671. /* output a bridge sequence (reg - val) */
  672. static void reg_w_array(struct gspca_dev *gspca_dev,
  673. const u8 (*data)[2], int len)
  674. {
  675. while (--len >= 0) {
  676. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  677. data++;
  678. }
  679. }
  680. /* output a sensor sequence (reg - val) */
  681. static void sccb_w_array(struct gspca_dev *gspca_dev,
  682. const u8 (*data)[2], int len)
  683. {
  684. while (--len >= 0) {
  685. if ((*data)[0] != 0xff) {
  686. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  687. } else {
  688. sccb_reg_read(gspca_dev, (*data)[1]);
  689. sccb_reg_write(gspca_dev, 0xff, 0x00);
  690. }
  691. data++;
  692. }
  693. }
  694. /* ov772x specific controls */
  695. static void set_frame_rate(struct gspca_dev *gspca_dev)
  696. {
  697. struct sd *sd = (struct sd *) gspca_dev;
  698. int i;
  699. struct rate_s {
  700. u8 fps;
  701. u8 r11;
  702. u8 r0d;
  703. u8 re5;
  704. };
  705. const struct rate_s *r;
  706. static const struct rate_s rate_0[] = { /* 640x480 */
  707. {60, 0x01, 0xc1, 0x04},
  708. {50, 0x01, 0x41, 0x02},
  709. {40, 0x02, 0xc1, 0x04},
  710. {30, 0x04, 0x81, 0x02},
  711. {15, 0x03, 0x41, 0x04},
  712. };
  713. static const struct rate_s rate_1[] = { /* 320x240 */
  714. /* {205, 0x01, 0xc1, 0x02}, * 205 FPS: video is partly corrupt */
  715. {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
  716. {150, 0x01, 0xc1, 0x04},
  717. {137, 0x02, 0xc1, 0x02},
  718. {125, 0x02, 0x81, 0x02},
  719. {100, 0x02, 0xc1, 0x04},
  720. {75, 0x03, 0xc1, 0x04},
  721. {60, 0x04, 0xc1, 0x04},
  722. {50, 0x02, 0x41, 0x04},
  723. {37, 0x03, 0x41, 0x04},
  724. {30, 0x04, 0x41, 0x04},
  725. };
  726. if (sd->sensor != SENSOR_OV772x)
  727. return;
  728. if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
  729. r = rate_0;
  730. i = ARRAY_SIZE(rate_0);
  731. } else {
  732. r = rate_1;
  733. i = ARRAY_SIZE(rate_1);
  734. }
  735. while (--i > 0) {
  736. if (sd->frame_rate >= r->fps)
  737. break;
  738. r++;
  739. }
  740. sccb_reg_write(gspca_dev, 0x11, r->r11);
  741. sccb_reg_write(gspca_dev, 0x0d, r->r0d);
  742. ov534_reg_write(gspca_dev, 0xe5, r->re5);
  743. PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
  744. }
  745. static void sethue(struct gspca_dev *gspca_dev, s32 val)
  746. {
  747. struct sd *sd = (struct sd *) gspca_dev;
  748. if (sd->sensor == SENSOR_OV767x) {
  749. /* TBD */
  750. } else {
  751. s16 huesin;
  752. s16 huecos;
  753. /* According to the datasheet the registers expect HUESIN and
  754. * HUECOS to be the result of the trigonometric functions,
  755. * scaled by 0x80.
  756. *
  757. * The 0x7fff here represents the maximum absolute value
  758. * returned byt fixp_sin and fixp_cos, so the scaling will
  759. * consider the result like in the interval [-1.0, 1.0].
  760. */
  761. huesin = fixp_sin16(val) * 0x80 / 0x7fff;
  762. huecos = fixp_cos16(val) * 0x80 / 0x7fff;
  763. if (huesin < 0) {
  764. sccb_reg_write(gspca_dev, 0xab,
  765. sccb_reg_read(gspca_dev, 0xab) | 0x2);
  766. huesin = -huesin;
  767. } else {
  768. sccb_reg_write(gspca_dev, 0xab,
  769. sccb_reg_read(gspca_dev, 0xab) & ~0x2);
  770. }
  771. sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
  772. sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
  773. }
  774. }
  775. static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
  776. {
  777. struct sd *sd = (struct sd *) gspca_dev;
  778. if (sd->sensor == SENSOR_OV767x) {
  779. int i;
  780. static u8 color_tb[][6] = {
  781. {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
  782. {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
  783. {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
  784. {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
  785. {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
  786. {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
  787. {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
  788. };
  789. for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
  790. sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
  791. } else {
  792. sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
  793. sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
  794. }
  795. }
  796. static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
  797. {
  798. struct sd *sd = (struct sd *) gspca_dev;
  799. if (sd->sensor == SENSOR_OV767x) {
  800. if (val < 0)
  801. val = 0x80 - val;
  802. sccb_reg_write(gspca_dev, 0x55, val); /* bright */
  803. } else {
  804. sccb_reg_write(gspca_dev, 0x9b, val);
  805. }
  806. }
  807. static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
  808. {
  809. struct sd *sd = (struct sd *) gspca_dev;
  810. if (sd->sensor == SENSOR_OV767x)
  811. sccb_reg_write(gspca_dev, 0x56, val); /* contras */
  812. else
  813. sccb_reg_write(gspca_dev, 0x9c, val);
  814. }
  815. static void setgain(struct gspca_dev *gspca_dev, s32 val)
  816. {
  817. switch (val & 0x30) {
  818. case 0x00:
  819. val &= 0x0f;
  820. break;
  821. case 0x10:
  822. val &= 0x0f;
  823. val |= 0x30;
  824. break;
  825. case 0x20:
  826. val &= 0x0f;
  827. val |= 0x70;
  828. break;
  829. default:
  830. /* case 0x30: */
  831. val &= 0x0f;
  832. val |= 0xf0;
  833. break;
  834. }
  835. sccb_reg_write(gspca_dev, 0x00, val);
  836. }
  837. static s32 getgain(struct gspca_dev *gspca_dev)
  838. {
  839. return sccb_reg_read(gspca_dev, 0x00);
  840. }
  841. static void setexposure(struct gspca_dev *gspca_dev, s32 val)
  842. {
  843. struct sd *sd = (struct sd *) gspca_dev;
  844. if (sd->sensor == SENSOR_OV767x) {
  845. /* set only aec[9:2] */
  846. sccb_reg_write(gspca_dev, 0x10, val); /* aech */
  847. } else {
  848. /* 'val' is one byte and represents half of the exposure value
  849. * we are going to set into registers, a two bytes value:
  850. *
  851. * MSB: ((u16) val << 1) >> 8 == val >> 7
  852. * LSB: ((u16) val << 1) & 0xff == val << 1
  853. */
  854. sccb_reg_write(gspca_dev, 0x08, val >> 7);
  855. sccb_reg_write(gspca_dev, 0x10, val << 1);
  856. }
  857. }
  858. static s32 getexposure(struct gspca_dev *gspca_dev)
  859. {
  860. struct sd *sd = (struct sd *) gspca_dev;
  861. if (sd->sensor == SENSOR_OV767x) {
  862. /* get only aec[9:2] */
  863. return sccb_reg_read(gspca_dev, 0x10); /* aech */
  864. } else {
  865. u8 hi = sccb_reg_read(gspca_dev, 0x08);
  866. u8 lo = sccb_reg_read(gspca_dev, 0x10);
  867. return (hi << 8 | lo) >> 1;
  868. }
  869. }
  870. static void setagc(struct gspca_dev *gspca_dev, s32 val)
  871. {
  872. if (val) {
  873. sccb_reg_write(gspca_dev, 0x13,
  874. sccb_reg_read(gspca_dev, 0x13) | 0x04);
  875. sccb_reg_write(gspca_dev, 0x64,
  876. sccb_reg_read(gspca_dev, 0x64) | 0x03);
  877. } else {
  878. sccb_reg_write(gspca_dev, 0x13,
  879. sccb_reg_read(gspca_dev, 0x13) & ~0x04);
  880. sccb_reg_write(gspca_dev, 0x64,
  881. sccb_reg_read(gspca_dev, 0x64) & ~0x03);
  882. }
  883. }
  884. static void setawb(struct gspca_dev *gspca_dev, s32 val)
  885. {
  886. struct sd *sd = (struct sd *) gspca_dev;
  887. if (val) {
  888. sccb_reg_write(gspca_dev, 0x13,
  889. sccb_reg_read(gspca_dev, 0x13) | 0x02);
  890. if (sd->sensor == SENSOR_OV772x)
  891. sccb_reg_write(gspca_dev, 0x63,
  892. sccb_reg_read(gspca_dev, 0x63) | 0xc0);
  893. } else {
  894. sccb_reg_write(gspca_dev, 0x13,
  895. sccb_reg_read(gspca_dev, 0x13) & ~0x02);
  896. if (sd->sensor == SENSOR_OV772x)
  897. sccb_reg_write(gspca_dev, 0x63,
  898. sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
  899. }
  900. }
  901. static void setaec(struct gspca_dev *gspca_dev, s32 val)
  902. {
  903. struct sd *sd = (struct sd *) gspca_dev;
  904. u8 data;
  905. data = sd->sensor == SENSOR_OV767x ?
  906. 0x05 : /* agc + aec */
  907. 0x01; /* agc */
  908. switch (val) {
  909. case V4L2_EXPOSURE_AUTO:
  910. sccb_reg_write(gspca_dev, 0x13,
  911. sccb_reg_read(gspca_dev, 0x13) | data);
  912. break;
  913. case V4L2_EXPOSURE_MANUAL:
  914. sccb_reg_write(gspca_dev, 0x13,
  915. sccb_reg_read(gspca_dev, 0x13) & ~data);
  916. break;
  917. }
  918. }
  919. static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
  920. {
  921. sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
  922. sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
  923. }
  924. static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
  925. {
  926. struct sd *sd = (struct sd *) gspca_dev;
  927. u8 val;
  928. if (sd->sensor == SENSOR_OV767x) {
  929. val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
  930. val &= ~0x30;
  931. if (hflip)
  932. val |= 0x20;
  933. if (vflip)
  934. val |= 0x10;
  935. sccb_reg_write(gspca_dev, 0x1e, val);
  936. } else {
  937. val = sccb_reg_read(gspca_dev, 0x0c);
  938. val &= ~0xc0;
  939. if (hflip == 0)
  940. val |= 0x40;
  941. if (vflip == 0)
  942. val |= 0x80;
  943. sccb_reg_write(gspca_dev, 0x0c, val);
  944. }
  945. }
  946. static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
  947. {
  948. struct sd *sd = (struct sd *) gspca_dev;
  949. val = val ? 0x9e : 0x00;
  950. if (sd->sensor == SENSOR_OV767x) {
  951. sccb_reg_write(gspca_dev, 0x2a, 0x00);
  952. if (val)
  953. val = 0x9d; /* insert dummy to 25fps for 50Hz */
  954. }
  955. sccb_reg_write(gspca_dev, 0x2b, val);
  956. }
  957. /* this function is called at probe time */
  958. static int sd_config(struct gspca_dev *gspca_dev,
  959. const struct usb_device_id *id)
  960. {
  961. struct sd *sd = (struct sd *) gspca_dev;
  962. struct cam *cam;
  963. cam = &gspca_dev->cam;
  964. cam->cam_mode = ov772x_mode;
  965. cam->nmodes = ARRAY_SIZE(ov772x_mode);
  966. sd->frame_rate = DEFAULT_FRAME_RATE;
  967. return 0;
  968. }
  969. static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  970. {
  971. struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
  972. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  973. switch (ctrl->id) {
  974. case V4L2_CID_AUTOGAIN:
  975. gspca_dev->usb_err = 0;
  976. if (ctrl->val && sd->gain && gspca_dev->streaming)
  977. sd->gain->val = getgain(gspca_dev);
  978. return gspca_dev->usb_err;
  979. case V4L2_CID_EXPOSURE_AUTO:
  980. gspca_dev->usb_err = 0;
  981. if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
  982. gspca_dev->streaming)
  983. sd->exposure->val = getexposure(gspca_dev);
  984. return gspca_dev->usb_err;
  985. }
  986. return -EINVAL;
  987. }
  988. static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
  989. {
  990. struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
  991. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  992. gspca_dev->usb_err = 0;
  993. if (!gspca_dev->streaming)
  994. return 0;
  995. switch (ctrl->id) {
  996. case V4L2_CID_HUE:
  997. sethue(gspca_dev, ctrl->val);
  998. break;
  999. case V4L2_CID_SATURATION:
  1000. setsaturation(gspca_dev, ctrl->val);
  1001. break;
  1002. case V4L2_CID_BRIGHTNESS:
  1003. setbrightness(gspca_dev, ctrl->val);
  1004. break;
  1005. case V4L2_CID_CONTRAST:
  1006. setcontrast(gspca_dev, ctrl->val);
  1007. break;
  1008. case V4L2_CID_AUTOGAIN:
  1009. /* case V4L2_CID_GAIN: */
  1010. setagc(gspca_dev, ctrl->val);
  1011. if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
  1012. setgain(gspca_dev, sd->gain->val);
  1013. break;
  1014. case V4L2_CID_AUTO_WHITE_BALANCE:
  1015. setawb(gspca_dev, ctrl->val);
  1016. break;
  1017. case V4L2_CID_EXPOSURE_AUTO:
  1018. /* case V4L2_CID_EXPOSURE: */
  1019. setaec(gspca_dev, ctrl->val);
  1020. if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
  1021. sd->exposure)
  1022. setexposure(gspca_dev, sd->exposure->val);
  1023. break;
  1024. case V4L2_CID_SHARPNESS:
  1025. setsharpness(gspca_dev, ctrl->val);
  1026. break;
  1027. case V4L2_CID_HFLIP:
  1028. sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
  1029. break;
  1030. case V4L2_CID_VFLIP:
  1031. sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
  1032. break;
  1033. case V4L2_CID_POWER_LINE_FREQUENCY:
  1034. setlightfreq(gspca_dev, ctrl->val);
  1035. break;
  1036. }
  1037. return gspca_dev->usb_err;
  1038. }
  1039. static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
  1040. .g_volatile_ctrl = ov534_g_volatile_ctrl,
  1041. .s_ctrl = ov534_s_ctrl,
  1042. };
  1043. static int sd_init_controls(struct gspca_dev *gspca_dev)
  1044. {
  1045. struct sd *sd = (struct sd *) gspca_dev;
  1046. struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
  1047. /* parameters with different values between the supported sensors */
  1048. int saturation_min;
  1049. int saturation_max;
  1050. int saturation_def;
  1051. int brightness_min;
  1052. int brightness_max;
  1053. int brightness_def;
  1054. int contrast_max;
  1055. int contrast_def;
  1056. int exposure_min;
  1057. int exposure_max;
  1058. int exposure_def;
  1059. int hflip_def;
  1060. if (sd->sensor == SENSOR_OV767x) {
  1061. saturation_min = 0,
  1062. saturation_max = 6,
  1063. saturation_def = 3,
  1064. brightness_min = -127;
  1065. brightness_max = 127;
  1066. brightness_def = 0;
  1067. contrast_max = 0x80;
  1068. contrast_def = 0x40;
  1069. exposure_min = 0x08;
  1070. exposure_max = 0x60;
  1071. exposure_def = 0x13;
  1072. hflip_def = 1;
  1073. } else {
  1074. saturation_min = 0,
  1075. saturation_max = 255,
  1076. saturation_def = 64,
  1077. brightness_min = 0;
  1078. brightness_max = 255;
  1079. brightness_def = 0;
  1080. contrast_max = 255;
  1081. contrast_def = 32;
  1082. exposure_min = 0;
  1083. exposure_max = 255;
  1084. exposure_def = 120;
  1085. hflip_def = 0;
  1086. }
  1087. gspca_dev->vdev.ctrl_handler = hdl;
  1088. v4l2_ctrl_handler_init(hdl, 13);
  1089. if (sd->sensor == SENSOR_OV772x)
  1090. sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1091. V4L2_CID_HUE, -90, 90, 1, 0);
  1092. sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1093. V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
  1094. saturation_def);
  1095. sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1096. V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
  1097. brightness_def);
  1098. sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1099. V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
  1100. if (sd->sensor == SENSOR_OV772x) {
  1101. sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1102. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1103. sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1104. V4L2_CID_GAIN, 0, 63, 1, 20);
  1105. }
  1106. sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
  1107. V4L2_CID_EXPOSURE_AUTO,
  1108. V4L2_EXPOSURE_MANUAL, 0,
  1109. V4L2_EXPOSURE_AUTO);
  1110. sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1111. V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
  1112. exposure_def);
  1113. sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1114. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  1115. if (sd->sensor == SENSOR_OV772x)
  1116. sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1117. V4L2_CID_SHARPNESS, 0, 63, 1, 0);
  1118. sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1119. V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
  1120. sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
  1121. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1122. sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
  1123. V4L2_CID_POWER_LINE_FREQUENCY,
  1124. V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
  1125. V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
  1126. if (hdl->error) {
  1127. pr_err("Could not initialize controls\n");
  1128. return hdl->error;
  1129. }
  1130. if (sd->sensor == SENSOR_OV772x)
  1131. v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
  1132. v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
  1133. true);
  1134. return 0;
  1135. }
  1136. /* this function is called at probe and resume time */
  1137. static int sd_init(struct gspca_dev *gspca_dev)
  1138. {
  1139. struct sd *sd = (struct sd *) gspca_dev;
  1140. u16 sensor_id;
  1141. static const struct reg_array bridge_init[NSENSORS] = {
  1142. [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
  1143. [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
  1144. };
  1145. static const struct reg_array sensor_init[NSENSORS] = {
  1146. [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
  1147. [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
  1148. };
  1149. /* reset bridge */
  1150. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  1151. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  1152. msleep(100);
  1153. /* initialize the sensor address */
  1154. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
  1155. /* reset sensor */
  1156. sccb_reg_write(gspca_dev, 0x12, 0x80);
  1157. msleep(10);
  1158. /* probe the sensor */
  1159. sccb_reg_read(gspca_dev, 0x0a);
  1160. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  1161. sccb_reg_read(gspca_dev, 0x0b);
  1162. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  1163. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  1164. if ((sensor_id & 0xfff0) == 0x7670) {
  1165. sd->sensor = SENSOR_OV767x;
  1166. gspca_dev->cam.cam_mode = ov767x_mode;
  1167. gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
  1168. } else {
  1169. sd->sensor = SENSOR_OV772x;
  1170. gspca_dev->cam.bulk = 1;
  1171. gspca_dev->cam.bulk_size = 16384;
  1172. gspca_dev->cam.bulk_nurbs = 2;
  1173. gspca_dev->cam.mode_framerates = ov772x_framerates;
  1174. }
  1175. /* initialize */
  1176. reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
  1177. bridge_init[sd->sensor].len);
  1178. ov534_set_led(gspca_dev, 1);
  1179. sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
  1180. sensor_init[sd->sensor].len);
  1181. sd_stopN(gspca_dev);
  1182. /* set_frame_rate(gspca_dev); */
  1183. return gspca_dev->usb_err;
  1184. }
  1185. static int sd_start(struct gspca_dev *gspca_dev)
  1186. {
  1187. struct sd *sd = (struct sd *) gspca_dev;
  1188. int mode;
  1189. static const struct reg_array bridge_start[NSENSORS][2] = {
  1190. [SENSOR_OV767x] = {{bridge_start_qvga_767x,
  1191. ARRAY_SIZE(bridge_start_qvga_767x)},
  1192. {bridge_start_vga_767x,
  1193. ARRAY_SIZE(bridge_start_vga_767x)}},
  1194. [SENSOR_OV772x] = {{bridge_start_qvga_772x,
  1195. ARRAY_SIZE(bridge_start_qvga_772x)},
  1196. {bridge_start_vga_772x,
  1197. ARRAY_SIZE(bridge_start_vga_772x)}},
  1198. };
  1199. static const struct reg_array sensor_start[NSENSORS][2] = {
  1200. [SENSOR_OV767x] = {{sensor_start_qvga_767x,
  1201. ARRAY_SIZE(sensor_start_qvga_767x)},
  1202. {sensor_start_vga_767x,
  1203. ARRAY_SIZE(sensor_start_vga_767x)}},
  1204. [SENSOR_OV772x] = {{sensor_start_qvga_772x,
  1205. ARRAY_SIZE(sensor_start_qvga_772x)},
  1206. {sensor_start_vga_772x,
  1207. ARRAY_SIZE(sensor_start_vga_772x)}},
  1208. };
  1209. /* (from ms-win trace) */
  1210. if (sd->sensor == SENSOR_OV767x)
  1211. sccb_reg_write(gspca_dev, 0x1e, 0x04);
  1212. /* black sun enable ? */
  1213. mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
  1214. reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
  1215. bridge_start[sd->sensor][mode].len);
  1216. sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
  1217. sensor_start[sd->sensor][mode].len);
  1218. set_frame_rate(gspca_dev);
  1219. if (sd->hue)
  1220. sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
  1221. setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
  1222. if (sd->autogain)
  1223. setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
  1224. setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
  1225. setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
  1226. if (sd->gain)
  1227. setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
  1228. setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
  1229. setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
  1230. setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
  1231. if (sd->sharpness)
  1232. setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
  1233. sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
  1234. v4l2_ctrl_g_ctrl(sd->vflip));
  1235. setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
  1236. ov534_set_led(gspca_dev, 1);
  1237. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  1238. return gspca_dev->usb_err;
  1239. }
  1240. static void sd_stopN(struct gspca_dev *gspca_dev)
  1241. {
  1242. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  1243. ov534_set_led(gspca_dev, 0);
  1244. }
  1245. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  1246. #define UVC_STREAM_EOH (1 << 7)
  1247. #define UVC_STREAM_ERR (1 << 6)
  1248. #define UVC_STREAM_STI (1 << 5)
  1249. #define UVC_STREAM_RES (1 << 4)
  1250. #define UVC_STREAM_SCR (1 << 3)
  1251. #define UVC_STREAM_PTS (1 << 2)
  1252. #define UVC_STREAM_EOF (1 << 1)
  1253. #define UVC_STREAM_FID (1 << 0)
  1254. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1255. u8 *data, int len)
  1256. {
  1257. struct sd *sd = (struct sd *) gspca_dev;
  1258. __u32 this_pts;
  1259. u16 this_fid;
  1260. int remaining_len = len;
  1261. int payload_len;
  1262. payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
  1263. do {
  1264. len = min(remaining_len, payload_len);
  1265. /* Payloads are prefixed with a UVC-style header. We
  1266. consider a frame to start when the FID toggles, or the PTS
  1267. changes. A frame ends when EOF is set, and we've received
  1268. the correct number of bytes. */
  1269. /* Verify UVC header. Header length is always 12 */
  1270. if (data[0] != 12 || len < 12) {
  1271. PDEBUG(D_PACK, "bad header");
  1272. goto discard;
  1273. }
  1274. /* Check errors */
  1275. if (data[1] & UVC_STREAM_ERR) {
  1276. PDEBUG(D_PACK, "payload error");
  1277. goto discard;
  1278. }
  1279. /* Extract PTS and FID */
  1280. if (!(data[1] & UVC_STREAM_PTS)) {
  1281. PDEBUG(D_PACK, "PTS not present");
  1282. goto discard;
  1283. }
  1284. this_pts = (data[5] << 24) | (data[4] << 16)
  1285. | (data[3] << 8) | data[2];
  1286. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  1287. /* If PTS or FID has changed, start a new frame. */
  1288. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  1289. if (gspca_dev->last_packet_type == INTER_PACKET)
  1290. gspca_frame_add(gspca_dev, LAST_PACKET,
  1291. NULL, 0);
  1292. sd->last_pts = this_pts;
  1293. sd->last_fid = this_fid;
  1294. gspca_frame_add(gspca_dev, FIRST_PACKET,
  1295. data + 12, len - 12);
  1296. /* If this packet is marked as EOF, end the frame */
  1297. } else if (data[1] & UVC_STREAM_EOF) {
  1298. sd->last_pts = 0;
  1299. if (gspca_dev->pixfmt.pixelformat == V4L2_PIX_FMT_YUYV
  1300. && gspca_dev->image_len + len - 12 !=
  1301. gspca_dev->pixfmt.width *
  1302. gspca_dev->pixfmt.height * 2) {
  1303. PDEBUG(D_PACK, "wrong sized frame");
  1304. goto discard;
  1305. }
  1306. gspca_frame_add(gspca_dev, LAST_PACKET,
  1307. data + 12, len - 12);
  1308. } else {
  1309. /* Add the data from this payload */
  1310. gspca_frame_add(gspca_dev, INTER_PACKET,
  1311. data + 12, len - 12);
  1312. }
  1313. /* Done this payload */
  1314. goto scan_next;
  1315. discard:
  1316. /* Discard data until a new frame starts. */
  1317. gspca_dev->last_packet_type = DISCARD_PACKET;
  1318. scan_next:
  1319. remaining_len -= len;
  1320. data += len;
  1321. } while (remaining_len > 0);
  1322. }
  1323. /* get stream parameters (framerate) */
  1324. static void sd_get_streamparm(struct gspca_dev *gspca_dev,
  1325. struct v4l2_streamparm *parm)
  1326. {
  1327. struct v4l2_captureparm *cp = &parm->parm.capture;
  1328. struct v4l2_fract *tpf = &cp->timeperframe;
  1329. struct sd *sd = (struct sd *) gspca_dev;
  1330. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  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);