usbip_common.h 9.9 KB

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