Remove some c++ comments and improve an output message
[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;           /* a sequence number for packets */
61         uint8_t stream_flag;        /* to identify different steram types */
62 };
63
64 /* V4L2 controls supported by the driver */
65 /* controls prototypes here */
66
67 static const struct ctrl sd_ctrls[] = {
68 };
69
70 #define MODE_640x480   0x0001
71 #define MODE_640x488   0x0002
72 #define MODE_1280x1024 0x0004
73
74 #define FORMAT_BAYER   0x0010
75 #define FORMAT_UYVY    0x0020
76 #define FORMAT_Y10B    0x0040
77
78 #define FPS_HIGH       0x0100
79
80 static const struct v4l2_pix_format video_camera_mode[] = {
81         {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
82          .bytesperline = 640,
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,
92          .bytesperline = 1280,
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},
106 };
107
108 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
109 {
110         return usb_control_msg(udev,
111                               usb_sndctrlpipe(udev, 0),
112                               0x00,
113                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
114                               0, 0, data, wLength, CTRL_TIMEOUT);
115 }
116
117 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
118 {
119         return usb_control_msg(udev,
120                               usb_rcvctrlpipe(udev, 0),
121                               0x00,
122                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
123                               0, 0, data, wLength, CTRL_TIMEOUT);
124 }
125
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)
128 {
129         struct sd *sd = (struct sd *) gspca_dev;
130         struct usb_device *udev = gspca_dev->dev;
131         int res, actual_len;
132         uint8_t obuf[0x400];
133         uint8_t ibuf[0x200];
134         cam_hdr *chdr = (void*)obuf;
135         cam_hdr *rhdr = (void*)ibuf;
136
137         if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
138                 err("send_cmd: Invalid command length (0x%x)", cmd_len);
139                 return -1;
140         }
141
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);
147
148         memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
149
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);
152         if (res < 0) {
153                 err("send_cmd: Output control transfer failed (%d)", res);
154                 return res;
155         }
156
157         do {
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);
163                 return res;
164         }
165         actual_len -= sizeof(*rhdr);
166
167         if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
168                 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
169                 return -1;
170         }
171         if (rhdr->cmd != chdr->cmd) {
172                 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
173                 return -1;
174         }
175         if (rhdr->tag != chdr->tag) {
176                 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
177                 return -1;
178         }
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));
182                 return -1;
183         }
184
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);
189         } else {
190                 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
191         }
192
193         sd->cam_tag++;
194
195         return actual_len;
196 }
197
198 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
199 {
200         uint16_t reply[2];
201         uint16_t cmd[2];
202         int res;
203
204         cmd[0] = cpu_to_le16(reg);
205         cmd[1] = cpu_to_le16(data);
206
207         PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
208         res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
209         if (res < 0)
210                 return res;
211         if (res != 2) {
212                 warn("send_cmd returned %d [%04x %04x], 0000 expected",
213                                 res, reply[0], reply[1]);
214         }
215         return 0;
216 }
217
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)
221 {
222         struct sd *sd = (struct sd *) gspca_dev;
223         struct cam *cam;
224
225         sd->cam_tag = 0;
226
227         /* Only color camera is supported for now,
228          * which has stream flag = 0x80 */
229         sd->stream_flag = 0x80;
230
231         cam = &gspca_dev->cam;
232
233         cam->cam_mode = video_camera_mode;
234         cam->nmodes = ARRAY_SIZE(video_camera_mode);
235
236 #if 0
237         /* Setting those values is not needed for color camera */
238         cam->npkt = 15;
239         gspca_dev->pkt_size = 960 * 2;
240 #endif
241
242         return 0;
243 }
244
245 /* this function is called at probe and resume time */
246 static int sd_init(struct gspca_dev *gspca_dev)
247 {
248         PDEBUG(D_PROBE, "Kinect Camera device.");
249
250         return 0;
251 }
252
253 static int sd_start(struct gspca_dev *gspca_dev)
254 {
255         int mode;
256         uint8_t fmt_reg, fmt_val;
257         uint8_t res_reg, res_val;
258         uint8_t fps_reg, fps_val;
259         uint8_t mode_val;
260
261         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
262
263         if (mode & FORMAT_Y10B) {
264                 fmt_reg = 0x19;
265                 res_reg = 0x1a;
266                 fps_reg = 0x1b;
267                 mode_val = 0x03;
268         } else {
269                 fmt_reg = 0x0c;
270                 res_reg = 0x0d;
271                 fps_reg = 0x0e;
272                 mode_val = 0x01;
273         }
274
275         /* format */
276         if (mode & FORMAT_UYVY)
277                 fmt_val = 0x05;
278         else
279                 fmt_val = 0x00;
280
281         if (mode & MODE_1280x1024)
282                 res_val = 0x02;
283         else
284                 res_val = 0x01;
285
286         if (mode & FPS_HIGH)
287                 fps_val = 0x1e;
288         else
289                 fps_val = 0x0f;
290
291
292         /* turn off IR-reset function */
293         write_register(gspca_dev, 0x105, 0x00);
294
295         /* Reset video stream */
296         write_register(gspca_dev, 0x05, 0x00);
297
298         /* Due to some ridiculous condition in the firmware, we have to start
299          * and stop the depth stream before the camera will hand us 1280x1024
300          * IR.  This is a stupid workaround, but we've yet to find a better
301          * solution.
302          *
303          * Thanks to Drew Fisher for figuring this out.
304          */
305         if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
306                 write_register(gspca_dev, 0x13, 0x01);
307                 write_register(gspca_dev, 0x14, 0x1e);
308                 write_register(gspca_dev, 0x06, 0x02);
309                 write_register(gspca_dev, 0x06, 0x00);
310         }
311
312         write_register(gspca_dev, fmt_reg, fmt_val);
313         write_register(gspca_dev, res_reg, res_val);
314         write_register(gspca_dev, fps_reg, fps_val);
315
316         /* Start video stream */
317         write_register(gspca_dev, 0x05, mode_val);
318
319         /* disable Hflip */
320         write_register(gspca_dev, 0x47, 0x00);
321
322         return 0;
323 }
324
325 static void sd_stopN(struct gspca_dev *gspca_dev)
326 {
327         /* reset video stream */
328         write_register(gspca_dev, 0x05, 0x00);
329 }
330
331 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
332 {
333         struct sd *sd = (struct sd *) gspca_dev;
334         
335         struct pkt_hdr *hdr = (void*)__data;
336         uint8_t *data = __data + sizeof(*hdr);
337         int datalen = len - sizeof(*hdr);
338
339         uint8_t sof = sd->stream_flag | 1;
340         uint8_t mof = sd->stream_flag | 2;
341         uint8_t eof = sd->stream_flag | 5;
342
343         if (len < 12)
344                 return;
345
346         if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
347                 warn("[Stream %02x] Invalid magic %02x%02x", sd->stream_flag,
348                                 hdr->magic[0], hdr->magic[1]);
349                 return;
350         }
351
352         if (hdr->flag == sof)
353                 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
354
355         else if (hdr->flag == mof)
356                 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
357
358         else if(hdr->flag == eof)
359                 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
360
361         else
362                 warn("Packet type not recognized...");
363 }
364
365 /* sub-driver description */
366 static const struct sd_desc sd_desc = {
367         .name      = MODULE_NAME,
368         .ctrls     = sd_ctrls,
369         .nctrls    = ARRAY_SIZE(sd_ctrls),
370         .config    = sd_config,
371         .init      = sd_init,
372         .start     = sd_start,
373         .stopN     = sd_stopN,
374         .pkt_scan  = sd_pkt_scan,
375         /*
376         .querymenu = sd_querymenu,
377         .get_streamparm = sd_get_streamparm,
378         .set_streamparm = sd_set_streamparm,
379         */
380 };
381
382 /* -- module initialisation -- */
383 static const __devinitdata struct usb_device_id device_table[] = {
384         {USB_DEVICE(0x045e, 0x02ae)},
385         {}
386 };
387
388 MODULE_DEVICE_TABLE(usb, device_table);
389
390 /* -- device connect -- */
391 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
392 {
393         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
394                                 THIS_MODULE);
395 }
396
397 static struct usb_driver sd_driver = {
398         .name       = MODULE_NAME,
399         .id_table   = device_table,
400         .probe      = sd_probe,
401         .disconnect = gspca_disconnect,
402 #ifdef CONFIG_PM
403         .suspend    = gspca_suspend,
404         .resume     = gspca_resume,
405 #endif
406 };
407
408 /* -- module insert / remove -- */
409 static int __init sd_mod_init(void)
410 {
411         return usb_register(&sd_driver);
412 }
413
414 static void __exit sd_mod_exit(void)
415 {
416         usb_deregister(&sd_driver);
417 }
418
419 module_init(sd_mod_init);
420 module_exit(sd_mod_exit);