]>
Commit | Line | Data |
---|---|---|
05a1f28e TH |
1 | /* |
2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi | |
c7af4c22 IK |
3 | * Copyright (C) 2015-2016 Samsung Electronics |
4 | * Krzysztof Opasiak <k.opasiak@samsung.com> | |
05a1f28e TH |
5 | * |
6 | * This is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
19 | * USA. | |
20 | */ | |
21 | ||
7aaacb43 | 22 | #ifndef __USBIP_COMMON_H |
23 | #define __USBIP_COMMON_H | |
05a1f28e | 24 | |
4980d968 | 25 | #include <linux/compiler.h> |
7aaacb43 | 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> | |
05a1f28e | 32 | #include <linux/usb.h> |
7aaacb43 | 33 | #include <linux/wait.h> |
0881e7bd | 34 | #include <linux/sched/task.h> |
96c27377 | 35 | #include <uapi/linux/usbip.h> |
05a1f28e | 36 | |
6973c6f2 | 37 | #define USBIP_VERSION "1.0.0" |
38 | ||
1a4b6f66 | 39 | #undef pr_fmt |
05a1f28e | 40 | |
1a4b6f66 | 41 | #ifdef DEBUG |
42 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__ | |
981f30cc | 43 | #else |
1a4b6f66 | 44 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
45 | #endif | |
05a1f28e | 46 | |
05a1f28e TH |
47 | enum { |
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 | ||
b8868e45 BM |
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) | |
87352760 | 72 | #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs) |
05a1f28e TH |
73 | |
74 | extern unsigned long usbip_debug_flag; | |
75 | extern struct device_attribute dev_attr_usbip_debug; | |
76 | ||
b8868e45 | 77 | #define usbip_dbg_with_flag(flag, fmt, args...) \ |
05a1f28e TH |
78 | do { \ |
79 | if (flag & usbip_debug_flag) \ | |
1a4b6f66 | 80 | pr_debug(fmt, ##args); \ |
05a1f28e TH |
81 | } while (0) |
82 | ||
4980d968 | 83 | #define usbip_dbg_sysfs(fmt, args...) \ |
b8868e45 | 84 | usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args) |
4980d968 | 85 | #define usbip_dbg_xmit(fmt, args...) \ |
b8868e45 | 86 | usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args) |
4980d968 | 87 | #define usbip_dbg_urb(fmt, args...) \ |
b8868e45 | 88 | usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args) |
4980d968 | 89 | #define usbip_dbg_eh(fmt, args...) \ |
b8868e45 BM |
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) | |
4980d968 | 100 | #define usbip_dbg_vhci_sysfs(fmt, args...) \ |
b8868e45 BM |
101 | usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args) |
102 | ||
4980d968 | 103 | #define usbip_dbg_stub_cmp(fmt, args...) \ |
b8868e45 | 104 | usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args) |
4980d968 | 105 | #define usbip_dbg_stub_rx(fmt, args...) \ |
b8868e45 | 106 | usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args) |
4980d968 | 107 | #define usbip_dbg_stub_tx(fmt, args...) \ |
b8868e45 | 108 | usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args) |
05a1f28e | 109 | |
05a1f28e | 110 | /* |
b7d27ead | 111 | * USB/IP request headers |
05a1f28e | 112 | * |
b7d27ead | 113 | * Each request is transferred across the network to its counterpart, which |
114 | * facilitates the normal USB communication. The values contained in the headers | |
115 | * are basically the same as in a URB. Currently, four request types are | |
116 | * defined: | |
117 | * | |
118 | * - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb() | |
05a1f28e | 119 | * (client to server) |
b7d27ead | 120 | * |
121 | * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT | |
05a1f28e TH |
122 | * (server to client) |
123 | * | |
b7d27ead | 124 | * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT, |
125 | * corresponds to usb_unlink_urb() | |
126 | * (client to server) | |
05a1f28e | 127 | * |
b7d27ead | 128 | * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK |
129 | * (server to client) | |
05a1f28e TH |
130 | * |
131 | */ | |
b7d27ead | 132 | #define USBIP_CMD_SUBMIT 0x0001 |
7518b9b8 AT |
133 | #define USBIP_CMD_UNLINK 0x0002 |
134 | #define USBIP_RET_SUBMIT 0x0003 | |
b7d27ead | 135 | #define USBIP_RET_UNLINK 0x0004 |
05a1f28e | 136 | |
7518b9b8 AT |
137 | #define USBIP_DIR_OUT 0x00 |
138 | #define USBIP_DIR_IN 0x01 | |
b7d27ead | 139 | |
140 | /** | |
141 | * struct usbip_header_basic - data pertinent to every request | |
142 | * @command: the usbip request type | |
143 | * @seqnum: sequential number that identifies requests; incremented per | |
144 | * connection | |
145 | * @devid: specifies a remote USB device uniquely instead of busnum and devnum; | |
146 | * in the stub driver, this value is ((busnum << 16) | devnum) | |
147 | * @direction: direction of the transfer | |
148 | * @ep: endpoint number | |
05a1f28e TH |
149 | */ |
150 | struct usbip_header_basic { | |
05a1f28e | 151 | __u32 command; |
05a1f28e | 152 | __u32 seqnum; |
05a1f28e | 153 | __u32 devid; |
05a1f28e | 154 | __u32 direction; |
b7d27ead | 155 | __u32 ep; |
4980d968 | 156 | } __packed; |
05a1f28e | 157 | |
b7d27ead | 158 | /** |
159 | * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header | |
160 | * @transfer_flags: URB flags | |
161 | * @transfer_buffer_length: the data size for (in) or (out) transfer | |
162 | * @start_frame: initial frame for isochronous or interrupt transfers | |
163 | * @number_of_packets: number of isochronous packets | |
164 | * @interval: maximum time for the request on the server-side host controller | |
165 | * @setup: setup data for a control request | |
05a1f28e TH |
166 | */ |
167 | struct usbip_header_cmd_submit { | |
05a1f28e | 168 | __u32 transfer_flags; |
05a1f28e TH |
169 | __s32 transfer_buffer_length; |
170 | ||
171 | /* it is difficult for usbip to sync frames (reserved only?) */ | |
172 | __s32 start_frame; | |
05a1f28e | 173 | __s32 number_of_packets; |
05a1f28e TH |
174 | __s32 interval; |
175 | ||
05a1f28e | 176 | unsigned char setup[8]; |
4980d968 | 177 | } __packed; |
05a1f28e | 178 | |
b7d27ead | 179 | /** |
180 | * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header | |
181 | * @status: return status of a non-iso request | |
182 | * @actual_length: number of bytes transferred | |
183 | * @start_frame: initial frame for isochronous or interrupt transfers | |
184 | * @number_of_packets: number of isochronous packets | |
185 | * @error_count: number of errors for isochronous transfers | |
05a1f28e TH |
186 | */ |
187 | struct usbip_header_ret_submit { | |
188 | __s32 status; | |
b7d27ead | 189 | __s32 actual_length; |
190 | __s32 start_frame; | |
191 | __s32 number_of_packets; | |
192 | __s32 error_count; | |
4980d968 | 193 | } __packed; |
05a1f28e | 194 | |
b7d27ead | 195 | /** |
196 | * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header | |
197 | * @seqnum: the URB seqnum to unlink | |
05a1f28e TH |
198 | */ |
199 | struct usbip_header_cmd_unlink { | |
b7d27ead | 200 | __u32 seqnum; |
4980d968 | 201 | } __packed; |
05a1f28e | 202 | |
b7d27ead | 203 | /** |
204 | * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header | |
205 | * @status: return status of the request | |
05a1f28e TH |
206 | */ |
207 | struct usbip_header_ret_unlink { | |
208 | __s32 status; | |
4980d968 | 209 | } __packed; |
05a1f28e | 210 | |
b7d27ead | 211 | /** |
212 | * struct usbip_header - common header for all usbip packets | |
213 | * @base: the basic header | |
214 | * @u: packet type dependent header | |
05a1f28e TH |
215 | */ |
216 | struct 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; | |
4980d968 | 225 | } __packed; |
05a1f28e | 226 | |
b7d27ead | 227 | /* |
228 | * This is the same as usb_iso_packet_descriptor but packed for pdu. | |
229 | */ | |
230 | struct usbip_iso_packet_descriptor { | |
231 | __u32 offset; | |
232 | __u32 length; /* expected length */ | |
233 | __u32 actual_length; | |
234 | __u32 status; | |
235 | } __packed; | |
05a1f28e | 236 | |
05a1f28e TH |
237 | enum usbip_side { |
238 | USBIP_VHCI, | |
239 | USBIP_STUB, | |
c7af4c22 | 240 | USBIP_VUDC, |
05a1f28e TH |
241 | }; |
242 | ||
b7d27ead | 243 | /* event handler */ |
05a1f28e TH |
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 | ||
134a9265 | 249 | #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE) |
05a1f28e TH |
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 | ||
c7af4c22 IK |
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 | /* catastrophic emulated usb error */ | |
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 | ||
05a1f28e TH |
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 | ||
b7d27ead | 267 | /* a common structure for stub_device and vhci_device */ |
268 | struct usbip_device { | |
269 | enum usbip_side side; | |
cd84ec96 | 270 | enum usbip_device_status status; |
b7d27ead | 271 | |
272 | /* lock for status */ | |
273 | spinlock_t lock; | |
274 | ||
275 | struct socket *tcp_socket; | |
276 | ||
277 | struct task_struct *tcp_rx; | |
278 | struct task_struct *tcp_tx; | |
279 | ||
05a1f28e | 280 | unsigned long event; |
05a1f28e TH |
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 | ||
ba46ce30 ON |
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 | ||
b7d27ead | 307 | /* usbip_common.c */ |
308 | void usbip_dump_urb(struct urb *purb); | |
309 | void usbip_dump_header(struct usbip_header *pdu); | |
310 | ||
5a08c526 | 311 | int usbip_recv(struct socket *sock, void *buf, int size); |
b7d27ead | 312 | |
05a1f28e | 313 | void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, |
4980d968 | 314 | int pack); |
05a1f28e | 315 | void usbip_header_correct_endian(struct usbip_header *pdu, int send); |
b7d27ead | 316 | |
36ac9b05 BW |
317 | struct usbip_iso_packet_descriptor* |
318 | usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen); | |
319 | ||
05a1f28e TH |
320 | /* some members of urb must be substituted before. */ |
321 | int usbip_recv_iso(struct usbip_device *ud, struct urb *urb); | |
ac2b41ac | 322 | void usbip_pad_iso(struct usbip_device *ud, struct urb *urb); |
b7d27ead | 323 | int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb); |
05a1f28e | 324 | |
05a1f28e | 325 | /* usbip_event.c */ |
bb7871ad NI |
326 | int usbip_init_eh(void); |
327 | void usbip_finish_eh(void); | |
b8868e45 | 328 | int usbip_start_eh(struct usbip_device *ud); |
05a1f28e TH |
329 | void usbip_stop_eh(struct usbip_device *ud); |
330 | void usbip_event_add(struct usbip_device *ud, unsigned long event); | |
b8868e45 | 331 | int usbip_event_happened(struct usbip_device *ud); |
bb7871ad | 332 | int usbip_in_eh(struct task_struct *task); |
05a1f28e | 333 | |
b7d27ead | 334 | static inline int interface_to_busnum(struct usb_interface *interface) |
335 | { | |
336 | struct usb_device *udev = interface_to_usbdev(interface); | |
3eed8c03 | 337 | |
b7d27ead | 338 | return udev->bus->busnum; |
339 | } | |
340 | ||
341 | static inline int interface_to_devnum(struct usb_interface *interface) | |
342 | { | |
343 | struct usb_device *udev = interface_to_usbdev(interface); | |
3eed8c03 | 344 | |
b7d27ead | 345 | return udev->devnum; |
346 | } | |
347 | ||
7aaacb43 | 348 | #endif /* __USBIP_COMMON_H */ |