blob: 473fb8a872893caa3494fe9a4a85b60600bdfd28 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06002/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +01004 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06006 */
7
matt mooney7aaacb42011-05-11 22:33:43 -07008#ifndef __USBIP_COMMON_H
9#define __USBIP_COMMON_H
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060010
matt mooney4980d962011-05-06 03:47:45 -070011#include <linux/compiler.h>
matt mooney7aaacb42011-05-11 22:33:43 -070012#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 Hirofuchi05a1f282008-07-09 14:56:51 -060018#include <linux/usb.h>
matt mooney7aaacb42011-05-11 22:33:43 -070019#include <linux/wait.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010020#include <linux/sched/task.h>
Valentina Manea96c27372014-08-20 07:31:00 +030021#include <uapi/linux/usbip.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060022
matt mooney1a4b6f62011-05-19 16:47:32 -070023#undef pr_fmt
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060024
matt mooney1a4b6f62011-05-19 16:47:32 -070025#ifdef DEBUG
26#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
matt mooney981f30c2011-05-11 01:54:13 -070027#else
matt mooney1a4b6f62011-05-19 16:47:32 -070028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29#endif
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060030
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060031enum {
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. Merrellb8868e42009-07-21 00:46:13 -060049#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 mooney87352762011-05-19 21:36:56 -070056#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060057
58extern unsigned long usbip_debug_flag;
59extern struct device_attribute dev_attr_usbip_debug;
60
Brian G. Merrellb8868e42009-07-21 00:46:13 -060061#define usbip_dbg_with_flag(flag, fmt, args...) \
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060062 do { \
63 if (flag & usbip_debug_flag) \
matt mooney1a4b6f62011-05-19 16:47:32 -070064 pr_debug(fmt, ##args); \
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060065 } while (0)
66
matt mooney4980d962011-05-06 03:47:45 -070067#define usbip_dbg_sysfs(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060068 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070069#define usbip_dbg_xmit(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060070 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070071#define usbip_dbg_urb(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060072 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070073#define usbip_dbg_eh(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060074 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060075
Brian G. Merrellb8868e42009-07-21 00:46:13 -060076#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 mooney4980d962011-05-06 03:47:45 -070084#define usbip_dbg_vhci_sysfs(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060085 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060086
matt mooney4980d962011-05-06 03:47:45 -070087#define usbip_dbg_stub_cmp(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060088 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070089#define usbip_dbg_stub_rx(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060090 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070091#define usbip_dbg_stub_tx(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060092 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060093
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060094/*
matt mooneyb7d27ea2011-05-19 21:37:02 -070095 * USB/IP request headers
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060096 *
matt mooneyb7d27ea2011-05-19 21:37:02 -070097 * 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 Hirofuchi05a1f282008-07-09 14:56:51 -0600103 * (client to server)
matt mooneyb7d27ea2011-05-19 21:37:02 -0700104 *
105 * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600106 * (server to client)
107 *
matt mooneyb7d27ea2011-05-19 21:37:02 -0700108 * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
109 * corresponds to usb_unlink_urb()
110 * (client to server)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600111 *
matt mooneyb7d27ea2011-05-19 21:37:02 -0700112 * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
113 * (server to client)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600114 *
115 */
matt mooneyb7d27ea2011-05-19 21:37:02 -0700116#define USBIP_CMD_SUBMIT 0x0001
Alexander Thomas7518b9b2011-09-19 16:56:51 +0200117#define USBIP_CMD_UNLINK 0x0002
118#define USBIP_RET_SUBMIT 0x0003
matt mooneyb7d27ea2011-05-19 21:37:02 -0700119#define USBIP_RET_UNLINK 0x0004
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600120
Alexander Thomas7518b9b2011-09-19 16:56:51 +0200121#define USBIP_DIR_OUT 0x00
122#define USBIP_DIR_IN 0x01
matt mooneyb7d27ea2011-05-19 21:37:02 -0700123
124/**
125 * struct usbip_header_basic - data pertinent to every request
126 * @command: the usbip request type
127 * @seqnum: sequential number that identifies requests; incremented per
128 * connection
129 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
130 * in the stub driver, this value is ((busnum << 16) | devnum)
131 * @direction: direction of the transfer
132 * @ep: endpoint number
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600133 */
134struct usbip_header_basic {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600135 __u32 command;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600136 __u32 seqnum;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600137 __u32 devid;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600138 __u32 direction;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700139 __u32 ep;
matt mooney4980d962011-05-06 03:47:45 -0700140} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600141
matt mooneyb7d27ea2011-05-19 21:37:02 -0700142/**
143 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
144 * @transfer_flags: URB flags
145 * @transfer_buffer_length: the data size for (in) or (out) transfer
146 * @start_frame: initial frame for isochronous or interrupt transfers
147 * @number_of_packets: number of isochronous packets
148 * @interval: maximum time for the request on the server-side host controller
149 * @setup: setup data for a control request
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600150 */
151struct usbip_header_cmd_submit {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600152 __u32 transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600153 __s32 transfer_buffer_length;
154
155 /* it is difficult for usbip to sync frames (reserved only?) */
156 __s32 start_frame;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600157 __s32 number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600158 __s32 interval;
159
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600160 unsigned char setup[8];
matt mooney4980d962011-05-06 03:47:45 -0700161} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600162
matt mooneyb7d27ea2011-05-19 21:37:02 -0700163/**
164 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
165 * @status: return status of a non-iso request
166 * @actual_length: number of bytes transferred
167 * @start_frame: initial frame for isochronous or interrupt transfers
168 * @number_of_packets: number of isochronous packets
169 * @error_count: number of errors for isochronous transfers
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600170 */
171struct usbip_header_ret_submit {
172 __s32 status;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700173 __s32 actual_length;
174 __s32 start_frame;
175 __s32 number_of_packets;
176 __s32 error_count;
matt mooney4980d962011-05-06 03:47:45 -0700177} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600178
matt mooneyb7d27ea2011-05-19 21:37:02 -0700179/**
180 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
181 * @seqnum: the URB seqnum to unlink
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600182 */
183struct usbip_header_cmd_unlink {
matt mooneyb7d27ea2011-05-19 21:37:02 -0700184 __u32 seqnum;
matt mooney4980d962011-05-06 03:47:45 -0700185} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600186
matt mooneyb7d27ea2011-05-19 21:37:02 -0700187/**
188 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
189 * @status: return status of the request
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600190 */
191struct usbip_header_ret_unlink {
192 __s32 status;
matt mooney4980d962011-05-06 03:47:45 -0700193} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600194
matt mooneyb7d27ea2011-05-19 21:37:02 -0700195/**
196 * struct usbip_header - common header for all usbip packets
197 * @base: the basic header
198 * @u: packet type dependent header
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600199 */
200struct usbip_header {
201 struct usbip_header_basic base;
202
203 union {
204 struct usbip_header_cmd_submit cmd_submit;
205 struct usbip_header_ret_submit ret_submit;
206 struct usbip_header_cmd_unlink cmd_unlink;
207 struct usbip_header_ret_unlink ret_unlink;
208 } u;
matt mooney4980d962011-05-06 03:47:45 -0700209} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600210
matt mooneyb7d27ea2011-05-19 21:37:02 -0700211/*
212 * This is the same as usb_iso_packet_descriptor but packed for pdu.
213 */
214struct usbip_iso_packet_descriptor {
215 __u32 offset;
216 __u32 length; /* expected length */
217 __u32 actual_length;
218 __u32 status;
219} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600220
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600221enum usbip_side {
222 USBIP_VHCI,
223 USBIP_STUB,
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100224 USBIP_VUDC,
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600225};
226
matt mooneyb7d27ea2011-05-19 21:37:02 -0700227/* event handler */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600228#define USBIP_EH_SHUTDOWN (1 << 0)
229#define USBIP_EH_BYE (1 << 1)
230#define USBIP_EH_RESET (1 << 2)
231#define USBIP_EH_UNUSABLE (1 << 3)
232
Alexander Popov134a9262016-05-20 12:37:28 +0300233#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600234#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
235#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
236#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
237#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
238
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100239#define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
240#define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
241#define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
242/* catastrophic emulated usb error */
243#define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
244#define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
245
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600246#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
247#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
248#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
249#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
250
matt mooneyb7d27ea2011-05-19 21:37:02 -0700251/* a common structure for stub_device and vhci_device */
252struct usbip_device {
253 enum usbip_side side;
Shuah Khancd84ec92014-03-03 16:38:44 -0700254 enum usbip_device_status status;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700255
256 /* lock for status */
257 spinlock_t lock;
258
Shuah Khan2f2d0082017-12-07 14:16:49 -0700259 int sockfd;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700260 struct socket *tcp_socket;
261
262 struct task_struct *tcp_rx;
263 struct task_struct *tcp_tx;
264
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600265 unsigned long event;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600266 wait_queue_head_t eh_waitq;
267
268 struct eh_ops {
269 void (*shutdown)(struct usbip_device *);
270 void (*reset)(struct usbip_device *);
271 void (*unusable)(struct usbip_device *);
272 } eh_ops;
273};
274
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100275#define kthread_get_run(threadfn, data, namefmt, ...) \
276({ \
277 struct task_struct *__k \
278 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
279 if (!IS_ERR(__k)) { \
280 get_task_struct(__k); \
281 wake_up_process(__k); \
282 } \
283 __k; \
284})
285
286#define kthread_stop_put(k) \
287 do { \
288 kthread_stop(k); \
289 put_task_struct(k); \
290 } while (0)
291
matt mooneyb7d27ea2011-05-19 21:37:02 -0700292/* usbip_common.c */
293void usbip_dump_urb(struct urb *purb);
294void usbip_dump_header(struct usbip_header *pdu);
295
Bart Westgeest5a08c522011-12-19 17:44:11 -0500296int usbip_recv(struct socket *sock, void *buf, int size);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700297
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600298void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooney4980d962011-05-06 03:47:45 -0700299 int pack);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600300void usbip_header_correct_endian(struct usbip_header *pdu, int send);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700301
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400302struct usbip_iso_packet_descriptor*
303usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
304
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600305/* some members of urb must be substituted before. */
306int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500307void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700308int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600309
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600310/* usbip_event.c */
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900311int usbip_init_eh(void);
312void usbip_finish_eh(void);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600313int usbip_start_eh(struct usbip_device *ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600314void usbip_stop_eh(struct usbip_device *ud);
315void usbip_event_add(struct usbip_device *ud, unsigned long event);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600316int usbip_event_happened(struct usbip_device *ud);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900317int usbip_in_eh(struct task_struct *task);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600318
matt mooneyb7d27ea2011-05-19 21:37:02 -0700319static inline int interface_to_busnum(struct usb_interface *interface)
320{
321 struct usb_device *udev = interface_to_usbdev(interface);
Pawel Lebioda3eed8c02014-05-14 19:20:27 +0200322
matt mooneyb7d27ea2011-05-19 21:37:02 -0700323 return udev->bus->busnum;
324}
325
326static inline int interface_to_devnum(struct usb_interface *interface)
327{
328 struct usb_device *udev = interface_to_usbdev(interface);
Pawel Lebioda3eed8c02014-05-14 19:20:27 +0200329
matt mooneyb7d27ea2011-05-19 21:37:02 -0700330 return udev->devnum;
331}
332
matt mooney7aaacb42011-05-11 22:33:43 -0700333#endif /* __USBIP_COMMON_H */