usbip_bind.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <libudev.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <getopt.h>
  24. #include "usbip_common.h"
  25. #include "utils.h"
  26. #include "usbip.h"
  27. #include "sysfs_utils.h"
  28. enum unbind_status {
  29. UNBIND_ST_OK,
  30. UNBIND_ST_USBIP_HOST,
  31. UNBIND_ST_FAILED
  32. };
  33. static const char usbip_bind_usage_string[] =
  34. "usbip bind <args>\n"
  35. " -b, --busid=<busid> Bind " USBIP_HOST_DRV_NAME ".ko to device "
  36. "on <busid>\n";
  37. void usbip_bind_usage(void)
  38. {
  39. printf("usage: %s", usbip_bind_usage_string);
  40. }
  41. /* call at unbound state */
  42. static int bind_usbip(char *busid)
  43. {
  44. char attr_name[] = "bind";
  45. char bind_attr_path[SYSFS_PATH_MAX];
  46. int rc = -1;
  47. snprintf(bind_attr_path, sizeof(bind_attr_path), "%s/%s/%s/%s/%s/%s",
  48. SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
  49. SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
  50. rc = write_sysfs_attribute(bind_attr_path, busid, strlen(busid));
  51. if (rc < 0) {
  52. err("error binding device %s to driver: %s", busid,
  53. strerror(errno));
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. /* buggy driver may cause dead lock */
  59. static int unbind_other(char *busid)
  60. {
  61. enum unbind_status status = UNBIND_ST_OK;
  62. char attr_name[] = "unbind";
  63. char unbind_attr_path[SYSFS_PATH_MAX];
  64. int rc = -1;
  65. struct udev *udev;
  66. struct udev_device *dev;
  67. const char *driver;
  68. const char *bDevClass;
  69. /* Create libudev context. */
  70. udev = udev_new();
  71. /* Get the device. */
  72. dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
  73. if (!dev) {
  74. dbg("unable to find device with bus ID %s", busid);
  75. goto err_close_busid_dev;
  76. }
  77. /* Check what kind of device it is. */
  78. bDevClass = udev_device_get_sysattr_value(dev, "bDeviceClass");
  79. if (!bDevClass) {
  80. dbg("unable to get bDevClass device attribute");
  81. goto err_close_busid_dev;
  82. }
  83. if (!strncmp(bDevClass, "09", strlen(bDevClass))) {
  84. dbg("skip unbinding of hub");
  85. goto err_close_busid_dev;
  86. }
  87. /* Get the device driver. */
  88. driver = udev_device_get_driver(dev);
  89. if (!driver) {
  90. /* No driver bound to this device. */
  91. goto out;
  92. }
  93. if (!strncmp(USBIP_HOST_DRV_NAME, driver,
  94. strlen(USBIP_HOST_DRV_NAME))) {
  95. /* Already bound to usbip-host. */
  96. status = UNBIND_ST_USBIP_HOST;
  97. goto out;
  98. }
  99. /* Unbind device from driver. */
  100. snprintf(unbind_attr_path, sizeof(unbind_attr_path), "%s/%s/%s/%s/%s/%s",
  101. SYSFS_MNT_PATH, SYSFS_BUS_NAME, SYSFS_BUS_TYPE,
  102. SYSFS_DRIVERS_NAME, driver, attr_name);
  103. rc = write_sysfs_attribute(unbind_attr_path, busid, strlen(busid));
  104. if (rc < 0) {
  105. err("error unbinding device %s from driver", busid);
  106. goto err_close_busid_dev;
  107. }
  108. goto out;
  109. err_close_busid_dev:
  110. status = UNBIND_ST_FAILED;
  111. out:
  112. udev_device_unref(dev);
  113. udev_unref(udev);
  114. return status;
  115. }
  116. static int bind_device(char *busid)
  117. {
  118. int rc;
  119. struct udev *udev;
  120. struct udev_device *dev;
  121. /* Check whether the device with this bus ID exists. */
  122. udev = udev_new();
  123. dev = udev_device_new_from_subsystem_sysname(udev, "usb", busid);
  124. if (!dev) {
  125. err("device with the specified bus ID does not exist");
  126. return -1;
  127. }
  128. udev_unref(udev);
  129. rc = unbind_other(busid);
  130. if (rc == UNBIND_ST_FAILED) {
  131. err("could not unbind driver from device on busid %s", busid);
  132. return -1;
  133. } else if (rc == UNBIND_ST_USBIP_HOST) {
  134. err("device on busid %s is already bound to %s", busid,
  135. USBIP_HOST_DRV_NAME);
  136. return -1;
  137. }
  138. rc = modify_match_busid(busid, 1);
  139. if (rc < 0) {
  140. err("unable to bind device on %s", busid);
  141. return -1;
  142. }
  143. rc = bind_usbip(busid);
  144. if (rc < 0) {
  145. err("could not bind device to %s", USBIP_HOST_DRV_NAME);
  146. modify_match_busid(busid, 0);
  147. return -1;
  148. }
  149. info("bind device on busid %s: complete", busid);
  150. return 0;
  151. }
  152. int usbip_bind(int argc, char *argv[])
  153. {
  154. static const struct option opts[] = {
  155. { "busid", required_argument, NULL, 'b' },
  156. { NULL, 0, NULL, 0 }
  157. };
  158. int opt;
  159. int ret = -1;
  160. for (;;) {
  161. opt = getopt_long(argc, argv, "b:", opts, NULL);
  162. if (opt == -1)
  163. break;
  164. switch (opt) {
  165. case 'b':
  166. ret = bind_device(optarg);
  167. goto out;
  168. default:
  169. goto err_out;
  170. }
  171. }
  172. err_out:
  173. usbip_bind_usage();
  174. out:
  175. return ret;
  176. }