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