kinect.c 12 KB

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