Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save monwarez/7c55bb8239929fdb52f452d5c1d12a93 to your computer and use it in GitHub Desktop.
Save monwarez/7c55bb8239929fdb52f452d5c1d12a93 to your computer and use it in GitHub Desktop.
Diff for libusbsio FreeBSD port
From 6da5bec9ab40f1ea17bfb1812cacd6ffbd854d0c Mon Sep 17 00:00:00 2001
From: Thibault Payet <[email protected]>
Date: Sun, 30 Apr 2023 15:13:02 +0200
Subject: [PATCH] Use FreeBSD version of hidapi instead of the bundled one
---
makefile | 19 +++++-
python/libusbsio/libusbsio.py | 10 +++
src/hid_api/freebsd/hid.c | 102 ++++++++++++++++++++++++++++++
src/hid_api/freebsd/hidapi_mock.h | 55 ++++++++++++++++
src/lpcusbsio.c | 4 ++
5 files changed, 188 insertions(+), 2 deletions(-)
create mode 100644 src/hid_api/freebsd/hid.c
create mode 100644 src/hid_api/freebsd/hidapi_mock.h
diff --git a/makefile b/makefile
index 5d7d3a6..3009815 100644
--- a/makefile
+++ b/makefile
@@ -6,15 +6,20 @@
# NXP USBSIO Library Makefile
#
-CC = gcc
-CXX = g++
+CC ?= gcc
+CXX ?= g++
AR = ar -rcs
UNAME := $(shell uname)
UNAME_M := $(shell uname -m)
VPATH := src
+ifeq ($(UNAME), FreeBSD)
+SRCS := lpcusbsio.c hid.c
+CFLAGS += -Iinclude -Isrc/hid_api/freebsd -fPIC -Wall -c
+else
SRCS := lpcusbsio.c hid.c
CFLAGS += -Iinclude -Isrc/hid_api/hidapi -fPIC -Wall -c
+endif
dir_guard = @mkdir -p $(@D)
@@ -35,6 +40,16 @@ LIBNAME_A = libusbsio.a
LIBNAME_SO = libusbsio.so
endif
+ifeq ($(UNAME), FreeBSD)
+BINDIR = freebsd_$(UNAME_M)
+VPATH += src/hid_api/freebsd
+LDFLAGS += -shared
+CFLAGS += `pkg-config libusb-1.0 libudev hidapi --cflags`
+LDFLAGS += `pkg-config libusb-1.0 libudev hidapi --libs`
+LIBNAME_A = libusbsio.a
+LIBNAME_SO = libusbsio.so
+endif
+
#
# macOS
#
diff --git a/python/libusbsio/libusbsio.py b/python/libusbsio/libusbsio.py
index ae11269..c784bcb 100644
--- a/python/libusbsio/libusbsio.py
+++ b/python/libusbsio/libusbsio.py
@@ -261,6 +261,16 @@ class LIBUSBSIO(object):
else:
platf = LIBUSBSIO.P_LINUX32
packing = 4
+ elif platform.system().lower().startswith('fre'):
+ dllname = "libusbsio.so"
+ is_64bits = sys.maxsize > 2**32
+ dfltdir = "freebsd_" + platform.machine()
+ if is_64bits:
+ platf = LIBUSBSIO.P_LINUX64
+ packing = 8
+ else:
+ platf = LIBUSBSIO.P_LINUX32
+ packing = 4
elif platform.system().lower().startswith('dar'):
dfltdir = "osx_" + platform.machine()
if platform.machine() == "arm64":
diff --git a/src/hid_api/freebsd/hid.c b/src/hid_api/freebsd/hid.c
new file mode 100644
index 0000000..a61e9b8
--- /dev/null
+++ b/src/hid_api/freebsd/hid.c
@@ -0,0 +1,102 @@
+/*******************************************************
+ HIDAPI - Multi-Platform library for
+ communication with HID devices.
+
+ Alan Ott
+ Signal 11 Software
+
+ 8/22/2009
+ Linux Version - 6/2/2009
+
+ Copyright 2009, All Rights Reserved.
+
+ At the discretion of the user of this library,
+ this software may be licensed under the terms of the
+ GNU General Public License v3, a BSD-Style license, or the
+ original HIDAPI license as outlined in the LICENSE.txt,
+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
+ files located at the root of the source distribution.
+ These files may also be found in the public source
+ code repository located at:
+ http://github.com/signal11/hidapi .
+********************************************************/
+
+/*
+ * Copyright 2014, 2021 NXP
+ * Modified for use in NXP LIBUSBSIO Library
+ */
+
+/* C */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <errno.h>
+
+/* Unix */
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/utsname.h>
+#include <fcntl.h>
+#include <poll.h>
+
+#include <libudev.h>
+
+#include "hidapi_mock.h"
+
+int HID_API_EXPORT hid_write_timeout(hid_device *dev, const unsigned char *data, size_t length, int milliseconds)
+{
+ unsigned char* padded = NULL;
+ int bytes_written = 0;
+
+ if (length <= 0)
+ return 0;
+
+#if 0
+ if (length < (size_t)dev->output_report_length)
+ {
+ padded = calloc(1, dev->output_report_length);
+
+ if(padded != NULL)
+ {
+ /* use buffer of report_length size padded with zeroes instead of original data */
+ memcpy(padded, data, length);
+ length = (size_t)dev->output_report_length;
+ data = padded;
+ }
+ }
+#endif
+
+ /*
+ * Note:
+ * 1. Blocking Write for USB is not real blocking. There is a build-in timeout in Linux, which
+ * is defined by USB_CTRL_SET_TIMEOUT in linux/include/linux/usb.h
+ * 2. Do not use poll()/ppoll() for timeout control. POLLOUT wouldn't be triggered by HIDRAW.
+ */
+ bytes_written = hid_write(dev, data, length);
+
+ if(padded)
+ free(padded);
+
+ return bytes_written;
+}
+
+int HID_API_EXPORT hid_get_report_lengths(hid_device* device, unsigned short* output_report_length, unsigned short* input_report_length)
+{
+ if (output_report_length)
+ *output_report_length = 64;//device->output_report_length;
+ if (input_report_length)
+ *input_report_length = 64;//device->input_report_length;
+ return 0;
+}
+
+int HID_API_EXPORT hid_get_usage(hid_device* device, unsigned short* usage_page, unsigned short* usage)
+{
+ if (usage_page)
+ *usage_page = 0; // TODO: this seems commons for hidapi on Linux without hidraw
+ if (usage)
+ *usage = 0; // TODO: device->usage; (does a single usage value even make sense?)
+ return 0;
+}
diff --git a/src/hid_api/freebsd/hidapi_mock.h b/src/hid_api/freebsd/hidapi_mock.h
new file mode 100644
index 0000000..c1feb59
--- /dev/null
+++ b/src/hid_api/freebsd/hidapi_mock.h
@@ -0,0 +1,55 @@
+/*******************************************************
+ HIDAPI - Multi-Platform library for
+ communication with HID devices.
+
+ Alan Ott
+ Signal 11 Software
+
+ 8/22/2009
+ Linux Version - 6/2/2009
+
+ Copyright 2009, All Rights Reserved.
+
+ At the discretion of the user of this library,
+ this software may be licensed under the terms of the
+ GNU General Public License v3, a BSD-Style license, or the
+ original HIDAPI license as outlined in the LICENSE.txt,
+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
+ files located at the root of the source distribution.
+ These files may also be found in the public source
+ code repository located at:
+ http://github.com/signal11/hidapi .
+********************************************************/
+
+/*
+ * Copyright 2014, 2021 NXP
+ * Modified for use in NXP LIBUSBSIO Library
+ */
+
+#pragma once
+/* C */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <errno.h>
+
+/* Unix */
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/utsname.h>
+#include <fcntl.h>
+#include <poll.h>
+
+#include <libudev.h>
+
+#include <hidapi.h>
+#include <hidapi_libusb.h>
+
+int HID_API_EXPORT hid_write_timeout(hid_device *dev, const unsigned char *data, size_t length, int milliseconds);
+
+int HID_API_EXPORT hid_get_report_lengths(hid_device* device, unsigned short* output_report_length, unsigned short* input_report_length);
+
+int HID_API_EXPORT hid_get_usage(hid_device* device, unsigned short* usage_page, unsigned short* usage);
diff --git a/src/lpcusbsio.c b/src/lpcusbsio.c
index b71bb31..dacf9ee 100644
--- a/src/lpcusbsio.c
+++ b/src/lpcusbsio.c
@@ -13,7 +13,11 @@
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
+#if defined(__FreeBSD__)
+#include "hidapi_mock.h"
+#else
#include "hidapi.h"
+#endif
#include "lpcusbsio.h"
#include "lpcusbsio_protocol.h"
#ifdef _WIN32
--
2.40.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment