Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / gspca / kinect.c
blobf993f6280c5694d7e50b19ba67564375a71b8eda
1 /*
2 * kinect sensor device camera, gspca driver
4 * Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
6 * Based on the OpenKinect project and libfreenect
7 * http://openkinect.org/wiki/Init_Analysis
9 * Special thanks to Steven Toth and kernellabs.com for sponsoring a Kinect
10 * sensor device which I tested the driver on.
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.
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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #define MODULE_NAME "kinect"
27 #include "gspca.h"
29 #define CTRL_TIMEOUT 500
31 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
32 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
33 MODULE_LICENSE("GPL");
35 static bool depth_mode;
37 struct pkt_hdr {
38 uint8_t magic[2];
39 uint8_t pad;
40 uint8_t flag;
41 uint8_t unk1;
42 uint8_t seq;
43 uint8_t unk2;
44 uint8_t unk3;
45 uint32_t timestamp;
48 struct cam_hdr {
49 uint8_t magic[2];
50 __le16 len;
51 __le16 cmd;
52 __le16 tag;
55 /* specific webcam descriptor */
56 struct sd {
57 struct gspca_dev gspca_dev; /* !! must be the first item */
58 uint16_t cam_tag; /* a sequence number for packets */
59 uint8_t stream_flag; /* to identify different stream types */
60 uint8_t obuf[0x400]; /* output buffer for control commands */
61 uint8_t ibuf[0x200]; /* input buffer for control commands */
64 #define MODE_640x480 0x0001
65 #define MODE_640x488 0x0002
66 #define MODE_1280x1024 0x0004
68 #define FORMAT_BAYER 0x0010
69 #define FORMAT_UYVY 0x0020
70 #define FORMAT_Y10B 0x0040
72 #define FPS_HIGH 0x0100
74 static const struct v4l2_pix_format depth_camera_mode[] = {
75 {640, 480, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
76 .bytesperline = 640 * 10 / 8,
77 .sizeimage = 640 * 480 * 10 / 8,
78 .colorspace = V4L2_COLORSPACE_SRGB,
79 .priv = MODE_640x488 | FORMAT_Y10B},
82 static const struct v4l2_pix_format video_camera_mode[] = {
83 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
84 .bytesperline = 640,
85 .sizeimage = 640 * 480,
86 .colorspace = V4L2_COLORSPACE_SRGB,
87 .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
88 {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
89 .bytesperline = 640 * 2,
90 .sizeimage = 640 * 480 * 2,
91 .colorspace = V4L2_COLORSPACE_SRGB,
92 .priv = MODE_640x480 | FORMAT_UYVY},
93 {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
94 .bytesperline = 1280,
95 .sizeimage = 1280 * 1024,
96 .colorspace = V4L2_COLORSPACE_SRGB,
97 .priv = MODE_1280x1024 | FORMAT_BAYER},
98 {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
99 .bytesperline = 640 * 10 / 8,
100 .sizeimage = 640 * 488 * 10 / 8,
101 .colorspace = V4L2_COLORSPACE_SRGB,
102 .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
103 {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
104 .bytesperline = 1280 * 10 / 8,
105 .sizeimage = 1280 * 1024 * 10 / 8,
106 .colorspace = V4L2_COLORSPACE_SRGB,
107 .priv = MODE_1280x1024 | FORMAT_Y10B},
110 static int kinect_write(struct usb_device *udev, uint8_t *data,
111 uint16_t wLength)
113 return usb_control_msg(udev,
114 usb_sndctrlpipe(udev, 0),
115 0x00,
116 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
117 0, 0, data, wLength, CTRL_TIMEOUT);
120 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
122 return usb_control_msg(udev,
123 usb_rcvctrlpipe(udev, 0),
124 0x00,
125 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
126 0, 0, data, wLength, CTRL_TIMEOUT);
129 static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
130 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
132 struct sd *sd = (struct sd *) gspca_dev;
133 struct usb_device *udev = gspca_dev->dev;
134 int res, actual_len;
135 uint8_t *obuf = sd->obuf;
136 uint8_t *ibuf = sd->ibuf;
137 struct cam_hdr *chdr = (void *)obuf;
138 struct cam_hdr *rhdr = (void *)ibuf;
140 if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
141 pr_err("send_cmd: Invalid command length (0x%x)\n", cmd_len);
142 return -1;
145 chdr->magic[0] = 0x47;
146 chdr->magic[1] = 0x4d;
147 chdr->cmd = cpu_to_le16(cmd);
148 chdr->tag = cpu_to_le16(sd->cam_tag);
149 chdr->len = cpu_to_le16(cmd_len / 2);
151 memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
153 res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
154 gspca_dbg(gspca_dev, D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d\n",
155 cmd,
156 sd->cam_tag, cmd_len, res);
157 if (res < 0) {
158 pr_err("send_cmd: Output control transfer failed (%d)\n", res);
159 return res;
162 do {
163 actual_len = kinect_read(udev, ibuf, 0x200);
164 } while (actual_len == 0);
165 gspca_dbg(gspca_dev, D_USBO, "Control reply: %d\n", actual_len);
166 if (actual_len < (int)sizeof(*rhdr)) {
167 pr_err("send_cmd: Input control transfer failed (%d)\n",
168 actual_len);
169 return actual_len < 0 ? actual_len : -EREMOTEIO;
171 actual_len -= sizeof(*rhdr);
173 if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
174 pr_err("send_cmd: Bad magic %02x %02x\n",
175 rhdr->magic[0], rhdr->magic[1]);
176 return -1;
178 if (rhdr->cmd != chdr->cmd) {
179 pr_err("send_cmd: Bad cmd %02x != %02x\n",
180 rhdr->cmd, chdr->cmd);
181 return -1;
183 if (rhdr->tag != chdr->tag) {
184 pr_err("send_cmd: Bad tag %04x != %04x\n",
185 rhdr->tag, chdr->tag);
186 return -1;
188 if (le16_to_cpu(rhdr->len) != (actual_len/2)) {
189 pr_err("send_cmd: Bad len %04x != %04x\n",
190 le16_to_cpu(rhdr->len), (int)(actual_len/2));
191 return -1;
194 if (actual_len > reply_len) {
195 pr_warn("send_cmd: Data buffer is %d bytes long, but got %d bytes\n",
196 reply_len, actual_len);
197 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
198 } else {
199 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
202 sd->cam_tag++;
204 return actual_len;
207 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg,
208 uint16_t data)
210 uint16_t reply[2];
211 __le16 cmd[2];
212 int res;
214 cmd[0] = cpu_to_le16(reg);
215 cmd[1] = cpu_to_le16(data);
217 gspca_dbg(gspca_dev, D_USBO, "Write Reg 0x%04x <= 0x%02x\n", reg, data);
218 res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
219 if (res < 0)
220 return res;
221 if (res != 2) {
222 pr_warn("send_cmd returned %d [%04x %04x], 0000 expected\n",
223 res, reply[0], reply[1]);
225 return 0;
228 /* this function is called at probe time */
229 static int sd_config_video(struct gspca_dev *gspca_dev,
230 const struct usb_device_id *id)
232 struct sd *sd = (struct sd *) gspca_dev;
233 struct cam *cam;
235 sd->cam_tag = 0;
237 sd->stream_flag = 0x80;
239 cam = &gspca_dev->cam;
241 cam->cam_mode = video_camera_mode;
242 cam->nmodes = ARRAY_SIZE(video_camera_mode);
244 gspca_dev->xfer_ep = 0x81;
246 #if 0
247 /* Setting those values is not needed for video stream */
248 cam->npkt = 15;
249 gspca_dev->pkt_size = 960 * 2;
250 #endif
252 return 0;
255 static int sd_config_depth(struct gspca_dev *gspca_dev,
256 const struct usb_device_id *id)
258 struct sd *sd = (struct sd *) gspca_dev;
259 struct cam *cam;
261 sd->cam_tag = 0;
263 sd->stream_flag = 0x70;
265 cam = &gspca_dev->cam;
267 cam->cam_mode = depth_camera_mode;
268 cam->nmodes = ARRAY_SIZE(depth_camera_mode);
270 gspca_dev->xfer_ep = 0x82;
272 return 0;
275 /* this function is called at probe and resume time */
276 static int sd_init(struct gspca_dev *gspca_dev)
278 gspca_dbg(gspca_dev, D_PROBE, "Kinect Camera device.\n");
280 return 0;
283 static int sd_start_video(struct gspca_dev *gspca_dev)
285 int mode;
286 uint8_t fmt_reg, fmt_val;
287 uint8_t res_reg, res_val;
288 uint8_t fps_reg, fps_val;
289 uint8_t mode_val;
291 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
293 if (mode & FORMAT_Y10B) {
294 fmt_reg = 0x19;
295 res_reg = 0x1a;
296 fps_reg = 0x1b;
297 mode_val = 0x03;
298 } else {
299 fmt_reg = 0x0c;
300 res_reg = 0x0d;
301 fps_reg = 0x0e;
302 mode_val = 0x01;
305 /* format */
306 if (mode & FORMAT_UYVY)
307 fmt_val = 0x05;
308 else
309 fmt_val = 0x00;
311 if (mode & MODE_1280x1024)
312 res_val = 0x02;
313 else
314 res_val = 0x01;
316 if (mode & FPS_HIGH)
317 fps_val = 0x1e;
318 else
319 fps_val = 0x0f;
322 /* turn off IR-reset function */
323 write_register(gspca_dev, 0x105, 0x00);
325 /* Reset video stream */
326 write_register(gspca_dev, 0x05, 0x00);
328 /* Due to some ridiculous condition in the firmware, we have to start
329 * and stop the depth stream before the camera will hand us 1280x1024
330 * IR. This is a stupid workaround, but we've yet to find a better
331 * solution.
333 * Thanks to Drew Fisher for figuring this out.
335 if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
336 write_register(gspca_dev, 0x13, 0x01);
337 write_register(gspca_dev, 0x14, 0x1e);
338 write_register(gspca_dev, 0x06, 0x02);
339 write_register(gspca_dev, 0x06, 0x00);
342 write_register(gspca_dev, fmt_reg, fmt_val);
343 write_register(gspca_dev, res_reg, res_val);
344 write_register(gspca_dev, fps_reg, fps_val);
346 /* Start video stream */
347 write_register(gspca_dev, 0x05, mode_val);
349 /* disable Hflip */
350 write_register(gspca_dev, 0x47, 0x00);
352 return 0;
355 static int sd_start_depth(struct gspca_dev *gspca_dev)
357 /* turn off IR-reset function */
358 write_register(gspca_dev, 0x105, 0x00);
360 /* reset depth stream */
361 write_register(gspca_dev, 0x06, 0x00);
362 /* Depth Stream Format 0x03: 11 bit stream | 0x02: 10 bit */
363 write_register(gspca_dev, 0x12, 0x02);
364 /* Depth Stream Resolution 1: standard (640x480) */
365 write_register(gspca_dev, 0x13, 0x01);
366 /* Depth Framerate / 0x1e (30): 30 fps */
367 write_register(gspca_dev, 0x14, 0x1e);
368 /* Depth Stream Control / 2: Open Depth Stream */
369 write_register(gspca_dev, 0x06, 0x02);
370 /* disable depth hflip / LSB = 0: Smoothing Disabled */
371 write_register(gspca_dev, 0x17, 0x00);
373 return 0;
376 static void sd_stopN_video(struct gspca_dev *gspca_dev)
378 /* reset video stream */
379 write_register(gspca_dev, 0x05, 0x00);
382 static void sd_stopN_depth(struct gspca_dev *gspca_dev)
384 /* reset depth stream */
385 write_register(gspca_dev, 0x06, 0x00);
388 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
390 struct sd *sd = (struct sd *) gspca_dev;
392 struct pkt_hdr *hdr = (void *)__data;
393 uint8_t *data = __data + sizeof(*hdr);
394 int datalen = len - sizeof(*hdr);
396 uint8_t sof = sd->stream_flag | 1;
397 uint8_t mof = sd->stream_flag | 2;
398 uint8_t eof = sd->stream_flag | 5;
400 if (len < 12)
401 return;
403 if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
404 pr_warn("[Stream %02x] Invalid magic %02x%02x\n",
405 sd->stream_flag, hdr->magic[0], hdr->magic[1]);
406 return;
409 if (hdr->flag == sof)
410 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
412 else if (hdr->flag == mof)
413 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
415 else if (hdr->flag == eof)
416 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
418 else
419 pr_warn("Packet type not recognized...\n");
422 /* sub-driver description */
423 static const struct sd_desc sd_desc_video = {
424 .name = MODULE_NAME,
425 .config = sd_config_video,
426 .init = sd_init,
427 .start = sd_start_video,
428 .stopN = sd_stopN_video,
429 .pkt_scan = sd_pkt_scan,
431 .get_streamparm = sd_get_streamparm,
432 .set_streamparm = sd_set_streamparm,
435 static const struct sd_desc sd_desc_depth = {
436 .name = MODULE_NAME,
437 .config = sd_config_depth,
438 .init = sd_init,
439 .start = sd_start_depth,
440 .stopN = sd_stopN_depth,
441 .pkt_scan = sd_pkt_scan,
443 .get_streamparm = sd_get_streamparm,
444 .set_streamparm = sd_set_streamparm,
448 /* -- module initialisation -- */
449 static const struct usb_device_id device_table[] = {
450 {USB_DEVICE(0x045e, 0x02ae)},
451 {USB_DEVICE(0x045e, 0x02bf)},
455 MODULE_DEVICE_TABLE(usb, device_table);
457 /* -- device connect -- */
458 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
460 if (depth_mode)
461 return gspca_dev_probe(intf, id, &sd_desc_depth,
462 sizeof(struct sd), THIS_MODULE);
463 else
464 return gspca_dev_probe(intf, id, &sd_desc_video,
465 sizeof(struct sd), THIS_MODULE);
468 static struct usb_driver sd_driver = {
469 .name = MODULE_NAME,
470 .id_table = device_table,
471 .probe = sd_probe,
472 .disconnect = gspca_disconnect,
473 #ifdef CONFIG_PM
474 .suspend = gspca_suspend,
475 .resume = gspca_resume,
476 .reset_resume = gspca_resume,
477 #endif
480 module_usb_driver(sd_driver);
482 module_param(depth_mode, bool, 0644);
483 MODULE_PARM_DESC(depth_mode, "0=video 1=depth");