usbip_common.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005-2007 Takahiro Hirofuchi
  4. */
  5. #include <libudev.h>
  6. #include "usbip_common.h"
  7. #include "names.h"
  8. #undef PROGNAME
  9. #define PROGNAME "libusbip"
  10. int usbip_use_syslog;
  11. int usbip_use_stderr;
  12. int usbip_use_debug;
  13. extern struct udev *udev_context;
  14. struct speed_string {
  15. int num;
  16. char *speed;
  17. char *desc;
  18. };
  19. static const struct speed_string speed_strings[] = {
  20. { USB_SPEED_UNKNOWN, "unknown", "Unknown Speed"},
  21. { USB_SPEED_LOW, "1.5", "Low Speed(1.5Mbps)" },
  22. { USB_SPEED_FULL, "12", "Full Speed(12Mbps)" },
  23. { USB_SPEED_HIGH, "480", "High Speed(480Mbps)" },
  24. { USB_SPEED_WIRELESS, "53.3-480", "Wireless"},
  25. { USB_SPEED_SUPER, "5000", "Super Speed(5000Mbps)" },
  26. { 0, NULL, NULL }
  27. };
  28. struct portst_string {
  29. int num;
  30. char *desc;
  31. };
  32. static struct portst_string portst_strings[] = {
  33. { SDEV_ST_AVAILABLE, "Device Available" },
  34. { SDEV_ST_USED, "Device in Use" },
  35. { SDEV_ST_ERROR, "Device Error"},
  36. { VDEV_ST_NULL, "Port Available"},
  37. { VDEV_ST_NOTASSIGNED, "Port Initializing"},
  38. { VDEV_ST_USED, "Port in Use"},
  39. { VDEV_ST_ERROR, "Port Error"},
  40. { 0, NULL}
  41. };
  42. const char *usbip_status_string(int32_t status)
  43. {
  44. for (int i = 0; portst_strings[i].desc != NULL; i++)
  45. if (portst_strings[i].num == status)
  46. return portst_strings[i].desc;
  47. return "Unknown Status";
  48. }
  49. const char *usbip_speed_string(int num)
  50. {
  51. for (int i = 0; speed_strings[i].speed != NULL; i++)
  52. if (speed_strings[i].num == num)
  53. return speed_strings[i].desc;
  54. return "Unknown Speed";
  55. }
  56. struct op_common_status_string {
  57. int num;
  58. char *desc;
  59. };
  60. static struct op_common_status_string op_common_status_strings[] = {
  61. { ST_OK, "Request Completed Successfully" },
  62. { ST_NA, "Request Failed" },
  63. { ST_DEV_BUSY, "Device busy (exported)" },
  64. { ST_DEV_ERR, "Device in error state" },
  65. { ST_NODEV, "Device not found" },
  66. { ST_ERROR, "Unexpected response" },
  67. { 0, NULL}
  68. };
  69. const char *usbip_op_common_status_string(int status)
  70. {
  71. for (int i = 0; op_common_status_strings[i].desc != NULL; i++)
  72. if (op_common_status_strings[i].num == status)
  73. return op_common_status_strings[i].desc;
  74. return "Unknown Op Common Status";
  75. }
  76. #define DBG_UDEV_INTEGER(name)\
  77. dbg("%-20s = %x", to_string(name), (int) udev->name)
  78. #define DBG_UINF_INTEGER(name)\
  79. dbg("%-20s = %x", to_string(name), (int) uinf->name)
  80. void dump_usb_interface(struct usbip_usb_interface *uinf)
  81. {
  82. char buff[100];
  83. usbip_names_get_class(buff, sizeof(buff),
  84. uinf->bInterfaceClass,
  85. uinf->bInterfaceSubClass,
  86. uinf->bInterfaceProtocol);
  87. dbg("%-20s = %s", "Interface(C/SC/P)", buff);
  88. }
  89. void dump_usb_device(struct usbip_usb_device *udev)
  90. {
  91. char buff[100];
  92. dbg("%-20s = %s", "path", udev->path);
  93. dbg("%-20s = %s", "busid", udev->busid);
  94. usbip_names_get_class(buff, sizeof(buff),
  95. udev->bDeviceClass,
  96. udev->bDeviceSubClass,
  97. udev->bDeviceProtocol);
  98. dbg("%-20s = %s", "Device(C/SC/P)", buff);
  99. DBG_UDEV_INTEGER(bcdDevice);
  100. usbip_names_get_product(buff, sizeof(buff),
  101. udev->idVendor,
  102. udev->idProduct);
  103. dbg("%-20s = %s", "Vendor/Product", buff);
  104. DBG_UDEV_INTEGER(bNumConfigurations);
  105. DBG_UDEV_INTEGER(bNumInterfaces);
  106. dbg("%-20s = %s", "speed",
  107. usbip_speed_string(udev->speed));
  108. DBG_UDEV_INTEGER(busnum);
  109. DBG_UDEV_INTEGER(devnum);
  110. }
  111. int read_attr_value(struct udev_device *dev, const char *name,
  112. const char *format)
  113. {
  114. const char *attr;
  115. int num = 0;
  116. int ret;
  117. attr = udev_device_get_sysattr_value(dev, name);
  118. if (!attr) {
  119. err("udev_device_get_sysattr_value failed");
  120. goto err;
  121. }
  122. /* The client chooses the device configuration
  123. * when attaching it so right after being bound
  124. * to usbip-host on the server the device will
  125. * have no configuration.
  126. * Therefore, attributes such as bConfigurationValue
  127. * and bNumInterfaces will not exist and sscanf will
  128. * fail. Check for these cases and don't treat them
  129. * as errors.
  130. */
  131. ret = sscanf(attr, format, &num);
  132. if (ret < 1) {
  133. if (strcmp(name, "bConfigurationValue") &&
  134. strcmp(name, "bNumInterfaces")) {
  135. err("sscanf failed for attribute %s", name);
  136. goto err;
  137. }
  138. }
  139. err:
  140. return num;
  141. }
  142. int read_attr_speed(struct udev_device *dev)
  143. {
  144. const char *speed;
  145. speed = udev_device_get_sysattr_value(dev, "speed");
  146. if (!speed) {
  147. err("udev_device_get_sysattr_value failed");
  148. goto err;
  149. }
  150. for (int i = 0; speed_strings[i].speed != NULL; i++) {
  151. if (!strcmp(speed, speed_strings[i].speed))
  152. return speed_strings[i].num;
  153. }
  154. err:
  155. return USB_SPEED_UNKNOWN;
  156. }
  157. #define READ_ATTR(object, type, dev, name, format) \
  158. do { \
  159. (object)->name = (type) read_attr_value(dev, to_string(name), \
  160. format); \
  161. } while (0)
  162. int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev)
  163. {
  164. uint32_t busnum, devnum;
  165. const char *path, *name;
  166. READ_ATTR(udev, uint8_t, sdev, bDeviceClass, "%02x\n");
  167. READ_ATTR(udev, uint8_t, sdev, bDeviceSubClass, "%02x\n");
  168. READ_ATTR(udev, uint8_t, sdev, bDeviceProtocol, "%02x\n");
  169. READ_ATTR(udev, uint16_t, sdev, idVendor, "%04x\n");
  170. READ_ATTR(udev, uint16_t, sdev, idProduct, "%04x\n");
  171. READ_ATTR(udev, uint16_t, sdev, bcdDevice, "%04x\n");
  172. READ_ATTR(udev, uint8_t, sdev, bConfigurationValue, "%02x\n");
  173. READ_ATTR(udev, uint8_t, sdev, bNumConfigurations, "%02x\n");
  174. READ_ATTR(udev, uint8_t, sdev, bNumInterfaces, "%02x\n");
  175. READ_ATTR(udev, uint8_t, sdev, devnum, "%d\n");
  176. udev->speed = read_attr_speed(sdev);
  177. path = udev_device_get_syspath(sdev);
  178. name = udev_device_get_sysname(sdev);
  179. strncpy(udev->path, path, SYSFS_PATH_MAX - 1);
  180. udev->path[SYSFS_PATH_MAX - 1] = '\0';
  181. strncpy(udev->busid, name, SYSFS_BUS_ID_SIZE - 1);
  182. udev->busid[SYSFS_BUS_ID_SIZE - 1] = '\0';
  183. sscanf(name, "%u-%u", &busnum, &devnum);
  184. udev->busnum = busnum;
  185. return 0;
  186. }
  187. int read_usb_interface(struct usbip_usb_device *udev, int i,
  188. struct usbip_usb_interface *uinf)
  189. {
  190. char busid[SYSFS_BUS_ID_SIZE];
  191. int size;
  192. struct udev_device *sif;
  193. size = snprintf(busid, sizeof(busid), "%s:%d.%d",
  194. udev->busid, udev->bConfigurationValue, i);
  195. if (size < 0 || (unsigned int)size >= sizeof(busid)) {
  196. err("busid length %i >= %lu or < 0", size,
  197. (long unsigned)sizeof(busid));
  198. return -1;
  199. }
  200. sif = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid);
  201. if (!sif) {
  202. err("udev_device_new_from_subsystem_sysname %s failed", busid);
  203. return -1;
  204. }
  205. READ_ATTR(uinf, uint8_t, sif, bInterfaceClass, "%02x\n");
  206. READ_ATTR(uinf, uint8_t, sif, bInterfaceSubClass, "%02x\n");
  207. READ_ATTR(uinf, uint8_t, sif, bInterfaceProtocol, "%02x\n");
  208. return 0;
  209. }
  210. int usbip_names_init(char *f)
  211. {
  212. return names_init(f);
  213. }
  214. void usbip_names_free(void)
  215. {
  216. names_free();
  217. }
  218. void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
  219. uint16_t product)
  220. {
  221. const char *prod, *vend;
  222. prod = names_product(vendor, product);
  223. if (!prod)
  224. prod = "unknown product";
  225. vend = names_vendor(vendor);
  226. if (!vend)
  227. vend = "unknown vendor";
  228. snprintf(buff, size, "%s : %s (%04x:%04x)", vend, prod, vendor, product);
  229. }
  230. void usbip_names_get_class(char *buff, size_t size, uint8_t class,
  231. uint8_t subclass, uint8_t protocol)
  232. {
  233. const char *c, *s, *p;
  234. if (class == 0 && subclass == 0 && protocol == 0) {
  235. snprintf(buff, size, "(Defined at Interface level) (%02x/%02x/%02x)", class, subclass, protocol);
  236. return;
  237. }
  238. p = names_protocol(class, subclass, protocol);
  239. if (!p)
  240. p = "unknown protocol";
  241. s = names_subclass(class, subclass);
  242. if (!s)
  243. s = "unknown subclass";
  244. c = names_class(class);
  245. if (!c)
  246. c = "unknown class";
  247. snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, subclass, protocol);
  248. }