usbip_common.h 11 KB

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