Merge tag 'v0.4' into debian
authorAntonio Ospite <ao2@ao2.it>
Thu, 7 Jan 2016 16:36:24 +0000 (17:36 +0100)
committerAntonio Ospite <ao2@ao2.it>
Thu, 7 Jan 2016 16:36:24 +0000 (17:36 +0100)
Release v0.4

ChangeLog [deleted file]
NEWS [new file with mode: 0644]
kinect_upload_fw/Makefile
kinect_upload_fw/kinect_upload_fw.c

diff --git a/ChangeLog b/ChangeLog
deleted file mode 100644 (file)
index bb7db37..0000000
--- a/ChangeLog
+++ /dev/null
@@ -1,203 +0,0 @@
-2013-12-06 17:46:39 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * ChangeLog: refresh for v0.3 (HEAD, tag: v0.3, master)
-
-2013-12-06 17:45:36 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Makefile: remove a trailing space
-
-2013-12-06 17:43:22 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Makefile: don't create the empty dir /lib/firmware/kinect
-
-2013-12-06 17:42:43 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw/Makefile: fix double spacing
-
-2013-12-06 17:36:12 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw/endian.c: fix warning and rename a function
-
-2013-11-19 16:16:18 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * README: reword and re-indent some paragraphs (origin/master, origin/HEAD)
-
-2013-11-19 16:02:39 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Makefile: misc fixes
-
-2012-02-23 22:48:22 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: initialize 'transferred' before USB transfers
-
-2012-02-03 13:12:01 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * README: acknowledge Steven Toth's sponsorship
-
-2011-12-28 12:17:52 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: mention that p7zip-full >= 9.20 is needed
-
-2011-12-28 12:14:06 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * README: mention reloading udev rules
-
-2011-11-27 12:16:20 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Add ChangeLog for v0.2 (tag: v0.2)
-
-2011-11-27 10:23:30 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: tell where we are getting the firmware from
-
-2011-11-27 10:11:51 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * README: tell where we are getting the actual firmware from
-
-2011-11-27 10:10:28 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * README: fix a typo and add some clarifications
-
-2011-11-27 09:59:04 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: make the script less verbose when invoking 7z
-
-2011-11-27 09:46:38 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: download the latest KinectSDK-v1.0-beta2-x86.msi
-
-2011-11-27 09:45:54 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: mention wget in the internal documentation
-
-2011-11-27 00:34:57 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: specify libraries after objects in the linking command
-
-2011-11-05 00:10:30 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Add info about building kinect_upload_fw for MS Windows
-
-2011-11-04 23:59:19 +0100  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Update .gitignore
-
-2011-10-21 22:19:37 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix reading the firmware file on Windows
-
-2011-10-14 21:57:12 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix a mingw32 compilation error
-
-2011-10-13 23:35:10 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix generating endian.h before compiling kinect_upload_fw
-
-2011-10-13 23:31:11 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: disable some compiler flags
-
-2011-10-12 12:13:34 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Add ChangeLog for v0.1 (tag: v0.1)
-
-2011-10-06 14:49:01 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix a couple of minor warnings from 'sparse'
-
-2011-10-03 16:22:36 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: support architectures that are not little-endian
-
-2011-10-03 15:55:28 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: add a mechanism to check the integrity of the downloaded file
-
-2011-10-03 15:47:53 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: print error messages on stderr
-
-2011-09-30 23:00:42 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: really fix -Wformat warnings about sizeof()
-
-2011-09-30 13:59:17 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix issues shown by valgrind --leak-check=full --show-reachable=yes
-
-2011-09-30 13:43:52 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: cosmetic changes
-
-2011-09-30 13:38:42 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: print more meaningful error message on failure
-
-2011-09-30 13:26:48 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: return value from $() does not need to be quoted
-
-2011-09-30 13:21:06 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_fetch_fw: make FW_DESTDIR an absolute path
-
-2011-09-30 12:54:46 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Change description of kinect_fetch_fw
-
-2011-09-30 11:31:01 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Add more info in the README file
-
-2011-09-29 23:20:26 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix a -Wstrict-aliasing warning
-
-2011-09-29 23:13:09 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix assigning a string literal to a 'char *'
-
-2011-09-29 23:11:21 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix -Wsign-compare and -Wpointer-sign
-
-2011-09-29 23:09:48 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix -Wformat warnings
-
-2011-09-29 23:05:58 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix -Wmissing-prototypes warning
-
-2011-09-29 23:04:24 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix -Wold-style-definition warnings
-
-2011-09-29 22:06:26 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: fix spacing in Makefile, use simple expansion to set bindir
-
-2011-09-29 22:05:35 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: enable more warnings in Makefile
-
-2011-09-17 21:46:55 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * kinect_upload_fw: remove some trailing spaces
-
-2011-09-29 18:19:23 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Makefile: Add a rule to generate a GNU style Changelog
-
-2011-09-12 00:49:16 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Add a .gitignore file
-
-2011-09-11 20:42:39 +0200  Antonio Ospite  <ospite@studenti.unina.it>
-
-       * Initial import of the other pieces of knect_audio_setup
-
-2011-09-11 20:35:19 +0200  Drew Fisher  <drew.m.fisher@gmail.com>
-
-       * Initial import of kinect_upload_fw
diff --git a/NEWS b/NEWS
new file mode 100644 (file)
index 0000000..abd781d
--- /dev/null
+++ b/NEWS
@@ -0,0 +1,6 @@
+News for v0.4:
+==============
+
+  * kinect_upload_fw: fix setting the USB configuration for some devices
+  * kinect_upload_fw: make the code more robust
+  * Drop the Changelog file and use a NEWS file
index 15741d3..c8bbc86 100644 (file)
@@ -30,9 +30,12 @@ CFLAGS += -fno-common \
   -Wswitch-enum \
   -Wundef \
   -Wunreachable-code \
