linux_udev.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "config.h"
  23. #include <assert.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. #include "libusb.h"
  40. #include "libusbi.h"
  41. #include "linux_usbfs.h"
  42. /* udev context */
  43. static struct udev *udev_ctx = NULL;
  44. static int udev_monitor_fd = -1;
  45. static int udev_control_pipe[2] = {-1, -1};
  46. static struct udev_monitor *udev_monitor = NULL;
  47. static pthread_t linux_event_thread;
  48. static void udev_hotplug_event(struct udev_device* udev_dev);
  49. static void *linux_udev_event_thread_main(void *arg);
  50. int linux_udev_start_event_monitor(void)
  51. {
  52. int r;
  53. assert(udev_ctx == NULL);
  54. udev_ctx = udev_new();
  55. if (!udev_ctx) {
  56. usbi_err(NULL, "could not create udev context");
  57. return LIBUSB_ERROR_OTHER;
  58. }
  59. udev_monitor = udev_monitor_new_from_netlink(udev_ctx, "udev");
  60. if (!udev_monitor) {
  61. usbi_err(NULL, "could not initialize udev monitor");
  62. goto err_free_ctx;
  63. }
  64. r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0);
  65. if (r) {
  66. usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem");
  67. goto err_free_monitor;
  68. }
  69. if (udev_monitor_enable_receiving(udev_monitor)) {
  70. usbi_err(NULL, "failed to enable the udev monitor");
  71. goto err_free_monitor;
  72. }
  73. udev_monitor_fd = udev_monitor_get_fd(udev_monitor);
  74. /* Some older versions of udev are not non-blocking by default,
  75. * so make sure this is set */
  76. r = fcntl(udev_monitor_fd, F_GETFL);
  77. if (r == -1) {
  78. usbi_err(NULL, "getting udev monitor fd flags (%d)", errno);
  79. goto err_free_monitor;
  80. }
  81. r = fcntl(udev_monitor_fd, F_SETFL, r | O_NONBLOCK);
  82. if (r) {
  83. usbi_err(NULL, "setting udev monitor fd flags (%d)", errno);
  84. goto err_free_monitor;
  85. }
  86. r = usbi_pipe(udev_control_pipe);
  87. if (r) {
  88. usbi_err(NULL, "could not create udev control pipe");
  89. goto err_free_monitor;
  90. }
  91. r = pthread_create(&linux_event_thread, NULL, linux_udev_event_thread_main, NULL);
  92. if (r) {
  93. usbi_err(NULL, "creating hotplug event thread (%d)", r);
  94. goto err_close_pipe;
  95. }
  96. return LIBUSB_SUCCESS;
  97. err_close_pipe:
  98. close(udev_control_pipe[0]);
  99. close(udev_control_pipe[1]);
  100. err_free_monitor:
  101. udev_monitor_unref(udev_monitor);
  102. udev_monitor = NULL;
  103. udev_monitor_fd = -1;
  104. err_free_ctx:
  105. udev_unref(udev_ctx);
  106. udev_ctx = NULL;
  107. return LIBUSB_ERROR_OTHER;
  108. }
  109. int linux_udev_stop_event_monitor(void)
  110. {
  111. char dummy = 1;
  112. int r;
  113. assert(udev_ctx != NULL);
  114. assert(udev_monitor != NULL);
  115. assert(udev_monitor_fd != -1);
  116. /* Write some dummy data to the control pipe and
  117. * wait for the thread to exit */
  118. r = usbi_write(udev_control_pipe[1], &dummy, sizeof(dummy));
  119. if (r <= 0) {
  120. usbi_warn(NULL, "udev control pipe signal failed");
  121. }
  122. pthread_join(linux_event_thread, NULL);
  123. /* Release the udev monitor */
  124. udev_monitor_unref(udev_monitor);
  125. udev_monitor = NULL;
  126. udev_monitor_fd = -1;
  127. /* Clean up the udev context */
  128. udev_unref(udev_ctx);
  129. udev_ctx = NULL;
  130. /* close and reset control pipe */
  131. close(udev_control_pipe[0]);
  132. close(udev_control_pipe[1]);
  133. udev_control_pipe[0] = -1;
  134. udev_control_pipe[1] = -1;
  135. return LIBUSB_SUCCESS;
  136. }
  137. static void *linux_udev_event_thread_main(void __attribute__((unused)) *arg)
  138. {
  139. char dummy;
  140. int r;
  141. struct udev_device* udev_dev;
  142. struct pollfd fds[] = {
  143. {.fd = udev_control_pipe[0],
  144. .events = POLLIN},
  145. {.fd = udev_monitor_fd,
  146. .events = POLLIN},
  147. };
  148. usbi_dbg("udev event thread entering.");
  149. while (poll(fds, 2, -1) >= 0) {
  150. if (fds[0].revents & POLLIN) {
  151. /* activity on control pipe, read the byte and exit */
  152. r = usbi_read(udev_control_pipe[0], &dummy, sizeof(dummy));
  153. if (r <= 0) {
  154. usbi_warn(NULL, "udev control pipe read failed");
  155. }
  156. break;
  157. }
  158. if (fds[1].revents & POLLIN) {
  159. usbi_mutex_static_lock(&linux_hotplug_lock);
  160. udev_dev = udev_monitor_receive_device(udev_monitor);
  161. if (udev_dev)
  162. udev_hotplug_event(udev_dev);
  163. usbi_mutex_static_unlock(&linux_hotplug_lock);
  164. }
  165. }
  166. usbi_dbg("udev event thread exiting");
  167. return NULL;
  168. }
  169. static int udev_device_info(struct libusb_context *ctx, int detached,
  170. struct udev_device *udev_dev, uint8_t *busnum,
  171. uint8_t *devaddr, const char **sys_name) {
  172. const char *dev_node;
  173. dev_node = udev_device_get_devnode(udev_dev);
  174. if (!dev_node) {
  175. return LIBUSB_ERROR_OTHER;
  176. }
  177. *sys_name = udev_device_get_sysname(udev_dev);
  178. if (!*sys_name) {
  179. return LIBUSB_ERROR_OTHER;
  180. }
  181. return linux_get_device_address(ctx, detached, busnum, devaddr,
  182. dev_node, *sys_name);
  183. }
  184. static void udev_hotplug_event(struct udev_device* udev_dev)
  185. {
  186. const char* udev_action;
  187. const char* sys_name = NULL;
  188. uint8_t busnum = 0, devaddr = 0;
  189. int detached;
  190. int r;
  191. do {
  192. udev_action = udev_device_get_action(udev_dev);
  193. if (!udev_action) {
  194. break;
  195. }
  196. detached = !strncmp(udev_action, "remove", 6);
  197. r = udev_device_info(NULL, detached, udev_dev, &busnum, &devaddr, &sys_name);
  198. if (LIBUSB_SUCCESS != r) {
  199. break;
  200. }
  201. usbi_dbg("udev hotplug event. action: %s.", udev_action);
  202. if (strncmp(udev_action, "add", 3) == 0) {
  203. linux_hotplug_enumerate(busnum, devaddr, sys_name);
  204. } else if (detached) {
  205. linux_device_disconnected(busnum, devaddr, sys_name);
  206. } else {
  207. usbi_err(NULL, "ignoring udev action %s", udev_action);
  208. }
  209. } while (0);
  210. udev_device_unref(udev_dev);
  211. }
  212. int linux_udev_scan_devices(struct libusb_context *ctx)
  213. {
  214. struct udev_enumerate *enumerator;
  215. struct udev_list_entry *devices, *entry;
  216. struct udev_device *udev_dev;
  217. const char *sys_name;
  218. int r;
  219. assert(udev_ctx != NULL);
  220. enumerator = udev_enumerate_new(udev_ctx);
  221. if (NULL == enumerator) {
  222. usbi_err(ctx, "error creating udev enumerator");
  223. return LIBUSB_ERROR_OTHER;
  224. }
  225. udev_enumerate_add_match_subsystem(enumerator, "usb");
  226. udev_enumerate_scan_devices(enumerator);
  227. devices = udev_enumerate_get_list_entry(enumerator);
  228. udev_list_entry_foreach(entry, devices) {
  229. const char *path = udev_list_entry_get_name(entry);
  230. uint8_t busnum = 0, devaddr = 0;
  231. udev_dev = udev_device_new_from_syspath(udev_ctx, path);
  232. r = udev_device_info(ctx, 0, udev_dev, &busnum, &devaddr, &sys_name);
  233. if (r) {
  234. udev_device_unref(udev_dev);
  235. continue;
  236. }
  237. linux_enumerate_device(ctx, busnum, devaddr, sys_name);
  238. udev_device_unref(udev_dev);
  239. }
  240. udev_enumerate_unref(enumerator);
  241. return LIBUSB_SUCCESS;
  242. }
  243. void linux_udev_hotplug_poll(void)
  244. {
  245. struct udev_device* udev_dev;
  246. usbi_mutex_static_lock(&linux_hotplug_lock);
  247. do {
  248. udev_dev = udev_monitor_receive_device(udev_monitor);
  249. if (udev_dev) {
  250. usbi_dbg("Handling hotplug event from hotplug_poll");
  251. udev_hotplug_event(udev_dev);
  252. }
  253. } while (udev_dev);
  254. usbi_mutex_static_unlock(&linux_hotplug_lock);
  255. }