• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005-2007 Takahiro Hirofuchi
3  */
4 
5 #ifndef __USBIP_COMMON_H
6 #define __USBIP_COMMON_H
7 
8 #include <sysfs/libsysfs.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 
18 #ifndef USBIDS_FILE
19 #define USBIDS_FILE "/usr/share/hwdata/usb.ids"
20 #endif
21 
22 #ifndef VHCI_STATE_PATH
23 #define VHCI_STATE_PATH "/var/run/vhci_hcd"
24 #endif
25 
26 /* kernel module names */
27 #define USBIP_CORE_MOD_NAME	"usbip-core"
28 #define USBIP_HOST_DRV_NAME	"usbip-host"
29 #define USBIP_VHCI_DRV_NAME	"vhci_hcd"
30 
31 extern int usbip_use_syslog;
32 extern int usbip_use_stderr;
33 extern int usbip_use_debug ;
34 
35 #define PROGNAME "usbip"
36 
37 #define pr_fmt(fmt)	"%s: %s: " fmt "\n", PROGNAME
38 #define dbg_fmt(fmt)	pr_fmt("%s:%d:[%s] " fmt), "debug",	\
39 		        __FILE__, __LINE__, __FUNCTION__
40 
41 #define err(fmt, args...)						\
42 	do {								\
43 		if (usbip_use_syslog) {					\
44 			syslog(LOG_ERR, pr_fmt(fmt), "error", ##args);	\
45 		}							\
46 		if (usbip_use_stderr) {					\
47 			fprintf(stderr, pr_fmt(fmt), "error", ##args);	\
48 		}							\
49 	} while (0)
50 
51 #define info(fmt, args...)						\
52 	do {								\
53 		if (usbip_use_syslog) {					\
54 			syslog(LOG_INFO, pr_fmt(fmt), "info", ##args);	\
55 		}							\
56 		if (usbip_use_stderr) {					\
57 			fprintf(stderr, pr_fmt(fmt), "info", ##args);	\
58 		}							\
59 	} while (0)
60 
61 #define dbg(fmt, args...)						\
62 	do {								\
63 	if (usbip_use_debug) {						\
64 		if (usbip_use_syslog) {					\
65 			syslog(LOG_DEBUG, dbg_fmt(fmt), ##args);	\
66 		}							\
67 		if (usbip_use_stderr) {					\
68 			fprintf(stderr, dbg_fmt(fmt), ##args);		\
69 		}							\
70 	}								\
71 	} while (0)
72 
73 #define BUG()						\
74 	do {						\
75 		err("sorry, it's a bug!");		\
76 		abort();				\
77 	} while (0)
78 
79 enum usb_device_speed {
80 	USB_SPEED_UNKNOWN = 0,                  /* enumerating */
81 	USB_SPEED_LOW, USB_SPEED_FULL,          /* usb 1.1 */
82 	USB_SPEED_HIGH,                         /* usb 2.0 */
83 	USB_SPEED_VARIABLE                      /* wireless (usb 2.5) */
84 };
85 
86 /* FIXME: how to sync with drivers/usbip_common.h ? */
87 enum usbip_device_status {
88 	/* sdev is available. */
89 	SDEV_ST_AVAILABLE = 0x01,
90 	/* sdev is now used. */
91 	SDEV_ST_USED,
92 	/* sdev is unusable because of a fatal error. */
93 	SDEV_ST_ERROR,
94 
95 	/* vdev does not connect a remote device. */
96 	VDEV_ST_NULL,
97 	/* vdev is used, but the USB address is not assigned yet */
98 	VDEV_ST_NOTASSIGNED,
99 	VDEV_ST_USED,
100 	VDEV_ST_ERROR
101 };
102 
103 struct usbip_usb_interface {
104 	uint8_t bInterfaceClass;
105 	uint8_t bInterfaceSubClass;
106 	uint8_t bInterfaceProtocol;
107 	uint8_t padding;	/* alignment */
108 } __attribute__((packed));
109 
110 struct usbip_usb_device {
111 	char path[SYSFS_PATH_MAX];
112 	char busid[SYSFS_BUS_ID_SIZE];
113 
114 	uint32_t busnum;
115 	uint32_t devnum;
116 	uint32_t speed;
117 
118 	uint16_t idVendor;
119 	uint16_t idProduct;
120 	uint16_t bcdDevice;
121 
122 	uint8_t bDeviceClass;
123 	uint8_t bDeviceSubClass;
124 	uint8_t bDeviceProtocol;
125 	uint8_t bConfigurationValue;
126 	uint8_t bNumConfigurations;
127 	uint8_t bNumInterfaces;
128 } __attribute__((packed));
129 
130 #define to_string(s)	#s
131 
132 void dump_usb_interface(struct usbip_usb_interface *);
133 void dump_usb_device(struct usbip_usb_device *);
134 int read_usb_device(struct sysfs_device *sdev, struct usbip_usb_device *udev);
135 int read_attr_value(struct sysfs_device *dev, const char *name,
136 		    const char *format);
137 int read_usb_interface(struct usbip_usb_device *udev, int i,
138 		       struct usbip_usb_interface *uinf);
139 
140 const char *usbip_speed_string(int num);
141 const char *usbip_status_string(int32_t status);
142 
143 int usbip_names_init(char *);
144 void usbip_names_free(void);
145 void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
146 			     uint16_t product);
147 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
148 			   uint8_t subclass, uint8_t protocol);
149 
150 #endif /* __USBIP_COMMON_H */
151