| /* |
| * Linux-specific functions for libusb. |
| */ |
| |
| #include "libusbi.h" |
| |
| extern int sysfs_can_relate_devices; |
| extern int linux_get_device_address( |
| struct libusb_context *ctx, int detached, |
| uint8_t *busnum, uint8_t *devaddr,const char *dev_node, |
| const char *sys_name); |
| extern int linux_enumerate_device( |
| struct libusb_context *ctx, |
| uint8_t busnum, uint8_t devaddr, const char *sysfs_dir); |
| |
| /** |
| * Returns a USB device currently attached to the system. This is |
| * your entry point into connecting to that USB device. |
| * |
| * This return value of this function indicates whether the device was found or |
| * not. Success is a 0, other values indicate failure. |
| * |
| * \param ctx the context to operate on, or NULL for the default context |
| * \param devname SYSFS device name to check |
| * \param device output location for the device. Must be later freed with |
| * libusb_unref_device(). |
| * \returns 0 for success. |
| * \ref libusb_error according to errors encountered by the backend. |
| */ |
| int libusb_ext_get_one_device(libusb_context *ctx, const char* devname, |
| libusb_device **device) |
| { |
| if (!sysfs_can_relate_devices) { |
| return LIBUSB_ERROR_NOT_SUPPORTED; |
| } |
| |
| int r = 0; |
| uint8_t busnum, devaddr; |
| USBI_GET_CONTEXT(ctx); |
| |
| r = linux_get_device_address (ctx, 0, &busnum, &devaddr, NULL, devname); |
| if (r < 0) { |
| return r; |
| } |
| |
| r = linux_enumerate_device(ctx, busnum & 0xff, devaddr & 0xff, devname); |
| if (r < 0) { |
| return r; |
| } |
| |
| struct libusb_device *it; |
| usbi_mutex_lock(&ctx->usb_devs_lock); |
| list_for_each_entry(it, &ctx->usb_devs, list, struct libusb_device) { |
| if (it->bus_number == busnum && it->device_address == devaddr) { |
| *device = it; |
| } |
| } |
| usbi_mutex_unlock(&ctx->usb_devs_lock); |
| |
| return LIBUSB_SUCCESS; |
| } |