1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef __USBIP_COMMON_H
21#define __USBIP_COMMON_H
22
23#include <linux/compiler.h>
24#include <linux/device.h>
25#include <linux/interrupt.h>
26#include <linux/net.h>
27#include <linux/printk.h>
28#include <linux/spinlock.h>
29#include <linux/types.h>
30#include <linux/usb.h>
31#include <linux/wait.h>
32
33#define USBIP_VERSION "1.0.0"
34
35#undef pr_fmt
36
37#ifdef DEBUG
38#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
39#else
40#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41#endif
42
43enum {
44 usbip_debug_xmit = (1 << 0),
45 usbip_debug_sysfs = (1 << 1),
46 usbip_debug_urb = (1 << 2),
47 usbip_debug_eh = (1 << 3),
48
49 usbip_debug_stub_cmp = (1 << 8),
50 usbip_debug_stub_dev = (1 << 9),
51 usbip_debug_stub_rx = (1 << 10),
52 usbip_debug_stub_tx = (1 << 11),
53
54 usbip_debug_vhci_rh = (1 << 8),
55 usbip_debug_vhci_hc = (1 << 9),
56 usbip_debug_vhci_rx = (1 << 10),
57 usbip_debug_vhci_tx = (1 << 11),
58 usbip_debug_vhci_sysfs = (1 << 12)
59};
60
61#define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
62#define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
63#define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
64#define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
65#define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
66#define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
67#define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
68#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
69
70extern unsigned long usbip_debug_flag;
71extern struct device_attribute dev_attr_usbip_debug;
72
73#define usbip_dbg_with_flag(flag, fmt, args...) \
74 do { \
75 if (flag & usbip_debug_flag) \
76 pr_debug(fmt, ##args); \
77 } while (0)
78
79#define usbip_dbg_sysfs(fmt, args...) \
80 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
81#define usbip_dbg_xmit(fmt, args...) \
82 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
83#define usbip_dbg_urb(fmt, args...) \
84 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
85#define usbip_dbg_eh(fmt, args...) \
86 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
87
88#define usbip_dbg_vhci_rh(fmt, args...) \
89 usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
90#define usbip_dbg_vhci_hc(fmt, args...) \
91 usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
92#define usbip_dbg_vhci_rx(fmt, args...) \
93 usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
94#define usbip_dbg_vhci_tx(fmt, args...) \
95 usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
96#define usbip_dbg_vhci_sysfs(fmt, args...) \
97 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
98
99#define usbip_dbg_stub_cmp(fmt, args...) \
100 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
101#define usbip_dbg_stub_rx(fmt, args...) \
102 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
103#define usbip_dbg_stub_tx(fmt, args...) \
104 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128#define USBIP_CMD_SUBMIT 0x0001
129#define USBIP_CMD_UNLINK 0x0002
130#define USBIP_RET_SUBMIT 0x0003
131#define USBIP_RET_UNLINK 0x0004
132
133#define USBIP_DIR_OUT 0x00
134#define USBIP_DIR_IN 0x01
135
136
137
138
139
140
141
142
143
144
145
146struct usbip_header_basic {
147 __u32 command;
148 __u32 seqnum;
149 __u32 devid;
150 __u32 direction;
151 __u32 ep;
152} __packed;
153
154
155
156
157
158
159
160
161
162
163struct usbip_header_cmd_submit {
164 __u32 transfer_flags;
165 __s32 transfer_buffer_length;
166
167
168 __s32 start_frame;
169 __s32 number_of_packets;
170 __s32 interval;
171
172 unsigned char setup[8];
173} __packed;
174
175
176
177
178
179
180
181
182
183struct usbip_header_ret_submit {
184 __s32 status;
185 __s32 actual_length;
186 __s32 start_frame;
187 __s32 number_of_packets;
188 __s32 error_count;
189} __packed;
190
191
192
193
194
195struct usbip_header_cmd_unlink {
196 __u32 seqnum;
197} __packed;
198
199
200
201
202
203struct usbip_header_ret_unlink {
204 __s32 status;
205} __packed;
206
207
208
209
210
211
212struct usbip_header {
213 struct usbip_header_basic base;
214
215 union {
216 struct usbip_header_cmd_submit cmd_submit;
217 struct usbip_header_ret_submit ret_submit;
218 struct usbip_header_cmd_unlink cmd_unlink;
219 struct usbip_header_ret_unlink ret_unlink;
220 } u;
221} __packed;
222
223
224
225
226struct usbip_iso_packet_descriptor {
227 __u32 offset;
228 __u32 length;
229 __u32 actual_length;
230 __u32 status;
231} __packed;
232
233enum usbip_side {
234 USBIP_VHCI,
235 USBIP_STUB,
236};
237
238enum usbip_status {
239
240 SDEV_ST_AVAILABLE = 0x01,
241
242 SDEV_ST_USED,
243
244 SDEV_ST_ERROR,
245
246
247 VDEV_ST_NULL,
248
249 VDEV_ST_NOTASSIGNED,
250 VDEV_ST_USED,
251 VDEV_ST_ERROR
252};
253
254
255#define USBIP_EH_SHUTDOWN (1 << 0)
256#define USBIP_EH_BYE (1 << 1)
257#define USBIP_EH_RESET (1 << 2)
258#define USBIP_EH_UNUSABLE (1 << 3)
259
260#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
261#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
262#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
263#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
264#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
265
266#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
267#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
268#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
269#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
270
271
272struct usbip_device {
273 enum usbip_side side;
274 enum usbip_status status;
275
276
277 spinlock_t lock;
278
279 struct socket *tcp_socket;
280
281 struct task_struct *tcp_rx;
282 struct task_struct *tcp_tx;
283
284 unsigned long event;
285 struct task_struct *eh;
286 wait_queue_head_t eh_waitq;
287
288 struct eh_ops {
289 void (*shutdown)(struct usbip_device *);
290 void (*reset)(struct usbip_device *);
291 void (*unusable)(struct usbip_device *);
292 } eh_ops;
293};
294
295#define kthread_get_run(threadfn, data, namefmt, ...) \
296({ \
297 struct task_struct *__k \
298 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
299 if (!IS_ERR(__k)) { \
300 get_task_struct(__k); \
301 wake_up_process(__k); \
302 } \
303 __k; \
304})
305
306#define kthread_stop_put(k) \
307 do { \
308 kthread_stop(k); \
309 put_task_struct(k); \
310 } while (0)
311
312
313void usbip_dump_urb(struct urb *purb);
314void usbip_dump_header(struct usbip_header *pdu);
315
316int usbip_recv(struct socket *sock, void *buf, int size);
317struct socket *sockfd_to_socket(unsigned int sockfd);
318
319void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
320 int pack);
321void usbip_header_correct_endian(struct usbip_header *pdu, int send);
322
323struct usbip_iso_packet_descriptor*
324usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
325
326
327int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
328void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
329int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
330
331
332int usbip_start_eh(struct usbip_device *ud);
333void usbip_stop_eh(struct usbip_device *ud);
334void usbip_event_add(struct usbip_device *ud, unsigned long event);
335int usbip_event_happened(struct usbip_device *ud);
336
337static inline int interface_to_busnum(struct usb_interface *interface)
338{
339 struct usb_device *udev = interface_to_usbdev(interface);
340 return udev->bus->busnum;
341}
342
343static inline int interface_to_devnum(struct usb_interface *interface)
344{
345 struct usb_device *udev = interface_to_usbdev(interface);
346 return udev->devnum;
347}
348
349#endif
350