Add a configurable stream_flag field
[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         //cam->npkt = 15;
237         //gspca_dev->pkt_size = 960 * 2;
238
239         return 0;
240 }
241
242 /* this function is called at probe and resume time */
243 static int sd_init(struct gspca_dev *gspca_dev)
244 {
245         PDEBUG(D_PROBE, "Kinect Camera device.");
246
247         return 0;
248 }
249
250 static int sd_start(struct gspca_dev *gspca_dev)
251 {
252         int mode;
253         uint8_t fmt_reg, fmt_val;
254         uint8_t res_reg, res_val;
255         uint8_t fps_reg, fps_val;
256         uint8_t mode_val;
257
258         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
259
260         if (mode & FORMAT_Y10B) {
261                 fmt_reg = 0x19;
262                 res_reg = 0x1a;
263                 fps_reg = 0x1b;
264                 mode_val = 0x03;
265         } else {
266                 fmt_reg = 0x0c;
267                 res_reg = 0x0d;
268                 fps_reg = 0x0e;
269                 mode_val = 0x01;
270         }
271
272         /* format */
273         if (mode & FORMAT_UYVY)
274                 fmt_val = 0x05;
275         else
276                 fmt_val = 0x00;
277
278         if (mode & MODE_1280x1024)
279                 res_val = 0x02;
280         else
281                 res_val = 0x01;
282
283         if (mode & FPS_HIGH)
284                 fps_val = 0x1e;
285         else
286                 fps_val = 0x0f;
287
288
289         /* turn off IR-reset function */
290         write_register(gspca_dev, 0x105, 0x00);
291
292         /* Reset video stream */
293         write_register(gspca_dev, 0x05, 0x00);
294
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
298          * solution.
299          *
300          * Thanks to Drew Fisher for figuring this out.
301          */
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);
307         }
308
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);
312
313         /* Start video stream */
314         write_register(gspca_dev, 0x05, mode_val);
315
316         /* disable Hflip */
317         write_register(gspca_dev, 0x47, 0x00);
318
319         return 0;
320 }
321
322 static void sd_stopN(struct gspca_dev *gspca_dev)
323 {
324         write_register(gspca_dev, 0x05, 0x00); // reset video stream
325 }
326
327 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
328 {
329         struct sd *sd = (struct sd *) gspca_dev;
330         
331         struct pkt_hdr *hdr = (void*)__data;
332         uint8_t *data = __data + sizeof(*hdr);
333         int datalen = len - sizeof(*hdr);
334
335         uint8_t sof = sd->stream_flag | 1;
336         uint8_t mof = sd->stream_flag | 2;
337         uint8_t eof = sd->stream_flag | 5;
338
339         if (len < 12)
340                 return;
341
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]);
345                 return;
346         }
347
348         if (hdr->flag == sof)
349                 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
350
351         else if (hdr->flag == mof)
352                 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
353
354         else if(hdr->flag == eof)
355                 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
356
357         else
358                 warn("Not recognized...");
359 }
360
361 /* sub-driver description */
362 static const struct sd_desc sd_desc = {
363         .name      = MODULE_NAME,
364         .ctrls     = sd_ctrls,
365         .nctrls    = ARRAY_SIZE(sd_ctrls),
366         .config    = sd_config,
367         .init      = sd_init,
368         .start     = sd_start,
369         .stopN     = sd_stopN,
370         .pkt_scan  = sd_pkt_scan,
371         /*
372         .querymenu = sd_querymenu,
373         .get_streamparm = sd_get_streamparm,
374         .set_streamparm = sd_set_streamparm,
375         */
376 };
377
378 /* -- module initialisation -- */
379 static const __devinitdata struct usb_device_id device_table[] = {
380         {USB_DEVICE(0x045e, 0x02ae)},
381         {}
382 };
383
384 MODULE_DEVICE_TABLE(usb, device_table);
385
386 /* -- device connect -- */
387 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
388 {
389         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
390                                 THIS_MODULE);
391 }
392
393 static struct usb_driver sd_driver = {
394         .name       = MODULE_NAME,
395         .id_table   = device_table,
396         .probe      = sd_probe,
397         .disconnect = gspca_disconnect,
398 #ifdef CONFIG_PM
399         .suspend    = gspca_suspend,
400         .resume     = gspca_resume,
401 #endif
402 };
403
404 /* -- module insert / remove -- */
405 static int __init sd_mod_init(void)
406 {
407         return usb_register(&sd_driver);
408 }
409
410 static void __exit sd_mod_exit(void)
411 {
412         usb_deregister(&sd_driver);
413 }
414
415 module_init(sd_mod_init);
416 module_exit(sd_mod_exit);