Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / staging / 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
33 #define USBIP_VERSION "1.0.0"
34
35 #undef pr_fmt
36
37 #ifdef DEBUG
38 #define pr_fmt(fmt)     KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
39 #else
40 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
41 #endif
42
43 enum {
44         usbip_debug_xmit        = (1 << 0),
45         usbip_debug_sysfs       = (1 << 1),
46         usbip_debug_urb         = (1 << 2),
47         usbip_debug_eh          = (1 << 3),
48
49         usbip_debug_stub_cmp    = (1 << 8),
50         usbip_debug_stub_dev    = (1 << 9),
51         usbip_debug_stub_rx     = (1 << 10),
52         usbip_debug_stub_tx     = (1 << 11),
53
54         usbip_debug_vhci_rh     = (1 << 8),
55         usbip_debug_vhci_hc     = (1 << 9),
56         usbip_debug_vhci_rx     = (1 << 10),
57         usbip_debug_vhci_tx     = (1 << 11),
58         usbip_debug_vhci_sysfs  = (1 << 12)
59 };
60
61 #define usbip_dbg_flag_xmit     (usbip_debug_flag & usbip_debug_xmit)
62 #define usbip_dbg_flag_vhci_rh  (usbip_debug_flag & usbip_debug_vhci_rh)
63 #define usbip_dbg_flag_vhci_hc  (usbip_debug_flag & usbip_debug_vhci_hc)
64 #define usbip_dbg_flag_vhci_rx  (usbip_debug_flag & usbip_debug_vhci_rx)
65 #define usbip_dbg_flag_vhci_tx  (usbip_debug_flag & usbip_debug_vhci_tx)
66 #define usbip_dbg_flag_stub_rx  (usbip_debug_flag & usbip_debug_stub_rx)
67 #define usbip_dbg_flag_stub_tx  (usbip_debug_flag & usbip_debug_stub_tx)
68 #define usbip_dbg_flag_vhci_sysfs   (usbip_debug_flag & usbip_debug_vhci_sysfs)
69
70 extern unsigned long usbip_debug_flag;
71 extern struct device_attribute dev_attr_usbip_debug;
72
73 #define usbip_dbg_with_flag(flag, fmt, args...)         \
74         do {                                            \
75                 if (flag & usbip_debug_flag)            \
76                         pr_debug(fmt, ##args);          \
77         } while (0)
78
79 #define usbip_dbg_sysfs(fmt, args...) \
80         usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
81 #define usbip_dbg_xmit(fmt, args...) \
82         usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
83 #define usbip_dbg_urb(fmt, args...) \
84         usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
85 #define usbip_dbg_eh(fmt, args...) \
86         usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
87
88 #define usbip_dbg_vhci_rh(fmt, args...) \
89         usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
90 #define usbip_dbg_vhci_hc(fmt, args...) \
91         usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
92 #define usbip_dbg_vhci_rx(fmt, args...) \
93         usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
94 #define usbip_dbg_vhci_tx(fmt, args...) \
95         usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
96 #define usbip_dbg_vhci_sysfs(fmt, args...) \
97         usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
98
99 #define usbip_dbg_stub_cmp(fmt, args...) \
100         usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
101 #define usbip_dbg_stub_rx(fmt, args...) \
102         usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
103 #define usbip_dbg_stub_tx(fmt, args...) \
104         usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
105
106 /*
107  * USB/IP request headers.
108  * Currently, we define 4 request types:
109  *
110  *  - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
111  *    (client to server)
112  *  - RET_RETURN transfers the result of CMD_SUBMIT.
113  *    (server to client)
114  *  - CMD_UNLINK transfers an unlink request of a pending USB request.
115  *    (client to server)
116  *  - RET_UNLINK transfers the result of CMD_UNLINK.
117  *    (server to client)
118  *
119  * Note: The below request formats are based on the USB subsystem of Linux. Its
120  * details will be defined when other implementations come.
121  *
122  *
123  */
124
125 /*
126  * A basic header followed by other additional headers.
127  */
128 struct usbip_header_basic {
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         __u32 command;
134
135          /* sequential number which identifies requests.
136           * incremented per connections */
137         __u32 seqnum;
138
139         /* devid is used to specify a remote USB device uniquely instead
140          * of busnum and devnum in Linux. In the case of Linux stub_driver,
141          * this value is ((busnum << 16) | devnum) */
142         __u32 devid;
143
144 #define USBIP_DIR_OUT   0
145 #define USBIP_DIR_IN    1
146         __u32 direction;
147         __u32 ep;     /* endpoint number */
148 } __packed;
149
150 /*
151  * An additional header for a CMD_SUBMIT packet.
152  */
153 struct usbip_header_cmd_submit {
154         /* these values are basically the same as in a URB. */
155
156         /* the same in a URB. */
157         __u32 transfer_flags;
158
159         /* set the following data size (out),
160          * or expected reading data size (in) */
161         __s32 transfer_buffer_length;
162
163         /* it is difficult for usbip to sync frames (reserved only?) */
164         __s32 start_frame;
165
166         /* the number of iso descriptors that follows this header */
167         __s32 number_of_packets;
168
169         /* the maximum time within which this request works in a host
170          * controller of a server side */
171         __s32 interval;
172
173         /* set setup packet data for a CTRL request */
174         unsigned char setup[8];
175 } __packed;
176
177 /*
178  * An additional header for a RET_SUBMIT packet.
179  */
180 struct usbip_header_ret_submit {
181         __s32 status;
182         __s32 actual_length;            /* returned data length */
183         __s32 start_frame;              /* ISO and INT */
184         __s32 number_of_packets;        /* ISO only */
185         __s32 error_count;              /* ISO only */
186 } __packed;
187
188 /*
189  * An additional header for a CMD_UNLINK packet.
190  */
191 struct usbip_header_cmd_unlink {
192         __u32 seqnum;                   /* URB's seqnum that will be unlinked */
193 } __packed;
194
195 /*
196  * An additional header for a RET_UNLINK packet.
197  */
198 struct usbip_header_ret_unlink {
199         __s32 status;
200 } __packed;
201
202 /* the same as usb_iso_packet_descriptor but packed for pdu */
203 struct usbip_iso_packet_descriptor {
204         __u32 offset;
205         __u32 length;                   /* expected length */
206         __u32 actual_length;
207         __u32 status;
208 } __packed;
209
210 /*
211  * All usbip packets use a common header to keep code simple.
212  */
213 struct 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 int usbip_xmit(int, struct socket *, char *, int, int);
225 int usbip_sendmsg(struct socket *, struct msghdr *, int);
226
227 static inline int interface_to_busnum(struct usb_interface *interface)
228 {
229         struct usb_device *udev = interface_to_usbdev(interface);
230         return udev->bus->busnum;
231 }
232
233 static inline int interface_to_devnum(struct usb_interface *interface)
234 {
235         struct usb_device *udev = interface_to_usbdev(interface);
236         return udev->devnum;
237 }
238
239 static inline int interface_to_infnum(struct usb_interface *interface)
240 {
241         return interface->cur_altsetting->desc.bInterfaceNumber;
242 }
243
244 #if 0
245 int setnodelay(struct socket *);
246 int setquickack(struct socket *);
247 int setkeepalive(struct socket *socket);
248 void setreuse(struct socket *);
249 #endif
250
251 struct socket *sockfd_to_socket(unsigned int);
252 int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss);
253
254 void usbip_dump_urb(struct urb *purb);
255 void usbip_dump_header(struct usbip_header *pdu);
256
257 struct usbip_device;
258
259 enum usbip_side {
260         USBIP_VHCI,
261         USBIP_STUB,
262 };
263
264 enum usbip_status {
265         /* sdev is available. */
266         SDEV_ST_AVAILABLE = 0x01,
267         /* sdev is now used. */
268         SDEV_ST_USED,
269         /* sdev is unusable because of a fatal error. */
270         SDEV_ST_ERROR,
271
272         /* vdev does not connect a remote device. */
273         VDEV_ST_NULL,
274         /* vdev is used, but the USB address is not assigned yet */
275         VDEV_ST_NOTASSIGNED,
276         VDEV_ST_USED,
277         VDEV_ST_ERROR
278 };
279
280 /* a common structure for stub_device and vhci_device */
281 struct usbip_device {
282         enum usbip_side side;
283         enum usbip_status status;
284
285         /* lock for status */
286         spinlock_t lock;
287
288         struct socket *tcp_socket;
289
290         struct task_struct *tcp_rx;
291         struct task_struct *tcp_tx;
292
293         /* event handler */
294 #define USBIP_EH_SHUTDOWN       (1 << 0)
295 #define USBIP_EH_BYE            (1 << 1)
296 #define USBIP_EH_RESET          (1 << 2)
297 #define USBIP_EH_UNUSABLE       (1 << 3)
298
299 #define SDEV_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
300 #define SDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
301 #define SDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
302 #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
303 #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
304
305 #define VDEV_EVENT_REMOVED      (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
306 #define VDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
307 #define VDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
308 #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
309
310         unsigned long event;
311         struct task_struct *eh;
312         wait_queue_head_t eh_waitq;
313
314         struct eh_ops {
315                 void (*shutdown)(struct usbip_device *);
316                 void (*reset)(struct usbip_device *);
317                 void (*unusable)(struct usbip_device *);
318         } eh_ops;
319 };
320
321 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
322                     int pack);
323
324 void usbip_header_correct_endian(struct usbip_header *pdu, int send);
325 /* some members of urb must be substituted before. */
326 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
327 /* some members of urb must be substituted before. */
328 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
329 /* some members of urb must be substituted before. */
330 int usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
331 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
332
333 /* usbip_event.c */
334 int usbip_start_eh(struct usbip_device *ud);
335 void usbip_stop_eh(struct usbip_device *ud);
336 void usbip_event_add(struct usbip_device *ud, unsigned long event);
337 int usbip_event_happened(struct usbip_device *ud);
338
339 #endif /* __USBIP_COMMON_H */