|
Packit Service |
b0a153 |
/* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
|
|
Packit Service |
b0a153 |
/*
|
|
Packit Service |
b0a153 |
* Linux usbfs backend for libusb
|
|
Packit Service |
b0a153 |
* Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
|
|
Packit Service |
b0a153 |
* Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
|
|
Packit Service |
b0a153 |
* Copyright (c) 2012-2013 Nathan Hjelm <hjelmn@mac.com>
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
b0a153 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
b0a153 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
b0a153 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b0a153 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
b0a153 |
* Lesser General Public License for more details.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
b0a153 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
b0a153 |
*/
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include <config.h>
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include <assert.h>
|
|
Packit Service |
b0a153 |
#include <ctype.h>
|
|
Packit Service |
b0a153 |
#include <dirent.h>
|
|
Packit Service |
b0a153 |
#include <errno.h>
|
|
Packit Service |
b0a153 |
#include <fcntl.h>
|
|
Packit Service |
b0a153 |
#include <poll.h>
|
|
Packit Service |
b0a153 |
#include <stdio.h>
|
|
Packit Service |
b0a153 |
#include <stdlib.h>
|
|
Packit Service |
b0a153 |
#include <string.h>
|
|
Packit Service |
b0a153 |
#include <sys/ioctl.h>
|
|
Packit Service |
b0a153 |
#include <sys/stat.h>
|
|
Packit Service |
b0a153 |
#include <sys/types.h>
|
|
Packit Service |
b0a153 |
#include <sys/utsname.h>
|
|
Packit Service |
b0a153 |
#include <sys/socket.h>
|
|
Packit Service |
b0a153 |
#include <unistd.h>
|
|
Packit Service |
b0a153 |
#include <libudev.h>
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include "libusbi.h"
|
|
Packit Service |
b0a153 |
#include "linux_usbfs.h"
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* udev context */
|
|
Packit Service |
b0a153 |
static struct udev *udev_ctx = NULL;
|
|
Packit Service |
b0a153 |
static int udev_monitor_fd = -1;
|
|
Packit Service |
b0a153 |
static int udev_control_pipe[2] = {-1, -1};
|
|
Packit Service |
b0a153 |
static struct udev_monitor *udev_monitor = NULL;
|
|
Packit Service |
b0a153 |
static pthread_t linux_event_thread;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
static void udev_hotplug_event(struct udev_device* udev_dev);
|
|
Packit Service |
b0a153 |
static void *linux_udev_event_thread_main(void *arg);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int linux_udev_start_event_monitor(void)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
assert(udev_ctx == NULL);
|
|
Packit Service |
b0a153 |
udev_ctx = udev_new();
|
|
Packit Service |
b0a153 |
if (!udev_ctx) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "could not create udev context");
|
|
Packit Service |
b0a153 |
goto err;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
|
|
Packit Service |
b0a153 |
if (!udev_monitor) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "could not initialize udev monitor");
|
|
Packit Service |
b0a153 |
goto err_free_ctx;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
|
|
Packit Service |
b0a153 |
if (r) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
if (udev_monitor_enable_receiving(udev_monitor)) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "failed to enable the udev monitor");
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#if defined(FD_CLOEXEC)
|
|
Packit Service |
b0a153 |
/* Make sure the udev file descriptor is marked as CLOEXEC */
|
|
Packit Service |
b0a153 |
r = fcntl(udev_monitor_fd, F_GETFD);
|
|
Packit Service |
b0a153 |
if (r == -1) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "geting udev monitor fd flags (%d)", errno);
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
if (!(r & FD_CLOEXEC)) {
|
|
Packit Service |
b0a153 |
if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* Some older versions of udev are not non-blocking by default,
|
|
Packit Service |
b0a153 |
* so make sure this is set */
|
|
Packit Service |
b0a153 |
r = fcntl(udev_monitor_fd, F_GETFL);
|
|
Packit Service |
b0a153 |
if (r == -1) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "getting udev monitor fd status flags (%d)", errno);
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
if (!(r & O_NONBLOCK)) {
|
|
Packit Service |
b0a153 |
if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "setting udev monitor fd status flags (%d)", errno);
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = usbi_pipe(udev_control_pipe);
|
|
Packit Service |
b0a153 |
if (r) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "could not create udev control pipe");
|
|
Packit Service |
b0a153 |
goto err_free_monitor;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
|
|
Packit Service |
b0a153 |
if (r) {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "creating hotplug event thread (%d)", r);
|
|
Packit Service |
b0a153 |
goto err_close_pipe;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return LIBUSB_SUCCESS;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
err_close_pipe:
|
|
Packit Service |
b0a153 |
close(udev_control_pipe[0]);
|
|
Packit Service |
b0a153 |
close(udev_control_pipe[1]);
|
|
Packit Service |
b0a153 |
err_free_monitor:
|
|
Packit Service |
b0a153 |
udev_monitor_unref(udev_monitor);
|
|
Packit Service |
b0a153 |
udev_monitor = NULL;
|
|
Packit Service |
b0a153 |
udev_monitor_fd = -1;
|
|
Packit Service |
b0a153 |
err_free_ctx:
|
|
Packit Service |
b0a153 |
udev_unref(udev_ctx);
|
|
Packit Service |
b0a153 |
err:
|
|
Packit Service |
b0a153 |
udev_ctx = NULL;
|
|
Packit Service |
b0a153 |
return LIBUSB_ERROR_OTHER;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int linux_udev_stop_event_monitor(void)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
char dummy = 1;
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
assert(udev_ctx != NULL);
|
|
Packit Service |
b0a153 |
assert(udev_monitor != NULL);
|
|
Packit Service |
b0a153 |
assert(udev_monitor_fd != -1);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* Write some dummy data to the control pipe and
|
|
Packit Service |
b0a153 |
* wait for the thread to exit */
|
|
Packit Service |
b0a153 |
r = write(udev_control_pipe[1], &dummy, sizeof(dummy));
|
|
Packit Service |
b0a153 |
if (r <= 0) {
|
|
Packit Service |
b0a153 |
usbi_warn(NULL, "udev control pipe signal failed");
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
pthread_join(linux_event_thread, NULL);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* Release the udev monitor */
|
|
Packit Service |
b0a153 |
udev_monitor_unref(udev_monitor);
|
|
Packit Service |
b0a153 |
udev_monitor = NULL;
|
|
Packit Service |
b0a153 |
udev_monitor_fd = -1;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* Clean up the udev context */
|
|
Packit Service |
b0a153 |
udev_unref(udev_ctx);
|
|
Packit Service |
b0a153 |
udev_ctx = NULL;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/* close and reset control pipe */
|
|
Packit Service |
b0a153 |
close(udev_control_pipe[0]);
|
|
Packit Service |
b0a153 |
close(udev_control_pipe[1]);
|
|
Packit Service |
b0a153 |
udev_control_pipe[0] = -1;
|
|
Packit Service |
b0a153 |
udev_control_pipe[1] = -1;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return LIBUSB_SUCCESS;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
static void *linux_udev_event_thread_main(void *arg)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
char dummy;
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
ssize_t nb;
|
|
Packit Service |
b0a153 |
struct udev_device* udev_dev;
|
|
Packit Service |
b0a153 |
struct pollfd fds[] = {
|
|
Packit Service |
b0a153 |
{.fd = udev_control_pipe[0],
|
|
Packit Service |
b0a153 |
.events = POLLIN},
|
|
Packit Service |
b0a153 |
{.fd = udev_monitor_fd,
|
|
Packit Service |
b0a153 |
.events = POLLIN},
|
|
Packit Service |
b0a153 |
};
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
usbi_dbg("udev event thread entering.");
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
while ((r = poll(fds, 2, -1)) >= 0 || errno == EINTR) {
|
|
Packit Service |
b0a153 |
if (r < 0) {
|
|
Packit Service |
b0a153 |
/* temporary failure */
|
|
Packit Service |
b0a153 |
continue;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
if (fds[0].revents & POLLIN) {
|
|
Packit Service |
b0a153 |
/* activity on control pipe, read the byte and exit */
|
|
Packit Service |
b0a153 |
nb = read(udev_control_pipe[0], &dummy, sizeof(dummy));
|
|
Packit Service |
b0a153 |
if (nb <= 0) {
|
|
Packit Service |
b0a153 |
usbi_warn(NULL, "udev control pipe read failed");
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
break;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
if (fds[1].revents & POLLIN) {
|
|
Packit Service |
b0a153 |
usbi_mutex_static_lock(&linux_hotplug_lock);
|
|
Packit Service |
b0a153 |
udev_dev = udev_monitor_receive_device(udev_monitor);
|
|
Packit Service |
b0a153 |
if (udev_dev)
|
|
Packit Service |
b0a153 |
udev_hotplug_event(udev_dev);
|
|
Packit Service |
b0a153 |
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
usbi_dbg("udev event thread exiting");
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return NULL;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
static int udev_device_info(struct libusb_context *ctx, int detached,
|
|
Packit Service |
b0a153 |
struct udev_device *udev_dev, uint8_t *busnum,
|
|
Packit Service |
b0a153 |
uint8_t *devaddr, const char **sys_name) {
|
|
Packit Service |
b0a153 |
const char *dev_node;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
dev_node = udev_device_get_devnode(udev_dev);
|
|
Packit Service |
b0a153 |
if (!dev_node) {
|
|
Packit Service |
b0a153 |
return LIBUSB_ERROR_OTHER;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
*sys_name = udev_device_get_sysname(udev_dev);
|
|
Packit Service |
b0a153 |
if (!*sys_name) {
|
|
Packit Service |
b0a153 |
return LIBUSB_ERROR_OTHER;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return linux_get_device_address(ctx, detached, busnum, devaddr,
|
|
Packit Service |
b0a153 |
dev_node, *sys_name, -1);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
static void udev_hotplug_event(struct udev_device* udev_dev)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
const char* udev_action;
|
|
Packit Service |
b0a153 |
const char* sys_name = NULL;
|
|
Packit Service |
b0a153 |
uint8_t busnum = 0, devaddr = 0;
|
|
Packit Service |
b0a153 |
int detached;
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
do {
|
|
Packit Service |
b0a153 |
udev_action = udev_device_get_action(udev_dev);
|
|
Packit Service |
b0a153 |
if (!udev_action) {
|
|
Packit Service |
b0a153 |
break;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
detached = !strncmp(udev_action, "remove", 6);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
|
|
Packit Service |
b0a153 |
if (LIBUSB_SUCCESS != r) {
|
|
Packit Service |
b0a153 |
break;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
usbi_dbg("udev hotplug event. action: %s.", udev_action);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
if (strncmp(udev_action, "add", 3) == 0) {
|
|
Packit Service |
b0a153 |
linux_hotplug_enumerate(busnum, devaddr, sys_name);
|
|
Packit Service |
b0a153 |
} else if (detached) {
|
|
Packit Service |
b0a153 |
linux_device_disconnected(busnum, devaddr);
|
|
Packit Service |
b0a153 |
} else {
|
|
Packit Service |
b0a153 |
usbi_err(NULL, "ignoring udev action %s", udev_action);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
} while (0);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_device_unref(udev_dev);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int linux_udev_scan_devices(struct libusb_context *ctx)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
struct udev_enumerate *enumerator;
|
|
Packit Service |
b0a153 |
struct udev_list_entry *devices, *entry;
|
|
Packit Service |
b0a153 |
struct udev_device *udev_dev;
|
|
Packit Service |
b0a153 |
const char *sys_name;
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
assert(udev_ctx != NULL);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
enumerator = udev_enumerate_new(udev_ctx);
|
|
Packit Service |
b0a153 |
if (NULL == enumerator) {
|
|
Packit Service |
b0a153 |
usbi_err(ctx, "error creating udev enumerator");
|
|
Packit Service |
b0a153 |
return LIBUSB_ERROR_OTHER;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_enumerate_add_match_subsystem(enumerator, "usb");
|
|
Packit Service |
b0a153 |
udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
|
|
Packit Service |
b0a153 |
udev_enumerate_scan_devices(enumerator);
|
|
Packit Service |
b0a153 |
devices = udev_enumerate_get_list_entry(enumerator);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
entry = NULL;
|
|
Packit Service |
b0a153 |
udev_list_entry_foreach(entry, devices) {
|
|
Packit Service |
b0a153 |
const char *path = udev_list_entry_get_name(entry);
|
|
Packit Service |
b0a153 |
uint8_t busnum = 0, devaddr = 0;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_dev = udev_device_new_from_syspath(udev_ctx, path);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
|
|
Packit Service |
b0a153 |
if (r) {
|
|
Packit Service |
b0a153 |
udev_device_unref(udev_dev);
|
|
Packit Service |
b0a153 |
continue;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
linux_enumerate_device(ctx, busnum, devaddr, sys_name);
|
|
Packit Service |
b0a153 |
udev_device_unref(udev_dev);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
udev_enumerate_unref(enumerator);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return LIBUSB_SUCCESS;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
void linux_udev_hotplug_poll(void)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
struct udev_device* udev_dev;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
usbi_mutex_static_lock(&linux_hotplug_lock);
|
|
Packit Service |
b0a153 |
do {
|
|
Packit Service |
b0a153 |
udev_dev = udev_monitor_receive_device(udev_monitor);
|
|
Packit Service |
b0a153 |
if (udev_dev) {
|
|
Packit Service |
b0a153 |
usbi_dbg("Handling hotplug event from hotplug_poll");
|
|
Packit Service |
b0a153 |
udev_hotplug_event(udev_dev);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
} while (udev_dev);
|
|
Packit Service |
b0a153 |
usbi_mutex_static_unlock(&linux_hotplug_lock);
|
|
Packit Service |
b0a153 |
}
|