Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3 *
  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.
  8 *
  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.
 13 *
 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,
 17 * USA.
 18 */
 19
 20#ifndef __USBIP_COMMON_H
 21#define __USBIP_COMMON_H
 22
 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>
 
 32#include <uapi/linux/usbip.h>
 33
 34#define USBIP_VERSION "1.0.0"
 35
 36#undef pr_fmt
 37
 38#ifdef DEBUG
 39#define pr_fmt(fmt)     KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
 40#else
 41#define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
 42#endif
 43
 44enum {
 45	usbip_debug_xmit	= (1 << 0),
 46	usbip_debug_sysfs	= (1 << 1),
 47	usbip_debug_urb		= (1 << 2),
 48	usbip_debug_eh		= (1 << 3),
 49
 50	usbip_debug_stub_cmp	= (1 << 8),
 51	usbip_debug_stub_dev	= (1 << 9),
 52	usbip_debug_stub_rx	= (1 << 10),
 53	usbip_debug_stub_tx	= (1 << 11),
 54
 55	usbip_debug_vhci_rh	= (1 << 8),
 56	usbip_debug_vhci_hc	= (1 << 9),
 57	usbip_debug_vhci_rx	= (1 << 10),
 58	usbip_debug_vhci_tx	= (1 << 11),
 59	usbip_debug_vhci_sysfs  = (1 << 12)
 60};
 61
 62#define usbip_dbg_flag_xmit	(usbip_debug_flag & usbip_debug_xmit)
 63#define usbip_dbg_flag_vhci_rh	(usbip_debug_flag & usbip_debug_vhci_rh)
 64#define usbip_dbg_flag_vhci_hc	(usbip_debug_flag & usbip_debug_vhci_hc)
 65#define usbip_dbg_flag_vhci_rx	(usbip_debug_flag & usbip_debug_vhci_rx)
 66#define usbip_dbg_flag_vhci_tx	(usbip_debug_flag & usbip_debug_vhci_tx)
 67#define usbip_dbg_flag_stub_rx	(usbip_debug_flag & usbip_debug_stub_rx)
 68#define usbip_dbg_flag_stub_tx	(usbip_debug_flag & usbip_debug_stub_tx)
 69#define usbip_dbg_flag_vhci_sysfs  (usbip_debug_flag & usbip_debug_vhci_sysfs)
 70
 71extern unsigned long usbip_debug_flag;
 72extern struct device_attribute dev_attr_usbip_debug;
 73
 74#define usbip_dbg_with_flag(flag, fmt, args...)		\
 75	do {						\
 76		if (flag & usbip_debug_flag)		\
 77			pr_debug(fmt, ##args);		\
 78	} while (0)
 79
 80#define usbip_dbg_sysfs(fmt, args...) \
 81	usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
 82#define usbip_dbg_xmit(fmt, args...) \
 83	usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
 84#define usbip_dbg_urb(fmt, args...) \
 85	usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
 86#define usbip_dbg_eh(fmt, args...) \
 87	usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
 88
 89#define usbip_dbg_vhci_rh(fmt, args...)	\
 90	usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
 91#define usbip_dbg_vhci_hc(fmt, args...)	\
 92	usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
 93#define usbip_dbg_vhci_rx(fmt, args...)	\
 94	usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
 95#define usbip_dbg_vhci_tx(fmt, args...)	\
 96	usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
 97#define usbip_dbg_vhci_sysfs(fmt, args...) \
 98	usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
 99
100#define usbip_dbg_stub_cmp(fmt, args...) \
101	usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
102#define usbip_dbg_stub_rx(fmt, args...) \
103	usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
104#define usbip_dbg_stub_tx(fmt, args...) \
105	usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
106
107/*
108 * USB/IP request headers
109 *
110 * Each request is transferred across the network to its counterpart, which
111 * facilitates the normal USB communication. The values contained in the headers
112 * are basically the same as in a URB. Currently, four request types are
113 * defined:
114 *
115 *  - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
116 *    (client to server)
117 *
118 *  - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
119 *    (server to client)
120 *
121 *  - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
122 *    corresponds to usb_unlink_urb()
123 *    (client to server)
124 *
125 *  - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
126 *    (server to client)
127 *
128 */
129#define USBIP_CMD_SUBMIT	0x0001
130#define USBIP_CMD_UNLINK	0x0002
131#define USBIP_RET_SUBMIT	0x0003
132#define USBIP_RET_UNLINK	0x0004
133
134#define USBIP_DIR_OUT	0x00
135#define USBIP_DIR_IN	0x01
136
 
 
 
 
 
 
 
137/**
138 * struct usbip_header_basic - data pertinent to every request
139 * @command: the usbip request type
140 * @seqnum: sequential number that identifies requests; incremented per
141 *	    connection
142 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
143 *	   in the stub driver, this value is ((busnum << 16) | devnum)
144 * @direction: direction of the transfer
145 * @ep: endpoint number
146 */
147struct usbip_header_basic {
148	__u32 command;
149	__u32 seqnum;
150	__u32 devid;
151	__u32 direction;
152	__u32 ep;
153} __packed;
154
155/**
156 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
157 * @transfer_flags: URB flags
158 * @transfer_buffer_length: the data size for (in) or (out) transfer
159 * @start_frame: initial frame for isochronous or interrupt transfers
160 * @number_of_packets: number of isochronous packets
161 * @interval: maximum time for the request on the server-side host controller
162 * @setup: setup data for a control request
163 */
164struct usbip_header_cmd_submit {
165	__u32 transfer_flags;
166	__s32 transfer_buffer_length;
167
168	/* it is difficult for usbip to sync frames (reserved only?) */
169	__s32 start_frame;
170	__s32 number_of_packets;
171	__s32 interval;
172
173	unsigned char setup[8];
174} __packed;
175
176/**
177 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
178 * @status: return status of a non-iso request
179 * @actual_length: number of bytes transferred
180 * @start_frame: initial frame for isochronous or interrupt transfers
181 * @number_of_packets: number of isochronous packets
182 * @error_count: number of errors for isochronous transfers
183 */
184struct usbip_header_ret_submit {
185	__s32 status;
186	__s32 actual_length;
187	__s32 start_frame;
188	__s32 number_of_packets;
189	__s32 error_count;
190} __packed;
191
192/**
193 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
194 * @seqnum: the URB seqnum to unlink
195 */
196struct usbip_header_cmd_unlink {
197	__u32 seqnum;
198} __packed;
199
200/**
201 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
202 * @status: return status of the request
203 */
204struct usbip_header_ret_unlink {
205	__s32 status;
206} __packed;
207
208/**
209 * struct usbip_header - common header for all usbip packets
210 * @base: the basic header
211 * @u: packet type dependent header
212 */
213struct usbip_header {
214	struct usbip_header_basic base;
215
216	union {
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;
221	} u;
222} __packed;
223
224/*
225 * This is the same as usb_iso_packet_descriptor but packed for pdu.
226 */
227struct usbip_iso_packet_descriptor {
228	__u32 offset;
229	__u32 length;			/* expected length */
230	__u32 actual_length;
231	__u32 status;
232} __packed;
233
234enum usbip_side {
235	USBIP_VHCI,
236	USBIP_STUB,
 
237};
238
239/* event handler */
240#define USBIP_EH_SHUTDOWN	(1 << 0)
241#define USBIP_EH_BYE		(1 << 1)
242#define USBIP_EH_RESET		(1 << 2)
243#define USBIP_EH_UNUSABLE	(1 << 3)
244
245#define SDEV_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
246#define	SDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
247#define	SDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
248#define	SDEV_EVENT_ERROR_SUBMIT	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
249#define	SDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
250
251#define	VDEV_EVENT_REMOVED	(USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
 
 
 
 
 
 
 
252#define	VDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
253#define	VDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
254#define	VDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
255
256/* a common structure for stub_device and vhci_device */
257struct usbip_device {
258	enum usbip_side side;
259	enum usbip_device_status status;
260
261	/* lock for status */
262	spinlock_t lock;
263
 
264	struct socket *tcp_socket;
265
266	struct task_struct *tcp_rx;
267	struct task_struct *tcp_tx;
268
269	unsigned long event;
270	struct task_struct *eh;
271	wait_queue_head_t eh_waitq;
272
273	struct eh_ops {
274		void (*shutdown)(struct usbip_device *);
275		void (*reset)(struct usbip_device *);
276		void (*unusable)(struct usbip_device *);
277	} eh_ops;
278};
279
280#define kthread_get_run(threadfn, data, namefmt, ...)			   \
281({									   \
282	struct task_struct *__k						   \
283		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
284	if (!IS_ERR(__k)) {						   \
285		get_task_struct(__k);					   \
286		wake_up_process(__k);					   \
287	}								   \
288	__k;								   \
289})
290
291#define kthread_stop_put(k)		\
292	do {				\
293		kthread_stop(k);	\
294		put_task_struct(k);	\
295	} while (0)
296
297/* usbip_common.c */
298void usbip_dump_urb(struct urb *purb);
299void usbip_dump_header(struct usbip_header *pdu);
300
301int usbip_recv(struct socket *sock, void *buf, int size);
302
303void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
304		    int pack);
305void usbip_header_correct_endian(struct usbip_header *pdu, int send);
306
307struct usbip_iso_packet_descriptor*
308usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
309
310/* some members of urb must be substituted before. */
311int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
312void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
313int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
314
315/* usbip_event.c */
 
 
316int usbip_start_eh(struct usbip_device *ud);
317void usbip_stop_eh(struct usbip_device *ud);
318void usbip_event_add(struct usbip_device *ud, unsigned long event);
319int usbip_event_happened(struct usbip_device *ud);
 
