usbip_common.h 10.0 KB

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