Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
05a1f28e TH |
2 | /* |
3 | * Copyright (C) 2003-2008 Takahiro Hirofuchi | |
c7af4c22 IK |
4 | * Copyright (C) 2015-2016 Samsung Electronics |
5 | * Krzysztof Opasiak <k.opasiak@samsung.com> | |
05a1f28e TH |
6 | */ |
7 | ||
7aaacb43 | 8 | #ifndef __USBIP_COMMON_H |
9 | #define __USBIP_COMMON_H | |
05a1f28e | 10 | |
4980d968 | 11 | #include <linux/compiler.h> |
7aaacb43 | 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> | |
05a1f28e | 18 | #include <linux/usb.h> |
7aaacb43 | 19 | #include <linux/wait.h> |
0881e7bd | 20 | #include <linux/sched/task.h> |
96c27377 | 21 | #include <uapi/linux/usbip.h> |
05a1f28e | 22 | |
1a4b6f66 | 23 | #undef pr_fmt |
05a1f28e | 24 | |
1a4b6f66 | 25 | #ifdef DEBUG |
26 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__ | |
981f30cc | 27 | #else |
1a4b6f66 | 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
29 | #endif | |
05a1f28e | 30 | |
05a1f28e TH |
31 | enum { |
32 | usbip_debug_xmit = (1 << 0), | |
33 | usbip_debug_sysfs = (1 << 1), | |
34 | usbip_debug_urb = (1 << 2), | |
35 | usbip_debug_eh = (1 << 3), | |
36 | ||
37 | usbip_debug_stub_cmp = (1 << 8), | |
38 | usbip_debug_stub_dev = (1 << 9), | |
39 | usbip_debug_stub_rx = (1 << 10), | |
40 | usbip_debug_stub_tx = (1 << 11), | |
41 | ||
42 | usbip_debug_vhci_rh = (1 << 8), | |
43 | usbip_debug_vhci_hc = (1 << 9), | |
44 | usbip_debug_vhci_rx = (1 << 10), | |
45 | usbip_debug_vhci_tx = (1 << 11), | |
46 | usbip_debug_vhci_sysfs = (1 << 12) | |
47 | }; | |
48 | ||
b8868e45 BM |
49 | #define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit) |
50 | #define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh) | |
51 | #define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc) | |
52 | #define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx) | |
53 | #define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx) | |
54 | #define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx) | |
55 | #define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx) | |
87352760 | 56 | #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs) |
05a1f28e TH |
57 | |
58 | extern unsigned long usbip_debug_flag; | |
59 | extern struct device_attribute dev_attr_usbip_debug; | |
60 | ||
b8868e45 | 61 | #define usbip_dbg_with_flag(flag, fmt, args...) \ |
05a1f28e TH |
62 | do { \ |
63 | if (flag & usbip_debug_flag) \ | |
1a4b6f66 | 64 | pr_debug(fmt, ##args); \ |
05a1f28e TH |
65 | } while (0) |
66 | ||
4980d968 | 67 | #define usbip_dbg_sysfs(fmt, args...) \ |
b8868e45 | 68 | usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args) |
4980d968 | 69 | #define usbip_dbg_xmit(fmt, args...) \ |
b8868e45 | 70 | usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args) |
4980d968 | 71 | #define usbip_dbg_urb(fmt, args...) \ |
b8868e45 | 72 | usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args) |
4980d968 | 73 | #define usbip_dbg_eh(fmt, args...) \ |
b8868e45 BM |
74 | usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args) |
75 | ||
76 | #define usbip_dbg_vhci_rh(fmt, args...) \ | |
77 | usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args) | |
78 | #define usbip_dbg_vhci_hc(fmt, args...) \ | |
79 | usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args) | |
80 | #define usbip_dbg_vhci_rx(fmt, args...) \ | |
81 | usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args) | |
82 | #define usbip_dbg_vhci_tx(fmt, args...) \ | |
83 | usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args) | |
4980d968 | 84 | #define usbip_dbg_vhci_sysfs(fmt, args...) \ |
b8868e45 BM |
85 | usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args) |
86 | ||
4980d968 | 87 | #define usbip_dbg_stub_cmp(fmt, args...) \ |
b8868e45 | 88 | usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args) |
4980d968 | 89 | #define usbip_dbg_stub_rx(fmt, args...) \ |
b8868e45 | 90 | usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args) |
4980d968 | 91 | #define usbip_dbg_stub_tx(fmt, args...) \ |
b8868e45 | 92 | usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args) |
05a1f28e | 93 | |
05a1f28e | 94 | /* |
b7d27ead | 95 | * USB/IP request headers |
05a1f28e | 96 | * |
b7d27ead | 97 | * Each request is transferred across the network to its counterpart, which |
98 | * facilitates the normal USB communication. The values contained in the headers | |
99 | * are basically the same as in a URB. Currently, four request types are | |
100 | * defined: | |
101 | * | |
102 | * - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb() | |
05a1f28e | 103 | * (client to server) |
b7d27ead | 104 | * |
105 | * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT | |
05a1f28e TH |
106 | * (server to client) |
107 | * | |
b7d27ead | 108 | * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT, |
109 | * corresponds to usb_unlink_urb() | |
110 | * (client to server) | |
05a1f28e | 111 | * |
b7d27ead | 112 | * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK |
113 | * (server to client) | |
05a1f28e TH |
114 | * |
115 | */ | |
b7d27ead | 116 | #define USBIP_CMD_SUBMIT 0x0001 |
7518b9b8 AT |
117 | #define USBIP_CMD_UNLINK 0x0002 |
118 | #define USBIP_RET_SUBMIT 0x0003 | |
b7d27ead | 119 | #define USBIP_RET_UNLINK 0x0004 |
05a1f28e | 120 | |
7518b9b8 AT |
121 | #define USBIP_DIR_OUT 0x00 |
122 | #define USBIP_DIR_IN 0x01 | |
b7d27ead | 123 | |
124 | /** | |
125 | * struct usbip_header_basic - data pertinent to every request | |
126 | * @command: the usbip request type | |
127 | * @seqnum: sequential number that identifies requests; incremented per | |
128 | * connection | |
129 | * @devid: specifies a remote USB device uniquely instead of busnum and devnum; | |
130 | * in the stub driver, this value is ((busnum << 16) | devnum) | |
131 | * @direction: direction of the transfer | |
132 | * @ep: endpoint number | |
05a1f28e TH |
133 | */ |
134 | struct usbip_header_basic { | |
05a1f28e | 135 | __u32 command; |
05a1f28e | 136 | __u32 seqnum; |
05a1f28e | 137 | __u32 devid; |
05a1f28e | 138 | __u32 direction; |
b7d27ead | 139 | __u32 ep; |
4980d968 | 140 | } __packed; |
05a1f28e | 141 | |
b7d27ead | 142 | /** |
143 | * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header | |
144 | * @transfer_flags: URB flags | |
145 | * @transfer_buffer_length: the data size for (in) or (out) transfer | |
146 | * @start_frame: initial frame for isochronous or interrupt transfers | |
147 | * @number_of_packets: number of isochronous packets | |
148 | * @interval: maximum time for the request on the server-side host controller | |
149 | * @setup: setup data for a control request | |
05a1f28e TH |
150 | */ |
151 | struct usbip_header_cmd_submit { | |
05a1f28e | 152 | __u32 transfer_flags; |
05a1f28e TH |
153 | __s32 transfer_buffer_length; |
154 | ||
155 | /* it is difficult for usbip to sync frames (reserved only?) */ | |
156 | __s32 start_frame; | |
05a1f28e | 157 | __s32 number_of_packets; |
05a1f28e TH |
158 | __s32 interval; |
159 | ||
05a1f28e | 160 | unsigned char setup[8]; |
4980d968 | 161 | } __packed; |
05a1f28e | 162 | |
b7d27ead | 163 | /** |
164 | * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header | |
165 | * @status: return status of a non-iso request | |
166 | * @actual_length: number of bytes transferred | |
167 | * @start_frame: initial frame for isochronous or interrupt transfers | |
168 | * @number_of_packets: number of isochronous packets | |
169 | * @error_count: number of errors for isochronous transfers | |
05a1f28e TH |
170 | */ |
171 | struct usbip_header_ret_submit { | |
172 | __s32 status; | |
b7d27ead | 173 | __s32 actual_length; |
174 | __s32 start_frame; | |
175 | __s32 number_of_packets; | |
176 | __s32 error_count; | |
4980d968 | 177 | } __packed; |
05a1f28e | 178 | |
b7d27ead | 179 | /** |
180 | * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header | |
181 | * @seqnum: the URB seqnum to unlink | |
05a1f28e TH |
182 | */ |
183 | struct usbip_header_cmd_unlink { | |
b7d27ead | 184 | __u32 seqnum; |
4980d968 | 185 | } __packed; |
05a1f28e | 186 | |
b7d27ead | 187 | /** |
188 | * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header | |
189 | * @status: return status of the request | |
05a1f28e TH |
190 | */ |
191 | struct usbip_header_ret_unlink { | |
192 | __s32 status; | |
4980d968 | 193 | } __packed; |
05a1f28e | 194 | |
b7d27ead | 195 | /** |
196 | * struct usbip_header - common header for all usbip packets | |
197 | * @base: the basic header | |
198 | * @u: packet type dependent header | |
05a1f28e TH |
199 | */ |
200 | struct usbip_header { | |
201 | struct usbip_header_basic base; | |
202 | ||
203 | union { | |
204 | struct usbip_header_cmd_submit cmd_submit; | |
205 | struct usbip_header_ret_submit ret_submit; | |
206 | struct usbip_header_cmd_unlink cmd_unlink; | |
207 | struct usbip_header_ret_unlink ret_unlink; | |
208 | } u; | |
4980d968 | 209 | } __packed; |
05a1f28e | 210 | |
b7d27ead | 211 | /* |
212 | * This is the same as usb_iso_packet_descriptor but packed for pdu. | |
213 | */ | |
214 | struct usbip_iso_packet_descriptor { | |
215 | __u32 offset; | |
216 | __u32 length; /* expected length */ | |
217 | __u32 actual_length; | |
218 | __u32 status; | |
219 | } __packed; | |
05a1f28e | 220 | |
05a1f28e TH |
221 | enum usbip_side { |
222 | USBIP_VHCI, | |
223 | USBIP_STUB, | |
c7af4c22 | 224 | USBIP_VUDC, |
05a1f28e TH |
225 | }; |
226 | ||
b7d27ead | 227 | /* event handler */ |
05a1f28e TH |
228 | #define USBIP_EH_SHUTDOWN (1 << 0) |
229 | #define USBIP_EH_BYE (1 << 1) | |
230 | #define USBIP_EH_RESET (1 << 2) | |
231 | #define USBIP_EH_UNUSABLE (1 << 3) | |
232 | ||
134a9265 | 233 | #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE) |
05a1f28e TH |
234 | #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
235 | #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
236 | #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
237 | #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
238 | ||
c7af4c22 IK |
239 | #define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE) |
240 | #define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
241 | #define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
242 | /* catastrophic emulated usb error */ | |
243 | #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
244 | #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
245 | ||
9020a7ef | 246 | #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE) |
05a1f28e TH |
247 | #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
248 | #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
249 | #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
250 | ||
b7d27ead | 251 | /* a common structure for stub_device and vhci_device */ |
252 | struct usbip_device { | |
253 | enum usbip_side side; | |
cd84ec96 | 254 | enum usbip_device_status status; |
b7d27ead | 255 | |
256 | /* lock for status */ | |
257 | spinlock_t lock; | |
258 | ||
2f2d0088 | 259 | int sockfd; |
b7d27ead | 260 | struct socket *tcp_socket; |
261 | ||
262 | struct task_struct *tcp_rx; | |
263 | struct task_struct *tcp_tx; | |
264 | ||
05a1f28e | 265 | unsigned long event; |
05a1f28e TH |
266 | wait_queue_head_t eh_waitq; |
267 | ||
268 | struct eh_ops { | |
269 | void (*shutdown)(struct usbip_device *); | |
270 | void (*reset)(struct usbip_device *); | |
271 | void (*unusable)(struct usbip_device *); | |
272 | } eh_ops; | |
273 | }; | |
274 | ||
ba46ce30 ON |
275 | #define kthread_get_run(threadfn, data, namefmt, ...) \ |
276 | ({ \ | |
277 | struct task_struct *__k \ | |
278 | = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \ | |
279 | if (!IS_ERR(__k)) { \ | |
280 | get_task_struct(__k); \ | |
281 | wake_up_process(__k); \ | |
282 | } \ | |
283 | __k; \ | |
284 | }) | |
285 | ||
286 | #define kthread_stop_put(k) \ | |
287 | do { \ | |
288 | kthread_stop(k); \ | |
289 | put_task_struct(k); \ | |
290 | } while (0) | |
291 | ||
b7d27ead | 292 | /* usbip_common.c */ |
293 | void usbip_dump_urb(struct urb *purb); | |
294 | void usbip_dump_header(struct usbip_header *pdu); | |
295 | ||
5a08c526 | 296 | int usbip_recv(struct socket *sock, void *buf, int size); |
b7d27ead | 297 | |
05a1f28e | 298 | void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, |
4980d968 | 299 | int pack); |
05a1f28e | 300 | void usbip_header_correct_endian(struct usbip_header *pdu, int send); |
b7d27ead | 301 | |
36ac9b05 BW |
302 | struct usbip_iso_packet_descriptor* |
303 | usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen); | |
304 | ||
05a1f28e TH |
305 | /* some members of urb must be substituted before. */ |
306 | int usbip_recv_iso(struct usbip_device *ud, struct urb *urb); | |
ac2b41ac | 307 | void usbip_pad_iso(struct usbip_device *ud, struct urb *urb); |
b7d27ead | 308 | int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb); |
05a1f28e | 309 | |
05a1f28e | 310 | /* usbip_event.c */ |
bb7871ad NI |
311 | int usbip_init_eh(void); |
312 | void usbip_finish_eh(void); | |
b8868e45 | 313 | int usbip_start_eh(struct usbip_device *ud); |
05a1f28e TH |
314 | void usbip_stop_eh(struct usbip_device *ud); |
315 | void usbip_event_add(struct usbip_device *ud, unsigned long event); | |
b8868e45 | 316 | int usbip_event_happened(struct usbip_device *ud); |
bb7871ad | 317 | int usbip_in_eh(struct task_struct *task); |
05a1f28e | 318 | |
b7d27ead | 319 | static inline int interface_to_busnum(struct usb_interface *interface) |
320 | { | |
321 | struct usb_device *udev = interface_to_usbdev(interface); | |
3eed8c03 | 322 | |
b7d27ead | 323 | return udev->bus->busnum; |
324 | } | |
325 | ||
326 | static inline int interface_to_devnum(struct usb_interface *interface) | |
327 | { | |
328 | struct usb_device *udev = interface_to_usbdev(interface); | |
3eed8c03 | 329 | |
b7d27ead | 330 | return udev->devnum; |
331 | } | |
332 | ||
7aaacb43 | 333 | #endif /* __USBIP_COMMON_H */ |