ov534.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * ov534/ov772x gspca driver
  3. * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
  4. *
  5. * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
  6. * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
  7. * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #define MODULE_NAME "ov534"
  24. #include "gspca.h"
  25. #define OV534_REG_ADDRESS 0xf1 /* ? */
  26. #define OV534_REG_SUBADDR 0xf2
  27. #define OV534_REG_WRITE 0xf3
  28. #define OV534_REG_READ 0xf4
  29. #define OV534_REG_OPERATION 0xf5
  30. #define OV534_REG_STATUS 0xf6
  31. #define OV534_OP_WRITE_3 0x37
  32. #define OV534_OP_WRITE_2 0x33
  33. #define OV534_OP_READ_2 0xf9
  34. #define CTRL_TIMEOUT 500
  35. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  36. MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
  37. MODULE_LICENSE("GPL");
  38. /* global parameters */
  39. static int frame_rate;
  40. /* specific webcam descriptor */
  41. struct sd {
  42. struct gspca_dev gspca_dev; /* !! must be the first item */
  43. };
  44. /* V4L2 controls supported by the driver */
  45. static struct ctrl sd_ctrls[] = {
  46. };
  47. static struct v4l2_pix_format vga_mode[] = {
  48. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  49. .bytesperline = 640 * 2,
  50. .sizeimage = 640 * 480 * 2,
  51. .colorspace = V4L2_COLORSPACE_JPEG,
  52. .priv = 0},
  53. };
  54. static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
  55. {
  56. u8 data = val;
  57. int ret;
  58. PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
  59. ret = usb_control_msg(udev,
  60. usb_sndctrlpipe(udev, 0),
  61. 0x1,
  62. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  63. 0x0, reg, &data, 1, CTRL_TIMEOUT);
  64. if (ret < 0)
  65. PDEBUG(D_ERR, "write failed");
  66. }
  67. static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
  68. {
  69. u8 data;
  70. int ret;
  71. ret = usb_control_msg(udev,
  72. usb_rcvctrlpipe(udev, 0),
  73. 0x1,
  74. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  75. 0x0, reg, &data, 1, CTRL_TIMEOUT);
  76. PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
  77. if (ret < 0)
  78. PDEBUG(D_ERR, "read failed");
  79. return data;
  80. }
  81. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  82. * (direction and output)? */
  83. static void ov534_set_led(struct usb_device *udev, int status)
  84. {
  85. u8 data;
  86. PDEBUG(D_CONF, "led status: %d", status);
  87. data = ov534_reg_read(udev, 0x21);
  88. data |= 0x80;
  89. ov534_reg_write(udev, 0x21, data);
  90. data = ov534_reg_read(udev, 0x23);
  91. if (status)
  92. data |= 0x80;
  93. else
  94. data &= ~(0x80);
  95. ov534_reg_write(udev, 0x23, data);
  96. }
  97. static int sccb_check_status(struct usb_device *udev)
  98. {
  99. u8 data;
  100. int i;
  101. for (i = 0; i < 5; i++) {
  102. data = ov534_reg_read(udev, OV534_REG_STATUS);
  103. switch (data) {
  104. case 0x00:
  105. return 1;
  106. case 0x04:
  107. return 0;
  108. case 0x03:
  109. break;
  110. default:
  111. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
  112. data, i + 1);
  113. }
  114. }
  115. return 0;
  116. }
  117. static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
  118. {
  119. PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
  120. ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
  121. ov534_reg_write(udev, OV534_REG_WRITE, val);
  122. ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  123. if (!sccb_check_status(udev))
  124. PDEBUG(D_ERR, "sccb_reg_write failed");
  125. }
  126. static const __u8 ov534_reg_initdata[][2] = {
  127. { 0xe7, 0x3a },
  128. { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
  129. { 0xc2, 0x0c },
  130. { 0x88, 0xf8 },
  131. { 0xc3, 0x69 },
  132. { 0x89, 0xff },
  133. { 0x76, 0x03 },
  134. { 0x92, 0x01 },
  135. { 0x93, 0x18 },
  136. { 0x94, 0x10 },
  137. { 0x95, 0x10 },
  138. { 0xe2, 0x00 },
  139. { 0xe7, 0x3e },
  140. { 0x96, 0x00 },
  141. { 0x97, 0x20 },
  142. { 0x97, 0x20 },
  143. { 0x97, 0x20 },
  144. { 0x97, 0x0a },
  145. { 0x97, 0x3f },
  146. { 0x97, 0x4a },
  147. { 0x97, 0x20 },
  148. { 0x97, 0x15 },
  149. { 0x97, 0x0b },
  150. { 0x8e, 0x40 },
  151. { 0x1f, 0x81 },
  152. { 0x34, 0x05 },
  153. { 0xe3, 0x04 },
  154. { 0x88, 0x00 },
  155. { 0x89, 0x00 },
  156. { 0x76, 0x00 },
  157. { 0xe7, 0x2e },
  158. { 0x31, 0xf9 },
  159. { 0x25, 0x42 },
  160. { 0x21, 0xf0 },
  161. { 0x1c, 0x00 },
  162. { 0x1d, 0x40 },
  163. { 0x1d, 0x02 },
  164. { 0x1d, 0x00 },
  165. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  166. { 0x1d, 0x58 }, /* frame size */
  167. { 0x1d, 0x00 }, /* frame size */
  168. { 0x8d, 0x1c },
  169. { 0x8e, 0x80 },
  170. { 0xe5, 0x04 },
  171. { 0xc0, 0x50 },
  172. { 0xc1, 0x3c },
  173. { 0xc2, 0x0c },
  174. };
  175. static const __u8 ov772x_reg_initdata[][2] = {
  176. { 0x12, 0x80 },
  177. { 0x11, 0x01 },
  178. { 0x3d, 0x03 },
  179. { 0x17, 0x26 },
  180. { 0x18, 0xa0 },
  181. { 0x19, 0x07 },
  182. { 0x1a, 0xf0 },
  183. { 0x32, 0x00 },
  184. { 0x29, 0xa0 },
  185. { 0x2c, 0xf0 },
  186. { 0x65, 0x20 },
  187. { 0x11, 0x01 },
  188. { 0x42, 0x7f },
  189. { 0x63, 0xe0 },
  190. { 0x64, 0xff },
  191. { 0x66, 0x00 },
  192. { 0x13, 0xf0 },
  193. { 0x0d, 0x41 },
  194. { 0x0f, 0xc5 },
  195. { 0x14, 0x11 },
  196. { 0x22, 0x7f },
  197. { 0x23, 0x03 },
  198. { 0x24, 0x40 },
  199. { 0x25, 0x30 },
  200. { 0x26, 0xa1 },
  201. { 0x2a, 0x00 },
  202. { 0x2b, 0x00 },
  203. { 0x6b, 0xaa },
  204. { 0x13, 0xff },
  205. { 0x90, 0x05 },
  206. { 0x91, 0x01 },
  207. { 0x92, 0x03 },
  208. { 0x93, 0x00 },
  209. { 0x94, 0x60 },
  210. { 0x95, 0x3c },
  211. { 0x96, 0x24 },
  212. { 0x97, 0x1e },
  213. { 0x98, 0x62 },
  214. { 0x99, 0x80 },
  215. { 0x9a, 0x1e },
  216. { 0x9b, 0x08 },
  217. { 0x9c, 0x20 },
  218. { 0x9e, 0x81 },
  219. { 0xa6, 0x04 },
  220. { 0x7e, 0x0c },
  221. { 0x7f, 0x16 },
  222. { 0x80, 0x2a },
  223. { 0x81, 0x4e },
  224. { 0x82, 0x61 },
  225. { 0x83, 0x6f },
  226. { 0x84, 0x7b },
  227. { 0x85, 0x86 },
  228. { 0x86, 0x8e },
  229. { 0x87, 0x97 },
  230. { 0x88, 0xa4 },
  231. { 0x89, 0xaf },
  232. { 0x8a, 0xc5 },
  233. { 0x8b, 0xd7 },
  234. { 0x8c, 0xe8 },
  235. { 0x8d, 0x20 },
  236. { 0x0c, 0x90 },
  237. { 0x2b, 0x00 },
  238. { 0x22, 0x7f },
  239. { 0x23, 0x03 },
  240. { 0x11, 0x01 },
  241. { 0x0c, 0xd0 },
  242. { 0x64, 0xff },
  243. { 0x0d, 0x41 },
  244. { 0x14, 0x41 },
  245. { 0x0e, 0xcd },
  246. { 0xac, 0xbf },
  247. { 0x8e, 0x00 },
  248. { 0x0c, 0xd0 }
  249. };
  250. /* setup method */
  251. static void ov534_setup(struct usb_device *udev)
  252. {
  253. int i;
  254. /* Initialize bridge chip */
  255. for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
  256. ov534_reg_write(udev, ov534_reg_initdata[i][0],
  257. ov534_reg_initdata[i][1]);
  258. ov534_set_led(udev, 1);
  259. /* Initialize sensor */
  260. for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
  261. sccb_reg_write(udev, ov772x_reg_initdata[i][0],
  262. ov772x_reg_initdata[i][1]);
  263. ov534_reg_write(udev, 0xe0, 0x09);
  264. ov534_set_led(udev, 0);
  265. }
  266. /* this function is called at probe time */
  267. static int sd_config(struct gspca_dev *gspca_dev,
  268. const struct usb_device_id *id)
  269. {
  270. struct cam *cam;
  271. cam = &gspca_dev->cam;
  272. cam->epaddr = 0x01;
  273. cam->cam_mode = vga_mode;
  274. cam->nmodes = ARRAY_SIZE(vga_mode);
  275. cam->bulk_size = 2048;
  276. cam->bulk_nurbs = 2;
  277. return 0;
  278. }
  279. /* this function is called at probe and resume time */
  280. static int sd_init(struct gspca_dev *gspca_dev)
  281. {
  282. int fr;
  283. ov534_setup(gspca_dev->dev);
  284. fr = frame_rate;
  285. switch (fr) {
  286. case 50:
  287. sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
  288. sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
  289. ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
  290. break;
  291. case 40:
  292. sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
  293. sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
  294. ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
  295. break;
  296. /* case 30: */
  297. default:
  298. fr = 30;
  299. sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
  300. sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
  301. ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
  302. break;
  303. case 15:
  304. sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
  305. sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
  306. ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
  307. break;
  308. }
  309. PDEBUG(D_PROBE, "frame_rate: %d", fr);
  310. return 0;
  311. }
  312. static int sd_start(struct gspca_dev *gspca_dev)
  313. {
  314. /* start streaming data */
  315. ov534_set_led(gspca_dev->dev, 1);
  316. ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
  317. return 0;
  318. }
  319. static void sd_stopN(struct gspca_dev *gspca_dev)
  320. {
  321. /* stop streaming data */
  322. ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
  323. ov534_set_led(gspca_dev->dev, 0);
  324. }
  325. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  326. #define UVC_STREAM_EOH (1 << 7)
  327. #define UVC_STREAM_ERR (1 << 6)
  328. #define UVC_STREAM_STI (1 << 5)
  329. #define UVC_STREAM_RES (1 << 4)
  330. #define UVC_STREAM_SCR (1 << 3)
  331. #define UVC_STREAM_PTS (1 << 2)
  332. #define UVC_STREAM_EOF (1 << 1)
  333. #define UVC_STREAM_FID (1 << 0)
  334. static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
  335. __u8 *data, int len)
  336. {
  337. static __u32 last_pts;
  338. __u32 this_pts;
  339. static int last_fid;
  340. int this_fid;
  341. /* Payloads are prefixed with a the UVC-style header. We
  342. consider a frame to start when the FID toggles, or the PTS
  343. changes. A frame ends when EOF is set, and we've received
  344. the correct number of bytes. */
  345. /* Verify UVC header. Header length is always 12 */
  346. if (data[0] != 12 || len < 12) {
  347. PDEBUG(D_PACK, "bad header");
  348. goto discard;
  349. }
  350. /* Check errors */
  351. if (data[1] & UVC_STREAM_ERR) {
  352. PDEBUG(D_PACK, "payload error");
  353. goto discard;
  354. }
  355. /* Extract PTS and FID */
  356. if (!(data[1] & UVC_STREAM_PTS)) {
  357. PDEBUG(D_PACK, "PTS not present");
  358. goto discard;
  359. }
  360. this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
  361. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  362. /* If PTS or FID has changed, start a new frame. */
  363. if (this_pts != last_pts || this_fid != last_fid) {
  364. gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
  365. last_pts = this_pts;
  366. last_fid = this_fid;
  367. }
  368. /* Add the data from this payload */
  369. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  370. data + 12, len - 12);
  371. /* If this packet is marked as EOF, end the frame */
  372. if (data[1] & UVC_STREAM_EOF) {
  373. last_pts = 0;
  374. if ((frame->data_end - frame->data) !=
  375. (gspca_dev->width * gspca_dev->height * 2)) {
  376. PDEBUG(D_PACK, "short frame");
  377. goto discard;
  378. }
  379. gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
  380. }
  381. /* Done */
  382. return;
  383. discard:
  384. /* Discard data until a new frame starts. */
  385. gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
  386. }
  387. /* sub-driver description */
  388. static const struct sd_desc sd_desc = {
  389. .name = MODULE_NAME,
  390. .ctrls = sd_ctrls,
  391. .nctrls = ARRAY_SIZE(sd_ctrls),
  392. .config = sd_config,
  393. .init = sd_init,
  394. .start = sd_start,
  395. .stopN = sd_stopN,
  396. .pkt_scan = sd_pkt_scan,
  397. };
  398. /* -- module initialisation -- */
  399. static const __devinitdata struct usb_device_id device_table[] = {
  400. {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
  401. {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
  402. {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
  403. {}
  404. };
  405. MODULE_DEVICE_TABLE(usb, device_table);
  406. /* -- device connect -- */
  407. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  408. {
  409. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  410. THIS_MODULE);
  411. }
  412. static struct usb_driver sd_driver = {
  413. .name = MODULE_NAME,
  414. .id_table = device_table,
  415. .probe = sd_probe,
  416. .disconnect = gspca_disconnect,
  417. #ifdef CONFIG_PM
  418. .suspend = gspca_suspend,
  419. .resume = gspca_resume,
  420. #endif
  421. };
  422. /* -- module insert / remove -- */
  423. static int __init sd_mod_init(void)
  424. {
  425. if (usb_register(&sd_driver) < 0)
  426. return -1;
  427. PDEBUG(D_PROBE, "registered");
  428. return 0;
  429. }
  430. static void __exit sd_mod_exit(void)
  431. {
  432. usb_deregister(&sd_driver);
  433. PDEBUG(D_PROBE, "deregistered");
  434. }
  435. module_init(sd_mod_init);
  436. module_exit(sd_mod_exit);
  437. module_param(frame_rate, int, 0644);
  438. MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");