ov534.c 12 KB

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