ov534.c 27 KB

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