diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:27:07 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:27:07 +0000 |
commit | e2e1a00b1595b4930b86e3ba3ea2e9b771a79653 (patch) | |
tree | ba77e3ac3992e0dbf8f751513b9015d37122a2b1 /usbhid-dump/src/dev.c | |
parent | Initial commit. (diff) | |
download | usbutils-upstream.tar.xz usbutils-upstream.zip |
Adding upstream version 1:014.upstream/1%014upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'usbhid-dump/src/dev.c')
-rw-r--r-- | usbhid-dump/src/dev.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/usbhid-dump/src/dev.c b/usbhid-dump/src/dev.c new file mode 100644 index 0000000..1978496 --- /dev/null +++ b/usbhid-dump/src/dev.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * usbhid-dump - device + * + * Copyright (C) 2010 Nikolai Kondrashov <spbnick@gmail.com> + */ + +#include "config.h" + +#include "dev.h" +#include <assert.h> +#include <stdlib.h> + +bool +uhd_dev_valid(const uhd_dev *dev) +{ + return dev != NULL && + dev->handle != NULL; +} + + +enum libusb_error +uhd_dev_open(libusb_device *lusb_dev, + uhd_dev **pdev) +{ + enum libusb_error err; + uhd_dev *dev; + + assert(lusb_dev != NULL); + + dev = malloc(sizeof(*dev)); + if (dev == NULL) + return LIBUSB_ERROR_NO_MEM; + + dev->next = NULL; + + err = libusb_open(lusb_dev, &dev->handle); + if (err != LIBUSB_SUCCESS) + { + free(dev); + return err; + } + + assert(uhd_dev_valid(dev)); + + if (pdev != NULL) + *pdev = dev; + else + { + libusb_close(dev->handle); + free(dev); + } + + return LIBUSB_SUCCESS; +} + + +void +uhd_dev_close(uhd_dev *dev) +{ + if (dev == NULL) + return; + + assert(uhd_dev_valid(dev)); + + libusb_close(dev->handle); + dev->handle = NULL; + + free(dev); +} + + |