usbip_host_common.c 6.8 KB

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