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