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