b5077ca2306999f5ddd4aa462501ddae484f23a4
[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_1280x1024 0x0002
69
70 #define FORMAT_BAYER   0x0010
71 #define FORMAT_UYVY    0x0020
72
73 #define FPS_HIGH       0x0100
74
75 static const struct v4l2_pix_format video_camera_mode[] = {
76         {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
77          .bytesperline = 640,
78          .sizeimage = 640 * 480,
79          .colorspace = V4L2_COLORSPACE_SRGB,
80          .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
81         {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
82          .bytesperline = 640 * 2,
83          .sizeimage = 640 * 480 * 2,
84          .colorspace = V4L2_COLORSPACE_SRGB,
85          .priv = MODE_640x480 | FORMAT_UYVY},
86         {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
87          .bytesperline = 1280,
88          .sizeimage = 1280 * 1024,
89          .colorspace = V4L2_COLORSPACE_SRGB,
90          .priv = MODE_1280x1024 | FORMAT_BAYER},
91 };
92
93 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
94 {
95         return usb_control_msg(udev,
96                               usb_sndctrlpipe(udev, 0),
97                               0x00,
98                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99                               0, 0, data, wLength, CTRL_TIMEOUT);
100 }
101
102 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
103 {
104         return usb_control_msg(udev,
105                               usb_rcvctrlpipe(udev, 0),
106                               0x00,
107                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
108                               0, 0, data, wLength, CTRL_TIMEOUT);
109 }
110
111 static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
112                 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
113 {
114         struct sd *sd = (struct sd *) gspca_dev;
115         struct usb_device *udev = gspca_dev->dev;
116         int res, actual_len;
117         uint8_t obuf[0x400];
118         uint8_t ibuf[0x200];
119         cam_hdr *chdr = (void*)obuf;
120         cam_hdr *rhdr = (void*)ibuf;
121
122         if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
123                 err("send_cmd: Invalid command length (0x%x)", cmd_len);
124                 return -1;
125         }
126
127         chdr->magic[0] = 0x47;
128         chdr->magic[1] = 0x4d;
129         chdr->cmd = cpu_to_le16(cmd);
130         chdr->tag = cpu_to_le16(sd->cam_tag);
131         chdr->len = cpu_to_le16(cmd_len / 2);
132
133         memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
134
135         res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
136         info("Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
137         if (res < 0) {
138                 err("send_cmd: Output control transfer failed (%d)", res);
139                 return res;
140         }
141
142         do {
143                 actual_len = kinect_read(udev, ibuf, 0x200);
144         } while (actual_len == 0);
145         info("Control reply: %d", res);
146         if (actual_len < sizeof(*rhdr)) {
147                 err("send_cmd: Input control transfer failed (%d)", res);
148                 return res;
149         }
150         actual_len -= sizeof(*rhdr);
151
152         if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
153                 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
154                 return -1;
155         }
156         if (rhdr->cmd != chdr->cmd) {
157                 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
158                 return -1;
159         }
160         if (rhdr->tag != chdr->tag) {
161                 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
162                 return -1;
163         }
164         if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
165                 err("send_cmd: Bad len %04x != %04x",
166                                 cpu_to_le16(rhdr->len), (int)(actual_len/2));
167                 return -1;
168         }
169
170         if (actual_len > reply_len) {
171                 warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
172                                 reply_len, actual_len);
173                 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
174         } else {
175                 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
176         }
177
178         sd->cam_tag++;
179
180         return actual_len;
181 }
182
183 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
184 {
185         uint16_t reply[2];
186         uint16_t cmd[2];
187         int res;
188
189         cmd[0] = cpu_to_le16(reg);
190         cmd[1] = cpu_to_le16(data);
191
192         PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
193         res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
194         if (res < 0)
195                 return res;
196         if (res != 2) {
197                 warn("send_cmd returned %d [%04x %04x], 0000 expected",
198                                 res, reply[0], reply[1]);
199         }
200         return 0;
201 }
202
203 /* this function is called at probe time */
204 static int sd_config(struct gspca_dev *gspca_dev,
205                      const struct usb_device_id *id)
206 {
207         struct sd *sd = (struct sd *) gspca_dev;
208         struct cam *cam;
209
210         sd->cam_tag = 0;
211
212         cam = &gspca_dev->cam;
213
214         cam->cam_mode = video_camera_mode;
215         cam->nmodes = ARRAY_SIZE(video_camera_mode);
216
217         //cam->npkt = 15;
218         //gspca_dev->pkt_size = 960 * 2;
219
220         return 0;
221 }
222
223 /* this function is called at probe and resume time */
224 static int sd_init(struct gspca_dev *gspca_dev)
225 {
226         PDEBUG(D_PROBE, "Kinect Camera device.");
227
228         return 0;
229 }
230
231 static int sd_start(struct gspca_dev *gspca_dev)
232 {
233         int mode;
234         uint8_t fmt_reg, fmt_val;
235         uint8_t res_reg, res_val;
236         uint8_t fps_reg, fps_val;
237
238         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
239
240         fmt_reg = 0x0c;
241         res_reg = 0x0d;
242         fps_reg = 0x0e;
243
244         /* format */
245         if (mode & FORMAT_UYVY)
246                 fmt_val = 0x05;
247         else
248                 fmt_val = 0x00;
249
250         if (mode & MODE_1280x1024)
251                 res_val = 0x02;
252         else
253                 res_val = 0x01;
254
255         if (mode & FPS_HIGH)
256                 fps_val = 0x1e;
257         else
258                 fps_val = 0x0f;
259
260         /* Reset video stream */
261         write_register(gspca_dev, 0x05, 0x00);
262
263         write_register(gspca_dev, fmt_reg, fmt_val);
264         write_register(gspca_dev, res_reg, res_val);
265         write_register(gspca_dev, fps_reg, fps_val);
266
267         /* Start video stream */
268         write_register(gspca_dev, 0x05, 0x01);
269
270         /* disable Hflip */
271         write_register(gspca_dev, 0x47, 0x00);
272
273         return 0;
274 }
275
276 static void sd_stopN(struct gspca_dev *gspca_dev)
277 {
278         write_register(gspca_dev, 0x05, 0x00); // reset video stream
279 }
280
281 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
282 {
283         //struct sd *sd = (struct sd *) gspca_dev;
284         
285         struct pkt_hdr *hdr = (void*)__data;
286         uint8_t *data = __data + sizeof(*hdr);
287         int datalen = len - sizeof(*hdr);
288
289         /* strm->flag == 0x80 for the color camera stream */
290         uint8_t sof = 0x80 | 1;
291         uint8_t mof = 0x80 | 2;
292         uint8_t eof = 0x80 | 5;
293
294         if (len < 12)
295                 return;
296
297         if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
298                 warn("[Stream %02x] Invalid magic %02x%02x", 0x80, //strm->flag
299                                 hdr->magic[0], hdr->magic[1]);
300                 return;
301         }
302
303         if (hdr->flag == sof)
304                 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
305
306         else if (hdr->flag == mof)
307                 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
308
309         else if(hdr->flag == eof)
310                 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
311
312         else
313                 warn("Not recognized...");
314 }
315
316 /* sub-driver description */
317 static const struct sd_desc sd_desc = {
318         .name      = MODULE_NAME,
319         .ctrls     = sd_ctrls,
320         .nctrls    = ARRAY_SIZE(sd_ctrls),
321         .config    = sd_config,
322         .init      = sd_init,
323         .start     = sd_start,
324         .stopN     = sd_stopN,
325         .pkt_scan  = sd_pkt_scan,
326         /*
327         .querymenu = sd_querymenu,
328         .get_streamparm = sd_get_streamparm,
329         .set_streamparm = sd_set_streamparm,
330         */
331 };
332
333 /* -- module initialisation -- */
334 static const __devinitdata struct usb_device_id device_table[] = {
335         {USB_DEVICE(0x045e, 0x02ae)},
336         {}
337 };
338
339 MODULE_DEVICE_TABLE(usb, device_table);
340
341 /* -- device connect -- */
342 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
343 {
344         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
345                                 THIS_MODULE);
346 }
347
348 static struct usb_driver sd_driver = {
349         .name       = MODULE_NAME,
350         .id_table   = device_table,
351         .probe      = sd_probe,
352         .disconnect = gspca_disconnect,
353 #ifdef CONFIG_PM
354         .suspend    = gspca_suspend,
355         .resume     = gspca_resume,
356 #endif
357 };
358
359 /* -- module insert / remove -- */
360 static int __init sd_mod_init(void)
361 {
362         return usb_register(&sd_driver);
363 }
364
365 static void __exit sd_mod_exit(void)
366 {
367         usb_deregister(&sd_driver);
368 }
369
370 module_init(sd_mod_init);
371 module_exit(sd_mod_exit);