blob: 9ed15a10347753c2fc604261a2323d196be2c737 [file] [log] [blame]
Antonio Ospite66121552011-04-07 12:45:52 -03001/*
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 * Special thanks to Steven Toth and kernellabs.com for sponsoring a Kinect
10 * sensor device which I tested the driver on.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
Joe Perches133a9fe2011-08-21 19:56:57 -030027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Antonio Ospite66121552011-04-07 12:45:52 -030029#define MODULE_NAME "kinect"
30
31#include "gspca.h"
32
33#define CTRL_TIMEOUT 500
34
35MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
36MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
37MODULE_LICENSE("GPL");
38
Jarod Wilsonce14dcd2011-05-25 18:34:32 -030039#ifdef GSPCA_DEBUG
Antonio Ospite66121552011-04-07 12:45:52 -030040int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
41 D_USBI | D_USBO | D_V4L2;
42#endif
43
44struct pkt_hdr {
45 uint8_t magic[2];
46 uint8_t pad;
47 uint8_t flag;
48 uint8_t unk1;
49 uint8_t seq;
50 uint8_t unk2;
51 uint8_t unk3;
52 uint32_t timestamp;
53};
54
55struct cam_hdr {
56 uint8_t magic[2];
57 uint16_t len;
58 uint16_t cmd;
59 uint16_t tag;
60};
61
62/* specific webcam descriptor */
63struct sd {
64 struct gspca_dev gspca_dev; /* !! must be the first item */
65 uint16_t cam_tag; /* a sequence number for packets */
Drew Fisher3fabe8f2011-04-21 06:51:35 -030066 uint8_t stream_flag; /* to identify different stream types */
Drew Fisherbb066462011-04-21 06:51:34 -030067 uint8_t obuf[0x400]; /* output buffer for control commands */
68 uint8_t ibuf[0x200]; /* input buffer for control commands */
Antonio Ospite66121552011-04-07 12:45:52 -030069};
70
71/* V4L2 controls supported by the driver */
72/* controls prototypes here */
73
74static const struct ctrl sd_ctrls[] = {
75};
76
77#define MODE_640x480 0x0001
78#define MODE_640x488 0x0002
79#define MODE_1280x1024 0x0004
80
81#define FORMAT_BAYER 0x0010
82#define FORMAT_UYVY 0x0020
83#define FORMAT_Y10B 0x0040
84
85#define FPS_HIGH 0x0100
86
87static const struct v4l2_pix_format video_camera_mode[] = {
88 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
89 .bytesperline = 640,
90 .sizeimage = 640 * 480,
91 .colorspace = V4L2_COLORSPACE_SRGB,
92 .priv = MODE_640x480 | FORMAT_BAYER | FPS_HIGH},
93 {640, 480, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
94 .bytesperline = 640 * 2,
95 .sizeimage = 640 * 480 * 2,
96 .colorspace = V4L2_COLORSPACE_SRGB,
97 .priv = MODE_640x480 | FORMAT_UYVY},
98 {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
99 .bytesperline = 1280,
100 .sizeimage = 1280 * 1024,
101 .colorspace = V4L2_COLORSPACE_SRGB,
102 .priv = MODE_1280x1024 | FORMAT_BAYER},
103 {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
104 .bytesperline = 640 * 10 / 8,
105 .sizeimage = 640 * 488 * 10 / 8,
106 .colorspace = V4L2_COLORSPACE_SRGB,
107 .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
108 {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
109 .bytesperline = 1280 * 10 / 8,
110 .sizeimage = 1280 * 1024 * 10 / 8,
111 .colorspace = V4L2_COLORSPACE_SRGB,
112 .priv = MODE_1280x1024 | FORMAT_Y10B},
113};
114
115static int kinect_write(struct usb_device *udev, uint8_t *data,
116 uint16_t wLength)
117{
118 return usb_control_msg(udev,
119 usb_sndctrlpipe(udev, 0),
120 0x00,
121 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122 0, 0, data, wLength, CTRL_TIMEOUT);
123}
124
125static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
126{
127 return usb_control_msg(udev,
128 usb_rcvctrlpipe(udev, 0),
129 0x00,
130 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 0, 0, data, wLength, CTRL_TIMEOUT);
132}
133
134static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
135 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
136{
137 struct sd *sd = (struct sd *) gspca_dev;
138 struct usb_device *udev = gspca_dev->dev;
139 int res, actual_len;
Drew Fisherbb066462011-04-21 06:51:34 -0300140 uint8_t *obuf = sd->obuf;
141 uint8_t *ibuf = sd->ibuf;
Antonio Ospite66121552011-04-07 12:45:52 -0300142 struct cam_hdr *chdr = (void *)obuf;
143 struct cam_hdr *rhdr = (void *)ibuf;
144
145 if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300146 pr_err("send_cmd: Invalid command length (0x%x)\n", cmd_len);
Antonio Ospite66121552011-04-07 12:45:52 -0300147 return -1;
148 }
149
150 chdr->magic[0] = 0x47;
151 chdr->magic[1] = 0x4d;
152 chdr->cmd = cpu_to_le16(cmd);
153 chdr->tag = cpu_to_le16(sd->cam_tag);
154 chdr->len = cpu_to_le16(cmd_len / 2);
155
156 memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
157
158 res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
159 PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd,
160 sd->cam_tag, cmd_len, res);
161 if (res < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300162 pr_err("send_cmd: Output control transfer failed (%d)\n", res);
Antonio Ospite66121552011-04-07 12:45:52 -0300163 return res;
164 }
165
166 do {
167 actual_len = kinect_read(udev, ibuf, 0x200);
168 } while (actual_len == 0);
169 PDEBUG(D_USBO, "Control reply: %d", res);
170 if (actual_len < sizeof(*rhdr)) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300171 pr_err("send_cmd: Input control transfer failed (%d)\n", res);
Antonio Ospite66121552011-04-07 12:45:52 -0300172 return res;
173 }
174 actual_len -= sizeof(*rhdr);
175
176 if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300177 pr_err("send_cmd: Bad magic %02x %02x\n",
178 rhdr->magic[0], rhdr->magic[1]);
Antonio Ospite66121552011-04-07 12:45:52 -0300179 return -1;
180 }
181 if (rhdr->cmd != chdr->cmd) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300182 pr_err("send_cmd: Bad cmd %02x != %02x\n",
183 rhdr->cmd, chdr->cmd);
Antonio Ospite66121552011-04-07 12:45:52 -0300184 return -1;
185 }
186 if (rhdr->tag != chdr->tag) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300187 pr_err("send_cmd: Bad tag %04x != %04x\n",
188 rhdr->tag, chdr->tag);
Antonio Ospite66121552011-04-07 12:45:52 -0300189 return -1;
190 }
191 if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300192 pr_err("send_cmd: Bad len %04x != %04x\n",
193 cpu_to_le16(rhdr->len), (int)(actual_len/2));
Antonio Ospite66121552011-04-07 12:45:52 -0300194 return -1;
195 }
196
197 if (actual_len > reply_len) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300198 pr_warn("send_cmd: Data buffer is %d bytes long, but got %d bytes\n",
199 reply_len, actual_len);
Antonio Ospite66121552011-04-07 12:45:52 -0300200 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
201 } else {
202 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
203 }
204
205 sd->cam_tag++;
206
207 return actual_len;
208}
209
210static int write_register(struct gspca_dev *gspca_dev, uint16_t reg,
211 uint16_t data)
212{
213 uint16_t reply[2];
214 uint16_t cmd[2];
215 int res;
216
217 cmd[0] = cpu_to_le16(reg);
218 cmd[1] = cpu_to_le16(data);
219
220 PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
221 res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
222 if (res < 0)
223 return res;
224 if (res != 2) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300225 pr_warn("send_cmd returned %d [%04x %04x], 0000 expected\n",
226 res, reply[0], reply[1]);
Antonio Ospite66121552011-04-07 12:45:52 -0300227 }
228 return 0;
229}
230
231/* this function is called at probe time */
232static int sd_config(struct gspca_dev *gspca_dev,
233 const struct usb_device_id *id)
234{
235 struct sd *sd = (struct sd *) gspca_dev;
236 struct cam *cam;
237
238 sd->cam_tag = 0;
239
Antonio Ospitef1703aa2011-04-21 06:51:36 -0300240 /* Only video stream is supported for now,
Antonio Ospite66121552011-04-07 12:45:52 -0300241 * which has stream flag = 0x80 */
242 sd->stream_flag = 0x80;
243
244 cam = &gspca_dev->cam;
245
246 cam->cam_mode = video_camera_mode;
247 cam->nmodes = ARRAY_SIZE(video_camera_mode);
248
249#if 0
Antonio Ospitef1703aa2011-04-21 06:51:36 -0300250 /* Setting those values is not needed for video stream */
Antonio Ospite66121552011-04-07 12:45:52 -0300251 cam->npkt = 15;
252 gspca_dev->pkt_size = 960 * 2;
253#endif
254
255 return 0;
256}
257
258/* this function is called at probe and resume time */
259static int sd_init(struct gspca_dev *gspca_dev)
260{
261 PDEBUG(D_PROBE, "Kinect Camera device.");
262
263 return 0;
264}
265
266static int sd_start(struct gspca_dev *gspca_dev)
267{
268 int mode;
269 uint8_t fmt_reg, fmt_val;
270 uint8_t res_reg, res_val;
271 uint8_t fps_reg, fps_val;
272 uint8_t mode_val;
273
274 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
275
276 if (mode & FORMAT_Y10B) {
277 fmt_reg = 0x19;
278 res_reg = 0x1a;
279 fps_reg = 0x1b;
280 mode_val = 0x03;
281 } else {
282 fmt_reg = 0x0c;
283 res_reg = 0x0d;
284 fps_reg = 0x0e;
285 mode_val = 0x01;
286 }
287
288 /* format */
289 if (mode & FORMAT_UYVY)
290 fmt_val = 0x05;
291 else
292 fmt_val = 0x00;
293
294 if (mode & MODE_1280x1024)
295 res_val = 0x02;
296 else
297 res_val = 0x01;
298
299 if (mode & FPS_HIGH)
300 fps_val = 0x1e;
301 else
302 fps_val = 0x0f;
303
304
305 /* turn off IR-reset function */
306 write_register(gspca_dev, 0x105, 0x00);
307
308 /* Reset video stream */
309 write_register(gspca_dev, 0x05, 0x00);
310
311 /* Due to some ridiculous condition in the firmware, we have to start
312 * and stop the depth stream before the camera will hand us 1280x1024
313 * IR. This is a stupid workaround, but we've yet to find a better
314 * solution.
315 *
316 * Thanks to Drew Fisher for figuring this out.
317 */
318 if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
319 write_register(gspca_dev, 0x13, 0x01);
320 write_register(gspca_dev, 0x14, 0x1e);
321 write_register(gspca_dev, 0x06, 0x02);
322 write_register(gspca_dev, 0x06, 0x00);
323 }
324
325 write_register(gspca_dev, fmt_reg, fmt_val);
326 write_register(gspca_dev, res_reg, res_val);
327 write_register(gspca_dev, fps_reg, fps_val);
328
329 /* Start video stream */
330 write_register(gspca_dev, 0x05, mode_val);
331
332 /* disable Hflip */
333 write_register(gspca_dev, 0x47, 0x00);
334
335 return 0;
336}
337
338static void sd_stopN(struct gspca_dev *gspca_dev)
339{
340 /* reset video stream */
341 write_register(gspca_dev, 0x05, 0x00);
342}
343
344static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
345{
346 struct sd *sd = (struct sd *) gspca_dev;
347
348 struct pkt_hdr *hdr = (void *)__data;
349 uint8_t *data = __data + sizeof(*hdr);
350 int datalen = len - sizeof(*hdr);
351
352 uint8_t sof = sd->stream_flag | 1;
353 uint8_t mof = sd->stream_flag | 2;
354 uint8_t eof = sd->stream_flag | 5;
355
356 if (len < 12)
357 return;
358
359 if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
Joe Perches133a9fe2011-08-21 19:56:57 -0300360 pr_warn("[Stream %02x] Invalid magic %02x%02x\n",
361 sd->stream_flag, hdr->magic[0], hdr->magic[1]);
Antonio Ospite66121552011-04-07 12:45:52 -0300362 return;
363 }
364
365 if (hdr->flag == sof)
366 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
367
368 else if (hdr->flag == mof)
369 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
370
371 else if (hdr->flag == eof)
372 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
373
374 else
Joe Perches133a9fe2011-08-21 19:56:57 -0300375 pr_warn("Packet type not recognized...\n");
Antonio Ospite66121552011-04-07 12:45:52 -0300376}
377
378/* sub-driver description */
379static const struct sd_desc sd_desc = {
380 .name = MODULE_NAME,
381 .ctrls = sd_ctrls,
382 .nctrls = ARRAY_SIZE(sd_ctrls),
383 .config = sd_config,
384 .init = sd_init,
385 .start = sd_start,
386 .stopN = sd_stopN,
387 .pkt_scan = sd_pkt_scan,
388 /*
389 .querymenu = sd_querymenu,
390 .get_streamparm = sd_get_streamparm,
391 .set_streamparm = sd_set_streamparm,
392 */
393};
394
395/* -- module initialisation -- */
Jean-François Moinef7242d32011-05-17 03:56:09 -0300396static const struct usb_device_id device_table[] = {
Antonio Ospite66121552011-04-07 12:45:52 -0300397 {USB_DEVICE(0x045e, 0x02ae)},
398 {}
399};
400
401MODULE_DEVICE_TABLE(usb, device_table);
402
403/* -- device connect -- */
404static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
405{
406 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
407 THIS_MODULE);
408}
409
410static struct usb_driver sd_driver = {
411 .name = MODULE_NAME,
412 .id_table = device_table,
413 .probe = sd_probe,
414 .disconnect = gspca_disconnect,
415#ifdef CONFIG_PM
416 .suspend = gspca_suspend,
417 .resume = gspca_resume,
418#endif
419};
420
421/* -- module insert / remove -- */
422static int __init sd_mod_init(void)
423{
424 return usb_register(&sd_driver);
425}
426
427static void __exit sd_mod_exit(void)
428{
429 usb_deregister(&sd_driver);
430}
431
432module_init(sd_mod_init);
433module_exit(sd_mod_exit);