1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
3 * Linux usbfs backend for libusb
4 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * Copyright (c) 2012-2013 Nathan Hjelm <hjelmn@mac.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/utsname.h>
38 #include <sys/socket.h>
44 #include "linux_usbfs.h"
47 static struct udev *udev_ctx = NULL;
48 static int udev_monitor_fd = -1;
49 static struct udev_monitor *udev_monitor = NULL;
50 static pthread_t linux_event_thread;
52 static void udev_hotplug_event(void);
53 static void *linux_udev_event_thread_main(void *arg);
55 int linux_udev_start_event_monitor(void)
59 assert(udev_ctx == NULL);
60 udev_ctx = udev_new();
62 usbi_err(NULL, "could not create udev context");
63 return LIBUSB_ERROR_OTHER;
66 udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
68 usbi_err(NULL, "could not initialize udev monitor");
69 return LIBUSB_ERROR_OTHER;
72 r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
74 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
75 return LIBUSB_ERROR_OTHER;
78 if (udev_monitor_enable_receiving(udev_monitor)) {
79 usbi_err(NULL, "failed to enable the udev monitor");
80 return LIBUSB_ERROR_OTHER;
83 udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
85 pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
87 return LIBUSB_SUCCESS;
90 int linux_udev_stop_event_monitor(void)
92 assert(udev_ctx != NULL);
93 assert(udev_monitor != NULL);
94 assert(udev_monitor_fd != -1);
96 /* Cancel the event thread. This is the only way to garauntee the thread
97 exits since closing the monitor fd won't necessarily cause poll
99 pthread_cancel(linux_event_thread);
101 /* Release the udev monitor */
102 udev_monitor_unref(udev_monitor);
104 udev_monitor_fd = -1;
106 /* Clean up the udev context */
107 udev_unref(udev_ctx);
110 return LIBUSB_SUCCESS;
113 static void *linux_udev_event_thread_main(void *arg)
115 struct pollfd fds = {.fd = udev_monitor_fd,
118 usbi_dbg("udev event thread entering.");
120 while (1 == poll(&fds, 1, -1)) {
121 if (NULL == udev_monitor || POLLIN != fds.revents) {
125 udev_hotplug_event();
128 usbi_dbg("udev event thread exiting");
133 static int udev_device_info(struct libusb_context *ctx, int detached,
134 struct udev_device *udev_dev, uint8_t *busnum,
135 uint8_t *devaddr, const char **sys_name) {
136 const char *dev_node;
138 dev_node = udev_device_get_devnode(udev_dev);
140 return LIBUSB_ERROR_OTHER;
143 *sys_name = udev_device_get_sysname(udev_dev);
145 return LIBUSB_ERROR_OTHER;
148 return linux_get_device_address(ctx, detached, busnum, devaddr,
149 dev_node, *sys_name);
152 static void udev_hotplug_event(void)
154 struct udev_device* udev_dev;
155 const char* udev_action;
156 const char* sys_name = NULL;
157 uint8_t busnum = 0, devaddr = 0;
161 if (NULL == udev_monitor) {
166 udev_dev = udev_monitor_receive_device(udev_monitor);
168 usbi_err(NULL, "failed to read data from udev monitor socket.");
172 udev_action = udev_device_get_action(udev_dev);
177 detached = !strncmp(udev_action, "remove", 6);
179 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
180 if (LIBUSB_SUCCESS != r) {
184 usbi_dbg("udev hotplug event. action: %s.", udev_action);
186 if (strncmp(udev_action, "add", 3) == 0) {
187 linux_hotplug_enumerate(busnum, devaddr, sys_name);
188 } else if (detached) {
189 linux_hotplug_disconnected(busnum, devaddr, sys_name);
191 usbi_err(NULL, "ignoring udev action %s", udev_action);
195 udev_device_unref(udev_dev);
198 int linux_udev_scan_devices(struct libusb_context *ctx)
200 struct udev_enumerate *enumerator;
201 struct udev_list_entry *devices, *entry;
202 struct udev_device *udev_dev;
203 const char *sys_name;
206 assert(udev_ctx != NULL);
208 enumerator = udev_enumerate_new(udev_ctx);
209 if (NULL == enumerator) {
210 usbi_err(ctx, "error creating udev enumerator");
211 return LIBUSB_ERROR_OTHER;
214 udev_enumerate_add_match_subsystem(enumerator, "usb");
215 udev_enumerate_scan_devices(enumerator);
216 devices = udev_enumerate_get_list_entry(enumerator);
218 udev_list_entry_foreach(entry, devices) {
219 const char *path = udev_list_entry_get_name(entry);
220 uint8_t busnum = 0, devaddr = 0;
222 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
224 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
226 udev_device_unref(udev_dev);
230 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
231 udev_device_unref(udev_dev);
234 udev_enumerate_unref(enumerator);
236 return LIBUSB_SUCCESS;