usbip_host_driver.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  3. * 2005-2007 Takahiro Hirofuchi
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <libudev.h>
  24. #include "usbip_common.h"
  25. #include "usbip_host_driver.h"
  26. #include "list.h"
  27. #include "sysfs_utils.h"
  28. #undef PROGNAME
  29. #define PROGNAME "libusbip"
  30. struct usbip_host_driver *host_driver;
  31. struct udev *udev_context;
  32. static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
  33. {
  34. char status_attr_path[SYSFS_PATH_MAX];
  35. int fd;
  36. int length;
  37. char status;
  38. int value = 0;
  39. snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
  40. udev->path);
  41. fd = open(status_attr_path, O_RDONLY);
  42. if (fd < 0) {
  43. err("error opening attribute %s", status_attr_path);
  44. return -1;
  45. }
  46. length = read(fd, &status, 1);
  47. if (length < 0) {
  48. err("error reading attribute %s", status_attr_path);
  49. close(fd);
  50. return -1;
  51. }
  52. value = atoi(&status);
  53. return value;
  54. }
  55. static
  56. struct usbip_exported_device *usbip_exported_device_new(const char *sdevpath)
  57. {
  58. struct usbip_exported_device *edev = NULL;
  59. struct usbip_exported_device *edev_old;
  60. size_t size;
  61. int i;
  62. edev = calloc(1, sizeof(struct usbip_exported_device));
  63. edev->sudev = udev_device_new_from_syspath(udev_context, sdevpath);
  64. if (!edev->sudev) {
  65. err("udev_device_new_from_syspath: %s", sdevpath);
  66. goto err;
  67. }
  68. read_usb_device(edev->sudev, &edev->udev);
  69. edev->status = read_attr_usbip_status(&edev->udev);
  70. if (edev->status < 0)
  71. goto err;
  72. /* reallocate buffer to include usb interface data */
  73. size = sizeof(struct usbip_exported_device) +
  74. edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
  75. edev_old = edev;
  76. edev = realloc(edev, size);
  77. if (!edev) {
  78. edev = edev_old;
  79. dbg("realloc failed");
  80. goto err;
  81. }
  82. for (i = 0; i < edev->udev.bNumInterfaces; i++)
  83. read_usb_interface(&edev->udev, i, &edev->uinf[i]);
  84. return edev;
  85. err:
  86. if (edev->sudev)
  87. udev_device_unref(edev->sudev);
  88. if (edev)
  89. free(edev);
  90. return NULL;
  91. }
  92. static int refresh_exported_devices(void)
  93. {
  94. struct usbip_exported_device *edev;
  95. struct udev_enumerate *enumerate;
  96. struct udev_list_entry *devices, *dev_list_entry;
  97. struct udev_device *dev;
  98. const char *path;
  99. const char *driver;
  100. enumerate = udev_enumerate_new(udev_context);
  101. udev_enumerate_add_match_subsystem(enumerate, "usb");
  102. udev_enumerate_scan_devices(enumerate);
  103. devices = udev_enumerate_get_list_entry(enumerate);
  104. udev_list_entry_foreach(dev_list_entry, devices) {
  105. path = udev_list_entry_get_name(dev_list_entry);
  106. dev = udev_device_new_from_syspath(udev_context, path);
  107. if (dev == NULL)
  108. continue;
  109. /* Check whether device uses usbip-host driver. */
  110. driver = udev_device_get_driver(dev);
  111. if (driver != NULL && !strcmp(driver, USBIP_HOST_DRV_NAME)) {
  112. edev = usbip_exported_device_new(path);
  113. if (!edev) {
  114. dbg("usbip_exported_device_new failed");
  115. continue;
  116. }
  117. list_add(&edev->node, &host_driver->edev_list);
  118. host_driver->ndevs++;
  119. }
  120. }
  121. return 0;
  122. }
  123. static void usbip_exported_device_destroy(void)
  124. {
  125. struct list_head *i, *tmp;
  126. struct usbip_exported_device *edev;
  127. list_for_each_safe(i, tmp, &host_driver->edev_list) {
  128. edev = list_entry(i, struct usbip_exported_device, node);
  129. list_del(i);
  130. free(edev);
  131. }
  132. }
  133. int usbip_host_driver_open(void)
  134. {
  135. int rc;
  136. udev_context = udev_new();
  137. if (!udev_context) {
  138. err("udev_new failed");
  139. return -1;
  140. }
  141. host_driver = calloc(1, sizeof(*host_driver));
  142. host_driver->ndevs = 0;
  143. INIT_LIST_HEAD(&host_driver->edev_list);
  144. rc = refresh_exported_devices();
  145. if (rc < 0)
  146. goto err_free_host_driver;
  147. return 0;
  148. err_free_host_driver:
  149. free(host_driver);
  150. host_driver = NULL;
  151. udev_unref(udev_context);
  152. return -1;
  153. }
  154. void usbip_host_driver_close(void)
  155. {
  156. if (!host_driver)
  157. return;
  158. usbip_exported_device_destroy();
  159. free(host_driver);
  160. host_driver = NULL;
  161. udev_unref(udev_context);
  162. }
  163. int usbip_host_refresh_device_list(void)
  164. {
  165. int rc;
  166. usbip_exported_device_destroy();
  167. host_driver->ndevs = 0;
  168. INIT_LIST_HEAD(&host_driver->edev_list);
  169. rc = refresh_exported_devices();
  170. if (rc < 0)
  171. return -1;
  172. return 0;
  173. }
  174. int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
  175. {
  176. char attr_name[] = "usbip_sockfd";
  177. char sockfd_attr_path[SYSFS_PATH_MAX];
  178. char sockfd_buff[30];
  179. int ret;
  180. if (edev->status != SDEV_ST_AVAILABLE) {
  181. dbg("device not available: %s", edev->udev.busid);
  182. switch (edev->status) {
  183. case SDEV_ST_ERROR:
  184. dbg("status SDEV_ST_ERROR");
  185. break;
  186. case SDEV_ST_USED:
  187. dbg("status SDEV_ST_USED");
  188. break;
  189. default:
  190. dbg("status unknown: 0x%x", edev->status);
  191. }
  192. return -1;
  193. }
  194. /* only the first interface is true */
  195. snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
  196. edev->udev.path, attr_name);
  197. snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
  198. ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
  199. strlen(sockfd_buff));
  200. if (ret < 0) {
  201. err("write_sysfs_attribute failed: sockfd %s to %s",
  202. sockfd_buff, sockfd_attr_path);
  203. return ret;
  204. }
  205. info("connect: %s", edev->udev.busid);
  206. return ret;
  207. }
  208. struct usbip_exported_device *usbip_host_get_device(int num)
  209. {
  210. struct list_head *i;
  211. struct usbip_exported_device *edev;
  212. int cnt = 0;
  213. list_for_each(i, &host_driver->edev_list) {
  214. edev = list_entry(i, struct usbip_exported_device, node);
  215. if (num == cnt)
  216. return edev;
  217. else
  218. cnt++;
  219. }
  220. return NULL;
  221. }