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>
43 #include "linux_usbfs.h"
46 static struct udev *udev_ctx = NULL;
47 static int udev_monitor_fd = -1;
48 static int udev_control_pipe[2] = {-1, -1};
49 static struct udev_monitor *udev_monitor = NULL;
50 static pthread_t linux_event_thread;
52 static void udev_hotplug_event(struct udev_device* udev_dev);
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");
66 udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
68 usbi_err(NULL, "could not initialize udev monitor");
72 r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
74 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
75 goto err_free_monitor;
78 if (udev_monitor_enable_receiving(udev_monitor)) {
79 usbi_err(NULL, "failed to enable the udev monitor");
80 goto err_free_monitor;
83 udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
85 /* Some older versions of udev are not non-blocking by default,
86 * so make sure this is set */
87 r = fcntl(udev_monitor_fd, F_GETFL);
89 usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
90 goto err_free_monitor;
92 r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
94 usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
95 goto err_free_monitor;
98 r = usbi_pipe(udev_control_pipe);
100 usbi_err(NULL, "could not create udev control pipe");
101 goto err_free_monitor;
104 r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
106 usbi_err(NULL, "creating hotplug event thread (%d)", r);
110 return LIBUSB_SUCCESS;
113 close(udev_control_pipe[0]);
114 close(udev_control_pipe[1]);
116 udev_monitor_unref(udev_monitor);
118 udev_monitor_fd = -1;
120 udev_unref(udev_ctx);
123 return LIBUSB_ERROR_OTHER;
126 int linux_udev_stop_event_monitor(void)
131 assert(udev_ctx != NULL);
132 assert(udev_monitor != NULL);
133 assert(udev_monitor_fd != -1);
135 /* Write some dummy data to the control pipe and
136 * wait for the thread to exit */
137 r = write(udev_control_pipe[1], &dummy, sizeof(dummy));
139 usbi_warn(NULL, "udev control pipe signal failed");
141 pthread_join(linux_event_thread, NULL);
143 /* Release the udev monitor */
144 udev_monitor_unref(udev_monitor);
146 udev_monitor_fd = -1;
148 /* Clean up the udev context */
149 udev_unref(udev_ctx);
152 /* close and reset control pipe */
153 close(udev_control_pipe[0]);
154 close(udev_control_pipe[1]);
155 udev_control_pipe[0] = -1;
156 udev_control_pipe[1] = -1;
158 return LIBUSB_SUCCESS;
161 static void *linux_udev_event_thread_main(void *arg)
166 struct udev_device* udev_dev;
167 struct pollfd fds[] = {
168 {.fd = udev_control_pipe[0],
170 {.fd = udev_monitor_fd,
174 usbi_dbg("udev event thread entering.");
176 while ((r = poll(fds, 2, -1)) >= 0 || errno == EINTR) {
178 /* temporary failure */
181 if (fds[0].revents & POLLIN) {
182 /* activity on control pipe, read the byte and exit */
183 nb = read(udev_control_pipe[0], &dummy, sizeof(dummy));
185 usbi_warn(NULL, "udev control pipe read failed");
189 if (fds[1].revents & POLLIN) {
190 usbi_mutex_static_lock(&linux_hotplug_lock);
191 udev_dev = udev_monitor_receive_device(udev_monitor);
193 udev_hotplug_event(udev_dev);
194 usbi_mutex_static_unlock(&linux_hotplug_lock);
198 usbi_dbg("udev event thread exiting");
203 static int udev_device_info(struct libusb_context *ctx, int detached,
204 struct udev_device *udev_dev, uint8_t *busnum,
205 uint8_t *devaddr, const char **sys_name) {
206 const char *dev_node;
208 dev_node = udev_device_get_devnode(udev_dev);
210 return LIBUSB_ERROR_OTHER;
213 *sys_name = udev_device_get_sysname(udev_dev);
215 return LIBUSB_ERROR_OTHER;
218 return linux_get_device_address(ctx, detached, busnum, devaddr,
219 dev_node, *sys_name);
222 static void udev_hotplug_event(struct udev_device* udev_dev)
224 const char* udev_action;
225 const char* sys_name = NULL;
226 uint8_t busnum = 0, devaddr = 0;
231 udev_action = udev_device_get_action(udev_dev);
236 detached = !strncmp(udev_action, "remove", 6);
238 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
239 if (LIBUSB_SUCCESS != r) {
243 usbi_dbg("udev hotplug event. action: %s.", udev_action);
245 if (strncmp(udev_action, "add", 3) == 0) {
246 linux_hotplug_enumerate(busnum, devaddr, sys_name);
247 } else if (detached) {
248 linux_device_disconnected(busnum, devaddr);
250 usbi_err(NULL, "ignoring udev action %s", udev_action);
254 udev_device_unref(udev_dev);
257 int linux_udev_scan_devices(struct libusb_context *ctx)
259 struct udev_enumerate *enumerator;
260 struct udev_list_entry *devices, *entry;
261 struct udev_device *udev_dev;
262 const char *sys_name;
265 assert(udev_ctx != NULL);
267 enumerator = udev_enumerate_new(udev_ctx);
268 if (NULL == enumerator) {
269 usbi_err(ctx, "error creating udev enumerator");
270 return LIBUSB_ERROR_OTHER;
273 udev_enumerate_add_match_subsystem(enumerator, "usb");
274 udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
275 udev_enumerate_scan_devices(enumerator);
276 devices = udev_enumerate_get_list_entry(enumerator);
278 udev_list_entry_foreach(entry, devices) {
279 const char *path = udev_list_entry_get_name(entry);
280 uint8_t busnum = 0, devaddr = 0;
282 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
284 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
286 udev_device_unref(udev_dev);
290 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
291 udev_device_unref(udev_dev);
294 udev_enumerate_unref(enumerator);
296 return LIBUSB_SUCCESS;
299 void linux_udev_hotplug_poll(void)
301 struct udev_device* udev_dev;
303 usbi_mutex_static_lock(&linux_hotplug_lock);
305 udev_dev = udev_monitor_receive_device(udev_monitor);
307 usbi_dbg("Handling hotplug event from hotplug_poll");
308 udev_hotplug_event(udev_dev);
311 usbi_mutex_static_unlock(&linux_hotplug_lock);