usbip_common.h 9.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3
/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 5
 * Copyright (C) 2015-2016 Samsung Electronics
 *               Krzysztof Opasiak <k.opasiak@samsung.com>
6 7
 */

8 9
#ifndef __USBIP_COMMON_H
#define __USBIP_COMMON_H
10

11
#include <linux/compiler.h>
12 13 14 15 16 17
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/net.h>
#include <linux/printk.h>
#include <linux/spinlock.h>
#include <linux/types.h>
18
#include <linux/usb.h>
19
#include <linux/wait.h>
20
#include <linux/sched/task.h>
21
#include <uapi/linux/usbip.h>
22

23
#undef pr_fmt
24

25 26
#ifdef DEBUG
#define pr_fmt(fmt)     KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
27
#else
28 29
#define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
#endif
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

enum {
	usbip_debug_xmit	= (1 << 0),
	usbip_debug_sysfs	= (1 << 1),
	usbip_debug_urb		= (1 << 2),
	usbip_debug_eh		= (1 << 3),

	usbip_debug_stub_cmp	= (1 << 8),
	usbip_debug_stub_dev	= (1 << 9),
	usbip_debug_stub_rx	= (1 << 10),
	usbip_debug_stub_tx	= (1 << 11),

	usbip_debug_vhci_rh	= (1 << 8),
	usbip_debug_vhci_hc	= (1 << 9),
	usbip_debug_vhci_rx	= (1 << 10),
	usbip_debug_vhci_tx	= (1 << 11),
	usbip_debug_vhci_sysfs  = (1 << 12)
};

B
Brian G. Merrell 已提交
49 50 51 52 53 54 55
#define usbip_dbg_flag_xmit	(usbip_debug_flag & usbip_debug_xmit)
#define usbip_dbg_flag_vhci_rh	(usbip_debug_flag & usbip_debug_vhci_rh)
#define usbip_dbg_flag_vhci_hc	(usbip_debug_flag & usbip_debug_vhci_hc)
#define usbip_dbg_flag_vhci_rx	(usbip_debug_flag & usbip_debug_vhci_rx)
#define usbip_dbg_flag_vhci_tx	(usbip_debug_flag & usbip_debug_vhci_tx)
#define usbip_dbg_flag_stub_rx	(usbip_debug_flag & usbip_debug_stub_rx)
#define usbip_dbg_flag_stub_tx	(usbip_debug_flag & usbip_debug_stub_tx)
56
#define usbip_dbg_flag_vhci_sysfs  (usbip_debug_flag & usbip_debug_vhci_sysfs)
57 58 59 60

extern unsigned long usbip_debug_flag;
extern struct device_attribute dev_attr_usbip_debug;

B
Brian G. Merrell 已提交
61
#define usbip_dbg_with_flag(flag, fmt, args...)		\
62 63
	do {						\
		if (flag & usbip_debug_flag)		\
64
			pr_debug(fmt, ##args);		\
65 66
	} while (0)

67
#define usbip_dbg_sysfs(fmt, args...) \
B
Brian G. Merrell 已提交
68
	usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
69
#define usbip_dbg_xmit(fmt, args...) \
B
Brian G. Merrell 已提交
70
	usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
71
#define usbip_dbg_urb(fmt, args...) \
B
Brian G. Merrell 已提交
72
	usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
73
#define usbip_dbg_eh(fmt, args...) \
B
Brian G. Merrell 已提交
74 75 76 77 78 79 80 81 82 83
	usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)

#define usbip_dbg_vhci_rh(fmt, args...)	\
	usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
#define usbip_dbg_vhci_hc(fmt, args...)	\
	usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
#define usbip_dbg_vhci_rx(fmt, args...)	\
	usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
#define usbip_dbg_vhci_tx(fmt, args...)	\
	usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
84
#define usbip_dbg_vhci_sysfs(fmt, args...) \
B
Brian G. Merrell 已提交
85 86
	usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)

