Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
6 */
7
8#ifndef __USBIP_COMMON_H
9#define __USBIP_COMMON_H
10
11#include <linux/compiler.h>
12#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>
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#ifdef DEBUG
26#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
27#else
28#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29#endif
30
31enum {
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
49#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)
56#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
57
58extern unsigned long usbip_debug_flag;
59extern struct device_attribute dev_attr_usbip_debug;
60
61#define usbip_dbg_with_flag(flag, fmt, args...) \
62 do { \
63 if (flag & usbip_debug_flag) \
64 pr_debug(fmt, ##args); \
65 } while (0)
66
67#define usbip_dbg_sysfs(fmt, args...) \
68 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
69#define usbip_dbg_xmit(fmt, args...) \
70 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
71#define usbip_dbg_urb(fmt, args...) \
72 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
73#define usbip_dbg_eh(fmt, args...) \
74 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
75
76#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)
84#define usbip_dbg_vhci_sysfs(fmt, args...) \
85 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
86
87#define usbip_dbg_stub_cmp(fmt, args...) \
88 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
89#define usbip_dbg_stub_rx(fmt, args...) \
90 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
91#define usbip_dbg_stub_tx(fmt, args...) \
92 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
93
94/*
95 * USB/IP request headers
96 *
97 * 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()
103 * (client to server)
104 *
105 * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
106 * (server to client)
107 *
108 * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
109 * corresponds to usb_unlink_urb()
110 * (client to server)
111 *
112 * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
113 * (server to client)
114 *
115 */
116#define USBIP_CMD_SUBMIT 0x0001
117#define USBIP_CMD_UNLINK 0x0002
118#define USBIP_RET_SUBMIT 0x0003
119#define USBIP_RET_UNLINK 0x0004
120
121#define USBIP_DIR_OUT 0x00
122#define USBIP_DIR_IN 0x01
123
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
133 */
134struct usbip_header_basic {
135 __u32 command;
136 __u32 seqnum;
137 __u32 devid;
138 __u32 direction;
139 __u32 ep;
140} __packed;
141
142/**
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
150 */
151struct usbip_header_cmd_submit {
152 __u32 transfer_flags;
153 __s32 transfer_buffer_length;
154
155 /* it is difficult for usbip to sync frames (reserved only?) */
156 __s32 start_frame;
157 __s32 number_of_packets;
158 __s32 interval;
159
160 unsigned char setup[8];
161} __packed;
162
163/**
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
170 */
171struct usbip_header_ret_submit {
172 __s32 status;
173 __s32 actual_length;
174 __s32 start_frame;
175 __s32 number_of_packets;
176 __s32 error_count;
177} __packed;
178
179/**
180 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
181 * @seqnum: the URB seqnum to unlink
182 */
183struct usbip_header_cmd_unlink {
184 __u32 seqnum;
185} __packed;
186
187/**
188 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
189 * @status: return status of the request
190 */
191struct usbip_header_ret_unlink {
192 __s32 status;
193} __packed;
194
195/**
196 * struct usbip_header - common header for all usbip packets
197 * @base: the basic header
198 * @u: packet type dependent header
199 */
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;
209} __packed;
210
211/*
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;
220
221enum usbip_side {
222 USBIP_VHCI,
223 USBIP_STUB,
224 USBIP_VUDC,
225};
226
227/* event handler */
228#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
233#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
234#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
239#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
246#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | 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
251/* a common structure for stub_device and vhci_device */
252struct usbip_device {
253 enum usbip_side side;
254 enum usbip_device_status status;
255
256 /* lock for status */
257 spinlock_t lock;
258
259 int sockfd;
260 struct socket *tcp_socket;
261
262 struct task_struct *tcp_rx;
263 struct task_struct *tcp_tx;
264
265 unsigned long event;
266 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
275#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
292/* usbip_common.c */
293void usbip_dump_urb(struct urb *purb);
294void usbip_dump_header(struct usbip_header *pdu);
295
296int usbip_recv(struct socket *sock, void *buf, int size);
297
298void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
299 int pack);
300void usbip_header_correct_endian(struct usbip_header *pdu, int send);
301
302struct usbip_iso_packet_descriptor*
303usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
304
305/* some members of urb must be substituted before. */
306int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
307void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
308int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
309
310/* usbip_event.c */
311int usbip_init_eh(void);
312void usbip_finish_eh(void);
313int usbip_start_eh(struct usbip_device *ud);
314void usbip_stop_eh(struct usbip_device *ud);
315void usbip_event_add(struct usbip_device *ud, unsigned long event);
316int usbip_event_happened(struct usbip_device *ud);
317int usbip_in_eh(struct task_struct *task);
318
319static inline int interface_to_busnum(struct usb_interface *interface)
320{
321 struct usb_device *udev = interface_to_usbdev(interface);
322
323 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);
329
330 return udev->devnum;
331}
332
333#endif /* __USBIP_COMMON_H */
1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
5 *
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
22#ifndef __USBIP_COMMON_H
23#define __USBIP_COMMON_H
24
25#include <linux/compiler.h>
26#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>
32#include <linux/usb.h>
33#include <linux/wait.h>
34#include <uapi/linux/usbip.h>
35
36#define USBIP_VERSION "1.0.0"
37
38#undef pr_fmt
39
40#ifdef DEBUG
41#define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
42#else
43#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44#endif
45
46enum {
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
64#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)
71#define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
72
73extern unsigned long usbip_debug_flag;
74extern struct device_attribute dev_attr_usbip_debug;
75
76#define usbip_dbg_with_flag(flag, fmt, args...) \
77 do { \
78 if (flag & usbip_debug_flag) \
79 pr_debug(fmt, ##args); \
80 } while (0)
81
82#define usbip_dbg_sysfs(fmt, args...) \
83 usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
84#define usbip_dbg_xmit(fmt, args...) \
85 usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
86#define usbip_dbg_urb(fmt, args...) \
87 usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
88#define usbip_dbg_eh(fmt, args...) \
89 usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
90
91#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)
99#define usbip_dbg_vhci_sysfs(fmt, args...) \
100 usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
101
102#define usbip_dbg_stub_cmp(fmt, args...) \
103 usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
104#define usbip_dbg_stub_rx(fmt, args...) \
105 usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
106#define usbip_dbg_stub_tx(fmt, args...) \
107 usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
108
109/*
110 * USB/IP request headers
111 *
112 * 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()
118 * (client to server)
119 *
120 * - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
121 * (server to client)
122 *
123 * - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
124 * corresponds to usb_unlink_urb()
125 * (client to server)
126 *
127 * - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
128 * (server to client)
129 *
130 */
131#define USBIP_CMD_SUBMIT 0x0001
132#define USBIP_CMD_UNLINK 0x0002
133#define USBIP_RET_SUBMIT 0x0003
134#define USBIP_RET_UNLINK 0x0004
135
136#define USBIP_DIR_OUT 0x00
137#define USBIP_DIR_IN 0x01
138
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
148 */
149struct usbip_header_basic {
150 __u32 command;
151 __u32 seqnum;
152 __u32 devid;
153 __u32 direction;
154 __u32 ep;
155} __packed;
156
157/**
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
165 */
166struct usbip_header_cmd_submit {
167 __u32 transfer_flags;
168 __s32 transfer_buffer_length;
169
170 /* it is difficult for usbip to sync frames (reserved only?) */
171 __s32 start_frame;
172 __s32 number_of_packets;
173 __s32 interval;
174
175 unsigned char setup[8];
176} __packed;
177
178/**
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
185 */
186struct usbip_header_ret_submit {
187 __s32 status;
188 __s32 actual_length;
189 __s32 start_frame;
190 __s32 number_of_packets;
191 __s32 error_count;
192} __packed;
193
194/**
195 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
196 * @seqnum: the URB seqnum to unlink
197 */
198struct usbip_header_cmd_unlink {
199 __u32 seqnum;
200} __packed;
201
202/**
203 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
204 * @status: return status of the request
205 */
206struct usbip_header_ret_unlink {
207 __s32 status;
208} __packed;
209
210/**
211 * struct usbip_header - common header for all usbip packets
212 * @base: the basic header
213 * @u: packet type dependent header
214 */
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;
224} __packed;
225
226/*
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;
235
236enum usbip_side {
237 USBIP_VHCI,
238 USBIP_STUB,
239 USBIP_VUDC,
240};
241
242/* event handler */
243#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
248#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
249#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
254#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
261#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
266/* a common structure for stub_device and vhci_device */
267struct usbip_device {
268 enum usbip_side side;
269 enum usbip_device_status status;
270
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
279 unsigned long event;
280 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
289#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
306/* usbip_common.c */
307void usbip_dump_urb(struct urb *purb);
308void usbip_dump_header(struct usbip_header *pdu);
309
310int usbip_recv(struct socket *sock, void *buf, int size);
311
312void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
313 int pack);
314void usbip_header_correct_endian(struct usbip_header *pdu, int send);
315
316struct usbip_iso_packet_descriptor*
317usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
318
319/* some members of urb must be substituted before. */
320int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
321void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
322int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
323
324/* usbip_event.c */
325int usbip_init_eh(void);
326void usbip_finish_eh(void);
327int usbip_start_eh(struct usbip_device *ud);
328void usbip_stop_eh(struct usbip_device *ud);
329void usbip_event_add(struct usbip_device *ud, unsigned long event);
330int usbip_event_happened(struct usbip_device *ud);
331int usbip_in_eh(struct task_struct *task);
332
333static inline int interface_to_busnum(struct usb_interface *interface)
334{
335 struct usb_device *udev = interface_to_usbdev(interface);
336
337 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);
343
344 return udev->devnum;
345}
346
347#endif /* __USBIP_COMMON_H */