Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2003-2008 Takahiro Hirofuchi |
Igor Kotrasinski | c7af4c2 | 2016-03-08 21:48:57 +0100 | [diff] [blame] | 4 | * Copyright (C) 2015-2016 Samsung Electronics |
| 5 | * Krzysztof Opasiak <k.opasiak@samsung.com> |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 8 | #ifndef __USBIP_COMMON_H |
| 9 | #define __USBIP_COMMON_H |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 10 | |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 11 | #include <linux/compiler.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 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> |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 18 | #include <linux/usb.h> |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 19 | #include <linux/wait.h> |
Ingo Molnar | 0881e7b | 2017-02-05 15:30:50 +0100 | [diff] [blame] | 20 | #include <linux/sched/task.h> |
Valentina Manea | 96c2737 | 2014-08-20 07:31:00 +0300 | [diff] [blame] | 21 | #include <uapi/linux/usbip.h> |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 22 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 23 | #undef pr_fmt |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 24 | |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 25 | #ifdef DEBUG |
| 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__ |
matt mooney | 981f30c | 2011-05-11 01:54:13 -0700 | [diff] [blame] | 27 | #else |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 29 | #endif |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 30 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 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 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 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) |
matt mooney | 8735276 | 2011-05-19 21:36:56 -0700 | [diff] [blame] | 56 | #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 57 | |
| 58 | extern unsigned long usbip_debug_flag; |
| 59 | extern struct device_attribute dev_attr_usbip_debug; |
| 60 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 61 | #define usbip_dbg_with_flag(flag, fmt, args...) \ |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 62 | do { \ |
| 63 | if (flag & usbip_debug_flag) \ |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 64 | pr_debug(fmt, ##args); \ |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 65 | } while (0) |
| 66 | |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 67 | #define usbip_dbg_sysfs(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 68 | usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 69 | #define usbip_dbg_xmit(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 70 | usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 71 | #define usbip_dbg_urb(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 72 | usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 73 | #define usbip_dbg_eh(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 74 | usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 75 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 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) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 84 | #define usbip_dbg_vhci_sysfs(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 85 | usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 86 | |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 87 | #define usbip_dbg_stub_cmp(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 88 | usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 89 | #define usbip_dbg_stub_rx(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 90 | usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args) |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 91 | #define usbip_dbg_stub_tx(fmt, args...) \ |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 92 | usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 93 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 94 | /* |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 95 | * USB/IP request headers |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 96 | * |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 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() |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 103 | * (client to server) |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 104 | * |
| 105 | * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 106 | * (server to client) |
| 107 | * |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 108 | * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT, |
| 109 | * corresponds to usb_unlink_urb() |
| 110 | * (client to server) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 111 | * |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 112 | * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK |
| 113 | * (server to client) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 114 | * |
| 115 | */ |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 116 | #define USBIP_CMD_SUBMIT 0x0001 |
Alexander Thomas | 7518b9b | 2011-09-19 16:56:51 +0200 | [diff] [blame] | 117 | #define USBIP_CMD_UNLINK 0x0002 |
| 118 | #define USBIP_RET_SUBMIT 0x0003 |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 119 | #define USBIP_RET_UNLINK 0x0004 |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 120 | |
Alexander Thomas | 7518b9b | 2011-09-19 16:56:51 +0200 | [diff] [blame] | 121 | #define USBIP_DIR_OUT 0x00 |
| 122 | #define USBIP_DIR_IN 0x01 |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 123 | |
Malte Leip | c409ca3 | 2019-04-14 12:00:12 +0200 | [diff] [blame] | 124 | /* |
| 125 | * Arbitrary limit for the maximum number of isochronous packets in an URB, |
| 126 | * compare for example the uhci_submit_isochronous function in |
| 127 | * drivers/usb/host/uhci-q.c |
| 128 | */ |
| 129 | #define USBIP_MAX_ISO_PACKETS 1024 |
| 130 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 131 | /** |
| 132 | * struct usbip_header_basic - data pertinent to every request |
| 133 | * @command: the usbip request type |
| 134 | * @seqnum: sequential number that identifies requests; incremented per |
| 135 | * connection |
| 136 | * @devid: specifies a remote USB device uniquely instead of busnum and devnum; |
| 137 | * in the stub driver, this value is ((busnum << 16) | devnum) |
| 138 | * @direction: direction of the transfer |
| 139 | * @ep: endpoint number |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 140 | */ |
| 141 | struct usbip_header_basic { |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 142 | __u32 command; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 143 | __u32 seqnum; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 144 | __u32 devid; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 145 | __u32 direction; |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 146 | __u32 ep; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 147 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 148 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 149 | /** |
| 150 | * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header |
| 151 | * @transfer_flags: URB flags |
| 152 | * @transfer_buffer_length: the data size for (in) or (out) transfer |
| 153 | * @start_frame: initial frame for isochronous or interrupt transfers |
| 154 | * @number_of_packets: number of isochronous packets |
| 155 | * @interval: maximum time for the request on the server-side host controller |
| 156 | * @setup: setup data for a control request |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 157 | */ |
| 158 | struct usbip_header_cmd_submit { |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 159 | __u32 transfer_flags; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 160 | __s32 transfer_buffer_length; |
| 161 | |
| 162 | /* it is difficult for usbip to sync frames (reserved only?) */ |
| 163 | __s32 start_frame; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 164 | __s32 number_of_packets; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 165 | __s32 interval; |
| 166 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 167 | unsigned char setup[8]; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 168 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 169 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 170 | /** |
| 171 | * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header |
| 172 | * @status: return status of a non-iso request |
| 173 | * @actual_length: number of bytes transferred |
| 174 | * @start_frame: initial frame for isochronous or interrupt transfers |
| 175 | * @number_of_packets: number of isochronous packets |
| 176 | * @error_count: number of errors for isochronous transfers |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 177 | */ |
| 178 | struct usbip_header_ret_submit { |
| 179 | __s32 status; |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 180 | __s32 actual_length; |
| 181 | __s32 start_frame; |
| 182 | __s32 number_of_packets; |
| 183 | __s32 error_count; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 184 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 185 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 186 | /** |
| 187 | * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header |
| 188 | * @seqnum: the URB seqnum to unlink |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 189 | */ |
| 190 | struct usbip_header_cmd_unlink { |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 191 | __u32 seqnum; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 192 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 193 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 194 | /** |
| 195 | * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header |
| 196 | * @status: return status of the request |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 197 | */ |
| 198 | struct usbip_header_ret_unlink { |
| 199 | __s32 status; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 200 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 201 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 202 | /** |
| 203 | * struct usbip_header - common header for all usbip packets |
| 204 | * @base: the basic header |
| 205 | * @u: packet type dependent header |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 206 | */ |
| 207 | struct usbip_header { |
| 208 | struct usbip_header_basic base; |
| 209 | |
| 210 | union { |
| 211 | struct usbip_header_cmd_submit cmd_submit; |
| 212 | struct usbip_header_ret_submit ret_submit; |
| 213 | struct usbip_header_cmd_unlink cmd_unlink; |
| 214 | struct usbip_header_ret_unlink ret_unlink; |
| 215 | } u; |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 216 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 217 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 218 | /* |
| 219 | * This is the same as usb_iso_packet_descriptor but packed for pdu. |
| 220 | */ |
| 221 | struct usbip_iso_packet_descriptor { |
| 222 | __u32 offset; |
| 223 | __u32 length; /* expected length */ |
| 224 | __u32 actual_length; |
| 225 | __u32 status; |
| 226 | } __packed; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 227 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 228 | enum usbip_side { |
| 229 | USBIP_VHCI, |
| 230 | USBIP_STUB, |
Igor Kotrasinski | c7af4c2 | 2016-03-08 21:48:57 +0100 | [diff] [blame] | 231 | USBIP_VUDC, |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 232 | }; |
| 233 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 234 | /* event handler */ |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 235 | #define USBIP_EH_SHUTDOWN (1 << 0) |
| 236 | #define USBIP_EH_BYE (1 << 1) |
| 237 | #define USBIP_EH_RESET (1 << 2) |
| 238 | #define USBIP_EH_UNUSABLE (1 << 3) |
| 239 | |
Alexander Popov | 134a926 | 2016-05-20 12:37:28 +0300 | [diff] [blame] | 240 | #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 241 | #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 242 | #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 243 | #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 244 | #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) |
| 245 | |
Igor Kotrasinski | c7af4c2 | 2016-03-08 21:48:57 +0100 | [diff] [blame] | 246 | #define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE) |
| 247 | #define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 248 | #define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 249 | /* catastrophic emulated usb error */ |
| 250 | #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) |
| 251 | #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) |
| 252 | |
Shuah Khan | 9020a7e | 2018-04-02 14:52:32 -0600 | [diff] [blame] | 253 | #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE) |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 254 | #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 255 | #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
| 256 | #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) |
| 257 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 258 | /* a common structure for stub_device and vhci_device */ |
| 259 | struct usbip_device { |
| 260 | enum usbip_side side; |
Shuah Khan | cd84ec9 | 2014-03-03 16:38:44 -0700 | [diff] [blame] | 261 | enum usbip_device_status status; |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 262 | |
| 263 | /* lock for status */ |
| 264 | spinlock_t lock; |
| 265 | |
Shuah Khan | 2f2d008 | 2017-12-07 14:16:49 -0700 | [diff] [blame] | 266 | int sockfd; |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 267 | struct socket *tcp_socket; |
| 268 | |
| 269 | struct task_struct *tcp_rx; |
| 270 | struct task_struct *tcp_tx; |
| 271 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 272 | unsigned long event; |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 273 | wait_queue_head_t eh_waitq; |
| 274 | |
| 275 | struct eh_ops { |
| 276 | void (*shutdown)(struct usbip_device *); |
| 277 | void (*reset)(struct usbip_device *); |
| 278 | void (*unusable)(struct usbip_device *); |
| 279 | } eh_ops; |
Nazime Hande Harputluoglu | 894f1f4 | 2020-11-24 00:50:52 +0100 | [diff] [blame] | 280 | |
| 281 | #ifdef CONFIG_KCOV |
| 282 | u64 kcov_handle; |
| 283 | #endif |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 284 | }; |
| 285 | |
Oleg Nesterov | ba46ce3 | 2012-03-13 19:07:18 +0100 | [diff] [blame] | 286 | #define kthread_get_run(threadfn, data, namefmt, ...) \ |
| 287 | ({ \ |
| 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); \ |
| 293 | } \ |
| 294 | __k; \ |
| 295 | }) |
| 296 | |
| 297 | #define kthread_stop_put(k) \ |
| 298 | do { \ |
| 299 | kthread_stop(k); \ |
| 300 | put_task_struct(k); \ |
| 301 | } while (0) |
| 302 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 303 | /* usbip_common.c */ |
| 304 | void usbip_dump_urb(struct urb *purb); |
| 305 | void usbip_dump_header(struct usbip_header *pdu); |
| 306 | |
Bart Westgeest | 5a08c52 | 2011-12-19 17:44:11 -0500 | [diff] [blame] | 307 | int usbip_recv(struct socket *sock, void *buf, int size); |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 308 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 309 | void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, |
matt mooney | 4980d96 | 2011-05-06 03:47:45 -0700 | [diff] [blame] | 310 | int pack); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 311 | void usbip_header_correct_endian(struct usbip_header *pdu, int send); |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 312 | |
Bart Westgeest | 36ac9b0 | 2012-10-10 13:34:25 -0400 | [diff] [blame] | 313 | struct usbip_iso_packet_descriptor* |
| 314 | usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen); |
| 315 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 316 | /* some members of urb must be substituted before. */ |
| 317 | int usbip_recv_iso(struct usbip_device *ud, struct urb *urb); |
Bart Westgeest | ac2b41a | 2012-01-23 10:55:46 -0500 | [diff] [blame] | 318 | void usbip_pad_iso(struct usbip_device *ud, struct urb *urb); |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 319 | int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 320 | |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 321 | /* usbip_event.c */ |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 322 | int usbip_init_eh(void); |
| 323 | void usbip_finish_eh(void); |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 324 | int usbip_start_eh(struct usbip_device *ud); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 325 | void usbip_stop_eh(struct usbip_device *ud); |
| 326 | void usbip_event_add(struct usbip_device *ud, unsigned long event); |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 327 | int usbip_event_happened(struct usbip_device *ud); |
Nobuo Iwata | bb7871a | 2016-03-24 10:50:59 +0900 | [diff] [blame] | 328 | int usbip_in_eh(struct task_struct *task); |
Takahiro Hirofuchi | 05a1f28 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 329 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 330 | static inline int interface_to_busnum(struct usb_interface *interface) |
| 331 | { |
| 332 | struct usb_device *udev = interface_to_usbdev(interface); |
Pawel Lebioda | 3eed8c0 | 2014-05-14 19:20:27 +0200 | [diff] [blame] | 333 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 334 | return udev->bus->busnum; |
| 335 | } |
| 336 | |
| 337 | static inline int interface_to_devnum(struct usb_interface *interface) |
| 338 | { |
| 339 | struct usb_device *udev = interface_to_usbdev(interface); |
Pawel Lebioda | 3eed8c0 | 2014-05-14 19:20:27 +0200 | [diff] [blame] | 340 | |
matt mooney | b7d27ea | 2011-05-19 21:37:02 -0700 | [diff] [blame] | 341 | return udev->devnum; |
| 342 | } |
| 343 | |
Nazime Hande Harputluoglu | 894f1f4 | 2020-11-24 00:50:52 +0100 | [diff] [blame] | 344 | #ifdef CONFIG_KCOV |
| 345 | |
| 346 | static inline void usbip_kcov_handle_init(struct usbip_device *ud) |
| 347 | { |
| 348 | ud->kcov_handle = kcov_common_handle(); |
| 349 | } |
| 350 | |
| 351 | static inline void usbip_kcov_remote_start(struct usbip_device *ud) |
| 352 | { |
| 353 | kcov_remote_start_common(ud->kcov_handle); |
| 354 | } |
| 355 | |
| 356 | static inline void usbip_kcov_remote_stop(void) |
| 357 | { |
| 358 | kcov_remote_stop(); |
| 359 | } |
| 360 | |
| 361 | #else /* CONFIG_KCOV */ |
| 362 | |
| 363 | static inline void usbip_kcov_handle_init(struct usbip_device *ud) { } |
| 364 | static inline void usbip_kcov_remote_start(struct usbip_device *ud) { } |
| 365 | static inline void usbip_kcov_remote_stop(void) { } |
| 366 | |
| 367 | #endif /* CONFIG_KCOV */ |
| 368 | |
matt mooney | 7aaacb4 | 2011-05-11 22:33:43 -0700 | [diff] [blame] | 369 | #endif /* __USBIP_COMMON_H */ |