ov534.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /* specific webcam descriptor */
  40. struct sd {
  41. struct gspca_dev gspca_dev; /* !! must be the first item */
  42. __u32 last_fid;
  43. __u32 last_pts;
  44. int frame_rate;
  45. };
  46. /* V4L2 controls supported by the driver */
  47. static struct ctrl sd_ctrls[] = {
  48. };
  49. static struct v4l2_pix_format vga_mode[] = {
  50. {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
  51. .bytesperline = 640 * 2,
  52. .sizeimage = 640 * 480 * 2,
  53. .colorspace = V4L2_COLORSPACE_JPEG,
  54. .priv = 0},
  55. };
  56. static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
  57. {
  58. u8 data = val;
  59. int ret;
  60. PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
  61. ret = usb_control_msg(udev,
  62. usb_sndctrlpipe(udev, 0),
  63. 0x1,
  64. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  65. 0x0, reg, &data, 1, CTRL_TIMEOUT);
  66. if (ret < 0)
  67. PDEBUG(D_ERR, "write failed");
  68. }
  69. static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
  70. {
  71. u8 data;
  72. int ret;
  73. ret = usb_control_msg(udev,
  74. usb_rcvctrlpipe(udev, 0),
  75. 0x1,
  76. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  77. 0x0, reg, &data, 1, CTRL_TIMEOUT);
  78. PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
  79. if (ret < 0)
  80. PDEBUG(D_ERR, "read failed");
  81. return data;
  82. }
  83. /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
  84. * (direction and output)? */
  85. static void ov534_set_led(struct usb_device *udev, int status)
  86. {
  87. u8 data;
  88. PDEBUG(D_CONF, "led status: %d", status);
  89. data = ov534_reg_read(udev, 0x21);
  90. data |= 0x80;
  91. ov534_reg_write(udev, 0x21, data);
  92. data = ov534_reg_read(udev, 0x23);
  93. if (status)
  94. data |= 0x80;
  95. else
  96. data &= ~(0x80);
  97. ov534_reg_write(udev, 0x23, data);
  98. }
  99. static int sccb_check_status(struct usb_device *udev)
  100. {
  101. u8 data;
  102. int i;
  103. for (i = 0; i < 5; i++) {
  104. data = ov534_reg_read(udev, OV534_REG_STATUS);
  105. switch (data) {
  106. case 0x00:
  107. return 1;
  108. case 0x04:
  109. return 0;
  110. case 0x03:
  111. break;
  112. default:
  113. PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
  114. data, i + 1);
  115. }
  116. }
  117. return 0;
  118. }
  119. static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
  120. {
  121. PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
  122. ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
  123. ov534_reg_write(udev, OV534_REG_WRITE, val);
  124. ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
  125. if (!sccb_check_status(udev))
  126. PDEBUG(D_ERR, "sccb_reg_write failed");
  127. }
  128. #ifdef GSPCA_DEBUG
  129. static u8 sccb_reg_read(struct usb_device *udev, u16 reg)
  130. {
  131. ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
  132. ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
  133. if (!sccb_check_status(udev))
  134. PDEBUG(D_ERR, "sccb_reg_read failed 1");
  135. ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_READ_2);
  136. if (!sccb_check_status(udev))
  137. PDEBUG(D_ERR, "sccb_reg_read failed 2");
  138. return ov534_reg_read(udev, OV534_REG_READ);
  139. }
  140. #endif
  141. static const __u8 ov534_reg_initdata[][2] = {
  142. { 0xe7, 0x3a },
  143. { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
  144. { 0xc2, 0x0c },
  145. { 0x88, 0xf8 },
  146. { 0xc3, 0x69 },
  147. { 0x89, 0xff },
  148. { 0x76, 0x03 },
  149. { 0x92, 0x01 },
  150. { 0x93, 0x18 },
  151. { 0x94, 0x10 },
  152. { 0x95, 0x10 },
  153. { 0xe2, 0x00 },
  154. { 0xe7, 0x3e },
  155. { 0x96, 0x00 },
  156. { 0x97, 0x20 },
  157. { 0x97, 0x20 },
  158. { 0x97, 0x20 },
  159. { 0x97, 0x0a },
  160. { 0x97, 0x3f },
  161. { 0x97, 0x4a },
  162. { 0x97, 0x20 },
  163. { 0x97, 0x15 },
  164. { 0x97, 0x0b },
  165. { 0x8e, 0x40 },
  166. { 0x1f, 0x81 },
  167. { 0x34, 0x05 },
  168. { 0xe3, 0x04 },
  169. { 0x88, 0x00 },
  170. { 0x89, 0x00 },
  171. { 0x76, 0x00 },
  172. { 0xe7, 0x2e },
  173. { 0x31, 0xf9 },
  174. { 0x25, 0x42 },
  175. { 0x21, 0xf0 },
  176. { 0x1c, 0x00 },
  177. { 0x1d, 0x40 },
  178. { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
  179. { 0x1d, 0x00 }, /* payload size */
  180. { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
  181. { 0x1d, 0x58 }, /* frame size */
  182. { 0x1d, 0x00 }, /* frame size */
  183. { 0x1c, 0x0a },
  184. { 0x1d, 0x08 }, /* turn on UVC header */
  185. { 0x1d, 0x0e }, /* .. */
  186. { 0x8d, 0x1c },
  187. { 0x8e, 0x80 },
  188. { 0xe5, 0x04 },
  189. { 0xc0, 0x50 },
  190. { 0xc1, 0x3c },
  191. { 0xc2, 0x0c },
  192. };
  193. static const __u8 ov772x_reg_initdata[][2] = {
  194. { 0x12, 0x80 },
  195. { 0x11, 0x01 },
  196. { 0x3d, 0x03 },
  197. { 0x17, 0x26 },
  198. { 0x18, 0xa0 },
  199. { 0x19, 0x07 },
  200. { 0x1a, 0xf0 },
  201. { 0x32, 0x00 },
  202. { 0x29, 0xa0 },
  203. { 0x2c, 0xf0 },
  204. { 0x65, 0x20 },
  205. { 0x11, 0x01 },
  206. { 0x42, 0x7f },
  207. { 0x63, 0xe0 },
  208. { 0x64, 0xff },
  209. { 0x66, 0x00 },
  210. { 0x13, 0xf0 },
  211. { 0x0d, 0x41 },
  212. { 0x0f, 0xc5 },
  213. { 0x14, 0x11 },
  214. { 0x22, 0x7f },
  215. { 0x23, 0x03 },
  216. { 0x24, 0x40 },
  217. { 0x25, 0x30 },
  218. { 0x26, 0xa1 },
  219. { 0x2a, 0x00 },
  220. { 0x2b, 0x00 },
  221. { 0x6b, 0xaa },
  222. { 0x13, 0xff },
  223. { 0x90, 0x05 },
  224. { 0x91, 0x01 },
  225. { 0x92, 0x03 },
  226. { 0x93, 0x00 },
  227. { 0x94, 0x60 },
  228. { 0x95, 0x3c },
  229. { 0x96, 0x24 },
  230. { 0x97, 0x1e },
  231. { 0x98, 0x62 },
  232. { 0x99, 0x80 },
  233. { 0x9a, 0x1e },
  234. { 0x9b, 0x08 },
  235. { 0x9c, 0x20 },
  236. { 0x9e, 0x81 },
  237. { 0xa6, 0x04 },
  238. { 0x7e, 0x0c },
  239. { 0x7f, 0x16 },
  240. { 0x80, 0x2a },
  241. { 0x81, 0x4e },
  242. { 0x82, 0x61 },
  243. { 0x83, 0x6f },
  244. { 0x84, 0x7b },
  245. { 0x85, 0x86 },
  246. { 0x86, 0x8e },
  247. { 0x87, 0x97 },
  248. { 0x88, 0xa4 },
  249. { 0x89, 0xaf },
  250. { 0x8a, 0xc5 },
  251. { 0x8b, 0xd7 },
  252. { 0x8c, 0xe8 },
  253. { 0x8d, 0x20 },
  254. { 0x0c, 0x90 },
  255. { 0x2b, 0x00 },
  256. { 0x22, 0x7f },
  257. { 0x23, 0x03 },
  258. { 0x11, 0x01 },
  259. { 0x0c, 0xd0 },
  260. { 0x64, 0xff },
  261. { 0x0d, 0x41 },
  262. { 0x14, 0x41 },
  263. { 0x0e, 0xcd },
  264. { 0xac, 0xbf },
  265. { 0x8e, 0x00 },
  266. { 0x0c, 0xd0 }
  267. };
  268. /* set framerate */
  269. static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
  270. {
  271. struct sd *sd = (struct sd *) gspca_dev;
  272. int fr = sd->frame_rate;
  273. switch (fr) {
  274. case 50:
  275. sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
  276. sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
  277. ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
  278. break;
  279. case 40:
  280. sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
  281. sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
  282. ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
  283. break;
  284. /* case 30: */
  285. default:
  286. fr = 30;
  287. sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
  288. sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
  289. ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
  290. break;
  291. case 15:
  292. sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
  293. sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
  294. ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
  295. break;
  296. }
  297. sd->frame_rate = fr;
  298. PDEBUG(D_PROBE, "frame_rate: %d", fr);
  299. }
  300. /* setup method */
  301. static void ov534_setup(struct usb_device *udev)
  302. {
  303. int i;
  304. /* Initialize bridge chip */
  305. for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
  306. ov534_reg_write(udev, ov534_reg_initdata[i][0],
  307. ov534_reg_initdata[i][1]);
  308. PDEBUG(D_PROBE, "sensor is ov%02x%02x",
  309. sccb_reg_read(udev, 0x0a), sccb_reg_read(udev, 0x0b));
  310. ov534_set_led(udev, 1);
  311. /* Initialize sensor */
  312. for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
  313. sccb_reg_write(udev, ov772x_reg_initdata[i][0],
  314. ov772x_reg_initdata[i][1]);
  315. ov534_reg_write(udev, 0xe0, 0x09);
  316. ov534_set_led(udev, 0);
  317. }
  318. /* this function is called at probe time */
  319. static int sd_config(struct gspca_dev *gspca_dev,
  320. const struct usb_device_id *id)
  321. {
  322. struct cam *cam;
  323. cam = &gspca_dev->cam;
  324. cam->epaddr = 0x01;
  325. cam->cam_mode = vga_mode;
  326. cam->nmodes = ARRAY_SIZE(vga_mode);
  327. cam->bulk_size = 16384;
  328. cam->bulk_nurbs = 2;
  329. return 0;
  330. }
  331. /* this function is called at probe and resume time */
  332. static int sd_init(struct gspca_dev *gspca_dev)
  333. {
  334. ov534_setup(gspca_dev->dev);
  335. ov534_set_frame_rate(gspca_dev);
  336. return 0;
  337. }
  338. static int sd_start(struct gspca_dev *gspca_dev)
  339. {
  340. /* start streaming data */
  341. ov534_set_led(gspca_dev->dev, 1);
  342. ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
  343. return 0;
  344. }
  345. static void sd_stopN(struct gspca_dev *gspca_dev)
  346. {
  347. /* stop streaming data */
  348. ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
  349. ov534_set_led(gspca_dev->dev, 0);
  350. }
  351. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  352. #define UVC_STREAM_EOH (1 << 7)
  353. #define UVC_STREAM_ERR (1 << 6)
  354. #define UVC_STREAM_STI (1 << 5)
  355. #define UVC_STREAM_RES (1 << 4)
  356. #define UVC_STREAM_SCR (1 << 3)
  357. #define UVC_STREAM_PTS (1 << 2)
  358. #define UVC_STREAM_EOF (1 << 1)
  359. #define UVC_STREAM_FID (1 << 0)
  360. static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
  361. __u8 *data, int len)
  362. {
  363. struct sd *sd = (struct sd *) gspca_dev;
  364. __u32 this_pts;
  365. int this_fid;
  366. int remaining_len = len;
  367. __u8 *next_data = data;
  368. scan_next:
  369. if (remaining_len <= 0)
  370. return;
  371. data = next_data;
  372. len = min(remaining_len, 2048);
  373. remaining_len -= len;
  374. next_data += len;
  375. /* Payloads are prefixed with a UVC-style header. We
  376. consider a frame to start when the FID toggles, or the PTS
  377. changes. A frame ends when EOF is set, and we've received
  378. the correct number of bytes. */
  379. /* Verify UVC header. Header length is always 12 */
  380. if (data[0] != 12 || len < 12) {
  381. PDEBUG(D_PACK, "bad header");
  382. goto discard;
  383. }
  384. /* Check errors */
  385. if (data[1] & UVC_STREAM_ERR) {
  386. PDEBUG(D_PACK, "payload error");
  387. goto discard;
  388. }
  389. /* Extract PTS and FID */
  390. if (!(data[1] & UVC_STREAM_PTS)) {
  391. PDEBUG(D_PACK, "PTS not present");
  392. goto discard;
  393. }
  394. this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
  395. this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
  396. /* If PTS or FID has changed, start a new frame. */
  397. if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
  398. gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
  399. sd->last_pts = this_pts;
  400. sd->last_fid = this_fid;
  401. }
  402. /* Add the data from this payload */
  403. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  404. data + 12, len - 12);
  405. /* If this packet is marked as EOF, end the frame */
  406. if (data[1] & UVC_STREAM_EOF) {
  407. sd->last_pts = 0;
  408. if ((frame->data_end - frame->data) !=
  409. (gspca_dev->width * gspca_dev->height * 2)) {
  410. PDEBUG(D_PACK, "short frame");
  411. goto discard;
  412. }
  413. gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
  414. }
  415. /* Done this payload */
  416. goto scan_next;
  417. discard:
  418. /* Discard data until a new frame starts. */
  419. gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
  420. goto scan_next;
  421. }
  422. /* get stream parameters (framerate) */
  423. int sd_get_streamparm(struct gspca_dev *gspca_dev,
  424. struct v4l2_streamparm *parm)
  425. {
  426. struct v4l2_captureparm *cp = &parm->parm.capture;
  427. struct v4l2_fract *tpf = &cp->timeperframe;
  428. struct sd *sd = (struct sd *) gspca_dev;
  429. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  430. return -EINVAL;
  431. cp->capability |= V4L2_CAP_TIMEPERFRAME;
  432. tpf->numerator = 1;
  433. tpf->denominator = sd->frame_rate;
  434. return 0;
  435. }
  436. /* set stream parameters (framerate) */
  437. int sd_set_streamparm(struct gspca_dev *gspca_dev,
  438. struct v4l2_streamparm *parm)
  439. {
  440. struct v4l2_captureparm *cp = &parm->parm.capture;
  441. struct v4l2_fract *tpf = &cp->timeperframe;
  442. struct sd *sd = (struct sd *) gspca_dev;
  443. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  444. return -EINVAL;
  445. /* Set requested framerate */
  446. sd->frame_rate = tpf->denominator / tpf->numerator;
  447. ov534_set_frame_rate(gspca_dev);
  448. /* Return the actual framerate */
  449. tpf->numerator = 1;
  450. tpf->denominator = sd->frame_rate;
  451. return 0;
  452. }
  453. /* sub-driver description */
  454. static const struct sd_desc sd_desc = {
  455. .name = MODULE_NAME,
  456. .ctrls = sd_ctrls,
  457. .nctrls = ARRAY_SIZE(sd_ctrls),
  458. .config = sd_config,
  459. .init = sd_init,
  460. .start = sd_start,
  461. .stopN = sd_stopN,
  462. .pkt_scan = sd_pkt_scan,
  463. .get_streamparm = sd_get_streamparm,
  464. .set_streamparm = sd_set_streamparm,
  465. };
  466. /* -- module initialisation -- */
  467. static const __devinitdata struct usb_device_id device_table[] = {
  468. {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
  469. {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
  470. {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
  471. {}
  472. };
  473. MODULE_DEVICE_TABLE(usb, device_table);
  474. /* -- device connect -- */
  475. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  476. {
  477. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  478. THIS_MODULE);
  479. }
  480. static struct usb_driver sd_driver = {
  481. .name = MODULE_NAME,
  482. .id_table = device_table,
  483. .probe = sd_probe,
  484. .disconnect = gspca_disconnect,
  485. #ifdef CONFIG_PM
  486. .suspend = gspca_suspend,
  487. .resume = gspca_resume,
  488. #endif
  489. };
  490. /* -- module insert / remove -- */
  491. static int __init sd_mod_init(void)
  492. {
  493. if (usb_register(&sd_driver) < 0)
  494. return -1;
  495. PDEBUG(D_PROBE, "registered");
  496. return 0;
  497. }
  498. static void __exit sd_mod_exit(void)
  499. {
  500. usb_deregister(&sd_driver);
  501. PDEBUG(D_PROBE, "deregistered");
  502. }
  503. module_init(sd_mod_init);
  504. module_exit(sd_mod_exit);