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