Add hotplug support to the Linux backend.
[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 <config.h>
24 #include <ctype.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/utsname.h>
36 #include <sys/socket.h>
37 #include <unistd.h>
38 #include <libudev.h>
39
40 #include "libusb.h"
41 #include "libusbi.h"
42 #include "linux_usbfs.h"
43
44 /* udev context */
45 static struct udev *udev_ctx = NULL;
46 static int udev_monitor_fd = -1;
47 static struct udev_monitor *udev_monitor = NULL;
48 static pthread_t linux_event_thread;
49
50 static void udev_hotplug_event(void);
51 static void *linux_udev_event_thread_main(void *arg);
52
53 int linux_udev_start_event_monitor(void)
54 {
55         int r;
56
57         if (NULL == udev_ctx) {
58                 udev_ctx = udev_new();
59                 if (!udev_ctx) {
60                         return LIBUSB_ERROR_OTHER;
61                 }
62         }
63
64         udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
65         if (!udev_monitor) {
66                 usbi_err(NULL, "could not initialize udev monitor");
67                 return LIBUSB_ERROR_OTHER;
68         }
69
70         r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
71         if (r) {
72                 usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
73                 return LIBUSB_ERROR_OTHER;
74         }
75
76         if (udev_monitor_enable_receiving(udev_monitor)) {
77                 usbi_err(NULL, "failed to enable the udev monitor");
78                 return LIBUSB_ERROR_OTHER;
79         }
80
81         udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
82
83         pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
84
85         return LIBUSB_SUCCESS;
86 }
87
88 int linux_udev_stop_event_monitor(void)
89 {
90         if (-1 == udev_monitor_fd) {
91                 /* this should never happen */
92                 return LIBUSB_ERROR_OTHER;
93         }
94
95         /* Cancel the event thread. This is the only way to garauntee the thread
96            exits since closing the monitor fd won't necessarily cause poll
97            to return. */
98         pthread_cancel(linux_event_thread);
99
100         /* Release the udev monitor */
101         udev_monitor_unref(udev_monitor);
102         udev_monitor = NULL;
103         udev_monitor_fd = -1;
104
105         /* Clean up the udev context */
106         udev_unref(udev_ctx);
107         udev_ctx = NULL;
108
109         return LIBUSB_SUCCESS;
110 }
111
112 static void *linux_udev_event_thread_main(void *arg)
113 {
114         struct pollfd fds = {.fd = udev_monitor_fd,
115                              .events = POLLIN};
116
117         usbi_dbg("udev event thread entering.");
118
119         while (1 == poll(&fds, 1, -1)) {
120                 if (NULL == udev_monitor || POLLIN != fds.revents) {
121                         break;
122                 }
123
124                 udev_hotplug_event();
125         }
126
127         usbi_dbg("udev event thread exiting");
128
129         return NULL;
130 }
131
132 static int udev_device_info(struct libusb_context *ctx, int detached,
133                             struct udev_device *udev_dev, uint8_t *busnum,
134                             uint8_t *devaddr, const char **sys_name) {
135         const char *dev_node;
136
137         dev_node = udev_device_get_devnode(udev_dev);
138         if (!dev_node) {
139                 return LIBUSB_ERROR_OTHER;
140         }
141
142         *sys_name = udev_device_get_sysname(udev_dev);
143         if (!*sys_name) {
144                 return LIBUSB_ERROR_OTHER;
145         }
146
147         return linux_get_device_address(ctx, detached, busnum, devaddr,
148                                         dev_node, *sys_name);
149 }
150
151 static void udev_hotplug_event(void)
152 {
153         struct udev_device* udev_dev;
154         const char* udev_action;
155         const char* sys_name = NULL;
156         uint8_t busnum = 0, devaddr = 0;
157         int detached;
158         int r;
159
160         if (NULL == udev_monitor) {
161                 return;
162         }
163
164         do {
165                 udev_dev = udev_monitor_receive_device(udev_monitor);
166                 if (!udev_dev) {
167                         usbi_err(NULL, "failed to read data from udev monitor socket.");
168                         return;
169                 }
170
171                 udev_action = udev_device_get_action(udev_dev);
172                 if (!udev_action) {
173                         break;
174                 }
175
176                 detached = !strncmp(udev_action, "remove", 6);
177
178                 r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
179                 if (LIBUSB_SUCCESS != r) {
180                         break;
181                 }
182
183                 usbi_dbg("udev hotplug event. action: %s.", udev_action);
184
185                 if (strncmp(udev_action, "add", 3) == 0) {
186                         linux_hotplug_enumerate(busnum, devaddr, sys_name);
187                 } else if (detached) {
188                         linux_hotplug_disconnected(busnum, devaddr, sys_name);
189                 } else {
190                         usbi_err(NULL, "ignoring udev action %s", udev_action);
191                 }
192         } while (0);
193
194         udev_device_unref(udev_dev);
195 }
196
197 int linux_udev_scan_devices(struct libusb_context *ctx)
198 {
199         struct udev_enumerate *enumerator;
200         struct udev_list_entry *devices, *entry;
201         struct udev_device *udev_dev;
202         const char *sys_name;
203         int r;
204
205         if (NULL == udev_ctx) {
206                 udev_ctx = udev_new();
207                 if (!udev_ctx) {
208                         return LIBUSB_ERROR_OTHER;
209                 }
210         }
211
212         enumerator = udev_enumerate_new(udev_ctx);
213         if (NULL == enumerator) {
214                 usbi_err(ctx, "error creating udev enumerator");
215                 return LIBUSB_ERROR_OTHER;
216         }
217
218         udev_enumerate_add_match_subsystem(enumerator, "usb");
219         udev_enumerate_scan_devices(enumerator);
220         devices = udev_enumerate_get_list_entry(enumerator);
221
222         udev_list_entry_foreach(entry, devices) {
223                 const char *path = udev_list_entry_get_name(entry);
224                 uint8_t busnum = 0, devaddr = 0;
225
226                 udev_dev = udev_device_new_from_syspath(udev_ctx, path);
227
228                 r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
229                 if (r) {
230                         udev_device_unref(udev_dev);
231                         continue;
232                 }
233
234                 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
235                 udev_device_unref(udev_dev);
236         }
237
238         udev_enumerate_unref(enumerator);
239
240         return LIBUSB_SUCCESS;
241 }