1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * kinect sensor device camera, gspca driver
5 * Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
7 * Based on the OpenKinect project and libfreenect
8 * http://openkinect.org/wiki/Init_Analysis
10 * Special thanks to Steven Toth and kernellabs.com for sponsoring a Kinect
11 * sensor device which I tested the driver on.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #define MODULE_NAME "kinect"
20 #define CTRL_TIMEOUT 500
22 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
23 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
24 MODULE_LICENSE("GPL");
26 static bool depth_mode
;
46 /* specific webcam descriptor */
48 struct gspca_dev gspca_dev
; /* !! must be the first item */
49 uint16_t cam_tag
; /* a sequence number for packets */
50 uint8_t stream_flag
; /* to identify different stream types */
51 uint8_t obuf
[0x400]; /* output buffer for control commands */
52 uint8_t ibuf
[0x200]; /* input buffer for control commands */
55 #define MODE_640x480 0x0001
56 #define MODE_640x488 0x0002
57 #define MODE_1280x1024 0x0004
59 #define FORMAT_BAYER 0x0010
60 #define FORMAT_UYVY 0x0020
61 #define FORMAT_Y10B 0x0040
63 #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
},
73 static const struct v4l2_pix_format video_camera_mode
[] = {
74 {640, 480, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
76 .sizeimage
= 640 * 480,
77 .colorspace
= V4L2_COLORSPACE_SRGB
,
78 .priv
= MODE_640x480
| FORMAT_BAYER
| FPS_HIGH
},
79 {640, 480, V4L2_PIX_FMT_UYVY
, V4L2_FIELD_NONE
,
80 .bytesperline
= 640 * 2,
81 .sizeimage
= 640 * 480 * 2,
82 .colorspace
= V4L2_COLORSPACE_SRGB
,
83 .priv
= MODE_640x480
| FORMAT_UYVY
},
84 {1280, 1024, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
86 .sizeimage
= 1280 * 1024,
87 .colorspace
= V4L2_COLORSPACE_SRGB
,
88 .priv
= MODE_1280x1024
| FORMAT_BAYER
},
89 {640, 488, V4L2_PIX_FMT_Y10BPACK
, V4L2_FIELD_NONE
,
90 .bytesperline
= 640 * 10 / 8,
91 .sizeimage
= 640 * 488 * 10 / 8,
92 .colorspace
= V4L2_COLORSPACE_SRGB
,
93 .priv
= MODE_640x488
| FORMAT_Y10B
| FPS_HIGH
},
94 {1280, 1024, V4L2_PIX_FMT_Y10BPACK
, V4L2_FIELD_NONE
,
95 .bytesperline
= 1280 * 10 / 8,
96 .sizeimage
= 1280 * 1024 * 10 / 8,
97 .colorspace
= V4L2_COLORSPACE_SRGB
,
98 .priv
= MODE_1280x1024
| FORMAT_Y10B
},
101 static int kinect_write(struct usb_device
*udev
, uint8_t *data
,
104 return usb_control_msg(udev
,
105 usb_sndctrlpipe(udev
, 0),
107 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
108 0, 0, data
, wLength
, CTRL_TIMEOUT
);
111 static int kinect_read(struct usb_device
*udev
, uint8_t *data
, uint16_t wLength
)
113 return usb_control_msg(udev
,
114 usb_rcvctrlpipe(udev
, 0),
116 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
117 0, 0, data
, wLength
, CTRL_TIMEOUT
);
120 static int send_cmd(struct gspca_dev
*gspca_dev
, uint16_t cmd
, void *cmdbuf
,
121 unsigned int cmd_len
, void *replybuf
, unsigned int reply_len
)
123 struct sd
*sd
= (struct sd
*) gspca_dev
;
124 struct usb_device
*udev
= gspca_dev
->dev
;
126 uint8_t *obuf
= sd
->obuf
;
127 uint8_t *ibuf
= sd
->ibuf
;
128 struct cam_hdr
*chdr
= (void *)obuf
;
129 struct cam_hdr
*rhdr
= (void *)ibuf
;
131 if (cmd_len
& 1 || cmd_len
> (0x400 - sizeof(*chdr
))) {
132 pr_err("send_cmd: Invalid command length (0x%x)\n", cmd_len
);
136 chdr
->magic
[0] = 0x47;
137 chdr
->magic
[1] = 0x4d;
138 chdr
->cmd
= cpu_to_le16(cmd
);
139 chdr
->tag
= cpu_to_le16(sd
->cam_tag
);
140 chdr
->len
= cpu_to_le16(cmd_len
/ 2);
142 memcpy(obuf
+sizeof(*chdr
), cmdbuf
, cmd_len
);
144 res
= kinect_write(udev
, obuf
, cmd_len
+ sizeof(*chdr
));
145 gspca_dbg(gspca_dev
, D_USBO
, "Control cmd=%04x tag=%04x len=%04x: %d\n",
147 sd
->cam_tag
, cmd_len
, res
);
149 pr_err("send_cmd: Output control transfer failed (%d)\n", res
);
154 actual_len
= kinect_read(udev
, ibuf
, 0x200);
155 } while (actual_len
== 0);
156 gspca_dbg(gspca_dev
, D_USBO
, "Control reply: %d\n", actual_len
);
157 if (actual_len
< (int)sizeof(*rhdr
)) {
158 pr_err("send_cmd: Input control transfer failed (%d)\n",
160 return actual_len
< 0 ? actual_len
: -EREMOTEIO
;
162 actual_len
-= sizeof(*rhdr
);
164 if (rhdr
->magic
[0] != 0x52 || rhdr
->magic
[1] != 0x42) {
165 pr_err("send_cmd: Bad magic %02x %02x\n",
166 rhdr
->magic
[0], rhdr
->magic
[1]);
169 if (rhdr
->cmd
!= chdr
->cmd
) {
170 pr_err("send_cmd: Bad cmd %02x != %02x\n",
171 rhdr
->cmd
, chdr
->cmd
);
174 if (rhdr
->tag
!= chdr
->tag
) {
175 pr_err("send_cmd: Bad tag %04x != %04x\n",
176 rhdr
->tag
, chdr
->tag
);
179 if (le16_to_cpu(rhdr
->len
) != (actual_len
/2)) {
180 pr_err("send_cmd: Bad len %04x != %04x\n",
181 le16_to_cpu(rhdr
->len
), (int)(actual_len
/2));
185 if (actual_len
> reply_len
) {
186 pr_warn("send_cmd: Data buffer is %d bytes long, but got %d bytes\n",
187 reply_len
, actual_len
);
188 memcpy(replybuf
, ibuf
+sizeof(*rhdr
), reply_len
);
190 memcpy(replybuf
, ibuf
+sizeof(*rhdr
), actual_len
);
198 static int write_register(struct gspca_dev
*gspca_dev
, uint16_t reg
,
205 cmd
[0] = cpu_to_le16(reg
);
206 cmd
[1] = cpu_to_le16(data
);
208 gspca_dbg(gspca_dev
, D_USBO
, "Write Reg 0x%04x <= 0x%02x\n", reg
, data
);
209 res
= send_cmd(gspca_dev
, 0x03, cmd
, 4, reply
, 4);
213 pr_warn("send_cmd returned %d [%04x %04x], 0000 expected\n",
214 res
, reply
[0], reply
[1]);
219 /* this function is called at probe time */
220 static int sd_config_video(struct gspca_dev
*gspca_dev
,
221 const struct usb_device_id
*id
)
223 struct sd
*sd
= (struct sd
*) gspca_dev
;
228 sd
->stream_flag
= 0x80;
230 cam
= &gspca_dev
->cam
;
232 cam
->cam_mode
= video_camera_mode
;
233 cam
->nmodes
= ARRAY_SIZE(video_camera_mode
);
235 gspca_dev
->xfer_ep
= 0x81;
238 /* Setting those values is not needed for video stream */
240 gspca_dev
->pkt_size
= 960 * 2;
246 static int sd_config_depth(struct gspca_dev
*gspca_dev
,
247 const struct usb_device_id
*id
)
249 struct sd
*sd
= (struct sd
*) gspca_dev
;
254 sd
->stream_flag
= 0x70;
256 cam
= &gspca_dev
->cam
;
258 cam
->cam_mode
= depth_camera_mode
;
259 cam
->nmodes
= ARRAY_SIZE(depth_camera_mode
);
261 gspca_dev
->xfer_ep
= 0x82;
266 /* this function is called at probe and resume time */
267 static int sd_init(struct gspca_dev
*gspca_dev
)
269 gspca_dbg(gspca_dev
, D_PROBE
, "Kinect Camera device.\n");
274 static int sd_start_video(struct gspca_dev
*gspca_dev
)
277 uint8_t fmt_reg
, fmt_val
;
278 uint8_t res_reg
, res_val
;
279 uint8_t fps_reg
, fps_val
;
282 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
284 if (mode
& FORMAT_Y10B
) {
297 if (mode
& FORMAT_UYVY
)
302 if (mode
& MODE_1280x1024
)
313 /* turn off IR-reset function */
314 write_register(gspca_dev
, 0x105, 0x00);
316 /* Reset video stream */
317 write_register(gspca_dev
, 0x05, 0x00);
319 /* Due to some ridiculous condition in the firmware, we have to start
320 * and stop the depth stream before the camera will hand us 1280x1024
321 * IR. This is a stupid workaround, but we've yet to find a better
324 * Thanks to Drew Fisher for figuring this out.
326 if (mode
& (FORMAT_Y10B
| MODE_1280x1024
)) {
327 write_register(gspca_dev
, 0x13, 0x01);
328 write_register(gspca_dev
, 0x14, 0x1e);
329 write_register(gspca_dev
, 0x06, 0x02);
330 write_register(gspca_dev
, 0x06, 0x00);
333 write_register(gspca_dev
, fmt_reg
, fmt_val
);
334 write_register(gspca_dev
, res_reg
, res_val
);
335 write_register(gspca_dev
, fps_reg
, fps_val
);
337 /* Start video stream */
338 write_register(gspca_dev
, 0x05, mode_val
);
341 write_register(gspca_dev
, 0x47, 0x00);
346 static int sd_start_depth(struct gspca_dev
*gspca_dev
)
348 /* turn off IR-reset function */
349 write_register(gspca_dev
, 0x105, 0x00);
351 /* reset depth stream */
352 write_register(gspca_dev
, 0x06, 0x00);
353 /* Depth Stream Format 0x03: 11 bit stream | 0x02: 10 bit */
354 write_register(gspca_dev
, 0x12, 0x02);
355 /* Depth Stream Resolution 1: standard (640x480) */
356 write_register(gspca_dev
, 0x13, 0x01);
357 /* Depth Framerate / 0x1e (30): 30 fps */
358 write_register(gspca_dev
, 0x14, 0x1e);
359 /* Depth Stream Control / 2: Open Depth Stream */
360 write_register(gspca_dev
, 0x06, 0x02);
361 /* disable depth hflip / LSB = 0: Smoothing Disabled */
362 write_register(gspca_dev
, 0x17, 0x00);
367 static void sd_stopN_video(struct gspca_dev
*gspca_dev
)
369 /* reset video stream */
370 write_register(gspca_dev
, 0x05, 0x00);
373 static void sd_stopN_depth(struct gspca_dev
*gspca_dev
)
375 /* reset depth stream */
376 write_register(gspca_dev
, 0x06, 0x00);
379 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
, u8
*__data
, int len
)
381 struct sd
*sd
= (struct sd
*) gspca_dev
;
383 struct pkt_hdr
*hdr
= (void *)__data
;
384 uint8_t *data
= __data
+ sizeof(*hdr
);
385 int datalen
= len
- sizeof(*hdr
);
387 uint8_t sof
= sd
->stream_flag
| 1;
388 uint8_t mof
= sd
->stream_flag
| 2;
389 uint8_t eof
= sd
->stream_flag
| 5;
394 if (hdr
->magic
[0] != 'R' || hdr
->magic
[1] != 'B') {
395 pr_warn("[Stream %02x] Invalid magic %02x%02x\n",
396 sd
->stream_flag
, hdr
->magic
[0], hdr
->magic
[1]);
400 if (hdr
->flag
== sof
)
401 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, datalen
);
403 else if (hdr
->flag
== mof
)
404 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, datalen
);
406 else if (hdr
->flag
== eof
)
407 gspca_frame_add(gspca_dev
, LAST_PACKET
, data
, datalen
);
410 pr_warn("Packet type not recognized...\n");
413 /* sub-driver description */
414 static const struct sd_desc sd_desc_video
= {
416 .config
= sd_config_video
,
418 .start
= sd_start_video
,
419 .stopN
= sd_stopN_video
,
420 .pkt_scan
= sd_pkt_scan
,
422 .get_streamparm = sd_get_streamparm,
423 .set_streamparm = sd_set_streamparm,
426 static const struct sd_desc sd_desc_depth
= {
428 .config
= sd_config_depth
,
430 .start
= sd_start_depth
,
431 .stopN
= sd_stopN_depth
,
432 .pkt_scan
= sd_pkt_scan
,
434 .get_streamparm = sd_get_streamparm,
435 .set_streamparm = sd_set_streamparm,
439 /* -- module initialisation -- */
440 static const struct usb_device_id device_table
[] = {
441 {USB_DEVICE(0x045e, 0x02ae)},
442 {USB_DEVICE(0x045e, 0x02bf)},
446 MODULE_DEVICE_TABLE(usb
, device_table
);
448 /* -- device connect -- */
449 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
452 return gspca_dev_probe(intf
, id
, &sd_desc_depth
,
453 sizeof(struct sd
), THIS_MODULE
);
455 return gspca_dev_probe(intf
, id
, &sd_desc_video
,
456 sizeof(struct sd
), THIS_MODULE
);
459 static struct usb_driver sd_driver
= {
461 .id_table
= device_table
,
463 .disconnect
= gspca_disconnect
,
465 .suspend
= gspca_suspend
,
466 .resume
= gspca_resume
,
467 .reset_resume
= gspca_resume
,
471 module_usb_driver(sd_driver
);
473 module_param(depth_mode
, bool, 0644);
474 MODULE_PARM_DESC(depth_mode
, "0=video 1=depth");