1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
8 #ifndef __USBIP_COMMON_H
9 #define __USBIP_COMMON_H
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>
27 #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 usbip_debug_xmit = (1 << 0),
34 usbip_debug_sysfs = (1 << 1),
35 usbip_debug_urb = (1 << 2),
36 usbip_debug_eh = (1 << 3),
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),
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)
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)
59 extern unsigned long usbip_debug_flag;
60 extern struct device_attribute dev_attr_usbip_debug;
62 #define usbip_dbg_with_flag(flag, fmt, args...) \
64 if (flag & usbip_debug_flag) \
65 pr_debug(fmt, ##args); \
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)
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)
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)
96 * USB/IP request headers
98 * Each request is transferred across the network to its counterpart, which
99 * facilitates the normal USB communication. The values contained in the headers
100 * are basically the same as in a URB. Currently, four request types are
103 * - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
106 * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
109 * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
110 * corresponds to usb_unlink_urb()
113 * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
117 #define USBIP_CMD_SUBMIT 0x0001
118 #define USBIP_CMD_UNLINK 0x0002
119 #define USBIP_RET_SUBMIT 0x0003
120 #define USBIP_RET_UNLINK 0x0004
122 #define USBIP_DIR_OUT 0x00
123 #define USBIP_DIR_IN 0x01
126 * Arbitrary limit for the maximum number of isochronous packets in an URB,
127 * compare for example the uhci_submit_isochronous function in
128 * drivers/usb/host/uhci-q.c
130 #define USBIP_MAX_ISO_PACKETS 1024
133 * struct usbip_header_basic - data pertinent to every request
134 * @command: the usbip request type
135 * @seqnum: sequential number that identifies requests; incremented per
137 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
138 * in the stub driver, this value is ((busnum << 16) | devnum)
139 * @direction: direction of the transfer
140 * @ep: endpoint number
142 struct usbip_header_basic {
151 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
152 * @transfer_flags: URB flags
153 * @transfer_buffer_length: the data size for (in) or (out) transfer
154 * @start_frame: initial frame for isochronous or interrupt transfers
155 * @number_of_packets: number of isochronous packets
156 * @interval: maximum time for the request on the server-side host controller
157 * @setup: setup data for a control request
159 struct usbip_header_cmd_submit {
160 __u32 transfer_flags;
161 __s32 transfer_buffer_length;
163 /* it is difficult for usbip to sync frames (reserved only?) */
165 __s32 number_of_packets;
168 unsigned char setup[8];
172 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
173 * @status: return status of a non-iso request
174 * @actual_length: number of bytes transferred
175 * @start_frame: initial frame for isochronous or interrupt transfers
176 * @number_of_packets: number of isochronous packets
177 * @error_count: number of errors for isochronous transfers
179 struct usbip_header_ret_submit {
183 __s32 number_of_packets;
188 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
189 * @seqnum: the URB seqnum to unlink
191 struct usbip_header_cmd_unlink {
196 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
197 * @status: return status of the request
199 struct usbip_header_ret_unlink {
204 * struct usbip_header - common header for all usbip packets
205 * @base: the basic header
206 * @u: packet type dependent header
208 struct usbip_header {
209 struct usbip_header_basic base;
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;
220 * This is the same as usb_iso_packet_descriptor but packed for pdu.
222 struct usbip_iso_packet_descriptor {
224 __u32 length; /* expected length */
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)
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)
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 /* catastrophic emulated usb error */
251 #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
252 #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
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)
259 /* a common structure for stub_device and vhci_device */
260 struct usbip_device {
261 enum usbip_side side;
262 enum usbip_device_status status;
264 /* lock for status */
267 /* mutex for synchronizing sysfs store paths */
268 struct mutex sysfs_lock;
271 struct socket *tcp_socket;
273 struct task_struct *tcp_rx;
274 struct task_struct *tcp_tx;
277 wait_queue_head_t eh_waitq;
280 void (*shutdown)(struct usbip_device *);
281 void (*reset)(struct usbip_device *);
282 void (*unusable)(struct usbip_device *);
286 #define kthread_get_run(threadfn, data, namefmt, ...) \
288 struct task_struct *__k \
289 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
290 if (!IS_ERR(__k)) { \
291 get_task_struct(__k); \
292 wake_up_process(__k); \
297 #define kthread_stop_put(k) \
300 put_task_struct(k); \
304 void usbip_dump_urb(struct urb *purb);
305 void usbip_dump_header(struct usbip_header *pdu);
307 int usbip_recv(struct socket *sock, void *buf, int size);
309 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
311 void usbip_header_correct_endian(struct usbip_header *pdu, int send);
313 struct usbip_iso_packet_descriptor*
314 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
316 /* some members of urb must be substituted before. */
317 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
318 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
319 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
322 int usbip_init_eh(void);
323 void usbip_finish_eh(void);
324 int usbip_start_eh(struct usbip_device *ud);
325 void usbip_stop_eh(struct usbip_device *ud);
326 void usbip_event_add(struct usbip_device *ud, unsigned long event);
327 int usbip_event_happened(struct usbip_device *ud);
328 int usbip_in_eh(struct task_struct *task);
330 static inline int interface_to_busnum(struct usb_interface *interface)
332 struct usb_device *udev = interface_to_usbdev(interface);
334 return udev->bus->busnum;
337 static inline int interface_to_devnum(struct usb_interface *interface)
339 struct usb_device *udev = interface_to_usbdev(interface);
344 #endif /* __USBIP_COMMON_H */