mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-29 18:08:29 +00:00
st/prober: Refactor libusb code to own file
This commit is contained in:
parent
7f07cee387
commit
d9b820c834
|
@ -27,13 +27,22 @@ if(BUILD_WITH_LIBUDEV)
|
|||
)
|
||||
endif()
|
||||
|
||||
# Add libusb
|
||||
if(BUILD_WITH_LIBUSB)
|
||||
list(APPEND PROBER_SOURCE_FILES
|
||||
p_libusb.c
|
||||
)
|
||||
list(APPEND PROBER_INCLUDES
|
||||
${LIBUSB_INCLUDES}
|
||||
)
|
||||
endif()
|
||||
|
||||
# Use OBJECT to not create a archive, since it just gets in the way.
|
||||
add_library(st_prober OBJECT ${PROBER_SOURCE_FILES})
|
||||
|
||||
target_include_directories(st_prober
|
||||
PRIVATE
|
||||
${PROBER_INCLUDES}
|
||||
${LIBUSB_INCLUDES}
|
||||
${LIBUVC_INCLUDES}
|
||||
)
|
||||
|
||||
|
|
|
@ -65,6 +65,8 @@ print_ports(char* tmp, size_t size, uint8_t* ports, int num)
|
|||
void
|
||||
p_dump_device(struct prober* p, struct prober_device* pdev, int id)
|
||||
{
|
||||
char tmp[1024];
|
||||
|
||||
if (pdev->usb.bus != 0 && pdev->usb.addr == 0 &&
|
||||
pdev->base.vendor_id != 0 && pdev->base.product_id == 0) {
|
||||
return;
|
||||
|
@ -84,28 +86,25 @@ p_dump_device(struct prober* p, struct prober_device* pdev, int id)
|
|||
pdev->bluetooth.id);
|
||||
}
|
||||
|
||||
libusb_device* usb_dev = pdev->usb.dev;
|
||||
if (usb_dev != NULL) {
|
||||
uint8_t ports[8];
|
||||
char tmp[1024];
|
||||
|
||||
printf("\t\tlibusb: %p\n", (void*)usb_dev);
|
||||
|
||||
int num =
|
||||
libusb_get_port_numbers(usb_dev, ports, ARRAY_SIZE(ports));
|
||||
|
||||
if (print_ports(tmp, ARRAY_SIZE(tmp), ports, num)) {
|
||||
printf("\t\tport%s %s\n", num > 1 ? "s:" : ": ",
|
||||
tmp);
|
||||
}
|
||||
int num = pdev->usb.num_ports;
|
||||
if (print_ports(tmp, ARRAY_SIZE(tmp), pdev->usb.ports, num)) {
|
||||
printf("\t\tport%s %s\n", num > 1 ? "s:" : ": ", tmp);
|
||||
}
|
||||
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
if (pdev->usb.dev != NULL) {
|
||||
printf("\t\tlibusb: %p\n", (void*)pdev->usb.dev);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
for (size_t j = 0; j < pdev->num_hidraws; j++) {
|
||||
struct prober_hidraw* hidraw = &pdev->hidraws[j];
|
||||
|
||||
printf("\t\tinterface: %u\n", (int)hidraw->interface);
|
||||
printf("\t\tpath: '%s'\n", hidraw->path);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef XRT_HAVE_LIBUVC
|
||||
uvc_device_t* uvc_dev = pdev->uvc.dev;
|
||||
|
|
96
src/xrt/state_trackers/prober/p_libusb.c
Normal file
96
src/xrt/state_trackers/prober/p_libusb.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Prober code interfacing to libusb.
|
||||
* @author Jakob Bornecrantz <jakob@collabora.com>
|
||||
* @ingroup st_prober
|
||||
*/
|
||||
|
||||
#include "util/u_debug.h"
|
||||
#include "util/u_misc.h"
|
||||
#include "p_prober.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int
|
||||
p_libusb_init(struct prober *p)
|
||||
{
|
||||
return libusb_init(&p->usb.ctx);
|
||||
}
|
||||
|
||||
void
|
||||
p_libusb_teardown(struct prober *p)
|
||||
{
|
||||
// Free all libusb resources.
|
||||
if (p->usb.list != NULL) {
|
||||
libusb_free_device_list(p->usb.list, 1);
|
||||
p->usb.list = NULL;
|
||||
}
|
||||
|
||||
if (p->usb.ctx != NULL) {
|
||||
libusb_exit(p->usb.ctx);
|
||||
p->usb.ctx = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
p_libusb_probe(struct prober *p)
|
||||
{
|
||||
int ret;
|
||||
|
||||
// Free old list first.
|
||||
if (p->usb.list != NULL) {
|
||||
libusb_free_device_list(p->usb.list, 1);
|
||||
p->usb.list = NULL;
|
||||
}
|
||||
|
||||
// Probe for USB devices.
|
||||
p->usb.count = libusb_get_device_list(p->usb.ctx, &p->usb.list);
|
||||
if (p->usb.count < 0) {
|
||||
P_ERROR(p, "\tFailed to enumerate usb devices\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < p->usb.count; i++) {
|
||||
libusb_device *device = p->usb.list[i];
|
||||
struct libusb_device_descriptor desc;
|
||||
struct prober_device *pdev = NULL;
|
||||
|
||||
libusb_get_device_descriptor(device, &desc);
|
||||
uint8_t bus = libusb_get_bus_number(device);
|
||||
uint8_t addr = libusb_get_device_address(device);
|
||||
uint16_t vendor = desc.idVendor;
|
||||
uint16_t product = desc.idProduct;
|
||||
uint8_t ports[8];
|
||||
|
||||
int num =
|
||||
libusb_get_port_numbers(device, ports, ARRAY_SIZE(ports));
|
||||
|
||||
ret = p_dev_get_usb_dev(p, bus, addr, vendor, product, &pdev);
|
||||
|
||||
P_SPEW(p,
|
||||
"libusb\n"
|
||||
"\t\tptr: %p (%i)\n"
|
||||
"\t\tvendor_id: %04x\n"
|
||||
"\t\tproduct_id: %04x\n"
|
||||
"\t\tbus: %i\n"
|
||||
"\t\taddr: %i",
|
||||
(void *)pdev, ret, vendor, product, bus, addr);
|
||||
|
||||
if (ret != 0) {
|
||||
P_ERROR(p, "p_dev_get_usb_device failed!");
|
||||
continue;
|
||||
}
|
||||
|
||||
pdev->usb.num_ports = num;
|
||||
memcpy(pdev->usb.ports, ports, sizeof(uint8_t) * num);
|
||||
|
||||
// Attach the libusb device to it.
|
||||
pdev->usb.dev = device;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -236,11 +236,13 @@ initialize(struct prober* p, struct xrt_prober_entry_lists* lists)
|
|||
return -1;
|
||||
}
|
||||
|
||||
ret = libusb_init(&p->usb.ctx);
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
ret = p_libusb_init(p);
|
||||
if (ret != 0) {
|
||||
teardown(p);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef XRT_HAVE_LIBUVC
|
||||
ret = uvc_init(&p->uvc.ctx, p->usb.ctx);
|
||||
|
@ -253,6 +255,7 @@ initialize(struct prober* p, struct xrt_prober_entry_lists* lists)
|
|||
for (int i = 0; i < MAX_AUTO_PROBERS && lists->auto_probers[i]; i++) {
|
||||
p->auto_probers[i] = lists->auto_probers[i]();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -315,68 +318,9 @@ teardown(struct prober* p)
|
|||
}
|
||||
#endif
|
||||
|
||||
// Free all libusb resources.
|
||||
if (p->usb.list != NULL) {
|
||||
libusb_free_device_list(p->usb.list, 1);
|
||||
p->usb.list = NULL;
|
||||
}
|
||||
|
||||
if (p->usb.ctx != NULL) {
|
||||
libusb_exit(p->usb.ctx);
|
||||
p->usb.ctx = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
p_libusb_probe(struct prober* p)
|
||||
{
|
||||
int ret;
|
||||
|
||||
// Free old list first.
|
||||
if (p->usb.list != NULL) {
|
||||
libusb_free_device_list(p->usb.list, 1);
|
||||
p->usb.list = NULL;
|
||||
}
|
||||
|
||||
// Probe for USB devices.
|
||||
p->usb.count = libusb_get_device_list(p->usb.ctx, &p->usb.list);
|
||||
if (p->usb.count < 0) {
|
||||
P_ERROR(p, "\tFailed to enumerate usb devices\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < p->usb.count; i++) {
|
||||
libusb_device* device = p->usb.list[i];
|
||||
struct libusb_device_descriptor desc;
|
||||
struct prober_device* pdev = NULL;
|
||||
|
||||
libusb_get_device_descriptor(device, &desc);
|
||||
uint8_t bus = libusb_get_bus_number(device);
|
||||
uint8_t addr = libusb_get_device_address(device);
|
||||
uint16_t vendor = desc.idVendor;
|
||||
uint16_t product = desc.idProduct;
|
||||
|
||||
ret = p_dev_get_usb_dev(p, bus, addr, vendor, product, &pdev);
|
||||
|
||||
P_SPEW(p,
|
||||
"libusb\n"
|
||||
"\t\tptr: %p (%i)\n"
|
||||
"\t\tvendor_id: %04x\n"
|
||||
"\t\tproduct_id: %04x\n"
|
||||
"\t\tbus: %i\n"
|
||||
"\t\taddr: %i",
|
||||
(void*)pdev, ret, vendor, product, bus, addr);
|
||||
|
||||
if (ret != 0) {
|
||||
P_ERROR(p, "p_dev_get_usb_device failed!");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Attach the libusb device to it.
|
||||
pdev->usb.dev = device;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
p_libusb_teardown(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -453,11 +397,13 @@ probe(struct xrt_prober* xp)
|
|||
// Free old list first.
|
||||
teardown_devices(p);
|
||||
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
ret = p_libusb_probe(p);
|
||||
if (ret != 0) {
|
||||
P_ERROR(p, "Failed to enumerate libusb devices\n");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = p_libuvc_probe(p);
|
||||
if (ret != 0) {
|
||||
|
|
|
@ -76,6 +76,9 @@ struct prober_device
|
|||
uint16_t bus;
|
||||
uint16_t addr;
|
||||
|
||||
uint8_t ports[8];
|
||||
uint32_t num_ports;
|
||||
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
libusb_device* dev;
|
||||
#endif
|
||||
|
@ -110,12 +113,14 @@ struct prober
|
|||
|
||||
struct xrt_prober_entry_lists* lists;
|
||||
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
struct
|
||||
{
|
||||
libusb_context* ctx;
|
||||
libusb_device** list;
|
||||
ssize_t count;
|
||||
} usb;
|
||||
#endif
|
||||
|
||||
#ifdef XRT_HAVE_LIBUVC
|
||||
struct
|
||||
|
@ -172,6 +177,16 @@ p_dev_get_bluetooth_dev(struct prober* p,
|
|||
uint16_t product_id,
|
||||
struct prober_device** out_pdev);
|
||||
|
||||
#ifdef XRT_HAVE_LIBUSB
|
||||
int
|
||||
p_libusb_init(struct prober* p);
|
||||
|
||||
void
|
||||
p_libusb_teardown(struct prober* p);
|
||||
|
||||
int
|
||||
p_libusb_probe(struct prober* p);
|
||||
#endif
|
||||
|
||||
#ifdef XRT_HAVE_LIBUDEV
|
||||
int
|
||||
|
|
Loading…
Reference in a new issue