]>
Commit | Line | Data |
---|---|---|
05a1f28e TH |
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 __VHCI_COMMON_H | |
21 | #define __VHCI_COMMON_H | |
22 | ||
23 | ||
24 | #include <linux/version.h> | |
25 | #include <linux/usb.h> | |
26 | #include <asm/byteorder.h> | |
27 | #include <net/sock.h> | |
28 | ||
29 | /*-------------------------------------------------------------------------*/ | |
30 | ||
31 | /* | |
32 | * define macros to print messages | |
33 | */ | |
34 | ||
35 | /** | |
411a861f | 36 | * usbip_udbg - print debug messages if CONFIG_USB_IP_DEBUG_ENABLE is defined |
05a1f28e TH |
37 | * @fmt: |
38 | * @args: | |
39 | */ | |
40 | ||
411a861f | 41 | #ifdef CONFIG_USB_IP_DEBUG_ENABLE |
05a1f28e | 42 | |
b8868e45 | 43 | #define usbip_udbg(fmt, args...) \ |
05a1f28e TH |
44 | do { \ |
45 | printk(KERN_DEBUG "%-10s:(%s,%d) %s: " fmt, \ | |
46 | (in_interrupt() ? "interrupt" : (current)->comm),\ | |
b8868e45 | 47 | __FILE__, __LINE__, __func__, ##args); \ |
05a1f28e TH |
48 | } while (0) |
49 | ||
411a861f | 50 | #else /* CONFIG_USB_IP_DEBUG_ENABLE */ |
05a1f28e | 51 | |
b8868e45 | 52 | #define usbip_udbg(fmt, args...) do { } while (0) |
05a1f28e | 53 | |
411a861f | 54 | #endif /* CONFIG_USB_IP_DEBUG_ENABLE */ |
05a1f28e TH |
55 | |
56 | ||
57 | enum { | |
58 | usbip_debug_xmit = (1 << 0), | |
59 | usbip_debug_sysfs = (1 << 1), | |
60 | usbip_debug_urb = (1 << 2), | |
61 | usbip_debug_eh = (1 << 3), | |
62 | ||
63 | usbip_debug_stub_cmp = (1 << 8), | |
64 | usbip_debug_stub_dev = (1 << 9), | |
65 | usbip_debug_stub_rx = (1 << 10), | |
66 | usbip_debug_stub_tx = (1 << 11), | |
67 | ||
68 | usbip_debug_vhci_rh = (1 << 8), | |
69 | usbip_debug_vhci_hc = (1 << 9), | |
70 | usbip_debug_vhci_rx = (1 << 10), | |
71 | usbip_debug_vhci_tx = (1 << 11), | |
72 | usbip_debug_vhci_sysfs = (1 << 12) | |
73 | }; | |
74 | ||
b8868e45 BM |
75 | #define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit) |
76 | #define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh) | |
77 | #define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc) | |
78 | #define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx) | |
79 | #define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx) | |
80 | #define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx) | |
81 | #define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx) | |
82 | #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs) | |
05a1f28e TH |
83 | |
84 | extern unsigned long usbip_debug_flag; | |
85 | extern struct device_attribute dev_attr_usbip_debug; | |
86 | ||
b8868e45 | 87 | #define usbip_dbg_with_flag(flag, fmt, args...) \ |
05a1f28e TH |
88 | do { \ |
89 | if (flag & usbip_debug_flag) \ | |
b8868e45 | 90 | usbip_udbg(fmt , ##args); \ |
05a1f28e TH |
91 | } while (0) |
92 | ||
b8868e45 BM |
93 | #define usbip_dbg_sysfs(fmt, args...) \ |
94 | usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args) | |
95 | #define usbip_dbg_xmit(fmt, args...) \ | |
96 | usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args) | |
97 | #define usbip_dbg_urb(fmt, args...) \ | |
98 | usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args) | |
99 | #define usbip_dbg_eh(fmt, args...) \ | |
100 | usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args) | |
101 | ||
102 | #define usbip_dbg_vhci_rh(fmt, args...) \ | |
103 | usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args) | |
104 | #define usbip_dbg_vhci_hc(fmt, args...) \ | |
105 | usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args) | |
106 | #define usbip_dbg_vhci_rx(fmt, args...) \ | |
107 | usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args) | |
108 | #define usbip_dbg_vhci_tx(fmt, args...) \ | |
109 | usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args) | |
110 | #define usbip_dbg_vhci_sysfs(fmt, args...) \ | |
111 | usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args) | |
112 | ||
113 | #define usbip_dbg_stub_cmp(fmt, args...) \ | |
114 | usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args) | |
115 | #define usbip_dbg_stub_rx(fmt, args...) \ | |
116 | usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args) | |
117 | #define usbip_dbg_stub_tx(fmt, args...) \ | |
118 | usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args) | |
05a1f28e TH |
119 | |
120 | ||
121 | /** | |
b8868e45 | 122 | * usbip_uerr - print error messages |
05a1f28e TH |
123 | * @fmt: |
124 | * @args: | |
125 | */ | |
b8868e45 | 126 | #define usbip_uerr(fmt, args...) \ |
05a1f28e TH |
127 | do { \ |
128 | printk(KERN_ERR "%-10s: ***ERROR*** (%s,%d) %s: " fmt, \ | |
129 | (in_interrupt() ? "interrupt" : (current)->comm),\ | |
130 | __FILE__, __LINE__, __func__, ##args); \ | |
131 | } while (0) | |
132 | ||
133 | /** | |
b8868e45 | 134 | * usbip_uinfo - print information messages |
05a1f28e TH |
135 | * @fmt: |
136 | * @args: | |
137 | */ | |
b8868e45 | 138 | #define usbip_uinfo(fmt, args...) \ |
05a1f28e TH |
139 | do { \ |
140 | printk(KERN_INFO "usbip: " fmt , ## args); \ | |
141 | } while (0) | |
142 | ||
143 | ||
144 | /*-------------------------------------------------------------------------*/ | |
145 | ||
146 | /* | |
147 | * USB/IP request headers. | |
148 | * Currently, we define 4 request types: | |
149 | * | |
150 | * - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb(). | |
151 | * (client to server) | |
152 | * - RET_RETURN transfers the result of CMD_SUBMIT. | |
153 | * (server to client) | |
154 | * - CMD_UNLINK transfers an unlink request of a pending USB request. | |
155 | * (client to server) | |
156 | * - RET_UNLINK transfers the result of CMD_UNLINK. | |
157 | * (server to client) | |
158 | * | |
159 | * Note: The below request formats are based on the USB subsystem of Linux. Its | |
160 | * details will be defined when other implementations come. | |
161 | * | |
162 | * | |
163 | */ | |
164 | ||
165 | /* | |
166 | * A basic header followed by other additional headers. | |
167 | */ | |
168 | struct usbip_header_basic { | |
169 | #define USBIP_CMD_SUBMIT 0x0001 | |
170 | #define USBIP_CMD_UNLINK 0x0002 | |
171 | #define USBIP_RET_SUBMIT 0x0003 | |
172 | #define USBIP_RET_UNLINK 0x0004 | |
173 | __u32 command; | |
174 | ||
65cfe842 | 175 | /* sequential number which identifies requests. |
05a1f28e TH |
176 | * incremented per connections */ |
177 | __u32 seqnum; | |
178 | ||
179 | /* devid is used to specify a remote USB device uniquely instead | |
180 | * of busnum and devnum in Linux. In the case of Linux stub_driver, | |
181 | * this value is ((busnum << 16) | devnum) */ | |
182 | __u32 devid; | |
183 | ||
184 | #define USBIP_DIR_OUT 0 | |
1bd3302c | 185 | #define USBIP_DIR_IN 1 |
05a1f28e TH |
186 | __u32 direction; |
187 | __u32 ep; /* endpoint number */ | |
188 | } __attribute__ ((packed)); | |
189 | ||
190 | /* | |
191 | * An additional header for a CMD_SUBMIT packet. | |
192 | */ | |
193 | struct usbip_header_cmd_submit { | |
194 | /* these values are basically the same as in a URB. */ | |
195 | ||
196 | /* the same in a URB. */ | |
197 | __u32 transfer_flags; | |
198 | ||
199 | /* set the following data size (out), | |
200 | * or expected reading data size (in) */ | |
201 | __s32 transfer_buffer_length; | |
202 | ||
203 | /* it is difficult for usbip to sync frames (reserved only?) */ | |
204 | __s32 start_frame; | |
205 | ||
206 | /* the number of iso descriptors that follows this header */ | |
207 | __s32 number_of_packets; | |
208 | ||
209 | /* the maximum time within which this request works in a host | |
210 | * controller of a server side */ | |
211 | __s32 interval; | |
212 | ||
213 | /* set setup packet data for a CTRL request */ | |
214 | unsigned char setup[8]; | |
215 | } __attribute__ ((packed)); | |
216 | ||
217 | /* | |
218 | * An additional header for a RET_SUBMIT packet. | |
219 | */ | |
220 | struct usbip_header_ret_submit { | |
221 | __s32 status; | |
222 | __s32 actual_length; /* returned data length */ | |
223 | __s32 start_frame; /* ISO and INT */ | |
224 | __s32 number_of_packets; /* ISO only */ | |
225 | __s32 error_count; /* ISO only */ | |
226 | } __attribute__ ((packed)); | |
227 | ||
228 | /* | |
229 | * An additional header for a CMD_UNLINK packet. | |
230 | */ | |
231 | struct usbip_header_cmd_unlink { | |
232 | __u32 seqnum; /* URB's seqnum which will be unlinked */ | |
233 | } __attribute__ ((packed)); | |
234 | ||
235 | ||
236 | /* | |
237 | * An additional header for a RET_UNLINK packet. | |
238 | */ | |
239 | struct usbip_header_ret_unlink { | |
240 | __s32 status; | |
241 | } __attribute__ ((packed)); | |
242 | ||
243 | ||
244 | /* the same as usb_iso_packet_descriptor but packed for pdu */ | |
245 | struct usbip_iso_packet_descriptor { | |
246 | __u32 offset; | |
247 | __u32 length; /* expected length */ | |
248 | __u32 actual_length; | |
249 | __u32 status; | |
250 | } __attribute__ ((packed)); | |
251 | ||
252 | ||
253 | /* | |
254 | * All usbip packets use a common header to keep code simple. | |
255 | */ | |
256 | struct usbip_header { | |
257 | struct usbip_header_basic base; | |
258 | ||
259 | union { | |
260 | struct usbip_header_cmd_submit cmd_submit; | |
261 | struct usbip_header_ret_submit ret_submit; | |
262 | struct usbip_header_cmd_unlink cmd_unlink; | |
263 | struct usbip_header_ret_unlink ret_unlink; | |
264 | } u; | |
265 | } __attribute__ ((packed)); | |
266 | ||
267 | ||
268 | ||
269 | ||
270 | /*-------------------------------------------------------------------------*/ | |
271 | ||
272 | ||
273 | int usbip_xmit(int, struct socket *, char *, int, int); | |
274 | int usbip_sendmsg(struct socket *, struct msghdr *, int); | |
275 | ||
276 | ||
277 | static inline int interface_to_busnum(struct usb_interface *interface) | |
278 | { | |
279 | struct usb_device *udev = interface_to_usbdev(interface); | |
280 | return udev->bus->busnum; | |
281 | } | |
282 | ||
283 | static inline int interface_to_devnum(struct usb_interface *interface) | |
284 | { | |
285 | struct usb_device *udev = interface_to_usbdev(interface); | |
286 | return udev->devnum; | |
287 | } | |
288 | ||
289 | static inline int interface_to_infnum(struct usb_interface *interface) | |
290 | { | |
291 | return interface->cur_altsetting->desc.bInterfaceNumber; | |
292 | } | |
293 | ||
294 | #if 0 | |
295 | int setnodelay(struct socket *); | |
296 | int setquickack(struct socket *); | |
297 | int setkeepalive(struct socket *socket); | |
298 | void setreuse(struct socket *); | |
299 | #endif | |
300 | ||
301 | struct socket *sockfd_to_socket(unsigned int); | |
302 | int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss); | |
303 | ||
304 | void usbip_dump_urb(struct urb *purb); | |
305 | void usbip_dump_header(struct usbip_header *pdu); | |
306 | ||
307 | ||
308 | struct usbip_device; | |
309 | ||
310 | struct usbip_task { | |
311 | struct task_struct *thread; | |
312 | struct completion thread_done; | |
313 | char *name; | |
314 | void (*loop_ops)(struct usbip_task *); | |
315 | }; | |
316 | ||
317 | enum usbip_side { | |
318 | USBIP_VHCI, | |
319 | USBIP_STUB, | |
320 | }; | |
321 | ||
322 | enum usbip_status { | |
323 | /* sdev is available. */ | |
324 | SDEV_ST_AVAILABLE = 0x01, | |
325 | /* sdev is now used. */ | |
326 | SDEV_ST_USED, | |
327 | /* sdev is unusable because of a fatal error. */ | |
328 | SDEV_ST_ERROR, | |
329 | ||
330 | /* vdev does not connect a remote device. */ | |
331 | VDEV_ST_NULL, | |
332 | /* vdev is used, but the USB address is not assigned yet */ | |
333 | VDEV_ST_NOTASSIGNED, | |
334 | VDEV_ST_USED, | |
335 | VDEV_ST_ERROR | |
336 | }; | |
337 | ||
338 | /* a common structure for stub_device and vhci_device */ | |
339 | struct usbip_device { | |
340 | enum usbip_side side; | |
341 | ||
342 | enum usbip_status status; | |
343 | ||
344 | /* lock for status */ | |
345 | spinlock_t lock; | |
346 | ||
347 | struct socket *tcp_socket; | |
348 | ||
349 | struct usbip_task tcp_rx; | |
350 | struct usbip_task tcp_tx; | |
351 | ||
352 | /* event handler */ | |
353 | #define USBIP_EH_SHUTDOWN (1 << 0) | |
354 | #define USBIP_EH_BYE (1 << 1) | |
355 | #define USBIP_EH_RESET (1 << 2) | |
356 | #define USBIP_EH_UNUSABLE (1 << 3) | |
357 | ||
b8868e45 | 358 | #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE) |
05a1f28e TH |
359 | #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) |
360 | #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
361 | #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
362 | #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
363 | ||
364 | #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE) | |
365 | #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
366 | #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET) | |
367 | #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE) | |
368 | ||
369 | unsigned long event; | |
370 | struct usbip_task eh; | |
371 | wait_queue_head_t eh_waitq; | |
372 | ||
373 | struct eh_ops { | |
374 | void (*shutdown)(struct usbip_device *); | |
375 | void (*reset)(struct usbip_device *); | |
376 | void (*unusable)(struct usbip_device *); | |
377 | } eh_ops; | |
378 | }; | |
379 | ||
380 | ||
381 | void usbip_task_init(struct usbip_task *ut, char *, | |
382 | void (*loop_ops)(struct usbip_task *)); | |
383 | ||
b8868e45 | 384 | int usbip_start_threads(struct usbip_device *ud); |
05a1f28e TH |
385 | void usbip_stop_threads(struct usbip_device *ud); |
386 | int usbip_thread(void *param); | |
387 | ||
388 | void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, | |
389 | int pack); | |
390 | ||
391 | void usbip_header_correct_endian(struct usbip_header *pdu, int send); | |
392 | /* some members of urb must be substituted before. */ | |
393 | int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb); | |
394 | /* some members of urb must be substituted before. */ | |
395 | int usbip_recv_iso(struct usbip_device *ud, struct urb *urb); | |
396 | void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen); | |
397 | ||
398 | ||
399 | /* usbip_event.c */ | |
b8868e45 | 400 | int usbip_start_eh(struct usbip_device *ud); |
05a1f28e TH |
401 | void usbip_stop_eh(struct usbip_device *ud); |
402 | void usbip_event_add(struct usbip_device *ud, unsigned long event); | |
b8868e45 | 403 | int usbip_event_happened(struct usbip_device *ud); |
05a1f28e TH |
404 | |
405 | ||
406 | #endif |