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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define MODULE_NAME "kinect"
28 #define CTRL_TIMEOUT 500
30 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
31 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
32 MODULE_LICENSE("GPL");
35 int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
36 D_USBI | D_USBO | D_V4L2;
57 /* specific webcam descriptor */
59 struct gspca_dev gspca_dev; /* !! must be the first item */
60 uint16_t cam_tag; /* a sequence number for packets */
61 uint8_t stream_flag; /* to identify different steram types */
64 /* V4L2 controls supported by the driver */
65 /* controls prototypes here */
67 static const struct ctrl sd_ctrls[] = {
70 #define MODE_640x480 0x0001
71 #define MODE_640x488 0x0002
72 #define MODE_1280x1024 0x0004
74 #define FORMAT_BAYER 0x0010
75 #define FORMAT_UYVY 0x0020
76 #define FORMAT_Y10B 0x0040
78 #define FPS_HIGH 0x0100
80 static const struct v4l2_pix_format video_camera_mode[] = {
81 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
83 .sizeimage = 640 * 480,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
86 {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
87 .bytesperline = 640 * 2,
88 .sizeimage = 640 * 480 * 2,
89 .colorspace = V4L2_COLORSPACE_SRGB,
90 .priv = MODE_640x480 | FORMAT_UYVY},
91 {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
93 .sizeimage = 1280 * 1024,
94 .colorspace = V4L2_COLORSPACE_SRGB,
95 .priv = MODE_1280x1024 | FORMAT_BAYER},
96 {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
97 .bytesperline = 640 * 10 / 8,
98 .sizeimage = 640 * 488 * 10 / 8,
99 .colorspace = V4L2_COLORSPACE_SRGB,
100 .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
101 {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
102 .bytesperline = 1280 * 10 / 8,
103 .sizeimage = 1280 * 1024 * 10 / 8,
104 .colorspace = V4L2_COLORSPACE_SRGB,
105 .priv = MODE_1280x1024 | FORMAT_Y10B},
108 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
110 return usb_control_msg(udev,
111 usb_sndctrlpipe(udev, 0),
113 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
114 0, 0, data, wLength, CTRL_TIMEOUT);
117 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
119 return usb_control_msg(udev,
120 usb_rcvctrlpipe(udev, 0),
122 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
123 0, 0, data, wLength, CTRL_TIMEOUT);
126 static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
127 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
129 struct sd *sd = (struct sd *) gspca_dev;
130 struct usb_device *udev = gspca_dev->dev;
134 cam_hdr *chdr = (void*)obuf;
135 cam_hdr *rhdr = (void*)ibuf;
137 if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
138 err("send_cmd: Invalid command length (0x%x)", cmd_len);
142 chdr->magic[0] = 0x47;
143 chdr->magic[1] = 0x4d;
144 chdr->cmd = cpu_to_le16(cmd);
145 chdr->tag = cpu_to_le16(sd->cam_tag);
146 chdr->len = cpu_to_le16(cmd_len / 2);
148 memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
150 res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
151 PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
153 err("send_cmd: Output control transfer failed (%d)", res);
158 actual_len = kinect_read(udev, ibuf, 0x200);
159 } while (actual_len == 0);
160 PDEBUG(D_USBO, "Control reply: %d", res);
161 if (actual_len < sizeof(*rhdr)) {
162 err("send_cmd: Input control transfer failed (%d)", res);
165 actual_len -= sizeof(*rhdr);
167 if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
168 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
171 if (rhdr->cmd != chdr->cmd) {
172 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
175 if (rhdr->tag != chdr->tag) {
176 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
179 if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
180 err("send_cmd: Bad len %04x != %04x",
181 cpu_to_le16(rhdr->len), (int)(actual_len/2));
185 if (actual_len > reply_len) {
186 warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
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, uint16_t data)
204 cmd[0] = cpu_to_le16(reg);
205 cmd[1] = cpu_to_le16(data);
207 PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
208 res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
212 warn("send_cmd returned %d [%04x %04x], 0000 expected",
213 res, reply[0], reply[1]);
218 /* this function is called at probe time */
219 static int sd_config(struct gspca_dev *gspca_dev,
220 const struct usb_device_id *id)
222 struct sd *sd = (struct sd *) gspca_dev;
227 /* Only color camera is supported for now,
228 * which has stream flag = 0x80 */
229 sd->stream_flag = 0x80;
231 cam = &gspca_dev->cam;
233 cam->cam_mode = video_camera_mode;
234 cam->nmodes = ARRAY_SIZE(video_camera_mode);
237 //gspca_dev->pkt_size = 960 * 2;
242 /* this function is called at probe and resume time */
243 static int sd_init(struct gspca_dev *gspca_dev)
245 PDEBUG(D_PROBE, "Kinect Camera device.");
250 static int sd_start(struct gspca_dev *gspca_dev)
253 uint8_t fmt_reg, fmt_val;
254 uint8_t res_reg, res_val;
255 uint8_t fps_reg, fps_val;
258 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
260 if (mode & FORMAT_Y10B) {
273 if (mode & FORMAT_UYVY)
278 if (mode & MODE_1280x1024)
289 /* turn off IR-reset function */
290 write_register(gspca_dev, 0x105, 0x00);
292 /* Reset video stream */
293 write_register(gspca_dev, 0x05, 0x00);
295 /* Due to some ridiculous condition in the firmware, we have to start
296 * and stop the depth stream before the camera will hand us 1280x1024
297 * IR. This is a stupid workaround, but we've yet to find a better
300 * Thanks to Drew Fisher for figuring this out.
302 if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
303 write_register(gspca_dev, 0x13, 0x01);
304 write_register(gspca_dev, 0x14, 0x1e);
305 write_register(gspca_dev, 0x06, 0x02);
306 write_register(gspca_dev, 0x06, 0x00);
309 write_register(gspca_dev, fmt_reg, fmt_val);
310 write_register(gspca_dev, res_reg, res_val);
311 write_register(gspca_dev, fps_reg, fps_val);
313 /* Start video stream */
314 write_register(gspca_dev, 0x05, mode_val);
317 write_register(gspca_dev, 0x47, 0x00);
322 static void sd_stopN(struct gspca_dev *gspca_dev)
324 write_register(gspca_dev, 0x05, 0x00); // reset video stream
327 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
329 struct sd *sd = (struct sd *) gspca_dev;
331 struct pkt_hdr *hdr = (void*)__data;
332 uint8_t *data = __data + sizeof(*hdr);
333 int datalen = len - sizeof(*hdr);
335 uint8_t sof = sd->stream_flag | 1;
336 uint8_t mof = sd->stream_flag | 2;
337 uint8_t eof = sd->stream_flag | 5;
342 if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
343 warn("[Stream %02x] Invalid magic %02x%02x", sd->stream_flag,
344 hdr->magic[0], hdr->magic[1]);
348 if (hdr->flag == sof)
349 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
351 else if (hdr->flag == mof)
352 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
354 else if(hdr->flag == eof)
355 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
358 warn("Not recognized...");
361 /* sub-driver description */
362 static const struct sd_desc sd_desc = {
365 .nctrls = ARRAY_SIZE(sd_ctrls),
370 .pkt_scan = sd_pkt_scan,
372 .querymenu = sd_querymenu,
373 .get_streamparm = sd_get_streamparm,
374 .set_streamparm = sd_set_streamparm,
378 /* -- module initialisation -- */
379 static const __devinitdata struct usb_device_id device_table[] = {
380 {USB_DEVICE(0x045e, 0x02ae)},
384 MODULE_DEVICE_TABLE(usb, device_table);
386 /* -- device connect -- */
387 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
389 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
393 static struct usb_driver sd_driver = {
395 .id_table = device_table,
397 .disconnect = gspca_disconnect,
399 .suspend = gspca_suspend,
400 .resume = gspca_resume,
404 /* -- module insert / remove -- */
405 static int __init sd_mod_init(void)
407 return usb_register(&sd_driver);
410 static void __exit sd_mod_exit(void)
412 usb_deregister(&sd_driver);
415 module_init(sd_mod_init);
416 module_exit(sd_mod_exit);