87
#define usbip_dbg_stub_cmp(fmt, args...) \
B
Brian G. Merrell 已提交
88
	usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
89
#define usbip_dbg_stub_rx(fmt, args...) \
B
Brian G. Merrell 已提交
90
	usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
91
#define usbip_dbg_stub_tx(fmt, args...) \
B
Brian G. Merrell 已提交
92
	usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
93 94

/*
95
 * USB/IP request headers
96
 *
97 98 99 100 101 102
 * Each request is transferred across the network to its counterpart, which
 * facilitates the normal USB communication. The values contained in the headers
 * are basically the same as in a URB. Currently, four request types are
 * defined:
 *
 *  - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
103
 *    (client to server)
104 105
 *
 *  - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
106 107
 *    (server to client)
 *
108 109 110
 *  - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
 *    corresponds to usb_unlink_urb()
 *    (client to server)
111
 *
112 113
 *  - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
 *    (server to client)
114 115
 *
 */
116
#define USBIP_CMD_SUBMIT	0x0001
117 118
#define USBIP_CMD_UNLINK	0x0002
#define USBIP_RET_SUBMIT	0x0003
119
#define USBIP_RET_UNLINK	0x0004
120

121 122
#define USBIP_DIR_OUT	0x00
#define USBIP_DIR_IN	0x01
123 124 125 126 127 128 129 130 131 132

/**
 * struct usbip_header_basic - data pertinent to every request
 * @command: the usbip request type
 * @seqnum: sequential number that identifies requests; incremented per
 *	    connection
 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
 *	   in the stub driver, this value is ((busnum << 16) | devnum)
 * @direction: direction of the transfer
 * @ep: endpoint number
133 134 135 136 137 138
 */
struct usbip_header_basic {
	__u32 command;
	__u32 seqnum;
	__u32 devid;
	__u32 direction;
139
	__u32 ep;
140
} __packed;
141

142 143 144 145 146 147 148 149
/**
 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
 * @transfer_flags: URB flags
 * @transfer_buffer_length: the data size for (in) or (out) transfer
 * @start_frame: initial frame for isochronous or interrupt transfers
 * @number_of_packets: number of isochronous packets
 * @interval: maximum time for the request on the server-side host controller
 * @setup: setup data for a control request
150 151 152 153 154 155 156 157 158 159 160
 */
struct usbip_header_cmd_submit {
	__u32 transfer_flags;
	__s32 transfer_buffer_length;

	/* it is difficult for usbip to sync frames (reserved only?) */
	__s32 start_frame;
	__s32 number_of_packets;
	__s32 interval;

	unsigned char setup[8];
161
} __packed;
162

163 164 165 166 167 168 169
/**
 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
 * @status: return status of a non-iso request
 * @actual_length: number of bytes transferred
 * @start_frame: initial frame for isochronous or interrupt transfers
 * @number_of_packets: number of isochronous packets
 * @error_count: number of errors for isochronous transfers
170 171 172
 */
struct usbip_header_ret_submit {
	__s32 status;
173 174 175 176
	__s32 actual_length;
	__s32 start_frame;
	__s32 number_of_packets;
	__s32 error_count;
177
} __packed;
178

179 180 181
/**
 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
 * @seqnum: the URB seqnum to unlink
182 183
 */
struct usbip_header_cmd_unlink {
184
	__u32 seqnum;
185
} __packed;
186

187 188 189
/**
 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
 * @status: return status of the request
190 191 192
 */
struct usbip_header_ret_unlink {
	__s32 status;
193
} __packed;
194

195 196 197 198
/**
 * struct usbip_header - common header for all usbip packets
 * @base: the basic header
 * @u: packet type dependent header
199 200 201 202 203 204 205 206 207 208
 */
struct usbip_header {
	struct usbip_header_basic base;

	union {
		struct usbip_header_cmd_submit	cmd_submit;
		struct usbip_header_ret_submit	ret_submit;
		struct usbip_header_cmd_unlink	cmd_unlink;
		struct usbip_header_ret_unlink	ret_unlink;
	} u;
209
} __packed;
210