320
321static inline int interface_to_busnum(struct usb_interface *interface)
322{
323	struct usb_device *udev = interface_to_usbdev(interface);
324
325	return udev->bus->busnum;
326}
327
328static inline int interface_to_devnum(struct usb_interface *interface)
329{
330	struct usb_device *udev = interface_to_usbdev(interface);
331
332	return udev->devnum;
333}
334
335#endif /* __USBIP_COMMON_H */
v5.4
  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 * Arbitrary limit for the maximum number of isochronous packets in an URB,
126 * compare for example the uhci_submit_isochronous function in
127 * drivers/usb/host/uhci-q.c
128 */
129#define USBIP_MAX_ISO_PACKETS 1024
130
131/**
132 * struct usbip_header_basic - data pertinent to every request
133 * @command: the usbip request type
134 * @seqnum: sequential number that identifies requests; incremented per
135 *	    connection
136 * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
137 *	   in the stub driver, this value is ((busnum << 16) | devnum)
138 * @direction: direction of the transfer
139 * @ep: endpoint number
140 */
141struct usbip_header_basic {
142	__u32 command;
143	__u32 seqnum;
144	__u32 devid;
145	__u32 direction;
146	__u32 ep;
147} __packed;
148
149/**
150 * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
151 * @transfer_flags: URB flags
152 * @transfer_buffer_length: the data size for (in) or (out) transfer
153 * @start_frame: initial frame for isochronous or interrupt transfers
154 * @number_of_packets: number of isochronous packets
155 * @interval: maximum time for the request on the server-side host controller
156 * @setup: setup data for a control request
157 */
158struct usbip_header_cmd_submit {
159	__u32 transfer_flags;
160	__s32 transfer_buffer_length;
161
162	/* it is difficult for usbip to sync frames (reserved only?) */
163	__s32 start_frame;
164	__s32 number_of_packets;
165	__s32 interval;
166
167	unsigned char setup[8];
168} __packed;
169
170/**
171 * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
172 * @status: return status of a non-iso request
173 * @actual_length: number of bytes transferred
174 * @start_frame: initial frame for isochronous or interrupt transfers
175 * @number_of_packets: number of isochronous packets
176 * @error_count: number of errors for isochronous transfers
177 */
178struct usbip_header_ret_submit {
179	__s32 status;
180	__s32 actual_length;
181	__s32 start_frame;
182	__s32 number_of_packets;
183	__s32 error_count;
184} __packed;
185
186/**
187 * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
188 * @seqnum: the URB seqnum to unlink
189 */
190struct usbip_header_cmd_unlink {
191	__u32 seqnum;
192} __packed;
193
194/**
195 * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
196 * @status: return status of the request
197 */
198struct usbip_header_ret_unlink {
199	__s32 status;
200} __packed;
201
202/**
203 * struct usbip_header - common header for all usbip packets
204 * @base: the basic header
205 * @u: packet type dependent header
206 */
207struct usbip_header {
208	struct usbip_header_basic base;
209
210	union {
211		struct usbip_header_cmd_submit	cmd_submit;
212		struct usbip_header_ret_submit	ret_submit;
213		struct usbip_header_cmd_unlink	cmd_unlink;
214		struct usbip_header_ret_unlink	ret_unlink;
215	} u;
216} __packed;
217
218/*
219 * This is the same as usb_iso_packet_descriptor but packed for pdu.
220 */
221struct usbip_iso_packet_descriptor {
222	__u32 offset;
223	__u32 length;			/* expected length */
224	__u32 actual_length;
225	__u32 status;
226} __packed;
227
228enum usbip_side {
229	USBIP_VHCI,
230	USBIP_STUB,
231	USBIP_VUDC,
232};
233
234/* event handler */
235#define USBIP_EH_SHUTDOWN	(1 << 0)
236#define USBIP_EH_BYE		(1 << 1)
237#define USBIP_EH_RESET		(1 << 2)
238#define USBIP_EH_UNUSABLE	(1 << 3)
239
240#define	SDEV_EVENT_REMOVED	(USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
241#define	SDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
242#define	SDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
243#define	SDEV_EVENT_ERROR_SUBMIT	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
244#define	SDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
245
246#define VUDC_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
247#define	VUDC_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
248#define	VUDC_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
249/* catastrophic emulated usb error */
250#define	VUDC_EVENT_ERROR_USB	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
251#define	VUDC_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
252
253#define	VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
254#define	VDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
255#define	VDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
256#define	VDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
257
258/* a common structure for stub_device and vhci_device */
259struct usbip_device {
260	enum usbip_side side;
261	enum usbip_device_status status;
262
263	/* lock for status */
264	spinlock_t lock;
265
266	int sockfd;
267	struct socket *tcp_socket;
268
269	struct task_struct *tcp_rx;
270	struct task_struct *tcp_tx;
271
272	unsigned long event;
 
273	wait_queue_head_t eh_waitq;
274
275	struct eh_ops {
276		void (*shutdown)(struct usbip_device *);
277		void (*reset)(struct usbip_device *);
278		void (*unusable)(struct usbip_device *);
279	} eh_ops;
280};
281
282#define kthread_get_run(threadfn, data, namefmt, ...)			   \
283({									   \
284	struct task_struct *__k						   \
285		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
286	if (!IS_ERR(__k)) {						   \
287		get_task_struct(__k);					   \
288		wake_up_process(__k);					   \
289	}								   \
290	__k;								   \
291})
292
293#define kthread_stop_put(k)		\
294	do {				\
295		kthread_stop(k);	\
296		put_task_struct(k);	\
297	} while (0)
298
299/* usbip_common.c */
300void usbip_dump_urb(struct urb *purb);
301void usbip_dump_header(struct usbip_header *pdu);
302
303int usbip_recv(struct socket *sock, void *buf, int size);
304
305void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
306		    int pack);
307void usbip_header_correct_endian(struct usbip_header *pdu, int send);
308
309struct usbip_iso_packet_descriptor*
310usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
311
312/* some members of urb must be substituted before. */
313int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
314void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
315int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
316
317/* usbip_event.c */
318int usbip_init_eh(void);
319void usbip_finish_eh(void);
320int usbip_start_eh(struct usbip_device *ud);
321void usbip_stop_eh(struct usbip_device *ud);
322void usbip_event_add(struct usbip_device *ud, unsigned long event);
323int usbip_event_happened(struct usbip_device *ud);
324int usbip_in_eh(struct task_struct *task);
325
326static inline int interface_to_busnum(struct usb_interface *interface)
327{
328	struct usb_device *udev = interface_to_usbdev(interface);
329
330	return udev->bus->busnum;
331}
332
333static inline int interface_to_devnum(struct usb_interface *interface)
334{
335	struct usb_device *udev = interface_to_usbdev(interface);
336
337	return udev->devnum;
338}
339
340#endif /* __USBIP_COMMON_H */