Support unloading the module built from the full kernel
[gspca_kinect.git] / kinect.c
index 1d7a74f..2028c64 100644 (file)
--- a/kinect.c
+++ b/kinect.c
@@ -1,11 +1,14 @@
 /*
  * kinect sensor device camera, gspca driver
  *
- * Copyright (C) 2010  Antonio Ospite <ospite@studenti.unina.it>
+ * Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
  *
- * Based on the OpenKinect project and libfreenect by Ector Martin
+ * Based on the OpenKinect project and libfreenect
  * http://openkinect.org/wiki/Init_Analysis
- * 
+ *
+ * Special thanks to Steven Toth and kernellabs.com for sponsoring a Kinect
+ * sensor device which I tested the driver on.
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -31,6 +34,11 @@ MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
 MODULE_DESCRIPTION("GSPCA/Kinect Sensor Device USB Camera Driver");
 MODULE_LICENSE("GPL");
 
+#ifdef DEBUG
+int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
+       D_USBI | D_USBO | D_V4L2;
+#endif
+
 struct pkt_hdr {
        uint8_t magic[2];
        uint8_t pad;
@@ -42,20 +50,20 @@ struct pkt_hdr {
        uint32_t timestamp;
 };
 
-typedef struct {
+struct cam_hdr {
        uint8_t magic[2];
        uint16_t len;
        uint16_t cmd;
        uint16_t tag;
-} cam_hdr;
-
-int gspca_debug = D_ERR | D_PROBE | D_CONF | D_STREAM | D_FRAM | D_PACK |
-       D_USBI | D_USBO | D_V4L2;
+};
 
 /* specific webcam descriptor */
 struct sd {
-       struct gspca_dev gspca_dev;     /* !! must be the first item */
-       uint16_t cam_tag;
+       struct gspca_dev gspca_dev; /* !! must be the first item */
+       uint16_t cam_tag;           /* a sequence number for packets */
+       uint8_t stream_flag;        /* to identify different stream types */
+       uint8_t obuf[0x400];        /* output buffer for control commands */
+       uint8_t ibuf[0x200];        /* input buffer for control commands */
 };
 
 /* V4L2 controls supported by the driver */
@@ -65,14 +73,16 @@ static const struct ctrl sd_ctrls[] = {
 };
 
 #define MODE_640x480   0x0001
-#define MODE_1280x1024 0x0002
+#define MODE_640x488   0x0002
+#define MODE_1280x1024 0x0004
 
 #define FORMAT_BAYER   0x0010
-#define FORMAT_UYUV    0x0020
+#define FORMAT_UYVY    0x0020
+#define FORMAT_Y10B    0x0040
 
 #define FPS_HIGH       0x0100
 
