1
2
3
4
5
6
7
8#ifndef __USBIP_COMMON_H
9#define __USBIP_COMMON_H
10
11#include <linux/compiler.h>
12#include <linux/device.h>
13#include <linux/interrupt.h>
14#include <linux/net.h>
15#include <linux/printk.h>
16#include <linux/spinlock.h>
17#include <linux/types.h>
18#include <linux/usb.h>
19#include <linux/wait.h>
20#include <linux/sched/task.h>
21#include <linux/kcov.h>
22#include <uapi/linux/usbip.h>
23
24#undef pr_fmt
25
26#ifdef DEBUG
27#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
28#else
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30#endif
31
32enum {
33 usbip_debug_xmit = (1 << 0),
34 usbip_debug_sysfs = (1 << 1),
35 usbip_debug_urb = (1 << 2),
36 usbip_debug_eh = (1 << 3),
37
38 usbip_debug_stub_cmp = (1 << 8),
39 usbip_debug_stub_dev = (1 << 9),
40 usbip_debug_stub_rx = (1 << 10),
41 usbip_debug_stub_tx = (1 << 11),
42
43 usbip_debug_vhci_rh = (1 << 8),
44 usbip_debug_vhci_hc = (1 << 9),
45 usbip_debug_vhci_rx = (1 << 10),
46 usbip_debug_vhci_tx = (1 << 11),
47 usbip_debug_vhci_sysfs = (1 << 12)
48};
49
50#define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
51#define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
52#define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
53#define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
54#define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
55#define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
56#define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
57#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
58
59extern unsigned long usbip_debug_flag;
60extern struct device_attribute dev_attr_usbip_debug;
61
62#define usbip_dbg_with_flag(flag, fmt, args...) \
63 do { \
64 if (flag & usbip_debug_flag) \
65 pr_debug(fmt, ##args); \
66 } while (0)
67
68#define usbip_dbg_sysfs(fmt, args...) \
69 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
70#define usbip_dbg_xmit(fmt, args...) \
71 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
72#define usbip_dbg_urb(fmt, args...) \
73 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
74#define usbip_dbg_eh(fmt, args...) \
75 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
76
77#define usbip_dbg_vhci_rh(fmt, args...) \
78 usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
79#define usbip_dbg_vhci_hc(fmt, args...) \
80 usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
81#define usbip_dbg_vhci_rx(fmt, args...) \
82 usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
83#define usbip_dbg_vhci_tx(fmt, args...) \
84 usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
85#define usbip_dbg_vhci_sysfs(fmt, args...) \
86 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
87
88#define usbip_dbg_stub_cmp(fmt, args...) \
89 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
90#define usbip_dbg_stub_rx(fmt, args...) \
91 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
92#define usbip_dbg_stub_tx(fmt, args...) \
93 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117#define USBIP_CMD_SUBMIT 0x0001
118#define USBIP_CMD_UNLINK 0x0002
119#define USBIP_RET_SUBMIT 0x0003
120#define USBIP_RET_UNLINK 0x0004
121
122#define USBIP_DIR_OUT 0x00
123#define USBIP_DIR_IN 0x01
124
125
126
127
128
129
130#define USBIP_MAX_ISO_PACKETS 1024
131
132
133
134
135
136
137
138
139
140
141
142struct usbip_header_basic {
143 __u32 command;
144 __u32 seqnum;
145 __u32 devid;
146 __u32 direction;
147 __u32 ep;
148} __packed;
149
150
151
152
153
154
155
156
157
158
159struct usbip_header_cmd_submit {
160 __u32 transfer_flags;
161 __s32 transfer_buffer_length;
162
163
164 __s32 start_frame;
165 __s32 number_of_packets;
166 __s32 interval;
167
168 unsigned char setup[8];
169} __packed;
170
171
172
173
174
175
176
177
178
179struct usbip_header_ret_submit {
180 __s32 status;
181 __s32 actual_length;
182 __s32 start_frame;
183 __s32 number_of_packets;
184 __s32 error_count;
185} __packed;
186
187
188
189
190
191struct usbip_header_cmd_unlink {
192 __u32 seqnum;
193} __packed;
194
195
196
197
198
199struct usbip_header_ret_unlink {
200 __s32 status;
201} __packed;
202
203
204
205
206
207
208struct usbip_header {
209 struct usbip_header_basic base;
210
211 union {
212 struct usbip_header_cmd_submit cmd_submit;
213 struct usbip_header_ret_submit ret_submit;
214 struct usbip_header_cmd_unlink cmd_unlink;
215 struct usbip_header_ret_unlink ret_unlink;
216 } u;
217} __packed;
218
219
220
221
222struct usbip_iso_packet_descriptor {
223 __u32 offset;
224 __u32 length;
225 __u32 actual_length;
226 __u32 status;
227} __packed;
228
229enum usbip_side {
230 USBIP_VHCI,
231 USBIP_STUB,
232 USBIP_VUDC,
233};
234
235
236#define USBIP_EH_SHUTDOWN (1 << 0)
237#define USBIP_EH_BYE (1 << 1)
238#define USBIP_EH_RESET (1 << 2)
239#define USBIP_EH_UNUSABLE (1 << 3)
240
241#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
242#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
243#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
244#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
245#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
246
247#define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
248#define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
249#define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
250
251#define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
252#define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
253
254#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
255#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
256#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
257#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
258
259
260struct usbip_device {
261 enum usbip_side side;
262 enum usbip_device_status status;
263
264
265 spinlock_t lock;
266
267
268 struct mutex sysfs_lock;
269
270 int sockfd;
271 struct socket *tcp_socket;
272
273 struct task_struct *tcp_rx;
274 struct task_struct *tcp_tx;
275
276 unsigned long event;
277 wait_queue_head_t eh_waitq;
278
279 struct eh_ops {
280 void (*shutdown)(struct usbip_device *);
281 void (*reset)(struct usbip_device *);
282 void (*unusable)(struct usbip_device *);
283 } eh_ops;
284
285#ifdef CONFIG_KCOV
286 u64 kcov_handle;
287#endif
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#ifdef CONFIG_KCOV
349
350static inline void usbip_kcov_handle_init(struct usbip_device *ud)
351{
352 ud->kcov_handle = kcov_common_handle();
353}
354
355static inline void usbip_kcov_remote_start(struct usbip_device *ud)
356{
357 kcov_remote_start_common(ud->kcov_handle);
358}
359
360static inline void usbip_kcov_remote_stop(void)
361{
362 kcov_remote_stop();
363}
364
365#else
366
367static inline void usbip_kcov_handle_init(struct usbip_device *ud) { }
368static inline void usbip_kcov_remote_start(struct usbip_device *ud) { }
369static inline void usbip_kcov_remote_stop(void) { }
370
371#endif
372
373#endif
374