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