xref: /linux/tools/usb/usbip/libsrc/usbip_common.h (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * Copyright (C) 2005-2007 Takahiro Hirofuchi
3  */
4 
5 #ifndef __USBIP_COMMON_H
6 #define __USBIP_COMMON_H
7 
8 #include <libudev.h>
9 
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <syslog.h>
16 #include <unistd.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usbip.h>
19 
20 #ifndef USBIDS_FILE
21 #define USBIDS_FILE "/usr/share/hwdata/usb.ids"
22 #endif
23 
24 #ifndef VHCI_STATE_PATH
25 #define VHCI_STATE_PATH "/var/run/vhci_hcd"
26 #endif
27 
28 /* kernel module names */
29 #define USBIP_CORE_MOD_NAME	"usbip-core"
30 #define USBIP_HOST_DRV_NAME	"usbip-host"
31 #define USBIP_VHCI_DRV_NAME	"vhci_hcd"
32 
33 /* sysfs constants */
34 #define SYSFS_MNT_PATH         "/sys"
35 #define SYSFS_BUS_NAME         "bus"
36 #define SYSFS_BUS_TYPE         "usb"
37 #define SYSFS_DRIVERS_NAME     "drivers"
38 
39 #define SYSFS_PATH_MAX		256
40 #define SYSFS_BUS_ID_SIZE	32
41 
42 extern int usbip_use_syslog;
43 extern int usbip_use_stderr;
44 extern int usbip_use_debug ;
45 
46 #define PROGNAME "usbip"
47 
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 
52 #define err(fmt, args...)						\
53 	do {								\
54 		if (usbip_use_syslog) {					\
55 			syslog(LOG_ERR, pr_fmt(fmt), "error", ##args);	\
56 		}							\
57 		if (usbip_use_stderr) {					\
58 			fprintf(stderr, pr_fmt(fmt), "error", ##args);	\
59 		}							\
60 	} while (0)
61 
62 #define info(fmt, args...)						\
63 	do {								\
64 		if (usbip_use_syslog) {					\
65 			syslog(LOG_INFO, pr_fmt(fmt), "info", ##args);	\
66 		}							\
67 		if (usbip_use_stderr) {					\
68 			fprintf(stderr, pr_fmt(fmt), "info", ##args);	\
69 		}							\
70 	} while (0)
71 
72 #define dbg(fmt, args...)						\
73 	do {								\
74 	if (usbip_use_debug) {						\
75 		if (usbip_use_syslog) {					\
76 			syslog(LOG_DEBUG, dbg_fmt(fmt), ##args);	\
77 		}							\
78 		if (usbip_use_stderr) {					\
79 			fprintf(stderr, dbg_fmt(fmt), ##args);		\
80 		}							\
81 	}								\
82 	} while (0)
83 
84 #define BUG()						\
85 	do {						\
86 		err("sorry, it's a bug!");		\
87 		abort();				\
88 	} while (0)
89 
90 struct usbip_usb_interface {
91 	uint8_t bInterfaceClass;
92 	uint8_t bInterfaceSubClass;
93 	uint8_t bInterfaceProtocol;
94 	uint8_t padding;	/* alignment */
95 } __attribute__((packed));
96 
97 struct usbip_usb_device {
98 	char path[SYSFS_PATH_MAX];
99 	char busid[SYSFS_BUS_ID_SIZE];
100 
101 	uint32_t busnum;
102 	uint32_t devnum;
103 	uint32_t speed;
104 
105 	uint16_t idVendor;
106 	uint16_t idProduct;
107 	uint16_t bcdDevice;
108 
109 	uint8_t bDeviceClass;
110 	uint8_t bDeviceSubClass;
111 	uint8_t bDeviceProtocol;
112 	uint8_t bConfigurationValue;
113 	uint8_t bNumConfigurations;
114 	uint8_t bNumInterfaces;
115 } __attribute__((packed));
116 
117 #define to_string(s)	#s
118 
119 void dump_usb_interface(struct usbip_usb_interface *);
120 void dump_usb_device(struct usbip_usb_device *);
121 int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev);
122 int read_attr_value(struct udev_device *dev, const char *name,
123 		    const char *format);
124 int read_usb_interface(struct usbip_usb_device *udev, int i,
125 		       struct usbip_usb_interface *uinf);
126 
127 const char *usbip_speed_string(int num);
128 const char *usbip_status_string(int32_t status);
129 
130 int usbip_names_init(char *);
131 void usbip_names_free(void);
132 void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
133 			     uint16_t product);
134 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
135 			   uint8_t subclass, uint8_t protocol);
136 
137 #endif /* __USBIP_COMMON_H */
138