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 MODULE_NAME "kinect"
31 #define CTRL_TIMEOUT 500
33 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
34 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
35 MODULE_LICENSE("GPL");
38 int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
39 D_USBI | D_USBO | D_V4L2;
60 /* specific webcam descriptor */
62 struct gspca_dev gspca_dev; /* !! must be the first item */
63 uint16_t cam_tag; /* a sequence number for packets */
64 uint8_t stream_flag; /* to identify different steram types */
67 /* V4L2 controls supported by the driver */
68 /* controls prototypes here */
70 static const struct ctrl sd_ctrls[] = {
73 #define MODE_640x480 0x0001
74 #define MODE_640x488 0x0002
75 #define MODE_1280x1024 0x0004
77 #define FORMAT_BAYER 0x0010
78 #define FORMAT_UYVY 0x0020
79 #define FORMAT_Y10B 0x0040
81 #define FPS_HIGH 0x0100
83 static const struct v4l2_pix_format video_camera_mode[] = {
84 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
86 .sizeimage = 640 * 480,
87 .colorspace = V4L2_COLORSPACE_SRGB,
88 .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
89 {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
90 .bytesperline = 640 * 2,
91 .sizeimage = 640 * 480 * 2,
92 .colorspace = V4L2_COLORSPACE_SRGB,
93 .priv = MODE_640x480 | FORMAT_UYVY},
94 {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
96 .sizeimage = 1280 * 1024,
97 .colorspace = V4L2_COLORSPACE_SRGB,
98 .priv = MODE_1280x1024 | FORMAT_BAYER},
99 {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
100 .bytesperline = 640 * 10 / 8,
101 .sizeimage = 640 * 488 * 10 / 8,
102 .colorspace = V4L2_COLORSPACE_SRGB,
103 .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
104 {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
105 .bytesperline = 1280 * 10 / 8,
106 .sizeimage = 1280 * 1024 * 10 / 8,
107 .colorspace = V4L2_COLORSPACE_SRGB,
108 .priv = MODE_1280x1024 | FORMAT_Y10B},
111 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
113 return usb_control_msg(udev,
114 usb_sndctrlpipe(udev, 0),
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),
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;
137 cam_hdr *chdr = (void*)obuf;
138 cam_hdr *rhdr = (void*)ibuf;
140 if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
141 err("send_cmd: Invalid command length (0x%x)", cmd_len);
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 PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
156 err("send_cmd: Output control transfer failed (%d)", res);
161 actual_len = kinect_read(udev, ibuf, 0x200);
162 } while (actual_len == 0);
163 PDEBUG(D_USBO, "Control reply: %d", res);
164 if (actual_len < sizeof(*rhdr)) {
165 err("send_cmd: Input control transfer failed (%d)", res);
168 actual_len -= sizeof(*rhdr);
170 if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
171 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
174 if (rhdr->cmd != chdr->cmd) {
175 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
178 if (rhdr->tag != chdr->tag) {
179 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
182 if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
183 err("send_cmd: Bad len %04x != %04x",
184 cpu_to_le16(rhdr->len), (int)(actual_len/2));
188 if (actual_len > reply_len) {
189 warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
190 reply_len, actual_len);
191 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
193 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
201 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
207 cmd[0] = cpu_to_le16(reg);
208 cmd[1] = cpu_to_le16(data);
210 PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
211 res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
215 warn("send_cmd returned %d [%04x %04x], 0000 expected",
216 res, reply[0], reply[1]);
221 /* this function is called at probe time */
222 static int sd_config(struct gspca_dev *gspca_dev,
223 const struct usb_device_id *id)
225 struct sd *sd = (struct sd *) gspca_dev;
230 /* Only color camera is supported for now,
231 * which has stream flag = 0x80 */
232 sd->stream_flag = 0x80;
234 cam = &gspca_dev->cam;
236 cam->cam_mode = video_camera_mode;
237 cam->nmodes = ARRAY_SIZE(video_camera_mode);
240 /* Setting those values is not needed for color camera */
242 gspca_dev->pkt_size = 960 * 2;
248 /* this function is called at probe and resume time */
249 static int sd_init(struct gspca_dev *gspca_dev)
251 PDEBUG(D_PROBE, "Kinect Camera device.");
256 static int sd_start(struct gspca_dev *gspca_dev)
259 uint8_t fmt_reg, fmt_val;
260 uint8_t res_reg, res_val;
261 uint8_t fps_reg, fps_val;
264 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
266 if (mode & FORMAT_Y10B) {
279 if (mode & FORMAT_UYVY)
284 if (mode & MODE_1280x1024)
295 /* turn off IR-reset function */
296 write_register(gspca_dev, 0x105, 0x00);
298 /* Reset video stream */
299 write_register(gspca_dev, 0x05, 0x00);
301 /* Due to some ridiculous condition in the firmware, we have to start
302 * and stop the depth stream before the camera will hand us 1280x1024
303 * IR. This is a stupid workaround, but we've yet to find a better
306 * Thanks to Drew Fisher for figuring this out.
308 if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
309 write_register(gspca_dev, 0x13, 0x01);
310 write_register(gspca_dev, 0x14, 0x1e);
311 write_register(gspca_dev, 0x06, 0x02);
312 write_register(gspca_dev, 0x06, 0x00);
315 write_register(gspca_dev, fmt_reg, fmt_val);
316 write_register(gspca_dev, res_reg, res_val);
317 write_register(gspca_dev, fps_reg, fps_val);
319 /* Start video stream */
320 write_register(gspca_dev, 0x05, mode_val);
323 write_register(gspca_dev, 0x47, 0x00);
328 static void sd_stopN(struct gspca_dev *gspca_dev)
330 /* reset video stream */
331 write_register(gspca_dev, 0x05, 0x00);
334 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
336 struct sd *sd = (struct sd *) gspca_dev;
338 struct pkt_hdr *hdr = (void*)__data;
339 uint8_t *data = __data + sizeof(*hdr);
340 int datalen = len - sizeof(*hdr);
342 uint8_t sof = sd->stream_flag | 1;
343 uint8_t mof = sd->stream_flag | 2;
344 uint8_t eof = sd->stream_flag | 5;
349 if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
350 warn("[Stream %02x] Invalid magic %02x%02x", sd->stream_flag,
351 hdr->magic[0], hdr->magic[1]);
355 if (hdr->flag == sof)
356 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
358 else if (hdr->flag == mof)
359 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
361 else if(hdr->flag == eof)
362 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
365 warn("Packet type not recognized...");
368 /* sub-driver description */
369 static const struct sd_desc sd_desc = {
372 .nctrls = ARRAY_SIZE(sd_ctrls),
377 .pkt_scan = sd_pkt_scan,
379 .querymenu = sd_querymenu,
380 .get_streamparm = sd_get_streamparm,
381 .set_streamparm = sd_set_streamparm,
385 /* -- module initialisation -- */
386 static const __devinitdata struct usb_device_id device_table[] = {
387 {USB_DEVICE(0x045e, 0x02ae)},
391 MODULE_DEVICE_TABLE(usb, device_table);
393 /* -- device connect -- */
394 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
396 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
400 static struct usb_driver sd_driver = {
402 .id_table = device_table,
404 .disconnect = gspca_disconnect,
406 .suspend = gspca_suspend,
407 .resume = gspca_resume,
411 /* -- module insert / remove -- */
412 static int __init sd_mod_init(void)
414 return usb_register(&sd_driver);
417 static void __exit sd_mod_exit(void)
419 usb_deregister(&sd_driver);
422 module_init(sd_mod_init);
423 module_exit(sd_mod_exit);