ov534.c 34 KB

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