+static int jtagkey_set_bbmode(unsigned char mode) {
+ int ret = 0;
+
+ if (bitbang_mode != mode) {
+ DPRINTF("switching bitbang-mode!\n");
+
+ /* Wait for the latency-timer to kick in */
+ usleep(2);
+ if ((ret = ftdi_set_bitmode(&ftdic, JTAGKEY_TCK|JTAGKEY_TDI|JTAGKEY_TMS|JTAGKEY_OEn, mode)) != 0) {
+ fprintf(stderr, "unable to enable bitbang mode: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
+ return ret;
+ }
+ if ((ret = ftdi_usb_purge_buffers(&ftdic)) != 0) {
+ fprintf(stderr, "unable to purge buffers: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
+ return ret;
+ }
+ /* Wait for the FTDI2232 to settle */
+ usleep(2);
+
+ bitbang_mode = mode;
+ }
+
+ return ret;
+}
+