kinect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * kinect sensor device camera, gspca driver
  3. *
  4. * Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
  5. *
  6. * Based on the OpenKinect project and libfreenect
  7. * http://openkinect.org/wiki/Init_Analysis
  8. *
  9. * Special thanks to Steven Toth and kernellabs.com for sponsoring a Kinect
  10. * sensor device which I tested the driver on.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define MODULE_NAME "kinect"
  27. #include "gspca.h"
  28. #define CTRL_TIMEOUT 500
  29. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  30. MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
  31. MODULE_LICENSE("GPL");
  32. #ifdef DEBUG
  33. int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
  34. D_USBI | D_USBO | D_V4L2;
  35. #endif
  36. struct pkt_hdr {
  37. uint8_t magic[2];
  38. uint8_t pad;
  39. uint8_t flag;
  40. uint8_t unk1;
  41. uint8_t seq;
  42. uint8_t unk2;
  43. uint8_t unk3;
  44. uint32_t timestamp;
  45. };
  46. struct cam_hdr {
  47. uint8_t magic[2];
  48. uint16_t len;
  49. uint16_t cmd;
  50. uint16_t tag;
  51. };
  52. /* specific webcam descriptor */
  53. struct sd {
  54. struct gspca_dev gspca_dev; /* !! must be the first item */
  55. uint16_t cam_tag; /* a sequence number for packets */
  56. uint8_t stream_flag; /* to identify different steram types */
  57. };
  58. /* V4L2 controls supported by the driver */
  59. /* controls prototypes here */
  60. static const struct ctrl sd_ctrls[] = {
  61. };
  62. #define MODE_640x480 0x0001
  63. #define MODE_640x488 0x0002
  64. #define MODE_1280x1024 0x0004
  65. #define FORMAT_BAYER 0x0010
  66. #define FORMAT_UYVY 0x0020
  67. #define FORMAT_Y10B 0x0040
  68. #define FPS_HIGH 0x0100
  69. static const struct v4l2_pix_format video_camera_mode[] = {
  70. {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  71. .bytesperline = 640,
  72. .sizeimage = 640 * 480,
  73. .colorspace = V4L2_COLORSPACE_SRGB,
  74. .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
  75. {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
  76. .bytesperline = 640 * 2,
  77. .sizeimage = 640 * 480 * 2,
  78. .colorspace = V4L2_COLORSPACE_SRGB,
  79. .priv = MODE_640x480 | FORMAT_UYVY},
  80. {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  81. .bytesperline = 1280,
  82. .sizeimage = 1280 * 1024,
  83. .colorspace = V4L2_COLORSPACE_SRGB,
  84. .priv = MODE_1280x1024 | FORMAT_BAYER},
  85. {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
  86. .bytesperline = 640 * 10 / 8,
  87. .sizeimage = 640 * 488 * 10 / 8,
  88. .colorspace = V4L2_COLORSPACE_SRGB,
  89. .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
  90. {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
  91. .bytesperline = 1280 * 10 / 8,
  92. .sizeimage = 1280 * 1024 * 10 / 8,
  93. .colorspace = V4L2_COLORSPACE_SRGB,
  94. .priv = MODE_1280x1024 | FORMAT_Y10B},
  95. };
  96. static int kinect_write(struct usb_device *udev, uint8_t *data,
  97. uint16_t wLength)
  98. {
  99. return usb_control_msg(udev,
  100. usb_sndctrlpipe(udev, 0),
  101. 0x00,
  102. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  103. 0, 0, data, wLength, CTRL_TIMEOUT);
  104. }
  105. static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
  106. {
  107. return usb_control_msg(udev,
  108. usb_rcvctrlpipe(udev, 0),
  109. 0x00,
  110. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  111. 0, 0, data, wLength, CTRL_TIMEOUT);
  112. }
  113. static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
  114. unsigned int cmd_len, void *replybuf, unsigned int reply_len)
  115. {
  116. struct sd *sd = (struct sd *) gspca_dev;
  117. struct usb_device *udev = gspca_dev->dev;
  118. int res, actual_len;
  119. uint8_t obuf[0x400];
  120. uint8_t ibuf[0x200];
  121. struct cam_hdr *chdr = (void *)obuf;
  122. struct cam_hdr *rhdr = (void *)ibuf;
  123. if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
  124. err("send_cmd: Invalid command length (0x%x)", cmd_len);
  125. return -1;
  126. }
  127. chdr->magic[0] = 0x47;
  128. chdr->magic[1] = 0x4d;
  129. chdr->cmd = cpu_to_le16(cmd);
  130. chdr->tag = cpu_to_le16(sd->cam_tag);
  131. chdr->len = cpu_to_le16(cmd_len / 2);
  132. memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
  133. res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
  134. PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd,
  135. sd->cam_tag, cmd_len, res);
  136. if (res < 0) {
  137. err("send_cmd: Output control transfer failed (%d)", res);
  138. return res;
  139. }
  140. do {
  141. actual_len = kinect_read(udev, ibuf, 0x200);
  142. } while (actual_len == 0);
  143. PDEBUG(D_USBO, "Control reply: %d", res);
  144. if (actual_len < sizeof(*rhdr)) {
  145. err("send_cmd: Input control transfer failed (%d)", res);
  146. return res;
  147. }
  148. actual_len -= sizeof(*rhdr);
  149. if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
  150. err("send_cmd: Bad magic %02x %02x", rhdr->magic[0],
  151. rhdr->magic[1]);
  152. return -1;
  153. }
  154. if (rhdr->cmd != chdr->cmd) {
  155. err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
  156. return -1;
  157. }
  158. if (rhdr->tag != chdr->tag) {
  159. err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
  160. return -1;
  161. }
  162. if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
  163. err("send_cmd: Bad len %04x != %04x",
  164. cpu_to_le16(rhdr->len), (int)(actual_len/2));
  165. return -1;
  166. }
  167. if (actual_len > reply_len) {
  168. warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
  169. reply_len, actual_len);
  170. memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
  171. } else {
  172. memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
  173. }
  174. sd->cam_tag++;
  175. return actual_len;
  176. }
  177. static int write_register(struct gspca_dev *gspca_dev, uint16_t reg,
  178. uint16_t data)
  179. {
  180. uint16_t reply[2];
  181. uint16_t cmd[2];
  182. int res;
  183. cmd[0] = cpu_to_le16(reg);
  184. cmd[1] = cpu_to_le16(data);
  185. PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
  186. res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
  187. if (res < 0)
  188. return res;
  189. if (res != 2) {
  190. warn("send_cmd returned %d [%04x %04x], 0000 expected",
  191. res, reply[0], reply[1]);
  192. }
  193. return 0;
  194. }
  195. /* this function is called at probe time */
  196. static int sd_config(struct gspca_dev *gspca_dev,
  197. const struct usb_device_id *id)
  198. {
  199. struct sd *sd = (struct sd *) gspca_dev;
  200. struct cam *cam;
  201. sd->cam_tag = 0;
  202. /* Only color camera is supported for now,
  203. * which has stream flag = 0x80 */
  204. sd->stream_flag = 0x80;
  205. cam = &gspca_dev->cam;
  206. cam->cam_mode = video_camera_mode;
  207. cam->nmodes = ARRAY_SIZE(video_camera_mode);
  208. #if 0
  209. /* Setting those values is not needed for color camera */
  210. cam->npkt = 15;
  211. gspca_dev->pkt_size = 960 * 2;
  212. #endif
  213. return 0;
  214. }
  215. /* this function is called at probe and resume time */
  216. static int sd_init(struct gspca_dev *gspca_dev)
  217. {
  218. PDEBUG(D_PROBE, "Kinect Camera device.");
  219. return 0;
  220. }
  221. static int sd_start(struct gspca_dev *gspca_dev)
  222. {
  223. int mode;
  224. uint8_t fmt_reg, fmt_val;
  225. uint8_t res_reg, res_val;
  226. uint8_t fps_reg, fps_val;
  227. uint8_t mode_val;
  228. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  229. if (mode & FORMAT_Y10B) {
  230. fmt_reg = 0x19;
  231. res_reg = 0x1a;
  232. fps_reg = 0x1b;
  233. mode_val = 0x03;
  234. } else {
  235. fmt_reg = 0x0c;
  236. res_reg = 0x0d;
  237. fps_reg = 0x0e;
  238. mode_val = 0x01;
  239. }
  240. /* format */
  241. if (mode & FORMAT_UYVY)
  242. fmt_val = 0x05;
  243. else
  244. fmt_val = 0x00;
  245. if (mode & MODE_1280x1024)
  246. res_val = 0x02;
  247. else
  248. res_val = 0x01;
  249. if (mode & FPS_HIGH)
  250. fps_val = 0x1e;
  251. else
  252. fps_val = 0x0f;
  253. /* turn off IR-reset function */
  254. write_register(gspca_dev, 0x105, 0x00);
  255. /* Reset video stream */
  256. write_register(gspca_dev, 0x05, 0x00);
  257. /* Due to some ridiculous condition in the firmware, we have to start
  258. * and stop the depth stream before the camera will hand us 1280x1024
  259. * IR. This is a stupid workaround, but we've yet to find a better
  260. * solution.
  261. *
  262. * Thanks to Drew Fisher for figuring this out.
  263. */
  264. if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
  265. write_register(gspca_dev, 0x13, 0x01);
  266. write_register(gspca_dev, 0x14, 0x1e);
  267. write_register(gspca_dev, 0x06, 0x02);
  268. write_register(gspca_dev, 0x06, 0x00);
  269. }
  270. write_register(gspca_dev, fmt_reg, fmt_val);
  271. write_register(gspca_dev, res_reg, res_val);
  272. write_register(gspca_dev, fps_reg, fps_val);
  273. /* Start video stream */
  274. write_register(gspca_dev, 0x05, mode_val);
  275. /* disable Hflip */
  276. write_register(gspca_dev, 0x47, 0x00);
  277. return 0;
  278. }
  279. static void sd_stopN(struct gspca_dev *gspca_dev)
  280. {
  281. /* reset video stream */
  282. write_register(gspca_dev, 0x05, 0x00);
  283. }
  284. static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
  285. {
  286. struct sd *sd = (struct sd *) gspca_dev;
  287. struct pkt_hdr *hdr = (void *)__data;
  288. uint8_t *data = __data + sizeof(*hdr);
  289. int datalen = len - sizeof(*hdr);
  290. uint8_t sof = sd->stream_flag | 1;
  291. uint8_t mof = sd->stream_flag | 2;
  292. uint8_t eof = sd->stream_flag | 5;
  293. if (len < 12)
  294. return;
  295. if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
  296. warn("[Stream %02x] Invalid magic %02x%02x", sd->stream_flag,
  297. hdr->magic[0], hdr->magic[1]);
  298. return;
  299. }
  300. if (hdr->flag == sof)
  301. gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
  302. else if (hdr->flag == mof)
  303. gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
  304. else if (hdr->flag == eof)
  305. gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
  306. else
  307. warn("Packet type not recognized...");
  308. }
  309. /* sub-driver description */
  310. static const struct sd_desc sd_desc = {
  311. .name = MODULE_NAME,
  312. .ctrls = sd_ctrls,
  313. .nctrls = ARRAY_SIZE(sd_ctrls),
  314. .config = sd_config,
  315. .init = sd_init,
  316. .start = sd_start,
  317. .stopN = sd_stopN,
  318. .pkt_scan = sd_pkt_scan,
  319. /*
  320. .querymenu = sd_querymenu,
  321. .get_streamparm = sd_get_streamparm,
  322. .set_streamparm = sd_set_streamparm,
  323. */
  324. };
  325. /* -- module initialisation -- */
  326. static const __devinitdata struct usb_device_id device_table[] = {
  327. {USB_DEVICE(0x045e, 0x02ae)},
  328. {}
  329. };
  330. MODULE_DEVICE_TABLE(usb, device_table);
  331. /* -- device connect -- */
  332. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  333. {
  334. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  335. THIS_MODULE);
  336. }
  337. static struct usb_driver sd_driver = {
  338. .name = MODULE_NAME,
  339. .id_table = device_table,
  340. .probe = sd_probe,
  341. .disconnect = gspca_disconnect,
  342. #ifdef CONFIG_PM
  343. .suspend = gspca_suspend,
  344. .resume = gspca_resume,
  345. #endif
  346. };
  347. /* -- module insert / remove -- */
  348. static int __init sd_mod_init(void)
  349. {
  350. return usb_register(&sd_driver);
  351. }
  352. static void __exit sd_mod_exit(void)
  353. {
  354. usb_deregister(&sd_driver);
  355. }
  356. module_init(sd_mod_init);
  357. module_exit(sd_mod_exit);