st/prober: Refactor hidraw enumeration code in udev file

This commit is contained in:
Jakob Bornecrantz 2019-06-20 15:14:53 +01:00
parent dfdc794272
commit cb44c822d2
3 changed files with 57 additions and 44 deletions

View file

@ -267,6 +267,7 @@ teardown_devices(struct prober* p)
for (size_t i = 0; i < p->num_devices; i++) { for (size_t i = 0; i < p->num_devices; i++) {
struct prober_device* pdev = &p->devices[i]; struct prober_device* pdev = &p->devices[i];
#ifdef XRT_OS_LINUX
for (size_t j = 0; j < pdev->num_hidraws; j++) { for (size_t j = 0; j < pdev->num_hidraws; j++) {
struct prober_hidraw* hidraw = &pdev->hidraws[j]; struct prober_hidraw* hidraw = &pdev->hidraws[j];
free((char*)hidraw->path); free((char*)hidraw->path);
@ -278,6 +279,7 @@ teardown_devices(struct prober* p)
pdev->hidraws = NULL; pdev->hidraws = NULL;
pdev->num_hidraws = 0; pdev->num_hidraws = 0;
} }
#endif
} }
if (p->devices != NULL) { if (p->devices != NULL) {

View file

@ -55,6 +55,7 @@
#define MAX_AUTO_PROBERS 8 #define MAX_AUTO_PROBERS 8
#ifdef XRT_OS_LINUX
/*! /*!
* A hidraw interface that a @ref prober_device exposes. * A hidraw interface that a @ref prober_device exposes.
*/ */
@ -63,6 +64,7 @@ struct prober_hidraw
ssize_t interface; ssize_t interface;
const char* path; const char* path;
}; };
#endif
/*! /*!
* A prober device. * A prober device.
@ -101,10 +103,10 @@ struct prober_device
{ {
const char** paths; const char** paths;
} v4l; } v4l;
#endif
size_t num_hidraws; size_t num_hidraws;
struct prober_hidraw* hidraws; struct prober_hidraw* hidraws;
#endif
}; };
struct prober struct prober

View file

@ -34,9 +34,12 @@
*/ */
static void static void
p_udev_add_interface(struct prober_device* pdev, p_udev_enumerate_hidraw(struct prober* p, struct udev* udev);
uint32_t interface,
const char* path); static void
p_udev_add_hidraw(struct prober_device* pdev,
uint32_t interface,
const char* path);
static int static int
p_udev_get_interface_number(struct udev_device* raw_dev, p_udev_get_interface_number(struct udev_device* raw_dev,
@ -50,10 +53,10 @@ p_udev_get_and_parse_uevent(struct udev_device* raw_dev,
uint64_t* out_bluetooth_serial); uint64_t* out_bluetooth_serial);
static int static int
p_udev_get_usb_address(struct udev_device* raw_dev, p_udev_get_usb_hid_address(struct udev_device* raw_dev,
uint32_t bus_type, uint32_t bus_type,
uint16_t* usb_bus, uint16_t* usb_bus,
uint16_t* usb_addr); uint16_t* usb_addr);
/* /*
@ -65,27 +68,32 @@ p_udev_get_usb_address(struct udev_device* raw_dev,
int int
p_udev_probe(struct prober* p) p_udev_probe(struct prober* p)
{ {
struct prober_device* pdev; struct udev* udev = udev_new();
struct udev* udev;
struct udev_enumerate* enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device* raw_dev = NULL;
uint16_t vendor_id, product_id, interface;
uint16_t usb_bus = 0;
uint16_t usb_addr = 0;
uint32_t bus_type;
uint64_t bluetooth_id;
int ret;
const char* sysfs_path;
const char* dev_path;
udev = udev_new();
if (!udev) { if (!udev) {
P_ERROR(p, "Can't create udev\n"); P_ERROR(p, "Can't create udev\n");
return -1; return -1;
} }
p_udev_enumerate_hidraw(p, udev);
udev = udev_unref(udev);
return 0;
}
/*
*
* Internal functions.
*
*/
static void
p_udev_enumerate_hidraw(struct prober* p, struct udev* udev)
{
struct udev_enumerate* enumerate;
struct udev_list_entry *devices, *dev_list_entry;
enumerate = udev_enumerate_new(udev); enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "hidraw"); udev_enumerate_add_match_subsystem(enumerate, "hidraw");
udev_enumerate_scan_devices(enumerate); udev_enumerate_scan_devices(enumerate);
@ -94,6 +102,17 @@ p_udev_probe(struct prober* p)
udev_list_entry_foreach(dev_list_entry, devices) udev_list_entry_foreach(dev_list_entry, devices)
{ {
struct prober_device* pdev = NULL;
struct udev_device* raw_dev = NULL;
uint16_t vendor_id, product_id, interface;
uint16_t usb_bus = 0;
uint16_t usb_addr = 0;
uint32_t bus_type = 0;
uint64_t bluetooth_id = 0;
const char* sysfs_path;
const char* dev_path;
int ret;
// Where in the sysfs is. // Where in the sysfs is.
sysfs_path = udev_list_entry_get_name(dev_list_entry); sysfs_path = udev_list_entry_get_name(dev_list_entry);
// Raw sysfs node. // Raw sysfs node.
@ -117,8 +136,8 @@ p_udev_probe(struct prober* p)
} }
// Get USB bus and address to de-dublicate devices. // Get USB bus and address to de-dublicate devices.
ret = p_udev_get_usb_address(raw_dev, bus_type, &usb_bus, ret = p_udev_get_usb_hid_address(raw_dev, bus_type, &usb_bus,
&usb_addr); &usb_addr);
if (ret != 0) { if (ret != 0) {
P_ERROR(p, "Failed to get USB bus and addr."); P_ERROR(p, "Failed to get USB bus and addr.");
goto next; goto next;
@ -161,29 +180,19 @@ p_udev_probe(struct prober* p)
} }
// Add this interface to the usb device. // Add this interface to the usb device.
p_udev_add_interface(pdev, interface, dev_path); p_udev_add_hidraw(pdev, interface, dev_path);
next: next:
udev_device_unref(raw_dev); udev_device_unref(raw_dev);
} }
enumerate = udev_enumerate_unref(enumerate); enumerate = udev_enumerate_unref(enumerate);
udev = udev_unref(udev);
return 0;
} }
/*
*
* Internal functions.
*
*/
static void static void
p_udev_add_interface(struct prober_device* pdev, p_udev_add_hidraw(struct prober_device* pdev,
uint32_t interface, uint32_t interface,
const char* path) const char* path)
{ {
size_t new_size = size_t new_size =
(pdev->num_hidraws + 1) * sizeof(struct prober_hidraw); (pdev->num_hidraws + 1) * sizeof(struct prober_hidraw);
@ -197,10 +206,10 @@ p_udev_add_interface(struct prober_device* pdev,
} }
static int static int
p_udev_get_usb_address(struct udev_device* raw_dev, p_udev_get_usb_hid_address(struct udev_device* raw_dev,
uint32_t bus_type, uint32_t bus_type,
uint16_t* usb_bus, uint16_t* usb_bus,
uint16_t* usb_addr) uint16_t* usb_addr)
{ {
struct udev_device* usb_dev; struct udev_device* usb_dev;
const char* bus_str; const char* bus_str;