211 212 213 214 215 216 217 218 219
/*
 * This is the same as usb_iso_packet_descriptor but packed for pdu.
 */
struct usbip_iso_packet_descriptor {
	__u32 offset;
	__u32 length;			/* expected length */
	__u32 actual_length;
	__u32 status;
} __packed;
220 221 222 223

enum usbip_side {
	USBIP_VHCI,
	USBIP_STUB,
224
	USBIP_VUDC,
225 226
};

227
/* event handler */
228 229 230 231 232
#define USBIP_EH_SHUTDOWN	(1 << 0)
#define USBIP_EH_BYE		(1 << 1)
#define USBIP_EH_RESET		(1 << 2)
#define USBIP_EH_UNUSABLE	(1 << 3)

233
#define	SDEV_EVENT_REMOVED	(USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
234 235 236 237 238
#define	SDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	SDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	SDEV_EVENT_ERROR_SUBMIT	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	SDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)

239 240 241 242 243 244 245
#define VUDC_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
#define	VUDC_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	VUDC_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
/* catastrophic emulated usb error */
#define	VUDC_EVENT_ERROR_USB	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
#define	VUDC_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)

246
#define	VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
247 248 249 250
#define	VDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	VDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
#define	VDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)

251 252 253
/* a common structure for stub_device and vhci_device */
struct usbip_device {
	enum usbip_side side;
254
	enum usbip_device_status status;
255 256 257 258

	/* lock for status */
	spinlock_t lock;

259
	int sockfd;
260 261 262 263 264
	struct socket *tcp_socket;

	struct task_struct *tcp_rx;
	struct task_struct *tcp_tx;

265 266 267 268 269 270 271 272 273 274
	unsigned long event;
	wait_queue_head_t eh_waitq;

	struct eh_ops {
		void (*shutdown)(struct usbip_device *);
		void (*reset)(struct usbip_device *);
		void (*unusable)(struct usbip_device *);
	} eh_ops;
};

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
#define kthread_get_run(threadfn, data, namefmt, ...)			   \
({									   \
	struct task_struct *__k						   \
		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
	if (!IS_ERR(__k)) {						   \
		get_task_struct(__k);					   \
		wake_up_process(__k);					   \
	}								   \
	__k;								   \
})

#define kthread_stop_put(k)		\
	do {				\
		kthread_stop(k);	\
		put_task_struct(k);	\
	} while (0)

292 293 294 295
/* usbip_common.c */
void usbip_dump_urb(struct urb *purb);
void usbip_dump_header(struct usbip_header *pdu);

296
int usbip_recv(struct socket *sock, void *buf, int size);
297

298
void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
299
		    int pack);
300
void usbip_header_correct_endian(struct usbip_header *pdu, int send);
301

302 303 304
struct usbip_iso_packet_descriptor*
usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);

305 306
/* some members of urb must be substituted before. */
int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
307
void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
308
int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
309 310

/* usbip_event.c */
N
Nobuo Iwata 已提交
311 312
int usbip_init_eh(void);
void usbip_finish_eh(void);
B
Brian G. Merrell 已提交
313
int usbip_start_eh(struct usbip_device *ud);
314 315
void usbip_stop_eh(struct usbip_device *ud);
void usbip_event_add(struct usbip_device *ud, unsigned long event);
B
Brian G. Merrell 已提交
316
int usbip_event_happened(struct usbip_device *ud);
N
Nobuo Iwata 已提交
317
int usbip_in_eh(struct task_struct *task);
318

319 320 321
static inline int interface_to_busnum(struct usb_interface *interface)
{
	struct usb_device *udev = interface_to_usbdev(interface);
322

323 324 325 326 327 328
	return udev->bus->busnum;
}

static inline int interface_to_devnum(struct usb_interface *interface)
{
	struct usb_device *udev = interface_to_usbdev(interface);
329

330 331 332
	return udev->devnum;
}

333
#endif /* __USBIP_COMMON_H */