blob: 9f490375ac92362dce2174fb246a2e1425dd25b7 [file] [log] [blame]
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06001/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +01003 * Copyright (C) 2015-2016 Samsung Electronics
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -06005 *
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
matt mooney7aaacb42011-05-11 22:33:43 -070022#ifndef __USBIP_COMMON_H
23#define __USBIP_COMMON_H
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060024
matt mooney4980d962011-05-06 03:47:45 -070025#include <linux/compiler.h>
matt mooney7aaacb42011-05-11 22:33:43 -070026#include <linux/device.h>
27#include <linux/interrupt.h>
28#include <linux/net.h>
29#include <linux/printk.h>
30#include <linux/spinlock.h>
31#include <linux/types.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060032#include <linux/usb.h>
matt mooney7aaacb42011-05-11 22:33:43 -070033#include <linux/wait.h>
Valentina Manea96c27372014-08-20 07:31:00 +030034#include <uapi/linux/usbip.h>
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060035
matt mooney6973c6f2011-05-11 01:54:14 -070036#define USBIP_VERSION "1.0.0"
37
matt mooney1a4b6f62011-05-19 16:47:32 -070038#undef pr_fmt
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060039
matt mooney1a4b6f62011-05-19 16:47:32 -070040#ifdef DEBUG
41#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
matt mooney981f30c2011-05-11 01:54:13 -070042#else
matt mooney1a4b6f62011-05-19 16:47:32 -070043#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44#endif
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060045
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060046enum {
47 usbip_debug_xmit = (1 << 0),
48 usbip_debug_sysfs = (1 << 1),
49 usbip_debug_urb = (1 << 2),
50 usbip_debug_eh = (1 << 3),
51
52 usbip_debug_stub_cmp = (1 << 8),
53 usbip_debug_stub_dev = (1 << 9),
54 usbip_debug_stub_rx = (1 << 10),
55 usbip_debug_stub_tx = (1 << 11),
56
57 usbip_debug_vhci_rh = (1 << 8),
58 usbip_debug_vhci_hc = (1 << 9),
59 usbip_debug_vhci_rx = (1 << 10),
60 usbip_debug_vhci_tx = (1 << 11),
61 usbip_debug_vhci_sysfs = (1 << 12)
62};
63
Brian G. Merrellb8868e42009-07-21 00:46:13 -060064#define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
65#define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
66#define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
67#define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
68#define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
69#define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
70#define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
matt mooney87352762011-05-19 21:36:56 -070071#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060072
73extern unsigned long usbip_debug_flag;
74extern struct device_attribute dev_attr_usbip_debug;
75
Brian G. Merrellb8868e42009-07-21 00:46:13 -060076#define usbip_dbg_with_flag(flag, fmt, args...) \
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060077 do { \
78 if (flag & usbip_debug_flag) \
matt mooney1a4b6f62011-05-19 16:47:32 -070079 pr_debug(fmt, ##args); \
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060080 } while (0)
81
matt mooney4980d962011-05-06 03:47:45 -070082#define usbip_dbg_sysfs(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060083 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070084#define usbip_dbg_xmit(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060085 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070086#define usbip_dbg_urb(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060087 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070088#define usbip_dbg_eh(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -060089 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -060090
Brian G. Merrellb8868e42009-07-21 00:46:13 -060091#define usbip_dbg_vhci_rh(fmt, args...) \
92 usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
93#define usbip_dbg_vhci_hc(fmt, args...) \
94 usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
95#define usbip_dbg_vhci_rx(fmt, args...) \
96 usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
97#define usbip_dbg_vhci_tx(fmt, args...) \
98 usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -070099#define usbip_dbg_vhci_sysfs(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600100 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600101
matt mooney4980d962011-05-06 03:47:45 -0700102#define usbip_dbg_stub_cmp(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600103 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -0700104#define usbip_dbg_stub_rx(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600105 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
matt mooney4980d962011-05-06 03:47:45 -0700106#define usbip_dbg_stub_tx(fmt, args...) \
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600107 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600108
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600109/*
matt mooneyb7d27ea2011-05-19 21:37:02 -0700110 * USB/IP request headers
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600111 *
matt mooneyb7d27ea2011-05-19 21:37:02 -0700112 * Each request is transferred across the network to its counterpart, which
113 * facilitates the normal USB communication. The values contained in the headers
114 * are basically the same as in a URB. Currently, four request types are
115 * defined:
116 *
117 * - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600118 * (client to server)
matt mooneyb7d27ea2011-05-19 21:37:02 -0700119 *
120 * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600121 * (server to client)
122 *
matt mooneyb7d27ea2011-05-19 21:37:02 -0700123 * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
124 * corresponds to usb_unlink_urb()
125 * (client to server)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600126 *
matt mooneyb7d27ea2011-05-19 21:37:02 -0700127 * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
128 * (server to client)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600129 *
130 */
matt mooneyb7d27ea2011-05-19 21:37:02 -0700131#define USBIP_CMD_SUBMIT 0x0001
Alexander Thomas7518b9b2011-09-19 16:56:51 +0200132#define USBIP_CMD_UNLINK 0x0002
133#define USBIP_RET_SUBMIT 0x0003
matt mooneyb7d27ea2011-05-19 21:37:02 -0700134#define USBIP_RET_UNLINK 0x0004
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600135
Alexander Thomas7518b9b2011-09-19 16:56:51 +0200136#define USBIP_DIR_OUT 0x00
137#define USBIP_DIR_IN 0x01
matt mooneyb7d27ea2011-05-19 21:37:02 -0700138
139/**
140 * struct usbip_header_basic - data pertinent to every request
141 * @command: the usbip request type
142 * @seqnum: sequential number that identifies requests; incremented per
143 * connection
144 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
145 * in the stub driver, this value is ((busnum << 16) | devnum)
146 * @direction: direction of the transfer
147 * @ep: endpoint number
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600148 */
149struct usbip_header_basic {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600150 __u32 command;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600151 __u32 seqnum;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600152 __u32 devid;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600153 __u32 direction;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700154 __u32 ep;
matt mooney4980d962011-05-06 03:47:45 -0700155} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600156
matt mooneyb7d27ea2011-05-19 21:37:02 -0700157/**
158 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
159 * @transfer_flags: URB flags
160 * @transfer_buffer_length: the data size for (in) or (out) transfer
161 * @start_frame: initial frame for isochronous or interrupt transfers
162 * @number_of_packets: number of isochronous packets
163 * @interval: maximum time for the request on the server-side host controller
164 * @setup: setup data for a control request
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600165 */
166struct usbip_header_cmd_submit {
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600167 __u32 transfer_flags;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600168 __s32 transfer_buffer_length;
169
170 /* it is difficult for usbip to sync frames (reserved only?) */
171 __s32 start_frame;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600172 __s32 number_of_packets;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600173 __s32 interval;
174
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600175 unsigned char setup[8];
matt mooney4980d962011-05-06 03:47:45 -0700176} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600177
matt mooneyb7d27ea2011-05-19 21:37:02 -0700178/**
179 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
180 * @status: return status of a non-iso request
181 * @actual_length: number of bytes transferred
182 * @start_frame: initial frame for isochronous or interrupt transfers
183 * @number_of_packets: number of isochronous packets
184 * @error_count: number of errors for isochronous transfers
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600185 */
186struct usbip_header_ret_submit {
187 __s32 status;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700188 __s32 actual_length;
189 __s32 start_frame;
190 __s32 number_of_packets;
191 __s32 error_count;
matt mooney4980d962011-05-06 03:47:45 -0700192} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600193
matt mooneyb7d27ea2011-05-19 21:37:02 -0700194/**
195 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
196 * @seqnum: the URB seqnum to unlink
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600197 */
198struct usbip_header_cmd_unlink {
matt mooneyb7d27ea2011-05-19 21:37:02 -0700199 __u32 seqnum;
matt mooney4980d962011-05-06 03:47:45 -0700200} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600201
matt mooneyb7d27ea2011-05-19 21:37:02 -0700202/**
203 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
204 * @status: return status of the request
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600205 */
206struct usbip_header_ret_unlink {
207 __s32 status;
matt mooney4980d962011-05-06 03:47:45 -0700208} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600209
matt mooneyb7d27ea2011-05-19 21:37:02 -0700210/**
211 * struct usbip_header - common header for all usbip packets
212 * @base: the basic header
213 * @u: packet type dependent header
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600214 */
215struct usbip_header {
216 struct usbip_header_basic base;
217
218 union {
219 struct usbip_header_cmd_submit cmd_submit;
220 struct usbip_header_ret_submit ret_submit;
221 struct usbip_header_cmd_unlink cmd_unlink;
222 struct usbip_header_ret_unlink ret_unlink;
223 } u;
matt mooney4980d962011-05-06 03:47:45 -0700224} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600225
matt mooneyb7d27ea2011-05-19 21:37:02 -0700226/*
227 * This is the same as usb_iso_packet_descriptor but packed for pdu.
228 */
229struct usbip_iso_packet_descriptor {
230 __u32 offset;
231 __u32 length; /* expected length */
232 __u32 actual_length;
233 __u32 status;
234} __packed;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600235
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600236enum usbip_side {
237 USBIP_VHCI,
238 USBIP_STUB,
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100239 USBIP_VUDC,
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600240};
241
matt mooneyb7d27ea2011-05-19 21:37:02 -0700242/* event handler */
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600243#define USBIP_EH_SHUTDOWN (1 << 0)
244#define USBIP_EH_BYE (1 << 1)
245#define USBIP_EH_RESET (1 << 2)
246#define USBIP_EH_UNUSABLE (1 << 3)
247
Alexander Popov134a9262016-05-20 12:37:28 +0300248#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600249#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
250#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
251#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
252#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
253
Igor Kotrasinskic7af4c22016-03-08 21:48:57 +0100254#define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
255#define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
256#define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
257/* catastrophic emulated usb error */
258#define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
259#define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
260
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600261#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
262#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
263#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
264#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
265
matt mooneyb7d27ea2011-05-19 21:37:02 -0700266/* a common structure for stub_device and vhci_device */
267struct usbip_device {
268 enum usbip_side side;
Shuah Khancd84ec92014-03-03 16:38:44 -0700269 enum usbip_device_status status;
matt mooneyb7d27ea2011-05-19 21:37:02 -0700270
271 /* lock for status */
272 spinlock_t lock;
273
274 struct socket *tcp_socket;
275
276 struct task_struct *tcp_rx;
277 struct task_struct *tcp_tx;
278
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600279 unsigned long event;
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600280 wait_queue_head_t eh_waitq;
281
282 struct eh_ops {
283 void (*shutdown)(struct usbip_device *);
284 void (*reset)(struct usbip_device *);
285 void (*unusable)(struct usbip_device *);
286 } eh_ops;
287};
288
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100289#define kthread_get_run(threadfn, data, namefmt, ...) \
290({ \
291 struct task_struct *__k \
292 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
293 if (!IS_ERR(__k)) { \
294 get_task_struct(__k); \
295 wake_up_process(__k); \
296 } \
297 __k; \
298})
299
300#define kthread_stop_put(k) \
301 do { \
302 kthread_stop(k); \
303 put_task_struct(k); \
304 } while (0)
305
matt mooneyb7d27ea2011-05-19 21:37:02 -0700306/* usbip_common.c */
307void usbip_dump_urb(struct urb *purb);
308void usbip_dump_header(struct usbip_header *pdu);
309
Bart Westgeest5a08c522011-12-19 17:44:11 -0500310int usbip_recv(struct socket *sock, void *buf, int size);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700311
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600312void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
matt mooney4980d962011-05-06 03:47:45 -0700313 int pack);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600314void usbip_header_correct_endian(struct usbip_header *pdu, int send);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700315
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400316struct usbip_iso_packet_descriptor*
317usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
318
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600319/* some members of urb must be substituted before. */
320int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
Bart Westgeestac2b41a2012-01-23 10:55:46 -0500321void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
matt mooneyb7d27ea2011-05-19 21:37:02 -0700322int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600323
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600324/* usbip_event.c */
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900325int usbip_init_eh(void);
326void usbip_finish_eh(void);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600327int usbip_start_eh(struct usbip_device *ud);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600328void usbip_stop_eh(struct usbip_device *ud);
329void usbip_event_add(struct usbip_device *ud, unsigned long event);
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600330int usbip_event_happened(struct usbip_device *ud);
Nobuo Iwatabb7871a2016-03-24 10:50:59 +0900331int usbip_in_eh(struct task_struct *task);
Takahiro Hirofuchi05a1f282008-07-09 14:56:51 -0600332
matt mooneyb7d27ea2011-05-19 21:37:02 -0700333static inline int interface_to_busnum(struct usb_interface *interface)
334{
335 struct usb_device *udev = interface_to_usbdev(interface);
Pawel Lebioda3eed8c02014-05-14 19:20:27 +0200336
matt mooneyb7d27ea2011-05-19 21:37:02 -0700337 return udev->bus->busnum;
338}
339
340static inline int interface_to_devnum(struct usb_interface *interface)
341{
342 struct usb_device *udev = interface_to_usbdev(interface);
Pawel Lebioda3eed8c02014-05-14 19:20:27 +0200343
matt mooneyb7d27ea2011-05-19 21:37:02 -0700344 return udev->devnum;
345}
346
matt mooney7aaacb42011-05-11 22:33:43 -0700347#endif /* __USBIP_COMMON_H */