vhci_driver.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005-2007 Takahiro Hirofuchi
  4. */
  5. #include "usbip_common.h"
  6. #include "vhci_driver.h"
  7. #include <limits.h>
  8. #include <netdb.h>
  9. #include <libudev.h>
  10. #include <dirent.h>
  11. #include "sysfs_utils.h"
  12. #undef PROGNAME
  13. #define PROGNAME "libusbip"
  14. struct usbip_vhci_driver *vhci_driver;
  15. struct udev *udev_context;
  16. static struct usbip_imported_device *
  17. imported_device_init(struct usbip_imported_device *idev, char *busid)
  18. {
  19. struct udev_device *sudev;
  20. sudev = udev_device_new_from_subsystem_sysname(udev_context,
  21. "usb", busid);
  22. if (!sudev) {
  23. dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
  24. goto err;
  25. }
  26. read_usb_device(sudev, &idev->udev);
  27. udev_device_unref(sudev);
  28. return idev;
  29. err:
  30. return NULL;
  31. }
  32. static int parse_status(const char *value)
  33. {
  34. int ret = 0;
  35. char *c;
  36. /* skip a header line */
  37. c = strchr(value, '\n');
  38. if (!c)
  39. return -1;
  40. c++;
  41. while (*c != '\0') {
  42. int port, status, speed, devid;
  43. int sockfd;
  44. char lbusid[SYSFS_BUS_ID_SIZE];
  45. struct usbip_imported_device *idev;
  46. char hub[3];
  47. ret = sscanf(c, "%2s %d %d %d %x %u %31s\n",
  48. hub, &port, &status, &speed,
  49. &devid, &sockfd, lbusid);
  50. if (ret < 5) {
  51. dbg("sscanf failed: %d", ret);
  52. BUG();
  53. }
  54. dbg("hub %s port %d status %d speed %d devid %x",
  55. hub, port, status, speed, devid);
  56. dbg("sockfd %u lbusid %s", sockfd, lbusid);
  57. /* if a device is connected, look at it */
  58. idev = &vhci_driver->idev[port];
  59. memset(idev, 0, sizeof(*idev));
  60. if (strncmp("hs", hub, 2) == 0)
  61. idev->hub = HUB_SPEED_HIGH;
  62. else /* strncmp("ss", hub, 2) == 0 */
  63. idev->hub = HUB_SPEED_SUPER;
  64. idev->port = port;
  65. idev->status = status;
  66. idev->devid = devid;
  67. idev->busnum = (devid >> 16);
  68. idev->devnum = (devid & 0x0000ffff);
  69. if (idev->status != VDEV_ST_NULL
  70. && idev->status != VDEV_ST_NOTASSIGNED) {
  71. idev = imported_device_init(idev, lbusid);
  72. if (!idev) {
  73. dbg("imported_device_init failed");
  74. return -1;
  75. }
  76. }
  77. /* go to the next line */
  78. c = strchr(c, '\n');
  79. if (!c)
  80. break;
  81. c++;
  82. }
  83. dbg("exit");
  84. return 0;
  85. }
  86. #define MAX_STATUS_NAME 18
  87. static int refresh_imported_device_list(void)
  88. {
  89. const char *attr_status;
  90. char status[MAX_STATUS_NAME+1] = "status";
  91. int i, ret;
  92. for (i = 0; i < vhci_driver->ncontrollers; i++) {
  93. if (i > 0)
  94. snprintf(status, sizeof(status), "status.%d", i);
  95. attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
  96. status);
  97. if (!attr_status) {
  98. err("udev_device_get_sysattr_value failed");
  99. return -1;
  100. }
  101. dbg("controller %d", i);
  102. ret = parse_status(attr_status);
  103. if (ret != 0)
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. static int get_nports(struct udev_device *hc_device)
  109. {
  110. const char *attr_nports;
  111. attr_nports = udev_device_get_sysattr_value(hc_device, "nports");
  112. if (!attr_nports) {
  113. err("udev_device_get_sysattr_value nports failed");
  114. return -1;
  115. }
  116. return (int)strtoul(attr_nports, NULL, 10);
  117. }
  118. static int vhci_hcd_filter(const struct dirent *dirent)
  119. {
  120. return !strncmp(dirent->d_name, "vhci_hcd.", 9);
  121. }
  122. static int get_ncontrollers(void)
  123. {
  124. struct dirent **namelist;
  125. struct udev_device *platform;
  126. int n;
  127. platform = udev_device_get_parent(vhci_driver->hc_device);
  128. if (platform == NULL)
  129. return -1;
  130. n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL);
  131. if (n < 0)
  132. err("scandir failed");
  133. else {
  134. for (int i = 0; i < n; i++)
  135. free(namelist[i]);
  136. free(namelist);
  137. }
  138. return n;
  139. }
  140. /*
  141. * Read the given port's record.
  142. *
  143. * To avoid buffer overflow we will read the entire line and
  144. * validate each part's size. The initial buffer is padded by 4 to
  145. * accommodate the 2 spaces, 1 newline and an additional character
  146. * which is needed to properly validate the 3rd part without it being
  147. * truncated to an acceptable length.
  148. */
  149. static int read_record(int rhport, char *host, unsigned long host_len,
  150. char *port, unsigned long port_len, char *busid)
  151. {
  152. int part;
  153. FILE *file;
  154. char path[PATH_MAX+1];
  155. char *buffer, *start, *end;
  156. char delim[] = {' ', ' ', '\n'};
  157. int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
  158. size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
  159. buffer = malloc(buffer_len);
  160. if (!buffer)
  161. return -1;
  162. snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
  163. file = fopen(path, "r");
  164. if (!file) {
  165. err("fopen");
  166. free(buffer);
  167. return -1;
  168. }
  169. if (fgets(buffer, buffer_len, file) == NULL) {
  170. err("fgets");
  171. free(buffer);
  172. fclose(file);
  173. return -1;
  174. }
  175. fclose(file);
  176. /* validate the length of each of the 3 parts */
  177. start = buffer;
  178. for (part = 0; part < 3; part++) {
  179. end = strchr(start, delim[part]);
  180. if (end == NULL || (end - start) > max_len[part]) {
  181. free(buffer);
  182. return -1;
  183. }
  184. start = end + 1;
  185. }
  186. if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
  187. err("sscanf");
  188. free(buffer);
  189. return -1;
  190. }
  191. free(buffer);
  192. return 0;
  193. }
  194. /* ---------------------------------------------------------------------- */
  195. int usbip_vhci_driver_open(void)
  196. {
  197. int nports;
  198. struct udev_device *hc_device;
  199. udev_context = udev_new();
  200. if (!udev_context) {
  201. err("udev_new failed");
  202. return -1;
  203. }
  204. /* will be freed in usbip_driver_close() */
  205. hc_device =
  206. udev_device_new_from_subsystem_sysname(udev_context,
  207. USBIP_VHCI_BUS_TYPE,
  208. USBIP_VHCI_DEVICE_NAME);
  209. if (!hc_device) {
  210. err("udev_device_new_from_subsystem_sysname failed");
  211. goto err;
  212. }
  213. nports = get_nports(hc_device);
  214. if (nports <= 0) {
  215. err("no available ports");
  216. goto err;
  217. }
  218. dbg("available ports: %d", nports);
  219. vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
  220. nports * sizeof(struct usbip_imported_device));
  221. if (!vhci_driver) {
  222. err("vhci_driver allocation failed");
  223. goto err;
  224. }
  225. vhci_driver->nports = nports;
  226. vhci_driver->hc_device = hc_device;
  227. vhci_driver->ncontrollers = get_ncontrollers();
  228. dbg("available controllers: %d", vhci_driver->ncontrollers);
  229. if (vhci_driver->ncontrollers <=0) {
  230. err("no available usb controllers");
  231. goto err;
  232. }
  233. if (refresh_imported_device_list())
  234. goto err;
  235. return 0;
  236. err:
  237. udev_device_unref(hc_device);
  238. if (vhci_driver)
  239. free(vhci_driver);
  240. vhci_driver = NULL;
  241. udev_unref(udev_context);
  242. return -1;
  243. }
  244. void usbip_vhci_driver_close(void)
  245. {
  246. if (!vhci_driver)
  247. return;
  248. udev_device_unref(vhci_driver->hc_device);
  249. free(vhci_driver);
  250. vhci_driver = NULL;
  251. udev_unref(udev_context);
  252. }
  253. int usbip_vhci_refresh_device_list(void)
  254. {
  255. if (refresh_imported_device_list())
  256. goto err;
  257. return 0;
  258. err:
  259. dbg("failed to refresh device list");
  260. return -1;
  261. }
  262. int usbip_vhci_get_free_port(uint32_t speed)
  263. {
  264. for (int i = 0; i < vhci_driver->nports; i++) {
  265. switch (speed) {
  266. case USB_SPEED_SUPER:
  267. if (vhci_driver->idev[i].hub != HUB_SPEED_SUPER)
  268. continue;
  269. break;
  270. default:
  271. if (vhci_driver->idev[i].hub != HUB_SPEED_HIGH)
  272. continue;
  273. break;
  274. }
  275. if (vhci_driver->idev[i].status == VDEV_ST_NULL)
  276. return vhci_driver->idev[i].port;
  277. }
  278. return -1;
  279. }
  280. int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
  281. uint32_t speed) {
  282. char buff[200]; /* what size should be ? */
  283. char attach_attr_path[SYSFS_PATH_MAX];
  284. char attr_attach[] = "attach";
  285. const char *path;
  286. int ret;
  287. snprintf(buff, sizeof(buff), "%u %d %u %u",
  288. port, sockfd, devid, speed);
  289. dbg("writing: %s", buff);
  290. path = udev_device_get_syspath(vhci_driver->hc_device);
  291. snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
  292. path, attr_attach);
  293. dbg("attach attribute path: %s", attach_attr_path);
  294. ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
  295. if (ret < 0) {
  296. dbg("write_sysfs_attribute failed");
  297. return -1;
  298. }
  299. dbg("attached port: %d", port);
  300. return 0;
  301. }
  302. static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
  303. {
  304. return (busnum << 16) | devnum;
  305. }
  306. /* will be removed */
  307. int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
  308. uint8_t devnum, uint32_t speed)
  309. {
  310. int devid = get_devid(busnum, devnum);
  311. return usbip_vhci_attach_device2(port, sockfd, devid, speed);
  312. }
  313. int usbip_vhci_detach_device(uint8_t port)
  314. {
  315. char detach_attr_path[SYSFS_PATH_MAX];
  316. char attr_detach[] = "detach";
  317. char buff[200]; /* what size should be ? */
  318. const char *path;
  319. int ret;
  320. snprintf(buff, sizeof(buff), "%u", port);
  321. dbg("writing: %s", buff);
  322. path = udev_device_get_syspath(vhci_driver->hc_device);
  323. snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
  324. path, attr_detach);
  325. dbg("detach attribute path: %s", detach_attr_path);
  326. ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
  327. if (ret < 0) {
  328. dbg("write_sysfs_attribute failed");
  329. return -1;
  330. }
  331. dbg("detached port: %d", port);
  332. return 0;
  333. }
  334. int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
  335. {
  336. char product_name[100];
  337. char host[NI_MAXHOST] = "unknown host";
  338. char serv[NI_MAXSERV] = "unknown port";
  339. char remote_busid[SYSFS_BUS_ID_SIZE];
  340. int ret;
  341. int read_record_error = 0;
  342. if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
  343. return 0;
  344. ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
  345. remote_busid);
  346. if (ret) {
  347. err("read_record");
  348. read_record_error = 1;
  349. }
  350. printf("Port %02d: <%s> at %s\n", idev->port,
  351. usbip_status_string(idev->status),
  352. usbip_speed_string(idev->udev.speed));
  353. usbip_names_get_product(product_name, sizeof(product_name),
  354. idev->udev.idVendor, idev->udev.idProduct);
  355. printf(" %s\n", product_name);
  356. if (!read_record_error) {
  357. printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
  358. host, serv, remote_busid);
  359. printf("%10s -> remote bus/dev %03d/%03d\n", " ",
  360. idev->busnum, idev->devnum);
  361. } else {
  362. printf("%10s -> unknown host, remote port and remote busid\n",
  363. idev->udev.busid);
  364. printf("%10s -> remote bus/dev %03d/%03d\n", " ",
  365. idev->busnum, idev->devnum);
  366. }
  367. return 0;
  368. }