Add IR stream support.
[gspca_kinect.git] / kinect.c
1 /*
2  * kinect sensor device camera, gspca driver
3  *
4  * Copyright (C) 2010  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * Based on the OpenKinect project and libfreenect by Ector Martin
7  * http://openkinect.org/wiki/Init_Analysis
8  * 
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
12  * any later version.
13  *
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.
18  *
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
22  */
23
24 #define MODULE_NAME "kinect"
25
26 #include "gspca.h"
27
28 #define CTRL_TIMEOUT 500
29
30 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
31 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
32 MODULE_LICENSE("GPL");
33
34 struct pkt_hdr {
35         uint8_t magic[2];
36         uint8_t pad;
37         uint8_t flag;
38         uint8_t unk1;
39         uint8_t seq;
40         uint8_t unk2;
41         uint8_t unk3;
42         uint32_t timestamp;
43 };
44
45 typedef struct {
46         uint8_t magic[2];
47         uint16_t len;
48         uint16_t cmd;
49         uint16_t tag;
50 } cam_hdr;
51
52 int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
53         D_USBI | D_USBO | D_V4L2;
54
55 /* specific webcam descriptor */
56 struct sd {
57         struct gspca_dev gspca_dev;     /* !! must be the first item */
58         uint16_t cam_tag;
59 };
60
61 /* V4L2 controls supported by the driver */
62 /* controls prototypes here */
63
64 static const struct ctrl sd_ctrls[] = {
65 };
66
67 #define MODE_640x480   0x0001
68 #define MODE_640x488   0x0002
69 #define MODE_1280x1024 0x0004
70
71 #define FORMAT_BAYER   0x0010
72 #define FORMAT_UYVY    0x0020
73 #define FORMAT_Y10B    0x0040
74
75 #define FPS_HIGH       0x0100
76
77 static const struct v4l2_pix_format video_camera_mode[] = {
78         {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
79          .bytesperline = 640,
80          .sizeimage = 640 * 480,
81          .colorspace = V4L2_COLORSPACE_SRGB,
82          .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
83         {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
84          .bytesperline = 640 * 2,
85          .sizeimage = 640 * 480 * 2,
86          .colorspace = V4L2_COLORSPACE_SRGB,
87          .priv = MODE_640x480 | FORMAT_UYVY},
88         {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
89          .bytesperline = 1280,
90          .sizeimage = 1280 * 1024,
91          .colorspace = V4L2_COLORSPACE_SRGB,
92          .priv = MODE_1280x1024 | FORMAT_BAYER},
93         {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
94          .bytesperline = 640 * 10 / 8,
95          .sizeimage =  640 * 488 * 10 / 8,
96          .colorspace = V4L2_COLORSPACE_SRGB,
97          .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
98         {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
99          .bytesperline = 1280 * 10 / 8,
100          .sizeimage =  1280 * 1024 * 10 / 8,
101          .colorspace = V4L2_COLORSPACE_SRGB,
102          .priv = MODE_1280x1024 | FORMAT_Y10B},
103 };
104
105 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
106 {
107         return usb_control_msg(udev,
108                               usb_sndctrlpipe(udev, 0),
109                               0x00,
110                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
111                               0, 0, data, wLength, CTRL_TIMEOUT);
112 }
113
114 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
115 {
116         return usb_control_msg(udev,
117                               usb_rcvctrlpipe(udev, 0),
118                               0x00,
119                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
120                               0, 0, data, wLength, CTRL_TIMEOUT);
121 }
122
123 static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
124                 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
125 {
126         struct sd *sd = (struct sd *) gspca_dev;
127         struct usb_device *udev = gspca_dev->dev;
128         int res, actual_len;
129         uint8_t obuf[0x400];
130         uint8_t ibuf[0x200];
131         cam_hdr *chdr = (void*)obuf;
132         cam_hdr *rhdr = (void*)ibuf;
133
134         if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
135                 err("send_cmd: Invalid command length (0x%x)", cmd_len);
136                 return -1;
137         }
138
139         chdr->magic[0] = 0x47;
140         chdr->magic[1] = 0x4d;
141         chdr->cmd = cpu_to_le16(cmd);
142         chdr->tag = cpu_to_le16(sd->cam_tag);
143         chdr->len = cpu_to_le16(cmd_len / 2);
144
145         memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
146
147         res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
148         info("Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
149         if (res < 0) {
150                 err("send_cmd: Output control transfer failed (%d)", res);
151                 return res;
152         }
153
154         do {
155                 actual_len = kinect_read(udev, ibuf, 0x200);
156         } while (actual_len == 0);
157         info("Control reply: %d", res);
158         if (actual_len < sizeof(*rhdr)) {
159                 err("send_cmd: Input control transfer failed (%d)", res);
160                 return res;
161         }
162         actual_len -= sizeof(*rhdr);
163
164         if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
165                 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
166                 return -1;
167         }
168         if (rhdr->cmd != chdr->cmd) {
169                 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
170                 return -1;
171         }
172         if (rhdr->tag != chdr->tag) {
173                 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
174                 return -1;
175         }
176         if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
177                 err("send_cmd: Bad len %04x != %04x",
178                                 cpu_to_le16(rhdr->len), (int)(actual_len/2));
179                 return -1;
180         }
181
182         if (actual_len > reply_len) {
183                 warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
184                                 reply_len, actual_len);
185                 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
186         } else {
187                 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
188         }
189
190         sd->cam_tag++;
191
192         return actual_len;
193 }
194
195 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
196 {
197         uint16_t reply[2];
198         uint16_t cmd[2];
199         int res;
200
201         cmd[0] = cpu_to_le16(reg);
202         cmd[1] = cpu_to_le16(data);
203
204         PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
205         res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
206         if (res < 0)
207                 return res;
208         if (res != 2) {
209                 warn("send_cmd returned %d [%04x %04x], 0000 expected",
210                                 res, reply[0], reply[1]);
211         }
212         return 0;
213 }
214
215 /* this function is called at probe time */
216 static int sd_config(struct gspca_dev *gspca_dev,
217                      const struct usb_device_id *id)
218 {
219         struct sd *sd = (struct sd *) gspca_dev;
220         struct cam *cam;
221
222         sd->cam_tag = 0;
223
224         cam = &gspca_dev->cam;
225
226         cam->cam_mode = video_camera_mode;
227         cam->nmodes = ARRAY_SIZE(video_camera_mode);
228
229         //cam->npkt = 15;
230         //gspca_dev->pkt_size = 960 * 2;
231
232         return 0;
233 }
234
235 /* this function is called at probe and resume time */
236 static int sd_init(struct gspca_dev *gspca_dev)
237 {
238         PDEBUG(D_PROBE, "Kinect Camera device.");
239
240         return 0;
241 }
242
243 static int sd_start(struct gspca_dev *gspca_dev)
244 {
245         int mode;
246         uint8_t fmt_reg, fmt_val;
247         uint8_t res_reg, res_val;
248         uint8_t fps_reg, fps_val;
249         uint8_t mode_val;
250
251         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
252
253         if (mode & FORMAT_Y10B) {
254                 fmt_reg = 0x19;
255                 res_reg = 0x1a;
256                 fps_reg = 0x1b;
257                 mode_val = 0x03;
258         } else {
259                 fmt_reg = 0x0c;
260                 res_reg = 0x0d;
261                 fps_reg = 0x0e;
262                 mode_val = 0x01;
263         }
264
265         /* format */
266         if (mode & FORMAT_UYVY)
267                 fmt_val = 0x05;
268         else
269                 fmt_val = 0x00;
270
271         if (mode & MODE_1280x1024)
272                 res_val = 0x02;
273         else
274                 res_val = 0x01;
275
276         if (mode & FPS_HIGH)
277                 fps_val = 0x1e;
278         else
279                 fps_val = 0x0f;
280
281
282         /* turn off IR-reset function */
283         write_register(gspca_dev, 0x105, 0x00);
284
285         /* Reset video stream */
286         write_register(gspca_dev, 0x05, 0x00);
287
288         /* Due to some ridiculous condition in the firmware, we have to start
289          * and stop the depth stream before the camera will hand us 1280x1024
290          * IR.  This is a stupid workaround, but we've yet to find a better
291          * solution.
292          *
293          * Thanks to Drew Fisher for figuring this out.
294          */
295         if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
296                 write_register(gspca_dev, 0x13, 0x01);
297                 write_register(gspca_dev, 0x14, 0x1e);
298                 write_register(gspca_dev, 0x06, 0x02);
299                 write_register(gspca_dev, 0x06, 0x00);
300         }
301
302         write_register(gspca_dev, fmt_reg, fmt_val);
303         write_register(gspca_dev, res_reg, res_val);
304         write_register(gspca_dev, fps_reg, fps_val);
305
306         /* Start video stream */
307         write_register(gspca_dev, 0x05, mode_val);
308
309         /* disable Hflip */
310         write_register(gspca_dev, 0x47, 0x00);
311
312         return 0;
313 }
314
315 static void sd_stopN(struct gspca_dev *gspca_dev)
316 {
317         write_register(gspca_dev, 0x05, 0x00); // reset video stream
318 }
319
320 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
321 {
322         //struct sd *sd = (struct sd *) gspca_dev;
323         
324         struct pkt_hdr *hdr = (void*)__data;
325         uint8_t *data = __data + sizeof(*hdr);
326         int datalen = len - sizeof(*hdr);
327
328         /* strm->flag == 0x80 for the color camera stream */
329         uint8_t sof = 0x80 | 1;
330         uint8_t mof = 0x80 | 2;
331         uint8_t eof = 0x80 | 5;
332
333         if (len < 12)
334                 return;
335
336         if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
337                 warn("[Stream %02x] Invalid magic %02x%02x", 0x80, //strm->flag
338                                 hdr->magic[0], hdr->magic[1]);
339                 return;
340         }
341
342         if (hdr->flag == sof)
343                 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
344
345         else if (hdr->flag == mof)
346                 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
347
348         else if(hdr->flag == eof)
349                 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
350
351         else
352                 warn("Not recognized...");
353 }
354
355 /* sub-driver description */
356 static const struct sd_desc sd_desc = {
357         .name      = MODULE_NAME,
358         .ctrls     = sd_ctrls,
359         .nctrls    = ARRAY_SIZE(sd_ctrls),
360         .config    = sd_config,
361         .init      = sd_init,
362         .start     = sd_start,
363         .stopN     = sd_stopN,
364         .pkt_scan  = sd_pkt_scan,
365         /*
366         .querymenu = sd_querymenu,
367         .get_streamparm = sd_get_streamparm,
368         .set_streamparm = sd_set_streamparm,
369         */
370 };
371
372 /* -- module initialisation -- */
373 static const __devinitdata struct usb_device_id device_table[] = {
374         {USB_DEVICE(0x045e, 0x02ae)},
375         {}
376 };
377
378 MODULE_DEVICE_TABLE(usb, device_table);
379
380 /* -- device connect -- */
381 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
382 {
383         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
384                                 THIS_MODULE);
385 }
386
387 static struct usb_driver sd_driver = {
388         .name       = MODULE_NAME,
389         .id_table   = device_table,
390         .probe      = sd_probe,
391         .disconnect = gspca_disconnect,
392 #ifdef CONFIG_PM
393         .suspend    = gspca_suspend,
394         .resume     = gspca_resume,
395 #endif
396 };
397
398 /* -- module insert / remove -- */
399 static int __init sd_mod_init(void)
400 {
401         return usb_register(&sd_driver);
402 }
403
404 static void __exit sd_mod_exit(void)
405 {
406         usb_deregister(&sd_driver);
407 }
408
409 module_init(sd_mod_init);
410 module_exit(sd_mod_exit);