linux_udev.c 6.2 KB

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