-static const struct v4l2_pix_format color_camera_mode[] = {
+static const struct v4l2_pix_format video_camera_mode[] = {
        {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
         .bytesperline = 640,
         .sizeimage = 640 * 480,
@@ -82,15 +92,26 @@ static const struct v4l2_pix_format color_camera_mode[] = {
         .bytesperline = 640 * 2,
         .sizeimage = 640 * 480 * 2,
         .colorspace = V4L2_COLORSPACE_SRGB,
-        .priv = MODE_640x480 | FORMAT_UYUV},
+        .priv = MODE_640x480 | FORMAT_UYVY},
        {1280, 1024, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
         .bytesperline = 1280,
         .sizeimage = 1280 * 1024,
         .colorspace = V4L2_COLORSPACE_SRGB,
         .priv = MODE_1280x1024 | FORMAT_BAYER},
+       {640, 488, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
+        .bytesperline = 640 * 10 / 8,
+        .sizeimage =  640 * 488 * 10 / 8,
+        .colorspace = V4L2_COLORSPACE_SRGB,
+        .priv = MODE_640x488 | FORMAT_Y10B | FPS_HIGH},
+       {1280, 1024, V4L2_PIX_FMT_Y10BPACK, V4L2_FIELD_NONE,
+        .bytesperline = 1280 * 10 / 8,
+        .sizeimage =  1280 * 1024 * 10 / 8,
+        .colorspace = V4L2_COLORSPACE_SRGB,
+        .priv = MODE_1280x1024 | FORMAT_Y10B},
 };
 
-static int kinect_write(struct usb_device *udev, uint8_t *data, uint16_t wLength)
+static int kinect_write(struct usb_device *udev, uint8_t *data,
+                       uint16_t wLength)
 {
        return usb_control_msg(udev,
                              usb_sndctrlpipe(udev, 0),
@@ -114,10 +135,10 @@ static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
        struct sd *sd = (struct sd *) gspca_dev;
        struct usb_device *udev = gspca_dev->dev;
        int res, actual_len;
-       uint8_t obuf[0x400];
-       uint8_t ibuf[0x200];
-       cam_hdr *chdr = (void*)obuf;
-       cam_hdr *rhdr = (void*)ibuf;
+       uint8_t *obuf = sd->obuf;
+       uint8_t *ibuf = sd->ibuf;
+       struct cam_hdr *chdr = (void *)obuf;
+       struct cam_hdr *rhdr = (void *)ibuf;
 
        if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
                err("send_cmd: Invalid command length (0x%x)", cmd_len);
@@ -133,7 +154,8 @@ static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
        memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
 
        res = kinect_write(udev, obuf, cmd_len + sizeof(*chdr));
-       info("Control cmd=%04x tag=%04x len=%04x: %d", cmd, sd->cam_tag, cmd_len, res);
+       PDEBUG(D_USBO, "Control cmd=%04x tag=%04x len=%04x: %d", cmd,
+               sd->cam_tag, cmd_len, res);
        if (res < 0) {
                err("send_cmd: Output control transfer failed (%d)", res);
                return res;
@@ -142,7 +164,7 @@ static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
        do {
                actual_len = kinect_read(udev, ibuf, 0x200);
        } while (actual_len == 0);
-       info("Control reply: %d", res);
+       PDEBUG(D_USBO, "Control reply: %d", res);
        if (actual_len < sizeof(*rhdr)) {
                err("send_cmd: Input control transfer failed (%d)", res);
                return res;
@@ -150,7 +172,8 @@ static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
        actual_len -= sizeof(*rhdr);
 
        if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
-               err("send_cmd: Bad magic %02x %02x", rhdr->magic[0], rhdr->magic[1]);
+               err("send_cmd: Bad magic %02x %02x", rhdr->magic[0],
+                       rhdr->magic[1]);
                return -1;
        }
        if (rhdr->cmd != chdr->cmd) {
@@ -180,7 +203,8 @@ static int send_cmd(struct gspca_dev *gspca_dev, uint16_t cmd, void *cmdbuf,
        return actual_len;
 }
 
-static int write_register(struct gspca_dev *gspca_dev, uint16_t reg, uint16_t data)
+static int write_register(struct gspca_dev *gspca_dev, uint16_t reg,
+                       uint16_t data)
 {
        uint16_t reply[2];
        uint16_t cmd[2];
@@ -209,13 +233,20 @@ static int sd_config(struct gspca_dev *gspca_dev,
 
        sd->cam_tag = 0;
 
+       /* Only video stream is supported for now,
+        * which has stream flag = 0x80 */
+       sd->stream_flag = 0x80;
+
        cam = &gspca_dev->cam;
 
-       cam->cam_mode = color_camera_mode;
-       cam->nmodes = ARRAY_SIZE(color_camera_mode);
+       cam->cam_mode = video_camera_mode;
+       cam->nmodes = ARRAY_SIZE(video_camera_mode);
 
-       //cam->npkt = 15;
-       //gspca_dev->pkt_size = 960 * 2;
+#if 0
+       /* Setting those values is not needed for video stream */
+       cam->npkt = 15;
+       gspca_dev->pkt_size = 960 * 2;
+#endif
 
        return 0;
 }
@@ -234,15 +265,24 @@ static int sd_start(struct gspca_dev *gspca_dev)
        uint8_t fmt_reg, fmt_val;
        uint8_t res_reg, res_val;
        uint8_t fps_reg, fps_val;
+       uint8_t mode_val;
 
        mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
 
-       fmt_reg = 0x0c;
-       res_reg = 0x0d;
-       fps_reg = 0x0e;
+       if (mode & FORMAT_Y10B) {
+               fmt_reg = 0x19;
+               res_reg = 0x1a;
+               fps_reg = 0x1b;
+               mode_val = 0x03;
+       } else {
+               fmt_reg = 0x0c;
+               res_reg = 0x0d;
+               fps_reg = 0x0e;
+               mode_val = 0x01;
+       }
 
        /* format */
-       if (mode & FORMAT_UYUV)
+       if (mode & FORMAT_UYVY)
                fmt_val = 0x05;
        else
                fmt_val = 0x00;
@@ -257,15 +297,33 @@ static int sd_start(struct gspca_dev *gspca_dev)
        else
                fps_val = 0x0f;
 
+
+       /* turn off IR-reset function */
+       write_register(gspca_dev, 0x105, 0x00);
+
        /* Reset video stream */
        write_register(gspca_dev, 0x05, 0x00);
 
+       /* Due to some ridiculous condition in the firmware, we have to start
+        * and stop the depth stream before the camera will hand us 1280x1024
+        * IR.  This is a stupid workaround, but we've yet to find a better
+        * solution.
+        *
+        * Thanks to Drew Fisher for figuring this out.
+        */
+       if (mode & (FORMAT_Y10B | MODE_1280x1024)) {
+               write_register(gspca_dev, 0x13, 0x01);
+               write_register(gspca_dev, 0x14, 0x1e);
+               write_register(gspca_dev, 0x06, 0x02);
+               write_register(gspca_dev, 0x06, 0x00);
+       }
+
        write_register(gspca_dev, fmt_reg, fmt_val);
        write_register(gspca_dev, res_reg, res_val);
        write_register(gspca_dev, fps_reg, fps_val);
 
        /* Start video stream */
-       write_register(gspca_dev, 0x05, 0x01);
+       write_register(gspca_dev, 0x05, mode_val);
 
        /* disable Hflip */
        write_register(gspca_dev, 0x47, 0x00);
@@ -275,27 +333,27 @@ static int sd_start(struct gspca_dev *gspca_dev)
 
 static void sd_stopN(struct gspca_dev *gspca_dev)
 {
-       write_register(gspca_dev, 0x05, 0x00); // reset video stream
+       /* reset video stream */
+       write_register(gspca_dev, 0x05, 0x00);
 }
 
 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
 {
-       //struct sd *sd = (struct sd *) gspca_dev;
-       
-       struct pkt_hdr *hdr = (void*)__data;
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       struct pkt_hdr *hdr = (void *)__data;
        uint8_t *data = __data + sizeof(*hdr);
        int datalen = len - sizeof(*hdr);
 
-       /* strm->flag == 0x80 for the color camera stream */
-       uint8_t sof = 0x80 | 1;
-       uint8_t mof = 0x80 | 2;
-       uint8_t eof = 0x80 | 5;
+       uint8_t sof = sd->stream_flag | 1;
+       uint8_t mof = sd->stream_flag | 2;
+       uint8_t eof = sd->stream_flag | 5;
 
        if (len < 12)
                return;
 
        if (hdr->magic[0] != 'R' || hdr->magic[1] != 'B') {
-               warn("[Stream %02x] Invalid magic %02x%02x", 0x80, //strm->flag
+               warn("[Stream %02x] Invalid magic %02x%02x", sd->stream_flag,
                                hdr->magic[0], hdr->magic[1]);
                return;
        }
@@ -306,11 +364,11 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *__data, int len)
        else if (hdr->flag == mof)
                gspca_frame_add(gspca_dev, INTER_PACKET, data, datalen);
 
-       else if(hdr->flag == eof)
+       else if (hdr->flag == eof)
                gspca_frame_add(gspca_dev, LAST_PACKET, data, datalen);
 
        else
-               warn("Not recognized...");
+               warn("Packet type not recognized...");
 }
 
 /* sub-driver description */