ov534.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * ov534-ov772x 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, hue, AWB control added
  14. * 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. /* specific webcam descriptor */
  46. struct sd {
  47. struct gspca_dev gspca_dev; /* !! must be the first item */
  48. __u32 last_pts;
  49. u16 last_fid;
  50. u8 frame_rate;
  51. u8 brightness;
  52. u8 contrast;
  53. u8 gain;
  54. u8 exposure;
  55. u8 redblc;
  56. u8 blueblc;
  57. u8 hue;
  58. u8 autogain;
  59. u8 awb;
  60. s8 sharpness;
  61. u8 hflip;
  62. u8 vflip;
  63. };
  64. /* V4L2 controls supported by the driver */
  65. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
  66. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
  67. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
  68. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
  69. static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val);
  70. static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val);
  71. static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val);
  72. static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val);
  73. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
  74. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
  75. static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
  76. static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
  77. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  78. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  79. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  80. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  81. static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
  82. static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
  83. static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
  84. static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
  85. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  86. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  87. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  88. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  89. static const struct ctrl sd_ctrls[] = {
  90. { /* 0 */
  91. {
  92. .id = V4L2_CID_BRIGHTNESS,
  93. .type = V4L2_CTRL_TYPE_INTEGER,
  94. .name = "Brightness",
  95. .minimum = 0,
  96. .maximum = 255,
  97. .step = 1,
  98. #define BRIGHTNESS_DEF 20
  99. .default_value = BRIGHTNESS_DEF,
  100. },
  101. .set = sd_setbrightness,
  102. .get = sd_getbrightness,
  103. },
  104. { /* 1 */
  105. {
  106. .id = V4L2_CID_CONTRAST,
  107. .type = V4L2_CTRL_TYPE_INTEGER,
  108. .name = "Contrast",
  109. .minimum = 0,
  110. .maximum = 255,
  111. .step = 1,
  112. #define CONTRAST_DEF 37
  113. .default_value = CONTRAST_DEF,
  114. },
  115. .set = sd_setcontrast,
  116. .get = sd_getcontrast,
  117. },
  118. { /* 2 */
  119. {
  120. .id = V4L2_CID_GAIN,
  121. .type = V4L2_CTRL_TYPE_INTEGER,
  122. .name = "Main Gain",
  123. .minimum = 0,
  124. .maximum = 63,
  125. .step = 1,
  126. #define GAIN_DEF 20
  127. .default_value = GAIN_DEF,
  128. },
  129. .set = sd_setgain,
  130. .get = sd_getgain,
  131. },
  132. { /* 3 */
  133. {
  134. .id = V4L2_CID_EXPOSURE,
  135. .type = V4L2_CTRL_TYPE_INTEGER,
  136. .name = "Exposure",
  137. .minimum = 0,
  138. .maximum = 255,
  139. .step = 1,
  140. #define EXPO_DEF 120
  141. .default_value = EXPO_DEF,
  142. },
  143. .set = sd_setexposure,
  144. .get = sd_getexposure,
  145. },
  146. { /* 4 */
  147. {
  148. .id = V4L2_CID_RED_BALANCE,
  149. .type = V4L2_CTRL_TYPE_INTEGER,
  150. .name = "Red Balance",
  151. .minimum = 0,
  152. .maximum = 255,
  153. .step = 1,
  154. #define RED_BALANCE_DEF 128
  155. .default_value = RED_BALANCE_DEF,
  156. },
  157. .set = sd_setredblc,
  158. .get = sd_getredblc,
  159. },
  160. { /* 5 */
  161. {
  162. .id = V4L2_CID_BLUE_BALANCE,
  163. .type = V4L2_CTRL_TYPE_INTEGER,
  164. .name = "Blue Balance",
  165. .minimum = 0,
  166. .maximum = 255,
  167. .step = 1,
  168. #define BLUE_BALANCE_DEF 128
  169. .default_value = BLUE_BALANCE_DEF,
  170. },
  171. .set = sd_setblueblc,
  172. .get = sd_getblueblc,
  173. },
  174. { /* 6 */
  175. {
  176. .id = V4L2_CID_HUE,
  177. .type = V4L2_CTRL_TYPE_INTEGER,
  178. .name = "Hue",
  179. .minimum = 0,
  180. .maximum = 255,
  181. .step = 1,
  182. #define HUE_DEF 143
  183. .default_value = HUE_DEF,
  184. },
  185. .set = sd_sethue,
  186. .get = sd_gethue,
  187. },
  188. { /* 7 */
  189. {
  190. .id = V4L2_CID_AUTOGAIN,
  191. .type = V4L2_CTRL_TYPE_BOOLEAN,
  192. .name = "Autogain",
  193. .minimum = 0,
  194. .maximum = 1,
  195. .step = 1,
  196. #define AUTOGAIN_DEF 0
  197. .default_value = AUTOGAIN_DEF,
  198. },
  199. .set = sd_setautogain,
  200. .get = sd_getautogain,
  201. },
  202. #define AWB_IDX 8
  203. { /* 8 */
  204. {
  205. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  206. .type = V4L2_CTRL_TYPE_BOOLEAN,
  207. .name = "Auto White Balance",
  208. .minimum = 0,
  209. .maximum = 1,
  210. .step = 1,
  211. #define AWB_DEF 0
  212. .default_value = AWB_DEF,
  213. },
  214. .set = sd_setawb,
  215. .get = sd_getawb,
  216. },
  217. { /* 9 */
  218. {
  219. .id = V4L2_CID_SHARPNESS,
  220. .type = V4L2_CTRL_TYPE_INTEGER,
  221. .name = "Sharpness",
  222. .minimum = 0,
  223. .maximum = 63,
  224. .step = 1,
  225. #define SHARPNESS_DEF 0
  226. .default_value = SHARPNESS_DEF,
  227. },
  228. .set = sd_setsharpness,
  229. .get = sd_getsharpness,
  230. },
  231. { /* 10 */
  232. {
  233. .id = V4L2_CID_HFLIP,
  234. .type = V4L2_CTRL_TYPE_BOOLEAN,
  235. .name = "HFlip",
  236. .minimum = 0,
  237. .maximum = 1,
  238. .step = 1,
  239. #define HFLIP_DEF 0
  240. .default_value = HFLIP_DEF,
  241. },
  242. .set = sd_sethflip,
  243. .get = sd_gethflip,
  244. },
  245. { /* 11 */
  246. {
  247. .id = V4L2_CID_VFLIP,
  248. .type = V4L2_CTRL_TYPE_BOOLEAN,
  249. .name = "VFlip",
  250. .minimum = 0,
  251. .maximum = 1,
  252. .step = 1,
  253. #define VFLIP_DEF 0
  254. .default_value = VFLIP_DEF,
  255. },
  256. .set = sd_setvflip,
  257. .get = sd_getvflip,
  258. },
  259. };
  260. static const struct v4l2_pix_format ov772x_mode[] = {
  261. {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  262. .bytesperline = 320 * 2,
  263. .sizeimage = 320 * 240 * 2,
  264. .colorspace = V4L2_COLORSPACE_SRGB,
  265. .priv = 1},
  266. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  267. .bytesperline = 640 * 2,
  268. .sizeimage = 640 * 480 * 2,
  269. .colorspace = V4L2_COLORSPACE_SRGB,
  270. .priv = 0},
  271. };
  272. static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
  273. static const u8 vga_rates[] = {60, 50, 40, 30, 15};
  274. static const struct framerates ov772x_framerates[] = {
  275. { /* 320x240 */
  276. .rates = qvga_rates,
  277. .nrates = ARRAY_SIZE(qvga_rates),
  278. },
  279. { /* 640x480 */
  280. .rates = vga_rates,
  281. .nrates = ARRAY_SIZE(vga_rates),
  282. },
  283. };
  284. static const u8 bridge_init[][2] = {
  285. { 0xc2, 0x0c },
  286. { 0x88, 0xf8 },
  287. { 0xc3, 0x69 },
  288. { 0x89, 0xff },
  289. { 0x76, 0x03 },
  290. { 0x92, 0x01 },
  291. { 0x93, 0x18 },
  292. { 0x94, 0x10 },
  293. { 0x95, 0x10 },
  294. { 0xe2, 0x00 },
  295. { 0xe7, 0x3e },
  296. { 0x96, 0x00 },
  297. { 0x97, 0x20 },
  298. { 0x97, 0x20 },
  299. { 0x97, 0x20 },
  300. { 0x97, 0x0a },
  301. { 0x97, 0x3f },
  302. { 0x97, 0x4a },
  303. { 0x97, 0x20 },
  304. { 0x97, 0x15 },
  305. { 0x97, 0x0b },
  306. { 0x8e, 0x40 },
  307. { 0x1f, 0x81 },
  308. { 0x34, 0x05 },
  309. { 0xe3, 0x04 },
  310. { 0x88, 0x00 },
  311. { 0x89, 0x00 },
  312. { 0x76, 0x00 },
  313. { 0xe7, 0x2e },
  314. { 0x31, 0xf9 },
  315. { 0x25, 0x42 },
  316. { 0x21, 0xf0 },
  317. { 0x1c, 0x00 },
  318. { 0x1d, 0x40 },
  319. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  320. { 0x1d, 0x00 }, /* payload size */
  321. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  322. { 0x1d, 0x58 }, /* frame size */
  323. { 0x1d, 0x00 }, /* frame size */
  324. { 0x1c, 0x0a },
  325. { 0x1d, 0x08 }, /* turn on UVC header */
  326. { 0x1d, 0x0e }, /* .. */
  327. { 0x8d, 0x1c },
  328. { 0x8e, 0x80 },
  329. { 0xe5, 0x04 },
  330. { 0xc0, 0x50 },
  331. { 0xc1, 0x3c },
  332. { 0xc2, 0x0c },
  333. };
  334. static const u8 sensor_init[][2] = {
  335. { 0x12, 0x80 },
  336. { 0x11, 0x01 },
  337. /*fixme: better have a delay?*/
  338. { 0x11, 0x01 },
  339. { 0x11, 0x01 },
  340. { 0x11, 0x01 },
  341. { 0x11, 0x01 },
  342. { 0x11, 0x01 },
  343. { 0x11, 0x01 },
  344. { 0x11, 0x01 },
  345. { 0x11, 0x01 },
  346. { 0x11, 0x01 },
  347. { 0x11, 0x01 },
  348. { 0x3d, 0x03 },
  349. { 0x17, 0x26 },
  350. { 0x18, 0xa0 },
  351. { 0x19, 0x07 },
  352. { 0x1a, 0xf0 },
  353. { 0x32, 0x00 },
  354. { 0x29, 0xa0 },
  355. { 0x2c, 0xf0 },
  356. { 0x65, 0x20 },
  357. { 0x11, 0x01 },
  358. { 0x42, 0x7f },
  359. { 0x63, 0xaa }, /* AWB - was e0 */
  360. { 0x64, 0xff },
  361. { 0x66, 0x00 },
  362. { 0x13, 0xf0 }, /* com8 */
  363. { 0x0d, 0x41 },
  364. { 0x0f, 0xc5 },
  365. { 0x14, 0x11 },
  366. { 0x22, 0x7f },
  367. { 0x23, 0x03 },
  368. { 0x24, 0x40 },
  369. { 0x25, 0x30 },
  370. { 0x26, 0xa1 },
  371. { 0x2a, 0x00 },
  372. { 0x2b, 0x00 },
  373. { 0x6b, 0xaa },
  374. { 0x13, 0xff }, /* AWB */
  375. { 0x90, 0x05 },
  376. { 0x91, 0x01 },
  377. { 0x92, 0x03 },
  378. { 0x93, 0x00 },
  379. { 0x94, 0x60 },
  380. { 0x95, 0x3c },
  381. { 0x96, 0x24 },
  382. { 0x97, 0x1e },
  383. { 0x98, 0x62 },
  384. { 0x99, 0x80 },
  385. { 0x9a, 0x1e },
  386. { 0x9b, 0x08 },
  387. { 0x9c, 0x20 },
  388. { 0x9e, 0x81 },
  389. { 0xa6, 0x04 },
  390. { 0x7e, 0x0c },
  391. { 0x7f, 0x16 },
  392. { 0x80, 0x2a },
  393. { 0x81, 0x4e },
  394. { 0x82, 0x61 },
  395. { 0x83, 0x6f },
  396. { 0x84, 0x7b },
  397. { 0x85, 0x86 },
  398. { 0x86, 0x8e },
  399. { 0x87, 0x97 },
  400. { 0x88, 0xa4 },
  401. { 0x89, 0xaf },
  402. { 0x8a, 0xc5 },
  403. { 0x8b, 0xd7 },
  404. { 0x8c, 0xe8 },
  405. { 0x8d, 0x20 },
  406. { 0x0c, 0x90 },
  407. { 0x2b, 0x00 },
  408. { 0x22, 0x7f },
  409. { 0x23, 0x03 },
  410. { 0x11, 0x01 },
  411. { 0x0c, 0xd0 },
  412. { 0x64, 0xff },
  413. { 0x0d, 0x41 },
  414. { 0x14, 0x41 },
  415. { 0x0e, 0xcd },
  416. { 0xac, 0xbf },
  417. { 0x8e, 0x00 }, /* De-noise threshold */
  418. { 0x0c, 0xd0 }
  419. };
  420. static const u8 bridge_start_vga[][2] = {
  421. {0x1c, 0x00},
  422. {0x1d, 0x40},
  423. {0x1d, 0x02},
  424. {0x1d, 0x00},
  425. {0x1d, 0x02},
  426. {0x1d, 0x58},
  427. {0x1d, 0x00},
  428. {0xc0, 0x50},
  429. {0xc1, 0x3c},
  430. };
  431. static const u8 sensor_start_vga[][2] = {
  432. {0x12, 0x00},
  433. {0x17, 0x26},
  434. {0x18, 0xa0},
  435. {0x19, 0x07},
  436. {0x1a, 0xf0},
  437. {0x29, 0xa0},
  438. {0x2c, 0xf0},
  439. {0x65, 0x20},
  440. };
  441. static const u8 bridge_start_qvga[][2] = {
  442. {0x1c, 0x00},
  443. {0x1d, 0x40},
  444. {0x1d, 0x02},
  445. {0x1d, 0x00},
  446. {0x1d, 0x01},
  447. {0x1d, 0x4b},
  448. {0x1d, 0x00},
  449. {0xc0, 0x28},
  450. {0xc1, 0x1e},
  451. };
  452. static const u8 sensor_start_qvga[][2] = {
  453. {0x12, 0x40},
  454. {0x17, 0x3f},
  455. {0x18, 0x50},
  456. {0x19, 0x03},
  457. {0x1a, 0x78},
  458. {0x29, 0x50},
  459. {0x2c, 0x78},
  460. {0x65, 0x2f},
  461. };
  462. static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
  463. {
  464. struct usb_device *udev = gspca_dev->dev;
  465. int ret;
  466. PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
  467. gspca_dev->usb_buf[0] = val;
  468. ret = usb_control_msg(udev,
  469. usb_sndctrlpipe(udev, 0),
  470. 0x01,
  471. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  472. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  473. if (ret < 0)
  474. PDEBUG(D_ERR, "write failed");
  475. }
  476. static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  477. {
  478. struct usb_device *udev = gspca_dev->dev;
  479. int ret;
  480. ret = usb_control_msg(udev,
  481. usb_rcvctrlpipe(udev, 0),
  482. 0x01,
  483. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  484. 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
  485. PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
  486. if (ret < 0)
  487. PDEBUG(D_ERR, "read failed");
  488. return gspca_dev->usb_buf[0];
  489. }
  490. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  491. * (direction and output)? */
  492. static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
  493. {
  494. u8 data;
  495. PDEBUG(D_CONF, "led status: %d", status);
  496. data = ov534_reg_read(gspca_dev, 0x21);
  497. data |= 0x80;
  498. ov534_reg_write(gspca_dev, 0x21, data);
  499. data = ov534_reg_read(gspca_dev, 0x23);
  500. if (status)
  501. data |= 0x80;
  502. else
  503. data &= ~0x80;
  504. ov534_reg_write(gspca_dev, 0x23, data);
  505. if (!status) {
  506. data = ov534_reg_read(gspca_dev, 0x21);
  507. data &= ~0x80;
  508. ov534_reg_write(gspca_dev, 0x21, data);
  509. }
  510. }
  511. static int sccb_check_status(struct gspca_dev *gspca_dev)
  512. {
  513. u8 data;
  514. int i;
  515. for (i = 0; i < 5; i++) {
  516. data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
  517. switch (data) {
  518. case 0x00:
  519. return 1;
  520. case 0x04:
  521. return 0;
  522. case 0x03:
  523. break;
  524. default:
  525. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
  526. data, i + 1);
  527. }
  528. }
  529. return 0;
  530. }
  531. static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
  532. {
  533. PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
  534. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  535. ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
  536. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  537. if (!sccb_check_status(gspca_dev))
  538. PDEBUG(D_ERR, "sccb_reg_write failed");
  539. }
  540. static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
  541. {
  542. ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
  543. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  544. if (!sccb_check_status(gspca_dev))
  545. PDEBUG(D_ERR, "sccb_reg_read failed 1");
  546. ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
  547. if (!sccb_check_status(gspca_dev))
  548. PDEBUG(D_ERR, "sccb_reg_read failed 2");
  549. return ov534_reg_read(gspca_dev, OV534_REG_READ);
  550. }
  551. /* output a bridge sequence (reg - val) */
  552. static void reg_w_array(struct gspca_dev *gspca_dev,
  553. const u8 (*data)[2], int len)
  554. {
  555. while (--len >= 0) {
  556. ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  557. data++;
  558. }
  559. }
  560. /* output a sensor sequence (reg - val) */
  561. static void sccb_w_array(struct gspca_dev *gspca_dev,
  562. const u8 (*data)[2], int len)
  563. {
  564. while (--len >= 0) {
  565. if ((*data)[0] != 0xff) {
  566. sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
  567. } else {
  568. sccb_reg_read(gspca_dev, (*data)[1]);
  569. sccb_reg_write(gspca_dev, 0xff, 0x00);
  570. }
  571. data++;
  572. }
  573. }
  574. /* ov772x specific controls */
  575. static void set_frame_rate(struct gspca_dev *gspca_dev)
  576. {
  577. struct sd *sd = (struct sd *) gspca_dev;
  578. int i;
  579. struct rate_s {
  580. u8 fps;
  581. u8 r11;
  582. u8 r0d;
  583. u8 re5;
  584. };
  585. const struct rate_s *r;
  586. static const struct rate_s rate_0[] = { /* 640x480 */
  587. {60, 0x01, 0xc1, 0x04},
  588. {50, 0x01, 0x41, 0x02},
  589. {40, 0x02, 0xc1, 0x04},
  590. {30, 0x04, 0x81, 0x02},
  591. {15, 0x03, 0x41, 0x04},
  592. };
  593. static const struct rate_s rate_1[] = { /* 320x240 */
  594. {125, 0x02, 0x81, 0x02},
  595. {100, 0x02, 0xc1, 0x04},
  596. {75, 0x03, 0xc1, 0x04},
  597. {60, 0x04, 0xc1, 0x04},
  598. {50, 0x02, 0x41, 0x04},
  599. {40, 0x03, 0x41, 0x04},
  600. {30, 0x04, 0x41, 0x04},
  601. };
  602. if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
  603. r = rate_0;
  604. i = ARRAY_SIZE(rate_0);
  605. } else {
  606. r = rate_1;
  607. i = ARRAY_SIZE(rate_1);
  608. }
  609. while (--i > 0) {
  610. if (sd->frame_rate >= r->fps)
  611. break;
  612. r++;
  613. }
  614. sccb_reg_write(gspca_dev, 0x11, r->r11);
  615. sccb_reg_write(gspca_dev, 0x0d, r->r0d);
  616. ov534_reg_write(gspca_dev, 0xe5, r->re5);
  617. PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
  618. }
  619. static void setbrightness(struct gspca_dev *gspca_dev)
  620. {
  621. struct sd *sd = (struct sd *) gspca_dev;
  622. sccb_reg_write(gspca_dev, 0x9B, sd->brightness);
  623. }
  624. static void setcontrast(struct gspca_dev *gspca_dev)
  625. {
  626. struct sd *sd = (struct sd *) gspca_dev;
  627. sccb_reg_write(gspca_dev, 0x9C, sd->contrast);
  628. }
  629. static void setgain(struct gspca_dev *gspca_dev)
  630. {
  631. struct sd *sd = (struct sd *) gspca_dev;
  632. u8 val;
  633. val = sd->gain;
  634. switch (val & 0x30) {
  635. case 0x00:
  636. val &= 0x0f;
  637. break;
  638. case 0x10:
  639. val &= 0x0f;
  640. val |= 0x30;
  641. break;
  642. case 0x20:
  643. val &= 0x0f;
  644. val |= 0x70;
  645. break;
  646. default:
  647. /* case 0x30: */
  648. val &= 0x0f;
  649. val |= 0xf0;
  650. break;
  651. }
  652. sccb_reg_write(gspca_dev, 0x00, val);
  653. }
  654. static void setexposure(struct gspca_dev *gspca_dev)
  655. {
  656. struct sd *sd = (struct sd *) gspca_dev;
  657. u8 val;
  658. val = sd->exposure;
  659. sccb_reg_write(gspca_dev, 0x08, val >> 7);
  660. sccb_reg_write(gspca_dev, 0x10, val << 1);
  661. }
  662. static void setredblc(struct gspca_dev *gspca_dev)
  663. {
  664. struct sd *sd = (struct sd *) gspca_dev;
  665. sccb_reg_write(gspca_dev, 0x43, sd->redblc);
  666. }
  667. static void setblueblc(struct gspca_dev *gspca_dev)
  668. {
  669. struct sd *sd = (struct sd *) gspca_dev;
  670. sccb_reg_write(gspca_dev, 0x42, sd->blueblc);
  671. }
  672. static void sethue(struct gspca_dev *gspca_dev)
  673. {
  674. struct sd *sd = (struct sd *) gspca_dev;
  675. sccb_reg_write(gspca_dev, 0x01, sd->hue);
  676. }
  677. static void setautogain(struct gspca_dev *gspca_dev)
  678. {
  679. struct sd *sd = (struct sd *) gspca_dev;
  680. if (sd->autogain) {
  681. sccb_reg_write(gspca_dev, 0x13, 0xf7); /* AGC,AEC,AWB ON */
  682. sccb_reg_write(gspca_dev, 0x64,
  683. sccb_reg_read(gspca_dev, 0x64) | 0x03);
  684. } else {
  685. sccb_reg_write(gspca_dev, 0x13, 0xf0); /* AGC,AEC,AWB OFF */
  686. sccb_reg_write(gspca_dev, 0x64,
  687. sccb_reg_read(gspca_dev, 0x64) & 0xfc);
  688. }
  689. }
  690. static void setawb(struct gspca_dev *gspca_dev)
  691. {
  692. struct sd *sd = (struct sd *) gspca_dev;
  693. if (sd->awb)
  694. sccb_reg_write(gspca_dev, 0x63, 0xe0); /* AWB on */
  695. else
  696. sccb_reg_write(gspca_dev, 0x63, 0xaa); /* AWB off */
  697. }
  698. static void setsharpness(struct gspca_dev *gspca_dev)
  699. {
  700. struct sd *sd = (struct sd *) gspca_dev;
  701. u8 val;
  702. val = sd->sharpness;
  703. sccb_reg_write(gspca_dev, 0x91, val); /* vga noise */
  704. sccb_reg_write(gspca_dev, 0x8e, val); /* qvga noise */
  705. }
  706. static void sethflip(struct gspca_dev *gspca_dev)
  707. {
  708. struct sd *sd = (struct sd *) gspca_dev;
  709. if (sd->hflip == 0)
  710. sccb_reg_write(gspca_dev, 0x0c,
  711. sccb_reg_read(gspca_dev, 0x0c) | 0x40);
  712. else
  713. sccb_reg_write(gspca_dev, 0x0c,
  714. sccb_reg_read(gspca_dev, 0x0c) & 0xbf);
  715. }
  716. static void setvflip(struct gspca_dev *gspca_dev)
  717. {
  718. struct sd *sd = (struct sd *) gspca_dev;
  719. if (sd->vflip == 0)
  720. sccb_reg_write(gspca_dev, 0x0c,
  721. sccb_reg_read(gspca_dev, 0x0c) | 0x80);
  722. else
  723. sccb_reg_write(gspca_dev, 0x0c,
  724. sccb_reg_read(gspca_dev, 0x0c) & 0x7f);
  725. }
  726. /* this function is called at probe time */
  727. static int sd_config(struct gspca_dev *gspca_dev,
  728. const struct usb_device_id *id)
  729. {
  730. struct sd *sd = (struct sd *) gspca_dev;
  731. struct cam *cam;
  732. cam = &gspca_dev->cam;
  733. cam->cam_mode = ov772x_mode;
  734. cam->nmodes = ARRAY_SIZE(ov772x_mode);
  735. cam->mode_framerates = ov772x_framerates;
  736. cam->bulk = 1;
  737. cam->bulk_size = 16384;
  738. cam->bulk_nurbs = 2;
  739. sd->frame_rate = 30;
  740. sd->brightness = BRIGHTNESS_DEF;
  741. sd->contrast = CONTRAST_DEF;
  742. sd->gain = GAIN_DEF;
  743. sd->exposure = EXPO_DEF;
  744. sd->redblc = RED_BALANCE_DEF;
  745. sd->blueblc = BLUE_BALANCE_DEF;
  746. sd->hue = HUE_DEF;
  747. #if AUTOGAIN_DEF != 0
  748. sd->autogain = AUTOGAIN_DEF;
  749. #else
  750. gspca_dev->ctrl_inac |= (1 << AWB_IDX);
  751. #endif
  752. #if AWB_DEF != 0
  753. sd->awb = AWB_DEF
  754. #endif
  755. #if SHARPNESS_DEF != 0
  756. sd->sharpness = SHARPNESS_DEF;
  757. #endif
  758. #if HFLIP_DEF != 0
  759. sd->hflip = HFLIP_DEF;
  760. #endif
  761. #if VFLIP_DEF != 0
  762. sd->vflip = VFLIP_DEF;
  763. #endif
  764. return 0;
  765. }
  766. /* this function is called at probe and resume time */
  767. static int sd_init(struct gspca_dev *gspca_dev)
  768. {
  769. u16 sensor_id;
  770. /* reset bridge */
  771. ov534_reg_write(gspca_dev, 0xe7, 0x3a);
  772. ov534_reg_write(gspca_dev, 0xe0, 0x08);
  773. msleep(100);
  774. /* initialize the sensor address */
  775. ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
  776. /* reset sensor */
  777. sccb_reg_write(gspca_dev, 0x12, 0x80);
  778. msleep(10);
  779. /* probe the sensor */
  780. sccb_reg_read(gspca_dev, 0x0a);
  781. sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
  782. sccb_reg_read(gspca_dev, 0x0b);
  783. sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
  784. PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
  785. /* initialize */
  786. reg_w_array(gspca_dev, bridge_init,
  787. ARRAY_SIZE(bridge_init));
  788. ov534_set_led(gspca_dev, 1);
  789. sccb_w_array(gspca_dev, sensor_init,
  790. ARRAY_SIZE(sensor_init));
  791. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  792. ov534_set_led(gspca_dev, 0);
  793. set_frame_rate(gspca_dev);
  794. return 0;
  795. }
  796. static int sd_start(struct gspca_dev *gspca_dev)
  797. {
  798. int mode;
  799. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  800. if (mode != 0) { /* 320x240 */
  801. reg_w_array(gspca_dev, bridge_start_qvga,
  802. ARRAY_SIZE(bridge_start_qvga));
  803. sccb_w_array(gspca_dev, sensor_start_qvga,
  804. ARRAY_SIZE(sensor_start_qvga));
  805. } else { /* 640x480 */
  806. reg_w_array(gspca_dev, bridge_start_vga,
  807. ARRAY_SIZE(bridge_start_vga));
  808. sccb_w_array(gspca_dev, sensor_start_vga,
  809. ARRAY_SIZE(sensor_start_vga));
  810. }
  811. set_frame_rate(gspca_dev);
  812. setautogain(gspca_dev);
  813. setawb(gspca_dev);
  814. setgain(gspca_dev);
  815. setredblc(gspca_dev);
  816. setblueblc(gspca_dev);
  817. sethue(gspca_dev);
  818. setexposure(gspca_dev);
  819. setbrightness(gspca_dev);
  820. setcontrast(gspca_dev);
  821. setsharpness(gspca_dev);
  822. setvflip(gspca_dev);
  823. sethflip(gspca_dev);
  824. ov534_set_led(gspca_dev, 1);
  825. ov534_reg_write(gspca_dev, 0xe0, 0x00);
  826. return 0;
  827. }
  828. static void sd_stopN(struct gspca_dev *gspca_dev)
  829. {
  830. ov534_reg_write(gspca_dev, 0xe0, 0x09);
  831. ov534_set_led(gspca_dev, 0);
  832. }
  833. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  834. #define UVC_STREAM_EOH (1 << 7)
  835. #define UVC_STREAM_ERR (1 << 6)
  836. #define UVC_STREAM_STI (1 << 5)
  837. #define UVC_STREAM_RES (1 << 4)
  838. #define UVC_STREAM_SCR (1 << 3)
  839. #define UVC_STREAM_PTS (1 << 2)
  840. #define UVC_STREAM_EOF (1 << 1)
  841. #define UVC_STREAM_FID (1 << 0)
  842. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  843. u8 *data, int len)
  844. {
  845. struct sd *sd = (struct sd *) gspca_dev;
  846. __u32 this_pts;
  847. u16 this_fid;
  848. int remaining_len = len;
  849. do {
  850. len = min(remaining_len, 2048);
  851. /* Payloads are prefixed with a UVC-style header. We
  852. consider a frame to start when the FID toggles, or the PTS
  853. changes. A frame ends when EOF is set, and we've received
  854. the correct number of bytes. */
  855. /* Verify UVC header. Header length is always 12 */
  856. if (data[0] != 12 || len < 12) {
  857. PDEBUG(D_PACK, "bad header");
  858. goto discard;
  859. }
  860. /* Check errors */
  861. if (data[1] & UVC_STREAM_ERR) {
  862. PDEBUG(D_PACK, "payload error");
  863. goto discard;
  864. }
  865. /* Extract PTS and FID */
  866. if (!(data[1] & UVC_STREAM_PTS)) {
  867. PDEBUG(D_PACK, "PTS not present");
  868. goto discard;
  869. }
  870. this_pts = (data[5] << 24) | (data[4] << 16)
  871. | (data[3] << 8) | data[2];
  872. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  873. /* If PTS or FID has changed, start a new frame. */
  874. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  875. if (gspca_dev->last_packet_type == INTER_PACKET)
  876. gspca_frame_add(gspca_dev, LAST_PACKET,
  877. NULL, 0);
  878. sd->last_pts = this_pts;
  879. sd->last_fid = this_fid;
  880. gspca_frame_add(gspca_dev, FIRST_PACKET,
  881. data + 12, len - 12);
  882. /* If this packet is marked as EOF, end the frame */
  883. } else if (data[1] & UVC_STREAM_EOF) {
  884. struct gspca_frame *frame;
  885. sd->last_pts = 0;
  886. frame = gspca_get_i_frame(gspca_dev);
  887. if (frame == NULL)
  888. goto discard;
  889. if (frame->data_end - frame->data + (len - 12) !=
  890. gspca_dev->width * gspca_dev->height * 2) {
  891. PDEBUG(D_PACK, "wrong sized frame");
  892. goto discard;
  893. }
  894. gspca_frame_add(gspca_dev, LAST_PACKET,
  895. data + 12, len - 12);
  896. } else {
  897. /* Add the data from this payload */
  898. gspca_frame_add(gspca_dev, INTER_PACKET,
  899. data + 12, len - 12);
  900. }
  901. /* Done this payload */
  902. goto scan_next;
  903. discard:
  904. /* Discard data until a new frame starts. */
  905. gspca_dev->last_packet_type = DISCARD_PACKET;
  906. scan_next:
  907. remaining_len -= len;
  908. data += len;
  909. } while (remaining_len > 0);
  910. }
  911. /* controls */
  912. static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
  913. {
  914. struct sd *sd = (struct sd *) gspca_dev;
  915. sd->gain = val;
  916. if (gspca_dev->streaming)
  917. setgain(gspca_dev);
  918. return 0;
  919. }
  920. static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
  921. {
  922. struct sd *sd = (struct sd *) gspca_dev;
  923. *val = sd->gain;
  924. return 0;
  925. }
  926. static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
  927. {
  928. struct sd *sd = (struct sd *) gspca_dev;
  929. sd->exposure = val;
  930. if (gspca_dev->streaming)
  931. setexposure(gspca_dev);
  932. return 0;
  933. }
  934. static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
  935. {
  936. struct sd *sd = (struct sd *) gspca_dev;
  937. *val = sd->exposure;
  938. return 0;
  939. }
  940. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  941. {
  942. struct sd *sd = (struct sd *) gspca_dev;
  943. sd->brightness = val;
  944. if (gspca_dev->streaming)
  945. setbrightness(gspca_dev);
  946. return 0;
  947. }
  948. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  949. {
  950. struct sd *sd = (struct sd *) gspca_dev;
  951. *val = sd->brightness;
  952. return 0;
  953. }
  954. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  955. {
  956. struct sd *sd = (struct sd *) gspca_dev;
  957. sd->contrast = val;
  958. if (gspca_dev->streaming)
  959. setcontrast(gspca_dev);
  960. return 0;
  961. }
  962. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  963. {
  964. struct sd *sd = (struct sd *) gspca_dev;
  965. *val = sd->contrast;
  966. return 0;
  967. }
  968. static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val)
  969. {
  970. struct sd *sd = (struct sd *) gspca_dev;
  971. sd->redblc = val;
  972. if (gspca_dev->streaming)
  973. setredblc(gspca_dev);
  974. return 0;
  975. }
  976. static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val)
  977. {
  978. struct sd *sd = (struct sd *) gspca_dev;
  979. *val = sd->redblc;
  980. return 0;
  981. }
  982. static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val)
  983. {
  984. struct sd *sd = (struct sd *) gspca_dev;
  985. sd->blueblc = val;
  986. if (gspca_dev->streaming)
  987. setblueblc(gspca_dev);
  988. return 0;
  989. }
  990. static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val)
  991. {
  992. struct sd *sd = (struct sd *) gspca_dev;
  993. *val = sd->blueblc;
  994. return 0;
  995. }
  996. static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
  997. {
  998. struct sd *sd = (struct sd *) gspca_dev;
  999. sd->hue = val;
  1000. if (gspca_dev->streaming)
  1001. sethue(gspca_dev);
  1002. return 0;
  1003. }
  1004. static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
  1005. {
  1006. struct sd *sd = (struct sd *) gspca_dev;
  1007. *val = sd->hue;
  1008. return 0;
  1009. }
  1010. static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
  1011. {
  1012. struct sd *sd = (struct sd *) gspca_dev;
  1013. sd->autogain = val;
  1014. if (gspca_dev->streaming) {
  1015. /* the auto white balance control works only
  1016. * when auto gain is set */
  1017. if (val)
  1018. gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
  1019. else
  1020. gspca_dev->ctrl_inac |= (1 << AWB_IDX);
  1021. setautogain(gspca_dev);
  1022. }
  1023. return 0;
  1024. }
  1025. static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
  1026. {
  1027. struct sd *sd = (struct sd *) gspca_dev;
  1028. *val = sd->autogain;
  1029. return 0;
  1030. }
  1031. static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
  1032. {
  1033. struct sd *sd = (struct sd *) gspca_dev;
  1034. sd->awb = val;
  1035. if (gspca_dev->streaming)
  1036. setawb(gspca_dev);
  1037. return 0;
  1038. }
  1039. static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
  1040. {
  1041. struct sd *sd = (struct sd *) gspca_dev;
  1042. *val = sd->awb;
  1043. return 0;
  1044. }
  1045. static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
  1046. {
  1047. struct sd *sd = (struct sd *) gspca_dev;
  1048. sd->sharpness = val;
  1049. if (gspca_dev->streaming)
  1050. setsharpness(gspca_dev);
  1051. return 0;
  1052. }
  1053. static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
  1054. {
  1055. struct sd *sd = (struct sd *) gspca_dev;
  1056. *val = sd->sharpness;
  1057. return 0;
  1058. }
  1059. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  1060. {
  1061. struct sd *sd = (struct sd *) gspca_dev;
  1062. sd->hflip = val;
  1063. if (gspca_dev->streaming)
  1064. sethflip(gspca_dev);
  1065. return 0;
  1066. }
  1067. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  1068. {
  1069. struct sd *sd = (struct sd *) gspca_dev;
  1070. *val = sd->hflip;
  1071. return 0;
  1072. }
  1073. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  1074. {
  1075. struct sd *sd = (struct sd *) gspca_dev;
  1076. sd->vflip = val;
  1077. if (gspca_dev->streaming)
  1078. setvflip(gspca_dev);
  1079. return 0;
  1080. }
  1081. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  1082. {
  1083. struct sd *sd = (struct sd *) gspca_dev;
  1084. *val = sd->vflip;
  1085. return 0;
  1086. }
  1087. /* get stream parameters (framerate) */
  1088. static int sd_get_streamparm(struct gspca_dev *gspca_dev,
  1089. struct v4l2_streamparm *parm)
  1090. {
  1091. struct v4l2_captureparm *cp = &parm->parm.capture;
  1092. struct v4l2_fract *tpf = &cp->timeperframe;
  1093. struct sd *sd = (struct sd *) gspca_dev;
  1094. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1095. return -EINVAL;
  1096. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  1097. tpf->numerator = 1;
  1098. tpf->denominator = sd->frame_rate;
  1099. return 0;
  1100. }
  1101. /* set stream parameters (framerate) */
  1102. static int sd_set_streamparm(struct gspca_dev *gspca_dev,
  1103. struct v4l2_streamparm *parm)
  1104. {
  1105. struct v4l2_captureparm *cp = &parm->parm.capture;
  1106. struct v4l2_fract *tpf = &cp->timeperframe;
  1107. struct sd *sd = (struct sd *) gspca_dev;
  1108. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1109. return -EINVAL;
  1110. /* Set requested framerate */
  1111. sd->frame_rate = tpf->denominator / tpf->numerator;
  1112. if (gspca_dev->streaming)
  1113. set_frame_rate(gspca_dev);
  1114. /* Return the actual framerate */
  1115. tpf->numerator = 1;
  1116. tpf->denominator = sd->frame_rate;
  1117. return 0;
  1118. }
  1119. /* sub-driver description */
  1120. static const struct sd_desc sd_desc = {
  1121. .name = MODULE_NAME,
  1122. .ctrls = sd_ctrls,
  1123. .nctrls = ARRAY_SIZE(sd_ctrls),
  1124. .config = sd_config,
  1125. .init = sd_init,
  1126. .start = sd_start,
  1127. .stopN = sd_stopN,
  1128. .pkt_scan = sd_pkt_scan,
  1129. .get_streamparm = sd_get_streamparm,
  1130. .set_streamparm = sd_set_streamparm,
  1131. };
  1132. /* -- module initialisation -- */
  1133. static const __devinitdata struct usb_device_id device_table[] = {
  1134. {USB_DEVICE(0x1415, 0x2000)},
  1135. {}
  1136. };
  1137. MODULE_DEVICE_TABLE(usb, device_table);
  1138. /* -- device connect -- */
  1139. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1140. {
  1141. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  1142. THIS_MODULE);
  1143. }
  1144. static struct usb_driver sd_driver = {
  1145. .name = MODULE_NAME,
  1146. .id_table = device_table,
  1147. .probe = sd_probe,
  1148. .disconnect = gspca_disconnect,
  1149. #ifdef CONFIG_PM
  1150. .suspend = gspca_suspend,
  1151. .resume = gspca_resume,
  1152. #endif
  1153. };
  1154. /* -- module insert / remove -- */
  1155. static int __init sd_mod_init(void)
  1156. {
  1157. int ret;
  1158. ret = usb_register(&sd_driver);
  1159. if (ret < 0)
  1160. return ret;
  1161. PDEBUG(D_PROBE, "registered");
  1162. return 0;
  1163. }
  1164. static void __exit sd_mod_exit(void)
  1165. {
  1166. usb_deregister(&sd_driver);
  1167. PDEBUG(D_PROBE, "deregistered");
  1168. }
  1169. module_init(sd_mod_init);
  1170. module_exit(sd_mod_exit);