2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #ifndef __USBIP_COMMON_H
21 #define __USBIP_COMMON_H
23 #include <linux/compiler.h>
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>
30 #include <linux/usb.h>
31 #include <linux/wait.h>
33 #define USBIP_VERSION "1.0.0"
38 #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
40 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 usbip_debug_xmit
= (1 << 0),
45 usbip_debug_sysfs
= (1 << 1),
46 usbip_debug_urb
= (1 << 2),
47 usbip_debug_eh
= (1 << 3),
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),
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)
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)
68 #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
70 extern unsigned long usbip_debug_flag
;
71 extern struct device_attribute dev_attr_usbip_debug
;
73 #define usbip_dbg_with_flag(flag, fmt, args...) \
75 if (flag & usbip_debug_flag) \
76 pr_debug(fmt, ##args); \
79 #define usbip_dbg_sysfs(fmt, args...) \
80 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
81 #define usbip_dbg_xmit(fmt, args...) \
82 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
83 #define usbip_dbg_urb(fmt, args...) \
84 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
85 #define usbip_dbg_eh(fmt, args...) \
86 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
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)
96 #define usbip_dbg_vhci_sysfs(fmt, args...) \
97 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
99 #define usbip_dbg_stub_cmp(fmt, args...) \
100 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
101 #define usbip_dbg_stub_rx(fmt, args...) \
102 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
103 #define usbip_dbg_stub_tx(fmt, args...) \
104 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
107 * USB/IP request headers.
108 * Currently, we define 4 request types:
110 * - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
112 * - RET_RETURN transfers the result of CMD_SUBMIT.
114 * - CMD_UNLINK transfers an unlink request of a pending USB request.
116 * - RET_UNLINK transfers the result of CMD_UNLINK.
119 * Note: The below request formats are based on the USB subsystem of Linux. Its
120 * details will be defined when other implementations come.
126 * A basic header followed by other additional headers.
128 struct usbip_header_basic
{
129 #define USBIP_CMD_SUBMIT 0x0001
130 #define USBIP_CMD_UNLINK 0x0002
131 #define USBIP_RET_SUBMIT 0x0003
132 #define USBIP_RET_UNLINK 0x0004
135 /* sequential number which identifies requests.
136 * incremented per connections */
139 /* devid is used to specify a remote USB device uniquely instead
140 * of busnum and devnum in Linux. In the case of Linux stub_driver,
141 * this value is ((busnum << 16) | devnum) */
144 #define USBIP_DIR_OUT 0
145 #define USBIP_DIR_IN 1
147 __u32 ep
; /* endpoint number */
151 * An additional header for a CMD_SUBMIT packet.
153 struct usbip_header_cmd_submit
{
154 /* these values are basically the same as in a URB. */
156 /* the same in a URB. */
157 __u32 transfer_flags
;
159 /* set the following data size (out),
160 * or expected reading data size (in) */
161 __s32 transfer_buffer_length
;
163 /* it is difficult for usbip to sync frames (reserved only?) */
166 /* the number of iso descriptors that follows this header */
167 __s32 number_of_packets
;
169 /* the maximum time within which this request works in a host
170 * controller of a server side */
173 /* set setup packet data for a CTRL request */
174 unsigned char setup
[8];
178 * An additional header for a RET_SUBMIT packet.
180 struct usbip_header_ret_submit
{
182 __s32 actual_length
; /* returned data length */
183 __s32 start_frame
; /* ISO and INT */
184 __s32 number_of_packets
; /* ISO only */
185 __s32 error_count
; /* ISO only */
189 * An additional header for a CMD_UNLINK packet.
191 struct usbip_header_cmd_unlink
{
192 __u32 seqnum
; /* URB's seqnum that will be unlinked */
196 * An additional header for a RET_UNLINK packet.
198 struct usbip_header_ret_unlink
{
202 /* the same as usb_iso_packet_descriptor but packed for pdu */
203 struct usbip_iso_packet_descriptor
{
205 __u32 length
; /* expected length */
211 * All usbip packets use a common header to keep code simple.
213 struct usbip_header
{
214 struct usbip_header_basic base
;
217 struct usbip_header_cmd_submit cmd_submit
;
218 struct usbip_header_ret_submit ret_submit
;
219 struct usbip_header_cmd_unlink cmd_unlink
;
220 struct usbip_header_ret_unlink ret_unlink
;
224 int usbip_xmit(int, struct socket
*, char *, int, int);
225 int usbip_sendmsg(struct socket
*, struct msghdr
*, int);
227 static inline int interface_to_busnum(struct usb_interface
*interface
)
229 struct usb_device
*udev
= interface_to_usbdev(interface
);
230 return udev
->bus
->busnum
;
233 static inline int interface_to_devnum(struct usb_interface
*interface
)
235 struct usb_device
*udev
= interface_to_usbdev(interface
);
239 static inline int interface_to_infnum(struct usb_interface
*interface
)
241 return interface
->cur_altsetting
->desc
.bInterfaceNumber
;
245 int setnodelay(struct socket
*);
246 int setquickack(struct socket
*);
247 int setkeepalive(struct socket
*socket
);
248 void setreuse(struct socket
*);
251 struct socket
*sockfd_to_socket(unsigned int);
252 int set_sockaddr(struct socket
*socket
, struct sockaddr_storage
*ss
);
254 void usbip_dump_urb(struct urb
*purb
);
255 void usbip_dump_header(struct usbip_header
*pdu
);
265 /* sdev is available. */
266 SDEV_ST_AVAILABLE
= 0x01,
267 /* sdev is now used. */
269 /* sdev is unusable because of a fatal error. */
272 /* vdev does not connect a remote device. */
274 /* vdev is used, but the USB address is not assigned yet */
280 /* a common structure for stub_device and vhci_device */
281 struct usbip_device
{
282 enum usbip_side side
;
283 enum usbip_status status
;
285 /* lock for status */
288 struct socket
*tcp_socket
;
290 struct task_struct
*tcp_rx
;
291 struct task_struct
*tcp_tx
;
294 #define USBIP_EH_SHUTDOWN (1 << 0)
295 #define USBIP_EH_BYE (1 << 1)
296 #define USBIP_EH_RESET (1 << 2)
297 #define USBIP_EH_UNUSABLE (1 << 3)
299 #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
300 #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
301 #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
302 #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
303 #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
305 #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
306 #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
307 #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
308 #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
311 struct task_struct
*eh
;
312 wait_queue_head_t eh_waitq
;
315 void (*shutdown
)(struct usbip_device
*);
316 void (*reset
)(struct usbip_device
*);
317 void (*unusable
)(struct usbip_device
*);
321 void usbip_pack_pdu(struct usbip_header
*pdu
, struct urb
*urb
, int cmd
,
324 void usbip_header_correct_endian(struct usbip_header
*pdu
, int send
);
325 /* some members of urb must be substituted before. */
326 int usbip_recv_xbuff(struct usbip_device
*ud
, struct urb
*urb
);
327 /* some members of urb must be substituted before. */
328 int usbip_recv_iso(struct usbip_device
*ud
, struct urb
*urb
);
329 /* some members of urb must be substituted before. */
330 int usbip_pad_iso(struct usbip_device
*ud
, struct urb
*urb
);
331 void *usbip_alloc_iso_desc_pdu(struct urb
*urb
, ssize_t
*bufflen
);
334 int usbip_start_eh(struct usbip_device
*ud
);
335 void usbip_stop_eh(struct usbip_device
*ud
);
336 void usbip_event_add(struct usbip_device
*ud
, unsigned long event
);
337 int usbip_event_happened(struct usbip_device
*ud
);
339 #endif /* __USBIP_COMMON_H */