usbip_common.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2005-2007 Takahiro Hirofuchi
  4. */
  5. #ifndef __USBIP_COMMON_H
  6. #define __USBIP_COMMON_H
  7. #include <libudev.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <syslog.h>
  13. #include <unistd.h>
  14. #include <linux/usb/ch9.h>
  15. #include <linux/usbip.h>
  16. #ifndef USBIDS_FILE
  17. #define USBIDS_FILE "/usr/share/hwdata/usb.ids"
  18. #endif
  19. #ifndef VHCI_STATE_PATH
  20. #define VHCI_STATE_PATH "/var/run/vhci_hcd"
  21. #endif
  22. #define VUDC_DEVICE_DESCR_FILE "dev_desc"
  23. /* kernel module names */
  24. #define USBIP_CORE_MOD_NAME "usbip-core"
  25. #define USBIP_HOST_DRV_NAME "usbip-host"
  26. #define USBIP_DEVICE_DRV_NAME "usbip-vudc"
  27. #define USBIP_VHCI_DRV_NAME "vhci_hcd"
  28. /* sysfs constants */
  29. #define SYSFS_MNT_PATH "/sys"
  30. #define SYSFS_BUS_NAME "bus"
  31. #define SYSFS_BUS_TYPE "usb"
  32. #define SYSFS_DRIVERS_NAME "drivers"
  33. #define SYSFS_PATH_MAX 256
  34. #define SYSFS_BUS_ID_SIZE 32
  35. /* Defines for op_code status in server/client op_common PDUs */
  36. #define ST_OK 0x00
  37. #define ST_NA 0x01
  38. /* Device requested for import is not available */
  39. #define ST_DEV_BUSY 0x02
  40. /* Device requested for import is in error state */
  41. #define ST_DEV_ERR 0x03
  42. #define ST_NODEV 0x04
  43. #define ST_ERROR 0x05
  44. extern int usbip_use_syslog;
  45. extern int usbip_use_stderr;
  46. extern int usbip_use_debug ;
  47. #define PROGNAME "usbip"
  48. #define pr_fmt(fmt) "%s: %s: " fmt "\n", PROGNAME
  49. #define dbg_fmt(fmt) pr_fmt("%s:%d:[%s] " fmt), "debug", \
  50. __FILE__, __LINE__, __func__
  51. #define err(fmt, args...) \
  52. do { \
  53. if (usbip_use_syslog) { \
  54. syslog(LOG_ERR, pr_fmt(fmt), "error", ##args); \
  55. } \
  56. if (usbip_use_stderr) { \
  57. fprintf(stderr, pr_fmt(fmt), "error", ##args); \
  58. } \
  59. } while (0)
  60. #define info(fmt, args...) \
  61. do { \
  62. if (usbip_use_syslog) { \
  63. syslog(LOG_INFO, pr_fmt(fmt), "info", ##args); \
  64. } \
  65. if (usbip_use_stderr) { \
  66. fprintf(stderr, pr_fmt(fmt), "info", ##args); \
  67. } \
  68. } while (0)
  69. #define dbg(fmt, args...) \
  70. do { \
  71. if (usbip_use_debug) { \
  72. if (usbip_use_syslog) { \
  73. syslog(LOG_DEBUG, dbg_fmt(fmt), ##args); \
  74. } \
  75. if (usbip_use_stderr) { \
  76. fprintf(stderr, dbg_fmt(fmt), ##args); \
  77. } \
  78. } \
  79. } while (0)
  80. #define BUG() \
  81. do { \
  82. err("sorry, it's a bug!"); \
  83. abort(); \
  84. } while (0)
  85. struct usbip_usb_interface {
  86. uint8_t bInterfaceClass;
  87. uint8_t bInterfaceSubClass;
  88. uint8_t bInterfaceProtocol;
  89. uint8_t padding; /* alignment */
  90. } __attribute__((packed));
  91. struct usbip_usb_device {
  92. char path[SYSFS_PATH_MAX];
  93. char busid[SYSFS_BUS_ID_SIZE];
  94. uint32_t busnum;
  95. uint32_t devnum;
  96. uint32_t speed;
  97. uint16_t idVendor;
  98. uint16_t idProduct;
  99. uint16_t bcdDevice;
  100. uint8_t bDeviceClass;
  101. uint8_t bDeviceSubClass;
  102. uint8_t bDeviceProtocol;
  103. uint8_t bConfigurationValue;
  104. uint8_t bNumConfigurations;
  105. uint8_t bNumInterfaces;
  106. } __attribute__((packed));
  107. #define to_string(s) #s
  108. void dump_usb_interface(struct usbip_usb_interface *);
  109. void dump_usb_device(struct usbip_usb_device *);
  110. int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev);
  111. int read_attr_value(struct udev_device *dev, const char *name,
  112. const char *format);
  113. int read_usb_interface(struct usbip_usb_device *udev, int i,
  114. struct usbip_usb_interface *uinf);
  115. const char *usbip_speed_string(int num);
  116. const char *usbip_status_string(int32_t status);
  117. const char *usbip_op_common_status_string(int status);
  118. int usbip_names_init(char *);
  119. void usbip_names_free(void);
  120. void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
  121. uint16_t product);
  122. void usbip_names_get_class(char *buff, size_t size, uint8_t class,
  123. uint8_t subclass, uint8_t protocol);
  124. #endif /* __USBIP_COMMON_H */