ov534.c 36 KB

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