Misc: Trim and consolidate header file usage
[platform/upstream/libusb.git] / libusb / os / linux_udev.c
1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
2 /*
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>
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "libusbi.h"
24 #include "linux_usbfs.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libudev.h>
29 #include <poll.h>
30 #include <pthread.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 /* udev context */
35 static struct udev *udev_ctx = NULL;
36 static int udev_monitor_fd = -1;
37 static int udev_control_pipe[2] = {-1, -1};
38 static struct udev_monitor *udev_monitor = NULL;
39 static pthread_t linux_event_thread;
40
41 static void udev_hotplug_event(struct udev_device* udev_dev);
42 static void *linux_udev_event_thread_main(void *arg);
43
44 int linux_udev_start_event_monitor(void)
45 {
46         int r;
47
48         assert(udev_ctx == NULL);
49         udev_ctx = udev_new();
50         if (!udev_ctx) {
51                 usbi_err(NULL, "could not create udev context");
52                 goto err;
53         }
54
55         udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
56         if (!udev_monitor) {
57                 usbi_err(NULL, "could not initialize udev monitor");
58                 goto err_free_ctx;
59         }
60
61         r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device");
62         if (r) {
63                 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
64                 goto err_free_monitor;
65         }
66
67         if (udev_monitor_enable_receiving(udev_monitor)) {
68                 usbi_err(NULL, "failed to enable the udev monitor");
69                 goto err_free_monitor;
70         }
71
72         udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
73
74 #if defined(FD_CLOEXEC)
75         /* Make sure the udev file descriptor is marked as CLOEXEC */
76         r = fcntl(udev_monitor_fd, F_GETFD);
77         if (r == -1) {
78                 usbi_err(NULL, "failed to get udev monitor fd flags, errno=%d", errno);
79                 goto err_free_monitor;
80         }
81         if (!(r & FD_CLOEXEC)) {
82                 if (fcntl(udev_monitor_fd, F_SETFD, r | FD_CLOEXEC) == -1) {
83                         usbi_err(NULL, "failed to set udev monitor fd flags, errno=%d", errno);
84                         goto err_free_monitor;
85                 }
86         }
87 #endif
88
89         /* Some older versions of udev are not non-blocking by default,
90          * so make sure this is set */
91         r = fcntl(udev_monitor_fd, F_GETFL);
92         if (r == -1) {
93                 usbi_err(NULL, "failed to get udev monitor fd status flags, errno=%d", errno);
94                 goto err_free_monitor;
95         }
96         if (!(r & O_NONBLOCK)) {
97                 if (fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK) == -1) {
98                         usbi_err(NULL, "failed to set udev monitor fd status flags, errno=%d", errno);
99                         goto err_free_monitor;
100                 }
101         }
102
103         r = usbi_pipe(udev_control_pipe);
104         if (r) {
105                 usbi_err(NULL, "could not create udev control pipe");
106                 goto err_free_monitor;
107         }
108
109         r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
110         if (r) {
111                 usbi_err(NULL, "creating hotplug event thread (%d)", r);
112                 goto err_close_pipe;
113         }
114
115         return LIBUSB_SUCCESS;
116
117 err_close_pipe:
118         close(udev_control_pipe[0]);
119         close(udev_control_pipe[1]);
120 err_free_monitor:
121         udev_monitor_unref(udev_monitor);
122         udev_monitor = NULL;
123         udev_monitor_fd = -1;
124 err_free_ctx:
125         udev_unref(udev_ctx);
126 err:
127         udev_ctx = NULL;
128         return LIBUSB_ERROR_OTHER;
129 }
130
131 int linux_udev_stop_event_monitor(void)
132 {
133         char dummy = 1;
134         int r;
135
136         assert(udev_ctx != NULL);
137         assert(udev_monitor != NULL);
138         assert(udev_monitor_fd != -1);
139
140         /* Write some dummy data to the control pipe and
141          * wait for the thread to exit */
142         r = write(udev_control_pipe[1], &dummy, sizeof(dummy));
143         if (r <= 0) {
144                 usbi_warn(NULL, "udev control pipe signal failed");
145         }
146         pthread_join(linux_event_thread, NULL);
147
148         /* Release the udev monitor */
149         udev_monitor_unref(udev_monitor);
150         udev_monitor = NULL;
151         udev_monitor_fd = -1;
152
153         /* Clean up the udev context */
154         udev_unref(udev_ctx);
155         udev_ctx = NULL;
156
157         /* close and reset control pipe */
158         close(udev_control_pipe[0]);
159         close(udev_control_pipe[1]);
160         udev_control_pipe[0] = -1;
161         udev_control_pipe[1] = -1;
162
163         return LIBUSB_SUCCESS;
164 }
165
166 static void *linux_udev_event_thread_main(void *arg)
167 {
168         char dummy;
169         int r;
170         ssize_t nb;
171         struct udev_device* udev_dev;
172         struct pollfd fds[] = {
173                 {.fd = udev_control_pipe[0],
174                  .events = POLLIN},
175                 {.fd = udev_monitor_fd,
176                  .events = POLLIN},
177         };
178
179         usbi_dbg("udev event thread entering.");
180
181         while ((r = poll(fds, 2, -1)) >= 0 || errno == EINTR) {
182                 if (r < 0) {
183                         /* temporary failure */
184                         continue;
185                 }
186                 if (fds[0].revents & POLLIN) {
187                         /* activity on control pipe, read the byte and exit */
188                         nb = read(udev_control_pipe[0], &dummy, sizeof(dummy));
189                         if (nb <= 0) {
190                                 usbi_warn(NULL, "udev control pipe read failed");
191                         }
192                         break;
193                 }
194                 if (fds[1].revents & POLLIN) {
195                         usbi_mutex_static_lock(&linux_hotplug_lock);
196                         udev_dev = udev_monitor_receive_device(udev_monitor);
197                         if (udev_dev)
198                                 udev_hotplug_event(udev_dev);
199                         usbi_mutex_static_unlock(&linux_hotplug_lock);
200                 }
201         }
202
203         usbi_dbg("udev event thread exiting");
204
205         return NULL;
206 }
207
208 static int udev_device_info(struct libusb_context *ctx, int detached,
209                             struct udev_device *udev_dev, uint8_t *busnum,
210                             uint8_t *devaddr, const char **sys_name) {
211         const char *dev_node;
212
213         dev_node = udev_device_get_devnode(udev_dev);
214         if (!dev_node) {
215                 return LIBUSB_ERROR_OTHER;
216         }
217
218         *sys_name = udev_device_get_sysname(udev_dev);
219         if (!*sys_name) {
220                 return LIBUSB_ERROR_OTHER;
221         }
222
223         return linux_get_device_address(ctx, detached, busnum, devaddr,
224                                         dev_node, *sys_name, -1);
225 }
226
227 static void udev_hotplug_event(struct udev_device* udev_dev)
228 {
229         const char* udev_action;
230         const char* sys_name = NULL;
231         uint8_t busnum = 0, devaddr = 0;
232         int detached;
233         int r;
234
235         do {
236                 udev_action = udev_device_get_action(udev_dev);
237                 if (!udev_action) {
238                         break;
239                 }
240
241                 detached = !strncmp(udev_action, "remove", 6);
242
243                 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
244                 if (LIBUSB_SUCCESS != r) {
245                         break;
246                 }
247
248                 usbi_dbg("udev hotplug event. action: %s.", udev_action);
249
250                 if (strncmp(udev_action, "add", 3) == 0) {
251                         linux_hotplug_enumerate(busnum, devaddr, sys_name);
252                 } else if (detached) {
253                         linux_device_disconnected(busnum, devaddr);
254                 } else if (strncmp(udev_action, "bind", 4) == 0) {
255                         /* silently ignore "known unhandled" action */
256                 } else {
257                         usbi_err(NULL, "ignoring udev action %s", udev_action);
258                 }
259         } while (0);
260
261         udev_device_unref(udev_dev);
262 }
263
264 int linux_udev_scan_devices(struct libusb_context *ctx)
265 {
266         struct udev_enumerate *enumerator;
267         struct udev_list_entry *devices, *entry;
268         struct udev_device *udev_dev;
269         const char *sys_name;
270         int r;
271
272         assert(udev_ctx != NULL);
273
274         enumerator = udev_enumerate_new(udev_ctx);
275         if (NULL == enumerator) {
276                 usbi_err(ctx, "error creating udev enumerator");
277                 return LIBUSB_ERROR_OTHER;
278         }
279
280         udev_enumerate_add_match_subsystem(enumerator, "usb");
281         udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device");
282         udev_enumerate_scan_devices(enumerator);
283         devices = udev_enumerate_get_list_entry(enumerator);
284
285         entry = NULL;
286         udev_list_entry_foreach(entry, devices) {
287                 const char *path = udev_list_entry_get_name(entry);
288                 uint8_t busnum = 0, devaddr = 0;
289
290                 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
291
292                 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
293                 if (r) {
294                         udev_device_unref(udev_dev);
295                         continue;
296                 }
297
298                 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
299                 udev_device_unref(udev_dev);
300         }
301
302         udev_enumerate_unref(enumerator);
303
304         return LIBUSB_SUCCESS;
305 }
306
307 void linux_udev_hotplug_poll(void)
308 {
309         struct udev_device* udev_dev;
310
311         usbi_mutex_static_lock(&linux_hotplug_lock);
312         do {
313                 udev_dev = udev_monitor_receive_device(udev_monitor);
314                 if (udev_dev) {
315                         usbi_dbg("Handling hotplug event from hotplug_poll");
316                         udev_hotplug_event(udev_dev);
317                 }
318         } while (udev_dev);
319         usbi_mutex_static_unlock(&linux_hotplug_lock);
320 }