GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / usb / usbip / usbip_common.h
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
44 enum {
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
71 extern unsigned long usbip_debug_flag;
72 extern 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  * Arbitrary limit for the maximum number of isochronous packets in an URB,
139  * compare for example the uhci_submit_isochronous function in
140  * drivers/usb/host/uhci-q.c
141  */
142 #define USBIP_MAX_ISO_PACKETS 1024
143
144 /**
145  * struct usbip_header_basic - data pertinent to every request
146  * @command: the usbip request type
147  * @seqnum: sequential number that identifies requests; incremented per
148  *          connection
149  * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
150  *         in the stub driver, this value is ((busnum << 16) | devnum)
151  * @direction: direction of the transfer
152  * @ep: endpoint number
153  */
154 struct usbip_header_basic {
155         __u32 command;
156         __u32 seqnum;
157         __u32 devid;
158         __u32 direction;
159         __u32 ep;
160 } __packed;
161
162 /**
163  * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
164  * @transfer_flags: URB flags
165  * @transfer_buffer_length: the data size for (in) or (out) transfer
166  * @start_frame: initial frame for isochronous or interrupt transfers
167  * @number_of_packets: number of isochronous packets
168  * @interval: maximum time for the request on the server-side host controller
169  * @setup: setup data for a control request
170  */
171 struct usbip_header_cmd_submit {
172         __u32 transfer_flags;
173         __s32 transfer_buffer_length;
174
175         /* it is difficult for usbip to sync frames (reserved only?) */
176         __s32 start_frame;
177         __s32 number_of_packets;
178         __s32 interval;
179
180         unsigned char setup[8];
181 } __packed;
182
183 /**
184  * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
185  * @status: return status of a non-iso request
186  * @actual_length: number of bytes transferred
187  * @start_frame: initial frame for isochronous or interrupt transfers
188  * @number_of_packets: number of isochronous packets
189  * @error_count: number of errors for isochronous transfers
190  */
191 struct usbip_header_ret_submit {
192         __s32 status;
193         __s32 actual_length;
194         __s32 start_frame;
195         __s32 number_of_packets;
196         __s32 error_count;
197 } __packed;
198
199 /**
200  * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
201  * @seqnum: the URB seqnum to unlink
202  */
203 struct usbip_header_cmd_unlink {
204         __u32 seqnum;
205 } __packed;
206
207 /**
208  * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
209  * @status: return status of the request
210  */
211 struct usbip_header_ret_unlink {
212         __s32 status;
213 } __packed;
214
215 /**
216  * struct usbip_header - common header for all usbip packets
217  * @base: the basic header
218  * @u: packet type dependent header
219  */
220 struct usbip_header {
221         struct usbip_header_basic base;
222
223         union {
224                 struct usbip_header_cmd_submit  cmd_submit;
225                 struct usbip_header_ret_submit  ret_submit;
226                 struct usbip_header_cmd_unlink  cmd_unlink;
227                 struct usbip_header_ret_unlink  ret_unlink;
228         } u;
229 } __packed;
230
231 /*
232  * This is the same as usb_iso_packet_descriptor but packed for pdu.
233  */
234 struct usbip_iso_packet_descriptor {
235         __u32 offset;
236         __u32 length;                   /* expected length */
237         __u32 actual_length;
238         __u32 status;
239 } __packed;
240
241 enum usbip_side {
242         USBIP_VHCI,
243         USBIP_STUB,
244 };
245
246 /* event handler */
247 #define USBIP_EH_SHUTDOWN       (1 << 0)
248 #define USBIP_EH_BYE            (1 << 1)
249 #define USBIP_EH_RESET          (1 << 2)
250 #define USBIP_EH_UNUSABLE       (1 << 3)
251
252 #define SDEV_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
253 #define SDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
254 #define SDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
255 #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
256 #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
257
258 #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
259 #define VDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
260 #define VDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
261 #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
262
263 /* a common structure for stub_device and vhci_device */
264 struct usbip_device {
265         enum usbip_side side;
266         enum usbip_device_status status;
267
268         /* lock for status */
269         spinlock_t lock;
270
271         int sockfd;
272         struct socket *tcp_socket;
273
274         struct task_struct *tcp_rx;
275         struct task_struct *tcp_tx;
276
277         unsigned long event;
278         struct task_struct *eh;
279         wait_queue_head_t eh_waitq;
280
281         struct eh_ops {
282                 void (*shutdown)(struct usbip_device *);
283                 void (*reset)(struct usbip_device *);
284                 void (*unusable)(struct usbip_device *);
285         } eh_ops;
286 };
287
288 #define kthread_get_run(threadfn, data, namefmt, ...)                      \
289 ({                                                                         \
290         struct task_struct *__k                                            \
291                 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
292         if (!IS_ERR(__k)) {                                                \
293                 get_task_struct(__k);                                      \
294                 wake_up_process(__k);                                      \
295         }                                                                  \
296         __k;                                                               \
297 })
298
299 #define kthread_stop_put(k)             \
300         do {                            \
301                 kthread_stop(k);        \
302                 put_task_struct(k);     \
303         } while (0)
304
305 /* usbip_common.c */
306 void usbip_dump_urb(struct urb *purb);
307 void usbip_dump_header(struct usbip_header *pdu);
308
309 int usbip_recv(struct socket *sock, void *buf, int size);
310
311 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
312                     int pack);
313 void usbip_header_correct_endian(struct usbip_header *pdu, int send);
314
315 struct usbip_iso_packet_descriptor*
316 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
317
318 /* some members of urb must be substituted before. */
319 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
320 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
321 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
322
323 /* usbip_event.c */
324 int usbip_start_eh(struct usbip_device *ud);
325 void usbip_stop_eh(struct usbip_device *ud);
326 void usbip_event_add(struct usbip_device *ud, unsigned long event);
327 int usbip_event_happened(struct usbip_device *ud);
328
329 static inline int interface_to_busnum(struct usb_interface *interface)
330 {
331         struct usb_device *udev = interface_to_usbdev(interface);
332
333         return udev->bus->busnum;
334 }
335
336 static inline int interface_to_devnum(struct usb_interface *interface)
337 {
338         struct usb_device *udev = interface_to_usbdev(interface);
339
340         return udev->devnum;
341 }
342
343 #endif /* __USBIP_COMMON_H */