-  -Wunsafe-loop-optimizations \
   -Wwrite-strings
 
+ifneq ($(CC),clang)
+  CFLAGS += -Wunsafe-loop-optimizations
+endif
+
 CFLAGS += $(shell pkg-config --cflags libusb-1.0)
 LDLIBS += $(shell pkg-config --libs libusb-1.0)
 
index b2188d6..b258ba0 100644 (file)
 
 #include "endian.h"
 
+#define KINECT_AUDIO_VID           0x045e
+#define KINECT_AUDIO_PID           0x02ad
+#define KINECT_AUDIO_CONFIGURATION 1
+#define KINECT_AUDIO_INTERFACE     0
+#define KINECT_AUDIO_IN_EP         0x81
+#define KINECT_AUDIO_OUT_EP        0x01
+
 static libusb_device_handle *dev;
 static unsigned int seq;
 
@@ -77,8 +84,9 @@ static void dump_bl_cmd(bootloader_command cmd) {
 static int get_first_reply(void) {
        unsigned char buffer[512];
        int res;
+
        int transferred = 0;
-       res = libusb_bulk_transfer(dev, 0x81, buffer, 512, &transferred, 0);
+       res = libusb_bulk_transfer(dev, KINECT_AUDIO_IN_EP, buffer, 512, &transferred, 0);
        if (res != 0 ) {
                LOG("Error reading first reply: %d\ttransferred: %d (expected %d)\n", res, transferred, 0x60);
                return res;
@@ -103,7 +111,7 @@ static int get_reply(void) {
        int res;
        int transferred = 0;
 
-       res = libusb_bulk_transfer(dev, 0x81, reply.dump, 512, &transferred, 0);
+       res = libusb_bulk_transfer(dev, KINECT_AUDIO_IN_EP, reply.dump, 512, &transferred, 0);
        if (res != 0 || transferred != sizeof(status_code)) {
                LOG("Error reading reply: %d\ttransferred: %d (expected %zu)\n", res, transferred, sizeof(status_code));
                return res;
@@ -130,33 +138,8 @@ static int get_reply(void) {
        return res;
 }
 
-int main(int argc, char** argv) {
-       char default_filename[] = "firmware.bin";
-       char* filename = default_filename;
-       int res = 0;
-
-       if (argc == 2) {
-               filename = argv[1];
-       }
-
-       FILE* fw = fopen(filename, "rb");
-       if (fw == NULL) {
-               fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
-               return errno;
-       }
-
-       libusb_init(NULL);
-       libusb_set_debug(NULL, 3);
-
-       dev = libusb_open_device_with_vid_pid(NULL, 0x045e, 0x02ad);
-       if (dev == NULL) {
-               fprintf(stderr, "Couldn't open device.\n");
-               res = -ENODEV;
-               goto fail_libusb_open;
-       }
-
-       libusb_set_configuration(dev, 1);
-       libusb_claim_interface(dev, 0);
+static int upload_firmware(FILE *fw) {
+       int res;
 
        seq = 1;
 
@@ -173,13 +156,27 @@ int main(int argc, char** argv) {
 
        int transferred = 0;
 
-       res = libusb_bulk_transfer(dev, 1, (unsigned char*)&cmd, sizeof(cmd), &transferred, 0);
+       res = libusb_bulk_transfer(dev, KINECT_AUDIO_OUT_EP, (unsigned char *)&cmd, sizeof(cmd), &transferred, 0);
        if (res != 0 || transferred != sizeof(cmd)) {
                LOG("Error: res: %d\ttransferred: %d (expected %zu)\n", res, transferred, sizeof(cmd));
-               goto cleanup;
+               goto out;
+       }
+
+       // This first one doesn't have the usual magic bytes at the beginning,
+       // and is 96 bytes long - much longer than the usual 12-byte replies.
+       res = get_first_reply();
+       if (res < 0) {
+               LOG("get_first_reply() failed");
+               goto out;
+       }
+
+       // I'm not sure why we do this twice here, but maybe it'll make sense
+       // later.
+       res = get_reply();
+       if (res < 0) {
+               LOG("First get_reply() failed");
+               goto out;
        }
-       res = get_first_reply(); // This first one doesn't have the usual magic bytes at the beginning, and is 96 bytes long - much longer than the usual 12-byte replies.
-       res = get_reply(); // I'm not sure why we do this twice here, but maybe it'll make sense later.
        seq++;
 
        // Split addr declaration and assignment in order to compile as C++,
@@ -190,36 +187,40 @@ int main(int argc, char** argv) {
        unsigned char page[0x4000];
        int read;
        do {
-               read = fread(page, 1, 0x4000, fw);
+               read = (int)fread(page, 1, 0x4000, fw);
                if (read <= 0) {
                        break;
                }
                //LOG("");
                cmd.seq = fn_le32(seq);
-               cmd.bytes = fn_le32(read);
+               cmd.bytes = fn_le32((unsigned int)read);
                cmd.cmd = fn_le32(0x03);
                cmd.write_addr = fn_le32(addr);
                LOG("About to send: ");
                dump_bl_cmd(cmd);
                // Send it off!
                transferred = 0;
-               res = libusb_bulk_transfer(dev, 1, (unsigned char*)&cmd, sizeof(cmd), &transferred, 0);
+               res = libusb_bulk_transfer(dev, KINECT_AUDIO_OUT_EP, (unsigned char *)&cmd, sizeof(cmd), &transferred, 0);
                if (res != 0 || transferred != sizeof(cmd)) {
                        LOG("Error: res: %d\ttransferred: %d (expected %zu)\n", res, transferred, sizeof(cmd));
-                       goto cleanup;
+                       goto out;
                }
                int bytes_sent = 0;
                while (bytes_sent < read) {
                        int to_send = (read - bytes_sent > 512 ? 512 : read - bytes_sent);
                        transferred = 0;
-                       res = libusb_bulk_transfer(dev, 1, &page[bytes_sent], to_send, &transferred, 0);
+                       res = libusb_bulk_transfer(dev, KINECT_AUDIO_OUT_EP, &page[bytes_sent], to_send, &transferred, 0);
                        if (res != 0 || transferred != to_send) {
                                LOG("Error: res: %d\ttransferred: %d (expected %d)\n", res, transferred, to_send);
-                               goto cleanup;
+                               goto out;
                        }
                        bytes_sent += to_send;
                }
                res = get_reply();
+               if (res < 0) {
+                       LOG("get_reply failed");
+                       goto out;
+               }
 
                addr += (uint32_t)read;
                seq++;
@@ -231,19 +232,108 @@ int main(int argc, char** argv) {
        cmd.write_addr = fn_le32(0x00080030);
        dump_bl_cmd(cmd);
        transferred = 0;
-       res = libusb_bulk_transfer(dev, 1, (unsigned char*)&cmd, sizeof(cmd), &transferred, 0);
+       res = libusb_bulk_transfer(dev, KINECT_AUDIO_OUT_EP, (unsigned char *)&cmd, sizeof(cmd), &transferred, 0);
        if (res != 0 || transferred != sizeof(cmd)) {
                LOG("Error: res: %d\ttransferred: %d (expected %zu)\n", res, transferred, sizeof(cmd));
-               goto cleanup;
+               goto out;
        }
        res = get_reply();
        seq++;
+
+out:
+       return res;
+}
+
+int main(int argc, char *argv[]) {
+       char default_filename[] = "firmware.bin";
+       char *filename = default_filename;
+       int ret = 0;
+
+       if (argc == 2) {
+               filename = argv[1];
+       }
+
+       FILE *fw = fopen(filename, "rb");
+       if (fw == NULL) {
+               fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
+               return -errno;
+       }
+
+       ret = libusb_init(NULL);
+       if (ret < 0) {
+               fprintf(stderr, "libusb_init failed: %s\n",
+                       libusb_error_name(ret));
+               goto out;
+       }
+
+       libusb_set_debug(NULL, 3);
+
+       dev = libusb_open_device_with_vid_pid(NULL, KINECT_AUDIO_VID, KINECT_AUDIO_PID);
+       if (dev == NULL) {
+               fprintf(stderr, "libusb_open failed: %s\n", strerror(errno));
+               ret = -errno;
+               goto out_libusb_exit;
+       }
+
+       int current_configuration = -1;
+       ret = libusb_get_configuration(dev, &current_configuration);
+       if (ret < 0) {
+               fprintf(stderr, "libusb_get_configuration failed: %s\n",
+                       libusb_error_name(ret));
+               goto out_libusb_close;
+       }
+
+       if (current_configuration != KINECT_AUDIO_CONFIGURATION) {
+               ret = libusb_set_configuration(dev, KINECT_AUDIO_CONFIGURATION);
+               if (ret < 0) {
+                       fprintf(stderr, "libusb_set_configuration failed: %s\n",
+                               libusb_error_name(ret));
+                       fprintf(stderr, "Cannot set configuration %d\n",
+                               KINECT_AUDIO_CONFIGURATION);
+                       goto out_libusb_close;
+               }
+       }
+
+       libusb_set_auto_detach_kernel_driver(dev, 1);
+
+       ret = libusb_claim_interface(dev, KINECT_AUDIO_INTERFACE);
+       if (ret < 0) {
+               fprintf(stderr, "libusb_claim_interface failed: %s\n",
+                       libusb_error_name(ret));
+               fprintf(stderr, "Cannot claim interface %d\n",
+                       KINECT_AUDIO_INTERFACE);
+               goto out_libusb_close;
+       }
+
+       /* 
+        * Checking that the configuration has not changed, as suggested in
+        * http://libusb.sourceforge.net/api-1.0/caveats.html
+        */
+       current_configuration = -1;
+       ret = libusb_get_configuration(dev, &current_configuration);
+       if (ret < 0) {
+               fprintf(stderr, "libusb_get_configuration after claim failed: %s\n",
+                       libusb_error_name(ret));
+               goto out_libusb_release_interface;
+       }
+
+       if (current_configuration != KINECT_AUDIO_CONFIGURATION) {
+               fprintf(stderr, "libusb configuration changed (expected: %d, current: %d)\n",
+                       KINECT_AUDIO_CONFIGURATION, current_configuration);
+               ret = -EINVAL;
+               goto out_libusb_release_interface;
+       }
+
+       ret = upload_firmware(fw);
        // Now the device reenumerates.
 
-cleanup:
+out_libusb_release_interface:
+       libusb_release_interface(dev, KINECT_AUDIO_INTERFACE);
+out_libusb_close:
        libusb_close(dev);
-fail_libusb_open:
+out_libusb_exit:
        libusb_exit(NULL);
+out:
        fclose(fw);
-       return res;
+       return ret;
 }