Support unloading the module built from the full kernel
[gspca_kinect.git] / kinect.c
index f8dcbeb..2028c64 100644 (file)
--- a/kinect.c
+++ b/kinect.c
@@ -5,7 +5,10 @@
  *
  * 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
@@ -47,18 +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;
+};
 
 /* specific webcam descriptor */
 struct sd {
        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 steram types */
+       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 */
@@ -105,7 +110,8 @@ static const struct v4l2_pix_format video_camera_mode[] = {
         .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),
@@ -129,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);
@@ -148,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));
-       PDEBUG(D_USBO, "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;
@@ -165,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) {
@@ -195,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];
@@ -224,7 +233,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
 
        sd->cam_tag = 0;
 
-       /* Only color camera is supported for now,
+       /* Only video stream is supported for now,
         * which has stream flag = 0x80 */
        sd->stream_flag = 0x80;
 
@@ -234,7 +243,7 @@ static int sd_config(struct gspca_dev *gspca_dev,
        cam->nmodes = ARRAY_SIZE(video_camera_mode);
 
 #if 0
-       /* Setting those values is not needed for color camera */
+       /* Setting those values is not needed for video stream */
        cam->npkt = 15;
        gspca_dev->pkt_size = 960 * 2;
 #endif
@@ -331,8 +340,8 @@ static void sd_stopN(struct gspca_dev *gspca_dev)
 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 pkt_hdr *hdr = (void *)__data;
        uint8_t *data = __data + sizeof(*hdr);
        int datalen = len - sizeof(*hdr);
 
@@ -355,7 +364,7 @@ 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