GNU Linux-libre 4.9.337-gnu1
[releases.git] / tools / usb / usbip / src / usbip_network.h
1 /*
2  * Copyright (C) 2005-2007 Takahiro Hirofuchi
3  */
4
5 #ifndef __USBIP_NETWORK_H
6 #define __USBIP_NETWORK_H
7
8 #ifdef HAVE_CONFIG_H
9 #include "../config.h"
10 #endif
11
12 #include <sys/types.h>
13
14 #include <stdint.h>
15
16 extern int usbip_port;
17 extern char *usbip_port_string;
18 void usbip_setup_port_number(char *arg);
19
20 /* ---------------------------------------------------------------------- */
21 /* Common header for all the kinds of PDUs. */
22 struct op_common {
23         uint16_t version;
24
25 #define OP_REQUEST      (0x80 << 8)
26 #define OP_REPLY        (0x00 << 8)
27         uint16_t code;
28
29         /* add more error code */
30 #define ST_OK   0x00
31 #define ST_NA   0x01
32         uint32_t status; /* op_code status (for reply) */
33
34 } __attribute__((packed));
35
36 /* ---------------------------------------------------------------------- */
37 /* Dummy Code */
38 #define OP_UNSPEC       0x00
39 #define OP_REQ_UNSPEC   OP_UNSPEC
40 #define OP_REP_UNSPEC   OP_UNSPEC
41
42 /* ---------------------------------------------------------------------- */
43 /* Retrieve USB device information. (still not used) */
44 #define OP_DEVINFO      0x02
45 #define OP_REQ_DEVINFO  (OP_REQUEST | OP_DEVINFO)
46 #define OP_REP_DEVINFO  (OP_REPLY   | OP_DEVINFO)
47
48 struct op_devinfo_request {
49         char busid[SYSFS_BUS_ID_SIZE];
50 } __attribute__((packed));
51
52 struct op_devinfo_reply {
53         struct usbip_usb_device udev;
54         struct usbip_usb_interface uinf[];
55 } __attribute__((packed));
56
57 /* ---------------------------------------------------------------------- */
58 /* Import a remote USB device. */
59 #define OP_IMPORT       0x03
60 #define OP_REQ_IMPORT   (OP_REQUEST | OP_IMPORT)
61 #define OP_REP_IMPORT   (OP_REPLY   | OP_IMPORT)
62
63 struct op_import_request {
64         char busid[SYSFS_BUS_ID_SIZE];
65 } __attribute__((packed));
66
67 struct op_import_reply {
68         struct usbip_usb_device udev;
69 //      struct usbip_usb_interface uinf[];
70 } __attribute__((packed));
71
72 #define PACK_OP_IMPORT_REQUEST(pack, request)  do {\
73 } while (0)
74
75 #define PACK_OP_IMPORT_REPLY(pack, reply)  do {\
76         usbip_net_pack_usb_device(pack, &(reply)->udev);\
77 } while (0)
78
79 /* ---------------------------------------------------------------------- */
80 /* Export a USB device to a remote host. */
81 #define OP_EXPORT       0x06
82 #define OP_REQ_EXPORT   (OP_REQUEST | OP_EXPORT)
83 #define OP_REP_EXPORT   (OP_REPLY   | OP_EXPORT)
84
85 struct op_export_request {
86         struct usbip_usb_device udev;
87 } __attribute__((packed));
88
89 struct op_export_reply {
90         int returncode;
91 } __attribute__((packed));
92
93
94 #define PACK_OP_EXPORT_REQUEST(pack, request)  do {\
95         usbip_net_pack_usb_device(pack, &(request)->udev);\
96 } while (0)
97
98 #define PACK_OP_EXPORT_REPLY(pack, reply)  do {\
99 } while (0)
100
101 /* ---------------------------------------------------------------------- */
102 /* un-Export a USB device from a remote host. */
103 #define OP_UNEXPORT     0x07
104 #define OP_REQ_UNEXPORT (OP_REQUEST | OP_UNEXPORT)
105 #define OP_REP_UNEXPORT (OP_REPLY   | OP_UNEXPORT)
106
107 struct op_unexport_request {
108         struct usbip_usb_device udev;
109 } __attribute__((packed));
110
111 struct op_unexport_reply {
112         int returncode;
113 } __attribute__((packed));
114
115 #define PACK_OP_UNEXPORT_REQUEST(pack, request)  do {\
116         usbip_net_pack_usb_device(pack, &(request)->udev);\
117 } while (0)
118
119 #define PACK_OP_UNEXPORT_REPLY(pack, reply)  do {\
120 } while (0)
121
122 /* ---------------------------------------------------------------------- */
123 /* Negotiate IPSec encryption key. (still not used) */
124 #define OP_CRYPKEY      0x04
125 #define OP_REQ_CRYPKEY  (OP_REQUEST | OP_CRYPKEY)
126 #define OP_REP_CRYPKEY  (OP_REPLY   | OP_CRYPKEY)
127
128 struct op_crypkey_request {
129         /* 128bit key */
130         uint32_t key[4];
131 } __attribute__((packed));
132
133 struct op_crypkey_reply {
134         uint32_t __reserved;
135 } __attribute__((packed));
136
137
138 /* ---------------------------------------------------------------------- */
139 /* Retrieve the list of exported USB devices. */
140 #define OP_DEVLIST      0x05
141 #define OP_REQ_DEVLIST  (OP_REQUEST | OP_DEVLIST)
142 #define OP_REP_DEVLIST  (OP_REPLY   | OP_DEVLIST)
143
144 struct op_devlist_request {
145 } __attribute__((packed));
146
147 struct op_devlist_reply {
148         uint32_t ndev;
149         /* followed by reply_extra[] */
150 } __attribute__((packed));
151
152 struct op_devlist_reply_extra {
153         struct usbip_usb_device    udev;
154         struct usbip_usb_interface uinf[];
155 } __attribute__((packed));
156
157 #define PACK_OP_DEVLIST_REQUEST(pack, request)  do {\
158 } while (0)
159
160 #define PACK_OP_DEVLIST_REPLY(pack, reply)  do {\
161         (reply)->ndev = usbip_net_pack_uint32_t(pack, (reply)->ndev);\
162 } while (0)
163
164 uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num);
165 uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num);
166 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev);
167 void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf);
168
169 ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen);
170 ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen);
171 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status);
172 int usbip_net_recv_op_common(int sockfd, uint16_t *code);
173 int usbip_net_set_reuseaddr(int sockfd);
174 int usbip_net_set_nodelay(int sockfd);
175 int usbip_net_set_keepalive(int sockfd);
176 int usbip_net_set_v6only(int sockfd);
177 int usbip_net_tcp_connect(char *hostname, char *port);
178
179 #endif /* __USBIP_NETWORK_H */