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