2 * kinect sensor device camera, gspca driver
4 * Copyright (C) 2010 Antonio Ospite <ospite@studenti.unina.it>
6 * Based on the OpenKinect project and libfreenect by Ector Martin
7 * http://openkinect.org/wiki/Init_Analysis
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
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.
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
24 #define MODULE_NAME "kinect"
28 #define CTRL_TIMEOUT 500
30 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
31 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
32 MODULE_LICENSE("GPL");
52 int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
53 D_USBI | D_USBO | D_V4L2;
55 /* specific webcam descriptor */
57 struct gspca_dev gspca_dev; /* !! must be the first item */
61 /* V4L2 controls supported by the driver */
62 /* controls prototypes here */
64 static const struct ctrl sd_ctrls[] = {
67 static const struct v4l2_pix_format color_camera_mode[] = {
68 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
70 .sizeimage = 640 * 480,
71 .colorspace = V4L2_COLORSPACE_SRGB,
75 static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
77 return usb_control_msg(udev,
78 usb_sndctrlpipe(udev, 0),
80 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
81 0, 0, data, wLength, CTRL_TIMEOUT);
84 static int kinect_read(struct usb_device *udev, uint8_t *data, uint16_t wLength)
86 return usb_control_msg(udev,
87 usb_rcvctrlpipe(udev, 0),
89 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 0, 0, data, wLength, CTRL_TIMEOUT);
93 static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
94 unsigned int cmd_len, void *replybuf, unsigned int reply_len)
96 struct sd *sd = (struct sd *) gspca_dev;
97 struct usb_device *udev = gspca_dev->dev;
101 cam_hdr *chdr = (void*)obuf;
102 cam_hdr *rhdr = (void*)ibuf;
104 if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
105 err("send_cmd: Invalid command length (0x%x)", cmd_len);
109 chdr->magic[0] = 0x47;
110 chdr->magic[1] = 0x4d;
111 chdr->cmd = cpu_to_le16(cmd);
112 chdr->tag = cpu_to_le16(sd->cam_tag);
113 chdr->len = cpu_to_le16(cmd_len / 2);
115 memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
117 res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
118 info("Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
120 err("send_cmd: Output control transfer failed (%d)", res);
125 actual_len = kinect_read(udev, ibuf, 0x200);
126 } while (actual_len == 0);
127 info("Control reply: %d", res);
128 if (actual_len < sizeof(*rhdr)) {
129 err("send_cmd: Input control transfer failed (%d)", res);
132 actual_len -= sizeof(*rhdr);
134 if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
135 err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
138 if (rhdr->cmd != chdr->cmd) {
139 err("send_cmd: Bad cmd %02x != %02x", rhdr->cmd, chdr->cmd);
142 if (rhdr->tag != chdr->tag) {
143 err("send_cmd: Bad tag %04x != %04x", rhdr->tag, chdr->tag);
146 if (cpu_to_le16(rhdr->len) != (actual_len/2)) {
147 err("send_cmd: Bad len %04x != %04x",
148 cpu_to_le16(rhdr->len), (int)(actual_len/2));
152 if (actual_len > reply_len) {
153 warn("send_cmd: Data buffer is %d bytes long, but got %d bytes",
154 reply_len, actual_len);
155 memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
157 memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
165 static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
171 cmd[0] = cpu_to_le16(reg);
172 cmd[1] = cpu_to_le16(data);
174 PDEBUG(D_USBO, "Write Reg 0x%04x <= 0x%02x", reg, data);
175 res = send_cmd(gspca_dev, 0x03, cmd, 4, reply, 4);
179 warn("send_cmd returned %d [%04x %04x], 0000 expected",
180 res, reply[0], reply[1]);
185 /* this function is called at probe time */
186 static int sd_config(struct gspca_dev *gspca_dev,
187 const struct usb_device_id *id)
189 struct sd *sd = (struct sd *) gspca_dev;
194 cam = &gspca_dev->cam;
196 cam->cam_mode = color_camera_mode;
197 cam->nmodes = ARRAY_SIZE(color_camera_mode);
200 //gspca_dev->pkt_size = 960 * 2;
205 /* this function is called at probe and resume time */
206 static int sd_init(struct gspca_dev *gspca_dev)
208 PDEBUG(D_PROBE, "Kinect Camera device.");
213 static int sd_start(struct gspca_dev *gspca_dev)
215 write_register(gspca_dev, 0x05, 0x00); // reset video stream
216 write_register(gspca_dev, 0x0c, 0x00);
217 write_register(gspca_dev, 0x0d, 0x01);
218 write_register(gspca_dev, 0x0e, 0x1e); // 30Hz bayer
220 /* Video Format: Raw bayer data */
221 write_register(gspca_dev, 0x05, 0x01); // start video stream
223 write_register(gspca_dev, 0x47, 0x00); // disable Hflip
228 static void sd_stopN(struct gspca_dev *gspca_dev)
230 write_register(gspca_dev, 0x05, 0x00); // reset video stream
233 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
235 //struct sd *sd = (struct sd *) gspca_dev;
237 struct pkt_hdr *hdr = (void*)__data;
238 uint8_t *data = __data + sizeof(*hdr);
239 int datalen = len - sizeof(*hdr);
241 /* strm->flag == 0x80 for the color camera stream */
242 uint8_t sof = 0x80 | 1;
243 uint8_t mof = 0x80 | 2;
244 uint8_t eof = 0x80 | 5;
249 if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
250 warn("[Stream %02x] Invalid magic %02x%02x", 0x80, //strm->flag
251 hdr->magic[0], hdr->magic[1]);
255 if (hdr->flag == sof)
256 gspca_frame_add(gspca_dev, FIRST_PACKET, data, datalen);
258 else if (hdr->flag == mof)
259 gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
261 else if(hdr->flag == eof)
262 gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
265 warn("Not recognized...");
268 /* sub-driver description */
269 static const struct sd_desc sd_desc = {
272 .nctrls = ARRAY_SIZE(sd_ctrls),
277 .pkt_scan = sd_pkt_scan,
279 .querymenu = sd_querymenu,
280 .get_streamparm = sd_get_streamparm,
281 .set_streamparm = sd_set_streamparm,
285 /* -- module initialisation -- */
286 static const __devinitdata struct usb_device_id device_table[] = {
287 {USB_DEVICE(0x045e, 0x02ae)},
291 MODULE_DEVICE_TABLE(usb, device_table);
293 /* -- device connect -- */
294 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
296 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
300 static struct usb_driver sd_driver = {
302 .id_table = device_table,
304 .disconnect = gspca_disconnect,
306 .suspend = gspca_suspend,
307 .resume = gspca_resume,
311 /* -- module insert / remove -- */
312 static int __init sd_mod_init(void)
314 return usb_register(&sd_driver);
317 static void __exit sd_mod_exit(void)
319 usb_deregister(&sd_driver);
322 module_init(sd_mod_init);
323 module_exit(sd_mod_exit);