usbip_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. * Copyright (C) 2015-2016 Samsung Electronics
  4. * Krzysztof Opasiak <k.opasiak@samsung.com>
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. #ifndef __USBIP_COMMON_H
  22. #define __USBIP_COMMON_H
  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. #define USBIP_VERSION "1.0.0"
  34. #undef pr_fmt
  35. #ifdef DEBUG
  36. #define pr_fmt(fmt) KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
  37. #else
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #endif
  40. enum {
  41. usbip_debug_xmit = (1 << 0),
  42. usbip_debug_sysfs = (1 << 1),
  43. usbip_debug_urb = (1 << 2),
  44. usbip_debug_eh = (1 << 3),
  45. usbip_debug_stub_cmp = (1 << 8),
  46. usbip_debug_stub_dev = (1 << 9),
  47. usbip_debug_stub_rx = (1 << 10),
  48. usbip_debug_stub_tx = (1 << 11),
  49. usbip_debug_vhci_rh = (1 << 8),
  50. usbip_debug_vhci_hc = (1 << 9),
  51. usbip_debug_vhci_rx = (1 << 10),
  52. usbip_debug_vhci_tx = (1 << 11),
  53. usbip_debug_vhci_sysfs = (1 << 12)
  54. };
  55. #define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
  56. #define usbip_dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
  57. #define usbip_dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
  58. #define usbip_dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
  59. #define usbip_dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
  60. #define usbip_dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
  61. #define usbip_dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
  62. #define usbip_dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
  63. extern unsigned long usbip_debug_flag;
  64. extern struct device_attribute dev_attr_usbip_debug;
  65. #define usbip_dbg_with_flag(flag, fmt, args...) \
  66. do { \
  67. if (flag & usbip_debug_flag) \
  68. pr_debug(fmt, ##args); \
  69. } while (0)
  70. #define usbip_dbg_sysfs(fmt, args...) \
  71. usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
  72. #define usbip_dbg_xmit(fmt, args...) \
  73. usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
  74. #define usbip_dbg_urb(fmt, args...) \
  75. usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
  76. #define usbip_dbg_eh(fmt, args...) \
  77. usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
  78. #define usbip_dbg_vhci_rh(fmt, args...) \
  79. usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
  80. #define usbip_dbg_vhci_hc(fmt, args...) \
  81. usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
  82. #define usbip_dbg_vhci_rx(fmt, args...) \
  83. usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
  84. #define usbip_dbg_vhci_tx(fmt, args...) \
  85. usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
  86. #define usbip_dbg_vhci_sysfs(fmt, args...) \
  87. usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
  88. #define usbip_dbg_stub_cmp(fmt, args...) \
  89. usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
  90. #define usbip_dbg_stub_rx(fmt, args...) \
  91. usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
  92. #define usbip_dbg_stub_tx(fmt, args...) \
  93. usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
  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. #define USBIP_DIR_OUT 0x00
  121. #define USBIP_DIR_IN 0x01
  122. /**
  123. * struct usbip_header_basic - data pertinent to every request
  124. * @command: the usbip request type
  125. * @seqnum: sequential number that identifies requests; incremented per
  126. * connection
  127. * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
  128. * in the stub driver, this value is ((busnum << 16) | devnum)
  129. * @direction: direction of the transfer
  130. * @ep: endpoint number
  131. */
  132. struct usbip_header_basic {
  133. __u32 command;
  134. __u32 seqnum;
  135. __u32 devid;
  136. __u32 direction;
  137. __u32 ep;
  138. } __packed;
  139. /**
  140. * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
  141. * @transfer_flags: URB flags
  142. * @transfer_buffer_length: the data size for (in) or (out) transfer
  143. * @start_frame: initial frame for isochronous or interrupt transfers
  144. * @number_of_packets: number of isochronous packets
  145. * @interval: maximum time for the request on the server-side host controller
  146. * @setup: setup data for a control request
  147. */
  148. struct usbip_header_cmd_submit {
  149. __u32 transfer_flags;
  150. __s32 transfer_buffer_length;
  151. /* it is difficult for usbip to sync frames (reserved only?) */
  152. __s32 start_frame;
  153. __s32 number_of_packets;
  154. __s32 interval;
  155. unsigned char setup[8];
  156. } __packed;
  157. /**
  158. * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
  159. * @status: return status of a non-iso request
  160. * @actual_length: number of bytes transferred
  161. * @start_frame: initial frame for isochronous or interrupt transfers
  162. * @number_of_packets: number of isochronous packets
  163. * @error_count: number of errors for isochronous transfers
  164. */
  165. struct usbip_header_ret_submit {
  166. __s32 status;
  167. __s32 actual_length;
  168. __s32 start_frame;
  169. __s32 number_of_packets;
  170. __s32 error_count;
  171. } __packed;
  172. /**
  173. * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
  174. * @seqnum: the URB seqnum to unlink
  175. */
  176. struct usbip_header_cmd_unlink {
  177. __u32 seqnum;
  178. } __packed;
  179. /**
  180. * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
  181. * @status: return status of the request
  182. */
  183. struct usbip_header_ret_unlink {
  184. __s32 status;
  185. } __packed;
  186. /**
  187. * struct usbip_header - common header for all usbip packets
  188. * @base: the basic header
  189. * @u: packet type dependent header
  190. */
  191. struct usbip_header {
  192. struct usbip_header_basic base;
  193. union {
  194. struct usbip_header_cmd_submit cmd_submit;
  195. struct usbip_header_ret_submit ret_submit;
  196. struct usbip_header_cmd_unlink cmd_unlink;
  197. struct usbip_header_ret_unlink ret_unlink;
  198. } u;
  199. } __packed;
  200. /*
  201. * This is the same as usb_iso_packet_descriptor but packed for pdu.
  202. */
  203. struct usbip_iso_packet_descriptor {
  204. __u32 offset;
  205. __u32 length; /* expected length */
  206. __u32 actual_length;
  207. __u32 status;
  208. } __packed;
  209. enum usbip_side {
  210. USBIP_VHCI,
  211. USBIP_STUB,
  212. USBIP_VUDC,
  213. };
  214. /* event handler */
  215. #define USBIP_EH_SHUTDOWN (1 << 0)
  216. #define USBIP_EH_BYE (1 << 1)
  217. #define USBIP_EH_RESET (1 << 2)
  218. #define USBIP_EH_UNUSABLE (1 << 3)
  219. #define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
  220. #define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  221. #define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  222. #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  223. #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  224. #define VUDC_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
  225. #define VUDC_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  226. #define VUDC_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  227. /* catastrophic emulated usb error */
  228. #define VUDC_EVENT_ERROR_USB (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  229. #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  230. #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
  231. #define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  232. #define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
  233. #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
  234. /* a common structure for stub_device and vhci_device */
  235. struct usbip_device {
  236. enum usbip_side side;
  237. enum usbip_device_status status;
  238. /* lock for status */
  239. spinlock_t lock;
  240. int sockfd;
  241. struct socket *tcp_socket;
  242. struct task_struct *tcp_rx;
  243. struct task_struct *tcp_tx;
  244. unsigned long event;
  245. wait_queue_head_t eh_waitq;
  246. struct eh_ops {
  247. void (*shutdown)(struct usbip_device *);
  248. void (*reset)(struct usbip_device *);
  249. void (*unusable)(struct usbip_device *);
  250. } eh_ops;
  251. };
  252. #define kthread_get_run(threadfn, data, namefmt, ...) \
  253. ({ \
  254. struct task_struct *__k \
  255. = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
  256. if (!IS_ERR(__k)) { \
  257. get_task_struct(__k); \
  258. wake_up_process(__k); \
  259. } \
  260. __k; \
  261. })
  262. #define kthread_stop_put(k) \
  263. do { \
  264. kthread_stop(k); \
  265. put_task_struct(k); \
  266. } while (0)
  267. /* usbip_common.c */
  268. void usbip_dump_urb(struct urb *purb);
  269. void usbip_dump_header(struct usbip_header *pdu);
  270. int usbip_recv(struct socket *sock, void *buf, int size);
  271. void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
  272. int pack);
  273. void usbip_header_correct_endian(struct usbip_header *pdu, int send);
  274. struct usbip_iso_packet_descriptor*
  275. usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
  276. /* some members of urb must be substituted before. */
  277. int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
  278. void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
  279. int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
  280. /* usbip_event.c */
  281. int usbip_init_eh(void);
  282. void usbip_finish_eh(void);
  283. int usbip_start_eh(struct usbip_device *ud);
  284. void usbip_stop_eh(struct usbip_device *ud);
  285. void usbip_event_add(struct usbip_device *ud, unsigned long event);
  286. int usbip_event_happened(struct usbip_device *ud);
  287. int usbip_in_eh(struct task_struct *task);
  288. static inline int interface_to_busnum(struct usb_interface *interface)
  289. {
  290. struct usb_device *udev = interface_to_usbdev(interface);
  291. return udev->bus->busnum;
  292. }
  293. static inline int interface_to_devnum(struct usb_interface *interface)
  294. {
  295. struct usb_device *udev = interface_to_usbdev(interface);
  296. return udev->devnum;
  297. }
  298. #endif /* __USBIP_COMMON_H */