kinect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #define MODULE_NAME "kinect"
  28. #include "gspca.h"
  29. #define CTRL_TIMEOUT 500
  30. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  31. MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
  32. MODULE_LICENSE("GPL");
  33. static bool depth_mode;
  34. struct pkt_hdr {
  35. uint8_t magic[2];
  36. uint8_t pad;
  37. uint8_t flag;
  38. uint8_t unk1;
  39. uint8_t seq;
  40. uint8_t unk2;
  41. uint8_t unk3;
  42. uint32_t timestamp;
  43. };
  44. struct cam_hdr {
  45. uint8_t magic[2];
  46. __le16 len;
  47. __le16 cmd;
  48. __le16 tag;
  49. };
  50. /* specific webcam descriptor */
  51. struct sd {
  52. struct gspca_dev gspca_dev; /* !! must be the first item */
  53. uint16_t cam_tag; /* a sequence number for packets */
  54. uint8_t stream_flag; /* to identify different stream types */
  55. uint8_t obuf[0x400]; /* output buffer for control commands */
  56. uint8_t ibuf[0x200]; /* input buffer for control commands */
  57. };
  58. #define MODE_640x480 0x0001
  59. #define MODE_640x488 0x0002
  60. #define MODE_1280x1024 0x0004
  61. #define FORMAT_BAYER 0x0010
  62. #define FORMAT_UYVY 0x0020
  63. #define FORMAT_Y10B 0x0040
  64. #define FPS_HIGH 0x0100
  65. static const struct v4l2_pix_format depth_camera_mode[] = {
  66. {640, 480, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
  67. .bytesperline = 640 * 10 / 8,
  68. .sizeimage = 640 * 480 * 10 / 8,
  69. .colorspace = V4L2_COLORSPACE_SRGB,
  70. .priv = MODE_640x488 | FORMAT_Y10B},
  71. };
  72. static const struct v4l2_pix_format video_camera_mode[] = {
  73. {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  74. .bytesperline = 640,
  75. .sizeimage = 640 * 480,
  76. .colorspace = V4L2_COLORSPACE_SRGB,
  77. .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
  78. {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
  79. .bytesperline = 640 * 2,
  80. .sizeimage = 640 * 480 * 2,
  81. .colorspace = V4L2_COLORSPACE_SRGB,
  82. .priv = MODE_640x480 | FORMAT_UYVY},
  83. {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  84. .bytesperline = 1280,
  85. .sizeimage = 1280 * 1024,
  86. .colorspace = V4L2_COLORSPACE_SRGB,
  87. .priv = MODE_1280x1024 | FORMAT_BAYER},
  88. {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
  89. .bytesperline = 640 * 10 / 8,
  90. .sizeimage = 640 * 488 * 10 / 8,
  91. .colorspace = V4L2_COLORSPACE_SRGB,
  92. .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
  93. {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
  94. .bytesperline = 1280 * 10 / 8,
  95. .sizeimage = 1280 * 1024 * 10 / 8,
  96. .colorspace = V4L2_COLORSPACE_SRGB,
  97. .priv = MODE_1280x1024 | FORMAT_Y10B},
  98. };
  99. static int kinect_write(struct usb_device *udev, uint8_t *data,
  100. uint16_t wLength)
  101. {
  102. return usb_control_msg(udev,
  103. usb_sndctrlpipe(udev, 0),
  104. 0x00,
  105. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  106. 0, 0, data, wLength, CTRL_TIMEOUT);
  107. }
  108. static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
  109. {
  110. return usb_control_msg(udev,
  111. usb_rcvctrlpipe(udev, 0),
  112. 0x00,
  113. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  114. 0, 0, data, wLength, CTRL_TIMEOUT);
  115. }
  116. static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
  117. unsigned int cmd_len, void *replybuf, unsigned int reply_len)
  118. {
  119. struct sd *sd = (struct sd *) gspca_dev;
  120. struct usb_device *udev = gspca_dev->dev;
  121. int res, actual_len;
  122. uint8_t *obuf = sd->obuf;
  123. uint8_t *ibuf = sd->ibuf;
  124. struct cam_hdr *chdr = (void *)obuf;
  125. struct cam_hdr *rhdr = (void *)ibuf;
  126. if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
  127. pr_err("send_cmd: Invalid command length (0x%x)\n", cmd_len);
  128. return -1;
  129. }
  130. chdr->magic[0] = 0x47;
  131. chdr->magic[1] = 0x4d;
  132. chdr->cmd = cpu_to_le16(cmd);
  133. chdr->tag = cpu_to_le16(sd->cam_tag);
  134. chdr->len = cpu_to_le16(cmd_len / 2);
  135. memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
  136. res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
  137. PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd,
  138. sd->cam_tag, cmd_len, res);
  139. if (res < 0) {
  140. pr_err("send_cmd: Output control transfer failed (%d)\n", res);
  141. return res;
  142. }
  143. do {
  144. actual_len = kinect_read(udev, ibuf, 0x200);
  145. } while (actual_len == 0);
  146. PDEBUG(D_USBO, "Control reply: %d", actual_len);
  147. if (actual_len < sizeof(*rhdr)) {
  148. pr_err("send_cmd: Input control transfer failed (%d)\n",
  149. actual_len);
  150. return actual_len < 0 ? actual_len : -EREMOTEIO;
  151. }
  152. actual_len -= sizeof(*rhdr);
  153. if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
  154. pr_err("send_cmd: Bad magic %02x %02x\n",
  155. rhdr->magic[0], rhdr->magic[1]);
  156. return -1;
  157. }
  158. if (rhdr->cmd != chdr->cmd) {
  159. pr_err("send_cmd: Bad cmd %02x != %02x\n",
  160. rhdr->cmd, chdr->cmd);
  161. return -1;
  162. }
  163. if (rhdr->tag != chdr->tag) {
  164. pr_err("send_cmd: Bad tag %04x != %04x\n",
  165. rhdr->tag, chdr->tag);
  166. return -1;
  167. }
  168. if (le16_to_cpu(rhdr->len) != (actual_len/2)) {
  169. pr_err("send_cmd: Bad len %04x != %04x\n",
  170. le16_to_cpu(rhdr->len), (int)(actual_len/2));
  171. return -1;
  172. }
  173. if (actual_len > reply_len) {
  174. pr_warn("send_cmd: Data buffer is %d bytes long, but got %d bytes\n",
  175. reply_len, actual_len);
  176. memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
  177. } else {
  178. memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
  179. }
  180. sd->cam_tag++;
  181. return actual_len;
  182. }
  183. static int write_register(struct gspca_dev *gspca_dev, uint16_t reg,
  184. uint16_t data)
  185. {
  186. uint16_t reply[2];
  187. __le16 cmd[2];
  188. int res;
  189. cmd[0] = cpu_to_le16(reg);
  190. cmd[1] = cpu_to_le16(data);
  191. PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
  192. res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
  193. if (res < 0)
  194. return res;
  195. if (res != 2) {
  196. pr_warn("send_cmd returned %d [%04x %04x], 0000 expected\n",
  197. res, reply[0], reply[1]);
  198. }
  199. return 0;
  200. }
  201. /* this function is called at probe time */
  202. static int sd_config_video(struct gspca_dev *gspca_dev,
  203. const struct usb_device_id *id)
  204. {
  205. struct sd *sd = (struct sd *) gspca_dev;
  206. struct cam *cam;
  207. sd->cam_tag = 0;
  208. sd->stream_flag = 0x80;
  209. cam = &gspca_dev->cam;
  210. cam->cam_mode = video_camera_mode;
  211. cam->nmodes = ARRAY_SIZE(video_camera_mode);
  212. gspca_dev->xfer_ep = 0x81;
  213. #if 0
  214. /* Setting those values is not needed for video stream */
  215. cam->npkt = 15;
  216. gspca_dev->pkt_size = 960 * 2;
  217. #endif
  218. return 0;
  219. }
  220. static int sd_config_depth(struct gspca_dev *gspca_dev,
  221. const struct usb_device_id *id)
  222. {
  223. struct sd *sd = (struct sd *) gspca_dev;
  224. struct cam *cam;
  225. sd->cam_tag = 0;
  226. sd->stream_flag = 0x70;
  227. cam = &gspca_dev->cam;
  228. cam->cam_mode = depth_camera_mode;
  229. cam->nmodes = ARRAY_SIZE(depth_camera_mode);
  230. gspca_dev->xfer_ep = 0x82;
  231. return 0;
  232. }
  233. /* this function is called at probe and resume time */
  234. static int sd_init(struct gspca_dev *gspca_dev)
  235. {
  236. PDEBUG(D_PROBE, "Kinect Camera device.");
  237. return 0;
  238. }
  239. static int sd_start_video(struct gspca_dev *gspca_dev)
  240. {
  241. int mode;
  242. uint8_t fmt_reg, fmt_val;
  243. uint8_t res_reg, res_val;
  244. uint8_t fps_reg, fps_val;
  245. uint8_t mode_val;
  246. mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
  247. if (mode & FORMAT_Y10B) {
  248. fmt_reg = 0x19;
  249. res_reg = 0x1a;
  250. fps_reg = 0x1b;
  251. mode_val = 0x03;
  252. } else {
  253. fmt_reg = 0x0c;
  254. res_reg = 0x0d;
  255. fps_reg = 0x0e;
  256. mode_val = 0x01;
  257. }
  258. /* format */
  259. if (mode & FORMAT_UYVY)
  260. fmt_val = 0x05;
  261. else
  262. fmt_val = 0x00;
  263. if (mode & MODE_1280x1024)
  264. res_val = 0x02;
  265. else
  266. res_val = 0x01;
  267. if (mode & FPS_HIGH)
  268. fps_val = 0x1e;
  269. else
  270. fps_val = 0x0f;
  271. /* turn off IR-reset function */
  272. write_register(gspca_dev, 0x105, 0x00);
  273. /* Reset video stream */
  274. write_register(gspca_dev, 0x05, 0x00);
  275. /* Due to some ridiculous condition in the firmware, we have to start
  276. * and stop the depth stream before the camera will hand us 1280x1024
  277. * IR. This is a stupid workaround, but we've yet to find a better
  278. * solution.
  279. *
  280. * Thanks to Drew Fisher for figuring this out.
  281. */
  282. if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
  283. write_register(gspca_dev, 0x13, 0x01);
  284. write_register(gspca_dev, 0x14, 0x1e);
  285. write_register(gspca_dev, 0x06, 0x02);
  286. write_register(gspca_dev, 0x06, 0x00);
  287. }
  288. write_register(gspca_dev, fmt_reg, fmt_val);
  289. write_register(gspca_dev, res_reg, res_val);
  290. write_register(gspca_dev, fps_reg, fps_val);
  291. /* Start video stream */
  292. write_register(gspca_dev, 0x05, mode_val);
  293. /* disable Hflip */
  294. write_register(gspca_dev, 0x47, 0x00);
  295. return 0;
  296. }
  297. static int sd_start_depth(struct gspca_dev *gspca_dev)
  298. {
  299. /* turn off IR-reset function */
  300. write_register(gspca_dev, 0x105, 0x00);
  301. /* reset depth stream */
  302. write_register(gspca_dev, 0x06, 0x00);
  303. /* Depth Stream Format 0x03: 11 bit stream | 0x02: 10 bit */
  304. write_register(gspca_dev, 0x12, 0x02);
  305. /* Depth Stream Resolution 1: standard (640x480) */
  306. write_register(gspca_dev, 0x13, 0x01);
  307. /* Depth Framerate / 0x1e (30): 30 fps */
  308. write_register(gspca_dev, 0x14, 0x1e);
  309. /* Depth Stream Control / 2: Open Depth Stream */
  310. write_register(gspca_dev, 0x06, 0x02);
  311. /* disable depth hflip / LSB = 0: Smoothing Disabled */
  312. write_register(gspca_dev, 0x17, 0x00);
  313. return 0;
  314. }
  315. static void sd_stopN_video(struct gspca_dev *gspca_dev)
  316. {
  317. /* reset video stream */
  318. write_register(gspca_dev, 0x05, 0x00);
  319. }
  320. static void sd_stopN_depth(struct gspca_dev *gspca_dev)
  321. {
  322. /* reset depth stream */
  323. write_register(gspca_dev, 0x06, 0x00);
  324. }
  325. static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
  326. {
  327. struct sd *sd = (struct sd *) gspca_dev;
  328. struct pkt_hdr *hdr = (void *)__data;
  329. uint8_t *data = __data + sizeof(*hdr);
  330. int datalen = len - sizeof(*hdr);
  331. uint8_t sof = sd->stream_flag | 1;
  332. uint8_t mof = sd->stream_flag | 2;
  333. uint8_t eof = sd->stream_flag | 5;
  334. if (len < 12)
  335. return;
  336. if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
  337. pr_warn("[Stream %02x] Invalid magic %02x%02x\n",
  338. sd->stream_flag, hdr->magic[0], hdr->magic[1]);
  339. return;
  340. }
  341. if (hdr->flag == sof)
  342. gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
  343. else if (hdr->flag == mof)
  344. gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
  345. else if (hdr->flag == eof)
  346. gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
  347. else
  348. pr_warn("Packet type not recognized...\n");
  349. }
  350. /* sub-driver description */
  351. static const struct sd_desc sd_desc_video = {
  352. .name = MODULE_NAME,
  353. .config = sd_config_video,
  354. .init = sd_init,
  355. .start = sd_start_video,
  356. .stopN = sd_stopN_video,
  357. .pkt_scan = sd_pkt_scan,
  358. /*
  359. .get_streamparm = sd_get_streamparm,
  360. .set_streamparm = sd_set_streamparm,
  361. */
  362. };
  363. static const struct sd_desc sd_desc_depth = {
  364. .name = MODULE_NAME,
  365. .config = sd_config_depth,
  366. .init = sd_init,
  367. .start = sd_start_depth,
  368. .stopN = sd_stopN_depth,
  369. .pkt_scan = sd_pkt_scan,
  370. /*
  371. .get_streamparm = sd_get_streamparm,
  372. .set_streamparm = sd_set_streamparm,
  373. */
  374. };
  375. /* -- module initialisation -- */
  376. static const struct usb_device_id device_table[] = {
  377. {USB_DEVICE(0x045e, 0x02ae)},
  378. {USB_DEVICE(0x045e, 0x02bf)},
  379. {}
  380. };
  381. MODULE_DEVICE_TABLE(usb, device_table);
  382. /* -- device connect -- */
  383. static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
  384. {
  385. if (depth_mode)
  386. return gspca_dev_probe(intf, id, &sd_desc_depth,
  387. sizeof(struct sd), THIS_MODULE);
  388. else
  389. return gspca_dev_probe(intf, id, &sd_desc_video,
  390. sizeof(struct sd), THIS_MODULE);
  391. }
  392. static struct usb_driver sd_driver = {
  393. .name = MODULE_NAME,
  394. .id_table = device_table,
  395. .probe = sd_probe,
  396. .disconnect = gspca_disconnect,
  397. #ifdef CONFIG_PM
  398. .suspend = gspca_suspend,
  399. .resume = gspca_resume,
  400. .reset_resume = gspca_resume,
  401. #endif
  402. };
  403. module_usb_driver(sd_driver);
  404. module_param(depth_mode, bool, 0644);
  405. MODULE_PARM_DESC(depth_mode, "0=video 1=depth");