kinect.c 10 KB

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