vhci_driver.c 8.3 KB

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