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
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.
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
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #define MODULE_NAME "kinect"
33 #define CTRL_TIMEOUT 500
35 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
36 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
37 MODULE_LICENSE("GPL");
39 static bool depth_mode
;
59 /* specific webcam descriptor */
61 struct gspca_dev gspca_dev
; /* !! must be the first item */
62 uint16_t cam_tag
; /* a sequence number for packets */
63 uint8_t stream_flag
; /* to identify different stream types */
64 uint8_t obuf
[0x400]; /* output buffer for control commands */
65 uint8_t ibuf
[0x200]; /* input buffer for control commands */
68 #define MODE_640x480 0x0001
69 #define MODE_640x488 0x0002
70 #define MODE_1280x1024 0x0004
72 #define FORMAT_BAYER 0x0010
73 #define FORMAT_UYVY 0x0020
74 #define FORMAT_Y10B 0x0040
76 #define FPS_HIGH 0x0100
78 static const struct v4l2_pix_format depth_camera_mode
[] = {
79 {640, 480, V4L2_PIX_FMT_Y10BPACK
, V4L2_FIELD_NONE
,
80 .bytesperline
= 640 * 10 / 8,
81 .sizeimage
= 640 * 480 * 10 / 8,
82 .colorspace
= V4L2_COLORSPACE_SRGB
,
83 .priv
= MODE_640x488
| FORMAT_Y10B
},
86 static const struct v4l2_pix_format video_camera_mode
[] = {
87 {640, 480, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
89 .sizeimage
= 640 * 480,
90 .colorspace
= V4L2_COLORSPACE_SRGB
,
91 .priv
= MODE_640x480
| FORMAT_BAYER
| FPS_HIGH
},
92 {640, 480, V4L2_PIX_FMT_UYVY
, V4L2_FIELD_NONE
,
93 .bytesperline
= 640 * 2,
94 .sizeimage
= 640 * 480 * 2,
95 .colorspace
= V4L2_COLORSPACE_SRGB
,
96 .priv
= MODE_640x480
| FORMAT_UYVY
},
97 {1280, 1024, V4L2_PIX_FMT_SGRBG8
, V4L2_FIELD_NONE
,
99 .sizeimage
= 1280 * 1024,
100 .colorspace
= V4L2_COLORSPACE_SRGB
,
101 .priv
= MODE_1280x1024
| FORMAT_BAYER
},
102 {640, 488, V4L2_PIX_FMT_Y10BPACK
, V4L2_FIELD_NONE
,
103 .bytesperline
= 640 * 10 / 8,
104 .sizeimage
= 640 * 488 * 10 / 8,
105 .colorspace
= V4L2_COLORSPACE_SRGB
,
106 .priv
= MODE_640x488
| FORMAT_Y10B
| FPS_HIGH
},
107 {1280, 1024, V4L2_PIX_FMT_Y10BPACK
, V4L2_FIELD_NONE
,
108 .bytesperline
= 1280 * 10 / 8,
109 .sizeimage
= 1280 * 1024 * 10 / 8,
110 .colorspace
= V4L2_COLORSPACE_SRGB
,
111 .priv
= MODE_1280x1024
| FORMAT_Y10B
},
114 static int kinect_write(struct usb_device
*udev
, uint8_t *data
,
117 return usb_control_msg(udev
,
118 usb_sndctrlpipe(udev
, 0),
120 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
121 0, 0, data
, wLength
, CTRL_TIMEOUT
);
124 static int kinect_read(struct usb_device
*udev
, uint8_t *data
, uint16_t wLength
)
126 return usb_control_msg(udev
,
127 usb_rcvctrlpipe(udev
, 0),
129 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
130 0, 0, data
, wLength
, CTRL_TIMEOUT
);
133 static int send_cmd(struct gspca_dev
*gspca_dev
, uint16_t cmd
, void *cmdbuf
,
134 unsigned int cmd_len
, void *replybuf
, unsigned int reply_len
)
136 struct sd
*sd
= (struct sd
*) gspca_dev
;
137 struct usb_device
*udev
= gspca_dev
->dev
;
139 uint8_t *obuf
= sd
->obuf
;
140 uint8_t *ibuf
= sd
->ibuf
;
141 struct cam_hdr
*chdr
= (void *)obuf
;
142 struct cam_hdr
*rhdr
= (void *)ibuf
;
144 if (cmd_len
& 1 || cmd_len
> (0x400 - sizeof(*chdr
))) {
145 pr_err("send_cmd: Invalid command length (0x%x)\n", cmd_len
);
149 chdr
->magic
[0] = 0x47;
150 chdr
->magic
[1] = 0x4d;
151 chdr
->cmd
= cpu_to_le16(cmd
);
152 chdr
->tag
= cpu_to_le16(sd
->cam_tag
);
153 chdr
->len
= cpu_to_le16(cmd_len
/ 2);
155 memcpy(obuf
+sizeof(*chdr
), cmdbuf
, cmd_len
);
157 res
= kinect_write(udev
, obuf
, cmd_len
+ sizeof(*chdr
));
158 PDEBUG(D_USBO
, "Control cmd=%04x tag=%04x len=%04x: %d", cmd
,
159 sd
->cam_tag
, cmd_len
, res
);
161 pr_err("send_cmd: Output control transfer failed (%d)\n", res
);
166 actual_len
= kinect_read(udev
, ibuf
, 0x200);
167 } while (actual_len
== 0);
168 PDEBUG(D_USBO
, "Control reply: %d", actual_len
);
169 if (actual_len
< sizeof(*rhdr
)) {
170 pr_err("send_cmd: Input control transfer failed (%d)\n",
172 return actual_len
< 0 ? actual_len
: -EREMOTEIO
;
174 actual_len
-= sizeof(*rhdr
);
176 if (rhdr
->magic
[0] != 0x52 || rhdr
->magic
[1] != 0x42) {
177 pr_err("send_cmd: Bad magic %02x %02x\n",
178 rhdr
->magic
[0], rhdr
->magic
[1]);
181 if (rhdr
->cmd
!= chdr
->cmd
) {
182 pr_err("send_cmd: Bad cmd %02x != %02x\n",
183 rhdr
->cmd
, chdr
->cmd
);
186 if (rhdr
->tag
!= chdr
->tag
) {
187 pr_err("send_cmd: Bad tag %04x != %04x\n",
188 rhdr
->tag
, chdr
->tag
);
191 if (le16_to_cpu(rhdr
->len
) != (actual_len
/2)) {
192 pr_err("send_cmd: Bad len %04x != %04x\n",
193 le16_to_cpu(rhdr
->len
), (int)(actual_len
/2));
197 if (actual_len
> reply_len
) {
198 pr_warn("send_cmd: Data buffer is %d bytes long, but got %d bytes\n",
199 reply_len
, actual_len
);
200 memcpy(replybuf
, ibuf
+sizeof(*rhdr
), reply_len
);
202 memcpy(replybuf
, ibuf
+sizeof(*rhdr
), actual_len
);
210 static int write_register(struct gspca_dev
*gspca_dev
, uint16_t reg
,
217 cmd
[0] = cpu_to_le16(reg
);
218 cmd
[1] = cpu_to_le16(data
);
220 PDEBUG(D_USBO
, "Write Reg 0x%04x <= 0x%02x", reg
, data
);
221 res
= send_cmd(gspca_dev
, 0x03, cmd
, 4, reply
, 4);
225 pr_warn("send_cmd returned %d [%04x %04x], 0000 expected\n",
226 res
, reply
[0], reply
[1]);
231 /* this function is called at probe time */
232 static int sd_config_video(struct gspca_dev
*gspca_dev
,
233 const struct usb_device_id
*id
)
235 struct sd
*sd
= (struct sd
*) gspca_dev
;
240 sd
->stream_flag
= 0x80;
242 cam
= &gspca_dev
->cam
;
244 cam
->cam_mode
= video_camera_mode
;
245 cam
->nmodes
= ARRAY_SIZE(video_camera_mode
);
247 gspca_dev
->xfer_ep
= 0x81;
250 /* Setting those values is not needed for video stream */
252 gspca_dev
->pkt_size
= 960 * 2;
258 static int sd_config_depth(struct gspca_dev
*gspca_dev
,
259 const struct usb_device_id
*id
)
261 struct sd
*sd
= (struct sd
*) gspca_dev
;
266 sd
->stream_flag
= 0x70;
268 cam
= &gspca_dev
->cam
;
270 cam
->cam_mode
= depth_camera_mode
;
271 cam
->nmodes
= ARRAY_SIZE(depth_camera_mode
);
273 gspca_dev
->xfer_ep
= 0x82;
278 /* this function is called at probe and resume time */
279 static int sd_init(struct gspca_dev
*gspca_dev
)
281 PDEBUG(D_PROBE
, "Kinect Camera device.");
286 static int sd_start_video(struct gspca_dev
*gspca_dev
)
289 uint8_t fmt_reg
, fmt_val
;
290 uint8_t res_reg
, res_val
;
291 uint8_t fps_reg
, fps_val
;
294 mode
= gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
;
296 if (mode
& FORMAT_Y10B
) {
309 if (mode
& FORMAT_UYVY
)
314 if (mode
& MODE_1280x1024
)
325 /* turn off IR-reset function */
326 write_register(gspca_dev
, 0x105, 0x00);
328 /* Reset video stream */
329 write_register(gspca_dev
, 0x05, 0x00);
331 /* Due to some ridiculous condition in the firmware, we have to start
332 * and stop the depth stream before the camera will hand us 1280x1024
333 * IR. This is a stupid workaround, but we've yet to find a better
336 * Thanks to Drew Fisher for figuring this out.
338 if (mode
& (FORMAT_Y10B
| MODE_1280x1024
)) {
339 write_register(gspca_dev
, 0x13, 0x01);
340 write_register(gspca_dev
, 0x14, 0x1e);
341 write_register(gspca_dev
, 0x06, 0x02);
342 write_register(gspca_dev
, 0x06, 0x00);
345 write_register(gspca_dev
, fmt_reg
, fmt_val
);
346 write_register(gspca_dev
, res_reg
, res_val
);
347 write_register(gspca_dev
, fps_reg
, fps_val
);
349 /* Start video stream */
350 write_register(gspca_dev
, 0x05, mode_val
);
353 write_register(gspca_dev
, 0x47, 0x00);
358 static int sd_start_depth(struct gspca_dev
*gspca_dev
)
360 /* turn off IR-reset function */
361 write_register(gspca_dev
, 0x105, 0x00);
363 /* reset depth stream */
364 write_register(gspca_dev
, 0x06, 0x00);
365 /* Depth Stream Format 0x03: 11 bit stream | 0x02: 10 bit */
366 write_register(gspca_dev
, 0x12, 0x02);
367 /* Depth Stream Resolution 1: standard (640x480) */
368 write_register(gspca_dev
, 0x13, 0x01);
369 /* Depth Framerate / 0x1e (30): 30 fps */
370 write_register(gspca_dev
, 0x14, 0x1e);
371 /* Depth Stream Control / 2: Open Depth Stream */
372 write_register(gspca_dev
, 0x06, 0x02);
373 /* disable depth hflip / LSB = 0: Smoothing Disabled */
374 write_register(gspca_dev
, 0x17, 0x00);
379 static void sd_stopN_video(struct gspca_dev
*gspca_dev
)
381 /* reset video stream */
382 write_register(gspca_dev
, 0x05, 0x00);
385 static void sd_stopN_depth(struct gspca_dev
*gspca_dev
)
387 /* reset depth stream */
388 write_register(gspca_dev
, 0x06, 0x00);
391 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
, u8
*__data
, int len
)
393 struct sd
*sd
= (struct sd
*) gspca_dev
;
395 struct pkt_hdr
*hdr
= (void *)__data
;
396 uint8_t *data
= __data
+ sizeof(*hdr
);
397 int datalen
= len
- sizeof(*hdr
);
399 uint8_t sof
= sd
->stream_flag
| 1;
400 uint8_t mof
= sd
->stream_flag
| 2;
401 uint8_t eof
= sd
->stream_flag
| 5;
406 if (hdr
->magic
[0] != 'R' || hdr
->magic
[1] != 'B') {
407 pr_warn("[Stream %02x] Invalid magic %02x%02x\n",
408 sd
->stream_flag
, hdr
->magic
[0], hdr
->magic
[1]);
412 if (hdr
->flag
== sof
)
413 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, datalen
);
415 else if (hdr
->flag
== mof
)
416 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, datalen
);
418 else if (hdr
->flag
== eof
)
419 gspca_frame_add(gspca_dev
, LAST_PACKET
, data
, datalen
);
422 pr_warn("Packet type not recognized...\n");
425 /* sub-driver description */
426 static const struct sd_desc sd_desc_video
= {
428 .config
= sd_config_video
,
430 .start
= sd_start_video
,
431 .stopN
= sd_stopN_video
,
432 .pkt_scan
= sd_pkt_scan
,
434 .get_streamparm = sd_get_streamparm,
435 .set_streamparm = sd_set_streamparm,
438 static const struct sd_desc sd_desc_depth
= {
440 .config
= sd_config_depth
,
442 .start
= sd_start_depth
,
443 .stopN
= sd_stopN_depth
,
444 .pkt_scan
= sd_pkt_scan
,
446 .get_streamparm = sd_get_streamparm,
447 .set_streamparm = sd_set_streamparm,
451 /* -- module initialisation -- */
452 static const struct usb_device_id device_table
[] = {
453 {USB_DEVICE(0x045e, 0x02ae)},
454 {USB_DEVICE(0x045e, 0x02bf)},
458 MODULE_DEVICE_TABLE(usb
, device_table
);
460 /* -- device connect -- */
461 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
464 return gspca_dev_probe(intf
, id
, &sd_desc_depth
,
465 sizeof(struct sd
), THIS_MODULE
);
467 return gspca_dev_probe(intf
, id
, &sd_desc_video
,
468 sizeof(struct sd
), THIS_MODULE
);
471 static struct usb_driver sd_driver
= {
473 .id_table
= device_table
,
475 .disconnect
= gspca_disconnect
,
477 .suspend
= gspca_suspend
,
478 .resume
= gspca_resume
,
479 .reset_resume
= gspca_resume
,
483 module_usb_driver(sd_driver
);
485 module_param(depth_mode
, bool, 0644);
486 MODULE_PARM_DESC(depth_mode
, "0=video 1=depth");