usbip_host_common.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015-2016 Samsung Electronics
  4. * Igor Kotrasinski <i.kotrasinsk@samsung.com>
  5. * Krzysztof Opasiak <k.opasiak@samsung.com>
  6. *
  7. * Refactored from usbip_host_driver.c, which is:
  8. * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  9. * 2005-2007 Takahiro Hirofuchi
  10. */
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <libudev.h>
  17. #include "usbip_common.h"
  18. #include "usbip_host_common.h"
  19. #include "list.h"
  20. #include "sysfs_utils.h"
  21. extern struct udev *udev_context;
  22. static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
  23. {
  24. char status_attr_path[SYSFS_PATH_MAX];
  25. int size;
  26. int fd;
  27. int length;
  28. char status[2] = { 0 };
  29. int value = 0;
  30. size = snprintf(status_attr_path, sizeof(status_attr_path),
  31. "%s/usbip_status", udev->path);
  32. if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
  33. err("usbip_status path length %i >= %lu or < 0", size,
  34. (long unsigned)sizeof(status_attr_path));
  35. return -1;
  36. }
  37. fd = open(status_attr_path, O_RDONLY);
  38. if (fd < 0) {
  39. err("error opening attribute %s", status_attr_path);
  40. return -1;
  41. }
  42. length = read(fd, status, 1);
  43. if (length < 0) {
  44. err("error reading attribute %s", status_attr_path);
  45. close(fd);
  46. return -1;
  47. }
  48. value = atoi(status);
  49. close(fd);
  50. return value;
  51. }
  52. static
  53. struct usbip_exported_device *usbip_exported_device_new(
  54. struct usbip_host_driver *hdriver, const char *sdevpath)
  55. {
  56. struct usbip_exported_device *edev = NULL;
  57. struct usbip_exported_device *edev_old;
  58. size_t size;
  59. int i;
  60. edev = calloc(1, sizeof(struct usbip_exported_device));
  61. edev->sudev =
  62. udev_device_new_from_syspath(udev_context, sdevpath);
  63. if (!edev->sudev) {
  64. err("udev_device_new_from_syspath: %s", sdevpath);
  65. goto err;
  66. }
  67. if (hdriver->ops.read_device(edev->sudev, &edev->udev) < 0)
  68. goto err;
  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. /* vudc does not support reading interfaces */
  84. if (!hdriver->ops.read_interface)
  85. break;
  86. hdriver->ops.read_interface(&edev->udev, i, &edev->uinf[i]);
  87. }
  88. return edev;
  89. err:
  90. if (edev->sudev)
  91. udev_device_unref(edev->sudev);
  92. if (edev)
  93. free(edev);
  94. return NULL;
  95. }
  96. static int refresh_exported_devices(struct usbip_host_driver *hdriver)
  97. {
  98. struct usbip_exported_device *edev;
  99. struct udev_enumerate *enumerate;
  100. struct udev_list_entry *devices, *dev_list_entry;
  101. struct udev_device *dev;
  102. const char *path;
  103. enumerate = udev_enumerate_new(udev_context);
  104. udev_enumerate_add_match_subsystem(enumerate, hdriver->udev_subsystem);
  105. udev_enumerate_scan_devices(enumerate);
  106. devices = udev_enumerate_get_list_entry(enumerate);
  107. udev_list_entry_foreach(dev_list_entry, devices) {
  108. path = udev_list_entry_get_name(dev_list_entry);
  109. dev = udev_device_new_from_syspath(udev_context,
  110. path);
  111. if (dev == NULL)
  112. continue;
  113. /* Check whether device uses usbip driver. */
  114. if (hdriver->ops.is_my_device(dev)) {
  115. edev = usbip_exported_device_new(hdriver, path);
  116. if (!edev) {
  117. dbg("usbip_exported_device_new failed");
  118. continue;
  119. }
  120. list_add(&edev->node, &hdriver->edev_list);
  121. hdriver->ndevs++;
  122. }
  123. }
  124. return 0;
  125. }
  126. static void usbip_exported_device_destroy(struct list_head *devs)
  127. {
  128. struct list_head *i, *tmp;
  129. struct usbip_exported_device *edev;
  130. list_for_each_safe(i, tmp, devs) {
  131. edev = list_entry(i, struct usbip_exported_device, node);
  132. list_del(i);
  133. free(edev);
  134. }
  135. }
  136. int usbip_generic_driver_open(struct usbip_host_driver *hdriver)
  137. {
  138. int rc;
  139. udev_context = udev_new();
  140. if (!udev_context) {
  141. err("udev_new failed");
  142. return -1;
  143. }
  144. rc = refresh_exported_devices(hdriver);
  145. if (rc < 0)
  146. goto err;
  147. return 0;
  148. err:
  149. udev_unref(udev_context);
  150. return -1;
  151. }
  152. void usbip_generic_driver_close(struct usbip_host_driver *hdriver)
  153. {
  154. if (!hdriver)
  155. return;
  156. usbip_exported_device_destroy(&hdriver->edev_list);
  157. udev_unref(udev_context);
  158. }
  159. int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver)
  160. {
  161. int rc;
  162. usbip_exported_device_destroy(&hdriver->edev_list);
  163. hdriver->ndevs = 0;
  164. INIT_LIST_HEAD(&hdriver->edev_list);
  165. rc = refresh_exported_devices(hdriver);
  166. if (rc < 0)
  167. return -1;
  168. return 0;
  169. }
  170. int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
  171. {
  172. char attr_name[] = "usbip_sockfd";
  173. char sockfd_attr_path[SYSFS_PATH_MAX];
  174. int size;
  175. char sockfd_buff[30];
  176. int ret;
  177. if (edev->status != SDEV_ST_AVAILABLE) {
  178. dbg("device not available: %s", edev->udev.busid);
  179. switch (edev->status) {
  180. case SDEV_ST_ERROR:
  181. dbg("status SDEV_ST_ERROR");
  182. ret = ST_DEV_ERR;
  183. break;
  184. case SDEV_ST_USED:
  185. dbg("status SDEV_ST_USED");
  186. ret = ST_DEV_BUSY;
  187. break;
  188. default:
  189. dbg("status unknown: 0x%x", edev->status);
  190. ret = -1;
  191. }
  192. return ret;
  193. }
  194. /* only the first interface is true */
  195. size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
  196. edev->udev.path, attr_name);
  197. if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
  198. err("exported device path length %i >= %lu or < 0", size,
  199. (long unsigned)sizeof(sockfd_attr_path));
  200. return -1;
  201. }
  202. size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
  203. if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
  204. err("socket length %i >= %lu or < 0", size,
  205. (long unsigned)sizeof(sockfd_buff));
  206. return -1;
  207. }
  208. ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
  209. strlen(sockfd_buff));
  210. if (ret < 0) {
  211. err("write_sysfs_attribute failed: sockfd %s to %s",
  212. sockfd_buff, sockfd_attr_path);
  213. return ret;
  214. }
  215. info("connect: %s", edev->udev.busid);
  216. return ret;
  217. }
  218. struct usbip_exported_device *usbip_generic_get_device(
  219. struct usbip_host_driver *hdriver, int num)
  220. {
  221. struct list_head *i;
  222. struct usbip_exported_device *edev;
  223. int cnt = 0;
  224. list_for_each(i, &hdriver->edev_list) {
  225. edev = list_entry(i, struct usbip_exported_device, node);
  226. if (num == cnt)
  227. return edev;
  228. cnt++;
  229. }
  230. return NULL;
  231. }