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